1 /* $NetBSD: getnetpath.c,v 1.17 2013/03/11 20:19:29 tron Exp $ */
4 * Copyright (c) 2010, Oracle America, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
16 * * Neither the name of the "Oracle America, 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
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 #if defined(LIBC_SCCS) && !defined(lint)
37 static char sccsid
[] = "@(#)getnetpath.c 1.11 91/12/19 SMI";
39 __RCSID("$NetBSD: getnetpath.c,v 1.17 2013/03/11 20:19:29 tron Exp $");
44 * Copyright (c) 1989 by Sun Microsystems, Inc.
47 #include "namespace.h"
51 #include <netconfig.h>
57 __weak_alias(getnetpath
,_getnetpath
)
58 __weak_alias(setnetpath
,_setnetpath
)
59 __weak_alias(endnetpath
,_endnetpath
)
63 * internal structure to keep track of a netpath "session"
65 struct netpath_chain
{
66 struct netconfig
*ncp
; /* an nconf entry */
67 struct netpath_chain
*nchain_next
; /* next nconf entry allocated */
72 int valid
; /* token that indicates a valid netpath_vars */
73 void *nc_handlep
; /* handle for current netconfig "session" */
74 char *netpath
; /* pointer to current view-point in NETPATH */
75 char *netpath_start
; /* pointer to start of our copy of NETPATH */
76 struct netpath_chain
*ncp_list
; /* list of nconfs allocated this session*/
79 #define NP_VALID 0xf00d
82 char *_get_next_token(char *, int);
86 * A call to setnetpath() establishes a NETPATH "session". setnetpath()
87 * must be called before the first call to getnetpath(). A "handle" is
88 * returned to distinguish the session; this handle should be passed
89 * subsequently to getnetpath(). (Handles are used to allow for nested calls
91 * If setnetpath() is unable to establish a session (due to lack of memory
92 * resources, or the absence of the /etc/netconfig file), a NULL pointer is
99 struct netpath_vars
*np_sessionp
; /* this session's variables */
100 char *npp
; /* NETPATH env variable */
106 if ((np_sessionp
= malloc(sizeof(*np_sessionp
))) == NULL
)
108 if ((np_sessionp
->nc_handlep
= setnetconfig()) == NULL
) {
110 syslog (LOG_ERR
, "rpc: failed to open " NETCONFIG
);
113 np_sessionp
->valid
= NP_VALID
;
114 np_sessionp
->ncp_list
= NULL
;
115 if ((npp
= getenv(NETPATH
)) == NULL
)
116 np_sessionp
->netpath
= NULL
;
118 (void) endnetconfig(np_sessionp
->nc_handlep
);
119 /* won't need nc session*/
120 np_sessionp
->nc_handlep
= NULL
;
121 if ((np_sessionp
->netpath
= malloc(strlen(npp
)+1)) == NULL
) {
125 (void) strcpy(np_sessionp
->netpath
, npp
);
127 np_sessionp
->netpath_start
= np_sessionp
->netpath
;
128 return ((void *)np_sessionp
);
132 * When first called, getnetpath() returns a pointer to the netconfig
133 * database entry corresponding to the first valid NETPATH component. The
134 * netconfig entry is formatted as a struct netconfig.
135 * On each subsequent call, getnetpath returns a pointer to the netconfig
136 * entry that corresponds to the next valid NETPATH component. getnetpath
137 * can thus be used to search the netconfig database for all networks
138 * included in the NETPATH variable.
139 * When NETPATH has been exhausted, getnetpath() returns NULL. It returns
140 * NULL and sets errno in case of an error (e.g., setnetpath was not called
142 * getnetpath() silently ignores invalid NETPATH components. A NETPATH
143 * compnent is invalid if there is no corresponding entry in the netconfig
145 * If the NETPATH variable is unset, getnetpath() behaves as if NETPATH
146 * were set to the sequence of default or visible networks in the netconfig
147 * database, in the order in which they are listed.
151 getnetpath(void *handlep
)
153 struct netpath_vars
*np_sessionp
= (struct netpath_vars
*)handlep
;
154 struct netconfig
*ncp
= NULL
; /* temp. holds a netconfig session */
155 struct netpath_chain
*chainp
; /* holds chain of ncp's we alloc */
156 char *npp
; /* holds current NETPATH */
158 if (np_sessionp
== NULL
|| np_sessionp
->valid
!= NP_VALID
) {
162 if (np_sessionp
->netpath_start
== NULL
) { /* NETPATH was not set */
163 do { /* select next visible network */
164 if (np_sessionp
->nc_handlep
== NULL
) {
165 np_sessionp
->nc_handlep
= setnetconfig();
166 if (np_sessionp
->nc_handlep
== NULL
)
168 "rpc: failed to open " NETCONFIG
);
170 if ((ncp
= getnetconfig(np_sessionp
->nc_handlep
))
173 } while ((ncp
->nc_flag
& NC_VISIBLE
) == 0);
177 * Find first valid network ID in netpath.
179 while ((npp
= np_sessionp
->netpath
) != NULL
&& strlen(npp
) != 0) {
180 np_sessionp
->netpath
= _get_next_token(npp
, ':');
182 * npp is a network identifier.
184 if ((ncp
= getnetconfigent(npp
)) != NULL
) {
185 /* cobble alloc chain entry */
186 chainp
= malloc(sizeof (struct netpath_chain
));
187 if (chainp
== NULL
) {
188 freenetconfigent(ncp
);
192 chainp
->nchain_next
= NULL
;
193 if (np_sessionp
->ncp_list
== NULL
)
194 np_sessionp
->ncp_list
= chainp
;
196 np_sessionp
->ncp_list
->nchain_next
= chainp
;
199 /* couldn't find this token in the database; go to next one. */
205 * endnetpath() may be called to unbind NETPATH when processing is complete,
206 * releasing resources for reuse. It returns 0 on success and -1 on failure
207 * (e.g. if setnetpath() was not called previously.
210 endnetpath(void *handlep
)
212 struct netpath_vars
*np_sessionp
= (struct netpath_vars
*)handlep
;
213 struct netpath_chain
*chainp
, *lastp
;
215 if (np_sessionp
== NULL
|| np_sessionp
->valid
!= NP_VALID
) {
219 if (np_sessionp
->nc_handlep
!= NULL
)
220 endnetconfig(np_sessionp
->nc_handlep
);
221 if (np_sessionp
->netpath_start
!= NULL
)
222 free(np_sessionp
->netpath_start
);
223 for (chainp
= np_sessionp
->ncp_list
; chainp
!= NULL
;
224 lastp
=chainp
, chainp
=chainp
->nchain_next
, free(lastp
)) {
225 freenetconfigent(chainp
->ncp
);
229 if (malloc_verify() == 0) {
230 fprintf(stderr
, "memory heap corrupted in endnetpath\n");
239 * Returns pointer to the rest-of-the-string after the current token.
240 * The token itself starts at arg, and we null terminate it. We return NULL
241 * if either the arg is empty, or if this is the last token.
246 char *npp
, /* string */
247 int token
/* char to parse string for */
250 char *cp
; /* char pointer */
251 char *np
; /* netpath pointer */
252 char *ep
; /* escape pointer */
254 _DIAGASSERT(npp
!= NULL
);
256 if ((cp
= strchr(npp
, token
)) == NULL
)
259 * did find a token, but it might be escaped.
261 if ((cp
> npp
) && (cp
[-1] == '\\')) {
263 * if slash was also escaped, carry on, otherwise find
266 if ((cp
> npp
+ 1) && (cp
[-2] != '\\')) {
267 /* shift r-o-s onto the escaped token */
268 strcpy(&cp
[-1], cp
); /* XXX: overlapping string copy */
270 * Do a recursive call.
271 * We don't know how many escaped tokens there might be.
273 return (_get_next_token(cp
, token
));
277 *cp
++ = '\0'; /* null-terminate token */
278 /* get rid of any backslash escapes */
280 while ((np
= strchr(ep
, '\\')) != 0) {
283 strcpy(np
, (ep
= &np
[1])); /* XXX: overlapping string copy */
285 return (cp
); /* return ptr to r-o-s */