1 /* $OpenBSD: control.c,v 1.11 2016/09/01 10:59:38 reyk Exp $ */ 2 3 /* 4 * Copyright (c) 2003, 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 USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/queue.h> 20 #include <sys/stat.h> 21 #include <sys/socket.h> 22 #include <sys/time.h> 23 #include <sys/un.h> 24 25 #include <errno.h> 26 #include <event.h> 27 #include <fcntl.h> 28 #include <stdlib.h> 29 #include <string.h> 30 #include <unistd.h> 31 #include <imsg.h> 32 33 #include "httpd.h" 34 35 #define CONTROL_BACKLOG 5 36 37 struct ctl_connlist ctl_conns; 38 39 void control_accept(int, short, void *); 40 void control_close(int, struct control_sock *); 41 42 int 43 control_init(struct privsep *ps, struct control_sock *cs) 44 { 45 struct httpd *env = ps->ps_env; 46 struct sockaddr_un sun; 47 int fd; 48 mode_t old_umask, mode; 49 50 if (cs->cs_name == NULL) 51 return (0); 52 53 if ((fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0)) == -1) { 54 log_warn("%s: socket", __func__); 55 return (-1); 56 } 57 58 sun.sun_family = AF_UNIX; 59 if (strlcpy(sun.sun_path, cs->cs_name, 60 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) { 61 log_warn("%s: %s name too long", __func__, cs->cs_name); 62 close(fd); 63 return (-1); 64 } 65 66 if (unlink(cs->cs_name) == -1) 67 if (errno != ENOENT) { 68 log_warn("%s: unlink %s", __func__, cs->cs_name); 69 close(fd); 70 return (-1); 71 } 72 73 if (cs->cs_restricted) { 74 old_umask = umask(S_IXUSR|S_IXGRP|S_IXOTH); 75 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; 76 } else { 77 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH); 78 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP; 79 } 80 81 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) { 82 log_warn("%s: bind: %s", __func__, cs->cs_name); 83 close(fd); 84 (void)umask(old_umask); 85 return (-1); 86 } 87 (void)umask(old_umask); 88 89 if (chmod(cs->cs_name, mode) == -1) { 90 log_warn("%s: chmod", __func__); 91 close(fd); 92 (void)unlink(cs->cs_name); 93 return (-1); 94 } 95 96 cs->cs_fd = fd; 97 cs->cs_env = env; 98 99 return (0); 100 } 101 102 int 103 control_listen(struct control_sock *cs) 104 { 105 if (cs->cs_name == NULL) 106 return (0); 107 108 if (listen(cs->cs_fd, CONTROL_BACKLOG) == -1) { 109 log_warn("%s: listen", __func__); 110 return (-1); 111 } 112 113 event_set(&cs->cs_ev, cs->cs_fd, EV_READ, 114 control_accept, cs); 115 event_add(&cs->cs_ev, NULL); 116 evtimer_set(&cs->cs_evt, control_accept, cs); 117 118 return (0); 119 } 120 121 void 122 control_cleanup(struct control_sock *cs) 123 { 124 if (cs->cs_name == NULL) 125 return; 126 event_del(&cs->cs_ev); 127 event_del(&cs->cs_evt); 128 } 129 130 /* ARGSUSED */ 131 void 132 control_accept(int listenfd, short event, void *arg) 133 { 134 int connfd; 135 socklen_t len; 136 struct sockaddr_un sun; 137 struct ctl_conn *c; 138 struct control_sock *cs = arg; 139 140 event_add(&cs->cs_ev, NULL); 141 if ((event & EV_TIMEOUT)) 142 return; 143 144 len = sizeof(sun); 145 if ((connfd = accept4(listenfd, 146 (struct sockaddr *)&sun, &len, SOCK_NONBLOCK)) == -1) { 147 /* 148 * Pause accept if we are out of file descriptors, or 149 * libevent will haunt us here too. 150 */ 151 if (errno == ENFILE || errno == EMFILE) { 152 struct timeval evtpause = { 1, 0 }; 153 154 event_del(&cs->cs_ev); 155 evtimer_add(&cs->cs_evt, &evtpause); 156 } else if (errno != EWOULDBLOCK && errno != EINTR && 157 errno != ECONNABORTED) 158 log_warn("%s: accept", __func__); 159 return; 160 } 161 162 if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) { 163 close(connfd); 164 log_warn("%s: calloc", __func__); 165 return; 166 } 167 168 imsg_init(&c->iev.ibuf, connfd); 169 c->iev.handler = control_dispatch_imsg; 170 c->iev.events = EV_READ; 171 c->iev.data = cs; /* proc.c cheats (reuses the handler) */ 172 event_set(&c->iev.ev, c->iev.ibuf.fd, c->iev.events, 173 c->iev.handler, cs); 174 event_add(&c->iev.ev, NULL); 175 176 TAILQ_INSERT_TAIL(&ctl_conns, c, entry); 177 } 178 179 struct ctl_conn * 180 control_connbyfd(int fd) 181 { 182 struct ctl_conn *c; 183 184 for (c = TAILQ_FIRST(&ctl_conns); c != NULL && c->iev.ibuf.fd != fd; 185 c = TAILQ_NEXT(c, entry)) 186 ; /* nothing */ 187 188 return (c); 189 } 190 191 void 192 control_close(int fd, struct control_sock *cs) 193 { 194 struct ctl_conn *c; 195 196 if ((c = control_connbyfd(fd)) == NULL) { 197 log_warn("%s: fd %d not found", __func__, fd); 198 return; 199 } 200 201 msgbuf_clear(&c->iev.ibuf.w); 202 TAILQ_REMOVE(&ctl_conns, c, entry); 203 204 event_del(&c->iev.ev); 205 close(c->iev.ibuf.fd); 206 207 /* Some file descriptors are available again. */ 208 if (evtimer_pending(&cs->cs_evt, NULL)) { 209 evtimer_del(&cs->cs_evt); 210 event_add(&cs->cs_ev, NULL); 211 } 212 213 free(c); 214 } 215 216 /* ARGSUSED */ 217 void 218 control_dispatch_imsg(int fd, short event, void *arg) 219 { 220 struct control_sock *cs = arg; 221 struct ctl_conn *c; 222 struct imsg imsg; 223 int n; 224 int verbose; 225 struct httpd *env = cs->cs_env; 226 227 if ((c = control_connbyfd(fd)) == NULL) { 228 log_warn("%s: fd %d not found", __func__, fd); 229 return; 230 } 231 232 if (event & EV_READ) { 233 if (((n = imsg_read(&c->iev.ibuf)) == -1 && errno != EAGAIN) || 234 n == 0) { 235 control_close(fd, cs); 236 return; 237 } 238 } 239 240 if (event & EV_WRITE) { 241 if (msgbuf_write(&c->iev.ibuf.w) <= 0 && errno != EAGAIN) { 242 control_close(fd, cs); 243 return; 244 } 245 } 246 247 for (;;) { 248 if ((n = imsg_get(&c->iev.ibuf, &imsg)) == -1) { 249 control_close(fd, cs); 250 return; 251 } 252 253 if (n == 0) 254 break; 255 256 if (c->waiting) { 257 log_debug("%s: unexpected imsg %d", 258 __func__, imsg.hdr.type); 259 imsg_free(&imsg); 260 control_close(fd, cs); 261 return; 262 } 263 264 switch (imsg.hdr.type) { 265 case IMSG_CTL_SHUTDOWN: 266 case IMSG_CTL_RELOAD: 267 case IMSG_CTL_REOPEN: 268 proc_forward_imsg(env->sc_ps, &imsg, PROC_PARENT, -1); 269 break; 270 case IMSG_CTL_NOTIFY: 271 if (c->flags & CTL_CONN_NOTIFY) { 272 log_debug("%s: " 273 "client requested notify more than once", 274 __func__); 275 imsg_compose_event(&c->iev, IMSG_CTL_FAIL, 276 0, env->sc_ps->ps_instance + 1, -1, 277 NULL, 0); 278 break; 279 } 280 c->flags |= CTL_CONN_NOTIFY; 281 break; 282 case IMSG_CTL_VERBOSE: 283 IMSG_SIZE_CHECK(&imsg, &verbose); 284 285 memcpy(&verbose, imsg.data, sizeof(verbose)); 286 287 proc_forward_imsg(env->sc_ps, &imsg, PROC_PARENT, -1); 288 proc_forward_imsg(env->sc_ps, &imsg, PROC_SERVER, -1); 289 290 memcpy(imsg.data, &verbose, sizeof(verbose)); 291 control_imsg_forward(env->sc_ps, &imsg); 292 log_verbose(verbose); 293 break; 294 default: 295 log_debug("%s: error handling imsg %d", 296 __func__, imsg.hdr.type); 297 break; 298 } 299 imsg_free(&imsg); 300 } 301 302 imsg_event_add(&c->iev); 303 } 304 305 void 306 control_imsg_forward(struct privsep *ps, struct imsg *imsg) 307 { 308 struct ctl_conn *c; 309 310 TAILQ_FOREACH(c, &ctl_conns, entry) 311 if (c->flags & CTL_CONN_NOTIFY) 312 imsg_compose_event(&c->iev, imsg->hdr.type, 313 0, ps->ps_instance + 1, -1, imsg->data, 314 imsg->hdr.len - IMSG_HEADER_SIZE); 315 } 316