Blob


1 /*
2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 * Copyright (c) 2015 Ted Unangst <tedu@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/queue.h>
19 #include <sys/tree.h>
20 #include <sys/time.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <sys/socket.h>
24 #include <sys/un.h>
25 #include <sys/wait.h>
27 #include <fcntl.h>
28 #include <err.h>
29 #include <errno.h>
30 #include <event.h>
31 #include <limits.h>
32 #include <pwd.h>
33 #include <grp.h>
34 #include <imsg.h>
35 #include <sha1.h>
36 #include <signal.h>
37 #include <siphash.h>
38 #include <stdarg.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <syslog.h>
43 #include <unistd.h>
45 #include "got_error.h"
46 #include "got_opentemp.h"
47 #include "got_path.h"
48 #include "got_repository.h"
49 #include "got_object.h"
50 #include "got_reference.h"
52 #include "got_lib_delta.h"
53 #include "got_lib_object.h"
54 #include "got_lib_object_cache.h"
55 #include "got_lib_sha1.h"
56 #include "got_lib_gitproto.h"
57 #include "got_lib_pack.h"
58 #include "got_lib_repository.h"
60 #include "gotd.h"
61 #include "log.h"
62 #include "auth.h"
63 #include "repo_read.h"
64 #include "repo_write.h"
66 #ifndef nitems
67 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
68 #endif
70 struct gotd_client {
71 STAILQ_ENTRY(gotd_client) entry;
72 enum gotd_client_state state;
73 struct gotd_client_capability *capabilities;
74 size_t ncapa_alloc;
75 size_t ncapabilities;
76 uint32_t id;
77 int fd;
78 int delta_cache_fd;
79 struct gotd_imsgev iev;
80 struct event tmo;
81 uid_t euid;
82 gid_t egid;
83 struct gotd_child_proc *repo_read;
84 struct gotd_child_proc *repo_write;
85 char *packfile_path;
86 char *packidx_path;
87 int nref_updates;
88 };
89 STAILQ_HEAD(gotd_clients, gotd_client);
91 static struct gotd_clients gotd_clients[GOTD_CLIENT_TABLE_SIZE];
92 static SIPHASH_KEY clients_hash_key;
93 volatile int client_cnt;
94 static struct timeval timeout = { 3600, 0 };
95 static int inflight;
96 static struct gotd gotd;
98 void gotd_sighdlr(int sig, short event, void *arg);
99 static void gotd_shutdown(void);
101 __dead static void
102 usage()
104 fprintf(stderr, "usage: %s [-dv] [-f config-file]\n", getprogname());
105 exit(1);
108 static int
109 unix_socket_listen(const char *unix_socket_path, uid_t uid, gid_t gid)
111 struct sockaddr_un sun;
112 int fd = -1;
113 mode_t old_umask, mode;
115 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
116 if (fd == -1) {
117 log_warn("socket");
118 return -1;
121 sun.sun_family = AF_UNIX;
122 if (strlcpy(sun.sun_path, unix_socket_path,
123 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
124 log_warnx("%s: name too long", unix_socket_path);
125 close(fd);
126 return -1;
129 if (unlink(unix_socket_path) == -1) {
130 if (errno != ENOENT) {
131 log_warn("unlink %s", unix_socket_path);
132 close(fd);
133 return -1;
137 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
138 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
140 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
141 log_warn("bind: %s", unix_socket_path);
142 close(fd);
143 umask(old_umask);
144 return -1;
147 umask(old_umask);
149 if (chmod(unix_socket_path, mode) == -1) {
150 log_warn("chmod %o %s", mode, unix_socket_path);
151 close(fd);
152 unlink(unix_socket_path);
153 return -1;
156 if (chown(unix_socket_path, uid, gid) == -1) {
157 log_warn("chown %s uid=%d gid=%d", unix_socket_path, uid, gid);
158 close(fd);
159 unlink(unix_socket_path);
160 return -1;
163 if (listen(fd, GOTD_UNIX_SOCKET_BACKLOG) == -1) {
164 log_warn("listen");
165 close(fd);
166 unlink(unix_socket_path);
167 return -1;
170 return fd;
173 static struct group *
174 match_group(gid_t *groups, int ngroups, const char *unix_group_name)
176 struct group *gr;
177 int i;
179 for (i = 0; i < ngroups; i++) {
180 gr = getgrgid(groups[i]);
181 if (gr == NULL) {
182 log_warn("getgrgid %d", groups[i]);
183 continue;
185 if (strcmp(gr->gr_name, unix_group_name) == 0)
186 return gr;
189 return NULL;
192 static int
193 accept_reserve(int fd, struct sockaddr *addr, socklen_t *addrlen,
194 int reserve, volatile int *counter)
196 int ret;
198 if (getdtablecount() + reserve +
199 ((*counter + 1) * GOTD_FD_NEEDED) >= getdtablesize()) {
200 log_debug("inflight fds exceeded");
201 errno = EMFILE;
202 return -1;
205 if ((ret = accept4(fd, addr, addrlen,
206 SOCK_NONBLOCK | SOCK_CLOEXEC)) > -1) {
207 (*counter)++;
210 return ret;
213 static uint64_t
214 client_hash(uint32_t client_id)
216 return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
219 static void
220 add_client(struct gotd_client *client)
222 uint64_t slot = client_hash(client->id) % nitems(gotd_clients);
223 STAILQ_INSERT_HEAD(&gotd_clients[slot], client, entry);
224 client_cnt++;
227 static struct gotd_client *
228 find_client(uint32_t client_id)
230 uint64_t slot;
231 struct gotd_client *c;
233 slot = client_hash(client_id) % nitems(gotd_clients);
234 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
235 if (c->id == client_id)
236 return c;
239 return NULL;
242 static uint32_t
243 get_client_id(void)
245 int duplicate = 0;
246 uint32_t id;
248 do {
249 id = arc4random();
250 duplicate = (find_client(id) != NULL);
251 } while (duplicate || id == 0);
253 return id;
256 static struct gotd_child_proc *
257 get_client_proc(struct gotd_client *client)
259 if (client->repo_read && client->repo_write) {
260 fatalx("uid %d is reading and writing in the same session",
261 client->euid);
262 /* NOTREACHED */
265 if (client->repo_read)
266 return client->repo_read;
267 else if (client->repo_write)
268 return client->repo_write;
270 return NULL;
273 static int
274 client_is_reading(struct gotd_client *client)
276 return client->repo_read != NULL;
279 static int
280 client_is_writing(struct gotd_client *client)
282 return client->repo_write != NULL;
285 static const struct got_error *
286 ensure_client_is_reading(struct gotd_client *client)
288 if (!client_is_reading(client)) {
289 return got_error_fmt(GOT_ERR_BAD_PACKET,
290 "uid %d made a read-request but is not reading from "
291 "a repository", client->euid);
294 return NULL;
297 static const struct got_error *
298 ensure_client_is_writing(struct gotd_client *client)
300 if (!client_is_writing(client)) {
301 return got_error_fmt(GOT_ERR_BAD_PACKET,
302 "uid %d made a write-request but is not writing to "
303 "a repository", client->euid);
306 return NULL;
309 static const struct got_error *
310 ensure_client_is_not_writing(struct gotd_client *client)
312 if (client_is_writing(client)) {
313 return got_error_fmt(GOT_ERR_BAD_PACKET,
314 "uid %d made a read-request but is writing to "
315 "a repository", client->euid);
318 return NULL;
321 static const struct got_error *
322 ensure_client_is_not_reading(struct gotd_client *client)
324 if (client_is_reading(client)) {
325 return got_error_fmt(GOT_ERR_BAD_PACKET,
326 "uid %d made a write-request but is reading from "
327 "a repository", client->euid);
330 return NULL;
333 static void
334 disconnect(struct gotd_client *client)
336 struct gotd_imsg_disconnect idisconnect;
337 struct gotd_child_proc *proc = get_client_proc(client);
338 uint64_t slot;
340 log_debug("uid %d: disconnecting", client->euid);
342 idisconnect.client_id = client->id;
343 if (proc) {
344 if (gotd_imsg_compose_event(&proc->iev,
345 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
346 &idisconnect, sizeof(idisconnect)) == -1)
347 log_warn("imsg compose DISCONNECT");
349 slot = client_hash(client->id) % nitems(gotd_clients);
350 STAILQ_REMOVE(&gotd_clients[slot], client, gotd_client, entry);
351 imsg_clear(&client->iev.ibuf);
352 event_del(&client->iev.ev);
353 evtimer_del(&client->tmo);
354 close(client->fd);
355 if (client->delta_cache_fd != -1)
356 close(client->delta_cache_fd);
357 if (client->packfile_path) {
358 if (unlink(client->packfile_path) == -1 && errno != ENOENT)
359 log_warn("unlink %s: ", client->packfile_path);
360 free(client->packfile_path);
362 if (client->packidx_path) {
363 if (unlink(client->packidx_path) == -1 && errno != ENOENT)
364 log_warn("unlink %s: ", client->packidx_path);
365 free(client->packidx_path);
367 free(client->capabilities);
368 free(client);
369 inflight--;
370 client_cnt--;
373 static void
374 disconnect_on_error(struct gotd_client *client, const struct got_error *err)
376 struct imsgbuf ibuf;
378 log_warnx("uid %d: %s", client->euid, err->msg);
379 if (err->code != GOT_ERR_EOF) {
380 imsg_init(&ibuf, client->fd);
381 gotd_imsg_send_error(&ibuf, 0, PROC_GOTD, err);
382 imsg_clear(&ibuf);
384 disconnect(client);
387 static const struct got_error *
388 send_repo_info(struct gotd_imsgev *iev, struct gotd_repo *repo)
390 const struct got_error *err = NULL;
391 struct gotd_imsg_info_repo irepo;
393 memset(&irepo, 0, sizeof(irepo));
395 if (strlcpy(irepo.repo_name, repo->name, sizeof(irepo.repo_name))
396 >= sizeof(irepo.repo_name))
397 return got_error_msg(GOT_ERR_NO_SPACE, "repo name too long");
398 if (strlcpy(irepo.repo_path, repo->path, sizeof(irepo.repo_path))
399 >= sizeof(irepo.repo_path))
400 return got_error_msg(GOT_ERR_NO_SPACE, "repo path too long");
402 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_REPO, PROC_GOTD, -1,
403 &irepo, sizeof(irepo)) == -1) {
404 err = got_error_from_errno("imsg compose INFO_REPO");
405 if (err)
406 return err;
409 return NULL;
412 static const struct got_error *
413 send_capability(struct gotd_client_capability *capa, struct gotd_imsgev* iev)
415 const struct got_error *err = NULL;
416 struct gotd_imsg_capability icapa;
417 size_t len;
418 struct ibuf *wbuf;
420 memset(&icapa, 0, sizeof(icapa));
422 icapa.key_len = strlen(capa->key);
423 len = sizeof(icapa) + icapa.key_len;
424 if (capa->value) {
425 icapa.value_len = strlen(capa->value);
426 len += icapa.value_len;
429 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_CAPABILITY, 0, 0, len);
430 if (wbuf == NULL) {
431 err = got_error_from_errno("imsg_create CAPABILITY");
432 return err;
435 if (imsg_add(wbuf, &icapa, sizeof(icapa)) == -1)
436 return got_error_from_errno("imsg_add CAPABILITY");
437 if (imsg_add(wbuf, capa->key, icapa.key_len) == -1)
438 return got_error_from_errno("imsg_add CAPABILITY");
439 if (capa->value) {
440 if (imsg_add(wbuf, capa->value, icapa.value_len) == -1)
441 return got_error_from_errno("imsg_add CAPABILITY");
444 wbuf->fd = -1;
445 imsg_close(&iev->ibuf, wbuf);
447 gotd_imsg_event_add(iev);
449 return NULL;
452 static const struct got_error *
453 send_client_info(struct gotd_imsgev *iev, struct gotd_client *client)
455 const struct got_error *err = NULL;
456 struct gotd_imsg_info_client iclient;
457 struct gotd_child_proc *proc;
458 size_t i;
460 memset(&iclient, 0, sizeof(iclient));
461 iclient.euid = client->euid;
462 iclient.egid = client->egid;
464 proc = get_client_proc(client);
465 if (proc) {
466 if (strlcpy(iclient.repo_name, proc->chroot_path,
467 sizeof(iclient.repo_name)) >= sizeof(iclient.repo_name)) {
468 return got_error_msg(GOT_ERR_NO_SPACE,
469 "repo name too long");
471 if (client_is_writing(client))
472 iclient.is_writing = 1;
475 iclient.state = client->state;
476 iclient.ncapabilities = client->ncapabilities;
478 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_CLIENT, PROC_GOTD, -1,
479 &iclient, sizeof(iclient)) == -1) {
480 err = got_error_from_errno("imsg compose INFO_CLIENT");
481 if (err)
482 return err;
485 for (i = 0; i < client->ncapabilities; i++) {
486 struct gotd_client_capability *capa;
487 capa = &client->capabilities[i];
488 err = send_capability(capa, iev);
489 if (err)
490 return err;
493 return NULL;
496 static const struct got_error *
497 send_info(struct gotd_client *client)
499 const struct got_error *err = NULL;
500 struct gotd_imsg_info info;
501 uint64_t slot;
502 struct gotd_repo *repo;
504 info.pid = gotd.pid;
505 info.verbosity = gotd.verbosity;
506 info.nrepos = gotd.nrepos;
507 info.nclients = client_cnt - 1;
509 if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_INFO, PROC_GOTD, -1,
510 &info, sizeof(info)) == -1) {
511 err = got_error_from_errno("imsg compose INFO");
512 if (err)
513 return err;
516 TAILQ_FOREACH(repo, &gotd.repos, entry) {
517 err = send_repo_info(&client->iev, repo);
518 if (err)
519 return err;
522 for (slot = 0; slot < nitems(gotd_clients); slot++) {
523 struct gotd_client *c;
524 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
525 if (c->id == client->id)
526 continue;
527 err = send_client_info(&client->iev, c);
528 if (err)
529 return err;
533 return NULL;
536 static const struct got_error *
537 stop_gotd(struct gotd_client *client)
540 if (client->euid != 0)
541 return got_error_set_errno(EPERM, "stop");
543 gotd_shutdown();
544 /* NOTREACHED */
545 return NULL;
548 static struct gotd_repo *
549 find_repo_by_name(const char *repo_name)
551 struct gotd_repo *repo;
552 size_t namelen;
554 TAILQ_FOREACH(repo, &gotd.repos, entry) {
555 namelen = strlen(repo->name);
556 if (strncmp(repo->name, repo_name, namelen) != 0)
557 continue;
558 if (repo_name[namelen] == '\0' ||
559 strcmp(&repo_name[namelen], ".git") == 0)
560 return repo;
563 return NULL;
566 static struct gotd_child_proc *
567 find_proc_by_repo_name(enum gotd_procid proc_id, const char *repo_name)
569 struct gotd_child_proc *proc;
570 int i;
571 size_t namelen;
573 for (i = 0; i < gotd.nprocs; i++) {
574 proc = &gotd.procs[i];
575 if (proc->type != proc_id)
576 continue;
577 namelen = strlen(proc->repo_name);
578 if (strncmp(proc->repo_name, repo_name, namelen) != 0)
579 continue;
580 if (repo_name[namelen] == '\0' ||
581 strcmp(&repo_name[namelen], ".git") == 0)
582 return proc;
585 return NULL;
588 static struct gotd_child_proc *
589 find_proc_by_fd(int fd)
591 struct gotd_child_proc *proc;
592 int i;
594 for (i = 0; i < gotd.nprocs; i++) {
595 proc = &gotd.procs[i];
596 if (proc->iev.ibuf.fd == fd)
597 return proc;
600 return NULL;
603 static const struct got_error *
604 forward_list_refs_request(struct gotd_client *client, struct imsg *imsg)
606 const struct got_error *err;
607 struct gotd_imsg_list_refs ireq;
608 struct gotd_imsg_list_refs_internal ilref;
609 struct gotd_repo *repo = NULL;
610 struct gotd_child_proc *proc = NULL;
611 size_t datalen;
612 int fd = -1;
614 log_debug("list-refs request from uid %d", client->euid);
616 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
617 if (datalen != sizeof(ireq))
618 return got_error(GOT_ERR_PRIVSEP_LEN);
620 memcpy(&ireq, imsg->data, datalen);
622 memset(&ilref, 0, sizeof(ilref));
623 ilref.client_id = client->id;
625 if (ireq.client_is_reading) {
626 err = ensure_client_is_not_writing(client);
627 if (err)
628 return err;
629 repo = find_repo_by_name(ireq.repo_name);
630 if (repo == NULL)
631 return got_error(GOT_ERR_NOT_GIT_REPO);
632 err = gotd_auth_check(&repo->rules, repo->name,
633 gotd.groups, gotd.ngroups, client->euid, client->egid,
634 GOTD_AUTH_READ);
635 if (err)
636 return err;
637 client->repo_read = find_proc_by_repo_name(PROC_REPO_READ,
638 ireq.repo_name);
639 if (client->repo_read == NULL)
640 return got_error(GOT_ERR_NOT_GIT_REPO);
641 } else {
642 err = ensure_client_is_not_reading(client);
643 if (err)
644 return err;
645 repo = find_repo_by_name(ireq.repo_name);
646 if (repo == NULL)
647 return got_error(GOT_ERR_NOT_GIT_REPO);
648 err = gotd_auth_check(&repo->rules, repo->name,
649 gotd.groups, gotd.ngroups, client->euid, client->egid,
650 GOTD_AUTH_READ | GOTD_AUTH_WRITE);
651 if (err)
652 return err;
653 client->repo_write = find_proc_by_repo_name(PROC_REPO_WRITE,
654 ireq.repo_name);
655 if (client->repo_write == NULL)
656 return got_error(GOT_ERR_NOT_GIT_REPO);
659 fd = dup(client->fd);
660 if (fd == -1)
661 return got_error_from_errno("dup");
663 proc = get_client_proc(client);
664 if (proc == NULL)
665 fatalx("no process found for uid %d", client->euid);
666 if (gotd_imsg_compose_event(&proc->iev,
667 GOTD_IMSG_LIST_REFS_INTERNAL, PROC_GOTD, fd,
668 &ilref, sizeof(ilref)) == -1) {
669 err = got_error_from_errno("imsg compose WANT");
670 close(fd);
671 return err;
674 return NULL;
677 static const struct got_error *
678 forward_want(struct gotd_client *client, struct imsg *imsg)
680 struct gotd_imsg_want ireq;
681 struct gotd_imsg_want iwant;
682 size_t datalen;
684 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
685 if (datalen != sizeof(ireq))
686 return got_error(GOT_ERR_PRIVSEP_LEN);
688 memcpy(&ireq, imsg->data, datalen);
690 memset(&iwant, 0, sizeof(iwant));
691 memcpy(iwant.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
692 iwant.client_id = client->id;
694 if (gotd_imsg_compose_event(&client->repo_read->iev, GOTD_IMSG_WANT,
695 PROC_GOTD, -1, &iwant, sizeof(iwant)) == -1)
696 return got_error_from_errno("imsg compose WANT");
698 return NULL;
701 static const struct got_error *
702 forward_ref_update(struct gotd_client *client, struct imsg *imsg)
704 const struct got_error *err = NULL;
705 struct gotd_imsg_ref_update ireq;
706 struct gotd_imsg_ref_update *iref = NULL;
707 size_t datalen;
709 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
710 if (datalen < sizeof(ireq))
711 return got_error(GOT_ERR_PRIVSEP_LEN);
712 memcpy(&ireq, imsg->data, sizeof(ireq));
713 if (datalen != sizeof(ireq) + ireq.name_len)
714 return got_error(GOT_ERR_PRIVSEP_LEN);
716 iref = malloc(datalen);
717 if (iref == NULL)
718 return got_error_from_errno("malloc");
719 memcpy(iref, imsg->data, datalen);
721 iref->client_id = client->id;
722 if (gotd_imsg_compose_event(&client->repo_write->iev,
723 GOTD_IMSG_REF_UPDATE, PROC_GOTD, -1, iref, datalen) == -1)
724 err = got_error_from_errno("imsg compose REF_UPDATE");
725 free(iref);
726 return err;
729 static const struct got_error *
730 forward_have(struct gotd_client *client, struct imsg *imsg)
732 struct gotd_imsg_have ireq;
733 struct gotd_imsg_have ihave;
734 size_t datalen;
736 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
737 if (datalen != sizeof(ireq))
738 return got_error(GOT_ERR_PRIVSEP_LEN);
740 memcpy(&ireq, imsg->data, datalen);
742 memset(&ihave, 0, sizeof(ihave));
743 memcpy(ihave.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
744 ihave.client_id = client->id;
746 if (gotd_imsg_compose_event(&client->repo_read->iev, GOTD_IMSG_HAVE,
747 PROC_GOTD, -1, &ihave, sizeof(ihave)) == -1)
748 return got_error_from_errno("imsg compose HAVE");
750 return NULL;
753 static int
754 client_has_capability(struct gotd_client *client, const char *capastr)
756 struct gotd_client_capability *capa;
757 size_t i;
759 if (client->ncapabilities == 0)
760 return 0;
762 for (i = 0; i < client->ncapabilities; i++) {
763 capa = &client->capabilities[i];
764 if (strcmp(capa->key, capastr) == 0)
765 return 1;
768 return 0;
771 static const struct got_error *
772 recv_capabilities(struct gotd_client *client, struct imsg *imsg)
774 struct gotd_imsg_capabilities icapas;
775 size_t datalen;
777 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
778 if (datalen != sizeof(icapas))
779 return got_error(GOT_ERR_PRIVSEP_LEN);
780 memcpy(&icapas, imsg->data, sizeof(icapas));
782 client->ncapa_alloc = icapas.ncapabilities;
783 client->capabilities = calloc(client->ncapa_alloc,
784 sizeof(*client->capabilities));
785 if (client->capabilities == NULL) {
786 client->ncapa_alloc = 0;
787 return got_error_from_errno("calloc");
790 log_debug("expecting %zu capabilities from uid %d",
791 client->ncapa_alloc, client->euid);
792 return NULL;
795 static const struct got_error *
796 recv_capability(struct gotd_client *client, struct imsg *imsg)
798 struct gotd_imsg_capability icapa;
799 struct gotd_client_capability *capa;
800 size_t datalen;
801 char *key, *value = NULL;
803 if (client->capabilities == NULL ||
804 client->ncapabilities >= client->ncapa_alloc) {
805 return got_error_msg(GOT_ERR_BAD_REQUEST,
806 "unexpected capability received");
809 memset(&icapa, 0, sizeof(icapa));
811 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
812 if (datalen < sizeof(icapa))
813 return got_error(GOT_ERR_PRIVSEP_LEN);
814 memcpy(&icapa, imsg->data, sizeof(icapa));
816 if (datalen != sizeof(icapa) + icapa.key_len + icapa.value_len)
817 return got_error(GOT_ERR_PRIVSEP_LEN);
819 key = malloc(icapa.key_len + 1);
820 if (key == NULL)
821 return got_error_from_errno("malloc");
822 if (icapa.value_len > 0) {
823 value = malloc(icapa.value_len + 1);
824 if (value == NULL) {
825 free(key);
826 return got_error_from_errno("malloc");
830 memcpy(key, imsg->data + sizeof(icapa), icapa.key_len);
831 key[icapa.key_len] = '\0';
832 if (value) {
833 memcpy(value, imsg->data + sizeof(icapa) + icapa.key_len,
834 icapa.value_len);
835 value[icapa.value_len] = '\0';
838 capa = &client->capabilities[client->ncapabilities++];
839 capa->key = key;
840 capa->value = value;
842 if (value)
843 log_debug("uid %d: capability %s=%s", client->euid, key, value);
844 else
845 log_debug("uid %d: capability %s", client->euid, key);
847 return NULL;
850 static const struct got_error *
851 send_packfile(struct gotd_client *client)
853 const struct got_error *err = NULL;
854 struct gotd_imsg_send_packfile ipack;
855 struct gotd_imsg_packfile_pipe ipipe;
856 int pipe[2];
858 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
859 return got_error_from_errno("socketpair");
861 memset(&ipack, 0, sizeof(ipack));
862 memset(&ipipe, 0, sizeof(ipipe));
864 ipack.client_id = client->id;
865 if (client_has_capability(client, GOT_CAPA_SIDE_BAND_64K))
866 ipack.report_progress = 1;
868 client->delta_cache_fd = got_opentempfd();
869 if (client->delta_cache_fd == -1)
870 return got_error_from_errno("got_opentempfd");
872 if (gotd_imsg_compose_event(&client->repo_read->iev,
873 GOTD_IMSG_SEND_PACKFILE, PROC_GOTD, client->delta_cache_fd,
874 &ipack, sizeof(ipack)) == -1) {
875 err = got_error_from_errno("imsg compose SEND_PACKFILE");
876 close(pipe[0]);
877 close(pipe[1]);
878 return err;
881 ipipe.client_id = client->id;
883 /* Send pack pipe end 0 to repo_read. */
884 if (gotd_imsg_compose_event(&client->repo_read->iev,
885 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[0],
886 &ipipe, sizeof(ipipe)) == -1) {
887 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
888 close(pipe[1]);
889 return err;
892 /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
893 if (gotd_imsg_compose_event(&client->iev,
894 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1], NULL, 0) == -1)
895 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
897 return err;
900 static const struct got_error *
901 recv_packfile(struct gotd_client *client)
903 const struct got_error *err = NULL;
904 struct gotd_imsg_recv_packfile ipack;
905 struct gotd_imsg_packfile_pipe ipipe;
906 struct gotd_imsg_packidx_file ifile;
907 char *basepath = NULL, *pack_path = NULL, *idx_path = NULL;
908 int packfd = -1, idxfd = -1;
909 int pipe[2] = { -1, -1 };
911 if (client->packfile_path) {
912 return got_error_fmt(GOT_ERR_PRIVSEP_MSG,
913 "uid %d already has a pack file", client->euid);
916 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
917 return got_error_from_errno("socketpair");
919 memset(&ipipe, 0, sizeof(ipipe));
920 ipipe.client_id = client->id;
922 if (gotd_imsg_compose_event(&client->repo_write->iev,
923 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[0],
924 &ipipe, sizeof(ipipe)) == -1) {
925 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
926 pipe[0] = -1;
927 goto done;
929 pipe[0] = -1;
931 if (gotd_imsg_compose_event(&client->repo_write->iev,
932 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1],
933 &ipipe, sizeof(ipipe)) == -1)
934 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
935 pipe[1] = -1;
937 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.pack",
938 client->repo_write->chroot_path, GOT_OBJECTS_PACK_DIR,
939 client->euid) == -1) {
940 err = got_error_from_errno("asprintf");
941 goto done;
944 err = got_opentemp_named_fd(&pack_path, &packfd, basepath, "");
945 if (err)
946 goto done;
948 free(basepath);
949 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.idx",
950 client->repo_write->chroot_path, GOT_OBJECTS_PACK_DIR,
951 client->euid) == -1) {
952 err = got_error_from_errno("asprintf");
953 basepath = NULL;
954 goto done;
956 err = got_opentemp_named_fd(&idx_path, &idxfd, basepath, "");
957 if (err)
958 goto done;
960 memset(&ifile, 0, sizeof(ifile));
961 ifile.client_id = client->id;
962 if (gotd_imsg_compose_event(&client->repo_write->iev,
963 GOTD_IMSG_PACKIDX_FILE, PROC_GOTD, idxfd,
964 &ifile, sizeof(ifile)) == -1) {
965 err = got_error_from_errno("imsg compose PACKIDX_FILE");
966 idxfd = -1;
967 goto done;
969 idxfd = -1;
971 memset(&ipack, 0, sizeof(ipack));
972 ipack.client_id = client->id;
973 if (client_has_capability(client, GOT_CAPA_REPORT_STATUS))
974 ipack.report_status = 1;
976 if (gotd_imsg_compose_event(&client->repo_write->iev,
977 GOTD_IMSG_RECV_PACKFILE, PROC_GOTD, packfd,
978 &ipack, sizeof(ipack)) == -1) {
979 err = got_error_from_errno("imsg compose RECV_PACKFILE");
980 packfd = -1;
981 goto done;
983 packfd = -1;
985 done:
986 free(basepath);
987 if (pipe[0] != -1 && close(pipe[0]) == -1 && err == NULL)
988 err = got_error_from_errno("close");
989 if (pipe[1] != -1 && close(pipe[1]) == -1 && err == NULL)
990 err = got_error_from_errno("close");
991 if (packfd != -1 && close(packfd) == -1 && err == NULL)
992 err = got_error_from_errno("close");
993 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
994 err = got_error_from_errno("close");
995 if (err) {
996 free(pack_path);
997 free(idx_path);
998 } else {
999 client->packfile_path = pack_path;
1000 client->packidx_path = idx_path;
1002 return err;
1005 static void
1006 gotd_request(int fd, short events, void *arg)
1008 struct gotd_imsgev *iev = arg;
1009 struct imsgbuf *ibuf = &iev->ibuf;
1010 struct gotd_client *client = iev->handler_arg;
1011 const struct got_error *err = NULL;
1012 struct imsg imsg;
1013 ssize_t n;
1015 if (events & EV_WRITE) {
1016 while (ibuf->w.queued) {
1017 n = msgbuf_write(&ibuf->w);
1018 if (n == -1 && errno == EPIPE) {
1020 * The client has closed its socket.
1021 * This can happen when Git clients are
1022 * done sending pack file data.
1024 msgbuf_clear(&ibuf->w);
1025 continue;
1026 } else if (n == -1 && errno != EAGAIN) {
1027 err = got_error_from_errno("imsg_flush");
1028 disconnect_on_error(client, err);
1029 return;
1031 if (n == 0) {
1032 /* Connection closed. */
1033 err = got_error(GOT_ERR_EOF);
1034 disconnect_on_error(client, err);
1035 return;
1039 /* Disconnect gotctl(8) now that messages have been sent. */
1040 if (!client_is_reading(client) && !client_is_writing(client)) {
1041 disconnect(client);
1042 return;
1046 if ((events & EV_READ) == 0)
1047 return;
1049 memset(&imsg, 0, sizeof(imsg));
1051 while (err == NULL) {
1052 err = gotd_imsg_recv(&imsg, ibuf, 0);
1053 if (err) {
1054 if (err->code == GOT_ERR_PRIVSEP_READ)
1055 err = NULL;
1056 break;
1059 evtimer_del(&client->tmo);
1061 switch (imsg.hdr.type) {
1062 case GOTD_IMSG_INFO:
1063 err = send_info(client);
1064 break;
1065 case GOTD_IMSG_STOP:
1066 err = stop_gotd(client);
1067 break;
1068 case GOTD_IMSG_LIST_REFS:
1069 if (client->state != GOTD_STATE_EXPECT_LIST_REFS) {
1070 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1071 "unexpected list-refs request received");
1072 break;
1074 err = forward_list_refs_request(client, &imsg);
1075 if (err)
1076 break;
1077 client->state = GOTD_STATE_EXPECT_CAPABILITIES;
1078 log_debug("uid %d: expecting capabilities",
1079 client->euid);
1080 break;
1081 case GOTD_IMSG_CAPABILITIES:
1082 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1083 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1084 "unexpected capabilities received");
1085 break;
1087 log_debug("receiving capabilities from uid %d",
1088 client->euid);
1089 err = recv_capabilities(client, &imsg);
1090 break;
1091 case GOTD_IMSG_CAPABILITY:
1092 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1093 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1094 "unexpected capability received");
1095 break;
1097 err = recv_capability(client, &imsg);
1098 if (err || client->ncapabilities < client->ncapa_alloc)
1099 break;
1100 if (client_is_reading(client)) {
1101 client->state = GOTD_STATE_EXPECT_WANT;
1102 log_debug("uid %d: expecting want-lines",
1103 client->euid);
1104 } else if (client_is_writing(client)) {
1105 client->state = GOTD_STATE_EXPECT_REF_UPDATE;
1106 log_debug("uid %d: expecting ref-update-lines",
1107 client->euid);
1108 } else
1109 fatalx("client %d is both reading and writing",
1110 client->euid);
1111 break;
1112 case GOTD_IMSG_WANT:
1113 if (client->state != GOTD_STATE_EXPECT_WANT) {
1114 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1115 "unexpected want-line received");
1116 break;
1118 log_debug("received want-line from uid %d",
1119 client->euid);
1120 err = ensure_client_is_reading(client);
1121 if (err)
1122 break;
1123 err = forward_want(client, &imsg);
1124 break;
1125 case GOTD_IMSG_REF_UPDATE:
1126 if (client->state != GOTD_STATE_EXPECT_REF_UPDATE) {
1127 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1128 "unexpected ref-update-line received");
1129 break;
1131 log_debug("received ref-update-line from uid %d",
1132 client->euid);
1133 err = ensure_client_is_writing(client);
1134 if (err)
1135 break;
1136 err = forward_ref_update(client, &imsg);
1137 if (err)
1138 break;
1139 client->state = GOTD_STATE_EXPECT_MORE_REF_UPDATES;
1140 break;
1141 case GOTD_IMSG_HAVE:
1142 if (client->state != GOTD_STATE_EXPECT_HAVE) {
1143 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1144 "unexpected have-line received");
1145 break;
1147 log_debug("received have-line from uid %d",
1148 client->euid);
1149 err = ensure_client_is_reading(client);
1150 if (err)
1151 break;
1152 err = forward_have(client, &imsg);
1153 if (err)
1154 break;
1155 break;
1156 case GOTD_IMSG_FLUSH:
1157 if (client->state == GOTD_STATE_EXPECT_WANT ||
1158 client->state == GOTD_STATE_EXPECT_HAVE) {
1159 err = ensure_client_is_reading(client);
1160 if (err)
1161 break;
1162 } else if (client->state ==
1163 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1164 err = ensure_client_is_writing(client);
1165 if (err)
1166 break;
1167 } else {
1168 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1169 "unexpected flush-pkt received");
1170 break;
1172 log_debug("received flush-pkt from uid %d",
1173 client->euid);
1174 if (client->state == GOTD_STATE_EXPECT_WANT) {
1175 client->state = GOTD_STATE_EXPECT_HAVE;
1176 log_debug("uid %d: expecting have-lines",
1177 client->euid);
1178 } else if (client->state == GOTD_STATE_EXPECT_HAVE) {
1179 client->state = GOTD_STATE_EXPECT_DONE;
1180 log_debug("uid %d: expecting 'done'",
1181 client->euid);
1182 } else if (client->state ==
1183 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1184 client->state = GOTD_STATE_EXPECT_PACKFILE;
1185 log_debug("uid %d: expecting packfile",
1186 client->euid);
1187 err = recv_packfile(client);
1188 } else {
1189 /* should not happen, see above */
1190 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1191 "unexpected client state");
1192 break;
1194 break;
1195 case GOTD_IMSG_DONE:
1196 if (client->state != GOTD_STATE_EXPECT_HAVE &&
1197 client->state != GOTD_STATE_EXPECT_DONE) {
1198 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1199 "unexpected flush-pkt received");
1200 break;
1202 log_debug("received 'done' from uid %d", client->euid);
1203 err = ensure_client_is_reading(client);
1204 if (err)
1205 break;
1206 client->state = GOTD_STATE_DONE;
1207 err = send_packfile(client);
1208 break;
1209 default:
1210 err = got_error(GOT_ERR_PRIVSEP_MSG);
1211 break;
1214 imsg_free(&imsg);
1217 if (err) {
1218 if (err->code != GOT_ERR_EOF ||
1219 client->state != GOTD_STATE_EXPECT_PACKFILE)
1220 disconnect_on_error(client, err);
1221 } else {
1222 gotd_imsg_event_add(&client->iev);
1223 evtimer_add(&client->tmo, &timeout);
1227 static void
1228 gotd_request_timeout(int fd, short events, void *arg)
1230 struct gotd_client *client = arg;
1232 log_debug("disconnecting uid %d due to timeout", client->euid);
1233 disconnect(client);
1236 static void
1237 gotd_accept(int fd, short event, void *arg)
1239 struct sockaddr_storage ss;
1240 struct timeval backoff;
1241 socklen_t len;
1242 int s = -1;
1243 struct gotd_client *client = NULL;
1244 uid_t euid;
1245 gid_t egid;
1247 backoff.tv_sec = 1;
1248 backoff.tv_usec = 0;
1250 if (event_add(&gotd.ev, NULL) == -1) {
1251 log_warn("event_add");
1252 return;
1254 if (event & EV_TIMEOUT)
1255 return;
1257 len = sizeof(ss);
1259 s = accept_reserve(fd, (struct sockaddr *)&ss, &len, GOTD_FD_RESERVE,
1260 &inflight);
1262 if (s == -1) {
1263 switch (errno) {
1264 case EINTR:
1265 case EWOULDBLOCK:
1266 case ECONNABORTED:
1267 return;
1268 case EMFILE:
1269 case ENFILE:
1270 event_del(&gotd.ev);
1271 evtimer_add(&gotd.pause, &backoff);
1272 return;
1273 default:
1274 log_warn("%s: accept", __func__);
1275 return;
1279 if (client_cnt >= GOTD_MAXCLIENTS)
1280 goto err;
1282 if (getpeereid(s, &euid, &egid) == -1) {
1283 log_warn("%s: getpeereid", __func__);
1284 goto err;
1287 client = calloc(1, sizeof(*client));
1288 if (client == NULL) {
1289 log_warn("%s: calloc", __func__);
1290 goto err;
1293 client->state = GOTD_STATE_EXPECT_LIST_REFS;
1294 client->id = get_client_id();
1295 client->fd = s;
1296 s = -1;
1297 client->delta_cache_fd = -1;
1298 client->euid = euid;
1299 client->egid = egid;
1300 client->nref_updates = -1;
1302 imsg_init(&client->iev.ibuf, client->fd);
1303 client->iev.handler = gotd_request;
1304 client->iev.events = EV_READ;
1305 client->iev.handler_arg = client;
1307 event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
1308 &client->iev);
1309 gotd_imsg_event_add(&client->iev);
1311 evtimer_set(&client->tmo, gotd_request_timeout, client);
1313 add_client(client);
1314 log_debug("%s: new client uid %d connected on fd %d", __func__,
1315 client->euid, client->fd);
1316 return;
1317 err:
1318 inflight--;
1319 if (s != -1)
1320 close(s);
1321 free(client);
1324 static void
1325 gotd_accept_paused(int fd, short event, void *arg)
1327 event_add(&gotd.ev, NULL);
1330 static const char *gotd_proc_names[PROC_MAX] = {
1331 "parent",
1332 "repo_read",
1333 "repo_write"
1336 static struct gotd_child_proc *
1337 get_proc_for_pid(pid_t pid)
1339 struct gotd_child_proc *proc;
1340 int i;
1342 for (i = 0; i < gotd.nprocs; i++) {
1343 proc = &gotd.procs[i];
1344 if (proc->pid == pid)
1345 return proc;
1348 return NULL;
1351 static void
1352 kill_proc(struct gotd_child_proc *proc, int fatal)
1354 if (fatal) {
1355 log_warnx("sending SIGKILL to PID %d", proc->pid);
1356 kill(proc->pid, SIGKILL);
1357 } else
1358 kill(proc->pid, SIGTERM);
1361 static void
1362 gotd_shutdown(void)
1364 pid_t pid;
1365 int status, i;
1366 struct gotd_child_proc *proc;
1368 for (i = 0; i < gotd.nprocs; i++) {
1369 proc = &gotd.procs[i];
1370 msgbuf_clear(&proc->iev.ibuf.w);
1371 close(proc->iev.ibuf.fd);
1372 kill_proc(proc, 0);
1375 log_debug("waiting for children to terminate");
1376 do {
1377 pid = wait(&status);
1378 if (pid == -1) {
1379 if (errno != EINTR && errno != ECHILD)
1380 fatal("wait");
1381 } else if (WIFSIGNALED(status)) {
1382 proc = get_proc_for_pid(pid);
1383 log_warnx("%s %s child process terminated; signal %d",
1384 proc ? gotd_proc_names[proc->type] : "",
1385 proc ? proc->chroot_path : "", WTERMSIG(status));
1387 } while (pid != -1 || (pid == -1 && errno == EINTR));
1389 log_info("terminating");
1390 exit(0);
1393 void
1394 gotd_sighdlr(int sig, short event, void *arg)
1397 * Normal signal handler rules don't apply because libevent
1398 * decouples for us.
1401 switch (sig) {
1402 case SIGHUP:
1403 log_info("%s: ignoring SIGHUP", __func__);
1404 break;
1405 case SIGUSR1:
1406 log_info("%s: ignoring SIGUSR1", __func__);
1407 break;
1408 case SIGTERM:
1409 case SIGINT:
1410 gotd_shutdown();
1411 log_warnx("gotd terminating");
1412 exit(0);
1413 break;
1414 default:
1415 fatalx("unexpected signal");
1419 static const struct got_error *
1420 ensure_proc_is_reading(struct gotd_client *client,
1421 struct gotd_child_proc *proc)
1423 if (!client_is_reading(client)) {
1424 kill_proc(proc, 1);
1425 return got_error_fmt(GOT_ERR_BAD_PACKET,
1426 "PID %d handled a read-request for uid %d but this "
1427 "user is not reading from a repository", proc->pid,
1428 client->euid);
1431 return NULL;
1434 static const struct got_error *
1435 ensure_proc_is_writing(struct gotd_client *client,
1436 struct gotd_child_proc *proc)
1438 if (!client_is_writing(client)) {
1439 kill_proc(proc, 1);
1440 return got_error_fmt(GOT_ERR_BAD_PACKET,
1441 "PID %d handled a write-request for uid %d but this "
1442 "user is not writing to a repository", proc->pid,
1443 client->euid);
1446 return NULL;
1449 static int
1450 verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
1451 struct imsg *imsg)
1453 const struct got_error *err;
1454 struct gotd_child_proc *client_proc;
1455 int ret = 0;
1457 client_proc = get_client_proc(client);
1458 if (client_proc == NULL)
1459 fatalx("no process found for uid %d", client->euid);
1461 if (proc->pid != client_proc->pid) {
1462 kill_proc(proc, 1);
1463 log_warnx("received message from PID %d for uid %d, while "
1464 "PID %d is the process serving this user",
1465 proc->pid, client->euid, client_proc->pid);
1466 return 0;
1469 switch (imsg->hdr.type) {
1470 case GOTD_IMSG_ERROR:
1471 ret = 1;
1472 break;
1473 case GOTD_IMSG_PACKFILE_DONE:
1474 err = ensure_proc_is_reading(client, proc);
1475 if (err)
1476 log_warnx("uid %d: %s", client->euid, err->msg);
1477 else
1478 ret = 1;
1479 break;
1480 case GOTD_IMSG_PACKFILE_INSTALL:
1481 case GOTD_IMSG_REF_UPDATES_START:
1482 case GOTD_IMSG_REF_UPDATE:
1483 err = ensure_proc_is_writing(client, proc);
1484 if (err)
1485 log_warnx("uid %d: %s", client->euid, err->msg);
1486 else
1487 ret = 1;
1488 break;
1489 default:
1490 log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
1491 break;
1494 return ret;
1497 static const struct got_error *
1498 recv_packfile_done(uint32_t *client_id, struct imsg *imsg)
1500 struct gotd_imsg_packfile_done idone;
1501 size_t datalen;
1503 log_debug("packfile-done received");
1505 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1506 if (datalen != sizeof(idone))
1507 return got_error(GOT_ERR_PRIVSEP_LEN);
1508 memcpy(&idone, imsg->data, sizeof(idone));
1510 *client_id = idone.client_id;
1511 return NULL;
1514 static const struct got_error *
1515 recv_packfile_install(uint32_t *client_id, struct imsg *imsg)
1517 struct gotd_imsg_packfile_install inst;
1518 size_t datalen;
1520 log_debug("packfile-install received");
1522 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1523 if (datalen != sizeof(inst))
1524 return got_error(GOT_ERR_PRIVSEP_LEN);
1525 memcpy(&inst, imsg->data, sizeof(inst));
1527 *client_id = inst.client_id;
1528 return NULL;
1531 static const struct got_error *
1532 recv_ref_updates_start(uint32_t *client_id, struct imsg *imsg)
1534 struct gotd_imsg_ref_updates_start istart;
1535 size_t datalen;
1537 log_debug("ref-updates-start received");
1539 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1540 if (datalen != sizeof(istart))
1541 return got_error(GOT_ERR_PRIVSEP_LEN);
1542 memcpy(&istart, imsg->data, sizeof(istart));
1544 *client_id = istart.client_id;
1545 return NULL;
1548 static const struct got_error *
1549 recv_ref_update(uint32_t *client_id, struct imsg *imsg)
1551 struct gotd_imsg_ref_update iref;
1552 size_t datalen;
1554 log_debug("ref-update received");
1556 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1557 if (datalen < sizeof(iref))
1558 return got_error(GOT_ERR_PRIVSEP_LEN);
1559 memcpy(&iref, imsg->data, sizeof(iref));
1561 *client_id = iref.client_id;
1562 return NULL;
1565 static const struct got_error *
1566 send_ref_update_ok(struct gotd_client *client,
1567 struct gotd_imsg_ref_update *iref, const char *refname)
1569 struct gotd_imsg_ref_update_ok iok;
1570 struct ibuf *wbuf;
1571 size_t len;
1573 memset(&iok, 0, sizeof(iok));
1574 iok.client_id = client->id;
1575 memcpy(iok.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1576 memcpy(iok.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1577 iok.name_len = strlen(refname);
1579 len = sizeof(iok) + iok.name_len;
1580 wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_OK,
1581 PROC_GOTD, gotd.pid, len);
1582 if (wbuf == NULL)
1583 return got_error_from_errno("imsg_create REF_UPDATE_OK");
1585 if (imsg_add(wbuf, &iok, sizeof(iok)) == -1)
1586 return got_error_from_errno("imsg_add REF_UPDATE_OK");
1587 if (imsg_add(wbuf, refname, iok.name_len) == -1)
1588 return got_error_from_errno("imsg_add REF_UPDATE_OK");
1590 wbuf->fd = -1;
1591 imsg_close(&client->iev.ibuf, wbuf);
1592 gotd_imsg_event_add(&client->iev);
1593 return NULL;
1596 static void
1597 send_refs_updated(struct gotd_client *client)
1599 if (gotd_imsg_compose_event(&client->iev,
1600 GOTD_IMSG_REFS_UPDATED, PROC_GOTD, -1, NULL, 0) == -1)
1601 log_warn("imsg compose REFS_UPDATED");
1604 static const struct got_error *
1605 send_ref_update_ng(struct gotd_client *client,
1606 struct gotd_imsg_ref_update *iref, const char *refname,
1607 const char *reason)
1609 const struct got_error *ng_err;
1610 struct gotd_imsg_ref_update_ng ing;
1611 struct ibuf *wbuf;
1612 size_t len;
1614 memset(&ing, 0, sizeof(ing));
1615 ing.client_id = client->id;
1616 memcpy(ing.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1617 memcpy(ing.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1618 ing.name_len = strlen(refname);
1620 ng_err = got_error_fmt(GOT_ERR_REF_BUSY, "%s", reason);
1621 ing.reason_len = strlen(ng_err->msg);
1623 len = sizeof(ing) + ing.name_len + ing.reason_len;
1624 wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_NG,
1625 PROC_GOTD, gotd.pid, len);
1626 if (wbuf == NULL)
1627 return got_error_from_errno("imsg_create REF_UPDATE_NG");
1629 if (imsg_add(wbuf, &ing, sizeof(ing)) == -1)
1630 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1631 if (imsg_add(wbuf, refname, ing.name_len) == -1)
1632 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1633 if (imsg_add(wbuf, ng_err->msg, ing.reason_len) == -1)
1634 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1636 wbuf->fd = -1;
1637 imsg_close(&client->iev.ibuf, wbuf);
1638 gotd_imsg_event_add(&client->iev);
1639 return NULL;
1642 static const struct got_error *
1643 install_pack(struct gotd_client *client, const char *repo_path,
1644 struct imsg *imsg)
1646 const struct got_error *err = NULL;
1647 struct gotd_imsg_packfile_install inst;
1648 char hex[SHA1_DIGEST_STRING_LENGTH];
1649 size_t datalen;
1650 char *packfile_path = NULL, *packidx_path = NULL;
1652 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1653 if (datalen != sizeof(inst))
1654 return got_error(GOT_ERR_PRIVSEP_LEN);
1655 memcpy(&inst, imsg->data, sizeof(inst));
1657 if (client->packfile_path == NULL)
1658 return got_error_msg(GOT_ERR_BAD_REQUEST,
1659 "client has no pack file");
1660 if (client->packidx_path == NULL)
1661 return got_error_msg(GOT_ERR_BAD_REQUEST,
1662 "client has no pack file index");
1664 if (got_sha1_digest_to_str(inst.pack_sha1, hex, sizeof(hex)) == NULL)
1665 return got_error_msg(GOT_ERR_NO_SPACE,
1666 "could not convert pack file SHA1 to hex");
1668 if (asprintf(&packfile_path, "/%s/%s/pack-%s.pack",
1669 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1670 err = got_error_from_errno("asprintf");
1671 goto done;
1674 if (asprintf(&packidx_path, "/%s/%s/pack-%s.idx",
1675 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1676 err = got_error_from_errno("asprintf");
1677 goto done;
1680 if (rename(client->packfile_path, packfile_path) == -1) {
1681 err = got_error_from_errno3("rename", client->packfile_path,
1682 packfile_path);
1683 goto done;
1686 free(client->packfile_path);
1687 client->packfile_path = NULL;
1689 if (rename(client->packidx_path, packidx_path) == -1) {
1690 err = got_error_from_errno3("rename", client->packidx_path,
1691 packidx_path);
1692 goto done;
1695 free(client->packidx_path);
1696 client->packidx_path = NULL;
1697 done:
1698 free(packfile_path);
1699 free(packidx_path);
1700 return err;
1703 static const struct got_error *
1704 begin_ref_updates(struct gotd_client *client, struct imsg *imsg)
1706 struct gotd_imsg_ref_updates_start istart;
1707 size_t datalen;
1709 if (client->nref_updates != -1)
1710 return got_error(GOT_ERR_PRIVSEP_MSG);
1712 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1713 if (datalen != sizeof(istart))
1714 return got_error(GOT_ERR_PRIVSEP_LEN);
1715 memcpy(&istart, imsg->data, sizeof(istart));
1717 if (istart.nref_updates <= 0)
1718 return got_error(GOT_ERR_PRIVSEP_MSG);
1720 client->nref_updates = istart.nref_updates;
1721 return NULL;
1724 static const struct got_error *
1725 update_ref(struct gotd_client *client, const char *repo_path,
1726 struct imsg *imsg)
1728 const struct got_error *err = NULL;
1729 struct got_repository *repo = NULL;
1730 struct got_reference *ref = NULL;
1731 struct gotd_imsg_ref_update iref;
1732 struct got_object_id old_id, new_id;
1733 struct got_object_id *id = NULL;
1734 struct got_object *obj = NULL;
1735 char *refname = NULL;
1736 size_t datalen;
1737 int locked = 0;
1739 log_debug("update-ref from uid %d", client->euid);
1741 if (client->nref_updates <= 0)
1742 return got_error(GOT_ERR_PRIVSEP_MSG);
1744 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1745 if (datalen < sizeof(iref))
1746 return got_error(GOT_ERR_PRIVSEP_LEN);
1747 memcpy(&iref, imsg->data, sizeof(iref));
1748 if (datalen != sizeof(iref) + iref.name_len)
1749 return got_error(GOT_ERR_PRIVSEP_LEN);
1750 refname = malloc(iref.name_len + 1);
1751 if (refname == NULL)
1752 return got_error_from_errno("malloc");
1753 memcpy(refname, imsg->data + sizeof(iref), iref.name_len);
1754 refname[iref.name_len] = '\0';
1756 log_debug("updating ref %s for uid %d", refname, client->euid);
1758 err = got_repo_open(&repo, repo_path, NULL, NULL);
1759 if (err)
1760 goto done;
1762 memcpy(old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
1763 memcpy(new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
1764 err = got_object_open(&obj, repo, &new_id);
1765 if (err)
1766 goto done;
1768 if (iref.ref_is_new) {
1769 err = got_ref_open(&ref, repo, refname, 0);
1770 if (err) {
1771 if (err->code != GOT_ERR_NOT_REF)
1772 goto done;
1773 err = got_ref_alloc(&ref, refname, &new_id);
1774 if (err)
1775 goto done;
1776 err = got_ref_write(ref, repo); /* will lock/unlock */
1777 if (err)
1778 goto done;
1779 } else {
1780 err = got_error_fmt(GOT_ERR_REF_BUSY,
1781 "%s has been created by someone else "
1782 "while transaction was in progress",
1783 got_ref_get_name(ref));
1784 goto done;
1786 } else {
1787 err = got_ref_open(&ref, repo, refname, 1 /* lock */);
1788 if (err)
1789 goto done;
1790 locked = 1;
1792 err = got_ref_resolve(&id, repo, ref);
1793 if (err)
1794 goto done;
1796 if (got_object_id_cmp(id, &old_id) != 0) {
1797 err = got_error_fmt(GOT_ERR_REF_BUSY,
1798 "%s has been modified by someone else "
1799 "while transaction was in progress",
1800 got_ref_get_name(ref));
1801 goto done;
1804 err = got_ref_change_ref(ref, &new_id);
1805 if (err)
1806 goto done;
1808 err = got_ref_write(ref, repo);
1809 if (err)
1810 goto done;
1812 free(id);
1813 id = NULL;
1815 done:
1816 if (err) {
1817 if (err->code == GOT_ERR_LOCKFILE_TIMEOUT) {
1818 err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
1819 "could not acquire exclusive file lock for %s",
1820 refname);
1822 send_ref_update_ng(client, &iref, refname, err->msg);
1823 } else
1824 send_ref_update_ok(client, &iref, refname);
1826 if (client->nref_updates > 0) {
1827 client->nref_updates--;
1828 if (client->nref_updates == 0)
1829 send_refs_updated(client);
1832 if (locked) {
1833 const struct got_error *unlock_err;
1834 unlock_err = got_ref_unlock(ref);
1835 if (unlock_err && err == NULL)
1836 err = unlock_err;
1838 if (ref)
1839 got_ref_close(ref);
1840 if (obj)
1841 got_object_close(obj);
1842 if (repo)
1843 got_repo_close(repo);
1844 free(refname);
1845 free(id);
1846 return err;
1849 static void
1850 gotd_dispatch(int fd, short event, void *arg)
1852 struct gotd_imsgev *iev = arg;
1853 struct imsgbuf *ibuf = &iev->ibuf;
1854 struct gotd_child_proc *proc = NULL;
1855 ssize_t n;
1856 int shut = 0;
1857 struct imsg imsg;
1859 if (event & EV_READ) {
1860 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1861 fatal("imsg_read error");
1862 if (n == 0) {
1863 /* Connection closed. */
1864 shut = 1;
1865 goto done;
1869 if (event & EV_WRITE) {
1870 n = msgbuf_write(&ibuf->w);
1871 if (n == -1 && errno != EAGAIN)
1872 fatal("msgbuf_write");
1873 if (n == 0) {
1874 /* Connection closed. */
1875 shut = 1;
1876 goto done;
1880 proc = find_proc_by_fd(fd);
1881 if (proc == NULL)
1882 fatalx("cannot find child process for fd %d", fd);
1884 for (;;) {
1885 const struct got_error *err = NULL;
1886 struct gotd_client *client = NULL;
1887 uint32_t client_id = 0;
1888 int do_disconnect = 0;
1889 int do_ref_updates = 0, do_ref_update = 0;
1890 int do_packfile_install = 0;
1892 if ((n = imsg_get(ibuf, &imsg)) == -1)
1893 fatal("%s: imsg_get error", __func__);
1894 if (n == 0) /* No more messages. */
1895 break;
1897 switch (imsg.hdr.type) {
1898 case GOTD_IMSG_ERROR:
1899 do_disconnect = 1;
1900 err = gotd_imsg_recv_error(&client_id, &imsg);
1901 break;
1902 case GOTD_IMSG_PACKFILE_DONE:
1903 do_disconnect = 1;
1904 err = recv_packfile_done(&client_id, &imsg);
1905 break;
1906 case GOTD_IMSG_PACKFILE_INSTALL:
1907 err = recv_packfile_install(&client_id, &imsg);
1908 if (err == NULL)
1909 do_packfile_install = 1;
1910 break;
1911 case GOTD_IMSG_REF_UPDATES_START:
1912 err = recv_ref_updates_start(&client_id, &imsg);
1913 if (err == NULL)
1914 do_ref_updates = 1;
1915 break;
1916 case GOTD_IMSG_REF_UPDATE:
1917 err = recv_ref_update(&client_id, &imsg);
1918 if (err == NULL)
1919 do_ref_update = 1;
1920 break;
1921 default:
1922 log_debug("unexpected imsg %d", imsg.hdr.type);
1923 break;
1926 client = find_client(client_id);
1927 if (client == NULL) {
1928 log_warnx("%s: client not found", __func__);
1929 imsg_free(&imsg);
1930 continue;
1933 if (!verify_imsg_src(client, proc, &imsg)) {
1934 log_debug("dropping imsg type %d from PID %d",
1935 imsg.hdr.type, proc->pid);
1936 imsg_free(&imsg);
1937 continue;
1939 if (err)
1940 log_warnx("uid %d: %s", client->euid, err->msg);
1942 if (do_disconnect) {
1943 if (err)
1944 disconnect_on_error(client, err);
1945 else
1946 disconnect(client);
1947 } else {
1948 if (do_packfile_install)
1949 err = install_pack(client, proc->chroot_path,
1950 &imsg);
1951 else if (do_ref_updates)
1952 err = begin_ref_updates(client, &imsg);
1953 else if (do_ref_update)
1954 err = update_ref(client, proc->chroot_path,
1955 &imsg);
1956 if (err)
1957 log_warnx("uid %d: %s", client->euid, err->msg);
1959 imsg_free(&imsg);
1961 done:
1962 if (!shut) {
1963 gotd_imsg_event_add(iev);
1964 } else {
1965 /* This pipe is dead. Remove its event handler */
1966 event_del(&iev->ev);
1967 event_loopexit(NULL);
1971 static pid_t
1972 start_child(enum gotd_procid proc_id, const char *chroot_path,
1973 char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
1975 char *argv[11];
1976 int argc = 0;
1977 pid_t pid;
1979 switch (pid = fork()) {
1980 case -1:
1981 fatal("cannot fork");
1982 case 0:
1983 break;
1984 default:
1985 close(fd);
1986 return pid;
1989 if (fd != GOTD_SOCK_FILENO) {
1990 if (dup2(fd, GOTD_SOCK_FILENO) == -1)
1991 fatal("cannot setup imsg fd");
1992 } else if (fcntl(fd, F_SETFD, 0) == -1)
1993 fatal("cannot setup imsg fd");
1995 argv[argc++] = argv0;
1996 switch (proc_id) {
1997 case PROC_REPO_READ:
1998 argv[argc++] = (char *)"-R";
1999 break;
2000 case PROC_REPO_WRITE:
2001 argv[argc++] = (char *)"-W";
2002 break;
2003 default:
2004 fatalx("invalid process id %d", proc_id);
2007 argv[argc++] = (char *)"-f";
2008 argv[argc++] = (char *)confpath;
2010 argv[argc++] = (char *)"-P";
2011 argv[argc++] = (char *)chroot_path;
2013 if (!daemonize)
2014 argv[argc++] = (char *)"-d";
2015 if (verbosity > 0)
2016 argv[argc++] = (char *)"-v";
2017 if (verbosity > 1)
2018 argv[argc++] = (char *)"-v";
2019 argv[argc++] = NULL;
2021 execvp(argv0, argv);
2022 fatal("execvp");
2025 static void
2026 start_repo_children(struct gotd *gotd, char *argv0, const char *confpath,
2027 int daemonize, int verbosity)
2029 struct gotd_repo *repo = NULL;
2030 struct gotd_child_proc *proc;
2031 int i;
2034 * XXX For now, use one reader and one writer per repository.
2035 * This should be changed to N readers + M writers.
2037 gotd->nprocs = gotd->nrepos * 2;
2038 gotd->procs = calloc(gotd->nprocs, sizeof(*gotd->procs));
2039 if (gotd->procs == NULL)
2040 fatal("calloc");
2041 for (i = 0; i < gotd->nprocs; i++) {
2042 if (repo == NULL)
2043 repo = TAILQ_FIRST(&gotd->repos);
2044 proc = &gotd->procs[i];
2045 if (i < gotd->nrepos)
2046 proc->type = PROC_REPO_READ;
2047 else
2048 proc->type = PROC_REPO_WRITE;
2049 if (strlcpy(proc->repo_name, repo->name,
2050 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
2051 fatalx("repository name too long: %s", repo->name);
2052 log_debug("adding repository %s", repo->name);
2053 if (realpath(repo->path, proc->chroot_path) == NULL)
2054 fatal("%s", repo->path);
2055 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
2056 PF_UNSPEC, proc->pipe) == -1)
2057 fatal("socketpair");
2058 proc->pid = start_child(proc->type, proc->chroot_path, argv0,
2059 confpath, proc->pipe[1], daemonize, verbosity);
2060 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
2061 log_debug("proc %s %s is on fd %d",
2062 gotd_proc_names[proc->type], proc->chroot_path,
2063 proc->pipe[0]);
2064 proc->iev.handler = gotd_dispatch;
2065 proc->iev.events = EV_READ;
2066 proc->iev.handler_arg = NULL;
2067 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
2068 gotd_dispatch, &proc->iev);
2070 repo = TAILQ_NEXT(repo, entry);
2074 static void
2075 apply_unveil(void)
2077 struct gotd_repo *repo;
2079 TAILQ_FOREACH(repo, &gotd.repos, entry) {
2080 if (unveil(repo->path, "rwc") == -1)
2081 fatal("unveil %s", repo->path);
2084 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
2085 fatal("unveil %s", GOT_TMPDIR_STR);
2087 if (unveil(NULL, NULL) == -1)
2088 fatal("unveil");
2091 int
2092 main(int argc, char **argv)
2094 const struct got_error *error = NULL;
2095 int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
2096 const char *confpath = GOTD_CONF_PATH;
2097 char *argv0 = argv[0];
2098 char title[2048];
2099 int ngroups = NGROUPS_MAX;
2100 struct passwd *pw = NULL;
2101 struct group *gr = NULL;
2102 char *repo_path = NULL;
2103 enum gotd_procid proc_id = PROC_GOTD;
2104 struct event evsigint, evsigterm, evsighup, evsigusr1;
2105 int *pack_fds = NULL, *temp_fds = NULL;
2107 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
2109 while ((ch = getopt(argc, argv, "df:nP:RvW")) != -1) {
2110 switch (ch) {
2111 case 'd':
2112 daemonize = 0;
2113 break;
2114 case 'f':
2115 confpath = optarg;
2116 break;
2117 case 'n':
2118 noaction = 1;
2119 break;
2120 case 'P':
2121 repo_path = realpath(optarg, NULL);
2122 if (repo_path == NULL)
2123 fatal("realpath '%s'", optarg);
2124 break;
2125 case 'R':
2126 proc_id = PROC_REPO_READ;
2127 break;
2128 case 'v':
2129 if (verbosity < 3)
2130 verbosity++;
2131 break;
2132 case 'W':
2133 proc_id = PROC_REPO_WRITE;
2134 break;
2135 default:
2136 usage();
2140 argc -= optind;
2141 argv += optind;
2143 if (argc != 0)
2144 usage();
2146 if (geteuid())
2147 fatalx("need root privileges");
2149 log_init(daemonize ? 0 : 1, LOG_DAEMON);
2150 log_setverbose(verbosity);
2152 if (parse_config(confpath, proc_id, &gotd) != 0)
2153 return 1;
2155 if (proc_id == PROC_GOTD &&
2156 (gotd.nrepos == 0 || TAILQ_EMPTY(&gotd.repos)))
2157 fatalx("no repository defined in configuration file");
2159 pw = getpwnam(gotd.user_name);
2160 if (pw == NULL)
2161 fatal("getpwuid: user %s not found", gotd.user_name);
2163 if (pw->pw_uid == 0) {
2164 fatalx("cannot run %s as %s: the user running %s "
2165 "must not be the superuser",
2166 getprogname(), pw->pw_name, getprogname());
2169 if (getgrouplist(pw->pw_name, pw->pw_gid, gotd.groups, &ngroups) == -1)
2170 log_warnx("group membership list truncated");
2171 gotd.ngroups = ngroups;
2173 gr = match_group(gotd.groups, ngroups, gotd.unix_group_name);
2174 if (gr == NULL) {
2175 fatalx("cannot start %s: the user running %s "
2176 "must be a secondary member of group %s",
2177 getprogname(), getprogname(), gotd.unix_group_name);
2179 if (gr->gr_gid == pw->pw_gid) {
2180 fatalx("cannot start %s: the user running %s "
2181 "must be a secondary member of group %s, but "
2182 "%s is the user's primary group",
2183 getprogname(), getprogname(), gotd.unix_group_name,
2184 gotd.unix_group_name);
2187 if (proc_id == PROC_GOTD &&
2188 !got_path_is_absolute(gotd.unix_socket_path))
2189 fatalx("bad unix socket path \"%s\": must be an absolute path",
2190 gotd.unix_socket_path);
2192 if (noaction)
2193 return 0;
2195 if (proc_id == PROC_GOTD && verbosity) {
2196 log_info("socket: %s", gotd.unix_socket_path);
2197 log_info("user: %s", pw->pw_name);
2198 log_info("secondary group: %s", gr->gr_name);
2200 fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
2201 gr->gr_gid);
2202 if (fd == -1) {
2203 fatal("cannot listen on unix socket %s",
2204 gotd.unix_socket_path);
2208 if (proc_id == PROC_GOTD) {
2209 gotd.pid = getpid();
2210 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2211 start_repo_children(&gotd, argv0, confpath, daemonize,
2212 verbosity);
2213 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
2214 if (daemonize && daemon(0, 0) == -1)
2215 fatal("daemon");
2216 } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE) {
2217 error = got_repo_pack_fds_open(&pack_fds);
2218 if (error != NULL)
2219 fatalx("cannot open pack tempfiles: %s", error->msg);
2220 error = got_repo_temp_fds_open(&temp_fds);
2221 if (error != NULL)
2222 fatalx("cannot open pack tempfiles: %s", error->msg);
2223 if (repo_path == NULL)
2224 fatalx("repository path not specified");
2225 snprintf(title, sizeof(title), "%s %s",
2226 gotd_proc_names[proc_id], repo_path);
2227 if (chroot(repo_path) == -1)
2228 fatal("chroot");
2229 if (chdir("/") == -1)
2230 fatal("chdir(\"/\")");
2231 if (daemonize && daemon(1, 0) == -1)
2232 fatal("daemon");
2233 } else
2234 fatal("invalid process id %d", proc_id);
2236 setproctitle("%s", title);
2237 log_procinit(title);
2239 /* Drop root privileges. */
2240 if (setgid(pw->pw_gid) == -1)
2241 fatal("setgid %d failed", pw->pw_gid);
2242 if (setuid(pw->pw_uid) == -1)
2243 fatal("setuid %d failed", pw->pw_uid);
2245 event_init();
2247 switch (proc_id) {
2248 case PROC_GOTD:
2249 #ifndef PROFILE
2250 if (pledge("stdio rpath wpath cpath proc getpw sendfd recvfd "
2251 "fattr flock unix unveil", NULL) == -1)
2252 err(1, "pledge");
2253 #endif
2254 break;
2255 case PROC_REPO_READ:
2256 #ifndef PROFILE
2257 if (pledge("stdio rpath recvfd", NULL) == -1)
2258 err(1, "pledge");
2259 #endif
2260 repo_read_main(title, pack_fds, temp_fds);
2261 /* NOTREACHED */
2262 exit(0);
2263 case PROC_REPO_WRITE:
2264 #ifndef PROFILE
2265 if (pledge("stdio rpath sendfd recvfd", NULL) == -1)
2266 err(1, "pledge");
2267 #endif
2268 repo_write_main(title, pack_fds, temp_fds);
2269 /* NOTREACHED */
2270 exit(0);
2271 default:
2272 fatal("invalid process id %d", proc_id);
2275 if (proc_id != PROC_GOTD)
2276 fatal("invalid process id %d", proc_id);
2278 apply_unveil();
2280 signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
2281 signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
2282 signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
2283 signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
2284 signal(SIGPIPE, SIG_IGN);
2286 signal_add(&evsigint, NULL);
2287 signal_add(&evsigterm, NULL);
2288 signal_add(&evsighup, NULL);
2289 signal_add(&evsigusr1, NULL);
2291 event_set(&gotd.ev, fd, EV_READ | EV_PERSIST, gotd_accept, NULL);
2292 if (event_add(&gotd.ev, NULL))
2293 fatalx("event add");
2294 evtimer_set(&gotd.pause, gotd_accept_paused, NULL);
2296 event_dispatch();
2298 if (fd != -1)
2299 close(fd);
2300 if (pack_fds)
2301 got_repo_pack_fds_close(pack_fds);
2302 free(repo_path);
2303 return 0;