djbdnscurve6  38
djbdnscurve6
dns_rcrw.c
Go to the documentation of this file.
1 #include <unistd.h>
2 #include "taia.h"
3 #include "env.h"
4 #include "byte.h"
5 #include "str.h"
6 #include "readclose.h"
7 #include "dns.h"
8 
9 static stralloc data = {0};
10 
11 static int init(stralloc *rules)
12 {
13  char host[256];
14  const char *x;
15  int i;
16  int j;
17  int k;
18 
19  if (!stralloc_copys(rules,"")) return DNS_MEM;
20 
21  x = env_get("DNSREWRITEFILE");
22  if (!x) x = "/etc/dnsrewrite";
23 
24  i = openreadclose(x,&data,64);
25  if (i == -1) return DNS_INT;
26 
27  if (i) {
28  if (!stralloc_append(&data,"\n")) return DNS_MEM;
29  i = 0;
30  for (j = 0; j < data.len; ++j)
31  if (data.s[j] == '\n') {
32  if (!stralloc_catb(rules,data.s + i,j - i)) return DNS_MEM;
33  while (rules->len) {
34  if (rules->s[rules->len - 1] != ' ')
35  if (rules->s[rules->len - 1] != '\t')
36  if (rules->s[rules->len - 1] != '\r')
37  break;
38  --rules->len;
39  }
40  if (!stralloc_0(rules)) return DNS_MEM;
41  i = j + 1;
42  }
43  return 0;
44  }
45 
46  x = env_get("LOCALDOMAIN");
47  if (x) {
48  if (!stralloc_copys(&data,x)) return DNS_MEM;
49  if (!stralloc_append(&data," ")) return DNS_MEM;
50  if (!stralloc_copys(rules,"?:")) return DNS_MEM;
51  i = 0;
52  for (j = 0; j < data.len; ++j)
53  if (data.s[j] == ' ') {
54  if (!stralloc_cats(rules,"+.")) return DNS_MEM;
55  if (!stralloc_catb(rules,data.s + i,j - i)) return DNS_MEM;
56  i = j + 1;
57  }
58  if (!stralloc_0(rules)) return DNS_MEM;
59  if (!stralloc_cats(rules,"*.:")) return DNS_MEM;
60  if (!stralloc_0(rules)) return DNS_MEM;
61  return 0;
62  }
63 
64  i = openreadclose("/etc/resolv.conf",&data,64);
65  if (i == -1) return DNS_INT;
66 
67  if (i) {
68  if (!stralloc_append(&data,"\n")) return DNS_MEM;
69  i = 0;
70  for (j = 0;j < data.len;++j)
71  if (data.s[j] == '\n') {
72  if (byte_equal("search ",7,data.s + i) ||
73  byte_equal("search\t",7,data.s + i) ||
74  byte_equal("domain ",7,data.s + i) ||
75  byte_equal("domain\t",7,data.s + i)) {
76  if (!stralloc_copys(rules,"?:")) return DNS_MEM;
77  i += 7;
78  while (i < j) {
79  k = byte_chr(data.s + i,j - i,' ');
80  k = byte_chr(data.s + i,k,'\t');
81  if (!k) { ++i; continue; }
82  if (!stralloc_cats(rules,"+.")) return DNS_MEM;
83  if (!stralloc_catb(rules,data.s + i,k)) return DNS_MEM;
84  i += k;
85  }
86  if (!stralloc_0(rules)) return DNS_MEM;
87  if (!stralloc_cats(rules,"*.:")) return DNS_MEM;
88  if (!stralloc_0(rules)) return DNS_MEM;
89  return 0;
90  }
91  i = j + 1;
92  }
93  }
94 
95  host[0] = 0;
96  if (gethostname(host,sizeof(host)) == -1) return DNS_ERR;
97  host[(sizeof(host)) - 1] = 0;
98  i = str_chr(host,'.');
99  if (host[i]) {
100  if (!stralloc_copys(rules,"?:")) return DNS_MEM;
101  if (!stralloc_cats(rules,host + i)) return DNS_MEM;
102  if (!stralloc_0(rules)) return DNS_MEM;
103  }
104  if (!stralloc_cats(rules,"*.:")) return DNS_MEM;
105  if (!stralloc_0(rules)) return DNS_MEM;
106 
107  return 0;
108 }
109 
110 static int ok = 0;
111 static unsigned int uses;
112 static struct taia deadline;
113 static stralloc rules = {0}; /* defined if ok */
114 
115 int dns_resolvconfrewrite(stralloc *out)
116 {
117  struct taia now;
118 
119  taia_now(&now);
120  if (taia_less(&deadline,&now)) ok = 0;
121  if (!uses) ok = 0;
122 
123  if (!ok) {
124  if (init(&rules) < 0) return DNS_INT;
125  taia_uint(&deadline,600);
126  taia_add(&deadline,&now,&deadline);
127  uses = 10000;
128  ok = 1;
129  }
130 
131  --uses;
132  if (!stralloc_copy(out,&rules)) return DNS_MEM;
133  return 0;
134 }
char data[32767]
Definition: axfrdns.c:131
struct tai now
Definition: axfrdns.c:130
#define DNS_ERR
Definition: dns.h:22
#define DNS_INT
Definition: dns.h:24
#define DNS_MEM
Definition: dns.h:21
int dns_resolvconfrewrite(stralloc *out)
Definition: dns_rcrw.c:115
struct line * x
void out(const char *s, unsigned int len)
Definition: generic-conf.c:54