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