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