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 /* Send pack pipe end 0 to repo_write. */
920 if (gotd_imsg_compose_event(&client->repo_write->iev,
921 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[0],
922 &ipipe, sizeof(ipipe)) == -1) {
923 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
924 pipe[0] = -1;
925 goto done;
927 pipe[0] = -1;
929 /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
930 if (gotd_imsg_compose_event(&client->iev,
931 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1], NULL, 0) == -1)
932 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
933 pipe[1] = -1;
935 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.pack",
936 client->repo_write->chroot_path, GOT_OBJECTS_PACK_DIR,
937 client->euid) == -1) {
938 err = got_error_from_errno("asprintf");
939 goto done;
942 err = got_opentemp_named_fd(&pack_path, &packfd, basepath, "");
943 if (err)
944 goto done;
946 free(basepath);
947 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.idx",
948 client->repo_write->chroot_path, GOT_OBJECTS_PACK_DIR,
949 client->euid) == -1) {
950 err = got_error_from_errno("asprintf");
951 basepath = NULL;
952 goto done;
954 err = got_opentemp_named_fd(&idx_path, &idxfd, basepath, "");
955 if (err)
956 goto done;
958 memset(&ifile, 0, sizeof(ifile));
959 ifile.client_id = client->id;
960 if (gotd_imsg_compose_event(&client->repo_write->iev,
961 GOTD_IMSG_PACKIDX_FILE, PROC_GOTD, idxfd,
962 &ifile, sizeof(ifile)) == -1) {
963 err = got_error_from_errno("imsg compose PACKIDX_FILE");
964 idxfd = -1;
965 goto done;
967 idxfd = -1;
969 memset(&ipack, 0, sizeof(ipack));
970 ipack.client_id = client->id;
971 if (client_has_capability(client, GOT_CAPA_REPORT_STATUS))
972 ipack.report_status = 1;
974 if (gotd_imsg_compose_event(&client->repo_write->iev,
975 GOTD_IMSG_RECV_PACKFILE, PROC_GOTD, packfd,
976 &ipack, sizeof(ipack)) == -1) {
977 err = got_error_from_errno("imsg compose RECV_PACKFILE");
978 packfd = -1;
979 goto done;
981 packfd = -1;
983 done:
984 free(basepath);
985 if (pipe[0] != -1 && close(pipe[0]) == -1 && err == NULL)
986 err = got_error_from_errno("close");
987 if (pipe[1] != -1 && close(pipe[1]) == -1 && err == NULL)
988 err = got_error_from_errno("close");
989 if (packfd != -1 && close(packfd) == -1 && err == NULL)
990 err = got_error_from_errno("close");
991 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
992 err = got_error_from_errno("close");
993 if (err) {
994 free(pack_path);
995 free(idx_path);
996 } else {
997 client->packfile_path = pack_path;
998 client->packidx_path = idx_path;
1000 return err;
1003 static void
1004 gotd_request(int fd, short events, void *arg)
1006 struct gotd_imsgev *iev = arg;
1007 struct imsgbuf *ibuf = &iev->ibuf;
1008 struct gotd_client *client = iev->handler_arg;
1009 const struct got_error *err = NULL;
1010 struct imsg imsg;
1011 ssize_t n;
1013 if (events & EV_WRITE) {
1014 while (ibuf->w.queued) {
1015 n = msgbuf_write(&ibuf->w);
1016 if (n == -1 && errno == EPIPE) {
1018 * The client has closed its socket.
1019 * This can happen when Git clients are
1020 * done sending pack file data.
1022 msgbuf_clear(&ibuf->w);
1023 continue;
1024 } else if (n == -1 && errno != EAGAIN) {
1025 err = got_error_from_errno("imsg_flush");
1026 disconnect_on_error(client, err);
1027 return;
1029 if (n == 0) {
1030 /* Connection closed. */
1031 err = got_error(GOT_ERR_EOF);
1032 disconnect_on_error(client, err);
1033 return;
1037 /* Disconnect gotctl(8) now that messages have been sent. */
1038 if (!client_is_reading(client) && !client_is_writing(client)) {
1039 disconnect(client);
1040 return;
1044 if ((events & EV_READ) == 0)
1045 return;
1047 memset(&imsg, 0, sizeof(imsg));
1049 while (err == NULL) {
1050 err = gotd_imsg_recv(&imsg, ibuf, 0);
1051 if (err) {
1052 if (err->code == GOT_ERR_PRIVSEP_READ)
1053 err = NULL;
1054 break;
1057 evtimer_del(&client->tmo);
1059 switch (imsg.hdr.type) {
1060 case GOTD_IMSG_INFO:
1061 err = send_info(client);
1062 break;
1063 case GOTD_IMSG_STOP:
1064 err = stop_gotd(client);
1065 break;
1066 case GOTD_IMSG_LIST_REFS:
1067 if (client->state != GOTD_STATE_EXPECT_LIST_REFS) {
1068 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1069 "unexpected list-refs request received");
1070 break;
1072 err = forward_list_refs_request(client, &imsg);
1073 if (err)
1074 break;
1075 client->state = GOTD_STATE_EXPECT_CAPABILITIES;
1076 log_debug("uid %d: expecting capabilities",
1077 client->euid);
1078 break;
1079 case GOTD_IMSG_CAPABILITIES:
1080 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1081 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1082 "unexpected capabilities received");
1083 break;
1085 log_debug("receiving capabilities from uid %d",
1086 client->euid);
1087 err = recv_capabilities(client, &imsg);
1088 break;
1089 case GOTD_IMSG_CAPABILITY:
1090 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1091 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1092 "unexpected capability received");
1093 break;
1095 err = recv_capability(client, &imsg);
1096 if (err || client->ncapabilities < client->ncapa_alloc)
1097 break;
1098 if (client_is_reading(client)) {
1099 client->state = GOTD_STATE_EXPECT_WANT;
1100 log_debug("uid %d: expecting want-lines",
1101 client->euid);
1102 } else if (client_is_writing(client)) {
1103 client->state = GOTD_STATE_EXPECT_REF_UPDATE;
1104 log_debug("uid %d: expecting ref-update-lines",
1105 client->euid);
1106 } else
1107 fatalx("client %d is both reading and writing",
1108 client->euid);
1109 break;
1110 case GOTD_IMSG_WANT:
1111 if (client->state != GOTD_STATE_EXPECT_WANT) {
1112 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1113 "unexpected want-line received");
1114 break;
1116 log_debug("received want-line from uid %d",
1117 client->euid);
1118 err = ensure_client_is_reading(client);
1119 if (err)
1120 break;
1121 err = forward_want(client, &imsg);
1122 break;
1123 case GOTD_IMSG_REF_UPDATE:
1124 if (client->state != GOTD_STATE_EXPECT_REF_UPDATE) {
1125 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1126 "unexpected ref-update-line received");
1127 break;
1129 log_debug("received ref-update-line from uid %d",
1130 client->euid);
1131 err = ensure_client_is_writing(client);
1132 if (err)
1133 break;
1134 err = forward_ref_update(client, &imsg);
1135 if (err)
1136 break;
1137 client->state = GOTD_STATE_EXPECT_MORE_REF_UPDATES;
1138 break;
1139 case GOTD_IMSG_HAVE:
1140 if (client->state != GOTD_STATE_EXPECT_HAVE) {
1141 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1142 "unexpected have-line received");
1143 break;
1145 log_debug("received have-line from uid %d",
1146 client->euid);
1147 err = ensure_client_is_reading(client);
1148 if (err)
1149 break;
1150 err = forward_have(client, &imsg);
1151 if (err)
1152 break;
1153 break;
1154 case GOTD_IMSG_FLUSH:
1155 if (client->state == GOTD_STATE_EXPECT_WANT ||
1156 client->state == GOTD_STATE_EXPECT_HAVE) {
1157 err = ensure_client_is_reading(client);
1158 if (err)
1159 break;
1160 } else if (client->state ==
1161 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1162 err = ensure_client_is_writing(client);
1163 if (err)
1164 break;
1165 } else {
1166 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1167 "unexpected flush-pkt received");
1168 break;
1170 log_debug("received flush-pkt from uid %d",
1171 client->euid);
1172 if (client->state == GOTD_STATE_EXPECT_WANT) {
1173 client->state = GOTD_STATE_EXPECT_HAVE;
1174 log_debug("uid %d: expecting have-lines",
1175 client->euid);
1176 } else if (client->state == GOTD_STATE_EXPECT_HAVE) {
1177 client->state = GOTD_STATE_EXPECT_DONE;
1178 log_debug("uid %d: expecting 'done'",
1179 client->euid);
1180 } else if (client->state ==
1181 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1182 client->state = GOTD_STATE_EXPECT_PACKFILE;
1183 log_debug("uid %d: expecting packfile",
1184 client->euid);
1185 err = recv_packfile(client);
1186 } else {
1187 /* should not happen, see above */
1188 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1189 "unexpected client state");
1190 break;
1192 break;
1193 case GOTD_IMSG_DONE:
1194 if (client->state != GOTD_STATE_EXPECT_HAVE &&
1195 client->state != GOTD_STATE_EXPECT_DONE) {
1196 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1197 "unexpected flush-pkt received");
1198 break;
1200 log_debug("received 'done' from uid %d", client->euid);
1201 err = ensure_client_is_reading(client);
1202 if (err)
1203 break;
1204 client->state = GOTD_STATE_DONE;
1205 err = send_packfile(client);
1206 break;
1207 default:
1208 err = got_error(GOT_ERR_PRIVSEP_MSG);
1209 break;
1212 imsg_free(&imsg);
1215 if (err) {
1216 if (err->code != GOT_ERR_EOF ||
1217 client->state != GOTD_STATE_EXPECT_PACKFILE)
1218 disconnect_on_error(client, err);
1219 } else {
1220 gotd_imsg_event_add(&client->iev);
1221 evtimer_add(&client->tmo, &timeout);
1225 static void
1226 gotd_request_timeout(int fd, short events, void *arg)
1228 struct gotd_client *client = arg;
1230 log_debug("disconnecting uid %d due to timeout", client->euid);
1231 disconnect(client);
1234 static void
1235 gotd_accept(int fd, short event, void *arg)
1237 struct sockaddr_storage ss;
1238 struct timeval backoff;
1239 socklen_t len;
1240 int s = -1;
1241 struct gotd_client *client = NULL;
1242 uid_t euid;
1243 gid_t egid;
1245 backoff.tv_sec = 1;
1246 backoff.tv_usec = 0;
1248 if (event_add(&gotd.ev, NULL) == -1) {
1249 log_warn("event_add");
1250 return;
1252 if (event & EV_TIMEOUT)
1253 return;
1255 len = sizeof(ss);
1257 s = accept_reserve(fd, (struct sockaddr *)&ss, &len, GOTD_FD_RESERVE,
1258 &inflight);
1260 if (s == -1) {
1261 switch (errno) {
1262 case EINTR:
1263 case EWOULDBLOCK:
1264 case ECONNABORTED:
1265 return;
1266 case EMFILE:
1267 case ENFILE:
1268 event_del(&gotd.ev);
1269 evtimer_add(&gotd.pause, &backoff);
1270 return;
1271 default:
1272 log_warn("%s: accept", __func__);
1273 return;
1277 if (client_cnt >= GOTD_MAXCLIENTS)
1278 goto err;
1280 if (getpeereid(s, &euid, &egid) == -1) {
1281 log_warn("%s: getpeereid", __func__);
1282 goto err;
1285 client = calloc(1, sizeof(*client));
1286 if (client == NULL) {
1287 log_warn("%s: calloc", __func__);
1288 goto err;
1291 client->state = GOTD_STATE_EXPECT_LIST_REFS;
1292 client->id = get_client_id();
1293 client->fd = s;
1294 s = -1;
1295 client->delta_cache_fd = -1;
1296 client->euid = euid;
1297 client->egid = egid;
1298 client->nref_updates = -1;
1300 imsg_init(&client->iev.ibuf, client->fd);
1301 client->iev.handler = gotd_request;
1302 client->iev.events = EV_READ;
1303 client->iev.handler_arg = client;
1305 event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
1306 &client->iev);
1307 gotd_imsg_event_add(&client->iev);
1309 evtimer_set(&client->tmo, gotd_request_timeout, client);
1311 add_client(client);
1312 log_debug("%s: new client uid %d connected on fd %d", __func__,
1313 client->euid, client->fd);
1314 return;
1315 err:
1316 inflight--;
1317 if (s != -1)
1318 close(s);
1319 free(client);
1322 static void
1323 gotd_accept_paused(int fd, short event, void *arg)
1325 event_add(&gotd.ev, NULL);
1328 static const char *gotd_proc_names[PROC_MAX] = {
1329 "parent",
1330 "repo_read",
1331 "repo_write"
1334 static struct gotd_child_proc *
1335 get_proc_for_pid(pid_t pid)
1337 struct gotd_child_proc *proc;
1338 int i;
1340 for (i = 0; i < gotd.nprocs; i++) {
1341 proc = &gotd.procs[i];
1342 if (proc->pid == pid)
1343 return proc;
1346 return NULL;
1349 static void
1350 kill_proc(struct gotd_child_proc *proc, int fatal)
1352 if (fatal) {
1353 log_warnx("sending SIGKILL to PID %d", proc->pid);
1354 kill(proc->pid, SIGKILL);
1355 } else
1356 kill(proc->pid, SIGTERM);
1359 static void
1360 gotd_shutdown(void)
1362 pid_t pid;
1363 int status, i;
1364 struct gotd_child_proc *proc;
1366 for (i = 0; i < gotd.nprocs; i++) {
1367 proc = &gotd.procs[i];
1368 msgbuf_clear(&proc->iev.ibuf.w);
1369 close(proc->iev.ibuf.fd);
1370 kill_proc(proc, 0);
1373 log_debug("waiting for children to terminate");
1374 do {
1375 pid = wait(&status);
1376 if (pid == -1) {
1377 if (errno != EINTR && errno != ECHILD)
1378 fatal("wait");
1379 } else if (WIFSIGNALED(status)) {
1380 proc = get_proc_for_pid(pid);
1381 log_warnx("%s %s child process terminated; signal %d",
1382 proc ? gotd_proc_names[proc->type] : "",
1383 proc ? proc->chroot_path : "", WTERMSIG(status));
1385 } while (pid != -1 || (pid == -1 && errno == EINTR));
1387 log_info("terminating");
1388 exit(0);
1391 void
1392 gotd_sighdlr(int sig, short event, void *arg)
1395 * Normal signal handler rules don't apply because libevent
1396 * decouples for us.
1399 switch (sig) {
1400 case SIGHUP:
1401 log_info("%s: ignoring SIGHUP", __func__);
1402 break;
1403 case SIGUSR1:
1404 log_info("%s: ignoring SIGUSR1", __func__);
1405 break;
1406 case SIGTERM:
1407 case SIGINT:
1408 gotd_shutdown();
1409 log_warnx("gotd terminating");
1410 exit(0);
1411 break;
1412 default:
1413 fatalx("unexpected signal");
1417 static const struct got_error *
1418 ensure_proc_is_reading(struct gotd_client *client,
1419 struct gotd_child_proc *proc)
1421 if (!client_is_reading(client)) {
1422 kill_proc(proc, 1);
1423 return got_error_fmt(GOT_ERR_BAD_PACKET,
1424 "PID %d handled a read-request for uid %d but this "
1425 "user is not reading from a repository", proc->pid,
1426 client->euid);
1429 return NULL;
1432 static const struct got_error *
1433 ensure_proc_is_writing(struct gotd_client *client,
1434 struct gotd_child_proc *proc)
1436 if (!client_is_writing(client)) {
1437 kill_proc(proc, 1);
1438 return got_error_fmt(GOT_ERR_BAD_PACKET,
1439 "PID %d handled a write-request for uid %d but this "
1440 "user is not writing to a repository", proc->pid,
1441 client->euid);
1444 return NULL;
1447 static int
1448 verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
1449 struct imsg *imsg)
1451 const struct got_error *err;
1452 struct gotd_child_proc *client_proc;
1453 int ret = 0;
1455 client_proc = get_client_proc(client);
1456 if (client_proc == NULL)
1457 fatalx("no process found for uid %d", client->euid);
1459 if (proc->pid != client_proc->pid) {
1460 kill_proc(proc, 1);
1461 log_warnx("received message from PID %d for uid %d, while "
1462 "PID %d is the process serving this user",
1463 proc->pid, client->euid, client_proc->pid);
1464 return 0;
1467 switch (imsg->hdr.type) {
1468 case GOTD_IMSG_ERROR:
1469 ret = 1;
1470 break;
1471 case GOTD_IMSG_PACKFILE_DONE:
1472 err = ensure_proc_is_reading(client, proc);
1473 if (err)
1474 log_warnx("uid %d: %s", client->euid, err->msg);
1475 else
1476 ret = 1;
1477 break;
1478 case GOTD_IMSG_PACKFILE_INSTALL:
1479 case GOTD_IMSG_REF_UPDATES_START:
1480 case GOTD_IMSG_REF_UPDATE:
1481 err = ensure_proc_is_writing(client, proc);
1482 if (err)
1483 log_warnx("uid %d: %s", client->euid, err->msg);
1484 else
1485 ret = 1;
1486 break;
1487 default:
1488 log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
1489 break;
1492 return ret;
1495 static const struct got_error *
1496 recv_packfile_done(uint32_t *client_id, struct imsg *imsg)
1498 struct gotd_imsg_packfile_done idone;
1499 size_t datalen;
1501 log_debug("packfile-done received");
1503 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1504 if (datalen != sizeof(idone))
1505 return got_error(GOT_ERR_PRIVSEP_LEN);
1506 memcpy(&idone, imsg->data, sizeof(idone));
1508 *client_id = idone.client_id;
1509 return NULL;
1512 static const struct got_error *
1513 recv_packfile_install(uint32_t *client_id, struct imsg *imsg)
1515 struct gotd_imsg_packfile_install inst;
1516 size_t datalen;
1518 log_debug("packfile-install received");
1520 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1521 if (datalen != sizeof(inst))
1522 return got_error(GOT_ERR_PRIVSEP_LEN);
1523 memcpy(&inst, imsg->data, sizeof(inst));
1525 *client_id = inst.client_id;
1526 return NULL;
1529 static const struct got_error *
1530 recv_ref_updates_start(uint32_t *client_id, struct imsg *imsg)
1532 struct gotd_imsg_ref_updates_start istart;
1533 size_t datalen;
1535 log_debug("ref-updates-start received");
1537 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1538 if (datalen != sizeof(istart))
1539 return got_error(GOT_ERR_PRIVSEP_LEN);
1540 memcpy(&istart, imsg->data, sizeof(istart));
1542 *client_id = istart.client_id;
1543 return NULL;
1546 static const struct got_error *
1547 recv_ref_update(uint32_t *client_id, struct imsg *imsg)
1549 struct gotd_imsg_ref_update iref;
1550 size_t datalen;
1552 log_debug("ref-update received");
1554 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1555 if (datalen < sizeof(iref))
1556 return got_error(GOT_ERR_PRIVSEP_LEN);
1557 memcpy(&iref, imsg->data, sizeof(iref));
1559 *client_id = iref.client_id;
1560 return NULL;
1563 static const struct got_error *
1564 send_ref_update_ok(struct gotd_client *client,
1565 struct gotd_imsg_ref_update *iref, const char *refname)
1567 struct gotd_imsg_ref_update_ok iok;
1568 struct ibuf *wbuf;
1569 size_t len;
1571 memset(&iok, 0, sizeof(iok));
1572 iok.client_id = client->id;
1573 memcpy(iok.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1574 memcpy(iok.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1575 iok.name_len = strlen(refname);
1577 len = sizeof(iok) + iok.name_len;
1578 wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_OK,
1579 PROC_GOTD, gotd.pid, len);
1580 if (wbuf == NULL)
1581 return got_error_from_errno("imsg_create REF_UPDATE_OK");
1583 if (imsg_add(wbuf, &iok, sizeof(iok)) == -1)
1584 return got_error_from_errno("imsg_add REF_UPDATE_OK");
1585 if (imsg_add(wbuf, refname, iok.name_len) == -1)
1586 return got_error_from_errno("imsg_add REF_UPDATE_OK");
1588 wbuf->fd = -1;
1589 imsg_close(&client->iev.ibuf, wbuf);
1590 gotd_imsg_event_add(&client->iev);
1591 return NULL;
1594 static void
1595 send_refs_updated(struct gotd_client *client)
1597 if (gotd_imsg_compose_event(&client->iev,
1598 GOTD_IMSG_REFS_UPDATED, PROC_GOTD, -1, NULL, 0) == -1)
1599 log_warn("imsg compose REFS_UPDATED");
1602 static const struct got_error *
1603 send_ref_update_ng(struct gotd_client *client,
1604 struct gotd_imsg_ref_update *iref, const char *refname,
1605 const char *reason)
1607 const struct got_error *ng_err;
1608 struct gotd_imsg_ref_update_ng ing;
1609 struct ibuf *wbuf;
1610 size_t len;
1612 memset(&ing, 0, sizeof(ing));
1613 ing.client_id = client->id;
1614 memcpy(ing.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1615 memcpy(ing.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1616 ing.name_len = strlen(refname);
1618 ng_err = got_error_fmt(GOT_ERR_REF_BUSY, "%s", reason);
1619 ing.reason_len = strlen(ng_err->msg);
1621 len = sizeof(ing) + ing.name_len + ing.reason_len;
1622 wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_NG,
1623 PROC_GOTD, gotd.pid, len);
1624 if (wbuf == NULL)
1625 return got_error_from_errno("imsg_create REF_UPDATE_NG");
1627 if (imsg_add(wbuf, &ing, sizeof(ing)) == -1)
1628 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1629 if (imsg_add(wbuf, refname, ing.name_len) == -1)
1630 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1631 if (imsg_add(wbuf, ng_err->msg, ing.reason_len) == -1)
1632 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1634 wbuf->fd = -1;
1635 imsg_close(&client->iev.ibuf, wbuf);
1636 gotd_imsg_event_add(&client->iev);
1637 return NULL;
1640 static const struct got_error *
1641 install_pack(struct gotd_client *client, const char *repo_path,
1642 struct imsg *imsg)
1644 const struct got_error *err = NULL;
1645 struct gotd_imsg_packfile_install inst;
1646 char hex[SHA1_DIGEST_STRING_LENGTH];
1647 size_t datalen;
1648 char *packfile_path = NULL, *packidx_path = NULL;
1650 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1651 if (datalen != sizeof(inst))
1652 return got_error(GOT_ERR_PRIVSEP_LEN);
1653 memcpy(&inst, imsg->data, sizeof(inst));
1655 if (client->packfile_path == NULL)
1656 return got_error_msg(GOT_ERR_BAD_REQUEST,
1657 "client has no pack file");
1658 if (client->packidx_path == NULL)
1659 return got_error_msg(GOT_ERR_BAD_REQUEST,
1660 "client has no pack file index");
1662 if (got_sha1_digest_to_str(inst.pack_sha1, hex, sizeof(hex)) == NULL)
1663 return got_error_msg(GOT_ERR_NO_SPACE,
1664 "could not convert pack file SHA1 to hex");
1666 if (asprintf(&packfile_path, "/%s/%s/pack-%s.pack",
1667 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1668 err = got_error_from_errno("asprintf");
1669 goto done;
1672 if (asprintf(&packidx_path, "/%s/%s/pack-%s.idx",
1673 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1674 err = got_error_from_errno("asprintf");
1675 goto done;
1678 if (rename(client->packfile_path, packfile_path) == -1) {
1679 err = got_error_from_errno3("rename", client->packfile_path,
1680 packfile_path);
1681 goto done;
1684 free(client->packfile_path);
1685 client->packfile_path = NULL;
1687 if (rename(client->packidx_path, packidx_path) == -1) {
1688 err = got_error_from_errno3("rename", client->packidx_path,
1689 packidx_path);
1690 goto done;
1693 free(client->packidx_path);
1694 client->packidx_path = NULL;
1695 done:
1696 free(packfile_path);
1697 free(packidx_path);
1698 return err;
1701 static const struct got_error *
1702 begin_ref_updates(struct gotd_client *client, struct imsg *imsg)
1704 struct gotd_imsg_ref_updates_start istart;
1705 size_t datalen;
1707 if (client->nref_updates != -1)
1708 return got_error(GOT_ERR_PRIVSEP_MSG);
1710 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1711 if (datalen != sizeof(istart))
1712 return got_error(GOT_ERR_PRIVSEP_LEN);
1713 memcpy(&istart, imsg->data, sizeof(istart));
1715 if (istart.nref_updates <= 0)
1716 return got_error(GOT_ERR_PRIVSEP_MSG);
1718 client->nref_updates = istart.nref_updates;
1719 return NULL;
1722 static const struct got_error *
1723 update_ref(struct gotd_client *client, const char *repo_path,
1724 struct imsg *imsg)
1726 const struct got_error *err = NULL;
1727 struct got_repository *repo = NULL;
1728 struct got_reference *ref = NULL;
1729 struct gotd_imsg_ref_update iref;
1730 struct got_object_id old_id, new_id;
1731 struct got_object_id *id = NULL;
1732 struct got_object *obj = NULL;
1733 char *refname = NULL;
1734 size_t datalen;
1735 int locked = 0;
1737 log_debug("update-ref from uid %d", client->euid);
1739 if (client->nref_updates <= 0)
1740 return got_error(GOT_ERR_PRIVSEP_MSG);
1742 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1743 if (datalen < sizeof(iref))
1744 return got_error(GOT_ERR_PRIVSEP_LEN);
1745 memcpy(&iref, imsg->data, sizeof(iref));
1746 if (datalen != sizeof(iref) + iref.name_len)
1747 return got_error(GOT_ERR_PRIVSEP_LEN);
1748 refname = malloc(iref.name_len + 1);
1749 if (refname == NULL)
1750 return got_error_from_errno("malloc");
1751 memcpy(refname, imsg->data + sizeof(iref), iref.name_len);
1752 refname[iref.name_len] = '\0';
1754 log_debug("updating ref %s for uid %d", refname, client->euid);
1756 err = got_repo_open(&repo, repo_path, NULL, NULL);
1757 if (err)
1758 goto done;
1760 memcpy(old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
1761 memcpy(new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
1762 err = got_object_open(&obj, repo, &new_id);
1763 if (err)
1764 goto done;
1766 if (iref.ref_is_new) {
1767 err = got_ref_open(&ref, repo, refname, 0);
1768 if (err) {
1769 if (err->code != GOT_ERR_NOT_REF)
1770 goto done;
1771 err = got_ref_alloc(&ref, refname, &new_id);
1772 if (err)
1773 goto done;
1774 err = got_ref_write(ref, repo); /* will lock/unlock */
1775 if (err)
1776 goto done;
1777 } else {
1778 err = got_error_fmt(GOT_ERR_REF_BUSY,
1779 "%s has been created by someone else "
1780 "while transaction was in progress",
1781 got_ref_get_name(ref));
1782 goto done;
1784 } else {
1785 err = got_ref_open(&ref, repo, refname, 1 /* lock */);
1786 if (err)
1787 goto done;
1788 locked = 1;
1790 err = got_ref_resolve(&id, repo, ref);
1791 if (err)
1792 goto done;
1794 if (got_object_id_cmp(id, &old_id) != 0) {
1795 err = got_error_fmt(GOT_ERR_REF_BUSY,
1796 "%s has been modified by someone else "
1797 "while transaction was in progress",
1798 got_ref_get_name(ref));
1799 goto done;
1802 err = got_ref_change_ref(ref, &new_id);
1803 if (err)
1804 goto done;
1806 err = got_ref_write(ref, repo);
1807 if (err)
1808 goto done;
1810 free(id);
1811 id = NULL;
1813 done:
1814 if (err) {
1815 if (err->code == GOT_ERR_LOCKFILE_TIMEOUT) {
1816 err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
1817 "could not acquire exclusive file lock for %s",
1818 refname);
1820 send_ref_update_ng(client, &iref, refname, err->msg);
1821 } else
1822 send_ref_update_ok(client, &iref, refname);
1824 if (client->nref_updates > 0) {
1825 client->nref_updates--;
1826 if (client->nref_updates == 0)
1827 send_refs_updated(client);
1830 if (locked) {
1831 const struct got_error *unlock_err;
1832 unlock_err = got_ref_unlock(ref);
1833 if (unlock_err && err == NULL)
1834 err = unlock_err;
1836 if (ref)
1837 got_ref_close(ref);
1838 if (obj)
1839 got_object_close(obj);
1840 if (repo)
1841 got_repo_close(repo);
1842 free(refname);
1843 free(id);
1844 return err;
1847 static void
1848 gotd_dispatch(int fd, short event, void *arg)
1850 struct gotd_imsgev *iev = arg;
1851 struct imsgbuf *ibuf = &iev->ibuf;
1852 struct gotd_child_proc *proc = NULL;
1853 ssize_t n;
1854 int shut = 0;
1855 struct imsg imsg;
1857 if (event & EV_READ) {
1858 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1859 fatal("imsg_read error");
1860 if (n == 0) {
1861 /* Connection closed. */
1862 shut = 1;
1863 goto done;
1867 if (event & EV_WRITE) {
1868 n = msgbuf_write(&ibuf->w);
1869 if (n == -1 && errno != EAGAIN)
1870 fatal("msgbuf_write");
1871 if (n == 0) {
1872 /* Connection closed. */
1873 shut = 1;
1874 goto done;
1878 proc = find_proc_by_fd(fd);
1879 if (proc == NULL)
1880 fatalx("cannot find child process for fd %d", fd);
1882 for (;;) {
1883 const struct got_error *err = NULL;
1884 struct gotd_client *client = NULL;
1885 uint32_t client_id = 0;
1886 int do_disconnect = 0;
1887 int do_ref_updates = 0, do_ref_update = 0;
1888 int do_packfile_install = 0;
1890 if ((n = imsg_get(ibuf, &imsg)) == -1)
1891 fatal("%s: imsg_get error", __func__);
1892 if (n == 0) /* No more messages. */
1893 break;
1895 switch (imsg.hdr.type) {
1896 case GOTD_IMSG_ERROR:
1897 do_disconnect = 1;
1898 err = gotd_imsg_recv_error(&client_id, &imsg);
1899 break;
1900 case GOTD_IMSG_PACKFILE_DONE:
1901 do_disconnect = 1;
1902 err = recv_packfile_done(&client_id, &imsg);
1903 break;
1904 case GOTD_IMSG_PACKFILE_INSTALL:
1905 err = recv_packfile_install(&client_id, &imsg);
1906 if (err == NULL)
1907 do_packfile_install = 1;
1908 break;
1909 case GOTD_IMSG_REF_UPDATES_START:
1910 err = recv_ref_updates_start(&client_id, &imsg);
1911 if (err == NULL)
1912 do_ref_updates = 1;
1913 break;
1914 case GOTD_IMSG_REF_UPDATE:
1915 err = recv_ref_update(&client_id, &imsg);
1916 if (err == NULL)
1917 do_ref_update = 1;
1918 break;
1919 default:
1920 log_debug("unexpected imsg %d", imsg.hdr.type);
1921 break;
1924 client = find_client(client_id);
1925 if (client == NULL) {
1926 log_warnx("%s: client not found", __func__);
1927 imsg_free(&imsg);
1928 continue;
1931 if (!verify_imsg_src(client, proc, &imsg)) {
1932 log_debug("dropping imsg type %d from PID %d",
1933 imsg.hdr.type, proc->pid);
1934 imsg_free(&imsg);
1935 continue;
1937 if (err)
1938 log_warnx("uid %d: %s", client->euid, err->msg);
1940 if (do_disconnect) {
1941 if (err)
1942 disconnect_on_error(client, err);
1943 else
1944 disconnect(client);
1945 } else {
1946 if (do_packfile_install)
1947 err = install_pack(client, proc->chroot_path,
1948 &imsg);
1949 else if (do_ref_updates)
1950 err = begin_ref_updates(client, &imsg);
1951 else if (do_ref_update)
1952 err = update_ref(client, proc->chroot_path,
1953 &imsg);
1954 if (err)
1955 log_warnx("uid %d: %s", client->euid, err->msg);
1957 imsg_free(&imsg);
1959 done:
1960 if (!shut) {
1961 gotd_imsg_event_add(iev);
1962 } else {
1963 /* This pipe is dead. Remove its event handler */
1964 event_del(&iev->ev);
1965 event_loopexit(NULL);
1969 static pid_t
1970 start_child(enum gotd_procid proc_id, const char *chroot_path,
1971 char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
1973 char *argv[11];
1974 int argc = 0;
1975 pid_t pid;
1977 switch (pid = fork()) {
1978 case -1:
1979 fatal("cannot fork");
1980 case 0:
1981 break;
1982 default:
1983 close(fd);
1984 return pid;
1987 if (fd != GOTD_SOCK_FILENO) {
1988 if (dup2(fd, GOTD_SOCK_FILENO) == -1)
1989 fatal("cannot setup imsg fd");
1990 } else if (fcntl(fd, F_SETFD, 0) == -1)
1991 fatal("cannot setup imsg fd");
1993 argv[argc++] = argv0;
1994 switch (proc_id) {
1995 case PROC_REPO_READ:
1996 argv[argc++] = (char *)"-R";
1997 break;
1998 case PROC_REPO_WRITE:
1999 argv[argc++] = (char *)"-W";
2000 break;
2001 default:
2002 fatalx("invalid process id %d", proc_id);
2005 argv[argc++] = (char *)"-f";
2006 argv[argc++] = (char *)confpath;
2008 argv[argc++] = (char *)"-P";
2009 argv[argc++] = (char *)chroot_path;
2011 if (!daemonize)
2012 argv[argc++] = (char *)"-d";
2013 if (verbosity > 0)
2014 argv[argc++] = (char *)"-v";
2015 if (verbosity > 1)
2016 argv[argc++] = (char *)"-v";
2017 argv[argc++] = NULL;
2019 execvp(argv0, argv);
2020 fatal("execvp");
2023 static void
2024 start_repo_children(struct gotd *gotd, char *argv0, const char *confpath,
2025 int daemonize, int verbosity)
2027 struct gotd_repo *repo = NULL;
2028 struct gotd_child_proc *proc;
2029 int i;
2032 * XXX For now, use one reader and one writer per repository.
2033 * This should be changed to N readers + M writers.
2035 gotd->nprocs = gotd->nrepos * 2;
2036 gotd->procs = calloc(gotd->nprocs, sizeof(*gotd->procs));
2037 if (gotd->procs == NULL)
2038 fatal("calloc");
2039 for (i = 0; i < gotd->nprocs; i++) {
2040 if (repo == NULL)
2041 repo = TAILQ_FIRST(&gotd->repos);
2042 proc = &gotd->procs[i];
2043 if (i < gotd->nrepos)
2044 proc->type = PROC_REPO_READ;
2045 else
2046 proc->type = PROC_REPO_WRITE;
2047 if (strlcpy(proc->repo_name, repo->name,
2048 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
2049 fatalx("repository name too long: %s", repo->name);
2050 log_debug("adding repository %s", repo->name);
2051 if (realpath(repo->path, proc->chroot_path) == NULL)
2052 fatal("%s", repo->path);
2053 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
2054 PF_UNSPEC, proc->pipe) == -1)
2055 fatal("socketpair");
2056 proc->pid = start_child(proc->type, proc->chroot_path, argv0,
2057 confpath, proc->pipe[1], daemonize, verbosity);
2058 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
2059 log_debug("proc %s %s is on fd %d",
2060 gotd_proc_names[proc->type], proc->chroot_path,
2061 proc->pipe[0]);
2062 proc->iev.handler = gotd_dispatch;
2063 proc->iev.events = EV_READ;
2064 proc->iev.handler_arg = NULL;
2065 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
2066 gotd_dispatch, &proc->iev);
2068 repo = TAILQ_NEXT(repo, entry);
2072 static void
2073 apply_unveil(void)
2075 struct gotd_repo *repo;
2077 TAILQ_FOREACH(repo, &gotd.repos, entry) {
2078 if (unveil(repo->path, "rwc") == -1)
2079 fatal("unveil %s", repo->path);
2082 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
2083 fatal("unveil %s", GOT_TMPDIR_STR);
2085 if (unveil(NULL, NULL) == -1)
2086 fatal("unveil");
2089 int
2090 main(int argc, char **argv)
2092 const struct got_error *error = NULL;
2093 int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
2094 const char *confpath = GOTD_CONF_PATH;
2095 char *argv0 = argv[0];
2096 char title[2048];
2097 gid_t groups[NGROUPS_MAX];
2098 int ngroups = NGROUPS_MAX;
2099 struct passwd *pw = NULL;
2100 struct group *gr = NULL;
2101 char *repo_path = NULL;
2102 enum gotd_procid proc_id = PROC_GOTD;
2103 struct event evsigint, evsigterm, evsighup, evsigusr1;
2104 int *pack_fds = NULL, *temp_fds = NULL;
2106 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
2108 while ((ch = getopt(argc, argv, "df:nP:RvW")) != -1) {
2109 switch (ch) {
2110 case 'd':
2111 daemonize = 0;
2112 break;
2113 case 'f':
2114 confpath = optarg;
2115 break;
2116 case 'n':
2117 noaction = 1;
2118 break;
2119 case 'P':
2120 repo_path = realpath(optarg, NULL);
2121 if (repo_path == NULL)
2122 fatal("realpath '%s'", optarg);
2123 break;
2124 case 'R':
2125 proc_id = PROC_REPO_READ;
2126 break;
2127 case 'v':
2128 if (verbosity < 3)
2129 verbosity++;
2130 break;
2131 case 'W':
2132 proc_id = PROC_REPO_WRITE;
2133 break;
2134 default:
2135 usage();
2139 argc -= optind;
2140 argv += optind;
2142 if (argc != 0)
2143 usage();
2145 if (geteuid())
2146 fatalx("need root privileges");
2148 log_init(daemonize ? 0 : 1, LOG_DAEMON);
2149 log_setverbose(verbosity);
2151 if (parse_config(confpath, proc_id, &gotd) != 0)
2152 return 1;
2154 if (proc_id == PROC_GOTD &&
2155 (gotd.nrepos == 0 || TAILQ_EMPTY(&gotd.repos)))
2156 fatalx("no repository defined in configuration file");
2158 pw = getpwnam(gotd.user_name);
2159 if (pw == NULL)
2160 fatal("getpwuid: user %s not found", gotd.user_name);
2162 if (pw->pw_uid == 0) {
2163 fatalx("cannot run %s as %s: the user running %s "
2164 "must not be the superuser",
2165 getprogname(), pw->pw_name, getprogname());
2168 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups) == -1)
2169 log_warnx("group membership list truncated");
2171 gr = match_group(groups, ngroups, gotd.unix_group_name);
2172 if (gr == NULL) {
2173 fatalx("cannot start %s: the user running %s "
2174 "must be a secondary member of group %s",
2175 getprogname(), getprogname(), gotd.unix_group_name);
2177 if (gr->gr_gid == pw->pw_gid) {
2178 fatalx("cannot start %s: the user running %s "
2179 "must be a secondary member of group %s, but "
2180 "%s is the user's primary group",
2181 getprogname(), getprogname(), gotd.unix_group_name,
2182 gotd.unix_group_name);
2185 if (proc_id == PROC_GOTD &&
2186 !got_path_is_absolute(gotd.unix_socket_path))
2187 fatalx("bad unix socket path \"%s\": must be an absolute path",
2188 gotd.unix_socket_path);
2190 if (noaction)
2191 return 0;
2193 if (proc_id == PROC_GOTD && verbosity) {
2194 log_info("socket: %s", gotd.unix_socket_path);
2195 log_info("user: %s", pw->pw_name);
2196 log_info("secondary group: %s", gr->gr_name);
2198 fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
2199 gr->gr_gid);
2200 if (fd == -1) {
2201 fatal("cannot listen on unix socket %s",
2202 gotd.unix_socket_path);
2206 if (proc_id == PROC_GOTD) {
2207 gotd.pid = getpid();
2208 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2209 start_repo_children(&gotd, argv0, confpath, daemonize,
2210 verbosity);
2211 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
2212 if (daemonize && daemon(0, 0) == -1)
2213 fatal("daemon");
2214 } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE) {
2215 error = got_repo_pack_fds_open(&pack_fds);
2216 if (error != NULL)
2217 fatalx("cannot open pack tempfiles: %s", error->msg);
2218 error = got_repo_temp_fds_open(&temp_fds);
2219 if (error != NULL)
2220 fatalx("cannot open pack tempfiles: %s", error->msg);
2221 if (repo_path == NULL)
2222 fatalx("repository path not specified");
2223 snprintf(title, sizeof(title), "%s %s",
2224 gotd_proc_names[proc_id], repo_path);
2225 if (chroot(repo_path) == -1)
2226 fatal("chroot");
2227 if (chdir("/") == -1)
2228 fatal("chdir(\"/\")");
2229 if (daemonize && daemon(1, 0) == -1)
2230 fatal("daemon");
2231 } else
2232 fatal("invalid process id %d", proc_id);
2234 setproctitle("%s", title);
2235 log_procinit(title);
2237 /* Drop root privileges. */
2238 if (setgid(pw->pw_gid) == -1)
2239 fatal("setgid %d failed", pw->pw_gid);
2240 if (setuid(pw->pw_uid) == -1)
2241 fatal("setuid %d failed", pw->pw_uid);
2243 event_init();
2245 switch (proc_id) {
2246 case PROC_GOTD:
2247 #ifndef PROFILE
2248 if (pledge("stdio rpath wpath cpath proc getpw sendfd recvfd "
2249 "fattr flock unix unveil", NULL) == -1)
2250 err(1, "pledge");
2251 #endif
2252 break;
2253 case PROC_REPO_READ:
2254 #ifndef PROFILE
2255 if (pledge("stdio rpath recvfd", NULL) == -1)
2256 err(1, "pledge");
2257 #endif
2258 repo_read_main(title, pack_fds, temp_fds);
2259 /* NOTREACHED */
2260 exit(0);
2261 case PROC_REPO_WRITE:
2262 #ifndef PROFILE
2263 if (pledge("stdio rpath recvfd", NULL) == -1)
2264 err(1, "pledge");
2265 #endif
2266 repo_write_main(title, pack_fds, temp_fds);
2267 /* NOTREACHED */
2268 exit(0);
2269 default:
2270 fatal("invalid process id %d", proc_id);
2273 if (proc_id != PROC_GOTD)
2274 fatal("invalid process id %d", proc_id);
2276 apply_unveil();
2278 signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
2279 signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
2280 signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
2281 signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
2282 signal(SIGPIPE, SIG_IGN);
2284 signal_add(&evsigint, NULL);
2285 signal_add(&evsigterm, NULL);
2286 signal_add(&evsighup, NULL);
2287 signal_add(&evsigusr1, NULL);
2289 event_set(&gotd.ev, fd, EV_READ | EV_PERSIST, gotd_accept, NULL);
2290 if (event_add(&gotd.ev, NULL))
2291 fatalx("event add");
2292 evtimer_set(&gotd.pause, gotd_accept_paused, NULL);
2294 event_dispatch();
2296 if (fd != -1)
2297 close(fd);
2298 if (pack_fds)
2299 got_repo_pack_fds_close(pack_fds);
2300 free(repo_path);
2301 return 0;