version 1.10.4.1, 1996/09/16 18:40:22 |
version 1.50.2.1, 2012/04/17 00:05:18 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
* 2. Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* documentation and/or other materials provided with the distribution. |
* 3. All advertising materials mentioning features or use of this software |
* 3. Neither the name of the University nor the names of its contributors |
* 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 |
|
* may be used to endorse or promote products derived from this software |
* may be used to endorse or promote products derived from this software |
* without specific prior written permission. |
* without specific prior written permission. |
* |
* |
|
|
* SUCH DAMAGE. |
* SUCH DAMAGE. |
*/ |
*/ |
|
|
|
#if HAVE_NBTOOL_CONFIG_H |
|
#include "nbtool_config.h" |
|
#endif |
|
|
|
#include <sys/cdefs.h> |
#if defined(LIBC_SCCS) && !defined(lint) |
#if defined(LIBC_SCCS) && !defined(lint) |
#if 0 |
#if 0 |
static char sccsid[] = "@(#)getcap.c 8.3 (Berkeley) 3/25/94"; |
static char sccsid[] = "@(#)getcap.c 8.3 (Berkeley) 3/25/94"; |
#else |
#else |
static char rcsid[] = "$NetBSD$"; |
__RCSID("$NetBSD$"); |
#endif |
#endif |
#endif /* LIBC_SCCS and not lint */ |
#endif /* LIBC_SCCS and not lint */ |
|
|
|
#ifndef SMALL |
#include "namespace.h" |
#include "namespace.h" |
|
#endif |
#include <sys/types.h> |
#include <sys/types.h> |
|
#include <sys/param.h> |
|
|
|
#include <assert.h> |
|
#include <stddef.h> |
#include <ctype.h> |
#include <ctype.h> |
|
#ifndef SMALL |
#include <db.h> |
#include <db.h> |
|
#endif |
#include <errno.h> |
#include <errno.h> |
#include <fcntl.h> |
#include <fcntl.h> |
#include <limits.h> |
#include <limits.h> |
Line 57 static char rcsid[] = "$NetBSD$"; |
|
Line 65 static char rcsid[] = "$NetBSD$"; |
|
#include <string.h> |
#include <string.h> |
#include <unistd.h> |
#include <unistd.h> |
|
|
|
#if defined(__weak_alias) && !defined(SMALL) |
|
__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(csetexpandtc,_csetexpandtc) |
|
#endif |
|
|
#define BFRAG 1024 |
#define BFRAG 1024 |
#define BSIZE 1024 |
#define BSIZE 1024 |
#define ESC ('[' & 037) /* ASCII ESC */ |
#define ESC ('[' & 037) /* ASCII ESC */ |
Line 70 static char rcsid[] = "$NetBSD$"; |
|
Line 92 static char rcsid[] = "$NetBSD$"; |
|
static size_t topreclen; /* toprec length */ |
static size_t topreclen; /* toprec length */ |
static char *toprec; /* Additional record specified by cgetset() */ |
static char *toprec; /* Additional record specified by cgetset() */ |
static int gottoprec; /* Flag indicating retrieval of toprecord */ |
static int gottoprec; /* Flag indicating retrieval of toprecord */ |
|
static int expandtc = 1; /* flag to expand tc= or not */ |
|
|
static int cdbget __P((DB *, char **, char *)); |
#ifndef SMALL |
static int getent __P((char **, u_int *, char **, int, char *, int, char *)); |
static int cdbget(DB *, char **, const char *); |
static int nfcmp __P((char *, 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 |
* Cgetset() allows the addition of a user specified buffer to be added |
Line 81 static int nfcmp __P((char *, char *)); |
|
Line 107 static int nfcmp __P((char *, char *)); |
|
* virtual database. 0 is returned on success, -1 on failure. |
* virtual database. 0 is returned on success, -1 on failure. |
*/ |
*/ |
int |
int |
cgetset(ent) |
cgetset(const char *ent) |
char *ent; |
|
{ |
{ |
|
const char *source, *check; |
|
char *dest; |
|
|
if (ent == NULL) { |
if (ent == NULL) { |
if (toprec) |
if (toprec != NULL) |
free(toprec); |
free(toprec); |
toprec = NULL; |
toprec = NULL; |
topreclen = 0; |
topreclen = 0; |
return (0); |
return 0; |
} |
} |
topreclen = strlen(ent); |
topreclen = strlen(ent); |
if ((toprec = malloc (topreclen + 1)) == NULL) { |
if ((toprec = malloc(topreclen + 1)) == NULL) { |
errno = ENOMEM; |
errno = ENOMEM; |
return (-1); |
return -1; |
} |
} |
gottoprec = 0; |
gottoprec = 0; |
(void)strcpy(toprec, ent); |
|
return (0); |
source = ent; |
|
dest = toprec; |
|
while (*source != '\0') { /* 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; |
} |
} |
|
|
/* |
/* |
|
|
*/ |
*/ |
char * |
char * |
cgetcap(buf, cap, type) |
cgetcap(buf, cap, type) |
char *buf, *cap; |
char *buf; |
|
const char *cap; |
int type; |
int type; |
{ |
{ |
register char *bp, *cp; |
char *bp; |
|
const char *cp; |
|
|
|
_DIAGASSERT(buf != NULL); |
|
_DIAGASSERT(cap != NULL); |
|
|
bp = buf; |
bp = buf; |
for (;;) { |
for (;;) { |
Line 129 cgetcap(buf, cap, type) |
|
Line 180 cgetcap(buf, cap, type) |
|
*/ |
*/ |
for (;;) |
for (;;) |
if (*bp == '\0') |
if (*bp == '\0') |
return (NULL); |
return NULL; |
else |
else if (*bp++ == ':') |
if (*bp++ == ':') |
break; |
break; |
|
|
|
/* |
/* |
* Try to match (cap, type) in buf. |
* Try to match (cap, type) in buf. |
Line 142 cgetcap(buf, cap, type) |
|
Line 192 cgetcap(buf, cap, type) |
|
if (*cp != '\0') |
if (*cp != '\0') |
continue; |
continue; |
if (*bp == '@') |
if (*bp == '@') |
return (NULL); |
return NULL; |
if (type == ':') { |
if (type == ':') { |
if (*bp != '\0' && *bp != ':') |
if (*bp != '\0' && *bp != ':') |
continue; |
continue; |
return(bp); |
return bp; |
} |
} |
if (*bp != type) |
if (*bp != type) |
continue; |
continue; |
bp++; |
bp++; |
return (*bp == '@' ? NULL : bp); |
return *bp == '@' ? NULL : bp; |
} |
} |
/* NOTREACHED */ |
/* NOTREACHED */ |
} |
} |
Line 165 cgetcap(buf, cap, type) |
|
Line 215 cgetcap(buf, cap, type) |
|
* encountered (couldn't open/read a file, etc.), and -3 if a potential |
* encountered (couldn't open/read a file, etc.), and -3 if a potential |
* reference loop is detected. |
* reference loop is detected. |
*/ |
*/ |
|
/* coverity[+alloc : arg-*0] */ |
int |
int |
cgetent(buf, db_array, name) |
cgetent(char **buf, const char * const *db_array, const char *name) |
char **buf, **db_array, *name; |
|
{ |
{ |
u_int dummy; |
size_t dummy; |
|
|
|
_DIAGASSERT(buf != NULL); |
|
_DIAGASSERT(db_array != NULL); |
|
_DIAGASSERT(name != NULL); |
|
|
return (getent(buf, &dummy, db_array, -1, name, 0, NULL)); |
return getent(buf, &dummy, db_array, -1, name, 0, NULL); |
|
} |
|
|
|
void |
|
csetexpandtc(int etc) |
|
{ |
|
expandtc = etc; |
} |
} |
|
|
/* |
/* |
Line 192 cgetent(buf, db_array, name) |
|
Line 252 cgetent(buf, db_array, name) |
|
* names interpolated, a name can't be found, or depth exceeds |
* names interpolated, a name can't be found, or depth exceeds |
* MAX_RECURSION. |
* MAX_RECURSION. |
*/ |
*/ |
|
/* coverity[+alloc : arg-*0] */ |
static int |
static int |
getent(cap, len, db_array, fd, name, depth, nfield) |
getent(char **cap, size_t *len, const char * const *db_array, int fd, |
char **cap, **db_array, *name, *nfield; |
const char *name, int depth, char *nfield) |
u_int *len; |
|
int fd, depth; |
|
{ |
{ |
|
#ifndef SMALL |
DB *capdbp; |
DB *capdbp; |
DBT key, data; |
char pbuf[MAXPATHLEN]; |
register char *r_end, *rp, **db_p; |
char *cbuf; |
int myfd, eof, foundit, retval, clen; |
int retval; |
char *record, *cbuf; |
size_t clen; |
|
#endif |
|
char *record, *newrecord; |
|
char *r_end, *rp; /* pacify gcc */ |
|
const char * const *db_p; |
|
int myfd, eof, foundit; |
int tc_not_resolved; |
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 */ |
|
|
|
myfd = 0; |
|
rp = NULL; |
|
|
/* |
/* |
* Return with ``loop detected'' error if we've recursed more than |
* Return with ``loop detected'' error if we've recursed more than |
* MAX_RECURSION times. |
* MAX_RECURSION times. |
*/ |
*/ |
if (depth > MAX_RECURSION) |
if (depth > MAX_RECURSION) |
return (-3); |
return -3; |
|
|
/* |
/* |
* Check if we have a top record from cgetset(). |
* Check if we have a top record from cgetset(). |
*/ |
*/ |
if (depth == 0 && toprec != NULL && cgetmatch(toprec, name) == 0) { |
if (depth == 0 && toprec != NULL && cgetmatch(toprec, name) == 0) { |
if ((record = malloc (topreclen + BFRAG)) == NULL) { |
if ((record = malloc(topreclen + BFRAG)) == NULL) { |
errno = ENOMEM; |
errno = ENOMEM; |
return (-2); |
return -2; |
} |
} |
(void)strcpy(record, toprec); |
(void)strcpy(record, toprec); /* XXX: strcpy is safe */ |
myfd = 0; |
|
db_p = db_array; |
db_p = db_array; |
rp = record + topreclen + 1; |
rp = record + topreclen + 1; |
r_end = rp + BFRAG; |
r_end = rp + BFRAG; |
Line 233 getent(cap, len, db_array, fd, name, dep |
|
Line 306 getent(cap, len, db_array, fd, name, dep |
|
*/ |
*/ |
if ((record = malloc(BFRAG)) == NULL) { |
if ((record = malloc(BFRAG)) == NULL) { |
errno = ENOMEM; |
errno = ENOMEM; |
return (-2); |
return -2; |
} |
} |
r_end = record + BFRAG; |
r_end = record + BFRAG; |
foundit = 0; |
foundit = 0; |
Line 250 getent(cap, len, db_array, fd, name, dep |
|
Line 323 getent(cap, len, db_array, fd, name, dep |
|
|
|
if (fd >= 0) { |
if (fd >= 0) { |
(void)lseek(fd, (off_t)0, SEEK_SET); |
(void)lseek(fd, (off_t)0, SEEK_SET); |
myfd = 0; |
|
} else { |
} else { |
|
#ifndef SMALL |
(void)snprintf(pbuf, sizeof(pbuf), "%s.db", *db_p); |
(void)snprintf(pbuf, sizeof(pbuf), "%s.db", *db_p); |
if ((capdbp = dbopen(pbuf, O_RDONLY, 0, DB_HASH, 0)) |
if (expandtc && |
|
(capdbp = dbopen(pbuf, O_RDONLY, 0, DB_HASH, 0)) |
!= NULL) { |
!= NULL) { |
free(record); |
free(record); |
retval = cdbget(capdbp, &record, name); |
retval = cdbget(capdbp, &record, name); |
if (retval < 0) { |
if (retval < 0) { |
/* no record available */ |
/* no record available */ |
(void)capdbp->close(capdbp); |
(void)capdbp->close(capdbp); |
return (retval); |
return retval; |
} |
} |
/* save the data; close frees it */ |
/* save the data; close frees it */ |
clen = strlen(record); |
clen = strlen(record); |
cbuf = malloc(clen + 1); |
if ((cbuf = malloc(clen + 1)) == NULL) { |
memcpy(cbuf, record, clen + 1); |
(void)capdbp->close(capdbp); |
|
errno = ENOMEM; |
|
return -2; |
|
} |
|
memmove(cbuf, record, clen + 1); |
if (capdbp->close(capdbp) < 0) { |
if (capdbp->close(capdbp) < 0) { |
|
int serrno = errno; |
|
|
free(cbuf); |
free(cbuf); |
return (-2); |
errno = serrno; |
|
return -2; |
} |
} |
*len = clen; |
*len = clen; |
*cap = cbuf; |
*cap = cbuf; |
return (retval); |
return retval; |
} else { |
} else |
|
#endif |
|
{ |
fd = open(*db_p, O_RDONLY, 0); |
fd = open(*db_p, O_RDONLY, 0); |
if (fd < 0) { |
if (fd < 0) { |
/* No error on unfound file. */ |
/* No error on unfound file. */ |
Line 287 getent(cap, len, db_array, fd, name, dep |
|
Line 370 getent(cap, len, db_array, fd, name, dep |
|
*/ |
*/ |
{ |
{ |
char buf[BUFSIZ]; |
char buf[BUFSIZ]; |
register char *b_end, *bp; |
char *b_end, *bp, *cp; |
register int c; |
int c, slash; |
|
|
/* |
/* |
* Loop invariants: |
* Loop invariants: |
Line 297 getent(cap, len, db_array, fd, name, dep |
|
Line 380 getent(cap, len, db_array, fd, name, dep |
|
* Rp always points just past last character in record. |
* Rp always points just past last character in record. |
* B_end always points just past last character in buf. |
* B_end always points just past last character in buf. |
* Bp always points at next character in buf. |
* Bp always points at next character in buf. |
|
* Cp remembers where the last colon was. |
*/ |
*/ |
b_end = buf; |
b_end = buf; |
bp = buf; |
bp = buf; |
|
cp = NULL; |
|
slash = 0; |
for (;;) { |
for (;;) { |
|
|
/* |
/* |
* Read in a line implementing (\, newline) |
* Read in a line implementing (\, newline) |
* line continuation. |
* line continuation. |
Line 309 getent(cap, len, db_array, fd, name, dep |
|
Line 394 getent(cap, len, db_array, fd, name, dep |
|
rp = record; |
rp = record; |
for (;;) { |
for (;;) { |
if (bp >= b_end) { |
if (bp >= b_end) { |
int n; |
ssize_t n; |
|
|
n = read(fd, buf, sizeof(buf)); |
n = read(fd, buf, sizeof(buf)); |
if (n <= 0) { |
if (n <= 0) { |
if (myfd) |
if (myfd) |
(void)close(fd); |
(void)close(fd); |
if (n < 0) { |
if (n < 0) { |
|
int serrno = errno; |
|
|
free(record); |
free(record); |
return (-2); |
errno = serrno; |
|
return -2; |
} else { |
} else { |
fd = -1; |
fd = -1; |
eof = 1; |
eof = 1; |
Line 330 getent(cap, len, db_array, fd, name, dep |
|
Line 418 getent(cap, len, db_array, fd, name, dep |
|
|
|
c = *bp++; |
c = *bp++; |
if (c == '\n') { |
if (c == '\n') { |
if (rp > record && *(rp-1) == '\\') { |
if (slash) { |
|
slash = 0; |
rp--; |
rp--; |
continue; |
continue; |
} else |
} else |
break; |
break; |
} |
} |
|
if (slash) { |
|
slash = 0; |
|
cp = 0; |
|
} |
|
if (c == ':') { |
|
/* |
|
* If the field was `empty' (i.e. |
|
* contained only white space), back up |
|
* to the colon (eliminating the |
|
* field). |
|
*/ |
|
if (cp != NULL) |
|
rp = cp; |
|
else |
|
cp = rp; |
|
} else if (c == '\\') { |
|
slash = 1; |
|
} else if (c != ' ' && c != '\t') { |
|
/* |
|
* Forget where the colon was, as this |
|
* is not an empty field. |
|
*/ |
|
cp = 0; |
|
} |
*rp++ = c; |
*rp++ = c; |
|
|
/* |
/* |
Line 344 getent(cap, len, db_array, fd, name, dep |
|
Line 457 getent(cap, len, db_array, fd, name, dep |
|
* some more. |
* some more. |
*/ |
*/ |
if (rp >= r_end) { |
if (rp >= r_end) { |
u_int pos; |
ptrdiff_t pos; |
size_t newsize; |
size_t newsize; |
|
|
pos = rp - record; |
pos = rp - record; |
newsize = r_end - record + BFRAG; |
newsize = r_end - record + BFRAG; |
record = realloc(record, newsize); |
newrecord = realloc(record, newsize); |
if (record == NULL) { |
if (newrecord == NULL) { |
errno = ENOMEM; |
free(record); |
if (myfd) |
if (myfd) |
(void)close(fd); |
(void)close(fd); |
return (-2); |
errno = ENOMEM; |
|
return -2; |
} |
} |
|
record = newrecord; |
r_end = record + newsize; |
r_end = record + newsize; |
rp = record + pos; |
rp = record + pos; |
} |
} |
} |
} |
/* loop invariant let's us do this */ |
/* Eliminate any white space after the last colon. */ |
|
if (cp) |
|
rp = cp + 1; |
|
/* Loop invariant lets us do this. */ |
*rp++ = '\0'; |
*rp++ = '\0'; |
|
|
/* |
/* |
Line 378 getent(cap, len, db_array, fd, name, dep |
|
Line 496 getent(cap, len, db_array, fd, name, dep |
|
/* |
/* |
* See if this is the record we want ... |
* See if this is the record we want ... |
*/ |
*/ |
if (cgetmatch(record, name) == 0) { |
if (cgetmatch(record, name) == 0) |
if (nfield == NULL || !nfcmp(nfield, record)) { |
if (nfield == NULL || !nfcmp(nfield, record)) { |
foundit = 1; |
foundit = 1; |
break; /* found it! */ |
break; /* found it! */ |
} |
} |
} |
|
} |
} |
} |
} |
if (foundit) |
if (foundit) |
break; |
break; |
} |
} |
|
|
if (!foundit) |
if (!foundit) |
return (-1); |
return -1; |
|
|
/* |
/* |
* Got the capability record, but now we have to expand all tc=name |
* Got the capability record, but now we have to expand all tc=name |
* references in it ... |
* references in it ... |
*/ |
*/ |
tc_exp: { |
tc_exp: |
register char *newicap, *s; |
tc_not_resolved = 0; |
register int newilen; |
if (expandtc) { |
u_int ilen; |
char *newicap, *s; |
int diff, iret, tclen; |
size_t ilen, newilen; |
|
int iret; |
|
ptrdiff_t diff, tclen; |
char *icap, *scan, *tc, *tcstart, *tcend; |
char *icap, *scan, *tc, *tcstart, *tcend; |
|
|
/* |
/* |
|
|
* scanned for tc=name constructs. |
* scanned for tc=name constructs. |
*/ |
*/ |
scan = record; |
scan = record; |
tc_not_resolved = 0; |
|
for (;;) { |
for (;;) { |
if ((tc = cgetcap(scan, "tc", '=')) == NULL) |
if ((tc = cgetcap(scan, "tc", '=')) == NULL) |
break; |
break; |
|
|
if (myfd) |
if (myfd) |
(void)close(fd); |
(void)close(fd); |
free(record); |
free(record); |
return (iret); |
return iret; |
} |
} |
if (iret == 1) |
if (iret == 1) |
tc_not_resolved = 1; |
tc_not_resolved = 1; |
|
|
for (;;) |
for (;;) |
if (*s == '\0') |
if (*s == '\0') |
break; |
break; |
else |
else if (*s++ == ':') |
if (*s++ == ':') |
break; |
break; |
|
newilen -= s - newicap; |
newilen -= s - newicap; |
newicap = s; |
newicap = s; |
|
|
/* make sure interpolated record is `:'-terminated */ |
/* make sure interpolated record is `:'-terminated */ |
s += newilen; |
s += newilen; |
if (*(s-1) != ':') { |
if (*(s - 1) != ':') { |
*s = ':'; /* overwrite NUL with : */ |
*s = ':'; /* overwrite NUL with : */ |
newilen++; |
newilen++; |
} |
} |
|
|
*/ |
*/ |
diff = newilen - tclen; |
diff = newilen - tclen; |
if (diff >= r_end - rp) { |
if (diff >= r_end - rp) { |
u_int pos, tcpos, tcposend; |
ptrdiff_t pos, tcpos, tcposend; |
size_t newsize; |
size_t newsize; |
|
|
pos = rp - record; |
pos = rp - record; |
newsize = r_end - record + diff + BFRAG; |
newsize = r_end - record + diff + BFRAG; |
tcpos = tcstart - record; |
tcpos = tcstart - record; |
tcposend = tcend - record; |
tcposend = tcend - record; |
record = realloc(record, newsize); |
newrecord = realloc(record, newsize); |
if (record == NULL) { |
if (newrecord == NULL) { |
errno = ENOMEM; |
free(record); |
if (myfd) |
if (myfd) |
(void)close(fd); |
(void)close(fd); |
free(icap); |
free(icap); |
return (-2); |
errno = ENOMEM; |
|
return -2; |
} |
} |
|
record = newrecord; |
r_end = record + newsize; |
r_end = record + newsize; |
rp = record + pos; |
rp = record + pos; |
tcstart = record + tcpos; |
tcstart = record + tcpos; |
|
|
* Insert tc'ed record into our record. |
* Insert tc'ed record into our record. |
*/ |
*/ |
s = tcstart + newilen; |
s = tcstart + newilen; |
bcopy(tcend, s, rp - tcend); |
memmove(s, tcend, (size_t)(rp - tcend)); |
bcopy(newicap, tcstart, newilen); |
memmove(tcstart, newicap, newilen); |
rp += diff; |
rp += diff; |
free(icap); |
free(icap); |
|
|
|
|
* Start scan on `:' so next cgetcap works properly |
* Start scan on `:' so next cgetcap works properly |
* (cgetcap always skips first field). |
* (cgetcap always skips first field). |
*/ |
*/ |
scan = s-1; |
scan = s - 1; |
} |
} |
|
|
} |
} |
|
|
if (myfd) |
if (myfd) |
(void)close(fd); |
(void)close(fd); |
*len = rp - record - 1; /* don't count NUL */ |
*len = rp - record - 1; /* don't count NUL */ |
if (r_end > rp) |
if (r_end > rp) { |
if ((record = |
if ((newrecord = |
realloc(record, (size_t)(rp - record))) == NULL) { |
realloc(record, (size_t)(rp - record))) == NULL) { |
|
free(record); |
errno = ENOMEM; |
errno = ENOMEM; |
return (-2); |
return -2; |
} |
} |
|
record = newrecord; |
|
} |
|
|
*cap = record; |
*cap = record; |
if (tc_not_resolved) |
if (tc_not_resolved) |
return (1); |
return 1; |
return (0); |
return 0; |
} |
} |
|
|
|
#ifndef SMALL |
static int |
static int |
cdbget(capdbp, bp, name) |
cdbget(DB *capdbp, char **bp, const char *name) |
DB *capdbp; |
|
char **bp, *name; |
|
{ |
{ |
DBT key, data; |
DBT key; |
char *buf; |
DBT data; |
int st; |
|
|
_DIAGASSERT(capdbp != NULL); |
|
_DIAGASSERT(bp != NULL); |
|
_DIAGASSERT(name != NULL); |
|
|
key.data = name; |
key.data = __UNCONST(name); |
key.size = strlen(name); |
key.size = strlen(name); |
|
|
for (;;) { |
for (;;) { |
/* Get the reference. */ |
/* Get the reference. */ |
switch(capdbp->get(capdbp, &key, &data, 0)) { |
switch(capdbp->get(capdbp, &key, &data, 0)) { |
case -1: |
case -1: |
return (-2); |
return -2; |
case 1: |
case 1: |
return (-1); |
return -1; |
} |
} |
|
|
/* If not an index to another record, leave. */ |
/* If not an index to another record, leave. */ |
Line 570 cdbget(capdbp, bp, name) |
|
Line 694 cdbget(capdbp, bp, name) |
|
} |
} |
|
|
*bp = (char *)data.data + 1; |
*bp = (char *)data.data + 1; |
return (((char *)(data.data))[0] == TCERR ? 1 : 0); |
return ((char *)(data.data))[0] == TCERR ? 1 : 0; |
} |
} |
|
#endif |
|
|
/* |
/* |
* Cgetmatch will return 0 if name is one of the names of the capability |
* Cgetmatch will return 0 if name is one of the names of the capability |
* record buf, -1 if not. |
* record buf, -1 if not. |
*/ |
*/ |
int |
int |
cgetmatch(buf, name) |
cgetmatch(const char *buf, const char *name) |
char *buf, *name; |
|
{ |
{ |
register char *np, *bp; |
const char *np, *bp; |
|
|
|
_DIAGASSERT(buf != NULL); |
|
_DIAGASSERT(name != NULL); |
|
|
/* |
/* |
* Start search at beginning of record. |
* Start search at beginning of record. |
Line 593 cgetmatch(buf, name) |
|
Line 720 cgetmatch(buf, name) |
|
*/ |
*/ |
np = name; |
np = name; |
for (;;) |
for (;;) |
if (*np == '\0') |
if (*np == '\0') { |
if (*bp == '|' || *bp == ':' || *bp == '\0') |
if (*bp == '|' || *bp == ':' || *bp == '\0') |
return (0); |
return 0; |
else |
else |
break; |
break; |
else |
} else if (*bp++ != *np++) |
if (*bp++ != *np++) |
break; |
break; |
|
|
|
/* |
/* |
* Match failed, skip to next name in record. |
* 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 (;;) |
for (;;) |
if (*bp == '\0' || *bp == ':') |
if (*bp == '\0' || *bp == ':') |
return (-1); /* match failed totally */ |
return -1; /* match failed totally */ |
else |
else if (*bp++ == '|') |
if (*bp++ == '|') |
break; /* found next name */ |
break; /* found next name */ |
|
} |
} |
} |
} |
|
|
|
|
|
|
|
|
|
|
int |
int |
cgetfirst(buf, db_array) |
cgetfirst(char **buf, const char * const *db_array) |
char **buf, **db_array; |
|
{ |
{ |
|
|
|
_DIAGASSERT(buf != NULL); |
|
_DIAGASSERT(db_array != NULL); |
|
|
(void)cgetclose(); |
(void)cgetclose(); |
return (cgetnext(buf, db_array)); |
return cgetnext(buf, db_array); |
} |
} |
|
|
static FILE *pfp; |
static FILE *pfp; |
static int slash; |
static int slash; |
static char **dbp; |
static const char * const *dbp; |
|
|
int |
int |
cgetclose() |
cgetclose(void) |
{ |
{ |
if (pfp != NULL) { |
if (pfp != NULL) { |
(void)fclose(pfp); |
(void)fclose(pfp); |
|
|
dbp = NULL; |
dbp = NULL; |
gottoprec = 0; |
gottoprec = 0; |
slash = 0; |
slash = 0; |
return(0); |
return 0; |
} |
} |
|
|
/* |
/* |
|
|
* specified by db_array. It returns 0 upon completion of the database, 1 |
* specified by db_array. It returns 0 upon completion of the database, 1 |
* upon returning an entry with more remaining, and -1 if an error occurs. |
* upon returning an entry with more remaining, and -1 if an error occurs. |
*/ |
*/ |
|
/* coverity[+alloc : arg-*0] */ |
int |
int |
cgetnext(bp, db_array) |
cgetnext(char **bp, const char * const *db_array) |
register char **bp; |
|
char **db_array; |
|
{ |
{ |
size_t len; |
size_t len = 0; |
int status, i, done; |
int status, done; |
char *cp, *line, *rp, *np, buf[BSIZE], nbuf[BSIZE]; |
char *cp, *line, *rp, *np, buf[BSIZE], nbuf[BSIZE]; |
u_int dummy; |
size_t dummy; |
|
|
|
_DIAGASSERT(bp != NULL); |
|
_DIAGASSERT(db_array != NULL); |
|
|
if (dbp == NULL) |
if (dbp == NULL) |
dbp = db_array; |
dbp = db_array; |
|
|
if (pfp == NULL && (pfp = fopen(*dbp, "r")) == NULL) { |
if (pfp == NULL && (pfp = fopen(*dbp, "re")) == NULL) { |
(void)cgetclose(); |
(void)cgetclose(); |
return (-1); |
return -1; |
} |
} |
for(;;) { |
for (;;) { |
if (toprec && !gottoprec) { |
if (toprec != NULL && !gottoprec) { |
gottoprec = 1; |
gottoprec = 1; |
line = toprec; |
line = toprec; |
} else { |
} else { |
line = fgetln(pfp, &len); |
line = fgetln(pfp, &len); |
if (line == NULL && pfp) { |
if (line == NULL) { |
(void)fclose(pfp); |
if (pfp == NULL) |
|
return -1; |
if (ferror(pfp)) { |
if (ferror(pfp)) { |
(void)cgetclose(); |
(void)cgetclose(); |
return (-1); |
return -1; |
} else { |
} else { |
|
(void)fclose(pfp); |
|
pfp = NULL; |
if (*++dbp == NULL) { |
if (*++dbp == NULL) { |
(void)cgetclose(); |
(void)cgetclose(); |
return (0); |
return 0; |
} else if ((pfp = |
} else if ((pfp = |
fopen(*dbp, "r")) == NULL) { |
fopen(*dbp, "re")) == NULL) { |
(void)cgetclose(); |
(void)cgetclose(); |
return (-1); |
return -1; |
} else |
} else |
continue; |
continue; |
} |
} |
Line 694 cgetnext(bp, db_array) |
|
Line 826 cgetnext(bp, db_array) |
|
slash = 0; |
slash = 0; |
continue; |
continue; |
} |
} |
if (isspace(*line) || |
if (isspace((unsigned char)*line) || |
*line == ':' || *line == '#' || slash) { |
*line == ':' || *line == '#' || slash) { |
if (line[len - 2] == '\\') |
if (line[len - 2] == '\\') |
slash = 1; |
slash = 1; |
Line 712 cgetnext(bp, db_array) |
|
Line 844 cgetnext(bp, db_array) |
|
/* |
/* |
* Line points to a name line. |
* Line points to a name line. |
*/ |
*/ |
i = 0; |
if (len > sizeof(nbuf)) |
|
return -1; |
done = 0; |
done = 0; |
np = nbuf; |
np = nbuf; |
for (;;) { |
for (;;) { |
Line 732 cgetnext(bp, db_array) |
|
Line 865 cgetnext(bp, db_array) |
|
} else { /* name field extends beyond the line */ |
} else { /* name field extends beyond the line */ |
line = fgetln(pfp, &len); |
line = fgetln(pfp, &len); |
if (line == NULL && pfp) { |
if (line == NULL && pfp) { |
(void)fclose(pfp); |
|
if (ferror(pfp)) { |
if (ferror(pfp)) { |
(void)cgetclose(); |
(void)cgetclose(); |
return (-1); |
return -1; |
} |
} |
|
(void)fclose(pfp); |
|
pfp = NULL; |
|
*np = '\0'; |
|
break; |
} else |
} else |
line[len - 1] = '\0'; |
line[len - 1] = '\0'; |
} |
} |
} |
} |
|
if (len > sizeof(buf)) |
|
return -1; |
rp = buf; |
rp = buf; |
for(cp = nbuf; *cp != NULL; cp++) |
for (cp = nbuf; *cp != '\0'; cp++) |
if (*cp == '|' || *cp == ':') |
if (*cp == '|' || *cp == ':') |
break; |
break; |
else |
else |
Line 761 cgetnext(bp, db_array) |
|
Line 899 cgetnext(bp, db_array) |
|
if (status == -2 || status == -3) |
if (status == -2 || status == -3) |
(void)cgetclose(); |
(void)cgetclose(); |
|
|
return (status + 1); |
return status + 1; |
} |
} |
/* NOTREACHED */ |
/* NOTREACHED */ |
} |
} |
Line 776 cgetnext(bp, db_array) |
|
Line 914 cgetnext(bp, db_array) |
|
* allocation failure). |
* allocation failure). |
*/ |
*/ |
int |
int |
cgetstr(buf, cap, str) |
cgetstr(char *buf, const char *cap, char **str) |
char *buf, *cap; |
|
char **str; |
|
{ |
{ |
register u_int m_room; |
u_int m_room; |
register char *bp, *mp; |
const char *bp; |
int len; |
char *mp; |
char *mem; |
ptrdiff_t len; |
|
char *mem, *newmem; |
|
|
|
_DIAGASSERT(buf != NULL); |
|
_DIAGASSERT(cap != NULL); |
|
_DIAGASSERT(str != NULL); |
|
|
/* |
/* |
* Find string capability cap |
* Find string capability cap |
*/ |
*/ |
bp = cgetcap(buf, cap, '='); |
bp = cgetcap(buf, cap, '='); |
if (bp == NULL) |
if (bp == NULL) |
return (-1); |
return -1; |
|
|
/* |
/* |
* Conversion / storage allocation loop ... Allocate memory in |
* Conversion / storage allocation loop ... Allocate memory in |
Line 798 cgetstr(buf, cap, str) |
|
Line 939 cgetstr(buf, cap, str) |
|
*/ |
*/ |
if ((mem = malloc(SFRAG)) == NULL) { |
if ((mem = malloc(SFRAG)) == NULL) { |
errno = ENOMEM; |
errno = ENOMEM; |
return (-2); /* couldn't even allocate the first fragment */ |
return -2; /* couldn't even allocate the first fragment */ |
} |
} |
m_room = SFRAG; |
m_room = SFRAG; |
mp = mem; |
mp = mem; |
Line 820 cgetstr(buf, cap, str) |
|
Line 961 cgetstr(buf, cap, str) |
|
if (*bp == ':' || *bp == '\0') |
if (*bp == ':' || *bp == '\0') |
break; /* drop unfinished escape */ |
break; /* drop unfinished escape */ |
if ('0' <= *bp && *bp <= '7') { |
if ('0' <= *bp && *bp <= '7') { |
register int n, i; |
int n, i; |
|
|
n = 0; |
n = 0; |
i = 3; /* maximum of three octal digits */ |
i = 3; /* maximum of three octal digits */ |
Line 870 cgetstr(buf, cap, str) |
|
Line 1011 cgetstr(buf, cap, str) |
|
if (m_room == 0) { |
if (m_room == 0) { |
size_t size = mp - mem; |
size_t size = mp - mem; |
|
|
if ((mem = realloc(mem, size + SFRAG)) == NULL) |
if ((newmem = realloc(mem, size + SFRAG)) == NULL) { |
return (-2); |
free(mem); |
|
return -2; |
|
} |
|
mem = newmem; |
m_room = SFRAG; |
m_room = SFRAG; |
mp = mem + size; |
mp = mem + size; |
} |
} |
Line 883 cgetstr(buf, cap, str) |
|
Line 1027 cgetstr(buf, cap, str) |
|
/* |
/* |
* Give back any extra memory and return value and success. |
* Give back any extra memory and return value and success. |
*/ |
*/ |
if (m_room != 0) |
if (m_room != 0) { |
if ((mem = realloc(mem, (size_t)(mp - mem))) == NULL) |
if ((newmem = realloc(mem, (size_t)(mp - mem))) == NULL) { |
return (-2); |
free(mem); |
|
return -2; |
|
} |
|
mem = newmem; |
|
} |
*str = mem; |
*str = mem; |
return (len); |
_DIAGASSERT(__type_fit(int, len)); |
|
return (int)len; |
} |
} |
|
|
/* |
/* |
Line 901 cgetstr(buf, cap, str) |
|
Line 1050 cgetstr(buf, cap, str) |
|
* error was encountered (storage allocation failure). |
* error was encountered (storage allocation failure). |
*/ |
*/ |
int |
int |
cgetustr(buf, cap, str) |
cgetustr(char *buf, const char *cap, char **str) |
char *buf, *cap, **str; |
|
{ |
{ |
register u_int m_room; |
u_int m_room; |
register char *bp, *mp; |
const char *bp; |
int len; |
char *mp; |
char *mem; |
size_t len; |
|
char *mem, *newmem; |
|
|
|
_DIAGASSERT(buf != NULL); |
|
_DIAGASSERT(cap != NULL); |
|
_DIAGASSERT(str != NULL); |
|
|
/* |
/* |
* Find string capability cap |
* Find string capability cap |
*/ |
*/ |
if ((bp = cgetcap(buf, cap, '=')) == NULL) |
if ((bp = cgetcap(buf, cap, '=')) == NULL) |
return (-1); |
return -1; |
|
|
/* |
/* |
* Conversion / storage allocation loop ... Allocate memory in |
* Conversion / storage allocation loop ... Allocate memory in |
Line 921 cgetustr(buf, cap, str) |
|
Line 1074 cgetustr(buf, cap, str) |
|
*/ |
*/ |
if ((mem = malloc(SFRAG)) == NULL) { |
if ((mem = malloc(SFRAG)) == NULL) { |
errno = ENOMEM; |
errno = ENOMEM; |
return (-2); /* couldn't even allocate the first fragment */ |
return -2; /* couldn't even allocate the first fragment */ |
} |
} |
m_room = SFRAG; |
m_room = SFRAG; |
mp = mem; |
mp = mem; |
Line 943 cgetustr(buf, cap, str) |
|
Line 1096 cgetustr(buf, cap, str) |
|
if (m_room == 0) { |
if (m_room == 0) { |
size_t size = mp - mem; |
size_t size = mp - mem; |
|
|
if ((mem = realloc(mem, size + SFRAG)) == NULL) |
if ((newmem = realloc(mem, size + SFRAG)) == NULL) { |
return (-2); |
free(mem); |
|
return -2; |
|
} |
|
mem = newmem; |
m_room = SFRAG; |
m_room = SFRAG; |
mp = mem + size; |
mp = mem + size; |
} |
} |
Line 956 cgetustr(buf, cap, str) |
|
Line 1112 cgetustr(buf, cap, str) |
|
/* |
/* |
* Give back any extra memory and return value and success. |
* Give back any extra memory and return value and success. |
*/ |
*/ |
if (m_room != 0) |
if (m_room != 0) { |
if ((mem = realloc(mem, (size_t)(mp - mem))) == NULL) |
if ((newmem = realloc(mem, (size_t)(mp - mem))) == NULL) { |
return (-2); |
free(mem); |
|
return -2; |
|
} |
|
mem = newmem; |
|
} |
*str = mem; |
*str = mem; |
return (len); |
_DIAGASSERT(__type_fit(int, len)); |
|
return (int)len; |
} |
} |
|
|
/* |
/* |
Line 970 cgetustr(buf, cap, str) |
|
Line 1131 cgetustr(buf, cap, str) |
|
* numeric capability couldn't be found. |
* numeric capability couldn't be found. |
*/ |
*/ |
int |
int |
cgetnum(buf, cap, num) |
cgetnum(char *buf, const char *cap, long *num) |
char *buf, *cap; |
|
long *num; |
|
{ |
{ |
register long n; |
long n; |
register int base, digit; |
int base, digit; |
register char *bp; |
const char *bp; |
|
|
|
_DIAGASSERT(buf != NULL); |
|
_DIAGASSERT(cap != NULL); |
|
_DIAGASSERT(num != NULL); |
|
|
/* |
/* |
* Find numeric capability cap |
* Find numeric capability cap |
*/ |
*/ |
bp = cgetcap(buf, cap, '#'); |
bp = cgetcap(buf, cap, '#'); |
if (bp == NULL) |
if (bp == NULL) |
return (-1); |
return -1; |
|
|
/* |
/* |
* Look at value and determine numeric base: |
* Look at value and determine numeric base: |
Line 1026 cgetnum(buf, cap, num) |
|
Line 1189 cgetnum(buf, cap, num) |
|
* Return value and success. |
* Return value and success. |
*/ |
*/ |
*num = n; |
*num = n; |
return (0); |
return 0; |
} |
} |
|
|
|
|
Line 1034 cgetnum(buf, cap, num) |
|
Line 1197 cgetnum(buf, cap, num) |
|
* Compare name field of record. |
* Compare name field of record. |
*/ |
*/ |
static int |
static int |
nfcmp(nf, rec) |
nfcmp(char *nf, char *rec) |
char *nf, *rec; |
|
{ |
{ |
char *cp, tmp; |
char *cp, tmp; |
int ret; |
int ret; |
|
|
|
_DIAGASSERT(nf != NULL); |
|
_DIAGASSERT(rec != NULL); |
|
|
for (cp = rec; *cp != ':'; cp++) |
for (cp = rec; *cp != ':'; cp++) |
; |
continue; |
|
|
tmp = *(cp + 1); |
tmp = *(cp + 1); |
*(cp + 1) = '\0'; |
*(cp + 1) = '\0'; |
ret = strcmp(nf, rec); |
ret = strcmp(nf, rec); |
*(cp + 1) = tmp; |
*(cp + 1) = tmp; |
|
|
return (ret); |
return ret; |
} |
} |