2 3efd8e31 2022-10-23 thomas * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
4 3efd8e31 2022-10-23 thomas * Permission to use, copy, modify, and distribute this software for any
5 3efd8e31 2022-10-23 thomas * purpose with or without fee is hereby granted, provided that the above
6 3efd8e31 2022-10-23 thomas * copyright notice and this permission notice appear in all copies.
8 3efd8e31 2022-10-23 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 3efd8e31 2022-10-23 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 3efd8e31 2022-10-23 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 3efd8e31 2022-10-23 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 3efd8e31 2022-10-23 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 3efd8e31 2022-10-23 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 3efd8e31 2022-10-23 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 3efd8e31 2022-10-23 thomas #include <sys/queue.h>
18 3efd8e31 2022-10-23 thomas #include <sys/tree.h>
19 3efd8e31 2022-10-23 thomas #include <sys/time.h>
20 3efd8e31 2022-10-23 thomas #include <sys/types.h>
21 3efd8e31 2022-10-23 thomas #include <sys/stat.h>
22 3efd8e31 2022-10-23 thomas #include <sys/socket.h>
23 3efd8e31 2022-10-23 thomas #include <sys/un.h>
24 3efd8e31 2022-10-23 thomas #include <sys/wait.h>
26 3efd8e31 2022-10-23 thomas #include <fcntl.h>
27 3efd8e31 2022-10-23 thomas #include <err.h>
28 3efd8e31 2022-10-23 thomas #include <errno.h>
29 3efd8e31 2022-10-23 thomas #include <event.h>
30 3efd8e31 2022-10-23 thomas #include <limits.h>
31 3efd8e31 2022-10-23 thomas #include <pwd.h>
32 3efd8e31 2022-10-23 thomas #include <imsg.h>
33 3efd8e31 2022-10-23 thomas #include <sha1.h>
34 3efd8e31 2022-10-23 thomas #include <signal.h>
35 3efd8e31 2022-10-23 thomas #include <siphash.h>
36 3efd8e31 2022-10-23 thomas #include <stdarg.h>
37 3efd8e31 2022-10-23 thomas #include <stdio.h>
38 3efd8e31 2022-10-23 thomas #include <stdlib.h>
39 3efd8e31 2022-10-23 thomas #include <string.h>
40 3efd8e31 2022-10-23 thomas #include <syslog.h>
41 3efd8e31 2022-10-23 thomas #include <unistd.h>
43 3efd8e31 2022-10-23 thomas #include "got_error.h"
44 3efd8e31 2022-10-23 thomas #include "got_opentemp.h"
45 3efd8e31 2022-10-23 thomas #include "got_path.h"
46 3efd8e31 2022-10-23 thomas #include "got_repository.h"
47 3efd8e31 2022-10-23 thomas #include "got_object.h"
48 3efd8e31 2022-10-23 thomas #include "got_reference.h"
50 3efd8e31 2022-10-23 thomas #include "got_lib_delta.h"
51 3efd8e31 2022-10-23 thomas #include "got_lib_object.h"
52 3efd8e31 2022-10-23 thomas #include "got_lib_object_cache.h"
53 3efd8e31 2022-10-23 thomas #include "got_lib_sha1.h"
54 3efd8e31 2022-10-23 thomas #include "got_lib_gitproto.h"
55 3efd8e31 2022-10-23 thomas #include "got_lib_pack.h"
56 3efd8e31 2022-10-23 thomas #include "got_lib_repository.h"
58 3efd8e31 2022-10-23 thomas #include "gotd.h"
59 3efd8e31 2022-10-23 thomas #include "log.h"
60 2b3d32a1 2022-12-30 thomas #include "listen.h"
61 729a7e24 2022-11-17 thomas #include "auth.h"
62 3efd8e31 2022-10-23 thomas #include "repo_read.h"
63 3efd8e31 2022-10-23 thomas #include "repo_write.h"
65 3efd8e31 2022-10-23 thomas #ifndef nitems
66 3efd8e31 2022-10-23 thomas #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
69 3efd8e31 2022-10-23 thomas struct gotd_client {
70 3efd8e31 2022-10-23 thomas STAILQ_ENTRY(gotd_client) entry;
71 3efd8e31 2022-10-23 thomas enum gotd_client_state state;
72 3efd8e31 2022-10-23 thomas struct gotd_client_capability *capabilities;
73 3efd8e31 2022-10-23 thomas size_t ncapa_alloc;
74 3efd8e31 2022-10-23 thomas size_t ncapabilities;
75 3efd8e31 2022-10-23 thomas uint32_t id;
77 3efd8e31 2022-10-23 thomas int delta_cache_fd;
78 3efd8e31 2022-10-23 thomas struct gotd_imsgev iev;
79 3efd8e31 2022-10-23 thomas struct event tmo;
80 3efd8e31 2022-10-23 thomas uid_t euid;
81 3efd8e31 2022-10-23 thomas gid_t egid;
82 3efd8e31 2022-10-23 thomas struct gotd_child_proc *repo_read;
83 3efd8e31 2022-10-23 thomas struct gotd_child_proc *repo_write;
84 c669c489 2022-12-30 thomas struct gotd_child_proc *auth;
85 c669c489 2022-12-30 thomas int required_auth;
86 3efd8e31 2022-10-23 thomas char *packfile_path;
87 3efd8e31 2022-10-23 thomas char *packidx_path;
88 3efd8e31 2022-10-23 thomas int nref_updates;
90 3efd8e31 2022-10-23 thomas STAILQ_HEAD(gotd_clients, gotd_client);
92 3efd8e31 2022-10-23 thomas static struct gotd_clients gotd_clients[GOTD_CLIENT_TABLE_SIZE];
93 3efd8e31 2022-10-23 thomas static SIPHASH_KEY clients_hash_key;
94 3efd8e31 2022-10-23 thomas volatile int client_cnt;
95 95ef3f8a 2022-12-30 thomas static struct timeval auth_timeout = { 5, 0 };
96 3efd8e31 2022-10-23 thomas static struct gotd gotd;
98 3efd8e31 2022-10-23 thomas void gotd_sighdlr(int sig, short event, void *arg);
99 c902213d 2022-10-29 thomas static void gotd_shutdown(void);
100 85b37c72 2022-12-30 thomas static const struct got_error *start_repo_child(struct gotd_client *,
101 85b37c72 2022-12-30 thomas enum gotd_procid, struct gotd_repo *, char *, const char *, int, int);
102 c669c489 2022-12-30 thomas static const struct got_error *start_auth_child(struct gotd_client *, int,
103 c669c489 2022-12-30 thomas struct gotd_repo *, char *, const char *, int, int);
104 85b37c72 2022-12-30 thomas static void kill_proc(struct gotd_child_proc *, int);
106 3efd8e31 2022-10-23 thomas __dead static void
109 5ac853dc 2022-10-24 thomas fprintf(stderr, "usage: %s [-dv] [-f config-file]\n", getprogname());
113 3efd8e31 2022-10-23 thomas static int
114 3efd8e31 2022-10-23 thomas unix_socket_listen(const char *unix_socket_path, uid_t uid, gid_t gid)
116 3efd8e31 2022-10-23 thomas struct sockaddr_un sun;
117 3efd8e31 2022-10-23 thomas int fd = -1;
118 3efd8e31 2022-10-23 thomas mode_t old_umask, mode;
120 3efd8e31 2022-10-23 thomas fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
121 3efd8e31 2022-10-23 thomas if (fd == -1) {
122 3efd8e31 2022-10-23 thomas log_warn("socket");
123 3efd8e31 2022-10-23 thomas return -1;
126 3efd8e31 2022-10-23 thomas sun.sun_family = AF_UNIX;
127 3efd8e31 2022-10-23 thomas if (strlcpy(sun.sun_path, unix_socket_path,
128 3efd8e31 2022-10-23 thomas sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
129 3efd8e31 2022-10-23 thomas log_warnx("%s: name too long", unix_socket_path);
130 3efd8e31 2022-10-23 thomas close(fd);
131 3efd8e31 2022-10-23 thomas return -1;
134 3efd8e31 2022-10-23 thomas if (unlink(unix_socket_path) == -1) {
135 3efd8e31 2022-10-23 thomas if (errno != ENOENT) {
136 3efd8e31 2022-10-23 thomas log_warn("unlink %s", unix_socket_path);
137 3efd8e31 2022-10-23 thomas close(fd);
138 3efd8e31 2022-10-23 thomas return -1;
142 3efd8e31 2022-10-23 thomas old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
143 f2fc8ce0 2023-01-06 thomas mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
145 3efd8e31 2022-10-23 thomas if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
146 3efd8e31 2022-10-23 thomas log_warn("bind: %s", unix_socket_path);
147 3efd8e31 2022-10-23 thomas close(fd);
148 3efd8e31 2022-10-23 thomas umask(old_umask);
149 3efd8e31 2022-10-23 thomas return -1;
152 3efd8e31 2022-10-23 thomas umask(old_umask);
154 3efd8e31 2022-10-23 thomas if (chmod(unix_socket_path, mode) == -1) {
155 3efd8e31 2022-10-23 thomas log_warn("chmod %o %s", mode, unix_socket_path);
156 3efd8e31 2022-10-23 thomas close(fd);
157 3efd8e31 2022-10-23 thomas unlink(unix_socket_path);
158 3efd8e31 2022-10-23 thomas return -1;
161 3efd8e31 2022-10-23 thomas if (chown(unix_socket_path, uid, gid) == -1) {
162 3efd8e31 2022-10-23 thomas log_warn("chown %s uid=%d gid=%d", unix_socket_path, uid, gid);
163 3efd8e31 2022-10-23 thomas close(fd);
164 3efd8e31 2022-10-23 thomas unlink(unix_socket_path);
165 3efd8e31 2022-10-23 thomas return -1;
168 3efd8e31 2022-10-23 thomas if (listen(fd, GOTD_UNIX_SOCKET_BACKLOG) == -1) {
169 3efd8e31 2022-10-23 thomas log_warn("listen");
170 3efd8e31 2022-10-23 thomas close(fd);
171 3efd8e31 2022-10-23 thomas unlink(unix_socket_path);
172 3efd8e31 2022-10-23 thomas return -1;
175 3efd8e31 2022-10-23 thomas return fd;
178 3efd8e31 2022-10-23 thomas static uint64_t
179 3efd8e31 2022-10-23 thomas client_hash(uint32_t client_id)
181 3efd8e31 2022-10-23 thomas return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
184 3efd8e31 2022-10-23 thomas static void
185 3efd8e31 2022-10-23 thomas add_client(struct gotd_client *client)
187 3efd8e31 2022-10-23 thomas uint64_t slot = client_hash(client->id) % nitems(gotd_clients);
188 3efd8e31 2022-10-23 thomas STAILQ_INSERT_HEAD(&gotd_clients[slot], client, entry);
189 3efd8e31 2022-10-23 thomas client_cnt++;
192 3efd8e31 2022-10-23 thomas static struct gotd_client *
193 3efd8e31 2022-10-23 thomas find_client(uint32_t client_id)
195 3efd8e31 2022-10-23 thomas uint64_t slot;
196 3efd8e31 2022-10-23 thomas struct gotd_client *c;
198 3efd8e31 2022-10-23 thomas slot = client_hash(client_id) % nitems(gotd_clients);
199 3efd8e31 2022-10-23 thomas STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
200 3efd8e31 2022-10-23 thomas if (c->id == client_id)
201 3efd8e31 2022-10-23 thomas return c;
204 3efd8e31 2022-10-23 thomas return NULL;
207 3efd8e31 2022-10-23 thomas static struct gotd_child_proc *
208 3efd8e31 2022-10-23 thomas get_client_proc(struct gotd_client *client)
210 3efd8e31 2022-10-23 thomas if (client->repo_read && client->repo_write) {
211 3efd8e31 2022-10-23 thomas fatalx("uid %d is reading and writing in the same session",
212 3efd8e31 2022-10-23 thomas client->euid);
213 3efd8e31 2022-10-23 thomas /* NOTREACHED */
216 3efd8e31 2022-10-23 thomas if (client->repo_read)
217 3efd8e31 2022-10-23 thomas return client->repo_read;
218 3efd8e31 2022-10-23 thomas else if (client->repo_write)
219 3efd8e31 2022-10-23 thomas return client->repo_write;
221 85b37c72 2022-12-30 thomas return NULL;
224 85b37c72 2022-12-30 thomas static struct gotd_client *
225 85b37c72 2022-12-30 thomas find_client_by_proc_fd(int fd)
227 85b37c72 2022-12-30 thomas uint64_t slot;
229 85b37c72 2022-12-30 thomas for (slot = 0; slot < nitems(gotd_clients); slot++) {
230 85b37c72 2022-12-30 thomas struct gotd_client *c;
232 85b37c72 2022-12-30 thomas STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
233 85b37c72 2022-12-30 thomas struct gotd_child_proc *proc = get_client_proc(c);
234 85b37c72 2022-12-30 thomas if (proc && proc->iev.ibuf.fd == fd)
235 85b37c72 2022-12-30 thomas return c;
236 c669c489 2022-12-30 thomas if (c->auth && c->auth->iev.ibuf.fd == fd)
237 c669c489 2022-12-30 thomas return c;
241 3efd8e31 2022-10-23 thomas return NULL;
244 3efd8e31 2022-10-23 thomas static int
245 3efd8e31 2022-10-23 thomas client_is_reading(struct gotd_client *client)
247 3efd8e31 2022-10-23 thomas return client->repo_read != NULL;
250 3efd8e31 2022-10-23 thomas static int
251 3efd8e31 2022-10-23 thomas client_is_writing(struct gotd_client *client)
253 3efd8e31 2022-10-23 thomas return client->repo_write != NULL;
256 3efd8e31 2022-10-23 thomas static const struct got_error *
257 3efd8e31 2022-10-23 thomas ensure_client_is_reading(struct gotd_client *client)
259 3efd8e31 2022-10-23 thomas if (!client_is_reading(client)) {
260 3efd8e31 2022-10-23 thomas return got_error_fmt(GOT_ERR_BAD_PACKET,
261 3efd8e31 2022-10-23 thomas "uid %d made a read-request but is not reading from "
262 3efd8e31 2022-10-23 thomas "a repository", client->euid);
265 3efd8e31 2022-10-23 thomas return NULL;
268 3efd8e31 2022-10-23 thomas static const struct got_error *
269 3efd8e31 2022-10-23 thomas ensure_client_is_writing(struct gotd_client *client)
271 3efd8e31 2022-10-23 thomas if (!client_is_writing(client)) {
272 3efd8e31 2022-10-23 thomas return got_error_fmt(GOT_ERR_BAD_PACKET,
273 3efd8e31 2022-10-23 thomas "uid %d made a write-request but is not writing to "
274 3efd8e31 2022-10-23 thomas "a repository", client->euid);
277 3efd8e31 2022-10-23 thomas return NULL;
280 3efd8e31 2022-10-23 thomas static const struct got_error *
281 3efd8e31 2022-10-23 thomas ensure_client_is_not_writing(struct gotd_client *client)
283 3efd8e31 2022-10-23 thomas if (client_is_writing(client)) {
284 3efd8e31 2022-10-23 thomas return got_error_fmt(GOT_ERR_BAD_PACKET,
285 3efd8e31 2022-10-23 thomas "uid %d made a read-request but is writing to "
286 3efd8e31 2022-10-23 thomas "a repository", client->euid);
289 3efd8e31 2022-10-23 thomas return NULL;
292 3efd8e31 2022-10-23 thomas static const struct got_error *
293 3efd8e31 2022-10-23 thomas ensure_client_is_not_reading(struct gotd_client *client)
295 3efd8e31 2022-10-23 thomas if (client_is_reading(client)) {
296 3efd8e31 2022-10-23 thomas return got_error_fmt(GOT_ERR_BAD_PACKET,
297 3efd8e31 2022-10-23 thomas "uid %d made a write-request but is reading from "
298 3efd8e31 2022-10-23 thomas "a repository", client->euid);
301 3efd8e31 2022-10-23 thomas return NULL;
304 85b37c72 2022-12-30 thomas static void
305 c669c489 2022-12-30 thomas wait_for_child(pid_t child_pid)
307 85b37c72 2022-12-30 thomas pid_t pid;
308 85b37c72 2022-12-30 thomas int status;
310 c669c489 2022-12-30 thomas log_debug("waiting for child PID %ld to terminate",
311 c669c489 2022-12-30 thomas (long)child_pid);
314 c669c489 2022-12-30 thomas pid = waitpid(child_pid, &status, WNOHANG);
315 85b37c72 2022-12-30 thomas if (pid == -1) {
316 85b37c72 2022-12-30 thomas if (errno != EINTR && errno != ECHILD)
317 85b37c72 2022-12-30 thomas fatal("wait");
318 85b37c72 2022-12-30 thomas } else if (WIFSIGNALED(status)) {
319 85b37c72 2022-12-30 thomas log_warnx("child PID %ld terminated; signal %d",
320 85b37c72 2022-12-30 thomas (long)pid, WTERMSIG(status));
322 85b37c72 2022-12-30 thomas } while (pid != -1 || (pid == -1 && errno == EINTR));
325 3efd8e31 2022-10-23 thomas static void
326 c669c489 2022-12-30 thomas kill_auth_proc(struct gotd_client *client)
328 c669c489 2022-12-30 thomas struct gotd_child_proc *proc;
330 c669c489 2022-12-30 thomas if (client->auth == NULL)
333 c669c489 2022-12-30 thomas proc = client->auth;
334 c669c489 2022-12-30 thomas client->auth = NULL;
336 c669c489 2022-12-30 thomas event_del(&proc->iev.ev);
337 c669c489 2022-12-30 thomas msgbuf_clear(&proc->iev.ibuf.w);
338 c669c489 2022-12-30 thomas close(proc->iev.ibuf.fd);
339 c669c489 2022-12-30 thomas kill_proc(proc, 0);
340 c669c489 2022-12-30 thomas wait_for_child(proc->pid);
341 c669c489 2022-12-30 thomas free(proc);
344 c669c489 2022-12-30 thomas static void
345 3efd8e31 2022-10-23 thomas disconnect(struct gotd_client *client)
347 3efd8e31 2022-10-23 thomas struct gotd_imsg_disconnect idisconnect;
348 3efd8e31 2022-10-23 thomas struct gotd_child_proc *proc = get_client_proc(client);
349 85b37c72 2022-12-30 thomas struct gotd_child_proc *listen_proc = &gotd.listen_proc;
350 3efd8e31 2022-10-23 thomas uint64_t slot;
352 3efd8e31 2022-10-23 thomas log_debug("uid %d: disconnecting", client->euid);
354 c669c489 2022-12-30 thomas kill_auth_proc(client);
356 3efd8e31 2022-10-23 thomas idisconnect.client_id = client->id;
357 c902213d 2022-10-29 thomas if (proc) {
358 c902213d 2022-10-29 thomas if (gotd_imsg_compose_event(&proc->iev,
359 c902213d 2022-10-29 thomas GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
360 c902213d 2022-10-29 thomas &idisconnect, sizeof(idisconnect)) == -1)
361 c902213d 2022-10-29 thomas log_warn("imsg compose DISCONNECT");
363 85b37c72 2022-12-30 thomas msgbuf_clear(&proc->iev.ibuf.w);
364 85b37c72 2022-12-30 thomas close(proc->iev.ibuf.fd);
365 85b37c72 2022-12-30 thomas kill_proc(proc, 0);
366 c669c489 2022-12-30 thomas wait_for_child(proc->pid);
367 85b37c72 2022-12-30 thomas free(proc);
368 85b37c72 2022-12-30 thomas proc = NULL;
371 2b3d32a1 2022-12-30 thomas if (gotd_imsg_compose_event(&listen_proc->iev,
372 2b3d32a1 2022-12-30 thomas GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
373 2b3d32a1 2022-12-30 thomas &idisconnect, sizeof(idisconnect)) == -1)
374 2b3d32a1 2022-12-30 thomas log_warn("imsg compose DISCONNECT");
376 3efd8e31 2022-10-23 thomas slot = client_hash(client->id) % nitems(gotd_clients);
377 3efd8e31 2022-10-23 thomas STAILQ_REMOVE(&gotd_clients[slot], client, gotd_client, entry);
378 3efd8e31 2022-10-23 thomas imsg_clear(&client->iev.ibuf);
379 3efd8e31 2022-10-23 thomas event_del(&client->iev.ev);
380 3efd8e31 2022-10-23 thomas evtimer_del(&client->tmo);
381 3efd8e31 2022-10-23 thomas close(client->fd);
382 3efd8e31 2022-10-23 thomas if (client->delta_cache_fd != -1)
383 3efd8e31 2022-10-23 thomas close(client->delta_cache_fd);
384 3efd8e31 2022-10-23 thomas if (client->packfile_path) {
385 3efd8e31 2022-10-23 thomas if (unlink(client->packfile_path) == -1 && errno != ENOENT)
386 3efd8e31 2022-10-23 thomas log_warn("unlink %s: ", client->packfile_path);
387 3efd8e31 2022-10-23 thomas free(client->packfile_path);
389 3efd8e31 2022-10-23 thomas if (client->packidx_path) {
390 3efd8e31 2022-10-23 thomas if (unlink(client->packidx_path) == -1 && errno != ENOENT)
391 3efd8e31 2022-10-23 thomas log_warn("unlink %s: ", client->packidx_path);
392 3efd8e31 2022-10-23 thomas free(client->packidx_path);
394 3efd8e31 2022-10-23 thomas free(client->capabilities);
395 3efd8e31 2022-10-23 thomas free(client);
396 3efd8e31 2022-10-23 thomas client_cnt--;
399 3efd8e31 2022-10-23 thomas static void
400 3efd8e31 2022-10-23 thomas disconnect_on_error(struct gotd_client *client, const struct got_error *err)
402 3efd8e31 2022-10-23 thomas struct imsgbuf ibuf;
404 3efd8e31 2022-10-23 thomas log_warnx("uid %d: %s", client->euid, err->msg);
405 3efd8e31 2022-10-23 thomas if (err->code != GOT_ERR_EOF) {
406 3efd8e31 2022-10-23 thomas imsg_init(&ibuf, client->fd);
407 3efd8e31 2022-10-23 thomas gotd_imsg_send_error(&ibuf, 0, PROC_GOTD, err);
408 3efd8e31 2022-10-23 thomas imsg_clear(&ibuf);
410 3efd8e31 2022-10-23 thomas disconnect(client);
413 c902213d 2022-10-29 thomas static const struct got_error *
414 c902213d 2022-10-29 thomas send_repo_info(struct gotd_imsgev *iev, struct gotd_repo *repo)
416 c902213d 2022-10-29 thomas const struct got_error *err = NULL;
417 c902213d 2022-10-29 thomas struct gotd_imsg_info_repo irepo;
419 c902213d 2022-10-29 thomas memset(&irepo, 0, sizeof(irepo));
421 c902213d 2022-10-29 thomas if (strlcpy(irepo.repo_name, repo->name, sizeof(irepo.repo_name))
422 c902213d 2022-10-29 thomas >= sizeof(irepo.repo_name))
423 c902213d 2022-10-29 thomas return got_error_msg(GOT_ERR_NO_SPACE, "repo name too long");
424 c902213d 2022-10-29 thomas if (strlcpy(irepo.repo_path, repo->path, sizeof(irepo.repo_path))
425 c902213d 2022-10-29 thomas >= sizeof(irepo.repo_path))
426 c902213d 2022-10-29 thomas return got_error_msg(GOT_ERR_NO_SPACE, "repo path too long");
428 c902213d 2022-10-29 thomas if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_REPO, PROC_GOTD, -1,
429 c902213d 2022-10-29 thomas &irepo, sizeof(irepo)) == -1) {
430 c902213d 2022-10-29 thomas err = got_error_from_errno("imsg compose INFO_REPO");
432 c902213d 2022-10-29 thomas return err;
435 c902213d 2022-10-29 thomas return NULL;
438 c902213d 2022-10-29 thomas static const struct got_error *
439 c902213d 2022-10-29 thomas send_capability(struct gotd_client_capability *capa, struct gotd_imsgev* iev)
441 c902213d 2022-10-29 thomas const struct got_error *err = NULL;
442 c902213d 2022-10-29 thomas struct gotd_imsg_capability icapa;
443 c902213d 2022-10-29 thomas size_t len;
444 c902213d 2022-10-29 thomas struct ibuf *wbuf;
446 c902213d 2022-10-29 thomas memset(&icapa, 0, sizeof(icapa));
448 c902213d 2022-10-29 thomas icapa.key_len = strlen(capa->key);
449 c902213d 2022-10-29 thomas len = sizeof(icapa) + icapa.key_len;
450 c902213d 2022-10-29 thomas if (capa->value) {
451 c902213d 2022-10-29 thomas icapa.value_len = strlen(capa->value);
452 c902213d 2022-10-29 thomas len += icapa.value_len;
455 c902213d 2022-10-29 thomas wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_CAPABILITY, 0, 0, len);
456 c902213d 2022-10-29 thomas if (wbuf == NULL) {
457 c902213d 2022-10-29 thomas err = got_error_from_errno("imsg_create CAPABILITY");
458 c902213d 2022-10-29 thomas return err;
461 c902213d 2022-10-29 thomas if (imsg_add(wbuf, &icapa, sizeof(icapa)) == -1)
462 c902213d 2022-10-29 thomas return got_error_from_errno("imsg_add CAPABILITY");
463 c902213d 2022-10-29 thomas if (imsg_add(wbuf, capa->key, icapa.key_len) == -1)
464 c902213d 2022-10-29 thomas return got_error_from_errno("imsg_add CAPABILITY");
465 c902213d 2022-10-29 thomas if (capa->value) {
466 c902213d 2022-10-29 thomas if (imsg_add(wbuf, capa->value, icapa.value_len) == -1)
467 c902213d 2022-10-29 thomas return got_error_from_errno("imsg_add CAPABILITY");
470 c902213d 2022-10-29 thomas wbuf->fd = -1;
471 c902213d 2022-10-29 thomas imsg_close(&iev->ibuf, wbuf);
473 c902213d 2022-10-29 thomas gotd_imsg_event_add(iev);
475 c902213d 2022-10-29 thomas return NULL;
478 c902213d 2022-10-29 thomas static const struct got_error *
479 c902213d 2022-10-29 thomas send_client_info(struct gotd_imsgev *iev, struct gotd_client *client)
481 c902213d 2022-10-29 thomas const struct got_error *err = NULL;
482 c902213d 2022-10-29 thomas struct gotd_imsg_info_client iclient;
483 c902213d 2022-10-29 thomas struct gotd_child_proc *proc;
484 c902213d 2022-10-29 thomas size_t i;
486 c902213d 2022-10-29 thomas memset(&iclient, 0, sizeof(iclient));
487 c902213d 2022-10-29 thomas iclient.euid = client->euid;
488 c902213d 2022-10-29 thomas iclient.egid = client->egid;
490 c902213d 2022-10-29 thomas proc = get_client_proc(client);
491 c902213d 2022-10-29 thomas if (proc) {
492 414e37cb 2022-12-30 thomas if (strlcpy(iclient.repo_name, proc->repo_path,
493 c902213d 2022-10-29 thomas sizeof(iclient.repo_name)) >= sizeof(iclient.repo_name)) {
494 c902213d 2022-10-29 thomas return got_error_msg(GOT_ERR_NO_SPACE,
495 c902213d 2022-10-29 thomas "repo name too long");
497 c902213d 2022-10-29 thomas if (client_is_writing(client))
498 c902213d 2022-10-29 thomas iclient.is_writing = 1;
501 c902213d 2022-10-29 thomas iclient.state = client->state;
502 c902213d 2022-10-29 thomas iclient.ncapabilities = client->ncapabilities;
504 c902213d 2022-10-29 thomas if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_CLIENT, PROC_GOTD, -1,
505 c902213d 2022-10-29 thomas &iclient, sizeof(iclient)) == -1) {
506 c902213d 2022-10-29 thomas err = got_error_from_errno("imsg compose INFO_CLIENT");
508 c902213d 2022-10-29 thomas return err;
511 c902213d 2022-10-29 thomas for (i = 0; i < client->ncapabilities; i++) {
512 c902213d 2022-10-29 thomas struct gotd_client_capability *capa;
513 c902213d 2022-10-29 thomas capa = &client->capabilities[i];
514 c902213d 2022-10-29 thomas err = send_capability(capa, iev);
516 c902213d 2022-10-29 thomas return err;
519 c902213d 2022-10-29 thomas return NULL;
522 c902213d 2022-10-29 thomas static const struct got_error *
523 c902213d 2022-10-29 thomas send_info(struct gotd_client *client)
525 c902213d 2022-10-29 thomas const struct got_error *err = NULL;
526 c902213d 2022-10-29 thomas struct gotd_imsg_info info;
527 c902213d 2022-10-29 thomas uint64_t slot;
528 c902213d 2022-10-29 thomas struct gotd_repo *repo;
530 c8cf6821 2023-01-06 thomas if (client->euid != 0)
531 c8cf6821 2023-01-06 thomas return got_error_set_errno(EPERM, "info");
533 c902213d 2022-10-29 thomas info.pid = gotd.pid;
534 c902213d 2022-10-29 thomas info.verbosity = gotd.verbosity;
535 c902213d 2022-10-29 thomas info.nrepos = gotd.nrepos;
536 c902213d 2022-10-29 thomas info.nclients = client_cnt - 1;
538 c902213d 2022-10-29 thomas if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_INFO, PROC_GOTD, -1,
539 c902213d 2022-10-29 thomas &info, sizeof(info)) == -1) {
540 c902213d 2022-10-29 thomas err = got_error_from_errno("imsg compose INFO");
542 c902213d 2022-10-29 thomas return err;
545 c902213d 2022-10-29 thomas TAILQ_FOREACH(repo, &gotd.repos, entry) {
546 c902213d 2022-10-29 thomas err = send_repo_info(&client->iev, repo);
548 c902213d 2022-10-29 thomas return err;
551 c902213d 2022-10-29 thomas for (slot = 0; slot < nitems(gotd_clients); slot++) {
552 c902213d 2022-10-29 thomas struct gotd_client *c;
553 c902213d 2022-10-29 thomas STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
554 c902213d 2022-10-29 thomas if (c->id == client->id)
555 c902213d 2022-10-29 thomas continue;
556 c902213d 2022-10-29 thomas err = send_client_info(&client->iev, c);
558 c902213d 2022-10-29 thomas return err;
562 c902213d 2022-10-29 thomas return NULL;
565 c902213d 2022-10-29 thomas static const struct got_error *
566 c902213d 2022-10-29 thomas stop_gotd(struct gotd_client *client)
569 c902213d 2022-10-29 thomas if (client->euid != 0)
570 c902213d 2022-10-29 thomas return got_error_set_errno(EPERM, "stop");
572 c902213d 2022-10-29 thomas gotd_shutdown();
573 c902213d 2022-10-29 thomas /* NOTREACHED */
574 729a7e24 2022-11-17 thomas return NULL;
577 729a7e24 2022-11-17 thomas static struct gotd_repo *
578 729a7e24 2022-11-17 thomas find_repo_by_name(const char *repo_name)
580 729a7e24 2022-11-17 thomas struct gotd_repo *repo;
581 729a7e24 2022-11-17 thomas size_t namelen;
583 729a7e24 2022-11-17 thomas TAILQ_FOREACH(repo, &gotd.repos, entry) {
584 729a7e24 2022-11-17 thomas namelen = strlen(repo->name);
585 729a7e24 2022-11-17 thomas if (strncmp(repo->name, repo_name, namelen) != 0)
586 729a7e24 2022-11-17 thomas continue;
587 729a7e24 2022-11-17 thomas if (repo_name[namelen] == '\0' ||
588 729a7e24 2022-11-17 thomas strcmp(&repo_name[namelen], ".git") == 0)
589 729a7e24 2022-11-17 thomas return repo;
592 3efd8e31 2022-10-23 thomas return NULL;
595 3efd8e31 2022-10-23 thomas static const struct got_error *
596 85b37c72 2022-12-30 thomas start_client_session(struct gotd_client *client, struct imsg *imsg)
598 3efd8e31 2022-10-23 thomas const struct got_error *err;
599 3efd8e31 2022-10-23 thomas struct gotd_imsg_list_refs ireq;
600 729a7e24 2022-11-17 thomas struct gotd_repo *repo = NULL;
601 3efd8e31 2022-10-23 thomas size_t datalen;
603 3efd8e31 2022-10-23 thomas log_debug("list-refs request from uid %d", client->euid);
605 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
606 3efd8e31 2022-10-23 thomas if (datalen != sizeof(ireq))
607 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
609 3efd8e31 2022-10-23 thomas memcpy(&ireq, imsg->data, datalen);
611 3efd8e31 2022-10-23 thomas if (ireq.client_is_reading) {
612 3efd8e31 2022-10-23 thomas err = ensure_client_is_not_writing(client);
614 3efd8e31 2022-10-23 thomas return err;
615 729a7e24 2022-11-17 thomas repo = find_repo_by_name(ireq.repo_name);
616 729a7e24 2022-11-17 thomas if (repo == NULL)
617 729a7e24 2022-11-17 thomas return got_error(GOT_ERR_NOT_GIT_REPO);
618 c669c489 2022-12-30 thomas err = start_auth_child(client, GOTD_AUTH_READ, repo,
619 85b37c72 2022-12-30 thomas gotd.argv0, gotd.confpath, gotd.daemonize,
620 85b37c72 2022-12-30 thomas gotd.verbosity);
622 85b37c72 2022-12-30 thomas return err;
624 3efd8e31 2022-10-23 thomas err = ensure_client_is_not_reading(client);
626 729a7e24 2022-11-17 thomas return err;
627 729a7e24 2022-11-17 thomas repo = find_repo_by_name(ireq.repo_name);
628 729a7e24 2022-11-17 thomas if (repo == NULL)
629 729a7e24 2022-11-17 thomas return got_error(GOT_ERR_NOT_GIT_REPO);
630 c669c489 2022-12-30 thomas err = start_auth_child(client,
631 c669c489 2022-12-30 thomas GOTD_AUTH_READ | GOTD_AUTH_WRITE,
632 c669c489 2022-12-30 thomas repo, gotd.argv0, gotd.confpath, gotd.daemonize,
633 85b37c72 2022-12-30 thomas gotd.verbosity);
635 85b37c72 2022-12-30 thomas return err;
638 c669c489 2022-12-30 thomas /* Flow continues upon authentication successs/failure or timeout. */
639 3efd8e31 2022-10-23 thomas return NULL;
642 3efd8e31 2022-10-23 thomas static const struct got_error *
643 3efd8e31 2022-10-23 thomas forward_want(struct gotd_client *client, struct imsg *imsg)
645 3efd8e31 2022-10-23 thomas struct gotd_imsg_want ireq;
646 3efd8e31 2022-10-23 thomas struct gotd_imsg_want iwant;
647 3efd8e31 2022-10-23 thomas size_t datalen;
649 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
650 3efd8e31 2022-10-23 thomas if (datalen != sizeof(ireq))
651 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
653 3efd8e31 2022-10-23 thomas memcpy(&ireq, imsg->data, datalen);
655 3efd8e31 2022-10-23 thomas memset(&iwant, 0, sizeof(iwant));
656 3efd8e31 2022-10-23 thomas memcpy(iwant.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
657 3efd8e31 2022-10-23 thomas iwant.client_id = client->id;
659 3efd8e31 2022-10-23 thomas if (gotd_imsg_compose_event(&client->repo_read->iev, GOTD_IMSG_WANT,
660 3efd8e31 2022-10-23 thomas PROC_GOTD, -1, &iwant, sizeof(iwant)) == -1)
661 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg compose WANT");
663 3efd8e31 2022-10-23 thomas return NULL;
666 3efd8e31 2022-10-23 thomas static const struct got_error *
667 3efd8e31 2022-10-23 thomas forward_ref_update(struct gotd_client *client, struct imsg *imsg)
669 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
670 3efd8e31 2022-10-23 thomas struct gotd_imsg_ref_update ireq;
671 3efd8e31 2022-10-23 thomas struct gotd_imsg_ref_update *iref = NULL;
672 3efd8e31 2022-10-23 thomas size_t datalen;
674 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
675 3efd8e31 2022-10-23 thomas if (datalen < sizeof(ireq))
676 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
677 3efd8e31 2022-10-23 thomas memcpy(&ireq, imsg->data, sizeof(ireq));
678 3efd8e31 2022-10-23 thomas if (datalen != sizeof(ireq) + ireq.name_len)
679 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
681 3efd8e31 2022-10-23 thomas iref = malloc(datalen);
682 3efd8e31 2022-10-23 thomas if (iref == NULL)
683 3efd8e31 2022-10-23 thomas return got_error_from_errno("malloc");
684 3efd8e31 2022-10-23 thomas memcpy(iref, imsg->data, datalen);
686 3efd8e31 2022-10-23 thomas iref->client_id = client->id;
687 3efd8e31 2022-10-23 thomas if (gotd_imsg_compose_event(&client->repo_write->iev,
688 3efd8e31 2022-10-23 thomas GOTD_IMSG_REF_UPDATE, PROC_GOTD, -1, iref, datalen) == -1)
689 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg compose REF_UPDATE");
690 3efd8e31 2022-10-23 thomas free(iref);
691 3efd8e31 2022-10-23 thomas return err;
694 3efd8e31 2022-10-23 thomas static const struct got_error *
695 3efd8e31 2022-10-23 thomas forward_have(struct gotd_client *client, struct imsg *imsg)
697 3efd8e31 2022-10-23 thomas struct gotd_imsg_have ireq;
698 3efd8e31 2022-10-23 thomas struct gotd_imsg_have ihave;
699 3efd8e31 2022-10-23 thomas size_t datalen;
701 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
702 3efd8e31 2022-10-23 thomas if (datalen != sizeof(ireq))
703 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
705 3efd8e31 2022-10-23 thomas memcpy(&ireq, imsg->data, datalen);
707 3efd8e31 2022-10-23 thomas memset(&ihave, 0, sizeof(ihave));
708 3efd8e31 2022-10-23 thomas memcpy(ihave.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
709 3efd8e31 2022-10-23 thomas ihave.client_id = client->id;
711 3efd8e31 2022-10-23 thomas if (gotd_imsg_compose_event(&client->repo_read->iev, GOTD_IMSG_HAVE,
712 3efd8e31 2022-10-23 thomas PROC_GOTD, -1, &ihave, sizeof(ihave)) == -1)
713 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg compose HAVE");
715 3efd8e31 2022-10-23 thomas return NULL;
718 3efd8e31 2022-10-23 thomas static int
719 3efd8e31 2022-10-23 thomas client_has_capability(struct gotd_client *client, const char *capastr)
721 3efd8e31 2022-10-23 thomas struct gotd_client_capability *capa;
722 3efd8e31 2022-10-23 thomas size_t i;
724 3efd8e31 2022-10-23 thomas if (client->ncapabilities == 0)
725 3efd8e31 2022-10-23 thomas return 0;
727 3efd8e31 2022-10-23 thomas for (i = 0; i < client->ncapabilities; i++) {
728 3efd8e31 2022-10-23 thomas capa = &client->capabilities[i];
729 3efd8e31 2022-10-23 thomas if (strcmp(capa->key, capastr) == 0)
730 3efd8e31 2022-10-23 thomas return 1;
733 3efd8e31 2022-10-23 thomas return 0;
736 3efd8e31 2022-10-23 thomas static const struct got_error *
737 3efd8e31 2022-10-23 thomas recv_capabilities(struct gotd_client *client, struct imsg *imsg)
739 3efd8e31 2022-10-23 thomas struct gotd_imsg_capabilities icapas;
740 3efd8e31 2022-10-23 thomas size_t datalen;
742 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
743 3efd8e31 2022-10-23 thomas if (datalen != sizeof(icapas))
744 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
745 3efd8e31 2022-10-23 thomas memcpy(&icapas, imsg->data, sizeof(icapas));
747 3efd8e31 2022-10-23 thomas client->ncapa_alloc = icapas.ncapabilities;
748 3efd8e31 2022-10-23 thomas client->capabilities = calloc(client->ncapa_alloc,
749 3efd8e31 2022-10-23 thomas sizeof(*client->capabilities));
750 3efd8e31 2022-10-23 thomas if (client->capabilities == NULL) {
751 3efd8e31 2022-10-23 thomas client->ncapa_alloc = 0;
752 3efd8e31 2022-10-23 thomas return got_error_from_errno("calloc");
755 3efd8e31 2022-10-23 thomas log_debug("expecting %zu capabilities from uid %d",
756 3efd8e31 2022-10-23 thomas client->ncapa_alloc, client->euid);
757 3efd8e31 2022-10-23 thomas return NULL;
760 3efd8e31 2022-10-23 thomas static const struct got_error *
761 3efd8e31 2022-10-23 thomas recv_capability(struct gotd_client *client, struct imsg *imsg)
763 3efd8e31 2022-10-23 thomas struct gotd_imsg_capability icapa;
764 3efd8e31 2022-10-23 thomas struct gotd_client_capability *capa;
765 3efd8e31 2022-10-23 thomas size_t datalen;
766 3efd8e31 2022-10-23 thomas char *key, *value = NULL;
768 3efd8e31 2022-10-23 thomas if (client->capabilities == NULL ||
769 3efd8e31 2022-10-23 thomas client->ncapabilities >= client->ncapa_alloc) {
770 3efd8e31 2022-10-23 thomas return got_error_msg(GOT_ERR_BAD_REQUEST,
771 3efd8e31 2022-10-23 thomas "unexpected capability received");
774 3efd8e31 2022-10-23 thomas memset(&icapa, 0, sizeof(icapa));
776 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
777 3efd8e31 2022-10-23 thomas if (datalen < sizeof(icapa))
778 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
779 3efd8e31 2022-10-23 thomas memcpy(&icapa, imsg->data, sizeof(icapa));
781 3efd8e31 2022-10-23 thomas if (datalen != sizeof(icapa) + icapa.key_len + icapa.value_len)
782 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
784 3efd8e31 2022-10-23 thomas key = malloc(icapa.key_len + 1);
785 3efd8e31 2022-10-23 thomas if (key == NULL)
786 3efd8e31 2022-10-23 thomas return got_error_from_errno("malloc");
787 3efd8e31 2022-10-23 thomas if (icapa.value_len > 0) {
788 3efd8e31 2022-10-23 thomas value = malloc(icapa.value_len + 1);
789 3efd8e31 2022-10-23 thomas if (value == NULL) {
790 3efd8e31 2022-10-23 thomas free(key);
791 3efd8e31 2022-10-23 thomas return got_error_from_errno("malloc");
795 3efd8e31 2022-10-23 thomas memcpy(key, imsg->data + sizeof(icapa), icapa.key_len);
796 3efd8e31 2022-10-23 thomas key[icapa.key_len] = '\0';
797 3efd8e31 2022-10-23 thomas if (value) {
798 3efd8e31 2022-10-23 thomas memcpy(value, imsg->data + sizeof(icapa) + icapa.key_len,
799 3efd8e31 2022-10-23 thomas icapa.value_len);
800 3efd8e31 2022-10-23 thomas value[icapa.value_len] = '\0';
803 3efd8e31 2022-10-23 thomas capa = &client->capabilities[client->ncapabilities++];
804 3efd8e31 2022-10-23 thomas capa->key = key;
805 3efd8e31 2022-10-23 thomas capa->value = value;
807 3efd8e31 2022-10-23 thomas if (value)
808 3efd8e31 2022-10-23 thomas log_debug("uid %d: capability %s=%s", client->euid, key, value);
810 3efd8e31 2022-10-23 thomas log_debug("uid %d: capability %s", client->euid, key);
812 3efd8e31 2022-10-23 thomas return NULL;
815 3efd8e31 2022-10-23 thomas static const struct got_error *
816 3efd8e31 2022-10-23 thomas send_packfile(struct gotd_client *client)
818 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
819 3efd8e31 2022-10-23 thomas struct gotd_imsg_send_packfile ipack;
820 3efd8e31 2022-10-23 thomas struct gotd_imsg_packfile_pipe ipipe;
821 3efd8e31 2022-10-23 thomas int pipe[2];
823 3efd8e31 2022-10-23 thomas if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
824 3efd8e31 2022-10-23 thomas return got_error_from_errno("socketpair");
826 3efd8e31 2022-10-23 thomas memset(&ipack, 0, sizeof(ipack));
827 3efd8e31 2022-10-23 thomas memset(&ipipe, 0, sizeof(ipipe));
829 3efd8e31 2022-10-23 thomas ipack.client_id = client->id;
830 3efd8e31 2022-10-23 thomas if (client_has_capability(client, GOT_CAPA_SIDE_BAND_64K))
831 3efd8e31 2022-10-23 thomas ipack.report_progress = 1;
833 3efd8e31 2022-10-23 thomas client->delta_cache_fd = got_opentempfd();
834 3efd8e31 2022-10-23 thomas if (client->delta_cache_fd == -1)
835 3efd8e31 2022-10-23 thomas return got_error_from_errno("got_opentempfd");
837 3efd8e31 2022-10-23 thomas if (gotd_imsg_compose_event(&client->repo_read->iev,
838 3efd8e31 2022-10-23 thomas GOTD_IMSG_SEND_PACKFILE, PROC_GOTD, client->delta_cache_fd,
839 3efd8e31 2022-10-23 thomas &ipack, sizeof(ipack)) == -1) {
840 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg compose SEND_PACKFILE");
841 3efd8e31 2022-10-23 thomas close(pipe[0]);
842 3efd8e31 2022-10-23 thomas close(pipe[1]);
843 3efd8e31 2022-10-23 thomas return err;
846 3efd8e31 2022-10-23 thomas ipipe.client_id = client->id;
848 cc4cc677 2022-10-28 thomas /* Send pack pipe end 0 to repo_read. */
849 3efd8e31 2022-10-23 thomas if (gotd_imsg_compose_event(&client->repo_read->iev,
850 3efd8e31 2022-10-23 thomas GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[0],
851 3efd8e31 2022-10-23 thomas &ipipe, sizeof(ipipe)) == -1) {
852 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg compose PACKFILE_PIPE");
853 3efd8e31 2022-10-23 thomas close(pipe[1]);
854 3efd8e31 2022-10-23 thomas return err;
857 cc4cc677 2022-10-28 thomas /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
858 cc4cc677 2022-10-28 thomas if (gotd_imsg_compose_event(&client->iev,
859 cc4cc677 2022-10-28 thomas GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1], NULL, 0) == -1)
860 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg compose PACKFILE_PIPE");
862 3efd8e31 2022-10-23 thomas return err;
865 3efd8e31 2022-10-23 thomas static const struct got_error *
866 3efd8e31 2022-10-23 thomas recv_packfile(struct gotd_client *client)
868 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
869 3efd8e31 2022-10-23 thomas struct gotd_imsg_recv_packfile ipack;
870 3efd8e31 2022-10-23 thomas struct gotd_imsg_packfile_pipe ipipe;
871 3efd8e31 2022-10-23 thomas struct gotd_imsg_packidx_file ifile;
872 3efd8e31 2022-10-23 thomas char *basepath = NULL, *pack_path = NULL, *idx_path = NULL;
873 3efd8e31 2022-10-23 thomas int packfd = -1, idxfd = -1;
874 3efd8e31 2022-10-23 thomas int pipe[2] = { -1, -1 };
876 3efd8e31 2022-10-23 thomas if (client->packfile_path) {
877 3efd8e31 2022-10-23 thomas return got_error_fmt(GOT_ERR_PRIVSEP_MSG,
878 3efd8e31 2022-10-23 thomas "uid %d already has a pack file", client->euid);
881 3efd8e31 2022-10-23 thomas if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
882 3efd8e31 2022-10-23 thomas return got_error_from_errno("socketpair");
884 3efd8e31 2022-10-23 thomas memset(&ipipe, 0, sizeof(ipipe));
885 3efd8e31 2022-10-23 thomas ipipe.client_id = client->id;
887 3efd8e31 2022-10-23 thomas if (gotd_imsg_compose_event(&client->repo_write->iev,
888 3efd8e31 2022-10-23 thomas GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[0],
889 3efd8e31 2022-10-23 thomas &ipipe, sizeof(ipipe)) == -1) {
890 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg compose PACKFILE_PIPE");
891 3efd8e31 2022-10-23 thomas pipe[0] = -1;
892 3efd8e31 2022-10-23 thomas goto done;
894 3efd8e31 2022-10-23 thomas pipe[0] = -1;
896 3efd8e31 2022-10-23 thomas if (gotd_imsg_compose_event(&client->repo_write->iev,
897 3efd8e31 2022-10-23 thomas GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1],
898 3efd8e31 2022-10-23 thomas &ipipe, sizeof(ipipe)) == -1)
899 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg compose PACKFILE_PIPE");
900 3efd8e31 2022-10-23 thomas pipe[1] = -1;
902 3efd8e31 2022-10-23 thomas if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.pack",
903 414e37cb 2022-12-30 thomas client->repo_write->repo_path, GOT_OBJECTS_PACK_DIR,
904 3efd8e31 2022-10-23 thomas client->euid) == -1) {
905 3efd8e31 2022-10-23 thomas err = got_error_from_errno("asprintf");
906 3efd8e31 2022-10-23 thomas goto done;
909 fc2a50f2 2022-11-01 thomas err = got_opentemp_named_fd(&pack_path, &packfd, basepath, "");
911 3efd8e31 2022-10-23 thomas goto done;
913 3efd8e31 2022-10-23 thomas free(basepath);
914 3efd8e31 2022-10-23 thomas if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.idx",
915 414e37cb 2022-12-30 thomas client->repo_write->repo_path, GOT_OBJECTS_PACK_DIR,
916 3efd8e31 2022-10-23 thomas client->euid) == -1) {
917 3efd8e31 2022-10-23 thomas err = got_error_from_errno("asprintf");
918 3efd8e31 2022-10-23 thomas basepath = NULL;
919 3efd8e31 2022-10-23 thomas goto done;
921 fc2a50f2 2022-11-01 thomas err = got_opentemp_named_fd(&idx_path, &idxfd, basepath, "");
923 3efd8e31 2022-10-23 thomas goto done;
925 3efd8e31 2022-10-23 thomas memset(&ifile, 0, sizeof(ifile));
926 3efd8e31 2022-10-23 thomas ifile.client_id = client->id;
927 3efd8e31 2022-10-23 thomas if (gotd_imsg_compose_event(&client->repo_write->iev,
928 3efd8e31 2022-10-23 thomas GOTD_IMSG_PACKIDX_FILE, PROC_GOTD, idxfd,
929 3efd8e31 2022-10-23 thomas &ifile, sizeof(ifile)) == -1) {
930 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg compose PACKIDX_FILE");
931 3efd8e31 2022-10-23 thomas idxfd = -1;
932 3efd8e31 2022-10-23 thomas goto done;
934 3efd8e31 2022-10-23 thomas idxfd = -1;
936 3efd8e31 2022-10-23 thomas memset(&ipack, 0, sizeof(ipack));
937 3efd8e31 2022-10-23 thomas ipack.client_id = client->id;
938 3efd8e31 2022-10-23 thomas if (client_has_capability(client, GOT_CAPA_REPORT_STATUS))
939 3efd8e31 2022-10-23 thomas ipack.report_status = 1;
941 3efd8e31 2022-10-23 thomas if (gotd_imsg_compose_event(&client->repo_write->iev,
942 3efd8e31 2022-10-23 thomas GOTD_IMSG_RECV_PACKFILE, PROC_GOTD, packfd,
943 3efd8e31 2022-10-23 thomas &ipack, sizeof(ipack)) == -1) {
944 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg compose RECV_PACKFILE");
945 3efd8e31 2022-10-23 thomas packfd = -1;
946 3efd8e31 2022-10-23 thomas goto done;
948 3efd8e31 2022-10-23 thomas packfd = -1;
951 3efd8e31 2022-10-23 thomas free(basepath);
952 3efd8e31 2022-10-23 thomas if (pipe[0] != -1 && close(pipe[0]) == -1 && err == NULL)
953 3efd8e31 2022-10-23 thomas err = got_error_from_errno("close");
954 3efd8e31 2022-10-23 thomas if (pipe[1] != -1 && close(pipe[1]) == -1 && err == NULL)
955 3efd8e31 2022-10-23 thomas err = got_error_from_errno("close");
956 3efd8e31 2022-10-23 thomas if (packfd != -1 && close(packfd) == -1 && err == NULL)
957 3efd8e31 2022-10-23 thomas err = got_error_from_errno("close");
958 3efd8e31 2022-10-23 thomas if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
959 3efd8e31 2022-10-23 thomas err = got_error_from_errno("close");
960 3efd8e31 2022-10-23 thomas if (err) {
961 3efd8e31 2022-10-23 thomas free(pack_path);
962 3efd8e31 2022-10-23 thomas free(idx_path);
964 3efd8e31 2022-10-23 thomas client->packfile_path = pack_path;
965 3efd8e31 2022-10-23 thomas client->packidx_path = idx_path;
967 3efd8e31 2022-10-23 thomas return err;
970 3efd8e31 2022-10-23 thomas static void
971 3efd8e31 2022-10-23 thomas gotd_request(int fd, short events, void *arg)
973 3efd8e31 2022-10-23 thomas struct gotd_imsgev *iev = arg;
974 3efd8e31 2022-10-23 thomas struct imsgbuf *ibuf = &iev->ibuf;
975 3efd8e31 2022-10-23 thomas struct gotd_client *client = iev->handler_arg;
976 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
977 3efd8e31 2022-10-23 thomas struct imsg imsg;
978 3efd8e31 2022-10-23 thomas ssize_t n;
980 3efd8e31 2022-10-23 thomas if (events & EV_WRITE) {
981 3efd8e31 2022-10-23 thomas while (ibuf->w.queued) {
982 3efd8e31 2022-10-23 thomas n = msgbuf_write(&ibuf->w);
983 3efd8e31 2022-10-23 thomas if (n == -1 && errno == EPIPE) {
985 3efd8e31 2022-10-23 thomas * The client has closed its socket.
986 3efd8e31 2022-10-23 thomas * This can happen when Git clients are
987 3efd8e31 2022-10-23 thomas * done sending pack file data.
989 3efd8e31 2022-10-23 thomas msgbuf_clear(&ibuf->w);
990 3efd8e31 2022-10-23 thomas continue;
991 3efd8e31 2022-10-23 thomas } else if (n == -1 && errno != EAGAIN) {
992 3efd8e31 2022-10-23 thomas err = got_error_from_errno("imsg_flush");
993 3efd8e31 2022-10-23 thomas disconnect_on_error(client, err);
996 3efd8e31 2022-10-23 thomas if (n == 0) {
997 3efd8e31 2022-10-23 thomas /* Connection closed. */
998 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_EOF);
999 3efd8e31 2022-10-23 thomas disconnect_on_error(client, err);
1004 c902213d 2022-10-29 thomas /* Disconnect gotctl(8) now that messages have been sent. */
1005 c902213d 2022-10-29 thomas if (!client_is_reading(client) && !client_is_writing(client)) {
1006 c902213d 2022-10-29 thomas disconnect(client);
1011 3efd8e31 2022-10-23 thomas if ((events & EV_READ) == 0)
1014 3efd8e31 2022-10-23 thomas memset(&imsg, 0, sizeof(imsg));
1016 3efd8e31 2022-10-23 thomas while (err == NULL) {
1017 3efd8e31 2022-10-23 thomas err = gotd_imsg_recv(&imsg, ibuf, 0);
1018 3efd8e31 2022-10-23 thomas if (err) {
1019 3efd8e31 2022-10-23 thomas if (err->code == GOT_ERR_PRIVSEP_READ)
1020 3efd8e31 2022-10-23 thomas err = NULL;
1024 3efd8e31 2022-10-23 thomas evtimer_del(&client->tmo);
1026 3efd8e31 2022-10-23 thomas switch (imsg.hdr.type) {
1027 c902213d 2022-10-29 thomas case GOTD_IMSG_INFO:
1028 c902213d 2022-10-29 thomas err = send_info(client);
1030 c902213d 2022-10-29 thomas case GOTD_IMSG_STOP:
1031 c902213d 2022-10-29 thomas err = stop_gotd(client);
1033 3efd8e31 2022-10-23 thomas case GOTD_IMSG_LIST_REFS:
1034 3efd8e31 2022-10-23 thomas if (client->state != GOTD_STATE_EXPECT_LIST_REFS) {
1035 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1036 3efd8e31 2022-10-23 thomas "unexpected list-refs request received");
1039 85b37c72 2022-12-30 thomas err = start_client_session(client, &imsg);
1040 3efd8e31 2022-10-23 thomas if (err)
1043 3efd8e31 2022-10-23 thomas case GOTD_IMSG_CAPABILITIES:
1044 3efd8e31 2022-10-23 thomas if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1045 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1046 3efd8e31 2022-10-23 thomas "unexpected capabilities received");
1049 3efd8e31 2022-10-23 thomas log_debug("receiving capabilities from uid %d",
1050 3efd8e31 2022-10-23 thomas client->euid);
1051 3efd8e31 2022-10-23 thomas err = recv_capabilities(client, &imsg);
1053 3efd8e31 2022-10-23 thomas case GOTD_IMSG_CAPABILITY:
1054 3efd8e31 2022-10-23 thomas if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1055 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1056 3efd8e31 2022-10-23 thomas "unexpected capability received");
1059 3efd8e31 2022-10-23 thomas err = recv_capability(client, &imsg);
1060 3efd8e31 2022-10-23 thomas if (err || client->ncapabilities < client->ncapa_alloc)
1062 3efd8e31 2022-10-23 thomas if (client_is_reading(client)) {
1063 3efd8e31 2022-10-23 thomas client->state = GOTD_STATE_EXPECT_WANT;
1064 3efd8e31 2022-10-23 thomas log_debug("uid %d: expecting want-lines",
1065 3efd8e31 2022-10-23 thomas client->euid);
1066 3efd8e31 2022-10-23 thomas } else if (client_is_writing(client)) {
1067 3efd8e31 2022-10-23 thomas client->state = GOTD_STATE_EXPECT_REF_UPDATE;
1068 3efd8e31 2022-10-23 thomas log_debug("uid %d: expecting ref-update-lines",
1069 3efd8e31 2022-10-23 thomas client->euid);
1071 3efd8e31 2022-10-23 thomas fatalx("client %d is both reading and writing",
1072 3efd8e31 2022-10-23 thomas client->euid);
1074 3efd8e31 2022-10-23 thomas case GOTD_IMSG_WANT:
1075 3efd8e31 2022-10-23 thomas if (client->state != GOTD_STATE_EXPECT_WANT) {
1076 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1077 3efd8e31 2022-10-23 thomas "unexpected want-line received");
1080 3efd8e31 2022-10-23 thomas log_debug("received want-line from uid %d",
1081 3efd8e31 2022-10-23 thomas client->euid);
1082 3efd8e31 2022-10-23 thomas err = ensure_client_is_reading(client);
1083 3efd8e31 2022-10-23 thomas if (err)
1085 3efd8e31 2022-10-23 thomas err = forward_want(client, &imsg);
1087 3efd8e31 2022-10-23 thomas case GOTD_IMSG_REF_UPDATE:
1088 3efd8e31 2022-10-23 thomas if (client->state != GOTD_STATE_EXPECT_REF_UPDATE) {
1089 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1090 3efd8e31 2022-10-23 thomas "unexpected ref-update-line received");
1093 3efd8e31 2022-10-23 thomas log_debug("received ref-update-line from uid %d",
1094 3efd8e31 2022-10-23 thomas client->euid);
1095 3efd8e31 2022-10-23 thomas err = ensure_client_is_writing(client);
1096 3efd8e31 2022-10-23 thomas if (err)
1098 3efd8e31 2022-10-23 thomas err = forward_ref_update(client, &imsg);
1099 3efd8e31 2022-10-23 thomas if (err)
1101 3efd8e31 2022-10-23 thomas client->state = GOTD_STATE_EXPECT_MORE_REF_UPDATES;
1103 3efd8e31 2022-10-23 thomas case GOTD_IMSG_HAVE:
1104 3efd8e31 2022-10-23 thomas if (client->state != GOTD_STATE_EXPECT_HAVE) {
1105 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1106 3efd8e31 2022-10-23 thomas "unexpected have-line received");
1109 3efd8e31 2022-10-23 thomas log_debug("received have-line from uid %d",
1110 3efd8e31 2022-10-23 thomas client->euid);
1111 3efd8e31 2022-10-23 thomas err = ensure_client_is_reading(client);
1112 3efd8e31 2022-10-23 thomas if (err)
1114 3efd8e31 2022-10-23 thomas err = forward_have(client, &imsg);
1115 3efd8e31 2022-10-23 thomas if (err)
1118 3efd8e31 2022-10-23 thomas case GOTD_IMSG_FLUSH:
1119 3efd8e31 2022-10-23 thomas if (client->state == GOTD_STATE_EXPECT_WANT ||
1120 3efd8e31 2022-10-23 thomas client->state == GOTD_STATE_EXPECT_HAVE) {
1121 3efd8e31 2022-10-23 thomas err = ensure_client_is_reading(client);
1122 3efd8e31 2022-10-23 thomas if (err)
1124 3efd8e31 2022-10-23 thomas } else if (client->state ==
1125 3efd8e31 2022-10-23 thomas GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1126 3efd8e31 2022-10-23 thomas err = ensure_client_is_writing(client);
1127 3efd8e31 2022-10-23 thomas if (err)
1129 3efd8e31 2022-10-23 thomas } else {
1130 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1131 3efd8e31 2022-10-23 thomas "unexpected flush-pkt received");
1134 3efd8e31 2022-10-23 thomas log_debug("received flush-pkt from uid %d",
1135 3efd8e31 2022-10-23 thomas client->euid);
1136 3efd8e31 2022-10-23 thomas if (client->state == GOTD_STATE_EXPECT_WANT) {
1137 3efd8e31 2022-10-23 thomas client->state = GOTD_STATE_EXPECT_HAVE;
1138 3efd8e31 2022-10-23 thomas log_debug("uid %d: expecting have-lines",
1139 3efd8e31 2022-10-23 thomas client->euid);
1140 3efd8e31 2022-10-23 thomas } else if (client->state == GOTD_STATE_EXPECT_HAVE) {
1141 3efd8e31 2022-10-23 thomas client->state = GOTD_STATE_EXPECT_DONE;
1142 3efd8e31 2022-10-23 thomas log_debug("uid %d: expecting 'done'",
1143 3efd8e31 2022-10-23 thomas client->euid);
1144 3efd8e31 2022-10-23 thomas } else if (client->state ==
1145 3efd8e31 2022-10-23 thomas GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1146 3efd8e31 2022-10-23 thomas client->state = GOTD_STATE_EXPECT_PACKFILE;
1147 3efd8e31 2022-10-23 thomas log_debug("uid %d: expecting packfile",
1148 3efd8e31 2022-10-23 thomas client->euid);
1149 3efd8e31 2022-10-23 thomas err = recv_packfile(client);
1150 3efd8e31 2022-10-23 thomas } else {
1151 3efd8e31 2022-10-23 thomas /* should not happen, see above */
1152 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1153 3efd8e31 2022-10-23 thomas "unexpected client state");
1157 3efd8e31 2022-10-23 thomas case GOTD_IMSG_DONE:
1158 3efd8e31 2022-10-23 thomas if (client->state != GOTD_STATE_EXPECT_HAVE &&
1159 3efd8e31 2022-10-23 thomas client->state != GOTD_STATE_EXPECT_DONE) {
1160 3efd8e31 2022-10-23 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1161 3efd8e31 2022-10-23 thomas "unexpected flush-pkt received");
1164 3efd8e31 2022-10-23 thomas log_debug("received 'done' from uid %d", client->euid);
1165 3efd8e31 2022-10-23 thomas err = ensure_client_is_reading(client);
1166 3efd8e31 2022-10-23 thomas if (err)
1168 3efd8e31 2022-10-23 thomas client->state = GOTD_STATE_DONE;
1169 3efd8e31 2022-10-23 thomas err = send_packfile(client);
1171 3efd8e31 2022-10-23 thomas default:
1172 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
1176 3efd8e31 2022-10-23 thomas imsg_free(&imsg);
1179 3efd8e31 2022-10-23 thomas if (err) {
1180 3efd8e31 2022-10-23 thomas if (err->code != GOT_ERR_EOF ||
1181 3efd8e31 2022-10-23 thomas client->state != GOTD_STATE_EXPECT_PACKFILE)
1182 3efd8e31 2022-10-23 thomas disconnect_on_error(client, err);
1183 3efd8e31 2022-10-23 thomas } else {
1184 3efd8e31 2022-10-23 thomas gotd_imsg_event_add(&client->iev);
1185 95ef3f8a 2022-12-30 thomas if (client->state == GOTD_STATE_EXPECT_LIST_REFS)
1186 95ef3f8a 2022-12-30 thomas evtimer_add(&client->tmo, &auth_timeout);
1188 0781db0e 2023-01-06 thomas evtimer_add(&client->tmo, &gotd.request_timeout);
1192 3efd8e31 2022-10-23 thomas static void
1193 3efd8e31 2022-10-23 thomas gotd_request_timeout(int fd, short events, void *arg)
1195 3efd8e31 2022-10-23 thomas struct gotd_client *client = arg;
1197 3efd8e31 2022-10-23 thomas log_debug("disconnecting uid %d due to timeout", client->euid);
1198 3efd8e31 2022-10-23 thomas disconnect(client);
1201 2b3d32a1 2022-12-30 thomas static const struct got_error *
1202 2b3d32a1 2022-12-30 thomas recv_connect(uint32_t *client_id, struct imsg *imsg)
1204 2b3d32a1 2022-12-30 thomas const struct got_error *err = NULL;
1205 2b3d32a1 2022-12-30 thomas struct gotd_imsg_connect iconnect;
1206 2b3d32a1 2022-12-30 thomas size_t datalen;
1207 3efd8e31 2022-10-23 thomas int s = -1;
1208 3efd8e31 2022-10-23 thomas struct gotd_client *client = NULL;
1210 2b3d32a1 2022-12-30 thomas *client_id = 0;
1212 2b3d32a1 2022-12-30 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1213 2b3d32a1 2022-12-30 thomas if (datalen != sizeof(iconnect))
1214 2b3d32a1 2022-12-30 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1215 2b3d32a1 2022-12-30 thomas memcpy(&iconnect, imsg->data, sizeof(iconnect));
1217 2b3d32a1 2022-12-30 thomas s = imsg->fd;
1218 3efd8e31 2022-10-23 thomas if (s == -1) {
1219 2b3d32a1 2022-12-30 thomas err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1220 2b3d32a1 2022-12-30 thomas goto done;
1223 2b3d32a1 2022-12-30 thomas if (find_client(iconnect.client_id)) {
1224 2b3d32a1 2022-12-30 thomas err = got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
1225 2b3d32a1 2022-12-30 thomas goto done;
1228 3efd8e31 2022-10-23 thomas client = calloc(1, sizeof(*client));
1229 3efd8e31 2022-10-23 thomas if (client == NULL) {
1230 2b3d32a1 2022-12-30 thomas err = got_error_from_errno("calloc");
1231 2b3d32a1 2022-12-30 thomas goto done;
1234 2b3d32a1 2022-12-30 thomas *client_id = iconnect.client_id;
1236 3efd8e31 2022-10-23 thomas client->state = GOTD_STATE_EXPECT_LIST_REFS;
1237 2b3d32a1 2022-12-30 thomas client->id = iconnect.client_id;
1238 3efd8e31 2022-10-23 thomas client->fd = s;
1240 3efd8e31 2022-10-23 thomas client->delta_cache_fd = -1;
1241 0bcde4c8 2022-12-30 thomas /* The auth process will verify UID/GID for us. */
1242 0bcde4c8 2022-12-30 thomas client->euid = iconnect.euid;
1243 0bcde4c8 2022-12-30 thomas client->egid = iconnect.egid;
1244 3efd8e31 2022-10-23 thomas client->nref_updates = -1;
1246 3efd8e31 2022-10-23 thomas imsg_init(&client->iev.ibuf, client->fd);
1247 3efd8e31 2022-10-23 thomas client->iev.handler = gotd_request;
1248 3efd8e31 2022-10-23 thomas client->iev.events = EV_READ;
1249 3efd8e31 2022-10-23 thomas client->iev.handler_arg = client;
1251 3efd8e31 2022-10-23 thomas event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
1252 3efd8e31 2022-10-23 thomas &client->iev);
1253 3efd8e31 2022-10-23 thomas gotd_imsg_event_add(&client->iev);
1255 3efd8e31 2022-10-23 thomas evtimer_set(&client->tmo, gotd_request_timeout, client);
1257 3efd8e31 2022-10-23 thomas add_client(client);
1258 3efd8e31 2022-10-23 thomas log_debug("%s: new client uid %d connected on fd %d", __func__,
1259 3efd8e31 2022-10-23 thomas client->euid, client->fd);
1261 2b3d32a1 2022-12-30 thomas if (err) {
1262 85b37c72 2022-12-30 thomas struct gotd_child_proc *listen_proc = &gotd.listen_proc;
1263 2b3d32a1 2022-12-30 thomas struct gotd_imsg_disconnect idisconnect;
1265 2b3d32a1 2022-12-30 thomas idisconnect.client_id = client->id;
1266 2b3d32a1 2022-12-30 thomas if (gotd_imsg_compose_event(&listen_proc->iev,
1267 2b3d32a1 2022-12-30 thomas GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
1268 2b3d32a1 2022-12-30 thomas &idisconnect, sizeof(idisconnect)) == -1)
1269 2b3d32a1 2022-12-30 thomas log_warn("imsg compose DISCONNECT");
1271 2b3d32a1 2022-12-30 thomas if (s != -1)
1272 2b3d32a1 2022-12-30 thomas close(s);
1275 2b3d32a1 2022-12-30 thomas return err;
1278 3efd8e31 2022-10-23 thomas static const char *gotd_proc_names[PROC_MAX] = {
1279 3efd8e31 2022-10-23 thomas "parent",
1280 2b3d32a1 2022-12-30 thomas "listen",
1282 3efd8e31 2022-10-23 thomas "repo_read",
1283 3efd8e31 2022-10-23 thomas "repo_write"
1286 3efd8e31 2022-10-23 thomas static void
1287 3efd8e31 2022-10-23 thomas kill_proc(struct gotd_child_proc *proc, int fatal)
1289 3efd8e31 2022-10-23 thomas if (fatal) {
1290 3efd8e31 2022-10-23 thomas log_warnx("sending SIGKILL to PID %d", proc->pid);
1291 3efd8e31 2022-10-23 thomas kill(proc->pid, SIGKILL);
1293 3efd8e31 2022-10-23 thomas kill(proc->pid, SIGTERM);
1296 3efd8e31 2022-10-23 thomas static void
1297 3efd8e31 2022-10-23 thomas gotd_shutdown(void)
1299 3efd8e31 2022-10-23 thomas struct gotd_child_proc *proc;
1300 85b37c72 2022-12-30 thomas uint64_t slot;
1302 85b37c72 2022-12-30 thomas for (slot = 0; slot < nitems(gotd_clients); slot++) {
1303 85b37c72 2022-12-30 thomas struct gotd_client *c, *tmp;
1305 85b37c72 2022-12-30 thomas STAILQ_FOREACH_SAFE(c, &gotd_clients[slot], entry, tmp)
1306 85b37c72 2022-12-30 thomas disconnect(c);
1309 85b37c72 2022-12-30 thomas proc = &gotd.listen_proc;
1310 85b37c72 2022-12-30 thomas msgbuf_clear(&proc->iev.ibuf.w);
1311 85b37c72 2022-12-30 thomas close(proc->iev.ibuf.fd);
1312 85b37c72 2022-12-30 thomas kill_proc(proc, 0);
1313 c669c489 2022-12-30 thomas wait_for_child(proc->pid);
1315 3efd8e31 2022-10-23 thomas log_info("terminating");
1316 3efd8e31 2022-10-23 thomas exit(0);
1320 3efd8e31 2022-10-23 thomas gotd_sighdlr(int sig, short event, void *arg)
1323 3efd8e31 2022-10-23 thomas * Normal signal handler rules don't apply because libevent
1324 3efd8e31 2022-10-23 thomas * decouples for us.
1327 3efd8e31 2022-10-23 thomas switch (sig) {
1328 3efd8e31 2022-10-23 thomas case SIGHUP:
1329 3efd8e31 2022-10-23 thomas log_info("%s: ignoring SIGHUP", __func__);
1331 3efd8e31 2022-10-23 thomas case SIGUSR1:
1332 3efd8e31 2022-10-23 thomas log_info("%s: ignoring SIGUSR1", __func__);
1334 3efd8e31 2022-10-23 thomas case SIGTERM:
1335 3efd8e31 2022-10-23 thomas case SIGINT:
1336 3efd8e31 2022-10-23 thomas gotd_shutdown();
1338 3efd8e31 2022-10-23 thomas default:
1339 3efd8e31 2022-10-23 thomas fatalx("unexpected signal");
1343 3efd8e31 2022-10-23 thomas static const struct got_error *
1344 3efd8e31 2022-10-23 thomas ensure_proc_is_reading(struct gotd_client *client,
1345 3efd8e31 2022-10-23 thomas struct gotd_child_proc *proc)
1347 3efd8e31 2022-10-23 thomas if (!client_is_reading(client)) {
1348 3efd8e31 2022-10-23 thomas kill_proc(proc, 1);
1349 3efd8e31 2022-10-23 thomas return got_error_fmt(GOT_ERR_BAD_PACKET,
1350 3efd8e31 2022-10-23 thomas "PID %d handled a read-request for uid %d but this "
1351 3efd8e31 2022-10-23 thomas "user is not reading from a repository", proc->pid,
1352 3efd8e31 2022-10-23 thomas client->euid);
1355 3efd8e31 2022-10-23 thomas return NULL;
1358 3efd8e31 2022-10-23 thomas static const struct got_error *
1359 3efd8e31 2022-10-23 thomas ensure_proc_is_writing(struct gotd_client *client,
1360 3efd8e31 2022-10-23 thomas struct gotd_child_proc *proc)
1362 3efd8e31 2022-10-23 thomas if (!client_is_writing(client)) {
1363 3efd8e31 2022-10-23 thomas kill_proc(proc, 1);
1364 3efd8e31 2022-10-23 thomas return got_error_fmt(GOT_ERR_BAD_PACKET,
1365 3efd8e31 2022-10-23 thomas "PID %d handled a write-request for uid %d but this "
1366 3efd8e31 2022-10-23 thomas "user is not writing to a repository", proc->pid,
1367 3efd8e31 2022-10-23 thomas client->euid);
1370 3efd8e31 2022-10-23 thomas return NULL;
1373 3efd8e31 2022-10-23 thomas static int
1374 3efd8e31 2022-10-23 thomas verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
1375 3efd8e31 2022-10-23 thomas struct imsg *imsg)
1377 3efd8e31 2022-10-23 thomas const struct got_error *err;
1378 3efd8e31 2022-10-23 thomas struct gotd_child_proc *client_proc;
1379 3efd8e31 2022-10-23 thomas int ret = 0;
1381 2b3d32a1 2022-12-30 thomas if (proc->type == PROC_REPO_READ || proc->type == PROC_REPO_WRITE) {
1382 2b3d32a1 2022-12-30 thomas client_proc = get_client_proc(client);
1383 2b3d32a1 2022-12-30 thomas if (client_proc == NULL)
1384 2b3d32a1 2022-12-30 thomas fatalx("no process found for uid %d", client->euid);
1385 2b3d32a1 2022-12-30 thomas if (proc->pid != client_proc->pid) {
1386 2b3d32a1 2022-12-30 thomas kill_proc(proc, 1);
1387 2b3d32a1 2022-12-30 thomas log_warnx("received message from PID %d for uid %d, "
1388 2b3d32a1 2022-12-30 thomas "while PID %d is the process serving this user",
1389 2b3d32a1 2022-12-30 thomas proc->pid, client->euid, client_proc->pid);
1390 2b3d32a1 2022-12-30 thomas return 0;
1394 3efd8e31 2022-10-23 thomas switch (imsg->hdr.type) {
1395 3efd8e31 2022-10-23 thomas case GOTD_IMSG_ERROR:
1396 3efd8e31 2022-10-23 thomas ret = 1;
1398 2b3d32a1 2022-12-30 thomas case GOTD_IMSG_CONNECT:
1399 2b3d32a1 2022-12-30 thomas if (proc->type != PROC_LISTEN) {
1400 2b3d32a1 2022-12-30 thomas err = got_error_fmt(GOT_ERR_BAD_PACKET,
1401 2b3d32a1 2022-12-30 thomas "new connection for uid %d from PID %d "
1402 2b3d32a1 2022-12-30 thomas "which is not the listen process",
1403 c669c489 2022-12-30 thomas proc->pid, client->euid);
1405 c669c489 2022-12-30 thomas ret = 1;
1407 c669c489 2022-12-30 thomas case GOTD_IMSG_ACCESS_GRANTED:
1408 c669c489 2022-12-30 thomas if (proc->type != PROC_AUTH) {
1409 c669c489 2022-12-30 thomas err = got_error_fmt(GOT_ERR_BAD_PACKET,
1410 c669c489 2022-12-30 thomas "authentication of uid %d from PID %d "
1411 c669c489 2022-12-30 thomas "which is not the auth process",
1412 2b3d32a1 2022-12-30 thomas proc->pid, client->euid);
1414 2b3d32a1 2022-12-30 thomas ret = 1;
1416 85b37c72 2022-12-30 thomas case GOTD_IMSG_REPO_CHILD_READY:
1417 85b37c72 2022-12-30 thomas if (proc->type != PROC_REPO_READ &&
1418 85b37c72 2022-12-30 thomas proc->type != PROC_REPO_WRITE) {
1419 85b37c72 2022-12-30 thomas err = got_error_fmt(GOT_ERR_BAD_PACKET,
1420 85b37c72 2022-12-30 thomas "unexpected \"ready\" signal from PID %d",
1421 85b37c72 2022-12-30 thomas proc->pid);
1423 85b37c72 2022-12-30 thomas ret = 1;
1425 3efd8e31 2022-10-23 thomas case GOTD_IMSG_PACKFILE_DONE:
1426 3efd8e31 2022-10-23 thomas err = ensure_proc_is_reading(client, proc);
1427 3efd8e31 2022-10-23 thomas if (err)
1428 3efd8e31 2022-10-23 thomas log_warnx("uid %d: %s", client->euid, err->msg);
1430 3efd8e31 2022-10-23 thomas ret = 1;
1432 3efd8e31 2022-10-23 thomas case GOTD_IMSG_PACKFILE_INSTALL:
1433 3efd8e31 2022-10-23 thomas case GOTD_IMSG_REF_UPDATES_START:
1434 3efd8e31 2022-10-23 thomas case GOTD_IMSG_REF_UPDATE:
1435 3efd8e31 2022-10-23 thomas err = ensure_proc_is_writing(client, proc);
1436 3efd8e31 2022-10-23 thomas if (err)
1437 3efd8e31 2022-10-23 thomas log_warnx("uid %d: %s", client->euid, err->msg);
1439 3efd8e31 2022-10-23 thomas ret = 1;
1441 3efd8e31 2022-10-23 thomas default:
1442 3efd8e31 2022-10-23 thomas log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
1446 3efd8e31 2022-10-23 thomas return ret;
1449 3efd8e31 2022-10-23 thomas static const struct got_error *
1450 85b37c72 2022-12-30 thomas list_refs_request(struct gotd_client *client, struct gotd_imsgev *iev)
1452 85b37c72 2022-12-30 thomas static const struct got_error *err;
1453 85b37c72 2022-12-30 thomas struct gotd_imsg_list_refs_internal ilref;
1456 85b37c72 2022-12-30 thomas memset(&ilref, 0, sizeof(ilref));
1457 85b37c72 2022-12-30 thomas ilref.client_id = client->id;
1459 85b37c72 2022-12-30 thomas fd = dup(client->fd);
1460 85b37c72 2022-12-30 thomas if (fd == -1)
1461 85b37c72 2022-12-30 thomas return got_error_from_errno("dup");
1463 85b37c72 2022-12-30 thomas if (gotd_imsg_compose_event(iev, GOTD_IMSG_LIST_REFS_INTERNAL,
1464 85b37c72 2022-12-30 thomas PROC_GOTD, fd, &ilref, sizeof(ilref)) == -1) {
1465 85b37c72 2022-12-30 thomas err = got_error_from_errno("imsg compose WANT");
1466 85b37c72 2022-12-30 thomas close(fd);
1467 85b37c72 2022-12-30 thomas return err;
1470 85b37c72 2022-12-30 thomas client->state = GOTD_STATE_EXPECT_CAPABILITIES;
1471 85b37c72 2022-12-30 thomas log_debug("uid %d: expecting capabilities", client->euid);
1472 85b37c72 2022-12-30 thomas return NULL;
1475 85b37c72 2022-12-30 thomas static const struct got_error *
1476 3efd8e31 2022-10-23 thomas recv_packfile_done(uint32_t *client_id, struct imsg *imsg)
1478 3efd8e31 2022-10-23 thomas struct gotd_imsg_packfile_done idone;
1479 3efd8e31 2022-10-23 thomas size_t datalen;
1481 3efd8e31 2022-10-23 thomas log_debug("packfile-done received");
1483 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1484 3efd8e31 2022-10-23 thomas if (datalen != sizeof(idone))
1485 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1486 3efd8e31 2022-10-23 thomas memcpy(&idone, imsg->data, sizeof(idone));
1488 3efd8e31 2022-10-23 thomas *client_id = idone.client_id;
1489 3efd8e31 2022-10-23 thomas return NULL;
1492 3efd8e31 2022-10-23 thomas static const struct got_error *
1493 3efd8e31 2022-10-23 thomas recv_packfile_install(uint32_t *client_id, struct imsg *imsg)
1495 3efd8e31 2022-10-23 thomas struct gotd_imsg_packfile_install inst;
1496 3efd8e31 2022-10-23 thomas size_t datalen;
1498 3efd8e31 2022-10-23 thomas log_debug("packfile-install received");
1500 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1501 3efd8e31 2022-10-23 thomas if (datalen != sizeof(inst))
1502 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1503 3efd8e31 2022-10-23 thomas memcpy(&inst, imsg->data, sizeof(inst));
1505 3efd8e31 2022-10-23 thomas *client_id = inst.client_id;
1506 3efd8e31 2022-10-23 thomas return NULL;
1509 3efd8e31 2022-10-23 thomas static const struct got_error *
1510 3efd8e31 2022-10-23 thomas recv_ref_updates_start(uint32_t *client_id, struct imsg *imsg)
1512 3efd8e31 2022-10-23 thomas struct gotd_imsg_ref_updates_start istart;
1513 3efd8e31 2022-10-23 thomas size_t datalen;
1515 3efd8e31 2022-10-23 thomas log_debug("ref-updates-start received");
1517 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1518 3efd8e31 2022-10-23 thomas if (datalen != sizeof(istart))
1519 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1520 3efd8e31 2022-10-23 thomas memcpy(&istart, imsg->data, sizeof(istart));
1522 3efd8e31 2022-10-23 thomas *client_id = istart.client_id;
1523 3efd8e31 2022-10-23 thomas return NULL;
1526 3efd8e31 2022-10-23 thomas static const struct got_error *
1527 3efd8e31 2022-10-23 thomas recv_ref_update(uint32_t *client_id, struct imsg *imsg)
1529 3efd8e31 2022-10-23 thomas struct gotd_imsg_ref_update iref;
1530 3efd8e31 2022-10-23 thomas size_t datalen;
1532 3efd8e31 2022-10-23 thomas log_debug("ref-update received");
1534 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1535 3efd8e31 2022-10-23 thomas if (datalen < sizeof(iref))
1536 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1537 3efd8e31 2022-10-23 thomas memcpy(&iref, imsg->data, sizeof(iref));
1539 3efd8e31 2022-10-23 thomas *client_id = iref.client_id;
1540 3efd8e31 2022-10-23 thomas return NULL;
1543 3efd8e31 2022-10-23 thomas static const struct got_error *
1544 3efd8e31 2022-10-23 thomas send_ref_update_ok(struct gotd_client *client,
1545 3efd8e31 2022-10-23 thomas struct gotd_imsg_ref_update *iref, const char *refname)
1547 3efd8e31 2022-10-23 thomas struct gotd_imsg_ref_update_ok iok;
1548 3efd8e31 2022-10-23 thomas struct ibuf *wbuf;
1549 3efd8e31 2022-10-23 thomas size_t len;
1551 3efd8e31 2022-10-23 thomas memset(&iok, 0, sizeof(iok));
1552 3efd8e31 2022-10-23 thomas iok.client_id = client->id;
1553 3efd8e31 2022-10-23 thomas memcpy(iok.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1554 3efd8e31 2022-10-23 thomas memcpy(iok.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1555 3efd8e31 2022-10-23 thomas iok.name_len = strlen(refname);
1557 3efd8e31 2022-10-23 thomas len = sizeof(iok) + iok.name_len;
1558 3efd8e31 2022-10-23 thomas wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_OK,
1559 3efd8e31 2022-10-23 thomas PROC_GOTD, gotd.pid, len);
1560 3efd8e31 2022-10-23 thomas if (wbuf == NULL)
1561 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_create REF_UPDATE_OK");
1563 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, &iok, sizeof(iok)) == -1)
1564 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_add REF_UPDATE_OK");
1565 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, refname, iok.name_len) == -1)
1566 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_add REF_UPDATE_OK");
1568 3efd8e31 2022-10-23 thomas wbuf->fd = -1;
1569 3efd8e31 2022-10-23 thomas imsg_close(&client->iev.ibuf, wbuf);
1570 3efd8e31 2022-10-23 thomas gotd_imsg_event_add(&client->iev);
1571 3efd8e31 2022-10-23 thomas return NULL;
1574 3efd8e31 2022-10-23 thomas static void
1575 3efd8e31 2022-10-23 thomas send_refs_updated(struct gotd_client *client)
1577 3efd8e31 2022-10-23 thomas if (gotd_imsg_compose_event(&client->iev,
1578 3efd8e31 2022-10-23 thomas GOTD_IMSG_REFS_UPDATED, PROC_GOTD, -1, NULL, 0) == -1)
1579 3efd8e31 2022-10-23 thomas log_warn("imsg compose REFS_UPDATED");
1582 3efd8e31 2022-10-23 thomas static const struct got_error *
1583 3efd8e31 2022-10-23 thomas send_ref_update_ng(struct gotd_client *client,
1584 3efd8e31 2022-10-23 thomas struct gotd_imsg_ref_update *iref, const char *refname,
1585 3efd8e31 2022-10-23 thomas const char *reason)
1587 3efd8e31 2022-10-23 thomas const struct got_error *ng_err;
1588 3efd8e31 2022-10-23 thomas struct gotd_imsg_ref_update_ng ing;
1589 3efd8e31 2022-10-23 thomas struct ibuf *wbuf;
1590 3efd8e31 2022-10-23 thomas size_t len;
1592 3efd8e31 2022-10-23 thomas memset(&ing, 0, sizeof(ing));
1593 3efd8e31 2022-10-23 thomas ing.client_id = client->id;
1594 3efd8e31 2022-10-23 thomas memcpy(ing.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1595 3efd8e31 2022-10-23 thomas memcpy(ing.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1596 3efd8e31 2022-10-23 thomas ing.name_len = strlen(refname);
1598 3efd8e31 2022-10-23 thomas ng_err = got_error_fmt(GOT_ERR_REF_BUSY, "%s", reason);
1599 3efd8e31 2022-10-23 thomas ing.reason_len = strlen(ng_err->msg);
1601 3efd8e31 2022-10-23 thomas len = sizeof(ing) + ing.name_len + ing.reason_len;
1602 3efd8e31 2022-10-23 thomas wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_NG,
1603 3efd8e31 2022-10-23 thomas PROC_GOTD, gotd.pid, len);
1604 3efd8e31 2022-10-23 thomas if (wbuf == NULL)
1605 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_create REF_UPDATE_NG");
1607 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, &ing, sizeof(ing)) == -1)
1608 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_add REF_UPDATE_NG");
1609 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, refname, ing.name_len) == -1)
1610 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_add REF_UPDATE_NG");
1611 3efd8e31 2022-10-23 thomas if (imsg_add(wbuf, ng_err->msg, ing.reason_len) == -1)
1612 3efd8e31 2022-10-23 thomas return got_error_from_errno("imsg_add REF_UPDATE_NG");
1614 3efd8e31 2022-10-23 thomas wbuf->fd = -1;
1615 3efd8e31 2022-10-23 thomas imsg_close(&client->iev.ibuf, wbuf);
1616 3efd8e31 2022-10-23 thomas gotd_imsg_event_add(&client->iev);
1617 3efd8e31 2022-10-23 thomas return NULL;
1620 3efd8e31 2022-10-23 thomas static const struct got_error *
1621 3efd8e31 2022-10-23 thomas install_pack(struct gotd_client *client, const char *repo_path,
1622 3efd8e31 2022-10-23 thomas struct imsg *imsg)
1624 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
1625 3efd8e31 2022-10-23 thomas struct gotd_imsg_packfile_install inst;
1626 3efd8e31 2022-10-23 thomas char hex[SHA1_DIGEST_STRING_LENGTH];
1627 3efd8e31 2022-10-23 thomas size_t datalen;
1628 3efd8e31 2022-10-23 thomas char *packfile_path = NULL, *packidx_path = NULL;
1630 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1631 3efd8e31 2022-10-23 thomas if (datalen != sizeof(inst))
1632 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1633 3efd8e31 2022-10-23 thomas memcpy(&inst, imsg->data, sizeof(inst));
1635 3efd8e31 2022-10-23 thomas if (client->packfile_path == NULL)
1636 3efd8e31 2022-10-23 thomas return got_error_msg(GOT_ERR_BAD_REQUEST,
1637 3efd8e31 2022-10-23 thomas "client has no pack file");
1638 3efd8e31 2022-10-23 thomas if (client->packidx_path == NULL)
1639 3efd8e31 2022-10-23 thomas return got_error_msg(GOT_ERR_BAD_REQUEST,
1640 3efd8e31 2022-10-23 thomas "client has no pack file index");
1642 3efd8e31 2022-10-23 thomas if (got_sha1_digest_to_str(inst.pack_sha1, hex, sizeof(hex)) == NULL)
1643 3efd8e31 2022-10-23 thomas return got_error_msg(GOT_ERR_NO_SPACE,
1644 3efd8e31 2022-10-23 thomas "could not convert pack file SHA1 to hex");
1646 3efd8e31 2022-10-23 thomas if (asprintf(&packfile_path, "/%s/%s/pack-%s.pack",
1647 3efd8e31 2022-10-23 thomas repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1648 3efd8e31 2022-10-23 thomas err = got_error_from_errno("asprintf");
1649 3efd8e31 2022-10-23 thomas goto done;
1652 3efd8e31 2022-10-23 thomas if (asprintf(&packidx_path, "/%s/%s/pack-%s.idx",
1653 3efd8e31 2022-10-23 thomas repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1654 3efd8e31 2022-10-23 thomas err = got_error_from_errno("asprintf");
1655 3efd8e31 2022-10-23 thomas goto done;
1658 3efd8e31 2022-10-23 thomas if (rename(client->packfile_path, packfile_path) == -1) {
1659 3efd8e31 2022-10-23 thomas err = got_error_from_errno3("rename", client->packfile_path,
1660 3efd8e31 2022-10-23 thomas packfile_path);
1661 3efd8e31 2022-10-23 thomas goto done;
1664 3efd8e31 2022-10-23 thomas free(client->packfile_path);
1665 3efd8e31 2022-10-23 thomas client->packfile_path = NULL;
1667 3efd8e31 2022-10-23 thomas if (rename(client->packidx_path, packidx_path) == -1) {
1668 3efd8e31 2022-10-23 thomas err = got_error_from_errno3("rename", client->packidx_path,
1669 3efd8e31 2022-10-23 thomas packidx_path);
1670 3efd8e31 2022-10-23 thomas goto done;
1673 3efd8e31 2022-10-23 thomas free(client->packidx_path);
1674 3efd8e31 2022-10-23 thomas client->packidx_path = NULL;
1676 3efd8e31 2022-10-23 thomas free(packfile_path);
1677 3efd8e31 2022-10-23 thomas free(packidx_path);
1678 3efd8e31 2022-10-23 thomas return err;
1681 3efd8e31 2022-10-23 thomas static const struct got_error *
1682 3efd8e31 2022-10-23 thomas begin_ref_updates(struct gotd_client *client, struct imsg *imsg)
1684 3efd8e31 2022-10-23 thomas struct gotd_imsg_ref_updates_start istart;
1685 3efd8e31 2022-10-23 thomas size_t datalen;
1687 3efd8e31 2022-10-23 thomas if (client->nref_updates != -1)
1688 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
1690 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1691 3efd8e31 2022-10-23 thomas if (datalen != sizeof(istart))
1692 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1693 3efd8e31 2022-10-23 thomas memcpy(&istart, imsg->data, sizeof(istart));
1695 3efd8e31 2022-10-23 thomas if (istart.nref_updates <= 0)
1696 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
1698 3efd8e31 2022-10-23 thomas client->nref_updates = istart.nref_updates;
1699 3efd8e31 2022-10-23 thomas return NULL;
1702 3efd8e31 2022-10-23 thomas static const struct got_error *
1703 3efd8e31 2022-10-23 thomas update_ref(struct gotd_client *client, const char *repo_path,
1704 3efd8e31 2022-10-23 thomas struct imsg *imsg)
1706 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
1707 3efd8e31 2022-10-23 thomas struct got_repository *repo = NULL;
1708 3efd8e31 2022-10-23 thomas struct got_reference *ref = NULL;
1709 3efd8e31 2022-10-23 thomas struct gotd_imsg_ref_update iref;
1710 3efd8e31 2022-10-23 thomas struct got_object_id old_id, new_id;
1711 3efd8e31 2022-10-23 thomas struct got_object_id *id = NULL;
1712 3efd8e31 2022-10-23 thomas struct got_object *obj = NULL;
1713 3efd8e31 2022-10-23 thomas char *refname = NULL;
1714 3efd8e31 2022-10-23 thomas size_t datalen;
1715 3efd8e31 2022-10-23 thomas int locked = 0;
1717 3efd8e31 2022-10-23 thomas log_debug("update-ref from uid %d", client->euid);
1719 3efd8e31 2022-10-23 thomas if (client->nref_updates <= 0)
1720 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
1722 3efd8e31 2022-10-23 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1723 3efd8e31 2022-10-23 thomas if (datalen < sizeof(iref))
1724 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1725 3efd8e31 2022-10-23 thomas memcpy(&iref, imsg->data, sizeof(iref));
1726 3efd8e31 2022-10-23 thomas if (datalen != sizeof(iref) + iref.name_len)
1727 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1728 3efd8e31 2022-10-23 thomas refname = malloc(iref.name_len + 1);
1729 3efd8e31 2022-10-23 thomas if (refname == NULL)
1730 3efd8e31 2022-10-23 thomas return got_error_from_errno("malloc");
1731 3efd8e31 2022-10-23 thomas memcpy(refname, imsg->data + sizeof(iref), iref.name_len);
1732 3efd8e31 2022-10-23 thomas refname[iref.name_len] = '\0';
1734 3efd8e31 2022-10-23 thomas log_debug("updating ref %s for uid %d", refname, client->euid);
1736 3efd8e31 2022-10-23 thomas err = got_repo_open(&repo, repo_path, NULL, NULL);
1737 3efd8e31 2022-10-23 thomas if (err)
1738 3efd8e31 2022-10-23 thomas goto done;
1740 3efd8e31 2022-10-23 thomas memcpy(old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
1741 3efd8e31 2022-10-23 thomas memcpy(new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
1742 3efd8e31 2022-10-23 thomas err = got_object_open(&obj, repo, &new_id);
1743 3efd8e31 2022-10-23 thomas if (err)
1744 3efd8e31 2022-10-23 thomas goto done;
1746 3efd8e31 2022-10-23 thomas if (iref.ref_is_new) {
1747 3efd8e31 2022-10-23 thomas err = got_ref_open(&ref, repo, refname, 0);
1748 3efd8e31 2022-10-23 thomas if (err) {
1749 3efd8e31 2022-10-23 thomas if (err->code != GOT_ERR_NOT_REF)
1750 3efd8e31 2022-10-23 thomas goto done;
1751 3efd8e31 2022-10-23 thomas err = got_ref_alloc(&ref, refname, &new_id);
1752 3efd8e31 2022-10-23 thomas if (err)
1753 3efd8e31 2022-10-23 thomas goto done;
1754 3efd8e31 2022-10-23 thomas err = got_ref_write(ref, repo); /* will lock/unlock */
1755 3efd8e31 2022-10-23 thomas if (err)
1756 3efd8e31 2022-10-23 thomas goto done;
1757 3efd8e31 2022-10-23 thomas } else {
1758 3efd8e31 2022-10-23 thomas err = got_error_fmt(GOT_ERR_REF_BUSY,
1759 3efd8e31 2022-10-23 thomas "%s has been created by someone else "
1760 3efd8e31 2022-10-23 thomas "while transaction was in progress",
1761 3efd8e31 2022-10-23 thomas got_ref_get_name(ref));
1762 3efd8e31 2022-10-23 thomas goto done;
1764 3efd8e31 2022-10-23 thomas } else {
1765 3efd8e31 2022-10-23 thomas err = got_ref_open(&ref, repo, refname, 1 /* lock */);
1766 3efd8e31 2022-10-23 thomas if (err)
1767 3efd8e31 2022-10-23 thomas goto done;
1768 3efd8e31 2022-10-23 thomas locked = 1;
1770 3efd8e31 2022-10-23 thomas err = got_ref_resolve(&id, repo, ref);
1771 3efd8e31 2022-10-23 thomas if (err)
1772 3efd8e31 2022-10-23 thomas goto done;
1774 3efd8e31 2022-10-23 thomas if (got_object_id_cmp(id, &old_id) != 0) {
1775 3efd8e31 2022-10-23 thomas err = got_error_fmt(GOT_ERR_REF_BUSY,
1776 3efd8e31 2022-10-23 thomas "%s has been modified by someone else "
1777 3efd8e31 2022-10-23 thomas "while transaction was in progress",
1778 3efd8e31 2022-10-23 thomas got_ref_get_name(ref));
1779 3efd8e31 2022-10-23 thomas goto done;
1782 3efd8e31 2022-10-23 thomas err = got_ref_change_ref(ref, &new_id);
1783 3efd8e31 2022-10-23 thomas if (err)
1784 3efd8e31 2022-10-23 thomas goto done;
1786 3efd8e31 2022-10-23 thomas err = got_ref_write(ref, repo);
1787 3efd8e31 2022-10-23 thomas if (err)
1788 3efd8e31 2022-10-23 thomas goto done;
1790 3efd8e31 2022-10-23 thomas free(id);
1791 3efd8e31 2022-10-23 thomas id = NULL;
1794 3efd8e31 2022-10-23 thomas if (err) {
1795 3efd8e31 2022-10-23 thomas if (err->code == GOT_ERR_LOCKFILE_TIMEOUT) {
1796 3efd8e31 2022-10-23 thomas err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
1797 3efd8e31 2022-10-23 thomas "could not acquire exclusive file lock for %s",
1798 3efd8e31 2022-10-23 thomas refname);
1800 3efd8e31 2022-10-23 thomas send_ref_update_ng(client, &iref, refname, err->msg);
1802 3efd8e31 2022-10-23 thomas send_ref_update_ok(client, &iref, refname);
1804 3efd8e31 2022-10-23 thomas if (client->nref_updates > 0) {
1805 3efd8e31 2022-10-23 thomas client->nref_updates--;
1806 3efd8e31 2022-10-23 thomas if (client->nref_updates == 0)
1807 3efd8e31 2022-10-23 thomas send_refs_updated(client);
1810 3efd8e31 2022-10-23 thomas if (locked) {
1811 3efd8e31 2022-10-23 thomas const struct got_error *unlock_err;
1812 3efd8e31 2022-10-23 thomas unlock_err = got_ref_unlock(ref);
1813 3efd8e31 2022-10-23 thomas if (unlock_err && err == NULL)
1814 3efd8e31 2022-10-23 thomas err = unlock_err;
1816 3efd8e31 2022-10-23 thomas if (ref)
1817 3efd8e31 2022-10-23 thomas got_ref_close(ref);
1818 3efd8e31 2022-10-23 thomas if (obj)
1819 3efd8e31 2022-10-23 thomas got_object_close(obj);
1820 3efd8e31 2022-10-23 thomas if (repo)
1821 3efd8e31 2022-10-23 thomas got_repo_close(repo);
1822 3efd8e31 2022-10-23 thomas free(refname);
1823 3efd8e31 2022-10-23 thomas free(id);
1824 3efd8e31 2022-10-23 thomas return err;
1827 3efd8e31 2022-10-23 thomas static void
1828 85b37c72 2022-12-30 thomas gotd_dispatch_listener(int fd, short event, void *arg)
1830 3efd8e31 2022-10-23 thomas struct gotd_imsgev *iev = arg;
1831 3efd8e31 2022-10-23 thomas struct imsgbuf *ibuf = &iev->ibuf;
1832 85b37c72 2022-12-30 thomas struct gotd_child_proc *proc = &gotd.listen_proc;
1833 85b37c72 2022-12-30 thomas ssize_t n;
1834 85b37c72 2022-12-30 thomas int shut = 0;
1835 85b37c72 2022-12-30 thomas struct imsg imsg;
1837 85b37c72 2022-12-30 thomas if (proc->iev.ibuf.fd != fd)
1838 85b37c72 2022-12-30 thomas fatalx("%s: unexpected fd %d", __func__, fd);
1840 85b37c72 2022-12-30 thomas if (event & EV_READ) {
1841 85b37c72 2022-12-30 thomas if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1842 85b37c72 2022-12-30 thomas fatal("imsg_read error");
1843 85b37c72 2022-12-30 thomas if (n == 0) {
1844 85b37c72 2022-12-30 thomas /* Connection closed. */
1845 85b37c72 2022-12-30 thomas shut = 1;
1846 85b37c72 2022-12-30 thomas goto done;
1850 85b37c72 2022-12-30 thomas if (event & EV_WRITE) {
1851 85b37c72 2022-12-30 thomas n = msgbuf_write(&ibuf->w);
1852 85b37c72 2022-12-30 thomas if (n == -1 && errno != EAGAIN)
1853 85b37c72 2022-12-30 thomas fatal("msgbuf_write");
1854 85b37c72 2022-12-30 thomas if (n == 0) {
1855 85b37c72 2022-12-30 thomas /* Connection closed. */
1856 85b37c72 2022-12-30 thomas shut = 1;
1857 85b37c72 2022-12-30 thomas goto done;
1861 85b37c72 2022-12-30 thomas for (;;) {
1862 85b37c72 2022-12-30 thomas const struct got_error *err = NULL;
1863 85b37c72 2022-12-30 thomas struct gotd_client *client = NULL;
1864 85b37c72 2022-12-30 thomas uint32_t client_id = 0;
1865 85b37c72 2022-12-30 thomas int do_disconnect = 0;
1867 85b37c72 2022-12-30 thomas if ((n = imsg_get(ibuf, &imsg)) == -1)
1868 85b37c72 2022-12-30 thomas fatal("%s: imsg_get error", __func__);
1869 85b37c72 2022-12-30 thomas if (n == 0) /* No more messages. */
1872 85b37c72 2022-12-30 thomas switch (imsg.hdr.type) {
1873 85b37c72 2022-12-30 thomas case GOTD_IMSG_ERROR:
1874 85b37c72 2022-12-30 thomas do_disconnect = 1;
1875 85b37c72 2022-12-30 thomas err = gotd_imsg_recv_error(&client_id, &imsg);
1877 85b37c72 2022-12-30 thomas case GOTD_IMSG_CONNECT:
1878 85b37c72 2022-12-30 thomas err = recv_connect(&client_id, &imsg);
1880 85b37c72 2022-12-30 thomas default:
1881 85b37c72 2022-12-30 thomas log_debug("unexpected imsg %d", imsg.hdr.type);
1885 85b37c72 2022-12-30 thomas client = find_client(client_id);
1886 85b37c72 2022-12-30 thomas if (client == NULL) {
1887 85b37c72 2022-12-30 thomas log_warnx("%s: client not found", __func__);
1888 85b37c72 2022-12-30 thomas imsg_free(&imsg);
1889 85b37c72 2022-12-30 thomas continue;
1892 85b37c72 2022-12-30 thomas if (err)
1893 85b37c72 2022-12-30 thomas log_warnx("uid %d: %s", client->euid, err->msg);
1895 85b37c72 2022-12-30 thomas if (do_disconnect) {
1896 85b37c72 2022-12-30 thomas if (err)
1897 85b37c72 2022-12-30 thomas disconnect_on_error(client, err);
1899 85b37c72 2022-12-30 thomas disconnect(client);
1902 85b37c72 2022-12-30 thomas imsg_free(&imsg);
1905 85b37c72 2022-12-30 thomas if (!shut) {
1906 85b37c72 2022-12-30 thomas gotd_imsg_event_add(iev);
1907 85b37c72 2022-12-30 thomas } else {
1908 85b37c72 2022-12-30 thomas /* This pipe is dead. Remove its event handler */
1909 85b37c72 2022-12-30 thomas event_del(&iev->ev);
1910 85b37c72 2022-12-30 thomas event_loopexit(NULL);
1914 85b37c72 2022-12-30 thomas static void
1915 c669c489 2022-12-30 thomas gotd_dispatch_auth_child(int fd, short event, void *arg)
1917 c669c489 2022-12-30 thomas const struct got_error *err = NULL;
1918 c669c489 2022-12-30 thomas struct gotd_imsgev *iev = arg;
1919 c669c489 2022-12-30 thomas struct imsgbuf *ibuf = &iev->ibuf;
1920 c669c489 2022-12-30 thomas struct gotd_client *client;
1921 c669c489 2022-12-30 thomas struct gotd_repo *repo = NULL;
1922 c669c489 2022-12-30 thomas ssize_t n;
1923 c669c489 2022-12-30 thomas int shut = 0;
1924 c669c489 2022-12-30 thomas struct imsg imsg;
1925 c669c489 2022-12-30 thomas uint32_t client_id = 0;
1926 c669c489 2022-12-30 thomas int do_disconnect = 0;
1927 c669c489 2022-12-30 thomas enum gotd_procid proc_type;
1929 c669c489 2022-12-30 thomas client = find_client_by_proc_fd(fd);
1930 c669c489 2022-12-30 thomas if (client == NULL)
1931 c669c489 2022-12-30 thomas fatalx("cannot find client for fd %d", fd);
1933 c669c489 2022-12-30 thomas if (client->auth == NULL)
1934 c669c489 2022-12-30 thomas fatalx("cannot find auth child process for fd %d", fd);
1936 c669c489 2022-12-30 thomas if (event & EV_READ) {
1937 c669c489 2022-12-30 thomas if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1938 c669c489 2022-12-30 thomas fatal("imsg_read error");
1939 c669c489 2022-12-30 thomas if (n == 0) {
1940 c669c489 2022-12-30 thomas /* Connection closed. */
1941 c669c489 2022-12-30 thomas shut = 1;
1942 c669c489 2022-12-30 thomas goto done;
1946 c669c489 2022-12-30 thomas if (event & EV_WRITE) {
1947 c669c489 2022-12-30 thomas n = msgbuf_write(&ibuf->w);
1948 c669c489 2022-12-30 thomas if (n == -1 && errno != EAGAIN)
1949 c669c489 2022-12-30 thomas fatal("msgbuf_write");
1950 c669c489 2022-12-30 thomas if (n == 0) {
1951 c669c489 2022-12-30 thomas /* Connection closed. */
1952 c669c489 2022-12-30 thomas shut = 1;
1954 c669c489 2022-12-30 thomas goto done;
1957 c669c489 2022-12-30 thomas if (client->auth->iev.ibuf.fd != fd)
1958 c669c489 2022-12-30 thomas fatalx("%s: unexpected fd %d", __func__, fd);
1960 c669c489 2022-12-30 thomas if ((n = imsg_get(ibuf, &imsg)) == -1)
1961 c669c489 2022-12-30 thomas fatal("%s: imsg_get error", __func__);
1962 c669c489 2022-12-30 thomas if (n == 0) /* No more messages. */
1965 c669c489 2022-12-30 thomas evtimer_del(&client->tmo);
1967 c669c489 2022-12-30 thomas switch (imsg.hdr.type) {
1968 c669c489 2022-12-30 thomas case GOTD_IMSG_ERROR:
1969 c669c489 2022-12-30 thomas do_disconnect = 1;
1970 c669c489 2022-12-30 thomas err = gotd_imsg_recv_error(&client_id, &imsg);
1972 c669c489 2022-12-30 thomas case GOTD_IMSG_ACCESS_GRANTED:
1974 c669c489 2022-12-30 thomas default:
1975 c669c489 2022-12-30 thomas do_disconnect = 1;
1976 c669c489 2022-12-30 thomas log_debug("unexpected imsg %d", imsg.hdr.type);
1980 c669c489 2022-12-30 thomas if (!verify_imsg_src(client, client->auth, &imsg)) {
1981 c669c489 2022-12-30 thomas do_disconnect = 1;
1982 c669c489 2022-12-30 thomas log_debug("dropping imsg type %d from PID %d",
1983 c669c489 2022-12-30 thomas imsg.hdr.type, client->auth->pid);
1985 c669c489 2022-12-30 thomas imsg_free(&imsg);
1987 c669c489 2022-12-30 thomas if (do_disconnect) {
1988 c669c489 2022-12-30 thomas if (err)
1989 c669c489 2022-12-30 thomas disconnect_on_error(client, err);
1991 c669c489 2022-12-30 thomas disconnect(client);
1992 c669c489 2022-12-30 thomas goto done;
1995 c669c489 2022-12-30 thomas repo = find_repo_by_name(client->auth->repo_name);
1996 c669c489 2022-12-30 thomas if (repo == NULL) {
1997 c669c489 2022-12-30 thomas err = got_error(GOT_ERR_NOT_GIT_REPO);
1998 c669c489 2022-12-30 thomas goto done;
2000 c669c489 2022-12-30 thomas kill_auth_proc(client);
2002 c669c489 2022-12-30 thomas log_info("authenticated uid %d for repository %s\n",
2003 c669c489 2022-12-30 thomas client->euid, repo->name);
2005 c669c489 2022-12-30 thomas if (client->required_auth & GOTD_AUTH_WRITE)
2006 c669c489 2022-12-30 thomas proc_type = PROC_REPO_WRITE;
2008 c669c489 2022-12-30 thomas proc_type = PROC_REPO_READ;
2010 46ecc01f 2022-12-30 thomas err = start_repo_child(client, proc_type, repo, gotd.argv0,
2011 46ecc01f 2022-12-30 thomas gotd.confpath, gotd.daemonize, gotd.verbosity);
2013 c669c489 2022-12-30 thomas if (err)
2014 c669c489 2022-12-30 thomas log_warnx("uid %d: %s", client->euid, err->msg);
2016 c669c489 2022-12-30 thomas /* We might have killed the auth process by now. */
2017 c669c489 2022-12-30 thomas if (client->auth != NULL) {
2018 c669c489 2022-12-30 thomas if (!shut) {
2019 c669c489 2022-12-30 thomas gotd_imsg_event_add(iev);
2020 c669c489 2022-12-30 thomas } else {
2021 c669c489 2022-12-30 thomas /* This pipe is dead. Remove its event handler */
2022 c669c489 2022-12-30 thomas event_del(&iev->ev);
2027 c669c489 2022-12-30 thomas static void
2028 85b37c72 2022-12-30 thomas gotd_dispatch_repo_child(int fd, short event, void *arg)
2030 85b37c72 2022-12-30 thomas struct gotd_imsgev *iev = arg;
2031 85b37c72 2022-12-30 thomas struct imsgbuf *ibuf = &iev->ibuf;
2032 3efd8e31 2022-10-23 thomas struct gotd_child_proc *proc = NULL;
2033 85b37c72 2022-12-30 thomas struct gotd_client *client = NULL;
2034 3efd8e31 2022-10-23 thomas ssize_t n;
2035 3efd8e31 2022-10-23 thomas int shut = 0;
2036 3efd8e31 2022-10-23 thomas struct imsg imsg;
2038 3efd8e31 2022-10-23 thomas if (event & EV_READ) {
2039 3efd8e31 2022-10-23 thomas if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
2040 3efd8e31 2022-10-23 thomas fatal("imsg_read error");
2041 3efd8e31 2022-10-23 thomas if (n == 0) {
2042 3efd8e31 2022-10-23 thomas /* Connection closed. */
2043 3efd8e31 2022-10-23 thomas shut = 1;
2044 3efd8e31 2022-10-23 thomas goto done;
2048 3efd8e31 2022-10-23 thomas if (event & EV_WRITE) {
2049 3efd8e31 2022-10-23 thomas n = msgbuf_write(&ibuf->w);
2050 3efd8e31 2022-10-23 thomas if (n == -1 && errno != EAGAIN)
2051 3efd8e31 2022-10-23 thomas fatal("msgbuf_write");
2052 3efd8e31 2022-10-23 thomas if (n == 0) {
2053 3efd8e31 2022-10-23 thomas /* Connection closed. */
2054 3efd8e31 2022-10-23 thomas shut = 1;
2055 3efd8e31 2022-10-23 thomas goto done;
2059 85b37c72 2022-12-30 thomas client = find_client_by_proc_fd(fd);
2060 85b37c72 2022-12-30 thomas if (client == NULL)
2061 85b37c72 2022-12-30 thomas fatalx("cannot find client for fd %d", fd);
2063 85b37c72 2022-12-30 thomas proc = get_client_proc(client);
2064 3efd8e31 2022-10-23 thomas if (proc == NULL)
2065 3efd8e31 2022-10-23 thomas fatalx("cannot find child process for fd %d", fd);
2067 3efd8e31 2022-10-23 thomas for (;;) {
2068 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
2069 3efd8e31 2022-10-23 thomas uint32_t client_id = 0;
2070 3efd8e31 2022-10-23 thomas int do_disconnect = 0;
2071 85b37c72 2022-12-30 thomas int do_list_refs = 0, do_ref_updates = 0, do_ref_update = 0;
2072 3efd8e31 2022-10-23 thomas int do_packfile_install = 0;
2074 3efd8e31 2022-10-23 thomas if ((n = imsg_get(ibuf, &imsg)) == -1)
2075 3efd8e31 2022-10-23 thomas fatal("%s: imsg_get error", __func__);
2076 3efd8e31 2022-10-23 thomas if (n == 0) /* No more messages. */
2079 3efd8e31 2022-10-23 thomas switch (imsg.hdr.type) {
2080 3efd8e31 2022-10-23 thomas case GOTD_IMSG_ERROR:
2081 3efd8e31 2022-10-23 thomas do_disconnect = 1;
2082 3efd8e31 2022-10-23 thomas err = gotd_imsg_recv_error(&client_id, &imsg);
2084 85b37c72 2022-12-30 thomas case GOTD_IMSG_REPO_CHILD_READY:
2085 85b37c72 2022-12-30 thomas do_list_refs = 1;
2087 3efd8e31 2022-10-23 thomas case GOTD_IMSG_PACKFILE_DONE:
2088 3efd8e31 2022-10-23 thomas do_disconnect = 1;
2089 3efd8e31 2022-10-23 thomas err = recv_packfile_done(&client_id, &imsg);
2091 3efd8e31 2022-10-23 thomas case GOTD_IMSG_PACKFILE_INSTALL:
2092 3efd8e31 2022-10-23 thomas err = recv_packfile_install(&client_id, &imsg);
2093 3efd8e31 2022-10-23 thomas if (err == NULL)
2094 3efd8e31 2022-10-23 thomas do_packfile_install = 1;
2096 3efd8e31 2022-10-23 thomas case GOTD_IMSG_REF_UPDATES_START:
2097 3efd8e31 2022-10-23 thomas err = recv_ref_updates_start(&client_id, &imsg);
2098 3efd8e31 2022-10-23 thomas if (err == NULL)
2099 3efd8e31 2022-10-23 thomas do_ref_updates = 1;
2101 3efd8e31 2022-10-23 thomas case GOTD_IMSG_REF_UPDATE:
2102 3efd8e31 2022-10-23 thomas err = recv_ref_update(&client_id, &imsg);
2103 3efd8e31 2022-10-23 thomas if (err == NULL)
2104 3efd8e31 2022-10-23 thomas do_ref_update = 1;
2106 3efd8e31 2022-10-23 thomas default:
2107 3efd8e31 2022-10-23 thomas log_debug("unexpected imsg %d", imsg.hdr.type);
2111 3efd8e31 2022-10-23 thomas if (!verify_imsg_src(client, proc, &imsg)) {
2112 3efd8e31 2022-10-23 thomas log_debug("dropping imsg type %d from PID %d",
2113 3efd8e31 2022-10-23 thomas imsg.hdr.type, proc->pid);
2114 3efd8e31 2022-10-23 thomas imsg_free(&imsg);
2115 3efd8e31 2022-10-23 thomas continue;
2117 3efd8e31 2022-10-23 thomas if (err)
2118 3efd8e31 2022-10-23 thomas log_warnx("uid %d: %s", client->euid, err->msg);
2120 3efd8e31 2022-10-23 thomas if (do_disconnect) {
2121 3efd8e31 2022-10-23 thomas if (err)
2122 3efd8e31 2022-10-23 thomas disconnect_on_error(client, err);
2124 3efd8e31 2022-10-23 thomas disconnect(client);
2125 965fcba6 2022-11-04 thomas } else {
2126 85b37c72 2022-12-30 thomas if (do_list_refs)
2127 85b37c72 2022-12-30 thomas err = list_refs_request(client, iev);
2128 85b37c72 2022-12-30 thomas else if (do_packfile_install)
2129 414e37cb 2022-12-30 thomas err = install_pack(client, proc->repo_path,
2131 965fcba6 2022-11-04 thomas else if (do_ref_updates)
2132 965fcba6 2022-11-04 thomas err = begin_ref_updates(client, &imsg);
2133 965fcba6 2022-11-04 thomas else if (do_ref_update)
2134 414e37cb 2022-12-30 thomas err = update_ref(client, proc->repo_path,
2136 965fcba6 2022-11-04 thomas if (err)
2137 965fcba6 2022-11-04 thomas log_warnx("uid %d: %s", client->euid, err->msg);
2139 3efd8e31 2022-10-23 thomas imsg_free(&imsg);
2142 3efd8e31 2022-10-23 thomas if (!shut) {
2143 3efd8e31 2022-10-23 thomas gotd_imsg_event_add(iev);
2144 3efd8e31 2022-10-23 thomas } else {
2145 3efd8e31 2022-10-23 thomas /* This pipe is dead. Remove its event handler */
2146 3efd8e31 2022-10-23 thomas event_del(&iev->ev);
2147 3efd8e31 2022-10-23 thomas event_loopexit(NULL);
2151 3efd8e31 2022-10-23 thomas static pid_t
2152 414e37cb 2022-12-30 thomas start_child(enum gotd_procid proc_id, const char *repo_path,
2153 832b8374 2022-10-31 thomas char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
2155 832b8374 2022-10-31 thomas char *argv[11];
2156 3efd8e31 2022-10-23 thomas int argc = 0;
2157 3efd8e31 2022-10-23 thomas pid_t pid;
2159 3efd8e31 2022-10-23 thomas switch (pid = fork()) {
2160 3efd8e31 2022-10-23 thomas case -1:
2161 3efd8e31 2022-10-23 thomas fatal("cannot fork");
2164 3efd8e31 2022-10-23 thomas default:
2165 3efd8e31 2022-10-23 thomas close(fd);
2166 3efd8e31 2022-10-23 thomas return pid;
2169 bb3a6ce9 2022-11-17 thomas if (fd != GOTD_FILENO_MSG_PIPE) {
2170 bb3a6ce9 2022-11-17 thomas if (dup2(fd, GOTD_FILENO_MSG_PIPE) == -1)
2171 3efd8e31 2022-10-23 thomas fatal("cannot setup imsg fd");
2172 3efd8e31 2022-10-23 thomas } else if (fcntl(fd, F_SETFD, 0) == -1)
2173 3efd8e31 2022-10-23 thomas fatal("cannot setup imsg fd");
2175 3efd8e31 2022-10-23 thomas argv[argc++] = argv0;
2176 3efd8e31 2022-10-23 thomas switch (proc_id) {
2177 2b3d32a1 2022-12-30 thomas case PROC_LISTEN:
2178 2b3d32a1 2022-12-30 thomas argv[argc++] = (char *)"-L";
2180 c669c489 2022-12-30 thomas case PROC_AUTH:
2181 c669c489 2022-12-30 thomas argv[argc++] = (char *)"-A";
2183 3efd8e31 2022-10-23 thomas case PROC_REPO_READ:
2184 3efd8e31 2022-10-23 thomas argv[argc++] = (char *)"-R";
2186 3efd8e31 2022-10-23 thomas case PROC_REPO_WRITE:
2187 3efd8e31 2022-10-23 thomas argv[argc++] = (char *)"-W";
2189 3efd8e31 2022-10-23 thomas default:
2190 3efd8e31 2022-10-23 thomas fatalx("invalid process id %d", proc_id);
2193 832b8374 2022-10-31 thomas argv[argc++] = (char *)"-f";
2194 832b8374 2022-10-31 thomas argv[argc++] = (char *)confpath;
2196 414e37cb 2022-12-30 thomas if (repo_path) {
2197 2b3d32a1 2022-12-30 thomas argv[argc++] = (char *)"-P";
2198 414e37cb 2022-12-30 thomas argv[argc++] = (char *)repo_path;
2201 3efd8e31 2022-10-23 thomas if (!daemonize)
2202 3efd8e31 2022-10-23 thomas argv[argc++] = (char *)"-d";
2203 3efd8e31 2022-10-23 thomas if (verbosity > 0)
2204 3efd8e31 2022-10-23 thomas argv[argc++] = (char *)"-v";
2205 3efd8e31 2022-10-23 thomas if (verbosity > 1)
2206 3efd8e31 2022-10-23 thomas argv[argc++] = (char *)"-v";
2207 3efd8e31 2022-10-23 thomas argv[argc++] = NULL;
2209 3efd8e31 2022-10-23 thomas execvp(argv0, argv);
2210 3efd8e31 2022-10-23 thomas fatal("execvp");
2213 3efd8e31 2022-10-23 thomas static void
2214 2b3d32a1 2022-12-30 thomas start_listener(char *argv0, const char *confpath, int daemonize, int verbosity)
2216 85b37c72 2022-12-30 thomas struct gotd_child_proc *proc = &gotd.listen_proc;
2218 2b3d32a1 2022-12-30 thomas proc->type = PROC_LISTEN;
2220 2b3d32a1 2022-12-30 thomas if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
2221 2b3d32a1 2022-12-30 thomas PF_UNSPEC, proc->pipe) == -1)
2222 2b3d32a1 2022-12-30 thomas fatal("socketpair");
2224 2b3d32a1 2022-12-30 thomas proc->pid = start_child(proc->type, NULL, argv0, confpath,
2225 2b3d32a1 2022-12-30 thomas proc->pipe[1], daemonize, verbosity);
2226 2b3d32a1 2022-12-30 thomas imsg_init(&proc->iev.ibuf, proc->pipe[0]);
2227 85b37c72 2022-12-30 thomas proc->iev.handler = gotd_dispatch_listener;
2228 2b3d32a1 2022-12-30 thomas proc->iev.events = EV_READ;
2229 2b3d32a1 2022-12-30 thomas proc->iev.handler_arg = NULL;
2232 85b37c72 2022-12-30 thomas static const struct got_error *
2233 85b37c72 2022-12-30 thomas start_repo_child(struct gotd_client *client, enum gotd_procid proc_type,
2234 85b37c72 2022-12-30 thomas struct gotd_repo *repo, char *argv0, const char *confpath,
2235 832b8374 2022-10-31 thomas int daemonize, int verbosity)
2237 3efd8e31 2022-10-23 thomas struct gotd_child_proc *proc;
2239 85b37c72 2022-12-30 thomas if (proc_type != PROC_REPO_READ && proc_type != PROC_REPO_WRITE)
2240 85b37c72 2022-12-30 thomas return got_error_msg(GOT_ERR_NOT_IMPL, "bad process type");
2242 85b37c72 2022-12-30 thomas proc = calloc(1, sizeof(*proc));
2243 85b37c72 2022-12-30 thomas if (proc == NULL)
2244 85b37c72 2022-12-30 thomas return got_error_from_errno("calloc");
2246 85b37c72 2022-12-30 thomas proc->type = proc_type;
2247 85b37c72 2022-12-30 thomas if (strlcpy(proc->repo_name, repo->name,
2248 85b37c72 2022-12-30 thomas sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
2249 85b37c72 2022-12-30 thomas fatalx("repository name too long: %s", repo->name);
2250 85b37c72 2022-12-30 thomas log_debug("starting %s for repository %s",
2251 85b37c72 2022-12-30 thomas proc->type == PROC_REPO_READ ? "reader" : "writer", repo->name);
2252 fe6a8988 2023-01-08 thomas if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
2253 fe6a8988 2023-01-08 thomas sizeof(proc->repo_path))
2254 fe6a8988 2023-01-08 thomas fatalx("repository path too long: %s", repo->path);
2255 85b37c72 2022-12-30 thomas if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
2256 85b37c72 2022-12-30 thomas PF_UNSPEC, proc->pipe) == -1)
2257 85b37c72 2022-12-30 thomas fatal("socketpair");
2258 85b37c72 2022-12-30 thomas proc->pid = start_child(proc->type, proc->repo_path, argv0,
2259 85b37c72 2022-12-30 thomas confpath, proc->pipe[1], daemonize, verbosity);
2260 85b37c72 2022-12-30 thomas imsg_init(&proc->iev.ibuf, proc->pipe[0]);
2261 85b37c72 2022-12-30 thomas log_debug("proc %s %s is on fd %d",
2262 85b37c72 2022-12-30 thomas gotd_proc_names[proc->type], proc->repo_path,
2263 85b37c72 2022-12-30 thomas proc->pipe[0]);
2264 85b37c72 2022-12-30 thomas proc->iev.handler = gotd_dispatch_repo_child;
2265 85b37c72 2022-12-30 thomas proc->iev.events = EV_READ;
2266 85b37c72 2022-12-30 thomas proc->iev.handler_arg = NULL;
2267 85b37c72 2022-12-30 thomas event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
2268 85b37c72 2022-12-30 thomas gotd_dispatch_repo_child, &proc->iev);
2269 85b37c72 2022-12-30 thomas gotd_imsg_event_add(&proc->iev);
2271 85b37c72 2022-12-30 thomas if (proc->type == PROC_REPO_READ)
2272 85b37c72 2022-12-30 thomas client->repo_read = proc;
2274 85b37c72 2022-12-30 thomas client->repo_write = proc;
2276 c669c489 2022-12-30 thomas return NULL;
2279 c669c489 2022-12-30 thomas static const struct got_error *
2280 c669c489 2022-12-30 thomas start_auth_child(struct gotd_client *client, int required_auth,
2281 c669c489 2022-12-30 thomas struct gotd_repo *repo, char *argv0, const char *confpath,
2282 c669c489 2022-12-30 thomas int daemonize, int verbosity)
2284 0bcde4c8 2022-12-30 thomas const struct got_error *err = NULL;
2285 c669c489 2022-12-30 thomas struct gotd_child_proc *proc;
2286 c669c489 2022-12-30 thomas struct gotd_imsg_auth iauth;
2289 c669c489 2022-12-30 thomas memset(&iauth, 0, sizeof(iauth));
2291 0bcde4c8 2022-12-30 thomas fd = dup(client->fd);
2292 0bcde4c8 2022-12-30 thomas if (fd == -1)
2293 0bcde4c8 2022-12-30 thomas return got_error_from_errno("dup");
2295 c669c489 2022-12-30 thomas proc = calloc(1, sizeof(*proc));
2296 0bcde4c8 2022-12-30 thomas if (proc == NULL) {
2297 0bcde4c8 2022-12-30 thomas err = got_error_from_errno("calloc");
2298 0bcde4c8 2022-12-30 thomas close(fd);
2299 0bcde4c8 2022-12-30 thomas return err;
2302 c669c489 2022-12-30 thomas proc->type = PROC_AUTH;
2303 c669c489 2022-12-30 thomas if (strlcpy(proc->repo_name, repo->name,
2304 c669c489 2022-12-30 thomas sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
2305 c669c489 2022-12-30 thomas fatalx("repository name too long: %s", repo->name);
2306 c669c489 2022-12-30 thomas log_debug("starting auth for uid %d repository %s",
2307 c669c489 2022-12-30 thomas client->euid, repo->name);
2308 fe6a8988 2023-01-08 thomas if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
2309 fe6a8988 2023-01-08 thomas sizeof(proc->repo_path))
2310 fe6a8988 2023-01-08 thomas fatalx("repository path too long: %s", repo->path);
2311 c669c489 2022-12-30 thomas if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
2312 c669c489 2022-12-30 thomas PF_UNSPEC, proc->pipe) == -1)
2313 c669c489 2022-12-30 thomas fatal("socketpair");
2314 c669c489 2022-12-30 thomas proc->pid = start_child(proc->type, proc->repo_path, argv0,
2315 c669c489 2022-12-30 thomas confpath, proc->pipe[1], daemonize, verbosity);
2316 c669c489 2022-12-30 thomas imsg_init(&proc->iev.ibuf, proc->pipe[0]);
2317 c669c489 2022-12-30 thomas log_debug("proc %s %s is on fd %d",
2318 c669c489 2022-12-30 thomas gotd_proc_names[proc->type], proc->repo_path,
2319 c669c489 2022-12-30 thomas proc->pipe[0]);
2320 c669c489 2022-12-30 thomas proc->iev.handler = gotd_dispatch_auth_child;
2321 c669c489 2022-12-30 thomas proc->iev.events = EV_READ;
2322 c669c489 2022-12-30 thomas proc->iev.handler_arg = NULL;
2323 c669c489 2022-12-30 thomas event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
2324 c669c489 2022-12-30 thomas gotd_dispatch_auth_child, &proc->iev);
2325 c669c489 2022-12-30 thomas gotd_imsg_event_add(&proc->iev);
2327 c669c489 2022-12-30 thomas iauth.euid = client->euid;
2328 c669c489 2022-12-30 thomas iauth.egid = client->egid;
2329 c669c489 2022-12-30 thomas iauth.required_auth = required_auth;
2330 c669c489 2022-12-30 thomas iauth.client_id = client->id;
2331 c669c489 2022-12-30 thomas if (gotd_imsg_compose_event(&proc->iev, GOTD_IMSG_AUTHENTICATE,
2332 0bcde4c8 2022-12-30 thomas PROC_GOTD, fd, &iauth, sizeof(iauth)) == -1) {
2333 c669c489 2022-12-30 thomas log_warn("imsg compose AUTHENTICATE");
2334 0bcde4c8 2022-12-30 thomas close(fd);
2335 0bcde4c8 2022-12-30 thomas /* Let the auth_timeout handler tidy up. */
2338 c669c489 2022-12-30 thomas client->auth = proc;
2339 c669c489 2022-12-30 thomas client->required_auth = required_auth;
2340 85b37c72 2022-12-30 thomas return NULL;
2343 414e37cb 2022-12-30 thomas static void
2344 414e37cb 2022-12-30 thomas apply_unveil_repo_readonly(const char *repo_path)
2346 414e37cb 2022-12-30 thomas if (unveil(repo_path, "r") == -1)
2347 414e37cb 2022-12-30 thomas fatal("unveil %s", repo_path);
2349 b942ab08 2022-12-30 thomas if (unveil(NULL, NULL) == -1)
2350 b942ab08 2022-12-30 thomas fatal("unveil");
2353 b942ab08 2022-12-30 thomas static void
2354 b942ab08 2022-12-30 thomas apply_unveil_none(void)
2356 b942ab08 2022-12-30 thomas if (unveil("/", "") == -1)
2357 b942ab08 2022-12-30 thomas fatal("unveil");
2359 414e37cb 2022-12-30 thomas if (unveil(NULL, NULL) == -1)
2360 414e37cb 2022-12-30 thomas fatal("unveil");
2363 3efd8e31 2022-10-23 thomas static void
2364 3efd8e31 2022-10-23 thomas apply_unveil(void)
2366 3efd8e31 2022-10-23 thomas struct gotd_repo *repo;
2368 85b37c72 2022-12-30 thomas if (unveil(gotd.argv0, "x") == -1)
2369 85b37c72 2022-12-30 thomas fatal("unveil %s", gotd.argv0);
2371 3efd8e31 2022-10-23 thomas TAILQ_FOREACH(repo, &gotd.repos, entry) {
2372 3efd8e31 2022-10-23 thomas if (unveil(repo->path, "rwc") == -1)
2373 3efd8e31 2022-10-23 thomas fatal("unveil %s", repo->path);
2376 3efd8e31 2022-10-23 thomas if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
2377 3efd8e31 2022-10-23 thomas fatal("unveil %s", GOT_TMPDIR_STR);
2379 3efd8e31 2022-10-23 thomas if (unveil(NULL, NULL) == -1)
2380 3efd8e31 2022-10-23 thomas fatal("unveil");
2384 3efd8e31 2022-10-23 thomas main(int argc, char **argv)
2386 3efd8e31 2022-10-23 thomas const struct got_error *error = NULL;
2387 3efd8e31 2022-10-23 thomas int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
2388 3efd8e31 2022-10-23 thomas const char *confpath = GOTD_CONF_PATH;
2389 3efd8e31 2022-10-23 thomas char *argv0 = argv[0];
2390 3efd8e31 2022-10-23 thomas char title[2048];
2391 3efd8e31 2022-10-23 thomas struct passwd *pw = NULL;
2392 3efd8e31 2022-10-23 thomas char *repo_path = NULL;
2393 3efd8e31 2022-10-23 thomas enum gotd_procid proc_id = PROC_GOTD;
2394 3efd8e31 2022-10-23 thomas struct event evsigint, evsigterm, evsighup, evsigusr1;
2395 3efd8e31 2022-10-23 thomas int *pack_fds = NULL, *temp_fds = NULL;
2397 3efd8e31 2022-10-23 thomas log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
2399 c669c489 2022-12-30 thomas while ((ch = getopt(argc, argv, "Adf:LnP:RvW")) != -1) {
2400 3efd8e31 2022-10-23 thomas switch (ch) {
2401 c669c489 2022-12-30 thomas case 'A':
2402 c669c489 2022-12-30 thomas proc_id = PROC_AUTH;
2404 3efd8e31 2022-10-23 thomas case 'd':
2405 3efd8e31 2022-10-23 thomas daemonize = 0;
2407 3efd8e31 2022-10-23 thomas case 'f':
2408 3efd8e31 2022-10-23 thomas confpath = optarg;
2410 2b3d32a1 2022-12-30 thomas case 'L':
2411 2b3d32a1 2022-12-30 thomas proc_id = PROC_LISTEN;
2413 3efd8e31 2022-10-23 thomas case 'n':
2414 3efd8e31 2022-10-23 thomas noaction = 1;
2416 f7065961 2022-10-27 thomas case 'P':
2417 f7065961 2022-10-27 thomas repo_path = realpath(optarg, NULL);
2418 f7065961 2022-10-27 thomas if (repo_path == NULL)
2419 f7065961 2022-10-27 thomas fatal("realpath '%s'", optarg);
2421 3efd8e31 2022-10-23 thomas case 'R':
2422 3efd8e31 2022-10-23 thomas proc_id = PROC_REPO_READ;
2424 f7065961 2022-10-27 thomas case 'v':
2425 f7065961 2022-10-27 thomas if (verbosity < 3)
2426 f7065961 2022-10-27 thomas verbosity++;
2428 3efd8e31 2022-10-23 thomas case 'W':
2429 3efd8e31 2022-10-23 thomas proc_id = PROC_REPO_WRITE;
2431 3efd8e31 2022-10-23 thomas default:
2432 3efd8e31 2022-10-23 thomas usage();
2436 3efd8e31 2022-10-23 thomas argc -= optind;
2437 3efd8e31 2022-10-23 thomas argv += optind;
2439 3efd8e31 2022-10-23 thomas if (argc != 0)
2440 3efd8e31 2022-10-23 thomas usage();
2442 85b37c72 2022-12-30 thomas /* Require an absolute path in argv[0] for reliable re-exec. */
2443 85b37c72 2022-12-30 thomas if (!got_path_is_absolute(argv0))
2444 85b37c72 2022-12-30 thomas fatalx("bad path \"%s\": must be an absolute path", argv0);
2446 85b37c72 2022-12-30 thomas if (geteuid() && (proc_id == PROC_GOTD || proc_id == PROC_LISTEN))
2447 3efd8e31 2022-10-23 thomas fatalx("need root privileges");
2449 3efd8e31 2022-10-23 thomas log_init(daemonize ? 0 : 1, LOG_DAEMON);
2450 3efd8e31 2022-10-23 thomas log_setverbose(verbosity);
2452 3efd8e31 2022-10-23 thomas if (parse_config(confpath, proc_id, &gotd) != 0)
2453 3efd8e31 2022-10-23 thomas return 1;
2455 85b37c72 2022-12-30 thomas gotd.argv0 = argv0;
2456 85b37c72 2022-12-30 thomas gotd.daemonize = daemonize;
2457 85b37c72 2022-12-30 thomas gotd.verbosity = verbosity;
2458 85b37c72 2022-12-30 thomas gotd.confpath = confpath;
2460 3efd8e31 2022-10-23 thomas if (proc_id == PROC_GOTD &&
2461 3efd8e31 2022-10-23 thomas (gotd.nrepos == 0 || TAILQ_EMPTY(&gotd.repos)))
2462 3efd8e31 2022-10-23 thomas fatalx("no repository defined in configuration file");
2464 3efd8e31 2022-10-23 thomas pw = getpwnam(gotd.user_name);
2465 3efd8e31 2022-10-23 thomas if (pw == NULL)
2466 3e7c54e1 2022-12-30 thomas fatalx("user %s not found", gotd.user_name);
2468 3efd8e31 2022-10-23 thomas if (pw->pw_uid == 0) {
2469 3efd8e31 2022-10-23 thomas fatalx("cannot run %s as %s: the user running %s "
2470 3efd8e31 2022-10-23 thomas "must not be the superuser",
2471 3efd8e31 2022-10-23 thomas getprogname(), pw->pw_name, getprogname());
2474 2b3d32a1 2022-12-30 thomas if (proc_id == PROC_LISTEN &&
2475 3efd8e31 2022-10-23 thomas !got_path_is_absolute(gotd.unix_socket_path))
2476 3efd8e31 2022-10-23 thomas fatalx("bad unix socket path \"%s\": must be an absolute path",
2477 3efd8e31 2022-10-23 thomas gotd.unix_socket_path);
2479 3efd8e31 2022-10-23 thomas if (noaction)
2480 3efd8e31 2022-10-23 thomas return 0;
2482 1eec6e4e 2022-12-06 thomas if (proc_id == PROC_GOTD) {
2483 2b3d32a1 2022-12-30 thomas gotd.pid = getpid();
2484 2b3d32a1 2022-12-30 thomas snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2485 2b3d32a1 2022-12-30 thomas start_listener(argv0, confpath, daemonize, verbosity);
2486 2b3d32a1 2022-12-30 thomas arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
2487 2b3d32a1 2022-12-30 thomas if (daemonize && daemon(1, 0) == -1)
2488 2b3d32a1 2022-12-30 thomas fatal("daemon");
2489 2b3d32a1 2022-12-30 thomas } else if (proc_id == PROC_LISTEN) {
2490 2b3d32a1 2022-12-30 thomas snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2491 1eec6e4e 2022-12-06 thomas if (verbosity) {
2492 1eec6e4e 2022-12-06 thomas log_info("socket: %s", gotd.unix_socket_path);
2493 1eec6e4e 2022-12-06 thomas log_info("user: %s", pw->pw_name);
2496 3efd8e31 2022-10-23 thomas fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
2497 f2fc8ce0 2023-01-06 thomas pw->pw_gid);
2498 3efd8e31 2022-10-23 thomas if (fd == -1) {
2499 3efd8e31 2022-10-23 thomas fatal("cannot listen on unix socket %s",
2500 3efd8e31 2022-10-23 thomas gotd.unix_socket_path);
2502 414e37cb 2022-12-30 thomas if (daemonize && daemon(0, 0) == -1)
2503 3efd8e31 2022-10-23 thomas fatal("daemon");
2504 c669c489 2022-12-30 thomas } else if (proc_id == PROC_AUTH) {
2505 c669c489 2022-12-30 thomas snprintf(title, sizeof(title), "%s %s",
2506 c669c489 2022-12-30 thomas gotd_proc_names[proc_id], repo_path);
2507 c669c489 2022-12-30 thomas if (daemonize && daemon(0, 0) == -1)
2508 c669c489 2022-12-30 thomas fatal("daemon");
2509 3efd8e31 2022-10-23 thomas } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE) {
2510 3efd8e31 2022-10-23 thomas error = got_repo_pack_fds_open(&pack_fds);
2511 3efd8e31 2022-10-23 thomas if (error != NULL)
2512 3efd8e31 2022-10-23 thomas fatalx("cannot open pack tempfiles: %s", error->msg);
2513 3efd8e31 2022-10-23 thomas error = got_repo_temp_fds_open(&temp_fds);
2514 3efd8e31 2022-10-23 thomas if (error != NULL)
2515 3efd8e31 2022-10-23 thomas fatalx("cannot open pack tempfiles: %s", error->msg);
2516 3efd8e31 2022-10-23 thomas if (repo_path == NULL)
2517 3efd8e31 2022-10-23 thomas fatalx("repository path not specified");
2518 3efd8e31 2022-10-23 thomas snprintf(title, sizeof(title), "%s %s",
2519 3efd8e31 2022-10-23 thomas gotd_proc_names[proc_id], repo_path);
2520 414e37cb 2022-12-30 thomas if (daemonize && daemon(0, 0) == -1)
2521 3efd8e31 2022-10-23 thomas fatal("daemon");
2523 3efd8e31 2022-10-23 thomas fatal("invalid process id %d", proc_id);
2525 3efd8e31 2022-10-23 thomas setproctitle("%s", title);
2526 3efd8e31 2022-10-23 thomas log_procinit(title);
2528 3efd8e31 2022-10-23 thomas /* Drop root privileges. */
2529 3efd8e31 2022-10-23 thomas if (setgid(pw->pw_gid) == -1)
2530 3efd8e31 2022-10-23 thomas fatal("setgid %d failed", pw->pw_gid);
2531 3efd8e31 2022-10-23 thomas if (setuid(pw->pw_uid) == -1)
2532 3efd8e31 2022-10-23 thomas fatal("setuid %d failed", pw->pw_uid);
2534 3efd8e31 2022-10-23 thomas event_init();
2536 3efd8e31 2022-10-23 thomas switch (proc_id) {
2537 3efd8e31 2022-10-23 thomas case PROC_GOTD:
2538 3efd8e31 2022-10-23 thomas #ifndef PROFILE
2539 c669c489 2022-12-30 thomas if (pledge("stdio rpath wpath cpath proc exec "
2540 0bcde4c8 2022-12-30 thomas "sendfd recvfd fattr flock unveil", NULL) == -1)
2541 3efd8e31 2022-10-23 thomas err(1, "pledge");
2544 2b3d32a1 2022-12-30 thomas case PROC_LISTEN:
2545 2b3d32a1 2022-12-30 thomas #ifndef PROFILE
2546 d4940d40 2023-01-06 thomas if (pledge("stdio sendfd unix unveil", NULL) == -1)
2547 2b3d32a1 2022-12-30 thomas err(1, "pledge");
2550 d4940d40 2023-01-06 thomas * Ensure that AF_UNIX bind(2) cannot be used with any other
2551 d4940d40 2023-01-06 thomas * sockets by revoking all filesystem access via unveil(2).
2553 d4940d40 2023-01-06 thomas apply_unveil_none();
2555 0781db0e 2023-01-06 thomas listen_main(title, fd, gotd.connection_limits,
2556 0781db0e 2023-01-06 thomas gotd.nconnection_limits);
2557 2b3d32a1 2022-12-30 thomas /* NOTREACHED */
2559 c669c489 2022-12-30 thomas case PROC_AUTH:
2560 c669c489 2022-12-30 thomas #ifndef PROFILE
2561 b942ab08 2022-12-30 thomas if (pledge("stdio getpw recvfd unix unveil", NULL) == -1)
2562 c669c489 2022-12-30 thomas err(1, "pledge");
2565 b942ab08 2022-12-30 thomas * We need the "unix" pledge promise for getpeername(2) only.
2566 b942ab08 2022-12-30 thomas * Ensure that AF_UNIX bind(2) cannot be used by revoking all
2567 b942ab08 2022-12-30 thomas * filesystem access via unveil(2). Access to password database
2568 b942ab08 2022-12-30 thomas * files will still work since "getpw" bypasses unveil(2).
2570 b942ab08 2022-12-30 thomas apply_unveil_none();
2572 c669c489 2022-12-30 thomas auth_main(title, &gotd.repos, repo_path);
2573 c669c489 2022-12-30 thomas /* NOTREACHED */
2575 3efd8e31 2022-10-23 thomas case PROC_REPO_READ:
2576 3efd8e31 2022-10-23 thomas #ifndef PROFILE
2577 414e37cb 2022-12-30 thomas if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2578 3efd8e31 2022-10-23 thomas err(1, "pledge");
2580 414e37cb 2022-12-30 thomas apply_unveil_repo_readonly(repo_path);
2581 414e37cb 2022-12-30 thomas repo_read_main(title, repo_path, pack_fds, temp_fds);
2582 3efd8e31 2022-10-23 thomas /* NOTREACHED */
2583 3efd8e31 2022-10-23 thomas exit(0);
2584 3efd8e31 2022-10-23 thomas case PROC_REPO_WRITE:
2585 3efd8e31 2022-10-23 thomas #ifndef PROFILE
2586 414e37cb 2022-12-30 thomas if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2587 3efd8e31 2022-10-23 thomas err(1, "pledge");
2589 414e37cb 2022-12-30 thomas apply_unveil_repo_readonly(repo_path);
2590 414e37cb 2022-12-30 thomas repo_write_main(title, repo_path, pack_fds, temp_fds);
2591 3efd8e31 2022-10-23 thomas /* NOTREACHED */
2592 3efd8e31 2022-10-23 thomas exit(0);
2593 3efd8e31 2022-10-23 thomas default:
2594 3efd8e31 2022-10-23 thomas fatal("invalid process id %d", proc_id);
2597 3efd8e31 2022-10-23 thomas if (proc_id != PROC_GOTD)
2598 3efd8e31 2022-10-23 thomas fatal("invalid process id %d", proc_id);
2600 3efd8e31 2022-10-23 thomas apply_unveil();
2602 3efd8e31 2022-10-23 thomas signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
2603 3efd8e31 2022-10-23 thomas signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
2604 3efd8e31 2022-10-23 thomas signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
2605 3efd8e31 2022-10-23 thomas signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
2606 3efd8e31 2022-10-23 thomas signal(SIGPIPE, SIG_IGN);
2608 3efd8e31 2022-10-23 thomas signal_add(&evsigint, NULL);
2609 3efd8e31 2022-10-23 thomas signal_add(&evsigterm, NULL);
2610 3efd8e31 2022-10-23 thomas signal_add(&evsighup, NULL);
2611 3efd8e31 2022-10-23 thomas signal_add(&evsigusr1, NULL);
2613 85b37c72 2022-12-30 thomas gotd_imsg_event_add(&gotd.listen_proc.iev);
2615 3efd8e31 2022-10-23 thomas event_dispatch();
2617 3efd8e31 2022-10-23 thomas if (pack_fds)
2618 3efd8e31 2022-10-23 thomas got_repo_pack_fds_close(pack_fds);
2619 3efd8e31 2022-10-23 thomas free(repo_path);
2620 3efd8e31 2022-10-23 thomas return 0;