getnetpath.c revision 1.1 1 /* $NetBSD: getnetpath.c,v 1.1 2000/06/02 23:11:11 fvdl Exp $ */
2
3 /*
4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 * unrestricted use provided that this legend is included on all tape
6 * media and as a part of the software program in whole or part. Users
7 * may copy or modify Sun RPC without charge, but are not authorized
8 * to license or distribute it to anyone else except as part of a product or
9 * program developed by the user or with the express written consent of
10 * Sun Microsystems, Inc.
11 *
12 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
13 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
14 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
15 *
16 * Sun RPC is provided with no support and without any obligation on the
17 * part of Sun Microsystems, Inc. to assist in its use, correction,
18 * modification or enhancement.
19 *
20 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
21 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
22 * OR ANY PART THEREOF.
23 *
24 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
25 * or profits or other special, indirect and consequential damages, even if
26 * Sun has been advised of the possibility of such damages.
27 *
28 * Sun Microsystems, Inc.
29 * 2550 Garcia Avenue
30 * Mountain View, California 94043
31 */
32 /*
33 #ifndef lint
34 static char sccsid[] = "@(#)getnetpath.c 1.11 91/12/19 SMI";
35 #endif
36 */
37
38 /*
39 * Copyright (c) 1989 by Sun Microsystems, Inc.
40 */
41
42 #include "namespace.h"
43 #include <sys/cdefs.h>
44 #include <stdio.h>
45 #include <errno.h>
46 #include <netconfig.h>
47 #include <stdlib.h>
48 #include <string.h>
49
50 #ifdef __weak_alias
51 __weak_alias(getnetpath,_getnetpath)
52 __weak_alias(setnetpath,_setnetpath)
53 #endif
54
55 /*
56 * internal structure to keep track of a netpath "session"
57 */
58 struct netpath_chain {
59 struct netconfig *ncp; /* an nconf entry */
60 struct netpath_chain *nchain_next; /* next nconf entry allocated */
61 };
62
63
64 struct netpath_vars {
65 int valid; /* token that indicates a valid netpath_vars */
66 void *nc_handlep; /* handle for current netconfig "session" */
67 char *netpath; /* pointer to current view-point in NETPATH */
68 char *netpath_start; /* pointer to start of our copy of NETPATH */
69 struct netpath_chain *ncp_list; /* list of nconfs allocated this session*/
70 };
71
72 #define NP_VALID 0xf00d
73 #define NP_INVALID 0
74
75 char *_get_next_token __P((char *, char));
76
77
78 /*
79 * A call to setnetpath() establishes a NETPATH "session". setnetpath()
80 * must be called before the first call to getnetpath(). A "handle" is
81 * returned to distinguish the session; this handle should be passed
82 * subsequently to getnetpath(). (Handles are used to allow for nested calls
83 * to setnetpath()).
84 * If setnetpath() is unable to establish a session (due to lack of memory
85 * resources, or the absence of the /etc/netconfig file), a NULL pointer is
86 * returned.
87 */
88
89 void *
90 setnetpath()
91 {
92
93 struct netpath_vars *np_sessionp; /* this session's variables */
94 char *npp; /* NETPATH env variable */
95
96 #ifdef MEM_CHK
97 malloc_debug(1);
98 #endif
99
100 if ((np_sessionp =
101 (struct netpath_vars *)malloc(sizeof (struct netpath_vars))) == NULL) {
102 return (NULL);
103 }
104 if ((np_sessionp->nc_handlep = setnetconfig()) == NULL) {
105 return (NULL);
106 }
107 np_sessionp->valid = NP_VALID;
108 np_sessionp->ncp_list = NULL;
109 if ((npp = getenv(NETPATH)) == (char *)NULL) {
110 np_sessionp->netpath = NULL;
111 } else {
112 (void) endnetconfig(np_sessionp->nc_handlep);/* won't need nc session*/
113 np_sessionp->nc_handlep = NULL;
114 if ((np_sessionp->netpath = malloc(strlen(npp)+1)) == NULL) {
115 free(np_sessionp);
116 return (NULL);
117 } else {
118 (void) strcpy(np_sessionp->netpath, npp);
119 }
120 }
121 np_sessionp->netpath_start = np_sessionp->netpath;
122 return ((void *)np_sessionp);
123 }
124
125 /*
126 * When first called, getnetpath() returns a pointer to the netconfig
127 * database entry corresponding to the first valid NETPATH component. The
128 * netconfig entry is formatted as a struct netconfig.
129 * On each subsequent call, getnetpath returns a pointer to the netconfig
130 * entry that corresponds to the next valid NETPATH component. getnetpath
131 * can thus be used to search the netconfig database for all networks
132 * included in the NETPATH variable.
133 * When NETPATH has been exhausted, getnetpath() returns NULL. It returns
134 * NULL and sets errno in case of an error (e.g., setnetpath was not called
135 * previously).
136 * getnetpath() silently ignores invalid NETPATH components. A NETPATH
137 * compnent is invalid if there is no corresponding entry in the netconfig
138 * database.
139 * If the NETPATH variable is unset, getnetpath() behaves as if NETPATH
140 * were set to the sequence of default or visible networks in the netconfig
141 * database, in the order in which they are listed.
142 */
143
144 struct netconfig *
145 getnetpath(handlep)
146 void *handlep;
147 {
148 struct netpath_vars *np_sessionp = (struct netpath_vars *)handlep;
149 struct netconfig *ncp = NULL; /* temp. holds a netconfig session */
150 struct netpath_chain *chainp; /* holds chain of ncp's we alloc */
151 char *npp; /* holds current NETPATH */
152
153 if (np_sessionp == NULL || np_sessionp->valid != NP_VALID) {
154 errno = EINVAL;
155 return (NULL);
156 }
157 if (np_sessionp->netpath_start == NULL) { /* NETPATH was not set */
158 do { /* select next visible network */
159 if (np_sessionp->nc_handlep == NULL) {
160 np_sessionp->nc_handlep = setnetconfig();
161 }
162 if ((ncp = getnetconfig(np_sessionp->nc_handlep)) == NULL) {
163 return(NULL);
164 }
165 } while ((ncp->nc_flag & NC_VISIBLE) == 0);
166 return (ncp);
167 }
168 /*
169 * Find first valid network ID in netpath.
170 */
171 while ((npp = np_sessionp->netpath) != NULL && strlen(npp) != 0) {
172 np_sessionp->netpath = _get_next_token(npp, ':');
173 /*
174 * npp is a network identifier.
175 */
176 if ((ncp = getnetconfigent(npp)) != NULL) {
177 chainp = (struct netpath_chain *) /* cobble alloc chain entry */
178 malloc(sizeof (struct netpath_chain));
179 chainp->ncp = ncp;
180 chainp->nchain_next = NULL;
181 if (np_sessionp->ncp_list == NULL) {
182 np_sessionp->ncp_list = chainp;
183 } else {
184 np_sessionp->ncp_list->nchain_next = chainp;
185 }
186 return (ncp);
187 }
188 /* couldn't find this token in the database; go to next one. */
189 }
190 return (NULL);
191 }
192
193 /*
194 * endnetpath() may be called to unbind NETPATH when processing is complete,
195 * releasing resources for reuse. It returns 0 on success and -1 on failure
196 * (e.g. if setnetpath() was not called previously.
197 */
198 int
199 endnetpath(handlep)
200 void *handlep;
201 {
202 struct netpath_vars *np_sessionp = (struct netpath_vars *)handlep;
203 struct netpath_chain *chainp, *lastp;
204
205 if (np_sessionp == NULL || np_sessionp->valid != NP_VALID) {
206 errno = EINVAL;
207 return (-1);
208 }
209 if (np_sessionp->nc_handlep != NULL)
210 endnetconfig(np_sessionp->nc_handlep);
211 if (np_sessionp->netpath_start != NULL)
212 free(np_sessionp->netpath_start);
213 for (chainp = np_sessionp->ncp_list; chainp != NULL;
214 lastp=chainp, chainp=chainp->nchain_next, free(lastp)) {
215 freenetconfigent(chainp->ncp);
216 }
217 free(np_sessionp);
218 #ifdef MEM_CHK
219 if (malloc_verify() == 0) {
220 fprintf(stderr, "memory heap corrupted in endnetpath\n");
221 exit(1);
222 }
223 #endif
224 return (0);
225 }
226
227
228
229 /*
230 * Returns pointer to the rest-of-the-string after the current token.
231 * The token itself starts at arg, and we null terminate it. We return NULL
232 * if either the arg is empty, or if this is the last token.
233 */
234
235 char *
236 _get_next_token(npp, token)
237 char *npp; /* string */
238 char token; /* char to parse string for */
239 {
240 char *cp; /* char pointer */
241 char *np; /* netpath pointer */
242 char *ep; /* escape pointer */
243
244 if ((cp = strchr(npp, token)) == NULL) {
245 return (NULL);
246 }
247 /*
248 * did find a token, but it might be escaped.
249 */
250 if (cp[-1] == '\\') {
251 /* if slash was also escaped, carry on, otherwise find next token */
252 if (cp[-2] != '\\') {
253 /* shift r-o-s onto the escaped token */
254 strcpy(&cp[-1], cp); /* XXX: overlapping string copy */
255 /*
256 * Do a recursive call.
257 * We don't know how many escaped tokens there might be.
258 */
259 return (_get_next_token(cp, token));
260 }
261 }
262
263 *cp++ = '\0'; /* null-terminate token */
264 /* get rid of any backslash escapes */
265 ep = npp;
266 while ((np = strchr(ep, '\\')) != 0) {
267 if (np[1] == '\\')
268 np++;
269 strcpy(np, (ep = &np[1])); /* XXX: overlapping string copy */
270 }
271 return (cp); /* return ptr to r-o-s */
272 }
273