Blame


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