control.c revision 1.30
1/* $OpenBSD: control.c,v 1.30 2020/10/09 08:59:15 tobhe Exp $ */ 2 3/* 4 * Copyright (c) 2010-2013 Reyk Floeter <reyk@openbsd.org> 5 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20#include <sys/queue.h> 21#include <sys/stat.h> 22#include <sys/socket.h> 23#include <sys/un.h> 24#include <sys/tree.h> 25 26#include <errno.h> 27#include <event.h> 28#include <fcntl.h> 29#include <stdlib.h> 30#include <string.h> 31#include <unistd.h> 32#include <signal.h> 33 34#include "iked.h" 35 36#define CONTROL_BACKLOG 5 37 38struct ctl_connlist ctl_conns; 39 40void 41 control_accept(int, short, void *); 42struct ctl_conn 43 *control_connbyfd(int); 44void control_close(int, struct control_sock *); 45void control_dispatch_imsg(int, short, void *); 46void control_dispatch_parent(int, short, void *); 47void control_imsg_forward(struct imsg *); 48void control_run(struct privsep *, struct privsep_proc *, void *); 49int control_dispatch_ikev2(int, struct privsep_proc *, struct imsg *); 50 51static struct privsep_proc procs[] = { 52 { "parent", PROC_PARENT, NULL }, 53 { "ikev2", PROC_IKEV2, control_dispatch_ikev2 }, 54}; 55 56pid_t 57control(struct privsep *ps, struct privsep_proc *p) 58{ 59 return (proc_run(ps, p, procs, nitems(procs), control_run, NULL)); 60} 61 62void 63control_run(struct privsep *ps, struct privsep_proc *p, void *arg) 64{ 65 /* 66 * pledge in the control process: 67 * stdio - for malloc and basic I/O including events. 68 * unix - for the control socket. 69 */ 70 if (pledge("stdio unix", NULL) == -1) 71 fatal("pledge"); 72} 73 74int 75control_init(struct privsep *ps, struct control_sock *cs) 76{ 77 struct iked *env = ps->ps_env; 78 struct sockaddr_un sun; 79 int fd; 80 mode_t old_umask, mode; 81 82 if (cs->cs_name == NULL) 83 return (0); 84 85 if ((fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0)) == -1) { 86 log_warn("%s: socket", __func__); 87 return (-1); 88 } 89 90 sun.sun_family = AF_UNIX; 91 if (strlcpy(sun.sun_path, cs->cs_name, 92 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) { 93 log_warn("%s: %s name too long", __func__, cs->cs_name); 94 close(fd); 95 return (-1); 96 } 97 98 if (unlink(cs->cs_name) == -1) 99 if (errno != ENOENT) { 100 log_warn("%s: unlink %s", __func__, cs->cs_name); 101 close(fd); 102 return (-1); 103 } 104 105 if (cs->cs_restricted) { 106 old_umask = umask(S_IXUSR|S_IXGRP|S_IXOTH); 107 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; 108 } else { 109 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH); 110 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP; 111 } 112 113 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) { 114 log_warn("%s: bind: %s", __func__, cs->cs_name); 115 close(fd); 116 (void)umask(old_umask); 117 return (-1); 118 } 119 (void)umask(old_umask); 120 121 if (chmod(cs->cs_name, mode) == -1) { 122 log_warn("%s: chmod", __func__); 123 close(fd); 124 (void)unlink(cs->cs_name); 125 return (-1); 126 } 127 128 cs->cs_fd = fd; 129 cs->cs_env = env; 130 131 return (0); 132} 133 134int 135control_listen(struct control_sock *cs) 136{ 137 if (cs->cs_name == NULL) 138 return (0); 139 140 if (listen(cs->cs_fd, CONTROL_BACKLOG) == -1) { 141 log_warn("%s: listen", __func__); 142 return (-1); 143 } 144 145 event_set(&cs->cs_ev, cs->cs_fd, EV_READ, 146 control_accept, cs); 147 event_add(&cs->cs_ev, NULL); 148 evtimer_set(&cs->cs_evt, control_accept, cs); 149 150 return (0); 151} 152 153/* ARGSUSED */ 154void 155control_accept(int listenfd, short event, void *arg) 156{ 157 struct control_sock *cs = arg; 158 int connfd; 159 socklen_t len; 160 struct sockaddr_un sun; 161 struct ctl_conn *c; 162 163 event_add(&cs->cs_ev, NULL); 164 if ((event & EV_TIMEOUT)) 165 return; 166 167 len = sizeof(sun); 168 if ((connfd = accept4(listenfd, 169 (struct sockaddr *)&sun, &len, SOCK_NONBLOCK)) == -1) { 170 /* 171 * Pause accept if we are out of file descriptors, or 172 * libevent will haunt us here too. 173 */ 174 if (errno == ENFILE || errno == EMFILE) { 175 struct timeval evtpause = { 1, 0 }; 176 177 event_del(&cs->cs_ev); 178 evtimer_add(&cs->cs_evt, &evtpause); 179 } else if (errno != EWOULDBLOCK && errno != EINTR && 180 errno != ECONNABORTED) 181 log_warn("%s: accept", __func__); 182 return; 183 } 184 185 if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) { 186 log_warn("%s", __func__); 187 close(connfd); 188 return; 189 } 190 191 imsg_init(&c->iev.ibuf, connfd); 192 c->iev.handler = control_dispatch_imsg; 193 c->iev.events = EV_READ; 194 c->iev.data = cs; 195 event_set(&c->iev.ev, c->iev.ibuf.fd, c->iev.events, 196 c->iev.handler, c->iev.data); 197 event_add(&c->iev.ev, NULL); 198 199 TAILQ_INSERT_TAIL(&ctl_conns, c, entry); 200} 201 202struct ctl_conn * 203control_connbyfd(int fd) 204{ 205 struct ctl_conn *c; 206 207 TAILQ_FOREACH(c, &ctl_conns, entry) { 208 if (c->iev.ibuf.fd == fd) 209 break; 210 } 211 212 return (c); 213} 214 215void 216control_close(int fd, struct control_sock *cs) 217{ 218 struct ctl_conn *c; 219 220 if ((c = control_connbyfd(fd)) == NULL) { 221 log_warn("%s: fd %d: not found", __func__, fd); 222 return; 223 } 224 225 msgbuf_clear(&c->iev.ibuf.w); 226 TAILQ_REMOVE(&ctl_conns, c, entry); 227 228 event_del(&c->iev.ev); 229 close(c->iev.ibuf.fd); 230 231 /* Some file descriptors are available again. */ 232 if (evtimer_pending(&cs->cs_evt, NULL)) { 233 evtimer_del(&cs->cs_evt); 234 event_add(&cs->cs_ev, NULL); 235 } 236 237 free(c); 238} 239 240/* ARGSUSED */ 241void 242control_dispatch_imsg(int fd, short event, void *arg) 243{ 244 struct control_sock *cs = arg; 245 struct iked *env = cs->cs_env; 246 struct ctl_conn *c; 247 struct imsg imsg; 248 int n, v; 249 250 if ((c = control_connbyfd(fd)) == NULL) { 251 log_warn("%s: fd %d: not found", __func__, fd); 252 return; 253 } 254 255 if (event & EV_READ) { 256 if (((n = imsg_read(&c->iev.ibuf)) == -1 && errno != EAGAIN) || 257 n == 0) { 258 control_close(fd, cs); 259 return; 260 } 261 } 262 if (event & EV_WRITE) { 263 if (msgbuf_write(&c->iev.ibuf.w) <= 0 && errno != EAGAIN) { 264 control_close(fd, cs); 265 return; 266 } 267 } 268 269 for (;;) { 270 if ((n = imsg_get(&c->iev.ibuf, &imsg)) == -1) { 271 control_close(fd, cs); 272 return; 273 } 274 275 if (n == 0) 276 break; 277 278 control_imsg_forward(&imsg); 279 280 switch (imsg.hdr.type) { 281 case IMSG_CTL_NOTIFY: 282 if (c->flags & CTL_CONN_NOTIFY) { 283 log_debug("%s: " 284 "client requested notify more than once", 285 __func__); 286 imsg_compose_event(&c->iev, IMSG_CTL_FAIL, 287 0, 0, -1, NULL, 0); 288 break; 289 } 290 c->flags |= CTL_CONN_NOTIFY; 291 break; 292 case IMSG_CTL_VERBOSE: 293 IMSG_SIZE_CHECK(&imsg, &v); 294 295 memcpy(&v, imsg.data, sizeof(v)); 296 log_setverbose(v); 297 298 proc_forward_imsg(&env->sc_ps, &imsg, PROC_PARENT, -1); 299 break; 300 case IMSG_CTL_RELOAD: 301 case IMSG_CTL_RESET: 302 case IMSG_CTL_COUPLE: 303 case IMSG_CTL_DECOUPLE: 304 case IMSG_CTL_ACTIVE: 305 case IMSG_CTL_PASSIVE: 306 proc_forward_imsg(&env->sc_ps, &imsg, PROC_PARENT, -1); 307 break; 308 case IMSG_CTL_RESET_ID: 309 proc_forward_imsg(&env->sc_ps, &imsg, PROC_IKEV2, -1); 310 break; 311 case IMSG_CTL_SHOW_SA: 312 proc_forward_imsg(&env->sc_ps, &imsg, PROC_IKEV2, -1); 313 c->flags |= CTL_CONN_NOTIFY; 314 break; 315 default: 316 log_debug("%s: error handling imsg %d", 317 __func__, imsg.hdr.type); 318 break; 319 } 320 imsg_free(&imsg); 321 } 322 323 imsg_event_add(&c->iev); 324} 325 326void 327control_imsg_forward(struct imsg *imsg) 328{ 329 struct ctl_conn *c; 330 331 TAILQ_FOREACH(c, &ctl_conns, entry) 332 if (c->flags & CTL_CONN_NOTIFY) 333 imsg_compose_event(&c->iev, imsg->hdr.type, 334 0, imsg->hdr.pid, -1, imsg->data, 335 imsg->hdr.len - IMSG_HEADER_SIZE); 336} 337 338int 339control_dispatch_ikev2(int fd, struct privsep_proc *p, struct imsg *imsg) 340{ 341 switch (imsg->hdr.type) { 342 case IMSG_CTL_SHOW_SA: 343 control_imsg_forward(imsg); 344 return (0); 345 default: 346 break; 347 } 348 349 return (-1); 350} 351