-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolvconf-admin.c
410 lines (368 loc) · 11.5 KB
/
resolvconf-admin.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/* setuid helper program for tools that need to be able to set up the local DNS.
*
* Author: Daniel Kahn Gillmor <[email protected]
* Copyright: 2017
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/* TODO: handle DHCPv6 */
/* TODO: discourage ecs (client subnet) see
https://sourceware.org/bugzilla/show_bug.cgi?id=18419 */
/* TODO: accept non-ascii domain names for -d and -s */
/* TODO: there are some small TOCTOU race conditions (probably fewer than
resolvconf itself) */
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <ifaddrs.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <ctype.h>
typedef struct { char data[INET_ADDRSTRLEN]; } addrstr;
#ifndef PROGNAME
#define PROGNAME "resolvconf-admin"
#endif
#ifndef PREAMBLE
#define PREAMBLE "# Generated by " PROGNAME
#endif
#ifndef BACKUPNAME
#define BACKUPNAME ETCRESOLVCONF ".bak." PROGNAME
#endif
#ifndef EXECPATH
#define EXECPATH "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
#endif
char * const envp[] = {
"PATH=" EXECPATH,
NULL
};
/* return 0 on success */
int
canonicalize_ip(const char *in, addrstr* out)
{
struct in_addr a;
if (inet_pton(AF_INET, in, &a) != 1)
return 1;
if (NULL == inet_ntop(AF_INET, &a, out->data, sizeof(out->data))) {
perror("inet_ntop failed");
return 2;
}
return 0;
}
/* returns 1 if ifname is the name of a legitimate network interface on the
current system, 0 if not */
int
is_valid_interface(const char *ifname)
{
struct ifaddrs *ifa, *cur;
if (getifaddrs(&ifa)) {
perror("Failed to getifaddrs()");
return 0;
}
int found = 0;
for (cur = ifa; cur; cur = cur->ifa_next) {
if (strcmp(ifname, cur->ifa_name) == 0) {
found = 1;
break;
}
}
freeifaddrs(ifa);
return found;
}
/* returns 1 if we think domain is a valid argument for "search" or "domain"
paths, 0 if not.
allowblank should be 1 for "search", 0 for "domain". see resolv.conf(5)
for more details.
*/
int
is_valid_domain(const char *domain, int allowblank)
{
if (domain == NULL || *domain == 0)
return 0;
while (*domain) {
if ((!(allowblank && isblank(*domain))) &&
(isspace(*domain) ||
(!isgraph(*domain)) ||
(ispunct(*domain) && *domain != '.')))
return 0;
domain++;
}
return 1;
}
void
usage(FILE* f, const char *arg0)
{
if (!arg0)
arg0 = PROGNAME;
fprintf(stderr, "Usage:\t%s add IFNAME [-d DOMAIN] [-s SEARCH] NAMESERVERIP [...]\n"
"\t%s del IFNAME\n"
"\t%s help\n"
"\n"
"If " SBINRESOLVCONF " is present, invoke it appropriately.\n"
"If " SBINRESOLVCONF " is not present, update " ETCRESOLVCONF " directly.\n",
arg0, arg0, arg0);
}
int
write_new_resolvconf(FILE* writer, int argc, const char **argv, const char *preamble) {
int i;
addrstr canonical;
const char *search = NULL, *domain = NULL;
fprintf(writer, "%s", preamble);
int total_nameservers = 0;
for (i = 0; i < argc; i++) {
if ((strcmp(argv[i], "-d") == 0) ||
(strcmp(argv[i], "-s") == 0)) {
i++;
if (i >= argc) {
fprintf(stderr, "Need DOMAIN argument for %s\n", argv[i-1]);
return 1;
}
if (argv[i-1][1] == 'd') {
if (domain) {
fprintf(stderr, "only one -d DOMAIN option allowed\n");
return 1;
}
if (!is_valid_domain(argv[i], 0)) {
fprintf(stderr, "Non-domain-name argument for -d: \"%s\", skipping..\n",
argv[i]);
continue;
}
domain = argv[i];
} else if (argv[i-1][1] == 's') {
if (search) {
fprintf(stderr, "only one -s SEARCH option allowed\n");
return 1;
}
if (!is_valid_domain(argv[i], 1)) {
fprintf(stderr, "bad argument for domain search list -s: \"%s\", skipping...\n",
argv[i]);
continue;
}
search = argv[i];
} else {
fprintf(stderr, "something went wrong with argument parsing\n");
return 1;
}
} else {
/* this should be a nameserver */
if (canonicalize_ip(argv[i], &canonical)) {
fprintf(stderr, "Failed to canonicalize IP address '%s'\n",
argv[i]);
return 1;
}
fprintf(writer, "nameserver %s\n", canonical.data);
total_nameservers++;
}
}
if (total_nameservers < 1) {
fprintf(stderr, "not enough nameservers\n");
return 1;
}
if (domain)
fprintf(writer, "domain %s\n", domain);
if (search)
fprintf(writer, "search %s\n", search);
return 0;
}
/* return 1 if it starts with the preamble; 0 if it exists and does not start
with the preamble; -1 if there was any weirdness that prevented us from
checking. */
int resolvconf_starts_with(const char *preamble) {
int ret = -1;
int oldfd = open(ETCRESOLVCONF, O_RDONLY);
if (oldfd == -1) {
perror("Failed to open " ETCRESOLVCONF " for reading");
} else {
struct stat statbuf;
size_t sz = strlen(preamble);
if (fstat(oldfd, &statbuf)) {
perror("failed to stat " ETCRESOLVCONF " after opening");
return -1;
}
if (statbuf.st_size < sz)
return 0;
char* oldcontents = (char *)mmap(NULL, sz,
PROT_READ, MAP_PRIVATE, oldfd, 0);
if (oldcontents == MAP_FAILED) {
perror("Failed to mmap " ETCRESOLVCONF);
return -1;
} else {
ret = (strncmp(preamble, oldcontents, sz) == 0);
if (munmap(oldcontents, sz))
perror("failed to mumap " ETCRESOLVCONF);
}
if (close(oldfd))
perror("failed to close " ETCRESOLVCONF);
}
return ret;
}
int
main(int argc, const char **argv)
{
int pipes[2] = { -1, -1 };
int use_sbin_resolvconf = 0;
char tmpname[] = ETCRESOLVCONF "." PROGNAME ".XXXXXX";
int ret;
const char *ifname = NULL;
char label[1024];
char preamble[1024];
int do_add = 0;
label[sizeof(label)-1] = 0;
preamble[sizeof(preamble)-1] = 0;
preamble[sizeof(preamble)-2] = '\n'; /* ensure that there is a trailing
newline if ifname happens to be
enormous */
if (argc < 2) {
usage(stderr, argv[0]);
return 1;
}
if (strcmp(argv[1], "help") == 0) {
usage(stdout, argv[0]);
return 0;
}
if (strcmp(argv[1], "add") == 0) {
do_add = 1;
} else if (strcmp(argv[1], "del") == 0) {
} else {
usage(stderr, argv[0]);
return 1;
}
ifname = argv[2];
if (!is_valid_interface(ifname)) {
fprintf(stderr, "'%s' is not a valid network interface on this host\n",
ifname);
return 1;
}
if (access(SBINRESOLVCONF, X_OK)) {
if (errno != ENOENT) {
perror("cannot execute " SBINRESOLVCONF);
fprintf(stderr, "falling back to overwriting " ETCRESOLVCONF " manually...\n");
}
} else {
use_sbin_resolvconf = 1;
}
snprintf(preamble, sizeof(preamble)-2, "%s\n# (%s)\n", PREAMBLE, ifname);
if (use_sbin_resolvconf) {
snprintf(label, sizeof(label)-1, "%s.%d." PROGNAME, ifname, getuid());
if (do_add) {
if (pipe(pipes)) {
perror("Failed to open a pipe");
return 1;
}
} else {
execle(SBINRESOLVCONF, "resolvconf", "-d", label, NULL, envp);
perror(SBINRESOLVCONF " -d failed.");
return 1;
}
} else {
if (do_add) {
pipes[1] = mkstemp(tmpname);
if (pipes[1] == -1) {
perror("Failed to make a temporary file for overwriting " ETCRESOLVCONF);
return 1;
}
} else {
if (resolvconf_starts_with(preamble) == 1) {
if (access(BACKUPNAME, F_OK) == 0) {
if (rename(BACKUPNAME, ETCRESOLVCONF))
perror("Failed to restore " BACKUPNAME " to " ETCRESOLVCONF);
}
} else {
fprintf(stderr, "%s is not created by %s for interface %s, not destroying.\n",
ETCRESOLVCONF, PROGNAME, ifname);
}
return 0;
}
}
FILE *writer = fdopen(pipes[1], "w");
if (writer == NULL) {
perror("Failed to set up buffered writer");
return 1;
}
ret = write_new_resolvconf(writer, argc-3, argv+3, preamble);
if (ret) {
if (writer)
fclose(writer);
if (!use_sbin_resolvconf) {
if (unlink(tmpname))
fprintf(stderr, "failed to unlink tempfile '%s': (%d) %s\n",
tmpname, errno, strerror(errno));
}
return ret;
}
if (use_sbin_resolvconf) {
if (fclose(writer)) {
perror("Failed to close write side of pipe");
return 1;
}
if (-1 == dup2(pipes[0], 0)) {
fprintf(stderr, "dup2(%d, 0) failed (%d) %s\n", pipes[0],
errno, strerror(errno));
return 1;
}
execle(SBINRESOLVCONF, "resolvconf", "-a", label, NULL, envp);
/* should never get here! */
perror(SBINRESOLVCONF " -a failed");
return 1;
} else {
struct stat statbuf;
if (stat(ETCRESOLVCONF, &statbuf)) {
perror("failed to stat " ETCRESOLVCONF);
fprintf(stderr, "Not copying ownership and permissions\n");
} else {
/* if possible, copy ownership and permissions */
if (fchown(fileno(writer), statbuf.st_uid, statbuf.st_gid))
perror("failed to copy ownership for " ETCRESOLVCONF);
if (fchmod(fileno(writer), statbuf.st_mode))
perror("failed to copy permissions for " ETCRESOLVCONF);
/* TODO: if possible, copy over ACL as well? would need to link to
libacl, which expands attack surface */
}
if (fclose(writer)) {
perror("Failed to close write side of pipe");
return 1;
}
/* back up old /etc/resolv.conf which we are clobbering here, if the
old file doesn't start with PREAMBLE. note that we only check
PREAMBLE here (that is, without the ifname), because we don't want
two running instances (e.g. on different interfaces) to
be backing up each other's data. */
if (0 == access(ETCRESOLVCONF, F_OK)) {
if (resolvconf_starts_with(PREAMBLE) != 1) {
if (0 == access(BACKUPNAME, F_OK)) {
if (unlink(BACKUPNAME)) {
if (errno != ENOENT)
perror("unlink(\"" BACKUPNAME "\") failed while backing up " ETCRESOLVCONF);
}
}
if (link(ETCRESOLVCONF, BACKUPNAME))
perror("failed to back up " ETCRESOLVCONF " to " BACKUPNAME);
}
}
if (rename(tmpname, ETCRESOLVCONF)) {
fprintf(stderr, "failed to rename \"%s\" to \"%s\": (%d) %s\n",
tmpname, ETCRESOLVCONF, errno, strerror(errno));
}
}
}