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;
111 return NULL;
115 static void
116 remove_session(struct gotd_notify_session *session)
118 uint64_t slot;
120 slot = session_hash(session->id) % nitems(gotd_notify_sessions);
121 STAILQ_REMOVE(&gotd_notify_sessions[slot], session,
122 gotd_notify_session, entry);
123 free(session);
126 static uint32_t
127 get_session_id(void)
129 int duplicate = 0;
130 uint32_t id;
132 do {
133 id = arc4random();
134 duplicate = (find_session(id) != NULL);
135 } while (duplicate || id == 0);
137 return id;
140 static void
141 gotd_notify_sighdlr(int sig, short event, void *arg)
143 /*
144 * Normal signal handler rules don't apply because libevent
145 * decouples for us.
146 */
148 switch (sig) {
149 case SIGHUP:
150 log_info("%s: ignoring SIGHUP", __func__);
151 break;
152 case SIGUSR1:
153 log_info("%s: ignoring SIGUSR1", __func__);
154 break;
155 case SIGTERM:
156 case SIGINT:
157 gotd_notify_shutdown();
158 /* NOTREACHED */
159 break;
160 default:
161 fatalx("unexpected signal");
165 static void
166 run_notification_helper(const char *prog, const char **argv, int fd)
168 const struct got_error *err = NULL;
169 pid_t pid;
170 int child_status;
172 pid = fork();
173 if (pid == -1) {
174 err = got_error_from_errno("fork");
175 log_warn("%s", err->msg);
176 return;
177 } else if (pid == 0) {
178 signal(SIGQUIT, SIG_DFL);
179 signal(SIGINT, SIG_DFL);
180 signal(SIGCHLD, SIG_DFL);
182 if (dup2(fd, STDIN_FILENO) == -1) {
183 fprintf(stderr, "%s: dup2: %s\n", getprogname(),
184 strerror(errno));
185 _exit(1);
188 closefrom(STDERR_FILENO + 1);
190 if (execv(prog, (char *const *)argv) == -1) {
191 fprintf(stderr, "%s: exec %s: %s\n", getprogname(),
192 prog, strerror(errno));
193 _exit(1);
196 /* not reached */
199 if (waitpid(pid, &child_status, 0) == -1) {
200 err = got_error_from_errno("waitpid");
201 goto done;
204 if (!WIFEXITED(child_status)) {
205 err = got_error(GOT_ERR_PRIVSEP_DIED);
206 goto done;
209 if (WEXITSTATUS(child_status) != 0)
210 err = got_error(GOT_ERR_PRIVSEP_EXIT);
211 done:
212 if (err)
213 log_warnx("%s: child %s pid %d: %s", gotd_notify.title,
214 prog, pid, err->msg);
217 static void
218 notify_email(struct gotd_notification_target *target, const char *subject_line,
219 int fd)
221 const char *argv[13];
222 int i = 0;
224 argv[i++] = GOTD_PATH_PROG_NOTIFY_EMAIL;
226 argv[i++] = "-f";
227 if (target->conf.email.sender)
228 argv[i++] = target->conf.email.sender;
229 else
230 argv[i++] = gotd_notify.default_sender;
232 if (target->conf.email.responder) {
233 argv[i++] = "-r";
234 argv[i++] = target->conf.email.responder;
237 if (target->conf.email.hostname) {
238 argv[i++] = "-h";
239 argv[i++] = target->conf.email.hostname;
242 if (target->conf.email.port) {
243 argv[i++] = "-p";
244 argv[i++] = target->conf.email.port;
247 argv[i++] = "-s";
248 argv[i++] = subject_line;
250 argv[i++] = target->conf.email.recipient;
252 argv[i] = NULL;
254 run_notification_helper(GOTD_PATH_PROG_NOTIFY_EMAIL, argv, fd);
257 static void
258 notify_http(struct gotd_notification_target *target, const char *subject_line,
259 int fd)
261 const char *argv[10] = { 0 }; /* TODO */
263 run_notification_helper(GOTD_PATH_PROG_NOTIFY_HTTP, argv, fd);
266 static const struct got_error *
267 send_notification(struct imsg *imsg, struct gotd_imsgev *iev)
269 const struct got_error *err = NULL;
270 struct gotd_imsg_notify inotify;
271 size_t datalen;
272 struct gotd_repo *repo;
273 struct gotd_notification_target *target;
274 int fd;
276 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
277 if (datalen != sizeof(inotify))
278 return got_error(GOT_ERR_PRIVSEP_LEN);
280 memcpy(&inotify, imsg->data, datalen);
282 repo = gotd_find_repo_by_name(inotify.repo_name, gotd_notify.repos);
283 if (repo == NULL)
284 return got_error(GOT_ERR_PRIVSEP_MSG);
286 fd = imsg_get_fd(imsg);
287 if (fd == -1)
288 return got_error(GOT_ERR_PRIVSEP_NO_FD);
290 if (lseek(fd, 0, SEEK_SET) == -1) {
291 err = got_error_from_errno("lseek");
292 goto done;
295 STAILQ_FOREACH(target, &repo->notification_targets, entry) {
296 switch (target->type) {
297 case GOTD_NOTIFICATION_VIA_EMAIL:
298 notify_email(target, inotify.subject_line, fd);
299 break;
300 case GOTD_NOTIFICATION_VIA_HTTP:
301 notify_http(target, inotify.subject_line, fd);
302 break;
306 if (gotd_imsg_compose_event(iev, GOTD_IMSG_NOTIFICATION_SENT,
307 PROC_NOTIFY, -1, NULL, 0) == -1) {
308 err = got_error_from_errno("imsg compose NOTIFY");
309 goto done;
311 done:
312 close(fd);
313 return err;
316 static void
317 notify_dispatch_session(int fd, short event, void *arg)
319 struct gotd_imsgev *iev = arg;
320 struct imsgbuf *ibuf = &iev->ibuf;
321 ssize_t n;
322 int shut = 0;
323 struct imsg imsg;
325 if (event & EV_READ) {
326 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
327 fatal("imsg_read error");
328 if (n == 0) {
329 /* Connection closed. */
330 shut = 1;
331 goto done;
335 if (event & EV_WRITE) {
336 n = msgbuf_write(&ibuf->w);
337 if (n == -1 && errno != EAGAIN)
338 fatal("msgbuf_write");
339 if (n == 0) {
340 /* Connection closed. */
341 shut = 1;
342 goto done;
346 for (;;) {
347 const struct got_error *err = NULL;
349 if ((n = imsg_get(ibuf, &imsg)) == -1)
350 fatal("%s: imsg_get error", __func__);
351 if (n == 0) /* No more messages. */
352 break;
354 switch (imsg.hdr.type) {
355 case GOTD_IMSG_NOTIFY:
356 err = send_notification(&imsg, iev);
357 break;
358 default:
359 log_debug("unexpected imsg %d", imsg.hdr.type);
360 break;
362 imsg_free(&imsg);
364 if (err)
365 log_warnx("%s: %s", __func__, err->msg);
367 done:
368 if (!shut) {
369 gotd_imsg_event_add(iev);
370 } else {
371 struct gotd_notify_session *session;
373 /* This pipe is dead. Remove its event handler */
374 event_del(&iev->ev);
375 imsg_clear(&iev->ibuf);
377 session = find_session_by_fd(fd);
378 if (session)
379 remove_session(session);
383 static const struct got_error *
384 recv_session(struct imsg *imsg)
386 struct gotd_notify_session *session;
387 size_t datalen;
388 int fd;
390 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
391 if (datalen != 0)
392 return got_error(GOT_ERR_PRIVSEP_LEN);
394 fd = imsg_get_fd(imsg);
395 if (fd == -1)
396 return got_error(GOT_ERR_PRIVSEP_NO_FD);
398 session = calloc(1, sizeof(*session));
399 if (session == NULL)
400 return got_error_from_errno("calloc");
402 session->id = get_session_id();
403 imsg_init(&session->iev.ibuf, fd);
404 session->iev.handler = notify_dispatch_session;
405 session->iev.events = EV_READ;
406 session->iev.handler_arg = NULL;
407 event_set(&session->iev.ev, session->iev.ibuf.fd, EV_READ,
408 notify_dispatch_session, &session->iev);
409 gotd_imsg_event_add(&session->iev);
410 add_session(session);
412 return NULL;
415 static void
416 notify_dispatch(int fd, short event, void *arg)
418 struct gotd_imsgev *iev = arg;
419 struct imsgbuf *ibuf = &iev->ibuf;
420 ssize_t n;
421 int shut = 0;
422 struct imsg imsg;
424 if (event & EV_READ) {
425 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
426 fatal("imsg_read error");
427 if (n == 0) {
428 /* Connection closed. */
429 shut = 1;
430 goto done;
434 if (event & EV_WRITE) {
435 n = msgbuf_write(&ibuf->w);
436 if (n == -1 && errno != EAGAIN)
437 fatal("msgbuf_write");
438 if (n == 0) {
439 /* Connection closed. */
440 shut = 1;
441 goto done;
445 for (;;) {
446 const struct got_error *err = NULL;
448 if ((n = imsg_get(ibuf, &imsg)) == -1)
449 fatal("%s: imsg_get error", __func__);
450 if (n == 0) /* No more messages. */
451 break;
453 switch (imsg.hdr.type) {
454 case GOTD_IMSG_CONNECT_SESSION:
455 err = recv_session(&imsg);
456 break;
457 default:
458 log_debug("unexpected imsg %d", imsg.hdr.type);
459 break;
461 imsg_free(&imsg);
463 if (err)
464 log_warnx("%s: %s", __func__, err->msg);
466 done:
467 if (!shut) {
468 gotd_imsg_event_add(iev);
469 } else {
470 /* This pipe is dead. Remove its event handler */
471 event_del(&iev->ev);
472 event_loopexit(NULL);
477 void
478 notify_main(const char *title, struct gotd_repolist *repos,
479 const char *default_sender)
481 const struct got_error *err = NULL;
482 struct event evsigint, evsigterm, evsighup, evsigusr1;
484 arc4random_buf(&sessions_hash_key, sizeof(sessions_hash_key));
486 gotd_notify.title = title;
487 gotd_notify.repos = repos;
488 gotd_notify.default_sender = default_sender;
489 gotd_notify.pid = getpid();
491 signal_set(&evsigint, SIGINT, gotd_notify_sighdlr, NULL);
492 signal_set(&evsigterm, SIGTERM, gotd_notify_sighdlr, NULL);
493 signal_set(&evsighup, SIGHUP, gotd_notify_sighdlr, NULL);
494 signal_set(&evsigusr1, SIGUSR1, gotd_notify_sighdlr, NULL);
495 signal(SIGPIPE, SIG_IGN);
497 signal_add(&evsigint, NULL);
498 signal_add(&evsigterm, NULL);
499 signal_add(&evsighup, NULL);
500 signal_add(&evsigusr1, NULL);
502 imsg_init(&gotd_notify.parent_iev.ibuf, GOTD_FILENO_MSG_PIPE);
503 gotd_notify.parent_iev.handler = notify_dispatch;
504 gotd_notify.parent_iev.events = EV_READ;
505 gotd_notify.parent_iev.handler_arg = NULL;
506 event_set(&gotd_notify.parent_iev.ev, gotd_notify.parent_iev.ibuf.fd,
507 EV_READ, notify_dispatch, &gotd_notify.parent_iev);
508 gotd_imsg_event_add(&gotd_notify.parent_iev);
510 event_dispatch();
512 if (err)
513 log_warnx("%s: %s", title, err->msg);
514 gotd_notify_shutdown();
517 void
518 gotd_notify_shutdown(void)
520 log_debug("%s: shutting down", gotd_notify.title);
521 exit(0);