shithub: riscv

Download patch

ref: 6bc544f3da5bc40abca38e5dc652f999d2162847
parent: a79465e118a0469bbda928f86b9eef6b42d18593
author: cinap_lenrek <[email protected]>
date: Sun Oct 22 15:22:21 EDT 2023

ndb/dns: refresh the cached dnsserver domain ndb entries in db2cache()

baddelegation() kept a cache of Ndbtuples
for its own machine's domain names that
was never refreshed.

now update that cache at the end of db2cache().

--- a/sys/src/cmd/ndb/dblookup.c
+++ b/sys/src/cmd/ndb/dblookup.c
@@ -21,6 +21,7 @@
 };
 
 static Ndb	*db;
+static Ndbtuple	*mydoms;
 static QLock	dblock;
 
 static Ipifc	*ipifcs;
@@ -744,6 +745,40 @@
 		getpid());
 }
 
+/*
+ *  get all my xxx
+ *  caller ndbfrees the result
+ */
+Ndbtuple*
+lookupinfo(char *attr)
+{
+	Ndbtuple *t, *nt;
+	char ip[64];
+	Ipifc *ifc;
+	Iplifc *lifc;
+
+	t = nil;
+	qlock(&dblock);
+	if(opendatabase() < 0){
+		qunlock(&dblock);
+		return nil;
+	}
+	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);
+}
+
 void
 db2cache(int doit)
 {
@@ -750,6 +785,7 @@
 	ulong youngest;
 	Ndb *ndb;
 	Dir *d;
+	static Ndbtuple *olddoms;
 	static ulong lastcheck, lastyoungest;
 
 	/* no faster than once every 2 minutes */
@@ -819,44 +855,11 @@
 		lastyoungest = youngest;
 		createptrs();
 	}
-
 	qunlock(&dblock);
-}
 
-extern char	mntpt[Maxpath];		/* net mountpoint */
-
-/*
- *  get all my xxx
- *  caller ndbfrees the result
- */
-Ndbtuple*
-lookupinfo(char *attr)
-{
-	Ndbtuple *t, *nt;
-	char ip[64];
-	Ipifc *ifc;
-	Iplifc *lifc;
-
-	t = nil;
-	qlock(&dblock);
-	if(opendatabase() < 0){
-		qunlock(&dblock);
-		return nil;
-	}
-	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);
+	ndbfree(olddoms);
+	olddoms = mydoms;
+	mydoms = lookupinfo("dom");
 }
 
 /*
@@ -865,32 +868,20 @@
 int
 baddelegation(RR *rp, RR *nsrp, uchar *addr)
 {
-	static int whined;
-	static Ndbtuple *t;
 	Ndbtuple *nt;
 
 	if(rp->type != Tns)
 		return 0;
-
-	if(t == nil)
-		t = lookupinfo("dom");
-	if(t != nil){
-		/* see if delegating to us what we don't own */
-		for(nt = t; nt != nil; nt = nt->entry)
-			if(rp->host && cistrcmp(rp->host->name, nt->val) == 0)
-				break;
-
-		if(nt != nil && !inmyarea(rp->owner->name)){
-			if (!whined) {
-				whined = 1;
-				dnslog("bad delegation %R from %I/%s; "
-					"no further logging of them",
-					rp, addr, nsrp->host->name);
-			}
-			return 1;
-		}
-	}
-	return 0;
+	/* see if delegating to us what we don't own */
+	for(nt = mydoms; nt != nil; nt = nt->entry)
+		if(rp->host && cistrcmp(rp->host->name, nt->val) == 0)
+			break;
+	if(nt == nil || inmyarea(rp->owner->name))
+		return 0;
+	dnslog("bad delegation %R from %I/%s; "
+		"no further logging of them",
+		rp, addr, nsrp->host->name);
+	return 1;
 }
 
 int