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