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] =
37 nstrcpy(char *to, char *from, int len)
39 strncpy(to, from, len);
50 snprint(buf, sizeof(buf), "%s/ndb", mntpt);
51 xdb = ndbopen(dbfile);
54 db = ndbcat(ndbopen(buf), xdb);
63 * lookup an RR in the network database, look for matches
64 * against both the domain name and the wildcarded domain name.
66 * the lock makes sure only one process can be accessing the data
67 * base at a time. This is important since there's a lot of
70 * e.g. for x.research.bell-labs.com, first look for a match against
71 * the x.research.bell-labs.com. If nothing matches, try *.research.bell-labs.com.
74 dblookup(char *name, int class, int type, int auth, int ttl)
85 /* so far only internet lookups are implemented */
93 for (type = Ta; type < Tall; type++)
95 rrcat(&rp, dblookup(name, class, type, auth, ttl));
100 dp = dnlookup(name, class, 1);
101 if(opendatabase() < 0)
106 /* first try the given name */
109 rp = rrlookup(dp, type, NOneg);
111 rp = dblookup1(name, type, auth, ttl);
115 /* try lower case version */
116 for(cp = name; *cp; cp++)
119 rp = rrlookup(dp, type, NOneg);
121 rp = dblookup1(name, type, auth, ttl);
125 /* walk the domain name trying the wildcard '*' at each position */
126 for(wild = strchr(name, '.'); wild; wild = strchr(wild+1, '.')){
127 snprint(buf, sizeof(buf), "*%s", wild);
128 ndp = dnlookup(buf, class, 1);
132 rp = rrlookup(ndp, type, NOneg);
134 rp = dblookup1(buf, type, auth, ttl);
139 /* add owner to uncached records */
141 for(tp = rp; tp; tp = tp->next)
144 /* don't call it non-existent if it's not ours */
145 if(err == Rname && !inmyarea(name))
147 dp->nonexistent = err;
155 * lookup an RR in the network database
158 dblookup1(char *name, int type, int auth, int ttl)
166 RR *(*f)(Ndbtuple*, Ndbtuple*);
201 return doaxfr(db, name);
207 * find a matching entry in the database
209 free(ndbgetvalue(db, &s, "dom", name, attr, &t));
212 * hack for local names
214 if(t == 0 && strchr(name, '.') == 0)
215 free(ndbgetvalue(db, &s, "sys", name, attr, &t));
219 /* search whole entry for default domain name */
220 strncpy(dname, name, sizeof dname);
221 for(nt = t; nt; nt = nt->entry)
222 if(strcmp(nt->attr, "dom") == 0){
223 nstrcpy(dname, nt->val, sizeof dname);
227 /* ttl is maximum of soa minttl and entry's ttl ala rfc883 */
228 nt = look(t, s.t, "ttl");
235 /* default ttl is one day */
240 * The database has 2 levels of precedence; line and entry.
241 * Pairs on the same line bind tighter than pairs in the
242 * same entry, so we search the line first.
248 if(found == 0 && strcmp(nt->attr, "dom") == 0){
249 nstrcpy(dname, nt->val, sizeof dname);
252 if(cistrcmp(attr, nt->attr) == 0){
259 dp = dnlookup(dname, Cin, 1);
270 /* search whole entry */
271 for(nt = t; nt; nt = nt->entry)
272 if(nt->ptr == 0 && cistrcmp(attr, nt->attr) == 0){
279 dp = dnlookup(dname, Cin, 1);
290 * make various types of resource records from a database entry
293 addrrr(Ndbtuple *entry, Ndbtuple *pair)
296 uchar addr[IPaddrlen];
299 parseip(addr, pair->val);
304 rp->ip = dnlookup(pair->val, Cin, 1);
308 nullrr(Ndbtuple *entry, Ndbtuple *pair)
314 rp->null->data = (uchar*)estrdup(pair->val);
315 rp->null->dlen = strlen((char*)rp->null->data);
319 * txt rr strings are at most 255 bytes long. one
320 * can represent longer strings by multiple concatenated
324 txtrr(Ndbtuple *entry, Ndbtuple *pair)
334 len = strlen(pair->val);
337 t = emalloc(sizeof(*t));
345 memmove(t->p, pair->val+sofar, i);
355 cnamerr(Ndbtuple *entry, Ndbtuple *pair)
360 rp = rralloc(Tcname);
361 rp->host = dnlookup(pair->val, Cin, 1);
365 mxrr(Ndbtuple *entry, Ndbtuple *pair)
370 rp->host = dnlookup(pair->val, Cin, 1);
371 pair = look(entry, pair, "pref");
373 rp->pref = atoi(pair->val);
379 nsrr(Ndbtuple *entry, Ndbtuple *pair)
385 rp->host = dnlookup(pair->val, Cin, 1);
386 t = look(entry, pair, "soa");
387 if(t && t->val[0] == 0)
392 ptrrr(Ndbtuple *entry, Ndbtuple *pair)
398 rp->ptr = dnlookup(pair->val, Cin, 1);
402 soarr(Ndbtuple *entry, Ndbtuple *pair)
405 Ndbtuple *ns, *mb, *t;
406 char mailbox[Domlen];
412 for(ndb = db; ndb; ndb = ndb->next)
413 if(ndb->mtime > rp->soa->serial)
414 rp->soa->serial = ndb->mtime;
415 rp->soa->refresh = Day;
416 rp->soa->retry = Hour;
417 rp->soa->expire = Day;
418 rp->soa->minttl = Day;
419 t = look(entry, pair, "ttl");
421 rp->soa->minttl = atoi(t->val);
422 t = look(entry, pair, "refresh");
424 rp->soa->refresh = atoi(t->val);
425 t = look(entry, pair, "serial");
427 rp->soa->serial = strtoul(t->val, 0, 10);
429 ns = look(entry, pair, "ns");
431 ns = look(entry, pair, "dom");
432 rp->host = dnlookup(ns->val, Cin, 1);
436 * mbox=person@machine.dom
437 * mbox=person.machine.dom
439 mb = look(entry, pair, "mbox");
441 mb = look(entry, pair, "mb");
443 if(strchr(mb->val, '.')) {
444 p = strchr(mb->val, '@');
447 rp->rmb = dnlookup(mb->val, Cin, 1);
449 snprint(mailbox, sizeof(mailbox), "%s.%s",
451 rp->rmb = dnlookup(mailbox, Cin, 1);
454 snprint(mailbox, sizeof(mailbox), "postmaster.%s",
456 rp->rmb = dnlookup(mailbox, Cin, 1);
459 /* hang dns slaves off of the soa. this is
460 * for managing the area.
462 for(t = entry; t != nil; t = t->entry)
463 if(strcmp(t->attr, "dnsslave") == 0)
464 addserver(&rp->soa->slaves, t->val);
470 * Look for a pair with the given attribute. look first on the same line,
471 * then in the whole entry.
474 look(Ndbtuple *entry, Ndbtuple *line, char *attr)
478 /* first look on same line (closer binding) */
480 if(cistrcmp(attr, nt->attr) == 0)
486 /* search whole tuple */
487 for(nt = entry; nt; nt = nt->entry)
488 if(cistrcmp(attr, nt->attr) == 0)
494 linkrr(RR *rp, DN *dp, RR **l)
503 /* these are answered specially by the tcp version */
505 doaxfr(Ndb *db, char *name)
514 * read the all the soa's from the database to determine area's.
515 * this is only used when we're not caching the database.
523 syslog(0, logfile, "rereading %s", db->file);
525 while(t = ndbparse(db)){
531 * read the database into the cache
534 dbpair2cache(DN *dp, Ndbtuple *entry, Ndbtuple *pair)
541 if(cistrcmp(pair->attr, "ip") == 0){
543 rp = addrrr(entry, pair);
544 } else if(cistrcmp(pair->attr, "ns") == 0){
545 rp = nsrr(entry, pair);
546 } else if(cistrcmp(pair->attr, "soa") == 0){
547 rp = soarr(entry, pair);
548 addarea(dp, rp, pair);
549 } else if(cistrcmp(pair->attr, "mx") == 0){
550 rp = mxrr(entry, pair);
551 } else if(cistrcmp(pair->attr, "cname") == 0){
552 rp = cnamerr(entry, pair);
553 } else if(cistrcmp(pair->attr, "nullrr") == 0){
554 rp = nullrr(entry, pair);
555 } else if(cistrcmp(pair->attr, "txtrr") == 0){
556 rp = txtrr(entry, pair);
564 t = look(entry, pair, "ttl");
566 rp->ttl = atoi(t->val);
570 dbtuple2cache(Ndbtuple *t)
575 for(et = t; et; et = et->entry){
576 if(strcmp(et->attr, "dom") == 0){
577 dp = dnlookup(et->val, Cin, 1);
579 /* first same line */
580 for(nt = et->line; nt != et; nt = nt->line){
581 dbpair2cache(dp, t, nt);
585 /* then rest of entry */
586 for(nt = t; nt; nt = nt->entry){
588 dbpair2cache(dp, t, nt);
595 dbfile2cache(Ndb *db)
600 syslog(0, logfile, "rereading %s", db->file);
602 while(t = ndbparse(db)){
612 ulong youngest, temp;
613 static ulong lastcheck;
614 static ulong lastyoungest;
616 /* no faster than once every 2 minutes */
617 if(now < lastcheck + 2*Min && !doit)
620 refresh_areas(owned);
624 if(opendatabase() < 0){
630 * file may be changing as we are reading it, so loop till
631 * mod times are consistent.
633 * we don't use the times in the ndb records because they may
634 * change outside of refreshing our cached knowledge.
639 for(ndb = db; ndb; ndb = ndb->next){
640 /* the dirfstat avoids walking the mount table each time */
641 if((d = dirfstat(Bfildes(&ndb->b))) != nil ||
642 (d = dirstat(ndb->file)) != nil){
643 temp = d->mtime; /* ulong vs int crap */
649 if(!doit && youngest == lastyoungest){
654 /* forget our area definition */
656 freearea(&delegated);
658 /* reopen all the files (to get oldest for time stamp) */
659 for(ndb = db; ndb; ndb = ndb->next)
663 /* mark all db records as timed out */
666 /* read in new entries */
667 for(ndb = db; ndb; ndb = ndb->next)
670 /* mark as authentic anything in our domain */
673 /* remove old entries */
676 /* read all the soa's to get database defaults */
677 for(ndb = db; ndb; ndb = ndb->next)
682 lastyoungest = youngest;
689 extern uchar ipaddr[IPaddrlen];
695 lookupinfo(char *attr)
701 snprint(buf, sizeof buf, "%I", ipaddr);
705 if(opendatabase() < 0){
709 t = ndbipinfo(db, "ip", buf, a, 1);
714 char *localservers = "local#dns#servers";
715 char *localserverprefix = "local#dns#server";
718 * return non-zero is this is a bad delegation
721 baddelegation(RR *rp, RR *nsrp, uchar *addr)
727 t = lookupinfo("dom");
731 for(; rp; rp = rp->next){
735 /* see if delegation is looping */
737 if(rp->owner != nsrp->owner)
738 if(subsume(rp->owner->name, nsrp->owner->name) &&
739 strcmp(nsrp->owner->name, localservers) != 0){
740 syslog(0, logfile, "delegation loop %R -> %R from %I", nsrp, rp, addr);
744 /* see if delegating to us what we don't own */
745 for(nt = t; nt != nil; nt = nt->entry)
746 if(rp->host && cistrcmp(rp->host->name, nt->val) == 0)
748 if(nt != nil && !inmyarea(rp->owner->name)){
749 syslog(0, logfile, "bad delegation %R from %I", rp, addr);
758 addlocaldnsserver(DN *dp, int class, char *ipaddr, int i)
764 /* ns record for name server, make up an impossible name */
766 snprint(buf, sizeof(buf), "%s%d", localserverprefix, i);
767 nsdp = dnlookup(buf, class, 1);
775 print("dns %s\n", ipaddr);
778 rp->ip = dnlookup(ipaddr, class, 1);
787 * return list of dns server addresses to use when
788 * acting just as a resolver.
791 dnsservers(int class)
800 dp = dnlookup(localservers, class, 1);
801 nsrp = rrlookup(dp, Tns, NOneg);
805 p = getenv("DNSSERVER");
808 n = tokenize(buf, args, nelem(args));
809 for(i = 0; i < n; i++)
810 addlocaldnsserver(dp, class, args[i], i);
813 t = lookupinfo("@dns");
817 for(nt = t; nt != nil; nt = nt->entry){
818 addlocaldnsserver(dp, class, nt->val, i);
824 return rrlookup(dp, Tns, NOneg);
828 addlocaldnsdomain(DN *dp, int class, char *domain)
834 rp->ptr = dnlookup(domain, class, 1);
842 * return list of domains to use when resolving names without '.'s
845 domainlist(int class)
851 dp = dnlookup("local#dns#domains", class, 1);
852 rp = rrlookup(dp, Tptr, NOneg);
856 t = lookupinfo("dnsdomain");
859 for(nt = t; nt != nil; nt = nt->entry)
860 addlocaldnsdomain(dp, class, nt->val);
863 return rrlookup(dp, Tptr, NOneg);
866 char *v4ptrdom = ".in-addr.arpa";
867 char *v6ptrdom = ".ip6.arpa"; /* ip6.int deprecated, rfc 3152 */
875 * create ptrs that are in our areas
884 uchar net[IPaddrlen];
885 uchar mask[IPaddrlen];
889 dlen = strlen(v4ptrdom);
890 for(s = owned; s; s = s->next){
891 len = strlen(s->soarr->owner->name);
894 if(cistrcmp(s->soarr->owner->name+len-dlen, v4ptrdom) != 0)
897 /* get mask and net value */
898 strncpy(buf, s->soarr->owner->name, sizeof(buf));
899 buf[sizeof(buf)-1] = 0;
900 n = getfields(buf, f, nelem(f), 0, ".");
901 memset(mask, 0xff, IPaddrlen);
902 ipmove(net, v4prefix);
905 net[IPv4off] = atoi(f[0]);
911 net[IPv4off] = atoi(f[1]);
912 net[IPv4off+1] = atoi(f[0]);
917 net[IPv4off] = atoi(f[2]);
918 net[IPv4off+1] = atoi(f[1]);
919 net[IPv4off+2] = atoi(f[0]);
922 case 6: /* rfc2317 */
923 net[IPv4off] = atoi(f[3]);
924 net[IPv4off+1] = atoi(f[2]);
925 net[IPv4off+2] = atoi(f[1]);
926 net[IPv4off+3] = atoi(f[0]);
927 sprint(ipa, "%I", net);
928 t = ndbipinfo(db, "ip", ipa, attribs, 1);
929 if(t == nil) /* could be a reverse with no forward */
931 nt = look(t, t, "ipmask");
932 if(nt == nil){ /* we're confused */
936 parseipmask(mask, nt->val);
943 /* go through all domain entries looking for RR's in this network and create ptrs */
944 dnptr(net, mask, s->soarr->owner->name, 6-n, 0);