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