2 13b2bc37 2022-10-23 stsp * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
4 13b2bc37 2022-10-23 stsp * Permission to use, copy, modify, and distribute this software for any
5 13b2bc37 2022-10-23 stsp * purpose with or without fee is hereby granted, provided that the above
6 13b2bc37 2022-10-23 stsp * copyright notice and this permission notice appear in all copies.
8 13b2bc37 2022-10-23 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 13b2bc37 2022-10-23 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 13b2bc37 2022-10-23 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 13b2bc37 2022-10-23 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 13b2bc37 2022-10-23 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 13b2bc37 2022-10-23 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 13b2bc37 2022-10-23 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 13b2bc37 2022-10-23 stsp #include <sys/queue.h>
18 13b2bc37 2022-10-23 stsp #include <sys/tree.h>
19 13b2bc37 2022-10-23 stsp #include <sys/time.h>
20 13b2bc37 2022-10-23 stsp #include <sys/types.h>
21 13b2bc37 2022-10-23 stsp #include <sys/stat.h>
22 13b2bc37 2022-10-23 stsp #include <sys/socket.h>
23 13b2bc37 2022-10-23 stsp #include <sys/un.h>
24 13b2bc37 2022-10-23 stsp #include <sys/wait.h>
26 13b2bc37 2022-10-23 stsp #include <fcntl.h>
27 13b2bc37 2022-10-23 stsp #include <err.h>
28 13b2bc37 2022-10-23 stsp #include <errno.h>
29 13b2bc37 2022-10-23 stsp #include <event.h>
30 13b2bc37 2022-10-23 stsp #include <limits.h>
31 13b2bc37 2022-10-23 stsp #include <pwd.h>
32 13b2bc37 2022-10-23 stsp #include <grp.h>
33 13b2bc37 2022-10-23 stsp #include <imsg.h>
34 13b2bc37 2022-10-23 stsp #include <sha1.h>
35 13b2bc37 2022-10-23 stsp #include <signal.h>
36 13b2bc37 2022-10-23 stsp #include <siphash.h>
37 13b2bc37 2022-10-23 stsp #include <stdarg.h>
38 13b2bc37 2022-10-23 stsp #include <stdio.h>
39 13b2bc37 2022-10-23 stsp #include <stdlib.h>
40 13b2bc37 2022-10-23 stsp #include <string.h>
41 13b2bc37 2022-10-23 stsp #include <syslog.h>
42 13b2bc37 2022-10-23 stsp #include <unistd.h>
44 13b2bc37 2022-10-23 stsp #include "got_error.h"
45 13b2bc37 2022-10-23 stsp #include "got_opentemp.h"
46 13b2bc37 2022-10-23 stsp #include "got_path.h"
47 13b2bc37 2022-10-23 stsp #include "got_repository.h"
48 13b2bc37 2022-10-23 stsp #include "got_object.h"
49 13b2bc37 2022-10-23 stsp #include "got_reference.h"
51 13b2bc37 2022-10-23 stsp #include "got_lib_delta.h"
52 13b2bc37 2022-10-23 stsp #include "got_lib_object.h"
53 13b2bc37 2022-10-23 stsp #include "got_lib_object_cache.h"
54 13b2bc37 2022-10-23 stsp #include "got_lib_sha1.h"
55 13b2bc37 2022-10-23 stsp #include "got_lib_gitproto.h"
56 13b2bc37 2022-10-23 stsp #include "got_lib_pack.h"
57 13b2bc37 2022-10-23 stsp #include "got_lib_repository.h"
59 13b2bc37 2022-10-23 stsp #include "gotd.h"
60 13b2bc37 2022-10-23 stsp #include "log.h"
61 d93ecf7d 2022-12-14 stsp #include "listen.h"
62 0ccf3acb 2022-11-16 stsp #include "auth.h"
63 13b2bc37 2022-10-23 stsp #include "repo_read.h"
64 13b2bc37 2022-10-23 stsp #include "repo_write.h"
66 13b2bc37 2022-10-23 stsp #ifndef nitems
67 13b2bc37 2022-10-23 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
70 13b2bc37 2022-10-23 stsp struct gotd_client {
71 13b2bc37 2022-10-23 stsp STAILQ_ENTRY(gotd_client) entry;
72 13b2bc37 2022-10-23 stsp enum gotd_client_state state;
73 13b2bc37 2022-10-23 stsp struct gotd_client_capability *capabilities;
74 13b2bc37 2022-10-23 stsp size_t ncapa_alloc;
75 13b2bc37 2022-10-23 stsp size_t ncapabilities;
76 13b2bc37 2022-10-23 stsp uint32_t id;
78 13b2bc37 2022-10-23 stsp int delta_cache_fd;
79 13b2bc37 2022-10-23 stsp struct gotd_imsgev iev;
80 13b2bc37 2022-10-23 stsp struct event tmo;
83 13b2bc37 2022-10-23 stsp struct gotd_child_proc *repo_read;
84 13b2bc37 2022-10-23 stsp struct gotd_child_proc *repo_write;
85 13b2bc37 2022-10-23 stsp char *packfile_path;
86 13b2bc37 2022-10-23 stsp char *packidx_path;
87 13b2bc37 2022-10-23 stsp int nref_updates;
89 13b2bc37 2022-10-23 stsp STAILQ_HEAD(gotd_clients, gotd_client);
91 13b2bc37 2022-10-23 stsp static struct gotd_clients gotd_clients[GOTD_CLIENT_TABLE_SIZE];
92 13b2bc37 2022-10-23 stsp static SIPHASH_KEY clients_hash_key;
93 13b2bc37 2022-10-23 stsp volatile int client_cnt;
94 13b2bc37 2022-10-23 stsp static struct timeval timeout = { 3600, 0 };
95 13b2bc37 2022-10-23 stsp static struct gotd gotd;
97 13b2bc37 2022-10-23 stsp void gotd_sighdlr(int sig, short event, void *arg);
98 f1752522 2022-10-29 stsp static void gotd_shutdown(void);
100 13b2bc37 2022-10-23 stsp __dead static void
103 88dec179 2022-10-24 stsp fprintf(stderr, "usage: %s [-dv] [-f config-file]\n", getprogname());
108 13b2bc37 2022-10-23 stsp unix_socket_listen(const char *unix_socket_path, uid_t uid, gid_t gid)
110 13b2bc37 2022-10-23 stsp struct sockaddr_un sun;
111 13b2bc37 2022-10-23 stsp int fd = -1;
112 13b2bc37 2022-10-23 stsp mode_t old_umask, mode;
114 13b2bc37 2022-10-23 stsp fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
115 13b2bc37 2022-10-23 stsp if (fd == -1) {
116 13b2bc37 2022-10-23 stsp log_warn("socket");
120 13b2bc37 2022-10-23 stsp sun.sun_family = AF_UNIX;
121 13b2bc37 2022-10-23 stsp if (strlcpy(sun.sun_path, unix_socket_path,
122 13b2bc37 2022-10-23 stsp sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
123 13b2bc37 2022-10-23 stsp log_warnx("%s: name too long", unix_socket_path);
128 13b2bc37 2022-10-23 stsp if (unlink(unix_socket_path) == -1) {
129 13b2bc37 2022-10-23 stsp if (errno != ENOENT) {
130 13b2bc37 2022-10-23 stsp log_warn("unlink %s", unix_socket_path);
136 13b2bc37 2022-10-23 stsp old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
137 13b2bc37 2022-10-23 stsp mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
139 13b2bc37 2022-10-23 stsp if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
140 13b2bc37 2022-10-23 stsp log_warn("bind: %s", unix_socket_path);
142 13b2bc37 2022-10-23 stsp umask(old_umask);
146 13b2bc37 2022-10-23 stsp umask(old_umask);
148 13b2bc37 2022-10-23 stsp if (chmod(unix_socket_path, mode) == -1) {
149 13b2bc37 2022-10-23 stsp log_warn("chmod %o %s", mode, unix_socket_path);
151 13b2bc37 2022-10-23 stsp unlink(unix_socket_path);
155 13b2bc37 2022-10-23 stsp if (chown(unix_socket_path, uid, gid) == -1) {
156 13b2bc37 2022-10-23 stsp log_warn("chown %s uid=%d gid=%d", unix_socket_path, uid, gid);
158 13b2bc37 2022-10-23 stsp unlink(unix_socket_path);
162 13b2bc37 2022-10-23 stsp if (listen(fd, GOTD_UNIX_SOCKET_BACKLOG) == -1) {
163 13b2bc37 2022-10-23 stsp log_warn("listen");
165 13b2bc37 2022-10-23 stsp unlink(unix_socket_path);
172 13b2bc37 2022-10-23 stsp static struct group *
173 13b2bc37 2022-10-23 stsp match_group(gid_t *groups, int ngroups, const char *unix_group_name)
175 13b2bc37 2022-10-23 stsp struct group *gr;
178 13b2bc37 2022-10-23 stsp for (i = 0; i < ngroups; i++) {
179 13b2bc37 2022-10-23 stsp gr = getgrgid(groups[i]);
180 13b2bc37 2022-10-23 stsp if (gr == NULL) {
181 13b2bc37 2022-10-23 stsp log_warn("getgrgid %d", groups[i]);
184 13b2bc37 2022-10-23 stsp if (strcmp(gr->gr_name, unix_group_name) == 0)
188 13b2bc37 2022-10-23 stsp return NULL;
191 13b2bc37 2022-10-23 stsp static uint64_t
192 13b2bc37 2022-10-23 stsp client_hash(uint32_t client_id)
194 13b2bc37 2022-10-23 stsp return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
197 13b2bc37 2022-10-23 stsp static void
198 13b2bc37 2022-10-23 stsp add_client(struct gotd_client *client)
200 13b2bc37 2022-10-23 stsp uint64_t slot = client_hash(client->id) % nitems(gotd_clients);
201 13b2bc37 2022-10-23 stsp STAILQ_INSERT_HEAD(&gotd_clients[slot], client, entry);
202 13b2bc37 2022-10-23 stsp client_cnt++;
205 13b2bc37 2022-10-23 stsp static struct gotd_client *
206 13b2bc37 2022-10-23 stsp find_client(uint32_t client_id)
208 13b2bc37 2022-10-23 stsp uint64_t slot;
209 13b2bc37 2022-10-23 stsp struct gotd_client *c;
211 13b2bc37 2022-10-23 stsp slot = client_hash(client_id) % nitems(gotd_clients);
212 13b2bc37 2022-10-23 stsp STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
213 13b2bc37 2022-10-23 stsp if (c->id == client_id)
217 13b2bc37 2022-10-23 stsp return NULL;
220 13b2bc37 2022-10-23 stsp static struct gotd_child_proc *
221 13b2bc37 2022-10-23 stsp get_client_proc(struct gotd_client *client)
223 13b2bc37 2022-10-23 stsp if (client->repo_read && client->repo_write) {
224 13b2bc37 2022-10-23 stsp fatalx("uid %d is reading and writing in the same session",
225 13b2bc37 2022-10-23 stsp client->euid);
226 13b2bc37 2022-10-23 stsp /* NOTREACHED */
229 13b2bc37 2022-10-23 stsp if (client->repo_read)
230 13b2bc37 2022-10-23 stsp return client->repo_read;
231 13b2bc37 2022-10-23 stsp else if (client->repo_write)
232 13b2bc37 2022-10-23 stsp return client->repo_write;
234 13b2bc37 2022-10-23 stsp return NULL;
238 13b2bc37 2022-10-23 stsp client_is_reading(struct gotd_client *client)
240 13b2bc37 2022-10-23 stsp return client->repo_read != NULL;
244 13b2bc37 2022-10-23 stsp client_is_writing(struct gotd_client *client)
246 13b2bc37 2022-10-23 stsp return client->repo_write != NULL;
249 13b2bc37 2022-10-23 stsp static const struct got_error *
250 13b2bc37 2022-10-23 stsp ensure_client_is_reading(struct gotd_client *client)
252 13b2bc37 2022-10-23 stsp if (!client_is_reading(client)) {
253 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_BAD_PACKET,
254 13b2bc37 2022-10-23 stsp "uid %d made a read-request but is not reading from "
255 13b2bc37 2022-10-23 stsp "a repository", client->euid);
258 13b2bc37 2022-10-23 stsp return NULL;
261 13b2bc37 2022-10-23 stsp static const struct got_error *
262 13b2bc37 2022-10-23 stsp ensure_client_is_writing(struct gotd_client *client)
264 13b2bc37 2022-10-23 stsp if (!client_is_writing(client)) {
265 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_BAD_PACKET,
266 13b2bc37 2022-10-23 stsp "uid %d made a write-request but is not writing to "
267 13b2bc37 2022-10-23 stsp "a repository", client->euid);
270 13b2bc37 2022-10-23 stsp return NULL;
273 13b2bc37 2022-10-23 stsp static const struct got_error *
274 13b2bc37 2022-10-23 stsp ensure_client_is_not_writing(struct gotd_client *client)
276 13b2bc37 2022-10-23 stsp if (client_is_writing(client)) {
277 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_BAD_PACKET,
278 13b2bc37 2022-10-23 stsp "uid %d made a read-request but is writing to "
279 13b2bc37 2022-10-23 stsp "a repository", client->euid);
282 13b2bc37 2022-10-23 stsp return NULL;
285 13b2bc37 2022-10-23 stsp static const struct got_error *
286 13b2bc37 2022-10-23 stsp ensure_client_is_not_reading(struct gotd_client *client)
288 13b2bc37 2022-10-23 stsp if (client_is_reading(client)) {
289 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_BAD_PACKET,
290 13b2bc37 2022-10-23 stsp "uid %d made a write-request but is reading from "
291 13b2bc37 2022-10-23 stsp "a repository", client->euid);
294 13b2bc37 2022-10-23 stsp return NULL;
297 13b2bc37 2022-10-23 stsp static void
298 13b2bc37 2022-10-23 stsp disconnect(struct gotd_client *client)
300 13b2bc37 2022-10-23 stsp struct gotd_imsg_disconnect idisconnect;
301 13b2bc37 2022-10-23 stsp struct gotd_child_proc *proc = get_client_proc(client);
302 d93ecf7d 2022-12-14 stsp struct gotd_child_proc *listen_proc = &gotd.procs[0];
303 13b2bc37 2022-10-23 stsp uint64_t slot;
305 13b2bc37 2022-10-23 stsp log_debug("uid %d: disconnecting", client->euid);
307 13b2bc37 2022-10-23 stsp idisconnect.client_id = client->id;
308 f1752522 2022-10-29 stsp if (proc) {
309 f1752522 2022-10-29 stsp if (gotd_imsg_compose_event(&proc->iev,
310 f1752522 2022-10-29 stsp GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
311 f1752522 2022-10-29 stsp &idisconnect, sizeof(idisconnect)) == -1)
312 f1752522 2022-10-29 stsp log_warn("imsg compose DISCONNECT");
315 d93ecf7d 2022-12-14 stsp if (gotd_imsg_compose_event(&listen_proc->iev,
316 d93ecf7d 2022-12-14 stsp GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
317 d93ecf7d 2022-12-14 stsp &idisconnect, sizeof(idisconnect)) == -1)
318 d93ecf7d 2022-12-14 stsp log_warn("imsg compose DISCONNECT");
320 13b2bc37 2022-10-23 stsp slot = client_hash(client->id) % nitems(gotd_clients);
321 13b2bc37 2022-10-23 stsp STAILQ_REMOVE(&gotd_clients[slot], client, gotd_client, entry);
322 13b2bc37 2022-10-23 stsp imsg_clear(&client->iev.ibuf);
323 13b2bc37 2022-10-23 stsp event_del(&client->iev.ev);
324 13b2bc37 2022-10-23 stsp evtimer_del(&client->tmo);
325 13b2bc37 2022-10-23 stsp close(client->fd);
326 13b2bc37 2022-10-23 stsp if (client->delta_cache_fd != -1)
327 13b2bc37 2022-10-23 stsp close(client->delta_cache_fd);
328 13b2bc37 2022-10-23 stsp if (client->packfile_path) {
329 13b2bc37 2022-10-23 stsp if (unlink(client->packfile_path) == -1 && errno != ENOENT)
330 13b2bc37 2022-10-23 stsp log_warn("unlink %s: ", client->packfile_path);
331 13b2bc37 2022-10-23 stsp free(client->packfile_path);
333 13b2bc37 2022-10-23 stsp if (client->packidx_path) {
334 13b2bc37 2022-10-23 stsp if (unlink(client->packidx_path) == -1 && errno != ENOENT)
335 13b2bc37 2022-10-23 stsp log_warn("unlink %s: ", client->packidx_path);
336 13b2bc37 2022-10-23 stsp free(client->packidx_path);
338 13b2bc37 2022-10-23 stsp free(client->capabilities);
339 13b2bc37 2022-10-23 stsp free(client);
340 13b2bc37 2022-10-23 stsp client_cnt--;
343 13b2bc37 2022-10-23 stsp static void
344 13b2bc37 2022-10-23 stsp disconnect_on_error(struct gotd_client *client, const struct got_error *err)
346 13b2bc37 2022-10-23 stsp struct imsgbuf ibuf;
348 13b2bc37 2022-10-23 stsp log_warnx("uid %d: %s", client->euid, err->msg);
349 13b2bc37 2022-10-23 stsp if (err->code != GOT_ERR_EOF) {
350 13b2bc37 2022-10-23 stsp imsg_init(&ibuf, client->fd);
351 13b2bc37 2022-10-23 stsp gotd_imsg_send_error(&ibuf, 0, PROC_GOTD, err);
352 13b2bc37 2022-10-23 stsp imsg_clear(&ibuf);
354 13b2bc37 2022-10-23 stsp disconnect(client);
357 f1752522 2022-10-29 stsp static const struct got_error *
358 f1752522 2022-10-29 stsp send_repo_info(struct gotd_imsgev *iev, struct gotd_repo *repo)
360 f1752522 2022-10-29 stsp const struct got_error *err = NULL;
361 f1752522 2022-10-29 stsp struct gotd_imsg_info_repo irepo;
363 f1752522 2022-10-29 stsp memset(&irepo, 0, sizeof(irepo));
365 f1752522 2022-10-29 stsp if (strlcpy(irepo.repo_name, repo->name, sizeof(irepo.repo_name))
366 f1752522 2022-10-29 stsp >= sizeof(irepo.repo_name))
367 f1752522 2022-10-29 stsp return got_error_msg(GOT_ERR_NO_SPACE, "repo name too long");
368 f1752522 2022-10-29 stsp if (strlcpy(irepo.repo_path, repo->path, sizeof(irepo.repo_path))
369 f1752522 2022-10-29 stsp >= sizeof(irepo.repo_path))
370 f1752522 2022-10-29 stsp return got_error_msg(GOT_ERR_NO_SPACE, "repo path too long");
372 f1752522 2022-10-29 stsp if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_REPO, PROC_GOTD, -1,
373 f1752522 2022-10-29 stsp &irepo, sizeof(irepo)) == -1) {
374 f1752522 2022-10-29 stsp err = got_error_from_errno("imsg compose INFO_REPO");
376 f1752522 2022-10-29 stsp return err;
379 f1752522 2022-10-29 stsp return NULL;
382 f1752522 2022-10-29 stsp static const struct got_error *
383 f1752522 2022-10-29 stsp send_capability(struct gotd_client_capability *capa, struct gotd_imsgev* iev)
385 f1752522 2022-10-29 stsp const struct got_error *err = NULL;
386 f1752522 2022-10-29 stsp struct gotd_imsg_capability icapa;
387 f1752522 2022-10-29 stsp size_t len;
388 f1752522 2022-10-29 stsp struct ibuf *wbuf;
390 f1752522 2022-10-29 stsp memset(&icapa, 0, sizeof(icapa));
392 f1752522 2022-10-29 stsp icapa.key_len = strlen(capa->key);
393 f1752522 2022-10-29 stsp len = sizeof(icapa) + icapa.key_len;
394 f1752522 2022-10-29 stsp if (capa->value) {
395 f1752522 2022-10-29 stsp icapa.value_len = strlen(capa->value);
396 f1752522 2022-10-29 stsp len += icapa.value_len;
399 f1752522 2022-10-29 stsp wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_CAPABILITY, 0, 0, len);
400 f1752522 2022-10-29 stsp if (wbuf == NULL) {
401 f1752522 2022-10-29 stsp err = got_error_from_errno("imsg_create CAPABILITY");
402 f1752522 2022-10-29 stsp return err;
405 f1752522 2022-10-29 stsp if (imsg_add(wbuf, &icapa, sizeof(icapa)) == -1)
406 f1752522 2022-10-29 stsp return got_error_from_errno("imsg_add CAPABILITY");
407 f1752522 2022-10-29 stsp if (imsg_add(wbuf, capa->key, icapa.key_len) == -1)
408 f1752522 2022-10-29 stsp return got_error_from_errno("imsg_add CAPABILITY");
409 f1752522 2022-10-29 stsp if (capa->value) {
410 f1752522 2022-10-29 stsp if (imsg_add(wbuf, capa->value, icapa.value_len) == -1)
411 f1752522 2022-10-29 stsp return got_error_from_errno("imsg_add CAPABILITY");
414 f1752522 2022-10-29 stsp wbuf->fd = -1;
415 f1752522 2022-10-29 stsp imsg_close(&iev->ibuf, wbuf);
417 f1752522 2022-10-29 stsp gotd_imsg_event_add(iev);
419 f1752522 2022-10-29 stsp return NULL;
422 f1752522 2022-10-29 stsp static const struct got_error *
423 f1752522 2022-10-29 stsp send_client_info(struct gotd_imsgev *iev, struct gotd_client *client)
425 f1752522 2022-10-29 stsp const struct got_error *err = NULL;
426 f1752522 2022-10-29 stsp struct gotd_imsg_info_client iclient;
427 f1752522 2022-10-29 stsp struct gotd_child_proc *proc;
430 f1752522 2022-10-29 stsp memset(&iclient, 0, sizeof(iclient));
431 f1752522 2022-10-29 stsp iclient.euid = client->euid;
432 f1752522 2022-10-29 stsp iclient.egid = client->egid;
434 f1752522 2022-10-29 stsp proc = get_client_proc(client);
435 f1752522 2022-10-29 stsp if (proc) {
436 eec68231 2022-12-14 stsp if (strlcpy(iclient.repo_name, proc->repo_path,
437 f1752522 2022-10-29 stsp sizeof(iclient.repo_name)) >= sizeof(iclient.repo_name)) {
438 f1752522 2022-10-29 stsp return got_error_msg(GOT_ERR_NO_SPACE,
439 f1752522 2022-10-29 stsp "repo name too long");
441 f1752522 2022-10-29 stsp if (client_is_writing(client))
442 f1752522 2022-10-29 stsp iclient.is_writing = 1;
445 f1752522 2022-10-29 stsp iclient.state = client->state;
446 f1752522 2022-10-29 stsp iclient.ncapabilities = client->ncapabilities;
448 f1752522 2022-10-29 stsp if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_CLIENT, PROC_GOTD, -1,
449 f1752522 2022-10-29 stsp &iclient, sizeof(iclient)) == -1) {
450 f1752522 2022-10-29 stsp err = got_error_from_errno("imsg compose INFO_CLIENT");
452 f1752522 2022-10-29 stsp return err;
455 f1752522 2022-10-29 stsp for (i = 0; i < client->ncapabilities; i++) {
456 f1752522 2022-10-29 stsp struct gotd_client_capability *capa;
457 f1752522 2022-10-29 stsp capa = &client->capabilities[i];
458 f1752522 2022-10-29 stsp err = send_capability(capa, iev);
460 f1752522 2022-10-29 stsp return err;
463 f1752522 2022-10-29 stsp return NULL;
466 f1752522 2022-10-29 stsp static const struct got_error *
467 f1752522 2022-10-29 stsp send_info(struct gotd_client *client)
469 f1752522 2022-10-29 stsp const struct got_error *err = NULL;
470 f1752522 2022-10-29 stsp struct gotd_imsg_info info;
471 f1752522 2022-10-29 stsp uint64_t slot;
472 f1752522 2022-10-29 stsp struct gotd_repo *repo;
474 f1752522 2022-10-29 stsp info.pid = gotd.pid;
475 f1752522 2022-10-29 stsp info.verbosity = gotd.verbosity;
476 f1752522 2022-10-29 stsp info.nrepos = gotd.nrepos;
477 f1752522 2022-10-29 stsp info.nclients = client_cnt - 1;
479 f1752522 2022-10-29 stsp if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_INFO, PROC_GOTD, -1,
480 f1752522 2022-10-29 stsp &info, sizeof(info)) == -1) {
481 f1752522 2022-10-29 stsp err = got_error_from_errno("imsg compose INFO");
483 f1752522 2022-10-29 stsp return err;
486 f1752522 2022-10-29 stsp TAILQ_FOREACH(repo, &gotd.repos, entry) {
487 f1752522 2022-10-29 stsp err = send_repo_info(&client->iev, repo);
489 f1752522 2022-10-29 stsp return err;
492 f1752522 2022-10-29 stsp for (slot = 0; slot < nitems(gotd_clients); slot++) {
493 f1752522 2022-10-29 stsp struct gotd_client *c;
494 f1752522 2022-10-29 stsp STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
495 f1752522 2022-10-29 stsp if (c->id == client->id)
497 f1752522 2022-10-29 stsp err = send_client_info(&client->iev, c);
499 f1752522 2022-10-29 stsp return err;
503 f1752522 2022-10-29 stsp return NULL;
506 f1752522 2022-10-29 stsp static const struct got_error *
507 f1752522 2022-10-29 stsp stop_gotd(struct gotd_client *client)
510 f1752522 2022-10-29 stsp if (client->euid != 0)
511 f1752522 2022-10-29 stsp return got_error_set_errno(EPERM, "stop");
513 f1752522 2022-10-29 stsp gotd_shutdown();
514 f1752522 2022-10-29 stsp /* NOTREACHED */
515 0ccf3acb 2022-11-16 stsp return NULL;
518 0ccf3acb 2022-11-16 stsp static struct gotd_repo *
519 0ccf3acb 2022-11-16 stsp find_repo_by_name(const char *repo_name)
521 0ccf3acb 2022-11-16 stsp struct gotd_repo *repo;
522 0ccf3acb 2022-11-16 stsp size_t namelen;
524 0ccf3acb 2022-11-16 stsp TAILQ_FOREACH(repo, &gotd.repos, entry) {
525 0ccf3acb 2022-11-16 stsp namelen = strlen(repo->name);
526 0ccf3acb 2022-11-16 stsp if (strncmp(repo->name, repo_name, namelen) != 0)
528 0ccf3acb 2022-11-16 stsp if (repo_name[namelen] == '\0' ||
529 0ccf3acb 2022-11-16 stsp strcmp(&repo_name[namelen], ".git") == 0)
530 0ccf3acb 2022-11-16 stsp return repo;
533 f1752522 2022-10-29 stsp return NULL;
536 13b2bc37 2022-10-23 stsp static struct gotd_child_proc *
537 13b2bc37 2022-10-23 stsp find_proc_by_repo_name(enum gotd_procid proc_id, const char *repo_name)
539 13b2bc37 2022-10-23 stsp struct gotd_child_proc *proc;
541 13b2bc37 2022-10-23 stsp size_t namelen;
543 13b2bc37 2022-10-23 stsp for (i = 0; i < gotd.nprocs; i++) {
544 13b2bc37 2022-10-23 stsp proc = &gotd.procs[i];
545 13b2bc37 2022-10-23 stsp if (proc->type != proc_id)
547 13b2bc37 2022-10-23 stsp namelen = strlen(proc->repo_name);
548 13b2bc37 2022-10-23 stsp if (strncmp(proc->repo_name, repo_name, namelen) != 0)
550 13b2bc37 2022-10-23 stsp if (repo_name[namelen] == '\0' ||
551 13b2bc37 2022-10-23 stsp strcmp(&repo_name[namelen], ".git") == 0)
552 13b2bc37 2022-10-23 stsp return proc;
555 13b2bc37 2022-10-23 stsp return NULL;
558 13b2bc37 2022-10-23 stsp static struct gotd_child_proc *
559 13b2bc37 2022-10-23 stsp find_proc_by_fd(int fd)
561 13b2bc37 2022-10-23 stsp struct gotd_child_proc *proc;
564 13b2bc37 2022-10-23 stsp for (i = 0; i < gotd.nprocs; i++) {
565 13b2bc37 2022-10-23 stsp proc = &gotd.procs[i];
566 13b2bc37 2022-10-23 stsp if (proc->iev.ibuf.fd == fd)
567 13b2bc37 2022-10-23 stsp return proc;
570 13b2bc37 2022-10-23 stsp return NULL;
573 13b2bc37 2022-10-23 stsp static const struct got_error *
574 13b2bc37 2022-10-23 stsp forward_list_refs_request(struct gotd_client *client, struct imsg *imsg)
576 13b2bc37 2022-10-23 stsp const struct got_error *err;
577 13b2bc37 2022-10-23 stsp struct gotd_imsg_list_refs ireq;
578 13b2bc37 2022-10-23 stsp struct gotd_imsg_list_refs_internal ilref;
579 0ccf3acb 2022-11-16 stsp struct gotd_repo *repo = NULL;
580 13b2bc37 2022-10-23 stsp struct gotd_child_proc *proc = NULL;
581 13b2bc37 2022-10-23 stsp size_t datalen;
582 13b2bc37 2022-10-23 stsp int fd = -1;
584 13b2bc37 2022-10-23 stsp log_debug("list-refs request from uid %d", client->euid);
586 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
587 13b2bc37 2022-10-23 stsp if (datalen != sizeof(ireq))
588 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
590 13b2bc37 2022-10-23 stsp memcpy(&ireq, imsg->data, datalen);
592 13b2bc37 2022-10-23 stsp memset(&ilref, 0, sizeof(ilref));
593 13b2bc37 2022-10-23 stsp ilref.client_id = client->id;
595 13b2bc37 2022-10-23 stsp if (ireq.client_is_reading) {
596 13b2bc37 2022-10-23 stsp err = ensure_client_is_not_writing(client);
598 13b2bc37 2022-10-23 stsp return err;
599 0ccf3acb 2022-11-16 stsp repo = find_repo_by_name(ireq.repo_name);
600 0ccf3acb 2022-11-16 stsp if (repo == NULL)
601 0ccf3acb 2022-11-16 stsp return got_error(GOT_ERR_NOT_GIT_REPO);
602 0ccf3acb 2022-11-16 stsp err = gotd_auth_check(&repo->rules, repo->name,
603 ddbe612c 2022-11-17 stsp client->euid, client->egid, GOTD_AUTH_READ);
605 0ccf3acb 2022-11-16 stsp return err;
606 13b2bc37 2022-10-23 stsp client->repo_read = find_proc_by_repo_name(PROC_REPO_READ,
607 13b2bc37 2022-10-23 stsp ireq.repo_name);
608 13b2bc37 2022-10-23 stsp if (client->repo_read == NULL)
609 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_NOT_GIT_REPO);
611 13b2bc37 2022-10-23 stsp err = ensure_client_is_not_reading(client);
613 0ccf3acb 2022-11-16 stsp return err;
614 0ccf3acb 2022-11-16 stsp repo = find_repo_by_name(ireq.repo_name);
615 0ccf3acb 2022-11-16 stsp if (repo == NULL)
616 0ccf3acb 2022-11-16 stsp return got_error(GOT_ERR_NOT_GIT_REPO);
617 ddbe612c 2022-11-17 stsp err = gotd_auth_check(&repo->rules, repo->name, client->euid,
618 ddbe612c 2022-11-17 stsp client->egid, GOTD_AUTH_READ | GOTD_AUTH_WRITE);
620 13b2bc37 2022-10-23 stsp return err;
621 13b2bc37 2022-10-23 stsp client->repo_write = find_proc_by_repo_name(PROC_REPO_WRITE,
622 13b2bc37 2022-10-23 stsp ireq.repo_name);
623 13b2bc37 2022-10-23 stsp if (client->repo_write == NULL)
624 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_NOT_GIT_REPO);
627 13b2bc37 2022-10-23 stsp fd = dup(client->fd);
628 13b2bc37 2022-10-23 stsp if (fd == -1)
629 13b2bc37 2022-10-23 stsp return got_error_from_errno("dup");
631 13b2bc37 2022-10-23 stsp proc = get_client_proc(client);
632 f1752522 2022-10-29 stsp if (proc == NULL)
633 f1752522 2022-10-29 stsp fatalx("no process found for uid %d", client->euid);
634 13b2bc37 2022-10-23 stsp if (gotd_imsg_compose_event(&proc->iev,
635 13b2bc37 2022-10-23 stsp GOTD_IMSG_LIST_REFS_INTERNAL, PROC_GOTD, fd,
636 13b2bc37 2022-10-23 stsp &ilref, sizeof(ilref)) == -1) {
637 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg compose WANT");
639 13b2bc37 2022-10-23 stsp return err;
642 13b2bc37 2022-10-23 stsp return NULL;
645 13b2bc37 2022-10-23 stsp static const struct got_error *
646 13b2bc37 2022-10-23 stsp forward_want(struct gotd_client *client, struct imsg *imsg)
648 13b2bc37 2022-10-23 stsp struct gotd_imsg_want ireq;
649 13b2bc37 2022-10-23 stsp struct gotd_imsg_want iwant;
650 13b2bc37 2022-10-23 stsp size_t datalen;
652 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
653 13b2bc37 2022-10-23 stsp if (datalen != sizeof(ireq))
654 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
656 13b2bc37 2022-10-23 stsp memcpy(&ireq, imsg->data, datalen);
658 13b2bc37 2022-10-23 stsp memset(&iwant, 0, sizeof(iwant));
659 13b2bc37 2022-10-23 stsp memcpy(iwant.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
660 13b2bc37 2022-10-23 stsp iwant.client_id = client->id;
662 13b2bc37 2022-10-23 stsp if (gotd_imsg_compose_event(&client->repo_read->iev, GOTD_IMSG_WANT,
663 13b2bc37 2022-10-23 stsp PROC_GOTD, -1, &iwant, sizeof(iwant)) == -1)
664 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg compose WANT");
666 13b2bc37 2022-10-23 stsp return NULL;
669 13b2bc37 2022-10-23 stsp static const struct got_error *
670 13b2bc37 2022-10-23 stsp forward_ref_update(struct gotd_client *client, struct imsg *imsg)
672 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
673 13b2bc37 2022-10-23 stsp struct gotd_imsg_ref_update ireq;
674 13b2bc37 2022-10-23 stsp struct gotd_imsg_ref_update *iref = NULL;
675 13b2bc37 2022-10-23 stsp size_t datalen;
677 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
678 13b2bc37 2022-10-23 stsp if (datalen < sizeof(ireq))
679 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
680 13b2bc37 2022-10-23 stsp memcpy(&ireq, imsg->data, sizeof(ireq));
681 13b2bc37 2022-10-23 stsp if (datalen != sizeof(ireq) + ireq.name_len)
682 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
684 13b2bc37 2022-10-23 stsp iref = malloc(datalen);
685 13b2bc37 2022-10-23 stsp if (iref == NULL)
686 13b2bc37 2022-10-23 stsp return got_error_from_errno("malloc");
687 13b2bc37 2022-10-23 stsp memcpy(iref, imsg->data, datalen);
689 13b2bc37 2022-10-23 stsp iref->client_id = client->id;
690 13b2bc37 2022-10-23 stsp if (gotd_imsg_compose_event(&client->repo_write->iev,
691 13b2bc37 2022-10-23 stsp GOTD_IMSG_REF_UPDATE, PROC_GOTD, -1, iref, datalen) == -1)
692 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg compose REF_UPDATE");
693 13b2bc37 2022-10-23 stsp free(iref);
694 13b2bc37 2022-10-23 stsp return err;
697 13b2bc37 2022-10-23 stsp static const struct got_error *
698 13b2bc37 2022-10-23 stsp forward_have(struct gotd_client *client, struct imsg *imsg)
700 13b2bc37 2022-10-23 stsp struct gotd_imsg_have ireq;
701 13b2bc37 2022-10-23 stsp struct gotd_imsg_have ihave;
702 13b2bc37 2022-10-23 stsp size_t datalen;
704 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
705 13b2bc37 2022-10-23 stsp if (datalen != sizeof(ireq))
706 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
708 13b2bc37 2022-10-23 stsp memcpy(&ireq, imsg->data, datalen);
710 13b2bc37 2022-10-23 stsp memset(&ihave, 0, sizeof(ihave));
711 13b2bc37 2022-10-23 stsp memcpy(ihave.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
712 13b2bc37 2022-10-23 stsp ihave.client_id = client->id;
714 13b2bc37 2022-10-23 stsp if (gotd_imsg_compose_event(&client->repo_read->iev, GOTD_IMSG_HAVE,
715 13b2bc37 2022-10-23 stsp PROC_GOTD, -1, &ihave, sizeof(ihave)) == -1)
716 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg compose HAVE");
718 13b2bc37 2022-10-23 stsp return NULL;
722 13b2bc37 2022-10-23 stsp client_has_capability(struct gotd_client *client, const char *capastr)
724 13b2bc37 2022-10-23 stsp struct gotd_client_capability *capa;
727 13b2bc37 2022-10-23 stsp if (client->ncapabilities == 0)
730 13b2bc37 2022-10-23 stsp for (i = 0; i < client->ncapabilities; i++) {
731 13b2bc37 2022-10-23 stsp capa = &client->capabilities[i];
732 13b2bc37 2022-10-23 stsp if (strcmp(capa->key, capastr) == 0)
739 13b2bc37 2022-10-23 stsp static const struct got_error *
740 13b2bc37 2022-10-23 stsp recv_capabilities(struct gotd_client *client, struct imsg *imsg)
742 13b2bc37 2022-10-23 stsp struct gotd_imsg_capabilities icapas;
743 13b2bc37 2022-10-23 stsp size_t datalen;
745 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
746 13b2bc37 2022-10-23 stsp if (datalen != sizeof(icapas))
747 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
748 13b2bc37 2022-10-23 stsp memcpy(&icapas, imsg->data, sizeof(icapas));
750 13b2bc37 2022-10-23 stsp client->ncapa_alloc = icapas.ncapabilities;
751 13b2bc37 2022-10-23 stsp client->capabilities = calloc(client->ncapa_alloc,
752 13b2bc37 2022-10-23 stsp sizeof(*client->capabilities));
753 13b2bc37 2022-10-23 stsp if (client->capabilities == NULL) {
754 13b2bc37 2022-10-23 stsp client->ncapa_alloc = 0;
755 13b2bc37 2022-10-23 stsp return got_error_from_errno("calloc");
758 13b2bc37 2022-10-23 stsp log_debug("expecting %zu capabilities from uid %d",
759 13b2bc37 2022-10-23 stsp client->ncapa_alloc, client->euid);
760 13b2bc37 2022-10-23 stsp return NULL;
763 13b2bc37 2022-10-23 stsp static const struct got_error *
764 13b2bc37 2022-10-23 stsp recv_capability(struct gotd_client *client, struct imsg *imsg)
766 13b2bc37 2022-10-23 stsp struct gotd_imsg_capability icapa;
767 13b2bc37 2022-10-23 stsp struct gotd_client_capability *capa;
768 13b2bc37 2022-10-23 stsp size_t datalen;
769 13b2bc37 2022-10-23 stsp char *key, *value = NULL;
771 13b2bc37 2022-10-23 stsp if (client->capabilities == NULL ||
772 13b2bc37 2022-10-23 stsp client->ncapabilities >= client->ncapa_alloc) {
773 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
774 13b2bc37 2022-10-23 stsp "unexpected capability received");
777 13b2bc37 2022-10-23 stsp memset(&icapa, 0, sizeof(icapa));
779 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
780 13b2bc37 2022-10-23 stsp if (datalen < sizeof(icapa))
781 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
782 13b2bc37 2022-10-23 stsp memcpy(&icapa, imsg->data, sizeof(icapa));
784 13b2bc37 2022-10-23 stsp if (datalen != sizeof(icapa) + icapa.key_len + icapa.value_len)
785 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
787 13b2bc37 2022-10-23 stsp key = malloc(icapa.key_len + 1);
788 13b2bc37 2022-10-23 stsp if (key == NULL)
789 13b2bc37 2022-10-23 stsp return got_error_from_errno("malloc");
790 13b2bc37 2022-10-23 stsp if (icapa.value_len > 0) {
791 13b2bc37 2022-10-23 stsp value = malloc(icapa.value_len + 1);
792 13b2bc37 2022-10-23 stsp if (value == NULL) {
794 13b2bc37 2022-10-23 stsp return got_error_from_errno("malloc");
798 13b2bc37 2022-10-23 stsp memcpy(key, imsg->data + sizeof(icapa), icapa.key_len);
799 13b2bc37 2022-10-23 stsp key[icapa.key_len] = '\0';
800 13b2bc37 2022-10-23 stsp if (value) {
801 13b2bc37 2022-10-23 stsp memcpy(value, imsg->data + sizeof(icapa) + icapa.key_len,
802 13b2bc37 2022-10-23 stsp icapa.value_len);
803 13b2bc37 2022-10-23 stsp value[icapa.value_len] = '\0';
806 13b2bc37 2022-10-23 stsp capa = &client->capabilities[client->ncapabilities++];
807 13b2bc37 2022-10-23 stsp capa->key = key;
808 13b2bc37 2022-10-23 stsp capa->value = value;
811 13b2bc37 2022-10-23 stsp log_debug("uid %d: capability %s=%s", client->euid, key, value);
813 13b2bc37 2022-10-23 stsp log_debug("uid %d: capability %s", client->euid, key);
815 13b2bc37 2022-10-23 stsp return NULL;
818 13b2bc37 2022-10-23 stsp static const struct got_error *
819 13b2bc37 2022-10-23 stsp send_packfile(struct gotd_client *client)
821 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
822 13b2bc37 2022-10-23 stsp struct gotd_imsg_send_packfile ipack;
823 13b2bc37 2022-10-23 stsp struct gotd_imsg_packfile_pipe ipipe;
824 13b2bc37 2022-10-23 stsp int pipe[2];
826 13b2bc37 2022-10-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
827 13b2bc37 2022-10-23 stsp return got_error_from_errno("socketpair");
829 13b2bc37 2022-10-23 stsp memset(&ipack, 0, sizeof(ipack));
830 13b2bc37 2022-10-23 stsp memset(&ipipe, 0, sizeof(ipipe));
832 13b2bc37 2022-10-23 stsp ipack.client_id = client->id;
833 13b2bc37 2022-10-23 stsp if (client_has_capability(client, GOT_CAPA_SIDE_BAND_64K))
834 13b2bc37 2022-10-23 stsp ipack.report_progress = 1;
836 13b2bc37 2022-10-23 stsp client->delta_cache_fd = got_opentempfd();
837 13b2bc37 2022-10-23 stsp if (client->delta_cache_fd == -1)
838 13b2bc37 2022-10-23 stsp return got_error_from_errno("got_opentempfd");
840 13b2bc37 2022-10-23 stsp if (gotd_imsg_compose_event(&client->repo_read->iev,
841 13b2bc37 2022-10-23 stsp GOTD_IMSG_SEND_PACKFILE, PROC_GOTD, client->delta_cache_fd,
842 13b2bc37 2022-10-23 stsp &ipack, sizeof(ipack)) == -1) {
843 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg compose SEND_PACKFILE");
844 13b2bc37 2022-10-23 stsp close(pipe[0]);
845 13b2bc37 2022-10-23 stsp close(pipe[1]);
846 13b2bc37 2022-10-23 stsp return err;
849 13b2bc37 2022-10-23 stsp ipipe.client_id = client->id;
851 86769de8 2022-10-28 stsp /* Send pack pipe end 0 to repo_read. */
852 13b2bc37 2022-10-23 stsp if (gotd_imsg_compose_event(&client->repo_read->iev,
853 13b2bc37 2022-10-23 stsp GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[0],
854 13b2bc37 2022-10-23 stsp &ipipe, sizeof(ipipe)) == -1) {
855 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg compose PACKFILE_PIPE");
856 13b2bc37 2022-10-23 stsp close(pipe[1]);
857 13b2bc37 2022-10-23 stsp return err;
860 86769de8 2022-10-28 stsp /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
861 86769de8 2022-10-28 stsp if (gotd_imsg_compose_event(&client->iev,
862 86769de8 2022-10-28 stsp GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1], NULL, 0) == -1)
863 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg compose PACKFILE_PIPE");
865 13b2bc37 2022-10-23 stsp return err;
868 13b2bc37 2022-10-23 stsp static const struct got_error *
869 13b2bc37 2022-10-23 stsp recv_packfile(struct gotd_client *client)
871 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
872 13b2bc37 2022-10-23 stsp struct gotd_imsg_recv_packfile ipack;
873 13b2bc37 2022-10-23 stsp struct gotd_imsg_packfile_pipe ipipe;
874 13b2bc37 2022-10-23 stsp struct gotd_imsg_packidx_file ifile;
875 13b2bc37 2022-10-23 stsp char *basepath = NULL, *pack_path = NULL, *idx_path = NULL;
876 13b2bc37 2022-10-23 stsp int packfd = -1, idxfd = -1;
877 13b2bc37 2022-10-23 stsp int pipe[2] = { -1, -1 };
879 13b2bc37 2022-10-23 stsp if (client->packfile_path) {
880 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_PRIVSEP_MSG,
881 13b2bc37 2022-10-23 stsp "uid %d already has a pack file", client->euid);
884 13b2bc37 2022-10-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
885 13b2bc37 2022-10-23 stsp return got_error_from_errno("socketpair");
887 13b2bc37 2022-10-23 stsp memset(&ipipe, 0, sizeof(ipipe));
888 13b2bc37 2022-10-23 stsp ipipe.client_id = client->id;
890 7fec5f4a 2022-10-28 stsp /* Send pack pipe end 0 to repo_write. */
891 13b2bc37 2022-10-23 stsp if (gotd_imsg_compose_event(&client->repo_write->iev,
892 13b2bc37 2022-10-23 stsp GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[0],
893 13b2bc37 2022-10-23 stsp &ipipe, sizeof(ipipe)) == -1) {
894 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg compose PACKFILE_PIPE");
895 13b2bc37 2022-10-23 stsp pipe[0] = -1;
898 13b2bc37 2022-10-23 stsp pipe[0] = -1;
900 7fec5f4a 2022-10-28 stsp /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
901 7fec5f4a 2022-10-28 stsp if (gotd_imsg_compose_event(&client->iev,
902 7fec5f4a 2022-10-28 stsp GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1], NULL, 0) == -1)
903 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg compose PACKFILE_PIPE");
904 13b2bc37 2022-10-23 stsp pipe[1] = -1;
906 13b2bc37 2022-10-23 stsp if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.pack",
907 eec68231 2022-12-14 stsp client->repo_write->repo_path, GOT_OBJECTS_PACK_DIR,
908 13b2bc37 2022-10-23 stsp client->euid) == -1) {
909 13b2bc37 2022-10-23 stsp err = got_error_from_errno("asprintf");
913 b90054ed 2022-11-01 stsp err = got_opentemp_named_fd(&pack_path, &packfd, basepath, "");
917 13b2bc37 2022-10-23 stsp free(basepath);
918 13b2bc37 2022-10-23 stsp if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.idx",
919 eec68231 2022-12-14 stsp client->repo_write->repo_path, GOT_OBJECTS_PACK_DIR,
920 13b2bc37 2022-10-23 stsp client->euid) == -1) {
921 13b2bc37 2022-10-23 stsp err = got_error_from_errno("asprintf");
922 13b2bc37 2022-10-23 stsp basepath = NULL;
925 b90054ed 2022-11-01 stsp err = got_opentemp_named_fd(&idx_path, &idxfd, basepath, "");
929 13b2bc37 2022-10-23 stsp memset(&ifile, 0, sizeof(ifile));
930 13b2bc37 2022-10-23 stsp ifile.client_id = client->id;
931 13b2bc37 2022-10-23 stsp if (gotd_imsg_compose_event(&client->repo_write->iev,
932 13b2bc37 2022-10-23 stsp GOTD_IMSG_PACKIDX_FILE, PROC_GOTD, idxfd,
933 13b2bc37 2022-10-23 stsp &ifile, sizeof(ifile)) == -1) {
934 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg compose PACKIDX_FILE");
935 13b2bc37 2022-10-23 stsp idxfd = -1;
938 13b2bc37 2022-10-23 stsp idxfd = -1;
940 13b2bc37 2022-10-23 stsp memset(&ipack, 0, sizeof(ipack));
941 13b2bc37 2022-10-23 stsp ipack.client_id = client->id;
942 13b2bc37 2022-10-23 stsp if (client_has_capability(client, GOT_CAPA_REPORT_STATUS))
943 13b2bc37 2022-10-23 stsp ipack.report_status = 1;
945 13b2bc37 2022-10-23 stsp if (gotd_imsg_compose_event(&client->repo_write->iev,
946 13b2bc37 2022-10-23 stsp GOTD_IMSG_RECV_PACKFILE, PROC_GOTD, packfd,
947 13b2bc37 2022-10-23 stsp &ipack, sizeof(ipack)) == -1) {
948 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg compose RECV_PACKFILE");
949 13b2bc37 2022-10-23 stsp packfd = -1;
952 13b2bc37 2022-10-23 stsp packfd = -1;
955 13b2bc37 2022-10-23 stsp free(basepath);
956 13b2bc37 2022-10-23 stsp if (pipe[0] != -1 && close(pipe[0]) == -1 && err == NULL)
957 13b2bc37 2022-10-23 stsp err = got_error_from_errno("close");
958 13b2bc37 2022-10-23 stsp if (pipe[1] != -1 && close(pipe[1]) == -1 && err == NULL)
959 13b2bc37 2022-10-23 stsp err = got_error_from_errno("close");
960 13b2bc37 2022-10-23 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
961 13b2bc37 2022-10-23 stsp err = got_error_from_errno("close");
962 13b2bc37 2022-10-23 stsp if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
963 13b2bc37 2022-10-23 stsp err = got_error_from_errno("close");
965 13b2bc37 2022-10-23 stsp free(pack_path);
966 13b2bc37 2022-10-23 stsp free(idx_path);
968 13b2bc37 2022-10-23 stsp client->packfile_path = pack_path;
969 13b2bc37 2022-10-23 stsp client->packidx_path = idx_path;
971 13b2bc37 2022-10-23 stsp return err;
974 13b2bc37 2022-10-23 stsp static void
975 13b2bc37 2022-10-23 stsp gotd_request(int fd, short events, void *arg)
977 13b2bc37 2022-10-23 stsp struct gotd_imsgev *iev = arg;
978 13b2bc37 2022-10-23 stsp struct imsgbuf *ibuf = &iev->ibuf;
979 13b2bc37 2022-10-23 stsp struct gotd_client *client = iev->handler_arg;
980 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
981 13b2bc37 2022-10-23 stsp struct imsg imsg;
984 13b2bc37 2022-10-23 stsp if (events & EV_WRITE) {
985 13b2bc37 2022-10-23 stsp while (ibuf->w.queued) {
986 13b2bc37 2022-10-23 stsp n = msgbuf_write(&ibuf->w);
987 13b2bc37 2022-10-23 stsp if (n == -1 && errno == EPIPE) {
989 13b2bc37 2022-10-23 stsp * The client has closed its socket.
990 13b2bc37 2022-10-23 stsp * This can happen when Git clients are
991 13b2bc37 2022-10-23 stsp * done sending pack file data.
993 13b2bc37 2022-10-23 stsp msgbuf_clear(&ibuf->w);
995 13b2bc37 2022-10-23 stsp } else if (n == -1 && errno != EAGAIN) {
996 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_flush");
997 13b2bc37 2022-10-23 stsp disconnect_on_error(client, err);
1000 13b2bc37 2022-10-23 stsp if (n == 0) {
1001 13b2bc37 2022-10-23 stsp /* Connection closed. */
1002 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_EOF);
1003 13b2bc37 2022-10-23 stsp disconnect_on_error(client, err);
1008 f1752522 2022-10-29 stsp /* Disconnect gotctl(8) now that messages have been sent. */
1009 f1752522 2022-10-29 stsp if (!client_is_reading(client) && !client_is_writing(client)) {
1010 f1752522 2022-10-29 stsp disconnect(client);
1015 13b2bc37 2022-10-23 stsp if ((events & EV_READ) == 0)
1018 13b2bc37 2022-10-23 stsp memset(&imsg, 0, sizeof(imsg));
1020 13b2bc37 2022-10-23 stsp while (err == NULL) {
1021 13b2bc37 2022-10-23 stsp err = gotd_imsg_recv(&imsg, ibuf, 0);
1022 13b2bc37 2022-10-23 stsp if (err) {
1023 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_PRIVSEP_READ)
1024 13b2bc37 2022-10-23 stsp err = NULL;
1028 13b2bc37 2022-10-23 stsp evtimer_del(&client->tmo);
1030 13b2bc37 2022-10-23 stsp switch (imsg.hdr.type) {
1031 f1752522 2022-10-29 stsp case GOTD_IMSG_INFO:
1032 f1752522 2022-10-29 stsp err = send_info(client);
1034 f1752522 2022-10-29 stsp case GOTD_IMSG_STOP:
1035 f1752522 2022-10-29 stsp err = stop_gotd(client);
1037 13b2bc37 2022-10-23 stsp case GOTD_IMSG_LIST_REFS:
1038 13b2bc37 2022-10-23 stsp if (client->state != GOTD_STATE_EXPECT_LIST_REFS) {
1039 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1040 13b2bc37 2022-10-23 stsp "unexpected list-refs request received");
1043 13b2bc37 2022-10-23 stsp err = forward_list_refs_request(client, &imsg);
1046 13b2bc37 2022-10-23 stsp client->state = GOTD_STATE_EXPECT_CAPABILITIES;
1047 13b2bc37 2022-10-23 stsp log_debug("uid %d: expecting capabilities",
1048 13b2bc37 2022-10-23 stsp client->euid);
1050 13b2bc37 2022-10-23 stsp case GOTD_IMSG_CAPABILITIES:
1051 13b2bc37 2022-10-23 stsp if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1052 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1053 13b2bc37 2022-10-23 stsp "unexpected capabilities received");
1056 13b2bc37 2022-10-23 stsp log_debug("receiving capabilities from uid %d",
1057 13b2bc37 2022-10-23 stsp client->euid);
1058 13b2bc37 2022-10-23 stsp err = recv_capabilities(client, &imsg);
1060 13b2bc37 2022-10-23 stsp case GOTD_IMSG_CAPABILITY:
1061 13b2bc37 2022-10-23 stsp if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1062 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1063 13b2bc37 2022-10-23 stsp "unexpected capability received");
1066 13b2bc37 2022-10-23 stsp err = recv_capability(client, &imsg);
1067 13b2bc37 2022-10-23 stsp if (err || client->ncapabilities < client->ncapa_alloc)
1069 13b2bc37 2022-10-23 stsp if (client_is_reading(client)) {
1070 13b2bc37 2022-10-23 stsp client->state = GOTD_STATE_EXPECT_WANT;
1071 13b2bc37 2022-10-23 stsp log_debug("uid %d: expecting want-lines",
1072 13b2bc37 2022-10-23 stsp client->euid);
1073 13b2bc37 2022-10-23 stsp } else if (client_is_writing(client)) {
1074 13b2bc37 2022-10-23 stsp client->state = GOTD_STATE_EXPECT_REF_UPDATE;
1075 13b2bc37 2022-10-23 stsp log_debug("uid %d: expecting ref-update-lines",
1076 13b2bc37 2022-10-23 stsp client->euid);
1078 13b2bc37 2022-10-23 stsp fatalx("client %d is both reading and writing",
1079 13b2bc37 2022-10-23 stsp client->euid);
1081 13b2bc37 2022-10-23 stsp case GOTD_IMSG_WANT:
1082 13b2bc37 2022-10-23 stsp if (client->state != GOTD_STATE_EXPECT_WANT) {
1083 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1084 13b2bc37 2022-10-23 stsp "unexpected want-line received");
1087 13b2bc37 2022-10-23 stsp log_debug("received want-line from uid %d",
1088 13b2bc37 2022-10-23 stsp client->euid);
1089 13b2bc37 2022-10-23 stsp err = ensure_client_is_reading(client);
1092 13b2bc37 2022-10-23 stsp err = forward_want(client, &imsg);
1094 13b2bc37 2022-10-23 stsp case GOTD_IMSG_REF_UPDATE:
1095 13b2bc37 2022-10-23 stsp if (client->state != GOTD_STATE_EXPECT_REF_UPDATE) {
1096 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1097 13b2bc37 2022-10-23 stsp "unexpected ref-update-line received");
1100 13b2bc37 2022-10-23 stsp log_debug("received ref-update-line from uid %d",
1101 13b2bc37 2022-10-23 stsp client->euid);
1102 13b2bc37 2022-10-23 stsp err = ensure_client_is_writing(client);
1105 13b2bc37 2022-10-23 stsp err = forward_ref_update(client, &imsg);
1108 13b2bc37 2022-10-23 stsp client->state = GOTD_STATE_EXPECT_MORE_REF_UPDATES;
1110 13b2bc37 2022-10-23 stsp case GOTD_IMSG_HAVE:
1111 13b2bc37 2022-10-23 stsp if (client->state != GOTD_STATE_EXPECT_HAVE) {
1112 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1113 13b2bc37 2022-10-23 stsp "unexpected have-line received");
1116 13b2bc37 2022-10-23 stsp log_debug("received have-line from uid %d",
1117 13b2bc37 2022-10-23 stsp client->euid);
1118 13b2bc37 2022-10-23 stsp err = ensure_client_is_reading(client);
1121 13b2bc37 2022-10-23 stsp err = forward_have(client, &imsg);
1125 13b2bc37 2022-10-23 stsp case GOTD_IMSG_FLUSH:
1126 13b2bc37 2022-10-23 stsp if (client->state == GOTD_STATE_EXPECT_WANT ||
1127 13b2bc37 2022-10-23 stsp client->state == GOTD_STATE_EXPECT_HAVE) {
1128 13b2bc37 2022-10-23 stsp err = ensure_client_is_reading(client);
1131 13b2bc37 2022-10-23 stsp } else if (client->state ==
1132 13b2bc37 2022-10-23 stsp GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1133 13b2bc37 2022-10-23 stsp err = ensure_client_is_writing(client);
1137 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1138 13b2bc37 2022-10-23 stsp "unexpected flush-pkt received");
1141 13b2bc37 2022-10-23 stsp log_debug("received flush-pkt from uid %d",
1142 13b2bc37 2022-10-23 stsp client->euid);
1143 13b2bc37 2022-10-23 stsp if (client->state == GOTD_STATE_EXPECT_WANT) {
1144 13b2bc37 2022-10-23 stsp client->state = GOTD_STATE_EXPECT_HAVE;
1145 13b2bc37 2022-10-23 stsp log_debug("uid %d: expecting have-lines",
1146 13b2bc37 2022-10-23 stsp client->euid);
1147 13b2bc37 2022-10-23 stsp } else if (client->state == GOTD_STATE_EXPECT_HAVE) {
1148 13b2bc37 2022-10-23 stsp client->state = GOTD_STATE_EXPECT_DONE;
1149 13b2bc37 2022-10-23 stsp log_debug("uid %d: expecting 'done'",
1150 13b2bc37 2022-10-23 stsp client->euid);
1151 13b2bc37 2022-10-23 stsp } else if (client->state ==
1152 13b2bc37 2022-10-23 stsp GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1153 13b2bc37 2022-10-23 stsp client->state = GOTD_STATE_EXPECT_PACKFILE;
1154 13b2bc37 2022-10-23 stsp log_debug("uid %d: expecting packfile",
1155 13b2bc37 2022-10-23 stsp client->euid);
1156 13b2bc37 2022-10-23 stsp err = recv_packfile(client);
1158 13b2bc37 2022-10-23 stsp /* should not happen, see above */
1159 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1160 13b2bc37 2022-10-23 stsp "unexpected client state");
1164 13b2bc37 2022-10-23 stsp case GOTD_IMSG_DONE:
1165 13b2bc37 2022-10-23 stsp if (client->state != GOTD_STATE_EXPECT_HAVE &&
1166 13b2bc37 2022-10-23 stsp client->state != GOTD_STATE_EXPECT_DONE) {
1167 13b2bc37 2022-10-23 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1168 13b2bc37 2022-10-23 stsp "unexpected flush-pkt received");
1171 13b2bc37 2022-10-23 stsp log_debug("received 'done' from uid %d", client->euid);
1172 13b2bc37 2022-10-23 stsp err = ensure_client_is_reading(client);
1175 13b2bc37 2022-10-23 stsp client->state = GOTD_STATE_DONE;
1176 13b2bc37 2022-10-23 stsp err = send_packfile(client);
1179 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1183 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
1186 13b2bc37 2022-10-23 stsp if (err) {
1187 13b2bc37 2022-10-23 stsp if (err->code != GOT_ERR_EOF ||
1188 13b2bc37 2022-10-23 stsp client->state != GOTD_STATE_EXPECT_PACKFILE)
1189 13b2bc37 2022-10-23 stsp disconnect_on_error(client, err);
1191 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(&client->iev);
1192 13b2bc37 2022-10-23 stsp evtimer_add(&client->tmo, &timeout);
1196 13b2bc37 2022-10-23 stsp static void
1197 13b2bc37 2022-10-23 stsp gotd_request_timeout(int fd, short events, void *arg)
1199 13b2bc37 2022-10-23 stsp struct gotd_client *client = arg;
1201 13b2bc37 2022-10-23 stsp log_debug("disconnecting uid %d due to timeout", client->euid);
1202 13b2bc37 2022-10-23 stsp disconnect(client);
1205 d93ecf7d 2022-12-14 stsp static const struct got_error *
1206 d93ecf7d 2022-12-14 stsp recv_connect(uint32_t *client_id, struct imsg *imsg)
1208 d93ecf7d 2022-12-14 stsp const struct got_error *err = NULL;
1209 d93ecf7d 2022-12-14 stsp struct gotd_imsg_connect iconnect;
1210 d93ecf7d 2022-12-14 stsp size_t datalen;
1211 13b2bc37 2022-10-23 stsp int s = -1;
1212 13b2bc37 2022-10-23 stsp struct gotd_client *client = NULL;
1213 13b2bc37 2022-10-23 stsp uid_t euid;
1214 13b2bc37 2022-10-23 stsp gid_t egid;
1216 d93ecf7d 2022-12-14 stsp *client_id = 0;
1218 d93ecf7d 2022-12-14 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1219 d93ecf7d 2022-12-14 stsp if (datalen != sizeof(iconnect))
1220 d93ecf7d 2022-12-14 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1221 d93ecf7d 2022-12-14 stsp memcpy(&iconnect, imsg->data, sizeof(iconnect));
1223 d93ecf7d 2022-12-14 stsp s = imsg->fd;
1224 13b2bc37 2022-10-23 stsp if (s == -1) {
1225 d93ecf7d 2022-12-14 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1226 d93ecf7d 2022-12-14 stsp goto done;
1229 d93ecf7d 2022-12-14 stsp if (find_client(iconnect.client_id)) {
1230 d93ecf7d 2022-12-14 stsp err = got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
1231 d93ecf7d 2022-12-14 stsp goto done;
1234 13b2bc37 2022-10-23 stsp if (getpeereid(s, &euid, &egid) == -1) {
1235 d93ecf7d 2022-12-14 stsp err = got_error_from_errno("getpeerid");
1236 d93ecf7d 2022-12-14 stsp goto done;
1239 13b2bc37 2022-10-23 stsp client = calloc(1, sizeof(*client));
1240 13b2bc37 2022-10-23 stsp if (client == NULL) {
1241 d93ecf7d 2022-12-14 stsp err = got_error_from_errno("calloc");
1242 d93ecf7d 2022-12-14 stsp goto done;
1245 d93ecf7d 2022-12-14 stsp *client_id = iconnect.client_id;
1247 13b2bc37 2022-10-23 stsp client->state = GOTD_STATE_EXPECT_LIST_REFS;
1248 d93ecf7d 2022-12-14 stsp client->id = iconnect.client_id;
1249 13b2bc37 2022-10-23 stsp client->fd = s;
1251 13b2bc37 2022-10-23 stsp client->delta_cache_fd = -1;
1252 13b2bc37 2022-10-23 stsp client->euid = euid;
1253 13b2bc37 2022-10-23 stsp client->egid = egid;
1254 13b2bc37 2022-10-23 stsp client->nref_updates = -1;
1256 13b2bc37 2022-10-23 stsp imsg_init(&client->iev.ibuf, client->fd);
1257 13b2bc37 2022-10-23 stsp client->iev.handler = gotd_request;
1258 13b2bc37 2022-10-23 stsp client->iev.events = EV_READ;
1259 13b2bc37 2022-10-23 stsp client->iev.handler_arg = client;
1261 13b2bc37 2022-10-23 stsp event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
1262 13b2bc37 2022-10-23 stsp &client->iev);
1263 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(&client->iev);
1265 13b2bc37 2022-10-23 stsp evtimer_set(&client->tmo, gotd_request_timeout, client);
1267 13b2bc37 2022-10-23 stsp add_client(client);
1268 13b2bc37 2022-10-23 stsp log_debug("%s: new client uid %d connected on fd %d", __func__,
1269 13b2bc37 2022-10-23 stsp client->euid, client->fd);
1271 d93ecf7d 2022-12-14 stsp if (err) {
1272 d93ecf7d 2022-12-14 stsp struct gotd_child_proc *listen_proc = &gotd.procs[0];
1273 d93ecf7d 2022-12-14 stsp struct gotd_imsg_disconnect idisconnect;
1275 d93ecf7d 2022-12-14 stsp idisconnect.client_id = client->id;
1276 d93ecf7d 2022-12-14 stsp if (gotd_imsg_compose_event(&listen_proc->iev,
1277 d93ecf7d 2022-12-14 stsp GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
1278 d93ecf7d 2022-12-14 stsp &idisconnect, sizeof(idisconnect)) == -1)
1279 d93ecf7d 2022-12-14 stsp log_warn("imsg compose DISCONNECT");
1281 d93ecf7d 2022-12-14 stsp if (s != -1)
1285 d93ecf7d 2022-12-14 stsp return err;
1288 13b2bc37 2022-10-23 stsp static const char *gotd_proc_names[PROC_MAX] = {
1291 13b2bc37 2022-10-23 stsp "repo_read",
1292 13b2bc37 2022-10-23 stsp "repo_write"
1295 13b2bc37 2022-10-23 stsp static struct gotd_child_proc *
1296 13b2bc37 2022-10-23 stsp get_proc_for_pid(pid_t pid)
1298 13b2bc37 2022-10-23 stsp struct gotd_child_proc *proc;
1301 13b2bc37 2022-10-23 stsp for (i = 0; i < gotd.nprocs; i++) {
1302 13b2bc37 2022-10-23 stsp proc = &gotd.procs[i];
1303 13b2bc37 2022-10-23 stsp if (proc->pid == pid)
1304 13b2bc37 2022-10-23 stsp return proc;
1307 13b2bc37 2022-10-23 stsp return NULL;
1310 13b2bc37 2022-10-23 stsp static void
1311 13b2bc37 2022-10-23 stsp kill_proc(struct gotd_child_proc *proc, int fatal)
1313 13b2bc37 2022-10-23 stsp if (fatal) {
1314 13b2bc37 2022-10-23 stsp log_warnx("sending SIGKILL to PID %d", proc->pid);
1315 13b2bc37 2022-10-23 stsp kill(proc->pid, SIGKILL);
1317 13b2bc37 2022-10-23 stsp kill(proc->pid, SIGTERM);
1320 13b2bc37 2022-10-23 stsp static void
1321 13b2bc37 2022-10-23 stsp gotd_shutdown(void)
1323 13b2bc37 2022-10-23 stsp pid_t pid;
1324 13b2bc37 2022-10-23 stsp int status, i;
1325 13b2bc37 2022-10-23 stsp struct gotd_child_proc *proc;
1327 13b2bc37 2022-10-23 stsp for (i = 0; i < gotd.nprocs; i++) {
1328 13b2bc37 2022-10-23 stsp proc = &gotd.procs[i];
1329 13b2bc37 2022-10-23 stsp msgbuf_clear(&proc->iev.ibuf.w);
1330 13b2bc37 2022-10-23 stsp close(proc->iev.ibuf.fd);
1331 13b2bc37 2022-10-23 stsp kill_proc(proc, 0);
1334 13b2bc37 2022-10-23 stsp log_debug("waiting for children to terminate");
1336 13b2bc37 2022-10-23 stsp pid = wait(&status);
1337 13b2bc37 2022-10-23 stsp if (pid == -1) {
1338 13b2bc37 2022-10-23 stsp if (errno != EINTR && errno != ECHILD)
1339 13b2bc37 2022-10-23 stsp fatal("wait");
1340 13b2bc37 2022-10-23 stsp } else if (WIFSIGNALED(status)) {
1341 13b2bc37 2022-10-23 stsp proc = get_proc_for_pid(pid);
1342 13b2bc37 2022-10-23 stsp log_warnx("%s %s child process terminated; signal %d",
1343 13b2bc37 2022-10-23 stsp proc ? gotd_proc_names[proc->type] : "",
1344 eec68231 2022-12-14 stsp proc ? proc->repo_path : "", WTERMSIG(status));
1346 13b2bc37 2022-10-23 stsp } while (pid != -1 || (pid == -1 && errno == EINTR));
1348 13b2bc37 2022-10-23 stsp log_info("terminating");
1353 13b2bc37 2022-10-23 stsp gotd_sighdlr(int sig, short event, void *arg)
1356 13b2bc37 2022-10-23 stsp * Normal signal handler rules don't apply because libevent
1357 13b2bc37 2022-10-23 stsp * decouples for us.
1360 13b2bc37 2022-10-23 stsp switch (sig) {
1361 13b2bc37 2022-10-23 stsp case SIGHUP:
1362 13b2bc37 2022-10-23 stsp log_info("%s: ignoring SIGHUP", __func__);
1364 13b2bc37 2022-10-23 stsp case SIGUSR1:
1365 13b2bc37 2022-10-23 stsp log_info("%s: ignoring SIGUSR1", __func__);
1367 13b2bc37 2022-10-23 stsp case SIGTERM:
1368 13b2bc37 2022-10-23 stsp case SIGINT:
1369 13b2bc37 2022-10-23 stsp gotd_shutdown();
1370 13b2bc37 2022-10-23 stsp log_warnx("gotd terminating");
1374 13b2bc37 2022-10-23 stsp fatalx("unexpected signal");
1378 13b2bc37 2022-10-23 stsp static const struct got_error *
1379 13b2bc37 2022-10-23 stsp ensure_proc_is_reading(struct gotd_client *client,
1380 13b2bc37 2022-10-23 stsp struct gotd_child_proc *proc)
1382 13b2bc37 2022-10-23 stsp if (!client_is_reading(client)) {
1383 13b2bc37 2022-10-23 stsp kill_proc(proc, 1);
1384 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_BAD_PACKET,
1385 13b2bc37 2022-10-23 stsp "PID %d handled a read-request for uid %d but this "
1386 13b2bc37 2022-10-23 stsp "user is not reading from a repository", proc->pid,
1387 13b2bc37 2022-10-23 stsp client->euid);
1390 13b2bc37 2022-10-23 stsp return NULL;
1393 13b2bc37 2022-10-23 stsp static const struct got_error *
1394 13b2bc37 2022-10-23 stsp ensure_proc_is_writing(struct gotd_client *client,
1395 13b2bc37 2022-10-23 stsp struct gotd_child_proc *proc)
1397 13b2bc37 2022-10-23 stsp if (!client_is_writing(client)) {
1398 13b2bc37 2022-10-23 stsp kill_proc(proc, 1);
1399 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_BAD_PACKET,
1400 13b2bc37 2022-10-23 stsp "PID %d handled a write-request for uid %d but this "
1401 13b2bc37 2022-10-23 stsp "user is not writing to a repository", proc->pid,
1402 13b2bc37 2022-10-23 stsp client->euid);
1405 13b2bc37 2022-10-23 stsp return NULL;
1408 13b2bc37 2022-10-23 stsp static int
1409 13b2bc37 2022-10-23 stsp verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
1410 13b2bc37 2022-10-23 stsp struct imsg *imsg)
1412 13b2bc37 2022-10-23 stsp const struct got_error *err;
1413 13b2bc37 2022-10-23 stsp struct gotd_child_proc *client_proc;
1414 13b2bc37 2022-10-23 stsp int ret = 0;
1416 d93ecf7d 2022-12-14 stsp if (proc->type == PROC_REPO_READ || proc->type == PROC_REPO_WRITE) {
1417 d93ecf7d 2022-12-14 stsp client_proc = get_client_proc(client);
1418 d93ecf7d 2022-12-14 stsp if (client_proc == NULL)
1419 d93ecf7d 2022-12-14 stsp fatalx("no process found for uid %d", client->euid);
1420 d93ecf7d 2022-12-14 stsp if (proc->pid != client_proc->pid) {
1421 d93ecf7d 2022-12-14 stsp kill_proc(proc, 1);
1422 d93ecf7d 2022-12-14 stsp log_warnx("received message from PID %d for uid %d, "
1423 d93ecf7d 2022-12-14 stsp "while PID %d is the process serving this user",
1424 d93ecf7d 2022-12-14 stsp proc->pid, client->euid, client_proc->pid);
1429 13b2bc37 2022-10-23 stsp switch (imsg->hdr.type) {
1430 13b2bc37 2022-10-23 stsp case GOTD_IMSG_ERROR:
1433 d93ecf7d 2022-12-14 stsp case GOTD_IMSG_CONNECT:
1434 d93ecf7d 2022-12-14 stsp if (proc->type != PROC_LISTEN) {
1435 d93ecf7d 2022-12-14 stsp err = got_error_fmt(GOT_ERR_BAD_PACKET,
1436 d93ecf7d 2022-12-14 stsp "new connection for uid %d from PID %d "
1437 d93ecf7d 2022-12-14 stsp "which is not the listen process",
1438 d93ecf7d 2022-12-14 stsp proc->pid, client->euid);
1442 13b2bc37 2022-10-23 stsp case GOTD_IMSG_PACKFILE_DONE:
1443 13b2bc37 2022-10-23 stsp err = ensure_proc_is_reading(client, proc);
1445 13b2bc37 2022-10-23 stsp log_warnx("uid %d: %s", client->euid, err->msg);
1449 13b2bc37 2022-10-23 stsp case GOTD_IMSG_PACKFILE_INSTALL:
1450 13b2bc37 2022-10-23 stsp case GOTD_IMSG_REF_UPDATES_START:
1451 13b2bc37 2022-10-23 stsp case GOTD_IMSG_REF_UPDATE:
1452 13b2bc37 2022-10-23 stsp err = ensure_proc_is_writing(client, proc);
1454 13b2bc37 2022-10-23 stsp log_warnx("uid %d: %s", client->euid, err->msg);
1459 13b2bc37 2022-10-23 stsp log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
1463 13b2bc37 2022-10-23 stsp return ret;
1466 13b2bc37 2022-10-23 stsp static const struct got_error *
1467 13b2bc37 2022-10-23 stsp recv_packfile_done(uint32_t *client_id, struct imsg *imsg)
1469 13b2bc37 2022-10-23 stsp struct gotd_imsg_packfile_done idone;
1470 13b2bc37 2022-10-23 stsp size_t datalen;
1472 13b2bc37 2022-10-23 stsp log_debug("packfile-done received");
1474 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1475 13b2bc37 2022-10-23 stsp if (datalen != sizeof(idone))
1476 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1477 13b2bc37 2022-10-23 stsp memcpy(&idone, imsg->data, sizeof(idone));
1479 13b2bc37 2022-10-23 stsp *client_id = idone.client_id;
1480 13b2bc37 2022-10-23 stsp return NULL;
1483 13b2bc37 2022-10-23 stsp static const struct got_error *
1484 13b2bc37 2022-10-23 stsp recv_packfile_install(uint32_t *client_id, struct imsg *imsg)
1486 13b2bc37 2022-10-23 stsp struct gotd_imsg_packfile_install inst;
1487 13b2bc37 2022-10-23 stsp size_t datalen;
1489 13b2bc37 2022-10-23 stsp log_debug("packfile-install received");
1491 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1492 13b2bc37 2022-10-23 stsp if (datalen != sizeof(inst))
1493 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1494 13b2bc37 2022-10-23 stsp memcpy(&inst, imsg->data, sizeof(inst));
1496 13b2bc37 2022-10-23 stsp *client_id = inst.client_id;
1497 13b2bc37 2022-10-23 stsp return NULL;
1500 13b2bc37 2022-10-23 stsp static const struct got_error *
1501 13b2bc37 2022-10-23 stsp recv_ref_updates_start(uint32_t *client_id, struct imsg *imsg)
1503 13b2bc37 2022-10-23 stsp struct gotd_imsg_ref_updates_start istart;
1504 13b2bc37 2022-10-23 stsp size_t datalen;
1506 13b2bc37 2022-10-23 stsp log_debug("ref-updates-start received");
1508 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1509 13b2bc37 2022-10-23 stsp if (datalen != sizeof(istart))
1510 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1511 13b2bc37 2022-10-23 stsp memcpy(&istart, imsg->data, sizeof(istart));
1513 13b2bc37 2022-10-23 stsp *client_id = istart.client_id;
1514 13b2bc37 2022-10-23 stsp return NULL;
1517 13b2bc37 2022-10-23 stsp static const struct got_error *
1518 13b2bc37 2022-10-23 stsp recv_ref_update(uint32_t *client_id, struct imsg *imsg)
1520 13b2bc37 2022-10-23 stsp struct gotd_imsg_ref_update iref;
1521 13b2bc37 2022-10-23 stsp size_t datalen;
1523 13b2bc37 2022-10-23 stsp log_debug("ref-update received");
1525 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1526 13b2bc37 2022-10-23 stsp if (datalen < sizeof(iref))
1527 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1528 13b2bc37 2022-10-23 stsp memcpy(&iref, imsg->data, sizeof(iref));
1530 13b2bc37 2022-10-23 stsp *client_id = iref.client_id;
1531 13b2bc37 2022-10-23 stsp return NULL;
1534 13b2bc37 2022-10-23 stsp static const struct got_error *
1535 13b2bc37 2022-10-23 stsp send_ref_update_ok(struct gotd_client *client,
1536 13b2bc37 2022-10-23 stsp struct gotd_imsg_ref_update *iref, const char *refname)
1538 13b2bc37 2022-10-23 stsp struct gotd_imsg_ref_update_ok iok;
1539 13b2bc37 2022-10-23 stsp struct ibuf *wbuf;
1540 13b2bc37 2022-10-23 stsp size_t len;
1542 13b2bc37 2022-10-23 stsp memset(&iok, 0, sizeof(iok));
1543 13b2bc37 2022-10-23 stsp iok.client_id = client->id;
1544 13b2bc37 2022-10-23 stsp memcpy(iok.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1545 13b2bc37 2022-10-23 stsp memcpy(iok.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1546 13b2bc37 2022-10-23 stsp iok.name_len = strlen(refname);
1548 13b2bc37 2022-10-23 stsp len = sizeof(iok) + iok.name_len;
1549 13b2bc37 2022-10-23 stsp wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_OK,
1550 13b2bc37 2022-10-23 stsp PROC_GOTD, gotd.pid, len);
1551 13b2bc37 2022-10-23 stsp if (wbuf == NULL)
1552 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_create REF_UPDATE_OK");
1554 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, &iok, sizeof(iok)) == -1)
1555 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add REF_UPDATE_OK");
1556 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, refname, iok.name_len) == -1)
1557 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add REF_UPDATE_OK");
1559 13b2bc37 2022-10-23 stsp wbuf->fd = -1;
1560 13b2bc37 2022-10-23 stsp imsg_close(&client->iev.ibuf, wbuf);
1561 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(&client->iev);
1562 13b2bc37 2022-10-23 stsp return NULL;
1565 13b2bc37 2022-10-23 stsp static void
1566 13b2bc37 2022-10-23 stsp send_refs_updated(struct gotd_client *client)
1568 13b2bc37 2022-10-23 stsp if (gotd_imsg_compose_event(&client->iev,
1569 13b2bc37 2022-10-23 stsp GOTD_IMSG_REFS_UPDATED, PROC_GOTD, -1, NULL, 0) == -1)
1570 13b2bc37 2022-10-23 stsp log_warn("imsg compose REFS_UPDATED");
1573 13b2bc37 2022-10-23 stsp static const struct got_error *
1574 13b2bc37 2022-10-23 stsp send_ref_update_ng(struct gotd_client *client,
1575 13b2bc37 2022-10-23 stsp struct gotd_imsg_ref_update *iref, const char *refname,
1576 13b2bc37 2022-10-23 stsp const char *reason)
1578 13b2bc37 2022-10-23 stsp const struct got_error *ng_err;
1579 13b2bc37 2022-10-23 stsp struct gotd_imsg_ref_update_ng ing;
1580 13b2bc37 2022-10-23 stsp struct ibuf *wbuf;
1581 13b2bc37 2022-10-23 stsp size_t len;
1583 13b2bc37 2022-10-23 stsp memset(&ing, 0, sizeof(ing));
1584 13b2bc37 2022-10-23 stsp ing.client_id = client->id;
1585 13b2bc37 2022-10-23 stsp memcpy(ing.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1586 13b2bc37 2022-10-23 stsp memcpy(ing.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1587 13b2bc37 2022-10-23 stsp ing.name_len = strlen(refname);
1589 13b2bc37 2022-10-23 stsp ng_err = got_error_fmt(GOT_ERR_REF_BUSY, "%s", reason);
1590 13b2bc37 2022-10-23 stsp ing.reason_len = strlen(ng_err->msg);
1592 13b2bc37 2022-10-23 stsp len = sizeof(ing) + ing.name_len + ing.reason_len;
1593 13b2bc37 2022-10-23 stsp wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_NG,
1594 13b2bc37 2022-10-23 stsp PROC_GOTD, gotd.pid, len);
1595 13b2bc37 2022-10-23 stsp if (wbuf == NULL)
1596 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_create REF_UPDATE_NG");
1598 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, &ing, sizeof(ing)) == -1)
1599 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add REF_UPDATE_NG");
1600 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, refname, ing.name_len) == -1)
1601 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add REF_UPDATE_NG");
1602 13b2bc37 2022-10-23 stsp if (imsg_add(wbuf, ng_err->msg, ing.reason_len) == -1)
1603 13b2bc37 2022-10-23 stsp return got_error_from_errno("imsg_add REF_UPDATE_NG");
1605 13b2bc37 2022-10-23 stsp wbuf->fd = -1;
1606 13b2bc37 2022-10-23 stsp imsg_close(&client->iev.ibuf, wbuf);
1607 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(&client->iev);
1608 13b2bc37 2022-10-23 stsp return NULL;
1611 13b2bc37 2022-10-23 stsp static const struct got_error *
1612 13b2bc37 2022-10-23 stsp install_pack(struct gotd_client *client, const char *repo_path,
1613 13b2bc37 2022-10-23 stsp struct imsg *imsg)
1615 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
1616 13b2bc37 2022-10-23 stsp struct gotd_imsg_packfile_install inst;
1617 13b2bc37 2022-10-23 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
1618 13b2bc37 2022-10-23 stsp size_t datalen;
1619 13b2bc37 2022-10-23 stsp char *packfile_path = NULL, *packidx_path = NULL;
1621 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1622 13b2bc37 2022-10-23 stsp if (datalen != sizeof(inst))
1623 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1624 13b2bc37 2022-10-23 stsp memcpy(&inst, imsg->data, sizeof(inst));
1626 13b2bc37 2022-10-23 stsp if (client->packfile_path == NULL)
1627 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
1628 13b2bc37 2022-10-23 stsp "client has no pack file");
1629 13b2bc37 2022-10-23 stsp if (client->packidx_path == NULL)
1630 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
1631 13b2bc37 2022-10-23 stsp "client has no pack file index");
1633 13b2bc37 2022-10-23 stsp if (got_sha1_digest_to_str(inst.pack_sha1, hex, sizeof(hex)) == NULL)
1634 13b2bc37 2022-10-23 stsp return got_error_msg(GOT_ERR_NO_SPACE,
1635 13b2bc37 2022-10-23 stsp "could not convert pack file SHA1 to hex");
1637 13b2bc37 2022-10-23 stsp if (asprintf(&packfile_path, "/%s/%s/pack-%s.pack",
1638 13b2bc37 2022-10-23 stsp repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1639 13b2bc37 2022-10-23 stsp err = got_error_from_errno("asprintf");
1640 13b2bc37 2022-10-23 stsp goto done;
1643 13b2bc37 2022-10-23 stsp if (asprintf(&packidx_path, "/%s/%s/pack-%s.idx",
1644 13b2bc37 2022-10-23 stsp repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1645 13b2bc37 2022-10-23 stsp err = got_error_from_errno("asprintf");
1646 13b2bc37 2022-10-23 stsp goto done;
1649 13b2bc37 2022-10-23 stsp if (rename(client->packfile_path, packfile_path) == -1) {
1650 13b2bc37 2022-10-23 stsp err = got_error_from_errno3("rename", client->packfile_path,
1651 13b2bc37 2022-10-23 stsp packfile_path);
1652 13b2bc37 2022-10-23 stsp goto done;
1655 13b2bc37 2022-10-23 stsp free(client->packfile_path);
1656 13b2bc37 2022-10-23 stsp client->packfile_path = NULL;
1658 13b2bc37 2022-10-23 stsp if (rename(client->packidx_path, packidx_path) == -1) {
1659 13b2bc37 2022-10-23 stsp err = got_error_from_errno3("rename", client->packidx_path,
1660 13b2bc37 2022-10-23 stsp packidx_path);
1661 13b2bc37 2022-10-23 stsp goto done;
1664 13b2bc37 2022-10-23 stsp free(client->packidx_path);
1665 13b2bc37 2022-10-23 stsp client->packidx_path = NULL;
1667 13b2bc37 2022-10-23 stsp free(packfile_path);
1668 13b2bc37 2022-10-23 stsp free(packidx_path);
1669 13b2bc37 2022-10-23 stsp return err;
1672 13b2bc37 2022-10-23 stsp static const struct got_error *
1673 13b2bc37 2022-10-23 stsp begin_ref_updates(struct gotd_client *client, struct imsg *imsg)
1675 13b2bc37 2022-10-23 stsp struct gotd_imsg_ref_updates_start istart;
1676 13b2bc37 2022-10-23 stsp size_t datalen;
1678 13b2bc37 2022-10-23 stsp if (client->nref_updates != -1)
1679 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1681 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1682 13b2bc37 2022-10-23 stsp if (datalen != sizeof(istart))
1683 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1684 13b2bc37 2022-10-23 stsp memcpy(&istart, imsg->data, sizeof(istart));
1686 13b2bc37 2022-10-23 stsp if (istart.nref_updates <= 0)
1687 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1689 13b2bc37 2022-10-23 stsp client->nref_updates = istart.nref_updates;
1690 13b2bc37 2022-10-23 stsp return NULL;
1693 13b2bc37 2022-10-23 stsp static const struct got_error *
1694 13b2bc37 2022-10-23 stsp update_ref(struct gotd_client *client, const char *repo_path,
1695 13b2bc37 2022-10-23 stsp struct imsg *imsg)
1697 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
1698 13b2bc37 2022-10-23 stsp struct got_repository *repo = NULL;
1699 13b2bc37 2022-10-23 stsp struct got_reference *ref = NULL;
1700 13b2bc37 2022-10-23 stsp struct gotd_imsg_ref_update iref;
1701 13b2bc37 2022-10-23 stsp struct got_object_id old_id, new_id;
1702 13b2bc37 2022-10-23 stsp struct got_object_id *id = NULL;
1703 13b2bc37 2022-10-23 stsp struct got_object *obj = NULL;
1704 13b2bc37 2022-10-23 stsp char *refname = NULL;
1705 13b2bc37 2022-10-23 stsp size_t datalen;
1706 13b2bc37 2022-10-23 stsp int locked = 0;
1708 13b2bc37 2022-10-23 stsp log_debug("update-ref from uid %d", client->euid);
1710 13b2bc37 2022-10-23 stsp if (client->nref_updates <= 0)
1711 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1713 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1714 13b2bc37 2022-10-23 stsp if (datalen < sizeof(iref))
1715 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1716 13b2bc37 2022-10-23 stsp memcpy(&iref, imsg->data, sizeof(iref));
1717 13b2bc37 2022-10-23 stsp if (datalen != sizeof(iref) + iref.name_len)
1718 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1719 13b2bc37 2022-10-23 stsp refname = malloc(iref.name_len + 1);
1720 13b2bc37 2022-10-23 stsp if (refname == NULL)
1721 13b2bc37 2022-10-23 stsp return got_error_from_errno("malloc");
1722 13b2bc37 2022-10-23 stsp memcpy(refname, imsg->data + sizeof(iref), iref.name_len);
1723 13b2bc37 2022-10-23 stsp refname[iref.name_len] = '\0';
1725 13b2bc37 2022-10-23 stsp log_debug("updating ref %s for uid %d", refname, client->euid);
1727 13b2bc37 2022-10-23 stsp err = got_repo_open(&repo, repo_path, NULL, NULL);
1729 13b2bc37 2022-10-23 stsp goto done;
1731 13b2bc37 2022-10-23 stsp memcpy(old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
1732 13b2bc37 2022-10-23 stsp memcpy(new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
1733 13b2bc37 2022-10-23 stsp err = got_object_open(&obj, repo, &new_id);
1735 13b2bc37 2022-10-23 stsp goto done;
1737 13b2bc37 2022-10-23 stsp if (iref.ref_is_new) {
1738 13b2bc37 2022-10-23 stsp err = got_ref_open(&ref, repo, refname, 0);
1739 13b2bc37 2022-10-23 stsp if (err) {
1740 13b2bc37 2022-10-23 stsp if (err->code != GOT_ERR_NOT_REF)
1741 13b2bc37 2022-10-23 stsp goto done;
1742 13b2bc37 2022-10-23 stsp err = got_ref_alloc(&ref, refname, &new_id);
1744 13b2bc37 2022-10-23 stsp goto done;
1745 13b2bc37 2022-10-23 stsp err = got_ref_write(ref, repo); /* will lock/unlock */
1747 13b2bc37 2022-10-23 stsp goto done;
1749 13b2bc37 2022-10-23 stsp err = got_error_fmt(GOT_ERR_REF_BUSY,
1750 13b2bc37 2022-10-23 stsp "%s has been created by someone else "
1751 13b2bc37 2022-10-23 stsp "while transaction was in progress",
1752 13b2bc37 2022-10-23 stsp got_ref_get_name(ref));
1753 13b2bc37 2022-10-23 stsp goto done;
1756 13b2bc37 2022-10-23 stsp err = got_ref_open(&ref, repo, refname, 1 /* lock */);
1758 13b2bc37 2022-10-23 stsp goto done;
1759 13b2bc37 2022-10-23 stsp locked = 1;
1761 13b2bc37 2022-10-23 stsp err = got_ref_resolve(&id, repo, ref);
1763 13b2bc37 2022-10-23 stsp goto done;
1765 13b2bc37 2022-10-23 stsp if (got_object_id_cmp(id, &old_id) != 0) {
1766 13b2bc37 2022-10-23 stsp err = got_error_fmt(GOT_ERR_REF_BUSY,
1767 13b2bc37 2022-10-23 stsp "%s has been modified by someone else "
1768 13b2bc37 2022-10-23 stsp "while transaction was in progress",
1769 13b2bc37 2022-10-23 stsp got_ref_get_name(ref));
1770 13b2bc37 2022-10-23 stsp goto done;
1773 13b2bc37 2022-10-23 stsp err = got_ref_change_ref(ref, &new_id);
1775 13b2bc37 2022-10-23 stsp goto done;
1777 13b2bc37 2022-10-23 stsp err = got_ref_write(ref, repo);
1779 13b2bc37 2022-10-23 stsp goto done;
1782 13b2bc37 2022-10-23 stsp id = NULL;
1785 13b2bc37 2022-10-23 stsp if (err) {
1786 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_LOCKFILE_TIMEOUT) {
1787 13b2bc37 2022-10-23 stsp err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
1788 13b2bc37 2022-10-23 stsp "could not acquire exclusive file lock for %s",
1791 13b2bc37 2022-10-23 stsp send_ref_update_ng(client, &iref, refname, err->msg);
1793 13b2bc37 2022-10-23 stsp send_ref_update_ok(client, &iref, refname);
1795 13b2bc37 2022-10-23 stsp if (client->nref_updates > 0) {
1796 13b2bc37 2022-10-23 stsp client->nref_updates--;
1797 13b2bc37 2022-10-23 stsp if (client->nref_updates == 0)
1798 13b2bc37 2022-10-23 stsp send_refs_updated(client);
1801 13b2bc37 2022-10-23 stsp if (locked) {
1802 13b2bc37 2022-10-23 stsp const struct got_error *unlock_err;
1803 13b2bc37 2022-10-23 stsp unlock_err = got_ref_unlock(ref);
1804 13b2bc37 2022-10-23 stsp if (unlock_err && err == NULL)
1805 13b2bc37 2022-10-23 stsp err = unlock_err;
1808 13b2bc37 2022-10-23 stsp got_ref_close(ref);
1810 13b2bc37 2022-10-23 stsp got_object_close(obj);
1812 13b2bc37 2022-10-23 stsp got_repo_close(repo);
1813 13b2bc37 2022-10-23 stsp free(refname);
1815 13b2bc37 2022-10-23 stsp return err;
1818 13b2bc37 2022-10-23 stsp static void
1819 13b2bc37 2022-10-23 stsp gotd_dispatch(int fd, short event, void *arg)
1821 13b2bc37 2022-10-23 stsp struct gotd_imsgev *iev = arg;
1822 13b2bc37 2022-10-23 stsp struct imsgbuf *ibuf = &iev->ibuf;
1823 13b2bc37 2022-10-23 stsp struct gotd_child_proc *proc = NULL;
1824 13b2bc37 2022-10-23 stsp ssize_t n;
1825 13b2bc37 2022-10-23 stsp int shut = 0;
1826 13b2bc37 2022-10-23 stsp struct imsg imsg;
1828 13b2bc37 2022-10-23 stsp if (event & EV_READ) {
1829 13b2bc37 2022-10-23 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1830 13b2bc37 2022-10-23 stsp fatal("imsg_read error");
1831 13b2bc37 2022-10-23 stsp if (n == 0) {
1832 13b2bc37 2022-10-23 stsp /* Connection closed. */
1834 13b2bc37 2022-10-23 stsp goto done;
1838 13b2bc37 2022-10-23 stsp if (event & EV_WRITE) {
1839 13b2bc37 2022-10-23 stsp n = msgbuf_write(&ibuf->w);
1840 13b2bc37 2022-10-23 stsp if (n == -1 && errno != EAGAIN)
1841 13b2bc37 2022-10-23 stsp fatal("msgbuf_write");
1842 13b2bc37 2022-10-23 stsp if (n == 0) {
1843 13b2bc37 2022-10-23 stsp /* Connection closed. */
1845 13b2bc37 2022-10-23 stsp goto done;
1849 13b2bc37 2022-10-23 stsp proc = find_proc_by_fd(fd);
1850 13b2bc37 2022-10-23 stsp if (proc == NULL)
1851 13b2bc37 2022-10-23 stsp fatalx("cannot find child process for fd %d", fd);
1853 13b2bc37 2022-10-23 stsp for (;;) {
1854 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
1855 13b2bc37 2022-10-23 stsp struct gotd_client *client = NULL;
1856 13b2bc37 2022-10-23 stsp uint32_t client_id = 0;
1857 13b2bc37 2022-10-23 stsp int do_disconnect = 0;
1858 13b2bc37 2022-10-23 stsp int do_ref_updates = 0, do_ref_update = 0;
1859 13b2bc37 2022-10-23 stsp int do_packfile_install = 0;
1861 13b2bc37 2022-10-23 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
1862 13b2bc37 2022-10-23 stsp fatal("%s: imsg_get error", __func__);
1863 13b2bc37 2022-10-23 stsp if (n == 0) /* No more messages. */
1866 13b2bc37 2022-10-23 stsp switch (imsg.hdr.type) {
1867 13b2bc37 2022-10-23 stsp case GOTD_IMSG_ERROR:
1868 13b2bc37 2022-10-23 stsp do_disconnect = 1;
1869 13b2bc37 2022-10-23 stsp err = gotd_imsg_recv_error(&client_id, &imsg);
1871 d93ecf7d 2022-12-14 stsp case GOTD_IMSG_CONNECT:
1872 d93ecf7d 2022-12-14 stsp err = recv_connect(&client_id, &imsg);
1874 13b2bc37 2022-10-23 stsp case GOTD_IMSG_PACKFILE_DONE:
1875 13b2bc37 2022-10-23 stsp do_disconnect = 1;
1876 13b2bc37 2022-10-23 stsp err = recv_packfile_done(&client_id, &imsg);
1878 13b2bc37 2022-10-23 stsp case GOTD_IMSG_PACKFILE_INSTALL:
1879 13b2bc37 2022-10-23 stsp err = recv_packfile_install(&client_id, &imsg);
1880 13b2bc37 2022-10-23 stsp if (err == NULL)
1881 13b2bc37 2022-10-23 stsp do_packfile_install = 1;
1883 13b2bc37 2022-10-23 stsp case GOTD_IMSG_REF_UPDATES_START:
1884 13b2bc37 2022-10-23 stsp err = recv_ref_updates_start(&client_id, &imsg);
1885 13b2bc37 2022-10-23 stsp if (err == NULL)
1886 13b2bc37 2022-10-23 stsp do_ref_updates = 1;
1888 13b2bc37 2022-10-23 stsp case GOTD_IMSG_REF_UPDATE:
1889 13b2bc37 2022-10-23 stsp err = recv_ref_update(&client_id, &imsg);
1890 13b2bc37 2022-10-23 stsp if (err == NULL)
1891 13b2bc37 2022-10-23 stsp do_ref_update = 1;
1894 13b2bc37 2022-10-23 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1898 13b2bc37 2022-10-23 stsp client = find_client(client_id);
1899 13b2bc37 2022-10-23 stsp if (client == NULL) {
1900 13b2bc37 2022-10-23 stsp log_warnx("%s: client not found", __func__);
1901 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
1905 13b2bc37 2022-10-23 stsp if (!verify_imsg_src(client, proc, &imsg)) {
1906 13b2bc37 2022-10-23 stsp log_debug("dropping imsg type %d from PID %d",
1907 13b2bc37 2022-10-23 stsp imsg.hdr.type, proc->pid);
1908 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
1912 13b2bc37 2022-10-23 stsp log_warnx("uid %d: %s", client->euid, err->msg);
1914 13b2bc37 2022-10-23 stsp if (do_disconnect) {
1916 13b2bc37 2022-10-23 stsp disconnect_on_error(client, err);
1918 13b2bc37 2022-10-23 stsp disconnect(client);
1920 36c7cfbb 2022-11-04 stsp if (do_packfile_install)
1921 eec68231 2022-12-14 stsp err = install_pack(client, proc->repo_path,
1923 36c7cfbb 2022-11-04 stsp else if (do_ref_updates)
1924 36c7cfbb 2022-11-04 stsp err = begin_ref_updates(client, &imsg);
1925 36c7cfbb 2022-11-04 stsp else if (do_ref_update)
1926 eec68231 2022-12-14 stsp err = update_ref(client, proc->repo_path,
1929 36c7cfbb 2022-11-04 stsp log_warnx("uid %d: %s", client->euid, err->msg);
1931 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
1934 13b2bc37 2022-10-23 stsp if (!shut) {
1935 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(iev);
1937 13b2bc37 2022-10-23 stsp /* This pipe is dead. Remove its event handler */
1938 13b2bc37 2022-10-23 stsp event_del(&iev->ev);
1939 13b2bc37 2022-10-23 stsp event_loopexit(NULL);
1943 13b2bc37 2022-10-23 stsp static pid_t
1944 eec68231 2022-12-14 stsp start_child(enum gotd_procid proc_id, const char *repo_path,
1945 585362fd 2022-10-31 op char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
1947 585362fd 2022-10-31 op char *argv[11];
1948 13b2bc37 2022-10-23 stsp int argc = 0;
1949 13b2bc37 2022-10-23 stsp pid_t pid;
1951 13b2bc37 2022-10-23 stsp switch (pid = fork()) {
1953 13b2bc37 2022-10-23 stsp fatal("cannot fork");
1957 13b2bc37 2022-10-23 stsp close(fd);
1958 13b2bc37 2022-10-23 stsp return pid;
1961 8c6fc146 2022-11-17 stsp if (fd != GOTD_FILENO_MSG_PIPE) {
1962 8c6fc146 2022-11-17 stsp if (dup2(fd, GOTD_FILENO_MSG_PIPE) == -1)
1963 13b2bc37 2022-10-23 stsp fatal("cannot setup imsg fd");
1964 13b2bc37 2022-10-23 stsp } else if (fcntl(fd, F_SETFD, 0) == -1)
1965 13b2bc37 2022-10-23 stsp fatal("cannot setup imsg fd");
1967 13b2bc37 2022-10-23 stsp argv[argc++] = argv0;
1968 13b2bc37 2022-10-23 stsp switch (proc_id) {
1969 d93ecf7d 2022-12-14 stsp case PROC_LISTEN:
1970 d93ecf7d 2022-12-14 stsp argv[argc++] = (char *)"-L";
1972 13b2bc37 2022-10-23 stsp case PROC_REPO_READ:
1973 13b2bc37 2022-10-23 stsp argv[argc++] = (char *)"-R";
1975 13b2bc37 2022-10-23 stsp case PROC_REPO_WRITE:
1976 13b2bc37 2022-10-23 stsp argv[argc++] = (char *)"-W";
1979 13b2bc37 2022-10-23 stsp fatalx("invalid process id %d", proc_id);
1982 585362fd 2022-10-31 op argv[argc++] = (char *)"-f";
1983 585362fd 2022-10-31 op argv[argc++] = (char *)confpath;
1985 eec68231 2022-12-14 stsp if (repo_path) {
1986 d93ecf7d 2022-12-14 stsp argv[argc++] = (char *)"-P";
1987 eec68231 2022-12-14 stsp argv[argc++] = (char *)repo_path;
1990 13b2bc37 2022-10-23 stsp if (!daemonize)
1991 13b2bc37 2022-10-23 stsp argv[argc++] = (char *)"-d";
1992 13b2bc37 2022-10-23 stsp if (verbosity > 0)
1993 13b2bc37 2022-10-23 stsp argv[argc++] = (char *)"-v";
1994 13b2bc37 2022-10-23 stsp if (verbosity > 1)
1995 13b2bc37 2022-10-23 stsp argv[argc++] = (char *)"-v";
1996 13b2bc37 2022-10-23 stsp argv[argc++] = NULL;
1998 13b2bc37 2022-10-23 stsp execvp(argv0, argv);
1999 13b2bc37 2022-10-23 stsp fatal("execvp");
2002 13b2bc37 2022-10-23 stsp static void
2003 d93ecf7d 2022-12-14 stsp start_listener(char *argv0, const char *confpath, int daemonize, int verbosity)
2005 d93ecf7d 2022-12-14 stsp struct gotd_child_proc *proc = &gotd.procs[0];
2007 d93ecf7d 2022-12-14 stsp proc->type = PROC_LISTEN;
2009 d93ecf7d 2022-12-14 stsp if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
2010 d93ecf7d 2022-12-14 stsp PF_UNSPEC, proc->pipe) == -1)
2011 d93ecf7d 2022-12-14 stsp fatal("socketpair");
2013 d93ecf7d 2022-12-14 stsp proc->pid = start_child(proc->type, NULL, argv0, confpath,
2014 d93ecf7d 2022-12-14 stsp proc->pipe[1], daemonize, verbosity);
2015 d93ecf7d 2022-12-14 stsp imsg_init(&proc->iev.ibuf, proc->pipe[0]);
2016 d93ecf7d 2022-12-14 stsp proc->iev.handler = gotd_dispatch;
2017 d93ecf7d 2022-12-14 stsp proc->iev.events = EV_READ;
2018 d93ecf7d 2022-12-14 stsp proc->iev.handler_arg = NULL;
2021 d93ecf7d 2022-12-14 stsp static void
2022 585362fd 2022-10-31 op start_repo_children(struct gotd *gotd, char *argv0, const char *confpath,
2023 585362fd 2022-10-31 op int daemonize, int verbosity)
2025 13b2bc37 2022-10-23 stsp struct gotd_repo *repo = NULL;
2026 13b2bc37 2022-10-23 stsp struct gotd_child_proc *proc;
2029 d93ecf7d 2022-12-14 stsp for (i = 1; i < gotd->nprocs; i++) {
2030 13b2bc37 2022-10-23 stsp if (repo == NULL)
2031 13b2bc37 2022-10-23 stsp repo = TAILQ_FIRST(&gotd->repos);
2032 13b2bc37 2022-10-23 stsp proc = &gotd->procs[i];
2033 d93ecf7d 2022-12-14 stsp if (i - 1 < gotd->nrepos)
2034 13b2bc37 2022-10-23 stsp proc->type = PROC_REPO_READ;
2036 13b2bc37 2022-10-23 stsp proc->type = PROC_REPO_WRITE;
2037 13b2bc37 2022-10-23 stsp if (strlcpy(proc->repo_name, repo->name,
2038 13b2bc37 2022-10-23 stsp sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
2039 13b2bc37 2022-10-23 stsp fatalx("repository name too long: %s", repo->name);
2040 13b2bc37 2022-10-23 stsp log_debug("adding repository %s", repo->name);
2041 eec68231 2022-12-14 stsp if (realpath(repo->path, proc->repo_path) == NULL)
2042 13b2bc37 2022-10-23 stsp fatal("%s", repo->path);
2043 13b2bc37 2022-10-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
2044 13b2bc37 2022-10-23 stsp PF_UNSPEC, proc->pipe) == -1)
2045 13b2bc37 2022-10-23 stsp fatal("socketpair");
2046 eec68231 2022-12-14 stsp proc->pid = start_child(proc->type, proc->repo_path, argv0,
2047 585362fd 2022-10-31 op confpath, proc->pipe[1], daemonize, verbosity);
2048 13b2bc37 2022-10-23 stsp imsg_init(&proc->iev.ibuf, proc->pipe[0]);
2049 13b2bc37 2022-10-23 stsp log_debug("proc %s %s is on fd %d",
2050 eec68231 2022-12-14 stsp gotd_proc_names[proc->type], proc->repo_path,
2051 13b2bc37 2022-10-23 stsp proc->pipe[0]);
2052 13b2bc37 2022-10-23 stsp proc->iev.handler = gotd_dispatch;
2053 13b2bc37 2022-10-23 stsp proc->iev.events = EV_READ;
2054 13b2bc37 2022-10-23 stsp proc->iev.handler_arg = NULL;
2055 13b2bc37 2022-10-23 stsp event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
2056 13b2bc37 2022-10-23 stsp gotd_dispatch, &proc->iev);
2058 13b2bc37 2022-10-23 stsp repo = TAILQ_NEXT(repo, entry);
2062 eec68231 2022-12-14 stsp static void
2063 eec68231 2022-12-14 stsp apply_unveil_repo_readonly(const char *repo_path)
2065 eec68231 2022-12-14 stsp if (unveil(repo_path, "r") == -1)
2066 eec68231 2022-12-14 stsp fatal("unveil %s", repo_path);
2068 eec68231 2022-12-14 stsp if (unveil(NULL, NULL) == -1)
2069 eec68231 2022-12-14 stsp fatal("unveil");
2072 13b2bc37 2022-10-23 stsp static void
2073 13b2bc37 2022-10-23 stsp apply_unveil(void)
2075 13b2bc37 2022-10-23 stsp struct gotd_repo *repo;
2077 13b2bc37 2022-10-23 stsp TAILQ_FOREACH(repo, &gotd.repos, entry) {
2078 13b2bc37 2022-10-23 stsp if (unveil(repo->path, "rwc") == -1)
2079 13b2bc37 2022-10-23 stsp fatal("unveil %s", repo->path);
2082 13b2bc37 2022-10-23 stsp if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
2083 13b2bc37 2022-10-23 stsp fatal("unveil %s", GOT_TMPDIR_STR);
2085 13b2bc37 2022-10-23 stsp if (unveil(NULL, NULL) == -1)
2086 13b2bc37 2022-10-23 stsp fatal("unveil");
2090 13b2bc37 2022-10-23 stsp main(int argc, char **argv)
2092 13b2bc37 2022-10-23 stsp const struct got_error *error = NULL;
2093 13b2bc37 2022-10-23 stsp int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
2094 13b2bc37 2022-10-23 stsp const char *confpath = GOTD_CONF_PATH;
2095 13b2bc37 2022-10-23 stsp char *argv0 = argv[0];
2096 13b2bc37 2022-10-23 stsp char title[2048];
2097 ddbe612c 2022-11-17 stsp gid_t groups[NGROUPS_MAX];
2098 0ccf3acb 2022-11-16 stsp int ngroups = NGROUPS_MAX;
2099 13b2bc37 2022-10-23 stsp struct passwd *pw = NULL;
2100 13b2bc37 2022-10-23 stsp struct group *gr = NULL;
2101 13b2bc37 2022-10-23 stsp char *repo_path = NULL;
2102 13b2bc37 2022-10-23 stsp enum gotd_procid proc_id = PROC_GOTD;
2103 13b2bc37 2022-10-23 stsp struct event evsigint, evsigterm, evsighup, evsigusr1;
2104 13b2bc37 2022-10-23 stsp int *pack_fds = NULL, *temp_fds = NULL;
2106 13b2bc37 2022-10-23 stsp log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
2108 d93ecf7d 2022-12-14 stsp while ((ch = getopt(argc, argv, "df:LnP:RvW")) != -1) {
2109 13b2bc37 2022-10-23 stsp switch (ch) {
2111 13b2bc37 2022-10-23 stsp daemonize = 0;
2114 13b2bc37 2022-10-23 stsp confpath = optarg;
2117 d93ecf7d 2022-12-14 stsp proc_id = PROC_LISTEN;
2120 13b2bc37 2022-10-23 stsp noaction = 1;
2123 6f319063 2022-10-27 stsp repo_path = realpath(optarg, NULL);
2124 6f319063 2022-10-27 stsp if (repo_path == NULL)
2125 6f319063 2022-10-27 stsp fatal("realpath '%s'", optarg);
2128 13b2bc37 2022-10-23 stsp proc_id = PROC_REPO_READ;
2131 6f319063 2022-10-27 stsp if (verbosity < 3)
2132 6f319063 2022-10-27 stsp verbosity++;
2135 13b2bc37 2022-10-23 stsp proc_id = PROC_REPO_WRITE;
2142 13b2bc37 2022-10-23 stsp argc -= optind;
2143 13b2bc37 2022-10-23 stsp argv += optind;
2145 13b2bc37 2022-10-23 stsp if (argc != 0)
2148 13b2bc37 2022-10-23 stsp if (geteuid())
2149 13b2bc37 2022-10-23 stsp fatalx("need root privileges");
2151 13b2bc37 2022-10-23 stsp log_init(daemonize ? 0 : 1, LOG_DAEMON);
2152 13b2bc37 2022-10-23 stsp log_setverbose(verbosity);
2154 13b2bc37 2022-10-23 stsp if (parse_config(confpath, proc_id, &gotd) != 0)
2157 13b2bc37 2022-10-23 stsp if (proc_id == PROC_GOTD &&
2158 13b2bc37 2022-10-23 stsp (gotd.nrepos == 0 || TAILQ_EMPTY(&gotd.repos)))
2159 13b2bc37 2022-10-23 stsp fatalx("no repository defined in configuration file");
2161 13b2bc37 2022-10-23 stsp pw = getpwnam(gotd.user_name);
2162 13b2bc37 2022-10-23 stsp if (pw == NULL)
2163 13b2bc37 2022-10-23 stsp fatal("getpwuid: user %s not found", gotd.user_name);
2165 13b2bc37 2022-10-23 stsp if (pw->pw_uid == 0) {
2166 13b2bc37 2022-10-23 stsp fatalx("cannot run %s as %s: the user running %s "
2167 13b2bc37 2022-10-23 stsp "must not be the superuser",
2168 13b2bc37 2022-10-23 stsp getprogname(), pw->pw_name, getprogname());
2171 ddbe612c 2022-11-17 stsp if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups) == -1)
2172 13b2bc37 2022-10-23 stsp log_warnx("group membership list truncated");
2174 ddbe612c 2022-11-17 stsp gr = match_group(groups, ngroups, gotd.unix_group_name);
2175 13b2bc37 2022-10-23 stsp if (gr == NULL) {
2176 13b2bc37 2022-10-23 stsp fatalx("cannot start %s: the user running %s "
2177 13b2bc37 2022-10-23 stsp "must be a secondary member of group %s",
2178 13b2bc37 2022-10-23 stsp getprogname(), getprogname(), gotd.unix_group_name);
2180 13b2bc37 2022-10-23 stsp if (gr->gr_gid == pw->pw_gid) {
2181 13b2bc37 2022-10-23 stsp fatalx("cannot start %s: the user running %s "
2182 13b2bc37 2022-10-23 stsp "must be a secondary member of group %s, but "
2183 13b2bc37 2022-10-23 stsp "%s is the user's primary group",
2184 13b2bc37 2022-10-23 stsp getprogname(), getprogname(), gotd.unix_group_name,
2185 13b2bc37 2022-10-23 stsp gotd.unix_group_name);
2188 d93ecf7d 2022-12-14 stsp if (proc_id == PROC_LISTEN &&
2189 13b2bc37 2022-10-23 stsp !got_path_is_absolute(gotd.unix_socket_path))
2190 13b2bc37 2022-10-23 stsp fatalx("bad unix socket path \"%s\": must be an absolute path",
2191 13b2bc37 2022-10-23 stsp gotd.unix_socket_path);
2193 13b2bc37 2022-10-23 stsp if (noaction)
2196 b1142068 2022-12-05 stsp if (proc_id == PROC_GOTD) {
2197 d93ecf7d 2022-12-14 stsp gotd.pid = getpid();
2198 d93ecf7d 2022-12-14 stsp snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2200 d93ecf7d 2022-12-14 stsp * Start a listener and repository readers/writers.
2201 d93ecf7d 2022-12-14 stsp * XXX For now, use one reader and one writer per repository.
2202 d93ecf7d 2022-12-14 stsp * This should be changed to N readers + M writers.
2204 d93ecf7d 2022-12-14 stsp gotd.nprocs = 1 + gotd.nrepos * 2;
2205 d93ecf7d 2022-12-14 stsp gotd.procs = calloc(gotd.nprocs, sizeof(*gotd.procs));
2206 d93ecf7d 2022-12-14 stsp if (gotd.procs == NULL)
2207 d93ecf7d 2022-12-14 stsp fatal("calloc");
2208 d93ecf7d 2022-12-14 stsp start_listener(argv0, confpath, daemonize, verbosity);
2209 d93ecf7d 2022-12-14 stsp start_repo_children(&gotd, argv0, confpath, daemonize,
2210 d93ecf7d 2022-12-14 stsp verbosity);
2211 d93ecf7d 2022-12-14 stsp arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
2212 d93ecf7d 2022-12-14 stsp if (daemonize && daemon(1, 0) == -1)
2213 d93ecf7d 2022-12-14 stsp fatal("daemon");
2214 d93ecf7d 2022-12-14 stsp } else if (proc_id == PROC_LISTEN) {
2215 d93ecf7d 2022-12-14 stsp snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2216 b1142068 2022-12-05 stsp if (verbosity) {
2217 b1142068 2022-12-05 stsp log_info("socket: %s", gotd.unix_socket_path);
2218 b1142068 2022-12-05 stsp log_info("user: %s", pw->pw_name);
2219 b1142068 2022-12-05 stsp log_info("secondary group: %s", gr->gr_name);
2222 13b2bc37 2022-10-23 stsp fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
2223 13b2bc37 2022-10-23 stsp gr->gr_gid);
2224 13b2bc37 2022-10-23 stsp if (fd == -1) {
2225 13b2bc37 2022-10-23 stsp fatal("cannot listen on unix socket %s",
2226 13b2bc37 2022-10-23 stsp gotd.unix_socket_path);
2228 eec68231 2022-12-14 stsp if (daemonize && daemon(0, 0) == -1)
2229 13b2bc37 2022-10-23 stsp fatal("daemon");
2230 13b2bc37 2022-10-23 stsp } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE) {
2231 13b2bc37 2022-10-23 stsp error = got_repo_pack_fds_open(&pack_fds);
2232 13b2bc37 2022-10-23 stsp if (error != NULL)
2233 13b2bc37 2022-10-23 stsp fatalx("cannot open pack tempfiles: %s", error->msg);
2234 13b2bc37 2022-10-23 stsp error = got_repo_temp_fds_open(&temp_fds);
2235 13b2bc37 2022-10-23 stsp if (error != NULL)
2236 13b2bc37 2022-10-23 stsp fatalx("cannot open pack tempfiles: %s", error->msg);
2237 13b2bc37 2022-10-23 stsp if (repo_path == NULL)
2238 13b2bc37 2022-10-23 stsp fatalx("repository path not specified");
2239 13b2bc37 2022-10-23 stsp snprintf(title, sizeof(title), "%s %s",
2240 13b2bc37 2022-10-23 stsp gotd_proc_names[proc_id], repo_path);
2241 eec68231 2022-12-14 stsp if (daemonize && daemon(0, 0) == -1)
2242 13b2bc37 2022-10-23 stsp fatal("daemon");
2244 13b2bc37 2022-10-23 stsp fatal("invalid process id %d", proc_id);
2246 13b2bc37 2022-10-23 stsp setproctitle("%s", title);
2247 13b2bc37 2022-10-23 stsp log_procinit(title);
2249 13b2bc37 2022-10-23 stsp /* Drop root privileges. */
2250 13b2bc37 2022-10-23 stsp if (setgid(pw->pw_gid) == -1)
2251 13b2bc37 2022-10-23 stsp fatal("setgid %d failed", pw->pw_gid);
2252 13b2bc37 2022-10-23 stsp if (setuid(pw->pw_uid) == -1)
2253 13b2bc37 2022-10-23 stsp fatal("setuid %d failed", pw->pw_uid);
2255 13b2bc37 2022-10-23 stsp event_init();
2257 13b2bc37 2022-10-23 stsp switch (proc_id) {
2258 13b2bc37 2022-10-23 stsp case PROC_GOTD:
2259 13b2bc37 2022-10-23 stsp #ifndef PROFILE
2260 13b2bc37 2022-10-23 stsp if (pledge("stdio rpath wpath cpath proc getpw sendfd recvfd "
2261 13b2bc37 2022-10-23 stsp "fattr flock unix unveil", NULL) == -1)
2262 13b2bc37 2022-10-23 stsp err(1, "pledge");
2265 d93ecf7d 2022-12-14 stsp case PROC_LISTEN:
2266 d93ecf7d 2022-12-14 stsp #ifndef PROFILE
2267 d93ecf7d 2022-12-14 stsp if (pledge("stdio sendfd unix", NULL) == -1)
2268 d93ecf7d 2022-12-14 stsp err(1, "pledge");
2270 d93ecf7d 2022-12-14 stsp listen_main(title, fd);
2271 d93ecf7d 2022-12-14 stsp /* NOTREACHED */
2273 13b2bc37 2022-10-23 stsp case PROC_REPO_READ:
2274 13b2bc37 2022-10-23 stsp #ifndef PROFILE
2275 eec68231 2022-12-14 stsp if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2276 13b2bc37 2022-10-23 stsp err(1, "pledge");
2278 eec68231 2022-12-14 stsp apply_unveil_repo_readonly(repo_path);
2279 eec68231 2022-12-14 stsp repo_read_main(title, repo_path, pack_fds, temp_fds);
2280 13b2bc37 2022-10-23 stsp /* NOTREACHED */
2282 13b2bc37 2022-10-23 stsp case PROC_REPO_WRITE:
2283 13b2bc37 2022-10-23 stsp #ifndef PROFILE
2284 eec68231 2022-12-14 stsp if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2285 13b2bc37 2022-10-23 stsp err(1, "pledge");
2287 eec68231 2022-12-14 stsp apply_unveil_repo_readonly(repo_path);
2288 eec68231 2022-12-14 stsp repo_write_main(title, repo_path, pack_fds, temp_fds);
2289 13b2bc37 2022-10-23 stsp /* NOTREACHED */
2292 13b2bc37 2022-10-23 stsp fatal("invalid process id %d", proc_id);
2295 13b2bc37 2022-10-23 stsp if (proc_id != PROC_GOTD)
2296 13b2bc37 2022-10-23 stsp fatal("invalid process id %d", proc_id);
2298 13b2bc37 2022-10-23 stsp apply_unveil();
2300 13b2bc37 2022-10-23 stsp signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
2301 13b2bc37 2022-10-23 stsp signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
2302 13b2bc37 2022-10-23 stsp signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
2303 13b2bc37 2022-10-23 stsp signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
2304 13b2bc37 2022-10-23 stsp signal(SIGPIPE, SIG_IGN);
2306 13b2bc37 2022-10-23 stsp signal_add(&evsigint, NULL);
2307 13b2bc37 2022-10-23 stsp signal_add(&evsigterm, NULL);
2308 13b2bc37 2022-10-23 stsp signal_add(&evsighup, NULL);
2309 13b2bc37 2022-10-23 stsp signal_add(&evsigusr1, NULL);
2311 d93ecf7d 2022-12-14 stsp gotd_imsg_event_add(&gotd.procs[0].iev);
2313 13b2bc37 2022-10-23 stsp event_dispatch();
2315 13b2bc37 2022-10-23 stsp if (pack_fds)
2316 13b2bc37 2022-10-23 stsp got_repo_pack_fds_close(pack_fds);
2317 13b2bc37 2022-10-23 stsp free(repo_path);