Blame


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