xref: /freebsd/lib/libc/rpc/getnetpath.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
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 /*
37  * Copyright (c) 1989 by Sun Microsystems, Inc.
38  */
39 
40 #include "namespace.h"
41 #include <stdio.h>
42 #include <errno.h>
43 #include <netconfig.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <syslog.h>
47 #include "un-namespace.h"
48 
49 /*
50  * internal structure to keep track of a netpath "session"
51  */
52 struct netpath_chain {
53     struct netconfig *ncp;  /* an nconf entry */
54     struct netpath_chain *nchain_next;	/* next nconf entry allocated */
55 };
56 
57 
58 struct netpath_vars {
59     int   valid;	    /* token that indicates a valid netpath_vars */
60     void *nc_handlep;	    /* handle for current netconfig "session" */
61     char *netpath;	    /* pointer to current view-point in NETPATH */
62     char *netpath_start;    /* pointer to start of our copy of NETPATH */
63     struct netpath_chain *ncp_list;  /* list of nconfs allocated this session*/
64 };
65 
66 #define NP_VALID	0xf00d
67 #define NP_INVALID	0
68 
69 char *_get_next_token(char *, int);
70 
71 
72 /*
73  * A call to setnetpath() establishes a NETPATH "session".  setnetpath()
74  * must be called before the first call to getnetpath().  A "handle" is
75  * returned to distinguish the session; this handle should be passed
76  * subsequently to getnetpath().  (Handles are used to allow for nested calls
77  * to setnetpath()).
78  * If setnetpath() is unable to establish a session (due to lack of memory
79  * resources, or the absence of the /etc/netconfig file), a NULL pointer is
80  * returned.
81  */
82 
83 void *
84 setnetpath(void)
85 {
86 
87     struct netpath_vars *np_sessionp;   /* this session's variables */
88     char *npp;				/* NETPATH env variable */
89 
90 #ifdef MEM_CHK
91     malloc_debug(1);
92 #endif
93 
94     if ((np_sessionp =
95 	(struct netpath_vars *)malloc(sizeof (struct netpath_vars))) == NULL) {
96 	return (NULL);
97     }
98     if ((np_sessionp->nc_handlep = setnetconfig()) == NULL) {
99 	syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
100 	goto failed;
101     }
102     np_sessionp->valid = NP_VALID;
103     np_sessionp->ncp_list = NULL;
104     if ((npp = getenv(NETPATH)) == NULL) {
105 	np_sessionp->netpath = NULL;
106     } else {
107 	(void) endnetconfig(np_sessionp->nc_handlep);/* won't need nc session*/
108 	np_sessionp->nc_handlep = NULL;
109 	if ((np_sessionp->netpath = malloc(strlen(npp)+1)) == NULL)
110 		goto failed;
111 	else {
112 	    (void) strcpy(np_sessionp->netpath, npp);
113 	}
114     }
115     np_sessionp->netpath_start = np_sessionp->netpath;
116     return ((void *)np_sessionp);
117 
118 failed:
119     free(np_sessionp);
120     return (NULL);
121 }
122 
123 /*
124  * When first called, getnetpath() returns a pointer to the netconfig
125  * database entry corresponding to the first valid NETPATH component.  The
126  * netconfig entry is formatted as a struct netconfig.
127  * On each subsequent call, getnetpath returns a pointer to the netconfig
128  * entry that corresponds to the next valid NETPATH component.  getnetpath
129  * can thus be used to search the netconfig database for all networks
130  * included in the NETPATH variable.
131  * When NETPATH has been exhausted, getnetpath() returns NULL.  It returns
132  * NULL and sets errno in case of an error (e.g., setnetpath was not called
133  * previously).
134  * getnetpath() silently ignores invalid NETPATH components.  A NETPATH
135  * component is invalid if there is no corresponding entry in the netconfig
136  * database.
137  * If the NETPATH variable is unset, getnetpath() behaves as if NETPATH
138  * were set to the sequence of default or visible networks in the netconfig
139  * database, in the order in which they are listed.
140  */
141 
142 struct netconfig *
143 getnetpath(void *handlep)
144 {
145     struct netpath_vars *np_sessionp = (struct netpath_vars *)handlep;
146     struct netconfig *ncp = NULL;   /* temp. holds a netconfig session */
147     struct netpath_chain *chainp;   /* holds chain of ncp's we alloc */
148     char  *npp;		/* holds current NETPATH */
149 
150     if (np_sessionp == NULL || np_sessionp->valid != NP_VALID) {
151 	errno = EINVAL;
152 	return (NULL);
153     }
154     if (np_sessionp->netpath_start == NULL) {	/* NETPATH was not set */
155 	do {                /* select next visible network */
156 	    if (np_sessionp->nc_handlep == NULL) {
157 		np_sessionp->nc_handlep = setnetconfig();
158 		if (np_sessionp->nc_handlep == NULL)
159 		    syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
160 	    }
161 	    if ((ncp = getnetconfig(np_sessionp->nc_handlep)) == NULL) {
162 		return(NULL);
163 	    }
164 	} while ((ncp->nc_flag & NC_VISIBLE) == 0);
165 	return (ncp);
166     }
167     /*
168      * Find first valid network ID in netpath.
169      */
170     while ((npp = np_sessionp->netpath) != NULL && strlen(npp) != 0) {
171 	np_sessionp->netpath = _get_next_token(npp, ':');
172     	/*
173     	 * npp is a network identifier.
174 	 */
175 	if ((ncp = getnetconfigent(npp)) != NULL) {
176 	    chainp = (struct netpath_chain *)	/* cobble alloc chain entry */
177 		    malloc(sizeof (struct netpath_chain));
178 	    chainp->ncp = ncp;
179 	    chainp->nchain_next = NULL;
180 	    if (np_sessionp->ncp_list == NULL) {
181 		np_sessionp->ncp_list = chainp;
182 	    } else {
183 		np_sessionp->ncp_list->nchain_next = chainp;
184 	    }
185 	    return (ncp);
186 	}
187 	/* couldn't find this token in the database; go to next one. */
188     }
189     return (NULL);
190 }
191 
192 /*
193  * endnetpath() may be called to unbind NETPATH when processing is complete,
194  * releasing resources for reuse.  It returns 0 on success and -1 on failure
195  * (e.g. if setnetpath() was not called previously.
196  */
197 int
198 endnetpath(void *handlep)
199 {
200     struct netpath_vars *np_sessionp = (struct netpath_vars *)handlep;
201     struct netpath_chain *chainp, *lastp;
202 
203     if (np_sessionp == NULL || np_sessionp->valid != NP_VALID) {
204 	errno = EINVAL;
205 	return (-1);
206     }
207     if (np_sessionp->nc_handlep != NULL)
208 	endnetconfig(np_sessionp->nc_handlep);
209     if (np_sessionp->netpath_start != NULL)
210 	free(np_sessionp->netpath_start);
211     for (chainp = np_sessionp->ncp_list; chainp != NULL;
212 	    lastp=chainp, chainp=chainp->nchain_next, free(lastp)) {
213 	freenetconfigent(chainp->ncp);
214     }
215     free(np_sessionp);
216 #ifdef MEM_CHK
217     if (malloc_verify() == 0) {
218 	fprintf(stderr, "memory heap corrupted in endnetpath\n");
219 	exit(1);
220     }
221 #endif
222     return (0);
223 }
224 
225 
226 
227 /*
228  * Returns pointer to the rest-of-the-string after the current token.
229  * The token itself starts at arg, and we null terminate it.  We return NULL
230  * if either the arg is empty, or if this is the last token.
231  *
232  * npp   - string
233  * token - char to parse string for
234  */
235 char *
236 _get_next_token(char *npp, int token)
237 {
238     char  *cp;		/* char pointer */
239     char  *np;		/* netpath pointer */
240     char  *ep;		/* escape pointer */
241 
242     if ((cp = strchr(npp, token)) == NULL) {
243 	return (NULL);
244     }
245     /*
246      * did find a token, but it might be escaped.
247      */
248     if ((cp > npp) && (cp[-1] == '\\')) {
249         /* if slash was also escaped, carry on, otherwise find next token */
250 	if ((cp > npp + 1) && (cp[-2] != '\\')) {
251 	    /* shift r-o-s  onto the escaped token */
252 	    strcpy(&cp[-1], cp);    /* XXX: overlapping string copy */
253 	    /*
254 	     * Do a recursive call.
255 	     * We don't know how many escaped tokens there might be.
256 	     */
257 	    return (_get_next_token(cp, token));
258 	}
259     }
260 
261     *cp++ = '\0';		/* null-terminate token */
262     /* get rid of any backslash escapes */
263     ep = npp;
264     while ((np = strchr(ep, '\\')) != NULL) {
265 	if (np[1] == '\\')
266 	    np++;
267 	strcpy(np, (ep = &np[1]));  /* XXX: overlapping string copy */
268     }
269     return (cp);		/* return ptr to r-o-s */
270 }
271