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 ae7c1b78 2023-01-10 stsp #include "session.h"
66 13b2bc37 2022-10-23 stsp #include "repo_read.h"
67 13b2bc37 2022-10-23 stsp #include "repo_write.h"
68 ba97b2d7 2024-03-20 stsp #include "notify.h"
70 13b2bc37 2022-10-23 stsp #ifndef nitems
71 13b2bc37 2022-10-23 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
74 eac23c30 2023-01-10 stsp enum gotd_client_state {
75 eac23c30 2023-01-10 stsp GOTD_CLIENT_STATE_NEW,
76 eac23c30 2023-01-10 stsp GOTD_CLIENT_STATE_ACCESS_GRANTED,
79 c929736a 2023-06-22 op struct gotd_child_proc {
81 c929736a 2023-06-22 op enum gotd_procid type;
82 c929736a 2023-06-22 op char repo_name[NAME_MAX];
83 c929736a 2023-06-22 op char repo_path[PATH_MAX];
85 c929736a 2023-06-22 op struct gotd_imsgev iev;
86 839338f6 2023-06-22 op struct event tmo;
88 839338f6 2023-06-22 op TAILQ_ENTRY(gotd_child_proc) entry;
90 839338f6 2023-06-22 op TAILQ_HEAD(gotd_procs, gotd_child_proc) procs;
92 13b2bc37 2022-10-23 stsp struct gotd_client {
93 13b2bc37 2022-10-23 stsp STAILQ_ENTRY(gotd_client) entry;
94 13b2bc37 2022-10-23 stsp enum gotd_client_state state;
95 13b2bc37 2022-10-23 stsp uint32_t id;
97 13b2bc37 2022-10-23 stsp struct gotd_imsgev iev;
98 13b2bc37 2022-10-23 stsp struct event tmo;
100 13b2bc37 2022-10-23 stsp gid_t egid;
101 56624d2b 2023-12-27 stsp char *username;
102 f7a854cf 2023-01-10 stsp struct gotd_child_proc *repo;
103 5e25db14 2022-12-29 stsp struct gotd_child_proc *auth;
104 ae7c1b78 2023-01-10 stsp struct gotd_child_proc *session;
105 5e25db14 2022-12-29 stsp int required_auth;
107 13b2bc37 2022-10-23 stsp STAILQ_HEAD(gotd_clients, gotd_client);
109 13b2bc37 2022-10-23 stsp static struct gotd_clients gotd_clients[GOTD_CLIENT_TABLE_SIZE];
110 13b2bc37 2022-10-23 stsp static SIPHASH_KEY clients_hash_key;
111 13b2bc37 2022-10-23 stsp volatile int client_cnt;
112 ef4e2f01 2022-12-29 stsp static struct timeval auth_timeout = { 5, 0 };
113 13b2bc37 2022-10-23 stsp static struct gotd gotd;
115 13b2bc37 2022-10-23 stsp void gotd_sighdlr(int sig, short event, void *arg);
116 f1752522 2022-10-29 stsp static void gotd_shutdown(void);
117 ae7c1b78 2023-01-10 stsp static const struct got_error *start_session_child(struct gotd_client *,
118 ae7c1b78 2023-01-10 stsp struct gotd_repo *, char *, const char *, int, int);
119 b50a2b46 2022-12-29 stsp static const struct got_error *start_repo_child(struct gotd_client *,
120 b50a2b46 2022-12-29 stsp enum gotd_procid, struct gotd_repo *, char *, const char *, int, int);
121 5e25db14 2022-12-29 stsp static const struct got_error *start_auth_child(struct gotd_client *, int,
122 5e25db14 2022-12-29 stsp struct gotd_repo *, char *, const char *, int, int);
123 b50a2b46 2022-12-29 stsp static void kill_proc(struct gotd_child_proc *, int);
124 839338f6 2023-06-22 op static void disconnect(struct gotd_client *);
126 13b2bc37 2022-10-23 stsp __dead static void
129 e9e01966 2023-01-18 stsp fprintf(stderr, "usage: %s [-dnv] [-f config-file]\n", getprogname());
134 13b2bc37 2022-10-23 stsp unix_socket_listen(const char *unix_socket_path, uid_t uid, gid_t gid)
136 13b2bc37 2022-10-23 stsp struct sockaddr_un sun;
137 13b2bc37 2022-10-23 stsp int fd = -1;
138 13b2bc37 2022-10-23 stsp mode_t old_umask, mode;
140 13b2bc37 2022-10-23 stsp fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
141 13b2bc37 2022-10-23 stsp if (fd == -1) {
142 13b2bc37 2022-10-23 stsp log_warn("socket");
146 13b2bc37 2022-10-23 stsp sun.sun_family = AF_UNIX;
147 13b2bc37 2022-10-23 stsp if (strlcpy(sun.sun_path, unix_socket_path,
148 13b2bc37 2022-10-23 stsp sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
149 13b2bc37 2022-10-23 stsp log_warnx("%s: name too long", unix_socket_path);
154 13b2bc37 2022-10-23 stsp if (unlink(unix_socket_path) == -1) {
155 13b2bc37 2022-10-23 stsp if (errno != ENOENT) {
156 13b2bc37 2022-10-23 stsp log_warn("unlink %s", unix_socket_path);
162 13b2bc37 2022-10-23 stsp old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
163 6f854dde 2023-01-04 stsp mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
165 13b2bc37 2022-10-23 stsp if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
166 13b2bc37 2022-10-23 stsp log_warn("bind: %s", unix_socket_path);
168 13b2bc37 2022-10-23 stsp umask(old_umask);
172 13b2bc37 2022-10-23 stsp umask(old_umask);
174 13b2bc37 2022-10-23 stsp if (chmod(unix_socket_path, mode) == -1) {
175 13b2bc37 2022-10-23 stsp log_warn("chmod %o %s", mode, unix_socket_path);
177 13b2bc37 2022-10-23 stsp unlink(unix_socket_path);
181 13b2bc37 2022-10-23 stsp if (chown(unix_socket_path, uid, gid) == -1) {
182 13b2bc37 2022-10-23 stsp log_warn("chown %s uid=%d gid=%d", unix_socket_path, uid, gid);
184 13b2bc37 2022-10-23 stsp unlink(unix_socket_path);
188 13b2bc37 2022-10-23 stsp if (listen(fd, GOTD_UNIX_SOCKET_BACKLOG) == -1) {
189 13b2bc37 2022-10-23 stsp log_warn("listen");
191 13b2bc37 2022-10-23 stsp unlink(unix_socket_path);
198 13b2bc37 2022-10-23 stsp static uint64_t
199 13b2bc37 2022-10-23 stsp client_hash(uint32_t client_id)
201 13b2bc37 2022-10-23 stsp return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
204 13b2bc37 2022-10-23 stsp static void
205 13b2bc37 2022-10-23 stsp add_client(struct gotd_client *client)
207 13b2bc37 2022-10-23 stsp uint64_t slot = client_hash(client->id) % nitems(gotd_clients);
208 13b2bc37 2022-10-23 stsp STAILQ_INSERT_HEAD(&gotd_clients[slot], client, entry);
209 13b2bc37 2022-10-23 stsp client_cnt++;
212 13b2bc37 2022-10-23 stsp static struct gotd_client *
213 13b2bc37 2022-10-23 stsp find_client(uint32_t client_id)
215 13b2bc37 2022-10-23 stsp uint64_t slot;
216 13b2bc37 2022-10-23 stsp struct gotd_client *c;
218 13b2bc37 2022-10-23 stsp slot = client_hash(client_id) % nitems(gotd_clients);
219 13b2bc37 2022-10-23 stsp STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
220 13b2bc37 2022-10-23 stsp if (c->id == client_id)
224 13b2bc37 2022-10-23 stsp return NULL;
227 b50a2b46 2022-12-29 stsp static struct gotd_client *
228 b50a2b46 2022-12-29 stsp find_client_by_proc_fd(int fd)
230 b50a2b46 2022-12-29 stsp uint64_t slot;
232 b50a2b46 2022-12-29 stsp for (slot = 0; slot < nitems(gotd_clients); slot++) {
233 b50a2b46 2022-12-29 stsp struct gotd_client *c;
235 b50a2b46 2022-12-29 stsp STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
236 f7a854cf 2023-01-10 stsp if (c->repo && c->repo->iev.ibuf.fd == fd)
238 5e25db14 2022-12-29 stsp if (c->auth && c->auth->iev.ibuf.fd == fd)
240 ae7c1b78 2023-01-10 stsp if (c->session && c->session->iev.ibuf.fd == fd)
245 13b2bc37 2022-10-23 stsp return NULL;
249 13b2bc37 2022-10-23 stsp client_is_reading(struct gotd_client *client)
251 f7a854cf 2023-01-10 stsp return (client->required_auth &
252 f7a854cf 2023-01-10 stsp (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) == GOTD_AUTH_READ;
256 13b2bc37 2022-10-23 stsp client_is_writing(struct gotd_client *client)
258 f7a854cf 2023-01-10 stsp return (client->required_auth &
259 f7a854cf 2023-01-10 stsp (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) ==
260 f7a854cf 2023-01-10 stsp (GOTD_AUTH_READ | GOTD_AUTH_WRITE);
263 13b2bc37 2022-10-23 stsp static const struct got_error *
264 13b2bc37 2022-10-23 stsp ensure_client_is_not_writing(struct gotd_client *client)
266 13b2bc37 2022-10-23 stsp if (client_is_writing(client)) {
267 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_BAD_PACKET,
268 13b2bc37 2022-10-23 stsp "uid %d made a read-request but is writing to "
269 13b2bc37 2022-10-23 stsp "a repository", client->euid);
272 13b2bc37 2022-10-23 stsp return NULL;
275 13b2bc37 2022-10-23 stsp static const struct got_error *
276 13b2bc37 2022-10-23 stsp ensure_client_is_not_reading(struct gotd_client *client)
278 13b2bc37 2022-10-23 stsp if (client_is_reading(client)) {
279 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_BAD_PACKET,
280 13b2bc37 2022-10-23 stsp "uid %d made a write-request but is reading from "
281 13b2bc37 2022-10-23 stsp "a repository", client->euid);
284 13b2bc37 2022-10-23 stsp return NULL;
287 b50a2b46 2022-12-29 stsp static void
288 839338f6 2023-06-22 op proc_done(struct gotd_child_proc *proc)
290 839338f6 2023-06-22 op struct gotd_client *client;
292 839338f6 2023-06-22 op TAILQ_REMOVE(&procs, proc, entry);
294 839338f6 2023-06-22 op client = find_client_by_proc_fd(proc->iev.ibuf.fd);
295 839338f6 2023-06-22 op if (client != NULL) {
296 839338f6 2023-06-22 op if (proc == client->repo)
297 839338f6 2023-06-22 op client->repo = NULL;
298 839338f6 2023-06-22 op if (proc == client->auth)
299 839338f6 2023-06-22 op client->auth = NULL;
300 839338f6 2023-06-22 op if (proc == client->session)
301 839338f6 2023-06-22 op client->session = NULL;
302 839338f6 2023-06-22 op disconnect(client);
305 ba97b2d7 2024-03-20 stsp if (proc == gotd.notify_proc)
306 ba97b2d7 2024-03-20 stsp gotd.notify_proc = NULL;
308 839338f6 2023-06-22 op evtimer_del(&proc->tmo);
310 839338f6 2023-06-22 op if (proc->iev.ibuf.fd != -1) {
311 839338f6 2023-06-22 op event_del(&proc->iev.ev);
312 839338f6 2023-06-22 op msgbuf_clear(&proc->iev.ibuf.w);
313 839338f6 2023-06-22 op close(proc->iev.ibuf.fd);
316 ae7c1b78 2023-01-10 stsp free(proc);
320 ba91039c 2023-06-22 op kill_repo_proc(struct gotd_client *client)
322 ba91039c 2023-06-22 op if (client->repo == NULL)
325 839338f6 2023-06-22 op kill_proc(client->repo, 0);
326 ba91039c 2023-06-22 op client->repo = NULL;
329 13b2bc37 2022-10-23 stsp static void
330 5e25db14 2022-12-29 stsp kill_auth_proc(struct gotd_client *client)
332 5e25db14 2022-12-29 stsp if (client->auth == NULL)
335 839338f6 2023-06-22 op kill_proc(client->auth, 0);
336 5e25db14 2022-12-29 stsp client->auth = NULL;
339 5e25db14 2022-12-29 stsp static void
340 ae7c1b78 2023-01-10 stsp kill_session_proc(struct gotd_client *client)
342 ae7c1b78 2023-01-10 stsp if (client->session == NULL)
345 839338f6 2023-06-22 op kill_proc(client->session, 0);
346 ae7c1b78 2023-01-10 stsp client->session = NULL;
349 ae7c1b78 2023-01-10 stsp static void
350 13b2bc37 2022-10-23 stsp disconnect(struct gotd_client *client)
352 13b2bc37 2022-10-23 stsp struct gotd_imsg_disconnect idisconnect;
353 c929736a 2023-06-22 op struct gotd_child_proc *listen_proc = gotd.listen_proc;
354 13b2bc37 2022-10-23 stsp uint64_t slot;
356 13b2bc37 2022-10-23 stsp log_debug("uid %d: disconnecting", client->euid);
358 5e25db14 2022-12-29 stsp kill_auth_proc(client);
359 ae7c1b78 2023-01-10 stsp kill_session_proc(client);
360 ba91039c 2023-06-22 op kill_repo_proc(client);
362 90270f79 2023-02-09 stsp idisconnect.client_id = client->id;
363 d93ecf7d 2022-12-14 stsp if (gotd_imsg_compose_event(&listen_proc->iev,
364 d93ecf7d 2022-12-14 stsp GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
365 d93ecf7d 2022-12-14 stsp &idisconnect, sizeof(idisconnect)) == -1)
366 d93ecf7d 2022-12-14 stsp log_warn("imsg compose DISCONNECT");
368 13b2bc37 2022-10-23 stsp slot = client_hash(client->id) % nitems(gotd_clients);
369 13b2bc37 2022-10-23 stsp STAILQ_REMOVE(&gotd_clients[slot], client, gotd_client, entry);
370 13b2bc37 2022-10-23 stsp imsg_clear(&client->iev.ibuf);
371 13b2bc37 2022-10-23 stsp event_del(&client->iev.ev);
372 13b2bc37 2022-10-23 stsp evtimer_del(&client->tmo);
373 ae7c1b78 2023-01-10 stsp if (client->fd != -1)
374 ae7c1b78 2023-01-10 stsp close(client->fd);
375 ae7c1b78 2023-01-10 stsp else if (client->iev.ibuf.fd != -1)
376 ae7c1b78 2023-01-10 stsp close(client->iev.ibuf.fd);
377 56624d2b 2023-12-27 stsp free(client->username);
378 13b2bc37 2022-10-23 stsp free(client);
379 13b2bc37 2022-10-23 stsp client_cnt--;
382 13b2bc37 2022-10-23 stsp static void
383 13b2bc37 2022-10-23 stsp disconnect_on_error(struct gotd_client *client, const struct got_error *err)
385 13b2bc37 2022-10-23 stsp struct imsgbuf ibuf;
387 1050403b 2023-08-11 stsp if (err->code != GOT_ERR_EOF) {
388 1050403b 2023-08-11 stsp log_warnx("uid %d: %s", client->euid, err->msg);
389 1050403b 2023-08-11 stsp if (client->fd != -1) {
390 1050403b 2023-08-11 stsp imsg_init(&ibuf, client->fd);
391 1050403b 2023-08-11 stsp gotd_imsg_send_error(&ibuf, 0, PROC_GOTD, err);
392 1050403b 2023-08-11 stsp imsg_clear(&ibuf);
395 13b2bc37 2022-10-23 stsp disconnect(client);
398 f1752522 2022-10-29 stsp static const struct got_error *
399 f1752522 2022-10-29 stsp send_repo_info(struct gotd_imsgev *iev, struct gotd_repo *repo)
401 f1752522 2022-10-29 stsp const struct got_error *err = NULL;
402 f1752522 2022-10-29 stsp struct gotd_imsg_info_repo irepo;
404 f1752522 2022-10-29 stsp memset(&irepo, 0, sizeof(irepo));
406 f1752522 2022-10-29 stsp if (strlcpy(irepo.repo_name, repo->name, sizeof(irepo.repo_name))
407 f1752522 2022-10-29 stsp >= sizeof(irepo.repo_name))
408 f1752522 2022-10-29 stsp return got_error_msg(GOT_ERR_NO_SPACE, "repo name too long");
409 f1752522 2022-10-29 stsp if (strlcpy(irepo.repo_path, repo->path, sizeof(irepo.repo_path))
410 f1752522 2022-10-29 stsp >= sizeof(irepo.repo_path))
411 f1752522 2022-10-29 stsp return got_error_msg(GOT_ERR_NO_SPACE, "repo path too long");
413 f1752522 2022-10-29 stsp if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_REPO, PROC_GOTD, -1,
414 f1752522 2022-10-29 stsp &irepo, sizeof(irepo)) == -1) {
415 f1752522 2022-10-29 stsp err = got_error_from_errno("imsg compose INFO_REPO");
417 f1752522 2022-10-29 stsp return err;
420 f1752522 2022-10-29 stsp return NULL;
423 f1752522 2022-10-29 stsp static const struct got_error *
424 f1752522 2022-10-29 stsp send_client_info(struct gotd_imsgev *iev, struct gotd_client *client)
426 f1752522 2022-10-29 stsp const struct got_error *err = NULL;
427 f1752522 2022-10-29 stsp struct gotd_imsg_info_client iclient;
428 f1752522 2022-10-29 stsp struct gotd_child_proc *proc;
430 f1752522 2022-10-29 stsp memset(&iclient, 0, sizeof(iclient));
431 f1752522 2022-10-29 stsp iclient.euid = client->euid;
432 f1752522 2022-10-29 stsp iclient.egid = client->egid;
434 f7a854cf 2023-01-10 stsp proc = client->repo;
435 f1752522 2022-10-29 stsp if (proc) {
436 eec68231 2022-12-14 stsp if (strlcpy(iclient.repo_name, proc->repo_path,
437 f1752522 2022-10-29 stsp sizeof(iclient.repo_name)) >= sizeof(iclient.repo_name)) {
438 f1752522 2022-10-29 stsp return got_error_msg(GOT_ERR_NO_SPACE,
439 f1752522 2022-10-29 stsp "repo name too long");
441 f1752522 2022-10-29 stsp if (client_is_writing(client))
442 f1752522 2022-10-29 stsp iclient.is_writing = 1;
444 ae7c1b78 2023-01-10 stsp iclient.repo_child_pid = proc->pid;
447 ae7c1b78 2023-01-10 stsp if (client->session)
448 ae7c1b78 2023-01-10 stsp iclient.session_child_pid = client->session->pid;
450 f1752522 2022-10-29 stsp if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_CLIENT, PROC_GOTD, -1,
451 f1752522 2022-10-29 stsp &iclient, sizeof(iclient)) == -1) {
452 f1752522 2022-10-29 stsp err = got_error_from_errno("imsg compose INFO_CLIENT");
454 f1752522 2022-10-29 stsp return err;
457 f1752522 2022-10-29 stsp return NULL;
460 f1752522 2022-10-29 stsp static const struct got_error *
461 f1752522 2022-10-29 stsp send_info(struct gotd_client *client)
463 f1752522 2022-10-29 stsp const struct got_error *err = NULL;
464 f1752522 2022-10-29 stsp struct gotd_imsg_info info;
465 f1752522 2022-10-29 stsp uint64_t slot;
466 f1752522 2022-10-29 stsp struct gotd_repo *repo;
468 78433331 2023-01-04 stsp if (client->euid != 0)
469 78433331 2023-01-04 stsp return got_error_set_errno(EPERM, "info");
471 f1752522 2022-10-29 stsp info.pid = gotd.pid;
472 f1752522 2022-10-29 stsp info.verbosity = gotd.verbosity;
473 f1752522 2022-10-29 stsp info.nrepos = gotd.nrepos;
474 f1752522 2022-10-29 stsp info.nclients = client_cnt - 1;
476 f1752522 2022-10-29 stsp if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_INFO, PROC_GOTD, -1,
477 f1752522 2022-10-29 stsp &info, sizeof(info)) == -1) {
478 f1752522 2022-10-29 stsp err = got_error_from_errno("imsg compose INFO");
480 f1752522 2022-10-29 stsp return err;
483 f1752522 2022-10-29 stsp TAILQ_FOREACH(repo, &gotd.repos, entry) {
484 f1752522 2022-10-29 stsp err = send_repo_info(&client->iev, repo);
486 f1752522 2022-10-29 stsp return err;
489 f1752522 2022-10-29 stsp for (slot = 0; slot < nitems(gotd_clients); slot++) {
490 f1752522 2022-10-29 stsp struct gotd_client *c;
491 f1752522 2022-10-29 stsp STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
492 f1752522 2022-10-29 stsp if (c->id == client->id)
494 f1752522 2022-10-29 stsp err = send_client_info(&client->iev, c);
496 f1752522 2022-10-29 stsp return err;
500 f1752522 2022-10-29 stsp return NULL;
503 f1752522 2022-10-29 stsp static const struct got_error *
504 f1752522 2022-10-29 stsp stop_gotd(struct gotd_client *client)
507 f1752522 2022-10-29 stsp if (client->euid != 0)
508 f1752522 2022-10-29 stsp return got_error_set_errno(EPERM, "stop");
510 f1752522 2022-10-29 stsp gotd_shutdown();
511 f1752522 2022-10-29 stsp /* NOTREACHED */
512 0ccf3acb 2022-11-16 stsp return NULL;
515 13b2bc37 2022-10-23 stsp static const struct got_error *
516 ae7c1b78 2023-01-10 stsp start_client_authentication(struct gotd_client *client, struct imsg *imsg)
518 13b2bc37 2022-10-23 stsp const struct got_error *err;
519 13b2bc37 2022-10-23 stsp struct gotd_imsg_list_refs ireq;
520 0ccf3acb 2022-11-16 stsp struct gotd_repo *repo = NULL;
521 13b2bc37 2022-10-23 stsp size_t datalen;
523 13b2bc37 2022-10-23 stsp log_debug("list-refs request from uid %d", client->euid);
525 eac23c30 2023-01-10 stsp if (client->state != GOTD_CLIENT_STATE_NEW)
526 ae7c1b78 2023-01-10 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
527 ae7c1b78 2023-01-10 stsp "unexpected list-refs request received");
529 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
530 13b2bc37 2022-10-23 stsp if (datalen != sizeof(ireq))
531 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
533 13b2bc37 2022-10-23 stsp memcpy(&ireq, imsg->data, datalen);
535 13b2bc37 2022-10-23 stsp if (ireq.client_is_reading) {
536 13b2bc37 2022-10-23 stsp err = ensure_client_is_not_writing(client);
538 13b2bc37 2022-10-23 stsp return err;
539 ba97b2d7 2024-03-20 stsp repo = gotd_find_repo_by_name(ireq.repo_name, &gotd.repos);
540 0ccf3acb 2022-11-16 stsp if (repo == NULL)
541 0ccf3acb 2022-11-16 stsp return got_error(GOT_ERR_NOT_GIT_REPO);
542 5e25db14 2022-12-29 stsp err = start_auth_child(client, GOTD_AUTH_READ, repo,
543 b50a2b46 2022-12-29 stsp gotd.argv0, gotd.confpath, gotd.daemonize,
544 b50a2b46 2022-12-29 stsp gotd.verbosity);
546 b50a2b46 2022-12-29 stsp return err;
548 13b2bc37 2022-10-23 stsp err = ensure_client_is_not_reading(client);
550 0ccf3acb 2022-11-16 stsp return err;
551 ba97b2d7 2024-03-20 stsp repo = gotd_find_repo_by_name(ireq.repo_name, &gotd.repos);
552 0ccf3acb 2022-11-16 stsp if (repo == NULL)
553 0ccf3acb 2022-11-16 stsp return got_error(GOT_ERR_NOT_GIT_REPO);
554 5e25db14 2022-12-29 stsp err = start_auth_child(client,
555 5e25db14 2022-12-29 stsp GOTD_AUTH_READ | GOTD_AUTH_WRITE,
556 5e25db14 2022-12-29 stsp repo, gotd.argv0, gotd.confpath, gotd.daemonize,
557 b50a2b46 2022-12-29 stsp gotd.verbosity);
559 b50a2b46 2022-12-29 stsp return err;
562 ae7c1b78 2023-01-10 stsp evtimer_add(&client->tmo, &auth_timeout);
564 5fb50fce 2023-07-14 stsp /* Flow continues upon authentication success/failure or timeout. */
565 13b2bc37 2022-10-23 stsp return NULL;
568 13b2bc37 2022-10-23 stsp static void
569 13b2bc37 2022-10-23 stsp gotd_request(int fd, short events, void *arg)
571 13b2bc37 2022-10-23 stsp struct gotd_imsgev *iev = arg;
572 13b2bc37 2022-10-23 stsp struct imsgbuf *ibuf = &iev->ibuf;
573 13b2bc37 2022-10-23 stsp struct gotd_client *client = iev->handler_arg;
574 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
575 13b2bc37 2022-10-23 stsp struct imsg imsg;
578 13b2bc37 2022-10-23 stsp if (events & EV_WRITE) {
579 13b2bc37 2022-10-23 stsp while (ibuf->w.queued) {
580 13b2bc37 2022-10-23 stsp n = msgbuf_write(&ibuf->w);
581 13b2bc37 2022-10-23 stsp if (n == -1 && errno == EPIPE) {
583 13b2bc37 2022-10-23 stsp * The client has closed its socket.
584 13b2bc37 2022-10-23 stsp * This can happen when Git clients are
585 13b2bc37 2022-10-23 stsp * done sending pack file data.
587 13b2bc37 2022-10-23 stsp msgbuf_clear(&ibuf->w);
589 13b2bc37 2022-10-23 stsp } else if (n == -1 && errno != EAGAIN) {
590 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_flush");
591 13b2bc37 2022-10-23 stsp disconnect_on_error(client, err);
594 13b2bc37 2022-10-23 stsp if (n == 0) {
595 13b2bc37 2022-10-23 stsp /* Connection closed. */
596 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_EOF);
597 13b2bc37 2022-10-23 stsp disconnect_on_error(client, err);
602 f1752522 2022-10-29 stsp /* Disconnect gotctl(8) now that messages have been sent. */
603 f1752522 2022-10-29 stsp if (!client_is_reading(client) && !client_is_writing(client)) {
604 f1752522 2022-10-29 stsp disconnect(client);
609 13b2bc37 2022-10-23 stsp if ((events & EV_READ) == 0)
612 13b2bc37 2022-10-23 stsp memset(&imsg, 0, sizeof(imsg));
614 13b2bc37 2022-10-23 stsp while (err == NULL) {
615 13b2bc37 2022-10-23 stsp err = gotd_imsg_recv(&imsg, ibuf, 0);
617 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_PRIVSEP_READ)
618 13b2bc37 2022-10-23 stsp err = NULL;
622 13b2bc37 2022-10-23 stsp evtimer_del(&client->tmo);
624 13b2bc37 2022-10-23 stsp switch (imsg.hdr.type) {
625 f1752522 2022-10-29 stsp case GOTD_IMSG_INFO:
626 f1752522 2022-10-29 stsp err = send_info(client);
628 f1752522 2022-10-29 stsp case GOTD_IMSG_STOP:
629 f1752522 2022-10-29 stsp err = stop_gotd(client);
631 13b2bc37 2022-10-23 stsp case GOTD_IMSG_LIST_REFS:
632 ae7c1b78 2023-01-10 stsp err = start_client_authentication(client, &imsg);
635 ae7c1b78 2023-01-10 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
636 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
640 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
644 b5225f29 2023-01-22 op disconnect_on_error(client, err);
646 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(&client->iev);
650 13b2bc37 2022-10-23 stsp static void
651 ae7c1b78 2023-01-10 stsp gotd_auth_timeout(int fd, short events, void *arg)
653 13b2bc37 2022-10-23 stsp struct gotd_client *client = arg;
655 ae7c1b78 2023-01-10 stsp log_debug("disconnecting uid %d due to authentication timeout",
656 ae7c1b78 2023-01-10 stsp client->euid);
657 13b2bc37 2022-10-23 stsp disconnect(client);
660 d93ecf7d 2022-12-14 stsp static const struct got_error *
661 d93ecf7d 2022-12-14 stsp recv_connect(uint32_t *client_id, struct imsg *imsg)
663 d93ecf7d 2022-12-14 stsp const struct got_error *err = NULL;
664 d93ecf7d 2022-12-14 stsp struct gotd_imsg_connect iconnect;
665 d93ecf7d 2022-12-14 stsp size_t datalen;
666 13b2bc37 2022-10-23 stsp int s = -1;
667 13b2bc37 2022-10-23 stsp struct gotd_client *client = NULL;
669 d93ecf7d 2022-12-14 stsp *client_id = 0;
671 d93ecf7d 2022-12-14 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
672 d93ecf7d 2022-12-14 stsp if (datalen != sizeof(iconnect))
673 d93ecf7d 2022-12-14 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
674 d93ecf7d 2022-12-14 stsp memcpy(&iconnect, imsg->data, sizeof(iconnect));
676 2c52c623 2024-01-30 op s = imsg_get_fd(imsg);
677 13b2bc37 2022-10-23 stsp if (s == -1) {
678 d93ecf7d 2022-12-14 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
682 d93ecf7d 2022-12-14 stsp if (find_client(iconnect.client_id)) {
683 d93ecf7d 2022-12-14 stsp err = got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
687 13b2bc37 2022-10-23 stsp client = calloc(1, sizeof(*client));
688 13b2bc37 2022-10-23 stsp if (client == NULL) {
689 d93ecf7d 2022-12-14 stsp err = got_error_from_errno("calloc");
693 d93ecf7d 2022-12-14 stsp *client_id = iconnect.client_id;
695 eac23c30 2023-01-10 stsp client->state = GOTD_CLIENT_STATE_NEW;
696 d93ecf7d 2022-12-14 stsp client->id = iconnect.client_id;
697 13b2bc37 2022-10-23 stsp client->fd = s;
699 365cf0f3 2022-12-29 stsp /* The auth process will verify UID/GID for us. */
700 365cf0f3 2022-12-29 stsp client->euid = iconnect.euid;
701 365cf0f3 2022-12-29 stsp client->egid = iconnect.egid;
703 13b2bc37 2022-10-23 stsp imsg_init(&client->iev.ibuf, client->fd);
704 13b2bc37 2022-10-23 stsp client->iev.handler = gotd_request;
705 13b2bc37 2022-10-23 stsp client->iev.events = EV_READ;
706 13b2bc37 2022-10-23 stsp client->iev.handler_arg = client;
708 13b2bc37 2022-10-23 stsp event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
709 13b2bc37 2022-10-23 stsp &client->iev);
710 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(&client->iev);
712 ae7c1b78 2023-01-10 stsp evtimer_set(&client->tmo, gotd_auth_timeout, client);
714 13b2bc37 2022-10-23 stsp add_client(client);
715 13b2bc37 2022-10-23 stsp log_debug("%s: new client uid %d connected on fd %d", __func__,
716 13b2bc37 2022-10-23 stsp client->euid, client->fd);
719 c929736a 2023-06-22 op struct gotd_child_proc *listen_proc = gotd.listen_proc;
720 d93ecf7d 2022-12-14 stsp struct gotd_imsg_disconnect idisconnect;
722 d93ecf7d 2022-12-14 stsp idisconnect.client_id = client->id;
723 d93ecf7d 2022-12-14 stsp if (gotd_imsg_compose_event(&listen_proc->iev,
724 d93ecf7d 2022-12-14 stsp GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
725 d93ecf7d 2022-12-14 stsp &idisconnect, sizeof(idisconnect)) == -1)
726 d93ecf7d 2022-12-14 stsp log_warn("imsg compose DISCONNECT");
728 d93ecf7d 2022-12-14 stsp if (s != -1)
732 d93ecf7d 2022-12-14 stsp return err;
735 13b2bc37 2022-10-23 stsp static const char *gotd_proc_names[PROC_MAX] = {
739 ce986f22 2023-06-19 stsp "session_read",
740 ce986f22 2023-06-19 stsp "session_write",
741 13b2bc37 2022-10-23 stsp "repo_read",
742 4b3827cd 2023-07-08 stsp "repo_write",
743 ba97b2d7 2024-03-20 stsp "gitwrapper",
747 13b2bc37 2022-10-23 stsp static void
748 13b2bc37 2022-10-23 stsp kill_proc(struct gotd_child_proc *proc, int fatal)
750 839338f6 2023-06-22 op struct timeval tv = { 5, 0 };
752 839338f6 2023-06-22 op log_debug("kill -%d %d", fatal ? SIGKILL : SIGTERM, proc->pid);
754 839338f6 2023-06-22 op if (proc->iev.ibuf.fd != -1) {
755 839338f6 2023-06-22 op event_del(&proc->iev.ev);
756 839338f6 2023-06-22 op msgbuf_clear(&proc->iev.ibuf.w);
757 839338f6 2023-06-22 op close(proc->iev.ibuf.fd);
758 839338f6 2023-06-22 op proc->iev.ibuf.fd = -1;
761 839338f6 2023-06-22 op if (!evtimer_pending(&proc->tmo, NULL) && !fatal)
762 839338f6 2023-06-22 op evtimer_add(&proc->tmo, &tv);
764 13b2bc37 2022-10-23 stsp if (fatal) {
765 13b2bc37 2022-10-23 stsp log_warnx("sending SIGKILL to PID %d", proc->pid);
766 13b2bc37 2022-10-23 stsp kill(proc->pid, SIGKILL);
768 13b2bc37 2022-10-23 stsp kill(proc->pid, SIGTERM);
771 13b2bc37 2022-10-23 stsp static void
772 839338f6 2023-06-22 op kill_proc_timeout(int fd, short ev, void *d)
774 839338f6 2023-06-22 op struct gotd_child_proc *proc = d;
776 839338f6 2023-06-22 op log_warnx("timeout waiting for PID %d to terminate;"
777 839338f6 2023-06-22 op " retrying with force", proc->pid);
778 839338f6 2023-06-22 op kill_proc(proc, 1);
782 13b2bc37 2022-10-23 stsp gotd_shutdown(void)
784 b50a2b46 2022-12-29 stsp uint64_t slot;
786 ae7c1b78 2023-01-10 stsp log_debug("shutting down");
787 b50a2b46 2022-12-29 stsp for (slot = 0; slot < nitems(gotd_clients); slot++) {
788 b50a2b46 2022-12-29 stsp struct gotd_client *c, *tmp;
790 b50a2b46 2022-12-29 stsp STAILQ_FOREACH_SAFE(c, &gotd_clients[slot], entry, tmp)
791 b50a2b46 2022-12-29 stsp disconnect(c);
794 839338f6 2023-06-22 op kill_proc(gotd.listen_proc, 0);
796 13b2bc37 2022-10-23 stsp log_info("terminating");
800 839338f6 2023-06-22 op static struct gotd_child_proc *
801 839338f6 2023-06-22 op find_proc_by_pid(pid_t pid)
803 839338f6 2023-06-22 op struct gotd_child_proc *proc = NULL;
805 839338f6 2023-06-22 op TAILQ_FOREACH(proc, &procs, entry)
806 839338f6 2023-06-22 op if (proc->pid == pid)
813 13b2bc37 2022-10-23 stsp gotd_sighdlr(int sig, short event, void *arg)
815 839338f6 2023-06-22 op struct gotd_child_proc *proc;
820 13b2bc37 2022-10-23 stsp * Normal signal handler rules don't apply because libevent
821 13b2bc37 2022-10-23 stsp * decouples for us.
824 13b2bc37 2022-10-23 stsp switch (sig) {
825 13b2bc37 2022-10-23 stsp case SIGHUP:
826 13b2bc37 2022-10-23 stsp log_info("%s: ignoring SIGHUP", __func__);
828 13b2bc37 2022-10-23 stsp case SIGUSR1:
829 13b2bc37 2022-10-23 stsp log_info("%s: ignoring SIGUSR1", __func__);
831 13b2bc37 2022-10-23 stsp case SIGTERM:
832 13b2bc37 2022-10-23 stsp case SIGINT:
833 13b2bc37 2022-10-23 stsp gotd_shutdown();
835 839338f6 2023-06-22 op case SIGCHLD:
837 839338f6 2023-06-22 op pid = waitpid(WAIT_ANY, &status, WNOHANG);
838 839338f6 2023-06-22 op if (pid == -1) {
839 839338f6 2023-06-22 op if (errno == EINTR)
841 839338f6 2023-06-22 op if (errno == ECHILD)
843 839338f6 2023-06-22 op fatal("waitpid");
845 839338f6 2023-06-22 op if (pid == 0)
848 839338f6 2023-06-22 op log_debug("reaped pid %d", pid);
849 839338f6 2023-06-22 op proc = find_proc_by_pid(pid);
850 839338f6 2023-06-22 op if (proc == NULL) {
851 839338f6 2023-06-22 op log_info("caught exit of unknown child %d",
856 839338f6 2023-06-22 op if (WIFSIGNALED(status)) {
857 839338f6 2023-06-22 op log_warnx("child PID %d terminated with"
858 839338f6 2023-06-22 op " signal %d", pid, WTERMSIG(status));
861 839338f6 2023-06-22 op proc_done(proc);
865 13b2bc37 2022-10-23 stsp fatalx("unexpected signal");
869 13b2bc37 2022-10-23 stsp static const struct got_error *
870 13b2bc37 2022-10-23 stsp ensure_proc_is_reading(struct gotd_client *client,
871 13b2bc37 2022-10-23 stsp struct gotd_child_proc *proc)
873 13b2bc37 2022-10-23 stsp if (!client_is_reading(client)) {
874 13b2bc37 2022-10-23 stsp kill_proc(proc, 1);
875 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_BAD_PACKET,
876 13b2bc37 2022-10-23 stsp "PID %d handled a read-request for uid %d but this "
877 13b2bc37 2022-10-23 stsp "user is not reading from a repository", proc->pid,
878 13b2bc37 2022-10-23 stsp client->euid);
881 13b2bc37 2022-10-23 stsp return NULL;
884 13b2bc37 2022-10-23 stsp static const struct got_error *
885 13b2bc37 2022-10-23 stsp ensure_proc_is_writing(struct gotd_client *client,
886 13b2bc37 2022-10-23 stsp struct gotd_child_proc *proc)
888 13b2bc37 2022-10-23 stsp if (!client_is_writing(client)) {
889 13b2bc37 2022-10-23 stsp kill_proc(proc, 1);
890 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_BAD_PACKET,
891 13b2bc37 2022-10-23 stsp "PID %d handled a write-request for uid %d but this "
892 13b2bc37 2022-10-23 stsp "user is not writing to a repository", proc->pid,
893 13b2bc37 2022-10-23 stsp client->euid);
896 13b2bc37 2022-10-23 stsp return NULL;
900 13b2bc37 2022-10-23 stsp verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
901 13b2bc37 2022-10-23 stsp struct imsg *imsg)
903 13b2bc37 2022-10-23 stsp const struct got_error *err;
904 13b2bc37 2022-10-23 stsp int ret = 0;
906 d93ecf7d 2022-12-14 stsp if (proc->type == PROC_REPO_READ || proc->type == PROC_REPO_WRITE) {
907 f7a854cf 2023-01-10 stsp if (client->repo == NULL)
908 d93ecf7d 2022-12-14 stsp fatalx("no process found for uid %d", client->euid);
909 f7a854cf 2023-01-10 stsp if (proc->pid != client->repo->pid) {
910 d93ecf7d 2022-12-14 stsp kill_proc(proc, 1);
911 d93ecf7d 2022-12-14 stsp log_warnx("received message from PID %d for uid %d, "
912 d93ecf7d 2022-12-14 stsp "while PID %d is the process serving this user",
913 f7a854cf 2023-01-10 stsp proc->pid, client->euid, client->repo->pid);
917 b0614828 2023-06-19 stsp if (proc->type == PROC_SESSION_READ ||
918 b0614828 2023-06-19 stsp proc->type == PROC_SESSION_WRITE) {
919 ae7c1b78 2023-01-10 stsp if (client->session == NULL) {
920 ae7c1b78 2023-01-10 stsp log_warnx("no session found for uid %d", client->euid);
923 ae7c1b78 2023-01-10 stsp if (proc->pid != client->session->pid) {
924 ae7c1b78 2023-01-10 stsp kill_proc(proc, 1);
925 ae7c1b78 2023-01-10 stsp log_warnx("received message from PID %d for uid %d, "
926 ae7c1b78 2023-01-10 stsp "while PID %d is the process serving this user",
927 ae7c1b78 2023-01-10 stsp proc->pid, client->euid, client->session->pid);
932 13b2bc37 2022-10-23 stsp switch (imsg->hdr.type) {
933 13b2bc37 2022-10-23 stsp case GOTD_IMSG_ERROR:
936 d93ecf7d 2022-12-14 stsp case GOTD_IMSG_CONNECT:
937 d93ecf7d 2022-12-14 stsp if (proc->type != PROC_LISTEN) {
938 d93ecf7d 2022-12-14 stsp err = got_error_fmt(GOT_ERR_BAD_PACKET,
939 d93ecf7d 2022-12-14 stsp "new connection for uid %d from PID %d "
940 d93ecf7d 2022-12-14 stsp "which is not the listen process",
941 76a9a38e 2023-12-27 stsp client->euid, proc->pid);
945 5e25db14 2022-12-29 stsp case GOTD_IMSG_ACCESS_GRANTED:
946 5e25db14 2022-12-29 stsp if (proc->type != PROC_AUTH) {
947 5e25db14 2022-12-29 stsp err = got_error_fmt(GOT_ERR_BAD_PACKET,
948 5e25db14 2022-12-29 stsp "authentication of uid %d from PID %d "
949 5e25db14 2022-12-29 stsp "which is not the auth process",
950 76a9a38e 2023-12-27 stsp client->euid, proc->pid);
954 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_CLIENT_SESSION_READY:
955 b0614828 2023-06-19 stsp if (proc->type != PROC_SESSION_READ &&
956 b0614828 2023-06-19 stsp proc->type != PROC_SESSION_WRITE) {
957 ae7c1b78 2023-01-10 stsp err = got_error_fmt(GOT_ERR_BAD_PACKET,
958 ae7c1b78 2023-01-10 stsp "unexpected \"ready\" signal from PID %d",
959 ae7c1b78 2023-01-10 stsp proc->pid);
963 b50a2b46 2022-12-29 stsp case GOTD_IMSG_REPO_CHILD_READY:
964 b50a2b46 2022-12-29 stsp if (proc->type != PROC_REPO_READ &&
965 b50a2b46 2022-12-29 stsp proc->type != PROC_REPO_WRITE) {
966 b50a2b46 2022-12-29 stsp err = got_error_fmt(GOT_ERR_BAD_PACKET,
967 b50a2b46 2022-12-29 stsp "unexpected \"ready\" signal from PID %d",
968 b50a2b46 2022-12-29 stsp proc->pid);
972 13b2bc37 2022-10-23 stsp case GOTD_IMSG_PACKFILE_DONE:
973 13b2bc37 2022-10-23 stsp err = ensure_proc_is_reading(client, proc);
975 13b2bc37 2022-10-23 stsp log_warnx("uid %d: %s", client->euid, err->msg);
979 13b2bc37 2022-10-23 stsp case GOTD_IMSG_PACKFILE_INSTALL:
980 13b2bc37 2022-10-23 stsp case GOTD_IMSG_REF_UPDATES_START:
981 13b2bc37 2022-10-23 stsp case GOTD_IMSG_REF_UPDATE:
982 13b2bc37 2022-10-23 stsp err = ensure_proc_is_writing(client, proc);
984 13b2bc37 2022-10-23 stsp log_warnx("uid %d: %s", client->euid, err->msg);
989 13b2bc37 2022-10-23 stsp log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
993 13b2bc37 2022-10-23 stsp return ret;
996 13b2bc37 2022-10-23 stsp static const struct got_error *
997 ae7c1b78 2023-01-10 stsp connect_repo_child(struct gotd_client *client,
998 ae7c1b78 2023-01-10 stsp struct gotd_child_proc *repo_proc)
1000 b50a2b46 2022-12-29 stsp static const struct got_error *err;
1001 ae7c1b78 2023-01-10 stsp struct gotd_imsgev *session_iev = &client->session->iev;
1002 ae7c1b78 2023-01-10 stsp struct gotd_imsg_connect_repo_child ireq;
1003 ae7c1b78 2023-01-10 stsp int pipe[2];
1005 eac23c30 2023-01-10 stsp if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED)
1006 ae7c1b78 2023-01-10 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
1007 ae7c1b78 2023-01-10 stsp "unexpected repo child ready signal received");
1009 ae7c1b78 2023-01-10 stsp if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1010 ae7c1b78 2023-01-10 stsp PF_UNSPEC, pipe) == -1)
1011 ae7c1b78 2023-01-10 stsp fatal("socketpair");
1013 ae7c1b78 2023-01-10 stsp memset(&ireq, 0, sizeof(ireq));
1014 ae7c1b78 2023-01-10 stsp ireq.proc_id = repo_proc->type;
1016 ae7c1b78 2023-01-10 stsp /* Pass repo child pipe to session child process. */
1017 ae7c1b78 2023-01-10 stsp if (gotd_imsg_compose_event(session_iev, GOTD_IMSG_CONNECT_REPO_CHILD,
1018 ae7c1b78 2023-01-10 stsp PROC_GOTD, pipe[0], &ireq, sizeof(ireq)) == -1) {
1019 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
1020 ae7c1b78 2023-01-10 stsp close(pipe[0]);
1021 ae7c1b78 2023-01-10 stsp close(pipe[1]);
1022 ae7c1b78 2023-01-10 stsp return err;
1025 ae7c1b78 2023-01-10 stsp /* Pass session child pipe to repo child process. */
1026 ae7c1b78 2023-01-10 stsp if (gotd_imsg_compose_event(&repo_proc->iev,
1027 ae7c1b78 2023-01-10 stsp GOTD_IMSG_CONNECT_REPO_CHILD, PROC_GOTD, pipe[1], NULL, 0) == -1) {
1028 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
1029 ae7c1b78 2023-01-10 stsp close(pipe[1]);
1030 ae7c1b78 2023-01-10 stsp return err;
1033 13b2bc37 2022-10-23 stsp return NULL;
1036 13b2bc37 2022-10-23 stsp static void
1037 b50a2b46 2022-12-29 stsp gotd_dispatch_listener(int fd, short event, void *arg)
1039 13b2bc37 2022-10-23 stsp struct gotd_imsgev *iev = arg;
1040 13b2bc37 2022-10-23 stsp struct imsgbuf *ibuf = &iev->ibuf;
1041 c929736a 2023-06-22 op struct gotd_child_proc *proc = gotd.listen_proc;
1042 b50a2b46 2022-12-29 stsp ssize_t n;
1043 b50a2b46 2022-12-29 stsp int shut = 0;
1044 b50a2b46 2022-12-29 stsp struct imsg imsg;
1046 b50a2b46 2022-12-29 stsp if (proc->iev.ibuf.fd != fd)
1047 b50a2b46 2022-12-29 stsp fatalx("%s: unexpected fd %d", __func__, fd);
1049 b50a2b46 2022-12-29 stsp if (event & EV_READ) {
1050 b50a2b46 2022-12-29 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1051 b50a2b46 2022-12-29 stsp fatal("imsg_read error");
1052 b50a2b46 2022-12-29 stsp if (n == 0) {
1053 b50a2b46 2022-12-29 stsp /* Connection closed. */
1055 b50a2b46 2022-12-29 stsp goto done;
1059 b50a2b46 2022-12-29 stsp if (event & EV_WRITE) {
1060 b50a2b46 2022-12-29 stsp n = msgbuf_write(&ibuf->w);
1061 b50a2b46 2022-12-29 stsp if (n == -1 && errno != EAGAIN)
1062 b50a2b46 2022-12-29 stsp fatal("msgbuf_write");
1063 b50a2b46 2022-12-29 stsp if (n == 0) {
1064 b50a2b46 2022-12-29 stsp /* Connection closed. */
1066 b50a2b46 2022-12-29 stsp goto done;
1070 b50a2b46 2022-12-29 stsp for (;;) {
1071 b50a2b46 2022-12-29 stsp const struct got_error *err = NULL;
1072 b50a2b46 2022-12-29 stsp struct gotd_client *client = NULL;
1073 b50a2b46 2022-12-29 stsp uint32_t client_id = 0;
1074 b50a2b46 2022-12-29 stsp int do_disconnect = 0;
1076 b50a2b46 2022-12-29 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
1077 b50a2b46 2022-12-29 stsp fatal("%s: imsg_get error", __func__);
1078 b50a2b46 2022-12-29 stsp if (n == 0) /* No more messages. */
1081 b50a2b46 2022-12-29 stsp switch (imsg.hdr.type) {
1082 b50a2b46 2022-12-29 stsp case GOTD_IMSG_ERROR:
1083 b50a2b46 2022-12-29 stsp do_disconnect = 1;
1084 b50a2b46 2022-12-29 stsp err = gotd_imsg_recv_error(&client_id, &imsg);
1086 b50a2b46 2022-12-29 stsp case GOTD_IMSG_CONNECT:
1087 b50a2b46 2022-12-29 stsp err = recv_connect(&client_id, &imsg);
1090 b50a2b46 2022-12-29 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1094 b50a2b46 2022-12-29 stsp client = find_client(client_id);
1095 b50a2b46 2022-12-29 stsp if (client == NULL) {
1096 b50a2b46 2022-12-29 stsp log_warnx("%s: client not found", __func__);
1097 b50a2b46 2022-12-29 stsp imsg_free(&imsg);
1102 b50a2b46 2022-12-29 stsp log_warnx("uid %d: %s", client->euid, err->msg);
1104 b50a2b46 2022-12-29 stsp if (do_disconnect) {
1106 b50a2b46 2022-12-29 stsp disconnect_on_error(client, err);
1108 b50a2b46 2022-12-29 stsp disconnect(client);
1111 b50a2b46 2022-12-29 stsp imsg_free(&imsg);
1114 b50a2b46 2022-12-29 stsp if (!shut) {
1115 b50a2b46 2022-12-29 stsp gotd_imsg_event_add(iev);
1117 b50a2b46 2022-12-29 stsp /* This pipe is dead. Remove its event handler */
1118 b50a2b46 2022-12-29 stsp event_del(&iev->ev);
1119 b50a2b46 2022-12-29 stsp event_loopexit(NULL);
1123 b50a2b46 2022-12-29 stsp static void
1124 ba97b2d7 2024-03-20 stsp gotd_dispatch_notifier(int fd, short event, void *arg)
1126 ba97b2d7 2024-03-20 stsp struct gotd_imsgev *iev = arg;
1127 ba97b2d7 2024-03-20 stsp struct imsgbuf *ibuf = &iev->ibuf;
1128 ba97b2d7 2024-03-20 stsp struct gotd_child_proc *proc = gotd.notify_proc;
1129 ba97b2d7 2024-03-20 stsp ssize_t n;
1130 ba97b2d7 2024-03-20 stsp int shut = 0;
1131 ba97b2d7 2024-03-20 stsp struct imsg imsg;
1133 ba97b2d7 2024-03-20 stsp if (proc->iev.ibuf.fd != fd)
1134 ba97b2d7 2024-03-20 stsp fatalx("%s: unexpected fd %d", __func__, fd);
1136 ba97b2d7 2024-03-20 stsp if (event & EV_READ) {
1137 ba97b2d7 2024-03-20 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1138 ba97b2d7 2024-03-20 stsp fatal("imsg_read error");
1139 ba97b2d7 2024-03-20 stsp if (n == 0) {
1140 ba97b2d7 2024-03-20 stsp /* Connection closed. */
1142 ba97b2d7 2024-03-20 stsp goto done;
1146 ba97b2d7 2024-03-20 stsp if (event & EV_WRITE) {
1147 ba97b2d7 2024-03-20 stsp n = msgbuf_write(&ibuf->w);
1148 ba97b2d7 2024-03-20 stsp if (n == -1 && errno != EAGAIN)
1149 ba97b2d7 2024-03-20 stsp fatal("msgbuf_write");
1150 ba97b2d7 2024-03-20 stsp if (n == 0) {
1151 ba97b2d7 2024-03-20 stsp /* Connection closed. */
1153 ba97b2d7 2024-03-20 stsp goto done;
1157 ba97b2d7 2024-03-20 stsp for (;;) {
1158 ba97b2d7 2024-03-20 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
1159 ba97b2d7 2024-03-20 stsp fatal("%s: imsg_get error", __func__);
1160 ba97b2d7 2024-03-20 stsp if (n == 0) /* No more messages. */
1163 ba97b2d7 2024-03-20 stsp switch (imsg.hdr.type) {
1165 ba97b2d7 2024-03-20 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1169 ba97b2d7 2024-03-20 stsp imsg_free(&imsg);
1172 ba97b2d7 2024-03-20 stsp if (!shut) {
1173 ba97b2d7 2024-03-20 stsp gotd_imsg_event_add(iev);
1175 ba97b2d7 2024-03-20 stsp /* This pipe is dead. Remove its event handler */
1176 ba97b2d7 2024-03-20 stsp event_del(&iev->ev);
1179 ba97b2d7 2024-03-20 stsp * Do not exit all of gotd if the notification handler dies.
1180 ba97b2d7 2024-03-20 stsp * We can continue operating without notifications until an
1181 ba97b2d7 2024-03-20 stsp * operator intervenes.
1183 ba97b2d7 2024-03-20 stsp log_warnx("notify child process (pid %d) closed its imsg pipe "
1184 ba97b2d7 2024-03-20 stsp "unexpectedly", proc->pid);
1185 ba97b2d7 2024-03-20 stsp proc_done(proc);
1189 ba97b2d7 2024-03-20 stsp static void
1190 5e25db14 2022-12-29 stsp gotd_dispatch_auth_child(int fd, short event, void *arg)
1192 5e25db14 2022-12-29 stsp const struct got_error *err = NULL;
1193 5e25db14 2022-12-29 stsp struct gotd_imsgev *iev = arg;
1194 5e25db14 2022-12-29 stsp struct imsgbuf *ibuf = &iev->ibuf;
1195 5e25db14 2022-12-29 stsp struct gotd_client *client;
1196 5e25db14 2022-12-29 stsp struct gotd_repo *repo = NULL;
1197 5e25db14 2022-12-29 stsp ssize_t n;
1198 5e25db14 2022-12-29 stsp int shut = 0;
1199 5e25db14 2022-12-29 stsp struct imsg imsg;
1200 5e25db14 2022-12-29 stsp uint32_t client_id = 0;
1201 5e25db14 2022-12-29 stsp int do_disconnect = 0;
1202 56624d2b 2023-12-27 stsp size_t datalen;
1204 5e25db14 2022-12-29 stsp client = find_client_by_proc_fd(fd);
1205 ae0cca99 2023-02-09 stsp if (client == NULL) {
1206 ae0cca99 2023-02-09 stsp /* Can happen during process teardown. */
1207 ae0cca99 2023-02-09 stsp warnx("cannot find client for fd %d", fd);
1209 ae0cca99 2023-02-09 stsp goto done;
1212 5e25db14 2022-12-29 stsp if (client->auth == NULL)
1213 5e25db14 2022-12-29 stsp fatalx("cannot find auth child process for fd %d", fd);
1215 5e25db14 2022-12-29 stsp if (event & EV_READ) {
1216 5e25db14 2022-12-29 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1217 5e25db14 2022-12-29 stsp fatal("imsg_read error");
1218 5e25db14 2022-12-29 stsp if (n == 0) {
1219 5e25db14 2022-12-29 stsp /* Connection closed. */
1221 5e25db14 2022-12-29 stsp goto done;
1225 5e25db14 2022-12-29 stsp if (event & EV_WRITE) {
1226 5e25db14 2022-12-29 stsp n = msgbuf_write(&ibuf->w);
1227 5e25db14 2022-12-29 stsp if (n == -1 && errno != EAGAIN)
1228 5e25db14 2022-12-29 stsp fatal("msgbuf_write");
1229 5e25db14 2022-12-29 stsp if (n == 0) {
1230 5e25db14 2022-12-29 stsp /* Connection closed. */
1233 5e25db14 2022-12-29 stsp goto done;
1236 5e25db14 2022-12-29 stsp if (client->auth->iev.ibuf.fd != fd)
1237 5e25db14 2022-12-29 stsp fatalx("%s: unexpected fd %d", __func__, fd);
1239 5e25db14 2022-12-29 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
1240 5e25db14 2022-12-29 stsp fatal("%s: imsg_get error", __func__);
1241 5e25db14 2022-12-29 stsp if (n == 0) /* No more messages. */
1244 5e25db14 2022-12-29 stsp evtimer_del(&client->tmo);
1246 56624d2b 2023-12-27 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1248 5e25db14 2022-12-29 stsp switch (imsg.hdr.type) {
1249 5e25db14 2022-12-29 stsp case GOTD_IMSG_ERROR:
1250 5e25db14 2022-12-29 stsp do_disconnect = 1;
1251 5e25db14 2022-12-29 stsp err = gotd_imsg_recv_error(&client_id, &imsg);
1253 5e25db14 2022-12-29 stsp case GOTD_IMSG_ACCESS_GRANTED:
1254 56624d2b 2023-12-27 stsp if (client->state != GOTD_CLIENT_STATE_NEW) {
1255 56624d2b 2023-12-27 stsp do_disconnect = 1;
1256 56624d2b 2023-12-27 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1260 5e25db14 2022-12-29 stsp do_disconnect = 1;
1261 5e25db14 2022-12-29 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1265 5e25db14 2022-12-29 stsp if (!verify_imsg_src(client, client->auth, &imsg)) {
1266 5e25db14 2022-12-29 stsp do_disconnect = 1;
1267 5e25db14 2022-12-29 stsp log_debug("dropping imsg type %d from PID %d",
1268 5e25db14 2022-12-29 stsp imsg.hdr.type, client->auth->pid);
1271 5e25db14 2022-12-29 stsp if (do_disconnect) {
1273 5e25db14 2022-12-29 stsp disconnect_on_error(client, err);
1275 5e25db14 2022-12-29 stsp disconnect(client);
1276 56624d2b 2023-12-27 stsp imsg_free(&imsg);
1280 56624d2b 2023-12-27 stsp client->state = GOTD_CLIENT_STATE_ACCESS_GRANTED;
1281 56624d2b 2023-12-27 stsp if (datalen > 0)
1282 56624d2b 2023-12-27 stsp client->username = strndup(imsg.data, datalen);
1283 56624d2b 2023-12-27 stsp imsg_free(&imsg);
1284 56624d2b 2023-12-27 stsp if (client->username == NULL &&
1285 56624d2b 2023-12-27 stsp asprintf(&client->username, "uid %d", client->euid) == -1) {
1286 56624d2b 2023-12-27 stsp err = got_error_from_errno("asprintf");
1287 56624d2b 2023-12-27 stsp goto done;
1290 ba97b2d7 2024-03-20 stsp repo = gotd_find_repo_by_name(client->auth->repo_name, &gotd.repos);
1291 5e25db14 2022-12-29 stsp if (repo == NULL) {
1292 5e25db14 2022-12-29 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
1293 5e25db14 2022-12-29 stsp goto done;
1295 5e25db14 2022-12-29 stsp kill_auth_proc(client);
1297 56624d2b 2023-12-27 stsp log_info("authenticated %s for repository %s",
1298 56624d2b 2023-12-27 stsp client->username, repo->name);
1300 ae7c1b78 2023-01-10 stsp err = start_session_child(client, repo, gotd.argv0,
1301 7fdc3e58 2022-12-30 mark gotd.confpath, gotd.daemonize, gotd.verbosity);
1303 ae7c1b78 2023-01-10 stsp goto done;
1306 5e25db14 2022-12-29 stsp log_warnx("uid %d: %s", client->euid, err->msg);
1308 5e25db14 2022-12-29 stsp /* We might have killed the auth process by now. */
1309 5e25db14 2022-12-29 stsp if (client->auth != NULL) {
1310 5e25db14 2022-12-29 stsp if (!shut) {
1311 5e25db14 2022-12-29 stsp gotd_imsg_event_add(iev);
1313 5e25db14 2022-12-29 stsp /* This pipe is dead. Remove its event handler */
1314 5e25db14 2022-12-29 stsp event_del(&iev->ev);
1319 ae7c1b78 2023-01-10 stsp static const struct got_error *
1320 ae7c1b78 2023-01-10 stsp connect_session(struct gotd_client *client)
1322 ae7c1b78 2023-01-10 stsp const struct got_error *err = NULL;
1323 ae7c1b78 2023-01-10 stsp struct gotd_imsg_connect iconnect;
1325 ba97b2d7 2024-03-20 stsp struct ibuf *wbuf;
1327 ae7c1b78 2023-01-10 stsp memset(&iconnect, 0, sizeof(iconnect));
1329 ae7c1b78 2023-01-10 stsp s = dup(client->fd);
1330 ae7c1b78 2023-01-10 stsp if (s == -1)
1331 ae7c1b78 2023-01-10 stsp return got_error_from_errno("dup");
1333 ae7c1b78 2023-01-10 stsp iconnect.client_id = client->id;
1334 ae7c1b78 2023-01-10 stsp iconnect.euid = client->euid;
1335 ae7c1b78 2023-01-10 stsp iconnect.egid = client->egid;
1336 ba97b2d7 2024-03-20 stsp iconnect.username_len = strlen(client->username);
1338 ba97b2d7 2024-03-20 stsp wbuf = imsg_create(&client->session->iev.ibuf, GOTD_IMSG_CONNECT,
1339 ba97b2d7 2024-03-20 stsp PROC_GOTD, gotd.pid, sizeof(iconnect) + iconnect.username_len);
1340 ba97b2d7 2024-03-20 stsp if (wbuf == NULL) {
1341 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("imsg compose CONNECT");
1343 ae7c1b78 2023-01-10 stsp return err;
1345 ba97b2d7 2024-03-20 stsp if (imsg_add(wbuf, &iconnect, sizeof(iconnect)) == -1) {
1347 ba97b2d7 2024-03-20 stsp return got_error_from_errno("imsg_add CONNECT");
1349 ba97b2d7 2024-03-20 stsp if (imsg_add(wbuf, client->username, iconnect.username_len) == -1) {
1351 ba97b2d7 2024-03-20 stsp return got_error_from_errno("imsg_add CONNECT");
1354 ba97b2d7 2024-03-20 stsp ibuf_fd_set(wbuf, s);
1355 ba97b2d7 2024-03-20 stsp imsg_close(&client->session->iev.ibuf, wbuf);
1356 ba97b2d7 2024-03-20 stsp gotd_imsg_event_add(&client->session->iev);
1359 ae7c1b78 2023-01-10 stsp * We are no longer interested in messages from this client.
1360 ae7c1b78 2023-01-10 stsp * Further client requests will be handled by the session process.
1362 ae7c1b78 2023-01-10 stsp msgbuf_clear(&client->iev.ibuf.w);
1363 ae7c1b78 2023-01-10 stsp imsg_clear(&client->iev.ibuf);
1364 ae7c1b78 2023-01-10 stsp event_del(&client->iev.ev);
1365 ae7c1b78 2023-01-10 stsp client->fd = -1; /* will be closed via copy in client->iev.ibuf.fd */
1367 ae7c1b78 2023-01-10 stsp return NULL;
1370 5e25db14 2022-12-29 stsp static void
1371 ae7c1b78 2023-01-10 stsp gotd_dispatch_client_session(int fd, short event, void *arg)
1373 b50a2b46 2022-12-29 stsp struct gotd_imsgev *iev = arg;
1374 b50a2b46 2022-12-29 stsp struct imsgbuf *ibuf = &iev->ibuf;
1375 13b2bc37 2022-10-23 stsp struct gotd_child_proc *proc = NULL;
1376 b50a2b46 2022-12-29 stsp struct gotd_client *client = NULL;
1377 13b2bc37 2022-10-23 stsp ssize_t n;
1378 13b2bc37 2022-10-23 stsp int shut = 0;
1379 13b2bc37 2022-10-23 stsp struct imsg imsg;
1381 ae7c1b78 2023-01-10 stsp client = find_client_by_proc_fd(fd);
1382 ae0cca99 2023-02-09 stsp if (client == NULL) {
1383 ae0cca99 2023-02-09 stsp /* Can happen during process teardown. */
1384 ae0cca99 2023-02-09 stsp warnx("cannot find client for fd %d", fd);
1386 ae0cca99 2023-02-09 stsp goto done;
1389 13b2bc37 2022-10-23 stsp if (event & EV_READ) {
1390 13b2bc37 2022-10-23 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1391 13b2bc37 2022-10-23 stsp fatal("imsg_read error");
1392 13b2bc37 2022-10-23 stsp if (n == 0) {
1393 13b2bc37 2022-10-23 stsp /* Connection closed. */
1395 13b2bc37 2022-10-23 stsp goto done;
1399 13b2bc37 2022-10-23 stsp if (event & EV_WRITE) {
1400 13b2bc37 2022-10-23 stsp n = msgbuf_write(&ibuf->w);
1401 13b2bc37 2022-10-23 stsp if (n == -1 && errno != EAGAIN)
1402 13b2bc37 2022-10-23 stsp fatal("msgbuf_write");
1403 13b2bc37 2022-10-23 stsp if (n == 0) {
1404 13b2bc37 2022-10-23 stsp /* Connection closed. */
1406 13b2bc37 2022-10-23 stsp goto done;
1410 ae7c1b78 2023-01-10 stsp proc = client->session;
1411 ae7c1b78 2023-01-10 stsp if (proc == NULL)
1412 ae7c1b78 2023-01-10 stsp fatalx("cannot find session child process for fd %d", fd);
1414 ae7c1b78 2023-01-10 stsp for (;;) {
1415 ae7c1b78 2023-01-10 stsp const struct got_error *err = NULL;
1416 ae7c1b78 2023-01-10 stsp uint32_t client_id = 0;
1417 ae7c1b78 2023-01-10 stsp int do_disconnect = 0, do_start_repo_child = 0;
1419 ae7c1b78 2023-01-10 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
1420 ae7c1b78 2023-01-10 stsp fatal("%s: imsg_get error", __func__);
1421 ae7c1b78 2023-01-10 stsp if (n == 0) /* No more messages. */
1424 ae7c1b78 2023-01-10 stsp switch (imsg.hdr.type) {
1425 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_ERROR:
1426 ae7c1b78 2023-01-10 stsp do_disconnect = 1;
1427 ae7c1b78 2023-01-10 stsp err = gotd_imsg_recv_error(&client_id, &imsg);
1429 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_CLIENT_SESSION_READY:
1430 eac23c30 2023-01-10 stsp if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED) {
1431 ae7c1b78 2023-01-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1434 ae7c1b78 2023-01-10 stsp do_start_repo_child = 1;
1436 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_DISCONNECT:
1437 ae7c1b78 2023-01-10 stsp do_disconnect = 1;
1440 ae7c1b78 2023-01-10 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1444 ae7c1b78 2023-01-10 stsp if (!verify_imsg_src(client, proc, &imsg)) {
1445 ae7c1b78 2023-01-10 stsp log_debug("dropping imsg type %d from PID %d",
1446 ae7c1b78 2023-01-10 stsp imsg.hdr.type, proc->pid);
1447 ae7c1b78 2023-01-10 stsp imsg_free(&imsg);
1451 ae7c1b78 2023-01-10 stsp log_warnx("uid %d: %s", client->euid, err->msg);
1453 ae7c1b78 2023-01-10 stsp if (do_start_repo_child) {
1454 ae7c1b78 2023-01-10 stsp struct gotd_repo *repo;
1455 b09c1279 2023-03-28 stsp const char *name = client->session->repo_name;
1457 ba97b2d7 2024-03-20 stsp repo = gotd_find_repo_by_name(name, &gotd.repos);
1458 ae7c1b78 2023-01-10 stsp if (repo != NULL) {
1459 ae7c1b78 2023-01-10 stsp enum gotd_procid proc_type;
1461 ae7c1b78 2023-01-10 stsp if (client->required_auth & GOTD_AUTH_WRITE)
1462 ae7c1b78 2023-01-10 stsp proc_type = PROC_REPO_WRITE;
1464 ae7c1b78 2023-01-10 stsp proc_type = PROC_REPO_READ;
1466 ae7c1b78 2023-01-10 stsp err = start_repo_child(client, proc_type, repo,
1467 ae7c1b78 2023-01-10 stsp gotd.argv0, gotd.confpath, gotd.daemonize,
1468 ae7c1b78 2023-01-10 stsp gotd.verbosity);
1470 ae7c1b78 2023-01-10 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
1472 ae7c1b78 2023-01-10 stsp if (err) {
1473 ae7c1b78 2023-01-10 stsp log_warnx("uid %d: %s", client->euid, err->msg);
1474 ae7c1b78 2023-01-10 stsp do_disconnect = 1;
1478 ae7c1b78 2023-01-10 stsp if (do_disconnect) {
1480 ae7c1b78 2023-01-10 stsp disconnect_on_error(client, err);
1482 ae7c1b78 2023-01-10 stsp disconnect(client);
1485 ae7c1b78 2023-01-10 stsp imsg_free(&imsg);
1488 ae7c1b78 2023-01-10 stsp if (!shut) {
1489 ae7c1b78 2023-01-10 stsp gotd_imsg_event_add(iev);
1491 ae7c1b78 2023-01-10 stsp /* This pipe is dead. Remove its event handler */
1492 ae7c1b78 2023-01-10 stsp event_del(&iev->ev);
1493 ae7c1b78 2023-01-10 stsp disconnect(client);
1497 ba97b2d7 2024-03-20 stsp static const struct got_error *
1498 ba97b2d7 2024-03-20 stsp connect_notifier_and_session(struct gotd_client *client)
1500 ba97b2d7 2024-03-20 stsp const struct got_error *err = NULL;
1501 ba97b2d7 2024-03-20 stsp struct gotd_imsgev *session_iev = &client->session->iev;
1502 ba97b2d7 2024-03-20 stsp int pipe[2];
1504 ba97b2d7 2024-03-20 stsp if (gotd.notify_proc == NULL)
1505 ba97b2d7 2024-03-20 stsp return NULL;
1507 ba97b2d7 2024-03-20 stsp if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1508 ba97b2d7 2024-03-20 stsp PF_UNSPEC, pipe) == -1)
1509 ba97b2d7 2024-03-20 stsp return got_error_from_errno("socketpair");
1511 ba97b2d7 2024-03-20 stsp /* Pass notifier pipe to session . */
1512 ba97b2d7 2024-03-20 stsp if (gotd_imsg_compose_event(session_iev, GOTD_IMSG_CONNECT_NOTIFIER,
1513 ba97b2d7 2024-03-20 stsp PROC_GOTD, pipe[0], NULL, 0) == -1) {
1514 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("imsg compose CONNECT_NOTIFIER");
1515 ba97b2d7 2024-03-20 stsp close(pipe[0]);
1516 ba97b2d7 2024-03-20 stsp close(pipe[1]);
1517 ba97b2d7 2024-03-20 stsp return err;
1520 ba97b2d7 2024-03-20 stsp /* Pass session pipe to notifier. */
1521 ba97b2d7 2024-03-20 stsp if (gotd_imsg_compose_event(&gotd.notify_proc->iev,
1522 ba97b2d7 2024-03-20 stsp GOTD_IMSG_CONNECT_SESSION, PROC_GOTD, pipe[1], NULL, 0) == -1) {
1523 ba97b2d7 2024-03-20 stsp err = got_error_from_errno("imsg compose CONNECT_SESSION");
1524 ba97b2d7 2024-03-20 stsp close(pipe[1]);
1525 ba97b2d7 2024-03-20 stsp return err;
1528 ba97b2d7 2024-03-20 stsp return NULL;
1531 ae7c1b78 2023-01-10 stsp static void
1532 ae7c1b78 2023-01-10 stsp gotd_dispatch_repo_child(int fd, short event, void *arg)
1534 ae7c1b78 2023-01-10 stsp struct gotd_imsgev *iev = arg;
1535 ae7c1b78 2023-01-10 stsp struct imsgbuf *ibuf = &iev->ibuf;
1536 ae7c1b78 2023-01-10 stsp struct gotd_child_proc *proc = NULL;
1537 ae7c1b78 2023-01-10 stsp struct gotd_client *client;
1538 ae7c1b78 2023-01-10 stsp ssize_t n;
1539 ae7c1b78 2023-01-10 stsp int shut = 0;
1540 ae7c1b78 2023-01-10 stsp struct imsg imsg;
1542 b50a2b46 2022-12-29 stsp client = find_client_by_proc_fd(fd);
1543 ae0cca99 2023-02-09 stsp if (client == NULL) {
1544 ae0cca99 2023-02-09 stsp /* Can happen during process teardown. */
1545 ae0cca99 2023-02-09 stsp warnx("cannot find client for fd %d", fd);
1547 ae0cca99 2023-02-09 stsp goto done;
1550 ae7c1b78 2023-01-10 stsp if (event & EV_READ) {
1551 ae7c1b78 2023-01-10 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1552 ae7c1b78 2023-01-10 stsp fatal("imsg_read error");
1553 ae7c1b78 2023-01-10 stsp if (n == 0) {
1554 ae7c1b78 2023-01-10 stsp /* Connection closed. */
1556 ae7c1b78 2023-01-10 stsp goto done;
1560 ae7c1b78 2023-01-10 stsp if (event & EV_WRITE) {
1561 ae7c1b78 2023-01-10 stsp n = msgbuf_write(&ibuf->w);
1562 ae7c1b78 2023-01-10 stsp if (n == -1 && errno != EAGAIN)
1563 ae7c1b78 2023-01-10 stsp fatal("msgbuf_write");
1564 ae7c1b78 2023-01-10 stsp if (n == 0) {
1565 ae7c1b78 2023-01-10 stsp /* Connection closed. */
1567 ae7c1b78 2023-01-10 stsp goto done;
1571 f7a854cf 2023-01-10 stsp proc = client->repo;
1572 13b2bc37 2022-10-23 stsp if (proc == NULL)
1573 13b2bc37 2022-10-23 stsp fatalx("cannot find child process for fd %d", fd);
1575 13b2bc37 2022-10-23 stsp for (;;) {
1576 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
1577 13b2bc37 2022-10-23 stsp uint32_t client_id = 0;
1578 13b2bc37 2022-10-23 stsp int do_disconnect = 0;
1580 13b2bc37 2022-10-23 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
1581 13b2bc37 2022-10-23 stsp fatal("%s: imsg_get error", __func__);
1582 13b2bc37 2022-10-23 stsp if (n == 0) /* No more messages. */
1585 13b2bc37 2022-10-23 stsp switch (imsg.hdr.type) {
1586 13b2bc37 2022-10-23 stsp case GOTD_IMSG_ERROR:
1587 13b2bc37 2022-10-23 stsp do_disconnect = 1;
1588 13b2bc37 2022-10-23 stsp err = gotd_imsg_recv_error(&client_id, &imsg);
1590 b50a2b46 2022-12-29 stsp case GOTD_IMSG_REPO_CHILD_READY:
1591 ae7c1b78 2023-01-10 stsp err = connect_session(client);
1594 ba97b2d7 2024-03-20 stsp err = connect_notifier_and_session(client);
1597 ae7c1b78 2023-01-10 stsp err = connect_repo_child(client, proc);
1600 13b2bc37 2022-10-23 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1604 13b2bc37 2022-10-23 stsp if (!verify_imsg_src(client, proc, &imsg)) {
1605 13b2bc37 2022-10-23 stsp log_debug("dropping imsg type %d from PID %d",
1606 13b2bc37 2022-10-23 stsp imsg.hdr.type, proc->pid);
1607 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
1611 13b2bc37 2022-10-23 stsp log_warnx("uid %d: %s", client->euid, err->msg);
1613 13b2bc37 2022-10-23 stsp if (do_disconnect) {
1615 13b2bc37 2022-10-23 stsp disconnect_on_error(client, err);
1617 13b2bc37 2022-10-23 stsp disconnect(client);
1620 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
1623 13b2bc37 2022-10-23 stsp if (!shut) {
1624 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(iev);
1626 13b2bc37 2022-10-23 stsp /* This pipe is dead. Remove its event handler */
1627 13b2bc37 2022-10-23 stsp event_del(&iev->ev);
1628 ae7c1b78 2023-01-10 stsp disconnect(client);
1632 13b2bc37 2022-10-23 stsp static pid_t
1633 eec68231 2022-12-14 stsp start_child(enum gotd_procid proc_id, const char *repo_path,
1634 585362fd 2022-10-31 op char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
1636 585362fd 2022-10-31 op char *argv[11];
1637 13b2bc37 2022-10-23 stsp int argc = 0;
1638 13b2bc37 2022-10-23 stsp pid_t pid;
1640 13b2bc37 2022-10-23 stsp switch (pid = fork()) {
1642 13b2bc37 2022-10-23 stsp fatal("cannot fork");
1646 13b2bc37 2022-10-23 stsp close(fd);
1647 13b2bc37 2022-10-23 stsp return pid;
1650 8c6fc146 2022-11-17 stsp if (fd != GOTD_FILENO_MSG_PIPE) {
1651 8c6fc146 2022-11-17 stsp if (dup2(fd, GOTD_FILENO_MSG_PIPE) == -1)
1652 13b2bc37 2022-10-23 stsp fatal("cannot setup imsg fd");
1653 13b2bc37 2022-10-23 stsp } else if (fcntl(fd, F_SETFD, 0) == -1)
1654 13b2bc37 2022-10-23 stsp fatal("cannot setup imsg fd");
1656 13b2bc37 2022-10-23 stsp argv[argc++] = argv0;
1657 13b2bc37 2022-10-23 stsp switch (proc_id) {
1658 d93ecf7d 2022-12-14 stsp case PROC_LISTEN:
1659 d93ecf7d 2022-12-14 stsp argv[argc++] = (char *)"-L";
1661 5e25db14 2022-12-29 stsp case PROC_AUTH:
1662 5e25db14 2022-12-29 stsp argv[argc++] = (char *)"-A";
1664 b0614828 2023-06-19 stsp case PROC_SESSION_READ:
1665 b0614828 2023-06-19 stsp argv[argc++] = (char *)"-s";
1667 b0614828 2023-06-19 stsp case PROC_SESSION_WRITE:
1668 ae7c1b78 2023-01-10 stsp argv[argc++] = (char *)"-S";
1670 13b2bc37 2022-10-23 stsp case PROC_REPO_READ:
1671 13b2bc37 2022-10-23 stsp argv[argc++] = (char *)"-R";
1673 13b2bc37 2022-10-23 stsp case PROC_REPO_WRITE:
1674 13b2bc37 2022-10-23 stsp argv[argc++] = (char *)"-W";
1676 ba97b2d7 2024-03-20 stsp case PROC_NOTIFY:
1677 ba97b2d7 2024-03-20 stsp argv[argc++] = (char *)"-N";
1680 13b2bc37 2022-10-23 stsp fatalx("invalid process id %d", proc_id);
1683 585362fd 2022-10-31 op argv[argc++] = (char *)"-f";
1684 585362fd 2022-10-31 op argv[argc++] = (char *)confpath;
1686 eec68231 2022-12-14 stsp if (repo_path) {
1687 d93ecf7d 2022-12-14 stsp argv[argc++] = (char *)"-P";
1688 eec68231 2022-12-14 stsp argv[argc++] = (char *)repo_path;
1691 13b2bc37 2022-10-23 stsp if (!daemonize)
1692 13b2bc37 2022-10-23 stsp argv[argc++] = (char *)"-d";
1693 13b2bc37 2022-10-23 stsp if (verbosity > 0)
1694 13b2bc37 2022-10-23 stsp argv[argc++] = (char *)"-v";
1695 13b2bc37 2022-10-23 stsp if (verbosity > 1)
1696 13b2bc37 2022-10-23 stsp argv[argc++] = (char *)"-v";
1697 13b2bc37 2022-10-23 stsp argv[argc++] = NULL;
1699 13b2bc37 2022-10-23 stsp execvp(argv0, argv);
1700 13b2bc37 2022-10-23 stsp fatal("execvp");
1703 13b2bc37 2022-10-23 stsp static void
1704 d93ecf7d 2022-12-14 stsp start_listener(char *argv0, const char *confpath, int daemonize, int verbosity)
1706 c929736a 2023-06-22 op struct gotd_child_proc *proc;
1708 c929736a 2023-06-22 op proc = calloc(1, sizeof(*proc));
1709 c929736a 2023-06-22 op if (proc == NULL)
1710 c929736a 2023-06-22 op fatal("calloc");
1712 839338f6 2023-06-22 op TAILQ_INSERT_HEAD(&procs, proc, entry);
1714 839338f6 2023-06-22 op /* proc->tmo is initialized in main() after event_init() */
1716 d93ecf7d 2022-12-14 stsp proc->type = PROC_LISTEN;
1718 d93ecf7d 2022-12-14 stsp if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1719 d93ecf7d 2022-12-14 stsp PF_UNSPEC, proc->pipe) == -1)
1720 d93ecf7d 2022-12-14 stsp fatal("socketpair");
1722 d93ecf7d 2022-12-14 stsp proc->pid = start_child(proc->type, NULL, argv0, confpath,
1723 d93ecf7d 2022-12-14 stsp proc->pipe[1], daemonize, verbosity);
1724 d93ecf7d 2022-12-14 stsp imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1725 b50a2b46 2022-12-29 stsp proc->iev.handler = gotd_dispatch_listener;
1726 d93ecf7d 2022-12-14 stsp proc->iev.events = EV_READ;
1727 d93ecf7d 2022-12-14 stsp proc->iev.handler_arg = NULL;
1729 c929736a 2023-06-22 op gotd.listen_proc = proc;
1732 ba97b2d7 2024-03-20 stsp static void
1733 ba97b2d7 2024-03-20 stsp start_notifier(char *argv0, const char *confpath, int daemonize, int verbosity)
1735 ba97b2d7 2024-03-20 stsp struct gotd_child_proc *proc;
1737 ba97b2d7 2024-03-20 stsp proc = calloc(1, sizeof(*proc));
1738 ba97b2d7 2024-03-20 stsp if (proc == NULL)
1739 ba97b2d7 2024-03-20 stsp fatal("calloc");
1741 ba97b2d7 2024-03-20 stsp TAILQ_INSERT_HEAD(&procs, proc, entry);
1743 ba97b2d7 2024-03-20 stsp /* proc->tmo is initialized in main() after event_init() */
1745 ba97b2d7 2024-03-20 stsp proc->type = PROC_NOTIFY;
1747 ba97b2d7 2024-03-20 stsp if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1748 ba97b2d7 2024-03-20 stsp PF_UNSPEC, proc->pipe) == -1)
1749 ba97b2d7 2024-03-20 stsp fatal("socketpair");
1751 ba97b2d7 2024-03-20 stsp proc->pid = start_child(proc->type, NULL, argv0, confpath,
1752 ba97b2d7 2024-03-20 stsp proc->pipe[1], daemonize, verbosity);
1753 ba97b2d7 2024-03-20 stsp imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1754 ba97b2d7 2024-03-20 stsp proc->iev.handler = gotd_dispatch_notifier;
1755 ba97b2d7 2024-03-20 stsp proc->iev.events = EV_READ;
1756 ba97b2d7 2024-03-20 stsp proc->iev.handler_arg = NULL;
1757 ba97b2d7 2024-03-20 stsp event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1758 ba97b2d7 2024-03-20 stsp gotd_dispatch_notifier, &proc->iev);
1760 ba97b2d7 2024-03-20 stsp gotd.notify_proc = proc;
1763 b50a2b46 2022-12-29 stsp static const struct got_error *
1764 ae7c1b78 2023-01-10 stsp start_session_child(struct gotd_client *client, struct gotd_repo *repo,
1765 ae7c1b78 2023-01-10 stsp char *argv0, const char *confpath, int daemonize, int verbosity)
1767 ae7c1b78 2023-01-10 stsp struct gotd_child_proc *proc;
1769 ae7c1b78 2023-01-10 stsp proc = calloc(1, sizeof(*proc));
1770 ae7c1b78 2023-01-10 stsp if (proc == NULL)
1771 ae7c1b78 2023-01-10 stsp return got_error_from_errno("calloc");
1773 839338f6 2023-06-22 op TAILQ_INSERT_HEAD(&procs, proc, entry);
1774 839338f6 2023-06-22 op evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1776 b0614828 2023-06-19 stsp if (client_is_reading(client))
1777 b0614828 2023-06-19 stsp proc->type = PROC_SESSION_READ;
1779 b0614828 2023-06-19 stsp proc->type = PROC_SESSION_WRITE;
1780 ae7c1b78 2023-01-10 stsp if (strlcpy(proc->repo_name, repo->name,
1781 ae7c1b78 2023-01-10 stsp sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1782 ae7c1b78 2023-01-10 stsp fatalx("repository name too long: %s", repo->name);
1783 ae7c1b78 2023-01-10 stsp log_debug("starting client uid %d session for repository %s",
1784 ae7c1b78 2023-01-10 stsp client->euid, repo->name);
1785 ae7c1b78 2023-01-10 stsp if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1786 ae7c1b78 2023-01-10 stsp sizeof(proc->repo_path))
1787 ae7c1b78 2023-01-10 stsp fatalx("repository path too long: %s", repo->path);
1788 ae7c1b78 2023-01-10 stsp if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1789 ae7c1b78 2023-01-10 stsp PF_UNSPEC, proc->pipe) == -1)
1790 ae7c1b78 2023-01-10 stsp fatal("socketpair");
1791 ae7c1b78 2023-01-10 stsp proc->pid = start_child(proc->type, proc->repo_path, argv0,
1792 ae7c1b78 2023-01-10 stsp confpath, proc->pipe[1], daemonize, verbosity);
1793 ae7c1b78 2023-01-10 stsp imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1794 ae7c1b78 2023-01-10 stsp log_debug("proc %s %s is on fd %d",
1795 ae7c1b78 2023-01-10 stsp gotd_proc_names[proc->type], proc->repo_path,
1796 ae7c1b78 2023-01-10 stsp proc->pipe[0]);
1797 ae7c1b78 2023-01-10 stsp proc->iev.handler = gotd_dispatch_client_session;
1798 ae7c1b78 2023-01-10 stsp proc->iev.events = EV_READ;
1799 ae7c1b78 2023-01-10 stsp proc->iev.handler_arg = NULL;
1800 ae7c1b78 2023-01-10 stsp event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1801 ae7c1b78 2023-01-10 stsp gotd_dispatch_client_session, &proc->iev);
1802 ae7c1b78 2023-01-10 stsp gotd_imsg_event_add(&proc->iev);
1804 ae7c1b78 2023-01-10 stsp client->session = proc;
1805 ae7c1b78 2023-01-10 stsp return NULL;
1808 ae7c1b78 2023-01-10 stsp static const struct got_error *
1809 b50a2b46 2022-12-29 stsp start_repo_child(struct gotd_client *client, enum gotd_procid proc_type,
1810 b50a2b46 2022-12-29 stsp struct gotd_repo *repo, char *argv0, const char *confpath,
1811 585362fd 2022-10-31 op int daemonize, int verbosity)
1813 13b2bc37 2022-10-23 stsp struct gotd_child_proc *proc;
1815 b50a2b46 2022-12-29 stsp if (proc_type != PROC_REPO_READ && proc_type != PROC_REPO_WRITE)
1816 b50a2b46 2022-12-29 stsp return got_error_msg(GOT_ERR_NOT_IMPL, "bad process type");
1818 b50a2b46 2022-12-29 stsp proc = calloc(1, sizeof(*proc));
1819 b50a2b46 2022-12-29 stsp if (proc == NULL)
1820 b50a2b46 2022-12-29 stsp return got_error_from_errno("calloc");
1822 839338f6 2023-06-22 op TAILQ_INSERT_HEAD(&procs, proc, entry);
1823 839338f6 2023-06-22 op evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1825 b50a2b46 2022-12-29 stsp proc->type = proc_type;
1826 b50a2b46 2022-12-29 stsp if (strlcpy(proc->repo_name, repo->name,
1827 b50a2b46 2022-12-29 stsp sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1828 b50a2b46 2022-12-29 stsp fatalx("repository name too long: %s", repo->name);
1829 b50a2b46 2022-12-29 stsp log_debug("starting %s for repository %s",
1830 b50a2b46 2022-12-29 stsp proc->type == PROC_REPO_READ ? "reader" : "writer", repo->name);
1831 9b7f22a6 2023-01-08 stsp if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1832 9b7f22a6 2023-01-08 stsp sizeof(proc->repo_path))
1833 9b7f22a6 2023-01-08 stsp fatalx("repository path too long: %s", repo->path);
1834 b50a2b46 2022-12-29 stsp if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1835 b50a2b46 2022-12-29 stsp PF_UNSPEC, proc->pipe) == -1)
1836 b50a2b46 2022-12-29 stsp fatal("socketpair");
1837 b50a2b46 2022-12-29 stsp proc->pid = start_child(proc->type, proc->repo_path, argv0,
1838 b50a2b46 2022-12-29 stsp confpath, proc->pipe[1], daemonize, verbosity);
1839 b50a2b46 2022-12-29 stsp imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1840 b50a2b46 2022-12-29 stsp log_debug("proc %s %s is on fd %d",
1841 b50a2b46 2022-12-29 stsp gotd_proc_names[proc->type], proc->repo_path,
1842 b50a2b46 2022-12-29 stsp proc->pipe[0]);
1843 b50a2b46 2022-12-29 stsp proc->iev.handler = gotd_dispatch_repo_child;
1844 b50a2b46 2022-12-29 stsp proc->iev.events = EV_READ;
1845 b50a2b46 2022-12-29 stsp proc->iev.handler_arg = NULL;
1846 b50a2b46 2022-12-29 stsp event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1847 b50a2b46 2022-12-29 stsp gotd_dispatch_repo_child, &proc->iev);
1848 b50a2b46 2022-12-29 stsp gotd_imsg_event_add(&proc->iev);
1850 f7a854cf 2023-01-10 stsp client->repo = proc;
1851 5e25db14 2022-12-29 stsp return NULL;
1854 5e25db14 2022-12-29 stsp static const struct got_error *
1855 5e25db14 2022-12-29 stsp start_auth_child(struct gotd_client *client, int required_auth,
1856 5e25db14 2022-12-29 stsp struct gotd_repo *repo, char *argv0, const char *confpath,
1857 5e25db14 2022-12-29 stsp int daemonize, int verbosity)
1859 365cf0f3 2022-12-29 stsp const struct got_error *err = NULL;
1860 5e25db14 2022-12-29 stsp struct gotd_child_proc *proc;
1861 5e25db14 2022-12-29 stsp struct gotd_imsg_auth iauth;
1864 5e25db14 2022-12-29 stsp memset(&iauth, 0, sizeof(iauth));
1866 365cf0f3 2022-12-29 stsp fd = dup(client->fd);
1867 365cf0f3 2022-12-29 stsp if (fd == -1)
1868 365cf0f3 2022-12-29 stsp return got_error_from_errno("dup");
1870 5e25db14 2022-12-29 stsp proc = calloc(1, sizeof(*proc));
1871 365cf0f3 2022-12-29 stsp if (proc == NULL) {
1872 365cf0f3 2022-12-29 stsp err = got_error_from_errno("calloc");
1873 365cf0f3 2022-12-29 stsp close(fd);
1874 365cf0f3 2022-12-29 stsp return err;
1877 839338f6 2023-06-22 op TAILQ_INSERT_HEAD(&procs, proc, entry);
1878 839338f6 2023-06-22 op evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1880 5e25db14 2022-12-29 stsp proc->type = PROC_AUTH;
1881 5e25db14 2022-12-29 stsp if (strlcpy(proc->repo_name, repo->name,
1882 5e25db14 2022-12-29 stsp sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1883 5e25db14 2022-12-29 stsp fatalx("repository name too long: %s", repo->name);
1884 5e25db14 2022-12-29 stsp log_debug("starting auth for uid %d repository %s",
1885 5e25db14 2022-12-29 stsp client->euid, repo->name);
1886 9b7f22a6 2023-01-08 stsp if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1887 9b7f22a6 2023-01-08 stsp sizeof(proc->repo_path))
1888 9b7f22a6 2023-01-08 stsp fatalx("repository path too long: %s", repo->path);
1889 5e25db14 2022-12-29 stsp if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1890 5e25db14 2022-12-29 stsp PF_UNSPEC, proc->pipe) == -1)
1891 5e25db14 2022-12-29 stsp fatal("socketpair");
1892 5e25db14 2022-12-29 stsp proc->pid = start_child(proc->type, proc->repo_path, argv0,
1893 5e25db14 2022-12-29 stsp confpath, proc->pipe[1], daemonize, verbosity);
1894 5e25db14 2022-12-29 stsp imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1895 5e25db14 2022-12-29 stsp log_debug("proc %s %s is on fd %d",
1896 5e25db14 2022-12-29 stsp gotd_proc_names[proc->type], proc->repo_path,
1897 5e25db14 2022-12-29 stsp proc->pipe[0]);
1898 5e25db14 2022-12-29 stsp proc->iev.handler = gotd_dispatch_auth_child;
1899 5e25db14 2022-12-29 stsp proc->iev.events = EV_READ;
1900 5e25db14 2022-12-29 stsp proc->iev.handler_arg = NULL;
1901 5e25db14 2022-12-29 stsp event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1902 5e25db14 2022-12-29 stsp gotd_dispatch_auth_child, &proc->iev);
1903 5e25db14 2022-12-29 stsp gotd_imsg_event_add(&proc->iev);
1905 5e25db14 2022-12-29 stsp iauth.euid = client->euid;
1906 5e25db14 2022-12-29 stsp iauth.egid = client->egid;
1907 5e25db14 2022-12-29 stsp iauth.required_auth = required_auth;
1908 5e25db14 2022-12-29 stsp iauth.client_id = client->id;
1909 5e25db14 2022-12-29 stsp if (gotd_imsg_compose_event(&proc->iev, GOTD_IMSG_AUTHENTICATE,
1910 365cf0f3 2022-12-29 stsp PROC_GOTD, fd, &iauth, sizeof(iauth)) == -1) {
1911 5e25db14 2022-12-29 stsp log_warn("imsg compose AUTHENTICATE");
1912 365cf0f3 2022-12-29 stsp close(fd);
1913 365cf0f3 2022-12-29 stsp /* Let the auth_timeout handler tidy up. */
1916 5e25db14 2022-12-29 stsp client->auth = proc;
1917 5e25db14 2022-12-29 stsp client->required_auth = required_auth;
1918 b50a2b46 2022-12-29 stsp return NULL;
1921 eec68231 2022-12-14 stsp static void
1922 b0614828 2023-06-19 stsp apply_unveil_repo_readonly(const char *repo_path, int need_tmpdir)
1924 b0614828 2023-06-19 stsp if (need_tmpdir) {
1925 b0614828 2023-06-19 stsp if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1926 b0614828 2023-06-19 stsp fatal("unveil %s", GOT_TMPDIR_STR);
1929 eec68231 2022-12-14 stsp if (unveil(repo_path, "r") == -1)
1930 eec68231 2022-12-14 stsp fatal("unveil %s", repo_path);
1932 44587340 2022-12-30 stsp if (unveil(NULL, NULL) == -1)
1933 44587340 2022-12-30 stsp fatal("unveil");
1936 44587340 2022-12-30 stsp static void
1937 ae7c1b78 2023-01-10 stsp apply_unveil_repo_readwrite(const char *repo_path)
1939 ae7c1b78 2023-01-10 stsp if (unveil(repo_path, "rwc") == -1)
1940 ae7c1b78 2023-01-10 stsp fatal("unveil %s", repo_path);
1942 ae7c1b78 2023-01-10 stsp if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1943 ae7c1b78 2023-01-10 stsp fatal("unveil %s", GOT_TMPDIR_STR);
1945 ae7c1b78 2023-01-10 stsp if (unveil(NULL, NULL) == -1)
1946 ae7c1b78 2023-01-10 stsp fatal("unveil");
1949 ae7c1b78 2023-01-10 stsp static void
1950 44587340 2022-12-30 stsp apply_unveil_none(void)
1952 44587340 2022-12-30 stsp if (unveil("/", "") == -1)
1953 44587340 2022-12-30 stsp fatal("unveil");
1955 eec68231 2022-12-14 stsp if (unveil(NULL, NULL) == -1)
1956 eec68231 2022-12-14 stsp fatal("unveil");
1959 13b2bc37 2022-10-23 stsp static void
1960 ae7c1b78 2023-01-10 stsp apply_unveil_selfexec(void)
1962 b50a2b46 2022-12-29 stsp if (unveil(gotd.argv0, "x") == -1)
1963 b50a2b46 2022-12-29 stsp fatal("unveil %s", gotd.argv0);
1965 13b2bc37 2022-10-23 stsp if (unveil(NULL, NULL) == -1)
1966 13b2bc37 2022-10-23 stsp fatal("unveil");
1969 abe89edb 2023-11-16 stsp static void
1970 abe89edb 2023-11-16 stsp set_max_datasize(void)
1972 abe89edb 2023-11-16 stsp struct rlimit rl;
1974 abe89edb 2023-11-16 stsp if (getrlimit(RLIMIT_DATA, &rl) != 0)
1977 abe89edb 2023-11-16 stsp rl.rlim_cur = rl.rlim_max;
1978 abe89edb 2023-11-16 stsp setrlimit(RLIMIT_DATA, &rl);
1981 ba97b2d7 2024-03-20 stsp static void
1982 ba97b2d7 2024-03-20 stsp unveil_notification_helpers(void)
1984 ba97b2d7 2024-03-20 stsp const char *helpers[] = {
1985 ba97b2d7 2024-03-20 stsp GOTD_PATH_PROG_NOTIFY_EMAIL,
1986 ba97b2d7 2024-03-20 stsp GOTD_PATH_PROG_NOTIFY_HTTP,
1990 ba97b2d7 2024-03-20 stsp for (i = 0; i < nitems(helpers); i++) {
1991 ba97b2d7 2024-03-20 stsp if (unveil(helpers[i], "x") == 0)
1993 ba97b2d7 2024-03-20 stsp fatal("unveil %s", helpers[i]);
1996 ba97b2d7 2024-03-20 stsp if (unveil(NULL, NULL) == -1)
1997 ba97b2d7 2024-03-20 stsp fatal("unveil");
2001 13b2bc37 2022-10-23 stsp main(int argc, char **argv)
2003 13b2bc37 2022-10-23 stsp const struct got_error *error = NULL;
2004 13b2bc37 2022-10-23 stsp int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
2005 13b2bc37 2022-10-23 stsp const char *confpath = GOTD_CONF_PATH;
2006 13b2bc37 2022-10-23 stsp char *argv0 = argv[0];
2007 13b2bc37 2022-10-23 stsp char title[2048];
2008 13b2bc37 2022-10-23 stsp struct passwd *pw = NULL;
2009 13b2bc37 2022-10-23 stsp char *repo_path = NULL;
2010 13b2bc37 2022-10-23 stsp enum gotd_procid proc_id = PROC_GOTD;
2011 839338f6 2023-06-22 op struct event evsigint, evsigterm, evsighup, evsigusr1, evsigchld;
2012 13b2bc37 2022-10-23 stsp int *pack_fds = NULL, *temp_fds = NULL;
2013 9afa3de2 2023-04-04 stsp struct gotd_repo *repo = NULL;
2014 ba97b2d7 2024-03-20 stsp char *default_sender = NULL;
2015 ba97b2d7 2024-03-20 stsp char hostname[HOST_NAME_MAX + 1];
2016 ba97b2d7 2024-03-20 stsp FILE *diff_f1 = NULL, *diff_f2 = NULL;
2017 ba97b2d7 2024-03-20 stsp int diff_fd1 = -1, diff_fd2 = -1;
2019 839338f6 2023-06-22 op TAILQ_INIT(&procs);
2021 13b2bc37 2022-10-23 stsp log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
2023 ba97b2d7 2024-03-20 stsp while ((ch = getopt(argc, argv, "Adf:LnNP:RsSvW")) != -1) {
2024 13b2bc37 2022-10-23 stsp switch (ch) {
2026 5e25db14 2022-12-29 stsp proc_id = PROC_AUTH;
2029 13b2bc37 2022-10-23 stsp daemonize = 0;
2032 13b2bc37 2022-10-23 stsp confpath = optarg;
2035 d93ecf7d 2022-12-14 stsp proc_id = PROC_LISTEN;
2038 13b2bc37 2022-10-23 stsp noaction = 1;
2041 ba97b2d7 2024-03-20 stsp proc_id = PROC_NOTIFY;
2044 6f319063 2022-10-27 stsp repo_path = realpath(optarg, NULL);
2045 6f319063 2022-10-27 stsp if (repo_path == NULL)
2046 6f319063 2022-10-27 stsp fatal("realpath '%s'", optarg);
2049 13b2bc37 2022-10-23 stsp proc_id = PROC_REPO_READ;
2052 b0614828 2023-06-19 stsp proc_id = PROC_SESSION_READ;
2055 b0614828 2023-06-19 stsp proc_id = PROC_SESSION_WRITE;
2058 6f319063 2022-10-27 stsp if (verbosity < 3)
2059 6f319063 2022-10-27 stsp verbosity++;
2062 13b2bc37 2022-10-23 stsp proc_id = PROC_REPO_WRITE;
2069 13b2bc37 2022-10-23 stsp argc -= optind;
2070 13b2bc37 2022-10-23 stsp argv += optind;
2072 13b2bc37 2022-10-23 stsp if (argc != 0)
2075 b50a2b46 2022-12-29 stsp if (geteuid() && (proc_id == PROC_GOTD || proc_id == PROC_LISTEN))
2076 13b2bc37 2022-10-23 stsp fatalx("need root privileges");
2078 4b3827cd 2023-07-08 stsp if (parse_config(confpath, proc_id, &gotd) != 0)
2081 13b2bc37 2022-10-23 stsp pw = getpwnam(gotd.user_name);
2082 13b2bc37 2022-10-23 stsp if (pw == NULL)
2083 898c8f8f 2022-12-29 op fatalx("user %s not found", gotd.user_name);
2085 f4e8c21c 2023-01-17 op if (pw->pw_uid == 0)
2086 f4e8c21c 2023-01-17 op fatalx("cannot run %s as the superuser", getprogname());
2088 f4e8c21c 2023-01-17 op if (noaction) {
2089 f4e8c21c 2023-01-17 op fprintf(stderr, "configuration OK\n");
2093 f4e8c21c 2023-01-17 op gotd.argv0 = argv0;
2094 f4e8c21c 2023-01-17 op gotd.daemonize = daemonize;
2095 f4e8c21c 2023-01-17 op gotd.verbosity = verbosity;
2096 f4e8c21c 2023-01-17 op gotd.confpath = confpath;
2098 f4e8c21c 2023-01-17 op /* Require an absolute path in argv[0] for reliable re-exec. */
2099 f4e8c21c 2023-01-17 op if (!got_path_is_absolute(argv0))
2100 f4e8c21c 2023-01-17 op fatalx("bad path \"%s\": must be an absolute path", argv0);
2102 f4e8c21c 2023-01-17 op log_init(daemonize ? 0 : 1, LOG_DAEMON);
2103 f4e8c21c 2023-01-17 op log_setverbose(verbosity);
2105 b1142068 2022-12-05 stsp if (proc_id == PROC_GOTD) {
2106 d93ecf7d 2022-12-14 stsp snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2107 d93ecf7d 2022-12-14 stsp arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
2108 d93ecf7d 2022-12-14 stsp if (daemonize && daemon(1, 0) == -1)
2109 d93ecf7d 2022-12-14 stsp fatal("daemon");
2110 f7eb3370 2023-01-23 stsp gotd.pid = getpid();
2111 f7eb3370 2023-01-23 stsp start_listener(argv0, confpath, daemonize, verbosity);
2112 ba97b2d7 2024-03-20 stsp start_notifier(argv0, confpath, daemonize, verbosity);
2113 d93ecf7d 2022-12-14 stsp } else if (proc_id == PROC_LISTEN) {
2114 d93ecf7d 2022-12-14 stsp snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2115 b1142068 2022-12-05 stsp if (verbosity) {
2116 b1142068 2022-12-05 stsp log_info("socket: %s", gotd.unix_socket_path);
2117 b1142068 2022-12-05 stsp log_info("user: %s", pw->pw_name);
2120 13b2bc37 2022-10-23 stsp fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
2121 6f854dde 2023-01-04 stsp pw->pw_gid);
2122 13b2bc37 2022-10-23 stsp if (fd == -1) {
2123 13b2bc37 2022-10-23 stsp fatal("cannot listen on unix socket %s",
2124 13b2bc37 2022-10-23 stsp gotd.unix_socket_path);
2126 5e25db14 2022-12-29 stsp } else if (proc_id == PROC_AUTH) {
2127 5e25db14 2022-12-29 stsp snprintf(title, sizeof(title), "%s %s",
2128 5e25db14 2022-12-29 stsp gotd_proc_names[proc_id], repo_path);
2129 ae7c1b78 2023-01-10 stsp } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE ||
2130 b0614828 2023-06-19 stsp proc_id == PROC_SESSION_READ || proc_id == PROC_SESSION_WRITE) {
2131 13b2bc37 2022-10-23 stsp error = got_repo_pack_fds_open(&pack_fds);
2132 13b2bc37 2022-10-23 stsp if (error != NULL)
2133 13b2bc37 2022-10-23 stsp fatalx("cannot open pack tempfiles: %s", error->msg);
2134 13b2bc37 2022-10-23 stsp error = got_repo_temp_fds_open(&temp_fds);
2135 13b2bc37 2022-10-23 stsp if (error != NULL)
2136 13b2bc37 2022-10-23 stsp fatalx("cannot open pack tempfiles: %s", error->msg);
2137 13b2bc37 2022-10-23 stsp if (repo_path == NULL)
2138 13b2bc37 2022-10-23 stsp fatalx("repository path not specified");
2139 13b2bc37 2022-10-23 stsp snprintf(title, sizeof(title), "%s %s",
2140 13b2bc37 2022-10-23 stsp gotd_proc_names[proc_id], repo_path);
2141 ba97b2d7 2024-03-20 stsp } else if (proc_id == PROC_NOTIFY) {
2142 ba97b2d7 2024-03-20 stsp snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2143 ba97b2d7 2024-03-20 stsp if (gethostname(hostname, sizeof(hostname)) == -1)
2144 ba97b2d7 2024-03-20 stsp fatal("gethostname");
2145 ba97b2d7 2024-03-20 stsp if (asprintf(&default_sender, "%s@%s",
2146 ba97b2d7 2024-03-20 stsp pw->pw_name, hostname) == -1)
2147 ba97b2d7 2024-03-20 stsp fatal("asprintf");
2149 13b2bc37 2022-10-23 stsp fatal("invalid process id %d", proc_id);
2151 13b2bc37 2022-10-23 stsp setproctitle("%s", title);
2152 13b2bc37 2022-10-23 stsp log_procinit(title);
2154 13b2bc37 2022-10-23 stsp /* Drop root privileges. */
2155 13b2bc37 2022-10-23 stsp if (setgid(pw->pw_gid) == -1)
2156 13b2bc37 2022-10-23 stsp fatal("setgid %d failed", pw->pw_gid);
2157 13b2bc37 2022-10-23 stsp if (setuid(pw->pw_uid) == -1)
2158 13b2bc37 2022-10-23 stsp fatal("setuid %d failed", pw->pw_uid);
2160 13b2bc37 2022-10-23 stsp event_init();
2162 13b2bc37 2022-10-23 stsp switch (proc_id) {
2163 13b2bc37 2022-10-23 stsp case PROC_GOTD:
2164 13b2bc37 2022-10-23 stsp #ifndef PROFILE
2165 ae7c1b78 2023-01-10 stsp /* "exec" promise will be limited to argv[0] via unveil(2). */
2166 ae7c1b78 2023-01-10 stsp if (pledge("stdio proc exec sendfd recvfd unveil", NULL) == -1)
2167 13b2bc37 2022-10-23 stsp err(1, "pledge");
2170 d93ecf7d 2022-12-14 stsp case PROC_LISTEN:
2171 d93ecf7d 2022-12-14 stsp #ifndef PROFILE
2172 77f619a8 2023-01-04 stsp if (pledge("stdio sendfd unix unveil", NULL) == -1)
2173 d93ecf7d 2022-12-14 stsp err(1, "pledge");
2176 77f619a8 2023-01-04 stsp * Ensure that AF_UNIX bind(2) cannot be used with any other
2177 77f619a8 2023-01-04 stsp * sockets by revoking all filesystem access via unveil(2).
2179 77f619a8 2023-01-04 stsp apply_unveil_none();
2181 40b85cca 2023-01-03 stsp listen_main(title, fd, gotd.connection_limits,
2182 40b85cca 2023-01-03 stsp gotd.nconnection_limits);
2183 d93ecf7d 2022-12-14 stsp /* NOTREACHED */
2185 5e25db14 2022-12-29 stsp case PROC_AUTH:
2186 5e25db14 2022-12-29 stsp #ifndef PROFILE
2187 44587340 2022-12-30 stsp if (pledge("stdio getpw recvfd unix unveil", NULL) == -1)
2188 5e25db14 2022-12-29 stsp err(1, "pledge");
2191 44587340 2022-12-30 stsp * We need the "unix" pledge promise for getpeername(2) only.
2192 44587340 2022-12-30 stsp * Ensure that AF_UNIX bind(2) cannot be used by revoking all
2193 44587340 2022-12-30 stsp * filesystem access via unveil(2). Access to password database
2194 44587340 2022-12-30 stsp * files will still work since "getpw" bypasses unveil(2).
2196 44587340 2022-12-30 stsp apply_unveil_none();
2198 5e25db14 2022-12-29 stsp auth_main(title, &gotd.repos, repo_path);
2199 5e25db14 2022-12-29 stsp /* NOTREACHED */
2201 b0614828 2023-06-19 stsp case PROC_SESSION_READ:
2202 b0614828 2023-06-19 stsp case PROC_SESSION_WRITE:
2203 ae7c1b78 2023-01-10 stsp #ifndef PROFILE
2205 ae7c1b78 2023-01-10 stsp * The "recvfd" promise is only needed during setup and
2206 ae7c1b78 2023-01-10 stsp * will be removed in a later pledge(2) call.
2208 ae7c1b78 2023-01-10 stsp if (pledge("stdio rpath wpath cpath recvfd sendfd fattr flock "
2209 ae7c1b78 2023-01-10 stsp "unveil", NULL) == -1)
2210 ae7c1b78 2023-01-10 stsp err(1, "pledge");
2212 b0614828 2023-06-19 stsp if (proc_id == PROC_SESSION_READ)
2213 b0614828 2023-06-19 stsp apply_unveil_repo_readonly(repo_path, 1);
2215 b0614828 2023-06-19 stsp apply_unveil_repo_readwrite(repo_path);
2216 ba97b2d7 2024-03-20 stsp repo = gotd_find_repo_by_path(repo_path, &gotd);
2217 ba97b2d7 2024-03-20 stsp if (repo == NULL)
2218 ba97b2d7 2024-03-20 stsp fatalx("no repository for path %s", repo_path);
2220 ae7c1b78 2023-01-10 stsp session_main(title, repo_path, pack_fds, temp_fds,
2221 ba97b2d7 2024-03-20 stsp &gotd.request_timeout, repo, proc_id);
2222 ae7c1b78 2023-01-10 stsp /* NOTREACHED */
2224 13b2bc37 2022-10-23 stsp case PROC_REPO_READ:
2225 abe89edb 2023-11-16 stsp set_max_datasize();
2226 13b2bc37 2022-10-23 stsp #ifndef PROFILE
2227 eec68231 2022-12-14 stsp if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2228 13b2bc37 2022-10-23 stsp err(1, "pledge");
2230 b0614828 2023-06-19 stsp apply_unveil_repo_readonly(repo_path, 0);
2231 eec68231 2022-12-14 stsp repo_read_main(title, repo_path, pack_fds, temp_fds);
2232 13b2bc37 2022-10-23 stsp /* NOTREACHED */
2234 13b2bc37 2022-10-23 stsp case PROC_REPO_WRITE:
2235 abe89edb 2023-11-16 stsp set_max_datasize();
2237 ba97b2d7 2024-03-20 stsp diff_f1 = got_opentemp();
2238 ba97b2d7 2024-03-20 stsp if (diff_f1 == NULL)
2239 ba97b2d7 2024-03-20 stsp fatal("got_opentemp");
2240 ba97b2d7 2024-03-20 stsp diff_f2 = got_opentemp();
2241 ba97b2d7 2024-03-20 stsp if (diff_f2 == NULL)
2242 ba97b2d7 2024-03-20 stsp fatal("got_opentemp");
2243 ba97b2d7 2024-03-20 stsp diff_fd1 = got_opentempfd();
2244 ba97b2d7 2024-03-20 stsp if (diff_fd1 == -1)
2245 ba97b2d7 2024-03-20 stsp fatal("got_opentempfd");
2246 ba97b2d7 2024-03-20 stsp diff_fd2 = got_opentempfd();
2247 ba97b2d7 2024-03-20 stsp if (diff_fd2 == -1)
2248 ba97b2d7 2024-03-20 stsp fatal("got_opentempfd");
2249 13b2bc37 2022-10-23 stsp #ifndef PROFILE
2250 eec68231 2022-12-14 stsp if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2251 13b2bc37 2022-10-23 stsp err(1, "pledge");
2253 b0614828 2023-06-19 stsp apply_unveil_repo_readonly(repo_path, 0);
2254 9afa3de2 2023-04-04 stsp repo = gotd_find_repo_by_path(repo_path, &gotd);
2255 9afa3de2 2023-04-04 stsp if (repo == NULL)
2256 9afa3de2 2023-04-04 stsp fatalx("no repository for path %s", repo_path);
2257 9afa3de2 2023-04-04 stsp repo_write_main(title, repo_path, pack_fds, temp_fds,
2258 ba97b2d7 2024-03-20 stsp diff_f1, diff_f2, diff_fd1, diff_fd2,
2259 9afa3de2 2023-04-04 stsp &repo->protected_tag_namespaces,
2260 9afa3de2 2023-04-04 stsp &repo->protected_branch_namespaces,
2261 9afa3de2 2023-04-04 stsp &repo->protected_branches);
2262 13b2bc37 2022-10-23 stsp /* NOTREACHED */
2264 ba97b2d7 2024-03-20 stsp case PROC_NOTIFY:
2265 ba97b2d7 2024-03-20 stsp #ifndef PROFILE
2266 ba97b2d7 2024-03-20 stsp if (pledge("stdio proc exec recvfd unveil", NULL) == -1)
2267 ba97b2d7 2024-03-20 stsp err(1, "pledge");
2270 ba97b2d7 2024-03-20 stsp * Limit "exec" promise to notification helpers via unveil(2).
2272 ba97b2d7 2024-03-20 stsp unveil_notification_helpers();
2274 ba97b2d7 2024-03-20 stsp notify_main(title, &gotd.repos, default_sender);
2275 ba97b2d7 2024-03-20 stsp /* NOTREACHED */
2278 13b2bc37 2022-10-23 stsp fatal("invalid process id %d", proc_id);
2281 13b2bc37 2022-10-23 stsp if (proc_id != PROC_GOTD)
2282 13b2bc37 2022-10-23 stsp fatal("invalid process id %d", proc_id);
2284 839338f6 2023-06-22 op evtimer_set(&gotd.listen_proc->tmo, kill_proc_timeout,
2285 839338f6 2023-06-22 op gotd.listen_proc);
2286 ba97b2d7 2024-03-20 stsp if (gotd.notify_proc) {
2287 ba97b2d7 2024-03-20 stsp evtimer_set(&gotd.notify_proc->tmo, kill_proc_timeout,
2288 ba97b2d7 2024-03-20 stsp gotd.notify_proc);
2291 ae7c1b78 2023-01-10 stsp apply_unveil_selfexec();
2293 13b2bc37 2022-10-23 stsp signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
2294 13b2bc37 2022-10-23 stsp signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
2295 13b2bc37 2022-10-23 stsp signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
2296 13b2bc37 2022-10-23 stsp signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
2297 839338f6 2023-06-22 op signal_set(&evsigchld, SIGCHLD, gotd_sighdlr, NULL);
2298 13b2bc37 2022-10-23 stsp signal(SIGPIPE, SIG_IGN);
2300 13b2bc37 2022-10-23 stsp signal_add(&evsigint, NULL);
2301 13b2bc37 2022-10-23 stsp signal_add(&evsigterm, NULL);
2302 13b2bc37 2022-10-23 stsp signal_add(&evsighup, NULL);
2303 13b2bc37 2022-10-23 stsp signal_add(&evsigusr1, NULL);
2304 839338f6 2023-06-22 op signal_add(&evsigchld, NULL);
2306 c929736a 2023-06-22 op gotd_imsg_event_add(&gotd.listen_proc->iev);
2307 ba97b2d7 2024-03-20 stsp if (gotd.notify_proc)
2308 ba97b2d7 2024-03-20 stsp gotd_imsg_event_add(&gotd.notify_proc->iev);
2310 13b2bc37 2022-10-23 stsp event_dispatch();
2312 13b2bc37 2022-10-23 stsp free(repo_path);
2313 ba97b2d7 2024-03-20 stsp free(default_sender);
2314 ae7c1b78 2023-01-10 stsp gotd_shutdown();