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