Blame


1 13b2bc37 2022-10-23 stsp /*
2 13b2bc37 2022-10-23 stsp * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 13b2bc37 2022-10-23 stsp *
4 13b2bc37 2022-10-23 stsp * Permission to use, copy, modify, and distribute this software for any
5 13b2bc37 2022-10-23 stsp * purpose with or without fee is hereby granted, provided that the above
6 13b2bc37 2022-10-23 stsp * copyright notice and this permission notice appear in all copies.
7 13b2bc37 2022-10-23 stsp *
8 13b2bc37 2022-10-23 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 13b2bc37 2022-10-23 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 13b2bc37 2022-10-23 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 13b2bc37 2022-10-23 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 13b2bc37 2022-10-23 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 13b2bc37 2022-10-23 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 13b2bc37 2022-10-23 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 13b2bc37 2022-10-23 stsp */
16 13b2bc37 2022-10-23 stsp
17 13b2bc37 2022-10-23 stsp #include <sys/queue.h>
18 13b2bc37 2022-10-23 stsp #include <sys/tree.h>
19 13b2bc37 2022-10-23 stsp #include <sys/time.h>
20 13b2bc37 2022-10-23 stsp #include <sys/types.h>
21 13b2bc37 2022-10-23 stsp #include <sys/stat.h>
22 13b2bc37 2022-10-23 stsp #include <sys/socket.h>
23 13b2bc37 2022-10-23 stsp #include <sys/un.h>
24 13b2bc37 2022-10-23 stsp #include <sys/wait.h>
25 13b2bc37 2022-10-23 stsp
26 13b2bc37 2022-10-23 stsp #include <fcntl.h>
27 13b2bc37 2022-10-23 stsp #include <err.h>
28 13b2bc37 2022-10-23 stsp #include <errno.h>
29 13b2bc37 2022-10-23 stsp #include <event.h>
30 13b2bc37 2022-10-23 stsp #include <limits.h>
31 13b2bc37 2022-10-23 stsp #include <pwd.h>
32 13b2bc37 2022-10-23 stsp #include <imsg.h>
33 13b2bc37 2022-10-23 stsp #include <sha1.h>
34 13b2bc37 2022-10-23 stsp #include <signal.h>
35 13b2bc37 2022-10-23 stsp #include <siphash.h>
36 13b2bc37 2022-10-23 stsp #include <stdarg.h>
37 13b2bc37 2022-10-23 stsp #include <stdio.h>
38 13b2bc37 2022-10-23 stsp #include <stdlib.h>
39 13b2bc37 2022-10-23 stsp #include <string.h>
40 13b2bc37 2022-10-23 stsp #include <syslog.h>
41 13b2bc37 2022-10-23 stsp #include <unistd.h>
42 13b2bc37 2022-10-23 stsp
43 13b2bc37 2022-10-23 stsp #include "got_error.h"
44 13b2bc37 2022-10-23 stsp #include "got_opentemp.h"
45 13b2bc37 2022-10-23 stsp #include "got_path.h"
46 13b2bc37 2022-10-23 stsp #include "got_repository.h"
47 13b2bc37 2022-10-23 stsp #include "got_object.h"
48 13b2bc37 2022-10-23 stsp #include "got_reference.h"
49 13b2bc37 2022-10-23 stsp
50 13b2bc37 2022-10-23 stsp #include "got_lib_delta.h"
51 13b2bc37 2022-10-23 stsp #include "got_lib_object.h"
52 13b2bc37 2022-10-23 stsp #include "got_lib_object_cache.h"
53 13b2bc37 2022-10-23 stsp #include "got_lib_sha1.h"
54 13b2bc37 2022-10-23 stsp #include "got_lib_gitproto.h"
55 13b2bc37 2022-10-23 stsp #include "got_lib_pack.h"
56 13b2bc37 2022-10-23 stsp #include "got_lib_repository.h"
57 13b2bc37 2022-10-23 stsp
58 13b2bc37 2022-10-23 stsp #include "gotd.h"
59 13b2bc37 2022-10-23 stsp #include "log.h"
60 d93ecf7d 2022-12-14 stsp #include "listen.h"
61 0ccf3acb 2022-11-16 stsp #include "auth.h"
62 ae7c1b78 2023-01-10 stsp #include "session.h"
63 13b2bc37 2022-10-23 stsp #include "repo_read.h"
64 13b2bc37 2022-10-23 stsp #include "repo_write.h"
65 13b2bc37 2022-10-23 stsp
66 13b2bc37 2022-10-23 stsp #ifndef nitems
67 13b2bc37 2022-10-23 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
68 13b2bc37 2022-10-23 stsp #endif
69 13b2bc37 2022-10-23 stsp
70 13b2bc37 2022-10-23 stsp struct gotd_client {
71 13b2bc37 2022-10-23 stsp STAILQ_ENTRY(gotd_client) entry;
72 13b2bc37 2022-10-23 stsp enum gotd_client_state state;
73 13b2bc37 2022-10-23 stsp uint32_t id;
74 13b2bc37 2022-10-23 stsp int fd;
75 13b2bc37 2022-10-23 stsp struct gotd_imsgev iev;
76 13b2bc37 2022-10-23 stsp struct event tmo;
77 13b2bc37 2022-10-23 stsp uid_t euid;
78 13b2bc37 2022-10-23 stsp gid_t egid;
79 13b2bc37 2022-10-23 stsp struct gotd_child_proc *repo_read;
80 13b2bc37 2022-10-23 stsp struct gotd_child_proc *repo_write;
81 5e25db14 2022-12-29 stsp struct gotd_child_proc *auth;
82 ae7c1b78 2023-01-10 stsp struct gotd_child_proc *session;
83 5e25db14 2022-12-29 stsp int required_auth;
84 13b2bc37 2022-10-23 stsp };
85 13b2bc37 2022-10-23 stsp STAILQ_HEAD(gotd_clients, gotd_client);
86 13b2bc37 2022-10-23 stsp
87 13b2bc37 2022-10-23 stsp static struct gotd_clients gotd_clients[GOTD_CLIENT_TABLE_SIZE];
88 13b2bc37 2022-10-23 stsp static SIPHASH_KEY clients_hash_key;
89 13b2bc37 2022-10-23 stsp volatile int client_cnt;
90 ef4e2f01 2022-12-29 stsp static struct timeval auth_timeout = { 5, 0 };
91 13b2bc37 2022-10-23 stsp static struct gotd gotd;
92 13b2bc37 2022-10-23 stsp
93 13b2bc37 2022-10-23 stsp void gotd_sighdlr(int sig, short event, void *arg);
94 f1752522 2022-10-29 stsp static void gotd_shutdown(void);
95 ae7c1b78 2023-01-10 stsp static const struct got_error *start_session_child(struct gotd_client *,
96 ae7c1b78 2023-01-10 stsp struct gotd_repo *, char *, const char *, int, int);
97 b50a2b46 2022-12-29 stsp static const struct got_error *start_repo_child(struct gotd_client *,
98 b50a2b46 2022-12-29 stsp enum gotd_procid, struct gotd_repo *, char *, const char *, int, int);
99 5e25db14 2022-12-29 stsp static const struct got_error *start_auth_child(struct gotd_client *, int,
100 5e25db14 2022-12-29 stsp struct gotd_repo *, char *, const char *, int, int);
101 b50a2b46 2022-12-29 stsp static void kill_proc(struct gotd_child_proc *, int);
102 13b2bc37 2022-10-23 stsp
103 13b2bc37 2022-10-23 stsp __dead static void
104 13b2bc37 2022-10-23 stsp usage()
105 13b2bc37 2022-10-23 stsp {
106 88dec179 2022-10-24 stsp fprintf(stderr, "usage: %s [-dv] [-f config-file]\n", getprogname());
107 88dec179 2022-10-24 stsp exit(1);
108 13b2bc37 2022-10-23 stsp }
109 13b2bc37 2022-10-23 stsp
110 13b2bc37 2022-10-23 stsp static int
111 13b2bc37 2022-10-23 stsp unix_socket_listen(const char *unix_socket_path, uid_t uid, gid_t gid)
112 13b2bc37 2022-10-23 stsp {
113 13b2bc37 2022-10-23 stsp struct sockaddr_un sun;
114 13b2bc37 2022-10-23 stsp int fd = -1;
115 13b2bc37 2022-10-23 stsp mode_t old_umask, mode;
116 13b2bc37 2022-10-23 stsp
117 13b2bc37 2022-10-23 stsp fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK| SOCK_CLOEXEC, 0);
118 13b2bc37 2022-10-23 stsp if (fd == -1) {
119 13b2bc37 2022-10-23 stsp log_warn("socket");
120 13b2bc37 2022-10-23 stsp return -1;
121 13b2bc37 2022-10-23 stsp }
122 13b2bc37 2022-10-23 stsp
123 13b2bc37 2022-10-23 stsp sun.sun_family = AF_UNIX;
124 13b2bc37 2022-10-23 stsp if (strlcpy(sun.sun_path, unix_socket_path,
125 13b2bc37 2022-10-23 stsp sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) {
126 13b2bc37 2022-10-23 stsp log_warnx("%s: name too long", unix_socket_path);
127 13b2bc37 2022-10-23 stsp close(fd);
128 13b2bc37 2022-10-23 stsp return -1;
129 13b2bc37 2022-10-23 stsp }
130 13b2bc37 2022-10-23 stsp
131 13b2bc37 2022-10-23 stsp if (unlink(unix_socket_path) == -1) {
132 13b2bc37 2022-10-23 stsp if (errno != ENOENT) {
133 13b2bc37 2022-10-23 stsp log_warn("unlink %s", unix_socket_path);
134 13b2bc37 2022-10-23 stsp close(fd);
135 13b2bc37 2022-10-23 stsp return -1;
136 13b2bc37 2022-10-23 stsp }
137 13b2bc37 2022-10-23 stsp }
138 13b2bc37 2022-10-23 stsp
139 13b2bc37 2022-10-23 stsp old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
140 6f854dde 2023-01-04 stsp mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
141 13b2bc37 2022-10-23 stsp
142 13b2bc37 2022-10-23 stsp if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
143 13b2bc37 2022-10-23 stsp log_warn("bind: %s", unix_socket_path);
144 13b2bc37 2022-10-23 stsp close(fd);
145 13b2bc37 2022-10-23 stsp umask(old_umask);
146 13b2bc37 2022-10-23 stsp return -1;
147 13b2bc37 2022-10-23 stsp }
148 13b2bc37 2022-10-23 stsp
149 13b2bc37 2022-10-23 stsp umask(old_umask);
150 13b2bc37 2022-10-23 stsp
151 13b2bc37 2022-10-23 stsp if (chmod(unix_socket_path, mode) == -1) {
152 13b2bc37 2022-10-23 stsp log_warn("chmod %o %s", mode, unix_socket_path);
153 13b2bc37 2022-10-23 stsp close(fd);
154 13b2bc37 2022-10-23 stsp unlink(unix_socket_path);
155 13b2bc37 2022-10-23 stsp return -1;
156 13b2bc37 2022-10-23 stsp }
157 13b2bc37 2022-10-23 stsp
158 13b2bc37 2022-10-23 stsp if (chown(unix_socket_path, uid, gid) == -1) {
159 13b2bc37 2022-10-23 stsp log_warn("chown %s uid=%d gid=%d", unix_socket_path, uid, gid);
160 13b2bc37 2022-10-23 stsp close(fd);
161 13b2bc37 2022-10-23 stsp unlink(unix_socket_path);
162 13b2bc37 2022-10-23 stsp return -1;
163 13b2bc37 2022-10-23 stsp }
164 13b2bc37 2022-10-23 stsp
165 13b2bc37 2022-10-23 stsp if (listen(fd, GOTD_UNIX_SOCKET_BACKLOG) == -1) {
166 13b2bc37 2022-10-23 stsp log_warn("listen");
167 13b2bc37 2022-10-23 stsp close(fd);
168 13b2bc37 2022-10-23 stsp unlink(unix_socket_path);
169 13b2bc37 2022-10-23 stsp return -1;
170 13b2bc37 2022-10-23 stsp }
171 13b2bc37 2022-10-23 stsp
172 13b2bc37 2022-10-23 stsp return fd;
173 13b2bc37 2022-10-23 stsp }
174 13b2bc37 2022-10-23 stsp
175 13b2bc37 2022-10-23 stsp static uint64_t
176 13b2bc37 2022-10-23 stsp client_hash(uint32_t client_id)
177 13b2bc37 2022-10-23 stsp {
178 13b2bc37 2022-10-23 stsp return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
179 13b2bc37 2022-10-23 stsp }
180 13b2bc37 2022-10-23 stsp
181 13b2bc37 2022-10-23 stsp static void
182 13b2bc37 2022-10-23 stsp add_client(struct gotd_client *client)
183 13b2bc37 2022-10-23 stsp {
184 13b2bc37 2022-10-23 stsp uint64_t slot = client_hash(client->id) % nitems(gotd_clients);
185 13b2bc37 2022-10-23 stsp STAILQ_INSERT_HEAD(&gotd_clients[slot], client, entry);
186 13b2bc37 2022-10-23 stsp client_cnt++;
187 13b2bc37 2022-10-23 stsp }
188 13b2bc37 2022-10-23 stsp
189 13b2bc37 2022-10-23 stsp static struct gotd_client *
190 13b2bc37 2022-10-23 stsp find_client(uint32_t client_id)
191 13b2bc37 2022-10-23 stsp {
192 13b2bc37 2022-10-23 stsp uint64_t slot;
193 13b2bc37 2022-10-23 stsp struct gotd_client *c;
194 13b2bc37 2022-10-23 stsp
195 13b2bc37 2022-10-23 stsp slot = client_hash(client_id) % nitems(gotd_clients);
196 13b2bc37 2022-10-23 stsp STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
197 13b2bc37 2022-10-23 stsp if (c->id == client_id)
198 13b2bc37 2022-10-23 stsp return c;
199 13b2bc37 2022-10-23 stsp }
200 13b2bc37 2022-10-23 stsp
201 13b2bc37 2022-10-23 stsp return NULL;
202 13b2bc37 2022-10-23 stsp }
203 13b2bc37 2022-10-23 stsp
204 13b2bc37 2022-10-23 stsp static struct gotd_child_proc *
205 ae7c1b78 2023-01-10 stsp get_client_repo_proc(struct gotd_client *client)
206 13b2bc37 2022-10-23 stsp {
207 13b2bc37 2022-10-23 stsp if (client->repo_read && client->repo_write) {
208 13b2bc37 2022-10-23 stsp fatalx("uid %d is reading and writing in the same session",
209 13b2bc37 2022-10-23 stsp client->euid);
210 13b2bc37 2022-10-23 stsp /* NOTREACHED */
211 13b2bc37 2022-10-23 stsp }
212 13b2bc37 2022-10-23 stsp
213 13b2bc37 2022-10-23 stsp if (client->repo_read)
214 13b2bc37 2022-10-23 stsp return client->repo_read;
215 13b2bc37 2022-10-23 stsp else if (client->repo_write)
216 13b2bc37 2022-10-23 stsp return client->repo_write;
217 b50a2b46 2022-12-29 stsp
218 b50a2b46 2022-12-29 stsp return NULL;
219 b50a2b46 2022-12-29 stsp }
220 b50a2b46 2022-12-29 stsp
221 b50a2b46 2022-12-29 stsp static struct gotd_client *
222 b50a2b46 2022-12-29 stsp find_client_by_proc_fd(int fd)
223 b50a2b46 2022-12-29 stsp {
224 b50a2b46 2022-12-29 stsp uint64_t slot;
225 b50a2b46 2022-12-29 stsp
226 b50a2b46 2022-12-29 stsp for (slot = 0; slot < nitems(gotd_clients); slot++) {
227 b50a2b46 2022-12-29 stsp struct gotd_client *c;
228 b50a2b46 2022-12-29 stsp
229 b50a2b46 2022-12-29 stsp STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
230 ae7c1b78 2023-01-10 stsp struct gotd_child_proc *proc = get_client_repo_proc(c);
231 b50a2b46 2022-12-29 stsp if (proc && proc->iev.ibuf.fd == fd)
232 b50a2b46 2022-12-29 stsp return c;
233 5e25db14 2022-12-29 stsp if (c->auth && c->auth->iev.ibuf.fd == fd)
234 ae7c1b78 2023-01-10 stsp return c;
235 ae7c1b78 2023-01-10 stsp if (c->session && c->session->iev.ibuf.fd == fd)
236 5e25db14 2022-12-29 stsp return c;
237 b50a2b46 2022-12-29 stsp }
238 b50a2b46 2022-12-29 stsp }
239 f1752522 2022-10-29 stsp
240 13b2bc37 2022-10-23 stsp return NULL;
241 13b2bc37 2022-10-23 stsp }
242 13b2bc37 2022-10-23 stsp
243 13b2bc37 2022-10-23 stsp static int
244 13b2bc37 2022-10-23 stsp client_is_reading(struct gotd_client *client)
245 13b2bc37 2022-10-23 stsp {
246 13b2bc37 2022-10-23 stsp return client->repo_read != NULL;
247 13b2bc37 2022-10-23 stsp }
248 13b2bc37 2022-10-23 stsp
249 13b2bc37 2022-10-23 stsp static int
250 13b2bc37 2022-10-23 stsp client_is_writing(struct gotd_client *client)
251 13b2bc37 2022-10-23 stsp {
252 13b2bc37 2022-10-23 stsp return client->repo_write != NULL;
253 13b2bc37 2022-10-23 stsp }
254 13b2bc37 2022-10-23 stsp
255 13b2bc37 2022-10-23 stsp static const struct got_error *
256 13b2bc37 2022-10-23 stsp ensure_client_is_not_writing(struct gotd_client *client)
257 13b2bc37 2022-10-23 stsp {
258 13b2bc37 2022-10-23 stsp if (client_is_writing(client)) {
259 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_BAD_PACKET,
260 13b2bc37 2022-10-23 stsp "uid %d made a read-request but is writing to "
261 13b2bc37 2022-10-23 stsp "a repository", client->euid);
262 13b2bc37 2022-10-23 stsp }
263 13b2bc37 2022-10-23 stsp
264 13b2bc37 2022-10-23 stsp return NULL;
265 13b2bc37 2022-10-23 stsp }
266 13b2bc37 2022-10-23 stsp
267 13b2bc37 2022-10-23 stsp static const struct got_error *
268 13b2bc37 2022-10-23 stsp ensure_client_is_not_reading(struct gotd_client *client)
269 13b2bc37 2022-10-23 stsp {
270 13b2bc37 2022-10-23 stsp if (client_is_reading(client)) {
271 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_BAD_PACKET,
272 13b2bc37 2022-10-23 stsp "uid %d made a write-request but is reading from "
273 13b2bc37 2022-10-23 stsp "a repository", client->euid);
274 13b2bc37 2022-10-23 stsp }
275 13b2bc37 2022-10-23 stsp
276 13b2bc37 2022-10-23 stsp return NULL;
277 b50a2b46 2022-12-29 stsp }
278 b50a2b46 2022-12-29 stsp
279 b50a2b46 2022-12-29 stsp static void
280 5e25db14 2022-12-29 stsp wait_for_child(pid_t child_pid)
281 b50a2b46 2022-12-29 stsp {
282 b50a2b46 2022-12-29 stsp pid_t pid;
283 b50a2b46 2022-12-29 stsp int status;
284 b50a2b46 2022-12-29 stsp
285 5e25db14 2022-12-29 stsp log_debug("waiting for child PID %ld to terminate",
286 5e25db14 2022-12-29 stsp (long)child_pid);
287 b50a2b46 2022-12-29 stsp
288 b50a2b46 2022-12-29 stsp do {
289 5e25db14 2022-12-29 stsp pid = waitpid(child_pid, &status, WNOHANG);
290 b50a2b46 2022-12-29 stsp if (pid == -1) {
291 b50a2b46 2022-12-29 stsp if (errno != EINTR && errno != ECHILD)
292 b50a2b46 2022-12-29 stsp fatal("wait");
293 b50a2b46 2022-12-29 stsp } else if (WIFSIGNALED(status)) {
294 b50a2b46 2022-12-29 stsp log_warnx("child PID %ld terminated; signal %d",
295 b50a2b46 2022-12-29 stsp (long)pid, WTERMSIG(status));
296 7fdc3e58 2022-12-30 mark }
297 b50a2b46 2022-12-29 stsp } while (pid != -1 || (pid == -1 && errno == EINTR));
298 ae7c1b78 2023-01-10 stsp }
299 ae7c1b78 2023-01-10 stsp
300 ae7c1b78 2023-01-10 stsp static void
301 ae7c1b78 2023-01-10 stsp proc_done(struct gotd_child_proc *proc)
302 ae7c1b78 2023-01-10 stsp {
303 ae7c1b78 2023-01-10 stsp event_del(&proc->iev.ev);
304 ae7c1b78 2023-01-10 stsp msgbuf_clear(&proc->iev.ibuf.w);
305 ae7c1b78 2023-01-10 stsp close(proc->iev.ibuf.fd);
306 ae7c1b78 2023-01-10 stsp kill_proc(proc, 0);
307 ae7c1b78 2023-01-10 stsp wait_for_child(proc->pid);
308 ae7c1b78 2023-01-10 stsp free(proc);
309 13b2bc37 2022-10-23 stsp }
310 13b2bc37 2022-10-23 stsp
311 13b2bc37 2022-10-23 stsp static void
312 5e25db14 2022-12-29 stsp kill_auth_proc(struct gotd_client *client)
313 5e25db14 2022-12-29 stsp {
314 5e25db14 2022-12-29 stsp struct gotd_child_proc *proc;
315 5e25db14 2022-12-29 stsp
316 5e25db14 2022-12-29 stsp if (client->auth == NULL)
317 5e25db14 2022-12-29 stsp return;
318 5e25db14 2022-12-29 stsp
319 5e25db14 2022-12-29 stsp proc = client->auth;
320 5e25db14 2022-12-29 stsp client->auth = NULL;
321 5e25db14 2022-12-29 stsp
322 ae7c1b78 2023-01-10 stsp proc_done(proc);
323 5e25db14 2022-12-29 stsp }
324 5e25db14 2022-12-29 stsp
325 5e25db14 2022-12-29 stsp static void
326 ae7c1b78 2023-01-10 stsp kill_session_proc(struct gotd_client *client)
327 ae7c1b78 2023-01-10 stsp {
328 ae7c1b78 2023-01-10 stsp struct gotd_child_proc *proc;
329 ae7c1b78 2023-01-10 stsp
330 ae7c1b78 2023-01-10 stsp if (client->session == NULL)
331 ae7c1b78 2023-01-10 stsp return;
332 ae7c1b78 2023-01-10 stsp
333 ae7c1b78 2023-01-10 stsp proc = client->session;
334 ae7c1b78 2023-01-10 stsp client->session = NULL;
335 ae7c1b78 2023-01-10 stsp
336 ae7c1b78 2023-01-10 stsp proc_done(proc);
337 ae7c1b78 2023-01-10 stsp }
338 ae7c1b78 2023-01-10 stsp
339 ae7c1b78 2023-01-10 stsp static void
340 13b2bc37 2022-10-23 stsp disconnect(struct gotd_client *client)
341 13b2bc37 2022-10-23 stsp {
342 13b2bc37 2022-10-23 stsp struct gotd_imsg_disconnect idisconnect;
343 ae7c1b78 2023-01-10 stsp struct gotd_child_proc *proc = get_client_repo_proc(client);
344 b50a2b46 2022-12-29 stsp struct gotd_child_proc *listen_proc = &gotd.listen_proc;
345 13b2bc37 2022-10-23 stsp uint64_t slot;
346 13b2bc37 2022-10-23 stsp
347 13b2bc37 2022-10-23 stsp log_debug("uid %d: disconnecting", client->euid);
348 5e25db14 2022-12-29 stsp
349 5e25db14 2022-12-29 stsp kill_auth_proc(client);
350 ae7c1b78 2023-01-10 stsp kill_session_proc(client);
351 13b2bc37 2022-10-23 stsp
352 13b2bc37 2022-10-23 stsp idisconnect.client_id = client->id;
353 f1752522 2022-10-29 stsp if (proc) {
354 f1752522 2022-10-29 stsp if (gotd_imsg_compose_event(&proc->iev,
355 f1752522 2022-10-29 stsp GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
356 f1752522 2022-10-29 stsp &idisconnect, sizeof(idisconnect)) == -1)
357 f1752522 2022-10-29 stsp log_warn("imsg compose DISCONNECT");
358 b50a2b46 2022-12-29 stsp
359 b50a2b46 2022-12-29 stsp msgbuf_clear(&proc->iev.ibuf.w);
360 b50a2b46 2022-12-29 stsp close(proc->iev.ibuf.fd);
361 b50a2b46 2022-12-29 stsp kill_proc(proc, 0);
362 5e25db14 2022-12-29 stsp wait_for_child(proc->pid);
363 b50a2b46 2022-12-29 stsp free(proc);
364 b50a2b46 2022-12-29 stsp proc = NULL;
365 f1752522 2022-10-29 stsp }
366 d93ecf7d 2022-12-14 stsp
367 d93ecf7d 2022-12-14 stsp if (gotd_imsg_compose_event(&listen_proc->iev,
368 d93ecf7d 2022-12-14 stsp GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
369 d93ecf7d 2022-12-14 stsp &idisconnect, sizeof(idisconnect)) == -1)
370 d93ecf7d 2022-12-14 stsp log_warn("imsg compose DISCONNECT");
371 d93ecf7d 2022-12-14 stsp
372 13b2bc37 2022-10-23 stsp slot = client_hash(client->id) % nitems(gotd_clients);
373 13b2bc37 2022-10-23 stsp STAILQ_REMOVE(&gotd_clients[slot], client, gotd_client, entry);
374 13b2bc37 2022-10-23 stsp imsg_clear(&client->iev.ibuf);
375 13b2bc37 2022-10-23 stsp event_del(&client->iev.ev);
376 13b2bc37 2022-10-23 stsp evtimer_del(&client->tmo);
377 ae7c1b78 2023-01-10 stsp if (client->fd != -1)
378 ae7c1b78 2023-01-10 stsp close(client->fd);
379 ae7c1b78 2023-01-10 stsp else if (client->iev.ibuf.fd != -1)
380 ae7c1b78 2023-01-10 stsp close(client->iev.ibuf.fd);
381 13b2bc37 2022-10-23 stsp free(client);
382 13b2bc37 2022-10-23 stsp client_cnt--;
383 13b2bc37 2022-10-23 stsp }
384 13b2bc37 2022-10-23 stsp
385 13b2bc37 2022-10-23 stsp static void
386 13b2bc37 2022-10-23 stsp disconnect_on_error(struct gotd_client *client, const struct got_error *err)
387 13b2bc37 2022-10-23 stsp {
388 13b2bc37 2022-10-23 stsp struct imsgbuf ibuf;
389 13b2bc37 2022-10-23 stsp
390 13b2bc37 2022-10-23 stsp log_warnx("uid %d: %s", client->euid, err->msg);
391 ae7c1b78 2023-01-10 stsp if (err->code != GOT_ERR_EOF && client->fd != -1) {
392 13b2bc37 2022-10-23 stsp imsg_init(&ibuf, client->fd);
393 13b2bc37 2022-10-23 stsp gotd_imsg_send_error(&ibuf, 0, PROC_GOTD, err);
394 13b2bc37 2022-10-23 stsp imsg_clear(&ibuf);
395 13b2bc37 2022-10-23 stsp }
396 13b2bc37 2022-10-23 stsp disconnect(client);
397 f1752522 2022-10-29 stsp }
398 f1752522 2022-10-29 stsp
399 f1752522 2022-10-29 stsp static const struct got_error *
400 f1752522 2022-10-29 stsp send_repo_info(struct gotd_imsgev *iev, struct gotd_repo *repo)
401 f1752522 2022-10-29 stsp {
402 f1752522 2022-10-29 stsp const struct got_error *err = NULL;
403 f1752522 2022-10-29 stsp struct gotd_imsg_info_repo irepo;
404 f1752522 2022-10-29 stsp
405 f1752522 2022-10-29 stsp memset(&irepo, 0, sizeof(irepo));
406 f1752522 2022-10-29 stsp
407 f1752522 2022-10-29 stsp if (strlcpy(irepo.repo_name, repo->name, sizeof(irepo.repo_name))
408 f1752522 2022-10-29 stsp >= sizeof(irepo.repo_name))
409 f1752522 2022-10-29 stsp return got_error_msg(GOT_ERR_NO_SPACE, "repo name too long");
410 f1752522 2022-10-29 stsp if (strlcpy(irepo.repo_path, repo->path, sizeof(irepo.repo_path))
411 f1752522 2022-10-29 stsp >= sizeof(irepo.repo_path))
412 f1752522 2022-10-29 stsp return got_error_msg(GOT_ERR_NO_SPACE, "repo path too long");
413 f1752522 2022-10-29 stsp
414 f1752522 2022-10-29 stsp if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_REPO, PROC_GOTD, -1,
415 f1752522 2022-10-29 stsp &irepo, sizeof(irepo)) == -1) {
416 f1752522 2022-10-29 stsp err = got_error_from_errno("imsg compose INFO_REPO");
417 f1752522 2022-10-29 stsp if (err)
418 f1752522 2022-10-29 stsp return err;
419 f1752522 2022-10-29 stsp }
420 f1752522 2022-10-29 stsp
421 f1752522 2022-10-29 stsp return NULL;
422 f1752522 2022-10-29 stsp }
423 f1752522 2022-10-29 stsp
424 f1752522 2022-10-29 stsp static const struct got_error *
425 f1752522 2022-10-29 stsp send_client_info(struct gotd_imsgev *iev, struct gotd_client *client)
426 f1752522 2022-10-29 stsp {
427 f1752522 2022-10-29 stsp const struct got_error *err = NULL;
428 f1752522 2022-10-29 stsp struct gotd_imsg_info_client iclient;
429 f1752522 2022-10-29 stsp struct gotd_child_proc *proc;
430 f1752522 2022-10-29 stsp
431 f1752522 2022-10-29 stsp memset(&iclient, 0, sizeof(iclient));
432 f1752522 2022-10-29 stsp iclient.euid = client->euid;
433 f1752522 2022-10-29 stsp iclient.egid = client->egid;
434 f1752522 2022-10-29 stsp
435 ae7c1b78 2023-01-10 stsp proc = get_client_repo_proc(client);
436 f1752522 2022-10-29 stsp if (proc) {
437 eec68231 2022-12-14 stsp if (strlcpy(iclient.repo_name, proc->repo_path,
438 f1752522 2022-10-29 stsp sizeof(iclient.repo_name)) >= sizeof(iclient.repo_name)) {
439 f1752522 2022-10-29 stsp return got_error_msg(GOT_ERR_NO_SPACE,
440 f1752522 2022-10-29 stsp "repo name too long");
441 f1752522 2022-10-29 stsp }
442 f1752522 2022-10-29 stsp if (client_is_writing(client))
443 f1752522 2022-10-29 stsp iclient.is_writing = 1;
444 ae7c1b78 2023-01-10 stsp
445 ae7c1b78 2023-01-10 stsp iclient.repo_child_pid = proc->pid;
446 f1752522 2022-10-29 stsp }
447 f1752522 2022-10-29 stsp
448 f1752522 2022-10-29 stsp iclient.state = client->state;
449 ae7c1b78 2023-01-10 stsp if (client->session)
450 ae7c1b78 2023-01-10 stsp iclient.session_child_pid = client->session->pid;
451 f1752522 2022-10-29 stsp
452 f1752522 2022-10-29 stsp if (gotd_imsg_compose_event(iev, GOTD_IMSG_INFO_CLIENT, PROC_GOTD, -1,
453 f1752522 2022-10-29 stsp &iclient, sizeof(iclient)) == -1) {
454 f1752522 2022-10-29 stsp err = got_error_from_errno("imsg compose INFO_CLIENT");
455 f1752522 2022-10-29 stsp if (err)
456 f1752522 2022-10-29 stsp return err;
457 f1752522 2022-10-29 stsp }
458 f1752522 2022-10-29 stsp
459 f1752522 2022-10-29 stsp return NULL;
460 f1752522 2022-10-29 stsp }
461 f1752522 2022-10-29 stsp
462 f1752522 2022-10-29 stsp static const struct got_error *
463 f1752522 2022-10-29 stsp send_info(struct gotd_client *client)
464 f1752522 2022-10-29 stsp {
465 f1752522 2022-10-29 stsp const struct got_error *err = NULL;
466 f1752522 2022-10-29 stsp struct gotd_imsg_info info;
467 f1752522 2022-10-29 stsp uint64_t slot;
468 f1752522 2022-10-29 stsp struct gotd_repo *repo;
469 f1752522 2022-10-29 stsp
470 78433331 2023-01-04 stsp if (client->euid != 0)
471 78433331 2023-01-04 stsp return got_error_set_errno(EPERM, "info");
472 78433331 2023-01-04 stsp
473 f1752522 2022-10-29 stsp info.pid = gotd.pid;
474 f1752522 2022-10-29 stsp info.verbosity = gotd.verbosity;
475 f1752522 2022-10-29 stsp info.nrepos = gotd.nrepos;
476 f1752522 2022-10-29 stsp info.nclients = client_cnt - 1;
477 f1752522 2022-10-29 stsp
478 f1752522 2022-10-29 stsp if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_INFO, PROC_GOTD, -1,
479 f1752522 2022-10-29 stsp &info, sizeof(info)) == -1) {
480 f1752522 2022-10-29 stsp err = got_error_from_errno("imsg compose INFO");
481 f1752522 2022-10-29 stsp if (err)
482 f1752522 2022-10-29 stsp return err;
483 f1752522 2022-10-29 stsp }
484 f1752522 2022-10-29 stsp
485 f1752522 2022-10-29 stsp TAILQ_FOREACH(repo, &gotd.repos, entry) {
486 f1752522 2022-10-29 stsp err = send_repo_info(&client->iev, repo);
487 f1752522 2022-10-29 stsp if (err)
488 f1752522 2022-10-29 stsp return err;
489 f1752522 2022-10-29 stsp }
490 f1752522 2022-10-29 stsp
491 f1752522 2022-10-29 stsp for (slot = 0; slot < nitems(gotd_clients); slot++) {
492 f1752522 2022-10-29 stsp struct gotd_client *c;
493 f1752522 2022-10-29 stsp STAILQ_FOREACH(c, &gotd_clients[slot], entry) {
494 f1752522 2022-10-29 stsp if (c->id == client->id)
495 f1752522 2022-10-29 stsp continue;
496 f1752522 2022-10-29 stsp err = send_client_info(&client->iev, c);
497 f1752522 2022-10-29 stsp if (err)
498 f1752522 2022-10-29 stsp return err;
499 f1752522 2022-10-29 stsp }
500 f1752522 2022-10-29 stsp }
501 f1752522 2022-10-29 stsp
502 f1752522 2022-10-29 stsp return NULL;
503 f1752522 2022-10-29 stsp }
504 f1752522 2022-10-29 stsp
505 f1752522 2022-10-29 stsp static const struct got_error *
506 f1752522 2022-10-29 stsp stop_gotd(struct gotd_client *client)
507 f1752522 2022-10-29 stsp {
508 f1752522 2022-10-29 stsp
509 f1752522 2022-10-29 stsp if (client->euid != 0)
510 f1752522 2022-10-29 stsp return got_error_set_errno(EPERM, "stop");
511 f1752522 2022-10-29 stsp
512 f1752522 2022-10-29 stsp gotd_shutdown();
513 f1752522 2022-10-29 stsp /* NOTREACHED */
514 0ccf3acb 2022-11-16 stsp return NULL;
515 0ccf3acb 2022-11-16 stsp }
516 0ccf3acb 2022-11-16 stsp
517 0ccf3acb 2022-11-16 stsp static struct gotd_repo *
518 0ccf3acb 2022-11-16 stsp find_repo_by_name(const char *repo_name)
519 0ccf3acb 2022-11-16 stsp {
520 0ccf3acb 2022-11-16 stsp struct gotd_repo *repo;
521 0ccf3acb 2022-11-16 stsp size_t namelen;
522 0ccf3acb 2022-11-16 stsp
523 0ccf3acb 2022-11-16 stsp TAILQ_FOREACH(repo, &gotd.repos, entry) {
524 0ccf3acb 2022-11-16 stsp namelen = strlen(repo->name);
525 0ccf3acb 2022-11-16 stsp if (strncmp(repo->name, repo_name, namelen) != 0)
526 0ccf3acb 2022-11-16 stsp continue;
527 0ccf3acb 2022-11-16 stsp if (repo_name[namelen] == '\0' ||
528 0ccf3acb 2022-11-16 stsp strcmp(&repo_name[namelen], ".git") == 0)
529 0ccf3acb 2022-11-16 stsp return repo;
530 13b2bc37 2022-10-23 stsp }
531 13b2bc37 2022-10-23 stsp
532 13b2bc37 2022-10-23 stsp return NULL;
533 13b2bc37 2022-10-23 stsp }
534 13b2bc37 2022-10-23 stsp
535 13b2bc37 2022-10-23 stsp static const struct got_error *
536 ae7c1b78 2023-01-10 stsp start_client_authentication(struct gotd_client *client, struct imsg *imsg)
537 13b2bc37 2022-10-23 stsp {
538 13b2bc37 2022-10-23 stsp const struct got_error *err;
539 13b2bc37 2022-10-23 stsp struct gotd_imsg_list_refs ireq;
540 0ccf3acb 2022-11-16 stsp struct gotd_repo *repo = NULL;
541 13b2bc37 2022-10-23 stsp size_t datalen;
542 13b2bc37 2022-10-23 stsp
543 13b2bc37 2022-10-23 stsp log_debug("list-refs request from uid %d", client->euid);
544 13b2bc37 2022-10-23 stsp
545 ae7c1b78 2023-01-10 stsp if (client->state != GOTD_STATE_EXPECT_LIST_REFS)
546 ae7c1b78 2023-01-10 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
547 ae7c1b78 2023-01-10 stsp "unexpected list-refs request received");
548 ae7c1b78 2023-01-10 stsp
549 13b2bc37 2022-10-23 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
550 13b2bc37 2022-10-23 stsp if (datalen != sizeof(ireq))
551 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
552 13b2bc37 2022-10-23 stsp
553 13b2bc37 2022-10-23 stsp memcpy(&ireq, imsg->data, datalen);
554 13b2bc37 2022-10-23 stsp
555 13b2bc37 2022-10-23 stsp if (ireq.client_is_reading) {
556 13b2bc37 2022-10-23 stsp err = ensure_client_is_not_writing(client);
557 13b2bc37 2022-10-23 stsp if (err)
558 13b2bc37 2022-10-23 stsp return err;
559 0ccf3acb 2022-11-16 stsp repo = find_repo_by_name(ireq.repo_name);
560 0ccf3acb 2022-11-16 stsp if (repo == NULL)
561 0ccf3acb 2022-11-16 stsp return got_error(GOT_ERR_NOT_GIT_REPO);
562 5e25db14 2022-12-29 stsp err = start_auth_child(client, GOTD_AUTH_READ, repo,
563 b50a2b46 2022-12-29 stsp gotd.argv0, gotd.confpath, gotd.daemonize,
564 b50a2b46 2022-12-29 stsp gotd.verbosity);
565 b50a2b46 2022-12-29 stsp if (err)
566 b50a2b46 2022-12-29 stsp return err;
567 13b2bc37 2022-10-23 stsp } else {
568 13b2bc37 2022-10-23 stsp err = ensure_client_is_not_reading(client);
569 0ccf3acb 2022-11-16 stsp if (err)
570 0ccf3acb 2022-11-16 stsp return err;
571 0ccf3acb 2022-11-16 stsp repo = find_repo_by_name(ireq.repo_name);
572 0ccf3acb 2022-11-16 stsp if (repo == NULL)
573 0ccf3acb 2022-11-16 stsp return got_error(GOT_ERR_NOT_GIT_REPO);
574 5e25db14 2022-12-29 stsp err = start_auth_child(client,
575 5e25db14 2022-12-29 stsp GOTD_AUTH_READ | GOTD_AUTH_WRITE,
576 5e25db14 2022-12-29 stsp repo, gotd.argv0, gotd.confpath, gotd.daemonize,
577 b50a2b46 2022-12-29 stsp gotd.verbosity);
578 b50a2b46 2022-12-29 stsp if (err)
579 b50a2b46 2022-12-29 stsp return err;
580 13b2bc37 2022-10-23 stsp }
581 13b2bc37 2022-10-23 stsp
582 ae7c1b78 2023-01-10 stsp evtimer_add(&client->tmo, &auth_timeout);
583 13b2bc37 2022-10-23 stsp
584 ae7c1b78 2023-01-10 stsp /* Flow continues upon authentication successs/failure or timeout. */
585 13b2bc37 2022-10-23 stsp return NULL;
586 13b2bc37 2022-10-23 stsp }
587 13b2bc37 2022-10-23 stsp
588 13b2bc37 2022-10-23 stsp static void
589 13b2bc37 2022-10-23 stsp gotd_request(int fd, short events, void *arg)
590 13b2bc37 2022-10-23 stsp {
591 13b2bc37 2022-10-23 stsp struct gotd_imsgev *iev = arg;
592 13b2bc37 2022-10-23 stsp struct imsgbuf *ibuf = &iev->ibuf;
593 13b2bc37 2022-10-23 stsp struct gotd_client *client = iev->handler_arg;
594 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
595 13b2bc37 2022-10-23 stsp struct imsg imsg;
596 13b2bc37 2022-10-23 stsp ssize_t n;
597 13b2bc37 2022-10-23 stsp
598 13b2bc37 2022-10-23 stsp if (events & EV_WRITE) {
599 13b2bc37 2022-10-23 stsp while (ibuf->w.queued) {
600 13b2bc37 2022-10-23 stsp n = msgbuf_write(&ibuf->w);
601 13b2bc37 2022-10-23 stsp if (n == -1 && errno == EPIPE) {
602 13b2bc37 2022-10-23 stsp /*
603 13b2bc37 2022-10-23 stsp * The client has closed its socket.
604 13b2bc37 2022-10-23 stsp * This can happen when Git clients are
605 13b2bc37 2022-10-23 stsp * done sending pack file data.
606 77d0cae1 2022-12-30 op */
607 13b2bc37 2022-10-23 stsp msgbuf_clear(&ibuf->w);
608 13b2bc37 2022-10-23 stsp continue;
609 13b2bc37 2022-10-23 stsp } else if (n == -1 && errno != EAGAIN) {
610 13b2bc37 2022-10-23 stsp err = got_error_from_errno("imsg_flush");
611 13b2bc37 2022-10-23 stsp disconnect_on_error(client, err);
612 13b2bc37 2022-10-23 stsp return;
613 13b2bc37 2022-10-23 stsp }
614 13b2bc37 2022-10-23 stsp if (n == 0) {
615 13b2bc37 2022-10-23 stsp /* Connection closed. */
616 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_EOF);
617 13b2bc37 2022-10-23 stsp disconnect_on_error(client, err);
618 13b2bc37 2022-10-23 stsp return;
619 13b2bc37 2022-10-23 stsp }
620 13b2bc37 2022-10-23 stsp }
621 f1752522 2022-10-29 stsp
622 f1752522 2022-10-29 stsp /* Disconnect gotctl(8) now that messages have been sent. */
623 f1752522 2022-10-29 stsp if (!client_is_reading(client) && !client_is_writing(client)) {
624 f1752522 2022-10-29 stsp disconnect(client);
625 f1752522 2022-10-29 stsp return;
626 f1752522 2022-10-29 stsp }
627 13b2bc37 2022-10-23 stsp }
628 13b2bc37 2022-10-23 stsp
629 13b2bc37 2022-10-23 stsp if ((events & EV_READ) == 0)
630 13b2bc37 2022-10-23 stsp return;
631 13b2bc37 2022-10-23 stsp
632 13b2bc37 2022-10-23 stsp memset(&imsg, 0, sizeof(imsg));
633 13b2bc37 2022-10-23 stsp
634 13b2bc37 2022-10-23 stsp while (err == NULL) {
635 13b2bc37 2022-10-23 stsp err = gotd_imsg_recv(&imsg, ibuf, 0);
636 13b2bc37 2022-10-23 stsp if (err) {
637 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_PRIVSEP_READ)
638 13b2bc37 2022-10-23 stsp err = NULL;
639 13b2bc37 2022-10-23 stsp break;
640 13b2bc37 2022-10-23 stsp }
641 13b2bc37 2022-10-23 stsp
642 13b2bc37 2022-10-23 stsp evtimer_del(&client->tmo);
643 13b2bc37 2022-10-23 stsp
644 13b2bc37 2022-10-23 stsp switch (imsg.hdr.type) {
645 f1752522 2022-10-29 stsp case GOTD_IMSG_INFO:
646 f1752522 2022-10-29 stsp err = send_info(client);
647 f1752522 2022-10-29 stsp break;
648 f1752522 2022-10-29 stsp case GOTD_IMSG_STOP:
649 f1752522 2022-10-29 stsp err = stop_gotd(client);
650 f1752522 2022-10-29 stsp break;
651 13b2bc37 2022-10-23 stsp case GOTD_IMSG_LIST_REFS:
652 ae7c1b78 2023-01-10 stsp err = start_client_authentication(client, &imsg);
653 13b2bc37 2022-10-23 stsp break;
654 13b2bc37 2022-10-23 stsp default:
655 ae7c1b78 2023-01-10 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
656 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
657 13b2bc37 2022-10-23 stsp break;
658 13b2bc37 2022-10-23 stsp }
659 13b2bc37 2022-10-23 stsp
660 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
661 13b2bc37 2022-10-23 stsp }
662 13b2bc37 2022-10-23 stsp
663 13b2bc37 2022-10-23 stsp if (err) {
664 13b2bc37 2022-10-23 stsp if (err->code != GOT_ERR_EOF ||
665 13b2bc37 2022-10-23 stsp client->state != GOTD_STATE_EXPECT_PACKFILE)
666 13b2bc37 2022-10-23 stsp disconnect_on_error(client, err);
667 13b2bc37 2022-10-23 stsp } else {
668 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(&client->iev);
669 13b2bc37 2022-10-23 stsp }
670 13b2bc37 2022-10-23 stsp }
671 13b2bc37 2022-10-23 stsp
672 13b2bc37 2022-10-23 stsp static void
673 ae7c1b78 2023-01-10 stsp gotd_auth_timeout(int fd, short events, void *arg)
674 13b2bc37 2022-10-23 stsp {
675 13b2bc37 2022-10-23 stsp struct gotd_client *client = arg;
676 13b2bc37 2022-10-23 stsp
677 ae7c1b78 2023-01-10 stsp log_debug("disconnecting uid %d due to authentication timeout",
678 ae7c1b78 2023-01-10 stsp client->euid);
679 13b2bc37 2022-10-23 stsp disconnect(client);
680 13b2bc37 2022-10-23 stsp }
681 13b2bc37 2022-10-23 stsp
682 d93ecf7d 2022-12-14 stsp static const struct got_error *
683 d93ecf7d 2022-12-14 stsp recv_connect(uint32_t *client_id, struct imsg *imsg)
684 13b2bc37 2022-10-23 stsp {
685 d93ecf7d 2022-12-14 stsp const struct got_error *err = NULL;
686 d93ecf7d 2022-12-14 stsp struct gotd_imsg_connect iconnect;
687 d93ecf7d 2022-12-14 stsp size_t datalen;
688 13b2bc37 2022-10-23 stsp int s = -1;
689 13b2bc37 2022-10-23 stsp struct gotd_client *client = NULL;
690 13b2bc37 2022-10-23 stsp
691 d93ecf7d 2022-12-14 stsp *client_id = 0;
692 13b2bc37 2022-10-23 stsp
693 d93ecf7d 2022-12-14 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
694 d93ecf7d 2022-12-14 stsp if (datalen != sizeof(iconnect))
695 d93ecf7d 2022-12-14 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
696 d93ecf7d 2022-12-14 stsp memcpy(&iconnect, imsg->data, sizeof(iconnect));
697 13b2bc37 2022-10-23 stsp
698 d93ecf7d 2022-12-14 stsp s = imsg->fd;
699 13b2bc37 2022-10-23 stsp if (s == -1) {
700 d93ecf7d 2022-12-14 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
701 d93ecf7d 2022-12-14 stsp goto done;
702 13b2bc37 2022-10-23 stsp }
703 13b2bc37 2022-10-23 stsp
704 d93ecf7d 2022-12-14 stsp if (find_client(iconnect.client_id)) {
705 d93ecf7d 2022-12-14 stsp err = got_error_msg(GOT_ERR_CLIENT_ID, "duplicate client ID");
706 d93ecf7d 2022-12-14 stsp goto done;
707 d93ecf7d 2022-12-14 stsp }
708 13b2bc37 2022-10-23 stsp
709 13b2bc37 2022-10-23 stsp client = calloc(1, sizeof(*client));
710 13b2bc37 2022-10-23 stsp if (client == NULL) {
711 d93ecf7d 2022-12-14 stsp err = got_error_from_errno("calloc");
712 d93ecf7d 2022-12-14 stsp goto done;
713 13b2bc37 2022-10-23 stsp }
714 13b2bc37 2022-10-23 stsp
715 d93ecf7d 2022-12-14 stsp *client_id = iconnect.client_id;
716 d93ecf7d 2022-12-14 stsp
717 13b2bc37 2022-10-23 stsp client->state = GOTD_STATE_EXPECT_LIST_REFS;
718 d93ecf7d 2022-12-14 stsp client->id = iconnect.client_id;
719 13b2bc37 2022-10-23 stsp client->fd = s;
720 13b2bc37 2022-10-23 stsp s = -1;
721 365cf0f3 2022-12-29 stsp /* The auth process will verify UID/GID for us. */
722 365cf0f3 2022-12-29 stsp client->euid = iconnect.euid;
723 365cf0f3 2022-12-29 stsp client->egid = iconnect.egid;
724 13b2bc37 2022-10-23 stsp
725 13b2bc37 2022-10-23 stsp imsg_init(&client->iev.ibuf, client->fd);
726 13b2bc37 2022-10-23 stsp client->iev.handler = gotd_request;
727 13b2bc37 2022-10-23 stsp client->iev.events = EV_READ;
728 13b2bc37 2022-10-23 stsp client->iev.handler_arg = client;
729 13b2bc37 2022-10-23 stsp
730 13b2bc37 2022-10-23 stsp event_set(&client->iev.ev, client->fd, EV_READ, gotd_request,
731 13b2bc37 2022-10-23 stsp &client->iev);
732 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(&client->iev);
733 13b2bc37 2022-10-23 stsp
734 ae7c1b78 2023-01-10 stsp evtimer_set(&client->tmo, gotd_auth_timeout, client);
735 13b2bc37 2022-10-23 stsp
736 13b2bc37 2022-10-23 stsp add_client(client);
737 13b2bc37 2022-10-23 stsp log_debug("%s: new client uid %d connected on fd %d", __func__,
738 13b2bc37 2022-10-23 stsp client->euid, client->fd);
739 d93ecf7d 2022-12-14 stsp done:
740 d93ecf7d 2022-12-14 stsp if (err) {
741 b50a2b46 2022-12-29 stsp struct gotd_child_proc *listen_proc = &gotd.listen_proc;
742 d93ecf7d 2022-12-14 stsp struct gotd_imsg_disconnect idisconnect;
743 13b2bc37 2022-10-23 stsp
744 d93ecf7d 2022-12-14 stsp idisconnect.client_id = client->id;
745 d93ecf7d 2022-12-14 stsp if (gotd_imsg_compose_event(&listen_proc->iev,
746 d93ecf7d 2022-12-14 stsp GOTD_IMSG_DISCONNECT, PROC_GOTD, -1,
747 d93ecf7d 2022-12-14 stsp &idisconnect, sizeof(idisconnect)) == -1)
748 d93ecf7d 2022-12-14 stsp log_warn("imsg compose DISCONNECT");
749 d93ecf7d 2022-12-14 stsp
750 d93ecf7d 2022-12-14 stsp if (s != -1)
751 d93ecf7d 2022-12-14 stsp close(s);
752 d93ecf7d 2022-12-14 stsp }
753 d93ecf7d 2022-12-14 stsp
754 d93ecf7d 2022-12-14 stsp return err;
755 13b2bc37 2022-10-23 stsp }
756 13b2bc37 2022-10-23 stsp
757 13b2bc37 2022-10-23 stsp static const char *gotd_proc_names[PROC_MAX] = {
758 13b2bc37 2022-10-23 stsp "parent",
759 d93ecf7d 2022-12-14 stsp "listen",
760 5e25db14 2022-12-29 stsp "auth",
761 ae7c1b78 2023-01-10 stsp "session",
762 13b2bc37 2022-10-23 stsp "repo_read",
763 13b2bc37 2022-10-23 stsp "repo_write"
764 13b2bc37 2022-10-23 stsp };
765 13b2bc37 2022-10-23 stsp
766 13b2bc37 2022-10-23 stsp static void
767 13b2bc37 2022-10-23 stsp kill_proc(struct gotd_child_proc *proc, int fatal)
768 13b2bc37 2022-10-23 stsp {
769 13b2bc37 2022-10-23 stsp if (fatal) {
770 13b2bc37 2022-10-23 stsp log_warnx("sending SIGKILL to PID %d", proc->pid);
771 13b2bc37 2022-10-23 stsp kill(proc->pid, SIGKILL);
772 13b2bc37 2022-10-23 stsp } else
773 13b2bc37 2022-10-23 stsp kill(proc->pid, SIGTERM);
774 13b2bc37 2022-10-23 stsp }
775 13b2bc37 2022-10-23 stsp
776 13b2bc37 2022-10-23 stsp static void
777 13b2bc37 2022-10-23 stsp gotd_shutdown(void)
778 13b2bc37 2022-10-23 stsp {
779 13b2bc37 2022-10-23 stsp struct gotd_child_proc *proc;
780 b50a2b46 2022-12-29 stsp uint64_t slot;
781 13b2bc37 2022-10-23 stsp
782 ae7c1b78 2023-01-10 stsp log_debug("shutting down");
783 b50a2b46 2022-12-29 stsp for (slot = 0; slot < nitems(gotd_clients); slot++) {
784 b50a2b46 2022-12-29 stsp struct gotd_client *c, *tmp;
785 b50a2b46 2022-12-29 stsp
786 b50a2b46 2022-12-29 stsp STAILQ_FOREACH_SAFE(c, &gotd_clients[slot], entry, tmp)
787 b50a2b46 2022-12-29 stsp disconnect(c);
788 13b2bc37 2022-10-23 stsp }
789 13b2bc37 2022-10-23 stsp
790 b50a2b46 2022-12-29 stsp proc = &gotd.listen_proc;
791 b50a2b46 2022-12-29 stsp msgbuf_clear(&proc->iev.ibuf.w);
792 b50a2b46 2022-12-29 stsp close(proc->iev.ibuf.fd);
793 b50a2b46 2022-12-29 stsp kill_proc(proc, 0);
794 5e25db14 2022-12-29 stsp wait_for_child(proc->pid);
795 13b2bc37 2022-10-23 stsp
796 13b2bc37 2022-10-23 stsp log_info("terminating");
797 13b2bc37 2022-10-23 stsp exit(0);
798 13b2bc37 2022-10-23 stsp }
799 13b2bc37 2022-10-23 stsp
800 13b2bc37 2022-10-23 stsp void
801 13b2bc37 2022-10-23 stsp gotd_sighdlr(int sig, short event, void *arg)
802 13b2bc37 2022-10-23 stsp {
803 13b2bc37 2022-10-23 stsp /*
804 13b2bc37 2022-10-23 stsp * Normal signal handler rules don't apply because libevent
805 13b2bc37 2022-10-23 stsp * decouples for us.
806 13b2bc37 2022-10-23 stsp */
807 13b2bc37 2022-10-23 stsp
808 13b2bc37 2022-10-23 stsp switch (sig) {
809 13b2bc37 2022-10-23 stsp case SIGHUP:
810 13b2bc37 2022-10-23 stsp log_info("%s: ignoring SIGHUP", __func__);
811 13b2bc37 2022-10-23 stsp break;
812 13b2bc37 2022-10-23 stsp case SIGUSR1:
813 13b2bc37 2022-10-23 stsp log_info("%s: ignoring SIGUSR1", __func__);
814 13b2bc37 2022-10-23 stsp break;
815 13b2bc37 2022-10-23 stsp case SIGTERM:
816 13b2bc37 2022-10-23 stsp case SIGINT:
817 13b2bc37 2022-10-23 stsp gotd_shutdown();
818 13b2bc37 2022-10-23 stsp break;
819 13b2bc37 2022-10-23 stsp default:
820 13b2bc37 2022-10-23 stsp fatalx("unexpected signal");
821 13b2bc37 2022-10-23 stsp }
822 13b2bc37 2022-10-23 stsp }
823 13b2bc37 2022-10-23 stsp
824 13b2bc37 2022-10-23 stsp static const struct got_error *
825 13b2bc37 2022-10-23 stsp ensure_proc_is_reading(struct gotd_client *client,
826 13b2bc37 2022-10-23 stsp struct gotd_child_proc *proc)
827 13b2bc37 2022-10-23 stsp {
828 13b2bc37 2022-10-23 stsp if (!client_is_reading(client)) {
829 13b2bc37 2022-10-23 stsp kill_proc(proc, 1);
830 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_BAD_PACKET,
831 13b2bc37 2022-10-23 stsp "PID %d handled a read-request for uid %d but this "
832 13b2bc37 2022-10-23 stsp "user is not reading from a repository", proc->pid,
833 13b2bc37 2022-10-23 stsp client->euid);
834 13b2bc37 2022-10-23 stsp }
835 13b2bc37 2022-10-23 stsp
836 13b2bc37 2022-10-23 stsp return NULL;
837 13b2bc37 2022-10-23 stsp }
838 13b2bc37 2022-10-23 stsp
839 13b2bc37 2022-10-23 stsp static const struct got_error *
840 13b2bc37 2022-10-23 stsp ensure_proc_is_writing(struct gotd_client *client,
841 13b2bc37 2022-10-23 stsp struct gotd_child_proc *proc)
842 13b2bc37 2022-10-23 stsp {
843 13b2bc37 2022-10-23 stsp if (!client_is_writing(client)) {
844 13b2bc37 2022-10-23 stsp kill_proc(proc, 1);
845 13b2bc37 2022-10-23 stsp return got_error_fmt(GOT_ERR_BAD_PACKET,
846 13b2bc37 2022-10-23 stsp "PID %d handled a write-request for uid %d but this "
847 13b2bc37 2022-10-23 stsp "user is not writing to a repository", proc->pid,
848 13b2bc37 2022-10-23 stsp client->euid);
849 13b2bc37 2022-10-23 stsp }
850 13b2bc37 2022-10-23 stsp
851 13b2bc37 2022-10-23 stsp return NULL;
852 13b2bc37 2022-10-23 stsp }
853 13b2bc37 2022-10-23 stsp
854 13b2bc37 2022-10-23 stsp static int
855 13b2bc37 2022-10-23 stsp verify_imsg_src(struct gotd_client *client, struct gotd_child_proc *proc,
856 13b2bc37 2022-10-23 stsp struct imsg *imsg)
857 13b2bc37 2022-10-23 stsp {
858 13b2bc37 2022-10-23 stsp const struct got_error *err;
859 13b2bc37 2022-10-23 stsp struct gotd_child_proc *client_proc;
860 13b2bc37 2022-10-23 stsp int ret = 0;
861 13b2bc37 2022-10-23 stsp
862 d93ecf7d 2022-12-14 stsp if (proc->type == PROC_REPO_READ || proc->type == PROC_REPO_WRITE) {
863 ae7c1b78 2023-01-10 stsp client_proc = get_client_repo_proc(client);
864 d93ecf7d 2022-12-14 stsp if (client_proc == NULL)
865 d93ecf7d 2022-12-14 stsp fatalx("no process found for uid %d", client->euid);
866 d93ecf7d 2022-12-14 stsp if (proc->pid != client_proc->pid) {
867 d93ecf7d 2022-12-14 stsp kill_proc(proc, 1);
868 d93ecf7d 2022-12-14 stsp log_warnx("received message from PID %d for uid %d, "
869 d93ecf7d 2022-12-14 stsp "while PID %d is the process serving this user",
870 d93ecf7d 2022-12-14 stsp proc->pid, client->euid, client_proc->pid);
871 ae7c1b78 2023-01-10 stsp return 0;
872 ae7c1b78 2023-01-10 stsp }
873 ae7c1b78 2023-01-10 stsp }
874 ae7c1b78 2023-01-10 stsp if (proc->type == PROC_SESSION) {
875 ae7c1b78 2023-01-10 stsp if (client->session == NULL) {
876 ae7c1b78 2023-01-10 stsp log_warnx("no session found for uid %d", client->euid);
877 d93ecf7d 2022-12-14 stsp return 0;
878 d93ecf7d 2022-12-14 stsp }
879 ae7c1b78 2023-01-10 stsp if (proc->pid != client->session->pid) {
880 ae7c1b78 2023-01-10 stsp kill_proc(proc, 1);
881 ae7c1b78 2023-01-10 stsp log_warnx("received message from PID %d for uid %d, "
882 ae7c1b78 2023-01-10 stsp "while PID %d is the process serving this user",
883 ae7c1b78 2023-01-10 stsp proc->pid, client->euid, client->session->pid);
884 ae7c1b78 2023-01-10 stsp return 0;
885 ae7c1b78 2023-01-10 stsp }
886 13b2bc37 2022-10-23 stsp }
887 13b2bc37 2022-10-23 stsp
888 13b2bc37 2022-10-23 stsp switch (imsg->hdr.type) {
889 13b2bc37 2022-10-23 stsp case GOTD_IMSG_ERROR:
890 13b2bc37 2022-10-23 stsp ret = 1;
891 13b2bc37 2022-10-23 stsp break;
892 d93ecf7d 2022-12-14 stsp case GOTD_IMSG_CONNECT:
893 d93ecf7d 2022-12-14 stsp if (proc->type != PROC_LISTEN) {
894 d93ecf7d 2022-12-14 stsp err = got_error_fmt(GOT_ERR_BAD_PACKET,
895 d93ecf7d 2022-12-14 stsp "new connection for uid %d from PID %d "
896 d93ecf7d 2022-12-14 stsp "which is not the listen process",
897 5e25db14 2022-12-29 stsp proc->pid, client->euid);
898 5e25db14 2022-12-29 stsp } else
899 5e25db14 2022-12-29 stsp ret = 1;
900 5e25db14 2022-12-29 stsp break;
901 5e25db14 2022-12-29 stsp case GOTD_IMSG_ACCESS_GRANTED:
902 5e25db14 2022-12-29 stsp if (proc->type != PROC_AUTH) {
903 5e25db14 2022-12-29 stsp err = got_error_fmt(GOT_ERR_BAD_PACKET,
904 5e25db14 2022-12-29 stsp "authentication of uid %d from PID %d "
905 5e25db14 2022-12-29 stsp "which is not the auth process",
906 d93ecf7d 2022-12-14 stsp proc->pid, client->euid);
907 d93ecf7d 2022-12-14 stsp } else
908 d93ecf7d 2022-12-14 stsp ret = 1;
909 d93ecf7d 2022-12-14 stsp break;
910 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_CLIENT_SESSION_READY:
911 ae7c1b78 2023-01-10 stsp if (proc->type != PROC_SESSION) {
912 ae7c1b78 2023-01-10 stsp err = got_error_fmt(GOT_ERR_BAD_PACKET,
913 ae7c1b78 2023-01-10 stsp "unexpected \"ready\" signal from PID %d",
914 ae7c1b78 2023-01-10 stsp proc->pid);
915 ae7c1b78 2023-01-10 stsp } else
916 ae7c1b78 2023-01-10 stsp ret = 1;
917 ae7c1b78 2023-01-10 stsp break;
918 b50a2b46 2022-12-29 stsp case GOTD_IMSG_REPO_CHILD_READY:
919 b50a2b46 2022-12-29 stsp if (proc->type != PROC_REPO_READ &&
920 b50a2b46 2022-12-29 stsp proc->type != PROC_REPO_WRITE) {
921 b50a2b46 2022-12-29 stsp err = got_error_fmt(GOT_ERR_BAD_PACKET,
922 b50a2b46 2022-12-29 stsp "unexpected \"ready\" signal from PID %d",
923 b50a2b46 2022-12-29 stsp proc->pid);
924 b50a2b46 2022-12-29 stsp } else
925 b50a2b46 2022-12-29 stsp ret = 1;
926 b50a2b46 2022-12-29 stsp break;
927 13b2bc37 2022-10-23 stsp case GOTD_IMSG_PACKFILE_DONE:
928 13b2bc37 2022-10-23 stsp err = ensure_proc_is_reading(client, proc);
929 13b2bc37 2022-10-23 stsp if (err)
930 13b2bc37 2022-10-23 stsp log_warnx("uid %d: %s", client->euid, err->msg);
931 13b2bc37 2022-10-23 stsp else
932 13b2bc37 2022-10-23 stsp ret = 1;
933 13b2bc37 2022-10-23 stsp break;
934 13b2bc37 2022-10-23 stsp case GOTD_IMSG_PACKFILE_INSTALL:
935 13b2bc37 2022-10-23 stsp case GOTD_IMSG_REF_UPDATES_START:
936 13b2bc37 2022-10-23 stsp case GOTD_IMSG_REF_UPDATE:
937 13b2bc37 2022-10-23 stsp err = ensure_proc_is_writing(client, proc);
938 13b2bc37 2022-10-23 stsp if (err)
939 13b2bc37 2022-10-23 stsp log_warnx("uid %d: %s", client->euid, err->msg);
940 13b2bc37 2022-10-23 stsp else
941 13b2bc37 2022-10-23 stsp ret = 1;
942 13b2bc37 2022-10-23 stsp break;
943 13b2bc37 2022-10-23 stsp default:
944 13b2bc37 2022-10-23 stsp log_debug("%s: unexpected imsg %d", __func__, imsg->hdr.type);
945 13b2bc37 2022-10-23 stsp break;
946 13b2bc37 2022-10-23 stsp }
947 13b2bc37 2022-10-23 stsp
948 13b2bc37 2022-10-23 stsp return ret;
949 13b2bc37 2022-10-23 stsp }
950 13b2bc37 2022-10-23 stsp
951 13b2bc37 2022-10-23 stsp static const struct got_error *
952 ae7c1b78 2023-01-10 stsp connect_repo_child(struct gotd_client *client,
953 ae7c1b78 2023-01-10 stsp struct gotd_child_proc *repo_proc)
954 b50a2b46 2022-12-29 stsp {
955 b50a2b46 2022-12-29 stsp static const struct got_error *err;
956 ae7c1b78 2023-01-10 stsp struct gotd_imsgev *session_iev = &client->session->iev;
957 ae7c1b78 2023-01-10 stsp struct gotd_imsg_connect_repo_child ireq;
958 ae7c1b78 2023-01-10 stsp int pipe[2];
959 b50a2b46 2022-12-29 stsp
960 ae7c1b78 2023-01-10 stsp if (client->state != GOTD_STATE_EXPECT_LIST_REFS)
961 ae7c1b78 2023-01-10 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
962 ae7c1b78 2023-01-10 stsp "unexpected repo child ready signal received");
963 b50a2b46 2022-12-29 stsp
964 ae7c1b78 2023-01-10 stsp if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
965 ae7c1b78 2023-01-10 stsp PF_UNSPEC, pipe) == -1)
966 ae7c1b78 2023-01-10 stsp fatal("socketpair");
967 b50a2b46 2022-12-29 stsp
968 ae7c1b78 2023-01-10 stsp memset(&ireq, 0, sizeof(ireq));
969 ae7c1b78 2023-01-10 stsp ireq.client_id = client->id;
970 ae7c1b78 2023-01-10 stsp ireq.proc_id = repo_proc->type;
971 13b2bc37 2022-10-23 stsp
972 ae7c1b78 2023-01-10 stsp /* Pass repo child pipe to session child process. */
973 ae7c1b78 2023-01-10 stsp if (gotd_imsg_compose_event(session_iev, GOTD_IMSG_CONNECT_REPO_CHILD,
974 ae7c1b78 2023-01-10 stsp PROC_GOTD, pipe[0], &ireq, sizeof(ireq)) == -1) {
975 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
976 ae7c1b78 2023-01-10 stsp close(pipe[0]);
977 ae7c1b78 2023-01-10 stsp close(pipe[1]);
978 ae7c1b78 2023-01-10 stsp return err;
979 13b2bc37 2022-10-23 stsp }
980 13b2bc37 2022-10-23 stsp
981 ae7c1b78 2023-01-10 stsp /* Pass session child pipe to repo child process. */
982 ae7c1b78 2023-01-10 stsp if (gotd_imsg_compose_event(&repo_proc->iev,
983 ae7c1b78 2023-01-10 stsp GOTD_IMSG_CONNECT_REPO_CHILD, PROC_GOTD, pipe[1], NULL, 0) == -1) {
984 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("imsg compose CONNECT_REPO_CHILD");
985 ae7c1b78 2023-01-10 stsp close(pipe[1]);
986 ae7c1b78 2023-01-10 stsp return err;
987 13b2bc37 2022-10-23 stsp }
988 13b2bc37 2022-10-23 stsp
989 13b2bc37 2022-10-23 stsp return NULL;
990 13b2bc37 2022-10-23 stsp }
991 13b2bc37 2022-10-23 stsp
992 13b2bc37 2022-10-23 stsp static void
993 b50a2b46 2022-12-29 stsp gotd_dispatch_listener(int fd, short event, void *arg)
994 13b2bc37 2022-10-23 stsp {
995 13b2bc37 2022-10-23 stsp struct gotd_imsgev *iev = arg;
996 13b2bc37 2022-10-23 stsp struct imsgbuf *ibuf = &iev->ibuf;
997 b50a2b46 2022-12-29 stsp struct gotd_child_proc *proc = &gotd.listen_proc;
998 b50a2b46 2022-12-29 stsp ssize_t n;
999 b50a2b46 2022-12-29 stsp int shut = 0;
1000 b50a2b46 2022-12-29 stsp struct imsg imsg;
1001 b50a2b46 2022-12-29 stsp
1002 b50a2b46 2022-12-29 stsp if (proc->iev.ibuf.fd != fd)
1003 b50a2b46 2022-12-29 stsp fatalx("%s: unexpected fd %d", __func__, fd);
1004 b50a2b46 2022-12-29 stsp
1005 b50a2b46 2022-12-29 stsp if (event & EV_READ) {
1006 b50a2b46 2022-12-29 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1007 b50a2b46 2022-12-29 stsp fatal("imsg_read error");
1008 b50a2b46 2022-12-29 stsp if (n == 0) {
1009 b50a2b46 2022-12-29 stsp /* Connection closed. */
1010 b50a2b46 2022-12-29 stsp shut = 1;
1011 b50a2b46 2022-12-29 stsp goto done;
1012 b50a2b46 2022-12-29 stsp }
1013 b50a2b46 2022-12-29 stsp }
1014 b50a2b46 2022-12-29 stsp
1015 b50a2b46 2022-12-29 stsp if (event & EV_WRITE) {
1016 b50a2b46 2022-12-29 stsp n = msgbuf_write(&ibuf->w);
1017 b50a2b46 2022-12-29 stsp if (n == -1 && errno != EAGAIN)
1018 b50a2b46 2022-12-29 stsp fatal("msgbuf_write");
1019 b50a2b46 2022-12-29 stsp if (n == 0) {
1020 b50a2b46 2022-12-29 stsp /* Connection closed. */
1021 b50a2b46 2022-12-29 stsp shut = 1;
1022 b50a2b46 2022-12-29 stsp goto done;
1023 b50a2b46 2022-12-29 stsp }
1024 b50a2b46 2022-12-29 stsp }
1025 b50a2b46 2022-12-29 stsp
1026 b50a2b46 2022-12-29 stsp for (;;) {
1027 b50a2b46 2022-12-29 stsp const struct got_error *err = NULL;
1028 b50a2b46 2022-12-29 stsp struct gotd_client *client = NULL;
1029 b50a2b46 2022-12-29 stsp uint32_t client_id = 0;
1030 b50a2b46 2022-12-29 stsp int do_disconnect = 0;
1031 b50a2b46 2022-12-29 stsp
1032 b50a2b46 2022-12-29 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
1033 b50a2b46 2022-12-29 stsp fatal("%s: imsg_get error", __func__);
1034 b50a2b46 2022-12-29 stsp if (n == 0) /* No more messages. */
1035 b50a2b46 2022-12-29 stsp break;
1036 b50a2b46 2022-12-29 stsp
1037 b50a2b46 2022-12-29 stsp switch (imsg.hdr.type) {
1038 b50a2b46 2022-12-29 stsp case GOTD_IMSG_ERROR:
1039 b50a2b46 2022-12-29 stsp do_disconnect = 1;
1040 b50a2b46 2022-12-29 stsp err = gotd_imsg_recv_error(&client_id, &imsg);
1041 b50a2b46 2022-12-29 stsp break;
1042 b50a2b46 2022-12-29 stsp case GOTD_IMSG_CONNECT:
1043 b50a2b46 2022-12-29 stsp err = recv_connect(&client_id, &imsg);
1044 b50a2b46 2022-12-29 stsp break;
1045 b50a2b46 2022-12-29 stsp default:
1046 b50a2b46 2022-12-29 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1047 b50a2b46 2022-12-29 stsp break;
1048 b50a2b46 2022-12-29 stsp }
1049 b50a2b46 2022-12-29 stsp
1050 b50a2b46 2022-12-29 stsp client = find_client(client_id);
1051 b50a2b46 2022-12-29 stsp if (client == NULL) {
1052 b50a2b46 2022-12-29 stsp log_warnx("%s: client not found", __func__);
1053 b50a2b46 2022-12-29 stsp imsg_free(&imsg);
1054 b50a2b46 2022-12-29 stsp continue;
1055 b50a2b46 2022-12-29 stsp }
1056 b50a2b46 2022-12-29 stsp
1057 b50a2b46 2022-12-29 stsp if (err)
1058 b50a2b46 2022-12-29 stsp log_warnx("uid %d: %s", client->euid, err->msg);
1059 b50a2b46 2022-12-29 stsp
1060 b50a2b46 2022-12-29 stsp if (do_disconnect) {
1061 b50a2b46 2022-12-29 stsp if (err)
1062 b50a2b46 2022-12-29 stsp disconnect_on_error(client, err);
1063 b50a2b46 2022-12-29 stsp else
1064 b50a2b46 2022-12-29 stsp disconnect(client);
1065 b50a2b46 2022-12-29 stsp }
1066 b50a2b46 2022-12-29 stsp
1067 b50a2b46 2022-12-29 stsp imsg_free(&imsg);
1068 b50a2b46 2022-12-29 stsp }
1069 b50a2b46 2022-12-29 stsp done:
1070 b50a2b46 2022-12-29 stsp if (!shut) {
1071 b50a2b46 2022-12-29 stsp gotd_imsg_event_add(iev);
1072 b50a2b46 2022-12-29 stsp } else {
1073 b50a2b46 2022-12-29 stsp /* This pipe is dead. Remove its event handler */
1074 b50a2b46 2022-12-29 stsp event_del(&iev->ev);
1075 b50a2b46 2022-12-29 stsp event_loopexit(NULL);
1076 b50a2b46 2022-12-29 stsp }
1077 b50a2b46 2022-12-29 stsp }
1078 b50a2b46 2022-12-29 stsp
1079 b50a2b46 2022-12-29 stsp static void
1080 5e25db14 2022-12-29 stsp gotd_dispatch_auth_child(int fd, short event, void *arg)
1081 5e25db14 2022-12-29 stsp {
1082 5e25db14 2022-12-29 stsp const struct got_error *err = NULL;
1083 5e25db14 2022-12-29 stsp struct gotd_imsgev *iev = arg;
1084 5e25db14 2022-12-29 stsp struct imsgbuf *ibuf = &iev->ibuf;
1085 5e25db14 2022-12-29 stsp struct gotd_client *client;
1086 5e25db14 2022-12-29 stsp struct gotd_repo *repo = NULL;
1087 5e25db14 2022-12-29 stsp ssize_t n;
1088 5e25db14 2022-12-29 stsp int shut = 0;
1089 5e25db14 2022-12-29 stsp struct imsg imsg;
1090 5e25db14 2022-12-29 stsp uint32_t client_id = 0;
1091 5e25db14 2022-12-29 stsp int do_disconnect = 0;
1092 5e25db14 2022-12-29 stsp
1093 5e25db14 2022-12-29 stsp client = find_client_by_proc_fd(fd);
1094 5e25db14 2022-12-29 stsp if (client == NULL)
1095 5e25db14 2022-12-29 stsp fatalx("cannot find client for fd %d", fd);
1096 5e25db14 2022-12-29 stsp
1097 5e25db14 2022-12-29 stsp if (client->auth == NULL)
1098 5e25db14 2022-12-29 stsp fatalx("cannot find auth child process for fd %d", fd);
1099 5e25db14 2022-12-29 stsp
1100 5e25db14 2022-12-29 stsp if (event & EV_READ) {
1101 5e25db14 2022-12-29 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1102 5e25db14 2022-12-29 stsp fatal("imsg_read error");
1103 5e25db14 2022-12-29 stsp if (n == 0) {
1104 5e25db14 2022-12-29 stsp /* Connection closed. */
1105 5e25db14 2022-12-29 stsp shut = 1;
1106 5e25db14 2022-12-29 stsp goto done;
1107 5e25db14 2022-12-29 stsp }
1108 5e25db14 2022-12-29 stsp }
1109 5e25db14 2022-12-29 stsp
1110 5e25db14 2022-12-29 stsp if (event & EV_WRITE) {
1111 5e25db14 2022-12-29 stsp n = msgbuf_write(&ibuf->w);
1112 5e25db14 2022-12-29 stsp if (n == -1 && errno != EAGAIN)
1113 5e25db14 2022-12-29 stsp fatal("msgbuf_write");
1114 5e25db14 2022-12-29 stsp if (n == 0) {
1115 5e25db14 2022-12-29 stsp /* Connection closed. */
1116 5e25db14 2022-12-29 stsp shut = 1;
1117 5e25db14 2022-12-29 stsp }
1118 5e25db14 2022-12-29 stsp goto done;
1119 5e25db14 2022-12-29 stsp }
1120 5e25db14 2022-12-29 stsp
1121 5e25db14 2022-12-29 stsp if (client->auth->iev.ibuf.fd != fd)
1122 5e25db14 2022-12-29 stsp fatalx("%s: unexpected fd %d", __func__, fd);
1123 5e25db14 2022-12-29 stsp
1124 5e25db14 2022-12-29 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
1125 5e25db14 2022-12-29 stsp fatal("%s: imsg_get error", __func__);
1126 5e25db14 2022-12-29 stsp if (n == 0) /* No more messages. */
1127 5e25db14 2022-12-29 stsp return;
1128 5e25db14 2022-12-29 stsp
1129 5e25db14 2022-12-29 stsp evtimer_del(&client->tmo);
1130 5e25db14 2022-12-29 stsp
1131 5e25db14 2022-12-29 stsp switch (imsg.hdr.type) {
1132 5e25db14 2022-12-29 stsp case GOTD_IMSG_ERROR:
1133 5e25db14 2022-12-29 stsp do_disconnect = 1;
1134 5e25db14 2022-12-29 stsp err = gotd_imsg_recv_error(&client_id, &imsg);
1135 5e25db14 2022-12-29 stsp break;
1136 5e25db14 2022-12-29 stsp case GOTD_IMSG_ACCESS_GRANTED:
1137 5e25db14 2022-12-29 stsp break;
1138 5e25db14 2022-12-29 stsp default:
1139 5e25db14 2022-12-29 stsp do_disconnect = 1;
1140 5e25db14 2022-12-29 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1141 5e25db14 2022-12-29 stsp break;
1142 5e25db14 2022-12-29 stsp }
1143 5e25db14 2022-12-29 stsp
1144 5e25db14 2022-12-29 stsp if (!verify_imsg_src(client, client->auth, &imsg)) {
1145 5e25db14 2022-12-29 stsp do_disconnect = 1;
1146 5e25db14 2022-12-29 stsp log_debug("dropping imsg type %d from PID %d",
1147 5e25db14 2022-12-29 stsp imsg.hdr.type, client->auth->pid);
1148 5e25db14 2022-12-29 stsp }
1149 5e25db14 2022-12-29 stsp imsg_free(&imsg);
1150 5e25db14 2022-12-29 stsp
1151 5e25db14 2022-12-29 stsp if (do_disconnect) {
1152 5e25db14 2022-12-29 stsp if (err)
1153 5e25db14 2022-12-29 stsp disconnect_on_error(client, err);
1154 5e25db14 2022-12-29 stsp else
1155 5e25db14 2022-12-29 stsp disconnect(client);
1156 5e25db14 2022-12-29 stsp goto done;
1157 5e25db14 2022-12-29 stsp }
1158 5e25db14 2022-12-29 stsp
1159 5e25db14 2022-12-29 stsp repo = find_repo_by_name(client->auth->repo_name);
1160 5e25db14 2022-12-29 stsp if (repo == NULL) {
1161 5e25db14 2022-12-29 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
1162 5e25db14 2022-12-29 stsp goto done;
1163 5e25db14 2022-12-29 stsp }
1164 5e25db14 2022-12-29 stsp kill_auth_proc(client);
1165 5e25db14 2022-12-29 stsp
1166 5e25db14 2022-12-29 stsp log_info("authenticated uid %d for repository %s\n",
1167 5e25db14 2022-12-29 stsp client->euid, repo->name);
1168 5e25db14 2022-12-29 stsp
1169 ae7c1b78 2023-01-10 stsp err = start_session_child(client, repo, gotd.argv0,
1170 7fdc3e58 2022-12-30 mark gotd.confpath, gotd.daemonize, gotd.verbosity);
1171 ae7c1b78 2023-01-10 stsp if (err)
1172 ae7c1b78 2023-01-10 stsp goto done;
1173 5e25db14 2022-12-29 stsp done:
1174 5e25db14 2022-12-29 stsp if (err)
1175 5e25db14 2022-12-29 stsp log_warnx("uid %d: %s", client->euid, err->msg);
1176 5e25db14 2022-12-29 stsp
1177 5e25db14 2022-12-29 stsp /* We might have killed the auth process by now. */
1178 5e25db14 2022-12-29 stsp if (client->auth != NULL) {
1179 5e25db14 2022-12-29 stsp if (!shut) {
1180 5e25db14 2022-12-29 stsp gotd_imsg_event_add(iev);
1181 5e25db14 2022-12-29 stsp } else {
1182 5e25db14 2022-12-29 stsp /* This pipe is dead. Remove its event handler */
1183 5e25db14 2022-12-29 stsp event_del(&iev->ev);
1184 5e25db14 2022-12-29 stsp }
1185 5e25db14 2022-12-29 stsp }
1186 5e25db14 2022-12-29 stsp }
1187 5e25db14 2022-12-29 stsp
1188 ae7c1b78 2023-01-10 stsp static const struct got_error *
1189 ae7c1b78 2023-01-10 stsp connect_session(struct gotd_client *client)
1190 ae7c1b78 2023-01-10 stsp {
1191 ae7c1b78 2023-01-10 stsp const struct got_error *err = NULL;
1192 ae7c1b78 2023-01-10 stsp struct gotd_imsg_connect iconnect;
1193 ae7c1b78 2023-01-10 stsp int s;
1194 ae7c1b78 2023-01-10 stsp
1195 ae7c1b78 2023-01-10 stsp memset(&iconnect, 0, sizeof(iconnect));
1196 ae7c1b78 2023-01-10 stsp
1197 ae7c1b78 2023-01-10 stsp s = dup(client->fd);
1198 ae7c1b78 2023-01-10 stsp if (s == -1)
1199 ae7c1b78 2023-01-10 stsp return got_error_from_errno("dup");
1200 ae7c1b78 2023-01-10 stsp
1201 ae7c1b78 2023-01-10 stsp iconnect.client_id = client->id;
1202 ae7c1b78 2023-01-10 stsp iconnect.euid = client->euid;
1203 ae7c1b78 2023-01-10 stsp iconnect.egid = client->egid;
1204 ae7c1b78 2023-01-10 stsp
1205 ae7c1b78 2023-01-10 stsp if (gotd_imsg_compose_event(&client->session->iev, GOTD_IMSG_CONNECT,
1206 ae7c1b78 2023-01-10 stsp PROC_GOTD, s, &iconnect, sizeof(iconnect)) == -1) {
1207 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("imsg compose CONNECT");
1208 ae7c1b78 2023-01-10 stsp close(s);
1209 ae7c1b78 2023-01-10 stsp return err;
1210 ae7c1b78 2023-01-10 stsp }
1211 ae7c1b78 2023-01-10 stsp
1212 ae7c1b78 2023-01-10 stsp /*
1213 ae7c1b78 2023-01-10 stsp * We are no longer interested in messages from this client.
1214 ae7c1b78 2023-01-10 stsp * Further client requests will be handled by the session process.
1215 ae7c1b78 2023-01-10 stsp */
1216 ae7c1b78 2023-01-10 stsp msgbuf_clear(&client->iev.ibuf.w);
1217 ae7c1b78 2023-01-10 stsp imsg_clear(&client->iev.ibuf);
1218 ae7c1b78 2023-01-10 stsp event_del(&client->iev.ev);
1219 ae7c1b78 2023-01-10 stsp client->fd = -1; /* will be closed via copy in client->iev.ibuf.fd */
1220 ae7c1b78 2023-01-10 stsp
1221 ae7c1b78 2023-01-10 stsp return NULL;
1222 ae7c1b78 2023-01-10 stsp }
1223 ae7c1b78 2023-01-10 stsp
1224 5e25db14 2022-12-29 stsp static void
1225 ae7c1b78 2023-01-10 stsp gotd_dispatch_client_session(int fd, short event, void *arg)
1226 b50a2b46 2022-12-29 stsp {
1227 b50a2b46 2022-12-29 stsp struct gotd_imsgev *iev = arg;
1228 b50a2b46 2022-12-29 stsp struct imsgbuf *ibuf = &iev->ibuf;
1229 13b2bc37 2022-10-23 stsp struct gotd_child_proc *proc = NULL;
1230 b50a2b46 2022-12-29 stsp struct gotd_client *client = NULL;
1231 13b2bc37 2022-10-23 stsp ssize_t n;
1232 13b2bc37 2022-10-23 stsp int shut = 0;
1233 13b2bc37 2022-10-23 stsp struct imsg imsg;
1234 13b2bc37 2022-10-23 stsp
1235 ae7c1b78 2023-01-10 stsp client = find_client_by_proc_fd(fd);
1236 ae7c1b78 2023-01-10 stsp if (client == NULL)
1237 ae7c1b78 2023-01-10 stsp fatalx("cannot find client for fd %d", fd);
1238 ae7c1b78 2023-01-10 stsp
1239 13b2bc37 2022-10-23 stsp if (event & EV_READ) {
1240 13b2bc37 2022-10-23 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1241 13b2bc37 2022-10-23 stsp fatal("imsg_read error");
1242 13b2bc37 2022-10-23 stsp if (n == 0) {
1243 13b2bc37 2022-10-23 stsp /* Connection closed. */
1244 13b2bc37 2022-10-23 stsp shut = 1;
1245 13b2bc37 2022-10-23 stsp goto done;
1246 13b2bc37 2022-10-23 stsp }
1247 13b2bc37 2022-10-23 stsp }
1248 13b2bc37 2022-10-23 stsp
1249 13b2bc37 2022-10-23 stsp if (event & EV_WRITE) {
1250 13b2bc37 2022-10-23 stsp n = msgbuf_write(&ibuf->w);
1251 13b2bc37 2022-10-23 stsp if (n == -1 && errno != EAGAIN)
1252 13b2bc37 2022-10-23 stsp fatal("msgbuf_write");
1253 13b2bc37 2022-10-23 stsp if (n == 0) {
1254 13b2bc37 2022-10-23 stsp /* Connection closed. */
1255 13b2bc37 2022-10-23 stsp shut = 1;
1256 13b2bc37 2022-10-23 stsp goto done;
1257 ae7c1b78 2023-01-10 stsp }
1258 ae7c1b78 2023-01-10 stsp }
1259 ae7c1b78 2023-01-10 stsp
1260 ae7c1b78 2023-01-10 stsp proc = client->session;
1261 ae7c1b78 2023-01-10 stsp if (proc == NULL)
1262 ae7c1b78 2023-01-10 stsp fatalx("cannot find session child process for fd %d", fd);
1263 ae7c1b78 2023-01-10 stsp
1264 ae7c1b78 2023-01-10 stsp for (;;) {
1265 ae7c1b78 2023-01-10 stsp const struct got_error *err = NULL;
1266 ae7c1b78 2023-01-10 stsp uint32_t client_id = 0;
1267 ae7c1b78 2023-01-10 stsp int do_disconnect = 0, do_start_repo_child = 0;
1268 ae7c1b78 2023-01-10 stsp
1269 ae7c1b78 2023-01-10 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
1270 ae7c1b78 2023-01-10 stsp fatal("%s: imsg_get error", __func__);
1271 ae7c1b78 2023-01-10 stsp if (n == 0) /* No more messages. */
1272 ae7c1b78 2023-01-10 stsp break;
1273 ae7c1b78 2023-01-10 stsp
1274 ae7c1b78 2023-01-10 stsp switch (imsg.hdr.type) {
1275 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_ERROR:
1276 ae7c1b78 2023-01-10 stsp do_disconnect = 1;
1277 ae7c1b78 2023-01-10 stsp err = gotd_imsg_recv_error(&client_id, &imsg);
1278 ae7c1b78 2023-01-10 stsp break;
1279 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_CLIENT_SESSION_READY:
1280 ae7c1b78 2023-01-10 stsp if (client->state != GOTD_STATE_EXPECT_LIST_REFS) {
1281 ae7c1b78 2023-01-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1282 ae7c1b78 2023-01-10 stsp break;
1283 ae7c1b78 2023-01-10 stsp }
1284 ae7c1b78 2023-01-10 stsp do_start_repo_child = 1;
1285 ae7c1b78 2023-01-10 stsp break;
1286 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_DISCONNECT:
1287 ae7c1b78 2023-01-10 stsp do_disconnect = 1;
1288 ae7c1b78 2023-01-10 stsp break;
1289 ae7c1b78 2023-01-10 stsp default:
1290 ae7c1b78 2023-01-10 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1291 ae7c1b78 2023-01-10 stsp break;
1292 13b2bc37 2022-10-23 stsp }
1293 ae7c1b78 2023-01-10 stsp
1294 ae7c1b78 2023-01-10 stsp if (!verify_imsg_src(client, proc, &imsg)) {
1295 ae7c1b78 2023-01-10 stsp log_debug("dropping imsg type %d from PID %d",
1296 ae7c1b78 2023-01-10 stsp imsg.hdr.type, proc->pid);
1297 ae7c1b78 2023-01-10 stsp imsg_free(&imsg);
1298 ae7c1b78 2023-01-10 stsp continue;
1299 ae7c1b78 2023-01-10 stsp }
1300 ae7c1b78 2023-01-10 stsp if (err)
1301 ae7c1b78 2023-01-10 stsp log_warnx("uid %d: %s", client->euid, err->msg);
1302 ae7c1b78 2023-01-10 stsp
1303 ae7c1b78 2023-01-10 stsp if (do_start_repo_child) {
1304 ae7c1b78 2023-01-10 stsp struct gotd_repo *repo;
1305 ae7c1b78 2023-01-10 stsp
1306 ae7c1b78 2023-01-10 stsp repo = find_repo_by_name(client->session->repo_name);
1307 ae7c1b78 2023-01-10 stsp if (repo != NULL) {
1308 ae7c1b78 2023-01-10 stsp enum gotd_procid proc_type;
1309 ae7c1b78 2023-01-10 stsp
1310 ae7c1b78 2023-01-10 stsp if (client->required_auth & GOTD_AUTH_WRITE)
1311 ae7c1b78 2023-01-10 stsp proc_type = PROC_REPO_WRITE;
1312 ae7c1b78 2023-01-10 stsp else
1313 ae7c1b78 2023-01-10 stsp proc_type = PROC_REPO_READ;
1314 ae7c1b78 2023-01-10 stsp
1315 ae7c1b78 2023-01-10 stsp err = start_repo_child(client, proc_type, repo,
1316 ae7c1b78 2023-01-10 stsp gotd.argv0, gotd.confpath, gotd.daemonize,
1317 ae7c1b78 2023-01-10 stsp gotd.verbosity);
1318 ae7c1b78 2023-01-10 stsp } else
1319 ae7c1b78 2023-01-10 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
1320 ae7c1b78 2023-01-10 stsp
1321 ae7c1b78 2023-01-10 stsp if (err) {
1322 ae7c1b78 2023-01-10 stsp log_warnx("uid %d: %s", client->euid, err->msg);
1323 ae7c1b78 2023-01-10 stsp do_disconnect = 1;
1324 ae7c1b78 2023-01-10 stsp }
1325 ae7c1b78 2023-01-10 stsp }
1326 ae7c1b78 2023-01-10 stsp
1327 ae7c1b78 2023-01-10 stsp if (do_disconnect) {
1328 ae7c1b78 2023-01-10 stsp if (err)
1329 ae7c1b78 2023-01-10 stsp disconnect_on_error(client, err);
1330 ae7c1b78 2023-01-10 stsp else
1331 ae7c1b78 2023-01-10 stsp disconnect(client);
1332 ae7c1b78 2023-01-10 stsp }
1333 ae7c1b78 2023-01-10 stsp
1334 ae7c1b78 2023-01-10 stsp imsg_free(&imsg);
1335 13b2bc37 2022-10-23 stsp }
1336 ae7c1b78 2023-01-10 stsp done:
1337 ae7c1b78 2023-01-10 stsp if (!shut) {
1338 ae7c1b78 2023-01-10 stsp gotd_imsg_event_add(iev);
1339 ae7c1b78 2023-01-10 stsp } else {
1340 ae7c1b78 2023-01-10 stsp /* This pipe is dead. Remove its event handler */
1341 ae7c1b78 2023-01-10 stsp event_del(&iev->ev);
1342 ae7c1b78 2023-01-10 stsp disconnect(client);
1343 ae7c1b78 2023-01-10 stsp }
1344 ae7c1b78 2023-01-10 stsp }
1345 13b2bc37 2022-10-23 stsp
1346 ae7c1b78 2023-01-10 stsp static void
1347 ae7c1b78 2023-01-10 stsp gotd_dispatch_repo_child(int fd, short event, void *arg)
1348 ae7c1b78 2023-01-10 stsp {
1349 ae7c1b78 2023-01-10 stsp struct gotd_imsgev *iev = arg;
1350 ae7c1b78 2023-01-10 stsp struct imsgbuf *ibuf = &iev->ibuf;
1351 ae7c1b78 2023-01-10 stsp struct gotd_child_proc *proc = NULL;
1352 ae7c1b78 2023-01-10 stsp struct gotd_client *client;
1353 ae7c1b78 2023-01-10 stsp ssize_t n;
1354 ae7c1b78 2023-01-10 stsp int shut = 0;
1355 ae7c1b78 2023-01-10 stsp struct imsg imsg;
1356 ae7c1b78 2023-01-10 stsp
1357 b50a2b46 2022-12-29 stsp client = find_client_by_proc_fd(fd);
1358 b50a2b46 2022-12-29 stsp if (client == NULL)
1359 b50a2b46 2022-12-29 stsp fatalx("cannot find client for fd %d", fd);
1360 b50a2b46 2022-12-29 stsp
1361 ae7c1b78 2023-01-10 stsp if (event & EV_READ) {
1362 ae7c1b78 2023-01-10 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1363 ae7c1b78 2023-01-10 stsp fatal("imsg_read error");
1364 ae7c1b78 2023-01-10 stsp if (n == 0) {
1365 ae7c1b78 2023-01-10 stsp /* Connection closed. */
1366 ae7c1b78 2023-01-10 stsp shut = 1;
1367 ae7c1b78 2023-01-10 stsp goto done;
1368 ae7c1b78 2023-01-10 stsp }
1369 ae7c1b78 2023-01-10 stsp }
1370 ae7c1b78 2023-01-10 stsp
1371 ae7c1b78 2023-01-10 stsp if (event & EV_WRITE) {
1372 ae7c1b78 2023-01-10 stsp n = msgbuf_write(&ibuf->w);
1373 ae7c1b78 2023-01-10 stsp if (n == -1 && errno != EAGAIN)
1374 ae7c1b78 2023-01-10 stsp fatal("msgbuf_write");
1375 ae7c1b78 2023-01-10 stsp if (n == 0) {
1376 ae7c1b78 2023-01-10 stsp /* Connection closed. */
1377 ae7c1b78 2023-01-10 stsp shut = 1;
1378 ae7c1b78 2023-01-10 stsp goto done;
1379 ae7c1b78 2023-01-10 stsp }
1380 ae7c1b78 2023-01-10 stsp }
1381 ae7c1b78 2023-01-10 stsp
1382 ae7c1b78 2023-01-10 stsp proc = get_client_repo_proc(client);
1383 13b2bc37 2022-10-23 stsp if (proc == NULL)
1384 13b2bc37 2022-10-23 stsp fatalx("cannot find child process for fd %d", fd);
1385 13b2bc37 2022-10-23 stsp
1386 13b2bc37 2022-10-23 stsp for (;;) {
1387 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
1388 13b2bc37 2022-10-23 stsp uint32_t client_id = 0;
1389 13b2bc37 2022-10-23 stsp int do_disconnect = 0;
1390 13b2bc37 2022-10-23 stsp
1391 13b2bc37 2022-10-23 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
1392 13b2bc37 2022-10-23 stsp fatal("%s: imsg_get error", __func__);
1393 13b2bc37 2022-10-23 stsp if (n == 0) /* No more messages. */
1394 13b2bc37 2022-10-23 stsp break;
1395 13b2bc37 2022-10-23 stsp
1396 13b2bc37 2022-10-23 stsp switch (imsg.hdr.type) {
1397 13b2bc37 2022-10-23 stsp case GOTD_IMSG_ERROR:
1398 13b2bc37 2022-10-23 stsp do_disconnect = 1;
1399 13b2bc37 2022-10-23 stsp err = gotd_imsg_recv_error(&client_id, &imsg);
1400 13b2bc37 2022-10-23 stsp break;
1401 b50a2b46 2022-12-29 stsp case GOTD_IMSG_REPO_CHILD_READY:
1402 ae7c1b78 2023-01-10 stsp err = connect_session(client);
1403 ae7c1b78 2023-01-10 stsp if (err)
1404 ae7c1b78 2023-01-10 stsp break;
1405 ae7c1b78 2023-01-10 stsp err = connect_repo_child(client, proc);
1406 d93ecf7d 2022-12-14 stsp break;
1407 13b2bc37 2022-10-23 stsp default:
1408 13b2bc37 2022-10-23 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1409 13b2bc37 2022-10-23 stsp break;
1410 13b2bc37 2022-10-23 stsp }
1411 13b2bc37 2022-10-23 stsp
1412 13b2bc37 2022-10-23 stsp if (!verify_imsg_src(client, proc, &imsg)) {
1413 13b2bc37 2022-10-23 stsp log_debug("dropping imsg type %d from PID %d",
1414 13b2bc37 2022-10-23 stsp imsg.hdr.type, proc->pid);
1415 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
1416 13b2bc37 2022-10-23 stsp continue;
1417 13b2bc37 2022-10-23 stsp }
1418 13b2bc37 2022-10-23 stsp if (err)
1419 13b2bc37 2022-10-23 stsp log_warnx("uid %d: %s", client->euid, err->msg);
1420 13b2bc37 2022-10-23 stsp
1421 13b2bc37 2022-10-23 stsp if (do_disconnect) {
1422 13b2bc37 2022-10-23 stsp if (err)
1423 13b2bc37 2022-10-23 stsp disconnect_on_error(client, err);
1424 13b2bc37 2022-10-23 stsp else
1425 13b2bc37 2022-10-23 stsp disconnect(client);
1426 36c7cfbb 2022-11-04 stsp }
1427 ae7c1b78 2023-01-10 stsp
1428 13b2bc37 2022-10-23 stsp imsg_free(&imsg);
1429 13b2bc37 2022-10-23 stsp }
1430 13b2bc37 2022-10-23 stsp done:
1431 13b2bc37 2022-10-23 stsp if (!shut) {
1432 13b2bc37 2022-10-23 stsp gotd_imsg_event_add(iev);
1433 13b2bc37 2022-10-23 stsp } else {
1434 13b2bc37 2022-10-23 stsp /* This pipe is dead. Remove its event handler */
1435 13b2bc37 2022-10-23 stsp event_del(&iev->ev);
1436 ae7c1b78 2023-01-10 stsp disconnect(client);
1437 13b2bc37 2022-10-23 stsp }
1438 13b2bc37 2022-10-23 stsp }
1439 13b2bc37 2022-10-23 stsp
1440 13b2bc37 2022-10-23 stsp static pid_t
1441 eec68231 2022-12-14 stsp start_child(enum gotd_procid proc_id, const char *repo_path,
1442 585362fd 2022-10-31 op char *argv0, const char *confpath, int fd, int daemonize, int verbosity)
1443 13b2bc37 2022-10-23 stsp {
1444 585362fd 2022-10-31 op char *argv[11];
1445 13b2bc37 2022-10-23 stsp int argc = 0;
1446 13b2bc37 2022-10-23 stsp pid_t pid;
1447 13b2bc37 2022-10-23 stsp
1448 13b2bc37 2022-10-23 stsp switch (pid = fork()) {
1449 13b2bc37 2022-10-23 stsp case -1:
1450 13b2bc37 2022-10-23 stsp fatal("cannot fork");
1451 13b2bc37 2022-10-23 stsp case 0:
1452 13b2bc37 2022-10-23 stsp break;
1453 13b2bc37 2022-10-23 stsp default:
1454 13b2bc37 2022-10-23 stsp close(fd);
1455 13b2bc37 2022-10-23 stsp return pid;
1456 13b2bc37 2022-10-23 stsp }
1457 13b2bc37 2022-10-23 stsp
1458 8c6fc146 2022-11-17 stsp if (fd != GOTD_FILENO_MSG_PIPE) {
1459 8c6fc146 2022-11-17 stsp if (dup2(fd, GOTD_FILENO_MSG_PIPE) == -1)
1460 13b2bc37 2022-10-23 stsp fatal("cannot setup imsg fd");
1461 13b2bc37 2022-10-23 stsp } else if (fcntl(fd, F_SETFD, 0) == -1)
1462 13b2bc37 2022-10-23 stsp fatal("cannot setup imsg fd");
1463 13b2bc37 2022-10-23 stsp
1464 13b2bc37 2022-10-23 stsp argv[argc++] = argv0;
1465 13b2bc37 2022-10-23 stsp switch (proc_id) {
1466 d93ecf7d 2022-12-14 stsp case PROC_LISTEN:
1467 d93ecf7d 2022-12-14 stsp argv[argc++] = (char *)"-L";
1468 d93ecf7d 2022-12-14 stsp break;
1469 5e25db14 2022-12-29 stsp case PROC_AUTH:
1470 5e25db14 2022-12-29 stsp argv[argc++] = (char *)"-A";
1471 5e25db14 2022-12-29 stsp break;
1472 ae7c1b78 2023-01-10 stsp case PROC_SESSION:
1473 ae7c1b78 2023-01-10 stsp argv[argc++] = (char *)"-S";
1474 ae7c1b78 2023-01-10 stsp break;
1475 13b2bc37 2022-10-23 stsp case PROC_REPO_READ:
1476 13b2bc37 2022-10-23 stsp argv[argc++] = (char *)"-R";
1477 13b2bc37 2022-10-23 stsp break;
1478 13b2bc37 2022-10-23 stsp case PROC_REPO_WRITE:
1479 13b2bc37 2022-10-23 stsp argv[argc++] = (char *)"-W";
1480 13b2bc37 2022-10-23 stsp break;
1481 13b2bc37 2022-10-23 stsp default:
1482 13b2bc37 2022-10-23 stsp fatalx("invalid process id %d", proc_id);
1483 13b2bc37 2022-10-23 stsp }
1484 13b2bc37 2022-10-23 stsp
1485 585362fd 2022-10-31 op argv[argc++] = (char *)"-f";
1486 585362fd 2022-10-31 op argv[argc++] = (char *)confpath;
1487 585362fd 2022-10-31 op
1488 eec68231 2022-12-14 stsp if (repo_path) {
1489 d93ecf7d 2022-12-14 stsp argv[argc++] = (char *)"-P";
1490 eec68231 2022-12-14 stsp argv[argc++] = (char *)repo_path;
1491 d93ecf7d 2022-12-14 stsp }
1492 13b2bc37 2022-10-23 stsp
1493 13b2bc37 2022-10-23 stsp if (!daemonize)
1494 13b2bc37 2022-10-23 stsp argv[argc++] = (char *)"-d";
1495 13b2bc37 2022-10-23 stsp if (verbosity > 0)
1496 13b2bc37 2022-10-23 stsp argv[argc++] = (char *)"-v";
1497 13b2bc37 2022-10-23 stsp if (verbosity > 1)
1498 13b2bc37 2022-10-23 stsp argv[argc++] = (char *)"-v";
1499 13b2bc37 2022-10-23 stsp argv[argc++] = NULL;
1500 13b2bc37 2022-10-23 stsp
1501 13b2bc37 2022-10-23 stsp execvp(argv0, argv);
1502 13b2bc37 2022-10-23 stsp fatal("execvp");
1503 13b2bc37 2022-10-23 stsp }
1504 13b2bc37 2022-10-23 stsp
1505 13b2bc37 2022-10-23 stsp static void
1506 d93ecf7d 2022-12-14 stsp start_listener(char *argv0, const char *confpath, int daemonize, int verbosity)
1507 d93ecf7d 2022-12-14 stsp {
1508 b50a2b46 2022-12-29 stsp struct gotd_child_proc *proc = &gotd.listen_proc;
1509 d93ecf7d 2022-12-14 stsp
1510 d93ecf7d 2022-12-14 stsp proc->type = PROC_LISTEN;
1511 d93ecf7d 2022-12-14 stsp
1512 d93ecf7d 2022-12-14 stsp if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1513 d93ecf7d 2022-12-14 stsp PF_UNSPEC, proc->pipe) == -1)
1514 d93ecf7d 2022-12-14 stsp fatal("socketpair");
1515 d93ecf7d 2022-12-14 stsp
1516 d93ecf7d 2022-12-14 stsp proc->pid = start_child(proc->type, NULL, argv0, confpath,
1517 d93ecf7d 2022-12-14 stsp proc->pipe[1], daemonize, verbosity);
1518 d93ecf7d 2022-12-14 stsp imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1519 b50a2b46 2022-12-29 stsp proc->iev.handler = gotd_dispatch_listener;
1520 d93ecf7d 2022-12-14 stsp proc->iev.events = EV_READ;
1521 d93ecf7d 2022-12-14 stsp proc->iev.handler_arg = NULL;
1522 d93ecf7d 2022-12-14 stsp }
1523 d93ecf7d 2022-12-14 stsp
1524 b50a2b46 2022-12-29 stsp static const struct got_error *
1525 ae7c1b78 2023-01-10 stsp start_session_child(struct gotd_client *client, struct gotd_repo *repo,
1526 ae7c1b78 2023-01-10 stsp char *argv0, const char *confpath, int daemonize, int verbosity)
1527 ae7c1b78 2023-01-10 stsp {
1528 ae7c1b78 2023-01-10 stsp struct gotd_child_proc *proc;
1529 ae7c1b78 2023-01-10 stsp
1530 ae7c1b78 2023-01-10 stsp proc = calloc(1, sizeof(*proc));
1531 ae7c1b78 2023-01-10 stsp if (proc == NULL)
1532 ae7c1b78 2023-01-10 stsp return got_error_from_errno("calloc");
1533 ae7c1b78 2023-01-10 stsp
1534 ae7c1b78 2023-01-10 stsp proc->type = PROC_SESSION;
1535 ae7c1b78 2023-01-10 stsp if (strlcpy(proc->repo_name, repo->name,
1536 ae7c1b78 2023-01-10 stsp sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1537 ae7c1b78 2023-01-10 stsp fatalx("repository name too long: %s", repo->name);
1538 ae7c1b78 2023-01-10 stsp log_debug("starting client uid %d session for repository %s",
1539 ae7c1b78 2023-01-10 stsp client->euid, repo->name);
1540 ae7c1b78 2023-01-10 stsp if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1541 ae7c1b78 2023-01-10 stsp sizeof(proc->repo_path))
1542 ae7c1b78 2023-01-10 stsp fatalx("repository path too long: %s", repo->path);
1543 ae7c1b78 2023-01-10 stsp if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1544 ae7c1b78 2023-01-10 stsp PF_UNSPEC, proc->pipe) == -1)
1545 ae7c1b78 2023-01-10 stsp fatal("socketpair");
1546 ae7c1b78 2023-01-10 stsp proc->pid = start_child(proc->type, proc->repo_path, argv0,
1547 ae7c1b78 2023-01-10 stsp confpath, proc->pipe[1], daemonize, verbosity);
1548 ae7c1b78 2023-01-10 stsp imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1549 ae7c1b78 2023-01-10 stsp log_debug("proc %s %s is on fd %d",
1550 ae7c1b78 2023-01-10 stsp gotd_proc_names[proc->type], proc->repo_path,
1551 ae7c1b78 2023-01-10 stsp proc->pipe[0]);
1552 ae7c1b78 2023-01-10 stsp proc->iev.handler = gotd_dispatch_client_session;
1553 ae7c1b78 2023-01-10 stsp proc->iev.events = EV_READ;
1554 ae7c1b78 2023-01-10 stsp proc->iev.handler_arg = NULL;
1555 ae7c1b78 2023-01-10 stsp event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1556 ae7c1b78 2023-01-10 stsp gotd_dispatch_client_session, &proc->iev);
1557 ae7c1b78 2023-01-10 stsp gotd_imsg_event_add(&proc->iev);
1558 ae7c1b78 2023-01-10 stsp
1559 ae7c1b78 2023-01-10 stsp client->session = proc;
1560 ae7c1b78 2023-01-10 stsp return NULL;
1561 ae7c1b78 2023-01-10 stsp }
1562 ae7c1b78 2023-01-10 stsp
1563 ae7c1b78 2023-01-10 stsp static const struct got_error *
1564 b50a2b46 2022-12-29 stsp start_repo_child(struct gotd_client *client, enum gotd_procid proc_type,
1565 b50a2b46 2022-12-29 stsp struct gotd_repo *repo, char *argv0, const char *confpath,
1566 585362fd 2022-10-31 op int daemonize, int verbosity)
1567 13b2bc37 2022-10-23 stsp {
1568 13b2bc37 2022-10-23 stsp struct gotd_child_proc *proc;
1569 13b2bc37 2022-10-23 stsp
1570 b50a2b46 2022-12-29 stsp if (proc_type != PROC_REPO_READ && proc_type != PROC_REPO_WRITE)
1571 b50a2b46 2022-12-29 stsp return got_error_msg(GOT_ERR_NOT_IMPL, "bad process type");
1572 7fdc3e58 2022-12-30 mark
1573 b50a2b46 2022-12-29 stsp proc = calloc(1, sizeof(*proc));
1574 b50a2b46 2022-12-29 stsp if (proc == NULL)
1575 b50a2b46 2022-12-29 stsp return got_error_from_errno("calloc");
1576 13b2bc37 2022-10-23 stsp
1577 b50a2b46 2022-12-29 stsp proc->type = proc_type;
1578 b50a2b46 2022-12-29 stsp if (strlcpy(proc->repo_name, repo->name,
1579 b50a2b46 2022-12-29 stsp sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1580 b50a2b46 2022-12-29 stsp fatalx("repository name too long: %s", repo->name);
1581 b50a2b46 2022-12-29 stsp log_debug("starting %s for repository %s",
1582 b50a2b46 2022-12-29 stsp proc->type == PROC_REPO_READ ? "reader" : "writer", repo->name);
1583 9b7f22a6 2023-01-08 stsp if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1584 9b7f22a6 2023-01-08 stsp sizeof(proc->repo_path))
1585 9b7f22a6 2023-01-08 stsp fatalx("repository path too long: %s", repo->path);
1586 b50a2b46 2022-12-29 stsp if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1587 b50a2b46 2022-12-29 stsp PF_UNSPEC, proc->pipe) == -1)
1588 b50a2b46 2022-12-29 stsp fatal("socketpair");
1589 b50a2b46 2022-12-29 stsp proc->pid = start_child(proc->type, proc->repo_path, argv0,
1590 b50a2b46 2022-12-29 stsp confpath, proc->pipe[1], daemonize, verbosity);
1591 b50a2b46 2022-12-29 stsp imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1592 b50a2b46 2022-12-29 stsp log_debug("proc %s %s is on fd %d",
1593 b50a2b46 2022-12-29 stsp gotd_proc_names[proc->type], proc->repo_path,
1594 b50a2b46 2022-12-29 stsp proc->pipe[0]);
1595 b50a2b46 2022-12-29 stsp proc->iev.handler = gotd_dispatch_repo_child;
1596 b50a2b46 2022-12-29 stsp proc->iev.events = EV_READ;
1597 b50a2b46 2022-12-29 stsp proc->iev.handler_arg = NULL;
1598 b50a2b46 2022-12-29 stsp event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1599 b50a2b46 2022-12-29 stsp gotd_dispatch_repo_child, &proc->iev);
1600 b50a2b46 2022-12-29 stsp gotd_imsg_event_add(&proc->iev);
1601 b50a2b46 2022-12-29 stsp
1602 b50a2b46 2022-12-29 stsp if (proc->type == PROC_REPO_READ)
1603 b50a2b46 2022-12-29 stsp client->repo_read = proc;
1604 b50a2b46 2022-12-29 stsp else
1605 b50a2b46 2022-12-29 stsp client->repo_write = proc;
1606 5e25db14 2022-12-29 stsp
1607 5e25db14 2022-12-29 stsp return NULL;
1608 5e25db14 2022-12-29 stsp }
1609 5e25db14 2022-12-29 stsp
1610 5e25db14 2022-12-29 stsp static const struct got_error *
1611 5e25db14 2022-12-29 stsp start_auth_child(struct gotd_client *client, int required_auth,
1612 5e25db14 2022-12-29 stsp struct gotd_repo *repo, char *argv0, const char *confpath,
1613 5e25db14 2022-12-29 stsp int daemonize, int verbosity)
1614 5e25db14 2022-12-29 stsp {
1615 365cf0f3 2022-12-29 stsp const struct got_error *err = NULL;
1616 5e25db14 2022-12-29 stsp struct gotd_child_proc *proc;
1617 5e25db14 2022-12-29 stsp struct gotd_imsg_auth iauth;
1618 365cf0f3 2022-12-29 stsp int fd;
1619 5e25db14 2022-12-29 stsp
1620 5e25db14 2022-12-29 stsp memset(&iauth, 0, sizeof(iauth));
1621 365cf0f3 2022-12-29 stsp
1622 365cf0f3 2022-12-29 stsp fd = dup(client->fd);
1623 365cf0f3 2022-12-29 stsp if (fd == -1)
1624 365cf0f3 2022-12-29 stsp return got_error_from_errno("dup");
1625 5e25db14 2022-12-29 stsp
1626 5e25db14 2022-12-29 stsp proc = calloc(1, sizeof(*proc));
1627 365cf0f3 2022-12-29 stsp if (proc == NULL) {
1628 365cf0f3 2022-12-29 stsp err = got_error_from_errno("calloc");
1629 365cf0f3 2022-12-29 stsp close(fd);
1630 365cf0f3 2022-12-29 stsp return err;
1631 365cf0f3 2022-12-29 stsp }
1632 5e25db14 2022-12-29 stsp
1633 5e25db14 2022-12-29 stsp proc->type = PROC_AUTH;
1634 5e25db14 2022-12-29 stsp if (strlcpy(proc->repo_name, repo->name,
1635 5e25db14 2022-12-29 stsp sizeof(proc->repo_name)) >= sizeof(proc->repo_name))
1636 5e25db14 2022-12-29 stsp fatalx("repository name too long: %s", repo->name);
1637 5e25db14 2022-12-29 stsp log_debug("starting auth for uid %d repository %s",
1638 5e25db14 2022-12-29 stsp client->euid, repo->name);
1639 9b7f22a6 2023-01-08 stsp if (strlcpy(proc->repo_path, repo->path, sizeof(proc->repo_path)) >=
1640 9b7f22a6 2023-01-08 stsp sizeof(proc->repo_path))
1641 9b7f22a6 2023-01-08 stsp fatalx("repository path too long: %s", repo->path);
1642 5e25db14 2022-12-29 stsp if (socketpair(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK,
1643 5e25db14 2022-12-29 stsp PF_UNSPEC, proc->pipe) == -1)
1644 5e25db14 2022-12-29 stsp fatal("socketpair");
1645 5e25db14 2022-12-29 stsp proc->pid = start_child(proc->type, proc->repo_path, argv0,
1646 5e25db14 2022-12-29 stsp confpath, proc->pipe[1], daemonize, verbosity);
1647 5e25db14 2022-12-29 stsp imsg_init(&proc->iev.ibuf, proc->pipe[0]);
1648 5e25db14 2022-12-29 stsp log_debug("proc %s %s is on fd %d",
1649 5e25db14 2022-12-29 stsp gotd_proc_names[proc->type], proc->repo_path,
1650 5e25db14 2022-12-29 stsp proc->pipe[0]);
1651 5e25db14 2022-12-29 stsp proc->iev.handler = gotd_dispatch_auth_child;
1652 5e25db14 2022-12-29 stsp proc->iev.events = EV_READ;
1653 5e25db14 2022-12-29 stsp proc->iev.handler_arg = NULL;
1654 5e25db14 2022-12-29 stsp event_set(&proc->iev.ev, proc->iev.ibuf.fd, EV_READ,
1655 5e25db14 2022-12-29 stsp gotd_dispatch_auth_child, &proc->iev);
1656 5e25db14 2022-12-29 stsp gotd_imsg_event_add(&proc->iev);
1657 5e25db14 2022-12-29 stsp
1658 5e25db14 2022-12-29 stsp iauth.euid = client->euid;
1659 5e25db14 2022-12-29 stsp iauth.egid = client->egid;
1660 5e25db14 2022-12-29 stsp iauth.required_auth = required_auth;
1661 5e25db14 2022-12-29 stsp iauth.client_id = client->id;
1662 5e25db14 2022-12-29 stsp if (gotd_imsg_compose_event(&proc->iev, GOTD_IMSG_AUTHENTICATE,
1663 365cf0f3 2022-12-29 stsp PROC_GOTD, fd, &iauth, sizeof(iauth)) == -1) {
1664 5e25db14 2022-12-29 stsp log_warn("imsg compose AUTHENTICATE");
1665 365cf0f3 2022-12-29 stsp close(fd);
1666 365cf0f3 2022-12-29 stsp /* Let the auth_timeout handler tidy up. */
1667 365cf0f3 2022-12-29 stsp }
1668 b50a2b46 2022-12-29 stsp
1669 5e25db14 2022-12-29 stsp client->auth = proc;
1670 5e25db14 2022-12-29 stsp client->required_auth = required_auth;
1671 b50a2b46 2022-12-29 stsp return NULL;
1672 eec68231 2022-12-14 stsp }
1673 eec68231 2022-12-14 stsp
1674 eec68231 2022-12-14 stsp static void
1675 eec68231 2022-12-14 stsp apply_unveil_repo_readonly(const char *repo_path)
1676 eec68231 2022-12-14 stsp {
1677 eec68231 2022-12-14 stsp if (unveil(repo_path, "r") == -1)
1678 eec68231 2022-12-14 stsp fatal("unveil %s", repo_path);
1679 44587340 2022-12-30 stsp
1680 44587340 2022-12-30 stsp if (unveil(NULL, NULL) == -1)
1681 44587340 2022-12-30 stsp fatal("unveil");
1682 44587340 2022-12-30 stsp }
1683 44587340 2022-12-30 stsp
1684 44587340 2022-12-30 stsp static void
1685 ae7c1b78 2023-01-10 stsp apply_unveil_repo_readwrite(const char *repo_path)
1686 ae7c1b78 2023-01-10 stsp {
1687 ae7c1b78 2023-01-10 stsp if (unveil(repo_path, "rwc") == -1)
1688 ae7c1b78 2023-01-10 stsp fatal("unveil %s", repo_path);
1689 ae7c1b78 2023-01-10 stsp
1690 ae7c1b78 2023-01-10 stsp if (unveil(GOT_TMPDIR_STR, "rwc") == -1)
1691 ae7c1b78 2023-01-10 stsp fatal("unveil %s", GOT_TMPDIR_STR);
1692 ae7c1b78 2023-01-10 stsp
1693 ae7c1b78 2023-01-10 stsp if (unveil(NULL, NULL) == -1)
1694 ae7c1b78 2023-01-10 stsp fatal("unveil");
1695 ae7c1b78 2023-01-10 stsp }
1696 ae7c1b78 2023-01-10 stsp
1697 ae7c1b78 2023-01-10 stsp static void
1698 44587340 2022-12-30 stsp apply_unveil_none(void)
1699 44587340 2022-12-30 stsp {
1700 44587340 2022-12-30 stsp if (unveil("/", "") == -1)
1701 44587340 2022-12-30 stsp fatal("unveil");
1702 eec68231 2022-12-14 stsp
1703 eec68231 2022-12-14 stsp if (unveil(NULL, NULL) == -1)
1704 eec68231 2022-12-14 stsp fatal("unveil");
1705 13b2bc37 2022-10-23 stsp }
1706 13b2bc37 2022-10-23 stsp
1707 13b2bc37 2022-10-23 stsp static void
1708 ae7c1b78 2023-01-10 stsp apply_unveil_selfexec(void)
1709 13b2bc37 2022-10-23 stsp {
1710 b50a2b46 2022-12-29 stsp if (unveil(gotd.argv0, "x") == -1)
1711 b50a2b46 2022-12-29 stsp fatal("unveil %s", gotd.argv0);
1712 b50a2b46 2022-12-29 stsp
1713 13b2bc37 2022-10-23 stsp if (unveil(NULL, NULL) == -1)
1714 13b2bc37 2022-10-23 stsp fatal("unveil");
1715 13b2bc37 2022-10-23 stsp }
1716 13b2bc37 2022-10-23 stsp
1717 13b2bc37 2022-10-23 stsp int
1718 13b2bc37 2022-10-23 stsp main(int argc, char **argv)
1719 13b2bc37 2022-10-23 stsp {
1720 13b2bc37 2022-10-23 stsp const struct got_error *error = NULL;
1721 13b2bc37 2022-10-23 stsp int ch, fd = -1, daemonize = 1, verbosity = 0, noaction = 0;
1722 13b2bc37 2022-10-23 stsp const char *confpath = GOTD_CONF_PATH;
1723 13b2bc37 2022-10-23 stsp char *argv0 = argv[0];
1724 13b2bc37 2022-10-23 stsp char title[2048];
1725 13b2bc37 2022-10-23 stsp struct passwd *pw = NULL;
1726 13b2bc37 2022-10-23 stsp char *repo_path = NULL;
1727 13b2bc37 2022-10-23 stsp enum gotd_procid proc_id = PROC_GOTD;
1728 13b2bc37 2022-10-23 stsp struct event evsigint, evsigterm, evsighup, evsigusr1;
1729 13b2bc37 2022-10-23 stsp int *pack_fds = NULL, *temp_fds = NULL;
1730 13b2bc37 2022-10-23 stsp
1731 13b2bc37 2022-10-23 stsp log_init(1, LOG_DAEMON); /* Log to stderr until daemonized. */
1732 13b2bc37 2022-10-23 stsp
1733 ae7c1b78 2023-01-10 stsp while ((ch = getopt(argc, argv, "Adf:LnP:RSvW")) != -1) {
1734 13b2bc37 2022-10-23 stsp switch (ch) {
1735 5e25db14 2022-12-29 stsp case 'A':
1736 5e25db14 2022-12-29 stsp proc_id = PROC_AUTH;
1737 5e25db14 2022-12-29 stsp break;
1738 13b2bc37 2022-10-23 stsp case 'd':
1739 13b2bc37 2022-10-23 stsp daemonize = 0;
1740 13b2bc37 2022-10-23 stsp break;
1741 13b2bc37 2022-10-23 stsp case 'f':
1742 13b2bc37 2022-10-23 stsp confpath = optarg;
1743 13b2bc37 2022-10-23 stsp break;
1744 d93ecf7d 2022-12-14 stsp case 'L':
1745 d93ecf7d 2022-12-14 stsp proc_id = PROC_LISTEN;
1746 d93ecf7d 2022-12-14 stsp break;
1747 13b2bc37 2022-10-23 stsp case 'n':
1748 13b2bc37 2022-10-23 stsp noaction = 1;
1749 13b2bc37 2022-10-23 stsp break;
1750 6f319063 2022-10-27 stsp case 'P':
1751 6f319063 2022-10-27 stsp repo_path = realpath(optarg, NULL);
1752 6f319063 2022-10-27 stsp if (repo_path == NULL)
1753 6f319063 2022-10-27 stsp fatal("realpath '%s'", optarg);
1754 13b2bc37 2022-10-23 stsp break;
1755 13b2bc37 2022-10-23 stsp case 'R':
1756 13b2bc37 2022-10-23 stsp proc_id = PROC_REPO_READ;
1757 13b2bc37 2022-10-23 stsp break;
1758 ae7c1b78 2023-01-10 stsp case 'S':
1759 ae7c1b78 2023-01-10 stsp proc_id = PROC_SESSION;
1760 ae7c1b78 2023-01-10 stsp break;
1761 6f319063 2022-10-27 stsp case 'v':
1762 6f319063 2022-10-27 stsp if (verbosity < 3)
1763 6f319063 2022-10-27 stsp verbosity++;
1764 6f319063 2022-10-27 stsp break;
1765 13b2bc37 2022-10-23 stsp case 'W':
1766 13b2bc37 2022-10-23 stsp proc_id = PROC_REPO_WRITE;
1767 13b2bc37 2022-10-23 stsp break;
1768 13b2bc37 2022-10-23 stsp default:
1769 13b2bc37 2022-10-23 stsp usage();
1770 13b2bc37 2022-10-23 stsp }
1771 13b2bc37 2022-10-23 stsp }
1772 13b2bc37 2022-10-23 stsp
1773 13b2bc37 2022-10-23 stsp argc -= optind;
1774 13b2bc37 2022-10-23 stsp argv += optind;
1775 13b2bc37 2022-10-23 stsp
1776 13b2bc37 2022-10-23 stsp if (argc != 0)
1777 13b2bc37 2022-10-23 stsp usage();
1778 13b2bc37 2022-10-23 stsp
1779 b50a2b46 2022-12-29 stsp /* Require an absolute path in argv[0] for reliable re-exec. */
1780 b50a2b46 2022-12-29 stsp if (!got_path_is_absolute(argv0))
1781 b50a2b46 2022-12-29 stsp fatalx("bad path \"%s\": must be an absolute path", argv0);
1782 b50a2b46 2022-12-29 stsp
1783 b50a2b46 2022-12-29 stsp if (geteuid() && (proc_id == PROC_GOTD || proc_id == PROC_LISTEN))
1784 13b2bc37 2022-10-23 stsp fatalx("need root privileges");
1785 13b2bc37 2022-10-23 stsp
1786 13b2bc37 2022-10-23 stsp log_init(daemonize ? 0 : 1, LOG_DAEMON);
1787 13b2bc37 2022-10-23 stsp log_setverbose(verbosity);
1788 13b2bc37 2022-10-23 stsp
1789 13b2bc37 2022-10-23 stsp if (parse_config(confpath, proc_id, &gotd) != 0)
1790 13b2bc37 2022-10-23 stsp return 1;
1791 13b2bc37 2022-10-23 stsp
1792 b50a2b46 2022-12-29 stsp gotd.argv0 = argv0;
1793 b50a2b46 2022-12-29 stsp gotd.daemonize = daemonize;
1794 b50a2b46 2022-12-29 stsp gotd.verbosity = verbosity;
1795 b50a2b46 2022-12-29 stsp gotd.confpath = confpath;
1796 b50a2b46 2022-12-29 stsp
1797 13b2bc37 2022-10-23 stsp if (proc_id == PROC_GOTD &&
1798 13b2bc37 2022-10-23 stsp (gotd.nrepos == 0 || TAILQ_EMPTY(&gotd.repos)))
1799 13b2bc37 2022-10-23 stsp fatalx("no repository defined in configuration file");
1800 13b2bc37 2022-10-23 stsp
1801 13b2bc37 2022-10-23 stsp pw = getpwnam(gotd.user_name);
1802 13b2bc37 2022-10-23 stsp if (pw == NULL)
1803 898c8f8f 2022-12-29 op fatalx("user %s not found", gotd.user_name);
1804 13b2bc37 2022-10-23 stsp
1805 13b2bc37 2022-10-23 stsp if (pw->pw_uid == 0) {
1806 13b2bc37 2022-10-23 stsp fatalx("cannot run %s as %s: the user running %s "
1807 13b2bc37 2022-10-23 stsp "must not be the superuser",
1808 13b2bc37 2022-10-23 stsp getprogname(), pw->pw_name, getprogname());
1809 13b2bc37 2022-10-23 stsp }
1810 13b2bc37 2022-10-23 stsp
1811 d93ecf7d 2022-12-14 stsp if (proc_id == PROC_LISTEN &&
1812 13b2bc37 2022-10-23 stsp !got_path_is_absolute(gotd.unix_socket_path))
1813 13b2bc37 2022-10-23 stsp fatalx("bad unix socket path \"%s\": must be an absolute path",
1814 13b2bc37 2022-10-23 stsp gotd.unix_socket_path);
1815 13b2bc37 2022-10-23 stsp
1816 13b2bc37 2022-10-23 stsp if (noaction)
1817 13b2bc37 2022-10-23 stsp return 0;
1818 13b2bc37 2022-10-23 stsp
1819 b1142068 2022-12-05 stsp if (proc_id == PROC_GOTD) {
1820 d93ecf7d 2022-12-14 stsp gotd.pid = getpid();
1821 d93ecf7d 2022-12-14 stsp snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1822 d93ecf7d 2022-12-14 stsp start_listener(argv0, confpath, daemonize, verbosity);
1823 d93ecf7d 2022-12-14 stsp arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
1824 d93ecf7d 2022-12-14 stsp if (daemonize && daemon(1, 0) == -1)
1825 d93ecf7d 2022-12-14 stsp fatal("daemon");
1826 d93ecf7d 2022-12-14 stsp } else if (proc_id == PROC_LISTEN) {
1827 d93ecf7d 2022-12-14 stsp snprintf(title, sizeof(title), "%s", gotd_proc_names[proc_id]);
1828 b1142068 2022-12-05 stsp if (verbosity) {
1829 b1142068 2022-12-05 stsp log_info("socket: %s", gotd.unix_socket_path);
1830 b1142068 2022-12-05 stsp log_info("user: %s", pw->pw_name);
1831 b1142068 2022-12-05 stsp }
1832 13b2bc37 2022-10-23 stsp
1833 13b2bc37 2022-10-23 stsp fd = unix_socket_listen(gotd.unix_socket_path, pw->pw_uid,
1834 6f854dde 2023-01-04 stsp pw->pw_gid);
1835 13b2bc37 2022-10-23 stsp if (fd == -1) {
1836 13b2bc37 2022-10-23 stsp fatal("cannot listen on unix socket %s",
1837 13b2bc37 2022-10-23 stsp gotd.unix_socket_path);
1838 13b2bc37 2022-10-23 stsp }
1839 eec68231 2022-12-14 stsp if (daemonize && daemon(0, 0) == -1)
1840 13b2bc37 2022-10-23 stsp fatal("daemon");
1841 5e25db14 2022-12-29 stsp } else if (proc_id == PROC_AUTH) {
1842 5e25db14 2022-12-29 stsp snprintf(title, sizeof(title), "%s %s",
1843 5e25db14 2022-12-29 stsp gotd_proc_names[proc_id], repo_path);
1844 5e25db14 2022-12-29 stsp if (daemonize && daemon(0, 0) == -1)
1845 5e25db14 2022-12-29 stsp fatal("daemon");
1846 ae7c1b78 2023-01-10 stsp } else if (proc_id == PROC_REPO_READ || proc_id == PROC_REPO_WRITE ||
1847 ae7c1b78 2023-01-10 stsp proc_id == PROC_SESSION) {
1848 13b2bc37 2022-10-23 stsp error = got_repo_pack_fds_open(&pack_fds);
1849 13b2bc37 2022-10-23 stsp if (error != NULL)
1850 13b2bc37 2022-10-23 stsp fatalx("cannot open pack tempfiles: %s", error->msg);
1851 13b2bc37 2022-10-23 stsp error = got_repo_temp_fds_open(&temp_fds);
1852 13b2bc37 2022-10-23 stsp if (error != NULL)
1853 13b2bc37 2022-10-23 stsp fatalx("cannot open pack tempfiles: %s", error->msg);
1854 13b2bc37 2022-10-23 stsp if (repo_path == NULL)
1855 13b2bc37 2022-10-23 stsp fatalx("repository path not specified");
1856 13b2bc37 2022-10-23 stsp snprintf(title, sizeof(title), "%s %s",
1857 13b2bc37 2022-10-23 stsp gotd_proc_names[proc_id], repo_path);
1858 eec68231 2022-12-14 stsp if (daemonize && daemon(0, 0) == -1)
1859 13b2bc37 2022-10-23 stsp fatal("daemon");
1860 13b2bc37 2022-10-23 stsp } else
1861 13b2bc37 2022-10-23 stsp fatal("invalid process id %d", proc_id);
1862 13b2bc37 2022-10-23 stsp
1863 13b2bc37 2022-10-23 stsp setproctitle("%s", title);
1864 13b2bc37 2022-10-23 stsp log_procinit(title);
1865 13b2bc37 2022-10-23 stsp
1866 13b2bc37 2022-10-23 stsp /* Drop root privileges. */
1867 13b2bc37 2022-10-23 stsp if (setgid(pw->pw_gid) == -1)
1868 13b2bc37 2022-10-23 stsp fatal("setgid %d failed", pw->pw_gid);
1869 13b2bc37 2022-10-23 stsp if (setuid(pw->pw_uid) == -1)
1870 13b2bc37 2022-10-23 stsp fatal("setuid %d failed", pw->pw_uid);
1871 13b2bc37 2022-10-23 stsp
1872 13b2bc37 2022-10-23 stsp event_init();
1873 13b2bc37 2022-10-23 stsp
1874 13b2bc37 2022-10-23 stsp switch (proc_id) {
1875 13b2bc37 2022-10-23 stsp case PROC_GOTD:
1876 13b2bc37 2022-10-23 stsp #ifndef PROFILE
1877 ae7c1b78 2023-01-10 stsp /* "exec" promise will be limited to argv[0] via unveil(2). */
1878 ae7c1b78 2023-01-10 stsp if (pledge("stdio proc exec sendfd recvfd unveil", NULL) == -1)
1879 13b2bc37 2022-10-23 stsp err(1, "pledge");
1880 13b2bc37 2022-10-23 stsp #endif
1881 13b2bc37 2022-10-23 stsp break;
1882 d93ecf7d 2022-12-14 stsp case PROC_LISTEN:
1883 d93ecf7d 2022-12-14 stsp #ifndef PROFILE
1884 77f619a8 2023-01-04 stsp if (pledge("stdio sendfd unix unveil", NULL) == -1)
1885 d93ecf7d 2022-12-14 stsp err(1, "pledge");
1886 d93ecf7d 2022-12-14 stsp #endif
1887 77f619a8 2023-01-04 stsp /*
1888 77f619a8 2023-01-04 stsp * Ensure that AF_UNIX bind(2) cannot be used with any other
1889 77f619a8 2023-01-04 stsp * sockets by revoking all filesystem access via unveil(2).
1890 77f619a8 2023-01-04 stsp */
1891 77f619a8 2023-01-04 stsp apply_unveil_none();
1892 77f619a8 2023-01-04 stsp
1893 40b85cca 2023-01-03 stsp listen_main(title, fd, gotd.connection_limits,
1894 40b85cca 2023-01-03 stsp gotd.nconnection_limits);
1895 d93ecf7d 2022-12-14 stsp /* NOTREACHED */
1896 d93ecf7d 2022-12-14 stsp break;
1897 5e25db14 2022-12-29 stsp case PROC_AUTH:
1898 5e25db14 2022-12-29 stsp #ifndef PROFILE
1899 44587340 2022-12-30 stsp if (pledge("stdio getpw recvfd unix unveil", NULL) == -1)
1900 5e25db14 2022-12-29 stsp err(1, "pledge");
1901 5e25db14 2022-12-29 stsp #endif
1902 44587340 2022-12-30 stsp /*
1903 44587340 2022-12-30 stsp * We need the "unix" pledge promise for getpeername(2) only.
1904 44587340 2022-12-30 stsp * Ensure that AF_UNIX bind(2) cannot be used by revoking all
1905 44587340 2022-12-30 stsp * filesystem access via unveil(2). Access to password database
1906 44587340 2022-12-30 stsp * files will still work since "getpw" bypasses unveil(2).
1907 44587340 2022-12-30 stsp */
1908 44587340 2022-12-30 stsp apply_unveil_none();
1909 44587340 2022-12-30 stsp
1910 5e25db14 2022-12-29 stsp auth_main(title, &gotd.repos, repo_path);
1911 5e25db14 2022-12-29 stsp /* NOTREACHED */
1912 5e25db14 2022-12-29 stsp break;
1913 ae7c1b78 2023-01-10 stsp case PROC_SESSION:
1914 ae7c1b78 2023-01-10 stsp #ifndef PROFILE
1915 ae7c1b78 2023-01-10 stsp /*
1916 ae7c1b78 2023-01-10 stsp * The "recvfd" promise is only needed during setup and
1917 ae7c1b78 2023-01-10 stsp * will be removed in a later pledge(2) call.
1918 ae7c1b78 2023-01-10 stsp */
1919 ae7c1b78 2023-01-10 stsp if (pledge("stdio rpath wpath cpath recvfd sendfd fattr flock "
1920 ae7c1b78 2023-01-10 stsp "unveil", NULL) == -1)
1921 ae7c1b78 2023-01-10 stsp err(1, "pledge");
1922 ae7c1b78 2023-01-10 stsp #endif
1923 ae7c1b78 2023-01-10 stsp apply_unveil_repo_readwrite(repo_path);
1924 ae7c1b78 2023-01-10 stsp session_main(title, repo_path, pack_fds, temp_fds,
1925 ae7c1b78 2023-01-10 stsp &gotd.request_timeout);
1926 ae7c1b78 2023-01-10 stsp /* NOTREACHED */
1927 ae7c1b78 2023-01-10 stsp break;
1928 13b2bc37 2022-10-23 stsp case PROC_REPO_READ:
1929 13b2bc37 2022-10-23 stsp #ifndef PROFILE
1930 eec68231 2022-12-14 stsp if (pledge("stdio rpath recvfd unveil", NULL) == -1)
1931 13b2bc37 2022-10-23 stsp err(1, "pledge");
1932 13b2bc37 2022-10-23 stsp #endif
1933 eec68231 2022-12-14 stsp apply_unveil_repo_readonly(repo_path);
1934 eec68231 2022-12-14 stsp repo_read_main(title, repo_path, pack_fds, temp_fds);
1935 13b2bc37 2022-10-23 stsp /* NOTREACHED */
1936 13b2bc37 2022-10-23 stsp exit(0);
1937 13b2bc37 2022-10-23 stsp case PROC_REPO_WRITE:
1938 13b2bc37 2022-10-23 stsp #ifndef PROFILE
1939 eec68231 2022-12-14 stsp if (pledge("stdio rpath recvfd unveil", NULL) == -1)
1940 13b2bc37 2022-10-23 stsp err(1, "pledge");
1941 13b2bc37 2022-10-23 stsp #endif
1942 eec68231 2022-12-14 stsp apply_unveil_repo_readonly(repo_path);
1943 eec68231 2022-12-14 stsp repo_write_main(title, repo_path, pack_fds, temp_fds);
1944 13b2bc37 2022-10-23 stsp /* NOTREACHED */
1945 13b2bc37 2022-10-23 stsp exit(0);
1946 13b2bc37 2022-10-23 stsp default:
1947 13b2bc37 2022-10-23 stsp fatal("invalid process id %d", proc_id);
1948 13b2bc37 2022-10-23 stsp }
1949 13b2bc37 2022-10-23 stsp
1950 13b2bc37 2022-10-23 stsp if (proc_id != PROC_GOTD)
1951 13b2bc37 2022-10-23 stsp fatal("invalid process id %d", proc_id);
1952 13b2bc37 2022-10-23 stsp
1953 ae7c1b78 2023-01-10 stsp apply_unveil_selfexec();
1954 13b2bc37 2022-10-23 stsp
1955 13b2bc37 2022-10-23 stsp signal_set(&evsigint, SIGINT, gotd_sighdlr, NULL);
1956 13b2bc37 2022-10-23 stsp signal_set(&evsigterm, SIGTERM, gotd_sighdlr, NULL);
1957 13b2bc37 2022-10-23 stsp signal_set(&evsighup, SIGHUP, gotd_sighdlr, NULL);
1958 13b2bc37 2022-10-23 stsp signal_set(&evsigusr1, SIGUSR1, gotd_sighdlr, NULL);
1959 13b2bc37 2022-10-23 stsp signal(SIGPIPE, SIG_IGN);
1960 13b2bc37 2022-10-23 stsp
1961 13b2bc37 2022-10-23 stsp signal_add(&evsigint, NULL);
1962 13b2bc37 2022-10-23 stsp signal_add(&evsigterm, NULL);
1963 13b2bc37 2022-10-23 stsp signal_add(&evsighup, NULL);
1964 13b2bc37 2022-10-23 stsp signal_add(&evsigusr1, NULL);
1965 13b2bc37 2022-10-23 stsp
1966 b50a2b46 2022-12-29 stsp gotd_imsg_event_add(&gotd.listen_proc.iev);
1967 13b2bc37 2022-10-23 stsp
1968 13b2bc37 2022-10-23 stsp event_dispatch();
1969 13b2bc37 2022-10-23 stsp
1970 13b2bc37 2022-10-23 stsp free(repo_path);
1971 ae7c1b78 2023-01-10 stsp gotd_shutdown();
1972 ae7c1b78 2023-01-10 stsp
1973 13b2bc37 2022-10-23 stsp return 0;
1974 13b2bc37 2022-10-23 stsp }