2 ae7c1b78 2023-01-10 stsp * Copyright (c) 2022, 2023 Stefan Sperling <stsp@openbsd.org>
4 ae7c1b78 2023-01-10 stsp * Permission to use, copy, modify, and distribute this software for any
5 ae7c1b78 2023-01-10 stsp * purpose with or without fee is hereby granted, provided that the above
6 ae7c1b78 2023-01-10 stsp * copyright notice and this permission notice appear in all copies.
8 ae7c1b78 2023-01-10 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 ae7c1b78 2023-01-10 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 ae7c1b78 2023-01-10 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 ae7c1b78 2023-01-10 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 ae7c1b78 2023-01-10 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 ae7c1b78 2023-01-10 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 ae7c1b78 2023-01-10 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 ae7c1b78 2023-01-10 stsp #include <sys/types.h>
18 ae7c1b78 2023-01-10 stsp #include <sys/queue.h>
19 ae7c1b78 2023-01-10 stsp #include <sys/tree.h>
20 ae7c1b78 2023-01-10 stsp #include <sys/socket.h>
21 ae7c1b78 2023-01-10 stsp #include <sys/uio.h>
23 ae7c1b78 2023-01-10 stsp #include <errno.h>
24 ae7c1b78 2023-01-10 stsp #include <event.h>
25 ae7c1b78 2023-01-10 stsp #include <limits.h>
26 ae7c1b78 2023-01-10 stsp #include <sha1.h>
27 ae7c1b78 2023-01-10 stsp #include <signal.h>
28 ae7c1b78 2023-01-10 stsp #include <stdint.h>
29 ae7c1b78 2023-01-10 stsp #include <stdio.h>
30 ae7c1b78 2023-01-10 stsp #include <stdlib.h>
31 ae7c1b78 2023-01-10 stsp #include <string.h>
32 ae7c1b78 2023-01-10 stsp #include <imsg.h>
33 ae7c1b78 2023-01-10 stsp #include <unistd.h>
35 ae7c1b78 2023-01-10 stsp #include "got_error.h"
36 ae7c1b78 2023-01-10 stsp #include "got_repository.h"
37 ae7c1b78 2023-01-10 stsp #include "got_object.h"
38 ae7c1b78 2023-01-10 stsp #include "got_path.h"
39 ae7c1b78 2023-01-10 stsp #include "got_reference.h"
40 ae7c1b78 2023-01-10 stsp #include "got_opentemp.h"
42 ae7c1b78 2023-01-10 stsp #include "got_lib_sha1.h"
43 ae7c1b78 2023-01-10 stsp #include "got_lib_delta.h"
44 ae7c1b78 2023-01-10 stsp #include "got_lib_object.h"
45 ae7c1b78 2023-01-10 stsp #include "got_lib_object_cache.h"
46 ae7c1b78 2023-01-10 stsp #include "got_lib_pack.h"
47 ae7c1b78 2023-01-10 stsp #include "got_lib_repository.h"
48 ae7c1b78 2023-01-10 stsp #include "got_lib_gitproto.h"
50 ae7c1b78 2023-01-10 stsp #include "gotd.h"
51 ae7c1b78 2023-01-10 stsp #include "log.h"
52 ae7c1b78 2023-01-10 stsp #include "session.h"
55 ae7c1b78 2023-01-10 stsp static struct gotd_session {
57 ae7c1b78 2023-01-10 stsp const char *title;
58 ae7c1b78 2023-01-10 stsp struct got_repository *repo;
59 ae7c1b78 2023-01-10 stsp int *pack_fds;
60 ae7c1b78 2023-01-10 stsp int *temp_fds;
61 ae7c1b78 2023-01-10 stsp struct gotd_imsgev parent_iev;
62 ae7c1b78 2023-01-10 stsp struct timeval request_timeout;
63 ae7c1b78 2023-01-10 stsp } gotd_session;
65 ae7c1b78 2023-01-10 stsp static struct gotd_session_client {
66 ae7c1b78 2023-01-10 stsp enum gotd_client_state state;
67 ae7c1b78 2023-01-10 stsp int is_writing;
68 ae7c1b78 2023-01-10 stsp struct gotd_client_capability *capabilities;
69 ae7c1b78 2023-01-10 stsp size_t ncapa_alloc;
70 ae7c1b78 2023-01-10 stsp size_t ncapabilities;
71 ae7c1b78 2023-01-10 stsp uint32_t id;
73 ae7c1b78 2023-01-10 stsp int delta_cache_fd;
74 ae7c1b78 2023-01-10 stsp struct gotd_imsgev iev;
75 ae7c1b78 2023-01-10 stsp struct gotd_imsgev repo_child_iev;
76 ae7c1b78 2023-01-10 stsp struct event tmo;
79 ae7c1b78 2023-01-10 stsp char *packfile_path;
80 ae7c1b78 2023-01-10 stsp char *packidx_path;
81 ae7c1b78 2023-01-10 stsp int nref_updates;
82 ae7c1b78 2023-01-10 stsp } gotd_session_client;
84 ae7c1b78 2023-01-10 stsp void gotd_session_sighdlr(int sig, short event, void *arg);
85 ae7c1b78 2023-01-10 stsp static void gotd_session_shutdown(void);
88 ae7c1b78 2023-01-10 stsp disconnect(struct gotd_session_client *client)
90 ae7c1b78 2023-01-10 stsp log_debug("uid %d: disconnecting", client->euid);
92 ae7c1b78 2023-01-10 stsp if (gotd_imsg_compose_event(&gotd_session.parent_iev,
93 ae7c1b78 2023-01-10 stsp GOTD_IMSG_DISCONNECT, PROC_SESSION, -1, NULL, 0) == -1)
94 ae7c1b78 2023-01-10 stsp log_warn("imsg compose DISCONNECT");
96 ae7c1b78 2023-01-10 stsp imsg_clear(&client->repo_child_iev.ibuf);
97 ae7c1b78 2023-01-10 stsp event_del(&client->repo_child_iev.ev);
98 ae7c1b78 2023-01-10 stsp evtimer_del(&client->tmo);
99 ae7c1b78 2023-01-10 stsp close(client->fd);
100 ae7c1b78 2023-01-10 stsp if (client->delta_cache_fd != -1)
101 ae7c1b78 2023-01-10 stsp close(client->delta_cache_fd);
102 ae7c1b78 2023-01-10 stsp if (client->packfile_path) {
103 ae7c1b78 2023-01-10 stsp if (unlink(client->packfile_path) == -1 && errno != ENOENT)
104 ae7c1b78 2023-01-10 stsp log_warn("unlink %s: ", client->packfile_path);
105 ae7c1b78 2023-01-10 stsp free(client->packfile_path);
107 ae7c1b78 2023-01-10 stsp if (client->packidx_path) {
108 ae7c1b78 2023-01-10 stsp if (unlink(client->packidx_path) == -1 && errno != ENOENT)
109 ae7c1b78 2023-01-10 stsp log_warn("unlink %s: ", client->packidx_path);
110 ae7c1b78 2023-01-10 stsp free(client->packidx_path);
112 ae7c1b78 2023-01-10 stsp free(client->capabilities);
114 ae7c1b78 2023-01-10 stsp gotd_session_shutdown();
117 ae7c1b78 2023-01-10 stsp static void
118 ae7c1b78 2023-01-10 stsp disconnect_on_error(struct gotd_session_client *client,
119 ae7c1b78 2023-01-10 stsp const struct got_error *err)
121 ae7c1b78 2023-01-10 stsp struct imsgbuf ibuf;
123 ae7c1b78 2023-01-10 stsp log_warnx("uid %d: %s", client->euid, err->msg);
124 ae7c1b78 2023-01-10 stsp if (err->code != GOT_ERR_EOF) {
125 ae7c1b78 2023-01-10 stsp imsg_init(&ibuf, client->fd);
126 ae7c1b78 2023-01-10 stsp gotd_imsg_send_error(&ibuf, 0, PROC_SESSION, err);
127 ae7c1b78 2023-01-10 stsp imsg_clear(&ibuf);
130 ae7c1b78 2023-01-10 stsp disconnect(client);
133 ae7c1b78 2023-01-10 stsp static void
134 ae7c1b78 2023-01-10 stsp gotd_request_timeout(int fd, short events, void *arg)
136 ae7c1b78 2023-01-10 stsp struct gotd_session_client *client = arg;
138 ae7c1b78 2023-01-10 stsp log_debug("disconnecting uid %d due to timeout", client->euid);
139 ae7c1b78 2023-01-10 stsp disconnect(client);
143 ae7c1b78 2023-01-10 stsp gotd_session_sighdlr(int sig, short event, void *arg)
146 ae7c1b78 2023-01-10 stsp * Normal signal handler rules don't apply because libevent
147 ae7c1b78 2023-01-10 stsp * decouples for us.
150 ae7c1b78 2023-01-10 stsp switch (sig) {
151 ae7c1b78 2023-01-10 stsp case SIGHUP:
152 ae7c1b78 2023-01-10 stsp log_info("%s: ignoring SIGHUP", __func__);
154 ae7c1b78 2023-01-10 stsp case SIGUSR1:
155 ae7c1b78 2023-01-10 stsp log_info("%s: ignoring SIGUSR1", __func__);
157 ae7c1b78 2023-01-10 stsp case SIGTERM:
158 ae7c1b78 2023-01-10 stsp case SIGINT:
159 ae7c1b78 2023-01-10 stsp gotd_session_shutdown();
160 ae7c1b78 2023-01-10 stsp /* NOTREACHED */
163 ae7c1b78 2023-01-10 stsp fatalx("unexpected signal");
167 ae7c1b78 2023-01-10 stsp static const struct got_error *
168 ae7c1b78 2023-01-10 stsp recv_packfile_done(uint32_t *client_id, struct imsg *imsg)
170 ae7c1b78 2023-01-10 stsp struct gotd_imsg_packfile_done idone;
171 ae7c1b78 2023-01-10 stsp size_t datalen;
173 ae7c1b78 2023-01-10 stsp log_debug("packfile-done received");
175 ae7c1b78 2023-01-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
176 ae7c1b78 2023-01-10 stsp if (datalen != sizeof(idone))
177 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
178 ae7c1b78 2023-01-10 stsp memcpy(&idone, imsg->data, sizeof(idone));
180 ae7c1b78 2023-01-10 stsp *client_id = idone.client_id;
181 ae7c1b78 2023-01-10 stsp return NULL;
184 ae7c1b78 2023-01-10 stsp static const struct got_error *
185 ae7c1b78 2023-01-10 stsp recv_packfile_install(uint32_t *client_id, struct imsg *imsg)
187 ae7c1b78 2023-01-10 stsp struct gotd_imsg_packfile_install inst;
188 ae7c1b78 2023-01-10 stsp size_t datalen;
190 ae7c1b78 2023-01-10 stsp log_debug("packfile-install received");
192 ae7c1b78 2023-01-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
193 ae7c1b78 2023-01-10 stsp if (datalen != sizeof(inst))
194 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
195 ae7c1b78 2023-01-10 stsp memcpy(&inst, imsg->data, sizeof(inst));
197 ae7c1b78 2023-01-10 stsp *client_id = inst.client_id;
198 ae7c1b78 2023-01-10 stsp return NULL;
201 ae7c1b78 2023-01-10 stsp static const struct got_error *
202 ae7c1b78 2023-01-10 stsp recv_ref_updates_start(uint32_t *client_id, struct imsg *imsg)
204 ae7c1b78 2023-01-10 stsp struct gotd_imsg_ref_updates_start istart;
205 ae7c1b78 2023-01-10 stsp size_t datalen;
207 ae7c1b78 2023-01-10 stsp log_debug("ref-updates-start received");
209 ae7c1b78 2023-01-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
210 ae7c1b78 2023-01-10 stsp if (datalen != sizeof(istart))
211 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
212 ae7c1b78 2023-01-10 stsp memcpy(&istart, imsg->data, sizeof(istart));
214 ae7c1b78 2023-01-10 stsp *client_id = istart.client_id;
215 ae7c1b78 2023-01-10 stsp return NULL;
218 ae7c1b78 2023-01-10 stsp static const struct got_error *
219 ae7c1b78 2023-01-10 stsp recv_ref_update(uint32_t *client_id, struct imsg *imsg)
221 ae7c1b78 2023-01-10 stsp struct gotd_imsg_ref_update iref;
222 ae7c1b78 2023-01-10 stsp size_t datalen;
224 ae7c1b78 2023-01-10 stsp log_debug("ref-update received");
226 ae7c1b78 2023-01-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
227 ae7c1b78 2023-01-10 stsp if (datalen < sizeof(iref))
228 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
229 ae7c1b78 2023-01-10 stsp memcpy(&iref, imsg->data, sizeof(iref));
231 ae7c1b78 2023-01-10 stsp *client_id = iref.client_id;
232 ae7c1b78 2023-01-10 stsp return NULL;
235 ae7c1b78 2023-01-10 stsp static const struct got_error *
236 ae7c1b78 2023-01-10 stsp send_ref_update_ok(struct gotd_session_client *client,
237 ae7c1b78 2023-01-10 stsp struct gotd_imsg_ref_update *iref, const char *refname)
239 ae7c1b78 2023-01-10 stsp struct gotd_imsg_ref_update_ok iok;
240 ae7c1b78 2023-01-10 stsp struct gotd_imsgev *iev = &client->iev;
241 ae7c1b78 2023-01-10 stsp struct ibuf *wbuf;
242 ae7c1b78 2023-01-10 stsp size_t len;
244 ae7c1b78 2023-01-10 stsp memset(&iok, 0, sizeof(iok));
245 ae7c1b78 2023-01-10 stsp iok.client_id = client->id;
246 ae7c1b78 2023-01-10 stsp memcpy(iok.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
247 ae7c1b78 2023-01-10 stsp memcpy(iok.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
248 ae7c1b78 2023-01-10 stsp iok.name_len = strlen(refname);
250 ae7c1b78 2023-01-10 stsp len = sizeof(iok) + iok.name_len;
251 ae7c1b78 2023-01-10 stsp wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE_OK,
252 ae7c1b78 2023-01-10 stsp PROC_SESSION, gotd_session.pid, len);
253 ae7c1b78 2023-01-10 stsp if (wbuf == NULL)
254 ae7c1b78 2023-01-10 stsp return got_error_from_errno("imsg_create REF_UPDATE_OK");
256 ae7c1b78 2023-01-10 stsp if (imsg_add(wbuf, &iok, sizeof(iok)) == -1)
257 ae7c1b78 2023-01-10 stsp return got_error_from_errno("imsg_add REF_UPDATE_OK");
258 ae7c1b78 2023-01-10 stsp if (imsg_add(wbuf, refname, iok.name_len) == -1)
259 ae7c1b78 2023-01-10 stsp return got_error_from_errno("imsg_add REF_UPDATE_OK");
261 ae7c1b78 2023-01-10 stsp wbuf->fd = -1;
262 ae7c1b78 2023-01-10 stsp imsg_close(&iev->ibuf, wbuf);
263 ae7c1b78 2023-01-10 stsp gotd_imsg_event_add(iev);
264 ae7c1b78 2023-01-10 stsp return NULL;
267 ae7c1b78 2023-01-10 stsp static void
268 ae7c1b78 2023-01-10 stsp send_refs_updated(struct gotd_session_client *client)
270 ae7c1b78 2023-01-10 stsp if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_REFS_UPDATED,
271 ae7c1b78 2023-01-10 stsp PROC_SESSION, -1, NULL, 0) == -1)
272 ae7c1b78 2023-01-10 stsp log_warn("imsg compose REFS_UPDATED");
275 ae7c1b78 2023-01-10 stsp static const struct got_error *
276 ae7c1b78 2023-01-10 stsp send_ref_update_ng(struct gotd_session_client *client,
277 ae7c1b78 2023-01-10 stsp struct gotd_imsg_ref_update *iref, const char *refname,
278 ae7c1b78 2023-01-10 stsp const char *reason)
280 ae7c1b78 2023-01-10 stsp const struct got_error *ng_err;
281 ae7c1b78 2023-01-10 stsp struct gotd_imsg_ref_update_ng ing;
282 ae7c1b78 2023-01-10 stsp struct gotd_imsgev *iev = &client->iev;
283 ae7c1b78 2023-01-10 stsp struct ibuf *wbuf;
284 ae7c1b78 2023-01-10 stsp size_t len;
286 ae7c1b78 2023-01-10 stsp memset(&ing, 0, sizeof(ing));
287 ae7c1b78 2023-01-10 stsp ing.client_id = client->id;
288 ae7c1b78 2023-01-10 stsp memcpy(ing.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
289 ae7c1b78 2023-01-10 stsp memcpy(ing.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
290 ae7c1b78 2023-01-10 stsp ing.name_len = strlen(refname);
292 ae7c1b78 2023-01-10 stsp ng_err = got_error_fmt(GOT_ERR_REF_BUSY, "%s", reason);
293 ae7c1b78 2023-01-10 stsp ing.reason_len = strlen(ng_err->msg);
295 ae7c1b78 2023-01-10 stsp len = sizeof(ing) + ing.name_len + ing.reason_len;
296 ae7c1b78 2023-01-10 stsp wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE_NG,
297 ae7c1b78 2023-01-10 stsp PROC_SESSION, gotd_session.pid, len);
298 ae7c1b78 2023-01-10 stsp if (wbuf == NULL)
299 ae7c1b78 2023-01-10 stsp return got_error_from_errno("imsg_create REF_UPDATE_NG");
301 ae7c1b78 2023-01-10 stsp if (imsg_add(wbuf, &ing, sizeof(ing)) == -1)
302 ae7c1b78 2023-01-10 stsp return got_error_from_errno("imsg_add REF_UPDATE_NG");
303 ae7c1b78 2023-01-10 stsp if (imsg_add(wbuf, refname, ing.name_len) == -1)
304 ae7c1b78 2023-01-10 stsp return got_error_from_errno("imsg_add REF_UPDATE_NG");
305 ae7c1b78 2023-01-10 stsp if (imsg_add(wbuf, ng_err->msg, ing.reason_len) == -1)
306 ae7c1b78 2023-01-10 stsp return got_error_from_errno("imsg_add REF_UPDATE_NG");
308 ae7c1b78 2023-01-10 stsp wbuf->fd = -1;
309 ae7c1b78 2023-01-10 stsp imsg_close(&iev->ibuf, wbuf);
310 ae7c1b78 2023-01-10 stsp gotd_imsg_event_add(iev);
311 ae7c1b78 2023-01-10 stsp return NULL;
314 ae7c1b78 2023-01-10 stsp static const struct got_error *
315 ae7c1b78 2023-01-10 stsp install_pack(struct gotd_session_client *client, const char *repo_path,
316 ae7c1b78 2023-01-10 stsp struct imsg *imsg)
318 ae7c1b78 2023-01-10 stsp const struct got_error *err = NULL;
319 ae7c1b78 2023-01-10 stsp struct gotd_imsg_packfile_install inst;
320 ae7c1b78 2023-01-10 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
321 ae7c1b78 2023-01-10 stsp size_t datalen;
322 ae7c1b78 2023-01-10 stsp char *packfile_path = NULL, *packidx_path = NULL;
324 ae7c1b78 2023-01-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
325 ae7c1b78 2023-01-10 stsp if (datalen != sizeof(inst))
326 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
327 ae7c1b78 2023-01-10 stsp memcpy(&inst, imsg->data, sizeof(inst));
329 ae7c1b78 2023-01-10 stsp if (client->packfile_path == NULL)
330 ae7c1b78 2023-01-10 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
331 ae7c1b78 2023-01-10 stsp "client has no pack file");
332 ae7c1b78 2023-01-10 stsp if (client->packidx_path == NULL)
333 ae7c1b78 2023-01-10 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
334 ae7c1b78 2023-01-10 stsp "client has no pack file index");
336 ae7c1b78 2023-01-10 stsp if (got_sha1_digest_to_str(inst.pack_sha1, hex, sizeof(hex)) == NULL)
337 ae7c1b78 2023-01-10 stsp return got_error_msg(GOT_ERR_NO_SPACE,
338 ae7c1b78 2023-01-10 stsp "could not convert pack file SHA1 to hex");
340 ae7c1b78 2023-01-10 stsp if (asprintf(&packfile_path, "/%s/%s/pack-%s.pack",
341 ae7c1b78 2023-01-10 stsp repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
342 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("asprintf");
346 ae7c1b78 2023-01-10 stsp if (asprintf(&packidx_path, "/%s/%s/pack-%s.idx",
347 ae7c1b78 2023-01-10 stsp repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
348 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("asprintf");
352 ae7c1b78 2023-01-10 stsp if (rename(client->packfile_path, packfile_path) == -1) {
353 ae7c1b78 2023-01-10 stsp err = got_error_from_errno3("rename", client->packfile_path,
354 ae7c1b78 2023-01-10 stsp packfile_path);
358 ae7c1b78 2023-01-10 stsp free(client->packfile_path);
359 ae7c1b78 2023-01-10 stsp client->packfile_path = NULL;
361 ae7c1b78 2023-01-10 stsp if (rename(client->packidx_path, packidx_path) == -1) {
362 ae7c1b78 2023-01-10 stsp err = got_error_from_errno3("rename", client->packidx_path,
363 ae7c1b78 2023-01-10 stsp packidx_path);
367 ae7c1b78 2023-01-10 stsp free(client->packidx_path);
368 ae7c1b78 2023-01-10 stsp client->packidx_path = NULL;
370 ae7c1b78 2023-01-10 stsp free(packfile_path);
371 ae7c1b78 2023-01-10 stsp free(packidx_path);
372 ae7c1b78 2023-01-10 stsp return err;
375 ae7c1b78 2023-01-10 stsp static const struct got_error *
376 ae7c1b78 2023-01-10 stsp begin_ref_updates(struct gotd_session_client *client, struct imsg *imsg)
378 ae7c1b78 2023-01-10 stsp struct gotd_imsg_ref_updates_start istart;
379 ae7c1b78 2023-01-10 stsp size_t datalen;
381 ae7c1b78 2023-01-10 stsp if (client->nref_updates != -1)
382 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
384 ae7c1b78 2023-01-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
385 ae7c1b78 2023-01-10 stsp if (datalen != sizeof(istart))
386 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
387 ae7c1b78 2023-01-10 stsp memcpy(&istart, imsg->data, sizeof(istart));
389 ae7c1b78 2023-01-10 stsp if (istart.nref_updates <= 0)
390 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
392 ae7c1b78 2023-01-10 stsp client->nref_updates = istart.nref_updates;
393 ae7c1b78 2023-01-10 stsp return NULL;
396 ae7c1b78 2023-01-10 stsp static const struct got_error *
397 ae7c1b78 2023-01-10 stsp update_ref(struct gotd_session_client *client, const char *repo_path,
398 ae7c1b78 2023-01-10 stsp struct imsg *imsg)
400 ae7c1b78 2023-01-10 stsp const struct got_error *err = NULL;
401 ae7c1b78 2023-01-10 stsp struct got_repository *repo = NULL;
402 ae7c1b78 2023-01-10 stsp struct got_reference *ref = NULL;
403 ae7c1b78 2023-01-10 stsp struct gotd_imsg_ref_update iref;
404 ae7c1b78 2023-01-10 stsp struct got_object_id old_id, new_id;
405 ae7c1b78 2023-01-10 stsp struct got_object_id *id = NULL;
406 ae7c1b78 2023-01-10 stsp struct got_object *obj = NULL;
407 ae7c1b78 2023-01-10 stsp char *refname = NULL;
408 ae7c1b78 2023-01-10 stsp size_t datalen;
409 ae7c1b78 2023-01-10 stsp int locked = 0;
411 ae7c1b78 2023-01-10 stsp log_debug("update-ref from uid %d", client->euid);
413 ae7c1b78 2023-01-10 stsp if (client->nref_updates <= 0)
414 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
416 ae7c1b78 2023-01-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
417 ae7c1b78 2023-01-10 stsp if (datalen < sizeof(iref))
418 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
419 ae7c1b78 2023-01-10 stsp memcpy(&iref, imsg->data, sizeof(iref));
420 ae7c1b78 2023-01-10 stsp if (datalen != sizeof(iref) + iref.name_len)
421 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
422 ae7c1b78 2023-01-10 stsp refname = malloc(iref.name_len + 1);
423 ae7c1b78 2023-01-10 stsp if (refname == NULL)
424 ae7c1b78 2023-01-10 stsp return got_error_from_errno("malloc");
425 ae7c1b78 2023-01-10 stsp memcpy(refname, imsg->data + sizeof(iref), iref.name_len);
426 ae7c1b78 2023-01-10 stsp refname[iref.name_len] = '\0';
428 ae7c1b78 2023-01-10 stsp log_debug("updating ref %s for uid %d", refname, client->euid);
430 ae7c1b78 2023-01-10 stsp err = got_repo_open(&repo, repo_path, NULL, NULL);
434 ae7c1b78 2023-01-10 stsp memcpy(old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
435 ae7c1b78 2023-01-10 stsp memcpy(new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
436 ae7c1b78 2023-01-10 stsp err = got_object_open(&obj, repo, &new_id);
440 ae7c1b78 2023-01-10 stsp if (iref.ref_is_new) {
441 ae7c1b78 2023-01-10 stsp err = got_ref_open(&ref, repo, refname, 0);
443 ae7c1b78 2023-01-10 stsp if (err->code != GOT_ERR_NOT_REF)
445 ae7c1b78 2023-01-10 stsp err = got_ref_alloc(&ref, refname, &new_id);
448 ae7c1b78 2023-01-10 stsp err = got_ref_write(ref, repo); /* will lock/unlock */
452 ae7c1b78 2023-01-10 stsp err = got_error_fmt(GOT_ERR_REF_BUSY,
453 ae7c1b78 2023-01-10 stsp "%s has been created by someone else "
454 ae7c1b78 2023-01-10 stsp "while transaction was in progress",
455 ae7c1b78 2023-01-10 stsp got_ref_get_name(ref));
459 ae7c1b78 2023-01-10 stsp err = got_ref_open(&ref, repo, refname, 1 /* lock */);
462 ae7c1b78 2023-01-10 stsp locked = 1;
464 ae7c1b78 2023-01-10 stsp err = got_ref_resolve(&id, repo, ref);
468 ae7c1b78 2023-01-10 stsp if (got_object_id_cmp(id, &old_id) != 0) {
469 ae7c1b78 2023-01-10 stsp err = got_error_fmt(GOT_ERR_REF_BUSY,
470 ae7c1b78 2023-01-10 stsp "%s has been modified by someone else "
471 ae7c1b78 2023-01-10 stsp "while transaction was in progress",
472 ae7c1b78 2023-01-10 stsp got_ref_get_name(ref));
476 ae7c1b78 2023-01-10 stsp err = got_ref_change_ref(ref, &new_id);
480 ae7c1b78 2023-01-10 stsp err = got_ref_write(ref, repo);
489 ae7c1b78 2023-01-10 stsp if (err->code == GOT_ERR_LOCKFILE_TIMEOUT) {
490 ae7c1b78 2023-01-10 stsp err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
491 ae7c1b78 2023-01-10 stsp "could not acquire exclusive file lock for %s",
494 ae7c1b78 2023-01-10 stsp send_ref_update_ng(client, &iref, refname, err->msg);
496 ae7c1b78 2023-01-10 stsp send_ref_update_ok(client, &iref, refname);
498 ae7c1b78 2023-01-10 stsp if (client->nref_updates > 0) {
499 ae7c1b78 2023-01-10 stsp client->nref_updates--;
500 ae7c1b78 2023-01-10 stsp if (client->nref_updates == 0)
501 ae7c1b78 2023-01-10 stsp send_refs_updated(client);
504 ae7c1b78 2023-01-10 stsp if (locked) {
505 ae7c1b78 2023-01-10 stsp const struct got_error *unlock_err;
506 ae7c1b78 2023-01-10 stsp unlock_err = got_ref_unlock(ref);
507 ae7c1b78 2023-01-10 stsp if (unlock_err && err == NULL)
508 ae7c1b78 2023-01-10 stsp err = unlock_err;
511 ae7c1b78 2023-01-10 stsp got_ref_close(ref);
513 ae7c1b78 2023-01-10 stsp got_object_close(obj);
515 ae7c1b78 2023-01-10 stsp got_repo_close(repo);
516 ae7c1b78 2023-01-10 stsp free(refname);
518 ae7c1b78 2023-01-10 stsp return err;
521 ae7c1b78 2023-01-10 stsp static void
522 ae7c1b78 2023-01-10 stsp session_dispatch_repo_child(int fd, short event, void *arg)
524 ae7c1b78 2023-01-10 stsp struct gotd_imsgev *iev = arg;
525 ae7c1b78 2023-01-10 stsp struct imsgbuf *ibuf = &iev->ibuf;
526 ae7c1b78 2023-01-10 stsp struct gotd_session_client *client = &gotd_session_client;
528 ae7c1b78 2023-01-10 stsp int shut = 0;
529 ae7c1b78 2023-01-10 stsp struct imsg imsg;
531 ae7c1b78 2023-01-10 stsp if (event & EV_READ) {
532 ae7c1b78 2023-01-10 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
533 ae7c1b78 2023-01-10 stsp fatal("imsg_read error");
534 ae7c1b78 2023-01-10 stsp if (n == 0) {
535 ae7c1b78 2023-01-10 stsp /* Connection closed. */
541 ae7c1b78 2023-01-10 stsp if (event & EV_WRITE) {
542 ae7c1b78 2023-01-10 stsp n = msgbuf_write(&ibuf->w);
543 ae7c1b78 2023-01-10 stsp if (n == -1 && errno != EAGAIN)
544 ae7c1b78 2023-01-10 stsp fatal("msgbuf_write");
545 ae7c1b78 2023-01-10 stsp if (n == 0) {
546 ae7c1b78 2023-01-10 stsp /* Connection closed. */
553 ae7c1b78 2023-01-10 stsp const struct got_error *err = NULL;
554 ae7c1b78 2023-01-10 stsp uint32_t client_id = 0;
555 ae7c1b78 2023-01-10 stsp int do_disconnect = 0;
556 ae7c1b78 2023-01-10 stsp int do_ref_updates = 0, do_ref_update = 0;
557 ae7c1b78 2023-01-10 stsp int do_packfile_install = 0;
559 ae7c1b78 2023-01-10 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
560 ae7c1b78 2023-01-10 stsp fatal("%s: imsg_get error", __func__);
561 ae7c1b78 2023-01-10 stsp if (n == 0) /* No more messages. */
564 ae7c1b78 2023-01-10 stsp switch (imsg.hdr.type) {
565 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_ERROR:
566 ae7c1b78 2023-01-10 stsp do_disconnect = 1;
567 ae7c1b78 2023-01-10 stsp err = gotd_imsg_recv_error(&client_id, &imsg);
569 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_PACKFILE_DONE:
570 ae7c1b78 2023-01-10 stsp do_disconnect = 1;
571 ae7c1b78 2023-01-10 stsp err = recv_packfile_done(&client_id, &imsg);
573 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_PACKFILE_INSTALL:
574 ae7c1b78 2023-01-10 stsp err = recv_packfile_install(&client_id, &imsg);
575 ae7c1b78 2023-01-10 stsp if (err == NULL)
576 ae7c1b78 2023-01-10 stsp do_packfile_install = 1;
578 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_REF_UPDATES_START:
579 ae7c1b78 2023-01-10 stsp err = recv_ref_updates_start(&client_id, &imsg);
580 ae7c1b78 2023-01-10 stsp if (err == NULL)
581 ae7c1b78 2023-01-10 stsp do_ref_updates = 1;
583 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_REF_UPDATE:
584 ae7c1b78 2023-01-10 stsp err = recv_ref_update(&client_id, &imsg);
585 ae7c1b78 2023-01-10 stsp if (err == NULL)
586 ae7c1b78 2023-01-10 stsp do_ref_update = 1;
589 ae7c1b78 2023-01-10 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
593 ae7c1b78 2023-01-10 stsp if (do_disconnect) {
595 ae7c1b78 2023-01-10 stsp disconnect_on_error(client, err);
597 ae7c1b78 2023-01-10 stsp disconnect(client);
599 ae7c1b78 2023-01-10 stsp if (do_packfile_install)
600 ae7c1b78 2023-01-10 stsp err = install_pack(client,
601 ae7c1b78 2023-01-10 stsp gotd_session.repo->path, &imsg);
602 ae7c1b78 2023-01-10 stsp else if (do_ref_updates)
603 ae7c1b78 2023-01-10 stsp err = begin_ref_updates(client, &imsg);
604 ae7c1b78 2023-01-10 stsp else if (do_ref_update)
605 ae7c1b78 2023-01-10 stsp err = update_ref(client,
606 ae7c1b78 2023-01-10 stsp gotd_session.repo->path, &imsg);
608 ae7c1b78 2023-01-10 stsp log_warnx("uid %d: %s", client->euid, err->msg);
610 ae7c1b78 2023-01-10 stsp imsg_free(&imsg);
613 ae7c1b78 2023-01-10 stsp if (!shut) {
614 ae7c1b78 2023-01-10 stsp gotd_imsg_event_add(iev);
616 ae7c1b78 2023-01-10 stsp /* This pipe is dead. Remove its event handler */
617 ae7c1b78 2023-01-10 stsp event_del(&iev->ev);
618 ae7c1b78 2023-01-10 stsp event_loopexit(NULL);
622 ae7c1b78 2023-01-10 stsp static const struct got_error *
623 ae7c1b78 2023-01-10 stsp recv_capabilities(struct gotd_session_client *client, struct imsg *imsg)
625 ae7c1b78 2023-01-10 stsp struct gotd_imsg_capabilities icapas;
626 ae7c1b78 2023-01-10 stsp size_t datalen;
628 ae7c1b78 2023-01-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
629 ae7c1b78 2023-01-10 stsp if (datalen != sizeof(icapas))
630 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
631 ae7c1b78 2023-01-10 stsp memcpy(&icapas, imsg->data, sizeof(icapas));
633 ae7c1b78 2023-01-10 stsp client->ncapa_alloc = icapas.ncapabilities;
634 ae7c1b78 2023-01-10 stsp client->capabilities = calloc(client->ncapa_alloc,
635 ae7c1b78 2023-01-10 stsp sizeof(*client->capabilities));
636 ae7c1b78 2023-01-10 stsp if (client->capabilities == NULL) {
637 ae7c1b78 2023-01-10 stsp client->ncapa_alloc = 0;
638 ae7c1b78 2023-01-10 stsp return got_error_from_errno("calloc");
641 ae7c1b78 2023-01-10 stsp log_debug("expecting %zu capabilities from uid %d",
642 ae7c1b78 2023-01-10 stsp client->ncapa_alloc, client->euid);
643 ae7c1b78 2023-01-10 stsp return NULL;
646 ae7c1b78 2023-01-10 stsp static const struct got_error *
647 ae7c1b78 2023-01-10 stsp recv_capability(struct gotd_session_client *client, struct imsg *imsg)
649 ae7c1b78 2023-01-10 stsp struct gotd_imsg_capability icapa;
650 ae7c1b78 2023-01-10 stsp struct gotd_client_capability *capa;
651 ae7c1b78 2023-01-10 stsp size_t datalen;
652 ae7c1b78 2023-01-10 stsp char *key, *value = NULL;
654 ae7c1b78 2023-01-10 stsp if (client->capabilities == NULL ||
655 ae7c1b78 2023-01-10 stsp client->ncapabilities >= client->ncapa_alloc) {
656 ae7c1b78 2023-01-10 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
657 ae7c1b78 2023-01-10 stsp "unexpected capability received");
660 ae7c1b78 2023-01-10 stsp memset(&icapa, 0, sizeof(icapa));
662 ae7c1b78 2023-01-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
663 ae7c1b78 2023-01-10 stsp if (datalen < sizeof(icapa))
664 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
665 ae7c1b78 2023-01-10 stsp memcpy(&icapa, imsg->data, sizeof(icapa));
667 ae7c1b78 2023-01-10 stsp if (datalen != sizeof(icapa) + icapa.key_len + icapa.value_len)
668 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
670 ae7c1b78 2023-01-10 stsp key = malloc(icapa.key_len + 1);
671 ae7c1b78 2023-01-10 stsp if (key == NULL)
672 ae7c1b78 2023-01-10 stsp return got_error_from_errno("malloc");
673 ae7c1b78 2023-01-10 stsp if (icapa.value_len > 0) {
674 ae7c1b78 2023-01-10 stsp value = malloc(icapa.value_len + 1);
675 ae7c1b78 2023-01-10 stsp if (value == NULL) {
677 ae7c1b78 2023-01-10 stsp return got_error_from_errno("malloc");
681 ae7c1b78 2023-01-10 stsp memcpy(key, imsg->data + sizeof(icapa), icapa.key_len);
682 ae7c1b78 2023-01-10 stsp key[icapa.key_len] = '\0';
683 ae7c1b78 2023-01-10 stsp if (value) {
684 ae7c1b78 2023-01-10 stsp memcpy(value, imsg->data + sizeof(icapa) + icapa.key_len,
685 ae7c1b78 2023-01-10 stsp icapa.value_len);
686 ae7c1b78 2023-01-10 stsp value[icapa.value_len] = '\0';
689 ae7c1b78 2023-01-10 stsp capa = &client->capabilities[client->ncapabilities++];
690 ae7c1b78 2023-01-10 stsp capa->key = key;
691 ae7c1b78 2023-01-10 stsp capa->value = value;
694 ae7c1b78 2023-01-10 stsp log_debug("uid %d: capability %s=%s", client->euid, key, value);
696 ae7c1b78 2023-01-10 stsp log_debug("uid %d: capability %s", client->euid, key);
698 ae7c1b78 2023-01-10 stsp return NULL;
701 ae7c1b78 2023-01-10 stsp static const struct got_error *
702 ae7c1b78 2023-01-10 stsp ensure_client_is_reading(struct gotd_session_client *client)
704 ae7c1b78 2023-01-10 stsp if (client->is_writing) {
705 ae7c1b78 2023-01-10 stsp return got_error_fmt(GOT_ERR_BAD_PACKET,
706 ae7c1b78 2023-01-10 stsp "uid %d made a read-request but is not reading from "
707 ae7c1b78 2023-01-10 stsp "a repository", client->euid);
710 ae7c1b78 2023-01-10 stsp return NULL;
713 ae7c1b78 2023-01-10 stsp static const struct got_error *
714 ae7c1b78 2023-01-10 stsp ensure_client_is_writing(struct gotd_session_client *client)
716 ae7c1b78 2023-01-10 stsp if (!client->is_writing) {
717 ae7c1b78 2023-01-10 stsp return got_error_fmt(GOT_ERR_BAD_PACKET,
718 ae7c1b78 2023-01-10 stsp "uid %d made a write-request but is not writing to "
719 ae7c1b78 2023-01-10 stsp "a repository", client->euid);
722 ae7c1b78 2023-01-10 stsp return NULL;
725 ae7c1b78 2023-01-10 stsp static const struct got_error *
726 ae7c1b78 2023-01-10 stsp forward_want(struct gotd_session_client *client, struct imsg *imsg)
728 ae7c1b78 2023-01-10 stsp struct gotd_imsg_want ireq;
729 ae7c1b78 2023-01-10 stsp struct gotd_imsg_want iwant;
730 ae7c1b78 2023-01-10 stsp size_t datalen;
732 ae7c1b78 2023-01-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
733 ae7c1b78 2023-01-10 stsp if (datalen != sizeof(ireq))
734 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
736 ae7c1b78 2023-01-10 stsp memcpy(&ireq, imsg->data, datalen);
738 ae7c1b78 2023-01-10 stsp memset(&iwant, 0, sizeof(iwant));
739 ae7c1b78 2023-01-10 stsp memcpy(iwant.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
740 ae7c1b78 2023-01-10 stsp iwant.client_id = client->id;
742 ae7c1b78 2023-01-10 stsp if (gotd_imsg_compose_event(&client->repo_child_iev, GOTD_IMSG_WANT,
743 ae7c1b78 2023-01-10 stsp PROC_SESSION, -1, &iwant, sizeof(iwant)) == -1)
744 ae7c1b78 2023-01-10 stsp return got_error_from_errno("imsg compose WANT");
746 ae7c1b78 2023-01-10 stsp return NULL;
749 ae7c1b78 2023-01-10 stsp static const struct got_error *
750 ae7c1b78 2023-01-10 stsp forward_ref_update(struct gotd_session_client *client, struct imsg *imsg)
752 ae7c1b78 2023-01-10 stsp const struct got_error *err = NULL;
753 ae7c1b78 2023-01-10 stsp struct gotd_imsg_ref_update ireq;
754 ae7c1b78 2023-01-10 stsp struct gotd_imsg_ref_update *iref = NULL;
755 ae7c1b78 2023-01-10 stsp size_t datalen;
757 ae7c1b78 2023-01-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
758 ae7c1b78 2023-01-10 stsp if (datalen < sizeof(ireq))
759 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
760 ae7c1b78 2023-01-10 stsp memcpy(&ireq, imsg->data, sizeof(ireq));
761 ae7c1b78 2023-01-10 stsp if (datalen != sizeof(ireq) + ireq.name_len)
762 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
764 ae7c1b78 2023-01-10 stsp iref = malloc(datalen);
765 ae7c1b78 2023-01-10 stsp if (iref == NULL)
766 ae7c1b78 2023-01-10 stsp return got_error_from_errno("malloc");
767 ae7c1b78 2023-01-10 stsp memcpy(iref, imsg->data, datalen);
769 ae7c1b78 2023-01-10 stsp iref->client_id = client->id;
770 ae7c1b78 2023-01-10 stsp if (gotd_imsg_compose_event(&client->repo_child_iev,
771 ae7c1b78 2023-01-10 stsp GOTD_IMSG_REF_UPDATE, PROC_SESSION, -1, iref, datalen) == -1)
772 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("imsg compose REF_UPDATE");
773 ae7c1b78 2023-01-10 stsp free(iref);
774 ae7c1b78 2023-01-10 stsp return err;
777 ae7c1b78 2023-01-10 stsp static const struct got_error *
778 ae7c1b78 2023-01-10 stsp forward_have(struct gotd_session_client *client, struct imsg *imsg)
780 ae7c1b78 2023-01-10 stsp struct gotd_imsg_have ireq;
781 ae7c1b78 2023-01-10 stsp struct gotd_imsg_have ihave;
782 ae7c1b78 2023-01-10 stsp size_t datalen;
784 ae7c1b78 2023-01-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
785 ae7c1b78 2023-01-10 stsp if (datalen != sizeof(ireq))
786 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
788 ae7c1b78 2023-01-10 stsp memcpy(&ireq, imsg->data, datalen);
790 ae7c1b78 2023-01-10 stsp memset(&ihave, 0, sizeof(ihave));
791 ae7c1b78 2023-01-10 stsp memcpy(ihave.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
792 ae7c1b78 2023-01-10 stsp ihave.client_id = client->id;
794 ae7c1b78 2023-01-10 stsp if (gotd_imsg_compose_event(&client->repo_child_iev, GOTD_IMSG_HAVE,
795 ae7c1b78 2023-01-10 stsp PROC_SESSION, -1, &ihave, sizeof(ihave)) == -1)
796 ae7c1b78 2023-01-10 stsp return got_error_from_errno("imsg compose HAVE");
798 ae7c1b78 2023-01-10 stsp return NULL;
802 ae7c1b78 2023-01-10 stsp client_has_capability(struct gotd_session_client *client, const char *capastr)
804 ae7c1b78 2023-01-10 stsp struct gotd_client_capability *capa;
807 ae7c1b78 2023-01-10 stsp if (client->ncapabilities == 0)
810 ae7c1b78 2023-01-10 stsp for (i = 0; i < client->ncapabilities; i++) {
811 ae7c1b78 2023-01-10 stsp capa = &client->capabilities[i];
812 ae7c1b78 2023-01-10 stsp if (strcmp(capa->key, capastr) == 0)
819 ae7c1b78 2023-01-10 stsp static const struct got_error *
820 ae7c1b78 2023-01-10 stsp recv_packfile(struct gotd_session_client *client)
822 ae7c1b78 2023-01-10 stsp const struct got_error *err = NULL;
823 ae7c1b78 2023-01-10 stsp struct gotd_imsg_recv_packfile ipack;
824 ae7c1b78 2023-01-10 stsp struct gotd_imsg_packfile_pipe ipipe;
825 ae7c1b78 2023-01-10 stsp struct gotd_imsg_packidx_file ifile;
826 ae7c1b78 2023-01-10 stsp char *basepath = NULL, *pack_path = NULL, *idx_path = NULL;
827 ae7c1b78 2023-01-10 stsp int packfd = -1, idxfd = -1;
828 ae7c1b78 2023-01-10 stsp int pipe[2] = { -1, -1 };
830 ae7c1b78 2023-01-10 stsp if (client->packfile_path) {
831 ae7c1b78 2023-01-10 stsp return got_error_fmt(GOT_ERR_PRIVSEP_MSG,
832 ae7c1b78 2023-01-10 stsp "uid %d already has a pack file", client->euid);
835 ae7c1b78 2023-01-10 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
836 ae7c1b78 2023-01-10 stsp return got_error_from_errno("socketpair");
838 ae7c1b78 2023-01-10 stsp memset(&ipipe, 0, sizeof(ipipe));
839 ae7c1b78 2023-01-10 stsp ipipe.client_id = client->id;
841 ae7c1b78 2023-01-10 stsp /* Send pack pipe end 0 to repo child process. */
842 ae7c1b78 2023-01-10 stsp if (gotd_imsg_compose_event(&client->repo_child_iev,
843 ae7c1b78 2023-01-10 stsp GOTD_IMSG_PACKFILE_PIPE, PROC_SESSION, pipe[0],
844 ae7c1b78 2023-01-10 stsp &ipipe, sizeof(ipipe)) == -1) {
845 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("imsg compose PACKFILE_PIPE");
846 ae7c1b78 2023-01-10 stsp pipe[0] = -1;
849 ae7c1b78 2023-01-10 stsp pipe[0] = -1;
851 ae7c1b78 2023-01-10 stsp /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
852 ae7c1b78 2023-01-10 stsp if (gotd_imsg_compose_event(&client->iev,
853 ae7c1b78 2023-01-10 stsp GOTD_IMSG_PACKFILE_PIPE, PROC_SESSION, pipe[1], NULL, 0) == -1)
854 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("imsg compose PACKFILE_PIPE");
855 ae7c1b78 2023-01-10 stsp pipe[1] = -1;
857 ae7c1b78 2023-01-10 stsp if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.pack",
858 ae7c1b78 2023-01-10 stsp got_repo_get_path(gotd_session.repo), GOT_OBJECTS_PACK_DIR,
859 ae7c1b78 2023-01-10 stsp client->euid) == -1) {
860 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("asprintf");
864 ae7c1b78 2023-01-10 stsp err = got_opentemp_named_fd(&pack_path, &packfd, basepath, "");
868 ae7c1b78 2023-01-10 stsp free(basepath);
869 ae7c1b78 2023-01-10 stsp if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.idx",
870 ae7c1b78 2023-01-10 stsp got_repo_get_path(gotd_session.repo), GOT_OBJECTS_PACK_DIR,
871 ae7c1b78 2023-01-10 stsp client->euid) == -1) {
872 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("asprintf");
873 ae7c1b78 2023-01-10 stsp basepath = NULL;
876 ae7c1b78 2023-01-10 stsp err = got_opentemp_named_fd(&idx_path, &idxfd, basepath, "");
880 ae7c1b78 2023-01-10 stsp memset(&ifile, 0, sizeof(ifile));
881 ae7c1b78 2023-01-10 stsp ifile.client_id = client->id;
882 ae7c1b78 2023-01-10 stsp if (gotd_imsg_compose_event(&client->repo_child_iev,
883 ae7c1b78 2023-01-10 stsp GOTD_IMSG_PACKIDX_FILE, PROC_SESSION,
884 ae7c1b78 2023-01-10 stsp idxfd, &ifile, sizeof(ifile)) == -1) {
885 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("imsg compose PACKIDX_FILE");
886 ae7c1b78 2023-01-10 stsp idxfd = -1;
889 ae7c1b78 2023-01-10 stsp idxfd = -1;
891 ae7c1b78 2023-01-10 stsp memset(&ipack, 0, sizeof(ipack));
892 ae7c1b78 2023-01-10 stsp ipack.client_id = client->id;
893 ae7c1b78 2023-01-10 stsp if (client_has_capability(client, GOT_CAPA_REPORT_STATUS))
894 ae7c1b78 2023-01-10 stsp ipack.report_status = 1;
896 ae7c1b78 2023-01-10 stsp if (gotd_imsg_compose_event(&client->repo_child_iev,
897 ae7c1b78 2023-01-10 stsp GOTD_IMSG_RECV_PACKFILE, PROC_SESSION, packfd,
898 ae7c1b78 2023-01-10 stsp &ipack, sizeof(ipack)) == -1) {
899 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("imsg compose RECV_PACKFILE");
900 ae7c1b78 2023-01-10 stsp packfd = -1;
903 ae7c1b78 2023-01-10 stsp packfd = -1;
906 ae7c1b78 2023-01-10 stsp free(basepath);
907 ae7c1b78 2023-01-10 stsp if (pipe[0] != -1 && close(pipe[0]) == -1 && err == NULL)
908 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("close");
909 ae7c1b78 2023-01-10 stsp if (pipe[1] != -1 && close(pipe[1]) == -1 && err == NULL)
910 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("close");
911 ae7c1b78 2023-01-10 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
912 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("close");
913 ae7c1b78 2023-01-10 stsp if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
914 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("close");
916 ae7c1b78 2023-01-10 stsp free(pack_path);
917 ae7c1b78 2023-01-10 stsp free(idx_path);
919 ae7c1b78 2023-01-10 stsp client->packfile_path = pack_path;
920 ae7c1b78 2023-01-10 stsp client->packidx_path = idx_path;
922 ae7c1b78 2023-01-10 stsp return err;
925 ae7c1b78 2023-01-10 stsp static const struct got_error *
926 ae7c1b78 2023-01-10 stsp send_packfile(struct gotd_session_client *client)
928 ae7c1b78 2023-01-10 stsp const struct got_error *err = NULL;
929 ae7c1b78 2023-01-10 stsp struct gotd_imsg_send_packfile ipack;
930 ae7c1b78 2023-01-10 stsp struct gotd_imsg_packfile_pipe ipipe;
931 ae7c1b78 2023-01-10 stsp int pipe[2];
933 ae7c1b78 2023-01-10 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
934 ae7c1b78 2023-01-10 stsp return got_error_from_errno("socketpair");
936 ae7c1b78 2023-01-10 stsp memset(&ipack, 0, sizeof(ipack));
937 ae7c1b78 2023-01-10 stsp memset(&ipipe, 0, sizeof(ipipe));
939 ae7c1b78 2023-01-10 stsp ipack.client_id = client->id;
940 ae7c1b78 2023-01-10 stsp if (client_has_capability(client, GOT_CAPA_SIDE_BAND_64K))
941 ae7c1b78 2023-01-10 stsp ipack.report_progress = 1;
943 ae7c1b78 2023-01-10 stsp client->delta_cache_fd = got_opentempfd();
944 ae7c1b78 2023-01-10 stsp if (client->delta_cache_fd == -1)
945 ae7c1b78 2023-01-10 stsp return got_error_from_errno("got_opentempfd");
947 ae7c1b78 2023-01-10 stsp if (gotd_imsg_compose_event(&client->repo_child_iev,
948 ae7c1b78 2023-01-10 stsp GOTD_IMSG_SEND_PACKFILE, PROC_GOTD, client->delta_cache_fd,
949 ae7c1b78 2023-01-10 stsp &ipack, sizeof(ipack)) == -1) {
950 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("imsg compose SEND_PACKFILE");
951 ae7c1b78 2023-01-10 stsp close(pipe[0]);
952 ae7c1b78 2023-01-10 stsp close(pipe[1]);
953 ae7c1b78 2023-01-10 stsp return err;
956 ae7c1b78 2023-01-10 stsp ipipe.client_id = client->id;
958 ae7c1b78 2023-01-10 stsp /* Send pack pipe end 0 to repo child process. */
959 ae7c1b78 2023-01-10 stsp if (gotd_imsg_compose_event(&client->repo_child_iev,
960 ae7c1b78 2023-01-10 stsp GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD,
961 ae7c1b78 2023-01-10 stsp pipe[0], &ipipe, sizeof(ipipe)) == -1) {
962 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("imsg compose PACKFILE_PIPE");
963 ae7c1b78 2023-01-10 stsp close(pipe[1]);
964 ae7c1b78 2023-01-10 stsp return err;
967 ae7c1b78 2023-01-10 stsp /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
968 ae7c1b78 2023-01-10 stsp if (gotd_imsg_compose_event(&client->iev,
969 ae7c1b78 2023-01-10 stsp GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1], NULL, 0) == -1)
970 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("imsg compose PACKFILE_PIPE");
972 ae7c1b78 2023-01-10 stsp return err;
975 ae7c1b78 2023-01-10 stsp static void
976 ae7c1b78 2023-01-10 stsp session_dispatch_listener(int fd, short events, void *arg)
978 ae7c1b78 2023-01-10 stsp struct gotd_imsgev *iev = arg;
979 ae7c1b78 2023-01-10 stsp struct imsgbuf *ibuf = &iev->ibuf;
980 ae7c1b78 2023-01-10 stsp struct gotd_session_client *client = &gotd_session_client;
981 ae7c1b78 2023-01-10 stsp const struct got_error *err = NULL;
982 ae7c1b78 2023-01-10 stsp struct imsg imsg;
985 ae7c1b78 2023-01-10 stsp if (events & EV_WRITE) {
986 ae7c1b78 2023-01-10 stsp while (ibuf->w.queued) {
987 ae7c1b78 2023-01-10 stsp n = msgbuf_write(&ibuf->w);
988 ae7c1b78 2023-01-10 stsp if (n == -1 && errno == EPIPE) {
990 ae7c1b78 2023-01-10 stsp * The client has closed its socket.
991 ae7c1b78 2023-01-10 stsp * This can happen when Git clients are
992 ae7c1b78 2023-01-10 stsp * done sending pack file data.
994 ae7c1b78 2023-01-10 stsp msgbuf_clear(&ibuf->w);
996 ae7c1b78 2023-01-10 stsp } else if (n == -1 && errno != EAGAIN) {
997 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("imsg_flush");
998 ae7c1b78 2023-01-10 stsp disconnect_on_error(client, err);
1001 ae7c1b78 2023-01-10 stsp if (n == 0) {
1002 ae7c1b78 2023-01-10 stsp /* Connection closed. */
1003 ae7c1b78 2023-01-10 stsp err = got_error(GOT_ERR_EOF);
1004 ae7c1b78 2023-01-10 stsp disconnect_on_error(client, err);
1010 ae7c1b78 2023-01-10 stsp if ((events & EV_READ) == 0)
1013 ae7c1b78 2023-01-10 stsp memset(&imsg, 0, sizeof(imsg));
1015 ae7c1b78 2023-01-10 stsp while (err == NULL) {
1016 ae7c1b78 2023-01-10 stsp err = gotd_imsg_recv(&imsg, ibuf, 0);
1017 ae7c1b78 2023-01-10 stsp if (err) {
1018 ae7c1b78 2023-01-10 stsp if (err->code == GOT_ERR_PRIVSEP_READ)
1019 ae7c1b78 2023-01-10 stsp err = NULL;
1023 ae7c1b78 2023-01-10 stsp evtimer_del(&client->tmo);
1025 ae7c1b78 2023-01-10 stsp switch (imsg.hdr.type) {
1026 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_CAPABILITIES:
1027 ae7c1b78 2023-01-10 stsp if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1028 ae7c1b78 2023-01-10 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1029 ae7c1b78 2023-01-10 stsp "unexpected capabilities received");
1032 ae7c1b78 2023-01-10 stsp log_debug("receiving capabilities from uid %d",
1033 ae7c1b78 2023-01-10 stsp client->euid);
1034 ae7c1b78 2023-01-10 stsp err = recv_capabilities(client, &imsg);
1036 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_CAPABILITY:
1037 ae7c1b78 2023-01-10 stsp if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1038 ae7c1b78 2023-01-10 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1039 ae7c1b78 2023-01-10 stsp "unexpected capability received");
1042 ae7c1b78 2023-01-10 stsp err = recv_capability(client, &imsg);
1043 ae7c1b78 2023-01-10 stsp if (err || client->ncapabilities < client->ncapa_alloc)
1045 ae7c1b78 2023-01-10 stsp if (!client->is_writing) {
1046 ae7c1b78 2023-01-10 stsp client->state = GOTD_STATE_EXPECT_WANT;
1047 ae7c1b78 2023-01-10 stsp log_debug("uid %d: expecting want-lines",
1048 ae7c1b78 2023-01-10 stsp client->euid);
1049 ae7c1b78 2023-01-10 stsp } else if (client->is_writing) {
1050 ae7c1b78 2023-01-10 stsp client->state = GOTD_STATE_EXPECT_REF_UPDATE;
1051 ae7c1b78 2023-01-10 stsp log_debug("uid %d: expecting ref-update-lines",
1052 ae7c1b78 2023-01-10 stsp client->euid);
1054 ae7c1b78 2023-01-10 stsp fatalx("client %d is both reading and writing",
1055 ae7c1b78 2023-01-10 stsp client->euid);
1057 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_WANT:
1058 ae7c1b78 2023-01-10 stsp if (client->state != GOTD_STATE_EXPECT_WANT) {
1059 ae7c1b78 2023-01-10 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1060 ae7c1b78 2023-01-10 stsp "unexpected want-line received");
1063 ae7c1b78 2023-01-10 stsp log_debug("received want-line from uid %d",
1064 ae7c1b78 2023-01-10 stsp client->euid);
1065 ae7c1b78 2023-01-10 stsp err = ensure_client_is_reading(client);
1068 ae7c1b78 2023-01-10 stsp err = forward_want(client, &imsg);
1070 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_REF_UPDATE:
1071 ae7c1b78 2023-01-10 stsp if (client->state != GOTD_STATE_EXPECT_REF_UPDATE) {
1072 ae7c1b78 2023-01-10 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1073 ae7c1b78 2023-01-10 stsp "unexpected ref-update-line received");
1076 ae7c1b78 2023-01-10 stsp log_debug("received ref-update-line from uid %d",
1077 ae7c1b78 2023-01-10 stsp client->euid);
1078 ae7c1b78 2023-01-10 stsp err = ensure_client_is_writing(client);
1081 ae7c1b78 2023-01-10 stsp err = forward_ref_update(client, &imsg);
1084 ae7c1b78 2023-01-10 stsp client->state = GOTD_STATE_EXPECT_MORE_REF_UPDATES;
1086 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_HAVE:
1087 ae7c1b78 2023-01-10 stsp if (client->state != GOTD_STATE_EXPECT_HAVE) {
1088 ae7c1b78 2023-01-10 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1089 ae7c1b78 2023-01-10 stsp "unexpected have-line received");
1092 ae7c1b78 2023-01-10 stsp log_debug("received have-line from uid %d",
1093 ae7c1b78 2023-01-10 stsp client->euid);
1094 ae7c1b78 2023-01-10 stsp err = ensure_client_is_reading(client);
1097 ae7c1b78 2023-01-10 stsp err = forward_have(client, &imsg);
1101 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_FLUSH:
1102 ae7c1b78 2023-01-10 stsp if (client->state == GOTD_STATE_EXPECT_WANT ||
1103 ae7c1b78 2023-01-10 stsp client->state == GOTD_STATE_EXPECT_HAVE) {
1104 ae7c1b78 2023-01-10 stsp err = ensure_client_is_reading(client);
1107 ae7c1b78 2023-01-10 stsp } else if (client->state ==
1108 ae7c1b78 2023-01-10 stsp GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1109 ae7c1b78 2023-01-10 stsp err = ensure_client_is_writing(client);
1113 ae7c1b78 2023-01-10 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1114 ae7c1b78 2023-01-10 stsp "unexpected flush-pkt received");
1117 ae7c1b78 2023-01-10 stsp log_debug("received flush-pkt from uid %d",
1118 ae7c1b78 2023-01-10 stsp client->euid);
1119 ae7c1b78 2023-01-10 stsp if (client->state == GOTD_STATE_EXPECT_WANT) {
1120 ae7c1b78 2023-01-10 stsp client->state = GOTD_STATE_EXPECT_HAVE;
1121 ae7c1b78 2023-01-10 stsp log_debug("uid %d: expecting have-lines",
1122 ae7c1b78 2023-01-10 stsp client->euid);
1123 ae7c1b78 2023-01-10 stsp } else if (client->state == GOTD_STATE_EXPECT_HAVE) {
1124 ae7c1b78 2023-01-10 stsp client->state = GOTD_STATE_EXPECT_DONE;
1125 ae7c1b78 2023-01-10 stsp log_debug("uid %d: expecting 'done'",
1126 ae7c1b78 2023-01-10 stsp client->euid);
1127 ae7c1b78 2023-01-10 stsp } else if (client->state ==
1128 ae7c1b78 2023-01-10 stsp GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1129 ae7c1b78 2023-01-10 stsp client->state = GOTD_STATE_EXPECT_PACKFILE;
1130 ae7c1b78 2023-01-10 stsp log_debug("uid %d: expecting packfile",
1131 ae7c1b78 2023-01-10 stsp client->euid);
1132 ae7c1b78 2023-01-10 stsp err = recv_packfile(client);
1134 ae7c1b78 2023-01-10 stsp /* should not happen, see above */
1135 ae7c1b78 2023-01-10 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1136 ae7c1b78 2023-01-10 stsp "unexpected client state");
1140 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_DONE:
1141 ae7c1b78 2023-01-10 stsp if (client->state != GOTD_STATE_EXPECT_HAVE &&
1142 ae7c1b78 2023-01-10 stsp client->state != GOTD_STATE_EXPECT_DONE) {
1143 ae7c1b78 2023-01-10 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1144 ae7c1b78 2023-01-10 stsp "unexpected flush-pkt received");
1147 ae7c1b78 2023-01-10 stsp log_debug("received 'done' from uid %d", client->euid);
1148 ae7c1b78 2023-01-10 stsp err = ensure_client_is_reading(client);
1151 ae7c1b78 2023-01-10 stsp client->state = GOTD_STATE_DONE;
1152 ae7c1b78 2023-01-10 stsp err = send_packfile(client);
1155 ae7c1b78 2023-01-10 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1156 ae7c1b78 2023-01-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1160 ae7c1b78 2023-01-10 stsp imsg_free(&imsg);
1163 ae7c1b78 2023-01-10 stsp if (err) {
1164 ae7c1b78 2023-01-10 stsp if (err->code != GOT_ERR_EOF ||
1165 ae7c1b78 2023-01-10 stsp client->state != GOTD_STATE_EXPECT_PACKFILE)
1166 ae7c1b78 2023-01-10 stsp disconnect_on_error(client, err);
1168 ae7c1b78 2023-01-10 stsp gotd_imsg_event_add(iev);
1169 ae7c1b78 2023-01-10 stsp evtimer_add(&client->tmo, &gotd_session.request_timeout);
1173 ae7c1b78 2023-01-10 stsp static const struct got_error *
1174 ae7c1b78 2023-01-10 stsp list_refs_request(void)
1176 ae7c1b78 2023-01-10 stsp static const struct got_error *err;
1177 ae7c1b78 2023-01-10 stsp struct gotd_session_client *client = &gotd_session_client;
1178 ae7c1b78 2023-01-10 stsp struct gotd_imsgev *iev = &client->repo_child_iev;
1179 ae7c1b78 2023-01-10 stsp struct gotd_imsg_list_refs_internal ilref;
1182 ae7c1b78 2023-01-10 stsp if (client->state != GOTD_STATE_EXPECT_LIST_REFS)
1183 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1185 ae7c1b78 2023-01-10 stsp memset(&ilref, 0, sizeof(ilref));
1186 ae7c1b78 2023-01-10 stsp ilref.client_id = client->id;
1188 ae7c1b78 2023-01-10 stsp fd = dup(client->fd);
1189 ae7c1b78 2023-01-10 stsp if (fd == -1)
1190 ae7c1b78 2023-01-10 stsp return got_error_from_errno("dup");
1192 ae7c1b78 2023-01-10 stsp if (gotd_imsg_compose_event(iev, GOTD_IMSG_LIST_REFS_INTERNAL,
1193 ae7c1b78 2023-01-10 stsp PROC_SESSION, fd, &ilref, sizeof(ilref)) == -1) {
1194 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("imsg compose LIST_REFS_INTERNAL");
1195 ae7c1b78 2023-01-10 stsp close(fd);
1196 ae7c1b78 2023-01-10 stsp return err;
1199 ae7c1b78 2023-01-10 stsp client->state = GOTD_STATE_EXPECT_CAPABILITIES;
1200 ae7c1b78 2023-01-10 stsp log_debug("uid %d: expecting capabilities", client->euid);
1201 ae7c1b78 2023-01-10 stsp return NULL;
1204 ae7c1b78 2023-01-10 stsp static const struct got_error *
1205 ae7c1b78 2023-01-10 stsp recv_connect(struct imsg *imsg)
1207 ae7c1b78 2023-01-10 stsp struct gotd_session_client *client = &gotd_session_client;
1208 ae7c1b78 2023-01-10 stsp struct gotd_imsg_connect iconnect;
1209 ae7c1b78 2023-01-10 stsp size_t datalen;
1211 ae7c1b78 2023-01-10 stsp if (client->state != GOTD_STATE_EXPECT_LIST_REFS)
1212 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1214 ae7c1b78 2023-01-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1215 ae7c1b78 2023-01-10 stsp if (datalen != sizeof(iconnect))
1216 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1217 ae7c1b78 2023-01-10 stsp memcpy(&iconnect, imsg->data, sizeof(iconnect));
1219 ae7c1b78 2023-01-10 stsp if (imsg->fd == -1)
1220 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
1222 ae7c1b78 2023-01-10 stsp client->fd = imsg->fd;
1223 ae7c1b78 2023-01-10 stsp client->euid = iconnect.euid;
1224 ae7c1b78 2023-01-10 stsp client->egid = iconnect.egid;
1226 ae7c1b78 2023-01-10 stsp imsg_init(&client->iev.ibuf, client->fd);
1227 ae7c1b78 2023-01-10 stsp client->iev.handler = session_dispatch_listener;
1228 ae7c1b78 2023-01-10 stsp client->iev.events = EV_READ;
1229 ae7c1b78 2023-01-10 stsp client->iev.handler_arg = NULL;
1230 ae7c1b78 2023-01-10 stsp event_set(&client->iev.ev, client->iev.ibuf.fd, EV_READ,
1231 ae7c1b78 2023-01-10 stsp session_dispatch_listener, &client->iev);
1232 ae7c1b78 2023-01-10 stsp gotd_imsg_event_add(&client->iev);
1233 ae7c1b78 2023-01-10 stsp evtimer_set(&client->tmo, gotd_request_timeout, client);
1235 ae7c1b78 2023-01-10 stsp return NULL;
1238 ae7c1b78 2023-01-10 stsp static const struct got_error *
1239 ae7c1b78 2023-01-10 stsp recv_repo_child(struct imsg *imsg)
1241 ae7c1b78 2023-01-10 stsp struct gotd_imsg_connect_repo_child ichild;
1242 ae7c1b78 2023-01-10 stsp struct gotd_session_client *client = &gotd_session_client;
1243 ae7c1b78 2023-01-10 stsp size_t datalen;
1245 ae7c1b78 2023-01-10 stsp if (client->state != GOTD_STATE_EXPECT_LIST_REFS)
1246 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1248 ae7c1b78 2023-01-10 stsp /* We should already have received a pipe to the listener. */
1249 ae7c1b78 2023-01-10 stsp if (client->fd == -1)
1250 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1252 ae7c1b78 2023-01-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1253 ae7c1b78 2023-01-10 stsp if (datalen != sizeof(ichild))
1254 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1256 ae7c1b78 2023-01-10 stsp memcpy(&ichild, imsg->data, sizeof(ichild));
1258 ae7c1b78 2023-01-10 stsp client->id = ichild.client_id;
1259 ae7c1b78 2023-01-10 stsp if (ichild.proc_id == PROC_REPO_WRITE)
1260 ae7c1b78 2023-01-10 stsp client->is_writing = 1;
1261 ae7c1b78 2023-01-10 stsp else if (ichild.proc_id == PROC_REPO_READ)
1262 ae7c1b78 2023-01-10 stsp client->is_writing = 0;
1264 ae7c1b78 2023-01-10 stsp return got_error_msg(GOT_ERR_PRIVSEP_MSG,
1265 ae7c1b78 2023-01-10 stsp "bad child process type");
1267 ae7c1b78 2023-01-10 stsp if (imsg->fd == -1)
1268 ae7c1b78 2023-01-10 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
1270 ae7c1b78 2023-01-10 stsp imsg_init(&client->repo_child_iev.ibuf, imsg->fd);
1271 ae7c1b78 2023-01-10 stsp client->repo_child_iev.handler = session_dispatch_repo_child;
1272 ae7c1b78 2023-01-10 stsp client->repo_child_iev.events = EV_READ;
1273 ae7c1b78 2023-01-10 stsp client->repo_child_iev.handler_arg = NULL;
1274 ae7c1b78 2023-01-10 stsp event_set(&client->repo_child_iev.ev, client->repo_child_iev.ibuf.fd,
1275 ae7c1b78 2023-01-10 stsp EV_READ, session_dispatch_repo_child, &client->repo_child_iev);
1276 ae7c1b78 2023-01-10 stsp gotd_imsg_event_add(&client->repo_child_iev);
1278 ae7c1b78 2023-01-10 stsp /* The "recvfd" pledge promise is no longer needed. */
1279 ae7c1b78 2023-01-10 stsp if (pledge("stdio rpath wpath cpath sendfd fattr flock", NULL) == -1)
1280 ae7c1b78 2023-01-10 stsp fatal("pledge");
1282 ae7c1b78 2023-01-10 stsp return NULL;
1285 ae7c1b78 2023-01-10 stsp static void
1286 ae7c1b78 2023-01-10 stsp session_dispatch(int fd, short event, void *arg)
1288 ae7c1b78 2023-01-10 stsp struct gotd_imsgev *iev = arg;
1289 ae7c1b78 2023-01-10 stsp struct imsgbuf *ibuf = &iev->ibuf;
1290 ae7c1b78 2023-01-10 stsp struct gotd_session_client *client = &gotd_session_client;
1291 ae7c1b78 2023-01-10 stsp ssize_t n;
1292 ae7c1b78 2023-01-10 stsp int shut = 0;
1293 ae7c1b78 2023-01-10 stsp struct imsg imsg;
1295 ae7c1b78 2023-01-10 stsp if (event & EV_READ) {
1296 ae7c1b78 2023-01-10 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1297 ae7c1b78 2023-01-10 stsp fatal("imsg_read error");
1298 ae7c1b78 2023-01-10 stsp if (n == 0) {
1299 ae7c1b78 2023-01-10 stsp /* Connection closed. */
1301 ae7c1b78 2023-01-10 stsp goto done;
1305 ae7c1b78 2023-01-10 stsp if (event & EV_WRITE) {
1306 ae7c1b78 2023-01-10 stsp n = msgbuf_write(&ibuf->w);
1307 ae7c1b78 2023-01-10 stsp if (n == -1 && errno != EAGAIN)
1308 ae7c1b78 2023-01-10 stsp fatal("msgbuf_write");
1309 ae7c1b78 2023-01-10 stsp if (n == 0) {
1310 ae7c1b78 2023-01-10 stsp /* Connection closed. */
1312 ae7c1b78 2023-01-10 stsp goto done;
1316 ae7c1b78 2023-01-10 stsp for (;;) {
1317 ae7c1b78 2023-01-10 stsp const struct got_error *err = NULL;
1318 ae7c1b78 2023-01-10 stsp uint32_t client_id = 0;
1319 ae7c1b78 2023-01-10 stsp int do_disconnect = 0, do_list_refs = 0;
1321 ae7c1b78 2023-01-10 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
1322 ae7c1b78 2023-01-10 stsp fatal("%s: imsg_get error", __func__);
1323 ae7c1b78 2023-01-10 stsp if (n == 0) /* No more messages. */
1326 ae7c1b78 2023-01-10 stsp switch (imsg.hdr.type) {
1327 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_ERROR:
1328 ae7c1b78 2023-01-10 stsp do_disconnect = 1;
1329 ae7c1b78 2023-01-10 stsp err = gotd_imsg_recv_error(&client_id, &imsg);
1331 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_CONNECT:
1332 ae7c1b78 2023-01-10 stsp err = recv_connect(&imsg);
1334 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_DISCONNECT:
1335 ae7c1b78 2023-01-10 stsp do_disconnect = 1;
1337 ae7c1b78 2023-01-10 stsp case GOTD_IMSG_CONNECT_REPO_CHILD:
1338 ae7c1b78 2023-01-10 stsp err = recv_repo_child(&imsg);
1341 ae7c1b78 2023-01-10 stsp do_list_refs = 1;
1344 ae7c1b78 2023-01-10 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1347 ae7c1b78 2023-01-10 stsp imsg_free(&imsg);
1349 ae7c1b78 2023-01-10 stsp if (do_disconnect) {
1351 ae7c1b78 2023-01-10 stsp disconnect_on_error(client, err);
1353 ae7c1b78 2023-01-10 stsp disconnect(client);
1354 ae7c1b78 2023-01-10 stsp } else if (do_list_refs)
1355 ae7c1b78 2023-01-10 stsp err = list_refs_request();
1358 ae7c1b78 2023-01-10 stsp log_warnx("uid %d: %s", client->euid, err->msg);
1361 ae7c1b78 2023-01-10 stsp if (!shut) {
1362 ae7c1b78 2023-01-10 stsp gotd_imsg_event_add(iev);
1364 ae7c1b78 2023-01-10 stsp /* This pipe is dead. Remove its event handler */
1365 ae7c1b78 2023-01-10 stsp event_del(&iev->ev);
1366 ae7c1b78 2023-01-10 stsp event_loopexit(NULL);
1371 ae7c1b78 2023-01-10 stsp session_main(const char *title, const char *repo_path,
1372 ae7c1b78 2023-01-10 stsp int *pack_fds, int *temp_fds, struct timeval *request_timeout)
1374 ae7c1b78 2023-01-10 stsp const struct got_error *err = NULL;
1375 ae7c1b78 2023-01-10 stsp struct event evsigint, evsigterm, evsighup, evsigusr1;
1377 ae7c1b78 2023-01-10 stsp gotd_session.title = title;
1378 ae7c1b78 2023-01-10 stsp gotd_session.pid = getpid();
1379 ae7c1b78 2023-01-10 stsp gotd_session.pack_fds = pack_fds;
1380 ae7c1b78 2023-01-10 stsp gotd_session.temp_fds = temp_fds;
1381 ae7c1b78 2023-01-10 stsp memcpy(&gotd_session.request_timeout, request_timeout,
1382 ae7c1b78 2023-01-10 stsp sizeof(gotd_session.request_timeout));
1384 ae7c1b78 2023-01-10 stsp err = got_repo_open(&gotd_session.repo, repo_path, NULL, pack_fds);
1386 ae7c1b78 2023-01-10 stsp goto done;
1387 ae7c1b78 2023-01-10 stsp if (!got_repo_is_bare(gotd_session.repo)) {
1388 ae7c1b78 2023-01-10 stsp err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1389 ae7c1b78 2023-01-10 stsp "bare git repository required");
1390 ae7c1b78 2023-01-10 stsp goto done;
1393 ae7c1b78 2023-01-10 stsp got_repo_temp_fds_set(gotd_session.repo, temp_fds);
1395 ae7c1b78 2023-01-10 stsp signal_set(&evsigint, SIGINT, gotd_session_sighdlr, NULL);
1396 ae7c1b78 2023-01-10 stsp signal_set(&evsigterm, SIGTERM, gotd_session_sighdlr, NULL);
1397 ae7c1b78 2023-01-10 stsp signal_set(&evsighup, SIGHUP, gotd_session_sighdlr, NULL);
1398 ae7c1b78 2023-01-10 stsp signal_set(&evsigusr1, SIGUSR1, gotd_session_sighdlr, NULL);
1399 ae7c1b78 2023-01-10 stsp signal(SIGPIPE, SIG_IGN);
1401 ae7c1b78 2023-01-10 stsp signal_add(&evsigint, NULL);
1402 ae7c1b78 2023-01-10 stsp signal_add(&evsigterm, NULL);
1403 ae7c1b78 2023-01-10 stsp signal_add(&evsighup, NULL);
1404 ae7c1b78 2023-01-10 stsp signal_add(&evsigusr1, NULL);
1406 ae7c1b78 2023-01-10 stsp gotd_session_client.state = GOTD_STATE_EXPECT_LIST_REFS;
1407 ae7c1b78 2023-01-10 stsp gotd_session_client.fd = -1;
1408 ae7c1b78 2023-01-10 stsp gotd_session_client.nref_updates = -1;
1409 2fc51af0 2023-01-10 stsp gotd_session_client.delta_cache_fd = -1;
1411 ae7c1b78 2023-01-10 stsp imsg_init(&gotd_session.parent_iev.ibuf, GOTD_FILENO_MSG_PIPE);
1412 ae7c1b78 2023-01-10 stsp gotd_session.parent_iev.handler = session_dispatch;
1413 ae7c1b78 2023-01-10 stsp gotd_session.parent_iev.events = EV_READ;
1414 ae7c1b78 2023-01-10 stsp gotd_session.parent_iev.handler_arg = NULL;
1415 ae7c1b78 2023-01-10 stsp event_set(&gotd_session.parent_iev.ev, gotd_session.parent_iev.ibuf.fd,
1416 ae7c1b78 2023-01-10 stsp EV_READ, session_dispatch, &gotd_session.parent_iev);
1417 ae7c1b78 2023-01-10 stsp if (gotd_imsg_compose_event(&gotd_session.parent_iev,
1418 ae7c1b78 2023-01-10 stsp GOTD_IMSG_CLIENT_SESSION_READY, PROC_SESSION, -1, NULL, 0) == -1) {
1419 ae7c1b78 2023-01-10 stsp err = got_error_from_errno("imsg compose CLIENT_SESSION_READY");
1420 ae7c1b78 2023-01-10 stsp goto done;
1423 ae7c1b78 2023-01-10 stsp event_dispatch();
1426 ae7c1b78 2023-01-10 stsp log_warnx("%s: %s", title, err->msg);
1427 ae7c1b78 2023-01-10 stsp gotd_session_shutdown();
1431 ae7c1b78 2023-01-10 stsp gotd_session_shutdown(void)
1433 ae7c1b78 2023-01-10 stsp log_debug("%s: shutting down", gotd_session.title);
1434 ae7c1b78 2023-01-10 stsp if (gotd_session.repo)
1435 ae7c1b78 2023-01-10 stsp got_repo_close(gotd_session.repo);
1436 ae7c1b78 2023-01-10 stsp got_repo_pack_fds_close(gotd_session.pack_fds);
1437 ae7c1b78 2023-01-10 stsp got_repo_temp_fds_close(gotd_session.temp_fds);