2 * Copyright (c) 2024 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include <sys/types.h>
18 #include <sys/queue.h>
20 #include <sys/socket.h>
36 #include "got_error.h"
44 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
47 static struct gotd_notify {
50 struct gotd_imsgev parent_iev;
51 struct gotd_repolist *repos;
52 const char *default_sender;
55 struct gotd_notify_session {
56 STAILQ_ENTRY(gotd_notify_session) entry;
58 struct gotd_imsgev iev;
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);
68 session_hash(uint32_t session_id)
70 return SipHash24(&sessions_hash_key, &session_id, sizeof(session_id));
74 add_session(struct gotd_notify_session *session)
78 slot = session_hash(session->id) % nitems(gotd_notify_sessions);
79 STAILQ_INSERT_HEAD(&gotd_notify_sessions[slot], session, entry);
82 static struct gotd_notify_session *
83 find_session(uint32_t session_id)
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)
97 static struct gotd_notify_session *
98 find_session_by_fd(int fd)
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)
114 remove_session(struct gotd_notify_session *session)
118 slot = session_hash(session->id) % nitems(gotd_notify_sessions);
119 STAILQ_REMOVE(&gotd_notify_sessions[slot], session,
120 gotd_notify_session, entry);
132 duplicate = (find_session(id) != NULL);
133 } while (duplicate || id == 0);
139 gotd_notify_sighdlr(int sig, short event, void *arg)
142 * Normal signal handler rules don't apply because libevent
148 log_info("%s: ignoring SIGHUP", __func__);
151 log_info("%s: ignoring SIGUSR1", __func__);
155 gotd_notify_shutdown();
159 fatalx("unexpected signal");
164 run_notification_helper(const char *prog, const char **argv, int fd)
166 const struct got_error *err = NULL;
172 err = got_error_from_errno("fork");
173 log_warn("%s", err->msg);
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(),
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));
197 if (waitpid(pid, &child_status, 0) == -1) {
198 err = got_error_from_errno("waitpid");
202 if (!WIFEXITED(child_status)) {
203 err = got_error(GOT_ERR_PRIVSEP_DIED);
207 if (WEXITSTATUS(child_status) != 0)
208 err = got_error(GOT_ERR_PRIVSEP_EXIT);
211 log_warnx("%s: child %s pid %d: %s", gotd_notify.title,
212 prog, pid, err->msg);
216 notify_email(struct gotd_notification_target *target, const char *subject_line,
219 const char *argv[13];
222 argv[i++] = GOTD_PATH_PROG_NOTIFY_EMAIL;
225 if (target->conf.email.sender)
226 argv[i++] = target->conf.email.sender;
228 argv[i++] = gotd_notify.default_sender;
230 if (target->conf.email.responder) {
232 argv[i++] = target->conf.email.responder;
235 if (target->conf.email.hostname) {
237 argv[i++] = target->conf.email.hostname;
240 if (target->conf.email.port) {
242 argv[i++] = target->conf.email.port;
246 argv[i++] = subject_line;
248 argv[i++] = target->conf.email.recipient;
252 run_notification_helper(GOTD_PATH_PROG_NOTIFY_EMAIL, argv, fd);
256 notify_http(struct gotd_notification_target *target, const char *subject_line,
259 const char *argv[10] = { 0 }; /* TODO */
261 run_notification_helper(GOTD_PATH_PROG_NOTIFY_HTTP, argv, fd);
264 static const struct got_error *
265 send_notification(struct imsg *imsg, struct gotd_imsgev *iev)
267 const struct got_error *err = NULL;
268 struct gotd_imsg_notify inotify;
270 struct gotd_repo *repo;
271 struct gotd_notification_target *target;
274 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
275 if (datalen != sizeof(inotify))
276 return got_error(GOT_ERR_PRIVSEP_LEN);
278 memcpy(&inotify, imsg->data, datalen);
280 repo = gotd_find_repo_by_name(inotify.repo_name, gotd_notify.repos);
282 return got_error(GOT_ERR_PRIVSEP_MSG);
284 fd = imsg_get_fd(imsg);
286 return got_error(GOT_ERR_PRIVSEP_NO_FD);
288 if (lseek(fd, 0, SEEK_SET) == -1) {
289 err = got_error_from_errno("lseek");
293 STAILQ_FOREACH(target, &repo->notification_targets, entry) {
294 switch (target->type) {
295 case GOTD_NOTIFICATION_VIA_EMAIL:
296 notify_email(target, inotify.subject_line, fd);
298 case GOTD_NOTIFICATION_VIA_HTTP:
299 notify_http(target, inotify.subject_line, fd);
304 if (gotd_imsg_compose_event(iev, GOTD_IMSG_NOTIFICATION_SENT,
305 PROC_NOTIFY, -1, NULL, 0) == -1) {
306 err = got_error_from_errno("imsg compose NOTIFY");
315 notify_dispatch_session(int fd, short event, void *arg)
317 struct gotd_imsgev *iev = arg;
318 struct imsgbuf *ibuf = &iev->ibuf;
323 if (event & EV_READ) {
324 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
325 fatal("imsg_read error");
327 /* Connection closed. */
333 if (event & EV_WRITE) {
334 n = msgbuf_write(&ibuf->w);
335 if (n == -1 && errno != EAGAIN)
336 fatal("msgbuf_write");
338 /* Connection closed. */
345 const struct got_error *err = NULL;
347 if ((n = imsg_get(ibuf, &imsg)) == -1)
348 fatal("%s: imsg_get error", __func__);
349 if (n == 0) /* No more messages. */
352 switch (imsg.hdr.type) {
353 case GOTD_IMSG_NOTIFY:
354 err = send_notification(&imsg, iev);
357 log_debug("unexpected imsg %d", imsg.hdr.type);
363 log_warnx("%s: %s", __func__, err->msg);
367 gotd_imsg_event_add(iev);
369 struct gotd_notify_session *session;
371 /* This pipe is dead. Remove its event handler */
373 imsg_clear(&iev->ibuf);
375 session = find_session_by_fd(fd);
377 remove_session(session);
381 static const struct got_error *
382 recv_session(struct imsg *imsg)
384 struct gotd_notify_session *session;
388 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
390 return got_error(GOT_ERR_PRIVSEP_LEN);
392 fd = imsg_get_fd(imsg);
394 return got_error(GOT_ERR_PRIVSEP_NO_FD);
396 session = calloc(1, sizeof(*session));
398 return got_error_from_errno("calloc");
400 session->id = get_session_id();
401 imsg_init(&session->iev.ibuf, fd);
402 session->iev.handler = notify_dispatch_session;
403 session->iev.events = EV_READ;
404 session->iev.handler_arg = NULL;
405 event_set(&session->iev.ev, session->iev.ibuf.fd, EV_READ,
406 notify_dispatch_session, &session->iev);
407 gotd_imsg_event_add(&session->iev);
408 add_session(session);
414 notify_dispatch(int fd, short event, void *arg)
416 struct gotd_imsgev *iev = arg;
417 struct imsgbuf *ibuf = &iev->ibuf;
422 if (event & EV_READ) {
423 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
424 fatal("imsg_read error");
426 /* Connection closed. */
432 if (event & EV_WRITE) {
433 n = msgbuf_write(&ibuf->w);
434 if (n == -1 && errno != EAGAIN)
435 fatal("msgbuf_write");
437 /* Connection closed. */
444 const struct got_error *err = NULL;
446 if ((n = imsg_get(ibuf, &imsg)) == -1)
447 fatal("%s: imsg_get error", __func__);
448 if (n == 0) /* No more messages. */
451 switch (imsg.hdr.type) {
452 case GOTD_IMSG_CONNECT_SESSION:
453 err = recv_session(&imsg);
456 log_debug("unexpected imsg %d", imsg.hdr.type);
462 log_warnx("%s: %s", __func__, err->msg);
466 gotd_imsg_event_add(iev);
468 /* This pipe is dead. Remove its event handler */
470 event_loopexit(NULL);
476 notify_main(const char *title, struct gotd_repolist *repos,
477 const char *default_sender)
479 const struct got_error *err = NULL;
480 struct event evsigint, evsigterm, evsighup, evsigusr1;
482 arc4random_buf(&sessions_hash_key, sizeof(sessions_hash_key));
484 gotd_notify.title = title;
485 gotd_notify.repos = repos;
486 gotd_notify.default_sender = default_sender;
487 gotd_notify.pid = getpid();
489 signal_set(&evsigint, SIGINT, gotd_notify_sighdlr, NULL);
490 signal_set(&evsigterm, SIGTERM, gotd_notify_sighdlr, NULL);
491 signal_set(&evsighup, SIGHUP, gotd_notify_sighdlr, NULL);
492 signal_set(&evsigusr1, SIGUSR1, gotd_notify_sighdlr, NULL);
493 signal(SIGPIPE, SIG_IGN);
495 signal_add(&evsigint, NULL);
496 signal_add(&evsigterm, NULL);
497 signal_add(&evsighup, NULL);
498 signal_add(&evsigusr1, NULL);
500 imsg_init(&gotd_notify.parent_iev.ibuf, GOTD_FILENO_MSG_PIPE);
501 gotd_notify.parent_iev.handler = notify_dispatch;
502 gotd_notify.parent_iev.events = EV_READ;
503 gotd_notify.parent_iev.handler_arg = NULL;
504 event_set(&gotd_notify.parent_iev.ev, gotd_notify.parent_iev.ibuf.fd,
505 EV_READ, notify_dispatch, &gotd_notify.parent_iev);
506 gotd_imsg_event_add(&gotd_notify.parent_iev);
511 log_warnx("%s: %s", title, err->msg);
512 gotd_notify_shutdown();
516 gotd_notify_shutdown(void)
518 log_debug("%s: shutting down", gotd_notify.title);