1 /* $NetBSD: getrpcent.c,v 1.17 2000/01/22 22:19:17 mycroft 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 #if defined(LIBC_SCCS) && !defined(lint) 34 static char *sccsid = "@(#)getrpcent.c 1.14 91/03/11 Copyr 1984 Sun Micro"; 35 #endif 36 #include <sys/cdefs.h> 37 __FBSDID("$FreeBSD$"); 38 39 /* 40 * Copyright (c) 1984 by Sun Microsystems, Inc. 41 */ 42 43 #include "namespace.h" 44 #include <sys/types.h> 45 46 #include <netinet/in.h> 47 #include <arpa/inet.h> 48 49 #include <assert.h> 50 #include <netdb.h> 51 #include <stdio.h> 52 #include <stdlib.h> 53 #include <string.h> 54 55 #include <rpc/rpc.h> 56 #ifdef YP 57 #include <rpcsvc/yp_prot.h> 58 #include <rpcsvc/ypclnt.h> 59 #endif 60 #include "un-namespace.h" 61 #include "libc_private.h" 62 63 /* 64 * Internet version. 65 */ 66 static struct rpcdata { 67 FILE *rpcf; 68 int stayopen; 69 #define MAXALIASES 35 70 char *rpc_aliases[MAXALIASES]; 71 struct rpcent rpc; 72 char line[BUFSIZ+1]; 73 #ifdef YP 74 char *domain; 75 char *current; 76 int currentlen; 77 #endif 78 } *rpcdata; 79 80 static struct rpcent *interpret(char *val, size_t len); 81 82 #ifdef YP 83 static int __yp_nomap = 0; 84 #endif /* YP */ 85 86 #define RPCDB "/etc/rpc" 87 88 static struct rpcdata *_rpcdata(void); 89 90 static struct rpcdata * 91 _rpcdata() 92 { 93 struct rpcdata *d = rpcdata; 94 95 if (d == 0) { 96 d = (struct rpcdata *)calloc(1, sizeof (struct rpcdata)); 97 rpcdata = d; 98 } 99 return (d); 100 } 101 102 struct rpcent * 103 getrpcbynumber(number) 104 int number; 105 { 106 #ifdef YP 107 int reason; 108 char adrstr[16]; 109 #endif 110 struct rpcent *p; 111 struct rpcdata *d = _rpcdata(); 112 113 if (d == 0) 114 return (0); 115 #ifdef YP 116 if (!__yp_nomap && _yp_check(&d->domain)) { 117 sprintf(adrstr, "%d", number); 118 reason = yp_match(d->domain, "rpc.bynumber", adrstr, strlen(adrstr), 119 &d->current, &d->currentlen); 120 switch(reason) { 121 case 0: 122 break; 123 case YPERR_MAP: 124 __yp_nomap = 1; 125 goto no_yp; 126 break; 127 default: 128 return(0); 129 break; 130 } 131 d->current[d->currentlen] = '\0'; 132 p = interpret(d->current, d->currentlen); 133 (void) free(d->current); 134 return p; 135 } 136 no_yp: 137 #endif /* YP */ 138 139 setrpcent(0); 140 while ((p = getrpcent()) != NULL) { 141 if (p->r_number == number) 142 break; 143 } 144 endrpcent(); 145 return (p); 146 } 147 148 struct rpcent * 149 getrpcbyname(name) 150 char *name; 151 { 152 struct rpcent *rpc = NULL; 153 char **rp; 154 155 assert(name != NULL); 156 157 setrpcent(0); 158 while ((rpc = getrpcent()) != NULL) { 159 if (strcmp(rpc->r_name, name) == 0) 160 goto done; 161 for (rp = rpc->r_aliases; *rp != NULL; rp++) { 162 if (strcmp(*rp, name) == 0) 163 goto done; 164 } 165 } 166 done: 167 endrpcent(); 168 return (rpc); 169 } 170 171 void 172 setrpcent(f) 173 int f; 174 { 175 struct rpcdata *d = _rpcdata(); 176 177 if (d == 0) 178 return; 179 #ifdef YP 180 if (!__yp_nomap && _yp_check(NULL)) { 181 if (d->current) 182 free(d->current); 183 d->current = NULL; 184 d->currentlen = 0; 185 return; 186 } 187 __yp_nomap = 0; 188 #endif /* YP */ 189 if (d->rpcf == NULL) 190 d->rpcf = fopen(RPCDB, "r"); 191 else 192 rewind(d->rpcf); 193 d->stayopen |= f; 194 } 195 196 void 197 endrpcent() 198 { 199 struct rpcdata *d = _rpcdata(); 200 201 if (d == 0) 202 return; 203 #ifdef YP 204 if (!__yp_nomap && _yp_check(NULL)) { 205 if (d->current && !d->stayopen) 206 free(d->current); 207 d->current = NULL; 208 d->currentlen = 0; 209 return; 210 } 211 __yp_nomap = 0; 212 #endif /* YP */ 213 if (d->rpcf && !d->stayopen) { 214 fclose(d->rpcf); 215 d->rpcf = NULL; 216 } 217 } 218 219 struct rpcent * 220 getrpcent() 221 { 222 struct rpcdata *d = _rpcdata(); 223 #ifdef YP 224 struct rpcent *hp; 225 int reason; 226 char *val = NULL; 227 int vallen; 228 #endif 229 230 if (d == 0) 231 return(NULL); 232 #ifdef YP 233 if (!__yp_nomap && _yp_check(&d->domain)) { 234 if (d->current == NULL && d->currentlen == 0) { 235 reason = yp_first(d->domain, "rpc.bynumber", 236 &d->current, &d->currentlen, 237 &val, &vallen); 238 } else { 239 reason = yp_next(d->domain, "rpc.bynumber", 240 d->current, d->currentlen, 241 &d->current, &d->currentlen, 242 &val, &vallen); 243 } 244 switch(reason) { 245 case 0: 246 break; 247 case YPERR_MAP: 248 __yp_nomap = 1; 249 goto no_yp; 250 break; 251 default: 252 return(0); 253 break; 254 } 255 val[vallen] = '\0'; 256 hp = interpret(val, vallen); 257 (void) free(val); 258 return hp; 259 } 260 no_yp: 261 #endif /* YP */ 262 if (d->rpcf == NULL && (d->rpcf = fopen(RPCDB, "r")) == NULL) 263 return (NULL); 264 /* -1 so there is room to append a \n below */ 265 if (fgets(d->line, BUFSIZ - 1, d->rpcf) == NULL) 266 return (NULL); 267 return (interpret(d->line, strlen(d->line))); 268 } 269 270 static struct rpcent * 271 interpret(val, len) 272 char *val; 273 size_t len; 274 { 275 struct rpcdata *d = _rpcdata(); 276 char *p; 277 char *cp, **q; 278 279 assert(val != NULL); 280 281 if (d == 0) 282 return (0); 283 (void) strncpy(d->line, val, BUFSIZ); 284 d->line[BUFSIZ] = '\0'; 285 p = d->line; 286 p[len] = '\n'; 287 if (*p == '#') 288 return (getrpcent()); 289 cp = strpbrk(p, "#\n"); 290 if (cp == NULL) 291 return (getrpcent()); 292 *cp = '\0'; 293 cp = strpbrk(p, " \t"); 294 if (cp == NULL) 295 return (getrpcent()); 296 *cp++ = '\0'; 297 /* THIS STUFF IS INTERNET SPECIFIC */ 298 d->rpc.r_name = d->line; 299 while (*cp == ' ' || *cp == '\t') 300 cp++; 301 d->rpc.r_number = atoi(cp); 302 q = d->rpc.r_aliases = d->rpc_aliases; 303 cp = strpbrk(cp, " \t"); 304 if (cp != NULL) 305 *cp++ = '\0'; 306 while (cp && *cp) { 307 if (*cp == ' ' || *cp == '\t') { 308 cp++; 309 continue; 310 } 311 if (q < &(d->rpc_aliases[MAXALIASES - 1])) 312 *q++ = cp; 313 cp = strpbrk(cp, " \t"); 314 if (cp != NULL) 315 *cp++ = '\0'; 316 } 317 *q = NULL; 318 return (&d->rpc); 319 } 320 321