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 /* Send pack pipe end 0 to repo_write. */
906 if (gotd_imsg_compose_event(&client->repo_write->iev,
907 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[0],
908 &ipipe, sizeof(ipipe)) == -1) {
909 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
910 pipe[0] = -1;
911 goto done;
913 pipe[0] = -1;
915 /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
916 if (gotd_imsg_compose_event(&client->iev,
917 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1], NULL, 0) == -1)
918 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
919 pipe[1] = -1;
921 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.pack",
922 client->repo_write->repo_path, GOT_OBJECTS_PACK_DIR,
923 client->euid) == -1) {
924 err = got_error_from_errno("asprintf");
925 goto done;
928 err = got_opentemp_named_fd(&pack_path, &packfd, basepath, "");
929 if (err)
930 goto done;
932 free(basepath);
933 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.idx",
934 client->repo_write->repo_path, GOT_OBJECTS_PACK_DIR,
935 client->euid) == -1) {
936 err = got_error_from_errno("asprintf");
937 basepath = NULL;
938 goto done;
940 err = got_opentemp_named_fd(&idx_path, &idxfd, basepath, "");
941 if (err)
942 goto done;
944 memset(&ifile, 0, sizeof(ifile));
945 ifile.client_id = client->id;
946 if (gotd_imsg_compose_event(&client->repo_write->iev,
947 GOTD_IMSG_PACKIDX_FILE, PROC_GOTD, idxfd,
948 &ifile, sizeof(ifile)) == -1) {
949 err = got_error_from_errno("imsg compose PACKIDX_FILE");
950 idxfd = -1;
951 goto done;
953 idxfd = -1;
955 memset(&ipack, 0, sizeof(ipack));
956 ipack.client_id = client->id;
957 if (client_has_capability(client, GOT_CAPA_REPORT_STATUS))
958 ipack.report_status = 1;
960 if (gotd_imsg_compose_event(&client->repo_write->iev,
961 GOTD_IMSG_RECV_PACKFILE, PROC_GOTD, packfd,
962 &ipack, sizeof(ipack)) == -1) {
963 err = got_error_from_errno("imsg compose RECV_PACKFILE");
964 packfd = -1;
965 goto done;
967 packfd = -1;
969 done:
970 free(basepath);
971 if (pipe[0] != -1 && close(pipe[0]) == -1 && err == NULL)
972 err = got_error_from_errno("close");
973 if (pipe[1] != -1 && close(pipe[1]) == -1 && err == NULL)
974 err = got_error_from_errno("close");
975 if (packfd != -1 && close(packfd) == -1 && err == NULL)
976 err = got_error_from_errno("close");
977 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
978 err = got_error_from_errno("close");
979 if (err) {
980 free(pack_path);
981 free(idx_path);
982 } else {
983 client->packfile_path = pack_path;
984 client->packidx_path = idx_path;
986 return err;
989 static void
990 gotd_request(int fd, short events, void *arg)
992 struct gotd_imsgev *iev = arg;
993 struct imsgbuf *ibuf = &iev->ibuf;
994 struct gotd_client *client = iev->handler_arg;
995 const struct got_error *err = NULL;
996 struct imsg imsg;
997 ssize_t n;
999 if (events & EV_WRITE) {
1000 while (ibuf->w.queued) {
1001 n = msgbuf_write(&ibuf->w);
1002 if (n == -1 && errno == EPIPE) {
1004 * The client has closed its socket.
1005 * This can happen when Git clients are
1006 * done sending pack file data.
1008 msgbuf_clear(&ibuf->w);
1009 continue;
1010 } else if (n == -1 && errno != EAGAIN) {
1011 err = got_error_from_errno("imsg_flush");
1012 disconnect_on_error(client, err);
1013 return;
1015 if (n == 0) {
1016 /* Connection closed. */
1017 err = got_error(GOT_ERR_EOF);
1018 disconnect_on_error(client, err);
1019 return;
1023 /* Disconnect gotctl(8) now that messages have been sent. */
1024 if (!client_is_reading(client) && !client_is_writing(client)) {
1025 disconnect(client);
1026 return;
1030 if ((events & EV_READ) == 0)
1031 return;
1033 memset(&imsg, 0, sizeof(imsg));
1035 while (err == NULL) {
1036 err = gotd_imsg_recv(&imsg, ibuf, 0);
1037 if (err) {
1038 if (err->code == GOT_ERR_PRIVSEP_READ)
1039 err = NULL;
1040 break;
1043 evtimer_del(&client->tmo);
1045 switch (imsg.hdr.type) {
1046 case GOTD_IMSG_INFO:
1047 err = send_info(client);
1048 break;
1049 case GOTD_IMSG_STOP:
1050 err = stop_gotd(client);
1051 break;
1052 case GOTD_IMSG_LIST_REFS:
1053 if (client->state != GOTD_STATE_EXPECT_LIST_REFS) {
1054 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1055 "unexpected list-refs request received");
1056 break;
1058 err = start_client_session(client, &imsg);
1059 if (err)
1060 break;
1061 break;
1062 case GOTD_IMSG_CAPABILITIES:
1063 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1064 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1065 "unexpected capabilities received");
1066 break;
1068 log_debug("receiving capabilities from uid %d",
1069 client->euid);
1070 err = recv_capabilities(client, &imsg);
1071 break;
1072 case GOTD_IMSG_CAPABILITY:
1073 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1074 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1075 "unexpected capability received");
1076 break;
1078 err = recv_capability(client, &imsg);
1079 if (err || client->ncapabilities < client->ncapa_alloc)
1080 break;
1081 if (client_is_reading(client)) {
1082 client->state = GOTD_STATE_EXPECT_WANT;
1083 log_debug("uid %d: expecting want-lines",
1084 client->euid);
1085 } else if (client_is_writing(client)) {
1086 client->state = GOTD_STATE_EXPECT_REF_UPDATE;
1087 log_debug("uid %d: expecting ref-update-lines",
1088 client->euid);
1089 } else
1090 fatalx("client %d is both reading and writing",
1091 client->euid);
1092 break;
1093 case GOTD_IMSG_WANT:
1094 if (client->state != GOTD_STATE_EXPECT_WANT) {
1095 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1096 "unexpected want-line received");
1097 break;
1099 log_debug("received want-line from uid %d",
1100 client->euid);
1101 err = ensure_client_is_reading(client);
1102 if (err)
1103 break;
1104 err = forward_want(client, &imsg);
1105 break;
1106 case GOTD_IMSG_REF_UPDATE:
1107 if (client->state != GOTD_STATE_EXPECT_REF_UPDATE) {
1108 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1109 "unexpected ref-update-line received");
1110 break;
1112 log_debug("received ref-update-line from uid %d",
1113 client->euid);
1114 err = ensure_client_is_writing(client);
1115 if (err)
1116 break;
1117 err = forward_ref_update(client, &imsg);
1118 if (err)
1119 break;
1120 client->state = GOTD_STATE_EXPECT_MORE_REF_UPDATES;
1121 break;
1122 case GOTD_IMSG_HAVE:
1123 if (client->state != GOTD_STATE_EXPECT_HAVE) {
1124 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1125 "unexpected have-line received");
1126 break;
1128 log_debug("received have-line from uid %d",
1129 client->euid);
1130 err = ensure_client_is_reading(client);
1131 if (err)
1132 break;
1133 err = forward_have(client, &imsg);
1134 if (err)
1135 break;
1136 break;
1137 case GOTD_IMSG_FLUSH:
1138 if (client->state == GOTD_STATE_EXPECT_WANT ||
1139 client->state == GOTD_STATE_EXPECT_HAVE) {
1140 err = ensure_client_is_reading(client);
1141 if (err)
1142 break;
1143 } else if (client->state ==
1144 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1145 err = ensure_client_is_writing(client);
1146 if (err)
1147 break;
1148 } else {
1149 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1150 "unexpected flush-pkt received");
1151 break;
1153 log_debug("received flush-pkt from uid %d",
1154 client->euid);
1155 if (client->state == GOTD_STATE_EXPECT_WANT) {
1156 client->state = GOTD_STATE_EXPECT_HAVE;
1157 log_debug("uid %d: expecting have-lines",
1158 client->euid);
1159 } else if (client->state == GOTD_STATE_EXPECT_HAVE) {
1160 client->state = GOTD_STATE_EXPECT_DONE;
1161 log_debug("uid %d: expecting 'done'",
1162 client->euid);
1163 } else if (client->state ==
1164 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1165 client->state = GOTD_STATE_EXPECT_PACKFILE;
1166 log_debug("uid %d: expecting packfile",
1167 client->euid);
1168 err = recv_packfile(client);
1169 } else {
1170 /* should not happen, see above */
1171 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1172 "unexpected client state");
1173 break;
1175 break;
1176 case GOTD_IMSG_DONE:
1177 if (client->state != GOTD_STATE_EXPECT_HAVE &&
1178 client->state != GOTD_STATE_EXPECT_DONE) {
1179 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1180 "unexpected flush-pkt received");
1181 break;
1183 log_debug("received 'done' from uid %d", client->euid);
1184 err = ensure_client_is_reading(client);
1185 if (err)
1186 break;
1187 client->state = GOTD_STATE_DONE;
1188 err = send_packfile(client);
1189 break;
1190 default:
1191 err = got_error(GOT_ERR_PRIVSEP_MSG);
1192 break;
1195 imsg_free(&imsg);
1198 if (err) {
1199 if (err->code != GOT_ERR_EOF ||
1200 client->state != GOTD_STATE_EXPECT_PACKFILE)
1201 disconnect_on_error(client, err);
1202 } else {
1203 gotd_imsg_event_add(&client->iev);
1204 if (client->state == GOTD_STATE_EXPECT_LIST_REFS)
1205 evtimer_add(&client->tmo, &auth_timeout);
1206 else
1207 evtimer_add(&client->tmo, &timeout);
1211 static void
1212 gotd_request_timeout(int fd, short events, void *arg)
1214 struct gotd_client *client = arg;
1216 log_debug("disconnecting uid %d due to timeout", client->euid);
1217 disconnect(client);
1220 static const struct got_error *
1221 recv_connect(uint32_t *client_id, struct imsg *imsg)
1223 const struct got_error *err = NULL;
1224 struct gotd_imsg_connect iconnect;
1225 size_t datalen;
1226 int s = -1;
1227 struct gotd_client *client = NULL;
1229 *client_id = 0;
1231 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1232 if (datalen != sizeof(iconnect))
1233 return got_error(GOT_ERR_PRIVSEP_LEN);
1234 memcpy(&iconnect, imsg->data, sizeof(iconnect));
1236 s = imsg->fd;
1237 if (s == -1) {
1238 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1239 goto done;
1242 if (find_client(iconnect.client_id)) {
1243 err = got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
1244 goto done;
1247 client = calloc(1, sizeof(*client));
1248 if (client == NULL) {
1249 err = got_error_from_errno("calloc");
1250 goto done;
1253 *client_id = iconnect.client_id;
1255 client->state = GOTD_STATE_EXPECT_LIST_REFS;
1256 client->id = iconnect.client_id;
1257 client->fd = s;
1258 s = -1;
1259 client->delta_cache_fd = -1;
1260 /* The auth process will verify UID/GID for us. */
1261 client->euid = iconnect.euid;
1262 client->egid = iconnect.egid;
1263 client->nref_updates = -1;
1265 imsg_init(&client->iev.ibuf, client->fd);
1266 client->iev.handler = gotd_request;
1267 client->iev.events = EV_READ;
1268 client->iev.handler_arg = client;
1270 event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
1271 &client->iev);
1272 gotd_imsg_event_add(&client->iev);
1274 evtimer_set(&client->tmo, gotd_request_timeout, client);
1276 add_client(client);
1277 log_debug("%s: new client uid %d connected on fd %d", __func__,
1278 client->euid, client->fd);
1279 done:
1280 if (err) {
1281 struct gotd_child_proc *listen_proc = &gotd.listen_proc;
1282 struct gotd_imsg_disconnect idisconnect;
1284 idisconnect.client_id = client->id;
1285 if (gotd_imsg_compose_event(&listen_proc->iev,
1286 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
1287 &idisconnect, sizeof(idisconnect)) == -1)
1288 log_warn("imsg compose DISCONNECT");
1290 if (s != -1)
1291 close(s);
1294 return err;
1297 static const char *gotd_proc_names[PROC_MAX] = {
1298 "parent",
1299 "listen",
1300 "auth",
1301 "repo_read",
1302 "repo_write"
1305 static void
1306 kill_proc(struct gotd_child_proc *proc, int fatal)
1308 if (fatal) {
1309 log_warnx("sending SIGKILL to PID %d", proc->pid);
1310 kill(proc->pid, SIGKILL);
1311 } else
1312 kill(proc->pid, SIGTERM);
1315 static void
1316 gotd_shutdown(void)
1318 struct gotd_child_proc *proc;
1319 uint64_t slot;
1321 for (slot = 0; slot < nitems(gotd_clients); slot++) {
1322 struct gotd_client *c, *tmp;
1324 STAILQ_FOREACH_SAFE(c, &gotd_clients[slot], entry, tmp)
1325 disconnect(c);
1328 proc = &gotd.listen_proc;
1329 msgbuf_clear(&proc->iev.ibuf.w);
1330 close(proc->iev.ibuf.fd);
1331 kill_proc(proc, 0);
1332 wait_for_child(proc->pid);
1334 log_info("terminating");
1335 exit(0);
1338 void
1339 gotd_sighdlr(int sig, short event, void *arg)
1342 * Normal signal handler rules don't apply because libevent
1343 * decouples for us.
1346 switch (sig) {
1347 case SIGHUP:
1348 log_info("%s: ignoring SIGHUP", __func__);
1349 break;
1350 case SIGUSR1:
1351 log_info("%s: ignoring SIGUSR1", __func__);
1352 break;
1353 case SIGTERM:
1354 case SIGINT:
1355 gotd_shutdown();
1356 log_warnx("gotd terminating");
1357 exit(0);
1358 break;
1359 default:
1360 fatalx("unexpected signal");
1364 static const struct got_error *
1365 ensure_proc_is_reading(struct gotd_client *client,
1366 struct gotd_child_proc *proc)
1368 if (!client_is_reading(client)) {
1369 kill_proc(proc, 1);
1370 return got_error_fmt(GOT_ERR_BAD_PACKET,
1371 "PID %d handled a read-request for uid %d but this "
1372 "user is not reading from a repository", proc->pid,
1373 client->euid);
1376 return NULL;
1379 static const struct got_error *
1380 ensure_proc_is_writing(struct gotd_client *client,
1381 struct gotd_child_proc *proc)
1383 if (!client_is_writing(client)) {
1384 kill_proc(proc, 1);
1385 return got_error_fmt(GOT_ERR_BAD_PACKET,
1386 "PID %d handled a write-request for uid %d but this "
1387 "user is not writing to a repository", proc->pid,
1388 client->euid);
1391 return NULL;
1394 static int
1395 verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
1396 struct imsg *imsg)
1398 const struct got_error *err;
1399 struct gotd_child_proc *client_proc;
1400 int ret = 0;
1402 if (proc->type == PROC_REPO_READ || proc->type == PROC_REPO_WRITE) {
1403 client_proc = get_client_proc(client);
1404 if (client_proc == NULL)
1405 fatalx("no process found for uid %d", client->euid);
1406 if (proc->pid != client_proc->pid) {
1407 kill_proc(proc, 1);
1408 log_warnx("received message from PID %d for uid %d, "
1409 "while PID %d is the process serving this user",
1410 proc->pid, client->euid, client_proc->pid);
1411 return 0;
1415 switch (imsg->hdr.type) {
1416 case GOTD_IMSG_ERROR:
1417 ret = 1;
1418 break;
1419 case GOTD_IMSG_CONNECT:
1420 if (proc->type != PROC_LISTEN) {
1421 err = got_error_fmt(GOT_ERR_BAD_PACKET,
1422 "new connection for uid %d from PID %d "
1423 "which is not the listen process",
1424 proc->pid, client->euid);
1425 } else
1426 ret = 1;
1427 break;
1428 case GOTD_IMSG_ACCESS_GRANTED:
1429 if (proc->type != PROC_AUTH) {
1430 err = got_error_fmt(GOT_ERR_BAD_PACKET,
1431 "authentication of uid %d from PID %d "
1432 "which is not the auth process",
1433 proc->pid, client->euid);
1434 } else
1435 ret = 1;
1436 break;
1437 case GOTD_IMSG_REPO_CHILD_READY:
1438 if (proc->type != PROC_REPO_READ &&
1439 proc->type != PROC_REPO_WRITE) {
1440 err = got_error_fmt(GOT_ERR_BAD_PACKET,
1441 "unexpected \"ready\" signal from PID %d",
1442 proc->pid);
1443 } else
1444 ret = 1;
1445 break;
1446 case GOTD_IMSG_PACKFILE_DONE:
1447 err = ensure_proc_is_reading(client, proc);
1448 if (err)
1449 log_warnx("uid %d: %s", client->euid, err->msg);
1450 else
1451 ret = 1;
1452 break;
1453 case GOTD_IMSG_PACKFILE_INSTALL:
1454 case GOTD_IMSG_REF_UPDATES_START:
1455 case GOTD_IMSG_REF_UPDATE:
1456 err = ensure_proc_is_writing(client, proc);
1457 if (err)
1458 log_warnx("uid %d: %s", client->euid, err->msg);
1459 else
1460 ret = 1;
1461 break;
1462 default:
1463 log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
1464 break;
1467 return ret;
1470 static const struct got_error *
1471 list_refs_request(struct gotd_client *client, struct gotd_imsgev *iev)
1473 static const struct got_error *err;
1474 struct gotd_imsg_list_refs_internal ilref;
1475 int fd;
1477 memset(&ilref, 0, sizeof(ilref));
1478 ilref.client_id = client->id;
1480 fd = dup(client->fd);
1481 if (fd == -1)
1482 return got_error_from_errno("dup");
1484 if (gotd_imsg_compose_event(iev, GOTD_IMSG_LIST_REFS_INTERNAL,
1485 PROC_GOTD, fd, &ilref, sizeof(ilref)) == -1) {
1486 err = got_error_from_errno("imsg compose WANT");
1487 close(fd);
1488 return err;
1491 client->state = GOTD_STATE_EXPECT_CAPABILITIES;
1492 log_debug("uid %d: expecting capabilities", client->euid);
1493 return NULL;
1496 static const struct got_error *
1497 recv_packfile_done(uint32_t *client_id, struct imsg *imsg)
1499 struct gotd_imsg_packfile_done idone;
1500 size_t datalen;
1502 log_debug("packfile-done received");
1504 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1505 if (datalen != sizeof(idone))
1506 return got_error(GOT_ERR_PRIVSEP_LEN);
1507 memcpy(&idone, imsg->data, sizeof(idone));
1509 *client_id = idone.client_id;
1510 return NULL;
1513 static const struct got_error *
1514 recv_packfile_install(uint32_t *client_id, struct imsg *imsg)
1516 struct gotd_imsg_packfile_install inst;
1517 size_t datalen;
1519 log_debug("packfile-install received");
1521 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1522 if (datalen != sizeof(inst))
1523 return got_error(GOT_ERR_PRIVSEP_LEN);
1524 memcpy(&inst, imsg->data, sizeof(inst));
1526 *client_id = inst.client_id;
1527 return NULL;
1530 static const struct got_error *
1531 recv_ref_updates_start(uint32_t *client_id, struct imsg *imsg)
1533 struct gotd_imsg_ref_updates_start istart;
1534 size_t datalen;
1536 log_debug("ref-updates-start received");
1538 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1539 if (datalen != sizeof(istart))
1540 return got_error(GOT_ERR_PRIVSEP_LEN);
1541 memcpy(&istart, imsg->data, sizeof(istart));
1543 *client_id = istart.client_id;
1544 return NULL;
1547 static const struct got_error *
1548 recv_ref_update(uint32_t *client_id, struct imsg *imsg)
1550 struct gotd_imsg_ref_update iref;
1551 size_t datalen;
1553 log_debug("ref-update received");
1555 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1556 if (datalen < sizeof(iref))
1557 return got_error(GOT_ERR_PRIVSEP_LEN);
1558 memcpy(&iref, imsg->data, sizeof(iref));
1560 *client_id = iref.client_id;
1561 return NULL;
1564 static const struct got_error *
1565 send_ref_update_ok(struct gotd_client *client,
1566 struct gotd_imsg_ref_update *iref, const char *refname)
1568 struct gotd_imsg_ref_update_ok iok;
1569 struct ibuf *wbuf;
1570 size_t len;
1572 memset(&iok, 0, sizeof(iok));
1573 iok.client_id = client->id;
1574 memcpy(iok.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1575 memcpy(iok.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1576 iok.name_len = strlen(refname);
1578 len = sizeof(iok) + iok.name_len;
1579 wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_OK,
1580 PROC_GOTD, gotd.pid, len);
1581 if (wbuf == NULL)
1582 return got_error_from_errno("imsg_create REF_UPDATE_OK");
1584 if (imsg_add(wbuf, &iok, sizeof(iok)) == -1)
1585 return got_error_from_errno("imsg_add REF_UPDATE_OK");
1586 if (imsg_add(wbuf, refname, iok.name_len) == -1)
1587 return got_error_from_errno("imsg_add REF_UPDATE_OK");
1589 wbuf->fd = -1;
1590 imsg_close(&client->iev.ibuf, wbuf);
1591 gotd_imsg_event_add(&client->iev);
1592 return NULL;
1595 static void
1596 send_refs_updated(struct gotd_client *client)
1598 if (gotd_imsg_compose_event(&client->iev,
1599 GOTD_IMSG_REFS_UPDATED, PROC_GOTD, -1, NULL, 0) == -1)
1600 log_warn("imsg compose REFS_UPDATED");
1603 static const struct got_error *
1604 send_ref_update_ng(struct gotd_client *client,
1605 struct gotd_imsg_ref_update *iref, const char *refname,
1606 const char *reason)
1608 const struct got_error *ng_err;
1609 struct gotd_imsg_ref_update_ng ing;
1610 struct ibuf *wbuf;
1611 size_t len;
1613 memset(&ing, 0, sizeof(ing));
1614 ing.client_id = client->id;
1615 memcpy(ing.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1616 memcpy(ing.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1617 ing.name_len = strlen(refname);
1619 ng_err = got_error_fmt(GOT_ERR_REF_BUSY, "%s", reason);
1620 ing.reason_len = strlen(ng_err->msg);
1622 len = sizeof(ing) + ing.name_len + ing.reason_len;
1623 wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_NG,
1624 PROC_GOTD, gotd.pid, len);
1625 if (wbuf == NULL)
1626 return got_error_from_errno("imsg_create REF_UPDATE_NG");
1628 if (imsg_add(wbuf, &ing, sizeof(ing)) == -1)
1629 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1630 if (imsg_add(wbuf, refname, ing.name_len) == -1)
1631 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1632 if (imsg_add(wbuf, ng_err->msg, ing.reason_len) == -1)
1633 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1635 wbuf->fd = -1;
1636 imsg_close(&client->iev.ibuf, wbuf);
1637 gotd_imsg_event_add(&client->iev);
1638 return NULL;
1641 static const struct got_error *
1642 install_pack(struct gotd_client *client, const char *repo_path,
1643 struct imsg *imsg)
1645 const struct got_error *err = NULL;
1646 struct gotd_imsg_packfile_install inst;
1647 char hex[SHA1_DIGEST_STRING_LENGTH];
1648 size_t datalen;
1649 char *packfile_path = NULL, *packidx_path = NULL;
1651 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1652 if (datalen != sizeof(inst))
1653 return got_error(GOT_ERR_PRIVSEP_LEN);
1654 memcpy(&inst, imsg->data, sizeof(inst));
1656 if (client->packfile_path == NULL)
1657 return got_error_msg(GOT_ERR_BAD_REQUEST,
1658 "client has no pack file");
1659 if (client->packidx_path == NULL)
1660 return got_error_msg(GOT_ERR_BAD_REQUEST,
1661 "client has no pack file index");
1663 if (got_sha1_digest_to_str(inst.pack_sha1, hex, sizeof(hex)) == NULL)
1664 return got_error_msg(GOT_ERR_NO_SPACE,
1665 "could not convert pack file SHA1 to hex");
1667 if (asprintf(&packfile_path, "/%s/%s/pack-%s.pack",
1668 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1669 err = got_error_from_errno("asprintf");
1670 goto done;
1673 if (asprintf(&packidx_path, "/%s/%s/pack-%s.idx",
1674 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1675 err = got_error_from_errno("asprintf");
1676 goto done;
1679 if (rename(client->packfile_path, packfile_path) == -1) {
1680 err = got_error_from_errno3("rename", client->packfile_path,
1681 packfile_path);
1682 goto done;
1685 free(client->packfile_path);
1686 client->packfile_path = NULL;
1688 if (rename(client->packidx_path, packidx_path) == -1) {
1689 err = got_error_from_errno3("rename", client->packidx_path,
1690 packidx_path);
1691 goto done;
1694 free(client->packidx_path);
1695 client->packidx_path = NULL;
1696 done:
1697 free(packfile_path);
1698 free(packidx_path);
1699 return err;
1702 static const struct got_error *
1703 begin_ref_updates(struct gotd_client *client, struct imsg *imsg)
1705 struct gotd_imsg_ref_updates_start istart;
1706 size_t datalen;
1708 if (client->nref_updates != -1)
1709 return got_error(GOT_ERR_PRIVSEP_MSG);
1711 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1712 if (datalen != sizeof(istart))
1713 return got_error(GOT_ERR_PRIVSEP_LEN);
1714 memcpy(&istart, imsg->data, sizeof(istart));
1716 if (istart.nref_updates <= 0)
1717 return got_error(GOT_ERR_PRIVSEP_MSG);
1719 client->nref_updates = istart.nref_updates;
1720 return NULL;
1723 static const struct got_error *
1724 update_ref(struct gotd_client *client, const char *repo_path,
1725 struct imsg *imsg)
1727 const struct got_error *err = NULL;
1728 struct got_repository *repo = NULL;
1729 struct got_reference *ref = NULL;
1730 struct gotd_imsg_ref_update iref;
1731 struct got_object_id old_id, new_id;
1732 struct got_object_id *id = NULL;
1733 struct got_object *obj = NULL;
1734 char *refname = NULL;
1735 size_t datalen;
1736 int locked = 0;
1738 log_debug("update-ref from uid %d", client->euid);
1740 if (client->nref_updates <= 0)
1741 return got_error(GOT_ERR_PRIVSEP_MSG);
1743 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1744 if (datalen < sizeof(iref))
1745 return got_error(GOT_ERR_PRIVSEP_LEN);
1746 memcpy(&iref, imsg->data, sizeof(iref));
1747 if (datalen != sizeof(iref) + iref.name_len)
1748 return got_error(GOT_ERR_PRIVSEP_LEN);
1749 refname = malloc(iref.name_len + 1);
1750 if (refname == NULL)
1751 return got_error_from_errno("malloc");
1752 memcpy(refname, imsg->data + sizeof(iref), iref.name_len);
1753 refname[iref.name_len] = '\0';
1755 log_debug("updating ref %s for uid %d", refname, client->euid);
1757 err = got_repo_open(&repo, repo_path, NULL, NULL);
1758 if (err)
1759 goto done;
1761 memcpy(old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
1762 memcpy(new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
1763 err = got_object_open(&obj, repo, &new_id);
1764 if (err)
1765 goto done;
1767 if (iref.ref_is_new) {
1768 err = got_ref_open(&ref, repo, refname, 0);
1769 if (err) {
1770 if (err->code != GOT_ERR_NOT_REF)
1771 goto done;
1772 err = got_ref_alloc(&ref, refname, &new_id);
1773 if (err)
1774 goto done;
1775 err = got_ref_write(ref, repo); /* will lock/unlock */
1776 if (err)
1777 goto done;
1778 } else {
1779 err = got_error_fmt(GOT_ERR_REF_BUSY,
1780 "%s has been created by someone else "
1781 "while transaction was in progress",
1782 got_ref_get_name(ref));
1783 goto done;
1785 } else {
1786 err = got_ref_open(&ref, repo, refname, 1 /* lock */);
1787 if (err)
1788 goto done;
1789 locked = 1;
1791 err = got_ref_resolve(&id, repo, ref);
1792 if (err)
1793 goto done;
1795 if (got_object_id_cmp(id, &old_id) != 0) {
1796 err = got_error_fmt(GOT_ERR_REF_BUSY,
1797 "%s has been modified by someone else "
1798 "while transaction was in progress",
1799 got_ref_get_name(ref));
1800 goto done;
1803 err = got_ref_change_ref(ref, &new_id);
1804 if (err)
1805 goto done;
1807 err = got_ref_write(ref, repo);
1808 if (err)
1809 goto done;
1811 free(id);
1812 id = NULL;
1814 done:
1815 if (err) {
1816 if (err->code == GOT_ERR_LOCKFILE_TIMEOUT) {
1817 err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
1818 "could not acquire exclusive file lock for %s",
1819 refname);
1821 send_ref_update_ng(client, &iref, refname, err->msg);
1822 } else
1823 send_ref_update_ok(client, &iref, refname);
1825 if (client->nref_updates > 0) {
1826 client->nref_updates--;
1827 if (client->nref_updates == 0)
1828 send_refs_updated(client);
1831 if (locked) {
1832 const struct got_error *unlock_err;
1833 unlock_err = got_ref_unlock(ref);
1834 if (unlock_err && err == NULL)
1835 err = unlock_err;
1837 if (ref)
1838 got_ref_close(ref);
1839 if (obj)
1840 got_object_close(obj);
1841 if (repo)
1842 got_repo_close(repo);
1843 free(refname);
1844 free(id);
1845 return err;
1848 static void
1849 gotd_dispatch_listener(int fd, short event, void *arg)
1851 struct gotd_imsgev *iev = arg;
1852 struct imsgbuf *ibuf = &iev->ibuf;
1853 struct gotd_child_proc *proc = &gotd.listen_proc;
1854 ssize_t n;
1855 int shut = 0;
1856 struct imsg imsg;
1858 if (proc->iev.ibuf.fd != fd)
1859 fatalx("%s: unexpected fd %d", __func__, fd);
1861 if (event & EV_READ) {
1862 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1863 fatal("imsg_read error");
1864 if (n == 0) {
1865 /* Connection closed. */
1866 shut = 1;
1867 goto done;
1871 if (event & EV_WRITE) {
1872 n = msgbuf_write(&ibuf->w);
1873 if (n == -1 && errno != EAGAIN)
1874 fatal("msgbuf_write");
1875 if (n == 0) {
1876 /* Connection closed. */
1877 shut = 1;
1878 goto done;
1882 for (;;) {
1883 const struct got_error *err = NULL;
1884 struct gotd_client *client = NULL;
1885 uint32_t client_id = 0;
1886 int do_disconnect = 0;
1888 if ((n = imsg_get(ibuf, &imsg)) == -1)
1889 fatal("%s: imsg_get error", __func__);
1890 if (n == 0) /* No more messages. */
1891 break;
1893 switch (imsg.hdr.type) {
1894 case GOTD_IMSG_ERROR:
1895 do_disconnect = 1;
1896 err = gotd_imsg_recv_error(&client_id, &imsg);
1897 break;
1898 case GOTD_IMSG_CONNECT:
1899 err = recv_connect(&client_id, &imsg);
1900 break;
1901 default:
1902 log_debug("unexpected imsg %d", imsg.hdr.type);
1903 break;
1906 client = find_client(client_id);
1907 if (client == NULL) {
1908 log_warnx("%s: client not found", __func__);
1909 imsg_free(&imsg);
1910 continue;
1913 if (err)
1914 log_warnx("uid %d: %s", client->euid, err->msg);
1916 if (do_disconnect) {
1917 if (err)
1918 disconnect_on_error(client, err);
1919 else
1920 disconnect(client);
1923 imsg_free(&imsg);
1925 done:
1926 if (!shut) {
1927 gotd_imsg_event_add(iev);
1928 } else {
1929 /* This pipe is dead. Remove its event handler */
1930 event_del(&iev->ev);
1931 event_loopexit(NULL);
1935 static void
1936 gotd_dispatch_auth_child(int fd, short event, void *arg)
1938 const struct got_error *err = NULL;
1939 struct gotd_imsgev *iev = arg;
1940 struct imsgbuf *ibuf = &iev->ibuf;
1941 struct gotd_client *client;
1942 struct gotd_repo *repo = NULL;
1943 ssize_t n;
1944 int shut = 0;
1945 struct imsg imsg;
1946 uint32_t client_id = 0;
1947 int do_disconnect = 0;
1948 enum gotd_procid proc_type;
1950 client = find_client_by_proc_fd(fd);
1951 if (client == NULL)
1952 fatalx("cannot find client for fd %d", fd);
1954 if (client->auth == NULL)
1955 fatalx("cannot find auth child process for fd %d", fd);
1957 if (event & EV_READ) {
1958 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1959 fatal("imsg_read error");
1960 if (n == 0) {
1961 /* Connection closed. */
1962 shut = 1;
1963 goto done;
1967 if (event & EV_WRITE) {
1968 n = msgbuf_write(&ibuf->w);
1969 if (n == -1 && errno != EAGAIN)
1970 fatal("msgbuf_write");
1971 if (n == 0) {
1972 /* Connection closed. */
1973 shut = 1;
1975 goto done;
1978 if (client->auth->iev.ibuf.fd != fd)
1979 fatalx("%s: unexpected fd %d", __func__, fd);
1981 if ((n = imsg_get(ibuf, &imsg)) == -1)
1982 fatal("%s: imsg_get error", __func__);
1983 if (n == 0) /* No more messages. */
1984 return;
1986 evtimer_del(&client->tmo);
1988 switch (imsg.hdr.type) {
1989 case GOTD_IMSG_ERROR:
1990 do_disconnect = 1;
1991 err = gotd_imsg_recv_error(&client_id, &imsg);
1992 break;
1993 case GOTD_IMSG_ACCESS_GRANTED:
1994 break;
1995 default:
1996 do_disconnect = 1;
1997 log_debug("unexpected imsg %d", imsg.hdr.type);
1998 break;
2001 if (!verify_imsg_src(client, client->auth, &imsg)) {
2002 do_disconnect = 1;
2003 log_debug("dropping imsg type %d from PID %d",
2004 imsg.hdr.type, client->auth->pid);
2006 imsg_free(&imsg);
2008 if (do_disconnect) {
2009 if (err)
2010 disconnect_on_error(client, err);
2011 else
2012 disconnect(client);
2013 goto done;
2016 repo = find_repo_by_name(client->auth->repo_name);
2017 if (repo == NULL) {
2018 err = got_error(GOT_ERR_NOT_GIT_REPO);
2019 goto done;
2021 kill_auth_proc(client);
2023 log_info("authenticated uid %d for repository %s\n",
2024 client->euid, repo->name);
2026 if (client->required_auth & GOTD_AUTH_WRITE)
2027 proc_type = PROC_REPO_WRITE;
2028 else
2029 proc_type = PROC_REPO_READ;
2031 err = start_repo_child(client, proc_type, repo, gotd.argv0,
2032 gotd.confpath, gotd.daemonize, gotd.verbosity);
2033 done:
2034 if (err)
2035 log_warnx("uid %d: %s", client->euid, err->msg);
2037 /* We might have killed the auth process by now. */
2038 if (client->auth != NULL) {
2039 if (!shut) {
2040 gotd_imsg_event_add(iev);
2041 } else {
2042 /* This pipe is dead. Remove its event handler */
2043 event_del(&iev->ev);
2048 static void
2049 gotd_dispatch_repo_child(int fd, short event, void *arg)
2051 struct gotd_imsgev *iev = arg;
2052 struct imsgbuf *ibuf = &iev->ibuf;
2053 struct gotd_child_proc *proc = NULL;
2054 struct gotd_client *client = NULL;
2055 ssize_t n;
2056 int shut = 0;
2057 struct imsg imsg;
2059 if (event & EV_READ) {
2060 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
2061 fatal("imsg_read error");
2062 if (n == 0) {
2063 /* Connection closed. */
2064 shut = 1;
2065 goto done;
2069 if (event & EV_WRITE) {
2070 n = msgbuf_write(&ibuf->w);
2071 if (n == -1 && errno != EAGAIN)
2072 fatal("msgbuf_write");
2073 if (n == 0) {
2074 /* Connection closed. */
2075 shut = 1;
2076 goto done;
2080 client = find_client_by_proc_fd(fd);
2081 if (client == NULL)
2082 fatalx("cannot find client for fd %d", fd);
2084 proc = get_client_proc(client);
2085 if (proc == NULL)
2086 fatalx("cannot find child process for fd %d", fd);
2088 for (;;) {
2089 const struct got_error *err = NULL;
2090 uint32_t client_id = 0;
2091 int do_disconnect = 0;
2092 int do_list_refs = 0, do_ref_updates = 0, do_ref_update = 0;
2093 int do_packfile_install = 0;
2095 if ((n = imsg_get(ibuf, &imsg)) == -1)
2096 fatal("%s: imsg_get error", __func__);
2097 if (n == 0) /* No more messages. */
2098 break;
2100 switch (imsg.hdr.type) {
2101 case GOTD_IMSG_ERROR:
2102 do_disconnect = 1;
2103 err = gotd_imsg_recv_error(&client_id, &imsg);
2104 break;
2105 case GOTD_IMSG_REPO_CHILD_READY:
2106 do_list_refs = 1;
2107 break;
2108 case GOTD_IMSG_PACKFILE_DONE:
2109 do_disconnect = 1;
2110 err = recv_packfile_done(&client_id, &imsg);
2111 break;
2112 case GOTD_IMSG_PACKFILE_INSTALL:
2113 err = recv_packfile_install(&client_id, &imsg);
2114 if (err == NULL)
2115 do_packfile_install = 1;
2116 break;
2117 case GOTD_IMSG_REF_UPDATES_START:
2118 err = recv_ref_updates_start(&client_id, &imsg);
2119 if (err == NULL)
2120 do_ref_updates = 1;
2121 break;
2122 case GOTD_IMSG_REF_UPDATE:
2123 err = recv_ref_update(&client_id, &imsg);
2124 if (err == NULL)
2125 do_ref_update = 1;
2126 break;
2127 default:
2128 log_debug("unexpected imsg %d", imsg.hdr.type);
2129 break;
2132 if (!verify_imsg_src(client, proc, &imsg)) {
2133 log_debug("dropping imsg type %d from PID %d",
2134 imsg.hdr.type, proc->pid);
2135 imsg_free(&imsg);
2136 continue;
2138 if (err)
2139 log_warnx("uid %d: %s", client->euid, err->msg);
2141 if (do_disconnect) {
2142 if (err)
2143 disconnect_on_error(client, err);
2144 else
2145 disconnect(client);
2146 } else {
2147 if (do_list_refs)
2148 err = list_refs_request(client, iev);
2149 else if (do_packfile_install)
2150 err = install_pack(client, proc->repo_path,
2151 &imsg);
2152 else if (do_ref_updates)
2153 err = begin_ref_updates(client, &imsg);
2154 else if (do_ref_update)
2155 err = update_ref(client, proc->repo_path,
2156 &imsg);
2157 if (err)
2158 log_warnx("uid %d: %s", client->euid, err->msg);
2160 imsg_free(&imsg);
2162 done:
2163 if (!shut) {
2164 gotd_imsg_event_add(iev);
2165 } else {
2166 /* This pipe is dead. Remove its event handler */
2167 event_del(&iev->ev);
2168 event_loopexit(NULL);
2172 static pid_t
2173 start_child(enum gotd_procid proc_id, const char *repo_path,
2174 char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
2176 char *argv[11];
2177 int argc = 0;
2178 pid_t pid;
2180 switch (pid = fork()) {
2181 case -1:
2182 fatal("cannot fork");
2183 case 0:
2184 break;
2185 default:
2186 close(fd);
2187 return pid;
2190 if (fd != GOTD_FILENO_MSG_PIPE) {
2191 if (dup2(fd, GOTD_FILENO_MSG_PIPE) == -1)
2192 fatal("cannot setup imsg fd");
2193 } else if (fcntl(fd, F_SETFD, 0) == -1)
2194 fatal("cannot setup imsg fd");
2196 argv[argc++] = argv0;
2197 switch (proc_id) {
2198 case PROC_LISTEN:
2199 argv[argc++] = (char *)"-L";
2200 break;
2201 case PROC_AUTH:
2202 argv[argc++] = (char *)"-A";
2203 break;
2204 case PROC_REPO_READ:
2205 argv[argc++] = (char *)"-R";
2206 break;
2207 case PROC_REPO_WRITE:
2208 argv[argc++] = (char *)"-W";
2209 break;
2210 default:
2211 fatalx("invalid process id %d", proc_id);
2214 argv[argc++] = (char *)"-f";
2215 argv[argc++] = (char *)confpath;
2217 if (repo_path) {
2218 argv[argc++] = (char *)"-P";
2219 argv[argc++] = (char *)repo_path;
2222 if (!daemonize)
2223 argv[argc++] = (char *)"-d";
2224 if (verbosity > 0)
2225 argv[argc++] = (char *)"-v";
2226 if (verbosity > 1)
2227 argv[argc++] = (char *)"-v";
2228 argv[argc++] = NULL;
2230 execvp(argv0, argv);
2231 fatal("execvp");
2234 static void
2235 start_listener(char *argv0, const char *confpath, int daemonize, int verbosity)
2237 struct gotd_child_proc *proc = &gotd.listen_proc;
2239 proc->type = PROC_LISTEN;
2241 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
2242 PF_UNSPEC, proc->pipe) == -1)
2243 fatal("socketpair");
2245 proc->pid = start_child(proc->type, NULL, argv0, confpath,
2246 proc->pipe[1], daemonize, verbosity);
2247 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
2248 proc->iev.handler = gotd_dispatch_listener;
2249 proc->iev.events = EV_READ;
2250 proc->iev.handler_arg = NULL;
2253 static const struct got_error *
2254 start_repo_child(struct gotd_client *client, enum gotd_procid proc_type,
2255 struct gotd_repo *repo, char *argv0, const char *confpath,
2256 int daemonize, int verbosity)
2258 struct gotd_child_proc *proc;
2260 if (proc_type != PROC_REPO_READ && proc_type != PROC_REPO_WRITE)
2261 return got_error_msg(GOT_ERR_NOT_IMPL, "bad process type");
2263 proc = calloc(1, sizeof(*proc));
2264 if (proc == NULL)
2265 return got_error_from_errno("calloc");
2267 proc->type = proc_type;
2268 if (strlcpy(proc->repo_name, repo->name,
2269 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
2270 fatalx("repository name too long: %s", repo->name);
2271 log_debug("starting %s for repository %s",
2272 proc->type == PROC_REPO_READ ? "reader" : "writer", repo->name);
2273 if (realpath(repo->path, proc->repo_path) == NULL)
2274 fatal("%s", repo->path);
2275 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
2276 PF_UNSPEC, proc->pipe) == -1)
2277 fatal("socketpair");
2278 proc->pid = start_child(proc->type, proc->repo_path, argv0,
2279 confpath, proc->pipe[1], daemonize, verbosity);
2280 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
2281 log_debug("proc %s %s is on fd %d",
2282 gotd_proc_names[proc->type], proc->repo_path,
2283 proc->pipe[0]);
2284 proc->iev.handler = gotd_dispatch_repo_child;
2285 proc->iev.events = EV_READ;
2286 proc->iev.handler_arg = NULL;
2287 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
2288 gotd_dispatch_repo_child, &proc->iev);
2289 gotd_imsg_event_add(&proc->iev);
2291 if (proc->type == PROC_REPO_READ)
2292 client->repo_read = proc;
2293 else
2294 client->repo_write = proc;
2296 return NULL;
2299 static const struct got_error *
2300 start_auth_child(struct gotd_client *client, int required_auth,
2301 struct gotd_repo *repo, char *argv0, const char *confpath,
2302 int daemonize, int verbosity)
2304 const struct got_error *err = NULL;
2305 struct gotd_child_proc *proc;
2306 struct gotd_imsg_auth iauth;
2307 int fd;
2309 memset(&iauth, 0, sizeof(iauth));
2311 fd = dup(client->fd);
2312 if (fd == -1)
2313 return got_error_from_errno("dup");
2315 proc = calloc(1, sizeof(*proc));
2316 if (proc == NULL) {
2317 err = got_error_from_errno("calloc");
2318 close(fd);
2319 return err;
2322 proc->type = PROC_AUTH;
2323 if (strlcpy(proc->repo_name, repo->name,
2324 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
2325 fatalx("repository name too long: %s", repo->name);
2326 log_debug("starting auth for uid %d repository %s",
2327 client->euid, repo->name);
2328 if (realpath(repo->path, proc->repo_path) == NULL)
2329 fatal("%s", repo->path);
2330 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
2331 PF_UNSPEC, proc->pipe) == -1)
2332 fatal("socketpair");
2333 proc->pid = start_child(proc->type, proc->repo_path, argv0,
2334 confpath, proc->pipe[1], daemonize, verbosity);
2335 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
2336 log_debug("proc %s %s is on fd %d",
2337 gotd_proc_names[proc->type], proc->repo_path,
2338 proc->pipe[0]);
2339 proc->iev.handler = gotd_dispatch_auth_child;
2340 proc->iev.events = EV_READ;
2341 proc->iev.handler_arg = NULL;
2342 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
2343 gotd_dispatch_auth_child, &proc->iev);
2344 gotd_imsg_event_add(&proc->iev);
2346 iauth.euid = client->euid;
2347 iauth.egid = client->egid;
2348 iauth.required_auth = required_auth;
2349 iauth.client_id = client->id;
2350 if (gotd_imsg_compose_event(&proc->iev, GOTD_IMSG_AUTHENTICATE,
2351 PROC_GOTD, fd, &iauth, sizeof(iauth)) == -1) {
2352 log_warn("imsg compose AUTHENTICATE");
2353 close(fd);
2354 /* Let the auth_timeout handler tidy up. */
2357 client->auth = proc;
2358 client->required_auth = required_auth;
2359 return NULL;
2362 static void
2363 apply_unveil_repo_readonly(const char *repo_path)
2365 if (unveil(repo_path, "r") == -1)
2366 fatal("unveil %s", repo_path);
2368 if (unveil(NULL, NULL) == -1)
2369 fatal("unveil");
2372 static void
2373 apply_unveil_none(void)
2375 if (unveil("/", "") == -1)
2376 fatal("unveil");
2378 if (unveil(NULL, NULL) == -1)
2379 fatal("unveil");
2382 static void
2383 apply_unveil(void)
2385 struct gotd_repo *repo;
2387 if (unveil(gotd.argv0, "x") == -1)
2388 fatal("unveil %s", gotd.argv0);
2390 TAILQ_FOREACH(repo, &gotd.repos, entry) {
2391 if (unveil(repo->path, "rwc") == -1)
2392 fatal("unveil %s", repo->path);
2395 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
2396 fatal("unveil %s", GOT_TMPDIR_STR);
2398 if (unveil(NULL, NULL) == -1)
2399 fatal("unveil");
2402 int
2403 main(int argc, char **argv)
2405 const struct got_error *error = NULL;
2406 int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
2407 const char *confpath = GOTD_CONF_PATH;
2408 char *argv0 = argv[0];
2409 char title[2048];
2410 gid_t groups[NGROUPS_MAX];
2411 int ngroups = NGROUPS_MAX;
2412 struct passwd *pw = NULL;
2413 struct group *gr = NULL;
2414 char *repo_path = NULL;
2415 enum gotd_procid proc_id = PROC_GOTD;
2416 struct event evsigint, evsigterm, evsighup, evsigusr1;
2417 int *pack_fds = NULL, *temp_fds = NULL;
2419 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
2421 while ((ch = getopt(argc, argv, "Adf:LnP:RvW")) != -1) {
2422 switch (ch) {
2423 case 'A':
2424 proc_id = PROC_AUTH;
2425 break;
2426 case 'd':
2427 daemonize = 0;
2428 break;
2429 case 'f':
2430 confpath = optarg;
2431 break;
2432 case 'L':
2433 proc_id = PROC_LISTEN;
2434 break;
2435 case 'n':
2436 noaction = 1;
2437 break;
2438 case 'P':
2439 repo_path = realpath(optarg, NULL);
2440 if (repo_path == NULL)
2441 fatal("realpath '%s'", optarg);
2442 break;
2443 case 'R':
2444 proc_id = PROC_REPO_READ;
2445 break;
2446 case 'v':
2447 if (verbosity < 3)
2448 verbosity++;
2449 break;
2450 case 'W':
2451 proc_id = PROC_REPO_WRITE;
2452 break;
2453 default:
2454 usage();
2458 argc -= optind;
2459 argv += optind;
2461 if (argc != 0)
2462 usage();
2464 /* Require an absolute path in argv[0] for reliable re-exec. */
2465 if (!got_path_is_absolute(argv0))
2466 fatalx("bad path \"%s\": must be an absolute path", argv0);
2468 if (geteuid() && (proc_id == PROC_GOTD || proc_id == PROC_LISTEN))
2469 fatalx("need root privileges");
2471 log_init(daemonize ? 0 : 1, LOG_DAEMON);
2472 log_setverbose(verbosity);
2474 if (parse_config(confpath, proc_id, &gotd) != 0)
2475 return 1;
2477 gotd.argv0 = argv0;
2478 gotd.daemonize = daemonize;
2479 gotd.verbosity = verbosity;
2480 gotd.confpath = confpath;
2482 if (proc_id == PROC_GOTD &&
2483 (gotd.nrepos == 0 || TAILQ_EMPTY(&gotd.repos)))
2484 fatalx("no repository defined in configuration file");
2486 pw = getpwnam(gotd.user_name);
2487 if (pw == NULL)
2488 fatalx("user %s not found", gotd.user_name);
2490 if (pw->pw_uid == 0) {
2491 fatalx("cannot run %s as %s: the user running %s "
2492 "must not be the superuser",
2493 getprogname(), pw->pw_name, getprogname());
2496 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups) == -1)
2497 log_warnx("group membership list truncated");
2499 gr = match_group(groups, ngroups, gotd.unix_group_name);
2500 if (gr == NULL) {
2501 fatalx("cannot start %s: the user running %s "
2502 "must be a secondary member of group %s",
2503 getprogname(), getprogname(), gotd.unix_group_name);
2505 if (gr->gr_gid == pw->pw_gid) {
2506 fatalx("cannot start %s: the user running %s "
2507 "must be a secondary member of group %s, but "
2508 "%s is the user's primary group",
2509 getprogname(), getprogname(), gotd.unix_group_name,
2510 gotd.unix_group_name);
2513 if (proc_id == PROC_LISTEN &&
2514 !got_path_is_absolute(gotd.unix_socket_path))
2515 fatalx("bad unix socket path \"%s\": must be an absolute path",
2516 gotd.unix_socket_path);
2518 if (noaction)
2519 return 0;
2521 if (proc_id == PROC_GOTD) {
2522 gotd.pid = getpid();
2523 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2524 start_listener(argv0, confpath, daemonize, verbosity);
2525 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
2526 if (daemonize && daemon(1, 0) == -1)
2527 fatal("daemon");
2528 } else if (proc_id == PROC_LISTEN) {
2529 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2530 if (verbosity) {
2531 log_info("socket: %s", gotd.unix_socket_path);
2532 log_info("user: %s", pw->pw_name);
2533 log_info("secondary group: %s", gr->gr_name);
2536 fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
2537 gr->gr_gid);
2538 if (fd == -1) {
2539 fatal("cannot listen on unix socket %s",
2540 gotd.unix_socket_path);
2542 if (daemonize && daemon(0, 0) == -1)
2543 fatal("daemon");
2544 } else if (proc_id == PROC_AUTH) {
2545 snprintf(title, sizeof(title), "%s %s",
2546 gotd_proc_names[proc_id], repo_path);
2547 if (daemonize && daemon(0, 0) == -1)
2548 fatal("daemon");
2549 } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE) {
2550 error = got_repo_pack_fds_open(&pack_fds);
2551 if (error != NULL)
2552 fatalx("cannot open pack tempfiles: %s", error->msg);
2553 error = got_repo_temp_fds_open(&temp_fds);
2554 if (error != NULL)
2555 fatalx("cannot open pack tempfiles: %s", error->msg);
2556 if (repo_path == NULL)
2557 fatalx("repository path not specified");
2558 snprintf(title, sizeof(title), "%s %s",
2559 gotd_proc_names[proc_id], repo_path);
2560 if (daemonize && daemon(0, 0) == -1)
2561 fatal("daemon");
2562 } else
2563 fatal("invalid process id %d", proc_id);
2565 setproctitle("%s", title);
2566 log_procinit(title);
2568 /* Drop root privileges. */
2569 if (setgid(pw->pw_gid) == -1)
2570 fatal("setgid %d failed", pw->pw_gid);
2571 if (setuid(pw->pw_uid) == -1)
2572 fatal("setuid %d failed", pw->pw_uid);
2574 event_init();
2576 switch (proc_id) {
2577 case PROC_GOTD:
2578 #ifndef PROFILE
2579 if (pledge("stdio rpath wpath cpath proc exec "
2580 "sendfd recvfd fattr flock unveil", NULL) == -1)
2581 err(1, "pledge");
2582 #endif
2583 break;
2584 case PROC_LISTEN:
2585 #ifndef PROFILE
2586 if (pledge("stdio sendfd unix", NULL) == -1)
2587 err(1, "pledge");
2588 #endif
2589 listen_main(title, fd);
2590 /* NOTREACHED */
2591 break;
2592 case PROC_AUTH:
2593 #ifndef PROFILE
2594 if (pledge("stdio getpw recvfd unix unveil", NULL) == -1)
2595 err(1, "pledge");
2596 #endif
2598 * We need the "unix" pledge promise for getpeername(2) only.
2599 * Ensure that AF_UNIX bind(2) cannot be used by revoking all
2600 * filesystem access via unveil(2). Access to password database
2601 * files will still work since "getpw" bypasses unveil(2).
2603 apply_unveil_none();
2605 auth_main(title, &gotd.repos, repo_path);
2606 /* NOTREACHED */
2607 break;
2608 case PROC_REPO_READ:
2609 #ifndef PROFILE
2610 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2611 err(1, "pledge");
2612 #endif
2613 apply_unveil_repo_readonly(repo_path);
2614 repo_read_main(title, repo_path, pack_fds, temp_fds);
2615 /* NOTREACHED */
2616 exit(0);
2617 case PROC_REPO_WRITE:
2618 #ifndef PROFILE
2619 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2620 err(1, "pledge");
2621 #endif
2622 apply_unveil_repo_readonly(repo_path);
2623 repo_write_main(title, repo_path, pack_fds, temp_fds);
2624 /* NOTREACHED */
2625 exit(0);
2626 default:
2627 fatal("invalid process id %d", proc_id);
2630 if (proc_id != PROC_GOTD)
2631 fatal("invalid process id %d", proc_id);
2633 apply_unveil();
2635 signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
2636 signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
2637 signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
2638 signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
2639 signal(SIGPIPE, SIG_IGN);
2641 signal_add(&evsigint, NULL);
2642 signal_add(&evsigterm, NULL);
2643 signal_add(&evsighup, NULL);
2644 signal_add(&evsigusr1, NULL);
2646 gotd_imsg_event_add(&gotd.listen_proc.iev);
2648 event_dispatch();
2650 if (pack_fds)
2651 got_repo_pack_fds_close(pack_fds);
2652 free(repo_path);
2653 return 0;