Blob


1 /*
2 * Copyright (c) 2024 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
20 #include <sys/socket.h>
21 #include <sys/wait.h>
23 #include <errno.h>
24 #include <event.h>
25 #include <siphash.h>
26 #include <limits.h>
27 #include <sha1.h>
28 #include <sha2.h>
29 #include <signal.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <imsg.h>
34 #include <unistd.h>
36 #include "got_error.h"
37 #include "got_path.h"
39 #include "gotd.h"
40 #include "log.h"
41 #include "notify.h"
43 #ifndef nitems
44 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
45 #endif
47 static struct gotd_notify {
48 pid_t pid;
49 const char *title;
50 struct gotd_imsgev parent_iev;
51 struct gotd_repolist *repos;
52 const char *default_sender;
53 } gotd_notify;
55 struct gotd_notify_session {
56 STAILQ_ENTRY(gotd_notify_session) entry;
57 uint32_t id;
58 struct gotd_imsgev iev;
59 };
60 STAILQ_HEAD(gotd_notify_sessions, gotd_notify_session);
62 static struct gotd_notify_sessions gotd_notify_sessions[GOTD_CLIENT_TABLE_SIZE];
63 static SIPHASH_KEY sessions_hash_key;
65 static void gotd_notify_shutdown(void);
67 static uint64_t
68 session_hash(uint32_t session_id)
69 {
70 return SipHash24(&sessions_hash_key, &session_id, sizeof(session_id));
71 }
73 static void
74 add_session(struct gotd_notify_session *session)
75 {
76 uint64_t slot;
78 slot = session_hash(session->id) % nitems(gotd_notify_sessions);
79 STAILQ_INSERT_HEAD(&gotd_notify_sessions[slot], session, entry);
80 }
82 static struct gotd_notify_session *
83 find_session(uint32_t session_id)
84 {
85 uint64_t slot;
86 struct gotd_notify_session *s;
88 slot = session_hash(session_id) % nitems(gotd_notify_sessions);
89 STAILQ_FOREACH(s, &gotd_notify_sessions[slot], entry) {
90 if (s->id == session_id)
91 return s;
92 }
94 return NULL;
95 }
97 static struct gotd_notify_session *
98 find_session_by_fd(int fd)
99 {
100 uint64_t slot;
101 struct gotd_notify_session *s;
103 for (slot = 0; slot < nitems(gotd_notify_sessions); slot++) {
104 STAILQ_FOREACH(s, &gotd_notify_sessions[slot], entry) {
105 if (s->iev.ibuf.fd == fd)
106 return s;
110 return NULL;
113 static void
114 remove_session(struct gotd_notify_session *session)
116 uint64_t slot;
118 slot = session_hash(session->id) % nitems(gotd_notify_sessions);
119 STAILQ_REMOVE(&gotd_notify_sessions[slot], session,
120 gotd_notify_session, entry);
121 free(session);
124 static uint32_t
125 get_session_id(void)
127 int duplicate = 0;
128 uint32_t id;
130 do {
131 id = arc4random();
132 duplicate = (find_session(id) != NULL);
133 } while (duplicate || id == 0);
135 return id;
138 static void
139 gotd_notify_sighdlr(int sig, short event, void *arg)
141 /*
142 * Normal signal handler rules don't apply because libevent
143 * decouples for us.
144 */
146 switch (sig) {
147 case SIGHUP:
148 log_info("%s: ignoring SIGHUP", __func__);
149 break;
150 case SIGUSR1:
151 log_info("%s: ignoring SIGUSR1", __func__);
152 break;
153 case SIGTERM:
154 case SIGINT:
155 gotd_notify_shutdown();
156 /* NOTREACHED */
157 break;
158 default:
159 fatalx("unexpected signal");
163 static void
164 run_notification_helper(const char *prog, const char **argv, int fd)
166 const struct got_error *err = NULL;
167 pid_t pid;
168 int child_status;
170 pid = fork();
171 if (pid == -1) {
172 err = got_error_from_errno("fork");
173 log_warn("%s", err->msg);
174 return;
175 } else if (pid == 0) {
176 signal(SIGQUIT, SIG_DFL);
177 signal(SIGINT, SIG_DFL);
178 signal(SIGCHLD, SIG_DFL);
180 if (dup2(fd, STDIN_FILENO) == -1) {
181 fprintf(stderr, "%s: dup2: %s\n", getprogname(),
182 strerror(errno));
183 _exit(1);
186 closefrom(STDERR_FILENO + 1);
188 if (execv(prog, (char *const *)argv) == -1) {
189 fprintf(stderr, "%s: exec %s: %s\n", getprogname(),
190 prog, strerror(errno));
191 _exit(1);
194 /* not reached */
197 if (waitpid(pid, &child_status, 0) == -1) {
198 err = got_error_from_errno("waitpid");
199 goto done;
202 if (!WIFEXITED(child_status)) {
203 err = got_error(GOT_ERR_PRIVSEP_DIED);
204 goto done;
207 if (WEXITSTATUS(child_status) != 0)
208 err = got_error(GOT_ERR_PRIVSEP_EXIT);
209 done:
210 if (err)
211 log_warnx("%s: child %s pid %d: %s", gotd_notify.title,
212 prog, pid, err->msg);
215 static void
216 notify_email(struct gotd_notification_target *target, const char *subject_line,
217 int fd)
219 const char *argv[13];
220 int i = 0;
222 argv[i++] = GOTD_PATH_PROG_NOTIFY_EMAIL;
224 argv[i++] = "-f";
225 if (target->conf.email.sender)
226 argv[i++] = target->conf.email.sender;
227 else
228 argv[i++] = gotd_notify.default_sender;
230 if (target->conf.email.responder) {
231 argv[i++] = "-r";
232 argv[i++] = target->conf.email.responder;
235 if (target->conf.email.hostname) {
236 argv[i++] = "-h";
237 argv[i++] = target->conf.email.hostname;
240 if (target->conf.email.port) {
241 argv[i++] = "-p";
242 argv[i++] = target->conf.email.port;
245 argv[i++] = "-s";
246 argv[i++] = subject_line;
248 argv[i++] = target->conf.email.recipient;
250 argv[i] = NULL;
252 run_notification_helper(GOTD_PATH_PROG_NOTIFY_EMAIL, argv, fd);
255 static void
256 notify_http(struct gotd_notification_target *target, int fd)
258 const char *argv[8];
259 int argc = 0;
261 argv[argc++] = GOTD_PATH_PROG_NOTIFY_HTTP;
262 if (target->conf.http.tls)
263 argv[argc++] = "-c";
265 argv[argc++] = "-h";
266 argv[argc++] = target->conf.http.hostname;
267 argv[argc++] = "-p";
268 argv[argc++] = target->conf.http.port;
270 argv[argc++] = target->conf.http.path;
272 argv[argc] = NULL;
274 run_notification_helper(GOTD_PATH_PROG_NOTIFY_HTTP, argv, fd);
277 static const struct got_error *
278 send_notification(struct imsg *imsg, struct gotd_imsgev *iev)
280 const struct got_error *err = NULL;
281 struct gotd_imsg_notify inotify;
282 size_t datalen;
283 struct gotd_repo *repo;
284 struct gotd_notification_target *target;
285 int fd;
287 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
288 if (datalen != sizeof(inotify))
289 return got_error(GOT_ERR_PRIVSEP_LEN);
291 memcpy(&inotify, imsg->data, datalen);
293 repo = gotd_find_repo_by_name(inotify.repo_name, gotd_notify.repos);
294 if (repo == NULL)
295 return got_error(GOT_ERR_PRIVSEP_MSG);
297 fd = imsg_get_fd(imsg);
298 if (fd == -1)
299 return got_error(GOT_ERR_PRIVSEP_NO_FD);
301 if (lseek(fd, 0, SEEK_SET) == -1) {
302 err = got_error_from_errno("lseek");
303 goto done;
306 STAILQ_FOREACH(target, &repo->notification_targets, entry) {
307 switch (target->type) {
308 case GOTD_NOTIFICATION_VIA_EMAIL:
309 notify_email(target, inotify.subject_line, fd);
310 break;
311 case GOTD_NOTIFICATION_VIA_HTTP:
312 notify_http(target, fd);
313 break;
317 if (gotd_imsg_compose_event(iev, GOTD_IMSG_NOTIFICATION_SENT,
318 PROC_NOTIFY, -1, NULL, 0) == -1) {
319 err = got_error_from_errno("imsg compose NOTIFY");
320 goto done;
322 done:
323 close(fd);
324 return err;
327 static void
328 notify_dispatch_session(int fd, short event, void *arg)
330 struct gotd_imsgev *iev = arg;
331 struct imsgbuf *ibuf = &iev->ibuf;
332 ssize_t n;
333 int shut = 0;
334 struct imsg imsg;
336 if (event & EV_READ) {
337 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
338 fatal("imsg_read error");
339 if (n == 0) {
340 /* Connection closed. */
341 shut = 1;
342 goto done;
346 if (event & EV_WRITE) {
347 n = msgbuf_write(&ibuf->w);
348 if (n == -1 && errno != EAGAIN)
349 fatal("msgbuf_write");
350 if (n == 0) {
351 /* Connection closed. */
352 shut = 1;
353 goto done;
357 for (;;) {
358 const struct got_error *err = NULL;
360 if ((n = imsg_get(ibuf, &imsg)) == -1)
361 fatal("%s: imsg_get error", __func__);
362 if (n == 0) /* No more messages. */
363 break;
365 switch (imsg.hdr.type) {
366 case GOTD_IMSG_NOTIFY:
367 err = send_notification(&imsg, iev);
368 break;
369 default:
370 log_debug("unexpected imsg %d", imsg.hdr.type);
371 break;
373 imsg_free(&imsg);
375 if (err)
376 log_warnx("%s: %s", __func__, err->msg);
378 done:
379 if (!shut) {
380 gotd_imsg_event_add(iev);
381 } else {
382 struct gotd_notify_session *session;
384 /* This pipe is dead. Remove its event handler */
385 event_del(&iev->ev);
386 imsg_clear(&iev->ibuf);
388 session = find_session_by_fd(fd);
389 if (session)
390 remove_session(session);
394 static const struct got_error *
395 recv_session(struct imsg *imsg)
397 struct gotd_notify_session *session;
398 size_t datalen;
399 int fd;
401 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
402 if (datalen != 0)
403 return got_error(GOT_ERR_PRIVSEP_LEN);
405 fd = imsg_get_fd(imsg);
406 if (fd == -1)
407 return got_error(GOT_ERR_PRIVSEP_NO_FD);
409 session = calloc(1, sizeof(*session));
410 if (session == NULL)
411 return got_error_from_errno("calloc");
413 session->id = get_session_id();
414 imsg_init(&session->iev.ibuf, fd);
415 session->iev.handler = notify_dispatch_session;
416 session->iev.events = EV_READ;
417 session->iev.handler_arg = NULL;
418 event_set(&session->iev.ev, session->iev.ibuf.fd, EV_READ,
419 notify_dispatch_session, &session->iev);
420 gotd_imsg_event_add(&session->iev);
421 add_session(session);
423 return NULL;
426 static void
427 notify_dispatch(int fd, short event, void *arg)
429 struct gotd_imsgev *iev = arg;
430 struct imsgbuf *ibuf = &iev->ibuf;
431 ssize_t n;
432 int shut = 0;
433 struct imsg imsg;
435 if (event & EV_READ) {
436 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
437 fatal("imsg_read error");
438 if (n == 0) {
439 /* Connection closed. */
440 shut = 1;
441 goto done;
445 if (event & EV_WRITE) {
446 n = msgbuf_write(&ibuf->w);
447 if (n == -1 && errno != EAGAIN)
448 fatal("msgbuf_write");
449 if (n == 0) {
450 /* Connection closed. */
451 shut = 1;
452 goto done;
456 for (;;) {
457 const struct got_error *err = NULL;
459 if ((n = imsg_get(ibuf, &imsg)) == -1)
460 fatal("%s: imsg_get error", __func__);
461 if (n == 0) /* No more messages. */
462 break;
464 switch (imsg.hdr.type) {
465 case GOTD_IMSG_CONNECT_SESSION:
466 err = recv_session(&imsg);
467 break;
468 default:
469 log_debug("unexpected imsg %d", imsg.hdr.type);
470 break;
472 imsg_free(&imsg);
474 if (err)
475 log_warnx("%s: %s", __func__, err->msg);
477 done:
478 if (!shut) {
479 gotd_imsg_event_add(iev);
480 } else {
481 /* This pipe is dead. Remove its event handler */
482 event_del(&iev->ev);
483 event_loopexit(NULL);
488 void
489 notify_main(const char *title, struct gotd_repolist *repos,
490 const char *default_sender)
492 const struct got_error *err = NULL;
493 struct event evsigint, evsigterm, evsighup, evsigusr1;
495 arc4random_buf(&sessions_hash_key, sizeof(sessions_hash_key));
497 gotd_notify.title = title;
498 gotd_notify.repos = repos;
499 gotd_notify.default_sender = default_sender;
500 gotd_notify.pid = getpid();
502 signal_set(&evsigint, SIGINT, gotd_notify_sighdlr, NULL);
503 signal_set(&evsigterm, SIGTERM, gotd_notify_sighdlr, NULL);
504 signal_set(&evsighup, SIGHUP, gotd_notify_sighdlr, NULL);
505 signal_set(&evsigusr1, SIGUSR1, gotd_notify_sighdlr, NULL);
506 signal(SIGPIPE, SIG_IGN);
508 signal_add(&evsigint, NULL);
509 signal_add(&evsigterm, NULL);
510 signal_add(&evsighup, NULL);
511 signal_add(&evsigusr1, NULL);
513 imsg_init(&gotd_notify.parent_iev.ibuf, GOTD_FILENO_MSG_PIPE);
514 gotd_notify.parent_iev.handler = notify_dispatch;
515 gotd_notify.parent_iev.events = EV_READ;
516 gotd_notify.parent_iev.handler_arg = NULL;
517 event_set(&gotd_notify.parent_iev.ev, gotd_notify.parent_iev.ibuf.fd,
518 EV_READ, notify_dispatch, &gotd_notify.parent_iev);
519 gotd_imsg_event_add(&gotd_notify.parent_iev);
521 event_dispatch();
523 if (err)
524 log_warnx("%s: %s", title, err->msg);
525 gotd_notify_shutdown();
528 void
529 gotd_notify_shutdown(void)
531 log_debug("%s: shutting down", gotd_notify.title);
532 exit(0);