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 <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/tree.h>
20 #include <sys/socket.h>
21 #include <sys/wait.h>
23 #include <errno.h>
24 #include <event.h>
25 #include <siphash.h>
26 #include <limits.h>
27 #include <sha1.h>
28 #include <sha2.h>
29 #include <signal.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <imsg.h>
34 #include <unistd.h>
36 #include "got_error.h"
37 #include "got_path.h"
39 #include "gotd.h"
40 #include "log.h"
41 #include "notify.h"
43 #ifndef nitems
44 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
45 #endif
47 static struct gotd_notify {
48 pid_t pid;
49 const char *title;
50 struct gotd_imsgev parent_iev;
51 struct gotd_repolist *repos;
52 const char *default_sender;
53 } gotd_notify;
55 struct gotd_notify_session {
56 STAILQ_ENTRY(gotd_notify_session) entry;
57 uint32_t id;
58 struct gotd_imsgev iev;
59 };
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);
67 static uint64_t
68 session_hash(uint32_t session_id)
69 {
70 return SipHash24(&sessions_hash_key, &session_id, sizeof(session_id));
71 }
73 static void
74 add_session(struct gotd_notify_session *session)
75 {
76 uint64_t slot;
78 slot = session_hash(session->id) % nitems(gotd_notify_sessions);
79 STAILQ_INSERT_HEAD(&gotd_notify_sessions[slot], session, entry);
80 }
82 static struct gotd_notify_session *
83 find_session(uint32_t session_id)
84 {
85 uint64_t slot;
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)
91 return s;
92 }
94 return NULL;
95 }
97 static struct gotd_notify_session *
98 find_session_by_fd(int fd)
99 {
100 uint64_t slot;
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)
106 return s;
110 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 *repo, int fd)
258 const char *argv[10];
259 int argc = 0;
261 argv[argc++] = GOTD_PATH_PROG_NOTIFY_HTTP;
262 if (target->conf.http.tls)
263 argv[argc++] = "-c";
265 argv[argc++] = "-r";
266 argv[argc++] = repo;
267 argv[argc++] = "-h";
268 argv[argc++] = target->conf.http.hostname;
269 argv[argc++] = "-p";
270 argv[argc++] = target->conf.http.port;
272 argv[argc++] = target->conf.http.path;
274 argv[argc] = NULL;
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;
284 size_t datalen;
285 struct gotd_repo *repo;
286 struct gotd_notification_target *target;
287 int fd;
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);
296 if (repo == NULL)
297 return got_error(GOT_ERR_PRIVSEP_MSG);
299 fd = imsg_get_fd(imsg);
300 if (fd == -1)
301 return got_error(GOT_ERR_PRIVSEP_NO_FD);
303 if (lseek(fd, 0, SEEK_SET) == -1) {
304 err = got_error_from_errno("lseek");
305 goto done;
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);
312 break;
313 case GOTD_NOTIFICATION_VIA_HTTP:
314 notify_http(target, repo->name, fd);
315 break;
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");
322 goto done;
324 done:
325 close(fd);
326 return err;
329 static void
330 notify_dispatch_session(int fd, short event, void *arg)
332 struct gotd_imsgev *iev = arg;
333 struct imsgbuf *ibuf = &iev->ibuf;
334 ssize_t n;
335 int shut = 0;
336 struct imsg imsg;
338 if (event & EV_READ) {
339 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
340 fatal("imsg_read error");
341 if (n == 0) {
342 /* Connection closed. */
343 shut = 1;
344 goto done;
348 if (event & EV_WRITE) {
349 n = msgbuf_write(&ibuf->w);
350 if (n == -1 && errno != EAGAIN)
351 fatal("msgbuf_write");
352 if (n == 0) {
353 /* Connection closed. */
354 shut = 1;
355 goto done;
359 for (;;) {
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. */
365 break;
367 switch (imsg.hdr.type) {
368 case GOTD_IMSG_NOTIFY:
369 err = send_notification(&imsg, iev);
370 break;
371 default:
372 log_debug("unexpected imsg %d", imsg.hdr.type);
373 break;
375 imsg_free(&imsg);
377 if (err)
378 log_warnx("%s: %s", __func__, err->msg);
380 done:
381 if (!shut) {
382 gotd_imsg_event_add(iev);
383 } else {
384 struct gotd_notify_session *session;
386 /* This pipe is dead. Remove its event handler */
387 event_del(&iev->ev);
388 imsg_clear(&iev->ibuf);
390 session = find_session_by_fd(fd);
391 if (session)
392 remove_session(session);
396 static const struct got_error *
397 recv_session(struct imsg *imsg)
399 struct gotd_notify_session *session;
400 size_t datalen;
401 int fd;
403 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
404 if (datalen != 0)
405 return got_error(GOT_ERR_PRIVSEP_LEN);
407 fd = imsg_get_fd(imsg);
408 if (fd == -1)
409 return got_error(GOT_ERR_PRIVSEP_NO_FD);
411 session = calloc(1, sizeof(*session));
412 if (session == NULL)
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);
425 return NULL;
428 static void
429 notify_dispatch(int fd, short event, void *arg)
431 struct gotd_imsgev *iev = arg;
432 struct imsgbuf *ibuf = &iev->ibuf;
433 ssize_t n;
434 int shut = 0;
435 struct imsg imsg;
437 if (event & EV_READ) {
438 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
439 fatal("imsg_read error");
440 if (n == 0) {
441 /* Connection closed. */
442 shut = 1;
443 goto done;
447 if (event & EV_WRITE) {
448 n = msgbuf_write(&ibuf->w);
449 if (n == -1 && errno != EAGAIN)
450 fatal("msgbuf_write");
451 if (n == 0) {
452 /* Connection closed. */
453 shut = 1;
454 goto done;
458 for (;;) {
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. */
464 break;
466 switch (imsg.hdr.type) {
467 case GOTD_IMSG_CONNECT_SESSION:
468 err = recv_session(&imsg);
469 break;
470 default:
471 log_debug("unexpected imsg %d", imsg.hdr.type);
472 break;
474 imsg_free(&imsg);
476 if (err)
477 log_warnx("%s: %s", __func__, err->msg);
479 done:
480 if (!shut) {
481 gotd_imsg_event_add(iev);
482 } else {
483 /* This pipe is dead. Remove its event handler */
484 event_del(&iev->ev);
485 event_loopexit(NULL);
490 void
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);
523 event_dispatch();
525 if (err)
526 log_warnx("%s: %s", title, err->msg);
527 gotd_notify_shutdown();
530 void
531 gotd_notify_shutdown(void)
533 log_debug("%s: shutting down", gotd_notify.title);
534 exit(0);