1 /* SPDX-License-Identifier: BSD-2-Clause */
3 * Privilege Separation for dhcpcd, control proxy
4 * Copyright (c) 2006-2021 Roy Marples <roy@marples.name>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 ps_ctl_startcb(void *arg)
42 struct dhcpcd_ctx *ctx = arg;
45 if (ctx->options & DHCPCD_MANAGER) {
46 setproctitle("[control proxy]");
49 setproctitle("[control proxy] %s%s%s",
51 ctx->options & DHCPCD_IPV4 ? " [ip4]" : "",
52 ctx->options & DHCPCD_IPV6 ? " [ip6]" : "");
54 (DHCPCD_IPV4 | DHCPCD_IPV6)) == DHCPCD_IPV4)
56 else if ((ctx->options &
57 (DHCPCD_IPV4 | DHCPCD_IPV6)) == DHCPCD_IPV6)
63 ctx->ps_control_pid = getpid();
65 return control_start(ctx,
66 ctx->options & DHCPCD_MANAGER ? NULL : *ctx->ifv, af);
70 ps_ctl_recvmsgcb(void *arg, struct ps_msghdr *psm, __unused struct msghdr *msg)
72 struct dhcpcd_ctx *ctx = arg;
74 if (psm->ps_cmd != PS_CTL_EOF) {
79 if (ctx->ps_control_client != NULL)
80 ctx->ps_control_client = NULL;
85 ps_ctl_recvmsg(void *arg)
87 struct dhcpcd_ctx *ctx = arg;
89 if (ps_recvpsmsg(ctx, ctx->ps_control_fd, ps_ctl_recvmsgcb, ctx) == -1)
94 ps_ctl_handleargs(struct fd_list *fd, char *data, size_t len)
97 /* Make any change here in dhcpcd.c as well. */
98 if (strncmp(data, "--version",
99 MIN(strlen("--version"), len)) == 0) {
100 return control_queue(fd, UNCONST(VERSION),
101 strlen(VERSION) + 1);
102 } else if (strncmp(data, "--getconfigfile",
103 MIN(strlen("--getconfigfile"), len)) == 0) {
104 return control_queue(fd, UNCONST(fd->ctx->cffile),
105 strlen(fd->ctx->cffile) + 1);
106 } else if (strncmp(data, "--listen",
107 MIN(strlen("--listen"), len)) == 0) {
108 fd->flags |= FD_LISTEN;
112 if (fd->ctx->ps_control_client != NULL &&
113 fd->ctx->ps_control_client != fd)
115 logerrx("%s: cannot handle another client", __func__);
122 ps_ctl_dispatch(void *arg, struct ps_msghdr *psm, struct msghdr *msg)
124 struct dhcpcd_ctx *ctx = arg;
125 struct iovec *iov = msg->msg_iov;
127 unsigned int fd_flags = FD_SENDLEN;
129 switch (psm->ps_flags) {
133 fd_flags |= FD_UNPRIV;
137 switch (psm->ps_cmd) {
139 if (msg->msg_iovlen != 1) {
143 if (ctx->ps_control_client != NULL) {
144 logerrx("%s: cannot handle another client", __func__);
147 fd = control_new(ctx, ctx->ps_control_data_fd, fd_flags);
150 ctx->ps_control_client = fd;
151 control_recvdata(fd, iov->iov_base, iov->iov_len);
154 control_free(ctx->ps_control_client);
164 ps_ctl_dodispatch(void *arg)
166 struct dhcpcd_ctx *ctx = arg;
168 if (ps_recvpsmsg(ctx, ctx->ps_control_fd, ps_ctl_dispatch, ctx) == -1)
173 ps_ctl_recv(void *arg)
175 struct dhcpcd_ctx *ctx = arg;
180 len = read(ctx->ps_control_data_fd, buf, sizeof(buf));
181 if (len == -1 || len == 0) {
182 logerr("%s: read", __func__);
183 eloop_exit(ctx->eloop, EXIT_FAILURE);
186 if (ctx->ps_control_client == NULL) /* client disconnected */
189 if (control_queue(ctx->ps_control_client, buf, (size_t)len) == -1)
190 logerr("%s: control_queue", __func__);
194 ps_ctl_listen(void *arg)
196 struct dhcpcd_ctx *ctx = arg;
202 len = read(ctx->ps_control->fd, buf, sizeof(buf));
203 if (len == -1 || len == 0) {
204 logerr("%s: read", __func__);
205 eloop_exit(ctx->eloop, EXIT_FAILURE);
209 /* Send to our listeners */
210 TAILQ_FOREACH(fd, &ctx->control_fds, next) {
211 if (!(fd->flags & FD_LISTEN))
213 if (control_queue(fd, buf, (size_t)len)== -1)
214 logerr("%s: control_queue", __func__);
219 ps_ctl_start(struct dhcpcd_ctx *ctx)
221 int data_fd[2], listen_fd[2];
224 if (xsocketpair(AF_UNIX, SOCK_STREAM | SOCK_CXNB, 0, data_fd) == -1 ||
225 xsocketpair(AF_UNIX, SOCK_STREAM | SOCK_CXNB, 0, listen_fd) == -1)
227 #ifdef PRIVSEP_RIGHTS
228 if (ps_rights_limit_fdpair(data_fd) == -1 ||
229 ps_rights_limit_fdpair(listen_fd) == -1)
233 pid = ps_dostart(ctx, &ctx->ps_control_pid, &ctx->ps_control_fd,
234 ps_ctl_recvmsg, ps_ctl_dodispatch, ctx,
235 ps_ctl_startcb, NULL,
241 ctx->ps_control_data_fd = data_fd[1];
243 ctx->ps_control = control_new(ctx,
244 listen_fd[1], FD_SENDLEN | FD_LISTEN);
245 if (ctx->ps_control == NULL)
251 ctx->ps_control_data_fd = data_fd[0];
253 if (eloop_event_add(ctx->eloop, ctx->ps_control_data_fd,
254 ps_ctl_recv, ctx) == -1)
257 ctx->ps_control = control_new(ctx,
260 if (ctx->ps_control == NULL)
262 if (eloop_event_add(ctx->eloop, ctx->ps_control->fd,
263 ps_ctl_listen, ctx) == -1)
266 ps_entersandbox("stdio inet", NULL);
271 ps_ctl_stop(struct dhcpcd_ctx *ctx)
274 return ps_dostop(ctx, &ctx->ps_control_pid, &ctx->ps_control_fd);
278 ps_ctl_sendargs(struct fd_list *fd, void *data, size_t len)
280 struct dhcpcd_ctx *ctx = fd->ctx;
282 if (ctx->ps_control_client != NULL && ctx->ps_control_client != fd)
283 logerrx("%s: cannot deal with another client", __func__);
284 ctx->ps_control_client = fd;
285 return ps_sendcmd(ctx, ctx->ps_control_fd, PS_CTL,
286 fd->flags & FD_UNPRIV ? PS_CTL_UNPRIV : PS_CTL_PRIV,
291 ps_ctl_sendeof(struct fd_list *fd)
293 struct dhcpcd_ctx *ctx = fd->ctx;
295 return ps_sendcmd(ctx, ctx->ps_control_fd, PS_CTL_EOF, 0, NULL, 0);