Blob


1 /*
2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/tree.h>
19 #include <sys/time.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <sys/socket.h>
23 #include <sys/un.h>
24 #include <sys/wait.h>
26 #include <fcntl.h>
27 #include <err.h>
28 #include <errno.h>
29 #include <event.h>
30 #include <limits.h>
31 #include <pwd.h>
32 #include <grp.h>
33 #include <imsg.h>
34 #include <sha1.h>
35 #include <signal.h>
36 #include <siphash.h>
37 #include <stdarg.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <syslog.h>
42 #include <unistd.h>
44 #include "got_error.h"
45 #include "got_opentemp.h"
46 #include "got_path.h"
47 #include "got_repository.h"
48 #include "got_object.h"
49 #include "got_reference.h"
51 #include "got_lib_delta.h"
52 #include "got_lib_object.h"
53 #include "got_lib_object_cache.h"
54 #include "got_lib_sha1.h"
55 #include "got_lib_gitproto.h"
56 #include "got_lib_pack.h"
57 #include "got_lib_repository.h"
59 #include "gotd.h"
60 #include "log.h"
61 #include "listen.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 struct gotd gotd;
97 void gotd_sighdlr(int sig, short event, void *arg);
98 static void gotd_shutdown(void);
100 __dead static void
101 usage()
103 fprintf(stderr, "usage: %s [-dv] [-f config-file]\n", getprogname());
104 exit(1);
107 static int
108 unix_socket_listen(const char *unix_socket_path, uid_t uid, gid_t gid)
110 struct sockaddr_un sun;
111 int fd = -1;
112 mode_t old_umask, mode;
114 fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
115 if (fd == -1) {
116 log_warn("socket");
117 return -1;
120 sun.sun_family = AF_UNIX;
121 if (strlcpy(sun.sun_path, unix_socket_path,
122 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
123 log_warnx("%s: name too long", unix_socket_path);
124 close(fd);
125 return -1;
128 if (unlink(unix_socket_path) == -1) {
129 if (errno != ENOENT) {
130 log_warn("unlink %s", unix_socket_path);
131 close(fd);
132 return -1;
136 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
137 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
139 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
140 log_warn("bind: %s", unix_socket_path);
141 close(fd);
142 umask(old_umask);
143 return -1;
146 umask(old_umask);
148 if (chmod(unix_socket_path, mode) == -1) {
149 log_warn("chmod %o %s", mode, unix_socket_path);
150 close(fd);
151 unlink(unix_socket_path);
152 return -1;
155 if (chown(unix_socket_path, uid, gid) == -1) {
156 log_warn("chown %s uid=%d gid=%d", unix_socket_path, uid, gid);
157 close(fd);
158 unlink(unix_socket_path);
159 return -1;
162 if (listen(fd, GOTD_UNIX_SOCKET_BACKLOG) == -1) {
163 log_warn("listen");
164 close(fd);
165 unlink(unix_socket_path);
166 return -1;
169 return fd;
172 static struct group *
173 match_group(gid_t *groups, int ngroups, const char *unix_group_name)
175 struct group *gr;
176 int i;
178 for (i = 0; i < ngroups; i++) {
179 gr = getgrgid(groups[i]);
180 if (gr == NULL) {
181 log_warn("getgrgid %d", groups[i]);
182 continue;
184 if (strcmp(gr->gr_name, unix_group_name) == 0)
185 return gr;
188 return NULL;
191 static uint64_t
192 client_hash(uint32_t client_id)
194 return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
197 static void
198 add_client(struct gotd_client *client)
200 uint64_t slot = client_hash(client->id) % nitems(gotd_clients);
201 STAILQ_INSERT_HEAD(&gotd_clients[slot], client, entry);
202 client_cnt++;
205 static struct gotd_client *
206 find_client(uint32_t client_id)
208 uint64_t slot;
209 struct gotd_client *c;
211 slot = client_hash(client_id) % nitems(gotd_clients);
212 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
213 if (c->id == client_id)
214 return c;
217 return NULL;
220 static struct gotd_child_proc *
221 get_client_proc(struct gotd_client *client)
223 if (client->repo_read && client->repo_write) {
224 fatalx("uid %d is reading and writing in the same session",
225 client->euid);
226 /* NOTREACHED */
229 if (client->repo_read)
230 return client->repo_read;
231 else if (client->repo_write)
232 return client->repo_write;
234 return NULL;
237 static int
238 client_is_reading(struct gotd_client *client)
240 return client->repo_read != NULL;
243 static int
244 client_is_writing(struct gotd_client *client)
246 return client->repo_write != NULL;
249 static const struct got_error *
250 ensure_client_is_reading(struct gotd_client *client)
252 if (!client_is_reading(client)) {
253 return got_error_fmt(GOT_ERR_BAD_PACKET,
254 "uid %d made a read-request but is not reading from "
255 "a repository", client->euid);
258 return NULL;
261 static const struct got_error *
262 ensure_client_is_writing(struct gotd_client *client)
264 if (!client_is_writing(client)) {
265 return got_error_fmt(GOT_ERR_BAD_PACKET,
266 "uid %d made a write-request but is not writing to "
267 "a repository", client->euid);
270 return NULL;
273 static const struct got_error *
274 ensure_client_is_not_writing(struct gotd_client *client)
276 if (client_is_writing(client)) {
277 return got_error_fmt(GOT_ERR_BAD_PACKET,
278 "uid %d made a read-request but is writing to "
279 "a repository", client->euid);
282 return NULL;
285 static const struct got_error *
286 ensure_client_is_not_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 write-request but is reading from "
291 "a repository", client->euid);
294 return NULL;
297 static void
298 disconnect(struct gotd_client *client)
300 struct gotd_imsg_disconnect idisconnect;
301 struct gotd_child_proc *proc = get_client_proc(client);
302 struct gotd_child_proc *listen_proc = &gotd.procs[0];
303 uint64_t slot;
305 log_debug("uid %d: disconnecting", client->euid);
307 idisconnect.client_id = client->id;
308 if (proc) {
309 if (gotd_imsg_compose_event(&proc->iev,
310 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
311 &idisconnect, sizeof(idisconnect)) == -1)
312 log_warn("imsg compose DISCONNECT");
315 if (gotd_imsg_compose_event(&listen_proc->iev,
316 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
317 &idisconnect, sizeof(idisconnect)) == -1)
318 log_warn("imsg compose DISCONNECT");
320 slot = client_hash(client->id) % nitems(gotd_clients);
321 STAILQ_REMOVE(&gotd_clients[slot], client, gotd_client, entry);
322 imsg_clear(&client->iev.ibuf);
323 event_del(&client->iev.ev);
324 evtimer_del(&client->tmo);
325 close(client->fd);
326 if (client->delta_cache_fd != -1)
327 close(client->delta_cache_fd);
328 if (client->packfile_path) {
329 if (unlink(client->packfile_path) == -1 && errno != ENOENT)
330 log_warn("unlink %s: ", client->packfile_path);
331 free(client->packfile_path);
333 if (client->packidx_path) {
334 if (unlink(client->packidx_path) == -1 && errno != ENOENT)
335 log_warn("unlink %s: ", client->packidx_path);
336 free(client->packidx_path);
338 free(client->capabilities);
339 free(client);
340 client_cnt--;
343 static void
344 disconnect_on_error(struct gotd_client *client, const struct got_error *err)
346 struct imsgbuf ibuf;
348 log_warnx("uid %d: %s", client->euid, err->msg);
349 if (err->code != GOT_ERR_EOF) {
350 imsg_init(&ibuf, client->fd);
351 gotd_imsg_send_error(&ibuf, 0, PROC_GOTD, err);
352 imsg_clear(&ibuf);
354 disconnect(client);
357 static const struct got_error *
358 send_repo_info(struct gotd_imsgev *iev, struct gotd_repo *repo)
360 const struct got_error *err = NULL;
361 struct gotd_imsg_info_repo irepo;
363 memset(&irepo, 0, sizeof(irepo));
365 if (strlcpy(irepo.repo_name, repo->name, sizeof(irepo.repo_name))
366 >= sizeof(irepo.repo_name))
367 return got_error_msg(GOT_ERR_NO_SPACE, "repo name too long");
368 if (strlcpy(irepo.repo_path, repo->path, sizeof(irepo.repo_path))
369 >= sizeof(irepo.repo_path))
370 return got_error_msg(GOT_ERR_NO_SPACE, "repo path too long");
372 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_REPO, PROC_GOTD, -1,
373 &irepo, sizeof(irepo)) == -1) {
374 err = got_error_from_errno("imsg compose INFO_REPO");
375 if (err)
376 return err;
379 return NULL;
382 static const struct got_error *
383 send_capability(struct gotd_client_capability *capa, struct gotd_imsgev* iev)
385 const struct got_error *err = NULL;
386 struct gotd_imsg_capability icapa;
387 size_t len;
388 struct ibuf *wbuf;
390 memset(&icapa, 0, sizeof(icapa));
392 icapa.key_len = strlen(capa->key);
393 len = sizeof(icapa) + icapa.key_len;
394 if (capa->value) {
395 icapa.value_len = strlen(capa->value);
396 len += icapa.value_len;
399 wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_CAPABILITY, 0, 0, len);
400 if (wbuf == NULL) {
401 err = got_error_from_errno("imsg_create CAPABILITY");
402 return err;
405 if (imsg_add(wbuf, &icapa, sizeof(icapa)) == -1)
406 return got_error_from_errno("imsg_add CAPABILITY");
407 if (imsg_add(wbuf, capa->key, icapa.key_len) == -1)
408 return got_error_from_errno("imsg_add CAPABILITY");
409 if (capa->value) {
410 if (imsg_add(wbuf, capa->value, icapa.value_len) == -1)
411 return got_error_from_errno("imsg_add CAPABILITY");
414 wbuf->fd = -1;
415 imsg_close(&iev->ibuf, wbuf);
417 gotd_imsg_event_add(iev);
419 return NULL;
422 static const struct got_error *
423 send_client_info(struct gotd_imsgev *iev, struct gotd_client *client)
425 const struct got_error *err = NULL;
426 struct gotd_imsg_info_client iclient;
427 struct gotd_child_proc *proc;
428 size_t i;
430 memset(&iclient, 0, sizeof(iclient));
431 iclient.euid = client->euid;
432 iclient.egid = client->egid;
434 proc = get_client_proc(client);
435 if (proc) {
436 if (strlcpy(iclient.repo_name, proc->repo_path,
437 sizeof(iclient.repo_name)) >= sizeof(iclient.repo_name)) {
438 return got_error_msg(GOT_ERR_NO_SPACE,
439 "repo name too long");
441 if (client_is_writing(client))
442 iclient.is_writing = 1;
445 iclient.state = client->state;
446 iclient.ncapabilities = client->ncapabilities;
448 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_CLIENT, PROC_GOTD, -1,
449 &iclient, sizeof(iclient)) == -1) {
450 err = got_error_from_errno("imsg compose INFO_CLIENT");
451 if (err)
452 return err;
455 for (i = 0; i < client->ncapabilities; i++) {
456 struct gotd_client_capability *capa;
457 capa = &client->capabilities[i];
458 err = send_capability(capa, iev);
459 if (err)
460 return err;
463 return NULL;
466 static const struct got_error *
467 send_info(struct gotd_client *client)
469 const struct got_error *err = NULL;
470 struct gotd_imsg_info info;
471 uint64_t slot;
472 struct gotd_repo *repo;
474 info.pid = gotd.pid;
475 info.verbosity = gotd.verbosity;
476 info.nrepos = gotd.nrepos;
477 info.nclients = client_cnt - 1;
479 if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_INFO, PROC_GOTD, -1,
480 &info, sizeof(info)) == -1) {
481 err = got_error_from_errno("imsg compose INFO");
482 if (err)
483 return err;
486 TAILQ_FOREACH(repo, &gotd.repos, entry) {
487 err = send_repo_info(&client->iev, repo);
488 if (err)
489 return err;
492 for (slot = 0; slot < nitems(gotd_clients); slot++) {
493 struct gotd_client *c;
494 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
495 if (c->id == client->id)
496 continue;
497 err = send_client_info(&client->iev, c);
498 if (err)
499 return err;
503 return NULL;
506 static const struct got_error *
507 stop_gotd(struct gotd_client *client)
510 if (client->euid != 0)
511 return got_error_set_errno(EPERM, "stop");
513 gotd_shutdown();
514 /* NOTREACHED */
515 return NULL;
518 static struct gotd_repo *
519 find_repo_by_name(const char *repo_name)
521 struct gotd_repo *repo;
522 size_t namelen;
524 TAILQ_FOREACH(repo, &gotd.repos, entry) {
525 namelen = strlen(repo->name);
526 if (strncmp(repo->name, repo_name, namelen) != 0)
527 continue;
528 if (repo_name[namelen] == '\0' ||
529 strcmp(&repo_name[namelen], ".git") == 0)
530 return repo;
533 return NULL;
536 static struct gotd_child_proc *
537 find_proc_by_repo_name(enum gotd_procid proc_id, const char *repo_name)
539 struct gotd_child_proc *proc;
540 int i;
541 size_t namelen;
543 for (i = 0; i < gotd.nprocs; i++) {
544 proc = &gotd.procs[i];
545 if (proc->type != proc_id)
546 continue;
547 namelen = strlen(proc->repo_name);
548 if (strncmp(proc->repo_name, repo_name, namelen) != 0)
549 continue;
550 if (repo_name[namelen] == '\0' ||
551 strcmp(&repo_name[namelen], ".git") == 0)
552 return proc;
555 return NULL;
558 static struct gotd_child_proc *
559 find_proc_by_fd(int fd)
561 struct gotd_child_proc *proc;
562 int i;
564 for (i = 0; i < gotd.nprocs; i++) {
565 proc = &gotd.procs[i];
566 if (proc->iev.ibuf.fd == fd)
567 return proc;
570 return NULL;
573 static const struct got_error *
574 forward_list_refs_request(struct gotd_client *client, struct imsg *imsg)
576 const struct got_error *err;
577 struct gotd_imsg_list_refs ireq;
578 struct gotd_imsg_list_refs_internal ilref;
579 struct gotd_repo *repo = NULL;
580 struct gotd_child_proc *proc = NULL;
581 size_t datalen;
582 int fd = -1;
584 log_debug("list-refs request from uid %d", client->euid);
586 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
587 if (datalen != sizeof(ireq))
588 return got_error(GOT_ERR_PRIVSEP_LEN);
590 memcpy(&ireq, imsg->data, datalen);
592 memset(&ilref, 0, sizeof(ilref));
593 ilref.client_id = client->id;
595 if (ireq.client_is_reading) {
596 err = ensure_client_is_not_writing(client);
597 if (err)
598 return err;
599 repo = find_repo_by_name(ireq.repo_name);
600 if (repo == NULL)
601 return got_error(GOT_ERR_NOT_GIT_REPO);
602 err = gotd_auth_check(&repo->rules, repo->name,
603 client->euid, client->egid, GOTD_AUTH_READ);
604 if (err)
605 return err;
606 client->repo_read = find_proc_by_repo_name(PROC_REPO_READ,
607 ireq.repo_name);
608 if (client->repo_read == NULL)
609 return got_error(GOT_ERR_NOT_GIT_REPO);
610 } else {
611 err = ensure_client_is_not_reading(client);
612 if (err)
613 return err;
614 repo = find_repo_by_name(ireq.repo_name);
615 if (repo == NULL)
616 return got_error(GOT_ERR_NOT_GIT_REPO);
617 err = gotd_auth_check(&repo->rules, repo->name, client->euid,
618 client->egid, GOTD_AUTH_READ | GOTD_AUTH_WRITE);
619 if (err)
620 return err;
621 client->repo_write = find_proc_by_repo_name(PROC_REPO_WRITE,
622 ireq.repo_name);
623 if (client->repo_write == NULL)
624 return got_error(GOT_ERR_NOT_GIT_REPO);
627 fd = dup(client->fd);
628 if (fd == -1)
629 return got_error_from_errno("dup");
631 proc = get_client_proc(client);
632 if (proc == NULL)
633 fatalx("no process found for uid %d", client->euid);
634 if (gotd_imsg_compose_event(&proc->iev,
635 GOTD_IMSG_LIST_REFS_INTERNAL, PROC_GOTD, fd,
636 &ilref, sizeof(ilref)) == -1) {
637 err = got_error_from_errno("imsg compose WANT");
638 close(fd);
639 return err;
642 return NULL;
645 static const struct got_error *
646 forward_want(struct gotd_client *client, struct imsg *imsg)
648 struct gotd_imsg_want ireq;
649 struct gotd_imsg_want iwant;
650 size_t datalen;
652 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
653 if (datalen != sizeof(ireq))
654 return got_error(GOT_ERR_PRIVSEP_LEN);
656 memcpy(&ireq, imsg->data, datalen);
658 memset(&iwant, 0, sizeof(iwant));
659 memcpy(iwant.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
660 iwant.client_id = client->id;
662 if (gotd_imsg_compose_event(&client->repo_read->iev, GOTD_IMSG_WANT,
663 PROC_GOTD, -1, &iwant, sizeof(iwant)) == -1)
664 return got_error_from_errno("imsg compose WANT");
666 return NULL;
669 static const struct got_error *
670 forward_ref_update(struct gotd_client *client, struct imsg *imsg)
672 const struct got_error *err = NULL;
673 struct gotd_imsg_ref_update ireq;
674 struct gotd_imsg_ref_update *iref = NULL;
675 size_t datalen;
677 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
678 if (datalen < sizeof(ireq))
679 return got_error(GOT_ERR_PRIVSEP_LEN);
680 memcpy(&ireq, imsg->data, sizeof(ireq));
681 if (datalen != sizeof(ireq) + ireq.name_len)
682 return got_error(GOT_ERR_PRIVSEP_LEN);
684 iref = malloc(datalen);
685 if (iref == NULL)
686 return got_error_from_errno("malloc");
687 memcpy(iref, imsg->data, datalen);
689 iref->client_id = client->id;
690 if (gotd_imsg_compose_event(&client->repo_write->iev,
691 GOTD_IMSG_REF_UPDATE, PROC_GOTD, -1, iref, datalen) == -1)
692 err = got_error_from_errno("imsg compose REF_UPDATE");
693 free(iref);
694 return err;
697 static const struct got_error *
698 forward_have(struct gotd_client *client, struct imsg *imsg)
700 struct gotd_imsg_have ireq;
701 struct gotd_imsg_have ihave;
702 size_t datalen;
704 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
705 if (datalen != sizeof(ireq))
706 return got_error(GOT_ERR_PRIVSEP_LEN);
708 memcpy(&ireq, imsg->data, datalen);
710 memset(&ihave, 0, sizeof(ihave));
711 memcpy(ihave.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
712 ihave.client_id = client->id;
714 if (gotd_imsg_compose_event(&client->repo_read->iev, GOTD_IMSG_HAVE,
715 PROC_GOTD, -1, &ihave, sizeof(ihave)) == -1)
716 return got_error_from_errno("imsg compose HAVE");
718 return NULL;
721 static int
722 client_has_capability(struct gotd_client *client, const char *capastr)
724 struct gotd_client_capability *capa;
725 size_t i;
727 if (client->ncapabilities == 0)
728 return 0;
730 for (i = 0; i < client->ncapabilities; i++) {
731 capa = &client->capabilities[i];
732 if (strcmp(capa->key, capastr) == 0)
733 return 1;
736 return 0;
739 static const struct got_error *
740 recv_capabilities(struct gotd_client *client, struct imsg *imsg)
742 struct gotd_imsg_capabilities icapas;
743 size_t datalen;
745 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
746 if (datalen != sizeof(icapas))
747 return got_error(GOT_ERR_PRIVSEP_LEN);
748 memcpy(&icapas, imsg->data, sizeof(icapas));
750 client->ncapa_alloc = icapas.ncapabilities;
751 client->capabilities = calloc(client->ncapa_alloc,
752 sizeof(*client->capabilities));
753 if (client->capabilities == NULL) {
754 client->ncapa_alloc = 0;
755 return got_error_from_errno("calloc");
758 log_debug("expecting %zu capabilities from uid %d",
759 client->ncapa_alloc, client->euid);
760 return NULL;
763 static const struct got_error *
764 recv_capability(struct gotd_client *client, struct imsg *imsg)
766 struct gotd_imsg_capability icapa;
767 struct gotd_client_capability *capa;
768 size_t datalen;
769 char *key, *value = NULL;
771 if (client->capabilities == NULL ||
772 client->ncapabilities >= client->ncapa_alloc) {
773 return got_error_msg(GOT_ERR_BAD_REQUEST,
774 "unexpected capability received");
777 memset(&icapa, 0, sizeof(icapa));
779 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
780 if (datalen < sizeof(icapa))
781 return got_error(GOT_ERR_PRIVSEP_LEN);
782 memcpy(&icapa, imsg->data, sizeof(icapa));
784 if (datalen != sizeof(icapa) + icapa.key_len + icapa.value_len)
785 return got_error(GOT_ERR_PRIVSEP_LEN);
787 key = malloc(icapa.key_len + 1);
788 if (key == NULL)
789 return got_error_from_errno("malloc");
790 if (icapa.value_len > 0) {
791 value = malloc(icapa.value_len + 1);
792 if (value == NULL) {
793 free(key);
794 return got_error_from_errno("malloc");
798 memcpy(key, imsg->data + sizeof(icapa), icapa.key_len);
799 key[icapa.key_len] = '\0';
800 if (value) {
801 memcpy(value, imsg->data + sizeof(icapa) + icapa.key_len,
802 icapa.value_len);
803 value[icapa.value_len] = '\0';
806 capa = &client->capabilities[client->ncapabilities++];
807 capa->key = key;
808 capa->value = value;
810 if (value)
811 log_debug("uid %d: capability %s=%s", client->euid, key, value);
812 else
813 log_debug("uid %d: capability %s", client->euid, key);
815 return NULL;
818 static const struct got_error *
819 send_packfile(struct gotd_client *client)
821 const struct got_error *err = NULL;
822 struct gotd_imsg_send_packfile ipack;
823 struct gotd_imsg_packfile_pipe ipipe;
824 int pipe[2];
826 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
827 return got_error_from_errno("socketpair");
829 memset(&ipack, 0, sizeof(ipack));
830 memset(&ipipe, 0, sizeof(ipipe));
832 ipack.client_id = client->id;
833 if (client_has_capability(client, GOT_CAPA_SIDE_BAND_64K))
834 ipack.report_progress = 1;
836 client->delta_cache_fd = got_opentempfd();
837 if (client->delta_cache_fd == -1)
838 return got_error_from_errno("got_opentempfd");
840 if (gotd_imsg_compose_event(&client->repo_read->iev,
841 GOTD_IMSG_SEND_PACKFILE, PROC_GOTD, client->delta_cache_fd,
842 &ipack, sizeof(ipack)) == -1) {
843 err = got_error_from_errno("imsg compose SEND_PACKFILE");
844 close(pipe[0]);
845 close(pipe[1]);
846 return err;
849 ipipe.client_id = client->id;
851 /* Send pack pipe end 0 to repo_read. */
852 if (gotd_imsg_compose_event(&client->repo_read->iev,
853 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[0],
854 &ipipe, sizeof(ipipe)) == -1) {
855 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
856 close(pipe[1]);
857 return err;
860 /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
861 if (gotd_imsg_compose_event(&client->iev,
862 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1], NULL, 0) == -1)
863 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
865 return err;
868 static const struct got_error *
869 recv_packfile(struct gotd_client *client)
871 const struct got_error *err = NULL;
872 struct gotd_imsg_recv_packfile ipack;
873 struct gotd_imsg_packfile_pipe ipipe;
874 struct gotd_imsg_packidx_file ifile;
875 char *basepath = NULL, *pack_path = NULL, *idx_path = NULL;
876 int packfd = -1, idxfd = -1;
877 int pipe[2] = { -1, -1 };
879 if (client->packfile_path) {
880 return got_error_fmt(GOT_ERR_PRIVSEP_MSG,
881 "uid %d already has a pack file", client->euid);
884 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
885 return got_error_from_errno("socketpair");
887 memset(&ipipe, 0, sizeof(ipipe));
888 ipipe.client_id = client->id;
890 if (gotd_imsg_compose_event(&client->repo_write->iev,
891 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[0],
892 &ipipe, sizeof(ipipe)) == -1) {
893 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
894 pipe[0] = -1;
895 goto done;
897 pipe[0] = -1;
899 if (gotd_imsg_compose_event(&client->repo_write->iev,
900 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1],
901 &ipipe, sizeof(ipipe)) == -1)
902 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
903 pipe[1] = -1;
905 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.pack",
906 client->repo_write->repo_path, GOT_OBJECTS_PACK_DIR,
907 client->euid) == -1) {
908 err = got_error_from_errno("asprintf");
909 goto done;
912 err = got_opentemp_named_fd(&pack_path, &packfd, basepath, "");
913 if (err)
914 goto done;
916 free(basepath);
917 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.idx",
918 client->repo_write->repo_path, GOT_OBJECTS_PACK_DIR,
919 client->euid) == -1) {
920 err = got_error_from_errno("asprintf");
921 basepath = NULL;
922 goto done;
924 err = got_opentemp_named_fd(&idx_path, &idxfd, basepath, "");
925 if (err)
926 goto done;
928 memset(&ifile, 0, sizeof(ifile));
929 ifile.client_id = client->id;
930 if (gotd_imsg_compose_event(&client->repo_write->iev,
931 GOTD_IMSG_PACKIDX_FILE, PROC_GOTD, idxfd,
932 &ifile, sizeof(ifile)) == -1) {
933 err = got_error_from_errno("imsg compose PACKIDX_FILE");
934 idxfd = -1;
935 goto done;
937 idxfd = -1;
939 memset(&ipack, 0, sizeof(ipack));
940 ipack.client_id = client->id;
941 if (client_has_capability(client, GOT_CAPA_REPORT_STATUS))
942 ipack.report_status = 1;
944 if (gotd_imsg_compose_event(&client->repo_write->iev,
945 GOTD_IMSG_RECV_PACKFILE, PROC_GOTD, packfd,
946 &ipack, sizeof(ipack)) == -1) {
947 err = got_error_from_errno("imsg compose RECV_PACKFILE");
948 packfd = -1;
949 goto done;
951 packfd = -1;
953 done:
954 free(basepath);
955 if (pipe[0] != -1 && close(pipe[0]) == -1 && err == NULL)
956 err = got_error_from_errno("close");
957 if (pipe[1] != -1 && close(pipe[1]) == -1 && err == NULL)
958 err = got_error_from_errno("close");
959 if (packfd != -1 && close(packfd) == -1 && err == NULL)
960 err = got_error_from_errno("close");
961 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
962 err = got_error_from_errno("close");
963 if (err) {
964 free(pack_path);
965 free(idx_path);
966 } else {
967 client->packfile_path = pack_path;
968 client->packidx_path = idx_path;
970 return err;
973 static void
974 gotd_request(int fd, short events, void *arg)
976 struct gotd_imsgev *iev = arg;
977 struct imsgbuf *ibuf = &iev->ibuf;
978 struct gotd_client *client = iev->handler_arg;
979 const struct got_error *err = NULL;
980 struct imsg imsg;
981 ssize_t n;
983 if (events & EV_WRITE) {
984 while (ibuf->w.queued) {
985 n = msgbuf_write(&ibuf->w);
986 if (n == -1 && errno == EPIPE) {
987 /*
988 * The client has closed its socket.
989 * This can happen when Git clients are
990 * done sending pack file data.
991 */
992 msgbuf_clear(&ibuf->w);
993 continue;
994 } else if (n == -1 && errno != EAGAIN) {
995 err = got_error_from_errno("imsg_flush");
996 disconnect_on_error(client, err);
997 return;
999 if (n == 0) {
1000 /* Connection closed. */
1001 err = got_error(GOT_ERR_EOF);
1002 disconnect_on_error(client, err);
1003 return;
1007 /* Disconnect gotctl(8) now that messages have been sent. */
1008 if (!client_is_reading(client) && !client_is_writing(client)) {
1009 disconnect(client);
1010 return;
1014 if ((events & EV_READ) == 0)
1015 return;
1017 memset(&imsg, 0, sizeof(imsg));
1019 while (err == NULL) {
1020 err = gotd_imsg_recv(&imsg, ibuf, 0);
1021 if (err) {
1022 if (err->code == GOT_ERR_PRIVSEP_READ)
1023 err = NULL;
1024 break;
1027 evtimer_del(&client->tmo);
1029 switch (imsg.hdr.type) {
1030 case GOTD_IMSG_INFO:
1031 err = send_info(client);
1032 break;
1033 case GOTD_IMSG_STOP:
1034 err = stop_gotd(client);
1035 break;
1036 case GOTD_IMSG_LIST_REFS:
1037 if (client->state != GOTD_STATE_EXPECT_LIST_REFS) {
1038 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1039 "unexpected list-refs request received");
1040 break;
1042 err = forward_list_refs_request(client, &imsg);
1043 if (err)
1044 break;
1045 client->state = GOTD_STATE_EXPECT_CAPABILITIES;
1046 log_debug("uid %d: expecting capabilities",
1047 client->euid);
1048 break;
1049 case GOTD_IMSG_CAPABILITIES:
1050 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1051 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1052 "unexpected capabilities received");
1053 break;
1055 log_debug("receiving capabilities from uid %d",
1056 client->euid);
1057 err = recv_capabilities(client, &imsg);
1058 break;
1059 case GOTD_IMSG_CAPABILITY:
1060 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1061 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1062 "unexpected capability received");
1063 break;
1065 err = recv_capability(client, &imsg);
1066 if (err || client->ncapabilities < client->ncapa_alloc)
1067 break;
1068 if (client_is_reading(client)) {
1069 client->state = GOTD_STATE_EXPECT_WANT;
1070 log_debug("uid %d: expecting want-lines",
1071 client->euid);
1072 } else if (client_is_writing(client)) {
1073 client->state = GOTD_STATE_EXPECT_REF_UPDATE;
1074 log_debug("uid %d: expecting ref-update-lines",
1075 client->euid);
1076 } else
1077 fatalx("client %d is both reading and writing",
1078 client->euid);
1079 break;
1080 case GOTD_IMSG_WANT:
1081 if (client->state != GOTD_STATE_EXPECT_WANT) {
1082 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1083 "unexpected want-line received");
1084 break;
1086 log_debug("received want-line from uid %d",
1087 client->euid);
1088 err = ensure_client_is_reading(client);
1089 if (err)
1090 break;
1091 err = forward_want(client, &imsg);
1092 break;
1093 case GOTD_IMSG_REF_UPDATE:
1094 if (client->state != GOTD_STATE_EXPECT_REF_UPDATE) {
1095 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1096 "unexpected ref-update-line received");
1097 break;
1099 log_debug("received ref-update-line from uid %d",
1100 client->euid);
1101 err = ensure_client_is_writing(client);
1102 if (err)
1103 break;
1104 err = forward_ref_update(client, &imsg);
1105 if (err)
1106 break;
1107 client->state = GOTD_STATE_EXPECT_MORE_REF_UPDATES;
1108 break;
1109 case GOTD_IMSG_HAVE:
1110 if (client->state != GOTD_STATE_EXPECT_HAVE) {
1111 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1112 "unexpected have-line received");
1113 break;
1115 log_debug("received have-line from uid %d",
1116 client->euid);
1117 err = ensure_client_is_reading(client);
1118 if (err)
1119 break;
1120 err = forward_have(client, &imsg);
1121 if (err)
1122 break;
1123 break;
1124 case GOTD_IMSG_FLUSH:
1125 if (client->state == GOTD_STATE_EXPECT_WANT ||
1126 client->state == GOTD_STATE_EXPECT_HAVE) {
1127 err = ensure_client_is_reading(client);
1128 if (err)
1129 break;
1130 } else if (client->state ==
1131 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1132 err = ensure_client_is_writing(client);
1133 if (err)
1134 break;
1135 } else {
1136 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1137 "unexpected flush-pkt received");
1138 break;
1140 log_debug("received flush-pkt from uid %d",
1141 client->euid);
1142 if (client->state == GOTD_STATE_EXPECT_WANT) {
1143 client->state = GOTD_STATE_EXPECT_HAVE;
1144 log_debug("uid %d: expecting have-lines",
1145 client->euid);
1146 } else if (client->state == GOTD_STATE_EXPECT_HAVE) {
1147 client->state = GOTD_STATE_EXPECT_DONE;
1148 log_debug("uid %d: expecting 'done'",
1149 client->euid);
1150 } else if (client->state ==
1151 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1152 client->state = GOTD_STATE_EXPECT_PACKFILE;
1153 log_debug("uid %d: expecting packfile",
1154 client->euid);
1155 err = recv_packfile(client);
1156 } else {
1157 /* should not happen, see above */
1158 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1159 "unexpected client state");
1160 break;
1162 break;
1163 case GOTD_IMSG_DONE:
1164 if (client->state != GOTD_STATE_EXPECT_HAVE &&
1165 client->state != GOTD_STATE_EXPECT_DONE) {
1166 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1167 "unexpected flush-pkt received");
1168 break;
1170 log_debug("received 'done' from uid %d", client->euid);
1171 err = ensure_client_is_reading(client);
1172 if (err)
1173 break;
1174 client->state = GOTD_STATE_DONE;
1175 err = send_packfile(client);
1176 break;
1177 default:
1178 err = got_error(GOT_ERR_PRIVSEP_MSG);
1179 break;
1182 imsg_free(&imsg);
1185 if (err) {
1186 if (err->code != GOT_ERR_EOF ||
1187 client->state != GOTD_STATE_EXPECT_PACKFILE)
1188 disconnect_on_error(client, err);
1189 } else {
1190 gotd_imsg_event_add(&client->iev);
1191 evtimer_add(&client->tmo, &timeout);
1195 static void
1196 gotd_request_timeout(int fd, short events, void *arg)
1198 struct gotd_client *client = arg;
1200 log_debug("disconnecting uid %d due to timeout", client->euid);
1201 disconnect(client);
1204 static const struct got_error *
1205 recv_connect(uint32_t *client_id, struct imsg *imsg)
1207 const struct got_error *err = NULL;
1208 struct gotd_imsg_connect iconnect;
1209 size_t datalen;
1210 int s = -1;
1211 struct gotd_client *client = NULL;
1212 uid_t euid;
1213 gid_t egid;
1215 *client_id = 0;
1217 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1218 if (datalen != sizeof(iconnect))
1219 return got_error(GOT_ERR_PRIVSEP_LEN);
1220 memcpy(&iconnect, imsg->data, sizeof(iconnect));
1222 s = imsg->fd;
1223 if (s == -1) {
1224 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1225 goto done;
1228 if (find_client(iconnect.client_id)) {
1229 err = got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
1230 goto done;
1233 if (getpeereid(s, &euid, &egid) == -1) {
1234 err = got_error_from_errno("getpeerid");
1235 goto done;
1238 client = calloc(1, sizeof(*client));
1239 if (client == NULL) {
1240 err = got_error_from_errno("calloc");
1241 goto done;
1244 *client_id = iconnect.client_id;
1246 client->state = GOTD_STATE_EXPECT_LIST_REFS;
1247 client->id = iconnect.client_id;
1248 client->fd = s;
1249 s = -1;
1250 client->delta_cache_fd = -1;
1251 client->euid = euid;
1252 client->egid = egid;
1253 client->nref_updates = -1;
1255 imsg_init(&client->iev.ibuf, client->fd);
1256 client->iev.handler = gotd_request;
1257 client->iev.events = EV_READ;
1258 client->iev.handler_arg = client;
1260 event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
1261 &client->iev);
1262 gotd_imsg_event_add(&client->iev);
1264 evtimer_set(&client->tmo, gotd_request_timeout, client);
1266 add_client(client);
1267 log_debug("%s: new client uid %d connected on fd %d", __func__,
1268 client->euid, client->fd);
1269 done:
1270 if (err) {
1271 struct gotd_child_proc *listen_proc = &gotd.procs[0];
1272 struct gotd_imsg_disconnect idisconnect;
1274 idisconnect.client_id = client->id;
1275 if (gotd_imsg_compose_event(&listen_proc->iev,
1276 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
1277 &idisconnect, sizeof(idisconnect)) == -1)
1278 log_warn("imsg compose DISCONNECT");
1280 if (s != -1)
1281 close(s);
1284 return err;
1287 static const char *gotd_proc_names[PROC_MAX] = {
1288 "parent",
1289 "listen",
1290 "repo_read",
1291 "repo_write"
1294 static struct gotd_child_proc *
1295 get_proc_for_pid(pid_t pid)
1297 struct gotd_child_proc *proc;
1298 int i;
1300 for (i = 0; i < gotd.nprocs; i++) {
1301 proc = &gotd.procs[i];
1302 if (proc->pid == pid)
1303 return proc;
1306 return NULL;
1309 static void
1310 kill_proc(struct gotd_child_proc *proc, int fatal)
1312 if (fatal) {
1313 log_warnx("sending SIGKILL to PID %d", proc->pid);
1314 kill(proc->pid, SIGKILL);
1315 } else
1316 kill(proc->pid, SIGTERM);
1319 static void
1320 gotd_shutdown(void)
1322 pid_t pid;
1323 int status, i;
1324 struct gotd_child_proc *proc;
1326 for (i = 0; i < gotd.nprocs; i++) {
1327 proc = &gotd.procs[i];
1328 msgbuf_clear(&proc->iev.ibuf.w);
1329 close(proc->iev.ibuf.fd);
1330 kill_proc(proc, 0);
1333 log_debug("waiting for children to terminate");
1334 do {
1335 pid = wait(&status);
1336 if (pid == -1) {
1337 if (errno != EINTR && errno != ECHILD)
1338 fatal("wait");
1339 } else if (WIFSIGNALED(status)) {
1340 proc = get_proc_for_pid(pid);
1341 log_warnx("%s %s child process terminated; signal %d",
1342 proc ? gotd_proc_names[proc->type] : "",
1343 proc ? proc->repo_path : "", WTERMSIG(status));
1345 } while (pid != -1 || (pid == -1 && errno == EINTR));
1347 log_info("terminating");
1348 exit(0);
1351 void
1352 gotd_sighdlr(int sig, short event, void *arg)
1355 * Normal signal handler rules don't apply because libevent
1356 * decouples for us.
1359 switch (sig) {
1360 case SIGHUP:
1361 log_info("%s: ignoring SIGHUP", __func__);
1362 break;
1363 case SIGUSR1:
1364 log_info("%s: ignoring SIGUSR1", __func__);
1365 break;
1366 case SIGTERM:
1367 case SIGINT:
1368 gotd_shutdown();
1369 log_warnx("gotd terminating");
1370 exit(0);
1371 break;
1372 default:
1373 fatalx("unexpected signal");
1377 static const struct got_error *
1378 ensure_proc_is_reading(struct gotd_client *client,
1379 struct gotd_child_proc *proc)
1381 if (!client_is_reading(client)) {
1382 kill_proc(proc, 1);
1383 return got_error_fmt(GOT_ERR_BAD_PACKET,
1384 "PID %d handled a read-request for uid %d but this "
1385 "user is not reading from a repository", proc->pid,
1386 client->euid);
1389 return NULL;
1392 static const struct got_error *
1393 ensure_proc_is_writing(struct gotd_client *client,
1394 struct gotd_child_proc *proc)
1396 if (!client_is_writing(client)) {
1397 kill_proc(proc, 1);
1398 return got_error_fmt(GOT_ERR_BAD_PACKET,
1399 "PID %d handled a write-request for uid %d but this "
1400 "user is not writing to a repository", proc->pid,
1401 client->euid);
1404 return NULL;
1407 static int
1408 verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
1409 struct imsg *imsg)
1411 const struct got_error *err;
1412 struct gotd_child_proc *client_proc;
1413 int ret = 0;
1415 if (proc->type == PROC_REPO_READ || proc->type == PROC_REPO_WRITE) {
1416 client_proc = get_client_proc(client);
1417 if (client_proc == NULL)
1418 fatalx("no process found for uid %d", client->euid);
1419 if (proc->pid != client_proc->pid) {
1420 kill_proc(proc, 1);
1421 log_warnx("received message from PID %d for uid %d, "
1422 "while PID %d is the process serving this user",
1423 proc->pid, client->euid, client_proc->pid);
1424 return 0;
1428 switch (imsg->hdr.type) {
1429 case GOTD_IMSG_ERROR:
1430 ret = 1;
1431 break;
1432 case GOTD_IMSG_CONNECT:
1433 if (proc->type != PROC_LISTEN) {
1434 err = got_error_fmt(GOT_ERR_BAD_PACKET,
1435 "new connection for uid %d from PID %d "
1436 "which is not the listen process",
1437 proc->pid, client->euid);
1438 } else
1439 ret = 1;
1440 break;
1441 case GOTD_IMSG_PACKFILE_DONE:
1442 err = ensure_proc_is_reading(client, proc);
1443 if (err)
1444 log_warnx("uid %d: %s", client->euid, err->msg);
1445 else
1446 ret = 1;
1447 break;
1448 case GOTD_IMSG_PACKFILE_INSTALL:
1449 case GOTD_IMSG_REF_UPDATES_START:
1450 case GOTD_IMSG_REF_UPDATE:
1451 err = ensure_proc_is_writing(client, proc);
1452 if (err)
1453 log_warnx("uid %d: %s", client->euid, err->msg);
1454 else
1455 ret = 1;
1456 break;
1457 default:
1458 log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
1459 break;
1462 return ret;
1465 static const struct got_error *
1466 recv_packfile_done(uint32_t *client_id, struct imsg *imsg)
1468 struct gotd_imsg_packfile_done idone;
1469 size_t datalen;
1471 log_debug("packfile-done received");
1473 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1474 if (datalen != sizeof(idone))
1475 return got_error(GOT_ERR_PRIVSEP_LEN);
1476 memcpy(&idone, imsg->data, sizeof(idone));
1478 *client_id = idone.client_id;
1479 return NULL;
1482 static const struct got_error *
1483 recv_packfile_install(uint32_t *client_id, struct imsg *imsg)
1485 struct gotd_imsg_packfile_install inst;
1486 size_t datalen;
1488 log_debug("packfile-install received");
1490 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1491 if (datalen != sizeof(inst))
1492 return got_error(GOT_ERR_PRIVSEP_LEN);
1493 memcpy(&inst, imsg->data, sizeof(inst));
1495 *client_id = inst.client_id;
1496 return NULL;
1499 static const struct got_error *
1500 recv_ref_updates_start(uint32_t *client_id, struct imsg *imsg)
1502 struct gotd_imsg_ref_updates_start istart;
1503 size_t datalen;
1505 log_debug("ref-updates-start received");
1507 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1508 if (datalen != sizeof(istart))
1509 return got_error(GOT_ERR_PRIVSEP_LEN);
1510 memcpy(&istart, imsg->data, sizeof(istart));
1512 *client_id = istart.client_id;
1513 return NULL;
1516 static const struct got_error *
1517 recv_ref_update(uint32_t *client_id, struct imsg *imsg)
1519 struct gotd_imsg_ref_update iref;
1520 size_t datalen;
1522 log_debug("ref-update received");
1524 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1525 if (datalen < sizeof(iref))
1526 return got_error(GOT_ERR_PRIVSEP_LEN);
1527 memcpy(&iref, imsg->data, sizeof(iref));
1529 *client_id = iref.client_id;
1530 return NULL;
1533 static const struct got_error *
1534 send_ref_update_ok(struct gotd_client *client,
1535 struct gotd_imsg_ref_update *iref, const char *refname)
1537 struct gotd_imsg_ref_update_ok iok;
1538 struct ibuf *wbuf;
1539 size_t len;
1541 memset(&iok, 0, sizeof(iok));
1542 iok.client_id = client->id;
1543 memcpy(iok.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1544 memcpy(iok.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1545 iok.name_len = strlen(refname);
1547 len = sizeof(iok) + iok.name_len;
1548 wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_OK,
1549 PROC_GOTD, gotd.pid, len);
1550 if (wbuf == NULL)
1551 return got_error_from_errno("imsg_create REF_UPDATE_OK");
1553 if (imsg_add(wbuf, &iok, sizeof(iok)) == -1)
1554 return got_error_from_errno("imsg_add REF_UPDATE_OK");
1555 if (imsg_add(wbuf, refname, iok.name_len) == -1)
1556 return got_error_from_errno("imsg_add REF_UPDATE_OK");
1558 wbuf->fd = -1;
1559 imsg_close(&client->iev.ibuf, wbuf);
1560 gotd_imsg_event_add(&client->iev);
1561 return NULL;
1564 static void
1565 send_refs_updated(struct gotd_client *client)
1567 if (gotd_imsg_compose_event(&client->iev,
1568 GOTD_IMSG_REFS_UPDATED, PROC_GOTD, -1, NULL, 0) == -1)
1569 log_warn("imsg compose REFS_UPDATED");
1572 static const struct got_error *
1573 send_ref_update_ng(struct gotd_client *client,
1574 struct gotd_imsg_ref_update *iref, const char *refname,
1575 const char *reason)
1577 const struct got_error *ng_err;
1578 struct gotd_imsg_ref_update_ng ing;
1579 struct ibuf *wbuf;
1580 size_t len;
1582 memset(&ing, 0, sizeof(ing));
1583 ing.client_id = client->id;
1584 memcpy(ing.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1585 memcpy(ing.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1586 ing.name_len = strlen(refname);
1588 ng_err = got_error_fmt(GOT_ERR_REF_BUSY, "%s", reason);
1589 ing.reason_len = strlen(ng_err->msg);
1591 len = sizeof(ing) + ing.name_len + ing.reason_len;
1592 wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_NG,
1593 PROC_GOTD, gotd.pid, len);
1594 if (wbuf == NULL)
1595 return got_error_from_errno("imsg_create REF_UPDATE_NG");
1597 if (imsg_add(wbuf, &ing, sizeof(ing)) == -1)
1598 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1599 if (imsg_add(wbuf, refname, ing.name_len) == -1)
1600 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1601 if (imsg_add(wbuf, ng_err->msg, ing.reason_len) == -1)
1602 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1604 wbuf->fd = -1;
1605 imsg_close(&client->iev.ibuf, wbuf);
1606 gotd_imsg_event_add(&client->iev);
1607 return NULL;
1610 static const struct got_error *
1611 install_pack(struct gotd_client *client, const char *repo_path,
1612 struct imsg *imsg)
1614 const struct got_error *err = NULL;
1615 struct gotd_imsg_packfile_install inst;
1616 char hex[SHA1_DIGEST_STRING_LENGTH];
1617 size_t datalen;
1618 char *packfile_path = NULL, *packidx_path = NULL;
1620 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1621 if (datalen != sizeof(inst))
1622 return got_error(GOT_ERR_PRIVSEP_LEN);
1623 memcpy(&inst, imsg->data, sizeof(inst));
1625 if (client->packfile_path == NULL)
1626 return got_error_msg(GOT_ERR_BAD_REQUEST,
1627 "client has no pack file");
1628 if (client->packidx_path == NULL)
1629 return got_error_msg(GOT_ERR_BAD_REQUEST,
1630 "client has no pack file index");
1632 if (got_sha1_digest_to_str(inst.pack_sha1, hex, sizeof(hex)) == NULL)
1633 return got_error_msg(GOT_ERR_NO_SPACE,
1634 "could not convert pack file SHA1 to hex");
1636 if (asprintf(&packfile_path, "/%s/%s/pack-%s.pack",
1637 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1638 err = got_error_from_errno("asprintf");
1639 goto done;
1642 if (asprintf(&packidx_path, "/%s/%s/pack-%s.idx",
1643 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1644 err = got_error_from_errno("asprintf");
1645 goto done;
1648 if (rename(client->packfile_path, packfile_path) == -1) {
1649 err = got_error_from_errno3("rename", client->packfile_path,
1650 packfile_path);
1651 goto done;
1654 free(client->packfile_path);
1655 client->packfile_path = NULL;
1657 if (rename(client->packidx_path, packidx_path) == -1) {
1658 err = got_error_from_errno3("rename", client->packidx_path,
1659 packidx_path);
1660 goto done;
1663 free(client->packidx_path);
1664 client->packidx_path = NULL;
1665 done:
1666 free(packfile_path);
1667 free(packidx_path);
1668 return err;
1671 static const struct got_error *
1672 begin_ref_updates(struct gotd_client *client, struct imsg *imsg)
1674 struct gotd_imsg_ref_updates_start istart;
1675 size_t datalen;
1677 if (client->nref_updates != -1)
1678 return got_error(GOT_ERR_PRIVSEP_MSG);
1680 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1681 if (datalen != sizeof(istart))
1682 return got_error(GOT_ERR_PRIVSEP_LEN);
1683 memcpy(&istart, imsg->data, sizeof(istart));
1685 if (istart.nref_updates <= 0)
1686 return got_error(GOT_ERR_PRIVSEP_MSG);
1688 client->nref_updates = istart.nref_updates;
1689 return NULL;
1692 static const struct got_error *
1693 update_ref(struct gotd_client *client, const char *repo_path,
1694 struct imsg *imsg)
1696 const struct got_error *err = NULL;
1697 struct got_repository *repo = NULL;
1698 struct got_reference *ref = NULL;
1699 struct gotd_imsg_ref_update iref;
1700 struct got_object_id old_id, new_id;
1701 struct got_object_id *id = NULL;
1702 struct got_object *obj = NULL;
1703 char *refname = NULL;
1704 size_t datalen;
1705 int locked = 0;
1707 log_debug("update-ref from uid %d", client->euid);
1709 if (client->nref_updates <= 0)
1710 return got_error(GOT_ERR_PRIVSEP_MSG);
1712 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1713 if (datalen < sizeof(iref))
1714 return got_error(GOT_ERR_PRIVSEP_LEN);
1715 memcpy(&iref, imsg->data, sizeof(iref));
1716 if (datalen != sizeof(iref) + iref.name_len)
1717 return got_error(GOT_ERR_PRIVSEP_LEN);
1718 refname = malloc(iref.name_len + 1);
1719 if (refname == NULL)
1720 return got_error_from_errno("malloc");
1721 memcpy(refname, imsg->data + sizeof(iref), iref.name_len);
1722 refname[iref.name_len] = '\0';
1724 log_debug("updating ref %s for uid %d", refname, client->euid);
1726 err = got_repo_open(&repo, repo_path, NULL, NULL);
1727 if (err)
1728 goto done;
1730 memcpy(old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
1731 memcpy(new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
1732 err = got_object_open(&obj, repo, &new_id);
1733 if (err)
1734 goto done;
1736 if (iref.ref_is_new) {
1737 err = got_ref_open(&ref, repo, refname, 0);
1738 if (err) {
1739 if (err->code != GOT_ERR_NOT_REF)
1740 goto done;
1741 err = got_ref_alloc(&ref, refname, &new_id);
1742 if (err)
1743 goto done;
1744 err = got_ref_write(ref, repo); /* will lock/unlock */
1745 if (err)
1746 goto done;
1747 } else {
1748 err = got_error_fmt(GOT_ERR_REF_BUSY,
1749 "%s has been created by someone else "
1750 "while transaction was in progress",
1751 got_ref_get_name(ref));
1752 goto done;
1754 } else {
1755 err = got_ref_open(&ref, repo, refname, 1 /* lock */);
1756 if (err)
1757 goto done;
1758 locked = 1;
1760 err = got_ref_resolve(&id, repo, ref);
1761 if (err)
1762 goto done;
1764 if (got_object_id_cmp(id, &old_id) != 0) {
1765 err = got_error_fmt(GOT_ERR_REF_BUSY,
1766 "%s has been modified by someone else "
1767 "while transaction was in progress",
1768 got_ref_get_name(ref));
1769 goto done;
1772 err = got_ref_change_ref(ref, &new_id);
1773 if (err)
1774 goto done;
1776 err = got_ref_write(ref, repo);
1777 if (err)
1778 goto done;
1780 free(id);
1781 id = NULL;
1783 done:
1784 if (err) {
1785 if (err->code == GOT_ERR_LOCKFILE_TIMEOUT) {
1786 err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
1787 "could not acquire exclusive file lock for %s",
1788 refname);
1790 send_ref_update_ng(client, &iref, refname, err->msg);
1791 } else
1792 send_ref_update_ok(client, &iref, refname);
1794 if (client->nref_updates > 0) {
1795 client->nref_updates--;
1796 if (client->nref_updates == 0)
1797 send_refs_updated(client);
1800 if (locked) {
1801 const struct got_error *unlock_err;
1802 unlock_err = got_ref_unlock(ref);
1803 if (unlock_err && err == NULL)
1804 err = unlock_err;
1806 if (ref)
1807 got_ref_close(ref);
1808 if (obj)
1809 got_object_close(obj);
1810 if (repo)
1811 got_repo_close(repo);
1812 free(refname);
1813 free(id);
1814 return err;
1817 static void
1818 gotd_dispatch(int fd, short event, void *arg)
1820 struct gotd_imsgev *iev = arg;
1821 struct imsgbuf *ibuf = &iev->ibuf;
1822 struct gotd_child_proc *proc = NULL;
1823 ssize_t n;
1824 int shut = 0;
1825 struct imsg imsg;
1827 if (event & EV_READ) {
1828 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1829 fatal("imsg_read error");
1830 if (n == 0) {
1831 /* Connection closed. */
1832 shut = 1;
1833 goto done;
1837 if (event & EV_WRITE) {
1838 n = msgbuf_write(&ibuf->w);
1839 if (n == -1 && errno != EAGAIN)
1840 fatal("msgbuf_write");
1841 if (n == 0) {
1842 /* Connection closed. */
1843 shut = 1;
1844 goto done;
1848 proc = find_proc_by_fd(fd);
1849 if (proc == NULL)
1850 fatalx("cannot find child process for fd %d", fd);
1852 for (;;) {
1853 const struct got_error *err = NULL;
1854 struct gotd_client *client = NULL;
1855 uint32_t client_id = 0;
1856 int do_disconnect = 0;
1857 int do_ref_updates = 0, do_ref_update = 0;
1858 int do_packfile_install = 0;
1860 if ((n = imsg_get(ibuf, &imsg)) == -1)
1861 fatal("%s: imsg_get error", __func__);
1862 if (n == 0) /* No more messages. */
1863 break;
1865 switch (imsg.hdr.type) {
1866 case GOTD_IMSG_ERROR:
1867 do_disconnect = 1;
1868 err = gotd_imsg_recv_error(&client_id, &imsg);
1869 break;
1870 case GOTD_IMSG_CONNECT:
1871 err = recv_connect(&client_id, &imsg);
1872 break;
1873 case GOTD_IMSG_PACKFILE_DONE:
1874 do_disconnect = 1;
1875 err = recv_packfile_done(&client_id, &imsg);
1876 break;
1877 case GOTD_IMSG_PACKFILE_INSTALL:
1878 err = recv_packfile_install(&client_id, &imsg);
1879 if (err == NULL)
1880 do_packfile_install = 1;
1881 break;
1882 case GOTD_IMSG_REF_UPDATES_START:
1883 err = recv_ref_updates_start(&client_id, &imsg);
1884 if (err == NULL)
1885 do_ref_updates = 1;
1886 break;
1887 case GOTD_IMSG_REF_UPDATE:
1888 err = recv_ref_update(&client_id, &imsg);
1889 if (err == NULL)
1890 do_ref_update = 1;
1891 break;
1892 default:
1893 log_debug("unexpected imsg %d", imsg.hdr.type);
1894 break;
1897 client = find_client(client_id);
1898 if (client == NULL) {
1899 log_warnx("%s: client not found", __func__);
1900 imsg_free(&imsg);
1901 continue;
1904 if (!verify_imsg_src(client, proc, &imsg)) {
1905 log_debug("dropping imsg type %d from PID %d",
1906 imsg.hdr.type, proc->pid);
1907 imsg_free(&imsg);
1908 continue;
1910 if (err)
1911 log_warnx("uid %d: %s", client->euid, err->msg);
1913 if (do_disconnect) {
1914 if (err)
1915 disconnect_on_error(client, err);
1916 else
1917 disconnect(client);
1918 } else {
1919 if (do_packfile_install)
1920 err = install_pack(client, proc->repo_path,
1921 &imsg);
1922 else if (do_ref_updates)
1923 err = begin_ref_updates(client, &imsg);
1924 else if (do_ref_update)
1925 err = update_ref(client, proc->repo_path,
1926 &imsg);
1927 if (err)
1928 log_warnx("uid %d: %s", client->euid, err->msg);
1930 imsg_free(&imsg);
1932 done:
1933 if (!shut) {
1934 gotd_imsg_event_add(iev);
1935 } else {
1936 /* This pipe is dead. Remove its event handler */
1937 event_del(&iev->ev);
1938 event_loopexit(NULL);
1942 static pid_t
1943 start_child(enum gotd_procid proc_id, const char *repo_path,
1944 char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
1946 char *argv[11];
1947 int argc = 0;
1948 pid_t pid;
1950 switch (pid = fork()) {
1951 case -1:
1952 fatal("cannot fork");
1953 case 0:
1954 break;
1955 default:
1956 close(fd);
1957 return pid;
1960 if (fd != GOTD_FILENO_MSG_PIPE) {
1961 if (dup2(fd, GOTD_FILENO_MSG_PIPE) == -1)
1962 fatal("cannot setup imsg fd");
1963 } else if (fcntl(fd, F_SETFD, 0) == -1)
1964 fatal("cannot setup imsg fd");
1966 argv[argc++] = argv0;
1967 switch (proc_id) {
1968 case PROC_LISTEN:
1969 argv[argc++] = (char *)"-L";
1970 break;
1971 case PROC_REPO_READ:
1972 argv[argc++] = (char *)"-R";
1973 break;
1974 case PROC_REPO_WRITE:
1975 argv[argc++] = (char *)"-W";
1976 break;
1977 default:
1978 fatalx("invalid process id %d", proc_id);
1981 argv[argc++] = (char *)"-f";
1982 argv[argc++] = (char *)confpath;
1984 if (repo_path) {
1985 argv[argc++] = (char *)"-P";
1986 argv[argc++] = (char *)repo_path;
1989 if (!daemonize)
1990 argv[argc++] = (char *)"-d";
1991 if (verbosity > 0)
1992 argv[argc++] = (char *)"-v";
1993 if (verbosity > 1)
1994 argv[argc++] = (char *)"-v";
1995 argv[argc++] = NULL;
1997 execvp(argv0, argv);
1998 fatal("execvp");
2001 static void
2002 start_listener(char *argv0, const char *confpath, int daemonize, int verbosity)
2004 struct gotd_child_proc *proc = &gotd.procs[0];
2006 proc->type = PROC_LISTEN;
2008 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
2009 PF_UNSPEC, proc->pipe) == -1)
2010 fatal("socketpair");
2012 proc->pid = start_child(proc->type, NULL, argv0, confpath,
2013 proc->pipe[1], daemonize, verbosity);
2014 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
2015 proc->iev.handler = gotd_dispatch;
2016 proc->iev.events = EV_READ;
2017 proc->iev.handler_arg = NULL;
2020 static void
2021 start_repo_children(struct gotd *gotd, char *argv0, const char *confpath,
2022 int daemonize, int verbosity)
2024 struct gotd_repo *repo = NULL;
2025 struct gotd_child_proc *proc;
2026 int i;
2028 for (i = 1; i < gotd->nprocs; i++) {
2029 if (repo == NULL)
2030 repo = TAILQ_FIRST(&gotd->repos);
2031 proc = &gotd->procs[i];
2032 if (i - 1 < gotd->nrepos)
2033 proc->type = PROC_REPO_READ;
2034 else
2035 proc->type = PROC_REPO_WRITE;
2036 if (strlcpy(proc->repo_name, repo->name,
2037 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
2038 fatalx("repository name too long: %s", repo->name);
2039 log_debug("adding repository %s", repo->name);
2040 if (realpath(repo->path, proc->repo_path) == NULL)
2041 fatal("%s", repo->path);
2042 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
2043 PF_UNSPEC, proc->pipe) == -1)
2044 fatal("socketpair");
2045 proc->pid = start_child(proc->type, proc->repo_path, argv0,
2046 confpath, proc->pipe[1], daemonize, verbosity);
2047 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
2048 log_debug("proc %s %s is on fd %d",
2049 gotd_proc_names[proc->type], proc->repo_path,
2050 proc->pipe[0]);
2051 proc->iev.handler = gotd_dispatch;
2052 proc->iev.events = EV_READ;
2053 proc->iev.handler_arg = NULL;
2054 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
2055 gotd_dispatch, &proc->iev);
2057 repo = TAILQ_NEXT(repo, entry);
2061 static void
2062 apply_unveil_repo_readonly(const char *repo_path)
2064 if (unveil(repo_path, "r") == -1)
2065 fatal("unveil %s", repo_path);
2067 if (unveil(NULL, NULL) == -1)
2068 fatal("unveil");
2071 static void
2072 apply_unveil(void)
2074 struct gotd_repo *repo;
2076 TAILQ_FOREACH(repo, &gotd.repos, entry) {
2077 if (unveil(repo->path, "rwc") == -1)
2078 fatal("unveil %s", repo->path);
2081 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
2082 fatal("unveil %s", GOT_TMPDIR_STR);
2084 if (unveil(NULL, NULL) == -1)
2085 fatal("unveil");
2088 int
2089 main(int argc, char **argv)
2091 const struct got_error *error = NULL;
2092 int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
2093 const char *confpath = GOTD_CONF_PATH;
2094 char *argv0 = argv[0];
2095 char title[2048];
2096 gid_t groups[NGROUPS_MAX];
2097 int ngroups = NGROUPS_MAX;
2098 struct passwd *pw = NULL;
2099 struct group *gr = NULL;
2100 char *repo_path = NULL;
2101 enum gotd_procid proc_id = PROC_GOTD;
2102 struct event evsigint, evsigterm, evsighup, evsigusr1;
2103 int *pack_fds = NULL, *temp_fds = NULL;
2105 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
2107 while ((ch = getopt(argc, argv, "df:LnP:RvW")) != -1) {
2108 switch (ch) {
2109 case 'd':
2110 daemonize = 0;
2111 break;
2112 case 'f':
2113 confpath = optarg;
2114 break;
2115 case 'L':
2116 proc_id = PROC_LISTEN;
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, groups, &ngroups) == -1)
2171 log_warnx("group membership list truncated");
2173 gr = match_group(groups, ngroups, gotd.unix_group_name);
2174 if (gr == NULL) {
2175 fatalx("cannot start %s: the user running %s "
2176 "must be a secondary member of group %s",
2177 getprogname(), getprogname(), gotd.unix_group_name);
2179 if (gr->gr_gid == pw->pw_gid) {
2180 fatalx("cannot start %s: the user running %s "
2181 "must be a secondary member of group %s, but "
2182 "%s is the user's primary group",
2183 getprogname(), getprogname(), gotd.unix_group_name,
2184 gotd.unix_group_name);
2187 if (proc_id == PROC_LISTEN &&
2188 !got_path_is_absolute(gotd.unix_socket_path))
2189 fatalx("bad unix socket path \"%s\": must be an absolute path",
2190 gotd.unix_socket_path);
2192 if (noaction)
2193 return 0;
2195 if (proc_id == PROC_GOTD) {
2196 gotd.pid = getpid();
2197 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2199 * Start a listener and repository readers/writers.
2200 * XXX For now, use one reader and one writer per repository.
2201 * This should be changed to N readers + M writers.
2203 gotd.nprocs = 1 + gotd.nrepos * 2;
2204 gotd.procs = calloc(gotd.nprocs, sizeof(*gotd.procs));
2205 if (gotd.procs == NULL)
2206 fatal("calloc");
2207 start_listener(argv0, confpath, daemonize, verbosity);
2208 start_repo_children(&gotd, argv0, confpath, daemonize,
2209 verbosity);
2210 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
2211 if (daemonize && daemon(1, 0) == -1)
2212 fatal("daemon");
2213 } else if (proc_id == PROC_LISTEN) {
2214 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2215 if (verbosity) {
2216 log_info("socket: %s", gotd.unix_socket_path);
2217 log_info("user: %s", pw->pw_name);
2218 log_info("secondary group: %s", gr->gr_name);
2221 fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
2222 gr->gr_gid);
2223 if (fd == -1) {
2224 fatal("cannot listen on unix socket %s",
2225 gotd.unix_socket_path);
2227 if (daemonize && daemon(0, 0) == -1)
2228 fatal("daemon");
2229 } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE) {
2230 error = got_repo_pack_fds_open(&pack_fds);
2231 if (error != NULL)
2232 fatalx("cannot open pack tempfiles: %s", error->msg);
2233 error = got_repo_temp_fds_open(&temp_fds);
2234 if (error != NULL)
2235 fatalx("cannot open pack tempfiles: %s", error->msg);
2236 if (repo_path == NULL)
2237 fatalx("repository path not specified");
2238 snprintf(title, sizeof(title), "%s %s",
2239 gotd_proc_names[proc_id], repo_path);
2240 if (daemonize && daemon(0, 0) == -1)
2241 fatal("daemon");
2242 } else
2243 fatal("invalid process id %d", proc_id);
2245 setproctitle("%s", title);
2246 log_procinit(title);
2248 /* Drop root privileges. */
2249 if (setgid(pw->pw_gid) == -1)
2250 fatal("setgid %d failed", pw->pw_gid);
2251 if (setuid(pw->pw_uid) == -1)
2252 fatal("setuid %d failed", pw->pw_uid);
2254 event_init();
2256 switch (proc_id) {
2257 case PROC_GOTD:
2258 #ifndef PROFILE
2259 if (pledge("stdio rpath wpath cpath proc getpw sendfd recvfd "
2260 "fattr flock unix unveil", NULL) == -1)
2261 err(1, "pledge");
2262 #endif
2263 break;
2264 case PROC_LISTEN:
2265 #ifndef PROFILE
2266 if (pledge("stdio sendfd unix", NULL) == -1)
2267 err(1, "pledge");
2268 #endif
2269 listen_main(title, fd);
2270 /* NOTREACHED */
2271 break;
2272 case PROC_REPO_READ:
2273 #ifndef PROFILE
2274 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2275 err(1, "pledge");
2276 #endif
2277 apply_unveil_repo_readonly(repo_path);
2278 repo_read_main(title, repo_path, pack_fds, temp_fds);
2279 /* NOTREACHED */
2280 exit(0);
2281 case PROC_REPO_WRITE:
2282 #ifndef PROFILE
2283 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2284 err(1, "pledge");
2285 #endif
2286 apply_unveil_repo_readonly(repo_path);
2287 repo_write_main(title, repo_path, pack_fds, temp_fds);
2288 /* NOTREACHED */
2289 exit(0);
2290 default:
2291 fatal("invalid process id %d", proc_id);
2294 if (proc_id != PROC_GOTD)
2295 fatal("invalid process id %d", proc_id);
2297 apply_unveil();
2299 signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
2300 signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
2301 signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
2302 signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
2303 signal(SIGPIPE, SIG_IGN);
2305 signal_add(&evsigint, NULL);
2306 signal_add(&evsigterm, NULL);
2307 signal_add(&evsighup, NULL);
2308 signal_add(&evsigusr1, NULL);
2310 gotd_imsg_event_add(&gotd.procs[0].iev);
2312 event_dispatch();
2314 if (pack_fds)
2315 got_repo_pack_fds_close(pack_fds);
2316 free(repo_path);
2317 return 0;