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