123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559 |
- #include "libbb.h"
- #include <syslog.h>
- #define DEBUG 0
- enum {
-
- DEFAULT_TTL = 120,
-
- MAX_PACK_LEN = 512,
- IP_STRING_LEN = sizeof(".xxx.xxx.xxx.xxx"),
- MAX_NAME_LEN = IP_STRING_LEN - 1 + sizeof(".in-addr.arpa"),
- REQ_A = 1,
- REQ_PTR = 12,
- };
- struct dns_head {
- uint16_t id;
- uint16_t flags;
- uint16_t nquer;
- uint16_t nansw;
- uint16_t nauth;
- uint16_t nadd;
- };
- struct type_and_class {
- uint16_t type PACKED;
- uint16_t class PACKED;
- } PACKED;
- struct dns_entry {
- struct dns_entry *next;
- uint32_t ip;
- char rip[IP_STRING_LEN];
- char name[1];
- };
- #define OPT_verbose (option_mask32 & 1)
- #define OPT_silent (option_mask32 & 2)
- static void undot(char *rip)
- {
- int i = 0;
- int s = 0;
- while (rip[i])
- i++;
- for (--i; i >= 0; i--) {
- if (rip[i] == '.') {
- rip[i] = s;
- s = 0;
- } else {
- s++;
- }
- }
- }
- static struct dns_entry *parse_conf_file(const char *fileconf)
- {
- char *token[2];
- parser_t *parser;
- struct dns_entry *m, *conf_data;
- struct dns_entry **nextp;
- conf_data = NULL;
- nextp = &conf_data;
- parser = config_open(fileconf);
- while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) {
- struct in_addr ip;
- uint32_t v32;
- if (inet_aton(token[1], &ip) == 0) {
- bb_error_msg("error at line %u, skipping", parser->lineno);
- continue;
- }
- if (OPT_verbose)
- bb_error_msg("name:%s, ip:%s", token[0], token[1]);
-
- m = xzalloc(sizeof(*m) + strlen(token[0]) + 1);
-
- *nextp = m;
- nextp = &m->next;
- m->name[0] = '.';
- strcpy(m->name + 1, token[0]);
- undot(m->name);
- m->ip = ip.s_addr;
- v32 = ntohl(m->ip);
-
- sprintf(m->rip, ".%u.%u.%u.%u",
- (uint8_t)(v32),
- (uint8_t)(v32 >> 8),
- (uint8_t)(v32 >> 16),
- (v32 >> 24)
- );
- undot(m->rip);
- }
- config_close(parser);
- return conf_data;
- }
- static char *table_lookup(struct dns_entry *d,
- uint16_t type,
- char* query_string)
- {
- while (d) {
- unsigned len = d->name[0];
-
- #if DEBUG
- char *p, *q;
- q = query_string + 1;
- p = d->name + 1;
- fprintf(stderr, "%d/%d p:%s q:%s %d\n",
- (int)strlen(p), len,
- p, q, (int)strlen(q)
- );
- #endif
- if (type == htons(REQ_A)) {
-
- if (len != 1 || d->name[1] != '*') {
- if (strcasecmp(d->name, query_string) != 0)
- goto next;
- }
- return (char *)&d->ip;
- #if DEBUG
- fprintf(stderr, "Found IP:%x\n", (int)d->ip);
- #endif
- return 0;
- }
-
- if ((len != 1 || d->name[1] != '*')
-
- && is_prefixed_with(query_string, d->rip)
- ) {
- #if DEBUG
- fprintf(stderr, "Found name:%s\n", d->name);
- #endif
- return d->name;
- }
- next:
- d = d->next;
- }
- return NULL;
- }
- static int process_packet(struct dns_entry *conf_data,
- uint32_t conf_ttl,
- uint8_t *buf)
- {
- struct dns_head *head;
- struct type_and_class *unaligned_type_class;
- const char *err_msg;
- char *query_string;
- char *answstr;
- uint8_t *answb;
- uint16_t outr_rlen;
- uint16_t outr_flags;
- uint16_t type;
- uint16_t class;
- int query_len;
- head = (struct dns_head *)buf;
- if (head->nquer == 0) {
- bb_error_msg("packet has 0 queries, ignored");
- return 0;
- }
- if (head->flags & htons(0x8000)) {
- bb_error_msg("response packet, ignored");
- return 0;
- }
-
- outr_flags = htons(0x8000 | 4);
- err_msg = NULL;
-
- query_string = (void *)(head + 1);
-
- query_len = strlen(query_string) + 1;
-
- unaligned_type_class = (void *)(query_string + query_len);
- query_len += sizeof(*unaligned_type_class);
-
- answb = (void *)(unaligned_type_class + 1);
-
- if ((head->flags & htons(0x7800)) != 0) {
- err_msg = "opcode != 0";
- goto empty_packet;
- }
- move_from_unaligned16(class, &unaligned_type_class->class);
- if (class != htons(1)) {
- err_msg = "class != 1";
- goto empty_packet;
- }
- move_from_unaligned16(type, &unaligned_type_class->type);
- if (type != htons(REQ_A) && type != htons(REQ_PTR)) {
-
- err_msg = "type is !REQ_A and !REQ_PTR";
- goto empty_packet;
- }
-
- answstr = table_lookup(conf_data, type, query_string);
- #if DEBUG
-
- bb_error_msg("'%s'->'%s'", query_string, answstr);
- #endif
- outr_rlen = 4;
- if (answstr && type == htons(REQ_PTR)) {
-
- outr_rlen = strlen(answstr) + 1;
- }
- if (!answstr
- || (unsigned)(answb - buf) + query_len + 4 + 2 + outr_rlen > MAX_PACK_LEN
- ) {
-
- err_msg = "name is not found";
- outr_flags = htons(0x8000 | 0x0400 | 3);
- goto empty_packet;
- }
-
- memcpy(answb, query_string, query_len);
- answb += query_len;
- move_to_unaligned32((uint32_t *)answb, htonl(conf_ttl));
- answb += 4;
- move_to_unaligned16((uint16_t *)answb, htons(outr_rlen));
- answb += 2;
- memcpy(answb, answstr, outr_rlen);
- answb += outr_rlen;
-
- if (OPT_verbose)
- bb_error_msg("returning positive reply");
- outr_flags = htons(0x8000 | 0x0400 | 0);
-
- head->nansw = htons(1);
- empty_packet:
- if ((outr_flags & htons(0xf)) != 0) {
- if (OPT_verbose) {
- bb_error_msg("%s, %s",
- err_msg,
- OPT_silent ? "dropping query" : "sending error reply"
- );
- }
- if (OPT_silent)
- return 0;
- }
- head->flags |= outr_flags;
- head->nauth = head->nadd = 0;
- head->nquer = htons(1);
- return answb - buf;
- }
- int dnsd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int dnsd_main(int argc UNUSED_PARAM, char **argv)
- {
- const char *listen_interface = "0.0.0.0";
- const char *fileconf = "/etc/dnsd.conf";
- struct dns_entry *conf_data;
- uint32_t conf_ttl = DEFAULT_TTL;
- char *sttl, *sport;
- len_and_sockaddr *lsa, *from, *to;
- unsigned lsa_size;
- int udps, opts;
- uint16_t port = 53;
-
- uint8_t buf[MAX_PACK_LEN + 1] ALIGN4;
- opts = getopt32(argv, "vsi:c:t:p:d", &listen_interface, &fileconf, &sttl, &sport);
-
-
-
-
- if (opts & (1 << 4))
- conf_ttl = xatou_range(sttl, 1, 0xffffffff);
- if (opts & (1 << 5))
- port = xatou_range(sport, 1, 0xffff);
- if (opts & (1 << 6)) {
- bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv);
- openlog(applet_name, LOG_PID, LOG_DAEMON);
- logmode = LOGMODE_SYSLOG;
- }
- conf_data = parse_conf_file(fileconf);
- 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);
- lsa_size = LSA_LEN_SIZE + lsa->len;
- from = xzalloc(lsa_size);
- to = xzalloc(lsa_size);
- {
- char *p = xmalloc_sockaddr2dotted(&lsa->u.sa);
- bb_error_msg("accepting UDP packets on %s", p);
- free(p);
- }
- while (1) {
- int r;
-
- 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("packet size %d, ignored", r);
- continue;
- }
- if (OPT_verbose)
- bb_error_msg("got UDP packet");
- buf[r] = '\0';
- r = process_packet(conf_data, conf_ttl, buf);
- if (r <= 0)
- continue;
- send_to_from(udps, buf, r, 0, &from->u.sa, &to->u.sa, lsa->len);
- }
- return 0;
- }
|