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 "auth.h"
62 #include "repo_read.h"
63 #include "repo_write.h"
65 #ifndef nitems
66 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
67 #endif
69 struct gotd_client {
70 STAILQ_ENTRY(gotd_client) entry;
71 enum gotd_client_state state;
72 struct gotd_client_capability *capabilities;
73 size_t ncapa_alloc;
74 size_t ncapabilities;
75 uint32_t id;
76 int fd;
77 int delta_cache_fd;
78 struct gotd_imsgev iev;
79 struct event tmo;
80 uid_t euid;
81 gid_t egid;
82 struct gotd_child_proc *repo_read;
83 struct gotd_child_proc *repo_write;
84 char *packfile_path;
85 char *packidx_path;
86 int nref_updates;
87 };
88 STAILQ_HEAD(gotd_clients, gotd_client);
90 static struct gotd_clients gotd_clients[GOTD_CLIENT_TABLE_SIZE];
91 static SIPHASH_KEY clients_hash_key;
92 volatile int client_cnt;
93 static struct timeval timeout = { 3600, 0 };
94 static int inflight;
95 static struct gotd gotd;
97 void gotd_sighdlr(int sig, short event, void *arg);
98 static void gotd_shutdown(void);
100 __dead static void
101 usage()
103 fprintf(stderr, "usage: %s [-dv] [-f config-file]\n", getprogname());
104 exit(1);
107 static int
108 unix_socket_listen(const char *unix_socket_path, uid_t uid, gid_t gid)
110 struct sockaddr_un sun;
111 int fd = -1;
112 mode_t old_umask, mode;
114 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
115 if (fd == -1) {
116 log_warn("socket");
117 return -1;
120 sun.sun_family = AF_UNIX;
121 if (strlcpy(sun.sun_path, unix_socket_path,
122 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
123 log_warnx("%s: name too long", unix_socket_path);
124 close(fd);
125 return -1;
128 if (unlink(unix_socket_path) == -1) {
129 if (errno != ENOENT) {
130 log_warn("unlink %s", unix_socket_path);
131 close(fd);
132 return -1;
136 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
137 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
139 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
140 log_warn("bind: %s", unix_socket_path);
141 close(fd);
142 umask(old_umask);
143 return -1;
146 umask(old_umask);
148 if (chmod(unix_socket_path, mode) == -1) {
149 log_warn("chmod %o %s", mode, unix_socket_path);
150 close(fd);
151 unlink(unix_socket_path);
152 return -1;
155 if (chown(unix_socket_path, uid, gid) == -1) {
156 log_warn("chown %s uid=%d gid=%d", unix_socket_path, uid, gid);
157 close(fd);
158 unlink(unix_socket_path);
159 return -1;
162 if (listen(fd, GOTD_UNIX_SOCKET_BACKLOG) == -1) {
163 log_warn("listen");
164 close(fd);
165 unlink(unix_socket_path);
166 return -1;
169 return fd;
172 static struct group *
173 match_group(gid_t *groups, int ngroups, const char *unix_group_name)
175 struct group *gr;
176 int i;
178 for (i = 0; i < ngroups; i++) {
179 gr = getgrgid(groups[i]);
180 if (gr == NULL) {
181 log_warn("getgrgid %d", groups[i]);
182 continue;
184 if (strcmp(gr->gr_name, unix_group_name) == 0)
185 return gr;
188 return NULL;
191 static int
192 accept_reserve(int fd, struct sockaddr *addr, socklen_t *addrlen,
193 int reserve, volatile int *counter)
195 int ret;
197 if (getdtablecount() + reserve +
198 ((*counter + 1) * GOTD_FD_NEEDED) >= getdtablesize()) {
199 log_debug("inflight fds exceeded");
200 errno = EMFILE;
201 return -1;
204 if ((ret = accept4(fd, addr, addrlen,
205 SOCK_NONBLOCK | SOCK_CLOEXEC)) > -1) {
206 (*counter)++;
209 return ret;
212 static uint64_t
213 client_hash(uint32_t client_id)
215 return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
218 static void
219 add_client(struct gotd_client *client)
221 uint64_t slot = client_hash(client->id) % nitems(gotd_clients);
222 STAILQ_INSERT_HEAD(&gotd_clients[slot], client, entry);
223 client_cnt++;
226 static struct gotd_client *
227 find_client(uint32_t client_id)
229 uint64_t slot;
230 struct gotd_client *c;
232 slot = client_hash(client_id) % nitems(gotd_clients);
233 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
234 if (c->id == client_id)
235 return c;
238 return NULL;
241 static uint32_t
242 get_client_id(void)
244 int duplicate = 0;
245 uint32_t id;
247 do {
248 id = arc4random();
249 duplicate = (find_client(id) != NULL);
250 } while (duplicate || id == 0);
252 return id;
255 static struct gotd_child_proc *
256 get_client_proc(struct gotd_client *client)
258 if (client->repo_read && client->repo_write) {
259 fatalx("uid %d is reading and writing in the same session",
260 client->euid);
261 /* NOTREACHED */
264 if (client->repo_read)
265 return client->repo_read;
266 else if (client->repo_write)
267 return client->repo_write;
269 return NULL;
272 static int
273 client_is_reading(struct gotd_client *client)
275 return client->repo_read != NULL;
278 static int
279 client_is_writing(struct gotd_client *client)
281 return client->repo_write != NULL;
284 static const struct got_error *
285 ensure_client_is_reading(struct gotd_client *client)
287 if (!client_is_reading(client)) {
288 return got_error_fmt(GOT_ERR_BAD_PACKET,
289 "uid %d made a read-request but is not reading from "
290 "a repository", client->euid);
293 return NULL;
296 static const struct got_error *
297 ensure_client_is_writing(struct gotd_client *client)
299 if (!client_is_writing(client)) {
300 return got_error_fmt(GOT_ERR_BAD_PACKET,
301 "uid %d made a write-request but is not writing to "
302 "a repository", client->euid);
305 return NULL;
308 static const struct got_error *
309 ensure_client_is_not_writing(struct gotd_client *client)
311 if (client_is_writing(client)) {
312 return got_error_fmt(GOT_ERR_BAD_PACKET,
313 "uid %d made a read-request but is writing to "
314 "a repository", client->euid);
317 return NULL;
320 static const struct got_error *
321 ensure_client_is_not_reading(struct gotd_client *client)
323 if (client_is_reading(client)) {
324 return got_error_fmt(GOT_ERR_BAD_PACKET,
325 "uid %d made a write-request but is reading from "
326 "a repository", client->euid);
329 return NULL;
332 static void
333 disconnect(struct gotd_client *client)
335 struct gotd_imsg_disconnect idisconnect;
336 struct gotd_child_proc *proc = get_client_proc(client);
337 uint64_t slot;
339 log_debug("uid %d: disconnecting", client->euid);
341 idisconnect.client_id = client->id;
342 if (proc) {
343 if (gotd_imsg_compose_event(&proc->iev,
344 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
345 &idisconnect, sizeof(idisconnect)) == -1)
346 log_warn("imsg compose DISCONNECT");
348 slot = client_hash(client->id) % nitems(gotd_clients);
349 STAILQ_REMOVE(&gotd_clients[slot], client, gotd_client, entry);
350 imsg_clear(&client->iev.ibuf);
351 event_del(&client->iev.ev);
352 evtimer_del(&client->tmo);
353 close(client->fd);
354 if (client->delta_cache_fd != -1)
355 close(client->delta_cache_fd);
356 if (client->packfile_path) {
357 if (unlink(client->packfile_path) == -1 && errno != ENOENT)
358 log_warn("unlink %s: ", client->packfile_path);
359 free(client->packfile_path);
361 if (client->packidx_path) {
362 if (unlink(client->packidx_path) == -1 && errno != ENOENT)
363 log_warn("unlink %s: ", client->packidx_path);
364 free(client->packidx_path);
366 free(client->capabilities);
367 free(client);
368 inflight--;
369 client_cnt--;
372 static void
373 disconnect_on_error(struct gotd_client *client, const struct got_error *err)
375 struct imsgbuf ibuf;
377 log_warnx("uid %d: %s", client->euid, err->msg);
378 if (err->code != GOT_ERR_EOF) {
379 imsg_init(&ibuf, client->fd);
380 gotd_imsg_send_error(&ibuf, 0, PROC_GOTD, err);
381 imsg_clear(&ibuf);
383 disconnect(client);
386 static const struct got_error *
387 send_repo_info(struct gotd_imsgev *iev, struct gotd_repo *repo)
389 const struct got_error *err = NULL;
390 struct gotd_imsg_info_repo irepo;
392 memset(&irepo, 0, sizeof(irepo));
394 if (strlcpy(irepo.repo_name, repo->name, sizeof(irepo.repo_name))
395 >= sizeof(irepo.repo_name))
396 return got_error_msg(GOT_ERR_NO_SPACE, "repo name too long");
397 if (strlcpy(irepo.repo_path, repo->path, sizeof(irepo.repo_path))
398 >= sizeof(irepo.repo_path))
399 return got_error_msg(GOT_ERR_NO_SPACE, "repo path too long");
401 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_REPO, PROC_GOTD, -1,
402 &irepo, sizeof(irepo)) == -1) {
403 err = got_error_from_errno("imsg compose INFO_REPO");
404 if (err)
405 return err;
408 return NULL;
411 static const struct got_error *
412 send_capability(struct gotd_client_capability *capa, struct gotd_imsgev* iev)
414 const struct got_error *err = NULL;
415 struct gotd_imsg_capability icapa;
416 size_t len;
417 struct ibuf *wbuf;
419 memset(&icapa, 0, sizeof(icapa));
421 icapa.key_len = strlen(capa->key);
422 len = sizeof(icapa) + icapa.key_len;
423 if (capa->value) {
424 icapa.value_len = strlen(capa->value);
425 len += icapa.value_len;
428 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_CAPABILITY, 0, 0, len);
429 if (wbuf == NULL) {
430 err = got_error_from_errno("imsg_create CAPABILITY");
431 return err;
434 if (imsg_add(wbuf, &icapa, sizeof(icapa)) == -1)
435 return got_error_from_errno("imsg_add CAPABILITY");
436 if (imsg_add(wbuf, capa->key, icapa.key_len) == -1)
437 return got_error_from_errno("imsg_add CAPABILITY");
438 if (capa->value) {
439 if (imsg_add(wbuf, capa->value, icapa.value_len) == -1)
440 return got_error_from_errno("imsg_add CAPABILITY");
443 wbuf->fd = -1;
444 imsg_close(&iev->ibuf, wbuf);
446 gotd_imsg_event_add(iev);
448 return NULL;
451 static const struct got_error *
452 send_client_info(struct gotd_imsgev *iev, struct gotd_client *client)
454 const struct got_error *err = NULL;
455 struct gotd_imsg_info_client iclient;
456 struct gotd_child_proc *proc;
457 size_t i;
459 memset(&iclient, 0, sizeof(iclient));
460 iclient.euid = client->euid;
461 iclient.egid = client->egid;
463 proc = get_client_proc(client);
464 if (proc) {
465 if (strlcpy(iclient.repo_name, proc->chroot_path,
466 sizeof(iclient.repo_name)) >= sizeof(iclient.repo_name)) {
467 return got_error_msg(GOT_ERR_NO_SPACE,
468 "repo name too long");
470 if (client_is_writing(client))
471 iclient.is_writing = 1;
474 iclient.state = client->state;
475 iclient.ncapabilities = client->ncapabilities;
477 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_CLIENT, PROC_GOTD, -1,
478 &iclient, sizeof(iclient)) == -1) {
479 err = got_error_from_errno("imsg compose INFO_CLIENT");
480 if (err)
481 return err;
484 for (i = 0; i < client->ncapabilities; i++) {
485 struct gotd_client_capability *capa;
486 capa = &client->capabilities[i];
487 err = send_capability(capa, iev);
488 if (err)
489 return err;
492 return NULL;
495 static const struct got_error *
496 send_info(struct gotd_client *client)
498 const struct got_error *err = NULL;
499 struct gotd_imsg_info info;
500 uint64_t slot;
501 struct gotd_repo *repo;
503 info.pid = gotd.pid;
504 info.verbosity = gotd.verbosity;
505 info.nrepos = gotd.nrepos;
506 info.nclients = client_cnt - 1;
508 if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_INFO, PROC_GOTD, -1,
509 &info, sizeof(info)) == -1) {
510 err = got_error_from_errno("imsg compose INFO");
511 if (err)
512 return err;
515 TAILQ_FOREACH(repo, &gotd.repos, entry) {
516 err = send_repo_info(&client->iev, repo);
517 if (err)
518 return err;
521 for (slot = 0; slot < nitems(gotd_clients); slot++) {
522 struct gotd_client *c;
523 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
524 if (c->id == client->id)
525 continue;
526 err = send_client_info(&client->iev, c);
527 if (err)
528 return err;
532 return NULL;
535 static const struct got_error *
536 stop_gotd(struct gotd_client *client)
539 if (client->euid != 0)
540 return got_error_set_errno(EPERM, "stop");
542 gotd_shutdown();
543 /* NOTREACHED */
544 return NULL;
547 static struct gotd_repo *
548 find_repo_by_name(const char *repo_name)
550 struct gotd_repo *repo;
551 size_t namelen;
553 TAILQ_FOREACH(repo, &gotd.repos, entry) {
554 namelen = strlen(repo->name);
555 if (strncmp(repo->name, repo_name, namelen) != 0)
556 continue;
557 if (repo_name[namelen] == '\0' ||
558 strcmp(&repo_name[namelen], ".git") == 0)
559 return repo;
562 return NULL;
565 static struct gotd_child_proc *
566 find_proc_by_repo_name(enum gotd_procid proc_id, const char *repo_name)
568 struct gotd_child_proc *proc;
569 int i;
570 size_t namelen;
572 for (i = 0; i < gotd.nprocs; i++) {
573 proc = &gotd.procs[i];
574 if (proc->type != proc_id)
575 continue;
576 namelen = strlen(proc->repo_name);
577 if (strncmp(proc->repo_name, repo_name, namelen) != 0)
578 continue;
579 if (repo_name[namelen] == '\0' ||
580 strcmp(&repo_name[namelen], ".git") == 0)
581 return proc;
584 return NULL;
587 static struct gotd_child_proc *
588 find_proc_by_fd(int fd)
590 struct gotd_child_proc *proc;
591 int i;
593 for (i = 0; i < gotd.nprocs; i++) {
594 proc = &gotd.procs[i];
595 if (proc->iev.ibuf.fd == fd)
596 return proc;
599 return NULL;
602 static const struct got_error *
603 forward_list_refs_request(struct gotd_client *client, struct imsg *imsg)
605 const struct got_error *err;
606 struct gotd_imsg_list_refs ireq;
607 struct gotd_imsg_list_refs_internal ilref;
608 struct gotd_repo *repo = NULL;
609 struct gotd_child_proc *proc = NULL;
610 size_t datalen;
611 int fd = -1;
613 log_debug("list-refs request from uid %d", client->euid);
615 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
616 if (datalen != sizeof(ireq))
617 return got_error(GOT_ERR_PRIVSEP_LEN);
619 memcpy(&ireq, imsg->data, datalen);
621 memset(&ilref, 0, sizeof(ilref));
622 ilref.client_id = client->id;
624 if (ireq.client_is_reading) {
625 err = ensure_client_is_not_writing(client);
626 if (err)
627 return err;
628 repo = find_repo_by_name(ireq.repo_name);
629 if (repo == NULL)
630 return got_error(GOT_ERR_NOT_GIT_REPO);
631 err = gotd_auth_check(&repo->rules, repo->name,
632 client->euid, client->egid, GOTD_AUTH_READ);
633 if (err)
634 return err;
635 client->repo_read = find_proc_by_repo_name(PROC_REPO_READ,
636 ireq.repo_name);
637 if (client->repo_read == NULL)
638 return got_error(GOT_ERR_NOT_GIT_REPO);
639 } else {
640 err = ensure_client_is_not_reading(client);
641 if (err)
642 return err;
643 repo = find_repo_by_name(ireq.repo_name);
644 if (repo == NULL)
645 return got_error(GOT_ERR_NOT_GIT_REPO);
646 err = gotd_auth_check(&repo->rules, repo->name, client->euid,
647 client->egid, GOTD_AUTH_READ | GOTD_AUTH_WRITE);
648 if (err)
649 return err;
650 client->repo_write = find_proc_by_repo_name(PROC_REPO_WRITE,
651 ireq.repo_name);
652 if (client->repo_write == NULL)
653 return got_error(GOT_ERR_NOT_GIT_REPO);
656 fd = dup(client->fd);
657 if (fd == -1)
658 return got_error_from_errno("dup");
660 proc = get_client_proc(client);
661 if (proc == NULL)
662 fatalx("no process found for uid %d", client->euid);
663 if (gotd_imsg_compose_event(&proc->iev,
664 GOTD_IMSG_LIST_REFS_INTERNAL, PROC_GOTD, fd,
665 &ilref, sizeof(ilref)) == -1) {
666 err = got_error_from_errno("imsg compose WANT");
667 close(fd);
668 return err;
671 return NULL;
674 static const struct got_error *
675 forward_want(struct gotd_client *client, struct imsg *imsg)
677 struct gotd_imsg_want ireq;
678 struct gotd_imsg_want iwant;
679 size_t datalen;
681 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
682 if (datalen != sizeof(ireq))
683 return got_error(GOT_ERR_PRIVSEP_LEN);
685 memcpy(&ireq, imsg->data, datalen);
687 memset(&iwant, 0, sizeof(iwant));
688 memcpy(iwant.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
689 iwant.client_id = client->id;
691 if (gotd_imsg_compose_event(&client->repo_read->iev, GOTD_IMSG_WANT,
692 PROC_GOTD, -1, &iwant, sizeof(iwant)) == -1)
693 return got_error_from_errno("imsg compose WANT");
695 return NULL;
698 static const struct got_error *
699 forward_ref_update(struct gotd_client *client, struct imsg *imsg)
701 const struct got_error *err = NULL;
702 struct gotd_imsg_ref_update ireq;
703 struct gotd_imsg_ref_update *iref = NULL;
704 size_t datalen;
706 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
707 if (datalen < sizeof(ireq))
708 return got_error(GOT_ERR_PRIVSEP_LEN);
709 memcpy(&ireq, imsg->data, sizeof(ireq));
710 if (datalen != sizeof(ireq) + ireq.name_len)
711 return got_error(GOT_ERR_PRIVSEP_LEN);
713 iref = malloc(datalen);
714 if (iref == NULL)
715 return got_error_from_errno("malloc");
716 memcpy(iref, imsg->data, datalen);
718 iref->client_id = client->id;
719 if (gotd_imsg_compose_event(&client->repo_write->iev,
720 GOTD_IMSG_REF_UPDATE, PROC_GOTD, -1, iref, datalen) == -1)
721 err = got_error_from_errno("imsg compose REF_UPDATE");
722 free(iref);
723 return err;
726 static const struct got_error *
727 forward_have(struct gotd_client *client, struct imsg *imsg)
729 struct gotd_imsg_have ireq;
730 struct gotd_imsg_have ihave;
731 size_t datalen;
733 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
734 if (datalen != sizeof(ireq))
735 return got_error(GOT_ERR_PRIVSEP_LEN);
737 memcpy(&ireq, imsg->data, datalen);
739 memset(&ihave, 0, sizeof(ihave));
740 memcpy(ihave.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
741 ihave.client_id = client->id;
743 if (gotd_imsg_compose_event(&client->repo_read->iev, GOTD_IMSG_HAVE,
744 PROC_GOTD, -1, &ihave, sizeof(ihave)) == -1)
745 return got_error_from_errno("imsg compose HAVE");
747 return NULL;
750 static int
751 client_has_capability(struct gotd_client *client, const char *capastr)
753 struct gotd_client_capability *capa;
754 size_t i;
756 if (client->ncapabilities == 0)
757 return 0;
759 for (i = 0; i < client->ncapabilities; i++) {
760 capa = &client->capabilities[i];
761 if (strcmp(capa->key, capastr) == 0)
762 return 1;
765 return 0;
768 static const struct got_error *
769 recv_capabilities(struct gotd_client *client, struct imsg *imsg)
771 struct gotd_imsg_capabilities icapas;
772 size_t datalen;
774 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
775 if (datalen != sizeof(icapas))
776 return got_error(GOT_ERR_PRIVSEP_LEN);
777 memcpy(&icapas, imsg->data, sizeof(icapas));
779 client->ncapa_alloc = icapas.ncapabilities;
780 client->capabilities = calloc(client->ncapa_alloc,
781 sizeof(*client->capabilities));
782 if (client->capabilities == NULL) {
783 client->ncapa_alloc = 0;
784 return got_error_from_errno("calloc");
787 log_debug("expecting %zu capabilities from uid %d",
788 client->ncapa_alloc, client->euid);
789 return NULL;
792 static const struct got_error *
793 recv_capability(struct gotd_client *client, struct imsg *imsg)
795 struct gotd_imsg_capability icapa;
796 struct gotd_client_capability *capa;
797 size_t datalen;
798 char *key, *value = NULL;
800 if (client->capabilities == NULL ||
801 client->ncapabilities >= client->ncapa_alloc) {
802 return got_error_msg(GOT_ERR_BAD_REQUEST,
803 "unexpected capability received");
806 memset(&icapa, 0, sizeof(icapa));
808 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
809 if (datalen < sizeof(icapa))
810 return got_error(GOT_ERR_PRIVSEP_LEN);
811 memcpy(&icapa, imsg->data, sizeof(icapa));
813 if (datalen != sizeof(icapa) + icapa.key_len + icapa.value_len)
814 return got_error(GOT_ERR_PRIVSEP_LEN);
816 key = malloc(icapa.key_len + 1);
817 if (key == NULL)
818 return got_error_from_errno("malloc");
819 if (icapa.value_len > 0) {
820 value = malloc(icapa.value_len + 1);
821 if (value == NULL) {
822 free(key);
823 return got_error_from_errno("malloc");
827 memcpy(key, imsg->data + sizeof(icapa), icapa.key_len);
828 key[icapa.key_len] = '\0';
829 if (value) {
830 memcpy(value, imsg->data + sizeof(icapa) + icapa.key_len,
831 icapa.value_len);
832 value[icapa.value_len] = '\0';
835 capa = &client->capabilities[client->ncapabilities++];
836 capa->key = key;
837 capa->value = value;
839 if (value)
840 log_debug("uid %d: capability %s=%s", client->euid, key, value);
841 else
842 log_debug("uid %d: capability %s", client->euid, key);
844 return NULL;
847 static const struct got_error *
848 send_packfile(struct gotd_client *client)
850 const struct got_error *err = NULL;
851 struct gotd_imsg_send_packfile ipack;
852 struct gotd_imsg_packfile_pipe ipipe;
853 int pipe[2];
855 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
856 return got_error_from_errno("socketpair");
858 memset(&ipack, 0, sizeof(ipack));
859 memset(&ipipe, 0, sizeof(ipipe));
861 ipack.client_id = client->id;
862 if (client_has_capability(client, GOT_CAPA_SIDE_BAND_64K))
863 ipack.report_progress = 1;
865 client->delta_cache_fd = got_opentempfd();
866 if (client->delta_cache_fd == -1)
867 return got_error_from_errno("got_opentempfd");
869 if (gotd_imsg_compose_event(&client->repo_read->iev,
870 GOTD_IMSG_SEND_PACKFILE, PROC_GOTD, client->delta_cache_fd,
871 &ipack, sizeof(ipack)) == -1) {
872 err = got_error_from_errno("imsg compose SEND_PACKFILE");
873 close(pipe[0]);
874 close(pipe[1]);
875 return err;
878 ipipe.client_id = client->id;
880 /* Send pack pipe end 0 to repo_read. */
881 if (gotd_imsg_compose_event(&client->repo_read->iev,
882 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[0],
883 &ipipe, sizeof(ipipe)) == -1) {
884 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
885 close(pipe[1]);
886 return err;
889 /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
890 if (gotd_imsg_compose_event(&client->iev,
891 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1], NULL, 0) == -1)
892 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
894 return err;
897 static const struct got_error *
898 recv_packfile(struct gotd_client *client)
900 const struct got_error *err = NULL;
901 struct gotd_imsg_recv_packfile ipack;
902 struct gotd_imsg_packfile_pipe ipipe;
903 struct gotd_imsg_packidx_file ifile;
904 char *basepath = NULL, *pack_path = NULL, *idx_path = NULL;
905 int packfd = -1, idxfd = -1;
906 int pipe[2] = { -1, -1 };
908 if (client->packfile_path) {
909 return got_error_fmt(GOT_ERR_PRIVSEP_MSG,
910 "uid %d already has a pack file", client->euid);
913 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
914 return got_error_from_errno("socketpair");
916 memset(&ipipe, 0, sizeof(ipipe));
917 ipipe.client_id = client->id;
919 if (gotd_imsg_compose_event(&client->repo_write->iev,
920 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[0],
921 &ipipe, sizeof(ipipe)) == -1) {
922 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
923 pipe[0] = -1;
924 goto done;
926 pipe[0] = -1;
928 if (gotd_imsg_compose_event(&client->repo_write->iev,
929 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1],
930 &ipipe, sizeof(ipipe)) == -1)
931 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
932 pipe[1] = -1;
934 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.pack",
935 client->repo_write->chroot_path, GOT_OBJECTS_PACK_DIR,
936 client->euid) == -1) {
937 err = got_error_from_errno("asprintf");
938 goto done;
941 err = got_opentemp_named_fd(&pack_path, &packfd, basepath, "");
942 if (err)
943 goto done;
945 free(basepath);
946 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.idx",
947 client->repo_write->chroot_path, GOT_OBJECTS_PACK_DIR,
948 client->euid) == -1) {
949 err = got_error_from_errno("asprintf");
950 basepath = NULL;
951 goto done;
953 err = got_opentemp_named_fd(&idx_path, &idxfd, basepath, "");
954 if (err)
955 goto done;
957 memset(&ifile, 0, sizeof(ifile));
958 ifile.client_id = client->id;
959 if (gotd_imsg_compose_event(&client->repo_write->iev,
960 GOTD_IMSG_PACKIDX_FILE, PROC_GOTD, idxfd,
961 &ifile, sizeof(ifile)) == -1) {
962 err = got_error_from_errno("imsg compose PACKIDX_FILE");
963 idxfd = -1;
964 goto done;
966 idxfd = -1;
968 memset(&ipack, 0, sizeof(ipack));
969 ipack.client_id = client->id;
970 if (client_has_capability(client, GOT_CAPA_REPORT_STATUS))
971 ipack.report_status = 1;
973 if (gotd_imsg_compose_event(&client->repo_write->iev,
974 GOTD_IMSG_RECV_PACKFILE, PROC_GOTD, packfd,
975 &ipack, sizeof(ipack)) == -1) {
976 err = got_error_from_errno("imsg compose RECV_PACKFILE");
977 packfd = -1;
978 goto done;
980 packfd = -1;
982 done:
983 free(basepath);
984 if (pipe[0] != -1 && close(pipe[0]) == -1 && err == NULL)
985 err = got_error_from_errno("close");
986 if (pipe[1] != -1 && close(pipe[1]) == -1 && err == NULL)
987 err = got_error_from_errno("close");
988 if (packfd != -1 && close(packfd) == -1 && err == NULL)
989 err = got_error_from_errno("close");
990 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
991 err = got_error_from_errno("close");
992 if (err) {
993 free(pack_path);
994 free(idx_path);
995 } else {
996 client->packfile_path = pack_path;
997 client->packidx_path = idx_path;
999 return err;
1002 static void
1003 gotd_request(int fd, short events, void *arg)
1005 struct gotd_imsgev *iev = arg;
1006 struct imsgbuf *ibuf = &iev->ibuf;
1007 struct gotd_client *client = iev->handler_arg;
1008 const struct got_error *err = NULL;
1009 struct imsg imsg;
1010 ssize_t n;
1012 if (events & EV_WRITE) {
1013 while (ibuf->w.queued) {
1014 n = msgbuf_write(&ibuf->w);
1015 if (n == -1 && errno == EPIPE) {
1017 * The client has closed its socket.
1018 * This can happen when Git clients are
1019 * done sending pack file data.
1021 msgbuf_clear(&ibuf->w);
1022 continue;
1023 } else if (n == -1 && errno != EAGAIN) {
1024 err = got_error_from_errno("imsg_flush");
1025 disconnect_on_error(client, err);
1026 return;
1028 if (n == 0) {
1029 /* Connection closed. */
1030 err = got_error(GOT_ERR_EOF);
1031 disconnect_on_error(client, err);
1032 return;
1036 /* Disconnect gotctl(8) now that messages have been sent. */
1037 if (!client_is_reading(client) && !client_is_writing(client)) {
1038 disconnect(client);
1039 return;
1043 if ((events & EV_READ) == 0)
1044 return;
1046 memset(&imsg, 0, sizeof(imsg));
1048 while (err == NULL) {
1049 err = gotd_imsg_recv(&imsg, ibuf, 0);
1050 if (err) {
1051 if (err->code == GOT_ERR_PRIVSEP_READ)
1052 err = NULL;
1053 break;
1056 evtimer_del(&client->tmo);
1058 switch (imsg.hdr.type) {
1059 case GOTD_IMSG_INFO:
1060 err = send_info(client);
1061 break;
1062 case GOTD_IMSG_STOP:
1063 err = stop_gotd(client);
1064 break;
1065 case GOTD_IMSG_LIST_REFS:
1066 if (client->state != GOTD_STATE_EXPECT_LIST_REFS) {
1067 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1068 "unexpected list-refs request received");
1069 break;
1071 err = forward_list_refs_request(client, &imsg);
1072 if (err)
1073 break;
1074 client->state = GOTD_STATE_EXPECT_CAPABILITIES;
1075 log_debug("uid %d: expecting capabilities",
1076 client->euid);
1077 break;
1078 case GOTD_IMSG_CAPABILITIES:
1079 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1080 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1081 "unexpected capabilities received");
1082 break;
1084 log_debug("receiving capabilities from uid %d",
1085 client->euid);
1086 err = recv_capabilities(client, &imsg);
1087 break;
1088 case GOTD_IMSG_CAPABILITY:
1089 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1090 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1091 "unexpected capability received");
1092 break;
1094 err = recv_capability(client, &imsg);
1095 if (err || client->ncapabilities < client->ncapa_alloc)
1096 break;
1097 if (client_is_reading(client)) {
1098 client->state = GOTD_STATE_EXPECT_WANT;
1099 log_debug("uid %d: expecting want-lines",
1100 client->euid);
1101 } else if (client_is_writing(client)) {
1102 client->state = GOTD_STATE_EXPECT_REF_UPDATE;
1103 log_debug("uid %d: expecting ref-update-lines",
1104 client->euid);
1105 } else
1106 fatalx("client %d is both reading and writing",
1107 client->euid);
1108 break;
1109 case GOTD_IMSG_WANT:
1110 if (client->state != GOTD_STATE_EXPECT_WANT) {
1111 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1112 "unexpected want-line received");
1113 break;
1115 log_debug("received want-line from uid %d",
1116 client->euid);
1117 err = ensure_client_is_reading(client);
1118 if (err)
1119 break;
1120 err = forward_want(client, &imsg);
1121 break;
1122 case GOTD_IMSG_REF_UPDATE:
1123 if (client->state != GOTD_STATE_EXPECT_REF_UPDATE) {
1124 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1125 "unexpected ref-update-line received");
1126 break;
1128 log_debug("received ref-update-line from uid %d",
1129 client->euid);
1130 err = ensure_client_is_writing(client);
1131 if (err)
1132 break;
1133 err = forward_ref_update(client, &imsg);
1134 if (err)
1135 break;
1136 client->state = GOTD_STATE_EXPECT_MORE_REF_UPDATES;
1137 break;
1138 case GOTD_IMSG_HAVE:
1139 if (client->state != GOTD_STATE_EXPECT_HAVE) {
1140 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1141 "unexpected have-line received");
1142 break;
1144 log_debug("received have-line from uid %d",
1145 client->euid);
1146 err = ensure_client_is_reading(client);
1147 if (err)
1148 break;
1149 err = forward_have(client, &imsg);
1150 if (err)
1151 break;
1152 break;
1153 case GOTD_IMSG_FLUSH:
1154 if (client->state == GOTD_STATE_EXPECT_WANT ||
1155 client->state == GOTD_STATE_EXPECT_HAVE) {
1156 err = ensure_client_is_reading(client);
1157 if (err)
1158 break;
1159 } else if (client->state ==
1160 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1161 err = ensure_client_is_writing(client);
1162 if (err)
1163 break;
1164 } else {
1165 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1166 "unexpected flush-pkt received");
1167 break;
1169 log_debug("received flush-pkt from uid %d",
1170 client->euid);
1171 if (client->state == GOTD_STATE_EXPECT_WANT) {
1172 client->state = GOTD_STATE_EXPECT_HAVE;
1173 log_debug("uid %d: expecting have-lines",
1174 client->euid);
1175 } else if (client->state == GOTD_STATE_EXPECT_HAVE) {
1176 client->state = GOTD_STATE_EXPECT_DONE;
1177 log_debug("uid %d: expecting 'done'",
1178 client->euid);
1179 } else if (client->state ==
1180 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1181 client->state = GOTD_STATE_EXPECT_PACKFILE;
1182 log_debug("uid %d: expecting packfile",
1183 client->euid);
1184 err = recv_packfile(client);
1185 } else {
1186 /* should not happen, see above */
1187 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1188 "unexpected client state");
1189 break;
1191 break;
1192 case GOTD_IMSG_DONE:
1193 if (client->state != GOTD_STATE_EXPECT_HAVE &&
1194 client->state != GOTD_STATE_EXPECT_DONE) {
1195 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1196 "unexpected flush-pkt received");
1197 break;
1199 log_debug("received 'done' from uid %d", client->euid);
1200 err = ensure_client_is_reading(client);
1201 if (err)
1202 break;
1203 client->state = GOTD_STATE_DONE;
1204 err = send_packfile(client);
1205 break;
1206 default:
1207 err = got_error(GOT_ERR_PRIVSEP_MSG);
1208 break;
1211 imsg_free(&imsg);
1214 if (err) {
1215 if (err->code != GOT_ERR_EOF ||
1216 client->state != GOTD_STATE_EXPECT_PACKFILE)
1217 disconnect_on_error(client, err);
1218 } else {
1219 gotd_imsg_event_add(&client->iev);
1220 evtimer_add(&client->tmo, &timeout);
1224 static void
1225 gotd_request_timeout(int fd, short events, void *arg)
1227 struct gotd_client *client = arg;
1229 log_debug("disconnecting uid %d due to timeout", client->euid);
1230 disconnect(client);
1233 static void
1234 gotd_accept(int fd, short event, void *arg)
1236 struct sockaddr_storage ss;
1237 struct timeval backoff;
1238 socklen_t len;
1239 int s = -1;
1240 struct gotd_client *client = NULL;
1241 uid_t euid;
1242 gid_t egid;
1244 backoff.tv_sec = 1;
1245 backoff.tv_usec = 0;
1247 if (event_add(&gotd.ev, NULL) == -1) {
1248 log_warn("event_add");
1249 return;
1251 if (event & EV_TIMEOUT)
1252 return;
1254 len = sizeof(ss);
1256 s = accept_reserve(fd, (struct sockaddr *)&ss, &len, GOTD_FD_RESERVE,
1257 &inflight);
1259 if (s == -1) {
1260 switch (errno) {
1261 case EINTR:
1262 case EWOULDBLOCK:
1263 case ECONNABORTED:
1264 return;
1265 case EMFILE:
1266 case ENFILE:
1267 event_del(&gotd.ev);
1268 evtimer_add(&gotd.pause, &backoff);
1269 return;
1270 default:
1271 log_warn("%s: accept", __func__);
1272 return;
1276 if (client_cnt >= GOTD_MAXCLIENTS)
1277 goto err;
1279 if (getpeereid(s, &euid, &egid) == -1) {
1280 log_warn("%s: getpeereid", __func__);
1281 goto err;
1284 client = calloc(1, sizeof(*client));
1285 if (client == NULL) {
1286 log_warn("%s: calloc", __func__);
1287 goto err;
1290 client->state = GOTD_STATE_EXPECT_LIST_REFS;
1291 client->id = get_client_id();
1292 client->fd = s;
1293 s = -1;
1294 client->delta_cache_fd = -1;
1295 client->euid = euid;
1296 client->egid = egid;
1297 client->nref_updates = -1;
1299 imsg_init(&client->iev.ibuf, client->fd);
1300 client->iev.handler = gotd_request;
1301 client->iev.events = EV_READ;
1302 client->iev.handler_arg = client;
1304 event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
1305 &client->iev);
1306 gotd_imsg_event_add(&client->iev);
1308 evtimer_set(&client->tmo, gotd_request_timeout, client);
1310 add_client(client);
1311 log_debug("%s: new client uid %d connected on fd %d", __func__,
1312 client->euid, client->fd);
1313 return;
1314 err:
1315 inflight--;
1316 if (s != -1)
1317 close(s);
1318 free(client);
1321 static void
1322 gotd_accept_paused(int fd, short event, void *arg)
1324 event_add(&gotd.ev, NULL);
1327 static const char *gotd_proc_names[PROC_MAX] = {
1328 "parent",
1329 "repo_read",
1330 "repo_write"
1333 static struct gotd_child_proc *
1334 get_proc_for_pid(pid_t pid)
1336 struct gotd_child_proc *proc;
1337 int i;
1339 for (i = 0; i < gotd.nprocs; i++) {
1340 proc = &gotd.procs[i];
1341 if (proc->pid == pid)
1342 return proc;
1345 return NULL;
1348 static void
1349 kill_proc(struct gotd_child_proc *proc, int fatal)
1351 if (fatal) {
1352 log_warnx("sending SIGKILL to PID %d", proc->pid);
1353 kill(proc->pid, SIGKILL);
1354 } else
1355 kill(proc->pid, SIGTERM);
1358 static void
1359 gotd_shutdown(void)
1361 pid_t pid;
1362 int status, i;
1363 struct gotd_child_proc *proc;
1365 for (i = 0; i < gotd.nprocs; i++) {
1366 proc = &gotd.procs[i];
1367 msgbuf_clear(&proc->iev.ibuf.w);
1368 close(proc->iev.ibuf.fd);
1369 kill_proc(proc, 0);
1372 log_debug("waiting for children to terminate");
1373 do {
1374 pid = wait(&status);
1375 if (pid == -1) {
1376 if (errno != EINTR && errno != ECHILD)
1377 fatal("wait");
1378 } else if (WIFSIGNALED(status)) {
1379 proc = get_proc_for_pid(pid);
1380 log_warnx("%s %s child process terminated; signal %d",
1381 proc ? gotd_proc_names[proc->type] : "",
1382 proc ? proc->chroot_path : "", WTERMSIG(status));
1384 } while (pid != -1 || (pid == -1 && errno == EINTR));
1386 log_info("terminating");
1387 exit(0);
1390 void
1391 gotd_sighdlr(int sig, short event, void *arg)
1394 * Normal signal handler rules don't apply because libevent
1395 * decouples for us.
1398 switch (sig) {
1399 case SIGHUP:
1400 log_info("%s: ignoring SIGHUP", __func__);
1401 break;
1402 case SIGUSR1:
1403 log_info("%s: ignoring SIGUSR1", __func__);
1404 break;
1405 case SIGTERM:
1406 case SIGINT:
1407 gotd_shutdown();
1408 log_warnx("gotd terminating");
1409 exit(0);
1410 break;
1411 default:
1412 fatalx("unexpected signal");
1416 static const struct got_error *
1417 ensure_proc_is_reading(struct gotd_client *client,
1418 struct gotd_child_proc *proc)
1420 if (!client_is_reading(client)) {
1421 kill_proc(proc, 1);
1422 return got_error_fmt(GOT_ERR_BAD_PACKET,
1423 "PID %d handled a read-request for uid %d but this "
1424 "user is not reading from a repository", proc->pid,
1425 client->euid);
1428 return NULL;
1431 static const struct got_error *
1432 ensure_proc_is_writing(struct gotd_client *client,
1433 struct gotd_child_proc *proc)
1435 if (!client_is_writing(client)) {
1436 kill_proc(proc, 1);
1437 return got_error_fmt(GOT_ERR_BAD_PACKET,
1438 "PID %d handled a write-request for uid %d but this "
1439 "user is not writing to a repository", proc->pid,
1440 client->euid);
1443 return NULL;
1446 static int
1447 verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
1448 struct imsg *imsg)
1450 const struct got_error *err;
1451 struct gotd_child_proc *client_proc;
1452 int ret = 0;
1454 client_proc = get_client_proc(client);
1455 if (client_proc == NULL)
1456 fatalx("no process found for uid %d", client->euid);
1458 if (proc->pid != client_proc->pid) {
1459 kill_proc(proc, 1);
1460 log_warnx("received message from PID %d for uid %d, while "
1461 "PID %d is the process serving this user",
1462 proc->pid, client->euid, client_proc->pid);
1463 return 0;
1466 switch (imsg->hdr.type) {
1467 case GOTD_IMSG_ERROR:
1468 ret = 1;
1469 break;
1470 case GOTD_IMSG_PACKFILE_DONE:
1471 err = ensure_proc_is_reading(client, proc);
1472 if (err)
1473 log_warnx("uid %d: %s", client->euid, err->msg);
1474 else
1475 ret = 1;
1476 break;
1477 case GOTD_IMSG_PACKFILE_INSTALL:
1478 case GOTD_IMSG_REF_UPDATES_START:
1479 case GOTD_IMSG_REF_UPDATE:
1480 err = ensure_proc_is_writing(client, proc);
1481 if (err)
1482 log_warnx("uid %d: %s", client->euid, err->msg);
1483 else
1484 ret = 1;
1485 break;
1486 default:
1487 log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
1488 break;
1491 return ret;
1494 static const struct got_error *
1495 recv_packfile_done(uint32_t *client_id, struct imsg *imsg)
1497 struct gotd_imsg_packfile_done idone;
1498 size_t datalen;
1500 log_debug("packfile-done received");
1502 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1503 if (datalen != sizeof(idone))
1504 return got_error(GOT_ERR_PRIVSEP_LEN);
1505 memcpy(&idone, imsg->data, sizeof(idone));
1507 *client_id = idone.client_id;
1508 return NULL;
1511 static const struct got_error *
1512 recv_packfile_install(uint32_t *client_id, struct imsg *imsg)
1514 struct gotd_imsg_packfile_install inst;
1515 size_t datalen;
1517 log_debug("packfile-install received");
1519 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1520 if (datalen != sizeof(inst))
1521 return got_error(GOT_ERR_PRIVSEP_LEN);
1522 memcpy(&inst, imsg->data, sizeof(inst));
1524 *client_id = inst.client_id;
1525 return NULL;
1528 static const struct got_error *
1529 recv_ref_updates_start(uint32_t *client_id, struct imsg *imsg)
1531 struct gotd_imsg_ref_updates_start istart;
1532 size_t datalen;
1534 log_debug("ref-updates-start received");
1536 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1537 if (datalen != sizeof(istart))
1538 return got_error(GOT_ERR_PRIVSEP_LEN);
1539 memcpy(&istart, imsg->data, sizeof(istart));
1541 *client_id = istart.client_id;
1542 return NULL;
1545 static const struct got_error *
1546 recv_ref_update(uint32_t *client_id, struct imsg *imsg)
1548 struct gotd_imsg_ref_update iref;
1549 size_t datalen;
1551 log_debug("ref-update received");
1553 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1554 if (datalen < sizeof(iref))
1555 return got_error(GOT_ERR_PRIVSEP_LEN);
1556 memcpy(&iref, imsg->data, sizeof(iref));
1558 *client_id = iref.client_id;
1559 return NULL;
1562 static const struct got_error *
1563 send_ref_update_ok(struct gotd_client *client,
1564 struct gotd_imsg_ref_update *iref, const char *refname)
1566 struct gotd_imsg_ref_update_ok iok;
1567 struct ibuf *wbuf;
1568 size_t len;
1570 memset(&iok, 0, sizeof(iok));
1571 iok.client_id = client->id;
1572 memcpy(iok.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1573 memcpy(iok.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1574 iok.name_len = strlen(refname);
1576 len = sizeof(iok) + iok.name_len;
1577 wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_OK,
1578 PROC_GOTD, gotd.pid, len);
1579 if (wbuf == NULL)
1580 return got_error_from_errno("imsg_create REF_UPDATE_OK");
1582 if (imsg_add(wbuf, &iok, sizeof(iok)) == -1)
1583 return got_error_from_errno("imsg_add REF_UPDATE_OK");
1584 if (imsg_add(wbuf, refname, iok.name_len) == -1)
1585 return got_error_from_errno("imsg_add REF_UPDATE_OK");
1587 wbuf->fd = -1;
1588 imsg_close(&client->iev.ibuf, wbuf);
1589 gotd_imsg_event_add(&client->iev);
1590 return NULL;
1593 static void
1594 send_refs_updated(struct gotd_client *client)
1596 if (gotd_imsg_compose_event(&client->iev,
1597 GOTD_IMSG_REFS_UPDATED, PROC_GOTD, -1, NULL, 0) == -1)
1598 log_warn("imsg compose REFS_UPDATED");
1601 static const struct got_error *
1602 send_ref_update_ng(struct gotd_client *client,
1603 struct gotd_imsg_ref_update *iref, const char *refname,
1604 const char *reason)
1606 const struct got_error *ng_err;
1607 struct gotd_imsg_ref_update_ng ing;
1608 struct ibuf *wbuf;
1609 size_t len;
1611 memset(&ing, 0, sizeof(ing));
1612 ing.client_id = client->id;
1613 memcpy(ing.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1614 memcpy(ing.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1615 ing.name_len = strlen(refname);
1617 ng_err = got_error_fmt(GOT_ERR_REF_BUSY, "%s", reason);
1618 ing.reason_len = strlen(ng_err->msg);
1620 len = sizeof(ing) + ing.name_len + ing.reason_len;
1621 wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_NG,
1622 PROC_GOTD, gotd.pid, len);
1623 if (wbuf == NULL)
1624 return got_error_from_errno("imsg_create REF_UPDATE_NG");
1626 if (imsg_add(wbuf, &ing, sizeof(ing)) == -1)
1627 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1628 if (imsg_add(wbuf, refname, ing.name_len) == -1)
1629 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1630 if (imsg_add(wbuf, ng_err->msg, ing.reason_len) == -1)
1631 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1633 wbuf->fd = -1;
1634 imsg_close(&client->iev.ibuf, wbuf);
1635 gotd_imsg_event_add(&client->iev);
1636 return NULL;
1639 static const struct got_error *
1640 install_pack(struct gotd_client *client, const char *repo_path,
1641 struct imsg *imsg)
1643 const struct got_error *err = NULL;
1644 struct gotd_imsg_packfile_install inst;
1645 char hex[SHA1_DIGEST_STRING_LENGTH];
1646 size_t datalen;
1647 char *packfile_path = NULL, *packidx_path = NULL;
1649 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1650 if (datalen != sizeof(inst))
1651 return got_error(GOT_ERR_PRIVSEP_LEN);
1652 memcpy(&inst, imsg->data, sizeof(inst));
1654 if (client->packfile_path == NULL)
1655 return got_error_msg(GOT_ERR_BAD_REQUEST,
1656 "client has no pack file");
1657 if (client->packidx_path == NULL)
1658 return got_error_msg(GOT_ERR_BAD_REQUEST,
1659 "client has no pack file index");
1661 if (got_sha1_digest_to_str(inst.pack_sha1, hex, sizeof(hex)) == NULL)
1662 return got_error_msg(GOT_ERR_NO_SPACE,
1663 "could not convert pack file SHA1 to hex");
1665 if (asprintf(&packfile_path, "/%s/%s/pack-%s.pack",
1666 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1667 err = got_error_from_errno("asprintf");
1668 goto done;
1671 if (asprintf(&packidx_path, "/%s/%s/pack-%s.idx",
1672 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1673 err = got_error_from_errno("asprintf");
1674 goto done;
1677 if (rename(client->packfile_path, packfile_path) == -1) {
1678 err = got_error_from_errno3("rename", client->packfile_path,
1679 packfile_path);
1680 goto done;
1683 free(client->packfile_path);
1684 client->packfile_path = NULL;
1686 if (rename(client->packidx_path, packidx_path) == -1) {
1687 err = got_error_from_errno3("rename", client->packidx_path,
1688 packidx_path);
1689 goto done;
1692 free(client->packidx_path);
1693 client->packidx_path = NULL;
1694 done:
1695 free(packfile_path);
1696 free(packidx_path);
1697 return err;
1700 static const struct got_error *
1701 begin_ref_updates(struct gotd_client *client, struct imsg *imsg)
1703 struct gotd_imsg_ref_updates_start istart;
1704 size_t datalen;
1706 if (client->nref_updates != -1)
1707 return got_error(GOT_ERR_PRIVSEP_MSG);
1709 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1710 if (datalen != sizeof(istart))
1711 return got_error(GOT_ERR_PRIVSEP_LEN);
1712 memcpy(&istart, imsg->data, sizeof(istart));
1714 if (istart.nref_updates <= 0)
1715 return got_error(GOT_ERR_PRIVSEP_MSG);
1717 client->nref_updates = istart.nref_updates;
1718 return NULL;
1721 static const struct got_error *
1722 update_ref(struct gotd_client *client, const char *repo_path,
1723 struct imsg *imsg)
1725 const struct got_error *err = NULL;
1726 struct got_repository *repo = NULL;
1727 struct got_reference *ref = NULL;
1728 struct gotd_imsg_ref_update iref;
1729 struct got_object_id old_id, new_id;
1730 struct got_object_id *id = NULL;
1731 struct got_object *obj = NULL;
1732 char *refname = NULL;
1733 size_t datalen;
1734 int locked = 0;
1736 log_debug("update-ref from uid %d", client->euid);
1738 if (client->nref_updates <= 0)
1739 return got_error(GOT_ERR_PRIVSEP_MSG);
1741 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1742 if (datalen < sizeof(iref))
1743 return got_error(GOT_ERR_PRIVSEP_LEN);
1744 memcpy(&iref, imsg->data, sizeof(iref));
1745 if (datalen != sizeof(iref) + iref.name_len)
1746 return got_error(GOT_ERR_PRIVSEP_LEN);
1747 refname = malloc(iref.name_len + 1);
1748 if (refname == NULL)
1749 return got_error_from_errno("malloc");
1750 memcpy(refname, imsg->data + sizeof(iref), iref.name_len);
1751 refname[iref.name_len] = '\0';
1753 log_debug("updating ref %s for uid %d", refname, client->euid);
1755 err = got_repo_open(&repo, repo_path, NULL, NULL);
1756 if (err)
1757 goto done;
1759 memcpy(old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
1760 memcpy(new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
1761 err = got_object_open(&obj, repo, &new_id);
1762 if (err)
1763 goto done;
1765 if (iref.ref_is_new) {
1766 err = got_ref_open(&ref, repo, refname, 0);
1767 if (err) {
1768 if (err->code != GOT_ERR_NOT_REF)
1769 goto done;
1770 err = got_ref_alloc(&ref, refname, &new_id);
1771 if (err)
1772 goto done;
1773 err = got_ref_write(ref, repo); /* will lock/unlock */
1774 if (err)
1775 goto done;
1776 } else {
1777 err = got_error_fmt(GOT_ERR_REF_BUSY,
1778 "%s has been created by someone else "
1779 "while transaction was in progress",
1780 got_ref_get_name(ref));
1781 goto done;
1783 } else {
1784 err = got_ref_open(&ref, repo, refname, 1 /* lock */);
1785 if (err)
1786 goto done;
1787 locked = 1;
1789 err = got_ref_resolve(&id, repo, ref);
1790 if (err)
1791 goto done;
1793 if (got_object_id_cmp(id, &old_id) != 0) {
1794 err = got_error_fmt(GOT_ERR_REF_BUSY,
1795 "%s has been modified by someone else "
1796 "while transaction was in progress",
1797 got_ref_get_name(ref));
1798 goto done;
1801 err = got_ref_change_ref(ref, &new_id);
1802 if (err)
1803 goto done;
1805 err = got_ref_write(ref, repo);
1806 if (err)
1807 goto done;
1809 free(id);
1810 id = NULL;
1812 done:
1813 if (err) {
1814 if (err->code == GOT_ERR_LOCKFILE_TIMEOUT) {
1815 err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
1816 "could not acquire exclusive file lock for %s",
1817 refname);
1819 send_ref_update_ng(client, &iref, refname, err->msg);
1820 } else
1821 send_ref_update_ok(client, &iref, refname);
1823 if (client->nref_updates > 0) {
1824 client->nref_updates--;
1825 if (client->nref_updates == 0)
1826 send_refs_updated(client);
1829 if (locked) {
1830 const struct got_error *unlock_err;
1831 unlock_err = got_ref_unlock(ref);
1832 if (unlock_err && err == NULL)
1833 err = unlock_err;
1835 if (ref)
1836 got_ref_close(ref);
1837 if (obj)
1838 got_object_close(obj);
1839 if (repo)
1840 got_repo_close(repo);
1841 free(refname);
1842 free(id);
1843 return err;
1846 static void
1847 gotd_dispatch(int fd, short event, void *arg)
1849 struct gotd_imsgev *iev = arg;
1850 struct imsgbuf *ibuf = &iev->ibuf;
1851 struct gotd_child_proc *proc = NULL;
1852 ssize_t n;
1853 int shut = 0;
1854 struct imsg imsg;
1856 if (event & EV_READ) {
1857 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1858 fatal("imsg_read error");
1859 if (n == 0) {
1860 /* Connection closed. */
1861 shut = 1;
1862 goto done;
1866 if (event & EV_WRITE) {
1867 n = msgbuf_write(&ibuf->w);
1868 if (n == -1 && errno != EAGAIN)
1869 fatal("msgbuf_write");
1870 if (n == 0) {
1871 /* Connection closed. */
1872 shut = 1;
1873 goto done;
1877 proc = find_proc_by_fd(fd);
1878 if (proc == NULL)
1879 fatalx("cannot find child process for fd %d", fd);
1881 for (;;) {
1882 const struct got_error *err = NULL;
1883 struct gotd_client *client = NULL;
1884 uint32_t client_id = 0;
1885 int do_disconnect = 0;
1886 int do_ref_updates = 0, do_ref_update = 0;
1887 int do_packfile_install = 0;
1889 if ((n = imsg_get(ibuf, &imsg)) == -1)
1890 fatal("%s: imsg_get error", __func__);
1891 if (n == 0) /* No more messages. */
1892 break;
1894 switch (imsg.hdr.type) {
1895 case GOTD_IMSG_ERROR:
1896 do_disconnect = 1;
1897 err = gotd_imsg_recv_error(&client_id, &imsg);
1898 break;
1899 case GOTD_IMSG_PACKFILE_DONE:
1900 do_disconnect = 1;
1901 err = recv_packfile_done(&client_id, &imsg);
1902 break;
1903 case GOTD_IMSG_PACKFILE_INSTALL:
1904 err = recv_packfile_install(&client_id, &imsg);
1905 if (err == NULL)
1906 do_packfile_install = 1;
1907 break;
1908 case GOTD_IMSG_REF_UPDATES_START:
1909 err = recv_ref_updates_start(&client_id, &imsg);
1910 if (err == NULL)
1911 do_ref_updates = 1;
1912 break;
1913 case GOTD_IMSG_REF_UPDATE:
1914 err = recv_ref_update(&client_id, &imsg);
1915 if (err == NULL)
1916 do_ref_update = 1;
1917 break;
1918 default:
1919 log_debug("unexpected imsg %d", imsg.hdr.type);
1920 break;
1923 client = find_client(client_id);
1924 if (client == NULL) {
1925 log_warnx("%s: client not found", __func__);
1926 imsg_free(&imsg);
1927 continue;
1930 if (!verify_imsg_src(client, proc, &imsg)) {
1931 log_debug("dropping imsg type %d from PID %d",
1932 imsg.hdr.type, proc->pid);
1933 imsg_free(&imsg);
1934 continue;
1936 if (err)
1937 log_warnx("uid %d: %s", client->euid, err->msg);
1939 if (do_disconnect) {
1940 if (err)
1941 disconnect_on_error(client, err);
1942 else
1943 disconnect(client);
1944 } else {
1945 if (do_packfile_install)
1946 err = install_pack(client, proc->chroot_path,
1947 &imsg);
1948 else if (do_ref_updates)
1949 err = begin_ref_updates(client, &imsg);
1950 else if (do_ref_update)
1951 err = update_ref(client, proc->chroot_path,
1952 &imsg);
1953 if (err)
1954 log_warnx("uid %d: %s", client->euid, err->msg);
1956 imsg_free(&imsg);
1958 done:
1959 if (!shut) {
1960 gotd_imsg_event_add(iev);
1961 } else {
1962 /* This pipe is dead. Remove its event handler */
1963 event_del(&iev->ev);
1964 event_loopexit(NULL);
1968 static pid_t
1969 start_child(enum gotd_procid proc_id, const char *chroot_path,
1970 char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
1972 char *argv[11];
1973 int argc = 0;
1974 pid_t pid;
1976 switch (pid = fork()) {
1977 case -1:
1978 fatal("cannot fork");
1979 case 0:
1980 break;
1981 default:
1982 close(fd);
1983 return pid;
1986 if (fd != GOTD_FILENO_MSG_PIPE) {
1987 if (dup2(fd, GOTD_FILENO_MSG_PIPE) == -1)
1988 fatal("cannot setup imsg fd");
1989 } else if (fcntl(fd, F_SETFD, 0) == -1)
1990 fatal("cannot setup imsg fd");
1992 argv[argc++] = argv0;
1993 switch (proc_id) {
1994 case PROC_REPO_READ:
1995 argv[argc++] = (char *)"-R";
1996 break;
1997 case PROC_REPO_WRITE:
1998 argv[argc++] = (char *)"-W";
1999 break;
2000 default:
2001 fatalx("invalid process id %d", proc_id);
2004 argv[argc++] = (char *)"-f";
2005 argv[argc++] = (char *)confpath;
2007 argv[argc++] = (char *)"-P";
2008 argv[argc++] = (char *)chroot_path;
2010 if (!daemonize)
2011 argv[argc++] = (char *)"-d";
2012 if (verbosity > 0)
2013 argv[argc++] = (char *)"-v";
2014 if (verbosity > 1)
2015 argv[argc++] = (char *)"-v";
2016 argv[argc++] = NULL;
2018 execvp(argv0, argv);
2019 fatal("execvp");
2022 static void
2023 start_repo_children(struct gotd *gotd, char *argv0, const char *confpath,
2024 int daemonize, int verbosity)
2026 struct gotd_repo *repo = NULL;
2027 struct gotd_child_proc *proc;
2028 int i;
2031 * XXX For now, use one reader and one writer per repository.
2032 * This should be changed to N readers + M writers.
2034 gotd->nprocs = gotd->nrepos * 2;
2035 gotd->procs = calloc(gotd->nprocs, sizeof(*gotd->procs));
2036 if (gotd->procs == NULL)
2037 fatal("calloc");
2038 for (i = 0; i < gotd->nprocs; i++) {
2039 if (repo == NULL)
2040 repo = TAILQ_FIRST(&gotd->repos);
2041 proc = &gotd->procs[i];
2042 if (i < gotd->nrepos)
2043 proc->type = PROC_REPO_READ;
2044 else
2045 proc->type = PROC_REPO_WRITE;
2046 if (strlcpy(proc->repo_name, repo->name,
2047 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
2048 fatalx("repository name too long: %s", repo->name);
2049 log_debug("adding repository %s", repo->name);
2050 if (realpath(repo->path, proc->chroot_path) == NULL)
2051 fatal("%s", repo->path);
2052 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
2053 PF_UNSPEC, proc->pipe) == -1)
2054 fatal("socketpair");
2055 proc->pid = start_child(proc->type, proc->chroot_path, argv0,
2056 confpath, proc->pipe[1], daemonize, verbosity);
2057 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
2058 log_debug("proc %s %s is on fd %d",
2059 gotd_proc_names[proc->type], proc->chroot_path,
2060 proc->pipe[0]);
2061 proc->iev.handler = gotd_dispatch;
2062 proc->iev.events = EV_READ;
2063 proc->iev.handler_arg = NULL;
2064 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
2065 gotd_dispatch, &proc->iev);
2067 repo = TAILQ_NEXT(repo, entry);
2071 static void
2072 apply_unveil(void)
2074 struct gotd_repo *repo;
2076 TAILQ_FOREACH(repo, &gotd.repos, entry) {
2077 if (unveil(repo->path, "rwc") == -1)
2078 fatal("unveil %s", repo->path);
2081 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
2082 fatal("unveil %s", GOT_TMPDIR_STR);
2084 if (unveil(NULL, NULL) == -1)
2085 fatal("unveil");
2088 int
2089 main(int argc, char **argv)
2091 const struct got_error *error = NULL;
2092 int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
2093 const char *confpath = GOTD_CONF_PATH;
2094 char *argv0 = argv[0];
2095 char title[2048];
2096 gid_t groups[NGROUPS_MAX];
2097 int ngroups = NGROUPS_MAX;
2098 struct passwd *pw = NULL;
2099 struct group *gr = NULL;
2100 char *repo_path = NULL;
2101 enum gotd_procid proc_id = PROC_GOTD;
2102 struct event evsigint, evsigterm, evsighup, evsigusr1;
2103 int *pack_fds = NULL, *temp_fds = NULL;
2105 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
2107 while ((ch = getopt(argc, argv, "df:nP:RvW")) != -1) {
2108 switch (ch) {
2109 case 'd':
2110 daemonize = 0;
2111 break;
2112 case 'f':
2113 confpath = optarg;
2114 break;
2115 case 'n':
2116 noaction = 1;
2117 break;
2118 case 'P':
2119 repo_path = realpath(optarg, NULL);
2120 if (repo_path == NULL)
2121 fatal("realpath '%s'", optarg);
2122 break;
2123 case 'R':
2124 proc_id = PROC_REPO_READ;
2125 break;
2126 case 'v':
2127 if (verbosity < 3)
2128 verbosity++;
2129 break;
2130 case 'W':
2131 proc_id = PROC_REPO_WRITE;
2132 break;
2133 default:
2134 usage();
2138 argc -= optind;
2139 argv += optind;
2141 if (argc != 0)
2142 usage();
2144 if (geteuid())
2145 fatalx("need root privileges");
2147 log_init(daemonize ? 0 : 1, LOG_DAEMON);
2148 log_setverbose(verbosity);
2150 if (parse_config(confpath, proc_id, &gotd) != 0)
2151 return 1;
2153 if (proc_id == PROC_GOTD &&
2154 (gotd.nrepos == 0 || TAILQ_EMPTY(&gotd.repos)))
2155 fatalx("no repository defined in configuration file");
2157 pw = getpwnam(gotd.user_name);
2158 if (pw == NULL)
2159 fatal("getpwuid: user %s not found", gotd.user_name);
2161 if (pw->pw_uid == 0) {
2162 fatalx("cannot run %s as %s: the user running %s "
2163 "must not be the superuser",
2164 getprogname(), pw->pw_name, getprogname());
2167 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups) == -1)
2168 log_warnx("group membership list truncated");
2170 gr = match_group(groups, ngroups, gotd.unix_group_name);
2171 if (gr == NULL) {
2172 fatalx("cannot start %s: the user running %s "
2173 "must be a secondary member of group %s",
2174 getprogname(), getprogname(), gotd.unix_group_name);
2176 if (gr->gr_gid == pw->pw_gid) {
2177 fatalx("cannot start %s: the user running %s "
2178 "must be a secondary member of group %s, but "
2179 "%s is the user's primary group",
2180 getprogname(), getprogname(), gotd.unix_group_name,
2181 gotd.unix_group_name);
2184 if (proc_id == PROC_GOTD &&
2185 !got_path_is_absolute(gotd.unix_socket_path))
2186 fatalx("bad unix socket path \"%s\": must be an absolute path",
2187 gotd.unix_socket_path);
2189 if (noaction)
2190 return 0;
2192 if (proc_id == PROC_GOTD && verbosity) {
2193 log_info("socket: %s", gotd.unix_socket_path);
2194 log_info("user: %s", pw->pw_name);
2195 log_info("secondary group: %s", gr->gr_name);
2197 fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
2198 gr->gr_gid);
2199 if (fd == -1) {
2200 fatal("cannot listen on unix socket %s",
2201 gotd.unix_socket_path);
2205 if (proc_id == PROC_GOTD) {
2206 gotd.pid = getpid();
2207 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2208 start_repo_children(&gotd, argv0, confpath, daemonize,
2209 verbosity);
2210 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
2211 if (daemonize && daemon(0, 0) == -1)
2212 fatal("daemon");
2213 } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE) {
2214 error = got_repo_pack_fds_open(&pack_fds);
2215 if (error != NULL)
2216 fatalx("cannot open pack tempfiles: %s", error->msg);
2217 error = got_repo_temp_fds_open(&temp_fds);
2218 if (error != NULL)
2219 fatalx("cannot open pack tempfiles: %s", error->msg);
2220 if (repo_path == NULL)
2221 fatalx("repository path not specified");
2222 snprintf(title, sizeof(title), "%s %s",
2223 gotd_proc_names[proc_id], repo_path);
2224 if (chroot(repo_path) == -1)
2225 fatal("chroot");
2226 if (chdir("/") == -1)
2227 fatal("chdir(\"/\")");
2228 if (daemonize && daemon(1, 0) == -1)
2229 fatal("daemon");
2230 } else
2231 fatal("invalid process id %d", proc_id);
2233 setproctitle("%s", title);
2234 log_procinit(title);
2236 /* Drop root privileges. */
2237 if (setgid(pw->pw_gid) == -1)
2238 fatal("setgid %d failed", pw->pw_gid);
2239 if (setuid(pw->pw_uid) == -1)
2240 fatal("setuid %d failed", pw->pw_uid);
2242 event_init();
2244 switch (proc_id) {
2245 case PROC_GOTD:
2246 #ifndef PROFILE
2247 if (pledge("stdio rpath wpath cpath proc getpw sendfd recvfd "
2248 "fattr flock unix unveil", NULL) == -1)
2249 err(1, "pledge");
2250 #endif
2251 break;
2252 case PROC_REPO_READ:
2253 #ifndef PROFILE
2254 if (pledge("stdio rpath recvfd", NULL) == -1)
2255 err(1, "pledge");
2256 #endif
2257 repo_read_main(title, pack_fds, temp_fds);
2258 /* NOTREACHED */
2259 exit(0);
2260 case PROC_REPO_WRITE:
2261 #ifndef PROFILE
2262 if (pledge("stdio rpath sendfd recvfd", NULL) == -1)
2263 err(1, "pledge");
2264 #endif
2265 repo_write_main(title, pack_fds, temp_fds);
2266 /* NOTREACHED */
2267 exit(0);
2268 default:
2269 fatal("invalid process id %d", proc_id);
2272 if (proc_id != PROC_GOTD)
2273 fatal("invalid process id %d", proc_id);
2275 apply_unveil();
2277 signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
2278 signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
2279 signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
2280 signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
2281 signal(SIGPIPE, SIG_IGN);
2283 signal_add(&evsigint, NULL);
2284 signal_add(&evsigterm, NULL);
2285 signal_add(&evsighup, NULL);
2286 signal_add(&evsigusr1, NULL);
2288 event_set(&gotd.ev, fd, EV_READ | EV_PERSIST, gotd_accept, NULL);
2289 if (event_add(&gotd.ev, NULL))
2290 fatalx("event add");
2291 evtimer_set(&gotd.pause, gotd_accept_paused, NULL);
2293 event_dispatch();
2295 if (fd != -1)
2296 close(fd);
2297 if (pack_fds)
2298 got_repo_pack_fds_close(pack_fds);
2299 free(repo_path);
2300 return 0;