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