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 "got_compat.h"
19 #include <sys/types.h>
20 #include <sys/queue.h>
21 #include <sys/socket.h>
34 #include "got_error.h"
42 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
45 static struct gotd_notify {
48 struct gotd_imsgev parent_iev;
49 struct gotd_repolist *repos;
50 const char *default_sender;
53 struct gotd_notify_session {
54 STAILQ_ENTRY(gotd_notify_session) entry;
56 struct gotd_imsgev iev;
58 STAILQ_HEAD(gotd_notify_sessions, gotd_notify_session);
60 static struct gotd_notify_sessions gotd_notify_sessions[GOTD_CLIENT_TABLE_SIZE];
61 static SIPHASH_KEY sessions_hash_key;
63 static void gotd_notify_shutdown(void);
66 session_hash(uint32_t session_id)
68 return SipHash24(&sessions_hash_key, &session_id, sizeof(session_id));
72 add_session(struct gotd_notify_session *session)
76 slot = session_hash(session->id) % nitems(gotd_notify_sessions);
77 STAILQ_INSERT_HEAD(&gotd_notify_sessions[slot], session, entry);
80 static struct gotd_notify_session *
81 find_session(uint32_t session_id)
84 struct gotd_notify_session *s;
86 slot = session_hash(session_id) % nitems(gotd_notify_sessions);
87 STAILQ_FOREACH(s, &gotd_notify_sessions[slot], entry) {
88 if (s->id == session_id)
95 static struct gotd_notify_session *
96 find_session_by_fd(int fd)
99 struct gotd_notify_session *s;
101 for (slot = 0; slot < nitems(gotd_notify_sessions); slot++) {
102 STAILQ_FOREACH(s, &gotd_notify_sessions[slot], entry) {
103 if (s->iev.ibuf.fd == fd)
112 remove_session(struct gotd_notify_session *session)
116 slot = session_hash(session->id) % nitems(gotd_notify_sessions);
117 STAILQ_REMOVE(&gotd_notify_sessions[slot], session,
118 gotd_notify_session, entry);
130 duplicate = (find_session(id) != NULL);
131 } while (duplicate || id == 0);
137 gotd_notify_sighdlr(int sig, short event, void *arg)
140 * Normal signal handler rules don't apply because libevent
146 log_info("%s: ignoring SIGHUP", __func__);
149 log_info("%s: ignoring SIGUSR1", __func__);
153 gotd_notify_shutdown();
157 fatalx("unexpected signal");
162 run_notification_helper(const char *prog, const char **argv, int fd,
163 const char *user, const char *pass)
165 const struct got_error *err = NULL;
171 err = got_error_from_errno("fork");
172 log_warn("%s", err->msg);
174 } else if (pid == 0) {
175 signal(SIGQUIT, SIG_DFL);
176 signal(SIGINT, SIG_DFL);
177 signal(SIGCHLD, SIG_DFL);
179 if (dup2(fd, STDIN_FILENO) == -1) {
180 fprintf(stderr, "%s: dup2: %s\n", getprogname(),
185 closefrom(STDERR_FILENO + 1);
187 if (user != NULL && pass != NULL) {
188 setenv("GOT_NOTIFY_HTTP_USER", user, 1);
189 setenv("GOT_NOTIFY_HTTP_PASS", pass, 1);
192 if (execv(prog, (char *const *)argv) == -1) {
193 fprintf(stderr, "%s: exec %s: %s\n", getprogname(),
194 prog, strerror(errno));
201 if (waitpid(pid, &child_status, 0) == -1) {
202 err = got_error_from_errno("waitpid");
206 if (!WIFEXITED(child_status)) {
207 err = got_error(GOT_ERR_PRIVSEP_DIED);
211 if (WEXITSTATUS(child_status) != 0)
212 err = got_error(GOT_ERR_PRIVSEP_EXIT);
215 log_warnx("%s: child %s pid %d: %s", gotd_notify.title,
216 prog, pid, err->msg);
220 notify_email(struct gotd_notification_target *target, const char *subject_line,
223 const char *argv[13];
226 argv[i++] = GOTD_PATH_PROG_NOTIFY_EMAIL;
229 if (target->conf.email.sender)
230 argv[i++] = target->conf.email.sender;
232 argv[i++] = gotd_notify.default_sender;
234 if (target->conf.email.responder) {
236 argv[i++] = target->conf.email.responder;
239 if (target->conf.email.hostname) {
241 argv[i++] = target->conf.email.hostname;
244 if (target->conf.email.port) {
246 argv[i++] = target->conf.email.port;
250 argv[i++] = subject_line;
252 argv[i++] = target->conf.email.recipient;
256 run_notification_helper(GOTD_PATH_PROG_NOTIFY_EMAIL, argv, fd,
261 notify_http(struct gotd_notification_target *target, const char *repo, int fd)
263 const char *argv[10];
266 argv[argc++] = GOTD_PATH_PROG_NOTIFY_HTTP;
267 if (target->conf.http.tls)
273 argv[argc++] = target->conf.http.hostname;
275 argv[argc++] = target->conf.http.port;
277 argv[argc++] = target->conf.http.path;
281 run_notification_helper(GOTD_PATH_PROG_NOTIFY_HTTP, argv, fd,
282 target->conf.http.user, target->conf.http.password);
285 static const struct got_error *
286 send_notification(struct imsg *imsg, struct gotd_imsgev *iev)
288 const struct got_error *err = NULL;
289 struct gotd_imsg_notify inotify;
291 struct gotd_repo *repo;
292 struct gotd_notification_target *target;
295 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
296 if (datalen != sizeof(inotify))
297 return got_error(GOT_ERR_PRIVSEP_LEN);
299 memcpy(&inotify, imsg->data, datalen);
301 repo = gotd_find_repo_by_name(inotify.repo_name, gotd_notify.repos);
303 return got_error(GOT_ERR_PRIVSEP_MSG);
305 fd = imsg_get_fd(imsg);
307 return got_error(GOT_ERR_PRIVSEP_NO_FD);
309 if (lseek(fd, 0, SEEK_SET) == -1) {
310 err = got_error_from_errno("lseek");
314 STAILQ_FOREACH(target, &repo->notification_targets, entry) {
315 switch (target->type) {
316 case GOTD_NOTIFICATION_VIA_EMAIL:
317 notify_email(target, inotify.subject_line, fd);
319 case GOTD_NOTIFICATION_VIA_HTTP:
320 notify_http(target, repo->name, fd);
325 if (gotd_imsg_compose_event(iev, GOTD_IMSG_NOTIFICATION_SENT,
326 PROC_NOTIFY, -1, NULL, 0) == -1) {
327 err = got_error_from_errno("imsg compose NOTIFY");
336 notify_dispatch_session(int fd, short event, void *arg)
338 struct gotd_imsgev *iev = arg;
339 struct imsgbuf *ibuf = &iev->ibuf;
344 if (event & EV_READ) {
345 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
346 fatal("imsg_read error");
348 /* Connection closed. */
354 if (event & EV_WRITE) {
355 n = msgbuf_write(&ibuf->w);
356 if (n == -1 && errno != EAGAIN)
357 fatal("msgbuf_write");
359 /* Connection closed. */
366 const struct got_error *err = NULL;
368 if ((n = imsg_get(ibuf, &imsg)) == -1)
369 fatal("%s: imsg_get error", __func__);
370 if (n == 0) /* No more messages. */
373 switch (imsg.hdr.type) {
374 case GOTD_IMSG_NOTIFY:
375 err = send_notification(&imsg, iev);
378 log_debug("unexpected imsg %d", imsg.hdr.type);
384 log_warnx("%s: %s", __func__, err->msg);
388 gotd_imsg_event_add(iev);
390 struct gotd_notify_session *session;
392 /* This pipe is dead. Remove its event handler */
394 imsg_clear(&iev->ibuf);
396 session = find_session_by_fd(fd);
398 remove_session(session);
402 static const struct got_error *
403 recv_session(struct imsg *imsg)
405 struct gotd_notify_session *session;
409 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
411 return got_error(GOT_ERR_PRIVSEP_LEN);
413 fd = imsg_get_fd(imsg);
415 return got_error(GOT_ERR_PRIVSEP_NO_FD);
417 session = calloc(1, sizeof(*session));
419 return got_error_from_errno("calloc");
421 session->id = get_session_id();
422 imsg_init(&session->iev.ibuf, fd);
423 session->iev.handler = notify_dispatch_session;
424 session->iev.events = EV_READ;
425 session->iev.handler_arg = NULL;
426 event_set(&session->iev.ev, session->iev.ibuf.fd, EV_READ,
427 notify_dispatch_session, &session->iev);
428 gotd_imsg_event_add(&session->iev);
429 add_session(session);
435 notify_dispatch(int fd, short event, void *arg)
437 struct gotd_imsgev *iev = arg;
438 struct imsgbuf *ibuf = &iev->ibuf;
443 if (event & EV_READ) {
444 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
445 fatal("imsg_read error");
447 /* Connection closed. */
453 if (event & EV_WRITE) {
454 n = msgbuf_write(&ibuf->w);
455 if (n == -1 && errno != EAGAIN)
456 fatal("msgbuf_write");
458 /* Connection closed. */
465 const struct got_error *err = NULL;
467 if ((n = imsg_get(ibuf, &imsg)) == -1)
468 fatal("%s: imsg_get error", __func__);
469 if (n == 0) /* No more messages. */
472 switch (imsg.hdr.type) {
473 case GOTD_IMSG_CONNECT_SESSION:
474 err = recv_session(&imsg);
477 log_debug("unexpected imsg %d", imsg.hdr.type);
483 log_warnx("%s: %s", __func__, err->msg);
487 gotd_imsg_event_add(iev);
489 /* This pipe is dead. Remove its event handler */
491 event_loopexit(NULL);
497 notify_main(const char *title, struct gotd_repolist *repos,
498 const char *default_sender)
500 const struct got_error *err = NULL;
501 struct event evsigint, evsigterm, evsighup, evsigusr1;
503 arc4random_buf(&sessions_hash_key, sizeof(sessions_hash_key));
505 gotd_notify.title = title;
506 gotd_notify.repos = repos;
507 gotd_notify.default_sender = default_sender;
508 gotd_notify.pid = getpid();
510 signal_set(&evsigint, SIGINT, gotd_notify_sighdlr, NULL);
511 signal_set(&evsigterm, SIGTERM, gotd_notify_sighdlr, NULL);
512 signal_set(&evsighup, SIGHUP, gotd_notify_sighdlr, NULL);
513 signal_set(&evsigusr1, SIGUSR1, gotd_notify_sighdlr, NULL);
514 signal(SIGPIPE, SIG_IGN);
516 signal_add(&evsigint, NULL);
517 signal_add(&evsigterm, NULL);
518 signal_add(&evsighup, NULL);
519 signal_add(&evsigusr1, NULL);
521 imsg_init(&gotd_notify.parent_iev.ibuf, GOTD_FILENO_MSG_PIPE);
522 gotd_notify.parent_iev.handler = notify_dispatch;
523 gotd_notify.parent_iev.events = EV_READ;
524 gotd_notify.parent_iev.handler_arg = NULL;
525 event_set(&gotd_notify.parent_iev.ev, gotd_notify.parent_iev.ibuf.fd,
526 EV_READ, notify_dispatch, &gotd_notify.parent_iev);
527 gotd_imsg_event_add(&gotd_notify.parent_iev);
532 log_warnx("%s: %s", title, err->msg);
533 gotd_notify_shutdown();
537 gotd_notify_shutdown(void)
539 log_debug("%s: shutting down", gotd_notify.title);