Blame


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