getnetgrent.c (c879ae3536e6d92b8d96c8965c5b05fcb9541520) | getnetgrent.c (f295618d06dd1b215fcd963b33c1265acea36ac9) |
---|---|
1/* 2 * Copyright (c) 1992, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Rick Macklem at The University of Guelph. 7 * 8 * Redistribution and use in source and binary forms, with or without --- 189 unchanged lines hidden (view full) --- 198 return; 199 } 200#else 201 if ((netf = fopen(_PATH_NETGROUP, "r"))) { 202#endif 203 if (parse_netgrp(group)) 204 endnetgrent(); 205 else { | 1/* 2 * Copyright (c) 1992, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Rick Macklem at The University of Guelph. 7 * 8 * Redistribution and use in source and binary forms, with or without --- 189 unchanged lines hidden (view full) --- 198 return; 199 } 200#else 201 if ((netf = fopen(_PATH_NETGROUP, "r"))) { 202#endif 203 if (parse_netgrp(group)) 204 endnetgrent(); 205 else { |
206 grouphead.grname = (char *) 207 malloc(strlen(group) + 1); 208 strcpy(grouphead.grname, group); | 206 grouphead.grname = strdup(group); |
209 } 210 if (netf) 211 fclose(netf); 212 } 213 } 214 nextgrp = grouphead.gr; 215} 216 --- 195 unchanged lines hidden (view full) --- 412 413/* 414 * Parse the netgroup file setting up the linked lists. 415 */ 416static int 417parse_netgrp(const char *group) 418{ 419 char *spos, *epos; | 207 } 208 if (netf) 209 fclose(netf); 210 } 211 } 212 nextgrp = grouphead.gr; 213} 214 --- 195 unchanged lines hidden (view full) --- 410 411/* 412 * Parse the netgroup file setting up the linked lists. 413 */ 414static int 415parse_netgrp(const char *group) 416{ 417 char *spos, *epos; |
420 int len, strpos; | 418 int len, strpos, freepos; |
421#ifdef DEBUG 422 int fields; 423#endif 424 char *pos, *gpos; 425 struct netgrp *grp; 426 struct linelist *lp = linehead; 427 428 /* --- 20 unchanged lines hidden (view full) --- 449 return (1); 450 } else 451 lp->l_parsed = 1; 452 pos = lp->l_line; 453 /* Watch for null pointer dereferences, dammit! */ 454 while (pos != NULL && *pos != '\0') { 455 if (*pos == '(') { 456 grp = (struct netgrp *)malloc(sizeof (struct netgrp)); | 419#ifdef DEBUG 420 int fields; 421#endif 422 char *pos, *gpos; 423 struct netgrp *grp; 424 struct linelist *lp = linehead; 425 426 /* --- 20 unchanged lines hidden (view full) --- 447 return (1); 448 } else 449 lp->l_parsed = 1; 450 pos = lp->l_line; 451 /* Watch for null pointer dereferences, dammit! */ 452 while (pos != NULL && *pos != '\0') { 453 if (*pos == '(') { 454 grp = (struct netgrp *)malloc(sizeof (struct netgrp)); |
455 if (grp == NULL) 456 return(1); |
|
457 bzero((char *)grp, sizeof (struct netgrp)); | 457 bzero((char *)grp, sizeof (struct netgrp)); |
458 grp->ng_next = grouphead.gr; 459 grouphead.gr = grp; | |
460 pos++; 461 gpos = strsep(&pos, ")"); 462#ifdef DEBUG 463 fields = 0; 464#endif 465 for (strpos = 0; strpos < 3; strpos++) { 466 if ((spos = strsep(&gpos, ","))) { 467#ifdef DEBUG --- 4 unchanged lines hidden (view full) --- 472 if ((epos = strpbrk(spos, " \t"))) { 473 *epos = '\0'; 474 len = epos - spos; 475 } else 476 len = strlen(spos); 477 if (len > 0) { 478 grp->ng_str[strpos] = (char *) 479 malloc(len + 1); | 458 pos++; 459 gpos = strsep(&pos, ")"); 460#ifdef DEBUG 461 fields = 0; 462#endif 463 for (strpos = 0; strpos < 3; strpos++) { 464 if ((spos = strsep(&gpos, ","))) { 465#ifdef DEBUG --- 4 unchanged lines hidden (view full) --- 470 if ((epos = strpbrk(spos, " \t"))) { 471 *epos = '\0'; 472 len = epos - spos; 473 } else 474 len = strlen(spos); 475 if (len > 0) { 476 grp->ng_str[strpos] = (char *) 477 malloc(len + 1); |
478 if (grp->ng_str[strpos] == NULL) { 479 for (freepos = 0; freepos < strpos; freepos++) 480 if (grp->ng_str[freepos] != NULL) 481 free(grp->ng_str[freepos]); 482 free(grp); 483 return(1); 484 } |
|
480 bcopy(spos, grp->ng_str[strpos], 481 len + 1); 482 } 483 } else { 484 /* 485 * All other systems I've tested 486 * return NULL for empty netgroup 487 * fields. It's up to user programs 488 * to handle the NULLs appropriately. 489 */ 490 grp->ng_str[strpos] = NULL; 491 } 492 } | 485 bcopy(spos, grp->ng_str[strpos], 486 len + 1); 487 } 488 } else { 489 /* 490 * All other systems I've tested 491 * return NULL for empty netgroup 492 * fields. It's up to user programs 493 * to handle the NULLs appropriately. 494 */ 495 grp->ng_str[strpos] = NULL; 496 } 497 } |
498 grp->ng_next = grouphead.gr; 499 grouphead.gr = grp; |
|
493#ifdef DEBUG 494 /* 495 * Note: on other platforms, malformed netgroup 496 * entries are not normally flagged. While we 497 * can catch bad entries and report them, we should 498 * stay silent by default for compatibility's sake. 499 */ 500 if (fields < 3) --- 20 unchanged lines hidden (view full) --- 521 522/* 523 * Read the netgroup file and save lines until the line for the netgroup 524 * is found. Return 1 if eof is encountered. 525 */ 526static struct linelist * 527read_for_group(const char *group) 528{ | 500#ifdef DEBUG 501 /* 502 * Note: on other platforms, malformed netgroup 503 * entries are not normally flagged. While we 504 * can catch bad entries and report them, we should 505 * stay silent by default for compatibility's sake. 506 */ 507 if (fields < 3) --- 20 unchanged lines hidden (view full) --- 528 529/* 530 * Read the netgroup file and save lines until the line for the netgroup 531 * is found. Return 1 if eof is encountered. 532 */ 533static struct linelist * 534read_for_group(const char *group) 535{ |
529 char *pos, *spos, *linep, *olinep; | 536 char *pos, *spos, *linep; |
530 int len, olen; 531 int cont; 532 struct linelist *lp; 533 char line[LINSIZ + 2]; 534#ifdef YP 535 char *result; 536 int resultlen; | 537 int len, olen; 538 int cont; 539 struct linelist *lp; 540 char line[LINSIZ + 2]; 541#ifdef YP 542 char *result; 543 int resultlen; |
544 linep = NULL; |
|
537 538 while (_netgr_yp_enabled || fgets(line, LINSIZ, netf) != NULL) { 539 if (_netgr_yp_enabled) { 540 if(!_netgr_yp_domain) 541 if(yp_get_default_domain(&_netgr_yp_domain)) 542 continue; 543 if (yp_match(_netgr_yp_domain, "netgroup", group, 544 strlen(group), &result, &resultlen)) { --- 4 unchanged lines hidden (view full) --- 549 _netgr_yp_enabled = 0; 550 continue; 551 } 552 } 553 snprintf(line, LINSIZ, "%s %s", group, result); 554 free(result); 555 } 556#else | 545 546 while (_netgr_yp_enabled || fgets(line, LINSIZ, netf) != NULL) { 547 if (_netgr_yp_enabled) { 548 if(!_netgr_yp_domain) 549 if(yp_get_default_domain(&_netgr_yp_domain)) 550 continue; 551 if (yp_match(_netgr_yp_domain, "netgroup", group, 552 strlen(group), &result, &resultlen)) { --- 4 unchanged lines hidden (view full) --- 557 _netgr_yp_enabled = 0; 558 continue; 559 } 560 } 561 snprintf(line, LINSIZ, "%s %s", group, result); 562 free(result); 563 } 564#else |
565 linep = NULL; |
|
557 while (fgets(line, LINSIZ, netf) != NULL) { 558#endif 559 pos = (char *)&line; 560#ifdef YP 561 if (*pos == '+') { 562 _netgr_yp_enabled = 1; 563 continue; 564 } --- 6 unchanged lines hidden (view full) --- 571 while (*pos != ' ' && *pos != '\t' && *pos != '\n' && 572 *pos != '\0') 573 pos++; 574 len = pos - spos; 575 while (*pos == ' ' || *pos == '\t') 576 pos++; 577 if (*pos != '\n' && *pos != '\0') { 578 lp = (struct linelist *)malloc(sizeof (*lp)); | 566 while (fgets(line, LINSIZ, netf) != NULL) { 567#endif 568 pos = (char *)&line; 569#ifdef YP 570 if (*pos == '+') { 571 _netgr_yp_enabled = 1; 572 continue; 573 } --- 6 unchanged lines hidden (view full) --- 580 while (*pos != ' ' && *pos != '\t' && *pos != '\n' && 581 *pos != '\0') 582 pos++; 583 len = pos - spos; 584 while (*pos == ' ' || *pos == '\t') 585 pos++; 586 if (*pos != '\n' && *pos != '\0') { 587 lp = (struct linelist *)malloc(sizeof (*lp)); |
588 if (lp == NULL) 589 return(NULL); |
|
579 lp->l_parsed = 0; 580 lp->l_groupname = (char *)malloc(len + 1); | 590 lp->l_parsed = 0; 591 lp->l_groupname = (char *)malloc(len + 1); |
592 if (lp->l_groupname == NULL) { 593 free(lp); 594 return(NULL); 595 } |
|
581 bcopy(spos, lp->l_groupname, len); 582 *(lp->l_groupname + len) = '\0'; 583 len = strlen(pos); 584 olen = 0; 585 586 /* 587 * Loop around handling line continuations. 588 */ 589 do { 590 if (*(pos + len - 1) == '\n') 591 len--; 592 if (*(pos + len - 1) == '\\') { 593 len--; 594 cont = 1; 595 } else 596 cont = 0; 597 if (len > 0) { | 596 bcopy(spos, lp->l_groupname, len); 597 *(lp->l_groupname + len) = '\0'; 598 len = strlen(pos); 599 olen = 0; 600 601 /* 602 * Loop around handling line continuations. 603 */ 604 do { 605 if (*(pos + len - 1) == '\n') 606 len--; 607 if (*(pos + len - 1) == '\\') { 608 len--; 609 cont = 1; 610 } else 611 cont = 0; 612 if (len > 0) { |
598 linep = (char *)malloc(olen + len + 1); 599 if (olen > 0) { 600 bcopy(olinep, linep, olen); 601 free(olinep); | 613 linep = (char *)reallocf(linep, olen + len + 1); 614 if (linep == NULL) { 615 free(lp->l_groupname); 616 free(lp); 617 return(NULL); |
602 } 603 bcopy(pos, linep + olen, len); 604 olen += len; 605 *(linep + olen) = '\0'; | 618 } 619 bcopy(pos, linep + olen, len); 620 olen += len; 621 *(linep + olen) = '\0'; |
606 olinep = linep; | |
607 } 608 if (cont) { 609 if (fgets(line, LINSIZ, netf)) { 610 pos = line; 611 len = strlen(pos); 612 } else 613 cont = 0; 614 } --- 14 unchanged lines hidden (view full) --- 629 * Yucky. The recursive nature of this whole mess might require 630 * us to make more than one pass through the netgroup file. 631 * This might be best left outside the #ifdef YP, but YP is 632 * defined by default anyway, so I'll leave it like this 633 * until I know better. 634 */ 635 rewind(netf); 636#endif | 622 } 623 if (cont) { 624 if (fgets(line, LINSIZ, netf)) { 625 pos = line; 626 len = strlen(pos); 627 } else 628 cont = 0; 629 } --- 14 unchanged lines hidden (view full) --- 644 * Yucky. The recursive nature of this whole mess might require 645 * us to make more than one pass through the netgroup file. 646 * This might be best left outside the #ifdef YP, but YP is 647 * defined by default anyway, so I'll leave it like this 648 * until I know better. 649 */ 650 rewind(netf); 651#endif |
637 return ((struct linelist *)0); | 652 return (NULL); |
638} | 653} |