git: 9front

Download patch

ref: e4632f477bba75f5eb04f4cf791cc652245cce76
parent: 6a617449164c9b7213f21e5b890d21494b4d587f
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Wed Nov 13 13:44:45 EST 2013

ndb/dns: case sensitive ndb attributes, Domlen consistency, dblookup() inplace lower case conversion, cleanups

ndb keys are strictly case sensitive, so consitently
use strcmp() when comparing attribute keys.

dblookup() used to lower case convert its name argument
inplace to match domain/sys name in ndb. better to do
the convesion in its own buffer and only read from the
name argument.

always use cistrcmp() when comparing DN.name.

the Domlen constant denotes the size of the buffer including
the null terminator. consistently use it as such.

have to hold dnlock in freearea() before reading list
head pointer.

--- a/sys/src/cmd/ndb/dblookup.c
+++ b/sys/src/cmd/ndb/dblookup.c
@@ -3,6 +3,7 @@
 #include <bio.h>
 #include <ndb.h>
 #include <ip.h>
+#include <ctype.h>
 #include "dns.h"
 
 enum {
@@ -102,7 +103,7 @@
 dblookup(char *name, int class, int type, int auth, int ttl)
 {
 	int err;
-	char *wild, *cp;
+	char *wild;
 	char buf[256];
 	RR *rp, *tp;
 	DN *dp, *ndp;
@@ -138,16 +139,6 @@
 	if(rp)
 		goto out;
 
-	/* try lower case version */
-	for(cp = name; *cp; cp++)
-		*cp = tolower(*cp);
-	if(cfg.cachedb)
-		rp = rrlookup(dp, type, NOneg);
-	else
-		rp = dblookup1(name, type, auth, ttl);
-	if(rp)
-		goto out;
-
 	/* walk the domain name trying the wildcard '*' at each position */
 	for(wild = strchr(name, '.'); wild; wild = strchr(wild+1, '.')){
 		snprint(buf, sizeof buf, "*%s", wild);
@@ -253,13 +244,24 @@
 	 *  find a matching entry in the database
 	 */
 	t = nil;
-	free(ndbgetvalue(db, &s, "dom", name, attr, &t));
+	nstrcpy(dname, name, sizeof dname);
+	free(ndbgetvalue(db, &s, "dom", dname, attr, &t));
+	if(t == nil && strchr(dname, '.') == nil)
+		free(ndbgetvalue(db, &s, "sys", dname, attr, &t));
+	if(t == nil) {
+		char *cp;
 
-	/*
-	 *  hack for local names
-	 */
-	if(t == nil && strchr(name, '.') == nil)
-		free(ndbgetvalue(db, &s, "sys", name, attr, &t));
+		/* try lower case */
+		for(cp = dname; *cp; cp++)
+			if(isupper(*cp)) {
+				for(; *cp; cp++)
+					*cp = tolower(*cp);
+				free(ndbgetvalue(db, &s, "dom", dname, attr, &t));
+				if(t == nil && strchr(dname, '.') == nil)
+					free(ndbgetvalue(db, &s, "sys", dname, attr, &t));
+				break;
+			}
+	}
 	if(t == nil) {
 //		dnslog("dnlookup1(%s) name not found", name);
 		return nil;
@@ -266,7 +268,6 @@
 	}
 
 	/* search whole entry for default domain name */
-	strncpy(dname, name, sizeof dname);
 	for(nt = t; nt; nt = nt->entry)
 		if(strcmp(nt->attr, "dom") == 0){
 			nstrcpy(dname, nt->val, sizeof dname);
@@ -295,7 +296,7 @@
 			nstrcpy(dname, nt->val, sizeof dname);
 			found = 1;
 		}
-		if(cistrcmp(attr, nt->attr) == 0){
+		if(strcmp(attr, nt->attr) == 0){
 			rp = (*f)(t, nt);
 			rp->auth = auth;
 			rp->db = 1;
@@ -315,7 +316,7 @@
 
 	/* search whole entry */
 	for(nt = t; nt; nt = nt->entry)
-		if(nt->ptr == 0 && cistrcmp(attr, nt->attr) == 0){
+		if(nt->ptr == 0 && strcmp(attr, nt->attr) == 0){
 			rp = (*f)(t, nt);
 			rp->db = 1;
 			if(ttl)
@@ -527,7 +528,7 @@
 
 	/* first look on same line (closer binding) */
 	for(nt = line;;){
-		if(cistrcmp(attr, nt->attr) == 0)
+		if(strcmp(attr, nt->attr) == 0)
 			return nt;
 		nt = nt->line;
 		if(nt == line)
@@ -535,7 +536,7 @@
 	}
 	/* search whole tuple */
 	for(nt = entry; nt; nt = nt->entry)
-		if(cistrcmp(attr, nt->attr) == 0)
+		if(strcmp(attr, nt->attr) == 0)
 			return nt;
 	return 0;
 }
@@ -585,24 +586,26 @@
 	static ulong ord;
 
 	rp = 0;
-	if(cistrcmp(pair->attr, "ip") == 0 ||
-	   cistrcmp(pair->attr, "ipv6") == 0){
+	if(strcmp(pair->attr, "ip") == 0 ||
+	   strcmp(pair->attr, "ipv6") == 0) {
 		dp->ordinal = ord++;
 		rp = addrrr(entry, pair);
-	} else 	if(cistrcmp(pair->attr, "ns") == 0)
+	}
+	else if(strcmp(pair->attr, "ns") == 0)
 		rp = nsrr(entry, pair);
-	else if(cistrcmp(pair->attr, "soa") == 0) {
+	else if(strcmp(pair->attr, "soa") == 0) {
 		rp = soarr(entry, pair);
 		addarea(dp, rp, pair);
-	} else if(cistrcmp(pair->attr, "mx") == 0)
+	}
+	else if(strcmp(pair->attr, "mx") == 0)
 		rp = mxrr(entry, pair);
-	else if(cistrcmp(pair->attr, "srv") == 0)
+	else if(strcmp(pair->attr, "srv") == 0)
 		rp = srvrr(entry, pair);
-	else if(cistrcmp(pair->attr, "cname") == 0)
+	else if(strcmp(pair->attr, "cname") == 0)
 		rp = cnamerr(entry, pair);
-	else if(cistrcmp(pair->attr, "nullrr") == 0)
+	else if(strcmp(pair->attr, "nullrr") == 0)
 		rp = nullrr(entry, pair);
-	else if(cistrcmp(pair->attr, "txtrr") == 0)
+	else if(strcmp(pair->attr, "txtrr") == 0)
 		rp = txtrr(entry, pair);
 	if(rp == nil)
 		return;
@@ -1023,7 +1026,7 @@
 {
 	int len, dlen, n;
 	char *dom;
-	char buf[Domlen+1], ipa[48];
+	char buf[Domlen], ipa[48];
 	char *f[40];
 	uchar net[IPaddrlen], mask[IPaddrlen];
 	Area *s;
@@ -1038,8 +1041,7 @@
 			continue;
 
 		/* get mask and net value */
-		strncpy(buf, dom, sizeof buf);
-		buf[sizeof buf-1] = 0;
+		nstrcpy(buf, dom, sizeof buf);
 		/* buf contains something like 178.204.in-addr.arpa (n==4) */
 		n = getfields(buf, f, nelem(f), 0, ".");
 		memset(mask, 0xff, IPaddrlen);
@@ -1123,7 +1125,7 @@
 {
 	int len, dlen, i, n, pfxnibs;
 	char *dom;
-	char buf[Domlen+1];
+	char buf[Domlen];
 	char *f[40];
 	uchar net[IPaddrlen], mask[IPaddrlen];
 	uchar nibnet[IPaddrlen*2], nibmask[IPaddrlen*2];
@@ -1138,8 +1140,7 @@
 			continue;
 
 		/* get mask and net value */
-		strncpy(buf, dom, sizeof buf);
-		buf[sizeof buf-1] = 0;
+		nstrcpy(buf, dom, sizeof buf);
 		/* buf contains something like 2.0.0.2.ip6.arpa (n==6) */
 		n = getfields(buf, f, nelem(f), 0, ".");
 		pfxnibs = n - 2;		/* 2 for .ip6.arpa */
--- a/sys/src/cmd/ndb/dnarea.c
+++ b/sys/src/cmd/ndb/dnarea.c
@@ -56,7 +56,7 @@
 		l = &owned;
 
 	for (s = *l; s != nil; s = s->next)
-		if (strcmp(dp->name, s->soarr->owner->name) == 0) {
+		if(s->soarr->owner == dp) {
 			unlock(&dnlock);
 			return;		/* we've already got one */
 		}
@@ -89,14 +89,14 @@
 {
 	Area *s;
 
+	lock(&dnlock);
 	while(s = *l){
 		*l = s->next;
-		lock(&dnlock);
 		rrfree(s->soarr);
 		memset(s, 0, sizeof *s);	/* cause trouble */
-		unlock(&dnlock);
 		free(s);
 	}
+	unlock(&dnlock);
 }
 
 /*
--- a/sys/src/cmd/ndb/dns.c
+++ b/sys/src/cmd/ndb/dns.c
@@ -3,7 +3,6 @@
 #include <auth.h>
 #include <fcall.h>
 #include <bio.h>
-#include <ctype.h>
 #include <ip.h>
 #include <pool.h>
 #include "dns.h"
--- a/sys/src/cmd/ndb/dnstcp.c
+++ b/sys/src/cmd/ndb/dnstcp.c
@@ -233,7 +233,7 @@
 	n = strlen(dp->name);
 	if(n < namelen)
 		return 0;
-	if(strcmp(name, dp->name + n - namelen) != 0)
+	if(cistrcmp(name, dp->name + n - namelen) != 0)
 		return 0;
 	if(n > namelen && dp->name[n - namelen - 1] != '.')
 		return 0;
--