10 static RR* dblookup1(char*, int, int, int);
11 static RR* addrrr(Ndbtuple*, Ndbtuple*);
12 static RR* nsrr(Ndbtuple*, Ndbtuple*);
13 static RR* cnamerr(Ndbtuple*, Ndbtuple*);
14 static RR* mxrr(Ndbtuple*, Ndbtuple*);
15 static RR* soarr(Ndbtuple*, Ndbtuple*);
16 static RR* ptrrr(Ndbtuple*, Ndbtuple*);
17 static Ndbtuple* look(Ndbtuple*, Ndbtuple*, char*);
18 static RR* doaxfr(Ndb*, char*);
19 static RR* nullrr(Ndbtuple *entry, Ndbtuple *pair);
20 static RR* txtrr(Ndbtuple *entry, Ndbtuple *pair);
22 static void createptrs(void);
24 static int implemented[Tall] =
46 nstrcpy(char *to, char *from, int len)
48 strncpy(to, from, len);
59 snprint(buf, sizeof(buf), "%s/ndb", mntpt);
60 xdb = ndbopen(dbfile);
63 db = ndbcat(ndbopen(buf), xdb);
72 * lookup an RR in the network database, look for matches
73 * against both the domain name and the wildcarded domain name.
75 * the lock makes sure only one process can be accessing the data
76 * base at a time. This is important since there's a lot of
79 * e.g. for x.research.bell-labs.com, first look for a match against
80 * the x.research.bell-labs.com. If nothing matches, try *.research.bell-labs.com.
83 dblookup(char *name, int class, int type, int auth, int ttl)
91 /* so far only internet lookups are implemented */
99 for (type = Ta; type < Tall; type++)
100 if(implemented[type])
101 rrcat(&rp, dblookup(name, class, type, auth, ttl));
107 dp = dnlookup(name, class, 1);
108 if(opendatabase() < 0)
113 /* first try the given name */
116 rp = rrlookup(dp, type, NOneg);
118 rp = dblookup1(name, type, auth, ttl);
122 /* try lower case version */
123 for(cp = name; *cp; cp++)
124 *cp = tolower((uchar)*cp);
126 rp = rrlookup(dp, type, NOneg);
128 rp = dblookup1(name, type, auth, ttl);
132 /* walk the domain name trying the wildcard '*' at each position */
133 for(wild = strchr(name, '.'); wild; wild = strchr(wild+1, '.')){
134 snprint(buf, sizeof(buf), "*%s", wild);
135 ndp = dnlookup(buf, class, 1);
139 rp = rrlookup(ndp, type, NOneg);
141 rp = dblookup1(buf, type, auth, ttl);
146 /* add owner to uncached records */
148 for(tp = rp; tp; tp = tp->next)
151 /* don't call it non-existent if it's not ours */
152 if(err == Rname && !inmyarea(name))
154 dp->nonexistent = err;
162 * lookup an RR in the network database
165 dblookup1(char *name, int type, int auth, int ttl)
173 RR *(*f)(Ndbtuple*, Ndbtuple*);
208 return doaxfr(db, name);
214 * find a matching entry in the database
216 free(ndbgetvalue(db, &s, "dom", name, attr, &t));
219 * hack for local names
221 if(t == 0 && strchr(name, '.') == 0)
222 free(ndbgetvalue(db, &s, "sys", name, attr, &t));
226 /* search whole entry for default domain name */
227 strncpy(dname, name, sizeof dname);
228 for(nt = t; nt; nt = nt->entry)
229 if(strcmp(nt->attr, "dom") == 0){
230 nstrcpy(dname, nt->val, sizeof dname);
234 /* ttl is maximum of soa minttl and entry's ttl ala rfc883 */
235 nt = look(t, s.t, "ttl");
242 /* default ttl is one day */
247 * The database has 2 levels of precedence; line and entry.
248 * Pairs on the same line bind tighter than pairs in the
249 * same entry, so we search the line first.
255 if(found == 0 && strcmp(nt->attr, "dom") == 0){
256 nstrcpy(dname, nt->val, sizeof dname);
259 if(cistrcmp(attr, nt->attr) == 0){
266 dp = dnlookup(dname, Cin, 1);
277 /* search whole entry */
278 for(nt = t; nt; nt = nt->entry)
279 if(nt->ptr == 0 && cistrcmp(attr, nt->attr) == 0){
286 dp = dnlookup(dname, Cin, 1);
297 * make various types of resource records from a database entry
300 addrrr(Ndbtuple *entry, Ndbtuple *pair)
303 uchar addr[IPaddrlen];
306 parseip(addr, pair->val);
311 rp->ip = dnlookup(pair->val, Cin, 1);
315 nullrr(Ndbtuple *entry, Ndbtuple *pair)
321 rp->null->data = (uchar*)estrdup(pair->val);
322 rp->null->dlen = strlen((char*)rp->null->data);
326 * txt rr strings are at most 255 bytes long. one
327 * can represent longer strings by multiple concatenated
331 txtrr(Ndbtuple *entry, Ndbtuple *pair)
341 len = strlen(pair->val);
344 t = emalloc(sizeof(*t));
352 memmove(t->p, pair->val+sofar, i);
362 cnamerr(Ndbtuple *entry, Ndbtuple *pair)
367 rp = rralloc(Tcname);
368 rp->host = dnlookup(pair->val, Cin, 1);
372 mxrr(Ndbtuple *entry, Ndbtuple *pair)
377 rp->host = dnlookup(pair->val, Cin, 1);
378 pair = look(entry, pair, "pref");
380 rp->pref = atoi(pair->val);
386 nsrr(Ndbtuple *entry, Ndbtuple *pair)
392 rp->host = dnlookup(pair->val, Cin, 1);
393 t = look(entry, pair, "soa");
394 if(t && t->val[0] == 0)
399 ptrrr(Ndbtuple *entry, Ndbtuple *pair)
405 rp->ptr = dnlookup(pair->val, Cin, 1);
409 soarr(Ndbtuple *entry, Ndbtuple *pair)
412 Ndbtuple *ns, *mb, *t;
413 char mailbox[Domlen];
419 for(ndb = db; ndb; ndb = ndb->next)
420 if(ndb->mtime > rp->soa->serial)
421 rp->soa->serial = ndb->mtime;
422 rp->soa->refresh = Day;
423 rp->soa->retry = Hour;
424 rp->soa->expire = Day;
425 rp->soa->minttl = Day;
426 t = look(entry, pair, "ttl");
428 rp->soa->minttl = atoi(t->val);
429 t = look(entry, pair, "refresh");
431 rp->soa->refresh = atoi(t->val);
432 t = look(entry, pair, "serial");
434 rp->soa->serial = strtoul(t->val, 0, 10);
436 ns = look(entry, pair, "ns");
438 ns = look(entry, pair, "dom");
439 rp->host = dnlookup(ns->val, Cin, 1);
443 * mbox=person@machine.dom
444 * mbox=person.machine.dom
446 mb = look(entry, pair, "mbox");
448 mb = look(entry, pair, "mb");
450 if(strchr(mb->val, '.')) {
451 p = strchr(mb->val, '@');
454 rp->rmb = dnlookup(mb->val, Cin, 1);
456 snprint(mailbox, sizeof(mailbox), "%s.%s",
458 rp->rmb = dnlookup(mailbox, Cin, 1);
461 snprint(mailbox, sizeof(mailbox), "postmaster.%s",
463 rp->rmb = dnlookup(mailbox, Cin, 1);
466 /* hang dns slaves off of the soa. this is
467 * for managing the area.
469 for(t = entry; t != nil; t = t->entry)
470 if(strcmp(t->attr, "dnsslave") == 0)
471 addserver(&rp->soa->slaves, t->val);
477 * Look for a pair with the given attribute. look first on the same line,
478 * then in the whole entry.
481 look(Ndbtuple *entry, Ndbtuple *line, char *attr)
485 /* first look on same line (closer binding) */
487 if(cistrcmp(attr, nt->attr) == 0)
493 /* search whole tuple */
494 for(nt = entry; nt; nt = nt->entry)
495 if(cistrcmp(attr, nt->attr) == 0)
500 /* these are answered specially by the tcp version */
502 doaxfr(Ndb *db, char *name)
511 * read the all the soa's from the database to determine area's.
512 * this is only used when we're not caching the database.
520 syslog(0, logfile, "rereading %s", db->file);
522 while(t = ndbparse(db)){
528 * read the database into the cache
531 dbpair2cache(DN *dp, Ndbtuple *entry, Ndbtuple *pair)
538 if(cistrcmp(pair->attr, "ip") == 0){
540 rp = addrrr(entry, pair);
541 } else if(cistrcmp(pair->attr, "ns") == 0){
542 rp = nsrr(entry, pair);
543 } else if(cistrcmp(pair->attr, "soa") == 0){
544 rp = soarr(entry, pair);
545 addarea(dp, rp, pair);
546 } else if(cistrcmp(pair->attr, "mx") == 0){
547 rp = mxrr(entry, pair);
548 } else if(cistrcmp(pair->attr, "cname") == 0){
549 rp = cnamerr(entry, pair);
550 } else if(cistrcmp(pair->attr, "nullrr") == 0){
551 rp = nullrr(entry, pair);
552 } else if(cistrcmp(pair->attr, "txtrr") == 0){
553 rp = txtrr(entry, pair);
561 t = look(entry, pair, "ttl");
563 rp->ttl = atoi(t->val);
567 dbtuple2cache(Ndbtuple *t)
572 for(et = t; et; et = et->entry){
573 if(strcmp(et->attr, "dom") == 0){
574 dp = dnlookup(et->val, Cin, 1);
576 /* first same line */
577 for(nt = et->line; nt != et; nt = nt->line){
578 dbpair2cache(dp, t, nt);
582 /* then rest of entry */
583 for(nt = t; nt; nt = nt->entry){
585 dbpair2cache(dp, t, nt);
592 dbfile2cache(Ndb *db)
597 syslog(0, logfile, "rereading %s", db->file);
599 while(t = ndbparse(db)){
609 ulong youngest, temp;
610 static ulong lastcheck;
611 static ulong lastyoungest;
613 /* no faster than once every 2 minutes */
614 if(now < lastcheck + 2*Min && !doit)
617 refresh_areas(owned);
621 if(opendatabase() < 0){
627 * file may be changing as we are reading it, so loop till
628 * mod times are consistent.
630 * we don't use the times in the ndb records because they may
631 * change outside of refreshing our cached knowledge.
636 for(ndb = db; ndb; ndb = ndb->next){
637 /* the dirfstat avoids walking the mount table each time */
638 if((d = dirfstat(Bfildes(&ndb->b))) != nil ||
639 (d = dirstat(ndb->file)) != nil){
640 temp = d->mtime; /* ulong vs int crap */
646 if(!doit && youngest == lastyoungest){
651 /* forget our area definition */
653 freearea(&delegated);
655 /* reopen all the files (to get oldest for time stamp) */
656 for(ndb = db; ndb; ndb = ndb->next)
660 /* mark all db records as timed out */
663 /* read in new entries */
664 for(ndb = db; ndb; ndb = ndb->next)
667 /* mark as authentic anything in our domain */
670 /* remove old entries */
673 /* read all the soa's to get database defaults */
674 for(ndb = db; ndb; ndb = ndb->next)
679 lastyoungest = youngest;
686 extern uchar ipaddr[IPaddrlen];
692 lookupinfo(char *attr)
698 snprint(buf, sizeof buf, "%I", ipaddr);
702 if(opendatabase() < 0){
706 t = ndbipinfo(db, "ip", buf, a, 1);
711 char *localservers = "local#dns#servers";
712 char *localserverprefix = "local#dns#server";
715 * return non-zero is this is a bad delegation
718 baddelegation(RR *rp, RR *nsrp, uchar *addr)
724 t = lookupinfo("dom");
728 for(; rp; rp = rp->next){
732 /* see if delegation is looping */
734 if(rp->owner != nsrp->owner)
735 if(subsume(rp->owner->name, nsrp->owner->name) &&
736 strcmp(nsrp->owner->name, localservers) != 0){
737 syslog(0, logfile, "delegation loop %R -> %R from %I", nsrp, rp, addr);
741 /* see if delegating to us what we don't own */
742 for(nt = t; nt != nil; nt = nt->entry)
743 if(rp->host && cistrcmp(rp->host->name, nt->val) == 0)
745 if(nt != nil && !inmyarea(rp->owner->name)){
746 syslog(0, logfile, "bad delegation %R from %I", rp, addr);
755 addlocaldnsserver(DN *dp, int class, char *ipaddr, int i)
761 /* ns record for name server, make up an impossible name */
763 snprint(buf, sizeof(buf), "%s%d", localserverprefix, i);
764 nsdp = dnlookup(buf, class, 1);
772 print("dns %s\n", ipaddr);
775 rp->ip = dnlookup(ipaddr, class, 1);
784 * return list of dns server addresses to use when
785 * acting just as a resolver.
788 dnsservers(int class)
797 dp = dnlookup(localservers, class, 1);
798 nsrp = rrlookup(dp, Tns, NOneg);
802 p = getenv("DNSSERVER");
805 n = tokenize(buf, args, nelem(args));
806 for(i = 0; i < n; i++)
807 addlocaldnsserver(dp, class, args[i], i);
810 t = lookupinfo("@dns");
814 for(nt = t; nt != nil; nt = nt->entry){
815 addlocaldnsserver(dp, class, nt->val, i);
821 return rrlookup(dp, Tns, NOneg);
825 addlocaldnsdomain(DN *dp, int class, char *domain)
831 rp->ptr = dnlookup(domain, class, 1);
839 * return list of domains to use when resolving names without '.'s
842 domainlist(int class)
848 dp = dnlookup("local#dns#domains", class, 1);
849 rp = rrlookup(dp, Tptr, NOneg);
853 t = lookupinfo("dnsdomain");
856 for(nt = t; nt != nil; nt = nt->entry)
857 addlocaldnsdomain(dp, class, nt->val);
860 return rrlookup(dp, Tptr, NOneg);
863 char *v4ptrdom = ".in-addr.arpa";
864 char *v6ptrdom = ".ip6.arpa"; /* ip6.int deprecated, rfc 3152 */
872 * create ptrs that are in our areas
881 uchar net[IPaddrlen];
882 uchar mask[IPaddrlen];
886 dlen = strlen(v4ptrdom);
887 for(s = owned; s; s = s->next){
888 len = strlen(s->soarr->owner->name);
891 if(cistrcmp(s->soarr->owner->name+len-dlen, v4ptrdom) != 0)
894 /* get mask and net value */
895 strncpy(buf, s->soarr->owner->name, sizeof(buf));
896 buf[sizeof(buf)-1] = 0;
897 n = getfields(buf, f, nelem(f), 0, ".");
898 memset(mask, 0xff, IPaddrlen);
899 ipmove(net, v4prefix);
902 net[IPv4off] = atoi(f[0]);
908 net[IPv4off] = atoi(f[1]);
909 net[IPv4off+1] = atoi(f[0]);
914 net[IPv4off] = atoi(f[2]);
915 net[IPv4off+1] = atoi(f[1]);
916 net[IPv4off+2] = atoi(f[0]);
919 case 6: /* rfc2317 */
920 net[IPv4off] = atoi(f[3]);
921 net[IPv4off+1] = atoi(f[2]);
922 net[IPv4off+2] = atoi(f[1]);
923 net[IPv4off+3] = atoi(f[0]);
924 sprint(ipa, "%I", net);
925 t = ndbipinfo(db, "ip", ipa, attribs, 1);
926 if(t == nil) /* could be a reverse with no forward */
928 nt = look(t, t, "ipmask");
929 if(nt == nil){ /* we're confused */
933 parseipmask(mask, nt->val);
940 /* go through all domain entries looking for RR's in this network and create ptrs */
941 dnptr(net, mask, s->soarr->owner->name, 6-n, 0);