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));
106 dp = dnlookup(name, class, 1);
107 if(opendatabase() < 0)
112 /* first try the given name */
115 rp = rrlookup(dp, type, NOneg);
117 rp = dblookup1(name, type, auth, ttl);
121 /* try lower case version */
122 for(cp = name; *cp; cp++)
123 *cp = tolower((uchar)*cp);
125 rp = rrlookup(dp, type, NOneg);
127 rp = dblookup1(name, type, auth, ttl);
131 /* walk the domain name trying the wildcard '*' at each position */
132 for(wild = strchr(name, '.'); wild; wild = strchr(wild+1, '.')){
133 snprint(buf, sizeof(buf), "*%s", wild);
134 ndp = dnlookup(buf, class, 1);
138 rp = rrlookup(ndp, type, NOneg);
140 rp = dblookup1(buf, type, auth, ttl);
145 /* add owner to uncached records */
147 for(tp = rp; tp; tp = tp->next)
150 /* don't call it non-existent if it's not ours */
151 if(err == Rname && !inmyarea(name))
153 dp->nonexistent = err;
161 * lookup an RR in the network database
164 dblookup1(char *name, int type, int auth, int ttl)
172 RR *(*f)(Ndbtuple*, Ndbtuple*);
207 return doaxfr(db, name);
213 * find a matching entry in the database
215 free(ndbgetvalue(db, &s, "dom", name, attr, &t));
218 * hack for local names
220 if(t == 0 && strchr(name, '.') == 0)
221 free(ndbgetvalue(db, &s, "sys", name, attr, &t));
225 /* search whole entry for default domain name */
226 strncpy(dname, name, sizeof dname);
227 for(nt = t; nt; nt = nt->entry)
228 if(strcmp(nt->attr, "dom") == 0){
229 nstrcpy(dname, nt->val, sizeof dname);
233 /* ttl is maximum of soa minttl and entry's ttl ala rfc883 */
234 nt = look(t, s.t, "ttl");
241 /* default ttl is one day */
246 * The database has 2 levels of precedence; line and entry.
247 * Pairs on the same line bind tighter than pairs in the
248 * same entry, so we search the line first.
254 if(found == 0 && strcmp(nt->attr, "dom") == 0){
255 nstrcpy(dname, nt->val, sizeof dname);
258 if(cistrcmp(attr, nt->attr) == 0){
265 dp = dnlookup(dname, Cin, 1);
276 /* search whole entry */
277 for(nt = t; nt; nt = nt->entry)
278 if(nt->ptr == 0 && cistrcmp(attr, nt->attr) == 0){
285 dp = dnlookup(dname, Cin, 1);
296 * make various types of resource records from a database entry
299 addrrr(Ndbtuple *entry, Ndbtuple *pair)
302 uchar addr[IPaddrlen];
305 parseip(addr, pair->val);
310 rp->ip = dnlookup(pair->val, Cin, 1);
314 nullrr(Ndbtuple *entry, Ndbtuple *pair)
320 rp->null->data = (uchar*)estrdup(pair->val);
321 rp->null->dlen = strlen((char*)rp->null->data);
325 * txt rr strings are at most 255 bytes long. one
326 * can represent longer strings by multiple concatenated
330 txtrr(Ndbtuple *entry, Ndbtuple *pair)
340 len = strlen(pair->val);
343 t = emalloc(sizeof(*t));
351 memmove(t->p, pair->val+sofar, i);
361 cnamerr(Ndbtuple *entry, Ndbtuple *pair)
366 rp = rralloc(Tcname);
367 rp->host = dnlookup(pair->val, Cin, 1);
371 mxrr(Ndbtuple *entry, Ndbtuple *pair)
376 rp->host = dnlookup(pair->val, Cin, 1);
377 pair = look(entry, pair, "pref");
379 rp->pref = atoi(pair->val);
385 nsrr(Ndbtuple *entry, Ndbtuple *pair)
391 rp->host = dnlookup(pair->val, Cin, 1);
392 t = look(entry, pair, "soa");
393 if(t && t->val[0] == 0)
398 ptrrr(Ndbtuple *entry, Ndbtuple *pair)
404 rp->ptr = dnlookup(pair->val, Cin, 1);
408 soarr(Ndbtuple *entry, Ndbtuple *pair)
411 Ndbtuple *ns, *mb, *t;
412 char mailbox[Domlen];
418 for(ndb = db; ndb; ndb = ndb->next)
419 if(ndb->mtime > rp->soa->serial)
420 rp->soa->serial = ndb->mtime;
421 rp->soa->refresh = Day;
422 rp->soa->retry = Hour;
423 rp->soa->expire = Day;
424 rp->soa->minttl = Day;
425 t = look(entry, pair, "ttl");
427 rp->soa->minttl = atoi(t->val);
428 t = look(entry, pair, "refresh");
430 rp->soa->refresh = atoi(t->val);
431 t = look(entry, pair, "serial");
433 rp->soa->serial = strtoul(t->val, 0, 10);
435 ns = look(entry, pair, "ns");
437 ns = look(entry, pair, "dom");
438 rp->host = dnlookup(ns->val, Cin, 1);
442 * mbox=person@machine.dom
443 * mbox=person.machine.dom
445 mb = look(entry, pair, "mbox");
447 mb = look(entry, pair, "mb");
449 if(strchr(mb->val, '.')) {
450 p = strchr(mb->val, '@');
453 rp->rmb = dnlookup(mb->val, Cin, 1);
455 snprint(mailbox, sizeof(mailbox), "%s.%s",
457 rp->rmb = dnlookup(mailbox, Cin, 1);
460 snprint(mailbox, sizeof(mailbox), "postmaster.%s",
462 rp->rmb = dnlookup(mailbox, Cin, 1);
465 /* hang dns slaves off of the soa. this is
466 * for managing the area.
468 for(t = entry; t != nil; t = t->entry)
469 if(strcmp(t->attr, "dnsslave") == 0)
470 addserver(&rp->soa->slaves, t->val);
476 * Look for a pair with the given attribute. look first on the same line,
477 * then in the whole entry.
480 look(Ndbtuple *entry, Ndbtuple *line, char *attr)
484 /* first look on same line (closer binding) */
486 if(cistrcmp(attr, nt->attr) == 0)
492 /* search whole tuple */
493 for(nt = entry; nt; nt = nt->entry)
494 if(cistrcmp(attr, nt->attr) == 0)
499 /* these are answered specially by the tcp version */
501 doaxfr(Ndb *db, char *name)
510 * read the all the soa's from the database to determine area's.
511 * this is only used when we're not caching the database.
519 syslog(0, logfile, "rereading %s", db->file);
521 while(t = ndbparse(db)){
527 * read the database into the cache
530 dbpair2cache(DN *dp, Ndbtuple *entry, Ndbtuple *pair)
537 if(cistrcmp(pair->attr, "ip") == 0){
539 rp = addrrr(entry, pair);
540 } else if(cistrcmp(pair->attr, "ns") == 0){
541 rp = nsrr(entry, pair);
542 } else if(cistrcmp(pair->attr, "soa") == 0){
543 rp = soarr(entry, pair);
544 addarea(dp, rp, pair);
545 } else if(cistrcmp(pair->attr, "mx") == 0){
546 rp = mxrr(entry, pair);
547 } else if(cistrcmp(pair->attr, "cname") == 0){
548 rp = cnamerr(entry, pair);
549 } else if(cistrcmp(pair->attr, "nullrr") == 0){
550 rp = nullrr(entry, pair);
551 } else if(cistrcmp(pair->attr, "txtrr") == 0){
552 rp = txtrr(entry, pair);
560 t = look(entry, pair, "ttl");
562 rp->ttl = atoi(t->val);
566 dbtuple2cache(Ndbtuple *t)
571 for(et = t; et; et = et->entry){
572 if(strcmp(et->attr, "dom") == 0){
573 dp = dnlookup(et->val, Cin, 1);
575 /* first same line */
576 for(nt = et->line; nt != et; nt = nt->line){
577 dbpair2cache(dp, t, nt);
581 /* then rest of entry */
582 for(nt = t; nt; nt = nt->entry){
584 dbpair2cache(dp, t, nt);
591 dbfile2cache(Ndb *db)
596 syslog(0, logfile, "rereading %s", db->file);
598 while(t = ndbparse(db)){
608 ulong youngest, temp;
609 static ulong lastcheck;
610 static ulong lastyoungest;
612 /* no faster than once every 2 minutes */
613 if(now < lastcheck + 2*Min && !doit)
616 refresh_areas(owned);
620 if(opendatabase() < 0){
626 * file may be changing as we are reading it, so loop till
627 * mod times are consistent.
629 * we don't use the times in the ndb records because they may
630 * change outside of refreshing our cached knowledge.
635 for(ndb = db; ndb; ndb = ndb->next){
636 /* the dirfstat avoids walking the mount table each time */
637 if((d = dirfstat(Bfildes(&ndb->b))) != nil ||
638 (d = dirstat(ndb->file)) != nil){
639 temp = d->mtime; /* ulong vs int crap */
645 if(!doit && youngest == lastyoungest){
650 /* forget our area definition */
652 freearea(&delegated);
654 /* reopen all the files (to get oldest for time stamp) */
655 for(ndb = db; ndb; ndb = ndb->next)
659 /* mark all db records as timed out */
662 /* read in new entries */
663 for(ndb = db; ndb; ndb = ndb->next)
666 /* mark as authentic anything in our domain */
669 /* remove old entries */
672 /* read all the soa's to get database defaults */
673 for(ndb = db; ndb; ndb = ndb->next)
678 lastyoungest = youngest;
685 extern uchar ipaddr[IPaddrlen];
691 lookupinfo(char *attr)
697 snprint(buf, sizeof buf, "%I", ipaddr);
701 if(opendatabase() < 0){
705 t = ndbipinfo(db, "ip", buf, a, 1);
710 char *localservers = "local#dns#servers";
711 char *localserverprefix = "local#dns#server";
714 * return non-zero is this is a bad delegation
717 baddelegation(RR *rp, RR *nsrp, uchar *addr)
723 t = lookupinfo("dom");
727 for(; rp; rp = rp->next){
731 /* see if delegation is looping */
733 if(rp->owner != nsrp->owner)
734 if(subsume(rp->owner->name, nsrp->owner->name) &&
735 strcmp(nsrp->owner->name, localservers) != 0){
736 syslog(0, logfile, "delegation loop %R -> %R from %I", nsrp, rp, addr);
740 /* see if delegating to us what we don't own */
741 for(nt = t; nt != nil; nt = nt->entry)
742 if(rp->host && cistrcmp(rp->host->name, nt->val) == 0)
744 if(nt != nil && !inmyarea(rp->owner->name)){
745 syslog(0, logfile, "bad delegation %R from %I", rp, addr);
754 addlocaldnsserver(DN *dp, int class, char *ipaddr, int i)
760 /* ns record for name server, make up an impossible name */
762 snprint(buf, sizeof(buf), "%s%d", localserverprefix, i);
763 nsdp = dnlookup(buf, class, 1);
771 print("dns %s\n", ipaddr);
774 rp->ip = dnlookup(ipaddr, class, 1);
783 * return list of dns server addresses to use when
784 * acting just as a resolver.
787 dnsservers(int class)
796 dp = dnlookup(localservers, class, 1);
797 nsrp = rrlookup(dp, Tns, NOneg);
801 p = getenv("DNSSERVER");
804 n = tokenize(buf, args, nelem(args));
805 for(i = 0; i < n; i++)
806 addlocaldnsserver(dp, class, args[i], i);
809 t = lookupinfo("@dns");
813 for(nt = t; nt != nil; nt = nt->entry){
814 addlocaldnsserver(dp, class, nt->val, i);
820 return rrlookup(dp, Tns, NOneg);
824 addlocaldnsdomain(DN *dp, int class, char *domain)
830 rp->ptr = dnlookup(domain, class, 1);
838 * return list of domains to use when resolving names without '.'s
841 domainlist(int class)
847 dp = dnlookup("local#dns#domains", class, 1);
848 rp = rrlookup(dp, Tptr, NOneg);
852 t = lookupinfo("dnsdomain");
855 for(nt = t; nt != nil; nt = nt->entry)
856 addlocaldnsdomain(dp, class, nt->val);
859 return rrlookup(dp, Tptr, NOneg);
862 char *v4ptrdom = ".in-addr.arpa";
863 char *v6ptrdom = ".ip6.arpa"; /* ip6.int deprecated, rfc 3152 */
871 * create ptrs that are in our areas
880 uchar net[IPaddrlen];
881 uchar mask[IPaddrlen];
885 dlen = strlen(v4ptrdom);
886 for(s = owned; s; s = s->next){
887 len = strlen(s->soarr->owner->name);
890 if(cistrcmp(s->soarr->owner->name+len-dlen, v4ptrdom) != 0)
893 /* get mask and net value */
894 strncpy(buf, s->soarr->owner->name, sizeof(buf));
895 buf[sizeof(buf)-1] = 0;
896 n = getfields(buf, f, nelem(f), 0, ".");
897 memset(mask, 0xff, IPaddrlen);
898 ipmove(net, v4prefix);
901 net[IPv4off] = atoi(f[0]);
907 net[IPv4off] = atoi(f[1]);
908 net[IPv4off+1] = atoi(f[0]);
913 net[IPv4off] = atoi(f[2]);
914 net[IPv4off+1] = atoi(f[1]);
915 net[IPv4off+2] = atoi(f[0]);
918 case 6: /* rfc2317 */
919 net[IPv4off] = atoi(f[3]);
920 net[IPv4off+1] = atoi(f[2]);
921 net[IPv4off+2] = atoi(f[1]);
922 net[IPv4off+3] = atoi(f[0]);
923 sprint(ipa, "%I", net);
924 t = ndbipinfo(db, "ip", ipa, attribs, 1);
925 if(t == nil) /* could be a reverse with no forward */
927 nt = look(t, t, "ipmask");
928 if(nt == nil){ /* we're confused */
932 parseipmask(mask, nt->val);
939 /* go through all domain entries looking for RR's in this network and create ptrs */
940 dnptr(net, mask, s->soarr->owner->name, 6-n, 0);