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