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