Debian Codegraph @

extra/tirpc/libtirpc-1.3.5/src/getnetpath.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2009, Sun Microsystems, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of Sun Microsystems, Inc. nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * Copyright (c) 1989 by Sun Microsystems, Inc.
31 */
32
33 #include <stdio.h>
34 #include <errno.h>
35 #include <netconfig.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <syslog.h>
39
40 /*
41 * internal structure to keep track of a netpath "session"
42 */
43 struct netpath_chain {
44 struct netconfig *ncp; /* an nconf entry */
45 struct netpath_chain *nchain_next; /* next nconf entry allocated */
46 };
47
48
49 struct netpath_vars {
50 int valid; /* token that indicates a valid netpath_vars */
51 void *nc_handlep; /* handle for current netconfig "session" */
52 char *netpath; /* pointer to current view-point in NETPATH */
53 char *netpath_start; /* pointer to start of our copy of NETPATH */
54 struct netpath_chain *ncp_list; /* list of nconfs allocated this session*/
55 };
56
57 #define NP_VALID 0xf00d
58 #define NP_INVALID 0
59
60 char *_get_next_token(char *, int);
61
62
63 /*
64 * A call to setnetpath() establishes a NETPATH "session". setnetpath()
65 * must be called before the first call to getnetpath(). A "handle" is
66 * returned to distinguish the session; this handle should be passed
67 * subsequently to getnetpath(). (Handles are used to allow for nested calls
68 * to setnetpath()).
69 * If setnetpath() is unable to establish a session (due to lack of memory
70 * resources, or the absence of the /etc/netconfig file), a NULL pointer is
71 * returned.
72 */
73
74 void *
75 setnetpath()
76 {
77
78 struct netpath_vars *np_sessionp; /* this session's variables */
79 char *npp; /* NETPATH env variable */
80
81 #ifdef MEM_CHK
82 malloc_debug(1);
83 #endif
84
85 if ((np_sessionp =
86 (struct netpath_vars *)malloc(sizeof (struct netpath_vars))) == NULL) {
87 return (NULL);
88 }
89 if ((np_sessionp->nc_handlep = setnetconfig()) == NULL) {
90 syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
91 free(np_sessionp);
92 return (NULL);
93 }
94 np_sessionp->valid = NP_VALID;
95 np_sessionp->ncp_list = NULL;
96 if ((npp = getenv(NETPATH)) == NULL) {
97 np_sessionp->netpath = NULL;
98 } else {
99 (void) endnetconfig(np_sessionp->nc_handlep);/* won't need nc session*/
100 np_sessionp->nc_handlep = NULL;
101 if ((np_sessionp->netpath = malloc(strlen(npp)+1)) == NULL) {
102 free(np_sessionp);
103 return (NULL);
104 } else {
105 (void) strcpy(np_sessionp->netpath, npp);
106 }
107 }
108 np_sessionp->netpath_start = np_sessionp->netpath;
109 return ((void *)np_sessionp);
110 }
111
112 /*
113 * When first called, getnetpath() returns a pointer to the netconfig
114 * database entry corresponding to the first valid NETPATH component. The
115 * netconfig entry is formatted as a struct netconfig.
116 * On each subsequent call, getnetpath returns a pointer to the netconfig
117 * entry that corresponds to the next valid NETPATH component. getnetpath
118 * can thus be used to search the netconfig database for all networks
119 * included in the NETPATH variable.
120 * When NETPATH has been exhausted, getnetpath() returns NULL. It returns
121 * NULL and sets errno in case of an error (e.g., setnetpath was not called
122 * previously).
123 * getnetpath() silently ignores invalid NETPATH components. A NETPATH
124 * compnent is invalid if there is no corresponding entry in the netconfig
125 * database.
126 * If the NETPATH variable is unset, getnetpath() behaves as if NETPATH
127 * were set to the sequence of default or visible networks in the netconfig
128 * database, in the order in which they are listed.
129 */
130
131 struct netconfig *
132 getnetpath(handlep)
133 void *handlep;
134 {
135 struct netpath_vars *np_sessionp = (struct netpath_vars *)handlep;
136 struct netconfig *ncp = NULL; /* temp. holds a netconfig session */
137 struct netpath_chain *chainp; /* holds chain of ncp's we alloc */
138 char *npp; /* holds current NETPATH */
139
140 if (np_sessionp == NULL || np_sessionp->valid != NP_VALID) {
141 errno = EINVAL;
142 return (NULL);
143 }
144 if (np_sessionp->netpath_start == NULL) { /* NETPATH was not set */
145 do { /* select next visible network */
146 if (np_sessionp->nc_handlep == NULL) {
147 np_sessionp->nc_handlep = setnetconfig();
148 if (np_sessionp->nc_handlep == NULL)
149 syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
150 }
151 if ((ncp = getnetconfig(np_sessionp->nc_handlep)) == NULL) {
152 return(NULL);
153 }
154 } while ((ncp->nc_flag & NC_VISIBLE) == 0);
155 return (ncp);
156 }
157 /*
158 * Find first valid network ID in netpath.
159 */
160 while ((npp = np_sessionp->netpath) != NULL && strlen(npp) != 0) {
161 np_sessionp->netpath = _get_next_token(npp, ':');
162 /*
163 * npp is a network identifier.
164 */
165 if ((ncp = getnetconfigent(npp)) != NULL) {
166 chainp = (struct netpath_chain *) /* cobble alloc chain entry */
167 malloc(sizeof (struct netpath_chain));
168 chainp->ncp = ncp;
169 chainp->nchain_next = NULL;
170 if (np_sessionp->ncp_list == NULL) {
171 np_sessionp->ncp_list = chainp;
172 } else {
173 np_sessionp->ncp_list->nchain_next = chainp;
174 }
175 return (ncp);
176 }
177 /* couldn't find this token in the database; go to next one. */
178 }
179 return (NULL);
180 }
181
182 /*
183 * endnetpath() may be called to unbind NETPATH when processing is complete,
184 * releasing resources for reuse. It returns 0 on success and -1 on failure
185 * (e.g. if setnetpath() was not called previously.
186 */
187 int
188 endnetpath(handlep)
189 void *handlep;
190 {
191 struct netpath_vars *np_sessionp = (struct netpath_vars *)handlep;
192 struct netpath_chain *chainp, *lastp;
193
194 if (np_sessionp == NULL || np_sessionp->valid != NP_VALID) {
195 errno = EINVAL;
196 return (-1);
197 }
198 if (np_sessionp->nc_handlep != NULL)
199 endnetconfig(np_sessionp->nc_handlep);
200 if (np_sessionp->netpath_start != NULL)
201 free(np_sessionp->netpath_start);
202 for (chainp = np_sessionp->ncp_list; chainp != NULL;
203 lastp=chainp, chainp=chainp->nchain_next, free(lastp)) {
204 freenetconfigent(chainp->ncp);
205 }
206 free(np_sessionp);
207 #ifdef MEM_CHK
208 if (malloc_verify() == 0) {
209 fprintf(stderr, "memory heap corrupted in endnetpath\n");
210 exit(1);
211 }
212 #endif
213 return (0);
214 }
215
216
217
218 /*
219 * Returns pointer to the rest-of-the-string after the current token.
220 * The token itself starts at arg, and we null terminate it. We return NULL
221 * if either the arg is empty, or if this is the last token.
222 */
223
224 char *
225 _get_next_token(npp, token)
226 char *npp; /* string */
227 int token; /* char to parse string for */
228 {
229 char *cp; /* char pointer */
230 char *np; /* netpath pointer */
231 char *ep; /* escape pointer */
232
233 if ((cp = strchr(npp, token)) == NULL) {
234 return (NULL);
235 }
236 /*
237 * did find a token, but it might be escaped.
238 */
239 if ((cp > npp) && (cp[-1] == '\\')) {
240 /* if slash was also escaped, carry on, otherwise find next token */
241 if ((cp > npp + 1) && (cp[-2] != '\\')) {
242 /* shift r-o-s onto the escaped token */
243 strcpy(&cp[-1], cp); /* XXX: overlapping string copy */
244 /*
245 * Do a recursive call.
246 * We don't know how many escaped tokens there might be.
247 */
248 return (_get_next_token(cp, token));
249 }
250 }
251
252 *cp++ = '\0'; /* null-terminate token */
253 /* get rid of any backslash escapes */
254 ep = npp;
255 while ((np = strchr(ep, '\\')) != 0) {
256 if (np[1] == '\\')
257 np++;
258 strcpy(np, (ep = &np[1])); /* XXX: overlapping string copy */
259 }
260 return (cp); /* return ptr to r-o-s */
261 }