1 /* $NetBSD: getnetpath.c,v 1.6 2002/04/16 19:19:33 groo Exp $ */ 2 3 /* 4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 5 * unrestricted use provided that this legend is included on all tape 6 * media and as a part of the software program in whole or part. Users 7 * may copy or modify Sun RPC without charge, but are not authorized 8 * to license or distribute it to anyone else except as part of a product or 9 * program developed by the user or with the express written consent of 10 * Sun Microsystems, Inc. 11 * 12 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 13 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 14 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 15 * 16 * Sun RPC is provided with no support and without any obligation on the 17 * part of Sun Microsystems, Inc. to assist in its use, correction, 18 * modification or enhancement. 19 * 20 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 21 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 22 * OR ANY PART THEREOF. 23 * 24 * In no event will Sun Microsystems, Inc. be liable for any lost revenue 25 * or profits or other special, indirect and consequential damages, even if 26 * Sun has been advised of the possibility of such damages. 27 * 28 * Sun Microsystems, Inc. 29 * 2550 Garcia Avenue 30 * Mountain View, California 94043 31 */ 32 /* 33 #ifndef lint 34 static char sccsid[] = "@(#)getnetpath.c 1.11 91/12/19 SMI"; 35 #endif 36 */ 37 38 /* 39 * Copyright (c) 1989 by Sun Microsystems, Inc. 40 */ 41 42 #include "namespace.h" 43 #include <sys/cdefs.h> 44 #include <stdio.h> 45 #include <assert.h> 46 #include <errno.h> 47 #include <netconfig.h> 48 #include <stdlib.h> 49 #include <string.h> 50 #include <syslog.h> 51 52 #ifdef __weak_alias 53 __weak_alias(getnetpath,_getnetpath) 54 __weak_alias(setnetpath,_setnetpath) 55 #endif 56 57 /* 58 * internal structure to keep track of a netpath "session" 59 */ 60 struct netpath_chain { 61 struct netconfig *ncp; /* an nconf entry */ 62 struct netpath_chain *nchain_next; /* next nconf entry allocated */ 63 }; 64 65 66 struct netpath_vars { 67 int valid; /* token that indicates a valid netpath_vars */ 68 void *nc_handlep; /* handle for current netconfig "session" */ 69 char *netpath; /* pointer to current view-point in NETPATH */ 70 char *netpath_start; /* pointer to start of our copy of NETPATH */ 71 struct netpath_chain *ncp_list; /* list of nconfs allocated this session*/ 72 }; 73 74 #define NP_VALID 0xf00d 75 #define NP_INVALID 0 76 77 char *_get_next_token __P((char *, int)); 78 79 80 /* 81 * A call to setnetpath() establishes a NETPATH "session". setnetpath() 82 * must be called before the first call to getnetpath(). A "handle" is 83 * returned to distinguish the session; this handle should be passed 84 * subsequently to getnetpath(). (Handles are used to allow for nested calls 85 * to setnetpath()). 86 * If setnetpath() is unable to establish a session (due to lack of memory 87 * resources, or the absence of the /etc/netconfig file), a NULL pointer is 88 * returned. 89 */ 90 91 void * 92 setnetpath() 93 { 94 struct netpath_vars *np_sessionp; /* this session's variables */ 95 char *npp; /* NETPATH env variable */ 96 97 #ifdef MEM_CHK 98 malloc_debug(1); 99 #endif 100 101 if ((np_sessionp = (struct netpath_vars *) 102 malloc(sizeof (struct netpath_vars))) == NULL) 103 return (NULL); 104 if ((np_sessionp->nc_handlep = setnetconfig()) == NULL) { 105 syslog (LOG_ERR, "rpc: failed to open " NETCONFIG); 106 return (NULL); 107 } 108 np_sessionp->valid = NP_VALID; 109 np_sessionp->ncp_list = NULL; 110 if ((npp = getenv(NETPATH)) == NULL) 111 np_sessionp->netpath = NULL; 112 else { 113 (void) endnetconfig(np_sessionp->nc_handlep); 114 /* won't need nc session*/ 115 np_sessionp->nc_handlep = NULL; 116 if ((np_sessionp->netpath = malloc(strlen(npp)+1)) == NULL) { 117 free(np_sessionp); 118 return (NULL); 119 } else 120 (void) strcpy(np_sessionp->netpath, npp); 121 } 122 np_sessionp->netpath_start = np_sessionp->netpath; 123 return ((void *)np_sessionp); 124 } 125 126 /* 127 * When first called, getnetpath() returns a pointer to the netconfig 128 * database entry corresponding to the first valid NETPATH component. The 129 * netconfig entry is formatted as a struct netconfig. 130 * On each subsequent call, getnetpath returns a pointer to the netconfig 131 * entry that corresponds to the next valid NETPATH component. getnetpath 132 * can thus be used to search the netconfig database for all networks 133 * included in the NETPATH variable. 134 * When NETPATH has been exhausted, getnetpath() returns NULL. It returns 135 * NULL and sets errno in case of an error (e.g., setnetpath was not called 136 * previously). 137 * getnetpath() silently ignores invalid NETPATH components. A NETPATH 138 * compnent is invalid if there is no corresponding entry in the netconfig 139 * database. 140 * If the NETPATH variable is unset, getnetpath() behaves as if NETPATH 141 * were set to the sequence of default or visible networks in the netconfig 142 * database, in the order in which they are listed. 143 */ 144 145 struct netconfig * 146 getnetpath(handlep) 147 void *handlep; 148 { 149 struct netpath_vars *np_sessionp = (struct netpath_vars *)handlep; 150 struct netconfig *ncp = NULL; /* temp. holds a netconfig session */ 151 struct netpath_chain *chainp; /* holds chain of ncp's we alloc */ 152 char *npp; /* holds current NETPATH */ 153 154 if (np_sessionp == NULL || np_sessionp->valid != NP_VALID) { 155 errno = EINVAL; 156 return (NULL); 157 } 158 if (np_sessionp->netpath_start == NULL) { /* NETPATH was not set */ 159 do { /* select next visible network */ 160 if (np_sessionp->nc_handlep == NULL) { 161 np_sessionp->nc_handlep = setnetconfig(); 162 if (np_sessionp->nc_handlep == NULL) 163 syslog (LOG_ERR, 164 "rpc: failed to open " NETCONFIG); 165 } 166 if ((ncp = getnetconfig(np_sessionp->nc_handlep)) 167 == NULL) 168 return(NULL); 169 } while ((ncp->nc_flag & NC_VISIBLE) == 0); 170 return (ncp); 171 } 172 /* 173 * Find first valid network ID in netpath. 174 */ 175 while ((npp = np_sessionp->netpath) != NULL && strlen(npp) != 0) { 176 np_sessionp->netpath = _get_next_token(npp, ':'); 177 /* 178 * npp is a network identifier. 179 */ 180 if ((ncp = getnetconfigent(npp)) != NULL) { 181 /* cobble alloc chain entry */ 182 chainp = (struct netpath_chain *) 183 malloc(sizeof (struct netpath_chain)); 184 chainp->ncp = ncp; 185 chainp->nchain_next = NULL; 186 if (np_sessionp->ncp_list == NULL) 187 np_sessionp->ncp_list = chainp; 188 else 189 np_sessionp->ncp_list->nchain_next = chainp; 190 return (ncp); 191 } 192 /* couldn't find this token in the database; go to next one. */ 193 } 194 return (NULL); 195 } 196 197 /* 198 * endnetpath() may be called to unbind NETPATH when processing is complete, 199 * releasing resources for reuse. It returns 0 on success and -1 on failure 200 * (e.g. if setnetpath() was not called previously. 201 */ 202 int 203 endnetpath(handlep) 204 void *handlep; 205 { 206 struct netpath_vars *np_sessionp = (struct netpath_vars *)handlep; 207 struct netpath_chain *chainp, *lastp; 208 209 if (np_sessionp == NULL || np_sessionp->valid != NP_VALID) { 210 errno = EINVAL; 211 return (-1); 212 } 213 if (np_sessionp->nc_handlep != NULL) 214 endnetconfig(np_sessionp->nc_handlep); 215 if (np_sessionp->netpath_start != NULL) 216 free(np_sessionp->netpath_start); 217 for (chainp = np_sessionp->ncp_list; chainp != NULL; 218 lastp=chainp, chainp=chainp->nchain_next, free(lastp)) { 219 freenetconfigent(chainp->ncp); 220 } 221 free(np_sessionp); 222 #ifdef MEM_CHK 223 if (malloc_verify() == 0) { 224 fprintf(stderr, "memory heap corrupted in endnetpath\n"); 225 exit(1); 226 } 227 #endif 228 return (0); 229 } 230 231 232 /* 233 * Returns pointer to the rest-of-the-string after the current token. 234 * The token itself starts at arg, and we null terminate it. We return NULL 235 * if either the arg is empty, or if this is the last token. 236 */ 237 238 char * 239 _get_next_token(npp, token) 240 char *npp; /* string */ 241 int token; /* char to parse string for */ 242 { 243 char *cp; /* char pointer */ 244 char *np; /* netpath pointer */ 245 char *ep; /* escape pointer */ 246 247 _DIAGASSERT(npp != NULL); 248 _DIAGASSERT(token != NULL); 249 250 if ((cp = strchr(npp, token)) == NULL) 251 return (NULL); 252 /* 253 * did find a token, but it might be escaped. 254 */ 255 if ((cp > npp) && (cp[-1] == '\\')) { 256 /* 257 * if slash was also escaped, carry on, otherwise find 258 * next token 259 */ 260 if ((cp > npp + 1) && (cp[-2] != '\\')) { 261 /* shift r-o-s onto the escaped token */ 262 strcpy(&cp[-1], cp); /* XXX: overlapping string copy */ 263 /* 264 * Do a recursive call. 265 * We don't know how many escaped tokens there might be. 266 */ 267 return (_get_next_token(cp, token)); 268 } 269 } 270 271 *cp++ = '\0'; /* null-terminate token */ 272 /* get rid of any backslash escapes */ 273 ep = npp; 274 while ((np = strchr(ep, '\\')) != 0) { 275 if (np[1] == '\\') 276 np++; 277 strcpy(np, (ep = &np[1])); /* XXX: overlapping string copy */ 278 } 279 return (cp); /* return ptr to r-o-s */ 280 } 281