Blame


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