1 /* $NetBSD: getnetconfig.c,v 1.9 2002/11/11 17:13:59 thorpej 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[] = "@(#)getnetconfig.c 1.12 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 <stddef.h> 49 #include <stdlib.h> 50 #include <string.h> 51 #include <rpc/rpc.h> 52 #include "rpc_internal.h" 53 54 #ifdef __weak_alias 55 __weak_alias(getnetconfig,_getnetconfig) 56 __weak_alias(setnetconfig,_setnetconfig) 57 __weak_alias(endnetconfig,_endnetconfig) 58 __weak_alias(getnetconfigent,_getnetconfigent) 59 __weak_alias(freenetconfigent,_freenetconfigent) 60 __weak_alias(nc_perror,_nc_perror) 61 __weak_alias(nc_sperror,_nc_sperror) 62 #endif 63 64 /* 65 * The five library routines in this file provide application access to the 66 * system network configuration database, /etc/netconfig. In addition to the 67 * netconfig database and the routines for accessing it, the environment 68 * variable NETPATH and its corresponding routines in getnetpath.c may also be 69 * used to specify the network transport to be used. 70 */ 71 72 73 /* 74 * netconfig errors 75 */ 76 77 #define NC_NONETCONFIG ENOENT 78 #define NC_NOMEM ENOMEM 79 #define NC_NOTINIT EINVAL /* setnetconfig was not called first */ 80 #define NC_BADFILE EBADF /* format for netconfig file is bad */ 81 82 /* 83 * semantics as strings (should be in netconfig.h) 84 */ 85 #define NC_TPI_CLTS_S "tpi_clts" 86 #define NC_TPI_COTS_S "tpi_cots" 87 #define NC_TPI_COTS_ORD_S "tpi_cots_ord" 88 #define NC_TPI_RAW_S "tpi_raw" 89 90 /* 91 * flags as characters (also should be in netconfig.h) 92 */ 93 #define NC_NOFLAG_C '-' 94 #define NC_VISIBLE_C 'v' 95 #define NC_BROADCAST_C 'b' 96 97 /* 98 * Character used to indicate there is no name-to-address lookup library 99 */ 100 #define NC_NOLOOKUP "-" 101 102 static const char * const _nc_errors[] = { 103 "Netconfig database not found", 104 "Not enough memory", 105 "Not initialized", 106 "Netconfig database has invalid format" 107 }; 108 109 struct netconfig_info { 110 int eof; /* all entries has been read */ 111 int ref; /* # of times setnetconfig() has been called */ 112 struct netconfig_list *head; /* head of the list */ 113 struct netconfig_list *tail; /* last of the list */ 114 }; 115 116 struct netconfig_list { 117 char *linep; /* hold line read from netconfig */ 118 struct netconfig *ncp; 119 struct netconfig_list *next; 120 }; 121 122 struct netconfig_vars { 123 int valid; /* token that indicates valid netconfig_vars */ 124 int flag; /* first time flag */ 125 struct netconfig_list *nc_configs; 126 /* pointer to the current netconfig entry */ 127 }; 128 129 #define NC_VALID 0xfeed 130 #define NC_STORAGE 0xf00d 131 #define NC_INVALID 0 132 133 134 static int *__nc_error __P((void)); 135 static int parse_ncp __P((char *, struct netconfig *)); 136 static struct netconfig *dup_ncp __P((struct netconfig *)); 137 138 139 static FILE *nc_file; /* for netconfig db */ 140 static struct netconfig_info ni = { 0, 0, NULL, NULL}; 141 142 #define MAXNETCONFIGLINE 1000 143 144 static int * 145 __nc_error() 146 { 147 #ifdef __REENT 148 static thread_key_t nc_key = 0; 149 int *nc_addr = NULL; 150 #endif 151 static int nc_error = 0; 152 153 #ifdef __REENT 154 if (_thr_getspecific(nc_key, (void **) &nc_addr) != 0) { 155 mutex_lock(&nc_lock); 156 if (_thr_keycreate(&rce_key, free) != 0) { 157 mutex_unlock(&nc_lock); 158 return nc_addr; 159 } 160 mutex_unlock(&nc_lock); 161 } 162 if (nc_addr == NULL) { 163 nc_addr = (int *)malloc(sizeof (int)); 164 if (_thr_setspecific(nc_key, (void *) nc_addr) != 0) { 165 if (nc_addr) 166 free(nc_addr); 167 return &nc_error; 168 } 169 *nc_addr = 0; 170 return nc_addr; 171 } 172 return nc_addr; 173 #else 174 return &nc_error; 175 #endif 176 } 177 178 #define nc_error (*(__nc_error())) 179 /* 180 * A call to setnetconfig() establishes a /etc/netconfig "session". A session 181 * "handle" is returned on a successful call. At the start of a session (after 182 * a call to setnetconfig()) searches through the /etc/netconfig database will 183 * proceed from the start of the file. The session handle must be passed to 184 * getnetconfig() to parse the file. Each call to getnetconfig() using the 185 * current handle will process one subsequent entry in /etc/netconfig. 186 * setnetconfig() must be called before the first call to getnetconfig(). 187 * (Handles are used to allow for nested calls to setnetpath()). 188 * 189 * A new session is established with each call to setnetconfig(), with a new 190 * handle being returned on each call. Previously established sessions remain 191 * active until endnetconfig() is called with that session's handle as an 192 * argument. 193 * 194 * setnetconfig() need *not* be called before a call to getnetconfigent(). 195 * setnetconfig() returns a NULL pointer on failure (for example, if 196 * the netconfig database is not present). 197 */ 198 void * 199 setnetconfig() 200 { 201 struct netconfig_vars *nc_vars; 202 203 if ((nc_vars = (struct netconfig_vars *) 204 malloc(sizeof (struct netconfig_vars))) == NULL) { 205 return(NULL); 206 } 207 208 /* 209 * For multiple calls, i.e. nc_file is not NULL, we just return the 210 * handle without reopening the netconfig db. 211 */ 212 ni.ref++; 213 if ((nc_file != NULL) || (nc_file = fopen(NETCONFIG, "r")) != NULL) { 214 nc_vars->valid = NC_VALID; 215 nc_vars->flag = 0; 216 nc_vars->nc_configs = ni.head; 217 return ((void *)nc_vars); 218 } 219 ni.ref--; 220 nc_error = NC_NONETCONFIG; 221 free(nc_vars); 222 return (NULL); 223 } 224 225 226 /* 227 * When first called, getnetconfig() returns a pointer to the first entry in 228 * the netconfig database, formatted as a struct netconfig. On each subsequent 229 * call, getnetconfig() returns a pointer to the next entry in the database. 230 * getnetconfig() can thus be used to search the entire netconfig file. 231 * getnetconfig() returns NULL at end of file. 232 */ 233 234 struct netconfig * 235 getnetconfig(handlep) 236 void *handlep; 237 { 238 struct netconfig_vars *ncp = (struct netconfig_vars *)handlep; 239 char *stringp; /* tmp string pointer */ 240 struct netconfig_list *list; 241 struct netconfig *np; 242 243 /* 244 * Verify that handle is valid 245 */ 246 if (ncp == NULL || nc_file == NULL) { 247 nc_error = NC_NOTINIT; 248 return (NULL); 249 } 250 251 switch (ncp->valid) { 252 case NC_VALID: 253 /* 254 * If entry has already been read into the list, 255 * we return the entry in the linked list. 256 * If this is the first time call, check if there are any 257 * entries in linked list. If no entries, we need to read the 258 * netconfig db. 259 * If we have been here and the next entry is there, we just 260 * return it. 261 */ 262 if (ncp->flag == 0) { /* first time */ 263 ncp->flag = 1; 264 ncp->nc_configs = ni.head; 265 if (ncp->nc_configs != NULL) /* entry already exist */ 266 return(ncp->nc_configs->ncp); 267 } 268 else if (ncp->nc_configs != NULL && 269 ncp->nc_configs->next != NULL) { 270 ncp->nc_configs = ncp->nc_configs->next; 271 return(ncp->nc_configs->ncp); 272 } 273 274 /* 275 * If we cannot find the entry in the list and is end of file, 276 * we give up. 277 */ 278 if (ni.eof == 1) 279 return(NULL); 280 break; 281 default: 282 nc_error = NC_NOTINIT; 283 return (NULL); 284 } 285 286 stringp = (char *) malloc(MAXNETCONFIGLINE); 287 if (stringp == NULL) 288 return (NULL); 289 290 #ifdef MEM_CHK 291 if (malloc_verify() == 0) { 292 fprintf(stderr, "memory heap corrupted in getnetconfig\n"); 293 exit(1); 294 } 295 #endif 296 297 /* 298 * Read a line from netconfig file. 299 */ 300 do { 301 if (fgets(stringp, MAXNETCONFIGLINE, nc_file) == NULL) { 302 free(stringp); 303 ni.eof = 1; 304 return (NULL); 305 } 306 } while (*stringp == '#'); 307 308 list = (struct netconfig_list *) malloc(sizeof (struct netconfig_list)); 309 if (list == NULL) { 310 free(stringp); 311 return(NULL); 312 } 313 np = (struct netconfig *) malloc(sizeof (struct netconfig)); 314 if (np == NULL) { 315 free(stringp); 316 free(list); 317 return(NULL); 318 } 319 list->ncp = np; 320 list->next = NULL; 321 list->ncp->nc_lookups = NULL; 322 list->linep = stringp; 323 if (parse_ncp(stringp, list->ncp) == -1) { 324 free(stringp); 325 free(np); 326 free(list); 327 return (NULL); 328 } else { 329 /* 330 * If this is the first entry that's been read, it is the 331 * head of the list. If not, put the entry at the end of 332 * the list. Reposition the current pointer of the handle to 333 * the last entry in the list. 334 */ 335 if (ni.head == NULL) /* first entry */ 336 ni.head = ni.tail = list; 337 else { 338 ni.tail->next = list; 339 ni.tail = ni.tail->next; 340 } 341 ncp->nc_configs = ni.tail; 342 return(ni.tail->ncp); 343 } 344 } 345 346 /* 347 * endnetconfig() may be called to "unbind" or "close" the netconfig database 348 * when processing is complete, releasing resources for reuse. endnetconfig() 349 * may not be called before setnetconfig(). endnetconfig() returns 0 on 350 * success and -1 on failure (for example, if setnetconfig() was not called 351 * previously). 352 */ 353 int 354 endnetconfig(handlep) 355 void *handlep; 356 { 357 struct netconfig_vars *nc_handlep = (struct netconfig_vars *)handlep; 358 359 struct netconfig_list *q, *p; 360 361 /* 362 * Verify that handle is valid 363 */ 364 if (nc_handlep == NULL || (nc_handlep->valid != NC_VALID && 365 nc_handlep->valid != NC_STORAGE)) { 366 nc_error = NC_NOTINIT; 367 return (-1); 368 } 369 370 /* 371 * Return 0 if anyone still needs it. 372 */ 373 nc_handlep->valid = NC_INVALID; 374 nc_handlep->flag = 0; 375 nc_handlep->nc_configs = NULL; 376 if (--ni.ref > 0) { 377 free(nc_handlep); 378 return(0); 379 } 380 381 /* 382 * Noone needs these entries anymore, then frees them. 383 * Make sure all info in netconfig_info structure has been 384 * reinitialized. 385 */ 386 q = p = ni.head; 387 ni.eof = ni.ref = 0; 388 ni.head = NULL; 389 ni.tail = NULL; 390 while (q) { 391 p = q->next; 392 if (q->ncp->nc_lookups != NULL) free(q->ncp->nc_lookups); 393 free(q->ncp); 394 free(q->linep); 395 free(q); 396 q = p; 397 } 398 free(nc_handlep); 399 400 fclose(nc_file); 401 nc_file = NULL; 402 return (0); 403 } 404 405 /* 406 * getnetconfigent(netid) returns a pointer to the struct netconfig structure 407 * corresponding to netid. It returns NULL if netid is invalid (that is, does 408 * not name an entry in the netconfig database). It returns NULL and sets 409 * errno in case of failure (for example, if the netconfig database cannot be 410 * opened). 411 */ 412 413 struct netconfig * 414 getnetconfigent(netid) 415 char *netid; 416 { 417 FILE *file; /* NETCONFIG db's file pointer */ 418 char *linep; /* holds current netconfig line */ 419 char *stringp; /* temporary string pointer */ 420 struct netconfig *ncp = NULL; /* returned value */ 421 struct netconfig_list *list; /* pointer to cache list */ 422 423 if (netid == NULL || strlen(netid) == 0) 424 return (NULL); 425 426 /* 427 * Look up table if the entries have already been read and parsed in 428 * getnetconfig(), then copy this entry into a buffer and return it. 429 * If we cannot find the entry in the current list and there are more 430 * entries in the netconfig db that has not been read, we then read the 431 * db and try find the match netid. 432 * If all the netconfig db has been read and placed into the list and 433 * there is no match for the netid, return NULL. 434 */ 435 if (ni.head != NULL) { 436 for (list = ni.head; list; list = list->next) { 437 if (strcmp(list->ncp->nc_netid, netid) == 0) 438 return(dup_ncp(list->ncp)); 439 } 440 if (ni.eof == 1) /* that's all the entries */ 441 return(NULL); 442 } 443 444 if ((file = fopen(NETCONFIG, "r")) == NULL) 445 return (NULL); 446 447 if ((linep = malloc(MAXNETCONFIGLINE)) == NULL) { 448 fclose(file); 449 return (NULL); 450 } 451 do { 452 ptrdiff_t len; 453 char *tmpp; /* tmp string pointer */ 454 455 do { 456 if ((stringp = fgets(linep, MAXNETCONFIGLINE, file)) 457 == NULL) 458 break; 459 } while (*stringp == '#'); 460 if (stringp == NULL) /* eof */ 461 break; 462 if ((tmpp = strpbrk(stringp, "\t ")) == NULL) { 463 /* can't parse file */ 464 nc_error = NC_BADFILE; 465 break; 466 } 467 if (strlen(netid) == (size_t) (len = tmpp - stringp) && /* a match */ 468 strncmp(stringp, netid, (size_t)len) == 0) { 469 if ((ncp = (struct netconfig *) 470 malloc(sizeof (struct netconfig))) == NULL) 471 break; 472 ncp->nc_lookups = NULL; 473 if (parse_ncp(linep, ncp) == -1) { 474 free(ncp); 475 ncp = NULL; 476 } 477 break; 478 } 479 } while (stringp != NULL); 480 if (ncp == NULL) 481 free(linep); 482 fclose(file); 483 return(ncp); 484 } 485 486 /* 487 * freenetconfigent(netconfigp) frees the netconfig structure pointed to by 488 * netconfigp (previously returned by getnetconfigent()). 489 */ 490 491 void 492 freenetconfigent(netconfigp) 493 struct netconfig *netconfigp; 494 { 495 if (netconfigp != NULL) { 496 /* holds all netconfigp's strings */ 497 free(netconfigp->nc_netid); 498 if (netconfigp->nc_lookups != NULL) 499 free(netconfigp->nc_lookups); 500 free(netconfigp); 501 } 502 } 503 504 /* 505 * Parse line and stuff it in a struct netconfig 506 * Typical line might look like: 507 * udp tpi_cots vb inet udp /dev/udp /usr/lib/ip.so,/usr/local/ip.so 508 * 509 * We return -1 if any of the tokens don't parse, or malloc fails. 510 * 511 * Note that we modify stringp (putting NULLs after tokens) and 512 * we set the ncp's string field pointers to point to these tokens within 513 * stringp. 514 */ 515 516 static int 517 parse_ncp(stringp, ncp) 518 char *stringp; /* string to parse */ 519 struct netconfig *ncp; /* where to put results */ 520 { 521 char *tokenp; /* for processing tokens */ 522 char *lasts; 523 524 _DIAGASSERT(stringp != NULL); 525 _DIAGASSERT(ncp != NULL); 526 527 nc_error = NC_BADFILE; 528 /* nearly anything that breaks is for this reason */ 529 stringp[strlen(stringp)-1] = '\0'; /* get rid of newline */ 530 /* netid */ 531 if ((ncp->nc_netid = strtok_r(stringp, "\t ", &lasts)) == NULL) 532 return (-1); 533 534 /* semantics */ 535 if ((tokenp = strtok_r(NULL, "\t ", &lasts)) == NULL) 536 return (-1); 537 if (strcmp(tokenp, NC_TPI_COTS_ORD_S) == 0) 538 ncp->nc_semantics = NC_TPI_COTS_ORD; 539 else if (strcmp(tokenp, NC_TPI_COTS_S) == 0) 540 ncp->nc_semantics = NC_TPI_COTS; 541 else if (strcmp(tokenp, NC_TPI_CLTS_S) == 0) 542 ncp->nc_semantics = NC_TPI_CLTS; 543 else if (strcmp(tokenp, NC_TPI_RAW_S) == 0) 544 ncp->nc_semantics = NC_TPI_RAW; 545 else 546 return (-1); 547 548 /* flags */ 549 if ((tokenp = strtok_r(NULL, "\t ", &lasts)) == NULL) 550 return (-1); 551 for (ncp->nc_flag = NC_NOFLAG; *tokenp != '\0'; tokenp++) { 552 switch (*tokenp) { 553 case NC_NOFLAG_C: 554 break; 555 case NC_VISIBLE_C: 556 ncp->nc_flag |= NC_VISIBLE; 557 break; 558 case NC_BROADCAST_C: 559 ncp->nc_flag |= NC_BROADCAST; 560 break; 561 default: 562 return (-1); 563 } 564 } 565 /* protocol family */ 566 if ((ncp->nc_protofmly = strtok_r(NULL, "\t ", &lasts)) == NULL) 567 return (-1); 568 /* protocol name */ 569 if ((ncp->nc_proto = strtok_r(NULL, "\t ", &lasts)) == NULL) 570 return (-1); 571 /* network device */ 572 if ((ncp->nc_device = strtok_r(NULL, "\t ", &lasts)) == NULL) 573 return (-1); 574 if ((tokenp = strtok_r(NULL, "\t ", &lasts)) == NULL) 575 return (-1); 576 if (strcmp(tokenp, NC_NOLOOKUP) == 0) { 577 ncp->nc_nlookups = 0; 578 ncp->nc_lookups = NULL; 579 } else { 580 char *cp; /* tmp string */ 581 582 if (ncp->nc_lookups != NULL) /* from last visit */ 583 free(ncp->nc_lookups); 584 /* preallocate one string pointer */ 585 ncp->nc_lookups = (char **)malloc(sizeof (char *)); 586 ncp->nc_nlookups = 0; 587 while ((cp = tokenp) != NULL) { 588 tokenp = _get_next_token(cp, ','); 589 ncp->nc_lookups[(size_t)ncp->nc_nlookups++] = cp; 590 ncp->nc_lookups = (char **) 591 realloc(ncp->nc_lookups, 592 (size_t)(ncp->nc_nlookups+1) *sizeof(char *)); 593 /* for next loop */ 594 } 595 } 596 return (0); 597 } 598 599 /* 600 * Returns a string describing the reason for failure. 601 */ 602 char * 603 nc_sperror() 604 { 605 const char *message; 606 607 switch(nc_error) { 608 case NC_NONETCONFIG: 609 message = _nc_errors[0]; 610 break; 611 case NC_NOMEM: 612 message = _nc_errors[1]; 613 break; 614 case NC_NOTINIT: 615 message = _nc_errors[2]; 616 break; 617 case NC_BADFILE: 618 message = _nc_errors[3]; 619 break; 620 default: 621 message = "Unknown network selection error"; 622 } 623 /* LINTED const castaway */ 624 return ((char *)message); 625 } 626 627 /* 628 * Prints a message onto standard error describing the reason for failure. 629 */ 630 void 631 nc_perror(s) 632 const char *s; 633 { 634 635 _DIAGASSERT(s != NULL); 636 637 fprintf(stderr, "%s: %s", s, nc_sperror()); 638 } 639 640 /* 641 * Duplicates the matched netconfig buffer. 642 */ 643 static struct netconfig * 644 dup_ncp(ncp) 645 struct netconfig *ncp; 646 { 647 struct netconfig *p; 648 char *tmp; 649 u_int i; 650 651 _DIAGASSERT(ncp != NULL); 652 653 if ((tmp=malloc(MAXNETCONFIGLINE)) == NULL) 654 return(NULL); 655 if ((p=(struct netconfig *)malloc(sizeof(struct netconfig))) == NULL) { 656 free(tmp); 657 return(NULL); 658 } 659 /* 660 * First we dup all the data from matched netconfig buffer. Then we 661 * adjust some of the member pointer to a pre-allocated buffer where 662 * contains part of the data. 663 * To follow the convention used in parse_ncp(), we store all the 664 * necessary information in the pre-allocated buffer and let each 665 * of the netconfig char pointer member point to the right address 666 * in the buffer. 667 */ 668 *p = *ncp; 669 p->nc_netid = (char *)strcpy(tmp,ncp->nc_netid); 670 tmp = strchr(tmp, NULL) + 1; 671 p->nc_protofmly = (char *)strcpy(tmp,ncp->nc_protofmly); 672 tmp = strchr(tmp, NULL) + 1; 673 p->nc_proto = (char *)strcpy(tmp,ncp->nc_proto); 674 tmp = strchr(tmp, NULL) + 1; 675 p->nc_device = (char *)strcpy(tmp,ncp->nc_device); 676 p->nc_lookups = (char **) 677 malloc((size_t)(p->nc_nlookups+1) * sizeof(char *)); 678 if (p->nc_lookups == NULL) { 679 free(p->nc_netid); 680 return(NULL); 681 } 682 for (i=0; i < p->nc_nlookups; i++) { 683 tmp = strchr(tmp, NULL) + 1; 684 p->nc_lookups[i] = (char *)strcpy(tmp,ncp->nc_lookups[i]); 685 } 686 return(p); 687 } 688