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 <grp.h>
33 #include <imsg.h>
34 #include <sha1.h>
35 #include <signal.h>
36 #include <siphash.h>
37 #include <stdarg.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <syslog.h>
42 #include <unistd.h>
44 #include "got_error.h"
45 #include "got_opentemp.h"
46 #include "got_path.h"
47 #include "got_repository.h"
48 #include "got_object.h"
49 #include "got_reference.h"
51 #include "got_lib_delta.h"
52 #include "got_lib_object.h"
53 #include "got_lib_object_cache.h"
54 #include "got_lib_sha1.h"
55 #include "got_lib_gitproto.h"
56 #include "got_lib_pack.h"
57 #include "got_lib_repository.h"
59 #include "gotd.h"
60 #include "log.h"
61 #include "listen.h"
62 #include "auth.h"
63 #include "repo_read.h"
64 #include "repo_write.h"
66 #ifndef nitems
67 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
68 #endif
70 struct gotd_client {
71 STAILQ_ENTRY(gotd_client) entry;
72 enum gotd_client_state state;
73 struct gotd_client_capability *capabilities;
74 size_t ncapa_alloc;
75 size_t ncapabilities;
76 uint32_t id;
77 int fd;
78 int delta_cache_fd;
79 struct gotd_imsgev iev;
80 struct event tmo;
81 uid_t euid;
82 gid_t egid;
83 struct gotd_child_proc *repo_read;
84 struct gotd_child_proc *repo_write;
85 struct gotd_child_proc *auth;
86 int required_auth;
87 char *packfile_path;
88 char *packidx_path;
89 int nref_updates;
90 };
91 STAILQ_HEAD(gotd_clients, gotd_client);
93 static struct gotd_clients gotd_clients[GOTD_CLIENT_TABLE_SIZE];
94 static SIPHASH_KEY clients_hash_key;
95 volatile int client_cnt;
96 static struct timeval timeout = { 3600, 0 };
97 static struct timeval auth_timeout = { 5, 0 };
98 static struct gotd gotd;
100 void gotd_sighdlr(int sig, short event, void *arg);
101 static void gotd_shutdown(void);
102 static const struct got_error *start_repo_child(struct gotd_client *,
103 enum gotd_procid, struct gotd_repo *, char *, const char *, int, int);
104 static const struct got_error *start_auth_child(struct gotd_client *, int,
105 struct gotd_repo *, char *, const char *, int, int);
106 static void kill_proc(struct gotd_child_proc *, int);
108 __dead static void
109 usage()
111 fprintf(stderr, "usage: %s [-dv] [-f config-file]\n", getprogname());
112 exit(1);
115 static int
116 unix_socket_listen(const char *unix_socket_path, uid_t uid, gid_t gid)
118 struct sockaddr_un sun;
119 int fd = -1;
120 mode_t old_umask, mode;
122 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
123 if (fd == -1) {
124 log_warn("socket");
125 return -1;
128 sun.sun_family = AF_UNIX;
129 if (strlcpy(sun.sun_path, unix_socket_path,
130 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
131 log_warnx("%s: name too long", unix_socket_path);
132 close(fd);
133 return -1;
136 if (unlink(unix_socket_path) == -1) {
137 if (errno != ENOENT) {
138 log_warn("unlink %s", unix_socket_path);
139 close(fd);
140 return -1;
144 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
145 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
147 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
148 log_warn("bind: %s", unix_socket_path);
149 close(fd);
150 umask(old_umask);
151 return -1;
154 umask(old_umask);
156 if (chmod(unix_socket_path, mode) == -1) {
157 log_warn("chmod %o %s", mode, unix_socket_path);
158 close(fd);
159 unlink(unix_socket_path);
160 return -1;
163 if (chown(unix_socket_path, uid, gid) == -1) {
164 log_warn("chown %s uid=%d gid=%d", unix_socket_path, uid, gid);
165 close(fd);
166 unlink(unix_socket_path);
167 return -1;
170 if (listen(fd, GOTD_UNIX_SOCKET_BACKLOG) == -1) {
171 log_warn("listen");
172 close(fd);
173 unlink(unix_socket_path);
174 return -1;
177 return fd;
180 static struct group *
181 match_group(gid_t *groups, int ngroups, const char *unix_group_name)
183 struct group *gr;
184 int i;
186 for (i = 0; i < ngroups; i++) {
187 gr = getgrgid(groups[i]);
188 if (gr == NULL) {
189 log_warn("getgrgid %d", groups[i]);
190 continue;
192 if (strcmp(gr->gr_name, unix_group_name) == 0)
193 return gr;
196 return NULL;
199 static uint64_t
200 client_hash(uint32_t client_id)
202 return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
205 static void
206 add_client(struct gotd_client *client)
208 uint64_t slot = client_hash(client->id) % nitems(gotd_clients);
209 STAILQ_INSERT_HEAD(&gotd_clients[slot], client, entry);
210 client_cnt++;
213 static struct gotd_client *
214 find_client(uint32_t client_id)
216 uint64_t slot;
217 struct gotd_client *c;
219 slot = client_hash(client_id) % nitems(gotd_clients);
220 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
221 if (c->id == client_id)
222 return c;
225 return NULL;
228 static struct gotd_child_proc *
229 get_client_proc(struct gotd_client *client)
231 if (client->repo_read && client->repo_write) {
232 fatalx("uid %d is reading and writing in the same session",
233 client->euid);
234 /* NOTREACHED */
237 if (client->repo_read)
238 return client->repo_read;
239 else if (client->repo_write)
240 return client->repo_write;
242 return NULL;
245 static struct gotd_client *
246 find_client_by_proc_fd(int fd)
248 uint64_t slot;
250 for (slot = 0; slot < nitems(gotd_clients); slot++) {
251 struct gotd_client *c;
253 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
254 struct gotd_child_proc *proc = get_client_proc(c);
255 if (proc && proc->iev.ibuf.fd == fd)
256 return c;
257 if (c->auth && c->auth->iev.ibuf.fd == fd)
258 return c;
262 return NULL;
265 static int
266 client_is_reading(struct gotd_client *client)
268 return client->repo_read != NULL;
271 static int
272 client_is_writing(struct gotd_client *client)
274 return client->repo_write != NULL;
277 static const struct got_error *
278 ensure_client_is_reading(struct gotd_client *client)
280 if (!client_is_reading(client)) {
281 return got_error_fmt(GOT_ERR_BAD_PACKET,
282 "uid %d made a read-request but is not reading from "
283 "a repository", client->euid);
286 return NULL;
289 static const struct got_error *
290 ensure_client_is_writing(struct gotd_client *client)
292 if (!client_is_writing(client)) {
293 return got_error_fmt(GOT_ERR_BAD_PACKET,
294 "uid %d made a write-request but is not writing to "
295 "a repository", client->euid);
298 return NULL;
301 static const struct got_error *
302 ensure_client_is_not_writing(struct gotd_client *client)
304 if (client_is_writing(client)) {
305 return got_error_fmt(GOT_ERR_BAD_PACKET,
306 "uid %d made a read-request but is writing to "
307 "a repository", client->euid);
310 return NULL;
313 static const struct got_error *
314 ensure_client_is_not_reading(struct gotd_client *client)
316 if (client_is_reading(client)) {
317 return got_error_fmt(GOT_ERR_BAD_PACKET,
318 "uid %d made a write-request but is reading from "
319 "a repository", client->euid);
322 return NULL;
325 static void
326 wait_for_child(pid_t child_pid)
328 pid_t pid;
329 int status;
331 log_debug("waiting for child PID %ld to terminate",
332 (long)child_pid);
334 do {
335 pid = waitpid(child_pid, &status, WNOHANG);
336 if (pid == -1) {
337 if (errno != EINTR && errno != ECHILD)
338 fatal("wait");
339 } else if (WIFSIGNALED(status)) {
340 log_warnx("child PID %ld terminated; signal %d",
341 (long)pid, WTERMSIG(status));
343 } while (pid != -1 || (pid == -1 && errno == EINTR));
346 static void
347 kill_auth_proc(struct gotd_client *client)
349 struct gotd_child_proc *proc;
351 if (client->auth == NULL)
352 return;
354 proc = client->auth;
355 client->auth = NULL;
357 event_del(&proc->iev.ev);
358 msgbuf_clear(&proc->iev.ibuf.w);
359 close(proc->iev.ibuf.fd);
360 kill_proc(proc, 0);
361 wait_for_child(proc->pid);
362 free(proc);
365 static void
366 disconnect(struct gotd_client *client)
368 struct gotd_imsg_disconnect idisconnect;
369 struct gotd_child_proc *proc = get_client_proc(client);
370 struct gotd_child_proc *listen_proc = &gotd.listen_proc;
371 uint64_t slot;
373 log_debug("uid %d: disconnecting", client->euid);
375 kill_auth_proc(client);
377 idisconnect.client_id = client->id;
378 if (proc) {
379 if (gotd_imsg_compose_event(&proc->iev,
380 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
381 &idisconnect, sizeof(idisconnect)) == -1)
382 log_warn("imsg compose DISCONNECT");
384 msgbuf_clear(&proc->iev.ibuf.w);
385 close(proc->iev.ibuf.fd);
386 kill_proc(proc, 0);
387 wait_for_child(proc->pid);
388 free(proc);
389 proc = NULL;
392 if (gotd_imsg_compose_event(&listen_proc->iev,
393 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
394 &idisconnect, sizeof(idisconnect)) == -1)
395 log_warn("imsg compose DISCONNECT");
397 slot = client_hash(client->id) % nitems(gotd_clients);
398 STAILQ_REMOVE(&gotd_clients[slot], client, gotd_client, entry);
399 imsg_clear(&client->iev.ibuf);
400 event_del(&client->iev.ev);
401 evtimer_del(&client->tmo);
402 close(client->fd);
403 if (client->delta_cache_fd != -1)
404 close(client->delta_cache_fd);
405 if (client->packfile_path) {
406 if (unlink(client->packfile_path) == -1 && errno != ENOENT)
407 log_warn("unlink %s: ", client->packfile_path);
408 free(client->packfile_path);
410 if (client->packidx_path) {
411 if (unlink(client->packidx_path) == -1 && errno != ENOENT)
412 log_warn("unlink %s: ", client->packidx_path);
413 free(client->packidx_path);
415 free(client->capabilities);
416 free(client);
417 client_cnt--;
420 static void
421 disconnect_on_error(struct gotd_client *client, const struct got_error *err)
423 struct imsgbuf ibuf;
425 log_warnx("uid %d: %s", client->euid, err->msg);
426 if (err->code != GOT_ERR_EOF) {
427 imsg_init(&ibuf, client->fd);
428 gotd_imsg_send_error(&ibuf, 0, PROC_GOTD, err);
429 imsg_clear(&ibuf);
431 disconnect(client);
434 static const struct got_error *
435 send_repo_info(struct gotd_imsgev *iev, struct gotd_repo *repo)
437 const struct got_error *err = NULL;
438 struct gotd_imsg_info_repo irepo;
440 memset(&irepo, 0, sizeof(irepo));
442 if (strlcpy(irepo.repo_name, repo->name, sizeof(irepo.repo_name))
443 >= sizeof(irepo.repo_name))
444 return got_error_msg(GOT_ERR_NO_SPACE, "repo name too long");
445 if (strlcpy(irepo.repo_path, repo->path, sizeof(irepo.repo_path))
446 >= sizeof(irepo.repo_path))
447 return got_error_msg(GOT_ERR_NO_SPACE, "repo path too long");
449 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_REPO, PROC_GOTD, -1,
450 &irepo, sizeof(irepo)) == -1) {
451 err = got_error_from_errno("imsg compose INFO_REPO");
452 if (err)
453 return err;
456 return NULL;
459 static const struct got_error *
460 send_capability(struct gotd_client_capability *capa, struct gotd_imsgev* iev)
462 const struct got_error *err = NULL;
463 struct gotd_imsg_capability icapa;
464 size_t len;
465 struct ibuf *wbuf;
467 memset(&icapa, 0, sizeof(icapa));
469 icapa.key_len = strlen(capa->key);
470 len = sizeof(icapa) + icapa.key_len;
471 if (capa->value) {
472 icapa.value_len = strlen(capa->value);
473 len += icapa.value_len;
476 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_CAPABILITY, 0, 0, len);
477 if (wbuf == NULL) {
478 err = got_error_from_errno("imsg_create CAPABILITY");
479 return err;
482 if (imsg_add(wbuf, &icapa, sizeof(icapa)) == -1)
483 return got_error_from_errno("imsg_add CAPABILITY");
484 if (imsg_add(wbuf, capa->key, icapa.key_len) == -1)
485 return got_error_from_errno("imsg_add CAPABILITY");
486 if (capa->value) {
487 if (imsg_add(wbuf, capa->value, icapa.value_len) == -1)
488 return got_error_from_errno("imsg_add CAPABILITY");
491 wbuf->fd = -1;
492 imsg_close(&iev->ibuf, wbuf);
494 gotd_imsg_event_add(iev);
496 return NULL;
499 static const struct got_error *
500 send_client_info(struct gotd_imsgev *iev, struct gotd_client *client)
502 const struct got_error *err = NULL;
503 struct gotd_imsg_info_client iclient;
504 struct gotd_child_proc *proc;
505 size_t i;
507 memset(&iclient, 0, sizeof(iclient));
508 iclient.euid = client->euid;
509 iclient.egid = client->egid;
511 proc = get_client_proc(client);
512 if (proc) {
513 if (strlcpy(iclient.repo_name, proc->repo_path,
514 sizeof(iclient.repo_name)) >= sizeof(iclient.repo_name)) {
515 return got_error_msg(GOT_ERR_NO_SPACE,
516 "repo name too long");
518 if (client_is_writing(client))
519 iclient.is_writing = 1;
522 iclient.state = client->state;
523 iclient.ncapabilities = client->ncapabilities;
525 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_CLIENT, PROC_GOTD, -1,
526 &iclient, sizeof(iclient)) == -1) {
527 err = got_error_from_errno("imsg compose INFO_CLIENT");
528 if (err)
529 return err;
532 for (i = 0; i < client->ncapabilities; i++) {
533 struct gotd_client_capability *capa;
534 capa = &client->capabilities[i];
535 err = send_capability(capa, iev);
536 if (err)
537 return err;
540 return NULL;
543 static const struct got_error *
544 send_info(struct gotd_client *client)
546 const struct got_error *err = NULL;
547 struct gotd_imsg_info info;
548 uint64_t slot;
549 struct gotd_repo *repo;
551 info.pid = gotd.pid;
552 info.verbosity = gotd.verbosity;
553 info.nrepos = gotd.nrepos;
554 info.nclients = client_cnt - 1;
556 if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_INFO, PROC_GOTD, -1,
557 &info, sizeof(info)) == -1) {
558 err = got_error_from_errno("imsg compose INFO");
559 if (err)
560 return err;
563 TAILQ_FOREACH(repo, &gotd.repos, entry) {
564 err = send_repo_info(&client->iev, repo);
565 if (err)
566 return err;
569 for (slot = 0; slot < nitems(gotd_clients); slot++) {
570 struct gotd_client *c;
571 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
572 if (c->id == client->id)
573 continue;
574 err = send_client_info(&client->iev, c);
575 if (err)
576 return err;
580 return NULL;
583 static const struct got_error *
584 stop_gotd(struct gotd_client *client)
587 if (client->euid != 0)
588 return got_error_set_errno(EPERM, "stop");
590 gotd_shutdown();
591 /* NOTREACHED */
592 return NULL;
595 static struct gotd_repo *
596 find_repo_by_name(const char *repo_name)
598 struct gotd_repo *repo;
599 size_t namelen;
601 TAILQ_FOREACH(repo, &gotd.repos, entry) {
602 namelen = strlen(repo->name);
603 if (strncmp(repo->name, repo_name, namelen) != 0)
604 continue;
605 if (repo_name[namelen] == '\0' ||
606 strcmp(&repo_name[namelen], ".git") == 0)
607 return repo;
610 return NULL;
613 static const struct got_error *
614 start_client_session(struct gotd_client *client, struct imsg *imsg)
616 const struct got_error *err;
617 struct gotd_imsg_list_refs ireq;
618 struct gotd_repo *repo = NULL;
619 size_t datalen;
621 log_debug("list-refs request from uid %d", client->euid);
623 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
624 if (datalen != sizeof(ireq))
625 return got_error(GOT_ERR_PRIVSEP_LEN);
627 memcpy(&ireq, imsg->data, datalen);
629 if (ireq.client_is_reading) {
630 err = ensure_client_is_not_writing(client);
631 if (err)
632 return err;
633 repo = find_repo_by_name(ireq.repo_name);
634 if (repo == NULL)
635 return got_error(GOT_ERR_NOT_GIT_REPO);
636 err = start_auth_child(client, GOTD_AUTH_READ, repo,
637 gotd.argv0, gotd.confpath, gotd.daemonize,
638 gotd.verbosity);
639 if (err)
640 return err;
641 } else {
642 err = ensure_client_is_not_reading(client);
643 if (err)
644 return err;
645 repo = find_repo_by_name(ireq.repo_name);
646 if (repo == NULL)
647 return got_error(GOT_ERR_NOT_GIT_REPO);
648 err = start_auth_child(client,
649 GOTD_AUTH_READ | GOTD_AUTH_WRITE,
650 repo, gotd.argv0, gotd.confpath, gotd.daemonize,
651 gotd.verbosity);
652 if (err)
653 return err;
656 /* Flow continues upon authentication successs/failure or timeout. */
657 return NULL;
660 static const struct got_error *
661 forward_want(struct gotd_client *client, struct imsg *imsg)
663 struct gotd_imsg_want ireq;
664 struct gotd_imsg_want iwant;
665 size_t datalen;
667 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
668 if (datalen != sizeof(ireq))
669 return got_error(GOT_ERR_PRIVSEP_LEN);
671 memcpy(&ireq, imsg->data, datalen);
673 memset(&iwant, 0, sizeof(iwant));
674 memcpy(iwant.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
675 iwant.client_id = client->id;
677 if (gotd_imsg_compose_event(&client->repo_read->iev, GOTD_IMSG_WANT,
678 PROC_GOTD, -1, &iwant, sizeof(iwant)) == -1)
679 return got_error_from_errno("imsg compose WANT");
681 return NULL;
684 static const struct got_error *
685 forward_ref_update(struct gotd_client *client, struct imsg *imsg)
687 const struct got_error *err = NULL;
688 struct gotd_imsg_ref_update ireq;
689 struct gotd_imsg_ref_update *iref = NULL;
690 size_t datalen;
692 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
693 if (datalen < sizeof(ireq))
694 return got_error(GOT_ERR_PRIVSEP_LEN);
695 memcpy(&ireq, imsg->data, sizeof(ireq));
696 if (datalen != sizeof(ireq) + ireq.name_len)
697 return got_error(GOT_ERR_PRIVSEP_LEN);
699 iref = malloc(datalen);
700 if (iref == NULL)
701 return got_error_from_errno("malloc");
702 memcpy(iref, imsg->data, datalen);
704 iref->client_id = client->id;
705 if (gotd_imsg_compose_event(&client->repo_write->iev,
706 GOTD_IMSG_REF_UPDATE, PROC_GOTD, -1, iref, datalen) == -1)
707 err = got_error_from_errno("imsg compose REF_UPDATE");
708 free(iref);
709 return err;
712 static const struct got_error *
713 forward_have(struct gotd_client *client, struct imsg *imsg)
715 struct gotd_imsg_have ireq;
716 struct gotd_imsg_have ihave;
717 size_t datalen;
719 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
720 if (datalen != sizeof(ireq))
721 return got_error(GOT_ERR_PRIVSEP_LEN);
723 memcpy(&ireq, imsg->data, datalen);
725 memset(&ihave, 0, sizeof(ihave));
726 memcpy(ihave.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
727 ihave.client_id = client->id;
729 if (gotd_imsg_compose_event(&client->repo_read->iev, GOTD_IMSG_HAVE,
730 PROC_GOTD, -1, &ihave, sizeof(ihave)) == -1)
731 return got_error_from_errno("imsg compose HAVE");
733 return NULL;
736 static int
737 client_has_capability(struct gotd_client *client, const char *capastr)
739 struct gotd_client_capability *capa;
740 size_t i;
742 if (client->ncapabilities == 0)
743 return 0;
745 for (i = 0; i < client->ncapabilities; i++) {
746 capa = &client->capabilities[i];
747 if (strcmp(capa->key, capastr) == 0)
748 return 1;
751 return 0;
754 static const struct got_error *
755 recv_capabilities(struct gotd_client *client, struct imsg *imsg)
757 struct gotd_imsg_capabilities icapas;
758 size_t datalen;
760 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
761 if (datalen != sizeof(icapas))
762 return got_error(GOT_ERR_PRIVSEP_LEN);
763 memcpy(&icapas, imsg->data, sizeof(icapas));
765 client->ncapa_alloc = icapas.ncapabilities;
766 client->capabilities = calloc(client->ncapa_alloc,
767 sizeof(*client->capabilities));
768 if (client->capabilities == NULL) {
769 client->ncapa_alloc = 0;
770 return got_error_from_errno("calloc");
773 log_debug("expecting %zu capabilities from uid %d",
774 client->ncapa_alloc, client->euid);
775 return NULL;
778 static const struct got_error *
779 recv_capability(struct gotd_client *client, struct imsg *imsg)
781 struct gotd_imsg_capability icapa;
782 struct gotd_client_capability *capa;
783 size_t datalen;
784 char *key, *value = NULL;
786 if (client->capabilities == NULL ||
787 client->ncapabilities >= client->ncapa_alloc) {
788 return got_error_msg(GOT_ERR_BAD_REQUEST,
789 "unexpected capability received");
792 memset(&icapa, 0, sizeof(icapa));
794 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
795 if (datalen < sizeof(icapa))
796 return got_error(GOT_ERR_PRIVSEP_LEN);
797 memcpy(&icapa, imsg->data, sizeof(icapa));
799 if (datalen != sizeof(icapa) + icapa.key_len + icapa.value_len)
800 return got_error(GOT_ERR_PRIVSEP_LEN);
802 key = malloc(icapa.key_len + 1);
803 if (key == NULL)
804 return got_error_from_errno("malloc");
805 if (icapa.value_len > 0) {
806 value = malloc(icapa.value_len + 1);
807 if (value == NULL) {
808 free(key);
809 return got_error_from_errno("malloc");
813 memcpy(key, imsg->data + sizeof(icapa), icapa.key_len);
814 key[icapa.key_len] = '\0';
815 if (value) {
816 memcpy(value, imsg->data + sizeof(icapa) + icapa.key_len,
817 icapa.value_len);
818 value[icapa.value_len] = '\0';
821 capa = &client->capabilities[client->ncapabilities++];
822 capa->key = key;
823 capa->value = value;
825 if (value)
826 log_debug("uid %d: capability %s=%s", client->euid, key, value);
827 else
828 log_debug("uid %d: capability %s", client->euid, key);
830 return NULL;
833 static const struct got_error *
834 send_packfile(struct gotd_client *client)
836 const struct got_error *err = NULL;
837 struct gotd_imsg_send_packfile ipack;
838 struct gotd_imsg_packfile_pipe ipipe;
839 int pipe[2];
841 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
842 return got_error_from_errno("socketpair");
844 memset(&ipack, 0, sizeof(ipack));
845 memset(&ipipe, 0, sizeof(ipipe));
847 ipack.client_id = client->id;
848 if (client_has_capability(client, GOT_CAPA_SIDE_BAND_64K))
849 ipack.report_progress = 1;
851 client->delta_cache_fd = got_opentempfd();
852 if (client->delta_cache_fd == -1)
853 return got_error_from_errno("got_opentempfd");
855 if (gotd_imsg_compose_event(&client->repo_read->iev,
856 GOTD_IMSG_SEND_PACKFILE, PROC_GOTD, client->delta_cache_fd,
857 &ipack, sizeof(ipack)) == -1) {
858 err = got_error_from_errno("imsg compose SEND_PACKFILE");
859 close(pipe[0]);
860 close(pipe[1]);
861 return err;
864 ipipe.client_id = client->id;
866 /* Send pack pipe end 0 to repo_read. */
867 if (gotd_imsg_compose_event(&client->repo_read->iev,
868 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[0],
869 &ipipe, sizeof(ipipe)) == -1) {
870 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
871 close(pipe[1]);
872 return err;
875 /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
876 if (gotd_imsg_compose_event(&client->iev,
877 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1], NULL, 0) == -1)
878 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
880 return err;
883 static const struct got_error *
884 recv_packfile(struct gotd_client *client)
886 const struct got_error *err = NULL;
887 struct gotd_imsg_recv_packfile ipack;
888 struct gotd_imsg_packfile_pipe ipipe;
889 struct gotd_imsg_packidx_file ifile;
890 char *basepath = NULL, *pack_path = NULL, *idx_path = NULL;
891 int packfd = -1, idxfd = -1;
892 int pipe[2] = { -1, -1 };
894 if (client->packfile_path) {
895 return got_error_fmt(GOT_ERR_PRIVSEP_MSG,
896 "uid %d already has a pack file", client->euid);
899 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
900 return got_error_from_errno("socketpair");
902 memset(&ipipe, 0, sizeof(ipipe));
903 ipipe.client_id = client->id;
905 if (gotd_imsg_compose_event(&client->repo_write->iev,
906 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[0],
907 &ipipe, sizeof(ipipe)) == -1) {
908 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
909 pipe[0] = -1;
910 goto done;
912 pipe[0] = -1;
914 if (gotd_imsg_compose_event(&client->repo_write->iev,
915 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1],
916 &ipipe, sizeof(ipipe)) == -1)
917 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
918 pipe[1] = -1;
920 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.pack",
921 client->repo_write->repo_path, GOT_OBJECTS_PACK_DIR,
922 client->euid) == -1) {
923 err = got_error_from_errno("asprintf");
924 goto done;
927 err = got_opentemp_named_fd(&pack_path, &packfd, basepath, "");
928 if (err)
929 goto done;
931 free(basepath);
932 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.idx",
933 client->repo_write->repo_path, GOT_OBJECTS_PACK_DIR,
934 client->euid) == -1) {
935 err = got_error_from_errno("asprintf");
936 basepath = NULL;
937 goto done;
939 err = got_opentemp_named_fd(&idx_path, &idxfd, basepath, "");
940 if (err)
941 goto done;
943 memset(&ifile, 0, sizeof(ifile));
944 ifile.client_id = client->id;
945 if (gotd_imsg_compose_event(&client->repo_write->iev,
946 GOTD_IMSG_PACKIDX_FILE, PROC_GOTD, idxfd,
947 &ifile, sizeof(ifile)) == -1) {
948 err = got_error_from_errno("imsg compose PACKIDX_FILE");
949 idxfd = -1;
950 goto done;
952 idxfd = -1;
954 memset(&ipack, 0, sizeof(ipack));
955 ipack.client_id = client->id;
956 if (client_has_capability(client, GOT_CAPA_REPORT_STATUS))
957 ipack.report_status = 1;
959 if (gotd_imsg_compose_event(&client->repo_write->iev,
960 GOTD_IMSG_RECV_PACKFILE, PROC_GOTD, packfd,
961 &ipack, sizeof(ipack)) == -1) {
962 err = got_error_from_errno("imsg compose RECV_PACKFILE");
963 packfd = -1;
964 goto done;
966 packfd = -1;
968 done:
969 free(basepath);
970 if (pipe[0] != -1 && close(pipe[0]) == -1 && err == NULL)
971 err = got_error_from_errno("close");
972 if (pipe[1] != -1 && close(pipe[1]) == -1 && err == NULL)
973 err = got_error_from_errno("close");
974 if (packfd != -1 && close(packfd) == -1 && err == NULL)
975 err = got_error_from_errno("close");
976 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
977 err = got_error_from_errno("close");
978 if (err) {
979 free(pack_path);
980 free(idx_path);
981 } else {
982 client->packfile_path = pack_path;
983 client->packidx_path = idx_path;
985 return err;
988 static void
989 gotd_request(int fd, short events, void *arg)
991 struct gotd_imsgev *iev = arg;
992 struct imsgbuf *ibuf = &iev->ibuf;
993 struct gotd_client *client = iev->handler_arg;
994 const struct got_error *err = NULL;
995 struct imsg imsg;
996 ssize_t n;
998 if (events & EV_WRITE) {
999 while (ibuf->w.queued) {
1000 n = msgbuf_write(&ibuf->w);
1001 if (n == -1 && errno == EPIPE) {
1003 * The client has closed its socket.
1004 * This can happen when Git clients are
1005 * done sending pack file data.
1007 msgbuf_clear(&ibuf->w);
1008 continue;
1009 } else if (n == -1 && errno != EAGAIN) {
1010 err = got_error_from_errno("imsg_flush");
1011 disconnect_on_error(client, err);
1012 return;
1014 if (n == 0) {
1015 /* Connection closed. */
1016 err = got_error(GOT_ERR_EOF);
1017 disconnect_on_error(client, err);
1018 return;
1022 /* Disconnect gotctl(8) now that messages have been sent. */
1023 if (!client_is_reading(client) && !client_is_writing(client)) {
1024 disconnect(client);
1025 return;
1029 if ((events & EV_READ) == 0)
1030 return;
1032 memset(&imsg, 0, sizeof(imsg));
1034 while (err == NULL) {
1035 err = gotd_imsg_recv(&imsg, ibuf, 0);
1036 if (err) {
1037 if (err->code == GOT_ERR_PRIVSEP_READ)
1038 err = NULL;
1039 break;
1042 evtimer_del(&client->tmo);
1044 switch (imsg.hdr.type) {
1045 case GOTD_IMSG_INFO:
1046 err = send_info(client);
1047 break;
1048 case GOTD_IMSG_STOP:
1049 err = stop_gotd(client);
1050 break;
1051 case GOTD_IMSG_LIST_REFS:
1052 if (client->state != GOTD_STATE_EXPECT_LIST_REFS) {
1053 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1054 "unexpected list-refs request received");
1055 break;
1057 err = start_client_session(client, &imsg);
1058 if (err)
1059 break;
1060 break;
1061 case GOTD_IMSG_CAPABILITIES:
1062 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1063 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1064 "unexpected capabilities received");
1065 break;
1067 log_debug("receiving capabilities from uid %d",
1068 client->euid);
1069 err = recv_capabilities(client, &imsg);
1070 break;
1071 case GOTD_IMSG_CAPABILITY:
1072 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1073 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1074 "unexpected capability received");
1075 break;
1077 err = recv_capability(client, &imsg);
1078 if (err || client->ncapabilities < client->ncapa_alloc)
1079 break;
1080 if (client_is_reading(client)) {
1081 client->state = GOTD_STATE_EXPECT_WANT;
1082 log_debug("uid %d: expecting want-lines",
1083 client->euid);
1084 } else if (client_is_writing(client)) {
1085 client->state = GOTD_STATE_EXPECT_REF_UPDATE;
1086 log_debug("uid %d: expecting ref-update-lines",
1087 client->euid);
1088 } else
1089 fatalx("client %d is both reading and writing",
1090 client->euid);
1091 break;
1092 case GOTD_IMSG_WANT:
1093 if (client->state != GOTD_STATE_EXPECT_WANT) {
1094 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1095 "unexpected want-line received");
1096 break;
1098 log_debug("received want-line from uid %d",
1099 client->euid);
1100 err = ensure_client_is_reading(client);
1101 if (err)
1102 break;
1103 err = forward_want(client, &imsg);
1104 break;
1105 case GOTD_IMSG_REF_UPDATE:
1106 if (client->state != GOTD_STATE_EXPECT_REF_UPDATE) {
1107 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1108 "unexpected ref-update-line received");
1109 break;
1111 log_debug("received ref-update-line from uid %d",
1112 client->euid);
1113 err = ensure_client_is_writing(client);
1114 if (err)
1115 break;
1116 err = forward_ref_update(client, &imsg);
1117 if (err)
1118 break;
1119 client->state = GOTD_STATE_EXPECT_MORE_REF_UPDATES;
1120 break;
1121 case GOTD_IMSG_HAVE:
1122 if (client->state != GOTD_STATE_EXPECT_HAVE) {
1123 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1124 "unexpected have-line received");
1125 break;
1127 log_debug("received have-line from uid %d",
1128 client->euid);
1129 err = ensure_client_is_reading(client);
1130 if (err)
1131 break;
1132 err = forward_have(client, &imsg);
1133 if (err)
1134 break;
1135 break;
1136 case GOTD_IMSG_FLUSH:
1137 if (client->state == GOTD_STATE_EXPECT_WANT ||
1138 client->state == GOTD_STATE_EXPECT_HAVE) {
1139 err = ensure_client_is_reading(client);
1140 if (err)
1141 break;
1142 } else if (client->state ==
1143 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1144 err = ensure_client_is_writing(client);
1145 if (err)
1146 break;
1147 } else {
1148 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1149 "unexpected flush-pkt received");
1150 break;
1152 log_debug("received flush-pkt from uid %d",
1153 client->euid);
1154 if (client->state == GOTD_STATE_EXPECT_WANT) {
1155 client->state = GOTD_STATE_EXPECT_HAVE;
1156 log_debug("uid %d: expecting have-lines",
1157 client->euid);
1158 } else if (client->state == GOTD_STATE_EXPECT_HAVE) {
1159 client->state = GOTD_STATE_EXPECT_DONE;
1160 log_debug("uid %d: expecting 'done'",
1161 client->euid);
1162 } else if (client->state ==
1163 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1164 client->state = GOTD_STATE_EXPECT_PACKFILE;
1165 log_debug("uid %d: expecting packfile",
1166 client->euid);
1167 err = recv_packfile(client);
1168 } else {
1169 /* should not happen, see above */
1170 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1171 "unexpected client state");
1172 break;
1174 break;
1175 case GOTD_IMSG_DONE:
1176 if (client->state != GOTD_STATE_EXPECT_HAVE &&
1177 client->state != GOTD_STATE_EXPECT_DONE) {
1178 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1179 "unexpected flush-pkt received");
1180 break;
1182 log_debug("received 'done' from uid %d", client->euid);
1183 err = ensure_client_is_reading(client);
1184 if (err)
1185 break;
1186 client->state = GOTD_STATE_DONE;
1187 err = send_packfile(client);
1188 break;
1189 default:
1190 err = got_error(GOT_ERR_PRIVSEP_MSG);
1191 break;
1194 imsg_free(&imsg);
1197 if (err) {
1198 if (err->code != GOT_ERR_EOF ||
1199 client->state != GOTD_STATE_EXPECT_PACKFILE)
1200 disconnect_on_error(client, err);
1201 } else {
1202 gotd_imsg_event_add(&client->iev);
1203 if (client->state == GOTD_STATE_EXPECT_LIST_REFS)
1204 evtimer_add(&client->tmo, &auth_timeout);
1205 else
1206 evtimer_add(&client->tmo, &timeout);
1210 static void
1211 gotd_request_timeout(int fd, short events, void *arg)
1213 struct gotd_client *client = arg;
1215 log_debug("disconnecting uid %d due to timeout", client->euid);
1216 disconnect(client);
1219 static const struct got_error *
1220 recv_connect(uint32_t *client_id, struct imsg *imsg)
1222 const struct got_error *err = NULL;
1223 struct gotd_imsg_connect iconnect;
1224 size_t datalen;
1225 int s = -1;
1226 struct gotd_client *client = NULL;
1228 *client_id = 0;
1230 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1231 if (datalen != sizeof(iconnect))
1232 return got_error(GOT_ERR_PRIVSEP_LEN);
1233 memcpy(&iconnect, imsg->data, sizeof(iconnect));
1235 s = imsg->fd;
1236 if (s == -1) {
1237 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1238 goto done;
1241 if (find_client(iconnect.client_id)) {
1242 err = got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
1243 goto done;
1246 client = calloc(1, sizeof(*client));
1247 if (client == NULL) {
1248 err = got_error_from_errno("calloc");
1249 goto done;
1252 *client_id = iconnect.client_id;
1254 client->state = GOTD_STATE_EXPECT_LIST_REFS;
1255 client->id = iconnect.client_id;
1256 client->fd = s;
1257 s = -1;
1258 client->delta_cache_fd = -1;
1259 /* The auth process will verify UID/GID for us. */
1260 client->euid = iconnect.euid;
1261 client->egid = iconnect.egid;
1262 client->nref_updates = -1;
1264 imsg_init(&client->iev.ibuf, client->fd);
1265 client->iev.handler = gotd_request;
1266 client->iev.events = EV_READ;
1267 client->iev.handler_arg = client;
1269 event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
1270 &client->iev);
1271 gotd_imsg_event_add(&client->iev);
1273 evtimer_set(&client->tmo, gotd_request_timeout, client);
1275 add_client(client);
1276 log_debug("%s: new client uid %d connected on fd %d", __func__,
1277 client->euid, client->fd);
1278 done:
1279 if (err) {
1280 struct gotd_child_proc *listen_proc = &gotd.listen_proc;
1281 struct gotd_imsg_disconnect idisconnect;
1283 idisconnect.client_id = client->id;
1284 if (gotd_imsg_compose_event(&listen_proc->iev,
1285 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
1286 &idisconnect, sizeof(idisconnect)) == -1)
1287 log_warn("imsg compose DISCONNECT");
1289 if (s != -1)
1290 close(s);
1293 return err;
1296 static const char *gotd_proc_names[PROC_MAX] = {
1297 "parent",
1298 "listen",
1299 "auth",
1300 "repo_read",
1301 "repo_write"
1304 static void
1305 kill_proc(struct gotd_child_proc *proc, int fatal)
1307 if (fatal) {
1308 log_warnx("sending SIGKILL to PID %d", proc->pid);
1309 kill(proc->pid, SIGKILL);
1310 } else
1311 kill(proc->pid, SIGTERM);
1314 static void
1315 gotd_shutdown(void)
1317 struct gotd_child_proc *proc;
1318 uint64_t slot;
1320 for (slot = 0; slot < nitems(gotd_clients); slot++) {
1321 struct gotd_client *c, *tmp;
1323 STAILQ_FOREACH_SAFE(c, &gotd_clients[slot], entry, tmp)
1324 disconnect(c);
1327 proc = &gotd.listen_proc;
1328 msgbuf_clear(&proc->iev.ibuf.w);
1329 close(proc->iev.ibuf.fd);
1330 kill_proc(proc, 0);
1331 wait_for_child(proc->pid);
1333 log_info("terminating");
1334 exit(0);
1337 void
1338 gotd_sighdlr(int sig, short event, void *arg)
1341 * Normal signal handler rules don't apply because libevent
1342 * decouples for us.
1345 switch (sig) {
1346 case SIGHUP:
1347 log_info("%s: ignoring SIGHUP", __func__);
1348 break;
1349 case SIGUSR1:
1350 log_info("%s: ignoring SIGUSR1", __func__);
1351 break;
1352 case SIGTERM:
1353 case SIGINT:
1354 gotd_shutdown();
1355 log_warnx("gotd terminating");
1356 exit(0);
1357 break;
1358 default:
1359 fatalx("unexpected signal");
1363 static const struct got_error *
1364 ensure_proc_is_reading(struct gotd_client *client,
1365 struct gotd_child_proc *proc)
1367 if (!client_is_reading(client)) {
1368 kill_proc(proc, 1);
1369 return got_error_fmt(GOT_ERR_BAD_PACKET,
1370 "PID %d handled a read-request for uid %d but this "
1371 "user is not reading from a repository", proc->pid,
1372 client->euid);
1375 return NULL;
1378 static const struct got_error *
1379 ensure_proc_is_writing(struct gotd_client *client,
1380 struct gotd_child_proc *proc)
1382 if (!client_is_writing(client)) {
1383 kill_proc(proc, 1);
1384 return got_error_fmt(GOT_ERR_BAD_PACKET,
1385 "PID %d handled a write-request for uid %d but this "
1386 "user is not writing to a repository", proc->pid,
1387 client->euid);
1390 return NULL;
1393 static int
1394 verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
1395 struct imsg *imsg)
1397 const struct got_error *err;
1398 struct gotd_child_proc *client_proc;
1399 int ret = 0;
1401 if (proc->type == PROC_REPO_READ || proc->type == PROC_REPO_WRITE) {
1402 client_proc = get_client_proc(client);
1403 if (client_proc == NULL)
1404 fatalx("no process found for uid %d", client->euid);
1405 if (proc->pid != client_proc->pid) {
1406 kill_proc(proc, 1);
1407 log_warnx("received message from PID %d for uid %d, "
1408 "while PID %d is the process serving this user",
1409 proc->pid, client->euid, client_proc->pid);
1410 return 0;
1414 switch (imsg->hdr.type) {
1415 case GOTD_IMSG_ERROR:
1416 ret = 1;
1417 break;
1418 case GOTD_IMSG_CONNECT:
1419 if (proc->type != PROC_LISTEN) {
1420 err = got_error_fmt(GOT_ERR_BAD_PACKET,
1421 "new connection for uid %d from PID %d "
1422 "which is not the listen process",
1423 proc->pid, client->euid);
1424 } else
1425 ret = 1;
1426 break;
1427 case GOTD_IMSG_ACCESS_GRANTED:
1428 if (proc->type != PROC_AUTH) {
1429 err = got_error_fmt(GOT_ERR_BAD_PACKET,
1430 "authentication of uid %d from PID %d "
1431 "which is not the auth process",
1432 proc->pid, client->euid);
1433 } else
1434 ret = 1;
1435 break;
1436 case GOTD_IMSG_REPO_CHILD_READY:
1437 if (proc->type != PROC_REPO_READ &&
1438 proc->type != PROC_REPO_WRITE) {
1439 err = got_error_fmt(GOT_ERR_BAD_PACKET,
1440 "unexpected \"ready\" signal from PID %d",
1441 proc->pid);
1442 } else
1443 ret = 1;
1444 break;
1445 case GOTD_IMSG_PACKFILE_DONE:
1446 err = ensure_proc_is_reading(client, proc);
1447 if (err)
1448 log_warnx("uid %d: %s", client->euid, err->msg);
1449 else
1450 ret = 1;
1451 break;
1452 case GOTD_IMSG_PACKFILE_INSTALL:
1453 case GOTD_IMSG_REF_UPDATES_START:
1454 case GOTD_IMSG_REF_UPDATE:
1455 err = ensure_proc_is_writing(client, proc);
1456 if (err)
1457 log_warnx("uid %d: %s", client->euid, err->msg);
1458 else
1459 ret = 1;
1460 break;
1461 default:
1462 log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
1463 break;
1466 return ret;
1469 static const struct got_error *
1470 list_refs_request(struct gotd_client *client, struct gotd_imsgev *iev)
1472 static const struct got_error *err;
1473 struct gotd_imsg_list_refs_internal ilref;
1474 int fd;
1476 memset(&ilref, 0, sizeof(ilref));
1477 ilref.client_id = client->id;
1479 fd = dup(client->fd);
1480 if (fd == -1)
1481 return got_error_from_errno("dup");
1483 if (gotd_imsg_compose_event(iev, GOTD_IMSG_LIST_REFS_INTERNAL,
1484 PROC_GOTD, fd, &ilref, sizeof(ilref)) == -1) {
1485 err = got_error_from_errno("imsg compose WANT");
1486 close(fd);
1487 return err;
1490 client->state = GOTD_STATE_EXPECT_CAPABILITIES;
1491 log_debug("uid %d: expecting capabilities", client->euid);
1492 return NULL;
1495 static const struct got_error *
1496 recv_packfile_done(uint32_t *client_id, struct imsg *imsg)
1498 struct gotd_imsg_packfile_done idone;
1499 size_t datalen;
1501 log_debug("packfile-done received");
1503 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1504 if (datalen != sizeof(idone))
1505 return got_error(GOT_ERR_PRIVSEP_LEN);
1506 memcpy(&idone, imsg->data, sizeof(idone));
1508 *client_id = idone.client_id;
1509 return NULL;
1512 static const struct got_error *
1513 recv_packfile_install(uint32_t *client_id, struct imsg *imsg)
1515 struct gotd_imsg_packfile_install inst;
1516 size_t datalen;
1518 log_debug("packfile-install received");
1520 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1521 if (datalen != sizeof(inst))
1522 return got_error(GOT_ERR_PRIVSEP_LEN);
1523 memcpy(&inst, imsg->data, sizeof(inst));
1525 *client_id = inst.client_id;
1526 return NULL;
1529 static const struct got_error *
1530 recv_ref_updates_start(uint32_t *client_id, struct imsg *imsg)
1532 struct gotd_imsg_ref_updates_start istart;
1533 size_t datalen;
1535 log_debug("ref-updates-start received");
1537 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1538 if (datalen != sizeof(istart))
1539 return got_error(GOT_ERR_PRIVSEP_LEN);
1540 memcpy(&istart, imsg->data, sizeof(istart));
1542 *client_id = istart.client_id;
1543 return NULL;
1546 static const struct got_error *
1547 recv_ref_update(uint32_t *client_id, struct imsg *imsg)
1549 struct gotd_imsg_ref_update iref;
1550 size_t datalen;
1552 log_debug("ref-update received");
1554 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1555 if (datalen < sizeof(iref))
1556 return got_error(GOT_ERR_PRIVSEP_LEN);
1557 memcpy(&iref, imsg->data, sizeof(iref));
1559 *client_id = iref.client_id;
1560 return NULL;
1563 static const struct got_error *
1564 send_ref_update_ok(struct gotd_client *client,
1565 struct gotd_imsg_ref_update *iref, const char *refname)
1567 struct gotd_imsg_ref_update_ok iok;
1568 struct ibuf *wbuf;
1569 size_t len;
1571 memset(&iok, 0, sizeof(iok));
1572 iok.client_id = client->id;
1573 memcpy(iok.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1574 memcpy(iok.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1575 iok.name_len = strlen(refname);
1577 len = sizeof(iok) + iok.name_len;
1578 wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_OK,
1579 PROC_GOTD, gotd.pid, len);
1580 if (wbuf == NULL)
1581 return got_error_from_errno("imsg_create REF_UPDATE_OK");
1583 if (imsg_add(wbuf, &iok, sizeof(iok)) == -1)
1584 return got_error_from_errno("imsg_add REF_UPDATE_OK");
1585 if (imsg_add(wbuf, refname, iok.name_len) == -1)
1586 return got_error_from_errno("imsg_add REF_UPDATE_OK");
1588 wbuf->fd = -1;
1589 imsg_close(&client->iev.ibuf, wbuf);
1590 gotd_imsg_event_add(&client->iev);
1591 return NULL;
1594 static void
1595 send_refs_updated(struct gotd_client *client)
1597 if (gotd_imsg_compose_event(&client->iev,
1598 GOTD_IMSG_REFS_UPDATED, PROC_GOTD, -1, NULL, 0) == -1)
1599 log_warn("imsg compose REFS_UPDATED");
1602 static const struct got_error *
1603 send_ref_update_ng(struct gotd_client *client,
1604 struct gotd_imsg_ref_update *iref, const char *refname,
1605 const char *reason)
1607 const struct got_error *ng_err;
1608 struct gotd_imsg_ref_update_ng ing;
1609 struct ibuf *wbuf;
1610 size_t len;
1612 memset(&ing, 0, sizeof(ing));
1613 ing.client_id = client->id;
1614 memcpy(ing.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1615 memcpy(ing.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1616 ing.name_len = strlen(refname);
1618 ng_err = got_error_fmt(GOT_ERR_REF_BUSY, "%s", reason);
1619 ing.reason_len = strlen(ng_err->msg);
1621 len = sizeof(ing) + ing.name_len + ing.reason_len;
1622 wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_NG,
1623 PROC_GOTD, gotd.pid, len);
1624 if (wbuf == NULL)
1625 return got_error_from_errno("imsg_create REF_UPDATE_NG");
1627 if (imsg_add(wbuf, &ing, sizeof(ing)) == -1)
1628 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1629 if (imsg_add(wbuf, refname, ing.name_len) == -1)
1630 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1631 if (imsg_add(wbuf, ng_err->msg, ing.reason_len) == -1)
1632 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1634 wbuf->fd = -1;
1635 imsg_close(&client->iev.ibuf, wbuf);
1636 gotd_imsg_event_add(&client->iev);
1637 return NULL;
1640 static const struct got_error *
1641 install_pack(struct gotd_client *client, const char *repo_path,
1642 struct imsg *imsg)
1644 const struct got_error *err = NULL;
1645 struct gotd_imsg_packfile_install inst;
1646 char hex[SHA1_DIGEST_STRING_LENGTH];
1647 size_t datalen;
1648 char *packfile_path = NULL, *packidx_path = NULL;
1650 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1651 if (datalen != sizeof(inst))
1652 return got_error(GOT_ERR_PRIVSEP_LEN);
1653 memcpy(&inst, imsg->data, sizeof(inst));
1655 if (client->packfile_path == NULL)
1656 return got_error_msg(GOT_ERR_BAD_REQUEST,
1657 "client has no pack file");
1658 if (client->packidx_path == NULL)
1659 return got_error_msg(GOT_ERR_BAD_REQUEST,
1660 "client has no pack file index");
1662 if (got_sha1_digest_to_str(inst.pack_sha1, hex, sizeof(hex)) == NULL)
1663 return got_error_msg(GOT_ERR_NO_SPACE,
1664 "could not convert pack file SHA1 to hex");
1666 if (asprintf(&packfile_path, "/%s/%s/pack-%s.pack",
1667 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1668 err = got_error_from_errno("asprintf");
1669 goto done;
1672 if (asprintf(&packidx_path, "/%s/%s/pack-%s.idx",
1673 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1674 err = got_error_from_errno("asprintf");
1675 goto done;
1678 if (rename(client->packfile_path, packfile_path) == -1) {
1679 err = got_error_from_errno3("rename", client->packfile_path,
1680 packfile_path);
1681 goto done;
1684 free(client->packfile_path);
1685 client->packfile_path = NULL;
1687 if (rename(client->packidx_path, packidx_path) == -1) {
1688 err = got_error_from_errno3("rename", client->packidx_path,
1689 packidx_path);
1690 goto done;
1693 free(client->packidx_path);
1694 client->packidx_path = NULL;
1695 done:
1696 free(packfile_path);
1697 free(packidx_path);
1698 return err;
1701 static const struct got_error *
1702 begin_ref_updates(struct gotd_client *client, struct imsg *imsg)
1704 struct gotd_imsg_ref_updates_start istart;
1705 size_t datalen;
1707 if (client->nref_updates != -1)
1708 return got_error(GOT_ERR_PRIVSEP_MSG);
1710 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1711 if (datalen != sizeof(istart))
1712 return got_error(GOT_ERR_PRIVSEP_LEN);
1713 memcpy(&istart, imsg->data, sizeof(istart));
1715 if (istart.nref_updates <= 0)
1716 return got_error(GOT_ERR_PRIVSEP_MSG);
1718 client->nref_updates = istart.nref_updates;
1719 return NULL;
1722 static const struct got_error *
1723 update_ref(struct gotd_client *client, const char *repo_path,
1724 struct imsg *imsg)
1726 const struct got_error *err = NULL;
1727 struct got_repository *repo = NULL;
1728 struct got_reference *ref = NULL;
1729 struct gotd_imsg_ref_update iref;
1730 struct got_object_id old_id, new_id;
1731 struct got_object_id *id = NULL;
1732 struct got_object *obj = NULL;
1733 char *refname = NULL;
1734 size_t datalen;
1735 int locked = 0;
1737 log_debug("update-ref from uid %d", client->euid);
1739 if (client->nref_updates <= 0)
1740 return got_error(GOT_ERR_PRIVSEP_MSG);
1742 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1743 if (datalen < sizeof(iref))
1744 return got_error(GOT_ERR_PRIVSEP_LEN);
1745 memcpy(&iref, imsg->data, sizeof(iref));
1746 if (datalen != sizeof(iref) + iref.name_len)
1747 return got_error(GOT_ERR_PRIVSEP_LEN);
1748 refname = malloc(iref.name_len + 1);
1749 if (refname == NULL)
1750 return got_error_from_errno("malloc");
1751 memcpy(refname, imsg->data + sizeof(iref), iref.name_len);
1752 refname[iref.name_len] = '\0';
1754 log_debug("updating ref %s for uid %d", refname, client->euid);
1756 err = got_repo_open(&repo, repo_path, NULL, NULL);
1757 if (err)
1758 goto done;
1760 memcpy(old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
1761 memcpy(new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
1762 err = got_object_open(&obj, repo, &new_id);
1763 if (err)
1764 goto done;
1766 if (iref.ref_is_new) {
1767 err = got_ref_open(&ref, repo, refname, 0);
1768 if (err) {
1769 if (err->code != GOT_ERR_NOT_REF)
1770 goto done;
1771 err = got_ref_alloc(&ref, refname, &new_id);
1772 if (err)
1773 goto done;
1774 err = got_ref_write(ref, repo); /* will lock/unlock */
1775 if (err)
1776 goto done;
1777 } else {
1778 err = got_error_fmt(GOT_ERR_REF_BUSY,
1779 "%s has been created by someone else "
1780 "while transaction was in progress",
1781 got_ref_get_name(ref));
1782 goto done;
1784 } else {
1785 err = got_ref_open(&ref, repo, refname, 1 /* lock */);
1786 if (err)
1787 goto done;
1788 locked = 1;
1790 err = got_ref_resolve(&id, repo, ref);
1791 if (err)
1792 goto done;
1794 if (got_object_id_cmp(id, &old_id) != 0) {
1795 err = got_error_fmt(GOT_ERR_REF_BUSY,
1796 "%s has been modified by someone else "
1797 "while transaction was in progress",
1798 got_ref_get_name(ref));
1799 goto done;
1802 err = got_ref_change_ref(ref, &new_id);
1803 if (err)
1804 goto done;
1806 err = got_ref_write(ref, repo);
1807 if (err)
1808 goto done;
1810 free(id);
1811 id = NULL;
1813 done:
1814 if (err) {
1815 if (err->code == GOT_ERR_LOCKFILE_TIMEOUT) {
1816 err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
1817 "could not acquire exclusive file lock for %s",
1818 refname);
1820 send_ref_update_ng(client, &iref, refname, err->msg);
1821 } else
1822 send_ref_update_ok(client, &iref, refname);
1824 if (client->nref_updates > 0) {
1825 client->nref_updates--;
1826 if (client->nref_updates == 0)
1827 send_refs_updated(client);
1830 if (locked) {
1831 const struct got_error *unlock_err;
1832 unlock_err = got_ref_unlock(ref);
1833 if (unlock_err && err == NULL)
1834 err = unlock_err;
1836 if (ref)
1837 got_ref_close(ref);
1838 if (obj)
1839 got_object_close(obj);
1840 if (repo)
1841 got_repo_close(repo);
1842 free(refname);
1843 free(id);
1844 return err;
1847 static void
1848 gotd_dispatch_listener(int fd, short event, void *arg)
1850 struct gotd_imsgev *iev = arg;
1851 struct imsgbuf *ibuf = &iev->ibuf;
1852 struct gotd_child_proc *proc = &gotd.listen_proc;
1853 ssize_t n;
1854 int shut = 0;
1855 struct imsg imsg;
1857 if (proc->iev.ibuf.fd != fd)
1858 fatalx("%s: unexpected fd %d", __func__, fd);
1860 if (event & EV_READ) {
1861 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1862 fatal("imsg_read error");
1863 if (n == 0) {
1864 /* Connection closed. */
1865 shut = 1;
1866 goto done;
1870 if (event & EV_WRITE) {
1871 n = msgbuf_write(&ibuf->w);
1872 if (n == -1 && errno != EAGAIN)
1873 fatal("msgbuf_write");
1874 if (n == 0) {
1875 /* Connection closed. */
1876 shut = 1;
1877 goto done;
1881 for (;;) {
1882 const struct got_error *err = NULL;
1883 struct gotd_client *client = NULL;
1884 uint32_t client_id = 0;
1885 int do_disconnect = 0;
1887 if ((n = imsg_get(ibuf, &imsg)) == -1)
1888 fatal("%s: imsg_get error", __func__);
1889 if (n == 0) /* No more messages. */
1890 break;
1892 switch (imsg.hdr.type) {
1893 case GOTD_IMSG_ERROR:
1894 do_disconnect = 1;
1895 err = gotd_imsg_recv_error(&client_id, &imsg);
1896 break;
1897 case GOTD_IMSG_CONNECT:
1898 err = recv_connect(&client_id, &imsg);
1899 break;
1900 default:
1901 log_debug("unexpected imsg %d", imsg.hdr.type);
1902 break;
1905 client = find_client(client_id);
1906 if (client == NULL) {
1907 log_warnx("%s: client not found", __func__);
1908 imsg_free(&imsg);
1909 continue;
1912 if (err)
1913 log_warnx("uid %d: %s", client->euid, err->msg);
1915 if (do_disconnect) {
1916 if (err)
1917 disconnect_on_error(client, err);
1918 else
1919 disconnect(client);
1922 imsg_free(&imsg);
1924 done:
1925 if (!shut) {
1926 gotd_imsg_event_add(iev);
1927 } else {
1928 /* This pipe is dead. Remove its event handler */
1929 event_del(&iev->ev);
1930 event_loopexit(NULL);
1934 static void
1935 gotd_dispatch_auth_child(int fd, short event, void *arg)
1937 const struct got_error *err = NULL;
1938 struct gotd_imsgev *iev = arg;
1939 struct imsgbuf *ibuf = &iev->ibuf;
1940 struct gotd_client *client;
1941 struct gotd_repo *repo = NULL;
1942 ssize_t n;
1943 int shut = 0;
1944 struct imsg imsg;
1945 uint32_t client_id = 0;
1946 int do_disconnect = 0;
1947 enum gotd_procid proc_type;
1949 client = find_client_by_proc_fd(fd);
1950 if (client == NULL)
1951 fatalx("cannot find client for fd %d", fd);
1953 if (client->auth == NULL)
1954 fatalx("cannot find auth child process for fd %d", fd);
1956 if (event & EV_READ) {
1957 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1958 fatal("imsg_read error");
1959 if (n == 0) {
1960 /* Connection closed. */
1961 shut = 1;
1962 goto done;
1966 if (event & EV_WRITE) {
1967 n = msgbuf_write(&ibuf->w);
1968 if (n == -1 && errno != EAGAIN)
1969 fatal("msgbuf_write");
1970 if (n == 0) {
1971 /* Connection closed. */
1972 shut = 1;
1974 goto done;
1977 if (client->auth->iev.ibuf.fd != fd)
1978 fatalx("%s: unexpected fd %d", __func__, fd);
1980 if ((n = imsg_get(ibuf, &imsg)) == -1)
1981 fatal("%s: imsg_get error", __func__);
1982 if (n == 0) /* No more messages. */
1983 return;
1985 evtimer_del(&client->tmo);
1987 switch (imsg.hdr.type) {
1988 case GOTD_IMSG_ERROR:
1989 do_disconnect = 1;
1990 err = gotd_imsg_recv_error(&client_id, &imsg);
1991 break;
1992 case GOTD_IMSG_ACCESS_GRANTED:
1993 break;
1994 default:
1995 do_disconnect = 1;
1996 log_debug("unexpected imsg %d", imsg.hdr.type);
1997 break;
2000 if (!verify_imsg_src(client, client->auth, &imsg)) {
2001 do_disconnect = 1;
2002 log_debug("dropping imsg type %d from PID %d",
2003 imsg.hdr.type, client->auth->pid);
2005 imsg_free(&imsg);
2007 if (do_disconnect) {
2008 if (err)
2009 disconnect_on_error(client, err);
2010 else
2011 disconnect(client);
2012 goto done;
2015 repo = find_repo_by_name(client->auth->repo_name);
2016 if (repo == NULL) {
2017 err = got_error(GOT_ERR_NOT_GIT_REPO);
2018 goto done;
2020 kill_auth_proc(client);
2022 log_info("authenticated uid %d for repository %s\n",
2023 client->euid, repo->name);
2025 if (client->required_auth & GOTD_AUTH_WRITE)
2026 proc_type = PROC_REPO_WRITE;
2027 else
2028 proc_type = PROC_REPO_READ;
2030 err = start_repo_child(client, proc_type, repo, gotd.argv0,
2031 gotd.confpath, gotd.daemonize, gotd.verbosity);
2032 done:
2033 if (err)
2034 log_warnx("uid %d: %s", client->euid, err->msg);
2036 /* We might have killed the auth process by now. */
2037 if (client->auth != NULL) {
2038 if (!shut) {
2039 gotd_imsg_event_add(iev);
2040 } else {
2041 /* This pipe is dead. Remove its event handler */
2042 event_del(&iev->ev);
2047 static void
2048 gotd_dispatch_repo_child(int fd, short event, void *arg)
2050 struct gotd_imsgev *iev = arg;
2051 struct imsgbuf *ibuf = &iev->ibuf;
2052 struct gotd_child_proc *proc = NULL;
2053 struct gotd_client *client = NULL;
2054 ssize_t n;
2055 int shut = 0;
2056 struct imsg imsg;
2058 if (event & EV_READ) {
2059 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
2060 fatal("imsg_read error");
2061 if (n == 0) {
2062 /* Connection closed. */
2063 shut = 1;
2064 goto done;
2068 if (event & EV_WRITE) {
2069 n = msgbuf_write(&ibuf->w);
2070 if (n == -1 && errno != EAGAIN)
2071 fatal("msgbuf_write");
2072 if (n == 0) {
2073 /* Connection closed. */
2074 shut = 1;
2075 goto done;
2079 client = find_client_by_proc_fd(fd);
2080 if (client == NULL)
2081 fatalx("cannot find client for fd %d", fd);
2083 proc = get_client_proc(client);
2084 if (proc == NULL)
2085 fatalx("cannot find child process for fd %d", fd);
2087 for (;;) {
2088 const struct got_error *err = NULL;
2089 uint32_t client_id = 0;
2090 int do_disconnect = 0;
2091 int do_list_refs = 0, do_ref_updates = 0, do_ref_update = 0;
2092 int do_packfile_install = 0;
2094 if ((n = imsg_get(ibuf, &imsg)) == -1)
2095 fatal("%s: imsg_get error", __func__);
2096 if (n == 0) /* No more messages. */
2097 break;
2099 switch (imsg.hdr.type) {
2100 case GOTD_IMSG_ERROR:
2101 do_disconnect = 1;
2102 err = gotd_imsg_recv_error(&client_id, &imsg);
2103 break;
2104 case GOTD_IMSG_REPO_CHILD_READY:
2105 do_list_refs = 1;
2106 break;
2107 case GOTD_IMSG_PACKFILE_DONE:
2108 do_disconnect = 1;
2109 err = recv_packfile_done(&client_id, &imsg);
2110 break;
2111 case GOTD_IMSG_PACKFILE_INSTALL:
2112 err = recv_packfile_install(&client_id, &imsg);
2113 if (err == NULL)
2114 do_packfile_install = 1;
2115 break;
2116 case GOTD_IMSG_REF_UPDATES_START:
2117 err = recv_ref_updates_start(&client_id, &imsg);
2118 if (err == NULL)
2119 do_ref_updates = 1;
2120 break;
2121 case GOTD_IMSG_REF_UPDATE:
2122 err = recv_ref_update(&client_id, &imsg);
2123 if (err == NULL)
2124 do_ref_update = 1;
2125 break;
2126 default:
2127 log_debug("unexpected imsg %d", imsg.hdr.type);
2128 break;
2131 if (!verify_imsg_src(client, proc, &imsg)) {
2132 log_debug("dropping imsg type %d from PID %d",
2133 imsg.hdr.type, proc->pid);
2134 imsg_free(&imsg);
2135 continue;
2137 if (err)
2138 log_warnx("uid %d: %s", client->euid, err->msg);
2140 if (do_disconnect) {
2141 if (err)
2142 disconnect_on_error(client, err);
2143 else
2144 disconnect(client);
2145 } else {
2146 if (do_list_refs)
2147 err = list_refs_request(client, iev);
2148 else if (do_packfile_install)
2149 err = install_pack(client, proc->repo_path,
2150 &imsg);
2151 else if (do_ref_updates)
2152 err = begin_ref_updates(client, &imsg);
2153 else if (do_ref_update)
2154 err = update_ref(client, proc->repo_path,
2155 &imsg);
2156 if (err)
2157 log_warnx("uid %d: %s", client->euid, err->msg);
2159 imsg_free(&imsg);
2161 done:
2162 if (!shut) {
2163 gotd_imsg_event_add(iev);
2164 } else {
2165 /* This pipe is dead. Remove its event handler */
2166 event_del(&iev->ev);
2167 event_loopexit(NULL);
2171 static pid_t
2172 start_child(enum gotd_procid proc_id, const char *repo_path,
2173 char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
2175 char *argv[11];
2176 int argc = 0;
2177 pid_t pid;
2179 switch (pid = fork()) {
2180 case -1:
2181 fatal("cannot fork");
2182 case 0:
2183 break;
2184 default:
2185 close(fd);
2186 return pid;
2189 if (fd != GOTD_FILENO_MSG_PIPE) {
2190 if (dup2(fd, GOTD_FILENO_MSG_PIPE) == -1)
2191 fatal("cannot setup imsg fd");
2192 } else if (fcntl(fd, F_SETFD, 0) == -1)
2193 fatal("cannot setup imsg fd");
2195 argv[argc++] = argv0;
2196 switch (proc_id) {
2197 case PROC_LISTEN:
2198 argv[argc++] = (char *)"-L";
2199 break;
2200 case PROC_AUTH:
2201 argv[argc++] = (char *)"-A";
2202 break;
2203 case PROC_REPO_READ:
2204 argv[argc++] = (char *)"-R";
2205 break;
2206 case PROC_REPO_WRITE:
2207 argv[argc++] = (char *)"-W";
2208 break;
2209 default:
2210 fatalx("invalid process id %d", proc_id);
2213 argv[argc++] = (char *)"-f";
2214 argv[argc++] = (char *)confpath;
2216 if (repo_path) {
2217 argv[argc++] = (char *)"-P";
2218 argv[argc++] = (char *)repo_path;
2221 if (!daemonize)
2222 argv[argc++] = (char *)"-d";
2223 if (verbosity > 0)
2224 argv[argc++] = (char *)"-v";
2225 if (verbosity > 1)
2226 argv[argc++] = (char *)"-v";
2227 argv[argc++] = NULL;
2229 execvp(argv0, argv);
2230 fatal("execvp");
2233 static void
2234 start_listener(char *argv0, const char *confpath, int daemonize, int verbosity)
2236 struct gotd_child_proc *proc = &gotd.listen_proc;
2238 proc->type = PROC_LISTEN;
2240 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
2241 PF_UNSPEC, proc->pipe) == -1)
2242 fatal("socketpair");
2244 proc->pid = start_child(proc->type, NULL, argv0, confpath,
2245 proc->pipe[1], daemonize, verbosity);
2246 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
2247 proc->iev.handler = gotd_dispatch_listener;
2248 proc->iev.events = EV_READ;
2249 proc->iev.handler_arg = NULL;
2252 static const struct got_error *
2253 start_repo_child(struct gotd_client *client, enum gotd_procid proc_type,
2254 struct gotd_repo *repo, char *argv0, const char *confpath,
2255 int daemonize, int verbosity)
2257 struct gotd_child_proc *proc;
2259 if (proc_type != PROC_REPO_READ && proc_type != PROC_REPO_WRITE)
2260 return got_error_msg(GOT_ERR_NOT_IMPL, "bad process type");
2262 proc = calloc(1, sizeof(*proc));
2263 if (proc == NULL)
2264 return got_error_from_errno("calloc");
2266 proc->type = proc_type;
2267 if (strlcpy(proc->repo_name, repo->name,
2268 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
2269 fatalx("repository name too long: %s", repo->name);
2270 log_debug("starting %s for repository %s",
2271 proc->type == PROC_REPO_READ ? "reader" : "writer", repo->name);
2272 if (realpath(repo->path, proc->repo_path) == NULL)
2273 fatal("%s", repo->path);
2274 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
2275 PF_UNSPEC, proc->pipe) == -1)
2276 fatal("socketpair");
2277 proc->pid = start_child(proc->type, proc->repo_path, argv0,
2278 confpath, proc->pipe[1], daemonize, verbosity);
2279 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
2280 log_debug("proc %s %s is on fd %d",
2281 gotd_proc_names[proc->type], proc->repo_path,
2282 proc->pipe[0]);
2283 proc->iev.handler = gotd_dispatch_repo_child;
2284 proc->iev.events = EV_READ;
2285 proc->iev.handler_arg = NULL;
2286 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
2287 gotd_dispatch_repo_child, &proc->iev);
2288 gotd_imsg_event_add(&proc->iev);
2290 if (proc->type == PROC_REPO_READ)
2291 client->repo_read = proc;
2292 else
2293 client->repo_write = proc;
2295 return NULL;
2298 static const struct got_error *
2299 start_auth_child(struct gotd_client *client, int required_auth,
2300 struct gotd_repo *repo, char *argv0, const char *confpath,
2301 int daemonize, int verbosity)
2303 const struct got_error *err = NULL;
2304 struct gotd_child_proc *proc;
2305 struct gotd_imsg_auth iauth;
2306 int fd;
2308 memset(&iauth, 0, sizeof(iauth));
2310 fd = dup(client->fd);
2311 if (fd == -1)
2312 return got_error_from_errno("dup");
2314 proc = calloc(1, sizeof(*proc));
2315 if (proc == NULL) {
2316 err = got_error_from_errno("calloc");
2317 close(fd);
2318 return err;
2321 proc->type = PROC_AUTH;
2322 if (strlcpy(proc->repo_name, repo->name,
2323 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
2324 fatalx("repository name too long: %s", repo->name);
2325 log_debug("starting auth for uid %d repository %s",
2326 client->euid, repo->name);
2327 if (realpath(repo->path, proc->repo_path) == NULL)
2328 fatal("%s", repo->path);
2329 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
2330 PF_UNSPEC, proc->pipe) == -1)
2331 fatal("socketpair");
2332 proc->pid = start_child(proc->type, proc->repo_path, argv0,
2333 confpath, proc->pipe[1], daemonize, verbosity);
2334 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
2335 log_debug("proc %s %s is on fd %d",
2336 gotd_proc_names[proc->type], proc->repo_path,
2337 proc->pipe[0]);
2338 proc->iev.handler = gotd_dispatch_auth_child;
2339 proc->iev.events = EV_READ;
2340 proc->iev.handler_arg = NULL;
2341 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
2342 gotd_dispatch_auth_child, &proc->iev);
2343 gotd_imsg_event_add(&proc->iev);
2345 iauth.euid = client->euid;
2346 iauth.egid = client->egid;
2347 iauth.required_auth = required_auth;
2348 iauth.client_id = client->id;
2349 if (gotd_imsg_compose_event(&proc->iev, GOTD_IMSG_AUTHENTICATE,
2350 PROC_GOTD, fd, &iauth, sizeof(iauth)) == -1) {
2351 log_warn("imsg compose AUTHENTICATE");
2352 close(fd);
2353 /* Let the auth_timeout handler tidy up. */
2356 client->auth = proc;
2357 client->required_auth = required_auth;
2358 return NULL;
2361 static void
2362 apply_unveil_repo_readonly(const char *repo_path)
2364 if (unveil(repo_path, "r") == -1)
2365 fatal("unveil %s", repo_path);
2367 if (unveil(NULL, NULL) == -1)
2368 fatal("unveil");
2371 static void
2372 apply_unveil_none(void)
2374 if (unveil("/", "") == -1)
2375 fatal("unveil");
2377 if (unveil(NULL, NULL) == -1)
2378 fatal("unveil");
2381 static void
2382 apply_unveil(void)
2384 struct gotd_repo *repo;
2386 if (unveil(gotd.argv0, "x") == -1)
2387 fatal("unveil %s", gotd.argv0);
2389 TAILQ_FOREACH(repo, &gotd.repos, entry) {
2390 if (unveil(repo->path, "rwc") == -1)
2391 fatal("unveil %s", repo->path);
2394 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
2395 fatal("unveil %s", GOT_TMPDIR_STR);
2397 if (unveil(NULL, NULL) == -1)
2398 fatal("unveil");
2401 int
2402 main(int argc, char **argv)
2404 const struct got_error *error = NULL;
2405 int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
2406 const char *confpath = GOTD_CONF_PATH;
2407 char *argv0 = argv[0];
2408 char title[2048];
2409 gid_t groups[NGROUPS_MAX];
2410 int ngroups = NGROUPS_MAX;
2411 struct passwd *pw = NULL;
2412 struct group *gr = NULL;
2413 char *repo_path = NULL;
2414 enum gotd_procid proc_id = PROC_GOTD;
2415 struct event evsigint, evsigterm, evsighup, evsigusr1;
2416 int *pack_fds = NULL, *temp_fds = NULL;
2418 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
2420 while ((ch = getopt(argc, argv, "Adf:LnP:RvW")) != -1) {
2421 switch (ch) {
2422 case 'A':
2423 proc_id = PROC_AUTH;
2424 break;
2425 case 'd':
2426 daemonize = 0;
2427 break;
2428 case 'f':
2429 confpath = optarg;
2430 break;
2431 case 'L':
2432 proc_id = PROC_LISTEN;
2433 break;
2434 case 'n':
2435 noaction = 1;
2436 break;
2437 case 'P':
2438 repo_path = realpath(optarg, NULL);
2439 if (repo_path == NULL)
2440 fatal("realpath '%s'", optarg);
2441 break;
2442 case 'R':
2443 proc_id = PROC_REPO_READ;
2444 break;
2445 case 'v':
2446 if (verbosity < 3)
2447 verbosity++;
2448 break;
2449 case 'W':
2450 proc_id = PROC_REPO_WRITE;
2451 break;
2452 default:
2453 usage();
2457 argc -= optind;
2458 argv += optind;
2460 if (argc != 0)
2461 usage();
2463 /* Require an absolute path in argv[0] for reliable re-exec. */
2464 if (!got_path_is_absolute(argv0))
2465 fatalx("bad path \"%s\": must be an absolute path", argv0);
2467 if (geteuid() && (proc_id == PROC_GOTD || proc_id == PROC_LISTEN))
2468 fatalx("need root privileges");
2470 log_init(daemonize ? 0 : 1, LOG_DAEMON);
2471 log_setverbose(verbosity);
2473 if (parse_config(confpath, proc_id, &gotd) != 0)
2474 return 1;
2476 gotd.argv0 = argv0;
2477 gotd.daemonize = daemonize;
2478 gotd.verbosity = verbosity;
2479 gotd.confpath = confpath;
2481 if (proc_id == PROC_GOTD &&
2482 (gotd.nrepos == 0 || TAILQ_EMPTY(&gotd.repos)))
2483 fatalx("no repository defined in configuration file");
2485 pw = getpwnam(gotd.user_name);
2486 if (pw == NULL)
2487 fatalx("user %s not found", gotd.user_name);
2489 if (pw->pw_uid == 0) {
2490 fatalx("cannot run %s as %s: the user running %s "
2491 "must not be the superuser",
2492 getprogname(), pw->pw_name, getprogname());
2495 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups) == -1)
2496 log_warnx("group membership list truncated");
2498 gr = match_group(groups, ngroups, gotd.unix_group_name);
2499 if (gr == NULL) {
2500 fatalx("cannot start %s: the user running %s "
2501 "must be a secondary member of group %s",
2502 getprogname(), getprogname(), gotd.unix_group_name);
2504 if (gr->gr_gid == pw->pw_gid) {
2505 fatalx("cannot start %s: the user running %s "
2506 "must be a secondary member of group %s, but "
2507 "%s is the user's primary group",
2508 getprogname(), getprogname(), gotd.unix_group_name,
2509 gotd.unix_group_name);
2512 if (proc_id == PROC_LISTEN &&
2513 !got_path_is_absolute(gotd.unix_socket_path))
2514 fatalx("bad unix socket path \"%s\": must be an absolute path",
2515 gotd.unix_socket_path);
2517 if (noaction)
2518 return 0;
2520 if (proc_id == PROC_GOTD) {
2521 gotd.pid = getpid();
2522 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2523 start_listener(argv0, confpath, daemonize, verbosity);
2524 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
2525 if (daemonize && daemon(1, 0) == -1)
2526 fatal("daemon");
2527 } else if (proc_id == PROC_LISTEN) {
2528 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2529 if (verbosity) {
2530 log_info("socket: %s", gotd.unix_socket_path);
2531 log_info("user: %s", pw->pw_name);
2532 log_info("secondary group: %s", gr->gr_name);
2535 fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
2536 gr->gr_gid);
2537 if (fd == -1) {
2538 fatal("cannot listen on unix socket %s",
2539 gotd.unix_socket_path);
2541 if (daemonize && daemon(0, 0) == -1)
2542 fatal("daemon");
2543 } else if (proc_id == PROC_AUTH) {
2544 snprintf(title, sizeof(title), "%s %s",
2545 gotd_proc_names[proc_id], repo_path);
2546 if (daemonize && daemon(0, 0) == -1)
2547 fatal("daemon");
2548 } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE) {
2549 error = got_repo_pack_fds_open(&pack_fds);
2550 if (error != NULL)
2551 fatalx("cannot open pack tempfiles: %s", error->msg);
2552 error = got_repo_temp_fds_open(&temp_fds);
2553 if (error != NULL)
2554 fatalx("cannot open pack tempfiles: %s", error->msg);
2555 if (repo_path == NULL)
2556 fatalx("repository path not specified");
2557 snprintf(title, sizeof(title), "%s %s",
2558 gotd_proc_names[proc_id], repo_path);
2559 if (daemonize && daemon(0, 0) == -1)
2560 fatal("daemon");
2561 } else
2562 fatal("invalid process id %d", proc_id);
2564 setproctitle("%s", title);
2565 log_procinit(title);
2567 /* Drop root privileges. */
2568 if (setgid(pw->pw_gid) == -1)
2569 fatal("setgid %d failed", pw->pw_gid);
2570 if (setuid(pw->pw_uid) == -1)
2571 fatal("setuid %d failed", pw->pw_uid);
2573 event_init();
2575 switch (proc_id) {
2576 case PROC_GOTD:
2577 #ifndef PROFILE
2578 if (pledge("stdio rpath wpath cpath proc exec "
2579 "sendfd recvfd fattr flock unveil", NULL) == -1)
2580 err(1, "pledge");
2581 #endif
2582 break;
2583 case PROC_LISTEN:
2584 #ifndef PROFILE
2585 if (pledge("stdio sendfd unix", NULL) == -1)
2586 err(1, "pledge");
2587 #endif
2588 listen_main(title, fd);
2589 /* NOTREACHED */
2590 break;
2591 case PROC_AUTH:
2592 #ifndef PROFILE
2593 if (pledge("stdio getpw recvfd unix unveil", NULL) == -1)
2594 err(1, "pledge");
2595 #endif
2597 * We need the "unix" pledge promise for getpeername(2) only.
2598 * Ensure that AF_UNIX bind(2) cannot be used by revoking all
2599 * filesystem access via unveil(2). Access to password database
2600 * files will still work since "getpw" bypasses unveil(2).
2602 apply_unveil_none();
2604 auth_main(title, &gotd.repos, repo_path);
2605 /* NOTREACHED */
2606 break;
2607 case PROC_REPO_READ:
2608 #ifndef PROFILE
2609 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2610 err(1, "pledge");
2611 #endif
2612 apply_unveil_repo_readonly(repo_path);
2613 repo_read_main(title, repo_path, pack_fds, temp_fds);
2614 /* NOTREACHED */
2615 exit(0);
2616 case PROC_REPO_WRITE:
2617 #ifndef PROFILE
2618 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2619 err(1, "pledge");
2620 #endif
2621 apply_unveil_repo_readonly(repo_path);
2622 repo_write_main(title, repo_path, pack_fds, temp_fds);
2623 /* NOTREACHED */
2624 exit(0);
2625 default:
2626 fatal("invalid process id %d", proc_id);
2629 if (proc_id != PROC_GOTD)
2630 fatal("invalid process id %d", proc_id);
2632 apply_unveil();
2634 signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
2635 signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
2636 signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
2637 signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
2638 signal(SIGPIPE, SIG_IGN);
2640 signal_add(&evsigint, NULL);
2641 signal_add(&evsigterm, NULL);
2642 signal_add(&evsighup, NULL);
2643 signal_add(&evsigusr1, NULL);
2645 gotd_imsg_event_add(&gotd.listen_proc.iev);
2647 event_dispatch();
2649 if (pack_fds)
2650 got_repo_pack_fds_close(pack_fds);
2651 free(repo_path);
2652 return 0;