Blob


1 /*
2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/tree.h>
19 #include <sys/time.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <sys/socket.h>
23 #include <sys/un.h>
24 #include <sys/wait.h>
26 #include <fcntl.h>
27 #include <err.h>
28 #include <errno.h>
29 #include <event.h>
30 #include <limits.h>
31 #include <pwd.h>
32 #include <grp.h>
33 #include <imsg.h>
34 #include <sha1.h>
35 #include <signal.h>
36 #include <siphash.h>
37 #include <stdarg.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <syslog.h>
42 #include <unistd.h>
44 #include "got_error.h"
45 #include "got_opentemp.h"
46 #include "got_path.h"
47 #include "got_repository.h"
48 #include "got_object.h"
49 #include "got_reference.h"
51 #include "got_lib_delta.h"
52 #include "got_lib_object.h"
53 #include "got_lib_object_cache.h"
54 #include "got_lib_sha1.h"
55 #include "got_lib_gitproto.h"
56 #include "got_lib_pack.h"
57 #include "got_lib_repository.h"
59 #include "gotd.h"
60 #include "log.h"
61 #include "listen.h"
62 #include "auth.h"
63 #include "repo_read.h"
64 #include "repo_write.h"
66 #ifndef nitems
67 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
68 #endif
70 struct gotd_client {
71 STAILQ_ENTRY(gotd_client) entry;
72 enum gotd_client_state state;
73 struct gotd_client_capability *capabilities;
74 size_t ncapa_alloc;
75 size_t ncapabilities;
76 uint32_t id;
77 int fd;
78 int delta_cache_fd;
79 struct gotd_imsgev iev;
80 struct event tmo;
81 uid_t euid;
82 gid_t egid;
83 struct gotd_child_proc *repo_read;
84 struct gotd_child_proc *repo_write;
85 struct gotd_child_proc *auth;
86 int required_auth;
87 char *packfile_path;
88 char *packidx_path;
89 int nref_updates;
90 };
91 STAILQ_HEAD(gotd_clients, gotd_client);
93 static struct gotd_clients gotd_clients[GOTD_CLIENT_TABLE_SIZE];
94 static SIPHASH_KEY clients_hash_key;
95 volatile int client_cnt;
96 static struct timeval timeout = { 3600, 0 };
97 static struct timeval auth_timeout = { 5, 0 };
98 static struct gotd gotd;
100 void gotd_sighdlr(int sig, short event, void *arg);
101 static void gotd_shutdown(void);
102 static const struct got_error *start_repo_child(struct gotd_client *,
103 enum gotd_procid, struct gotd_repo *, char *, const char *, int, int);
104 static const struct got_error *start_auth_child(struct gotd_client *, int,
105 struct gotd_repo *, char *, const char *, int, int);
106 static void kill_proc(struct gotd_child_proc *, int);
108 __dead static void
109 usage()
111 fprintf(stderr, "usage: %s [-dv] [-f config-file]\n", getprogname());
112 exit(1);
115 static int
116 unix_socket_listen(const char *unix_socket_path, uid_t uid, gid_t gid)
118 struct sockaddr_un sun;
119 int fd = -1;
120 mode_t old_umask, mode;
122 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
123 if (fd == -1) {
124 log_warn("socket");
125 return -1;
128 sun.sun_family = AF_UNIX;
129 if (strlcpy(sun.sun_path, unix_socket_path,
130 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
131 log_warnx("%s: name too long", unix_socket_path);
132 close(fd);
133 return -1;
136 if (unlink(unix_socket_path) == -1) {
137 if (errno != ENOENT) {
138 log_warn("unlink %s", unix_socket_path);
139 close(fd);
140 return -1;
144 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
145 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
147 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
148 log_warn("bind: %s", unix_socket_path);
149 close(fd);
150 umask(old_umask);
151 return -1;
154 umask(old_umask);
156 if (chmod(unix_socket_path, mode) == -1) {
157 log_warn("chmod %o %s", mode, unix_socket_path);
158 close(fd);
159 unlink(unix_socket_path);
160 return -1;
163 if (chown(unix_socket_path, uid, gid) == -1) {
164 log_warn("chown %s uid=%d gid=%d", unix_socket_path, uid, gid);
165 close(fd);
166 unlink(unix_socket_path);
167 return -1;
170 if (listen(fd, GOTD_UNIX_SOCKET_BACKLOG) == -1) {
171 log_warn("listen");
172 close(fd);
173 unlink(unix_socket_path);
174 return -1;
177 return fd;
180 static struct group *
181 match_group(gid_t *groups, int ngroups, const char *unix_group_name)
183 struct group *gr;
184 int i;
186 for (i = 0; i < ngroups; i++) {
187 gr = getgrgid(groups[i]);
188 if (gr == NULL) {
189 log_warn("getgrgid %d", groups[i]);
190 continue;
192 if (strcmp(gr->gr_name, unix_group_name) == 0)
193 return gr;
196 return NULL;
199 static uint64_t
200 client_hash(uint32_t client_id)
202 return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
205 static void
206 add_client(struct gotd_client *client)
208 uint64_t slot = client_hash(client->id) % nitems(gotd_clients);
209 STAILQ_INSERT_HEAD(&gotd_clients[slot], client, entry);
210 client_cnt++;
213 static struct gotd_client *
214 find_client(uint32_t client_id)
216 uint64_t slot;
217 struct gotd_client *c;
219 slot = client_hash(client_id) % nitems(gotd_clients);
220 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
221 if (c->id == client_id)
222 return c;
225 return NULL;
228 static struct gotd_child_proc *
229 get_client_proc(struct gotd_client *client)
231 if (client->repo_read && client->repo_write) {
232 fatalx("uid %d is reading and writing in the same session",
233 client->euid);
234 /* NOTREACHED */
237 if (client->repo_read)
238 return client->repo_read;
239 else if (client->repo_write)
240 return client->repo_write;
242 return NULL;
245 static struct gotd_client *
246 find_client_by_proc_fd(int fd)
248 uint64_t slot;
250 for (slot = 0; slot < nitems(gotd_clients); slot++) {
251 struct gotd_client *c;
253 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
254 struct gotd_child_proc *proc = get_client_proc(c);
255 if (proc && proc->iev.ibuf.fd == fd)
256 return c;
257 if (c->auth && c->auth->iev.ibuf.fd == fd)
258 return c;
262 return NULL;
265 static int
266 client_is_reading(struct gotd_client *client)
268 return client->repo_read != NULL;
271 static int
272 client_is_writing(struct gotd_client *client)
274 return client->repo_write != NULL;
277 static const struct got_error *
278 ensure_client_is_reading(struct gotd_client *client)
280 if (!client_is_reading(client)) {
281 return got_error_fmt(GOT_ERR_BAD_PACKET,
282 "uid %d made a read-request but is not reading from "
283 "a repository", client->euid);
286 return NULL;
289 static const struct got_error *
290 ensure_client_is_writing(struct gotd_client *client)
292 if (!client_is_writing(client)) {
293 return got_error_fmt(GOT_ERR_BAD_PACKET,
294 "uid %d made a write-request but is not writing to "
295 "a repository", client->euid);
298 return NULL;
301 static const struct got_error *
302 ensure_client_is_not_writing(struct gotd_client *client)
304 if (client_is_writing(client)) {
305 return got_error_fmt(GOT_ERR_BAD_PACKET,
306 "uid %d made a read-request but is writing to "
307 "a repository", client->euid);
310 return NULL;
313 static const struct got_error *
314 ensure_client_is_not_reading(struct gotd_client *client)
316 if (client_is_reading(client)) {
317 return got_error_fmt(GOT_ERR_BAD_PACKET,
318 "uid %d made a write-request but is reading from "
319 "a repository", client->euid);
322 return NULL;
325 static void
326 wait_for_child(pid_t child_pid)
328 pid_t pid;
329 int status;
331 log_debug("waiting for child PID %ld to terminate",
332 (long)child_pid);
334 do {
335 pid = waitpid(child_pid, &status, WNOHANG);
336 if (pid == -1) {
337 if (errno != EINTR && errno != ECHILD)
338 fatal("wait");
339 } else if (WIFSIGNALED(status)) {
340 log_warnx("child PID %ld terminated; signal %d",
341 (long)pid, WTERMSIG(status));
343 } while (pid != -1 || (pid == -1 && errno == EINTR));
346 static void
347 kill_auth_proc(struct gotd_client *client)
349 struct gotd_child_proc *proc;
351 if (client->auth == NULL)
352 return;
354 proc = client->auth;
355 client->auth = NULL;
357 event_del(&proc->iev.ev);
358 msgbuf_clear(&proc->iev.ibuf.w);
359 close(proc->iev.ibuf.fd);
360 kill_proc(proc, 0);
361 wait_for_child(proc->pid);
362 free(proc);
365 static void
366 disconnect(struct gotd_client *client)
368 struct gotd_imsg_disconnect idisconnect;
369 struct gotd_child_proc *proc = get_client_proc(client);
370 struct gotd_child_proc *listen_proc = &gotd.listen_proc;
371 uint64_t slot;
373 log_debug("uid %d: disconnecting", client->euid);
375 kill_auth_proc(client);
377 idisconnect.client_id = client->id;
378 if (proc) {
379 if (gotd_imsg_compose_event(&proc->iev,
380 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
381 &idisconnect, sizeof(idisconnect)) == -1)
382 log_warn("imsg compose DISCONNECT");
384 msgbuf_clear(&proc->iev.ibuf.w);
385 close(proc->iev.ibuf.fd);
386 kill_proc(proc, 0);
387 wait_for_child(proc->pid);
388 free(proc);
389 proc = NULL;
392 if (gotd_imsg_compose_event(&listen_proc->iev,
393 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
394 &idisconnect, sizeof(idisconnect)) == -1)
395 log_warn("imsg compose DISCONNECT");
397 slot = client_hash(client->id) % nitems(gotd_clients);
398 STAILQ_REMOVE(&gotd_clients[slot], client, gotd_client, entry);
399 imsg_clear(&client->iev.ibuf);
400 event_del(&client->iev.ev);
401 evtimer_del(&client->tmo);
402 close(client->fd);
403 if (client->delta_cache_fd != -1)
404 close(client->delta_cache_fd);
405 if (client->packfile_path) {
406 if (unlink(client->packfile_path) == -1 && errno != ENOENT)
407 log_warn("unlink %s: ", client->packfile_path);
408 free(client->packfile_path);
410 if (client->packidx_path) {
411 if (unlink(client->packidx_path) == -1 && errno != ENOENT)
412 log_warn("unlink %s: ", client->packidx_path);
413 free(client->packidx_path);
415 free(client->capabilities);
416 free(client);
417 client_cnt--;
420 static void
421 disconnect_on_error(struct gotd_client *client, const struct got_error *err)
423 struct imsgbuf ibuf;
425 log_warnx("uid %d: %s", client->euid, err->msg);
426 if (err->code != GOT_ERR_EOF) {
427 imsg_init(&ibuf, client->fd);
428 gotd_imsg_send_error(&ibuf, 0, PROC_GOTD, err);
429 imsg_clear(&ibuf);
431 disconnect(client);
434 static const struct got_error *
435 send_repo_info(struct gotd_imsgev *iev, struct gotd_repo *repo)
437 const struct got_error *err = NULL;
438 struct gotd_imsg_info_repo irepo;
440 memset(&irepo, 0, sizeof(irepo));
442 if (strlcpy(irepo.repo_name, repo->name, sizeof(irepo.repo_name))
443 >= sizeof(irepo.repo_name))
444 return got_error_msg(GOT_ERR_NO_SPACE, "repo name too long");
445 if (strlcpy(irepo.repo_path, repo->path, sizeof(irepo.repo_path))
446 >= sizeof(irepo.repo_path))
447 return got_error_msg(GOT_ERR_NO_SPACE, "repo path too long");
449 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_REPO, PROC_GOTD, -1,
450 &irepo, sizeof(irepo)) == -1) {
451 err = got_error_from_errno("imsg compose INFO_REPO");
452 if (err)
453 return err;
456 return NULL;
459 static const struct got_error *
460 send_capability(struct gotd_client_capability *capa, struct gotd_imsgev* iev)
462 const struct got_error *err = NULL;
463 struct gotd_imsg_capability icapa;
464 size_t len;
465 struct ibuf *wbuf;
467 memset(&icapa, 0, sizeof(icapa));
469 icapa.key_len = strlen(capa->key);
470 len = sizeof(icapa) + icapa.key_len;
471 if (capa->value) {
472 icapa.value_len = strlen(capa->value);
473 len += icapa.value_len;
476 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_CAPABILITY, 0, 0, len);
477 if (wbuf == NULL) {
478 err = got_error_from_errno("imsg_create CAPABILITY");
479 return err;
482 if (imsg_add(wbuf, &icapa, sizeof(icapa)) == -1)
483 return got_error_from_errno("imsg_add CAPABILITY");
484 if (imsg_add(wbuf, capa->key, icapa.key_len) == -1)
485 return got_error_from_errno("imsg_add CAPABILITY");
486 if (capa->value) {
487 if (imsg_add(wbuf, capa->value, icapa.value_len) == -1)
488 return got_error_from_errno("imsg_add CAPABILITY");
491 wbuf->fd = -1;
492 imsg_close(&iev->ibuf, wbuf);
494 gotd_imsg_event_add(iev);
496 return NULL;
499 static const struct got_error *
500 send_client_info(struct gotd_imsgev *iev, struct gotd_client *client)
502 const struct got_error *err = NULL;
503 struct gotd_imsg_info_client iclient;
504 struct gotd_child_proc *proc;
505 size_t i;
507 memset(&iclient, 0, sizeof(iclient));
508 iclient.euid = client->euid;
509 iclient.egid = client->egid;
511 proc = get_client_proc(client);
512 if (proc) {
513 if (strlcpy(iclient.repo_name, proc->repo_path,
514 sizeof(iclient.repo_name)) >= sizeof(iclient.repo_name)) {
515 return got_error_msg(GOT_ERR_NO_SPACE,
516 "repo name too long");
518 if (client_is_writing(client))
519 iclient.is_writing = 1;
522 iclient.state = client->state;
523 iclient.ncapabilities = client->ncapabilities;
525 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_CLIENT, PROC_GOTD, -1,
526 &iclient, sizeof(iclient)) == -1) {
527 err = got_error_from_errno("imsg compose INFO_CLIENT");
528 if (err)
529 return err;
532 for (i = 0; i < client->ncapabilities; i++) {
533 struct gotd_client_capability *capa;
534 capa = &client->capabilities[i];
535 err = send_capability(capa, iev);
536 if (err)
537 return err;
540 return NULL;
543 static const struct got_error *
544 send_info(struct gotd_client *client)
546 const struct got_error *err = NULL;
547 struct gotd_imsg_info info;
548 uint64_t slot;
549 struct gotd_repo *repo;
551 info.pid = gotd.pid;
552 info.verbosity = gotd.verbosity;
553 info.nrepos = gotd.nrepos;
554 info.nclients = client_cnt - 1;
556 if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_INFO, PROC_GOTD, -1,
557 &info, sizeof(info)) == -1) {
558 err = got_error_from_errno("imsg compose INFO");
559 if (err)
560 return err;
563 TAILQ_FOREACH(repo, &gotd.repos, entry) {
564 err = send_repo_info(&client->iev, repo);
565 if (err)
566 return err;
569 for (slot = 0; slot < nitems(gotd_clients); slot++) {
570 struct gotd_client *c;
571 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
572 if (c->id == client->id)
573 continue;
574 err = send_client_info(&client->iev, c);
575 if (err)
576 return err;
580 return NULL;
583 static const struct got_error *
584 stop_gotd(struct gotd_client *client)
587 if (client->euid != 0)
588 return got_error_set_errno(EPERM, "stop");
590 gotd_shutdown();
591 /* NOTREACHED */
592 return NULL;
595 static struct gotd_repo *
596 find_repo_by_name(const char *repo_name)
598 struct gotd_repo *repo;
599 size_t namelen;
601 TAILQ_FOREACH(repo, &gotd.repos, entry) {
602 namelen = strlen(repo->name);
603 if (strncmp(repo->name, repo_name, namelen) != 0)
604 continue;
605 if (repo_name[namelen] == '\0' ||
606 strcmp(&repo_name[namelen], ".git") == 0)
607 return repo;
610 return NULL;
613 static const struct got_error *
614 start_client_session(struct gotd_client *client, struct imsg *imsg)
616 const struct got_error *err;
617 struct gotd_imsg_list_refs ireq;
618 struct gotd_repo *repo = NULL;
619 size_t datalen;
621 log_debug("list-refs request from uid %d", client->euid);
623 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
624 if (datalen != sizeof(ireq))
625 return got_error(GOT_ERR_PRIVSEP_LEN);
627 memcpy(&ireq, imsg->data, datalen);
629 if (ireq.client_is_reading) {
630 err = ensure_client_is_not_writing(client);
631 if (err)
632 return err;
633 repo = find_repo_by_name(ireq.repo_name);
634 if (repo == NULL)
635 return got_error(GOT_ERR_NOT_GIT_REPO);
636 err = start_auth_child(client, GOTD_AUTH_READ, repo,
637 gotd.argv0, gotd.confpath, gotd.daemonize,
638 gotd.verbosity);
639 if (err)
640 return err;
641 } else {
642 err = ensure_client_is_not_reading(client);
643 if (err)
644 return err;
645 repo = find_repo_by_name(ireq.repo_name);
646 if (repo == NULL)
647 return got_error(GOT_ERR_NOT_GIT_REPO);
648 err = start_auth_child(client,
649 GOTD_AUTH_READ | GOTD_AUTH_WRITE,
650 repo, gotd.argv0, gotd.confpath, gotd.daemonize,
651 gotd.verbosity);
652 if (err)
653 return err;
656 /* Flow continues upon authentication successs/failure or timeout. */
657 return NULL;
660 static const struct got_error *
661 forward_want(struct gotd_client *client, struct imsg *imsg)
663 struct gotd_imsg_want ireq;
664 struct gotd_imsg_want iwant;
665 size_t datalen;
667 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
668 if (datalen != sizeof(ireq))
669 return got_error(GOT_ERR_PRIVSEP_LEN);
671 memcpy(&ireq, imsg->data, datalen);
673 memset(&iwant, 0, sizeof(iwant));
674 memcpy(iwant.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
675 iwant.client_id = client->id;
677 if (gotd_imsg_compose_event(&client->repo_read->iev, GOTD_IMSG_WANT,
678 PROC_GOTD, -1, &iwant, sizeof(iwant)) == -1)
679 return got_error_from_errno("imsg compose WANT");
681 return NULL;
684 static const struct got_error *
685 forward_ref_update(struct gotd_client *client, struct imsg *imsg)
687 const struct got_error *err = NULL;
688 struct gotd_imsg_ref_update ireq;
689 struct gotd_imsg_ref_update *iref = NULL;
690 size_t datalen;
692 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
693 if (datalen < sizeof(ireq))
694 return got_error(GOT_ERR_PRIVSEP_LEN);
695 memcpy(&ireq, imsg->data, sizeof(ireq));
696 if (datalen != sizeof(ireq) + ireq.name_len)
697 return got_error(GOT_ERR_PRIVSEP_LEN);
699 iref = malloc(datalen);
700 if (iref == NULL)
701 return got_error_from_errno("malloc");
702 memcpy(iref, imsg->data, datalen);
704 iref->client_id = client->id;
705 if (gotd_imsg_compose_event(&client->repo_write->iev,
706 GOTD_IMSG_REF_UPDATE, PROC_GOTD, -1, iref, datalen) == -1)
707 err = got_error_from_errno("imsg compose REF_UPDATE");
708 free(iref);
709 return err;
712 static const struct got_error *
713 forward_have(struct gotd_client *client, struct imsg *imsg)
715 struct gotd_imsg_have ireq;
716 struct gotd_imsg_have ihave;
717 size_t datalen;
719 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
720 if (datalen != sizeof(ireq))
721 return got_error(GOT_ERR_PRIVSEP_LEN);
723 memcpy(&ireq, imsg->data, datalen);
725 memset(&ihave, 0, sizeof(ihave));
726 memcpy(ihave.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
727 ihave.client_id = client->id;
729 if (gotd_imsg_compose_event(&client->repo_read->iev, GOTD_IMSG_HAVE,
730 PROC_GOTD, -1, &ihave, sizeof(ihave)) == -1)
731 return got_error_from_errno("imsg compose HAVE");
733 return NULL;
736 static int
737 client_has_capability(struct gotd_client *client, const char *capastr)
739 struct gotd_client_capability *capa;
740 size_t i;
742 if (client->ncapabilities == 0)
743 return 0;
745 for (i = 0; i < client->ncapabilities; i++) {
746 capa = &client->capabilities[i];
747 if (strcmp(capa->key, capastr) == 0)
748 return 1;
751 return 0;
754 static const struct got_error *
755 recv_capabilities(struct gotd_client *client, struct imsg *imsg)
757 struct gotd_imsg_capabilities icapas;
758 size_t datalen;
760 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
761 if (datalen != sizeof(icapas))
762 return got_error(GOT_ERR_PRIVSEP_LEN);
763 memcpy(&icapas, imsg->data, sizeof(icapas));
765 client->ncapa_alloc = icapas.ncapabilities;
766 client->capabilities = calloc(client->ncapa_alloc,
767 sizeof(*client->capabilities));
768 if (client->capabilities == NULL) {
769 client->ncapa_alloc = 0;
770 return got_error_from_errno("calloc");
773 log_debug("expecting %zu capabilities from uid %d",
774 client->ncapa_alloc, client->euid);
775 return NULL;
778 static const struct got_error *
779 recv_capability(struct gotd_client *client, struct imsg *imsg)
781 struct gotd_imsg_capability icapa;
782 struct gotd_client_capability *capa;
783 size_t datalen;
784 char *key, *value = NULL;
786 if (client->capabilities == NULL ||
787 client->ncapabilities >= client->ncapa_alloc) {
788 return got_error_msg(GOT_ERR_BAD_REQUEST,
789 "unexpected capability received");
792 memset(&icapa, 0, sizeof(icapa));
794 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
795 if (datalen < sizeof(icapa))
796 return got_error(GOT_ERR_PRIVSEP_LEN);
797 memcpy(&icapa, imsg->data, sizeof(icapa));
799 if (datalen != sizeof(icapa) + icapa.key_len + icapa.value_len)
800 return got_error(GOT_ERR_PRIVSEP_LEN);
802 key = malloc(icapa.key_len + 1);
803 if (key == NULL)
804 return got_error_from_errno("malloc");
805 if (icapa.value_len > 0) {
806 value = malloc(icapa.value_len + 1);
807 if (value == NULL) {
808 free(key);
809 return got_error_from_errno("malloc");
813 memcpy(key, imsg->data + sizeof(icapa), icapa.key_len);
814 key[icapa.key_len] = '\0';
815 if (value) {
816 memcpy(value, imsg->data + sizeof(icapa) + icapa.key_len,
817 icapa.value_len);
818 value[icapa.value_len] = '\0';
821 capa = &client->capabilities[client->ncapabilities++];
822 capa->key = key;
823 capa->value = value;
825 if (value)
826 log_debug("uid %d: capability %s=%s", client->euid, key, value);
827 else
828 log_debug("uid %d: capability %s", client->euid, key);
830 return NULL;
833 static const struct got_error *
834 send_packfile(struct gotd_client *client)
836 const struct got_error *err = NULL;
837 struct gotd_imsg_send_packfile ipack;
838 struct gotd_imsg_packfile_pipe ipipe;
839 int pipe[2];
841 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
842 return got_error_from_errno("socketpair");
844 memset(&ipack, 0, sizeof(ipack));
845 memset(&ipipe, 0, sizeof(ipipe));
847 ipack.client_id = client->id;
848 if (client_has_capability(client, GOT_CAPA_SIDE_BAND_64K))
849 ipack.report_progress = 1;
851 client->delta_cache_fd = got_opentempfd();
852 if (client->delta_cache_fd == -1)
853 return got_error_from_errno("got_opentempfd");
855 if (gotd_imsg_compose_event(&client->repo_read->iev,
856 GOTD_IMSG_SEND_PACKFILE, PROC_GOTD, client->delta_cache_fd,
857 &ipack, sizeof(ipack)) == -1) {
858 err = got_error_from_errno("imsg compose SEND_PACKFILE");
859 close(pipe[0]);
860 close(pipe[1]);
861 return err;
864 ipipe.client_id = client->id;
866 /* Send pack pipe end 0 to repo_read. */
867 if (gotd_imsg_compose_event(&client->repo_read->iev,
868 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[0],
869 &ipipe, sizeof(ipipe)) == -1) {
870 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
871 close(pipe[1]);
872 return err;
875 /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
876 if (gotd_imsg_compose_event(&client->iev,
877 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1], NULL, 0) == -1)
878 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
880 return err;
883 static const struct got_error *
884 recv_packfile(struct gotd_client *client)
886 const struct got_error *err = NULL;
887 struct gotd_imsg_recv_packfile ipack;
888 struct gotd_imsg_packfile_pipe ipipe;
889 struct gotd_imsg_packidx_file ifile;
890 char *basepath = NULL, *pack_path = NULL, *idx_path = NULL;
891 int packfd = -1, idxfd = -1;
892 int pipe[2] = { -1, -1 };
894 if (client->packfile_path) {
895 return got_error_fmt(GOT_ERR_PRIVSEP_MSG,
896 "uid %d already has a pack file", client->euid);
899 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
900 return got_error_from_errno("socketpair");
902 memset(&ipipe, 0, sizeof(ipipe));
903 ipipe.client_id = client->id;
905 if (gotd_imsg_compose_event(&client->repo_write->iev,
906 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[0],
907 &ipipe, sizeof(ipipe)) == -1) {
908 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
909 pipe[0] = -1;
910 goto done;
912 pipe[0] = -1;
914 if (gotd_imsg_compose_event(&client->repo_write->iev,
915 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1],
916 &ipipe, sizeof(ipipe)) == -1)
917 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
918 pipe[1] = -1;
920 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.pack",
921 client->repo_write->repo_path, GOT_OBJECTS_PACK_DIR,
922 client->euid) == -1) {
923 err = got_error_from_errno("asprintf");
924 goto done;
927 err = got_opentemp_named_fd(&pack_path, &packfd, basepath, "");
928 if (err)
929 goto done;
931 free(basepath);
932 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.idx",
933 client->repo_write->repo_path, GOT_OBJECTS_PACK_DIR,
934 client->euid) == -1) {
935 err = got_error_from_errno("asprintf");
936 basepath = NULL;
937 goto done;
939 err = got_opentemp_named_fd(&idx_path, &idxfd, basepath, "");
940 if (err)
941 goto done;
943 memset(&ifile, 0, sizeof(ifile));
944 ifile.client_id = client->id;
945 if (gotd_imsg_compose_event(&client->repo_write->iev,
946 GOTD_IMSG_PACKIDX_FILE, PROC_GOTD, idxfd,
947 &ifile, sizeof(ifile)) == -1) {
948 err = got_error_from_errno("imsg compose PACKIDX_FILE");
949 idxfd = -1;
950 goto done;
952 idxfd = -1;
954 memset(&ipack, 0, sizeof(ipack));
955 ipack.client_id = client->id;
956 if (client_has_capability(client, GOT_CAPA_REPORT_STATUS))
957 ipack.report_status = 1;
959 if (gotd_imsg_compose_event(&client->repo_write->iev,
960 GOTD_IMSG_RECV_PACKFILE, PROC_GOTD, packfd,
961 &ipack, sizeof(ipack)) == -1) {
962 err = got_error_from_errno("imsg compose RECV_PACKFILE");
963 packfd = -1;
964 goto done;
966 packfd = -1;
968 done:
969 free(basepath);
970 if (pipe[0] != -1 && close(pipe[0]) == -1 && err == NULL)
971 err = got_error_from_errno("close");
972 if (pipe[1] != -1 && close(pipe[1]) == -1 && err == NULL)
973 err = got_error_from_errno("close");
974 if (packfd != -1 && close(packfd) == -1 && err == NULL)
975 err = got_error_from_errno("close");
976 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
977 err = got_error_from_errno("close");
978 if (err) {
979 free(pack_path);
980 free(idx_path);
981 } else {
982 client->packfile_path = pack_path;
983 client->packidx_path = idx_path;
985 return err;
988 static void
989 gotd_request(int fd, short events, void *arg)
991 struct gotd_imsgev *iev = arg;
992 struct imsgbuf *ibuf = &iev->ibuf;
993 struct gotd_client *client = iev->handler_arg;
994 const struct got_error *err = NULL;
995 struct imsg imsg;
996 ssize_t n;
998 if (events & EV_WRITE) {
999 while (ibuf->w.queued) {
1000 n = msgbuf_write(&ibuf->w);
1001 if (n == -1 && errno == EPIPE) {
1003 * The client has closed its socket.
1004 * This can happen when Git clients are
1005 * done sending pack file data.
1007 msgbuf_clear(&ibuf->w);
1008 continue;
1009 } else if (n == -1 && errno != EAGAIN) {
1010 err = got_error_from_errno("imsg_flush");
1011 disconnect_on_error(client, err);
1012 return;
1014 if (n == 0) {
1015 /* Connection closed. */
1016 err = got_error(GOT_ERR_EOF);
1017 disconnect_on_error(client, err);
1018 return;
1022 /* Disconnect gotctl(8) now that messages have been sent. */
1023 if (!client_is_reading(client) && !client_is_writing(client)) {
1024 disconnect(client);
1025 return;
1029 if ((events & EV_READ) == 0)
1030 return;
1032 memset(&imsg, 0, sizeof(imsg));
1034 while (err == NULL) {
1035 err = gotd_imsg_recv(&imsg, ibuf, 0);
1036 if (err) {
1037 if (err->code == GOT_ERR_PRIVSEP_READ)
1038 err = NULL;
1039 break;
1042 evtimer_del(&client->tmo);
1044 switch (imsg.hdr.type) {
1045 case GOTD_IMSG_INFO:
1046 err = send_info(client);
1047 break;
1048 case GOTD_IMSG_STOP:
1049 err = stop_gotd(client);
1050 break;
1051 case GOTD_IMSG_LIST_REFS:
1052 if (client->state != GOTD_STATE_EXPECT_LIST_REFS) {
1053 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1054 "unexpected list-refs request received");
1055 break;
1057 err = start_client_session(client, &imsg);
1058 if (err)
1059 break;
1060 break;
1061 case GOTD_IMSG_CAPABILITIES:
1062 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1063 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1064 "unexpected capabilities received");
1065 break;
1067 log_debug("receiving capabilities from uid %d",
1068 client->euid);
1069 err = recv_capabilities(client, &imsg);
1070 break;
1071 case GOTD_IMSG_CAPABILITY:
1072 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1073 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1074 "unexpected capability received");
1075 break;
1077 err = recv_capability(client, &imsg);
1078 if (err || client->ncapabilities < client->ncapa_alloc)
1079 break;
1080 if (client_is_reading(client)) {
1081 client->state = GOTD_STATE_EXPECT_WANT;
1082 log_debug("uid %d: expecting want-lines",
1083 client->euid);
1084 } else if (client_is_writing(client)) {
1085 client->state = GOTD_STATE_EXPECT_REF_UPDATE;
1086 log_debug("uid %d: expecting ref-update-lines",
1087 client->euid);
1088 } else
1089 fatalx("client %d is both reading and writing",
1090 client->euid);
1091 break;
1092 case GOTD_IMSG_WANT:
1093 if (client->state != GOTD_STATE_EXPECT_WANT) {
1094 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1095 "unexpected want-line received");
1096 break;
1098 log_debug("received want-line from uid %d",
1099 client->euid);
1100 err = ensure_client_is_reading(client);
1101 if (err)
1102 break;
1103 err = forward_want(client, &imsg);
1104 break;
1105 case GOTD_IMSG_REF_UPDATE:
1106 if (client->state != GOTD_STATE_EXPECT_REF_UPDATE) {
1107 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1108 "unexpected ref-update-line received");
1109 break;
1111 log_debug("received ref-update-line from uid %d",
1112 client->euid);
1113 err = ensure_client_is_writing(client);
1114 if (err)
1115 break;
1116 err = forward_ref_update(client, &imsg);
1117 if (err)
1118 break;
1119 client->state = GOTD_STATE_EXPECT_MORE_REF_UPDATES;
1120 break;
1121 case GOTD_IMSG_HAVE:
1122 if (client->state != GOTD_STATE_EXPECT_HAVE) {
1123 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1124 "unexpected have-line received");
1125 break;
1127 log_debug("received have-line from uid %d",
1128 client->euid);
1129 err = ensure_client_is_reading(client);
1130 if (err)
1131 break;
1132 err = forward_have(client, &imsg);
1133 if (err)
1134 break;
1135 break;
1136 case GOTD_IMSG_FLUSH:
1137 if (client->state == GOTD_STATE_EXPECT_WANT ||
1138 client->state == GOTD_STATE_EXPECT_HAVE) {
1139 err = ensure_client_is_reading(client);
1140 if (err)
1141 break;
1142 } else if (client->state ==
1143 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1144 err = ensure_client_is_writing(client);
1145 if (err)
1146 break;
1147 } else {
1148 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1149 "unexpected flush-pkt received");
1150 break;
1152 log_debug("received flush-pkt from uid %d",
1153 client->euid);
1154 if (client->state == GOTD_STATE_EXPECT_WANT) {
1155 client->state = GOTD_STATE_EXPECT_HAVE;
1156 log_debug("uid %d: expecting have-lines",
1157 client->euid);
1158 } else if (client->state == GOTD_STATE_EXPECT_HAVE) {
1159 client->state = GOTD_STATE_EXPECT_DONE;
1160 log_debug("uid %d: expecting 'done'",
1161 client->euid);
1162 } else if (client->state ==
1163 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1164 client->state = GOTD_STATE_EXPECT_PACKFILE;
1165 log_debug("uid %d: expecting packfile",
1166 client->euid);
1167 err = recv_packfile(client);
1168 } else {
1169 /* should not happen, see above */
1170 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1171 "unexpected client state");
1172 break;
1174 break;
1175 case GOTD_IMSG_DONE:
1176 if (client->state != GOTD_STATE_EXPECT_HAVE &&
1177 client->state != GOTD_STATE_EXPECT_DONE) {
1178 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1179 "unexpected flush-pkt received");
1180 break;
1182 log_debug("received 'done' from uid %d", client->euid);
1183 err = ensure_client_is_reading(client);
1184 if (err)
1185 break;
1186 client->state = GOTD_STATE_DONE;
1187 err = send_packfile(client);
1188 break;
1189 default:
1190 err = got_error(GOT_ERR_PRIVSEP_MSG);
1191 break;
1194 imsg_free(&imsg);
1197 if (err) {
1198 if (err->code != GOT_ERR_EOF ||
1199 client->state != GOTD_STATE_EXPECT_PACKFILE)
1200 disconnect_on_error(client, err);
1201 } else {
1202 gotd_imsg_event_add(&client->iev);
1203 if (client->state == GOTD_STATE_EXPECT_LIST_REFS)
1204 evtimer_add(&client->tmo, &auth_timeout);
1205 else
1206 evtimer_add(&client->tmo, &timeout);
1210 static void
1211 gotd_request_timeout(int fd, short events, void *arg)
1213 struct gotd_client *client = arg;
1215 log_debug("disconnecting uid %d due to timeout", client->euid);
1216 disconnect(client);
1219 static const struct got_error *
1220 recv_connect(uint32_t *client_id, struct imsg *imsg)
1222 const struct got_error *err = NULL;
1223 struct gotd_imsg_connect iconnect;
1224 size_t datalen;
1225 int s = -1;
1226 struct gotd_client *client = NULL;
1227 uid_t euid;
1228 gid_t egid;
1230 *client_id = 0;
1232 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1233 if (datalen != sizeof(iconnect))
1234 return got_error(GOT_ERR_PRIVSEP_LEN);
1235 memcpy(&iconnect, imsg->data, sizeof(iconnect));
1237 s = imsg->fd;
1238 if (s == -1) {
1239 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1240 goto done;
1243 if (find_client(iconnect.client_id)) {
1244 err = got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
1245 goto done;
1248 if (getpeereid(s, &euid, &egid) == -1) {
1249 err = got_error_from_errno("getpeerid");
1250 goto done;
1253 client = calloc(1, sizeof(*client));
1254 if (client == NULL) {
1255 err = got_error_from_errno("calloc");
1256 goto done;
1259 *client_id = iconnect.client_id;
1261 client->state = GOTD_STATE_EXPECT_LIST_REFS;
1262 client->id = iconnect.client_id;
1263 client->fd = s;
1264 s = -1;
1265 client->delta_cache_fd = -1;
1266 client->euid = euid;
1267 client->egid = egid;
1268 client->nref_updates = -1;
1270 imsg_init(&client->iev.ibuf, client->fd);
1271 client->iev.handler = gotd_request;
1272 client->iev.events = EV_READ;
1273 client->iev.handler_arg = client;
1275 event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
1276 &client->iev);
1277 gotd_imsg_event_add(&client->iev);
1279 evtimer_set(&client->tmo, gotd_request_timeout, client);
1281 add_client(client);
1282 log_debug("%s: new client uid %d connected on fd %d", __func__,
1283 client->euid, client->fd);
1284 done:
1285 if (err) {
1286 struct gotd_child_proc *listen_proc = &gotd.listen_proc;
1287 struct gotd_imsg_disconnect idisconnect;
1289 idisconnect.client_id = client->id;
1290 if (gotd_imsg_compose_event(&listen_proc->iev,
1291 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
1292 &idisconnect, sizeof(idisconnect)) == -1)
1293 log_warn("imsg compose DISCONNECT");
1295 if (s != -1)
1296 close(s);
1299 return err;
1302 static const char *gotd_proc_names[PROC_MAX] = {
1303 "parent",
1304 "listen",
1305 "auth",
1306 "repo_read",
1307 "repo_write"
1310 static void
1311 kill_proc(struct gotd_child_proc *proc, int fatal)
1313 if (fatal) {
1314 log_warnx("sending SIGKILL to PID %d", proc->pid);
1315 kill(proc->pid, SIGKILL);
1316 } else
1317 kill(proc->pid, SIGTERM);
1320 static void
1321 gotd_shutdown(void)
1323 struct gotd_child_proc *proc;
1324 uint64_t slot;
1326 for (slot = 0; slot < nitems(gotd_clients); slot++) {
1327 struct gotd_client *c, *tmp;
1329 STAILQ_FOREACH_SAFE(c, &gotd_clients[slot], entry, tmp)
1330 disconnect(c);
1333 proc = &gotd.listen_proc;
1334 msgbuf_clear(&proc->iev.ibuf.w);
1335 close(proc->iev.ibuf.fd);
1336 kill_proc(proc, 0);
1337 wait_for_child(proc->pid);
1339 log_info("terminating");
1340 exit(0);
1343 void
1344 gotd_sighdlr(int sig, short event, void *arg)
1347 * Normal signal handler rules don't apply because libevent
1348 * decouples for us.
1351 switch (sig) {
1352 case SIGHUP:
1353 log_info("%s: ignoring SIGHUP", __func__);
1354 break;
1355 case SIGUSR1:
1356 log_info("%s: ignoring SIGUSR1", __func__);
1357 break;
1358 case SIGTERM:
1359 case SIGINT:
1360 gotd_shutdown();
1361 log_warnx("gotd terminating");
1362 exit(0);
1363 break;
1364 default:
1365 fatalx("unexpected signal");
1369 static const struct got_error *
1370 ensure_proc_is_reading(struct gotd_client *client,
1371 struct gotd_child_proc *proc)
1373 if (!client_is_reading(client)) {
1374 kill_proc(proc, 1);
1375 return got_error_fmt(GOT_ERR_BAD_PACKET,
1376 "PID %d handled a read-request for uid %d but this "
1377 "user is not reading from a repository", proc->pid,
1378 client->euid);
1381 return NULL;
1384 static const struct got_error *
1385 ensure_proc_is_writing(struct gotd_client *client,
1386 struct gotd_child_proc *proc)
1388 if (!client_is_writing(client)) {
1389 kill_proc(proc, 1);
1390 return got_error_fmt(GOT_ERR_BAD_PACKET,
1391 "PID %d handled a write-request for uid %d but this "
1392 "user is not writing to a repository", proc->pid,
1393 client->euid);
1396 return NULL;
1399 static int
1400 verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
1401 struct imsg *imsg)
1403 const struct got_error *err;
1404 struct gotd_child_proc *client_proc;
1405 int ret = 0;
1407 if (proc->type == PROC_REPO_READ || proc->type == PROC_REPO_WRITE) {
1408 client_proc = get_client_proc(client);
1409 if (client_proc == NULL)
1410 fatalx("no process found for uid %d", client->euid);
1411 if (proc->pid != client_proc->pid) {
1412 kill_proc(proc, 1);
1413 log_warnx("received message from PID %d for uid %d, "
1414 "while PID %d is the process serving this user",
1415 proc->pid, client->euid, client_proc->pid);
1416 return 0;
1420 switch (imsg->hdr.type) {
1421 case GOTD_IMSG_ERROR:
1422 ret = 1;
1423 break;
1424 case GOTD_IMSG_CONNECT:
1425 if (proc->type != PROC_LISTEN) {
1426 err = got_error_fmt(GOT_ERR_BAD_PACKET,
1427 "new connection for uid %d from PID %d "
1428 "which is not the listen process",
1429 proc->pid, client->euid);
1430 } else
1431 ret = 1;
1432 break;
1433 case GOTD_IMSG_ACCESS_GRANTED:
1434 if (proc->type != PROC_AUTH) {
1435 err = got_error_fmt(GOT_ERR_BAD_PACKET,
1436 "authentication of uid %d from PID %d "
1437 "which is not the auth process",
1438 proc->pid, client->euid);
1439 } else
1440 ret = 1;
1441 break;
1442 case GOTD_IMSG_REPO_CHILD_READY:
1443 if (proc->type != PROC_REPO_READ &&
1444 proc->type != PROC_REPO_WRITE) {
1445 err = got_error_fmt(GOT_ERR_BAD_PACKET,
1446 "unexpected \"ready\" signal from PID %d",
1447 proc->pid);
1448 } else
1449 ret = 1;
1450 break;
1451 case GOTD_IMSG_PACKFILE_DONE:
1452 err = ensure_proc_is_reading(client, proc);
1453 if (err)
1454 log_warnx("uid %d: %s", client->euid, err->msg);
1455 else
1456 ret = 1;
1457 break;
1458 case GOTD_IMSG_PACKFILE_INSTALL:
1459 case GOTD_IMSG_REF_UPDATES_START:
1460 case GOTD_IMSG_REF_UPDATE:
1461 err = ensure_proc_is_writing(client, proc);
1462 if (err)
1463 log_warnx("uid %d: %s", client->euid, err->msg);
1464 else
1465 ret = 1;
1466 break;
1467 default:
1468 log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
1469 break;
1472 return ret;
1475 static const struct got_error *
1476 list_refs_request(struct gotd_client *client, struct gotd_imsgev *iev)
1478 static const struct got_error *err;
1479 struct gotd_imsg_list_refs_internal ilref;
1480 int fd;
1482 memset(&ilref, 0, sizeof(ilref));
1483 ilref.client_id = client->id;
1485 fd = dup(client->fd);
1486 if (fd == -1)
1487 return got_error_from_errno("dup");
1489 if (gotd_imsg_compose_event(iev, GOTD_IMSG_LIST_REFS_INTERNAL,
1490 PROC_GOTD, fd, &ilref, sizeof(ilref)) == -1) {
1491 err = got_error_from_errno("imsg compose WANT");
1492 close(fd);
1493 return err;
1496 client->state = GOTD_STATE_EXPECT_CAPABILITIES;
1497 log_debug("uid %d: expecting capabilities", client->euid);
1498 return NULL;
1501 static const struct got_error *
1502 recv_packfile_done(uint32_t *client_id, struct imsg *imsg)
1504 struct gotd_imsg_packfile_done idone;
1505 size_t datalen;
1507 log_debug("packfile-done received");
1509 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1510 if (datalen != sizeof(idone))
1511 return got_error(GOT_ERR_PRIVSEP_LEN);
1512 memcpy(&idone, imsg->data, sizeof(idone));
1514 *client_id = idone.client_id;
1515 return NULL;
1518 static const struct got_error *
1519 recv_packfile_install(uint32_t *client_id, struct imsg *imsg)
1521 struct gotd_imsg_packfile_install inst;
1522 size_t datalen;
1524 log_debug("packfile-install received");
1526 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1527 if (datalen != sizeof(inst))
1528 return got_error(GOT_ERR_PRIVSEP_LEN);
1529 memcpy(&inst, imsg->data, sizeof(inst));
1531 *client_id = inst.client_id;
1532 return NULL;
1535 static const struct got_error *
1536 recv_ref_updates_start(uint32_t *client_id, struct imsg *imsg)
1538 struct gotd_imsg_ref_updates_start istart;
1539 size_t datalen;
1541 log_debug("ref-updates-start received");
1543 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1544 if (datalen != sizeof(istart))
1545 return got_error(GOT_ERR_PRIVSEP_LEN);
1546 memcpy(&istart, imsg->data, sizeof(istart));
1548 *client_id = istart.client_id;
1549 return NULL;
1552 static const struct got_error *
1553 recv_ref_update(uint32_t *client_id, struct imsg *imsg)
1555 struct gotd_imsg_ref_update iref;
1556 size_t datalen;
1558 log_debug("ref-update received");
1560 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1561 if (datalen < sizeof(iref))
1562 return got_error(GOT_ERR_PRIVSEP_LEN);
1563 memcpy(&iref, imsg->data, sizeof(iref));
1565 *client_id = iref.client_id;
1566 return NULL;
1569 static const struct got_error *
1570 send_ref_update_ok(struct gotd_client *client,
1571 struct gotd_imsg_ref_update *iref, const char *refname)
1573 struct gotd_imsg_ref_update_ok iok;
1574 struct ibuf *wbuf;
1575 size_t len;
1577 memset(&iok, 0, sizeof(iok));
1578 iok.client_id = client->id;
1579 memcpy(iok.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1580 memcpy(iok.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1581 iok.name_len = strlen(refname);
1583 len = sizeof(iok) + iok.name_len;
1584 wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_OK,
1585 PROC_GOTD, gotd.pid, len);
1586 if (wbuf == NULL)
1587 return got_error_from_errno("imsg_create REF_UPDATE_OK");
1589 if (imsg_add(wbuf, &iok, sizeof(iok)) == -1)
1590 return got_error_from_errno("imsg_add REF_UPDATE_OK");
1591 if (imsg_add(wbuf, refname, iok.name_len) == -1)
1592 return got_error_from_errno("imsg_add REF_UPDATE_OK");
1594 wbuf->fd = -1;
1595 imsg_close(&client->iev.ibuf, wbuf);
1596 gotd_imsg_event_add(&client->iev);
1597 return NULL;
1600 static void
1601 send_refs_updated(struct gotd_client *client)
1603 if (gotd_imsg_compose_event(&client->iev,
1604 GOTD_IMSG_REFS_UPDATED, PROC_GOTD, -1, NULL, 0) == -1)
1605 log_warn("imsg compose REFS_UPDATED");
1608 static const struct got_error *
1609 send_ref_update_ng(struct gotd_client *client,
1610 struct gotd_imsg_ref_update *iref, const char *refname,
1611 const char *reason)
1613 const struct got_error *ng_err;
1614 struct gotd_imsg_ref_update_ng ing;
1615 struct ibuf *wbuf;
1616 size_t len;
1618 memset(&ing, 0, sizeof(ing));
1619 ing.client_id = client->id;
1620 memcpy(ing.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1621 memcpy(ing.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1622 ing.name_len = strlen(refname);
1624 ng_err = got_error_fmt(GOT_ERR_REF_BUSY, "%s", reason);
1625 ing.reason_len = strlen(ng_err->msg);
1627 len = sizeof(ing) + ing.name_len + ing.reason_len;
1628 wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_NG,
1629 PROC_GOTD, gotd.pid, len);
1630 if (wbuf == NULL)
1631 return got_error_from_errno("imsg_create REF_UPDATE_NG");
1633 if (imsg_add(wbuf, &ing, sizeof(ing)) == -1)
1634 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1635 if (imsg_add(wbuf, refname, ing.name_len) == -1)
1636 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1637 if (imsg_add(wbuf, ng_err->msg, ing.reason_len) == -1)
1638 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1640 wbuf->fd = -1;
1641 imsg_close(&client->iev.ibuf, wbuf);
1642 gotd_imsg_event_add(&client->iev);
1643 return NULL;
1646 static const struct got_error *
1647 install_pack(struct gotd_client *client, const char *repo_path,
1648 struct imsg *imsg)
1650 const struct got_error *err = NULL;
1651 struct gotd_imsg_packfile_install inst;
1652 char hex[SHA1_DIGEST_STRING_LENGTH];
1653 size_t datalen;
1654 char *packfile_path = NULL, *packidx_path = NULL;
1656 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1657 if (datalen != sizeof(inst))
1658 return got_error(GOT_ERR_PRIVSEP_LEN);
1659 memcpy(&inst, imsg->data, sizeof(inst));
1661 if (client->packfile_path == NULL)
1662 return got_error_msg(GOT_ERR_BAD_REQUEST,
1663 "client has no pack file");
1664 if (client->packidx_path == NULL)
1665 return got_error_msg(GOT_ERR_BAD_REQUEST,
1666 "client has no pack file index");
1668 if (got_sha1_digest_to_str(inst.pack_sha1, hex, sizeof(hex)) == NULL)
1669 return got_error_msg(GOT_ERR_NO_SPACE,
1670 "could not convert pack file SHA1 to hex");
1672 if (asprintf(&packfile_path, "/%s/%s/pack-%s.pack",
1673 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1674 err = got_error_from_errno("asprintf");
1675 goto done;
1678 if (asprintf(&packidx_path, "/%s/%s/pack-%s.idx",
1679 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1680 err = got_error_from_errno("asprintf");
1681 goto done;
1684 if (rename(client->packfile_path, packfile_path) == -1) {
1685 err = got_error_from_errno3("rename", client->packfile_path,
1686 packfile_path);
1687 goto done;
1690 free(client->packfile_path);
1691 client->packfile_path = NULL;
1693 if (rename(client->packidx_path, packidx_path) == -1) {
1694 err = got_error_from_errno3("rename", client->packidx_path,
1695 packidx_path);
1696 goto done;
1699 free(client->packidx_path);
1700 client->packidx_path = NULL;
1701 done:
1702 free(packfile_path);
1703 free(packidx_path);
1704 return err;
1707 static const struct got_error *
1708 begin_ref_updates(struct gotd_client *client, struct imsg *imsg)
1710 struct gotd_imsg_ref_updates_start istart;
1711 size_t datalen;
1713 if (client->nref_updates != -1)
1714 return got_error(GOT_ERR_PRIVSEP_MSG);
1716 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1717 if (datalen != sizeof(istart))
1718 return got_error(GOT_ERR_PRIVSEP_LEN);
1719 memcpy(&istart, imsg->data, sizeof(istart));
1721 if (istart.nref_updates <= 0)
1722 return got_error(GOT_ERR_PRIVSEP_MSG);
1724 client->nref_updates = istart.nref_updates;
1725 return NULL;
1728 static const struct got_error *
1729 update_ref(struct gotd_client *client, const char *repo_path,
1730 struct imsg *imsg)
1732 const struct got_error *err = NULL;
1733 struct got_repository *repo = NULL;
1734 struct got_reference *ref = NULL;
1735 struct gotd_imsg_ref_update iref;
1736 struct got_object_id old_id, new_id;
1737 struct got_object_id *id = NULL;
1738 struct got_object *obj = NULL;
1739 char *refname = NULL;
1740 size_t datalen;
1741 int locked = 0;
1743 log_debug("update-ref from uid %d", client->euid);
1745 if (client->nref_updates <= 0)
1746 return got_error(GOT_ERR_PRIVSEP_MSG);
1748 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1749 if (datalen < sizeof(iref))
1750 return got_error(GOT_ERR_PRIVSEP_LEN);
1751 memcpy(&iref, imsg->data, sizeof(iref));
1752 if (datalen != sizeof(iref) + iref.name_len)
1753 return got_error(GOT_ERR_PRIVSEP_LEN);
1754 refname = malloc(iref.name_len + 1);
1755 if (refname == NULL)
1756 return got_error_from_errno("malloc");
1757 memcpy(refname, imsg->data + sizeof(iref), iref.name_len);
1758 refname[iref.name_len] = '\0';
1760 log_debug("updating ref %s for uid %d", refname, client->euid);
1762 err = got_repo_open(&repo, repo_path, NULL, NULL);
1763 if (err)
1764 goto done;
1766 memcpy(old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
1767 memcpy(new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
1768 err = got_object_open(&obj, repo, &new_id);
1769 if (err)
1770 goto done;
1772 if (iref.ref_is_new) {
1773 err = got_ref_open(&ref, repo, refname, 0);
1774 if (err) {
1775 if (err->code != GOT_ERR_NOT_REF)
1776 goto done;
1777 err = got_ref_alloc(&ref, refname, &new_id);
1778 if (err)
1779 goto done;
1780 err = got_ref_write(ref, repo); /* will lock/unlock */
1781 if (err)
1782 goto done;
1783 } else {
1784 err = got_error_fmt(GOT_ERR_REF_BUSY,
1785 "%s has been created by someone else "
1786 "while transaction was in progress",
1787 got_ref_get_name(ref));
1788 goto done;
1790 } else {
1791 err = got_ref_open(&ref, repo, refname, 1 /* lock */);
1792 if (err)
1793 goto done;
1794 locked = 1;
1796 err = got_ref_resolve(&id, repo, ref);
1797 if (err)
1798 goto done;
1800 if (got_object_id_cmp(id, &old_id) != 0) {
1801 err = got_error_fmt(GOT_ERR_REF_BUSY,
1802 "%s has been modified by someone else "
1803 "while transaction was in progress",
1804 got_ref_get_name(ref));
1805 goto done;
1808 err = got_ref_change_ref(ref, &new_id);
1809 if (err)
1810 goto done;
1812 err = got_ref_write(ref, repo);
1813 if (err)
1814 goto done;
1816 free(id);
1817 id = NULL;
1819 done:
1820 if (err) {
1821 if (err->code == GOT_ERR_LOCKFILE_TIMEOUT) {
1822 err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
1823 "could not acquire exclusive file lock for %s",
1824 refname);
1826 send_ref_update_ng(client, &iref, refname, err->msg);
1827 } else
1828 send_ref_update_ok(client, &iref, refname);
1830 if (client->nref_updates > 0) {
1831 client->nref_updates--;
1832 if (client->nref_updates == 0)
1833 send_refs_updated(client);
1836 if (locked) {
1837 const struct got_error *unlock_err;
1838 unlock_err = got_ref_unlock(ref);
1839 if (unlock_err && err == NULL)
1840 err = unlock_err;
1842 if (ref)
1843 got_ref_close(ref);
1844 if (obj)
1845 got_object_close(obj);
1846 if (repo)
1847 got_repo_close(repo);
1848 free(refname);
1849 free(id);
1850 return err;
1853 static void
1854 gotd_dispatch_listener(int fd, short event, void *arg)
1856 struct gotd_imsgev *iev = arg;
1857 struct imsgbuf *ibuf = &iev->ibuf;
1858 struct gotd_child_proc *proc = &gotd.listen_proc;
1859 ssize_t n;
1860 int shut = 0;
1861 struct imsg imsg;
1863 if (proc->iev.ibuf.fd != fd)
1864 fatalx("%s: unexpected fd %d", __func__, fd);
1866 if (event & EV_READ) {
1867 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1868 fatal("imsg_read error");
1869 if (n == 0) {
1870 /* Connection closed. */
1871 shut = 1;
1872 goto done;
1876 if (event & EV_WRITE) {
1877 n = msgbuf_write(&ibuf->w);
1878 if (n == -1 && errno != EAGAIN)
1879 fatal("msgbuf_write");
1880 if (n == 0) {
1881 /* Connection closed. */
1882 shut = 1;
1883 goto done;
1887 for (;;) {
1888 const struct got_error *err = NULL;
1889 struct gotd_client *client = NULL;
1890 uint32_t client_id = 0;
1891 int do_disconnect = 0;
1893 if ((n = imsg_get(ibuf, &imsg)) == -1)
1894 fatal("%s: imsg_get error", __func__);
1895 if (n == 0) /* No more messages. */
1896 break;
1898 switch (imsg.hdr.type) {
1899 case GOTD_IMSG_ERROR:
1900 do_disconnect = 1;
1901 err = gotd_imsg_recv_error(&client_id, &imsg);
1902 break;
1903 case GOTD_IMSG_CONNECT:
1904 err = recv_connect(&client_id, &imsg);
1905 break;
1906 default:
1907 log_debug("unexpected imsg %d", imsg.hdr.type);
1908 break;
1911 client = find_client(client_id);
1912 if (client == NULL) {
1913 log_warnx("%s: client not found", __func__);
1914 imsg_free(&imsg);
1915 continue;
1918 if (err)
1919 log_warnx("uid %d: %s", client->euid, err->msg);
1921 if (do_disconnect) {
1922 if (err)
1923 disconnect_on_error(client, err);
1924 else
1925 disconnect(client);
1928 imsg_free(&imsg);
1930 done:
1931 if (!shut) {
1932 gotd_imsg_event_add(iev);
1933 } else {
1934 /* This pipe is dead. Remove its event handler */
1935 event_del(&iev->ev);
1936 event_loopexit(NULL);
1940 static void
1941 gotd_dispatch_auth_child(int fd, short event, void *arg)
1943 const struct got_error *err = NULL;
1944 struct gotd_imsgev *iev = arg;
1945 struct imsgbuf *ibuf = &iev->ibuf;
1946 struct gotd_client *client;
1947 struct gotd_repo *repo = NULL;
1948 ssize_t n;
1949 int shut = 0;
1950 struct imsg imsg;
1951 uint32_t client_id = 0;
1952 int do_disconnect = 0;
1953 enum gotd_procid proc_type;
1955 client = find_client_by_proc_fd(fd);
1956 if (client == NULL)
1957 fatalx("cannot find client for fd %d", fd);
1959 if (client->auth == NULL)
1960 fatalx("cannot find auth child process for fd %d", fd);
1962 if (event & EV_READ) {
1963 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1964 fatal("imsg_read error");
1965 if (n == 0) {
1966 /* Connection closed. */
1967 shut = 1;
1968 goto done;
1972 if (event & EV_WRITE) {
1973 n = msgbuf_write(&ibuf->w);
1974 if (n == -1 && errno != EAGAIN)
1975 fatal("msgbuf_write");
1976 if (n == 0) {
1977 /* Connection closed. */
1978 shut = 1;
1980 goto done;
1983 if (client->auth->iev.ibuf.fd != fd)
1984 fatalx("%s: unexpected fd %d", __func__, fd);
1986 if ((n = imsg_get(ibuf, &imsg)) == -1)
1987 fatal("%s: imsg_get error", __func__);
1988 if (n == 0) /* No more messages. */
1989 return;
1991 evtimer_del(&client->tmo);
1993 switch (imsg.hdr.type) {
1994 case GOTD_IMSG_ERROR:
1995 do_disconnect = 1;
1996 err = gotd_imsg_recv_error(&client_id, &imsg);
1997 break;
1998 case GOTD_IMSG_ACCESS_GRANTED:
1999 break;
2000 default:
2001 do_disconnect = 1;
2002 log_debug("unexpected imsg %d", imsg.hdr.type);
2003 break;
2006 if (!verify_imsg_src(client, client->auth, &imsg)) {
2007 do_disconnect = 1;
2008 log_debug("dropping imsg type %d from PID %d",
2009 imsg.hdr.type, client->auth->pid);
2011 imsg_free(&imsg);
2013 if (do_disconnect) {
2014 if (err)
2015 disconnect_on_error(client, err);
2016 else
2017 disconnect(client);
2018 goto done;
2021 repo = find_repo_by_name(client->auth->repo_name);
2022 if (repo == NULL) {
2023 err = got_error(GOT_ERR_NOT_GIT_REPO);
2024 goto done;
2026 kill_auth_proc(client);
2028 log_info("authenticated uid %d for repository %s\n",
2029 client->euid, repo->name);
2031 if (client->required_auth & GOTD_AUTH_WRITE)
2032 proc_type = PROC_REPO_WRITE;
2033 else
2034 proc_type = PROC_REPO_READ;
2036 err = start_repo_child(client, proc_type, repo,
2037 gotd.argv0, gotd.confpath, gotd.daemonize,
2038 gotd.verbosity);
2039 done:
2040 if (err)
2041 log_warnx("uid %d: %s", client->euid, err->msg);
2043 /* We might have killed the auth process by now. */
2044 if (client->auth != NULL) {
2045 if (!shut) {
2046 gotd_imsg_event_add(iev);
2047 } else {
2048 /* This pipe is dead. Remove its event handler */
2049 event_del(&iev->ev);
2054 static void
2055 gotd_dispatch_repo_child(int fd, short event, void *arg)
2057 struct gotd_imsgev *iev = arg;
2058 struct imsgbuf *ibuf = &iev->ibuf;
2059 struct gotd_child_proc *proc = NULL;
2060 struct gotd_client *client = NULL;
2061 ssize_t n;
2062 int shut = 0;
2063 struct imsg imsg;
2065 if (event & EV_READ) {
2066 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
2067 fatal("imsg_read error");
2068 if (n == 0) {
2069 /* Connection closed. */
2070 shut = 1;
2071 goto done;
2075 if (event & EV_WRITE) {
2076 n = msgbuf_write(&ibuf->w);
2077 if (n == -1 && errno != EAGAIN)
2078 fatal("msgbuf_write");
2079 if (n == 0) {
2080 /* Connection closed. */
2081 shut = 1;
2082 goto done;
2086 client = find_client_by_proc_fd(fd);
2087 if (client == NULL)
2088 fatalx("cannot find client for fd %d", fd);
2090 proc = get_client_proc(client);
2091 if (proc == NULL)
2092 fatalx("cannot find child process for fd %d", fd);
2094 for (;;) {
2095 const struct got_error *err = NULL;
2096 uint32_t client_id = 0;
2097 int do_disconnect = 0;
2098 int do_list_refs = 0, do_ref_updates = 0, do_ref_update = 0;
2099 int do_packfile_install = 0;
2101 if ((n = imsg_get(ibuf, &imsg)) == -1)
2102 fatal("%s: imsg_get error", __func__);
2103 if (n == 0) /* No more messages. */
2104 break;
2106 switch (imsg.hdr.type) {
2107 case GOTD_IMSG_ERROR:
2108 do_disconnect = 1;
2109 err = gotd_imsg_recv_error(&client_id, &imsg);
2110 break;
2111 case GOTD_IMSG_REPO_CHILD_READY:
2112 do_list_refs = 1;
2113 break;
2114 case GOTD_IMSG_PACKFILE_DONE:
2115 do_disconnect = 1;
2116 err = recv_packfile_done(&client_id, &imsg);
2117 break;
2118 case GOTD_IMSG_PACKFILE_INSTALL:
2119 err = recv_packfile_install(&client_id, &imsg);
2120 if (err == NULL)
2121 do_packfile_install = 1;
2122 break;
2123 case GOTD_IMSG_REF_UPDATES_START:
2124 err = recv_ref_updates_start(&client_id, &imsg);
2125 if (err == NULL)
2126 do_ref_updates = 1;
2127 break;
2128 case GOTD_IMSG_REF_UPDATE:
2129 err = recv_ref_update(&client_id, &imsg);
2130 if (err == NULL)
2131 do_ref_update = 1;
2132 break;
2133 default:
2134 log_debug("unexpected imsg %d", imsg.hdr.type);
2135 break;
2138 if (!verify_imsg_src(client, proc, &imsg)) {
2139 log_debug("dropping imsg type %d from PID %d",
2140 imsg.hdr.type, proc->pid);
2141 imsg_free(&imsg);
2142 continue;
2144 if (err)
2145 log_warnx("uid %d: %s", client->euid, err->msg);
2147 if (do_disconnect) {
2148 if (err)
2149 disconnect_on_error(client, err);
2150 else
2151 disconnect(client);
2152 } else {
2153 if (do_list_refs)
2154 err = list_refs_request(client, iev);
2155 else if (do_packfile_install)
2156 err = install_pack(client, proc->repo_path,
2157 &imsg);
2158 else if (do_ref_updates)
2159 err = begin_ref_updates(client, &imsg);
2160 else if (do_ref_update)
2161 err = update_ref(client, proc->repo_path,
2162 &imsg);
2163 if (err)
2164 log_warnx("uid %d: %s", client->euid, err->msg);
2166 imsg_free(&imsg);
2168 done:
2169 if (!shut) {
2170 gotd_imsg_event_add(iev);
2171 } else {
2172 /* This pipe is dead. Remove its event handler */
2173 event_del(&iev->ev);
2174 event_loopexit(NULL);
2178 static pid_t
2179 start_child(enum gotd_procid proc_id, const char *repo_path,
2180 char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
2182 char *argv[11];
2183 int argc = 0;
2184 pid_t pid;
2186 switch (pid = fork()) {
2187 case -1:
2188 fatal("cannot fork");
2189 case 0:
2190 break;
2191 default:
2192 close(fd);
2193 return pid;
2196 if (fd != GOTD_FILENO_MSG_PIPE) {
2197 if (dup2(fd, GOTD_FILENO_MSG_PIPE) == -1)
2198 fatal("cannot setup imsg fd");
2199 } else if (fcntl(fd, F_SETFD, 0) == -1)
2200 fatal("cannot setup imsg fd");
2202 argv[argc++] = argv0;
2203 switch (proc_id) {
2204 case PROC_LISTEN:
2205 argv[argc++] = (char *)"-L";
2206 break;
2207 case PROC_AUTH:
2208 argv[argc++] = (char *)"-A";
2209 break;
2210 case PROC_REPO_READ:
2211 argv[argc++] = (char *)"-R";
2212 break;
2213 case PROC_REPO_WRITE:
2214 argv[argc++] = (char *)"-W";
2215 break;
2216 default:
2217 fatalx("invalid process id %d", proc_id);
2220 argv[argc++] = (char *)"-f";
2221 argv[argc++] = (char *)confpath;
2223 if (repo_path) {
2224 argv[argc++] = (char *)"-P";
2225 argv[argc++] = (char *)repo_path;
2228 if (!daemonize)
2229 argv[argc++] = (char *)"-d";
2230 if (verbosity > 0)
2231 argv[argc++] = (char *)"-v";
2232 if (verbosity > 1)
2233 argv[argc++] = (char *)"-v";
2234 argv[argc++] = NULL;
2236 execvp(argv0, argv);
2237 fatal("execvp");
2240 static void
2241 start_listener(char *argv0, const char *confpath, int daemonize, int verbosity)
2243 struct gotd_child_proc *proc = &gotd.listen_proc;
2245 proc->type = PROC_LISTEN;
2247 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
2248 PF_UNSPEC, proc->pipe) == -1)
2249 fatal("socketpair");
2251 proc->pid = start_child(proc->type, NULL, argv0, confpath,
2252 proc->pipe[1], daemonize, verbosity);
2253 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
2254 proc->iev.handler = gotd_dispatch_listener;
2255 proc->iev.events = EV_READ;
2256 proc->iev.handler_arg = NULL;
2259 static const struct got_error *
2260 start_repo_child(struct gotd_client *client, enum gotd_procid proc_type,
2261 struct gotd_repo *repo, char *argv0, const char *confpath,
2262 int daemonize, int verbosity)
2264 struct gotd_child_proc *proc;
2266 if (proc_type != PROC_REPO_READ && proc_type != PROC_REPO_WRITE)
2267 return got_error_msg(GOT_ERR_NOT_IMPL, "bad process type");
2269 proc = calloc(1, sizeof(*proc));
2270 if (proc == NULL)
2271 return got_error_from_errno("calloc");
2273 proc->type = proc_type;
2274 if (strlcpy(proc->repo_name, repo->name,
2275 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
2276 fatalx("repository name too long: %s", repo->name);
2277 log_debug("starting %s for repository %s",
2278 proc->type == PROC_REPO_READ ? "reader" : "writer", repo->name);
2279 if (realpath(repo->path, proc->repo_path) == NULL)
2280 fatal("%s", repo->path);
2281 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
2282 PF_UNSPEC, proc->pipe) == -1)
2283 fatal("socketpair");
2284 proc->pid = start_child(proc->type, proc->repo_path, argv0,
2285 confpath, proc->pipe[1], daemonize, verbosity);
2286 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
2287 log_debug("proc %s %s is on fd %d",
2288 gotd_proc_names[proc->type], proc->repo_path,
2289 proc->pipe[0]);
2290 proc->iev.handler = gotd_dispatch_repo_child;
2291 proc->iev.events = EV_READ;
2292 proc->iev.handler_arg = NULL;
2293 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
2294 gotd_dispatch_repo_child, &proc->iev);
2295 gotd_imsg_event_add(&proc->iev);
2297 if (proc->type == PROC_REPO_READ)
2298 client->repo_read = proc;
2299 else
2300 client->repo_write = proc;
2302 return NULL;
2305 static const struct got_error *
2306 start_auth_child(struct gotd_client *client, int required_auth,
2307 struct gotd_repo *repo, char *argv0, const char *confpath,
2308 int daemonize, int verbosity)
2310 struct gotd_child_proc *proc;
2311 struct gotd_imsg_auth iauth;
2313 memset(&iauth, 0, sizeof(iauth));
2315 proc = calloc(1, sizeof(*proc));
2316 if (proc == NULL)
2317 return got_error_from_errno("calloc");
2319 proc->type = PROC_AUTH;
2320 if (strlcpy(proc->repo_name, repo->name,
2321 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
2322 fatalx("repository name too long: %s", repo->name);
2323 log_debug("starting auth for uid %d repository %s",
2324 client->euid, repo->name);
2325 if (realpath(repo->path, proc->repo_path) == NULL)
2326 fatal("%s", repo->path);
2327 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
2328 PF_UNSPEC, proc->pipe) == -1)
2329 fatal("socketpair");
2330 proc->pid = start_child(proc->type, proc->repo_path, argv0,
2331 confpath, proc->pipe[1], daemonize, verbosity);
2332 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
2333 log_debug("proc %s %s is on fd %d",
2334 gotd_proc_names[proc->type], proc->repo_path,
2335 proc->pipe[0]);
2336 proc->iev.handler = gotd_dispatch_auth_child;
2337 proc->iev.events = EV_READ;
2338 proc->iev.handler_arg = NULL;
2339 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
2340 gotd_dispatch_auth_child, &proc->iev);
2341 gotd_imsg_event_add(&proc->iev);
2343 iauth.euid = client->euid;
2344 iauth.egid = client->egid;
2345 iauth.required_auth = required_auth;
2346 iauth.client_id = client->id;
2347 if (gotd_imsg_compose_event(&proc->iev, GOTD_IMSG_AUTHENTICATE,
2348 PROC_GOTD, -1, &iauth, sizeof(iauth)) == -1)
2349 log_warn("imsg compose AUTHENTICATE");
2351 client->auth = proc;
2352 client->required_auth = required_auth;
2353 return NULL;
2356 static void
2357 apply_unveil_repo_readonly(const char *repo_path)
2359 if (unveil(repo_path, "r") == -1)
2360 fatal("unveil %s", repo_path);
2362 if (unveil(NULL, NULL) == -1)
2363 fatal("unveil");
2366 static void
2367 apply_unveil(void)
2369 struct gotd_repo *repo;
2371 if (unveil(gotd.argv0, "x") == -1)
2372 fatal("unveil %s", gotd.argv0);
2374 TAILQ_FOREACH(repo, &gotd.repos, entry) {
2375 if (unveil(repo->path, "rwc") == -1)
2376 fatal("unveil %s", repo->path);
2379 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
2380 fatal("unveil %s", GOT_TMPDIR_STR);
2382 if (unveil(NULL, NULL) == -1)
2383 fatal("unveil");
2386 int
2387 main(int argc, char **argv)
2389 const struct got_error *error = NULL;
2390 int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
2391 const char *confpath = GOTD_CONF_PATH;
2392 char *argv0 = argv[0];
2393 char title[2048];
2394 gid_t groups[NGROUPS_MAX];
2395 int ngroups = NGROUPS_MAX;
2396 struct passwd *pw = NULL;
2397 struct group *gr = NULL;
2398 char *repo_path = NULL;
2399 enum gotd_procid proc_id = PROC_GOTD;
2400 struct event evsigint, evsigterm, evsighup, evsigusr1;
2401 int *pack_fds = NULL, *temp_fds = NULL;
2403 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
2405 while ((ch = getopt(argc, argv, "Adf:LnP:RvW")) != -1) {
2406 switch (ch) {
2407 case 'A':
2408 proc_id = PROC_AUTH;
2409 break;
2410 case 'd':
2411 daemonize = 0;
2412 break;
2413 case 'f':
2414 confpath = optarg;
2415 break;
2416 case 'L':
2417 proc_id = PROC_LISTEN;
2418 break;
2419 case 'n':
2420 noaction = 1;
2421 break;
2422 case 'P':
2423 repo_path = realpath(optarg, NULL);
2424 if (repo_path == NULL)
2425 fatal("realpath '%s'", optarg);
2426 break;
2427 case 'R':
2428 proc_id = PROC_REPO_READ;
2429 break;
2430 case 'v':
2431 if (verbosity < 3)
2432 verbosity++;
2433 break;
2434 case 'W':
2435 proc_id = PROC_REPO_WRITE;
2436 break;
2437 default:
2438 usage();
2442 argc -= optind;
2443 argv += optind;
2445 if (argc != 0)
2446 usage();
2448 /* Require an absolute path in argv[0] for reliable re-exec. */
2449 if (!got_path_is_absolute(argv0))
2450 fatalx("bad path \"%s\": must be an absolute path", argv0);
2452 if (geteuid() && (proc_id == PROC_GOTD || proc_id == PROC_LISTEN))
2453 fatalx("need root privileges");
2455 log_init(daemonize ? 0 : 1, LOG_DAEMON);
2456 log_setverbose(verbosity);
2458 if (parse_config(confpath, proc_id, &gotd) != 0)
2459 return 1;
2461 gotd.argv0 = argv0;
2462 gotd.daemonize = daemonize;
2463 gotd.verbosity = verbosity;
2464 gotd.confpath = confpath;
2466 if (proc_id == PROC_GOTD &&
2467 (gotd.nrepos == 0 || TAILQ_EMPTY(&gotd.repos)))
2468 fatalx("no repository defined in configuration file");
2470 pw = getpwnam(gotd.user_name);
2471 if (pw == NULL)
2472 fatalx("user %s not found", gotd.user_name);
2474 if (pw->pw_uid == 0) {
2475 fatalx("cannot run %s as %s: the user running %s "
2476 "must not be the superuser",
2477 getprogname(), pw->pw_name, getprogname());
2480 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups) == -1)
2481 log_warnx("group membership list truncated");
2483 gr = match_group(groups, ngroups, gotd.unix_group_name);
2484 if (gr == NULL) {
2485 fatalx("cannot start %s: the user running %s "
2486 "must be a secondary member of group %s",
2487 getprogname(), getprogname(), gotd.unix_group_name);
2489 if (gr->gr_gid == pw->pw_gid) {
2490 fatalx("cannot start %s: the user running %s "
2491 "must be a secondary member of group %s, but "
2492 "%s is the user's primary group",
2493 getprogname(), getprogname(), gotd.unix_group_name,
2494 gotd.unix_group_name);
2497 if (proc_id == PROC_LISTEN &&
2498 !got_path_is_absolute(gotd.unix_socket_path))
2499 fatalx("bad unix socket path \"%s\": must be an absolute path",
2500 gotd.unix_socket_path);
2502 if (noaction)
2503 return 0;
2505 if (proc_id == PROC_GOTD) {
2506 gotd.pid = getpid();
2507 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2508 start_listener(argv0, confpath, daemonize, verbosity);
2509 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
2510 if (daemonize && daemon(1, 0) == -1)
2511 fatal("daemon");
2512 } else if (proc_id == PROC_LISTEN) {
2513 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2514 if (verbosity) {
2515 log_info("socket: %s", gotd.unix_socket_path);
2516 log_info("user: %s", pw->pw_name);
2517 log_info("secondary group: %s", gr->gr_name);
2520 fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
2521 gr->gr_gid);
2522 if (fd == -1) {
2523 fatal("cannot listen on unix socket %s",
2524 gotd.unix_socket_path);
2526 if (daemonize && daemon(0, 0) == -1)
2527 fatal("daemon");
2528 } else if (proc_id == PROC_AUTH) {
2529 snprintf(title, sizeof(title), "%s %s",
2530 gotd_proc_names[proc_id], repo_path);
2531 if (daemonize && daemon(0, 0) == -1)
2532 fatal("daemon");
2533 } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE) {
2534 error = got_repo_pack_fds_open(&pack_fds);
2535 if (error != NULL)
2536 fatalx("cannot open pack tempfiles: %s", error->msg);
2537 error = got_repo_temp_fds_open(&temp_fds);
2538 if (error != NULL)
2539 fatalx("cannot open pack tempfiles: %s", error->msg);
2540 if (repo_path == NULL)
2541 fatalx("repository path not specified");
2542 snprintf(title, sizeof(title), "%s %s",
2543 gotd_proc_names[proc_id], repo_path);
2544 if (daemonize && daemon(0, 0) == -1)
2545 fatal("daemon");
2546 } else
2547 fatal("invalid process id %d", proc_id);
2549 setproctitle("%s", title);
2550 log_procinit(title);
2552 /* Drop root privileges. */
2553 if (setgid(pw->pw_gid) == -1)
2554 fatal("setgid %d failed", pw->pw_gid);
2555 if (setuid(pw->pw_uid) == -1)
2556 fatal("setuid %d failed", pw->pw_uid);
2558 event_init();
2560 switch (proc_id) {
2561 case PROC_GOTD:
2562 #ifndef PROFILE
2563 if (pledge("stdio rpath wpath cpath proc exec "
2564 "sendfd recvfd fattr flock unix unveil", NULL) == -1)
2565 err(1, "pledge");
2566 #endif
2567 break;
2568 case PROC_LISTEN:
2569 #ifndef PROFILE
2570 if (pledge("stdio sendfd unix", NULL) == -1)
2571 err(1, "pledge");
2572 #endif
2573 listen_main(title, fd);
2574 /* NOTREACHED */
2575 break;
2576 case PROC_AUTH:
2577 #ifndef PROFILE
2578 if (pledge("stdio getpw", NULL) == -1)
2579 err(1, "pledge");
2580 #endif
2581 auth_main(title, &gotd.repos, repo_path);
2582 /* NOTREACHED */
2583 break;
2584 case PROC_REPO_READ:
2585 #ifndef PROFILE
2586 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2587 err(1, "pledge");
2588 #endif
2589 apply_unveil_repo_readonly(repo_path);
2590 repo_read_main(title, repo_path, pack_fds, temp_fds);
2591 /* NOTREACHED */
2592 exit(0);
2593 case PROC_REPO_WRITE:
2594 #ifndef PROFILE
2595 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2596 err(1, "pledge");
2597 #endif
2598 apply_unveil_repo_readonly(repo_path);
2599 repo_write_main(title, repo_path, pack_fds, temp_fds);
2600 /* NOTREACHED */
2601 exit(0);
2602 default:
2603 fatal("invalid process id %d", proc_id);
2606 if (proc_id != PROC_GOTD)
2607 fatal("invalid process id %d", proc_id);
2609 apply_unveil();
2611 signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
2612 signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
2613 signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
2614 signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
2615 signal(SIGPIPE, SIG_IGN);
2617 signal_add(&evsigint, NULL);
2618 signal_add(&evsigterm, NULL);
2619 signal_add(&evsighup, NULL);
2620 signal_add(&evsigusr1, NULL);
2622 gotd_imsg_event_add(&gotd.listen_proc.iev);
2624 event_dispatch();
2626 if (pack_fds)
2627 got_repo_pack_fds_close(pack_fds);
2628 free(repo_path);
2629 return 0;