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 *repo, int fd)
258 const char *argv[10];
261 argv[argc++] = GOTD_PATH_PROG_NOTIFY_HTTP;
262 if (target->conf.http.tls)
268 argv[argc++] = target->conf.http.hostname;
270 argv[argc++] = target->conf.http.port;
272 argv[argc++] = target->conf.http.path;
276 run_notification_helper(GOTD_PATH_PROG_NOTIFY_HTTP, argv, fd);
279 static const struct got_error *
280 send_notification(struct imsg *imsg, struct gotd_imsgev *iev)
282 const struct got_error *err = NULL;
283 struct gotd_imsg_notify inotify;
285 struct gotd_repo *repo;
286 struct gotd_notification_target *target;
289 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
290 if (datalen != sizeof(inotify))
291 return got_error(GOT_ERR_PRIVSEP_LEN);
293 memcpy(&inotify, imsg->data, datalen);
295 repo = gotd_find_repo_by_name(inotify.repo_name, gotd_notify.repos);
297 return got_error(GOT_ERR_PRIVSEP_MSG);
299 fd = imsg_get_fd(imsg);
301 return got_error(GOT_ERR_PRIVSEP_NO_FD);
303 if (lseek(fd, 0, SEEK_SET) == -1) {
304 err = got_error_from_errno("lseek");
308 STAILQ_FOREACH(target, &repo->notification_targets, entry) {
309 switch (target->type) {
310 case GOTD_NOTIFICATION_VIA_EMAIL:
311 notify_email(target, inotify.subject_line, fd);
313 case GOTD_NOTIFICATION_VIA_HTTP:
314 notify_http(target, repo->name, fd);
319 if (gotd_imsg_compose_event(iev, GOTD_IMSG_NOTIFICATION_SENT,
320 PROC_NOTIFY, -1, NULL, 0) == -1) {
321 err = got_error_from_errno("imsg compose NOTIFY");
330 notify_dispatch_session(int fd, short event, void *arg)
332 struct gotd_imsgev *iev = arg;
333 struct imsgbuf *ibuf = &iev->ibuf;
338 if (event & EV_READ) {
339 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
340 fatal("imsg_read error");
342 /* Connection closed. */
348 if (event & EV_WRITE) {
349 n = msgbuf_write(&ibuf->w);
350 if (n == -1 && errno != EAGAIN)
351 fatal("msgbuf_write");
353 /* Connection closed. */
360 const struct got_error *err = NULL;
362 if ((n = imsg_get(ibuf, &imsg)) == -1)
363 fatal("%s: imsg_get error", __func__);
364 if (n == 0) /* No more messages. */
367 switch (imsg.hdr.type) {
368 case GOTD_IMSG_NOTIFY:
369 err = send_notification(&imsg, iev);
372 log_debug("unexpected imsg %d", imsg.hdr.type);
378 log_warnx("%s: %s", __func__, err->msg);
382 gotd_imsg_event_add(iev);
384 struct gotd_notify_session *session;
386 /* This pipe is dead. Remove its event handler */
388 imsg_clear(&iev->ibuf);
390 session = find_session_by_fd(fd);
392 remove_session(session);
396 static const struct got_error *
397 recv_session(struct imsg *imsg)
399 struct gotd_notify_session *session;
403 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
405 return got_error(GOT_ERR_PRIVSEP_LEN);
407 fd = imsg_get_fd(imsg);
409 return got_error(GOT_ERR_PRIVSEP_NO_FD);
411 session = calloc(1, sizeof(*session));
413 return got_error_from_errno("calloc");
415 session->id = get_session_id();
416 imsg_init(&session->iev.ibuf, fd);
417 session->iev.handler = notify_dispatch_session;
418 session->iev.events = EV_READ;
419 session->iev.handler_arg = NULL;
420 event_set(&session->iev.ev, session->iev.ibuf.fd, EV_READ,
421 notify_dispatch_session, &session->iev);
422 gotd_imsg_event_add(&session->iev);
423 add_session(session);
429 notify_dispatch(int fd, short event, void *arg)
431 struct gotd_imsgev *iev = arg;
432 struct imsgbuf *ibuf = &iev->ibuf;
437 if (event & EV_READ) {
438 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
439 fatal("imsg_read error");
441 /* Connection closed. */
447 if (event & EV_WRITE) {
448 n = msgbuf_write(&ibuf->w);
449 if (n == -1 && errno != EAGAIN)
450 fatal("msgbuf_write");
452 /* Connection closed. */
459 const struct got_error *err = NULL;
461 if ((n = imsg_get(ibuf, &imsg)) == -1)
462 fatal("%s: imsg_get error", __func__);
463 if (n == 0) /* No more messages. */
466 switch (imsg.hdr.type) {
467 case GOTD_IMSG_CONNECT_SESSION:
468 err = recv_session(&imsg);
471 log_debug("unexpected imsg %d", imsg.hdr.type);
477 log_warnx("%s: %s", __func__, err->msg);
481 gotd_imsg_event_add(iev);
483 /* This pipe is dead. Remove its event handler */
485 event_loopexit(NULL);
491 notify_main(const char *title, struct gotd_repolist *repos,
492 const char *default_sender)
494 const struct got_error *err = NULL;
495 struct event evsigint, evsigterm, evsighup, evsigusr1;
497 arc4random_buf(&sessions_hash_key, sizeof(sessions_hash_key));
499 gotd_notify.title = title;
500 gotd_notify.repos = repos;
501 gotd_notify.default_sender = default_sender;
502 gotd_notify.pid = getpid();
504 signal_set(&evsigint, SIGINT, gotd_notify_sighdlr, NULL);
505 signal_set(&evsigterm, SIGTERM, gotd_notify_sighdlr, NULL);
506 signal_set(&evsighup, SIGHUP, gotd_notify_sighdlr, NULL);
507 signal_set(&evsigusr1, SIGUSR1, gotd_notify_sighdlr, NULL);
508 signal(SIGPIPE, SIG_IGN);
510 signal_add(&evsigint, NULL);
511 signal_add(&evsigterm, NULL);
512 signal_add(&evsighup, NULL);
513 signal_add(&evsigusr1, NULL);
515 imsg_init(&gotd_notify.parent_iev.ibuf, GOTD_FILENO_MSG_PIPE);
516 gotd_notify.parent_iev.handler = notify_dispatch;
517 gotd_notify.parent_iev.events = EV_READ;
518 gotd_notify.parent_iev.handler_arg = NULL;
519 event_set(&gotd_notify.parent_iev.ev, gotd_notify.parent_iev.ibuf.fd,
520 EV_READ, notify_dispatch, &gotd_notify.parent_iev);
521 gotd_imsg_event_add(&gotd_notify.parent_iev);
526 log_warnx("%s: %s", title, err->msg);
527 gotd_notify_shutdown();
531 gotd_notify_shutdown(void)
533 log_debug("%s: shutting down", gotd_notify.title);