lametoy.patch 2010.04.17

patch placed into the public domain by Matthew Dempsky 2010.04.17

 cleaned version of http://marc.info/?l=djbdns&m=127113010816128&w=2
 tinydns normally doesnt respond at all when asked a question for a domain
 that's not in it's data.cdb.  this patch lets you give answers for
 those domains.  set /etc/tinydns/env/LAME_A LAME_TXT LAME_MX to the
 responses you want to give on these domains.  tinydns will log answers
 from this mechanism with a "B" (for bogus).
 



###


diff -Nru djbdns-1.05.orig/Makefile djbdns-1.05/Makefile
--- djbdns-1.05.orig/Makefile	2010-04-16 23:22:16.000000000 -0700
+++ djbdns-1.05/Makefile	2010-04-16 23:54:47.000000000 -0700
@@ -1020,9 +1020,9 @@
 
 tinydns-get: \
 load tinydns-get.o tdlookup.o response.o printpacket.o printrecord.o \
-parsetype.o dns.a libtai.a cdb.a buffer.a alloc.a unix.a byte.a
+parsetype.o dns.a env.a libtai.a cdb.a buffer.a alloc.a unix.a byte.a
 	./load tinydns-get tdlookup.o response.o printpacket.o \
-	printrecord.o parsetype.o dns.a libtai.a cdb.a buffer.a \
+	printrecord.o parsetype.o dns.a env.a libtai.a cdb.a buffer.a \
 	alloc.a unix.a byte.a 
 
 tinydns-get.o: \
diff -Nru djbdns-1.05.orig/axfrdns.c djbdns-1.05/axfrdns.c
--- djbdns-1.05.orig/axfrdns.c	2010-04-16 23:22:16.000000000 -0700
+++ djbdns-1.05/axfrdns.c	2010-04-16 23:51:42.000000000 -0700
@@ -324,6 +324,7 @@
 
   droproot(FATAL);
   dns_random_init(seed);
+  if (!tdlookup_init()) strerr_die2sys(111,FATAL,"tdlookup_init failed");
 
   axfr = env_get("AXFR");
   
diff -Nru djbdns-1.05.orig/server.c djbdns-1.05/server.c
--- djbdns-1.05.orig/server.c	2010-04-16 23:22:16.000000000 -0700
+++ djbdns-1.05/server.c	2010-04-16 23:57:17.000000000 -0700
@@ -55,12 +55,11 @@
   if (byte_equal(qtype,2,DNS_T_AXFR)) goto NOTIMP;
 
   case_lowerb(q,dns_domain_length(q));
-  if (!respond(q,qtype,ip)) {
-    qlog(ip,port,header,q,qtype," - ");
-    return 0;
+  switch(respond(q,qtype,ip)) {
+    case 0: qlog(ip,port,header,q,qtype," - "); return 0;
+    case 2: qlog(ip,port,header,q,qtype," B "); return 1;
+    default: qlog(ip,port,header,q,qtype," + "); return 1;
   }
-  qlog(ip,port,header,q,qtype," + ");
-  return 1;
 
   NOTIMP:
   response[3] &= ~15;
diff -Nru djbdns-1.05.orig/tdlookup.c djbdns-1.05/tdlookup.c
--- djbdns-1.05.orig/tdlookup.c	2010-04-16 23:22:16.000000000 -0700
+++ djbdns-1.05/tdlookup.c	2010-04-16 23:52:51.000000000 -0700
@@ -4,11 +4,54 @@
 #include "tai.h"
 #include "cdb.h"
 #include "byte.h"
+#include "env.h"
+#include "ip4.h"
+#include "str.h"
+#include "stralloc.h"
 #include "case.h"
 #include "dns.h"
 #include "seek.h"
 #include "response.h"
 
+static char lameip[4];
+static char *lamemx;
+static stralloc lametxt;
+
+int tdlookup_init(void)
+{
+  char *x;
+  char ch;
+  int i;
+  int j;
+  int k;
+
+  x = env_get("LAME_A");
+  if (x)
+    if (!ip4_scan(x,lameip))
+      byte_zero(lameip,4);
+
+  x = env_get("LAME_MX");
+  if (x)
+    if (!dns_domain_fromdot(&lamemx,x,str_len(x)))
+      return 0;
+
+  x = env_get("LAME_TXT");
+  if (x) {
+    i = 0;
+    j = str_len(x);
+    while (i < j) {
+      k = j - i;
+      if (k > 255) k = 255;
+      ch = k;
+      if (!stralloc_append(&lametxt,&ch)) return 0;
+      if (!stralloc_catb(&lametxt,x + i,k)) return 0;
+      i += k;
+    }
+  }
+
+  return 1;
+}
+
 static int want(const char *owner,const char type[2])
 {
   unsigned int pos;
@@ -136,7 +179,34 @@
       if (byte_equal(type,2,DNS_T_NS)) flagns = 1;
     }
     if (flagns) break;
-    if (!*control) return 0; /* q is not within our bailiwick */
+    if (!*control) {
+      /* q is not within our bailiwick */
+      flagfound = 0;
+      if (byte_diff(lameip,4,"\0\0\0\0"))
+	if (byte_equal(qtype,2,DNS_T_A) || byte_equal(qtype,2,DNS_T_ANY)) {
+	  if (!response_rstart(q,DNS_T_A,3600)) return 0;
+	  if (!response_addbytes(lameip,4)) return 0;
+	  response_rfinish(RESPONSE_ANSWER);
+	  flagfound = 1;
+	}
+      if (lamemx)
+	if (byte_equal(qtype,2,DNS_T_MX) || byte_equal(qtype,2,DNS_T_ANY)) {
+	  if (!response_rstart(q,DNS_T_MX,3600)) return 0;
+	  if (!response_addbytes("\0\0",2)) return 0;
+	  if (!response_addname(lamemx)) return 0;
+	  response_rfinish(RESPONSE_ANSWER);
+	  flagfound = 1;
+	}
+      if (lametxt.len)
+	if (byte_equal(qtype,2,DNS_T_TXT) || byte_equal(qtype,2,DNS_T_ANY)) {
+	  if (!response_rstart(q,DNS_T_TXT,3600)) return 0;
+	  if (!response_addbytes(lametxt.s,lametxt.len)) return 0;
+	  response_rfinish(RESPONSE_ANSWER);
+	  flagfound = 1;
+	}
+      if (flagfound) return 2;
+      return 0;
+    }
     control += *control;
     control += 1;
   }
diff -Nru djbdns-1.05.orig/tdlookup.h djbdns-1.05/tdlookup.h
--- djbdns-1.05.orig/tdlookup.h	1969-12-31 16:00:00.000000000 -0800
+++ djbdns-1.05/tdlookup.h	2010-04-16 23:49:28.000000000 -0700
@@ -0,0 +1,6 @@
+#ifndef TDLOOKUP_H
+#define TDLOOKUP_H
+
+extern int tdlookup_init(void);
+
+#endif
diff -Nru djbdns-1.05.orig/tinydns-get.c djbdns-1.05/tinydns-get.c
--- djbdns-1.05.orig/tinydns-get.c	2010-04-16 23:22:16.000000000 -0700
+++ djbdns-1.05/tinydns-get.c	2010-04-16 23:55:32.000000000 -0700
@@ -12,6 +12,7 @@
 #include "parsetype.h"
 #include "ip4.h"
 #include "dns.h"
+#include "tdlookup.h"
 
 extern int respond(char *,char *,char *);
 
@@ -36,6 +37,8 @@
 {
   uint16 u16;
 
+  if (!tdlookup_init()) strerr_die2sys(111,FATAL,"tdlookup_init failed");
+
   if (!*argv) usage();
 
   if (!*++argv) usage();
diff -Nru djbdns-1.05.orig/tinydns.c djbdns-1.05/tinydns.c
--- djbdns-1.05.orig/tinydns.c	2010-04-16 23:22:16.000000000 -0700
+++ djbdns-1.05/tinydns.c	2010-04-16 23:51:29.000000000 -0700
@@ -1,4 +1,6 @@
 #include "dns.h"
+#include "tdlookup.h"
+#include "strerr.h"
 
 const char *fatal = "tinydns: fatal: ";
 const char *starting = "starting tinydns\n";
@@ -8,4 +10,5 @@
 void initialize(void)
 {
   dns_random_init(seed);
+  if (!tdlookup_init()) strerr_die2sys(111,fatal,"tdlookup_init failed");
 }
