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;
1228 uid_t euid;
1229 gid_t egid;
1231 *client_id = 0;
1233 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1234 if (datalen != sizeof(iconnect))
1235 return got_error(GOT_ERR_PRIVSEP_LEN);
1236 memcpy(&iconnect, imsg->data, sizeof(iconnect));
1238 s = imsg->fd;
1239 if (s == -1) {
1240 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1241 goto done;
1244 if (find_client(iconnect.client_id)) {
1245 err = got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
1246 goto done;
1249 if (getpeereid(s, &euid, &egid) == -1) {
1250 err = got_error_from_errno("getpeerid");
1251 goto done;
1254 client = calloc(1, sizeof(*client));
1255 if (client == NULL) {
1256 err = got_error_from_errno("calloc");
1257 goto done;
1260 *client_id = iconnect.client_id;
1262 client->state = GOTD_STATE_EXPECT_LIST_REFS;
1263 client->id = iconnect.client_id;
1264 client->fd = s;
1265 s = -1;
1266 client->delta_cache_fd = -1;
1267 client->euid = euid;
1268 client->egid = egid;
1269 client->nref_updates = -1;
1271 imsg_init(&client->iev.ibuf, client->fd);
1272 client->iev.handler = gotd_request;
1273 client->iev.events = EV_READ;
1274 client->iev.handler_arg = client;
1276 event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
1277 &client->iev);
1278 gotd_imsg_event_add(&client->iev);
1280 evtimer_set(&client->tmo, gotd_request_timeout, client);
1282 add_client(client);
1283 log_debug("%s: new client uid %d connected on fd %d", __func__,
1284 client->euid, client->fd);
1285 done:
1286 if (err) {
1287 struct gotd_child_proc *listen_proc = &gotd.listen_proc;
1288 struct gotd_imsg_disconnect idisconnect;
1290 idisconnect.client_id = client->id;
1291 if (gotd_imsg_compose_event(&listen_proc->iev,
1292 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
1293 &idisconnect, sizeof(idisconnect)) == -1)
1294 log_warn("imsg compose DISCONNECT");
1296 if (s != -1)
1297 close(s);
1300 return err;
1303 static const char *gotd_proc_names[PROC_MAX] = {
1304 "parent",
1305 "listen",
1306 "auth",
1307 "repo_read",
1308 "repo_write"
1311 static void
1312 kill_proc(struct gotd_child_proc *proc, int fatal)
1314 if (fatal) {
1315 log_warnx("sending SIGKILL to PID %d", proc->pid);
1316 kill(proc->pid, SIGKILL);
1317 } else
1318 kill(proc->pid, SIGTERM);
1321 static void
1322 gotd_shutdown(void)
1324 struct gotd_child_proc *proc;
1325 uint64_t slot;
1327 for (slot = 0; slot < nitems(gotd_clients); slot++) {
1328 struct gotd_client *c, *tmp;
1330 STAILQ_FOREACH_SAFE(c, &gotd_clients[slot], entry, tmp)
1331 disconnect(c);
1334 proc = &gotd.listen_proc;
1335 msgbuf_clear(&proc->iev.ibuf.w);
1336 close(proc->iev.ibuf.fd);
1337 kill_proc(proc, 0);
1338 wait_for_child(proc->pid);
1340 log_info("terminating");
1341 exit(0);
1344 void
1345 gotd_sighdlr(int sig, short event, void *arg)
1348 * Normal signal handler rules don't apply because libevent
1349 * decouples for us.
1352 switch (sig) {
1353 case SIGHUP:
1354 log_info("%s: ignoring SIGHUP", __func__);
1355 break;
1356 case SIGUSR1:
1357 log_info("%s: ignoring SIGUSR1", __func__);
1358 break;
1359 case SIGTERM:
1360 case SIGINT:
1361 gotd_shutdown();
1362 log_warnx("gotd terminating");
1363 exit(0);
1364 break;
1365 default:
1366 fatalx("unexpected signal");
1370 static const struct got_error *
1371 ensure_proc_is_reading(struct gotd_client *client,
1372 struct gotd_child_proc *proc)
1374 if (!client_is_reading(client)) {
1375 kill_proc(proc, 1);
1376 return got_error_fmt(GOT_ERR_BAD_PACKET,
1377 "PID %d handled a read-request for uid %d but this "
1378 "user is not reading from a repository", proc->pid,
1379 client->euid);
1382 return NULL;
1385 static const struct got_error *
1386 ensure_proc_is_writing(struct gotd_client *client,
1387 struct gotd_child_proc *proc)
1389 if (!client_is_writing(client)) {
1390 kill_proc(proc, 1);
1391 return got_error_fmt(GOT_ERR_BAD_PACKET,
1392 "PID %d handled a write-request for uid %d but this "
1393 "user is not writing to a repository", proc->pid,
1394 client->euid);
1397 return NULL;
1400 static int
1401 verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
1402 struct imsg *imsg)
1404 const struct got_error *err;
1405 struct gotd_child_proc *client_proc;
1406 int ret = 0;
1408 if (proc->type == PROC_REPO_READ || proc->type == PROC_REPO_WRITE) {
1409 client_proc = get_client_proc(client);
1410 if (client_proc == NULL)
1411 fatalx("no process found for uid %d", client->euid);
1412 if (proc->pid != client_proc->pid) {
1413 kill_proc(proc, 1);
1414 log_warnx("received message from PID %d for uid %d, "
1415 "while PID %d is the process serving this user",
1416 proc->pid, client->euid, client_proc->pid);
1417 return 0;
1421 switch (imsg->hdr.type) {
1422 case GOTD_IMSG_ERROR:
1423 ret = 1;
1424 break;
1425 case GOTD_IMSG_CONNECT:
1426 if (proc->type != PROC_LISTEN) {
1427 err = got_error_fmt(GOT_ERR_BAD_PACKET,
1428 "new connection for uid %d from PID %d "
1429 "which is not the listen process",
1430 proc->pid, client->euid);
1431 } else
1432 ret = 1;
1433 break;
1434 case GOTD_IMSG_ACCESS_GRANTED:
1435 if (proc->type != PROC_AUTH) {
1436 err = got_error_fmt(GOT_ERR_BAD_PACKET,
1437 "authentication of uid %d from PID %d "
1438 "which is not the auth process",
1439 proc->pid, client->euid);
1440 } else
1441 ret = 1;
1442 break;
1443 case GOTD_IMSG_REPO_CHILD_READY:
1444 if (proc->type != PROC_REPO_READ &&
1445 proc->type != PROC_REPO_WRITE) {
1446 err = got_error_fmt(GOT_ERR_BAD_PACKET,
1447 "unexpected \"ready\" signal from PID %d",
1448 proc->pid);
1449 } else
1450 ret = 1;
1451 break;
1452 case GOTD_IMSG_PACKFILE_DONE:
1453 err = ensure_proc_is_reading(client, proc);
1454 if (err)
1455 log_warnx("uid %d: %s", client->euid, err->msg);
1456 else
1457 ret = 1;
1458 break;
1459 case GOTD_IMSG_PACKFILE_INSTALL:
1460 case GOTD_IMSG_REF_UPDATES_START:
1461 case GOTD_IMSG_REF_UPDATE:
1462 err = ensure_proc_is_writing(client, proc);
1463 if (err)
1464 log_warnx("uid %d: %s", client->euid, err->msg);
1465 else
1466 ret = 1;
1467 break;
1468 default:
1469 log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
1470 break;
1473 return ret;
1476 static const struct got_error *
1477 list_refs_request(struct gotd_client *client, struct gotd_imsgev *iev)
1479 static const struct got_error *err;
1480 struct gotd_imsg_list_refs_internal ilref;
1481 int fd;
1483 memset(&ilref, 0, sizeof(ilref));
1484 ilref.client_id = client->id;
1486 fd = dup(client->fd);
1487 if (fd == -1)
1488 return got_error_from_errno("dup");
1490 if (gotd_imsg_compose_event(iev, GOTD_IMSG_LIST_REFS_INTERNAL,
1491 PROC_GOTD, fd, &ilref, sizeof(ilref)) == -1) {
1492 err = got_error_from_errno("imsg compose WANT");
1493 close(fd);
1494 return err;
1497 client->state = GOTD_STATE_EXPECT_CAPABILITIES;
1498 log_debug("uid %d: expecting capabilities", client->euid);
1499 return NULL;
1502 static const struct got_error *
1503 recv_packfile_done(uint32_t *client_id, struct imsg *imsg)
1505 struct gotd_imsg_packfile_done idone;
1506 size_t datalen;
1508 log_debug("packfile-done received");
1510 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1511 if (datalen != sizeof(idone))
1512 return got_error(GOT_ERR_PRIVSEP_LEN);
1513 memcpy(&idone, imsg->data, sizeof(idone));
1515 *client_id = idone.client_id;
1516 return NULL;
1519 static const struct got_error *
1520 recv_packfile_install(uint32_t *client_id, struct imsg *imsg)
1522 struct gotd_imsg_packfile_install inst;
1523 size_t datalen;
1525 log_debug("packfile-install received");
1527 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1528 if (datalen != sizeof(inst))
1529 return got_error(GOT_ERR_PRIVSEP_LEN);
1530 memcpy(&inst, imsg->data, sizeof(inst));
1532 *client_id = inst.client_id;
1533 return NULL;
1536 static const struct got_error *
1537 recv_ref_updates_start(uint32_t *client_id, struct imsg *imsg)
1539 struct gotd_imsg_ref_updates_start istart;
1540 size_t datalen;
1542 log_debug("ref-updates-start received");
1544 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1545 if (datalen != sizeof(istart))
1546 return got_error(GOT_ERR_PRIVSEP_LEN);
1547 memcpy(&istart, imsg->data, sizeof(istart));
1549 *client_id = istart.client_id;
1550 return NULL;
1553 static const struct got_error *
1554 recv_ref_update(uint32_t *client_id, struct imsg *imsg)
1556 struct gotd_imsg_ref_update iref;
1557 size_t datalen;
1559 log_debug("ref-update received");
1561 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1562 if (datalen < sizeof(iref))
1563 return got_error(GOT_ERR_PRIVSEP_LEN);
1564 memcpy(&iref, imsg->data, sizeof(iref));
1566 *client_id = iref.client_id;
1567 return NULL;
1570 static const struct got_error *
1571 send_ref_update_ok(struct gotd_client *client,
1572 struct gotd_imsg_ref_update *iref, const char *refname)
1574 struct gotd_imsg_ref_update_ok iok;
1575 struct ibuf *wbuf;
1576 size_t len;
1578 memset(&iok, 0, sizeof(iok));
1579 iok.client_id = client->id;
1580 memcpy(iok.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1581 memcpy(iok.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1582 iok.name_len = strlen(refname);
1584 len = sizeof(iok) + iok.name_len;
1585 wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_OK,
1586 PROC_GOTD, gotd.pid, len);
1587 if (wbuf == NULL)
1588 return got_error_from_errno("imsg_create REF_UPDATE_OK");
1590 if (imsg_add(wbuf, &iok, sizeof(iok)) == -1)
1591 return got_error_from_errno("imsg_add REF_UPDATE_OK");
1592 if (imsg_add(wbuf, refname, iok.name_len) == -1)
1593 return got_error_from_errno("imsg_add REF_UPDATE_OK");
1595 wbuf->fd = -1;
1596 imsg_close(&client->iev.ibuf, wbuf);
1597 gotd_imsg_event_add(&client->iev);
1598 return NULL;
1601 static void
1602 send_refs_updated(struct gotd_client *client)
1604 if (gotd_imsg_compose_event(&client->iev,
1605 GOTD_IMSG_REFS_UPDATED, PROC_GOTD, -1, NULL, 0) == -1)
1606 log_warn("imsg compose REFS_UPDATED");
1609 static const struct got_error *
1610 send_ref_update_ng(struct gotd_client *client,
1611 struct gotd_imsg_ref_update *iref, const char *refname,
1612 const char *reason)
1614 const struct got_error *ng_err;
1615 struct gotd_imsg_ref_update_ng ing;
1616 struct ibuf *wbuf;
1617 size_t len;
1619 memset(&ing, 0, sizeof(ing));
1620 ing.client_id = client->id;
1621 memcpy(ing.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1622 memcpy(ing.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1623 ing.name_len = strlen(refname);
1625 ng_err = got_error_fmt(GOT_ERR_REF_BUSY, "%s", reason);
1626 ing.reason_len = strlen(ng_err->msg);
1628 len = sizeof(ing) + ing.name_len + ing.reason_len;
1629 wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_NG,
1630 PROC_GOTD, gotd.pid, len);
1631 if (wbuf == NULL)
1632 return got_error_from_errno("imsg_create REF_UPDATE_NG");
1634 if (imsg_add(wbuf, &ing, sizeof(ing)) == -1)
1635 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1636 if (imsg_add(wbuf, refname, ing.name_len) == -1)
1637 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1638 if (imsg_add(wbuf, ng_err->msg, ing.reason_len) == -1)
1639 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1641 wbuf->fd = -1;
1642 imsg_close(&client->iev.ibuf, wbuf);
1643 gotd_imsg_event_add(&client->iev);
1644 return NULL;
1647 static const struct got_error *
1648 install_pack(struct gotd_client *client, const char *repo_path,
1649 struct imsg *imsg)
1651 const struct got_error *err = NULL;
1652 struct gotd_imsg_packfile_install inst;
1653 char hex[SHA1_DIGEST_STRING_LENGTH];
1654 size_t datalen;
1655 char *packfile_path = NULL, *packidx_path = NULL;
1657 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1658 if (datalen != sizeof(inst))
1659 return got_error(GOT_ERR_PRIVSEP_LEN);
1660 memcpy(&inst, imsg->data, sizeof(inst));
1662 if (client->packfile_path == NULL)
1663 return got_error_msg(GOT_ERR_BAD_REQUEST,
1664 "client has no pack file");
1665 if (client->packidx_path == NULL)
1666 return got_error_msg(GOT_ERR_BAD_REQUEST,
1667 "client has no pack file index");
1669 if (got_sha1_digest_to_str(inst.pack_sha1, hex, sizeof(hex)) == NULL)
1670 return got_error_msg(GOT_ERR_NO_SPACE,
1671 "could not convert pack file SHA1 to hex");
1673 if (asprintf(&packfile_path, "/%s/%s/pack-%s.pack",
1674 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1675 err = got_error_from_errno("asprintf");
1676 goto done;
1679 if (asprintf(&packidx_path, "/%s/%s/pack-%s.idx",
1680 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1681 err = got_error_from_errno("asprintf");
1682 goto done;
1685 if (rename(client->packfile_path, packfile_path) == -1) {
1686 err = got_error_from_errno3("rename", client->packfile_path,
1687 packfile_path);
1688 goto done;
1691 free(client->packfile_path);
1692 client->packfile_path = NULL;
1694 if (rename(client->packidx_path, packidx_path) == -1) {
1695 err = got_error_from_errno3("rename", client->packidx_path,
1696 packidx_path);
1697 goto done;
1700 free(client->packidx_path);
1701 client->packidx_path = NULL;
1702 done:
1703 free(packfile_path);
1704 free(packidx_path);
1705 return err;
1708 static const struct got_error *
1709 begin_ref_updates(struct gotd_client *client, struct imsg *imsg)
1711 struct gotd_imsg_ref_updates_start istart;
1712 size_t datalen;
1714 if (client->nref_updates != -1)
1715 return got_error(GOT_ERR_PRIVSEP_MSG);
1717 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1718 if (datalen != sizeof(istart))
1719 return got_error(GOT_ERR_PRIVSEP_LEN);
1720 memcpy(&istart, imsg->data, sizeof(istart));
1722 if (istart.nref_updates <= 0)
1723 return got_error(GOT_ERR_PRIVSEP_MSG);
1725 client->nref_updates = istart.nref_updates;
1726 return NULL;
1729 static const struct got_error *
1730 update_ref(struct gotd_client *client, const char *repo_path,
1731 struct imsg *imsg)
1733 const struct got_error *err = NULL;
1734 struct got_repository *repo = NULL;
1735 struct got_reference *ref = NULL;
1736 struct gotd_imsg_ref_update iref;
1737 struct got_object_id old_id, new_id;
1738 struct got_object_id *id = NULL;
1739 struct got_object *obj = NULL;
1740 char *refname = NULL;
1741 size_t datalen;
1742 int locked = 0;
1744 log_debug("update-ref from uid %d", client->euid);
1746 if (client->nref_updates <= 0)
1747 return got_error(GOT_ERR_PRIVSEP_MSG);
1749 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1750 if (datalen < sizeof(iref))
1751 return got_error(GOT_ERR_PRIVSEP_LEN);
1752 memcpy(&iref, imsg->data, sizeof(iref));
1753 if (datalen != sizeof(iref) + iref.name_len)
1754 return got_error(GOT_ERR_PRIVSEP_LEN);
1755 refname = malloc(iref.name_len + 1);
1756 if (refname == NULL)
1757 return got_error_from_errno("malloc");
1758 memcpy(refname, imsg->data + sizeof(iref), iref.name_len);
1759 refname[iref.name_len] = '\0';
1761 log_debug("updating ref %s for uid %d", refname, client->euid);
1763 err = got_repo_open(&repo, repo_path, NULL, NULL);
1764 if (err)
1765 goto done;
1767 memcpy(old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
1768 memcpy(new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
1769 err = got_object_open(&obj, repo, &new_id);
1770 if (err)
1771 goto done;
1773 if (iref.ref_is_new) {
1774 err = got_ref_open(&ref, repo, refname, 0);
1775 if (err) {
1776 if (err->code != GOT_ERR_NOT_REF)
1777 goto done;
1778 err = got_ref_alloc(&ref, refname, &new_id);
1779 if (err)
1780 goto done;
1781 err = got_ref_write(ref, repo); /* will lock/unlock */
1782 if (err)
1783 goto done;
1784 } else {
1785 err = got_error_fmt(GOT_ERR_REF_BUSY,
1786 "%s has been created by someone else "
1787 "while transaction was in progress",
1788 got_ref_get_name(ref));
1789 goto done;
1791 } else {
1792 err = got_ref_open(&ref, repo, refname, 1 /* lock */);
1793 if (err)
1794 goto done;
1795 locked = 1;
1797 err = got_ref_resolve(&id, repo, ref);
1798 if (err)
1799 goto done;
1801 if (got_object_id_cmp(id, &old_id) != 0) {
1802 err = got_error_fmt(GOT_ERR_REF_BUSY,
1803 "%s has been modified by someone else "
1804 "while transaction was in progress",
1805 got_ref_get_name(ref));
1806 goto done;
1809 err = got_ref_change_ref(ref, &new_id);
1810 if (err)
1811 goto done;
1813 err = got_ref_write(ref, repo);
1814 if (err)
1815 goto done;
1817 free(id);
1818 id = NULL;
1820 done:
1821 if (err) {
1822 if (err->code == GOT_ERR_LOCKFILE_TIMEOUT) {
1823 err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
1824 "could not acquire exclusive file lock for %s",
1825 refname);
1827 send_ref_update_ng(client, &iref, refname, err->msg);
1828 } else
1829 send_ref_update_ok(client, &iref, refname);
1831 if (client->nref_updates > 0) {
1832 client->nref_updates--;
1833 if (client->nref_updates == 0)
1834 send_refs_updated(client);
1837 if (locked) {
1838 const struct got_error *unlock_err;
1839 unlock_err = got_ref_unlock(ref);
1840 if (unlock_err && err == NULL)
1841 err = unlock_err;
1843 if (ref)
1844 got_ref_close(ref);
1845 if (obj)
1846 got_object_close(obj);
1847 if (repo)
1848 got_repo_close(repo);
1849 free(refname);
1850 free(id);
1851 return err;
1854 static void
1855 gotd_dispatch_listener(int fd, short event, void *arg)
1857 struct gotd_imsgev *iev = arg;
1858 struct imsgbuf *ibuf = &iev->ibuf;
1859 struct gotd_child_proc *proc = &gotd.listen_proc;
1860 ssize_t n;
1861 int shut = 0;
1862 struct imsg imsg;
1864 if (proc->iev.ibuf.fd != fd)
1865 fatalx("%s: unexpected fd %d", __func__, fd);
1867 if (event & EV_READ) {
1868 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1869 fatal("imsg_read error");
1870 if (n == 0) {
1871 /* Connection closed. */
1872 shut = 1;
1873 goto done;
1877 if (event & EV_WRITE) {
1878 n = msgbuf_write(&ibuf->w);
1879 if (n == -1 && errno != EAGAIN)
1880 fatal("msgbuf_write");
1881 if (n == 0) {
1882 /* Connection closed. */
1883 shut = 1;
1884 goto done;
1888 for (;;) {
1889 const struct got_error *err = NULL;
1890 struct gotd_client *client = NULL;
1891 uint32_t client_id = 0;
1892 int do_disconnect = 0;
1894 if ((n = imsg_get(ibuf, &imsg)) == -1)
1895 fatal("%s: imsg_get error", __func__);
1896 if (n == 0) /* No more messages. */
1897 break;
1899 switch (imsg.hdr.type) {
1900 case GOTD_IMSG_ERROR:
1901 do_disconnect = 1;
1902 err = gotd_imsg_recv_error(&client_id, &imsg);
1903 break;
1904 case GOTD_IMSG_CONNECT:
1905 err = recv_connect(&client_id, &imsg);
1906 break;
1907 default:
1908 log_debug("unexpected imsg %d", imsg.hdr.type);
1909 break;
1912 client = find_client(client_id);
1913 if (client == NULL) {
1914 log_warnx("%s: client not found", __func__);
1915 imsg_free(&imsg);
1916 continue;
1919 if (err)
1920 log_warnx("uid %d: %s", client->euid, err->msg);
1922 if (do_disconnect) {
1923 if (err)
1924 disconnect_on_error(client, err);
1925 else
1926 disconnect(client);
1929 imsg_free(&imsg);
1931 done:
1932 if (!shut) {
1933 gotd_imsg_event_add(iev);
1934 } else {
1935 /* This pipe is dead. Remove its event handler */
1936 event_del(&iev->ev);
1937 event_loopexit(NULL);
1941 static void
1942 gotd_dispatch_auth_child(int fd, short event, void *arg)
1944 const struct got_error *err = NULL;
1945 struct gotd_imsgev *iev = arg;
1946 struct imsgbuf *ibuf = &iev->ibuf;
1947 struct gotd_client *client;
1948 struct gotd_repo *repo = NULL;
1949 ssize_t n;
1950 int shut = 0;
1951 struct imsg imsg;
1952 uint32_t client_id = 0;
1953 int do_disconnect = 0;
1954 enum gotd_procid proc_type;
1956 client = find_client_by_proc_fd(fd);
1957 if (client == NULL)
1958 fatalx("cannot find client for fd %d", fd);
1960 if (client->auth == NULL)
1961 fatalx("cannot find auth child process for fd %d", fd);
1963 if (event & EV_READ) {
1964 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1965 fatal("imsg_read error");
1966 if (n == 0) {
1967 /* Connection closed. */
1968 shut = 1;
1969 goto done;
1973 if (event & EV_WRITE) {
1974 n = msgbuf_write(&ibuf->w);
1975 if (n == -1 && errno != EAGAIN)
1976 fatal("msgbuf_write");
1977 if (n == 0) {
1978 /* Connection closed. */
1979 shut = 1;
1981 goto done;
1984 if (client->auth->iev.ibuf.fd != fd)
1985 fatalx("%s: unexpected fd %d", __func__, fd);
1987 if ((n = imsg_get(ibuf, &imsg)) == -1)
1988 fatal("%s: imsg_get error", __func__);
1989 if (n == 0) /* No more messages. */
1990 return;
1992 evtimer_del(&client->tmo);
1994 switch (imsg.hdr.type) {
1995 case GOTD_IMSG_ERROR:
1996 do_disconnect = 1;
1997 err = gotd_imsg_recv_error(&client_id, &imsg);
1998 break;
1999 case GOTD_IMSG_ACCESS_GRANTED:
2000 break;
2001 default:
2002 do_disconnect = 1;
2003 log_debug("unexpected imsg %d", imsg.hdr.type);
2004 break;
2007 if (!verify_imsg_src(client, client->auth, &imsg)) {
2008 do_disconnect = 1;
2009 log_debug("dropping imsg type %d from PID %d",
2010 imsg.hdr.type, client->auth->pid);
2012 imsg_free(&imsg);
2014 if (do_disconnect) {
2015 if (err)
2016 disconnect_on_error(client, err);
2017 else
2018 disconnect(client);
2019 goto done;
2022 repo = find_repo_by_name(client->auth->repo_name);
2023 if (repo == NULL) {
2024 err = got_error(GOT_ERR_NOT_GIT_REPO);
2025 goto done;
2027 kill_auth_proc(client);
2029 log_info("authenticated uid %d for repository %s\n",
2030 client->euid, repo->name);
2032 if (client->required_auth & GOTD_AUTH_WRITE)
2033 proc_type = PROC_REPO_WRITE;
2034 else
2035 proc_type = PROC_REPO_READ;
2037 err = start_repo_child(client, proc_type, repo,
2038 gotd.argv0, gotd.confpath, gotd.daemonize,
2039 gotd.verbosity);
2040 done:
2041 if (err)
2042 log_warnx("uid %d: %s", client->euid, err->msg);
2044 /* We might have killed the auth process by now. */
2045 if (client->auth != NULL) {
2046 if (!shut) {
2047 gotd_imsg_event_add(iev);
2048 } else {
2049 /* This pipe is dead. Remove its event handler */
2050 event_del(&iev->ev);
2055 static void
2056 gotd_dispatch_repo_child(int fd, short event, void *arg)
2058 struct gotd_imsgev *iev = arg;
2059 struct imsgbuf *ibuf = &iev->ibuf;
2060 struct gotd_child_proc *proc = NULL;
2061 struct gotd_client *client = NULL;
2062 ssize_t n;
2063 int shut = 0;
2064 struct imsg imsg;
2066 if (event & EV_READ) {
2067 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
2068 fatal("imsg_read error");
2069 if (n == 0) {
2070 /* Connection closed. */
2071 shut = 1;
2072 goto done;
2076 if (event & EV_WRITE) {
2077 n = msgbuf_write(&ibuf->w);
2078 if (n == -1 && errno != EAGAIN)
2079 fatal("msgbuf_write");
2080 if (n == 0) {
2081 /* Connection closed. */
2082 shut = 1;
2083 goto done;
2087 client = find_client_by_proc_fd(fd);
2088 if (client == NULL)
2089 fatalx("cannot find client for fd %d", fd);
2091 proc = get_client_proc(client);
2092 if (proc == NULL)
2093 fatalx("cannot find child process for fd %d", fd);
2095 for (;;) {
2096 const struct got_error *err = NULL;
2097 uint32_t client_id = 0;
2098 int do_disconnect = 0;
2099 int do_list_refs = 0, do_ref_updates = 0, do_ref_update = 0;
2100 int do_packfile_install = 0;
2102 if ((n = imsg_get(ibuf, &imsg)) == -1)
2103 fatal("%s: imsg_get error", __func__);
2104 if (n == 0) /* No more messages. */
2105 break;
2107 switch (imsg.hdr.type) {
2108 case GOTD_IMSG_ERROR:
2109 do_disconnect = 1;
2110 err = gotd_imsg_recv_error(&client_id, &imsg);
2111 break;
2112 case GOTD_IMSG_REPO_CHILD_READY:
2113 do_list_refs = 1;
2114 break;
2115 case GOTD_IMSG_PACKFILE_DONE:
2116 do_disconnect = 1;
2117 err = recv_packfile_done(&client_id, &imsg);
2118 break;
2119 case GOTD_IMSG_PACKFILE_INSTALL:
2120 err = recv_packfile_install(&client_id, &imsg);
2121 if (err == NULL)
2122 do_packfile_install = 1;
2123 break;
2124 case GOTD_IMSG_REF_UPDATES_START:
2125 err = recv_ref_updates_start(&client_id, &imsg);
2126 if (err == NULL)
2127 do_ref_updates = 1;
2128 break;
2129 case GOTD_IMSG_REF_UPDATE:
2130 err = recv_ref_update(&client_id, &imsg);
2131 if (err == NULL)
2132 do_ref_update = 1;
2133 break;
2134 default:
2135 log_debug("unexpected imsg %d", imsg.hdr.type);
2136 break;
2139 if (!verify_imsg_src(client, proc, &imsg)) {
2140 log_debug("dropping imsg type %d from PID %d",
2141 imsg.hdr.type, proc->pid);
2142 imsg_free(&imsg);
2143 continue;
2145 if (err)
2146 log_warnx("uid %d: %s", client->euid, err->msg);
2148 if (do_disconnect) {
2149 if (err)
2150 disconnect_on_error(client, err);
2151 else
2152 disconnect(client);
2153 } else {
2154 if (do_list_refs)
2155 err = list_refs_request(client, iev);
2156 else if (do_packfile_install)
2157 err = install_pack(client, proc->repo_path,
2158 &imsg);
2159 else if (do_ref_updates)
2160 err = begin_ref_updates(client, &imsg);
2161 else if (do_ref_update)
2162 err = update_ref(client, proc->repo_path,
2163 &imsg);
2164 if (err)
2165 log_warnx("uid %d: %s", client->euid, err->msg);
2167 imsg_free(&imsg);
2169 done:
2170 if (!shut) {
2171 gotd_imsg_event_add(iev);
2172 } else {
2173 /* This pipe is dead. Remove its event handler */
2174 event_del(&iev->ev);
2175 event_loopexit(NULL);
2179 static pid_t
2180 start_child(enum gotd_procid proc_id, const char *repo_path,
2181 char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
2183 char *argv[11];
2184 int argc = 0;
2185 pid_t pid;
2187 switch (pid = fork()) {
2188 case -1:
2189 fatal("cannot fork");
2190 case 0:
2191 break;
2192 default:
2193 close(fd);
2194 return pid;
2197 if (fd != GOTD_FILENO_MSG_PIPE) {
2198 if (dup2(fd, GOTD_FILENO_MSG_PIPE) == -1)
2199 fatal("cannot setup imsg fd");
2200 } else if (fcntl(fd, F_SETFD, 0) == -1)
2201 fatal("cannot setup imsg fd");
2203 argv[argc++] = argv0;
2204 switch (proc_id) {
2205 case PROC_LISTEN:
2206 argv[argc++] = (char *)"-L";
2207 break;
2208 case PROC_AUTH:
2209 argv[argc++] = (char *)"-A";
2210 break;
2211 case PROC_REPO_READ:
2212 argv[argc++] = (char *)"-R";
2213 break;
2214 case PROC_REPO_WRITE:
2215 argv[argc++] = (char *)"-W";
2216 break;
2217 default:
2218 fatalx("invalid process id %d", proc_id);
2221 argv[argc++] = (char *)"-f";
2222 argv[argc++] = (char *)confpath;
2224 if (repo_path) {
2225 argv[argc++] = (char *)"-P";
2226 argv[argc++] = (char *)repo_path;
2229 if (!daemonize)
2230 argv[argc++] = (char *)"-d";
2231 if (verbosity > 0)
2232 argv[argc++] = (char *)"-v";
2233 if (verbosity > 1)
2234 argv[argc++] = (char *)"-v";
2235 argv[argc++] = NULL;
2237 execvp(argv0, argv);
2238 fatal("execvp");
2241 static void
2242 start_listener(char *argv0, const char *confpath, int daemonize, int verbosity)
2244 struct gotd_child_proc *proc = &gotd.listen_proc;
2246 proc->type = PROC_LISTEN;
2248 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
2249 PF_UNSPEC, proc->pipe) == -1)
2250 fatal("socketpair");
2252 proc->pid = start_child(proc->type, NULL, argv0, confpath,
2253 proc->pipe[1], daemonize, verbosity);
2254 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
2255 proc->iev.handler = gotd_dispatch_listener;
2256 proc->iev.events = EV_READ;
2257 proc->iev.handler_arg = NULL;
2260 static const struct got_error *
2261 start_repo_child(struct gotd_client *client, enum gotd_procid proc_type,
2262 struct gotd_repo *repo, char *argv0, const char *confpath,
2263 int daemonize, int verbosity)
2265 struct gotd_child_proc *proc;
2267 if (proc_type != PROC_REPO_READ && proc_type != PROC_REPO_WRITE)
2268 return got_error_msg(GOT_ERR_NOT_IMPL, "bad process type");
2270 proc = calloc(1, sizeof(*proc));
2271 if (proc == NULL)
2272 return got_error_from_errno("calloc");
2274 proc->type = proc_type;
2275 if (strlcpy(proc->repo_name, repo->name,
2276 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
2277 fatalx("repository name too long: %s", repo->name);
2278 log_debug("starting %s for repository %s",
2279 proc->type == PROC_REPO_READ ? "reader" : "writer", repo->name);
2280 if (realpath(repo->path, proc->repo_path) == NULL)
2281 fatal("%s", repo->path);
2282 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
2283 PF_UNSPEC, proc->pipe) == -1)
2284 fatal("socketpair");
2285 proc->pid = start_child(proc->type, proc->repo_path, argv0,
2286 confpath, proc->pipe[1], daemonize, verbosity);
2287 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
2288 log_debug("proc %s %s is on fd %d",
2289 gotd_proc_names[proc->type], proc->repo_path,
2290 proc->pipe[0]);
2291 proc->iev.handler = gotd_dispatch_repo_child;
2292 proc->iev.events = EV_READ;
2293 proc->iev.handler_arg = NULL;
2294 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
2295 gotd_dispatch_repo_child, &proc->iev);
2296 gotd_imsg_event_add(&proc->iev);
2298 if (proc->type == PROC_REPO_READ)
2299 client->repo_read = proc;
2300 else
2301 client->repo_write = proc;
2303 return NULL;
2306 static const struct got_error *
2307 start_auth_child(struct gotd_client *client, int required_auth,
2308 struct gotd_repo *repo, char *argv0, const char *confpath,
2309 int daemonize, int verbosity)
2311 struct gotd_child_proc *proc;
2312 struct gotd_imsg_auth iauth;
2314 memset(&iauth, 0, sizeof(iauth));
2316 proc = calloc(1, sizeof(*proc));
2317 if (proc == NULL)
2318 return got_error_from_errno("calloc");
2320 proc->type = PROC_AUTH;
2321 if (strlcpy(proc->repo_name, repo->name,
2322 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
2323 fatalx("repository name too long: %s", repo->name);
2324 log_debug("starting auth for uid %d repository %s",
2325 client->euid, repo->name);
2326 if (realpath(repo->path, proc->repo_path) == NULL)
2327 fatal("%s", repo->path);
2328 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
2329 PF_UNSPEC, proc->pipe) == -1)
2330 fatal("socketpair");
2331 proc->pid = start_child(proc->type, proc->repo_path, argv0,
2332 confpath, proc->pipe[1], daemonize, verbosity);
2333 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
2334 log_debug("proc %s %s is on fd %d",
2335 gotd_proc_names[proc->type], proc->repo_path,
2336 proc->pipe[0]);
2337 proc->iev.handler = gotd_dispatch_auth_child;
2338 proc->iev.events = EV_READ;
2339 proc->iev.handler_arg = NULL;
2340 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
2341 gotd_dispatch_auth_child, &proc->iev);
2342 gotd_imsg_event_add(&proc->iev);
2344 iauth.euid = client->euid;
2345 iauth.egid = client->egid;
2346 iauth.required_auth = required_auth;
2347 iauth.client_id = client->id;
2348 if (gotd_imsg_compose_event(&proc->iev, GOTD_IMSG_AUTHENTICATE,
2349 PROC_GOTD, -1, &iauth, sizeof(iauth)) == -1)
2350 log_warn("imsg compose AUTHENTICATE");
2352 client->auth = proc;
2353 client->required_auth = required_auth;
2354 return NULL;
2357 static void
2358 apply_unveil_repo_readonly(const char *repo_path)
2360 if (unveil(repo_path, "r") == -1)
2361 fatal("unveil %s", repo_path);
2363 if (unveil(NULL, NULL) == -1)
2364 fatal("unveil");
2367 static void
2368 apply_unveil(void)
2370 struct gotd_repo *repo;
2372 if (unveil(gotd.argv0, "x") == -1)
2373 fatal("unveil %s", gotd.argv0);
2375 TAILQ_FOREACH(repo, &gotd.repos, entry) {
2376 if (unveil(repo->path, "rwc") == -1)
2377 fatal("unveil %s", repo->path);
2380 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
2381 fatal("unveil %s", GOT_TMPDIR_STR);
2383 if (unveil(NULL, NULL) == -1)
2384 fatal("unveil");
2387 int
2388 main(int argc, char **argv)
2390 const struct got_error *error = NULL;
2391 int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
2392 const char *confpath = GOTD_CONF_PATH;
2393 char *argv0 = argv[0];
2394 char title[2048];
2395 gid_t groups[NGROUPS_MAX];
2396 int ngroups = NGROUPS_MAX;
2397 struct passwd *pw = NULL;
2398 struct group *gr = NULL;
2399 char *repo_path = NULL;
2400 enum gotd_procid proc_id = PROC_GOTD;
2401 struct event evsigint, evsigterm, evsighup, evsigusr1;
2402 int *pack_fds = NULL, *temp_fds = NULL;
2404 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
2406 while ((ch = getopt(argc, argv, "Adf:LnP:RvW")) != -1) {
2407 switch (ch) {
2408 case 'A':
2409 proc_id = PROC_AUTH;
2410 break;
2411 case 'd':
2412 daemonize = 0;
2413 break;
2414 case 'f':
2415 confpath = optarg;
2416 break;
2417 case 'L':
2418 proc_id = PROC_LISTEN;
2419 break;
2420 case 'n':
2421 noaction = 1;
2422 break;
2423 case 'P':
2424 repo_path = realpath(optarg, NULL);
2425 if (repo_path == NULL)
2426 fatal("realpath '%s'", optarg);
2427 break;
2428 case 'R':
2429 proc_id = PROC_REPO_READ;
2430 break;
2431 case 'v':
2432 if (verbosity < 3)
2433 verbosity++;
2434 break;
2435 case 'W':
2436 proc_id = PROC_REPO_WRITE;
2437 break;
2438 default:
2439 usage();
2443 argc -= optind;
2444 argv += optind;
2446 if (argc != 0)
2447 usage();
2449 /* Require an absolute path in argv[0] for reliable re-exec. */
2450 if (!got_path_is_absolute(argv0))
2451 fatalx("bad path \"%s\": must be an absolute path", argv0);
2453 if (geteuid() && (proc_id == PROC_GOTD || proc_id == PROC_LISTEN))
2454 fatalx("need root privileges");
2456 log_init(daemonize ? 0 : 1, LOG_DAEMON);
2457 log_setverbose(verbosity);
2459 if (parse_config(confpath, proc_id, &gotd) != 0)
2460 return 1;
2462 gotd.argv0 = argv0;
2463 gotd.daemonize = daemonize;
2464 gotd.verbosity = verbosity;
2465 gotd.confpath = confpath;
2467 if (proc_id == PROC_GOTD &&
2468 (gotd.nrepos == 0 || TAILQ_EMPTY(&gotd.repos)))
2469 fatalx("no repository defined in configuration file");
2471 pw = getpwnam(gotd.user_name);
2472 if (pw == NULL)
2473 fatalx("user %s not found", gotd.user_name);
2475 if (pw->pw_uid == 0) {
2476 fatalx("cannot run %s as %s: the user running %s "
2477 "must not be the superuser",
2478 getprogname(), pw->pw_name, getprogname());
2481 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups) == -1)
2482 log_warnx("group membership list truncated");
2484 gr = match_group(groups, ngroups, gotd.unix_group_name);
2485 if (gr == NULL) {
2486 fatalx("cannot start %s: the user running %s "
2487 "must be a secondary member of group %s",
2488 getprogname(), getprogname(), gotd.unix_group_name);
2490 if (gr->gr_gid == pw->pw_gid) {
2491 fatalx("cannot start %s: the user running %s "
2492 "must be a secondary member of group %s, but "
2493 "%s is the user's primary group",
2494 getprogname(), getprogname(), gotd.unix_group_name,
2495 gotd.unix_group_name);
2498 if (proc_id == PROC_LISTEN &&
2499 !got_path_is_absolute(gotd.unix_socket_path))
2500 fatalx("bad unix socket path \"%s\": must be an absolute path",
2501 gotd.unix_socket_path);
2503 if (noaction)
2504 return 0;
2506 if (proc_id == PROC_GOTD) {
2507 gotd.pid = getpid();
2508 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2509 start_listener(argv0, confpath, daemonize, verbosity);
2510 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
2511 if (daemonize && daemon(1, 0) == -1)
2512 fatal("daemon");
2513 } else if (proc_id == PROC_LISTEN) {
2514 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2515 if (verbosity) {
2516 log_info("socket: %s", gotd.unix_socket_path);
2517 log_info("user: %s", pw->pw_name);
2518 log_info("secondary group: %s", gr->gr_name);
2521 fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
2522 gr->gr_gid);
2523 if (fd == -1) {
2524 fatal("cannot listen on unix socket %s",
2525 gotd.unix_socket_path);
2527 if (daemonize && daemon(0, 0) == -1)
2528 fatal("daemon");
2529 } else if (proc_id == PROC_AUTH) {
2530 snprintf(title, sizeof(title), "%s %s",
2531 gotd_proc_names[proc_id], repo_path);
2532 if (daemonize && daemon(0, 0) == -1)
2533 fatal("daemon");
2534 } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE) {
2535 error = got_repo_pack_fds_open(&pack_fds);
2536 if (error != NULL)
2537 fatalx("cannot open pack tempfiles: %s", error->msg);
2538 error = got_repo_temp_fds_open(&temp_fds);
2539 if (error != NULL)
2540 fatalx("cannot open pack tempfiles: %s", error->msg);
2541 if (repo_path == NULL)
2542 fatalx("repository path not specified");
2543 snprintf(title, sizeof(title), "%s %s",
2544 gotd_proc_names[proc_id], repo_path);
2545 if (daemonize && daemon(0, 0) == -1)
2546 fatal("daemon");
2547 } else
2548 fatal("invalid process id %d", proc_id);
2550 setproctitle("%s", title);
2551 log_procinit(title);
2553 /* Drop root privileges. */
2554 if (setgid(pw->pw_gid) == -1)
2555 fatal("setgid %d failed", pw->pw_gid);
2556 if (setuid(pw->pw_uid) == -1)
2557 fatal("setuid %d failed", pw->pw_uid);
2559 event_init();
2561 switch (proc_id) {
2562 case PROC_GOTD:
2563 #ifndef PROFILE
2564 if (pledge("stdio rpath wpath cpath proc exec "
2565 "sendfd recvfd fattr flock unix unveil", NULL) == -1)
2566 err(1, "pledge");
2567 #endif
2568 break;
2569 case PROC_LISTEN:
2570 #ifndef PROFILE
2571 if (pledge("stdio sendfd unix", NULL) == -1)
2572 err(1, "pledge");
2573 #endif
2574 listen_main(title, fd);
2575 /* NOTREACHED */
2576 break;
2577 case PROC_AUTH:
2578 #ifndef PROFILE
2579 if (pledge("stdio getpw", NULL) == -1)
2580 err(1, "pledge");
2581 #endif
2582 auth_main(title, &gotd.repos, repo_path);
2583 /* NOTREACHED */
2584 break;
2585 case PROC_REPO_READ:
2586 #ifndef PROFILE
2587 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2588 err(1, "pledge");
2589 #endif
2590 apply_unveil_repo_readonly(repo_path);
2591 repo_read_main(title, repo_path, pack_fds, temp_fds);
2592 /* NOTREACHED */
2593 exit(0);
2594 case PROC_REPO_WRITE:
2595 #ifndef PROFILE
2596 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2597 err(1, "pledge");
2598 #endif
2599 apply_unveil_repo_readonly(repo_path);
2600 repo_write_main(title, repo_path, pack_fds, temp_fds);
2601 /* NOTREACHED */
2602 exit(0);
2603 default:
2604 fatal("invalid process id %d", proc_id);
2607 if (proc_id != PROC_GOTD)
2608 fatal("invalid process id %d", proc_id);
2610 apply_unveil();
2612 signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
2613 signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
2614 signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
2615 signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
2616 signal(SIGPIPE, SIG_IGN);
2618 signal_add(&evsigint, NULL);
2619 signal_add(&evsigterm, NULL);
2620 signal_add(&evsighup, NULL);
2621 signal_add(&evsigusr1, NULL);
2623 gotd_imsg_event_add(&gotd.listen_proc.iev);
2625 event_dispatch();
2627 if (pack_fds)
2628 got_repo_pack_fds_close(pack_fds);
2629 free(repo_path);
2630 return 0;