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;
108 return NULL;
111 static void
112 remove_session(struct gotd_notify_session *session)
114 uint64_t slot;
116 slot = session_hash(session->id) % nitems(gotd_notify_sessions);
117 STAILQ_REMOVE(&gotd_notify_sessions[slot], session,
118 gotd_notify_session, entry);
119 free(session);
122 static uint32_t
123 get_session_id(void)
125 int duplicate = 0;
126 uint32_t id;
128 do {
129 id = arc4random();
130 duplicate = (find_session(id) != NULL);
131 } while (duplicate || id == 0);
133 return id;
136 static void
137 gotd_notify_sighdlr(int sig, short event, void *arg)
139 /*
140 * Normal signal handler rules don't apply because libevent
141 * decouples for us.
142 */
144 switch (sig) {
145 case SIGHUP:
146 log_info("%s: ignoring SIGHUP", __func__);
147 break;
148 case SIGUSR1:
149 log_info("%s: ignoring SIGUSR1", __func__);
150 break;
151 case SIGTERM:
152 case SIGINT:
153 gotd_notify_shutdown();
154 /* NOTREACHED */
155 break;
156 default:
157 fatalx("unexpected signal");
161 static void
162 run_notification_helper(const char *prog, const char **argv, int fd)
164 const struct got_error *err = NULL;
165 pid_t pid;
166 int child_status;
168 pid = fork();
169 if (pid == -1) {
170 err = got_error_from_errno("fork");
171 log_warn("%s", err->msg);
172 return;
173 } else if (pid == 0) {
174 signal(SIGQUIT, SIG_DFL);
175 signal(SIGINT, SIG_DFL);
176 signal(SIGCHLD, SIG_DFL);
178 if (dup2(fd, STDIN_FILENO) == -1) {
179 fprintf(stderr, "%s: dup2: %s\n", getprogname(),
180 strerror(errno));
181 _exit(1);
184 closefrom(STDERR_FILENO + 1);
186 if (execv(prog, (char *const *)argv) == -1) {
187 fprintf(stderr, "%s: exec %s: %s\n", getprogname(),
188 prog, strerror(errno));
189 _exit(1);
192 /* not reached */
195 if (waitpid(pid, &child_status, 0) == -1) {
196 err = got_error_from_errno("waitpid");
197 goto done;
200 if (!WIFEXITED(child_status)) {
201 err = got_error(GOT_ERR_PRIVSEP_DIED);
202 goto done;
205 if (WEXITSTATUS(child_status) != 0)
206 err = got_error(GOT_ERR_PRIVSEP_EXIT);
207 done:
208 if (err)
209 log_warnx("%s: child %s pid %d: %s", gotd_notify.title,
210 prog, pid, err->msg);
213 static void
214 notify_email(struct gotd_notification_target *target, const char *subject_line,
215 int fd)
217 const char *argv[13];
218 int i = 0;
220 argv[i++] = GOTD_PATH_PROG_NOTIFY_EMAIL;
222 argv[i++] = "-f";
223 if (target->conf.email.sender)
224 argv[i++] = target->conf.email.sender;
225 else
226 argv[i++] = gotd_notify.default_sender;
228 if (target->conf.email.responder) {
229 argv[i++] = "-r";
230 argv[i++] = target->conf.email.responder;
233 if (target->conf.email.hostname) {
234 argv[i++] = "-h";
235 argv[i++] = target->conf.email.hostname;
238 if (target->conf.email.port) {
239 argv[i++] = "-p";
240 argv[i++] = target->conf.email.port;
243 argv[i++] = "-s";
244 argv[i++] = subject_line;
246 argv[i++] = target->conf.email.recipient;
248 argv[i] = NULL;
250 run_notification_helper(GOTD_PATH_PROG_NOTIFY_EMAIL, argv, fd);
253 static void
254 notify_http(struct gotd_notification_target *target, const char *subject_line,
255 int fd)
257 const char *argv[10] = { 0 }; /* TODO */
259 run_notification_helper(GOTD_PATH_PROG_NOTIFY_HTTP, argv, fd);
262 static const struct got_error *
263 send_notification(struct imsg *imsg, struct gotd_imsgev *iev)
265 const struct got_error *err = NULL;
266 struct gotd_imsg_notify inotify;
267 size_t datalen;
268 struct gotd_repo *repo;
269 struct gotd_notification_target *target;
270 int fd;
272 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
273 if (datalen != sizeof(inotify))
274 return got_error(GOT_ERR_PRIVSEP_LEN);
276 memcpy(&inotify, imsg->data, datalen);
278 repo = gotd_find_repo_by_name(inotify.repo_name, gotd_notify.repos);
279 if (repo == NULL)
280 return got_error(GOT_ERR_PRIVSEP_MSG);
282 fd = imsg_get_fd(imsg);
283 if (fd == -1)
284 return got_error(GOT_ERR_PRIVSEP_NO_FD);
286 if (lseek(fd, 0, SEEK_SET) == -1) {
287 err = got_error_from_errno("lseek");
288 goto done;
291 STAILQ_FOREACH(target, &repo->notification_targets, entry) {
292 switch (target->type) {
293 case GOTD_NOTIFICATION_VIA_EMAIL:
294 notify_email(target, inotify.subject_line, fd);
295 break;
296 case GOTD_NOTIFICATION_VIA_HTTP:
297 notify_http(target, inotify.subject_line, fd);
298 break;
302 if (gotd_imsg_compose_event(iev, GOTD_IMSG_NOTIFICATION_SENT,
303 PROC_NOTIFY, -1, NULL, 0) == -1) {
304 err = got_error_from_errno("imsg compose NOTIFY");
305 goto done;
307 done:
308 close(fd);
309 return err;
312 static void
313 notify_dispatch_session(int fd, short event, void *arg)
315 struct gotd_imsgev *iev = arg;
316 struct imsgbuf *ibuf = &iev->ibuf;
317 ssize_t n;
318 int shut = 0;
319 struct imsg imsg;
321 if (event & EV_READ) {
322 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
323 fatal("imsg_read error");
324 if (n == 0) {
325 /* Connection closed. */
326 shut = 1;
327 goto done;
331 if (event & EV_WRITE) {
332 n = msgbuf_write(&ibuf->w);
333 if (n == -1 && errno != EAGAIN)
334 fatal("msgbuf_write");
335 if (n == 0) {
336 /* Connection closed. */
337 shut = 1;
338 goto done;
342 for (;;) {
343 const struct got_error *err = NULL;
345 if ((n = imsg_get(ibuf, &imsg)) == -1)
346 fatal("%s: imsg_get error", __func__);
347 if (n == 0) /* No more messages. */
348 break;
350 switch (imsg.hdr.type) {
351 case GOTD_IMSG_NOTIFY:
352 err = send_notification(&imsg, iev);
353 break;
354 default:
355 log_debug("unexpected imsg %d", imsg.hdr.type);
356 break;
358 imsg_free(&imsg);
360 if (err)
361 log_warnx("%s: %s", __func__, err->msg);
363 done:
364 if (!shut) {
365 gotd_imsg_event_add(iev);
366 } else {
367 struct gotd_notify_session *session;
369 /* This pipe is dead. Remove its event handler */
370 event_del(&iev->ev);
371 imsg_clear(&iev->ibuf);
373 session = find_session_by_fd(fd);
374 if (session)
375 remove_session(session);
379 static const struct got_error *
380 recv_session(struct imsg *imsg)
382 struct gotd_notify_session *session;
383 size_t datalen;
384 int fd;
386 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
387 if (datalen != 0)
388 return got_error(GOT_ERR_PRIVSEP_LEN);
390 fd = imsg_get_fd(imsg);
391 if (fd == -1)
392 return got_error(GOT_ERR_PRIVSEP_NO_FD);
394 session = calloc(1, sizeof(*session));
395 if (session == NULL)
396 return got_error_from_errno("calloc");
398 session->id = get_session_id();
399 imsg_init(&session->iev.ibuf, fd);
400 session->iev.handler = notify_dispatch_session;
401 session->iev.events = EV_READ;
402 session->iev.handler_arg = NULL;
403 event_set(&session->iev.ev, session->iev.ibuf.fd, EV_READ,
404 notify_dispatch_session, &session->iev);
405 gotd_imsg_event_add(&session->iev);
406 add_session(session);
408 return NULL;
411 static void
412 notify_dispatch(int fd, short event, void *arg)
414 struct gotd_imsgev *iev = arg;
415 struct imsgbuf *ibuf = &iev->ibuf;
416 ssize_t n;
417 int shut = 0;
418 struct imsg imsg;
420 if (event & EV_READ) {
421 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
422 fatal("imsg_read error");
423 if (n == 0) {
424 /* Connection closed. */
425 shut = 1;
426 goto done;
430 if (event & EV_WRITE) {
431 n = msgbuf_write(&ibuf->w);
432 if (n == -1 && errno != EAGAIN)
433 fatal("msgbuf_write");
434 if (n == 0) {
435 /* Connection closed. */
436 shut = 1;
437 goto done;
441 for (;;) {
442 const struct got_error *err = NULL;
444 if ((n = imsg_get(ibuf, &imsg)) == -1)
445 fatal("%s: imsg_get error", __func__);
446 if (n == 0) /* No more messages. */
447 break;
449 switch (imsg.hdr.type) {
450 case GOTD_IMSG_CONNECT_SESSION:
451 err = recv_session(&imsg);
452 break;
453 default:
454 log_debug("unexpected imsg %d", imsg.hdr.type);
455 break;
457 imsg_free(&imsg);
459 if (err)
460 log_warnx("%s: %s", __func__, err->msg);
462 done:
463 if (!shut) {
464 gotd_imsg_event_add(iev);
465 } else {
466 /* This pipe is dead. Remove its event handler */
467 event_del(&iev->ev);
468 event_loopexit(NULL);
473 void
474 notify_main(const char *title, struct gotd_repolist *repos,
475 const char *default_sender)
477 const struct got_error *err = NULL;
478 struct event evsigint, evsigterm, evsighup, evsigusr1;
480 arc4random_buf(&sessions_hash_key, sizeof(sessions_hash_key));
482 gotd_notify.title = title;
483 gotd_notify.repos = repos;
484 gotd_notify.default_sender = default_sender;
485 gotd_notify.pid = getpid();
487 signal_set(&evsigint, SIGINT, gotd_notify_sighdlr, NULL);
488 signal_set(&evsigterm, SIGTERM, gotd_notify_sighdlr, NULL);
489 signal_set(&evsighup, SIGHUP, gotd_notify_sighdlr, NULL);
490 signal_set(&evsigusr1, SIGUSR1, gotd_notify_sighdlr, NULL);
491 signal(SIGPIPE, SIG_IGN);
493 signal_add(&evsigint, NULL);
494 signal_add(&evsigterm, NULL);
495 signal_add(&evsighup, NULL);
496 signal_add(&evsigusr1, NULL);
498 imsg_init(&gotd_notify.parent_iev.ibuf, GOTD_FILENO_MSG_PIPE);
499 gotd_notify.parent_iev.handler = notify_dispatch;
500 gotd_notify.parent_iev.events = EV_READ;
501 gotd_notify.parent_iev.handler_arg = NULL;
502 event_set(&gotd_notify.parent_iev.ev, gotd_notify.parent_iev.ibuf.fd,
503 EV_READ, notify_dispatch, &gotd_notify.parent_iev);
504 gotd_imsg_event_add(&gotd_notify.parent_iev);
506 event_dispatch();
508 if (err)
509 log_warnx("%s: %s", title, err->msg);
510 gotd_notify_shutdown();
513 void
514 gotd_notify_shutdown(void)
516 log_debug("%s: shutting down", gotd_notify.title);
517 exit(0);