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"
70 5fb267cb 2024-09-08 op #include "secrets.h"
72 13b2bc37 2022-10-23 stsp #ifndef nitems
73 13b2bc37 2022-10-23 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
76 eac23c30 2023-01-10 stsp enum gotd_client_state {
77 eac23c30 2023-01-10 stsp GOTD_CLIENT_STATE_NEW,
78 eac23c30 2023-01-10 stsp GOTD_CLIENT_STATE_ACCESS_GRANTED,
81 c929736a 2023-06-22 op struct gotd_child_proc {
83 c929736a 2023-06-22 op enum gotd_procid type;
84 c929736a 2023-06-22 op char repo_name[NAME_MAX];
85 c929736a 2023-06-22 op char repo_path[PATH_MAX];
87 c929736a 2023-06-22 op struct gotd_imsgev iev;
88 839338f6 2023-06-22 op struct event tmo;
90 839338f6 2023-06-22 op TAILQ_ENTRY(gotd_child_proc) entry;
92 839338f6 2023-06-22 op TAILQ_HEAD(gotd_procs, gotd_child_proc) procs;
94 13b2bc37 2022-10-23 stsp struct gotd_client {
95 13b2bc37 2022-10-23 stsp STAILQ_ENTRY(gotd_client) entry;
96 13b2bc37 2022-10-23 stsp enum gotd_client_state state;
97 13b2bc37 2022-10-23 stsp uint32_t id;
99 13b2bc37 2022-10-23 stsp struct gotd_imsgev iev;
100 13b2bc37 2022-10-23 stsp struct event tmo;
101 13b2bc37 2022-10-23 stsp uid_t euid;
102 13b2bc37 2022-10-23 stsp gid_t egid;
103 56624d2b 2023-12-27 stsp char *username;
104 f7a854cf 2023-01-10 stsp struct gotd_child_proc *repo;
105 5e25db14 2022-12-29 stsp struct gotd_child_proc *auth;
106 ae7c1b78 2023-01-10 stsp struct gotd_child_proc *session;
107 5e25db14 2022-12-29 stsp int required_auth;
109 13b2bc37 2022-10-23 stsp STAILQ_HEAD(gotd_clients, gotd_client);
111 13b2bc37 2022-10-23 stsp static struct gotd_clients gotd_clients[GOTD_CLIENT_TABLE_SIZE];
112 13b2bc37 2022-10-23 stsp static SIPHASH_KEY clients_hash_key;
113 13b2bc37 2022-10-23 stsp volatile int client_cnt;
114 ef4e2f01 2022-12-29 stsp static struct timeval auth_timeout = { 5, 0 };
115 13b2bc37 2022-10-23 stsp static struct gotd gotd;
117 13b2bc37 2022-10-23 stsp void gotd_sighdlr(int sig, short event, void *arg);
118 f1752522 2022-10-29 stsp static void gotd_shutdown(void);
119 ae7c1b78 2023-01-10 stsp static const struct got_error *start_session_child(struct gotd_client *,
120 ae7c1b78 2023-01-10 stsp struct gotd_repo *, char *, const char *, int, int);
121 b50a2b46 2022-12-29 stsp static const struct got_error *start_repo_child(struct gotd_client *,
122 b50a2b46 2022-12-29 stsp enum gotd_procid, struct gotd_repo *, char *, const char *, int, int);
123 5e25db14 2022-12-29 stsp static const struct got_error *start_auth_child(struct gotd_client *, int,
124 5e25db14 2022-12-29 stsp struct gotd_repo *, char *, const char *, int, int);
125 b50a2b46 2022-12-29 stsp static void kill_proc(struct gotd_child_proc *, int);
126 839338f6 2023-06-22 op static void disconnect(struct gotd_client *);
128 13b2bc37 2022-10-23 stsp __dead static void
131 5fb267cb 2024-09-08 op fprintf(stderr, "usage: %s [-dnv] [-f config-file] [-s secrets]\n",
132 5fb267cb 2024-09-08 op getprogname());
137 13b2bc37 2022-10-23 stsp unix_socket_listen(const char *unix_socket_path, uid_t uid, gid_t gid)
139 13b2bc37 2022-10-23 stsp struct sockaddr_un sun;
140 13b2bc37 2022-10-23 stsp int fd = -1;
141 13b2bc37 2022-10-23 stsp mode_t old_umask, mode;
143 13b2bc37 2022-10-23 stsp fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
144 13b2bc37 2022-10-23 stsp if (fd == -1) {
145 13b2bc37 2022-10-23 stsp log_warn("socket");
149 13b2bc37 2022-10-23 stsp sun.sun_family = AF_UNIX;
150 13b2bc37 2022-10-23 stsp if (strlcpy(sun.sun_path, unix_socket_path,
151 13b2bc37 2022-10-23 stsp sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
152 13b2bc37 2022-10-23 stsp log_warnx("%s: name too long", unix_socket_path);
157 13b2bc37 2022-10-23 stsp if (unlink(unix_socket_path) == -1) {
158 13b2bc37 2022-10-23 stsp if (errno != ENOENT) {
159 13b2bc37 2022-10-23 stsp log_warn("unlink %s", unix_socket_path);
165 13b2bc37 2022-10-23 stsp old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
166 6f854dde 2023-01-04 stsp mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
168 13b2bc37 2022-10-23 stsp if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
169 13b2bc37 2022-10-23 stsp log_warn("bind: %s", unix_socket_path);
171 13b2bc37 2022-10-23 stsp umask(old_umask);
175 13b2bc37 2022-10-23 stsp umask(old_umask);
177 13b2bc37 2022-10-23 stsp if (chmod(unix_socket_path, mode) == -1) {
178 13b2bc37 2022-10-23 stsp log_warn("chmod %o %s", mode, unix_socket_path);
180 13b2bc37 2022-10-23 stsp unlink(unix_socket_path);
184 13b2bc37 2022-10-23 stsp if (chown(unix_socket_path, uid, gid) == -1) {
185 13b2bc37 2022-10-23 stsp log_warn("chown %s uid=%d gid=%d", unix_socket_path, uid, gid);
187 13b2bc37 2022-10-23 stsp unlink(unix_socket_path);
191 13b2bc37 2022-10-23 stsp if (listen(fd, GOTD_UNIX_SOCKET_BACKLOG) == -1) {
192 13b2bc37 2022-10-23 stsp log_warn("listen");
194 13b2bc37 2022-10-23 stsp unlink(unix_socket_path);
201 13b2bc37 2022-10-23 stsp static uint64_t
202 13b2bc37 2022-10-23 stsp client_hash(uint32_t client_id)
204 13b2bc37 2022-10-23 stsp return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
207 13b2bc37 2022-10-23 stsp static void
208 13b2bc37 2022-10-23 stsp add_client(struct gotd_client *client)
210 13b2bc37 2022-10-23 stsp uint64_t slot = client_hash(client->id) % nitems(gotd_clients);
211 13b2bc37 2022-10-23 stsp STAILQ_INSERT_HEAD(&gotd_clients[slot], client, entry);
212 13b2bc37 2022-10-23 stsp client_cnt++;
215 13b2bc37 2022-10-23 stsp static struct gotd_client *
216 13b2bc37 2022-10-23 stsp find_client(uint32_t client_id)
218 13b2bc37 2022-10-23 stsp uint64_t slot;
219 13b2bc37 2022-10-23 stsp struct gotd_client *c;
221 13b2bc37 2022-10-23 stsp slot = client_hash(client_id) % nitems(gotd_clients);
222 13b2bc37 2022-10-23 stsp STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
223 13b2bc37 2022-10-23 stsp if (c->id == client_id)
227 13b2bc37 2022-10-23 stsp return NULL;
230 b50a2b46 2022-12-29 stsp static struct gotd_client *
231 b50a2b46 2022-12-29 stsp find_client_by_proc_fd(int fd)
233 b50a2b46 2022-12-29 stsp uint64_t slot;
235 b50a2b46 2022-12-29 stsp for (slot = 0; slot < nitems(gotd_clients); slot++) {
236 b50a2b46 2022-12-29 stsp struct gotd_client *c;
238 b50a2b46 2022-12-29 stsp STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
239 f7a854cf 2023-01-10 stsp if (c->repo && c->repo->iev.ibuf.fd == fd)
241 5e25db14 2022-12-29 stsp if (c->auth && c->auth->iev.ibuf.fd == fd)
243 ae7c1b78 2023-01-10 stsp if (c->session && c->session->iev.ibuf.fd == fd)
248 13b2bc37 2022-10-23 stsp return NULL;
252 13b2bc37 2022-10-23 stsp client_is_reading(struct gotd_client *client)
254 f7a854cf 2023-01-10 stsp return (client->required_auth &
255 f7a854cf 2023-01-10 stsp (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) == GOTD_AUTH_READ;
259 13b2bc37 2022-10-23 stsp client_is_writing(struct gotd_client *client)
261 f7a854cf 2023-01-10 stsp return (client->required_auth &
262 f7a854cf 2023-01-10 stsp (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) ==
263 f7a854cf 2023-01-10 stsp (GOTD_AUTH_READ | GOTD_AUTH_WRITE);
266 13b2bc37 2022-10-23 stsp static const struct got_error *
267 13b2bc37 2022-10-23 stsp ensure_client_is_not_writing(struct gotd_client *client)
269 13b2bc37 2022-10-23 stsp if (client_is_writing(client)) {
270 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_BAD_PACKET,
271 13b2bc37 2022-10-23 stsp "uid %d made a read-request but is writing to "
272 13b2bc37 2022-10-23 stsp "a repository", client->euid);
275 13b2bc37 2022-10-23 stsp return NULL;
278 13b2bc37 2022-10-23 stsp static const struct got_error *
279 13b2bc37 2022-10-23 stsp ensure_client_is_not_reading(struct gotd_client *client)
281 13b2bc37 2022-10-23 stsp if (client_is_reading(client)) {
282 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_BAD_PACKET,
283 13b2bc37 2022-10-23 stsp "uid %d made a write-request but is reading from "
284 13b2bc37 2022-10-23 stsp "a repository", client->euid);
287 13b2bc37 2022-10-23 stsp return NULL;
290 b50a2b46 2022-12-29 stsp static void
291 839338f6 2023-06-22 op proc_done(struct gotd_child_proc *proc)
293 839338f6 2023-06-22 op struct gotd_client *client;
295 839338f6 2023-06-22 op TAILQ_REMOVE(&procs, proc, entry);
297 839338f6 2023-06-22 op client = find_client_by_proc_fd(proc->iev.ibuf.fd);
298 839338f6 2023-06-22 op if (client != NULL) {
299 839338f6 2023-06-22 op if (proc == client->repo)
300 839338f6 2023-06-22 op client->repo = NULL;
301 839338f6 2023-06-22 op if (proc == client->auth)
302 839338f6 2023-06-22 op client->auth = NULL;
303 839338f6 2023-06-22 op if (proc == client->session)
304 839338f6 2023-06-22 op client->session = NULL;
305 839338f6 2023-06-22 op disconnect(client);
308 ba97b2d7 2024-03-20 stsp if (proc == gotd.notify_proc)
309 ba97b2d7 2024-03-20 stsp gotd.notify_proc = NULL;
311 839338f6 2023-06-22 op evtimer_del(&proc->tmo);
313 839338f6 2023-06-22 op if (proc->iev.ibuf.fd != -1) {
314 839338f6 2023-06-22 op event_del(&proc->iev.ev);
315 839338f6 2023-06-22 op msgbuf_clear(&proc->iev.ibuf.w);
316 839338f6 2023-06-22 op close(proc->iev.ibuf.fd);
319 ae7c1b78 2023-01-10 stsp free(proc);
323 ba91039c 2023-06-22 op kill_repo_proc(struct gotd_client *client)
325 ba91039c 2023-06-22 op if (client->repo == NULL)
328 839338f6 2023-06-22 op kill_proc(client->repo, 0);
329 ba91039c 2023-06-22 op client->repo = NULL;
332 13b2bc37 2022-10-23 stsp static void
333 5e25db14 2022-12-29 stsp kill_auth_proc(struct gotd_client *client)
335 5e25db14 2022-12-29 stsp if (client->auth == NULL)
338 839338f6 2023-06-22 op kill_proc(client->auth, 0);
339 5e25db14 2022-12-29 stsp client->auth = NULL;
342 5e25db14 2022-12-29 stsp static void
343 ae7c1b78 2023-01-10 stsp kill_session_proc(struct gotd_client *client)
345 ae7c1b78 2023-01-10 stsp if (client->session == NULL)
348 839338f6 2023-06-22 op kill_proc(client->session, 0);
349 ae7c1b78 2023-01-10 stsp client->session = NULL;
352 ae7c1b78 2023-01-10 stsp static void
353 13b2bc37 2022-10-23 stsp disconnect(struct gotd_client *client)
355 13b2bc37 2022-10-23 stsp struct gotd_imsg_disconnect idisconnect;
356 c929736a 2023-06-22 op struct gotd_child_proc *listen_proc = gotd.listen_proc;
357 13b2bc37 2022-10-23 stsp uint64_t slot;
359 13b2bc37 2022-10-23 stsp log_debug("uid %d: disconnecting", client->euid);
361 5e25db14 2022-12-29 stsp kill_auth_proc(client);
362 ae7c1b78 2023-01-10 stsp kill_session_proc(client);
363 ba91039c 2023-06-22 op kill_repo_proc(client);
365 90270f79 2023-02-09 stsp idisconnect.client_id = client->id;
366 d93ecf7d 2022-12-14 stsp if (gotd_imsg_compose_event(&listen_proc->iev,
367 d93ecf7d 2022-12-14 stsp GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
368 d93ecf7d 2022-12-14 stsp &idisconnect, sizeof(idisconnect)) == -1)
369 d93ecf7d 2022-12-14 stsp log_warn("imsg compose DISCONNECT");
371 13b2bc37 2022-10-23 stsp slot = client_hash(client->id) % nitems(gotd_clients);
372 13b2bc37 2022-10-23 stsp STAILQ_REMOVE(&gotd_clients[slot], client, gotd_client, entry);
373 13b2bc37 2022-10-23 stsp imsg_clear(&client->iev.ibuf);
374 13b2bc37 2022-10-23 stsp event_del(&client->iev.ev);
375 13b2bc37 2022-10-23 stsp evtimer_del(&client->tmo);
376 ae7c1b78 2023-01-10 stsp if (client->fd != -1)
377 ae7c1b78 2023-01-10 stsp close(client->fd);
378 ae7c1b78 2023-01-10 stsp else if (client->iev.ibuf.fd != -1)
379 ae7c1b78 2023-01-10 stsp close(client->iev.ibuf.fd);
380 56624d2b 2023-12-27 stsp free(client->username);
381 13b2bc37 2022-10-23 stsp free(client);
382 13b2bc37 2022-10-23 stsp client_cnt--;
385 13b2bc37 2022-10-23 stsp static void
386 13b2bc37 2022-10-23 stsp disconnect_on_error(struct gotd_client *client, const struct got_error *err)
388 13b2bc37 2022-10-23 stsp struct imsgbuf ibuf;
390 1050403b 2023-08-11 stsp if (err->code != GOT_ERR_EOF) {
391 1050403b 2023-08-11 stsp log_warnx("uid %d: %s", client->euid, err->msg);
392 1050403b 2023-08-11 stsp if (client->fd != -1) {
393 1050403b 2023-08-11 stsp imsg_init(&ibuf, client->fd);
394 1050403b 2023-08-11 stsp gotd_imsg_send_error(&ibuf, 0, PROC_GOTD, err);
395 1050403b 2023-08-11 stsp imsg_clear(&ibuf);
398 13b2bc37 2022-10-23 stsp disconnect(client);
401 f1752522 2022-10-29 stsp static const struct got_error *
402 f1752522 2022-10-29 stsp send_repo_info(struct gotd_imsgev *iev, struct gotd_repo *repo)
404 f1752522 2022-10-29 stsp const struct got_error *err = NULL;
405 f1752522 2022-10-29 stsp struct gotd_imsg_info_repo irepo;
407 f1752522 2022-10-29 stsp memset(&irepo, 0, sizeof(irepo));
409 f1752522 2022-10-29 stsp if (strlcpy(irepo.repo_name, repo->name, sizeof(irepo.repo_name))
410 f1752522 2022-10-29 stsp >= sizeof(irepo.repo_name))
411 f1752522 2022-10-29 stsp return got_error_msg(GOT_ERR_NO_SPACE, "repo name too long");
412 f1752522 2022-10-29 stsp if (strlcpy(irepo.repo_path, repo->path, sizeof(irepo.repo_path))
413 f1752522 2022-10-29 stsp >= sizeof(irepo.repo_path))
414 f1752522 2022-10-29 stsp return got_error_msg(GOT_ERR_NO_SPACE, "repo path too long");
416 f1752522 2022-10-29 stsp if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_REPO, PROC_GOTD, -1,
417 f1752522 2022-10-29 stsp &irepo, sizeof(irepo)) == -1) {
418 f1752522 2022-10-29 stsp err = got_error_from_errno("imsg compose INFO_REPO");
420 f1752522 2022-10-29 stsp return err;
423 f1752522 2022-10-29 stsp return NULL;
426 f1752522 2022-10-29 stsp static const struct got_error *
427 f1752522 2022-10-29 stsp send_client_info(struct gotd_imsgev *iev, struct gotd_client *client)
429 f1752522 2022-10-29 stsp const struct got_error *err = NULL;
430 f1752522 2022-10-29 stsp struct gotd_imsg_info_client iclient;
431 f1752522 2022-10-29 stsp struct gotd_child_proc *proc;
433 f1752522 2022-10-29 stsp memset(&iclient, 0, sizeof(iclient));
434 f1752522 2022-10-29 stsp iclient.euid = client->euid;
435 f1752522 2022-10-29 stsp iclient.egid = client->egid;
437 f7a854cf 2023-01-10 stsp proc = client->repo;
438 f1752522 2022-10-29 stsp if (proc) {
439 eec68231 2022-12-14 stsp if (strlcpy(iclient.repo_name, proc->repo_path,
440 f1752522 2022-10-29 stsp sizeof(iclient.repo_name)) >= sizeof(iclient.repo_name)) {
441 f1752522 2022-10-29 stsp return got_error_msg(GOT_ERR_NO_SPACE,
442 f1752522 2022-10-29 stsp "repo name too long");
444 f1752522 2022-10-29 stsp if (client_is_writing(client))
445 f1752522 2022-10-29 stsp iclient.is_writing = 1;
447 ae7c1b78 2023-01-10 stsp iclient.repo_child_pid = proc->pid;
450 ae7c1b78 2023-01-10 stsp if (client->session)
451 ae7c1b78 2023-01-10 stsp iclient.session_child_pid = client->session->pid;
453 f1752522 2022-10-29 stsp if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_CLIENT, PROC_GOTD, -1,
454 f1752522 2022-10-29 stsp &iclient, sizeof(iclient)) == -1) {
455 f1752522 2022-10-29 stsp err = got_error_from_errno("imsg compose INFO_CLIENT");
457 f1752522 2022-10-29 stsp return err;
460 f1752522 2022-10-29 stsp return NULL;
463 f1752522 2022-10-29 stsp static const struct got_error *
464 f1752522 2022-10-29 stsp send_info(struct gotd_client *client)
466 f1752522 2022-10-29 stsp const struct got_error *err = NULL;
467 f1752522 2022-10-29 stsp struct gotd_imsg_info info;
468 f1752522 2022-10-29 stsp uint64_t slot;
469 f1752522 2022-10-29 stsp struct gotd_repo *repo;
471 78433331 2023-01-04 stsp if (client->euid != 0)
472 78433331 2023-01-04 stsp return got_error_set_errno(EPERM, "info");
474 f1752522 2022-10-29 stsp info.pid = gotd.pid;
475 f1752522 2022-10-29 stsp info.verbosity = gotd.verbosity;
476 f1752522 2022-10-29 stsp info.nrepos = gotd.nrepos;
477 f1752522 2022-10-29 stsp info.nclients = client_cnt - 1;
479 f1752522 2022-10-29 stsp if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_INFO, PROC_GOTD, -1,
480 f1752522 2022-10-29 stsp &info, sizeof(info)) == -1) {
481 f1752522 2022-10-29 stsp err = got_error_from_errno("imsg compose INFO");
483 f1752522 2022-10-29 stsp return err;
486 f1752522 2022-10-29 stsp TAILQ_FOREACH(repo, &gotd.repos, entry) {
487 f1752522 2022-10-29 stsp err = send_repo_info(&client->iev, repo);
489 f1752522 2022-10-29 stsp return err;
492 f1752522 2022-10-29 stsp for (slot = 0; slot < nitems(gotd_clients); slot++) {
493 f1752522 2022-10-29 stsp struct gotd_client *c;
494 f1752522 2022-10-29 stsp STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
495 f1752522 2022-10-29 stsp if (c->id == client->id)
497 f1752522 2022-10-29 stsp err = send_client_info(&client->iev, c);
499 f1752522 2022-10-29 stsp return err;
503 f1752522 2022-10-29 stsp return NULL;
506 f1752522 2022-10-29 stsp static const struct got_error *
507 f1752522 2022-10-29 stsp stop_gotd(struct gotd_client *client)
510 f1752522 2022-10-29 stsp if (client->euid != 0)
511 f1752522 2022-10-29 stsp return got_error_set_errno(EPERM, "stop");
513 f1752522 2022-10-29 stsp gotd_shutdown();
514 f1752522 2022-10-29 stsp /* NOTREACHED */
515 0ccf3acb 2022-11-16 stsp return NULL;
518 13b2bc37 2022-10-23 stsp static const struct got_error *
519 ae7c1b78 2023-01-10 stsp start_client_authentication(struct gotd_client *client, struct imsg *imsg)
521 13b2bc37 2022-10-23 stsp const struct got_error *err;
522 13b2bc37 2022-10-23 stsp struct gotd_imsg_list_refs ireq;
523 0ccf3acb 2022-11-16 stsp struct gotd_repo *repo = NULL;
524 13b2bc37 2022-10-23 stsp size_t datalen;
526 13b2bc37 2022-10-23 stsp log_debug("list-refs request from uid %d", client->euid);
528 eac23c30 2023-01-10 stsp if (client->state != GOTD_CLIENT_STATE_NEW)
529 ae7c1b78 2023-01-10 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
530 ae7c1b78 2023-01-10 stsp "unexpected list-refs request received");
532 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
533 13b2bc37 2022-10-23 stsp if (datalen != sizeof(ireq))
534 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
536 13b2bc37 2022-10-23 stsp memcpy(&ireq, imsg->data, datalen);
538 13b2bc37 2022-10-23 stsp if (ireq.client_is_reading) {
539 13b2bc37 2022-10-23 stsp err = ensure_client_is_not_writing(client);
541 13b2bc37 2022-10-23 stsp return err;
542 ba97b2d7 2024-03-20 stsp repo = gotd_find_repo_by_name(ireq.repo_name, &gotd.repos);
543 0ccf3acb 2022-11-16 stsp if (repo == NULL)
544 0ccf3acb 2022-11-16 stsp return got_error(GOT_ERR_NOT_GIT_REPO);
545 5e25db14 2022-12-29 stsp err = start_auth_child(client, GOTD_AUTH_READ, repo,
546 b50a2b46 2022-12-29 stsp gotd.argv0, gotd.confpath, gotd.daemonize,
547 b50a2b46 2022-12-29 stsp gotd.verbosity);
549 b50a2b46 2022-12-29 stsp return err;
551 13b2bc37 2022-10-23 stsp err = ensure_client_is_not_reading(client);
553 0ccf3acb 2022-11-16 stsp return err;
554 ba97b2d7 2024-03-20 stsp repo = gotd_find_repo_by_name(ireq.repo_name, &gotd.repos);
555 0ccf3acb 2022-11-16 stsp if (repo == NULL)
556 0ccf3acb 2022-11-16 stsp return got_error(GOT_ERR_NOT_GIT_REPO);
557 5e25db14 2022-12-29 stsp err = start_auth_child(client,
558 5e25db14 2022-12-29 stsp GOTD_AUTH_READ | GOTD_AUTH_WRITE,
559 5e25db14 2022-12-29 stsp repo, gotd.argv0, gotd.confpath, gotd.daemonize,
560 b50a2b46 2022-12-29 stsp gotd.verbosity);
562 b50a2b46 2022-12-29 stsp return err;
565 ae7c1b78 2023-01-10 stsp evtimer_add(&client->tmo, &auth_timeout);
567 5fb50fce 2023-07-14 stsp /* Flow continues upon authentication success/failure or timeout. */
568 13b2bc37 2022-10-23 stsp return NULL;
571 13b2bc37 2022-10-23 stsp static void
572 13b2bc37 2022-10-23 stsp gotd_request(int fd, short events, void *arg)
574 13b2bc37 2022-10-23 stsp struct gotd_imsgev *iev = arg;
575 13b2bc37 2022-10-23 stsp struct imsgbuf *ibuf = &iev->ibuf;
576 13b2bc37 2022-10-23 stsp struct gotd_client *client = iev->handler_arg;
577 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
578 13b2bc37 2022-10-23 stsp struct imsg imsg;
581 13b2bc37 2022-10-23 stsp if (events & EV_WRITE) {
582 13b2bc37 2022-10-23 stsp while (ibuf->w.queued) {
583 13b2bc37 2022-10-23 stsp n = msgbuf_write(&ibuf->w);
584 13b2bc37 2022-10-23 stsp if (n == -1 && errno == EPIPE) {
586 13b2bc37 2022-10-23 stsp * The client has closed its socket.
587 13b2bc37 2022-10-23 stsp * This can happen when Git clients are
588 13b2bc37 2022-10-23 stsp * done sending pack file data.
590 13b2bc37 2022-10-23 stsp msgbuf_clear(&ibuf->w);
592 13b2bc37 2022-10-23 stsp } else if (n == -1 && errno != EAGAIN) {
593 c025ee6f 2024-04-30 stsp err = got_error_from_errno("msgbuf_write");
594 13b2bc37 2022-10-23 stsp disconnect_on_error(client, err);
597 13b2bc37 2022-10-23 stsp if (n == 0) {
598 13b2bc37 2022-10-23 stsp /* Connection closed. */
599 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_EOF);
600 13b2bc37 2022-10-23 stsp disconnect_on_error(client, err);
605 f1752522 2022-10-29 stsp /* Disconnect gotctl(8) now that messages have been sent. */
606 f1752522 2022-10-29 stsp if (!client_is_reading(client) && !client_is_writing(client)) {
607 f1752522 2022-10-29 stsp disconnect(client);
612 13b2bc37 2022-10-23 stsp if ((events & EV_READ) == 0)
615 13b2bc37 2022-10-23 stsp memset(&imsg, 0, sizeof(imsg));
617 13b2bc37 2022-10-23 stsp while (err == NULL) {
618 13b2bc37 2022-10-23 stsp err = gotd_imsg_recv(&imsg, ibuf, 0);
620 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_PRIVSEP_READ)
621 13b2bc37 2022-10-23 stsp err = NULL;
625 13b2bc37 2022-10-23 stsp evtimer_del(&client->tmo);
627 13b2bc37 2022-10-23 stsp switch (imsg.hdr.type) {
628 f1752522 2022-10-29 stsp case GOTD_IMSG_INFO:
629 f1752522 2022-10-29 stsp err = send_info(client);
631 f1752522 2022-10-29 stsp case GOTD_IMSG_STOP:
632 f1752522 2022-10-29 stsp err = stop_gotd(client);
634 13b2bc37 2022-10-23 stsp case GOTD_IMSG_LIST_REFS:
635 ae7c1b78 2023-01-10 stsp err = start_client_authentication(client, &imsg);
638 ae7c1b78 2023-01-10 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
639 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
643 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
647 b5225f29 2023-01-22 op disconnect_on_error(client, err);
649 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(&client->iev);
653 13b2bc37 2022-10-23 stsp static void
654 ae7c1b78 2023-01-10 stsp gotd_auth_timeout(int fd, short events, void *arg)
656 13b2bc37 2022-10-23 stsp struct gotd_client *client = arg;
658 ae7c1b78 2023-01-10 stsp log_debug("disconnecting uid %d due to authentication timeout",
659 ae7c1b78 2023-01-10 stsp client->euid);
660 13b2bc37 2022-10-23 stsp disconnect(client);
663 d93ecf7d 2022-12-14 stsp static const struct got_error *
664 d93ecf7d 2022-12-14 stsp recv_connect(uint32_t *client_id, struct imsg *imsg)
666 d93ecf7d 2022-12-14 stsp const struct got_error *err = NULL;
667 d93ecf7d 2022-12-14 stsp struct gotd_imsg_connect iconnect;
668 d93ecf7d 2022-12-14 stsp size_t datalen;
669 13b2bc37 2022-10-23 stsp int s = -1;
670 13b2bc37 2022-10-23 stsp struct gotd_client *client = NULL;
672 d93ecf7d 2022-12-14 stsp *client_id = 0;
674 d93ecf7d 2022-12-14 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
675 d93ecf7d 2022-12-14 stsp if (datalen != sizeof(iconnect))
676 d93ecf7d 2022-12-14 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
677 d93ecf7d 2022-12-14 stsp memcpy(&iconnect, imsg->data, sizeof(iconnect));
679 2c52c623 2024-01-30 op s = imsg_get_fd(imsg);
680 13b2bc37 2022-10-23 stsp if (s == -1) {
681 d93ecf7d 2022-12-14 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
685 d93ecf7d 2022-12-14 stsp if (find_client(iconnect.client_id)) {
686 d93ecf7d 2022-12-14 stsp err = got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
690 13b2bc37 2022-10-23 stsp client = calloc(1, sizeof(*client));
691 13b2bc37 2022-10-23 stsp if (client == NULL) {
692 d93ecf7d 2022-12-14 stsp err = got_error_from_errno("calloc");
696 d93ecf7d 2022-12-14 stsp *client_id = iconnect.client_id;
698 eac23c30 2023-01-10 stsp client->state = GOTD_CLIENT_STATE_NEW;
699 d93ecf7d 2022-12-14 stsp client->id = iconnect.client_id;
700 13b2bc37 2022-10-23 stsp client->fd = s;
702 365cf0f3 2022-12-29 stsp /* The auth process will verify UID/GID for us. */
703 365cf0f3 2022-12-29 stsp client->euid = iconnect.euid;
704 365cf0f3 2022-12-29 stsp client->egid = iconnect.egid;
706 13b2bc37 2022-10-23 stsp imsg_init(&client->iev.ibuf, client->fd);
707 13b2bc37 2022-10-23 stsp client->iev.handler = gotd_request;
708 13b2bc37 2022-10-23 stsp client->iev.events = EV_READ;
709 13b2bc37 2022-10-23 stsp client->iev.handler_arg = client;
711 13b2bc37 2022-10-23 stsp event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
712 13b2bc37 2022-10-23 stsp &client->iev);
713 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(&client->iev);
715 ae7c1b78 2023-01-10 stsp evtimer_set(&client->tmo, gotd_auth_timeout, client);
717 13b2bc37 2022-10-23 stsp add_client(client);
718 13b2bc37 2022-10-23 stsp log_debug("%s: new client uid %d connected on fd %d", __func__,
719 13b2bc37 2022-10-23 stsp client->euid, client->fd);
722 c929736a 2023-06-22 op struct gotd_child_proc *listen_proc = gotd.listen_proc;
723 d93ecf7d 2022-12-14 stsp struct gotd_imsg_disconnect idisconnect;
725 d93ecf7d 2022-12-14 stsp idisconnect.client_id = client->id;
726 d93ecf7d 2022-12-14 stsp if (gotd_imsg_compose_event(&listen_proc->iev,
727 d93ecf7d 2022-12-14 stsp GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
728 d93ecf7d 2022-12-14 stsp &idisconnect, sizeof(idisconnect)) == -1)
729 d93ecf7d 2022-12-14 stsp log_warn("imsg compose DISCONNECT");
731 d93ecf7d 2022-12-14 stsp if (s != -1)
735 d93ecf7d 2022-12-14 stsp return err;
738 13b2bc37 2022-10-23 stsp static const char *gotd_proc_names[PROC_MAX] = {
742 ce986f22 2023-06-19 stsp "session_read",
743 ce986f22 2023-06-19 stsp "session_write",
744 13b2bc37 2022-10-23 stsp "repo_read",
745 4b3827cd 2023-07-08 stsp "repo_write",
746 ba97b2d7 2024-03-20 stsp "gitwrapper",
750 13b2bc37 2022-10-23 stsp static void
751 13b2bc37 2022-10-23 stsp kill_proc(struct gotd_child_proc *proc, int fatal)
753 839338f6 2023-06-22 op struct timeval tv = { 5, 0 };
755 839338f6 2023-06-22 op log_debug("kill -%d %d", fatal ? SIGKILL : SIGTERM, proc->pid);
757 839338f6 2023-06-22 op if (proc->iev.ibuf.fd != -1) {
758 839338f6 2023-06-22 op event_del(&proc->iev.ev);
759 839338f6 2023-06-22 op msgbuf_clear(&proc->iev.ibuf.w);
760 839338f6 2023-06-22 op close(proc->iev.ibuf.fd);
761 839338f6 2023-06-22 op proc->iev.ibuf.fd = -1;
764 839338f6 2023-06-22 op if (!evtimer_pending(&proc->tmo, NULL) && !fatal)
765 839338f6 2023-06-22 op evtimer_add(&proc->tmo, &tv);
767 13b2bc37 2022-10-23 stsp if (fatal) {
768 13b2bc37 2022-10-23 stsp log_warnx("sending SIGKILL to PID %d", proc->pid);
769 13b2bc37 2022-10-23 stsp kill(proc->pid, SIGKILL);
771 13b2bc37 2022-10-23 stsp kill(proc->pid, SIGTERM);
774 13b2bc37 2022-10-23 stsp static void
775 839338f6 2023-06-22 op kill_proc_timeout(int fd, short ev, void *d)
777 839338f6 2023-06-22 op struct gotd_child_proc *proc = d;
779 839338f6 2023-06-22 op log_warnx("timeout waiting for PID %d to terminate;"
780 839338f6 2023-06-22 op " retrying with force", proc->pid);
781 839338f6 2023-06-22 op kill_proc(proc, 1);
785 13b2bc37 2022-10-23 stsp gotd_shutdown(void)
787 b50a2b46 2022-12-29 stsp uint64_t slot;
789 ae7c1b78 2023-01-10 stsp log_debug("shutting down");
790 b50a2b46 2022-12-29 stsp for (slot = 0; slot < nitems(gotd_clients); slot++) {
791 b50a2b46 2022-12-29 stsp struct gotd_client *c, *tmp;
793 b50a2b46 2022-12-29 stsp STAILQ_FOREACH_SAFE(c, &gotd_clients[slot], entry, tmp)
794 b50a2b46 2022-12-29 stsp disconnect(c);
797 839338f6 2023-06-22 op kill_proc(gotd.listen_proc, 0);
799 13b2bc37 2022-10-23 stsp log_info("terminating");
803 839338f6 2023-06-22 op static struct gotd_child_proc *
804 839338f6 2023-06-22 op find_proc_by_pid(pid_t pid)
806 839338f6 2023-06-22 op struct gotd_child_proc *proc = NULL;
808 839338f6 2023-06-22 op TAILQ_FOREACH(proc, &procs, entry)
809 839338f6 2023-06-22 op if (proc->pid == pid)
816 13b2bc37 2022-10-23 stsp gotd_sighdlr(int sig, short event, void *arg)
818 839338f6 2023-06-22 op struct gotd_child_proc *proc;
823 13b2bc37 2022-10-23 stsp * Normal signal handler rules don't apply because libevent
824 13b2bc37 2022-10-23 stsp * decouples for us.
827 13b2bc37 2022-10-23 stsp switch (sig) {
828 13b2bc37 2022-10-23 stsp case SIGHUP:
829 13b2bc37 2022-10-23 stsp log_info("%s: ignoring SIGHUP", __func__);
831 13b2bc37 2022-10-23 stsp case SIGUSR1:
832 13b2bc37 2022-10-23 stsp log_info("%s: ignoring SIGUSR1", __func__);
834 13b2bc37 2022-10-23 stsp case SIGTERM:
835 13b2bc37 2022-10-23 stsp case SIGINT:
836 13b2bc37 2022-10-23 stsp gotd_shutdown();
838 839338f6 2023-06-22 op case SIGCHLD:
840 839338f6 2023-06-22 op pid = waitpid(WAIT_ANY, &status, WNOHANG);
841 839338f6 2023-06-22 op if (pid == -1) {
842 839338f6 2023-06-22 op if (errno == EINTR)
844 839338f6 2023-06-22 op if (errno == ECHILD)
846 839338f6 2023-06-22 op fatal("waitpid");
848 839338f6 2023-06-22 op if (pid == 0)
851 839338f6 2023-06-22 op log_debug("reaped pid %d", pid);
852 839338f6 2023-06-22 op proc = find_proc_by_pid(pid);
853 839338f6 2023-06-22 op if (proc == NULL) {
854 839338f6 2023-06-22 op log_info("caught exit of unknown child %d",
859 839338f6 2023-06-22 op if (WIFSIGNALED(status)) {
860 839338f6 2023-06-22 op log_warnx("child PID %d terminated with"
861 839338f6 2023-06-22 op " signal %d", pid, WTERMSIG(status));
864 839338f6 2023-06-22 op proc_done(proc);
868 13b2bc37 2022-10-23 stsp fatalx("unexpected signal");
872 13b2bc37 2022-10-23 stsp static const struct got_error *
873 13b2bc37 2022-10-23 stsp ensure_proc_is_reading(struct gotd_client *client,
874 13b2bc37 2022-10-23 stsp struct gotd_child_proc *proc)
876 13b2bc37 2022-10-23 stsp if (!client_is_reading(client)) {
877 13b2bc37 2022-10-23 stsp kill_proc(proc, 1);
878 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_BAD_PACKET,
879 13b2bc37 2022-10-23 stsp "PID %d handled a read-request for uid %d but this "
880 13b2bc37 2022-10-23 stsp "user is not reading from a repository", proc->pid,
881 13b2bc37 2022-10-23 stsp client->euid);
884 13b2bc37 2022-10-23 stsp return NULL;
887 13b2bc37 2022-10-23 stsp static const struct got_error *
888 13b2bc37 2022-10-23 stsp ensure_proc_is_writing(struct gotd_client *client,
889 13b2bc37 2022-10-23 stsp struct gotd_child_proc *proc)
891 13b2bc37 2022-10-23 stsp if (!client_is_writing(client)) {
892 13b2bc37 2022-10-23 stsp kill_proc(proc, 1);
893 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_BAD_PACKET,
894 13b2bc37 2022-10-23 stsp "PID %d handled a write-request for uid %d but this "
895 13b2bc37 2022-10-23 stsp "user is not writing to a repository", proc->pid,
896 13b2bc37 2022-10-23 stsp client->euid);
899 13b2bc37 2022-10-23 stsp return NULL;
903 13b2bc37 2022-10-23 stsp verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
904 13b2bc37 2022-10-23 stsp struct imsg *imsg)
906 13b2bc37 2022-10-23 stsp const struct got_error *err;
907 13b2bc37 2022-10-23 stsp int ret = 0;
909 d93ecf7d 2022-12-14 stsp if (proc->type == PROC_REPO_READ || proc->type == PROC_REPO_WRITE) {
910 f7a854cf 2023-01-10 stsp if (client->repo == NULL)
911 d93ecf7d 2022-12-14 stsp fatalx("no process found for uid %d", client->euid);
912 f7a854cf 2023-01-10 stsp if (proc->pid != client->repo->pid) {
913 d93ecf7d 2022-12-14 stsp kill_proc(proc, 1);
914 d93ecf7d 2022-12-14 stsp log_warnx("received message from PID %d for uid %d, "
915 d93ecf7d 2022-12-14 stsp "while PID %d is the process serving this user",
916 f7a854cf 2023-01-10 stsp proc->pid, client->euid, client->repo->pid);
920 b0614828 2023-06-19 stsp if (proc->type == PROC_SESSION_READ ||
921 b0614828 2023-06-19 stsp proc->type == PROC_SESSION_WRITE) {
922 ae7c1b78 2023-01-10 stsp if (client->session == NULL) {
923 ae7c1b78 2023-01-10 stsp log_warnx("no session found for uid %d", client->euid);
926 ae7c1b78 2023-01-10 stsp if (proc->pid != client->session->pid) {
927 ae7c1b78 2023-01-10 stsp kill_proc(proc, 1);
928 ae7c1b78 2023-01-10 stsp log_warnx("received message from PID %d for uid %d, "
929 ae7c1b78 2023-01-10 stsp "while PID %d is the process serving this user",
930 ae7c1b78 2023-01-10 stsp proc->pid, client->euid, client->session->pid);
935 13b2bc37 2022-10-23 stsp switch (imsg->hdr.type) {
936 13b2bc37 2022-10-23 stsp case GOTD_IMSG_ERROR:
939 d93ecf7d 2022-12-14 stsp case GOTD_IMSG_CONNECT:
940 d93ecf7d 2022-12-14 stsp if (proc->type != PROC_LISTEN) {
941 d93ecf7d 2022-12-14 stsp err = got_error_fmt(GOT_ERR_BAD_PACKET,
942 d93ecf7d 2022-12-14 stsp "new connection for uid %d from PID %d "
943 d93ecf7d 2022-12-14 stsp "which is not the listen process",
944 76a9a38e 2023-12-27 stsp client->euid, proc->pid);
948 5e25db14 2022-12-29 stsp case GOTD_IMSG_ACCESS_GRANTED:
949 5e25db14 2022-12-29 stsp if (proc->type != PROC_AUTH) {
950 5e25db14 2022-12-29 stsp err = got_error_fmt(GOT_ERR_BAD_PACKET,
951 5e25db14 2022-12-29 stsp "authentication of uid %d from PID %d "
952 5e25db14 2022-12-29 stsp "which is not the auth process",
953 76a9a38e 2023-12-27 stsp client->euid, proc->pid);
957 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_CLIENT_SESSION_READY:
958 b0614828 2023-06-19 stsp if (proc->type != PROC_SESSION_READ &&
959 b0614828 2023-06-19 stsp proc->type != PROC_SESSION_WRITE) {
960 ae7c1b78 2023-01-10 stsp err = got_error_fmt(GOT_ERR_BAD_PACKET,
961 ae7c1b78 2023-01-10 stsp "unexpected \"ready\" signal from PID %d",
962 ae7c1b78 2023-01-10 stsp proc->pid);
966 b50a2b46 2022-12-29 stsp case GOTD_IMSG_REPO_CHILD_READY:
967 b50a2b46 2022-12-29 stsp if (proc->type != PROC_REPO_READ &&
968 b50a2b46 2022-12-29 stsp proc->type != PROC_REPO_WRITE) {
969 b50a2b46 2022-12-29 stsp err = got_error_fmt(GOT_ERR_BAD_PACKET,
970 b50a2b46 2022-12-29 stsp "unexpected \"ready\" signal from PID %d",
971 b50a2b46 2022-12-29 stsp proc->pid);
975 13b2bc37 2022-10-23 stsp case GOTD_IMSG_PACKFILE_DONE:
976 13b2bc37 2022-10-23 stsp err = ensure_proc_is_reading(client, proc);
978 13b2bc37 2022-10-23 stsp log_warnx("uid %d: %s", client->euid, err->msg);
982 13b2bc37 2022-10-23 stsp case GOTD_IMSG_PACKFILE_INSTALL:
983 13b2bc37 2022-10-23 stsp case GOTD_IMSG_REF_UPDATES_START:
984 13b2bc37 2022-10-23 stsp case GOTD_IMSG_REF_UPDATE:
985 13b2bc37 2022-10-23 stsp err = ensure_proc_is_writing(client, proc);
987 13b2bc37 2022-10-23 stsp log_warnx("uid %d: %s", client->euid, err->msg);
992 13b2bc37 2022-10-23 stsp log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
996 13b2bc37 2022-10-23 stsp return ret;
999 13b2bc37 2022-10-23 stsp static const struct got_error *
1000 ae7c1b78 2023-01-10 stsp connect_repo_child(struct gotd_client *client,
1001 ae7c1b78 2023-01-10 stsp struct gotd_child_proc *repo_proc)
1003 b50a2b46 2022-12-29 stsp static const struct got_error *err;
1004 ae7c1b78 2023-01-10 stsp struct gotd_imsgev *session_iev = &client->session->iev;
1005 ae7c1b78 2023-01-10 stsp struct gotd_imsg_connect_repo_child ireq;
1006 ae7c1b78 2023-01-10 stsp int pipe[2];
1008 eac23c30 2023-01-10 stsp if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED)
1009 ae7c1b78 2023-01-10 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
1010 ae7c1b78 2023-01-10 stsp "unexpected repo child ready signal received");
1012 ae7c1b78 2023-01-10 stsp if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1013 ae7c1b78 2023-01-10 stsp PF_UNSPEC, pipe) == -1)
1014 ae7c1b78 2023-01-10 stsp fatal("socketpair");
1016 ae7c1b78 2023-01-10 stsp memset(&ireq, 0, sizeof(ireq));
1017 ae7c1b78 2023-01-10 stsp ireq.proc_id = repo_proc->type;
1019 ae7c1b78 2023-01-10 stsp /* Pass repo child pipe to session child process. */
1020 ae7c1b78 2023-01-10 stsp if (gotd_imsg_compose_event(session_iev, GOTD_IMSG_CONNECT_REPO_CHILD,
1021 ae7c1b78 2023-01-10 stsp PROC_GOTD, pipe[0], &ireq, sizeof(ireq)) == -1) {
1022 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
1023 ae7c1b78 2023-01-10 stsp close(pipe[0]);
1024 ae7c1b78 2023-01-10 stsp close(pipe[1]);
1025 ae7c1b78 2023-01-10 stsp return err;
1028 ae7c1b78 2023-01-10 stsp /* Pass session child pipe to repo child process. */
1029 ae7c1b78 2023-01-10 stsp if (gotd_imsg_compose_event(&repo_proc->iev,
1030 ae7c1b78 2023-01-10 stsp GOTD_IMSG_CONNECT_REPO_CHILD, PROC_GOTD, pipe[1], NULL, 0) == -1) {
1031 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
1032 ae7c1b78 2023-01-10 stsp close(pipe[1]);
1033 ae7c1b78 2023-01-10 stsp return err;
1036 13b2bc37 2022-10-23 stsp return NULL;
1039 13b2bc37 2022-10-23 stsp static void
1040 b50a2b46 2022-12-29 stsp gotd_dispatch_listener(int fd, short event, void *arg)
1042 13b2bc37 2022-10-23 stsp struct gotd_imsgev *iev = arg;
1043 13b2bc37 2022-10-23 stsp struct imsgbuf *ibuf = &iev->ibuf;
1044 c929736a 2023-06-22 op struct gotd_child_proc *proc = gotd.listen_proc;
1045 b50a2b46 2022-12-29 stsp ssize_t n;
1046 b50a2b46 2022-12-29 stsp int shut = 0;
1047 b50a2b46 2022-12-29 stsp struct imsg imsg;
1049 b50a2b46 2022-12-29 stsp if (proc->iev.ibuf.fd != fd)
1050 b50a2b46 2022-12-29 stsp fatalx("%s: unexpected fd %d", __func__, fd);
1052 b50a2b46 2022-12-29 stsp if (event & EV_READ) {
1053 b50a2b46 2022-12-29 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1054 b50a2b46 2022-12-29 stsp fatal("imsg_read error");
1055 b50a2b46 2022-12-29 stsp if (n == 0) {
1056 b50a2b46 2022-12-29 stsp /* Connection closed. */
1058 b50a2b46 2022-12-29 stsp goto done;
1062 b50a2b46 2022-12-29 stsp if (event & EV_WRITE) {
1063 b50a2b46 2022-12-29 stsp n = msgbuf_write(&ibuf->w);
1064 b50a2b46 2022-12-29 stsp if (n == -1 && errno != EAGAIN)
1065 b50a2b46 2022-12-29 stsp fatal("msgbuf_write");
1066 b50a2b46 2022-12-29 stsp if (n == 0) {
1067 b50a2b46 2022-12-29 stsp /* Connection closed. */
1069 b50a2b46 2022-12-29 stsp goto done;
1073 b50a2b46 2022-12-29 stsp for (;;) {
1074 b50a2b46 2022-12-29 stsp const struct got_error *err = NULL;
1075 b50a2b46 2022-12-29 stsp struct gotd_client *client = NULL;
1076 b50a2b46 2022-12-29 stsp uint32_t client_id = 0;
1077 b50a2b46 2022-12-29 stsp int do_disconnect = 0;
1079 b50a2b46 2022-12-29 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
1080 b50a2b46 2022-12-29 stsp fatal("%s: imsg_get error", __func__);
1081 b50a2b46 2022-12-29 stsp if (n == 0) /* No more messages. */
1084 b50a2b46 2022-12-29 stsp switch (imsg.hdr.type) {
1085 b50a2b46 2022-12-29 stsp case GOTD_IMSG_ERROR:
1086 b50a2b46 2022-12-29 stsp do_disconnect = 1;
1087 b50a2b46 2022-12-29 stsp err = gotd_imsg_recv_error(&client_id, &imsg);
1089 b50a2b46 2022-12-29 stsp case GOTD_IMSG_CONNECT:
1090 b50a2b46 2022-12-29 stsp err = recv_connect(&client_id, &imsg);
1093 b50a2b46 2022-12-29 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1097 b50a2b46 2022-12-29 stsp client = find_client(client_id);
1098 b50a2b46 2022-12-29 stsp if (client == NULL) {
1099 b50a2b46 2022-12-29 stsp log_warnx("%s: client not found", __func__);
1100 b50a2b46 2022-12-29 stsp imsg_free(&imsg);
1105 b50a2b46 2022-12-29 stsp log_warnx("uid %d: %s", client->euid, err->msg);
1107 b50a2b46 2022-12-29 stsp if (do_disconnect) {
1109 b50a2b46 2022-12-29 stsp disconnect_on_error(client, err);
1111 b50a2b46 2022-12-29 stsp disconnect(client);
1114 b50a2b46 2022-12-29 stsp imsg_free(&imsg);
1117 b50a2b46 2022-12-29 stsp if (!shut) {
1118 b50a2b46 2022-12-29 stsp gotd_imsg_event_add(iev);
1120 b50a2b46 2022-12-29 stsp /* This pipe is dead. Remove its event handler */
1121 b50a2b46 2022-12-29 stsp event_del(&iev->ev);
1122 b50a2b46 2022-12-29 stsp event_loopexit(NULL);
1126 b50a2b46 2022-12-29 stsp static void
1127 ba97b2d7 2024-03-20 stsp gotd_dispatch_notifier(int fd, short event, void *arg)
1129 ba97b2d7 2024-03-20 stsp struct gotd_imsgev *iev = arg;
1130 ba97b2d7 2024-03-20 stsp struct imsgbuf *ibuf = &iev->ibuf;
1131 ba97b2d7 2024-03-20 stsp struct gotd_child_proc *proc = gotd.notify_proc;
1132 ba97b2d7 2024-03-20 stsp ssize_t n;
1133 ba97b2d7 2024-03-20 stsp int shut = 0;
1134 ba97b2d7 2024-03-20 stsp struct imsg imsg;
1136 ba97b2d7 2024-03-20 stsp if (proc->iev.ibuf.fd != fd)
1137 ba97b2d7 2024-03-20 stsp fatalx("%s: unexpected fd %d", __func__, fd);
1139 ba97b2d7 2024-03-20 stsp if (event & EV_READ) {
1140 ba97b2d7 2024-03-20 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1141 ba97b2d7 2024-03-20 stsp fatal("imsg_read error");
1142 ba97b2d7 2024-03-20 stsp if (n == 0) {
1143 ba97b2d7 2024-03-20 stsp /* Connection closed. */
1145 ba97b2d7 2024-03-20 stsp goto done;
1149 ba97b2d7 2024-03-20 stsp if (event & EV_WRITE) {
1150 ba97b2d7 2024-03-20 stsp n = msgbuf_write(&ibuf->w);
1151 ba97b2d7 2024-03-20 stsp if (n == -1 && errno != EAGAIN)
1152 ba97b2d7 2024-03-20 stsp fatal("msgbuf_write");
1153 ba97b2d7 2024-03-20 stsp if (n == 0) {
1154 ba97b2d7 2024-03-20 stsp /* Connection closed. */
1156 ba97b2d7 2024-03-20 stsp goto done;
1160 ba97b2d7 2024-03-20 stsp for (;;) {
1161 ba97b2d7 2024-03-20 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
1162 ba97b2d7 2024-03-20 stsp fatal("%s: imsg_get error", __func__);
1163 ba97b2d7 2024-03-20 stsp if (n == 0) /* No more messages. */
1166 ba97b2d7 2024-03-20 stsp switch (imsg.hdr.type) {
1168 ba97b2d7 2024-03-20 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1172 ba97b2d7 2024-03-20 stsp imsg_free(&imsg);
1175 ba97b2d7 2024-03-20 stsp if (!shut) {
1176 ba97b2d7 2024-03-20 stsp gotd_imsg_event_add(iev);
1178 ba97b2d7 2024-03-20 stsp /* This pipe is dead. Remove its event handler */
1179 ba97b2d7 2024-03-20 stsp event_del(&iev->ev);
1182 ba97b2d7 2024-03-20 stsp * Do not exit all of gotd if the notification handler dies.
1183 ba97b2d7 2024-03-20 stsp * We can continue operating without notifications until an
1184 ba97b2d7 2024-03-20 stsp * operator intervenes.
1186 ba97b2d7 2024-03-20 stsp log_warnx("notify child process (pid %d) closed its imsg pipe "
1187 ba97b2d7 2024-03-20 stsp "unexpectedly", proc->pid);
1188 ba97b2d7 2024-03-20 stsp proc_done(proc);
1192 ba97b2d7 2024-03-20 stsp static void
1193 5e25db14 2022-12-29 stsp gotd_dispatch_auth_child(int fd, short event, void *arg)
1195 5e25db14 2022-12-29 stsp const struct got_error *err = NULL;
1196 5e25db14 2022-12-29 stsp struct gotd_imsgev *iev = arg;
1197 5e25db14 2022-12-29 stsp struct imsgbuf *ibuf = &iev->ibuf;
1198 5e25db14 2022-12-29 stsp struct gotd_client *client;
1199 5e25db14 2022-12-29 stsp struct gotd_repo *repo = NULL;
1200 5e25db14 2022-12-29 stsp ssize_t n;
1201 5e25db14 2022-12-29 stsp int shut = 0;
1202 5e25db14 2022-12-29 stsp struct imsg imsg;
1203 5e25db14 2022-12-29 stsp uint32_t client_id = 0;
1204 5e25db14 2022-12-29 stsp int do_disconnect = 0;
1205 56624d2b 2023-12-27 stsp size_t datalen;
1207 5e25db14 2022-12-29 stsp client = find_client_by_proc_fd(fd);
1208 ae0cca99 2023-02-09 stsp if (client == NULL) {
1209 ae0cca99 2023-02-09 stsp /* Can happen during process teardown. */
1210 ae0cca99 2023-02-09 stsp warnx("cannot find client for fd %d", fd);
1212 ae0cca99 2023-02-09 stsp goto done;
1215 5e25db14 2022-12-29 stsp if (client->auth == NULL)
1216 5e25db14 2022-12-29 stsp fatalx("cannot find auth child process for fd %d", fd);
1218 5e25db14 2022-12-29 stsp if (event & EV_READ) {
1219 5e25db14 2022-12-29 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1220 5e25db14 2022-12-29 stsp fatal("imsg_read error");
1221 5e25db14 2022-12-29 stsp if (n == 0) {
1222 5e25db14 2022-12-29 stsp /* Connection closed. */
1224 5e25db14 2022-12-29 stsp goto done;
1228 5e25db14 2022-12-29 stsp if (event & EV_WRITE) {
1229 5e25db14 2022-12-29 stsp n = msgbuf_write(&ibuf->w);
1230 5e25db14 2022-12-29 stsp if (n == -1 && errno != EAGAIN)
1231 5e25db14 2022-12-29 stsp fatal("msgbuf_write");
1232 5e25db14 2022-12-29 stsp if (n == 0) {
1233 5e25db14 2022-12-29 stsp /* Connection closed. */
1236 5e25db14 2022-12-29 stsp goto done;
1239 5e25db14 2022-12-29 stsp if (client->auth->iev.ibuf.fd != fd)
1240 5e25db14 2022-12-29 stsp fatalx("%s: unexpected fd %d", __func__, fd);
1242 5e25db14 2022-12-29 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
1243 5e25db14 2022-12-29 stsp fatal("%s: imsg_get error", __func__);
1244 5e25db14 2022-12-29 stsp if (n == 0) /* No more messages. */
1247 5e25db14 2022-12-29 stsp evtimer_del(&client->tmo);
1249 56624d2b 2023-12-27 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1251 5e25db14 2022-12-29 stsp switch (imsg.hdr.type) {
1252 5e25db14 2022-12-29 stsp case GOTD_IMSG_ERROR:
1253 5e25db14 2022-12-29 stsp do_disconnect = 1;
1254 5e25db14 2022-12-29 stsp err = gotd_imsg_recv_error(&client_id, &imsg);
1256 5e25db14 2022-12-29 stsp case GOTD_IMSG_ACCESS_GRANTED:
1257 56624d2b 2023-12-27 stsp if (client->state != GOTD_CLIENT_STATE_NEW) {
1258 56624d2b 2023-12-27 stsp do_disconnect = 1;
1259 56624d2b 2023-12-27 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1263 5e25db14 2022-12-29 stsp do_disconnect = 1;
1264 5e25db14 2022-12-29 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1268 5e25db14 2022-12-29 stsp if (!verify_imsg_src(client, client->auth, &imsg)) {
1269 5e25db14 2022-12-29 stsp do_disconnect = 1;
1270 5e25db14 2022-12-29 stsp log_debug("dropping imsg type %d from PID %d",
1271 5e25db14 2022-12-29 stsp imsg.hdr.type, client->auth->pid);
1274 5e25db14 2022-12-29 stsp if (do_disconnect) {
1276 5e25db14 2022-12-29 stsp disconnect_on_error(client, err);
1278 5e25db14 2022-12-29 stsp disconnect(client);
1279 56624d2b 2023-12-27 stsp imsg_free(&imsg);
1283 56624d2b 2023-12-27 stsp client->state = GOTD_CLIENT_STATE_ACCESS_GRANTED;
1284 56624d2b 2023-12-27 stsp if (datalen > 0)
1285 56624d2b 2023-12-27 stsp client->username = strndup(imsg.data, datalen);
1286 56624d2b 2023-12-27 stsp imsg_free(&imsg);
1287 56624d2b 2023-12-27 stsp if (client->username == NULL &&
1288 56624d2b 2023-12-27 stsp asprintf(&client->username, "uid %d", client->euid) == -1) {
1289 56624d2b 2023-12-27 stsp err = got_error_from_errno("asprintf");
1290 56624d2b 2023-12-27 stsp goto done;
1293 ba97b2d7 2024-03-20 stsp repo = gotd_find_repo_by_name(client->auth->repo_name, &gotd.repos);
1294 5e25db14 2022-12-29 stsp if (repo == NULL) {
1295 5e25db14 2022-12-29 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
1296 5e25db14 2022-12-29 stsp goto done;
1298 5e25db14 2022-12-29 stsp kill_auth_proc(client);
1300 56624d2b 2023-12-27 stsp log_info("authenticated %s for repository %s",
1301 56624d2b 2023-12-27 stsp client->username, repo->name);
1303 ae7c1b78 2023-01-10 stsp err = start_session_child(client, repo, gotd.argv0,
1304 7fdc3e58 2022-12-30 mark gotd.confpath, gotd.daemonize, gotd.verbosity);
1306 ae7c1b78 2023-01-10 stsp goto done;
1309 5e25db14 2022-12-29 stsp log_warnx("uid %d: %s", client->euid, err->msg);
1311 5e25db14 2022-12-29 stsp /* We might have killed the auth process by now. */
1312 5e25db14 2022-12-29 stsp if (client->auth != NULL) {
1313 5e25db14 2022-12-29 stsp if (!shut) {
1314 5e25db14 2022-12-29 stsp gotd_imsg_event_add(iev);
1316 5e25db14 2022-12-29 stsp /* This pipe is dead. Remove its event handler */
1317 5e25db14 2022-12-29 stsp event_del(&iev->ev);
1322 ae7c1b78 2023-01-10 stsp static const struct got_error *
1323 ae7c1b78 2023-01-10 stsp connect_session(struct gotd_client *client)
1325 ae7c1b78 2023-01-10 stsp const struct got_error *err = NULL;
1326 ae7c1b78 2023-01-10 stsp struct gotd_imsg_connect iconnect;
1328 ba97b2d7 2024-03-20 stsp struct ibuf *wbuf;
1330 ae7c1b78 2023-01-10 stsp memset(&iconnect, 0, sizeof(iconnect));
1332 ae7c1b78 2023-01-10 stsp s = dup(client->fd);
1333 ae7c1b78 2023-01-10 stsp if (s == -1)
1334 ae7c1b78 2023-01-10 stsp return got_error_from_errno("dup");
1336 ae7c1b78 2023-01-10 stsp iconnect.client_id = client->id;
1337 ae7c1b78 2023-01-10 stsp iconnect.euid = client->euid;
1338 ae7c1b78 2023-01-10 stsp iconnect.egid = client->egid;
1339 ba97b2d7 2024-03-20 stsp iconnect.username_len = strlen(client->username);
1341 ba97b2d7 2024-03-20 stsp wbuf = imsg_create(&client->session->iev.ibuf, GOTD_IMSG_CONNECT,
1342 ba97b2d7 2024-03-20 stsp PROC_GOTD, gotd.pid, sizeof(iconnect) + iconnect.username_len);
1343 ba97b2d7 2024-03-20 stsp if (wbuf == NULL) {
1344 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("imsg compose CONNECT");
1346 ae7c1b78 2023-01-10 stsp return err;
1348 ba97b2d7 2024-03-20 stsp if (imsg_add(wbuf, &iconnect, sizeof(iconnect)) == -1) {
1350 ba97b2d7 2024-03-20 stsp return got_error_from_errno("imsg_add CONNECT");
1352 ba97b2d7 2024-03-20 stsp if (imsg_add(wbuf, client->username, iconnect.username_len) == -1) {
1354 ba97b2d7 2024-03-20 stsp return got_error_from_errno("imsg_add CONNECT");
1357 ba97b2d7 2024-03-20 stsp ibuf_fd_set(wbuf, s);
1358 ba97b2d7 2024-03-20 stsp imsg_close(&client->session->iev.ibuf, wbuf);
1359 ba97b2d7 2024-03-20 stsp gotd_imsg_event_add(&client->session->iev);
1362 ae7c1b78 2023-01-10 stsp * We are no longer interested in messages from this client.
1363 ae7c1b78 2023-01-10 stsp * Further client requests will be handled by the session process.
1365 ae7c1b78 2023-01-10 stsp msgbuf_clear(&client->iev.ibuf.w);
1366 ae7c1b78 2023-01-10 stsp imsg_clear(&client->iev.ibuf);
1367 ae7c1b78 2023-01-10 stsp event_del(&client->iev.ev);
1368 ae7c1b78 2023-01-10 stsp client->fd = -1; /* will be closed via copy in client->iev.ibuf.fd */
1370 ae7c1b78 2023-01-10 stsp return NULL;
1373 5e25db14 2022-12-29 stsp static void
1374 ae7c1b78 2023-01-10 stsp gotd_dispatch_client_session(int fd, short event, void *arg)
1376 b50a2b46 2022-12-29 stsp struct gotd_imsgev *iev = arg;
1377 b50a2b46 2022-12-29 stsp struct imsgbuf *ibuf = &iev->ibuf;
1378 13b2bc37 2022-10-23 stsp struct gotd_child_proc *proc = NULL;
1379 b50a2b46 2022-12-29 stsp struct gotd_client *client = NULL;
1380 13b2bc37 2022-10-23 stsp ssize_t n;
1381 13b2bc37 2022-10-23 stsp int shut = 0;
1382 13b2bc37 2022-10-23 stsp struct imsg imsg;
1384 ae7c1b78 2023-01-10 stsp client = find_client_by_proc_fd(fd);
1385 ae0cca99 2023-02-09 stsp if (client == NULL) {
1386 ae0cca99 2023-02-09 stsp /* Can happen during process teardown. */
1387 ae0cca99 2023-02-09 stsp warnx("cannot find client for fd %d", fd);
1389 ae0cca99 2023-02-09 stsp goto done;
1392 13b2bc37 2022-10-23 stsp if (event & EV_READ) {
1393 13b2bc37 2022-10-23 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1394 13b2bc37 2022-10-23 stsp fatal("imsg_read error");
1395 13b2bc37 2022-10-23 stsp if (n == 0) {
1396 13b2bc37 2022-10-23 stsp /* Connection closed. */
1398 13b2bc37 2022-10-23 stsp goto done;
1402 13b2bc37 2022-10-23 stsp if (event & EV_WRITE) {
1403 13b2bc37 2022-10-23 stsp n = msgbuf_write(&ibuf->w);
1404 13b2bc37 2022-10-23 stsp if (n == -1 && errno != EAGAIN)
1405 13b2bc37 2022-10-23 stsp fatal("msgbuf_write");
1406 13b2bc37 2022-10-23 stsp if (n == 0) {
1407 13b2bc37 2022-10-23 stsp /* Connection closed. */
1409 13b2bc37 2022-10-23 stsp goto done;
1413 ae7c1b78 2023-01-10 stsp proc = client->session;
1414 ae7c1b78 2023-01-10 stsp if (proc == NULL)
1415 ae7c1b78 2023-01-10 stsp fatalx("cannot find session child process for fd %d", fd);
1417 ae7c1b78 2023-01-10 stsp for (;;) {
1418 ae7c1b78 2023-01-10 stsp const struct got_error *err = NULL;
1419 ae7c1b78 2023-01-10 stsp uint32_t client_id = 0;
1420 ae7c1b78 2023-01-10 stsp int do_disconnect = 0, do_start_repo_child = 0;
1422 ae7c1b78 2023-01-10 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
1423 ae7c1b78 2023-01-10 stsp fatal("%s: imsg_get error", __func__);
1424 ae7c1b78 2023-01-10 stsp if (n == 0) /* No more messages. */
1427 ae7c1b78 2023-01-10 stsp switch (imsg.hdr.type) {
1428 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_ERROR:
1429 ae7c1b78 2023-01-10 stsp do_disconnect = 1;
1430 ae7c1b78 2023-01-10 stsp err = gotd_imsg_recv_error(&client_id, &imsg);
1432 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_CLIENT_SESSION_READY:
1433 eac23c30 2023-01-10 stsp if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED) {
1434 ae7c1b78 2023-01-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1437 ae7c1b78 2023-01-10 stsp do_start_repo_child = 1;
1439 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_DISCONNECT:
1440 ae7c1b78 2023-01-10 stsp do_disconnect = 1;
1443 ae7c1b78 2023-01-10 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1447 ae7c1b78 2023-01-10 stsp if (!verify_imsg_src(client, proc, &imsg)) {
1448 ae7c1b78 2023-01-10 stsp log_debug("dropping imsg type %d from PID %d",
1449 ae7c1b78 2023-01-10 stsp imsg.hdr.type, proc->pid);
1450 ae7c1b78 2023-01-10 stsp imsg_free(&imsg);
1454 ae7c1b78 2023-01-10 stsp log_warnx("uid %d: %s", client->euid, err->msg);
1456 ae7c1b78 2023-01-10 stsp if (do_start_repo_child) {
1457 ae7c1b78 2023-01-10 stsp struct gotd_repo *repo;
1458 b09c1279 2023-03-28 stsp const char *name = client->session->repo_name;
1460 ba97b2d7 2024-03-20 stsp repo = gotd_find_repo_by_name(name, &gotd.repos);
1461 ae7c1b78 2023-01-10 stsp if (repo != NULL) {
1462 ae7c1b78 2023-01-10 stsp enum gotd_procid proc_type;
1464 ae7c1b78 2023-01-10 stsp if (client->required_auth & GOTD_AUTH_WRITE)
1465 ae7c1b78 2023-01-10 stsp proc_type = PROC_REPO_WRITE;
1467 ae7c1b78 2023-01-10 stsp proc_type = PROC_REPO_READ;
1469 ae7c1b78 2023-01-10 stsp err = start_repo_child(client, proc_type, repo,
1470 ae7c1b78 2023-01-10 stsp gotd.argv0, gotd.confpath, gotd.daemonize,
1471 ae7c1b78 2023-01-10 stsp gotd.verbosity);
1473 ae7c1b78 2023-01-10 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
1475 ae7c1b78 2023-01-10 stsp if (err) {
1476 ae7c1b78 2023-01-10 stsp log_warnx("uid %d: %s", client->euid, err->msg);
1477 ae7c1b78 2023-01-10 stsp do_disconnect = 1;
1481 ae7c1b78 2023-01-10 stsp if (do_disconnect) {
1483 ae7c1b78 2023-01-10 stsp disconnect_on_error(client, err);
1485 ae7c1b78 2023-01-10 stsp disconnect(client);
1488 ae7c1b78 2023-01-10 stsp imsg_free(&imsg);
1491 ae7c1b78 2023-01-10 stsp if (!shut) {
1492 ae7c1b78 2023-01-10 stsp gotd_imsg_event_add(iev);
1494 ae7c1b78 2023-01-10 stsp /* This pipe is dead. Remove its event handler */
1495 ae7c1b78 2023-01-10 stsp event_del(&iev->ev);
1496 ae7c1b78 2023-01-10 stsp disconnect(client);
1500 ba97b2d7 2024-03-20 stsp static const struct got_error *
1501 ba97b2d7 2024-03-20 stsp connect_notifier_and_session(struct gotd_client *client)
1503 ba97b2d7 2024-03-20 stsp const struct got_error *err = NULL;
1504 ba97b2d7 2024-03-20 stsp struct gotd_imsgev *session_iev = &client->session->iev;
1505 ba97b2d7 2024-03-20 stsp int pipe[2];
1507 ba97b2d7 2024-03-20 stsp if (gotd.notify_proc == NULL)
1508 ba97b2d7 2024-03-20 stsp return NULL;
1510 ba97b2d7 2024-03-20 stsp if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1511 ba97b2d7 2024-03-20 stsp PF_UNSPEC, pipe) == -1)
1512 ba97b2d7 2024-03-20 stsp return got_error_from_errno("socketpair");
1514 ba97b2d7 2024-03-20 stsp /* Pass notifier pipe to session . */
1515 ba97b2d7 2024-03-20 stsp if (gotd_imsg_compose_event(session_iev, GOTD_IMSG_CONNECT_NOTIFIER,
1516 ba97b2d7 2024-03-20 stsp PROC_GOTD, pipe[0], NULL, 0) == -1) {
1517 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("imsg compose CONNECT_NOTIFIER");
1518 ba97b2d7 2024-03-20 stsp close(pipe[0]);
1519 ba97b2d7 2024-03-20 stsp close(pipe[1]);
1520 ba97b2d7 2024-03-20 stsp return err;
1523 ba97b2d7 2024-03-20 stsp /* Pass session pipe to notifier. */
1524 ba97b2d7 2024-03-20 stsp if (gotd_imsg_compose_event(&gotd.notify_proc->iev,
1525 ba97b2d7 2024-03-20 stsp GOTD_IMSG_CONNECT_SESSION, PROC_GOTD, pipe[1], NULL, 0) == -1) {
1526 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("imsg compose CONNECT_SESSION");
1527 ba97b2d7 2024-03-20 stsp close(pipe[1]);
1528 ba97b2d7 2024-03-20 stsp return err;
1531 ba97b2d7 2024-03-20 stsp return NULL;
1534 ae7c1b78 2023-01-10 stsp static void
1535 ae7c1b78 2023-01-10 stsp gotd_dispatch_repo_child(int fd, short event, void *arg)
1537 ae7c1b78 2023-01-10 stsp struct gotd_imsgev *iev = arg;
1538 ae7c1b78 2023-01-10 stsp struct imsgbuf *ibuf = &iev->ibuf;
1539 ae7c1b78 2023-01-10 stsp struct gotd_child_proc *proc = NULL;
1540 ae7c1b78 2023-01-10 stsp struct gotd_client *client;
1541 ae7c1b78 2023-01-10 stsp ssize_t n;
1542 ae7c1b78 2023-01-10 stsp int shut = 0;
1543 ae7c1b78 2023-01-10 stsp struct imsg imsg;
1545 b50a2b46 2022-12-29 stsp client = find_client_by_proc_fd(fd);
1546 ae0cca99 2023-02-09 stsp if (client == NULL) {
1547 ae0cca99 2023-02-09 stsp /* Can happen during process teardown. */
1548 ae0cca99 2023-02-09 stsp warnx("cannot find client for fd %d", fd);
1550 ae0cca99 2023-02-09 stsp goto done;
1553 ae7c1b78 2023-01-10 stsp if (event & EV_READ) {
1554 ae7c1b78 2023-01-10 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1555 ae7c1b78 2023-01-10 stsp fatal("imsg_read error");
1556 ae7c1b78 2023-01-10 stsp if (n == 0) {
1557 ae7c1b78 2023-01-10 stsp /* Connection closed. */
1559 ae7c1b78 2023-01-10 stsp goto done;
1563 ae7c1b78 2023-01-10 stsp if (event & EV_WRITE) {
1564 ae7c1b78 2023-01-10 stsp n = msgbuf_write(&ibuf->w);
1565 ae7c1b78 2023-01-10 stsp if (n == -1 && errno != EAGAIN)
1566 ae7c1b78 2023-01-10 stsp fatal("msgbuf_write");
1567 ae7c1b78 2023-01-10 stsp if (n == 0) {
1568 ae7c1b78 2023-01-10 stsp /* Connection closed. */
1570 ae7c1b78 2023-01-10 stsp goto done;
1574 f7a854cf 2023-01-10 stsp proc = client->repo;
1575 13b2bc37 2022-10-23 stsp if (proc == NULL)
1576 13b2bc37 2022-10-23 stsp fatalx("cannot find child process for fd %d", fd);
1578 13b2bc37 2022-10-23 stsp for (;;) {
1579 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
1580 13b2bc37 2022-10-23 stsp uint32_t client_id = 0;
1581 13b2bc37 2022-10-23 stsp int do_disconnect = 0;
1583 13b2bc37 2022-10-23 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
1584 13b2bc37 2022-10-23 stsp fatal("%s: imsg_get error", __func__);
1585 13b2bc37 2022-10-23 stsp if (n == 0) /* No more messages. */
1588 13b2bc37 2022-10-23 stsp switch (imsg.hdr.type) {
1589 13b2bc37 2022-10-23 stsp case GOTD_IMSG_ERROR:
1590 13b2bc37 2022-10-23 stsp do_disconnect = 1;
1591 13b2bc37 2022-10-23 stsp err = gotd_imsg_recv_error(&client_id, &imsg);
1593 b50a2b46 2022-12-29 stsp case GOTD_IMSG_REPO_CHILD_READY:
1594 ae7c1b78 2023-01-10 stsp err = connect_session(client);
1597 ba97b2d7 2024-03-20 stsp err = connect_notifier_and_session(client);
1600 ae7c1b78 2023-01-10 stsp err = connect_repo_child(client, proc);
1603 13b2bc37 2022-10-23 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1607 13b2bc37 2022-10-23 stsp if (!verify_imsg_src(client, proc, &imsg)) {
1608 13b2bc37 2022-10-23 stsp log_debug("dropping imsg type %d from PID %d",
1609 13b2bc37 2022-10-23 stsp imsg.hdr.type, proc->pid);
1610 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
1614 13b2bc37 2022-10-23 stsp log_warnx("uid %d: %s", client->euid, err->msg);
1616 13b2bc37 2022-10-23 stsp if (do_disconnect) {
1618 13b2bc37 2022-10-23 stsp disconnect_on_error(client, err);
1620 13b2bc37 2022-10-23 stsp disconnect(client);
1623 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
1626 13b2bc37 2022-10-23 stsp if (!shut) {
1627 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(iev);
1629 13b2bc37 2022-10-23 stsp /* This pipe is dead. Remove its event handler */
1630 13b2bc37 2022-10-23 stsp event_del(&iev->ev);
1631 ae7c1b78 2023-01-10 stsp disconnect(client);
1635 13b2bc37 2022-10-23 stsp static pid_t
1636 eec68231 2022-12-14 stsp start_child(enum gotd_procid proc_id, const char *repo_path,
1637 585362fd 2022-10-31 op char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
1639 97ddbd29 2024-09-04 op const char *argv[11];
1640 97ddbd29 2024-09-04 op int argc = 0;
1643 13b2bc37 2022-10-23 stsp switch (pid = fork()) {
1645 13b2bc37 2022-10-23 stsp fatal("cannot fork");
1649 13b2bc37 2022-10-23 stsp close(fd);
1650 13b2bc37 2022-10-23 stsp return pid;
1653 8c6fc146 2022-11-17 stsp if (fd != GOTD_FILENO_MSG_PIPE) {
1654 8c6fc146 2022-11-17 stsp if (dup2(fd, GOTD_FILENO_MSG_PIPE) == -1)
1655 13b2bc37 2022-10-23 stsp fatal("cannot setup imsg fd");
1656 13b2bc37 2022-10-23 stsp } else if (fcntl(fd, F_SETFD, 0) == -1)
1657 13b2bc37 2022-10-23 stsp fatal("cannot setup imsg fd");
1659 13b2bc37 2022-10-23 stsp argv[argc++] = argv0;
1660 13b2bc37 2022-10-23 stsp switch (proc_id) {
1661 d93ecf7d 2022-12-14 stsp case PROC_LISTEN:
1662 97ddbd29 2024-09-04 op argv[argc++] = "-TL";
1664 5e25db14 2022-12-29 stsp case PROC_AUTH:
1665 97ddbd29 2024-09-04 op argv[argc++] = "-TA";
1667 b0614828 2023-06-19 stsp case PROC_SESSION_READ:
1668 97ddbd29 2024-09-04 op argv[argc++] = "-Ts";
1670 b0614828 2023-06-19 stsp case PROC_SESSION_WRITE:
1671 97ddbd29 2024-09-04 op argv[argc++] = "-TS";
1673 13b2bc37 2022-10-23 stsp case PROC_REPO_READ:
1674 97ddbd29 2024-09-04 op argv[argc++] = "-TR";
1676 13b2bc37 2022-10-23 stsp case PROC_REPO_WRITE:
1677 97ddbd29 2024-09-04 op argv[argc++] = "-TW";
1679 ba97b2d7 2024-03-20 stsp case PROC_NOTIFY:
1680 97ddbd29 2024-09-04 op argv[argc++] = "-TN";
1683 13b2bc37 2022-10-23 stsp fatalx("invalid process id %d", proc_id);
1686 97ddbd29 2024-09-04 op argv[argc++] = "-f";
1687 97ddbd29 2024-09-04 op argv[argc++] = confpath;
1689 eec68231 2022-12-14 stsp if (repo_path) {
1690 97ddbd29 2024-09-04 op argv[argc++] = "-P";
1691 97ddbd29 2024-09-04 op argv[argc++] = repo_path;
1694 13b2bc37 2022-10-23 stsp if (!daemonize)
1695 97ddbd29 2024-09-04 op argv[argc++] = "-d";
1696 13b2bc37 2022-10-23 stsp if (verbosity > 0)
1697 97ddbd29 2024-09-04 op argv[argc++] = "-v";
1698 13b2bc37 2022-10-23 stsp if (verbosity > 1)
1699 97ddbd29 2024-09-04 op argv[argc++] = "-v";
1700 13b2bc37 2022-10-23 stsp argv[argc++] = NULL;
1702 97ddbd29 2024-09-04 op execvp(argv0, (char * const *)argv);
1703 13b2bc37 2022-10-23 stsp fatal("execvp");
1706 13b2bc37 2022-10-23 stsp static void
1707 d93ecf7d 2022-12-14 stsp start_listener(char *argv0, const char *confpath, int daemonize, int verbosity)
1709 c929736a 2023-06-22 op struct gotd_child_proc *proc;
1711 c929736a 2023-06-22 op proc = calloc(1, sizeof(*proc));
1712 c929736a 2023-06-22 op if (proc == NULL)
1713 c929736a 2023-06-22 op fatal("calloc");
1715 839338f6 2023-06-22 op TAILQ_INSERT_HEAD(&procs, proc, entry);
1717 839338f6 2023-06-22 op /* proc->tmo is initialized in main() after event_init() */
1719 d93ecf7d 2022-12-14 stsp proc->type = PROC_LISTEN;
1721 d93ecf7d 2022-12-14 stsp if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1722 d93ecf7d 2022-12-14 stsp PF_UNSPEC, proc->pipe) == -1)
1723 d93ecf7d 2022-12-14 stsp fatal("socketpair");
1725 d93ecf7d 2022-12-14 stsp proc->pid = start_child(proc->type, NULL, argv0, confpath,
1726 d93ecf7d 2022-12-14 stsp proc->pipe[1], daemonize, verbosity);
1727 d93ecf7d 2022-12-14 stsp imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1728 b50a2b46 2022-12-29 stsp proc->iev.handler = gotd_dispatch_listener;
1729 d93ecf7d 2022-12-14 stsp proc->iev.events = EV_READ;
1730 d93ecf7d 2022-12-14 stsp proc->iev.handler_arg = NULL;
1732 c929736a 2023-06-22 op gotd.listen_proc = proc;
1735 ba97b2d7 2024-03-20 stsp static void
1736 ba97b2d7 2024-03-20 stsp start_notifier(char *argv0, const char *confpath, int daemonize, int verbosity)
1738 ba97b2d7 2024-03-20 stsp struct gotd_child_proc *proc;
1740 ba97b2d7 2024-03-20 stsp proc = calloc(1, sizeof(*proc));
1741 ba97b2d7 2024-03-20 stsp if (proc == NULL)
1742 ba97b2d7 2024-03-20 stsp fatal("calloc");
1744 ba97b2d7 2024-03-20 stsp TAILQ_INSERT_HEAD(&procs, proc, entry);
1746 ba97b2d7 2024-03-20 stsp /* proc->tmo is initialized in main() after event_init() */
1748 ba97b2d7 2024-03-20 stsp proc->type = PROC_NOTIFY;
1750 ba97b2d7 2024-03-20 stsp if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1751 ba97b2d7 2024-03-20 stsp PF_UNSPEC, proc->pipe) == -1)
1752 ba97b2d7 2024-03-20 stsp fatal("socketpair");
1754 ba97b2d7 2024-03-20 stsp proc->pid = start_child(proc->type, NULL, argv0, confpath,
1755 ba97b2d7 2024-03-20 stsp proc->pipe[1], daemonize, verbosity);
1756 ba97b2d7 2024-03-20 stsp imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1757 ba97b2d7 2024-03-20 stsp proc->iev.handler = gotd_dispatch_notifier;
1758 ba97b2d7 2024-03-20 stsp proc->iev.events = EV_READ;
1759 ba97b2d7 2024-03-20 stsp proc->iev.handler_arg = NULL;
1760 ba97b2d7 2024-03-20 stsp event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1761 ba97b2d7 2024-03-20 stsp gotd_dispatch_notifier, &proc->iev);
1763 ba97b2d7 2024-03-20 stsp gotd.notify_proc = proc;
1766 b50a2b46 2022-12-29 stsp static const struct got_error *
1767 ae7c1b78 2023-01-10 stsp start_session_child(struct gotd_client *client, struct gotd_repo *repo,
1768 ae7c1b78 2023-01-10 stsp char *argv0, const char *confpath, int daemonize, int verbosity)
1770 ae7c1b78 2023-01-10 stsp struct gotd_child_proc *proc;
1772 ae7c1b78 2023-01-10 stsp proc = calloc(1, sizeof(*proc));
1773 ae7c1b78 2023-01-10 stsp if (proc == NULL)
1774 ae7c1b78 2023-01-10 stsp return got_error_from_errno("calloc");
1776 839338f6 2023-06-22 op TAILQ_INSERT_HEAD(&procs, proc, entry);
1777 839338f6 2023-06-22 op evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1779 b0614828 2023-06-19 stsp if (client_is_reading(client))
1780 b0614828 2023-06-19 stsp proc->type = PROC_SESSION_READ;
1782 b0614828 2023-06-19 stsp proc->type = PROC_SESSION_WRITE;
1783 ae7c1b78 2023-01-10 stsp if (strlcpy(proc->repo_name, repo->name,
1784 ae7c1b78 2023-01-10 stsp sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1785 ae7c1b78 2023-01-10 stsp fatalx("repository name too long: %s", repo->name);
1786 ae7c1b78 2023-01-10 stsp log_debug("starting client uid %d session for repository %s",
1787 ae7c1b78 2023-01-10 stsp client->euid, repo->name);
1788 ae7c1b78 2023-01-10 stsp if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1789 ae7c1b78 2023-01-10 stsp sizeof(proc->repo_path))
1790 ae7c1b78 2023-01-10 stsp fatalx("repository path too long: %s", repo->path);
1791 ae7c1b78 2023-01-10 stsp if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1792 ae7c1b78 2023-01-10 stsp PF_UNSPEC, proc->pipe) == -1)
1793 ae7c1b78 2023-01-10 stsp fatal("socketpair");
1794 ae7c1b78 2023-01-10 stsp proc->pid = start_child(proc->type, proc->repo_path, argv0,
1795 ae7c1b78 2023-01-10 stsp confpath, proc->pipe[1], daemonize, verbosity);
1796 ae7c1b78 2023-01-10 stsp imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1797 ae7c1b78 2023-01-10 stsp log_debug("proc %s %s is on fd %d",
1798 ae7c1b78 2023-01-10 stsp gotd_proc_names[proc->type], proc->repo_path,
1799 ae7c1b78 2023-01-10 stsp proc->pipe[0]);
1800 ae7c1b78 2023-01-10 stsp proc->iev.handler = gotd_dispatch_client_session;
1801 ae7c1b78 2023-01-10 stsp proc->iev.events = EV_READ;
1802 ae7c1b78 2023-01-10 stsp proc->iev.handler_arg = NULL;
1803 ae7c1b78 2023-01-10 stsp event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1804 ae7c1b78 2023-01-10 stsp gotd_dispatch_client_session, &proc->iev);
1805 ae7c1b78 2023-01-10 stsp gotd_imsg_event_add(&proc->iev);
1807 ae7c1b78 2023-01-10 stsp client->session = proc;
1808 ae7c1b78 2023-01-10 stsp return NULL;
1811 ae7c1b78 2023-01-10 stsp static const struct got_error *
1812 b50a2b46 2022-12-29 stsp start_repo_child(struct gotd_client *client, enum gotd_procid proc_type,
1813 b50a2b46 2022-12-29 stsp struct gotd_repo *repo, char *argv0, const char *confpath,
1814 585362fd 2022-10-31 op int daemonize, int verbosity)
1816 13b2bc37 2022-10-23 stsp struct gotd_child_proc *proc;
1818 b50a2b46 2022-12-29 stsp if (proc_type != PROC_REPO_READ && proc_type != PROC_REPO_WRITE)
1819 b50a2b46 2022-12-29 stsp return got_error_msg(GOT_ERR_NOT_IMPL, "bad process type");
1821 b50a2b46 2022-12-29 stsp proc = calloc(1, sizeof(*proc));
1822 b50a2b46 2022-12-29 stsp if (proc == NULL)
1823 b50a2b46 2022-12-29 stsp return got_error_from_errno("calloc");
1825 839338f6 2023-06-22 op TAILQ_INSERT_HEAD(&procs, proc, entry);
1826 839338f6 2023-06-22 op evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1828 b50a2b46 2022-12-29 stsp proc->type = proc_type;
1829 b50a2b46 2022-12-29 stsp if (strlcpy(proc->repo_name, repo->name,
1830 b50a2b46 2022-12-29 stsp sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1831 b50a2b46 2022-12-29 stsp fatalx("repository name too long: %s", repo->name);
1832 b50a2b46 2022-12-29 stsp log_debug("starting %s for repository %s",
1833 b50a2b46 2022-12-29 stsp proc->type == PROC_REPO_READ ? "reader" : "writer", repo->name);
1834 9b7f22a6 2023-01-08 stsp if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1835 9b7f22a6 2023-01-08 stsp sizeof(proc->repo_path))
1836 9b7f22a6 2023-01-08 stsp fatalx("repository path too long: %s", repo->path);
1837 b50a2b46 2022-12-29 stsp if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1838 b50a2b46 2022-12-29 stsp PF_UNSPEC, proc->pipe) == -1)
1839 b50a2b46 2022-12-29 stsp fatal("socketpair");
1840 b50a2b46 2022-12-29 stsp proc->pid = start_child(proc->type, proc->repo_path, argv0,
1841 b50a2b46 2022-12-29 stsp confpath, proc->pipe[1], daemonize, verbosity);
1842 b50a2b46 2022-12-29 stsp imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1843 b50a2b46 2022-12-29 stsp log_debug("proc %s %s is on fd %d",
1844 b50a2b46 2022-12-29 stsp gotd_proc_names[proc->type], proc->repo_path,
1845 b50a2b46 2022-12-29 stsp proc->pipe[0]);
1846 b50a2b46 2022-12-29 stsp proc->iev.handler = gotd_dispatch_repo_child;
1847 b50a2b46 2022-12-29 stsp proc->iev.events = EV_READ;
1848 b50a2b46 2022-12-29 stsp proc->iev.handler_arg = NULL;
1849 b50a2b46 2022-12-29 stsp event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1850 b50a2b46 2022-12-29 stsp gotd_dispatch_repo_child, &proc->iev);
1851 b50a2b46 2022-12-29 stsp gotd_imsg_event_add(&proc->iev);
1853 f7a854cf 2023-01-10 stsp client->repo = proc;
1854 5e25db14 2022-12-29 stsp return NULL;
1857 5e25db14 2022-12-29 stsp static const struct got_error *
1858 5e25db14 2022-12-29 stsp start_auth_child(struct gotd_client *client, int required_auth,
1859 5e25db14 2022-12-29 stsp struct gotd_repo *repo, char *argv0, const char *confpath,
1860 5e25db14 2022-12-29 stsp int daemonize, int verbosity)
1862 365cf0f3 2022-12-29 stsp const struct got_error *err = NULL;
1863 5e25db14 2022-12-29 stsp struct gotd_child_proc *proc;
1864 5e25db14 2022-12-29 stsp struct gotd_imsg_auth iauth;
1867 5e25db14 2022-12-29 stsp memset(&iauth, 0, sizeof(iauth));
1869 365cf0f3 2022-12-29 stsp fd = dup(client->fd);
1870 365cf0f3 2022-12-29 stsp if (fd == -1)
1871 365cf0f3 2022-12-29 stsp return got_error_from_errno("dup");
1873 5e25db14 2022-12-29 stsp proc = calloc(1, sizeof(*proc));
1874 365cf0f3 2022-12-29 stsp if (proc == NULL) {
1875 365cf0f3 2022-12-29 stsp err = got_error_from_errno("calloc");
1876 365cf0f3 2022-12-29 stsp close(fd);
1877 365cf0f3 2022-12-29 stsp return err;
1880 839338f6 2023-06-22 op TAILQ_INSERT_HEAD(&procs, proc, entry);
1881 839338f6 2023-06-22 op evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1883 5e25db14 2022-12-29 stsp proc->type = PROC_AUTH;
1884 5e25db14 2022-12-29 stsp if (strlcpy(proc->repo_name, repo->name,
1885 5e25db14 2022-12-29 stsp sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1886 5e25db14 2022-12-29 stsp fatalx("repository name too long: %s", repo->name);
1887 5e25db14 2022-12-29 stsp log_debug("starting auth for uid %d repository %s",
1888 5e25db14 2022-12-29 stsp client->euid, repo->name);
1889 9b7f22a6 2023-01-08 stsp if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1890 9b7f22a6 2023-01-08 stsp sizeof(proc->repo_path))
1891 9b7f22a6 2023-01-08 stsp fatalx("repository path too long: %s", repo->path);
1892 5e25db14 2022-12-29 stsp if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1893 5e25db14 2022-12-29 stsp PF_UNSPEC, proc->pipe) == -1)
1894 5e25db14 2022-12-29 stsp fatal("socketpair");
1895 5e25db14 2022-12-29 stsp proc->pid = start_child(proc->type, proc->repo_path, argv0,
1896 5e25db14 2022-12-29 stsp confpath, proc->pipe[1], daemonize, verbosity);
1897 5e25db14 2022-12-29 stsp imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1898 5e25db14 2022-12-29 stsp log_debug("proc %s %s is on fd %d",
1899 5e25db14 2022-12-29 stsp gotd_proc_names[proc->type], proc->repo_path,
1900 5e25db14 2022-12-29 stsp proc->pipe[0]);
1901 5e25db14 2022-12-29 stsp proc->iev.handler = gotd_dispatch_auth_child;
1902 5e25db14 2022-12-29 stsp proc->iev.events = EV_READ;
1903 5e25db14 2022-12-29 stsp proc->iev.handler_arg = NULL;
1904 5e25db14 2022-12-29 stsp event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1905 5e25db14 2022-12-29 stsp gotd_dispatch_auth_child, &proc->iev);
1906 5e25db14 2022-12-29 stsp gotd_imsg_event_add(&proc->iev);
1908 5e25db14 2022-12-29 stsp iauth.euid = client->euid;
1909 5e25db14 2022-12-29 stsp iauth.egid = client->egid;
1910 5e25db14 2022-12-29 stsp iauth.required_auth = required_auth;
1911 5e25db14 2022-12-29 stsp iauth.client_id = client->id;
1912 5e25db14 2022-12-29 stsp if (gotd_imsg_compose_event(&proc->iev, GOTD_IMSG_AUTHENTICATE,
1913 365cf0f3 2022-12-29 stsp PROC_GOTD, fd, &iauth, sizeof(iauth)) == -1) {
1914 5e25db14 2022-12-29 stsp log_warn("imsg compose AUTHENTICATE");
1915 365cf0f3 2022-12-29 stsp close(fd);
1916 365cf0f3 2022-12-29 stsp /* Let the auth_timeout handler tidy up. */
1919 5e25db14 2022-12-29 stsp client->auth = proc;
1920 5e25db14 2022-12-29 stsp client->required_auth = required_auth;
1921 b50a2b46 2022-12-29 stsp return NULL;
1924 eec68231 2022-12-14 stsp static void
1925 b0614828 2023-06-19 stsp apply_unveil_repo_readonly(const char *repo_path, int need_tmpdir)
1927 b0614828 2023-06-19 stsp if (need_tmpdir) {
1928 b0614828 2023-06-19 stsp if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1929 b0614828 2023-06-19 stsp fatal("unveil %s", GOT_TMPDIR_STR);
1932 eec68231 2022-12-14 stsp if (unveil(repo_path, "r") == -1)
1933 eec68231 2022-12-14 stsp fatal("unveil %s", repo_path);
1935 44587340 2022-12-30 stsp if (unveil(NULL, NULL) == -1)
1936 44587340 2022-12-30 stsp fatal("unveil");
1939 44587340 2022-12-30 stsp static void
1940 ae7c1b78 2023-01-10 stsp apply_unveil_repo_readwrite(const char *repo_path)
1942 ae7c1b78 2023-01-10 stsp if (unveil(repo_path, "rwc") == -1)
1943 ae7c1b78 2023-01-10 stsp fatal("unveil %s", repo_path);
1945 ae7c1b78 2023-01-10 stsp if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1946 ae7c1b78 2023-01-10 stsp fatal("unveil %s", GOT_TMPDIR_STR);
1948 ae7c1b78 2023-01-10 stsp if (unveil(NULL, NULL) == -1)
1949 ae7c1b78 2023-01-10 stsp fatal("unveil");
1952 ae7c1b78 2023-01-10 stsp static void
1953 44587340 2022-12-30 stsp apply_unveil_none(void)
1955 44587340 2022-12-30 stsp if (unveil("/", "") == -1)
1956 44587340 2022-12-30 stsp fatal("unveil");
1958 eec68231 2022-12-14 stsp if (unveil(NULL, NULL) == -1)
1959 eec68231 2022-12-14 stsp fatal("unveil");
1962 13b2bc37 2022-10-23 stsp static void
1963 ae7c1b78 2023-01-10 stsp apply_unveil_selfexec(void)
1965 b50a2b46 2022-12-29 stsp if (unveil(gotd.argv0, "x") == -1)
1966 b50a2b46 2022-12-29 stsp fatal("unveil %s", gotd.argv0);
1968 13b2bc37 2022-10-23 stsp if (unveil(NULL, NULL) == -1)
1969 13b2bc37 2022-10-23 stsp fatal("unveil");
1972 abe89edb 2023-11-16 stsp static void
1973 abe89edb 2023-11-16 stsp set_max_datasize(void)
1975 abe89edb 2023-11-16 stsp struct rlimit rl;
1977 abe89edb 2023-11-16 stsp if (getrlimit(RLIMIT_DATA, &rl) != 0)
1980 abe89edb 2023-11-16 stsp rl.rlim_cur = rl.rlim_max;
1981 abe89edb 2023-11-16 stsp setrlimit(RLIMIT_DATA, &rl);
1984 ba97b2d7 2024-03-20 stsp static void
1985 ba97b2d7 2024-03-20 stsp unveil_notification_helpers(void)
1987 ba97b2d7 2024-03-20 stsp const char *helpers[] = {
1988 ba97b2d7 2024-03-20 stsp GOTD_PATH_PROG_NOTIFY_EMAIL,
1989 ba97b2d7 2024-03-20 stsp GOTD_PATH_PROG_NOTIFY_HTTP,
1993 ba97b2d7 2024-03-20 stsp for (i = 0; i < nitems(helpers); i++) {
1994 ba97b2d7 2024-03-20 stsp if (unveil(helpers[i], "x") == 0)
1996 ba97b2d7 2024-03-20 stsp fatal("unveil %s", helpers[i]);
1999 ba97b2d7 2024-03-20 stsp if (unveil(NULL, NULL) == -1)
2000 ba97b2d7 2024-03-20 stsp fatal("unveil");
2003 b4358cd0 2024-09-08 stsp static void
2004 b4358cd0 2024-09-08 stsp check_file_secrecy(int fd, const char *fname)
2006 b4358cd0 2024-09-08 stsp struct stat st;
2008 b4358cd0 2024-09-08 stsp if (fstat(fd, &st))
2009 b4358cd0 2024-09-08 stsp fatal("cannot stat %s", fname);
2011 b4358cd0 2024-09-08 stsp if (st.st_uid != 0)
2012 b4358cd0 2024-09-08 stsp fatalx("secrets file %s must be owned by root", fname);
2014 b4358cd0 2024-09-08 stsp if (st.st_gid != 0)
2015 b4358cd0 2024-09-08 stsp fatalx("secrets file %s must be owned by group wheel/root",
2018 b4358cd0 2024-09-08 stsp if (st.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO))
2019 b4358cd0 2024-09-08 stsp fatalx("secrets file %s must not be group writable or world "
2020 b4358cd0 2024-09-08 stsp "readable/writable", fname);
2024 13b2bc37 2022-10-23 stsp main(int argc, char **argv)
2026 13b2bc37 2022-10-23 stsp const struct got_error *error = NULL;
2027 5fb267cb 2024-09-08 op struct gotd_secrets *secrets = NULL;
2028 13b2bc37 2022-10-23 stsp int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
2029 13b2bc37 2022-10-23 stsp const char *confpath = GOTD_CONF_PATH;
2030 f7520f7a 2024-09-08 stsp char *secretspath = NULL;
2031 13b2bc37 2022-10-23 stsp char *argv0 = argv[0];
2032 13b2bc37 2022-10-23 stsp char title[2048];
2033 13b2bc37 2022-10-23 stsp struct passwd *pw = NULL;
2035 13b2bc37 2022-10-23 stsp char *repo_path = NULL;
2036 13b2bc37 2022-10-23 stsp enum gotd_procid proc_id = PROC_GOTD;
2037 839338f6 2023-06-22 op struct event evsigint, evsigterm, evsighup, evsigusr1, evsigchld;
2038 13b2bc37 2022-10-23 stsp int *pack_fds = NULL, *temp_fds = NULL;
2039 9afa3de2 2023-04-04 stsp struct gotd_repo *repo = NULL;
2040 ba97b2d7 2024-03-20 stsp char *default_sender = NULL;
2041 d194664f 2024-09-10 op char hostname[_POSIX_HOST_NAME_MAX + 1];
2043 ba97b2d7 2024-03-20 stsp FILE *diff_f1 = NULL, *diff_f2 = NULL;
2044 ba97b2d7 2024-03-20 stsp int diff_fd1 = -1, diff_fd2 = -1;
2045 df3d15f3 2024-08-06 op const char *errstr;
2047 839338f6 2023-06-22 op TAILQ_INIT(&procs);
2049 13b2bc37 2022-10-23 stsp log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
2051 5fb267cb 2024-09-08 op while ((ch = getopt(argc, argv, "df:nP:s:T:v")) != -1) {
2052 13b2bc37 2022-10-23 stsp switch (ch) {
2054 13b2bc37 2022-10-23 stsp daemonize = 0;
2057 13b2bc37 2022-10-23 stsp confpath = optarg;
2060 13b2bc37 2022-10-23 stsp noaction = 1;
2063 6f319063 2022-10-27 stsp repo_path = realpath(optarg, NULL);
2064 6f319063 2022-10-27 stsp if (repo_path == NULL)
2065 6f319063 2022-10-27 stsp fatal("realpath '%s'", optarg);
2068 f7520f7a 2024-09-08 stsp secretspath = realpath(optarg, NULL);
2069 f7520f7a 2024-09-08 stsp if (secretspath == NULL)
2070 f7520f7a 2024-09-08 stsp fatal("realpath '%s'", optarg);
2073 17f7cea3 2024-09-04 op switch (*optarg) {
2075 17f7cea3 2024-09-04 op proc_id = PROC_AUTH;
2078 17f7cea3 2024-09-04 op proc_id = PROC_LISTEN;
2081 17f7cea3 2024-09-04 op proc_id = PROC_NOTIFY;
2084 17f7cea3 2024-09-04 op proc_id = PROC_REPO_READ;
2087 17f7cea3 2024-09-04 op proc_id = PROC_SESSION_READ;
2090 17f7cea3 2024-09-04 op proc_id = PROC_SESSION_WRITE;
2093 17f7cea3 2024-09-04 op proc_id = PROC_REPO_WRITE;
2096 17f7cea3 2024-09-04 op errx(1, "unknown proc type %s", optarg);
2100 6f319063 2022-10-27 stsp if (verbosity < 3)
2101 6f319063 2022-10-27 stsp verbosity++;
2108 13b2bc37 2022-10-23 stsp argc -= optind;
2109 13b2bc37 2022-10-23 stsp argv += optind;
2111 13b2bc37 2022-10-23 stsp if (argc != 0)
2114 b50a2b46 2022-12-29 stsp if (geteuid() && (proc_id == PROC_GOTD || proc_id == PROC_LISTEN))
2115 13b2bc37 2022-10-23 stsp fatalx("need root privileges");
2117 5fb267cb 2024-09-08 op if (proc_id == PROC_GOTD) {
2118 5fb267cb 2024-09-08 op const char *p = secretspath ? secretspath : GOTD_SECRETS_PATH;
2120 5fb267cb 2024-09-08 op fp = fopen(p, "r");
2121 5fb267cb 2024-09-08 op if (fp == NULL && (secretspath != NULL || errno != ENOENT))
2122 b4358cd0 2024-09-08 stsp fatal("can't open secrets file %s", p);
2124 5fb267cb 2024-09-08 op if (fp != NULL) {
2125 b4358cd0 2024-09-08 stsp check_file_secrecy(fileno(fp), p);
2126 5fb267cb 2024-09-08 op error = gotd_secrets_parse(p, fp, &secrets);
2129 5fb267cb 2024-09-08 op fatalx("failed to parse secrets file %s: %s",
2130 5fb267cb 2024-09-08 op p, error->msg);
2134 5fb267cb 2024-09-08 op if (parse_config(confpath, proc_id, secrets, &gotd) != 0)
2137 13b2bc37 2022-10-23 stsp pw = getpwnam(gotd.user_name);
2138 df3d15f3 2024-08-06 op if (pw == NULL) {
2139 df3d15f3 2024-08-06 op uid = strtonum(gotd.user_name, 0, UID_MAX - 1, &errstr);
2140 df3d15f3 2024-08-06 op if (errstr == NULL)
2141 df3d15f3 2024-08-06 op pw = getpwuid(uid);
2143 13b2bc37 2022-10-23 stsp if (pw == NULL)
2144 898c8f8f 2022-12-29 op fatalx("user %s not found", gotd.user_name);
2146 f4e8c21c 2023-01-17 op if (pw->pw_uid == 0)
2147 f4e8c21c 2023-01-17 op fatalx("cannot run %s as the superuser", getprogname());
2150 ef3a5d52 2024-08-07 stsp * SHA2 repositories cannot be used with gotd until Git protocol v2
2151 ef3a5d52 2024-08-07 stsp * support is added. Reject them at startup for now.
2153 ef3a5d52 2024-08-07 stsp TAILQ_FOREACH(repo, &gotd.repos, entry) {
2154 ef3a5d52 2024-08-07 stsp struct got_repository *r;
2156 ef3a5d52 2024-08-07 stsp error = got_repo_open(&r, repo->path, NULL, NULL);
2157 ef3a5d52 2024-08-07 stsp if (error) {
2158 ef3a5d52 2024-08-07 stsp if (error->code == GOT_ERR_ERRNO && errno == ENOENT)
2160 ef3a5d52 2024-08-07 stsp fatalx("%s: %s", repo->path, error->msg);
2163 ef3a5d52 2024-08-07 stsp if (got_repo_get_object_format(r) != GOT_HASH_SHA1) {
2164 ef3a5d52 2024-08-07 stsp error = got_error_msg(GOT_ERR_NOT_IMPL,
2165 ef3a5d52 2024-08-07 stsp "sha256 object IDs unsupported in network "
2166 ef3a5d52 2024-08-07 stsp "protocol");
2167 ef3a5d52 2024-08-07 stsp fatalx("%s: %s", repo->path, error->msg);
2170 ef3a5d52 2024-08-07 stsp got_repo_close(r);
2173 f4e8c21c 2023-01-17 op if (noaction) {
2174 f4e8c21c 2023-01-17 op fprintf(stderr, "configuration OK\n");
2178 f4e8c21c 2023-01-17 op gotd.argv0 = argv0;
2179 f4e8c21c 2023-01-17 op gotd.daemonize = daemonize;
2180 f4e8c21c 2023-01-17 op gotd.verbosity = verbosity;
2181 f4e8c21c 2023-01-17 op gotd.confpath = confpath;
2183 f4e8c21c 2023-01-17 op /* Require an absolute path in argv[0] for reliable re-exec. */
2184 f4e8c21c 2023-01-17 op if (!got_path_is_absolute(argv0))
2185 f4e8c21c 2023-01-17 op fatalx("bad path \"%s\": must be an absolute path", argv0);
2187 f4e8c21c 2023-01-17 op log_init(daemonize ? 0 : 1, LOG_DAEMON);
2188 f4e8c21c 2023-01-17 op log_setverbose(verbosity);
2190 b1142068 2022-12-05 stsp if (proc_id == PROC_GOTD) {
2191 d93ecf7d 2022-12-14 stsp snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2192 d93ecf7d 2022-12-14 stsp arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
2193 d93ecf7d 2022-12-14 stsp if (daemonize && daemon(1, 0) == -1)
2194 d93ecf7d 2022-12-14 stsp fatal("daemon");
2195 f7eb3370 2023-01-23 stsp gotd.pid = getpid();
2196 f7eb3370 2023-01-23 stsp start_listener(argv0, confpath, daemonize, verbosity);
2197 ba97b2d7 2024-03-20 stsp start_notifier(argv0, confpath, daemonize, verbosity);
2198 d93ecf7d 2022-12-14 stsp } else if (proc_id == PROC_LISTEN) {
2199 d93ecf7d 2022-12-14 stsp snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2200 b1142068 2022-12-05 stsp if (verbosity) {
2201 b1142068 2022-12-05 stsp log_info("socket: %s", gotd.unix_socket_path);
2202 b1142068 2022-12-05 stsp log_info("user: %s", pw->pw_name);
2205 13b2bc37 2022-10-23 stsp fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
2206 6f854dde 2023-01-04 stsp pw->pw_gid);
2207 13b2bc37 2022-10-23 stsp if (fd == -1) {
2208 13b2bc37 2022-10-23 stsp fatal("cannot listen on unix socket %s",
2209 13b2bc37 2022-10-23 stsp gotd.unix_socket_path);
2211 5e25db14 2022-12-29 stsp } else if (proc_id == PROC_AUTH) {
2212 5e25db14 2022-12-29 stsp snprintf(title, sizeof(title), "%s %s",
2213 5e25db14 2022-12-29 stsp gotd_proc_names[proc_id], repo_path);
2214 ae7c1b78 2023-01-10 stsp } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE ||
2215 b0614828 2023-06-19 stsp proc_id == PROC_SESSION_READ || proc_id == PROC_SESSION_WRITE) {
2216 13b2bc37 2022-10-23 stsp error = got_repo_pack_fds_open(&pack_fds);
2217 13b2bc37 2022-10-23 stsp if (error != NULL)
2218 13b2bc37 2022-10-23 stsp fatalx("cannot open pack tempfiles: %s", error->msg);
2219 13b2bc37 2022-10-23 stsp error = got_repo_temp_fds_open(&temp_fds);
2220 13b2bc37 2022-10-23 stsp if (error != NULL)
2221 13b2bc37 2022-10-23 stsp fatalx("cannot open pack tempfiles: %s", error->msg);
2222 13b2bc37 2022-10-23 stsp if (repo_path == NULL)
2223 13b2bc37 2022-10-23 stsp fatalx("repository path not specified");
2224 13b2bc37 2022-10-23 stsp snprintf(title, sizeof(title), "%s %s",
2225 13b2bc37 2022-10-23 stsp gotd_proc_names[proc_id], repo_path);
2226 ba97b2d7 2024-03-20 stsp } else if (proc_id == PROC_NOTIFY) {
2227 ba97b2d7 2024-03-20 stsp snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2228 ba97b2d7 2024-03-20 stsp if (gethostname(hostname, sizeof(hostname)) == -1)
2229 ba97b2d7 2024-03-20 stsp fatal("gethostname");
2230 ba97b2d7 2024-03-20 stsp if (asprintf(&default_sender, "%s@%s",
2231 ba97b2d7 2024-03-20 stsp pw->pw_name, hostname) == -1)
2232 ba97b2d7 2024-03-20 stsp fatal("asprintf");
2234 13b2bc37 2022-10-23 stsp fatal("invalid process id %d", proc_id);
2236 13b2bc37 2022-10-23 stsp setproctitle("%s", title);
2237 13b2bc37 2022-10-23 stsp log_procinit(title);
2239 13b2bc37 2022-10-23 stsp /* Drop root privileges. */
2240 13b2bc37 2022-10-23 stsp if (setgid(pw->pw_gid) == -1)
2241 13b2bc37 2022-10-23 stsp fatal("setgid %d failed", pw->pw_gid);
2242 13b2bc37 2022-10-23 stsp if (setuid(pw->pw_uid) == -1)
2243 13b2bc37 2022-10-23 stsp fatal("setuid %d failed", pw->pw_uid);
2245 13b2bc37 2022-10-23 stsp event_init();
2247 13b2bc37 2022-10-23 stsp switch (proc_id) {
2248 13b2bc37 2022-10-23 stsp case PROC_GOTD:
2249 13b2bc37 2022-10-23 stsp #ifndef PROFILE
2250 ae7c1b78 2023-01-10 stsp /* "exec" promise will be limited to argv[0] via unveil(2). */
2251 ae7c1b78 2023-01-10 stsp if (pledge("stdio proc exec sendfd recvfd unveil", NULL) == -1)
2252 13b2bc37 2022-10-23 stsp err(1, "pledge");
2255 d93ecf7d 2022-12-14 stsp case PROC_LISTEN:
2256 d93ecf7d 2022-12-14 stsp #ifndef PROFILE
2257 77f619a8 2023-01-04 stsp if (pledge("stdio sendfd unix unveil", NULL) == -1)
2258 d93ecf7d 2022-12-14 stsp err(1, "pledge");
2261 77f619a8 2023-01-04 stsp * Ensure that AF_UNIX bind(2) cannot be used with any other
2262 77f619a8 2023-01-04 stsp * sockets by revoking all filesystem access via unveil(2).
2264 77f619a8 2023-01-04 stsp apply_unveil_none();
2266 40b85cca 2023-01-03 stsp listen_main(title, fd, gotd.connection_limits,
2267 40b85cca 2023-01-03 stsp gotd.nconnection_limits);
2268 d93ecf7d 2022-12-14 stsp /* NOTREACHED */
2270 5e25db14 2022-12-29 stsp case PROC_AUTH:
2271 5e25db14 2022-12-29 stsp #ifndef PROFILE
2272 44587340 2022-12-30 stsp if (pledge("stdio getpw recvfd unix unveil", NULL) == -1)
2273 5e25db14 2022-12-29 stsp err(1, "pledge");
2276 44587340 2022-12-30 stsp * We need the "unix" pledge promise for getpeername(2) only.
2277 44587340 2022-12-30 stsp * Ensure that AF_UNIX bind(2) cannot be used by revoking all
2278 44587340 2022-12-30 stsp * filesystem access via unveil(2). Access to password database
2279 44587340 2022-12-30 stsp * files will still work since "getpw" bypasses unveil(2).
2281 44587340 2022-12-30 stsp apply_unveil_none();
2283 5e25db14 2022-12-29 stsp auth_main(title, &gotd.repos, repo_path);
2284 5e25db14 2022-12-29 stsp /* NOTREACHED */
2286 b0614828 2023-06-19 stsp case PROC_SESSION_READ:
2287 b0614828 2023-06-19 stsp case PROC_SESSION_WRITE:
2288 ae7c1b78 2023-01-10 stsp #ifndef PROFILE
2290 ae7c1b78 2023-01-10 stsp * The "recvfd" promise is only needed during setup and
2291 ae7c1b78 2023-01-10 stsp * will be removed in a later pledge(2) call.
2293 ae7c1b78 2023-01-10 stsp if (pledge("stdio rpath wpath cpath recvfd sendfd fattr flock "
2294 ae7c1b78 2023-01-10 stsp "unveil", NULL) == -1)
2295 ae7c1b78 2023-01-10 stsp err(1, "pledge");
2297 b0614828 2023-06-19 stsp if (proc_id == PROC_SESSION_READ)
2298 b0614828 2023-06-19 stsp apply_unveil_repo_readonly(repo_path, 1);
2300 b0614828 2023-06-19 stsp apply_unveil_repo_readwrite(repo_path);
2301 ba97b2d7 2024-03-20 stsp repo = gotd_find_repo_by_path(repo_path, &gotd);
2302 ba97b2d7 2024-03-20 stsp if (repo == NULL)
2303 ba97b2d7 2024-03-20 stsp fatalx("no repository for path %s", repo_path);
2305 e70fd952 2024-03-22 stsp if (proc_id == PROC_SESSION_READ)
2306 e70fd952 2024-03-22 stsp session_read_main(title, repo_path, pack_fds, temp_fds,
2307 e70fd952 2024-03-22 stsp &gotd.request_timeout, repo);
2309 e70fd952 2024-03-22 stsp session_write_main(title, repo_path, pack_fds, temp_fds,
2310 e70fd952 2024-03-22 stsp &gotd.request_timeout, repo);
2311 ae7c1b78 2023-01-10 stsp /* NOTREACHED */
2313 13b2bc37 2022-10-23 stsp case PROC_REPO_READ:
2314 abe89edb 2023-11-16 stsp set_max_datasize();
2315 13b2bc37 2022-10-23 stsp #ifndef PROFILE
2316 eec68231 2022-12-14 stsp if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2317 13b2bc37 2022-10-23 stsp err(1, "pledge");
2319 b0614828 2023-06-19 stsp apply_unveil_repo_readonly(repo_path, 0);
2320 eec68231 2022-12-14 stsp repo_read_main(title, repo_path, pack_fds, temp_fds);
2321 13b2bc37 2022-10-23 stsp /* NOTREACHED */
2323 13b2bc37 2022-10-23 stsp case PROC_REPO_WRITE:
2324 abe89edb 2023-11-16 stsp set_max_datasize();
2326 ba97b2d7 2024-03-20 stsp diff_f1 = got_opentemp();
2327 ba97b2d7 2024-03-20 stsp if (diff_f1 == NULL)
2328 ba97b2d7 2024-03-20 stsp fatal("got_opentemp");
2329 ba97b2d7 2024-03-20 stsp diff_f2 = got_opentemp();
2330 ba97b2d7 2024-03-20 stsp if (diff_f2 == NULL)
2331 ba97b2d7 2024-03-20 stsp fatal("got_opentemp");
2332 ba97b2d7 2024-03-20 stsp diff_fd1 = got_opentempfd();
2333 ba97b2d7 2024-03-20 stsp if (diff_fd1 == -1)
2334 ba97b2d7 2024-03-20 stsp fatal("got_opentempfd");
2335 ba97b2d7 2024-03-20 stsp diff_fd2 = got_opentempfd();
2336 ba97b2d7 2024-03-20 stsp if (diff_fd2 == -1)
2337 ba97b2d7 2024-03-20 stsp fatal("got_opentempfd");
2338 13b2bc37 2022-10-23 stsp #ifndef PROFILE
2339 eec68231 2022-12-14 stsp if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2340 13b2bc37 2022-10-23 stsp err(1, "pledge");
2342 b0614828 2023-06-19 stsp apply_unveil_repo_readonly(repo_path, 0);
2343 9afa3de2 2023-04-04 stsp repo = gotd_find_repo_by_path(repo_path, &gotd);
2344 9afa3de2 2023-04-04 stsp if (repo == NULL)
2345 9afa3de2 2023-04-04 stsp fatalx("no repository for path %s", repo_path);
2346 9afa3de2 2023-04-04 stsp repo_write_main(title, repo_path, pack_fds, temp_fds,
2347 ba97b2d7 2024-03-20 stsp diff_f1, diff_f2, diff_fd1, diff_fd2,
2348 9afa3de2 2023-04-04 stsp &repo->protected_tag_namespaces,
2349 9afa3de2 2023-04-04 stsp &repo->protected_branch_namespaces,
2350 9afa3de2 2023-04-04 stsp &repo->protected_branches);
2351 13b2bc37 2022-10-23 stsp /* NOTREACHED */
2353 ba97b2d7 2024-03-20 stsp case PROC_NOTIFY:
2354 ba97b2d7 2024-03-20 stsp #ifndef PROFILE
2355 ba97b2d7 2024-03-20 stsp if (pledge("stdio proc exec recvfd unveil", NULL) == -1)
2356 ba97b2d7 2024-03-20 stsp err(1, "pledge");
2359 ba97b2d7 2024-03-20 stsp * Limit "exec" promise to notification helpers via unveil(2).
2361 ba97b2d7 2024-03-20 stsp unveil_notification_helpers();
2363 ba97b2d7 2024-03-20 stsp notify_main(title, &gotd.repos, default_sender);
2364 ba97b2d7 2024-03-20 stsp /* NOTREACHED */
2367 13b2bc37 2022-10-23 stsp fatal("invalid process id %d", proc_id);
2370 13b2bc37 2022-10-23 stsp if (proc_id != PROC_GOTD)
2371 13b2bc37 2022-10-23 stsp fatal("invalid process id %d", proc_id);
2373 839338f6 2023-06-22 op evtimer_set(&gotd.listen_proc->tmo, kill_proc_timeout,
2374 839338f6 2023-06-22 op gotd.listen_proc);
2375 ba97b2d7 2024-03-20 stsp if (gotd.notify_proc) {
2376 ba97b2d7 2024-03-20 stsp evtimer_set(&gotd.notify_proc->tmo, kill_proc_timeout,
2377 ba97b2d7 2024-03-20 stsp gotd.notify_proc);
2380 ae7c1b78 2023-01-10 stsp apply_unveil_selfexec();
2382 13b2bc37 2022-10-23 stsp signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
2383 13b2bc37 2022-10-23 stsp signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
2384 13b2bc37 2022-10-23 stsp signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
2385 13b2bc37 2022-10-23 stsp signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
2386 839338f6 2023-06-22 op signal_set(&evsigchld, SIGCHLD, gotd_sighdlr, NULL);
2387 13b2bc37 2022-10-23 stsp signal(SIGPIPE, SIG_IGN);
2389 13b2bc37 2022-10-23 stsp signal_add(&evsigint, NULL);
2390 13b2bc37 2022-10-23 stsp signal_add(&evsigterm, NULL);
2391 13b2bc37 2022-10-23 stsp signal_add(&evsighup, NULL);
2392 13b2bc37 2022-10-23 stsp signal_add(&evsigusr1, NULL);
2393 839338f6 2023-06-22 op signal_add(&evsigchld, NULL);
2395 c929736a 2023-06-22 op gotd_imsg_event_add(&gotd.listen_proc->iev);
2396 5fb267cb 2024-09-08 op if (gotd.notify_proc) {
2397 5fb267cb 2024-09-08 op struct imsgbuf *imsgbuf = &gotd.notify_proc->iev.ibuf;
2398 5fb267cb 2024-09-08 op struct gotd_secret *s;
2399 5fb267cb 2024-09-08 op size_t i, n = 0;
2401 ba97b2d7 2024-03-20 stsp gotd_imsg_event_add(&gotd.notify_proc->iev);
2403 5fb267cb 2024-09-08 op if (gotd.secrets)
2404 5fb267cb 2024-09-08 op n = gotd.secrets->len;
2406 5fb267cb 2024-09-08 op if (imsg_compose(imsgbuf, GOTD_IMSG_SECRETS, 0, 0, -1,
2407 5fb267cb 2024-09-08 op &n, sizeof(n)) == -1)
2408 5fb267cb 2024-09-08 op fatal("imsg_compose GOTD_IMSG_SECRETS");
2409 5fb267cb 2024-09-08 op if (imsg_flush(imsgbuf))
2410 5fb267cb 2024-09-08 op fatal("imsg_flush");
2412 5fb267cb 2024-09-08 op for (i = 0; i < n; ++i) {
2413 5fb267cb 2024-09-08 op struct iovec iov[5];
2415 5fb267cb 2024-09-08 op s = &gotd.secrets->secrets[i];
2417 5fb267cb 2024-09-08 op iov[0].iov_base = &s->type;
2418 5fb267cb 2024-09-08 op iov[0].iov_len = sizeof(s->type);
2420 6e1b28b6 2024-09-09 op iov[1].iov_base = s->label;
2421 6e1b28b6 2024-09-09 op iov[1].iov_len = strlen(s->label) + 1;
2423 6e1b28b6 2024-09-09 op iov[2].iov_base = s->user;
2424 6e1b28b6 2024-09-09 op iov[2].iov_len = s->user ? strlen(s->user) + 1 : 0 ;
2426 6e1b28b6 2024-09-09 op iov[3].iov_base = s->pass;
2427 6e1b28b6 2024-09-09 op iov[3].iov_len = s->pass ? strlen(s->pass) + 1 : 0 ;
2429 6e1b28b6 2024-09-09 op iov[4].iov_base = s->hmac;
2430 6e1b28b6 2024-09-09 op iov[4].iov_len = s->hmac ? strlen(s->hmac) + 1 : 0 ;
2432 5fb267cb 2024-09-08 op if (imsg_composev(imsgbuf, GOTD_IMSG_SECRET,
2433 5fb267cb 2024-09-08 op 0, 0, -1, iov, 5) == -1)
2434 5fb267cb 2024-09-08 op fatal("imsg_composev GOTD_IMSG_SECRET");
2435 5fb267cb 2024-09-08 op if (imsg_flush(imsgbuf))
2436 5fb267cb 2024-09-08 op fatal("imsg_flush");
2439 5fb267cb 2024-09-08 op gotd_secrets_free(gotd.secrets);
2440 5fb267cb 2024-09-08 op gotd.secrets = NULL;
2443 13b2bc37 2022-10-23 stsp event_dispatch();
2445 13b2bc37 2022-10-23 stsp free(repo_path);
2446 f7520f7a 2024-09-08 stsp free(secretspath);
2447 ba97b2d7 2024-03-20 stsp free(default_sender);
2448 ae7c1b78 2023-01-10 stsp gotd_shutdown();