Blame


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