Sweep-fix comparing pointers with 0 (and assigning 0 to pointers).
[dragonfly.git] / lib / libc / rpc / getnetpath.c
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  */
34
35 /*
36  * Copyright (c) 1989 by Sun Microsystems, Inc.
37  */
38
39 #include "namespace.h"
40 #include <stdio.h>
41 #include <errno.h>
42 #include <netconfig.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <syslog.h>
46 #include "un-namespace.h"
47
48 /*
49  * internal structure to keep track of a netpath "session"
50  */
51 struct netpath_chain {
52     struct netconfig *ncp;  /* an nconf entry */
53     struct netpath_chain *nchain_next;  /* next nconf entry allocated */
54 };
55
56
57 struct netpath_vars {
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*/
63 };
64
65 #define NP_VALID        0xf00d
66 #define NP_INVALID      0
67
68 char *_get_next_token(char *, int);
69
70
71 /*
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
76  * to setnetpath()).
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
79  * returned.
80  */
81
82 void *
83 setnetpath(void)
84 {
85
86     struct netpath_vars *np_sessionp;   /* this session's variables */
87     char *npp;                          /* NETPATH env variable */
88
89 #ifdef MEM_CHK
90     malloc_debug(1);
91 #endif
92
93     if ((np_sessionp =
94         (struct netpath_vars *)malloc(sizeof (struct netpath_vars))) == NULL) {
95         return (NULL);
96     }
97     if ((np_sessionp->nc_handlep = setnetconfig()) == NULL) {
98         free(np_sessionp);
99         syslog (LOG_ERR, "rpc: failed to open " NETCONFIG);
100         goto failed;
101     }
102     np_sessionp->valid = NP_VALID;
103     np_sessionp->ncp_list = NULL;
104     if ((npp = getenv(NETPATH)) == NULL) {
105         np_sessionp->netpath = NULL;
106     } else {
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)
110                 goto failed;
111         else {
112             strcpy(np_sessionp->netpath, npp);
113         }
114     }
115     np_sessionp->netpath_start = np_sessionp->netpath;
116     return ((void *)np_sessionp);
117
118 failed:
119     free(np_sessionp);
120     return (NULL);
121 }
122
123 /*
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
133  * previously).
134  * getnetpath() silently ignores invalid NETPATH components.  A NETPATH
135  * compnent is invalid if there is no corresponding entry in the netconfig
136  * database.
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.
140  */
141
142 struct netconfig *
143 getnetpath(void *handlep)
144 {
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 */
149
150     if (np_sessionp == NULL || np_sessionp->valid != NP_VALID) {
151         errno = EINVAL;
152         return (NULL);
153     }
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);
160             }
161             if ((ncp = getnetconfig(np_sessionp->nc_handlep)) == NULL) {
162                 return(NULL);
163             }
164         } while ((ncp->nc_flag & NC_VISIBLE) == 0);
165         return (ncp);
166     }
167     /*
168      * Find first valid network ID in netpath.
169      */
170     while ((npp = np_sessionp->netpath) != NULL && strlen(npp) != 0) {
171         np_sessionp->netpath = _get_next_token(npp, ':');
172         /*
173          * npp is a network identifier.
174          */
175         if ((ncp = getnetconfigent(npp)) != NULL) {
176             chainp = (struct netpath_chain *)   /* cobble alloc chain entry */
177                     malloc(sizeof (struct netpath_chain));
178             chainp->ncp = ncp;
179             chainp->nchain_next = NULL;
180             if (np_sessionp->ncp_list == NULL) {
181                 np_sessionp->ncp_list = chainp;
182             } else {
183                 np_sessionp->ncp_list->nchain_next = chainp;
184             }
185             return (ncp);
186         }
187         /* couldn't find this token in the database; go to next one. */
188     }
189     return (NULL);
190 }
191
192 /*
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.
196  */
197 int
198 endnetpath(void *handlep)
199 {
200     struct netpath_vars *np_sessionp = (struct netpath_vars *)handlep;
201     struct netpath_chain *chainp, *lastp;
202
203     if (np_sessionp == NULL || np_sessionp->valid != NP_VALID) {
204         errno = EINVAL;
205         return (-1);
206     }
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);
214     }
215     free(np_sessionp);
216 #ifdef MEM_CHK
217     if (malloc_verify() == 0) {
218         fprintf(stderr, "memory heap corrupted in endnetpath\n");
219         exit(1);
220     }
221 #endif
222     return (0);
223 }
224
225
226
227 /*
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.
231  */
232
233 char *
234 _get_next_token(char *npp,              /* string */
235                 int token)              /* char to parse string for */
236 {
237     char  *cp;          /* char pointer */
238     char  *np;          /* netpath pointer */
239     char  *ep;          /* escape pointer */
240
241     if ((cp = strchr(npp, token)) == NULL) {
242         return (NULL);
243     }
244     /*
245      * did find a token, but it might be escaped.
246      */
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 */
252             /*
253              * Do a recursive call.
254              * We don't know how many escaped tokens there might be.
255              */
256             return (_get_next_token(cp, token));
257         }
258     }
259
260     *cp++ = '\0';               /* null-terminate token */
261     /* get rid of any backslash escapes */
262     ep = npp;
263     while ((np = strchr(ep, '\\')) != NULL) {
264         if (np[1] == '\\')
265             np++;
266         strcpy(np, (ep = &np[1]));  /* XXX: overlapping string copy */
267     }
268     return (cp);                /* return ptr to r-o-s */
269 }