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 "got_compat.h"
19 #include <sys/queue.h>
20 #include <sys/time.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <sys/socket.h>
24 #include <sys/un.h>
25 #include <sys/wait.h>
26 #include <sys/resource.h>
28 #include <fcntl.h>
29 #include <err.h>
30 #include <errno.h>
31 #include <event.h>
32 #include <limits.h>
33 #include <pwd.h>
34 #include <imsg.h>
35 #include <signal.h>
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <syslog.h>
41 #include <unistd.h>
43 #include "got_error.h"
44 #include "got_opentemp.h"
45 #include "got_path.h"
46 #include "got_repository.h"
47 #include "got_object.h"
48 #include "got_reference.h"
49 #include "got_diff.h"
51 #include "got_lib_delta.h"
52 #include "got_lib_object.h"
53 #include "got_lib_object_cache.h"
54 #include "got_lib_hash.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 "session.h"
64 #include "repo_read.h"
65 #include "repo_write.h"
66 #include "notify.h"
68 #ifndef nitems
69 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
70 #endif
72 enum gotd_client_state {
73 GOTD_CLIENT_STATE_NEW,
74 GOTD_CLIENT_STATE_ACCESS_GRANTED,
75 };
77 struct gotd_child_proc {
78 pid_t pid;
79 enum gotd_procid type;
80 char repo_name[NAME_MAX];
81 char repo_path[PATH_MAX];
82 int pipe[2];
83 struct gotd_imsgev iev;
84 struct event tmo;
86 TAILQ_ENTRY(gotd_child_proc) entry;
87 };
88 TAILQ_HEAD(gotd_procs, gotd_child_proc) procs;
90 struct gotd_client {
91 STAILQ_ENTRY(gotd_client) entry;
92 enum gotd_client_state state;
93 uint32_t id;
94 int fd;
95 struct gotd_imsgev iev;
96 struct event tmo;
97 uid_t euid;
98 gid_t egid;
99 char *username;
100 struct gotd_child_proc *repo;
101 struct gotd_child_proc *auth;
102 struct gotd_child_proc *session;
103 int required_auth;
104 };
105 STAILQ_HEAD(gotd_clients, gotd_client);
107 static struct gotd_clients gotd_clients[GOTD_CLIENT_TABLE_SIZE];
108 static SIPHASH_KEY clients_hash_key;
109 volatile int client_cnt;
110 static struct timeval auth_timeout = { 5, 0 };
111 static struct gotd gotd;
113 void gotd_sighdlr(int sig, short event, void *arg);
114 static void gotd_shutdown(void);
115 static const struct got_error *start_session_child(struct gotd_client *,
116 struct gotd_repo *, char *, const char *, int, int);
117 static const struct got_error *start_repo_child(struct gotd_client *,
118 enum gotd_procid, struct gotd_repo *, char *, const char *, int, int);
119 static const struct got_error *start_auth_child(struct gotd_client *, int,
120 struct gotd_repo *, char *, const char *, int, int);
121 static void kill_proc(struct gotd_child_proc *, int);
122 static void disconnect(struct gotd_client *);
123 static void drop_privs(struct passwd *);
125 __dead static void
126 usage(void)
128 fprintf(stderr, "usage: %s [-dnv] [-f config-file]\n", getprogname());
129 exit(1);
132 static void
133 drop_privs(struct passwd *pw)
135 /* Drop root privileges. */
136 if (setgid(pw->pw_gid) == -1)
137 fatal("setgid %d failed", pw->pw_gid);
138 if (setuid(pw->pw_uid) == -1)
139 fatal("setuid %d failed", pw->pw_uid);
142 static int
143 unix_socket_listen(const char *unix_socket_path, uid_t uid, gid_t gid)
145 struct sockaddr_un sun;
146 int fd = -1;
147 mode_t old_umask, mode;
148 int sock_flags = SOCK_STREAM | SOCK_NONBLOCK;
150 #ifdef SOCK_CLOEXEC
151 sock_flags |= SOCK_CLOEXEC;
152 #endif
154 fd = socket(AF_UNIX, sock_flags, 0);
155 if (fd == -1) {
156 log_warn("socket");
157 return -1;
160 sun.sun_family = AF_UNIX;
161 if (strlcpy(sun.sun_path, unix_socket_path,
162 sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
163 log_warnx("%s: name too long", unix_socket_path);
164 close(fd);
165 return -1;
168 if (unlink(unix_socket_path) == -1) {
169 if (errno != ENOENT) {
170 log_warn("unlink %s", unix_socket_path);
171 close(fd);
172 return -1;
176 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
177 mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
179 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
180 log_warn("bind: %s", unix_socket_path);
181 close(fd);
182 umask(old_umask);
183 return -1;
186 umask(old_umask);
188 if (chmod(unix_socket_path, mode) == -1) {
189 log_warn("chmod %o %s", mode, unix_socket_path);
190 close(fd);
191 unlink(unix_socket_path);
192 return -1;
195 if (chown(unix_socket_path, uid, gid) == -1) {
196 log_warn("chown %s uid=%d gid=%d", unix_socket_path, uid, gid);
197 close(fd);
198 unlink(unix_socket_path);
199 return -1;
202 if (listen(fd, GOTD_UNIX_SOCKET_BACKLOG) == -1) {
203 log_warn("listen");
204 close(fd);
205 unlink(unix_socket_path);
206 return -1;
209 return fd;
212 static uint64_t
213 client_hash(uint32_t client_id)
215 return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
218 static void
219 add_client(struct gotd_client *client)
221 uint64_t slot = client_hash(client->id) % nitems(gotd_clients);
222 STAILQ_INSERT_HEAD(&gotd_clients[slot], client, entry);
223 client_cnt++;
226 static struct gotd_client *
227 find_client(uint32_t client_id)
229 uint64_t slot;
230 struct gotd_client *c;
232 slot = client_hash(client_id) % nitems(gotd_clients);
233 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
234 if (c->id == client_id)
235 return c;
238 return NULL;
241 static struct gotd_client *
242 find_client_by_proc_fd(int fd)
244 uint64_t slot;
246 for (slot = 0; slot < nitems(gotd_clients); slot++) {
247 struct gotd_client *c;
249 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
250 if (c->repo && c->repo->iev.ibuf.fd == fd)
251 return c;
252 if (c->auth && c->auth->iev.ibuf.fd == fd)
253 return c;
254 if (c->session && c->session->iev.ibuf.fd == fd)
255 return c;
259 return NULL;
262 static int
263 client_is_reading(struct gotd_client *client)
265 return (client->required_auth &
266 (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) == GOTD_AUTH_READ;
269 static int
270 client_is_writing(struct gotd_client *client)
272 return (client->required_auth &
273 (GOTD_AUTH_READ | GOTD_AUTH_WRITE)) ==
274 (GOTD_AUTH_READ | GOTD_AUTH_WRITE);
277 static const struct got_error *
278 ensure_client_is_not_writing(struct gotd_client *client)
280 if (client_is_writing(client)) {
281 return got_error_fmt(GOT_ERR_BAD_PACKET,
282 "uid %d made a read-request but is writing to "
283 "a repository", client->euid);
286 return NULL;
289 static const struct got_error *
290 ensure_client_is_not_reading(struct gotd_client *client)
292 if (client_is_reading(client)) {
293 return got_error_fmt(GOT_ERR_BAD_PACKET,
294 "uid %d made a write-request but is reading from "
295 "a repository", client->euid);
298 return NULL;
301 static void
302 proc_done(struct gotd_child_proc *proc)
304 struct gotd_client *client;
306 TAILQ_REMOVE(&procs, proc, entry);
308 client = find_client_by_proc_fd(proc->iev.ibuf.fd);
309 if (client != NULL) {
310 if (proc == client->repo)
311 client->repo = NULL;
312 if (proc == client->auth)
313 client->auth = NULL;
314 if (proc == client->session)
315 client->session = NULL;
316 disconnect(client);
319 if (proc == gotd.notify_proc)
320 gotd.notify_proc = NULL;
322 evtimer_del(&proc->tmo);
324 if (proc->iev.ibuf.fd != -1) {
325 event_del(&proc->iev.ev);
326 msgbuf_clear(&proc->iev.ibuf.w);
327 close(proc->iev.ibuf.fd);
330 free(proc);
333 static void
334 kill_repo_proc(struct gotd_client *client)
336 if (client->repo == NULL)
337 return;
339 kill_proc(client->repo, 0);
340 client->repo = NULL;
343 static void
344 kill_auth_proc(struct gotd_client *client)
346 if (client->auth == NULL)
347 return;
349 kill_proc(client->auth, 0);
350 client->auth = NULL;
353 static void
354 kill_session_proc(struct gotd_client *client)
356 if (client->session == NULL)
357 return;
359 kill_proc(client->session, 0);
360 client->session = NULL;
363 static void
364 disconnect(struct gotd_client *client)
366 struct gotd_imsg_disconnect idisconnect;
367 struct gotd_child_proc *listen_proc = gotd.listen_proc;
368 uint64_t slot;
370 log_debug("uid %d: disconnecting", client->euid);
372 kill_auth_proc(client);
373 kill_session_proc(client);
374 kill_repo_proc(client);
376 idisconnect.client_id = client->id;
377 if (gotd_imsg_compose_event(&listen_proc->iev,
378 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
379 &idisconnect, sizeof(idisconnect)) == -1)
380 log_warn("imsg compose DISCONNECT");
382 slot = client_hash(client->id) % nitems(gotd_clients);
383 STAILQ_REMOVE(&gotd_clients[slot], client, gotd_client, entry);
384 imsg_clear(&client->iev.ibuf);
385 event_del(&client->iev.ev);
386 evtimer_del(&client->tmo);
387 if (client->fd != -1)
388 close(client->fd);
389 else if (client->iev.ibuf.fd != -1)
390 close(client->iev.ibuf.fd);
391 free(client->username);
392 free(client);
393 client_cnt--;
396 static void
397 disconnect_on_error(struct gotd_client *client, const struct got_error *err)
399 struct imsgbuf ibuf;
401 if (err->code != GOT_ERR_EOF) {
402 log_warnx("uid %d: %s", client->euid, err->msg);
403 if (client->fd != -1) {
404 imsg_init(&ibuf, client->fd);
405 gotd_imsg_send_error(&ibuf, 0, PROC_GOTD, err);
406 imsg_clear(&ibuf);
409 disconnect(client);
412 static const struct got_error *
413 send_repo_info(struct gotd_imsgev *iev, struct gotd_repo *repo)
415 const struct got_error *err = NULL;
416 struct gotd_imsg_info_repo irepo;
418 memset(&irepo, 0, sizeof(irepo));
420 if (strlcpy(irepo.repo_name, repo->name, sizeof(irepo.repo_name))
421 >= sizeof(irepo.repo_name))
422 return got_error_msg(GOT_ERR_NO_SPACE, "repo name too long");
423 if (strlcpy(irepo.repo_path, repo->path, sizeof(irepo.repo_path))
424 >= sizeof(irepo.repo_path))
425 return got_error_msg(GOT_ERR_NO_SPACE, "repo path too long");
427 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_REPO, PROC_GOTD, -1,
428 &irepo, sizeof(irepo)) == -1) {
429 err = got_error_from_errno("imsg compose INFO_REPO");
430 if (err)
431 return err;
434 return NULL;
437 static const struct got_error *
438 send_client_info(struct gotd_imsgev *iev, struct gotd_client *client)
440 const struct got_error *err = NULL;
441 struct gotd_imsg_info_client iclient;
442 struct gotd_child_proc *proc;
444 memset(&iclient, 0, sizeof(iclient));
445 iclient.euid = client->euid;
446 iclient.egid = client->egid;
448 proc = client->repo;
449 if (proc) {
450 if (strlcpy(iclient.repo_name, proc->repo_path,
451 sizeof(iclient.repo_name)) >= sizeof(iclient.repo_name)) {
452 return got_error_msg(GOT_ERR_NO_SPACE,
453 "repo name too long");
455 if (client_is_writing(client))
456 iclient.is_writing = 1;
458 iclient.repo_child_pid = proc->pid;
461 if (client->session)
462 iclient.session_child_pid = client->session->pid;
464 if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_CLIENT, PROC_GOTD, -1,
465 &iclient, sizeof(iclient)) == -1) {
466 err = got_error_from_errno("imsg compose INFO_CLIENT");
467 if (err)
468 return err;
471 return NULL;
474 static const struct got_error *
475 send_info(struct gotd_client *client)
477 const struct got_error *err = NULL;
478 struct gotd_imsg_info info;
479 uint64_t slot;
480 struct gotd_repo *repo;
482 if (client->euid != 0)
483 return got_error_set_errno(EPERM, "info");
485 info.pid = gotd.pid;
486 info.verbosity = gotd.verbosity;
487 info.nrepos = gotd.nrepos;
488 info.nclients = client_cnt - 1;
490 if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_INFO, PROC_GOTD, -1,
491 &info, sizeof(info)) == -1) {
492 err = got_error_from_errno("imsg compose INFO");
493 if (err)
494 return err;
497 TAILQ_FOREACH(repo, &gotd.repos, entry) {
498 err = send_repo_info(&client->iev, repo);
499 if (err)
500 return err;
503 for (slot = 0; slot < nitems(gotd_clients); slot++) {
504 struct gotd_client *c;
505 STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
506 if (c->id == client->id)
507 continue;
508 err = send_client_info(&client->iev, c);
509 if (err)
510 return err;
514 return NULL;
517 static const struct got_error *
518 stop_gotd(struct gotd_client *client)
521 if (client->euid != 0)
522 return got_error_set_errno(EPERM, "stop");
524 gotd_shutdown();
525 /* NOTREACHED */
526 return NULL;
529 static const struct got_error *
530 start_client_authentication(struct gotd_client *client, struct imsg *imsg)
532 const struct got_error *err;
533 struct gotd_imsg_list_refs ireq;
534 struct gotd_repo *repo = NULL;
535 size_t datalen;
537 log_debug("list-refs request from uid %d", client->euid);
539 if (client->state != GOTD_CLIENT_STATE_NEW)
540 return got_error_msg(GOT_ERR_BAD_REQUEST,
541 "unexpected list-refs request received");
543 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
544 if (datalen != sizeof(ireq))
545 return got_error(GOT_ERR_PRIVSEP_LEN);
547 memcpy(&ireq, imsg->data, datalen);
549 if (ireq.client_is_reading) {
550 err = ensure_client_is_not_writing(client);
551 if (err)
552 return err;
553 repo = gotd_find_repo_by_name(ireq.repo_name, &gotd.repos);
554 if (repo == NULL)
555 return got_error(GOT_ERR_NOT_GIT_REPO);
556 err = start_auth_child(client, GOTD_AUTH_READ, repo,
557 gotd.argv0, gotd.confpath, gotd.daemonize,
558 gotd.verbosity);
559 if (err)
560 return err;
561 } else {
562 err = ensure_client_is_not_reading(client);
563 if (err)
564 return err;
565 repo = gotd_find_repo_by_name(ireq.repo_name, &gotd.repos);
566 if (repo == NULL)
567 return got_error(GOT_ERR_NOT_GIT_REPO);
568 err = start_auth_child(client,
569 GOTD_AUTH_READ | GOTD_AUTH_WRITE,
570 repo, gotd.argv0, gotd.confpath, gotd.daemonize,
571 gotd.verbosity);
572 if (err)
573 return err;
576 evtimer_add(&client->tmo, &auth_timeout);
578 /* Flow continues upon authentication success/failure or timeout. */
579 return NULL;
582 static void
583 gotd_request(int fd, short events, void *arg)
585 struct gotd_imsgev *iev = arg;
586 struct imsgbuf *ibuf = &iev->ibuf;
587 struct gotd_client *client = iev->handler_arg;
588 const struct got_error *err = NULL;
589 struct imsg imsg;
590 ssize_t n;
592 if (events & EV_WRITE) {
593 while (ibuf->w.queued) {
594 n = msgbuf_write(&ibuf->w);
595 if (n == -1 && errno == EPIPE) {
596 /*
597 * The client has closed its socket.
598 * This can happen when Git clients are
599 * done sending pack file data.
600 */
601 msgbuf_clear(&ibuf->w);
602 continue;
603 } else if (n == -1 && errno != EAGAIN) {
604 err = got_error_from_errno("imsg_flush");
605 disconnect_on_error(client, err);
606 return;
608 if (n == 0) {
609 /* Connection closed. */
610 err = got_error(GOT_ERR_EOF);
611 disconnect_on_error(client, err);
612 return;
616 /* Disconnect gotctl(8) now that messages have been sent. */
617 if (!client_is_reading(client) && !client_is_writing(client)) {
618 disconnect(client);
619 return;
623 if ((events & EV_READ) == 0)
624 return;
626 memset(&imsg, 0, sizeof(imsg));
628 while (err == NULL) {
629 err = gotd_imsg_recv(&imsg, ibuf, 0);
630 if (err) {
631 if (err->code == GOT_ERR_PRIVSEP_READ)
632 err = NULL;
633 break;
636 evtimer_del(&client->tmo);
638 switch (imsg.hdr.type) {
639 case GOTD_IMSG_INFO:
640 err = send_info(client);
641 break;
642 case GOTD_IMSG_STOP:
643 err = stop_gotd(client);
644 break;
645 case GOTD_IMSG_LIST_REFS:
646 err = start_client_authentication(client, &imsg);
647 break;
648 default:
649 log_debug("unexpected imsg %d", imsg.hdr.type);
650 err = got_error(GOT_ERR_PRIVSEP_MSG);
651 break;
654 imsg_free(&imsg);
657 if (err) {
658 disconnect_on_error(client, err);
659 } else {
660 gotd_imsg_event_add(&client->iev);
664 static void
665 gotd_auth_timeout(int fd, short events, void *arg)
667 struct gotd_client *client = arg;
669 log_debug("disconnecting uid %d due to authentication timeout",
670 client->euid);
671 disconnect(client);
674 static const struct got_error *
675 recv_connect(uint32_t *client_id, struct imsg *imsg)
677 const struct got_error *err = NULL;
678 struct gotd_imsg_connect iconnect;
679 size_t datalen;
680 int s = -1;
681 struct gotd_client *client = NULL;
683 *client_id = 0;
685 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
686 if (datalen != sizeof(iconnect))
687 return got_error(GOT_ERR_PRIVSEP_LEN);
688 memcpy(&iconnect, imsg->data, sizeof(iconnect));
690 s = imsg_get_fd(imsg);
691 if (s == -1) {
692 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
693 goto done;
696 if (find_client(iconnect.client_id)) {
697 err = got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
698 goto done;
701 client = calloc(1, sizeof(*client));
702 if (client == NULL) {
703 err = got_error_from_errno("calloc");
704 goto done;
707 *client_id = iconnect.client_id;
709 client->state = GOTD_CLIENT_STATE_NEW;
710 client->id = iconnect.client_id;
711 client->fd = s;
712 s = -1;
713 /* The auth process will verify UID/GID for us. */
714 client->euid = iconnect.euid;
715 client->egid = iconnect.egid;
717 imsg_init(&client->iev.ibuf, client->fd);
718 client->iev.handler = gotd_request;
719 client->iev.events = EV_READ;
720 client->iev.handler_arg = client;
722 event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
723 &client->iev);
724 gotd_imsg_event_add(&client->iev);
726 evtimer_set(&client->tmo, gotd_auth_timeout, client);
728 add_client(client);
729 log_debug("%s: new client uid %d connected on fd %d", __func__,
730 client->euid, client->fd);
731 done:
732 if (err) {
733 struct gotd_child_proc *listen_proc = gotd.listen_proc;
734 struct gotd_imsg_disconnect idisconnect;
736 idisconnect.client_id = client->id;
737 if (gotd_imsg_compose_event(&listen_proc->iev,
738 GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
739 &idisconnect, sizeof(idisconnect)) == -1)
740 log_warn("imsg compose DISCONNECT");
742 if (s != -1)
743 close(s);
746 return err;
749 static const char *gotd_proc_names[PROC_MAX] = {
750 "parent",
751 "listen",
752 "auth",
753 "session_read",
754 "session_write",
755 "repo_read",
756 "repo_write",
757 "gitwrapper",
758 "notify"
759 };
761 static void
762 kill_proc(struct gotd_child_proc *proc, int fatal)
764 struct timeval tv = { 5, 0 };
766 log_debug("kill -%d %d", fatal ? SIGKILL : SIGTERM, proc->pid);
768 if (proc->iev.ibuf.fd != -1) {
769 event_del(&proc->iev.ev);
770 msgbuf_clear(&proc->iev.ibuf.w);
771 close(proc->iev.ibuf.fd);
772 proc->iev.ibuf.fd = -1;
775 if (!evtimer_pending(&proc->tmo, NULL) && !fatal)
776 evtimer_add(&proc->tmo, &tv);
778 if (fatal) {
779 log_warnx("sending SIGKILL to PID %d", proc->pid);
780 kill(proc->pid, SIGKILL);
781 } else
782 kill(proc->pid, SIGTERM);
785 static void
786 kill_proc_timeout(int fd, short ev, void *d)
788 struct gotd_child_proc *proc = d;
790 log_warnx("timeout waiting for PID %d to terminate;"
791 " retrying with force", proc->pid);
792 kill_proc(proc, 1);
795 static void
796 gotd_shutdown(void)
798 uint64_t slot;
800 log_debug("shutting down");
801 for (slot = 0; slot < nitems(gotd_clients); slot++) {
802 struct gotd_client *c, *tmp;
804 STAILQ_FOREACH_SAFE(c, &gotd_clients[slot], entry, tmp)
805 disconnect(c);
808 kill_proc(gotd.listen_proc, 0);
810 log_info("terminating");
811 exit(0);
814 static struct gotd_child_proc *
815 find_proc_by_pid(pid_t pid)
817 struct gotd_child_proc *proc = NULL;
819 TAILQ_FOREACH(proc, &procs, entry)
820 if (proc->pid == pid)
821 break;
823 return proc;
826 void
827 gotd_sighdlr(int sig, short event, void *arg)
829 struct gotd_child_proc *proc;
830 pid_t pid;
831 int status;
833 /*
834 * Normal signal handler rules don't apply because libevent
835 * decouples for us.
836 */
838 switch (sig) {
839 case SIGHUP:
840 log_info("%s: ignoring SIGHUP", __func__);
841 break;
842 case SIGUSR1:
843 log_info("%s: ignoring SIGUSR1", __func__);
844 break;
845 case SIGTERM:
846 case SIGINT:
847 gotd_shutdown();
848 break;
849 case SIGCHLD:
850 for (;;) {
851 pid = waitpid(WAIT_ANY, &status, WNOHANG);
852 if (pid == -1) {
853 if (errno == EINTR)
854 continue;
855 if (errno == ECHILD)
856 break;
857 fatal("waitpid");
859 if (pid == 0)
860 break;
862 log_debug("reaped pid %d", pid);
863 proc = find_proc_by_pid(pid);
864 if (proc == NULL) {
865 log_info("caught exit of unknown child %d",
866 pid);
867 continue;
870 if (WIFSIGNALED(status)) {
871 log_warnx("child PID %d terminated with"
872 " signal %d", pid, WTERMSIG(status));
875 proc_done(proc);
877 break;
878 default:
879 fatalx("unexpected signal");
883 static const struct got_error *
884 ensure_proc_is_reading(struct gotd_client *client,
885 struct gotd_child_proc *proc)
887 if (!client_is_reading(client)) {
888 kill_proc(proc, 1);
889 return got_error_fmt(GOT_ERR_BAD_PACKET,
890 "PID %d handled a read-request for uid %d but this "
891 "user is not reading from a repository", proc->pid,
892 client->euid);
895 return NULL;
898 static const struct got_error *
899 ensure_proc_is_writing(struct gotd_client *client,
900 struct gotd_child_proc *proc)
902 if (!client_is_writing(client)) {
903 kill_proc(proc, 1);
904 return got_error_fmt(GOT_ERR_BAD_PACKET,
905 "PID %d handled a write-request for uid %d but this "
906 "user is not writing to a repository", proc->pid,
907 client->euid);
910 return NULL;
913 static int
914 verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
915 struct imsg *imsg)
917 const struct got_error *err;
918 int ret = 0;
920 if (proc->type == PROC_REPO_READ || proc->type == PROC_REPO_WRITE) {
921 if (client->repo == NULL)
922 fatalx("no process found for uid %d", client->euid);
923 if (proc->pid != client->repo->pid) {
924 kill_proc(proc, 1);
925 log_warnx("received message from PID %d for uid %d, "
926 "while PID %d is the process serving this user",
927 proc->pid, client->euid, client->repo->pid);
928 return 0;
931 if (proc->type == PROC_SESSION_READ ||
932 proc->type == PROC_SESSION_WRITE) {
933 if (client->session == NULL) {
934 log_warnx("no session found for uid %d", client->euid);
935 return 0;
937 if (proc->pid != client->session->pid) {
938 kill_proc(proc, 1);
939 log_warnx("received message from PID %d for uid %d, "
940 "while PID %d is the process serving this user",
941 proc->pid, client->euid, client->session->pid);
942 return 0;
946 switch (imsg->hdr.type) {
947 case GOTD_IMSG_ERROR:
948 ret = 1;
949 break;
950 case GOTD_IMSG_CONNECT:
951 if (proc->type != PROC_LISTEN) {
952 err = got_error_fmt(GOT_ERR_BAD_PACKET,
953 "new connection for uid %d from PID %d "
954 "which is not the listen process",
955 client->euid, proc->pid);
956 } else
957 ret = 1;
958 break;
959 case GOTD_IMSG_ACCESS_GRANTED:
960 if (proc->type != PROC_AUTH) {
961 err = got_error_fmt(GOT_ERR_BAD_PACKET,
962 "authentication of uid %d from PID %d "
963 "which is not the auth process",
964 client->euid, proc->pid);
965 } else
966 ret = 1;
967 break;
968 case GOTD_IMSG_CLIENT_SESSION_READY:
969 if (proc->type != PROC_SESSION_READ &&
970 proc->type != PROC_SESSION_WRITE) {
971 err = got_error_fmt(GOT_ERR_BAD_PACKET,
972 "unexpected \"ready\" signal from PID %d",
973 proc->pid);
974 } else
975 ret = 1;
976 break;
977 case GOTD_IMSG_REPO_CHILD_READY:
978 if (proc->type != PROC_REPO_READ &&
979 proc->type != PROC_REPO_WRITE) {
980 err = got_error_fmt(GOT_ERR_BAD_PACKET,
981 "unexpected \"ready\" signal from PID %d",
982 proc->pid);
983 } else
984 ret = 1;
985 break;
986 case GOTD_IMSG_PACKFILE_DONE:
987 err = ensure_proc_is_reading(client, proc);
988 if (err)
989 log_warnx("uid %d: %s", client->euid, err->msg);
990 else
991 ret = 1;
992 break;
993 case GOTD_IMSG_PACKFILE_INSTALL:
994 case GOTD_IMSG_REF_UPDATES_START:
995 case GOTD_IMSG_REF_UPDATE:
996 err = ensure_proc_is_writing(client, proc);
997 if (err)
998 log_warnx("uid %d: %s", client->euid, err->msg);
999 else
1000 ret = 1;
1001 break;
1002 default:
1003 log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
1004 break;
1007 return ret;
1010 static const struct got_error *
1011 connect_repo_child(struct gotd_client *client,
1012 struct gotd_child_proc *repo_proc)
1014 static const struct got_error *err;
1015 struct gotd_imsgev *session_iev = &client->session->iev;
1016 struct gotd_imsg_connect_repo_child ireq;
1017 int pipe[2];
1018 int sock_flags = SOCK_STREAM | SOCK_NONBLOCK;
1020 #ifdef SOCK_CLOEXEC
1021 sock_flags |= SOCK_CLOEXEC;
1022 #endif
1024 if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED)
1025 return got_error_msg(GOT_ERR_BAD_REQUEST,
1026 "unexpected repo child ready signal received");
1028 if (socketpair(AF_UNIX, sock_flags, PF_UNSPEC, pipe) == -1)
1029 fatal("socketpair");
1031 memset(&ireq, 0, sizeof(ireq));
1032 ireq.client_id = client->id;
1033 ireq.proc_id = repo_proc->type;
1035 /* Pass repo child pipe to session child process. */
1036 if (gotd_imsg_compose_event(session_iev, GOTD_IMSG_CONNECT_REPO_CHILD,
1037 PROC_GOTD, pipe[0], &ireq, sizeof(ireq)) == -1) {
1038 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
1039 close(pipe[0]);
1040 close(pipe[1]);
1041 return err;
1044 /* Pass session child pipe to repo child process. */
1045 if (gotd_imsg_compose_event(&repo_proc->iev,
1046 GOTD_IMSG_CONNECT_REPO_CHILD, PROC_GOTD, pipe[1], NULL, 0) == -1) {
1047 err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
1048 close(pipe[1]);
1049 return err;
1052 return NULL;
1055 static void
1056 gotd_dispatch_listener(int fd, short event, void *arg)
1058 struct gotd_imsgev *iev = arg;
1059 struct imsgbuf *ibuf = &iev->ibuf;
1060 struct gotd_child_proc *proc = gotd.listen_proc;
1061 ssize_t n;
1062 int shut = 0;
1063 struct imsg imsg;
1065 if (proc->iev.ibuf.fd != fd)
1066 fatalx("%s: unexpected fd %d", __func__, fd);
1068 if (event & EV_READ) {
1069 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1070 fatal("imsg_read error");
1071 if (n == 0) {
1072 /* Connection closed. */
1073 shut = 1;
1074 goto done;
1078 if (event & EV_WRITE) {
1079 n = msgbuf_write(&ibuf->w);
1080 if (n == -1 && errno != EAGAIN)
1081 fatal("msgbuf_write");
1082 if (n == 0) {
1083 /* Connection closed. */
1084 shut = 1;
1085 goto done;
1089 for (;;) {
1090 const struct got_error *err = NULL;
1091 struct gotd_client *client = NULL;
1092 uint32_t client_id = 0;
1093 int do_disconnect = 0;
1095 if ((n = imsg_get(ibuf, &imsg)) == -1)
1096 fatal("%s: imsg_get error", __func__);
1097 if (n == 0) /* No more messages. */
1098 break;
1100 switch (imsg.hdr.type) {
1101 case GOTD_IMSG_ERROR:
1102 do_disconnect = 1;
1103 err = gotd_imsg_recv_error(&client_id, &imsg);
1104 break;
1105 case GOTD_IMSG_CONNECT:
1106 err = recv_connect(&client_id, &imsg);
1107 break;
1108 default:
1109 log_debug("unexpected imsg %d", imsg.hdr.type);
1110 break;
1113 client = find_client(client_id);
1114 if (client == NULL) {
1115 log_warnx("%s: client not found", __func__);
1116 imsg_free(&imsg);
1117 continue;
1120 if (err)
1121 log_warnx("uid %d: %s", client->euid, err->msg);
1123 if (do_disconnect) {
1124 if (err)
1125 disconnect_on_error(client, err);
1126 else
1127 disconnect(client);
1130 imsg_free(&imsg);
1132 done:
1133 if (!shut) {
1134 gotd_imsg_event_add(iev);
1135 } else {
1136 /* This pipe is dead. Remove its event handler */
1137 event_del(&iev->ev);
1138 event_loopexit(NULL);
1142 static void
1143 gotd_dispatch_notifier(int fd, short event, void *arg)
1145 struct gotd_imsgev *iev = arg;
1146 struct imsgbuf *ibuf = &iev->ibuf;
1147 struct gotd_child_proc *proc = gotd.notify_proc;
1148 ssize_t n;
1149 int shut = 0;
1150 struct imsg imsg;
1152 if (proc->iev.ibuf.fd != fd)
1153 fatalx("%s: unexpected fd %d", __func__, fd);
1155 if (event & EV_READ) {
1156 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1157 fatal("imsg_read error");
1158 if (n == 0) {
1159 /* Connection closed. */
1160 shut = 1;
1161 goto done;
1165 if (event & EV_WRITE) {
1166 n = msgbuf_write(&ibuf->w);
1167 if (n == -1 && errno != EAGAIN)
1168 fatal("msgbuf_write");
1169 if (n == 0) {
1170 /* Connection closed. */
1171 shut = 1;
1172 goto done;
1176 for (;;) {
1177 if ((n = imsg_get(ibuf, &imsg)) == -1)
1178 fatal("%s: imsg_get error", __func__);
1179 if (n == 0) /* No more messages. */
1180 break;
1182 switch (imsg.hdr.type) {
1183 default:
1184 log_debug("unexpected imsg %d", imsg.hdr.type);
1185 break;
1188 imsg_free(&imsg);
1190 done:
1191 if (!shut) {
1192 gotd_imsg_event_add(iev);
1193 } else {
1194 /* This pipe is dead. Remove its event handler */
1195 event_del(&iev->ev);
1198 * Do not exit all of gotd if the notification handler dies.
1199 * We can continue operating without notifications until an
1200 * operator intervenes.
1202 log_warnx("notify child process (pid %d) closed its imsg pipe "
1203 "unexpectedly", proc->pid);
1204 proc_done(proc);
1208 static void
1209 gotd_dispatch_auth_child(int fd, short event, void *arg)
1211 const struct got_error *err = NULL;
1212 struct gotd_imsgev *iev = arg;
1213 struct imsgbuf *ibuf = &iev->ibuf;
1214 struct gotd_client *client;
1215 struct gotd_repo *repo = NULL;
1216 ssize_t n;
1217 int shut = 0;
1218 struct imsg imsg;
1219 uint32_t client_id = 0;
1220 int do_disconnect = 0;
1221 size_t datalen;
1223 client = find_client_by_proc_fd(fd);
1224 if (client == NULL) {
1225 /* Can happen during process teardown. */
1226 warnx("cannot find client for fd %d", fd);
1227 shut = 1;
1228 goto done;
1231 if (client->auth == NULL)
1232 fatalx("cannot find auth child process for fd %d", fd);
1234 if (event & EV_READ) {
1235 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1236 fatal("imsg_read error");
1237 if (n == 0) {
1238 /* Connection closed. */
1239 shut = 1;
1240 goto done;
1244 if (event & EV_WRITE) {
1245 n = msgbuf_write(&ibuf->w);
1246 if (n == -1 && errno != EAGAIN)
1247 fatal("msgbuf_write");
1248 if (n == 0) {
1249 /* Connection closed. */
1250 shut = 1;
1252 goto done;
1255 if (client->auth->iev.ibuf.fd != fd)
1256 fatalx("%s: unexpected fd %d", __func__, fd);
1258 if ((n = imsg_get(ibuf, &imsg)) == -1)
1259 fatal("%s: imsg_get error", __func__);
1260 if (n == 0) /* No more messages. */
1261 return;
1263 evtimer_del(&client->tmo);
1265 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1267 switch (imsg.hdr.type) {
1268 case GOTD_IMSG_ERROR:
1269 do_disconnect = 1;
1270 err = gotd_imsg_recv_error(&client_id, &imsg);
1271 break;
1272 case GOTD_IMSG_ACCESS_GRANTED:
1273 if (client->state != GOTD_CLIENT_STATE_NEW) {
1274 do_disconnect = 1;
1275 err = got_error(GOT_ERR_PRIVSEP_MSG);
1277 break;
1278 default:
1279 do_disconnect = 1;
1280 log_debug("unexpected imsg %d", imsg.hdr.type);
1281 break;
1284 if (!verify_imsg_src(client, client->auth, &imsg)) {
1285 do_disconnect = 1;
1286 log_debug("dropping imsg type %d from PID %d",
1287 imsg.hdr.type, client->auth->pid);
1290 if (do_disconnect) {
1291 if (err)
1292 disconnect_on_error(client, err);
1293 else
1294 disconnect(client);
1295 imsg_free(&imsg);
1296 return;
1299 client->state = GOTD_CLIENT_STATE_ACCESS_GRANTED;
1300 if (datalen > 0)
1301 client->username = strndup(imsg.data, datalen);
1302 imsg_free(&imsg);
1303 if (client->username == NULL &&
1304 asprintf(&client->username, "uid %d", client->euid) == -1) {
1305 err = got_error_from_errno("asprintf");
1306 goto done;
1309 repo = gotd_find_repo_by_name(client->auth->repo_name, &gotd.repos);
1310 if (repo == NULL) {
1311 err = got_error(GOT_ERR_NOT_GIT_REPO);
1312 goto done;
1314 kill_auth_proc(client);
1316 log_info("authenticated %s for repository %s",
1317 client->username, repo->name);
1319 err = start_session_child(client, repo, gotd.argv0,
1320 gotd.confpath, gotd.daemonize, gotd.verbosity);
1321 if (err)
1322 goto done;
1323 done:
1324 if (err)
1325 log_warnx("uid %d: %s", client->euid, err->msg);
1327 /* We might have killed the auth process by now. */
1328 if (client->auth != NULL) {
1329 if (!shut) {
1330 gotd_imsg_event_add(iev);
1331 } else {
1332 /* This pipe is dead. Remove its event handler */
1333 event_del(&iev->ev);
1338 static const struct got_error *
1339 connect_session(struct gotd_client *client)
1341 const struct got_error *err = NULL;
1342 struct gotd_imsg_connect iconnect;
1343 int s;
1344 struct ibuf *wbuf;
1346 memset(&iconnect, 0, sizeof(iconnect));
1348 s = dup(client->fd);
1349 if (s == -1)
1350 return got_error_from_errno("dup");
1352 iconnect.client_id = client->id;
1353 iconnect.euid = client->euid;
1354 iconnect.egid = client->egid;
1355 iconnect.username_len = strlen(client->username);
1357 wbuf = imsg_create(&client->session->iev.ibuf, GOTD_IMSG_CONNECT,
1358 PROC_GOTD, gotd.pid, sizeof(iconnect) + iconnect.username_len);
1359 if (wbuf == NULL) {
1360 err = got_error_from_errno("imsg compose CONNECT");
1361 close(s);
1362 return err;
1364 if (imsg_add(wbuf, &iconnect, sizeof(iconnect)) == -1) {
1365 close(s);
1366 return got_error_from_errno("imsg_add CONNECT");
1368 if (imsg_add(wbuf, client->username, iconnect.username_len) == -1) {
1369 close(s);
1370 return got_error_from_errno("imsg_add CONNECT");
1373 ibuf_fd_set(wbuf, s);
1374 imsg_close(&client->session->iev.ibuf, wbuf);
1375 gotd_imsg_event_add(&client->session->iev);
1378 * We are no longer interested in messages from this client.
1379 * Further client requests will be handled by the session process.
1381 msgbuf_clear(&client->iev.ibuf.w);
1382 imsg_clear(&client->iev.ibuf);
1383 event_del(&client->iev.ev);
1384 client->fd = -1; /* will be closed via copy in client->iev.ibuf.fd */
1386 return NULL;
1389 static void
1390 gotd_dispatch_client_session(int fd, short event, void *arg)
1392 struct gotd_imsgev *iev = arg;
1393 struct imsgbuf *ibuf = &iev->ibuf;
1394 struct gotd_child_proc *proc = NULL;
1395 struct gotd_client *client = NULL;
1396 ssize_t n;
1397 int shut = 0;
1398 struct imsg imsg;
1400 client = find_client_by_proc_fd(fd);
1401 if (client == NULL) {
1402 /* Can happen during process teardown. */
1403 warnx("cannot find client for fd %d", fd);
1404 shut = 1;
1405 goto done;
1408 if (event & EV_READ) {
1409 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1410 fatal("imsg_read error");
1411 if (n == 0) {
1412 /* Connection closed. */
1413 shut = 1;
1414 goto done;
1418 if (event & EV_WRITE) {
1419 n = msgbuf_write(&ibuf->w);
1420 if (n == -1 && errno != EAGAIN)
1421 fatal("msgbuf_write");
1422 if (n == 0) {
1423 /* Connection closed. */
1424 shut = 1;
1425 goto done;
1429 proc = client->session;
1430 if (proc == NULL)
1431 fatalx("cannot find session child process for fd %d", fd);
1433 for (;;) {
1434 const struct got_error *err = NULL;
1435 uint32_t client_id = 0;
1436 int do_disconnect = 0, do_start_repo_child = 0;
1438 if ((n = imsg_get(ibuf, &imsg)) == -1)
1439 fatal("%s: imsg_get error", __func__);
1440 if (n == 0) /* No more messages. */
1441 break;
1443 switch (imsg.hdr.type) {
1444 case GOTD_IMSG_ERROR:
1445 do_disconnect = 1;
1446 err = gotd_imsg_recv_error(&client_id, &imsg);
1447 break;
1448 case GOTD_IMSG_CLIENT_SESSION_READY:
1449 if (client->state != GOTD_CLIENT_STATE_ACCESS_GRANTED) {
1450 err = got_error(GOT_ERR_PRIVSEP_MSG);
1451 break;
1453 do_start_repo_child = 1;
1454 break;
1455 case GOTD_IMSG_DISCONNECT:
1456 do_disconnect = 1;
1457 break;
1458 default:
1459 log_debug("unexpected imsg %d", imsg.hdr.type);
1460 break;
1463 if (!verify_imsg_src(client, proc, &imsg)) {
1464 log_debug("dropping imsg type %d from PID %d",
1465 imsg.hdr.type, proc->pid);
1466 imsg_free(&imsg);
1467 continue;
1469 if (err)
1470 log_warnx("uid %d: %s", client->euid, err->msg);
1472 if (do_start_repo_child) {
1473 struct gotd_repo *repo;
1474 const char *name = client->session->repo_name;
1476 repo = gotd_find_repo_by_name(name, &gotd.repos);
1477 if (repo != NULL) {
1478 enum gotd_procid proc_type;
1480 if (client->required_auth & GOTD_AUTH_WRITE)
1481 proc_type = PROC_REPO_WRITE;
1482 else
1483 proc_type = PROC_REPO_READ;
1485 err = start_repo_child(client, proc_type, repo,
1486 gotd.argv0, gotd.confpath, gotd.daemonize,
1487 gotd.verbosity);
1488 } else
1489 err = got_error(GOT_ERR_NOT_GIT_REPO);
1491 if (err) {
1492 log_warnx("uid %d: %s", client->euid, err->msg);
1493 do_disconnect = 1;
1497 if (do_disconnect) {
1498 if (err)
1499 disconnect_on_error(client, err);
1500 else
1501 disconnect(client);
1504 imsg_free(&imsg);
1506 done:
1507 if (!shut) {
1508 gotd_imsg_event_add(iev);
1509 } else {
1510 /* This pipe is dead. Remove its event handler */
1511 event_del(&iev->ev);
1512 disconnect(client);
1516 static const struct got_error *
1517 connect_notifier_and_session(struct gotd_client *client)
1519 const struct got_error *err = NULL;
1520 struct gotd_imsgev *session_iev = &client->session->iev;
1521 int pipe[2];
1522 int sock_flags = SOCK_STREAM|SOCK_NONBLOCK;
1524 if (gotd.notify_proc == NULL)
1525 return NULL;
1527 #ifdef SOCK_CLOEXEC
1528 sock_flags |= SOCK_CLOEXEC;
1529 #endif
1530 if (socketpair(AF_UNIX, sock_flags,
1531 PF_UNSPEC, pipe) == -1)
1532 return got_error_from_errno("socketpair");
1534 /* Pass notifier pipe to session . */
1535 if (gotd_imsg_compose_event(session_iev, GOTD_IMSG_CONNECT_NOTIFIER,
1536 PROC_GOTD, pipe[0], NULL, 0) == -1) {
1537 err = got_error_from_errno("imsg compose CONNECT_NOTIFIER");
1538 close(pipe[0]);
1539 close(pipe[1]);
1540 return err;
1543 /* Pass session pipe to notifier. */
1544 if (gotd_imsg_compose_event(&gotd.notify_proc->iev,
1545 GOTD_IMSG_CONNECT_SESSION, PROC_GOTD, pipe[1], NULL, 0) == -1) {
1546 err = got_error_from_errno("imsg compose CONNECT_SESSION");
1547 close(pipe[1]);
1548 return err;
1551 return NULL;
1554 static void
1555 gotd_dispatch_repo_child(int fd, short event, void *arg)
1557 struct gotd_imsgev *iev = arg;
1558 struct imsgbuf *ibuf = &iev->ibuf;
1559 struct gotd_child_proc *proc = NULL;
1560 struct gotd_client *client;
1561 ssize_t n;
1562 int shut = 0;
1563 struct imsg imsg;
1565 client = find_client_by_proc_fd(fd);
1566 if (client == NULL) {
1567 /* Can happen during process teardown. */
1568 warnx("cannot find client for fd %d", fd);
1569 shut = 1;
1570 goto done;
1573 if (event & EV_READ) {
1574 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1575 fatal("imsg_read error");
1576 if (n == 0) {
1577 /* Connection closed. */
1578 shut = 1;
1579 goto done;
1583 if (event & EV_WRITE) {
1584 n = msgbuf_write(&ibuf->w);
1585 if (n == -1 && errno != EAGAIN)
1586 fatal("msgbuf_write");
1587 if (n == 0) {
1588 /* Connection closed. */
1589 shut = 1;
1590 goto done;
1594 proc = client->repo;
1595 if (proc == NULL)
1596 fatalx("cannot find child process for fd %d", fd);
1598 for (;;) {
1599 const struct got_error *err = NULL;
1600 uint32_t client_id = 0;
1601 int do_disconnect = 0;
1603 if ((n = imsg_get(ibuf, &imsg)) == -1)
1604 fatal("%s: imsg_get error", __func__);
1605 if (n == 0) /* No more messages. */
1606 break;
1608 switch (imsg.hdr.type) {
1609 case GOTD_IMSG_ERROR:
1610 do_disconnect = 1;
1611 err = gotd_imsg_recv_error(&client_id, &imsg);
1612 break;
1613 case GOTD_IMSG_REPO_CHILD_READY:
1614 err = connect_session(client);
1615 if (err)
1616 break;
1617 err = connect_notifier_and_session(client);
1618 if (err)
1619 break;
1620 err = connect_repo_child(client, proc);
1621 break;
1622 default:
1623 log_debug("unexpected imsg %d", imsg.hdr.type);
1624 break;
1627 if (!verify_imsg_src(client, proc, &imsg)) {
1628 log_debug("dropping imsg type %d from PID %d",
1629 imsg.hdr.type, proc->pid);
1630 imsg_free(&imsg);
1631 continue;
1633 if (err)
1634 log_warnx("uid %d: %s", client->euid, err->msg);
1636 if (do_disconnect) {
1637 if (err)
1638 disconnect_on_error(client, err);
1639 else
1640 disconnect(client);
1643 imsg_free(&imsg);
1645 done:
1646 if (!shut) {
1647 gotd_imsg_event_add(iev);
1648 } else {
1649 /* This pipe is dead. Remove its event handler */
1650 event_del(&iev->ev);
1651 disconnect(client);
1655 static pid_t
1656 start_child(enum gotd_procid proc_id, const char *repo_path,
1657 char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
1659 char *argv[11];
1660 int argc = 0;
1661 pid_t pid;
1663 switch (pid = fork()) {
1664 case -1:
1665 fatal("cannot fork");
1666 case 0:
1667 break;
1668 default:
1669 close(fd);
1670 return pid;
1673 if (fd != GOTD_FILENO_MSG_PIPE) {
1674 if (dup2(fd, GOTD_FILENO_MSG_PIPE) == -1)
1675 fatal("cannot setup imsg fd");
1676 } else if (fcntl(fd, F_SETFD, 0) == -1)
1677 fatal("cannot setup imsg fd");
1679 argv[argc++] = argv0;
1680 switch (proc_id) {
1681 case PROC_LISTEN:
1682 argv[argc++] = (char *)"-L";
1683 break;
1684 case PROC_AUTH:
1685 argv[argc++] = (char *)"-A";
1686 break;
1687 case PROC_SESSION_READ:
1688 argv[argc++] = (char *)"-s";
1689 break;
1690 case PROC_SESSION_WRITE:
1691 argv[argc++] = (char *)"-S";
1692 break;
1693 case PROC_REPO_READ:
1694 argv[argc++] = (char *)"-R";
1695 break;
1696 case PROC_REPO_WRITE:
1697 argv[argc++] = (char *)"-W";
1698 break;
1699 case PROC_NOTIFY:
1700 argv[argc++] = (char *)"-N";
1701 break;
1702 default:
1703 fatalx("invalid process id %d", proc_id);
1706 argv[argc++] = (char *)"-f";
1707 argv[argc++] = (char *)confpath;
1709 if (repo_path) {
1710 argv[argc++] = (char *)"-P";
1711 argv[argc++] = (char *)repo_path;
1714 if (!daemonize)
1715 argv[argc++] = (char *)"-d";
1716 if (verbosity > 0)
1717 argv[argc++] = (char *)"-v";
1718 if (verbosity > 1)
1719 argv[argc++] = (char *)"-v";
1720 argv[argc++] = NULL;
1722 execvp(argv0, argv);
1723 fatal("execvp");
1726 static void
1727 start_listener(char *argv0, const char *confpath, int daemonize, int verbosity)
1729 struct gotd_child_proc *proc;
1730 int sock_flags = SOCK_STREAM|SOCK_NONBLOCK;
1732 #ifdef SOCK_CLOEXEC
1733 sock_flags |= SOCK_CLOEXEC;
1734 #endif
1736 proc = calloc(1, sizeof(*proc));
1737 if (proc == NULL)
1738 fatal("calloc");
1740 TAILQ_INSERT_HEAD(&procs, proc, entry);
1742 /* proc->tmo is initialized in main() after event_init() */
1744 proc->type = PROC_LISTEN;
1746 if (socketpair(AF_UNIX, sock_flags,
1747 PF_UNSPEC, proc->pipe) == -1)
1748 fatal("socketpair");
1750 proc->pid = start_child(proc->type, NULL, argv0, confpath,
1751 proc->pipe[1], daemonize, verbosity);
1752 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1753 proc->iev.handler = gotd_dispatch_listener;
1754 proc->iev.events = EV_READ;
1755 proc->iev.handler_arg = NULL;
1757 gotd.listen_proc = proc;
1760 static void
1761 start_notifier(char *argv0, const char *confpath, int daemonize, int verbosity)
1763 struct gotd_child_proc *proc;
1764 int sock_flags = SOCK_STREAM | SOCK_NONBLOCK;
1767 proc = calloc(1, sizeof(*proc));
1768 if (proc == NULL)
1769 fatal("calloc");
1771 TAILQ_INSERT_HEAD(&procs, proc, entry);
1773 /* proc->tmo is initialized in main() after event_init() */
1775 proc->type = PROC_NOTIFY;
1777 #ifdef SOCK_CLOEXEC
1778 sock_flags |= SOCK_CLOEXEC;
1779 #endif
1780 if (socketpair(AF_UNIX, sock_flags,
1781 PF_UNSPEC, proc->pipe) == -1)
1782 fatal("socketpair");
1784 proc->pid = start_child(proc->type, NULL, argv0, confpath,
1785 proc->pipe[1], daemonize, verbosity);
1786 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1787 proc->iev.handler = gotd_dispatch_notifier;
1788 proc->iev.events = EV_READ;
1789 proc->iev.handler_arg = NULL;
1790 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1791 gotd_dispatch_notifier, &proc->iev);
1793 gotd.notify_proc = proc;
1796 static const struct got_error *
1797 start_session_child(struct gotd_client *client, struct gotd_repo *repo,
1798 char *argv0, const char *confpath, int daemonize, int verbosity)
1800 struct gotd_child_proc *proc;
1801 int sock_flags = SOCK_STREAM | SOCK_NONBLOCK;
1803 #ifdef SOCK_CLOEXEC
1804 sock_flags |= SOCK_CLOEXEC;
1805 #endif
1807 proc = calloc(1, sizeof(*proc));
1808 if (proc == NULL)
1809 return got_error_from_errno("calloc");
1811 TAILQ_INSERT_HEAD(&procs, proc, entry);
1812 evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1814 if (client_is_reading(client))
1815 proc->type = PROC_SESSION_READ;
1816 else
1817 proc->type = PROC_SESSION_WRITE;
1818 if (strlcpy(proc->repo_name, repo->name,
1819 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1820 fatalx("repository name too long: %s", repo->name);
1821 log_debug("starting client uid %d session for repository %s",
1822 client->euid, repo->name);
1823 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1824 sizeof(proc->repo_path))
1825 fatalx("repository path too long: %s", repo->path);
1826 if (socketpair(AF_UNIX, sock_flags, PF_UNSPEC, proc->pipe) == -1)
1827 fatal("socketpair");
1828 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1829 confpath, proc->pipe[1], daemonize, verbosity);
1830 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1831 log_debug("proc %s %s is on fd %d",
1832 gotd_proc_names[proc->type], proc->repo_path,
1833 proc->pipe[0]);
1834 proc->iev.handler = gotd_dispatch_client_session;
1835 proc->iev.events = EV_READ;
1836 proc->iev.handler_arg = NULL;
1837 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1838 gotd_dispatch_client_session, &proc->iev);
1839 gotd_imsg_event_add(&proc->iev);
1841 client->session = proc;
1842 return NULL;
1845 static const struct got_error *
1846 start_repo_child(struct gotd_client *client, enum gotd_procid proc_type,
1847 struct gotd_repo *repo, char *argv0, const char *confpath,
1848 int daemonize, int verbosity)
1850 struct gotd_child_proc *proc;
1851 int sock_flags = SOCK_STREAM|SOCK_NONBLOCK;
1853 #ifdef SOCK_CLOEXEC
1854 sock_flags |= SOCK_CLOEXEC;
1855 #endif
1857 if (proc_type != PROC_REPO_READ && proc_type != PROC_REPO_WRITE)
1858 return got_error_msg(GOT_ERR_NOT_IMPL, "bad process type");
1860 proc = calloc(1, sizeof(*proc));
1861 if (proc == NULL)
1862 return got_error_from_errno("calloc");
1864 TAILQ_INSERT_HEAD(&procs, proc, entry);
1865 evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1867 proc->type = proc_type;
1868 if (strlcpy(proc->repo_name, repo->name,
1869 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1870 fatalx("repository name too long: %s", repo->name);
1871 log_debug("starting %s for repository %s",
1872 proc->type == PROC_REPO_READ ? "reader" : "writer", repo->name);
1874 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1875 sizeof(proc->repo_path))
1876 fatalx("repository path too long: %s", repo->path);
1877 if (realpath(repo->path, proc->repo_path) == NULL)
1878 fatal("%s", repo->path);
1879 if (socketpair(AF_UNIX, sock_flags,
1880 PF_UNSPEC, proc->pipe) == -1)
1881 fatal("socketpair");
1882 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1883 confpath, proc->pipe[1], daemonize, verbosity);
1884 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1885 log_debug("proc %s %s is on fd %d",
1886 gotd_proc_names[proc->type], proc->repo_path,
1887 proc->pipe[0]);
1888 proc->iev.handler = gotd_dispatch_repo_child;
1889 proc->iev.events = EV_READ;
1890 proc->iev.handler_arg = NULL;
1891 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1892 gotd_dispatch_repo_child, &proc->iev);
1893 gotd_imsg_event_add(&proc->iev);
1895 client->repo = proc;
1896 return NULL;
1899 static const struct got_error *
1900 start_auth_child(struct gotd_client *client, int required_auth,
1901 struct gotd_repo *repo, char *argv0, const char *confpath,
1902 int daemonize, int verbosity)
1904 const struct got_error *err = NULL;
1905 struct gotd_child_proc *proc;
1906 struct gotd_imsg_auth iauth;
1907 int fd;
1908 int sock_flags = SOCK_STREAM|SOCK_NONBLOCK;
1910 #ifdef SOCK_CLOEXEC
1911 sock_flags |= SOCK_CLOEXEC;
1912 #endif
1914 memset(&iauth, 0, sizeof(iauth));
1916 fd = dup(client->fd);
1917 if (fd == -1)
1918 return got_error_from_errno("dup");
1920 proc = calloc(1, sizeof(*proc));
1921 if (proc == NULL) {
1922 err = got_error_from_errno("calloc");
1923 close(fd);
1924 return err;
1927 TAILQ_INSERT_HEAD(&procs, proc, entry);
1928 evtimer_set(&proc->tmo, kill_proc_timeout, proc);
1930 proc->type = PROC_AUTH;
1931 if (strlcpy(proc->repo_name, repo->name,
1932 sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1933 fatalx("repository name too long: %s", repo->name);
1934 log_debug("starting auth for uid %d repository %s",
1935 client->euid, repo->name);
1936 if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1937 sizeof(proc->repo_path))
1938 fatalx("repository path too long: %s", repo->path);
1939 if (realpath(repo->path, proc->repo_path) == NULL)
1940 fatal("%s", repo->path);
1941 if (socketpair(AF_UNIX, sock_flags,
1942 PF_UNSPEC, proc->pipe) == -1)
1943 fatal("socketpair");
1944 proc->pid = start_child(proc->type, proc->repo_path, argv0,
1945 confpath, proc->pipe[1], daemonize, verbosity);
1946 imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1947 log_debug("proc %s %s is on fd %d",
1948 gotd_proc_names[proc->type], proc->repo_path,
1949 proc->pipe[0]);
1950 proc->iev.handler = gotd_dispatch_auth_child;
1951 proc->iev.events = EV_READ;
1952 proc->iev.handler_arg = NULL;
1953 event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1954 gotd_dispatch_auth_child, &proc->iev);
1955 gotd_imsg_event_add(&proc->iev);
1957 iauth.euid = client->euid;
1958 iauth.egid = client->egid;
1959 iauth.required_auth = required_auth;
1960 iauth.client_id = client->id;
1961 if (gotd_imsg_compose_event(&proc->iev, GOTD_IMSG_AUTHENTICATE,
1962 PROC_GOTD, fd, &iauth, sizeof(iauth)) == -1) {
1963 log_warn("imsg compose AUTHENTICATE");
1964 close(fd);
1965 /* Let the auth_timeout handler tidy up. */
1968 client->auth = proc;
1969 client->required_auth = required_auth;
1970 return NULL;
1973 static void
1974 apply_unveil_repo_readonly(const char *repo_path, int need_tmpdir)
1976 if (need_tmpdir) {
1977 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1978 fatal("unveil %s", GOT_TMPDIR_STR);
1981 if (unveil(repo_path, "r") == -1)
1982 fatal("unveil %s", repo_path);
1984 if (unveil(NULL, NULL) == -1)
1985 fatal("unveil");
1988 static void
1989 apply_unveil_repo_readwrite(const char *repo_path)
1991 if (unveil(repo_path, "rwc") == -1)
1992 fatal("unveil %s", repo_path);
1994 if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1995 fatal("unveil %s", GOT_TMPDIR_STR);
1997 if (unveil(NULL, NULL) == -1)
1998 fatal("unveil");
2001 static void
2002 apply_unveil_none(void)
2004 if (unveil("/", "") == -1)
2005 fatal("unveil");
2007 if (unveil(NULL, NULL) == -1)
2008 fatal("unveil");
2011 static void
2012 apply_unveil_selfexec(void)
2014 if (unveil(gotd.argv0, "x") == -1)
2015 fatal("unveil %s", gotd.argv0);
2017 if (unveil(NULL, NULL) == -1)
2018 fatal("unveil");
2021 static void
2022 set_max_datasize(void)
2024 struct rlimit rl;
2026 if (getrlimit(RLIMIT_DATA, &rl) != 0)
2027 return;
2029 rl.rlim_cur = rl.rlim_max;
2030 setrlimit(RLIMIT_DATA, &rl);
2033 static void
2034 unveil_notification_helpers(void)
2036 const char *helpers[] = {
2037 GOTD_PATH_PROG_NOTIFY_EMAIL,
2038 GOTD_PATH_PROG_NOTIFY_HTTP,
2040 size_t i;
2042 for (i = 0; i < nitems(helpers); i++) {
2043 if (unveil(helpers[i], "x") == 0)
2044 continue;
2045 fatal("unveil %s", helpers[i]);
2048 if (unveil(NULL, NULL) == -1)
2049 fatal("unveil");
2052 int
2053 main(int argc, char **argv)
2055 const struct got_error *error = NULL;
2056 int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
2057 const char *confpath = GOTD_CONF_PATH;
2058 char *argv0 = argv[0];
2059 char title[2048];
2060 struct passwd *pw = NULL;
2061 char *repo_path = NULL;
2062 enum gotd_procid proc_id = PROC_GOTD;
2063 struct event evsigint, evsigterm, evsighup, evsigusr1, evsigchld;
2064 int *pack_fds = NULL, *temp_fds = NULL;
2065 struct gotd_repo *repo = NULL;
2066 char *default_sender = NULL;
2067 char hostname[_POSIX_HOST_NAME_MAX + 1];
2068 FILE *diff_f1 = NULL, *diff_f2 = NULL;
2069 int diff_fd1 = -1, diff_fd2 = -1;
2071 TAILQ_INIT(&procs);
2073 log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
2075 while ((ch = getopt(argc, argv, "Adf:LnNP:RsSvW")) != -1) {
2076 switch (ch) {
2077 case 'A':
2078 proc_id = PROC_AUTH;
2079 break;
2080 case 'd':
2081 daemonize = 0;
2082 break;
2083 case 'f':
2084 confpath = optarg;
2085 break;
2086 case 'L':
2087 proc_id = PROC_LISTEN;
2088 break;
2089 case 'n':
2090 noaction = 1;
2091 break;
2092 case 'N':
2093 proc_id = PROC_NOTIFY;
2094 break;
2095 case 'P':
2096 repo_path = realpath(optarg, NULL);
2097 if (repo_path == NULL)
2098 fatal("realpath '%s'", optarg);
2099 break;
2100 case 'R':
2101 proc_id = PROC_REPO_READ;
2102 break;
2103 case 's':
2104 proc_id = PROC_SESSION_READ;
2105 break;
2106 case 'S':
2107 proc_id = PROC_SESSION_WRITE;
2108 break;
2109 case 'v':
2110 if (verbosity < 3)
2111 verbosity++;
2112 break;
2113 case 'W':
2114 proc_id = PROC_REPO_WRITE;
2115 break;
2116 default:
2117 usage();
2121 argc -= optind;
2122 argv += optind;
2124 if (argc != 0)
2125 usage();
2127 if (geteuid() && (proc_id == PROC_GOTD || proc_id == PROC_LISTEN))
2128 fatalx("need root privileges");
2130 if (parse_config(confpath, proc_id, &gotd) != 0)
2131 return 1;
2133 pw = getpwnam(gotd.user_name);
2134 if (pw == NULL)
2135 fatalx("user %s not found", gotd.user_name);
2137 if (pw->pw_uid == 0)
2138 fatalx("cannot run %s as the superuser", getprogname());
2140 if (noaction) {
2141 fprintf(stderr, "configuration OK\n");
2142 return 0;
2145 gotd.argv0 = argv0;
2146 gotd.daemonize = daemonize;
2147 gotd.verbosity = verbosity;
2148 gotd.confpath = confpath;
2150 /* Require an absolute path in argv[0] for reliable re-exec. */
2151 if (!got_path_is_absolute(argv0))
2152 fatalx("bad path \"%s\": must be an absolute path", argv0);
2154 log_init(daemonize ? 0 : 1, LOG_DAEMON);
2155 log_setverbose(verbosity);
2157 if (proc_id == PROC_GOTD) {
2158 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2159 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
2160 if (daemonize && daemon(1, 0) == -1)
2161 fatal("daemon");
2162 gotd.pid = getpid();
2163 start_listener(argv0, confpath, daemonize, verbosity);
2164 start_notifier(argv0, confpath, daemonize, verbosity);
2165 } else if (proc_id == PROC_LISTEN) {
2166 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2167 if (verbosity) {
2168 log_info("socket: %s", gotd.unix_socket_path);
2169 log_info("user: %s", pw->pw_name);
2172 fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
2173 pw->pw_gid);
2174 if (fd == -1) {
2175 fatal("cannot listen on unix socket %s",
2176 gotd.unix_socket_path);
2178 } else if (proc_id == PROC_AUTH) {
2179 snprintf(title, sizeof(title), "%s %s",
2180 gotd_proc_names[proc_id], repo_path);
2181 } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE ||
2182 proc_id == PROC_SESSION_READ || proc_id == PROC_SESSION_WRITE) {
2183 error = got_repo_pack_fds_open(&pack_fds);
2184 if (error != NULL)
2185 fatalx("cannot open pack tempfiles: %s", error->msg);
2186 error = got_repo_temp_fds_open(&temp_fds);
2187 if (error != NULL)
2188 fatalx("cannot open pack tempfiles: %s", error->msg);
2189 if (repo_path == NULL)
2190 fatalx("repository path not specified");
2191 snprintf(title, sizeof(title), "%s %s",
2192 gotd_proc_names[proc_id], repo_path);
2193 } else if (proc_id == PROC_NOTIFY) {
2194 snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
2195 if (gethostname(hostname, sizeof(hostname)) == -1)
2196 fatal("gethostname");
2197 if (asprintf(&default_sender, "%s@%s",
2198 pw->pw_name, hostname) == -1)
2199 fatal("asprintf");
2200 } else
2201 fatal("invalid process id %d", proc_id);
2203 setproctitle("%s", title);
2204 log_procinit(title);
2206 if (proc_id != PROC_GOTD && proc_id != PROC_LISTEN &&
2207 proc_id != PROC_REPO_READ && proc_id != PROC_REPO_WRITE) {
2208 /* Drop root privileges. */
2209 if (setgid(pw->pw_gid) == -1)
2210 fatal("setgid %d failed", pw->pw_gid);
2211 if (setuid(pw->pw_uid) == -1)
2212 fatal("setuid %d failed", pw->pw_uid);
2215 event_init();
2217 switch (proc_id) {
2218 case PROC_GOTD:
2219 #ifndef PROFILE
2220 /* "exec" promise will be limited to argv[0] via unveil(2). */
2221 if (pledge("stdio proc exec sendfd recvfd unveil", NULL) == -1)
2222 err(1, "pledge");
2223 #endif
2224 break;
2225 case PROC_LISTEN:
2226 #ifndef PROFILE
2227 if (pledge("stdio sendfd unix unveil", NULL) == -1)
2228 err(1, "pledge");
2229 #endif
2231 * Ensure that AF_UNIX bind(2) cannot be used with any other
2232 * sockets by revoking all filesystem access via unveil(2).
2234 apply_unveil_none();
2236 enter_chroot(GOTD_EMPTY_PATH);
2237 drop_privs(pw);
2239 listen_main(title, fd, gotd.connection_limits,
2240 gotd.nconnection_limits);
2241 /* NOTREACHED */
2242 break;
2243 case PROC_AUTH:
2244 #ifndef PROFILE
2245 if (pledge("stdio getpw recvfd unix unveil", NULL) == -1)
2246 err(1, "pledge");
2247 #endif
2249 * We need the "unix" pledge promise for getpeername(2) only.
2250 * Ensure that AF_UNIX bind(2) cannot be used by revoking all
2251 * filesystem access via unveil(2). Access to password database
2252 * files will still work since "getpw" bypasses unveil(2).
2254 apply_unveil_none();
2256 auth_main(title, &gotd.repos, repo_path);
2257 /* NOTREACHED */
2258 break;
2259 case PROC_SESSION_READ:
2260 case PROC_SESSION_WRITE:
2261 #ifndef PROFILE
2263 * The "recvfd" promise is only needed during setup and
2264 * will be removed in a later pledge(2) call.
2266 if (pledge("stdio rpath wpath cpath recvfd sendfd fattr flock "
2267 "unveil", NULL) == -1)
2268 err(1, "pledge");
2269 #endif
2270 if (proc_id == PROC_SESSION_READ)
2271 apply_unveil_repo_readonly(repo_path, 1);
2272 else {
2273 apply_unveil_repo_readwrite(repo_path);
2274 repo = gotd_find_repo_by_path(repo_path, &gotd);
2275 if (repo == NULL)
2276 fatalx("no repository for path %s", repo_path);
2278 session_main(title, repo_path, pack_fds, temp_fds,
2279 &gotd.request_timeout, repo, proc_id);
2280 /* NOTREACHED */
2281 break;
2282 case PROC_REPO_READ:
2283 set_max_datasize();
2284 #ifndef PROFILE
2285 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2286 err(1, "pledge");
2287 #endif
2288 apply_unveil_repo_readonly(repo_path, 0);
2290 if (enter_chroot(repo_path)) {
2291 log_info("change repo path %s", repo_path);
2292 free(repo_path);
2293 repo_path = strdup("/");
2294 if (repo_path == NULL)
2295 fatal("strdup");
2296 log_info("repo path is now %s", repo_path);
2298 drop_privs(pw);
2300 repo_read_main(title, repo_path, pack_fds, temp_fds);
2301 /* NOTREACHED */
2302 exit(0);
2303 case PROC_REPO_WRITE:
2304 set_max_datasize();
2306 diff_f1 = got_opentemp();
2307 if (diff_f1 == NULL)
2308 fatal("got_opentemp");
2309 diff_f2 = got_opentemp();
2310 if (diff_f2 == NULL)
2311 fatal("got_opentemp");
2312 diff_fd1 = got_opentempfd();
2313 if (diff_fd1 == -1)
2314 fatal("got_opentempfd");
2315 diff_fd2 = got_opentempfd();
2316 if (diff_fd2 == -1)
2317 fatal("got_opentempfd");
2318 #ifndef PROFILE
2319 if (pledge("stdio rpath recvfd unveil", NULL) == -1)
2320 err(1, "pledge");
2321 #endif
2322 apply_unveil_repo_readonly(repo_path, 0);
2323 repo = gotd_find_repo_by_path(repo_path, &gotd);
2324 if (repo == NULL)
2325 fatalx("no repository for path %s", repo_path);
2327 if (enter_chroot(repo_path)) {
2328 free(repo_path);
2329 repo_path = strdup("/");
2330 if (repo_path == NULL)
2331 fatal("strdup");
2333 drop_privs(pw);
2335 repo_write_main(title, repo_path, pack_fds, temp_fds,
2336 diff_f1, diff_f2, diff_fd1, diff_fd2,
2337 &repo->protected_tag_namespaces,
2338 &repo->protected_branch_namespaces,
2339 &repo->protected_branches);
2340 /* NOTREACHED */
2341 exit(0);
2342 case PROC_NOTIFY:
2343 #ifndef PROFILE
2344 if (pledge("stdio proc exec recvfd unveil", NULL) == -1)
2345 err(1, "pledge");
2346 #endif
2348 * Limit "exec" promise to notification helpers via unveil(2).
2350 unveil_notification_helpers();
2352 notify_main(title, &gotd.repos, default_sender);
2353 /* NOTREACHED */
2354 exit(0);
2355 default:
2356 fatal("invalid process id %d", proc_id);
2359 if (proc_id != PROC_GOTD)
2360 fatal("invalid process id %d", proc_id);
2362 evtimer_set(&gotd.listen_proc->tmo, kill_proc_timeout,
2363 gotd.listen_proc);
2364 if (gotd.notify_proc) {
2365 evtimer_set(&gotd.notify_proc->tmo, kill_proc_timeout,
2366 gotd.notify_proc);
2369 apply_unveil_selfexec();
2371 signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
2372 signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
2373 signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
2374 signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
2375 signal_set(&evsigchld, SIGCHLD, gotd_sighdlr, NULL);
2376 signal(SIGPIPE, SIG_IGN);
2378 signal_add(&evsigint, NULL);
2379 signal_add(&evsigterm, NULL);
2380 signal_add(&evsighup, NULL);
2381 signal_add(&evsigusr1, NULL);
2382 signal_add(&evsigchld, NULL);
2384 gotd_imsg_event_add(&gotd.listen_proc->iev);
2385 if (gotd.notify_proc)
2386 gotd_imsg_event_add(&gotd.notify_proc->iev);
2388 event_dispatch();
2390 free(repo_path);
2391 free(default_sender);
2392 gotd_shutdown();
2394 return 0;