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