1 /* $NetBSD: getnetpath.c,v 1.8 2003/09/09 03:56:40 itojun 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 #include <sys/cdefs.h> 34 #if defined(LIBC_SCCS) && !defined(lint) 35 #if 0 36 static char sccsid[] = "@(#)getnetpath.c 1.11 91/12/19 SMI"; 37 #else 38 __RCSID("$NetBSD: getnetpath.c,v 1.8 2003/09/09 03:56:40 itojun Exp $"); 39 #endif 40 #endif 41 42 /* 43 * Copyright (c) 1989 by Sun Microsystems, Inc. 44 */ 45 46 #include "namespace.h" 47 #include <sys/cdefs.h> 48 #include <stdio.h> 49 #include <assert.h> 50 #include <errno.h> 51 #include <netconfig.h> 52 #include <stdlib.h> 53 #include <string.h> 54 #include <syslog.h> 55 56 #ifdef __weak_alias 57 __weak_alias(getnetpath,_getnetpath) 58 __weak_alias(setnetpath,_setnetpath) 59 #endif 60 61 /* 62 * internal structure to keep track of a netpath "session" 63 */ 64 struct netpath_chain { 65 struct netconfig *ncp; /* an nconf entry */ 66 struct netpath_chain *nchain_next; /* next nconf entry allocated */ 67 }; 68 69 70 struct netpath_vars { 71 int valid; /* token that indicates a valid netpath_vars */ 72 void *nc_handlep; /* handle for current netconfig "session" */ 73 char *netpath; /* pointer to current view-point in NETPATH */ 74 char *netpath_start; /* pointer to start of our copy of NETPATH */ 75 struct netpath_chain *ncp_list; /* list of nconfs allocated this session*/ 76 }; 77 78 #define NP_VALID 0xf00d 79 #define NP_INVALID 0 80 81 char *_get_next_token __P((char *, int)); 82 83 84 /* 85 * A call to setnetpath() establishes a NETPATH "session". setnetpath() 86 * must be called before the first call to getnetpath(). A "handle" is 87 * returned to distinguish the session; this handle should be passed 88 * subsequently to getnetpath(). (Handles are used to allow for nested calls 89 * to setnetpath()). 90 * If setnetpath() is unable to establish a session (due to lack of memory 91 * resources, or the absence of the /etc/netconfig file), a NULL pointer is 92 * returned. 93 */ 94 95 void * 96 setnetpath() 97 { 98 struct netpath_vars *np_sessionp; /* this session's variables */ 99 char *npp; /* NETPATH env variable */ 100 101 #ifdef MEM_CHK 102 malloc_debug(1); 103 #endif 104 105 if ((np_sessionp = (struct netpath_vars *) 106 malloc(sizeof (struct netpath_vars))) == NULL) 107 return (NULL); 108 if ((np_sessionp->nc_handlep = setnetconfig()) == NULL) { 109 syslog (LOG_ERR, "rpc: failed to open " NETCONFIG); 110 return (NULL); 111 } 112 np_sessionp->valid = NP_VALID; 113 np_sessionp->ncp_list = NULL; 114 if ((npp = getenv(NETPATH)) == NULL) 115 np_sessionp->netpath = NULL; 116 else { 117 (void) endnetconfig(np_sessionp->nc_handlep); 118 /* won't need nc session*/ 119 np_sessionp->nc_handlep = NULL; 120 if ((np_sessionp->netpath = malloc(strlen(npp)+1)) == NULL) { 121 free(np_sessionp); 122 return (NULL); 123 } else 124 (void) strcpy(np_sessionp->netpath, npp); 125 } 126 np_sessionp->netpath_start = np_sessionp->netpath; 127 return ((void *)np_sessionp); 128 } 129 130 /* 131 * When first called, getnetpath() returns a pointer to the netconfig 132 * database entry corresponding to the first valid NETPATH component. The 133 * netconfig entry is formatted as a struct netconfig. 134 * On each subsequent call, getnetpath returns a pointer to the netconfig 135 * entry that corresponds to the next valid NETPATH component. getnetpath 136 * can thus be used to search the netconfig database for all networks 137 * included in the NETPATH variable. 138 * When NETPATH has been exhausted, getnetpath() returns NULL. It returns 139 * NULL and sets errno in case of an error (e.g., setnetpath was not called 140 * previously). 141 * getnetpath() silently ignores invalid NETPATH components. A NETPATH 142 * compnent is invalid if there is no corresponding entry in the netconfig 143 * database. 144 * If the NETPATH variable is unset, getnetpath() behaves as if NETPATH 145 * were set to the sequence of default or visible networks in the netconfig 146 * database, in the order in which they are listed. 147 */ 148 149 struct netconfig * 150 getnetpath(handlep) 151 void *handlep; 152 { 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 */ 157 158 if (np_sessionp == NULL || np_sessionp->valid != NP_VALID) { 159 errno = EINVAL; 160 return (NULL); 161 } 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) 167 syslog (LOG_ERR, 168 "rpc: failed to open " NETCONFIG); 169 } 170 if ((ncp = getnetconfig(np_sessionp->nc_handlep)) 171 == NULL) 172 return(NULL); 173 } while ((ncp->nc_flag & NC_VISIBLE) == 0); 174 return (ncp); 175 } 176 /* 177 * Find first valid network ID in netpath. 178 */ 179 while ((npp = np_sessionp->netpath) != NULL && strlen(npp) != 0) { 180 np_sessionp->netpath = _get_next_token(npp, ':'); 181 /* 182 * npp is a network identifier. 183 */ 184 if ((ncp = getnetconfigent(npp)) != NULL) { 185 /* cobble alloc chain entry */ 186 chainp = (struct netpath_chain *) 187 malloc(sizeof (struct netpath_chain)); 188 chainp->ncp = ncp; 189 chainp->nchain_next = NULL; 190 if (np_sessionp->ncp_list == NULL) 191 np_sessionp->ncp_list = chainp; 192 else 193 np_sessionp->ncp_list->nchain_next = chainp; 194 return (ncp); 195 } 196 /* couldn't find this token in the database; go to next one. */ 197 } 198 return (NULL); 199 } 200 201 /* 202 * endnetpath() may be called to unbind NETPATH when processing is complete, 203 * releasing resources for reuse. It returns 0 on success and -1 on failure 204 * (e.g. if setnetpath() was not called previously. 205 */ 206 int 207 endnetpath(handlep) 208 void *handlep; 209 { 210 struct netpath_vars *np_sessionp = (struct netpath_vars *)handlep; 211 struct netpath_chain *chainp, *lastp; 212 213 if (np_sessionp == NULL || np_sessionp->valid != NP_VALID) { 214 errno = EINVAL; 215 return (-1); 216 } 217 if (np_sessionp->nc_handlep != NULL) 218 endnetconfig(np_sessionp->nc_handlep); 219 if (np_sessionp->netpath_start != NULL) 220 free(np_sessionp->netpath_start); 221 for (chainp = np_sessionp->ncp_list; chainp != NULL; 222 lastp=chainp, chainp=chainp->nchain_next, free(lastp)) { 223 freenetconfigent(chainp->ncp); 224 } 225 free(np_sessionp); 226 #ifdef MEM_CHK 227 if (malloc_verify() == 0) { 228 fprintf(stderr, "memory heap corrupted in endnetpath\n"); 229 exit(1); 230 } 231 #endif 232 return (0); 233 } 234 235 236 /* 237 * Returns pointer to the rest-of-the-string after the current token. 238 * The token itself starts at arg, and we null terminate it. We return NULL 239 * if either the arg is empty, or if this is the last token. 240 */ 241 242 char * 243 _get_next_token(npp, token) 244 char *npp; /* string */ 245 int token; /* char to parse string for */ 246 { 247 char *cp; /* char pointer */ 248 char *np; /* netpath pointer */ 249 char *ep; /* escape pointer */ 250 251 _DIAGASSERT(npp != NULL); 252 _DIAGASSERT(token != NULL); 253 254 if ((cp = strchr(npp, token)) == NULL) 255 return (NULL); 256 /* 257 * did find a token, but it might be escaped. 258 */ 259 if ((cp > npp) && (cp[-1] == '\\')) { 260 /* 261 * if slash was also escaped, carry on, otherwise find 262 * next token 263 */ 264 if ((cp > npp + 1) && (cp[-2] != '\\')) { 265 /* shift r-o-s onto the escaped token */ 266 strcpy(&cp[-1], cp); /* XXX: overlapping string copy */ 267 /* 268 * Do a recursive call. 269 * We don't know how many escaped tokens there might be. 270 */ 271 return (_get_next_token(cp, token)); 272 } 273 } 274 275 *cp++ = '\0'; /* null-terminate token */ 276 /* get rid of any backslash escapes */ 277 ep = npp; 278 while ((np = strchr(ep, '\\')) != 0) { 279 if (np[1] == '\\') 280 np++; 281 strcpy(np, (ep = &np[1])); /* XXX: overlapping string copy */ 282 } 283 return (cp); /* return ptr to r-o-s */ 284 } 285