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.
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.
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.
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.
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.
26 * Sun Microsystems, Inc.
28 * Mountain View, California 94043
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 $
36 * Copyright (c) 1989 by Sun Microsystems, Inc.
39 #include "namespace.h"
42 #include <netconfig.h>
46 #include "un-namespace.h"
49 * internal structure to keep track of a netpath "session"
51 struct netpath_chain
{
52 struct netconfig
*ncp
; /* an nconf entry */
53 struct netpath_chain
*nchain_next
; /* next nconf entry allocated */
58 int valid
; /* token that indicates a valid netpath_vars */
59 void *nc_handlep
; /* handle for current netconfig "session" */
60 char *netpath
; /* pointer to current view-point in NETPATH */
61 char *netpath_start
; /* pointer to start of our copy of NETPATH */
62 struct netpath_chain
*ncp_list
; /* list of nconfs allocated this session*/
65 #define NP_VALID 0xf00d
68 char *_get_next_token(char *, int);
72 * A call to setnetpath() establishes a NETPATH "session". setnetpath()
73 * must be called before the first call to getnetpath(). A "handle" is
74 * returned to distinguish the session; this handle should be passed
75 * subsequently to getnetpath(). (Handles are used to allow for nested calls
77 * If setnetpath() is unable to establish a session (due to lack of memory
78 * resources, or the absence of the /etc/netconfig file), a NULL pointer is
86 struct netpath_vars
*np_sessionp
; /* this session's variables */
87 char *npp
; /* NETPATH env variable */
94 (struct netpath_vars
*)malloc(sizeof (struct netpath_vars
))) == NULL
) {
97 if ((np_sessionp
->nc_handlep
= setnetconfig()) == NULL
) {
99 syslog (LOG_ERR
, "rpc: failed to open " NETCONFIG
);
102 np_sessionp
->valid
= NP_VALID
;
103 np_sessionp
->ncp_list
= NULL
;
104 if ((npp
= getenv(NETPATH
)) == NULL
) {
105 np_sessionp
->netpath
= NULL
;
107 endnetconfig(np_sessionp
->nc_handlep
);/* won't need nc session*/
108 np_sessionp
->nc_handlep
= NULL
;
109 if ((np_sessionp
->netpath
= malloc(strlen(npp
)+1)) == NULL
)
112 strcpy(np_sessionp
->netpath
, npp
);
115 np_sessionp
->netpath_start
= np_sessionp
->netpath
;
116 return ((void *)np_sessionp
);
124 * When first called, getnetpath() returns a pointer to the netconfig
125 * database entry corresponding to the first valid NETPATH component. The
126 * netconfig entry is formatted as a struct netconfig.
127 * On each subsequent call, getnetpath returns a pointer to the netconfig
128 * entry that corresponds to the next valid NETPATH component. getnetpath
129 * can thus be used to search the netconfig database for all networks
130 * included in the NETPATH variable.
131 * When NETPATH has been exhausted, getnetpath() returns NULL. It returns
132 * NULL and sets errno in case of an error (e.g., setnetpath was not called
134 * getnetpath() silently ignores invalid NETPATH components. A NETPATH
135 * compnent is invalid if there is no corresponding entry in the netconfig
137 * If the NETPATH variable is unset, getnetpath() behaves as if NETPATH
138 * were set to the sequence of default or visible networks in the netconfig
139 * database, in the order in which they are listed.
143 getnetpath(void *handlep
)
145 struct netpath_vars
*np_sessionp
= (struct netpath_vars
*)handlep
;
146 struct netconfig
*ncp
= NULL
; /* temp. holds a netconfig session */
147 struct netpath_chain
*chainp
; /* holds chain of ncp's we alloc */
148 char *npp
; /* holds current NETPATH */
150 if (np_sessionp
== NULL
|| np_sessionp
->valid
!= NP_VALID
) {
154 if (np_sessionp
->netpath_start
== NULL
) { /* NETPATH was not set */
155 do { /* select next visible network */
156 if (np_sessionp
->nc_handlep
== NULL
) {
157 np_sessionp
->nc_handlep
= setnetconfig();
158 if (np_sessionp
->nc_handlep
== NULL
)
159 syslog (LOG_ERR
, "rpc: failed to open " NETCONFIG
);
161 if ((ncp
= getnetconfig(np_sessionp
->nc_handlep
)) == NULL
) {
164 } while ((ncp
->nc_flag
& NC_VISIBLE
) == 0);
168 * Find first valid network ID in netpath.
170 while ((npp
= np_sessionp
->netpath
) != NULL
&& strlen(npp
) != 0) {
171 np_sessionp
->netpath
= _get_next_token(npp
, ':');
173 * npp is a network identifier.
175 if ((ncp
= getnetconfigent(npp
)) != NULL
) {
176 chainp
= (struct netpath_chain
*) /* cobble alloc chain entry */
177 malloc(sizeof (struct netpath_chain
));
179 chainp
->nchain_next
= NULL
;
180 if (np_sessionp
->ncp_list
== NULL
) {
181 np_sessionp
->ncp_list
= chainp
;
183 np_sessionp
->ncp_list
->nchain_next
= chainp
;
187 /* couldn't find this token in the database; go to next one. */
193 * endnetpath() may be called to unbind NETPATH when processing is complete,
194 * releasing resources for reuse. It returns 0 on success and -1 on failure
195 * (e.g. if setnetpath() was not called previously.
198 endnetpath(void *handlep
)
200 struct netpath_vars
*np_sessionp
= (struct netpath_vars
*)handlep
;
201 struct netpath_chain
*chainp
, *lastp
;
203 if (np_sessionp
== NULL
|| np_sessionp
->valid
!= NP_VALID
) {
207 if (np_sessionp
->nc_handlep
!= NULL
)
208 endnetconfig(np_sessionp
->nc_handlep
);
209 if (np_sessionp
->netpath_start
!= NULL
)
210 free(np_sessionp
->netpath_start
);
211 for (chainp
= np_sessionp
->ncp_list
; chainp
!= NULL
;
212 lastp
=chainp
, chainp
=chainp
->nchain_next
, free(lastp
)) {
213 freenetconfigent(chainp
->ncp
);
217 if (malloc_verify() == 0) {
218 fprintf(stderr
, "memory heap corrupted in endnetpath\n");
228 * Returns pointer to the rest-of-the-string after the current token.
229 * The token itself starts at arg, and we null terminate it. We return NULL
230 * if either the arg is empty, or if this is the last token.
234 _get_next_token(char *npp
, /* string */
235 int token
) /* char to parse string for */
237 char *cp
; /* char pointer */
238 char *np
; /* netpath pointer */
239 char *ep
; /* escape pointer */
241 if ((cp
= strchr(npp
, token
)) == NULL
) {
245 * did find a token, but it might be escaped.
247 if ((cp
> npp
) && (cp
[-1] == '\\')) {
248 /* if slash was also escaped, carry on, otherwise find next token */
249 if ((cp
> npp
+ 1) && (cp
[-2] != '\\')) {
250 /* shift r-o-s onto the escaped token */
251 strcpy(&cp
[-1], cp
); /* XXX: overlapping string copy */
253 * Do a recursive call.
254 * We don't know how many escaped tokens there might be.
256 return (_get_next_token(cp
, token
));
260 *cp
++ = '\0'; /* null-terminate token */
261 /* get rid of any backslash escapes */
263 while ((np
= strchr(ep
, '\\')) != 0) {
266 strcpy(np
, (ep
= &np
[1])); /* XXX: overlapping string copy */
268 return (cp
); /* return ptr to r-o-s */