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 client->euid, client->egid, GOTD_AUTH_READ);
634 if (err)
635 return err;
636 client->repo_read = find_proc_by_repo_name(PROC_REPO_READ,
637 ireq.repo_name);
638 if (client->repo_read == NULL)
639 return got_error(GOT_ERR_NOT_GIT_REPO);
640 } else {
641 err = ensure_client_is_not_reading(client);
642 if (err)
643 return err;
644 repo = find_repo_by_name(ireq.repo_name);
645 if (repo == NULL)
646 return got_error(GOT_ERR_NOT_GIT_REPO);
647 err = gotd_auth_check(&repo->rules, repo->name, client->euid,
648 client->egid, GOTD_AUTH_READ | GOTD_AUTH_WRITE);
649 if (err)
650 return err;
651 client->repo_write = find_proc_by_repo_name(PROC_REPO_WRITE,
652 ireq.repo_name);
653 if (client->repo_write == NULL)
654 return got_error(GOT_ERR_NOT_GIT_REPO);
657 fd = dup(client->fd);
658 if (fd == -1)
659 return got_error_from_errno("dup");
661 proc = get_client_proc(client);
662 if (proc == NULL)
663 fatalx("no process found for uid %d", client->euid);
664 if (gotd_imsg_compose_event(&proc->iev,
665 GOTD_IMSG_LIST_REFS_INTERNAL, PROC_GOTD, fd,
666 &ilref, sizeof(ilref)) == -1) {
667 err = got_error_from_errno("imsg compose WANT");
668 close(fd);
669 return err;
672 return NULL;
675 static const struct got_error *
676 forward_want(struct gotd_client *client, struct imsg *imsg)
678 struct gotd_imsg_want ireq;
679 struct gotd_imsg_want iwant;
680 size_t datalen;
682 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
683 if (datalen != sizeof(ireq))
684 return got_error(GOT_ERR_PRIVSEP_LEN);
686 memcpy(&ireq, imsg->data, datalen);
688 memset(&iwant, 0, sizeof(iwant));
689 memcpy(iwant.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
690 iwant.client_id = client->id;
692 if (gotd_imsg_compose_event(&client->repo_read->iev, GOTD_IMSG_WANT,
693 PROC_GOTD, -1, &iwant, sizeof(iwant)) == -1)
694 return got_error_from_errno("imsg compose WANT");
696 return NULL;
699 static const struct got_error *
700 forward_ref_update(struct gotd_client *client, struct imsg *imsg)
702 const struct got_error *err = NULL;
703 struct gotd_imsg_ref_update ireq;
704 struct gotd_imsg_ref_update *iref = NULL;
705 size_t datalen;
707 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
708 if (datalen < sizeof(ireq))
709 return got_error(GOT_ERR_PRIVSEP_LEN);
710 memcpy(&ireq, imsg->data, sizeof(ireq));
711 if (datalen != sizeof(ireq) + ireq.name_len)
712 return got_error(GOT_ERR_PRIVSEP_LEN);
714 iref = malloc(datalen);
715 if (iref == NULL)
716 return got_error_from_errno("malloc");
717 memcpy(iref, imsg->data, datalen);
719 iref->client_id = client->id;
720 if (gotd_imsg_compose_event(&client->repo_write->iev,
721 GOTD_IMSG_REF_UPDATE, PROC_GOTD, -1, iref, datalen) == -1)
722 err = got_error_from_errno("imsg compose REF_UPDATE");
723 free(iref);
724 return err;
727 static const struct got_error *
728 forward_have(struct gotd_client *client, struct imsg *imsg)
730 struct gotd_imsg_have ireq;
731 struct gotd_imsg_have ihave;
732 size_t datalen;
734 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
735 if (datalen != sizeof(ireq))
736 return got_error(GOT_ERR_PRIVSEP_LEN);
738 memcpy(&ireq, imsg->data, datalen);
740 memset(&ihave, 0, sizeof(ihave));
741 memcpy(ihave.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
742 ihave.client_id = client->id;
744 if (gotd_imsg_compose_event(&client->repo_read->iev, GOTD_IMSG_HAVE,
745 PROC_GOTD, -1, &ihave, sizeof(ihave)) == -1)
746 return got_error_from_errno("imsg compose HAVE");
748 return NULL;
751 static int
752 client_has_capability(struct gotd_client *client, const char *capastr)
754 struct gotd_client_capability *capa;
755 size_t i;
757 if (client->ncapabilities == 0)
758 return 0;
760 for (i = 0; i < client->ncapabilities; i++) {
761 capa = &client->capabilities[i];
762 if (strcmp(capa->key, capastr) == 0)
763 return 1;
766 return 0;
769 static const struct got_error *
770 recv_capabilities(struct gotd_client *client, struct imsg *imsg)
772 struct gotd_imsg_capabilities icapas;
773 size_t datalen;
775 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
776 if (datalen != sizeof(icapas))
777 return got_error(GOT_ERR_PRIVSEP_LEN);
778 memcpy(&icapas, imsg->data, sizeof(icapas));
780 client->ncapa_alloc = icapas.ncapabilities;
781 client->capabilities = calloc(client->ncapa_alloc,
782 sizeof(*client->capabilities));
783 if (client->capabilities == NULL) {
784 client->ncapa_alloc = 0;
785 return got_error_from_errno("calloc");
788 log_debug("expecting %zu capabilities from uid %d",
789 client->ncapa_alloc, client->euid);
790 return NULL;
793 static const struct got_error *
794 recv_capability(struct gotd_client *client, struct imsg *imsg)
796 struct gotd_imsg_capability icapa;
797 struct gotd_client_capability *capa;
798 size_t datalen;
799 char *key, *value = NULL;
801 if (client->capabilities == NULL ||
802 client->ncapabilities >= client->ncapa_alloc) {
803 return got_error_msg(GOT_ERR_BAD_REQUEST,
804 "unexpected capability received");
807 memset(&icapa, 0, sizeof(icapa));
809 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
810 if (datalen < sizeof(icapa))
811 return got_error(GOT_ERR_PRIVSEP_LEN);
812 memcpy(&icapa, imsg->data, sizeof(icapa));
814 if (datalen != sizeof(icapa) + icapa.key_len + icapa.value_len)
815 return got_error(GOT_ERR_PRIVSEP_LEN);
817 key = malloc(icapa.key_len + 1);
818 if (key == NULL)
819 return got_error_from_errno("malloc");
820 if (icapa.value_len > 0) {
821 value = malloc(icapa.value_len + 1);
822 if (value == NULL) {
823 free(key);
824 return got_error_from_errno("malloc");
828 memcpy(key, imsg->data + sizeof(icapa), icapa.key_len);
829 key[icapa.key_len] = '\0';
830 if (value) {
831 memcpy(value, imsg->data + sizeof(icapa) + icapa.key_len,
832 icapa.value_len);
833 value[icapa.value_len] = '\0';
836 capa = &client->capabilities[client->ncapabilities++];
837 capa->key = key;
838 capa->value = value;
840 if (value)
841 log_debug("uid %d: capability %s=%s", client->euid, key, value);
842 else
843 log_debug("uid %d: capability %s", client->euid, key);
845 return NULL;
848 static const struct got_error *
849 send_packfile(struct gotd_client *client)
851 const struct got_error *err = NULL;
852 struct gotd_imsg_send_packfile ipack;
853 struct gotd_imsg_packfile_pipe ipipe;
854 int pipe[2];
856 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
857 return got_error_from_errno("socketpair");
859 memset(&ipack, 0, sizeof(ipack));
860 memset(&ipipe, 0, sizeof(ipipe));
862 ipack.client_id = client->id;
863 if (client_has_capability(client, GOT_CAPA_SIDE_BAND_64K))
864 ipack.report_progress = 1;
866 client->delta_cache_fd = got_opentempfd();
867 if (client->delta_cache_fd == -1)
868 return got_error_from_errno("got_opentempfd");
870 if (gotd_imsg_compose_event(&client->repo_read->iev,
871 GOTD_IMSG_SEND_PACKFILE, PROC_GOTD, client->delta_cache_fd,
872 &ipack, sizeof(ipack)) == -1) {
873 err = got_error_from_errno("imsg compose SEND_PACKFILE");
874 close(pipe[0]);
875 close(pipe[1]);
876 return err;
879 ipipe.client_id = client->id;
881 /* Send pack pipe end 0 to repo_read. */
882 if (gotd_imsg_compose_event(&client->repo_read->iev,
883 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[0],
884 &ipipe, sizeof(ipipe)) == -1) {
885 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
886 close(pipe[1]);
887 return err;
890 /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
891 if (gotd_imsg_compose_event(&client->iev,
892 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1], NULL, 0) == -1)
893 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
895 return err;
898 static const struct got_error *
899 recv_packfile(struct gotd_client *client)
901 const struct got_error *err = NULL;
902 struct gotd_imsg_recv_packfile ipack;
903 struct gotd_imsg_packfile_pipe ipipe;
904 struct gotd_imsg_packidx_file ifile;
905 char *basepath = NULL, *pack_path = NULL, *idx_path = NULL;
906 int packfd = -1, idxfd = -1;
907 int pipe[2] = { -1, -1 };
909 if (client->packfile_path) {
910 return got_error_fmt(GOT_ERR_PRIVSEP_MSG,
911 "uid %d already has a pack file", client->euid);
914 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
915 return got_error_from_errno("socketpair");
917 memset(&ipipe, 0, sizeof(ipipe));
918 ipipe.client_id = client->id;
920 /* Send pack pipe end 0 to repo_write. */
921 if (gotd_imsg_compose_event(&client->repo_write->iev,
922 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[0],
923 &ipipe, sizeof(ipipe)) == -1) {
924 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
925 pipe[0] = -1;
926 goto done;
928 pipe[0] = -1;
930 /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
931 if (gotd_imsg_compose_event(&client->iev,
932 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1], NULL, 0) == -1)
933 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
934 pipe[1] = -1;
936 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.pack",
937 client->repo_write->chroot_path, GOT_OBJECTS_PACK_DIR,
938 client->euid) == -1) {
939 err = got_error_from_errno("asprintf");
940 goto done;
943 err = got_opentemp_named_fd(&pack_path, &packfd, basepath, "");
944 if (err)
945 goto done;
947 free(basepath);
948 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.idx",
949 client->repo_write->chroot_path, GOT_OBJECTS_PACK_DIR,
950 client->euid) == -1) {
951 err = got_error_from_errno("asprintf");
952 basepath = NULL;
953 goto done;
955 err = got_opentemp_named_fd(&idx_path, &idxfd, basepath, "");
956 if (err)
957 goto done;
959 memset(&ifile, 0, sizeof(ifile));
960 ifile.client_id = client->id;
961 if (gotd_imsg_compose_event(&client->repo_write->iev,
962 GOTD_IMSG_PACKIDX_FILE, PROC_GOTD, idxfd,
963 &ifile, sizeof(ifile)) == -1) {
964 err = got_error_from_errno("imsg compose PACKIDX_FILE");
965 idxfd = -1;
966 goto done;
968 idxfd = -1;
970 memset(&ipack, 0, sizeof(ipack));
971 ipack.client_id = client->id;
972 if (client_has_capability(client, GOT_CAPA_REPORT_STATUS))
973 ipack.report_status = 1;
975 if (gotd_imsg_compose_event(&client->repo_write->iev,
976 GOTD_IMSG_RECV_PACKFILE, PROC_GOTD, packfd,
977 &ipack, sizeof(ipack)) == -1) {
978 err = got_error_from_errno("imsg compose RECV_PACKFILE");
979 packfd = -1;
980 goto done;
982 packfd = -1;
984 done:
985 free(basepath);
986 if (pipe[0] != -1 && close(pipe[0]) == -1 && err == NULL)
987 err = got_error_from_errno("close");
988 if (pipe[1] != -1 && close(pipe[1]) == -1 && err == NULL)
989 err = got_error_from_errno("close");
990 if (packfd != -1 && close(packfd) == -1 && err == NULL)
991 err = got_error_from_errno("close");
992 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
993 err = got_error_from_errno("close");
994 if (err) {
995 free(pack_path);
996 free(idx_path);
997 } else {
998 client->packfile_path = pack_path;
999 client->packidx_path = idx_path;
1001 return err;
1004 static void
1005 gotd_request(int fd, short events, void *arg)
1007 struct gotd_imsgev *iev = arg;
1008 struct imsgbuf *ibuf = &iev->ibuf;
1009 struct gotd_client *client = iev->handler_arg;
1010 const struct got_error *err = NULL;
1011 struct imsg imsg;
1012 ssize_t n;
1014 if (events & EV_WRITE) {
1015 while (ibuf->w.queued) {
1016 n = msgbuf_write(&ibuf->w);
1017 if (n == -1 && errno == EPIPE) {
1019 * The client has closed its socket.
1020 * This can happen when Git clients are
1021 * done sending pack file data.
1023 msgbuf_clear(&ibuf->w);
1024 continue;
1025 } else if (n == -1 && errno != EAGAIN) {
1026 err = got_error_from_errno("imsg_flush");
1027 disconnect_on_error(client, err);
1028 return;
1030 if (n == 0) {
1031 /* Connection closed. */
1032 err = got_error(GOT_ERR_EOF);
1033 disconnect_on_error(client, err);
1034 return;
1038 /* Disconnect gotctl(8) now that messages have been sent. */
1039 if (!client_is_reading(client) && !client_is_writing(client)) {
1040 disconnect(client);
1041 return;
1045 if ((events & EV_READ) == 0)
1046 return;
1048 memset(&imsg, 0, sizeof(imsg));
1050 while (err == NULL) {
1051 err = gotd_imsg_recv(&imsg, ibuf, 0);
1052 if (err) {
1053 if (err->code == GOT_ERR_PRIVSEP_READ)
1054 err = NULL;
1055 break;
1058 evtimer_del(&client->tmo);
1060 switch (imsg.hdr.type) {
1061 case GOTD_IMSG_INFO:
1062 err = send_info(client);
1063 break;
1064 case GOTD_IMSG_STOP:
1065 err = stop_gotd(client);
1066 break;
1067 case GOTD_IMSG_LIST_REFS:
1068 if (client->state != GOTD_STATE_EXPECT_LIST_REFS) {
1069 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1070 "unexpected list-refs request received");
1071 break;
1073 err = forward_list_refs_request(client, &imsg);
1074 if (err)
1075 break;
1076 client->state = GOTD_STATE_EXPECT_CAPABILITIES;
1077 log_debug("uid %d: expecting capabilities",
1078 client->euid);
1079 break;
1080 case GOTD_IMSG_CAPABILITIES:
1081 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1082 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1083 "unexpected capabilities received");
1084 break;
1086 log_debug("receiving capabilities from uid %d",
1087 client->euid);
1088 err = recv_capabilities(client, &imsg);
1089 break;
1090 case GOTD_IMSG_CAPABILITY:
1091 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1092 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1093 "unexpected capability received");
1094 break;
1096 err = recv_capability(client, &imsg);
1097 if (err || client->ncapabilities < client->ncapa_alloc)
1098 break;
1099 if (client_is_reading(client)) {
1100 client->state = GOTD_STATE_EXPECT_WANT;
1101 log_debug("uid %d: expecting want-lines",
1102 client->euid);
1103 } else if (client_is_writing(client)) {
1104 client->state = GOTD_STATE_EXPECT_REF_UPDATE;
1105 log_debug("uid %d: expecting ref-update-lines",
1106 client->euid);
1107 } else
1108 fatalx("client %d is both reading and writing",
1109 client->euid);
1110 break;
1111 case GOTD_IMSG_WANT:
1112 if (client->state != GOTD_STATE_EXPECT_WANT) {
1113 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1114 "unexpected want-line received");
1115 break;
1117 log_debug("received want-line from uid %d",
1118 client->euid);
1119 err = ensure_client_is_reading(client);
1120 if (err)
1121 break;
1122 err = forward_want(client, &imsg);
1123 break;
1124 case GOTD_IMSG_REF_UPDATE:
1125 if (client->state != GOTD_STATE_EXPECT_REF_UPDATE) {
1126 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1127 "unexpected ref-update-line received");
1128 break;
1130 log_debug("received ref-update-line from uid %d",
1131 client->euid);
1132 err = ensure_client_is_writing(client);
1133 if (err)
1134 break;
1135 err = forward_ref_update(client, &imsg);
1136 if (err)
1137 break;
1138 client->state = GOTD_STATE_EXPECT_MORE_REF_UPDATES;
1139 break;
1140 case GOTD_IMSG_HAVE:
1141 if (client->state != GOTD_STATE_EXPECT_HAVE) {
1142 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1143 "unexpected have-line received");
1144 break;
1146 log_debug("received have-line from uid %d",
1147 client->euid);
1148 err = ensure_client_is_reading(client);
1149 if (err)
1150 break;
1151 err = forward_have(client, &imsg);
1152 if (err)
1153 break;
1154 break;
1155 case GOTD_IMSG_FLUSH:
1156 if (client->state == GOTD_STATE_EXPECT_WANT ||
1157 client->state == GOTD_STATE_EXPECT_HAVE) {
1158 err = ensure_client_is_reading(client);
1159 if (err)
1160 break;
1161 } else if (client->state ==
1162 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1163 err = ensure_client_is_writing(client);
1164 if (err)
1165 break;
1166 } else {
1167 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1168 "unexpected flush-pkt received");
1169 break;
1171 log_debug("received flush-pkt from uid %d",
1172 client->euid);
1173 if (client->state == GOTD_STATE_EXPECT_WANT) {
1174 client->state = GOTD_STATE_EXPECT_HAVE;
1175 log_debug("uid %d: expecting have-lines",
1176 client->euid);
1177 } else if (client->state == GOTD_STATE_EXPECT_HAVE) {
1178 client->state = GOTD_STATE_EXPECT_DONE;
1179 log_debug("uid %d: expecting 'done'",
1180 client->euid);
1181 } else if (client->state ==
1182 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1183 client->state = GOTD_STATE_EXPECT_PACKFILE;
1184 log_debug("uid %d: expecting packfile",
1185 client->euid);
1186 err = recv_packfile(client);
1187 } else {
1188 /* should not happen, see above */
1189 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1190 "unexpected client state");
1191 break;
1193 break;
1194 case GOTD_IMSG_DONE:
1195 if (client->state != GOTD_STATE_EXPECT_HAVE &&
1196 client->state != GOTD_STATE_EXPECT_DONE) {
1197 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1198 "unexpected flush-pkt received");
1199 break;
1201 log_debug("received 'done' from uid %d", client->euid);
1202 err = ensure_client_is_reading(client);
1203 if (err)
1204 break;
1205 client->state = GOTD_STATE_DONE;
1206 err = send_packfile(client);
1207 break;
1208 default:
1209 err = got_error(GOT_ERR_PRIVSEP_MSG);
1210 break;
1213 imsg_free(&imsg);
1216 if (err) {
1217 if (err->code != GOT_ERR_EOF ||
1218 client->state != GOTD_STATE_EXPECT_PACKFILE)
1219 disconnect_on_error(client, err);
1220 } else {
1221 gotd_imsg_event_add(&client->iev);
1222 evtimer_add(&client->tmo, &timeout);
1226 static void
1227 gotd_request_timeout(int fd, short events, void *arg)
1229 struct gotd_client *client = arg;
1231 log_debug("disconnecting uid %d due to timeout", client->euid);
1232 disconnect(client);
1235 static void
1236 gotd_accept(int fd, short event, void *arg)
1238 struct sockaddr_storage ss;
1239 struct timeval backoff;
1240 socklen_t len;
1241 int s = -1;
1242 struct gotd_client *client = NULL;
1243 uid_t euid;
1244 gid_t egid;
1246 backoff.tv_sec = 1;
1247 backoff.tv_usec = 0;
1249 if (event_add(&gotd.ev, NULL) == -1) {
1250 log_warn("event_add");
1251 return;
1253 if (event & EV_TIMEOUT)
1254 return;
1256 len = sizeof(ss);
1258 s = accept_reserve(fd, (struct sockaddr *)&ss, &len, GOTD_FD_RESERVE,
1259 &inflight);
1261 if (s == -1) {
1262 switch (errno) {
1263 case EINTR:
1264 case EWOULDBLOCK:
1265 case ECONNABORTED:
1266 return;
1267 case EMFILE:
1268 case ENFILE:
1269 event_del(&gotd.ev);
1270 evtimer_add(&gotd.pause, &backoff);
1271 return;
1272 default:
1273 log_warn("%s: accept", __func__);
1274 return;
1278 if (client_cnt >= GOTD_MAXCLIENTS)
1279 goto err;
1281 if (getpeereid(s, &euid, &egid) == -1) {
1282 log_warn("%s: getpeereid", __func__);
1283 goto err;
1286 client = calloc(1, sizeof(*client));
1287 if (client == NULL) {
1288 log_warn("%s: calloc", __func__);
1289 goto err;
1292 client->state = GOTD_STATE_EXPECT_LIST_REFS;
1293 client->id = get_client_id();
1294 client->fd = s;
1295 s = -1;
1296 client->delta_cache_fd = -1;
1297 client->euid = euid;
1298 client->egid = egid;
1299 client->nref_updates = -1;
1301 imsg_init(&client->iev.ibuf, client->fd);
1302 client->iev.handler = gotd_request;
1303 client->iev.events = EV_READ;
1304 client->iev.handler_arg = client;
1306 event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
1307 &client->iev);
1308 gotd_imsg_event_add(&client->iev);
1310 evtimer_set(&client->tmo, gotd_request_timeout, client);
1312 add_client(client);
1313 log_debug("%s: new client uid %d connected on fd %d", __func__,
1314 client->euid, client->fd);
1315 return;
1316 err:
1317 inflight--;
1318 if (s != -1)
1319 close(s);
1320 free(client);
1323 static void
1324 gotd_accept_paused(int fd, short event, void *arg)
1326 event_add(&gotd.ev, NULL);
1329 static const char *gotd_proc_names[PROC_MAX] = {
1330 "parent",
1331 "repo_read",
1332 "repo_write"
1335 static struct gotd_child_proc *
1336 get_proc_for_pid(pid_t pid)
1338 struct gotd_child_proc *proc;
1339 int i;
1341 for (i = 0; i < gotd.nprocs; i++) {
1342 proc = &gotd.procs[i];
1343 if (proc->pid == pid)
1344 return proc;
1347 return NULL;
1350 static void
1351 kill_proc(struct gotd_child_proc *proc, int fatal)
1353 if (fatal) {
1354 log_warnx("sending SIGKILL to PID %d", proc->pid);
1355 kill(proc->pid, SIGKILL);
1356 } else
1357 kill(proc->pid, SIGTERM);
1360 static void
1361 gotd_shutdown(void)
1363 pid_t pid;
1364 int status, i;
1365 struct gotd_child_proc *proc;
1367 for (i = 0; i < gotd.nprocs; i++) {
1368 proc = &gotd.procs[i];
1369 msgbuf_clear(&proc->iev.ibuf.w);
1370 close(proc->iev.ibuf.fd);
1371 kill_proc(proc, 0);
1374 log_debug("waiting for children to terminate");
1375 do {
1376 pid = wait(&status);
1377 if (pid == -1) {
1378 if (errno != EINTR && errno != ECHILD)
1379 fatal("wait");
1380 } else if (WIFSIGNALED(status)) {
1381 proc = get_proc_for_pid(pid);
1382 log_warnx("%s %s child process terminated; signal %d",
1383 proc ? gotd_proc_names[proc->type] : "",
1384 proc ? proc->chroot_path : "", WTERMSIG(status));
1386 } while (pid != -1 || (pid == -1 && errno == EINTR));
1388 log_info("terminating");
1389 exit(0);
1392 void
1393 gotd_sighdlr(int sig, short event, void *arg)
1396 * Normal signal handler rules don't apply because libevent
1397 * decouples for us.
1400 switch (sig) {
1401 case SIGHUP:
1402 log_info("%s: ignoring SIGHUP", __func__);
1403 break;
1404 case SIGUSR1:
1405 log_info("%s: ignoring SIGUSR1", __func__);
1406 break;
1407 case SIGTERM:
1408 case SIGINT:
1409 gotd_shutdown();
1410 log_warnx("gotd terminating");
1411 exit(0);
1412 break;
1413 default:
1414 fatalx("unexpected signal");
1418 static const struct got_error *
1419 ensure_proc_is_reading(struct gotd_client *client,
1420 struct gotd_child_proc *proc)
1422 if (!client_is_reading(client)) {
1423 kill_proc(proc, 1);
1424 return got_error_fmt(GOT_ERR_BAD_PACKET,
1425 "PID %d handled a read-request for uid %d but this "
1426 "user is not reading from a repository", proc->pid,
1427 client->euid);
1430 return NULL;
1433 static const struct got_error *
1434 ensure_proc_is_writing(struct gotd_client *client,
1435 struct gotd_child_proc *proc)
1437 if (!client_is_writing(client)) {
1438 kill_proc(proc, 1);
1439 return got_error_fmt(GOT_ERR_BAD_PACKET,
1440 "PID %d handled a write-request for uid %d but this "
1441 "user is not writing to a repository", proc->pid,
1442 client->euid);
1445 return NULL;
1448 static int
1449 verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
1450 struct imsg *imsg)
1452 const struct got_error *err;
1453 struct gotd_child_proc *client_proc;
1454 int ret = 0;
1456 client_proc = get_client_proc(client);
1457 if (client_proc == NULL)
1458 fatalx("no process found for uid %d", client->euid);
1460 if (proc->pid != client_proc->pid) {
1461 kill_proc(proc, 1);
1462 log_warnx("received message from PID %d for uid %d, while "
1463 "PID %d is the process serving this user",
1464 proc->pid, client->euid, client_proc->pid);
1465 return 0;
1468 switch (imsg->hdr.type) {
1469 case GOTD_IMSG_ERROR:
1470 ret = 1;
1471 break;
1472 case GOTD_IMSG_PACKFILE_DONE:
1473 err = ensure_proc_is_reading(client, proc);
1474 if (err)
1475 log_warnx("uid %d: %s", client->euid, err->msg);
1476 else
1477 ret = 1;
1478 break;
1479 case GOTD_IMSG_PACKFILE_INSTALL:
1480 case GOTD_IMSG_REF_UPDATES_START:
1481 case GOTD_IMSG_REF_UPDATE:
1482 err = ensure_proc_is_writing(client, proc);
1483 if (err)
1484 log_warnx("uid %d: %s", client->euid, err->msg);
1485 else
1486 ret = 1;
1487 break;
1488 default:
1489 log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
1490 break;
1493 return ret;
1496 static const struct got_error *
1497 recv_packfile_done(uint32_t *client_id, struct imsg *imsg)
1499 struct gotd_imsg_packfile_done idone;
1500 size_t datalen;
1502 log_debug("packfile-done received");
1504 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1505 if (datalen != sizeof(idone))
1506 return got_error(GOT_ERR_PRIVSEP_LEN);
1507 memcpy(&idone, imsg->data, sizeof(idone));
1509 *client_id = idone.client_id;
1510 return NULL;
1513 static const struct got_error *
1514 recv_packfile_install(uint32_t *client_id, struct imsg *imsg)
1516 struct gotd_imsg_packfile_install inst;
1517 size_t datalen;
1519 log_debug("packfile-install received");
1521 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1522 if (datalen != sizeof(inst))
1523 return got_error(GOT_ERR_PRIVSEP_LEN);
1524 memcpy(&inst, imsg->data, sizeof(inst));
1526 *client_id = inst.client_id;
1527 return NULL;
1530 static const struct got_error *
1531 recv_ref_updates_start(uint32_t *client_id, struct imsg *imsg)
1533 struct gotd_imsg_ref_updates_start istart;
1534 size_t datalen;
1536 log_debug("ref-updates-start received");
1538 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1539 if (datalen != sizeof(istart))
1540 return got_error(GOT_ERR_PRIVSEP_LEN);
1541 memcpy(&istart, imsg->data, sizeof(istart));
1543 *client_id = istart.client_id;
1544 return NULL;
1547 static const struct got_error *
1548 recv_ref_update(uint32_t *client_id, struct imsg *imsg)
1550 struct gotd_imsg_ref_update iref;
1551 size_t datalen;
1553 log_debug("ref-update received");
1555 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1556 if (datalen < sizeof(iref))
1557 return got_error(GOT_ERR_PRIVSEP_LEN);
1558 memcpy(&iref, imsg->data, sizeof(iref));
1560 *client_id = iref.client_id;
1561 return NULL;
1564 static const struct got_error *
1565 send_ref_update_ok(struct gotd_client *client,
1566 struct gotd_imsg_ref_update *iref, const char *refname)
1568 struct gotd_imsg_ref_update_ok iok;
1569 struct ibuf *wbuf;
1570 size_t len;
1572 memset(&iok, 0, sizeof(iok));
1573 iok.client_id = client->id;
1574 memcpy(iok.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1575 memcpy(iok.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1576 iok.name_len = strlen(refname);
1578 len = sizeof(iok) + iok.name_len;
1579 wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_OK,
1580 PROC_GOTD, gotd.pid, len);
1581 if (wbuf == NULL)
1582 return got_error_from_errno("imsg_create REF_UPDATE_OK");
1584 if (imsg_add(wbuf, &iok, sizeof(iok)) == -1)
1585 return got_error_from_errno("imsg_add REF_UPDATE_OK");
1586 if (imsg_add(wbuf, refname, iok.name_len) == -1)
1587 return got_error_from_errno("imsg_add REF_UPDATE_OK");
1589 wbuf->fd = -1;
1590 imsg_close(&client->iev.ibuf, wbuf);
1591 gotd_imsg_event_add(&client->iev);
1592 return NULL;
1595 static void
1596 send_refs_updated(struct gotd_client *client)
1598 if (gotd_imsg_compose_event(&client->iev,
1599 GOTD_IMSG_REFS_UPDATED, PROC_GOTD, -1, NULL, 0) == -1)
1600 log_warn("imsg compose REFS_UPDATED");
1603 static const struct got_error *
1604 send_ref_update_ng(struct gotd_client *client,
1605 struct gotd_imsg_ref_update *iref, const char *refname,
1606 const char *reason)
1608 const struct got_error *ng_err;
1609 struct gotd_imsg_ref_update_ng ing;
1610 struct ibuf *wbuf;
1611 size_t len;
1613 memset(&ing, 0, sizeof(ing));
1614 ing.client_id = client->id;
1615 memcpy(ing.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1616 memcpy(ing.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1617 ing.name_len = strlen(refname);
1619 ng_err = got_error_fmt(GOT_ERR_REF_BUSY, "%s", reason);
1620 ing.reason_len = strlen(ng_err->msg);
1622 len = sizeof(ing) + ing.name_len + ing.reason_len;
1623 wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_NG,
1624 PROC_GOTD, gotd.pid, len);
1625 if (wbuf == NULL)
1626 return got_error_from_errno("imsg_create REF_UPDATE_NG");
1628 if (imsg_add(wbuf, &ing, sizeof(ing)) == -1)
1629 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1630 if (imsg_add(wbuf, refname, ing.name_len) == -1)
1631 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1632 if (imsg_add(wbuf, ng_err->msg, ing.reason_len) == -1)
1633 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1635 wbuf->fd = -1;
1636 imsg_close(&client->iev.ibuf, wbuf);
1637 gotd_imsg_event_add(&client->iev);
1638 return NULL;
1641 static const struct got_error *
1642 install_pack(struct gotd_client *client, const char *repo_path,
1643 struct imsg *imsg)
1645 const struct got_error *err = NULL;
1646 struct gotd_imsg_packfile_install inst;
1647 char hex[SHA1_DIGEST_STRING_LENGTH];
1648 size_t datalen;
1649 char *packfile_path = NULL, *packidx_path = NULL;
1651 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1652 if (datalen != sizeof(inst))
1653 return got_error(GOT_ERR_PRIVSEP_LEN);
1654 memcpy(&inst, imsg->data, sizeof(inst));
1656 if (client->packfile_path == NULL)
1657 return got_error_msg(GOT_ERR_BAD_REQUEST,
1658 "client has no pack file");
1659 if (client->packidx_path == NULL)
1660 return got_error_msg(GOT_ERR_BAD_REQUEST,
1661 "client has no pack file index");
1663 if (got_sha1_digest_to_str(inst.pack_sha1, hex, sizeof(hex)) == NULL)
1664 return got_error_msg(GOT_ERR_NO_SPACE,
1665 "could not convert pack file SHA1 to hex");
1667 if (asprintf(&packfile_path, "/%s/%s/pack-%s.pack",
1668 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1669 err = got_error_from_errno("asprintf");
1670 goto done;
1673 if (asprintf(&packidx_path, "/%s/%s/pack-%s.idx",
1674 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1675 err = got_error_from_errno("asprintf");
1676 goto done;
1679 if (rename(client->packfile_path, packfile_path) == -1) {
1680 err = got_error_from_errno3("rename", client->packfile_path,
1681 packfile_path);
1682 goto done;
1685 free(client->packfile_path);
1686 client->packfile_path = NULL;
1688 if (rename(client->packidx_path, packidx_path) == -1) {
1689 err = got_error_from_errno3("rename", client->packidx_path,
1690 packidx_path);
1691 goto done;
1694 free(client->packidx_path);
1695 client->packidx_path = NULL;
1696 done:
1697 free(packfile_path);
1698 free(packidx_path);
1699 return err;
1702 static const struct got_error *
1703 begin_ref_updates(struct gotd_client *client, struct imsg *imsg)
1705 struct gotd_imsg_ref_updates_start istart;
1706 size_t datalen;
1708 if (client->nref_updates != -1)
1709 return got_error(GOT_ERR_PRIVSEP_MSG);
1711 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1712 if (datalen != sizeof(istart))
1713 return got_error(GOT_ERR_PRIVSEP_LEN);
1714 memcpy(&istart, imsg->data, sizeof(istart));
1716 if (istart.nref_updates <= 0)
1717 return got_error(GOT_ERR_PRIVSEP_MSG);
1719 client->nref_updates = istart.nref_updates;
1720 return NULL;
1723 static const struct got_error *
1724 update_ref(struct gotd_client *client, const char *repo_path,
1725 struct imsg *imsg)
1727 const struct got_error *err = NULL;
1728 struct got_repository *repo = NULL;
1729 struct got_reference *ref = NULL;
1730 struct gotd_imsg_ref_update iref;
1731 struct got_object_id old_id, new_id;
1732 struct got_object_id *id = NULL;
1733 struct got_object *obj = NULL;
1734 char *refname = NULL;
1735 size_t datalen;
1736 int locked = 0;
1738 log_debug("update-ref from uid %d", client->euid);
1740 if (client->nref_updates <= 0)
1741 return got_error(GOT_ERR_PRIVSEP_MSG);
1743 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1744 if (datalen < sizeof(iref))
1745 return got_error(GOT_ERR_PRIVSEP_LEN);
1746 memcpy(&iref, imsg->data, sizeof(iref));
1747 if (datalen != sizeof(iref) + iref.name_len)
1748 return got_error(GOT_ERR_PRIVSEP_LEN);
1749 refname = malloc(iref.name_len + 1);
1750 if (refname == NULL)
1751 return got_error_from_errno("malloc");
1752 memcpy(refname, imsg->data + sizeof(iref), iref.name_len);
1753 refname[iref.name_len] = '\0';
1755 log_debug("updating ref %s for uid %d", refname, client->euid);
1757 err = got_repo_open(&repo, repo_path, NULL, NULL);
1758 if (err)
1759 goto done;
1761 memcpy(old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
1762 memcpy(new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
1763 err = got_object_open(&obj, repo, &new_id);
1764 if (err)
1765 goto done;
1767 if (iref.ref_is_new) {
1768 err = got_ref_open(&ref, repo, refname, 0);
1769 if (err) {
1770 if (err->code != GOT_ERR_NOT_REF)
1771 goto done;
1772 err = got_ref_alloc(&ref, refname, &new_id);
1773 if (err)
1774 goto done;
1775 err = got_ref_write(ref, repo); /* will lock/unlock */
1776 if (err)
1777 goto done;
1778 } else {
1779 err = got_error_fmt(GOT_ERR_REF_BUSY,
1780 "%s has been created by someone else "
1781 "while transaction was in progress",
1782 got_ref_get_name(ref));
1783 goto done;
1785 } else {
1786 err = got_ref_open(&ref, repo, refname, 1 /* lock */);
1787 if (err)
1788 goto done;
1789 locked = 1;
1791 err = got_ref_resolve(&id, repo, ref);
1792 if (err)
1793 goto done;
1795 if (got_object_id_cmp(id, &old_id) != 0) {
1796 err = got_error_fmt(GOT_ERR_REF_BUSY,
1797 "%s has been modified by someone else "
1798 "while transaction was in progress",
1799 got_ref_get_name(ref));
1800 goto done;
1803 err = got_ref_change_ref(ref, &new_id);
1804 if (err)
1805 goto done;
1807 err = got_ref_write(ref, repo);
1808 if (err)
1809 goto done;
1811 free(id);
1812 id = NULL;
1814 done:
1815 if (err) {
1816 if (err->code == GOT_ERR_LOCKFILE_TIMEOUT) {
1817 err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
1818 "could not acquire exclusive file lock for %s",
1819 refname);
1821 send_ref_update_ng(client, &iref, refname, err->msg);
1822 } else
1823 send_ref_update_ok(client, &iref, refname);
1825 if (client->nref_updates > 0) {
1826 client->nref_updates--;
1827 if (client->nref_updates == 0)
1828 send_refs_updated(client);
1831 if (locked) {
1832 const struct got_error *unlock_err;
1833 unlock_err = got_ref_unlock(ref);
1834 if (unlock_err && err == NULL)
1835 err = unlock_err;
1837 if (ref)
1838 got_ref_close(ref);
1839 if (obj)
1840 got_object_close(obj);
1841 if (repo)
1842 got_repo_close(repo);
1843 free(refname);
1844 free(id);
1845 return err;
1848 static void
1849 gotd_dispatch(int fd, short event, void *arg)
1851 struct gotd_imsgev *iev = arg;
1852 struct imsgbuf *ibuf = &iev->ibuf;
1853 struct gotd_child_proc *proc = NULL;
1854 ssize_t n;
1855 int shut = 0;
1856 struct imsg imsg;
1858 if (event & EV_READ) {
1859 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1860 fatal("imsg_read error");
1861 if (n == 0) {
1862 /* Connection closed. */
1863 shut = 1;
1864 goto done;
1868 if (event & EV_WRITE) {
1869 n = msgbuf_write(&ibuf->w);
1870 if (n == -1 && errno != EAGAIN)
1871 fatal("msgbuf_write");
1872 if (n == 0) {
1873 /* Connection closed. */
1874 shut = 1;
1875 goto done;
1879 proc = find_proc_by_fd(fd);
1880 if (proc == NULL)
1881 fatalx("cannot find child process for fd %d", fd);
1883 for (;;) {
1884 const struct got_error *err = NULL;
1885 struct gotd_client *client = NULL;
1886 uint32_t client_id = 0;
1887 int do_disconnect = 0;
1888 int do_ref_updates = 0, do_ref_update = 0;
1889 int do_packfile_install = 0;
1891 if ((n = imsg_get(ibuf, &imsg)) == -1)
1892 fatal("%s: imsg_get error", __func__);
1893 if (n == 0) /* No more messages. */
1894 break;
1896 switch (imsg.hdr.type) {
1897 case GOTD_IMSG_ERROR:
1898 do_disconnect = 1;
1899 err = gotd_imsg_recv_error(&client_id, &imsg);
1900 break;
1901 case GOTD_IMSG_PACKFILE_DONE:
1902 do_disconnect = 1;
1903 err = recv_packfile_done(&client_id, &imsg);
1904 break;
1905 case GOTD_IMSG_PACKFILE_INSTALL:
1906 err = recv_packfile_install(&client_id, &imsg);
1907 if (err == NULL)
1908 do_packfile_install = 1;
1909 break;
1910 case GOTD_IMSG_REF_UPDATES_START:
1911 err = recv_ref_updates_start(&client_id, &imsg);
1912 if (err == NULL)
1913 do_ref_updates = 1;
1914 break;
1915 case GOTD_IMSG_REF_UPDATE:
1916 err = recv_ref_update(&client_id, &imsg);
1917 if (err == NULL)
1918 do_ref_update = 1;
1919 break;
1920 default:
1921 log_debug("unexpected imsg %d", imsg.hdr.type);
1922 break;
1925 client = find_client(client_id);
1926 if (client == NULL) {
1927 log_warnx("%s: client not found", __func__);
1928 imsg_free(&imsg);
1929 continue;
1932 if (!verify_imsg_src(client, proc, &imsg)) {
1933 log_debug("dropping imsg type %d from PID %d",
1934 imsg.hdr.type, proc->pid);
1935 imsg_free(&imsg);
1936 continue;
1938 if (err)
1939 log_warnx("uid %d: %s", client->euid, err->msg);
1941 if (do_disconnect) {
1942 if (err)
1943 disconnect_on_error(client, err);
1944 else
1945 disconnect(client);
1946 } else {
1947 if (do_packfile_install)
1948 err = install_pack(client, proc->chroot_path,
1949 &imsg);
1950 else if (do_ref_updates)
1951 err = begin_ref_updates(client, &imsg);
1952 else if (do_ref_update)
1953 err = update_ref(client, proc->chroot_path,
1954 &imsg);
1955 if (err)
1956 log_warnx("uid %d: %s", client->euid, err->msg);
1958 imsg_free(&imsg);
1960 done:
1961 if (!shut) {
1962 gotd_imsg_event_add(iev);
1963 } else {
1964 /* This pipe is dead. Remove its event handler */
1965 event_del(&iev->ev);
1966 event_loopexit(NULL);
1970 static pid_t
1971 start_child(enum gotd_procid proc_id, const char *chroot_path,
1972 char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
1974 char *argv[11];
1975 int argc = 0;
1976 pid_t pid;
1978 switch (pid = fork()) {
1979 case -1:
1980 fatal("cannot fork");
1981 case 0:
1982 break;
1983 default:
1984 close(fd);
1985 return pid;
1988 if (fd != GOTD_SOCK_FILENO) {
1989 if (dup2(fd, GOTD_SOCK_FILENO) == -1)
1990 fatal("cannot setup imsg fd");
1991 } else if (fcntl(fd, F_SETFD, 0) == -1)
1992 fatal("cannot setup imsg fd");
1994 argv[argc++] = argv0;
1995 switch (proc_id) {
1996 case PROC_REPO_READ:
1997 argv[argc++] = (char *)"-R";
1998 break;
1999 case PROC_REPO_WRITE:
2000 argv[argc++] = (char *)"-W";
2001 break;
2002 default:
2003 fatalx("invalid process id %d", proc_id);
2006 argv[argc++] = (char *)"-f";
2007 argv[argc++] = (char *)confpath;
2009 argv[argc++] = (char *)"-P";
2010 argv[argc++] = (char *)chroot_path;
2012 if (!daemonize)
2013 argv[argc++] = (char *)"-d";
2014 if (verbosity > 0)
2015 argv[argc++] = (char *)"-v";
2016 if (verbosity > 1)
2017 argv[argc++] = (char *)"-v";
2018 argv[argc++] = NULL;
2020 execvp(argv0, argv);
2021 fatal("execvp");
2024 static void
2025 start_repo_children(struct gotd *gotd, char *argv0, const char *confpath,
2026 int daemonize, int verbosity)
2028 struct gotd_repo *repo = NULL;
2029 struct gotd_child_proc *proc;
2030 int i;
2033 * XXX For now, use one reader and one writer per repository.
2034 * This should be changed to N readers + M writers.
2036 gotd->nprocs = gotd->nrepos * 2;
2037 gotd->procs = calloc(gotd->nprocs, sizeof(*gotd->procs));
2038 if (gotd->procs == NULL)
2039 fatal("calloc");
2040 for (i = 0; i < gotd->nprocs; i++) {
2041 if (repo == NULL)
2042 repo = TAILQ_FIRST(&gotd->repos);
2043 proc = &gotd->procs[i];
2044 if (i < gotd->nrepos)
2045 proc->type = PROC_REPO_READ;
2046 else
2047 proc->type = PROC_REPO_WRITE;
2048 if (strlcpy(proc->repo_name, repo->name,
2049 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
2050 fatalx("repository name too long: %s", repo->name);
2051 log_debug("adding repository %s", repo->name);
2052 if (realpath(repo->path, proc->chroot_path) == NULL)
2053 fatal("%s", repo->path);
2054 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
2055 PF_UNSPEC, proc->pipe) == -1)
2056 fatal("socketpair");
2057 proc->pid = start_child(proc->type, proc->chroot_path, argv0,
2058 confpath, proc->pipe[1], daemonize, verbosity);
2059 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
2060 log_debug("proc %s %s is on fd %d",
2061 gotd_proc_names[proc->type], proc->chroot_path,
2062 proc->pipe[0]);
2063 proc->iev.handler = gotd_dispatch;
2064 proc->iev.events = EV_READ;
2065 proc->iev.handler_arg = NULL;
2066 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
2067 gotd_dispatch, &proc->iev);
2069 repo = TAILQ_NEXT(repo, entry);
2073 static void
2074 apply_unveil(void)
2076 struct gotd_repo *repo;
2078 TAILQ_FOREACH(repo, &gotd.repos, entry) {
2079 if (unveil(repo->path, "rwc") == -1)
2080 fatal("unveil %s", repo->path);
2083 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
2084 fatal("unveil %s", GOT_TMPDIR_STR);
2086 if (unveil(NULL, NULL) == -1)
2087 fatal("unveil");
2090 int
2091 main(int argc, char **argv)
2093 const struct got_error *error = NULL;
2094 int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
2095 const char *confpath = GOTD_CONF_PATH;
2096 char *argv0 = argv[0];
2097 char title[2048];
2098 gid_t groups[NGROUPS_MAX];
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, groups, &ngroups) == -1)
2170 log_warnx("group membership list truncated");
2172 gr = match_group(groups, ngroups, gotd.unix_group_name);
2173 if (gr == NULL) {
2174 fatalx("cannot start %s: the user running %s "
2175 "must be a secondary member of group %s",
2176 getprogname(), getprogname(), gotd.unix_group_name);
2178 if (gr->gr_gid == pw->pw_gid) {
2179 fatalx("cannot start %s: the user running %s "
2180 "must be a secondary member of group %s, but "
2181 "%s is the user's primary group",
2182 getprogname(), getprogname(), gotd.unix_group_name,
2183 gotd.unix_group_name);
2186 if (proc_id == PROC_GOTD &&
2187 !got_path_is_absolute(gotd.unix_socket_path))
2188 fatalx("bad unix socket path \"%s\": must be an absolute path",
2189 gotd.unix_socket_path);
2191 if (noaction)
2192 return 0;
2194 if (proc_id == PROC_GOTD && verbosity) {
2195 log_info("socket: %s", gotd.unix_socket_path);
2196 log_info("user: %s", pw->pw_name);
2197 log_info("secondary group: %s", gr->gr_name);
2199 fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
2200 gr->gr_gid);
2201 if (fd == -1) {
2202 fatal("cannot listen on unix socket %s",
2203 gotd.unix_socket_path);
2207 if (proc_id == PROC_GOTD) {
2208 gotd.pid = getpid();
2209 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2210 start_repo_children(&gotd, argv0, confpath, daemonize,
2211 verbosity);
2212 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
2213 if (daemonize && daemon(0, 0) == -1)
2214 fatal("daemon");
2215 } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE) {
2216 error = got_repo_pack_fds_open(&pack_fds);
2217 if (error != NULL)
2218 fatalx("cannot open pack tempfiles: %s", error->msg);
2219 error = got_repo_temp_fds_open(&temp_fds);
2220 if (error != NULL)
2221 fatalx("cannot open pack tempfiles: %s", error->msg);
2222 if (repo_path == NULL)
2223 fatalx("repository path not specified");
2224 snprintf(title, sizeof(title), "%s %s",
2225 gotd_proc_names[proc_id], repo_path);
2226 if (chroot(repo_path) == -1)
2227 fatal("chroot");
2228 if (chdir("/") == -1)
2229 fatal("chdir(\"/\")");
2230 if (daemonize && daemon(1, 0) == -1)
2231 fatal("daemon");
2232 } else
2233 fatal("invalid process id %d", proc_id);
2235 setproctitle("%s", title);
2236 log_procinit(title);
2238 /* Drop root privileges. */
2239 if (setgid(pw->pw_gid) == -1)
2240 fatal("setgid %d failed", pw->pw_gid);
2241 if (setuid(pw->pw_uid) == -1)
2242 fatal("setuid %d failed", pw->pw_uid);
2244 event_init();
2246 switch (proc_id) {
2247 case PROC_GOTD:
2248 #ifndef PROFILE
2249 if (pledge("stdio rpath wpath cpath proc getpw sendfd recvfd "
2250 "fattr flock unix unveil", NULL) == -1)
2251 err(1, "pledge");
2252 #endif
2253 break;
2254 case PROC_REPO_READ:
2255 #ifndef PROFILE
2256 if (pledge("stdio rpath recvfd", NULL) == -1)
2257 err(1, "pledge");
2258 #endif
2259 repo_read_main(title, pack_fds, temp_fds);
2260 /* NOTREACHED */
2261 exit(0);
2262 case PROC_REPO_WRITE:
2263 #ifndef PROFILE
2264 if (pledge("stdio rpath recvfd", NULL) == -1)
2265 err(1, "pledge");
2266 #endif
2267 repo_write_main(title, pack_fds, temp_fds);
2268 /* NOTREACHED */
2269 exit(0);
2270 default:
2271 fatal("invalid process id %d", proc_id);
2274 if (proc_id != PROC_GOTD)
2275 fatal("invalid process id %d", proc_id);
2277 apply_unveil();
2279 signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
2280 signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
2281 signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
2282 signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
2283 signal(SIGPIPE, SIG_IGN);
2285 signal_add(&evsigint, NULL);
2286 signal_add(&evsigterm, NULL);
2287 signal_add(&evsighup, NULL);
2288 signal_add(&evsigusr1, NULL);
2290 event_set(&gotd.ev, fd, EV_READ | EV_PERSIST, gotd_accept, NULL);
2291 if (event_add(&gotd.ev, NULL))
2292 fatalx("event add");
2293 evtimer_set(&gotd.pause, gotd_accept_paused, NULL);
2295 event_dispatch();
2297 if (fd != -1)
2298 close(fd);
2299 if (pack_fds)
2300 got_repo_pack_fds_close(pack_fds);
2301 free(repo_path);
2302 return 0;