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