* the first porting of oao' scdns to busybox also.
*/
-#include "busybox.h"
-
-static char *fileconf = "/etc/dnsd.conf";
-#define LOCK_FILE "/var/run/dnsd.lock"
-#define LOG_FILE "/var/log/dnsd.log"
-
-// Must matct getopt32 call
-#define OPT_daemon (option_mask32 & 0x10)
-#define OPT_verbose (option_mask32 & 0x20)
+#include "libbb.h"
+#include <syslog.h>
//#define DEBUG 1
+#define DEBUG 0
enum {
MAX_HOST_LEN = 16, // longest host name allowed is 15
ttl(4B) + rlen(2B) + r (MAX_NAME_LEN =21B) +
2*querystring (2 MAX_NAME_LEN= 42B), all together 90 Byte
*/
- MAX_PACK_LEN = 512 + 1,
+ MAX_PACK_LEN = 512,
DEFAULT_TTL = 30, // increase this when not testing?
REQ_PTR = 12
};
-struct dns_repl { // resource record, add 0 or 1 to accepted dns_msg in resp
- uint16_t rlen;
- uint8_t *r; // resource
- uint16_t flags;
-};
-
struct dns_head { // the message from client and first part of response mag
uint16_t id;
uint16_t flags;
char name[MAX_HOST_LEN];
};
-static struct dns_entry *dnsentry = NULL;
-// FIXME! unused! :(
-static int daemonmode;
+static struct dns_entry *dnsentry;
static uint32_t ttl = DEFAULT_TTL;
+static const char *fileconf = "/etc/dnsd.conf";
+
+// Must match getopt32 call
+#define OPT_daemon (option_mask32 & 0x10)
+#define OPT_verbose (option_mask32 & 0x20)
+
+
/*
* Convert host name from C-string to dns length/string.
*/
}
}
-/*
- * Append message to log file
- */
-static void log_message(char *filename, char *message)
-{
- FILE *logfile;
- if (!daemonmode)
- return;
- logfile = fopen(filename, "a");
- if (!logfile)
- return;
- fprintf(logfile, "%s\n", message);
- fclose(logfile);
-}
-
/*
* Read one line of hostname/IP from file
* Returns 0 for each valid entry read, -1 at EOF
* Assumes all host names are lower case only
- * Hostnames with more than one label is not handled correctly.
+ * Hostnames with more than one label are not handled correctly.
* Presently the dot is copied into name without
* converting to a length/string substring for that label.
*/
-
static int getfileentry(FILE * fp, struct dns_entry *s)
{
unsigned int a,b,c,d;
- char *r, *name;
+ char *line, *r, *name;
restart:
- r = xmalloc_fgets(fp);
+ line = r = xmalloc_fgets(fp);
if (!r)
return -1;
while (*r == ' ' || *r == '\t') {
r++;
- if (!*r || *r == '#' || *r == '\n')
+ if (!*r || *r == '#' || *r == '\n') {
+ free(line);
goto restart; /* skipping empty/blank and commented lines */
+ }
}
name = r;
while (*r != ' ' && *r != '\t')
r++;
- *r++ = 0;
- if (sscanf(r, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
+ *r++ = '\0';
+ if (sscanf(r, ".%u.%u.%u.%u"+1, &a, &b, &c, &d) != 4) {
+ free(line);
goto restart; /* skipping wrong lines */
+ }
- sprintf(s->ip, "%u.%u.%u.%u", a, b, c, d);
+ sprintf(s->ip, ".%u.%u.%u.%u"+1, a, b, c, d);
sprintf(s->rip, ".%u.%u.%u.%u", d, c, b, a);
undot((uint8_t*)s->rip);
- convname(s->name,(uint8_t*)name);
+ convname(s->name, (uint8_t*)name);
if (OPT_verbose)
fprintf(stderr, "\tname:%s, ip:%s\n", &(s->name[1]),s->ip);
+ free(line);
return 0;
}
{
FILE *fp;
struct dns_entry *m, *prev;
- prev = dnsentry = NULL;
+ prev = dnsentry = NULL;
fp = xfopen(fileconf, "r");
while (1) {
- m = xmalloc(sizeof(struct dns_entry));
-
- m->next = NULL;
+ m = xzalloc(sizeof(*m));
+ /*m->next = NULL;*/
if (getfileentry(fp, m))
break;
fclose(fp);
}
-
-/*
- * Set up UDP socket
- */
-static int listen_socket(char *iface_addr, int listen_port)
-{
- struct sockaddr_in a;
- char msg[100];
- int sck;
- sck = xsocket(PF_INET, SOCK_DGRAM, 0);
- if (setsockopt_reuseaddr(sck) < 0)
- bb_perror_msg_and_die("setsockopt() failed");
- memset(&a, 0, sizeof(a));
- a.sin_port = htons(listen_port);
- a.sin_family = AF_INET;
- if (!inet_aton(iface_addr, &a.sin_addr))
- bb_perror_msg_and_die("bad iface address");
- xbind(sck, (struct sockaddr *)&a, sizeof(a));
- xlisten(sck, 50);
- sprintf(msg, "accepting UDP packets on addr:port %s:%d\n",
- iface_addr, (int)listen_port);
- log_message(LOG_FILE, msg);
- return sck;
-}
-
/*
* Look query up in dns records and return answer if found
* qs is the query string, first byte the string length
static int table_lookup(uint16_t type, uint8_t * as, uint8_t * qs)
{
int i;
- struct dns_entry *d=dnsentry;
+ struct dns_entry *d = dnsentry;
do {
-#ifdef DEBUG
+#if DEBUG
char *p,*q;
q = (char *)&(qs[1]);
p = &(d->name[1]);
fprintf(stderr, "\n%s: %d/%d p:%s q:%s %d",
- __FUNCTION__, strlen(p), (int)(d->name[0]), p, q, strlen(q));
+ __FUNCTION__, (int)strlen(p), (int)(d->name[0]),
+ p, q, (int)strlen(q));
#endif
if (type == REQ_A) { /* search by host name */
for (i = 1; i <= (int)(d->name[0]); i++)
if (tolower(qs[i]) != d->name[i])
break;
if (i > (int)(d->name[0])) {
-#ifdef DEBUG
- fprintf(stderr, " OK");
-#endif
strcpy((char *)as, d->ip);
-#ifdef DEBUG
- fprintf(stderr, " as:%s\n", as);
+#if DEBUG
+ fprintf(stderr, " OK as:%s\n", as);
#endif
- return 0;
+ return 0;
}
- } else
- if (type == REQ_PTR) { /* search by IP-address */
+ } else if (type == REQ_PTR) { /* search by IP-address */
if (!strncmp((char*)&d->rip[1], (char*)&qs[1], strlen(d->rip)-1)) {
strcpy((char *)as, d->name);
return 0;
return -1;
}
-
/*
* Decode message and generate answer
*/
-#define eret(s) do { fputs(s, stderr); return -1; } while (0)
-static int process_packet(uint8_t * buf)
+static int process_packet(uint8_t *buf)
{
+ uint8_t answstr[MAX_NAME_LEN + 1];
struct dns_head *head;
struct dns_prop *qprop;
- struct dns_repl outr;
- void *next, *from, *answb;
-
- uint8_t answstr[MAX_NAME_LEN + 1];
- int lookup_result, type, len, packet_len;
+ uint8_t *from, *answb;
+ uint16_t outr_rlen;
+ uint16_t outr_flags;
uint16_t flags;
+ int lookup_result, type, packet_len;
+ int querystr_len;
answstr[0] = '\0';
head = (struct dns_head *)buf;
- if (head->nquer == 0)
- eret("no queries\n");
+ if (head->nquer == 0) {
+ bb_error_msg("no queries");
+ return -1;
+ }
- if (head->flags & 0x8000)
- eret("ignoring response packet\n");
+ if (head->flags & 0x8000) {
+ bb_error_msg("ignoring response packet");
+ return -1;
+ }
from = (void *)&head[1]; // start of query string
- next = answb = from + strlen((char *)from) + 1 + sizeof(struct dns_prop); // where to append answer block
+//FIXME: strlen of untrusted data??!
+ querystr_len = strlen((char *)from) + 1 + sizeof(struct dns_prop);
+ answb = from + querystr_len; // where to append answer block
- outr.rlen = 0; // may change later
- outr.r = NULL;
- outr.flags = 0;
+ outr_rlen = 0;
+ outr_flags = 0;
qprop = (struct dns_prop *)(answb - 4);
type = ntohs(qprop->type);
}
if (ntohs(qprop->class) != 1 /* class INET */ ) {
- outr.flags = 4; /* not supported */
+ outr_flags = 4; /* not supported */
goto empty_packet;
}
/* we only support standard queries */
goto empty_packet;
// We have a standard query
- log_message(LOG_FILE, (char *)from);
- lookup_result = table_lookup(type, answstr, (uint8_t*)from);
+ bb_info_msg("%s", (char *)from);
+ lookup_result = table_lookup(type, answstr, from);
if (lookup_result != 0) {
- outr.flags = 3 | 0x0400; //name do not exist and auth
+ outr_flags = 3 | 0x0400; // name do not exist and auth
goto empty_packet;
}
if (type == REQ_A) { // return an address
- struct in_addr a;
- if (!inet_aton((char*)answstr, &a)) {//dotted dec to long conv
- outr.flags = 1; /* Frmt err */
+ struct in_addr a; // NB! its "struct { unsigned __long__ s_addr; }"
+ uint32_t v32;
+ if (!inet_aton((char*)answstr, &a)) { //dotted dec to long conv
+ outr_flags = 1; /* Frmt err */
goto empty_packet;
}
- memcpy(answstr, &a.s_addr, 4); // save before a disappears
- outr.rlen = 4; // uint32_t IP
- }
- else
- outr.rlen = strlen((char *)answstr) + 1; // a host name
- outr.r = answstr; // 32 bit ip or a host name
- outr.flags |= 0x0400; /* authority-bit */
+ v32 = a.s_addr; /* in case long != int */
+ memcpy(answstr, &v32, 4);
+ outr_rlen = 4; // uint32_t IP
+ } else
+ outr_rlen = strlen((char *)answstr) + 1; // a host name
+ outr_flags |= 0x0400; /* authority-bit */
// we have an answer
head->nansw = htons(1);
// copy query block to answer block
- len = answb - from;
- memcpy(answb, from, len);
- next += len;
+ memcpy(answb, from, querystr_len);
+ answb += querystr_len;
// and append answer rr
- *(uint32_t *) next = htonl(ttl);
- next += 4;
- *(uint16_t *) next = htons(outr.rlen);
- next += 2;
- memcpy(next, (void *)answstr, outr.rlen);
- next += outr.rlen;
+// FIXME: unaligned accesses??
+ *(uint32_t *) answb = htonl(ttl);
+ answb += 4;
+ *(uint16_t *) answb = htons(outr_rlen);
+ answb += 2;
+ memcpy(answb, answstr, outr_rlen);
+ answb += outr_rlen;
empty_packet:
flags = ntohs(head->flags);
// clear rcode and RA, set responsebit and our new flags
- flags |= (outr.flags & 0xff80) | 0x8000;
+ flags |= (outr_flags & 0xff80) | 0x8000;
head->flags = htons(flags);
- head->nauth = head->nadd = htons(0);
+ head->nauth = head->nadd = 0;
head->nquer = htons(1);
- packet_len = next - (void *)buf;
+ packet_len = answb - buf;
return packet_len;
}
/*
* Exit on signal
*/
-static void interrupt(int x)
+static void interrupt(int sig)
{
- unlink(LOCK_FILE);
- write(2, "interrupt exiting\n", 18);
- exit(2);
+ /* unlink("/var/run/dnsd.lock"); */
+ bb_error_msg("interrupt, exiting\n");
+ kill_myself_with_sig(sig);
}
-int dnsd_main(int argc, char **argv)
+int dnsd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int dnsd_main(int argc ATTRIBUTE_UNUSED, char **argv)
{
+ const char *listen_interface = "0.0.0.0";
+ char *sttl, *sport;
+ len_and_sockaddr *lsa, *from, *to;
+ unsigned lsa_size;
int udps;
uint16_t port = 53;
- uint8_t buf[MAX_PACK_LEN];
- char *listen_interface = "0.0.0.0";
- char *sttl, *sport;
+ /* Paranoid sizing: querystring x2 + ttl + outr_rlen + answstr */
+ /* I'd rather see process_packet() fixed instead... */
+ uint8_t buf[MAX_PACK_LEN * 2 + 4 + 2 + (MAX_NAME_LEN+1)];
- getopt32(argc, argv, "i:c:t:p:dv", &listen_interface, &fileconf, &sttl, &sport);
+ getopt32(argv, "i:c:t:p:dv", &listen_interface, &fileconf, &sttl, &sport);
//if (option_mask32 & 0x1) // -i
//if (option_mask32 & 0x2) // -c
if (option_mask32 & 0x4) // -t
- if (!(ttl = atol(sttl)))
- bb_show_usage();
+ ttl = xatou_range(sttl, 1, 0xffffffff);
if (option_mask32 & 0x8) // -p
- if (!(port = atol(sport)))
- bb_show_usage();
+ port = xatou_range(sport, 1, 0xffff);
if (OPT_verbose) {
bb_info_msg("listen_interface: %s", listen_interface);
bb_info_msg("fileconf: %s", fileconf);
}
- if (OPT_daemon)
-#ifdef BB_NOMMU
- /* reexec for vfork() do continue parent */
- vfork_daemon_rexec(1, 0, argc, argv, "-d");
-#else
- xdaemon(1, 0);
-#endif
+ if (OPT_daemon) {
+ bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv);
+ openlog(applet_name, LOG_PID, LOG_DAEMON);
+ logmode = LOGMODE_SYSLOG;
+ }
dnsentryinit();
signal(SIGINT, interrupt);
- signal(SIGPIPE, SIG_IGN);
- signal(SIGHUP, SIG_IGN);
+ bb_signals(0
+ /* why? + (1 << SIGPIPE) */
+ + (1 << SIGHUP)
#ifdef SIGTSTP
- signal(SIGTSTP, SIG_IGN);
+ + (1 << SIGTSTP)
#endif
#ifdef SIGURG
- signal(SIGURG, SIG_IGN);
+ + (1 << SIGURG)
#endif
+ , SIG_IGN);
- udps = listen_socket(listen_interface, port);
+ lsa = xdotted2sockaddr(listen_interface, port);
+ udps = xsocket(lsa->u.sa.sa_family, SOCK_DGRAM, 0);
+ xbind(udps, &lsa->u.sa, lsa->len);
+ socket_want_pktinfo(udps); /* needed for recv_from_to to work */
+ lsa_size = LSA_LEN_SIZE + lsa->len;
+ from = xzalloc(lsa_size);
+ to = xzalloc(lsa_size);
+
+ bb_info_msg("Accepting UDP packets on %s",
+ xmalloc_sockaddr2dotted(&lsa->u.sa));
while (1) {
- fd_set fdset;
int r;
-
- FD_ZERO(&fdset);
- FD_SET(udps, &fdset);
- // Block until a message arrives
- r = select(udps + 1, &fdset, NULL, NULL, NULL);
- if (r < 0)
- bb_perror_msg_and_die("select error");
- if (r == 0)
- bb_perror_msg_and_die("select spurious return");
-
- /* Can this test ever be false? - yes */
- if (FD_ISSET(udps, &fdset)) {
- struct sockaddr_in from;
- int fromlen = sizeof(from);
- r = recvfrom(udps, buf, sizeof(buf), 0,
- (struct sockaddr *)&from,
- (void *)&fromlen);
- if (OPT_verbose)
- fprintf(stderr, "\n--- Got UDP ");
- log_message(LOG_FILE, "\n--- Got UDP ");
-
- if (r < 12 || r > 512) {
- bb_error_msg("invalid packet size");
- continue;
- }
- if (r > 0) {
- r = process_packet(buf);
- if (r > 0)
- sendto(udps, buf,
- r, 0, (struct sockaddr *)&from,
- fromlen);
- }
- } // end if
- } // end while
+ /* Try to get *DEST* address (to which of our addresses
+ * this query was directed), and reply from the same address.
+ * Or else we can exhibit usual UDP ugliness:
+ * [ip1.multihomed.ip2] <= query to ip1 <= peer
+ * [ip1.multihomed.ip2] => reply from ip2 => peer (confused) */
+ memcpy(to, lsa, lsa_size);
+ r = recv_from_to(udps, buf, MAX_PACK_LEN + 1, 0, &from->u.sa, &to->u.sa, lsa->len);
+ if (r < 12 || r > MAX_PACK_LEN) {
+ bb_error_msg("invalid packet size");
+ continue;
+ }
+ if (OPT_verbose)
+ bb_info_msg("Got UDP packet");
+ buf[r] = '\0'; /* paranoia */
+ r = process_packet(buf);
+ if (r <= 0)
+ continue;
+ send_to_from(udps, buf, r, 0, &to->u.sa, &from->u.sa, lsa->len);
+ }
return 0;
}