xref: /dragonfly/lib/libc/rpc/getnetpath.c (revision 335b9e93)
1 /*-
2  * Copyright (c) 2009, Sun Microsystems, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * - Redistributions of source code must retain the above copyright notice,
8  *   this list of conditions and the following disclaimer.
9  * - Redistributions in binary form must reproduce the above copyright notice,
10  *   this list of conditions and the following disclaimer in the documentation
11  *   and/or other materials provided with the distribution.
12  * - Neither the name of Sun Microsystems, Inc. nor the names of its
13  *   contributors may be used to endorse or promote products derived
14  *   from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  *
28  * @(#)getnetpath.c	1.11 91/12/19 SMI
29  * $NetBSD: getnetpath.c,v 1.3 2000/07/06 03:10:34 christos Exp $
30  * $FreeBSD: src/lib/libc/rpc/getnetpath.c,v 1.8 2007/09/20 22:35:24 matteo Exp $
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 	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 	    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  * compnent 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 
230 char *
231 _get_next_token(char *npp,		/* string */
232 		int token)		/* char to parse string for */
233 {
234     char  *cp;		/* char pointer */
235     char  *np;		/* netpath pointer */
236     char  *ep;		/* escape pointer */
237 
238     if ((cp = strchr(npp, token)) == NULL) {
239 	return (NULL);
240     }
241     /*
242      * did find a token, but it might be escaped.
243      */
244     if ((cp > npp) && (cp[-1] == '\\')) {
245         /* if slash was also escaped, carry on, otherwise find next token */
246 	if ((cp > npp + 1) && (cp[-2] != '\\')) {
247 	    /* shift r-o-s  onto the escaped token */
248 	    strcpy(&cp[-1], cp);    /* XXX: overlapping string copy */
249 	    /*
250 	     * Do a recursive call.
251 	     * We don't know how many escaped tokens there might be.
252 	     */
253 	    return (_get_next_token(cp, token));
254 	}
255     }
256 
257     *cp++ = '\0';		/* null-terminate token */
258     /* get rid of any backslash escapes */
259     ep = npp;
260     while ((np = strchr(ep, '\\')) != NULL) {
261 	if (np[1] == '\\')
262 	    np++;
263 	strcpy(np, (ep = &np[1]));  /* XXX: overlapping string copy */
264     }
265     return (cp);		/* return ptr to r-o-s */
266 }
267