#include <config.h>
#include "roken.h"
#include <sys/types.h>
#include <ctype.h>
#if defined(HAVE_DB_185_H)
#include <db_185.h>
#elif defined(HAVE_DB_H)
#include <db.h>
#endif
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define BFRAG 1024
#if 0
#define BSIZE 1024
#endif
#define ESC ('[' & 037)
#define MAX_RECURSION 32
#define SFRAG 100
#define RECOK (char)0
#define TCERR (char)1
#define SHADOW (char)2
static size_t topreclen;
static char *toprec;
static int gottoprec;
#if 0
#if defined(HAVE_DBOPEN) && defined(HAVE_DB_H)
#define USE_DB
#endif
#endif
#ifdef USE_DB
static int cdbget (DB *, char **, const char *);
#endif
static int getent (char **, size_t *, char **, int, const char *, int, char *);
static int nfcmp (char *, char *);
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL cgetset(const char *ent);
ROKEN_LIB_FUNCTION char * ROKEN_LIB_CALL cgetcap(char *buf, const char *cap, int type);
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL cgetent(char **buf, char **db_array, const char *name);
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL cgetmatch(const char *buf, const char *name);
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL cgetclose(void);
#if 0
int cgetfirst(char **buf, char **db_array);
int cgetnext(char **bp, char **db_array);
#endif
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL cgetstr(char *buf, const char *cap, char **str);
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL cgetustr(char *buf, const char *cap, char **str);
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL cgetnum(char *buf, const char *cap, long *num);
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
cgetset(const char *ent)
{
const char *source, *check;
char *dest;
if (ent == NULL) {
if (toprec)
free(toprec);
toprec = NULL;
topreclen = 0;
return (0);
}
topreclen = strlen(ent);
if ((toprec = malloc (topreclen + 1)) == NULL) {
errno = ENOMEM;
return (-1);
}
gottoprec = 0;
source=ent;
dest=toprec;
while (*source) {
*dest++ = *source++;
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);
}
ROKEN_LIB_FUNCTION char * ROKEN_LIB_CALL
cgetcap(char *buf, const char *cap, int type)
{
char *bp;
const char *cp;
bp = buf;
for (;;) {
for (;;)
if (*bp == '\0')
return (NULL);
else
if (*bp++ == ':')
break;
for (cp = cap; *cp == *bp && *bp != '\0'; cp++, bp++)
continue;
if (*cp != '\0')
continue;
if (*bp == '@')
return (NULL);
if (type == ':') {
if (*bp != '\0' && *bp != ':')
continue;
return(bp);
}
if (*bp != type)
continue;
bp++;
return (*bp == '@' ? NULL : bp);
}
}
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
cgetent(char **buf, char **db_array, const char *name)
{
size_t dummy;
return (getent(buf, &dummy, db_array, -1, name, 0, NULL));
}
static int
getent(char **cap, size_t *len, char **db_array, int fd,
const char *name, int depth, char *nfield)
{
char *r_end, *rp = NULL, **db_p;
int myfd = 0, eof, foundit;
char *record;
int tc_not_resolved;
if (depth > MAX_RECURSION)
return (-3);
if (depth == 0 && toprec != NULL && cgetmatch(toprec, name) == 0) {
size_t len = topreclen + BFRAG;
if ((record = malloc (len)) == NULL) {
errno = ENOMEM;
return (-2);
}
(void)strlcpy(record, toprec, len);
db_p = db_array;
rp = record + topreclen + 1;
r_end = rp + BFRAG;
goto tc_exp;
}
if ((record = malloc(BFRAG)) == NULL) {
errno = ENOMEM;
return (-2);
}
r_end = record + BFRAG;
foundit = 0;
for (db_p = db_array; *db_p != NULL; db_p++) {
eof = 0;
if (fd >= 0) {
(void)lseek(fd, (off_t)0, SEEK_SET);
} else {
#ifdef USE_DB
char pbuf[_POSIX_PATH_MAX];
char *cbuf;
size_t clen;
int retval;
DB *capdbp;
(void)snprintf(pbuf, sizeof(pbuf), "%s.db", *db_p);
if ((capdbp = dbopen(pbuf, O_RDONLY, 0, DB_HASH, 0))
!= NULL) {
free(record);
retval = cdbget(capdbp, &record, name);
if (retval < 0) {
(void)capdbp->close(capdbp);
return (retval);
}
clen = strlen(record);
cbuf = malloc(clen + 1);
if (cbuf == NULL)
return (-2);
memmove(cbuf, record, clen + 1);
if (capdbp->close(capdbp) < 0) {
free(cbuf);
return (-2);
}
*len = clen;
*cap = cbuf;
return (retval);
} else
#endif
{
fd = open(*db_p, O_RDONLY, 0);
if (fd < 0) {
continue;
}
myfd = 1;
}
}
{
char buf[BUFSIZ];
char *b_end, *bp, *cp;
int c, slash;
b_end = buf;
bp = buf;
cp = 0;
slash = 0;
for (;;) {
rp = record;
for (;;) {
if (bp >= b_end) {
int n;
n = read(fd, buf, sizeof(buf));
if (n <= 0) {
if (myfd)
(void)close(fd);
if (n < 0) {
free(record);
return (-2);
} else {
fd = -1;
eof = 1;
break;
}
}
b_end = buf+n;
bp = buf;
}
c = *bp++;
if (c == '\n') {
if (slash) {
slash = 0;
rp--;
continue;
} else
break;
}
if (slash) {
slash = 0;
cp = 0;
}
if (c == ':') {
if (cp)
rp = cp;
else
cp = rp;
} else if (c == '\\') {
slash = 1;
} else if (c != ' ' && c != '\t') {
cp = 0;
}
*rp++ = c;
if (rp >= r_end) {
u_int pos;
size_t newsize;
pos = rp - record;
newsize = r_end - record + BFRAG;
record = realloc(record, newsize);
if (record == NULL) {
errno = ENOMEM;
if (myfd)
(void)close(fd);
return (-2);
}
r_end = record + newsize;
rp = record + pos;
}
}
if (cp)
rp = cp + 1;
*rp++ = '\0';
if (eof)
break;
if (*record == '\0' || *record == '#')
continue;
if (cgetmatch(record, name) == 0) {
if (nfield == NULL || !nfcmp(nfield, record)) {
foundit = 1;
break;
}
}
}
}
if (foundit)
break;
}
if (!foundit)
return (-1);
tc_exp: {
char *newicap, *s;
size_t ilen, newilen;
int diff, iret, tclen;
char *icap, *scan, *tc, *tcstart, *tcend;
scan = record;
tc_not_resolved = 0;
for (;;) {
if ((tc = cgetcap(scan, "tc", '=')) == NULL)
break;
s = tc;
for (;;)
if (*s == '\0')
break;
else
if (*s++ == ':') {
*(s - 1) = '\0';
break;
}
tcstart = tc - 3;
tclen = s - tcstart;
tcend = s;
iret = getent(&icap, &ilen, db_p, fd, tc, depth+1,
NULL);
newicap = icap;
newilen = ilen;
if (iret != 0) {
if (iret < -1) {
if (myfd)
(void)close(fd);
free(record);
return (iret);
}
if (iret == 1)
tc_not_resolved = 1;
if (iret == -1) {
*(s - 1) = ':';
scan = s - 1;
tc_not_resolved = 1;
continue;
}
}
s = newicap;
for (;;)
if (*s == '\0')
break;
else
if (*s++ == ':')
break;
newilen -= s - newicap;
newicap = s;
s += newilen;
if (*(s-1) != ':') {
*s = ':';
newilen++;
}
diff = newilen - tclen;
if (diff >= r_end - rp) {
u_int pos, tcpos, tcposend;
size_t newsize;
pos = rp - record;
newsize = r_end - record + diff + BFRAG;
tcpos = tcstart - record;
tcposend = tcend - record;
record = realloc(record, newsize);
if (record == NULL) {
errno = ENOMEM;
if (myfd)
(void)close(fd);
free(icap);
return (-2);
}
r_end = record + newsize;
rp = record + pos;
tcstart = record + tcpos;
tcend = record + tcposend;
}
s = tcstart + newilen;
memmove(s, tcend, (size_t)(rp - tcend));
memmove(tcstart, newicap, newilen);
rp += diff;
free(icap);
scan = s-1;
}
}
if (myfd)
(void)close(fd);
*len = rp - record - 1;
if (r_end > rp)
if ((record =
realloc(record, (size_t)(rp - record))) == NULL) {
errno = ENOMEM;
return (-2);
}
*cap = record;
if (tc_not_resolved)
return (1);
return (0);
}
#ifdef USE_DB
static int
cdbget(DB *capdbp, char **bp, const char *name)
{
DBT key;
DBT data;
key.data = (char *)name;
key.size = strlen(name);
for (;;) {
switch(capdbp->get(capdbp, &key, &data, 0)) {
case -1:
return (-2);
case 1:
return (-1);
}
if (((char *)data.data)[0] != SHADOW)
break;
key.data = (char *)data.data + 1;
key.size = data.size - 1;
}
*bp = (char *)data.data + 1;
return (((char *)(data.data))[0] == TCERR ? 1 : 0);
}
#endif
int
cgetmatch(const char *buf, const char *name)
{
const char *np, *bp;
bp = buf;
for (;;) {
np = name;
for (;;)
if (*np == '\0') {
if (*bp == '|' || *bp == ':' || *bp == '\0')
return (0);
else
break;
} else
if (*bp++ != *np++)
break;
bp--;
for (;;)
if (*bp == '\0' || *bp == ':')
return (-1);
else
if (*bp++ == '|')
break;
}
}
#if 0
int
cgetfirst(char **buf, char **db_array)
{
(void)cgetclose();
return (cgetnext(buf, db_array));
}
#endif
static FILE *pfp;
static int slash;
static char **dbp;
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
cgetclose(void)
{
if (pfp != NULL) {
(void)fclose(pfp);
pfp = NULL;
}
dbp = NULL;
gottoprec = 0;
slash = 0;
return(0);
}
#if 0
int
cgetnext(char **bp, char **db_array)
{
size_t len;
int status, done;
char *cp, *line, *rp, *np, buf[BSIZE], nbuf[BSIZE];
size_t dummy;
if (dbp == NULL)
dbp = db_array;
if (pfp == NULL && (pfp = fopen(*dbp, "r")) == NULL) {
(void)cgetclose();
return (-1);
}
for(;;) {
if (toprec && !gottoprec) {
gottoprec = 1;
line = toprec;
} else {
line = fgetln(pfp, &len);
if (line == NULL && pfp) {
if (ferror(pfp)) {
(void)cgetclose();
return (-1);
} else {
(void)fclose(pfp);
pfp = NULL;
if (*++dbp == NULL) {
(void)cgetclose();
return (0);
} else if ((pfp =
fopen(*dbp, "r")) == NULL) {
(void)cgetclose();
return (-1);
} else
continue;
}
} else
line[len - 1] = '\0';
if (len == 1) {
slash = 0;
continue;
}
if (isspace((unsigned char)*line) ||
*line == ':' || *line == '#' || slash) {
if (line[len - 2] == '\\')
slash = 1;
else
slash = 0;
continue;
}
if (line[len - 2] == '\\')
slash = 1;
else
slash = 0;
}
done = 0;
np = nbuf;
for (;;) {
for (cp = line; *cp != '\0'; cp++) {
if (*cp == ':') {
*np++ = ':';
done = 1;
break;
}
if (*cp == '\\')
break;
*np++ = *cp;
}
if (done) {
*np = '\0';
break;
} else {
line = fgetln(pfp, &len);
if (line == NULL && pfp) {
if (ferror(pfp)) {
(void)cgetclose();
return (-1);
}
(void)fclose(pfp);
pfp = NULL;
*np = '\0';
break;
} else
line[len - 1] = '\0';
}
}
rp = buf;
for(cp = nbuf; *cp != '\0'; cp++)
if (*cp == '|' || *cp == ':')
break;
else
*rp++ = *cp;
*rp = '\0';
status = getent(bp, &dummy, db_array, -1, buf, 0, NULL);
if (status == -2 || status == -3)
(void)cgetclose();
return (status + 1);
}
}
#endif
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
cgetstr(char *buf, const char *cap, char **str)
{
u_int m_room;
const char *bp;
char *mp;
int len;
char *mem, *nmem;
*str = NULL;
bp = cgetcap(buf, cap, '=');
if (bp == NULL)
return (-1);
if ((mem = malloc(SFRAG)) == NULL) {
errno = ENOMEM;
return (-2);
}
m_room = SFRAG;
mp = mem;
while (*bp != ':' && *bp != '\0') {
if (*bp == '^') {
bp++;
if (*bp == ':' || *bp == '\0')
break;
*mp++ = *bp++ & 037;
} else if (*bp == '\\') {
bp++;
if (*bp == ':' || *bp == '\0')
break;
if ('0' <= *bp && *bp <= '7') {
int n, i;
n = 0;
i = 3;
do {
n = n * 8 + (*bp++ - '0');
} while (--i && '0' <= *bp && *bp <= '7');
*mp++ = n;
}
else switch (*bp++) {
case 'b': case 'B':
*mp++ = '\b';
break;
case 't': case 'T':
*mp++ = '\t';
break;
case 'n': case 'N':
*mp++ = '\n';
break;
case 'f': case 'F':
*mp++ = '\f';
break;
case 'r': case 'R':
*mp++ = '\r';
break;
case 'e': case 'E':
*mp++ = ESC;
break;
case 'c': case 'C':
*mp++ = ':';
break;
default:
*mp++ = *(bp-1);
break;
}
} else
*mp++ = *bp++;
m_room--;
if (m_room == 0) {
size_t size = mp - mem;
if ((nmem = realloc(mem, size + SFRAG)) == NULL) {
free(mem);
return (-2);
}
mem = nmem;
m_room = SFRAG;
mp = mem + size;
}
}
*mp++ = '\0';
m_room--;
len = mp - mem - 1;
if (m_room != 0) {
if ((nmem = realloc(mem, (size_t)(mp - mem))) == NULL) {
free(mem);
return (-2);
}
mem = nmem;
}
*str = mem;
return (len);
}
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
cgetustr(char *buf, const char *cap, char **str)
{
u_int m_room;
const char *bp;
char *mp;
int len;
char *mem;
if ((bp = cgetcap(buf, cap, '=')) == NULL)
return (-1);
if ((mem = malloc(SFRAG)) == NULL) {
errno = ENOMEM;
return (-2);
}
m_room = SFRAG;
mp = mem;
while (*bp != ':' && *bp != '\0') {
*mp++ = *bp++;
m_room--;
if (m_room == 0) {
size_t size = mp - mem;
if ((mem = realloc(mem, size + SFRAG)) == NULL)
return (-2);
m_room = SFRAG;
mp = mem + size;
}
}
*mp++ = '\0';
m_room--;
len = mp - mem - 1;
if (m_room != 0)
if ((mem = realloc(mem, (size_t)(mp - mem))) == NULL)
return (-2);
*str = mem;
return (len);
}
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
cgetnum(char *buf, const char *cap, long *num)
{
long n;
int base, digit;
const char *bp;
bp = cgetcap(buf, cap, '#');
if (bp == NULL)
return (-1);
if (*bp == '0') {
bp++;
if (*bp == 'x' || *bp == 'X') {
bp++;
base = 16;
} else
base = 8;
} else
base = 10;
n = 0;
for (;;) {
if ('0' <= *bp && *bp <= '9')
digit = *bp - '0';
else if ('a' <= *bp && *bp <= 'f')
digit = 10 + *bp - 'a';
else if ('A' <= *bp && *bp <= 'F')
digit = 10 + *bp - 'A';
else
break;
if (digit >= base)
break;
n = n * base + digit;
bp++;
}
*num = n;
return (0);
}
static int
nfcmp(char *nf, char *rec)
{
char *cp, tmp;
int ret;
for (cp = rec; *cp != ':'; cp++)
;
tmp = *(cp + 1);
*(cp + 1) = '\0';
ret = strcmp(nf, rec);
*(cp + 1) = tmp;
return (ret);
}