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 13b2bc37 2022-10-23 stsp #include <signal.h>
35 13b2bc37 2022-10-23 stsp #include <siphash.h>
36 13b2bc37 2022-10-23 stsp #include <stdarg.h>
37 13b2bc37 2022-10-23 stsp #include <stdio.h>
38 13b2bc37 2022-10-23 stsp #include <stdlib.h>
39 13b2bc37 2022-10-23 stsp #include <string.h>
40 13b2bc37 2022-10-23 stsp #include <syslog.h>
41 13b2bc37 2022-10-23 stsp #include <unistd.h>
43 13b2bc37 2022-10-23 stsp #include "got_error.h"
44 13b2bc37 2022-10-23 stsp #include "got_opentemp.h"
45 13b2bc37 2022-10-23 stsp #include "got_path.h"
46 13b2bc37 2022-10-23 stsp #include "got_repository.h"
47 13b2bc37 2022-10-23 stsp #include "got_object.h"
48 13b2bc37 2022-10-23 stsp #include "got_reference.h"
50 13b2bc37 2022-10-23 stsp #include "got_lib_delta.h"
51 13b2bc37 2022-10-23 stsp #include "got_lib_object.h"
52 13b2bc37 2022-10-23 stsp #include "got_lib_object_cache.h"
53 13b2bc37 2022-10-23 stsp #include "got_lib_sha1.h"
54 13b2bc37 2022-10-23 stsp #include "got_lib_gitproto.h"
55 13b2bc37 2022-10-23 stsp #include "got_lib_pack.h"
56 13b2bc37 2022-10-23 stsp #include "got_lib_repository.h"
58 13b2bc37 2022-10-23 stsp #include "gotd.h"
59 13b2bc37 2022-10-23 stsp #include "log.h"
60 d93ecf7d 2022-12-14 stsp #include "listen.h"
61 0ccf3acb 2022-11-16 stsp #include "auth.h"
62 13b2bc37 2022-10-23 stsp #include "repo_read.h"
63 13b2bc37 2022-10-23 stsp #include "repo_write.h"
65 13b2bc37 2022-10-23 stsp #ifndef nitems
66 13b2bc37 2022-10-23 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
69 13b2bc37 2022-10-23 stsp struct gotd_client {
70 13b2bc37 2022-10-23 stsp STAILQ_ENTRY(gotd_client) entry;
71 13b2bc37 2022-10-23 stsp enum gotd_client_state state;
72 13b2bc37 2022-10-23 stsp struct gotd_client_capability *capabilities;
73 13b2bc37 2022-10-23 stsp size_t ncapa_alloc;
74 13b2bc37 2022-10-23 stsp size_t ncapabilities;
75 13b2bc37 2022-10-23 stsp uint32_t id;
77 13b2bc37 2022-10-23 stsp int delta_cache_fd;
78 13b2bc37 2022-10-23 stsp struct gotd_imsgev iev;
79 13b2bc37 2022-10-23 stsp struct event tmo;
82 13b2bc37 2022-10-23 stsp struct gotd_child_proc *repo_read;
83 13b2bc37 2022-10-23 stsp struct gotd_child_proc *repo_write;
84 5e25db14 2022-12-29 stsp struct gotd_child_proc *auth;
85 5e25db14 2022-12-29 stsp int required_auth;
86 13b2bc37 2022-10-23 stsp char *packfile_path;
87 13b2bc37 2022-10-23 stsp char *packidx_path;
88 13b2bc37 2022-10-23 stsp int nref_updates;
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 b50a2b46 2022-12-29 stsp static const struct got_error *start_repo_child(struct gotd_client *,
101 b50a2b46 2022-12-29 stsp enum gotd_procid, struct gotd_repo *, char *, const char *, int, int);
102 5e25db14 2022-12-29 stsp static const struct got_error *start_auth_child(struct gotd_client *, int,
103 5e25db14 2022-12-29 stsp struct gotd_repo *, char *, const char *, int, int);
104 b50a2b46 2022-12-29 stsp static void kill_proc(struct gotd_child_proc *, int);
106 13b2bc37 2022-10-23 stsp __dead static void
109 88dec179 2022-10-24 stsp fprintf(stderr, "usage: %s [-dv] [-f config-file]\n", getprogname());
114 13b2bc37 2022-10-23 stsp unix_socket_listen(const char *unix_socket_path, uid_t uid, gid_t gid)
116 13b2bc37 2022-10-23 stsp struct sockaddr_un sun;
117 13b2bc37 2022-10-23 stsp int fd = -1;
118 13b2bc37 2022-10-23 stsp mode_t old_umask, mode;
120 13b2bc37 2022-10-23 stsp fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
121 13b2bc37 2022-10-23 stsp if (fd == -1) {
122 13b2bc37 2022-10-23 stsp log_warn("socket");
126 13b2bc37 2022-10-23 stsp sun.sun_family = AF_UNIX;
127 13b2bc37 2022-10-23 stsp if (strlcpy(sun.sun_path, unix_socket_path,
128 13b2bc37 2022-10-23 stsp sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
129 13b2bc37 2022-10-23 stsp log_warnx("%s: name too long", unix_socket_path);
134 13b2bc37 2022-10-23 stsp if (unlink(unix_socket_path) == -1) {
135 13b2bc37 2022-10-23 stsp if (errno != ENOENT) {
136 13b2bc37 2022-10-23 stsp log_warn("unlink %s", unix_socket_path);
142 13b2bc37 2022-10-23 stsp old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
143 6f854dde 2023-01-04 stsp mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
145 13b2bc37 2022-10-23 stsp if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
146 13b2bc37 2022-10-23 stsp log_warn("bind: %s", unix_socket_path);
148 13b2bc37 2022-10-23 stsp umask(old_umask);
152 13b2bc37 2022-10-23 stsp umask(old_umask);
154 13b2bc37 2022-10-23 stsp if (chmod(unix_socket_path, mode) == -1) {
155 13b2bc37 2022-10-23 stsp log_warn("chmod %o %s", mode, unix_socket_path);
157 13b2bc37 2022-10-23 stsp unlink(unix_socket_path);
161 13b2bc37 2022-10-23 stsp if (chown(unix_socket_path, uid, gid) == -1) {
162 13b2bc37 2022-10-23 stsp log_warn("chown %s uid=%d gid=%d", unix_socket_path, uid, gid);
164 13b2bc37 2022-10-23 stsp unlink(unix_socket_path);
168 13b2bc37 2022-10-23 stsp if (listen(fd, GOTD_UNIX_SOCKET_BACKLOG) == -1) {
169 13b2bc37 2022-10-23 stsp log_warn("listen");
171 13b2bc37 2022-10-23 stsp unlink(unix_socket_path);
178 13b2bc37 2022-10-23 stsp static uint64_t
179 13b2bc37 2022-10-23 stsp client_hash(uint32_t client_id)
181 13b2bc37 2022-10-23 stsp return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
184 13b2bc37 2022-10-23 stsp static void
185 13b2bc37 2022-10-23 stsp add_client(struct gotd_client *client)
187 13b2bc37 2022-10-23 stsp uint64_t slot = client_hash(client->id) % nitems(gotd_clients);
188 13b2bc37 2022-10-23 stsp STAILQ_INSERT_HEAD(&gotd_clients[slot], client, entry);
189 13b2bc37 2022-10-23 stsp client_cnt++;
192 13b2bc37 2022-10-23 stsp static struct gotd_client *
193 13b2bc37 2022-10-23 stsp find_client(uint32_t client_id)
195 13b2bc37 2022-10-23 stsp uint64_t slot;
196 13b2bc37 2022-10-23 stsp struct gotd_client *c;
198 13b2bc37 2022-10-23 stsp slot = client_hash(client_id) % nitems(gotd_clients);
199 13b2bc37 2022-10-23 stsp STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
200 13b2bc37 2022-10-23 stsp if (c->id == client_id)
204 13b2bc37 2022-10-23 stsp return NULL;
207 13b2bc37 2022-10-23 stsp static struct gotd_child_proc *
208 13b2bc37 2022-10-23 stsp get_client_proc(struct gotd_client *client)
210 13b2bc37 2022-10-23 stsp if (client->repo_read && client->repo_write) {
211 13b2bc37 2022-10-23 stsp fatalx("uid %d is reading and writing in the same session",
212 13b2bc37 2022-10-23 stsp client->euid);
213 13b2bc37 2022-10-23 stsp /* NOTREACHED */
216 13b2bc37 2022-10-23 stsp if (client->repo_read)
217 13b2bc37 2022-10-23 stsp return client->repo_read;
218 13b2bc37 2022-10-23 stsp else if (client->repo_write)
219 13b2bc37 2022-10-23 stsp return client->repo_write;
221 b50a2b46 2022-12-29 stsp return NULL;
224 b50a2b46 2022-12-29 stsp static struct gotd_client *
225 b50a2b46 2022-12-29 stsp find_client_by_proc_fd(int fd)
227 b50a2b46 2022-12-29 stsp uint64_t slot;
229 b50a2b46 2022-12-29 stsp for (slot = 0; slot < nitems(gotd_clients); slot++) {
230 b50a2b46 2022-12-29 stsp struct gotd_client *c;
232 b50a2b46 2022-12-29 stsp STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
233 b50a2b46 2022-12-29 stsp struct gotd_child_proc *proc = get_client_proc(c);
234 b50a2b46 2022-12-29 stsp if (proc && proc->iev.ibuf.fd == fd)
236 5e25db14 2022-12-29 stsp if (c->auth && c->auth->iev.ibuf.fd == fd)
241 13b2bc37 2022-10-23 stsp return NULL;
245 13b2bc37 2022-10-23 stsp client_is_reading(struct gotd_client *client)
247 13b2bc37 2022-10-23 stsp return client->repo_read != NULL;
251 13b2bc37 2022-10-23 stsp client_is_writing(struct gotd_client *client)
253 13b2bc37 2022-10-23 stsp return client->repo_write != NULL;
256 13b2bc37 2022-10-23 stsp static const struct got_error *
257 13b2bc37 2022-10-23 stsp ensure_client_is_reading(struct gotd_client *client)
259 13b2bc37 2022-10-23 stsp if (!client_is_reading(client)) {
260 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_BAD_PACKET,
261 13b2bc37 2022-10-23 stsp "uid %d made a read-request but is not reading from "
262 13b2bc37 2022-10-23 stsp "a repository", client->euid);
265 13b2bc37 2022-10-23 stsp return NULL;
268 13b2bc37 2022-10-23 stsp static const struct got_error *
269 13b2bc37 2022-10-23 stsp ensure_client_is_writing(struct gotd_client *client)
271 13b2bc37 2022-10-23 stsp if (!client_is_writing(client)) {
272 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_BAD_PACKET,
273 13b2bc37 2022-10-23 stsp "uid %d made a write-request but is not writing to "
274 13b2bc37 2022-10-23 stsp "a repository", client->euid);
277 13b2bc37 2022-10-23 stsp return NULL;
280 13b2bc37 2022-10-23 stsp static const struct got_error *
281 13b2bc37 2022-10-23 stsp ensure_client_is_not_writing(struct gotd_client *client)
283 13b2bc37 2022-10-23 stsp if (client_is_writing(client)) {
284 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_BAD_PACKET,
285 13b2bc37 2022-10-23 stsp "uid %d made a read-request but is writing to "
286 13b2bc37 2022-10-23 stsp "a repository", client->euid);
289 13b2bc37 2022-10-23 stsp return NULL;
292 13b2bc37 2022-10-23 stsp static const struct got_error *
293 13b2bc37 2022-10-23 stsp ensure_client_is_not_reading(struct gotd_client *client)
295 13b2bc37 2022-10-23 stsp if (client_is_reading(client)) {
296 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_BAD_PACKET,
297 13b2bc37 2022-10-23 stsp "uid %d made a write-request but is reading from "
298 13b2bc37 2022-10-23 stsp "a repository", client->euid);
301 13b2bc37 2022-10-23 stsp return NULL;
304 b50a2b46 2022-12-29 stsp static void
305 5e25db14 2022-12-29 stsp wait_for_child(pid_t child_pid)
308 b50a2b46 2022-12-29 stsp int status;
310 5e25db14 2022-12-29 stsp log_debug("waiting for child PID %ld to terminate",
311 5e25db14 2022-12-29 stsp (long)child_pid);
314 5e25db14 2022-12-29 stsp pid = waitpid(child_pid, &status, WNOHANG);
315 b50a2b46 2022-12-29 stsp if (pid == -1) {
316 b50a2b46 2022-12-29 stsp if (errno != EINTR && errno != ECHILD)
317 b50a2b46 2022-12-29 stsp fatal("wait");
318 b50a2b46 2022-12-29 stsp } else if (WIFSIGNALED(status)) {
319 b50a2b46 2022-12-29 stsp log_warnx("child PID %ld terminated; signal %d",
320 b50a2b46 2022-12-29 stsp (long)pid, WTERMSIG(status));
322 b50a2b46 2022-12-29 stsp } while (pid != -1 || (pid == -1 && errno == EINTR));
325 13b2bc37 2022-10-23 stsp static void
326 5e25db14 2022-12-29 stsp kill_auth_proc(struct gotd_client *client)
328 5e25db14 2022-12-29 stsp struct gotd_child_proc *proc;
330 5e25db14 2022-12-29 stsp if (client->auth == NULL)
333 5e25db14 2022-12-29 stsp proc = client->auth;
334 5e25db14 2022-12-29 stsp client->auth = NULL;
336 5e25db14 2022-12-29 stsp event_del(&proc->iev.ev);
337 5e25db14 2022-12-29 stsp msgbuf_clear(&proc->iev.ibuf.w);
338 5e25db14 2022-12-29 stsp close(proc->iev.ibuf.fd);
339 5e25db14 2022-12-29 stsp kill_proc(proc, 0);
340 5e25db14 2022-12-29 stsp wait_for_child(proc->pid);
341 5e25db14 2022-12-29 stsp free(proc);
344 5e25db14 2022-12-29 stsp static void
345 13b2bc37 2022-10-23 stsp disconnect(struct gotd_client *client)
347 13b2bc37 2022-10-23 stsp struct gotd_imsg_disconnect idisconnect;
348 13b2bc37 2022-10-23 stsp struct gotd_child_proc *proc = get_client_proc(client);
349 b50a2b46 2022-12-29 stsp struct gotd_child_proc *listen_proc = &gotd.listen_proc;
350 13b2bc37 2022-10-23 stsp uint64_t slot;
352 13b2bc37 2022-10-23 stsp log_debug("uid %d: disconnecting", client->euid);
354 5e25db14 2022-12-29 stsp kill_auth_proc(client);
356 13b2bc37 2022-10-23 stsp idisconnect.client_id = client->id;
357 f1752522 2022-10-29 stsp if (proc) {
358 f1752522 2022-10-29 stsp if (gotd_imsg_compose_event(&proc->iev,
359 f1752522 2022-10-29 stsp GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
360 f1752522 2022-10-29 stsp &idisconnect, sizeof(idisconnect)) == -1)
361 f1752522 2022-10-29 stsp log_warn("imsg compose DISCONNECT");
363 b50a2b46 2022-12-29 stsp msgbuf_clear(&proc->iev.ibuf.w);
364 b50a2b46 2022-12-29 stsp close(proc->iev.ibuf.fd);
365 b50a2b46 2022-12-29 stsp kill_proc(proc, 0);
366 5e25db14 2022-12-29 stsp wait_for_child(proc->pid);
367 b50a2b46 2022-12-29 stsp free(proc);
368 b50a2b46 2022-12-29 stsp proc = NULL;
371 d93ecf7d 2022-12-14 stsp if (gotd_imsg_compose_event(&listen_proc->iev,
372 d93ecf7d 2022-12-14 stsp GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
373 d93ecf7d 2022-12-14 stsp &idisconnect, sizeof(idisconnect)) == -1)
374 d93ecf7d 2022-12-14 stsp log_warn("imsg compose DISCONNECT");
376 13b2bc37 2022-10-23 stsp slot = client_hash(client->id) % nitems(gotd_clients);
377 13b2bc37 2022-10-23 stsp STAILQ_REMOVE(&gotd_clients[slot], client, gotd_client, entry);
378 13b2bc37 2022-10-23 stsp imsg_clear(&client->iev.ibuf);
379 13b2bc37 2022-10-23 stsp event_del(&client->iev.ev);
380 13b2bc37 2022-10-23 stsp evtimer_del(&client->tmo);
381 13b2bc37 2022-10-23 stsp close(client->fd);
382 13b2bc37 2022-10-23 stsp if (client->delta_cache_fd != -1)
383 13b2bc37 2022-10-23 stsp close(client->delta_cache_fd);
384 13b2bc37 2022-10-23 stsp if (client->packfile_path) {
385 13b2bc37 2022-10-23 stsp if (unlink(client->packfile_path) == -1 && errno != ENOENT)
386 13b2bc37 2022-10-23 stsp log_warn("unlink %s: ", client->packfile_path);
387 13b2bc37 2022-10-23 stsp free(client->packfile_path);
389 13b2bc37 2022-10-23 stsp if (client->packidx_path) {
390 13b2bc37 2022-10-23 stsp if (unlink(client->packidx_path) == -1 && errno != ENOENT)
391 13b2bc37 2022-10-23 stsp log_warn("unlink %s: ", client->packidx_path);
392 13b2bc37 2022-10-23 stsp free(client->packidx_path);
394 13b2bc37 2022-10-23 stsp free(client->capabilities);
395 13b2bc37 2022-10-23 stsp free(client);
396 13b2bc37 2022-10-23 stsp client_cnt--;
399 13b2bc37 2022-10-23 stsp static void
400 13b2bc37 2022-10-23 stsp disconnect_on_error(struct gotd_client *client, const struct got_error *err)
402 13b2bc37 2022-10-23 stsp struct imsgbuf ibuf;
404 13b2bc37 2022-10-23 stsp log_warnx("uid %d: %s", client->euid, err->msg);
405 13b2bc37 2022-10-23 stsp if (err->code != GOT_ERR_EOF) {
406 13b2bc37 2022-10-23 stsp imsg_init(&ibuf, client->fd);
407 13b2bc37 2022-10-23 stsp gotd_imsg_send_error(&ibuf, 0, PROC_GOTD, err);
408 13b2bc37 2022-10-23 stsp imsg_clear(&ibuf);
410 13b2bc37 2022-10-23 stsp disconnect(client);
413 f1752522 2022-10-29 stsp static const struct got_error *
414 f1752522 2022-10-29 stsp send_repo_info(struct gotd_imsgev *iev, struct gotd_repo *repo)
416 f1752522 2022-10-29 stsp const struct got_error *err = NULL;
417 f1752522 2022-10-29 stsp struct gotd_imsg_info_repo irepo;
419 f1752522 2022-10-29 stsp memset(&irepo, 0, sizeof(irepo));
421 f1752522 2022-10-29 stsp if (strlcpy(irepo.repo_name, repo->name, sizeof(irepo.repo_name))
422 f1752522 2022-10-29 stsp >= sizeof(irepo.repo_name))
423 f1752522 2022-10-29 stsp return got_error_msg(GOT_ERR_NO_SPACE, "repo name too long");
424 f1752522 2022-10-29 stsp if (strlcpy(irepo.repo_path, repo->path, sizeof(irepo.repo_path))
425 f1752522 2022-10-29 stsp >= sizeof(irepo.repo_path))
426 f1752522 2022-10-29 stsp return got_error_msg(GOT_ERR_NO_SPACE, "repo path too long");
428 f1752522 2022-10-29 stsp if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_REPO, PROC_GOTD, -1,
429 f1752522 2022-10-29 stsp &irepo, sizeof(irepo)) == -1) {
430 f1752522 2022-10-29 stsp err = got_error_from_errno("imsg compose INFO_REPO");
432 f1752522 2022-10-29 stsp return err;
435 f1752522 2022-10-29 stsp return NULL;
438 f1752522 2022-10-29 stsp static const struct got_error *
439 f1752522 2022-10-29 stsp send_capability(struct gotd_client_capability *capa, struct gotd_imsgev* iev)
441 f1752522 2022-10-29 stsp const struct got_error *err = NULL;
442 f1752522 2022-10-29 stsp struct gotd_imsg_capability icapa;
443 f1752522 2022-10-29 stsp size_t len;
444 f1752522 2022-10-29 stsp struct ibuf *wbuf;
446 f1752522 2022-10-29 stsp memset(&icapa, 0, sizeof(icapa));
448 f1752522 2022-10-29 stsp icapa.key_len = strlen(capa->key);
449 f1752522 2022-10-29 stsp len = sizeof(icapa) + icapa.key_len;
450 f1752522 2022-10-29 stsp if (capa->value) {
451 f1752522 2022-10-29 stsp icapa.value_len = strlen(capa->value);
452 f1752522 2022-10-29 stsp len += icapa.value_len;
455 f1752522 2022-10-29 stsp wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_CAPABILITY, 0, 0, len);
456 f1752522 2022-10-29 stsp if (wbuf == NULL) {
457 f1752522 2022-10-29 stsp err = got_error_from_errno("imsg_create CAPABILITY");
458 f1752522 2022-10-29 stsp return err;
461 f1752522 2022-10-29 stsp if (imsg_add(wbuf, &icapa, sizeof(icapa)) == -1)
462 f1752522 2022-10-29 stsp return got_error_from_errno("imsg_add CAPABILITY");
463 f1752522 2022-10-29 stsp if (imsg_add(wbuf, capa->key, icapa.key_len) == -1)
464 f1752522 2022-10-29 stsp return got_error_from_errno("imsg_add CAPABILITY");
465 f1752522 2022-10-29 stsp if (capa->value) {
466 f1752522 2022-10-29 stsp if (imsg_add(wbuf, capa->value, icapa.value_len) == -1)
467 f1752522 2022-10-29 stsp return got_error_from_errno("imsg_add CAPABILITY");
470 f1752522 2022-10-29 stsp wbuf->fd = -1;
471 f1752522 2022-10-29 stsp imsg_close(&iev->ibuf, wbuf);
473 f1752522 2022-10-29 stsp gotd_imsg_event_add(iev);
475 f1752522 2022-10-29 stsp return NULL;
478 f1752522 2022-10-29 stsp static const struct got_error *
479 f1752522 2022-10-29 stsp send_client_info(struct gotd_imsgev *iev, struct gotd_client *client)
481 f1752522 2022-10-29 stsp const struct got_error *err = NULL;
482 f1752522 2022-10-29 stsp struct gotd_imsg_info_client iclient;
483 f1752522 2022-10-29 stsp struct gotd_child_proc *proc;
486 f1752522 2022-10-29 stsp memset(&iclient, 0, sizeof(iclient));
487 f1752522 2022-10-29 stsp iclient.euid = client->euid;
488 f1752522 2022-10-29 stsp iclient.egid = client->egid;
490 f1752522 2022-10-29 stsp proc = get_client_proc(client);
491 f1752522 2022-10-29 stsp if (proc) {
492 eec68231 2022-12-14 stsp if (strlcpy(iclient.repo_name, proc->repo_path,
493 f1752522 2022-10-29 stsp sizeof(iclient.repo_name)) >= sizeof(iclient.repo_name)) {
494 f1752522 2022-10-29 stsp return got_error_msg(GOT_ERR_NO_SPACE,
495 f1752522 2022-10-29 stsp "repo name too long");
497 f1752522 2022-10-29 stsp if (client_is_writing(client))
498 f1752522 2022-10-29 stsp iclient.is_writing = 1;
501 f1752522 2022-10-29 stsp iclient.state = client->state;
502 f1752522 2022-10-29 stsp iclient.ncapabilities = client->ncapabilities;
504 f1752522 2022-10-29 stsp if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_CLIENT, PROC_GOTD, -1,
505 f1752522 2022-10-29 stsp &iclient, sizeof(iclient)) == -1) {
506 f1752522 2022-10-29 stsp err = got_error_from_errno("imsg compose INFO_CLIENT");
508 f1752522 2022-10-29 stsp return err;
511 f1752522 2022-10-29 stsp for (i = 0; i < client->ncapabilities; i++) {
512 f1752522 2022-10-29 stsp struct gotd_client_capability *capa;
513 f1752522 2022-10-29 stsp capa = &client->capabilities[i];
514 f1752522 2022-10-29 stsp err = send_capability(capa, iev);
516 f1752522 2022-10-29 stsp return err;
519 f1752522 2022-10-29 stsp return NULL;
522 f1752522 2022-10-29 stsp static const struct got_error *
523 f1752522 2022-10-29 stsp send_info(struct gotd_client *client)
525 f1752522 2022-10-29 stsp const struct got_error *err = NULL;
526 f1752522 2022-10-29 stsp struct gotd_imsg_info info;
527 f1752522 2022-10-29 stsp uint64_t slot;
528 f1752522 2022-10-29 stsp struct gotd_repo *repo;
530 78433331 2023-01-04 stsp if (client->euid != 0)
531 78433331 2023-01-04 stsp return got_error_set_errno(EPERM, "info");
533 f1752522 2022-10-29 stsp info.pid = gotd.pid;
534 f1752522 2022-10-29 stsp info.verbosity = gotd.verbosity;
535 f1752522 2022-10-29 stsp info.nrepos = gotd.nrepos;
536 f1752522 2022-10-29 stsp info.nclients = client_cnt - 1;
538 f1752522 2022-10-29 stsp if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_INFO, PROC_GOTD, -1,
539 f1752522 2022-10-29 stsp &info, sizeof(info)) == -1) {
540 f1752522 2022-10-29 stsp err = got_error_from_errno("imsg compose INFO");
542 f1752522 2022-10-29 stsp return err;
545 f1752522 2022-10-29 stsp TAILQ_FOREACH(repo, &gotd.repos, entry) {
546 f1752522 2022-10-29 stsp err = send_repo_info(&client->iev, repo);
548 f1752522 2022-10-29 stsp return err;
551 f1752522 2022-10-29 stsp for (slot = 0; slot < nitems(gotd_clients); slot++) {
552 f1752522 2022-10-29 stsp struct gotd_client *c;
553 f1752522 2022-10-29 stsp STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
554 f1752522 2022-10-29 stsp if (c->id == client->id)
556 f1752522 2022-10-29 stsp err = send_client_info(&client->iev, c);
558 f1752522 2022-10-29 stsp return err;
562 f1752522 2022-10-29 stsp return NULL;
565 f1752522 2022-10-29 stsp static const struct got_error *
566 f1752522 2022-10-29 stsp stop_gotd(struct gotd_client *client)
569 f1752522 2022-10-29 stsp if (client->euid != 0)
570 f1752522 2022-10-29 stsp return got_error_set_errno(EPERM, "stop");
572 f1752522 2022-10-29 stsp gotd_shutdown();
573 f1752522 2022-10-29 stsp /* NOTREACHED */
574 0ccf3acb 2022-11-16 stsp return NULL;
577 0ccf3acb 2022-11-16 stsp static struct gotd_repo *
578 0ccf3acb 2022-11-16 stsp find_repo_by_name(const char *repo_name)
580 0ccf3acb 2022-11-16 stsp struct gotd_repo *repo;
581 0ccf3acb 2022-11-16 stsp size_t namelen;
583 0ccf3acb 2022-11-16 stsp TAILQ_FOREACH(repo, &gotd.repos, entry) {
584 0ccf3acb 2022-11-16 stsp namelen = strlen(repo->name);
585 0ccf3acb 2022-11-16 stsp if (strncmp(repo->name, repo_name, namelen) != 0)
587 0ccf3acb 2022-11-16 stsp if (repo_name[namelen] == '\0' ||
588 0ccf3acb 2022-11-16 stsp strcmp(&repo_name[namelen], ".git") == 0)
589 0ccf3acb 2022-11-16 stsp return repo;
592 13b2bc37 2022-10-23 stsp return NULL;
595 13b2bc37 2022-10-23 stsp static const struct got_error *
596 b50a2b46 2022-12-29 stsp start_client_session(struct gotd_client *client, struct imsg *imsg)
598 13b2bc37 2022-10-23 stsp const struct got_error *err;
599 13b2bc37 2022-10-23 stsp struct gotd_imsg_list_refs ireq;
600 0ccf3acb 2022-11-16 stsp struct gotd_repo *repo = NULL;
601 13b2bc37 2022-10-23 stsp size_t datalen;
603 13b2bc37 2022-10-23 stsp log_debug("list-refs request from uid %d", client->euid);
605 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
606 13b2bc37 2022-10-23 stsp if (datalen != sizeof(ireq))
607 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
609 13b2bc37 2022-10-23 stsp memcpy(&ireq, imsg->data, datalen);
611 13b2bc37 2022-10-23 stsp if (ireq.client_is_reading) {
612 13b2bc37 2022-10-23 stsp err = ensure_client_is_not_writing(client);
614 13b2bc37 2022-10-23 stsp return err;
615 0ccf3acb 2022-11-16 stsp repo = find_repo_by_name(ireq.repo_name);
616 0ccf3acb 2022-11-16 stsp if (repo == NULL)
617 0ccf3acb 2022-11-16 stsp return got_error(GOT_ERR_NOT_GIT_REPO);
618 5e25db14 2022-12-29 stsp err = start_auth_child(client, GOTD_AUTH_READ, repo,
619 b50a2b46 2022-12-29 stsp gotd.argv0, gotd.confpath, gotd.daemonize,
620 b50a2b46 2022-12-29 stsp gotd.verbosity);
622 b50a2b46 2022-12-29 stsp return err;
624 13b2bc37 2022-10-23 stsp err = ensure_client_is_not_reading(client);
626 0ccf3acb 2022-11-16 stsp return err;
627 0ccf3acb 2022-11-16 stsp repo = find_repo_by_name(ireq.repo_name);
628 0ccf3acb 2022-11-16 stsp if (repo == NULL)
629 0ccf3acb 2022-11-16 stsp return got_error(GOT_ERR_NOT_GIT_REPO);
630 5e25db14 2022-12-29 stsp err = start_auth_child(client,
631 5e25db14 2022-12-29 stsp GOTD_AUTH_READ | GOTD_AUTH_WRITE,
632 5e25db14 2022-12-29 stsp repo, gotd.argv0, gotd.confpath, gotd.daemonize,
633 b50a2b46 2022-12-29 stsp gotd.verbosity);
635 b50a2b46 2022-12-29 stsp return err;
638 5e25db14 2022-12-29 stsp /* Flow continues upon authentication successs/failure or timeout. */
639 13b2bc37 2022-10-23 stsp return NULL;
642 13b2bc37 2022-10-23 stsp static const struct got_error *
643 13b2bc37 2022-10-23 stsp forward_want(struct gotd_client *client, struct imsg *imsg)
645 13b2bc37 2022-10-23 stsp struct gotd_imsg_want ireq;
646 13b2bc37 2022-10-23 stsp struct gotd_imsg_want iwant;
647 13b2bc37 2022-10-23 stsp size_t datalen;
649 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
650 13b2bc37 2022-10-23 stsp if (datalen != sizeof(ireq))
651 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
653 13b2bc37 2022-10-23 stsp memcpy(&ireq, imsg->data, datalen);
655 13b2bc37 2022-10-23 stsp memset(&iwant, 0, sizeof(iwant));
656 13b2bc37 2022-10-23 stsp memcpy(iwant.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
657 13b2bc37 2022-10-23 stsp iwant.client_id = client->id;
659 13b2bc37 2022-10-23 stsp if (gotd_imsg_compose_event(&client->repo_read->iev, GOTD_IMSG_WANT,
660 13b2bc37 2022-10-23 stsp PROC_GOTD, -1, &iwant, sizeof(iwant)) == -1)
661 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg compose WANT");
663 13b2bc37 2022-10-23 stsp return NULL;
666 13b2bc37 2022-10-23 stsp static const struct got_error *
667 13b2bc37 2022-10-23 stsp forward_ref_update(struct gotd_client *client, struct imsg *imsg)
669 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
670 13b2bc37 2022-10-23 stsp struct gotd_imsg_ref_update ireq;
671 13b2bc37 2022-10-23 stsp struct gotd_imsg_ref_update *iref = NULL;
672 13b2bc37 2022-10-23 stsp size_t datalen;
674 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
675 13b2bc37 2022-10-23 stsp if (datalen < sizeof(ireq))
676 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
677 13b2bc37 2022-10-23 stsp memcpy(&ireq, imsg->data, sizeof(ireq));
678 13b2bc37 2022-10-23 stsp if (datalen != sizeof(ireq) + ireq.name_len)
679 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
681 13b2bc37 2022-10-23 stsp iref = malloc(datalen);
682 13b2bc37 2022-10-23 stsp if (iref == NULL)
683 13b2bc37 2022-10-23 stsp return got_error_from_errno("malloc");
684 13b2bc37 2022-10-23 stsp memcpy(iref, imsg->data, datalen);
686 13b2bc37 2022-10-23 stsp iref->client_id = client->id;
687 13b2bc37 2022-10-23 stsp if (gotd_imsg_compose_event(&client->repo_write->iev,
688 13b2bc37 2022-10-23 stsp GOTD_IMSG_REF_UPDATE, PROC_GOTD, -1, iref, datalen) == -1)
689 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg compose REF_UPDATE");
690 13b2bc37 2022-10-23 stsp free(iref);
691 13b2bc37 2022-10-23 stsp return err;
694 13b2bc37 2022-10-23 stsp static const struct got_error *
695 13b2bc37 2022-10-23 stsp forward_have(struct gotd_client *client, struct imsg *imsg)
697 13b2bc37 2022-10-23 stsp struct gotd_imsg_have ireq;
698 13b2bc37 2022-10-23 stsp struct gotd_imsg_have ihave;
699 13b2bc37 2022-10-23 stsp size_t datalen;
701 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
702 13b2bc37 2022-10-23 stsp if (datalen != sizeof(ireq))
703 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
705 13b2bc37 2022-10-23 stsp memcpy(&ireq, imsg->data, datalen);
707 13b2bc37 2022-10-23 stsp memset(&ihave, 0, sizeof(ihave));
708 13b2bc37 2022-10-23 stsp memcpy(ihave.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
709 13b2bc37 2022-10-23 stsp ihave.client_id = client->id;
711 13b2bc37 2022-10-23 stsp if (gotd_imsg_compose_event(&client->repo_read->iev, GOTD_IMSG_HAVE,
712 13b2bc37 2022-10-23 stsp PROC_GOTD, -1, &ihave, sizeof(ihave)) == -1)
713 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg compose HAVE");
715 13b2bc37 2022-10-23 stsp return NULL;
719 13b2bc37 2022-10-23 stsp client_has_capability(struct gotd_client *client, const char *capastr)
721 13b2bc37 2022-10-23 stsp struct gotd_client_capability *capa;
724 13b2bc37 2022-10-23 stsp if (client->ncapabilities == 0)
727 13b2bc37 2022-10-23 stsp for (i = 0; i < client->ncapabilities; i++) {
728 13b2bc37 2022-10-23 stsp capa = &client->capabilities[i];
729 13b2bc37 2022-10-23 stsp if (strcmp(capa->key, capastr) == 0)
736 13b2bc37 2022-10-23 stsp static const struct got_error *
737 13b2bc37 2022-10-23 stsp recv_capabilities(struct gotd_client *client, struct imsg *imsg)
739 13b2bc37 2022-10-23 stsp struct gotd_imsg_capabilities icapas;
740 13b2bc37 2022-10-23 stsp size_t datalen;
742 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
743 13b2bc37 2022-10-23 stsp if (datalen != sizeof(icapas))
744 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
745 13b2bc37 2022-10-23 stsp memcpy(&icapas, imsg->data, sizeof(icapas));
747 13b2bc37 2022-10-23 stsp client->ncapa_alloc = icapas.ncapabilities;
748 13b2bc37 2022-10-23 stsp client->capabilities = calloc(client->ncapa_alloc,
749 13b2bc37 2022-10-23 stsp sizeof(*client->capabilities));
750 13b2bc37 2022-10-23 stsp if (client->capabilities == NULL) {
751 13b2bc37 2022-10-23 stsp client->ncapa_alloc = 0;
752 13b2bc37 2022-10-23 stsp return got_error_from_errno("calloc");
755 13b2bc37 2022-10-23 stsp log_debug("expecting %zu capabilities from uid %d",
756 13b2bc37 2022-10-23 stsp client->ncapa_alloc, client->euid);
757 13b2bc37 2022-10-23 stsp return NULL;
760 13b2bc37 2022-10-23 stsp static const struct got_error *
761 13b2bc37 2022-10-23 stsp recv_capability(struct gotd_client *client, struct imsg *imsg)
763 13b2bc37 2022-10-23 stsp struct gotd_imsg_capability icapa;
764 13b2bc37 2022-10-23 stsp struct gotd_client_capability *capa;
765 13b2bc37 2022-10-23 stsp size_t datalen;
766 13b2bc37 2022-10-23 stsp char *key, *value = NULL;
768 13b2bc37 2022-10-23 stsp if (client->capabilities == NULL ||
769 13b2bc37 2022-10-23 stsp client->ncapabilities >= client->ncapa_alloc) {
770 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
771 13b2bc37 2022-10-23 stsp "unexpected capability received");
774 13b2bc37 2022-10-23 stsp memset(&icapa, 0, sizeof(icapa));
776 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
777 13b2bc37 2022-10-23 stsp if (datalen < sizeof(icapa))
778 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
779 13b2bc37 2022-10-23 stsp memcpy(&icapa, imsg->data, sizeof(icapa));
781 13b2bc37 2022-10-23 stsp if (datalen != sizeof(icapa) + icapa.key_len + icapa.value_len)
782 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
784 13b2bc37 2022-10-23 stsp key = malloc(icapa.key_len + 1);
785 13b2bc37 2022-10-23 stsp if (key == NULL)
786 13b2bc37 2022-10-23 stsp return got_error_from_errno("malloc");
787 13b2bc37 2022-10-23 stsp if (icapa.value_len > 0) {
788 13b2bc37 2022-10-23 stsp value = malloc(icapa.value_len + 1);
789 13b2bc37 2022-10-23 stsp if (value == NULL) {
791 13b2bc37 2022-10-23 stsp return got_error_from_errno("malloc");
795 13b2bc37 2022-10-23 stsp memcpy(key, imsg->data + sizeof(icapa), icapa.key_len);
796 13b2bc37 2022-10-23 stsp key[icapa.key_len] = '\0';
797 13b2bc37 2022-10-23 stsp if (value) {
798 13b2bc37 2022-10-23 stsp memcpy(value, imsg->data + sizeof(icapa) + icapa.key_len,
799 13b2bc37 2022-10-23 stsp icapa.value_len);
800 13b2bc37 2022-10-23 stsp value[icapa.value_len] = '\0';
803 13b2bc37 2022-10-23 stsp capa = &client->capabilities[client->ncapabilities++];
804 13b2bc37 2022-10-23 stsp capa->key = key;
805 13b2bc37 2022-10-23 stsp capa->value = value;
808 13b2bc37 2022-10-23 stsp log_debug("uid %d: capability %s=%s", client->euid, key, value);
810 13b2bc37 2022-10-23 stsp log_debug("uid %d: capability %s", client->euid, key);
812 13b2bc37 2022-10-23 stsp return NULL;
815 13b2bc37 2022-10-23 stsp static const struct got_error *
816 13b2bc37 2022-10-23 stsp send_packfile(struct gotd_client *client)
818 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
819 13b2bc37 2022-10-23 stsp struct gotd_imsg_send_packfile ipack;
820 13b2bc37 2022-10-23 stsp struct gotd_imsg_packfile_pipe ipipe;
821 13b2bc37 2022-10-23 stsp int pipe[2];
823 13b2bc37 2022-10-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
824 13b2bc37 2022-10-23 stsp return got_error_from_errno("socketpair");
826 13b2bc37 2022-10-23 stsp memset(&ipack, 0, sizeof(ipack));
827 13b2bc37 2022-10-23 stsp memset(&ipipe, 0, sizeof(ipipe));
829 13b2bc37 2022-10-23 stsp ipack.client_id = client->id;
830 13b2bc37 2022-10-23 stsp if (client_has_capability(client, GOT_CAPA_SIDE_BAND_64K))
831 13b2bc37 2022-10-23 stsp ipack.report_progress = 1;
833 13b2bc37 2022-10-23 stsp client->delta_cache_fd = got_opentempfd();
834 13b2bc37 2022-10-23 stsp if (client->delta_cache_fd == -1)
835 13b2bc37 2022-10-23 stsp return got_error_from_errno("got_opentempfd");
837 13b2bc37 2022-10-23 stsp if (gotd_imsg_compose_event(&client->repo_read->iev,
838 13b2bc37 2022-10-23 stsp GOTD_IMSG_SEND_PACKFILE, PROC_GOTD, client->delta_cache_fd,
839 13b2bc37 2022-10-23 stsp &ipack, sizeof(ipack)) == -1) {
840 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg compose SEND_PACKFILE");
841 13b2bc37 2022-10-23 stsp close(pipe[0]);
842 13b2bc37 2022-10-23 stsp close(pipe[1]);
843 13b2bc37 2022-10-23 stsp return err;
846 13b2bc37 2022-10-23 stsp ipipe.client_id = client->id;
848 86769de8 2022-10-28 stsp /* Send pack pipe end 0 to repo_read. */
849 13b2bc37 2022-10-23 stsp if (gotd_imsg_compose_event(&client->repo_read->iev,
850 13b2bc37 2022-10-23 stsp GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[0],
851 13b2bc37 2022-10-23 stsp &ipipe, sizeof(ipipe)) == -1) {
852 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg compose PACKFILE_PIPE");
853 13b2bc37 2022-10-23 stsp close(pipe[1]);
854 13b2bc37 2022-10-23 stsp return err;
857 86769de8 2022-10-28 stsp /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
858 86769de8 2022-10-28 stsp if (gotd_imsg_compose_event(&client->iev,
859 86769de8 2022-10-28 stsp GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1], NULL, 0) == -1)
860 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg compose PACKFILE_PIPE");
862 13b2bc37 2022-10-23 stsp return err;
865 13b2bc37 2022-10-23 stsp static const struct got_error *
866 13b2bc37 2022-10-23 stsp recv_packfile(struct gotd_client *client)
868 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
869 13b2bc37 2022-10-23 stsp struct gotd_imsg_recv_packfile ipack;
870 13b2bc37 2022-10-23 stsp struct gotd_imsg_packfile_pipe ipipe;
871 13b2bc37 2022-10-23 stsp struct gotd_imsg_packidx_file ifile;
872 13b2bc37 2022-10-23 stsp char *basepath = NULL, *pack_path = NULL, *idx_path = NULL;
873 13b2bc37 2022-10-23 stsp int packfd = -1, idxfd = -1;
874 13b2bc37 2022-10-23 stsp int pipe[2] = { -1, -1 };
876 13b2bc37 2022-10-23 stsp if (client->packfile_path) {
877 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_PRIVSEP_MSG,
878 13b2bc37 2022-10-23 stsp "uid %d already has a pack file", client->euid);
881 13b2bc37 2022-10-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
882 13b2bc37 2022-10-23 stsp return got_error_from_errno("socketpair");
884 13b2bc37 2022-10-23 stsp memset(&ipipe, 0, sizeof(ipipe));
885 13b2bc37 2022-10-23 stsp ipipe.client_id = client->id;
887 7fec5f4a 2022-10-28 stsp /* Send pack pipe end 0 to repo_write. */
888 13b2bc37 2022-10-23 stsp if (gotd_imsg_compose_event(&client->repo_write->iev,
889 13b2bc37 2022-10-23 stsp GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[0],
890 13b2bc37 2022-10-23 stsp &ipipe, sizeof(ipipe)) == -1) {
891 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg compose PACKFILE_PIPE");
892 13b2bc37 2022-10-23 stsp pipe[0] = -1;
895 13b2bc37 2022-10-23 stsp pipe[0] = -1;
897 7fec5f4a 2022-10-28 stsp /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
898 7fec5f4a 2022-10-28 stsp if (gotd_imsg_compose_event(&client->iev,
899 7fec5f4a 2022-10-28 stsp GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1], NULL, 0) == -1)
900 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg compose PACKFILE_PIPE");
901 13b2bc37 2022-10-23 stsp pipe[1] = -1;
903 13b2bc37 2022-10-23 stsp if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.pack",
904 eec68231 2022-12-14 stsp client->repo_write->repo_path, GOT_OBJECTS_PACK_DIR,
905 13b2bc37 2022-10-23 stsp client->euid) == -1) {
906 13b2bc37 2022-10-23 stsp err = got_error_from_errno("asprintf");
910 b90054ed 2022-11-01 stsp err = got_opentemp_named_fd(&pack_path, &packfd, basepath, "");
914 13b2bc37 2022-10-23 stsp free(basepath);
915 13b2bc37 2022-10-23 stsp if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.idx",
916 eec68231 2022-12-14 stsp client->repo_write->repo_path, GOT_OBJECTS_PACK_DIR,
917 13b2bc37 2022-10-23 stsp client->euid) == -1) {
918 13b2bc37 2022-10-23 stsp err = got_error_from_errno("asprintf");
919 13b2bc37 2022-10-23 stsp basepath = NULL;
922 b90054ed 2022-11-01 stsp err = got_opentemp_named_fd(&idx_path, &idxfd, basepath, "");
926 13b2bc37 2022-10-23 stsp memset(&ifile, 0, sizeof(ifile));
927 13b2bc37 2022-10-23 stsp ifile.client_id = client->id;
928 13b2bc37 2022-10-23 stsp if (gotd_imsg_compose_event(&client->repo_write->iev,
929 13b2bc37 2022-10-23 stsp GOTD_IMSG_PACKIDX_FILE, PROC_GOTD, idxfd,
930 13b2bc37 2022-10-23 stsp &ifile, sizeof(ifile)) == -1) {
931 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg compose PACKIDX_FILE");
932 13b2bc37 2022-10-23 stsp idxfd = -1;
935 13b2bc37 2022-10-23 stsp idxfd = -1;
937 13b2bc37 2022-10-23 stsp memset(&ipack, 0, sizeof(ipack));
938 13b2bc37 2022-10-23 stsp ipack.client_id = client->id;
939 13b2bc37 2022-10-23 stsp if (client_has_capability(client, GOT_CAPA_REPORT_STATUS))
940 13b2bc37 2022-10-23 stsp ipack.report_status = 1;
942 13b2bc37 2022-10-23 stsp if (gotd_imsg_compose_event(&client->repo_write->iev,
943 13b2bc37 2022-10-23 stsp GOTD_IMSG_RECV_PACKFILE, PROC_GOTD, packfd,
944 13b2bc37 2022-10-23 stsp &ipack, sizeof(ipack)) == -1) {
945 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg compose RECV_PACKFILE");
946 13b2bc37 2022-10-23 stsp packfd = -1;
949 13b2bc37 2022-10-23 stsp packfd = -1;
952 13b2bc37 2022-10-23 stsp free(basepath);
953 13b2bc37 2022-10-23 stsp if (pipe[0] != -1 && close(pipe[0]) == -1 && err == NULL)
954 13b2bc37 2022-10-23 stsp err = got_error_from_errno("close");
955 13b2bc37 2022-10-23 stsp if (pipe[1] != -1 && close(pipe[1]) == -1 && err == NULL)
956 13b2bc37 2022-10-23 stsp err = got_error_from_errno("close");
957 13b2bc37 2022-10-23 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
958 13b2bc37 2022-10-23 stsp err = got_error_from_errno("close");
959 13b2bc37 2022-10-23 stsp if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
960 13b2bc37 2022-10-23 stsp err = got_error_from_errno("close");
962 13b2bc37 2022-10-23 stsp free(pack_path);
963 13b2bc37 2022-10-23 stsp free(idx_path);
965 13b2bc37 2022-10-23 stsp client->packfile_path = pack_path;
966 13b2bc37 2022-10-23 stsp client->packidx_path = idx_path;
968 13b2bc37 2022-10-23 stsp return err;
971 13b2bc37 2022-10-23 stsp static void
972 13b2bc37 2022-10-23 stsp gotd_request(int fd, short events, void *arg)
974 13b2bc37 2022-10-23 stsp struct gotd_imsgev *iev = arg;
975 13b2bc37 2022-10-23 stsp struct imsgbuf *ibuf = &iev->ibuf;
976 13b2bc37 2022-10-23 stsp struct gotd_client *client = iev->handler_arg;
977 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
978 13b2bc37 2022-10-23 stsp struct imsg imsg;
981 13b2bc37 2022-10-23 stsp if (events & EV_WRITE) {
982 13b2bc37 2022-10-23 stsp while (ibuf->w.queued) {
983 13b2bc37 2022-10-23 stsp n = msgbuf_write(&ibuf->w);
984 13b2bc37 2022-10-23 stsp if (n == -1 && errno == EPIPE) {
986 13b2bc37 2022-10-23 stsp * The client has closed its socket.
987 13b2bc37 2022-10-23 stsp * This can happen when Git clients are
988 13b2bc37 2022-10-23 stsp * done sending pack file data.
990 13b2bc37 2022-10-23 stsp msgbuf_clear(&ibuf->w);
992 13b2bc37 2022-10-23 stsp } else if (n == -1 && errno != EAGAIN) {
993 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_flush");
994 13b2bc37 2022-10-23 stsp disconnect_on_error(client, err);
997 13b2bc37 2022-10-23 stsp if (n == 0) {
998 13b2bc37 2022-10-23 stsp /* Connection closed. */
999 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_EOF);
1000 13b2bc37 2022-10-23 stsp disconnect_on_error(client, err);
1005 f1752522 2022-10-29 stsp /* Disconnect gotctl(8) now that messages have been sent. */
1006 f1752522 2022-10-29 stsp if (!client_is_reading(client) && !client_is_writing(client)) {
1007 f1752522 2022-10-29 stsp disconnect(client);
1012 13b2bc37 2022-10-23 stsp if ((events & EV_READ) == 0)
1015 13b2bc37 2022-10-23 stsp memset(&imsg, 0, sizeof(imsg));
1017 13b2bc37 2022-10-23 stsp while (err == NULL) {
1018 13b2bc37 2022-10-23 stsp err = gotd_imsg_recv(&imsg, ibuf, 0);
1019 13b2bc37 2022-10-23 stsp if (err) {
1020 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_PRIVSEP_READ)
1021 13b2bc37 2022-10-23 stsp err = NULL;
1025 13b2bc37 2022-10-23 stsp evtimer_del(&client->tmo);
1027 13b2bc37 2022-10-23 stsp switch (imsg.hdr.type) {
1028 f1752522 2022-10-29 stsp case GOTD_IMSG_INFO:
1029 f1752522 2022-10-29 stsp err = send_info(client);
1031 f1752522 2022-10-29 stsp case GOTD_IMSG_STOP:
1032 f1752522 2022-10-29 stsp err = stop_gotd(client);
1034 13b2bc37 2022-10-23 stsp case GOTD_IMSG_LIST_REFS:
1035 13b2bc37 2022-10-23 stsp if (client->state != GOTD_STATE_EXPECT_LIST_REFS) {
1036 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1037 13b2bc37 2022-10-23 stsp "unexpected list-refs request received");
1040 b50a2b46 2022-12-29 stsp err = start_client_session(client, &imsg);
1044 13b2bc37 2022-10-23 stsp case GOTD_IMSG_CAPABILITIES:
1045 13b2bc37 2022-10-23 stsp if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1046 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1047 13b2bc37 2022-10-23 stsp "unexpected capabilities received");
1050 13b2bc37 2022-10-23 stsp log_debug("receiving capabilities from uid %d",
1051 13b2bc37 2022-10-23 stsp client->euid);
1052 13b2bc37 2022-10-23 stsp err = recv_capabilities(client, &imsg);
1054 13b2bc37 2022-10-23 stsp case GOTD_IMSG_CAPABILITY:
1055 13b2bc37 2022-10-23 stsp if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1056 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1057 13b2bc37 2022-10-23 stsp "unexpected capability received");
1060 13b2bc37 2022-10-23 stsp err = recv_capability(client, &imsg);
1061 13b2bc37 2022-10-23 stsp if (err || client->ncapabilities < client->ncapa_alloc)
1063 13b2bc37 2022-10-23 stsp if (client_is_reading(client)) {
1064 13b2bc37 2022-10-23 stsp client->state = GOTD_STATE_EXPECT_WANT;
1065 13b2bc37 2022-10-23 stsp log_debug("uid %d: expecting want-lines",
1066 13b2bc37 2022-10-23 stsp client->euid);
1067 13b2bc37 2022-10-23 stsp } else if (client_is_writing(client)) {
1068 13b2bc37 2022-10-23 stsp client->state = GOTD_STATE_EXPECT_REF_UPDATE;
1069 13b2bc37 2022-10-23 stsp log_debug("uid %d: expecting ref-update-lines",
1070 13b2bc37 2022-10-23 stsp client->euid);
1072 13b2bc37 2022-10-23 stsp fatalx("client %d is both reading and writing",
1073 13b2bc37 2022-10-23 stsp client->euid);
1075 13b2bc37 2022-10-23 stsp case GOTD_IMSG_WANT:
1076 13b2bc37 2022-10-23 stsp if (client->state != GOTD_STATE_EXPECT_WANT) {
1077 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1078 13b2bc37 2022-10-23 stsp "unexpected want-line received");
1081 13b2bc37 2022-10-23 stsp log_debug("received want-line from uid %d",
1082 13b2bc37 2022-10-23 stsp client->euid);
1083 13b2bc37 2022-10-23 stsp err = ensure_client_is_reading(client);
1086 13b2bc37 2022-10-23 stsp err = forward_want(client, &imsg);
1088 13b2bc37 2022-10-23 stsp case GOTD_IMSG_REF_UPDATE:
1089 13b2bc37 2022-10-23 stsp if (client->state != GOTD_STATE_EXPECT_REF_UPDATE) {
1090 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1091 13b2bc37 2022-10-23 stsp "unexpected ref-update-line received");
1094 13b2bc37 2022-10-23 stsp log_debug("received ref-update-line from uid %d",
1095 13b2bc37 2022-10-23 stsp client->euid);
1096 13b2bc37 2022-10-23 stsp err = ensure_client_is_writing(client);
1099 13b2bc37 2022-10-23 stsp err = forward_ref_update(client, &imsg);
1102 13b2bc37 2022-10-23 stsp client->state = GOTD_STATE_EXPECT_MORE_REF_UPDATES;
1104 13b2bc37 2022-10-23 stsp case GOTD_IMSG_HAVE:
1105 13b2bc37 2022-10-23 stsp if (client->state != GOTD_STATE_EXPECT_HAVE) {
1106 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1107 13b2bc37 2022-10-23 stsp "unexpected have-line received");
1110 13b2bc37 2022-10-23 stsp log_debug("received have-line from uid %d",
1111 13b2bc37 2022-10-23 stsp client->euid);
1112 13b2bc37 2022-10-23 stsp err = ensure_client_is_reading(client);
1115 13b2bc37 2022-10-23 stsp err = forward_have(client, &imsg);
1119 13b2bc37 2022-10-23 stsp case GOTD_IMSG_FLUSH:
1120 13b2bc37 2022-10-23 stsp if (client->state == GOTD_STATE_EXPECT_WANT ||
1121 13b2bc37 2022-10-23 stsp client->state == GOTD_STATE_EXPECT_HAVE) {
1122 13b2bc37 2022-10-23 stsp err = ensure_client_is_reading(client);
1125 13b2bc37 2022-10-23 stsp } else if (client->state ==
1126 13b2bc37 2022-10-23 stsp GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1127 13b2bc37 2022-10-23 stsp err = ensure_client_is_writing(client);
1131 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1132 13b2bc37 2022-10-23 stsp "unexpected flush-pkt received");
1135 13b2bc37 2022-10-23 stsp log_debug("received flush-pkt from uid %d",
1136 13b2bc37 2022-10-23 stsp client->euid);
1137 13b2bc37 2022-10-23 stsp if (client->state == GOTD_STATE_EXPECT_WANT) {
1138 13b2bc37 2022-10-23 stsp client->state = GOTD_STATE_EXPECT_HAVE;
1139 13b2bc37 2022-10-23 stsp log_debug("uid %d: expecting have-lines",
1140 13b2bc37 2022-10-23 stsp client->euid);
1141 13b2bc37 2022-10-23 stsp } else if (client->state == GOTD_STATE_EXPECT_HAVE) {
1142 13b2bc37 2022-10-23 stsp client->state = GOTD_STATE_EXPECT_DONE;
1143 13b2bc37 2022-10-23 stsp log_debug("uid %d: expecting 'done'",
1144 13b2bc37 2022-10-23 stsp client->euid);
1145 13b2bc37 2022-10-23 stsp } else if (client->state ==
1146 13b2bc37 2022-10-23 stsp GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1147 13b2bc37 2022-10-23 stsp client->state = GOTD_STATE_EXPECT_PACKFILE;
1148 13b2bc37 2022-10-23 stsp log_debug("uid %d: expecting packfile",
1149 13b2bc37 2022-10-23 stsp client->euid);
1150 13b2bc37 2022-10-23 stsp err = recv_packfile(client);
1152 13b2bc37 2022-10-23 stsp /* should not happen, see above */
1153 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1154 13b2bc37 2022-10-23 stsp "unexpected client state");
1158 13b2bc37 2022-10-23 stsp case GOTD_IMSG_DONE:
1159 13b2bc37 2022-10-23 stsp if (client->state != GOTD_STATE_EXPECT_HAVE &&
1160 13b2bc37 2022-10-23 stsp client->state != GOTD_STATE_EXPECT_DONE) {
1161 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1162 13b2bc37 2022-10-23 stsp "unexpected flush-pkt received");
1165 13b2bc37 2022-10-23 stsp log_debug("received 'done' from uid %d", client->euid);
1166 13b2bc37 2022-10-23 stsp err = ensure_client_is_reading(client);
1169 13b2bc37 2022-10-23 stsp client->state = GOTD_STATE_DONE;
1170 13b2bc37 2022-10-23 stsp err = send_packfile(client);
1173 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1177 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
1180 13b2bc37 2022-10-23 stsp if (err) {
1181 13b2bc37 2022-10-23 stsp if (err->code != GOT_ERR_EOF ||
1182 13b2bc37 2022-10-23 stsp client->state != GOTD_STATE_EXPECT_PACKFILE)
1183 13b2bc37 2022-10-23 stsp disconnect_on_error(client, err);
1185 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(&client->iev);
1186 ef4e2f01 2022-12-29 stsp if (client->state == GOTD_STATE_EXPECT_LIST_REFS)
1187 ef4e2f01 2022-12-29 stsp evtimer_add(&client->tmo, &auth_timeout);
1189 40b85cca 2023-01-03 stsp evtimer_add(&client->tmo, &gotd.request_timeout);
1193 13b2bc37 2022-10-23 stsp static void
1194 13b2bc37 2022-10-23 stsp gotd_request_timeout(int fd, short events, void *arg)
1196 13b2bc37 2022-10-23 stsp struct gotd_client *client = arg;
1198 13b2bc37 2022-10-23 stsp log_debug("disconnecting uid %d due to timeout", client->euid);
1199 13b2bc37 2022-10-23 stsp disconnect(client);
1202 d93ecf7d 2022-12-14 stsp static const struct got_error *
1203 d93ecf7d 2022-12-14 stsp recv_connect(uint32_t *client_id, struct imsg *imsg)
1205 d93ecf7d 2022-12-14 stsp const struct got_error *err = NULL;
1206 d93ecf7d 2022-12-14 stsp struct gotd_imsg_connect iconnect;
1207 d93ecf7d 2022-12-14 stsp size_t datalen;
1208 13b2bc37 2022-10-23 stsp int s = -1;
1209 13b2bc37 2022-10-23 stsp struct gotd_client *client = NULL;
1211 d93ecf7d 2022-12-14 stsp *client_id = 0;
1213 d93ecf7d 2022-12-14 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1214 d93ecf7d 2022-12-14 stsp if (datalen != sizeof(iconnect))
1215 d93ecf7d 2022-12-14 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1216 d93ecf7d 2022-12-14 stsp memcpy(&iconnect, imsg->data, sizeof(iconnect));
1218 d93ecf7d 2022-12-14 stsp s = imsg->fd;
1219 13b2bc37 2022-10-23 stsp if (s == -1) {
1220 d93ecf7d 2022-12-14 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1221 d93ecf7d 2022-12-14 stsp goto done;
1224 d93ecf7d 2022-12-14 stsp if (find_client(iconnect.client_id)) {
1225 d93ecf7d 2022-12-14 stsp err = got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
1226 d93ecf7d 2022-12-14 stsp goto done;
1229 13b2bc37 2022-10-23 stsp client = calloc(1, sizeof(*client));
1230 13b2bc37 2022-10-23 stsp if (client == NULL) {
1231 d93ecf7d 2022-12-14 stsp err = got_error_from_errno("calloc");
1232 d93ecf7d 2022-12-14 stsp goto done;
1235 d93ecf7d 2022-12-14 stsp *client_id = iconnect.client_id;
1237 13b2bc37 2022-10-23 stsp client->state = GOTD_STATE_EXPECT_LIST_REFS;
1238 d93ecf7d 2022-12-14 stsp client->id = iconnect.client_id;
1239 13b2bc37 2022-10-23 stsp client->fd = s;
1241 13b2bc37 2022-10-23 stsp client->delta_cache_fd = -1;
1242 365cf0f3 2022-12-29 stsp /* The auth process will verify UID/GID for us. */
1243 365cf0f3 2022-12-29 stsp client->euid = iconnect.euid;
1244 365cf0f3 2022-12-29 stsp client->egid = iconnect.egid;
1245 13b2bc37 2022-10-23 stsp client->nref_updates = -1;
1247 13b2bc37 2022-10-23 stsp imsg_init(&client->iev.ibuf, client->fd);
1248 13b2bc37 2022-10-23 stsp client->iev.handler = gotd_request;
1249 13b2bc37 2022-10-23 stsp client->iev.events = EV_READ;
1250 13b2bc37 2022-10-23 stsp client->iev.handler_arg = client;
1252 13b2bc37 2022-10-23 stsp event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
1253 13b2bc37 2022-10-23 stsp &client->iev);
1254 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(&client->iev);
1256 13b2bc37 2022-10-23 stsp evtimer_set(&client->tmo, gotd_request_timeout, client);
1258 13b2bc37 2022-10-23 stsp add_client(client);
1259 13b2bc37 2022-10-23 stsp log_debug("%s: new client uid %d connected on fd %d", __func__,
1260 13b2bc37 2022-10-23 stsp client->euid, client->fd);
1262 d93ecf7d 2022-12-14 stsp if (err) {
1263 b50a2b46 2022-12-29 stsp struct gotd_child_proc *listen_proc = &gotd.listen_proc;
1264 d93ecf7d 2022-12-14 stsp struct gotd_imsg_disconnect idisconnect;
1266 d93ecf7d 2022-12-14 stsp idisconnect.client_id = client->id;
1267 d93ecf7d 2022-12-14 stsp if (gotd_imsg_compose_event(&listen_proc->iev,
1268 d93ecf7d 2022-12-14 stsp GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
1269 d93ecf7d 2022-12-14 stsp &idisconnect, sizeof(idisconnect)) == -1)
1270 d93ecf7d 2022-12-14 stsp log_warn("imsg compose DISCONNECT");
1272 d93ecf7d 2022-12-14 stsp if (s != -1)
1276 d93ecf7d 2022-12-14 stsp return err;
1279 13b2bc37 2022-10-23 stsp static const char *gotd_proc_names[PROC_MAX] = {
1283 13b2bc37 2022-10-23 stsp "repo_read",
1284 13b2bc37 2022-10-23 stsp "repo_write"
1287 13b2bc37 2022-10-23 stsp static void
1288 13b2bc37 2022-10-23 stsp kill_proc(struct gotd_child_proc *proc, int fatal)
1290 13b2bc37 2022-10-23 stsp if (fatal) {
1291 13b2bc37 2022-10-23 stsp log_warnx("sending SIGKILL to PID %d", proc->pid);
1292 13b2bc37 2022-10-23 stsp kill(proc->pid, SIGKILL);
1294 13b2bc37 2022-10-23 stsp kill(proc->pid, SIGTERM);
1297 13b2bc37 2022-10-23 stsp static void
1298 13b2bc37 2022-10-23 stsp gotd_shutdown(void)
1300 13b2bc37 2022-10-23 stsp struct gotd_child_proc *proc;
1301 b50a2b46 2022-12-29 stsp uint64_t slot;
1303 b50a2b46 2022-12-29 stsp for (slot = 0; slot < nitems(gotd_clients); slot++) {
1304 b50a2b46 2022-12-29 stsp struct gotd_client *c, *tmp;
1306 b50a2b46 2022-12-29 stsp STAILQ_FOREACH_SAFE(c, &gotd_clients[slot], entry, tmp)
1307 b50a2b46 2022-12-29 stsp disconnect(c);
1310 b50a2b46 2022-12-29 stsp proc = &gotd.listen_proc;
1311 b50a2b46 2022-12-29 stsp msgbuf_clear(&proc->iev.ibuf.w);
1312 b50a2b46 2022-12-29 stsp close(proc->iev.ibuf.fd);
1313 b50a2b46 2022-12-29 stsp kill_proc(proc, 0);
1314 5e25db14 2022-12-29 stsp wait_for_child(proc->pid);
1316 13b2bc37 2022-10-23 stsp log_info("terminating");
1321 13b2bc37 2022-10-23 stsp gotd_sighdlr(int sig, short event, void *arg)
1324 13b2bc37 2022-10-23 stsp * Normal signal handler rules don't apply because libevent
1325 13b2bc37 2022-10-23 stsp * decouples for us.
1328 13b2bc37 2022-10-23 stsp switch (sig) {
1329 13b2bc37 2022-10-23 stsp case SIGHUP:
1330 13b2bc37 2022-10-23 stsp log_info("%s: ignoring SIGHUP", __func__);
1332 13b2bc37 2022-10-23 stsp case SIGUSR1:
1333 13b2bc37 2022-10-23 stsp log_info("%s: ignoring SIGUSR1", __func__);
1335 13b2bc37 2022-10-23 stsp case SIGTERM:
1336 13b2bc37 2022-10-23 stsp case SIGINT:
1337 13b2bc37 2022-10-23 stsp gotd_shutdown();
1340 13b2bc37 2022-10-23 stsp fatalx("unexpected signal");
1344 13b2bc37 2022-10-23 stsp static const struct got_error *
1345 13b2bc37 2022-10-23 stsp ensure_proc_is_reading(struct gotd_client *client,
1346 13b2bc37 2022-10-23 stsp struct gotd_child_proc *proc)
1348 13b2bc37 2022-10-23 stsp if (!client_is_reading(client)) {
1349 13b2bc37 2022-10-23 stsp kill_proc(proc, 1);
1350 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_BAD_PACKET,
1351 13b2bc37 2022-10-23 stsp "PID %d handled a read-request for uid %d but this "
1352 13b2bc37 2022-10-23 stsp "user is not reading from a repository", proc->pid,
1353 13b2bc37 2022-10-23 stsp client->euid);
1356 13b2bc37 2022-10-23 stsp return NULL;
1359 13b2bc37 2022-10-23 stsp static const struct got_error *
1360 13b2bc37 2022-10-23 stsp ensure_proc_is_writing(struct gotd_client *client,
1361 13b2bc37 2022-10-23 stsp struct gotd_child_proc *proc)
1363 13b2bc37 2022-10-23 stsp if (!client_is_writing(client)) {
1364 13b2bc37 2022-10-23 stsp kill_proc(proc, 1);
1365 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_BAD_PACKET,
1366 13b2bc37 2022-10-23 stsp "PID %d handled a write-request for uid %d but this "
1367 13b2bc37 2022-10-23 stsp "user is not writing to a repository", proc->pid,
1368 13b2bc37 2022-10-23 stsp client->euid);
1371 13b2bc37 2022-10-23 stsp return NULL;
1374 13b2bc37 2022-10-23 stsp static int
1375 13b2bc37 2022-10-23 stsp verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
1376 13b2bc37 2022-10-23 stsp struct imsg *imsg)
1378 13b2bc37 2022-10-23 stsp const struct got_error *err;
1379 13b2bc37 2022-10-23 stsp struct gotd_child_proc *client_proc;
1380 13b2bc37 2022-10-23 stsp int ret = 0;
1382 d93ecf7d 2022-12-14 stsp if (proc->type == PROC_REPO_READ || proc->type == PROC_REPO_WRITE) {
1383 d93ecf7d 2022-12-14 stsp client_proc = get_client_proc(client);
1384 d93ecf7d 2022-12-14 stsp if (client_proc == NULL)
1385 d93ecf7d 2022-12-14 stsp fatalx("no process found for uid %d", client->euid);
1386 d93ecf7d 2022-12-14 stsp if (proc->pid != client_proc->pid) {
1387 d93ecf7d 2022-12-14 stsp kill_proc(proc, 1);
1388 d93ecf7d 2022-12-14 stsp log_warnx("received message from PID %d for uid %d, "
1389 d93ecf7d 2022-12-14 stsp "while PID %d is the process serving this user",
1390 d93ecf7d 2022-12-14 stsp proc->pid, client->euid, client_proc->pid);
1395 13b2bc37 2022-10-23 stsp switch (imsg->hdr.type) {
1396 13b2bc37 2022-10-23 stsp case GOTD_IMSG_ERROR:
1399 d93ecf7d 2022-12-14 stsp case GOTD_IMSG_CONNECT:
1400 d93ecf7d 2022-12-14 stsp if (proc->type != PROC_LISTEN) {
1401 d93ecf7d 2022-12-14 stsp err = got_error_fmt(GOT_ERR_BAD_PACKET,
1402 d93ecf7d 2022-12-14 stsp "new connection for uid %d from PID %d "
1403 d93ecf7d 2022-12-14 stsp "which is not the listen process",
1404 5e25db14 2022-12-29 stsp proc->pid, client->euid);
1408 5e25db14 2022-12-29 stsp case GOTD_IMSG_ACCESS_GRANTED:
1409 5e25db14 2022-12-29 stsp if (proc->type != PROC_AUTH) {
1410 5e25db14 2022-12-29 stsp err = got_error_fmt(GOT_ERR_BAD_PACKET,
1411 5e25db14 2022-12-29 stsp "authentication of uid %d from PID %d "
1412 5e25db14 2022-12-29 stsp "which is not the auth process",
1413 d93ecf7d 2022-12-14 stsp proc->pid, client->euid);
1417 b50a2b46 2022-12-29 stsp case GOTD_IMSG_REPO_CHILD_READY:
1418 b50a2b46 2022-12-29 stsp if (proc->type != PROC_REPO_READ &&
1419 b50a2b46 2022-12-29 stsp proc->type != PROC_REPO_WRITE) {
1420 b50a2b46 2022-12-29 stsp err = got_error_fmt(GOT_ERR_BAD_PACKET,
1421 b50a2b46 2022-12-29 stsp "unexpected \"ready\" signal from PID %d",
1422 b50a2b46 2022-12-29 stsp proc->pid);
1426 13b2bc37 2022-10-23 stsp case GOTD_IMSG_PACKFILE_DONE:
1427 13b2bc37 2022-10-23 stsp err = ensure_proc_is_reading(client, proc);
1429 13b2bc37 2022-10-23 stsp log_warnx("uid %d: %s", client->euid, err->msg);
1433 13b2bc37 2022-10-23 stsp case GOTD_IMSG_PACKFILE_INSTALL:
1434 13b2bc37 2022-10-23 stsp case GOTD_IMSG_REF_UPDATES_START:
1435 13b2bc37 2022-10-23 stsp case GOTD_IMSG_REF_UPDATE:
1436 13b2bc37 2022-10-23 stsp err = ensure_proc_is_writing(client, proc);
1438 13b2bc37 2022-10-23 stsp log_warnx("uid %d: %s", client->euid, err->msg);
1443 13b2bc37 2022-10-23 stsp log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
1447 13b2bc37 2022-10-23 stsp return ret;
1450 13b2bc37 2022-10-23 stsp static const struct got_error *
1451 b50a2b46 2022-12-29 stsp list_refs_request(struct gotd_client *client, struct gotd_imsgev *iev)
1453 b50a2b46 2022-12-29 stsp static const struct got_error *err;
1454 b50a2b46 2022-12-29 stsp struct gotd_imsg_list_refs_internal ilref;
1457 b50a2b46 2022-12-29 stsp memset(&ilref, 0, sizeof(ilref));
1458 b50a2b46 2022-12-29 stsp ilref.client_id = client->id;
1460 b50a2b46 2022-12-29 stsp fd = dup(client->fd);
1461 b50a2b46 2022-12-29 stsp if (fd == -1)
1462 b50a2b46 2022-12-29 stsp return got_error_from_errno("dup");
1464 b50a2b46 2022-12-29 stsp if (gotd_imsg_compose_event(iev, GOTD_IMSG_LIST_REFS_INTERNAL,
1465 b50a2b46 2022-12-29 stsp PROC_GOTD, fd, &ilref, sizeof(ilref)) == -1) {
1466 b50a2b46 2022-12-29 stsp err = got_error_from_errno("imsg compose WANT");
1467 b50a2b46 2022-12-29 stsp close(fd);
1468 b50a2b46 2022-12-29 stsp return err;
1471 b50a2b46 2022-12-29 stsp client->state = GOTD_STATE_EXPECT_CAPABILITIES;
1472 b50a2b46 2022-12-29 stsp log_debug("uid %d: expecting capabilities", client->euid);
1473 b50a2b46 2022-12-29 stsp return NULL;
1476 b50a2b46 2022-12-29 stsp static const struct got_error *
1477 13b2bc37 2022-10-23 stsp recv_packfile_done(uint32_t *client_id, struct imsg *imsg)
1479 13b2bc37 2022-10-23 stsp struct gotd_imsg_packfile_done idone;
1480 13b2bc37 2022-10-23 stsp size_t datalen;
1482 13b2bc37 2022-10-23 stsp log_debug("packfile-done received");
1484 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1485 13b2bc37 2022-10-23 stsp if (datalen != sizeof(idone))
1486 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1487 13b2bc37 2022-10-23 stsp memcpy(&idone, imsg->data, sizeof(idone));
1489 13b2bc37 2022-10-23 stsp *client_id = idone.client_id;
1490 13b2bc37 2022-10-23 stsp return NULL;
1493 13b2bc37 2022-10-23 stsp static const struct got_error *
1494 13b2bc37 2022-10-23 stsp recv_packfile_install(uint32_t *client_id, struct imsg *imsg)
1496 13b2bc37 2022-10-23 stsp struct gotd_imsg_packfile_install inst;
1497 13b2bc37 2022-10-23 stsp size_t datalen;
1499 13b2bc37 2022-10-23 stsp log_debug("packfile-install received");
1501 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1502 13b2bc37 2022-10-23 stsp if (datalen != sizeof(inst))
1503 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1504 13b2bc37 2022-10-23 stsp memcpy(&inst, imsg->data, sizeof(inst));
1506 13b2bc37 2022-10-23 stsp *client_id = inst.client_id;
1507 13b2bc37 2022-10-23 stsp return NULL;
1510 13b2bc37 2022-10-23 stsp static const struct got_error *
1511 13b2bc37 2022-10-23 stsp recv_ref_updates_start(uint32_t *client_id, struct imsg *imsg)
1513 13b2bc37 2022-10-23 stsp struct gotd_imsg_ref_updates_start istart;
1514 13b2bc37 2022-10-23 stsp size_t datalen;
1516 13b2bc37 2022-10-23 stsp log_debug("ref-updates-start received");
1518 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1519 13b2bc37 2022-10-23 stsp if (datalen != sizeof(istart))
1520 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1521 13b2bc37 2022-10-23 stsp memcpy(&istart, imsg->data, sizeof(istart));
1523 13b2bc37 2022-10-23 stsp *client_id = istart.client_id;
1524 13b2bc37 2022-10-23 stsp return NULL;
1527 13b2bc37 2022-10-23 stsp static const struct got_error *
1528 13b2bc37 2022-10-23 stsp recv_ref_update(uint32_t *client_id, struct imsg *imsg)
1530 13b2bc37 2022-10-23 stsp struct gotd_imsg_ref_update iref;
1531 13b2bc37 2022-10-23 stsp size_t datalen;
1533 13b2bc37 2022-10-23 stsp log_debug("ref-update received");
1535 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1536 13b2bc37 2022-10-23 stsp if (datalen < sizeof(iref))
1537 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1538 13b2bc37 2022-10-23 stsp memcpy(&iref, imsg->data, sizeof(iref));
1540 13b2bc37 2022-10-23 stsp *client_id = iref.client_id;
1541 13b2bc37 2022-10-23 stsp return NULL;
1544 13b2bc37 2022-10-23 stsp static const struct got_error *
1545 13b2bc37 2022-10-23 stsp send_ref_update_ok(struct gotd_client *client,
1546 13b2bc37 2022-10-23 stsp struct gotd_imsg_ref_update *iref, const char *refname)
1548 13b2bc37 2022-10-23 stsp struct gotd_imsg_ref_update_ok iok;
1549 13b2bc37 2022-10-23 stsp struct ibuf *wbuf;
1550 13b2bc37 2022-10-23 stsp size_t len;
1552 13b2bc37 2022-10-23 stsp memset(&iok, 0, sizeof(iok));
1553 13b2bc37 2022-10-23 stsp iok.client_id = client->id;
1554 13b2bc37 2022-10-23 stsp memcpy(iok.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1555 13b2bc37 2022-10-23 stsp memcpy(iok.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1556 13b2bc37 2022-10-23 stsp iok.name_len = strlen(refname);
1558 13b2bc37 2022-10-23 stsp len = sizeof(iok) + iok.name_len;
1559 13b2bc37 2022-10-23 stsp wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_OK,
1560 13b2bc37 2022-10-23 stsp PROC_GOTD, gotd.pid, len);
1561 13b2bc37 2022-10-23 stsp if (wbuf == NULL)
1562 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_create REF_UPDATE_OK");
1564 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, &iok, sizeof(iok)) == -1)
1565 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add REF_UPDATE_OK");
1566 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, refname, iok.name_len) == -1)
1567 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add REF_UPDATE_OK");
1569 13b2bc37 2022-10-23 stsp wbuf->fd = -1;
1570 13b2bc37 2022-10-23 stsp imsg_close(&client->iev.ibuf, wbuf);
1571 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(&client->iev);
1572 13b2bc37 2022-10-23 stsp return NULL;
1575 13b2bc37 2022-10-23 stsp static void
1576 13b2bc37 2022-10-23 stsp send_refs_updated(struct gotd_client *client)
1578 13b2bc37 2022-10-23 stsp if (gotd_imsg_compose_event(&client->iev,
1579 13b2bc37 2022-10-23 stsp GOTD_IMSG_REFS_UPDATED, PROC_GOTD, -1, NULL, 0) == -1)
1580 13b2bc37 2022-10-23 stsp log_warn("imsg compose REFS_UPDATED");
1583 13b2bc37 2022-10-23 stsp static const struct got_error *
1584 13b2bc37 2022-10-23 stsp send_ref_update_ng(struct gotd_client *client,
1585 13b2bc37 2022-10-23 stsp struct gotd_imsg_ref_update *iref, const char *refname,
1586 13b2bc37 2022-10-23 stsp const char *reason)
1588 13b2bc37 2022-10-23 stsp const struct got_error *ng_err;
1589 13b2bc37 2022-10-23 stsp struct gotd_imsg_ref_update_ng ing;
1590 13b2bc37 2022-10-23 stsp struct ibuf *wbuf;
1591 13b2bc37 2022-10-23 stsp size_t len;
1593 13b2bc37 2022-10-23 stsp memset(&ing, 0, sizeof(ing));
1594 13b2bc37 2022-10-23 stsp ing.client_id = client->id;
1595 13b2bc37 2022-10-23 stsp memcpy(ing.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1596 13b2bc37 2022-10-23 stsp memcpy(ing.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1597 13b2bc37 2022-10-23 stsp ing.name_len = strlen(refname);
1599 13b2bc37 2022-10-23 stsp ng_err = got_error_fmt(GOT_ERR_REF_BUSY, "%s", reason);
1600 13b2bc37 2022-10-23 stsp ing.reason_len = strlen(ng_err->msg);
1602 13b2bc37 2022-10-23 stsp len = sizeof(ing) + ing.name_len + ing.reason_len;
1603 13b2bc37 2022-10-23 stsp wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_NG,
1604 13b2bc37 2022-10-23 stsp PROC_GOTD, gotd.pid, len);
1605 13b2bc37 2022-10-23 stsp if (wbuf == NULL)
1606 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_create REF_UPDATE_NG");
1608 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, &ing, sizeof(ing)) == -1)
1609 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add REF_UPDATE_NG");
1610 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, refname, ing.name_len) == -1)
1611 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add REF_UPDATE_NG");
1612 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, ng_err->msg, ing.reason_len) == -1)
1613 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add REF_UPDATE_NG");
1615 13b2bc37 2022-10-23 stsp wbuf->fd = -1;
1616 13b2bc37 2022-10-23 stsp imsg_close(&client->iev.ibuf, wbuf);
1617 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(&client->iev);
1618 13b2bc37 2022-10-23 stsp return NULL;
1621 13b2bc37 2022-10-23 stsp static const struct got_error *
1622 13b2bc37 2022-10-23 stsp install_pack(struct gotd_client *client, const char *repo_path,
1623 13b2bc37 2022-10-23 stsp struct imsg *imsg)
1625 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
1626 13b2bc37 2022-10-23 stsp struct gotd_imsg_packfile_install inst;
1627 13b2bc37 2022-10-23 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
1628 13b2bc37 2022-10-23 stsp size_t datalen;
1629 13b2bc37 2022-10-23 stsp char *packfile_path = NULL, *packidx_path = NULL;
1631 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1632 13b2bc37 2022-10-23 stsp if (datalen != sizeof(inst))
1633 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1634 13b2bc37 2022-10-23 stsp memcpy(&inst, imsg->data, sizeof(inst));
1636 13b2bc37 2022-10-23 stsp if (client->packfile_path == NULL)
1637 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
1638 13b2bc37 2022-10-23 stsp "client has no pack file");
1639 13b2bc37 2022-10-23 stsp if (client->packidx_path == NULL)
1640 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
1641 13b2bc37 2022-10-23 stsp "client has no pack file index");
1643 13b2bc37 2022-10-23 stsp if (got_sha1_digest_to_str(inst.pack_sha1, hex, sizeof(hex)) == NULL)
1644 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_NO_SPACE,
1645 13b2bc37 2022-10-23 stsp "could not convert pack file SHA1 to hex");
1647 13b2bc37 2022-10-23 stsp if (asprintf(&packfile_path, "/%s/%s/pack-%s.pack",
1648 13b2bc37 2022-10-23 stsp repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1649 13b2bc37 2022-10-23 stsp err = got_error_from_errno("asprintf");
1650 13b2bc37 2022-10-23 stsp goto done;
1653 13b2bc37 2022-10-23 stsp if (asprintf(&packidx_path, "/%s/%s/pack-%s.idx",
1654 13b2bc37 2022-10-23 stsp repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1655 13b2bc37 2022-10-23 stsp err = got_error_from_errno("asprintf");
1656 13b2bc37 2022-10-23 stsp goto done;
1659 13b2bc37 2022-10-23 stsp if (rename(client->packfile_path, packfile_path) == -1) {
1660 13b2bc37 2022-10-23 stsp err = got_error_from_errno3("rename", client->packfile_path,
1661 13b2bc37 2022-10-23 stsp packfile_path);
1662 13b2bc37 2022-10-23 stsp goto done;
1665 13b2bc37 2022-10-23 stsp free(client->packfile_path);
1666 13b2bc37 2022-10-23 stsp client->packfile_path = NULL;
1668 13b2bc37 2022-10-23 stsp if (rename(client->packidx_path, packidx_path) == -1) {
1669 13b2bc37 2022-10-23 stsp err = got_error_from_errno3("rename", client->packidx_path,
1670 13b2bc37 2022-10-23 stsp packidx_path);
1671 13b2bc37 2022-10-23 stsp goto done;
1674 13b2bc37 2022-10-23 stsp free(client->packidx_path);
1675 13b2bc37 2022-10-23 stsp client->packidx_path = NULL;
1677 13b2bc37 2022-10-23 stsp free(packfile_path);
1678 13b2bc37 2022-10-23 stsp free(packidx_path);
1679 13b2bc37 2022-10-23 stsp return err;
1682 13b2bc37 2022-10-23 stsp static const struct got_error *
1683 13b2bc37 2022-10-23 stsp begin_ref_updates(struct gotd_client *client, struct imsg *imsg)
1685 13b2bc37 2022-10-23 stsp struct gotd_imsg_ref_updates_start istart;
1686 13b2bc37 2022-10-23 stsp size_t datalen;
1688 13b2bc37 2022-10-23 stsp if (client->nref_updates != -1)
1689 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1691 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1692 13b2bc37 2022-10-23 stsp if (datalen != sizeof(istart))
1693 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1694 13b2bc37 2022-10-23 stsp memcpy(&istart, imsg->data, sizeof(istart));
1696 13b2bc37 2022-10-23 stsp if (istart.nref_updates <= 0)
1697 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1699 13b2bc37 2022-10-23 stsp client->nref_updates = istart.nref_updates;
1700 13b2bc37 2022-10-23 stsp return NULL;
1703 13b2bc37 2022-10-23 stsp static const struct got_error *
1704 13b2bc37 2022-10-23 stsp update_ref(struct gotd_client *client, const char *repo_path,
1705 13b2bc37 2022-10-23 stsp struct imsg *imsg)
1707 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
1708 13b2bc37 2022-10-23 stsp struct got_repository *repo = NULL;
1709 13b2bc37 2022-10-23 stsp struct got_reference *ref = NULL;
1710 13b2bc37 2022-10-23 stsp struct gotd_imsg_ref_update iref;
1711 13b2bc37 2022-10-23 stsp struct got_object_id old_id, new_id;
1712 13b2bc37 2022-10-23 stsp struct got_object_id *id = NULL;
1713 13b2bc37 2022-10-23 stsp struct got_object *obj = NULL;
1714 13b2bc37 2022-10-23 stsp char *refname = NULL;
1715 13b2bc37 2022-10-23 stsp size_t datalen;
1716 13b2bc37 2022-10-23 stsp int locked = 0;
1718 13b2bc37 2022-10-23 stsp log_debug("update-ref from uid %d", client->euid);
1720 13b2bc37 2022-10-23 stsp if (client->nref_updates <= 0)
1721 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1723 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1724 13b2bc37 2022-10-23 stsp if (datalen < sizeof(iref))
1725 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1726 13b2bc37 2022-10-23 stsp memcpy(&iref, imsg->data, sizeof(iref));
1727 13b2bc37 2022-10-23 stsp if (datalen != sizeof(iref) + iref.name_len)
1728 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1729 13b2bc37 2022-10-23 stsp refname = malloc(iref.name_len + 1);
1730 13b2bc37 2022-10-23 stsp if (refname == NULL)
1731 13b2bc37 2022-10-23 stsp return got_error_from_errno("malloc");
1732 13b2bc37 2022-10-23 stsp memcpy(refname, imsg->data + sizeof(iref), iref.name_len);
1733 13b2bc37 2022-10-23 stsp refname[iref.name_len] = '\0';
1735 13b2bc37 2022-10-23 stsp log_debug("updating ref %s for uid %d", refname, client->euid);
1737 13b2bc37 2022-10-23 stsp err = got_repo_open(&repo, repo_path, NULL, NULL);
1739 13b2bc37 2022-10-23 stsp goto done;
1741 13b2bc37 2022-10-23 stsp memcpy(old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
1742 13b2bc37 2022-10-23 stsp memcpy(new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
1743 13b2bc37 2022-10-23 stsp err = got_object_open(&obj, repo, &new_id);
1745 13b2bc37 2022-10-23 stsp goto done;
1747 13b2bc37 2022-10-23 stsp if (iref.ref_is_new) {
1748 13b2bc37 2022-10-23 stsp err = got_ref_open(&ref, repo, refname, 0);
1749 13b2bc37 2022-10-23 stsp if (err) {
1750 13b2bc37 2022-10-23 stsp if (err->code != GOT_ERR_NOT_REF)
1751 13b2bc37 2022-10-23 stsp goto done;
1752 13b2bc37 2022-10-23 stsp err = got_ref_alloc(&ref, refname, &new_id);
1754 13b2bc37 2022-10-23 stsp goto done;
1755 13b2bc37 2022-10-23 stsp err = got_ref_write(ref, repo); /* will lock/unlock */
1757 13b2bc37 2022-10-23 stsp goto done;
1759 13b2bc37 2022-10-23 stsp err = got_error_fmt(GOT_ERR_REF_BUSY,
1760 13b2bc37 2022-10-23 stsp "%s has been created by someone else "
1761 13b2bc37 2022-10-23 stsp "while transaction was in progress",
1762 13b2bc37 2022-10-23 stsp got_ref_get_name(ref));
1763 13b2bc37 2022-10-23 stsp goto done;
1766 13b2bc37 2022-10-23 stsp err = got_ref_open(&ref, repo, refname, 1 /* lock */);
1768 13b2bc37 2022-10-23 stsp goto done;
1769 13b2bc37 2022-10-23 stsp locked = 1;
1771 13b2bc37 2022-10-23 stsp err = got_ref_resolve(&id, repo, ref);
1773 13b2bc37 2022-10-23 stsp goto done;
1775 13b2bc37 2022-10-23 stsp if (got_object_id_cmp(id, &old_id) != 0) {
1776 13b2bc37 2022-10-23 stsp err = got_error_fmt(GOT_ERR_REF_BUSY,
1777 13b2bc37 2022-10-23 stsp "%s has been modified by someone else "
1778 13b2bc37 2022-10-23 stsp "while transaction was in progress",
1779 13b2bc37 2022-10-23 stsp got_ref_get_name(ref));
1780 13b2bc37 2022-10-23 stsp goto done;
1783 13b2bc37 2022-10-23 stsp err = got_ref_change_ref(ref, &new_id);
1785 13b2bc37 2022-10-23 stsp goto done;
1787 13b2bc37 2022-10-23 stsp err = got_ref_write(ref, repo);
1789 13b2bc37 2022-10-23 stsp goto done;
1792 13b2bc37 2022-10-23 stsp id = NULL;
1795 13b2bc37 2022-10-23 stsp if (err) {
1796 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_LOCKFILE_TIMEOUT) {
1797 13b2bc37 2022-10-23 stsp err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
1798 13b2bc37 2022-10-23 stsp "could not acquire exclusive file lock for %s",
1801 13b2bc37 2022-10-23 stsp send_ref_update_ng(client, &iref, refname, err->msg);
1803 13b2bc37 2022-10-23 stsp send_ref_update_ok(client, &iref, refname);
1805 13b2bc37 2022-10-23 stsp if (client->nref_updates > 0) {
1806 13b2bc37 2022-10-23 stsp client->nref_updates--;
1807 13b2bc37 2022-10-23 stsp if (client->nref_updates == 0)
1808 13b2bc37 2022-10-23 stsp send_refs_updated(client);
1811 13b2bc37 2022-10-23 stsp if (locked) {
1812 13b2bc37 2022-10-23 stsp const struct got_error *unlock_err;
1813 13b2bc37 2022-10-23 stsp unlock_err = got_ref_unlock(ref);
1814 13b2bc37 2022-10-23 stsp if (unlock_err && err == NULL)
1815 13b2bc37 2022-10-23 stsp err = unlock_err;
1818 13b2bc37 2022-10-23 stsp got_ref_close(ref);
1820 13b2bc37 2022-10-23 stsp got_object_close(obj);
1822 13b2bc37 2022-10-23 stsp got_repo_close(repo);
1823 13b2bc37 2022-10-23 stsp free(refname);
1825 13b2bc37 2022-10-23 stsp return err;
1828 13b2bc37 2022-10-23 stsp static void
1829 b50a2b46 2022-12-29 stsp gotd_dispatch_listener(int fd, short event, void *arg)
1831 13b2bc37 2022-10-23 stsp struct gotd_imsgev *iev = arg;
1832 13b2bc37 2022-10-23 stsp struct imsgbuf *ibuf = &iev->ibuf;
1833 b50a2b46 2022-12-29 stsp struct gotd_child_proc *proc = &gotd.listen_proc;
1834 b50a2b46 2022-12-29 stsp ssize_t n;
1835 b50a2b46 2022-12-29 stsp int shut = 0;
1836 b50a2b46 2022-12-29 stsp struct imsg imsg;
1838 b50a2b46 2022-12-29 stsp if (proc->iev.ibuf.fd != fd)
1839 b50a2b46 2022-12-29 stsp fatalx("%s: unexpected fd %d", __func__, fd);
1841 b50a2b46 2022-12-29 stsp if (event & EV_READ) {
1842 b50a2b46 2022-12-29 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1843 b50a2b46 2022-12-29 stsp fatal("imsg_read error");
1844 b50a2b46 2022-12-29 stsp if (n == 0) {
1845 b50a2b46 2022-12-29 stsp /* Connection closed. */
1847 b50a2b46 2022-12-29 stsp goto done;
1851 b50a2b46 2022-12-29 stsp if (event & EV_WRITE) {
1852 b50a2b46 2022-12-29 stsp n = msgbuf_write(&ibuf->w);
1853 b50a2b46 2022-12-29 stsp if (n == -1 && errno != EAGAIN)
1854 b50a2b46 2022-12-29 stsp fatal("msgbuf_write");
1855 b50a2b46 2022-12-29 stsp if (n == 0) {
1856 b50a2b46 2022-12-29 stsp /* Connection closed. */
1858 b50a2b46 2022-12-29 stsp goto done;
1862 b50a2b46 2022-12-29 stsp for (;;) {
1863 b50a2b46 2022-12-29 stsp const struct got_error *err = NULL;
1864 b50a2b46 2022-12-29 stsp struct gotd_client *client = NULL;
1865 b50a2b46 2022-12-29 stsp uint32_t client_id = 0;
1866 b50a2b46 2022-12-29 stsp int do_disconnect = 0;
1868 b50a2b46 2022-12-29 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
1869 b50a2b46 2022-12-29 stsp fatal("%s: imsg_get error", __func__);
1870 b50a2b46 2022-12-29 stsp if (n == 0) /* No more messages. */
1873 b50a2b46 2022-12-29 stsp switch (imsg.hdr.type) {
1874 b50a2b46 2022-12-29 stsp case GOTD_IMSG_ERROR:
1875 b50a2b46 2022-12-29 stsp do_disconnect = 1;
1876 b50a2b46 2022-12-29 stsp err = gotd_imsg_recv_error(&client_id, &imsg);
1878 b50a2b46 2022-12-29 stsp case GOTD_IMSG_CONNECT:
1879 b50a2b46 2022-12-29 stsp err = recv_connect(&client_id, &imsg);
1882 b50a2b46 2022-12-29 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1886 b50a2b46 2022-12-29 stsp client = find_client(client_id);
1887 b50a2b46 2022-12-29 stsp if (client == NULL) {
1888 b50a2b46 2022-12-29 stsp log_warnx("%s: client not found", __func__);
1889 b50a2b46 2022-12-29 stsp imsg_free(&imsg);
1894 b50a2b46 2022-12-29 stsp log_warnx("uid %d: %s", client->euid, err->msg);
1896 b50a2b46 2022-12-29 stsp if (do_disconnect) {
1898 b50a2b46 2022-12-29 stsp disconnect_on_error(client, err);
1900 b50a2b46 2022-12-29 stsp disconnect(client);
1903 b50a2b46 2022-12-29 stsp imsg_free(&imsg);
1906 b50a2b46 2022-12-29 stsp if (!shut) {
1907 b50a2b46 2022-12-29 stsp gotd_imsg_event_add(iev);
1909 b50a2b46 2022-12-29 stsp /* This pipe is dead. Remove its event handler */
1910 b50a2b46 2022-12-29 stsp event_del(&iev->ev);
1911 b50a2b46 2022-12-29 stsp event_loopexit(NULL);
1915 b50a2b46 2022-12-29 stsp static void
1916 5e25db14 2022-12-29 stsp gotd_dispatch_auth_child(int fd, short event, void *arg)
1918 5e25db14 2022-12-29 stsp const struct got_error *err = NULL;
1919 5e25db14 2022-12-29 stsp struct gotd_imsgev *iev = arg;
1920 5e25db14 2022-12-29 stsp struct imsgbuf *ibuf = &iev->ibuf;
1921 5e25db14 2022-12-29 stsp struct gotd_client *client;
1922 5e25db14 2022-12-29 stsp struct gotd_repo *repo = NULL;
1923 5e25db14 2022-12-29 stsp ssize_t n;
1924 5e25db14 2022-12-29 stsp int shut = 0;
1925 5e25db14 2022-12-29 stsp struct imsg imsg;
1926 5e25db14 2022-12-29 stsp uint32_t client_id = 0;
1927 5e25db14 2022-12-29 stsp int do_disconnect = 0;
1928 5e25db14 2022-12-29 stsp enum gotd_procid proc_type;
1930 5e25db14 2022-12-29 stsp client = find_client_by_proc_fd(fd);
1931 5e25db14 2022-12-29 stsp if (client == NULL)
1932 5e25db14 2022-12-29 stsp fatalx("cannot find client for fd %d", fd);
1934 5e25db14 2022-12-29 stsp if (client->auth == NULL)
1935 5e25db14 2022-12-29 stsp fatalx("cannot find auth child process for fd %d", fd);
1937 5e25db14 2022-12-29 stsp if (event & EV_READ) {
1938 5e25db14 2022-12-29 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1939 5e25db14 2022-12-29 stsp fatal("imsg_read error");
1940 5e25db14 2022-12-29 stsp if (n == 0) {
1941 5e25db14 2022-12-29 stsp /* Connection closed. */
1943 5e25db14 2022-12-29 stsp goto done;
1947 5e25db14 2022-12-29 stsp if (event & EV_WRITE) {
1948 5e25db14 2022-12-29 stsp n = msgbuf_write(&ibuf->w);
1949 5e25db14 2022-12-29 stsp if (n == -1 && errno != EAGAIN)
1950 5e25db14 2022-12-29 stsp fatal("msgbuf_write");
1951 5e25db14 2022-12-29 stsp if (n == 0) {
1952 5e25db14 2022-12-29 stsp /* Connection closed. */
1955 5e25db14 2022-12-29 stsp goto done;
1958 5e25db14 2022-12-29 stsp if (client->auth->iev.ibuf.fd != fd)
1959 5e25db14 2022-12-29 stsp fatalx("%s: unexpected fd %d", __func__, fd);
1961 5e25db14 2022-12-29 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
1962 5e25db14 2022-12-29 stsp fatal("%s: imsg_get error", __func__);
1963 5e25db14 2022-12-29 stsp if (n == 0) /* No more messages. */
1966 5e25db14 2022-12-29 stsp evtimer_del(&client->tmo);
1968 5e25db14 2022-12-29 stsp switch (imsg.hdr.type) {
1969 5e25db14 2022-12-29 stsp case GOTD_IMSG_ERROR:
1970 5e25db14 2022-12-29 stsp do_disconnect = 1;
1971 5e25db14 2022-12-29 stsp err = gotd_imsg_recv_error(&client_id, &imsg);
1973 5e25db14 2022-12-29 stsp case GOTD_IMSG_ACCESS_GRANTED:
1976 5e25db14 2022-12-29 stsp do_disconnect = 1;
1977 5e25db14 2022-12-29 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1981 5e25db14 2022-12-29 stsp if (!verify_imsg_src(client, client->auth, &imsg)) {
1982 5e25db14 2022-12-29 stsp do_disconnect = 1;
1983 5e25db14 2022-12-29 stsp log_debug("dropping imsg type %d from PID %d",
1984 5e25db14 2022-12-29 stsp imsg.hdr.type, client->auth->pid);
1986 5e25db14 2022-12-29 stsp imsg_free(&imsg);
1988 5e25db14 2022-12-29 stsp if (do_disconnect) {
1990 5e25db14 2022-12-29 stsp disconnect_on_error(client, err);
1992 5e25db14 2022-12-29 stsp disconnect(client);
1993 5e25db14 2022-12-29 stsp goto done;
1996 5e25db14 2022-12-29 stsp repo = find_repo_by_name(client->auth->repo_name);
1997 5e25db14 2022-12-29 stsp if (repo == NULL) {
1998 5e25db14 2022-12-29 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
1999 5e25db14 2022-12-29 stsp goto done;
2001 5e25db14 2022-12-29 stsp kill_auth_proc(client);
2003 5e25db14 2022-12-29 stsp log_info("authenticated uid %d for repository %s\n",
2004 5e25db14 2022-12-29 stsp client->euid, repo->name);
2006 5e25db14 2022-12-29 stsp if (client->required_auth & GOTD_AUTH_WRITE)
2007 5e25db14 2022-12-29 stsp proc_type = PROC_REPO_WRITE;
2009 5e25db14 2022-12-29 stsp proc_type = PROC_REPO_READ;
2011 7fdc3e58 2022-12-30 mark err = start_repo_child(client, proc_type, repo, gotd.argv0,
2012 7fdc3e58 2022-12-30 mark gotd.confpath, gotd.daemonize, gotd.verbosity);
2015 5e25db14 2022-12-29 stsp log_warnx("uid %d: %s", client->euid, err->msg);
2017 5e25db14 2022-12-29 stsp /* We might have killed the auth process by now. */
2018 5e25db14 2022-12-29 stsp if (client->auth != NULL) {
2019 5e25db14 2022-12-29 stsp if (!shut) {
2020 5e25db14 2022-12-29 stsp gotd_imsg_event_add(iev);
2022 5e25db14 2022-12-29 stsp /* This pipe is dead. Remove its event handler */
2023 5e25db14 2022-12-29 stsp event_del(&iev->ev);
2028 5e25db14 2022-12-29 stsp static void
2029 b50a2b46 2022-12-29 stsp gotd_dispatch_repo_child(int fd, short event, void *arg)
2031 b50a2b46 2022-12-29 stsp struct gotd_imsgev *iev = arg;
2032 b50a2b46 2022-12-29 stsp struct imsgbuf *ibuf = &iev->ibuf;
2033 13b2bc37 2022-10-23 stsp struct gotd_child_proc *proc = NULL;
2034 b50a2b46 2022-12-29 stsp struct gotd_client *client = NULL;
2035 13b2bc37 2022-10-23 stsp ssize_t n;
2036 13b2bc37 2022-10-23 stsp int shut = 0;
2037 13b2bc37 2022-10-23 stsp struct imsg imsg;
2039 13b2bc37 2022-10-23 stsp if (event & EV_READ) {
2040 13b2bc37 2022-10-23 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
2041 13b2bc37 2022-10-23 stsp fatal("imsg_read error");
2042 13b2bc37 2022-10-23 stsp if (n == 0) {
2043 13b2bc37 2022-10-23 stsp /* Connection closed. */
2045 13b2bc37 2022-10-23 stsp goto done;
2049 13b2bc37 2022-10-23 stsp if (event & EV_WRITE) {
2050 13b2bc37 2022-10-23 stsp n = msgbuf_write(&ibuf->w);
2051 13b2bc37 2022-10-23 stsp if (n == -1 && errno != EAGAIN)
2052 13b2bc37 2022-10-23 stsp fatal("msgbuf_write");
2053 13b2bc37 2022-10-23 stsp if (n == 0) {
2054 13b2bc37 2022-10-23 stsp /* Connection closed. */
2056 13b2bc37 2022-10-23 stsp goto done;
2060 b50a2b46 2022-12-29 stsp client = find_client_by_proc_fd(fd);
2061 b50a2b46 2022-12-29 stsp if (client == NULL)
2062 b50a2b46 2022-12-29 stsp fatalx("cannot find client for fd %d", fd);
2064 b50a2b46 2022-12-29 stsp proc = get_client_proc(client);
2065 13b2bc37 2022-10-23 stsp if (proc == NULL)
2066 13b2bc37 2022-10-23 stsp fatalx("cannot find child process for fd %d", fd);
2068 13b2bc37 2022-10-23 stsp for (;;) {
2069 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
2070 13b2bc37 2022-10-23 stsp uint32_t client_id = 0;
2071 13b2bc37 2022-10-23 stsp int do_disconnect = 0;
2072 b50a2b46 2022-12-29 stsp int do_list_refs = 0, do_ref_updates = 0, do_ref_update = 0;
2073 13b2bc37 2022-10-23 stsp int do_packfile_install = 0;
2075 13b2bc37 2022-10-23 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
2076 13b2bc37 2022-10-23 stsp fatal("%s: imsg_get error", __func__);
2077 13b2bc37 2022-10-23 stsp if (n == 0) /* No more messages. */
2080 13b2bc37 2022-10-23 stsp switch (imsg.hdr.type) {
2081 13b2bc37 2022-10-23 stsp case GOTD_IMSG_ERROR:
2082 13b2bc37 2022-10-23 stsp do_disconnect = 1;
2083 13b2bc37 2022-10-23 stsp err = gotd_imsg_recv_error(&client_id, &imsg);
2085 b50a2b46 2022-12-29 stsp case GOTD_IMSG_REPO_CHILD_READY:
2086 b50a2b46 2022-12-29 stsp do_list_refs = 1;
2088 13b2bc37 2022-10-23 stsp case GOTD_IMSG_PACKFILE_DONE:
2089 13b2bc37 2022-10-23 stsp do_disconnect = 1;
2090 13b2bc37 2022-10-23 stsp err = recv_packfile_done(&client_id, &imsg);
2092 13b2bc37 2022-10-23 stsp case GOTD_IMSG_PACKFILE_INSTALL:
2093 13b2bc37 2022-10-23 stsp err = recv_packfile_install(&client_id, &imsg);
2094 13b2bc37 2022-10-23 stsp if (err == NULL)
2095 13b2bc37 2022-10-23 stsp do_packfile_install = 1;
2097 13b2bc37 2022-10-23 stsp case GOTD_IMSG_REF_UPDATES_START:
2098 13b2bc37 2022-10-23 stsp err = recv_ref_updates_start(&client_id, &imsg);
2099 13b2bc37 2022-10-23 stsp if (err == NULL)
2100 13b2bc37 2022-10-23 stsp do_ref_updates = 1;
2102 13b2bc37 2022-10-23 stsp case GOTD_IMSG_REF_UPDATE:
2103 13b2bc37 2022-10-23 stsp err = recv_ref_update(&client_id, &imsg);
2104 13b2bc37 2022-10-23 stsp if (err == NULL)
2105 13b2bc37 2022-10-23 stsp do_ref_update = 1;
2108 13b2bc37 2022-10-23 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
2112 13b2bc37 2022-10-23 stsp if (!verify_imsg_src(client, proc, &imsg)) {
2113 13b2bc37 2022-10-23 stsp log_debug("dropping imsg type %d from PID %d",
2114 13b2bc37 2022-10-23 stsp imsg.hdr.type, proc->pid);
2115 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
2119 13b2bc37 2022-10-23 stsp log_warnx("uid %d: %s", client->euid, err->msg);
2121 13b2bc37 2022-10-23 stsp if (do_disconnect) {
2123 13b2bc37 2022-10-23 stsp disconnect_on_error(client, err);
2125 13b2bc37 2022-10-23 stsp disconnect(client);
2127 b50a2b46 2022-12-29 stsp if (do_list_refs)
2128 b50a2b46 2022-12-29 stsp err = list_refs_request(client, iev);
2129 b50a2b46 2022-12-29 stsp else if (do_packfile_install)
2130 eec68231 2022-12-14 stsp err = install_pack(client, proc->repo_path,
2132 36c7cfbb 2022-11-04 stsp else if (do_ref_updates)
2133 36c7cfbb 2022-11-04 stsp err = begin_ref_updates(client, &imsg);
2134 36c7cfbb 2022-11-04 stsp else if (do_ref_update)
2135 eec68231 2022-12-14 stsp err = update_ref(client, proc->repo_path,
2138 36c7cfbb 2022-11-04 stsp log_warnx("uid %d: %s", client->euid, err->msg);
2140 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
2143 13b2bc37 2022-10-23 stsp if (!shut) {
2144 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(iev);
2146 13b2bc37 2022-10-23 stsp /* This pipe is dead. Remove its event handler */
2147 13b2bc37 2022-10-23 stsp event_del(&iev->ev);
2148 13b2bc37 2022-10-23 stsp event_loopexit(NULL);
2152 13b2bc37 2022-10-23 stsp static pid_t
2153 eec68231 2022-12-14 stsp start_child(enum gotd_procid proc_id, const char *repo_path,
2154 585362fd 2022-10-31 op char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
2156 585362fd 2022-10-31 op char *argv[11];
2157 13b2bc37 2022-10-23 stsp int argc = 0;
2158 13b2bc37 2022-10-23 stsp pid_t pid;
2160 13b2bc37 2022-10-23 stsp switch (pid = fork()) {
2162 13b2bc37 2022-10-23 stsp fatal("cannot fork");
2166 13b2bc37 2022-10-23 stsp close(fd);
2167 13b2bc37 2022-10-23 stsp return pid;
2170 8c6fc146 2022-11-17 stsp if (fd != GOTD_FILENO_MSG_PIPE) {
2171 8c6fc146 2022-11-17 stsp if (dup2(fd, GOTD_FILENO_MSG_PIPE) == -1)
2172 13b2bc37 2022-10-23 stsp fatal("cannot setup imsg fd");
2173 13b2bc37 2022-10-23 stsp } else if (fcntl(fd, F_SETFD, 0) == -1)
2174 13b2bc37 2022-10-23 stsp fatal("cannot setup imsg fd");
2176 13b2bc37 2022-10-23 stsp argv[argc++] = argv0;
2177 13b2bc37 2022-10-23 stsp switch (proc_id) {
2178 d93ecf7d 2022-12-14 stsp case PROC_LISTEN:
2179 d93ecf7d 2022-12-14 stsp argv[argc++] = (char *)"-L";
2181 5e25db14 2022-12-29 stsp case PROC_AUTH:
2182 5e25db14 2022-12-29 stsp argv[argc++] = (char *)"-A";
2184 13b2bc37 2022-10-23 stsp case PROC_REPO_READ:
2185 13b2bc37 2022-10-23 stsp argv[argc++] = (char *)"-R";
2187 13b2bc37 2022-10-23 stsp case PROC_REPO_WRITE:
2188 13b2bc37 2022-10-23 stsp argv[argc++] = (char *)"-W";
2191 13b2bc37 2022-10-23 stsp fatalx("invalid process id %d", proc_id);
2194 585362fd 2022-10-31 op argv[argc++] = (char *)"-f";
2195 585362fd 2022-10-31 op argv[argc++] = (char *)confpath;
2197 eec68231 2022-12-14 stsp if (repo_path) {
2198 d93ecf7d 2022-12-14 stsp argv[argc++] = (char *)"-P";
2199 eec68231 2022-12-14 stsp argv[argc++] = (char *)repo_path;
2202 13b2bc37 2022-10-23 stsp if (!daemonize)
2203 13b2bc37 2022-10-23 stsp argv[argc++] = (char *)"-d";
2204 13b2bc37 2022-10-23 stsp if (verbosity > 0)
2205 13b2bc37 2022-10-23 stsp argv[argc++] = (char *)"-v";
2206 13b2bc37 2022-10-23 stsp if (verbosity > 1)
2207 13b2bc37 2022-10-23 stsp argv[argc++] = (char *)"-v";
2208 13b2bc37 2022-10-23 stsp argv[argc++] = NULL;
2210 13b2bc37 2022-10-23 stsp execvp(argv0, argv);
2211 13b2bc37 2022-10-23 stsp fatal("execvp");
2214 13b2bc37 2022-10-23 stsp static void
2215 d93ecf7d 2022-12-14 stsp start_listener(char *argv0, const char *confpath, int daemonize, int verbosity)
2217 b50a2b46 2022-12-29 stsp struct gotd_child_proc *proc = &gotd.listen_proc;
2219 d93ecf7d 2022-12-14 stsp proc->type = PROC_LISTEN;
2221 d93ecf7d 2022-12-14 stsp if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
2222 d93ecf7d 2022-12-14 stsp PF_UNSPEC, proc->pipe) == -1)
2223 d93ecf7d 2022-12-14 stsp fatal("socketpair");
2225 d93ecf7d 2022-12-14 stsp proc->pid = start_child(proc->type, NULL, argv0, confpath,
2226 d93ecf7d 2022-12-14 stsp proc->pipe[1], daemonize, verbosity);
2227 d93ecf7d 2022-12-14 stsp imsg_init(&proc->iev.ibuf, proc->pipe[0]);
2228 b50a2b46 2022-12-29 stsp proc->iev.handler = gotd_dispatch_listener;
2229 d93ecf7d 2022-12-14 stsp proc->iev.events = EV_READ;
2230 d93ecf7d 2022-12-14 stsp proc->iev.handler_arg = NULL;
2233 b50a2b46 2022-12-29 stsp static const struct got_error *
2234 b50a2b46 2022-12-29 stsp start_repo_child(struct gotd_client *client, enum gotd_procid proc_type,
2235 b50a2b46 2022-12-29 stsp struct gotd_repo *repo, char *argv0, const char *confpath,
2236 585362fd 2022-10-31 op int daemonize, int verbosity)
2238 13b2bc37 2022-10-23 stsp struct gotd_child_proc *proc;
2240 b50a2b46 2022-12-29 stsp if (proc_type != PROC_REPO_READ && proc_type != PROC_REPO_WRITE)
2241 b50a2b46 2022-12-29 stsp return got_error_msg(GOT_ERR_NOT_IMPL, "bad process type");
2243 b50a2b46 2022-12-29 stsp proc = calloc(1, sizeof(*proc));
2244 b50a2b46 2022-12-29 stsp if (proc == NULL)
2245 b50a2b46 2022-12-29 stsp return got_error_from_errno("calloc");
2247 b50a2b46 2022-12-29 stsp proc->type = proc_type;
2248 b50a2b46 2022-12-29 stsp if (strlcpy(proc->repo_name, repo->name,
2249 b50a2b46 2022-12-29 stsp sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
2250 b50a2b46 2022-12-29 stsp fatalx("repository name too long: %s", repo->name);
2251 b50a2b46 2022-12-29 stsp log_debug("starting %s for repository %s",
2252 b50a2b46 2022-12-29 stsp proc->type == PROC_REPO_READ ? "reader" : "writer", repo->name);
2253 b50a2b46 2022-12-29 stsp if (realpath(repo->path, proc->repo_path) == NULL)
2254 e34d1285 2023-01-08 stsp return got_error_from_errno2("realpath", repo->path);
2255 b50a2b46 2022-12-29 stsp if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
2256 b50a2b46 2022-12-29 stsp PF_UNSPEC, proc->pipe) == -1)
2257 b50a2b46 2022-12-29 stsp fatal("socketpair");
2258 b50a2b46 2022-12-29 stsp proc->pid = start_child(proc->type, proc->repo_path, argv0,
2259 b50a2b46 2022-12-29 stsp confpath, proc->pipe[1], daemonize, verbosity);
2260 b50a2b46 2022-12-29 stsp imsg_init(&proc->iev.ibuf, proc->pipe[0]);
2261 b50a2b46 2022-12-29 stsp log_debug("proc %s %s is on fd %d",
2262 b50a2b46 2022-12-29 stsp gotd_proc_names[proc->type], proc->repo_path,
2263 b50a2b46 2022-12-29 stsp proc->pipe[0]);
2264 b50a2b46 2022-12-29 stsp proc->iev.handler = gotd_dispatch_repo_child;
2265 b50a2b46 2022-12-29 stsp proc->iev.events = EV_READ;
2266 b50a2b46 2022-12-29 stsp proc->iev.handler_arg = NULL;
2267 b50a2b46 2022-12-29 stsp event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
2268 b50a2b46 2022-12-29 stsp gotd_dispatch_repo_child, &proc->iev);
2269 b50a2b46 2022-12-29 stsp gotd_imsg_event_add(&proc->iev);
2271 b50a2b46 2022-12-29 stsp if (proc->type == PROC_REPO_READ)
2272 b50a2b46 2022-12-29 stsp client->repo_read = proc;
2274 b50a2b46 2022-12-29 stsp client->repo_write = proc;
2276 5e25db14 2022-12-29 stsp return NULL;
2279 5e25db14 2022-12-29 stsp static const struct got_error *
2280 5e25db14 2022-12-29 stsp start_auth_child(struct gotd_client *client, int required_auth,
2281 5e25db14 2022-12-29 stsp struct gotd_repo *repo, char *argv0, const char *confpath,
2282 5e25db14 2022-12-29 stsp int daemonize, int verbosity)
2284 365cf0f3 2022-12-29 stsp const struct got_error *err = NULL;
2285 5e25db14 2022-12-29 stsp struct gotd_child_proc *proc;
2286 5e25db14 2022-12-29 stsp struct gotd_imsg_auth iauth;
2289 5e25db14 2022-12-29 stsp memset(&iauth, 0, sizeof(iauth));
2291 365cf0f3 2022-12-29 stsp fd = dup(client->fd);
2292 365cf0f3 2022-12-29 stsp if (fd == -1)
2293 365cf0f3 2022-12-29 stsp return got_error_from_errno("dup");
2295 5e25db14 2022-12-29 stsp proc = calloc(1, sizeof(*proc));
2296 365cf0f3 2022-12-29 stsp if (proc == NULL) {
2297 365cf0f3 2022-12-29 stsp err = got_error_from_errno("calloc");
2298 365cf0f3 2022-12-29 stsp close(fd);
2299 365cf0f3 2022-12-29 stsp return err;
2302 5e25db14 2022-12-29 stsp proc->type = PROC_AUTH;
2303 5e25db14 2022-12-29 stsp if (strlcpy(proc->repo_name, repo->name,
2304 5e25db14 2022-12-29 stsp sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
2305 5e25db14 2022-12-29 stsp fatalx("repository name too long: %s", repo->name);
2306 5e25db14 2022-12-29 stsp log_debug("starting auth for uid %d repository %s",
2307 5e25db14 2022-12-29 stsp client->euid, repo->name);
2308 5e25db14 2022-12-29 stsp if (realpath(repo->path, proc->repo_path) == NULL)
2309 e34d1285 2023-01-08 stsp return got_error_from_errno2("realpath", repo->path);
2310 5e25db14 2022-12-29 stsp if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
2311 5e25db14 2022-12-29 stsp PF_UNSPEC, proc->pipe) == -1)
2312 5e25db14 2022-12-29 stsp fatal("socketpair");
2313 5e25db14 2022-12-29 stsp proc->pid = start_child(proc->type, proc->repo_path, argv0,
2314 5e25db14 2022-12-29 stsp confpath, proc->pipe[1], daemonize, verbosity);
2315 5e25db14 2022-12-29 stsp imsg_init(&proc->iev.ibuf, proc->pipe[0]);
2316 5e25db14 2022-12-29 stsp log_debug("proc %s %s is on fd %d",
2317 5e25db14 2022-12-29 stsp gotd_proc_names[proc->type], proc->repo_path,
2318 5e25db14 2022-12-29 stsp proc->pipe[0]);
2319 5e25db14 2022-12-29 stsp proc->iev.handler = gotd_dispatch_auth_child;
2320 5e25db14 2022-12-29 stsp proc->iev.events = EV_READ;
2321 5e25db14 2022-12-29 stsp proc->iev.handler_arg = NULL;
2322 5e25db14 2022-12-29 stsp event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
2323 5e25db14 2022-12-29 stsp gotd_dispatch_auth_child, &proc->iev);
2324 5e25db14 2022-12-29 stsp gotd_imsg_event_add(&proc->iev);
2326 5e25db14 2022-12-29 stsp iauth.euid = client->euid;
2327 5e25db14 2022-12-29 stsp iauth.egid = client->egid;
2328 5e25db14 2022-12-29 stsp iauth.required_auth = required_auth;
2329 5e25db14 2022-12-29 stsp iauth.client_id = client->id;
2330 5e25db14 2022-12-29 stsp if (gotd_imsg_compose_event(&proc->iev, GOTD_IMSG_AUTHENTICATE,
2331 365cf0f3 2022-12-29 stsp PROC_GOTD, fd, &iauth, sizeof(iauth)) == -1) {
2332 5e25db14 2022-12-29 stsp log_warn("imsg compose AUTHENTICATE");
2333 365cf0f3 2022-12-29 stsp close(fd);
2334 365cf0f3 2022-12-29 stsp /* Let the auth_timeout handler tidy up. */
2337 5e25db14 2022-12-29 stsp client->auth = proc;
2338 5e25db14 2022-12-29 stsp client->required_auth = required_auth;
2339 b50a2b46 2022-12-29 stsp return NULL;
2342 eec68231 2022-12-14 stsp static void
2343 eec68231 2022-12-14 stsp apply_unveil_repo_readonly(const char *repo_path)
2345 eec68231 2022-12-14 stsp if (unveil(repo_path, "r") == -1)
2346 eec68231 2022-12-14 stsp fatal("unveil %s", repo_path);
2348 44587340 2022-12-30 stsp if (unveil(NULL, NULL) == -1)
2349 44587340 2022-12-30 stsp fatal("unveil");
2352 44587340 2022-12-30 stsp static void
2353 44587340 2022-12-30 stsp apply_unveil_none(void)
2355 44587340 2022-12-30 stsp if (unveil("/", "") == -1)
2356 44587340 2022-12-30 stsp fatal("unveil");
2358 eec68231 2022-12-14 stsp if (unveil(NULL, NULL) == -1)
2359 eec68231 2022-12-14 stsp fatal("unveil");
2362 13b2bc37 2022-10-23 stsp static void
2363 13b2bc37 2022-10-23 stsp apply_unveil(void)
2365 13b2bc37 2022-10-23 stsp struct gotd_repo *repo;
2367 b50a2b46 2022-12-29 stsp if (unveil(gotd.argv0, "x") == -1)
2368 b50a2b46 2022-12-29 stsp fatal("unveil %s", gotd.argv0);
2370 13b2bc37 2022-10-23 stsp TAILQ_FOREACH(repo, &gotd.repos, entry) {
2371 13b2bc37 2022-10-23 stsp if (unveil(repo->path, "rwc") == -1)
2372 13b2bc37 2022-10-23 stsp fatal("unveil %s", repo->path);
2375 13b2bc37 2022-10-23 stsp if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
2376 13b2bc37 2022-10-23 stsp fatal("unveil %s", GOT_TMPDIR_STR);
2378 13b2bc37 2022-10-23 stsp if (unveil(NULL, NULL) == -1)
2379 13b2bc37 2022-10-23 stsp fatal("unveil");
2383 13b2bc37 2022-10-23 stsp main(int argc, char **argv)
2385 13b2bc37 2022-10-23 stsp const struct got_error *error = NULL;
2386 13b2bc37 2022-10-23 stsp int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
2387 13b2bc37 2022-10-23 stsp const char *confpath = GOTD_CONF_PATH;
2388 13b2bc37 2022-10-23 stsp char *argv0 = argv[0];
2389 13b2bc37 2022-10-23 stsp char title[2048];
2390 13b2bc37 2022-10-23 stsp struct passwd *pw = NULL;
2391 13b2bc37 2022-10-23 stsp char *repo_path = NULL;
2392 13b2bc37 2022-10-23 stsp enum gotd_procid proc_id = PROC_GOTD;
2393 13b2bc37 2022-10-23 stsp struct event evsigint, evsigterm, evsighup, evsigusr1;
2394 13b2bc37 2022-10-23 stsp int *pack_fds = NULL, *temp_fds = NULL;
2396 13b2bc37 2022-10-23 stsp log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
2398 5e25db14 2022-12-29 stsp while ((ch = getopt(argc, argv, "Adf:LnP:RvW")) != -1) {
2399 13b2bc37 2022-10-23 stsp switch (ch) {
2401 5e25db14 2022-12-29 stsp proc_id = PROC_AUTH;
2404 13b2bc37 2022-10-23 stsp daemonize = 0;
2407 13b2bc37 2022-10-23 stsp confpath = optarg;
2410 d93ecf7d 2022-12-14 stsp proc_id = PROC_LISTEN;
2413 13b2bc37 2022-10-23 stsp noaction = 1;
2416 6f319063 2022-10-27 stsp repo_path = realpath(optarg, NULL);
2417 6f319063 2022-10-27 stsp if (repo_path == NULL)
2418 6f319063 2022-10-27 stsp fatal("realpath '%s'", optarg);
2421 13b2bc37 2022-10-23 stsp proc_id = PROC_REPO_READ;
2424 6f319063 2022-10-27 stsp if (verbosity < 3)
2425 6f319063 2022-10-27 stsp verbosity++;
2428 13b2bc37 2022-10-23 stsp proc_id = PROC_REPO_WRITE;
2435 13b2bc37 2022-10-23 stsp argc -= optind;
2436 13b2bc37 2022-10-23 stsp argv += optind;
2438 13b2bc37 2022-10-23 stsp if (argc != 0)
2441 b50a2b46 2022-12-29 stsp /* Require an absolute path in argv[0] for reliable re-exec. */
2442 b50a2b46 2022-12-29 stsp if (!got_path_is_absolute(argv0))
2443 b50a2b46 2022-12-29 stsp fatalx("bad path \"%s\": must be an absolute path", argv0);
2445 b50a2b46 2022-12-29 stsp if (geteuid() && (proc_id == PROC_GOTD || proc_id == PROC_LISTEN))
2446 13b2bc37 2022-10-23 stsp fatalx("need root privileges");
2448 13b2bc37 2022-10-23 stsp log_init(daemonize ? 0 : 1, LOG_DAEMON);
2449 13b2bc37 2022-10-23 stsp log_setverbose(verbosity);
2451 13b2bc37 2022-10-23 stsp if (parse_config(confpath, proc_id, &gotd) != 0)
2454 b50a2b46 2022-12-29 stsp gotd.argv0 = argv0;
2455 b50a2b46 2022-12-29 stsp gotd.daemonize = daemonize;
2456 b50a2b46 2022-12-29 stsp gotd.verbosity = verbosity;
2457 b50a2b46 2022-12-29 stsp gotd.confpath = confpath;
2459 13b2bc37 2022-10-23 stsp if (proc_id == PROC_GOTD &&
2460 13b2bc37 2022-10-23 stsp (gotd.nrepos == 0 || TAILQ_EMPTY(&gotd.repos)))
2461 13b2bc37 2022-10-23 stsp fatalx("no repository defined in configuration file");
2463 13b2bc37 2022-10-23 stsp pw = getpwnam(gotd.user_name);
2464 13b2bc37 2022-10-23 stsp if (pw == NULL)
2465 898c8f8f 2022-12-29 op fatalx("user %s not found", gotd.user_name);
2467 13b2bc37 2022-10-23 stsp if (pw->pw_uid == 0) {
2468 13b2bc37 2022-10-23 stsp fatalx("cannot run %s as %s: the user running %s "
2469 13b2bc37 2022-10-23 stsp "must not be the superuser",
2470 13b2bc37 2022-10-23 stsp getprogname(), pw->pw_name, getprogname());
2473 d93ecf7d 2022-12-14 stsp if (proc_id == PROC_LISTEN &&
2474 13b2bc37 2022-10-23 stsp !got_path_is_absolute(gotd.unix_socket_path))
2475 13b2bc37 2022-10-23 stsp fatalx("bad unix socket path \"%s\": must be an absolute path",
2476 13b2bc37 2022-10-23 stsp gotd.unix_socket_path);
2478 13b2bc37 2022-10-23 stsp if (noaction)
2481 b1142068 2022-12-05 stsp if (proc_id == PROC_GOTD) {
2482 d93ecf7d 2022-12-14 stsp gotd.pid = getpid();
2483 d93ecf7d 2022-12-14 stsp snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2484 d93ecf7d 2022-12-14 stsp start_listener(argv0, confpath, daemonize, verbosity);
2485 d93ecf7d 2022-12-14 stsp arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
2486 d93ecf7d 2022-12-14 stsp if (daemonize && daemon(1, 0) == -1)
2487 d93ecf7d 2022-12-14 stsp fatal("daemon");
2488 d93ecf7d 2022-12-14 stsp } else if (proc_id == PROC_LISTEN) {
2489 d93ecf7d 2022-12-14 stsp snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2490 b1142068 2022-12-05 stsp if (verbosity) {
2491 b1142068 2022-12-05 stsp log_info("socket: %s", gotd.unix_socket_path);
2492 b1142068 2022-12-05 stsp log_info("user: %s", pw->pw_name);
2495 13b2bc37 2022-10-23 stsp fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
2496 6f854dde 2023-01-04 stsp pw->pw_gid);
2497 13b2bc37 2022-10-23 stsp if (fd == -1) {
2498 13b2bc37 2022-10-23 stsp fatal("cannot listen on unix socket %s",
2499 13b2bc37 2022-10-23 stsp gotd.unix_socket_path);
2501 eec68231 2022-12-14 stsp if (daemonize && daemon(0, 0) == -1)
2502 13b2bc37 2022-10-23 stsp fatal("daemon");
2503 5e25db14 2022-12-29 stsp } else if (proc_id == PROC_AUTH) {
2504 5e25db14 2022-12-29 stsp snprintf(title, sizeof(title), "%s %s",
2505 5e25db14 2022-12-29 stsp gotd_proc_names[proc_id], repo_path);
2506 5e25db14 2022-12-29 stsp if (daemonize && daemon(0, 0) == -1)
2507 5e25db14 2022-12-29 stsp fatal("daemon");
2508 13b2bc37 2022-10-23 stsp } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE) {
2509 13b2bc37 2022-10-23 stsp error = got_repo_pack_fds_open(&pack_fds);
2510 13b2bc37 2022-10-23 stsp if (error != NULL)
2511 13b2bc37 2022-10-23 stsp fatalx("cannot open pack tempfiles: %s", error->msg);
2512 13b2bc37 2022-10-23 stsp error = got_repo_temp_fds_open(&temp_fds);
2513 13b2bc37 2022-10-23 stsp if (error != NULL)
2514 13b2bc37 2022-10-23 stsp fatalx("cannot open pack tempfiles: %s", error->msg);
2515 13b2bc37 2022-10-23 stsp if (repo_path == NULL)
2516 13b2bc37 2022-10-23 stsp fatalx("repository path not specified");
2517 13b2bc37 2022-10-23 stsp snprintf(title, sizeof(title), "%s %s",
2518 13b2bc37 2022-10-23 stsp gotd_proc_names[proc_id], repo_path);
2519 eec68231 2022-12-14 stsp if (daemonize && daemon(0, 0) == -1)
2520 13b2bc37 2022-10-23 stsp fatal("daemon");
2522 13b2bc37 2022-10-23 stsp fatal("invalid process id %d", proc_id);
2524 13b2bc37 2022-10-23 stsp setproctitle("%s", title);
2525 13b2bc37 2022-10-23 stsp log_procinit(title);
2527 13b2bc37 2022-10-23 stsp /* Drop root privileges. */
2528 13b2bc37 2022-10-23 stsp if (setgid(pw->pw_gid) == -1)
2529 13b2bc37 2022-10-23 stsp fatal("setgid %d failed", pw->pw_gid);
2530 13b2bc37 2022-10-23 stsp if (setuid(pw->pw_uid) == -1)
2531 13b2bc37 2022-10-23 stsp fatal("setuid %d failed", pw->pw_uid);
2533 13b2bc37 2022-10-23 stsp event_init();
2535 13b2bc37 2022-10-23 stsp switch (proc_id) {
2536 13b2bc37 2022-10-23 stsp case PROC_GOTD:
2537 13b2bc37 2022-10-23 stsp #ifndef PROFILE
2538 5e25db14 2022-12-29 stsp if (pledge("stdio rpath wpath cpath proc exec "
2539 365cf0f3 2022-12-29 stsp "sendfd recvfd fattr flock unveil", NULL) == -1)
2540 13b2bc37 2022-10-23 stsp err(1, "pledge");
2543 d93ecf7d 2022-12-14 stsp case PROC_LISTEN:
2544 d93ecf7d 2022-12-14 stsp #ifndef PROFILE
2545 77f619a8 2023-01-04 stsp if (pledge("stdio sendfd unix unveil", NULL) == -1)
2546 d93ecf7d 2022-12-14 stsp err(1, "pledge");
2549 77f619a8 2023-01-04 stsp * Ensure that AF_UNIX bind(2) cannot be used with any other
2550 77f619a8 2023-01-04 stsp * sockets by revoking all filesystem access via unveil(2).
2552 77f619a8 2023-01-04 stsp apply_unveil_none();
2554 40b85cca 2023-01-03 stsp listen_main(title, fd, gotd.connection_limits,
2555 40b85cca 2023-01-03 stsp gotd.nconnection_limits);
2556 d93ecf7d 2022-12-14 stsp /* NOTREACHED */
2558 5e25db14 2022-12-29 stsp case PROC_AUTH:
2559 5e25db14 2022-12-29 stsp #ifndef PROFILE
2560 44587340 2022-12-30 stsp if (pledge("stdio getpw recvfd unix unveil", NULL) == -1)
2561 5e25db14 2022-12-29 stsp err(1, "pledge");
2564 44587340 2022-12-30 stsp * We need the "unix" pledge promise for getpeername(2) only.
2565 44587340 2022-12-30 stsp * Ensure that AF_UNIX bind(2) cannot be used by revoking all
2566 44587340 2022-12-30 stsp * filesystem access via unveil(2). Access to password database
2567 44587340 2022-12-30 stsp * files will still work since "getpw" bypasses unveil(2).
2569 44587340 2022-12-30 stsp apply_unveil_none();
2571 5e25db14 2022-12-29 stsp auth_main(title, &gotd.repos, repo_path);
2572 5e25db14 2022-12-29 stsp /* NOTREACHED */
2574 13b2bc37 2022-10-23 stsp case PROC_REPO_READ:
2575 13b2bc37 2022-10-23 stsp #ifndef PROFILE
2576 eec68231 2022-12-14 stsp if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2577 13b2bc37 2022-10-23 stsp err(1, "pledge");
2579 eec68231 2022-12-14 stsp apply_unveil_repo_readonly(repo_path);
2580 eec68231 2022-12-14 stsp repo_read_main(title, repo_path, pack_fds, temp_fds);
2581 13b2bc37 2022-10-23 stsp /* NOTREACHED */
2583 13b2bc37 2022-10-23 stsp case PROC_REPO_WRITE:
2584 13b2bc37 2022-10-23 stsp #ifndef PROFILE
2585 eec68231 2022-12-14 stsp if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2586 13b2bc37 2022-10-23 stsp err(1, "pledge");
2588 eec68231 2022-12-14 stsp apply_unveil_repo_readonly(repo_path);
2589 eec68231 2022-12-14 stsp repo_write_main(title, repo_path, pack_fds, temp_fds);
2590 13b2bc37 2022-10-23 stsp /* NOTREACHED */
2593 13b2bc37 2022-10-23 stsp fatal("invalid process id %d", proc_id);
2596 13b2bc37 2022-10-23 stsp if (proc_id != PROC_GOTD)
2597 13b2bc37 2022-10-23 stsp fatal("invalid process id %d", proc_id);
2599 13b2bc37 2022-10-23 stsp apply_unveil();
2601 13b2bc37 2022-10-23 stsp signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
2602 13b2bc37 2022-10-23 stsp signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
2603 13b2bc37 2022-10-23 stsp signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
2604 13b2bc37 2022-10-23 stsp signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
2605 13b2bc37 2022-10-23 stsp signal(SIGPIPE, SIG_IGN);
2607 13b2bc37 2022-10-23 stsp signal_add(&evsigint, NULL);
2608 13b2bc37 2022-10-23 stsp signal_add(&evsigterm, NULL);
2609 13b2bc37 2022-10-23 stsp signal_add(&evsighup, NULL);
2610 13b2bc37 2022-10-23 stsp signal_add(&evsigusr1, NULL);
2612 b50a2b46 2022-12-29 stsp gotd_imsg_event_add(&gotd.listen_proc.iev);
2614 13b2bc37 2022-10-23 stsp event_dispatch();
2616 13b2bc37 2022-10-23 stsp if (pack_fds)
2617 13b2bc37 2022-10-23 stsp got_repo_pack_fds_close(pack_fds);
2618 13b2bc37 2022-10-23 stsp free(repo_path);