1 /* $NetBSD: getnetpath.c,v 1.3 2000/07/06 03:10:34 christos Exp $ */
4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 * unrestricted use provided that this legend is included on all tape
6 * media and as a part of the software program in whole or part. Users
7 * may copy or modify Sun RPC without charge, but are not authorized
8 * to license or distribute it to anyone else except as part of a product or
9 * program developed by the user or with the express written consent of
10 * Sun Microsystems, Inc.
12 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
13 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
14 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
16 * Sun RPC is provided with no support and without any obligation on the
17 * part of Sun Microsystems, Inc. to assist in its use, correction,
18 * modification or enhancement.
20 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
21 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
22 * OR ANY PART THEREOF.
24 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
25 * or profits or other special, indirect and consequential damages, even if
26 * Sun has been advised of the possibility of such damages.
28 * Sun Microsystems, Inc.
30 * Mountain View, California 94043
33 #if defined(LIBC_SCCS) && !defined(lint)
34 static char sccsid[] = "@(#)getnetpath.c 1.11 91/12/19 SMI";
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
40 * Copyright (c) 1989 by Sun Microsystems, Inc.
43 #include "namespace.h"
44 #include <sys/cdefs.h>
47 #include <netconfig.h>
51 #include "un-namespace.h"
54 * internal structure to keep track of a netpath "session"
56 struct netpath_chain {
57 struct netconfig *ncp; /* an nconf entry */
58 struct netpath_chain *nchain_next; /* next nconf entry allocated */
63 int valid; /* token that indicates a valid netpath_vars */
64 void *nc_handlep; /* handle for current netconfig "session" */
65 char *netpath; /* pointer to current view-point in NETPATH */
66 char *netpath_start; /* pointer to start of our copy of NETPATH */
67 struct netpath_chain *ncp_list; /* list of nconfs allocated this session*/
70 #define NP_VALID 0xf00d
73 char *_get_next_token(char *, int);
77 * A call to setnetpath() establishes a NETPATH "session". setnetpath()
78 * must be called before the first call to getnetpath(). A "handle" is
79 * returned to distinguish the session; this handle should be passed
80 * subsequently to getnetpath(). (Handles are used to allow for nested calls
82 * If setnetpath() is unable to establish a session (due to lack of memory
83 * resources, or the absence of the /etc/netconfig file), a NULL pointer is
91 struct netpath_vars *np_sessionp; /* this session's variables */
92 char *npp; /* NETPATH env variable */
99 (struct netpath_vars *)malloc(sizeof (struct netpath_vars))) == NULL) {
102 if ((np_sessionp->nc_handlep = setnetconfig()) == NULL) {
104 syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
107 np_sessionp->valid = NP_VALID;
108 np_sessionp->ncp_list = NULL;
109 if ((npp = getenv(NETPATH)) == NULL) {
110 np_sessionp->netpath = NULL;
112 (void) endnetconfig(np_sessionp->nc_handlep);/* won't need nc session*/
113 np_sessionp->nc_handlep = NULL;
114 if ((np_sessionp->netpath = malloc(strlen(npp)+1)) == NULL) {
118 (void) strcpy(np_sessionp->netpath, npp);
121 np_sessionp->netpath_start = np_sessionp->netpath;
122 return ((void *)np_sessionp);
126 * When first called, getnetpath() returns a pointer to the netconfig
127 * database entry corresponding to the first valid NETPATH component. The
128 * netconfig entry is formatted as a struct netconfig.
129 * On each subsequent call, getnetpath returns a pointer to the netconfig
130 * entry that corresponds to the next valid NETPATH component. getnetpath
131 * can thus be used to search the netconfig database for all networks
132 * included in the NETPATH variable.
133 * When NETPATH has been exhausted, getnetpath() returns NULL. It returns
134 * NULL and sets errno in case of an error (e.g., setnetpath was not called
136 * getnetpath() silently ignores invalid NETPATH components. A NETPATH
137 * compnent is invalid if there is no corresponding entry in the netconfig
139 * If the NETPATH variable is unset, getnetpath() behaves as if NETPATH
140 * were set to the sequence of default or visible networks in the netconfig
141 * database, in the order in which they are listed.
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 */
153 if (np_sessionp == NULL || np_sessionp->valid != NP_VALID) {
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);
164 if ((ncp = getnetconfig(np_sessionp->nc_handlep)) == NULL) {
167 } while ((ncp->nc_flag & NC_VISIBLE) == 0);
171 * Find first valid network ID in netpath.
173 while ((npp = np_sessionp->netpath) != NULL && strlen(npp) != 0) {
174 np_sessionp->netpath = _get_next_token(npp, ':');
176 * npp is a network identifier.
178 if ((ncp = getnetconfigent(npp)) != NULL) {
179 chainp = (struct netpath_chain *) /* cobble alloc chain entry */
180 malloc(sizeof (struct netpath_chain));
182 chainp->nchain_next = NULL;
183 if (np_sessionp->ncp_list == NULL) {
184 np_sessionp->ncp_list = chainp;
186 np_sessionp->ncp_list->nchain_next = chainp;
190 /* couldn't find this token in the database; go to next one. */
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.
204 struct netpath_vars *np_sessionp = (struct netpath_vars *)handlep;
205 struct netpath_chain *chainp, *lastp;
207 if (np_sessionp == NULL || np_sessionp->valid != NP_VALID) {
211 if (np_sessionp->nc_handlep != NULL)
212 endnetconfig(np_sessionp->nc_handlep);
213 if (np_sessionp->netpath_start != NULL)
214 free(np_sessionp->netpath_start);
215 for (chainp = np_sessionp->ncp_list; chainp != NULL;
216 lastp=chainp, chainp=chainp->nchain_next, free(lastp)) {
217 freenetconfigent(chainp->ncp);
221 if (malloc_verify() == 0) {
222 fprintf(stderr, "memory heap corrupted in endnetpath\n");
232 * Returns pointer to the rest-of-the-string after the current token.
233 * The token itself starts at arg, and we null terminate it. We return NULL
234 * if either the arg is empty, or if this is the last token.
238 _get_next_token(npp, token)
239 char *npp; /* string */
240 int token; /* char to parse string for */
242 char *cp; /* char pointer */
243 char *np; /* netpath pointer */
244 char *ep; /* escape pointer */
246 if ((cp = strchr(npp, token)) == NULL) {
250 * did find a token, but it might be escaped.
252 if ((cp > npp) && (cp[-1] == '\\')) {
253 /* if slash was also escaped, carry on, otherwise find next token */
254 if ((cp > npp + 1) && (cp[-2] != '\\')) {
255 /* shift r-o-s onto the escaped token */
256 strcpy(&cp[-1], cp); /* XXX: overlapping string copy */
258 * Do a recursive call.
259 * We don't know how many escaped tokens there might be.
261 return (_get_next_token(cp, token));
265 *cp++ = '\0'; /* null-terminate token */
266 /* get rid of any backslash escapes */
268 while ((np = strchr(ep, '\\')) != 0) {
271 strcpy(np, (ep = &np[1])); /* XXX: overlapping string copy */
273 return (cp); /* return ptr to r-o-s */