Blame


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