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,
163 const char *user, const char *pass)
165 const struct got_error *err = NULL;
166 pid_t pid;
167 int child_status;
169 pid = fork();
170 if (pid == -1) {
171 err = got_error_from_errno("fork");
172 log_warn("%s", err->msg);
173 return;
174 } else if (pid == 0) {
175 signal(SIGQUIT, SIG_DFL);
176 signal(SIGINT, SIG_DFL);
177 signal(SIGCHLD, SIG_DFL);
179 if (dup2(fd, STDIN_FILENO) == -1) {
180 fprintf(stderr, "%s: dup2: %s\n", getprogname(),
181 strerror(errno));
182 _exit(1);
185 closefrom(STDERR_FILENO + 1);
187 if (user != NULL && pass != NULL) {
188 setenv("GOT_NOTIFY_HTTP_USER", user, 1);
189 setenv("GOT_NOTIFY_HTTP_PASS", pass, 1);
192 if (execv(prog, (char *const *)argv) == -1) {
193 fprintf(stderr, "%s: exec %s: %s\n", getprogname(),
194 prog, strerror(errno));
195 _exit(1);
198 /* not reached */
201 if (waitpid(pid, &child_status, 0) == -1) {
202 err = got_error_from_errno("waitpid");
203 goto done;
206 if (!WIFEXITED(child_status)) {
207 err = got_error(GOT_ERR_PRIVSEP_DIED);
208 goto done;
211 if (WEXITSTATUS(child_status) != 0)
212 err = got_error(GOT_ERR_PRIVSEP_EXIT);
213 done:
214 if (err)
215 log_warnx("%s: child %s pid %d: %s", gotd_notify.title,
216 prog, pid, err->msg);
219 static void
220 notify_email(struct gotd_notification_target *target, const char *subject_line,
221 int fd)
223 const char *argv[13];
224 int i = 0;
226 argv[i++] = GOTD_PATH_PROG_NOTIFY_EMAIL;
228 argv[i++] = "-f";
229 if (target->conf.email.sender)
230 argv[i++] = target->conf.email.sender;
231 else
232 argv[i++] = gotd_notify.default_sender;
234 if (target->conf.email.responder) {
235 argv[i++] = "-r";
236 argv[i++] = target->conf.email.responder;
239 if (target->conf.email.hostname) {
240 argv[i++] = "-h";
241 argv[i++] = target->conf.email.hostname;
244 if (target->conf.email.port) {
245 argv[i++] = "-p";
246 argv[i++] = target->conf.email.port;
249 argv[i++] = "-s";
250 argv[i++] = subject_line;
252 argv[i++] = target->conf.email.recipient;
254 argv[i] = NULL;
256 run_notification_helper(GOTD_PATH_PROG_NOTIFY_EMAIL, argv, fd,
257 NULL, NULL);
260 static void
261 notify_http(struct gotd_notification_target *target, const char *repo, int fd)
263 const char *argv[10];
264 int argc = 0;
266 argv[argc++] = GOTD_PATH_PROG_NOTIFY_HTTP;
267 if (target->conf.http.tls)
268 argv[argc++] = "-c";
270 argv[argc++] = "-r";
271 argv[argc++] = repo;
272 argv[argc++] = "-h";
273 argv[argc++] = target->conf.http.hostname;
274 argv[argc++] = "-p";
275 argv[argc++] = target->conf.http.port;
277 argv[argc++] = target->conf.http.path;
279 argv[argc] = NULL;
281 run_notification_helper(GOTD_PATH_PROG_NOTIFY_HTTP, argv, fd,
282 target->conf.http.user, target->conf.http.password);
285 static const struct got_error *
286 send_notification(struct imsg *imsg, struct gotd_imsgev *iev)
288 const struct got_error *err = NULL;
289 struct gotd_imsg_notify inotify;
290 size_t datalen;
291 struct gotd_repo *repo;
292 struct gotd_notification_target *target;
293 int fd;
295 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
296 if (datalen != sizeof(inotify))
297 return got_error(GOT_ERR_PRIVSEP_LEN);
299 memcpy(&inotify, imsg->data, datalen);
301 repo = gotd_find_repo_by_name(inotify.repo_name, gotd_notify.repos);
302 if (repo == NULL)
303 return got_error(GOT_ERR_PRIVSEP_MSG);
305 fd = imsg_get_fd(imsg);
306 if (fd == -1)
307 return got_error(GOT_ERR_PRIVSEP_NO_FD);
309 if (lseek(fd, 0, SEEK_SET) == -1) {
310 err = got_error_from_errno("lseek");
311 goto done;
314 STAILQ_FOREACH(target, &repo->notification_targets, entry) {
315 switch (target->type) {
316 case GOTD_NOTIFICATION_VIA_EMAIL:
317 notify_email(target, inotify.subject_line, fd);
318 break;
319 case GOTD_NOTIFICATION_VIA_HTTP:
320 notify_http(target, repo->name, fd);
321 break;
325 if (gotd_imsg_compose_event(iev, GOTD_IMSG_NOTIFICATION_SENT,
326 PROC_NOTIFY, -1, NULL, 0) == -1) {
327 err = got_error_from_errno("imsg compose NOTIFY");
328 goto done;
330 done:
331 close(fd);
332 return err;
335 static void
336 notify_dispatch_session(int fd, short event, void *arg)
338 struct gotd_imsgev *iev = arg;
339 struct imsgbuf *ibuf = &iev->ibuf;
340 ssize_t n;
341 int shut = 0;
342 struct imsg imsg;
344 if (event & EV_READ) {
345 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
346 fatal("imsg_read error");
347 if (n == 0) {
348 /* Connection closed. */
349 shut = 1;
350 goto done;
354 if (event & EV_WRITE) {
355 n = msgbuf_write(&ibuf->w);
356 if (n == -1 && errno != EAGAIN)
357 fatal("msgbuf_write");
358 if (n == 0) {
359 /* Connection closed. */
360 shut = 1;
361 goto done;
365 for (;;) {
366 const struct got_error *err = NULL;
368 if ((n = imsg_get(ibuf, &imsg)) == -1)
369 fatal("%s: imsg_get error", __func__);
370 if (n == 0) /* No more messages. */
371 break;
373 switch (imsg.hdr.type) {
374 case GOTD_IMSG_NOTIFY:
375 err = send_notification(&imsg, iev);
376 break;
377 default:
378 log_debug("unexpected imsg %d", imsg.hdr.type);
379 break;
381 imsg_free(&imsg);
383 if (err)
384 log_warnx("%s: %s", __func__, err->msg);
386 done:
387 if (!shut) {
388 gotd_imsg_event_add(iev);
389 } else {
390 struct gotd_notify_session *session;
392 /* This pipe is dead. Remove its event handler */
393 event_del(&iev->ev);
394 imsg_clear(&iev->ibuf);
396 session = find_session_by_fd(fd);
397 if (session)
398 remove_session(session);
402 static const struct got_error *
403 recv_session(struct imsg *imsg)
405 struct gotd_notify_session *session;
406 size_t datalen;
407 int fd;
409 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
410 if (datalen != 0)
411 return got_error(GOT_ERR_PRIVSEP_LEN);
413 fd = imsg_get_fd(imsg);
414 if (fd == -1)
415 return got_error(GOT_ERR_PRIVSEP_NO_FD);
417 session = calloc(1, sizeof(*session));
418 if (session == NULL)
419 return got_error_from_errno("calloc");
421 session->id = get_session_id();
422 imsg_init(&session->iev.ibuf, fd);
423 session->iev.handler = notify_dispatch_session;
424 session->iev.events = EV_READ;
425 session->iev.handler_arg = NULL;
426 event_set(&session->iev.ev, session->iev.ibuf.fd, EV_READ,
427 notify_dispatch_session, &session->iev);
428 gotd_imsg_event_add(&session->iev);
429 add_session(session);
431 return NULL;
434 static void
435 notify_dispatch(int fd, short event, void *arg)
437 struct gotd_imsgev *iev = arg;
438 struct imsgbuf *ibuf = &iev->ibuf;
439 ssize_t n;
440 int shut = 0;
441 struct imsg imsg;
443 if (event & EV_READ) {
444 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
445 fatal("imsg_read error");
446 if (n == 0) {
447 /* Connection closed. */
448 shut = 1;
449 goto done;
453 if (event & EV_WRITE) {
454 n = msgbuf_write(&ibuf->w);
455 if (n == -1 && errno != EAGAIN)
456 fatal("msgbuf_write");
457 if (n == 0) {
458 /* Connection closed. */
459 shut = 1;
460 goto done;
464 for (;;) {
465 const struct got_error *err = NULL;
467 if ((n = imsg_get(ibuf, &imsg)) == -1)
468 fatal("%s: imsg_get error", __func__);
469 if (n == 0) /* No more messages. */
470 break;
472 switch (imsg.hdr.type) {
473 case GOTD_IMSG_CONNECT_SESSION:
474 err = recv_session(&imsg);
475 break;
476 default:
477 log_debug("unexpected imsg %d", imsg.hdr.type);
478 break;
480 imsg_free(&imsg);
482 if (err)
483 log_warnx("%s: %s", __func__, err->msg);
485 done:
486 if (!shut) {
487 gotd_imsg_event_add(iev);
488 } else {
489 /* This pipe is dead. Remove its event handler */
490 event_del(&iev->ev);
491 event_loopexit(NULL);
496 void
497 notify_main(const char *title, struct gotd_repolist *repos,
498 const char *default_sender)
500 const struct got_error *err = NULL;
501 struct event evsigint, evsigterm, evsighup, evsigusr1;
503 arc4random_buf(&sessions_hash_key, sizeof(sessions_hash_key));
505 gotd_notify.title = title;
506 gotd_notify.repos = repos;
507 gotd_notify.default_sender = default_sender;
508 gotd_notify.pid = getpid();
510 signal_set(&evsigint, SIGINT, gotd_notify_sighdlr, NULL);
511 signal_set(&evsigterm, SIGTERM, gotd_notify_sighdlr, NULL);
512 signal_set(&evsighup, SIGHUP, gotd_notify_sighdlr, NULL);
513 signal_set(&evsigusr1, SIGUSR1, gotd_notify_sighdlr, NULL);
514 signal(SIGPIPE, SIG_IGN);
516 signal_add(&evsigint, NULL);
517 signal_add(&evsigterm, NULL);
518 signal_add(&evsighup, NULL);
519 signal_add(&evsigusr1, NULL);
521 imsg_init(&gotd_notify.parent_iev.ibuf, GOTD_FILENO_MSG_PIPE);
522 gotd_notify.parent_iev.handler = notify_dispatch;
523 gotd_notify.parent_iev.events = EV_READ;
524 gotd_notify.parent_iev.handler_arg = NULL;
525 event_set(&gotd_notify.parent_iev.ev, gotd_notify.parent_iev.ibuf.fd,
526 EV_READ, notify_dispatch, &gotd_notify.parent_iev);
527 gotd_imsg_event_add(&gotd_notify.parent_iev);
529 event_dispatch();
531 if (err)
532 log_warnx("%s: %s", title, err->msg);
533 gotd_notify_shutdown();
536 void
537 gotd_notify_shutdown(void)
539 log_debug("%s: shutting down", gotd_notify.title);
540 exit(0);