-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathifchd.c
379 lines (337 loc) · 10.9 KB
/
ifchd.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
// Copyright 2004-2020 Nicholas J. Kain <njkain at gmail dot com>
// SPDX-License-Identifier: MIT
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/time.h>
#include <sys/types.h>
#include <poll.h>
#include <sys/prctl.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <errno.h>
#include "nk/log.h"
#include "nk/privs.h"
#include "nk/io.h"
#include "ifchd.h"
#include "ndhc.h"
#include "ifchd-parse.h"
#include "sys.h"
#include "ifset.h"
struct ifchd_client cl;
static int resolv_conf_fd = -1;
/* int ntp_conf_fd = -1; */
static int resolv_conf_head_fd = -1;
static int resolv_conf_tail_fd = -1;
/* If true, allow HOSTNAME changes from dhcp server. */
int allow_hostname = 0;
uid_t ifch_uid = 0;
gid_t ifch_gid = 0;
static void writeordie(int fd, const char *buf, size_t len)
{
ssize_t r = safe_write(fd, buf, len);
if (r < 0 || (size_t)r != len)
suicide("%s: (%s) write failed: %zd\n", client_config.interface,
__func__, r);
}
static int write_append_fd(int to_fd, int from_fd, const char *descr)
{
if (from_fd < 0) return 0;
if (to_fd < 0) return -1;
const off_t lse = lseek(from_fd, 0, SEEK_END);
if (lse < 0) {
log_line("%s: (%s) lseek(SEEK_END) failed %s\n",
client_config.interface, __func__, descr);
return -2;
}
if (lseek(from_fd, 0, SEEK_SET) < 0) {
log_line("%s: (%s) lseek(SEEK_SET) failed %s\n",
client_config.interface, __func__, descr);
return -2;
}
char buf[4096];
size_t from_fd_len = (size_t)lse;
while (from_fd_len > 0) {
const size_t to_read = from_fd_len <= sizeof buf ? from_fd_len : sizeof buf;
ssize_t r = safe_read(from_fd, buf, to_read);
if (r < 0 || (size_t)r != to_read)
suicide("%s: (%s) read failed %s\n", client_config.interface, __func__, descr);
r = safe_write(to_fd, buf, to_read);
if (r < 0 || (size_t)r != to_read)
suicide("%s: (%s) write failed %s\n", client_config.interface, __func__, descr);
from_fd_len -= to_read;
}
return 0;
}
/* Writes a new resolv.conf based on the information we have received. */
static int write_resolve_conf(void)
{
static const char ns_str[] = "nameserver ";
static const char dom_str[] = "domain ";
static const char srch_str[] = "search ";
off_t off;
char buf[MAX_BUF];
if (resolv_conf_fd < 0)
return 0;
if (strlen(cl.namesvrs) == 0)
return -1;
if (lseek(resolv_conf_fd, 0, SEEK_SET) < 0)
return -1;
write_append_fd(resolv_conf_fd, resolv_conf_head_fd, "prepending resolv_conf head");
char *p = cl.namesvrs;
while (p && (*p != '\0')) {
char *q = strchr(p, ',');
if (!q)
q = strchr(p, '\0');
else
*q++ = '\0';
if (!memccpy(buf, p, 0, sizeof buf)) {
log_line("%s: (%s) memccpy failed appending nameservers\n",
client_config.interface, __func__);
return -1;
}
writeordie(resolv_conf_fd, ns_str, strlen(ns_str));
writeordie(resolv_conf_fd, buf, strlen(buf));
writeordie(resolv_conf_fd, "\n", 1);
p = q;
}
p = cl.domains;
int numdoms = 0;
while (p && (*p != '\0')) {
char *q = strchr(p, ',');
if (!q)
q = strchr(p, '\0');
else
*q++ = '\0';
if (!memccpy(buf, p, 0, sizeof buf)) {
log_line("%s: (%s) memccpy failed appending domains\n",
client_config.interface, __func__);
return -1;
}
if (numdoms == 0) {
writeordie(resolv_conf_fd, dom_str, strlen(dom_str));
writeordie(resolv_conf_fd, buf, strlen(buf));
} else {
if (numdoms == 1) {
writeordie(resolv_conf_fd, "\n", 1);
writeordie(resolv_conf_fd, srch_str, strlen(srch_str));
writeordie(resolv_conf_fd, buf, strlen(buf));
} else {
writeordie(resolv_conf_fd, " ", 1);
writeordie(resolv_conf_fd, buf, strlen(buf));
}
}
++numdoms;
p = q;
if (numdoms > 6)
break;
}
writeordie(resolv_conf_fd, "\n", 1);
write_append_fd(resolv_conf_fd, resolv_conf_tail_fd, "appending resolv_conf tail");
off = lseek(resolv_conf_fd, 0, SEEK_CUR);
if (off < 0) {
log_line("%s: (%s) lseek returned error: %s\n", client_config.interface,
__func__, strerror(errno));
return -1;
}
if (safe_ftruncate(resolv_conf_fd, off) < 0) {
log_line("%s: (%s) ftruncate returned error: %s\n", client_config.interface,
__func__, strerror(errno));
return -1;
}
if (fsync(resolv_conf_fd) < 0) {
log_line("%s: (%s) fsync returned error: %s\n", client_config.interface,
__func__, strerror(errno));
return -1;
}
return 0;
}
/* XXX: addme */
int perform_timezone(const char *str, size_t len)
{
(void)len;
log_line("Timezone setting NYI: '%s'\n", str);
return 0;
}
/* Add a dns server to the /etc/resolv.conf -- we already have a fd. */
int perform_dns(const char *str, size_t len)
{
if (resolv_conf_fd < 0)
return 0;
int ret = -1;
if (len > sizeof cl.namesvrs) {
log_line("DNS server list is too long: %zu > %zu\n", len, sizeof cl.namesvrs);
return ret;
}
if (!memccpy(cl.namesvrs, str, 0, sizeof cl.namesvrs)) {
memset(cl.namesvrs, 0, sizeof cl.namesvrs);
log_line("%s: (%s) memccpy failed\n", client_config.interface, __func__);
return ret;
}
ret = write_resolve_conf();
if (ret >= 0)
log_line("Added DNS server: '%s'\n", str);
return ret;
}
/* Updates for print daemons are too non-standard to be useful. */
int perform_lprsvr(const char *str, size_t len)
{
(void)len;
log_line("Line printer server setting NYI: '%s'\n", str);
return 0;
}
/* Sets machine hostname. */
int perform_hostname(const char *str, size_t len)
{
if (!allow_hostname)
return 0;
if (sethostname(str, len) < 0) {
log_line("sethostname returned %s\n", strerror(errno));
return -1;
}
log_line("Set hostname: '%s'\n", str);
return 0;
}
/* update "domain" and "search" in /etc/resolv.conf */
int perform_domain(const char *str, size_t len)
{
if (resolv_conf_fd < 0)
return 0;
int ret = -1;
if (len > sizeof cl.domains) {
log_line("DNS domain list is too long: %zu > %zu\n", len, sizeof cl.domains);
return ret;
}
if (!memccpy(cl.domains, str, 0, sizeof cl.domains)) {
memset(cl.domains, 0, sizeof cl.domains);
log_line("%s: (%s) memccpy failed\n", client_config.interface, __func__);
return ret;
}
ret = write_resolve_conf();
if (ret == 0)
log_line("Added DNS domain: '%s'\n", str);
return ret;
}
/* I don't think this can be done without a netfilter extension
* that isn't in the mainline kernels. */
int perform_ipttl(const char *str, size_t len)
{
(void)len;
log_line("TTL setting NYI: '%s'\n", str);
return 0;
}
/* XXX: addme */
int perform_ntpsrv(const char *str, size_t len)
{
(void)len;
log_line("NTP server setting NYI: '%s'\n", str);
return 0;
}
/* Maybe Samba cares about this feature? I don't know. */
int perform_wins(const char *str, size_t len)
{
(void)str;
(void)len;
return 0;
}
static void inform_execute(char c)
{
ssize_t r = safe_write(ifchSock[1], &c, sizeof c);
if (r == 0) {
// Remote end hung up.
exit(EXIT_SUCCESS);
} else if (r < 0)
suicide("%s: (%s) error writing to ifch -> ndhc socket: %s\n",
client_config.interface, __func__, strerror(errno));
}
static void process_client_socket(void)
{
char buf[MAX_BUF];
memset(buf, '\0', sizeof buf);
ssize_t r = safe_recv(ifchSock[1], buf, sizeof buf - 1, MSG_DONTWAIT);
if (r == 0) {
// Remote end hung up.
exit(EXIT_SUCCESS);
} else if (r < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK)
return;
suicide("%s: (%s) error reading from ndhc -> ifch socket: %s\n",
client_config.interface, __func__, strerror(errno));
}
int ebr = execute_buffer(buf);
if (ebr < 0) {
if (ebr == -99)
suicide("%s: (%s) unrecoverable failure in command sequence: '%s'\n",
client_config.interface, __func__, buf);
inform_execute('-');
} else
inform_execute('+');
}
static void do_ifch_work(void)
{
cl.state = STATE_NOTHING;
memset(cl.ibuf, 0, sizeof cl.ibuf);
memset(cl.namesvrs, 0, sizeof cl.namesvrs);
memset(cl.domains, 0, sizeof cl.domains);
struct pollfd pfds[2] = {0};
pfds[0].fd = ifchSock[1];
pfds[0].events = POLLIN|POLLHUP|POLLERR|POLLRDHUP;
pfds[1].fd = ifchStream[1];
pfds[1].events = POLLHUP|POLLERR|POLLRDHUP;
for (;;) {
if (poll(pfds, 2, -1) < 0) {
if (errno != EINTR) suicide("poll failed\n");
}
if (pfds[0].revents & POLLIN) {
process_client_socket();
}
if (pfds[0].revents & (POLLHUP|POLLERR|POLLRDHUP)) {
suicide("ifchSock closed unexpectedly\n");
}
if (pfds[1].revents & (POLLHUP|POLLERR|POLLRDHUP)) {
exit(EXIT_SUCCESS);
}
}
}
// If we are requested to update resolv.conf, preopen the fd before we drop
// root, making sure that if we create resolv.conf, it will be world-readable.
static void setup_resolv_conf(void)
{
if (strncmp(resolv_conf_d, "", sizeof resolv_conf_d)) {
umask(022);
resolv_conf_fd = open(resolv_conf_d, O_RDWR|O_CREAT|O_CLOEXEC, 644);
umask(077);
if (resolv_conf_fd < 0) {
suicide("FATAL - unable to open resolv.conf\n");
}
char buf[PATH_MAX];
ssize_t sl = snprintf(buf, sizeof buf, "%s.head", resolv_conf_d);
if (sl < 0 || (size_t)sl > sizeof buf)
log_line("snprintf failed appending resolv_conf_head; path too long?\n");
else
resolv_conf_head_fd = open(buf, O_RDONLY|O_CLOEXEC, 0);
sl = snprintf(buf, sizeof buf, "%s.tail", resolv_conf_d);
if (sl < 0 || (size_t)sl > sizeof buf)
log_line("snprintf failed appending resolv_conf_tail; path too long?\n");
else
resolv_conf_tail_fd = open(buf, O_RDONLY|O_CLOEXEC, 0);
memset(buf, '\0', sizeof buf);
}
memset(resolv_conf_d, '\0', sizeof resolv_conf_d);
}
void ifch_main(void)
{
prctl(PR_SET_NAME, "ndhc: ifch");
umask(077);
setup_signals_subprocess();
setup_resolv_conf();
nk_set_chroot(chroot_dir);
memset(chroot_dir, '\0', sizeof chroot_dir);
unsigned char keepcaps[] = { CAP_NET_ADMIN };
nk_set_uidgid(ifch_uid, ifch_gid, keepcaps, sizeof keepcaps);
do_ifch_work();
}