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