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