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