Blob


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