-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresconf.c
165 lines (145 loc) · 3.96 KB
/
resconf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* Records resolver configuration, as read from /etc/resolv.conf
*/
#include "common.h"
#include "resconf.h"
#include "stream.h"
#include "list.h"
extern int verbose;
/* Private storage for resolver configuration strings */
static struct {
char *nameserver, *domain, *search;
} resconf;
static void load_resolv_file(const char *path);
static char **to_field(const char *name);
/* Reads lines from resolv.conf and calls resconf_set() to set options */
void
resconf_init()
{
char *localdomain;
const char *resconf_path;
resconf_path = getenv("DNS_RESOLV_CONF");
if (resconf_path && *resconf_path)
fprintf(stderr, "using DNS_RESOLV_CONF %s\n", resconf_path);
else
resconf_path = "/etc/resolv.conf";
load_resolv_file(resconf_path);
/* The environment variable LOCALDOMAIN overrides search */
localdomain = getenv("LOCALDOMAIN");
if (localdomain && *localdomain) {
if (verbose)
fprintf(stderr, "using LOCALDOMAIN %s\n", localdomain);
resconf_set("search", localdomain);
}
}
static void
load_resolv_file(const char *path)
{
struct stream stream;
struct buffer option;
struct buffer arg;
int oldlen;
char **field, *fieldch;
#define WHITESPACE " \t"
#define ENDOFLINE "\n"
#define IDENTIFIER "a-zA-Z_0-9."
if (!stream_init_path(&stream, path))
return;
buffer_init(&option);
buffer_init(&arg);
for (;;) {
stream_while(&stream, ENDOFLINE, NULL); /* skip line end(s) */
stream_while(&stream, WHITESPACE, NULL); /* skip leading whitesp */
if (!stream_ok(&stream))
break;
option.len = 0;
stream_while(&stream, IDENTIFIER, &option); /* read word */
if (!option.len) {
/* Ignore bad identifiers */
stream_until(&stream, ENDOFLINE, NULL); /* ignore rest of line */
continue;
}
buffer_append(&option, '\0');
arg.len = 0;
/* Copy old field value into arg first */
field = to_field(option.data);
if (field && *field)
for (fieldch = *field; *fieldch; fieldch++)
buffer_append(&arg, *fieldch);
for (;;) {
stream_while(&stream, WHITESPACE, NULL); /* skip whitesp */
if (arg.len)
buffer_append(&arg, ' ');
oldlen = arg.len;
stream_until(&stream, WHITESPACE ENDOFLINE, &arg);
if (oldlen == arg.len) { /* no more words */
if (arg.len)
arg.len--; /* kill last space */
break;
}
}
stream_until(&stream, ENDOFLINE, NULL);
buffer_append(&arg, '\0');
resconf_set(option.data, arg.data);
}
buffer_fini(&arg);
buffer_fini(&option);
stream_fini(&stream);
}
/* Returns an internal pointer to the correct resconf field */
static char **
to_field(const char *name)
{
if (strcmp(name, "nameserver") == 0)
return &resconf.nameserver;
else if (strcmp(name, "domain") == 0)
return &resconf.domain;
else if (strcmp(name, "search") == 0)
return &resconf.search;
else
return NULL;
}
/* Returns an array of strings for the given option, or NULL if the
* option is unknown. */
char **
resconf_get(const char *option)
{
char **ptr;
if (!(ptr = to_field(option)))
return NULL;
return list_from_string(*ptr ? *ptr : "");
}
/* Free list returned by resconf_get. */
void
resconf_free(char **argv)
{
list_free(argv);
}
/* Sets a resolver option. Unknown options are ignored. */
void
resconf_set(const char *option, const char *arg)
{
char **ptr;
if (!*arg) /* Ignore when argument missing */
return;
if (!(ptr = to_field(option)))
return; /* Ignore unknown option names */
if (*ptr)
free(*ptr); /* Free old option */
*ptr = arg ? strdup(arg) : NULL;
/*
* 'search' and 'domain' are mutually exclusive.
* Setting one destroys the other
*/
if (arg && ptr == &resconf.domain && resconf.search) {
free(resconf.search);
resconf.search = NULL;
}
if (arg && ptr == &resconf.search && resconf.domain) {
free(resconf.domain);
resconf.domain = NULL;
}
if (verbose > 2)
fprintf(stderr, "resconf_set: set %s = %s\n", option,
*ptr ? *ptr : "(null)");
}