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"
51 ba97b2d7 2024-03-20 stsp #include "got_diff.h"
53 13b2bc37 2022-10-23 stsp #include "got_lib_delta.h"
54 13b2bc37 2022-10-23 stsp #include "got_lib_object.h"
55 13b2bc37 2022-10-23 stsp #include "got_lib_object_cache.h"
56 53bf0b54 2023-02-23 op #include "got_lib_hash.h"
57 13b2bc37 2022-10-23 stsp #include "got_lib_gitproto.h"
58 13b2bc37 2022-10-23 stsp #include "got_lib_pack.h"
59 13b2bc37 2022-10-23 stsp #include "got_lib_repository.h"
61 13b2bc37 2022-10-23 stsp #include "gotd.h"
62 13b2bc37 2022-10-23 stsp #include "log.h"
63 d93ecf7d 2022-12-14 stsp #include "listen.h"
64 0ccf3acb 2022-11-16 stsp #include "auth.h"
65 e70fd952 2024-03-22 stsp #include "session_read.h"
66 e70fd952 2024-03-22 stsp #include "session_write.h"
67 13b2bc37 2022-10-23 stsp #include "repo_read.h"
68 13b2bc37 2022-10-23 stsp #include "repo_write.h"
69 ba97b2d7 2024-03-20 stsp #include "notify.h"
71 13b2bc37 2022-10-23 stsp #ifndef nitems
72 13b2bc37 2022-10-23 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
75 eac23c30 2023-01-10 stsp enum gotd_client_state {
76 eac23c30 2023-01-10 stsp GOTD_CLIENT_STATE_NEW,
77 eac23c30 2023-01-10 stsp GOTD_CLIENT_STATE_ACCESS_GRANTED,
80 c929736a 2023-06-22 op struct gotd_child_proc {
82 c929736a 2023-06-22 op enum gotd_procid type;
83 c929736a 2023-06-22 op char repo_name[NAME_MAX];
84 c929736a 2023-06-22 op char repo_path[PATH_MAX];
86 c929736a 2023-06-22 op struct gotd_imsgev iev;
87 839338f6 2023-06-22 op struct event tmo;
89 839338f6 2023-06-22 op TAILQ_ENTRY(gotd_child_proc) entry;
91 839338f6 2023-06-22 op TAILQ_HEAD(gotd_procs, gotd_child_proc) procs;
93 13b2bc37 2022-10-23 stsp struct gotd_client {
94 13b2bc37 2022-10-23 stsp STAILQ_ENTRY(gotd_client) entry;
95 13b2bc37 2022-10-23 stsp enum gotd_client_state state;
96 13b2bc37 2022-10-23 stsp uint32_t id;
98 13b2bc37 2022-10-23 stsp struct gotd_imsgev iev;
99 13b2bc37 2022-10-23 stsp struct event tmo;
100 13b2bc37 2022-10-23 stsp uid_t euid;
101 13b2bc37 2022-10-23 stsp gid_t egid;
102 56624d2b 2023-12-27 stsp char *username;
103 f7a854cf 2023-01-10 stsp struct gotd_child_proc *repo;
104 5e25db14 2022-12-29 stsp struct gotd_child_proc *auth;
105 ae7c1b78 2023-01-10 stsp struct gotd_child_proc *session;
106 5e25db14 2022-12-29 stsp int required_auth;
108 13b2bc37 2022-10-23 stsp STAILQ_HEAD(gotd_clients, gotd_client);
110 13b2bc37 2022-10-23 stsp static struct gotd_clients gotd_clients[GOTD_CLIENT_TABLE_SIZE];
111 13b2bc37 2022-10-23 stsp static SIPHASH_KEY clients_hash_key;
112 13b2bc37 2022-10-23 stsp volatile int client_cnt;
113 ef4e2f01 2022-12-29 stsp static struct timeval auth_timeout = { 5, 0 };
114 13b2bc37 2022-10-23 stsp static struct gotd gotd;
116 13b2bc37 2022-10-23 stsp void gotd_sighdlr(int sig, short event, void *arg);
117 f1752522 2022-10-29 stsp static void gotd_shutdown(void);
118 ae7c1b78 2023-01-10 stsp static const struct got_error *start_session_child(struct gotd_client *,
119 ae7c1b78 2023-01-10 stsp struct gotd_repo *, char *, const char *, int, int);
120 b50a2b46 2022-12-29 stsp static const struct got_error *start_repo_child(struct gotd_client *,
121 b50a2b46 2022-12-29 stsp enum gotd_procid, struct gotd_repo *, char *, const char *, int, int);
122 5e25db14 2022-12-29 stsp static const struct got_error *start_auth_child(struct gotd_client *, int,
123 5e25db14 2022-12-29 stsp struct gotd_repo *, char *, const char *, int, int);
124 b50a2b46 2022-12-29 stsp static void kill_proc(struct gotd_child_proc *, int);
125 839338f6 2023-06-22 op static void disconnect(struct gotd_client *);
127 13b2bc37 2022-10-23 stsp __dead static void
130 e9e01966 2023-01-18 stsp fprintf(stderr, "usage: %s [-dnv] [-f config-file]\n", getprogname());
135 13b2bc37 2022-10-23 stsp unix_socket_listen(const char *unix_socket_path, uid_t uid, gid_t gid)
137 13b2bc37 2022-10-23 stsp struct sockaddr_un sun;
138 13b2bc37 2022-10-23 stsp int fd = -1;
139 13b2bc37 2022-10-23 stsp mode_t old_umask, mode;
141 13b2bc37 2022-10-23 stsp fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
142 13b2bc37 2022-10-23 stsp if (fd == -1) {
143 13b2bc37 2022-10-23 stsp log_warn("socket");
147 13b2bc37 2022-10-23 stsp sun.sun_family = AF_UNIX;
148 13b2bc37 2022-10-23 stsp if (strlcpy(sun.sun_path, unix_socket_path,
149 13b2bc37 2022-10-23 stsp sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
150 13b2bc37 2022-10-23 stsp log_warnx("%s: name too long", unix_socket_path);
155 13b2bc37 2022-10-23 stsp if (unlink(unix_socket_path) == -1) {
156 13b2bc37 2022-10-23 stsp if (errno != ENOENT) {
157 13b2bc37 2022-10-23 stsp log_warn("unlink %s", unix_socket_path);
163 13b2bc37 2022-10-23 stsp old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
164 6f854dde 2023-01-04 stsp mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
166 13b2bc37 2022-10-23 stsp if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
167 13b2bc37 2022-10-23 stsp log_warn("bind: %s", unix_socket_path);
169 13b2bc37 2022-10-23 stsp umask(old_umask);
173 13b2bc37 2022-10-23 stsp umask(old_umask);
175 13b2bc37 2022-10-23 stsp if (chmod(unix_socket_path, mode) == -1) {
176 13b2bc37 2022-10-23 stsp log_warn("chmod %o %s", mode, unix_socket_path);
178 13b2bc37 2022-10-23 stsp unlink(unix_socket_path);
182 13b2bc37 2022-10-23 stsp if (chown(unix_socket_path, uid, gid) == -1) {
183 13b2bc37 2022-10-23 stsp log_warn("chown %s uid=%d gid=%d", unix_socket_path, uid, gid);
185 13b2bc37 2022-10-23 stsp unlink(unix_socket_path);
189 13b2bc37 2022-10-23 stsp if (listen(fd, GOTD_UNIX_SOCKET_BACKLOG) == -1) {
190 13b2bc37 2022-10-23 stsp log_warn("listen");
192 13b2bc37 2022-10-23 stsp unlink(unix_socket_path);
199 13b2bc37 2022-10-23 stsp static uint64_t
200 13b2bc37 2022-10-23 stsp client_hash(uint32_t client_id)
202 13b2bc37 2022-10-23 stsp return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
205 13b2bc37 2022-10-23 stsp static void
206 13b2bc37 2022-10-23 stsp add_client(struct gotd_client *client)
208 13b2bc37 2022-10-23 stsp uint64_t slot = client_hash(client->id) % nitems(gotd_clients);
209 13b2bc37 2022-10-23 stsp STAILQ_INSERT_HEAD(&gotd_clients[slot], client, entry);
210 13b2bc37 2022-10-23 stsp client_cnt++;
213 13b2bc37 2022-10-23 stsp static struct gotd_client *
214 13b2bc37 2022-10-23 stsp find_client(uint32_t client_id)
216 13b2bc37 2022-10-23 stsp uint64_t slot;
217 13b2bc37 2022-10-23 stsp struct gotd_client *c;
219 13b2bc37 2022-10-23 stsp slot = client_hash(client_id) % nitems(gotd_clients);
220 13b2bc37 2022-10-23 stsp STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
221 13b2bc37 2022-10-23 stsp if (c->id == client_id)
225 13b2bc37 2022-10-23 stsp return NULL;
228 b50a2b46 2022-12-29 stsp static struct gotd_client *
229 b50a2b46 2022-12-29 stsp find_client_by_proc_fd(int fd)
231 b50a2b46 2022-12-29 stsp uint64_t slot;
233 b50a2b46 2022-12-29 stsp for (slot = 0; slot < nitems(gotd_clients); slot++) {
234 b50a2b46 2022-12-29 stsp struct gotd_client *c;
236 b50a2b46 2022-12-29 stsp STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
237 f7a854cf 2023-01-10 stsp if (c->repo && c->repo->iev.ibuf.fd == fd)
239 5e25db14 2022-12-29 stsp if (c->auth && c->auth->iev.ibuf.fd == fd)
241 ae7c1b78 2023-01-10 stsp if (c->session && c->session->iev.ibuf.fd == fd)
246 13b2bc37 2022-10-23 stsp return NULL;
250 13b2bc37 2022-10-23 stsp client_is_reading(struct gotd_client *client)
252 f7a854cf 2023-01-10 stsp return (client->required_auth &
253 f7a854cf 2023-01-10 stsp (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) == GOTD_AUTH_READ;
257 13b2bc37 2022-10-23 stsp client_is_writing(struct gotd_client *client)
259 f7a854cf 2023-01-10 stsp return (client->required_auth &
260 f7a854cf 2023-01-10 stsp (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) ==
261 f7a854cf 2023-01-10 stsp (GOTD_AUTH_READ | GOTD_AUTH_WRITE);
264 13b2bc37 2022-10-23 stsp static const struct got_error *
265 13b2bc37 2022-10-23 stsp ensure_client_is_not_writing(struct gotd_client *client)
267 13b2bc37 2022-10-23 stsp if (client_is_writing(client)) {
268 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_BAD_PACKET,
269 13b2bc37 2022-10-23 stsp "uid %d made a read-request but is writing to "
270 13b2bc37 2022-10-23 stsp "a repository", client->euid);
273 13b2bc37 2022-10-23 stsp return NULL;
276 13b2bc37 2022-10-23 stsp static const struct got_error *
277 13b2bc37 2022-10-23 stsp ensure_client_is_not_reading(struct gotd_client *client)
279 13b2bc37 2022-10-23 stsp if (client_is_reading(client)) {
280 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_BAD_PACKET,
281 13b2bc37 2022-10-23 stsp "uid %d made a write-request but is reading from "
282 13b2bc37 2022-10-23 stsp "a repository", client->euid);
285 13b2bc37 2022-10-23 stsp return NULL;
288 b50a2b46 2022-12-29 stsp static void
289 839338f6 2023-06-22 op proc_done(struct gotd_child_proc *proc)
291 839338f6 2023-06-22 op struct gotd_client *client;
293 839338f6 2023-06-22 op TAILQ_REMOVE(&procs, proc, entry);
295 839338f6 2023-06-22 op client = find_client_by_proc_fd(proc->iev.ibuf.fd);
296 839338f6 2023-06-22 op if (client != NULL) {
297 839338f6 2023-06-22 op if (proc == client->repo)
298 839338f6 2023-06-22 op client->repo = NULL;
299 839338f6 2023-06-22 op if (proc == client->auth)
300 839338f6 2023-06-22 op client->auth = NULL;
301 839338f6 2023-06-22 op if (proc == client->session)
302 839338f6 2023-06-22 op client->session = NULL;
303 839338f6 2023-06-22 op disconnect(client);
306 ba97b2d7 2024-03-20 stsp if (proc == gotd.notify_proc)
307 ba97b2d7 2024-03-20 stsp gotd.notify_proc = NULL;
309 839338f6 2023-06-22 op evtimer_del(&proc->tmo);
311 839338f6 2023-06-22 op if (proc->iev.ibuf.fd != -1) {
312 839338f6 2023-06-22 op event_del(&proc->iev.ev);
313 839338f6 2023-06-22 op msgbuf_clear(&proc->iev.ibuf.w);
314 839338f6 2023-06-22 op close(proc->iev.ibuf.fd);
317 ae7c1b78 2023-01-10 stsp free(proc);
321 ba91039c 2023-06-22 op kill_repo_proc(struct gotd_client *client)
323 ba91039c 2023-06-22 op if (client->repo == NULL)
326 839338f6 2023-06-22 op kill_proc(client->repo, 0);
327 ba91039c 2023-06-22 op client->repo = NULL;
330 13b2bc37 2022-10-23 stsp static void
331 5e25db14 2022-12-29 stsp kill_auth_proc(struct gotd_client *client)
333 5e25db14 2022-12-29 stsp if (client->auth == NULL)
336 839338f6 2023-06-22 op kill_proc(client->auth, 0);
337 5e25db14 2022-12-29 stsp client->auth = NULL;
340 5e25db14 2022-12-29 stsp static void
341 ae7c1b78 2023-01-10 stsp kill_session_proc(struct gotd_client *client)
343 ae7c1b78 2023-01-10 stsp if (client->session == NULL)
346 839338f6 2023-06-22 op kill_proc(client->session, 0);
347 ae7c1b78 2023-01-10 stsp client->session = NULL;
350 ae7c1b78 2023-01-10 stsp static void
351 13b2bc37 2022-10-23 stsp disconnect(struct gotd_client *client)
353 13b2bc37 2022-10-23 stsp struct gotd_imsg_disconnect idisconnect;
354 c929736a 2023-06-22 op struct gotd_child_proc *listen_proc = gotd.listen_proc;
355 13b2bc37 2022-10-23 stsp uint64_t slot;
357 13b2bc37 2022-10-23 stsp log_debug("uid %d: disconnecting", client->euid);
359 5e25db14 2022-12-29 stsp kill_auth_proc(client);
360 ae7c1b78 2023-01-10 stsp kill_session_proc(client);
361 ba91039c 2023-06-22 op kill_repo_proc(client);
363 90270f79 2023-02-09 stsp idisconnect.client_id = client->id;
364 d93ecf7d 2022-12-14 stsp if (gotd_imsg_compose_event(&listen_proc->iev,
365 d93ecf7d 2022-12-14 stsp GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
366 d93ecf7d 2022-12-14 stsp &idisconnect, sizeof(idisconnect)) == -1)
367 d93ecf7d 2022-12-14 stsp log_warn("imsg compose DISCONNECT");
369 13b2bc37 2022-10-23 stsp slot = client_hash(client->id) % nitems(gotd_clients);
370 13b2bc37 2022-10-23 stsp STAILQ_REMOVE(&gotd_clients[slot], client, gotd_client, entry);
371 13b2bc37 2022-10-23 stsp imsg_clear(&client->iev.ibuf);
372 13b2bc37 2022-10-23 stsp event_del(&client->iev.ev);
373 13b2bc37 2022-10-23 stsp evtimer_del(&client->tmo);
374 ae7c1b78 2023-01-10 stsp if (client->fd != -1)
375 ae7c1b78 2023-01-10 stsp close(client->fd);
376 ae7c1b78 2023-01-10 stsp else if (client->iev.ibuf.fd != -1)
377 ae7c1b78 2023-01-10 stsp close(client->iev.ibuf.fd);
378 56624d2b 2023-12-27 stsp free(client->username);
379 13b2bc37 2022-10-23 stsp free(client);
380 13b2bc37 2022-10-23 stsp client_cnt--;
383 13b2bc37 2022-10-23 stsp static void
384 13b2bc37 2022-10-23 stsp disconnect_on_error(struct gotd_client *client, const struct got_error *err)
386 13b2bc37 2022-10-23 stsp struct imsgbuf ibuf;
388 1050403b 2023-08-11 stsp if (err->code != GOT_ERR_EOF) {
389 1050403b 2023-08-11 stsp log_warnx("uid %d: %s", client->euid, err->msg);
390 1050403b 2023-08-11 stsp if (client->fd != -1) {
391 1050403b 2023-08-11 stsp imsg_init(&ibuf, client->fd);
392 1050403b 2023-08-11 stsp gotd_imsg_send_error(&ibuf, 0, PROC_GOTD, err);
393 1050403b 2023-08-11 stsp imsg_clear(&ibuf);
396 13b2bc37 2022-10-23 stsp disconnect(client);
399 f1752522 2022-10-29 stsp static const struct got_error *
400 f1752522 2022-10-29 stsp send_repo_info(struct gotd_imsgev *iev, struct gotd_repo *repo)
402 f1752522 2022-10-29 stsp const struct got_error *err = NULL;
403 f1752522 2022-10-29 stsp struct gotd_imsg_info_repo irepo;
405 f1752522 2022-10-29 stsp memset(&irepo, 0, sizeof(irepo));
407 f1752522 2022-10-29 stsp if (strlcpy(irepo.repo_name, repo->name, sizeof(irepo.repo_name))
408 f1752522 2022-10-29 stsp >= sizeof(irepo.repo_name))
409 f1752522 2022-10-29 stsp return got_error_msg(GOT_ERR_NO_SPACE, "repo name too long");
410 f1752522 2022-10-29 stsp if (strlcpy(irepo.repo_path, repo->path, sizeof(irepo.repo_path))
411 f1752522 2022-10-29 stsp >= sizeof(irepo.repo_path))
412 f1752522 2022-10-29 stsp return got_error_msg(GOT_ERR_NO_SPACE, "repo path too long");
414 f1752522 2022-10-29 stsp if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_REPO, PROC_GOTD, -1,
415 f1752522 2022-10-29 stsp &irepo, sizeof(irepo)) == -1) {
416 f1752522 2022-10-29 stsp err = got_error_from_errno("imsg compose INFO_REPO");
418 f1752522 2022-10-29 stsp return err;
421 f1752522 2022-10-29 stsp return NULL;
424 f1752522 2022-10-29 stsp static const struct got_error *
425 f1752522 2022-10-29 stsp send_client_info(struct gotd_imsgev *iev, struct gotd_client *client)
427 f1752522 2022-10-29 stsp const struct got_error *err = NULL;
428 f1752522 2022-10-29 stsp struct gotd_imsg_info_client iclient;
429 f1752522 2022-10-29 stsp struct gotd_child_proc *proc;
431 f1752522 2022-10-29 stsp memset(&iclient, 0, sizeof(iclient));
432 f1752522 2022-10-29 stsp iclient.euid = client->euid;
433 f1752522 2022-10-29 stsp iclient.egid = client->egid;
435 f7a854cf 2023-01-10 stsp proc = client->repo;
436 f1752522 2022-10-29 stsp if (proc) {
437 eec68231 2022-12-14 stsp if (strlcpy(iclient.repo_name, proc->repo_path,
438 f1752522 2022-10-29 stsp sizeof(iclient.repo_name)) >= sizeof(iclient.repo_name)) {
439 f1752522 2022-10-29 stsp return got_error_msg(GOT_ERR_NO_SPACE,
440 f1752522 2022-10-29 stsp "repo name too long");
442 f1752522 2022-10-29 stsp if (client_is_writing(client))
443 f1752522 2022-10-29 stsp iclient.is_writing = 1;
445 ae7c1b78 2023-01-10 stsp iclient.repo_child_pid = proc->pid;
448 ae7c1b78 2023-01-10 stsp if (client->session)
449 ae7c1b78 2023-01-10 stsp iclient.session_child_pid = client->session->pid;
451 f1752522 2022-10-29 stsp if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_CLIENT, PROC_GOTD, -1,
452 f1752522 2022-10-29 stsp &iclient, sizeof(iclient)) == -1) {
453 f1752522 2022-10-29 stsp err = got_error_from_errno("imsg compose INFO_CLIENT");
455 f1752522 2022-10-29 stsp return err;
458 f1752522 2022-10-29 stsp return NULL;
461 f1752522 2022-10-29 stsp static const struct got_error *
462 f1752522 2022-10-29 stsp send_info(struct gotd_client *client)
464 f1752522 2022-10-29 stsp const struct got_error *err = NULL;
465 f1752522 2022-10-29 stsp struct gotd_imsg_info info;
466 f1752522 2022-10-29 stsp uint64_t slot;
467 f1752522 2022-10-29 stsp struct gotd_repo *repo;
469 78433331 2023-01-04 stsp if (client->euid != 0)
470 78433331 2023-01-04 stsp return got_error_set_errno(EPERM, "info");
472 f1752522 2022-10-29 stsp info.pid = gotd.pid;
473 f1752522 2022-10-29 stsp info.verbosity = gotd.verbosity;
474 f1752522 2022-10-29 stsp info.nrepos = gotd.nrepos;
475 f1752522 2022-10-29 stsp info.nclients = client_cnt - 1;
477 f1752522 2022-10-29 stsp if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_INFO, PROC_GOTD, -1,
478 f1752522 2022-10-29 stsp &info, sizeof(info)) == -1) {
479 f1752522 2022-10-29 stsp err = got_error_from_errno("imsg compose INFO");
481 f1752522 2022-10-29 stsp return err;
484 f1752522 2022-10-29 stsp TAILQ_FOREACH(repo, &gotd.repos, entry) {
485 f1752522 2022-10-29 stsp err = send_repo_info(&client->iev, repo);
487 f1752522 2022-10-29 stsp return err;
490 f1752522 2022-10-29 stsp for (slot = 0; slot < nitems(gotd_clients); slot++) {
491 f1752522 2022-10-29 stsp struct gotd_client *c;
492 f1752522 2022-10-29 stsp STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
493 f1752522 2022-10-29 stsp if (c->id == client->id)
495 f1752522 2022-10-29 stsp err = send_client_info(&client->iev, c);
497 f1752522 2022-10-29 stsp return err;
501 f1752522 2022-10-29 stsp return NULL;
504 f1752522 2022-10-29 stsp static const struct got_error *
505 f1752522 2022-10-29 stsp stop_gotd(struct gotd_client *client)
508 f1752522 2022-10-29 stsp if (client->euid != 0)
509 f1752522 2022-10-29 stsp return got_error_set_errno(EPERM, "stop");
511 f1752522 2022-10-29 stsp gotd_shutdown();
512 f1752522 2022-10-29 stsp /* NOTREACHED */
513 0ccf3acb 2022-11-16 stsp return NULL;
516 13b2bc37 2022-10-23 stsp static const struct got_error *
517 ae7c1b78 2023-01-10 stsp start_client_authentication(struct gotd_client *client, struct imsg *imsg)
519 13b2bc37 2022-10-23 stsp const struct got_error *err;
520 13b2bc37 2022-10-23 stsp struct gotd_imsg_list_refs ireq;
521 0ccf3acb 2022-11-16 stsp struct gotd_repo *repo = NULL;
522 13b2bc37 2022-10-23 stsp size_t datalen;
524 13b2bc37 2022-10-23 stsp log_debug("list-refs request from uid %d", client->euid);
526 eac23c30 2023-01-10 stsp if (client->state != GOTD_CLIENT_STATE_NEW)
527 ae7c1b78 2023-01-10 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
528 ae7c1b78 2023-01-10 stsp "unexpected list-refs request received");
530 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
531 13b2bc37 2022-10-23 stsp if (datalen != sizeof(ireq))
532 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
534 13b2bc37 2022-10-23 stsp memcpy(&ireq, imsg->data, datalen);
536 13b2bc37 2022-10-23 stsp if (ireq.client_is_reading) {
537 13b2bc37 2022-10-23 stsp err = ensure_client_is_not_writing(client);
539 13b2bc37 2022-10-23 stsp return err;
540 ba97b2d7 2024-03-20 stsp repo = gotd_find_repo_by_name(ireq.repo_name, &gotd.repos);
541 0ccf3acb 2022-11-16 stsp if (repo == NULL)
542 0ccf3acb 2022-11-16 stsp return got_error(GOT_ERR_NOT_GIT_REPO);
543 5e25db14 2022-12-29 stsp err = start_auth_child(client, GOTD_AUTH_READ, repo,
544 b50a2b46 2022-12-29 stsp gotd.argv0, gotd.confpath, gotd.daemonize,
545 b50a2b46 2022-12-29 stsp gotd.verbosity);
547 b50a2b46 2022-12-29 stsp return err;
549 13b2bc37 2022-10-23 stsp err = ensure_client_is_not_reading(client);
551 0ccf3acb 2022-11-16 stsp return err;
552 ba97b2d7 2024-03-20 stsp repo = gotd_find_repo_by_name(ireq.repo_name, &gotd.repos);
553 0ccf3acb 2022-11-16 stsp if (repo == NULL)
554 0ccf3acb 2022-11-16 stsp return got_error(GOT_ERR_NOT_GIT_REPO);
555 5e25db14 2022-12-29 stsp err = start_auth_child(client,
556 5e25db14 2022-12-29 stsp GOTD_AUTH_READ | GOTD_AUTH_WRITE,
557 5e25db14 2022-12-29 stsp repo, gotd.argv0, gotd.confpath, gotd.daemonize,
558 b50a2b46 2022-12-29 stsp gotd.verbosity);
560 b50a2b46 2022-12-29 stsp return err;
563 ae7c1b78 2023-01-10 stsp evtimer_add(&client->tmo, &auth_timeout);
565 5fb50fce 2023-07-14 stsp /* Flow continues upon authentication success/failure or timeout. */
566 13b2bc37 2022-10-23 stsp return NULL;
569 13b2bc37 2022-10-23 stsp static void
570 13b2bc37 2022-10-23 stsp gotd_request(int fd, short events, void *arg)
572 13b2bc37 2022-10-23 stsp struct gotd_imsgev *iev = arg;
573 13b2bc37 2022-10-23 stsp struct imsgbuf *ibuf = &iev->ibuf;
574 13b2bc37 2022-10-23 stsp struct gotd_client *client = iev->handler_arg;
575 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
576 13b2bc37 2022-10-23 stsp struct imsg imsg;
579 13b2bc37 2022-10-23 stsp if (events & EV_WRITE) {
580 13b2bc37 2022-10-23 stsp while (ibuf->w.queued) {
581 13b2bc37 2022-10-23 stsp n = msgbuf_write(&ibuf->w);
582 13b2bc37 2022-10-23 stsp if (n == -1 && errno == EPIPE) {
584 13b2bc37 2022-10-23 stsp * The client has closed its socket.
585 13b2bc37 2022-10-23 stsp * This can happen when Git clients are
586 13b2bc37 2022-10-23 stsp * done sending pack file data.
588 13b2bc37 2022-10-23 stsp msgbuf_clear(&ibuf->w);
590 13b2bc37 2022-10-23 stsp } else if (n == -1 && errno != EAGAIN) {
591 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_flush");
592 13b2bc37 2022-10-23 stsp disconnect_on_error(client, err);
595 13b2bc37 2022-10-23 stsp if (n == 0) {
596 13b2bc37 2022-10-23 stsp /* Connection closed. */
597 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_EOF);
598 13b2bc37 2022-10-23 stsp disconnect_on_error(client, err);
603 f1752522 2022-10-29 stsp /* Disconnect gotctl(8) now that messages have been sent. */
604 f1752522 2022-10-29 stsp if (!client_is_reading(client) && !client_is_writing(client)) {
605 f1752522 2022-10-29 stsp disconnect(client);
610 13b2bc37 2022-10-23 stsp if ((events & EV_READ) == 0)
613 13b2bc37 2022-10-23 stsp memset(&imsg, 0, sizeof(imsg));
615 13b2bc37 2022-10-23 stsp while (err == NULL) {
616 13b2bc37 2022-10-23 stsp err = gotd_imsg_recv(&imsg, ibuf, 0);
618 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_PRIVSEP_READ)
619 13b2bc37 2022-10-23 stsp err = NULL;
623 13b2bc37 2022-10-23 stsp evtimer_del(&client->tmo);
625 13b2bc37 2022-10-23 stsp switch (imsg.hdr.type) {
626 f1752522 2022-10-29 stsp case GOTD_IMSG_INFO:
627 f1752522 2022-10-29 stsp err = send_info(client);
629 f1752522 2022-10-29 stsp case GOTD_IMSG_STOP:
630 f1752522 2022-10-29 stsp err = stop_gotd(client);
632 13b2bc37 2022-10-23 stsp case GOTD_IMSG_LIST_REFS:
633 ae7c1b78 2023-01-10 stsp err = start_client_authentication(client, &imsg);
636 ae7c1b78 2023-01-10 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
637 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
641 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
645 b5225f29 2023-01-22 op disconnect_on_error(client, err);
647 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(&client->iev);
651 13b2bc37 2022-10-23 stsp static void
652 ae7c1b78 2023-01-10 stsp gotd_auth_timeout(int fd, short events, void *arg)
654 13b2bc37 2022-10-23 stsp struct gotd_client *client = arg;
656 ae7c1b78 2023-01-10 stsp log_debug("disconnecting uid %d due to authentication timeout",
657 ae7c1b78 2023-01-10 stsp client->euid);
658 13b2bc37 2022-10-23 stsp disconnect(client);
661 d93ecf7d 2022-12-14 stsp static const struct got_error *
662 d93ecf7d 2022-12-14 stsp recv_connect(uint32_t *client_id, struct imsg *imsg)
664 d93ecf7d 2022-12-14 stsp const struct got_error *err = NULL;
665 d93ecf7d 2022-12-14 stsp struct gotd_imsg_connect iconnect;
666 d93ecf7d 2022-12-14 stsp size_t datalen;
667 13b2bc37 2022-10-23 stsp int s = -1;
668 13b2bc37 2022-10-23 stsp struct gotd_client *client = NULL;
670 d93ecf7d 2022-12-14 stsp *client_id = 0;
672 d93ecf7d 2022-12-14 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
673 d93ecf7d 2022-12-14 stsp if (datalen != sizeof(iconnect))
674 d93ecf7d 2022-12-14 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
675 d93ecf7d 2022-12-14 stsp memcpy(&iconnect, imsg->data, sizeof(iconnect));
677 2c52c623 2024-01-30 op s = imsg_get_fd(imsg);
678 13b2bc37 2022-10-23 stsp if (s == -1) {
679 d93ecf7d 2022-12-14 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
683 d93ecf7d 2022-12-14 stsp if (find_client(iconnect.client_id)) {
684 d93ecf7d 2022-12-14 stsp err = got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
688 13b2bc37 2022-10-23 stsp client = calloc(1, sizeof(*client));
689 13b2bc37 2022-10-23 stsp if (client == NULL) {
690 d93ecf7d 2022-12-14 stsp err = got_error_from_errno("calloc");
694 d93ecf7d 2022-12-14 stsp *client_id = iconnect.client_id;
696 eac23c30 2023-01-10 stsp client->state = GOTD_CLIENT_STATE_NEW;
697 d93ecf7d 2022-12-14 stsp client->id = iconnect.client_id;
698 13b2bc37 2022-10-23 stsp client->fd = s;
700 365cf0f3 2022-12-29 stsp /* The auth process will verify UID/GID for us. */
701 365cf0f3 2022-12-29 stsp client->euid = iconnect.euid;
702 365cf0f3 2022-12-29 stsp client->egid = iconnect.egid;
704 13b2bc37 2022-10-23 stsp imsg_init(&client->iev.ibuf, client->fd);
705 13b2bc37 2022-10-23 stsp client->iev.handler = gotd_request;
706 13b2bc37 2022-10-23 stsp client->iev.events = EV_READ;
707 13b2bc37 2022-10-23 stsp client->iev.handler_arg = client;
709 13b2bc37 2022-10-23 stsp event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
710 13b2bc37 2022-10-23 stsp &client->iev);
711 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(&client->iev);
713 ae7c1b78 2023-01-10 stsp evtimer_set(&client->tmo, gotd_auth_timeout, client);
715 13b2bc37 2022-10-23 stsp add_client(client);
716 13b2bc37 2022-10-23 stsp log_debug("%s: new client uid %d connected on fd %d", __func__,
717 13b2bc37 2022-10-23 stsp client->euid, client->fd);
720 c929736a 2023-06-22 op struct gotd_child_proc *listen_proc = gotd.listen_proc;
721 d93ecf7d 2022-12-14 stsp struct gotd_imsg_disconnect idisconnect;
723 d93ecf7d 2022-12-14 stsp idisconnect.client_id = client->id;
724 d93ecf7d 2022-12-14 stsp if (gotd_imsg_compose_event(&listen_proc->iev,
725 d93ecf7d 2022-12-14 stsp GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
726 d93ecf7d 2022-12-14 stsp &idisconnect, sizeof(idisconnect)) == -1)
727 d93ecf7d 2022-12-14 stsp log_warn("imsg compose DISCONNECT");
729 d93ecf7d 2022-12-14 stsp if (s != -1)
733 d93ecf7d 2022-12-14 stsp return err;
736 13b2bc37 2022-10-23 stsp static const char *gotd_proc_names[PROC_MAX] = {
740 ce986f22 2023-06-19 stsp "session_read",
741 ce986f22 2023-06-19 stsp "session_write",
742 13b2bc37 2022-10-23 stsp "repo_read",
743 4b3827cd 2023-07-08 stsp "repo_write",
744 ba97b2d7 2024-03-20 stsp "gitwrapper",
748 13b2bc37 2022-10-23 stsp static void
749 13b2bc37 2022-10-23 stsp kill_proc(struct gotd_child_proc *proc, int fatal)
751 839338f6 2023-06-22 op struct timeval tv = { 5, 0 };
753 839338f6 2023-06-22 op log_debug("kill -%d %d", fatal ? SIGKILL : SIGTERM, proc->pid);
755 839338f6 2023-06-22 op if (proc->iev.ibuf.fd != -1) {
756 839338f6 2023-06-22 op event_del(&proc->iev.ev);
757 839338f6 2023-06-22 op msgbuf_clear(&proc->iev.ibuf.w);
758 839338f6 2023-06-22 op close(proc->iev.ibuf.fd);
759 839338f6 2023-06-22 op proc->iev.ibuf.fd = -1;
762 839338f6 2023-06-22 op if (!evtimer_pending(&proc->tmo, NULL) && !fatal)
763 839338f6 2023-06-22 op evtimer_add(&proc->tmo, &tv);
765 13b2bc37 2022-10-23 stsp if (fatal) {
766 13b2bc37 2022-10-23 stsp log_warnx("sending SIGKILL to PID %d", proc->pid);
767 13b2bc37 2022-10-23 stsp kill(proc->pid, SIGKILL);
769 13b2bc37 2022-10-23 stsp kill(proc->pid, SIGTERM);
772 13b2bc37 2022-10-23 stsp static void
773 839338f6 2023-06-22 op kill_proc_timeout(int fd, short ev, void *d)
775 839338f6 2023-06-22 op struct gotd_child_proc *proc = d;
777 839338f6 2023-06-22 op log_warnx("timeout waiting for PID %d to terminate;"
778 839338f6 2023-06-22 op " retrying with force", proc->pid);
779 839338f6 2023-06-22 op kill_proc(proc, 1);
783 13b2bc37 2022-10-23 stsp gotd_shutdown(void)
785 b50a2b46 2022-12-29 stsp uint64_t slot;
787 ae7c1b78 2023-01-10 stsp log_debug("shutting down");
788 b50a2b46 2022-12-29 stsp for (slot = 0; slot < nitems(gotd_clients); slot++) {
789 b50a2b46 2022-12-29 stsp struct gotd_client *c, *tmp;
791 b50a2b46 2022-12-29 stsp STAILQ_FOREACH_SAFE(c, &gotd_clients[slot], entry, tmp)
792 b50a2b46 2022-12-29 stsp disconnect(c);
795 839338f6 2023-06-22 op kill_proc(gotd.listen_proc, 0);
797 13b2bc37 2022-10-23 stsp log_info("terminating");
801 839338f6 2023-06-22 op static struct gotd_child_proc *
802 839338f6 2023-06-22 op find_proc_by_pid(pid_t pid)
804 839338f6 2023-06-22 op struct gotd_child_proc *proc = NULL;
806 839338f6 2023-06-22 op TAILQ_FOREACH(proc, &procs, entry)
807 839338f6 2023-06-22 op if (proc->pid == pid)
814 13b2bc37 2022-10-23 stsp gotd_sighdlr(int sig, short event, void *arg)
816 839338f6 2023-06-22 op struct gotd_child_proc *proc;
821 13b2bc37 2022-10-23 stsp * Normal signal handler rules don't apply because libevent
822 13b2bc37 2022-10-23 stsp * decouples for us.
825 13b2bc37 2022-10-23 stsp switch (sig) {
826 13b2bc37 2022-10-23 stsp case SIGHUP:
827 13b2bc37 2022-10-23 stsp log_info("%s: ignoring SIGHUP", __func__);
829 13b2bc37 2022-10-23 stsp case SIGUSR1:
830 13b2bc37 2022-10-23 stsp log_info("%s: ignoring SIGUSR1", __func__);
832 13b2bc37 2022-10-23 stsp case SIGTERM:
833 13b2bc37 2022-10-23 stsp case SIGINT:
834 13b2bc37 2022-10-23 stsp gotd_shutdown();
836 839338f6 2023-06-22 op case SIGCHLD:
838 839338f6 2023-06-22 op pid = waitpid(WAIT_ANY, &status, WNOHANG);
839 839338f6 2023-06-22 op if (pid == -1) {
840 839338f6 2023-06-22 op if (errno == EINTR)
842 839338f6 2023-06-22 op if (errno == ECHILD)
844 839338f6 2023-06-22 op fatal("waitpid");
846 839338f6 2023-06-22 op if (pid == 0)
849 839338f6 2023-06-22 op log_debug("reaped pid %d", pid);
850 839338f6 2023-06-22 op proc = find_proc_by_pid(pid);
851 839338f6 2023-06-22 op if (proc == NULL) {
852 839338f6 2023-06-22 op log_info("caught exit of unknown child %d",
857 839338f6 2023-06-22 op if (WIFSIGNALED(status)) {
858 839338f6 2023-06-22 op log_warnx("child PID %d terminated with"
859 839338f6 2023-06-22 op " signal %d", pid, WTERMSIG(status));
862 839338f6 2023-06-22 op proc_done(proc);
866 13b2bc37 2022-10-23 stsp fatalx("unexpected signal");
870 13b2bc37 2022-10-23 stsp static const struct got_error *
871 13b2bc37 2022-10-23 stsp ensure_proc_is_reading(struct gotd_client *client,
872 13b2bc37 2022-10-23 stsp struct gotd_child_proc *proc)
874 13b2bc37 2022-10-23 stsp if (!client_is_reading(client)) {
875 13b2bc37 2022-10-23 stsp kill_proc(proc, 1);
876 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_BAD_PACKET,
877 13b2bc37 2022-10-23 stsp "PID %d handled a read-request for uid %d but this "
878 13b2bc37 2022-10-23 stsp "user is not reading from a repository", proc->pid,
879 13b2bc37 2022-10-23 stsp client->euid);
882 13b2bc37 2022-10-23 stsp return NULL;
885 13b2bc37 2022-10-23 stsp static const struct got_error *
886 13b2bc37 2022-10-23 stsp ensure_proc_is_writing(struct gotd_client *client,
887 13b2bc37 2022-10-23 stsp struct gotd_child_proc *proc)
889 13b2bc37 2022-10-23 stsp if (!client_is_writing(client)) {
890 13b2bc37 2022-10-23 stsp kill_proc(proc, 1);
891 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_BAD_PACKET,
892 13b2bc37 2022-10-23 stsp "PID %d handled a write-request for uid %d but this "
893 13b2bc37 2022-10-23 stsp "user is not writing to a repository", proc->pid,
894 13b2bc37 2022-10-23 stsp client->euid);
897 13b2bc37 2022-10-23 stsp return NULL;
901 13b2bc37 2022-10-23 stsp verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
902 13b2bc37 2022-10-23 stsp struct imsg *imsg)
904 13b2bc37 2022-10-23 stsp const struct got_error *err;
905 13b2bc37 2022-10-23 stsp int ret = 0;
907 d93ecf7d 2022-12-14 stsp if (proc->type == PROC_REPO_READ || proc->type == PROC_REPO_WRITE) {
908 f7a854cf 2023-01-10 stsp if (client->repo == NULL)
909 d93ecf7d 2022-12-14 stsp fatalx("no process found for uid %d", client->euid);
910 f7a854cf 2023-01-10 stsp if (proc->pid != client->repo->pid) {
911 d93ecf7d 2022-12-14 stsp kill_proc(proc, 1);
912 d93ecf7d 2022-12-14 stsp log_warnx("received message from PID %d for uid %d, "
913 d93ecf7d 2022-12-14 stsp "while PID %d is the process serving this user",
914 f7a854cf 2023-01-10 stsp proc->pid, client->euid, client->repo->pid);
918 b0614828 2023-06-19 stsp if (proc->type == PROC_SESSION_READ ||
919 b0614828 2023-06-19 stsp proc->type == PROC_SESSION_WRITE) {
920 ae7c1b78 2023-01-10 stsp if (client->session == NULL) {
921 ae7c1b78 2023-01-10 stsp log_warnx("no session found for uid %d", client->euid);
924 ae7c1b78 2023-01-10 stsp if (proc->pid != client->session->pid) {
925 ae7c1b78 2023-01-10 stsp kill_proc(proc, 1);
926 ae7c1b78 2023-01-10 stsp log_warnx("received message from PID %d for uid %d, "
927 ae7c1b78 2023-01-10 stsp "while PID %d is the process serving this user",
928 ae7c1b78 2023-01-10 stsp proc->pid, client->euid, client->session->pid);
933 13b2bc37 2022-10-23 stsp switch (imsg->hdr.type) {
934 13b2bc37 2022-10-23 stsp case GOTD_IMSG_ERROR:
937 d93ecf7d 2022-12-14 stsp case GOTD_IMSG_CONNECT:
938 d93ecf7d 2022-12-14 stsp if (proc->type != PROC_LISTEN) {
939 d93ecf7d 2022-12-14 stsp err = got_error_fmt(GOT_ERR_BAD_PACKET,
940 d93ecf7d 2022-12-14 stsp "new connection for uid %d from PID %d "
941 d93ecf7d 2022-12-14 stsp "which is not the listen process",
942 76a9a38e 2023-12-27 stsp client->euid, proc->pid);
946 5e25db14 2022-12-29 stsp case GOTD_IMSG_ACCESS_GRANTED:
947 5e25db14 2022-12-29 stsp if (proc->type != PROC_AUTH) {
948 5e25db14 2022-12-29 stsp err = got_error_fmt(GOT_ERR_BAD_PACKET,
949 5e25db14 2022-12-29 stsp "authentication of uid %d from PID %d "
950 5e25db14 2022-12-29 stsp "which is not the auth process",
951 76a9a38e 2023-12-27 stsp client->euid, proc->pid);
955 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_CLIENT_SESSION_READY:
956 b0614828 2023-06-19 stsp if (proc->type != PROC_SESSION_READ &&
957 b0614828 2023-06-19 stsp proc->type != PROC_SESSION_WRITE) {
958 ae7c1b78 2023-01-10 stsp err = got_error_fmt(GOT_ERR_BAD_PACKET,
959 ae7c1b78 2023-01-10 stsp "unexpected \"ready\" signal from PID %d",
960 ae7c1b78 2023-01-10 stsp proc->pid);
964 b50a2b46 2022-12-29 stsp case GOTD_IMSG_REPO_CHILD_READY:
965 b50a2b46 2022-12-29 stsp if (proc->type != PROC_REPO_READ &&
966 b50a2b46 2022-12-29 stsp proc->type != PROC_REPO_WRITE) {
967 b50a2b46 2022-12-29 stsp err = got_error_fmt(GOT_ERR_BAD_PACKET,
968 b50a2b46 2022-12-29 stsp "unexpected \"ready\" signal from PID %d",
969 b50a2b46 2022-12-29 stsp proc->pid);
973 13b2bc37 2022-10-23 stsp case GOTD_IMSG_PACKFILE_DONE:
974 13b2bc37 2022-10-23 stsp err = ensure_proc_is_reading(client, proc);
976 13b2bc37 2022-10-23 stsp log_warnx("uid %d: %s", client->euid, err->msg);
980 13b2bc37 2022-10-23 stsp case GOTD_IMSG_PACKFILE_INSTALL:
981 13b2bc37 2022-10-23 stsp case GOTD_IMSG_REF_UPDATES_START:
982 13b2bc37 2022-10-23 stsp case GOTD_IMSG_REF_UPDATE:
983 13b2bc37 2022-10-23 stsp err = ensure_proc_is_writing(client, proc);
985 13b2bc37 2022-10-23 stsp log_warnx("uid %d: %s", client->euid, err->msg);
990 13b2bc37 2022-10-23 stsp log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
994 13b2bc37 2022-10-23 stsp return ret;
997 13b2bc37 2022-10-23 stsp static const struct got_error *
998 ae7c1b78 2023-01-10 stsp connect_repo_child(struct gotd_client *client,
999 ae7c1b78 2023-01-10 stsp struct gotd_child_proc *repo_proc)
1001 b50a2b46 2022-12-29 stsp static const struct got_error *err;
1002 ae7c1b78 2023-01-10 stsp struct gotd_imsgev *session_iev = &client->session->iev;
1003 ae7c1b78 2023-01-10 stsp struct gotd_imsg_connect_repo_child ireq;
1004 ae7c1b78 2023-01-10 stsp int pipe[2];
1006 eac23c30 2023-01-10 stsp if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED)
1007 ae7c1b78 2023-01-10 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
1008 ae7c1b78 2023-01-10 stsp "unexpected repo child ready signal received");
1010 ae7c1b78 2023-01-10 stsp if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1011 ae7c1b78 2023-01-10 stsp PF_UNSPEC, pipe) == -1)
1012 ae7c1b78 2023-01-10 stsp fatal("socketpair");
1014 ae7c1b78 2023-01-10 stsp memset(&ireq, 0, sizeof(ireq));
1015 ae7c1b78 2023-01-10 stsp ireq.proc_id = repo_proc->type;
1017 ae7c1b78 2023-01-10 stsp /* Pass repo child pipe to session child process. */
1018 ae7c1b78 2023-01-10 stsp if (gotd_imsg_compose_event(session_iev, GOTD_IMSG_CONNECT_REPO_CHILD,
1019 ae7c1b78 2023-01-10 stsp PROC_GOTD, pipe[0], &ireq, sizeof(ireq)) == -1) {
1020 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
1021 ae7c1b78 2023-01-10 stsp close(pipe[0]);
1022 ae7c1b78 2023-01-10 stsp close(pipe[1]);
1023 ae7c1b78 2023-01-10 stsp return err;
1026 ae7c1b78 2023-01-10 stsp /* Pass session child pipe to repo child process. */
1027 ae7c1b78 2023-01-10 stsp if (gotd_imsg_compose_event(&repo_proc->iev,
1028 ae7c1b78 2023-01-10 stsp GOTD_IMSG_CONNECT_REPO_CHILD, PROC_GOTD, pipe[1], NULL, 0) == -1) {
1029 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
1030 ae7c1b78 2023-01-10 stsp close(pipe[1]);
1031 ae7c1b78 2023-01-10 stsp return err;
1034 13b2bc37 2022-10-23 stsp return NULL;
1037 13b2bc37 2022-10-23 stsp static void
1038 b50a2b46 2022-12-29 stsp gotd_dispatch_listener(int fd, short event, void *arg)
1040 13b2bc37 2022-10-23 stsp struct gotd_imsgev *iev = arg;
1041 13b2bc37 2022-10-23 stsp struct imsgbuf *ibuf = &iev->ibuf;
1042 c929736a 2023-06-22 op struct gotd_child_proc *proc = gotd.listen_proc;
1043 b50a2b46 2022-12-29 stsp ssize_t n;
1044 b50a2b46 2022-12-29 stsp int shut = 0;
1045 b50a2b46 2022-12-29 stsp struct imsg imsg;
1047 b50a2b46 2022-12-29 stsp if (proc->iev.ibuf.fd != fd)
1048 b50a2b46 2022-12-29 stsp fatalx("%s: unexpected fd %d", __func__, fd);
1050 b50a2b46 2022-12-29 stsp if (event & EV_READ) {
1051 b50a2b46 2022-12-29 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1052 b50a2b46 2022-12-29 stsp fatal("imsg_read error");
1053 b50a2b46 2022-12-29 stsp if (n == 0) {
1054 b50a2b46 2022-12-29 stsp /* Connection closed. */
1056 b50a2b46 2022-12-29 stsp goto done;
1060 b50a2b46 2022-12-29 stsp if (event & EV_WRITE) {
1061 b50a2b46 2022-12-29 stsp n = msgbuf_write(&ibuf->w);
1062 b50a2b46 2022-12-29 stsp if (n == -1 && errno != EAGAIN)
1063 b50a2b46 2022-12-29 stsp fatal("msgbuf_write");
1064 b50a2b46 2022-12-29 stsp if (n == 0) {
1065 b50a2b46 2022-12-29 stsp /* Connection closed. */
1067 b50a2b46 2022-12-29 stsp goto done;
1071 b50a2b46 2022-12-29 stsp for (;;) {
1072 b50a2b46 2022-12-29 stsp const struct got_error *err = NULL;
1073 b50a2b46 2022-12-29 stsp struct gotd_client *client = NULL;
1074 b50a2b46 2022-12-29 stsp uint32_t client_id = 0;
1075 b50a2b46 2022-12-29 stsp int do_disconnect = 0;
1077 b50a2b46 2022-12-29 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
1078 b50a2b46 2022-12-29 stsp fatal("%s: imsg_get error", __func__);
1079 b50a2b46 2022-12-29 stsp if (n == 0) /* No more messages. */
1082 b50a2b46 2022-12-29 stsp switch (imsg.hdr.type) {
1083 b50a2b46 2022-12-29 stsp case GOTD_IMSG_ERROR:
1084 b50a2b46 2022-12-29 stsp do_disconnect = 1;
1085 b50a2b46 2022-12-29 stsp err = gotd_imsg_recv_error(&client_id, &imsg);
1087 b50a2b46 2022-12-29 stsp case GOTD_IMSG_CONNECT:
1088 b50a2b46 2022-12-29 stsp err = recv_connect(&client_id, &imsg);
1091 b50a2b46 2022-12-29 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1095 b50a2b46 2022-12-29 stsp client = find_client(client_id);
1096 b50a2b46 2022-12-29 stsp if (client == NULL) {
1097 b50a2b46 2022-12-29 stsp log_warnx("%s: client not found", __func__);
1098 b50a2b46 2022-12-29 stsp imsg_free(&imsg);
1103 b50a2b46 2022-12-29 stsp log_warnx("uid %d: %s", client->euid, err->msg);
1105 b50a2b46 2022-12-29 stsp if (do_disconnect) {
1107 b50a2b46 2022-12-29 stsp disconnect_on_error(client, err);
1109 b50a2b46 2022-12-29 stsp disconnect(client);
1112 b50a2b46 2022-12-29 stsp imsg_free(&imsg);
1115 b50a2b46 2022-12-29 stsp if (!shut) {
1116 b50a2b46 2022-12-29 stsp gotd_imsg_event_add(iev);
1118 b50a2b46 2022-12-29 stsp /* This pipe is dead. Remove its event handler */
1119 b50a2b46 2022-12-29 stsp event_del(&iev->ev);
1120 b50a2b46 2022-12-29 stsp event_loopexit(NULL);
1124 b50a2b46 2022-12-29 stsp static void
1125 ba97b2d7 2024-03-20 stsp gotd_dispatch_notifier(int fd, short event, void *arg)
1127 ba97b2d7 2024-03-20 stsp struct gotd_imsgev *iev = arg;
1128 ba97b2d7 2024-03-20 stsp struct imsgbuf *ibuf = &iev->ibuf;
1129 ba97b2d7 2024-03-20 stsp struct gotd_child_proc *proc = gotd.notify_proc;
1130 ba97b2d7 2024-03-20 stsp ssize_t n;
1131 ba97b2d7 2024-03-20 stsp int shut = 0;
1132 ba97b2d7 2024-03-20 stsp struct imsg imsg;
1134 ba97b2d7 2024-03-20 stsp if (proc->iev.ibuf.fd != fd)
1135 ba97b2d7 2024-03-20 stsp fatalx("%s: unexpected fd %d", __func__, fd);
1137 ba97b2d7 2024-03-20 stsp if (event & EV_READ) {
1138 ba97b2d7 2024-03-20 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1139 ba97b2d7 2024-03-20 stsp fatal("imsg_read error");
1140 ba97b2d7 2024-03-20 stsp if (n == 0) {
1141 ba97b2d7 2024-03-20 stsp /* Connection closed. */
1143 ba97b2d7 2024-03-20 stsp goto done;
1147 ba97b2d7 2024-03-20 stsp if (event & EV_WRITE) {
1148 ba97b2d7 2024-03-20 stsp n = msgbuf_write(&ibuf->w);
1149 ba97b2d7 2024-03-20 stsp if (n == -1 && errno != EAGAIN)
1150 ba97b2d7 2024-03-20 stsp fatal("msgbuf_write");
1151 ba97b2d7 2024-03-20 stsp if (n == 0) {
1152 ba97b2d7 2024-03-20 stsp /* Connection closed. */
1154 ba97b2d7 2024-03-20 stsp goto done;
1158 ba97b2d7 2024-03-20 stsp for (;;) {
1159 ba97b2d7 2024-03-20 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
1160 ba97b2d7 2024-03-20 stsp fatal("%s: imsg_get error", __func__);
1161 ba97b2d7 2024-03-20 stsp if (n == 0) /* No more messages. */
1164 ba97b2d7 2024-03-20 stsp switch (imsg.hdr.type) {
1166 ba97b2d7 2024-03-20 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1170 ba97b2d7 2024-03-20 stsp imsg_free(&imsg);
1173 ba97b2d7 2024-03-20 stsp if (!shut) {
1174 ba97b2d7 2024-03-20 stsp gotd_imsg_event_add(iev);
1176 ba97b2d7 2024-03-20 stsp /* This pipe is dead. Remove its event handler */
1177 ba97b2d7 2024-03-20 stsp event_del(&iev->ev);
1180 ba97b2d7 2024-03-20 stsp * Do not exit all of gotd if the notification handler dies.
1181 ba97b2d7 2024-03-20 stsp * We can continue operating without notifications until an
1182 ba97b2d7 2024-03-20 stsp * operator intervenes.
1184 ba97b2d7 2024-03-20 stsp log_warnx("notify child process (pid %d) closed its imsg pipe "
1185 ba97b2d7 2024-03-20 stsp "unexpectedly", proc->pid);
1186 ba97b2d7 2024-03-20 stsp proc_done(proc);
1190 ba97b2d7 2024-03-20 stsp static void
1191 5e25db14 2022-12-29 stsp gotd_dispatch_auth_child(int fd, short event, void *arg)
1193 5e25db14 2022-12-29 stsp const struct got_error *err = NULL;
1194 5e25db14 2022-12-29 stsp struct gotd_imsgev *iev = arg;
1195 5e25db14 2022-12-29 stsp struct imsgbuf *ibuf = &iev->ibuf;
1196 5e25db14 2022-12-29 stsp struct gotd_client *client;
1197 5e25db14 2022-12-29 stsp struct gotd_repo *repo = NULL;
1198 5e25db14 2022-12-29 stsp ssize_t n;
1199 5e25db14 2022-12-29 stsp int shut = 0;
1200 5e25db14 2022-12-29 stsp struct imsg imsg;
1201 5e25db14 2022-12-29 stsp uint32_t client_id = 0;
1202 5e25db14 2022-12-29 stsp int do_disconnect = 0;
1203 56624d2b 2023-12-27 stsp size_t datalen;
1205 5e25db14 2022-12-29 stsp client = find_client_by_proc_fd(fd);
1206 ae0cca99 2023-02-09 stsp if (client == NULL) {
1207 ae0cca99 2023-02-09 stsp /* Can happen during process teardown. */
1208 ae0cca99 2023-02-09 stsp warnx("cannot find client for fd %d", fd);
1210 ae0cca99 2023-02-09 stsp goto done;
1213 5e25db14 2022-12-29 stsp if (client->auth == NULL)
1214 5e25db14 2022-12-29 stsp fatalx("cannot find auth child process for fd %d", fd);
1216 5e25db14 2022-12-29 stsp if (event & EV_READ) {
1217 5e25db14 2022-12-29 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1218 5e25db14 2022-12-29 stsp fatal("imsg_read error");
1219 5e25db14 2022-12-29 stsp if (n == 0) {
1220 5e25db14 2022-12-29 stsp /* Connection closed. */
1222 5e25db14 2022-12-29 stsp goto done;
1226 5e25db14 2022-12-29 stsp if (event & EV_WRITE) {
1227 5e25db14 2022-12-29 stsp n = msgbuf_write(&ibuf->w);
1228 5e25db14 2022-12-29 stsp if (n == -1 && errno != EAGAIN)
1229 5e25db14 2022-12-29 stsp fatal("msgbuf_write");
1230 5e25db14 2022-12-29 stsp if (n == 0) {
1231 5e25db14 2022-12-29 stsp /* Connection closed. */
1234 5e25db14 2022-12-29 stsp goto done;
1237 5e25db14 2022-12-29 stsp if (client->auth->iev.ibuf.fd != fd)
1238 5e25db14 2022-12-29 stsp fatalx("%s: unexpected fd %d", __func__, fd);
1240 5e25db14 2022-12-29 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
1241 5e25db14 2022-12-29 stsp fatal("%s: imsg_get error", __func__);
1242 5e25db14 2022-12-29 stsp if (n == 0) /* No more messages. */
1245 5e25db14 2022-12-29 stsp evtimer_del(&client->tmo);
1247 56624d2b 2023-12-27 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1249 5e25db14 2022-12-29 stsp switch (imsg.hdr.type) {
1250 5e25db14 2022-12-29 stsp case GOTD_IMSG_ERROR:
1251 5e25db14 2022-12-29 stsp do_disconnect = 1;
1252 5e25db14 2022-12-29 stsp err = gotd_imsg_recv_error(&client_id, &imsg);
1254 5e25db14 2022-12-29 stsp case GOTD_IMSG_ACCESS_GRANTED:
1255 56624d2b 2023-12-27 stsp if (client->state != GOTD_CLIENT_STATE_NEW) {
1256 56624d2b 2023-12-27 stsp do_disconnect = 1;
1257 56624d2b 2023-12-27 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1261 5e25db14 2022-12-29 stsp do_disconnect = 1;
1262 5e25db14 2022-12-29 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1266 5e25db14 2022-12-29 stsp if (!verify_imsg_src(client, client->auth, &imsg)) {
1267 5e25db14 2022-12-29 stsp do_disconnect = 1;
1268 5e25db14 2022-12-29 stsp log_debug("dropping imsg type %d from PID %d",
1269 5e25db14 2022-12-29 stsp imsg.hdr.type, client->auth->pid);
1272 5e25db14 2022-12-29 stsp if (do_disconnect) {
1274 5e25db14 2022-12-29 stsp disconnect_on_error(client, err);
1276 5e25db14 2022-12-29 stsp disconnect(client);
1277 56624d2b 2023-12-27 stsp imsg_free(&imsg);
1281 56624d2b 2023-12-27 stsp client->state = GOTD_CLIENT_STATE_ACCESS_GRANTED;
1282 56624d2b 2023-12-27 stsp if (datalen > 0)
1283 56624d2b 2023-12-27 stsp client->username = strndup(imsg.data, datalen);
1284 56624d2b 2023-12-27 stsp imsg_free(&imsg);
1285 56624d2b 2023-12-27 stsp if (client->username == NULL &&
1286 56624d2b 2023-12-27 stsp asprintf(&client->username, "uid %d", client->euid) == -1) {
1287 56624d2b 2023-12-27 stsp err = got_error_from_errno("asprintf");
1288 56624d2b 2023-12-27 stsp goto done;
1291 ba97b2d7 2024-03-20 stsp repo = gotd_find_repo_by_name(client->auth->repo_name, &gotd.repos);
1292 5e25db14 2022-12-29 stsp if (repo == NULL) {
1293 5e25db14 2022-12-29 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
1294 5e25db14 2022-12-29 stsp goto done;
1296 5e25db14 2022-12-29 stsp kill_auth_proc(client);
1298 56624d2b 2023-12-27 stsp log_info("authenticated %s for repository %s",
1299 56624d2b 2023-12-27 stsp client->username, repo->name);
1301 ae7c1b78 2023-01-10 stsp err = start_session_child(client, repo, gotd.argv0,
1302 7fdc3e58 2022-12-30 mark gotd.confpath, gotd.daemonize, gotd.verbosity);
1304 ae7c1b78 2023-01-10 stsp goto done;
1307 5e25db14 2022-12-29 stsp log_warnx("uid %d: %s", client->euid, err->msg);
1309 5e25db14 2022-12-29 stsp /* We might have killed the auth process by now. */
1310 5e25db14 2022-12-29 stsp if (client->auth != NULL) {
1311 5e25db14 2022-12-29 stsp if (!shut) {
1312 5e25db14 2022-12-29 stsp gotd_imsg_event_add(iev);
1314 5e25db14 2022-12-29 stsp /* This pipe is dead. Remove its event handler */
1315 5e25db14 2022-12-29 stsp event_del(&iev->ev);
1320 ae7c1b78 2023-01-10 stsp static const struct got_error *
1321 ae7c1b78 2023-01-10 stsp connect_session(struct gotd_client *client)
1323 ae7c1b78 2023-01-10 stsp const struct got_error *err = NULL;
1324 ae7c1b78 2023-01-10 stsp struct gotd_imsg_connect iconnect;
1326 ba97b2d7 2024-03-20 stsp struct ibuf *wbuf;
1328 ae7c1b78 2023-01-10 stsp memset(&iconnect, 0, sizeof(iconnect));
1330 ae7c1b78 2023-01-10 stsp s = dup(client->fd);
1331 ae7c1b78 2023-01-10 stsp if (s == -1)
1332 ae7c1b78 2023-01-10 stsp return got_error_from_errno("dup");
1334 ae7c1b78 2023-01-10 stsp iconnect.client_id = client->id;
1335 ae7c1b78 2023-01-10 stsp iconnect.euid = client->euid;
1336 ae7c1b78 2023-01-10 stsp iconnect.egid = client->egid;
1337 ba97b2d7 2024-03-20 stsp iconnect.username_len = strlen(client->username);
1339 ba97b2d7 2024-03-20 stsp wbuf = imsg_create(&client->session->iev.ibuf, GOTD_IMSG_CONNECT,
1340 ba97b2d7 2024-03-20 stsp PROC_GOTD, gotd.pid, sizeof(iconnect) + iconnect.username_len);
1341 ba97b2d7 2024-03-20 stsp if (wbuf == NULL) {
1342 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("imsg compose CONNECT");
1344 ae7c1b78 2023-01-10 stsp return err;
1346 ba97b2d7 2024-03-20 stsp if (imsg_add(wbuf, &iconnect, sizeof(iconnect)) == -1) {
1348 ba97b2d7 2024-03-20 stsp return got_error_from_errno("imsg_add CONNECT");
1350 ba97b2d7 2024-03-20 stsp if (imsg_add(wbuf, client->username, iconnect.username_len) == -1) {
1352 ba97b2d7 2024-03-20 stsp return got_error_from_errno("imsg_add CONNECT");
1355 ba97b2d7 2024-03-20 stsp ibuf_fd_set(wbuf, s);
1356 ba97b2d7 2024-03-20 stsp imsg_close(&client->session->iev.ibuf, wbuf);
1357 ba97b2d7 2024-03-20 stsp gotd_imsg_event_add(&client->session->iev);
1360 ae7c1b78 2023-01-10 stsp * We are no longer interested in messages from this client.
1361 ae7c1b78 2023-01-10 stsp * Further client requests will be handled by the session process.
1363 ae7c1b78 2023-01-10 stsp msgbuf_clear(&client->iev.ibuf.w);
1364 ae7c1b78 2023-01-10 stsp imsg_clear(&client->iev.ibuf);
1365 ae7c1b78 2023-01-10 stsp event_del(&client->iev.ev);
1366 ae7c1b78 2023-01-10 stsp client->fd = -1; /* will be closed via copy in client->iev.ibuf.fd */
1368 ae7c1b78 2023-01-10 stsp return NULL;
1371 5e25db14 2022-12-29 stsp static void
1372 ae7c1b78 2023-01-10 stsp gotd_dispatch_client_session(int fd, short event, void *arg)
1374 b50a2b46 2022-12-29 stsp struct gotd_imsgev *iev = arg;
1375 b50a2b46 2022-12-29 stsp struct imsgbuf *ibuf = &iev->ibuf;
1376 13b2bc37 2022-10-23 stsp struct gotd_child_proc *proc = NULL;
1377 b50a2b46 2022-12-29 stsp struct gotd_client *client = NULL;
1378 13b2bc37 2022-10-23 stsp ssize_t n;
1379 13b2bc37 2022-10-23 stsp int shut = 0;
1380 13b2bc37 2022-10-23 stsp struct imsg imsg;
1382 ae7c1b78 2023-01-10 stsp client = find_client_by_proc_fd(fd);
1383 ae0cca99 2023-02-09 stsp if (client == NULL) {
1384 ae0cca99 2023-02-09 stsp /* Can happen during process teardown. */
1385 ae0cca99 2023-02-09 stsp warnx("cannot find client for fd %d", fd);
1387 ae0cca99 2023-02-09 stsp goto done;
1390 13b2bc37 2022-10-23 stsp if (event & EV_READ) {
1391 13b2bc37 2022-10-23 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1392 13b2bc37 2022-10-23 stsp fatal("imsg_read error");
1393 13b2bc37 2022-10-23 stsp if (n == 0) {
1394 13b2bc37 2022-10-23 stsp /* Connection closed. */
1396 13b2bc37 2022-10-23 stsp goto done;
1400 13b2bc37 2022-10-23 stsp if (event & EV_WRITE) {
1401 13b2bc37 2022-10-23 stsp n = msgbuf_write(&ibuf->w);
1402 13b2bc37 2022-10-23 stsp if (n == -1 && errno != EAGAIN)
1403 13b2bc37 2022-10-23 stsp fatal("msgbuf_write");
1404 13b2bc37 2022-10-23 stsp if (n == 0) {
1405 13b2bc37 2022-10-23 stsp /* Connection closed. */
1407 13b2bc37 2022-10-23 stsp goto done;
1411 ae7c1b78 2023-01-10 stsp proc = client->session;
1412 ae7c1b78 2023-01-10 stsp if (proc == NULL)
1413 ae7c1b78 2023-01-10 stsp fatalx("cannot find session child process for fd %d", fd);
1415 ae7c1b78 2023-01-10 stsp for (;;) {
1416 ae7c1b78 2023-01-10 stsp const struct got_error *err = NULL;
1417 ae7c1b78 2023-01-10 stsp uint32_t client_id = 0;
1418 ae7c1b78 2023-01-10 stsp int do_disconnect = 0, do_start_repo_child = 0;
1420 ae7c1b78 2023-01-10 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
1421 ae7c1b78 2023-01-10 stsp fatal("%s: imsg_get error", __func__);
1422 ae7c1b78 2023-01-10 stsp if (n == 0) /* No more messages. */
1425 ae7c1b78 2023-01-10 stsp switch (imsg.hdr.type) {
1426 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_ERROR:
1427 ae7c1b78 2023-01-10 stsp do_disconnect = 1;
1428 ae7c1b78 2023-01-10 stsp err = gotd_imsg_recv_error(&client_id, &imsg);
1430 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_CLIENT_SESSION_READY:
1431 eac23c30 2023-01-10 stsp if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED) {
1432 ae7c1b78 2023-01-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1435 ae7c1b78 2023-01-10 stsp do_start_repo_child = 1;
1437 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_DISCONNECT:
1438 ae7c1b78 2023-01-10 stsp do_disconnect = 1;
1441 ae7c1b78 2023-01-10 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1445 ae7c1b78 2023-01-10 stsp if (!verify_imsg_src(client, proc, &imsg)) {
1446 ae7c1b78 2023-01-10 stsp log_debug("dropping imsg type %d from PID %d",
1447 ae7c1b78 2023-01-10 stsp imsg.hdr.type, proc->pid);
1448 ae7c1b78 2023-01-10 stsp imsg_free(&imsg);
1452 ae7c1b78 2023-01-10 stsp log_warnx("uid %d: %s", client->euid, err->msg);
1454 ae7c1b78 2023-01-10 stsp if (do_start_repo_child) {
1455 ae7c1b78 2023-01-10 stsp struct gotd_repo *repo;
1456 b09c1279 2023-03-28 stsp const char *name = client->session->repo_name;
1458 ba97b2d7 2024-03-20 stsp repo = gotd_find_repo_by_name(name, &gotd.repos);
1459 ae7c1b78 2023-01-10 stsp if (repo != NULL) {
1460 ae7c1b78 2023-01-10 stsp enum gotd_procid proc_type;
1462 ae7c1b78 2023-01-10 stsp if (client->required_auth & GOTD_AUTH_WRITE)
1463 ae7c1b78 2023-01-10 stsp proc_type = PROC_REPO_WRITE;
1465 ae7c1b78 2023-01-10 stsp proc_type = PROC_REPO_READ;
1467 ae7c1b78 2023-01-10 stsp err = start_repo_child(client, proc_type, repo,
1468 ae7c1b78 2023-01-10 stsp gotd.argv0, gotd.confpath, gotd.daemonize,
1469 ae7c1b78 2023-01-10 stsp gotd.verbosity);
1471 ae7c1b78 2023-01-10 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
1473 ae7c1b78 2023-01-10 stsp if (err) {
1474 ae7c1b78 2023-01-10 stsp log_warnx("uid %d: %s", client->euid, err->msg);
1475 ae7c1b78 2023-01-10 stsp do_disconnect = 1;
1479 ae7c1b78 2023-01-10 stsp if (do_disconnect) {
1481 ae7c1b78 2023-01-10 stsp disconnect_on_error(client, err);
1483 ae7c1b78 2023-01-10 stsp disconnect(client);
1486 ae7c1b78 2023-01-10 stsp imsg_free(&imsg);
1489 ae7c1b78 2023-01-10 stsp if (!shut) {
1490 ae7c1b78 2023-01-10 stsp gotd_imsg_event_add(iev);
1492 ae7c1b78 2023-01-10 stsp /* This pipe is dead. Remove its event handler */
1493 ae7c1b78 2023-01-10 stsp event_del(&iev->ev);
1494 ae7c1b78 2023-01-10 stsp disconnect(client);
1498 ba97b2d7 2024-03-20 stsp static const struct got_error *
1499 ba97b2d7 2024-03-20 stsp connect_notifier_and_session(struct gotd_client *client)
1501 ba97b2d7 2024-03-20 stsp const struct got_error *err = NULL;
1502 ba97b2d7 2024-03-20 stsp struct gotd_imsgev *session_iev = &client->session->iev;
1503 ba97b2d7 2024-03-20 stsp int pipe[2];
1505 ba97b2d7 2024-03-20 stsp if (gotd.notify_proc == NULL)
1506 ba97b2d7 2024-03-20 stsp return NULL;
1508 ba97b2d7 2024-03-20 stsp if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1509 ba97b2d7 2024-03-20 stsp PF_UNSPEC, pipe) == -1)
1510 ba97b2d7 2024-03-20 stsp return got_error_from_errno("socketpair");
1512 ba97b2d7 2024-03-20 stsp /* Pass notifier pipe to session . */
1513 ba97b2d7 2024-03-20 stsp if (gotd_imsg_compose_event(session_iev, GOTD_IMSG_CONNECT_NOTIFIER,
1514 ba97b2d7 2024-03-20 stsp PROC_GOTD, pipe[0], NULL, 0) == -1) {
1515 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("imsg compose CONNECT_NOTIFIER");
1516 ba97b2d7 2024-03-20 stsp close(pipe[0]);
1517 ba97b2d7 2024-03-20 stsp close(pipe[1]);
1518 ba97b2d7 2024-03-20 stsp return err;
1521 ba97b2d7 2024-03-20 stsp /* Pass session pipe to notifier. */
1522 ba97b2d7 2024-03-20 stsp if (gotd_imsg_compose_event(&gotd.notify_proc->iev,
1523 ba97b2d7 2024-03-20 stsp GOTD_IMSG_CONNECT_SESSION, PROC_GOTD, pipe[1], NULL, 0) == -1) {
1524 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("imsg compose CONNECT_SESSION");
1525 ba97b2d7 2024-03-20 stsp close(pipe[1]);
1526 ba97b2d7 2024-03-20 stsp return err;
1529 ba97b2d7 2024-03-20 stsp return NULL;
1532 ae7c1b78 2023-01-10 stsp static void
1533 ae7c1b78 2023-01-10 stsp gotd_dispatch_repo_child(int fd, short event, void *arg)
1535 ae7c1b78 2023-01-10 stsp struct gotd_imsgev *iev = arg;
1536 ae7c1b78 2023-01-10 stsp struct imsgbuf *ibuf = &iev->ibuf;
1537 ae7c1b78 2023-01-10 stsp struct gotd_child_proc *proc = NULL;
1538 ae7c1b78 2023-01-10 stsp struct gotd_client *client;
1539 ae7c1b78 2023-01-10 stsp ssize_t n;
1540 ae7c1b78 2023-01-10 stsp int shut = 0;
1541 ae7c1b78 2023-01-10 stsp struct imsg imsg;
1543 b50a2b46 2022-12-29 stsp client = find_client_by_proc_fd(fd);
1544 ae0cca99 2023-02-09 stsp if (client == NULL) {
1545 ae0cca99 2023-02-09 stsp /* Can happen during process teardown. */
1546 ae0cca99 2023-02-09 stsp warnx("cannot find client for fd %d", fd);
1548 ae0cca99 2023-02-09 stsp goto done;
1551 ae7c1b78 2023-01-10 stsp if (event & EV_READ) {
1552 ae7c1b78 2023-01-10 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1553 ae7c1b78 2023-01-10 stsp fatal("imsg_read error");
1554 ae7c1b78 2023-01-10 stsp if (n == 0) {
1555 ae7c1b78 2023-01-10 stsp /* Connection closed. */
1557 ae7c1b78 2023-01-10 stsp goto done;
1561 ae7c1b78 2023-01-10 stsp if (event & EV_WRITE) {
1562 ae7c1b78 2023-01-10 stsp n = msgbuf_write(&ibuf->w);
1563 ae7c1b78 2023-01-10 stsp if (n == -1 && errno != EAGAIN)
1564 ae7c1b78 2023-01-10 stsp fatal("msgbuf_write");
1565 ae7c1b78 2023-01-10 stsp if (n == 0) {
1566 ae7c1b78 2023-01-10 stsp /* Connection closed. */
1568 ae7c1b78 2023-01-10 stsp goto done;
1572 f7a854cf 2023-01-10 stsp proc = client->repo;
1573 13b2bc37 2022-10-23 stsp if (proc == NULL)
1574 13b2bc37 2022-10-23 stsp fatalx("cannot find child process for fd %d", fd);
1576 13b2bc37 2022-10-23 stsp for (;;) {
1577 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
1578 13b2bc37 2022-10-23 stsp uint32_t client_id = 0;
1579 13b2bc37 2022-10-23 stsp int do_disconnect = 0;
1581 13b2bc37 2022-10-23 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
1582 13b2bc37 2022-10-23 stsp fatal("%s: imsg_get error", __func__);
1583 13b2bc37 2022-10-23 stsp if (n == 0) /* No more messages. */
1586 13b2bc37 2022-10-23 stsp switch (imsg.hdr.type) {
1587 13b2bc37 2022-10-23 stsp case GOTD_IMSG_ERROR:
1588 13b2bc37 2022-10-23 stsp do_disconnect = 1;
1589 13b2bc37 2022-10-23 stsp err = gotd_imsg_recv_error(&client_id, &imsg);
1591 b50a2b46 2022-12-29 stsp case GOTD_IMSG_REPO_CHILD_READY:
1592 ae7c1b78 2023-01-10 stsp err = connect_session(client);
1595 ba97b2d7 2024-03-20 stsp err = connect_notifier_and_session(client);
1598 ae7c1b78 2023-01-10 stsp err = connect_repo_child(client, proc);
1601 13b2bc37 2022-10-23 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1605 13b2bc37 2022-10-23 stsp if (!verify_imsg_src(client, proc, &imsg)) {
1606 13b2bc37 2022-10-23 stsp log_debug("dropping imsg type %d from PID %d",
1607 13b2bc37 2022-10-23 stsp imsg.hdr.type, proc->pid);
1608 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
1612 13b2bc37 2022-10-23 stsp log_warnx("uid %d: %s", client->euid, err->msg);
1614 13b2bc37 2022-10-23 stsp if (do_disconnect) {
1616 13b2bc37 2022-10-23 stsp disconnect_on_error(client, err);
1618 13b2bc37 2022-10-23 stsp disconnect(client);
1621 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
1624 13b2bc37 2022-10-23 stsp if (!shut) {
1625 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(iev);
1627 13b2bc37 2022-10-23 stsp /* This pipe is dead. Remove its event handler */
1628 13b2bc37 2022-10-23 stsp event_del(&iev->ev);
1629 ae7c1b78 2023-01-10 stsp disconnect(client);
1633 13b2bc37 2022-10-23 stsp static pid_t
1634 eec68231 2022-12-14 stsp start_child(enum gotd_procid proc_id, const char *repo_path,
1635 585362fd 2022-10-31 op char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
1637 585362fd 2022-10-31 op char *argv[11];
1638 13b2bc37 2022-10-23 stsp int argc = 0;
1639 13b2bc37 2022-10-23 stsp pid_t pid;
1641 13b2bc37 2022-10-23 stsp switch (pid = fork()) {
1643 13b2bc37 2022-10-23 stsp fatal("cannot fork");
1647 13b2bc37 2022-10-23 stsp close(fd);
1648 13b2bc37 2022-10-23 stsp return pid;
1651 8c6fc146 2022-11-17 stsp if (fd != GOTD_FILENO_MSG_PIPE) {
1652 8c6fc146 2022-11-17 stsp if (dup2(fd, GOTD_FILENO_MSG_PIPE) == -1)
1653 13b2bc37 2022-10-23 stsp fatal("cannot setup imsg fd");
1654 13b2bc37 2022-10-23 stsp } else if (fcntl(fd, F_SETFD, 0) == -1)
1655 13b2bc37 2022-10-23 stsp fatal("cannot setup imsg fd");
1657 13b2bc37 2022-10-23 stsp argv[argc++] = argv0;
1658 13b2bc37 2022-10-23 stsp switch (proc_id) {
1659 d93ecf7d 2022-12-14 stsp case PROC_LISTEN:
1660 d93ecf7d 2022-12-14 stsp argv[argc++] = (char *)"-L";
1662 5e25db14 2022-12-29 stsp case PROC_AUTH:
1663 5e25db14 2022-12-29 stsp argv[argc++] = (char *)"-A";
1665 b0614828 2023-06-19 stsp case PROC_SESSION_READ:
1666 b0614828 2023-06-19 stsp argv[argc++] = (char *)"-s";
1668 b0614828 2023-06-19 stsp case PROC_SESSION_WRITE:
1669 ae7c1b78 2023-01-10 stsp argv[argc++] = (char *)"-S";
1671 13b2bc37 2022-10-23 stsp case PROC_REPO_READ:
1672 13b2bc37 2022-10-23 stsp argv[argc++] = (char *)"-R";
1674 13b2bc37 2022-10-23 stsp case PROC_REPO_WRITE:
1675 13b2bc37 2022-10-23 stsp argv[argc++] = (char *)"-W";
1677 ba97b2d7 2024-03-20 stsp case PROC_NOTIFY:
1678 ba97b2d7 2024-03-20 stsp argv[argc++] = (char *)"-N";
1681 13b2bc37 2022-10-23 stsp fatalx("invalid process id %d", proc_id);
1684 585362fd 2022-10-31 op argv[argc++] = (char *)"-f";
1685 585362fd 2022-10-31 op argv[argc++] = (char *)confpath;
1687 eec68231 2022-12-14 stsp if (repo_path) {
1688 d93ecf7d 2022-12-14 stsp argv[argc++] = (char *)"-P";
1689 eec68231 2022-12-14 stsp argv[argc++] = (char *)repo_path;
1692 13b2bc37 2022-10-23 stsp if (!daemonize)
1693 13b2bc37 2022-10-23 stsp argv[argc++] = (char *)"-d";
1694 13b2bc37 2022-10-23 stsp if (verbosity > 0)
1695 13b2bc37 2022-10-23 stsp argv[argc++] = (char *)"-v";
1696 13b2bc37 2022-10-23 stsp if (verbosity > 1)
1697 13b2bc37 2022-10-23 stsp argv[argc++] = (char *)"-v";
1698 13b2bc37 2022-10-23 stsp argv[argc++] = NULL;
1700 13b2bc37 2022-10-23 stsp execvp(argv0, argv);
1701 13b2bc37 2022-10-23 stsp fatal("execvp");
1704 13b2bc37 2022-10-23 stsp static void
1705 d93ecf7d 2022-12-14 stsp start_listener(char *argv0, const char *confpath, int daemonize, int verbosity)
1707 c929736a 2023-06-22 op struct gotd_child_proc *proc;
1709 c929736a 2023-06-22 op proc = calloc(1, sizeof(*proc));
1710 c929736a 2023-06-22 op if (proc == NULL)
1711 c929736a 2023-06-22 op fatal("calloc");
1713 839338f6 2023-06-22 op TAILQ_INSERT_HEAD(&procs, proc, entry);
1715 839338f6 2023-06-22 op /* proc->tmo is initialized in main() after event_init() */
1717 d93ecf7d 2022-12-14 stsp proc->type = PROC_LISTEN;
1719 d93ecf7d 2022-12-14 stsp if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1720 d93ecf7d 2022-12-14 stsp PF_UNSPEC, proc->pipe) == -1)
1721 d93ecf7d 2022-12-14 stsp fatal("socketpair");
1723 d93ecf7d 2022-12-14 stsp proc->pid = start_child(proc->type, NULL, argv0, confpath,
1724 d93ecf7d 2022-12-14 stsp proc->pipe[1], daemonize, verbosity);
1725 d93ecf7d 2022-12-14 stsp imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1726 b50a2b46 2022-12-29 stsp proc->iev.handler = gotd_dispatch_listener;
1727 d93ecf7d 2022-12-14 stsp proc->iev.events = EV_READ;
1728 d93ecf7d 2022-12-14 stsp proc->iev.handler_arg = NULL;
1730 c929736a 2023-06-22 op gotd.listen_proc = proc;
1733 ba97b2d7 2024-03-20 stsp static void
1734 ba97b2d7 2024-03-20 stsp start_notifier(char *argv0, const char *confpath, int daemonize, int verbosity)
1736 ba97b2d7 2024-03-20 stsp struct gotd_child_proc *proc;
1738 ba97b2d7 2024-03-20 stsp proc = calloc(1, sizeof(*proc));
1739 ba97b2d7 2024-03-20 stsp if (proc == NULL)
1740 ba97b2d7 2024-03-20 stsp fatal("calloc");
1742 ba97b2d7 2024-03-20 stsp TAILQ_INSERT_HEAD(&procs, proc, entry);
1744 ba97b2d7 2024-03-20 stsp /* proc->tmo is initialized in main() after event_init() */
1746 ba97b2d7 2024-03-20 stsp proc->type = PROC_NOTIFY;
1748 ba97b2d7 2024-03-20 stsp if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1749 ba97b2d7 2024-03-20 stsp PF_UNSPEC, proc->pipe) == -1)
1750 ba97b2d7 2024-03-20 stsp fatal("socketpair");
1752 ba97b2d7 2024-03-20 stsp proc->pid = start_child(proc->type, NULL, argv0, confpath,
1753 ba97b2d7 2024-03-20 stsp proc->pipe[1], daemonize, verbosity);
1754 ba97b2d7 2024-03-20 stsp imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1755 ba97b2d7 2024-03-20 stsp proc->iev.handler = gotd_dispatch_notifier;
1756 ba97b2d7 2024-03-20 stsp proc->iev.events = EV_READ;
1757 ba97b2d7 2024-03-20 stsp proc->iev.handler_arg = NULL;
1758 ba97b2d7 2024-03-20 stsp event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1759 ba97b2d7 2024-03-20 stsp gotd_dispatch_notifier, &proc->iev);
1761 ba97b2d7 2024-03-20 stsp gotd.notify_proc = proc;
1764 b50a2b46 2022-12-29 stsp static const struct got_error *
1765 ae7c1b78 2023-01-10 stsp start_session_child(struct gotd_client *client, struct gotd_repo *repo,
1766 ae7c1b78 2023-01-10 stsp char *argv0, const char *confpath, int daemonize, int verbosity)
1768 ae7c1b78 2023-01-10 stsp struct gotd_child_proc *proc;
1770 ae7c1b78 2023-01-10 stsp proc = calloc(1, sizeof(*proc));
1771 ae7c1b78 2023-01-10 stsp if (proc == NULL)
1772 ae7c1b78 2023-01-10 stsp return got_error_from_errno("calloc");
1774 839338f6 2023-06-22 op TAILQ_INSERT_HEAD(&procs, proc, entry);
1775 839338f6 2023-06-22 op evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1777 b0614828 2023-06-19 stsp if (client_is_reading(client))
1778 b0614828 2023-06-19 stsp proc->type = PROC_SESSION_READ;
1780 b0614828 2023-06-19 stsp proc->type = PROC_SESSION_WRITE;
1781 ae7c1b78 2023-01-10 stsp if (strlcpy(proc->repo_name, repo->name,
1782 ae7c1b78 2023-01-10 stsp sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1783 ae7c1b78 2023-01-10 stsp fatalx("repository name too long: %s", repo->name);
1784 ae7c1b78 2023-01-10 stsp log_debug("starting client uid %d session for repository %s",
1785 ae7c1b78 2023-01-10 stsp client->euid, repo->name);
1786 ae7c1b78 2023-01-10 stsp if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1787 ae7c1b78 2023-01-10 stsp sizeof(proc->repo_path))
1788 ae7c1b78 2023-01-10 stsp fatalx("repository path too long: %s", repo->path);
1789 ae7c1b78 2023-01-10 stsp if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1790 ae7c1b78 2023-01-10 stsp PF_UNSPEC, proc->pipe) == -1)
1791 ae7c1b78 2023-01-10 stsp fatal("socketpair");
1792 ae7c1b78 2023-01-10 stsp proc->pid = start_child(proc->type, proc->repo_path, argv0,
1793 ae7c1b78 2023-01-10 stsp confpath, proc->pipe[1], daemonize, verbosity);
1794 ae7c1b78 2023-01-10 stsp imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1795 ae7c1b78 2023-01-10 stsp log_debug("proc %s %s is on fd %d",
1796 ae7c1b78 2023-01-10 stsp gotd_proc_names[proc->type], proc->repo_path,
1797 ae7c1b78 2023-01-10 stsp proc->pipe[0]);
1798 ae7c1b78 2023-01-10 stsp proc->iev.handler = gotd_dispatch_client_session;
1799 ae7c1b78 2023-01-10 stsp proc->iev.events = EV_READ;
1800 ae7c1b78 2023-01-10 stsp proc->iev.handler_arg = NULL;
1801 ae7c1b78 2023-01-10 stsp event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1802 ae7c1b78 2023-01-10 stsp gotd_dispatch_client_session, &proc->iev);
1803 ae7c1b78 2023-01-10 stsp gotd_imsg_event_add(&proc->iev);
1805 ae7c1b78 2023-01-10 stsp client->session = proc;
1806 ae7c1b78 2023-01-10 stsp return NULL;
1809 ae7c1b78 2023-01-10 stsp static const struct got_error *
1810 b50a2b46 2022-12-29 stsp start_repo_child(struct gotd_client *client, enum gotd_procid proc_type,
1811 b50a2b46 2022-12-29 stsp struct gotd_repo *repo, char *argv0, const char *confpath,
1812 585362fd 2022-10-31 op int daemonize, int verbosity)
1814 13b2bc37 2022-10-23 stsp struct gotd_child_proc *proc;
1816 b50a2b46 2022-12-29 stsp if (proc_type != PROC_REPO_READ && proc_type != PROC_REPO_WRITE)
1817 b50a2b46 2022-12-29 stsp return got_error_msg(GOT_ERR_NOT_IMPL, "bad process type");
1819 b50a2b46 2022-12-29 stsp proc = calloc(1, sizeof(*proc));
1820 b50a2b46 2022-12-29 stsp if (proc == NULL)
1821 b50a2b46 2022-12-29 stsp return got_error_from_errno("calloc");
1823 839338f6 2023-06-22 op TAILQ_INSERT_HEAD(&procs, proc, entry);
1824 839338f6 2023-06-22 op evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1826 b50a2b46 2022-12-29 stsp proc->type = proc_type;
1827 b50a2b46 2022-12-29 stsp if (strlcpy(proc->repo_name, repo->name,
1828 b50a2b46 2022-12-29 stsp sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1829 b50a2b46 2022-12-29 stsp fatalx("repository name too long: %s", repo->name);
1830 b50a2b46 2022-12-29 stsp log_debug("starting %s for repository %s",
1831 b50a2b46 2022-12-29 stsp proc->type == PROC_REPO_READ ? "reader" : "writer", repo->name);
1832 9b7f22a6 2023-01-08 stsp if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1833 9b7f22a6 2023-01-08 stsp sizeof(proc->repo_path))
1834 9b7f22a6 2023-01-08 stsp fatalx("repository path too long: %s", repo->path);
1835 b50a2b46 2022-12-29 stsp if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1836 b50a2b46 2022-12-29 stsp PF_UNSPEC, proc->pipe) == -1)
1837 b50a2b46 2022-12-29 stsp fatal("socketpair");
1838 b50a2b46 2022-12-29 stsp proc->pid = start_child(proc->type, proc->repo_path, argv0,
1839 b50a2b46 2022-12-29 stsp confpath, proc->pipe[1], daemonize, verbosity);
1840 b50a2b46 2022-12-29 stsp imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1841 b50a2b46 2022-12-29 stsp log_debug("proc %s %s is on fd %d",
1842 b50a2b46 2022-12-29 stsp gotd_proc_names[proc->type], proc->repo_path,
1843 b50a2b46 2022-12-29 stsp proc->pipe[0]);
1844 b50a2b46 2022-12-29 stsp proc->iev.handler = gotd_dispatch_repo_child;
1845 b50a2b46 2022-12-29 stsp proc->iev.events = EV_READ;
1846 b50a2b46 2022-12-29 stsp proc->iev.handler_arg = NULL;
1847 b50a2b46 2022-12-29 stsp event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1848 b50a2b46 2022-12-29 stsp gotd_dispatch_repo_child, &proc->iev);
1849 b50a2b46 2022-12-29 stsp gotd_imsg_event_add(&proc->iev);
1851 f7a854cf 2023-01-10 stsp client->repo = proc;
1852 5e25db14 2022-12-29 stsp return NULL;
1855 5e25db14 2022-12-29 stsp static const struct got_error *
1856 5e25db14 2022-12-29 stsp start_auth_child(struct gotd_client *client, int required_auth,
1857 5e25db14 2022-12-29 stsp struct gotd_repo *repo, char *argv0, const char *confpath,
1858 5e25db14 2022-12-29 stsp int daemonize, int verbosity)
1860 365cf0f3 2022-12-29 stsp const struct got_error *err = NULL;
1861 5e25db14 2022-12-29 stsp struct gotd_child_proc *proc;
1862 5e25db14 2022-12-29 stsp struct gotd_imsg_auth iauth;
1865 5e25db14 2022-12-29 stsp memset(&iauth, 0, sizeof(iauth));
1867 365cf0f3 2022-12-29 stsp fd = dup(client->fd);
1868 365cf0f3 2022-12-29 stsp if (fd == -1)
1869 365cf0f3 2022-12-29 stsp return got_error_from_errno("dup");
1871 5e25db14 2022-12-29 stsp proc = calloc(1, sizeof(*proc));
1872 365cf0f3 2022-12-29 stsp if (proc == NULL) {
1873 365cf0f3 2022-12-29 stsp err = got_error_from_errno("calloc");
1874 365cf0f3 2022-12-29 stsp close(fd);
1875 365cf0f3 2022-12-29 stsp return err;
1878 839338f6 2023-06-22 op TAILQ_INSERT_HEAD(&procs, proc, entry);
1879 839338f6 2023-06-22 op evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1881 5e25db14 2022-12-29 stsp proc->type = PROC_AUTH;
1882 5e25db14 2022-12-29 stsp if (strlcpy(proc->repo_name, repo->name,
1883 5e25db14 2022-12-29 stsp sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1884 5e25db14 2022-12-29 stsp fatalx("repository name too long: %s", repo->name);
1885 5e25db14 2022-12-29 stsp log_debug("starting auth for uid %d repository %s",
1886 5e25db14 2022-12-29 stsp client->euid, repo->name);
1887 9b7f22a6 2023-01-08 stsp if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1888 9b7f22a6 2023-01-08 stsp sizeof(proc->repo_path))
1889 9b7f22a6 2023-01-08 stsp fatalx("repository path too long: %s", repo->path);
1890 5e25db14 2022-12-29 stsp if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1891 5e25db14 2022-12-29 stsp PF_UNSPEC, proc->pipe) == -1)
1892 5e25db14 2022-12-29 stsp fatal("socketpair");
1893 5e25db14 2022-12-29 stsp proc->pid = start_child(proc->type, proc->repo_path, argv0,
1894 5e25db14 2022-12-29 stsp confpath, proc->pipe[1], daemonize, verbosity);
1895 5e25db14 2022-12-29 stsp imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1896 5e25db14 2022-12-29 stsp log_debug("proc %s %s is on fd %d",
1897 5e25db14 2022-12-29 stsp gotd_proc_names[proc->type], proc->repo_path,
1898 5e25db14 2022-12-29 stsp proc->pipe[0]);
1899 5e25db14 2022-12-29 stsp proc->iev.handler = gotd_dispatch_auth_child;
1900 5e25db14 2022-12-29 stsp proc->iev.events = EV_READ;
1901 5e25db14 2022-12-29 stsp proc->iev.handler_arg = NULL;
1902 5e25db14 2022-12-29 stsp event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1903 5e25db14 2022-12-29 stsp gotd_dispatch_auth_child, &proc->iev);
1904 5e25db14 2022-12-29 stsp gotd_imsg_event_add(&proc->iev);
1906 5e25db14 2022-12-29 stsp iauth.euid = client->euid;
1907 5e25db14 2022-12-29 stsp iauth.egid = client->egid;
1908 5e25db14 2022-12-29 stsp iauth.required_auth = required_auth;
1909 5e25db14 2022-12-29 stsp iauth.client_id = client->id;
1910 5e25db14 2022-12-29 stsp if (gotd_imsg_compose_event(&proc->iev, GOTD_IMSG_AUTHENTICATE,
1911 365cf0f3 2022-12-29 stsp PROC_GOTD, fd, &iauth, sizeof(iauth)) == -1) {
1912 5e25db14 2022-12-29 stsp log_warn("imsg compose AUTHENTICATE");
1913 365cf0f3 2022-12-29 stsp close(fd);
1914 365cf0f3 2022-12-29 stsp /* Let the auth_timeout handler tidy up. */
1917 5e25db14 2022-12-29 stsp client->auth = proc;
1918 5e25db14 2022-12-29 stsp client->required_auth = required_auth;
1919 b50a2b46 2022-12-29 stsp return NULL;
1922 eec68231 2022-12-14 stsp static void
1923 b0614828 2023-06-19 stsp apply_unveil_repo_readonly(const char *repo_path, int need_tmpdir)
1925 b0614828 2023-06-19 stsp if (need_tmpdir) {
1926 b0614828 2023-06-19 stsp if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1927 b0614828 2023-06-19 stsp fatal("unveil %s", GOT_TMPDIR_STR);
1930 eec68231 2022-12-14 stsp if (unveil(repo_path, "r") == -1)
1931 eec68231 2022-12-14 stsp fatal("unveil %s", repo_path);
1933 44587340 2022-12-30 stsp if (unveil(NULL, NULL) == -1)
1934 44587340 2022-12-30 stsp fatal("unveil");
1937 44587340 2022-12-30 stsp static void
1938 ae7c1b78 2023-01-10 stsp apply_unveil_repo_readwrite(const char *repo_path)
1940 ae7c1b78 2023-01-10 stsp if (unveil(repo_path, "rwc") == -1)
1941 ae7c1b78 2023-01-10 stsp fatal("unveil %s", repo_path);
1943 ae7c1b78 2023-01-10 stsp if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1944 ae7c1b78 2023-01-10 stsp fatal("unveil %s", GOT_TMPDIR_STR);
1946 ae7c1b78 2023-01-10 stsp if (unveil(NULL, NULL) == -1)
1947 ae7c1b78 2023-01-10 stsp fatal("unveil");
1950 ae7c1b78 2023-01-10 stsp static void
1951 44587340 2022-12-30 stsp apply_unveil_none(void)
1953 44587340 2022-12-30 stsp if (unveil("/", "") == -1)
1954 44587340 2022-12-30 stsp fatal("unveil");
1956 eec68231 2022-12-14 stsp if (unveil(NULL, NULL) == -1)
1957 eec68231 2022-12-14 stsp fatal("unveil");
1960 13b2bc37 2022-10-23 stsp static void
1961 ae7c1b78 2023-01-10 stsp apply_unveil_selfexec(void)
1963 b50a2b46 2022-12-29 stsp if (unveil(gotd.argv0, "x") == -1)
1964 b50a2b46 2022-12-29 stsp fatal("unveil %s", gotd.argv0);
1966 13b2bc37 2022-10-23 stsp if (unveil(NULL, NULL) == -1)
1967 13b2bc37 2022-10-23 stsp fatal("unveil");
1970 abe89edb 2023-11-16 stsp static void
1971 abe89edb 2023-11-16 stsp set_max_datasize(void)
1973 abe89edb 2023-11-16 stsp struct rlimit rl;
1975 abe89edb 2023-11-16 stsp if (getrlimit(RLIMIT_DATA, &rl) != 0)
1978 abe89edb 2023-11-16 stsp rl.rlim_cur = rl.rlim_max;
1979 abe89edb 2023-11-16 stsp setrlimit(RLIMIT_DATA, &rl);
1982 ba97b2d7 2024-03-20 stsp static void
1983 ba97b2d7 2024-03-20 stsp unveil_notification_helpers(void)
1985 ba97b2d7 2024-03-20 stsp const char *helpers[] = {
1986 ba97b2d7 2024-03-20 stsp GOTD_PATH_PROG_NOTIFY_EMAIL,
1987 ba97b2d7 2024-03-20 stsp GOTD_PATH_PROG_NOTIFY_HTTP,
1991 ba97b2d7 2024-03-20 stsp for (i = 0; i < nitems(helpers); i++) {
1992 ba97b2d7 2024-03-20 stsp if (unveil(helpers[i], "x") == 0)
1994 ba97b2d7 2024-03-20 stsp fatal("unveil %s", helpers[i]);
1997 ba97b2d7 2024-03-20 stsp if (unveil(NULL, NULL) == -1)
1998 ba97b2d7 2024-03-20 stsp fatal("unveil");
2002 13b2bc37 2022-10-23 stsp main(int argc, char **argv)
2004 13b2bc37 2022-10-23 stsp const struct got_error *error = NULL;
2005 13b2bc37 2022-10-23 stsp int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
2006 13b2bc37 2022-10-23 stsp const char *confpath = GOTD_CONF_PATH;
2007 13b2bc37 2022-10-23 stsp char *argv0 = argv[0];
2008 13b2bc37 2022-10-23 stsp char title[2048];
2009 13b2bc37 2022-10-23 stsp struct passwd *pw = NULL;
2010 13b2bc37 2022-10-23 stsp char *repo_path = NULL;
2011 13b2bc37 2022-10-23 stsp enum gotd_procid proc_id = PROC_GOTD;
2012 839338f6 2023-06-22 op struct event evsigint, evsigterm, evsighup, evsigusr1, evsigchld;
2013 13b2bc37 2022-10-23 stsp int *pack_fds = NULL, *temp_fds = NULL;
2014 9afa3de2 2023-04-04 stsp struct gotd_repo *repo = NULL;
2015 ba97b2d7 2024-03-20 stsp char *default_sender = NULL;
2016 ba97b2d7 2024-03-20 stsp char hostname[HOST_NAME_MAX + 1];
2017 ba97b2d7 2024-03-20 stsp FILE *diff_f1 = NULL, *diff_f2 = NULL;
2018 ba97b2d7 2024-03-20 stsp int diff_fd1 = -1, diff_fd2 = -1;
2020 839338f6 2023-06-22 op TAILQ_INIT(&procs);
2022 13b2bc37 2022-10-23 stsp log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
2024 ba97b2d7 2024-03-20 stsp while ((ch = getopt(argc, argv, "Adf:LnNP:RsSvW")) != -1) {
2025 13b2bc37 2022-10-23 stsp switch (ch) {
2027 5e25db14 2022-12-29 stsp proc_id = PROC_AUTH;
2030 13b2bc37 2022-10-23 stsp daemonize = 0;
2033 13b2bc37 2022-10-23 stsp confpath = optarg;
2036 d93ecf7d 2022-12-14 stsp proc_id = PROC_LISTEN;
2039 13b2bc37 2022-10-23 stsp noaction = 1;
2042 ba97b2d7 2024-03-20 stsp proc_id = PROC_NOTIFY;
2045 6f319063 2022-10-27 stsp repo_path = realpath(optarg, NULL);
2046 6f319063 2022-10-27 stsp if (repo_path == NULL)
2047 6f319063 2022-10-27 stsp fatal("realpath '%s'", optarg);
2050 13b2bc37 2022-10-23 stsp proc_id = PROC_REPO_READ;
2053 b0614828 2023-06-19 stsp proc_id = PROC_SESSION_READ;
2056 b0614828 2023-06-19 stsp proc_id = PROC_SESSION_WRITE;
2059 6f319063 2022-10-27 stsp if (verbosity < 3)
2060 6f319063 2022-10-27 stsp verbosity++;
2063 13b2bc37 2022-10-23 stsp proc_id = PROC_REPO_WRITE;
2070 13b2bc37 2022-10-23 stsp argc -= optind;
2071 13b2bc37 2022-10-23 stsp argv += optind;
2073 13b2bc37 2022-10-23 stsp if (argc != 0)
2076 b50a2b46 2022-12-29 stsp if (geteuid() && (proc_id == PROC_GOTD || proc_id == PROC_LISTEN))
2077 13b2bc37 2022-10-23 stsp fatalx("need root privileges");
2079 4b3827cd 2023-07-08 stsp if (parse_config(confpath, proc_id, &gotd) != 0)
2082 13b2bc37 2022-10-23 stsp pw = getpwnam(gotd.user_name);
2083 13b2bc37 2022-10-23 stsp if (pw == NULL)
2084 898c8f8f 2022-12-29 op fatalx("user %s not found", gotd.user_name);
2086 f4e8c21c 2023-01-17 op if (pw->pw_uid == 0)
2087 f4e8c21c 2023-01-17 op fatalx("cannot run %s as the superuser", getprogname());
2089 f4e8c21c 2023-01-17 op if (noaction) {
2090 f4e8c21c 2023-01-17 op fprintf(stderr, "configuration OK\n");
2094 f4e8c21c 2023-01-17 op gotd.argv0 = argv0;
2095 f4e8c21c 2023-01-17 op gotd.daemonize = daemonize;
2096 f4e8c21c 2023-01-17 op gotd.verbosity = verbosity;
2097 f4e8c21c 2023-01-17 op gotd.confpath = confpath;
2099 f4e8c21c 2023-01-17 op /* Require an absolute path in argv[0] for reliable re-exec. */
2100 f4e8c21c 2023-01-17 op if (!got_path_is_absolute(argv0))
2101 f4e8c21c 2023-01-17 op fatalx("bad path \"%s\": must be an absolute path", argv0);
2103 f4e8c21c 2023-01-17 op log_init(daemonize ? 0 : 1, LOG_DAEMON);
2104 f4e8c21c 2023-01-17 op log_setverbose(verbosity);
2106 b1142068 2022-12-05 stsp if (proc_id == PROC_GOTD) {
2107 d93ecf7d 2022-12-14 stsp snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2108 d93ecf7d 2022-12-14 stsp arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
2109 d93ecf7d 2022-12-14 stsp if (daemonize && daemon(1, 0) == -1)
2110 d93ecf7d 2022-12-14 stsp fatal("daemon");
2111 f7eb3370 2023-01-23 stsp gotd.pid = getpid();
2112 f7eb3370 2023-01-23 stsp start_listener(argv0, confpath, daemonize, verbosity);
2113 ba97b2d7 2024-03-20 stsp start_notifier(argv0, confpath, daemonize, verbosity);
2114 d93ecf7d 2022-12-14 stsp } else if (proc_id == PROC_LISTEN) {
2115 d93ecf7d 2022-12-14 stsp snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2116 b1142068 2022-12-05 stsp if (verbosity) {
2117 b1142068 2022-12-05 stsp log_info("socket: %s", gotd.unix_socket_path);
2118 b1142068 2022-12-05 stsp log_info("user: %s", pw->pw_name);
2121 13b2bc37 2022-10-23 stsp fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
2122 6f854dde 2023-01-04 stsp pw->pw_gid);
2123 13b2bc37 2022-10-23 stsp if (fd == -1) {
2124 13b2bc37 2022-10-23 stsp fatal("cannot listen on unix socket %s",
2125 13b2bc37 2022-10-23 stsp gotd.unix_socket_path);
2127 5e25db14 2022-12-29 stsp } else if (proc_id == PROC_AUTH) {
2128 5e25db14 2022-12-29 stsp snprintf(title, sizeof(title), "%s %s",
2129 5e25db14 2022-12-29 stsp gotd_proc_names[proc_id], repo_path);
2130 ae7c1b78 2023-01-10 stsp } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE ||
2131 b0614828 2023-06-19 stsp proc_id == PROC_SESSION_READ || proc_id == PROC_SESSION_WRITE) {
2132 13b2bc37 2022-10-23 stsp error = got_repo_pack_fds_open(&pack_fds);
2133 13b2bc37 2022-10-23 stsp if (error != NULL)
2134 13b2bc37 2022-10-23 stsp fatalx("cannot open pack tempfiles: %s", error->msg);
2135 13b2bc37 2022-10-23 stsp error = got_repo_temp_fds_open(&temp_fds);
2136 13b2bc37 2022-10-23 stsp if (error != NULL)
2137 13b2bc37 2022-10-23 stsp fatalx("cannot open pack tempfiles: %s", error->msg);
2138 13b2bc37 2022-10-23 stsp if (repo_path == NULL)
2139 13b2bc37 2022-10-23 stsp fatalx("repository path not specified");
2140 13b2bc37 2022-10-23 stsp snprintf(title, sizeof(title), "%s %s",
2141 13b2bc37 2022-10-23 stsp gotd_proc_names[proc_id], repo_path);
2142 ba97b2d7 2024-03-20 stsp } else if (proc_id == PROC_NOTIFY) {
2143 ba97b2d7 2024-03-20 stsp snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2144 ba97b2d7 2024-03-20 stsp if (gethostname(hostname, sizeof(hostname)) == -1)
2145 ba97b2d7 2024-03-20 stsp fatal("gethostname");
2146 ba97b2d7 2024-03-20 stsp if (asprintf(&default_sender, "%s@%s",
2147 ba97b2d7 2024-03-20 stsp pw->pw_name, hostname) == -1)
2148 ba97b2d7 2024-03-20 stsp fatal("asprintf");
2150 13b2bc37 2022-10-23 stsp fatal("invalid process id %d", proc_id);
2152 13b2bc37 2022-10-23 stsp setproctitle("%s", title);
2153 13b2bc37 2022-10-23 stsp log_procinit(title);
2155 13b2bc37 2022-10-23 stsp /* Drop root privileges. */
2156 13b2bc37 2022-10-23 stsp if (setgid(pw->pw_gid) == -1)
2157 13b2bc37 2022-10-23 stsp fatal("setgid %d failed", pw->pw_gid);
2158 13b2bc37 2022-10-23 stsp if (setuid(pw->pw_uid) == -1)
2159 13b2bc37 2022-10-23 stsp fatal("setuid %d failed", pw->pw_uid);
2161 13b2bc37 2022-10-23 stsp event_init();
2163 13b2bc37 2022-10-23 stsp switch (proc_id) {
2164 13b2bc37 2022-10-23 stsp case PROC_GOTD:
2165 13b2bc37 2022-10-23 stsp #ifndef PROFILE
2166 ae7c1b78 2023-01-10 stsp /* "exec" promise will be limited to argv[0] via unveil(2). */
2167 ae7c1b78 2023-01-10 stsp if (pledge("stdio proc exec sendfd recvfd unveil", NULL) == -1)
2168 13b2bc37 2022-10-23 stsp err(1, "pledge");
2171 d93ecf7d 2022-12-14 stsp case PROC_LISTEN:
2172 d93ecf7d 2022-12-14 stsp #ifndef PROFILE
2173 77f619a8 2023-01-04 stsp if (pledge("stdio sendfd unix unveil", NULL) == -1)
2174 d93ecf7d 2022-12-14 stsp err(1, "pledge");
2177 77f619a8 2023-01-04 stsp * Ensure that AF_UNIX bind(2) cannot be used with any other
2178 77f619a8 2023-01-04 stsp * sockets by revoking all filesystem access via unveil(2).
2180 77f619a8 2023-01-04 stsp apply_unveil_none();
2182 40b85cca 2023-01-03 stsp listen_main(title, fd, gotd.connection_limits,
2183 40b85cca 2023-01-03 stsp gotd.nconnection_limits);
2184 d93ecf7d 2022-12-14 stsp /* NOTREACHED */
2186 5e25db14 2022-12-29 stsp case PROC_AUTH:
2187 5e25db14 2022-12-29 stsp #ifndef PROFILE
2188 44587340 2022-12-30 stsp if (pledge("stdio getpw recvfd unix unveil", NULL) == -1)
2189 5e25db14 2022-12-29 stsp err(1, "pledge");
2192 44587340 2022-12-30 stsp * We need the "unix" pledge promise for getpeername(2) only.
2193 44587340 2022-12-30 stsp * Ensure that AF_UNIX bind(2) cannot be used by revoking all
2194 44587340 2022-12-30 stsp * filesystem access via unveil(2). Access to password database
2195 44587340 2022-12-30 stsp * files will still work since "getpw" bypasses unveil(2).
2197 44587340 2022-12-30 stsp apply_unveil_none();
2199 5e25db14 2022-12-29 stsp auth_main(title, &gotd.repos, repo_path);
2200 5e25db14 2022-12-29 stsp /* NOTREACHED */
2202 b0614828 2023-06-19 stsp case PROC_SESSION_READ:
2203 b0614828 2023-06-19 stsp case PROC_SESSION_WRITE:
2204 ae7c1b78 2023-01-10 stsp #ifndef PROFILE
2206 ae7c1b78 2023-01-10 stsp * The "recvfd" promise is only needed during setup and
2207 ae7c1b78 2023-01-10 stsp * will be removed in a later pledge(2) call.
2209 ae7c1b78 2023-01-10 stsp if (pledge("stdio rpath wpath cpath recvfd sendfd fattr flock "
2210 ae7c1b78 2023-01-10 stsp "unveil", NULL) == -1)
2211 ae7c1b78 2023-01-10 stsp err(1, "pledge");
2213 b0614828 2023-06-19 stsp if (proc_id == PROC_SESSION_READ)
2214 b0614828 2023-06-19 stsp apply_unveil_repo_readonly(repo_path, 1);
2216 b0614828 2023-06-19 stsp apply_unveil_repo_readwrite(repo_path);
2217 ba97b2d7 2024-03-20 stsp repo = gotd_find_repo_by_path(repo_path, &gotd);
2218 ba97b2d7 2024-03-20 stsp if (repo == NULL)
2219 ba97b2d7 2024-03-20 stsp fatalx("no repository for path %s", repo_path);
2221 e70fd952 2024-03-22 stsp if (proc_id == PROC_SESSION_READ)
2222 e70fd952 2024-03-22 stsp session_read_main(title, repo_path, pack_fds, temp_fds,
2223 e70fd952 2024-03-22 stsp &gotd.request_timeout, repo);
2225 e70fd952 2024-03-22 stsp session_write_main(title, repo_path, pack_fds, temp_fds,
2226 e70fd952 2024-03-22 stsp &gotd.request_timeout, repo);
2227 ae7c1b78 2023-01-10 stsp /* NOTREACHED */
2229 13b2bc37 2022-10-23 stsp case PROC_REPO_READ:
2230 abe89edb 2023-11-16 stsp set_max_datasize();
2231 13b2bc37 2022-10-23 stsp #ifndef PROFILE
2232 eec68231 2022-12-14 stsp if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2233 13b2bc37 2022-10-23 stsp err(1, "pledge");
2235 b0614828 2023-06-19 stsp apply_unveil_repo_readonly(repo_path, 0);
2236 eec68231 2022-12-14 stsp repo_read_main(title, repo_path, pack_fds, temp_fds);
2237 13b2bc37 2022-10-23 stsp /* NOTREACHED */
2239 13b2bc37 2022-10-23 stsp case PROC_REPO_WRITE:
2240 abe89edb 2023-11-16 stsp set_max_datasize();
2242 ba97b2d7 2024-03-20 stsp diff_f1 = got_opentemp();
2243 ba97b2d7 2024-03-20 stsp if (diff_f1 == NULL)
2244 ba97b2d7 2024-03-20 stsp fatal("got_opentemp");
2245 ba97b2d7 2024-03-20 stsp diff_f2 = got_opentemp();
2246 ba97b2d7 2024-03-20 stsp if (diff_f2 == NULL)
2247 ba97b2d7 2024-03-20 stsp fatal("got_opentemp");
2248 ba97b2d7 2024-03-20 stsp diff_fd1 = got_opentempfd();
2249 ba97b2d7 2024-03-20 stsp if (diff_fd1 == -1)
2250 ba97b2d7 2024-03-20 stsp fatal("got_opentempfd");
2251 ba97b2d7 2024-03-20 stsp diff_fd2 = got_opentempfd();
2252 ba97b2d7 2024-03-20 stsp if (diff_fd2 == -1)
2253 ba97b2d7 2024-03-20 stsp fatal("got_opentempfd");
2254 13b2bc37 2022-10-23 stsp #ifndef PROFILE
2255 eec68231 2022-12-14 stsp if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2256 13b2bc37 2022-10-23 stsp err(1, "pledge");
2258 b0614828 2023-06-19 stsp apply_unveil_repo_readonly(repo_path, 0);
2259 9afa3de2 2023-04-04 stsp repo = gotd_find_repo_by_path(repo_path, &gotd);
2260 9afa3de2 2023-04-04 stsp if (repo == NULL)
2261 9afa3de2 2023-04-04 stsp fatalx("no repository for path %s", repo_path);
2262 9afa3de2 2023-04-04 stsp repo_write_main(title, repo_path, pack_fds, temp_fds,
2263 ba97b2d7 2024-03-20 stsp diff_f1, diff_f2, diff_fd1, diff_fd2,
2264 9afa3de2 2023-04-04 stsp &repo->protected_tag_namespaces,
2265 9afa3de2 2023-04-04 stsp &repo->protected_branch_namespaces,
2266 9afa3de2 2023-04-04 stsp &repo->protected_branches);
2267 13b2bc37 2022-10-23 stsp /* NOTREACHED */
2269 ba97b2d7 2024-03-20 stsp case PROC_NOTIFY:
2270 ba97b2d7 2024-03-20 stsp #ifndef PROFILE
2271 ba97b2d7 2024-03-20 stsp if (pledge("stdio proc exec recvfd unveil", NULL) == -1)
2272 ba97b2d7 2024-03-20 stsp err(1, "pledge");
2275 ba97b2d7 2024-03-20 stsp * Limit "exec" promise to notification helpers via unveil(2).
2277 ba97b2d7 2024-03-20 stsp unveil_notification_helpers();
2279 ba97b2d7 2024-03-20 stsp notify_main(title, &gotd.repos, default_sender);
2280 ba97b2d7 2024-03-20 stsp /* NOTREACHED */
2283 13b2bc37 2022-10-23 stsp fatal("invalid process id %d", proc_id);
2286 13b2bc37 2022-10-23 stsp if (proc_id != PROC_GOTD)
2287 13b2bc37 2022-10-23 stsp fatal("invalid process id %d", proc_id);
2289 839338f6 2023-06-22 op evtimer_set(&gotd.listen_proc->tmo, kill_proc_timeout,
2290 839338f6 2023-06-22 op gotd.listen_proc);
2291 ba97b2d7 2024-03-20 stsp if (gotd.notify_proc) {
2292 ba97b2d7 2024-03-20 stsp evtimer_set(&gotd.notify_proc->tmo, kill_proc_timeout,
2293 ba97b2d7 2024-03-20 stsp gotd.notify_proc);
2296 ae7c1b78 2023-01-10 stsp apply_unveil_selfexec();
2298 13b2bc37 2022-10-23 stsp signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
2299 13b2bc37 2022-10-23 stsp signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
2300 13b2bc37 2022-10-23 stsp signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
2301 13b2bc37 2022-10-23 stsp signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
2302 839338f6 2023-06-22 op signal_set(&evsigchld, SIGCHLD, gotd_sighdlr, NULL);
2303 13b2bc37 2022-10-23 stsp signal(SIGPIPE, SIG_IGN);
2305 13b2bc37 2022-10-23 stsp signal_add(&evsigint, NULL);
2306 13b2bc37 2022-10-23 stsp signal_add(&evsigterm, NULL);
2307 13b2bc37 2022-10-23 stsp signal_add(&evsighup, NULL);
2308 13b2bc37 2022-10-23 stsp signal_add(&evsigusr1, NULL);
2309 839338f6 2023-06-22 op signal_add(&evsigchld, NULL);
2311 c929736a 2023-06-22 op gotd_imsg_event_add(&gotd.listen_proc->iev);
2312 ba97b2d7 2024-03-20 stsp if (gotd.notify_proc)
2313 ba97b2d7 2024-03-20 stsp gotd_imsg_event_add(&gotd.notify_proc->iev);
2315 13b2bc37 2022-10-23 stsp event_dispatch();
2317 13b2bc37 2022-10-23 stsp free(repo_path);
2318 ba97b2d7 2024-03-20 stsp free(default_sender);
2319 ae7c1b78 2023-01-10 stsp gotd_shutdown();