Blame


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