6
#include "openreadclose.h"
9
static stralloc data = {0};
11
static int init(stralloc *rules)
19
if (!stralloc_copys(rules,"")) return -1;
21
x = env_get("DNSREWRITEFILE");
22
if (!x) x = "/etc/dnsrewrite";
24
i = openreadclose(x,&data,64);
25
if (i == -1) return -1;
28
if (!stralloc_append(&data,"\n")) return -1;
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 -1;
34
if (rules->s[rules->len - 1] != ' ')
35
if (rules->s[rules->len - 1] != '\t')
36
if (rules->s[rules->len - 1] != '\r')
40
if (!stralloc_0(rules)) return -1;
46
x = env_get("LOCALDOMAIN");
48
if (!stralloc_copys(&data,x)) return -1;
49
if (!stralloc_append(&data," ")) return -1;
50
if (!stralloc_copys(rules,"?:")) return -1;
52
for (j = 0;j < data.len;++j)
53
if (data.s[j] == ' ') {
54
if (!stralloc_cats(rules,"+.")) return -1;
55
if (!stralloc_catb(rules,data.s + i,j - i)) return -1;
58
if (!stralloc_0(rules)) return -1;
59
if (!stralloc_cats(rules,"*.:")) return -1;
60
if (!stralloc_0(rules)) return -1;
64
i = openreadclose("/etc/resolv.conf",&data,64);
65
if (i == -1) return -1;
68
if (!stralloc_append(&data,"\n")) return -1;
70
for (j = 0;j < data.len;++j)
71
if (data.s[j] == '\n') {
72
if (byte_equal("search ",7,data.s + i) || byte_equal("search\t",7,data.s + i) || byte_equal("domain ",7,data.s + i) || byte_equal("domain\t",7,data.s + i)) {
73
if (!stralloc_copys(rules,"?:")) return -1;
76
k = byte_chr(data.s + i,j - i,' ');
77
k = byte_chr(data.s + i,k,'\t');
78
if (!k) { ++i; continue; }
79
if (!stralloc_cats(rules,"+.")) return -1;
80
if (!stralloc_catb(rules,data.s + i,k)) return -1;
83
if (!stralloc_0(rules)) return -1;
84
if (!stralloc_cats(rules,"*.:")) return -1;
85
if (!stralloc_0(rules)) return -1;
93
if (gethostname(host,sizeof host) == -1) return -1;
94
host[(sizeof host) - 1] = 0;
95
i = str_chr(host,'.');
97
if (!stralloc_copys(rules,"?:")) return -1;
98
if (!stralloc_cats(rules,host + i)) return -1;
99
if (!stralloc_0(rules)) return -1;
101
if (!stralloc_cats(rules,"*.:")) return -1;
102
if (!stralloc_0(rules)) return -1;
108
static unsigned int uses;
109
static struct taia deadline;
110
static stralloc rules = {0}; /* defined if ok */
112
int dns_resolvconfrewrite(stralloc *out)
117
if (taia_less(&deadline,&now)) ok = 0;
121
if (init(&rules) == -1) return -1;
122
taia_uint(&deadline,600);
123
taia_add(&deadline,&now,&deadline);
129
if (!stralloc_copy(out,&rules)) return -1;