#include <sys/cdefs.h>
#include <stdbool.h>
#include "dhcpd.h"
#include "dhctoken.h"
void
skip_to_semi(FILE *cfile)
{
int brace_count = 0, token;
char *val;
do {
token = peek_token(&val, cfile);
if (token == RBRACE) {
if (brace_count) {
token = next_token(&val, cfile);
if (!--brace_count)
return;
} else
return;
} else if (token == LBRACE) {
brace_count++;
} else if (token == SEMI && !brace_count) {
token = next_token(&val, cfile);
return;
} else if (token == '\n') {
token = next_token(&val, cfile);
return;
}
token = next_token(&val, cfile);
} while (token != EOF);
}
int
parse_semi(FILE *cfile)
{
int token;
char *val;
token = next_token(&val, cfile);
if (token != SEMI) {
parse_warn("semicolon expected.");
skip_to_semi(cfile);
return (0);
}
return (1);
}
char *
parse_string(FILE *cfile)
{
char *val, *s;
size_t valsize;
int token;
token = next_token(&val, cfile);
if (token != STRING) {
parse_warn("filename must be a string");
skip_to_semi(cfile);
return (NULL);
}
valsize = strlen(val) + 1;
s = malloc(valsize);
if (!s)
error("no memory for string %s.", val);
memcpy(s, val, valsize);
if (!parse_semi(cfile)) {
free(s);
return (NULL);
}
return (s);
}
int
parse_ip_addr(FILE *cfile, struct iaddr *addr)
{
addr->len = 4;
if (parse_numeric_aggregate(cfile, addr->iabuf,
&addr->len, DOT, 10, 8))
return (1);
return (0);
}
void
parse_hardware_param(FILE *cfile, struct hardware *hardware)
{
unsigned char *t;
int token;
size_t hlen;
char *val;
token = next_token(&val, cfile);
switch (token) {
case ETHERNET:
hardware->htype = HTYPE_ETHER;
break;
case TOKEN_RING:
hardware->htype = HTYPE_IEEE802;
break;
case FDDI:
hardware->htype = HTYPE_FDDI;
break;
default:
parse_warn("expecting a network hardware type");
skip_to_semi(cfile);
return;
}
hlen = 0;
t = parse_numeric_aggregate(cfile, NULL, &hlen, COLON, 16, 8);
if (!t)
return;
if (hlen > sizeof(hardware->haddr)) {
free(t);
parse_warn("hardware address too long");
} else {
hardware->hlen = hlen;
memcpy((unsigned char *)&hardware->haddr[0], t,
hardware->hlen);
if (hlen < sizeof(hardware->haddr))
memset(&hardware->haddr[hlen], 0,
sizeof(hardware->haddr) - hlen);
free(t);
}
token = next_token(&val, cfile);
if (token != SEMI) {
parse_warn("expecting semicolon.");
skip_to_semi(cfile);
}
}
void
parse_lease_time(FILE *cfile, time_t *timep)
{
char *val;
int token;
token = next_token(&val, cfile);
if (token != NUMBER) {
parse_warn("Expecting numeric lease time");
skip_to_semi(cfile);
return;
}
convert_num((unsigned char *)timep, val, 10, 32);
*timep = ntohl(*timep);
parse_semi(cfile);
}
unsigned char *
parse_numeric_aggregate(FILE *cfile, unsigned char *buf, size_t *max,
int separator, unsigned base, int size)
{
unsigned char *bufp = buf, *s = NULL;
int token;
char *val, *t;
size_t valsize, count = 0;
pair c = NULL;
unsigned char *lbufp = NULL;
if (!bufp && *max) {
lbufp = bufp = malloc(*max * size / 8);
if (!bufp)
error("can't allocate space for numeric aggregate");
} else
s = bufp;
do {
if (count) {
token = peek_token(&val, cfile);
if (token != separator) {
if (!*max)
break;
if (token != RBRACE && token != LBRACE)
token = next_token(&val, cfile);
parse_warn("too few numbers.");
if (token != SEMI)
skip_to_semi(cfile);
free(lbufp);
return (NULL);
}
token = next_token(&val, cfile);
}
token = next_token(&val, cfile);
if (token == EOF) {
parse_warn("unexpected end of file");
break;
}
if (token != NUMBER &&
(base != 16 || token != NUMBER_OR_NAME)) {
parse_warn("expecting numeric value.");
skip_to_semi(cfile);
free(lbufp);
return (NULL);
}
if (s) {
convert_num(s, val, base, size);
s += size / 8;
} else {
valsize = strlen(val) + 1;
t = malloc(valsize);
if (!t)
error("no temp space for number.");
memcpy(t, val, valsize);
c = cons(t, c);
}
} while (++count != *max);
if (c) {
free(lbufp);
bufp = malloc(count * size / 8);
if (!bufp)
error("can't allocate space for numeric aggregate.");
s = bufp + count - size / 8;
*max = count;
}
while (c) {
pair cdr = c->cdr;
convert_num(s, (char *)c->car, base, size);
s -= size / 8;
free(c->car);
free(c);
c = cdr;
}
return (bufp);
}
void
convert_num(unsigned char *buf, char *str, unsigned base, int size)
{
bool negative = false;
unsigned tval, max;
u_int32_t val = 0;
char *ptr = str;
if (*ptr == '-') {
negative = true;
ptr++;
}
if (!base) {
if (ptr[0] == '0') {
if (ptr[1] == 'x') {
base = 16;
ptr += 2;
} else if (isascii(ptr[1]) && isdigit(ptr[1])) {
base = 8;
ptr += 1;
} else
base = 10;
} else
base = 10;
}
do {
tval = *ptr++;
if (tval >= 'a')
tval = tval - 'a' + 10;
else if (tval >= 'A')
tval = tval - 'A' + 10;
else if (tval >= '0')
tval -= '0';
else {
warning("Bogus number: %s.", str);
break;
}
if (tval >= base) {
warning("Bogus number: %s: digit %d not in base %d",
str, tval, base);
break;
}
val = val * base + tval;
} while (*ptr);
if (negative)
max = (1 << (size - 1));
else
max = (1 << (size - 1)) + ((1 << (size - 1)) - 1);
if (val > max) {
switch (base) {
case 8:
warning("value %s%o exceeds max (%d) for precision.",
negative ? "-" : "", val, max);
break;
case 16:
warning("value %s%x exceeds max (%d) for precision.",
negative ? "-" : "", val, max);
break;
default:
warning("value %s%u exceeds max (%d) for precision.",
negative ? "-" : "", val, max);
break;
}
}
if (negative)
switch (size) {
case 8:
*buf = -(unsigned long)val;
break;
case 16:
putShort(buf, -(unsigned long)val);
break;
case 32:
putLong(buf, -(unsigned long)val);
break;
default:
warning("Unexpected integer size: %d", size);
break;
}
else
switch (size) {
case 8:
*buf = (u_int8_t)val;
break;
case 16:
putUShort(buf, (u_int16_t)val);
break;
case 32:
putULong(buf, val);
break;
default:
warning("Unexpected integer size: %d", size);
break;
}
}
time_t
parse_date(FILE *cfile)
{
int token;
struct tm tm;
char *val;
token = next_token(&val, cfile);
if (token != NUMBER) {
parse_warn("numeric day of week expected.");
if (token != SEMI)
skip_to_semi(cfile);
return (0);
}
tm.tm_wday = atoi(val);
token = next_token(&val, cfile);
if (token != NUMBER) {
parse_warn("numeric year expected.");
if (token != SEMI)
skip_to_semi(cfile);
return (0);
}
tm.tm_year = atoi(val);
if (tm.tm_year > 1900)
tm.tm_year -= 1900;
token = next_token(&val, cfile);
if (token != SLASH) {
parse_warn("expected slash separating year from month.");
if (token != SEMI)
skip_to_semi(cfile);
return (0);
}
token = next_token(&val, cfile);
if (token != NUMBER) {
parse_warn("numeric month expected.");
if (token != SEMI)
skip_to_semi(cfile);
return (0);
}
tm.tm_mon = atoi(val) - 1;
token = next_token(&val, cfile);
if (token != SLASH) {
parse_warn("expected slash separating month from day.");
if (token != SEMI)
skip_to_semi(cfile);
return (0);
}
token = next_token(&val, cfile);
if (token != NUMBER) {
parse_warn("numeric day of month expected.");
if (token != SEMI)
skip_to_semi(cfile);
return (0);
}
tm.tm_mday = atoi(val);
token = next_token(&val, cfile);
if (token != NUMBER) {
parse_warn("numeric hour expected.");
if (token != SEMI)
skip_to_semi(cfile);
return (0);
}
tm.tm_hour = atoi(val);
token = next_token(&val, cfile);
if (token != COLON) {
parse_warn("expected colon separating hour from minute.");
if (token != SEMI)
skip_to_semi(cfile);
return (0);
}
token = next_token(&val, cfile);
if (token != NUMBER) {
parse_warn("numeric minute expected.");
if (token != SEMI)
skip_to_semi(cfile);
return (0);
}
tm.tm_min = atoi(val);
token = next_token(&val, cfile);
if (token != COLON) {
parse_warn("expected colon separating hour from minute.");
if (token != SEMI)
skip_to_semi(cfile);
return (0);
}
token = next_token(&val, cfile);
if (token != NUMBER) {
parse_warn("numeric minute expected.");
if (token != SEMI)
skip_to_semi(cfile);
return (0);
}
tm.tm_sec = atoi(val);
tm.tm_isdst = 0;
tm.tm_yday = 0;
token = next_token(&val, cfile);
if (token != SEMI) {
parse_warn("semicolon expected.");
skip_to_semi(cfile);
return (0);
}
return (timegm(&tm));
}