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