1 /* $NetBSD: getnetpath.c,v 1.3 2000/07/06 03:10:34 christos Exp $ */
4 * Copyright (c) 2009, Sun Microsystems, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * - Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * - Neither the name of Sun Microsystems, Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
31 #if defined(LIBC_SCCS) && !defined(lint)
32 static char sccsid[] = "@(#)getnetpath.c 1.11 91/12/19 SMI";
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
38 * Copyright (c) 1989 by Sun Microsystems, Inc.
41 #include "namespace.h"
44 #include <netconfig.h>
48 #include "un-namespace.h"
51 * internal structure to keep track of a netpath "session"
53 struct netpath_chain {
54 struct netconfig *ncp; /* an nconf entry */
55 struct netpath_chain *nchain_next; /* next nconf entry allocated */
60 int valid; /* token that indicates a valid netpath_vars */
61 void *nc_handlep; /* handle for current netconfig "session" */
62 char *netpath; /* pointer to current view-point in NETPATH */
63 char *netpath_start; /* pointer to start of our copy of NETPATH */
64 struct netpath_chain *ncp_list; /* list of nconfs allocated this session*/
67 #define NP_VALID 0xf00d
70 char *_get_next_token(char *, int);
74 * A call to setnetpath() establishes a NETPATH "session". setnetpath()
75 * must be called before the first call to getnetpath(). A "handle" is
76 * returned to distinguish the session; this handle should be passed
77 * subsequently to getnetpath(). (Handles are used to allow for nested calls
79 * If setnetpath() is unable to establish a session (due to lack of memory
80 * resources, or the absence of the /etc/netconfig file), a NULL pointer is
88 struct netpath_vars *np_sessionp; /* this session's variables */
89 char *npp; /* NETPATH env variable */
96 (struct netpath_vars *)malloc(sizeof (struct netpath_vars))) == NULL) {
99 if ((np_sessionp->nc_handlep = setnetconfig()) == NULL) {
100 syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
103 np_sessionp->valid = NP_VALID;
104 np_sessionp->ncp_list = NULL;
105 if ((npp = getenv(NETPATH)) == NULL) {
106 np_sessionp->netpath = NULL;
108 (void) endnetconfig(np_sessionp->nc_handlep);/* won't need nc session*/
109 np_sessionp->nc_handlep = NULL;
110 if ((np_sessionp->netpath = malloc(strlen(npp)+1)) == NULL)
113 (void) strcpy(np_sessionp->netpath, npp);
116 np_sessionp->netpath_start = np_sessionp->netpath;
117 return ((void *)np_sessionp);
125 * When first called, getnetpath() returns a pointer to the netconfig
126 * database entry corresponding to the first valid NETPATH component. The
127 * netconfig entry is formatted as a struct netconfig.
128 * On each subsequent call, getnetpath returns a pointer to the netconfig
129 * entry that corresponds to the next valid NETPATH component. getnetpath
130 * can thus be used to search the netconfig database for all networks
131 * included in the NETPATH variable.
132 * When NETPATH has been exhausted, getnetpath() returns NULL. It returns
133 * NULL and sets errno in case of an error (e.g., setnetpath was not called
135 * getnetpath() silently ignores invalid NETPATH components. A NETPATH
136 * compnent is invalid if there is no corresponding entry in the netconfig
138 * If the NETPATH variable is unset, getnetpath() behaves as if NETPATH
139 * were set to the sequence of default or visible networks in the netconfig
140 * database, in the order in which they are listed.
144 getnetpath(void *handlep)
146 struct netpath_vars *np_sessionp = (struct netpath_vars *)handlep;
147 struct netconfig *ncp = NULL; /* temp. holds a netconfig session */
148 struct netpath_chain *chainp; /* holds chain of ncp's we alloc */
149 char *npp; /* holds current NETPATH */
151 if (np_sessionp == NULL || np_sessionp->valid != NP_VALID) {
155 if (np_sessionp->netpath_start == NULL) { /* NETPATH was not set */
156 do { /* select next visible network */
157 if (np_sessionp->nc_handlep == NULL) {
158 np_sessionp->nc_handlep = setnetconfig();
159 if (np_sessionp->nc_handlep == NULL)
160 syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
162 if ((ncp = getnetconfig(np_sessionp->nc_handlep)) == NULL) {
165 } while ((ncp->nc_flag & NC_VISIBLE) == 0);
169 * Find first valid network ID in netpath.
171 while ((npp = np_sessionp->netpath) != NULL && strlen(npp) != 0) {
172 np_sessionp->netpath = _get_next_token(npp, ':');
174 * npp is a network identifier.
176 if ((ncp = getnetconfigent(npp)) != NULL) {
177 chainp = (struct netpath_chain *) /* cobble alloc chain entry */
178 malloc(sizeof (struct netpath_chain));
180 chainp->nchain_next = NULL;
181 if (np_sessionp->ncp_list == NULL) {
182 np_sessionp->ncp_list = chainp;
184 np_sessionp->ncp_list->nchain_next = chainp;
188 /* couldn't find this token in the database; go to next one. */
194 * endnetpath() may be called to unbind NETPATH when processing is complete,
195 * releasing resources for reuse. It returns 0 on success and -1 on failure
196 * (e.g. if setnetpath() was not called previously.
199 endnetpath(void *handlep)
201 struct netpath_vars *np_sessionp = (struct netpath_vars *)handlep;
202 struct netpath_chain *chainp, *lastp;
204 if (np_sessionp == NULL || np_sessionp->valid != NP_VALID) {
208 if (np_sessionp->nc_handlep != NULL)
209 endnetconfig(np_sessionp->nc_handlep);
210 if (np_sessionp->netpath_start != NULL)
211 free(np_sessionp->netpath_start);
212 for (chainp = np_sessionp->ncp_list; chainp != NULL;
213 lastp=chainp, chainp=chainp->nchain_next, free(lastp)) {
214 freenetconfigent(chainp->ncp);
218 if (malloc_verify() == 0) {
219 fprintf(stderr, "memory heap corrupted in endnetpath\n");
229 * Returns pointer to the rest-of-the-string after the current token.
230 * The token itself starts at arg, and we null terminate it. We return NULL
231 * if either the arg is empty, or if this is the last token.
234 * token - char to parse string for
237 _get_next_token(char *npp, int token)
239 char *cp; /* char pointer */
240 char *np; /* netpath pointer */
241 char *ep; /* escape pointer */
243 if ((cp = strchr(npp, token)) == NULL) {
247 * did find a token, but it might be escaped.
249 if ((cp > npp) && (cp[-1] == '\\')) {
250 /* if slash was also escaped, carry on, otherwise find next token */
251 if ((cp > npp + 1) && (cp[-2] != '\\')) {
252 /* shift r-o-s onto the escaped token */
253 strcpy(&cp[-1], cp); /* XXX: overlapping string copy */
255 * Do a recursive call.
256 * We don't know how many escaped tokens there might be.
258 return (_get_next_token(cp, token));
262 *cp++ = '\0'; /* null-terminate token */
263 /* get rid of any backslash escapes */
265 while ((np = strchr(ep, '\\')) != NULL) {
268 strcpy(np, (ep = &np[1])); /* XXX: overlapping string copy */
270 return (cp); /* return ptr to r-o-s */