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