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 /* Send pack pipe end 0 to repo_write. */
891 if (gotd_imsg_compose_event(&client->repo_write->iev,
892 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[0],
893 &ipipe, sizeof(ipipe)) == -1) {
894 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
895 pipe[0] = -1;
896 goto done;
898 pipe[0] = -1;
900 /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
901 if (gotd_imsg_compose_event(&client->iev,
902 GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1], NULL, 0) == -1)
903 err = got_error_from_errno("imsg compose PACKFILE_PIPE");
904 pipe[1] = -1;
906 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.pack",
907 client->repo_write->repo_path, GOT_OBJECTS_PACK_DIR,
908 client->euid) == -1) {
909 err = got_error_from_errno("asprintf");
910 goto done;
913 err = got_opentemp_named_fd(&pack_path, &packfd, basepath, "");
914 if (err)
915 goto done;
917 free(basepath);
918 if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.idx",
919 client->repo_write->repo_path, GOT_OBJECTS_PACK_DIR,
920 client->euid) == -1) {
921 err = got_error_from_errno("asprintf");
922 basepath = NULL;
923 goto done;
925 err = got_opentemp_named_fd(&idx_path, &idxfd, basepath, "");
926 if (err)
927 goto done;
929 memset(&ifile, 0, sizeof(ifile));
930 ifile.client_id = client->id;
931 if (gotd_imsg_compose_event(&client->repo_write->iev,
932 GOTD_IMSG_PACKIDX_FILE, PROC_GOTD, idxfd,
933 &ifile, sizeof(ifile)) == -1) {
934 err = got_error_from_errno("imsg compose PACKIDX_FILE");
935 idxfd = -1;
936 goto done;
938 idxfd = -1;
940 memset(&ipack, 0, sizeof(ipack));
941 ipack.client_id = client->id;
942 if (client_has_capability(client, GOT_CAPA_REPORT_STATUS))
943 ipack.report_status = 1;
945 if (gotd_imsg_compose_event(&client->repo_write->iev,
946 GOTD_IMSG_RECV_PACKFILE, PROC_GOTD, packfd,
947 &ipack, sizeof(ipack)) == -1) {
948 err = got_error_from_errno("imsg compose RECV_PACKFILE");
949 packfd = -1;
950 goto done;
952 packfd = -1;
954 done:
955 free(basepath);
956 if (pipe[0] != -1 && close(pipe[0]) == -1 && err == NULL)
957 err = got_error_from_errno("close");
958 if (pipe[1] != -1 && close(pipe[1]) == -1 && err == NULL)
959 err = got_error_from_errno("close");
960 if (packfd != -1 && close(packfd) == -1 && err == NULL)
961 err = got_error_from_errno("close");
962 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
963 err = got_error_from_errno("close");
964 if (err) {
965 free(pack_path);
966 free(idx_path);
967 } else {
968 client->packfile_path = pack_path;
969 client->packidx_path = idx_path;
971 return err;
974 static void
975 gotd_request(int fd, short events, void *arg)
977 struct gotd_imsgev *iev = arg;
978 struct imsgbuf *ibuf = &iev->ibuf;
979 struct gotd_client *client = iev->handler_arg;
980 const struct got_error *err = NULL;
981 struct imsg imsg;
982 ssize_t n;
984 if (events & EV_WRITE) {
985 while (ibuf->w.queued) {
986 n = msgbuf_write(&ibuf->w);
987 if (n == -1 && errno == EPIPE) {
988 /*
989 * The client has closed its socket.
990 * This can happen when Git clients are
991 * done sending pack file data.
992 */
993 msgbuf_clear(&ibuf->w);
994 continue;
995 } else if (n == -1 && errno != EAGAIN) {
996 err = got_error_from_errno("imsg_flush");
997 disconnect_on_error(client, err);
998 return;
1000 if (n == 0) {
1001 /* Connection closed. */
1002 err = got_error(GOT_ERR_EOF);
1003 disconnect_on_error(client, err);
1004 return;
1008 /* Disconnect gotctl(8) now that messages have been sent. */
1009 if (!client_is_reading(client) && !client_is_writing(client)) {
1010 disconnect(client);
1011 return;
1015 if ((events & EV_READ) == 0)
1016 return;
1018 memset(&imsg, 0, sizeof(imsg));
1020 while (err == NULL) {
1021 err = gotd_imsg_recv(&imsg, ibuf, 0);
1022 if (err) {
1023 if (err->code == GOT_ERR_PRIVSEP_READ)
1024 err = NULL;
1025 break;
1028 evtimer_del(&client->tmo);
1030 switch (imsg.hdr.type) {
1031 case GOTD_IMSG_INFO:
1032 err = send_info(client);
1033 break;
1034 case GOTD_IMSG_STOP:
1035 err = stop_gotd(client);
1036 break;
1037 case GOTD_IMSG_LIST_REFS:
1038 if (client->state != GOTD_STATE_EXPECT_LIST_REFS) {
1039 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1040 "unexpected list-refs request received");
1041 break;
1043 err = forward_list_refs_request(client, &imsg);
1044 if (err)
1045 break;
1046 client->state = GOTD_STATE_EXPECT_CAPABILITIES;
1047 log_debug("uid %d: expecting capabilities",
1048 client->euid);
1049 break;
1050 case GOTD_IMSG_CAPABILITIES:
1051 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1052 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1053 "unexpected capabilities received");
1054 break;
1056 log_debug("receiving capabilities from uid %d",
1057 client->euid);
1058 err = recv_capabilities(client, &imsg);
1059 break;
1060 case GOTD_IMSG_CAPABILITY:
1061 if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1062 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1063 "unexpected capability received");
1064 break;
1066 err = recv_capability(client, &imsg);
1067 if (err || client->ncapabilities < client->ncapa_alloc)
1068 break;
1069 if (client_is_reading(client)) {
1070 client->state = GOTD_STATE_EXPECT_WANT;
1071 log_debug("uid %d: expecting want-lines",
1072 client->euid);
1073 } else if (client_is_writing(client)) {
1074 client->state = GOTD_STATE_EXPECT_REF_UPDATE;
1075 log_debug("uid %d: expecting ref-update-lines",
1076 client->euid);
1077 } else
1078 fatalx("client %d is both reading and writing",
1079 client->euid);
1080 break;
1081 case GOTD_IMSG_WANT:
1082 if (client->state != GOTD_STATE_EXPECT_WANT) {
1083 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1084 "unexpected want-line received");
1085 break;
1087 log_debug("received want-line from uid %d",
1088 client->euid);
1089 err = ensure_client_is_reading(client);
1090 if (err)
1091 break;
1092 err = forward_want(client, &imsg);
1093 break;
1094 case GOTD_IMSG_REF_UPDATE:
1095 if (client->state != GOTD_STATE_EXPECT_REF_UPDATE) {
1096 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1097 "unexpected ref-update-line received");
1098 break;
1100 log_debug("received ref-update-line from uid %d",
1101 client->euid);
1102 err = ensure_client_is_writing(client);
1103 if (err)
1104 break;
1105 err = forward_ref_update(client, &imsg);
1106 if (err)
1107 break;
1108 client->state = GOTD_STATE_EXPECT_MORE_REF_UPDATES;
1109 break;
1110 case GOTD_IMSG_HAVE:
1111 if (client->state != GOTD_STATE_EXPECT_HAVE) {
1112 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1113 "unexpected have-line received");
1114 break;
1116 log_debug("received have-line from uid %d",
1117 client->euid);
1118 err = ensure_client_is_reading(client);
1119 if (err)
1120 break;
1121 err = forward_have(client, &imsg);
1122 if (err)
1123 break;
1124 break;
1125 case GOTD_IMSG_FLUSH:
1126 if (client->state == GOTD_STATE_EXPECT_WANT ||
1127 client->state == GOTD_STATE_EXPECT_HAVE) {
1128 err = ensure_client_is_reading(client);
1129 if (err)
1130 break;
1131 } else if (client->state ==
1132 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1133 err = ensure_client_is_writing(client);
1134 if (err)
1135 break;
1136 } else {
1137 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1138 "unexpected flush-pkt received");
1139 break;
1141 log_debug("received flush-pkt from uid %d",
1142 client->euid);
1143 if (client->state == GOTD_STATE_EXPECT_WANT) {
1144 client->state = GOTD_STATE_EXPECT_HAVE;
1145 log_debug("uid %d: expecting have-lines",
1146 client->euid);
1147 } else if (client->state == GOTD_STATE_EXPECT_HAVE) {
1148 client->state = GOTD_STATE_EXPECT_DONE;
1149 log_debug("uid %d: expecting 'done'",
1150 client->euid);
1151 } else if (client->state ==
1152 GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1153 client->state = GOTD_STATE_EXPECT_PACKFILE;
1154 log_debug("uid %d: expecting packfile",
1155 client->euid);
1156 err = recv_packfile(client);
1157 } else {
1158 /* should not happen, see above */
1159 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1160 "unexpected client state");
1161 break;
1163 break;
1164 case GOTD_IMSG_DONE:
1165 if (client->state != GOTD_STATE_EXPECT_HAVE &&
1166 client->state != GOTD_STATE_EXPECT_DONE) {
1167 err = got_error_msg(GOT_ERR_BAD_REQUEST,
1168 "unexpected flush-pkt received");
1169 break;
1171 log_debug("received 'done' from uid %d", client->euid);
1172 err = ensure_client_is_reading(client);
1173 if (err)
1174 break;
1175 client->state = GOTD_STATE_DONE;
1176 err = send_packfile(client);
1177 break;
1178 default:
1179 err = got_error(GOT_ERR_PRIVSEP_MSG);
1180 break;
1183 imsg_free(&imsg);
1186 if (err) {
1187 if (err->code != GOT_ERR_EOF ||
1188 client->state != GOTD_STATE_EXPECT_PACKFILE)
1189 disconnect_on_error(client, err);
1190 } else {
1191 gotd_imsg_event_add(&client->iev);
1192 evtimer_add(&client->tmo, &timeout);
1196 static void
1197 gotd_request_timeout(int fd, short events, void *arg)
1199 struct gotd_client *client = arg;
1201 log_debug("disconnecting uid %d due to timeout", client->euid);
1202 disconnect(client);
1205 static const struct got_error *
1206 recv_connect(uint32_t *client_id, struct imsg *imsg)
1208 const struct got_error *err = NULL;
1209 struct gotd_imsg_connect iconnect;
1210 size_t datalen;
1211 int s = -1;
1212 struct gotd_client *client = NULL;
1213 uid_t euid;
1214 gid_t egid;
1216 *client_id = 0;
1218 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1219 if (datalen != sizeof(iconnect))
1220 return got_error(GOT_ERR_PRIVSEP_LEN);
1221 memcpy(&iconnect, imsg->data, sizeof(iconnect));
1223 s = imsg->fd;
1224 if (s == -1) {
1225 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1226 goto done;
1229 if (find_client(iconnect.client_id)) {
1230 err = got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
1231 goto done;
1234 if (getpeereid(s, &euid, &egid) == -1) {
1235 err = got_error_from_errno("getpeerid");
1236 goto done;
1239 client = calloc(1, sizeof(*client));
1240 if (client == NULL) {
1241 err = got_error_from_errno("calloc");
1242 goto done;
1245 *client_id = iconnect.client_id;
1247 client->state = GOTD_STATE_EXPECT_LIST_REFS;
1248 client->id = iconnect.client_id;
1249 client->fd = s;
1250 s = -1;
1251 client->delta_cache_fd = -1;
1252 client->euid = euid;
1253 client->egid = egid;
1254 client->nref_updates = -1;
1256 imsg_init(&client->iev.ibuf, client->fd);
1257 client->iev.handler = gotd_request;
1258 client->iev.events = EV_READ;
1259 client->iev.handler_arg = client;
1261 event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
1262 &client->iev);
1263 gotd_imsg_event_add(&client->iev);
1265 evtimer_set(&client->tmo, gotd_request_timeout, client);
1267 add_client(client);
1268 log_debug("%s: new client uid %d connected on fd %d", __func__,
1269 client->euid, client->fd);
1270 done:
1271 if (err) {
1272 struct gotd_child_proc *listen_proc = &gotd.procs[0];
1273 struct gotd_imsg_disconnect idisconnect;
1275 idisconnect.client_id = client->id;
1276 if (gotd_imsg_compose_event(&listen_proc->iev,
1277 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
1278 &idisconnect, sizeof(idisconnect)) == -1)
1279 log_warn("imsg compose DISCONNECT");
1281 if (s != -1)
1282 close(s);
1285 return err;
1288 static const char *gotd_proc_names[PROC_MAX] = {
1289 "parent",
1290 "listen",
1291 "repo_read",
1292 "repo_write"
1295 static struct gotd_child_proc *
1296 get_proc_for_pid(pid_t pid)
1298 struct gotd_child_proc *proc;
1299 int i;
1301 for (i = 0; i < gotd.nprocs; i++) {
1302 proc = &gotd.procs[i];
1303 if (proc->pid == pid)
1304 return proc;
1307 return NULL;
1310 static void
1311 kill_proc(struct gotd_child_proc *proc, int fatal)
1313 if (fatal) {
1314 log_warnx("sending SIGKILL to PID %d", proc->pid);
1315 kill(proc->pid, SIGKILL);
1316 } else
1317 kill(proc->pid, SIGTERM);
1320 static void
1321 gotd_shutdown(void)
1323 pid_t pid;
1324 int status, i;
1325 struct gotd_child_proc *proc;
1327 for (i = 0; i < gotd.nprocs; i++) {
1328 proc = &gotd.procs[i];
1329 msgbuf_clear(&proc->iev.ibuf.w);
1330 close(proc->iev.ibuf.fd);
1331 kill_proc(proc, 0);
1334 log_debug("waiting for children to terminate");
1335 do {
1336 pid = wait(&status);
1337 if (pid == -1) {
1338 if (errno != EINTR && errno != ECHILD)
1339 fatal("wait");
1340 } else if (WIFSIGNALED(status)) {
1341 proc = get_proc_for_pid(pid);
1342 log_warnx("%s %s child process terminated; signal %d",
1343 proc ? gotd_proc_names[proc->type] : "",
1344 proc ? proc->repo_path : "", WTERMSIG(status));
1346 } while (pid != -1 || (pid == -1 && errno == EINTR));
1348 log_info("terminating");
1349 exit(0);
1352 void
1353 gotd_sighdlr(int sig, short event, void *arg)
1356 * Normal signal handler rules don't apply because libevent
1357 * decouples for us.
1360 switch (sig) {
1361 case SIGHUP:
1362 log_info("%s: ignoring SIGHUP", __func__);
1363 break;
1364 case SIGUSR1:
1365 log_info("%s: ignoring SIGUSR1", __func__);
1366 break;
1367 case SIGTERM:
1368 case SIGINT:
1369 gotd_shutdown();
1370 log_warnx("gotd terminating");
1371 exit(0);
1372 break;
1373 default:
1374 fatalx("unexpected signal");
1378 static const struct got_error *
1379 ensure_proc_is_reading(struct gotd_client *client,
1380 struct gotd_child_proc *proc)
1382 if (!client_is_reading(client)) {
1383 kill_proc(proc, 1);
1384 return got_error_fmt(GOT_ERR_BAD_PACKET,
1385 "PID %d handled a read-request for uid %d but this "
1386 "user is not reading from a repository", proc->pid,
1387 client->euid);
1390 return NULL;
1393 static const struct got_error *
1394 ensure_proc_is_writing(struct gotd_client *client,
1395 struct gotd_child_proc *proc)
1397 if (!client_is_writing(client)) {
1398 kill_proc(proc, 1);
1399 return got_error_fmt(GOT_ERR_BAD_PACKET,
1400 "PID %d handled a write-request for uid %d but this "
1401 "user is not writing to a repository", proc->pid,
1402 client->euid);
1405 return NULL;
1408 static int
1409 verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
1410 struct imsg *imsg)
1412 const struct got_error *err;
1413 struct gotd_child_proc *client_proc;
1414 int ret = 0;
1416 if (proc->type == PROC_REPO_READ || proc->type == PROC_REPO_WRITE) {
1417 client_proc = get_client_proc(client);
1418 if (client_proc == NULL)
1419 fatalx("no process found for uid %d", client->euid);
1420 if (proc->pid != client_proc->pid) {
1421 kill_proc(proc, 1);
1422 log_warnx("received message from PID %d for uid %d, "
1423 "while PID %d is the process serving this user",
1424 proc->pid, client->euid, client_proc->pid);
1425 return 0;
1429 switch (imsg->hdr.type) {
1430 case GOTD_IMSG_ERROR:
1431 ret = 1;
1432 break;
1433 case GOTD_IMSG_CONNECT:
1434 if (proc->type != PROC_LISTEN) {
1435 err = got_error_fmt(GOT_ERR_BAD_PACKET,
1436 "new connection for uid %d from PID %d "
1437 "which is not the listen process",
1438 proc->pid, client->euid);
1439 } else
1440 ret = 1;
1441 break;
1442 case GOTD_IMSG_PACKFILE_DONE:
1443 err = ensure_proc_is_reading(client, proc);
1444 if (err)
1445 log_warnx("uid %d: %s", client->euid, err->msg);
1446 else
1447 ret = 1;
1448 break;
1449 case GOTD_IMSG_PACKFILE_INSTALL:
1450 case GOTD_IMSG_REF_UPDATES_START:
1451 case GOTD_IMSG_REF_UPDATE:
1452 err = ensure_proc_is_writing(client, proc);
1453 if (err)
1454 log_warnx("uid %d: %s", client->euid, err->msg);
1455 else
1456 ret = 1;
1457 break;
1458 default:
1459 log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
1460 break;
1463 return ret;
1466 static const struct got_error *
1467 recv_packfile_done(uint32_t *client_id, struct imsg *imsg)
1469 struct gotd_imsg_packfile_done idone;
1470 size_t datalen;
1472 log_debug("packfile-done received");
1474 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1475 if (datalen != sizeof(idone))
1476 return got_error(GOT_ERR_PRIVSEP_LEN);
1477 memcpy(&idone, imsg->data, sizeof(idone));
1479 *client_id = idone.client_id;
1480 return NULL;
1483 static const struct got_error *
1484 recv_packfile_install(uint32_t *client_id, struct imsg *imsg)
1486 struct gotd_imsg_packfile_install inst;
1487 size_t datalen;
1489 log_debug("packfile-install received");
1491 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1492 if (datalen != sizeof(inst))
1493 return got_error(GOT_ERR_PRIVSEP_LEN);
1494 memcpy(&inst, imsg->data, sizeof(inst));
1496 *client_id = inst.client_id;
1497 return NULL;
1500 static const struct got_error *
1501 recv_ref_updates_start(uint32_t *client_id, struct imsg *imsg)
1503 struct gotd_imsg_ref_updates_start istart;
1504 size_t datalen;
1506 log_debug("ref-updates-start received");
1508 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1509 if (datalen != sizeof(istart))
1510 return got_error(GOT_ERR_PRIVSEP_LEN);
1511 memcpy(&istart, imsg->data, sizeof(istart));
1513 *client_id = istart.client_id;
1514 return NULL;
1517 static const struct got_error *
1518 recv_ref_update(uint32_t *client_id, struct imsg *imsg)
1520 struct gotd_imsg_ref_update iref;
1521 size_t datalen;
1523 log_debug("ref-update received");
1525 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1526 if (datalen < sizeof(iref))
1527 return got_error(GOT_ERR_PRIVSEP_LEN);
1528 memcpy(&iref, imsg->data, sizeof(iref));
1530 *client_id = iref.client_id;
1531 return NULL;
1534 static const struct got_error *
1535 send_ref_update_ok(struct gotd_client *client,
1536 struct gotd_imsg_ref_update *iref, const char *refname)
1538 struct gotd_imsg_ref_update_ok iok;
1539 struct ibuf *wbuf;
1540 size_t len;
1542 memset(&iok, 0, sizeof(iok));
1543 iok.client_id = client->id;
1544 memcpy(iok.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1545 memcpy(iok.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1546 iok.name_len = strlen(refname);
1548 len = sizeof(iok) + iok.name_len;
1549 wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_OK,
1550 PROC_GOTD, gotd.pid, len);
1551 if (wbuf == NULL)
1552 return got_error_from_errno("imsg_create REF_UPDATE_OK");
1554 if (imsg_add(wbuf, &iok, sizeof(iok)) == -1)
1555 return got_error_from_errno("imsg_add REF_UPDATE_OK");
1556 if (imsg_add(wbuf, refname, iok.name_len) == -1)
1557 return got_error_from_errno("imsg_add REF_UPDATE_OK");
1559 wbuf->fd = -1;
1560 imsg_close(&client->iev.ibuf, wbuf);
1561 gotd_imsg_event_add(&client->iev);
1562 return NULL;
1565 static void
1566 send_refs_updated(struct gotd_client *client)
1568 if (gotd_imsg_compose_event(&client->iev,
1569 GOTD_IMSG_REFS_UPDATED, PROC_GOTD, -1, NULL, 0) == -1)
1570 log_warn("imsg compose REFS_UPDATED");
1573 static const struct got_error *
1574 send_ref_update_ng(struct gotd_client *client,
1575 struct gotd_imsg_ref_update *iref, const char *refname,
1576 const char *reason)
1578 const struct got_error *ng_err;
1579 struct gotd_imsg_ref_update_ng ing;
1580 struct ibuf *wbuf;
1581 size_t len;
1583 memset(&ing, 0, sizeof(ing));
1584 ing.client_id = client->id;
1585 memcpy(ing.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
1586 memcpy(ing.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
1587 ing.name_len = strlen(refname);
1589 ng_err = got_error_fmt(GOT_ERR_REF_BUSY, "%s", reason);
1590 ing.reason_len = strlen(ng_err->msg);
1592 len = sizeof(ing) + ing.name_len + ing.reason_len;
1593 wbuf = imsg_create(&client->iev.ibuf, GOTD_IMSG_REF_UPDATE_NG,
1594 PROC_GOTD, gotd.pid, len);
1595 if (wbuf == NULL)
1596 return got_error_from_errno("imsg_create REF_UPDATE_NG");
1598 if (imsg_add(wbuf, &ing, sizeof(ing)) == -1)
1599 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1600 if (imsg_add(wbuf, refname, ing.name_len) == -1)
1601 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1602 if (imsg_add(wbuf, ng_err->msg, ing.reason_len) == -1)
1603 return got_error_from_errno("imsg_add REF_UPDATE_NG");
1605 wbuf->fd = -1;
1606 imsg_close(&client->iev.ibuf, wbuf);
1607 gotd_imsg_event_add(&client->iev);
1608 return NULL;
1611 static const struct got_error *
1612 install_pack(struct gotd_client *client, const char *repo_path,
1613 struct imsg *imsg)
1615 const struct got_error *err = NULL;
1616 struct gotd_imsg_packfile_install inst;
1617 char hex[SHA1_DIGEST_STRING_LENGTH];
1618 size_t datalen;
1619 char *packfile_path = NULL, *packidx_path = NULL;
1621 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1622 if (datalen != sizeof(inst))
1623 return got_error(GOT_ERR_PRIVSEP_LEN);
1624 memcpy(&inst, imsg->data, sizeof(inst));
1626 if (client->packfile_path == NULL)
1627 return got_error_msg(GOT_ERR_BAD_REQUEST,
1628 "client has no pack file");
1629 if (client->packidx_path == NULL)
1630 return got_error_msg(GOT_ERR_BAD_REQUEST,
1631 "client has no pack file index");
1633 if (got_sha1_digest_to_str(inst.pack_sha1, hex, sizeof(hex)) == NULL)
1634 return got_error_msg(GOT_ERR_NO_SPACE,
1635 "could not convert pack file SHA1 to hex");
1637 if (asprintf(&packfile_path, "/%s/%s/pack-%s.pack",
1638 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1639 err = got_error_from_errno("asprintf");
1640 goto done;
1643 if (asprintf(&packidx_path, "/%s/%s/pack-%s.idx",
1644 repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
1645 err = got_error_from_errno("asprintf");
1646 goto done;
1649 if (rename(client->packfile_path, packfile_path) == -1) {
1650 err = got_error_from_errno3("rename", client->packfile_path,
1651 packfile_path);
1652 goto done;
1655 free(client->packfile_path);
1656 client->packfile_path = NULL;
1658 if (rename(client->packidx_path, packidx_path) == -1) {
1659 err = got_error_from_errno3("rename", client->packidx_path,
1660 packidx_path);
1661 goto done;
1664 free(client->packidx_path);
1665 client->packidx_path = NULL;
1666 done:
1667 free(packfile_path);
1668 free(packidx_path);
1669 return err;
1672 static const struct got_error *
1673 begin_ref_updates(struct gotd_client *client, struct imsg *imsg)
1675 struct gotd_imsg_ref_updates_start istart;
1676 size_t datalen;
1678 if (client->nref_updates != -1)
1679 return got_error(GOT_ERR_PRIVSEP_MSG);
1681 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1682 if (datalen != sizeof(istart))
1683 return got_error(GOT_ERR_PRIVSEP_LEN);
1684 memcpy(&istart, imsg->data, sizeof(istart));
1686 if (istart.nref_updates <= 0)
1687 return got_error(GOT_ERR_PRIVSEP_MSG);
1689 client->nref_updates = istart.nref_updates;
1690 return NULL;
1693 static const struct got_error *
1694 update_ref(struct gotd_client *client, const char *repo_path,
1695 struct imsg *imsg)
1697 const struct got_error *err = NULL;
1698 struct got_repository *repo = NULL;
1699 struct got_reference *ref = NULL;
1700 struct gotd_imsg_ref_update iref;
1701 struct got_object_id old_id, new_id;
1702 struct got_object_id *id = NULL;
1703 struct got_object *obj = NULL;
1704 char *refname = NULL;
1705 size_t datalen;
1706 int locked = 0;
1708 log_debug("update-ref from uid %d", client->euid);
1710 if (client->nref_updates <= 0)
1711 return got_error(GOT_ERR_PRIVSEP_MSG);
1713 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1714 if (datalen < sizeof(iref))
1715 return got_error(GOT_ERR_PRIVSEP_LEN);
1716 memcpy(&iref, imsg->data, sizeof(iref));
1717 if (datalen != sizeof(iref) + iref.name_len)
1718 return got_error(GOT_ERR_PRIVSEP_LEN);
1719 refname = malloc(iref.name_len + 1);
1720 if (refname == NULL)
1721 return got_error_from_errno("malloc");
1722 memcpy(refname, imsg->data + sizeof(iref), iref.name_len);
1723 refname[iref.name_len] = '\0';
1725 log_debug("updating ref %s for uid %d", refname, client->euid);
1727 err = got_repo_open(&repo, repo_path, NULL, NULL);
1728 if (err)
1729 goto done;
1731 memcpy(old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
1732 memcpy(new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
1733 err = got_object_open(&obj, repo, &new_id);
1734 if (err)
1735 goto done;
1737 if (iref.ref_is_new) {
1738 err = got_ref_open(&ref, repo, refname, 0);
1739 if (err) {
1740 if (err->code != GOT_ERR_NOT_REF)
1741 goto done;
1742 err = got_ref_alloc(&ref, refname, &new_id);
1743 if (err)
1744 goto done;
1745 err = got_ref_write(ref, repo); /* will lock/unlock */
1746 if (err)
1747 goto done;
1748 } else {
1749 err = got_error_fmt(GOT_ERR_REF_BUSY,
1750 "%s has been created by someone else "
1751 "while transaction was in progress",
1752 got_ref_get_name(ref));
1753 goto done;
1755 } else {
1756 err = got_ref_open(&ref, repo, refname, 1 /* lock */);
1757 if (err)
1758 goto done;
1759 locked = 1;
1761 err = got_ref_resolve(&id, repo, ref);
1762 if (err)
1763 goto done;
1765 if (got_object_id_cmp(id, &old_id) != 0) {
1766 err = got_error_fmt(GOT_ERR_REF_BUSY,
1767 "%s has been modified by someone else "
1768 "while transaction was in progress",
1769 got_ref_get_name(ref));
1770 goto done;
1773 err = got_ref_change_ref(ref, &new_id);
1774 if (err)
1775 goto done;
1777 err = got_ref_write(ref, repo);
1778 if (err)
1779 goto done;
1781 free(id);
1782 id = NULL;
1784 done:
1785 if (err) {
1786 if (err->code == GOT_ERR_LOCKFILE_TIMEOUT) {
1787 err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
1788 "could not acquire exclusive file lock for %s",
1789 refname);
1791 send_ref_update_ng(client, &iref, refname, err->msg);
1792 } else
1793 send_ref_update_ok(client, &iref, refname);
1795 if (client->nref_updates > 0) {
1796 client->nref_updates--;
1797 if (client->nref_updates == 0)
1798 send_refs_updated(client);
1801 if (locked) {
1802 const struct got_error *unlock_err;
1803 unlock_err = got_ref_unlock(ref);
1804 if (unlock_err && err == NULL)
1805 err = unlock_err;
1807 if (ref)
1808 got_ref_close(ref);
1809 if (obj)
1810 got_object_close(obj);
1811 if (repo)
1812 got_repo_close(repo);
1813 free(refname);
1814 free(id);
1815 return err;
1818 static void
1819 gotd_dispatch(int fd, short event, void *arg)
1821 struct gotd_imsgev *iev = arg;
1822 struct imsgbuf *ibuf = &iev->ibuf;
1823 struct gotd_child_proc *proc = NULL;
1824 ssize_t n;
1825 int shut = 0;
1826 struct imsg imsg;
1828 if (event & EV_READ) {
1829 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1830 fatal("imsg_read error");
1831 if (n == 0) {
1832 /* Connection closed. */
1833 shut = 1;
1834 goto done;
1838 if (event & EV_WRITE) {
1839 n = msgbuf_write(&ibuf->w);
1840 if (n == -1 && errno != EAGAIN)
1841 fatal("msgbuf_write");
1842 if (n == 0) {
1843 /* Connection closed. */
1844 shut = 1;
1845 goto done;
1849 proc = find_proc_by_fd(fd);
1850 if (proc == NULL)
1851 fatalx("cannot find child process for fd %d", fd);
1853 for (;;) {
1854 const struct got_error *err = NULL;
1855 struct gotd_client *client = NULL;
1856 uint32_t client_id = 0;
1857 int do_disconnect = 0;
1858 int do_ref_updates = 0, do_ref_update = 0;
1859 int do_packfile_install = 0;
1861 if ((n = imsg_get(ibuf, &imsg)) == -1)
1862 fatal("%s: imsg_get error", __func__);
1863 if (n == 0) /* No more messages. */
1864 break;
1866 switch (imsg.hdr.type) {
1867 case GOTD_IMSG_ERROR:
1868 do_disconnect = 1;
1869 err = gotd_imsg_recv_error(&client_id, &imsg);
1870 break;
1871 case GOTD_IMSG_CONNECT:
1872 err = recv_connect(&client_id, &imsg);
1873 break;
1874 case GOTD_IMSG_PACKFILE_DONE:
1875 do_disconnect = 1;
1876 err = recv_packfile_done(&client_id, &imsg);
1877 break;
1878 case GOTD_IMSG_PACKFILE_INSTALL:
1879 err = recv_packfile_install(&client_id, &imsg);
1880 if (err == NULL)
1881 do_packfile_install = 1;
1882 break;
1883 case GOTD_IMSG_REF_UPDATES_START:
1884 err = recv_ref_updates_start(&client_id, &imsg);
1885 if (err == NULL)
1886 do_ref_updates = 1;
1887 break;
1888 case GOTD_IMSG_REF_UPDATE:
1889 err = recv_ref_update(&client_id, &imsg);
1890 if (err == NULL)
1891 do_ref_update = 1;
1892 break;
1893 default:
1894 log_debug("unexpected imsg %d", imsg.hdr.type);
1895 break;
1898 client = find_client(client_id);
1899 if (client == NULL) {
1900 log_warnx("%s: client not found", __func__);
1901 imsg_free(&imsg);
1902 continue;
1905 if (!verify_imsg_src(client, proc, &imsg)) {
1906 log_debug("dropping imsg type %d from PID %d",
1907 imsg.hdr.type, proc->pid);
1908 imsg_free(&imsg);
1909 continue;
1911 if (err)
1912 log_warnx("uid %d: %s", client->euid, err->msg);
1914 if (do_disconnect) {
1915 if (err)
1916 disconnect_on_error(client, err);
1917 else
1918 disconnect(client);
1919 } else {
1920 if (do_packfile_install)
1921 err = install_pack(client, proc->repo_path,
1922 &imsg);
1923 else if (do_ref_updates)
1924 err = begin_ref_updates(client, &imsg);
1925 else if (do_ref_update)
1926 err = update_ref(client, proc->repo_path,
1927 &imsg);
1928 if (err)
1929 log_warnx("uid %d: %s", client->euid, err->msg);
1931 imsg_free(&imsg);
1933 done:
1934 if (!shut) {
1935 gotd_imsg_event_add(iev);
1936 } else {
1937 /* This pipe is dead. Remove its event handler */
1938 event_del(&iev->ev);
1939 event_loopexit(NULL);
1943 static pid_t
1944 start_child(enum gotd_procid proc_id, const char *repo_path,
1945 char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
1947 char *argv[11];
1948 int argc = 0;
1949 pid_t pid;
1951 switch (pid = fork()) {
1952 case -1:
1953 fatal("cannot fork");
1954 case 0:
1955 break;
1956 default:
1957 close(fd);
1958 return pid;
1961 if (fd != GOTD_FILENO_MSG_PIPE) {
1962 if (dup2(fd, GOTD_FILENO_MSG_PIPE) == -1)
1963 fatal("cannot setup imsg fd");
1964 } else if (fcntl(fd, F_SETFD, 0) == -1)
1965 fatal("cannot setup imsg fd");
1967 argv[argc++] = argv0;
1968 switch (proc_id) {
1969 case PROC_LISTEN:
1970 argv[argc++] = (char *)"-L";
1971 break;
1972 case PROC_REPO_READ:
1973 argv[argc++] = (char *)"-R";
1974 break;
1975 case PROC_REPO_WRITE:
1976 argv[argc++] = (char *)"-W";
1977 break;
1978 default:
1979 fatalx("invalid process id %d", proc_id);
1982 argv[argc++] = (char *)"-f";
1983 argv[argc++] = (char *)confpath;
1985 if (repo_path) {
1986 argv[argc++] = (char *)"-P";
1987 argv[argc++] = (char *)repo_path;
1990 if (!daemonize)
1991 argv[argc++] = (char *)"-d";
1992 if (verbosity > 0)
1993 argv[argc++] = (char *)"-v";
1994 if (verbosity > 1)
1995 argv[argc++] = (char *)"-v";
1996 argv[argc++] = NULL;
1998 execvp(argv0, argv);
1999 fatal("execvp");
2002 static void
2003 start_listener(char *argv0, const char *confpath, int daemonize, int verbosity)
2005 struct gotd_child_proc *proc = &gotd.procs[0];
2007 proc->type = PROC_LISTEN;
2009 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
2010 PF_UNSPEC, proc->pipe) == -1)
2011 fatal("socketpair");
2013 proc->pid = start_child(proc->type, NULL, argv0, confpath,
2014 proc->pipe[1], daemonize, verbosity);
2015 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
2016 proc->iev.handler = gotd_dispatch;
2017 proc->iev.events = EV_READ;
2018 proc->iev.handler_arg = NULL;
2021 static void
2022 start_repo_children(struct gotd *gotd, char *argv0, const char *confpath,
2023 int daemonize, int verbosity)
2025 struct gotd_repo *repo = NULL;
2026 struct gotd_child_proc *proc;
2027 int i;
2029 for (i = 1; i < gotd->nprocs; i++) {
2030 if (repo == NULL)
2031 repo = TAILQ_FIRST(&gotd->repos);
2032 proc = &gotd->procs[i];
2033 if (i - 1 < gotd->nrepos)
2034 proc->type = PROC_REPO_READ;
2035 else
2036 proc->type = PROC_REPO_WRITE;
2037 if (strlcpy(proc->repo_name, repo->name,
2038 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
2039 fatalx("repository name too long: %s", repo->name);
2040 log_debug("adding repository %s", repo->name);
2041 if (realpath(repo->path, proc->repo_path) == NULL)
2042 fatal("%s", repo->path);
2043 if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
2044 PF_UNSPEC, proc->pipe) == -1)
2045 fatal("socketpair");
2046 proc->pid = start_child(proc->type, proc->repo_path, argv0,
2047 confpath, proc->pipe[1], daemonize, verbosity);
2048 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
2049 log_debug("proc %s %s is on fd %d",
2050 gotd_proc_names[proc->type], proc->repo_path,
2051 proc->pipe[0]);
2052 proc->iev.handler = gotd_dispatch;
2053 proc->iev.events = EV_READ;
2054 proc->iev.handler_arg = NULL;
2055 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
2056 gotd_dispatch, &proc->iev);
2058 repo = TAILQ_NEXT(repo, entry);
2062 static void
2063 apply_unveil_repo_readonly(const char *repo_path)
2065 if (unveil(repo_path, "r") == -1)
2066 fatal("unveil %s", repo_path);
2068 if (unveil(NULL, NULL) == -1)
2069 fatal("unveil");
2072 static void
2073 apply_unveil(void)
2075 struct gotd_repo *repo;
2077 TAILQ_FOREACH(repo, &gotd.repos, entry) {
2078 if (unveil(repo->path, "rwc") == -1)
2079 fatal("unveil %s", repo->path);
2082 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
2083 fatal("unveil %s", GOT_TMPDIR_STR);
2085 if (unveil(NULL, NULL) == -1)
2086 fatal("unveil");
2089 int
2090 main(int argc, char **argv)
2092 const struct got_error *error = NULL;
2093 int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
2094 const char *confpath = GOTD_CONF_PATH;
2095 char *argv0 = argv[0];
2096 char title[2048];
2097 gid_t groups[NGROUPS_MAX];
2098 int ngroups = NGROUPS_MAX;
2099 struct passwd *pw = NULL;
2100 struct group *gr = NULL;
2101 char *repo_path = NULL;
2102 enum gotd_procid proc_id = PROC_GOTD;
2103 struct event evsigint, evsigterm, evsighup, evsigusr1;
2104 int *pack_fds = NULL, *temp_fds = NULL;
2106 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
2108 while ((ch = getopt(argc, argv, "df:LnP:RvW")) != -1) {
2109 switch (ch) {
2110 case 'd':
2111 daemonize = 0;
2112 break;
2113 case 'f':
2114 confpath = optarg;
2115 break;
2116 case 'L':
2117 proc_id = PROC_LISTEN;
2118 break;
2119 case 'n':
2120 noaction = 1;
2121 break;
2122 case 'P':
2123 repo_path = realpath(optarg, NULL);
2124 if (repo_path == NULL)
2125 fatal("realpath '%s'", optarg);
2126 break;
2127 case 'R':
2128 proc_id = PROC_REPO_READ;
2129 break;
2130 case 'v':
2131 if (verbosity < 3)
2132 verbosity++;
2133 break;
2134 case 'W':
2135 proc_id = PROC_REPO_WRITE;
2136 break;
2137 default:
2138 usage();
2142 argc -= optind;
2143 argv += optind;
2145 if (argc != 0)
2146 usage();
2148 if (geteuid())
2149 fatalx("need root privileges");
2151 log_init(daemonize ? 0 : 1, LOG_DAEMON);
2152 log_setverbose(verbosity);
2154 if (parse_config(confpath, proc_id, &gotd) != 0)
2155 return 1;
2157 if (proc_id == PROC_GOTD &&
2158 (gotd.nrepos == 0 || TAILQ_EMPTY(&gotd.repos)))
2159 fatalx("no repository defined in configuration file");
2161 pw = getpwnam(gotd.user_name);
2162 if (pw == NULL)
2163 fatal("getpwuid: user %s not found", gotd.user_name);
2165 if (pw->pw_uid == 0) {
2166 fatalx("cannot run %s as %s: the user running %s "
2167 "must not be the superuser",
2168 getprogname(), pw->pw_name, getprogname());
2171 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups) == -1)
2172 log_warnx("group membership list truncated");
2174 gr = match_group(groups, ngroups, gotd.unix_group_name);
2175 if (gr == NULL) {
2176 fatalx("cannot start %s: the user running %s "
2177 "must be a secondary member of group %s",
2178 getprogname(), getprogname(), gotd.unix_group_name);
2180 if (gr->gr_gid == pw->pw_gid) {
2181 fatalx("cannot start %s: the user running %s "
2182 "must be a secondary member of group %s, but "
2183 "%s is the user's primary group",
2184 getprogname(), getprogname(), gotd.unix_group_name,
2185 gotd.unix_group_name);
2188 if (proc_id == PROC_LISTEN &&
2189 !got_path_is_absolute(gotd.unix_socket_path))
2190 fatalx("bad unix socket path \"%s\": must be an absolute path",
2191 gotd.unix_socket_path);
2193 if (noaction)
2194 return 0;
2196 if (proc_id == PROC_GOTD) {
2197 gotd.pid = getpid();
2198 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2200 * Start a listener and repository readers/writers.
2201 * XXX For now, use one reader and one writer per repository.
2202 * This should be changed to N readers + M writers.
2204 gotd.nprocs = 1 + gotd.nrepos * 2;
2205 gotd.procs = calloc(gotd.nprocs, sizeof(*gotd.procs));
2206 if (gotd.procs == NULL)
2207 fatal("calloc");
2208 start_listener(argv0, confpath, daemonize, verbosity);
2209 start_repo_children(&gotd, argv0, confpath, daemonize,
2210 verbosity);
2211 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
2212 if (daemonize && daemon(1, 0) == -1)
2213 fatal("daemon");
2214 } else if (proc_id == PROC_LISTEN) {
2215 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2216 if (verbosity) {
2217 log_info("socket: %s", gotd.unix_socket_path);
2218 log_info("user: %s", pw->pw_name);
2219 log_info("secondary group: %s", gr->gr_name);
2222 fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
2223 gr->gr_gid);
2224 if (fd == -1) {
2225 fatal("cannot listen on unix socket %s",
2226 gotd.unix_socket_path);
2228 if (daemonize && daemon(0, 0) == -1)
2229 fatal("daemon");
2230 } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE) {
2231 error = got_repo_pack_fds_open(&pack_fds);
2232 if (error != NULL)
2233 fatalx("cannot open pack tempfiles: %s", error->msg);
2234 error = got_repo_temp_fds_open(&temp_fds);
2235 if (error != NULL)
2236 fatalx("cannot open pack tempfiles: %s", error->msg);
2237 if (repo_path == NULL)
2238 fatalx("repository path not specified");
2239 snprintf(title, sizeof(title), "%s %s",
2240 gotd_proc_names[proc_id], repo_path);
2241 if (daemonize && daemon(0, 0) == -1)
2242 fatal("daemon");
2243 } else
2244 fatal("invalid process id %d", proc_id);
2246 setproctitle("%s", title);
2247 log_procinit(title);
2249 /* Drop root privileges. */
2250 if (setgid(pw->pw_gid) == -1)
2251 fatal("setgid %d failed", pw->pw_gid);
2252 if (setuid(pw->pw_uid) == -1)
2253 fatal("setuid %d failed", pw->pw_uid);
2255 event_init();
2257 switch (proc_id) {
2258 case PROC_GOTD:
2259 #ifndef PROFILE
2260 if (pledge("stdio rpath wpath cpath proc getpw sendfd recvfd "
2261 "fattr flock unix unveil", NULL) == -1)
2262 err(1, "pledge");
2263 #endif
2264 break;
2265 case PROC_LISTEN:
2266 #ifndef PROFILE
2267 if (pledge("stdio sendfd unix", NULL) == -1)
2268 err(1, "pledge");
2269 #endif
2270 listen_main(title, fd);
2271 /* NOTREACHED */
2272 break;
2273 case PROC_REPO_READ:
2274 #ifndef PROFILE
2275 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2276 err(1, "pledge");
2277 #endif
2278 apply_unveil_repo_readonly(repo_path);
2279 repo_read_main(title, repo_path, pack_fds, temp_fds);
2280 /* NOTREACHED */
2281 exit(0);
2282 case PROC_REPO_WRITE:
2283 #ifndef PROFILE
2284 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2285 err(1, "pledge");
2286 #endif
2287 apply_unveil_repo_readonly(repo_path);
2288 repo_write_main(title, repo_path, pack_fds, temp_fds);
2289 /* NOTREACHED */
2290 exit(0);
2291 default:
2292 fatal("invalid process id %d", proc_id);
2295 if (proc_id != PROC_GOTD)
2296 fatal("invalid process id %d", proc_id);
2298 apply_unveil();
2300 signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
2301 signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
2302 signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
2303 signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
2304 signal(SIGPIPE, SIG_IGN);
2306 signal_add(&evsigint, NULL);
2307 signal_add(&evsigterm, NULL);
2308 signal_add(&evsighup, NULL);
2309 signal_add(&evsigusr1, NULL);
2311 gotd_imsg_event_add(&gotd.procs[0].iev);
2313 event_dispatch();
2315 if (pack_fds)
2316 got_repo_pack_fds_close(pack_fds);
2317 free(repo_path);
2318 return 0;