2 3efd8e31 2022-10-23 thomas * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 3efd8e31 2022-10-23 thomas #include <sys/queue.h>
18 3efd8e31 2022-10-23 thomas #include <sys/time.h>
19 3efd8e31 2022-10-23 thomas #include <sys/types.h>
20 3efd8e31 2022-10-23 thomas #include <sys/stat.h>
21 3efd8e31 2022-10-23 thomas #include <sys/socket.h>
22 3efd8e31 2022-10-23 thomas #include <sys/un.h>
23 3efd8e31 2022-10-23 thomas #include <sys/wait.h>
25 3efd8e31 2022-10-23 thomas #include <fcntl.h>
26 3efd8e31 2022-10-23 thomas #include <err.h>
27 3efd8e31 2022-10-23 thomas #include <errno.h>
28 3efd8e31 2022-10-23 thomas #include <event.h>
29 3efd8e31 2022-10-23 thomas #include <limits.h>
30 3efd8e31 2022-10-23 thomas #include <pwd.h>
31 3efd8e31 2022-10-23 thomas #include <imsg.h>
32 3efd8e31 2022-10-23 thomas #include <signal.h>
33 3efd8e31 2022-10-23 thomas #include <stdarg.h>
34 3efd8e31 2022-10-23 thomas #include <stdio.h>
35 3efd8e31 2022-10-23 thomas #include <stdlib.h>
36 3efd8e31 2022-10-23 thomas #include <string.h>
37 3efd8e31 2022-10-23 thomas #include <syslog.h>
38 3efd8e31 2022-10-23 thomas #include <unistd.h>
40 3efd8e31 2022-10-23 thomas #include "got_error.h"
41 3efd8e31 2022-10-23 thomas #include "got_opentemp.h"
42 3efd8e31 2022-10-23 thomas #include "got_path.h"
43 3efd8e31 2022-10-23 thomas #include "got_repository.h"
44 3efd8e31 2022-10-23 thomas #include "got_object.h"
45 3efd8e31 2022-10-23 thomas #include "got_reference.h"
47 3efd8e31 2022-10-23 thomas #include "got_lib_delta.h"
48 3efd8e31 2022-10-23 thomas #include "got_lib_object.h"
49 3efd8e31 2022-10-23 thomas #include "got_lib_object_cache.h"
50 be288a59 2023-02-23 thomas #include "got_lib_hash.h"
51 3efd8e31 2022-10-23 thomas #include "got_lib_gitproto.h"
52 3efd8e31 2022-10-23 thomas #include "got_lib_pack.h"
53 3efd8e31 2022-10-23 thomas #include "got_lib_repository.h"
55 3efd8e31 2022-10-23 thomas #include "gotd.h"
56 3efd8e31 2022-10-23 thomas #include "log.h"
57 2b3d32a1 2022-12-30 thomas #include "listen.h"
58 729a7e24 2022-11-17 thomas #include "auth.h"
59 62ee7d94 2023-01-10 thomas #include "session.h"
60 3efd8e31 2022-10-23 thomas #include "repo_read.h"
61 3efd8e31 2022-10-23 thomas #include "repo_write.h"
63 3efd8e31 2022-10-23 thomas #ifndef nitems
64 3efd8e31 2022-10-23 thomas #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
67 7b1db75e 2023-01-14 thomas enum gotd_client_state {
68 7b1db75e 2023-01-14 thomas GOTD_CLIENT_STATE_NEW,
69 7b1db75e 2023-01-14 thomas GOTD_CLIENT_STATE_ACCESS_GRANTED,
72 78943464 2023-06-22 thomas struct gotd_child_proc {
73 78943464 2023-06-22 thomas pid_t pid;
74 78943464 2023-06-22 thomas enum gotd_procid type;
75 78943464 2023-06-22 thomas char repo_name[NAME_MAX];
76 78943464 2023-06-22 thomas char repo_path[PATH_MAX];
77 78943464 2023-06-22 thomas int pipe[2];
78 78943464 2023-06-22 thomas struct gotd_imsgev iev;
79 2c8fb90b 2023-06-25 thomas struct event tmo;
81 2c8fb90b 2023-06-25 thomas TAILQ_ENTRY(gotd_child_proc) entry;
83 2c8fb90b 2023-06-25 thomas TAILQ_HEAD(gotd_procs, gotd_child_proc) procs;
85 3efd8e31 2022-10-23 thomas struct gotd_client {
86 3efd8e31 2022-10-23 thomas STAILQ_ENTRY(gotd_client) entry;
87 3efd8e31 2022-10-23 thomas enum gotd_client_state state;
88 3efd8e31 2022-10-23 thomas uint32_t id;
90 3efd8e31 2022-10-23 thomas struct gotd_imsgev iev;
91 3efd8e31 2022-10-23 thomas struct event tmo;
92 3efd8e31 2022-10-23 thomas uid_t euid;
93 3efd8e31 2022-10-23 thomas gid_t egid;
94 27b11d77 2023-01-14 thomas struct gotd_child_proc *repo;
95 c669c489 2022-12-30 thomas struct gotd_child_proc *auth;
96 62ee7d94 2023-01-10 thomas struct gotd_child_proc *session;
97 c669c489 2022-12-30 thomas int required_auth;
99 3efd8e31 2022-10-23 thomas STAILQ_HEAD(gotd_clients, gotd_client);
101 3efd8e31 2022-10-23 thomas static struct gotd_clients gotd_clients[GOTD_CLIENT_TABLE_SIZE];
102 3efd8e31 2022-10-23 thomas static SIPHASH_KEY clients_hash_key;
103 3efd8e31 2022-10-23 thomas volatile int client_cnt;
104 95ef3f8a 2022-12-30 thomas static struct timeval auth_timeout = { 5, 0 };
105 3efd8e31 2022-10-23 thomas static struct gotd gotd;
107 3efd8e31 2022-10-23 thomas void gotd_sighdlr(int sig, short event, void *arg);
108 c902213d 2022-10-29 thomas static void gotd_shutdown(void);
109 62ee7d94 2023-01-10 thomas static const struct got_error *start_session_child(struct gotd_client *,
110 62ee7d94 2023-01-10 thomas struct gotd_repo *, char *, const char *, int, int);
111 85b37c72 2022-12-30 thomas static const struct got_error *start_repo_child(struct gotd_client *,
112 85b37c72 2022-12-30 thomas enum gotd_procid, struct gotd_repo *, char *, const char *, int, int);
113 c669c489 2022-12-30 thomas static const struct got_error *start_auth_child(struct gotd_client *, int,
114 c669c489 2022-12-30 thomas struct gotd_repo *, char *, const char *, int, int);
115 85b37c72 2022-12-30 thomas static void kill_proc(struct gotd_child_proc *, int);
116 2c8fb90b 2023-06-25 thomas static void disconnect(struct gotd_client *);
118 3efd8e31 2022-10-23 thomas __dead static void
119 96d694ac 2023-02-17 thomas usage(void)
121 c855c9f0 2023-01-19 thomas fprintf(stderr, "usage: %s [-dnv] [-f config-file]\n", getprogname());
125 3efd8e31 2022-10-23 thomas static int
126 3efd8e31 2022-10-23 thomas unix_socket_listen(const char *unix_socket_path, uid_t uid, gid_t gid)
128 3efd8e31 2022-10-23 thomas struct sockaddr_un sun;
129 3efd8e31 2022-10-23 thomas int fd = -1;
130 3efd8e31 2022-10-23 thomas mode_t old_umask, mode;
131 cff566e8 2023-08-23 thomas int sock_flags = SOCK_STREAM | SOCK_NONBLOCK;
133 cff566e8 2023-08-23 thomas #ifdef SOCK_CLOEXEC
134 cff566e8 2023-08-23 thomas sock_flags |= SOCK_CLOEXEC;
137 cff566e8 2023-08-23 thomas fd = socket(AF_UNIX, sock_flags, 0);
138 3efd8e31 2022-10-23 thomas if (fd == -1) {
139 3efd8e31 2022-10-23 thomas log_warn("socket");
140 3efd8e31 2022-10-23 thomas return -1;
143 3efd8e31 2022-10-23 thomas sun.sun_family = AF_UNIX;
144 3efd8e31 2022-10-23 thomas if (strlcpy(sun.sun_path, unix_socket_path,
145 3efd8e31 2022-10-23 thomas sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
146 3efd8e31 2022-10-23 thomas log_warnx("%s: name too long", unix_socket_path);
147 3efd8e31 2022-10-23 thomas close(fd);
148 3efd8e31 2022-10-23 thomas return -1;
151 3efd8e31 2022-10-23 thomas if (unlink(unix_socket_path) == -1) {
152 3efd8e31 2022-10-23 thomas if (errno != ENOENT) {
153 3efd8e31 2022-10-23 thomas log_warn("unlink %s", unix_socket_path);
154 3efd8e31 2022-10-23 thomas close(fd);
155 3efd8e31 2022-10-23 thomas return -1;
159 3efd8e31 2022-10-23 thomas old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
160 f2fc8ce0 2023-01-06 thomas mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
162 3efd8e31 2022-10-23 thomas if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
163 3efd8e31 2022-10-23 thomas log_warn("bind: %s", unix_socket_path);
164 3efd8e31 2022-10-23 thomas close(fd);
165 3efd8e31 2022-10-23 thomas umask(old_umask);
166 3efd8e31 2022-10-23 thomas return -1;
169 3efd8e31 2022-10-23 thomas umask(old_umask);
171 3efd8e31 2022-10-23 thomas if (chmod(unix_socket_path, mode) == -1) {
172 3efd8e31 2022-10-23 thomas log_warn("chmod %o %s", mode, unix_socket_path);
173 3efd8e31 2022-10-23 thomas close(fd);
174 3efd8e31 2022-10-23 thomas unlink(unix_socket_path);
175 3efd8e31 2022-10-23 thomas return -1;
178 3efd8e31 2022-10-23 thomas if (chown(unix_socket_path, uid, gid) == -1) {
179 3efd8e31 2022-10-23 thomas log_warn("chown %s uid=%d gid=%d", unix_socket_path, uid, gid);
180 3efd8e31 2022-10-23 thomas close(fd);
181 3efd8e31 2022-10-23 thomas unlink(unix_socket_path);
182 3efd8e31 2022-10-23 thomas return -1;
185 3efd8e31 2022-10-23 thomas if (listen(fd, GOTD_UNIX_SOCKET_BACKLOG) == -1) {
186 3efd8e31 2022-10-23 thomas log_warn("listen");
187 3efd8e31 2022-10-23 thomas close(fd);
188 3efd8e31 2022-10-23 thomas unlink(unix_socket_path);
189 3efd8e31 2022-10-23 thomas return -1;
192 3efd8e31 2022-10-23 thomas return fd;
195 3efd8e31 2022-10-23 thomas static uint64_t
196 3efd8e31 2022-10-23 thomas client_hash(uint32_t client_id)
198 3efd8e31 2022-10-23 thomas return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
201 3efd8e31 2022-10-23 thomas static void
202 3efd8e31 2022-10-23 thomas add_client(struct gotd_client *client)
204 3efd8e31 2022-10-23 thomas uint64_t slot = client_hash(client->id) % nitems(gotd_clients);
205 3efd8e31 2022-10-23 thomas STAILQ_INSERT_HEAD(&gotd_clients[slot], client, entry);
206 3efd8e31 2022-10-23 thomas client_cnt++;
209 3efd8e31 2022-10-23 thomas static struct gotd_client *
210 3efd8e31 2022-10-23 thomas find_client(uint32_t client_id)
212 3efd8e31 2022-10-23 thomas uint64_t slot;
213 3efd8e31 2022-10-23 thomas struct gotd_client *c;
215 3efd8e31 2022-10-23 thomas slot = client_hash(client_id) % nitems(gotd_clients);
216 3efd8e31 2022-10-23 thomas STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
217 3efd8e31 2022-10-23 thomas if (c->id == client_id)
218 3efd8e31 2022-10-23 thomas return c;
221 3efd8e31 2022-10-23 thomas return NULL;
224 85b37c72 2022-12-30 thomas static struct gotd_client *
225 85b37c72 2022-12-30 thomas find_client_by_proc_fd(int fd)
227 85b37c72 2022-12-30 thomas uint64_t slot;
229 85b37c72 2022-12-30 thomas for (slot = 0; slot < nitems(gotd_clients); slot++) {
230 85b37c72 2022-12-30 thomas struct gotd_client *c;
232 85b37c72 2022-12-30 thomas STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
233 27b11d77 2023-01-14 thomas if (c->repo && c->repo->iev.ibuf.fd == fd)
234 85b37c72 2022-12-30 thomas return c;
235 c669c489 2022-12-30 thomas if (c->auth && c->auth->iev.ibuf.fd == fd)
236 62ee7d94 2023-01-10 thomas return c;
237 62ee7d94 2023-01-10 thomas if (c->session && c->session->iev.ibuf.fd == fd)
238 c669c489 2022-12-30 thomas return c;
242 3efd8e31 2022-10-23 thomas return NULL;
245 3efd8e31 2022-10-23 thomas static int
246 3efd8e31 2022-10-23 thomas client_is_reading(struct gotd_client *client)
248 27b11d77 2023-01-14 thomas return (client->required_auth &
249 27b11d77 2023-01-14 thomas (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) == GOTD_AUTH_READ;
252 3efd8e31 2022-10-23 thomas static int
253 3efd8e31 2022-10-23 thomas client_is_writing(struct gotd_client *client)
255 27b11d77 2023-01-14 thomas return (client->required_auth &
256 27b11d77 2023-01-14 thomas (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) ==
257 27b11d77 2023-01-14 thomas (GOTD_AUTH_READ | GOTD_AUTH_WRITE);
260 3efd8e31 2022-10-23 thomas static const struct got_error *
261 3efd8e31 2022-10-23 thomas ensure_client_is_not_writing(struct gotd_client *client)
263 3efd8e31 2022-10-23 thomas if (client_is_writing(client)) {
264 3efd8e31 2022-10-23 thomas return got_error_fmt(GOT_ERR_BAD_PACKET,
265 3efd8e31 2022-10-23 thomas "uid %d made a read-request but is writing to "
266 3efd8e31 2022-10-23 thomas "a repository", client->euid);
269 3efd8e31 2022-10-23 thomas return NULL;
272 3efd8e31 2022-10-23 thomas static const struct got_error *
273 3efd8e31 2022-10-23 thomas ensure_client_is_not_reading(struct gotd_client *client)
275 3efd8e31 2022-10-23 thomas if (client_is_reading(client)) {
276 3efd8e31 2022-10-23 thomas return got_error_fmt(GOT_ERR_BAD_PACKET,
277 3efd8e31 2022-10-23 thomas "uid %d made a write-request but is reading from "
278 3efd8e31 2022-10-23 thomas "a repository", client->euid);
281 3efd8e31 2022-10-23 thomas return NULL;
284 85b37c72 2022-12-30 thomas static void
285 2c8fb90b 2023-06-25 thomas proc_done(struct gotd_child_proc *proc)
287 2c8fb90b 2023-06-25 thomas struct gotd_client *client;
289 2c8fb90b 2023-06-25 thomas TAILQ_REMOVE(&procs, proc, entry);
291 2c8fb90b 2023-06-25 thomas client = find_client_by_proc_fd(proc->iev.ibuf.fd);
292 2c8fb90b 2023-06-25 thomas if (client != NULL) {
293 2c8fb90b 2023-06-25 thomas if (proc == client->repo)
294 2c8fb90b 2023-06-25 thomas client->repo = NULL;
295 2c8fb90b 2023-06-25 thomas if (proc == client->auth)
296 2c8fb90b 2023-06-25 thomas client->auth = NULL;
297 2c8fb90b 2023-06-25 thomas if (proc == client->session)
298 2c8fb90b 2023-06-25 thomas client->session = NULL;
299 2c8fb90b 2023-06-25 thomas disconnect(client);
302 2c8fb90b 2023-06-25 thomas evtimer_del(&proc->tmo);
304 2c8fb90b 2023-06-25 thomas if (proc->iev.ibuf.fd != -1) {
305 2c8fb90b 2023-06-25 thomas event_del(&proc->iev.ev);
306 2c8fb90b 2023-06-25 thomas msgbuf_clear(&proc->iev.ibuf.w);
307 2c8fb90b 2023-06-25 thomas close(proc->iev.ibuf.fd);
310 62ee7d94 2023-01-10 thomas free(proc);
313 b993e8cc 2023-06-22 thomas static void
314 b993e8cc 2023-06-22 thomas kill_repo_proc(struct gotd_client *client)
316 b993e8cc 2023-06-22 thomas if (client->repo == NULL)
319 2c8fb90b 2023-06-25 thomas kill_proc(client->repo, 0);
320 b993e8cc 2023-06-22 thomas client->repo = NULL;
323 3efd8e31 2022-10-23 thomas static void
324 c669c489 2022-12-30 thomas kill_auth_proc(struct gotd_client *client)
326 c669c489 2022-12-30 thomas if (client->auth == NULL)
329 2c8fb90b 2023-06-25 thomas kill_proc(client->auth, 0);
330 c669c489 2022-12-30 thomas client->auth = NULL;
333 c669c489 2022-12-30 thomas static void
334 62ee7d94 2023-01-10 thomas kill_session_proc(struct gotd_client *client)
336 62ee7d94 2023-01-10 thomas if (client->session == NULL)
339 2c8fb90b 2023-06-25 thomas kill_proc(client->session, 0);
340 62ee7d94 2023-01-10 thomas client->session = NULL;
343 62ee7d94 2023-01-10 thomas static void
344 3efd8e31 2022-10-23 thomas disconnect(struct gotd_client *client)
346 3efd8e31 2022-10-23 thomas struct gotd_imsg_disconnect idisconnect;
347 78943464 2023-06-22 thomas struct gotd_child_proc *listen_proc = gotd.listen_proc;
348 3efd8e31 2022-10-23 thomas uint64_t slot;
350 3efd8e31 2022-10-23 thomas log_debug("uid %d: disconnecting", client->euid);
352 c669c489 2022-12-30 thomas kill_auth_proc(client);
353 62ee7d94 2023-01-10 thomas kill_session_proc(client);
354 b993e8cc 2023-06-22 thomas kill_repo_proc(client);
356 52939b68 2023-02-17 thomas idisconnect.client_id = client->id;
357 2b3d32a1 2022-12-30 thomas if (gotd_imsg_compose_event(&listen_proc->iev,
358 2b3d32a1 2022-12-30 thomas GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
359 2b3d32a1 2022-12-30 thomas &idisconnect, sizeof(idisconnect)) == -1)
360 2b3d32a1 2022-12-30 thomas log_warn("imsg compose DISCONNECT");
362 3efd8e31 2022-10-23 thomas slot = client_hash(client->id) % nitems(gotd_clients);
363 3efd8e31 2022-10-23 thomas STAILQ_REMOVE(&gotd_clients[slot], client, gotd_client, entry);
364 3efd8e31 2022-10-23 thomas imsg_clear(&client->iev.ibuf);
365 3efd8e31 2022-10-23 thomas event_del(&client->iev.ev);
366 3efd8e31 2022-10-23 thomas evtimer_del(&client->tmo);
367 62ee7d94 2023-01-10 thomas if (client->fd != -1)
368 62ee7d94 2023-01-10 thomas close(client->fd);
369 62ee7d94 2023-01-10 thomas else if (client->iev.ibuf.fd != -1)
370 62ee7d94 2023-01-10 thomas close(client->iev.ibuf.fd);
371 3efd8e31 2022-10-23 thomas free(client);
372 3efd8e31 2022-10-23 thomas client_cnt--;
375 3efd8e31 2022-10-23 thomas static void
376 3efd8e31 2022-10-23 thomas disconnect_on_error(struct gotd_client *client, const struct got_error *err)
378 3efd8e31 2022-10-23 thomas struct imsgbuf ibuf;
380 a6153ffb 2023-08-12 thomas if (err->code != GOT_ERR_EOF) {
381 a6153ffb 2023-08-12 thomas log_warnx("uid %d: %s", client->euid, err->msg);
382 a6153ffb 2023-08-12 thomas if (client->fd != -1) {
383 a6153ffb 2023-08-12 thomas imsg_init(&ibuf, client->fd);
384 a6153ffb 2023-08-12 thomas gotd_imsg_send_error(&ibuf, 0, PROC_GOTD, err);
385 a6153ffb 2023-08-12 thomas imsg_clear(&ibuf);
388 3efd8e31 2022-10-23 thomas disconnect(client);
391 c902213d 2022-10-29 thomas static const struct got_error *
392 c902213d 2022-10-29 thomas send_repo_info(struct gotd_imsgev *iev, struct gotd_repo *repo)
394 c902213d 2022-10-29 thomas const struct got_error *err = NULL;
395 c902213d 2022-10-29 thomas struct gotd_imsg_info_repo irepo;
397 c902213d 2022-10-29 thomas memset(&irepo, 0, sizeof(irepo));
399 c902213d 2022-10-29 thomas if (strlcpy(irepo.repo_name, repo->name, sizeof(irepo.repo_name))
400 c902213d 2022-10-29 thomas >= sizeof(irepo.repo_name))
401 c902213d 2022-10-29 thomas return got_error_msg(GOT_ERR_NO_SPACE, "repo name too long");
402 c902213d 2022-10-29 thomas if (strlcpy(irepo.repo_path, repo->path, sizeof(irepo.repo_path))
403 c902213d 2022-10-29 thomas >= sizeof(irepo.repo_path))
404 c902213d 2022-10-29 thomas return got_error_msg(GOT_ERR_NO_SPACE, "repo path too long");
406 c902213d 2022-10-29 thomas if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_REPO, PROC_GOTD, -1,
407 c902213d 2022-10-29 thomas &irepo, sizeof(irepo)) == -1) {
408 c902213d 2022-10-29 thomas err = got_error_from_errno("imsg compose INFO_REPO");
410 c902213d 2022-10-29 thomas return err;
413 c902213d 2022-10-29 thomas return NULL;
416 c902213d 2022-10-29 thomas static const struct got_error *
417 c902213d 2022-10-29 thomas send_client_info(struct gotd_imsgev *iev, struct gotd_client *client)
419 c902213d 2022-10-29 thomas const struct got_error *err = NULL;
420 c902213d 2022-10-29 thomas struct gotd_imsg_info_client iclient;
421 c902213d 2022-10-29 thomas struct gotd_child_proc *proc;
423 c902213d 2022-10-29 thomas memset(&iclient, 0, sizeof(iclient));
424 c902213d 2022-10-29 thomas iclient.euid = client->euid;
425 c902213d 2022-10-29 thomas iclient.egid = client->egid;
427 27b11d77 2023-01-14 thomas proc = client->repo;
428 c902213d 2022-10-29 thomas if (proc) {
429 414e37cb 2022-12-30 thomas if (strlcpy(iclient.repo_name, proc->repo_path,
430 c902213d 2022-10-29 thomas sizeof(iclient.repo_name)) >= sizeof(iclient.repo_name)) {
431 c902213d 2022-10-29 thomas return got_error_msg(GOT_ERR_NO_SPACE,
432 c902213d 2022-10-29 thomas "repo name too long");
434 c902213d 2022-10-29 thomas if (client_is_writing(client))
435 c902213d 2022-10-29 thomas iclient.is_writing = 1;
437 62ee7d94 2023-01-10 thomas iclient.repo_child_pid = proc->pid;
440 62ee7d94 2023-01-10 thomas if (client->session)
441 62ee7d94 2023-01-10 thomas iclient.session_child_pid = client->session->pid;
443 c902213d 2022-10-29 thomas if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_CLIENT, PROC_GOTD, -1,
444 c902213d 2022-10-29 thomas &iclient, sizeof(iclient)) == -1) {
445 c902213d 2022-10-29 thomas err = got_error_from_errno("imsg compose INFO_CLIENT");
447 c902213d 2022-10-29 thomas return err;
450 c902213d 2022-10-29 thomas return NULL;
453 c902213d 2022-10-29 thomas static const struct got_error *
454 c902213d 2022-10-29 thomas send_info(struct gotd_client *client)
456 c902213d 2022-10-29 thomas const struct got_error *err = NULL;
457 c902213d 2022-10-29 thomas struct gotd_imsg_info info;
458 c902213d 2022-10-29 thomas uint64_t slot;
459 c902213d 2022-10-29 thomas struct gotd_repo *repo;
461 c8cf6821 2023-01-06 thomas if (client->euid != 0)
462 c8cf6821 2023-01-06 thomas return got_error_set_errno(EPERM, "info");
464 c902213d 2022-10-29 thomas info.pid = gotd.pid;
465 c902213d 2022-10-29 thomas info.verbosity = gotd.verbosity;
466 c902213d 2022-10-29 thomas info.nrepos = gotd.nrepos;
467 c902213d 2022-10-29 thomas info.nclients = client_cnt - 1;
469 c902213d 2022-10-29 thomas if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_INFO, PROC_GOTD, -1,
470 c902213d 2022-10-29 thomas &info, sizeof(info)) == -1) {
471 c902213d 2022-10-29 thomas err = got_error_from_errno("imsg compose INFO");
473 c902213d 2022-10-29 thomas return err;
476 c902213d 2022-10-29 thomas TAILQ_FOREACH(repo, &gotd.repos, entry) {
477 c902213d 2022-10-29 thomas err = send_repo_info(&client->iev, repo);
479 c902213d 2022-10-29 thomas return err;
482 c902213d 2022-10-29 thomas for (slot = 0; slot < nitems(gotd_clients); slot++) {
483 c902213d 2022-10-29 thomas struct gotd_client *c;
484 c902213d 2022-10-29 thomas STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
485 c902213d 2022-10-29 thomas if (c->id == client->id)
486 c902213d 2022-10-29 thomas continue;
487 c902213d 2022-10-29 thomas err = send_client_info(&client->iev, c);
489 c902213d 2022-10-29 thomas return err;
493 c902213d 2022-10-29 thomas return NULL;
496 c902213d 2022-10-29 thomas static const struct got_error *
497 c902213d 2022-10-29 thomas stop_gotd(struct gotd_client *client)
500 c902213d 2022-10-29 thomas if (client->euid != 0)
501 c902213d 2022-10-29 thomas return got_error_set_errno(EPERM, "stop");
503 c902213d 2022-10-29 thomas gotd_shutdown();
504 c902213d 2022-10-29 thomas /* NOTREACHED */
505 729a7e24 2022-11-17 thomas return NULL;
508 3efd8e31 2022-10-23 thomas static const struct got_error *
509 62ee7d94 2023-01-10 thomas start_client_authentication(struct gotd_client *client, struct imsg *imsg)
511 3efd8e31 2022-10-23 thomas const struct got_error *err;
512 3efd8e31 2022-10-23 thomas struct gotd_imsg_list_refs ireq;
513 729a7e24 2022-11-17 thomas struct gotd_repo *repo = NULL;
514 3efd8e31 2022-10-23 thomas size_t datalen;
516 3efd8e31 2022-10-23 thomas log_debug("list-refs request from uid %d", client->euid);
518 7b1db75e 2023-01-14 thomas if (client->state != GOTD_CLIENT_STATE_NEW)
519 62ee7d94 2023-01-10 thomas return got_error_msg(GOT_ERR_BAD_REQUEST,
520 62ee7d94 2023-01-10 thomas "unexpected list-refs request received");
522 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
523 3efd8e31 2022-10-23 thomas if (datalen != sizeof(ireq))
524 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
526 3efd8e31 2022-10-23 thomas memcpy(&ireq, imsg->data, datalen);
528 3efd8e31 2022-10-23 thomas if (ireq.client_is_reading) {
529 3efd8e31 2022-10-23 thomas err = ensure_client_is_not_writing(client);
531 3efd8e31 2022-10-23 thomas return err;
532 5dcb3a43 2023-04-01 thomas repo = gotd_find_repo_by_name(ireq.repo_name, &gotd);
533 729a7e24 2022-11-17 thomas if (repo == NULL)
534 729a7e24 2022-11-17 thomas return got_error(GOT_ERR_NOT_GIT_REPO);
535 c669c489 2022-12-30 thomas err = start_auth_child(client, GOTD_AUTH_READ, repo,
536 85b37c72 2022-12-30 thomas gotd.argv0, gotd.confpath, gotd.daemonize,
537 85b37c72 2022-12-30 thomas gotd.verbosity);
539 85b37c72 2022-12-30 thomas return err;
541 3efd8e31 2022-10-23 thomas err = ensure_client_is_not_reading(client);
543 729a7e24 2022-11-17 thomas return err;
544 5dcb3a43 2023-04-01 thomas repo = gotd_find_repo_by_name(ireq.repo_name, &gotd);
545 729a7e24 2022-11-17 thomas if (repo == NULL)
546 729a7e24 2022-11-17 thomas return got_error(GOT_ERR_NOT_GIT_REPO);
547 c669c489 2022-12-30 thomas err = start_auth_child(client,
548 c669c489 2022-12-30 thomas GOTD_AUTH_READ | GOTD_AUTH_WRITE,
549 c669c489 2022-12-30 thomas repo, gotd.argv0, gotd.confpath, gotd.daemonize,
550 85b37c72 2022-12-30 thomas gotd.verbosity);
552 85b37c72 2022-12-30 thomas return err;
555 62ee7d94 2023-01-10 thomas evtimer_add(&client->tmo, &auth_timeout);
557 f7abcac2 2023-07-17 thomas /* Flow continues upon authentication success/failure or timeout. */
558 3efd8e31 2022-10-23 thomas return NULL;
561 3efd8e31 2022-10-23 thomas static void
562 3efd8e31 2022-10-23 thomas gotd_request(int fd, short events, void *arg)
564 3efd8e31 2022-10-23 thomas struct gotd_imsgev *iev = arg;
565 3efd8e31 2022-10-23 thomas struct imsgbuf *ibuf = &iev->ibuf;
566 3efd8e31 2022-10-23 thomas struct gotd_client *client = iev->handler_arg;
567 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
568 3efd8e31 2022-10-23 thomas struct imsg imsg;
569 3efd8e31 2022-10-23 thomas ssize_t n;
571 3efd8e31 2022-10-23 thomas if (events & EV_WRITE) {
572 3efd8e31 2022-10-23 thomas while (ibuf->w.queued) {
573 3efd8e31 2022-10-23 thomas n = msgbuf_write(&ibuf->w);
574 3efd8e31 2022-10-23 thomas if (n == -1 && errno == EPIPE) {
576 3efd8e31 2022-10-23 thomas * The client has closed its socket.
577 3efd8e31 2022-10-23 thomas * This can happen when Git clients are
578 3efd8e31 2022-10-23 thomas * done sending pack file data.
580 3efd8e31 2022-10-23 thomas msgbuf_clear(&ibuf->w);
581 3efd8e31 2022-10-23 thomas continue;
582 3efd8e31 2022-10-23 thomas } else if (n == -1 && errno != EAGAIN) {
583 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_flush");
584 3efd8e31 2022-10-23 thomas disconnect_on_error(client, err);
587 3efd8e31 2022-10-23 thomas if (n == 0) {
588 3efd8e31 2022-10-23 thomas /* Connection closed. */
589 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_EOF);
590 3efd8e31 2022-10-23 thomas disconnect_on_error(client, err);
595 c902213d 2022-10-29 thomas /* Disconnect gotctl(8) now that messages have been sent. */
596 c902213d 2022-10-29 thomas if (!client_is_reading(client) && !client_is_writing(client)) {
597 c902213d 2022-10-29 thomas disconnect(client);
602 3efd8e31 2022-10-23 thomas if ((events & EV_READ) == 0)
605 3efd8e31 2022-10-23 thomas memset(&imsg, 0, sizeof(imsg));
607 3efd8e31 2022-10-23 thomas while (err == NULL) {
608 3efd8e31 2022-10-23 thomas err = gotd_imsg_recv(&imsg, ibuf, 0);
609 3efd8e31 2022-10-23 thomas if (err) {
610 3efd8e31 2022-10-23 thomas if (err->code == GOT_ERR_PRIVSEP_READ)
611 3efd8e31 2022-10-23 thomas err = NULL;
615 3efd8e31 2022-10-23 thomas evtimer_del(&client->tmo);
617 3efd8e31 2022-10-23 thomas switch (imsg.hdr.type) {
618 c902213d 2022-10-29 thomas case GOTD_IMSG_INFO:
619 c902213d 2022-10-29 thomas err = send_info(client);
621 c902213d 2022-10-29 thomas case GOTD_IMSG_STOP:
622 c902213d 2022-10-29 thomas err = stop_gotd(client);
624 3efd8e31 2022-10-23 thomas case GOTD_IMSG_LIST_REFS:
625 62ee7d94 2023-01-10 thomas err = start_client_authentication(client, &imsg);
628 62ee7d94 2023-01-10 thomas log_debug("unexpected imsg %d", imsg.hdr.type);
629 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
633 3efd8e31 2022-10-23 thomas imsg_free(&imsg);
636 3efd8e31 2022-10-23 thomas if (err) {
637 f5f71a04 2023-01-23 thomas disconnect_on_error(client, err);
639 3efd8e31 2022-10-23 thomas gotd_imsg_event_add(&client->iev);
643 3efd8e31 2022-10-23 thomas static void
644 62ee7d94 2023-01-10 thomas gotd_auth_timeout(int fd, short events, void *arg)
646 3efd8e31 2022-10-23 thomas struct gotd_client *client = arg;
648 62ee7d94 2023-01-10 thomas log_debug("disconnecting uid %d due to authentication timeout",
649 62ee7d94 2023-01-10 thomas client->euid);
650 3efd8e31 2022-10-23 thomas disconnect(client);
653 2b3d32a1 2022-12-30 thomas static const struct got_error *
654 2b3d32a1 2022-12-30 thomas recv_connect(uint32_t *client_id, struct imsg *imsg)
656 2b3d32a1 2022-12-30 thomas const struct got_error *err = NULL;
657 2b3d32a1 2022-12-30 thomas struct gotd_imsg_connect iconnect;
658 2b3d32a1 2022-12-30 thomas size_t datalen;
659 3efd8e31 2022-10-23 thomas int s = -1;
660 3efd8e31 2022-10-23 thomas struct gotd_client *client = NULL;
662 2b3d32a1 2022-12-30 thomas *client_id = 0;
664 2b3d32a1 2022-12-30 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
665 2b3d32a1 2022-12-30 thomas if (datalen != sizeof(iconnect))
666 2b3d32a1 2022-12-30 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
667 2b3d32a1 2022-12-30 thomas memcpy(&iconnect, imsg->data, sizeof(iconnect));
669 2b3d32a1 2022-12-30 thomas s = imsg->fd;
670 3efd8e31 2022-10-23 thomas if (s == -1) {
671 2b3d32a1 2022-12-30 thomas err = got_error(GOT_ERR_PRIVSEP_NO_FD);
672 2b3d32a1 2022-12-30 thomas goto done;
675 2b3d32a1 2022-12-30 thomas if (find_client(iconnect.client_id)) {
676 2b3d32a1 2022-12-30 thomas err = got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
677 2b3d32a1 2022-12-30 thomas goto done;
680 3efd8e31 2022-10-23 thomas client = calloc(1, sizeof(*client));
681 3efd8e31 2022-10-23 thomas if (client == NULL) {
682 2b3d32a1 2022-12-30 thomas err = got_error_from_errno("calloc");
683 2b3d32a1 2022-12-30 thomas goto done;
686 2b3d32a1 2022-12-30 thomas *client_id = iconnect.client_id;
688 7b1db75e 2023-01-14 thomas client->state = GOTD_CLIENT_STATE_NEW;
689 2b3d32a1 2022-12-30 thomas client->id = iconnect.client_id;
690 3efd8e31 2022-10-23 thomas client->fd = s;
692 0bcde4c8 2022-12-30 thomas /* The auth process will verify UID/GID for us. */
693 0bcde4c8 2022-12-30 thomas client->euid = iconnect.euid;
694 0bcde4c8 2022-12-30 thomas client->egid = iconnect.egid;
696 3efd8e31 2022-10-23 thomas imsg_init(&client->iev.ibuf, client->fd);
697 3efd8e31 2022-10-23 thomas client->iev.handler = gotd_request;
698 3efd8e31 2022-10-23 thomas client->iev.events = EV_READ;
699 3efd8e31 2022-10-23 thomas client->iev.handler_arg = client;
701 3efd8e31 2022-10-23 thomas event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
702 3efd8e31 2022-10-23 thomas &client->iev);
703 3efd8e31 2022-10-23 thomas gotd_imsg_event_add(&client->iev);
705 62ee7d94 2023-01-10 thomas evtimer_set(&client->tmo, gotd_auth_timeout, client);
707 3efd8e31 2022-10-23 thomas add_client(client);
708 3efd8e31 2022-10-23 thomas log_debug("%s: new client uid %d connected on fd %d", __func__,
709 3efd8e31 2022-10-23 thomas client->euid, client->fd);
711 2b3d32a1 2022-12-30 thomas if (err) {
712 78943464 2023-06-22 thomas struct gotd_child_proc *listen_proc = gotd.listen_proc;
713 2b3d32a1 2022-12-30 thomas struct gotd_imsg_disconnect idisconnect;
715 2b3d32a1 2022-12-30 thomas idisconnect.client_id = client->id;
716 2b3d32a1 2022-12-30 thomas if (gotd_imsg_compose_event(&listen_proc->iev,
717 2b3d32a1 2022-12-30 thomas GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
718 2b3d32a1 2022-12-30 thomas &idisconnect, sizeof(idisconnect)) == -1)
719 2b3d32a1 2022-12-30 thomas log_warn("imsg compose DISCONNECT");
721 2b3d32a1 2022-12-30 thomas if (s != -1)
722 2b3d32a1 2022-12-30 thomas close(s);
725 2b3d32a1 2022-12-30 thomas return err;
728 3efd8e31 2022-10-23 thomas static const char *gotd_proc_names[PROC_MAX] = {
729 3efd8e31 2022-10-23 thomas "parent",
730 2b3d32a1 2022-12-30 thomas "listen",
732 844dda16 2023-06-22 thomas "session_read",
733 844dda16 2023-06-22 thomas "session_write",
734 3efd8e31 2022-10-23 thomas "repo_read",
735 f3807fe5 2023-07-10 thomas "repo_write",
736 f3807fe5 2023-07-10 thomas "gitwrapper"
739 3efd8e31 2022-10-23 thomas static void
740 3efd8e31 2022-10-23 thomas kill_proc(struct gotd_child_proc *proc, int fatal)
742 2c8fb90b 2023-06-25 thomas struct timeval tv = { 5, 0 };
744 2c8fb90b 2023-06-25 thomas log_debug("kill -%d %d", fatal ? SIGKILL : SIGTERM, proc->pid);
746 2c8fb90b 2023-06-25 thomas if (proc->iev.ibuf.fd != -1) {
747 2c8fb90b 2023-06-25 thomas event_del(&proc->iev.ev);
748 2c8fb90b 2023-06-25 thomas msgbuf_clear(&proc->iev.ibuf.w);
749 2c8fb90b 2023-06-25 thomas close(proc->iev.ibuf.fd);
750 2c8fb90b 2023-06-25 thomas proc->iev.ibuf.fd = -1;
753 2c8fb90b 2023-06-25 thomas if (!evtimer_pending(&proc->tmo, NULL) && !fatal)
754 2c8fb90b 2023-06-25 thomas evtimer_add(&proc->tmo, &tv);
756 3efd8e31 2022-10-23 thomas if (fatal) {
757 3efd8e31 2022-10-23 thomas log_warnx("sending SIGKILL to PID %d", proc->pid);
758 3efd8e31 2022-10-23 thomas kill(proc->pid, SIGKILL);
760 3efd8e31 2022-10-23 thomas kill(proc->pid, SIGTERM);
763 3efd8e31 2022-10-23 thomas static void
764 2c8fb90b 2023-06-25 thomas kill_proc_timeout(int fd, short ev, void *d)
766 2c8fb90b 2023-06-25 thomas struct gotd_child_proc *proc = d;
768 2c8fb90b 2023-06-25 thomas log_warnx("timeout waiting for PID %d to terminate;"
769 2c8fb90b 2023-06-25 thomas " retrying with force", proc->pid);
770 2c8fb90b 2023-06-25 thomas kill_proc(proc, 1);
773 2c8fb90b 2023-06-25 thomas static void
774 3efd8e31 2022-10-23 thomas gotd_shutdown(void)
776 85b37c72 2022-12-30 thomas uint64_t slot;
778 62ee7d94 2023-01-10 thomas log_debug("shutting down");
779 85b37c72 2022-12-30 thomas for (slot = 0; slot < nitems(gotd_clients); slot++) {
780 85b37c72 2022-12-30 thomas struct gotd_client *c, *tmp;
782 85b37c72 2022-12-30 thomas STAILQ_FOREACH_SAFE(c, &gotd_clients[slot], entry, tmp)
783 85b37c72 2022-12-30 thomas disconnect(c);
786 2c8fb90b 2023-06-25 thomas kill_proc(gotd.listen_proc, 0);
788 3efd8e31 2022-10-23 thomas log_info("terminating");
792 2c8fb90b 2023-06-25 thomas static struct gotd_child_proc *
793 2c8fb90b 2023-06-25 thomas find_proc_by_pid(pid_t pid)
795 2c8fb90b 2023-06-25 thomas struct gotd_child_proc *proc = NULL;
797 2c8fb90b 2023-06-25 thomas TAILQ_FOREACH(proc, &procs, entry)
798 2c8fb90b 2023-06-25 thomas if (proc->pid == pid)
801 2c8fb90b 2023-06-25 thomas return proc;
805 3efd8e31 2022-10-23 thomas gotd_sighdlr(int sig, short event, void *arg)
807 2c8fb90b 2023-06-25 thomas struct gotd_child_proc *proc;
808 2c8fb90b 2023-06-25 thomas pid_t pid;
809 2c8fb90b 2023-06-25 thomas int status;
812 3efd8e31 2022-10-23 thomas * Normal signal handler rules don't apply because libevent
813 3efd8e31 2022-10-23 thomas * decouples for us.
816 3efd8e31 2022-10-23 thomas switch (sig) {
817 3efd8e31 2022-10-23 thomas case SIGHUP:
818 3efd8e31 2022-10-23 thomas log_info("%s: ignoring SIGHUP", __func__);
820 3efd8e31 2022-10-23 thomas case SIGUSR1:
821 3efd8e31 2022-10-23 thomas log_info("%s: ignoring SIGUSR1", __func__);
823 3efd8e31 2022-10-23 thomas case SIGTERM:
824 3efd8e31 2022-10-23 thomas case SIGINT:
825 3efd8e31 2022-10-23 thomas gotd_shutdown();
827 2c8fb90b 2023-06-25 thomas case SIGCHLD:
828 2c8fb90b 2023-06-25 thomas for (;;) {
829 2c8fb90b 2023-06-25 thomas pid = waitpid(WAIT_ANY, &status, WNOHANG);
830 2c8fb90b 2023-06-25 thomas if (pid == -1) {
831 2c8fb90b 2023-06-25 thomas if (errno == EINTR)
832 2c8fb90b 2023-06-25 thomas continue;
833 2c8fb90b 2023-06-25 thomas if (errno == ECHILD)
835 2c8fb90b 2023-06-25 thomas fatal("waitpid");
837 2c8fb90b 2023-06-25 thomas if (pid == 0)
840 2c8fb90b 2023-06-25 thomas log_debug("reaped pid %d", pid);
841 2c8fb90b 2023-06-25 thomas proc = find_proc_by_pid(pid);
842 2c8fb90b 2023-06-25 thomas if (proc == NULL) {
843 2c8fb90b 2023-06-25 thomas log_info("caught exit of unknown child %d",
845 2c8fb90b 2023-06-25 thomas continue;
848 2c8fb90b 2023-06-25 thomas if (WIFSIGNALED(status)) {
849 2c8fb90b 2023-06-25 thomas log_warnx("child PID %d terminated with"
850 2c8fb90b 2023-06-25 thomas " signal %d", pid, WTERMSIG(status));
853 2c8fb90b 2023-06-25 thomas proc_done(proc);
857 3efd8e31 2022-10-23 thomas fatalx("unexpected signal");
861 3efd8e31 2022-10-23 thomas static const struct got_error *
862 3efd8e31 2022-10-23 thomas ensure_proc_is_reading(struct gotd_client *client,
863 3efd8e31 2022-10-23 thomas struct gotd_child_proc *proc)
865 3efd8e31 2022-10-23 thomas if (!client_is_reading(client)) {
866 3efd8e31 2022-10-23 thomas kill_proc(proc, 1);
867 3efd8e31 2022-10-23 thomas return got_error_fmt(GOT_ERR_BAD_PACKET,
868 3efd8e31 2022-10-23 thomas "PID %d handled a read-request for uid %d but this "
869 3efd8e31 2022-10-23 thomas "user is not reading from a repository", proc->pid,
870 3efd8e31 2022-10-23 thomas client->euid);
873 3efd8e31 2022-10-23 thomas return NULL;
876 3efd8e31 2022-10-23 thomas static const struct got_error *
877 3efd8e31 2022-10-23 thomas ensure_proc_is_writing(struct gotd_client *client,
878 3efd8e31 2022-10-23 thomas struct gotd_child_proc *proc)
880 3efd8e31 2022-10-23 thomas if (!client_is_writing(client)) {
881 3efd8e31 2022-10-23 thomas kill_proc(proc, 1);
882 3efd8e31 2022-10-23 thomas return got_error_fmt(GOT_ERR_BAD_PACKET,
883 3efd8e31 2022-10-23 thomas "PID %d handled a write-request for uid %d but this "
884 3efd8e31 2022-10-23 thomas "user is not writing to a repository", proc->pid,
885 3efd8e31 2022-10-23 thomas client->euid);
888 3efd8e31 2022-10-23 thomas return NULL;
891 3efd8e31 2022-10-23 thomas static int
892 3efd8e31 2022-10-23 thomas verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
893 3efd8e31 2022-10-23 thomas struct imsg *imsg)
895 3efd8e31 2022-10-23 thomas const struct got_error *err;
896 3efd8e31 2022-10-23 thomas int ret = 0;
898 2b3d32a1 2022-12-30 thomas if (proc->type == PROC_REPO_READ || proc->type == PROC_REPO_WRITE) {
899 27b11d77 2023-01-14 thomas if (client->repo == NULL)
900 2b3d32a1 2022-12-30 thomas fatalx("no process found for uid %d", client->euid);
901 27b11d77 2023-01-14 thomas if (proc->pid != client->repo->pid) {
902 2b3d32a1 2022-12-30 thomas kill_proc(proc, 1);
903 2b3d32a1 2022-12-30 thomas log_warnx("received message from PID %d for uid %d, "
904 2b3d32a1 2022-12-30 thomas "while PID %d is the process serving this user",
905 27b11d77 2023-01-14 thomas proc->pid, client->euid, client->repo->pid);
906 2b3d32a1 2022-12-30 thomas return 0;
909 7fed8fa4 2023-06-22 thomas if (proc->type == PROC_SESSION_READ ||
910 7fed8fa4 2023-06-22 thomas proc->type == PROC_SESSION_WRITE) {
911 62ee7d94 2023-01-10 thomas if (client->session == NULL) {
912 62ee7d94 2023-01-10 thomas log_warnx("no session found for uid %d", client->euid);
913 62ee7d94 2023-01-10 thomas return 0;
915 62ee7d94 2023-01-10 thomas if (proc->pid != client->session->pid) {
916 62ee7d94 2023-01-10 thomas kill_proc(proc, 1);
917 62ee7d94 2023-01-10 thomas log_warnx("received message from PID %d for uid %d, "
918 62ee7d94 2023-01-10 thomas "while PID %d is the process serving this user",
919 62ee7d94 2023-01-10 thomas proc->pid, client->euid, client->session->pid);
920 62ee7d94 2023-01-10 thomas return 0;
924 3efd8e31 2022-10-23 thomas switch (imsg->hdr.type) {
925 3efd8e31 2022-10-23 thomas case GOTD_IMSG_ERROR:
928 2b3d32a1 2022-12-30 thomas case GOTD_IMSG_CONNECT:
929 2b3d32a1 2022-12-30 thomas if (proc->type != PROC_LISTEN) {
930 2b3d32a1 2022-12-30 thomas err = got_error_fmt(GOT_ERR_BAD_PACKET,
931 2b3d32a1 2022-12-30 thomas "new connection for uid %d from PID %d "
932 2b3d32a1 2022-12-30 thomas "which is not the listen process",
933 c669c489 2022-12-30 thomas proc->pid, client->euid);
937 c669c489 2022-12-30 thomas case GOTD_IMSG_ACCESS_GRANTED:
938 c669c489 2022-12-30 thomas if (proc->type != PROC_AUTH) {
939 c669c489 2022-12-30 thomas err = got_error_fmt(GOT_ERR_BAD_PACKET,
940 c669c489 2022-12-30 thomas "authentication of uid %d from PID %d "
941 c669c489 2022-12-30 thomas "which is not the auth process",
942 2b3d32a1 2022-12-30 thomas proc->pid, client->euid);
946 62ee7d94 2023-01-10 thomas case GOTD_IMSG_CLIENT_SESSION_READY:
947 7fed8fa4 2023-06-22 thomas if (proc->type != PROC_SESSION_READ &&
948 7fed8fa4 2023-06-22 thomas proc->type != PROC_SESSION_WRITE) {
949 62ee7d94 2023-01-10 thomas err = got_error_fmt(GOT_ERR_BAD_PACKET,
950 62ee7d94 2023-01-10 thomas "unexpected \"ready\" signal from PID %d",
951 62ee7d94 2023-01-10 thomas proc->pid);
955 85b37c72 2022-12-30 thomas case GOTD_IMSG_REPO_CHILD_READY:
956 85b37c72 2022-12-30 thomas if (proc->type != PROC_REPO_READ &&
957 85b37c72 2022-12-30 thomas proc->type != PROC_REPO_WRITE) {
958 85b37c72 2022-12-30 thomas err = got_error_fmt(GOT_ERR_BAD_PACKET,
959 85b37c72 2022-12-30 thomas "unexpected \"ready\" signal from PID %d",
960 85b37c72 2022-12-30 thomas proc->pid);
964 3efd8e31 2022-10-23 thomas case GOTD_IMSG_PACKFILE_DONE:
965 3efd8e31 2022-10-23 thomas err = ensure_proc_is_reading(client, proc);
967 3efd8e31 2022-10-23 thomas log_warnx("uid %d: %s", client->euid, err->msg);
971 3efd8e31 2022-10-23 thomas case GOTD_IMSG_PACKFILE_INSTALL:
972 3efd8e31 2022-10-23 thomas case GOTD_IMSG_REF_UPDATES_START:
973 3efd8e31 2022-10-23 thomas case GOTD_IMSG_REF_UPDATE:
974 3efd8e31 2022-10-23 thomas err = ensure_proc_is_writing(client, proc);
976 3efd8e31 2022-10-23 thomas log_warnx("uid %d: %s", client->euid, err->msg);
981 3efd8e31 2022-10-23 thomas log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
985 3efd8e31 2022-10-23 thomas return ret;
988 3efd8e31 2022-10-23 thomas static const struct got_error *
989 62ee7d94 2023-01-10 thomas connect_repo_child(struct gotd_client *client,
990 62ee7d94 2023-01-10 thomas struct gotd_child_proc *repo_proc)
992 85b37c72 2022-12-30 thomas static const struct got_error *err;
993 62ee7d94 2023-01-10 thomas struct gotd_imsgev *session_iev = &client->session->iev;
994 62ee7d94 2023-01-10 thomas struct gotd_imsg_connect_repo_child ireq;
995 62ee7d94 2023-01-10 thomas int pipe[2];
997 7b1db75e 2023-01-14 thomas if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED)
998 62ee7d94 2023-01-10 thomas return got_error_msg(GOT_ERR_BAD_REQUEST,
999 62ee7d94 2023-01-10 thomas "unexpected repo child ready signal received");
1001 62ee7d94 2023-01-10 thomas if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1002 62ee7d94 2023-01-10 thomas PF_UNSPEC, pipe) == -1)
1003 62ee7d94 2023-01-10 thomas fatal("socketpair");
1005 62ee7d94 2023-01-10 thomas memset(&ireq, 0, sizeof(ireq));
1006 62ee7d94 2023-01-10 thomas ireq.client_id = client->id;
1007 62ee7d94 2023-01-10 thomas ireq.proc_id = repo_proc->type;
1009 62ee7d94 2023-01-10 thomas /* Pass repo child pipe to session child process. */
1010 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(session_iev, GOTD_IMSG_CONNECT_REPO_CHILD,
1011 62ee7d94 2023-01-10 thomas PROC_GOTD, pipe[0], &ireq, sizeof(ireq)) == -1) {
1012 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
1013 62ee7d94 2023-01-10 thomas close(pipe[0]);
1014 62ee7d94 2023-01-10 thomas close(pipe[1]);
1015 62ee7d94 2023-01-10 thomas return err;
1018 62ee7d94 2023-01-10 thomas /* Pass session child pipe to repo child process. */
1019 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&repo_proc->iev,
1020 62ee7d94 2023-01-10 thomas GOTD_IMSG_CONNECT_REPO_CHILD, PROC_GOTD, pipe[1], NULL, 0) == -1) {
1021 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
1022 62ee7d94 2023-01-10 thomas close(pipe[1]);
1023 62ee7d94 2023-01-10 thomas return err;
1026 3efd8e31 2022-10-23 thomas return NULL;
1029 3efd8e31 2022-10-23 thomas static void
1030 85b37c72 2022-12-30 thomas gotd_dispatch_listener(int fd, short event, void *arg)
1032 3efd8e31 2022-10-23 thomas struct gotd_imsgev *iev = arg;
1033 3efd8e31 2022-10-23 thomas struct imsgbuf *ibuf = &iev->ibuf;
1034 78943464 2023-06-22 thomas struct gotd_child_proc *proc = gotd.listen_proc;
1035 85b37c72 2022-12-30 thomas ssize_t n;
1036 85b37c72 2022-12-30 thomas int shut = 0;
1037 85b37c72 2022-12-30 thomas struct imsg imsg;
1039 85b37c72 2022-12-30 thomas if (proc->iev.ibuf.fd != fd)
1040 85b37c72 2022-12-30 thomas fatalx("%s: unexpected fd %d", __func__, fd);
1042 85b37c72 2022-12-30 thomas if (event & EV_READ) {
1043 85b37c72 2022-12-30 thomas if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1044 85b37c72 2022-12-30 thomas fatal("imsg_read error");
1045 85b37c72 2022-12-30 thomas if (n == 0) {
1046 85b37c72 2022-12-30 thomas /* Connection closed. */
1047 85b37c72 2022-12-30 thomas shut = 1;
1048 85b37c72 2022-12-30 thomas goto done;
1052 85b37c72 2022-12-30 thomas if (event & EV_WRITE) {
1053 85b37c72 2022-12-30 thomas n = msgbuf_write(&ibuf->w);
1054 85b37c72 2022-12-30 thomas if (n == -1 && errno != EAGAIN)
1055 85b37c72 2022-12-30 thomas fatal("msgbuf_write");
1056 85b37c72 2022-12-30 thomas if (n == 0) {
1057 85b37c72 2022-12-30 thomas /* Connection closed. */
1058 85b37c72 2022-12-30 thomas shut = 1;
1059 85b37c72 2022-12-30 thomas goto done;
1063 85b37c72 2022-12-30 thomas for (;;) {
1064 85b37c72 2022-12-30 thomas const struct got_error *err = NULL;
1065 85b37c72 2022-12-30 thomas struct gotd_client *client = NULL;
1066 85b37c72 2022-12-30 thomas uint32_t client_id = 0;
1067 85b37c72 2022-12-30 thomas int do_disconnect = 0;
1069 85b37c72 2022-12-30 thomas if ((n = imsg_get(ibuf, &imsg)) == -1)
1070 85b37c72 2022-12-30 thomas fatal("%s: imsg_get error", __func__);
1071 85b37c72 2022-12-30 thomas if (n == 0) /* No more messages. */
1074 85b37c72 2022-12-30 thomas switch (imsg.hdr.type) {
1075 85b37c72 2022-12-30 thomas case GOTD_IMSG_ERROR:
1076 85b37c72 2022-12-30 thomas do_disconnect = 1;
1077 85b37c72 2022-12-30 thomas err = gotd_imsg_recv_error(&client_id, &imsg);
1079 85b37c72 2022-12-30 thomas case GOTD_IMSG_CONNECT:
1080 85b37c72 2022-12-30 thomas err = recv_connect(&client_id, &imsg);
1082 85b37c72 2022-12-30 thomas default:
1083 85b37c72 2022-12-30 thomas log_debug("unexpected imsg %d", imsg.hdr.type);
1087 85b37c72 2022-12-30 thomas client = find_client(client_id);
1088 85b37c72 2022-12-30 thomas if (client == NULL) {
1089 85b37c72 2022-12-30 thomas log_warnx("%s: client not found", __func__);
1090 85b37c72 2022-12-30 thomas imsg_free(&imsg);
1091 85b37c72 2022-12-30 thomas continue;
1094 85b37c72 2022-12-30 thomas if (err)
1095 85b37c72 2022-12-30 thomas log_warnx("uid %d: %s", client->euid, err->msg);
1097 85b37c72 2022-12-30 thomas if (do_disconnect) {
1098 85b37c72 2022-12-30 thomas if (err)
1099 85b37c72 2022-12-30 thomas disconnect_on_error(client, err);
1101 85b37c72 2022-12-30 thomas disconnect(client);
1104 85b37c72 2022-12-30 thomas imsg_free(&imsg);
1107 85b37c72 2022-12-30 thomas if (!shut) {
1108 85b37c72 2022-12-30 thomas gotd_imsg_event_add(iev);
1109 85b37c72 2022-12-30 thomas } else {
1110 85b37c72 2022-12-30 thomas /* This pipe is dead. Remove its event handler */
1111 85b37c72 2022-12-30 thomas event_del(&iev->ev);
1112 85b37c72 2022-12-30 thomas event_loopexit(NULL);
1116 85b37c72 2022-12-30 thomas static void
1117 c669c489 2022-12-30 thomas gotd_dispatch_auth_child(int fd, short event, void *arg)
1119 c669c489 2022-12-30 thomas const struct got_error *err = NULL;
1120 c669c489 2022-12-30 thomas struct gotd_imsgev *iev = arg;
1121 c669c489 2022-12-30 thomas struct imsgbuf *ibuf = &iev->ibuf;
1122 c669c489 2022-12-30 thomas struct gotd_client *client;
1123 c669c489 2022-12-30 thomas struct gotd_repo *repo = NULL;
1124 c669c489 2022-12-30 thomas ssize_t n;
1125 c669c489 2022-12-30 thomas int shut = 0;
1126 c669c489 2022-12-30 thomas struct imsg imsg;
1127 c669c489 2022-12-30 thomas uint32_t client_id = 0;
1128 c669c489 2022-12-30 thomas int do_disconnect = 0;
1130 c669c489 2022-12-30 thomas client = find_client_by_proc_fd(fd);
1131 b7acbe65 2023-02-17 thomas if (client == NULL) {
1132 b7acbe65 2023-02-17 thomas /* Can happen during process teardown. */
1133 b7acbe65 2023-02-17 thomas warnx("cannot find client for fd %d", fd);
1134 b7acbe65 2023-02-17 thomas shut = 1;
1135 b7acbe65 2023-02-17 thomas goto done;
1138 c669c489 2022-12-30 thomas if (client->auth == NULL)
1139 c669c489 2022-12-30 thomas fatalx("cannot find auth child process for fd %d", fd);
1141 c669c489 2022-12-30 thomas if (event & EV_READ) {
1142 c669c489 2022-12-30 thomas if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1143 c669c489 2022-12-30 thomas fatal("imsg_read error");
1144 c669c489 2022-12-30 thomas if (n == 0) {
1145 c669c489 2022-12-30 thomas /* Connection closed. */
1146 c669c489 2022-12-30 thomas shut = 1;
1147 c669c489 2022-12-30 thomas goto done;
1151 c669c489 2022-12-30 thomas if (event & EV_WRITE) {
1152 c669c489 2022-12-30 thomas n = msgbuf_write(&ibuf->w);
1153 c669c489 2022-12-30 thomas if (n == -1 && errno != EAGAIN)
1154 c669c489 2022-12-30 thomas fatal("msgbuf_write");
1155 c669c489 2022-12-30 thomas if (n == 0) {
1156 c669c489 2022-12-30 thomas /* Connection closed. */
1157 c669c489 2022-12-30 thomas shut = 1;
1159 c669c489 2022-12-30 thomas goto done;
1162 c669c489 2022-12-30 thomas if (client->auth->iev.ibuf.fd != fd)
1163 c669c489 2022-12-30 thomas fatalx("%s: unexpected fd %d", __func__, fd);
1165 c669c489 2022-12-30 thomas if ((n = imsg_get(ibuf, &imsg)) == -1)
1166 c669c489 2022-12-30 thomas fatal("%s: imsg_get error", __func__);
1167 c669c489 2022-12-30 thomas if (n == 0) /* No more messages. */
1170 c669c489 2022-12-30 thomas evtimer_del(&client->tmo);
1172 c669c489 2022-12-30 thomas switch (imsg.hdr.type) {
1173 c669c489 2022-12-30 thomas case GOTD_IMSG_ERROR:
1174 c669c489 2022-12-30 thomas do_disconnect = 1;
1175 c669c489 2022-12-30 thomas err = gotd_imsg_recv_error(&client_id, &imsg);
1177 c669c489 2022-12-30 thomas case GOTD_IMSG_ACCESS_GRANTED:
1178 7b1db75e 2023-01-14 thomas client->state = GOTD_CLIENT_STATE_ACCESS_GRANTED;
1180 c669c489 2022-12-30 thomas default:
1181 c669c489 2022-12-30 thomas do_disconnect = 1;
1182 c669c489 2022-12-30 thomas log_debug("unexpected imsg %d", imsg.hdr.type);
1186 c669c489 2022-12-30 thomas if (!verify_imsg_src(client, client->auth, &imsg)) {
1187 c669c489 2022-12-30 thomas do_disconnect = 1;
1188 c669c489 2022-12-30 thomas log_debug("dropping imsg type %d from PID %d",
1189 c669c489 2022-12-30 thomas imsg.hdr.type, client->auth->pid);
1191 c669c489 2022-12-30 thomas imsg_free(&imsg);
1193 c669c489 2022-12-30 thomas if (do_disconnect) {
1194 c669c489 2022-12-30 thomas if (err)
1195 c669c489 2022-12-30 thomas disconnect_on_error(client, err);
1197 c669c489 2022-12-30 thomas disconnect(client);
1201 5dcb3a43 2023-04-01 thomas repo = gotd_find_repo_by_name(client->auth->repo_name, &gotd);
1202 c669c489 2022-12-30 thomas if (repo == NULL) {
1203 c669c489 2022-12-30 thomas err = got_error(GOT_ERR_NOT_GIT_REPO);
1204 c669c489 2022-12-30 thomas goto done;
1206 c669c489 2022-12-30 thomas kill_auth_proc(client);
1208 e17294f7 2023-01-27 thomas log_info("authenticated uid %d for repository %s",
1209 c669c489 2022-12-30 thomas client->euid, repo->name);
1211 62ee7d94 2023-01-10 thomas err = start_session_child(client, repo, gotd.argv0,
1212 46ecc01f 2022-12-30 thomas gotd.confpath, gotd.daemonize, gotd.verbosity);
1213 62ee7d94 2023-01-10 thomas if (err)
1214 62ee7d94 2023-01-10 thomas goto done;
1216 c669c489 2022-12-30 thomas if (err)
1217 c669c489 2022-12-30 thomas log_warnx("uid %d: %s", client->euid, err->msg);
1219 c669c489 2022-12-30 thomas /* We might have killed the auth process by now. */
1220 c669c489 2022-12-30 thomas if (client->auth != NULL) {
1221 c669c489 2022-12-30 thomas if (!shut) {
1222 c669c489 2022-12-30 thomas gotd_imsg_event_add(iev);
1223 c669c489 2022-12-30 thomas } else {
1224 c669c489 2022-12-30 thomas /* This pipe is dead. Remove its event handler */
1225 c669c489 2022-12-30 thomas event_del(&iev->ev);
1230 62ee7d94 2023-01-10 thomas static const struct got_error *
1231 62ee7d94 2023-01-10 thomas connect_session(struct gotd_client *client)
1233 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
1234 62ee7d94 2023-01-10 thomas struct gotd_imsg_connect iconnect;
1237 62ee7d94 2023-01-10 thomas memset(&iconnect, 0, sizeof(iconnect));
1239 62ee7d94 2023-01-10 thomas s = dup(client->fd);
1240 62ee7d94 2023-01-10 thomas if (s == -1)
1241 62ee7d94 2023-01-10 thomas return got_error_from_errno("dup");
1243 62ee7d94 2023-01-10 thomas iconnect.client_id = client->id;
1244 62ee7d94 2023-01-10 thomas iconnect.euid = client->euid;
1245 62ee7d94 2023-01-10 thomas iconnect.egid = client->egid;
1247 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&client->session->iev, GOTD_IMSG_CONNECT,
1248 62ee7d94 2023-01-10 thomas PROC_GOTD, s, &iconnect, sizeof(iconnect)) == -1) {
1249 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose CONNECT");
1250 62ee7d94 2023-01-10 thomas close(s);
1251 62ee7d94 2023-01-10 thomas return err;
1255 62ee7d94 2023-01-10 thomas * We are no longer interested in messages from this client.
1256 62ee7d94 2023-01-10 thomas * Further client requests will be handled by the session process.
1258 62ee7d94 2023-01-10 thomas msgbuf_clear(&client->iev.ibuf.w);
1259 62ee7d94 2023-01-10 thomas imsg_clear(&client->iev.ibuf);
1260 62ee7d94 2023-01-10 thomas event_del(&client->iev.ev);
1261 62ee7d94 2023-01-10 thomas client->fd = -1; /* will be closed via copy in client->iev.ibuf.fd */
1263 62ee7d94 2023-01-10 thomas return NULL;
1266 c669c489 2022-12-30 thomas static void
1267 62ee7d94 2023-01-10 thomas gotd_dispatch_client_session(int fd, short event, void *arg)
1269 85b37c72 2022-12-30 thomas struct gotd_imsgev *iev = arg;
1270 85b37c72 2022-12-30 thomas struct imsgbuf *ibuf = &iev->ibuf;
1271 3efd8e31 2022-10-23 thomas struct gotd_child_proc *proc = NULL;
1272 85b37c72 2022-12-30 thomas struct gotd_client *client = NULL;
1273 3efd8e31 2022-10-23 thomas ssize_t n;
1274 3efd8e31 2022-10-23 thomas int shut = 0;
1275 3efd8e31 2022-10-23 thomas struct imsg imsg;
1277 62ee7d94 2023-01-10 thomas client = find_client_by_proc_fd(fd);
1278 b7acbe65 2023-02-17 thomas if (client == NULL) {
1279 b7acbe65 2023-02-17 thomas /* Can happen during process teardown. */
1280 b7acbe65 2023-02-17 thomas warnx("cannot find client for fd %d", fd);
1281 b7acbe65 2023-02-17 thomas shut = 1;
1282 b7acbe65 2023-02-17 thomas goto done;
1285 3efd8e31 2022-10-23 thomas if (event & EV_READ) {
1286 3efd8e31 2022-10-23 thomas if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1287 3efd8e31 2022-10-23 thomas fatal("imsg_read error");
1288 3efd8e31 2022-10-23 thomas if (n == 0) {
1289 3efd8e31 2022-10-23 thomas /* Connection closed. */
1290 3efd8e31 2022-10-23 thomas shut = 1;
1291 3efd8e31 2022-10-23 thomas goto done;
1295 3efd8e31 2022-10-23 thomas if (event & EV_WRITE) {
1296 3efd8e31 2022-10-23 thomas n = msgbuf_write(&ibuf->w);
1297 3efd8e31 2022-10-23 thomas if (n == -1 && errno != EAGAIN)
1298 3efd8e31 2022-10-23 thomas fatal("msgbuf_write");
1299 3efd8e31 2022-10-23 thomas if (n == 0) {
1300 3efd8e31 2022-10-23 thomas /* Connection closed. */
1301 3efd8e31 2022-10-23 thomas shut = 1;
1302 3efd8e31 2022-10-23 thomas goto done;
1306 62ee7d94 2023-01-10 thomas proc = client->session;
1307 62ee7d94 2023-01-10 thomas if (proc == NULL)
1308 62ee7d94 2023-01-10 thomas fatalx("cannot find session child process for fd %d", fd);
1310 62ee7d94 2023-01-10 thomas for (;;) {
1311 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
1312 62ee7d94 2023-01-10 thomas uint32_t client_id = 0;
1313 62ee7d94 2023-01-10 thomas int do_disconnect = 0, do_start_repo_child = 0;
1315 62ee7d94 2023-01-10 thomas if ((n = imsg_get(ibuf, &imsg)) == -1)
1316 62ee7d94 2023-01-10 thomas fatal("%s: imsg_get error", __func__);
1317 62ee7d94 2023-01-10 thomas if (n == 0) /* No more messages. */
1320 62ee7d94 2023-01-10 thomas switch (imsg.hdr.type) {
1321 62ee7d94 2023-01-10 thomas case GOTD_IMSG_ERROR:
1322 62ee7d94 2023-01-10 thomas do_disconnect = 1;
1323 62ee7d94 2023-01-10 thomas err = gotd_imsg_recv_error(&client_id, &imsg);
1325 62ee7d94 2023-01-10 thomas case GOTD_IMSG_CLIENT_SESSION_READY:
1326 7b1db75e 2023-01-14 thomas if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED) {
1327 62ee7d94 2023-01-10 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
1330 62ee7d94 2023-01-10 thomas do_start_repo_child = 1;
1332 62ee7d94 2023-01-10 thomas case GOTD_IMSG_DISCONNECT:
1333 62ee7d94 2023-01-10 thomas do_disconnect = 1;
1335 62ee7d94 2023-01-10 thomas default:
1336 62ee7d94 2023-01-10 thomas log_debug("unexpected imsg %d", imsg.hdr.type);
1340 62ee7d94 2023-01-10 thomas if (!verify_imsg_src(client, proc, &imsg)) {
1341 62ee7d94 2023-01-10 thomas log_debug("dropping imsg type %d from PID %d",
1342 62ee7d94 2023-01-10 thomas imsg.hdr.type, proc->pid);
1343 62ee7d94 2023-01-10 thomas imsg_free(&imsg);
1344 62ee7d94 2023-01-10 thomas continue;
1346 62ee7d94 2023-01-10 thomas if (err)
1347 62ee7d94 2023-01-10 thomas log_warnx("uid %d: %s", client->euid, err->msg);
1349 62ee7d94 2023-01-10 thomas if (do_start_repo_child) {
1350 62ee7d94 2023-01-10 thomas struct gotd_repo *repo;
1351 5dcb3a43 2023-04-01 thomas const char *name = client->session->repo_name;
1353 5dcb3a43 2023-04-01 thomas repo = gotd_find_repo_by_name(name, &gotd);
1354 62ee7d94 2023-01-10 thomas if (repo != NULL) {
1355 62ee7d94 2023-01-10 thomas enum gotd_procid proc_type;
1357 62ee7d94 2023-01-10 thomas if (client->required_auth & GOTD_AUTH_WRITE)
1358 62ee7d94 2023-01-10 thomas proc_type = PROC_REPO_WRITE;
1360 62ee7d94 2023-01-10 thomas proc_type = PROC_REPO_READ;
1362 62ee7d94 2023-01-10 thomas err = start_repo_child(client, proc_type, repo,
1363 62ee7d94 2023-01-10 thomas gotd.argv0, gotd.confpath, gotd.daemonize,
1364 62ee7d94 2023-01-10 thomas gotd.verbosity);
1366 62ee7d94 2023-01-10 thomas err = got_error(GOT_ERR_NOT_GIT_REPO);
1368 62ee7d94 2023-01-10 thomas if (err) {
1369 62ee7d94 2023-01-10 thomas log_warnx("uid %d: %s", client->euid, err->msg);
1370 62ee7d94 2023-01-10 thomas do_disconnect = 1;
1374 62ee7d94 2023-01-10 thomas if (do_disconnect) {
1375 62ee7d94 2023-01-10 thomas if (err)
1376 62ee7d94 2023-01-10 thomas disconnect_on_error(client, err);
1378 62ee7d94 2023-01-10 thomas disconnect(client);
1381 62ee7d94 2023-01-10 thomas imsg_free(&imsg);
1384 62ee7d94 2023-01-10 thomas if (!shut) {
1385 62ee7d94 2023-01-10 thomas gotd_imsg_event_add(iev);
1386 62ee7d94 2023-01-10 thomas } else {
1387 62ee7d94 2023-01-10 thomas /* This pipe is dead. Remove its event handler */
1388 62ee7d94 2023-01-10 thomas event_del(&iev->ev);
1389 62ee7d94 2023-01-10 thomas disconnect(client);
1393 62ee7d94 2023-01-10 thomas static void
1394 62ee7d94 2023-01-10 thomas gotd_dispatch_repo_child(int fd, short event, void *arg)
1396 62ee7d94 2023-01-10 thomas struct gotd_imsgev *iev = arg;
1397 62ee7d94 2023-01-10 thomas struct imsgbuf *ibuf = &iev->ibuf;
1398 62ee7d94 2023-01-10 thomas struct gotd_child_proc *proc = NULL;
1399 62ee7d94 2023-01-10 thomas struct gotd_client *client;
1400 62ee7d94 2023-01-10 thomas ssize_t n;
1401 62ee7d94 2023-01-10 thomas int shut = 0;
1402 62ee7d94 2023-01-10 thomas struct imsg imsg;
1404 85b37c72 2022-12-30 thomas client = find_client_by_proc_fd(fd);
1405 b7acbe65 2023-02-17 thomas if (client == NULL) {
1406 b7acbe65 2023-02-17 thomas /* Can happen during process teardown. */
1407 b7acbe65 2023-02-17 thomas warnx("cannot find client for fd %d", fd);
1408 b7acbe65 2023-02-17 thomas shut = 1;
1409 b7acbe65 2023-02-17 thomas goto done;
1412 62ee7d94 2023-01-10 thomas if (event & EV_READ) {
1413 62ee7d94 2023-01-10 thomas if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1414 62ee7d94 2023-01-10 thomas fatal("imsg_read error");
1415 62ee7d94 2023-01-10 thomas if (n == 0) {
1416 62ee7d94 2023-01-10 thomas /* Connection closed. */
1417 62ee7d94 2023-01-10 thomas shut = 1;
1418 62ee7d94 2023-01-10 thomas goto done;
1422 62ee7d94 2023-01-10 thomas if (event & EV_WRITE) {
1423 62ee7d94 2023-01-10 thomas n = msgbuf_write(&ibuf->w);
1424 62ee7d94 2023-01-10 thomas if (n == -1 && errno != EAGAIN)
1425 62ee7d94 2023-01-10 thomas fatal("msgbuf_write");
1426 62ee7d94 2023-01-10 thomas if (n == 0) {
1427 62ee7d94 2023-01-10 thomas /* Connection closed. */
1428 62ee7d94 2023-01-10 thomas shut = 1;
1429 62ee7d94 2023-01-10 thomas goto done;
1433 27b11d77 2023-01-14 thomas proc = client->repo;
1434 3efd8e31 2022-10-23 thomas if (proc == NULL)
1435 3efd8e31 2022-10-23 thomas fatalx("cannot find child process for fd %d", fd);
1437 3efd8e31 2022-10-23 thomas for (;;) {
1438 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
1439 3efd8e31 2022-10-23 thomas uint32_t client_id = 0;
1440 3efd8e31 2022-10-23 thomas int do_disconnect = 0;
1442 3efd8e31 2022-10-23 thomas if ((n = imsg_get(ibuf, &imsg)) == -1)
1443 3efd8e31 2022-10-23 thomas fatal("%s: imsg_get error", __func__);
1444 3efd8e31 2022-10-23 thomas if (n == 0) /* No more messages. */
1447 3efd8e31 2022-10-23 thomas switch (imsg.hdr.type) {
1448 3efd8e31 2022-10-23 thomas case GOTD_IMSG_ERROR:
1449 3efd8e31 2022-10-23 thomas do_disconnect = 1;
1450 3efd8e31 2022-10-23 thomas err = gotd_imsg_recv_error(&client_id, &imsg);
1452 85b37c72 2022-12-30 thomas case GOTD_IMSG_REPO_CHILD_READY:
1453 62ee7d94 2023-01-10 thomas err = connect_session(client);
1454 62ee7d94 2023-01-10 thomas if (err)
1456 62ee7d94 2023-01-10 thomas err = connect_repo_child(client, proc);
1458 3efd8e31 2022-10-23 thomas default:
1459 3efd8e31 2022-10-23 thomas log_debug("unexpected imsg %d", imsg.hdr.type);
1463 3efd8e31 2022-10-23 thomas if (!verify_imsg_src(client, proc, &imsg)) {
1464 3efd8e31 2022-10-23 thomas log_debug("dropping imsg type %d from PID %d",
1465 3efd8e31 2022-10-23 thomas imsg.hdr.type, proc->pid);
1466 3efd8e31 2022-10-23 thomas imsg_free(&imsg);
1467 3efd8e31 2022-10-23 thomas continue;
1469 3efd8e31 2022-10-23 thomas if (err)
1470 3efd8e31 2022-10-23 thomas log_warnx("uid %d: %s", client->euid, err->msg);
1472 3efd8e31 2022-10-23 thomas if (do_disconnect) {
1473 3efd8e31 2022-10-23 thomas if (err)
1474 3efd8e31 2022-10-23 thomas disconnect_on_error(client, err);
1476 3efd8e31 2022-10-23 thomas disconnect(client);
1479 3efd8e31 2022-10-23 thomas imsg_free(&imsg);
1482 3efd8e31 2022-10-23 thomas if (!shut) {
1483 3efd8e31 2022-10-23 thomas gotd_imsg_event_add(iev);
1484 3efd8e31 2022-10-23 thomas } else {
1485 3efd8e31 2022-10-23 thomas /* This pipe is dead. Remove its event handler */
1486 3efd8e31 2022-10-23 thomas event_del(&iev->ev);
1487 62ee7d94 2023-01-10 thomas disconnect(client);
1491 3efd8e31 2022-10-23 thomas static pid_t
1492 414e37cb 2022-12-30 thomas start_child(enum gotd_procid proc_id, const char *repo_path,
1493 832b8374 2022-10-31 thomas char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
1495 832b8374 2022-10-31 thomas char *argv[11];
1496 3efd8e31 2022-10-23 thomas int argc = 0;
1497 3efd8e31 2022-10-23 thomas pid_t pid;
1499 3efd8e31 2022-10-23 thomas switch (pid = fork()) {
1500 3efd8e31 2022-10-23 thomas case -1:
1501 3efd8e31 2022-10-23 thomas fatal("cannot fork");
1504 3efd8e31 2022-10-23 thomas default:
1505 3efd8e31 2022-10-23 thomas close(fd);
1506 3efd8e31 2022-10-23 thomas return pid;
1509 bb3a6ce9 2022-11-17 thomas if (fd != GOTD_FILENO_MSG_PIPE) {
1510 bb3a6ce9 2022-11-17 thomas if (dup2(fd, GOTD_FILENO_MSG_PIPE) == -1)
1511 3efd8e31 2022-10-23 thomas fatal("cannot setup imsg fd");
1512 3efd8e31 2022-10-23 thomas } else if (fcntl(fd, F_SETFD, 0) == -1)
1513 3efd8e31 2022-10-23 thomas fatal("cannot setup imsg fd");
1515 3efd8e31 2022-10-23 thomas argv[argc++] = argv0;
1516 3efd8e31 2022-10-23 thomas switch (proc_id) {
1517 2b3d32a1 2022-12-30 thomas case PROC_LISTEN:
1518 2b3d32a1 2022-12-30 thomas argv[argc++] = (char *)"-L";
1520 c669c489 2022-12-30 thomas case PROC_AUTH:
1521 c669c489 2022-12-30 thomas argv[argc++] = (char *)"-A";
1523 7fed8fa4 2023-06-22 thomas case PROC_SESSION_READ:
1524 7fed8fa4 2023-06-22 thomas argv[argc++] = (char *)"-s";
1526 7fed8fa4 2023-06-22 thomas case PROC_SESSION_WRITE:
1527 62ee7d94 2023-01-10 thomas argv[argc++] = (char *)"-S";
1529 3efd8e31 2022-10-23 thomas case PROC_REPO_READ:
1530 3efd8e31 2022-10-23 thomas argv[argc++] = (char *)"-R";
1532 3efd8e31 2022-10-23 thomas case PROC_REPO_WRITE:
1533 3efd8e31 2022-10-23 thomas argv[argc++] = (char *)"-W";
1535 3efd8e31 2022-10-23 thomas default:
1536 3efd8e31 2022-10-23 thomas fatalx("invalid process id %d", proc_id);
1539 832b8374 2022-10-31 thomas argv[argc++] = (char *)"-f";
1540 832b8374 2022-10-31 thomas argv[argc++] = (char *)confpath;
1542 414e37cb 2022-12-30 thomas if (repo_path) {
1543 2b3d32a1 2022-12-30 thomas argv[argc++] = (char *)"-P";
1544 414e37cb 2022-12-30 thomas argv[argc++] = (char *)repo_path;
1547 3efd8e31 2022-10-23 thomas if (!daemonize)
1548 3efd8e31 2022-10-23 thomas argv[argc++] = (char *)"-d";
1549 3efd8e31 2022-10-23 thomas if (verbosity > 0)
1550 3efd8e31 2022-10-23 thomas argv[argc++] = (char *)"-v";
1551 3efd8e31 2022-10-23 thomas if (verbosity > 1)
1552 3efd8e31 2022-10-23 thomas argv[argc++] = (char *)"-v";
1553 3efd8e31 2022-10-23 thomas argv[argc++] = NULL;
1555 3efd8e31 2022-10-23 thomas execvp(argv0, argv);
1556 3efd8e31 2022-10-23 thomas fatal("execvp");
1559 3efd8e31 2022-10-23 thomas static void
1560 2b3d32a1 2022-12-30 thomas start_listener(char *argv0, const char *confpath, int daemonize, int verbosity)
1562 78943464 2023-06-22 thomas struct gotd_child_proc *proc;
1564 78943464 2023-06-22 thomas proc = calloc(1, sizeof(*proc));
1565 78943464 2023-06-22 thomas if (proc == NULL)
1566 78943464 2023-06-22 thomas fatal("calloc");
1568 2c8fb90b 2023-06-25 thomas TAILQ_INSERT_HEAD(&procs, proc, entry);
1570 2c8fb90b 2023-06-25 thomas /* proc->tmo is initialized in main() after event_init() */
1572 2b3d32a1 2022-12-30 thomas proc->type = PROC_LISTEN;
1574 2b3d32a1 2022-12-30 thomas if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1575 2b3d32a1 2022-12-30 thomas PF_UNSPEC, proc->pipe) == -1)
1576 2b3d32a1 2022-12-30 thomas fatal("socketpair");
1578 2b3d32a1 2022-12-30 thomas proc->pid = start_child(proc->type, NULL, argv0, confpath,
1579 2b3d32a1 2022-12-30 thomas proc->pipe[1], daemonize, verbosity);
1580 2b3d32a1 2022-12-30 thomas imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1581 85b37c72 2022-12-30 thomas proc->iev.handler = gotd_dispatch_listener;
1582 2b3d32a1 2022-12-30 thomas proc->iev.events = EV_READ;
1583 2b3d32a1 2022-12-30 thomas proc->iev.handler_arg = NULL;
1585 78943464 2023-06-22 thomas gotd.listen_proc = proc;
1588 85b37c72 2022-12-30 thomas static const struct got_error *
1589 62ee7d94 2023-01-10 thomas start_session_child(struct gotd_client *client, struct gotd_repo *repo,
1590 62ee7d94 2023-01-10 thomas char *argv0, const char *confpath, int daemonize, int verbosity)
1592 62ee7d94 2023-01-10 thomas struct gotd_child_proc *proc;
1594 62ee7d94 2023-01-10 thomas proc = calloc(1, sizeof(*proc));
1595 62ee7d94 2023-01-10 thomas if (proc == NULL)
1596 62ee7d94 2023-01-10 thomas return got_error_from_errno("calloc");
1598 2c8fb90b 2023-06-25 thomas TAILQ_INSERT_HEAD(&procs, proc, entry);
1599 2c8fb90b 2023-06-25 thomas evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1601 7fed8fa4 2023-06-22 thomas if (client_is_reading(client))
1602 7fed8fa4 2023-06-22 thomas proc->type = PROC_SESSION_READ;
1604 7fed8fa4 2023-06-22 thomas proc->type = PROC_SESSION_WRITE;
1605 62ee7d94 2023-01-10 thomas if (strlcpy(proc->repo_name, repo->name,
1606 62ee7d94 2023-01-10 thomas sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1607 62ee7d94 2023-01-10 thomas fatalx("repository name too long: %s", repo->name);
1608 62ee7d94 2023-01-10 thomas log_debug("starting client uid %d session for repository %s",
1609 62ee7d94 2023-01-10 thomas client->euid, repo->name);
1610 62ee7d94 2023-01-10 thomas if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1611 62ee7d94 2023-01-10 thomas sizeof(proc->repo_path))
1612 62ee7d94 2023-01-10 thomas fatalx("repository path too long: %s", repo->path);
1613 62ee7d94 2023-01-10 thomas if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1614 62ee7d94 2023-01-10 thomas PF_UNSPEC, proc->pipe) == -1)
1615 62ee7d94 2023-01-10 thomas fatal("socketpair");
1616 62ee7d94 2023-01-10 thomas proc->pid = start_child(proc->type, proc->repo_path, argv0,
1617 62ee7d94 2023-01-10 thomas confpath, proc->pipe[1], daemonize, verbosity);
1618 62ee7d94 2023-01-10 thomas imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1619 62ee7d94 2023-01-10 thomas log_debug("proc %s %s is on fd %d",
1620 62ee7d94 2023-01-10 thomas gotd_proc_names[proc->type], proc->repo_path,
1621 62ee7d94 2023-01-10 thomas proc->pipe[0]);
1622 62ee7d94 2023-01-10 thomas proc->iev.handler = gotd_dispatch_client_session;
1623 62ee7d94 2023-01-10 thomas proc->iev.events = EV_READ;
1624 62ee7d94 2023-01-10 thomas proc->iev.handler_arg = NULL;
1625 62ee7d94 2023-01-10 thomas event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1626 62ee7d94 2023-01-10 thomas gotd_dispatch_client_session, &proc->iev);
1627 62ee7d94 2023-01-10 thomas gotd_imsg_event_add(&proc->iev);
1629 62ee7d94 2023-01-10 thomas client->session = proc;
1630 62ee7d94 2023-01-10 thomas return NULL;
1633 62ee7d94 2023-01-10 thomas static const struct got_error *
1634 85b37c72 2022-12-30 thomas start_repo_child(struct gotd_client *client, enum gotd_procid proc_type,
1635 85b37c72 2022-12-30 thomas struct gotd_repo *repo, char *argv0, const char *confpath,
1636 832b8374 2022-10-31 thomas int daemonize, int verbosity)
1638 3efd8e31 2022-10-23 thomas struct gotd_child_proc *proc;
1640 85b37c72 2022-12-30 thomas if (proc_type != PROC_REPO_READ && proc_type != PROC_REPO_WRITE)
1641 85b37c72 2022-12-30 thomas return got_error_msg(GOT_ERR_NOT_IMPL, "bad process type");
1643 85b37c72 2022-12-30 thomas proc = calloc(1, sizeof(*proc));
1644 85b37c72 2022-12-30 thomas if (proc == NULL)
1645 85b37c72 2022-12-30 thomas return got_error_from_errno("calloc");
1647 2c8fb90b 2023-06-25 thomas TAILQ_INSERT_HEAD(&procs, proc, entry);
1648 2c8fb90b 2023-06-25 thomas evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1650 85b37c72 2022-12-30 thomas proc->type = proc_type;
1651 85b37c72 2022-12-30 thomas if (strlcpy(proc->repo_name, repo->name,
1652 85b37c72 2022-12-30 thomas sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1653 85b37c72 2022-12-30 thomas fatalx("repository name too long: %s", repo->name);
1654 85b37c72 2022-12-30 thomas log_debug("starting %s for repository %s",
1655 85b37c72 2022-12-30 thomas proc->type == PROC_REPO_READ ? "reader" : "writer", repo->name);
1656 fe6a8988 2023-01-08 thomas if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1657 fe6a8988 2023-01-08 thomas sizeof(proc->repo_path))
1658 fe6a8988 2023-01-08 thomas fatalx("repository path too long: %s", repo->path);
1659 85b37c72 2022-12-30 thomas if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1660 85b37c72 2022-12-30 thomas PF_UNSPEC, proc->pipe) == -1)
1661 85b37c72 2022-12-30 thomas fatal("socketpair");
1662 85b37c72 2022-12-30 thomas proc->pid = start_child(proc->type, proc->repo_path, argv0,
1663 85b37c72 2022-12-30 thomas confpath, proc->pipe[1], daemonize, verbosity);
1664 85b37c72 2022-12-30 thomas imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1665 85b37c72 2022-12-30 thomas log_debug("proc %s %s is on fd %d",
1666 85b37c72 2022-12-30 thomas gotd_proc_names[proc->type], proc->repo_path,
1667 85b37c72 2022-12-30 thomas proc->pipe[0]);
1668 85b37c72 2022-12-30 thomas proc->iev.handler = gotd_dispatch_repo_child;
1669 85b37c72 2022-12-30 thomas proc->iev.events = EV_READ;
1670 85b37c72 2022-12-30 thomas proc->iev.handler_arg = NULL;
1671 85b37c72 2022-12-30 thomas event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1672 85b37c72 2022-12-30 thomas gotd_dispatch_repo_child, &proc->iev);
1673 85b37c72 2022-12-30 thomas gotd_imsg_event_add(&proc->iev);
1675 27b11d77 2023-01-14 thomas client->repo = proc;
1676 c669c489 2022-12-30 thomas return NULL;
1679 c669c489 2022-12-30 thomas static const struct got_error *
1680 c669c489 2022-12-30 thomas start_auth_child(struct gotd_client *client, int required_auth,
1681 c669c489 2022-12-30 thomas struct gotd_repo *repo, char *argv0, const char *confpath,
1682 c669c489 2022-12-30 thomas int daemonize, int verbosity)
1684 0bcde4c8 2022-12-30 thomas const struct got_error *err = NULL;
1685 c669c489 2022-12-30 thomas struct gotd_child_proc *proc;
1686 c669c489 2022-12-30 thomas struct gotd_imsg_auth iauth;
1689 c669c489 2022-12-30 thomas memset(&iauth, 0, sizeof(iauth));
1691 0bcde4c8 2022-12-30 thomas fd = dup(client->fd);
1692 0bcde4c8 2022-12-30 thomas if (fd == -1)
1693 0bcde4c8 2022-12-30 thomas return got_error_from_errno("dup");
1695 c669c489 2022-12-30 thomas proc = calloc(1, sizeof(*proc));
1696 0bcde4c8 2022-12-30 thomas if (proc == NULL) {
1697 0bcde4c8 2022-12-30 thomas err = got_error_from_errno("calloc");
1698 0bcde4c8 2022-12-30 thomas close(fd);
1699 0bcde4c8 2022-12-30 thomas return err;
1702 2c8fb90b 2023-06-25 thomas TAILQ_INSERT_HEAD(&procs, proc, entry);
1703 2c8fb90b 2023-06-25 thomas evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1705 c669c489 2022-12-30 thomas proc->type = PROC_AUTH;
1706 c669c489 2022-12-30 thomas if (strlcpy(proc->repo_name, repo->name,
1707 c669c489 2022-12-30 thomas sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1708 c669c489 2022-12-30 thomas fatalx("repository name too long: %s", repo->name);
1709 c669c489 2022-12-30 thomas log_debug("starting auth for uid %d repository %s",
1710 c669c489 2022-12-30 thomas client->euid, repo->name);
1711 fe6a8988 2023-01-08 thomas if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1712 fe6a8988 2023-01-08 thomas sizeof(proc->repo_path))
1713 fe6a8988 2023-01-08 thomas fatalx("repository path too long: %s", repo->path);
1714 c669c489 2022-12-30 thomas if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1715 c669c489 2022-12-30 thomas PF_UNSPEC, proc->pipe) == -1)
1716 c669c489 2022-12-30 thomas fatal("socketpair");
1717 c669c489 2022-12-30 thomas proc->pid = start_child(proc->type, proc->repo_path, argv0,
1718 c669c489 2022-12-30 thomas confpath, proc->pipe[1], daemonize, verbosity);
1719 c669c489 2022-12-30 thomas imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1720 c669c489 2022-12-30 thomas log_debug("proc %s %s is on fd %d",
1721 c669c489 2022-12-30 thomas gotd_proc_names[proc->type], proc->repo_path,
1722 c669c489 2022-12-30 thomas proc->pipe[0]);
1723 c669c489 2022-12-30 thomas proc->iev.handler = gotd_dispatch_auth_child;
1724 c669c489 2022-12-30 thomas proc->iev.events = EV_READ;
1725 c669c489 2022-12-30 thomas proc->iev.handler_arg = NULL;
1726 c669c489 2022-12-30 thomas event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1727 c669c489 2022-12-30 thomas gotd_dispatch_auth_child, &proc->iev);
1728 c669c489 2022-12-30 thomas gotd_imsg_event_add(&proc->iev);
1730 c669c489 2022-12-30 thomas iauth.euid = client->euid;
1731 c669c489 2022-12-30 thomas iauth.egid = client->egid;
1732 c669c489 2022-12-30 thomas iauth.required_auth = required_auth;
1733 c669c489 2022-12-30 thomas iauth.client_id = client->id;
1734 c669c489 2022-12-30 thomas if (gotd_imsg_compose_event(&proc->iev, GOTD_IMSG_AUTHENTICATE,
1735 0bcde4c8 2022-12-30 thomas PROC_GOTD, fd, &iauth, sizeof(iauth)) == -1) {
1736 c669c489 2022-12-30 thomas log_warn("imsg compose AUTHENTICATE");
1737 0bcde4c8 2022-12-30 thomas close(fd);
1738 0bcde4c8 2022-12-30 thomas /* Let the auth_timeout handler tidy up. */
1741 c669c489 2022-12-30 thomas client->auth = proc;
1742 c669c489 2022-12-30 thomas client->required_auth = required_auth;
1743 85b37c72 2022-12-30 thomas return NULL;
1746 414e37cb 2022-12-30 thomas static void
1747 7fed8fa4 2023-06-22 thomas apply_unveil_repo_readonly(const char *repo_path, int need_tmpdir)
1749 7fed8fa4 2023-06-22 thomas if (need_tmpdir) {
1750 7fed8fa4 2023-06-22 thomas if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1751 7fed8fa4 2023-06-22 thomas fatal("unveil %s", GOT_TMPDIR_STR);
1754 414e37cb 2022-12-30 thomas if (unveil(repo_path, "r") == -1)
1755 414e37cb 2022-12-30 thomas fatal("unveil %s", repo_path);
1757 b942ab08 2022-12-30 thomas if (unveil(NULL, NULL) == -1)
1758 b942ab08 2022-12-30 thomas fatal("unveil");
1761 b942ab08 2022-12-30 thomas static void
1762 62ee7d94 2023-01-10 thomas apply_unveil_repo_readwrite(const char *repo_path)
1764 62ee7d94 2023-01-10 thomas if (unveil(repo_path, "rwc") == -1)
1765 62ee7d94 2023-01-10 thomas fatal("unveil %s", repo_path);
1767 62ee7d94 2023-01-10 thomas if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1768 62ee7d94 2023-01-10 thomas fatal("unveil %s", GOT_TMPDIR_STR);
1770 62ee7d94 2023-01-10 thomas if (unveil(NULL, NULL) == -1)
1771 62ee7d94 2023-01-10 thomas fatal("unveil");
1774 62ee7d94 2023-01-10 thomas static void
1775 b942ab08 2022-12-30 thomas apply_unveil_none(void)
1777 b942ab08 2022-12-30 thomas if (unveil("/", "") == -1)
1778 b942ab08 2022-12-30 thomas fatal("unveil");
1780 414e37cb 2022-12-30 thomas if (unveil(NULL, NULL) == -1)
1781 414e37cb 2022-12-30 thomas fatal("unveil");
1784 3efd8e31 2022-10-23 thomas static void
1785 62ee7d94 2023-01-10 thomas apply_unveil_selfexec(void)
1787 85b37c72 2022-12-30 thomas if (unveil(gotd.argv0, "x") == -1)
1788 85b37c72 2022-12-30 thomas fatal("unveil %s", gotd.argv0);
1790 3efd8e31 2022-10-23 thomas if (unveil(NULL, NULL) == -1)
1791 3efd8e31 2022-10-23 thomas fatal("unveil");
1795 3efd8e31 2022-10-23 thomas main(int argc, char **argv)
1797 3efd8e31 2022-10-23 thomas const struct got_error *error = NULL;
1798 3efd8e31 2022-10-23 thomas int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
1799 3efd8e31 2022-10-23 thomas const char *confpath = GOTD_CONF_PATH;
1800 3efd8e31 2022-10-23 thomas char *argv0 = argv[0];
1801 3efd8e31 2022-10-23 thomas char title[2048];
1802 3efd8e31 2022-10-23 thomas struct passwd *pw = NULL;
1803 3efd8e31 2022-10-23 thomas char *repo_path = NULL;
1804 3efd8e31 2022-10-23 thomas enum gotd_procid proc_id = PROC_GOTD;
1805 2c8fb90b 2023-06-25 thomas struct event evsigint, evsigterm, evsighup, evsigusr1, evsigchld;
1806 3efd8e31 2022-10-23 thomas int *pack_fds = NULL, *temp_fds = NULL;
1807 6d7eb4f7 2023-04-04 thomas struct gotd_repo *repo = NULL;
1809 2c8fb90b 2023-06-25 thomas TAILQ_INIT(&procs);
1811 3efd8e31 2022-10-23 thomas log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
1813 7fed8fa4 2023-06-22 thomas while ((ch = getopt(argc, argv, "Adf:LnP:RsSvW")) != -1) {
1814 3efd8e31 2022-10-23 thomas switch (ch) {
1815 c669c489 2022-12-30 thomas case 'A':
1816 c669c489 2022-12-30 thomas proc_id = PROC_AUTH;
1818 3efd8e31 2022-10-23 thomas case 'd':
1819 3efd8e31 2022-10-23 thomas daemonize = 0;
1821 3efd8e31 2022-10-23 thomas case 'f':
1822 3efd8e31 2022-10-23 thomas confpath = optarg;
1824 2b3d32a1 2022-12-30 thomas case 'L':
1825 2b3d32a1 2022-12-30 thomas proc_id = PROC_LISTEN;
1827 3efd8e31 2022-10-23 thomas case 'n':
1828 3efd8e31 2022-10-23 thomas noaction = 1;
1830 f7065961 2022-10-27 thomas case 'P':
1831 f7065961 2022-10-27 thomas repo_path = realpath(optarg, NULL);
1832 f7065961 2022-10-27 thomas if (repo_path == NULL)
1833 f7065961 2022-10-27 thomas fatal("realpath '%s'", optarg);
1835 3efd8e31 2022-10-23 thomas case 'R':
1836 3efd8e31 2022-10-23 thomas proc_id = PROC_REPO_READ;
1838 7fed8fa4 2023-06-22 thomas case 's':
1839 7fed8fa4 2023-06-22 thomas proc_id = PROC_SESSION_READ;
1841 62ee7d94 2023-01-10 thomas case 'S':
1842 7fed8fa4 2023-06-22 thomas proc_id = PROC_SESSION_WRITE;
1844 f7065961 2022-10-27 thomas case 'v':
1845 f7065961 2022-10-27 thomas if (verbosity < 3)
1846 f7065961 2022-10-27 thomas verbosity++;
1848 3efd8e31 2022-10-23 thomas case 'W':
1849 3efd8e31 2022-10-23 thomas proc_id = PROC_REPO_WRITE;
1851 3efd8e31 2022-10-23 thomas default:
1852 3efd8e31 2022-10-23 thomas usage();
1856 3efd8e31 2022-10-23 thomas argc -= optind;
1857 3efd8e31 2022-10-23 thomas argv += optind;
1859 3efd8e31 2022-10-23 thomas if (argc != 0)
1860 3efd8e31 2022-10-23 thomas usage();
1862 85b37c72 2022-12-30 thomas if (geteuid() && (proc_id == PROC_GOTD || proc_id == PROC_LISTEN))
1863 3efd8e31 2022-10-23 thomas fatalx("need root privileges");
1865 f3807fe5 2023-07-10 thomas if (parse_config(confpath, proc_id, &gotd) != 0)
1866 3efd8e31 2022-10-23 thomas return 1;
1868 3efd8e31 2022-10-23 thomas pw = getpwnam(gotd.user_name);
1869 3efd8e31 2022-10-23 thomas if (pw == NULL)
1870 3e7c54e1 2022-12-30 thomas fatalx("user %s not found", gotd.user_name);
1872 b4b04e88 2023-01-19 thomas if (pw->pw_uid == 0)
1873 b4b04e88 2023-01-19 thomas fatalx("cannot run %s as the superuser", getprogname());
1875 b4b04e88 2023-01-19 thomas if (noaction) {
1876 b4b04e88 2023-01-19 thomas fprintf(stderr, "configuration OK\n");
1877 3efd8e31 2022-10-23 thomas return 0;
1880 b4b04e88 2023-01-19 thomas gotd.argv0 = argv0;
1881 b4b04e88 2023-01-19 thomas gotd.daemonize = daemonize;
1882 b4b04e88 2023-01-19 thomas gotd.verbosity = verbosity;
1883 b4b04e88 2023-01-19 thomas gotd.confpath = confpath;
1885 b4b04e88 2023-01-19 thomas /* Require an absolute path in argv[0] for reliable re-exec. */
1886 b4b04e88 2023-01-19 thomas if (!got_path_is_absolute(argv0))
1887 b4b04e88 2023-01-19 thomas fatalx("bad path \"%s\": must be an absolute path", argv0);
1889 b4b04e88 2023-01-19 thomas log_init(daemonize ? 0 : 1, LOG_DAEMON);
1890 b4b04e88 2023-01-19 thomas log_setverbose(verbosity);
1892 1eec6e4e 2022-12-06 thomas if (proc_id == PROC_GOTD) {
1893 2b3d32a1 2022-12-30 thomas snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1894 2b3d32a1 2022-12-30 thomas arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
1895 2b3d32a1 2022-12-30 thomas if (daemonize && daemon(1, 0) == -1)
1896 2b3d32a1 2022-12-30 thomas fatal("daemon");
1897 1f1613cf 2023-01-23 thomas gotd.pid = getpid();
1898 1f1613cf 2023-01-23 thomas start_listener(argv0, confpath, daemonize, verbosity);
1899 2b3d32a1 2022-12-30 thomas } else if (proc_id == PROC_LISTEN) {
1900 2b3d32a1 2022-12-30 thomas snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1901 1eec6e4e 2022-12-06 thomas if (verbosity) {
1902 1eec6e4e 2022-12-06 thomas log_info("socket: %s", gotd.unix_socket_path);
1903 1eec6e4e 2022-12-06 thomas log_info("user: %s", pw->pw_name);
1906 3efd8e31 2022-10-23 thomas fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
1907 f2fc8ce0 2023-01-06 thomas pw->pw_gid);
1908 3efd8e31 2022-10-23 thomas if (fd == -1) {
1909 3efd8e31 2022-10-23 thomas fatal("cannot listen on unix socket %s",
1910 3efd8e31 2022-10-23 thomas gotd.unix_socket_path);
1912 c669c489 2022-12-30 thomas } else if (proc_id == PROC_AUTH) {
1913 c669c489 2022-12-30 thomas snprintf(title, sizeof(title), "%s %s",
1914 c669c489 2022-12-30 thomas gotd_proc_names[proc_id], repo_path);
1915 62ee7d94 2023-01-10 thomas } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE ||
1916 7fed8fa4 2023-06-22 thomas proc_id == PROC_SESSION_READ || proc_id == PROC_SESSION_WRITE) {
1917 3efd8e31 2022-10-23 thomas error = got_repo_pack_fds_open(&pack_fds);
1918 3efd8e31 2022-10-23 thomas if (error != NULL)
1919 3efd8e31 2022-10-23 thomas fatalx("cannot open pack tempfiles: %s", error->msg);
1920 3efd8e31 2022-10-23 thomas error = got_repo_temp_fds_open(&temp_fds);
1921 3efd8e31 2022-10-23 thomas if (error != NULL)
1922 3efd8e31 2022-10-23 thomas fatalx("cannot open pack tempfiles: %s", error->msg);
1923 3efd8e31 2022-10-23 thomas if (repo_path == NULL)
1924 3efd8e31 2022-10-23 thomas fatalx("repository path not specified");
1925 3efd8e31 2022-10-23 thomas snprintf(title, sizeof(title), "%s %s",
1926 3efd8e31 2022-10-23 thomas gotd_proc_names[proc_id], repo_path);
1928 3efd8e31 2022-10-23 thomas fatal("invalid process id %d", proc_id);
1930 3efd8e31 2022-10-23 thomas setproctitle("%s", title);
1931 3efd8e31 2022-10-23 thomas log_procinit(title);
1933 3efd8e31 2022-10-23 thomas /* Drop root privileges. */
1934 3efd8e31 2022-10-23 thomas if (setgid(pw->pw_gid) == -1)
1935 3efd8e31 2022-10-23 thomas fatal("setgid %d failed", pw->pw_gid);
1936 3efd8e31 2022-10-23 thomas if (setuid(pw->pw_uid) == -1)
1937 3efd8e31 2022-10-23 thomas fatal("setuid %d failed", pw->pw_uid);
1939 3efd8e31 2022-10-23 thomas event_init();
1941 3efd8e31 2022-10-23 thomas switch (proc_id) {
1942 3efd8e31 2022-10-23 thomas case PROC_GOTD:
1943 3efd8e31 2022-10-23 thomas #ifndef PROFILE
1944 62ee7d94 2023-01-10 thomas /* "exec" promise will be limited to argv[0] via unveil(2). */
1945 62ee7d94 2023-01-10 thomas if (pledge("stdio proc exec sendfd recvfd unveil", NULL) == -1)
1946 3efd8e31 2022-10-23 thomas err(1, "pledge");
1949 2b3d32a1 2022-12-30 thomas case PROC_LISTEN:
1950 2b3d32a1 2022-12-30 thomas #ifndef PROFILE
1951 d4940d40 2023-01-06 thomas if (pledge("stdio sendfd unix unveil", NULL) == -1)
1952 2b3d32a1 2022-12-30 thomas err(1, "pledge");
1955 d4940d40 2023-01-06 thomas * Ensure that AF_UNIX bind(2) cannot be used with any other
1956 d4940d40 2023-01-06 thomas * sockets by revoking all filesystem access via unveil(2).
1958 d4940d40 2023-01-06 thomas apply_unveil_none();
1960 0781db0e 2023-01-06 thomas listen_main(title, fd, gotd.connection_limits,
1961 0781db0e 2023-01-06 thomas gotd.nconnection_limits);
1962 2b3d32a1 2022-12-30 thomas /* NOTREACHED */
1964 c669c489 2022-12-30 thomas case PROC_AUTH:
1965 c669c489 2022-12-30 thomas #ifndef PROFILE
1966 b942ab08 2022-12-30 thomas if (pledge("stdio getpw recvfd unix unveil", NULL) == -1)
1967 c669c489 2022-12-30 thomas err(1, "pledge");
1970 b942ab08 2022-12-30 thomas * We need the "unix" pledge promise for getpeername(2) only.
1971 b942ab08 2022-12-30 thomas * Ensure that AF_UNIX bind(2) cannot be used by revoking all
1972 b942ab08 2022-12-30 thomas * filesystem access via unveil(2). Access to password database
1973 b942ab08 2022-12-30 thomas * files will still work since "getpw" bypasses unveil(2).
1975 b942ab08 2022-12-30 thomas apply_unveil_none();
1977 c669c489 2022-12-30 thomas auth_main(title, &gotd.repos, repo_path);
1978 c669c489 2022-12-30 thomas /* NOTREACHED */
1980 7fed8fa4 2023-06-22 thomas case PROC_SESSION_READ:
1981 7fed8fa4 2023-06-22 thomas case PROC_SESSION_WRITE:
1982 62ee7d94 2023-01-10 thomas #ifndef PROFILE
1984 62ee7d94 2023-01-10 thomas * The "recvfd" promise is only needed during setup and
1985 62ee7d94 2023-01-10 thomas * will be removed in a later pledge(2) call.
1987 62ee7d94 2023-01-10 thomas if (pledge("stdio rpath wpath cpath recvfd sendfd fattr flock "
1988 62ee7d94 2023-01-10 thomas "unveil", NULL) == -1)
1989 62ee7d94 2023-01-10 thomas err(1, "pledge");
1991 7fed8fa4 2023-06-22 thomas if (proc_id == PROC_SESSION_READ)
1992 7fed8fa4 2023-06-22 thomas apply_unveil_repo_readonly(repo_path, 1);
1994 7fed8fa4 2023-06-22 thomas apply_unveil_repo_readwrite(repo_path);
1995 62ee7d94 2023-01-10 thomas session_main(title, repo_path, pack_fds, temp_fds,
1996 7fed8fa4 2023-06-22 thomas &gotd.request_timeout, proc_id);
1997 62ee7d94 2023-01-10 thomas /* NOTREACHED */
1999 3efd8e31 2022-10-23 thomas case PROC_REPO_READ:
2000 3efd8e31 2022-10-23 thomas #ifndef PROFILE
2001 414e37cb 2022-12-30 thomas if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2002 3efd8e31 2022-10-23 thomas err(1, "pledge");
2004 7fed8fa4 2023-06-22 thomas apply_unveil_repo_readonly(repo_path, 0);
2005 414e37cb 2022-12-30 thomas repo_read_main(title, repo_path, pack_fds, temp_fds);
2006 3efd8e31 2022-10-23 thomas /* NOTREACHED */
2007 3efd8e31 2022-10-23 thomas exit(0);
2008 3efd8e31 2022-10-23 thomas case PROC_REPO_WRITE:
2009 3efd8e31 2022-10-23 thomas #ifndef PROFILE
2010 414e37cb 2022-12-30 thomas if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2011 3efd8e31 2022-10-23 thomas err(1, "pledge");
2013 7fed8fa4 2023-06-22 thomas apply_unveil_repo_readonly(repo_path, 0);
2014 6d7eb4f7 2023-04-04 thomas repo = gotd_find_repo_by_path(repo_path, &gotd);
2015 6d7eb4f7 2023-04-04 thomas if (repo == NULL)
2016 6d7eb4f7 2023-04-04 thomas fatalx("no repository for path %s", repo_path);
2017 6d7eb4f7 2023-04-04 thomas repo_write_main(title, repo_path, pack_fds, temp_fds,
2018 6d7eb4f7 2023-04-04 thomas &repo->protected_tag_namespaces,
2019 6d7eb4f7 2023-04-04 thomas &repo->protected_branch_namespaces,
2020 6d7eb4f7 2023-04-04 thomas &repo->protected_branches);
2021 3efd8e31 2022-10-23 thomas /* NOTREACHED */
2022 3efd8e31 2022-10-23 thomas exit(0);
2023 3efd8e31 2022-10-23 thomas default:
2024 3efd8e31 2022-10-23 thomas fatal("invalid process id %d", proc_id);
2027 3efd8e31 2022-10-23 thomas if (proc_id != PROC_GOTD)
2028 3efd8e31 2022-10-23 thomas fatal("invalid process id %d", proc_id);
2030 2c8fb90b 2023-06-25 thomas evtimer_set(&gotd.listen_proc->tmo, kill_proc_timeout,
2031 2c8fb90b 2023-06-25 thomas gotd.listen_proc);
2033 62ee7d94 2023-01-10 thomas apply_unveil_selfexec();
2035 3efd8e31 2022-10-23 thomas signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
2036 3efd8e31 2022-10-23 thomas signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
2037 3efd8e31 2022-10-23 thomas signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
2038 3efd8e31 2022-10-23 thomas signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
2039 2c8fb90b 2023-06-25 thomas signal_set(&evsigchld, SIGCHLD, gotd_sighdlr, NULL);
2040 3efd8e31 2022-10-23 thomas signal(SIGPIPE, SIG_IGN);
2042 3efd8e31 2022-10-23 thomas signal_add(&evsigint, NULL);
2043 3efd8e31 2022-10-23 thomas signal_add(&evsigterm, NULL);
2044 3efd8e31 2022-10-23 thomas signal_add(&evsighup, NULL);
2045 3efd8e31 2022-10-23 thomas signal_add(&evsigusr1, NULL);
2046 2c8fb90b 2023-06-25 thomas signal_add(&evsigchld, NULL);
2048 78943464 2023-06-22 thomas gotd_imsg_event_add(&gotd.listen_proc->iev);
2050 3efd8e31 2022-10-23 thomas event_dispatch();
2052 3efd8e31 2022-10-23 thomas free(repo_path);
2053 62ee7d94 2023-01-10 thomas gotd_shutdown();
2055 3efd8e31 2022-10-23 thomas return 0;