1*c2c66affSColin Finck /*
2*c2c66affSColin Finck * Copyright (c) 2009, Sun Microsystems, Inc.
3*c2c66affSColin Finck * All rights reserved.
4*c2c66affSColin Finck *
5*c2c66affSColin Finck * Redistribution and use in source and binary forms, with or without
6*c2c66affSColin Finck * modification, are permitted provided that the following conditions are met:
7*c2c66affSColin Finck * - Redistributions of source code must retain the above copyright notice,
8*c2c66affSColin Finck * this list of conditions and the following disclaimer.
9*c2c66affSColin Finck * - Redistributions in binary form must reproduce the above copyright notice,
10*c2c66affSColin Finck * this list of conditions and the following disclaimer in the documentation
11*c2c66affSColin Finck * and/or other materials provided with the distribution.
12*c2c66affSColin Finck * - Neither the name of Sun Microsystems, Inc. nor the names of its
13*c2c66affSColin Finck * contributors may be used to endorse or promote products derived
14*c2c66affSColin Finck * from this software without specific prior written permission.
15*c2c66affSColin Finck *
16*c2c66affSColin Finck * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17*c2c66affSColin Finck * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*c2c66affSColin Finck * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*c2c66affSColin Finck * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20*c2c66affSColin Finck * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*c2c66affSColin Finck * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*c2c66affSColin Finck * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*c2c66affSColin Finck * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*c2c66affSColin Finck * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*c2c66affSColin Finck * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*c2c66affSColin Finck * POSSIBILITY OF SUCH DAMAGE.
27*c2c66affSColin Finck */
28*c2c66affSColin Finck //#include <sys/cdefs.h>
29*c2c66affSColin Finck
30*c2c66affSColin Finck /*
31*c2c66affSColin Finck * Copyright (c) 1989 by Sun Microsystems, Inc.
32*c2c66affSColin Finck */
33*c2c66affSColin Finck
34*c2c66affSColin Finck //#include <sys/cdefs.h>
35*c2c66affSColin Finck #include <wintirpc.h>
36*c2c66affSColin Finck #include <stdio.h>
37*c2c66affSColin Finck #include <errno.h>
38*c2c66affSColin Finck #include <netconfig.h>
39*c2c66affSColin Finck #include <stdlib.h>
40*c2c66affSColin Finck #include <string.h>
41*c2c66affSColin Finck //#include <syslog.h>
42*c2c66affSColin Finck
43*c2c66affSColin Finck /*
44*c2c66affSColin Finck * internal structure to keep track of a netpath "session"
45*c2c66affSColin Finck */
46*c2c66affSColin Finck struct netpath_chain {
47*c2c66affSColin Finck struct netconfig *ncp; /* an nconf entry */
48*c2c66affSColin Finck struct netpath_chain *nchain_next; /* next nconf entry allocated */
49*c2c66affSColin Finck };
50*c2c66affSColin Finck
51*c2c66affSColin Finck
52*c2c66affSColin Finck struct netpath_vars {
53*c2c66affSColin Finck int valid; /* token that indicates a valid netpath_vars */
54*c2c66affSColin Finck void *nc_handlep; /* handle for current netconfig "session" */
55*c2c66affSColin Finck char *netpath; /* pointer to current view-point in NETPATH */
56*c2c66affSColin Finck char *netpath_start; /* pointer to start of our copy of NETPATH */
57*c2c66affSColin Finck struct netpath_chain *ncp_list; /* list of nconfs allocated this session*/
58*c2c66affSColin Finck };
59*c2c66affSColin Finck
60*c2c66affSColin Finck #define NP_VALID 0xf00d
61*c2c66affSColin Finck #define NP_INVALID 0
62*c2c66affSColin Finck
63*c2c66affSColin Finck char *_get_next_token(char *, int);
64*c2c66affSColin Finck
65*c2c66affSColin Finck
66*c2c66affSColin Finck /*
67*c2c66affSColin Finck * A call to setnetpath() establishes a NETPATH "session". setnetpath()
68*c2c66affSColin Finck * must be called before the first call to getnetpath(). A "handle" is
69*c2c66affSColin Finck * returned to distinguish the session; this handle should be passed
70*c2c66affSColin Finck * subsequently to getnetpath(). (Handles are used to allow for nested calls
71*c2c66affSColin Finck * to setnetpath()).
72*c2c66affSColin Finck * If setnetpath() is unable to establish a session (due to lack of memory
73*c2c66affSColin Finck * resources, or the absence of the /etc/netconfig file), a NULL pointer is
74*c2c66affSColin Finck * returned.
75*c2c66affSColin Finck */
76*c2c66affSColin Finck
77*c2c66affSColin Finck void *
setnetpath()78*c2c66affSColin Finck setnetpath()
79*c2c66affSColin Finck {
80*c2c66affSColin Finck
81*c2c66affSColin Finck struct netpath_vars *np_sessionp; /* this session's variables */
82*c2c66affSColin Finck char *npp; /* NETPATH env variable */
83*c2c66affSColin Finck
84*c2c66affSColin Finck #ifdef MEM_CHK
85*c2c66affSColin Finck malloc_debug(1);
86*c2c66affSColin Finck #endif
87*c2c66affSColin Finck
88*c2c66affSColin Finck if ((np_sessionp =
89*c2c66affSColin Finck (struct netpath_vars *)malloc(sizeof (struct netpath_vars))) == NULL) {
90*c2c66affSColin Finck return (NULL);
91*c2c66affSColin Finck }
92*c2c66affSColin Finck if ((np_sessionp->nc_handlep = setnetconfig()) == NULL) {
93*c2c66affSColin Finck //syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
94*c2c66affSColin Finck return (NULL);
95*c2c66affSColin Finck }
96*c2c66affSColin Finck np_sessionp->valid = NP_VALID;
97*c2c66affSColin Finck np_sessionp->ncp_list = NULL;
98*c2c66affSColin Finck if ((npp = getenv(NETPATH)) == NULL) {
99*c2c66affSColin Finck np_sessionp->netpath = NULL;
100*c2c66affSColin Finck } else {
101*c2c66affSColin Finck (void) endnetconfig(np_sessionp->nc_handlep);/* won't need nc session*/
102*c2c66affSColin Finck np_sessionp->nc_handlep = NULL;
103*c2c66affSColin Finck if ((np_sessionp->netpath = malloc(strlen(npp)+1)) == NULL) {
104*c2c66affSColin Finck free(np_sessionp);
105*c2c66affSColin Finck return (NULL);
106*c2c66affSColin Finck } else {
107*c2c66affSColin Finck (void) strcpy(np_sessionp->netpath, npp);
108*c2c66affSColin Finck }
109*c2c66affSColin Finck }
110*c2c66affSColin Finck np_sessionp->netpath_start = np_sessionp->netpath;
111*c2c66affSColin Finck return ((void *)np_sessionp);
112*c2c66affSColin Finck }
113*c2c66affSColin Finck
114*c2c66affSColin Finck /*
115*c2c66affSColin Finck * When first called, getnetpath() returns a pointer to the netconfig
116*c2c66affSColin Finck * database entry corresponding to the first valid NETPATH component. The
117*c2c66affSColin Finck * netconfig entry is formatted as a struct netconfig.
118*c2c66affSColin Finck * On each subsequent call, getnetpath returns a pointer to the netconfig
119*c2c66affSColin Finck * entry that corresponds to the next valid NETPATH component. getnetpath
120*c2c66affSColin Finck * can thus be used to search the netconfig database for all networks
121*c2c66affSColin Finck * included in the NETPATH variable.
122*c2c66affSColin Finck * When NETPATH has been exhausted, getnetpath() returns NULL. It returns
123*c2c66affSColin Finck * NULL and sets errno in case of an error (e.g., setnetpath was not called
124*c2c66affSColin Finck * previously).
125*c2c66affSColin Finck * getnetpath() silently ignores invalid NETPATH components. A NETPATH
126*c2c66affSColin Finck * compnent is invalid if there is no corresponding entry in the netconfig
127*c2c66affSColin Finck * database.
128*c2c66affSColin Finck * If the NETPATH variable is unset, getnetpath() behaves as if NETPATH
129*c2c66affSColin Finck * were set to the sequence of default or visible networks in the netconfig
130*c2c66affSColin Finck * database, in the order in which they are listed.
131*c2c66affSColin Finck */
132*c2c66affSColin Finck
133*c2c66affSColin Finck struct netconfig *
getnetpath(handlep)134*c2c66affSColin Finck getnetpath(handlep)
135*c2c66affSColin Finck void *handlep;
136*c2c66affSColin Finck {
137*c2c66affSColin Finck struct netpath_vars *np_sessionp = (struct netpath_vars *)handlep;
138*c2c66affSColin Finck struct netconfig *ncp = NULL; /* temp. holds a netconfig session */
139*c2c66affSColin Finck struct netpath_chain *chainp; /* holds chain of ncp's we alloc */
140*c2c66affSColin Finck char *npp; /* holds current NETPATH */
141*c2c66affSColin Finck
142*c2c66affSColin Finck if (np_sessionp == NULL || np_sessionp->valid != NP_VALID) {
143*c2c66affSColin Finck errno = EINVAL;
144*c2c66affSColin Finck return (NULL);
145*c2c66affSColin Finck }
146*c2c66affSColin Finck if (np_sessionp->netpath_start == NULL) { /* NETPATH was not set */
147*c2c66affSColin Finck do { /* select next visible network */
148*c2c66affSColin Finck if (np_sessionp->nc_handlep == NULL) {
149*c2c66affSColin Finck np_sessionp->nc_handlep = setnetconfig();
150*c2c66affSColin Finck if (np_sessionp->nc_handlep == NULL) {
151*c2c66affSColin Finck //syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
152*c2c66affSColin Finck }
153*c2c66affSColin Finck }
154*c2c66affSColin Finck if ((ncp = getnetconfig(np_sessionp->nc_handlep)) == NULL) {
155*c2c66affSColin Finck return(NULL);
156*c2c66affSColin Finck }
157*c2c66affSColin Finck } while ((ncp->nc_flag & NC_VISIBLE) == 0);
158*c2c66affSColin Finck return (ncp);
159*c2c66affSColin Finck }
160*c2c66affSColin Finck /*
161*c2c66affSColin Finck * Find first valid network ID in netpath.
162*c2c66affSColin Finck */
163*c2c66affSColin Finck while ((npp = np_sessionp->netpath) != NULL && strlen(npp) != 0) {
164*c2c66affSColin Finck np_sessionp->netpath = _get_next_token(npp, ':');
165*c2c66affSColin Finck /*
166*c2c66affSColin Finck * npp is a network identifier.
167*c2c66affSColin Finck */
168*c2c66affSColin Finck if ((ncp = getnetconfigent(npp)) != NULL) {
169*c2c66affSColin Finck chainp = (struct netpath_chain *) /* cobble alloc chain entry */
170*c2c66affSColin Finck malloc(sizeof (struct netpath_chain));
171*c2c66affSColin Finck chainp->ncp = ncp;
172*c2c66affSColin Finck chainp->nchain_next = NULL;
173*c2c66affSColin Finck if (np_sessionp->ncp_list == NULL) {
174*c2c66affSColin Finck np_sessionp->ncp_list = chainp;
175*c2c66affSColin Finck } else {
176*c2c66affSColin Finck np_sessionp->ncp_list->nchain_next = chainp;
177*c2c66affSColin Finck }
178*c2c66affSColin Finck return (ncp);
179*c2c66affSColin Finck }
180*c2c66affSColin Finck /* couldn't find this token in the database; go to next one. */
181*c2c66affSColin Finck }
182*c2c66affSColin Finck return (NULL);
183*c2c66affSColin Finck }
184*c2c66affSColin Finck
185*c2c66affSColin Finck /*
186*c2c66affSColin Finck * endnetpath() may be called to unbind NETPATH when processing is complete,
187*c2c66affSColin Finck * releasing resources for reuse. It returns 0 on success and -1 on failure
188*c2c66affSColin Finck * (e.g. if setnetpath() was not called previously.
189*c2c66affSColin Finck */
190*c2c66affSColin Finck int
endnetpath(handlep)191*c2c66affSColin Finck endnetpath(handlep)
192*c2c66affSColin Finck void *handlep;
193*c2c66affSColin Finck {
194*c2c66affSColin Finck struct netpath_vars *np_sessionp = (struct netpath_vars *)handlep;
195*c2c66affSColin Finck struct netpath_chain *chainp, *lastp;
196*c2c66affSColin Finck
197*c2c66affSColin Finck if (np_sessionp == NULL || np_sessionp->valid != NP_VALID) {
198*c2c66affSColin Finck errno = EINVAL;
199*c2c66affSColin Finck return (-1);
200*c2c66affSColin Finck }
201*c2c66affSColin Finck if (np_sessionp->nc_handlep != NULL)
202*c2c66affSColin Finck endnetconfig(np_sessionp->nc_handlep);
203*c2c66affSColin Finck if (np_sessionp->netpath_start != NULL)
204*c2c66affSColin Finck free(np_sessionp->netpath_start);
205*c2c66affSColin Finck for (chainp = np_sessionp->ncp_list; chainp != NULL;
206*c2c66affSColin Finck lastp=chainp, chainp=chainp->nchain_next, free(lastp)) {
207*c2c66affSColin Finck freenetconfigent(chainp->ncp);
208*c2c66affSColin Finck }
209*c2c66affSColin Finck free(np_sessionp);
210*c2c66affSColin Finck #ifdef MEM_CHK
211*c2c66affSColin Finck if (malloc_verify() == 0) {
212*c2c66affSColin Finck fprintf(stderr, "memory heap corrupted in endnetpath\n");
213*c2c66affSColin Finck exit(1);
214*c2c66affSColin Finck }
215*c2c66affSColin Finck #endif
216*c2c66affSColin Finck return (0);
217*c2c66affSColin Finck }
218*c2c66affSColin Finck
219*c2c66affSColin Finck
220*c2c66affSColin Finck
221*c2c66affSColin Finck /*
222*c2c66affSColin Finck * Returns pointer to the rest-of-the-string after the current token.
223*c2c66affSColin Finck * The token itself starts at arg, and we null terminate it. We return NULL
224*c2c66affSColin Finck * if either the arg is empty, or if this is the last token.
225*c2c66affSColin Finck */
226*c2c66affSColin Finck
227*c2c66affSColin Finck char *
_get_next_token(npp,token)228*c2c66affSColin Finck _get_next_token(npp, token)
229*c2c66affSColin Finck char *npp; /* string */
230*c2c66affSColin Finck int token; /* char to parse string for */
231*c2c66affSColin Finck {
232*c2c66affSColin Finck char *cp; /* char pointer */
233*c2c66affSColin Finck char *np; /* netpath pointer */
234*c2c66affSColin Finck char *ep; /* escape pointer */
235*c2c66affSColin Finck
236*c2c66affSColin Finck if ((cp = strchr(npp, token)) == NULL) {
237*c2c66affSColin Finck return (NULL);
238*c2c66affSColin Finck }
239*c2c66affSColin Finck /*
240*c2c66affSColin Finck * did find a token, but it might be escaped.
241*c2c66affSColin Finck */
242*c2c66affSColin Finck if ((cp > npp) && (cp[-1] == '\\')) {
243*c2c66affSColin Finck /* if slash was also escaped, carry on, otherwise find next token */
244*c2c66affSColin Finck if ((cp > npp + 1) && (cp[-2] != '\\')) {
245*c2c66affSColin Finck /* shift r-o-s onto the escaped token */
246*c2c66affSColin Finck strcpy(&cp[-1], cp); /* XXX: overlapping string copy */
247*c2c66affSColin Finck /*
248*c2c66affSColin Finck * Do a recursive call.
249*c2c66affSColin Finck * We don't know how many escaped tokens there might be.
250*c2c66affSColin Finck */
251*c2c66affSColin Finck return (_get_next_token(cp, token));
252*c2c66affSColin Finck }
253*c2c66affSColin Finck }
254*c2c66affSColin Finck
255*c2c66affSColin Finck *cp++ = '\0'; /* null-terminate token */
256*c2c66affSColin Finck /* get rid of any backslash escapes */
257*c2c66affSColin Finck ep = npp;
258*c2c66affSColin Finck while ((np = strchr(ep, '\\')) != 0) {
259*c2c66affSColin Finck if (np[1] == '\\')
260*c2c66affSColin Finck np++;
261*c2c66affSColin Finck strcpy(np, (ep = &np[1])); /* XXX: overlapping string copy */
262*c2c66affSColin Finck }
263*c2c66affSColin Finck return (cp); /* return ptr to r-o-s */
264*c2c66affSColin Finck }
265