2 62ee7d94 2023-01-10 thomas * Copyright (c) 2022, 2023 Stefan Sperling <stsp@openbsd.org>
4 62ee7d94 2023-01-10 thomas * Permission to use, copy, modify, and distribute this software for any
5 62ee7d94 2023-01-10 thomas * purpose with or without fee is hereby granted, provided that the above
6 62ee7d94 2023-01-10 thomas * copyright notice and this permission notice appear in all copies.
8 62ee7d94 2023-01-10 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 62ee7d94 2023-01-10 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 62ee7d94 2023-01-10 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 62ee7d94 2023-01-10 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 62ee7d94 2023-01-10 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 62ee7d94 2023-01-10 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 62ee7d94 2023-01-10 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 62ee7d94 2023-01-10 thomas #include <sys/types.h>
18 62ee7d94 2023-01-10 thomas #include <sys/queue.h>
19 62ee7d94 2023-01-10 thomas #include <sys/socket.h>
20 851a5b48 2023-02-03 thomas #include <sys/stat.h>
21 62ee7d94 2023-01-10 thomas #include <sys/uio.h>
23 62ee7d94 2023-01-10 thomas #include <errno.h>
24 62ee7d94 2023-01-10 thomas #include <event.h>
25 62ee7d94 2023-01-10 thomas #include <limits.h>
26 62ee7d94 2023-01-10 thomas #include <signal.h>
27 62ee7d94 2023-01-10 thomas #include <stdint.h>
28 62ee7d94 2023-01-10 thomas #include <stdio.h>
29 62ee7d94 2023-01-10 thomas #include <stdlib.h>
30 62ee7d94 2023-01-10 thomas #include <string.h>
31 62ee7d94 2023-01-10 thomas #include <imsg.h>
32 62ee7d94 2023-01-10 thomas #include <unistd.h>
34 62ee7d94 2023-01-10 thomas #include "got_error.h"
35 62ee7d94 2023-01-10 thomas #include "got_repository.h"
36 62ee7d94 2023-01-10 thomas #include "got_object.h"
37 62ee7d94 2023-01-10 thomas #include "got_path.h"
38 62ee7d94 2023-01-10 thomas #include "got_reference.h"
39 62ee7d94 2023-01-10 thomas #include "got_opentemp.h"
41 be288a59 2023-02-23 thomas #include "got_lib_hash.h"
42 62ee7d94 2023-01-10 thomas #include "got_lib_delta.h"
43 62ee7d94 2023-01-10 thomas #include "got_lib_object.h"
44 62ee7d94 2023-01-10 thomas #include "got_lib_object_cache.h"
45 62ee7d94 2023-01-10 thomas #include "got_lib_pack.h"
46 62ee7d94 2023-01-10 thomas #include "got_lib_repository.h"
47 62ee7d94 2023-01-10 thomas #include "got_lib_gitproto.h"
49 62ee7d94 2023-01-10 thomas #include "gotd.h"
50 62ee7d94 2023-01-10 thomas #include "log.h"
51 62ee7d94 2023-01-10 thomas #include "session.h"
54 62ee7d94 2023-01-10 thomas static struct gotd_session {
55 62ee7d94 2023-01-10 thomas pid_t pid;
56 62ee7d94 2023-01-10 thomas const char *title;
57 62ee7d94 2023-01-10 thomas struct got_repository *repo;
58 62ee7d94 2023-01-10 thomas int *pack_fds;
59 62ee7d94 2023-01-10 thomas int *temp_fds;
60 62ee7d94 2023-01-10 thomas struct gotd_imsgev parent_iev;
61 62ee7d94 2023-01-10 thomas struct timeval request_timeout;
62 62ee7d94 2023-01-10 thomas } gotd_session;
64 62ee7d94 2023-01-10 thomas static struct gotd_session_client {
65 7b1db75e 2023-01-14 thomas enum gotd_session_state state;
66 62ee7d94 2023-01-10 thomas int is_writing;
67 62ee7d94 2023-01-10 thomas struct gotd_client_capability *capabilities;
68 62ee7d94 2023-01-10 thomas size_t ncapa_alloc;
69 62ee7d94 2023-01-10 thomas size_t ncapabilities;
70 62ee7d94 2023-01-10 thomas uint32_t id;
72 62ee7d94 2023-01-10 thomas int delta_cache_fd;
73 62ee7d94 2023-01-10 thomas struct gotd_imsgev iev;
74 62ee7d94 2023-01-10 thomas struct gotd_imsgev repo_child_iev;
75 62ee7d94 2023-01-10 thomas struct event tmo;
76 62ee7d94 2023-01-10 thomas uid_t euid;
77 62ee7d94 2023-01-10 thomas gid_t egid;
78 62ee7d94 2023-01-10 thomas char *packfile_path;
79 62ee7d94 2023-01-10 thomas char *packidx_path;
80 62ee7d94 2023-01-10 thomas int nref_updates;
81 98c7fd82 2023-01-23 thomas int accept_flush_pkt;
82 62ee7d94 2023-01-10 thomas } gotd_session_client;
84 62ee7d94 2023-01-10 thomas void gotd_session_sighdlr(int sig, short event, void *arg);
85 62ee7d94 2023-01-10 thomas static void gotd_session_shutdown(void);
87 62ee7d94 2023-01-10 thomas static void
88 62ee7d94 2023-01-10 thomas disconnect(struct gotd_session_client *client)
90 62ee7d94 2023-01-10 thomas log_debug("uid %d: disconnecting", client->euid);
92 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&gotd_session.parent_iev,
93 62ee7d94 2023-01-10 thomas GOTD_IMSG_DISCONNECT, PROC_SESSION, -1, NULL, 0) == -1)
94 62ee7d94 2023-01-10 thomas log_warn("imsg compose DISCONNECT");
96 62ee7d94 2023-01-10 thomas imsg_clear(&client->repo_child_iev.ibuf);
97 62ee7d94 2023-01-10 thomas event_del(&client->repo_child_iev.ev);
98 62ee7d94 2023-01-10 thomas evtimer_del(&client->tmo);
99 62ee7d94 2023-01-10 thomas close(client->fd);
100 62ee7d94 2023-01-10 thomas if (client->delta_cache_fd != -1)
101 62ee7d94 2023-01-10 thomas close(client->delta_cache_fd);
102 62ee7d94 2023-01-10 thomas if (client->packfile_path) {
103 62ee7d94 2023-01-10 thomas if (unlink(client->packfile_path) == -1 && errno != ENOENT)
104 62ee7d94 2023-01-10 thomas log_warn("unlink %s: ", client->packfile_path);
105 62ee7d94 2023-01-10 thomas free(client->packfile_path);
107 62ee7d94 2023-01-10 thomas if (client->packidx_path) {
108 62ee7d94 2023-01-10 thomas if (unlink(client->packidx_path) == -1 && errno != ENOENT)
109 62ee7d94 2023-01-10 thomas log_warn("unlink %s: ", client->packidx_path);
110 62ee7d94 2023-01-10 thomas free(client->packidx_path);
112 62ee7d94 2023-01-10 thomas free(client->capabilities);
114 62ee7d94 2023-01-10 thomas gotd_session_shutdown();
117 62ee7d94 2023-01-10 thomas static void
118 62ee7d94 2023-01-10 thomas disconnect_on_error(struct gotd_session_client *client,
119 62ee7d94 2023-01-10 thomas const struct got_error *err)
121 62ee7d94 2023-01-10 thomas struct imsgbuf ibuf;
123 62ee7d94 2023-01-10 thomas log_warnx("uid %d: %s", client->euid, err->msg);
124 62ee7d94 2023-01-10 thomas if (err->code != GOT_ERR_EOF) {
125 62ee7d94 2023-01-10 thomas imsg_init(&ibuf, client->fd);
126 62ee7d94 2023-01-10 thomas gotd_imsg_send_error(&ibuf, 0, PROC_SESSION, err);
127 62ee7d94 2023-01-10 thomas imsg_clear(&ibuf);
130 62ee7d94 2023-01-10 thomas disconnect(client);
133 62ee7d94 2023-01-10 thomas static void
134 62ee7d94 2023-01-10 thomas gotd_request_timeout(int fd, short events, void *arg)
136 62ee7d94 2023-01-10 thomas struct gotd_session_client *client = arg;
138 62ee7d94 2023-01-10 thomas log_debug("disconnecting uid %d due to timeout", client->euid);
139 62ee7d94 2023-01-10 thomas disconnect(client);
143 62ee7d94 2023-01-10 thomas gotd_session_sighdlr(int sig, short event, void *arg)
146 62ee7d94 2023-01-10 thomas * Normal signal handler rules don't apply because libevent
147 62ee7d94 2023-01-10 thomas * decouples for us.
150 62ee7d94 2023-01-10 thomas switch (sig) {
151 62ee7d94 2023-01-10 thomas case SIGHUP:
152 62ee7d94 2023-01-10 thomas log_info("%s: ignoring SIGHUP", __func__);
154 62ee7d94 2023-01-10 thomas case SIGUSR1:
155 62ee7d94 2023-01-10 thomas log_info("%s: ignoring SIGUSR1", __func__);
157 62ee7d94 2023-01-10 thomas case SIGTERM:
158 62ee7d94 2023-01-10 thomas case SIGINT:
159 62ee7d94 2023-01-10 thomas gotd_session_shutdown();
160 62ee7d94 2023-01-10 thomas /* NOTREACHED */
163 62ee7d94 2023-01-10 thomas fatalx("unexpected signal");
167 62ee7d94 2023-01-10 thomas static const struct got_error *
168 62ee7d94 2023-01-10 thomas recv_packfile_done(uint32_t *client_id, struct imsg *imsg)
170 62ee7d94 2023-01-10 thomas struct gotd_imsg_packfile_done idone;
171 62ee7d94 2023-01-10 thomas size_t datalen;
173 62ee7d94 2023-01-10 thomas log_debug("packfile-done received");
175 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
176 62ee7d94 2023-01-10 thomas if (datalen != sizeof(idone))
177 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
178 62ee7d94 2023-01-10 thomas memcpy(&idone, imsg->data, sizeof(idone));
180 62ee7d94 2023-01-10 thomas *client_id = idone.client_id;
181 62ee7d94 2023-01-10 thomas return NULL;
184 62ee7d94 2023-01-10 thomas static const struct got_error *
185 62ee7d94 2023-01-10 thomas recv_packfile_install(uint32_t *client_id, struct imsg *imsg)
187 62ee7d94 2023-01-10 thomas struct gotd_imsg_packfile_install inst;
188 62ee7d94 2023-01-10 thomas size_t datalen;
190 62ee7d94 2023-01-10 thomas log_debug("packfile-install received");
192 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
193 62ee7d94 2023-01-10 thomas if (datalen != sizeof(inst))
194 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
195 62ee7d94 2023-01-10 thomas memcpy(&inst, imsg->data, sizeof(inst));
197 62ee7d94 2023-01-10 thomas *client_id = inst.client_id;
198 62ee7d94 2023-01-10 thomas return NULL;
201 62ee7d94 2023-01-10 thomas static const struct got_error *
202 62ee7d94 2023-01-10 thomas recv_ref_updates_start(uint32_t *client_id, struct imsg *imsg)
204 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_updates_start istart;
205 62ee7d94 2023-01-10 thomas size_t datalen;
207 62ee7d94 2023-01-10 thomas log_debug("ref-updates-start received");
209 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
210 62ee7d94 2023-01-10 thomas if (datalen != sizeof(istart))
211 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
212 62ee7d94 2023-01-10 thomas memcpy(&istart, imsg->data, sizeof(istart));
214 62ee7d94 2023-01-10 thomas *client_id = istart.client_id;
215 62ee7d94 2023-01-10 thomas return NULL;
218 62ee7d94 2023-01-10 thomas static const struct got_error *
219 62ee7d94 2023-01-10 thomas recv_ref_update(uint32_t *client_id, struct imsg *imsg)
221 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_update iref;
222 62ee7d94 2023-01-10 thomas size_t datalen;
224 62ee7d94 2023-01-10 thomas log_debug("ref-update received");
226 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
227 62ee7d94 2023-01-10 thomas if (datalen < sizeof(iref))
228 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
229 62ee7d94 2023-01-10 thomas memcpy(&iref, imsg->data, sizeof(iref));
231 62ee7d94 2023-01-10 thomas *client_id = iref.client_id;
232 62ee7d94 2023-01-10 thomas return NULL;
235 62ee7d94 2023-01-10 thomas static const struct got_error *
236 62ee7d94 2023-01-10 thomas send_ref_update_ok(struct gotd_session_client *client,
237 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_update *iref, const char *refname)
239 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_update_ok iok;
240 62ee7d94 2023-01-10 thomas struct gotd_imsgev *iev = &client->iev;
241 62ee7d94 2023-01-10 thomas struct ibuf *wbuf;
242 62ee7d94 2023-01-10 thomas size_t len;
244 62ee7d94 2023-01-10 thomas memset(&iok, 0, sizeof(iok));
245 62ee7d94 2023-01-10 thomas iok.client_id = client->id;
246 62ee7d94 2023-01-10 thomas memcpy(iok.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
247 62ee7d94 2023-01-10 thomas memcpy(iok.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
248 62ee7d94 2023-01-10 thomas iok.name_len = strlen(refname);
250 62ee7d94 2023-01-10 thomas len = sizeof(iok) + iok.name_len;
251 62ee7d94 2023-01-10 thomas wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE_OK,
252 62ee7d94 2023-01-10 thomas PROC_SESSION, gotd_session.pid, len);
253 62ee7d94 2023-01-10 thomas if (wbuf == NULL)
254 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg_create REF_UPDATE_OK");
256 62ee7d94 2023-01-10 thomas if (imsg_add(wbuf, &iok, sizeof(iok)) == -1)
257 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg_add REF_UPDATE_OK");
258 62ee7d94 2023-01-10 thomas if (imsg_add(wbuf, refname, iok.name_len) == -1)
259 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg_add REF_UPDATE_OK");
261 62ee7d94 2023-01-10 thomas wbuf->fd = -1;
262 62ee7d94 2023-01-10 thomas imsg_close(&iev->ibuf, wbuf);
263 62ee7d94 2023-01-10 thomas gotd_imsg_event_add(iev);
264 62ee7d94 2023-01-10 thomas return NULL;
267 62ee7d94 2023-01-10 thomas static void
268 62ee7d94 2023-01-10 thomas send_refs_updated(struct gotd_session_client *client)
270 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_REFS_UPDATED,
271 62ee7d94 2023-01-10 thomas PROC_SESSION, -1, NULL, 0) == -1)
272 62ee7d94 2023-01-10 thomas log_warn("imsg compose REFS_UPDATED");
275 62ee7d94 2023-01-10 thomas static const struct got_error *
276 62ee7d94 2023-01-10 thomas send_ref_update_ng(struct gotd_session_client *client,
277 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_update *iref, const char *refname,
278 62ee7d94 2023-01-10 thomas const char *reason)
280 62ee7d94 2023-01-10 thomas const struct got_error *ng_err;
281 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_update_ng ing;
282 62ee7d94 2023-01-10 thomas struct gotd_imsgev *iev = &client->iev;
283 62ee7d94 2023-01-10 thomas struct ibuf *wbuf;
284 62ee7d94 2023-01-10 thomas size_t len;
286 62ee7d94 2023-01-10 thomas memset(&ing, 0, sizeof(ing));
287 62ee7d94 2023-01-10 thomas ing.client_id = client->id;
288 62ee7d94 2023-01-10 thomas memcpy(ing.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
289 62ee7d94 2023-01-10 thomas memcpy(ing.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
290 62ee7d94 2023-01-10 thomas ing.name_len = strlen(refname);
292 62ee7d94 2023-01-10 thomas ng_err = got_error_fmt(GOT_ERR_REF_BUSY, "%s", reason);
293 62ee7d94 2023-01-10 thomas ing.reason_len = strlen(ng_err->msg);
295 62ee7d94 2023-01-10 thomas len = sizeof(ing) + ing.name_len + ing.reason_len;
296 62ee7d94 2023-01-10 thomas wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE_NG,
297 62ee7d94 2023-01-10 thomas PROC_SESSION, gotd_session.pid, len);
298 62ee7d94 2023-01-10 thomas if (wbuf == NULL)
299 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg_create REF_UPDATE_NG");
301 62ee7d94 2023-01-10 thomas if (imsg_add(wbuf, &ing, sizeof(ing)) == -1)
302 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg_add REF_UPDATE_NG");
303 62ee7d94 2023-01-10 thomas if (imsg_add(wbuf, refname, ing.name_len) == -1)
304 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg_add REF_UPDATE_NG");
305 62ee7d94 2023-01-10 thomas if (imsg_add(wbuf, ng_err->msg, ing.reason_len) == -1)
306 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg_add REF_UPDATE_NG");
308 62ee7d94 2023-01-10 thomas wbuf->fd = -1;
309 62ee7d94 2023-01-10 thomas imsg_close(&iev->ibuf, wbuf);
310 62ee7d94 2023-01-10 thomas gotd_imsg_event_add(iev);
311 62ee7d94 2023-01-10 thomas return NULL;
314 62ee7d94 2023-01-10 thomas static const struct got_error *
315 62ee7d94 2023-01-10 thomas install_pack(struct gotd_session_client *client, const char *repo_path,
316 62ee7d94 2023-01-10 thomas struct imsg *imsg)
318 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
319 62ee7d94 2023-01-10 thomas struct gotd_imsg_packfile_install inst;
320 62ee7d94 2023-01-10 thomas char hex[SHA1_DIGEST_STRING_LENGTH];
321 62ee7d94 2023-01-10 thomas size_t datalen;
322 62ee7d94 2023-01-10 thomas char *packfile_path = NULL, *packidx_path = NULL;
324 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
325 62ee7d94 2023-01-10 thomas if (datalen != sizeof(inst))
326 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
327 62ee7d94 2023-01-10 thomas memcpy(&inst, imsg->data, sizeof(inst));
329 62ee7d94 2023-01-10 thomas if (client->packfile_path == NULL)
330 62ee7d94 2023-01-10 thomas return got_error_msg(GOT_ERR_BAD_REQUEST,
331 62ee7d94 2023-01-10 thomas "client has no pack file");
332 62ee7d94 2023-01-10 thomas if (client->packidx_path == NULL)
333 62ee7d94 2023-01-10 thomas return got_error_msg(GOT_ERR_BAD_REQUEST,
334 62ee7d94 2023-01-10 thomas "client has no pack file index");
336 62ee7d94 2023-01-10 thomas if (got_sha1_digest_to_str(inst.pack_sha1, hex, sizeof(hex)) == NULL)
337 62ee7d94 2023-01-10 thomas return got_error_msg(GOT_ERR_NO_SPACE,
338 62ee7d94 2023-01-10 thomas "could not convert pack file SHA1 to hex");
340 62ee7d94 2023-01-10 thomas if (asprintf(&packfile_path, "/%s/%s/pack-%s.pack",
341 62ee7d94 2023-01-10 thomas repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
342 62ee7d94 2023-01-10 thomas err = got_error_from_errno("asprintf");
343 62ee7d94 2023-01-10 thomas goto done;
346 62ee7d94 2023-01-10 thomas if (asprintf(&packidx_path, "/%s/%s/pack-%s.idx",
347 62ee7d94 2023-01-10 thomas repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
348 62ee7d94 2023-01-10 thomas err = got_error_from_errno("asprintf");
349 62ee7d94 2023-01-10 thomas goto done;
352 62ee7d94 2023-01-10 thomas if (rename(client->packfile_path, packfile_path) == -1) {
353 62ee7d94 2023-01-10 thomas err = got_error_from_errno3("rename", client->packfile_path,
354 62ee7d94 2023-01-10 thomas packfile_path);
355 62ee7d94 2023-01-10 thomas goto done;
358 62ee7d94 2023-01-10 thomas free(client->packfile_path);
359 62ee7d94 2023-01-10 thomas client->packfile_path = NULL;
361 62ee7d94 2023-01-10 thomas if (rename(client->packidx_path, packidx_path) == -1) {
362 62ee7d94 2023-01-10 thomas err = got_error_from_errno3("rename", client->packidx_path,
363 62ee7d94 2023-01-10 thomas packidx_path);
364 62ee7d94 2023-01-10 thomas goto done;
367 62ee7d94 2023-01-10 thomas free(client->packidx_path);
368 62ee7d94 2023-01-10 thomas client->packidx_path = NULL;
370 62ee7d94 2023-01-10 thomas free(packfile_path);
371 62ee7d94 2023-01-10 thomas free(packidx_path);
372 62ee7d94 2023-01-10 thomas return err;
375 62ee7d94 2023-01-10 thomas static const struct got_error *
376 62ee7d94 2023-01-10 thomas begin_ref_updates(struct gotd_session_client *client, struct imsg *imsg)
378 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_updates_start istart;
379 62ee7d94 2023-01-10 thomas size_t datalen;
381 62ee7d94 2023-01-10 thomas if (client->nref_updates != -1)
382 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
384 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
385 62ee7d94 2023-01-10 thomas if (datalen != sizeof(istart))
386 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
387 62ee7d94 2023-01-10 thomas memcpy(&istart, imsg->data, sizeof(istart));
389 62ee7d94 2023-01-10 thomas if (istart.nref_updates <= 0)
390 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
392 62ee7d94 2023-01-10 thomas client->nref_updates = istart.nref_updates;
393 62ee7d94 2023-01-10 thomas return NULL;
396 62ee7d94 2023-01-10 thomas static const struct got_error *
397 d98779cd 2023-01-19 thomas update_ref(int *shut, struct gotd_session_client *client,
398 d98779cd 2023-01-19 thomas const char *repo_path, struct imsg *imsg)
400 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
401 62ee7d94 2023-01-10 thomas struct got_repository *repo = NULL;
402 62ee7d94 2023-01-10 thomas struct got_reference *ref = NULL;
403 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_update iref;
404 62ee7d94 2023-01-10 thomas struct got_object_id old_id, new_id;
405 62ee7d94 2023-01-10 thomas struct got_object_id *id = NULL;
406 62ee7d94 2023-01-10 thomas struct got_object *obj = NULL;
407 62ee7d94 2023-01-10 thomas char *refname = NULL;
408 62ee7d94 2023-01-10 thomas size_t datalen;
409 62ee7d94 2023-01-10 thomas int locked = 0;
410 8a9c582b 2023-06-22 thomas char hex1[SHA1_DIGEST_STRING_LENGTH];
411 8a9c582b 2023-06-22 thomas char hex2[SHA1_DIGEST_STRING_LENGTH];
413 62ee7d94 2023-01-10 thomas log_debug("update-ref from uid %d", client->euid);
415 62ee7d94 2023-01-10 thomas if (client->nref_updates <= 0)
416 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
418 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
419 62ee7d94 2023-01-10 thomas if (datalen < sizeof(iref))
420 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
421 62ee7d94 2023-01-10 thomas memcpy(&iref, imsg->data, sizeof(iref));
422 62ee7d94 2023-01-10 thomas if (datalen != sizeof(iref) + iref.name_len)
423 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
424 fcbb06bf 2023-01-14 thomas refname = strndup(imsg->data + sizeof(iref), iref.name_len);
425 62ee7d94 2023-01-10 thomas if (refname == NULL)
426 fcbb06bf 2023-01-14 thomas return got_error_from_errno("strndup");
428 62ee7d94 2023-01-10 thomas log_debug("updating ref %s for uid %d", refname, client->euid);
430 62ee7d94 2023-01-10 thomas err = got_repo_open(&repo, repo_path, NULL, NULL);
432 62ee7d94 2023-01-10 thomas goto done;
434 62ee7d94 2023-01-10 thomas memcpy(old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
435 62ee7d94 2023-01-10 thomas memcpy(new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
436 49563dfb 2023-01-28 thomas err = got_object_open(&obj, repo,
437 49563dfb 2023-01-28 thomas iref.delete_ref ? &old_id : &new_id);
439 62ee7d94 2023-01-10 thomas goto done;
441 62ee7d94 2023-01-10 thomas if (iref.ref_is_new) {
442 62ee7d94 2023-01-10 thomas err = got_ref_open(&ref, repo, refname, 0);
443 62ee7d94 2023-01-10 thomas if (err) {
444 62ee7d94 2023-01-10 thomas if (err->code != GOT_ERR_NOT_REF)
445 62ee7d94 2023-01-10 thomas goto done;
446 62ee7d94 2023-01-10 thomas err = got_ref_alloc(&ref, refname, &new_id);
448 62ee7d94 2023-01-10 thomas goto done;
449 62ee7d94 2023-01-10 thomas err = got_ref_write(ref, repo); /* will lock/unlock */
451 62ee7d94 2023-01-10 thomas goto done;
453 8a9c582b 2023-06-22 thomas err = got_ref_resolve(&id, repo, ref);
455 8a9c582b 2023-06-22 thomas goto done;
456 8a9c582b 2023-06-22 thomas got_object_id_hex(&new_id, hex1, sizeof(hex1));
457 8a9c582b 2023-06-22 thomas got_object_id_hex(id, hex2, sizeof(hex2));
458 62ee7d94 2023-01-10 thomas err = got_error_fmt(GOT_ERR_REF_BUSY,
459 8a9c582b 2023-06-22 thomas "Addition %s: %s failed; %s: %s has been "
460 8a9c582b 2023-06-22 thomas "created by someone else while transaction "
461 8a9c582b 2023-06-22 thomas "was in progress",
462 8a9c582b 2023-06-22 thomas got_ref_get_name(ref), hex1,
463 8a9c582b 2023-06-22 thomas got_ref_get_name(ref), hex2);
464 62ee7d94 2023-01-10 thomas goto done;
466 49563dfb 2023-01-28 thomas } else if (iref.delete_ref) {
467 49563dfb 2023-01-28 thomas err = got_ref_open(&ref, repo, refname, 1 /* lock */);
469 49563dfb 2023-01-28 thomas goto done;
470 49563dfb 2023-01-28 thomas locked = 1;
472 49563dfb 2023-01-28 thomas err = got_ref_resolve(&id, repo, ref);
474 49563dfb 2023-01-28 thomas goto done;
476 49563dfb 2023-01-28 thomas if (got_object_id_cmp(id, &old_id) != 0) {
477 8a9c582b 2023-06-22 thomas got_object_id_hex(&old_id, hex1, sizeof(hex1));
478 8a9c582b 2023-06-22 thomas got_object_id_hex(id, hex2, sizeof(hex2));
479 49563dfb 2023-01-28 thomas err = got_error_fmt(GOT_ERR_REF_BUSY,
480 8a9c582b 2023-06-22 thomas "Deletion %s: %s failed; %s: %s has been "
481 8a9c582b 2023-06-22 thomas "created by someone else while transaction "
482 8a9c582b 2023-06-22 thomas "was in progress",
483 8a9c582b 2023-06-22 thomas got_ref_get_name(ref), hex1,
484 8a9c582b 2023-06-22 thomas got_ref_get_name(ref), hex2);
485 49563dfb 2023-01-28 thomas goto done;
488 49563dfb 2023-01-28 thomas err = got_ref_delete(ref, repo);
490 49563dfb 2023-01-28 thomas goto done;
492 49563dfb 2023-01-28 thomas free(id);
493 49563dfb 2023-01-28 thomas id = NULL;
495 62ee7d94 2023-01-10 thomas err = got_ref_open(&ref, repo, refname, 1 /* lock */);
497 62ee7d94 2023-01-10 thomas goto done;
498 62ee7d94 2023-01-10 thomas locked = 1;
500 62ee7d94 2023-01-10 thomas err = got_ref_resolve(&id, repo, ref);
502 62ee7d94 2023-01-10 thomas goto done;
504 62ee7d94 2023-01-10 thomas if (got_object_id_cmp(id, &old_id) != 0) {
505 8a9c582b 2023-06-22 thomas got_object_id_hex(&old_id, hex1, sizeof(hex1));
506 8a9c582b 2023-06-22 thomas got_object_id_hex(id, hex2, sizeof(hex2));
507 62ee7d94 2023-01-10 thomas err = got_error_fmt(GOT_ERR_REF_BUSY,
508 8a9c582b 2023-06-22 thomas "Update %s: %s failed; %s: %s has been "
509 8a9c582b 2023-06-22 thomas "created by someone else while transaction "
510 8a9c582b 2023-06-22 thomas "was in progress",
511 8a9c582b 2023-06-22 thomas got_ref_get_name(ref), hex1,
512 8a9c582b 2023-06-22 thomas got_ref_get_name(ref), hex2);
513 62ee7d94 2023-01-10 thomas goto done;
516 169def41 2023-06-22 thomas if (got_object_id_cmp(&new_id, &old_id) != 0) {
517 169def41 2023-06-22 thomas err = got_ref_change_ref(ref, &new_id);
519 169def41 2023-06-22 thomas goto done;
521 169def41 2023-06-22 thomas err = got_ref_write(ref, repo);
523 169def41 2023-06-22 thomas goto done;
526 62ee7d94 2023-01-10 thomas free(id);
527 62ee7d94 2023-01-10 thomas id = NULL;
530 62ee7d94 2023-01-10 thomas if (err) {
531 62ee7d94 2023-01-10 thomas if (err->code == GOT_ERR_LOCKFILE_TIMEOUT) {
532 62ee7d94 2023-01-10 thomas err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
533 62ee7d94 2023-01-10 thomas "could not acquire exclusive file lock for %s",
534 62ee7d94 2023-01-10 thomas refname);
536 62ee7d94 2023-01-10 thomas send_ref_update_ng(client, &iref, refname, err->msg);
538 62ee7d94 2023-01-10 thomas send_ref_update_ok(client, &iref, refname);
540 62ee7d94 2023-01-10 thomas if (client->nref_updates > 0) {
541 62ee7d94 2023-01-10 thomas client->nref_updates--;
542 d98779cd 2023-01-19 thomas if (client->nref_updates == 0) {
543 62ee7d94 2023-01-10 thomas send_refs_updated(client);
544 d98779cd 2023-01-19 thomas *shut = 1;
548 62ee7d94 2023-01-10 thomas if (locked) {
549 62ee7d94 2023-01-10 thomas const struct got_error *unlock_err;
550 62ee7d94 2023-01-10 thomas unlock_err = got_ref_unlock(ref);
551 62ee7d94 2023-01-10 thomas if (unlock_err && err == NULL)
552 62ee7d94 2023-01-10 thomas err = unlock_err;
555 62ee7d94 2023-01-10 thomas got_ref_close(ref);
557 62ee7d94 2023-01-10 thomas got_object_close(obj);
558 62ee7d94 2023-01-10 thomas if (repo)
559 62ee7d94 2023-01-10 thomas got_repo_close(repo);
560 62ee7d94 2023-01-10 thomas free(refname);
561 62ee7d94 2023-01-10 thomas free(id);
562 62ee7d94 2023-01-10 thomas return err;
565 62ee7d94 2023-01-10 thomas static void
566 62ee7d94 2023-01-10 thomas session_dispatch_repo_child(int fd, short event, void *arg)
568 62ee7d94 2023-01-10 thomas struct gotd_imsgev *iev = arg;
569 62ee7d94 2023-01-10 thomas struct imsgbuf *ibuf = &iev->ibuf;
570 62ee7d94 2023-01-10 thomas struct gotd_session_client *client = &gotd_session_client;
571 62ee7d94 2023-01-10 thomas ssize_t n;
572 62ee7d94 2023-01-10 thomas int shut = 0;
573 62ee7d94 2023-01-10 thomas struct imsg imsg;
575 62ee7d94 2023-01-10 thomas if (event & EV_READ) {
576 62ee7d94 2023-01-10 thomas if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
577 62ee7d94 2023-01-10 thomas fatal("imsg_read error");
578 62ee7d94 2023-01-10 thomas if (n == 0) {
579 62ee7d94 2023-01-10 thomas /* Connection closed. */
580 62ee7d94 2023-01-10 thomas shut = 1;
581 62ee7d94 2023-01-10 thomas goto done;
585 62ee7d94 2023-01-10 thomas if (event & EV_WRITE) {
586 62ee7d94 2023-01-10 thomas n = msgbuf_write(&ibuf->w);
587 62ee7d94 2023-01-10 thomas if (n == -1 && errno != EAGAIN)
588 62ee7d94 2023-01-10 thomas fatal("msgbuf_write");
589 62ee7d94 2023-01-10 thomas if (n == 0) {
590 62ee7d94 2023-01-10 thomas /* Connection closed. */
591 62ee7d94 2023-01-10 thomas shut = 1;
592 62ee7d94 2023-01-10 thomas goto done;
596 62ee7d94 2023-01-10 thomas for (;;) {
597 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
598 62ee7d94 2023-01-10 thomas uint32_t client_id = 0;
599 62ee7d94 2023-01-10 thomas int do_disconnect = 0;
600 62ee7d94 2023-01-10 thomas int do_ref_updates = 0, do_ref_update = 0;
601 62ee7d94 2023-01-10 thomas int do_packfile_install = 0;
603 62ee7d94 2023-01-10 thomas if ((n = imsg_get(ibuf, &imsg)) == -1)
604 62ee7d94 2023-01-10 thomas fatal("%s: imsg_get error", __func__);
605 62ee7d94 2023-01-10 thomas if (n == 0) /* No more messages. */
608 62ee7d94 2023-01-10 thomas switch (imsg.hdr.type) {
609 62ee7d94 2023-01-10 thomas case GOTD_IMSG_ERROR:
610 62ee7d94 2023-01-10 thomas do_disconnect = 1;
611 62ee7d94 2023-01-10 thomas err = gotd_imsg_recv_error(&client_id, &imsg);
613 62ee7d94 2023-01-10 thomas case GOTD_IMSG_PACKFILE_DONE:
614 62ee7d94 2023-01-10 thomas do_disconnect = 1;
615 62ee7d94 2023-01-10 thomas err = recv_packfile_done(&client_id, &imsg);
617 62ee7d94 2023-01-10 thomas case GOTD_IMSG_PACKFILE_INSTALL:
618 62ee7d94 2023-01-10 thomas err = recv_packfile_install(&client_id, &imsg);
619 62ee7d94 2023-01-10 thomas if (err == NULL)
620 62ee7d94 2023-01-10 thomas do_packfile_install = 1;
622 62ee7d94 2023-01-10 thomas case GOTD_IMSG_REF_UPDATES_START:
623 62ee7d94 2023-01-10 thomas err = recv_ref_updates_start(&client_id, &imsg);
624 62ee7d94 2023-01-10 thomas if (err == NULL)
625 62ee7d94 2023-01-10 thomas do_ref_updates = 1;
627 62ee7d94 2023-01-10 thomas case GOTD_IMSG_REF_UPDATE:
628 62ee7d94 2023-01-10 thomas err = recv_ref_update(&client_id, &imsg);
629 62ee7d94 2023-01-10 thomas if (err == NULL)
630 62ee7d94 2023-01-10 thomas do_ref_update = 1;
633 62ee7d94 2023-01-10 thomas log_debug("unexpected imsg %d", imsg.hdr.type);
637 62ee7d94 2023-01-10 thomas if (do_disconnect) {
639 62ee7d94 2023-01-10 thomas disconnect_on_error(client, err);
641 62ee7d94 2023-01-10 thomas disconnect(client);
643 62ee7d94 2023-01-10 thomas if (do_packfile_install)
644 62ee7d94 2023-01-10 thomas err = install_pack(client,
645 62ee7d94 2023-01-10 thomas gotd_session.repo->path, &imsg);
646 62ee7d94 2023-01-10 thomas else if (do_ref_updates)
647 62ee7d94 2023-01-10 thomas err = begin_ref_updates(client, &imsg);
648 62ee7d94 2023-01-10 thomas else if (do_ref_update)
649 d98779cd 2023-01-19 thomas err = update_ref(&shut, client,
650 62ee7d94 2023-01-10 thomas gotd_session.repo->path, &imsg);
652 62ee7d94 2023-01-10 thomas log_warnx("uid %d: %s", client->euid, err->msg);
654 62ee7d94 2023-01-10 thomas imsg_free(&imsg);
657 62ee7d94 2023-01-10 thomas if (!shut) {
658 62ee7d94 2023-01-10 thomas gotd_imsg_event_add(iev);
660 62ee7d94 2023-01-10 thomas /* This pipe is dead. Remove its event handler */
661 62ee7d94 2023-01-10 thomas event_del(&iev->ev);
662 62ee7d94 2023-01-10 thomas event_loopexit(NULL);
666 62ee7d94 2023-01-10 thomas static const struct got_error *
667 62ee7d94 2023-01-10 thomas recv_capabilities(struct gotd_session_client *client, struct imsg *imsg)
669 62ee7d94 2023-01-10 thomas struct gotd_imsg_capabilities icapas;
670 62ee7d94 2023-01-10 thomas size_t datalen;
672 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
673 62ee7d94 2023-01-10 thomas if (datalen != sizeof(icapas))
674 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
675 62ee7d94 2023-01-10 thomas memcpy(&icapas, imsg->data, sizeof(icapas));
677 62ee7d94 2023-01-10 thomas client->ncapa_alloc = icapas.ncapabilities;
678 62ee7d94 2023-01-10 thomas client->capabilities = calloc(client->ncapa_alloc,
679 62ee7d94 2023-01-10 thomas sizeof(*client->capabilities));
680 62ee7d94 2023-01-10 thomas if (client->capabilities == NULL) {
681 62ee7d94 2023-01-10 thomas client->ncapa_alloc = 0;
682 62ee7d94 2023-01-10 thomas return got_error_from_errno("calloc");
685 62ee7d94 2023-01-10 thomas log_debug("expecting %zu capabilities from uid %d",
686 62ee7d94 2023-01-10 thomas client->ncapa_alloc, client->euid);
687 62ee7d94 2023-01-10 thomas return NULL;
690 62ee7d94 2023-01-10 thomas static const struct got_error *
691 62ee7d94 2023-01-10 thomas recv_capability(struct gotd_session_client *client, struct imsg *imsg)
693 62ee7d94 2023-01-10 thomas struct gotd_imsg_capability icapa;
694 62ee7d94 2023-01-10 thomas struct gotd_client_capability *capa;
695 62ee7d94 2023-01-10 thomas size_t datalen;
696 62ee7d94 2023-01-10 thomas char *key, *value = NULL;
698 62ee7d94 2023-01-10 thomas if (client->capabilities == NULL ||
699 62ee7d94 2023-01-10 thomas client->ncapabilities >= client->ncapa_alloc) {
700 62ee7d94 2023-01-10 thomas return got_error_msg(GOT_ERR_BAD_REQUEST,
701 62ee7d94 2023-01-10 thomas "unexpected capability received");
704 62ee7d94 2023-01-10 thomas memset(&icapa, 0, sizeof(icapa));
706 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
707 62ee7d94 2023-01-10 thomas if (datalen < sizeof(icapa))
708 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
709 62ee7d94 2023-01-10 thomas memcpy(&icapa, imsg->data, sizeof(icapa));
711 62ee7d94 2023-01-10 thomas if (datalen != sizeof(icapa) + icapa.key_len + icapa.value_len)
712 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
714 fcbb06bf 2023-01-14 thomas key = strndup(imsg->data + sizeof(icapa), icapa.key_len);
715 62ee7d94 2023-01-10 thomas if (key == NULL)
716 fcbb06bf 2023-01-14 thomas return got_error_from_errno("strndup");
717 62ee7d94 2023-01-10 thomas if (icapa.value_len > 0) {
718 fcbb06bf 2023-01-14 thomas value = strndup(imsg->data + sizeof(icapa) + icapa.key_len,
719 fcbb06bf 2023-01-14 thomas icapa.value_len);
720 62ee7d94 2023-01-10 thomas if (value == NULL) {
721 62ee7d94 2023-01-10 thomas free(key);
722 fcbb06bf 2023-01-14 thomas return got_error_from_errno("strndup");
726 62ee7d94 2023-01-10 thomas capa = &client->capabilities[client->ncapabilities++];
727 62ee7d94 2023-01-10 thomas capa->key = key;
728 62ee7d94 2023-01-10 thomas capa->value = value;
730 62ee7d94 2023-01-10 thomas if (value)
731 62ee7d94 2023-01-10 thomas log_debug("uid %d: capability %s=%s", client->euid, key, value);
733 62ee7d94 2023-01-10 thomas log_debug("uid %d: capability %s", client->euid, key);
735 62ee7d94 2023-01-10 thomas return NULL;
738 62ee7d94 2023-01-10 thomas static const struct got_error *
739 62ee7d94 2023-01-10 thomas ensure_client_is_reading(struct gotd_session_client *client)
741 62ee7d94 2023-01-10 thomas if (client->is_writing) {
742 62ee7d94 2023-01-10 thomas return got_error_fmt(GOT_ERR_BAD_PACKET,
743 62ee7d94 2023-01-10 thomas "uid %d made a read-request but is not reading from "
744 62ee7d94 2023-01-10 thomas "a repository", client->euid);
747 62ee7d94 2023-01-10 thomas return NULL;
750 62ee7d94 2023-01-10 thomas static const struct got_error *
751 62ee7d94 2023-01-10 thomas ensure_client_is_writing(struct gotd_session_client *client)
753 62ee7d94 2023-01-10 thomas if (!client->is_writing) {
754 62ee7d94 2023-01-10 thomas return got_error_fmt(GOT_ERR_BAD_PACKET,
755 62ee7d94 2023-01-10 thomas "uid %d made a write-request but is not writing to "
756 62ee7d94 2023-01-10 thomas "a repository", client->euid);
759 62ee7d94 2023-01-10 thomas return NULL;
762 62ee7d94 2023-01-10 thomas static const struct got_error *
763 62ee7d94 2023-01-10 thomas forward_want(struct gotd_session_client *client, struct imsg *imsg)
765 62ee7d94 2023-01-10 thomas struct gotd_imsg_want ireq;
766 62ee7d94 2023-01-10 thomas struct gotd_imsg_want iwant;
767 62ee7d94 2023-01-10 thomas size_t datalen;
769 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
770 62ee7d94 2023-01-10 thomas if (datalen != sizeof(ireq))
771 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
773 62ee7d94 2023-01-10 thomas memcpy(&ireq, imsg->data, datalen);
775 62ee7d94 2023-01-10 thomas memset(&iwant, 0, sizeof(iwant));
776 62ee7d94 2023-01-10 thomas memcpy(iwant.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
777 62ee7d94 2023-01-10 thomas iwant.client_id = client->id;
779 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&client->repo_child_iev, GOTD_IMSG_WANT,
780 62ee7d94 2023-01-10 thomas PROC_SESSION, -1, &iwant, sizeof(iwant)) == -1)
781 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg compose WANT");
783 62ee7d94 2023-01-10 thomas return NULL;
786 62ee7d94 2023-01-10 thomas static const struct got_error *
787 62ee7d94 2023-01-10 thomas forward_ref_update(struct gotd_session_client *client, struct imsg *imsg)
789 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
790 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_update ireq;
791 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_update *iref = NULL;
792 62ee7d94 2023-01-10 thomas size_t datalen;
794 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
795 62ee7d94 2023-01-10 thomas if (datalen < sizeof(ireq))
796 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
797 62ee7d94 2023-01-10 thomas memcpy(&ireq, imsg->data, sizeof(ireq));
798 62ee7d94 2023-01-10 thomas if (datalen != sizeof(ireq) + ireq.name_len)
799 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
801 62ee7d94 2023-01-10 thomas iref = malloc(datalen);
802 62ee7d94 2023-01-10 thomas if (iref == NULL)
803 62ee7d94 2023-01-10 thomas return got_error_from_errno("malloc");
804 62ee7d94 2023-01-10 thomas memcpy(iref, imsg->data, datalen);
806 62ee7d94 2023-01-10 thomas iref->client_id = client->id;
807 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&client->repo_child_iev,
808 62ee7d94 2023-01-10 thomas GOTD_IMSG_REF_UPDATE, PROC_SESSION, -1, iref, datalen) == -1)
809 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose REF_UPDATE");
810 62ee7d94 2023-01-10 thomas free(iref);
811 62ee7d94 2023-01-10 thomas return err;
814 62ee7d94 2023-01-10 thomas static const struct got_error *
815 62ee7d94 2023-01-10 thomas forward_have(struct gotd_session_client *client, struct imsg *imsg)
817 62ee7d94 2023-01-10 thomas struct gotd_imsg_have ireq;
818 62ee7d94 2023-01-10 thomas struct gotd_imsg_have ihave;
819 62ee7d94 2023-01-10 thomas size_t datalen;
821 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
822 62ee7d94 2023-01-10 thomas if (datalen != sizeof(ireq))
823 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
825 62ee7d94 2023-01-10 thomas memcpy(&ireq, imsg->data, datalen);
827 62ee7d94 2023-01-10 thomas memset(&ihave, 0, sizeof(ihave));
828 62ee7d94 2023-01-10 thomas memcpy(ihave.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
829 62ee7d94 2023-01-10 thomas ihave.client_id = client->id;
831 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&client->repo_child_iev, GOTD_IMSG_HAVE,
832 62ee7d94 2023-01-10 thomas PROC_SESSION, -1, &ihave, sizeof(ihave)) == -1)
833 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg compose HAVE");
835 62ee7d94 2023-01-10 thomas return NULL;
838 62ee7d94 2023-01-10 thomas static int
839 62ee7d94 2023-01-10 thomas client_has_capability(struct gotd_session_client *client, const char *capastr)
841 62ee7d94 2023-01-10 thomas struct gotd_client_capability *capa;
842 62ee7d94 2023-01-10 thomas size_t i;
844 62ee7d94 2023-01-10 thomas if (client->ncapabilities == 0)
845 62ee7d94 2023-01-10 thomas return 0;
847 62ee7d94 2023-01-10 thomas for (i = 0; i < client->ncapabilities; i++) {
848 62ee7d94 2023-01-10 thomas capa = &client->capabilities[i];
849 62ee7d94 2023-01-10 thomas if (strcmp(capa->key, capastr) == 0)
850 62ee7d94 2023-01-10 thomas return 1;
853 62ee7d94 2023-01-10 thomas return 0;
856 62ee7d94 2023-01-10 thomas static const struct got_error *
857 62ee7d94 2023-01-10 thomas recv_packfile(struct gotd_session_client *client)
859 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
860 62ee7d94 2023-01-10 thomas struct gotd_imsg_recv_packfile ipack;
861 62ee7d94 2023-01-10 thomas struct gotd_imsg_packfile_pipe ipipe;
862 62ee7d94 2023-01-10 thomas struct gotd_imsg_packidx_file ifile;
863 62ee7d94 2023-01-10 thomas char *basepath = NULL, *pack_path = NULL, *idx_path = NULL;
864 62ee7d94 2023-01-10 thomas int packfd = -1, idxfd = -1;
865 62ee7d94 2023-01-10 thomas int pipe[2] = { -1, -1 };
867 62ee7d94 2023-01-10 thomas if (client->packfile_path) {
868 62ee7d94 2023-01-10 thomas return got_error_fmt(GOT_ERR_PRIVSEP_MSG,
869 62ee7d94 2023-01-10 thomas "uid %d already has a pack file", client->euid);
872 62ee7d94 2023-01-10 thomas if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
873 62ee7d94 2023-01-10 thomas return got_error_from_errno("socketpair");
875 62ee7d94 2023-01-10 thomas memset(&ipipe, 0, sizeof(ipipe));
876 62ee7d94 2023-01-10 thomas ipipe.client_id = client->id;
878 62ee7d94 2023-01-10 thomas /* Send pack pipe end 0 to repo child process. */
879 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&client->repo_child_iev,
880 62ee7d94 2023-01-10 thomas GOTD_IMSG_PACKFILE_PIPE, PROC_SESSION, pipe[0],
881 62ee7d94 2023-01-10 thomas &ipipe, sizeof(ipipe)) == -1) {
882 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose PACKFILE_PIPE");
883 62ee7d94 2023-01-10 thomas pipe[0] = -1;
884 62ee7d94 2023-01-10 thomas goto done;
886 62ee7d94 2023-01-10 thomas pipe[0] = -1;
888 62ee7d94 2023-01-10 thomas /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
889 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&client->iev,
890 62ee7d94 2023-01-10 thomas GOTD_IMSG_PACKFILE_PIPE, PROC_SESSION, pipe[1], NULL, 0) == -1)
891 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose PACKFILE_PIPE");
892 62ee7d94 2023-01-10 thomas pipe[1] = -1;
894 62ee7d94 2023-01-10 thomas if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.pack",
895 62ee7d94 2023-01-10 thomas got_repo_get_path(gotd_session.repo), GOT_OBJECTS_PACK_DIR,
896 62ee7d94 2023-01-10 thomas client->euid) == -1) {
897 62ee7d94 2023-01-10 thomas err = got_error_from_errno("asprintf");
898 62ee7d94 2023-01-10 thomas goto done;
901 62ee7d94 2023-01-10 thomas err = got_opentemp_named_fd(&pack_path, &packfd, basepath, "");
903 62ee7d94 2023-01-10 thomas goto done;
904 851a5b48 2023-02-03 thomas if (fchmod(packfd, GOT_DEFAULT_PACK_MODE) == -1) {
905 851a5b48 2023-02-03 thomas err = got_error_from_errno2("fchmod", pack_path);
906 851a5b48 2023-02-03 thomas goto done;
909 62ee7d94 2023-01-10 thomas free(basepath);
910 62ee7d94 2023-01-10 thomas if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.idx",
911 62ee7d94 2023-01-10 thomas got_repo_get_path(gotd_session.repo), GOT_OBJECTS_PACK_DIR,
912 62ee7d94 2023-01-10 thomas client->euid) == -1) {
913 62ee7d94 2023-01-10 thomas err = got_error_from_errno("asprintf");
914 62ee7d94 2023-01-10 thomas basepath = NULL;
915 62ee7d94 2023-01-10 thomas goto done;
917 62ee7d94 2023-01-10 thomas err = got_opentemp_named_fd(&idx_path, &idxfd, basepath, "");
919 62ee7d94 2023-01-10 thomas goto done;
920 851a5b48 2023-02-03 thomas if (fchmod(idxfd, GOT_DEFAULT_PACK_MODE) == -1) {
921 851a5b48 2023-02-03 thomas err = got_error_from_errno2("fchmod", idx_path);
922 851a5b48 2023-02-03 thomas goto done;
925 62ee7d94 2023-01-10 thomas memset(&ifile, 0, sizeof(ifile));
926 62ee7d94 2023-01-10 thomas ifile.client_id = client->id;
927 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&client->repo_child_iev,
928 62ee7d94 2023-01-10 thomas GOTD_IMSG_PACKIDX_FILE, PROC_SESSION,
929 62ee7d94 2023-01-10 thomas idxfd, &ifile, sizeof(ifile)) == -1) {
930 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose PACKIDX_FILE");
931 62ee7d94 2023-01-10 thomas idxfd = -1;
932 62ee7d94 2023-01-10 thomas goto done;
934 62ee7d94 2023-01-10 thomas idxfd = -1;
936 62ee7d94 2023-01-10 thomas memset(&ipack, 0, sizeof(ipack));
937 62ee7d94 2023-01-10 thomas ipack.client_id = client->id;
938 62ee7d94 2023-01-10 thomas if (client_has_capability(client, GOT_CAPA_REPORT_STATUS))
939 62ee7d94 2023-01-10 thomas ipack.report_status = 1;
941 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&client->repo_child_iev,
942 62ee7d94 2023-01-10 thomas GOTD_IMSG_RECV_PACKFILE, PROC_SESSION, packfd,
943 62ee7d94 2023-01-10 thomas &ipack, sizeof(ipack)) == -1) {
944 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose RECV_PACKFILE");
945 62ee7d94 2023-01-10 thomas packfd = -1;
946 62ee7d94 2023-01-10 thomas goto done;
948 62ee7d94 2023-01-10 thomas packfd = -1;
951 62ee7d94 2023-01-10 thomas free(basepath);
952 62ee7d94 2023-01-10 thomas if (pipe[0] != -1 && close(pipe[0]) == -1 && err == NULL)
953 62ee7d94 2023-01-10 thomas err = got_error_from_errno("close");
954 62ee7d94 2023-01-10 thomas if (pipe[1] != -1 && close(pipe[1]) == -1 && err == NULL)
955 62ee7d94 2023-01-10 thomas err = got_error_from_errno("close");
956 62ee7d94 2023-01-10 thomas if (packfd != -1 && close(packfd) == -1 && err == NULL)
957 62ee7d94 2023-01-10 thomas err = got_error_from_errno("close");
958 62ee7d94 2023-01-10 thomas if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
959 62ee7d94 2023-01-10 thomas err = got_error_from_errno("close");
960 62ee7d94 2023-01-10 thomas if (err) {
961 62ee7d94 2023-01-10 thomas free(pack_path);
962 62ee7d94 2023-01-10 thomas free(idx_path);
964 62ee7d94 2023-01-10 thomas client->packfile_path = pack_path;
965 62ee7d94 2023-01-10 thomas client->packidx_path = idx_path;
967 62ee7d94 2023-01-10 thomas return err;
970 62ee7d94 2023-01-10 thomas static const struct got_error *
971 62ee7d94 2023-01-10 thomas send_packfile(struct gotd_session_client *client)
973 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
974 62ee7d94 2023-01-10 thomas struct gotd_imsg_send_packfile ipack;
975 62ee7d94 2023-01-10 thomas struct gotd_imsg_packfile_pipe ipipe;
976 62ee7d94 2023-01-10 thomas int pipe[2];
978 62ee7d94 2023-01-10 thomas if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
979 62ee7d94 2023-01-10 thomas return got_error_from_errno("socketpair");
981 62ee7d94 2023-01-10 thomas memset(&ipack, 0, sizeof(ipack));
982 62ee7d94 2023-01-10 thomas memset(&ipipe, 0, sizeof(ipipe));
984 62ee7d94 2023-01-10 thomas ipack.client_id = client->id;
985 62ee7d94 2023-01-10 thomas if (client_has_capability(client, GOT_CAPA_SIDE_BAND_64K))
986 62ee7d94 2023-01-10 thomas ipack.report_progress = 1;
988 62ee7d94 2023-01-10 thomas client->delta_cache_fd = got_opentempfd();
989 62ee7d94 2023-01-10 thomas if (client->delta_cache_fd == -1)
990 62ee7d94 2023-01-10 thomas return got_error_from_errno("got_opentempfd");
992 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&client->repo_child_iev,
993 62ee7d94 2023-01-10 thomas GOTD_IMSG_SEND_PACKFILE, PROC_GOTD, client->delta_cache_fd,
994 62ee7d94 2023-01-10 thomas &ipack, sizeof(ipack)) == -1) {
995 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose SEND_PACKFILE");
996 62ee7d94 2023-01-10 thomas close(pipe[0]);
997 62ee7d94 2023-01-10 thomas close(pipe[1]);
998 62ee7d94 2023-01-10 thomas return err;
1001 62ee7d94 2023-01-10 thomas ipipe.client_id = client->id;
1003 62ee7d94 2023-01-10 thomas /* Send pack pipe end 0 to repo child process. */
1004 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&client->repo_child_iev,
1005 62ee7d94 2023-01-10 thomas GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD,
1006 62ee7d94 2023-01-10 thomas pipe[0], &ipipe, sizeof(ipipe)) == -1) {
1007 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose PACKFILE_PIPE");
1008 62ee7d94 2023-01-10 thomas close(pipe[1]);
1009 62ee7d94 2023-01-10 thomas return err;
1012 62ee7d94 2023-01-10 thomas /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
1013 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&client->iev,
1014 62ee7d94 2023-01-10 thomas GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1], NULL, 0) == -1)
1015 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose PACKFILE_PIPE");
1017 62ee7d94 2023-01-10 thomas return err;
1020 62ee7d94 2023-01-10 thomas static void
1021 8cb46987 2023-02-07 thomas session_dispatch_client(int fd, short events, void *arg)
1023 62ee7d94 2023-01-10 thomas struct gotd_imsgev *iev = arg;
1024 62ee7d94 2023-01-10 thomas struct imsgbuf *ibuf = &iev->ibuf;
1025 62ee7d94 2023-01-10 thomas struct gotd_session_client *client = &gotd_session_client;
1026 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
1027 62ee7d94 2023-01-10 thomas struct imsg imsg;
1028 62ee7d94 2023-01-10 thomas ssize_t n;
1030 62ee7d94 2023-01-10 thomas if (events & EV_WRITE) {
1031 62ee7d94 2023-01-10 thomas while (ibuf->w.queued) {
1032 62ee7d94 2023-01-10 thomas n = msgbuf_write(&ibuf->w);
1033 62ee7d94 2023-01-10 thomas if (n == -1 && errno == EPIPE) {
1035 62ee7d94 2023-01-10 thomas * The client has closed its socket.
1036 62ee7d94 2023-01-10 thomas * This can happen when Git clients are
1037 62ee7d94 2023-01-10 thomas * done sending pack file data.
1039 62ee7d94 2023-01-10 thomas msgbuf_clear(&ibuf->w);
1040 62ee7d94 2023-01-10 thomas continue;
1041 62ee7d94 2023-01-10 thomas } else if (n == -1 && errno != EAGAIN) {
1042 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg_flush");
1043 62ee7d94 2023-01-10 thomas disconnect_on_error(client, err);
1046 62ee7d94 2023-01-10 thomas if (n == 0) {
1047 62ee7d94 2023-01-10 thomas /* Connection closed. */
1048 62ee7d94 2023-01-10 thomas err = got_error(GOT_ERR_EOF);
1049 62ee7d94 2023-01-10 thomas disconnect_on_error(client, err);
1055 62ee7d94 2023-01-10 thomas if ((events & EV_READ) == 0)
1058 62ee7d94 2023-01-10 thomas memset(&imsg, 0, sizeof(imsg));
1060 62ee7d94 2023-01-10 thomas while (err == NULL) {
1061 62ee7d94 2023-01-10 thomas err = gotd_imsg_recv(&imsg, ibuf, 0);
1062 62ee7d94 2023-01-10 thomas if (err) {
1063 62ee7d94 2023-01-10 thomas if (err->code == GOT_ERR_PRIVSEP_READ)
1064 62ee7d94 2023-01-10 thomas err = NULL;
1065 8e92c55c 2023-06-08 thomas else if (err->code == GOT_ERR_EOF &&
1066 8e92c55c 2023-06-08 thomas client->state == GOTD_STATE_EXPECT_CAPABILITIES) {
1068 8e92c55c 2023-06-08 thomas * The client has closed its socket before
1069 8e92c55c 2023-06-08 thomas * sending its capability announcement.
1070 8e92c55c 2023-06-08 thomas * This can happen when Git clients have
1071 8e92c55c 2023-06-08 thomas * no ref-updates to send.
1073 8e92c55c 2023-06-08 thomas disconnect_on_error(client, err);
1079 62ee7d94 2023-01-10 thomas evtimer_del(&client->tmo);
1081 62ee7d94 2023-01-10 thomas switch (imsg.hdr.type) {
1082 62ee7d94 2023-01-10 thomas case GOTD_IMSG_CAPABILITIES:
1083 62ee7d94 2023-01-10 thomas if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1084 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1085 62ee7d94 2023-01-10 thomas "unexpected capabilities received");
1088 62ee7d94 2023-01-10 thomas log_debug("receiving capabilities from uid %d",
1089 62ee7d94 2023-01-10 thomas client->euid);
1090 62ee7d94 2023-01-10 thomas err = recv_capabilities(client, &imsg);
1092 62ee7d94 2023-01-10 thomas case GOTD_IMSG_CAPABILITY:
1093 62ee7d94 2023-01-10 thomas if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1094 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1095 62ee7d94 2023-01-10 thomas "unexpected capability received");
1098 62ee7d94 2023-01-10 thomas err = recv_capability(client, &imsg);
1099 62ee7d94 2023-01-10 thomas if (err || client->ncapabilities < client->ncapa_alloc)
1101 62ee7d94 2023-01-10 thomas if (!client->is_writing) {
1102 62ee7d94 2023-01-10 thomas client->state = GOTD_STATE_EXPECT_WANT;
1103 98c7fd82 2023-01-23 thomas client->accept_flush_pkt = 1;
1104 62ee7d94 2023-01-10 thomas log_debug("uid %d: expecting want-lines",
1105 62ee7d94 2023-01-10 thomas client->euid);
1106 62ee7d94 2023-01-10 thomas } else if (client->is_writing) {
1107 62ee7d94 2023-01-10 thomas client->state = GOTD_STATE_EXPECT_REF_UPDATE;
1108 98c7fd82 2023-01-23 thomas client->accept_flush_pkt = 1;
1109 62ee7d94 2023-01-10 thomas log_debug("uid %d: expecting ref-update-lines",
1110 62ee7d94 2023-01-10 thomas client->euid);
1112 62ee7d94 2023-01-10 thomas fatalx("client %d is both reading and writing",
1113 62ee7d94 2023-01-10 thomas client->euid);
1115 62ee7d94 2023-01-10 thomas case GOTD_IMSG_WANT:
1116 62ee7d94 2023-01-10 thomas if (client->state != GOTD_STATE_EXPECT_WANT) {
1117 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1118 62ee7d94 2023-01-10 thomas "unexpected want-line received");
1121 62ee7d94 2023-01-10 thomas log_debug("received want-line from uid %d",
1122 62ee7d94 2023-01-10 thomas client->euid);
1123 62ee7d94 2023-01-10 thomas err = ensure_client_is_reading(client);
1124 62ee7d94 2023-01-10 thomas if (err)
1126 98c7fd82 2023-01-23 thomas client->accept_flush_pkt = 1;
1127 62ee7d94 2023-01-10 thomas err = forward_want(client, &imsg);
1129 62ee7d94 2023-01-10 thomas case GOTD_IMSG_REF_UPDATE:
1130 0445d8ec 2023-01-19 thomas if (client->state != GOTD_STATE_EXPECT_REF_UPDATE &&
1131 0445d8ec 2023-01-19 thomas client->state !=
1132 0445d8ec 2023-01-19 thomas GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1133 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1134 62ee7d94 2023-01-10 thomas "unexpected ref-update-line received");
1137 62ee7d94 2023-01-10 thomas log_debug("received ref-update-line from uid %d",
1138 62ee7d94 2023-01-10 thomas client->euid);
1139 62ee7d94 2023-01-10 thomas err = ensure_client_is_writing(client);
1140 62ee7d94 2023-01-10 thomas if (err)
1142 62ee7d94 2023-01-10 thomas err = forward_ref_update(client, &imsg);
1143 62ee7d94 2023-01-10 thomas if (err)
1145 62ee7d94 2023-01-10 thomas client->state = GOTD_STATE_EXPECT_MORE_REF_UPDATES;
1146 98c7fd82 2023-01-23 thomas client->accept_flush_pkt = 1;
1148 62ee7d94 2023-01-10 thomas case GOTD_IMSG_HAVE:
1149 62ee7d94 2023-01-10 thomas if (client->state != GOTD_STATE_EXPECT_HAVE) {
1150 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1151 62ee7d94 2023-01-10 thomas "unexpected have-line received");
1154 62ee7d94 2023-01-10 thomas log_debug("received have-line from uid %d",
1155 62ee7d94 2023-01-10 thomas client->euid);
1156 62ee7d94 2023-01-10 thomas err = ensure_client_is_reading(client);
1157 62ee7d94 2023-01-10 thomas if (err)
1159 62ee7d94 2023-01-10 thomas err = forward_have(client, &imsg);
1160 62ee7d94 2023-01-10 thomas if (err)
1162 98c7fd82 2023-01-23 thomas client->accept_flush_pkt = 1;
1164 62ee7d94 2023-01-10 thomas case GOTD_IMSG_FLUSH:
1165 62ee7d94 2023-01-10 thomas if (client->state == GOTD_STATE_EXPECT_WANT ||
1166 62ee7d94 2023-01-10 thomas client->state == GOTD_STATE_EXPECT_HAVE) {
1167 62ee7d94 2023-01-10 thomas err = ensure_client_is_reading(client);
1168 62ee7d94 2023-01-10 thomas if (err)
1170 62ee7d94 2023-01-10 thomas } else if (client->state ==
1171 62ee7d94 2023-01-10 thomas GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1172 62ee7d94 2023-01-10 thomas err = ensure_client_is_writing(client);
1173 62ee7d94 2023-01-10 thomas if (err)
1175 6110f5ef 2023-01-19 thomas } else if (client->state != GOTD_STATE_EXPECT_DONE) {
1176 98c7fd82 2023-01-23 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1177 98c7fd82 2023-01-23 thomas "unexpected flush-pkt received");
1180 98c7fd82 2023-01-23 thomas if (!client->accept_flush_pkt) {
1181 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1182 62ee7d94 2023-01-10 thomas "unexpected flush-pkt received");
1187 98c7fd82 2023-01-23 thomas * Accept just one flush packet at a time.
1188 98c7fd82 2023-01-23 thomas * Future client state transitions will set this flag
1189 98c7fd82 2023-01-23 thomas * again if another flush packet is expected.
1191 98c7fd82 2023-01-23 thomas client->accept_flush_pkt = 0;
1193 62ee7d94 2023-01-10 thomas log_debug("received flush-pkt from uid %d",
1194 62ee7d94 2023-01-10 thomas client->euid);
1195 62ee7d94 2023-01-10 thomas if (client->state == GOTD_STATE_EXPECT_WANT) {
1196 62ee7d94 2023-01-10 thomas client->state = GOTD_STATE_EXPECT_HAVE;
1197 62ee7d94 2023-01-10 thomas log_debug("uid %d: expecting have-lines",
1198 62ee7d94 2023-01-10 thomas client->euid);
1199 62ee7d94 2023-01-10 thomas } else if (client->state == GOTD_STATE_EXPECT_HAVE) {
1200 62ee7d94 2023-01-10 thomas client->state = GOTD_STATE_EXPECT_DONE;
1201 98c7fd82 2023-01-23 thomas client->accept_flush_pkt = 1;
1202 62ee7d94 2023-01-10 thomas log_debug("uid %d: expecting 'done'",
1203 62ee7d94 2023-01-10 thomas client->euid);
1204 62ee7d94 2023-01-10 thomas } else if (client->state ==
1205 62ee7d94 2023-01-10 thomas GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1206 62ee7d94 2023-01-10 thomas client->state = GOTD_STATE_EXPECT_PACKFILE;
1207 62ee7d94 2023-01-10 thomas log_debug("uid %d: expecting packfile",
1208 62ee7d94 2023-01-10 thomas client->euid);
1209 62ee7d94 2023-01-10 thomas err = recv_packfile(client);
1210 6110f5ef 2023-01-19 thomas } else if (client->state != GOTD_STATE_EXPECT_DONE) {
1211 62ee7d94 2023-01-10 thomas /* should not happen, see above */
1212 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1213 62ee7d94 2023-01-10 thomas "unexpected client state");
1217 62ee7d94 2023-01-10 thomas case GOTD_IMSG_DONE:
1218 62ee7d94 2023-01-10 thomas if (client->state != GOTD_STATE_EXPECT_HAVE &&
1219 62ee7d94 2023-01-10 thomas client->state != GOTD_STATE_EXPECT_DONE) {
1220 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1221 62ee7d94 2023-01-10 thomas "unexpected flush-pkt received");
1224 62ee7d94 2023-01-10 thomas log_debug("received 'done' from uid %d", client->euid);
1225 62ee7d94 2023-01-10 thomas err = ensure_client_is_reading(client);
1226 62ee7d94 2023-01-10 thomas if (err)
1228 62ee7d94 2023-01-10 thomas client->state = GOTD_STATE_DONE;
1229 98c7fd82 2023-01-23 thomas client->accept_flush_pkt = 1;
1230 62ee7d94 2023-01-10 thomas err = send_packfile(client);
1232 62ee7d94 2023-01-10 thomas default:
1233 62ee7d94 2023-01-10 thomas log_debug("unexpected imsg %d", imsg.hdr.type);
1234 62ee7d94 2023-01-10 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
1238 62ee7d94 2023-01-10 thomas imsg_free(&imsg);
1241 62ee7d94 2023-01-10 thomas if (err) {
1242 62ee7d94 2023-01-10 thomas if (err->code != GOT_ERR_EOF ||
1243 62ee7d94 2023-01-10 thomas client->state != GOTD_STATE_EXPECT_PACKFILE)
1244 62ee7d94 2023-01-10 thomas disconnect_on_error(client, err);
1245 62ee7d94 2023-01-10 thomas } else {
1246 62ee7d94 2023-01-10 thomas gotd_imsg_event_add(iev);
1247 62ee7d94 2023-01-10 thomas evtimer_add(&client->tmo, &gotd_session.request_timeout);
1251 62ee7d94 2023-01-10 thomas static const struct got_error *
1252 62ee7d94 2023-01-10 thomas list_refs_request(void)
1254 62ee7d94 2023-01-10 thomas static const struct got_error *err;
1255 62ee7d94 2023-01-10 thomas struct gotd_session_client *client = &gotd_session_client;
1256 62ee7d94 2023-01-10 thomas struct gotd_imsgev *iev = &client->repo_child_iev;
1257 62ee7d94 2023-01-10 thomas struct gotd_imsg_list_refs_internal ilref;
1260 62ee7d94 2023-01-10 thomas if (client->state != GOTD_STATE_EXPECT_LIST_REFS)
1261 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
1263 62ee7d94 2023-01-10 thomas memset(&ilref, 0, sizeof(ilref));
1264 62ee7d94 2023-01-10 thomas ilref.client_id = client->id;
1266 62ee7d94 2023-01-10 thomas fd = dup(client->fd);
1267 62ee7d94 2023-01-10 thomas if (fd == -1)
1268 62ee7d94 2023-01-10 thomas return got_error_from_errno("dup");
1270 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(iev, GOTD_IMSG_LIST_REFS_INTERNAL,
1271 62ee7d94 2023-01-10 thomas PROC_SESSION, fd, &ilref, sizeof(ilref)) == -1) {
1272 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose LIST_REFS_INTERNAL");
1273 62ee7d94 2023-01-10 thomas close(fd);
1274 62ee7d94 2023-01-10 thomas return err;
1277 62ee7d94 2023-01-10 thomas client->state = GOTD_STATE_EXPECT_CAPABILITIES;
1278 62ee7d94 2023-01-10 thomas log_debug("uid %d: expecting capabilities", client->euid);
1279 62ee7d94 2023-01-10 thomas return NULL;
1282 62ee7d94 2023-01-10 thomas static const struct got_error *
1283 62ee7d94 2023-01-10 thomas recv_connect(struct imsg *imsg)
1285 62ee7d94 2023-01-10 thomas struct gotd_session_client *client = &gotd_session_client;
1286 62ee7d94 2023-01-10 thomas struct gotd_imsg_connect iconnect;
1287 62ee7d94 2023-01-10 thomas size_t datalen;
1289 62ee7d94 2023-01-10 thomas if (client->state != GOTD_STATE_EXPECT_LIST_REFS)
1290 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
1292 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1293 62ee7d94 2023-01-10 thomas if (datalen != sizeof(iconnect))
1294 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1295 62ee7d94 2023-01-10 thomas memcpy(&iconnect, imsg->data, sizeof(iconnect));
1297 62ee7d94 2023-01-10 thomas if (imsg->fd == -1)
1298 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
1300 62ee7d94 2023-01-10 thomas client->fd = imsg->fd;
1301 62ee7d94 2023-01-10 thomas client->euid = iconnect.euid;
1302 62ee7d94 2023-01-10 thomas client->egid = iconnect.egid;
1304 62ee7d94 2023-01-10 thomas imsg_init(&client->iev.ibuf, client->fd);
1305 8cb46987 2023-02-07 thomas client->iev.handler = session_dispatch_client;
1306 62ee7d94 2023-01-10 thomas client->iev.events = EV_READ;
1307 62ee7d94 2023-01-10 thomas client->iev.handler_arg = NULL;
1308 62ee7d94 2023-01-10 thomas event_set(&client->iev.ev, client->iev.ibuf.fd, EV_READ,
1309 8cb46987 2023-02-07 thomas session_dispatch_client, &client->iev);
1310 62ee7d94 2023-01-10 thomas gotd_imsg_event_add(&client->iev);
1311 62ee7d94 2023-01-10 thomas evtimer_set(&client->tmo, gotd_request_timeout, client);
1313 62ee7d94 2023-01-10 thomas return NULL;
1316 62ee7d94 2023-01-10 thomas static const struct got_error *
1317 62ee7d94 2023-01-10 thomas recv_repo_child(struct imsg *imsg)
1319 62ee7d94 2023-01-10 thomas struct gotd_imsg_connect_repo_child ichild;
1320 62ee7d94 2023-01-10 thomas struct gotd_session_client *client = &gotd_session_client;
1321 62ee7d94 2023-01-10 thomas size_t datalen;
1323 62ee7d94 2023-01-10 thomas if (client->state != GOTD_STATE_EXPECT_LIST_REFS)
1324 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
1326 62ee7d94 2023-01-10 thomas /* We should already have received a pipe to the listener. */
1327 62ee7d94 2023-01-10 thomas if (client->fd == -1)
1328 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
1330 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1331 62ee7d94 2023-01-10 thomas if (datalen != sizeof(ichild))
1332 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1334 62ee7d94 2023-01-10 thomas memcpy(&ichild, imsg->data, sizeof(ichild));
1336 62ee7d94 2023-01-10 thomas client->id = ichild.client_id;
1337 62ee7d94 2023-01-10 thomas if (ichild.proc_id == PROC_REPO_WRITE)
1338 62ee7d94 2023-01-10 thomas client->is_writing = 1;
1339 62ee7d94 2023-01-10 thomas else if (ichild.proc_id == PROC_REPO_READ)
1340 62ee7d94 2023-01-10 thomas client->is_writing = 0;
1342 62ee7d94 2023-01-10 thomas return got_error_msg(GOT_ERR_PRIVSEP_MSG,
1343 62ee7d94 2023-01-10 thomas "bad child process type");
1345 62ee7d94 2023-01-10 thomas if (imsg->fd == -1)
1346 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
1348 62ee7d94 2023-01-10 thomas imsg_init(&client->repo_child_iev.ibuf, imsg->fd);
1349 62ee7d94 2023-01-10 thomas client->repo_child_iev.handler = session_dispatch_repo_child;
1350 62ee7d94 2023-01-10 thomas client->repo_child_iev.events = EV_READ;
1351 62ee7d94 2023-01-10 thomas client->repo_child_iev.handler_arg = NULL;
1352 62ee7d94 2023-01-10 thomas event_set(&client->repo_child_iev.ev, client->repo_child_iev.ibuf.fd,
1353 62ee7d94 2023-01-10 thomas EV_READ, session_dispatch_repo_child, &client->repo_child_iev);
1354 62ee7d94 2023-01-10 thomas gotd_imsg_event_add(&client->repo_child_iev);
1356 62ee7d94 2023-01-10 thomas /* The "recvfd" pledge promise is no longer needed. */
1357 62ee7d94 2023-01-10 thomas if (pledge("stdio rpath wpath cpath sendfd fattr flock", NULL) == -1)
1358 62ee7d94 2023-01-10 thomas fatal("pledge");
1360 62ee7d94 2023-01-10 thomas return NULL;
1363 62ee7d94 2023-01-10 thomas static void
1364 62ee7d94 2023-01-10 thomas session_dispatch(int fd, short event, void *arg)
1366 62ee7d94 2023-01-10 thomas struct gotd_imsgev *iev = arg;
1367 62ee7d94 2023-01-10 thomas struct imsgbuf *ibuf = &iev->ibuf;
1368 62ee7d94 2023-01-10 thomas struct gotd_session_client *client = &gotd_session_client;
1369 62ee7d94 2023-01-10 thomas ssize_t n;
1370 62ee7d94 2023-01-10 thomas int shut = 0;
1371 62ee7d94 2023-01-10 thomas struct imsg imsg;
1373 62ee7d94 2023-01-10 thomas if (event & EV_READ) {
1374 62ee7d94 2023-01-10 thomas if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1375 62ee7d94 2023-01-10 thomas fatal("imsg_read error");
1376 62ee7d94 2023-01-10 thomas if (n == 0) {
1377 62ee7d94 2023-01-10 thomas /* Connection closed. */
1378 62ee7d94 2023-01-10 thomas shut = 1;
1379 62ee7d94 2023-01-10 thomas goto done;
1383 62ee7d94 2023-01-10 thomas if (event & EV_WRITE) {
1384 62ee7d94 2023-01-10 thomas n = msgbuf_write(&ibuf->w);
1385 62ee7d94 2023-01-10 thomas if (n == -1 && errno != EAGAIN)
1386 62ee7d94 2023-01-10 thomas fatal("msgbuf_write");
1387 62ee7d94 2023-01-10 thomas if (n == 0) {
1388 62ee7d94 2023-01-10 thomas /* Connection closed. */
1389 62ee7d94 2023-01-10 thomas shut = 1;
1390 62ee7d94 2023-01-10 thomas goto done;
1394 62ee7d94 2023-01-10 thomas for (;;) {
1395 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
1396 62ee7d94 2023-01-10 thomas uint32_t client_id = 0;
1397 62ee7d94 2023-01-10 thomas int do_disconnect = 0, do_list_refs = 0;
1399 62ee7d94 2023-01-10 thomas if ((n = imsg_get(ibuf, &imsg)) == -1)
1400 62ee7d94 2023-01-10 thomas fatal("%s: imsg_get error", __func__);
1401 62ee7d94 2023-01-10 thomas if (n == 0) /* No more messages. */
1404 62ee7d94 2023-01-10 thomas switch (imsg.hdr.type) {
1405 62ee7d94 2023-01-10 thomas case GOTD_IMSG_ERROR:
1406 62ee7d94 2023-01-10 thomas do_disconnect = 1;
1407 62ee7d94 2023-01-10 thomas err = gotd_imsg_recv_error(&client_id, &imsg);
1409 62ee7d94 2023-01-10 thomas case GOTD_IMSG_CONNECT:
1410 62ee7d94 2023-01-10 thomas err = recv_connect(&imsg);
1412 62ee7d94 2023-01-10 thomas case GOTD_IMSG_DISCONNECT:
1413 62ee7d94 2023-01-10 thomas do_disconnect = 1;
1415 62ee7d94 2023-01-10 thomas case GOTD_IMSG_CONNECT_REPO_CHILD:
1416 62ee7d94 2023-01-10 thomas err = recv_repo_child(&imsg);
1417 62ee7d94 2023-01-10 thomas if (err)
1419 62ee7d94 2023-01-10 thomas do_list_refs = 1;
1421 62ee7d94 2023-01-10 thomas default:
1422 62ee7d94 2023-01-10 thomas log_debug("unexpected imsg %d", imsg.hdr.type);
1425 62ee7d94 2023-01-10 thomas imsg_free(&imsg);
1427 62ee7d94 2023-01-10 thomas if (do_disconnect) {
1428 62ee7d94 2023-01-10 thomas if (err)
1429 62ee7d94 2023-01-10 thomas disconnect_on_error(client, err);
1431 62ee7d94 2023-01-10 thomas disconnect(client);
1432 62ee7d94 2023-01-10 thomas } else if (do_list_refs)
1433 62ee7d94 2023-01-10 thomas err = list_refs_request();
1435 62ee7d94 2023-01-10 thomas if (err)
1436 62ee7d94 2023-01-10 thomas log_warnx("uid %d: %s", client->euid, err->msg);
1439 62ee7d94 2023-01-10 thomas if (!shut) {
1440 62ee7d94 2023-01-10 thomas gotd_imsg_event_add(iev);
1441 62ee7d94 2023-01-10 thomas } else {
1442 62ee7d94 2023-01-10 thomas /* This pipe is dead. Remove its event handler */
1443 62ee7d94 2023-01-10 thomas event_del(&iev->ev);
1444 62ee7d94 2023-01-10 thomas event_loopexit(NULL);
1449 62ee7d94 2023-01-10 thomas session_main(const char *title, const char *repo_path,
1450 62ee7d94 2023-01-10 thomas int *pack_fds, int *temp_fds, struct timeval *request_timeout)
1452 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
1453 62ee7d94 2023-01-10 thomas struct event evsigint, evsigterm, evsighup, evsigusr1;
1455 62ee7d94 2023-01-10 thomas gotd_session.title = title;
1456 62ee7d94 2023-01-10 thomas gotd_session.pid = getpid();
1457 62ee7d94 2023-01-10 thomas gotd_session.pack_fds = pack_fds;
1458 62ee7d94 2023-01-10 thomas gotd_session.temp_fds = temp_fds;
1459 62ee7d94 2023-01-10 thomas memcpy(&gotd_session.request_timeout, request_timeout,
1460 62ee7d94 2023-01-10 thomas sizeof(gotd_session.request_timeout));
1462 62ee7d94 2023-01-10 thomas err = got_repo_open(&gotd_session.repo, repo_path, NULL, pack_fds);
1463 62ee7d94 2023-01-10 thomas if (err)
1464 62ee7d94 2023-01-10 thomas goto done;
1465 62ee7d94 2023-01-10 thomas if (!got_repo_is_bare(gotd_session.repo)) {
1466 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1467 62ee7d94 2023-01-10 thomas "bare git repository required");
1468 62ee7d94 2023-01-10 thomas goto done;
1471 62ee7d94 2023-01-10 thomas got_repo_temp_fds_set(gotd_session.repo, temp_fds);
1473 62ee7d94 2023-01-10 thomas signal_set(&evsigint, SIGINT, gotd_session_sighdlr, NULL);
1474 62ee7d94 2023-01-10 thomas signal_set(&evsigterm, SIGTERM, gotd_session_sighdlr, NULL);
1475 62ee7d94 2023-01-10 thomas signal_set(&evsighup, SIGHUP, gotd_session_sighdlr, NULL);
1476 62ee7d94 2023-01-10 thomas signal_set(&evsigusr1, SIGUSR1, gotd_session_sighdlr, NULL);
1477 62ee7d94 2023-01-10 thomas signal(SIGPIPE, SIG_IGN);
1479 62ee7d94 2023-01-10 thomas signal_add(&evsigint, NULL);
1480 62ee7d94 2023-01-10 thomas signal_add(&evsigterm, NULL);
1481 62ee7d94 2023-01-10 thomas signal_add(&evsighup, NULL);
1482 62ee7d94 2023-01-10 thomas signal_add(&evsigusr1, NULL);
1484 62ee7d94 2023-01-10 thomas gotd_session_client.state = GOTD_STATE_EXPECT_LIST_REFS;
1485 62ee7d94 2023-01-10 thomas gotd_session_client.fd = -1;
1486 62ee7d94 2023-01-10 thomas gotd_session_client.nref_updates = -1;
1487 a6f25078 2023-01-10 thomas gotd_session_client.delta_cache_fd = -1;
1488 98c7fd82 2023-01-23 thomas gotd_session_client.accept_flush_pkt = 1;
1490 62ee7d94 2023-01-10 thomas imsg_init(&gotd_session.parent_iev.ibuf, GOTD_FILENO_MSG_PIPE);
1491 62ee7d94 2023-01-10 thomas gotd_session.parent_iev.handler = session_dispatch;
1492 62ee7d94 2023-01-10 thomas gotd_session.parent_iev.events = EV_READ;
1493 62ee7d94 2023-01-10 thomas gotd_session.parent_iev.handler_arg = NULL;
1494 62ee7d94 2023-01-10 thomas event_set(&gotd_session.parent_iev.ev, gotd_session.parent_iev.ibuf.fd,
1495 62ee7d94 2023-01-10 thomas EV_READ, session_dispatch, &gotd_session.parent_iev);
1496 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&gotd_session.parent_iev,
1497 62ee7d94 2023-01-10 thomas GOTD_IMSG_CLIENT_SESSION_READY, PROC_SESSION, -1, NULL, 0) == -1) {
1498 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose CLIENT_SESSION_READY");
1499 62ee7d94 2023-01-10 thomas goto done;
1502 62ee7d94 2023-01-10 thomas event_dispatch();
1504 62ee7d94 2023-01-10 thomas if (err)
1505 62ee7d94 2023-01-10 thomas log_warnx("%s: %s", title, err->msg);
1506 62ee7d94 2023-01-10 thomas gotd_session_shutdown();
1510 62ee7d94 2023-01-10 thomas gotd_session_shutdown(void)
1512 5330ab76 2023-02-17 thomas log_debug("shutting down");
1513 62ee7d94 2023-01-10 thomas if (gotd_session.repo)
1514 62ee7d94 2023-01-10 thomas got_repo_close(gotd_session.repo);
1515 62ee7d94 2023-01-10 thomas got_repo_pack_fds_close(gotd_session.pack_fds);
1516 62ee7d94 2023-01-10 thomas got_repo_temp_fds_close(gotd_session.temp_fds);
1517 62ee7d94 2023-01-10 thomas exit(0);