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, int fd)
261 argv[argc++] = GOTD_PATH_PROG_NOTIFY_HTTP;
262 if (target->conf.http.tls)
266 argv[argc++] = target->conf.http.hostname;
268 argv[argc++] = target->conf.http.port;
270 argv[argc++] = target->conf.http.path;
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;
283 struct gotd_repo *repo;
284 struct gotd_notification_target *target;
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);
295 return got_error(GOT_ERR_PRIVSEP_MSG);
297 fd = imsg_get_fd(imsg);
299 return got_error(GOT_ERR_PRIVSEP_NO_FD);
301 if (lseek(fd, 0, SEEK_SET) == -1) {
302 err = got_error_from_errno("lseek");
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);
311 case GOTD_NOTIFICATION_VIA_HTTP:
312 notify_http(target, fd);
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");
328 notify_dispatch_session(int fd, short event, void *arg)
330 struct gotd_imsgev *iev = arg;
331 struct imsgbuf *ibuf = &iev->ibuf;
336 if (event & EV_READ) {
337 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
338 fatal("imsg_read error");
340 /* Connection closed. */
346 if (event & EV_WRITE) {
347 n = msgbuf_write(&ibuf->w);
348 if (n == -1 && errno != EAGAIN)
349 fatal("msgbuf_write");
351 /* Connection closed. */
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. */
365 switch (imsg.hdr.type) {
366 case GOTD_IMSG_NOTIFY:
367 err = send_notification(&imsg, iev);
370 log_debug("unexpected imsg %d", imsg.hdr.type);
376 log_warnx("%s: %s", __func__, err->msg);
380 gotd_imsg_event_add(iev);
382 struct gotd_notify_session *session;
384 /* This pipe is dead. Remove its event handler */
386 imsg_clear(&iev->ibuf);
388 session = find_session_by_fd(fd);
390 remove_session(session);
394 static const struct got_error *
395 recv_session(struct imsg *imsg)
397 struct gotd_notify_session *session;
401 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
403 return got_error(GOT_ERR_PRIVSEP_LEN);
405 fd = imsg_get_fd(imsg);
407 return got_error(GOT_ERR_PRIVSEP_NO_FD);
409 session = calloc(1, sizeof(*session));
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);
427 notify_dispatch(int fd, short event, void *arg)
429 struct gotd_imsgev *iev = arg;
430 struct imsgbuf *ibuf = &iev->ibuf;
435 if (event & EV_READ) {
436 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
437 fatal("imsg_read error");
439 /* Connection closed. */
445 if (event & EV_WRITE) {
446 n = msgbuf_write(&ibuf->w);
447 if (n == -1 && errno != EAGAIN)
448 fatal("msgbuf_write");
450 /* Connection closed. */
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. */
464 switch (imsg.hdr.type) {
465 case GOTD_IMSG_CONNECT_SESSION:
466 err = recv_session(&imsg);
469 log_debug("unexpected imsg %d", imsg.hdr.type);
475 log_warnx("%s: %s", __func__, err->msg);
479 gotd_imsg_event_add(iev);
481 /* This pipe is dead. Remove its event handler */
483 event_loopexit(NULL);
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);
524 log_warnx("%s: %s", title, err->msg);
525 gotd_notify_shutdown();
529 gotd_notify_shutdown(void)
531 log_debug("%s: shutting down", gotd_notify.title);