Blame


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