[BACK]Return to getcap.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / lib / libc / gen

Annotation of src/lib/libc/gen/getcap.c, Revision 1.22

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

CVSweb <webmaster@jp.NetBSD.org>