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