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