1 /* $NetBSD: getcap.c,v 1.33 2000/06/02 13:13:07 itojun Exp $ */ 2 3 /*- 4 * Copyright (c) 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Casey Leedom of Lawrence Livermore National Laboratory. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 */ 38 39 #include <sys/cdefs.h> 40 #if defined(LIBC_SCCS) && !defined(lint) 41 #if 0 42 static char sccsid[] = "@(#)getcap.c 8.3 (Berkeley) 3/25/94"; 43 #else 44 __RCSID("$NetBSD: getcap.c,v 1.33 2000/06/02 13:13:07 itojun Exp $"); 45 #endif 46 #endif /* LIBC_SCCS and not lint */ 47 48 #include "namespace.h" 49 #include <sys/types.h> 50 51 #include <assert.h> 52 #include <ctype.h> 53 #include <db.h> 54 #include <errno.h> 55 #include <fcntl.h> 56 #include <limits.h> 57 #include <stdio.h> 58 #include <stdlib.h> 59 #include <string.h> 60 #include <unistd.h> 61 62 #ifdef __weak_alias 63 __weak_alias(cgetcap,_cgetcap) 64 __weak_alias(cgetclose,_cgetclose) 65 __weak_alias(cgetent,_cgetent) 66 __weak_alias(cgetfirst,_cgetfirst) 67 __weak_alias(cgetmatch,_cgetmatch) 68 __weak_alias(cgetnext,_cgetnext) 69 __weak_alias(cgetnum,_cgetnum) 70 __weak_alias(cgetset,_cgetset) 71 __weak_alias(cgetstr,_cgetstr) 72 __weak_alias(cgetustr,_cgetustr) 73 #endif 74 75 #define BFRAG 1024 76 #define BSIZE 1024 77 #define ESC ('[' & 037) /* ASCII ESC */ 78 #define MAX_RECURSION 32 /* maximum getent recursion */ 79 #define SFRAG 100 /* cgetstr mallocs in SFRAG chunks */ 80 81 #define RECOK (char)0 82 #define TCERR (char)1 83 #define SHADOW (char)2 84 85 static size_t topreclen; /* toprec length */ 86 static char *toprec; /* Additional record specified by cgetset() */ 87 static int gottoprec; /* Flag indicating retrieval of toprecord */ 88 89 static int cdbget __P((DB *, char **, const char *)); 90 static int getent __P((char **, size_t *, char **, int, const char *, int, char *)); 91 static int nfcmp __P((char *, char *)); 92 93 /* 94 * Cgetset() allows the addition of a user specified buffer to be added 95 * to the database array, in effect "pushing" the buffer on top of the 96 * virtual database. 0 is returned on success, -1 on failure. 97 */ 98 int 99 cgetset(ent) 100 const char *ent; 101 { 102 const char *source, *check; 103 char *dest; 104 105 if (ent == NULL) { 106 if (toprec) 107 free(toprec); 108 toprec = NULL; 109 topreclen = 0; 110 return (0); 111 } 112 topreclen = strlen(ent); 113 if ((toprec = malloc (topreclen + 1)) == NULL) { 114 errno = ENOMEM; 115 return (-1); 116 } 117 gottoprec = 0; 118 119 source=ent; 120 dest=toprec; 121 while (*source) { /* Strip whitespace */ 122 *dest++ = *source++; /* Do not check first field */ 123 while (*source == ':') { 124 check=source+1; 125 while (*check && (isspace((unsigned char)*check) || 126 (*check=='\\' && isspace((unsigned char)check[1])))) 127 ++check; 128 if( *check == ':' ) 129 source=check; 130 else 131 break; 132 133 } 134 } 135 *dest=0; 136 137 return (0); 138 } 139 140 /* 141 * Cgetcap searches the capability record buf for the capability cap with 142 * type `type'. A pointer to the value of cap is returned on success, NULL 143 * if the requested capability couldn't be found. 144 * 145 * Specifying a type of ':' means that nothing should follow cap (:cap:). 146 * In this case a pointer to the terminating ':' or NUL will be returned if 147 * cap is found. 148 * 149 * If (cap, '@') or (cap, terminator, '@') is found before (cap, terminator) 150 * return NULL. 151 */ 152 char * 153 cgetcap(buf, cap, type) 154 char *buf; 155 const char *cap; 156 int type; 157 { 158 char *bp; 159 const char *cp; 160 161 _DIAGASSERT(buf != NULL); 162 _DIAGASSERT(cap != NULL); 163 164 bp = buf; 165 for (;;) { 166 /* 167 * Skip past the current capability field - it's either the 168 * name field if this is the first time through the loop, or 169 * the remainder of a field whose name failed to match cap. 170 */ 171 for (;;) 172 if (*bp == '\0') 173 return (NULL); 174 else 175 if (*bp++ == ':') 176 break; 177 178 /* 179 * Try to match (cap, type) in buf. 180 */ 181 for (cp = cap; *cp == *bp && *bp != '\0'; cp++, bp++) 182 continue; 183 if (*cp != '\0') 184 continue; 185 if (*bp == '@') 186 return (NULL); 187 if (type == ':') { 188 if (*bp != '\0' && *bp != ':') 189 continue; 190 return(bp); 191 } 192 if (*bp != type) 193 continue; 194 bp++; 195 return (*bp == '@' ? NULL : bp); 196 } 197 /* NOTREACHED */ 198 } 199 200 /* 201 * Cgetent extracts the capability record name from the NULL terminated file 202 * array db_array and returns a pointer to a malloc'd copy of it in buf. 203 * Buf must be retained through all subsequent calls to cgetcap, cgetnum, 204 * cgetflag, and cgetstr, but may then be free'd. 0 is returned on success, 205 * -1 if the requested record couldn't be found, -2 if a system error was 206 * encountered (couldn't open/read a file, etc.), and -3 if a potential 207 * reference loop is detected. 208 */ 209 int 210 cgetent(buf, db_array, name) 211 char **buf, **db_array; 212 const char *name; 213 { 214 size_t dummy; 215 216 _DIAGASSERT(buf != NULL); 217 _DIAGASSERT(db_array != NULL); 218 _DIAGASSERT(name != NULL); 219 220 return (getent(buf, &dummy, db_array, -1, name, 0, NULL)); 221 } 222 223 /* 224 * Getent implements the functions of cgetent. If fd is non-negative, 225 * *db_array has already been opened and fd is the open file descriptor. We 226 * do this to save time and avoid using up file descriptors for tc= 227 * recursions. 228 * 229 * Getent returns the same success/failure codes as cgetent. On success, a 230 * pointer to a malloc'ed capability record with all tc= capabilities fully 231 * expanded and its length (not including trailing ASCII NUL) are left in 232 * *cap and *len. 233 * 234 * Basic algorithm: 235 * + Allocate memory incrementally as needed in chunks of size BFRAG 236 * for capability buffer. 237 * + Recurse for each tc=name and interpolate result. Stop when all 238 * names interpolated, a name can't be found, or depth exceeds 239 * MAX_RECURSION. 240 */ 241 static int 242 getent(cap, len, db_array, fd, name, depth, nfield) 243 char **cap, **db_array, *nfield; 244 const char *name; 245 size_t *len; 246 int fd, depth; 247 { 248 DB *capdbp; 249 char *r_end, *rp = NULL, **db_p; /* pacify gcc */ 250 int myfd = 0, eof, foundit, retval; 251 size_t clen; 252 char *record, *cbuf, *newrecord; 253 int tc_not_resolved; 254 char pbuf[_POSIX_PATH_MAX]; 255 256 _DIAGASSERT(cap != NULL); 257 _DIAGASSERT(len != NULL); 258 _DIAGASSERT(db_array != NULL); 259 /* fd may be -1 */ 260 _DIAGASSERT(name != NULL); 261 /* nfield may be NULL */ 262 263 /* 264 * Return with ``loop detected'' error if we've recursed more than 265 * MAX_RECURSION times. 266 */ 267 if (depth > MAX_RECURSION) 268 return (-3); 269 270 /* 271 * Check if we have a top record from cgetset(). 272 */ 273 if (depth == 0 && toprec != NULL && cgetmatch(toprec, name) == 0) { 274 if ((record = malloc (topreclen + BFRAG)) == NULL) { 275 errno = ENOMEM; 276 return (-2); 277 } 278 (void)strcpy(record, toprec); /* XXX: strcpy is safe */ 279 db_p = db_array; 280 rp = record + topreclen + 1; 281 r_end = rp + BFRAG; 282 goto tc_exp; 283 } 284 /* 285 * Allocate first chunk of memory. 286 */ 287 if ((record = malloc(BFRAG)) == NULL) { 288 errno = ENOMEM; 289 return (-2); 290 } 291 r_end = record + BFRAG; 292 foundit = 0; 293 /* 294 * Loop through database array until finding the record. 295 */ 296 297 for (db_p = db_array; *db_p != NULL; db_p++) { 298 eof = 0; 299 300 /* 301 * Open database if not already open. 302 */ 303 304 if (fd >= 0) { 305 (void)lseek(fd, (off_t)0, SEEK_SET); 306 } else { 307 (void)snprintf(pbuf, sizeof(pbuf), "%s.db", *db_p); 308 if ((capdbp = dbopen(pbuf, O_RDONLY, 0, DB_HASH, 0)) 309 != NULL) { 310 free(record); 311 retval = cdbget(capdbp, &record, name); 312 if (retval < 0) { 313 /* no record available */ 314 (void)capdbp->close(capdbp); 315 return (retval); 316 } 317 /* save the data; close frees it */ 318 clen = strlen(record); 319 cbuf = malloc(clen + 1); 320 memmove(cbuf, record, clen + 1); 321 if (capdbp->close(capdbp) < 0) { 322 int serrno = errno; 323 324 free(cbuf); 325 errno = serrno; 326 return (-2); 327 } 328 *len = clen; 329 *cap = cbuf; 330 return (retval); 331 } else { 332 fd = open(*db_p, O_RDONLY, 0); 333 if (fd < 0) { 334 /* No error on unfound file. */ 335 continue; 336 } 337 myfd = 1; 338 } 339 } 340 /* 341 * Find the requested capability record ... 342 */ 343 { 344 char buf[BUFSIZ]; 345 char *b_end, *bp, *cp; 346 int c, slash; 347 348 /* 349 * Loop invariants: 350 * There is always room for one more character in record. 351 * R_end always points just past end of record. 352 * Rp always points just past last character in record. 353 * B_end always points just past last character in buf. 354 * Bp always points at next character in buf. 355 * Cp remembers where the last colon was. 356 */ 357 b_end = buf; 358 bp = buf; 359 cp = 0; 360 slash = 0; 361 for (;;) { 362 363 /* 364 * Read in a line implementing (\, newline) 365 * line continuation. 366 */ 367 rp = record; 368 for (;;) { 369 if (bp >= b_end) { 370 int n; 371 372 n = read(fd, buf, sizeof(buf)); 373 if (n <= 0) { 374 if (myfd) 375 (void)close(fd); 376 if (n < 0) { 377 int serrno = errno; 378 379 free(record); 380 errno = serrno; 381 return (-2); 382 } else { 383 fd = -1; 384 eof = 1; 385 break; 386 } 387 } 388 b_end = buf+n; 389 bp = buf; 390 } 391 392 c = *bp++; 393 if (c == '\n') { 394 if (slash) { 395 slash = 0; 396 rp--; 397 continue; 398 } else 399 break; 400 } 401 if (slash) { 402 slash = 0; 403 cp = 0; 404 } 405 if (c == ':') { 406 /* 407 * If the field was `empty' (i.e. 408 * contained only white space), back up 409 * to the colon (eliminating the 410 * field). 411 */ 412 if (cp) 413 rp = cp; 414 else 415 cp = rp; 416 } else if (c == '\\') { 417 slash = 1; 418 } else if (c != ' ' && c != '\t') { 419 /* 420 * Forget where the colon was, as this 421 * is not an empty field. 422 */ 423 cp = 0; 424 } 425 *rp++ = c; 426 427 /* 428 * Enforce loop invariant: if no room 429 * left in record buffer, try to get 430 * some more. 431 */ 432 if (rp >= r_end) { 433 u_int pos; 434 size_t newsize; 435 436 pos = rp - record; 437 newsize = r_end - record + BFRAG; 438 newrecord = realloc(record, newsize); 439 if (newrecord == NULL) { 440 free(record); 441 if (myfd) 442 (void)close(fd); 443 errno = ENOMEM; 444 return (-2); 445 } 446 record = newrecord; 447 r_end = record + newsize; 448 rp = record + pos; 449 } 450 } 451 /* Eliminate any white space after the last colon. */ 452 if (cp) 453 rp = cp + 1; 454 /* Loop invariant lets us do this. */ 455 *rp++ = '\0'; 456 457 /* 458 * If encountered eof check next file. 459 */ 460 if (eof) 461 break; 462 463 /* 464 * Toss blank lines and comments. 465 */ 466 if (*record == '\0' || *record == '#') 467 continue; 468 469 /* 470 * See if this is the record we want ... 471 */ 472 if (cgetmatch(record, name) == 0) { 473 if (nfield == NULL || !nfcmp(nfield, record)) { 474 foundit = 1; 475 break; /* found it! */ 476 } 477 } 478 } 479 } 480 if (foundit) 481 break; 482 } 483 484 if (!foundit) 485 return (-1); 486 487 /* 488 * Got the capability record, but now we have to expand all tc=name 489 * references in it ... 490 */ 491 tc_exp: { 492 char *newicap, *s; 493 size_t ilen, newilen; 494 int diff, iret, tclen; 495 char *icap, *scan, *tc, *tcstart, *tcend; 496 497 /* 498 * Loop invariants: 499 * There is room for one more character in record. 500 * R_end points just past end of record. 501 * Rp points just past last character in record. 502 * Scan points at remainder of record that needs to be 503 * scanned for tc=name constructs. 504 */ 505 scan = record; 506 tc_not_resolved = 0; 507 for (;;) { 508 if ((tc = cgetcap(scan, "tc", '=')) == NULL) 509 break; 510 511 /* 512 * Find end of tc=name and stomp on the trailing `:' 513 * (if present) so we can use it to call ourselves. 514 */ 515 s = tc; 516 for (;;) 517 if (*s == '\0') 518 break; 519 else 520 if (*s++ == ':') { 521 *(s - 1) = '\0'; 522 break; 523 } 524 tcstart = tc - 3; 525 tclen = s - tcstart; 526 tcend = s; 527 528 iret = getent(&icap, &ilen, db_p, fd, tc, depth+1, 529 NULL); 530 newicap = icap; /* Put into a register. */ 531 newilen = ilen; 532 if (iret != 0) { 533 /* an error */ 534 if (iret < -1) { 535 if (myfd) 536 (void)close(fd); 537 free(record); 538 return (iret); 539 } 540 if (iret == 1) 541 tc_not_resolved = 1; 542 /* couldn't resolve tc */ 543 if (iret == -1) { 544 *(s - 1) = ':'; 545 scan = s - 1; 546 tc_not_resolved = 1; 547 continue; 548 549 } 550 } 551 /* not interested in name field of tc'ed record */ 552 s = newicap; 553 for (;;) 554 if (*s == '\0') 555 break; 556 else 557 if (*s++ == ':') 558 break; 559 newilen -= s - newicap; 560 newicap = s; 561 562 /* make sure interpolated record is `:'-terminated */ 563 s += newilen; 564 if (*(s-1) != ':') { 565 *s = ':'; /* overwrite NUL with : */ 566 newilen++; 567 } 568 569 /* 570 * Make sure there's enough room to insert the 571 * new record. 572 */ 573 diff = newilen - tclen; 574 if (diff >= r_end - rp) { 575 u_int pos, tcpos, tcposend; 576 size_t newsize; 577 578 pos = rp - record; 579 newsize = r_end - record + diff + BFRAG; 580 tcpos = tcstart - record; 581 tcposend = tcend - record; 582 newrecord = realloc(record, newsize); 583 if (newrecord == NULL) { 584 free(record); 585 if (myfd) 586 (void)close(fd); 587 free(icap); 588 errno = ENOMEM; 589 return (-2); 590 } 591 record = newrecord; 592 r_end = record + newsize; 593 rp = record + pos; 594 tcstart = record + tcpos; 595 tcend = record + tcposend; 596 } 597 598 /* 599 * Insert tc'ed record into our record. 600 */ 601 s = tcstart + newilen; 602 memmove(s, tcend, (size_t)(rp - tcend)); 603 memmove(tcstart, newicap, newilen); 604 rp += diff; 605 free(icap); 606 607 /* 608 * Start scan on `:' so next cgetcap works properly 609 * (cgetcap always skips first field). 610 */ 611 scan = s-1; 612 } 613 614 } 615 /* 616 * Close file (if we opened it), give back any extra memory, and 617 * return capability, length and success. 618 */ 619 if (myfd) 620 (void)close(fd); 621 *len = rp - record - 1; /* don't count NUL */ 622 if (r_end > rp) { 623 if ((newrecord = 624 realloc(record, (size_t)(rp - record))) == NULL) { 625 free(record); 626 errno = ENOMEM; 627 return (-2); 628 } 629 record = newrecord; 630 } 631 632 *cap = record; 633 if (tc_not_resolved) 634 return (1); 635 return (0); 636 } 637 638 static int 639 cdbget(capdbp, bp, name) 640 DB *capdbp; 641 char **bp; 642 const char *name; 643 { 644 DBT key; 645 DBT data; 646 647 _DIAGASSERT(capdbp != NULL); 648 _DIAGASSERT(bp != NULL); 649 _DIAGASSERT(name != NULL); 650 651 /* LINTED key is not modified */ 652 key.data = (char *)name; 653 key.size = strlen(name); 654 655 for (;;) { 656 /* Get the reference. */ 657 switch(capdbp->get(capdbp, &key, &data, 0)) { 658 case -1: 659 return (-2); 660 case 1: 661 return (-1); 662 } 663 664 /* If not an index to another record, leave. */ 665 if (((char *)data.data)[0] != SHADOW) 666 break; 667 668 key.data = (char *)data.data + 1; 669 key.size = data.size - 1; 670 } 671 672 *bp = (char *)data.data + 1; 673 return (((char *)(data.data))[0] == TCERR ? 1 : 0); 674 } 675 676 /* 677 * Cgetmatch will return 0 if name is one of the names of the capability 678 * record buf, -1 if not. 679 */ 680 int 681 cgetmatch(buf, name) 682 const char *buf, *name; 683 { 684 const char *np, *bp; 685 686 _DIAGASSERT(buf != NULL); 687 _DIAGASSERT(name != NULL); 688 689 /* 690 * Start search at beginning of record. 691 */ 692 bp = buf; 693 for (;;) { 694 /* 695 * Try to match a record name. 696 */ 697 np = name; 698 for (;;) 699 if (*np == '\0') { 700 if (*bp == '|' || *bp == ':' || *bp == '\0') 701 return (0); 702 else 703 break; 704 } else 705 if (*bp++ != *np++) 706 break; 707 708 /* 709 * Match failed, skip to next name in record. 710 */ 711 bp--; /* a '|' or ':' may have stopped the match */ 712 for (;;) 713 if (*bp == '\0' || *bp == ':') 714 return (-1); /* match failed totally */ 715 else 716 if (*bp++ == '|') 717 break; /* found next name */ 718 } 719 } 720 721 int 722 cgetfirst(buf, db_array) 723 char **buf, **db_array; 724 { 725 726 _DIAGASSERT(buf != NULL); 727 _DIAGASSERT(db_array != NULL); 728 729 (void)cgetclose(); 730 return (cgetnext(buf, db_array)); 731 } 732 733 static FILE *pfp; 734 static int slash; 735 static char **dbp; 736 737 int 738 cgetclose() 739 { 740 if (pfp != NULL) { 741 (void)fclose(pfp); 742 pfp = NULL; 743 } 744 dbp = NULL; 745 gottoprec = 0; 746 slash = 0; 747 return(0); 748 } 749 750 /* 751 * Cgetnext() gets either the first or next entry in the logical database 752 * specified by db_array. It returns 0 upon completion of the database, 1 753 * upon returning an entry with more remaining, and -1 if an error occurs. 754 */ 755 int 756 cgetnext(bp, db_array) 757 char **bp; 758 char **db_array; 759 { 760 size_t len; 761 int status, done; 762 char *cp, *line, *rp, *np, buf[BSIZE], nbuf[BSIZE]; 763 size_t dummy; 764 765 _DIAGASSERT(bp != NULL); 766 _DIAGASSERT(db_array != NULL); 767 768 if (dbp == NULL) 769 dbp = db_array; 770 771 if (pfp == NULL && (pfp = fopen(*dbp, "r")) == NULL) { 772 (void)cgetclose(); 773 return (-1); 774 } 775 for(;;) { 776 if (toprec && !gottoprec) { 777 gottoprec = 1; 778 line = toprec; 779 } else { 780 line = fgetln(pfp, &len); 781 if (line == NULL && pfp) { 782 if (ferror(pfp)) { 783 (void)cgetclose(); 784 return (-1); 785 } else { 786 (void)fclose(pfp); 787 pfp = NULL; 788 if (*++dbp == NULL) { 789 (void)cgetclose(); 790 return (0); 791 } else if ((pfp = 792 fopen(*dbp, "r")) == NULL) { 793 (void)cgetclose(); 794 return (-1); 795 } else 796 continue; 797 } 798 } else 799 line[len - 1] = '\0'; 800 if (len == 1) { 801 slash = 0; 802 continue; 803 } 804 if (isspace((unsigned char)*line) || 805 *line == ':' || *line == '#' || slash) { 806 if (line[len - 2] == '\\') 807 slash = 1; 808 else 809 slash = 0; 810 continue; 811 } 812 if (line[len - 2] == '\\') 813 slash = 1; 814 else 815 slash = 0; 816 } 817 818 819 /* 820 * Line points to a name line. 821 */ 822 done = 0; 823 np = nbuf; 824 for (;;) { 825 for (cp = line; *cp != '\0'; cp++) { 826 if (*cp == ':') { 827 *np++ = ':'; 828 done = 1; 829 break; 830 } 831 if (*cp == '\\') 832 break; 833 *np++ = *cp; 834 } 835 if (done) { 836 *np = '\0'; 837 break; 838 } else { /* name field extends beyond the line */ 839 line = fgetln(pfp, &len); 840 if (line == NULL && pfp) { 841 if (ferror(pfp)) { 842 (void)cgetclose(); 843 return (-1); 844 } 845 (void)fclose(pfp); 846 pfp = NULL; 847 *np = '\0'; 848 break; 849 } else 850 line[len - 1] = '\0'; 851 } 852 } 853 rp = buf; 854 for(cp = nbuf; *cp != '\0'; cp++) 855 if (*cp == '|' || *cp == ':') 856 break; 857 else 858 *rp++ = *cp; 859 860 *rp = '\0'; 861 /* 862 * XXX 863 * Last argument of getent here should be nbuf if we want true 864 * sequential access in the case of duplicates. 865 * With NULL, getent will return the first entry found 866 * rather than the duplicate entry record. This is a 867 * matter of semantics that should be resolved. 868 */ 869 status = getent(bp, &dummy, db_array, -1, buf, 0, NULL); 870 if (status == -2 || status == -3) 871 (void)cgetclose(); 872 873 return (status + 1); 874 } 875 /* NOTREACHED */ 876 } 877 878 /* 879 * Cgetstr retrieves the value of the string capability cap from the 880 * capability record pointed to by buf. A pointer to a decoded, NUL 881 * terminated, malloc'd copy of the string is returned in the char * 882 * pointed to by str. The length of the string not including the trailing 883 * NUL is returned on success, -1 if the requested string capability 884 * couldn't be found, -2 if a system error was encountered (storage 885 * allocation failure). 886 */ 887 int 888 cgetstr(buf, cap, str) 889 char *buf; 890 const char *cap; 891 char **str; 892 { 893 u_int m_room; 894 const char *bp; 895 char *mp; 896 int len; 897 char *mem, *newmem; 898 899 _DIAGASSERT(buf != NULL); 900 _DIAGASSERT(cap != NULL); 901 _DIAGASSERT(str != NULL); 902 903 /* 904 * Find string capability cap 905 */ 906 bp = cgetcap(buf, cap, '='); 907 if (bp == NULL) 908 return (-1); 909 910 /* 911 * Conversion / storage allocation loop ... Allocate memory in 912 * chunks SFRAG in size. 913 */ 914 if ((mem = malloc(SFRAG)) == NULL) { 915 errno = ENOMEM; 916 return (-2); /* couldn't even allocate the first fragment */ 917 } 918 m_room = SFRAG; 919 mp = mem; 920 921 while (*bp != ':' && *bp != '\0') { 922 /* 923 * Loop invariants: 924 * There is always room for one more character in mem. 925 * Mp always points just past last character in mem. 926 * Bp always points at next character in buf. 927 */ 928 if (*bp == '^') { 929 bp++; 930 if (*bp == ':' || *bp == '\0') 931 break; /* drop unfinished escape */ 932 *mp++ = *bp++ & 037; 933 } else if (*bp == '\\') { 934 bp++; 935 if (*bp == ':' || *bp == '\0') 936 break; /* drop unfinished escape */ 937 if ('0' <= *bp && *bp <= '7') { 938 int n, i; 939 940 n = 0; 941 i = 3; /* maximum of three octal digits */ 942 do { 943 n = n * 8 + (*bp++ - '0'); 944 } while (--i && '0' <= *bp && *bp <= '7'); 945 *mp++ = n; 946 } 947 else switch (*bp++) { 948 case 'b': case 'B': 949 *mp++ = '\b'; 950 break; 951 case 't': case 'T': 952 *mp++ = '\t'; 953 break; 954 case 'n': case 'N': 955 *mp++ = '\n'; 956 break; 957 case 'f': case 'F': 958 *mp++ = '\f'; 959 break; 960 case 'r': case 'R': 961 *mp++ = '\r'; 962 break; 963 case 'e': case 'E': 964 *mp++ = ESC; 965 break; 966 case 'c': case 'C': 967 *mp++ = ':'; 968 break; 969 default: 970 /* 971 * Catches '\', '^', and 972 * everything else. 973 */ 974 *mp++ = *(bp-1); 975 break; 976 } 977 } else 978 *mp++ = *bp++; 979 m_room--; 980 981 /* 982 * Enforce loop invariant: if no room left in current 983 * buffer, try to get some more. 984 */ 985 if (m_room == 0) { 986 size_t size = mp - mem; 987 988 if ((newmem = realloc(mem, size + SFRAG)) == NULL) { 989 free(mem); 990 return (-2); 991 } 992 mem = newmem; 993 m_room = SFRAG; 994 mp = mem + size; 995 } 996 } 997 *mp++ = '\0'; /* loop invariant let's us do this */ 998 m_room--; 999 len = mp - mem - 1; 1000 1001 /* 1002 * Give back any extra memory and return value and success. 1003 */ 1004 if (m_room != 0) { 1005 if ((newmem = realloc(mem, (size_t)(mp - mem))) == NULL) { 1006 free(mem); 1007 return (-2); 1008 } 1009 mem = newmem; 1010 } 1011 *str = mem; 1012 return (len); 1013 } 1014 1015 /* 1016 * Cgetustr retrieves the value of the string capability cap from the 1017 * capability record pointed to by buf. The difference between cgetustr() 1018 * and cgetstr() is that cgetustr does not decode escapes but rather treats 1019 * all characters literally. A pointer to a NUL terminated malloc'd 1020 * copy of the string is returned in the char pointed to by str. The 1021 * length of the string not including the trailing NUL is returned on success, 1022 * -1 if the requested string capability couldn't be found, -2 if a system 1023 * error was encountered (storage allocation failure). 1024 */ 1025 int 1026 cgetustr(buf, cap, str) 1027 char *buf; 1028 const char *cap; 1029 char **str; 1030 { 1031 u_int m_room; 1032 const char *bp; 1033 char *mp; 1034 int len; 1035 char *mem, *newmem; 1036 1037 _DIAGASSERT(buf != NULL); 1038 _DIAGASSERT(cap != NULL); 1039 _DIAGASSERT(str != NULL); 1040 1041 /* 1042 * Find string capability cap 1043 */ 1044 if ((bp = cgetcap(buf, cap, '=')) == NULL) 1045 return (-1); 1046 1047 /* 1048 * Conversion / storage allocation loop ... Allocate memory in 1049 * chunks SFRAG in size. 1050 */ 1051 if ((mem = malloc(SFRAG)) == NULL) { 1052 errno = ENOMEM; 1053 return (-2); /* couldn't even allocate the first fragment */ 1054 } 1055 m_room = SFRAG; 1056 mp = mem; 1057 1058 while (*bp != ':' && *bp != '\0') { 1059 /* 1060 * Loop invariants: 1061 * There is always room for one more character in mem. 1062 * Mp always points just past last character in mem. 1063 * Bp always points at next character in buf. 1064 */ 1065 *mp++ = *bp++; 1066 m_room--; 1067 1068 /* 1069 * Enforce loop invariant: if no room left in current 1070 * buffer, try to get some more. 1071 */ 1072 if (m_room == 0) { 1073 size_t size = mp - mem; 1074 1075 if ((newmem = realloc(mem, size + SFRAG)) == NULL) { 1076 free(mem); 1077 return (-2); 1078 } 1079 mem = newmem; 1080 m_room = SFRAG; 1081 mp = mem + size; 1082 } 1083 } 1084 *mp++ = '\0'; /* loop invariant let's us do this */ 1085 m_room--; 1086 len = mp - mem - 1; 1087 1088 /* 1089 * Give back any extra memory and return value and success. 1090 */ 1091 if (m_room != 0) { 1092 if ((newmem = realloc(mem, (size_t)(mp - mem))) == NULL) { 1093 free(mem); 1094 return (-2); 1095 } 1096 mem = newmem; 1097 } 1098 *str = mem; 1099 return (len); 1100 } 1101 1102 /* 1103 * Cgetnum retrieves the value of the numeric capability cap from the 1104 * capability record pointed to by buf. The numeric value is returned in 1105 * the long pointed to by num. 0 is returned on success, -1 if the requested 1106 * numeric capability couldn't be found. 1107 */ 1108 int 1109 cgetnum(buf, cap, num) 1110 char *buf; 1111 const char *cap; 1112 long *num; 1113 { 1114 long n; 1115 int base, digit; 1116 const char *bp; 1117 1118 _DIAGASSERT(buf != NULL); 1119 _DIAGASSERT(cap != NULL); 1120 _DIAGASSERT(num != NULL); 1121 1122 /* 1123 * Find numeric capability cap 1124 */ 1125 bp = cgetcap(buf, cap, '#'); 1126 if (bp == NULL) 1127 return (-1); 1128 1129 /* 1130 * Look at value and determine numeric base: 1131 * 0x... or 0X... hexadecimal, 1132 * else 0... octal, 1133 * else decimal. 1134 */ 1135 if (*bp == '0') { 1136 bp++; 1137 if (*bp == 'x' || *bp == 'X') { 1138 bp++; 1139 base = 16; 1140 } else 1141 base = 8; 1142 } else 1143 base = 10; 1144 1145 /* 1146 * Conversion loop ... 1147 */ 1148 n = 0; 1149 for (;;) { 1150 if ('0' <= *bp && *bp <= '9') 1151 digit = *bp - '0'; 1152 else if ('a' <= *bp && *bp <= 'f') 1153 digit = 10 + *bp - 'a'; 1154 else if ('A' <= *bp && *bp <= 'F') 1155 digit = 10 + *bp - 'A'; 1156 else 1157 break; 1158 1159 if (digit >= base) 1160 break; 1161 1162 n = n * base + digit; 1163 bp++; 1164 } 1165 1166 /* 1167 * Return value and success. 1168 */ 1169 *num = n; 1170 return (0); 1171 } 1172 1173 1174 /* 1175 * Compare name field of record. 1176 */ 1177 static int 1178 nfcmp(nf, rec) 1179 char *nf, *rec; 1180 { 1181 char *cp, tmp; 1182 int ret; 1183 1184 _DIAGASSERT(nf != NULL); 1185 _DIAGASSERT(rec != NULL); 1186 1187 for (cp = rec; *cp != ':'; cp++) 1188 ; 1189 1190 tmp = *(cp + 1); 1191 *(cp + 1) = '\0'; 1192 ret = strcmp(nf, rec); 1193 *(cp + 1) = tmp; 1194 1195 return (ret); 1196 } 1197