Blame


1 ce1bfad9 2024-03-30 thomas /*
2 ce1bfad9 2024-03-30 thomas * Copyright (c) 2024 Stefan Sperling <stsp@openbsd.org>
3 ce1bfad9 2024-03-30 thomas *
4 ce1bfad9 2024-03-30 thomas * Permission to use, copy, modify, and distribute this software for any
5 ce1bfad9 2024-03-30 thomas * purpose with or without fee is hereby granted, provided that the above
6 ce1bfad9 2024-03-30 thomas * copyright notice and this permission notice appear in all copies.
7 ce1bfad9 2024-03-30 thomas *
8 ce1bfad9 2024-03-30 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 ce1bfad9 2024-03-30 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 ce1bfad9 2024-03-30 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 ce1bfad9 2024-03-30 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 ce1bfad9 2024-03-30 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 ce1bfad9 2024-03-30 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 ce1bfad9 2024-03-30 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 ce1bfad9 2024-03-30 thomas */
16 ce1bfad9 2024-03-30 thomas
17 b2ce1dae 2024-03-30 thomas #include "got_compat.h"
18 b2ce1dae 2024-03-30 thomas
19 ce1bfad9 2024-03-30 thomas #include <sys/types.h>
20 ce1bfad9 2024-03-30 thomas #include <sys/queue.h>
21 ce1bfad9 2024-03-30 thomas #include <sys/socket.h>
22 ce1bfad9 2024-03-30 thomas #include <sys/wait.h>
23 ce1bfad9 2024-03-30 thomas
24 ce1bfad9 2024-03-30 thomas #include <errno.h>
25 ce1bfad9 2024-03-30 thomas #include <event.h>
26 ce1bfad9 2024-03-30 thomas #include <limits.h>
27 ce1bfad9 2024-03-30 thomas #include <signal.h>
28 ce1bfad9 2024-03-30 thomas #include <stdio.h>
29 ce1bfad9 2024-03-30 thomas #include <stdlib.h>
30 ce1bfad9 2024-03-30 thomas #include <string.h>
31 ce1bfad9 2024-03-30 thomas #include <imsg.h>
32 ce1bfad9 2024-03-30 thomas #include <unistd.h>
33 ce1bfad9 2024-03-30 thomas
34 ce1bfad9 2024-03-30 thomas #include "got_error.h"
35 ce1bfad9 2024-03-30 thomas #include "got_path.h"
36 ce1bfad9 2024-03-30 thomas
37 ce1bfad9 2024-03-30 thomas #include "gotd.h"
38 ce1bfad9 2024-03-30 thomas #include "log.h"
39 ce1bfad9 2024-03-30 thomas #include "notify.h"
40 ce1bfad9 2024-03-30 thomas
41 ce1bfad9 2024-03-30 thomas #ifndef nitems
42 ce1bfad9 2024-03-30 thomas #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
43 ce1bfad9 2024-03-30 thomas #endif
44 ce1bfad9 2024-03-30 thomas
45 ce1bfad9 2024-03-30 thomas static struct gotd_notify {
46 ce1bfad9 2024-03-30 thomas pid_t pid;
47 ce1bfad9 2024-03-30 thomas const char *title;
48 ce1bfad9 2024-03-30 thomas struct gotd_imsgev parent_iev;
49 ce1bfad9 2024-03-30 thomas struct gotd_repolist *repos;
50 ce1bfad9 2024-03-30 thomas const char *default_sender;
51 ce1bfad9 2024-03-30 thomas } gotd_notify;
52 ce1bfad9 2024-03-30 thomas
53 ce1bfad9 2024-03-30 thomas struct gotd_notify_session {
54 ce1bfad9 2024-03-30 thomas STAILQ_ENTRY(gotd_notify_session) entry;
55 ce1bfad9 2024-03-30 thomas uint32_t id;
56 ce1bfad9 2024-03-30 thomas struct gotd_imsgev iev;
57 ce1bfad9 2024-03-30 thomas };
58 ce1bfad9 2024-03-30 thomas STAILQ_HEAD(gotd_notify_sessions, gotd_notify_session);
59 ce1bfad9 2024-03-30 thomas
60 ce1bfad9 2024-03-30 thomas static struct gotd_notify_sessions gotd_notify_sessions[GOTD_CLIENT_TABLE_SIZE];
61 ce1bfad9 2024-03-30 thomas static SIPHASH_KEY sessions_hash_key;
62 ce1bfad9 2024-03-30 thomas
63 ce1bfad9 2024-03-30 thomas static void gotd_notify_shutdown(void);
64 ce1bfad9 2024-03-30 thomas
65 ce1bfad9 2024-03-30 thomas static uint64_t
66 ce1bfad9 2024-03-30 thomas session_hash(uint32_t session_id)
67 ce1bfad9 2024-03-30 thomas {
68 ce1bfad9 2024-03-30 thomas return SipHash24(&sessions_hash_key, &session_id, sizeof(session_id));
69 ce1bfad9 2024-03-30 thomas }
70 ce1bfad9 2024-03-30 thomas
71 ce1bfad9 2024-03-30 thomas static void
72 ce1bfad9 2024-03-30 thomas add_session(struct gotd_notify_session *session)
73 ce1bfad9 2024-03-30 thomas {
74 ce1bfad9 2024-03-30 thomas uint64_t slot;
75 ce1bfad9 2024-03-30 thomas
76 ce1bfad9 2024-03-30 thomas slot = session_hash(session->id) % nitems(gotd_notify_sessions);
77 ce1bfad9 2024-03-30 thomas STAILQ_INSERT_HEAD(&gotd_notify_sessions[slot], session, entry);
78 ce1bfad9 2024-03-30 thomas }
79 ce1bfad9 2024-03-30 thomas
80 ce1bfad9 2024-03-30 thomas static struct gotd_notify_session *
81 ce1bfad9 2024-03-30 thomas find_session(uint32_t session_id)
82 ce1bfad9 2024-03-30 thomas {
83 ce1bfad9 2024-03-30 thomas uint64_t slot;
84 ce1bfad9 2024-03-30 thomas struct gotd_notify_session *s;
85 ce1bfad9 2024-03-30 thomas
86 ce1bfad9 2024-03-30 thomas slot = session_hash(session_id) % nitems(gotd_notify_sessions);
87 ce1bfad9 2024-03-30 thomas STAILQ_FOREACH(s, &gotd_notify_sessions[slot], entry) {
88 ce1bfad9 2024-03-30 thomas if (s->id == session_id)
89 ce1bfad9 2024-03-30 thomas return s;
90 ce1bfad9 2024-03-30 thomas }
91 ce1bfad9 2024-03-30 thomas
92 ce1bfad9 2024-03-30 thomas return NULL;
93 ce1bfad9 2024-03-30 thomas }
94 ce1bfad9 2024-03-30 thomas
95 ce1bfad9 2024-03-30 thomas static struct gotd_notify_session *
96 ce1bfad9 2024-03-30 thomas find_session_by_fd(int fd)
97 ce1bfad9 2024-03-30 thomas {
98 ce1bfad9 2024-03-30 thomas uint64_t slot;
99 ce1bfad9 2024-03-30 thomas struct gotd_notify_session *s;
100 ce1bfad9 2024-03-30 thomas
101 ce1bfad9 2024-03-30 thomas for (slot = 0; slot < nitems(gotd_notify_sessions); slot++) {
102 ce1bfad9 2024-03-30 thomas STAILQ_FOREACH(s, &gotd_notify_sessions[slot], entry) {
103 ce1bfad9 2024-03-30 thomas if (s->iev.ibuf.fd == fd)
104 ce1bfad9 2024-03-30 thomas return s;
105 ce1bfad9 2024-03-30 thomas }
106 ce1bfad9 2024-03-30 thomas }
107 ce1bfad9 2024-03-30 thomas
108 ce1bfad9 2024-03-30 thomas return NULL;
109 ce1bfad9 2024-03-30 thomas }
110 ce1bfad9 2024-03-30 thomas
111 ce1bfad9 2024-03-30 thomas static void
112 ce1bfad9 2024-03-30 thomas remove_session(struct gotd_notify_session *session)
113 ce1bfad9 2024-03-30 thomas {
114 ce1bfad9 2024-03-30 thomas uint64_t slot;
115 ce1bfad9 2024-03-30 thomas
116 ce1bfad9 2024-03-30 thomas slot = session_hash(session->id) % nitems(gotd_notify_sessions);
117 ce1bfad9 2024-03-30 thomas STAILQ_REMOVE(&gotd_notify_sessions[slot], session,
118 ce1bfad9 2024-03-30 thomas gotd_notify_session, entry);
119 ce1bfad9 2024-03-30 thomas free(session);
120 ce1bfad9 2024-03-30 thomas }
121 ce1bfad9 2024-03-30 thomas
122 ce1bfad9 2024-03-30 thomas static uint32_t
123 ce1bfad9 2024-03-30 thomas get_session_id(void)
124 ce1bfad9 2024-03-30 thomas {
125 ce1bfad9 2024-03-30 thomas int duplicate = 0;
126 ce1bfad9 2024-03-30 thomas uint32_t id;
127 ce1bfad9 2024-03-30 thomas
128 ce1bfad9 2024-03-30 thomas do {
129 ce1bfad9 2024-03-30 thomas id = arc4random();
130 ce1bfad9 2024-03-30 thomas duplicate = (find_session(id) != NULL);
131 ce1bfad9 2024-03-30 thomas } while (duplicate || id == 0);
132 ce1bfad9 2024-03-30 thomas
133 ce1bfad9 2024-03-30 thomas return id;
134 ce1bfad9 2024-03-30 thomas }
135 ce1bfad9 2024-03-30 thomas
136 ce1bfad9 2024-03-30 thomas static void
137 ce1bfad9 2024-03-30 thomas gotd_notify_sighdlr(int sig, short event, void *arg)
138 ce1bfad9 2024-03-30 thomas {
139 ce1bfad9 2024-03-30 thomas /*
140 ce1bfad9 2024-03-30 thomas * Normal signal handler rules don't apply because libevent
141 ce1bfad9 2024-03-30 thomas * decouples for us.
142 ce1bfad9 2024-03-30 thomas */
143 ce1bfad9 2024-03-30 thomas
144 ce1bfad9 2024-03-30 thomas switch (sig) {
145 ce1bfad9 2024-03-30 thomas case SIGHUP:
146 ce1bfad9 2024-03-30 thomas log_info("%s: ignoring SIGHUP", __func__);
147 ce1bfad9 2024-03-30 thomas break;
148 ce1bfad9 2024-03-30 thomas case SIGUSR1:
149 ce1bfad9 2024-03-30 thomas log_info("%s: ignoring SIGUSR1", __func__);
150 ce1bfad9 2024-03-30 thomas break;
151 ce1bfad9 2024-03-30 thomas case SIGTERM:
152 ce1bfad9 2024-03-30 thomas case SIGINT:
153 ce1bfad9 2024-03-30 thomas gotd_notify_shutdown();
154 ce1bfad9 2024-03-30 thomas /* NOTREACHED */
155 ce1bfad9 2024-03-30 thomas break;
156 ce1bfad9 2024-03-30 thomas default:
157 ce1bfad9 2024-03-30 thomas fatalx("unexpected signal");
158 ce1bfad9 2024-03-30 thomas }
159 ce1bfad9 2024-03-30 thomas }
160 ce1bfad9 2024-03-30 thomas
161 ce1bfad9 2024-03-30 thomas static void
162 ce1bfad9 2024-03-30 thomas run_notification_helper(const char *prog, const char **argv, int fd)
163 ce1bfad9 2024-03-30 thomas {
164 ce1bfad9 2024-03-30 thomas const struct got_error *err = NULL;
165 ce1bfad9 2024-03-30 thomas pid_t pid;
166 ce1bfad9 2024-03-30 thomas int child_status;
167 ce1bfad9 2024-03-30 thomas
168 ce1bfad9 2024-03-30 thomas pid = fork();
169 ce1bfad9 2024-03-30 thomas if (pid == -1) {
170 ce1bfad9 2024-03-30 thomas err = got_error_from_errno("fork");
171 ce1bfad9 2024-03-30 thomas log_warn("%s", err->msg);
172 ce1bfad9 2024-03-30 thomas return;
173 ce1bfad9 2024-03-30 thomas } else if (pid == 0) {
174 ce1bfad9 2024-03-30 thomas signal(SIGQUIT, SIG_DFL);
175 ce1bfad9 2024-03-30 thomas signal(SIGINT, SIG_DFL);
176 ce1bfad9 2024-03-30 thomas signal(SIGCHLD, SIG_DFL);
177 ce1bfad9 2024-03-30 thomas
178 ce1bfad9 2024-03-30 thomas if (dup2(fd, STDIN_FILENO) == -1) {
179 ce1bfad9 2024-03-30 thomas fprintf(stderr, "%s: dup2: %s\n", getprogname(),
180 ce1bfad9 2024-03-30 thomas strerror(errno));
181 ce1bfad9 2024-03-30 thomas _exit(1);
182 ce1bfad9 2024-03-30 thomas }
183 ce1bfad9 2024-03-30 thomas
184 ce1bfad9 2024-03-30 thomas closefrom(STDERR_FILENO + 1);
185 ce1bfad9 2024-03-30 thomas
186 ce1bfad9 2024-03-30 thomas if (execv(prog, (char *const *)argv) == -1) {
187 ce1bfad9 2024-03-30 thomas fprintf(stderr, "%s: exec %s: %s\n", getprogname(),
188 ce1bfad9 2024-03-30 thomas prog, strerror(errno));
189 ce1bfad9 2024-03-30 thomas _exit(1);
190 ce1bfad9 2024-03-30 thomas }
191 ce1bfad9 2024-03-30 thomas
192 ce1bfad9 2024-03-30 thomas /* not reached */
193 ce1bfad9 2024-03-30 thomas }
194 ce1bfad9 2024-03-30 thomas
195 ce1bfad9 2024-03-30 thomas if (waitpid(pid, &child_status, 0) == -1) {
196 ce1bfad9 2024-03-30 thomas err = got_error_from_errno("waitpid");
197 ce1bfad9 2024-03-30 thomas goto done;
198 ce1bfad9 2024-03-30 thomas }
199 ce1bfad9 2024-03-30 thomas
200 ce1bfad9 2024-03-30 thomas if (!WIFEXITED(child_status)) {
201 ce1bfad9 2024-03-30 thomas err = got_error(GOT_ERR_PRIVSEP_DIED);
202 ce1bfad9 2024-03-30 thomas goto done;
203 ce1bfad9 2024-03-30 thomas }
204 ce1bfad9 2024-03-30 thomas
205 ce1bfad9 2024-03-30 thomas if (WEXITSTATUS(child_status) != 0)
206 ce1bfad9 2024-03-30 thomas err = got_error(GOT_ERR_PRIVSEP_EXIT);
207 ce1bfad9 2024-03-30 thomas done:
208 ce1bfad9 2024-03-30 thomas if (err)
209 ce1bfad9 2024-03-30 thomas log_warnx("%s: child %s pid %d: %s", gotd_notify.title,
210 ce1bfad9 2024-03-30 thomas prog, pid, err->msg);
211 ce1bfad9 2024-03-30 thomas }
212 ce1bfad9 2024-03-30 thomas
213 ce1bfad9 2024-03-30 thomas static void
214 ce1bfad9 2024-03-30 thomas notify_email(struct gotd_notification_target *target, const char *subject_line,
215 ce1bfad9 2024-03-30 thomas int fd)
216 ce1bfad9 2024-03-30 thomas {
217 ce1bfad9 2024-03-30 thomas const char *argv[13];
218 ce1bfad9 2024-03-30 thomas int i = 0;
219 ce1bfad9 2024-03-30 thomas
220 ce1bfad9 2024-03-30 thomas argv[i++] = GOTD_PATH_PROG_NOTIFY_EMAIL;
221 ce1bfad9 2024-03-30 thomas
222 ce1bfad9 2024-03-30 thomas argv[i++] = "-f";
223 ce1bfad9 2024-03-30 thomas if (target->conf.email.sender)
224 ce1bfad9 2024-03-30 thomas argv[i++] = target->conf.email.sender;
225 ce1bfad9 2024-03-30 thomas else
226 ce1bfad9 2024-03-30 thomas argv[i++] = gotd_notify.default_sender;
227 ce1bfad9 2024-03-30 thomas
228 ce1bfad9 2024-03-30 thomas if (target->conf.email.responder) {
229 ce1bfad9 2024-03-30 thomas argv[i++] = "-r";
230 ce1bfad9 2024-03-30 thomas argv[i++] = target->conf.email.responder;
231 ce1bfad9 2024-03-30 thomas }
232 ce1bfad9 2024-03-30 thomas
233 ce1bfad9 2024-03-30 thomas if (target->conf.email.hostname) {
234 ce1bfad9 2024-03-30 thomas argv[i++] = "-h";
235 ce1bfad9 2024-03-30 thomas argv[i++] = target->conf.email.hostname;
236 ce1bfad9 2024-03-30 thomas }
237 ce1bfad9 2024-03-30 thomas
238 ce1bfad9 2024-03-30 thomas if (target->conf.email.port) {
239 ce1bfad9 2024-03-30 thomas argv[i++] = "-p";
240 ce1bfad9 2024-03-30 thomas argv[i++] = target->conf.email.port;
241 ce1bfad9 2024-03-30 thomas }
242 ce1bfad9 2024-03-30 thomas
243 ce1bfad9 2024-03-30 thomas argv[i++] = "-s";
244 ce1bfad9 2024-03-30 thomas argv[i++] = subject_line;
245 ce1bfad9 2024-03-30 thomas
246 ce1bfad9 2024-03-30 thomas argv[i++] = target->conf.email.recipient;
247 ce1bfad9 2024-03-30 thomas
248 ce1bfad9 2024-03-30 thomas argv[i] = NULL;
249 ce1bfad9 2024-03-30 thomas
250 ce1bfad9 2024-03-30 thomas run_notification_helper(GOTD_PATH_PROG_NOTIFY_EMAIL, argv, fd);
251 ce1bfad9 2024-03-30 thomas }
252 ce1bfad9 2024-03-30 thomas
253 ce1bfad9 2024-03-30 thomas static void
254 94a3f4e9 2024-03-30 thomas notify_http(struct gotd_notification_target *target, int fd)
255 ce1bfad9 2024-03-30 thomas {
256 94a3f4e9 2024-03-30 thomas const char *argv[8];
257 94a3f4e9 2024-03-30 thomas int argc = 0;
258 ce1bfad9 2024-03-30 thomas
259 94a3f4e9 2024-03-30 thomas argv[argc++] = GOTD_PATH_PROG_NOTIFY_HTTP;
260 94a3f4e9 2024-03-30 thomas if (target->conf.http.tls)
261 94a3f4e9 2024-03-30 thomas argv[argc++] = "-c";
262 94a3f4e9 2024-03-30 thomas
263 94a3f4e9 2024-03-30 thomas argv[argc++] = "-h";
264 94a3f4e9 2024-03-30 thomas argv[argc++] = target->conf.http.hostname;
265 94a3f4e9 2024-03-30 thomas argv[argc++] = "-p";
266 94a3f4e9 2024-03-30 thomas argv[argc++] = target->conf.http.port;
267 94a3f4e9 2024-03-30 thomas
268 94a3f4e9 2024-03-30 thomas argv[argc++] = target->conf.http.path;
269 94a3f4e9 2024-03-30 thomas
270 94a3f4e9 2024-03-30 thomas argv[argc] = NULL;
271 94a3f4e9 2024-03-30 thomas
272 ce1bfad9 2024-03-30 thomas run_notification_helper(GOTD_PATH_PROG_NOTIFY_HTTP, argv, fd);
273 ce1bfad9 2024-03-30 thomas }
274 ce1bfad9 2024-03-30 thomas
275 ce1bfad9 2024-03-30 thomas static const struct got_error *
276 ce1bfad9 2024-03-30 thomas send_notification(struct imsg *imsg, struct gotd_imsgev *iev)
277 ce1bfad9 2024-03-30 thomas {
278 ce1bfad9 2024-03-30 thomas const struct got_error *err = NULL;
279 ce1bfad9 2024-03-30 thomas struct gotd_imsg_notify inotify;
280 ce1bfad9 2024-03-30 thomas size_t datalen;
281 ce1bfad9 2024-03-30 thomas struct gotd_repo *repo;
282 ce1bfad9 2024-03-30 thomas struct gotd_notification_target *target;
283 ce1bfad9 2024-03-30 thomas int fd;
284 ce1bfad9 2024-03-30 thomas
285 ce1bfad9 2024-03-30 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
286 ce1bfad9 2024-03-30 thomas if (datalen != sizeof(inotify))
287 ce1bfad9 2024-03-30 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
288 ce1bfad9 2024-03-30 thomas
289 ce1bfad9 2024-03-30 thomas memcpy(&inotify, imsg->data, datalen);
290 ce1bfad9 2024-03-30 thomas
291 ce1bfad9 2024-03-30 thomas repo = gotd_find_repo_by_name(inotify.repo_name, gotd_notify.repos);
292 ce1bfad9 2024-03-30 thomas if (repo == NULL)
293 ce1bfad9 2024-03-30 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
294 ce1bfad9 2024-03-30 thomas
295 ce1bfad9 2024-03-30 thomas fd = imsg_get_fd(imsg);
296 ce1bfad9 2024-03-30 thomas if (fd == -1)
297 ce1bfad9 2024-03-30 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
298 ce1bfad9 2024-03-30 thomas
299 ce1bfad9 2024-03-30 thomas if (lseek(fd, 0, SEEK_SET) == -1) {
300 ce1bfad9 2024-03-30 thomas err = got_error_from_errno("lseek");
301 ce1bfad9 2024-03-30 thomas goto done;
302 ce1bfad9 2024-03-30 thomas }
303 ce1bfad9 2024-03-30 thomas
304 ce1bfad9 2024-03-30 thomas STAILQ_FOREACH(target, &repo->notification_targets, entry) {
305 ce1bfad9 2024-03-30 thomas switch (target->type) {
306 ce1bfad9 2024-03-30 thomas case GOTD_NOTIFICATION_VIA_EMAIL:
307 ce1bfad9 2024-03-30 thomas notify_email(target, inotify.subject_line, fd);
308 ce1bfad9 2024-03-30 thomas break;
309 ce1bfad9 2024-03-30 thomas case GOTD_NOTIFICATION_VIA_HTTP:
310 94a3f4e9 2024-03-30 thomas notify_http(target, fd);
311 ce1bfad9 2024-03-30 thomas break;
312 ce1bfad9 2024-03-30 thomas }
313 ce1bfad9 2024-03-30 thomas }
314 ce1bfad9 2024-03-30 thomas
315 ce1bfad9 2024-03-30 thomas if (gotd_imsg_compose_event(iev, GOTD_IMSG_NOTIFICATION_SENT,
316 ce1bfad9 2024-03-30 thomas PROC_NOTIFY, -1, NULL, 0) == -1) {
317 ce1bfad9 2024-03-30 thomas err = got_error_from_errno("imsg compose NOTIFY");
318 ce1bfad9 2024-03-30 thomas goto done;
319 ce1bfad9 2024-03-30 thomas }
320 ce1bfad9 2024-03-30 thomas done:
321 ce1bfad9 2024-03-30 thomas close(fd);
322 ce1bfad9 2024-03-30 thomas return err;
323 ce1bfad9 2024-03-30 thomas }
324 ce1bfad9 2024-03-30 thomas
325 ce1bfad9 2024-03-30 thomas static void
326 ce1bfad9 2024-03-30 thomas notify_dispatch_session(int fd, short event, void *arg)
327 ce1bfad9 2024-03-30 thomas {
328 ce1bfad9 2024-03-30 thomas struct gotd_imsgev *iev = arg;
329 ce1bfad9 2024-03-30 thomas struct imsgbuf *ibuf = &iev->ibuf;
330 ce1bfad9 2024-03-30 thomas ssize_t n;
331 ce1bfad9 2024-03-30 thomas int shut = 0;
332 ce1bfad9 2024-03-30 thomas struct imsg imsg;
333 ce1bfad9 2024-03-30 thomas
334 ce1bfad9 2024-03-30 thomas if (event & EV_READ) {
335 ce1bfad9 2024-03-30 thomas if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
336 ce1bfad9 2024-03-30 thomas fatal("imsg_read error");
337 ce1bfad9 2024-03-30 thomas if (n == 0) {
338 ce1bfad9 2024-03-30 thomas /* Connection closed. */
339 ce1bfad9 2024-03-30 thomas shut = 1;
340 ce1bfad9 2024-03-30 thomas goto done;
341 ce1bfad9 2024-03-30 thomas }
342 ce1bfad9 2024-03-30 thomas }
343 ce1bfad9 2024-03-30 thomas
344 ce1bfad9 2024-03-30 thomas if (event & EV_WRITE) {
345 ce1bfad9 2024-03-30 thomas n = msgbuf_write(&ibuf->w);
346 ce1bfad9 2024-03-30 thomas if (n == -1 && errno != EAGAIN)
347 ce1bfad9 2024-03-30 thomas fatal("msgbuf_write");
348 ce1bfad9 2024-03-30 thomas if (n == 0) {
349 ce1bfad9 2024-03-30 thomas /* Connection closed. */
350 ce1bfad9 2024-03-30 thomas shut = 1;
351 ce1bfad9 2024-03-30 thomas goto done;
352 ce1bfad9 2024-03-30 thomas }
353 ce1bfad9 2024-03-30 thomas }
354 ce1bfad9 2024-03-30 thomas
355 ce1bfad9 2024-03-30 thomas for (;;) {
356 ce1bfad9 2024-03-30 thomas const struct got_error *err = NULL;
357 ce1bfad9 2024-03-30 thomas
358 ce1bfad9 2024-03-30 thomas if ((n = imsg_get(ibuf, &imsg)) == -1)
359 ce1bfad9 2024-03-30 thomas fatal("%s: imsg_get error", __func__);
360 ce1bfad9 2024-03-30 thomas if (n == 0) /* No more messages. */
361 ce1bfad9 2024-03-30 thomas break;
362 ce1bfad9 2024-03-30 thomas
363 ce1bfad9 2024-03-30 thomas switch (imsg.hdr.type) {
364 ce1bfad9 2024-03-30 thomas case GOTD_IMSG_NOTIFY:
365 ce1bfad9 2024-03-30 thomas err = send_notification(&imsg, iev);
366 ce1bfad9 2024-03-30 thomas break;
367 ce1bfad9 2024-03-30 thomas default:
368 ce1bfad9 2024-03-30 thomas log_debug("unexpected imsg %d", imsg.hdr.type);
369 ce1bfad9 2024-03-30 thomas break;
370 ce1bfad9 2024-03-30 thomas }
371 ce1bfad9 2024-03-30 thomas imsg_free(&imsg);
372 ce1bfad9 2024-03-30 thomas
373 ce1bfad9 2024-03-30 thomas if (err)
374 ce1bfad9 2024-03-30 thomas log_warnx("%s: %s", __func__, err->msg);
375 ce1bfad9 2024-03-30 thomas }
376 ce1bfad9 2024-03-30 thomas done:
377 ce1bfad9 2024-03-30 thomas if (!shut) {
378 ce1bfad9 2024-03-30 thomas gotd_imsg_event_add(iev);
379 ce1bfad9 2024-03-30 thomas } else {
380 ce1bfad9 2024-03-30 thomas struct gotd_notify_session *session;
381 ce1bfad9 2024-03-30 thomas
382 ce1bfad9 2024-03-30 thomas /* This pipe is dead. Remove its event handler */
383 ce1bfad9 2024-03-30 thomas event_del(&iev->ev);
384 ce1bfad9 2024-03-30 thomas imsg_clear(&iev->ibuf);
385 ce1bfad9 2024-03-30 thomas
386 ce1bfad9 2024-03-30 thomas session = find_session_by_fd(fd);
387 ce1bfad9 2024-03-30 thomas if (session)
388 ce1bfad9 2024-03-30 thomas remove_session(session);
389 ce1bfad9 2024-03-30 thomas }
390 ce1bfad9 2024-03-30 thomas }
391 ce1bfad9 2024-03-30 thomas
392 ce1bfad9 2024-03-30 thomas static const struct got_error *
393 ce1bfad9 2024-03-30 thomas recv_session(struct imsg *imsg)
394 ce1bfad9 2024-03-30 thomas {
395 ce1bfad9 2024-03-30 thomas struct gotd_notify_session *session;
396 ce1bfad9 2024-03-30 thomas size_t datalen;
397 ce1bfad9 2024-03-30 thomas int fd;
398 ce1bfad9 2024-03-30 thomas
399 ce1bfad9 2024-03-30 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
400 ce1bfad9 2024-03-30 thomas if (datalen != 0)
401 ce1bfad9 2024-03-30 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
402 ce1bfad9 2024-03-30 thomas
403 ce1bfad9 2024-03-30 thomas fd = imsg_get_fd(imsg);
404 ce1bfad9 2024-03-30 thomas if (fd == -1)
405 ce1bfad9 2024-03-30 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
406 ce1bfad9 2024-03-30 thomas
407 ce1bfad9 2024-03-30 thomas session = calloc(1, sizeof(*session));
408 ce1bfad9 2024-03-30 thomas if (session == NULL)
409 ce1bfad9 2024-03-30 thomas return got_error_from_errno("calloc");
410 ce1bfad9 2024-03-30 thomas
411 ce1bfad9 2024-03-30 thomas session->id = get_session_id();
412 ce1bfad9 2024-03-30 thomas imsg_init(&session->iev.ibuf, fd);
413 ce1bfad9 2024-03-30 thomas session->iev.handler = notify_dispatch_session;
414 ce1bfad9 2024-03-30 thomas session->iev.events = EV_READ;
415 ce1bfad9 2024-03-30 thomas session->iev.handler_arg = NULL;
416 ce1bfad9 2024-03-30 thomas event_set(&session->iev.ev, session->iev.ibuf.fd, EV_READ,
417 ce1bfad9 2024-03-30 thomas notify_dispatch_session, &session->iev);
418 ce1bfad9 2024-03-30 thomas gotd_imsg_event_add(&session->iev);
419 ce1bfad9 2024-03-30 thomas add_session(session);
420 ce1bfad9 2024-03-30 thomas
421 ce1bfad9 2024-03-30 thomas return NULL;
422 ce1bfad9 2024-03-30 thomas }
423 ce1bfad9 2024-03-30 thomas
424 ce1bfad9 2024-03-30 thomas static void
425 ce1bfad9 2024-03-30 thomas notify_dispatch(int fd, short event, void *arg)
426 ce1bfad9 2024-03-30 thomas {
427 ce1bfad9 2024-03-30 thomas struct gotd_imsgev *iev = arg;
428 ce1bfad9 2024-03-30 thomas struct imsgbuf *ibuf = &iev->ibuf;
429 ce1bfad9 2024-03-30 thomas ssize_t n;
430 ce1bfad9 2024-03-30 thomas int shut = 0;
431 ce1bfad9 2024-03-30 thomas struct imsg imsg;
432 ce1bfad9 2024-03-30 thomas
433 ce1bfad9 2024-03-30 thomas if (event & EV_READ) {
434 ce1bfad9 2024-03-30 thomas if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
435 ce1bfad9 2024-03-30 thomas fatal("imsg_read error");
436 ce1bfad9 2024-03-30 thomas if (n == 0) {
437 ce1bfad9 2024-03-30 thomas /* Connection closed. */
438 ce1bfad9 2024-03-30 thomas shut = 1;
439 ce1bfad9 2024-03-30 thomas goto done;
440 ce1bfad9 2024-03-30 thomas }
441 ce1bfad9 2024-03-30 thomas }
442 ce1bfad9 2024-03-30 thomas
443 ce1bfad9 2024-03-30 thomas if (event & EV_WRITE) {
444 ce1bfad9 2024-03-30 thomas n = msgbuf_write(&ibuf->w);
445 ce1bfad9 2024-03-30 thomas if (n == -1 && errno != EAGAIN)
446 ce1bfad9 2024-03-30 thomas fatal("msgbuf_write");
447 ce1bfad9 2024-03-30 thomas if (n == 0) {
448 ce1bfad9 2024-03-30 thomas /* Connection closed. */
449 ce1bfad9 2024-03-30 thomas shut = 1;
450 ce1bfad9 2024-03-30 thomas goto done;
451 ce1bfad9 2024-03-30 thomas }
452 ce1bfad9 2024-03-30 thomas }
453 ce1bfad9 2024-03-30 thomas
454 ce1bfad9 2024-03-30 thomas for (;;) {
455 ce1bfad9 2024-03-30 thomas const struct got_error *err = NULL;
456 ce1bfad9 2024-03-30 thomas
457 ce1bfad9 2024-03-30 thomas if ((n = imsg_get(ibuf, &imsg)) == -1)
458 ce1bfad9 2024-03-30 thomas fatal("%s: imsg_get error", __func__);
459 ce1bfad9 2024-03-30 thomas if (n == 0) /* No more messages. */
460 ce1bfad9 2024-03-30 thomas break;
461 ce1bfad9 2024-03-30 thomas
462 ce1bfad9 2024-03-30 thomas switch (imsg.hdr.type) {
463 ce1bfad9 2024-03-30 thomas case GOTD_IMSG_CONNECT_SESSION:
464 ce1bfad9 2024-03-30 thomas err = recv_session(&imsg);
465 ce1bfad9 2024-03-30 thomas break;
466 ce1bfad9 2024-03-30 thomas default:
467 ce1bfad9 2024-03-30 thomas log_debug("unexpected imsg %d", imsg.hdr.type);
468 ce1bfad9 2024-03-30 thomas break;
469 ce1bfad9 2024-03-30 thomas }
470 ce1bfad9 2024-03-30 thomas imsg_free(&imsg);
471 ce1bfad9 2024-03-30 thomas
472 ce1bfad9 2024-03-30 thomas if (err)
473 ce1bfad9 2024-03-30 thomas log_warnx("%s: %s", __func__, err->msg);
474 ce1bfad9 2024-03-30 thomas }
475 ce1bfad9 2024-03-30 thomas done:
476 ce1bfad9 2024-03-30 thomas if (!shut) {
477 ce1bfad9 2024-03-30 thomas gotd_imsg_event_add(iev);
478 ce1bfad9 2024-03-30 thomas } else {
479 ce1bfad9 2024-03-30 thomas /* This pipe is dead. Remove its event handler */
480 ce1bfad9 2024-03-30 thomas event_del(&iev->ev);
481 ce1bfad9 2024-03-30 thomas event_loopexit(NULL);
482 ce1bfad9 2024-03-30 thomas }
483 ce1bfad9 2024-03-30 thomas
484 ce1bfad9 2024-03-30 thomas }
485 ce1bfad9 2024-03-30 thomas
486 ce1bfad9 2024-03-30 thomas void
487 ce1bfad9 2024-03-30 thomas notify_main(const char *title, struct gotd_repolist *repos,
488 ce1bfad9 2024-03-30 thomas const char *default_sender)
489 ce1bfad9 2024-03-30 thomas {
490 ce1bfad9 2024-03-30 thomas const struct got_error *err = NULL;
491 ce1bfad9 2024-03-30 thomas struct event evsigint, evsigterm, evsighup, evsigusr1;
492 ce1bfad9 2024-03-30 thomas
493 ce1bfad9 2024-03-30 thomas arc4random_buf(&sessions_hash_key, sizeof(sessions_hash_key));
494 ce1bfad9 2024-03-30 thomas
495 ce1bfad9 2024-03-30 thomas gotd_notify.title = title;
496 ce1bfad9 2024-03-30 thomas gotd_notify.repos = repos;
497 ce1bfad9 2024-03-30 thomas gotd_notify.default_sender = default_sender;
498 ce1bfad9 2024-03-30 thomas gotd_notify.pid = getpid();
499 ce1bfad9 2024-03-30 thomas
500 ce1bfad9 2024-03-30 thomas signal_set(&evsigint, SIGINT, gotd_notify_sighdlr, NULL);
501 ce1bfad9 2024-03-30 thomas signal_set(&evsigterm, SIGTERM, gotd_notify_sighdlr, NULL);
502 ce1bfad9 2024-03-30 thomas signal_set(&evsighup, SIGHUP, gotd_notify_sighdlr, NULL);
503 ce1bfad9 2024-03-30 thomas signal_set(&evsigusr1, SIGUSR1, gotd_notify_sighdlr, NULL);
504 ce1bfad9 2024-03-30 thomas signal(SIGPIPE, SIG_IGN);
505 ce1bfad9 2024-03-30 thomas
506 ce1bfad9 2024-03-30 thomas signal_add(&evsigint, NULL);
507 ce1bfad9 2024-03-30 thomas signal_add(&evsigterm, NULL);
508 ce1bfad9 2024-03-30 thomas signal_add(&evsighup, NULL);
509 ce1bfad9 2024-03-30 thomas signal_add(&evsigusr1, NULL);
510 ce1bfad9 2024-03-30 thomas
511 ce1bfad9 2024-03-30 thomas imsg_init(&gotd_notify.parent_iev.ibuf, GOTD_FILENO_MSG_PIPE);
512 ce1bfad9 2024-03-30 thomas gotd_notify.parent_iev.handler = notify_dispatch;
513 ce1bfad9 2024-03-30 thomas gotd_notify.parent_iev.events = EV_READ;
514 ce1bfad9 2024-03-30 thomas gotd_notify.parent_iev.handler_arg = NULL;
515 ce1bfad9 2024-03-30 thomas event_set(&gotd_notify.parent_iev.ev, gotd_notify.parent_iev.ibuf.fd,
516 ce1bfad9 2024-03-30 thomas EV_READ, notify_dispatch, &gotd_notify.parent_iev);
517 ce1bfad9 2024-03-30 thomas gotd_imsg_event_add(&gotd_notify.parent_iev);
518 ce1bfad9 2024-03-30 thomas
519 ce1bfad9 2024-03-30 thomas event_dispatch();
520 ce1bfad9 2024-03-30 thomas
521 ce1bfad9 2024-03-30 thomas if (err)
522 ce1bfad9 2024-03-30 thomas log_warnx("%s: %s", title, err->msg);
523 ce1bfad9 2024-03-30 thomas gotd_notify_shutdown();
524 ce1bfad9 2024-03-30 thomas }
525 ce1bfad9 2024-03-30 thomas
526 ce1bfad9 2024-03-30 thomas void
527 ce1bfad9 2024-03-30 thomas gotd_notify_shutdown(void)
528 ce1bfad9 2024-03-30 thomas {
529 b1a47061 2024-03-30 thomas log_debug("%s: shutting down", gotd_notify.title);
530 ce1bfad9 2024-03-30 thomas exit(0);
531 ce1bfad9 2024-03-30 thomas }