xref: /dragonfly/lib/libc/rpc/getnetpath.c (revision 19fe1c42)
1 /*
2  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3  * unrestricted use provided that this legend is included on all tape
4  * media and as a part of the software program in whole or part.  Users
5  * may copy or modify Sun RPC without charge, but are not authorized
6  * to license or distribute it to anyone else except as part of a product or
7  * program developed by the user or with the express written consent of
8  * Sun Microsystems, Inc.
9  *
10  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13  *
14  * Sun RPC is provided with no support and without any obligation on the
15  * part of Sun Microsystems, Inc. to assist in its use, correction,
16  * modification or enhancement.
17  *
18  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20  * OR ANY PART THEREOF.
21  *
22  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23  * or profits or other special, indirect and consequential damages, even if
24  * Sun has been advised of the possibility of such damages.
25  *
26  * Sun Microsystems, Inc.
27  * 2550 Garcia Avenue
28  * Mountain View, California  94043
29  *
30  * @(#)getnetpath.c	1.11 91/12/19 SMI
31  * $NetBSD: getnetpath.c,v 1.3 2000/07/06 03:10:34 christos Exp $
32  * $FreeBSD: src/lib/libc/rpc/getnetpath.c,v 1.8 2007/09/20 22:35:24 matteo Exp $
33  * $DragonFly$
34  */
35 
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 	free(np_sessionp);
100 	syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
101 	goto failed;
102     }
103     np_sessionp->valid = NP_VALID;
104     np_sessionp->ncp_list = NULL;
105     if ((npp = getenv(NETPATH)) == NULL) {
106 	np_sessionp->netpath = NULL;
107     } else {
108 	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)
111 		goto failed;
112 	else {
113 	    strcpy(np_sessionp->netpath, npp);
114 	}
115     }
116     np_sessionp->netpath_start = np_sessionp->netpath;
117     return ((void *)np_sessionp);
118 
119 failed:
120     free(np_sessionp);
121     return (NULL);
122 }
123 
124 /*
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
134  * previously).
135  * getnetpath() silently ignores invalid NETPATH components.  A NETPATH
136  * compnent is invalid if there is no corresponding entry in the netconfig
137  * database.
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.
141  */
142 
143 struct netconfig *
144 getnetpath(void *handlep)
145 {
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 */
150 
151     if (np_sessionp == NULL || np_sessionp->valid != NP_VALID) {
152 	errno = EINVAL;
153 	return (NULL);
154     }
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);
161 	    }
162 	    if ((ncp = getnetconfig(np_sessionp->nc_handlep)) == NULL) {
163 		return(NULL);
164 	    }
165 	} while ((ncp->nc_flag & NC_VISIBLE) == 0);
166 	return (ncp);
167     }
168     /*
169      * Find first valid network ID in netpath.
170      */
171     while ((npp = np_sessionp->netpath) != NULL && strlen(npp) != 0) {
172 	np_sessionp->netpath = _get_next_token(npp, ':');
173 	/*
174 	 * npp is a network identifier.
175 	 */
176 	if ((ncp = getnetconfigent(npp)) != NULL) {
177 	    chainp = (struct netpath_chain *)	/* cobble alloc chain entry */
178 		    malloc(sizeof (struct netpath_chain));
179 	    chainp->ncp = ncp;
180 	    chainp->nchain_next = NULL;
181 	    if (np_sessionp->ncp_list == NULL) {
182 		np_sessionp->ncp_list = chainp;
183 	    } else {
184 		np_sessionp->ncp_list->nchain_next = chainp;
185 	    }
186 	    return (ncp);
187 	}
188 	/* couldn't find this token in the database; go to next one. */
189     }
190     return (NULL);
191 }
192 
193 /*
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.
197  */
198 int
199 endnetpath(void *handlep)
200 {
201     struct netpath_vars *np_sessionp = (struct netpath_vars *)handlep;
202     struct netpath_chain *chainp, *lastp;
203 
204     if (np_sessionp == NULL || np_sessionp->valid != NP_VALID) {
205 	errno = EINVAL;
206 	return (-1);
207     }
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);
215     }
216     free(np_sessionp);
217 #ifdef MEM_CHK
218     if (malloc_verify() == 0) {
219 	fprintf(stderr, "memory heap corrupted in endnetpath\n");
220 	exit(1);
221     }
222 #endif
223     return (0);
224 }
225 
226 
227 
228 /*
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.
232  */
233 
234 char *
235 _get_next_token(char *npp,		/* string */
236 		int token)		/* char to parse string for */
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, '\\')) != 0) {
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