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