Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. =================================================================== RCS file: /ftp/cvs/cvsroot/src/lib/libc/gen/getcap.c,v rcsdiff: /ftp/cvs/cvsroot/src/lib/libc/gen/getcap.c,v: warning: Unknown phrases like `commitid ...;' are present. retrieving revision 1.21 retrieving revision 1.43 diff -u -p -r1.21 -r1.43 --- src/lib/libc/gen/getcap.c 1998/07/27 07:41:53 1.21 +++ src/lib/libc/gen/getcap.c 2006/03/19 02:12:09 1.43 @@ -1,4 +1,4 @@ -/* $NetBSD: getcap.c,v 1.21 1998/07/27 07:41:53 mycroft Exp $ */ +/* $NetBSD: getcap.c,v 1.43 2006/03/19 02:12:09 christos Exp $ */ /*- * Copyright (c) 1992, 1993 @@ -15,11 +15,7 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors + * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * @@ -36,19 +32,30 @@ * SUCH DAMAGE. */ +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + #include #if defined(LIBC_SCCS) && !defined(lint) #if 0 static char sccsid[] = "@(#)getcap.c 8.3 (Berkeley) 3/25/94"; #else -__RCSID("$NetBSD: getcap.c,v 1.21 1998/07/27 07:41:53 mycroft Exp $"); +__RCSID("$NetBSD: getcap.c,v 1.43 2006/03/19 02:12:09 christos Exp $"); #endif #endif /* LIBC_SCCS and not lint */ +#ifndef SMALL #include "namespace.h" +#endif #include +#include + +#include #include +#ifndef SMALL #include +#endif #include #include #include @@ -58,16 +65,16 @@ __RCSID("$NetBSD: getcap.c,v 1.21 1998/0 #include #ifdef __weak_alias -__weak_alias(cgetcap,_cgetcap); -__weak_alias(cgetclose,_cgetclose); -__weak_alias(cgetent,_cgetent); -__weak_alias(cgetfirst,_cgetfirst); -__weak_alias(cgetmatch,_cgetmatch); -__weak_alias(cgetnext,_cgetnext); -__weak_alias(cgetnum,_cgetnum); -__weak_alias(cgetset,_cgetset); -__weak_alias(cgetstr,_cgetstr); -__weak_alias(cgetustr,_cgetustr); +__weak_alias(cgetcap,_cgetcap) +__weak_alias(cgetclose,_cgetclose) +__weak_alias(cgetent,_cgetent) +__weak_alias(cgetfirst,_cgetfirst) +__weak_alias(cgetmatch,_cgetmatch) +__weak_alias(cgetnext,_cgetnext) +__weak_alias(cgetnum,_cgetnum) +__weak_alias(cgetset,_cgetset) +__weak_alias(cgetstr,_cgetstr) +__weak_alias(cgetustr,_cgetustr) #endif #define BFRAG 1024 @@ -84,9 +91,12 @@ static size_t topreclen; /* toprec leng static char *toprec; /* Additional record specified by cgetset() */ static int gottoprec; /* Flag indicating retrieval of toprecord */ -static int cdbget __P((DB *, char **, const char *)); -static int getent __P((char **, size_t *, char **, int, const char *, int, char *)); -static int nfcmp __P((char *, char *)); +#ifndef SMALL +static int cdbget(DB *, char **, const char *); +#endif +static int getent(char **, size_t *, const char * const *, int, + const char *, int, char *); +static int nfcmp(char *, char *); /* * Cgetset() allows the addition of a user specified buffer to be added @@ -94,9 +104,11 @@ static int nfcmp __P((char *, char *)); * virtual database. 0 is returned on success, -1 on failure. */ int -cgetset(ent) - const char *ent; +cgetset(const char *ent) { + const char *source, *check; + char *dest; + if (ent == NULL) { if (toprec) free(toprec); @@ -110,7 +122,25 @@ cgetset(ent) return (-1); } gottoprec = 0; - (void)strcpy(toprec, ent); /* XXX: strcpy is safe */ + + source=ent; + dest=toprec; + while (*source) { /* Strip whitespace */ + *dest++ = *source++; /* Do not check first field */ + while (*source == ':') { + check=source+1; + while (*check && (isspace((unsigned char)*check) || + (*check=='\\' && isspace((unsigned char)check[1])))) + ++check; + if( *check == ':' ) + source=check; + else + break; + + } + } + *dest=0; + return (0); } @@ -135,6 +165,9 @@ cgetcap(buf, cap, type) char *bp; const char *cp; + _DIAGASSERT(buf != NULL); + _DIAGASSERT(cap != NULL); + bp = buf; for (;;) { /* @@ -181,12 +214,14 @@ cgetcap(buf, cap, type) * reference loop is detected. */ int -cgetent(buf, db_array, name) - char **buf, **db_array; - const char *name; +cgetent(char **buf, const char * const *db_array, const char *name) { size_t dummy; + _DIAGASSERT(buf != NULL); + _DIAGASSERT(db_array != NULL); + _DIAGASSERT(name != NULL); + return (getent(buf, &dummy, db_array, -1, name, 0, NULL)); } @@ -209,20 +244,29 @@ cgetent(buf, db_array, name) * MAX_RECURSION. */ static int -getent(cap, len, db_array, fd, name, depth, nfield) - char **cap, **db_array, *nfield; - const char *name; - size_t *len; - int fd, depth; +getent(char **cap, size_t *len, const char * const *db_array, int fd, + const char *name, int depth, char *nfield) { +#ifndef SMALL DB *capdbp; - char *r_end, *rp = NULL, **db_p; /* pacify gcc */ - int myfd = 0, eof, foundit, retval; + char pbuf[MAXPATHLEN]; + char *cbuf; + int retval; size_t clen; - char *record, *cbuf; +#endif + char *record, *newrecord; + char *r_end, *rp = NULL; /* pacify gcc */ + const char * const *db_p; + int myfd = 0, eof, foundit; int tc_not_resolved; - char pbuf[_POSIX_PATH_MAX]; + _DIAGASSERT(cap != NULL); + _DIAGASSERT(len != NULL); + _DIAGASSERT(db_array != NULL); + /* fd may be -1 */ + _DIAGASSERT(name != NULL); + /* nfield may be NULL */ + /* * Return with ``loop detected'' error if we've recursed more than * MAX_RECURSION times. @@ -267,6 +311,7 @@ getent(cap, len, db_array, fd, name, dep if (fd >= 0) { (void)lseek(fd, (off_t)0, SEEK_SET); } else { +#ifndef SMALL (void)snprintf(pbuf, sizeof(pbuf), "%s.db", *db_p); if ((capdbp = dbopen(pbuf, O_RDONLY, 0, DB_HASH, 0)) != NULL) { @@ -280,15 +325,20 @@ getent(cap, len, db_array, fd, name, dep /* save the data; close frees it */ clen = strlen(record); cbuf = malloc(clen + 1); - memcpy(cbuf, record, clen + 1); + memmove(cbuf, record, clen + 1); if (capdbp->close(capdbp) < 0) { + int serrno = errno; + free(cbuf); + errno = serrno; return (-2); } *len = clen; *cap = cbuf; return (retval); - } else { + } else +#endif + { fd = open(*db_p, O_RDONLY, 0); if (fd < 0) { /* No error on unfound file. */ @@ -334,7 +384,10 @@ getent(cap, len, db_array, fd, name, dep if (myfd) (void)close(fd); if (n < 0) { + int serrno = errno; + free(record); + errno = serrno; return (-2); } else { fd = -1; @@ -392,13 +445,15 @@ getent(cap, len, db_array, fd, name, dep pos = rp - record; newsize = r_end - record + BFRAG; - record = realloc(record, newsize); - if (record == NULL) { - errno = ENOMEM; + newrecord = realloc(record, newsize); + if (newrecord == NULL) { + free(record); if (myfd) (void)close(fd); + errno = ENOMEM; return (-2); } + record = newrecord; r_end = record + newsize; rp = record + pos; } @@ -534,14 +589,16 @@ tc_exp: { newsize = r_end - record + diff + BFRAG; tcpos = tcstart - record; tcposend = tcend - record; - record = realloc(record, newsize); - if (record == NULL) { - errno = ENOMEM; + newrecord = realloc(record, newsize); + if (newrecord == NULL) { + free(record); if (myfd) (void)close(fd); free(icap); + errno = ENOMEM; return (-2); } + record = newrecord; r_end = record + newsize; rp = record + pos; tcstart = record + tcpos; @@ -552,8 +609,8 @@ tc_exp: { * Insert tc'ed record into our record. */ s = tcstart + newilen; - bcopy(tcend, s, (size_t)(rp - tcend)); - bcopy(newicap, tcstart, newilen); + memmove(s, tcend, (size_t)(rp - tcend)); + memmove(tcstart, newicap, newilen); rp += diff; free(icap); @@ -572,12 +629,15 @@ tc_exp: { if (myfd) (void)close(fd); *len = rp - record - 1; /* don't count NUL */ - if (r_end > rp) - if ((record = + if (r_end > rp) { + if ((newrecord = realloc(record, (size_t)(rp - record))) == NULL) { + free(record); errno = ENOMEM; return (-2); } + record = newrecord; + } *cap = record; if (tc_not_resolved) @@ -585,15 +645,18 @@ tc_exp: { return (0); } +#ifndef SMALL static int -cdbget(capdbp, bp, name) - DB *capdbp; - char **bp; - const char *name; +cdbget(DB *capdbp, char **bp, const char *name) { - DBT key, data; + DBT key; + DBT data; - key.data = (char *)name; + _DIAGASSERT(capdbp != NULL); + _DIAGASSERT(bp != NULL); + _DIAGASSERT(name != NULL); + + key.data = __UNCONST(name); key.size = strlen(name); for (;;) { @@ -616,17 +679,20 @@ cdbget(capdbp, bp, name) *bp = (char *)data.data + 1; return (((char *)(data.data))[0] == TCERR ? 1 : 0); } +#endif /* * Cgetmatch will return 0 if name is one of the names of the capability * record buf, -1 if not. */ int -cgetmatch(buf, name) - const char *buf, *name; +cgetmatch(const char *buf, const char *name) { const char *np, *bp; + _DIAGASSERT(buf != NULL); + _DIAGASSERT(name != NULL); + /* * Start search at beginning of record. */ @@ -637,19 +703,22 @@ cgetmatch(buf, name) */ np = name; for (;;) - if (*np == '\0') + if (*np == '\0') { if (*bp == '|' || *bp == ':' || *bp == '\0') return (0); else break; - else + } else if (*bp++ != *np++) break; /* * Match failed, skip to next name in record. */ - bp--; /* a '|' or ':' may have stopped the match */ + if (bp > buf) + bp--; /* a '|' or ':' may have stopped the match */ + else + return (-1); for (;;) if (*bp == '\0' || *bp == ':') return (-1); /* match failed totally */ @@ -660,19 +729,22 @@ cgetmatch(buf, name) } int -cgetfirst(buf, db_array) - char **buf, **db_array; +cgetfirst(char **buf, const char * const *db_array) { + + _DIAGASSERT(buf != NULL); + _DIAGASSERT(db_array != NULL); + (void)cgetclose(); return (cgetnext(buf, db_array)); } static FILE *pfp; static int slash; -static char **dbp; +static const char * const *dbp; int -cgetclose() +cgetclose(void) { if (pfp != NULL) { (void)fclose(pfp); @@ -690,15 +762,16 @@ cgetclose() * upon returning an entry with more remaining, and -1 if an error occurs. */ int -cgetnext(bp, db_array) - char **bp; - char **db_array; +cgetnext(char **bp, const char * const *db_array) { - size_t len; + size_t len = 0; int status, done; char *cp, *line, *rp, *np, buf[BSIZE], nbuf[BSIZE]; size_t dummy; + _DIAGASSERT(bp != NULL); + _DIAGASSERT(db_array != NULL); + if (dbp == NULL) dbp = db_array; @@ -735,7 +808,7 @@ cgetnext(bp, db_array) slash = 0; continue; } - if (isspace(*line) || + if (isspace((unsigned char)*line) || *line == ':' || *line == '#' || slash) { if (line[len - 2] == '\\') slash = 1; @@ -753,6 +826,8 @@ cgetnext(bp, db_array) /* * Line points to a name line. */ + if (len > sizeof(nbuf)) + return -1; done = 0; np = nbuf; for (;;) { @@ -784,6 +859,8 @@ cgetnext(bp, db_array) line[len - 1] = '\0'; } } + if (len > sizeof(buf)) + return -1; rp = buf; for(cp = nbuf; *cp != '\0'; cp++) if (*cp == '|' || *cp == ':') @@ -819,16 +896,17 @@ cgetnext(bp, db_array) * allocation failure). */ int -cgetstr(buf, cap, str) - char *buf; - const char *cap; - char **str; +cgetstr(char *buf, const char *cap, char **str) { u_int m_room; const char *bp; char *mp; int len; - char *mem; + char *mem, *newmem; + + _DIAGASSERT(buf != NULL); + _DIAGASSERT(cap != NULL); + _DIAGASSERT(str != NULL); /* * Find string capability cap @@ -915,8 +993,11 @@ cgetstr(buf, cap, str) if (m_room == 0) { size_t size = mp - mem; - if ((mem = realloc(mem, size + SFRAG)) == NULL) + if ((newmem = realloc(mem, size + SFRAG)) == NULL) { + free(mem); return (-2); + } + mem = newmem; m_room = SFRAG; mp = mem + size; } @@ -928,9 +1009,13 @@ cgetstr(buf, cap, str) /* * Give back any extra memory and return value and success. */ - if (m_room != 0) - if ((mem = realloc(mem, (size_t)(mp - mem))) == NULL) + if (m_room != 0) { + if ((newmem = realloc(mem, (size_t)(mp - mem))) == NULL) { + free(mem); return (-2); + } + mem = newmem; + } *str = mem; return (len); } @@ -946,16 +1031,17 @@ cgetstr(buf, cap, str) * error was encountered (storage allocation failure). */ int -cgetustr(buf, cap, str) - char *buf; - const char *cap; - char **str; +cgetustr(char *buf, const char *cap, char **str) { u_int m_room; const char *bp; char *mp; int len; - char *mem; + char *mem, *newmem; + + _DIAGASSERT(buf != NULL); + _DIAGASSERT(cap != NULL); + _DIAGASSERT(str != NULL); /* * Find string capability cap @@ -991,8 +1077,11 @@ cgetustr(buf, cap, str) if (m_room == 0) { size_t size = mp - mem; - if ((mem = realloc(mem, size + SFRAG)) == NULL) + if ((newmem = realloc(mem, size + SFRAG)) == NULL) { + free(mem); return (-2); + } + mem = newmem; m_room = SFRAG; mp = mem + size; } @@ -1004,9 +1093,13 @@ cgetustr(buf, cap, str) /* * Give back any extra memory and return value and success. */ - if (m_room != 0) - if ((mem = realloc(mem, (size_t)(mp - mem))) == NULL) + if (m_room != 0) { + if ((newmem = realloc(mem, (size_t)(mp - mem))) == NULL) { + free(mem); return (-2); + } + mem = newmem; + } *str = mem; return (len); } @@ -1018,15 +1111,16 @@ cgetustr(buf, cap, str) * numeric capability couldn't be found. */ int -cgetnum(buf, cap, num) - char *buf; - const char *cap; - long *num; +cgetnum(char *buf, const char *cap, long *num) { long n; int base, digit; const char *bp; + _DIAGASSERT(buf != NULL); + _DIAGASSERT(cap != NULL); + _DIAGASSERT(num != NULL); + /* * Find numeric capability cap */ @@ -1083,12 +1177,14 @@ cgetnum(buf, cap, num) * Compare name field of record. */ static int -nfcmp(nf, rec) - char *nf, *rec; +nfcmp(char *nf, char *rec) { char *cp, tmp; int ret; - + + _DIAGASSERT(nf != NULL); + _DIAGASSERT(rec != NULL); + for (cp = rec; *cp != ':'; cp++) ;