1 /* $OpenBSD: control.c,v 1.32 2021/11/21 22:44:08 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 38 struct ctl_connlist ctl_conns = TAILQ_HEAD_INITIALIZER(ctl_conns); 39 40 void 41 control_accept(int, short, void *); 42 struct ctl_conn 43 *control_connbyfd(int); 44 void control_close(int, struct control_sock *); 45 void control_dispatch_imsg(int, short, void *); 46 void control_dispatch_parent(int, short, void *); 47 void control_imsg_forward(struct imsg *); 48 void control_run(struct privsep *, struct privsep_proc *, void *); 49 int control_dispatch_ikev2(int, struct privsep_proc *, struct imsg *); 50 int control_dispatch_ca(int, struct privsep_proc *, struct imsg *); 51 52 static struct privsep_proc procs[] = { 53 { "parent", PROC_PARENT, NULL }, 54 { "ikev2", PROC_IKEV2, control_dispatch_ikev2 }, 55 { "ca", PROC_CERT, control_dispatch_ca }, 56 }; 57 58 pid_t 59 control(struct privsep *ps, struct privsep_proc *p) 60 { 61 return (proc_run(ps, p, procs, nitems(procs), control_run, NULL)); 62 } 63 64 void 65 control_run(struct privsep *ps, struct privsep_proc *p, void *arg) 66 { 67 /* 68 * pledge in the control process: 69 * stdio - for malloc and basic I/O including events. 70 * unix - for the control socket. 71 */ 72 if (pledge("stdio unix", NULL) == -1) 73 fatal("pledge"); 74 } 75 76 int 77 control_init(struct privsep *ps, struct control_sock *cs) 78 { 79 struct iked *env = ps->ps_env; 80 struct sockaddr_un sun; 81 int fd; 82 mode_t old_umask, mode; 83 84 if (cs->cs_name == NULL) 85 return (0); 86 87 if ((fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0)) == -1) { 88 log_warn("%s: socket", __func__); 89 return (-1); 90 } 91 92 sun.sun_family = AF_UNIX; 93 if (strlcpy(sun.sun_path, cs->cs_name, 94 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) { 95 log_warn("%s: %s name too long", __func__, cs->cs_name); 96 close(fd); 97 return (-1); 98 } 99 100 if (unlink(cs->cs_name) == -1) 101 if (errno != ENOENT) { 102 log_warn("%s: unlink %s", __func__, cs->cs_name); 103 close(fd); 104 return (-1); 105 } 106 107 if (cs->cs_restricted) { 108 old_umask = umask(S_IXUSR|S_IXGRP|S_IXOTH); 109 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; 110 } else { 111 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH); 112 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP; 113 } 114 115 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) { 116 log_warn("%s: bind: %s", __func__, cs->cs_name); 117 close(fd); 118 (void)umask(old_umask); 119 return (-1); 120 } 121 (void)umask(old_umask); 122 123 if (chmod(cs->cs_name, mode) == -1) { 124 log_warn("%s: chmod", __func__); 125 close(fd); 126 (void)unlink(cs->cs_name); 127 return (-1); 128 } 129 130 cs->cs_fd = fd; 131 cs->cs_env = env; 132 133 return (0); 134 } 135 136 int 137 control_listen(struct control_sock *cs) 138 { 139 if (cs->cs_name == NULL) 140 return (0); 141 142 if (listen(cs->cs_fd, CONTROL_BACKLOG) == -1) { 143 log_warn("%s: listen", __func__); 144 return (-1); 145 } 146 147 event_set(&cs->cs_ev, cs->cs_fd, EV_READ, 148 control_accept, cs); 149 event_add(&cs->cs_ev, NULL); 150 evtimer_set(&cs->cs_evt, control_accept, cs); 151 152 return (0); 153 } 154 155 /* ARGSUSED */ 156 void 157 control_accept(int listenfd, short event, void *arg) 158 { 159 struct control_sock *cs = arg; 160 int connfd; 161 socklen_t len; 162 struct sockaddr_un sun; 163 struct ctl_conn *c; 164 165 event_add(&cs->cs_ev, NULL); 166 if ((event & EV_TIMEOUT)) 167 return; 168 169 len = sizeof(sun); 170 if ((connfd = accept4(listenfd, 171 (struct sockaddr *)&sun, &len, SOCK_NONBLOCK)) == -1) { 172 /* 173 * Pause accept if we are out of file descriptors, or 174 * libevent will haunt us here too. 175 */ 176 if (errno == ENFILE || errno == EMFILE) { 177 struct timeval evtpause = { 1, 0 }; 178 179 event_del(&cs->cs_ev); 180 evtimer_add(&cs->cs_evt, &evtpause); 181 } else if (errno != EWOULDBLOCK && errno != EINTR && 182 errno != ECONNABORTED) 183 log_warn("%s: accept", __func__); 184 return; 185 } 186 187 if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) { 188 log_warn("%s", __func__); 189 close(connfd); 190 return; 191 } 192 193 imsg_init(&c->iev.ibuf, connfd); 194 c->iev.handler = control_dispatch_imsg; 195 c->iev.events = EV_READ; 196 c->iev.data = cs; 197 event_set(&c->iev.ev, c->iev.ibuf.fd, c->iev.events, 198 c->iev.handler, c->iev.data); 199 event_add(&c->iev.ev, NULL); 200 201 TAILQ_INSERT_TAIL(&ctl_conns, c, entry); 202 } 203 204 struct ctl_conn * 205 control_connbyfd(int fd) 206 { 207 struct ctl_conn *c; 208 209 TAILQ_FOREACH(c, &ctl_conns, entry) { 210 if (c->iev.ibuf.fd == fd) 211 break; 212 } 213 214 return (c); 215 } 216 217 void 218 control_close(int fd, struct control_sock *cs) 219 { 220 struct ctl_conn *c; 221 222 if ((c = control_connbyfd(fd)) == NULL) { 223 log_warn("%s: fd %d: not found", __func__, fd); 224 return; 225 } 226 227 msgbuf_clear(&c->iev.ibuf.w); 228 TAILQ_REMOVE(&ctl_conns, c, entry); 229 230 event_del(&c->iev.ev); 231 close(c->iev.ibuf.fd); 232 233 /* Some file descriptors are available again. */ 234 if (evtimer_pending(&cs->cs_evt, NULL)) { 235 evtimer_del(&cs->cs_evt); 236 event_add(&cs->cs_ev, NULL); 237 } 238 239 free(c); 240 } 241 242 /* ARGSUSED */ 243 void 244 control_dispatch_imsg(int fd, short event, void *arg) 245 { 246 struct control_sock *cs = arg; 247 struct iked *env = cs->cs_env; 248 struct ctl_conn *c; 249 struct imsg imsg; 250 int n, v; 251 252 if ((c = control_connbyfd(fd)) == NULL) { 253 log_warn("%s: fd %d: not found", __func__, fd); 254 return; 255 } 256 257 if (event & EV_READ) { 258 if (((n = imsg_read(&c->iev.ibuf)) == -1 && errno != EAGAIN) || 259 n == 0) { 260 control_close(fd, cs); 261 return; 262 } 263 } 264 if (event & EV_WRITE) { 265 if (msgbuf_write(&c->iev.ibuf.w) <= 0 && errno != EAGAIN) { 266 control_close(fd, cs); 267 return; 268 } 269 } 270 271 for (;;) { 272 if ((n = imsg_get(&c->iev.ibuf, &imsg)) == -1) { 273 control_close(fd, cs); 274 return; 275 } 276 277 if (n == 0) 278 break; 279 280 control_imsg_forward(&imsg); 281 282 switch (imsg.hdr.type) { 283 case IMSG_CTL_NOTIFY: 284 if (c->flags & CTL_CONN_NOTIFY) { 285 log_debug("%s: " 286 "client requested notify more than once", 287 __func__); 288 imsg_compose_event(&c->iev, IMSG_CTL_FAIL, 289 0, 0, -1, NULL, 0); 290 break; 291 } 292 c->flags |= CTL_CONN_NOTIFY; 293 break; 294 case IMSG_CTL_VERBOSE: 295 IMSG_SIZE_CHECK(&imsg, &v); 296 297 memcpy(&v, imsg.data, sizeof(v)); 298 log_setverbose(v); 299 300 proc_forward_imsg(&env->sc_ps, &imsg, PROC_PARENT, -1); 301 break; 302 case IMSG_CTL_RELOAD: 303 case IMSG_CTL_RESET: 304 case IMSG_CTL_COUPLE: 305 case IMSG_CTL_DECOUPLE: 306 case IMSG_CTL_ACTIVE: 307 case IMSG_CTL_PASSIVE: 308 proc_forward_imsg(&env->sc_ps, &imsg, PROC_PARENT, -1); 309 break; 310 case IMSG_CTL_RESET_ID: 311 proc_forward_imsg(&env->sc_ps, &imsg, PROC_IKEV2, -1); 312 break; 313 case IMSG_CTL_SHOW_SA: 314 proc_forward_imsg(&env->sc_ps, &imsg, PROC_IKEV2, -1); 315 c->flags |= CTL_CONN_NOTIFY; 316 break; 317 case IMSG_CTL_SHOW_CERTSTORE: 318 proc_forward_imsg(&env->sc_ps, &imsg, PROC_CERT, -1); 319 c->flags |= CTL_CONN_NOTIFY; 320 break; 321 default: 322 log_debug("%s: error handling imsg %d", 323 __func__, imsg.hdr.type); 324 break; 325 } 326 imsg_free(&imsg); 327 } 328 329 imsg_event_add(&c->iev); 330 } 331 332 void 333 control_imsg_forward(struct imsg *imsg) 334 { 335 struct ctl_conn *c; 336 337 TAILQ_FOREACH(c, &ctl_conns, entry) 338 if (c->flags & CTL_CONN_NOTIFY) 339 imsg_compose_event(&c->iev, imsg->hdr.type, 340 0, imsg->hdr.pid, -1, imsg->data, 341 imsg->hdr.len - IMSG_HEADER_SIZE); 342 } 343 344 int 345 control_dispatch_ikev2(int fd, struct privsep_proc *p, struct imsg *imsg) 346 { 347 switch (imsg->hdr.type) { 348 case IMSG_CTL_SHOW_SA: 349 control_imsg_forward(imsg); 350 return (0); 351 default: 352 break; 353 } 354 355 return (-1); 356 } 357 358 int 359 control_dispatch_ca(int fd, struct privsep_proc *p, struct imsg *imsg) 360 { 361 switch (imsg->hdr.type) { 362 case IMSG_CTL_SHOW_CERTSTORE: 363 control_imsg_forward(imsg); 364 return (0); 365 default: 366 break; 367 } 368 369 return (-1); 370 } 371