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 /* Send pack pipe end 0 to repo_write. */
923 if (gotd_imsg_compose_event(&client->repo_write->iev,
924 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[0],
925 &ipipe, sizeof(ipipe)) == -1) {
926 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
927 pipe[0] = -1;
928 goto done;
930 pipe[0] = -1;
932 /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
933 if (gotd_imsg_compose_event(&client->iev,
934 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1], NULL, 0) == -1)
935 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
936 pipe[1] = -1;
938 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.pack",
939 client->repo_write->chroot_path, GOT_OBJECTS_PACK_DIR,
940 client->euid) == -1) {
941 err = got_error_from_errno("asprintf");
942 goto done;
945 err = got_opentemp_named_fd(&pack_path, &packfd, basepath, "");
946 if (err)
947 goto done;
949 free(basepath);
950 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.idx",
951 client->repo_write->chroot_path, GOT_OBJECTS_PACK_DIR,
952 client->euid) == -1) {
953 err = got_error_from_errno("asprintf");
954 basepath = NULL;
955 goto done;
957 err = got_opentemp_named_fd(&idx_path, &idxfd, basepath, "");
958 if (err)
959 goto done;
961 memset(&ifile, 0, sizeof(ifile));
962 ifile.client_id = client->id;
963 if (gotd_imsg_compose_event(&client->repo_write->iev,
964 GOTD_IMSG_PACKIDX_FILE, PROC_GOTD, idxfd,
965 &ifile, sizeof(ifile)) == -1) {
966 err = got_error_from_errno("imsg compose PACKIDX_FILE");
967 idxfd = -1;
968 goto done;
970 idxfd = -1;
972 memset(&ipack, 0, sizeof(ipack));
973 ipack.client_id = client->id;
974 if (client_has_capability(client, GOT_CAPA_REPORT_STATUS))
975 ipack.report_status = 1;
977 if (gotd_imsg_compose_event(&client->repo_write->iev,
978 GOTD_IMSG_RECV_PACKFILE, PROC_GOTD, packfd,
979 &ipack, sizeof(ipack)) == -1) {
980 err = got_error_from_errno("imsg compose RECV_PACKFILE");
981 packfd = -1;
982 goto done;
984 packfd = -1;
986 done:
987 free(basepath);
988 if (pipe[0] != -1 && close(pipe[0]) == -1 && err == NULL)
989 err = got_error_from_errno("close");
990 if (pipe[1] != -1 && close(pipe[1]) == -1 && err == NULL)
991 err = got_error_from_errno("close");
992 if (packfd != -1 && close(packfd) == -1 && err == NULL)
993 err = got_error_from_errno("close");
994 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
995 err = got_error_from_errno("close");
996 if (err) {
997 free(pack_path);
998 free(idx_path);
999 } else {
1000 client->packfile_path = pack_path;
1001 client->packidx_path = idx_path;
1003 return err;
1006 static void
1007 gotd_request(int fd, short events, void *arg)
1009 struct gotd_imsgev *iev = arg;
1010 struct imsgbuf *ibuf = &iev->ibuf;
1011 struct gotd_client *client = iev->handler_arg;
1012 const struct got_error *err = NULL;
1013 struct imsg imsg;
1014 ssize_t n;
1016 if (events & EV_WRITE) {
1017 while (ibuf->w.queued) {
1018 n = msgbuf_write(&ibuf->w);
1019 if (n == -1 && errno == EPIPE) {
1021 * The client has closed its socket.
1022 * This can happen when Git clients are
1023 * done sending pack file data.
1025 msgbuf_clear(&ibuf->w);
1026 continue;
1027 } else if (n == -1 && errno != EAGAIN) {
1028 err = got_error_from_errno("imsg_flush");
1029 disconnect_on_error(client, err);
1030 return;
1032 if (n == 0) {
1033 /* Connection closed. */
1034 err = got_error(GOT_ERR_EOF);
1035 disconnect_on_error(client, err);
1036 return;
1040 /* Disconnect gotctl(8) now that messages have been sent. */
1041 if (!client_is_reading(client) && !client_is_writing(client)) {
1042 disconnect(client);
1043 return;
1047 if ((events & EV_READ) == 0)
1048 return;
1050 memset(&imsg, 0, sizeof(imsg));
1052 while (err == NULL) {
1053 err = gotd_imsg_recv(&imsg, ibuf, 0);
1054 if (err) {
1055 if (err->code == GOT_ERR_PRIVSEP_READ)
1056 err = NULL;
1057 break;
1060 evtimer_del(&client->tmo);
1062 switch (imsg.hdr.type) {
1063 case GOTD_IMSG_INFO:
1064 err = send_info(client);
1065 break;
1066 case GOTD_IMSG_STOP:
1067 err = stop_gotd(client);
1068 break;
1069 case GOTD_IMSG_LIST_REFS:
1070 if (client->state != GOTD_STATE_EXPECT_LIST_REFS) {
1071 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1072 "unexpected list-refs request received");
1073 break;
1075 err = forward_list_refs_request(client, &imsg);
1076 if (err)
1077 break;
1078 client->state = GOTD_STATE_EXPECT_CAPABILITIES;
1079 log_debug("uid %d: expecting capabilities",
1080 client->euid);
1081 break;
1082 case GOTD_IMSG_CAPABILITIES:
1083 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1084 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1085 "unexpected capabilities received");
1086 break;
1088 log_debug("receiving capabilities from uid %d",
1089 client->euid);
1090 err = recv_capabilities(client, &imsg);
1091 break;
1092 case GOTD_IMSG_CAPABILITY:
1093 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1094 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1095 "unexpected capability received");
1096 break;
1098 err = recv_capability(client, &imsg);
1099 if (err || client->ncapabilities < client->ncapa_alloc)
1100 break;
1101 if (client_is_reading(client)) {
1102 client->state = GOTD_STATE_EXPECT_WANT;
1103 log_debug("uid %d: expecting want-lines",
1104 client->euid);
1105 } else if (client_is_writing(client)) {
1106 client->state = GOTD_STATE_EXPECT_REF_UPDATE;
1107 log_debug("uid %d: expecting ref-update-lines",
1108 client->euid);
1109 } else
1110 fatalx("client %d is both reading and writing",
1111 client->euid);
1112 break;
1113 case GOTD_IMSG_WANT:
1114 if (client->state != GOTD_STATE_EXPECT_WANT) {
1115 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1116 "unexpected want-line received");
1117 break;
1119 log_debug("received want-line from uid %d",
1120 client->euid);
1121 err = ensure_client_is_reading(client);
1122 if (err)
1123 break;
1124 err = forward_want(client, &imsg);
1125 break;
1126 case GOTD_IMSG_REF_UPDATE:
1127 if (client->state != GOTD_STATE_EXPECT_REF_UPDATE) {
1128 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1129 "unexpected ref-update-line received");
1130 break;
1132 log_debug("received ref-update-line from uid %d",
1133 client->euid);
1134 err = ensure_client_is_writing(client);
1135 if (err)
1136 break;
1137 err = forward_ref_update(client, &imsg);
1138 if (err)
1139 break;
1140 client->state = GOTD_STATE_EXPECT_MORE_REF_UPDATES;
1141 break;
1142 case GOTD_IMSG_HAVE:
1143 if (client->state != GOTD_STATE_EXPECT_HAVE) {
1144 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1145 "unexpected have-line received");
1146 break;
1148 log_debug("received have-line from uid %d",
1149 client->euid);
1150 err = ensure_client_is_reading(client);
1151 if (err)
1152 break;
1153 err = forward_have(client, &imsg);
1154 if (err)
1155 break;
1156 break;
1157 case GOTD_IMSG_FLUSH:
1158 if (client->state == GOTD_STATE_EXPECT_WANT ||
1159 client->state == GOTD_STATE_EXPECT_HAVE) {
1160 err = ensure_client_is_reading(client);
1161 if (err)
1162 break;
1163 } else if (client->state ==
1164 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1165 err = ensure_client_is_writing(client);
1166 if (err)
1167 break;
1168 } else {
1169 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1170 "unexpected flush-pkt received");
1171 break;
1173 log_debug("received flush-pkt from uid %d",
1174 client->euid);
1175 if (client->state == GOTD_STATE_EXPECT_WANT) {
1176 client->state = GOTD_STATE_EXPECT_HAVE;
1177 log_debug("uid %d: expecting have-lines",
1178 client->euid);
1179 } else if (client->state == GOTD_STATE_EXPECT_HAVE) {
1180 client->state = GOTD_STATE_EXPECT_DONE;
1181 log_debug("uid %d: expecting 'done'",
1182 client->euid);
1183 } else if (client->state ==
1184 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1185 client->state = GOTD_STATE_EXPECT_PACKFILE;
1186 log_debug("uid %d: expecting packfile",
1187 client->euid);
1188 err = recv_packfile(client);
1189 } else {
1190 /* should not happen, see above */
1191 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1192 "unexpected client state");
1193 break;
1195 break;
1196 case GOTD_IMSG_DONE:
1197 if (client->state != GOTD_STATE_EXPECT_HAVE &&
1198 client->state != GOTD_STATE_EXPECT_DONE) {
1199 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1200 "unexpected flush-pkt received");
1201 break;
1203 log_debug("received 'done' from uid %d", client->euid);
1204 err = ensure_client_is_reading(client);
1205 if (err)
1206 break;
1207 client->state = GOTD_STATE_DONE;
1208 err = send_packfile(client);
1209 break;
1210 default:
1211 err = got_error(GOT_ERR_PRIVSEP_MSG);
1212 break;
1215 imsg_free(&imsg);
1218 if (err) {
1219 if (err->code != GOT_ERR_EOF ||
1220 client->state != GOTD_STATE_EXPECT_PACKFILE)
1221 disconnect_on_error(client, err);
1222 } else {
1223 gotd_imsg_event_add(&client->iev);
1224 evtimer_add(&client->tmo, &timeout);
1228 static void
1229 gotd_request_timeout(int fd, short events, void *arg)
1231 struct gotd_client *client = arg;
1233 log_debug("disconnecting uid %d due to timeout", client->euid);
1234 disconnect(client);
1237 static void
1238 gotd_accept(int fd, short event, void *arg)
1240 struct sockaddr_storage ss;
1241 struct timeval backoff;
1242 socklen_t len;
1243 int s = -1;
1244 struct gotd_client *client = NULL;
1245 uid_t euid;
1246 gid_t egid;
1248 backoff.tv_sec = 1;
1249 backoff.tv_usec = 0;
1251 if (event_add(&gotd.ev, NULL) == -1) {
1252 log_warn("event_add");
1253 return;
1255 if (event & EV_TIMEOUT)
1256 return;
1258 len = sizeof(ss);
1260 s = accept_reserve(fd, (struct sockaddr *)&ss, &len, GOTD_FD_RESERVE,
1261 &inflight);
1263 if (s == -1) {
1264 switch (errno) {
1265 case EINTR:
1266 case EWOULDBLOCK:
1267 case ECONNABORTED:
1268 return;
1269 case EMFILE:
1270 case ENFILE:
1271 event_del(&gotd.ev);
1272 evtimer_add(&gotd.pause, &backoff);
1273 return;
1274 default:
1275 log_warn("%s: accept", __func__);
1276 return;
1280 if (client_cnt >= GOTD_MAXCLIENTS)
1281 goto err;
1283 if (getpeereid(s, &euid, &egid) == -1) {
1284 log_warn("%s: getpeereid", __func__);
1285 goto err;
1288 client = calloc(1, sizeof(*client));
1289 if (client == NULL) {
1290 log_warn("%s: calloc", __func__);
1291 goto err;
1294 client->state = GOTD_STATE_EXPECT_LIST_REFS;
1295 client->id = get_client_id();
1296 client->fd = s;
1297 s = -1;
1298 client->delta_cache_fd = -1;
1299 client->euid = euid;
1300 client->egid = egid;
1301 client->nref_updates = -1;
1303 imsg_init(&client->iev.ibuf, client->fd);
1304 client->iev.handler = gotd_request;
1305 client->iev.events = EV_READ;
1306 client->iev.handler_arg = client;
1308 event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
1309 &client->iev);
1310 gotd_imsg_event_add(&client->iev);
1312 evtimer_set(&client->tmo, gotd_request_timeout, client);
1314 add_client(client);
1315 log_debug("%s: new client uid %d connected on fd %d", __func__,
1316 client->euid, client->fd);
1317 return;
1318 err:
1319 inflight--;
1320 if (s != -1)
1321 close(s);
1322 free(client);
1325 static void
1326 gotd_accept_paused(int fd, short event, void *arg)
1328 event_add(&gotd.ev, NULL);
1331 static const char *gotd_proc_names[PROC_MAX] = {
1332 "parent",
1333 "repo_read",
1334 "repo_write"
1337 static struct gotd_child_proc *
1338 get_proc_for_pid(pid_t pid)
1340 struct gotd_child_proc *proc;
1341 int i;
1343 for (i = 0; i < gotd.nprocs; i++) {
1344 proc = &gotd.procs[i];
1345 if (proc->pid == pid)
1346 return proc;
1349 return NULL;
1352 static void
1353 kill_proc(struct gotd_child_proc *proc, int fatal)
1355 if (fatal) {
1356 log_warnx("sending SIGKILL to PID %d", proc->pid);
1357 kill(proc->pid, SIGKILL);
1358 } else
1359 kill(proc->pid, SIGTERM);
1362 static void
1363 gotd_shutdown(void)
1365 pid_t pid;
1366 int status, i;
1367 struct gotd_child_proc *proc;
1369 for (i = 0; i < gotd.nprocs; i++) {
1370 proc = &gotd.procs[i];
1371 msgbuf_clear(&proc->iev.ibuf.w);
1372 close(proc->iev.ibuf.fd);
1373 kill_proc(proc, 0);
1376 log_debug("waiting for children to terminate");
1377 do {
1378 pid = wait(&status);
1379 if (pid == -1) {
1380 if (errno != EINTR && errno != ECHILD)
1381 fatal("wait");
1382 } else if (WIFSIGNALED(status)) {
1383 proc = get_proc_for_pid(pid);
1384 log_warnx("%s %s child process terminated; signal %d",
1385 proc ? gotd_proc_names[proc->type] : "",
1386 proc ? proc->chroot_path : "", WTERMSIG(status));
1388 } while (pid != -1 || (pid == -1 && errno == EINTR));
1390 log_info("terminating");
1391 exit(0);
1394 void
1395 gotd_sighdlr(int sig, short event, void *arg)
1398 * Normal signal handler rules don't apply because libevent
1399 * decouples for us.
1402 switch (sig) {
1403 case SIGHUP:
1404 log_info("%s: ignoring SIGHUP", __func__);
1405 break;
1406 case SIGUSR1:
1407 log_info("%s: ignoring SIGUSR1", __func__);
1408 break;
1409 case SIGTERM:
1410 case SIGINT:
1411 gotd_shutdown();
1412 log_warnx("gotd terminating");
1413 exit(0);
1414 break;
1415 default:
1416 fatalx("unexpected signal");
1420 static const struct got_error *
1421 ensure_proc_is_reading(struct gotd_client *client,
1422 struct gotd_child_proc *proc)
1424 if (!client_is_reading(client)) {
1425 kill_proc(proc, 1);
1426 return got_error_fmt(GOT_ERR_BAD_PACKET,
1427 "PID %d handled a read-request for uid %d but this "
1428 "user is not reading from a repository", proc->pid,
1429 client->euid);
1432 return NULL;
1435 static const struct got_error *
1436 ensure_proc_is_writing(struct gotd_client *client,
1437 struct gotd_child_proc *proc)
1439 if (!client_is_writing(client)) {
1440 kill_proc(proc, 1);
1441 return got_error_fmt(GOT_ERR_BAD_PACKET,
1442 "PID %d handled a write-request for uid %d but this "
1443 "user is not writing to a repository", proc->pid,
1444 client->euid);
1447 return NULL;
1450 static int
1451 verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
1452 struct imsg *imsg)
1454 const struct got_error *err;
1455 struct gotd_child_proc *client_proc;
1456 int ret = 0;
1458 client_proc = get_client_proc(client);
1459 if (client_proc == NULL)
1460 fatalx("no process found for uid %d", client->euid);
1462 if (proc->pid != client_proc->pid) {
1463 kill_proc(proc, 1);
1464 log_warnx("received message from PID %d for uid %d, while "
1465 "PID %d is the process serving this user",
1466 proc->pid, client->euid, client_proc->pid);
1467 return 0;
1470 switch (imsg->hdr.type) {
1471 case GOTD_IMSG_ERROR:
1472 ret = 1;
1473 break;
1474 case GOTD_IMSG_PACKFILE_DONE:
1475 err = ensure_proc_is_reading(client, proc);
1476 if (err)
1477 log_warnx("uid %d: %s", client->euid, err->msg);
1478 else
1479 ret = 1;
1480 break;
1481 case GOTD_IMSG_PACKFILE_INSTALL:
1482 case GOTD_IMSG_REF_UPDATES_START:
1483 case GOTD_IMSG_REF_UPDATE:
1484 err = ensure_proc_is_writing(client, proc);
1485 if (err)
1486 log_warnx("uid %d: %s", client->euid, err->msg);
1487 else
1488 ret = 1;
1489 break;
1490 default:
1491 log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
1492 break;
1495 return ret;
1498 static const struct got_error *
1499 recv_packfile_done(uint32_t *client_id, struct imsg *imsg)
1501 struct gotd_imsg_packfile_done idone;
1502 size_t datalen;
1504 log_debug("packfile-done received");
1506 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1507 if (datalen != sizeof(idone))
1508 return got_error(GOT_ERR_PRIVSEP_LEN);
1509 memcpy(&idone, imsg->data, sizeof(idone));
1511 *client_id = idone.client_id;
1512 return NULL;
1515 static const struct got_error *
1516 recv_packfile_install(uint32_t *client_id, struct imsg *imsg)
1518 struct gotd_imsg_packfile_install inst;
1519 size_t datalen;
1521 log_debug("packfile-install received");
1523 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1524 if (datalen != sizeof(inst))
1525 return got_error(GOT_ERR_PRIVSEP_LEN);
1526 memcpy(&inst, imsg->data, sizeof(inst));
1528 *client_id = inst.client_id;
1529 return NULL;
1532 static const struct got_error *
1533 recv_ref_updates_start(uint32_t *client_id, struct imsg *imsg)
1535 struct gotd_imsg_ref_updates_start istart;
1536 size_t datalen;
1538 log_debug("ref-updates-start received");
1540 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1541 if (datalen != sizeof(istart))
1542 return got_error(GOT_ERR_PRIVSEP_LEN);
1543 memcpy(&istart, imsg->data, sizeof(istart));
1545 *client_id = istart.client_id;
1546 return NULL;
1549 static const struct got_error *
1550 recv_ref_update(uint32_t *client_id, struct imsg *imsg)
1552 struct gotd_imsg_ref_update iref;
1553 size_t datalen;
1555 log_debug("ref-update received");
1557 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1558 if (datalen < sizeof(iref))
1559 return got_error(GOT_ERR_PRIVSEP_LEN);
1560 memcpy(&iref, imsg->data, sizeof(iref));
1562 *client_id = iref.client_id;
1563 return NULL;
1566 static const struct got_error *
1567 send_ref_update_ok(struct gotd_client *client,
1568 struct gotd_imsg_ref_update *iref, const char *refname)
1570 struct gotd_imsg_ref_update_ok iok;
1571 struct ibuf *wbuf;
1572 size_t len;
1574 memset(&iok, 0, sizeof(iok));
1575 iok.client_id = client->id;
1576 memcpy(iok.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1577 memcpy(iok.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1578 iok.name_len = strlen(refname);
1580 len = sizeof(iok) + iok.name_len;
1581 wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_OK,
1582 PROC_GOTD, gotd.pid, len);
1583 if (wbuf == NULL)
1584 return got_error_from_errno("imsg_create REF_UPDATE_OK");
1586 if (imsg_add(wbuf, &iok, sizeof(iok)) == -1)
1587 return got_error_from_errno("imsg_add REF_UPDATE_OK");
1588 if (imsg_add(wbuf, refname, iok.name_len) == -1)
1589 return got_error_from_errno("imsg_add REF_UPDATE_OK");
1591 wbuf->fd = -1;
1592 imsg_close(&client->iev.ibuf, wbuf);
1593 gotd_imsg_event_add(&client->iev);
1594 return NULL;
1597 static void
1598 send_refs_updated(struct gotd_client *client)
1600 if (gotd_imsg_compose_event(&client->iev,
1601 GOTD_IMSG_REFS_UPDATED, PROC_GOTD, -1, NULL, 0) == -1)
1602 log_warn("imsg compose REFS_UPDATED");
1605 static const struct got_error *
1606 send_ref_update_ng(struct gotd_client *client,
1607 struct gotd_imsg_ref_update *iref, const char *refname,
1608 const char *reason)
1610 const struct got_error *ng_err;
1611 struct gotd_imsg_ref_update_ng ing;
1612 struct ibuf *wbuf;
1613 size_t len;
1615 memset(&ing, 0, sizeof(ing));
1616 ing.client_id = client->id;
1617 memcpy(ing.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1618 memcpy(ing.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1619 ing.name_len = strlen(refname);
1621 ng_err = got_error_fmt(GOT_ERR_REF_BUSY, "%s", reason);
1622 ing.reason_len = strlen(ng_err->msg);
1624 len = sizeof(ing) + ing.name_len + ing.reason_len;
1625 wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_NG,
1626 PROC_GOTD, gotd.pid, len);
1627 if (wbuf == NULL)
1628 return got_error_from_errno("imsg_create REF_UPDATE_NG");
1630 if (imsg_add(wbuf, &ing, sizeof(ing)) == -1)
1631 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1632 if (imsg_add(wbuf, refname, ing.name_len) == -1)
1633 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1634 if (imsg_add(wbuf, ng_err->msg, ing.reason_len) == -1)
1635 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1637 wbuf->fd = -1;
1638 imsg_close(&client->iev.ibuf, wbuf);
1639 gotd_imsg_event_add(&client->iev);
1640 return NULL;
1643 static const struct got_error *
1644 install_pack(struct gotd_client *client, const char *repo_path,
1645 struct imsg *imsg)
1647 const struct got_error *err = NULL;
1648 struct gotd_imsg_packfile_install inst;
1649 char hex[SHA1_DIGEST_STRING_LENGTH];
1650 size_t datalen;
1651 char *packfile_path = NULL, *packidx_path = NULL;
1653 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1654 if (datalen != sizeof(inst))
1655 return got_error(GOT_ERR_PRIVSEP_LEN);
1656 memcpy(&inst, imsg->data, sizeof(inst));
1658 if (client->packfile_path == NULL)
1659 return got_error_msg(GOT_ERR_BAD_REQUEST,
1660 "client has no pack file");
1661 if (client->packidx_path == NULL)
1662 return got_error_msg(GOT_ERR_BAD_REQUEST,
1663 "client has no pack file index");
1665 if (got_sha1_digest_to_str(inst.pack_sha1, hex, sizeof(hex)) == NULL)
1666 return got_error_msg(GOT_ERR_NO_SPACE,
1667 "could not convert pack file SHA1 to hex");
1669 if (asprintf(&packfile_path, "/%s/%s/pack-%s.pack",
1670 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1671 err = got_error_from_errno("asprintf");
1672 goto done;
1675 if (asprintf(&packidx_path, "/%s/%s/pack-%s.idx",
1676 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1677 err = got_error_from_errno("asprintf");
1678 goto done;
1681 if (rename(client->packfile_path, packfile_path) == -1) {
1682 err = got_error_from_errno3("rename", client->packfile_path,
1683 packfile_path);
1684 goto done;
1687 free(client->packfile_path);
1688 client->packfile_path = NULL;
1690 if (rename(client->packidx_path, packidx_path) == -1) {
1691 err = got_error_from_errno3("rename", client->packidx_path,
1692 packidx_path);
1693 goto done;
1696 free(client->packidx_path);
1697 client->packidx_path = NULL;
1698 done:
1699 free(packfile_path);
1700 free(packidx_path);
1701 return err;
1704 static const struct got_error *
1705 begin_ref_updates(struct gotd_client *client, struct imsg *imsg)
1707 struct gotd_imsg_ref_updates_start istart;
1708 size_t datalen;
1710 if (client->nref_updates != -1)
1711 return got_error(GOT_ERR_PRIVSEP_MSG);
1713 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1714 if (datalen != sizeof(istart))
1715 return got_error(GOT_ERR_PRIVSEP_LEN);
1716 memcpy(&istart, imsg->data, sizeof(istart));
1718 if (istart.nref_updates <= 0)
1719 return got_error(GOT_ERR_PRIVSEP_MSG);
1721 client->nref_updates = istart.nref_updates;
1722 return NULL;
1725 static const struct got_error *
1726 update_ref(struct gotd_client *client, const char *repo_path,
1727 struct imsg *imsg)
1729 const struct got_error *err = NULL;
1730 struct got_repository *repo = NULL;
1731 struct got_reference *ref = NULL;
1732 struct gotd_imsg_ref_update iref;
1733 struct got_object_id old_id, new_id;
1734 struct got_object_id *id = NULL;
1735 struct got_object *obj = NULL;
1736 char *refname = NULL;
1737 size_t datalen;
1738 int locked = 0;
1740 log_debug("update-ref from uid %d", client->euid);
1742 if (client->nref_updates <= 0)
1743 return got_error(GOT_ERR_PRIVSEP_MSG);
1745 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1746 if (datalen < sizeof(iref))
1747 return got_error(GOT_ERR_PRIVSEP_LEN);
1748 memcpy(&iref, imsg->data, sizeof(iref));
1749 if (datalen != sizeof(iref) + iref.name_len)
1750 return got_error(GOT_ERR_PRIVSEP_LEN);
1751 refname = malloc(iref.name_len + 1);
1752 if (refname == NULL)
1753 return got_error_from_errno("malloc");
1754 memcpy(refname, imsg->data + sizeof(iref), iref.name_len);
1755 refname[iref.name_len] = '\0';
1757 log_debug("updating ref %s for uid %d", refname, client->euid);
1759 err = got_repo_open(&repo, repo_path, NULL, NULL);
1760 if (err)
1761 goto done;
1763 memcpy(old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
1764 memcpy(new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
1765 err = got_object_open(&obj, repo, &new_id);
1766 if (err)
1767 goto done;
1769 if (iref.ref_is_new) {
1770 err = got_ref_open(&ref, repo, refname, 0);
1771 if (err) {
1772 if (err->code != GOT_ERR_NOT_REF)
1773 goto done;
1774 err = got_ref_alloc(&ref, refname, &new_id);
1775 if (err)
1776 goto done;
1777 err = got_ref_write(ref, repo); /* will lock/unlock */
1778 if (err)
1779 goto done;
1780 } else {
1781 err = got_error_fmt(GOT_ERR_REF_BUSY,
1782 "%s has been created by someone else "
1783 "while transaction was in progress",
1784 got_ref_get_name(ref));
1785 goto done;
1787 } else {
1788 err = got_ref_open(&ref, repo, refname, 1 /* lock */);
1789 if (err)
1790 goto done;
1791 locked = 1;
1793 err = got_ref_resolve(&id, repo, ref);
1794 if (err)
1795 goto done;
1797 if (got_object_id_cmp(id, &old_id) != 0) {
1798 err = got_error_fmt(GOT_ERR_REF_BUSY,
1799 "%s has been modified by someone else "
1800 "while transaction was in progress",
1801 got_ref_get_name(ref));
1802 goto done;
1805 err = got_ref_change_ref(ref, &new_id);
1806 if (err)
1807 goto done;
1809 err = got_ref_write(ref, repo);
1810 if (err)
1811 goto done;
1813 free(id);
1814 id = NULL;
1816 done:
1817 if (err) {
1818 if (err->code == GOT_ERR_LOCKFILE_TIMEOUT) {
1819 err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
1820 "could not acquire exclusive file lock for %s",
1821 refname);
1823 send_ref_update_ng(client, &iref, refname, err->msg);
1824 } else
1825 send_ref_update_ok(client, &iref, refname);
1827 if (client->nref_updates > 0) {
1828 client->nref_updates--;
1829 if (client->nref_updates == 0)
1830 send_refs_updated(client);
1833 if (locked) {
1834 const struct got_error *unlock_err;
1835 unlock_err = got_ref_unlock(ref);
1836 if (unlock_err && err == NULL)
1837 err = unlock_err;
1839 if (ref)
1840 got_ref_close(ref);
1841 if (obj)
1842 got_object_close(obj);
1843 if (repo)
1844 got_repo_close(repo);
1845 free(refname);
1846 free(id);
1847 return err;
1850 static void
1851 gotd_dispatch(int fd, short event, void *arg)
1853 struct gotd_imsgev *iev = arg;
1854 struct imsgbuf *ibuf = &iev->ibuf;
1855 struct gotd_child_proc *proc = NULL;
1856 ssize_t n;
1857 int shut = 0;
1858 struct imsg imsg;
1860 if (event & EV_READ) {
1861 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1862 fatal("imsg_read error");
1863 if (n == 0) {
1864 /* Connection closed. */
1865 shut = 1;
1866 goto done;
1870 if (event & EV_WRITE) {
1871 n = msgbuf_write(&ibuf->w);
1872 if (n == -1 && errno != EAGAIN)
1873 fatal("msgbuf_write");
1874 if (n == 0) {
1875 /* Connection closed. */
1876 shut = 1;
1877 goto done;
1881 proc = find_proc_by_fd(fd);
1882 if (proc == NULL)
1883 fatalx("cannot find child process for fd %d", fd);
1885 for (;;) {
1886 const struct got_error *err = NULL;
1887 struct gotd_client *client = NULL;
1888 uint32_t client_id = 0;
1889 int do_disconnect = 0;
1890 int do_ref_updates = 0, do_ref_update = 0;
1891 int do_packfile_install = 0;
1893 if ((n = imsg_get(ibuf, &imsg)) == -1)
1894 fatal("%s: imsg_get error", __func__);
1895 if (n == 0) /* No more messages. */
1896 break;
1898 switch (imsg.hdr.type) {
1899 case GOTD_IMSG_ERROR:
1900 do_disconnect = 1;
1901 err = gotd_imsg_recv_error(&client_id, &imsg);
1902 break;
1903 case GOTD_IMSG_PACKFILE_DONE:
1904 do_disconnect = 1;
1905 err = recv_packfile_done(&client_id, &imsg);
1906 break;
1907 case GOTD_IMSG_PACKFILE_INSTALL:
1908 err = recv_packfile_install(&client_id, &imsg);
1909 if (err == NULL)
1910 do_packfile_install = 1;
1911 break;
1912 case GOTD_IMSG_REF_UPDATES_START:
1913 err = recv_ref_updates_start(&client_id, &imsg);
1914 if (err == NULL)
1915 do_ref_updates = 1;
1916 break;
1917 case GOTD_IMSG_REF_UPDATE:
1918 err = recv_ref_update(&client_id, &imsg);
1919 if (err == NULL)
1920 do_ref_update = 1;
1921 break;
1922 default:
1923 log_debug("unexpected imsg %d", imsg.hdr.type);
1924 break;
1927 client = find_client(client_id);
1928 if (client == NULL) {
1929 log_warnx("%s: client not found", __func__);
1930 imsg_free(&imsg);
1931 continue;
1934 if (!verify_imsg_src(client, proc, &imsg)) {
1935 log_debug("dropping imsg type %d from PID %d",
1936 imsg.hdr.type, proc->pid);
1937 imsg_free(&imsg);
1938 continue;
1940 if (err)
1941 log_warnx("uid %d: %s", client->euid, err->msg);
1943 if (do_disconnect) {
1944 if (err)
1945 disconnect_on_error(client, err);
1946 else
1947 disconnect(client);
1948 } else {
1949 if (do_packfile_install)
1950 err = install_pack(client, proc->chroot_path,
1951 &imsg);
1952 else if (do_ref_updates)
1953 err = begin_ref_updates(client, &imsg);
1954 else if (do_ref_update)
1955 err = update_ref(client, proc->chroot_path,
1956 &imsg);
1957 if (err)
1958 log_warnx("uid %d: %s", client->euid, err->msg);
1960 imsg_free(&imsg);
1962 done:
1963 if (!shut) {
1964 gotd_imsg_event_add(iev);
1965 } else {
1966 /* This pipe is dead. Remove its event handler */
1967 event_del(&iev->ev);
1968 event_loopexit(NULL);
1972 static pid_t
1973 start_child(enum gotd_procid proc_id, const char *chroot_path,
1974 char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
1976 char *argv[11];
1977 int argc = 0;
1978 pid_t pid;
1980 switch (pid = fork()) {
1981 case -1:
1982 fatal("cannot fork");
1983 case 0:
1984 break;
1985 default:
1986 close(fd);
1987 return pid;
1990 if (fd != GOTD_SOCK_FILENO) {
1991 if (dup2(fd, GOTD_SOCK_FILENO) == -1)
1992 fatal("cannot setup imsg fd");
1993 } else if (fcntl(fd, F_SETFD, 0) == -1)
1994 fatal("cannot setup imsg fd");
1996 argv[argc++] = argv0;
1997 switch (proc_id) {
1998 case PROC_REPO_READ:
1999 argv[argc++] = (char *)"-R";
2000 break;
2001 case PROC_REPO_WRITE:
2002 argv[argc++] = (char *)"-W";
2003 break;
2004 default:
2005 fatalx("invalid process id %d", proc_id);
2008 argv[argc++] = (char *)"-f";
2009 argv[argc++] = (char *)confpath;
2011 argv[argc++] = (char *)"-P";
2012 argv[argc++] = (char *)chroot_path;
2014 if (!daemonize)
2015 argv[argc++] = (char *)"-d";
2016 if (verbosity > 0)
2017 argv[argc++] = (char *)"-v";
2018 if (verbosity > 1)
2019 argv[argc++] = (char *)"-v";
2020 argv[argc++] = NULL;
2022 execvp(argv0, argv);
2023 fatal("execvp");
2026 static void
2027 start_repo_children(struct gotd *gotd, char *argv0, const char *confpath,
2028 int daemonize, int verbosity)
2030 struct gotd_repo *repo = NULL;
2031 struct gotd_child_proc *proc;
2032 int i;
2035 * XXX For now, use one reader and one writer per repository.
2036 * This should be changed to N readers + M writers.
2038 gotd->nprocs = gotd->nrepos * 2;
2039 gotd->procs = calloc(gotd->nprocs, sizeof(*gotd->procs));
2040 if (gotd->procs == NULL)
2041 fatal("calloc");
2042 for (i = 0; i < gotd->nprocs; i++) {
2043 if (repo == NULL)
2044 repo = TAILQ_FIRST(&gotd->repos);
2045 proc = &gotd->procs[i];
2046 if (i < gotd->nrepos)
2047 proc->type = PROC_REPO_READ;
2048 else
2049 proc->type = PROC_REPO_WRITE;
2050 if (strlcpy(proc->repo_name, repo->name,
2051 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
2052 fatalx("repository name too long: %s", repo->name);
2053 log_debug("adding repository %s", repo->name);
2054 if (realpath(repo->path, proc->chroot_path) == NULL)
2055 fatal("%s", repo->path);
2056 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
2057 PF_UNSPEC, proc->pipe) == -1)
2058 fatal("socketpair");
2059 proc->pid = start_child(proc->type, proc->chroot_path, argv0,
2060 confpath, proc->pipe[1], daemonize, verbosity);
2061 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
2062 log_debug("proc %s %s is on fd %d",
2063 gotd_proc_names[proc->type], proc->chroot_path,
2064 proc->pipe[0]);
2065 proc->iev.handler = gotd_dispatch;
2066 proc->iev.events = EV_READ;
2067 proc->iev.handler_arg = NULL;
2068 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
2069 gotd_dispatch, &proc->iev);
2071 repo = TAILQ_NEXT(repo, entry);
2075 static void
2076 apply_unveil(void)
2078 struct gotd_repo *repo;
2080 TAILQ_FOREACH(repo, &gotd.repos, entry) {
2081 if (unveil(repo->path, "rwc") == -1)
2082 fatal("unveil %s", repo->path);
2085 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
2086 fatal("unveil %s", GOT_TMPDIR_STR);
2088 if (unveil(NULL, NULL) == -1)
2089 fatal("unveil");
2092 int
2093 main(int argc, char **argv)
2095 const struct got_error *error = NULL;
2096 int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
2097 const char *confpath = GOTD_CONF_PATH;
2098 char *argv0 = argv[0];
2099 char title[2048];
2100 int ngroups = NGROUPS_MAX;
2101 struct passwd *pw = NULL;
2102 struct group *gr = NULL;
2103 char *repo_path = NULL;
2104 enum gotd_procid proc_id = PROC_GOTD;
2105 struct event evsigint, evsigterm, evsighup, evsigusr1;
2106 int *pack_fds = NULL, *temp_fds = NULL;
2108 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
2110 while ((ch = getopt(argc, argv, "df:nP:RvW")) != -1) {
2111 switch (ch) {
2112 case 'd':
2113 daemonize = 0;
2114 break;
2115 case 'f':
2116 confpath = optarg;
2117 break;
2118 case 'n':
2119 noaction = 1;
2120 break;
2121 case 'P':
2122 repo_path = realpath(optarg, NULL);
2123 if (repo_path == NULL)
2124 fatal("realpath '%s'", optarg);
2125 break;
2126 case 'R':
2127 proc_id = PROC_REPO_READ;
2128 break;
2129 case 'v':
2130 if (verbosity < 3)
2131 verbosity++;
2132 break;
2133 case 'W':
2134 proc_id = PROC_REPO_WRITE;
2135 break;
2136 default:
2137 usage();
2141 argc -= optind;
2142 argv += optind;
2144 if (argc != 0)
2145 usage();
2147 if (geteuid())
2148 fatalx("need root privileges");
2150 log_init(daemonize ? 0 : 1, LOG_DAEMON);
2151 log_setverbose(verbosity);
2153 if (parse_config(confpath, proc_id, &gotd) != 0)
2154 return 1;
2156 if (proc_id == PROC_GOTD &&
2157 (gotd.nrepos == 0 || TAILQ_EMPTY(&gotd.repos)))
2158 fatalx("no repository defined in configuration file");
2160 pw = getpwnam(gotd.user_name);
2161 if (pw == NULL)
2162 fatal("getpwuid: user %s not found", gotd.user_name);
2164 if (pw->pw_uid == 0) {
2165 fatalx("cannot run %s as %s: the user running %s "
2166 "must not be the superuser",
2167 getprogname(), pw->pw_name, getprogname());
2170 if (getgrouplist(pw->pw_name, pw->pw_gid, gotd.groups, &ngroups) == -1)
2171 log_warnx("group membership list truncated");
2172 gotd.ngroups = ngroups;
2174 gr = match_group(gotd.groups, ngroups, gotd.unix_group_name);
2175 if (gr == NULL) {
2176 fatalx("cannot start %s: the user running %s "
2177 "must be a secondary member of group %s",
2178 getprogname(), getprogname(), gotd.unix_group_name);
2180 if (gr->gr_gid == pw->pw_gid) {
2181 fatalx("cannot start %s: the user running %s "
2182 "must be a secondary member of group %s, but "
2183 "%s is the user's primary group",
2184 getprogname(), getprogname(), gotd.unix_group_name,
2185 gotd.unix_group_name);
2188 if (proc_id == PROC_GOTD &&
2189 !got_path_is_absolute(gotd.unix_socket_path))
2190 fatalx("bad unix socket path \"%s\": must be an absolute path",
2191 gotd.unix_socket_path);
2193 if (noaction)
2194 return 0;
2196 if (proc_id == PROC_GOTD && verbosity) {
2197 log_info("socket: %s", gotd.unix_socket_path);
2198 log_info("user: %s", pw->pw_name);
2199 log_info("secondary group: %s", gr->gr_name);
2201 fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
2202 gr->gr_gid);
2203 if (fd == -1) {
2204 fatal("cannot listen on unix socket %s",
2205 gotd.unix_socket_path);
2209 if (proc_id == PROC_GOTD) {
2210 gotd.pid = getpid();
2211 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2212 start_repo_children(&gotd, argv0, confpath, daemonize,
2213 verbosity);
2214 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
2215 if (daemonize && daemon(0, 0) == -1)
2216 fatal("daemon");
2217 } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE) {
2218 error = got_repo_pack_fds_open(&pack_fds);
2219 if (error != NULL)
2220 fatalx("cannot open pack tempfiles: %s", error->msg);
2221 error = got_repo_temp_fds_open(&temp_fds);
2222 if (error != NULL)
2223 fatalx("cannot open pack tempfiles: %s", error->msg);
2224 if (repo_path == NULL)
2225 fatalx("repository path not specified");
2226 snprintf(title, sizeof(title), "%s %s",
2227 gotd_proc_names[proc_id], repo_path);
2228 if (chroot(repo_path) == -1)
2229 fatal("chroot");
2230 if (chdir("/") == -1)
2231 fatal("chdir(\"/\")");
2232 if (daemonize && daemon(1, 0) == -1)
2233 fatal("daemon");
2234 } else
2235 fatal("invalid process id %d", proc_id);
2237 setproctitle("%s", title);
2238 log_procinit(title);
2240 /* Drop root privileges. */
2241 if (setgid(pw->pw_gid) == -1)
2242 fatal("setgid %d failed", pw->pw_gid);
2243 if (setuid(pw->pw_uid) == -1)
2244 fatal("setuid %d failed", pw->pw_uid);
2246 event_init();
2248 switch (proc_id) {
2249 case PROC_GOTD:
2250 #ifndef PROFILE
2251 if (pledge("stdio rpath wpath cpath proc getpw sendfd recvfd "
2252 "fattr flock unix unveil", NULL) == -1)
2253 err(1, "pledge");
2254 #endif
2255 break;
2256 case PROC_REPO_READ:
2257 #ifndef PROFILE
2258 if (pledge("stdio rpath recvfd", NULL) == -1)
2259 err(1, "pledge");
2260 #endif
2261 repo_read_main(title, pack_fds, temp_fds);
2262 /* NOTREACHED */
2263 exit(0);
2264 case PROC_REPO_WRITE:
2265 #ifndef PROFILE
2266 if (pledge("stdio rpath recvfd", NULL) == -1)
2267 err(1, "pledge");
2268 #endif
2269 repo_write_main(title, pack_fds, temp_fds);
2270 /* NOTREACHED */
2271 exit(0);
2272 default:
2273 fatal("invalid process id %d", proc_id);
2276 if (proc_id != PROC_GOTD)
2277 fatal("invalid process id %d", proc_id);
2279 apply_unveil();
2281 signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
2282 signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
2283 signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
2284 signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
2285 signal(SIGPIPE, SIG_IGN);
2287 signal_add(&evsigint, NULL);
2288 signal_add(&evsigterm, NULL);
2289 signal_add(&evsighup, NULL);
2290 signal_add(&evsigusr1, NULL);
2292 event_set(&gotd.ev, fd, EV_READ | EV_PERSIST, gotd_accept, NULL);
2293 if (event_add(&gotd.ev, NULL))
2294 fatalx("event add");
2295 evtimer_set(&gotd.pause, gotd_accept_paused, NULL);
2297 event_dispatch();
2299 if (fd != -1)
2300 close(fd);
2301 if (pack_fds)
2302 got_repo_pack_fds_close(pack_fds);
2303 free(repo_path);
2304 return 0;