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>
26 13b2bc37 2022-10-23 stsp #include <fcntl.h>
27 13b2bc37 2022-10-23 stsp #include <err.h>
28 13b2bc37 2022-10-23 stsp #include <errno.h>
29 13b2bc37 2022-10-23 stsp #include <event.h>
30 13b2bc37 2022-10-23 stsp #include <limits.h>
31 13b2bc37 2022-10-23 stsp #include <pwd.h>
32 13b2bc37 2022-10-23 stsp #include <imsg.h>
33 13b2bc37 2022-10-23 stsp #include <sha1.h>
34 5822e79e 2023-02-23 op #include <sha2.h>
35 13b2bc37 2022-10-23 stsp #include <signal.h>
36 13b2bc37 2022-10-23 stsp #include <siphash.h>
37 13b2bc37 2022-10-23 stsp #include <stdarg.h>
38 13b2bc37 2022-10-23 stsp #include <stdio.h>
39 13b2bc37 2022-10-23 stsp #include <stdlib.h>
40 13b2bc37 2022-10-23 stsp #include <string.h>
41 13b2bc37 2022-10-23 stsp #include <syslog.h>
42 13b2bc37 2022-10-23 stsp #include <unistd.h>
44 13b2bc37 2022-10-23 stsp #include "got_error.h"
45 13b2bc37 2022-10-23 stsp #include "got_opentemp.h"
46 13b2bc37 2022-10-23 stsp #include "got_path.h"
47 13b2bc37 2022-10-23 stsp #include "got_repository.h"
48 13b2bc37 2022-10-23 stsp #include "got_object.h"
49 13b2bc37 2022-10-23 stsp #include "got_reference.h"
51 13b2bc37 2022-10-23 stsp #include "got_lib_delta.h"
52 13b2bc37 2022-10-23 stsp #include "got_lib_object.h"
53 13b2bc37 2022-10-23 stsp #include "got_lib_object_cache.h"
54 53bf0b54 2023-02-23 op #include "got_lib_hash.h"
55 13b2bc37 2022-10-23 stsp #include "got_lib_gitproto.h"
56 13b2bc37 2022-10-23 stsp #include "got_lib_pack.h"
57 13b2bc37 2022-10-23 stsp #include "got_lib_repository.h"
59 13b2bc37 2022-10-23 stsp #include "gotd.h"
60 13b2bc37 2022-10-23 stsp #include "log.h"
61 d93ecf7d 2022-12-14 stsp #include "listen.h"
62 0ccf3acb 2022-11-16 stsp #include "auth.h"
63 ae7c1b78 2023-01-10 stsp #include "session.h"
64 13b2bc37 2022-10-23 stsp #include "repo_read.h"
65 13b2bc37 2022-10-23 stsp #include "repo_write.h"
67 13b2bc37 2022-10-23 stsp #ifndef nitems
68 13b2bc37 2022-10-23 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
71 eac23c30 2023-01-10 stsp enum gotd_client_state {
72 eac23c30 2023-01-10 stsp GOTD_CLIENT_STATE_NEW,
73 eac23c30 2023-01-10 stsp GOTD_CLIENT_STATE_ACCESS_GRANTED,
76 13b2bc37 2022-10-23 stsp struct gotd_client {
77 13b2bc37 2022-10-23 stsp STAILQ_ENTRY(gotd_client) entry;
78 13b2bc37 2022-10-23 stsp enum gotd_client_state state;
79 13b2bc37 2022-10-23 stsp uint32_t id;
81 13b2bc37 2022-10-23 stsp struct gotd_imsgev iev;
82 13b2bc37 2022-10-23 stsp struct event tmo;
85 f7a854cf 2023-01-10 stsp struct gotd_child_proc *repo;
86 5e25db14 2022-12-29 stsp struct gotd_child_proc *auth;
87 ae7c1b78 2023-01-10 stsp struct gotd_child_proc *session;
88 5e25db14 2022-12-29 stsp int required_auth;
90 13b2bc37 2022-10-23 stsp STAILQ_HEAD(gotd_clients, gotd_client);
92 13b2bc37 2022-10-23 stsp static struct gotd_clients gotd_clients[GOTD_CLIENT_TABLE_SIZE];
93 13b2bc37 2022-10-23 stsp static SIPHASH_KEY clients_hash_key;
94 13b2bc37 2022-10-23 stsp volatile int client_cnt;
95 ef4e2f01 2022-12-29 stsp static struct timeval auth_timeout = { 5, 0 };
96 13b2bc37 2022-10-23 stsp static struct gotd gotd;
98 13b2bc37 2022-10-23 stsp void gotd_sighdlr(int sig, short event, void *arg);
99 f1752522 2022-10-29 stsp static void gotd_shutdown(void);
100 ae7c1b78 2023-01-10 stsp static const struct got_error *start_session_child(struct gotd_client *,
101 ae7c1b78 2023-01-10 stsp struct gotd_repo *, char *, const char *, int, int);
102 b50a2b46 2022-12-29 stsp static const struct got_error *start_repo_child(struct gotd_client *,
103 b50a2b46 2022-12-29 stsp enum gotd_procid, struct gotd_repo *, char *, const char *, int, int);
104 5e25db14 2022-12-29 stsp static const struct got_error *start_auth_child(struct gotd_client *, int,
105 5e25db14 2022-12-29 stsp struct gotd_repo *, char *, const char *, int, int);
106 b50a2b46 2022-12-29 stsp static void kill_proc(struct gotd_child_proc *, int);
108 13b2bc37 2022-10-23 stsp __dead static void
111 e9e01966 2023-01-18 stsp fprintf(stderr, "usage: %s [-dnv] [-f config-file]\n", getprogname());
116 13b2bc37 2022-10-23 stsp unix_socket_listen(const char *unix_socket_path, uid_t uid, gid_t gid)
118 13b2bc37 2022-10-23 stsp struct sockaddr_un sun;
119 13b2bc37 2022-10-23 stsp int fd = -1;
120 13b2bc37 2022-10-23 stsp mode_t old_umask, mode;
122 13b2bc37 2022-10-23 stsp fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
123 13b2bc37 2022-10-23 stsp if (fd == -1) {
124 13b2bc37 2022-10-23 stsp log_warn("socket");
128 13b2bc37 2022-10-23 stsp sun.sun_family = AF_UNIX;
129 13b2bc37 2022-10-23 stsp if (strlcpy(sun.sun_path, unix_socket_path,
130 13b2bc37 2022-10-23 stsp sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
131 13b2bc37 2022-10-23 stsp log_warnx("%s: name too long", unix_socket_path);
136 13b2bc37 2022-10-23 stsp if (unlink(unix_socket_path) == -1) {
137 13b2bc37 2022-10-23 stsp if (errno != ENOENT) {
138 13b2bc37 2022-10-23 stsp log_warn("unlink %s", unix_socket_path);
144 13b2bc37 2022-10-23 stsp old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
145 6f854dde 2023-01-04 stsp mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
147 13b2bc37 2022-10-23 stsp if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
148 13b2bc37 2022-10-23 stsp log_warn("bind: %s", unix_socket_path);
150 13b2bc37 2022-10-23 stsp umask(old_umask);
154 13b2bc37 2022-10-23 stsp umask(old_umask);
156 13b2bc37 2022-10-23 stsp if (chmod(unix_socket_path, mode) == -1) {
157 13b2bc37 2022-10-23 stsp log_warn("chmod %o %s", mode, unix_socket_path);
159 13b2bc37 2022-10-23 stsp unlink(unix_socket_path);
163 13b2bc37 2022-10-23 stsp if (chown(unix_socket_path, uid, gid) == -1) {
164 13b2bc37 2022-10-23 stsp log_warn("chown %s uid=%d gid=%d", unix_socket_path, uid, gid);
166 13b2bc37 2022-10-23 stsp unlink(unix_socket_path);
170 13b2bc37 2022-10-23 stsp if (listen(fd, GOTD_UNIX_SOCKET_BACKLOG) == -1) {
171 13b2bc37 2022-10-23 stsp log_warn("listen");
173 13b2bc37 2022-10-23 stsp unlink(unix_socket_path);
180 13b2bc37 2022-10-23 stsp static uint64_t
181 13b2bc37 2022-10-23 stsp client_hash(uint32_t client_id)
183 13b2bc37 2022-10-23 stsp return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
186 13b2bc37 2022-10-23 stsp static void
187 13b2bc37 2022-10-23 stsp add_client(struct gotd_client *client)
189 13b2bc37 2022-10-23 stsp uint64_t slot = client_hash(client->id) % nitems(gotd_clients);
190 13b2bc37 2022-10-23 stsp STAILQ_INSERT_HEAD(&gotd_clients[slot], client, entry);
191 13b2bc37 2022-10-23 stsp client_cnt++;
194 13b2bc37 2022-10-23 stsp static struct gotd_client *
195 13b2bc37 2022-10-23 stsp find_client(uint32_t client_id)
197 13b2bc37 2022-10-23 stsp uint64_t slot;
198 13b2bc37 2022-10-23 stsp struct gotd_client *c;
200 13b2bc37 2022-10-23 stsp slot = client_hash(client_id) % nitems(gotd_clients);
201 13b2bc37 2022-10-23 stsp STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
202 13b2bc37 2022-10-23 stsp if (c->id == client_id)
206 13b2bc37 2022-10-23 stsp return NULL;
209 b50a2b46 2022-12-29 stsp static struct gotd_client *
210 b50a2b46 2022-12-29 stsp find_client_by_proc_fd(int fd)
212 b50a2b46 2022-12-29 stsp uint64_t slot;
214 b50a2b46 2022-12-29 stsp for (slot = 0; slot < nitems(gotd_clients); slot++) {
215 b50a2b46 2022-12-29 stsp struct gotd_client *c;
217 b50a2b46 2022-12-29 stsp STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
218 f7a854cf 2023-01-10 stsp if (c->repo && c->repo->iev.ibuf.fd == fd)
220 5e25db14 2022-12-29 stsp if (c->auth && c->auth->iev.ibuf.fd == fd)
222 ae7c1b78 2023-01-10 stsp if (c->session && c->session->iev.ibuf.fd == fd)
227 13b2bc37 2022-10-23 stsp return NULL;
231 13b2bc37 2022-10-23 stsp client_is_reading(struct gotd_client *client)
233 f7a854cf 2023-01-10 stsp return (client->required_auth &
234 f7a854cf 2023-01-10 stsp (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) == GOTD_AUTH_READ;
238 13b2bc37 2022-10-23 stsp client_is_writing(struct gotd_client *client)
240 f7a854cf 2023-01-10 stsp return (client->required_auth &
241 f7a854cf 2023-01-10 stsp (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) ==
242 f7a854cf 2023-01-10 stsp (GOTD_AUTH_READ | GOTD_AUTH_WRITE);
245 13b2bc37 2022-10-23 stsp static const struct got_error *
246 13b2bc37 2022-10-23 stsp ensure_client_is_not_writing(struct gotd_client *client)
248 13b2bc37 2022-10-23 stsp if (client_is_writing(client)) {
249 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_BAD_PACKET,
250 13b2bc37 2022-10-23 stsp "uid %d made a read-request but is writing to "
251 13b2bc37 2022-10-23 stsp "a repository", client->euid);
254 13b2bc37 2022-10-23 stsp return NULL;
257 13b2bc37 2022-10-23 stsp static const struct got_error *
258 13b2bc37 2022-10-23 stsp ensure_client_is_not_reading(struct gotd_client *client)
260 13b2bc37 2022-10-23 stsp if (client_is_reading(client)) {
261 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_BAD_PACKET,
262 13b2bc37 2022-10-23 stsp "uid %d made a write-request but is reading from "
263 13b2bc37 2022-10-23 stsp "a repository", client->euid);
266 13b2bc37 2022-10-23 stsp return NULL;
269 b50a2b46 2022-12-29 stsp static void
270 5e25db14 2022-12-29 stsp wait_for_child(pid_t child_pid)
273 b50a2b46 2022-12-29 stsp int status;
275 5e25db14 2022-12-29 stsp log_debug("waiting for child PID %ld to terminate",
276 5e25db14 2022-12-29 stsp (long)child_pid);
279 5e25db14 2022-12-29 stsp pid = waitpid(child_pid, &status, WNOHANG);
280 b50a2b46 2022-12-29 stsp if (pid == -1) {
281 b50a2b46 2022-12-29 stsp if (errno != EINTR && errno != ECHILD)
282 b50a2b46 2022-12-29 stsp fatal("wait");
283 b50a2b46 2022-12-29 stsp } else if (WIFSIGNALED(status)) {
284 b50a2b46 2022-12-29 stsp log_warnx("child PID %ld terminated; signal %d",
285 b50a2b46 2022-12-29 stsp (long)pid, WTERMSIG(status));
287 b50a2b46 2022-12-29 stsp } while (pid != -1 || (pid == -1 && errno == EINTR));
290 ae7c1b78 2023-01-10 stsp static void
291 ae7c1b78 2023-01-10 stsp proc_done(struct gotd_child_proc *proc)
293 ae7c1b78 2023-01-10 stsp event_del(&proc->iev.ev);
294 ae7c1b78 2023-01-10 stsp msgbuf_clear(&proc->iev.ibuf.w);
295 ae7c1b78 2023-01-10 stsp close(proc->iev.ibuf.fd);
296 ae7c1b78 2023-01-10 stsp kill_proc(proc, 0);
297 ae7c1b78 2023-01-10 stsp wait_for_child(proc->pid);
298 ae7c1b78 2023-01-10 stsp free(proc);
301 13b2bc37 2022-10-23 stsp static void
302 5e25db14 2022-12-29 stsp kill_auth_proc(struct gotd_client *client)
304 5e25db14 2022-12-29 stsp struct gotd_child_proc *proc;
306 5e25db14 2022-12-29 stsp if (client->auth == NULL)
309 5e25db14 2022-12-29 stsp proc = client->auth;
310 5e25db14 2022-12-29 stsp client->auth = NULL;
312 ae7c1b78 2023-01-10 stsp proc_done(proc);
315 5e25db14 2022-12-29 stsp static void
316 ae7c1b78 2023-01-10 stsp kill_session_proc(struct gotd_client *client)
318 ae7c1b78 2023-01-10 stsp struct gotd_child_proc *proc;
320 ae7c1b78 2023-01-10 stsp if (client->session == NULL)
323 ae7c1b78 2023-01-10 stsp proc = client->session;
324 ae7c1b78 2023-01-10 stsp client->session = NULL;
326 ae7c1b78 2023-01-10 stsp proc_done(proc);
329 ae7c1b78 2023-01-10 stsp static void
330 13b2bc37 2022-10-23 stsp disconnect(struct gotd_client *client)
332 13b2bc37 2022-10-23 stsp struct gotd_imsg_disconnect idisconnect;
333 f7a854cf 2023-01-10 stsp struct gotd_child_proc *proc = client->repo;
334 b50a2b46 2022-12-29 stsp struct gotd_child_proc *listen_proc = &gotd.listen_proc;
335 13b2bc37 2022-10-23 stsp uint64_t slot;
337 13b2bc37 2022-10-23 stsp log_debug("uid %d: disconnecting", client->euid);
339 5e25db14 2022-12-29 stsp kill_auth_proc(client);
340 ae7c1b78 2023-01-10 stsp kill_session_proc(client);
342 f1752522 2022-10-29 stsp if (proc) {
343 90270f79 2023-02-09 stsp event_del(&proc->iev.ev);
344 b50a2b46 2022-12-29 stsp msgbuf_clear(&proc->iev.ibuf.w);
345 b50a2b46 2022-12-29 stsp close(proc->iev.ibuf.fd);
346 b50a2b46 2022-12-29 stsp kill_proc(proc, 0);
347 5e25db14 2022-12-29 stsp wait_for_child(proc->pid);
348 b50a2b46 2022-12-29 stsp free(proc);
349 b50a2b46 2022-12-29 stsp proc = NULL;
352 90270f79 2023-02-09 stsp idisconnect.client_id = client->id;
353 d93ecf7d 2022-12-14 stsp if (gotd_imsg_compose_event(&listen_proc->iev,
354 d93ecf7d 2022-12-14 stsp GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
355 d93ecf7d 2022-12-14 stsp &idisconnect, sizeof(idisconnect)) == -1)
356 d93ecf7d 2022-12-14 stsp log_warn("imsg compose DISCONNECT");
358 13b2bc37 2022-10-23 stsp slot = client_hash(client->id) % nitems(gotd_clients);
359 13b2bc37 2022-10-23 stsp STAILQ_REMOVE(&gotd_clients[slot], client, gotd_client, entry);
360 13b2bc37 2022-10-23 stsp imsg_clear(&client->iev.ibuf);
361 13b2bc37 2022-10-23 stsp event_del(&client->iev.ev);
362 13b2bc37 2022-10-23 stsp evtimer_del(&client->tmo);
363 ae7c1b78 2023-01-10 stsp if (client->fd != -1)
364 ae7c1b78 2023-01-10 stsp close(client->fd);
365 ae7c1b78 2023-01-10 stsp else if (client->iev.ibuf.fd != -1)
366 ae7c1b78 2023-01-10 stsp close(client->iev.ibuf.fd);
367 13b2bc37 2022-10-23 stsp free(client);
368 13b2bc37 2022-10-23 stsp client_cnt--;
371 13b2bc37 2022-10-23 stsp static void
372 13b2bc37 2022-10-23 stsp disconnect_on_error(struct gotd_client *client, const struct got_error *err)
374 13b2bc37 2022-10-23 stsp struct imsgbuf ibuf;
376 13b2bc37 2022-10-23 stsp log_warnx("uid %d: %s", client->euid, err->msg);
377 ae7c1b78 2023-01-10 stsp if (err->code != GOT_ERR_EOF && client->fd != -1) {
378 13b2bc37 2022-10-23 stsp imsg_init(&ibuf, client->fd);
379 13b2bc37 2022-10-23 stsp gotd_imsg_send_error(&ibuf, 0, PROC_GOTD, err);
380 13b2bc37 2022-10-23 stsp imsg_clear(&ibuf);
382 13b2bc37 2022-10-23 stsp disconnect(client);
385 f1752522 2022-10-29 stsp static const struct got_error *
386 f1752522 2022-10-29 stsp send_repo_info(struct gotd_imsgev *iev, struct gotd_repo *repo)
388 f1752522 2022-10-29 stsp const struct got_error *err = NULL;
389 f1752522 2022-10-29 stsp struct gotd_imsg_info_repo irepo;
391 f1752522 2022-10-29 stsp memset(&irepo, 0, sizeof(irepo));
393 f1752522 2022-10-29 stsp if (strlcpy(irepo.repo_name, repo->name, sizeof(irepo.repo_name))
394 f1752522 2022-10-29 stsp >= sizeof(irepo.repo_name))
395 f1752522 2022-10-29 stsp return got_error_msg(GOT_ERR_NO_SPACE, "repo name too long");
396 f1752522 2022-10-29 stsp if (strlcpy(irepo.repo_path, repo->path, sizeof(irepo.repo_path))
397 f1752522 2022-10-29 stsp >= sizeof(irepo.repo_path))
398 f1752522 2022-10-29 stsp return got_error_msg(GOT_ERR_NO_SPACE, "repo path too long");
400 f1752522 2022-10-29 stsp if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_REPO, PROC_GOTD, -1,
401 f1752522 2022-10-29 stsp &irepo, sizeof(irepo)) == -1) {
402 f1752522 2022-10-29 stsp err = got_error_from_errno("imsg compose INFO_REPO");
404 f1752522 2022-10-29 stsp return err;
407 f1752522 2022-10-29 stsp return NULL;
410 f1752522 2022-10-29 stsp static const struct got_error *
411 f1752522 2022-10-29 stsp send_client_info(struct gotd_imsgev *iev, struct gotd_client *client)
413 f1752522 2022-10-29 stsp const struct got_error *err = NULL;
414 f1752522 2022-10-29 stsp struct gotd_imsg_info_client iclient;
415 f1752522 2022-10-29 stsp struct gotd_child_proc *proc;
417 f1752522 2022-10-29 stsp memset(&iclient, 0, sizeof(iclient));
418 f1752522 2022-10-29 stsp iclient.euid = client->euid;
419 f1752522 2022-10-29 stsp iclient.egid = client->egid;
421 f7a854cf 2023-01-10 stsp proc = client->repo;
422 f1752522 2022-10-29 stsp if (proc) {
423 eec68231 2022-12-14 stsp if (strlcpy(iclient.repo_name, proc->repo_path,
424 f1752522 2022-10-29 stsp sizeof(iclient.repo_name)) >= sizeof(iclient.repo_name)) {
425 f1752522 2022-10-29 stsp return got_error_msg(GOT_ERR_NO_SPACE,
426 f1752522 2022-10-29 stsp "repo name too long");
428 f1752522 2022-10-29 stsp if (client_is_writing(client))
429 f1752522 2022-10-29 stsp iclient.is_writing = 1;
431 ae7c1b78 2023-01-10 stsp iclient.repo_child_pid = proc->pid;
434 ae7c1b78 2023-01-10 stsp if (client->session)
435 ae7c1b78 2023-01-10 stsp iclient.session_child_pid = client->session->pid;
437 f1752522 2022-10-29 stsp if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_CLIENT, PROC_GOTD, -1,
438 f1752522 2022-10-29 stsp &iclient, sizeof(iclient)) == -1) {
439 f1752522 2022-10-29 stsp err = got_error_from_errno("imsg compose INFO_CLIENT");
441 f1752522 2022-10-29 stsp return err;
444 f1752522 2022-10-29 stsp return NULL;
447 f1752522 2022-10-29 stsp static const struct got_error *
448 f1752522 2022-10-29 stsp send_info(struct gotd_client *client)
450 f1752522 2022-10-29 stsp const struct got_error *err = NULL;
451 f1752522 2022-10-29 stsp struct gotd_imsg_info info;
452 f1752522 2022-10-29 stsp uint64_t slot;
453 f1752522 2022-10-29 stsp struct gotd_repo *repo;
455 78433331 2023-01-04 stsp if (client->euid != 0)
456 78433331 2023-01-04 stsp return got_error_set_errno(EPERM, "info");
458 f1752522 2022-10-29 stsp info.pid = gotd.pid;
459 f1752522 2022-10-29 stsp info.verbosity = gotd.verbosity;
460 f1752522 2022-10-29 stsp info.nrepos = gotd.nrepos;
461 f1752522 2022-10-29 stsp info.nclients = client_cnt - 1;
463 f1752522 2022-10-29 stsp if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_INFO, PROC_GOTD, -1,
464 f1752522 2022-10-29 stsp &info, sizeof(info)) == -1) {
465 f1752522 2022-10-29 stsp err = got_error_from_errno("imsg compose INFO");
467 f1752522 2022-10-29 stsp return err;
470 f1752522 2022-10-29 stsp TAILQ_FOREACH(repo, &gotd.repos, entry) {
471 f1752522 2022-10-29 stsp err = send_repo_info(&client->iev, repo);
473 f1752522 2022-10-29 stsp return err;
476 f1752522 2022-10-29 stsp for (slot = 0; slot < nitems(gotd_clients); slot++) {
477 f1752522 2022-10-29 stsp struct gotd_client *c;
478 f1752522 2022-10-29 stsp STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
479 f1752522 2022-10-29 stsp if (c->id == client->id)
481 f1752522 2022-10-29 stsp err = send_client_info(&client->iev, c);
483 f1752522 2022-10-29 stsp return err;
487 f1752522 2022-10-29 stsp return NULL;
490 f1752522 2022-10-29 stsp static const struct got_error *
491 f1752522 2022-10-29 stsp stop_gotd(struct gotd_client *client)
494 f1752522 2022-10-29 stsp if (client->euid != 0)
495 f1752522 2022-10-29 stsp return got_error_set_errno(EPERM, "stop");
497 f1752522 2022-10-29 stsp gotd_shutdown();
498 f1752522 2022-10-29 stsp /* NOTREACHED */
499 0ccf3acb 2022-11-16 stsp return NULL;
502 13b2bc37 2022-10-23 stsp static const struct got_error *
503 ae7c1b78 2023-01-10 stsp start_client_authentication(struct gotd_client *client, struct imsg *imsg)
505 13b2bc37 2022-10-23 stsp const struct got_error *err;
506 13b2bc37 2022-10-23 stsp struct gotd_imsg_list_refs ireq;
507 0ccf3acb 2022-11-16 stsp struct gotd_repo *repo = NULL;
508 13b2bc37 2022-10-23 stsp size_t datalen;
510 13b2bc37 2022-10-23 stsp log_debug("list-refs request from uid %d", client->euid);
512 eac23c30 2023-01-10 stsp if (client->state != GOTD_CLIENT_STATE_NEW)
513 ae7c1b78 2023-01-10 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
514 ae7c1b78 2023-01-10 stsp "unexpected list-refs request received");
516 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
517 13b2bc37 2022-10-23 stsp if (datalen != sizeof(ireq))
518 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
520 13b2bc37 2022-10-23 stsp memcpy(&ireq, imsg->data, datalen);
522 13b2bc37 2022-10-23 stsp if (ireq.client_is_reading) {
523 13b2bc37 2022-10-23 stsp err = ensure_client_is_not_writing(client);
525 13b2bc37 2022-10-23 stsp return err;
526 b09c1279 2023-03-28 stsp repo = gotd_find_repo_by_name(ireq.repo_name, &gotd);
527 0ccf3acb 2022-11-16 stsp if (repo == NULL)
528 0ccf3acb 2022-11-16 stsp return got_error(GOT_ERR_NOT_GIT_REPO);
529 5e25db14 2022-12-29 stsp err = start_auth_child(client, GOTD_AUTH_READ, repo,
530 b50a2b46 2022-12-29 stsp gotd.argv0, gotd.confpath, gotd.daemonize,
531 b50a2b46 2022-12-29 stsp gotd.verbosity);
533 b50a2b46 2022-12-29 stsp return err;
535 13b2bc37 2022-10-23 stsp err = ensure_client_is_not_reading(client);
537 0ccf3acb 2022-11-16 stsp return err;
538 b09c1279 2023-03-28 stsp repo = gotd_find_repo_by_name(ireq.repo_name, &gotd);
539 0ccf3acb 2022-11-16 stsp if (repo == NULL)
540 0ccf3acb 2022-11-16 stsp return got_error(GOT_ERR_NOT_GIT_REPO);
541 5e25db14 2022-12-29 stsp err = start_auth_child(client,
542 5e25db14 2022-12-29 stsp GOTD_AUTH_READ | GOTD_AUTH_WRITE,
543 5e25db14 2022-12-29 stsp repo, gotd.argv0, gotd.confpath, gotd.daemonize,
544 b50a2b46 2022-12-29 stsp gotd.verbosity);
546 b50a2b46 2022-12-29 stsp return err;
549 ae7c1b78 2023-01-10 stsp evtimer_add(&client->tmo, &auth_timeout);
551 ae7c1b78 2023-01-10 stsp /* Flow continues upon authentication successs/failure or timeout. */
552 13b2bc37 2022-10-23 stsp return NULL;
555 13b2bc37 2022-10-23 stsp static void
556 13b2bc37 2022-10-23 stsp gotd_request(int fd, short events, void *arg)
558 13b2bc37 2022-10-23 stsp struct gotd_imsgev *iev = arg;
559 13b2bc37 2022-10-23 stsp struct imsgbuf *ibuf = &iev->ibuf;
560 13b2bc37 2022-10-23 stsp struct gotd_client *client = iev->handler_arg;
561 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
562 13b2bc37 2022-10-23 stsp struct imsg imsg;
565 13b2bc37 2022-10-23 stsp if (events & EV_WRITE) {
566 13b2bc37 2022-10-23 stsp while (ibuf->w.queued) {
567 13b2bc37 2022-10-23 stsp n = msgbuf_write(&ibuf->w);
568 13b2bc37 2022-10-23 stsp if (n == -1 && errno == EPIPE) {
570 13b2bc37 2022-10-23 stsp * The client has closed its socket.
571 13b2bc37 2022-10-23 stsp * This can happen when Git clients are
572 13b2bc37 2022-10-23 stsp * done sending pack file data.
574 13b2bc37 2022-10-23 stsp msgbuf_clear(&ibuf->w);
576 13b2bc37 2022-10-23 stsp } else if (n == -1 && errno != EAGAIN) {
577 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_flush");
578 13b2bc37 2022-10-23 stsp disconnect_on_error(client, err);
581 13b2bc37 2022-10-23 stsp if (n == 0) {
582 13b2bc37 2022-10-23 stsp /* Connection closed. */
583 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_EOF);
584 13b2bc37 2022-10-23 stsp disconnect_on_error(client, err);
589 f1752522 2022-10-29 stsp /* Disconnect gotctl(8) now that messages have been sent. */
590 f1752522 2022-10-29 stsp if (!client_is_reading(client) && !client_is_writing(client)) {
591 f1752522 2022-10-29 stsp disconnect(client);
596 13b2bc37 2022-10-23 stsp if ((events & EV_READ) == 0)
599 13b2bc37 2022-10-23 stsp memset(&imsg, 0, sizeof(imsg));
601 13b2bc37 2022-10-23 stsp while (err == NULL) {
602 13b2bc37 2022-10-23 stsp err = gotd_imsg_recv(&imsg, ibuf, 0);
604 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_PRIVSEP_READ)
605 13b2bc37 2022-10-23 stsp err = NULL;
609 13b2bc37 2022-10-23 stsp evtimer_del(&client->tmo);
611 13b2bc37 2022-10-23 stsp switch (imsg.hdr.type) {
612 f1752522 2022-10-29 stsp case GOTD_IMSG_INFO:
613 f1752522 2022-10-29 stsp err = send_info(client);
615 f1752522 2022-10-29 stsp case GOTD_IMSG_STOP:
616 f1752522 2022-10-29 stsp err = stop_gotd(client);
618 13b2bc37 2022-10-23 stsp case GOTD_IMSG_LIST_REFS:
619 ae7c1b78 2023-01-10 stsp err = start_client_authentication(client, &imsg);
622 ae7c1b78 2023-01-10 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
623 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
627 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
631 b5225f29 2023-01-22 op disconnect_on_error(client, err);
633 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(&client->iev);
637 13b2bc37 2022-10-23 stsp static void
638 ae7c1b78 2023-01-10 stsp gotd_auth_timeout(int fd, short events, void *arg)
640 13b2bc37 2022-10-23 stsp struct gotd_client *client = arg;
642 ae7c1b78 2023-01-10 stsp log_debug("disconnecting uid %d due to authentication timeout",
643 ae7c1b78 2023-01-10 stsp client->euid);
644 13b2bc37 2022-10-23 stsp disconnect(client);
647 d93ecf7d 2022-12-14 stsp static const struct got_error *
648 d93ecf7d 2022-12-14 stsp recv_connect(uint32_t *client_id, struct imsg *imsg)
650 d93ecf7d 2022-12-14 stsp const struct got_error *err = NULL;
651 d93ecf7d 2022-12-14 stsp struct gotd_imsg_connect iconnect;
652 d93ecf7d 2022-12-14 stsp size_t datalen;
653 13b2bc37 2022-10-23 stsp int s = -1;
654 13b2bc37 2022-10-23 stsp struct gotd_client *client = NULL;
656 d93ecf7d 2022-12-14 stsp *client_id = 0;
658 d93ecf7d 2022-12-14 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
659 d93ecf7d 2022-12-14 stsp if (datalen != sizeof(iconnect))
660 d93ecf7d 2022-12-14 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
661 d93ecf7d 2022-12-14 stsp memcpy(&iconnect, imsg->data, sizeof(iconnect));
663 d93ecf7d 2022-12-14 stsp s = imsg->fd;
664 13b2bc37 2022-10-23 stsp if (s == -1) {
665 d93ecf7d 2022-12-14 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
669 d93ecf7d 2022-12-14 stsp if (find_client(iconnect.client_id)) {
670 d93ecf7d 2022-12-14 stsp err = got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
674 13b2bc37 2022-10-23 stsp client = calloc(1, sizeof(*client));
675 13b2bc37 2022-10-23 stsp if (client == NULL) {
676 d93ecf7d 2022-12-14 stsp err = got_error_from_errno("calloc");
680 d93ecf7d 2022-12-14 stsp *client_id = iconnect.client_id;
682 eac23c30 2023-01-10 stsp client->state = GOTD_CLIENT_STATE_NEW;
683 d93ecf7d 2022-12-14 stsp client->id = iconnect.client_id;
684 13b2bc37 2022-10-23 stsp client->fd = s;
686 365cf0f3 2022-12-29 stsp /* The auth process will verify UID/GID for us. */
687 365cf0f3 2022-12-29 stsp client->euid = iconnect.euid;
688 365cf0f3 2022-12-29 stsp client->egid = iconnect.egid;
690 13b2bc37 2022-10-23 stsp imsg_init(&client->iev.ibuf, client->fd);
691 13b2bc37 2022-10-23 stsp client->iev.handler = gotd_request;
692 13b2bc37 2022-10-23 stsp client->iev.events = EV_READ;
693 13b2bc37 2022-10-23 stsp client->iev.handler_arg = client;
695 13b2bc37 2022-10-23 stsp event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
696 13b2bc37 2022-10-23 stsp &client->iev);
697 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(&client->iev);
699 ae7c1b78 2023-01-10 stsp evtimer_set(&client->tmo, gotd_auth_timeout, client);
701 13b2bc37 2022-10-23 stsp add_client(client);
702 13b2bc37 2022-10-23 stsp log_debug("%s: new client uid %d connected on fd %d", __func__,
703 13b2bc37 2022-10-23 stsp client->euid, client->fd);
706 b50a2b46 2022-12-29 stsp struct gotd_child_proc *listen_proc = &gotd.listen_proc;
707 d93ecf7d 2022-12-14 stsp struct gotd_imsg_disconnect idisconnect;
709 d93ecf7d 2022-12-14 stsp idisconnect.client_id = client->id;
710 d93ecf7d 2022-12-14 stsp if (gotd_imsg_compose_event(&listen_proc->iev,
711 d93ecf7d 2022-12-14 stsp GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
712 d93ecf7d 2022-12-14 stsp &idisconnect, sizeof(idisconnect)) == -1)
713 d93ecf7d 2022-12-14 stsp log_warn("imsg compose DISCONNECT");
715 d93ecf7d 2022-12-14 stsp if (s != -1)
719 d93ecf7d 2022-12-14 stsp return err;
722 13b2bc37 2022-10-23 stsp static const char *gotd_proc_names[PROC_MAX] = {
727 13b2bc37 2022-10-23 stsp "repo_read",
728 13b2bc37 2022-10-23 stsp "repo_write"
731 13b2bc37 2022-10-23 stsp static void
732 13b2bc37 2022-10-23 stsp kill_proc(struct gotd_child_proc *proc, int fatal)
734 13b2bc37 2022-10-23 stsp if (fatal) {
735 13b2bc37 2022-10-23 stsp log_warnx("sending SIGKILL to PID %d", proc->pid);
736 13b2bc37 2022-10-23 stsp kill(proc->pid, SIGKILL);
738 13b2bc37 2022-10-23 stsp kill(proc->pid, SIGTERM);
741 13b2bc37 2022-10-23 stsp static void
742 13b2bc37 2022-10-23 stsp gotd_shutdown(void)
744 13b2bc37 2022-10-23 stsp struct gotd_child_proc *proc;
745 b50a2b46 2022-12-29 stsp uint64_t slot;
747 ae7c1b78 2023-01-10 stsp log_debug("shutting down");
748 b50a2b46 2022-12-29 stsp for (slot = 0; slot < nitems(gotd_clients); slot++) {
749 b50a2b46 2022-12-29 stsp struct gotd_client *c, *tmp;
751 b50a2b46 2022-12-29 stsp STAILQ_FOREACH_SAFE(c, &gotd_clients[slot], entry, tmp)
752 b50a2b46 2022-12-29 stsp disconnect(c);
755 b50a2b46 2022-12-29 stsp proc = &gotd.listen_proc;
756 b50a2b46 2022-12-29 stsp msgbuf_clear(&proc->iev.ibuf.w);
757 b50a2b46 2022-12-29 stsp close(proc->iev.ibuf.fd);
758 b50a2b46 2022-12-29 stsp kill_proc(proc, 0);
759 5e25db14 2022-12-29 stsp wait_for_child(proc->pid);
761 13b2bc37 2022-10-23 stsp log_info("terminating");
766 13b2bc37 2022-10-23 stsp gotd_sighdlr(int sig, short event, void *arg)
769 13b2bc37 2022-10-23 stsp * Normal signal handler rules don't apply because libevent
770 13b2bc37 2022-10-23 stsp * decouples for us.
773 13b2bc37 2022-10-23 stsp switch (sig) {
774 13b2bc37 2022-10-23 stsp case SIGHUP:
775 13b2bc37 2022-10-23 stsp log_info("%s: ignoring SIGHUP", __func__);
777 13b2bc37 2022-10-23 stsp case SIGUSR1:
778 13b2bc37 2022-10-23 stsp log_info("%s: ignoring SIGUSR1", __func__);
780 13b2bc37 2022-10-23 stsp case SIGTERM:
781 13b2bc37 2022-10-23 stsp case SIGINT:
782 13b2bc37 2022-10-23 stsp gotd_shutdown();
785 13b2bc37 2022-10-23 stsp fatalx("unexpected signal");
789 13b2bc37 2022-10-23 stsp static const struct got_error *
790 13b2bc37 2022-10-23 stsp ensure_proc_is_reading(struct gotd_client *client,
791 13b2bc37 2022-10-23 stsp struct gotd_child_proc *proc)
793 13b2bc37 2022-10-23 stsp if (!client_is_reading(client)) {
794 13b2bc37 2022-10-23 stsp kill_proc(proc, 1);
795 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_BAD_PACKET,
796 13b2bc37 2022-10-23 stsp "PID %d handled a read-request for uid %d but this "
797 13b2bc37 2022-10-23 stsp "user is not reading from a repository", proc->pid,
798 13b2bc37 2022-10-23 stsp client->euid);
801 13b2bc37 2022-10-23 stsp return NULL;
804 13b2bc37 2022-10-23 stsp static const struct got_error *
805 13b2bc37 2022-10-23 stsp ensure_proc_is_writing(struct gotd_client *client,
806 13b2bc37 2022-10-23 stsp struct gotd_child_proc *proc)
808 13b2bc37 2022-10-23 stsp if (!client_is_writing(client)) {
809 13b2bc37 2022-10-23 stsp kill_proc(proc, 1);
810 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_BAD_PACKET,
811 13b2bc37 2022-10-23 stsp "PID %d handled a write-request for uid %d but this "
812 13b2bc37 2022-10-23 stsp "user is not writing to a repository", proc->pid,
813 13b2bc37 2022-10-23 stsp client->euid);
816 13b2bc37 2022-10-23 stsp return NULL;
820 13b2bc37 2022-10-23 stsp verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
821 13b2bc37 2022-10-23 stsp struct imsg *imsg)
823 13b2bc37 2022-10-23 stsp const struct got_error *err;
824 13b2bc37 2022-10-23 stsp int ret = 0;
826 d93ecf7d 2022-12-14 stsp if (proc->type == PROC_REPO_READ || proc->type == PROC_REPO_WRITE) {
827 f7a854cf 2023-01-10 stsp if (client->repo == NULL)
828 d93ecf7d 2022-12-14 stsp fatalx("no process found for uid %d", client->euid);
829 f7a854cf 2023-01-10 stsp if (proc->pid != client->repo->pid) {
830 d93ecf7d 2022-12-14 stsp kill_proc(proc, 1);
831 d93ecf7d 2022-12-14 stsp log_warnx("received message from PID %d for uid %d, "
832 d93ecf7d 2022-12-14 stsp "while PID %d is the process serving this user",
833 f7a854cf 2023-01-10 stsp proc->pid, client->euid, client->repo->pid);
837 ae7c1b78 2023-01-10 stsp if (proc->type == PROC_SESSION) {
838 ae7c1b78 2023-01-10 stsp if (client->session == NULL) {
839 ae7c1b78 2023-01-10 stsp log_warnx("no session found for uid %d", client->euid);
842 ae7c1b78 2023-01-10 stsp if (proc->pid != client->session->pid) {
843 ae7c1b78 2023-01-10 stsp kill_proc(proc, 1);
844 ae7c1b78 2023-01-10 stsp log_warnx("received message from PID %d for uid %d, "
845 ae7c1b78 2023-01-10 stsp "while PID %d is the process serving this user",
846 ae7c1b78 2023-01-10 stsp proc->pid, client->euid, client->session->pid);
851 13b2bc37 2022-10-23 stsp switch (imsg->hdr.type) {
852 13b2bc37 2022-10-23 stsp case GOTD_IMSG_ERROR:
855 d93ecf7d 2022-12-14 stsp case GOTD_IMSG_CONNECT:
856 d93ecf7d 2022-12-14 stsp if (proc->type != PROC_LISTEN) {
857 d93ecf7d 2022-12-14 stsp err = got_error_fmt(GOT_ERR_BAD_PACKET,
858 d93ecf7d 2022-12-14 stsp "new connection for uid %d from PID %d "
859 d93ecf7d 2022-12-14 stsp "which is not the listen process",
860 5e25db14 2022-12-29 stsp proc->pid, client->euid);
864 5e25db14 2022-12-29 stsp case GOTD_IMSG_ACCESS_GRANTED:
865 5e25db14 2022-12-29 stsp if (proc->type != PROC_AUTH) {
866 5e25db14 2022-12-29 stsp err = got_error_fmt(GOT_ERR_BAD_PACKET,
867 5e25db14 2022-12-29 stsp "authentication of uid %d from PID %d "
868 5e25db14 2022-12-29 stsp "which is not the auth process",
869 d93ecf7d 2022-12-14 stsp proc->pid, client->euid);
873 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_CLIENT_SESSION_READY:
874 ae7c1b78 2023-01-10 stsp if (proc->type != PROC_SESSION) {
875 ae7c1b78 2023-01-10 stsp err = got_error_fmt(GOT_ERR_BAD_PACKET,
876 ae7c1b78 2023-01-10 stsp "unexpected \"ready\" signal from PID %d",
877 ae7c1b78 2023-01-10 stsp proc->pid);
881 b50a2b46 2022-12-29 stsp case GOTD_IMSG_REPO_CHILD_READY:
882 b50a2b46 2022-12-29 stsp if (proc->type != PROC_REPO_READ &&
883 b50a2b46 2022-12-29 stsp proc->type != PROC_REPO_WRITE) {
884 b50a2b46 2022-12-29 stsp err = got_error_fmt(GOT_ERR_BAD_PACKET,
885 b50a2b46 2022-12-29 stsp "unexpected \"ready\" signal from PID %d",
886 b50a2b46 2022-12-29 stsp proc->pid);
890 13b2bc37 2022-10-23 stsp case GOTD_IMSG_PACKFILE_DONE:
891 13b2bc37 2022-10-23 stsp err = ensure_proc_is_reading(client, proc);
893 13b2bc37 2022-10-23 stsp log_warnx("uid %d: %s", client->euid, err->msg);
897 13b2bc37 2022-10-23 stsp case GOTD_IMSG_PACKFILE_INSTALL:
898 13b2bc37 2022-10-23 stsp case GOTD_IMSG_REF_UPDATES_START:
899 13b2bc37 2022-10-23 stsp case GOTD_IMSG_REF_UPDATE:
900 13b2bc37 2022-10-23 stsp err = ensure_proc_is_writing(client, proc);
902 13b2bc37 2022-10-23 stsp log_warnx("uid %d: %s", client->euid, err->msg);
907 13b2bc37 2022-10-23 stsp log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
911 13b2bc37 2022-10-23 stsp return ret;
914 13b2bc37 2022-10-23 stsp static const struct got_error *
915 ae7c1b78 2023-01-10 stsp connect_repo_child(struct gotd_client *client,
916 ae7c1b78 2023-01-10 stsp struct gotd_child_proc *repo_proc)
918 b50a2b46 2022-12-29 stsp static const struct got_error *err;
919 ae7c1b78 2023-01-10 stsp struct gotd_imsgev *session_iev = &client->session->iev;
920 ae7c1b78 2023-01-10 stsp struct gotd_imsg_connect_repo_child ireq;
921 ae7c1b78 2023-01-10 stsp int pipe[2];
923 eac23c30 2023-01-10 stsp if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED)
924 ae7c1b78 2023-01-10 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
925 ae7c1b78 2023-01-10 stsp "unexpected repo child ready signal received");
927 ae7c1b78 2023-01-10 stsp if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
928 ae7c1b78 2023-01-10 stsp PF_UNSPEC, pipe) == -1)
929 ae7c1b78 2023-01-10 stsp fatal("socketpair");
931 ae7c1b78 2023-01-10 stsp memset(&ireq, 0, sizeof(ireq));
932 ae7c1b78 2023-01-10 stsp ireq.client_id = client->id;
933 ae7c1b78 2023-01-10 stsp ireq.proc_id = repo_proc->type;
935 ae7c1b78 2023-01-10 stsp /* Pass repo child pipe to session child process. */
936 ae7c1b78 2023-01-10 stsp if (gotd_imsg_compose_event(session_iev, GOTD_IMSG_CONNECT_REPO_CHILD,
937 ae7c1b78 2023-01-10 stsp PROC_GOTD, pipe[0], &ireq, sizeof(ireq)) == -1) {
938 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
939 ae7c1b78 2023-01-10 stsp close(pipe[0]);
940 ae7c1b78 2023-01-10 stsp close(pipe[1]);
941 ae7c1b78 2023-01-10 stsp return err;
944 ae7c1b78 2023-01-10 stsp /* Pass session child pipe to repo child process. */
945 ae7c1b78 2023-01-10 stsp if (gotd_imsg_compose_event(&repo_proc->iev,
946 ae7c1b78 2023-01-10 stsp GOTD_IMSG_CONNECT_REPO_CHILD, PROC_GOTD, pipe[1], NULL, 0) == -1) {
947 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
948 ae7c1b78 2023-01-10 stsp close(pipe[1]);
949 ae7c1b78 2023-01-10 stsp return err;
952 13b2bc37 2022-10-23 stsp return NULL;
955 13b2bc37 2022-10-23 stsp static void
956 b50a2b46 2022-12-29 stsp gotd_dispatch_listener(int fd, short event, void *arg)
958 13b2bc37 2022-10-23 stsp struct gotd_imsgev *iev = arg;
959 13b2bc37 2022-10-23 stsp struct imsgbuf *ibuf = &iev->ibuf;
960 b50a2b46 2022-12-29 stsp struct gotd_child_proc *proc = &gotd.listen_proc;
962 b50a2b46 2022-12-29 stsp int shut = 0;
963 b50a2b46 2022-12-29 stsp struct imsg imsg;
965 b50a2b46 2022-12-29 stsp if (proc->iev.ibuf.fd != fd)
966 b50a2b46 2022-12-29 stsp fatalx("%s: unexpected fd %d", __func__, fd);
968 b50a2b46 2022-12-29 stsp if (event & EV_READ) {
969 b50a2b46 2022-12-29 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
970 b50a2b46 2022-12-29 stsp fatal("imsg_read error");
971 b50a2b46 2022-12-29 stsp if (n == 0) {
972 b50a2b46 2022-12-29 stsp /* Connection closed. */
978 b50a2b46 2022-12-29 stsp if (event & EV_WRITE) {
979 b50a2b46 2022-12-29 stsp n = msgbuf_write(&ibuf->w);
980 b50a2b46 2022-12-29 stsp if (n == -1 && errno != EAGAIN)
981 b50a2b46 2022-12-29 stsp fatal("msgbuf_write");
982 b50a2b46 2022-12-29 stsp if (n == 0) {
983 b50a2b46 2022-12-29 stsp /* Connection closed. */
990 b50a2b46 2022-12-29 stsp const struct got_error *err = NULL;
991 b50a2b46 2022-12-29 stsp struct gotd_client *client = NULL;
992 b50a2b46 2022-12-29 stsp uint32_t client_id = 0;
993 b50a2b46 2022-12-29 stsp int do_disconnect = 0;
995 b50a2b46 2022-12-29 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
996 b50a2b46 2022-12-29 stsp fatal("%s: imsg_get error", __func__);
997 b50a2b46 2022-12-29 stsp if (n == 0) /* No more messages. */
1000 b50a2b46 2022-12-29 stsp switch (imsg.hdr.type) {
1001 b50a2b46 2022-12-29 stsp case GOTD_IMSG_ERROR:
1002 b50a2b46 2022-12-29 stsp do_disconnect = 1;
1003 b50a2b46 2022-12-29 stsp err = gotd_imsg_recv_error(&client_id, &imsg);
1005 b50a2b46 2022-12-29 stsp case GOTD_IMSG_CONNECT:
1006 b50a2b46 2022-12-29 stsp err = recv_connect(&client_id, &imsg);
1009 b50a2b46 2022-12-29 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1013 b50a2b46 2022-12-29 stsp client = find_client(client_id);
1014 b50a2b46 2022-12-29 stsp if (client == NULL) {
1015 b50a2b46 2022-12-29 stsp log_warnx("%s: client not found", __func__);
1016 b50a2b46 2022-12-29 stsp imsg_free(&imsg);
1021 b50a2b46 2022-12-29 stsp log_warnx("uid %d: %s", client->euid, err->msg);
1023 b50a2b46 2022-12-29 stsp if (do_disconnect) {
1025 b50a2b46 2022-12-29 stsp disconnect_on_error(client, err);
1027 b50a2b46 2022-12-29 stsp disconnect(client);
1030 b50a2b46 2022-12-29 stsp imsg_free(&imsg);
1033 b50a2b46 2022-12-29 stsp if (!shut) {
1034 b50a2b46 2022-12-29 stsp gotd_imsg_event_add(iev);
1036 b50a2b46 2022-12-29 stsp /* This pipe is dead. Remove its event handler */
1037 b50a2b46 2022-12-29 stsp event_del(&iev->ev);
1038 b50a2b46 2022-12-29 stsp event_loopexit(NULL);
1042 b50a2b46 2022-12-29 stsp static void
1043 5e25db14 2022-12-29 stsp gotd_dispatch_auth_child(int fd, short event, void *arg)
1045 5e25db14 2022-12-29 stsp const struct got_error *err = NULL;
1046 5e25db14 2022-12-29 stsp struct gotd_imsgev *iev = arg;
1047 5e25db14 2022-12-29 stsp struct imsgbuf *ibuf = &iev->ibuf;
1048 5e25db14 2022-12-29 stsp struct gotd_client *client;
1049 5e25db14 2022-12-29 stsp struct gotd_repo *repo = NULL;
1050 5e25db14 2022-12-29 stsp ssize_t n;
1051 5e25db14 2022-12-29 stsp int shut = 0;
1052 5e25db14 2022-12-29 stsp struct imsg imsg;
1053 5e25db14 2022-12-29 stsp uint32_t client_id = 0;
1054 5e25db14 2022-12-29 stsp int do_disconnect = 0;
1056 5e25db14 2022-12-29 stsp client = find_client_by_proc_fd(fd);
1057 ae0cca99 2023-02-09 stsp if (client == NULL) {
1058 ae0cca99 2023-02-09 stsp /* Can happen during process teardown. */
1059 ae0cca99 2023-02-09 stsp warnx("cannot find client for fd %d", fd);
1061 ae0cca99 2023-02-09 stsp goto done;
1064 5e25db14 2022-12-29 stsp if (client->auth == NULL)
1065 5e25db14 2022-12-29 stsp fatalx("cannot find auth child process for fd %d", fd);
1067 5e25db14 2022-12-29 stsp if (event & EV_READ) {
1068 5e25db14 2022-12-29 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1069 5e25db14 2022-12-29 stsp fatal("imsg_read error");
1070 5e25db14 2022-12-29 stsp if (n == 0) {
1071 5e25db14 2022-12-29 stsp /* Connection closed. */
1073 5e25db14 2022-12-29 stsp goto done;
1077 5e25db14 2022-12-29 stsp if (event & EV_WRITE) {
1078 5e25db14 2022-12-29 stsp n = msgbuf_write(&ibuf->w);
1079 5e25db14 2022-12-29 stsp if (n == -1 && errno != EAGAIN)
1080 5e25db14 2022-12-29 stsp fatal("msgbuf_write");
1081 5e25db14 2022-12-29 stsp if (n == 0) {
1082 5e25db14 2022-12-29 stsp /* Connection closed. */
1085 5e25db14 2022-12-29 stsp goto done;
1088 5e25db14 2022-12-29 stsp if (client->auth->iev.ibuf.fd != fd)
1089 5e25db14 2022-12-29 stsp fatalx("%s: unexpected fd %d", __func__, fd);
1091 5e25db14 2022-12-29 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
1092 5e25db14 2022-12-29 stsp fatal("%s: imsg_get error", __func__);
1093 5e25db14 2022-12-29 stsp if (n == 0) /* No more messages. */
1096 5e25db14 2022-12-29 stsp evtimer_del(&client->tmo);
1098 5e25db14 2022-12-29 stsp switch (imsg.hdr.type) {
1099 5e25db14 2022-12-29 stsp case GOTD_IMSG_ERROR:
1100 5e25db14 2022-12-29 stsp do_disconnect = 1;
1101 5e25db14 2022-12-29 stsp err = gotd_imsg_recv_error(&client_id, &imsg);
1103 5e25db14 2022-12-29 stsp case GOTD_IMSG_ACCESS_GRANTED:
1104 eac23c30 2023-01-10 stsp client->state = GOTD_CLIENT_STATE_ACCESS_GRANTED;
1107 5e25db14 2022-12-29 stsp do_disconnect = 1;
1108 5e25db14 2022-12-29 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1112 5e25db14 2022-12-29 stsp if (!verify_imsg_src(client, client->auth, &imsg)) {
1113 5e25db14 2022-12-29 stsp do_disconnect = 1;
1114 5e25db14 2022-12-29 stsp log_debug("dropping imsg type %d from PID %d",
1115 5e25db14 2022-12-29 stsp imsg.hdr.type, client->auth->pid);
1117 5e25db14 2022-12-29 stsp imsg_free(&imsg);
1119 5e25db14 2022-12-29 stsp if (do_disconnect) {
1121 5e25db14 2022-12-29 stsp disconnect_on_error(client, err);
1123 5e25db14 2022-12-29 stsp disconnect(client);
1127 b09c1279 2023-03-28 stsp repo = gotd_find_repo_by_name(client->auth->repo_name, &gotd);
1128 5e25db14 2022-12-29 stsp if (repo == NULL) {
1129 5e25db14 2022-12-29 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
1130 5e25db14 2022-12-29 stsp goto done;
1132 5e25db14 2022-12-29 stsp kill_auth_proc(client);
1134 d30e708b 2023-01-27 op log_info("authenticated uid %d for repository %s",
1135 5e25db14 2022-12-29 stsp client->euid, repo->name);
1137 ae7c1b78 2023-01-10 stsp err = start_session_child(client, repo, gotd.argv0,
1138 7fdc3e58 2022-12-30 mark gotd.confpath, gotd.daemonize, gotd.verbosity);
1140 ae7c1b78 2023-01-10 stsp goto done;
1143 5e25db14 2022-12-29 stsp log_warnx("uid %d: %s", client->euid, err->msg);
1145 5e25db14 2022-12-29 stsp /* We might have killed the auth process by now. */
1146 5e25db14 2022-12-29 stsp if (client->auth != NULL) {
1147 5e25db14 2022-12-29 stsp if (!shut) {
1148 5e25db14 2022-12-29 stsp gotd_imsg_event_add(iev);
1150 5e25db14 2022-12-29 stsp /* This pipe is dead. Remove its event handler */
1151 5e25db14 2022-12-29 stsp event_del(&iev->ev);
1156 ae7c1b78 2023-01-10 stsp static const struct got_error *
1157 ae7c1b78 2023-01-10 stsp connect_session(struct gotd_client *client)
1159 ae7c1b78 2023-01-10 stsp const struct got_error *err = NULL;
1160 ae7c1b78 2023-01-10 stsp struct gotd_imsg_connect iconnect;
1163 ae7c1b78 2023-01-10 stsp memset(&iconnect, 0, sizeof(iconnect));
1165 ae7c1b78 2023-01-10 stsp s = dup(client->fd);
1166 ae7c1b78 2023-01-10 stsp if (s == -1)
1167 ae7c1b78 2023-01-10 stsp return got_error_from_errno("dup");
1169 ae7c1b78 2023-01-10 stsp iconnect.client_id = client->id;
1170 ae7c1b78 2023-01-10 stsp iconnect.euid = client->euid;
1171 ae7c1b78 2023-01-10 stsp iconnect.egid = client->egid;
1173 ae7c1b78 2023-01-10 stsp if (gotd_imsg_compose_event(&client->session->iev, GOTD_IMSG_CONNECT,
1174 ae7c1b78 2023-01-10 stsp PROC_GOTD, s, &iconnect, sizeof(iconnect)) == -1) {
1175 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("imsg compose CONNECT");
1177 ae7c1b78 2023-01-10 stsp return err;
1181 ae7c1b78 2023-01-10 stsp * We are no longer interested in messages from this client.
1182 ae7c1b78 2023-01-10 stsp * Further client requests will be handled by the session process.
1184 ae7c1b78 2023-01-10 stsp msgbuf_clear(&client->iev.ibuf.w);
1185 ae7c1b78 2023-01-10 stsp imsg_clear(&client->iev.ibuf);
1186 ae7c1b78 2023-01-10 stsp event_del(&client->iev.ev);
1187 ae7c1b78 2023-01-10 stsp client->fd = -1; /* will be closed via copy in client->iev.ibuf.fd */
1189 ae7c1b78 2023-01-10 stsp return NULL;
1192 5e25db14 2022-12-29 stsp static void
1193 ae7c1b78 2023-01-10 stsp gotd_dispatch_client_session(int fd, short event, void *arg)
1195 b50a2b46 2022-12-29 stsp struct gotd_imsgev *iev = arg;
1196 b50a2b46 2022-12-29 stsp struct imsgbuf *ibuf = &iev->ibuf;
1197 13b2bc37 2022-10-23 stsp struct gotd_child_proc *proc = NULL;
1198 b50a2b46 2022-12-29 stsp struct gotd_client *client = NULL;
1199 13b2bc37 2022-10-23 stsp ssize_t n;
1200 13b2bc37 2022-10-23 stsp int shut = 0;
1201 13b2bc37 2022-10-23 stsp struct imsg imsg;
1203 ae7c1b78 2023-01-10 stsp client = find_client_by_proc_fd(fd);
1204 ae0cca99 2023-02-09 stsp if (client == NULL) {
1205 ae0cca99 2023-02-09 stsp /* Can happen during process teardown. */
1206 ae0cca99 2023-02-09 stsp warnx("cannot find client for fd %d", fd);
1208 ae0cca99 2023-02-09 stsp goto done;
1211 13b2bc37 2022-10-23 stsp if (event & EV_READ) {
1212 13b2bc37 2022-10-23 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1213 13b2bc37 2022-10-23 stsp fatal("imsg_read error");
1214 13b2bc37 2022-10-23 stsp if (n == 0) {
1215 13b2bc37 2022-10-23 stsp /* Connection closed. */
1217 13b2bc37 2022-10-23 stsp goto done;
1221 13b2bc37 2022-10-23 stsp if (event & EV_WRITE) {
1222 13b2bc37 2022-10-23 stsp n = msgbuf_write(&ibuf->w);
1223 13b2bc37 2022-10-23 stsp if (n == -1 && errno != EAGAIN)
1224 13b2bc37 2022-10-23 stsp fatal("msgbuf_write");
1225 13b2bc37 2022-10-23 stsp if (n == 0) {
1226 13b2bc37 2022-10-23 stsp /* Connection closed. */
1228 13b2bc37 2022-10-23 stsp goto done;
1232 ae7c1b78 2023-01-10 stsp proc = client->session;
1233 ae7c1b78 2023-01-10 stsp if (proc == NULL)
1234 ae7c1b78 2023-01-10 stsp fatalx("cannot find session child process for fd %d", fd);
1236 ae7c1b78 2023-01-10 stsp for (;;) {
1237 ae7c1b78 2023-01-10 stsp const struct got_error *err = NULL;
1238 ae7c1b78 2023-01-10 stsp uint32_t client_id = 0;
1239 ae7c1b78 2023-01-10 stsp int do_disconnect = 0, do_start_repo_child = 0;
1241 ae7c1b78 2023-01-10 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
1242 ae7c1b78 2023-01-10 stsp fatal("%s: imsg_get error", __func__);
1243 ae7c1b78 2023-01-10 stsp if (n == 0) /* No more messages. */
1246 ae7c1b78 2023-01-10 stsp switch (imsg.hdr.type) {
1247 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_ERROR:
1248 ae7c1b78 2023-01-10 stsp do_disconnect = 1;
1249 ae7c1b78 2023-01-10 stsp err = gotd_imsg_recv_error(&client_id, &imsg);
1251 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_CLIENT_SESSION_READY:
1252 eac23c30 2023-01-10 stsp if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED) {
1253 ae7c1b78 2023-01-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1256 ae7c1b78 2023-01-10 stsp do_start_repo_child = 1;
1258 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_DISCONNECT:
1259 ae7c1b78 2023-01-10 stsp do_disconnect = 1;
1262 ae7c1b78 2023-01-10 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1266 ae7c1b78 2023-01-10 stsp if (!verify_imsg_src(client, proc, &imsg)) {
1267 ae7c1b78 2023-01-10 stsp log_debug("dropping imsg type %d from PID %d",
1268 ae7c1b78 2023-01-10 stsp imsg.hdr.type, proc->pid);
1269 ae7c1b78 2023-01-10 stsp imsg_free(&imsg);
1273 ae7c1b78 2023-01-10 stsp log_warnx("uid %d: %s", client->euid, err->msg);
1275 ae7c1b78 2023-01-10 stsp if (do_start_repo_child) {
1276 ae7c1b78 2023-01-10 stsp struct gotd_repo *repo;
1277 b09c1279 2023-03-28 stsp const char *name = client->session->repo_name;
1279 b09c1279 2023-03-28 stsp repo = gotd_find_repo_by_name(name, &gotd);
1280 ae7c1b78 2023-01-10 stsp if (repo != NULL) {
1281 ae7c1b78 2023-01-10 stsp enum gotd_procid proc_type;
1283 ae7c1b78 2023-01-10 stsp if (client->required_auth & GOTD_AUTH_WRITE)
1284 ae7c1b78 2023-01-10 stsp proc_type = PROC_REPO_WRITE;
1286 ae7c1b78 2023-01-10 stsp proc_type = PROC_REPO_READ;
1288 ae7c1b78 2023-01-10 stsp err = start_repo_child(client, proc_type, repo,
1289 ae7c1b78 2023-01-10 stsp gotd.argv0, gotd.confpath, gotd.daemonize,
1290 ae7c1b78 2023-01-10 stsp gotd.verbosity);
1292 ae7c1b78 2023-01-10 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
1294 ae7c1b78 2023-01-10 stsp if (err) {
1295 ae7c1b78 2023-01-10 stsp log_warnx("uid %d: %s", client->euid, err->msg);
1296 ae7c1b78 2023-01-10 stsp do_disconnect = 1;
1300 ae7c1b78 2023-01-10 stsp if (do_disconnect) {
1302 ae7c1b78 2023-01-10 stsp disconnect_on_error(client, err);
1304 ae7c1b78 2023-01-10 stsp disconnect(client);
1307 ae7c1b78 2023-01-10 stsp imsg_free(&imsg);
1310 ae7c1b78 2023-01-10 stsp if (!shut) {
1311 ae7c1b78 2023-01-10 stsp gotd_imsg_event_add(iev);
1313 ae7c1b78 2023-01-10 stsp /* This pipe is dead. Remove its event handler */
1314 ae7c1b78 2023-01-10 stsp event_del(&iev->ev);
1315 ae7c1b78 2023-01-10 stsp disconnect(client);
1319 ae7c1b78 2023-01-10 stsp static void
1320 ae7c1b78 2023-01-10 stsp gotd_dispatch_repo_child(int fd, short event, void *arg)
1322 ae7c1b78 2023-01-10 stsp struct gotd_imsgev *iev = arg;
1323 ae7c1b78 2023-01-10 stsp struct imsgbuf *ibuf = &iev->ibuf;
1324 ae7c1b78 2023-01-10 stsp struct gotd_child_proc *proc = NULL;
1325 ae7c1b78 2023-01-10 stsp struct gotd_client *client;
1326 ae7c1b78 2023-01-10 stsp ssize_t n;
1327 ae7c1b78 2023-01-10 stsp int shut = 0;
1328 ae7c1b78 2023-01-10 stsp struct imsg imsg;
1330 b50a2b46 2022-12-29 stsp client = find_client_by_proc_fd(fd);
1331 ae0cca99 2023-02-09 stsp if (client == NULL) {
1332 ae0cca99 2023-02-09 stsp /* Can happen during process teardown. */
1333 ae0cca99 2023-02-09 stsp warnx("cannot find client for fd %d", fd);
1335 ae0cca99 2023-02-09 stsp goto done;
1338 ae7c1b78 2023-01-10 stsp if (event & EV_READ) {
1339 ae7c1b78 2023-01-10 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1340 ae7c1b78 2023-01-10 stsp fatal("imsg_read error");
1341 ae7c1b78 2023-01-10 stsp if (n == 0) {
1342 ae7c1b78 2023-01-10 stsp /* Connection closed. */
1344 ae7c1b78 2023-01-10 stsp goto done;
1348 ae7c1b78 2023-01-10 stsp if (event & EV_WRITE) {
1349 ae7c1b78 2023-01-10 stsp n = msgbuf_write(&ibuf->w);
1350 ae7c1b78 2023-01-10 stsp if (n == -1 && errno != EAGAIN)
1351 ae7c1b78 2023-01-10 stsp fatal("msgbuf_write");
1352 ae7c1b78 2023-01-10 stsp if (n == 0) {
1353 ae7c1b78 2023-01-10 stsp /* Connection closed. */
1355 ae7c1b78 2023-01-10 stsp goto done;
1359 f7a854cf 2023-01-10 stsp proc = client->repo;
1360 13b2bc37 2022-10-23 stsp if (proc == NULL)
1361 13b2bc37 2022-10-23 stsp fatalx("cannot find child process for fd %d", fd);
1363 13b2bc37 2022-10-23 stsp for (;;) {
1364 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
1365 13b2bc37 2022-10-23 stsp uint32_t client_id = 0;
1366 13b2bc37 2022-10-23 stsp int do_disconnect = 0;
1368 13b2bc37 2022-10-23 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
1369 13b2bc37 2022-10-23 stsp fatal("%s: imsg_get error", __func__);
1370 13b2bc37 2022-10-23 stsp if (n == 0) /* No more messages. */
1373 13b2bc37 2022-10-23 stsp switch (imsg.hdr.type) {
1374 13b2bc37 2022-10-23 stsp case GOTD_IMSG_ERROR:
1375 13b2bc37 2022-10-23 stsp do_disconnect = 1;
1376 13b2bc37 2022-10-23 stsp err = gotd_imsg_recv_error(&client_id, &imsg);
1378 b50a2b46 2022-12-29 stsp case GOTD_IMSG_REPO_CHILD_READY:
1379 ae7c1b78 2023-01-10 stsp err = connect_session(client);
1382 ae7c1b78 2023-01-10 stsp err = connect_repo_child(client, proc);
1385 13b2bc37 2022-10-23 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1389 13b2bc37 2022-10-23 stsp if (!verify_imsg_src(client, proc, &imsg)) {
1390 13b2bc37 2022-10-23 stsp log_debug("dropping imsg type %d from PID %d",
1391 13b2bc37 2022-10-23 stsp imsg.hdr.type, proc->pid);
1392 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
1396 13b2bc37 2022-10-23 stsp log_warnx("uid %d: %s", client->euid, err->msg);
1398 13b2bc37 2022-10-23 stsp if (do_disconnect) {
1400 13b2bc37 2022-10-23 stsp disconnect_on_error(client, err);
1402 13b2bc37 2022-10-23 stsp disconnect(client);
1405 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
1408 13b2bc37 2022-10-23 stsp if (!shut) {
1409 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(iev);
1411 13b2bc37 2022-10-23 stsp /* This pipe is dead. Remove its event handler */
1412 13b2bc37 2022-10-23 stsp event_del(&iev->ev);
1413 ae7c1b78 2023-01-10 stsp disconnect(client);
1417 13b2bc37 2022-10-23 stsp static pid_t
1418 eec68231 2022-12-14 stsp start_child(enum gotd_procid proc_id, const char *repo_path,
1419 585362fd 2022-10-31 op char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
1421 585362fd 2022-10-31 op char *argv[11];
1422 13b2bc37 2022-10-23 stsp int argc = 0;
1423 13b2bc37 2022-10-23 stsp pid_t pid;
1425 13b2bc37 2022-10-23 stsp switch (pid = fork()) {
1427 13b2bc37 2022-10-23 stsp fatal("cannot fork");
1431 13b2bc37 2022-10-23 stsp close(fd);
1432 13b2bc37 2022-10-23 stsp return pid;
1435 8c6fc146 2022-11-17 stsp if (fd != GOTD_FILENO_MSG_PIPE) {
1436 8c6fc146 2022-11-17 stsp if (dup2(fd, GOTD_FILENO_MSG_PIPE) == -1)
1437 13b2bc37 2022-10-23 stsp fatal("cannot setup imsg fd");
1438 13b2bc37 2022-10-23 stsp } else if (fcntl(fd, F_SETFD, 0) == -1)
1439 13b2bc37 2022-10-23 stsp fatal("cannot setup imsg fd");
1441 13b2bc37 2022-10-23 stsp argv[argc++] = argv0;
1442 13b2bc37 2022-10-23 stsp switch (proc_id) {
1443 d93ecf7d 2022-12-14 stsp case PROC_LISTEN:
1444 d93ecf7d 2022-12-14 stsp argv[argc++] = (char *)"-L";
1446 5e25db14 2022-12-29 stsp case PROC_AUTH:
1447 5e25db14 2022-12-29 stsp argv[argc++] = (char *)"-A";
1449 ae7c1b78 2023-01-10 stsp case PROC_SESSION:
1450 ae7c1b78 2023-01-10 stsp argv[argc++] = (char *)"-S";
1452 13b2bc37 2022-10-23 stsp case PROC_REPO_READ:
1453 13b2bc37 2022-10-23 stsp argv[argc++] = (char *)"-R";
1455 13b2bc37 2022-10-23 stsp case PROC_REPO_WRITE:
1456 13b2bc37 2022-10-23 stsp argv[argc++] = (char *)"-W";
1459 13b2bc37 2022-10-23 stsp fatalx("invalid process id %d", proc_id);
1462 585362fd 2022-10-31 op argv[argc++] = (char *)"-f";
1463 585362fd 2022-10-31 op argv[argc++] = (char *)confpath;
1465 eec68231 2022-12-14 stsp if (repo_path) {
1466 d93ecf7d 2022-12-14 stsp argv[argc++] = (char *)"-P";
1467 eec68231 2022-12-14 stsp argv[argc++] = (char *)repo_path;
1470 13b2bc37 2022-10-23 stsp if (!daemonize)
1471 13b2bc37 2022-10-23 stsp argv[argc++] = (char *)"-d";
1472 13b2bc37 2022-10-23 stsp if (verbosity > 0)
1473 13b2bc37 2022-10-23 stsp argv[argc++] = (char *)"-v";
1474 13b2bc37 2022-10-23 stsp if (verbosity > 1)
1475 13b2bc37 2022-10-23 stsp argv[argc++] = (char *)"-v";
1476 13b2bc37 2022-10-23 stsp argv[argc++] = NULL;
1478 13b2bc37 2022-10-23 stsp execvp(argv0, argv);
1479 13b2bc37 2022-10-23 stsp fatal("execvp");
1482 13b2bc37 2022-10-23 stsp static void
1483 d93ecf7d 2022-12-14 stsp start_listener(char *argv0, const char *confpath, int daemonize, int verbosity)
1485 b50a2b46 2022-12-29 stsp struct gotd_child_proc *proc = &gotd.listen_proc;
1487 d93ecf7d 2022-12-14 stsp proc->type = PROC_LISTEN;
1489 d93ecf7d 2022-12-14 stsp if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1490 d93ecf7d 2022-12-14 stsp PF_UNSPEC, proc->pipe) == -1)
1491 d93ecf7d 2022-12-14 stsp fatal("socketpair");
1493 d93ecf7d 2022-12-14 stsp proc->pid = start_child(proc->type, NULL, argv0, confpath,
1494 d93ecf7d 2022-12-14 stsp proc->pipe[1], daemonize, verbosity);
1495 d93ecf7d 2022-12-14 stsp imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1496 b50a2b46 2022-12-29 stsp proc->iev.handler = gotd_dispatch_listener;
1497 d93ecf7d 2022-12-14 stsp proc->iev.events = EV_READ;
1498 d93ecf7d 2022-12-14 stsp proc->iev.handler_arg = NULL;
1501 b50a2b46 2022-12-29 stsp static const struct got_error *
1502 ae7c1b78 2023-01-10 stsp start_session_child(struct gotd_client *client, struct gotd_repo *repo,
1503 ae7c1b78 2023-01-10 stsp char *argv0, const char *confpath, int daemonize, int verbosity)
1505 ae7c1b78 2023-01-10 stsp struct gotd_child_proc *proc;
1507 ae7c1b78 2023-01-10 stsp proc = calloc(1, sizeof(*proc));
1508 ae7c1b78 2023-01-10 stsp if (proc == NULL)
1509 ae7c1b78 2023-01-10 stsp return got_error_from_errno("calloc");
1511 ae7c1b78 2023-01-10 stsp proc->type = PROC_SESSION;
1512 ae7c1b78 2023-01-10 stsp if (strlcpy(proc->repo_name, repo->name,
1513 ae7c1b78 2023-01-10 stsp sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1514 ae7c1b78 2023-01-10 stsp fatalx("repository name too long: %s", repo->name);
1515 ae7c1b78 2023-01-10 stsp log_debug("starting client uid %d session for repository %s",
1516 ae7c1b78 2023-01-10 stsp client->euid, repo->name);
1517 ae7c1b78 2023-01-10 stsp if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1518 ae7c1b78 2023-01-10 stsp sizeof(proc->repo_path))
1519 ae7c1b78 2023-01-10 stsp fatalx("repository path too long: %s", repo->path);
1520 ae7c1b78 2023-01-10 stsp if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1521 ae7c1b78 2023-01-10 stsp PF_UNSPEC, proc->pipe) == -1)
1522 ae7c1b78 2023-01-10 stsp fatal("socketpair");
1523 ae7c1b78 2023-01-10 stsp proc->pid = start_child(proc->type, proc->repo_path, argv0,
1524 ae7c1b78 2023-01-10 stsp confpath, proc->pipe[1], daemonize, verbosity);
1525 ae7c1b78 2023-01-10 stsp imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1526 ae7c1b78 2023-01-10 stsp log_debug("proc %s %s is on fd %d",
1527 ae7c1b78 2023-01-10 stsp gotd_proc_names[proc->type], proc->repo_path,
1528 ae7c1b78 2023-01-10 stsp proc->pipe[0]);
1529 ae7c1b78 2023-01-10 stsp proc->iev.handler = gotd_dispatch_client_session;
1530 ae7c1b78 2023-01-10 stsp proc->iev.events = EV_READ;
1531 ae7c1b78 2023-01-10 stsp proc->iev.handler_arg = NULL;
1532 ae7c1b78 2023-01-10 stsp event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1533 ae7c1b78 2023-01-10 stsp gotd_dispatch_client_session, &proc->iev);
1534 ae7c1b78 2023-01-10 stsp gotd_imsg_event_add(&proc->iev);
1536 ae7c1b78 2023-01-10 stsp client->session = proc;
1537 ae7c1b78 2023-01-10 stsp return NULL;
1540 ae7c1b78 2023-01-10 stsp static const struct got_error *
1541 b50a2b46 2022-12-29 stsp start_repo_child(struct gotd_client *client, enum gotd_procid proc_type,
1542 b50a2b46 2022-12-29 stsp struct gotd_repo *repo, char *argv0, const char *confpath,
1543 585362fd 2022-10-31 op int daemonize, int verbosity)
1545 13b2bc37 2022-10-23 stsp struct gotd_child_proc *proc;
1547 b50a2b46 2022-12-29 stsp if (proc_type != PROC_REPO_READ && proc_type != PROC_REPO_WRITE)
1548 b50a2b46 2022-12-29 stsp return got_error_msg(GOT_ERR_NOT_IMPL, "bad process type");
1550 b50a2b46 2022-12-29 stsp proc = calloc(1, sizeof(*proc));
1551 b50a2b46 2022-12-29 stsp if (proc == NULL)
1552 b50a2b46 2022-12-29 stsp return got_error_from_errno("calloc");
1554 b50a2b46 2022-12-29 stsp proc->type = proc_type;
1555 b50a2b46 2022-12-29 stsp if (strlcpy(proc->repo_name, repo->name,
1556 b50a2b46 2022-12-29 stsp sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1557 b50a2b46 2022-12-29 stsp fatalx("repository name too long: %s", repo->name);
1558 b50a2b46 2022-12-29 stsp log_debug("starting %s for repository %s",
1559 b50a2b46 2022-12-29 stsp proc->type == PROC_REPO_READ ? "reader" : "writer", repo->name);
1560 9b7f22a6 2023-01-08 stsp if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1561 9b7f22a6 2023-01-08 stsp sizeof(proc->repo_path))
1562 9b7f22a6 2023-01-08 stsp fatalx("repository path too long: %s", repo->path);
1563 b50a2b46 2022-12-29 stsp if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1564 b50a2b46 2022-12-29 stsp PF_UNSPEC, proc->pipe) == -1)
1565 b50a2b46 2022-12-29 stsp fatal("socketpair");
1566 b50a2b46 2022-12-29 stsp proc->pid = start_child(proc->type, proc->repo_path, argv0,
1567 b50a2b46 2022-12-29 stsp confpath, proc->pipe[1], daemonize, verbosity);
1568 b50a2b46 2022-12-29 stsp imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1569 b50a2b46 2022-12-29 stsp log_debug("proc %s %s is on fd %d",
1570 b50a2b46 2022-12-29 stsp gotd_proc_names[proc->type], proc->repo_path,
1571 b50a2b46 2022-12-29 stsp proc->pipe[0]);
1572 b50a2b46 2022-12-29 stsp proc->iev.handler = gotd_dispatch_repo_child;
1573 b50a2b46 2022-12-29 stsp proc->iev.events = EV_READ;
1574 b50a2b46 2022-12-29 stsp proc->iev.handler_arg = NULL;
1575 b50a2b46 2022-12-29 stsp event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1576 b50a2b46 2022-12-29 stsp gotd_dispatch_repo_child, &proc->iev);
1577 b50a2b46 2022-12-29 stsp gotd_imsg_event_add(&proc->iev);
1579 f7a854cf 2023-01-10 stsp client->repo = proc;
1580 5e25db14 2022-12-29 stsp return NULL;
1583 5e25db14 2022-12-29 stsp static const struct got_error *
1584 5e25db14 2022-12-29 stsp start_auth_child(struct gotd_client *client, int required_auth,
1585 5e25db14 2022-12-29 stsp struct gotd_repo *repo, char *argv0, const char *confpath,
1586 5e25db14 2022-12-29 stsp int daemonize, int verbosity)
1588 365cf0f3 2022-12-29 stsp const struct got_error *err = NULL;
1589 5e25db14 2022-12-29 stsp struct gotd_child_proc *proc;
1590 5e25db14 2022-12-29 stsp struct gotd_imsg_auth iauth;
1593 5e25db14 2022-12-29 stsp memset(&iauth, 0, sizeof(iauth));
1595 365cf0f3 2022-12-29 stsp fd = dup(client->fd);
1596 365cf0f3 2022-12-29 stsp if (fd == -1)
1597 365cf0f3 2022-12-29 stsp return got_error_from_errno("dup");
1599 5e25db14 2022-12-29 stsp proc = calloc(1, sizeof(*proc));
1600 365cf0f3 2022-12-29 stsp if (proc == NULL) {
1601 365cf0f3 2022-12-29 stsp err = got_error_from_errno("calloc");
1602 365cf0f3 2022-12-29 stsp close(fd);
1603 365cf0f3 2022-12-29 stsp return err;
1606 5e25db14 2022-12-29 stsp proc->type = PROC_AUTH;
1607 5e25db14 2022-12-29 stsp if (strlcpy(proc->repo_name, repo->name,
1608 5e25db14 2022-12-29 stsp sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1609 5e25db14 2022-12-29 stsp fatalx("repository name too long: %s", repo->name);
1610 5e25db14 2022-12-29 stsp log_debug("starting auth for uid %d repository %s",
1611 5e25db14 2022-12-29 stsp client->euid, repo->name);
1612 9b7f22a6 2023-01-08 stsp if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1613 9b7f22a6 2023-01-08 stsp sizeof(proc->repo_path))
1614 9b7f22a6 2023-01-08 stsp fatalx("repository path too long: %s", repo->path);
1615 5e25db14 2022-12-29 stsp if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1616 5e25db14 2022-12-29 stsp PF_UNSPEC, proc->pipe) == -1)
1617 5e25db14 2022-12-29 stsp fatal("socketpair");
1618 5e25db14 2022-12-29 stsp proc->pid = start_child(proc->type, proc->repo_path, argv0,
1619 5e25db14 2022-12-29 stsp confpath, proc->pipe[1], daemonize, verbosity);
1620 5e25db14 2022-12-29 stsp imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1621 5e25db14 2022-12-29 stsp log_debug("proc %s %s is on fd %d",
1622 5e25db14 2022-12-29 stsp gotd_proc_names[proc->type], proc->repo_path,
1623 5e25db14 2022-12-29 stsp proc->pipe[0]);
1624 5e25db14 2022-12-29 stsp proc->iev.handler = gotd_dispatch_auth_child;
1625 5e25db14 2022-12-29 stsp proc->iev.events = EV_READ;
1626 5e25db14 2022-12-29 stsp proc->iev.handler_arg = NULL;
1627 5e25db14 2022-12-29 stsp event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1628 5e25db14 2022-12-29 stsp gotd_dispatch_auth_child, &proc->iev);
1629 5e25db14 2022-12-29 stsp gotd_imsg_event_add(&proc->iev);
1631 5e25db14 2022-12-29 stsp iauth.euid = client->euid;
1632 5e25db14 2022-12-29 stsp iauth.egid = client->egid;
1633 5e25db14 2022-12-29 stsp iauth.required_auth = required_auth;
1634 5e25db14 2022-12-29 stsp iauth.client_id = client->id;
1635 5e25db14 2022-12-29 stsp if (gotd_imsg_compose_event(&proc->iev, GOTD_IMSG_AUTHENTICATE,
1636 365cf0f3 2022-12-29 stsp PROC_GOTD, fd, &iauth, sizeof(iauth)) == -1) {
1637 5e25db14 2022-12-29 stsp log_warn("imsg compose AUTHENTICATE");
1638 365cf0f3 2022-12-29 stsp close(fd);
1639 365cf0f3 2022-12-29 stsp /* Let the auth_timeout handler tidy up. */
1642 5e25db14 2022-12-29 stsp client->auth = proc;
1643 5e25db14 2022-12-29 stsp client->required_auth = required_auth;
1644 b50a2b46 2022-12-29 stsp return NULL;
1647 eec68231 2022-12-14 stsp static void
1648 eec68231 2022-12-14 stsp apply_unveil_repo_readonly(const char *repo_path)
1650 eec68231 2022-12-14 stsp if (unveil(repo_path, "r") == -1)
1651 eec68231 2022-12-14 stsp fatal("unveil %s", repo_path);
1653 44587340 2022-12-30 stsp if (unveil(NULL, NULL) == -1)
1654 44587340 2022-12-30 stsp fatal("unveil");
1657 44587340 2022-12-30 stsp static void
1658 ae7c1b78 2023-01-10 stsp apply_unveil_repo_readwrite(const char *repo_path)
1660 ae7c1b78 2023-01-10 stsp if (unveil(repo_path, "rwc") == -1)
1661 ae7c1b78 2023-01-10 stsp fatal("unveil %s", repo_path);
1663 ae7c1b78 2023-01-10 stsp if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1664 ae7c1b78 2023-01-10 stsp fatal("unveil %s", GOT_TMPDIR_STR);
1666 ae7c1b78 2023-01-10 stsp if (unveil(NULL, NULL) == -1)
1667 ae7c1b78 2023-01-10 stsp fatal("unveil");
1670 ae7c1b78 2023-01-10 stsp static void
1671 44587340 2022-12-30 stsp apply_unveil_none(void)
1673 44587340 2022-12-30 stsp if (unveil("/", "") == -1)
1674 44587340 2022-12-30 stsp fatal("unveil");
1676 eec68231 2022-12-14 stsp if (unveil(NULL, NULL) == -1)
1677 eec68231 2022-12-14 stsp fatal("unveil");
1680 13b2bc37 2022-10-23 stsp static void
1681 ae7c1b78 2023-01-10 stsp apply_unveil_selfexec(void)
1683 b50a2b46 2022-12-29 stsp if (unveil(gotd.argv0, "x") == -1)
1684 b50a2b46 2022-12-29 stsp fatal("unveil %s", gotd.argv0);
1686 13b2bc37 2022-10-23 stsp if (unveil(NULL, NULL) == -1)
1687 13b2bc37 2022-10-23 stsp fatal("unveil");
1691 13b2bc37 2022-10-23 stsp main(int argc, char **argv)
1693 13b2bc37 2022-10-23 stsp const struct got_error *error = NULL;
1694 13b2bc37 2022-10-23 stsp int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
1695 13b2bc37 2022-10-23 stsp const char *confpath = GOTD_CONF_PATH;
1696 13b2bc37 2022-10-23 stsp char *argv0 = argv[0];
1697 13b2bc37 2022-10-23 stsp char title[2048];
1698 13b2bc37 2022-10-23 stsp struct passwd *pw = NULL;
1699 13b2bc37 2022-10-23 stsp char *repo_path = NULL;
1700 13b2bc37 2022-10-23 stsp enum gotd_procid proc_id = PROC_GOTD;
1701 13b2bc37 2022-10-23 stsp struct event evsigint, evsigterm, evsighup, evsigusr1;
1702 13b2bc37 2022-10-23 stsp int *pack_fds = NULL, *temp_fds = NULL;
1703 9afa3de2 2023-04-04 stsp struct gotd_repo *repo = NULL;
1705 13b2bc37 2022-10-23 stsp log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
1707 ae7c1b78 2023-01-10 stsp while ((ch = getopt(argc, argv, "Adf:LnP:RSvW")) != -1) {
1708 13b2bc37 2022-10-23 stsp switch (ch) {
1710 5e25db14 2022-12-29 stsp proc_id = PROC_AUTH;
1713 13b2bc37 2022-10-23 stsp daemonize = 0;
1716 13b2bc37 2022-10-23 stsp confpath = optarg;
1719 d93ecf7d 2022-12-14 stsp proc_id = PROC_LISTEN;
1722 13b2bc37 2022-10-23 stsp noaction = 1;
1725 6f319063 2022-10-27 stsp repo_path = realpath(optarg, NULL);
1726 6f319063 2022-10-27 stsp if (repo_path == NULL)
1727 6f319063 2022-10-27 stsp fatal("realpath '%s'", optarg);
1730 13b2bc37 2022-10-23 stsp proc_id = PROC_REPO_READ;
1733 ae7c1b78 2023-01-10 stsp proc_id = PROC_SESSION;
1736 6f319063 2022-10-27 stsp if (verbosity < 3)
1737 6f319063 2022-10-27 stsp verbosity++;
1740 13b2bc37 2022-10-23 stsp proc_id = PROC_REPO_WRITE;
1747 13b2bc37 2022-10-23 stsp argc -= optind;
1748 13b2bc37 2022-10-23 stsp argv += optind;
1750 13b2bc37 2022-10-23 stsp if (argc != 0)
1753 b50a2b46 2022-12-29 stsp if (geteuid() && (proc_id == PROC_GOTD || proc_id == PROC_LISTEN))
1754 13b2bc37 2022-10-23 stsp fatalx("need root privileges");
1756 e9e0377f 2023-03-29 stsp if (parse_config(confpath, proc_id, &gotd, 1) != 0)
1759 13b2bc37 2022-10-23 stsp pw = getpwnam(gotd.user_name);
1760 13b2bc37 2022-10-23 stsp if (pw == NULL)
1761 898c8f8f 2022-12-29 op fatalx("user %s not found", gotd.user_name);
1763 f4e8c21c 2023-01-17 op if (pw->pw_uid == 0)
1764 f4e8c21c 2023-01-17 op fatalx("cannot run %s as the superuser", getprogname());
1766 f4e8c21c 2023-01-17 op if (noaction) {
1767 f4e8c21c 2023-01-17 op fprintf(stderr, "configuration OK\n");
1771 f4e8c21c 2023-01-17 op gotd.argv0 = argv0;
1772 f4e8c21c 2023-01-17 op gotd.daemonize = daemonize;
1773 f4e8c21c 2023-01-17 op gotd.verbosity = verbosity;
1774 f4e8c21c 2023-01-17 op gotd.confpath = confpath;
1776 f4e8c21c 2023-01-17 op /* Require an absolute path in argv[0] for reliable re-exec. */
1777 f4e8c21c 2023-01-17 op if (!got_path_is_absolute(argv0))
1778 f4e8c21c 2023-01-17 op fatalx("bad path \"%s\": must be an absolute path", argv0);
1780 f4e8c21c 2023-01-17 op log_init(daemonize ? 0 : 1, LOG_DAEMON);
1781 f4e8c21c 2023-01-17 op log_setverbose(verbosity);
1783 b1142068 2022-12-05 stsp if (proc_id == PROC_GOTD) {
1784 d93ecf7d 2022-12-14 stsp snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1785 d93ecf7d 2022-12-14 stsp arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
1786 d93ecf7d 2022-12-14 stsp if (daemonize && daemon(1, 0) == -1)
1787 d93ecf7d 2022-12-14 stsp fatal("daemon");
1788 f7eb3370 2023-01-23 stsp gotd.pid = getpid();
1789 f7eb3370 2023-01-23 stsp start_listener(argv0, confpath, daemonize, verbosity);
1790 d93ecf7d 2022-12-14 stsp } else if (proc_id == PROC_LISTEN) {
1791 d93ecf7d 2022-12-14 stsp snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1792 b1142068 2022-12-05 stsp if (verbosity) {
1793 b1142068 2022-12-05 stsp log_info("socket: %s", gotd.unix_socket_path);
1794 b1142068 2022-12-05 stsp log_info("user: %s", pw->pw_name);
1797 13b2bc37 2022-10-23 stsp fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
1798 6f854dde 2023-01-04 stsp pw->pw_gid);
1799 13b2bc37 2022-10-23 stsp if (fd == -1) {
1800 13b2bc37 2022-10-23 stsp fatal("cannot listen on unix socket %s",
1801 13b2bc37 2022-10-23 stsp gotd.unix_socket_path);
1803 5e25db14 2022-12-29 stsp } else if (proc_id == PROC_AUTH) {
1804 5e25db14 2022-12-29 stsp snprintf(title, sizeof(title), "%s %s",
1805 5e25db14 2022-12-29 stsp gotd_proc_names[proc_id], repo_path);
1806 ae7c1b78 2023-01-10 stsp } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE ||
1807 ae7c1b78 2023-01-10 stsp proc_id == PROC_SESSION) {
1808 13b2bc37 2022-10-23 stsp error = got_repo_pack_fds_open(&pack_fds);
1809 13b2bc37 2022-10-23 stsp if (error != NULL)
1810 13b2bc37 2022-10-23 stsp fatalx("cannot open pack tempfiles: %s", error->msg);
1811 13b2bc37 2022-10-23 stsp error = got_repo_temp_fds_open(&temp_fds);
1812 13b2bc37 2022-10-23 stsp if (error != NULL)
1813 13b2bc37 2022-10-23 stsp fatalx("cannot open pack tempfiles: %s", error->msg);
1814 13b2bc37 2022-10-23 stsp if (repo_path == NULL)
1815 13b2bc37 2022-10-23 stsp fatalx("repository path not specified");
1816 13b2bc37 2022-10-23 stsp snprintf(title, sizeof(title), "%s %s",
1817 13b2bc37 2022-10-23 stsp gotd_proc_names[proc_id], repo_path);
1819 13b2bc37 2022-10-23 stsp fatal("invalid process id %d", proc_id);
1821 13b2bc37 2022-10-23 stsp setproctitle("%s", title);
1822 13b2bc37 2022-10-23 stsp log_procinit(title);
1824 13b2bc37 2022-10-23 stsp /* Drop root privileges. */
1825 13b2bc37 2022-10-23 stsp if (setgid(pw->pw_gid) == -1)
1826 13b2bc37 2022-10-23 stsp fatal("setgid %d failed", pw->pw_gid);
1827 13b2bc37 2022-10-23 stsp if (setuid(pw->pw_uid) == -1)
1828 13b2bc37 2022-10-23 stsp fatal("setuid %d failed", pw->pw_uid);
1830 13b2bc37 2022-10-23 stsp event_init();
1832 13b2bc37 2022-10-23 stsp switch (proc_id) {
1833 13b2bc37 2022-10-23 stsp case PROC_GOTD:
1834 13b2bc37 2022-10-23 stsp #ifndef PROFILE
1835 ae7c1b78 2023-01-10 stsp /* "exec" promise will be limited to argv[0] via unveil(2). */
1836 ae7c1b78 2023-01-10 stsp if (pledge("stdio proc exec sendfd recvfd unveil", NULL) == -1)
1837 13b2bc37 2022-10-23 stsp err(1, "pledge");
1840 d93ecf7d 2022-12-14 stsp case PROC_LISTEN:
1841 d93ecf7d 2022-12-14 stsp #ifndef PROFILE
1842 77f619a8 2023-01-04 stsp if (pledge("stdio sendfd unix unveil", NULL) == -1)
1843 d93ecf7d 2022-12-14 stsp err(1, "pledge");
1846 77f619a8 2023-01-04 stsp * Ensure that AF_UNIX bind(2) cannot be used with any other
1847 77f619a8 2023-01-04 stsp * sockets by revoking all filesystem access via unveil(2).
1849 77f619a8 2023-01-04 stsp apply_unveil_none();
1851 40b85cca 2023-01-03 stsp listen_main(title, fd, gotd.connection_limits,
1852 40b85cca 2023-01-03 stsp gotd.nconnection_limits);
1853 d93ecf7d 2022-12-14 stsp /* NOTREACHED */
1855 5e25db14 2022-12-29 stsp case PROC_AUTH:
1856 5e25db14 2022-12-29 stsp #ifndef PROFILE
1857 44587340 2022-12-30 stsp if (pledge("stdio getpw recvfd unix unveil", NULL) == -1)
1858 5e25db14 2022-12-29 stsp err(1, "pledge");
1861 44587340 2022-12-30 stsp * We need the "unix" pledge promise for getpeername(2) only.
1862 44587340 2022-12-30 stsp * Ensure that AF_UNIX bind(2) cannot be used by revoking all
1863 44587340 2022-12-30 stsp * filesystem access via unveil(2). Access to password database
1864 44587340 2022-12-30 stsp * files will still work since "getpw" bypasses unveil(2).
1866 44587340 2022-12-30 stsp apply_unveil_none();
1868 5e25db14 2022-12-29 stsp auth_main(title, &gotd.repos, repo_path);
1869 5e25db14 2022-12-29 stsp /* NOTREACHED */
1871 ae7c1b78 2023-01-10 stsp case PROC_SESSION:
1872 ae7c1b78 2023-01-10 stsp #ifndef PROFILE
1874 ae7c1b78 2023-01-10 stsp * The "recvfd" promise is only needed during setup and
1875 ae7c1b78 2023-01-10 stsp * will be removed in a later pledge(2) call.
1877 ae7c1b78 2023-01-10 stsp if (pledge("stdio rpath wpath cpath recvfd sendfd fattr flock "
1878 ae7c1b78 2023-01-10 stsp "unveil", NULL) == -1)
1879 ae7c1b78 2023-01-10 stsp err(1, "pledge");
1881 ae7c1b78 2023-01-10 stsp apply_unveil_repo_readwrite(repo_path);
1882 ae7c1b78 2023-01-10 stsp session_main(title, repo_path, pack_fds, temp_fds,
1883 ae7c1b78 2023-01-10 stsp &gotd.request_timeout);
1884 ae7c1b78 2023-01-10 stsp /* NOTREACHED */
1886 13b2bc37 2022-10-23 stsp case PROC_REPO_READ:
1887 13b2bc37 2022-10-23 stsp #ifndef PROFILE
1888 eec68231 2022-12-14 stsp if (pledge("stdio rpath recvfd unveil", NULL) == -1)
1889 13b2bc37 2022-10-23 stsp err(1, "pledge");
1891 eec68231 2022-12-14 stsp apply_unveil_repo_readonly(repo_path);
1892 eec68231 2022-12-14 stsp repo_read_main(title, repo_path, pack_fds, temp_fds);
1893 13b2bc37 2022-10-23 stsp /* NOTREACHED */
1895 13b2bc37 2022-10-23 stsp case PROC_REPO_WRITE:
1896 13b2bc37 2022-10-23 stsp #ifndef PROFILE
1897 eec68231 2022-12-14 stsp if (pledge("stdio rpath recvfd unveil", NULL) == -1)
1898 13b2bc37 2022-10-23 stsp err(1, "pledge");
1900 eec68231 2022-12-14 stsp apply_unveil_repo_readonly(repo_path);
1901 9afa3de2 2023-04-04 stsp repo = gotd_find_repo_by_path(repo_path, &gotd);
1902 9afa3de2 2023-04-04 stsp if (repo == NULL)
1903 9afa3de2 2023-04-04 stsp fatalx("no repository for path %s", repo_path);
1904 9afa3de2 2023-04-04 stsp repo_write_main(title, repo_path, pack_fds, temp_fds,
1905 9afa3de2 2023-04-04 stsp &repo->protected_tag_namespaces,
1906 9afa3de2 2023-04-04 stsp &repo->protected_branch_namespaces,
1907 9afa3de2 2023-04-04 stsp &repo->protected_branches);
1908 13b2bc37 2022-10-23 stsp /* NOTREACHED */
1911 13b2bc37 2022-10-23 stsp fatal("invalid process id %d", proc_id);
1914 13b2bc37 2022-10-23 stsp if (proc_id != PROC_GOTD)
1915 13b2bc37 2022-10-23 stsp fatal("invalid process id %d", proc_id);
1917 ae7c1b78 2023-01-10 stsp apply_unveil_selfexec();
1919 13b2bc37 2022-10-23 stsp signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
1920 13b2bc37 2022-10-23 stsp signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
1921 13b2bc37 2022-10-23 stsp signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
1922 13b2bc37 2022-10-23 stsp signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
1923 13b2bc37 2022-10-23 stsp signal(SIGPIPE, SIG_IGN);
1925 13b2bc37 2022-10-23 stsp signal_add(&evsigint, NULL);
1926 13b2bc37 2022-10-23 stsp signal_add(&evsigterm, NULL);
1927 13b2bc37 2022-10-23 stsp signal_add(&evsighup, NULL);
1928 13b2bc37 2022-10-23 stsp signal_add(&evsigusr1, NULL);
1930 b50a2b46 2022-12-29 stsp gotd_imsg_event_add(&gotd.listen_proc.iev);
1932 13b2bc37 2022-10-23 stsp event_dispatch();
1934 13b2bc37 2022-10-23 stsp free(repo_path);
1935 ae7c1b78 2023-01-10 stsp gotd_shutdown();