11 Nibmask = (1<<Nibwidth) - 1,
12 V6maxrevdomdepth = 128 / Nibwidth, /* bits / bits-per-nibble */
15 * ttl for generated ptr records. it was zero, which might seem
16 * like a good idea, but some dns implementations seem to be
17 * confused by a zero ttl, and instead of using the data and then
18 * discarding the RR, they conclude that they don't have valid data.
26 static RR* addrrr(Ndbtuple*, Ndbtuple*);
27 static RR* cnamerr(Ndbtuple*, Ndbtuple*);
28 static void createptrs(void);
29 static RR* dblookup1(char*, int, int, int);
30 static RR* doaxfr(Ndb*, char*);
31 static Ndbtuple*look(Ndbtuple*, Ndbtuple*, char*);
32 static RR* mxrr(Ndbtuple*, Ndbtuple*);
33 static RR* nsrr(Ndbtuple*, Ndbtuple*);
34 static RR* nullrr(Ndbtuple*, Ndbtuple*);
35 static RR* ptrrr(Ndbtuple*, Ndbtuple*);
36 static RR* soarr(Ndbtuple*, Ndbtuple*);
37 static RR* srvrr(Ndbtuple*, Ndbtuple*);
38 static RR* txtrr(Ndbtuple*, Ndbtuple*);
40 static int implemented[Tall] =
54 /* straddle server configuration */
55 static Ndbtuple *indoms, *innmsrvs, *outnmsrvs;
58 nstrcpy(char *to, char *from, int len)
60 strncpy(to, from, len);
73 xdb = ndbopen(dbfile); /* /lib/ndb */
75 snprint(netdbnm, sizeof netdbnm, "%s/ndb", mntpt);
76 for(netdb = xdb; netdb; netdb = netdb->next)
77 if(strcmp(netdb->file, netdbnm) == 0){
82 netdb = ndbopen(netdbnm); /* /net/ndb */
86 db = ndbcat(netdb, xdb); /* both */
91 * lookup an RR in the network database, look for matches
92 * against both the domain name and the wildcarded domain name.
94 * the lock makes sure only one process can be accessing the data
95 * base at a time. This is important since there's a lot of
98 * e.g. for x.research.bell-labs.com, first look for a match against
99 * the x.research.bell-labs.com. If nothing matches,
100 * try *.research.bell-labs.com.
103 dblookup(char *name, int class, int type, int auth, int ttl)
106 char buf[Domlen], *wild;
110 /* so far only internet lookups are implemented */
118 for (type = Ta; type < Tall; type++)
119 if(implemented[type])
120 rrcat(&rp, dblookup(name, class, type, auth, ttl));
126 dp = idnlookup(name, class, 1);
128 if(opendatabase() < 0)
133 /* first try the given name */
135 rp = rrlookup(dp, type, NOneg);
137 rp = dblookup1(name, type, auth, ttl);
141 /* walk the domain name trying the wildcard '*' at each position */
142 for(wild = strchr(name, '.'); wild; wild = strchr(wild+1, '.')){
143 snprint(buf, sizeof buf, "*%s", wild);
144 ndp = idnlookup(buf, class, 1);
148 rp = rrlookup(ndp, type, NOneg);
150 rp = dblookup1(buf, type, auth, ttl);
155 /* add owner to uncached records */
157 for(tp = rp; tp; tp = tp->next)
161 * don't call it non-existent if it's not ours
162 * (unless we're a resolver).
164 if(err == Rname && (!inmyarea(dp->name) || cfg.resolver))
174 intval(Ndbtuple *entry, Ndbtuple *pair, char *attr, ulong def)
176 Ndbtuple *t = look(entry, pair, attr);
178 return (t? strtoul(t->val, 0, 10): def);
189 cp += runetochar(cp, &r);
194 * lookup an RR in the network database
197 dblookup1(char *name, int type, int auth, int ttl)
205 RR *(*f)(Ndbtuple*, Ndbtuple*);
248 return doaxfr(db, name);
250 // dnslog("dblookup1(%s) bad type", name);
255 * find a matching entry in the database
257 nstrcpy(dname, name, sizeof dname);
260 case 1: /* try unicode */
261 if(idn2utf(name, dname, sizeof dname) == nil){
262 nstrcpy(dname, name, sizeof dname);
265 if(strcmp(name, dname) == 0)
268 case 3: /* try ascii (lower case) */
269 if(utf2idn(name, dname, sizeof dname) == nil)
273 if(strcmp(name, dname) == 0)
278 free(ndbgetvalue(db, &s, "dom", dname, attr, &t));
279 if(t == nil && strchr(dname, '.') == nil)
280 free(ndbgetvalue(db, &s, "sys", dname, attr, &t));
286 // dnslog("dblookup1(%s) name not found", name);
290 /* search whole entry for default domain name */
291 for(nt = t; nt; nt = nt->entry)
292 if(strcmp(nt->attr, "dom") == 0){
293 nstrcpy(dname, nt->val, sizeof dname);
297 /* ttl is maximum of soa minttl and entry's ttl ala rfc883 */
298 x = intval(t, s.t, "ttl", 0);
302 /* default ttl is one day */
307 * The database has 2 levels of precedence; line and entry.
308 * Pairs on the same line bind tighter than pairs in the
309 * same entry, so we search the line first.
315 if(found == 0 && strcmp(nt->attr, "dom") == 0){
316 nstrcpy(dname, nt->val, sizeof dname);
319 if(strcmp(attr, nt->attr) == 0){
326 dp = idnlookup(dname, Cin, 1);
337 /* search whole entry */
338 for(nt = t; nt; nt = nt->entry)
339 if(nt->ptr == 0 && strcmp(attr, nt->attr) == 0){
346 dp = idnlookup(dname, Cin, 1);
353 // dnslog("dblookup1(%s) -> %#p", name, list);
358 * make various types of resource records from a database entry
361 addrrr(Ndbtuple *entry, Ndbtuple *pair)
364 uchar addr[IPaddrlen];
367 parseip(addr, pair->val);
372 rp->ip = dnlookup(pair->val, Cin, 1);
376 nullrr(Ndbtuple *entry, Ndbtuple *pair)
382 rp->null->data = (uchar*)estrdup(pair->val);
383 rp->null->dlen = strlen((char*)rp->null->data);
387 * txt rr strings are at most 255 bytes long. one
388 * can represent longer strings by multiple concatenated
392 txtrr(Ndbtuple *entry, Ndbtuple *pair)
402 len = strlen(pair->val);
405 t = emalloc(sizeof(*t));
413 memmove(t->p, pair->val+sofar, i);
423 cnamerr(Ndbtuple *entry, Ndbtuple *pair)
428 rp = rralloc(Tcname);
429 rp->host = idnlookup(pair->val, Cin, 1);
433 mxrr(Ndbtuple *entry, Ndbtuple *pair)
438 rp->host = idnlookup(pair->val, Cin, 1);
439 rp->pref = intval(entry, pair, "pref", 1);
443 nsrr(Ndbtuple *entry, Ndbtuple *pair)
449 rp->host = idnlookup(pair->val, Cin, 1);
450 t = look(entry, pair, "soa");
451 if(t && t->val[0] == 0)
456 ptrrr(Ndbtuple *entry, Ndbtuple *pair)
462 rp->ptr = dnlookup(pair->val, Cin, 1);
466 soarr(Ndbtuple *entry, Ndbtuple *pair)
469 Ndbtuple *ns, *mb, *t;
470 char mailbox[Domlen];
476 for(ndb = db; ndb; ndb = ndb->next)
477 if(ndb->mtime > rp->soa->serial)
478 rp->soa->serial = ndb->mtime;
480 rp->soa->retry = intval(entry, pair, "retry", Hour);
481 rp->soa->expire = intval(entry, pair, "expire", Day);
482 rp->soa->minttl = intval(entry, pair, "ttl", Day);
483 rp->soa->refresh = intval(entry, pair, "refresh", Day);
484 rp->soa->serial = intval(entry, pair, "serial", rp->soa->serial);
486 ns = look(entry, pair, "ns");
488 ns = look(entry, pair, "dom");
489 rp->host = idnlookup(ns->val, Cin, 1);
493 * mbox=person@machine.dom
494 * mbox=person.machine.dom
496 mb = look(entry, pair, "mbox");
498 mb = look(entry, pair, "mb");
500 if(strchr(mb->val, '.')) {
501 p = strchr(mb->val, '@');
504 rp->rmb = idnlookup(mb->val, Cin, 1);
506 snprint(mailbox, sizeof mailbox, "%s.%s",
508 rp->rmb = idnlookup(mailbox, Cin, 1);
511 snprint(mailbox, sizeof mailbox, "postmaster.%s", ns->val);
512 rp->rmb = idnlookup(mailbox, Cin, 1);
516 * hang dns slaves off of the soa. this is
517 * for managing the area.
519 for(t = entry; t != nil; t = t->entry)
520 if(strcmp(t->attr, "dnsslave") == 0)
521 addserver(&rp->soa->slaves, t->val);
527 srvrr(Ndbtuple *entry, Ndbtuple *pair)
532 rp->host = idnlookup(pair->val, Cin, 1);
533 rp->srv->pri = intval(entry, pair, "pri", 0);
534 rp->srv->weight = intval(entry, pair, "weight", 0);
535 /* TODO: translate service name to port # */
536 rp->port = intval(entry, pair, "port", 0);
541 * Look for a pair with the given attribute. look first on the same line,
542 * then in the whole entry.
545 look(Ndbtuple *entry, Ndbtuple *line, char *attr)
549 /* first look on same line (closer binding) */
551 if(strcmp(attr, nt->attr) == 0)
557 /* search whole tuple */
558 for(nt = entry; nt; nt = nt->entry)
559 if(strcmp(attr, nt->attr) == 0)
564 /* these are answered specially by the tcp version */
566 doaxfr(Ndb *db, char *name)
573 * read the database into the cache
576 dbpair2cache(DN *dp, Ndbtuple *entry, Ndbtuple *pair)
582 if(strcmp(pair->attr, "ip") == 0 ||
583 strcmp(pair->attr, "ipv6") == 0) {
585 rp = addrrr(entry, pair);
587 else if(strcmp(pair->attr, "ns") == 0)
588 rp = nsrr(entry, pair);
589 else if(strcmp(pair->attr, "soa") == 0) {
590 rp = soarr(entry, pair);
591 addarea(dp, rp, pair);
593 else if(strcmp(pair->attr, "mx") == 0)
594 rp = mxrr(entry, pair);
595 else if(strcmp(pair->attr, "srv") == 0)
596 rp = srvrr(entry, pair);
597 else if(strcmp(pair->attr, "cname") == 0)
598 rp = cnamerr(entry, pair);
599 else if(strcmp(pair->attr, "nullrr") == 0)
600 rp = nullrr(entry, pair);
601 else if(strcmp(pair->attr, "txtrr") == 0)
602 rp = txtrr(entry, pair);
608 rp->ttl = intval(entry, pair, "ttl", rp->ttl);
609 rrattach(rp, Notauthoritative);
613 dbtuple2cache(Ndbtuple *t)
618 for(et = t; et; et = et->entry)
619 if(strcmp(et->attr, "dom") == 0){
620 dp = idnlookup(et->val, Cin, 1);
622 /* first same line */
623 for(nt = et->line; nt != et; nt = nt->line){
624 dbpair2cache(dp, t, nt);
628 /* then rest of entry */
629 for(nt = t; nt; nt = nt->entry){
631 dbpair2cache(dp, t, nt);
637 dbfile2cache(Ndb *db)
642 dnslog("rereading %s", db->file);
644 while(t = ndbparse(db)){
650 /* called with dblock held */
656 if (!cfg.inside || !cfg.straddle || !cfg.serve)
662 indoms = innmsrvs = outnmsrvs = nil;
666 free(ndbgetvalue(db, &s, "sys", "inside-dom", "dom", &indoms));
667 free(ndbgetvalue(db, &s, "sys", "inside-ns", "ip", &innmsrvs));
668 free(ndbgetvalue(db, &s, "sys", "outside-ns", "ip", &outnmsrvs));
669 dnslog("[%d] ndb changed: reloaded inside-dom, inside-ns, outside-ns",
679 static ulong lastcheck, lastyoungest;
681 /* no faster than once every 2 minutes */
682 if(now < lastcheck + 2*Min && !doit)
685 refresh_areas(owned);
689 if(opendatabase() < 0){
695 * file may be changing as we are reading it, so loop till
696 * mod times are consistent.
698 * we don't use the times in the ndb records because they may
699 * change outside of refreshing our cached knowledge.
704 for(ndb = db; ndb; ndb = ndb->next)
705 /* dirfstat avoids walking the mount table each time */
706 if((d = dirfstat(Bfildes(&ndb->b))) != nil ||
707 (d = dirstat(ndb->file)) != nil){
708 if(d->mtime > youngest)
712 if(!doit && youngest == lastyoungest)
715 /* forget our area definition */
717 freearea(&delegated);
719 /* reopen all the files (to get oldest for time stamp) */
720 for(ndb = db; ndb; ndb = ndb->next)
723 /* reload straddle-server configuration */
726 /* mark all db records as timed out */
730 /* read in new entries */
731 for(ndb = db; ndb; ndb = ndb->next)
736 * mark as authoritative anything in our domain,
737 * delete timed out db records
741 /* remove old entries */
745 lastyoungest = youngest;
752 extern char mntpt[Maxpath]; /* net mountpoint */
753 static uchar ipaddr[IPaddrlen]; /* my ip address */
757 * caller ndbfrees the result
760 lookupinfo(char *attr)
766 if(ipcmp(ipaddr, IPnoaddr) == 0)
767 if(myipaddr(ipaddr, mntpt) < 0)
770 snprint(buf, sizeof buf, "%I", ipaddr);
774 if(opendatabase() < 0){
778 t = ndbipinfo(db, "ip", buf, a, 1);
784 * return non-zero if this is a bad delegation
787 baddelegation(RR *rp, RR *nsrp, uchar *addr)
797 t = lookupinfo("dom");
799 /* see if delegating to us what we don't own */
800 for(nt = t; nt != nil; nt = nt->entry)
801 if(rp->host && cistrcmp(rp->host->name, nt->val) == 0)
804 if(nt != nil && !inmyarea(rp->owner->name)){
807 dnslog("bad delegation %R from %I/%s; "
808 "no further logging of them",
809 rp, addr, nsrp->host->name);
824 if(ipcmp(ipaddr, IPnoaddr) == 0)
825 if(myipaddr(ipaddr, mntpt) < 0)
828 snprint(buf, sizeof buf, "%I", ipaddr);
829 if (strcmp(addr, buf) == 0) {
830 dnslog("rejecting my ip %s as local dns server", addr);
834 snprint(buf, sizeof buf, "%s/ipselftab", mntpt);
835 bp = Bopen(buf, OREAD);
837 while ((line = Brdline(bp, '\n')) != nil) {
838 line[Blinelen(bp) - 1] = '\0';
839 sp = strchr(line, ' ');
842 if (strcmp(addr, line) == 0) {
843 dnslog("rejecting my ip %s as local dns server",
854 static char *locdns[20];
855 static QLock locdnslck;
858 addlocaldnsserver(DN *dp, int class, char *ipaddr, int i)
866 /* reject our own ip addresses so we don't query ourselves via udp */
871 for (n = 0; n < i && n < nelem(locdns) && locdns[n]; n++)
872 if (strcmp(locdns[n], ipaddr) == 0) {
873 dnslog("rejecting duplicate local dns server ip %s",
878 if (n < nelem(locdns))
879 if (locdns[n] == nil || ++n < nelem(locdns))
880 locdns[n] = strdup(ipaddr); /* remember 1st few local ns */
883 /* ns record for name server, make up an impossible name */
885 snprint(buf, sizeof buf, "local#dns#server%d", i);
886 nsdp = dnlookup(buf, class, 1);
888 rp->owner = dp; /* e.g., local#dns#servers */
892 rrattach(rp, Authoritative); /* will not attach rrs in my area */
895 /* A or AAAA record */
896 if (parseip(ip, ipaddr) >= 0 && isv4(ip))
900 rp->ip = dnlookup(ipaddr, class, 1);
905 rrattach(rp, Authoritative); /* will not attach rrs in my area */
908 dnslog("added local dns server %s at %s", buf, ipaddr);
912 * return list of dns server addresses to use when
913 * acting just as a resolver.
916 dnsservers(int class)
925 dp = dnlookup("local#dns#servers", class, 1);
926 nsrp = rrlookup(dp, Tns, NOneg);
930 p = getenv("DNSSERVER"); /* list of ip addresses */
932 n = tokenize(p, args, nelem(args));
933 for(i = 0; i < n; i++)
934 addlocaldnsserver(dp, class, args[i], i);
937 t = lookupinfo("@dns"); /* @dns=ip1 @dns=ip2 ... */
941 for(nt = t; nt != nil; nt = nt->entry){
942 addlocaldnsserver(dp, class, nt->val, i);
948 return rrlookup(dp, Tns, NOneg);
952 addlocaldnsdomain(DN *dp, int class, char *domain)
958 rp->ptr = dnlookup(domain, class, 1);
962 rrattach(rp, Authoritative);
967 * return list of domains to use when resolving names without '.'s
970 domainlist(int class)
976 dp = dnlookup("local#dns#domains", class, 1);
977 rp = rrlookup(dp, Tptr, NOneg);
981 t = lookupinfo("dnsdomain");
984 for(nt = t; nt != nil; nt = nt->entry)
985 addlocaldnsdomain(dp, class, nt->val);
988 return rrlookup(dp, Tptr, NOneg);
991 char *v4ptrdom = ".in-addr.arpa";
992 char *v6ptrdom = ".ip6.arpa"; /* ip6.int deprecated, rfc 3152 */
1000 * create ptrs that are in our v4 areas
1007 char buf[Domlen], ipa[48];
1009 uchar net[IPaddrlen], mask[IPaddrlen];
1013 dlen = strlen(v4ptrdom);
1014 for(s = owned; s; s = s->next){
1015 dom = s->soarr->owner->name;
1017 if((len <= dlen || cistrcmp(dom+len-dlen, v4ptrdom) != 0) &&
1018 cistrcmp(dom, v4ptrdom+1) != 0)
1021 /* get mask and net value */
1022 nstrcpy(buf, dom, sizeof buf);
1023 /* buf contains something like 178.204.in-addr.arpa (n==4) */
1024 n = getfields(buf, f, nelem(f), 0, ".");
1025 memset(mask, 0xff, IPaddrlen);
1026 ipmove(net, v4prefix);
1029 net[IPv4off] = atoi(f[0]);
1030 mask[IPv4off+1] = 0;
1031 mask[IPv4off+2] = 0;
1032 mask[IPv4off+3] = 0;
1035 net[IPv4off] = atoi(f[1]);
1036 net[IPv4off+1] = atoi(f[0]);
1037 mask[IPv4off+2] = 0;
1038 mask[IPv4off+3] = 0;
1041 net[IPv4off] = atoi(f[2]);
1042 net[IPv4off+1] = atoi(f[1]);
1043 net[IPv4off+2] = atoi(f[0]);
1044 mask[IPv4off+3] = 0;
1046 case 6: /* rfc2317: classless in-addr.arpa delegation */
1047 net[IPv4off] = atoi(f[3]);
1048 net[IPv4off+1] = atoi(f[2]);
1049 net[IPv4off+2] = atoi(f[1]);
1050 net[IPv4off+3] = atoi(f[0]);
1051 sprint(ipa, "%I", net);
1052 t = ndbipinfo(db, "ip", ipa, attribs, 1);
1053 if(t == nil) /* could be a reverse with no forward */
1055 nt = look(t, t, "ipmask");
1056 if(nt == nil){ /* we're confused */
1060 parseipmask(mask, nt->val);
1069 * go through all domain entries looking for RR's
1070 * in this network and create ptrs.
1071 * +2 for ".in-addr.arpa".
1073 dnptr(net, mask, dom, Ta, 4+2-n, Ptrttl);
1077 /* convert bytes to nibbles, big-endian */
1079 bytes2nibbles(uchar *nibbles, uchar *bytes, int nbytes)
1081 while (nbytes-- > 0) {
1082 *nibbles++ = *bytes >> Nibwidth;
1083 *nibbles++ = *bytes++ & Nibmask;
1088 nibbles2bytes(uchar *bytes, uchar *nibbles, int nnibs)
1090 for (; nnibs >= 2; nnibs -= 2) {
1091 *bytes++ = nibbles[0] << Nibwidth | (nibbles[1]&Nibmask);
1095 *bytes = nibbles[0] << Nibwidth;
1099 * create ptrs that are in our v6 areas. see rfc3596
1104 int len, dlen, i, n, pfxnibs;
1108 uchar net[IPaddrlen], mask[IPaddrlen];
1109 uchar nibnet[IPaddrlen*2], nibmask[IPaddrlen*2];
1112 dlen = strlen(v6ptrdom);
1113 for(s = owned; s; s = s->next){
1114 dom = s->soarr->owner->name;
1116 if((len <= dlen || cistrcmp(dom+len-dlen, v6ptrdom) != 0) &&
1117 cistrcmp(dom, v6ptrdom+1) != 0)
1120 /* get mask and net value */
1121 nstrcpy(buf, dom, sizeof buf);
1122 /* buf contains something like 2.0.0.2.ip6.arpa (n==6) */
1123 n = getfields(buf, f, nelem(f), 0, ".");
1124 pfxnibs = n - 2; /* 2 for .ip6.arpa */
1125 if (pfxnibs < 0 || pfxnibs > V6maxrevdomdepth)
1128 memset(net, 0, IPaddrlen);
1129 memset(mask, 0xff, IPaddrlen);
1130 bytes2nibbles(nibnet, net, IPaddrlen);
1131 bytes2nibbles(nibmask, mask, IPaddrlen);
1133 /* copy prefix of f, in reverse order, to start of net. */
1134 for (i = 0; i < pfxnibs; i++)
1135 nibnet[i] = strtol(f[pfxnibs - 1 - i], nil, 16);
1136 /* zero nibbles of mask after prefix in net */
1137 memset(nibmask + pfxnibs, 0, V6maxrevdomdepth - pfxnibs);
1139 nibbles2bytes(net, nibnet, 2*IPaddrlen);
1140 nibbles2bytes(mask, nibmask, 2*IPaddrlen);
1143 * go through all domain entries looking for RR's
1144 * in this network and create ptrs.
1146 dnptr(net, mask, dom, Taaaa, V6maxrevdomdepth - pfxnibs, Ptrttl);
1151 * create ptrs that are in our areas
1161 * is this domain (or DOMAIN or Domain or dOMAIN)
1162 * internal to our organisation (behind our firewall)?
1163 * only inside straddling servers care, everybody else gets told `yes',
1164 * so they'll use mntpt for their queries.
1167 insideaddr(char *dom)
1169 int domlen, vallen, rv;
1172 if (!cfg.inside || !cfg.straddle || !cfg.serve)
1174 if (dom[0] == '\0' || strcmp(dom, ".") == 0) /* dns root? */
1175 return 1; /* hack for initialisation */
1180 if (indoms == nil) {
1182 return 1; /* no "inside-dom" sys, try inside nameservers */
1186 domlen = strlen(dom);
1187 for (t = indoms; t != nil; t = t->entry) {
1188 if (strcmp(t->attr, "dom") != 0)
1190 vallen = strlen(t->val);
1191 if (cistrcmp(dom, t->val) == 0 ||
1193 cistrcmp(dom + domlen - vallen, t->val) == 0 &&
1194 dom[domlen - vallen - 1] == '.') {
1206 uchar ipa[IPaddrlen];
1209 for (t = innmsrvs; t != nil; t = t->entry)
1210 if (strcmp(t->attr, "ip") == 0) {
1211 parseip(ipa, t->val);
1212 if (memcmp(ipa, ip, sizeof ipa) == 0)
1219 outsidensip(int n, uchar *ip)
1225 for (t = outnmsrvs; t != nil; t = t->entry)
1226 if (strcmp(t->attr, "ip") == 0 && i++ == n) {
1227 parseip(ip, t->val);