1 |
|
|
/* $OpenBSD: privsep.c,v 1.71 2017/09/20 19:21:00 krw Exp $ */ |
2 |
|
|
|
3 |
|
|
/* |
4 |
|
|
* Copyright (c) 2004 Henning Brauer <henning@openbsd.org> |
5 |
|
|
* |
6 |
|
|
* Permission to use, copy, modify, and distribute this software for any |
7 |
|
|
* purpose with or without fee is hereby granted, provided that the above |
8 |
|
|
* copyright notice and this permission notice appear in all copies. |
9 |
|
|
* |
10 |
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
11 |
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
12 |
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
13 |
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
14 |
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER IN |
15 |
|
|
* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
16 |
|
|
* OF OR IN CONNECTION WITH THE USE, ABUSE OR PERFORMANCE OF THIS SOFTWARE. |
17 |
|
|
*/ |
18 |
|
|
|
19 |
|
|
#include <sys/queue.h> |
20 |
|
|
#include <sys/socket.h> |
21 |
|
|
|
22 |
|
|
#include <net/if.h> |
23 |
|
|
#include <net/route.h> |
24 |
|
|
|
25 |
|
|
#include <netinet/in.h> |
26 |
|
|
#include <netinet/if_ether.h> |
27 |
|
|
|
28 |
|
|
#include <errno.h> |
29 |
|
|
#include <imsg.h> |
30 |
|
|
#include <signal.h> |
31 |
|
|
#include <stdio.h> |
32 |
|
|
#include <stdint.h> |
33 |
|
|
#include <stdlib.h> |
34 |
|
|
#include <string.h> |
35 |
|
|
|
36 |
|
|
#include "dhcp.h" |
37 |
|
|
#include "dhcpd.h" |
38 |
|
|
#include "log.h" |
39 |
|
|
#include "privsep.h" |
40 |
|
|
|
41 |
|
|
void |
42 |
|
|
dispatch_imsg(char *name, int rdomain, int ioctlfd, int routefd, |
43 |
|
|
struct imsgbuf *ibuf) |
44 |
|
|
{ |
45 |
|
|
static char *resolv_conf; |
46 |
|
|
static int lastidx; |
47 |
|
|
struct imsg imsg; |
48 |
|
|
ssize_t n; |
49 |
|
|
size_t sz; |
50 |
|
|
int index, newidx, retries; |
51 |
|
|
|
52 |
|
|
index = if_nametoindex(name); |
53 |
|
|
if (index == 0) { |
54 |
|
|
log_warnx("%s: unknown interface", log_procname); |
55 |
|
|
quit = INTERNALSIG; |
56 |
|
|
return; |
57 |
|
|
} |
58 |
|
|
|
59 |
|
|
for (;;) { |
60 |
|
|
if ((n = imsg_get(ibuf, &imsg)) == -1) |
61 |
|
|
fatal("imsg_get"); |
62 |
|
|
|
63 |
|
|
if (n == 0) |
64 |
|
|
break; |
65 |
|
|
|
66 |
|
|
switch (imsg.hdr.type) { |
67 |
|
|
case IMSG_DELETE_ADDRESS: |
68 |
|
|
if (imsg.hdr.len != IMSG_HEADER_SIZE + |
69 |
|
|
sizeof(struct imsg_delete_address)) |
70 |
|
|
log_warnx("%s: bad IMSG_DELETE_ADDRESS", |
71 |
|
|
log_procname); |
72 |
|
|
else |
73 |
|
|
priv_delete_address(name, ioctlfd, imsg.data); |
74 |
|
|
break; |
75 |
|
|
|
76 |
|
|
case IMSG_SET_ADDRESS: |
77 |
|
|
if (imsg.hdr.len != IMSG_HEADER_SIZE + |
78 |
|
|
sizeof(struct imsg_set_address)) |
79 |
|
|
log_warnx("%s: bad IMSG_SET_ADDRESS", |
80 |
|
|
log_procname); |
81 |
|
|
else |
82 |
|
|
priv_set_address(name, ioctlfd, imsg.data); |
83 |
|
|
break; |
84 |
|
|
|
85 |
|
|
case IMSG_FLUSH_ROUTES: |
86 |
|
|
if (imsg.hdr.len != IMSG_HEADER_SIZE + |
87 |
|
|
sizeof(struct imsg_flush_routes)) |
88 |
|
|
log_warnx("%s: bad IMSG_FLUSH_ROUTES", |
89 |
|
|
log_procname); |
90 |
|
|
else |
91 |
|
|
priv_flush_routes(index, routefd, rdomain, |
92 |
|
|
imsg.data); |
93 |
|
|
break; |
94 |
|
|
|
95 |
|
|
case IMSG_ADD_ROUTE: |
96 |
|
|
if (imsg.hdr.len != IMSG_HEADER_SIZE + |
97 |
|
|
sizeof(struct imsg_add_route)) |
98 |
|
|
log_warnx("%s: bad IMSG_ADD_ROUTE", |
99 |
|
|
log_procname); |
100 |
|
|
else |
101 |
|
|
priv_add_route(name, rdomain, routefd, |
102 |
|
|
imsg.data); |
103 |
|
|
break; |
104 |
|
|
|
105 |
|
|
case IMSG_SET_MTU: |
106 |
|
|
if (imsg.hdr.len != IMSG_HEADER_SIZE + |
107 |
|
|
sizeof(struct imsg_set_mtu)) |
108 |
|
|
log_warnx("%s: bad IMSG_SET_MTU", log_procname); |
109 |
|
|
else |
110 |
|
|
priv_set_mtu(name, ioctlfd, imsg.data); |
111 |
|
|
break; |
112 |
|
|
|
113 |
|
|
case IMSG_SET_RESOLV_CONF: |
114 |
|
|
if (imsg.hdr.len < IMSG_HEADER_SIZE) |
115 |
|
|
log_warnx("%s: bad IMSG_SET_RESOLV_CONF", |
116 |
|
|
log_procname); |
117 |
|
|
else { |
118 |
|
|
free(resolv_conf); |
119 |
|
|
resolv_conf = NULL; |
120 |
|
|
sz = imsg.hdr.len - IMSG_HEADER_SIZE; |
121 |
|
|
if (sz > 0) { |
122 |
|
|
resolv_conf = malloc(sz); |
123 |
|
|
if (resolv_conf == NULL) |
124 |
|
|
log_warn("%s: resolv_conf", |
125 |
|
|
log_procname); |
126 |
|
|
else |
127 |
|
|
strlcpy(resolv_conf, |
128 |
|
|
imsg.data, sz); |
129 |
|
|
} |
130 |
|
|
lastidx = 0; |
131 |
|
|
} |
132 |
|
|
break; |
133 |
|
|
|
134 |
|
|
case IMSG_WRITE_RESOLV_CONF: |
135 |
|
|
if (imsg.hdr.len != IMSG_HEADER_SIZE) |
136 |
|
|
log_warnx("%s: bad IMSG_WRITE_RESOLV_CONF", |
137 |
|
|
log_procname); |
138 |
|
|
else { |
139 |
|
|
retries = 0; |
140 |
|
|
do { |
141 |
|
|
newidx = default_route_index(rdomain, |
142 |
|
|
routefd); |
143 |
|
|
retries++; |
144 |
|
|
} while (newidx == 0 && retries < 3); |
145 |
|
|
if (newidx == index && newidx != lastidx) |
146 |
|
|
priv_write_resolv_conf(resolv_conf); |
147 |
|
|
lastidx = newidx; |
148 |
|
|
} |
149 |
|
|
break; |
150 |
|
|
|
151 |
|
|
case IMSG_HUP: |
152 |
|
|
if (imsg.hdr.len != IMSG_HEADER_SIZE) |
153 |
|
|
log_warnx("%s: bad IMSG_HUP", log_procname); |
154 |
|
|
else |
155 |
|
|
quit = SIGHUP; |
156 |
|
|
break; |
157 |
|
|
|
158 |
|
|
default: |
159 |
|
|
log_warnx("%s: received unknown message, code %u", |
160 |
|
|
log_procname, imsg.hdr.type); |
161 |
|
|
} |
162 |
|
|
|
163 |
|
|
imsg_free(&imsg); |
164 |
|
|
} |
165 |
|
|
} |