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