1 /* $NetBSD: getnetpath.c,v 1.3 2000/07/06 03:10:34 christos Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (c) 2009, Sun Microsystems, Inc.
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.
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.
34 * Copyright (c) 1989 by Sun Microsystems, Inc.
37 #include "namespace.h"
40 #include <netconfig.h>
44 #include "un-namespace.h"
47 * internal structure to keep track of a netpath "session"
49 struct netpath_chain {
50 struct netconfig *ncp; /* an nconf entry */
51 struct netpath_chain *nchain_next; /* next nconf entry allocated */
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*/
63 #define NP_VALID 0xf00d
66 char *_get_next_token(char *, int);
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
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
84 struct netpath_vars *np_sessionp; /* this session's variables */
85 char *npp; /* NETPATH env variable */
92 (struct netpath_vars *)malloc(sizeof (struct netpath_vars))) == NULL) {
95 if ((np_sessionp->nc_handlep = setnetconfig()) == NULL) {
96 syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
99 np_sessionp->valid = NP_VALID;
100 np_sessionp->ncp_list = NULL;
101 if ((npp = getenv(NETPATH)) == NULL) {
102 np_sessionp->netpath = NULL;
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)
109 (void) strcpy(np_sessionp->netpath, npp);
112 np_sessionp->netpath_start = np_sessionp->netpath;
113 return ((void *)np_sessionp);
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
131 * getnetpath() silently ignores invalid NETPATH components. A NETPATH
132 * component is invalid if there is no corresponding entry in the netconfig
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.
140 getnetpath(void *handlep)
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 */
147 if (np_sessionp == NULL || np_sessionp->valid != NP_VALID) {
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);
158 if ((ncp = getnetconfig(np_sessionp->nc_handlep)) == NULL) {
161 } while ((ncp->nc_flag & NC_VISIBLE) == 0);
165 * Find first valid network ID in netpath.
167 while ((npp = np_sessionp->netpath) != NULL && strlen(npp) != 0) {
168 np_sessionp->netpath = _get_next_token(npp, ':');
170 * npp is a network identifier.
172 if ((ncp = getnetconfigent(npp)) != NULL) {
173 chainp = (struct netpath_chain *) /* cobble alloc chain entry */
174 malloc(sizeof (struct netpath_chain));
176 chainp->nchain_next = NULL;
177 if (np_sessionp->ncp_list == NULL) {
178 np_sessionp->ncp_list = chainp;
180 np_sessionp->ncp_list->nchain_next = chainp;
184 /* couldn't find this token in the database; go to next one. */
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.
195 endnetpath(void *handlep)
197 struct netpath_vars *np_sessionp = (struct netpath_vars *)handlep;
198 struct netpath_chain *chainp, *lastp;
200 if (np_sessionp == NULL || np_sessionp->valid != NP_VALID) {
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);
214 if (malloc_verify() == 0) {
215 fprintf(stderr, "memory heap corrupted in endnetpath\n");
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.
230 * token - char to parse string for
233 _get_next_token(char *npp, int token)
235 char *cp; /* char pointer */
236 char *np; /* netpath pointer */
237 char *ep; /* escape pointer */
239 if ((cp = strchr(npp, token)) == NULL) {
243 * did find a token, but it might be escaped.
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 */
251 * Do a recursive call.
252 * We don't know how many escaped tokens there might be.
254 return (_get_next_token(cp, token));
258 *cp++ = '\0'; /* null-terminate token */
259 /* get rid of any backslash escapes */
261 while ((np = strchr(ep, '\\')) != NULL) {
264 strcpy(np, (ep = &np[1])); /* XXX: overlapping string copy */
266 return (cp); /* return ptr to r-o-s */