Blame


1 13b2bc37 2022-10-23 stsp /*
2 13b2bc37 2022-10-23 stsp * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 13b2bc37 2022-10-23 stsp *
4 13b2bc37 2022-10-23 stsp * Permission to use, copy, modify, and distribute this software for any
5 13b2bc37 2022-10-23 stsp * purpose with or without fee is hereby granted, provided that the above
6 13b2bc37 2022-10-23 stsp * copyright notice and this permission notice appear in all copies.
7 13b2bc37 2022-10-23 stsp *
8 13b2bc37 2022-10-23 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 13b2bc37 2022-10-23 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 13b2bc37 2022-10-23 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 13b2bc37 2022-10-23 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 13b2bc37 2022-10-23 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 13b2bc37 2022-10-23 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 13b2bc37 2022-10-23 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 13b2bc37 2022-10-23 stsp */
16 13b2bc37 2022-10-23 stsp
17 13b2bc37 2022-10-23 stsp #include <sys/queue.h>
18 13b2bc37 2022-10-23 stsp #include <sys/tree.h>
19 13b2bc37 2022-10-23 stsp #include <sys/time.h>
20 13b2bc37 2022-10-23 stsp #include <sys/types.h>
21 13b2bc37 2022-10-23 stsp #include <sys/stat.h>
22 13b2bc37 2022-10-23 stsp #include <sys/socket.h>
23 13b2bc37 2022-10-23 stsp #include <sys/un.h>
24 13b2bc37 2022-10-23 stsp #include <sys/wait.h>
25 abe89edb 2023-11-16 stsp #include <sys/resource.h>
26 13b2bc37 2022-10-23 stsp
27 13b2bc37 2022-10-23 stsp #include <fcntl.h>
28 13b2bc37 2022-10-23 stsp #include <err.h>
29 13b2bc37 2022-10-23 stsp #include <errno.h>
30 13b2bc37 2022-10-23 stsp #include <event.h>
31 13b2bc37 2022-10-23 stsp #include <limits.h>
32 13b2bc37 2022-10-23 stsp #include <pwd.h>
33 13b2bc37 2022-10-23 stsp #include <imsg.h>
34 13b2bc37 2022-10-23 stsp #include <sha1.h>
35 5822e79e 2023-02-23 op #include <sha2.h>
36 13b2bc37 2022-10-23 stsp #include <signal.h>
37 13b2bc37 2022-10-23 stsp #include <siphash.h>
38 13b2bc37 2022-10-23 stsp #include <stdarg.h>
39 13b2bc37 2022-10-23 stsp #include <stdio.h>
40 13b2bc37 2022-10-23 stsp #include <stdlib.h>
41 13b2bc37 2022-10-23 stsp #include <string.h>
42 13b2bc37 2022-10-23 stsp #include <syslog.h>
43 13b2bc37 2022-10-23 stsp #include <unistd.h>
44 13b2bc37 2022-10-23 stsp
45 13b2bc37 2022-10-23 stsp #include "got_error.h"
46 13b2bc37 2022-10-23 stsp #include "got_opentemp.h"
47 13b2bc37 2022-10-23 stsp #include "got_path.h"
48 13b2bc37 2022-10-23 stsp #include "got_repository.h"
49 13b2bc37 2022-10-23 stsp #include "got_object.h"
50 13b2bc37 2022-10-23 stsp #include "got_reference.h"
51 ba97b2d7 2024-03-20 stsp #include "got_diff.h"
52 13b2bc37 2022-10-23 stsp
53 13b2bc37 2022-10-23 stsp #include "got_lib_delta.h"
54 13b2bc37 2022-10-23 stsp #include "got_lib_object.h"
55 13b2bc37 2022-10-23 stsp #include "got_lib_object_cache.h"
56 53bf0b54 2023-02-23 op #include "got_lib_hash.h"
57 13b2bc37 2022-10-23 stsp #include "got_lib_gitproto.h"
58 13b2bc37 2022-10-23 stsp #include "got_lib_pack.h"
59 13b2bc37 2022-10-23 stsp #include "got_lib_repository.h"
60 13b2bc37 2022-10-23 stsp
61 13b2bc37 2022-10-23 stsp #include "gotd.h"
62 13b2bc37 2022-10-23 stsp #include "log.h"
63 d93ecf7d 2022-12-14 stsp #include "listen.h"
64 0ccf3acb 2022-11-16 stsp #include "auth.h"
65 e70fd952 2024-03-22 stsp #include "session_read.h"
66 e70fd952 2024-03-22 stsp #include "session_write.h"
67 13b2bc37 2022-10-23 stsp #include "repo_read.h"
68 13b2bc37 2022-10-23 stsp #include "repo_write.h"
69 ba97b2d7 2024-03-20 stsp #include "notify.h"
70 13b2bc37 2022-10-23 stsp
71 13b2bc37 2022-10-23 stsp #ifndef nitems
72 13b2bc37 2022-10-23 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
73 13b2bc37 2022-10-23 stsp #endif
74 13b2bc37 2022-10-23 stsp
75 eac23c30 2023-01-10 stsp enum gotd_client_state {
76 eac23c30 2023-01-10 stsp GOTD_CLIENT_STATE_NEW,
77 eac23c30 2023-01-10 stsp GOTD_CLIENT_STATE_ACCESS_GRANTED,
78 c929736a 2023-06-22 op };
79 c929736a 2023-06-22 op
80 c929736a 2023-06-22 op struct gotd_child_proc {
81 c929736a 2023-06-22 op pid_t pid;
82 c929736a 2023-06-22 op enum gotd_procid type;
83 c929736a 2023-06-22 op char repo_name[NAME_MAX];
84 c929736a 2023-06-22 op char repo_path[PATH_MAX];
85 c929736a 2023-06-22 op int pipe[2];
86 c929736a 2023-06-22 op struct gotd_imsgev iev;
87 839338f6 2023-06-22 op struct event tmo;
88 839338f6 2023-06-22 op
89 839338f6 2023-06-22 op TAILQ_ENTRY(gotd_child_proc) entry;
90 eac23c30 2023-01-10 stsp };
91 839338f6 2023-06-22 op TAILQ_HEAD(gotd_procs, gotd_child_proc) procs;
92 eac23c30 2023-01-10 stsp
93 13b2bc37 2022-10-23 stsp struct gotd_client {
94 13b2bc37 2022-10-23 stsp STAILQ_ENTRY(gotd_client) entry;
95 13b2bc37 2022-10-23 stsp enum gotd_client_state state;
96 13b2bc37 2022-10-23 stsp uint32_t id;
97 13b2bc37 2022-10-23 stsp int fd;
98 13b2bc37 2022-10-23 stsp struct gotd_imsgev iev;
99 13b2bc37 2022-10-23 stsp struct event tmo;
100 13b2bc37 2022-10-23 stsp uid_t euid;
101 13b2bc37 2022-10-23 stsp gid_t egid;
102 56624d2b 2023-12-27 stsp char *username;
103 f7a854cf 2023-01-10 stsp struct gotd_child_proc *repo;
104 5e25db14 2022-12-29 stsp struct gotd_child_proc *auth;
105 ae7c1b78 2023-01-10 stsp struct gotd_child_proc *session;
106 5e25db14 2022-12-29 stsp int required_auth;
107 13b2bc37 2022-10-23 stsp };
108 13b2bc37 2022-10-23 stsp STAILQ_HEAD(gotd_clients, gotd_client);
109 13b2bc37 2022-10-23 stsp
110 13b2bc37 2022-10-23 stsp static struct gotd_clients gotd_clients[GOTD_CLIENT_TABLE_SIZE];
111 13b2bc37 2022-10-23 stsp static SIPHASH_KEY clients_hash_key;
112 13b2bc37 2022-10-23 stsp volatile int client_cnt;
113 ef4e2f01 2022-12-29 stsp static struct timeval auth_timeout = { 5, 0 };
114 13b2bc37 2022-10-23 stsp static struct gotd gotd;
115 13b2bc37 2022-10-23 stsp
116 13b2bc37 2022-10-23 stsp void gotd_sighdlr(int sig, short event, void *arg);
117 f1752522 2022-10-29 stsp static void gotd_shutdown(void);
118 ae7c1b78 2023-01-10 stsp static const struct got_error *start_session_child(struct gotd_client *,
119 ae7c1b78 2023-01-10 stsp struct gotd_repo *, char *, const char *, int, int);
120 b50a2b46 2022-12-29 stsp static const struct got_error *start_repo_child(struct gotd_client *,
121 b50a2b46 2022-12-29 stsp enum gotd_procid, struct gotd_repo *, char *, const char *, int, int);
122 5e25db14 2022-12-29 stsp static const struct got_error *start_auth_child(struct gotd_client *, int,
123 5e25db14 2022-12-29 stsp struct gotd_repo *, char *, const char *, int, int);
124 b50a2b46 2022-12-29 stsp static void kill_proc(struct gotd_child_proc *, int);
125 839338f6 2023-06-22 op static void disconnect(struct gotd_client *);
126 13b2bc37 2022-10-23 stsp
127 13b2bc37 2022-10-23 stsp __dead static void
128 575dc3f9 2023-02-09 op usage(void)
129 13b2bc37 2022-10-23 stsp {
130 e9e01966 2023-01-18 stsp fprintf(stderr, "usage: %s [-dnv] [-f config-file]\n", getprogname());
131 88dec179 2022-10-24 stsp exit(1);
132 13b2bc37 2022-10-23 stsp }
133 13b2bc37 2022-10-23 stsp
134 13b2bc37 2022-10-23 stsp static int
135 13b2bc37 2022-10-23 stsp unix_socket_listen(const char *unix_socket_path, uid_t uid, gid_t gid)
136 13b2bc37 2022-10-23 stsp {
137 13b2bc37 2022-10-23 stsp struct sockaddr_un sun;
138 13b2bc37 2022-10-23 stsp int fd = -1;
139 13b2bc37 2022-10-23 stsp mode_t old_umask, mode;
140 13b2bc37 2022-10-23 stsp
141 13b2bc37 2022-10-23 stsp fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
142 13b2bc37 2022-10-23 stsp if (fd == -1) {
143 13b2bc37 2022-10-23 stsp log_warn("socket");
144 13b2bc37 2022-10-23 stsp return -1;
145 13b2bc37 2022-10-23 stsp }
146 13b2bc37 2022-10-23 stsp
147 13b2bc37 2022-10-23 stsp sun.sun_family = AF_UNIX;
148 13b2bc37 2022-10-23 stsp if (strlcpy(sun.sun_path, unix_socket_path,
149 13b2bc37 2022-10-23 stsp sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
150 13b2bc37 2022-10-23 stsp log_warnx("%s: name too long", unix_socket_path);
151 13b2bc37 2022-10-23 stsp close(fd);
152 13b2bc37 2022-10-23 stsp return -1;
153 13b2bc37 2022-10-23 stsp }
154 13b2bc37 2022-10-23 stsp
155 13b2bc37 2022-10-23 stsp if (unlink(unix_socket_path) == -1) {
156 13b2bc37 2022-10-23 stsp if (errno != ENOENT) {
157 13b2bc37 2022-10-23 stsp log_warn("unlink %s", unix_socket_path);
158 13b2bc37 2022-10-23 stsp close(fd);
159 13b2bc37 2022-10-23 stsp return -1;
160 13b2bc37 2022-10-23 stsp }
161 13b2bc37 2022-10-23 stsp }
162 13b2bc37 2022-10-23 stsp
163 13b2bc37 2022-10-23 stsp old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
164 6f854dde 2023-01-04 stsp mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
165 13b2bc37 2022-10-23 stsp
166 13b2bc37 2022-10-23 stsp if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
167 13b2bc37 2022-10-23 stsp log_warn("bind: %s", unix_socket_path);
168 13b2bc37 2022-10-23 stsp close(fd);
169 13b2bc37 2022-10-23 stsp umask(old_umask);
170 13b2bc37 2022-10-23 stsp return -1;
171 13b2bc37 2022-10-23 stsp }
172 13b2bc37 2022-10-23 stsp
173 13b2bc37 2022-10-23 stsp umask(old_umask);
174 13b2bc37 2022-10-23 stsp
175 13b2bc37 2022-10-23 stsp if (chmod(unix_socket_path, mode) == -1) {
176 13b2bc37 2022-10-23 stsp log_warn("chmod %o %s", mode, unix_socket_path);
177 13b2bc37 2022-10-23 stsp close(fd);
178 13b2bc37 2022-10-23 stsp unlink(unix_socket_path);
179 13b2bc37 2022-10-23 stsp return -1;
180 13b2bc37 2022-10-23 stsp }
181 13b2bc37 2022-10-23 stsp
182 13b2bc37 2022-10-23 stsp if (chown(unix_socket_path, uid, gid) == -1) {
183 13b2bc37 2022-10-23 stsp log_warn("chown %s uid=%d gid=%d", unix_socket_path, uid, gid);
184 13b2bc37 2022-10-23 stsp close(fd);
185 13b2bc37 2022-10-23 stsp unlink(unix_socket_path);
186 13b2bc37 2022-10-23 stsp return -1;
187 13b2bc37 2022-10-23 stsp }
188 13b2bc37 2022-10-23 stsp
189 13b2bc37 2022-10-23 stsp if (listen(fd, GOTD_UNIX_SOCKET_BACKLOG) == -1) {
190 13b2bc37 2022-10-23 stsp log_warn("listen");
191 13b2bc37 2022-10-23 stsp close(fd);
192 13b2bc37 2022-10-23 stsp unlink(unix_socket_path);
193 13b2bc37 2022-10-23 stsp return -1;
194 13b2bc37 2022-10-23 stsp }
195 13b2bc37 2022-10-23 stsp
196 13b2bc37 2022-10-23 stsp return fd;
197 13b2bc37 2022-10-23 stsp }
198 13b2bc37 2022-10-23 stsp
199 13b2bc37 2022-10-23 stsp static uint64_t
200 13b2bc37 2022-10-23 stsp client_hash(uint32_t client_id)
201 13b2bc37 2022-10-23 stsp {
202 13b2bc37 2022-10-23 stsp return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
203 13b2bc37 2022-10-23 stsp }
204 13b2bc37 2022-10-23 stsp
205 13b2bc37 2022-10-23 stsp static void
206 13b2bc37 2022-10-23 stsp add_client(struct gotd_client *client)
207 13b2bc37 2022-10-23 stsp {
208 13b2bc37 2022-10-23 stsp uint64_t slot = client_hash(client->id) % nitems(gotd_clients);
209 13b2bc37 2022-10-23 stsp STAILQ_INSERT_HEAD(&gotd_clients[slot], client, entry);
210 13b2bc37 2022-10-23 stsp client_cnt++;
211 13b2bc37 2022-10-23 stsp }
212 13b2bc37 2022-10-23 stsp
213 13b2bc37 2022-10-23 stsp static struct gotd_client *
214 13b2bc37 2022-10-23 stsp find_client(uint32_t client_id)
215 13b2bc37 2022-10-23 stsp {
216 13b2bc37 2022-10-23 stsp uint64_t slot;
217 13b2bc37 2022-10-23 stsp struct gotd_client *c;
218 13b2bc37 2022-10-23 stsp
219 13b2bc37 2022-10-23 stsp slot = client_hash(client_id) % nitems(gotd_clients);
220 13b2bc37 2022-10-23 stsp STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
221 13b2bc37 2022-10-23 stsp if (c->id == client_id)
222 13b2bc37 2022-10-23 stsp return c;
223 13b2bc37 2022-10-23 stsp }
224 13b2bc37 2022-10-23 stsp
225 13b2bc37 2022-10-23 stsp return NULL;
226 13b2bc37 2022-10-23 stsp }
227 13b2bc37 2022-10-23 stsp
228 b50a2b46 2022-12-29 stsp static struct gotd_client *
229 b50a2b46 2022-12-29 stsp find_client_by_proc_fd(int fd)
230 b50a2b46 2022-12-29 stsp {
231 b50a2b46 2022-12-29 stsp uint64_t slot;
232 b50a2b46 2022-12-29 stsp
233 b50a2b46 2022-12-29 stsp for (slot = 0; slot < nitems(gotd_clients); slot++) {
234 b50a2b46 2022-12-29 stsp struct gotd_client *c;
235 b50a2b46 2022-12-29 stsp
236 b50a2b46 2022-12-29 stsp STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
237 f7a854cf 2023-01-10 stsp if (c->repo && c->repo->iev.ibuf.fd == fd)
238 b50a2b46 2022-12-29 stsp return c;
239 5e25db14 2022-12-29 stsp if (c->auth && c->auth->iev.ibuf.fd == fd)
240 ae7c1b78 2023-01-10 stsp return c;
241 ae7c1b78 2023-01-10 stsp if (c->session && c->session->iev.ibuf.fd == fd)
242 5e25db14 2022-12-29 stsp return c;
243 b50a2b46 2022-12-29 stsp }
244 b50a2b46 2022-12-29 stsp }
245 f1752522 2022-10-29 stsp
246 13b2bc37 2022-10-23 stsp return NULL;
247 13b2bc37 2022-10-23 stsp }
248 13b2bc37 2022-10-23 stsp
249 13b2bc37 2022-10-23 stsp static int
250 13b2bc37 2022-10-23 stsp client_is_reading(struct gotd_client *client)
251 13b2bc37 2022-10-23 stsp {
252 f7a854cf 2023-01-10 stsp return (client->required_auth &
253 f7a854cf 2023-01-10 stsp (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) == GOTD_AUTH_READ;
254 13b2bc37 2022-10-23 stsp }
255 13b2bc37 2022-10-23 stsp
256 13b2bc37 2022-10-23 stsp static int
257 13b2bc37 2022-10-23 stsp client_is_writing(struct gotd_client *client)
258 13b2bc37 2022-10-23 stsp {
259 f7a854cf 2023-01-10 stsp return (client->required_auth &
260 f7a854cf 2023-01-10 stsp (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) ==
261 f7a854cf 2023-01-10 stsp (GOTD_AUTH_READ | GOTD_AUTH_WRITE);
262 13b2bc37 2022-10-23 stsp }
263 13b2bc37 2022-10-23 stsp
264 13b2bc37 2022-10-23 stsp static const struct got_error *
265 13b2bc37 2022-10-23 stsp ensure_client_is_not_writing(struct gotd_client *client)
266 13b2bc37 2022-10-23 stsp {
267 13b2bc37 2022-10-23 stsp if (client_is_writing(client)) {
268 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_BAD_PACKET,
269 13b2bc37 2022-10-23 stsp "uid %d made a read-request but is writing to "
270 13b2bc37 2022-10-23 stsp "a repository", client->euid);
271 13b2bc37 2022-10-23 stsp }
272 13b2bc37 2022-10-23 stsp
273 13b2bc37 2022-10-23 stsp return NULL;
274 13b2bc37 2022-10-23 stsp }
275 13b2bc37 2022-10-23 stsp
276 13b2bc37 2022-10-23 stsp static const struct got_error *
277 13b2bc37 2022-10-23 stsp ensure_client_is_not_reading(struct gotd_client *client)
278 13b2bc37 2022-10-23 stsp {
279 13b2bc37 2022-10-23 stsp if (client_is_reading(client)) {
280 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_BAD_PACKET,
281 13b2bc37 2022-10-23 stsp "uid %d made a write-request but is reading from "
282 13b2bc37 2022-10-23 stsp "a repository", client->euid);
283 13b2bc37 2022-10-23 stsp }
284 13b2bc37 2022-10-23 stsp
285 13b2bc37 2022-10-23 stsp return NULL;
286 b50a2b46 2022-12-29 stsp }
287 b50a2b46 2022-12-29 stsp
288 b50a2b46 2022-12-29 stsp static void
289 839338f6 2023-06-22 op proc_done(struct gotd_child_proc *proc)
290 b50a2b46 2022-12-29 stsp {
291 839338f6 2023-06-22 op struct gotd_client *client;
292 b50a2b46 2022-12-29 stsp
293 839338f6 2023-06-22 op TAILQ_REMOVE(&procs, proc, entry);
294 b50a2b46 2022-12-29 stsp
295 839338f6 2023-06-22 op client = find_client_by_proc_fd(proc->iev.ibuf.fd);
296 839338f6 2023-06-22 op if (client != NULL) {
297 839338f6 2023-06-22 op if (proc == client->repo)
298 839338f6 2023-06-22 op client->repo = NULL;
299 839338f6 2023-06-22 op if (proc == client->auth)
300 839338f6 2023-06-22 op client->auth = NULL;
301 839338f6 2023-06-22 op if (proc == client->session)
302 839338f6 2023-06-22 op client->session = NULL;
303 839338f6 2023-06-22 op disconnect(client);
304 839338f6 2023-06-22 op }
305 ae7c1b78 2023-01-10 stsp
306 ba97b2d7 2024-03-20 stsp if (proc == gotd.notify_proc)
307 ba97b2d7 2024-03-20 stsp gotd.notify_proc = NULL;
308 ba97b2d7 2024-03-20 stsp
309 839338f6 2023-06-22 op evtimer_del(&proc->tmo);
310 839338f6 2023-06-22 op
311 839338f6 2023-06-22 op if (proc->iev.ibuf.fd != -1) {
312 839338f6 2023-06-22 op event_del(&proc->iev.ev);
313 839338f6 2023-06-22 op msgbuf_clear(&proc->iev.ibuf.w);
314 839338f6 2023-06-22 op close(proc->iev.ibuf.fd);
315 839338f6 2023-06-22 op }
316 839338f6 2023-06-22 op
317 ae7c1b78 2023-01-10 stsp free(proc);
318 ba91039c 2023-06-22 op }
319 ba91039c 2023-06-22 op
320 ba91039c 2023-06-22 op static void
321 ba91039c 2023-06-22 op kill_repo_proc(struct gotd_client *client)
322 ba91039c 2023-06-22 op {
323 ba91039c 2023-06-22 op if (client->repo == NULL)
324 ba91039c 2023-06-22 op return;
325 ba91039c 2023-06-22 op
326 839338f6 2023-06-22 op kill_proc(client->repo, 0);
327 ba91039c 2023-06-22 op client->repo = NULL;
328 13b2bc37 2022-10-23 stsp }
329 13b2bc37 2022-10-23 stsp
330 13b2bc37 2022-10-23 stsp static void
331 5e25db14 2022-12-29 stsp kill_auth_proc(struct gotd_client *client)
332 5e25db14 2022-12-29 stsp {
333 5e25db14 2022-12-29 stsp if (client->auth == NULL)
334 5e25db14 2022-12-29 stsp return;
335 5e25db14 2022-12-29 stsp
336 839338f6 2023-06-22 op kill_proc(client->auth, 0);
337 5e25db14 2022-12-29 stsp client->auth = NULL;
338 5e25db14 2022-12-29 stsp }
339 5e25db14 2022-12-29 stsp
340 5e25db14 2022-12-29 stsp static void
341 ae7c1b78 2023-01-10 stsp kill_session_proc(struct gotd_client *client)
342 ae7c1b78 2023-01-10 stsp {
343 ae7c1b78 2023-01-10 stsp if (client->session == NULL)
344 ae7c1b78 2023-01-10 stsp return;
345 ae7c1b78 2023-01-10 stsp
346 839338f6 2023-06-22 op kill_proc(client->session, 0);
347 ae7c1b78 2023-01-10 stsp client->session = NULL;
348 ae7c1b78 2023-01-10 stsp }
349 ae7c1b78 2023-01-10 stsp
350 ae7c1b78 2023-01-10 stsp static void
351 13b2bc37 2022-10-23 stsp disconnect(struct gotd_client *client)
352 13b2bc37 2022-10-23 stsp {
353 13b2bc37 2022-10-23 stsp struct gotd_imsg_disconnect idisconnect;
354 c929736a 2023-06-22 op struct gotd_child_proc *listen_proc = gotd.listen_proc;
355 13b2bc37 2022-10-23 stsp uint64_t slot;
356 13b2bc37 2022-10-23 stsp
357 13b2bc37 2022-10-23 stsp log_debug("uid %d: disconnecting", client->euid);
358 5e25db14 2022-12-29 stsp
359 5e25db14 2022-12-29 stsp kill_auth_proc(client);
360 ae7c1b78 2023-01-10 stsp kill_session_proc(client);
361 ba91039c 2023-06-22 op kill_repo_proc(client);
362 d93ecf7d 2022-12-14 stsp
363 90270f79 2023-02-09 stsp idisconnect.client_id = client->id;
364 d93ecf7d 2022-12-14 stsp if (gotd_imsg_compose_event(&listen_proc->iev,
365 d93ecf7d 2022-12-14 stsp GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
366 d93ecf7d 2022-12-14 stsp &idisconnect, sizeof(idisconnect)) == -1)
367 d93ecf7d 2022-12-14 stsp log_warn("imsg compose DISCONNECT");
368 d93ecf7d 2022-12-14 stsp
369 13b2bc37 2022-10-23 stsp slot = client_hash(client->id) % nitems(gotd_clients);
370 13b2bc37 2022-10-23 stsp STAILQ_REMOVE(&gotd_clients[slot], client, gotd_client, entry);
371 13b2bc37 2022-10-23 stsp imsg_clear(&client->iev.ibuf);
372 13b2bc37 2022-10-23 stsp event_del(&client->iev.ev);
373 13b2bc37 2022-10-23 stsp evtimer_del(&client->tmo);
374 ae7c1b78 2023-01-10 stsp if (client->fd != -1)
375 ae7c1b78 2023-01-10 stsp close(client->fd);
376 ae7c1b78 2023-01-10 stsp else if (client->iev.ibuf.fd != -1)
377 ae7c1b78 2023-01-10 stsp close(client->iev.ibuf.fd);
378 56624d2b 2023-12-27 stsp free(client->username);
379 13b2bc37 2022-10-23 stsp free(client);
380 13b2bc37 2022-10-23 stsp client_cnt--;
381 13b2bc37 2022-10-23 stsp }
382 13b2bc37 2022-10-23 stsp
383 13b2bc37 2022-10-23 stsp static void
384 13b2bc37 2022-10-23 stsp disconnect_on_error(struct gotd_client *client, const struct got_error *err)
385 13b2bc37 2022-10-23 stsp {
386 13b2bc37 2022-10-23 stsp struct imsgbuf ibuf;
387 13b2bc37 2022-10-23 stsp
388 1050403b 2023-08-11 stsp if (err->code != GOT_ERR_EOF) {
389 1050403b 2023-08-11 stsp log_warnx("uid %d: %s", client->euid, err->msg);
390 1050403b 2023-08-11 stsp if (client->fd != -1) {
391 1050403b 2023-08-11 stsp imsg_init(&ibuf, client->fd);
392 1050403b 2023-08-11 stsp gotd_imsg_send_error(&ibuf, 0, PROC_GOTD, err);
393 1050403b 2023-08-11 stsp imsg_clear(&ibuf);
394 1050403b 2023-08-11 stsp }
395 13b2bc37 2022-10-23 stsp }
396 13b2bc37 2022-10-23 stsp disconnect(client);
397 f1752522 2022-10-29 stsp }
398 f1752522 2022-10-29 stsp
399 f1752522 2022-10-29 stsp static const struct got_error *
400 f1752522 2022-10-29 stsp send_repo_info(struct gotd_imsgev *iev, struct gotd_repo *repo)
401 f1752522 2022-10-29 stsp {
402 f1752522 2022-10-29 stsp const struct got_error *err = NULL;
403 f1752522 2022-10-29 stsp struct gotd_imsg_info_repo irepo;
404 f1752522 2022-10-29 stsp
405 f1752522 2022-10-29 stsp memset(&irepo, 0, sizeof(irepo));
406 f1752522 2022-10-29 stsp
407 f1752522 2022-10-29 stsp if (strlcpy(irepo.repo_name, repo->name, sizeof(irepo.repo_name))
408 f1752522 2022-10-29 stsp >= sizeof(irepo.repo_name))
409 f1752522 2022-10-29 stsp return got_error_msg(GOT_ERR_NO_SPACE, "repo name too long");
410 f1752522 2022-10-29 stsp if (strlcpy(irepo.repo_path, repo->path, sizeof(irepo.repo_path))
411 f1752522 2022-10-29 stsp >= sizeof(irepo.repo_path))
412 f1752522 2022-10-29 stsp return got_error_msg(GOT_ERR_NO_SPACE, "repo path too long");
413 f1752522 2022-10-29 stsp
414 f1752522 2022-10-29 stsp if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_REPO, PROC_GOTD, -1,
415 f1752522 2022-10-29 stsp &irepo, sizeof(irepo)) == -1) {
416 f1752522 2022-10-29 stsp err = got_error_from_errno("imsg compose INFO_REPO");
417 f1752522 2022-10-29 stsp if (err)
418 f1752522 2022-10-29 stsp return err;
419 f1752522 2022-10-29 stsp }
420 f1752522 2022-10-29 stsp
421 f1752522 2022-10-29 stsp return NULL;
422 f1752522 2022-10-29 stsp }
423 f1752522 2022-10-29 stsp
424 f1752522 2022-10-29 stsp static const struct got_error *
425 f1752522 2022-10-29 stsp send_client_info(struct gotd_imsgev *iev, struct gotd_client *client)
426 f1752522 2022-10-29 stsp {
427 f1752522 2022-10-29 stsp const struct got_error *err = NULL;
428 f1752522 2022-10-29 stsp struct gotd_imsg_info_client iclient;
429 f1752522 2022-10-29 stsp struct gotd_child_proc *proc;
430 f1752522 2022-10-29 stsp
431 f1752522 2022-10-29 stsp memset(&iclient, 0, sizeof(iclient));
432 f1752522 2022-10-29 stsp iclient.euid = client->euid;
433 f1752522 2022-10-29 stsp iclient.egid = client->egid;
434 f1752522 2022-10-29 stsp
435 f7a854cf 2023-01-10 stsp proc = client->repo;
436 f1752522 2022-10-29 stsp if (proc) {
437 eec68231 2022-12-14 stsp if (strlcpy(iclient.repo_name, proc->repo_path,
438 f1752522 2022-10-29 stsp sizeof(iclient.repo_name)) >= sizeof(iclient.repo_name)) {
439 f1752522 2022-10-29 stsp return got_error_msg(GOT_ERR_NO_SPACE,
440 f1752522 2022-10-29 stsp "repo name too long");
441 f1752522 2022-10-29 stsp }
442 f1752522 2022-10-29 stsp if (client_is_writing(client))
443 f1752522 2022-10-29 stsp iclient.is_writing = 1;
444 ae7c1b78 2023-01-10 stsp
445 ae7c1b78 2023-01-10 stsp iclient.repo_child_pid = proc->pid;
446 f1752522 2022-10-29 stsp }
447 f1752522 2022-10-29 stsp
448 ae7c1b78 2023-01-10 stsp if (client->session)
449 ae7c1b78 2023-01-10 stsp iclient.session_child_pid = client->session->pid;
450 f1752522 2022-10-29 stsp
451 f1752522 2022-10-29 stsp if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_CLIENT, PROC_GOTD, -1,
452 f1752522 2022-10-29 stsp &iclient, sizeof(iclient)) == -1) {
453 f1752522 2022-10-29 stsp err = got_error_from_errno("imsg compose INFO_CLIENT");
454 f1752522 2022-10-29 stsp if (err)
455 f1752522 2022-10-29 stsp return err;
456 f1752522 2022-10-29 stsp }
457 f1752522 2022-10-29 stsp
458 f1752522 2022-10-29 stsp return NULL;
459 f1752522 2022-10-29 stsp }
460 f1752522 2022-10-29 stsp
461 f1752522 2022-10-29 stsp static const struct got_error *
462 f1752522 2022-10-29 stsp send_info(struct gotd_client *client)
463 f1752522 2022-10-29 stsp {
464 f1752522 2022-10-29 stsp const struct got_error *err = NULL;
465 f1752522 2022-10-29 stsp struct gotd_imsg_info info;
466 f1752522 2022-10-29 stsp uint64_t slot;
467 f1752522 2022-10-29 stsp struct gotd_repo *repo;
468 f1752522 2022-10-29 stsp
469 78433331 2023-01-04 stsp if (client->euid != 0)
470 78433331 2023-01-04 stsp return got_error_set_errno(EPERM, "info");
471 78433331 2023-01-04 stsp
472 f1752522 2022-10-29 stsp info.pid = gotd.pid;
473 f1752522 2022-10-29 stsp info.verbosity = gotd.verbosity;
474 f1752522 2022-10-29 stsp info.nrepos = gotd.nrepos;
475 f1752522 2022-10-29 stsp info.nclients = client_cnt - 1;
476 f1752522 2022-10-29 stsp
477 f1752522 2022-10-29 stsp if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_INFO, PROC_GOTD, -1,
478 f1752522 2022-10-29 stsp &info, sizeof(info)) == -1) {
479 f1752522 2022-10-29 stsp err = got_error_from_errno("imsg compose INFO");
480 f1752522 2022-10-29 stsp if (err)
481 f1752522 2022-10-29 stsp return err;
482 f1752522 2022-10-29 stsp }
483 f1752522 2022-10-29 stsp
484 f1752522 2022-10-29 stsp TAILQ_FOREACH(repo, &gotd.repos, entry) {
485 f1752522 2022-10-29 stsp err = send_repo_info(&client->iev, repo);
486 f1752522 2022-10-29 stsp if (err)
487 f1752522 2022-10-29 stsp return err;
488 f1752522 2022-10-29 stsp }
489 f1752522 2022-10-29 stsp
490 f1752522 2022-10-29 stsp for (slot = 0; slot < nitems(gotd_clients); slot++) {
491 f1752522 2022-10-29 stsp struct gotd_client *c;
492 f1752522 2022-10-29 stsp STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
493 f1752522 2022-10-29 stsp if (c->id == client->id)
494 f1752522 2022-10-29 stsp continue;
495 f1752522 2022-10-29 stsp err = send_client_info(&client->iev, c);
496 f1752522 2022-10-29 stsp if (err)
497 f1752522 2022-10-29 stsp return err;
498 f1752522 2022-10-29 stsp }
499 f1752522 2022-10-29 stsp }
500 f1752522 2022-10-29 stsp
501 f1752522 2022-10-29 stsp return NULL;
502 f1752522 2022-10-29 stsp }
503 f1752522 2022-10-29 stsp
504 f1752522 2022-10-29 stsp static const struct got_error *
505 f1752522 2022-10-29 stsp stop_gotd(struct gotd_client *client)
506 f1752522 2022-10-29 stsp {
507 f1752522 2022-10-29 stsp
508 f1752522 2022-10-29 stsp if (client->euid != 0)
509 f1752522 2022-10-29 stsp return got_error_set_errno(EPERM, "stop");
510 f1752522 2022-10-29 stsp
511 f1752522 2022-10-29 stsp gotd_shutdown();
512 f1752522 2022-10-29 stsp /* NOTREACHED */
513 0ccf3acb 2022-11-16 stsp return NULL;
514 0ccf3acb 2022-11-16 stsp }
515 0ccf3acb 2022-11-16 stsp
516 13b2bc37 2022-10-23 stsp static const struct got_error *
517 ae7c1b78 2023-01-10 stsp start_client_authentication(struct gotd_client *client, struct imsg *imsg)
518 13b2bc37 2022-10-23 stsp {
519 13b2bc37 2022-10-23 stsp const struct got_error *err;
520 13b2bc37 2022-10-23 stsp struct gotd_imsg_list_refs ireq;
521 0ccf3acb 2022-11-16 stsp struct gotd_repo *repo = NULL;
522 13b2bc37 2022-10-23 stsp size_t datalen;
523 13b2bc37 2022-10-23 stsp
524 13b2bc37 2022-10-23 stsp log_debug("list-refs request from uid %d", client->euid);
525 13b2bc37 2022-10-23 stsp
526 eac23c30 2023-01-10 stsp if (client->state != GOTD_CLIENT_STATE_NEW)
527 ae7c1b78 2023-01-10 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
528 ae7c1b78 2023-01-10 stsp "unexpected list-refs request received");
529 ae7c1b78 2023-01-10 stsp
530 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
531 13b2bc37 2022-10-23 stsp if (datalen != sizeof(ireq))
532 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
533 13b2bc37 2022-10-23 stsp
534 13b2bc37 2022-10-23 stsp memcpy(&ireq, imsg->data, datalen);
535 13b2bc37 2022-10-23 stsp
536 13b2bc37 2022-10-23 stsp if (ireq.client_is_reading) {
537 13b2bc37 2022-10-23 stsp err = ensure_client_is_not_writing(client);
538 13b2bc37 2022-10-23 stsp if (err)
539 13b2bc37 2022-10-23 stsp return err;
540 ba97b2d7 2024-03-20 stsp repo = gotd_find_repo_by_name(ireq.repo_name, &gotd.repos);
541 0ccf3acb 2022-11-16 stsp if (repo == NULL)
542 0ccf3acb 2022-11-16 stsp return got_error(GOT_ERR_NOT_GIT_REPO);
543 5e25db14 2022-12-29 stsp err = start_auth_child(client, GOTD_AUTH_READ, repo,
544 b50a2b46 2022-12-29 stsp gotd.argv0, gotd.confpath, gotd.daemonize,
545 b50a2b46 2022-12-29 stsp gotd.verbosity);
546 b50a2b46 2022-12-29 stsp if (err)
547 b50a2b46 2022-12-29 stsp return err;
548 13b2bc37 2022-10-23 stsp } else {
549 13b2bc37 2022-10-23 stsp err = ensure_client_is_not_reading(client);
550 0ccf3acb 2022-11-16 stsp if (err)
551 0ccf3acb 2022-11-16 stsp return err;
552 ba97b2d7 2024-03-20 stsp repo = gotd_find_repo_by_name(ireq.repo_name, &gotd.repos);
553 0ccf3acb 2022-11-16 stsp if (repo == NULL)
554 0ccf3acb 2022-11-16 stsp return got_error(GOT_ERR_NOT_GIT_REPO);
555 5e25db14 2022-12-29 stsp err = start_auth_child(client,
556 5e25db14 2022-12-29 stsp GOTD_AUTH_READ | GOTD_AUTH_WRITE,
557 5e25db14 2022-12-29 stsp repo, gotd.argv0, gotd.confpath, gotd.daemonize,
558 b50a2b46 2022-12-29 stsp gotd.verbosity);
559 b50a2b46 2022-12-29 stsp if (err)
560 b50a2b46 2022-12-29 stsp return err;
561 13b2bc37 2022-10-23 stsp }
562 13b2bc37 2022-10-23 stsp
563 ae7c1b78 2023-01-10 stsp evtimer_add(&client->tmo, &auth_timeout);
564 13b2bc37 2022-10-23 stsp
565 5fb50fce 2023-07-14 stsp /* Flow continues upon authentication success/failure or timeout. */
566 13b2bc37 2022-10-23 stsp return NULL;
567 13b2bc37 2022-10-23 stsp }
568 13b2bc37 2022-10-23 stsp
569 13b2bc37 2022-10-23 stsp static void
570 13b2bc37 2022-10-23 stsp gotd_request(int fd, short events, void *arg)
571 13b2bc37 2022-10-23 stsp {
572 13b2bc37 2022-10-23 stsp struct gotd_imsgev *iev = arg;
573 13b2bc37 2022-10-23 stsp struct imsgbuf *ibuf = &iev->ibuf;
574 13b2bc37 2022-10-23 stsp struct gotd_client *client = iev->handler_arg;
575 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
576 13b2bc37 2022-10-23 stsp struct imsg imsg;
577 13b2bc37 2022-10-23 stsp ssize_t n;
578 13b2bc37 2022-10-23 stsp
579 13b2bc37 2022-10-23 stsp if (events & EV_WRITE) {
580 13b2bc37 2022-10-23 stsp while (ibuf->w.queued) {
581 13b2bc37 2022-10-23 stsp n = msgbuf_write(&ibuf->w);
582 13b2bc37 2022-10-23 stsp if (n == -1 && errno == EPIPE) {
583 13b2bc37 2022-10-23 stsp /*
584 13b2bc37 2022-10-23 stsp * The client has closed its socket.
585 13b2bc37 2022-10-23 stsp * This can happen when Git clients are
586 13b2bc37 2022-10-23 stsp * done sending pack file data.
587 77d0cae1 2022-12-30 op */
588 13b2bc37 2022-10-23 stsp msgbuf_clear(&ibuf->w);
589 13b2bc37 2022-10-23 stsp continue;
590 13b2bc37 2022-10-23 stsp } else if (n == -1 && errno != EAGAIN) {
591 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_flush");
592 13b2bc37 2022-10-23 stsp disconnect_on_error(client, err);
593 13b2bc37 2022-10-23 stsp return;
594 13b2bc37 2022-10-23 stsp }
595 13b2bc37 2022-10-23 stsp if (n == 0) {
596 13b2bc37 2022-10-23 stsp /* Connection closed. */
597 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_EOF);
598 13b2bc37 2022-10-23 stsp disconnect_on_error(client, err);
599 13b2bc37 2022-10-23 stsp return;
600 13b2bc37 2022-10-23 stsp }
601 13b2bc37 2022-10-23 stsp }
602 f1752522 2022-10-29 stsp
603 f1752522 2022-10-29 stsp /* Disconnect gotctl(8) now that messages have been sent. */
604 f1752522 2022-10-29 stsp if (!client_is_reading(client) && !client_is_writing(client)) {
605 f1752522 2022-10-29 stsp disconnect(client);
606 f1752522 2022-10-29 stsp return;
607 f1752522 2022-10-29 stsp }
608 13b2bc37 2022-10-23 stsp }
609 13b2bc37 2022-10-23 stsp
610 13b2bc37 2022-10-23 stsp if ((events & EV_READ) == 0)
611 13b2bc37 2022-10-23 stsp return;
612 13b2bc37 2022-10-23 stsp
613 13b2bc37 2022-10-23 stsp memset(&imsg, 0, sizeof(imsg));
614 13b2bc37 2022-10-23 stsp
615 13b2bc37 2022-10-23 stsp while (err == NULL) {
616 13b2bc37 2022-10-23 stsp err = gotd_imsg_recv(&imsg, ibuf, 0);
617 13b2bc37 2022-10-23 stsp if (err) {
618 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_PRIVSEP_READ)
619 13b2bc37 2022-10-23 stsp err = NULL;
620 13b2bc37 2022-10-23 stsp break;
621 13b2bc37 2022-10-23 stsp }
622 13b2bc37 2022-10-23 stsp
623 13b2bc37 2022-10-23 stsp evtimer_del(&client->tmo);
624 13b2bc37 2022-10-23 stsp
625 13b2bc37 2022-10-23 stsp switch (imsg.hdr.type) {
626 f1752522 2022-10-29 stsp case GOTD_IMSG_INFO:
627 f1752522 2022-10-29 stsp err = send_info(client);
628 f1752522 2022-10-29 stsp break;
629 f1752522 2022-10-29 stsp case GOTD_IMSG_STOP:
630 f1752522 2022-10-29 stsp err = stop_gotd(client);
631 f1752522 2022-10-29 stsp break;
632 13b2bc37 2022-10-23 stsp case GOTD_IMSG_LIST_REFS:
633 ae7c1b78 2023-01-10 stsp err = start_client_authentication(client, &imsg);
634 13b2bc37 2022-10-23 stsp break;
635 13b2bc37 2022-10-23 stsp default:
636 ae7c1b78 2023-01-10 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
637 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
638 13b2bc37 2022-10-23 stsp break;
639 13b2bc37 2022-10-23 stsp }
640 13b2bc37 2022-10-23 stsp
641 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
642 13b2bc37 2022-10-23 stsp }
643 13b2bc37 2022-10-23 stsp
644 13b2bc37 2022-10-23 stsp if (err) {
645 b5225f29 2023-01-22 op disconnect_on_error(client, err);
646 13b2bc37 2022-10-23 stsp } else {
647 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(&client->iev);
648 13b2bc37 2022-10-23 stsp }
649 13b2bc37 2022-10-23 stsp }
650 13b2bc37 2022-10-23 stsp
651 13b2bc37 2022-10-23 stsp static void
652 ae7c1b78 2023-01-10 stsp gotd_auth_timeout(int fd, short events, void *arg)
653 13b2bc37 2022-10-23 stsp {
654 13b2bc37 2022-10-23 stsp struct gotd_client *client = arg;
655 13b2bc37 2022-10-23 stsp
656 ae7c1b78 2023-01-10 stsp log_debug("disconnecting uid %d due to authentication timeout",
657 ae7c1b78 2023-01-10 stsp client->euid);
658 13b2bc37 2022-10-23 stsp disconnect(client);
659 13b2bc37 2022-10-23 stsp }
660 13b2bc37 2022-10-23 stsp
661 d93ecf7d 2022-12-14 stsp static const struct got_error *
662 d93ecf7d 2022-12-14 stsp recv_connect(uint32_t *client_id, struct imsg *imsg)
663 13b2bc37 2022-10-23 stsp {
664 d93ecf7d 2022-12-14 stsp const struct got_error *err = NULL;
665 d93ecf7d 2022-12-14 stsp struct gotd_imsg_connect iconnect;
666 d93ecf7d 2022-12-14 stsp size_t datalen;
667 13b2bc37 2022-10-23 stsp int s = -1;
668 13b2bc37 2022-10-23 stsp struct gotd_client *client = NULL;
669 13b2bc37 2022-10-23 stsp
670 d93ecf7d 2022-12-14 stsp *client_id = 0;
671 13b2bc37 2022-10-23 stsp
672 d93ecf7d 2022-12-14 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
673 d93ecf7d 2022-12-14 stsp if (datalen != sizeof(iconnect))
674 d93ecf7d 2022-12-14 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
675 d93ecf7d 2022-12-14 stsp memcpy(&iconnect, imsg->data, sizeof(iconnect));
676 13b2bc37 2022-10-23 stsp
677 2c52c623 2024-01-30 op s = imsg_get_fd(imsg);
678 13b2bc37 2022-10-23 stsp if (s == -1) {
679 d93ecf7d 2022-12-14 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
680 d93ecf7d 2022-12-14 stsp goto done;
681 13b2bc37 2022-10-23 stsp }
682 13b2bc37 2022-10-23 stsp
683 d93ecf7d 2022-12-14 stsp if (find_client(iconnect.client_id)) {
684 d93ecf7d 2022-12-14 stsp err = got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
685 d93ecf7d 2022-12-14 stsp goto done;
686 d93ecf7d 2022-12-14 stsp }
687 13b2bc37 2022-10-23 stsp
688 13b2bc37 2022-10-23 stsp client = calloc(1, sizeof(*client));
689 13b2bc37 2022-10-23 stsp if (client == NULL) {
690 d93ecf7d 2022-12-14 stsp err = got_error_from_errno("calloc");
691 d93ecf7d 2022-12-14 stsp goto done;
692 13b2bc37 2022-10-23 stsp }
693 13b2bc37 2022-10-23 stsp
694 d93ecf7d 2022-12-14 stsp *client_id = iconnect.client_id;
695 d93ecf7d 2022-12-14 stsp
696 eac23c30 2023-01-10 stsp client->state = GOTD_CLIENT_STATE_NEW;
697 d93ecf7d 2022-12-14 stsp client->id = iconnect.client_id;
698 13b2bc37 2022-10-23 stsp client->fd = s;
699 13b2bc37 2022-10-23 stsp s = -1;
700 365cf0f3 2022-12-29 stsp /* The auth process will verify UID/GID for us. */
701 365cf0f3 2022-12-29 stsp client->euid = iconnect.euid;
702 365cf0f3 2022-12-29 stsp client->egid = iconnect.egid;
703 13b2bc37 2022-10-23 stsp
704 13b2bc37 2022-10-23 stsp imsg_init(&client->iev.ibuf, client->fd);
705 13b2bc37 2022-10-23 stsp client->iev.handler = gotd_request;
706 13b2bc37 2022-10-23 stsp client->iev.events = EV_READ;
707 13b2bc37 2022-10-23 stsp client->iev.handler_arg = client;
708 13b2bc37 2022-10-23 stsp
709 13b2bc37 2022-10-23 stsp event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
710 13b2bc37 2022-10-23 stsp &client->iev);
711 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(&client->iev);
712 13b2bc37 2022-10-23 stsp
713 ae7c1b78 2023-01-10 stsp evtimer_set(&client->tmo, gotd_auth_timeout, client);
714 13b2bc37 2022-10-23 stsp
715 13b2bc37 2022-10-23 stsp add_client(client);
716 13b2bc37 2022-10-23 stsp log_debug("%s: new client uid %d connected on fd %d", __func__,
717 13b2bc37 2022-10-23 stsp client->euid, client->fd);
718 d93ecf7d 2022-12-14 stsp done:
719 d93ecf7d 2022-12-14 stsp if (err) {
720 c929736a 2023-06-22 op struct gotd_child_proc *listen_proc = gotd.listen_proc;
721 d93ecf7d 2022-12-14 stsp struct gotd_imsg_disconnect idisconnect;
722 13b2bc37 2022-10-23 stsp
723 d93ecf7d 2022-12-14 stsp idisconnect.client_id = client->id;
724 d93ecf7d 2022-12-14 stsp if (gotd_imsg_compose_event(&listen_proc->iev,
725 d93ecf7d 2022-12-14 stsp GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
726 d93ecf7d 2022-12-14 stsp &idisconnect, sizeof(idisconnect)) == -1)
727 d93ecf7d 2022-12-14 stsp log_warn("imsg compose DISCONNECT");
728 d93ecf7d 2022-12-14 stsp
729 d93ecf7d 2022-12-14 stsp if (s != -1)
730 d93ecf7d 2022-12-14 stsp close(s);
731 d93ecf7d 2022-12-14 stsp }
732 d93ecf7d 2022-12-14 stsp
733 d93ecf7d 2022-12-14 stsp return err;
734 13b2bc37 2022-10-23 stsp }
735 13b2bc37 2022-10-23 stsp
736 13b2bc37 2022-10-23 stsp static const char *gotd_proc_names[PROC_MAX] = {
737 13b2bc37 2022-10-23 stsp "parent",
738 d93ecf7d 2022-12-14 stsp "listen",
739 5e25db14 2022-12-29 stsp "auth",
740 ce986f22 2023-06-19 stsp "session_read",
741 ce986f22 2023-06-19 stsp "session_write",
742 13b2bc37 2022-10-23 stsp "repo_read",
743 4b3827cd 2023-07-08 stsp "repo_write",
744 ba97b2d7 2024-03-20 stsp "gitwrapper",
745 ba97b2d7 2024-03-20 stsp "notify"
746 13b2bc37 2022-10-23 stsp };
747 13b2bc37 2022-10-23 stsp
748 13b2bc37 2022-10-23 stsp static void
749 13b2bc37 2022-10-23 stsp kill_proc(struct gotd_child_proc *proc, int fatal)
750 13b2bc37 2022-10-23 stsp {
751 839338f6 2023-06-22 op struct timeval tv = { 5, 0 };
752 839338f6 2023-06-22 op
753 839338f6 2023-06-22 op log_debug("kill -%d %d", fatal ? SIGKILL : SIGTERM, proc->pid);
754 839338f6 2023-06-22 op
755 839338f6 2023-06-22 op if (proc->iev.ibuf.fd != -1) {
756 839338f6 2023-06-22 op event_del(&proc->iev.ev);
757 839338f6 2023-06-22 op msgbuf_clear(&proc->iev.ibuf.w);
758 839338f6 2023-06-22 op close(proc->iev.ibuf.fd);
759 839338f6 2023-06-22 op proc->iev.ibuf.fd = -1;
760 839338f6 2023-06-22 op }
761 839338f6 2023-06-22 op
762 839338f6 2023-06-22 op if (!evtimer_pending(&proc->tmo, NULL) && !fatal)
763 839338f6 2023-06-22 op evtimer_add(&proc->tmo, &tv);
764 839338f6 2023-06-22 op
765 13b2bc37 2022-10-23 stsp if (fatal) {
766 13b2bc37 2022-10-23 stsp log_warnx("sending SIGKILL to PID %d", proc->pid);
767 13b2bc37 2022-10-23 stsp kill(proc->pid, SIGKILL);
768 13b2bc37 2022-10-23 stsp } else
769 13b2bc37 2022-10-23 stsp kill(proc->pid, SIGTERM);
770 13b2bc37 2022-10-23 stsp }
771 13b2bc37 2022-10-23 stsp
772 13b2bc37 2022-10-23 stsp static void
773 839338f6 2023-06-22 op kill_proc_timeout(int fd, short ev, void *d)
774 839338f6 2023-06-22 op {
775 839338f6 2023-06-22 op struct gotd_child_proc *proc = d;
776 839338f6 2023-06-22 op
777 839338f6 2023-06-22 op log_warnx("timeout waiting for PID %d to terminate;"
778 839338f6 2023-06-22 op " retrying with force", proc->pid);
779 839338f6 2023-06-22 op kill_proc(proc, 1);
780 839338f6 2023-06-22 op }
781 839338f6 2023-06-22 op
782 839338f6 2023-06-22 op static void
783 13b2bc37 2022-10-23 stsp gotd_shutdown(void)
784 13b2bc37 2022-10-23 stsp {
785 b50a2b46 2022-12-29 stsp uint64_t slot;
786 13b2bc37 2022-10-23 stsp
787 ae7c1b78 2023-01-10 stsp log_debug("shutting down");
788 b50a2b46 2022-12-29 stsp for (slot = 0; slot < nitems(gotd_clients); slot++) {
789 b50a2b46 2022-12-29 stsp struct gotd_client *c, *tmp;
790 b50a2b46 2022-12-29 stsp
791 b50a2b46 2022-12-29 stsp STAILQ_FOREACH_SAFE(c, &gotd_clients[slot], entry, tmp)
792 b50a2b46 2022-12-29 stsp disconnect(c);
793 13b2bc37 2022-10-23 stsp }
794 13b2bc37 2022-10-23 stsp
795 839338f6 2023-06-22 op kill_proc(gotd.listen_proc, 0);
796 13b2bc37 2022-10-23 stsp
797 13b2bc37 2022-10-23 stsp log_info("terminating");
798 13b2bc37 2022-10-23 stsp exit(0);
799 13b2bc37 2022-10-23 stsp }
800 13b2bc37 2022-10-23 stsp
801 839338f6 2023-06-22 op static struct gotd_child_proc *
802 839338f6 2023-06-22 op find_proc_by_pid(pid_t pid)
803 839338f6 2023-06-22 op {
804 839338f6 2023-06-22 op struct gotd_child_proc *proc = NULL;
805 839338f6 2023-06-22 op
806 839338f6 2023-06-22 op TAILQ_FOREACH(proc, &procs, entry)
807 839338f6 2023-06-22 op if (proc->pid == pid)
808 839338f6 2023-06-22 op break;
809 839338f6 2023-06-22 op
810 839338f6 2023-06-22 op return proc;
811 839338f6 2023-06-22 op }
812 839338f6 2023-06-22 op
813 13b2bc37 2022-10-23 stsp void
814 13b2bc37 2022-10-23 stsp gotd_sighdlr(int sig, short event, void *arg)
815 13b2bc37 2022-10-23 stsp {
816 839338f6 2023-06-22 op struct gotd_child_proc *proc;
817 839338f6 2023-06-22 op pid_t pid;
818 839338f6 2023-06-22 op int status;
819 839338f6 2023-06-22 op
820 13b2bc37 2022-10-23 stsp /*
821 13b2bc37 2022-10-23 stsp * Normal signal handler rules don't apply because libevent
822 13b2bc37 2022-10-23 stsp * decouples for us.
823 13b2bc37 2022-10-23 stsp */
824 13b2bc37 2022-10-23 stsp
825 13b2bc37 2022-10-23 stsp switch (sig) {
826 13b2bc37 2022-10-23 stsp case SIGHUP:
827 13b2bc37 2022-10-23 stsp log_info("%s: ignoring SIGHUP", __func__);
828 13b2bc37 2022-10-23 stsp break;
829 13b2bc37 2022-10-23 stsp case SIGUSR1:
830 13b2bc37 2022-10-23 stsp log_info("%s: ignoring SIGUSR1", __func__);
831 13b2bc37 2022-10-23 stsp break;
832 13b2bc37 2022-10-23 stsp case SIGTERM:
833 13b2bc37 2022-10-23 stsp case SIGINT:
834 13b2bc37 2022-10-23 stsp gotd_shutdown();
835 13b2bc37 2022-10-23 stsp break;
836 839338f6 2023-06-22 op case SIGCHLD:
837 839338f6 2023-06-22 op for (;;) {
838 839338f6 2023-06-22 op pid = waitpid(WAIT_ANY, &status, WNOHANG);
839 839338f6 2023-06-22 op if (pid == -1) {
840 839338f6 2023-06-22 op if (errno == EINTR)
841 839338f6 2023-06-22 op continue;
842 839338f6 2023-06-22 op if (errno == ECHILD)
843 839338f6 2023-06-22 op break;
844 839338f6 2023-06-22 op fatal("waitpid");
845 839338f6 2023-06-22 op }
846 839338f6 2023-06-22 op if (pid == 0)
847 839338f6 2023-06-22 op break;
848 839338f6 2023-06-22 op
849 839338f6 2023-06-22 op log_debug("reaped pid %d", pid);
850 839338f6 2023-06-22 op proc = find_proc_by_pid(pid);
851 839338f6 2023-06-22 op if (proc == NULL) {
852 839338f6 2023-06-22 op log_info("caught exit of unknown child %d",
853 839338f6 2023-06-22 op pid);
854 839338f6 2023-06-22 op continue;
855 839338f6 2023-06-22 op }
856 839338f6 2023-06-22 op
857 839338f6 2023-06-22 op if (WIFSIGNALED(status)) {
858 839338f6 2023-06-22 op log_warnx("child PID %d terminated with"
859 839338f6 2023-06-22 op " signal %d", pid, WTERMSIG(status));
860 839338f6 2023-06-22 op }
861 839338f6 2023-06-22 op
862 839338f6 2023-06-22 op proc_done(proc);
863 839338f6 2023-06-22 op }
864 839338f6 2023-06-22 op break;
865 13b2bc37 2022-10-23 stsp default:
866 13b2bc37 2022-10-23 stsp fatalx("unexpected signal");
867 13b2bc37 2022-10-23 stsp }
868 13b2bc37 2022-10-23 stsp }
869 13b2bc37 2022-10-23 stsp
870 13b2bc37 2022-10-23 stsp static const struct got_error *
871 13b2bc37 2022-10-23 stsp ensure_proc_is_reading(struct gotd_client *client,
872 13b2bc37 2022-10-23 stsp struct gotd_child_proc *proc)
873 13b2bc37 2022-10-23 stsp {
874 13b2bc37 2022-10-23 stsp if (!client_is_reading(client)) {
875 13b2bc37 2022-10-23 stsp kill_proc(proc, 1);
876 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_BAD_PACKET,
877 13b2bc37 2022-10-23 stsp "PID %d handled a read-request for uid %d but this "
878 13b2bc37 2022-10-23 stsp "user is not reading from a repository", proc->pid,
879 13b2bc37 2022-10-23 stsp client->euid);
880 13b2bc37 2022-10-23 stsp }
881 13b2bc37 2022-10-23 stsp
882 13b2bc37 2022-10-23 stsp return NULL;
883 13b2bc37 2022-10-23 stsp }
884 13b2bc37 2022-10-23 stsp
885 13b2bc37 2022-10-23 stsp static const struct got_error *
886 13b2bc37 2022-10-23 stsp ensure_proc_is_writing(struct gotd_client *client,
887 13b2bc37 2022-10-23 stsp struct gotd_child_proc *proc)
888 13b2bc37 2022-10-23 stsp {
889 13b2bc37 2022-10-23 stsp if (!client_is_writing(client)) {
890 13b2bc37 2022-10-23 stsp kill_proc(proc, 1);
891 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_BAD_PACKET,
892 13b2bc37 2022-10-23 stsp "PID %d handled a write-request for uid %d but this "
893 13b2bc37 2022-10-23 stsp "user is not writing to a repository", proc->pid,
894 13b2bc37 2022-10-23 stsp client->euid);
895 13b2bc37 2022-10-23 stsp }
896 13b2bc37 2022-10-23 stsp
897 13b2bc37 2022-10-23 stsp return NULL;
898 13b2bc37 2022-10-23 stsp }
899 13b2bc37 2022-10-23 stsp
900 13b2bc37 2022-10-23 stsp static int
901 13b2bc37 2022-10-23 stsp verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
902 13b2bc37 2022-10-23 stsp struct imsg *imsg)
903 13b2bc37 2022-10-23 stsp {
904 13b2bc37 2022-10-23 stsp const struct got_error *err;
905 13b2bc37 2022-10-23 stsp int ret = 0;
906 13b2bc37 2022-10-23 stsp
907 d93ecf7d 2022-12-14 stsp if (proc->type == PROC_REPO_READ || proc->type == PROC_REPO_WRITE) {
908 f7a854cf 2023-01-10 stsp if (client->repo == NULL)
909 d93ecf7d 2022-12-14 stsp fatalx("no process found for uid %d", client->euid);
910 f7a854cf 2023-01-10 stsp if (proc->pid != client->repo->pid) {
911 d93ecf7d 2022-12-14 stsp kill_proc(proc, 1);
912 d93ecf7d 2022-12-14 stsp log_warnx("received message from PID %d for uid %d, "
913 d93ecf7d 2022-12-14 stsp "while PID %d is the process serving this user",
914 f7a854cf 2023-01-10 stsp proc->pid, client->euid, client->repo->pid);
915 ae7c1b78 2023-01-10 stsp return 0;
916 ae7c1b78 2023-01-10 stsp }
917 ae7c1b78 2023-01-10 stsp }
918 b0614828 2023-06-19 stsp if (proc->type == PROC_SESSION_READ ||
919 b0614828 2023-06-19 stsp proc->type == PROC_SESSION_WRITE) {
920 ae7c1b78 2023-01-10 stsp if (client->session == NULL) {
921 ae7c1b78 2023-01-10 stsp log_warnx("no session found for uid %d", client->euid);
922 d93ecf7d 2022-12-14 stsp return 0;
923 d93ecf7d 2022-12-14 stsp }
924 ae7c1b78 2023-01-10 stsp if (proc->pid != client->session->pid) {
925 ae7c1b78 2023-01-10 stsp kill_proc(proc, 1);
926 ae7c1b78 2023-01-10 stsp log_warnx("received message from PID %d for uid %d, "
927 ae7c1b78 2023-01-10 stsp "while PID %d is the process serving this user",
928 ae7c1b78 2023-01-10 stsp proc->pid, client->euid, client->session->pid);
929 ae7c1b78 2023-01-10 stsp return 0;
930 ae7c1b78 2023-01-10 stsp }
931 13b2bc37 2022-10-23 stsp }
932 13b2bc37 2022-10-23 stsp
933 13b2bc37 2022-10-23 stsp switch (imsg->hdr.type) {
934 13b2bc37 2022-10-23 stsp case GOTD_IMSG_ERROR:
935 13b2bc37 2022-10-23 stsp ret = 1;
936 13b2bc37 2022-10-23 stsp break;
937 d93ecf7d 2022-12-14 stsp case GOTD_IMSG_CONNECT:
938 d93ecf7d 2022-12-14 stsp if (proc->type != PROC_LISTEN) {
939 d93ecf7d 2022-12-14 stsp err = got_error_fmt(GOT_ERR_BAD_PACKET,
940 d93ecf7d 2022-12-14 stsp "new connection for uid %d from PID %d "
941 d93ecf7d 2022-12-14 stsp "which is not the listen process",
942 76a9a38e 2023-12-27 stsp client->euid, proc->pid);
943 5e25db14 2022-12-29 stsp } else
944 5e25db14 2022-12-29 stsp ret = 1;
945 5e25db14 2022-12-29 stsp break;
946 5e25db14 2022-12-29 stsp case GOTD_IMSG_ACCESS_GRANTED:
947 5e25db14 2022-12-29 stsp if (proc->type != PROC_AUTH) {
948 5e25db14 2022-12-29 stsp err = got_error_fmt(GOT_ERR_BAD_PACKET,
949 5e25db14 2022-12-29 stsp "authentication of uid %d from PID %d "
950 5e25db14 2022-12-29 stsp "which is not the auth process",
951 76a9a38e 2023-12-27 stsp client->euid, proc->pid);
952 d93ecf7d 2022-12-14 stsp } else
953 d93ecf7d 2022-12-14 stsp ret = 1;
954 d93ecf7d 2022-12-14 stsp break;
955 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_CLIENT_SESSION_READY:
956 b0614828 2023-06-19 stsp if (proc->type != PROC_SESSION_READ &&
957 b0614828 2023-06-19 stsp proc->type != PROC_SESSION_WRITE) {
958 ae7c1b78 2023-01-10 stsp err = got_error_fmt(GOT_ERR_BAD_PACKET,
959 ae7c1b78 2023-01-10 stsp "unexpected \"ready\" signal from PID %d",
960 ae7c1b78 2023-01-10 stsp proc->pid);
961 ae7c1b78 2023-01-10 stsp } else
962 ae7c1b78 2023-01-10 stsp ret = 1;
963 ae7c1b78 2023-01-10 stsp break;
964 b50a2b46 2022-12-29 stsp case GOTD_IMSG_REPO_CHILD_READY:
965 b50a2b46 2022-12-29 stsp if (proc->type != PROC_REPO_READ &&
966 b50a2b46 2022-12-29 stsp proc->type != PROC_REPO_WRITE) {
967 b50a2b46 2022-12-29 stsp err = got_error_fmt(GOT_ERR_BAD_PACKET,
968 b50a2b46 2022-12-29 stsp "unexpected \"ready\" signal from PID %d",
969 b50a2b46 2022-12-29 stsp proc->pid);
970 b50a2b46 2022-12-29 stsp } else
971 b50a2b46 2022-12-29 stsp ret = 1;
972 b50a2b46 2022-12-29 stsp break;
973 13b2bc37 2022-10-23 stsp case GOTD_IMSG_PACKFILE_DONE:
974 13b2bc37 2022-10-23 stsp err = ensure_proc_is_reading(client, proc);
975 13b2bc37 2022-10-23 stsp if (err)
976 13b2bc37 2022-10-23 stsp log_warnx("uid %d: %s", client->euid, err->msg);
977 13b2bc37 2022-10-23 stsp else
978 13b2bc37 2022-10-23 stsp ret = 1;
979 13b2bc37 2022-10-23 stsp break;
980 13b2bc37 2022-10-23 stsp case GOTD_IMSG_PACKFILE_INSTALL:
981 13b2bc37 2022-10-23 stsp case GOTD_IMSG_REF_UPDATES_START:
982 13b2bc37 2022-10-23 stsp case GOTD_IMSG_REF_UPDATE:
983 13b2bc37 2022-10-23 stsp err = ensure_proc_is_writing(client, proc);
984 13b2bc37 2022-10-23 stsp if (err)
985 13b2bc37 2022-10-23 stsp log_warnx("uid %d: %s", client->euid, err->msg);
986 13b2bc37 2022-10-23 stsp else
987 13b2bc37 2022-10-23 stsp ret = 1;
988 13b2bc37 2022-10-23 stsp break;
989 13b2bc37 2022-10-23 stsp default:
990 13b2bc37 2022-10-23 stsp log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
991 13b2bc37 2022-10-23 stsp break;
992 13b2bc37 2022-10-23 stsp }
993 13b2bc37 2022-10-23 stsp
994 13b2bc37 2022-10-23 stsp return ret;
995 13b2bc37 2022-10-23 stsp }
996 13b2bc37 2022-10-23 stsp
997 13b2bc37 2022-10-23 stsp static const struct got_error *
998 ae7c1b78 2023-01-10 stsp connect_repo_child(struct gotd_client *client,
999 ae7c1b78 2023-01-10 stsp struct gotd_child_proc *repo_proc)
1000 b50a2b46 2022-12-29 stsp {
1001 b50a2b46 2022-12-29 stsp static const struct got_error *err;
1002 ae7c1b78 2023-01-10 stsp struct gotd_imsgev *session_iev = &client->session->iev;
1003 ae7c1b78 2023-01-10 stsp struct gotd_imsg_connect_repo_child ireq;
1004 ae7c1b78 2023-01-10 stsp int pipe[2];
1005 b50a2b46 2022-12-29 stsp
1006 eac23c30 2023-01-10 stsp if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED)
1007 ae7c1b78 2023-01-10 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
1008 ae7c1b78 2023-01-10 stsp "unexpected repo child ready signal received");
1009 b50a2b46 2022-12-29 stsp
1010 ae7c1b78 2023-01-10 stsp if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1011 ae7c1b78 2023-01-10 stsp PF_UNSPEC, pipe) == -1)
1012 ae7c1b78 2023-01-10 stsp fatal("socketpair");
1013 b50a2b46 2022-12-29 stsp
1014 ae7c1b78 2023-01-10 stsp memset(&ireq, 0, sizeof(ireq));
1015 ae7c1b78 2023-01-10 stsp ireq.proc_id = repo_proc->type;
1016 13b2bc37 2022-10-23 stsp
1017 ae7c1b78 2023-01-10 stsp /* Pass repo child pipe to session child process. */
1018 ae7c1b78 2023-01-10 stsp if (gotd_imsg_compose_event(session_iev, GOTD_IMSG_CONNECT_REPO_CHILD,
1019 ae7c1b78 2023-01-10 stsp PROC_GOTD, pipe[0], &ireq, sizeof(ireq)) == -1) {
1020 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
1021 ae7c1b78 2023-01-10 stsp close(pipe[0]);
1022 ae7c1b78 2023-01-10 stsp close(pipe[1]);
1023 ae7c1b78 2023-01-10 stsp return err;
1024 13b2bc37 2022-10-23 stsp }
1025 13b2bc37 2022-10-23 stsp
1026 ae7c1b78 2023-01-10 stsp /* Pass session child pipe to repo child process. */
1027 ae7c1b78 2023-01-10 stsp if (gotd_imsg_compose_event(&repo_proc->iev,
1028 ae7c1b78 2023-01-10 stsp GOTD_IMSG_CONNECT_REPO_CHILD, PROC_GOTD, pipe[1], NULL, 0) == -1) {
1029 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
1030 ae7c1b78 2023-01-10 stsp close(pipe[1]);
1031 ae7c1b78 2023-01-10 stsp return err;
1032 13b2bc37 2022-10-23 stsp }
1033 13b2bc37 2022-10-23 stsp
1034 13b2bc37 2022-10-23 stsp return NULL;
1035 13b2bc37 2022-10-23 stsp }
1036 13b2bc37 2022-10-23 stsp
1037 13b2bc37 2022-10-23 stsp static void
1038 b50a2b46 2022-12-29 stsp gotd_dispatch_listener(int fd, short event, void *arg)
1039 13b2bc37 2022-10-23 stsp {
1040 13b2bc37 2022-10-23 stsp struct gotd_imsgev *iev = arg;
1041 13b2bc37 2022-10-23 stsp struct imsgbuf *ibuf = &iev->ibuf;
1042 c929736a 2023-06-22 op struct gotd_child_proc *proc = gotd.listen_proc;
1043 b50a2b46 2022-12-29 stsp ssize_t n;
1044 b50a2b46 2022-12-29 stsp int shut = 0;
1045 b50a2b46 2022-12-29 stsp struct imsg imsg;
1046 b50a2b46 2022-12-29 stsp
1047 b50a2b46 2022-12-29 stsp if (proc->iev.ibuf.fd != fd)
1048 b50a2b46 2022-12-29 stsp fatalx("%s: unexpected fd %d", __func__, fd);
1049 b50a2b46 2022-12-29 stsp
1050 b50a2b46 2022-12-29 stsp if (event & EV_READ) {
1051 b50a2b46 2022-12-29 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1052 b50a2b46 2022-12-29 stsp fatal("imsg_read error");
1053 b50a2b46 2022-12-29 stsp if (n == 0) {
1054 b50a2b46 2022-12-29 stsp /* Connection closed. */
1055 b50a2b46 2022-12-29 stsp shut = 1;
1056 b50a2b46 2022-12-29 stsp goto done;
1057 b50a2b46 2022-12-29 stsp }
1058 b50a2b46 2022-12-29 stsp }
1059 b50a2b46 2022-12-29 stsp
1060 b50a2b46 2022-12-29 stsp if (event & EV_WRITE) {
1061 b50a2b46 2022-12-29 stsp n = msgbuf_write(&ibuf->w);
1062 b50a2b46 2022-12-29 stsp if (n == -1 && errno != EAGAIN)
1063 b50a2b46 2022-12-29 stsp fatal("msgbuf_write");
1064 b50a2b46 2022-12-29 stsp if (n == 0) {
1065 b50a2b46 2022-12-29 stsp /* Connection closed. */
1066 b50a2b46 2022-12-29 stsp shut = 1;
1067 b50a2b46 2022-12-29 stsp goto done;
1068 b50a2b46 2022-12-29 stsp }
1069 b50a2b46 2022-12-29 stsp }
1070 b50a2b46 2022-12-29 stsp
1071 b50a2b46 2022-12-29 stsp for (;;) {
1072 b50a2b46 2022-12-29 stsp const struct got_error *err = NULL;
1073 b50a2b46 2022-12-29 stsp struct gotd_client *client = NULL;
1074 b50a2b46 2022-12-29 stsp uint32_t client_id = 0;
1075 b50a2b46 2022-12-29 stsp int do_disconnect = 0;
1076 b50a2b46 2022-12-29 stsp
1077 b50a2b46 2022-12-29 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
1078 b50a2b46 2022-12-29 stsp fatal("%s: imsg_get error", __func__);
1079 b50a2b46 2022-12-29 stsp if (n == 0) /* No more messages. */
1080 b50a2b46 2022-12-29 stsp break;
1081 b50a2b46 2022-12-29 stsp
1082 b50a2b46 2022-12-29 stsp switch (imsg.hdr.type) {
1083 b50a2b46 2022-12-29 stsp case GOTD_IMSG_ERROR:
1084 b50a2b46 2022-12-29 stsp do_disconnect = 1;
1085 b50a2b46 2022-12-29 stsp err = gotd_imsg_recv_error(&client_id, &imsg);
1086 b50a2b46 2022-12-29 stsp break;
1087 b50a2b46 2022-12-29 stsp case GOTD_IMSG_CONNECT:
1088 b50a2b46 2022-12-29 stsp err = recv_connect(&client_id, &imsg);
1089 b50a2b46 2022-12-29 stsp break;
1090 b50a2b46 2022-12-29 stsp default:
1091 b50a2b46 2022-12-29 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1092 b50a2b46 2022-12-29 stsp break;
1093 b50a2b46 2022-12-29 stsp }
1094 b50a2b46 2022-12-29 stsp
1095 b50a2b46 2022-12-29 stsp client = find_client(client_id);
1096 b50a2b46 2022-12-29 stsp if (client == NULL) {
1097 b50a2b46 2022-12-29 stsp log_warnx("%s: client not found", __func__);
1098 b50a2b46 2022-12-29 stsp imsg_free(&imsg);
1099 b50a2b46 2022-12-29 stsp continue;
1100 b50a2b46 2022-12-29 stsp }
1101 b50a2b46 2022-12-29 stsp
1102 b50a2b46 2022-12-29 stsp if (err)
1103 b50a2b46 2022-12-29 stsp log_warnx("uid %d: %s", client->euid, err->msg);
1104 b50a2b46 2022-12-29 stsp
1105 b50a2b46 2022-12-29 stsp if (do_disconnect) {
1106 b50a2b46 2022-12-29 stsp if (err)
1107 b50a2b46 2022-12-29 stsp disconnect_on_error(client, err);
1108 b50a2b46 2022-12-29 stsp else
1109 b50a2b46 2022-12-29 stsp disconnect(client);
1110 b50a2b46 2022-12-29 stsp }
1111 b50a2b46 2022-12-29 stsp
1112 b50a2b46 2022-12-29 stsp imsg_free(&imsg);
1113 b50a2b46 2022-12-29 stsp }
1114 b50a2b46 2022-12-29 stsp done:
1115 b50a2b46 2022-12-29 stsp if (!shut) {
1116 b50a2b46 2022-12-29 stsp gotd_imsg_event_add(iev);
1117 b50a2b46 2022-12-29 stsp } else {
1118 b50a2b46 2022-12-29 stsp /* This pipe is dead. Remove its event handler */
1119 b50a2b46 2022-12-29 stsp event_del(&iev->ev);
1120 b50a2b46 2022-12-29 stsp event_loopexit(NULL);
1121 b50a2b46 2022-12-29 stsp }
1122 b50a2b46 2022-12-29 stsp }
1123 b50a2b46 2022-12-29 stsp
1124 b50a2b46 2022-12-29 stsp static void
1125 ba97b2d7 2024-03-20 stsp gotd_dispatch_notifier(int fd, short event, void *arg)
1126 ba97b2d7 2024-03-20 stsp {
1127 ba97b2d7 2024-03-20 stsp struct gotd_imsgev *iev = arg;
1128 ba97b2d7 2024-03-20 stsp struct imsgbuf *ibuf = &iev->ibuf;
1129 ba97b2d7 2024-03-20 stsp struct gotd_child_proc *proc = gotd.notify_proc;
1130 ba97b2d7 2024-03-20 stsp ssize_t n;
1131 ba97b2d7 2024-03-20 stsp int shut = 0;
1132 ba97b2d7 2024-03-20 stsp struct imsg imsg;
1133 ba97b2d7 2024-03-20 stsp
1134 ba97b2d7 2024-03-20 stsp if (proc->iev.ibuf.fd != fd)
1135 ba97b2d7 2024-03-20 stsp fatalx("%s: unexpected fd %d", __func__, fd);
1136 ba97b2d7 2024-03-20 stsp
1137 ba97b2d7 2024-03-20 stsp if (event & EV_READ) {
1138 ba97b2d7 2024-03-20 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1139 ba97b2d7 2024-03-20 stsp fatal("imsg_read error");
1140 ba97b2d7 2024-03-20 stsp if (n == 0) {
1141 ba97b2d7 2024-03-20 stsp /* Connection closed. */
1142 ba97b2d7 2024-03-20 stsp shut = 1;
1143 ba97b2d7 2024-03-20 stsp goto done;
1144 ba97b2d7 2024-03-20 stsp }
1145 ba97b2d7 2024-03-20 stsp }
1146 ba97b2d7 2024-03-20 stsp
1147 ba97b2d7 2024-03-20 stsp if (event & EV_WRITE) {
1148 ba97b2d7 2024-03-20 stsp n = msgbuf_write(&ibuf->w);
1149 ba97b2d7 2024-03-20 stsp if (n == -1 && errno != EAGAIN)
1150 ba97b2d7 2024-03-20 stsp fatal("msgbuf_write");
1151 ba97b2d7 2024-03-20 stsp if (n == 0) {
1152 ba97b2d7 2024-03-20 stsp /* Connection closed. */
1153 ba97b2d7 2024-03-20 stsp shut = 1;
1154 ba97b2d7 2024-03-20 stsp goto done;
1155 ba97b2d7 2024-03-20 stsp }
1156 ba97b2d7 2024-03-20 stsp }
1157 ba97b2d7 2024-03-20 stsp
1158 ba97b2d7 2024-03-20 stsp for (;;) {
1159 ba97b2d7 2024-03-20 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
1160 ba97b2d7 2024-03-20 stsp fatal("%s: imsg_get error", __func__);
1161 ba97b2d7 2024-03-20 stsp if (n == 0) /* No more messages. */
1162 ba97b2d7 2024-03-20 stsp break;
1163 ba97b2d7 2024-03-20 stsp
1164 ba97b2d7 2024-03-20 stsp switch (imsg.hdr.type) {
1165 ba97b2d7 2024-03-20 stsp default:
1166 ba97b2d7 2024-03-20 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1167 ba97b2d7 2024-03-20 stsp break;
1168 ba97b2d7 2024-03-20 stsp }
1169 ba97b2d7 2024-03-20 stsp
1170 ba97b2d7 2024-03-20 stsp imsg_free(&imsg);
1171 ba97b2d7 2024-03-20 stsp }
1172 ba97b2d7 2024-03-20 stsp done:
1173 ba97b2d7 2024-03-20 stsp if (!shut) {
1174 ba97b2d7 2024-03-20 stsp gotd_imsg_event_add(iev);
1175 ba97b2d7 2024-03-20 stsp } else {
1176 ba97b2d7 2024-03-20 stsp /* This pipe is dead. Remove its event handler */
1177 ba97b2d7 2024-03-20 stsp event_del(&iev->ev);
1178 ba97b2d7 2024-03-20 stsp
1179 ba97b2d7 2024-03-20 stsp /*
1180 ba97b2d7 2024-03-20 stsp * Do not exit all of gotd if the notification handler dies.
1181 ba97b2d7 2024-03-20 stsp * We can continue operating without notifications until an
1182 ba97b2d7 2024-03-20 stsp * operator intervenes.
1183 ba97b2d7 2024-03-20 stsp */
1184 ba97b2d7 2024-03-20 stsp log_warnx("notify child process (pid %d) closed its imsg pipe "
1185 ba97b2d7 2024-03-20 stsp "unexpectedly", proc->pid);
1186 ba97b2d7 2024-03-20 stsp proc_done(proc);
1187 ba97b2d7 2024-03-20 stsp }
1188 ba97b2d7 2024-03-20 stsp }
1189 ba97b2d7 2024-03-20 stsp
1190 ba97b2d7 2024-03-20 stsp static void
1191 5e25db14 2022-12-29 stsp gotd_dispatch_auth_child(int fd, short event, void *arg)
1192 5e25db14 2022-12-29 stsp {
1193 5e25db14 2022-12-29 stsp const struct got_error *err = NULL;
1194 5e25db14 2022-12-29 stsp struct gotd_imsgev *iev = arg;
1195 5e25db14 2022-12-29 stsp struct imsgbuf *ibuf = &iev->ibuf;
1196 5e25db14 2022-12-29 stsp struct gotd_client *client;
1197 5e25db14 2022-12-29 stsp struct gotd_repo *repo = NULL;
1198 5e25db14 2022-12-29 stsp ssize_t n;
1199 5e25db14 2022-12-29 stsp int shut = 0;
1200 5e25db14 2022-12-29 stsp struct imsg imsg;
1201 5e25db14 2022-12-29 stsp uint32_t client_id = 0;
1202 5e25db14 2022-12-29 stsp int do_disconnect = 0;
1203 56624d2b 2023-12-27 stsp size_t datalen;
1204 5e25db14 2022-12-29 stsp
1205 5e25db14 2022-12-29 stsp client = find_client_by_proc_fd(fd);
1206 ae0cca99 2023-02-09 stsp if (client == NULL) {
1207 ae0cca99 2023-02-09 stsp /* Can happen during process teardown. */
1208 ae0cca99 2023-02-09 stsp warnx("cannot find client for fd %d", fd);
1209 ae0cca99 2023-02-09 stsp shut = 1;
1210 ae0cca99 2023-02-09 stsp goto done;
1211 ae0cca99 2023-02-09 stsp }
1212 5e25db14 2022-12-29 stsp
1213 5e25db14 2022-12-29 stsp if (client->auth == NULL)
1214 5e25db14 2022-12-29 stsp fatalx("cannot find auth child process for fd %d", fd);
1215 5e25db14 2022-12-29 stsp
1216 5e25db14 2022-12-29 stsp if (event & EV_READ) {
1217 5e25db14 2022-12-29 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1218 5e25db14 2022-12-29 stsp fatal("imsg_read error");
1219 5e25db14 2022-12-29 stsp if (n == 0) {
1220 5e25db14 2022-12-29 stsp /* Connection closed. */
1221 5e25db14 2022-12-29 stsp shut = 1;
1222 5e25db14 2022-12-29 stsp goto done;
1223 5e25db14 2022-12-29 stsp }
1224 5e25db14 2022-12-29 stsp }
1225 5e25db14 2022-12-29 stsp
1226 5e25db14 2022-12-29 stsp if (event & EV_WRITE) {
1227 5e25db14 2022-12-29 stsp n = msgbuf_write(&ibuf->w);
1228 5e25db14 2022-12-29 stsp if (n == -1 && errno != EAGAIN)
1229 5e25db14 2022-12-29 stsp fatal("msgbuf_write");
1230 5e25db14 2022-12-29 stsp if (n == 0) {
1231 5e25db14 2022-12-29 stsp /* Connection closed. */
1232 5e25db14 2022-12-29 stsp shut = 1;
1233 5e25db14 2022-12-29 stsp }
1234 5e25db14 2022-12-29 stsp goto done;
1235 5e25db14 2022-12-29 stsp }
1236 5e25db14 2022-12-29 stsp
1237 5e25db14 2022-12-29 stsp if (client->auth->iev.ibuf.fd != fd)
1238 5e25db14 2022-12-29 stsp fatalx("%s: unexpected fd %d", __func__, fd);
1239 5e25db14 2022-12-29 stsp
1240 5e25db14 2022-12-29 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
1241 5e25db14 2022-12-29 stsp fatal("%s: imsg_get error", __func__);
1242 5e25db14 2022-12-29 stsp if (n == 0) /* No more messages. */
1243 5e25db14 2022-12-29 stsp return;
1244 5e25db14 2022-12-29 stsp
1245 5e25db14 2022-12-29 stsp evtimer_del(&client->tmo);
1246 5e25db14 2022-12-29 stsp
1247 56624d2b 2023-12-27 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1248 56624d2b 2023-12-27 stsp
1249 5e25db14 2022-12-29 stsp switch (imsg.hdr.type) {
1250 5e25db14 2022-12-29 stsp case GOTD_IMSG_ERROR:
1251 5e25db14 2022-12-29 stsp do_disconnect = 1;
1252 5e25db14 2022-12-29 stsp err = gotd_imsg_recv_error(&client_id, &imsg);
1253 5e25db14 2022-12-29 stsp break;
1254 5e25db14 2022-12-29 stsp case GOTD_IMSG_ACCESS_GRANTED:
1255 56624d2b 2023-12-27 stsp if (client->state != GOTD_CLIENT_STATE_NEW) {
1256 56624d2b 2023-12-27 stsp do_disconnect = 1;
1257 56624d2b 2023-12-27 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1258 56624d2b 2023-12-27 stsp }
1259 5e25db14 2022-12-29 stsp break;
1260 5e25db14 2022-12-29 stsp default:
1261 5e25db14 2022-12-29 stsp do_disconnect = 1;
1262 5e25db14 2022-12-29 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1263 5e25db14 2022-12-29 stsp break;
1264 5e25db14 2022-12-29 stsp }
1265 5e25db14 2022-12-29 stsp
1266 5e25db14 2022-12-29 stsp if (!verify_imsg_src(client, client->auth, &imsg)) {
1267 5e25db14 2022-12-29 stsp do_disconnect = 1;
1268 5e25db14 2022-12-29 stsp log_debug("dropping imsg type %d from PID %d",
1269 5e25db14 2022-12-29 stsp imsg.hdr.type, client->auth->pid);
1270 5e25db14 2022-12-29 stsp }
1271 5e25db14 2022-12-29 stsp
1272 5e25db14 2022-12-29 stsp if (do_disconnect) {
1273 5e25db14 2022-12-29 stsp if (err)
1274 5e25db14 2022-12-29 stsp disconnect_on_error(client, err);
1275 5e25db14 2022-12-29 stsp else
1276 5e25db14 2022-12-29 stsp disconnect(client);
1277 56624d2b 2023-12-27 stsp imsg_free(&imsg);
1278 c000aa35 2023-05-01 mark return;
1279 56624d2b 2023-12-27 stsp }
1280 56624d2b 2023-12-27 stsp
1281 56624d2b 2023-12-27 stsp client->state = GOTD_CLIENT_STATE_ACCESS_GRANTED;
1282 56624d2b 2023-12-27 stsp if (datalen > 0)
1283 56624d2b 2023-12-27 stsp client->username = strndup(imsg.data, datalen);
1284 56624d2b 2023-12-27 stsp imsg_free(&imsg);
1285 56624d2b 2023-12-27 stsp if (client->username == NULL &&
1286 56624d2b 2023-12-27 stsp asprintf(&client->username, "uid %d", client->euid) == -1) {
1287 56624d2b 2023-12-27 stsp err = got_error_from_errno("asprintf");
1288 56624d2b 2023-12-27 stsp goto done;
1289 5e25db14 2022-12-29 stsp }
1290 5e25db14 2022-12-29 stsp
1291 ba97b2d7 2024-03-20 stsp repo = gotd_find_repo_by_name(client->auth->repo_name, &gotd.repos);
1292 5e25db14 2022-12-29 stsp if (repo == NULL) {
1293 5e25db14 2022-12-29 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
1294 5e25db14 2022-12-29 stsp goto done;
1295 5e25db14 2022-12-29 stsp }
1296 5e25db14 2022-12-29 stsp kill_auth_proc(client);
1297 5e25db14 2022-12-29 stsp
1298 56624d2b 2023-12-27 stsp log_info("authenticated %s for repository %s",
1299 56624d2b 2023-12-27 stsp client->username, repo->name);
1300 5e25db14 2022-12-29 stsp
1301 ae7c1b78 2023-01-10 stsp err = start_session_child(client, repo, gotd.argv0,
1302 7fdc3e58 2022-12-30 mark gotd.confpath, gotd.daemonize, gotd.verbosity);
1303 ae7c1b78 2023-01-10 stsp if (err)
1304 ae7c1b78 2023-01-10 stsp goto done;
1305 5e25db14 2022-12-29 stsp done:
1306 5e25db14 2022-12-29 stsp if (err)
1307 5e25db14 2022-12-29 stsp log_warnx("uid %d: %s", client->euid, err->msg);
1308 5e25db14 2022-12-29 stsp
1309 5e25db14 2022-12-29 stsp /* We might have killed the auth process by now. */
1310 5e25db14 2022-12-29 stsp if (client->auth != NULL) {
1311 5e25db14 2022-12-29 stsp if (!shut) {
1312 5e25db14 2022-12-29 stsp gotd_imsg_event_add(iev);
1313 5e25db14 2022-12-29 stsp } else {
1314 5e25db14 2022-12-29 stsp /* This pipe is dead. Remove its event handler */
1315 5e25db14 2022-12-29 stsp event_del(&iev->ev);
1316 5e25db14 2022-12-29 stsp }
1317 5e25db14 2022-12-29 stsp }
1318 5e25db14 2022-12-29 stsp }
1319 5e25db14 2022-12-29 stsp
1320 ae7c1b78 2023-01-10 stsp static const struct got_error *
1321 ae7c1b78 2023-01-10 stsp connect_session(struct gotd_client *client)
1322 ae7c1b78 2023-01-10 stsp {
1323 ae7c1b78 2023-01-10 stsp const struct got_error *err = NULL;
1324 ae7c1b78 2023-01-10 stsp struct gotd_imsg_connect iconnect;
1325 ae7c1b78 2023-01-10 stsp int s;
1326 ba97b2d7 2024-03-20 stsp struct ibuf *wbuf;
1327 ae7c1b78 2023-01-10 stsp
1328 ae7c1b78 2023-01-10 stsp memset(&iconnect, 0, sizeof(iconnect));
1329 ae7c1b78 2023-01-10 stsp
1330 ae7c1b78 2023-01-10 stsp s = dup(client->fd);
1331 ae7c1b78 2023-01-10 stsp if (s == -1)
1332 ae7c1b78 2023-01-10 stsp return got_error_from_errno("dup");
1333 ae7c1b78 2023-01-10 stsp
1334 ae7c1b78 2023-01-10 stsp iconnect.client_id = client->id;
1335 ae7c1b78 2023-01-10 stsp iconnect.euid = client->euid;
1336 ae7c1b78 2023-01-10 stsp iconnect.egid = client->egid;
1337 ba97b2d7 2024-03-20 stsp iconnect.username_len = strlen(client->username);
1338 ae7c1b78 2023-01-10 stsp
1339 ba97b2d7 2024-03-20 stsp wbuf = imsg_create(&client->session->iev.ibuf, GOTD_IMSG_CONNECT,
1340 ba97b2d7 2024-03-20 stsp PROC_GOTD, gotd.pid, sizeof(iconnect) + iconnect.username_len);
1341 ba97b2d7 2024-03-20 stsp if (wbuf == NULL) {
1342 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("imsg compose CONNECT");
1343 ae7c1b78 2023-01-10 stsp close(s);
1344 ae7c1b78 2023-01-10 stsp return err;
1345 ba97b2d7 2024-03-20 stsp }
1346 ba97b2d7 2024-03-20 stsp if (imsg_add(wbuf, &iconnect, sizeof(iconnect)) == -1) {
1347 ba97b2d7 2024-03-20 stsp close(s);
1348 ba97b2d7 2024-03-20 stsp return got_error_from_errno("imsg_add CONNECT");
1349 ba97b2d7 2024-03-20 stsp }
1350 ba97b2d7 2024-03-20 stsp if (imsg_add(wbuf, client->username, iconnect.username_len) == -1) {
1351 ba97b2d7 2024-03-20 stsp close(s);
1352 ba97b2d7 2024-03-20 stsp return got_error_from_errno("imsg_add CONNECT");
1353 ae7c1b78 2023-01-10 stsp }
1354 ae7c1b78 2023-01-10 stsp
1355 ba97b2d7 2024-03-20 stsp ibuf_fd_set(wbuf, s);
1356 ba97b2d7 2024-03-20 stsp imsg_close(&client->session->iev.ibuf, wbuf);
1357 ba97b2d7 2024-03-20 stsp gotd_imsg_event_add(&client->session->iev);
1358 ba97b2d7 2024-03-20 stsp
1359 ae7c1b78 2023-01-10 stsp /*
1360 ae7c1b78 2023-01-10 stsp * We are no longer interested in messages from this client.
1361 ae7c1b78 2023-01-10 stsp * Further client requests will be handled by the session process.
1362 ae7c1b78 2023-01-10 stsp */
1363 ae7c1b78 2023-01-10 stsp msgbuf_clear(&client->iev.ibuf.w);
1364 ae7c1b78 2023-01-10 stsp imsg_clear(&client->iev.ibuf);
1365 ae7c1b78 2023-01-10 stsp event_del(&client->iev.ev);
1366 ae7c1b78 2023-01-10 stsp client->fd = -1; /* will be closed via copy in client->iev.ibuf.fd */
1367 ae7c1b78 2023-01-10 stsp
1368 ae7c1b78 2023-01-10 stsp return NULL;
1369 ae7c1b78 2023-01-10 stsp }
1370 ae7c1b78 2023-01-10 stsp
1371 5e25db14 2022-12-29 stsp static void
1372 ae7c1b78 2023-01-10 stsp gotd_dispatch_client_session(int fd, short event, void *arg)
1373 b50a2b46 2022-12-29 stsp {
1374 b50a2b46 2022-12-29 stsp struct gotd_imsgev *iev = arg;
1375 b50a2b46 2022-12-29 stsp struct imsgbuf *ibuf = &iev->ibuf;
1376 13b2bc37 2022-10-23 stsp struct gotd_child_proc *proc = NULL;
1377 b50a2b46 2022-12-29 stsp struct gotd_client *client = NULL;
1378 13b2bc37 2022-10-23 stsp ssize_t n;
1379 13b2bc37 2022-10-23 stsp int shut = 0;
1380 13b2bc37 2022-10-23 stsp struct imsg imsg;
1381 13b2bc37 2022-10-23 stsp
1382 ae7c1b78 2023-01-10 stsp client = find_client_by_proc_fd(fd);
1383 ae0cca99 2023-02-09 stsp if (client == NULL) {
1384 ae0cca99 2023-02-09 stsp /* Can happen during process teardown. */
1385 ae0cca99 2023-02-09 stsp warnx("cannot find client for fd %d", fd);
1386 ae0cca99 2023-02-09 stsp shut = 1;
1387 ae0cca99 2023-02-09 stsp goto done;
1388 ae0cca99 2023-02-09 stsp }
1389 ae7c1b78 2023-01-10 stsp
1390 13b2bc37 2022-10-23 stsp if (event & EV_READ) {
1391 13b2bc37 2022-10-23 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1392 13b2bc37 2022-10-23 stsp fatal("imsg_read error");
1393 13b2bc37 2022-10-23 stsp if (n == 0) {
1394 13b2bc37 2022-10-23 stsp /* Connection closed. */
1395 13b2bc37 2022-10-23 stsp shut = 1;
1396 13b2bc37 2022-10-23 stsp goto done;
1397 13b2bc37 2022-10-23 stsp }
1398 13b2bc37 2022-10-23 stsp }
1399 13b2bc37 2022-10-23 stsp
1400 13b2bc37 2022-10-23 stsp if (event & EV_WRITE) {
1401 13b2bc37 2022-10-23 stsp n = msgbuf_write(&ibuf->w);
1402 13b2bc37 2022-10-23 stsp if (n == -1 && errno != EAGAIN)
1403 13b2bc37 2022-10-23 stsp fatal("msgbuf_write");
1404 13b2bc37 2022-10-23 stsp if (n == 0) {
1405 13b2bc37 2022-10-23 stsp /* Connection closed. */
1406 13b2bc37 2022-10-23 stsp shut = 1;
1407 13b2bc37 2022-10-23 stsp goto done;
1408 ae7c1b78 2023-01-10 stsp }
1409 ae7c1b78 2023-01-10 stsp }
1410 ae7c1b78 2023-01-10 stsp
1411 ae7c1b78 2023-01-10 stsp proc = client->session;
1412 ae7c1b78 2023-01-10 stsp if (proc == NULL)
1413 ae7c1b78 2023-01-10 stsp fatalx("cannot find session child process for fd %d", fd);
1414 ae7c1b78 2023-01-10 stsp
1415 ae7c1b78 2023-01-10 stsp for (;;) {
1416 ae7c1b78 2023-01-10 stsp const struct got_error *err = NULL;
1417 ae7c1b78 2023-01-10 stsp uint32_t client_id = 0;
1418 ae7c1b78 2023-01-10 stsp int do_disconnect = 0, do_start_repo_child = 0;
1419 ae7c1b78 2023-01-10 stsp
1420 ae7c1b78 2023-01-10 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
1421 ae7c1b78 2023-01-10 stsp fatal("%s: imsg_get error", __func__);
1422 ae7c1b78 2023-01-10 stsp if (n == 0) /* No more messages. */
1423 ae7c1b78 2023-01-10 stsp break;
1424 ae7c1b78 2023-01-10 stsp
1425 ae7c1b78 2023-01-10 stsp switch (imsg.hdr.type) {
1426 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_ERROR:
1427 ae7c1b78 2023-01-10 stsp do_disconnect = 1;
1428 ae7c1b78 2023-01-10 stsp err = gotd_imsg_recv_error(&client_id, &imsg);
1429 ae7c1b78 2023-01-10 stsp break;
1430 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_CLIENT_SESSION_READY:
1431 eac23c30 2023-01-10 stsp if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED) {
1432 ae7c1b78 2023-01-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1433 ae7c1b78 2023-01-10 stsp break;
1434 ae7c1b78 2023-01-10 stsp }
1435 ae7c1b78 2023-01-10 stsp do_start_repo_child = 1;
1436 ae7c1b78 2023-01-10 stsp break;
1437 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_DISCONNECT:
1438 ae7c1b78 2023-01-10 stsp do_disconnect = 1;
1439 ae7c1b78 2023-01-10 stsp break;
1440 ae7c1b78 2023-01-10 stsp default:
1441 ae7c1b78 2023-01-10 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1442 ae7c1b78 2023-01-10 stsp break;
1443 13b2bc37 2022-10-23 stsp }
1444 ae7c1b78 2023-01-10 stsp
1445 ae7c1b78 2023-01-10 stsp if (!verify_imsg_src(client, proc, &imsg)) {
1446 ae7c1b78 2023-01-10 stsp log_debug("dropping imsg type %d from PID %d",
1447 ae7c1b78 2023-01-10 stsp imsg.hdr.type, proc->pid);
1448 ae7c1b78 2023-01-10 stsp imsg_free(&imsg);
1449 ae7c1b78 2023-01-10 stsp continue;
1450 ae7c1b78 2023-01-10 stsp }
1451 ae7c1b78 2023-01-10 stsp if (err)
1452 ae7c1b78 2023-01-10 stsp log_warnx("uid %d: %s", client->euid, err->msg);
1453 ae7c1b78 2023-01-10 stsp
1454 ae7c1b78 2023-01-10 stsp if (do_start_repo_child) {
1455 ae7c1b78 2023-01-10 stsp struct gotd_repo *repo;
1456 b09c1279 2023-03-28 stsp const char *name = client->session->repo_name;
1457 ae7c1b78 2023-01-10 stsp
1458 ba97b2d7 2024-03-20 stsp repo = gotd_find_repo_by_name(name, &gotd.repos);
1459 ae7c1b78 2023-01-10 stsp if (repo != NULL) {
1460 ae7c1b78 2023-01-10 stsp enum gotd_procid proc_type;
1461 ae7c1b78 2023-01-10 stsp
1462 ae7c1b78 2023-01-10 stsp if (client->required_auth & GOTD_AUTH_WRITE)
1463 ae7c1b78 2023-01-10 stsp proc_type = PROC_REPO_WRITE;
1464 ae7c1b78 2023-01-10 stsp else
1465 ae7c1b78 2023-01-10 stsp proc_type = PROC_REPO_READ;
1466 ae7c1b78 2023-01-10 stsp
1467 ae7c1b78 2023-01-10 stsp err = start_repo_child(client, proc_type, repo,
1468 ae7c1b78 2023-01-10 stsp gotd.argv0, gotd.confpath, gotd.daemonize,
1469 ae7c1b78 2023-01-10 stsp gotd.verbosity);
1470 ae7c1b78 2023-01-10 stsp } else
1471 ae7c1b78 2023-01-10 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
1472 ae7c1b78 2023-01-10 stsp
1473 ae7c1b78 2023-01-10 stsp if (err) {
1474 ae7c1b78 2023-01-10 stsp log_warnx("uid %d: %s", client->euid, err->msg);
1475 ae7c1b78 2023-01-10 stsp do_disconnect = 1;
1476 ae7c1b78 2023-01-10 stsp }
1477 ae7c1b78 2023-01-10 stsp }
1478 ae7c1b78 2023-01-10 stsp
1479 ae7c1b78 2023-01-10 stsp if (do_disconnect) {
1480 ae7c1b78 2023-01-10 stsp if (err)
1481 ae7c1b78 2023-01-10 stsp disconnect_on_error(client, err);
1482 ae7c1b78 2023-01-10 stsp else
1483 ae7c1b78 2023-01-10 stsp disconnect(client);
1484 ae7c1b78 2023-01-10 stsp }
1485 ae7c1b78 2023-01-10 stsp
1486 ae7c1b78 2023-01-10 stsp imsg_free(&imsg);
1487 13b2bc37 2022-10-23 stsp }
1488 ae7c1b78 2023-01-10 stsp done:
1489 ae7c1b78 2023-01-10 stsp if (!shut) {
1490 ae7c1b78 2023-01-10 stsp gotd_imsg_event_add(iev);
1491 ae7c1b78 2023-01-10 stsp } else {
1492 ae7c1b78 2023-01-10 stsp /* This pipe is dead. Remove its event handler */
1493 ae7c1b78 2023-01-10 stsp event_del(&iev->ev);
1494 ae7c1b78 2023-01-10 stsp disconnect(client);
1495 ba97b2d7 2024-03-20 stsp }
1496 ba97b2d7 2024-03-20 stsp }
1497 ba97b2d7 2024-03-20 stsp
1498 ba97b2d7 2024-03-20 stsp static const struct got_error *
1499 ba97b2d7 2024-03-20 stsp connect_notifier_and_session(struct gotd_client *client)
1500 ba97b2d7 2024-03-20 stsp {
1501 ba97b2d7 2024-03-20 stsp const struct got_error *err = NULL;
1502 ba97b2d7 2024-03-20 stsp struct gotd_imsgev *session_iev = &client->session->iev;
1503 ba97b2d7 2024-03-20 stsp int pipe[2];
1504 ba97b2d7 2024-03-20 stsp
1505 ba97b2d7 2024-03-20 stsp if (gotd.notify_proc == NULL)
1506 ba97b2d7 2024-03-20 stsp return NULL;
1507 ba97b2d7 2024-03-20 stsp
1508 ba97b2d7 2024-03-20 stsp if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1509 ba97b2d7 2024-03-20 stsp PF_UNSPEC, pipe) == -1)
1510 ba97b2d7 2024-03-20 stsp return got_error_from_errno("socketpair");
1511 ba97b2d7 2024-03-20 stsp
1512 ba97b2d7 2024-03-20 stsp /* Pass notifier pipe to session . */
1513 ba97b2d7 2024-03-20 stsp if (gotd_imsg_compose_event(session_iev, GOTD_IMSG_CONNECT_NOTIFIER,
1514 ba97b2d7 2024-03-20 stsp PROC_GOTD, pipe[0], NULL, 0) == -1) {
1515 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("imsg compose CONNECT_NOTIFIER");
1516 ba97b2d7 2024-03-20 stsp close(pipe[0]);
1517 ba97b2d7 2024-03-20 stsp close(pipe[1]);
1518 ba97b2d7 2024-03-20 stsp return err;
1519 ba97b2d7 2024-03-20 stsp }
1520 ba97b2d7 2024-03-20 stsp
1521 ba97b2d7 2024-03-20 stsp /* Pass session pipe to notifier. */
1522 ba97b2d7 2024-03-20 stsp if (gotd_imsg_compose_event(&gotd.notify_proc->iev,
1523 ba97b2d7 2024-03-20 stsp GOTD_IMSG_CONNECT_SESSION, PROC_GOTD, pipe[1], NULL, 0) == -1) {
1524 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("imsg compose CONNECT_SESSION");
1525 ba97b2d7 2024-03-20 stsp close(pipe[1]);
1526 ba97b2d7 2024-03-20 stsp return err;
1527 ae7c1b78 2023-01-10 stsp }
1528 ba97b2d7 2024-03-20 stsp
1529 ba97b2d7 2024-03-20 stsp return NULL;
1530 ae7c1b78 2023-01-10 stsp }
1531 13b2bc37 2022-10-23 stsp
1532 ae7c1b78 2023-01-10 stsp static void
1533 ae7c1b78 2023-01-10 stsp gotd_dispatch_repo_child(int fd, short event, void *arg)
1534 ae7c1b78 2023-01-10 stsp {
1535 ae7c1b78 2023-01-10 stsp struct gotd_imsgev *iev = arg;
1536 ae7c1b78 2023-01-10 stsp struct imsgbuf *ibuf = &iev->ibuf;
1537 ae7c1b78 2023-01-10 stsp struct gotd_child_proc *proc = NULL;
1538 ae7c1b78 2023-01-10 stsp struct gotd_client *client;
1539 ae7c1b78 2023-01-10 stsp ssize_t n;
1540 ae7c1b78 2023-01-10 stsp int shut = 0;
1541 ae7c1b78 2023-01-10 stsp struct imsg imsg;
1542 ae7c1b78 2023-01-10 stsp
1543 b50a2b46 2022-12-29 stsp client = find_client_by_proc_fd(fd);
1544 ae0cca99 2023-02-09 stsp if (client == NULL) {
1545 ae0cca99 2023-02-09 stsp /* Can happen during process teardown. */
1546 ae0cca99 2023-02-09 stsp warnx("cannot find client for fd %d", fd);
1547 ae0cca99 2023-02-09 stsp shut = 1;
1548 ae0cca99 2023-02-09 stsp goto done;
1549 ae0cca99 2023-02-09 stsp }
1550 b50a2b46 2022-12-29 stsp
1551 ae7c1b78 2023-01-10 stsp if (event & EV_READ) {
1552 ae7c1b78 2023-01-10 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1553 ae7c1b78 2023-01-10 stsp fatal("imsg_read error");
1554 ae7c1b78 2023-01-10 stsp if (n == 0) {
1555 ae7c1b78 2023-01-10 stsp /* Connection closed. */
1556 ae7c1b78 2023-01-10 stsp shut = 1;
1557 ae7c1b78 2023-01-10 stsp goto done;
1558 ae7c1b78 2023-01-10 stsp }
1559 ae7c1b78 2023-01-10 stsp }
1560 ae7c1b78 2023-01-10 stsp
1561 ae7c1b78 2023-01-10 stsp if (event & EV_WRITE) {
1562 ae7c1b78 2023-01-10 stsp n = msgbuf_write(&ibuf->w);
1563 ae7c1b78 2023-01-10 stsp if (n == -1 && errno != EAGAIN)
1564 ae7c1b78 2023-01-10 stsp fatal("msgbuf_write");
1565 ae7c1b78 2023-01-10 stsp if (n == 0) {
1566 ae7c1b78 2023-01-10 stsp /* Connection closed. */
1567 ae7c1b78 2023-01-10 stsp shut = 1;
1568 ae7c1b78 2023-01-10 stsp goto done;
1569 ae7c1b78 2023-01-10 stsp }
1570 ae7c1b78 2023-01-10 stsp }
1571 ae7c1b78 2023-01-10 stsp
1572 f7a854cf 2023-01-10 stsp proc = client->repo;
1573 13b2bc37 2022-10-23 stsp if (proc == NULL)
1574 13b2bc37 2022-10-23 stsp fatalx("cannot find child process for fd %d", fd);
1575 13b2bc37 2022-10-23 stsp
1576 13b2bc37 2022-10-23 stsp for (;;) {
1577 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
1578 13b2bc37 2022-10-23 stsp uint32_t client_id = 0;
1579 13b2bc37 2022-10-23 stsp int do_disconnect = 0;
1580 13b2bc37 2022-10-23 stsp
1581 13b2bc37 2022-10-23 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
1582 13b2bc37 2022-10-23 stsp fatal("%s: imsg_get error", __func__);
1583 13b2bc37 2022-10-23 stsp if (n == 0) /* No more messages. */
1584 13b2bc37 2022-10-23 stsp break;
1585 13b2bc37 2022-10-23 stsp
1586 13b2bc37 2022-10-23 stsp switch (imsg.hdr.type) {
1587 13b2bc37 2022-10-23 stsp case GOTD_IMSG_ERROR:
1588 13b2bc37 2022-10-23 stsp do_disconnect = 1;
1589 13b2bc37 2022-10-23 stsp err = gotd_imsg_recv_error(&client_id, &imsg);
1590 13b2bc37 2022-10-23 stsp break;
1591 b50a2b46 2022-12-29 stsp case GOTD_IMSG_REPO_CHILD_READY:
1592 ae7c1b78 2023-01-10 stsp err = connect_session(client);
1593 ae7c1b78 2023-01-10 stsp if (err)
1594 ae7c1b78 2023-01-10 stsp break;
1595 ba97b2d7 2024-03-20 stsp err = connect_notifier_and_session(client);
1596 ba97b2d7 2024-03-20 stsp if (err)
1597 ba97b2d7 2024-03-20 stsp break;
1598 ae7c1b78 2023-01-10 stsp err = connect_repo_child(client, proc);
1599 d93ecf7d 2022-12-14 stsp break;
1600 13b2bc37 2022-10-23 stsp default:
1601 13b2bc37 2022-10-23 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1602 13b2bc37 2022-10-23 stsp break;
1603 13b2bc37 2022-10-23 stsp }
1604 13b2bc37 2022-10-23 stsp
1605 13b2bc37 2022-10-23 stsp if (!verify_imsg_src(client, proc, &imsg)) {
1606 13b2bc37 2022-10-23 stsp log_debug("dropping imsg type %d from PID %d",
1607 13b2bc37 2022-10-23 stsp imsg.hdr.type, proc->pid);
1608 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
1609 13b2bc37 2022-10-23 stsp continue;
1610 13b2bc37 2022-10-23 stsp }
1611 13b2bc37 2022-10-23 stsp if (err)
1612 13b2bc37 2022-10-23 stsp log_warnx("uid %d: %s", client->euid, err->msg);
1613 13b2bc37 2022-10-23 stsp
1614 13b2bc37 2022-10-23 stsp if (do_disconnect) {
1615 13b2bc37 2022-10-23 stsp if (err)
1616 13b2bc37 2022-10-23 stsp disconnect_on_error(client, err);
1617 13b2bc37 2022-10-23 stsp else
1618 13b2bc37 2022-10-23 stsp disconnect(client);
1619 36c7cfbb 2022-11-04 stsp }
1620 ae7c1b78 2023-01-10 stsp
1621 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
1622 13b2bc37 2022-10-23 stsp }
1623 13b2bc37 2022-10-23 stsp done:
1624 13b2bc37 2022-10-23 stsp if (!shut) {
1625 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(iev);
1626 13b2bc37 2022-10-23 stsp } else {
1627 13b2bc37 2022-10-23 stsp /* This pipe is dead. Remove its event handler */
1628 13b2bc37 2022-10-23 stsp event_del(&iev->ev);
1629 ae7c1b78 2023-01-10 stsp disconnect(client);
1630 13b2bc37 2022-10-23 stsp }
1631 13b2bc37 2022-10-23 stsp }
1632 13b2bc37 2022-10-23 stsp
1633 13b2bc37 2022-10-23 stsp static pid_t
1634 eec68231 2022-12-14 stsp start_child(enum gotd_procid proc_id, const char *repo_path,
1635 585362fd 2022-10-31 op char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
1636 13b2bc37 2022-10-23 stsp {
1637 585362fd 2022-10-31 op char *argv[11];
1638 13b2bc37 2022-10-23 stsp int argc = 0;
1639 13b2bc37 2022-10-23 stsp pid_t pid;
1640 13b2bc37 2022-10-23 stsp
1641 13b2bc37 2022-10-23 stsp switch (pid = fork()) {
1642 13b2bc37 2022-10-23 stsp case -1:
1643 13b2bc37 2022-10-23 stsp fatal("cannot fork");
1644 13b2bc37 2022-10-23 stsp case 0:
1645 13b2bc37 2022-10-23 stsp break;
1646 13b2bc37 2022-10-23 stsp default:
1647 13b2bc37 2022-10-23 stsp close(fd);
1648 13b2bc37 2022-10-23 stsp return pid;
1649 13b2bc37 2022-10-23 stsp }
1650 13b2bc37 2022-10-23 stsp
1651 8c6fc146 2022-11-17 stsp if (fd != GOTD_FILENO_MSG_PIPE) {
1652 8c6fc146 2022-11-17 stsp if (dup2(fd, GOTD_FILENO_MSG_PIPE) == -1)
1653 13b2bc37 2022-10-23 stsp fatal("cannot setup imsg fd");
1654 13b2bc37 2022-10-23 stsp } else if (fcntl(fd, F_SETFD, 0) == -1)
1655 13b2bc37 2022-10-23 stsp fatal("cannot setup imsg fd");
1656 13b2bc37 2022-10-23 stsp
1657 13b2bc37 2022-10-23 stsp argv[argc++] = argv0;
1658 13b2bc37 2022-10-23 stsp switch (proc_id) {
1659 d93ecf7d 2022-12-14 stsp case PROC_LISTEN:
1660 d93ecf7d 2022-12-14 stsp argv[argc++] = (char *)"-L";
1661 d93ecf7d 2022-12-14 stsp break;
1662 5e25db14 2022-12-29 stsp case PROC_AUTH:
1663 5e25db14 2022-12-29 stsp argv[argc++] = (char *)"-A";
1664 5e25db14 2022-12-29 stsp break;
1665 b0614828 2023-06-19 stsp case PROC_SESSION_READ:
1666 b0614828 2023-06-19 stsp argv[argc++] = (char *)"-s";
1667 b0614828 2023-06-19 stsp break;
1668 b0614828 2023-06-19 stsp case PROC_SESSION_WRITE:
1669 ae7c1b78 2023-01-10 stsp argv[argc++] = (char *)"-S";
1670 ae7c1b78 2023-01-10 stsp break;
1671 13b2bc37 2022-10-23 stsp case PROC_REPO_READ:
1672 13b2bc37 2022-10-23 stsp argv[argc++] = (char *)"-R";
1673 13b2bc37 2022-10-23 stsp break;
1674 13b2bc37 2022-10-23 stsp case PROC_REPO_WRITE:
1675 13b2bc37 2022-10-23 stsp argv[argc++] = (char *)"-W";
1676 13b2bc37 2022-10-23 stsp break;
1677 ba97b2d7 2024-03-20 stsp case PROC_NOTIFY:
1678 ba97b2d7 2024-03-20 stsp argv[argc++] = (char *)"-N";
1679 ba97b2d7 2024-03-20 stsp break;
1680 13b2bc37 2022-10-23 stsp default:
1681 13b2bc37 2022-10-23 stsp fatalx("invalid process id %d", proc_id);
1682 13b2bc37 2022-10-23 stsp }
1683 13b2bc37 2022-10-23 stsp
1684 585362fd 2022-10-31 op argv[argc++] = (char *)"-f";
1685 585362fd 2022-10-31 op argv[argc++] = (char *)confpath;
1686 585362fd 2022-10-31 op
1687 eec68231 2022-12-14 stsp if (repo_path) {
1688 d93ecf7d 2022-12-14 stsp argv[argc++] = (char *)"-P";
1689 eec68231 2022-12-14 stsp argv[argc++] = (char *)repo_path;
1690 d93ecf7d 2022-12-14 stsp }
1691 13b2bc37 2022-10-23 stsp
1692 13b2bc37 2022-10-23 stsp if (!daemonize)
1693 13b2bc37 2022-10-23 stsp argv[argc++] = (char *)"-d";
1694 13b2bc37 2022-10-23 stsp if (verbosity > 0)
1695 13b2bc37 2022-10-23 stsp argv[argc++] = (char *)"-v";
1696 13b2bc37 2022-10-23 stsp if (verbosity > 1)
1697 13b2bc37 2022-10-23 stsp argv[argc++] = (char *)"-v";
1698 13b2bc37 2022-10-23 stsp argv[argc++] = NULL;
1699 13b2bc37 2022-10-23 stsp
1700 13b2bc37 2022-10-23 stsp execvp(argv0, argv);
1701 13b2bc37 2022-10-23 stsp fatal("execvp");
1702 13b2bc37 2022-10-23 stsp }
1703 13b2bc37 2022-10-23 stsp
1704 13b2bc37 2022-10-23 stsp static void
1705 d93ecf7d 2022-12-14 stsp start_listener(char *argv0, const char *confpath, int daemonize, int verbosity)
1706 d93ecf7d 2022-12-14 stsp {
1707 c929736a 2023-06-22 op struct gotd_child_proc *proc;
1708 d93ecf7d 2022-12-14 stsp
1709 c929736a 2023-06-22 op proc = calloc(1, sizeof(*proc));
1710 c929736a 2023-06-22 op if (proc == NULL)
1711 c929736a 2023-06-22 op fatal("calloc");
1712 c929736a 2023-06-22 op
1713 839338f6 2023-06-22 op TAILQ_INSERT_HEAD(&procs, proc, entry);
1714 839338f6 2023-06-22 op
1715 839338f6 2023-06-22 op /* proc->tmo is initialized in main() after event_init() */
1716 839338f6 2023-06-22 op
1717 d93ecf7d 2022-12-14 stsp proc->type = PROC_LISTEN;
1718 d93ecf7d 2022-12-14 stsp
1719 d93ecf7d 2022-12-14 stsp if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1720 d93ecf7d 2022-12-14 stsp PF_UNSPEC, proc->pipe) == -1)
1721 d93ecf7d 2022-12-14 stsp fatal("socketpair");
1722 d93ecf7d 2022-12-14 stsp
1723 d93ecf7d 2022-12-14 stsp proc->pid = start_child(proc->type, NULL, argv0, confpath,
1724 d93ecf7d 2022-12-14 stsp proc->pipe[1], daemonize, verbosity);
1725 d93ecf7d 2022-12-14 stsp imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1726 b50a2b46 2022-12-29 stsp proc->iev.handler = gotd_dispatch_listener;
1727 d93ecf7d 2022-12-14 stsp proc->iev.events = EV_READ;
1728 d93ecf7d 2022-12-14 stsp proc->iev.handler_arg = NULL;
1729 c929736a 2023-06-22 op
1730 c929736a 2023-06-22 op gotd.listen_proc = proc;
1731 ba97b2d7 2024-03-20 stsp }
1732 ba97b2d7 2024-03-20 stsp
1733 ba97b2d7 2024-03-20 stsp static void
1734 ba97b2d7 2024-03-20 stsp start_notifier(char *argv0, const char *confpath, int daemonize, int verbosity)
1735 ba97b2d7 2024-03-20 stsp {
1736 ba97b2d7 2024-03-20 stsp struct gotd_child_proc *proc;
1737 ba97b2d7 2024-03-20 stsp
1738 ba97b2d7 2024-03-20 stsp proc = calloc(1, sizeof(*proc));
1739 ba97b2d7 2024-03-20 stsp if (proc == NULL)
1740 ba97b2d7 2024-03-20 stsp fatal("calloc");
1741 ba97b2d7 2024-03-20 stsp
1742 ba97b2d7 2024-03-20 stsp TAILQ_INSERT_HEAD(&procs, proc, entry);
1743 ba97b2d7 2024-03-20 stsp
1744 ba97b2d7 2024-03-20 stsp /* proc->tmo is initialized in main() after event_init() */
1745 ba97b2d7 2024-03-20 stsp
1746 ba97b2d7 2024-03-20 stsp proc->type = PROC_NOTIFY;
1747 ba97b2d7 2024-03-20 stsp
1748 ba97b2d7 2024-03-20 stsp if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1749 ba97b2d7 2024-03-20 stsp PF_UNSPEC, proc->pipe) == -1)
1750 ba97b2d7 2024-03-20 stsp fatal("socketpair");
1751 ba97b2d7 2024-03-20 stsp
1752 ba97b2d7 2024-03-20 stsp proc->pid = start_child(proc->type, NULL, argv0, confpath,
1753 ba97b2d7 2024-03-20 stsp proc->pipe[1], daemonize, verbosity);
1754 ba97b2d7 2024-03-20 stsp imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1755 ba97b2d7 2024-03-20 stsp proc->iev.handler = gotd_dispatch_notifier;
1756 ba97b2d7 2024-03-20 stsp proc->iev.events = EV_READ;
1757 ba97b2d7 2024-03-20 stsp proc->iev.handler_arg = NULL;
1758 ba97b2d7 2024-03-20 stsp event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1759 ba97b2d7 2024-03-20 stsp gotd_dispatch_notifier, &proc->iev);
1760 ba97b2d7 2024-03-20 stsp
1761 ba97b2d7 2024-03-20 stsp gotd.notify_proc = proc;
1762 d93ecf7d 2022-12-14 stsp }
1763 d93ecf7d 2022-12-14 stsp
1764 b50a2b46 2022-12-29 stsp static const struct got_error *
1765 ae7c1b78 2023-01-10 stsp start_session_child(struct gotd_client *client, struct gotd_repo *repo,
1766 ae7c1b78 2023-01-10 stsp char *argv0, const char *confpath, int daemonize, int verbosity)
1767 ae7c1b78 2023-01-10 stsp {
1768 ae7c1b78 2023-01-10 stsp struct gotd_child_proc *proc;
1769 ae7c1b78 2023-01-10 stsp
1770 ae7c1b78 2023-01-10 stsp proc = calloc(1, sizeof(*proc));
1771 ae7c1b78 2023-01-10 stsp if (proc == NULL)
1772 ae7c1b78 2023-01-10 stsp return got_error_from_errno("calloc");
1773 ae7c1b78 2023-01-10 stsp
1774 839338f6 2023-06-22 op TAILQ_INSERT_HEAD(&procs, proc, entry);
1775 839338f6 2023-06-22 op evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1776 839338f6 2023-06-22 op
1777 b0614828 2023-06-19 stsp if (client_is_reading(client))
1778 b0614828 2023-06-19 stsp proc->type = PROC_SESSION_READ;
1779 b0614828 2023-06-19 stsp else
1780 b0614828 2023-06-19 stsp proc->type = PROC_SESSION_WRITE;
1781 ae7c1b78 2023-01-10 stsp if (strlcpy(proc->repo_name, repo->name,
1782 ae7c1b78 2023-01-10 stsp sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1783 ae7c1b78 2023-01-10 stsp fatalx("repository name too long: %s", repo->name);
1784 ae7c1b78 2023-01-10 stsp log_debug("starting client uid %d session for repository %s",
1785 ae7c1b78 2023-01-10 stsp client->euid, repo->name);
1786 ae7c1b78 2023-01-10 stsp if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1787 ae7c1b78 2023-01-10 stsp sizeof(proc->repo_path))
1788 ae7c1b78 2023-01-10 stsp fatalx("repository path too long: %s", repo->path);
1789 ae7c1b78 2023-01-10 stsp if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1790 ae7c1b78 2023-01-10 stsp PF_UNSPEC, proc->pipe) == -1)
1791 ae7c1b78 2023-01-10 stsp fatal("socketpair");
1792 ae7c1b78 2023-01-10 stsp proc->pid = start_child(proc->type, proc->repo_path, argv0,
1793 ae7c1b78 2023-01-10 stsp confpath, proc->pipe[1], daemonize, verbosity);
1794 ae7c1b78 2023-01-10 stsp imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1795 ae7c1b78 2023-01-10 stsp log_debug("proc %s %s is on fd %d",
1796 ae7c1b78 2023-01-10 stsp gotd_proc_names[proc->type], proc->repo_path,
1797 ae7c1b78 2023-01-10 stsp proc->pipe[0]);
1798 ae7c1b78 2023-01-10 stsp proc->iev.handler = gotd_dispatch_client_session;
1799 ae7c1b78 2023-01-10 stsp proc->iev.events = EV_READ;
1800 ae7c1b78 2023-01-10 stsp proc->iev.handler_arg = NULL;
1801 ae7c1b78 2023-01-10 stsp event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1802 ae7c1b78 2023-01-10 stsp gotd_dispatch_client_session, &proc->iev);
1803 ae7c1b78 2023-01-10 stsp gotd_imsg_event_add(&proc->iev);
1804 ae7c1b78 2023-01-10 stsp
1805 ae7c1b78 2023-01-10 stsp client->session = proc;
1806 ae7c1b78 2023-01-10 stsp return NULL;
1807 ae7c1b78 2023-01-10 stsp }
1808 ae7c1b78 2023-01-10 stsp
1809 ae7c1b78 2023-01-10 stsp static const struct got_error *
1810 b50a2b46 2022-12-29 stsp start_repo_child(struct gotd_client *client, enum gotd_procid proc_type,
1811 b50a2b46 2022-12-29 stsp struct gotd_repo *repo, char *argv0, const char *confpath,
1812 585362fd 2022-10-31 op int daemonize, int verbosity)
1813 13b2bc37 2022-10-23 stsp {
1814 13b2bc37 2022-10-23 stsp struct gotd_child_proc *proc;
1815 13b2bc37 2022-10-23 stsp
1816 b50a2b46 2022-12-29 stsp if (proc_type != PROC_REPO_READ && proc_type != PROC_REPO_WRITE)
1817 b50a2b46 2022-12-29 stsp return got_error_msg(GOT_ERR_NOT_IMPL, "bad process type");
1818 7fdc3e58 2022-12-30 mark
1819 b50a2b46 2022-12-29 stsp proc = calloc(1, sizeof(*proc));
1820 b50a2b46 2022-12-29 stsp if (proc == NULL)
1821 b50a2b46 2022-12-29 stsp return got_error_from_errno("calloc");
1822 13b2bc37 2022-10-23 stsp
1823 839338f6 2023-06-22 op TAILQ_INSERT_HEAD(&procs, proc, entry);
1824 839338f6 2023-06-22 op evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1825 839338f6 2023-06-22 op
1826 b50a2b46 2022-12-29 stsp proc->type = proc_type;
1827 b50a2b46 2022-12-29 stsp if (strlcpy(proc->repo_name, repo->name,
1828 b50a2b46 2022-12-29 stsp sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1829 b50a2b46 2022-12-29 stsp fatalx("repository name too long: %s", repo->name);
1830 b50a2b46 2022-12-29 stsp log_debug("starting %s for repository %s",
1831 b50a2b46 2022-12-29 stsp proc->type == PROC_REPO_READ ? "reader" : "writer", repo->name);
1832 9b7f22a6 2023-01-08 stsp if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1833 9b7f22a6 2023-01-08 stsp sizeof(proc->repo_path))
1834 9b7f22a6 2023-01-08 stsp fatalx("repository path too long: %s", repo->path);
1835 b50a2b46 2022-12-29 stsp if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1836 b50a2b46 2022-12-29 stsp PF_UNSPEC, proc->pipe) == -1)
1837 b50a2b46 2022-12-29 stsp fatal("socketpair");
1838 b50a2b46 2022-12-29 stsp proc->pid = start_child(proc->type, proc->repo_path, argv0,
1839 b50a2b46 2022-12-29 stsp confpath, proc->pipe[1], daemonize, verbosity);
1840 b50a2b46 2022-12-29 stsp imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1841 b50a2b46 2022-12-29 stsp log_debug("proc %s %s is on fd %d",
1842 b50a2b46 2022-12-29 stsp gotd_proc_names[proc->type], proc->repo_path,
1843 b50a2b46 2022-12-29 stsp proc->pipe[0]);
1844 b50a2b46 2022-12-29 stsp proc->iev.handler = gotd_dispatch_repo_child;
1845 b50a2b46 2022-12-29 stsp proc->iev.events = EV_READ;
1846 b50a2b46 2022-12-29 stsp proc->iev.handler_arg = NULL;
1847 b50a2b46 2022-12-29 stsp event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1848 b50a2b46 2022-12-29 stsp gotd_dispatch_repo_child, &proc->iev);
1849 b50a2b46 2022-12-29 stsp gotd_imsg_event_add(&proc->iev);
1850 b50a2b46 2022-12-29 stsp
1851 f7a854cf 2023-01-10 stsp client->repo = proc;
1852 5e25db14 2022-12-29 stsp return NULL;
1853 5e25db14 2022-12-29 stsp }
1854 5e25db14 2022-12-29 stsp
1855 5e25db14 2022-12-29 stsp static const struct got_error *
1856 5e25db14 2022-12-29 stsp start_auth_child(struct gotd_client *client, int required_auth,
1857 5e25db14 2022-12-29 stsp struct gotd_repo *repo, char *argv0, const char *confpath,
1858 5e25db14 2022-12-29 stsp int daemonize, int verbosity)
1859 5e25db14 2022-12-29 stsp {
1860 365cf0f3 2022-12-29 stsp const struct got_error *err = NULL;
1861 5e25db14 2022-12-29 stsp struct gotd_child_proc *proc;
1862 5e25db14 2022-12-29 stsp struct gotd_imsg_auth iauth;
1863 365cf0f3 2022-12-29 stsp int fd;
1864 5e25db14 2022-12-29 stsp
1865 5e25db14 2022-12-29 stsp memset(&iauth, 0, sizeof(iauth));
1866 365cf0f3 2022-12-29 stsp
1867 365cf0f3 2022-12-29 stsp fd = dup(client->fd);
1868 365cf0f3 2022-12-29 stsp if (fd == -1)
1869 365cf0f3 2022-12-29 stsp return got_error_from_errno("dup");
1870 5e25db14 2022-12-29 stsp
1871 5e25db14 2022-12-29 stsp proc = calloc(1, sizeof(*proc));
1872 365cf0f3 2022-12-29 stsp if (proc == NULL) {
1873 365cf0f3 2022-12-29 stsp err = got_error_from_errno("calloc");
1874 365cf0f3 2022-12-29 stsp close(fd);
1875 365cf0f3 2022-12-29 stsp return err;
1876 365cf0f3 2022-12-29 stsp }
1877 839338f6 2023-06-22 op
1878 839338f6 2023-06-22 op TAILQ_INSERT_HEAD(&procs, proc, entry);
1879 839338f6 2023-06-22 op evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1880 5e25db14 2022-12-29 stsp
1881 5e25db14 2022-12-29 stsp proc->type = PROC_AUTH;
1882 5e25db14 2022-12-29 stsp if (strlcpy(proc->repo_name, repo->name,
1883 5e25db14 2022-12-29 stsp sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1884 5e25db14 2022-12-29 stsp fatalx("repository name too long: %s", repo->name);
1885 5e25db14 2022-12-29 stsp log_debug("starting auth for uid %d repository %s",
1886 5e25db14 2022-12-29 stsp client->euid, repo->name);
1887 9b7f22a6 2023-01-08 stsp if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1888 9b7f22a6 2023-01-08 stsp sizeof(proc->repo_path))
1889 9b7f22a6 2023-01-08 stsp fatalx("repository path too long: %s", repo->path);
1890 5e25db14 2022-12-29 stsp if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1891 5e25db14 2022-12-29 stsp PF_UNSPEC, proc->pipe) == -1)
1892 5e25db14 2022-12-29 stsp fatal("socketpair");
1893 5e25db14 2022-12-29 stsp proc->pid = start_child(proc->type, proc->repo_path, argv0,
1894 5e25db14 2022-12-29 stsp confpath, proc->pipe[1], daemonize, verbosity);
1895 5e25db14 2022-12-29 stsp imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1896 5e25db14 2022-12-29 stsp log_debug("proc %s %s is on fd %d",
1897 5e25db14 2022-12-29 stsp gotd_proc_names[proc->type], proc->repo_path,
1898 5e25db14 2022-12-29 stsp proc->pipe[0]);
1899 5e25db14 2022-12-29 stsp proc->iev.handler = gotd_dispatch_auth_child;
1900 5e25db14 2022-12-29 stsp proc->iev.events = EV_READ;
1901 5e25db14 2022-12-29 stsp proc->iev.handler_arg = NULL;
1902 5e25db14 2022-12-29 stsp event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1903 5e25db14 2022-12-29 stsp gotd_dispatch_auth_child, &proc->iev);
1904 5e25db14 2022-12-29 stsp gotd_imsg_event_add(&proc->iev);
1905 5e25db14 2022-12-29 stsp
1906 5e25db14 2022-12-29 stsp iauth.euid = client->euid;
1907 5e25db14 2022-12-29 stsp iauth.egid = client->egid;
1908 5e25db14 2022-12-29 stsp iauth.required_auth = required_auth;
1909 5e25db14 2022-12-29 stsp iauth.client_id = client->id;
1910 5e25db14 2022-12-29 stsp if (gotd_imsg_compose_event(&proc->iev, GOTD_IMSG_AUTHENTICATE,
1911 365cf0f3 2022-12-29 stsp PROC_GOTD, fd, &iauth, sizeof(iauth)) == -1) {
1912 5e25db14 2022-12-29 stsp log_warn("imsg compose AUTHENTICATE");
1913 365cf0f3 2022-12-29 stsp close(fd);
1914 365cf0f3 2022-12-29 stsp /* Let the auth_timeout handler tidy up. */
1915 365cf0f3 2022-12-29 stsp }
1916 b50a2b46 2022-12-29 stsp
1917 5e25db14 2022-12-29 stsp client->auth = proc;
1918 5e25db14 2022-12-29 stsp client->required_auth = required_auth;
1919 b50a2b46 2022-12-29 stsp return NULL;
1920 eec68231 2022-12-14 stsp }
1921 eec68231 2022-12-14 stsp
1922 eec68231 2022-12-14 stsp static void
1923 b0614828 2023-06-19 stsp apply_unveil_repo_readonly(const char *repo_path, int need_tmpdir)
1924 eec68231 2022-12-14 stsp {
1925 b0614828 2023-06-19 stsp if (need_tmpdir) {
1926 b0614828 2023-06-19 stsp if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1927 b0614828 2023-06-19 stsp fatal("unveil %s", GOT_TMPDIR_STR);
1928 b0614828 2023-06-19 stsp }
1929 b0614828 2023-06-19 stsp
1930 eec68231 2022-12-14 stsp if (unveil(repo_path, "r") == -1)
1931 eec68231 2022-12-14 stsp fatal("unveil %s", repo_path);
1932 44587340 2022-12-30 stsp
1933 44587340 2022-12-30 stsp if (unveil(NULL, NULL) == -1)
1934 44587340 2022-12-30 stsp fatal("unveil");
1935 44587340 2022-12-30 stsp }
1936 44587340 2022-12-30 stsp
1937 44587340 2022-12-30 stsp static void
1938 ae7c1b78 2023-01-10 stsp apply_unveil_repo_readwrite(const char *repo_path)
1939 ae7c1b78 2023-01-10 stsp {
1940 ae7c1b78 2023-01-10 stsp if (unveil(repo_path, "rwc") == -1)
1941 ae7c1b78 2023-01-10 stsp fatal("unveil %s", repo_path);
1942 ae7c1b78 2023-01-10 stsp
1943 ae7c1b78 2023-01-10 stsp if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1944 ae7c1b78 2023-01-10 stsp fatal("unveil %s", GOT_TMPDIR_STR);
1945 ae7c1b78 2023-01-10 stsp
1946 ae7c1b78 2023-01-10 stsp if (unveil(NULL, NULL) == -1)
1947 ae7c1b78 2023-01-10 stsp fatal("unveil");
1948 ae7c1b78 2023-01-10 stsp }
1949 ae7c1b78 2023-01-10 stsp
1950 ae7c1b78 2023-01-10 stsp static void
1951 44587340 2022-12-30 stsp apply_unveil_none(void)
1952 44587340 2022-12-30 stsp {
1953 44587340 2022-12-30 stsp if (unveil("/", "") == -1)
1954 44587340 2022-12-30 stsp fatal("unveil");
1955 eec68231 2022-12-14 stsp
1956 eec68231 2022-12-14 stsp if (unveil(NULL, NULL) == -1)
1957 eec68231 2022-12-14 stsp fatal("unveil");
1958 13b2bc37 2022-10-23 stsp }
1959 13b2bc37 2022-10-23 stsp
1960 13b2bc37 2022-10-23 stsp static void
1961 ae7c1b78 2023-01-10 stsp apply_unveil_selfexec(void)
1962 13b2bc37 2022-10-23 stsp {
1963 b50a2b46 2022-12-29 stsp if (unveil(gotd.argv0, "x") == -1)
1964 b50a2b46 2022-12-29 stsp fatal("unveil %s", gotd.argv0);
1965 b50a2b46 2022-12-29 stsp
1966 13b2bc37 2022-10-23 stsp if (unveil(NULL, NULL) == -1)
1967 13b2bc37 2022-10-23 stsp fatal("unveil");
1968 13b2bc37 2022-10-23 stsp }
1969 13b2bc37 2022-10-23 stsp
1970 abe89edb 2023-11-16 stsp static void
1971 abe89edb 2023-11-16 stsp set_max_datasize(void)
1972 abe89edb 2023-11-16 stsp {
1973 abe89edb 2023-11-16 stsp struct rlimit rl;
1974 abe89edb 2023-11-16 stsp
1975 abe89edb 2023-11-16 stsp if (getrlimit(RLIMIT_DATA, &rl) != 0)
1976 abe89edb 2023-11-16 stsp return;
1977 abe89edb 2023-11-16 stsp
1978 abe89edb 2023-11-16 stsp rl.rlim_cur = rl.rlim_max;
1979 abe89edb 2023-11-16 stsp setrlimit(RLIMIT_DATA, &rl);
1980 ba97b2d7 2024-03-20 stsp }
1981 ba97b2d7 2024-03-20 stsp
1982 ba97b2d7 2024-03-20 stsp static void
1983 ba97b2d7 2024-03-20 stsp unveil_notification_helpers(void)
1984 ba97b2d7 2024-03-20 stsp {
1985 ba97b2d7 2024-03-20 stsp const char *helpers[] = {
1986 ba97b2d7 2024-03-20 stsp GOTD_PATH_PROG_NOTIFY_EMAIL,
1987 ba97b2d7 2024-03-20 stsp GOTD_PATH_PROG_NOTIFY_HTTP,
1988 ba97b2d7 2024-03-20 stsp };
1989 ba97b2d7 2024-03-20 stsp size_t i;
1990 ba97b2d7 2024-03-20 stsp
1991 ba97b2d7 2024-03-20 stsp for (i = 0; i < nitems(helpers); i++) {
1992 ba97b2d7 2024-03-20 stsp if (unveil(helpers[i], "x") == 0)
1993 ba97b2d7 2024-03-20 stsp continue;
1994 ba97b2d7 2024-03-20 stsp fatal("unveil %s", helpers[i]);
1995 ba97b2d7 2024-03-20 stsp }
1996 ba97b2d7 2024-03-20 stsp
1997 ba97b2d7 2024-03-20 stsp if (unveil(NULL, NULL) == -1)
1998 ba97b2d7 2024-03-20 stsp fatal("unveil");
1999 abe89edb 2023-11-16 stsp }
2000 abe89edb 2023-11-16 stsp
2001 13b2bc37 2022-10-23 stsp int
2002 13b2bc37 2022-10-23 stsp main(int argc, char **argv)
2003 13b2bc37 2022-10-23 stsp {
2004 13b2bc37 2022-10-23 stsp const struct got_error *error = NULL;
2005 13b2bc37 2022-10-23 stsp int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
2006 13b2bc37 2022-10-23 stsp const char *confpath = GOTD_CONF_PATH;
2007 13b2bc37 2022-10-23 stsp char *argv0 = argv[0];
2008 13b2bc37 2022-10-23 stsp char title[2048];
2009 13b2bc37 2022-10-23 stsp struct passwd *pw = NULL;
2010 13b2bc37 2022-10-23 stsp char *repo_path = NULL;
2011 13b2bc37 2022-10-23 stsp enum gotd_procid proc_id = PROC_GOTD;
2012 839338f6 2023-06-22 op struct event evsigint, evsigterm, evsighup, evsigusr1, evsigchld;
2013 13b2bc37 2022-10-23 stsp int *pack_fds = NULL, *temp_fds = NULL;
2014 9afa3de2 2023-04-04 stsp struct gotd_repo *repo = NULL;
2015 ba97b2d7 2024-03-20 stsp char *default_sender = NULL;
2016 ba97b2d7 2024-03-20 stsp char hostname[HOST_NAME_MAX + 1];
2017 ba97b2d7 2024-03-20 stsp FILE *diff_f1 = NULL, *diff_f2 = NULL;
2018 ba97b2d7 2024-03-20 stsp int diff_fd1 = -1, diff_fd2 = -1;
2019 13b2bc37 2022-10-23 stsp
2020 839338f6 2023-06-22 op TAILQ_INIT(&procs);
2021 839338f6 2023-06-22 op
2022 13b2bc37 2022-10-23 stsp log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
2023 13b2bc37 2022-10-23 stsp
2024 ba97b2d7 2024-03-20 stsp while ((ch = getopt(argc, argv, "Adf:LnNP:RsSvW")) != -1) {
2025 13b2bc37 2022-10-23 stsp switch (ch) {
2026 5e25db14 2022-12-29 stsp case 'A':
2027 5e25db14 2022-12-29 stsp proc_id = PROC_AUTH;
2028 5e25db14 2022-12-29 stsp break;
2029 13b2bc37 2022-10-23 stsp case 'd':
2030 13b2bc37 2022-10-23 stsp daemonize = 0;
2031 13b2bc37 2022-10-23 stsp break;
2032 13b2bc37 2022-10-23 stsp case 'f':
2033 13b2bc37 2022-10-23 stsp confpath = optarg;
2034 13b2bc37 2022-10-23 stsp break;
2035 d93ecf7d 2022-12-14 stsp case 'L':
2036 d93ecf7d 2022-12-14 stsp proc_id = PROC_LISTEN;
2037 d93ecf7d 2022-12-14 stsp break;
2038 13b2bc37 2022-10-23 stsp case 'n':
2039 13b2bc37 2022-10-23 stsp noaction = 1;
2040 13b2bc37 2022-10-23 stsp break;
2041 ba97b2d7 2024-03-20 stsp case 'N':
2042 ba97b2d7 2024-03-20 stsp proc_id = PROC_NOTIFY;
2043 ba97b2d7 2024-03-20 stsp break;
2044 6f319063 2022-10-27 stsp case 'P':
2045 6f319063 2022-10-27 stsp repo_path = realpath(optarg, NULL);
2046 6f319063 2022-10-27 stsp if (repo_path == NULL)
2047 6f319063 2022-10-27 stsp fatal("realpath '%s'", optarg);
2048 13b2bc37 2022-10-23 stsp break;
2049 13b2bc37 2022-10-23 stsp case 'R':
2050 13b2bc37 2022-10-23 stsp proc_id = PROC_REPO_READ;
2051 13b2bc37 2022-10-23 stsp break;
2052 b0614828 2023-06-19 stsp case 's':
2053 b0614828 2023-06-19 stsp proc_id = PROC_SESSION_READ;
2054 b0614828 2023-06-19 stsp break;
2055 ae7c1b78 2023-01-10 stsp case 'S':
2056 b0614828 2023-06-19 stsp proc_id = PROC_SESSION_WRITE;
2057 ae7c1b78 2023-01-10 stsp break;
2058 6f319063 2022-10-27 stsp case 'v':
2059 6f319063 2022-10-27 stsp if (verbosity < 3)
2060 6f319063 2022-10-27 stsp verbosity++;
2061 6f319063 2022-10-27 stsp break;
2062 13b2bc37 2022-10-23 stsp case 'W':
2063 13b2bc37 2022-10-23 stsp proc_id = PROC_REPO_WRITE;
2064 13b2bc37 2022-10-23 stsp break;
2065 13b2bc37 2022-10-23 stsp default:
2066 13b2bc37 2022-10-23 stsp usage();
2067 13b2bc37 2022-10-23 stsp }
2068 13b2bc37 2022-10-23 stsp }
2069 13b2bc37 2022-10-23 stsp
2070 13b2bc37 2022-10-23 stsp argc -= optind;
2071 13b2bc37 2022-10-23 stsp argv += optind;
2072 13b2bc37 2022-10-23 stsp
2073 13b2bc37 2022-10-23 stsp if (argc != 0)
2074 13b2bc37 2022-10-23 stsp usage();
2075 b50a2b46 2022-12-29 stsp
2076 b50a2b46 2022-12-29 stsp if (geteuid() && (proc_id == PROC_GOTD || proc_id == PROC_LISTEN))
2077 13b2bc37 2022-10-23 stsp fatalx("need root privileges");
2078 13b2bc37 2022-10-23 stsp
2079 4b3827cd 2023-07-08 stsp if (parse_config(confpath, proc_id, &gotd) != 0)
2080 13b2bc37 2022-10-23 stsp return 1;
2081 13b2bc37 2022-10-23 stsp
2082 13b2bc37 2022-10-23 stsp pw = getpwnam(gotd.user_name);
2083 13b2bc37 2022-10-23 stsp if (pw == NULL)
2084 898c8f8f 2022-12-29 op fatalx("user %s not found", gotd.user_name);
2085 13b2bc37 2022-10-23 stsp
2086 f4e8c21c 2023-01-17 op if (pw->pw_uid == 0)
2087 f4e8c21c 2023-01-17 op fatalx("cannot run %s as the superuser", getprogname());
2088 13b2bc37 2022-10-23 stsp
2089 f4e8c21c 2023-01-17 op if (noaction) {
2090 f4e8c21c 2023-01-17 op fprintf(stderr, "configuration OK\n");
2091 13b2bc37 2022-10-23 stsp return 0;
2092 f4e8c21c 2023-01-17 op }
2093 13b2bc37 2022-10-23 stsp
2094 f4e8c21c 2023-01-17 op gotd.argv0 = argv0;
2095 f4e8c21c 2023-01-17 op gotd.daemonize = daemonize;
2096 f4e8c21c 2023-01-17 op gotd.verbosity = verbosity;
2097 f4e8c21c 2023-01-17 op gotd.confpath = confpath;
2098 f4e8c21c 2023-01-17 op
2099 f4e8c21c 2023-01-17 op /* Require an absolute path in argv[0] for reliable re-exec. */
2100 f4e8c21c 2023-01-17 op if (!got_path_is_absolute(argv0))
2101 f4e8c21c 2023-01-17 op fatalx("bad path \"%s\": must be an absolute path", argv0);
2102 f4e8c21c 2023-01-17 op
2103 f4e8c21c 2023-01-17 op log_init(daemonize ? 0 : 1, LOG_DAEMON);
2104 f4e8c21c 2023-01-17 op log_setverbose(verbosity);
2105 f4e8c21c 2023-01-17 op
2106 b1142068 2022-12-05 stsp if (proc_id == PROC_GOTD) {
2107 d93ecf7d 2022-12-14 stsp snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2108 d93ecf7d 2022-12-14 stsp arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
2109 d93ecf7d 2022-12-14 stsp if (daemonize && daemon(1, 0) == -1)
2110 d93ecf7d 2022-12-14 stsp fatal("daemon");
2111 f7eb3370 2023-01-23 stsp gotd.pid = getpid();
2112 f7eb3370 2023-01-23 stsp start_listener(argv0, confpath, daemonize, verbosity);
2113 ba97b2d7 2024-03-20 stsp start_notifier(argv0, confpath, daemonize, verbosity);
2114 d93ecf7d 2022-12-14 stsp } else if (proc_id == PROC_LISTEN) {
2115 d93ecf7d 2022-12-14 stsp snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2116 b1142068 2022-12-05 stsp if (verbosity) {
2117 b1142068 2022-12-05 stsp log_info("socket: %s", gotd.unix_socket_path);
2118 b1142068 2022-12-05 stsp log_info("user: %s", pw->pw_name);
2119 b1142068 2022-12-05 stsp }
2120 13b2bc37 2022-10-23 stsp
2121 13b2bc37 2022-10-23 stsp fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
2122 6f854dde 2023-01-04 stsp pw->pw_gid);
2123 13b2bc37 2022-10-23 stsp if (fd == -1) {
2124 13b2bc37 2022-10-23 stsp fatal("cannot listen on unix socket %s",
2125 13b2bc37 2022-10-23 stsp gotd.unix_socket_path);
2126 13b2bc37 2022-10-23 stsp }
2127 5e25db14 2022-12-29 stsp } else if (proc_id == PROC_AUTH) {
2128 5e25db14 2022-12-29 stsp snprintf(title, sizeof(title), "%s %s",
2129 5e25db14 2022-12-29 stsp gotd_proc_names[proc_id], repo_path);
2130 ae7c1b78 2023-01-10 stsp } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE ||
2131 b0614828 2023-06-19 stsp proc_id == PROC_SESSION_READ || proc_id == PROC_SESSION_WRITE) {
2132 13b2bc37 2022-10-23 stsp error = got_repo_pack_fds_open(&pack_fds);
2133 13b2bc37 2022-10-23 stsp if (error != NULL)
2134 13b2bc37 2022-10-23 stsp fatalx("cannot open pack tempfiles: %s", error->msg);
2135 13b2bc37 2022-10-23 stsp error = got_repo_temp_fds_open(&temp_fds);
2136 13b2bc37 2022-10-23 stsp if (error != NULL)
2137 13b2bc37 2022-10-23 stsp fatalx("cannot open pack tempfiles: %s", error->msg);
2138 13b2bc37 2022-10-23 stsp if (repo_path == NULL)
2139 13b2bc37 2022-10-23 stsp fatalx("repository path not specified");
2140 13b2bc37 2022-10-23 stsp snprintf(title, sizeof(title), "%s %s",
2141 13b2bc37 2022-10-23 stsp gotd_proc_names[proc_id], repo_path);
2142 ba97b2d7 2024-03-20 stsp } else if (proc_id == PROC_NOTIFY) {
2143 ba97b2d7 2024-03-20 stsp snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2144 ba97b2d7 2024-03-20 stsp if (gethostname(hostname, sizeof(hostname)) == -1)
2145 ba97b2d7 2024-03-20 stsp fatal("gethostname");
2146 ba97b2d7 2024-03-20 stsp if (asprintf(&default_sender, "%s@%s",
2147 ba97b2d7 2024-03-20 stsp pw->pw_name, hostname) == -1)
2148 ba97b2d7 2024-03-20 stsp fatal("asprintf");
2149 13b2bc37 2022-10-23 stsp } else
2150 13b2bc37 2022-10-23 stsp fatal("invalid process id %d", proc_id);
2151 13b2bc37 2022-10-23 stsp
2152 13b2bc37 2022-10-23 stsp setproctitle("%s", title);
2153 13b2bc37 2022-10-23 stsp log_procinit(title);
2154 13b2bc37 2022-10-23 stsp
2155 13b2bc37 2022-10-23 stsp /* Drop root privileges. */
2156 13b2bc37 2022-10-23 stsp if (setgid(pw->pw_gid) == -1)
2157 13b2bc37 2022-10-23 stsp fatal("setgid %d failed", pw->pw_gid);
2158 13b2bc37 2022-10-23 stsp if (setuid(pw->pw_uid) == -1)
2159 13b2bc37 2022-10-23 stsp fatal("setuid %d failed", pw->pw_uid);
2160 13b2bc37 2022-10-23 stsp
2161 13b2bc37 2022-10-23 stsp event_init();
2162 13b2bc37 2022-10-23 stsp
2163 13b2bc37 2022-10-23 stsp switch (proc_id) {
2164 13b2bc37 2022-10-23 stsp case PROC_GOTD:
2165 13b2bc37 2022-10-23 stsp #ifndef PROFILE
2166 ae7c1b78 2023-01-10 stsp /* "exec" promise will be limited to argv[0] via unveil(2). */
2167 ae7c1b78 2023-01-10 stsp if (pledge("stdio proc exec sendfd recvfd unveil", NULL) == -1)
2168 13b2bc37 2022-10-23 stsp err(1, "pledge");
2169 13b2bc37 2022-10-23 stsp #endif
2170 13b2bc37 2022-10-23 stsp break;
2171 d93ecf7d 2022-12-14 stsp case PROC_LISTEN:
2172 d93ecf7d 2022-12-14 stsp #ifndef PROFILE
2173 77f619a8 2023-01-04 stsp if (pledge("stdio sendfd unix unveil", NULL) == -1)
2174 d93ecf7d 2022-12-14 stsp err(1, "pledge");
2175 d93ecf7d 2022-12-14 stsp #endif
2176 77f619a8 2023-01-04 stsp /*
2177 77f619a8 2023-01-04 stsp * Ensure that AF_UNIX bind(2) cannot be used with any other
2178 77f619a8 2023-01-04 stsp * sockets by revoking all filesystem access via unveil(2).
2179 77f619a8 2023-01-04 stsp */
2180 77f619a8 2023-01-04 stsp apply_unveil_none();
2181 77f619a8 2023-01-04 stsp
2182 40b85cca 2023-01-03 stsp listen_main(title, fd, gotd.connection_limits,
2183 40b85cca 2023-01-03 stsp gotd.nconnection_limits);
2184 d93ecf7d 2022-12-14 stsp /* NOTREACHED */
2185 d93ecf7d 2022-12-14 stsp break;
2186 5e25db14 2022-12-29 stsp case PROC_AUTH:
2187 5e25db14 2022-12-29 stsp #ifndef PROFILE
2188 44587340 2022-12-30 stsp if (pledge("stdio getpw recvfd unix unveil", NULL) == -1)
2189 5e25db14 2022-12-29 stsp err(1, "pledge");
2190 5e25db14 2022-12-29 stsp #endif
2191 44587340 2022-12-30 stsp /*
2192 44587340 2022-12-30 stsp * We need the "unix" pledge promise for getpeername(2) only.
2193 44587340 2022-12-30 stsp * Ensure that AF_UNIX bind(2) cannot be used by revoking all
2194 44587340 2022-12-30 stsp * filesystem access via unveil(2). Access to password database
2195 44587340 2022-12-30 stsp * files will still work since "getpw" bypasses unveil(2).
2196 44587340 2022-12-30 stsp */
2197 44587340 2022-12-30 stsp apply_unveil_none();
2198 44587340 2022-12-30 stsp
2199 5e25db14 2022-12-29 stsp auth_main(title, &gotd.repos, repo_path);
2200 5e25db14 2022-12-29 stsp /* NOTREACHED */
2201 5e25db14 2022-12-29 stsp break;
2202 b0614828 2023-06-19 stsp case PROC_SESSION_READ:
2203 b0614828 2023-06-19 stsp case PROC_SESSION_WRITE:
2204 ae7c1b78 2023-01-10 stsp #ifndef PROFILE
2205 ae7c1b78 2023-01-10 stsp /*
2206 ae7c1b78 2023-01-10 stsp * The "recvfd" promise is only needed during setup and
2207 ae7c1b78 2023-01-10 stsp * will be removed in a later pledge(2) call.
2208 ae7c1b78 2023-01-10 stsp */
2209 ae7c1b78 2023-01-10 stsp if (pledge("stdio rpath wpath cpath recvfd sendfd fattr flock "
2210 ae7c1b78 2023-01-10 stsp "unveil", NULL) == -1)
2211 ae7c1b78 2023-01-10 stsp err(1, "pledge");
2212 ae7c1b78 2023-01-10 stsp #endif
2213 b0614828 2023-06-19 stsp if (proc_id == PROC_SESSION_READ)
2214 b0614828 2023-06-19 stsp apply_unveil_repo_readonly(repo_path, 1);
2215 ba97b2d7 2024-03-20 stsp else {
2216 b0614828 2023-06-19 stsp apply_unveil_repo_readwrite(repo_path);
2217 ba97b2d7 2024-03-20 stsp repo = gotd_find_repo_by_path(repo_path, &gotd);
2218 ba97b2d7 2024-03-20 stsp if (repo == NULL)
2219 ba97b2d7 2024-03-20 stsp fatalx("no repository for path %s", repo_path);
2220 ba97b2d7 2024-03-20 stsp }
2221 e70fd952 2024-03-22 stsp if (proc_id == PROC_SESSION_READ)
2222 e70fd952 2024-03-22 stsp session_read_main(title, repo_path, pack_fds, temp_fds,
2223 e70fd952 2024-03-22 stsp &gotd.request_timeout, repo);
2224 e70fd952 2024-03-22 stsp else
2225 e70fd952 2024-03-22 stsp session_write_main(title, repo_path, pack_fds, temp_fds,
2226 e70fd952 2024-03-22 stsp &gotd.request_timeout, repo);
2227 ae7c1b78 2023-01-10 stsp /* NOTREACHED */
2228 ae7c1b78 2023-01-10 stsp break;
2229 13b2bc37 2022-10-23 stsp case PROC_REPO_READ:
2230 abe89edb 2023-11-16 stsp set_max_datasize();
2231 13b2bc37 2022-10-23 stsp #ifndef PROFILE
2232 eec68231 2022-12-14 stsp if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2233 13b2bc37 2022-10-23 stsp err(1, "pledge");
2234 13b2bc37 2022-10-23 stsp #endif
2235 b0614828 2023-06-19 stsp apply_unveil_repo_readonly(repo_path, 0);
2236 eec68231 2022-12-14 stsp repo_read_main(title, repo_path, pack_fds, temp_fds);
2237 13b2bc37 2022-10-23 stsp /* NOTREACHED */
2238 13b2bc37 2022-10-23 stsp exit(0);
2239 13b2bc37 2022-10-23 stsp case PROC_REPO_WRITE:
2240 abe89edb 2023-11-16 stsp set_max_datasize();
2241 ba97b2d7 2024-03-20 stsp
2242 ba97b2d7 2024-03-20 stsp diff_f1 = got_opentemp();
2243 ba97b2d7 2024-03-20 stsp if (diff_f1 == NULL)
2244 ba97b2d7 2024-03-20 stsp fatal("got_opentemp");
2245 ba97b2d7 2024-03-20 stsp diff_f2 = got_opentemp();
2246 ba97b2d7 2024-03-20 stsp if (diff_f2 == NULL)
2247 ba97b2d7 2024-03-20 stsp fatal("got_opentemp");
2248 ba97b2d7 2024-03-20 stsp diff_fd1 = got_opentempfd();
2249 ba97b2d7 2024-03-20 stsp if (diff_fd1 == -1)
2250 ba97b2d7 2024-03-20 stsp fatal("got_opentempfd");
2251 ba97b2d7 2024-03-20 stsp diff_fd2 = got_opentempfd();
2252 ba97b2d7 2024-03-20 stsp if (diff_fd2 == -1)
2253 ba97b2d7 2024-03-20 stsp fatal("got_opentempfd");
2254 13b2bc37 2022-10-23 stsp #ifndef PROFILE
2255 eec68231 2022-12-14 stsp if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2256 13b2bc37 2022-10-23 stsp err(1, "pledge");
2257 13b2bc37 2022-10-23 stsp #endif
2258 b0614828 2023-06-19 stsp apply_unveil_repo_readonly(repo_path, 0);
2259 9afa3de2 2023-04-04 stsp repo = gotd_find_repo_by_path(repo_path, &gotd);
2260 9afa3de2 2023-04-04 stsp if (repo == NULL)
2261 9afa3de2 2023-04-04 stsp fatalx("no repository for path %s", repo_path);
2262 9afa3de2 2023-04-04 stsp repo_write_main(title, repo_path, pack_fds, temp_fds,
2263 ba97b2d7 2024-03-20 stsp diff_f1, diff_f2, diff_fd1, diff_fd2,
2264 9afa3de2 2023-04-04 stsp &repo->protected_tag_namespaces,
2265 9afa3de2 2023-04-04 stsp &repo->protected_branch_namespaces,
2266 9afa3de2 2023-04-04 stsp &repo->protected_branches);
2267 13b2bc37 2022-10-23 stsp /* NOTREACHED */
2268 13b2bc37 2022-10-23 stsp exit(0);
2269 ba97b2d7 2024-03-20 stsp case PROC_NOTIFY:
2270 ba97b2d7 2024-03-20 stsp #ifndef PROFILE
2271 ba97b2d7 2024-03-20 stsp if (pledge("stdio proc exec recvfd unveil", NULL) == -1)
2272 ba97b2d7 2024-03-20 stsp err(1, "pledge");
2273 ba97b2d7 2024-03-20 stsp #endif
2274 ba97b2d7 2024-03-20 stsp /*
2275 ba97b2d7 2024-03-20 stsp * Limit "exec" promise to notification helpers via unveil(2).
2276 ba97b2d7 2024-03-20 stsp */
2277 ba97b2d7 2024-03-20 stsp unveil_notification_helpers();
2278 ba97b2d7 2024-03-20 stsp
2279 ba97b2d7 2024-03-20 stsp notify_main(title, &gotd.repos, default_sender);
2280 ba97b2d7 2024-03-20 stsp /* NOTREACHED */
2281 ba97b2d7 2024-03-20 stsp exit(0);
2282 13b2bc37 2022-10-23 stsp default:
2283 13b2bc37 2022-10-23 stsp fatal("invalid process id %d", proc_id);
2284 13b2bc37 2022-10-23 stsp }
2285 13b2bc37 2022-10-23 stsp
2286 13b2bc37 2022-10-23 stsp if (proc_id != PROC_GOTD)
2287 13b2bc37 2022-10-23 stsp fatal("invalid process id %d", proc_id);
2288 13b2bc37 2022-10-23 stsp
2289 839338f6 2023-06-22 op evtimer_set(&gotd.listen_proc->tmo, kill_proc_timeout,
2290 839338f6 2023-06-22 op gotd.listen_proc);
2291 ba97b2d7 2024-03-20 stsp if (gotd.notify_proc) {
2292 ba97b2d7 2024-03-20 stsp evtimer_set(&gotd.notify_proc->tmo, kill_proc_timeout,
2293 ba97b2d7 2024-03-20 stsp gotd.notify_proc);
2294 ba97b2d7 2024-03-20 stsp }
2295 839338f6 2023-06-22 op
2296 ae7c1b78 2023-01-10 stsp apply_unveil_selfexec();
2297 13b2bc37 2022-10-23 stsp
2298 13b2bc37 2022-10-23 stsp signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
2299 13b2bc37 2022-10-23 stsp signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
2300 13b2bc37 2022-10-23 stsp signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
2301 13b2bc37 2022-10-23 stsp signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
2302 839338f6 2023-06-22 op signal_set(&evsigchld, SIGCHLD, gotd_sighdlr, NULL);
2303 13b2bc37 2022-10-23 stsp signal(SIGPIPE, SIG_IGN);
2304 13b2bc37 2022-10-23 stsp
2305 13b2bc37 2022-10-23 stsp signal_add(&evsigint, NULL);
2306 13b2bc37 2022-10-23 stsp signal_add(&evsigterm, NULL);
2307 13b2bc37 2022-10-23 stsp signal_add(&evsighup, NULL);
2308 13b2bc37 2022-10-23 stsp signal_add(&evsigusr1, NULL);
2309 839338f6 2023-06-22 op signal_add(&evsigchld, NULL);
2310 13b2bc37 2022-10-23 stsp
2311 c929736a 2023-06-22 op gotd_imsg_event_add(&gotd.listen_proc->iev);
2312 ba97b2d7 2024-03-20 stsp if (gotd.notify_proc)
2313 ba97b2d7 2024-03-20 stsp gotd_imsg_event_add(&gotd.notify_proc->iev);
2314 13b2bc37 2022-10-23 stsp
2315 13b2bc37 2022-10-23 stsp event_dispatch();
2316 13b2bc37 2022-10-23 stsp
2317 13b2bc37 2022-10-23 stsp free(repo_path);
2318 ba97b2d7 2024-03-20 stsp free(default_sender);
2319 ae7c1b78 2023-01-10 stsp gotd_shutdown();
2320 ae7c1b78 2023-01-10 stsp
2321 13b2bc37 2022-10-23 stsp return 0;
2322 13b2bc37 2022-10-23 stsp }