]> git.lizzy.rs Git - plan9front.git/blobdiff - sys/src/cmd/ndb/dblookup.c
ndb/dns: do recursive lookup for dnsslave=
[plan9front.git] / sys / src / cmd / ndb / dblookup.c
index 106395a390c5e0bd706ac56146644649097a864f..ec3f94d1cb980073fb2db6cba8c170c89ac8b9ac 100644 (file)
@@ -20,8 +20,11 @@ enum {
        Ptrttl = 2*Min,
 };
 
-static Ndb *db;
-static Lock    dblock;
+static Ndb     *db;
+static QLock   dblock;
+
+static Ipifc   *ipifcs;
+static QLock   ipifclock;
 
 static RR*     addrrr(Ndbtuple*, Ndbtuple*);
 static RR*     cnamerr(Ndbtuple*, Ndbtuple*);
@@ -67,7 +70,7 @@ opendatabase(void)
        char netdbnm[256];
        Ndb *xdb, *netdb;
 
-       if (db)
+       if(db != nil)
                return 0;
 
        xdb = ndbopen(dbfile);          /* /lib/ndb */
@@ -84,7 +87,7 @@ opendatabase(void)
                netdb->nohash = 1;
 
        db = ndbcat(netdb, xdb);        /* both */
-       return db? 0: -1;
+       return db!=nil ? 0: -1;
 }
 
 /*
@@ -122,7 +125,7 @@ dblookup(char *name, int class, int type, int auth, int ttl)
                return rp;
        }
 
-       lock(&dblock);
+       qlock(&dblock);
        dp = idnlookup(name, class, 1);
 
        if(opendatabase() < 0)
@@ -166,7 +169,7 @@ out:
                dp->respcode = err;
        }
 
-       unlock(&dblock);
+       qunlock(&dblock);
        return rp;
 }
 
@@ -254,11 +257,12 @@ dblookup1(char *name, int type, int auth, int ttl)
        /*
         *  find a matching entry in the database
         */
+       t = nil;
        nstrcpy(dname, name, sizeof dname);
        for(x=0; x<4; x++){
                switch(x){
                case 1: /* try unicode */
-                       if(idn2utf(name, dname, sizeof dname) == nil){
+                       if(idn2utf(name, dname, sizeof dname) < 0){
                                nstrcpy(dname, name, sizeof dname);
                                continue;
                        }
@@ -266,7 +270,7 @@ dblookup1(char *name, int type, int auth, int ttl)
                                continue;
                        break;
                case 3: /* try ascii (lower case) */
-                       if(utf2idn(name, dname, sizeof dname) == nil)
+                       if(utf2idn(name, dname, sizeof dname) < 0)
                                continue;
                case 2:
                        mklowcase(dname);
@@ -274,10 +278,23 @@ dblookup1(char *name, int type, int auth, int ttl)
                                continue;
                        break;
                }
-               t = nil;
-               free(ndbgetvalue(db, &s, "dom", dname, attr, &t));
-               if(t == nil && strchr(dname, '.') == nil)
-                       free(ndbgetvalue(db, &s, "sys", dname, attr, &t));
+               for(nt = ndbsearch(db, &s, "dom", dname); nt != nil; nt = ndbsnext(&s, "dom", dname)) {
+                       if(ndbfindattr(nt, s.t, attr) == nil) {
+                               ndbfree(nt);
+                               continue;
+                       }
+                       t = ndbconcatenate(t, ndbreorder(nt, s.t));
+               }
+               if(t == nil && strchr(dname, '.') == nil) {
+                       for(nt = ndbsearch(db, &s, "sys", dname); nt != nil; nt = ndbsnext(&s, "sys", dname)) {
+                               if(ndbfindattr(nt, s.t, attr) == nil) {
+                                       ndbfree(nt);
+                                       continue;
+                               }
+                               t = ndbconcatenate(t, ndbreorder(nt, s.t));
+                       }
+               }
+               s.t = t;
                if(t != nil)
                        break;
        }
@@ -287,6 +304,7 @@ dblookup1(char *name, int type, int auth, int ttl)
                return nil;
        }
 
+
        /* search whole entry for default domain name */
        for(nt = t; nt; nt = nt->entry)
                if(strcmp(nt->attr, "dom") == 0){
@@ -316,8 +334,7 @@ dblookup1(char *name, int type, int auth, int ttl)
                        nstrcpy(dname, nt->val, sizeof dname);
                        found = 1;
                }
-               if(strcmp(attr, nt->attr) == 0){
-                       rp = (*f)(t, nt);
+               if(strcmp(attr, nt->attr) == 0 && (rp = (*f)(t, nt)) != nil){
                        rp->auth = auth;
                        rp->db = 1;
                        if(ttl)
@@ -336,12 +353,11 @@ dblookup1(char *name, int type, int auth, int ttl)
 
        /* search whole entry */
        for(nt = t; nt; nt = nt->entry)
-               if(nt->ptr == 0 && strcmp(attr, nt->attr) == 0){
-                       rp = (*f)(t, nt);
+               if(nt->ptr == 0 && strcmp(attr, nt->attr) == 0 && (rp = (*f)(t, nt)) != nil){
+                       rp->auth = auth;
                        rp->db = 1;
                        if(ttl)
                                rp->ttl = ttl;
-                       rp->auth = auth;
                        if(dp == nil)
                                dp = idnlookup(dname, Cin, 1);
                        rp->owner = dp;
@@ -358,26 +374,22 @@ dblookup1(char *name, int type, int auth, int ttl)
  *  make various types of resource records from a database entry
  */
 static RR*
-addrrr(Ndbtuple *entry, Ndbtuple *pair)
+addrrr(Ndbtuple*, Ndbtuple *pair)
 {
        RR *rp;
-       uchar addr[IPaddrlen];
+       uchar ip[IPaddrlen];
 
-       USED(entry);
-       parseip(addr, pair->val);
-       if(isv4(addr))
-               rp = rralloc(Ta);
-       else
-               rp = rralloc(Taaaa);
-       rp->ip = dnlookup(pair->val, Cin, 1);
+       if(parseip(ip, pair->val) == -1)
+               return nil;
+       rp = rralloc(isv4(ip) ? Ta : Taaaa);
+       rp->ip = ipalookup(ip, Cin, 1);
        return rp;
 }
 static RR*
-nullrr(Ndbtuple *entry, Ndbtuple *pair)
+nullrr(Ndbtuple*, Ndbtuple *pair)
 {
        RR *rp;
 
-       USED(entry);
        rp = rralloc(Tnull);
        rp->null->data = (uchar*)estrdup(pair->val);
        rp->null->dlen = strlen((char*)rp->null->data);
@@ -389,13 +401,12 @@ nullrr(Ndbtuple *entry, Ndbtuple *pair)
  *  <= 255 byte ones.
  */
 static RR*
-txtrr(Ndbtuple *entry, Ndbtuple *pair)
+txtrr(Ndbtuple*, Ndbtuple *pair)
 {
        RR *rp;
        Txt *t, **l;
        int i, len, sofar;
 
-       USED(entry);
        rp = rralloc(Ttxt);
        l = &rp->txt;
        rp->txt = nil;
@@ -420,11 +431,10 @@ txtrr(Ndbtuple *entry, Ndbtuple *pair)
        return rp;
 }
 static RR*
-cnamerr(Ndbtuple *entry, Ndbtuple *pair)
+cnamerr(Ndbtuple*, Ndbtuple *pair)
 {
        RR *rp;
 
-       USED(entry);
        rp = rralloc(Tcname);
        rp->host = idnlookup(pair->val, Cin, 1);
        return rp;
@@ -453,11 +463,10 @@ nsrr(Ndbtuple *entry, Ndbtuple *pair)
        return rp;
 }
 static RR*
-ptrrr(Ndbtuple *entry, Ndbtuple *pair)
+ptrrr(Ndbtuple*, Ndbtuple *pair)
 {
        RR *rp;
 
-       USED(entry);
        rp = rralloc(Tns);
        rp->ptr = dnlookup(pair->val, Cin, 1);
        return rp;
@@ -561,16 +570,6 @@ look(Ndbtuple *entry, Ndbtuple *line, char *attr)
        return 0;
 }
 
-static RR**
-linkrr(RR *rp, DN *dp, RR **l)
-{
-       rp->owner = dp;
-       rp->auth = 1;
-       rp->db = 1;
-       *l = rp;
-       return &rp->next;
-}
-
 /* these are answered specially by the tcp version */
 static RR*
 doaxfr(Ndb *db, char *name)
@@ -694,13 +693,16 @@ db2cache(int doit)
 
        refresh_areas(owned);
 
-       lock(&dblock);
-
+       qlock(&dblock);
        if(opendatabase() < 0){
-               unlock(&dblock);
+               qunlock(&dblock);
                return;
        }
 
+       qlock(&ipifclock);
+       ipifcs = readipifc(mntpt, ipifcs, -1);
+       qunlock(&ipifclock);
+
        /*
         *  file may be changing as we are reading it, so loop till
         *  mod times are consistent.
@@ -756,11 +758,10 @@ db2cache(int doit)
                createptrs();
        }
 
-       unlock(&dblock);
+       qunlock(&dblock);
 }
 
 extern char    mntpt[Maxpath];         /* net mountpoint */
-static uchar   ipaddr[IPaddrlen];      /* my ip address */
 
 /*
  *  get all my xxx
@@ -769,25 +770,31 @@ static uchar      ipaddr[IPaddrlen];      /* my ip address */
 Ndbtuple*
 lookupinfo(char *attr)
 {
-       char buf[64];
-       char *a[2];
-       Ndbtuple *t;
-
-       if(ipcmp(ipaddr, IPnoaddr) == 0)
-               if(myipaddr(ipaddr, mntpt) < 0)
-                       return nil;
-
-       snprint(buf, sizeof buf, "%I", ipaddr);
-       a[0] = attr;
+       Ndbtuple *t, *nt;
+       char ip[64];
+       Ipifc *ifc;
+       Iplifc *lifc;
 
-       lock(&dblock);
+       t = nil;
+       qlock(&dblock);
        if(opendatabase() < 0){
-               unlock(&dblock);
+               qunlock(&dblock);
                return nil;
        }
-       t = ndbipinfo(db, "ip", buf, a, 1);
-       unlock(&dblock);
-       return t;
+       qlock(&ipifclock);
+       if(ipifcs == nil)
+               ipifcs = readipifc(mntpt, ipifcs, -1);
+       for(ifc = ipifcs; ifc != nil; ifc = ifc->next){
+               for(lifc = ifc->lifc; lifc != nil; lifc = lifc->next){
+                       snprint(ip, sizeof(ip), "%I", lifc->ip);
+                       nt = ndbipinfo(db, "ip", ip, &attr, 1);
+                       t = ndbconcatenate(t, nt);
+               }
+       }
+       qunlock(&ipifclock);
+       qunlock(&dblock);
+
+       return ndbdedup(t);
 }
 
 /*
@@ -825,75 +832,71 @@ baddelegation(RR *rp, RR *nsrp, uchar *addr)
 }
 
 int
-myaddr(char *addr)
+myip(uchar *ip)
 {
-       char *line, *sp;
-       char buf[64];
-       Biobuf *bp;
-
-       if(ipcmp(ipaddr, IPnoaddr) == 0)
-               if(myipaddr(ipaddr, mntpt) < 0)
-                       return -1;
-
-       snprint(buf, sizeof buf, "%I", ipaddr);
-       if (strcmp(addr, buf) == 0) {
-               dnslog("rejecting my ip %s as local dns server", addr);
-               return 1;
-       }
-
-       snprint(buf, sizeof buf, "%s/ipselftab", mntpt);
-       bp = Bopen(buf, OREAD);
-       if (bp != nil) {
-               while ((line = Brdline(bp, '\n')) != nil) {
-                       line[Blinelen(bp) - 1] = '\0';
-                       sp = strchr(line, ' ');
-                       if (sp) {
-                               *sp = '\0';
-                               if (strcmp(addr, line) == 0) {
-                                       dnslog("rejecting my ip %s as local dns server",
-                                               addr);
-                                       return 1;
-                               }
+       Ipifc *ifc;
+       Iplifc *lifc;
+
+       qlock(&ipifclock);
+       for(ifc = ipifcs; ifc != nil; ifc = ifc->next){
+               for(lifc = ifc->lifc; lifc != nil; lifc = lifc->next){
+                       if(ipcmp(ip, lifc->ip) == 0){
+                               qunlock(&ipifclock);
+                               return 1;
                        }
                }
-               Bterm(bp);
        }
+       qunlock(&ipifclock);
+
        return 0;
 }
 
-static char *locdns[20];
-static QLock locdnslck;
-
 static void
-addlocaldnsserver(DN *dp, int class, char *ipaddr, int i)
+addlocaldnsserver(DN *dp, int class, char *addr, int i)
 {
-       int n;
-       DN *nsdp;
-       RR *rp;
-       char buf[32];
        uchar ip[IPaddrlen];
+       DN *nsdp, *ipdp;
+       RR *rp, *tp;
+       int type, n;
+       char buf[32];
+
+       if(parseip(ip, addr) == -1 || ipcmp(ip, IPnoaddr) == 0){
+               dnslog("rejecting bad ip %s as local dns server", addr);
+               return;
+       }
 
        /* reject our own ip addresses so we don't query ourselves via udp */
-       if (myaddr(ipaddr))
+       if(myip(ip)){
+               dnslog("rejecting my ip %I as local dns server", ip);
                return;
+       }
 
-       qlock(&locdnslck);
-       for (n = 0; n < i && n < nelem(locdns) && locdns[n]; n++)
-               if (strcmp(locdns[n], ipaddr) == 0) {
-                       dnslog("rejecting duplicate local dns server ip %s",
-                               ipaddr);
-                       qunlock(&locdnslck);
-                       return;
+       /* A or AAAA record */
+       type = isv4(ip) ? Ta : Taaaa;
+       ipdp = ipalookup(ip, class, 1);
+
+       /* check duplicate ip */
+       for(n = 0; n < i; n++){
+               snprint(buf, sizeof buf, "local#dns#server%d", n);
+               nsdp = dnlookup(buf, class, 0);
+               if(nsdp == nil)
+                       continue;
+               rp = rrlookup(nsdp, type, NOneg);
+               for(tp = rp; tp != nil; tp = tp->next){
+                       if(tp->ip == ipdp){
+                               dnslog("rejecting duplicate local dns server ip %I", ip);
+                               rrfreelist(rp);
+                               return;
+                       }
                }
-       if (n < nelem(locdns))
-               if (locdns[n] == nil || ++n < nelem(locdns))
-                       locdns[n] = strdup(ipaddr); /* remember 1st few local ns */
-       qunlock(&locdnslck);
+               rrfreelist(rp);
+       }
 
-       /* ns record for name server, make up an impossible name */
-       rp = rralloc(Tns);
        snprint(buf, sizeof buf, "local#dns#server%d", i);
        nsdp = dnlookup(buf, class, 1);
+
+       /* ns record for name server, make up an impossible name */
+       rp = rralloc(Tns);
        rp->host = nsdp;
        rp->owner = dp;                 /* e.g., local#dns#servers */
        rp->local = 1;
@@ -902,12 +905,8 @@ addlocaldnsserver(DN *dp, int class, char *ipaddr, int i)
        rrattach(rp, Authoritative);    /* will not attach rrs in my area */
        dnagenever(dp);
 
-       /* A or AAAA record */
-       if (parseip(ip, ipaddr) >= 0 && isv4(ip))
-               rp = rralloc(Ta);
-       else
-               rp = rralloc(Taaaa);
-       rp->ip = dnlookup(ipaddr, class, 1);
+       rp = rralloc(type);
+       rp->ip = ipdp;
        rp->owner = nsdp;
        rp->local = 1;
        rp->db = 1;
@@ -915,7 +914,7 @@ addlocaldnsserver(DN *dp, int class, char *ipaddr, int i)
        rrattach(rp, Authoritative);    /* will not attach rrs in my area */
        dnagenever(nsdp);
 
-       dnslog("added local dns server %s at %s", buf, ipaddr);
+       dnslog("added local dns server %s at %I", buf, ip);
 }
 
 /*
@@ -927,7 +926,7 @@ dnsservers(int class)
 {
        int i, n;
        char *p;
-       char *args[5];
+       char *args[16];
        Ndbtuple *t, *nt;
        RR *nsrp;
        DN *dp;
@@ -1184,11 +1183,11 @@ insideaddr(char *dom)
        if (dom[0] == '\0' || strcmp(dom, ".") == 0)    /* dns root? */
                return 1;                       /* hack for initialisation */
 
-       lock(&dblock);
+       qlock(&dblock);
        if (indoms == nil)
                loaddomsrvs();
        if (indoms == nil) {
-               unlock(&dblock);
+               qunlock(&dblock);
                return 1;  /* no "inside-dom" sys, try inside nameservers */
        }
 
@@ -1206,7 +1205,7 @@ insideaddr(char *dom)
                        break;
                }
        }
-       unlock(&dblock);
+       qunlock(&dblock);
        return rv;
 }
 
@@ -1218,8 +1217,7 @@ insidens(uchar *ip)
 
        for (t = innmsrvs; t != nil; t = t->entry)
                if (strcmp(t->attr, "ip") == 0) {
-                       parseip(ipa, t->val);
-                       if (memcmp(ipa, ip, sizeof ipa) == 0)
+                       if (parseip(ipa, t->val) != -1 && ipcmp(ipa, ip) == 0)
                                return 1;
                }
        return 0;
@@ -1234,7 +1232,8 @@ outsidensip(int n, uchar *ip)
        i = 0;
        for (t = outnmsrvs; t != nil; t = t->entry)
                if (strcmp(t->attr, "ip") == 0 && i++ == n) {
-                       parseip(ip, t->val);
+                       if (parseip(ip, t->val) == -1)
+                               return -1;
                        return 0;
                }
        return -1;