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 syslog (LOG_ERR, "rpc: failed to open " NETCONFIG); 99 goto failed; 100 } 101 np_sessionp->valid = NP_VALID; 102 np_sessionp->ncp_list = NULL; 103 if ((npp = getenv(NETPATH)) == NULL) { 104 np_sessionp->netpath = NULL; 105 } else { 106 endnetconfig(np_sessionp->nc_handlep);/* won't need nc session*/ 107 np_sessionp->nc_handlep = NULL; 108 if ((np_sessionp->netpath = malloc(strlen(npp)+1)) == NULL) 109 goto failed; 110 else { 111 strcpy(np_sessionp->netpath, npp); 112 } 113 } 114 np_sessionp->netpath_start = np_sessionp->netpath; 115 return ((void *)np_sessionp); 116 117 failed: 118 free(np_sessionp); 119 return (NULL); 120 } 121 122 /* 123 * When first called, getnetpath() returns a pointer to the netconfig 124 * database entry corresponding to the first valid NETPATH component. The 125 * netconfig entry is formatted as a struct netconfig. 126 * On each subsequent call, getnetpath returns a pointer to the netconfig 127 * entry that corresponds to the next valid NETPATH component. getnetpath 128 * can thus be used to search the netconfig database for all networks 129 * included in the NETPATH variable. 130 * When NETPATH has been exhausted, getnetpath() returns NULL. It returns 131 * NULL and sets errno in case of an error (e.g., setnetpath was not called 132 * previously). 133 * getnetpath() silently ignores invalid NETPATH components. A NETPATH 134 * compnent is invalid if there is no corresponding entry in the netconfig 135 * database. 136 * If the NETPATH variable is unset, getnetpath() behaves as if NETPATH 137 * were set to the sequence of default or visible networks in the netconfig 138 * database, in the order in which they are listed. 139 */ 140 141 struct netconfig * 142 getnetpath(void *handlep) 143 { 144 struct netpath_vars *np_sessionp = (struct netpath_vars *)handlep; 145 struct netconfig *ncp = NULL; /* temp. holds a netconfig session */ 146 struct netpath_chain *chainp; /* holds chain of ncp's we alloc */ 147 char *npp; /* holds current NETPATH */ 148 149 if (np_sessionp == NULL || np_sessionp->valid != NP_VALID) { 150 errno = EINVAL; 151 return (NULL); 152 } 153 if (np_sessionp->netpath_start == NULL) { /* NETPATH was not set */ 154 do { /* select next visible network */ 155 if (np_sessionp->nc_handlep == NULL) { 156 np_sessionp->nc_handlep = setnetconfig(); 157 if (np_sessionp->nc_handlep == NULL) 158 syslog (LOG_ERR, "rpc: failed to open " NETCONFIG); 159 } 160 if ((ncp = getnetconfig(np_sessionp->nc_handlep)) == NULL) { 161 return(NULL); 162 } 163 } while ((ncp->nc_flag & NC_VISIBLE) == 0); 164 return (ncp); 165 } 166 /* 167 * Find first valid network ID in netpath. 168 */ 169 while ((npp = np_sessionp->netpath) != NULL && strlen(npp) != 0) { 170 np_sessionp->netpath = _get_next_token(npp, ':'); 171 /* 172 * npp is a network identifier. 173 */ 174 if ((ncp = getnetconfigent(npp)) != NULL) { 175 chainp = (struct netpath_chain *) /* cobble alloc chain entry */ 176 malloc(sizeof (struct netpath_chain)); 177 chainp->ncp = ncp; 178 chainp->nchain_next = NULL; 179 if (np_sessionp->ncp_list == NULL) { 180 np_sessionp->ncp_list = chainp; 181 } else { 182 np_sessionp->ncp_list->nchain_next = chainp; 183 } 184 return (ncp); 185 } 186 /* couldn't find this token in the database; go to next one. */ 187 } 188 return (NULL); 189 } 190 191 /* 192 * endnetpath() may be called to unbind NETPATH when processing is complete, 193 * releasing resources for reuse. It returns 0 on success and -1 on failure 194 * (e.g. if setnetpath() was not called previously. 195 */ 196 int 197 endnetpath(void *handlep) 198 { 199 struct netpath_vars *np_sessionp = (struct netpath_vars *)handlep; 200 struct netpath_chain *chainp, *lastp; 201 202 if (np_sessionp == NULL || np_sessionp->valid != NP_VALID) { 203 errno = EINVAL; 204 return (-1); 205 } 206 if (np_sessionp->nc_handlep != NULL) 207 endnetconfig(np_sessionp->nc_handlep); 208 if (np_sessionp->netpath_start != NULL) 209 free(np_sessionp->netpath_start); 210 for (chainp = np_sessionp->ncp_list; chainp != NULL; 211 lastp=chainp, chainp=chainp->nchain_next, free(lastp)) { 212 freenetconfigent(chainp->ncp); 213 } 214 free(np_sessionp); 215 #ifdef MEM_CHK 216 if (malloc_verify() == 0) { 217 fprintf(stderr, "memory heap corrupted in endnetpath\n"); 218 exit(1); 219 } 220 #endif 221 return (0); 222 } 223 224 225 226 /* 227 * Returns pointer to the rest-of-the-string after the current token. 228 * The token itself starts at arg, and we null terminate it. We return NULL 229 * if either the arg is empty, or if this is the last token. 230 */ 231 232 char * 233 _get_next_token(char *npp, /* string */ 234 int token) /* char to parse string for */ 235 { 236 char *cp; /* char pointer */ 237 char *np; /* netpath pointer */ 238 char *ep; /* escape pointer */ 239 240 if ((cp = strchr(npp, token)) == NULL) { 241 return (NULL); 242 } 243 /* 244 * did find a token, but it might be escaped. 245 */ 246 if ((cp > npp) && (cp[-1] == '\\')) { 247 /* if slash was also escaped, carry on, otherwise find next token */ 248 if ((cp > npp + 1) && (cp[-2] != '\\')) { 249 /* shift r-o-s onto the escaped token */ 250 strcpy(&cp[-1], cp); /* XXX: overlapping string copy */ 251 /* 252 * Do a recursive call. 253 * We don't know how many escaped tokens there might be. 254 */ 255 return (_get_next_token(cp, token)); 256 } 257 } 258 259 *cp++ = '\0'; /* null-terminate token */ 260 /* get rid of any backslash escapes */ 261 ep = npp; 262 while ((np = strchr(ep, '\\')) != NULL) { 263 if (np[1] == '\\') 264 np++; 265 strcpy(np, (ep = &np[1])); /* XXX: overlapping string copy */ 266 } 267 return (cp); /* return ptr to r-o-s */ 268 } 269