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 "got_compat.h"
19 #include <sys/types.h>
20 #include <sys/queue.h>
21 #include <sys/socket.h>
22 #include <sys/wait.h>
24 #include <errno.h>
25 #include <event.h>
26 #include <limits.h>
27 #include <signal.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <imsg.h>
32 #include <unistd.h>
34 #include "got_error.h"
35 #include "got_path.h"
37 #include "gotd.h"
38 #include "log.h"
39 #include "notify.h"
41 #ifndef nitems
42 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
43 #endif
45 static struct gotd_notify {
46 pid_t pid;
47 const char *title;
48 struct gotd_imsgev parent_iev;
49 struct gotd_repolist *repos;
50 const char *default_sender;
51 } gotd_notify;
53 struct gotd_notify_session {
54 STAILQ_ENTRY(gotd_notify_session) entry;
55 uint32_t id;
56 struct gotd_imsgev iev;
57 };
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);
65 static uint64_t
66 session_hash(uint32_t session_id)
67 {
68 return SipHash24(&sessions_hash_key, &session_id, sizeof(session_id));
69 }
71 static void
72 add_session(struct gotd_notify_session *session)
73 {
74 uint64_t slot;
76 slot = session_hash(session->id) % nitems(gotd_notify_sessions);
77 STAILQ_INSERT_HEAD(&gotd_notify_sessions[slot], session, entry);
78 }
80 static struct gotd_notify_session *
81 find_session(uint32_t session_id)
82 {
83 uint64_t slot;
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)
89 return s;
90 }
92 return NULL;
93 }
95 static struct gotd_notify_session *
96 find_session_by_fd(int fd)
97 {
98 uint64_t slot;
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)
104 return s;
109 return NULL;
113 static void
114 remove_session(struct gotd_notify_session *session)
116 uint64_t slot;
118 slot = session_hash(session->id) % nitems(gotd_notify_sessions);
119 STAILQ_REMOVE(&gotd_notify_sessions[slot], session,
120 gotd_notify_session, entry);
121 free(session);
124 static uint32_t
125 get_session_id(void)
127 int duplicate = 0;
128 uint32_t id;
130 do {
131 id = arc4random();
132 duplicate = (find_session(id) != NULL);
133 } while (duplicate || id == 0);
135 return id;
138 static void
139 gotd_notify_sighdlr(int sig, short event, void *arg)
141 /*
142 * Normal signal handler rules don't apply because libevent
143 * decouples for us.
144 */
146 switch (sig) {
147 case SIGHUP:
148 log_info("%s: ignoring SIGHUP", __func__);
149 break;
150 case SIGUSR1:
151 log_info("%s: ignoring SIGUSR1", __func__);
152 break;
153 case SIGTERM:
154 case SIGINT:
155 gotd_notify_shutdown();
156 /* NOTREACHED */
157 break;
158 default:
159 fatalx("unexpected signal");
163 static void
164 run_notification_helper(const char *prog, const char **argv, int fd)
166 const struct got_error *err = NULL;
167 pid_t pid;
168 int child_status;
170 pid = fork();
171 if (pid == -1) {
172 err = got_error_from_errno("fork");
173 log_warn("%s", err->msg);
174 return;
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(),
182 strerror(errno));
183 _exit(1);
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));
191 _exit(1);
194 /* not reached */
197 if (waitpid(pid, &child_status, 0) == -1) {
198 err = got_error_from_errno("waitpid");
199 goto done;
202 if (!WIFEXITED(child_status)) {
203 err = got_error(GOT_ERR_PRIVSEP_DIED);
204 goto done;
207 if (WEXITSTATUS(child_status) != 0)
208 err = got_error(GOT_ERR_PRIVSEP_EXIT);
209 done:
210 if (err)
211 log_warnx("%s: child %s pid %d: %s", gotd_notify.title,
212 prog, pid, err->msg);
215 static void
216 notify_email(struct gotd_notification_target *target, const char *subject_line,
217 int fd)
219 const char *argv[13];
220 int i = 0;
222 argv[i++] = GOTD_PATH_PROG_NOTIFY_EMAIL;
224 argv[i++] = "-f";
225 if (target->conf.email.sender)
226 argv[i++] = target->conf.email.sender;
227 else
228 argv[i++] = gotd_notify.default_sender;
230 if (target->conf.email.responder) {
231 argv[i++] = "-r";
232 argv[i++] = target->conf.email.responder;
235 if (target->conf.email.hostname) {
236 argv[i++] = "-h";
237 argv[i++] = target->conf.email.hostname;
240 if (target->conf.email.port) {
241 argv[i++] = "-p";
242 argv[i++] = target->conf.email.port;
245 argv[i++] = "-s";
246 argv[i++] = subject_line;
248 argv[i++] = target->conf.email.recipient;
250 argv[i] = NULL;
252 run_notification_helper(GOTD_PATH_PROG_NOTIFY_EMAIL, argv, fd);
255 static void
256 notify_http(struct gotd_notification_target *target, const char *subject_line,
257 int fd)
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;
269 size_t datalen;
270 struct gotd_repo *repo;
271 struct gotd_notification_target *target;
272 int fd;
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);
281 if (repo == NULL)
282 return got_error(GOT_ERR_PRIVSEP_MSG);
284 fd = imsg_get_fd(imsg);
285 if (fd == -1)
286 return got_error(GOT_ERR_PRIVSEP_NO_FD);
288 if (lseek(fd, 0, SEEK_SET) == -1) {
289 err = got_error_from_errno("lseek");
290 goto done;
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);
297 break;
298 case GOTD_NOTIFICATION_VIA_HTTP:
299 notify_http(target, inotify.subject_line, fd);
300 break;
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");
307 goto done;
309 done:
310 close(fd);
311 return err;
314 static void
315 notify_dispatch_session(int fd, short event, void *arg)
317 struct gotd_imsgev *iev = arg;
318 struct imsgbuf *ibuf = &iev->ibuf;
319 ssize_t n;
320 int shut = 0;
321 struct imsg imsg;
323 if (event & EV_READ) {
324 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
325 fatal("imsg_read error");
326 if (n == 0) {
327 /* Connection closed. */
328 shut = 1;
329 goto done;
333 if (event & EV_WRITE) {
334 n = msgbuf_write(&ibuf->w);
335 if (n == -1 && errno != EAGAIN)
336 fatal("msgbuf_write");
337 if (n == 0) {
338 /* Connection closed. */
339 shut = 1;
340 goto done;
344 for (;;) {
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. */
350 break;
352 switch (imsg.hdr.type) {
353 case GOTD_IMSG_NOTIFY:
354 err = send_notification(&imsg, iev);
355 break;
356 default:
357 log_debug("unexpected imsg %d", imsg.hdr.type);
358 break;
360 imsg_free(&imsg);
362 if (err)
363 log_warnx("%s: %s", __func__, err->msg);
365 done:
366 if (!shut) {
367 gotd_imsg_event_add(iev);
368 } else {
369 struct gotd_notify_session *session;
371 /* This pipe is dead. Remove its event handler */
372 event_del(&iev->ev);
373 imsg_clear(&iev->ibuf);
375 session = find_session_by_fd(fd);
376 if (session)
377 remove_session(session);
381 static const struct got_error *
382 recv_session(struct imsg *imsg)
384 struct gotd_notify_session *session;
385 size_t datalen;
386 int fd;
388 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
389 if (datalen != 0)
390 return got_error(GOT_ERR_PRIVSEP_LEN);
392 fd = imsg_get_fd(imsg);
393 if (fd == -1)
394 return got_error(GOT_ERR_PRIVSEP_NO_FD);
396 session = calloc(1, sizeof(*session));
397 if (session == NULL)
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);
410 return NULL;
413 static void
414 notify_dispatch(int fd, short event, void *arg)
416 struct gotd_imsgev *iev = arg;
417 struct imsgbuf *ibuf = &iev->ibuf;
418 ssize_t n;
419 int shut = 0;
420 struct imsg imsg;
422 if (event & EV_READ) {
423 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
424 fatal("imsg_read error");
425 if (n == 0) {
426 /* Connection closed. */
427 shut = 1;
428 goto done;
432 if (event & EV_WRITE) {
433 n = msgbuf_write(&ibuf->w);
434 if (n == -1 && errno != EAGAIN)
435 fatal("msgbuf_write");
436 if (n == 0) {
437 /* Connection closed. */
438 shut = 1;
439 goto done;
443 for (;;) {
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. */
449 break;
451 switch (imsg.hdr.type) {
452 case GOTD_IMSG_CONNECT_SESSION:
453 err = recv_session(&imsg);
454 break;
455 default:
456 log_debug("unexpected imsg %d", imsg.hdr.type);
457 break;
459 imsg_free(&imsg);
461 if (err)
462 log_warnx("%s: %s", __func__, err->msg);
464 done:
465 if (!shut) {
466 gotd_imsg_event_add(iev);
467 } else {
468 /* This pipe is dead. Remove its event handler */
469 event_del(&iev->ev);
470 event_loopexit(NULL);
475 void
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);
508 event_dispatch();
510 if (err)
511 log_warnx("%s: %s", title, err->msg);
512 gotd_notify_shutdown();
515 void
516 gotd_notify_shutdown(void)
518 log_debug("shutting down");
519 exit(0);