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