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