Download | Plain Text | No Line Numbers


  1. diff -Naur ./djbdns-1.05.orig/dnscache.c ./djbdns-1.05/dnscache.c
  2. --- ./djbdns-1.05.orig/dnscache.c 2011-07-12 15:32:50.000000000 +0200
  3. +++ ./djbdns-1.05/dnscache.c 2011-07-13 00:23:19.000000000 +0200
  4. @@ -24,6 +24,9 @@
  5. #include "okclient.h"
  6. #include "droproot.h"
  7. #include "maxclient.h"
  8. +#include "openreadclose.h"
  9. +#include "open.h"
  10. +#include "cdb.h"
  11.  
  12. long interface;
  13.  
  14. @@ -473,6 +476,18 @@
  15. if (socket_listen(tcp53,20) == -1)
  16. strerr_die2sys(111,FATAL,"unable to listen on TCP socket: ");
  17.  
  18. + int fd = open_read("rewrite.cdb");
  19. + if (fd > -1) {
  20. + cdb_init(&c, fd);
  21. + query_rewrite_enabled = 1;
  22. + }
  23. + log_rewrite(query_rewrite_enabled);
  24. +
  25. log_startup();
  26. doit();
  27. +
  28. + if (query_rewrite_enabled) {
  29. + cdb_free(&c);
  30. + close(fd);
  31. + }
  32. }
  33. diff -Naur ./djbdns-1.05.orig/log.c ./djbdns-1.05/log.c
  34. --- ./djbdns-1.05.orig/log.c 2011-07-12 15:32:50.000000000 +0200
  35. +++ ./djbdns-1.05/log.c 2011-07-13 00:17:20.000000000 +0200
  36. @@ -292,3 +292,10 @@
  37. number(tactive);
  38. line();
  39. }
  40. +
  41. +void log_rewrite(unsigned enabled)
  42. +{
  43. + string("rewrite ");
  44. + string((enabled) ? "enabled" : "disabled");
  45. + line();
  46. +}
  47. diff -Naur ./djbdns-1.05.orig/log.h ./djbdns-1.05/log.h
  48. --- ./djbdns-1.05.orig/log.h 2011-07-12 15:32:50.000000000 +0200
  49. +++ ./djbdns-1.05/log.h 2011-07-13 00:22:05.000000000 +0200
  50. @@ -33,5 +33,6 @@
  51. extern void log_rrsoa(const char *,const char *,const char *,const char *,const char *,unsigned int);
  52.  
  53. extern void log_stats(void);
  54. +extern void log_rewrite(unsigned enabled);
  55.  
  56. #endif
  57. diff -Naur ./djbdns-1.05.orig/Makefile ./djbdns-1.05/Makefile
  58. --- ./djbdns-1.05.orig/Makefile 2011-07-12 15:32:50.000000000 +0200
  59. +++ ./djbdns-1.05/Makefile 2011-07-13 00:21:52.000000000 +0200
  60. @@ -344,10 +344,10 @@
  61. dnscache: \
  62. load dnscache.o droproot.o okclient.o log.o cache.o query.o qmerge.o \
  63. response.o dd.o roots.o iopause.o prot.o dns.a env.a alloc.a buffer.a \
  64. -libtai.a unix.a byte.a socket.lib
  65. +libtai.a cdb.a unix.a byte.a socket.lib
  66. ./load dnscache droproot.o okclient.o log.o cache.o \
  67. query.o qmerge.o response.o dd.o roots.o iopause.o prot.o dns.a \
  68. - env.a alloc.a buffer.a libtai.a unix.a byte.a `cat \
  69. + env.a alloc.a buffer.a libtai.a cdb.a unix.a byte.a `cat \
  70. socket.lib`
  71.  
  72. dnscache-conf: \
  73. diff -Naur ./djbdns-1.05.orig/query.c ./djbdns-1.05/query.c
  74. --- ./djbdns-1.05.orig/query.c 2011-07-12 15:32:50.000000000 +0200
  75. +++ ./djbdns-1.05/query.c 2011-07-13 00:24:01.000000000 +0200
  76. @@ -13,6 +13,53 @@
  77. #include "response.h"
  78. #include "query.h"
  79. #include "ip6.h"
  80. +#include "cdb.h"
  81. +
  82. +struct cdb c;
  83. +int query_rewrite_enabled = 0;
  84. +static char cdb_data[32767];
  85. +static uint32 cdb_dlen;
  86. +static char cdb_type[2];
  87. +static unsigned int cdb_dpos;
  88. +static char *cdb_domain;
  89. +
  90. +int query_rewrite_name(char **name, unsigned from_client)
  91. +{
  92. + int r;
  93. + unsigned int pos, len;
  94. + char *newname, *tmp;
  95. + unsigned char x;
  96. + const char *type = (from_client) ? DNS_T_CNAME : DNS_T_PTR;
  97. +
  98. + if (!query_rewrite_enabled) return 1;
  99. +
  100. + tmp = *name;
  101. + while (x = *tmp++) {
  102. + tmp += (unsigned int) x;
  103. + r = cdb_find(&c, tmp, dns_domain_length(tmp));
  104. + if (r <= 0) continue;
  105. + cdb_dlen = cdb_datalen(&c);
  106. + if (cdb_dlen > sizeof cdb_data) continue;
  107. + if (cdb_read(&c, cdb_data, cdb_dlen, cdb_datapos(&c)) == -1) continue;
  108. + cdb_dpos = dns_packet_copy(cdb_data, cdb_dlen, 0, cdb_type, 2);
  109. + if (!byte_equal(cdb_type, 2, type)) continue;
  110. + cdb_dpos += 1 + 4 + 8;
  111. + cdb_dpos = dns_packet_getname(cdb_data, cdb_dlen, cdb_dpos, &cdb_domain);
  112. + if (!cdb_dpos) continue;
  113. +
  114. + len = dns_domain_length(cdb_domain);
  115. + pos = tmp - *name;
  116. + newname = alloc(pos + len);
  117. + if (!newname) return 0;
  118. + byte_copy(newname, pos, *name); // copy name prefix
  119. + byte_copy(newname + pos, len, cdb_domain); // copy new name suffix
  120. + if (*name) alloc_free(*name);
  121. + *name = newname;
  122. + break;
  123. + }
  124. +
  125. + return 1;
  126. +}
  127.  
  128. extern stralloc ignoreip;
  129.  
  130. @@ -97,6 +144,7 @@
  131. static int rqa(struct query *z)
  132. {
  133. int i;
  134. + char *namecpy = 0;
  135.  
  136. for (i = QUERY_MAXALIAS - 1;i >= 0;--i)
  137. if (z->alias[i]) {
  138. @@ -109,7 +157,13 @@
  139. return 1;
  140. }
  141.  
  142. - if (!response_query(z->name[0],z->type,z->class)) return 0;
  143. + if (query_rewrite_enabled) {
  144. + if (!dns_domain_copy(&namecpy,z->name[0])) return 0;
  145. + if (!query_rewrite_name(&namecpy, 0)) return 0;
  146. + if (!response_query(namecpy,z->type,z->class)) return 0;
  147. + alloc_free(namecpy);
  148. + } else
  149. + if (!response_query(z->name[0],z->type,z->class)) return 0;
  150. return 1;
  151. }
  152.  
  153. @@ -643,6 +697,8 @@
  154.  
  155. dns_sortip6(z->servers[z->level],256);
  156. dtype = z->level ? DNS_T_A : z->type;
  157. +
  158. + if (!query_rewrite_name(&z->name[z->level], 1)) goto DIE;
  159. if (qmerge_start(&z->qm,z->servers[z->level],flagforwardonly,z->name[z->level],dtype,z->localip,z->control[z->level]) == -1) goto DIE;
  160. return 0;
  161.  
  162. @@ -786,6 +842,7 @@
  163.  
  164. if (!dns_domain_suffix(t1,control)) { i = j; continue; }
  165. if (!roots_same(t1,control)) { i = j; continue; }
  166. + if (!query_rewrite_name(&t1, 0)) goto DIE;
  167.  
  168. if (byte_equal(type,2,DNS_T_ANY))
  169. ;
  170. @@ -983,6 +1040,7 @@
  171. if (dns_domain_equal(t1,d))
  172. if (byte_equal(header + 2,2,DNS_C_IN)) /* should always be true */
  173. if (typematch(header,dtype)) {
  174. + if (!query_rewrite_name(&t1, 0)) goto DIE;
  175. if (!response_rstart(t1,header,ttl)) goto DIE;
  176.  
  177. if (typematch(header,DNS_T_NS) || typematch(header,DNS_T_CNAME) || typematch(header,DNS_T_PTR)) {
  178. diff -Naur ./djbdns-1.05.orig/query.h ./djbdns-1.05/query.h
  179. --- ./djbdns-1.05.orig/query.h 2011-07-12 15:32:50.000000000 +0200
  180. +++ ./djbdns-1.05/query.h 2011-07-12 23:57:12.000000000 +0200
  181. @@ -30,4 +30,7 @@
  182.  
  183. extern void query_forwardonly(void);
  184.  
  185. +extern struct cdb c;
  186. +extern int query_rewrite_enabled;
  187. +
  188. #endif
  189.