Blame


1 62ee7d94 2023-01-10 thomas /*
2 62ee7d94 2023-01-10 thomas * Copyright (c) 2022, 2023 Stefan Sperling <stsp@openbsd.org>
3 62ee7d94 2023-01-10 thomas *
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.
7 62ee7d94 2023-01-10 thomas *
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.
15 62ee7d94 2023-01-10 thomas */
16 fde1a996 2023-08-23 thomas
17 fde1a996 2023-08-23 thomas #include "got_compat.h"
18 62ee7d94 2023-01-10 thomas
19 62ee7d94 2023-01-10 thomas #include <sys/types.h>
20 62ee7d94 2023-01-10 thomas #include <sys/queue.h>
21 62ee7d94 2023-01-10 thomas #include <sys/socket.h>
22 851a5b48 2023-02-03 thomas #include <sys/stat.h>
23 62ee7d94 2023-01-10 thomas #include <sys/uio.h>
24 62ee7d94 2023-01-10 thomas
25 62ee7d94 2023-01-10 thomas #include <errno.h>
26 62ee7d94 2023-01-10 thomas #include <event.h>
27 62ee7d94 2023-01-10 thomas #include <limits.h>
28 62ee7d94 2023-01-10 thomas #include <signal.h>
29 62ee7d94 2023-01-10 thomas #include <stdint.h>
30 62ee7d94 2023-01-10 thomas #include <stdio.h>
31 62ee7d94 2023-01-10 thomas #include <stdlib.h>
32 62ee7d94 2023-01-10 thomas #include <string.h>
33 62ee7d94 2023-01-10 thomas #include <imsg.h>
34 62ee7d94 2023-01-10 thomas #include <unistd.h>
35 621b8de0 2023-08-23 thomas
36 621b8de0 2023-08-23 thomas #include "got_compat.h"
37 62ee7d94 2023-01-10 thomas
38 62ee7d94 2023-01-10 thomas #include "got_error.h"
39 62ee7d94 2023-01-10 thomas #include "got_repository.h"
40 62ee7d94 2023-01-10 thomas #include "got_object.h"
41 62ee7d94 2023-01-10 thomas #include "got_path.h"
42 62ee7d94 2023-01-10 thomas #include "got_reference.h"
43 62ee7d94 2023-01-10 thomas #include "got_opentemp.h"
44 62ee7d94 2023-01-10 thomas
45 be288a59 2023-02-23 thomas #include "got_lib_hash.h"
46 62ee7d94 2023-01-10 thomas #include "got_lib_delta.h"
47 62ee7d94 2023-01-10 thomas #include "got_lib_object.h"
48 62ee7d94 2023-01-10 thomas #include "got_lib_object_cache.h"
49 62ee7d94 2023-01-10 thomas #include "got_lib_pack.h"
50 62ee7d94 2023-01-10 thomas #include "got_lib_repository.h"
51 62ee7d94 2023-01-10 thomas #include "got_lib_gitproto.h"
52 62ee7d94 2023-01-10 thomas
53 62ee7d94 2023-01-10 thomas #include "gotd.h"
54 62ee7d94 2023-01-10 thomas #include "log.h"
55 62ee7d94 2023-01-10 thomas #include "session.h"
56 62ee7d94 2023-01-10 thomas
57 62ee7d94 2023-01-10 thomas
58 62ee7d94 2023-01-10 thomas static struct gotd_session {
59 62ee7d94 2023-01-10 thomas pid_t pid;
60 62ee7d94 2023-01-10 thomas const char *title;
61 62ee7d94 2023-01-10 thomas struct got_repository *repo;
62 62ee7d94 2023-01-10 thomas int *pack_fds;
63 62ee7d94 2023-01-10 thomas int *temp_fds;
64 62ee7d94 2023-01-10 thomas struct gotd_imsgev parent_iev;
65 62ee7d94 2023-01-10 thomas struct timeval request_timeout;
66 7fed8fa4 2023-06-22 thomas enum gotd_procid proc_id;
67 62ee7d94 2023-01-10 thomas } gotd_session;
68 62ee7d94 2023-01-10 thomas
69 62ee7d94 2023-01-10 thomas static struct gotd_session_client {
70 7b1db75e 2023-01-14 thomas enum gotd_session_state state;
71 62ee7d94 2023-01-10 thomas int is_writing;
72 62ee7d94 2023-01-10 thomas struct gotd_client_capability *capabilities;
73 62ee7d94 2023-01-10 thomas size_t ncapa_alloc;
74 62ee7d94 2023-01-10 thomas size_t ncapabilities;
75 62ee7d94 2023-01-10 thomas uint32_t id;
76 62ee7d94 2023-01-10 thomas int fd;
77 62ee7d94 2023-01-10 thomas int delta_cache_fd;
78 62ee7d94 2023-01-10 thomas struct gotd_imsgev iev;
79 62ee7d94 2023-01-10 thomas struct gotd_imsgev repo_child_iev;
80 62ee7d94 2023-01-10 thomas struct event tmo;
81 62ee7d94 2023-01-10 thomas uid_t euid;
82 62ee7d94 2023-01-10 thomas gid_t egid;
83 62ee7d94 2023-01-10 thomas char *packfile_path;
84 62ee7d94 2023-01-10 thomas char *packidx_path;
85 62ee7d94 2023-01-10 thomas int nref_updates;
86 98c7fd82 2023-01-23 thomas int accept_flush_pkt;
87 c8b73ac1 2023-08-23 thomas int flush_disconnect;
88 62ee7d94 2023-01-10 thomas } gotd_session_client;
89 62ee7d94 2023-01-10 thomas
90 62ee7d94 2023-01-10 thomas void gotd_session_sighdlr(int sig, short event, void *arg);
91 62ee7d94 2023-01-10 thomas static void gotd_session_shutdown(void);
92 62ee7d94 2023-01-10 thomas
93 62ee7d94 2023-01-10 thomas static void
94 62ee7d94 2023-01-10 thomas disconnect(struct gotd_session_client *client)
95 62ee7d94 2023-01-10 thomas {
96 62ee7d94 2023-01-10 thomas log_debug("uid %d: disconnecting", client->euid);
97 62ee7d94 2023-01-10 thomas
98 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&gotd_session.parent_iev,
99 7fed8fa4 2023-06-22 thomas GOTD_IMSG_DISCONNECT, gotd_session.proc_id, -1, NULL, 0) == -1)
100 62ee7d94 2023-01-10 thomas log_warn("imsg compose DISCONNECT");
101 62ee7d94 2023-01-10 thomas
102 62ee7d94 2023-01-10 thomas imsg_clear(&client->repo_child_iev.ibuf);
103 62ee7d94 2023-01-10 thomas event_del(&client->repo_child_iev.ev);
104 62ee7d94 2023-01-10 thomas evtimer_del(&client->tmo);
105 62ee7d94 2023-01-10 thomas close(client->fd);
106 62ee7d94 2023-01-10 thomas if (client->delta_cache_fd != -1)
107 62ee7d94 2023-01-10 thomas close(client->delta_cache_fd);
108 62ee7d94 2023-01-10 thomas if (client->packfile_path) {
109 62ee7d94 2023-01-10 thomas if (unlink(client->packfile_path) == -1 && errno != ENOENT)
110 62ee7d94 2023-01-10 thomas log_warn("unlink %s: ", client->packfile_path);
111 62ee7d94 2023-01-10 thomas free(client->packfile_path);
112 62ee7d94 2023-01-10 thomas }
113 62ee7d94 2023-01-10 thomas if (client->packidx_path) {
114 62ee7d94 2023-01-10 thomas if (unlink(client->packidx_path) == -1 && errno != ENOENT)
115 62ee7d94 2023-01-10 thomas log_warn("unlink %s: ", client->packidx_path);
116 62ee7d94 2023-01-10 thomas free(client->packidx_path);
117 62ee7d94 2023-01-10 thomas }
118 62ee7d94 2023-01-10 thomas free(client->capabilities);
119 62ee7d94 2023-01-10 thomas
120 62ee7d94 2023-01-10 thomas gotd_session_shutdown();
121 62ee7d94 2023-01-10 thomas }
122 62ee7d94 2023-01-10 thomas
123 62ee7d94 2023-01-10 thomas static void
124 62ee7d94 2023-01-10 thomas disconnect_on_error(struct gotd_session_client *client,
125 62ee7d94 2023-01-10 thomas const struct got_error *err)
126 62ee7d94 2023-01-10 thomas {
127 62ee7d94 2023-01-10 thomas struct imsgbuf ibuf;
128 62ee7d94 2023-01-10 thomas
129 62ee7d94 2023-01-10 thomas if (err->code != GOT_ERR_EOF) {
130 a6153ffb 2023-08-12 thomas log_warnx("uid %d: %s", client->euid, err->msg);
131 62ee7d94 2023-01-10 thomas imsg_init(&ibuf, client->fd);
132 7fed8fa4 2023-06-22 thomas gotd_imsg_send_error(&ibuf, 0, gotd_session.proc_id, err);
133 62ee7d94 2023-01-10 thomas imsg_clear(&ibuf);
134 62ee7d94 2023-01-10 thomas }
135 62ee7d94 2023-01-10 thomas
136 62ee7d94 2023-01-10 thomas disconnect(client);
137 62ee7d94 2023-01-10 thomas }
138 62ee7d94 2023-01-10 thomas
139 62ee7d94 2023-01-10 thomas static void
140 62ee7d94 2023-01-10 thomas gotd_request_timeout(int fd, short events, void *arg)
141 62ee7d94 2023-01-10 thomas {
142 62ee7d94 2023-01-10 thomas struct gotd_session_client *client = arg;
143 62ee7d94 2023-01-10 thomas
144 62ee7d94 2023-01-10 thomas log_debug("disconnecting uid %d due to timeout", client->euid);
145 62ee7d94 2023-01-10 thomas disconnect(client);
146 62ee7d94 2023-01-10 thomas }
147 62ee7d94 2023-01-10 thomas
148 62ee7d94 2023-01-10 thomas void
149 62ee7d94 2023-01-10 thomas gotd_session_sighdlr(int sig, short event, void *arg)
150 62ee7d94 2023-01-10 thomas {
151 62ee7d94 2023-01-10 thomas /*
152 62ee7d94 2023-01-10 thomas * Normal signal handler rules don't apply because libevent
153 62ee7d94 2023-01-10 thomas * decouples for us.
154 62ee7d94 2023-01-10 thomas */
155 62ee7d94 2023-01-10 thomas
156 62ee7d94 2023-01-10 thomas switch (sig) {
157 62ee7d94 2023-01-10 thomas case SIGHUP:
158 62ee7d94 2023-01-10 thomas log_info("%s: ignoring SIGHUP", __func__);
159 62ee7d94 2023-01-10 thomas break;
160 62ee7d94 2023-01-10 thomas case SIGUSR1:
161 62ee7d94 2023-01-10 thomas log_info("%s: ignoring SIGUSR1", __func__);
162 62ee7d94 2023-01-10 thomas break;
163 62ee7d94 2023-01-10 thomas case SIGTERM:
164 62ee7d94 2023-01-10 thomas case SIGINT:
165 62ee7d94 2023-01-10 thomas gotd_session_shutdown();
166 62ee7d94 2023-01-10 thomas /* NOTREACHED */
167 62ee7d94 2023-01-10 thomas break;
168 62ee7d94 2023-01-10 thomas default:
169 62ee7d94 2023-01-10 thomas fatalx("unexpected signal");
170 62ee7d94 2023-01-10 thomas }
171 62ee7d94 2023-01-10 thomas }
172 62ee7d94 2023-01-10 thomas
173 62ee7d94 2023-01-10 thomas static const struct got_error *
174 62ee7d94 2023-01-10 thomas recv_packfile_done(uint32_t *client_id, struct imsg *imsg)
175 62ee7d94 2023-01-10 thomas {
176 62ee7d94 2023-01-10 thomas struct gotd_imsg_packfile_done idone;
177 62ee7d94 2023-01-10 thomas size_t datalen;
178 62ee7d94 2023-01-10 thomas
179 62ee7d94 2023-01-10 thomas log_debug("packfile-done received");
180 62ee7d94 2023-01-10 thomas
181 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
182 62ee7d94 2023-01-10 thomas if (datalen != sizeof(idone))
183 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
184 62ee7d94 2023-01-10 thomas memcpy(&idone, imsg->data, sizeof(idone));
185 62ee7d94 2023-01-10 thomas
186 62ee7d94 2023-01-10 thomas *client_id = idone.client_id;
187 62ee7d94 2023-01-10 thomas return NULL;
188 62ee7d94 2023-01-10 thomas }
189 62ee7d94 2023-01-10 thomas
190 62ee7d94 2023-01-10 thomas static const struct got_error *
191 62ee7d94 2023-01-10 thomas recv_packfile_install(uint32_t *client_id, struct imsg *imsg)
192 62ee7d94 2023-01-10 thomas {
193 62ee7d94 2023-01-10 thomas struct gotd_imsg_packfile_install inst;
194 62ee7d94 2023-01-10 thomas size_t datalen;
195 62ee7d94 2023-01-10 thomas
196 62ee7d94 2023-01-10 thomas log_debug("packfile-install received");
197 62ee7d94 2023-01-10 thomas
198 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
199 62ee7d94 2023-01-10 thomas if (datalen != sizeof(inst))
200 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
201 62ee7d94 2023-01-10 thomas memcpy(&inst, imsg->data, sizeof(inst));
202 62ee7d94 2023-01-10 thomas
203 62ee7d94 2023-01-10 thomas *client_id = inst.client_id;
204 62ee7d94 2023-01-10 thomas return NULL;
205 62ee7d94 2023-01-10 thomas }
206 62ee7d94 2023-01-10 thomas
207 62ee7d94 2023-01-10 thomas static const struct got_error *
208 62ee7d94 2023-01-10 thomas recv_ref_updates_start(uint32_t *client_id, struct imsg *imsg)
209 62ee7d94 2023-01-10 thomas {
210 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_updates_start istart;
211 62ee7d94 2023-01-10 thomas size_t datalen;
212 62ee7d94 2023-01-10 thomas
213 62ee7d94 2023-01-10 thomas log_debug("ref-updates-start received");
214 62ee7d94 2023-01-10 thomas
215 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
216 62ee7d94 2023-01-10 thomas if (datalen != sizeof(istart))
217 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
218 62ee7d94 2023-01-10 thomas memcpy(&istart, imsg->data, sizeof(istart));
219 62ee7d94 2023-01-10 thomas
220 62ee7d94 2023-01-10 thomas *client_id = istart.client_id;
221 62ee7d94 2023-01-10 thomas return NULL;
222 62ee7d94 2023-01-10 thomas }
223 62ee7d94 2023-01-10 thomas
224 62ee7d94 2023-01-10 thomas static const struct got_error *
225 62ee7d94 2023-01-10 thomas recv_ref_update(uint32_t *client_id, struct imsg *imsg)
226 62ee7d94 2023-01-10 thomas {
227 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_update iref;
228 62ee7d94 2023-01-10 thomas size_t datalen;
229 62ee7d94 2023-01-10 thomas
230 62ee7d94 2023-01-10 thomas log_debug("ref-update received");
231 62ee7d94 2023-01-10 thomas
232 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
233 62ee7d94 2023-01-10 thomas if (datalen < sizeof(iref))
234 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
235 62ee7d94 2023-01-10 thomas memcpy(&iref, imsg->data, sizeof(iref));
236 62ee7d94 2023-01-10 thomas
237 62ee7d94 2023-01-10 thomas *client_id = iref.client_id;
238 62ee7d94 2023-01-10 thomas return NULL;
239 62ee7d94 2023-01-10 thomas }
240 62ee7d94 2023-01-10 thomas
241 62ee7d94 2023-01-10 thomas static const struct got_error *
242 62ee7d94 2023-01-10 thomas send_ref_update_ok(struct gotd_session_client *client,
243 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_update *iref, const char *refname)
244 62ee7d94 2023-01-10 thomas {
245 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_update_ok iok;
246 62ee7d94 2023-01-10 thomas struct gotd_imsgev *iev = &client->iev;
247 62ee7d94 2023-01-10 thomas struct ibuf *wbuf;
248 62ee7d94 2023-01-10 thomas size_t len;
249 62ee7d94 2023-01-10 thomas
250 62ee7d94 2023-01-10 thomas memset(&iok, 0, sizeof(iok));
251 62ee7d94 2023-01-10 thomas iok.client_id = client->id;
252 62ee7d94 2023-01-10 thomas memcpy(iok.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
253 62ee7d94 2023-01-10 thomas memcpy(iok.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
254 62ee7d94 2023-01-10 thomas iok.name_len = strlen(refname);
255 62ee7d94 2023-01-10 thomas
256 62ee7d94 2023-01-10 thomas len = sizeof(iok) + iok.name_len;
257 62ee7d94 2023-01-10 thomas wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE_OK,
258 7fed8fa4 2023-06-22 thomas gotd_session.proc_id, gotd_session.pid, len);
259 62ee7d94 2023-01-10 thomas if (wbuf == NULL)
260 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg_create REF_UPDATE_OK");
261 62ee7d94 2023-01-10 thomas
262 62ee7d94 2023-01-10 thomas if (imsg_add(wbuf, &iok, sizeof(iok)) == -1)
263 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg_add REF_UPDATE_OK");
264 62ee7d94 2023-01-10 thomas if (imsg_add(wbuf, refname, iok.name_len) == -1)
265 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg_add REF_UPDATE_OK");
266 62ee7d94 2023-01-10 thomas
267 62ee7d94 2023-01-10 thomas wbuf->fd = -1;
268 62ee7d94 2023-01-10 thomas imsg_close(&iev->ibuf, wbuf);
269 62ee7d94 2023-01-10 thomas gotd_imsg_event_add(iev);
270 62ee7d94 2023-01-10 thomas return NULL;
271 62ee7d94 2023-01-10 thomas }
272 62ee7d94 2023-01-10 thomas
273 62ee7d94 2023-01-10 thomas static void
274 62ee7d94 2023-01-10 thomas send_refs_updated(struct gotd_session_client *client)
275 62ee7d94 2023-01-10 thomas {
276 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_REFS_UPDATED,
277 7fed8fa4 2023-06-22 thomas gotd_session.proc_id, -1, NULL, 0) == -1)
278 62ee7d94 2023-01-10 thomas log_warn("imsg compose REFS_UPDATED");
279 62ee7d94 2023-01-10 thomas }
280 62ee7d94 2023-01-10 thomas
281 62ee7d94 2023-01-10 thomas static const struct got_error *
282 62ee7d94 2023-01-10 thomas send_ref_update_ng(struct gotd_session_client *client,
283 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_update *iref, const char *refname,
284 62ee7d94 2023-01-10 thomas const char *reason)
285 62ee7d94 2023-01-10 thomas {
286 62ee7d94 2023-01-10 thomas const struct got_error *ng_err;
287 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_update_ng ing;
288 62ee7d94 2023-01-10 thomas struct gotd_imsgev *iev = &client->iev;
289 62ee7d94 2023-01-10 thomas struct ibuf *wbuf;
290 62ee7d94 2023-01-10 thomas size_t len;
291 62ee7d94 2023-01-10 thomas
292 62ee7d94 2023-01-10 thomas memset(&ing, 0, sizeof(ing));
293 62ee7d94 2023-01-10 thomas ing.client_id = client->id;
294 62ee7d94 2023-01-10 thomas memcpy(ing.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
295 62ee7d94 2023-01-10 thomas memcpy(ing.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
296 62ee7d94 2023-01-10 thomas ing.name_len = strlen(refname);
297 62ee7d94 2023-01-10 thomas
298 62ee7d94 2023-01-10 thomas ng_err = got_error_fmt(GOT_ERR_REF_BUSY, "%s", reason);
299 62ee7d94 2023-01-10 thomas ing.reason_len = strlen(ng_err->msg);
300 62ee7d94 2023-01-10 thomas
301 62ee7d94 2023-01-10 thomas len = sizeof(ing) + ing.name_len + ing.reason_len;
302 62ee7d94 2023-01-10 thomas wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE_NG,
303 7fed8fa4 2023-06-22 thomas gotd_session.proc_id, gotd_session.pid, len);
304 62ee7d94 2023-01-10 thomas if (wbuf == NULL)
305 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg_create REF_UPDATE_NG");
306 62ee7d94 2023-01-10 thomas
307 62ee7d94 2023-01-10 thomas if (imsg_add(wbuf, &ing, sizeof(ing)) == -1)
308 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg_add REF_UPDATE_NG");
309 62ee7d94 2023-01-10 thomas if (imsg_add(wbuf, refname, ing.name_len) == -1)
310 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg_add REF_UPDATE_NG");
311 62ee7d94 2023-01-10 thomas if (imsg_add(wbuf, ng_err->msg, ing.reason_len) == -1)
312 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg_add REF_UPDATE_NG");
313 62ee7d94 2023-01-10 thomas
314 62ee7d94 2023-01-10 thomas wbuf->fd = -1;
315 62ee7d94 2023-01-10 thomas imsg_close(&iev->ibuf, wbuf);
316 62ee7d94 2023-01-10 thomas gotd_imsg_event_add(iev);
317 62ee7d94 2023-01-10 thomas return NULL;
318 62ee7d94 2023-01-10 thomas }
319 62ee7d94 2023-01-10 thomas
320 62ee7d94 2023-01-10 thomas static const struct got_error *
321 62ee7d94 2023-01-10 thomas install_pack(struct gotd_session_client *client, const char *repo_path,
322 62ee7d94 2023-01-10 thomas struct imsg *imsg)
323 62ee7d94 2023-01-10 thomas {
324 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
325 62ee7d94 2023-01-10 thomas struct gotd_imsg_packfile_install inst;
326 62ee7d94 2023-01-10 thomas char hex[SHA1_DIGEST_STRING_LENGTH];
327 62ee7d94 2023-01-10 thomas size_t datalen;
328 62ee7d94 2023-01-10 thomas char *packfile_path = NULL, *packidx_path = NULL;
329 62ee7d94 2023-01-10 thomas
330 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
331 62ee7d94 2023-01-10 thomas if (datalen != sizeof(inst))
332 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
333 62ee7d94 2023-01-10 thomas memcpy(&inst, imsg->data, sizeof(inst));
334 62ee7d94 2023-01-10 thomas
335 62ee7d94 2023-01-10 thomas if (client->packfile_path == NULL)
336 62ee7d94 2023-01-10 thomas return got_error_msg(GOT_ERR_BAD_REQUEST,
337 62ee7d94 2023-01-10 thomas "client has no pack file");
338 62ee7d94 2023-01-10 thomas if (client->packidx_path == NULL)
339 62ee7d94 2023-01-10 thomas return got_error_msg(GOT_ERR_BAD_REQUEST,
340 62ee7d94 2023-01-10 thomas "client has no pack file index");
341 62ee7d94 2023-01-10 thomas
342 62ee7d94 2023-01-10 thomas if (got_sha1_digest_to_str(inst.pack_sha1, hex, sizeof(hex)) == NULL)
343 62ee7d94 2023-01-10 thomas return got_error_msg(GOT_ERR_NO_SPACE,
344 62ee7d94 2023-01-10 thomas "could not convert pack file SHA1 to hex");
345 62ee7d94 2023-01-10 thomas
346 62ee7d94 2023-01-10 thomas if (asprintf(&packfile_path, "/%s/%s/pack-%s.pack",
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;
350 62ee7d94 2023-01-10 thomas }
351 62ee7d94 2023-01-10 thomas
352 62ee7d94 2023-01-10 thomas if (asprintf(&packidx_path, "/%s/%s/pack-%s.idx",
353 62ee7d94 2023-01-10 thomas repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
354 62ee7d94 2023-01-10 thomas err = got_error_from_errno("asprintf");
355 62ee7d94 2023-01-10 thomas goto done;
356 62ee7d94 2023-01-10 thomas }
357 62ee7d94 2023-01-10 thomas
358 62ee7d94 2023-01-10 thomas if (rename(client->packfile_path, packfile_path) == -1) {
359 62ee7d94 2023-01-10 thomas err = got_error_from_errno3("rename", client->packfile_path,
360 62ee7d94 2023-01-10 thomas packfile_path);
361 62ee7d94 2023-01-10 thomas goto done;
362 62ee7d94 2023-01-10 thomas }
363 62ee7d94 2023-01-10 thomas
364 62ee7d94 2023-01-10 thomas free(client->packfile_path);
365 62ee7d94 2023-01-10 thomas client->packfile_path = NULL;
366 62ee7d94 2023-01-10 thomas
367 62ee7d94 2023-01-10 thomas if (rename(client->packidx_path, packidx_path) == -1) {
368 62ee7d94 2023-01-10 thomas err = got_error_from_errno3("rename", client->packidx_path,
369 62ee7d94 2023-01-10 thomas packidx_path);
370 62ee7d94 2023-01-10 thomas goto done;
371 62ee7d94 2023-01-10 thomas }
372 62ee7d94 2023-01-10 thomas
373 62ee7d94 2023-01-10 thomas free(client->packidx_path);
374 62ee7d94 2023-01-10 thomas client->packidx_path = NULL;
375 62ee7d94 2023-01-10 thomas done:
376 62ee7d94 2023-01-10 thomas free(packfile_path);
377 62ee7d94 2023-01-10 thomas free(packidx_path);
378 62ee7d94 2023-01-10 thomas return err;
379 62ee7d94 2023-01-10 thomas }
380 62ee7d94 2023-01-10 thomas
381 62ee7d94 2023-01-10 thomas static const struct got_error *
382 62ee7d94 2023-01-10 thomas begin_ref_updates(struct gotd_session_client *client, struct imsg *imsg)
383 62ee7d94 2023-01-10 thomas {
384 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_updates_start istart;
385 62ee7d94 2023-01-10 thomas size_t datalen;
386 62ee7d94 2023-01-10 thomas
387 62ee7d94 2023-01-10 thomas if (client->nref_updates != -1)
388 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
389 62ee7d94 2023-01-10 thomas
390 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
391 62ee7d94 2023-01-10 thomas if (datalen != sizeof(istart))
392 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
393 62ee7d94 2023-01-10 thomas memcpy(&istart, imsg->data, sizeof(istart));
394 62ee7d94 2023-01-10 thomas
395 62ee7d94 2023-01-10 thomas if (istart.nref_updates <= 0)
396 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
397 62ee7d94 2023-01-10 thomas
398 62ee7d94 2023-01-10 thomas client->nref_updates = istart.nref_updates;
399 62ee7d94 2023-01-10 thomas return NULL;
400 62ee7d94 2023-01-10 thomas }
401 62ee7d94 2023-01-10 thomas
402 62ee7d94 2023-01-10 thomas static const struct got_error *
403 d98779cd 2023-01-19 thomas update_ref(int *shut, struct gotd_session_client *client,
404 d98779cd 2023-01-19 thomas const char *repo_path, struct imsg *imsg)
405 62ee7d94 2023-01-10 thomas {
406 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
407 62ee7d94 2023-01-10 thomas struct got_repository *repo = NULL;
408 62ee7d94 2023-01-10 thomas struct got_reference *ref = NULL;
409 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_update iref;
410 62ee7d94 2023-01-10 thomas struct got_object_id old_id, new_id;
411 62ee7d94 2023-01-10 thomas struct got_object_id *id = NULL;
412 62ee7d94 2023-01-10 thomas struct got_object *obj = NULL;
413 62ee7d94 2023-01-10 thomas char *refname = NULL;
414 62ee7d94 2023-01-10 thomas size_t datalen;
415 62ee7d94 2023-01-10 thomas int locked = 0;
416 8a9c582b 2023-06-22 thomas char hex1[SHA1_DIGEST_STRING_LENGTH];
417 8a9c582b 2023-06-22 thomas char hex2[SHA1_DIGEST_STRING_LENGTH];
418 62ee7d94 2023-01-10 thomas
419 62ee7d94 2023-01-10 thomas log_debug("update-ref from uid %d", client->euid);
420 62ee7d94 2023-01-10 thomas
421 62ee7d94 2023-01-10 thomas if (client->nref_updates <= 0)
422 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
423 62ee7d94 2023-01-10 thomas
424 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
425 62ee7d94 2023-01-10 thomas if (datalen < sizeof(iref))
426 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
427 62ee7d94 2023-01-10 thomas memcpy(&iref, imsg->data, sizeof(iref));
428 62ee7d94 2023-01-10 thomas if (datalen != sizeof(iref) + iref.name_len)
429 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
430 fcbb06bf 2023-01-14 thomas refname = strndup(imsg->data + sizeof(iref), iref.name_len);
431 62ee7d94 2023-01-10 thomas if (refname == NULL)
432 fcbb06bf 2023-01-14 thomas return got_error_from_errno("strndup");
433 62ee7d94 2023-01-10 thomas
434 62ee7d94 2023-01-10 thomas log_debug("updating ref %s for uid %d", refname, client->euid);
435 62ee7d94 2023-01-10 thomas
436 62ee7d94 2023-01-10 thomas err = got_repo_open(&repo, repo_path, NULL, NULL);
437 62ee7d94 2023-01-10 thomas if (err)
438 62ee7d94 2023-01-10 thomas goto done;
439 62ee7d94 2023-01-10 thomas
440 62ee7d94 2023-01-10 thomas memcpy(old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
441 62ee7d94 2023-01-10 thomas memcpy(new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
442 49563dfb 2023-01-28 thomas err = got_object_open(&obj, repo,
443 49563dfb 2023-01-28 thomas iref.delete_ref ? &old_id : &new_id);
444 62ee7d94 2023-01-10 thomas if (err)
445 62ee7d94 2023-01-10 thomas goto done;
446 62ee7d94 2023-01-10 thomas
447 62ee7d94 2023-01-10 thomas if (iref.ref_is_new) {
448 62ee7d94 2023-01-10 thomas err = got_ref_open(&ref, repo, refname, 0);
449 62ee7d94 2023-01-10 thomas if (err) {
450 62ee7d94 2023-01-10 thomas if (err->code != GOT_ERR_NOT_REF)
451 62ee7d94 2023-01-10 thomas goto done;
452 62ee7d94 2023-01-10 thomas err = got_ref_alloc(&ref, refname, &new_id);
453 62ee7d94 2023-01-10 thomas if (err)
454 62ee7d94 2023-01-10 thomas goto done;
455 62ee7d94 2023-01-10 thomas err = got_ref_write(ref, repo); /* will lock/unlock */
456 62ee7d94 2023-01-10 thomas if (err)
457 62ee7d94 2023-01-10 thomas goto done;
458 62ee7d94 2023-01-10 thomas } else {
459 8a9c582b 2023-06-22 thomas err = got_ref_resolve(&id, repo, ref);
460 8a9c582b 2023-06-22 thomas if (err)
461 8a9c582b 2023-06-22 thomas goto done;
462 8a9c582b 2023-06-22 thomas got_object_id_hex(&new_id, hex1, sizeof(hex1));
463 8a9c582b 2023-06-22 thomas got_object_id_hex(id, hex2, sizeof(hex2));
464 62ee7d94 2023-01-10 thomas err = got_error_fmt(GOT_ERR_REF_BUSY,
465 8a9c582b 2023-06-22 thomas "Addition %s: %s failed; %s: %s has been "
466 8a9c582b 2023-06-22 thomas "created by someone else while transaction "
467 8a9c582b 2023-06-22 thomas "was in progress",
468 8a9c582b 2023-06-22 thomas got_ref_get_name(ref), hex1,
469 8a9c582b 2023-06-22 thomas got_ref_get_name(ref), hex2);
470 62ee7d94 2023-01-10 thomas goto done;
471 62ee7d94 2023-01-10 thomas }
472 49563dfb 2023-01-28 thomas } else if (iref.delete_ref) {
473 49563dfb 2023-01-28 thomas err = got_ref_open(&ref, repo, refname, 1 /* lock */);
474 49563dfb 2023-01-28 thomas if (err)
475 49563dfb 2023-01-28 thomas goto done;
476 49563dfb 2023-01-28 thomas locked = 1;
477 49563dfb 2023-01-28 thomas
478 49563dfb 2023-01-28 thomas err = got_ref_resolve(&id, repo, ref);
479 49563dfb 2023-01-28 thomas if (err)
480 49563dfb 2023-01-28 thomas goto done;
481 49563dfb 2023-01-28 thomas
482 49563dfb 2023-01-28 thomas if (got_object_id_cmp(id, &old_id) != 0) {
483 8a9c582b 2023-06-22 thomas got_object_id_hex(&old_id, hex1, sizeof(hex1));
484 8a9c582b 2023-06-22 thomas got_object_id_hex(id, hex2, sizeof(hex2));
485 49563dfb 2023-01-28 thomas err = got_error_fmt(GOT_ERR_REF_BUSY,
486 8a9c582b 2023-06-22 thomas "Deletion %s: %s failed; %s: %s has been "
487 8a9c582b 2023-06-22 thomas "created by someone else while transaction "
488 8a9c582b 2023-06-22 thomas "was in progress",
489 8a9c582b 2023-06-22 thomas got_ref_get_name(ref), hex1,
490 8a9c582b 2023-06-22 thomas got_ref_get_name(ref), hex2);
491 49563dfb 2023-01-28 thomas goto done;
492 49563dfb 2023-01-28 thomas }
493 49563dfb 2023-01-28 thomas
494 49563dfb 2023-01-28 thomas err = got_ref_delete(ref, repo);
495 49563dfb 2023-01-28 thomas if (err)
496 49563dfb 2023-01-28 thomas goto done;
497 49563dfb 2023-01-28 thomas
498 49563dfb 2023-01-28 thomas free(id);
499 49563dfb 2023-01-28 thomas id = NULL;
500 62ee7d94 2023-01-10 thomas } else {
501 62ee7d94 2023-01-10 thomas err = got_ref_open(&ref, repo, refname, 1 /* lock */);
502 62ee7d94 2023-01-10 thomas if (err)
503 62ee7d94 2023-01-10 thomas goto done;
504 62ee7d94 2023-01-10 thomas locked = 1;
505 62ee7d94 2023-01-10 thomas
506 62ee7d94 2023-01-10 thomas err = got_ref_resolve(&id, repo, ref);
507 62ee7d94 2023-01-10 thomas if (err)
508 62ee7d94 2023-01-10 thomas goto done;
509 62ee7d94 2023-01-10 thomas
510 62ee7d94 2023-01-10 thomas if (got_object_id_cmp(id, &old_id) != 0) {
511 8a9c582b 2023-06-22 thomas got_object_id_hex(&old_id, hex1, sizeof(hex1));
512 8a9c582b 2023-06-22 thomas got_object_id_hex(id, hex2, sizeof(hex2));
513 62ee7d94 2023-01-10 thomas err = got_error_fmt(GOT_ERR_REF_BUSY,
514 8a9c582b 2023-06-22 thomas "Update %s: %s failed; %s: %s has been "
515 8a9c582b 2023-06-22 thomas "created by someone else while transaction "
516 8a9c582b 2023-06-22 thomas "was in progress",
517 8a9c582b 2023-06-22 thomas got_ref_get_name(ref), hex1,
518 8a9c582b 2023-06-22 thomas got_ref_get_name(ref), hex2);
519 62ee7d94 2023-01-10 thomas goto done;
520 62ee7d94 2023-01-10 thomas }
521 62ee7d94 2023-01-10 thomas
522 169def41 2023-06-22 thomas if (got_object_id_cmp(&new_id, &old_id) != 0) {
523 169def41 2023-06-22 thomas err = got_ref_change_ref(ref, &new_id);
524 169def41 2023-06-22 thomas if (err)
525 169def41 2023-06-22 thomas goto done;
526 62ee7d94 2023-01-10 thomas
527 169def41 2023-06-22 thomas err = got_ref_write(ref, repo);
528 169def41 2023-06-22 thomas if (err)
529 169def41 2023-06-22 thomas goto done;
530 169def41 2023-06-22 thomas }
531 62ee7d94 2023-01-10 thomas
532 62ee7d94 2023-01-10 thomas free(id);
533 62ee7d94 2023-01-10 thomas id = NULL;
534 62ee7d94 2023-01-10 thomas }
535 62ee7d94 2023-01-10 thomas done:
536 62ee7d94 2023-01-10 thomas if (err) {
537 62ee7d94 2023-01-10 thomas if (err->code == GOT_ERR_LOCKFILE_TIMEOUT) {
538 62ee7d94 2023-01-10 thomas err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
539 62ee7d94 2023-01-10 thomas "could not acquire exclusive file lock for %s",
540 62ee7d94 2023-01-10 thomas refname);
541 62ee7d94 2023-01-10 thomas }
542 62ee7d94 2023-01-10 thomas send_ref_update_ng(client, &iref, refname, err->msg);
543 62ee7d94 2023-01-10 thomas } else
544 62ee7d94 2023-01-10 thomas send_ref_update_ok(client, &iref, refname);
545 62ee7d94 2023-01-10 thomas
546 62ee7d94 2023-01-10 thomas if (client->nref_updates > 0) {
547 62ee7d94 2023-01-10 thomas client->nref_updates--;
548 d98779cd 2023-01-19 thomas if (client->nref_updates == 0) {
549 62ee7d94 2023-01-10 thomas send_refs_updated(client);
550 c8b73ac1 2023-08-23 thomas client->flush_disconnect = 1;
551 d98779cd 2023-01-19 thomas }
552 62ee7d94 2023-01-10 thomas
553 62ee7d94 2023-01-10 thomas }
554 62ee7d94 2023-01-10 thomas if (locked) {
555 62ee7d94 2023-01-10 thomas const struct got_error *unlock_err;
556 62ee7d94 2023-01-10 thomas unlock_err = got_ref_unlock(ref);
557 62ee7d94 2023-01-10 thomas if (unlock_err && err == NULL)
558 62ee7d94 2023-01-10 thomas err = unlock_err;
559 62ee7d94 2023-01-10 thomas }
560 62ee7d94 2023-01-10 thomas if (ref)
561 62ee7d94 2023-01-10 thomas got_ref_close(ref);
562 62ee7d94 2023-01-10 thomas if (obj)
563 62ee7d94 2023-01-10 thomas got_object_close(obj);
564 62ee7d94 2023-01-10 thomas if (repo)
565 62ee7d94 2023-01-10 thomas got_repo_close(repo);
566 62ee7d94 2023-01-10 thomas free(refname);
567 62ee7d94 2023-01-10 thomas free(id);
568 62ee7d94 2023-01-10 thomas return err;
569 62ee7d94 2023-01-10 thomas }
570 62ee7d94 2023-01-10 thomas
571 62ee7d94 2023-01-10 thomas static void
572 62ee7d94 2023-01-10 thomas session_dispatch_repo_child(int fd, short event, void *arg)
573 62ee7d94 2023-01-10 thomas {
574 62ee7d94 2023-01-10 thomas struct gotd_imsgev *iev = arg;
575 62ee7d94 2023-01-10 thomas struct imsgbuf *ibuf = &iev->ibuf;
576 62ee7d94 2023-01-10 thomas struct gotd_session_client *client = &gotd_session_client;
577 62ee7d94 2023-01-10 thomas ssize_t n;
578 62ee7d94 2023-01-10 thomas int shut = 0;
579 62ee7d94 2023-01-10 thomas struct imsg imsg;
580 62ee7d94 2023-01-10 thomas
581 62ee7d94 2023-01-10 thomas if (event & EV_READ) {
582 62ee7d94 2023-01-10 thomas if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
583 62ee7d94 2023-01-10 thomas fatal("imsg_read error");
584 62ee7d94 2023-01-10 thomas if (n == 0) {
585 62ee7d94 2023-01-10 thomas /* Connection closed. */
586 62ee7d94 2023-01-10 thomas shut = 1;
587 62ee7d94 2023-01-10 thomas goto done;
588 62ee7d94 2023-01-10 thomas }
589 62ee7d94 2023-01-10 thomas }
590 62ee7d94 2023-01-10 thomas
591 62ee7d94 2023-01-10 thomas if (event & EV_WRITE) {
592 62ee7d94 2023-01-10 thomas n = msgbuf_write(&ibuf->w);
593 62ee7d94 2023-01-10 thomas if (n == -1 && errno != EAGAIN)
594 62ee7d94 2023-01-10 thomas fatal("msgbuf_write");
595 62ee7d94 2023-01-10 thomas if (n == 0) {
596 62ee7d94 2023-01-10 thomas /* Connection closed. */
597 62ee7d94 2023-01-10 thomas shut = 1;
598 62ee7d94 2023-01-10 thomas goto done;
599 62ee7d94 2023-01-10 thomas }
600 62ee7d94 2023-01-10 thomas }
601 62ee7d94 2023-01-10 thomas
602 62ee7d94 2023-01-10 thomas for (;;) {
603 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
604 62ee7d94 2023-01-10 thomas uint32_t client_id = 0;
605 62ee7d94 2023-01-10 thomas int do_disconnect = 0;
606 62ee7d94 2023-01-10 thomas int do_ref_updates = 0, do_ref_update = 0;
607 62ee7d94 2023-01-10 thomas int do_packfile_install = 0;
608 62ee7d94 2023-01-10 thomas
609 62ee7d94 2023-01-10 thomas if ((n = imsg_get(ibuf, &imsg)) == -1)
610 62ee7d94 2023-01-10 thomas fatal("%s: imsg_get error", __func__);
611 62ee7d94 2023-01-10 thomas if (n == 0) /* No more messages. */
612 62ee7d94 2023-01-10 thomas break;
613 62ee7d94 2023-01-10 thomas
614 62ee7d94 2023-01-10 thomas switch (imsg.hdr.type) {
615 62ee7d94 2023-01-10 thomas case GOTD_IMSG_ERROR:
616 62ee7d94 2023-01-10 thomas do_disconnect = 1;
617 62ee7d94 2023-01-10 thomas err = gotd_imsg_recv_error(&client_id, &imsg);
618 62ee7d94 2023-01-10 thomas break;
619 62ee7d94 2023-01-10 thomas case GOTD_IMSG_PACKFILE_DONE:
620 62ee7d94 2023-01-10 thomas do_disconnect = 1;
621 62ee7d94 2023-01-10 thomas err = recv_packfile_done(&client_id, &imsg);
622 62ee7d94 2023-01-10 thomas break;
623 62ee7d94 2023-01-10 thomas case GOTD_IMSG_PACKFILE_INSTALL:
624 62ee7d94 2023-01-10 thomas err = recv_packfile_install(&client_id, &imsg);
625 62ee7d94 2023-01-10 thomas if (err == NULL)
626 62ee7d94 2023-01-10 thomas do_packfile_install = 1;
627 62ee7d94 2023-01-10 thomas break;
628 62ee7d94 2023-01-10 thomas case GOTD_IMSG_REF_UPDATES_START:
629 62ee7d94 2023-01-10 thomas err = recv_ref_updates_start(&client_id, &imsg);
630 62ee7d94 2023-01-10 thomas if (err == NULL)
631 62ee7d94 2023-01-10 thomas do_ref_updates = 1;
632 62ee7d94 2023-01-10 thomas break;
633 62ee7d94 2023-01-10 thomas case GOTD_IMSG_REF_UPDATE:
634 62ee7d94 2023-01-10 thomas err = recv_ref_update(&client_id, &imsg);
635 62ee7d94 2023-01-10 thomas if (err == NULL)
636 62ee7d94 2023-01-10 thomas do_ref_update = 1;
637 62ee7d94 2023-01-10 thomas break;
638 62ee7d94 2023-01-10 thomas default:
639 62ee7d94 2023-01-10 thomas log_debug("unexpected imsg %d", imsg.hdr.type);
640 62ee7d94 2023-01-10 thomas break;
641 62ee7d94 2023-01-10 thomas }
642 62ee7d94 2023-01-10 thomas
643 62ee7d94 2023-01-10 thomas if (do_disconnect) {
644 62ee7d94 2023-01-10 thomas if (err)
645 62ee7d94 2023-01-10 thomas disconnect_on_error(client, err);
646 62ee7d94 2023-01-10 thomas else
647 62ee7d94 2023-01-10 thomas disconnect(client);
648 62ee7d94 2023-01-10 thomas } else {
649 62ee7d94 2023-01-10 thomas if (do_packfile_install)
650 62ee7d94 2023-01-10 thomas err = install_pack(client,
651 62ee7d94 2023-01-10 thomas gotd_session.repo->path, &imsg);
652 62ee7d94 2023-01-10 thomas else if (do_ref_updates)
653 62ee7d94 2023-01-10 thomas err = begin_ref_updates(client, &imsg);
654 62ee7d94 2023-01-10 thomas else if (do_ref_update)
655 d98779cd 2023-01-19 thomas err = update_ref(&shut, client,
656 62ee7d94 2023-01-10 thomas gotd_session.repo->path, &imsg);
657 62ee7d94 2023-01-10 thomas if (err)
658 62ee7d94 2023-01-10 thomas log_warnx("uid %d: %s", client->euid, err->msg);
659 62ee7d94 2023-01-10 thomas }
660 62ee7d94 2023-01-10 thomas imsg_free(&imsg);
661 62ee7d94 2023-01-10 thomas }
662 62ee7d94 2023-01-10 thomas done:
663 62ee7d94 2023-01-10 thomas if (!shut) {
664 62ee7d94 2023-01-10 thomas gotd_imsg_event_add(iev);
665 62ee7d94 2023-01-10 thomas } else {
666 62ee7d94 2023-01-10 thomas /* This pipe is dead. Remove its event handler */
667 62ee7d94 2023-01-10 thomas event_del(&iev->ev);
668 62ee7d94 2023-01-10 thomas event_loopexit(NULL);
669 62ee7d94 2023-01-10 thomas }
670 62ee7d94 2023-01-10 thomas }
671 62ee7d94 2023-01-10 thomas
672 62ee7d94 2023-01-10 thomas static const struct got_error *
673 62ee7d94 2023-01-10 thomas recv_capabilities(struct gotd_session_client *client, struct imsg *imsg)
674 62ee7d94 2023-01-10 thomas {
675 62ee7d94 2023-01-10 thomas struct gotd_imsg_capabilities icapas;
676 62ee7d94 2023-01-10 thomas size_t datalen;
677 62ee7d94 2023-01-10 thomas
678 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
679 62ee7d94 2023-01-10 thomas if (datalen != sizeof(icapas))
680 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
681 62ee7d94 2023-01-10 thomas memcpy(&icapas, imsg->data, sizeof(icapas));
682 62ee7d94 2023-01-10 thomas
683 62ee7d94 2023-01-10 thomas client->ncapa_alloc = icapas.ncapabilities;
684 62ee7d94 2023-01-10 thomas client->capabilities = calloc(client->ncapa_alloc,
685 62ee7d94 2023-01-10 thomas sizeof(*client->capabilities));
686 62ee7d94 2023-01-10 thomas if (client->capabilities == NULL) {
687 62ee7d94 2023-01-10 thomas client->ncapa_alloc = 0;
688 62ee7d94 2023-01-10 thomas return got_error_from_errno("calloc");
689 62ee7d94 2023-01-10 thomas }
690 62ee7d94 2023-01-10 thomas
691 62ee7d94 2023-01-10 thomas log_debug("expecting %zu capabilities from uid %d",
692 62ee7d94 2023-01-10 thomas client->ncapa_alloc, client->euid);
693 62ee7d94 2023-01-10 thomas return NULL;
694 62ee7d94 2023-01-10 thomas }
695 62ee7d94 2023-01-10 thomas
696 62ee7d94 2023-01-10 thomas static const struct got_error *
697 62ee7d94 2023-01-10 thomas recv_capability(struct gotd_session_client *client, struct imsg *imsg)
698 62ee7d94 2023-01-10 thomas {
699 62ee7d94 2023-01-10 thomas struct gotd_imsg_capability icapa;
700 62ee7d94 2023-01-10 thomas struct gotd_client_capability *capa;
701 62ee7d94 2023-01-10 thomas size_t datalen;
702 62ee7d94 2023-01-10 thomas char *key, *value = NULL;
703 62ee7d94 2023-01-10 thomas
704 62ee7d94 2023-01-10 thomas if (client->capabilities == NULL ||
705 62ee7d94 2023-01-10 thomas client->ncapabilities >= client->ncapa_alloc) {
706 62ee7d94 2023-01-10 thomas return got_error_msg(GOT_ERR_BAD_REQUEST,
707 62ee7d94 2023-01-10 thomas "unexpected capability received");
708 62ee7d94 2023-01-10 thomas }
709 62ee7d94 2023-01-10 thomas
710 62ee7d94 2023-01-10 thomas memset(&icapa, 0, sizeof(icapa));
711 62ee7d94 2023-01-10 thomas
712 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
713 62ee7d94 2023-01-10 thomas if (datalen < sizeof(icapa))
714 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
715 62ee7d94 2023-01-10 thomas memcpy(&icapa, imsg->data, sizeof(icapa));
716 62ee7d94 2023-01-10 thomas
717 62ee7d94 2023-01-10 thomas if (datalen != sizeof(icapa) + icapa.key_len + icapa.value_len)
718 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
719 62ee7d94 2023-01-10 thomas
720 fcbb06bf 2023-01-14 thomas key = strndup(imsg->data + sizeof(icapa), icapa.key_len);
721 62ee7d94 2023-01-10 thomas if (key == NULL)
722 fcbb06bf 2023-01-14 thomas return got_error_from_errno("strndup");
723 62ee7d94 2023-01-10 thomas if (icapa.value_len > 0) {
724 fcbb06bf 2023-01-14 thomas value = strndup(imsg->data + sizeof(icapa) + icapa.key_len,
725 fcbb06bf 2023-01-14 thomas icapa.value_len);
726 62ee7d94 2023-01-10 thomas if (value == NULL) {
727 62ee7d94 2023-01-10 thomas free(key);
728 fcbb06bf 2023-01-14 thomas return got_error_from_errno("strndup");
729 62ee7d94 2023-01-10 thomas }
730 62ee7d94 2023-01-10 thomas }
731 62ee7d94 2023-01-10 thomas
732 62ee7d94 2023-01-10 thomas capa = &client->capabilities[client->ncapabilities++];
733 62ee7d94 2023-01-10 thomas capa->key = key;
734 62ee7d94 2023-01-10 thomas capa->value = value;
735 62ee7d94 2023-01-10 thomas
736 62ee7d94 2023-01-10 thomas if (value)
737 62ee7d94 2023-01-10 thomas log_debug("uid %d: capability %s=%s", client->euid, key, value);
738 62ee7d94 2023-01-10 thomas else
739 62ee7d94 2023-01-10 thomas log_debug("uid %d: capability %s", client->euid, key);
740 62ee7d94 2023-01-10 thomas
741 62ee7d94 2023-01-10 thomas return NULL;
742 62ee7d94 2023-01-10 thomas }
743 62ee7d94 2023-01-10 thomas
744 62ee7d94 2023-01-10 thomas static const struct got_error *
745 62ee7d94 2023-01-10 thomas ensure_client_is_reading(struct gotd_session_client *client)
746 62ee7d94 2023-01-10 thomas {
747 62ee7d94 2023-01-10 thomas if (client->is_writing) {
748 62ee7d94 2023-01-10 thomas return got_error_fmt(GOT_ERR_BAD_PACKET,
749 62ee7d94 2023-01-10 thomas "uid %d made a read-request but is not reading from "
750 62ee7d94 2023-01-10 thomas "a repository", client->euid);
751 62ee7d94 2023-01-10 thomas }
752 62ee7d94 2023-01-10 thomas
753 62ee7d94 2023-01-10 thomas return NULL;
754 62ee7d94 2023-01-10 thomas }
755 62ee7d94 2023-01-10 thomas
756 62ee7d94 2023-01-10 thomas static const struct got_error *
757 62ee7d94 2023-01-10 thomas ensure_client_is_writing(struct gotd_session_client *client)
758 62ee7d94 2023-01-10 thomas {
759 62ee7d94 2023-01-10 thomas if (!client->is_writing) {
760 62ee7d94 2023-01-10 thomas return got_error_fmt(GOT_ERR_BAD_PACKET,
761 62ee7d94 2023-01-10 thomas "uid %d made a write-request but is not writing to "
762 62ee7d94 2023-01-10 thomas "a repository", client->euid);
763 62ee7d94 2023-01-10 thomas }
764 62ee7d94 2023-01-10 thomas
765 62ee7d94 2023-01-10 thomas return NULL;
766 62ee7d94 2023-01-10 thomas }
767 62ee7d94 2023-01-10 thomas
768 62ee7d94 2023-01-10 thomas static const struct got_error *
769 62ee7d94 2023-01-10 thomas forward_want(struct gotd_session_client *client, struct imsg *imsg)
770 62ee7d94 2023-01-10 thomas {
771 62ee7d94 2023-01-10 thomas struct gotd_imsg_want ireq;
772 62ee7d94 2023-01-10 thomas struct gotd_imsg_want iwant;
773 62ee7d94 2023-01-10 thomas size_t datalen;
774 62ee7d94 2023-01-10 thomas
775 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
776 62ee7d94 2023-01-10 thomas if (datalen != sizeof(ireq))
777 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
778 62ee7d94 2023-01-10 thomas
779 62ee7d94 2023-01-10 thomas memcpy(&ireq, imsg->data, datalen);
780 62ee7d94 2023-01-10 thomas
781 62ee7d94 2023-01-10 thomas memset(&iwant, 0, sizeof(iwant));
782 62ee7d94 2023-01-10 thomas memcpy(iwant.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
783 62ee7d94 2023-01-10 thomas iwant.client_id = client->id;
784 62ee7d94 2023-01-10 thomas
785 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&client->repo_child_iev, GOTD_IMSG_WANT,
786 7fed8fa4 2023-06-22 thomas gotd_session.proc_id, -1, &iwant, sizeof(iwant)) == -1)
787 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg compose WANT");
788 62ee7d94 2023-01-10 thomas
789 62ee7d94 2023-01-10 thomas return NULL;
790 62ee7d94 2023-01-10 thomas }
791 62ee7d94 2023-01-10 thomas
792 62ee7d94 2023-01-10 thomas static const struct got_error *
793 62ee7d94 2023-01-10 thomas forward_ref_update(struct gotd_session_client *client, struct imsg *imsg)
794 62ee7d94 2023-01-10 thomas {
795 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
796 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_update ireq;
797 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_update *iref = NULL;
798 62ee7d94 2023-01-10 thomas size_t datalen;
799 62ee7d94 2023-01-10 thomas
800 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
801 62ee7d94 2023-01-10 thomas if (datalen < sizeof(ireq))
802 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
803 62ee7d94 2023-01-10 thomas memcpy(&ireq, imsg->data, sizeof(ireq));
804 62ee7d94 2023-01-10 thomas if (datalen != sizeof(ireq) + ireq.name_len)
805 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
806 62ee7d94 2023-01-10 thomas
807 62ee7d94 2023-01-10 thomas iref = malloc(datalen);
808 62ee7d94 2023-01-10 thomas if (iref == NULL)
809 62ee7d94 2023-01-10 thomas return got_error_from_errno("malloc");
810 62ee7d94 2023-01-10 thomas memcpy(iref, imsg->data, datalen);
811 62ee7d94 2023-01-10 thomas
812 62ee7d94 2023-01-10 thomas iref->client_id = client->id;
813 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&client->repo_child_iev,
814 7fed8fa4 2023-06-22 thomas GOTD_IMSG_REF_UPDATE, gotd_session.proc_id, -1,
815 7fed8fa4 2023-06-22 thomas iref, datalen) == -1)
816 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose REF_UPDATE");
817 62ee7d94 2023-01-10 thomas free(iref);
818 62ee7d94 2023-01-10 thomas return err;
819 62ee7d94 2023-01-10 thomas }
820 62ee7d94 2023-01-10 thomas
821 62ee7d94 2023-01-10 thomas static const struct got_error *
822 62ee7d94 2023-01-10 thomas forward_have(struct gotd_session_client *client, struct imsg *imsg)
823 62ee7d94 2023-01-10 thomas {
824 62ee7d94 2023-01-10 thomas struct gotd_imsg_have ireq;
825 62ee7d94 2023-01-10 thomas struct gotd_imsg_have ihave;
826 62ee7d94 2023-01-10 thomas size_t datalen;
827 62ee7d94 2023-01-10 thomas
828 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
829 62ee7d94 2023-01-10 thomas if (datalen != sizeof(ireq))
830 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
831 62ee7d94 2023-01-10 thomas
832 62ee7d94 2023-01-10 thomas memcpy(&ireq, imsg->data, datalen);
833 62ee7d94 2023-01-10 thomas
834 62ee7d94 2023-01-10 thomas memset(&ihave, 0, sizeof(ihave));
835 62ee7d94 2023-01-10 thomas memcpy(ihave.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
836 62ee7d94 2023-01-10 thomas ihave.client_id = client->id;
837 62ee7d94 2023-01-10 thomas
838 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&client->repo_child_iev, GOTD_IMSG_HAVE,
839 7fed8fa4 2023-06-22 thomas gotd_session.proc_id, -1, &ihave, sizeof(ihave)) == -1)
840 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg compose HAVE");
841 62ee7d94 2023-01-10 thomas
842 62ee7d94 2023-01-10 thomas return NULL;
843 62ee7d94 2023-01-10 thomas }
844 62ee7d94 2023-01-10 thomas
845 62ee7d94 2023-01-10 thomas static int
846 62ee7d94 2023-01-10 thomas client_has_capability(struct gotd_session_client *client, const char *capastr)
847 62ee7d94 2023-01-10 thomas {
848 62ee7d94 2023-01-10 thomas struct gotd_client_capability *capa;
849 62ee7d94 2023-01-10 thomas size_t i;
850 62ee7d94 2023-01-10 thomas
851 62ee7d94 2023-01-10 thomas if (client->ncapabilities == 0)
852 62ee7d94 2023-01-10 thomas return 0;
853 62ee7d94 2023-01-10 thomas
854 62ee7d94 2023-01-10 thomas for (i = 0; i < client->ncapabilities; i++) {
855 62ee7d94 2023-01-10 thomas capa = &client->capabilities[i];
856 62ee7d94 2023-01-10 thomas if (strcmp(capa->key, capastr) == 0)
857 62ee7d94 2023-01-10 thomas return 1;
858 62ee7d94 2023-01-10 thomas }
859 62ee7d94 2023-01-10 thomas
860 62ee7d94 2023-01-10 thomas return 0;
861 62ee7d94 2023-01-10 thomas }
862 62ee7d94 2023-01-10 thomas
863 62ee7d94 2023-01-10 thomas static const struct got_error *
864 62ee7d94 2023-01-10 thomas recv_packfile(struct gotd_session_client *client)
865 62ee7d94 2023-01-10 thomas {
866 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
867 62ee7d94 2023-01-10 thomas struct gotd_imsg_recv_packfile ipack;
868 62ee7d94 2023-01-10 thomas struct gotd_imsg_packfile_pipe ipipe;
869 62ee7d94 2023-01-10 thomas struct gotd_imsg_packidx_file ifile;
870 62ee7d94 2023-01-10 thomas char *basepath = NULL, *pack_path = NULL, *idx_path = NULL;
871 62ee7d94 2023-01-10 thomas int packfd = -1, idxfd = -1;
872 62ee7d94 2023-01-10 thomas int pipe[2] = { -1, -1 };
873 62ee7d94 2023-01-10 thomas
874 62ee7d94 2023-01-10 thomas if (client->packfile_path) {
875 62ee7d94 2023-01-10 thomas return got_error_fmt(GOT_ERR_PRIVSEP_MSG,
876 62ee7d94 2023-01-10 thomas "uid %d already has a pack file", client->euid);
877 62ee7d94 2023-01-10 thomas }
878 62ee7d94 2023-01-10 thomas
879 62ee7d94 2023-01-10 thomas if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
880 62ee7d94 2023-01-10 thomas return got_error_from_errno("socketpair");
881 62ee7d94 2023-01-10 thomas
882 62ee7d94 2023-01-10 thomas memset(&ipipe, 0, sizeof(ipipe));
883 62ee7d94 2023-01-10 thomas ipipe.client_id = client->id;
884 62ee7d94 2023-01-10 thomas
885 62ee7d94 2023-01-10 thomas /* Send pack pipe end 0 to repo child process. */
886 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&client->repo_child_iev,
887 7fed8fa4 2023-06-22 thomas GOTD_IMSG_PACKFILE_PIPE, gotd_session.proc_id, pipe[0],
888 62ee7d94 2023-01-10 thomas &ipipe, sizeof(ipipe)) == -1) {
889 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose PACKFILE_PIPE");
890 62ee7d94 2023-01-10 thomas pipe[0] = -1;
891 62ee7d94 2023-01-10 thomas goto done;
892 62ee7d94 2023-01-10 thomas }
893 62ee7d94 2023-01-10 thomas pipe[0] = -1;
894 62ee7d94 2023-01-10 thomas
895 62ee7d94 2023-01-10 thomas /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
896 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&client->iev,
897 7fed8fa4 2023-06-22 thomas GOTD_IMSG_PACKFILE_PIPE, gotd_session.proc_id, pipe[1],
898 7fed8fa4 2023-06-22 thomas NULL, 0) == -1)
899 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose PACKFILE_PIPE");
900 62ee7d94 2023-01-10 thomas pipe[1] = -1;
901 62ee7d94 2023-01-10 thomas
902 62ee7d94 2023-01-10 thomas if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.pack",
903 62ee7d94 2023-01-10 thomas got_repo_get_path(gotd_session.repo), GOT_OBJECTS_PACK_DIR,
904 62ee7d94 2023-01-10 thomas client->euid) == -1) {
905 62ee7d94 2023-01-10 thomas err = got_error_from_errno("asprintf");
906 62ee7d94 2023-01-10 thomas goto done;
907 62ee7d94 2023-01-10 thomas }
908 62ee7d94 2023-01-10 thomas
909 62ee7d94 2023-01-10 thomas err = got_opentemp_named_fd(&pack_path, &packfd, basepath, "");
910 62ee7d94 2023-01-10 thomas if (err)
911 62ee7d94 2023-01-10 thomas goto done;
912 851a5b48 2023-02-03 thomas if (fchmod(packfd, GOT_DEFAULT_PACK_MODE) == -1) {
913 851a5b48 2023-02-03 thomas err = got_error_from_errno2("fchmod", pack_path);
914 851a5b48 2023-02-03 thomas goto done;
915 851a5b48 2023-02-03 thomas }
916 62ee7d94 2023-01-10 thomas
917 62ee7d94 2023-01-10 thomas free(basepath);
918 62ee7d94 2023-01-10 thomas if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.idx",
919 62ee7d94 2023-01-10 thomas got_repo_get_path(gotd_session.repo), GOT_OBJECTS_PACK_DIR,
920 62ee7d94 2023-01-10 thomas client->euid) == -1) {
921 62ee7d94 2023-01-10 thomas err = got_error_from_errno("asprintf");
922 62ee7d94 2023-01-10 thomas basepath = NULL;
923 62ee7d94 2023-01-10 thomas goto done;
924 62ee7d94 2023-01-10 thomas }
925 62ee7d94 2023-01-10 thomas err = got_opentemp_named_fd(&idx_path, &idxfd, basepath, "");
926 62ee7d94 2023-01-10 thomas if (err)
927 62ee7d94 2023-01-10 thomas goto done;
928 851a5b48 2023-02-03 thomas if (fchmod(idxfd, GOT_DEFAULT_PACK_MODE) == -1) {
929 851a5b48 2023-02-03 thomas err = got_error_from_errno2("fchmod", idx_path);
930 851a5b48 2023-02-03 thomas goto done;
931 851a5b48 2023-02-03 thomas }
932 62ee7d94 2023-01-10 thomas
933 62ee7d94 2023-01-10 thomas memset(&ifile, 0, sizeof(ifile));
934 62ee7d94 2023-01-10 thomas ifile.client_id = client->id;
935 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&client->repo_child_iev,
936 7fed8fa4 2023-06-22 thomas GOTD_IMSG_PACKIDX_FILE, gotd_session.proc_id,
937 62ee7d94 2023-01-10 thomas idxfd, &ifile, sizeof(ifile)) == -1) {
938 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose PACKIDX_FILE");
939 62ee7d94 2023-01-10 thomas idxfd = -1;
940 62ee7d94 2023-01-10 thomas goto done;
941 62ee7d94 2023-01-10 thomas }
942 62ee7d94 2023-01-10 thomas idxfd = -1;
943 62ee7d94 2023-01-10 thomas
944 62ee7d94 2023-01-10 thomas memset(&ipack, 0, sizeof(ipack));
945 62ee7d94 2023-01-10 thomas ipack.client_id = client->id;
946 62ee7d94 2023-01-10 thomas if (client_has_capability(client, GOT_CAPA_REPORT_STATUS))
947 62ee7d94 2023-01-10 thomas ipack.report_status = 1;
948 62ee7d94 2023-01-10 thomas
949 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&client->repo_child_iev,
950 7fed8fa4 2023-06-22 thomas GOTD_IMSG_RECV_PACKFILE, gotd_session.proc_id, packfd,
951 62ee7d94 2023-01-10 thomas &ipack, sizeof(ipack)) == -1) {
952 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose RECV_PACKFILE");
953 62ee7d94 2023-01-10 thomas packfd = -1;
954 62ee7d94 2023-01-10 thomas goto done;
955 62ee7d94 2023-01-10 thomas }
956 62ee7d94 2023-01-10 thomas packfd = -1;
957 62ee7d94 2023-01-10 thomas
958 62ee7d94 2023-01-10 thomas done:
959 62ee7d94 2023-01-10 thomas free(basepath);
960 62ee7d94 2023-01-10 thomas if (pipe[0] != -1 && close(pipe[0]) == -1 && err == NULL)
961 62ee7d94 2023-01-10 thomas err = got_error_from_errno("close");
962 62ee7d94 2023-01-10 thomas if (pipe[1] != -1 && close(pipe[1]) == -1 && err == NULL)
963 62ee7d94 2023-01-10 thomas err = got_error_from_errno("close");
964 62ee7d94 2023-01-10 thomas if (packfd != -1 && close(packfd) == -1 && err == NULL)
965 62ee7d94 2023-01-10 thomas err = got_error_from_errno("close");
966 62ee7d94 2023-01-10 thomas if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
967 62ee7d94 2023-01-10 thomas err = got_error_from_errno("close");
968 62ee7d94 2023-01-10 thomas if (err) {
969 62ee7d94 2023-01-10 thomas free(pack_path);
970 62ee7d94 2023-01-10 thomas free(idx_path);
971 62ee7d94 2023-01-10 thomas } else {
972 62ee7d94 2023-01-10 thomas client->packfile_path = pack_path;
973 62ee7d94 2023-01-10 thomas client->packidx_path = idx_path;
974 62ee7d94 2023-01-10 thomas }
975 62ee7d94 2023-01-10 thomas return err;
976 62ee7d94 2023-01-10 thomas }
977 62ee7d94 2023-01-10 thomas
978 62ee7d94 2023-01-10 thomas static const struct got_error *
979 62ee7d94 2023-01-10 thomas send_packfile(struct gotd_session_client *client)
980 62ee7d94 2023-01-10 thomas {
981 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
982 62ee7d94 2023-01-10 thomas struct gotd_imsg_send_packfile ipack;
983 62ee7d94 2023-01-10 thomas struct gotd_imsg_packfile_pipe ipipe;
984 62ee7d94 2023-01-10 thomas int pipe[2];
985 62ee7d94 2023-01-10 thomas
986 62ee7d94 2023-01-10 thomas if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
987 62ee7d94 2023-01-10 thomas return got_error_from_errno("socketpair");
988 62ee7d94 2023-01-10 thomas
989 62ee7d94 2023-01-10 thomas memset(&ipack, 0, sizeof(ipack));
990 62ee7d94 2023-01-10 thomas memset(&ipipe, 0, sizeof(ipipe));
991 62ee7d94 2023-01-10 thomas
992 62ee7d94 2023-01-10 thomas ipack.client_id = client->id;
993 62ee7d94 2023-01-10 thomas if (client_has_capability(client, GOT_CAPA_SIDE_BAND_64K))
994 62ee7d94 2023-01-10 thomas ipack.report_progress = 1;
995 62ee7d94 2023-01-10 thomas
996 62ee7d94 2023-01-10 thomas client->delta_cache_fd = got_opentempfd();
997 62ee7d94 2023-01-10 thomas if (client->delta_cache_fd == -1)
998 62ee7d94 2023-01-10 thomas return got_error_from_errno("got_opentempfd");
999 62ee7d94 2023-01-10 thomas
1000 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&client->repo_child_iev,
1001 62ee7d94 2023-01-10 thomas GOTD_IMSG_SEND_PACKFILE, PROC_GOTD, client->delta_cache_fd,
1002 62ee7d94 2023-01-10 thomas &ipack, sizeof(ipack)) == -1) {
1003 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose SEND_PACKFILE");
1004 62ee7d94 2023-01-10 thomas close(pipe[0]);
1005 62ee7d94 2023-01-10 thomas close(pipe[1]);
1006 62ee7d94 2023-01-10 thomas return err;
1007 62ee7d94 2023-01-10 thomas }
1008 62ee7d94 2023-01-10 thomas
1009 62ee7d94 2023-01-10 thomas ipipe.client_id = client->id;
1010 62ee7d94 2023-01-10 thomas
1011 62ee7d94 2023-01-10 thomas /* Send pack pipe end 0 to repo child process. */
1012 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&client->repo_child_iev,
1013 62ee7d94 2023-01-10 thomas GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD,
1014 62ee7d94 2023-01-10 thomas pipe[0], &ipipe, sizeof(ipipe)) == -1) {
1015 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose PACKFILE_PIPE");
1016 62ee7d94 2023-01-10 thomas close(pipe[1]);
1017 62ee7d94 2023-01-10 thomas return err;
1018 62ee7d94 2023-01-10 thomas }
1019 62ee7d94 2023-01-10 thomas
1020 62ee7d94 2023-01-10 thomas /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
1021 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&client->iev,
1022 62ee7d94 2023-01-10 thomas GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1], NULL, 0) == -1)
1023 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose PACKFILE_PIPE");
1024 62ee7d94 2023-01-10 thomas
1025 62ee7d94 2023-01-10 thomas return err;
1026 62ee7d94 2023-01-10 thomas }
1027 62ee7d94 2023-01-10 thomas
1028 62ee7d94 2023-01-10 thomas static void
1029 8cb46987 2023-02-07 thomas session_dispatch_client(int fd, short events, void *arg)
1030 62ee7d94 2023-01-10 thomas {
1031 62ee7d94 2023-01-10 thomas struct gotd_imsgev *iev = arg;
1032 62ee7d94 2023-01-10 thomas struct imsgbuf *ibuf = &iev->ibuf;
1033 62ee7d94 2023-01-10 thomas struct gotd_session_client *client = &gotd_session_client;
1034 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
1035 62ee7d94 2023-01-10 thomas struct imsg imsg;
1036 62ee7d94 2023-01-10 thomas ssize_t n;
1037 62ee7d94 2023-01-10 thomas
1038 62ee7d94 2023-01-10 thomas if (events & EV_WRITE) {
1039 62ee7d94 2023-01-10 thomas while (ibuf->w.queued) {
1040 62ee7d94 2023-01-10 thomas n = msgbuf_write(&ibuf->w);
1041 62ee7d94 2023-01-10 thomas if (n == -1 && errno == EPIPE) {
1042 62ee7d94 2023-01-10 thomas /*
1043 62ee7d94 2023-01-10 thomas * The client has closed its socket.
1044 62ee7d94 2023-01-10 thomas * This can happen when Git clients are
1045 62ee7d94 2023-01-10 thomas * done sending pack file data.
1046 62ee7d94 2023-01-10 thomas */
1047 62ee7d94 2023-01-10 thomas msgbuf_clear(&ibuf->w);
1048 62ee7d94 2023-01-10 thomas continue;
1049 62ee7d94 2023-01-10 thomas } else if (n == -1 && errno != EAGAIN) {
1050 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg_flush");
1051 62ee7d94 2023-01-10 thomas disconnect_on_error(client, err);
1052 62ee7d94 2023-01-10 thomas return;
1053 62ee7d94 2023-01-10 thomas }
1054 62ee7d94 2023-01-10 thomas if (n == 0) {
1055 62ee7d94 2023-01-10 thomas /* Connection closed. */
1056 62ee7d94 2023-01-10 thomas err = got_error(GOT_ERR_EOF);
1057 62ee7d94 2023-01-10 thomas disconnect_on_error(client, err);
1058 62ee7d94 2023-01-10 thomas return;
1059 62ee7d94 2023-01-10 thomas }
1060 c8b73ac1 2023-08-23 thomas }
1061 c8b73ac1 2023-08-23 thomas
1062 c8b73ac1 2023-08-23 thomas if (client->flush_disconnect) {
1063 c8b73ac1 2023-08-23 thomas disconnect(client);
1064 c8b73ac1 2023-08-23 thomas return;
1065 62ee7d94 2023-01-10 thomas }
1066 62ee7d94 2023-01-10 thomas }
1067 62ee7d94 2023-01-10 thomas
1068 62ee7d94 2023-01-10 thomas if ((events & EV_READ) == 0)
1069 62ee7d94 2023-01-10 thomas return;
1070 62ee7d94 2023-01-10 thomas
1071 62ee7d94 2023-01-10 thomas memset(&imsg, 0, sizeof(imsg));
1072 62ee7d94 2023-01-10 thomas
1073 62ee7d94 2023-01-10 thomas while (err == NULL) {
1074 62ee7d94 2023-01-10 thomas err = gotd_imsg_recv(&imsg, ibuf, 0);
1075 62ee7d94 2023-01-10 thomas if (err) {
1076 62ee7d94 2023-01-10 thomas if (err->code == GOT_ERR_PRIVSEP_READ)
1077 62ee7d94 2023-01-10 thomas err = NULL;
1078 8e92c55c 2023-06-08 thomas else if (err->code == GOT_ERR_EOF &&
1079 8e92c55c 2023-06-08 thomas client->state == GOTD_STATE_EXPECT_CAPABILITIES) {
1080 8e92c55c 2023-06-08 thomas /*
1081 8e92c55c 2023-06-08 thomas * The client has closed its socket before
1082 8e92c55c 2023-06-08 thomas * sending its capability announcement.
1083 8e92c55c 2023-06-08 thomas * This can happen when Git clients have
1084 8e92c55c 2023-06-08 thomas * no ref-updates to send.
1085 8e92c55c 2023-06-08 thomas */
1086 8e92c55c 2023-06-08 thomas disconnect_on_error(client, err);
1087 8e92c55c 2023-06-08 thomas return;
1088 8e92c55c 2023-06-08 thomas }
1089 62ee7d94 2023-01-10 thomas break;
1090 62ee7d94 2023-01-10 thomas }
1091 62ee7d94 2023-01-10 thomas
1092 62ee7d94 2023-01-10 thomas evtimer_del(&client->tmo);
1093 62ee7d94 2023-01-10 thomas
1094 62ee7d94 2023-01-10 thomas switch (imsg.hdr.type) {
1095 62ee7d94 2023-01-10 thomas case GOTD_IMSG_CAPABILITIES:
1096 62ee7d94 2023-01-10 thomas if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1097 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1098 62ee7d94 2023-01-10 thomas "unexpected capabilities received");
1099 62ee7d94 2023-01-10 thomas break;
1100 62ee7d94 2023-01-10 thomas }
1101 62ee7d94 2023-01-10 thomas log_debug("receiving capabilities from uid %d",
1102 62ee7d94 2023-01-10 thomas client->euid);
1103 62ee7d94 2023-01-10 thomas err = recv_capabilities(client, &imsg);
1104 62ee7d94 2023-01-10 thomas break;
1105 62ee7d94 2023-01-10 thomas case GOTD_IMSG_CAPABILITY:
1106 62ee7d94 2023-01-10 thomas if (client->state != GOTD_STATE_EXPECT_CAPABILITIES) {
1107 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1108 62ee7d94 2023-01-10 thomas "unexpected capability received");
1109 62ee7d94 2023-01-10 thomas break;
1110 62ee7d94 2023-01-10 thomas }
1111 62ee7d94 2023-01-10 thomas err = recv_capability(client, &imsg);
1112 62ee7d94 2023-01-10 thomas if (err || client->ncapabilities < client->ncapa_alloc)
1113 62ee7d94 2023-01-10 thomas break;
1114 62ee7d94 2023-01-10 thomas if (!client->is_writing) {
1115 62ee7d94 2023-01-10 thomas client->state = GOTD_STATE_EXPECT_WANT;
1116 98c7fd82 2023-01-23 thomas client->accept_flush_pkt = 1;
1117 62ee7d94 2023-01-10 thomas log_debug("uid %d: expecting want-lines",
1118 62ee7d94 2023-01-10 thomas client->euid);
1119 62ee7d94 2023-01-10 thomas } else if (client->is_writing) {
1120 62ee7d94 2023-01-10 thomas client->state = GOTD_STATE_EXPECT_REF_UPDATE;
1121 98c7fd82 2023-01-23 thomas client->accept_flush_pkt = 1;
1122 62ee7d94 2023-01-10 thomas log_debug("uid %d: expecting ref-update-lines",
1123 62ee7d94 2023-01-10 thomas client->euid);
1124 62ee7d94 2023-01-10 thomas } else
1125 62ee7d94 2023-01-10 thomas fatalx("client %d is both reading and writing",
1126 62ee7d94 2023-01-10 thomas client->euid);
1127 62ee7d94 2023-01-10 thomas break;
1128 62ee7d94 2023-01-10 thomas case GOTD_IMSG_WANT:
1129 62ee7d94 2023-01-10 thomas if (client->state != GOTD_STATE_EXPECT_WANT) {
1130 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1131 62ee7d94 2023-01-10 thomas "unexpected want-line received");
1132 62ee7d94 2023-01-10 thomas break;
1133 62ee7d94 2023-01-10 thomas }
1134 62ee7d94 2023-01-10 thomas log_debug("received want-line from uid %d",
1135 62ee7d94 2023-01-10 thomas client->euid);
1136 62ee7d94 2023-01-10 thomas err = ensure_client_is_reading(client);
1137 62ee7d94 2023-01-10 thomas if (err)
1138 62ee7d94 2023-01-10 thomas break;
1139 98c7fd82 2023-01-23 thomas client->accept_flush_pkt = 1;
1140 62ee7d94 2023-01-10 thomas err = forward_want(client, &imsg);
1141 62ee7d94 2023-01-10 thomas break;
1142 62ee7d94 2023-01-10 thomas case GOTD_IMSG_REF_UPDATE:
1143 0445d8ec 2023-01-19 thomas if (client->state != GOTD_STATE_EXPECT_REF_UPDATE &&
1144 0445d8ec 2023-01-19 thomas client->state !=
1145 0445d8ec 2023-01-19 thomas GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1146 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1147 62ee7d94 2023-01-10 thomas "unexpected ref-update-line received");
1148 62ee7d94 2023-01-10 thomas break;
1149 62ee7d94 2023-01-10 thomas }
1150 62ee7d94 2023-01-10 thomas log_debug("received ref-update-line from uid %d",
1151 62ee7d94 2023-01-10 thomas client->euid);
1152 62ee7d94 2023-01-10 thomas err = ensure_client_is_writing(client);
1153 62ee7d94 2023-01-10 thomas if (err)
1154 62ee7d94 2023-01-10 thomas break;
1155 62ee7d94 2023-01-10 thomas err = forward_ref_update(client, &imsg);
1156 62ee7d94 2023-01-10 thomas if (err)
1157 62ee7d94 2023-01-10 thomas break;
1158 62ee7d94 2023-01-10 thomas client->state = GOTD_STATE_EXPECT_MORE_REF_UPDATES;
1159 98c7fd82 2023-01-23 thomas client->accept_flush_pkt = 1;
1160 62ee7d94 2023-01-10 thomas break;
1161 62ee7d94 2023-01-10 thomas case GOTD_IMSG_HAVE:
1162 62ee7d94 2023-01-10 thomas if (client->state != GOTD_STATE_EXPECT_HAVE) {
1163 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1164 62ee7d94 2023-01-10 thomas "unexpected have-line received");
1165 62ee7d94 2023-01-10 thomas break;
1166 62ee7d94 2023-01-10 thomas }
1167 62ee7d94 2023-01-10 thomas log_debug("received have-line from uid %d",
1168 62ee7d94 2023-01-10 thomas client->euid);
1169 62ee7d94 2023-01-10 thomas err = ensure_client_is_reading(client);
1170 62ee7d94 2023-01-10 thomas if (err)
1171 62ee7d94 2023-01-10 thomas break;
1172 62ee7d94 2023-01-10 thomas err = forward_have(client, &imsg);
1173 62ee7d94 2023-01-10 thomas if (err)
1174 62ee7d94 2023-01-10 thomas break;
1175 98c7fd82 2023-01-23 thomas client->accept_flush_pkt = 1;
1176 62ee7d94 2023-01-10 thomas break;
1177 62ee7d94 2023-01-10 thomas case GOTD_IMSG_FLUSH:
1178 62ee7d94 2023-01-10 thomas if (client->state == GOTD_STATE_EXPECT_WANT ||
1179 62ee7d94 2023-01-10 thomas client->state == GOTD_STATE_EXPECT_HAVE) {
1180 62ee7d94 2023-01-10 thomas err = ensure_client_is_reading(client);
1181 62ee7d94 2023-01-10 thomas if (err)
1182 62ee7d94 2023-01-10 thomas break;
1183 62ee7d94 2023-01-10 thomas } else if (client->state ==
1184 62ee7d94 2023-01-10 thomas GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1185 62ee7d94 2023-01-10 thomas err = ensure_client_is_writing(client);
1186 62ee7d94 2023-01-10 thomas if (err)
1187 62ee7d94 2023-01-10 thomas break;
1188 6110f5ef 2023-01-19 thomas } else if (client->state != GOTD_STATE_EXPECT_DONE) {
1189 98c7fd82 2023-01-23 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1190 98c7fd82 2023-01-23 thomas "unexpected flush-pkt received");
1191 98c7fd82 2023-01-23 thomas break;
1192 98c7fd82 2023-01-23 thomas }
1193 98c7fd82 2023-01-23 thomas if (!client->accept_flush_pkt) {
1194 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1195 62ee7d94 2023-01-10 thomas "unexpected flush-pkt received");
1196 62ee7d94 2023-01-10 thomas break;
1197 62ee7d94 2023-01-10 thomas }
1198 98c7fd82 2023-01-23 thomas
1199 98c7fd82 2023-01-23 thomas /*
1200 98c7fd82 2023-01-23 thomas * Accept just one flush packet at a time.
1201 98c7fd82 2023-01-23 thomas * Future client state transitions will set this flag
1202 98c7fd82 2023-01-23 thomas * again if another flush packet is expected.
1203 98c7fd82 2023-01-23 thomas */
1204 98c7fd82 2023-01-23 thomas client->accept_flush_pkt = 0;
1205 98c7fd82 2023-01-23 thomas
1206 62ee7d94 2023-01-10 thomas log_debug("received flush-pkt from uid %d",
1207 62ee7d94 2023-01-10 thomas client->euid);
1208 62ee7d94 2023-01-10 thomas if (client->state == GOTD_STATE_EXPECT_WANT) {
1209 62ee7d94 2023-01-10 thomas client->state = GOTD_STATE_EXPECT_HAVE;
1210 62ee7d94 2023-01-10 thomas log_debug("uid %d: expecting have-lines",
1211 62ee7d94 2023-01-10 thomas client->euid);
1212 62ee7d94 2023-01-10 thomas } else if (client->state == GOTD_STATE_EXPECT_HAVE) {
1213 62ee7d94 2023-01-10 thomas client->state = GOTD_STATE_EXPECT_DONE;
1214 98c7fd82 2023-01-23 thomas client->accept_flush_pkt = 1;
1215 62ee7d94 2023-01-10 thomas log_debug("uid %d: expecting 'done'",
1216 62ee7d94 2023-01-10 thomas client->euid);
1217 62ee7d94 2023-01-10 thomas } else if (client->state ==
1218 62ee7d94 2023-01-10 thomas GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1219 62ee7d94 2023-01-10 thomas client->state = GOTD_STATE_EXPECT_PACKFILE;
1220 62ee7d94 2023-01-10 thomas log_debug("uid %d: expecting packfile",
1221 62ee7d94 2023-01-10 thomas client->euid);
1222 62ee7d94 2023-01-10 thomas err = recv_packfile(client);
1223 6110f5ef 2023-01-19 thomas } else if (client->state != GOTD_STATE_EXPECT_DONE) {
1224 62ee7d94 2023-01-10 thomas /* should not happen, see above */
1225 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1226 62ee7d94 2023-01-10 thomas "unexpected client state");
1227 62ee7d94 2023-01-10 thomas break;
1228 62ee7d94 2023-01-10 thomas }
1229 62ee7d94 2023-01-10 thomas break;
1230 62ee7d94 2023-01-10 thomas case GOTD_IMSG_DONE:
1231 62ee7d94 2023-01-10 thomas if (client->state != GOTD_STATE_EXPECT_HAVE &&
1232 62ee7d94 2023-01-10 thomas client->state != GOTD_STATE_EXPECT_DONE) {
1233 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1234 62ee7d94 2023-01-10 thomas "unexpected flush-pkt received");
1235 62ee7d94 2023-01-10 thomas break;
1236 62ee7d94 2023-01-10 thomas }
1237 62ee7d94 2023-01-10 thomas log_debug("received 'done' from uid %d", client->euid);
1238 62ee7d94 2023-01-10 thomas err = ensure_client_is_reading(client);
1239 62ee7d94 2023-01-10 thomas if (err)
1240 62ee7d94 2023-01-10 thomas break;
1241 62ee7d94 2023-01-10 thomas client->state = GOTD_STATE_DONE;
1242 98c7fd82 2023-01-23 thomas client->accept_flush_pkt = 1;
1243 62ee7d94 2023-01-10 thomas err = send_packfile(client);
1244 62ee7d94 2023-01-10 thomas break;
1245 62ee7d94 2023-01-10 thomas default:
1246 62ee7d94 2023-01-10 thomas log_debug("unexpected imsg %d", imsg.hdr.type);
1247 62ee7d94 2023-01-10 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
1248 62ee7d94 2023-01-10 thomas break;
1249 62ee7d94 2023-01-10 thomas }
1250 62ee7d94 2023-01-10 thomas
1251 62ee7d94 2023-01-10 thomas imsg_free(&imsg);
1252 62ee7d94 2023-01-10 thomas }
1253 62ee7d94 2023-01-10 thomas
1254 62ee7d94 2023-01-10 thomas if (err) {
1255 62ee7d94 2023-01-10 thomas if (err->code != GOT_ERR_EOF ||
1256 62ee7d94 2023-01-10 thomas client->state != GOTD_STATE_EXPECT_PACKFILE)
1257 62ee7d94 2023-01-10 thomas disconnect_on_error(client, err);
1258 62ee7d94 2023-01-10 thomas } else {
1259 62ee7d94 2023-01-10 thomas gotd_imsg_event_add(iev);
1260 62ee7d94 2023-01-10 thomas evtimer_add(&client->tmo, &gotd_session.request_timeout);
1261 62ee7d94 2023-01-10 thomas }
1262 62ee7d94 2023-01-10 thomas }
1263 62ee7d94 2023-01-10 thomas
1264 62ee7d94 2023-01-10 thomas static const struct got_error *
1265 62ee7d94 2023-01-10 thomas list_refs_request(void)
1266 62ee7d94 2023-01-10 thomas {
1267 62ee7d94 2023-01-10 thomas static const struct got_error *err;
1268 62ee7d94 2023-01-10 thomas struct gotd_session_client *client = &gotd_session_client;
1269 62ee7d94 2023-01-10 thomas struct gotd_imsgev *iev = &client->repo_child_iev;
1270 62ee7d94 2023-01-10 thomas struct gotd_imsg_list_refs_internal ilref;
1271 62ee7d94 2023-01-10 thomas int fd;
1272 62ee7d94 2023-01-10 thomas
1273 62ee7d94 2023-01-10 thomas if (client->state != GOTD_STATE_EXPECT_LIST_REFS)
1274 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
1275 62ee7d94 2023-01-10 thomas
1276 62ee7d94 2023-01-10 thomas memset(&ilref, 0, sizeof(ilref));
1277 62ee7d94 2023-01-10 thomas ilref.client_id = client->id;
1278 62ee7d94 2023-01-10 thomas
1279 62ee7d94 2023-01-10 thomas fd = dup(client->fd);
1280 62ee7d94 2023-01-10 thomas if (fd == -1)
1281 62ee7d94 2023-01-10 thomas return got_error_from_errno("dup");
1282 62ee7d94 2023-01-10 thomas
1283 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(iev, GOTD_IMSG_LIST_REFS_INTERNAL,
1284 7fed8fa4 2023-06-22 thomas gotd_session.proc_id, fd, &ilref, sizeof(ilref)) == -1) {
1285 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose LIST_REFS_INTERNAL");
1286 62ee7d94 2023-01-10 thomas close(fd);
1287 62ee7d94 2023-01-10 thomas return err;
1288 62ee7d94 2023-01-10 thomas }
1289 62ee7d94 2023-01-10 thomas
1290 62ee7d94 2023-01-10 thomas client->state = GOTD_STATE_EXPECT_CAPABILITIES;
1291 62ee7d94 2023-01-10 thomas log_debug("uid %d: expecting capabilities", client->euid);
1292 62ee7d94 2023-01-10 thomas return NULL;
1293 62ee7d94 2023-01-10 thomas }
1294 62ee7d94 2023-01-10 thomas
1295 62ee7d94 2023-01-10 thomas static const struct got_error *
1296 62ee7d94 2023-01-10 thomas recv_connect(struct imsg *imsg)
1297 62ee7d94 2023-01-10 thomas {
1298 62ee7d94 2023-01-10 thomas struct gotd_session_client *client = &gotd_session_client;
1299 62ee7d94 2023-01-10 thomas struct gotd_imsg_connect iconnect;
1300 62ee7d94 2023-01-10 thomas size_t datalen;
1301 62ee7d94 2023-01-10 thomas
1302 62ee7d94 2023-01-10 thomas if (client->state != GOTD_STATE_EXPECT_LIST_REFS)
1303 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
1304 62ee7d94 2023-01-10 thomas
1305 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1306 62ee7d94 2023-01-10 thomas if (datalen != sizeof(iconnect))
1307 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1308 62ee7d94 2023-01-10 thomas memcpy(&iconnect, imsg->data, sizeof(iconnect));
1309 62ee7d94 2023-01-10 thomas
1310 62ee7d94 2023-01-10 thomas if (imsg->fd == -1)
1311 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
1312 62ee7d94 2023-01-10 thomas
1313 62ee7d94 2023-01-10 thomas client->fd = imsg->fd;
1314 62ee7d94 2023-01-10 thomas client->euid = iconnect.euid;
1315 62ee7d94 2023-01-10 thomas client->egid = iconnect.egid;
1316 62ee7d94 2023-01-10 thomas
1317 62ee7d94 2023-01-10 thomas imsg_init(&client->iev.ibuf, client->fd);
1318 8cb46987 2023-02-07 thomas client->iev.handler = session_dispatch_client;
1319 62ee7d94 2023-01-10 thomas client->iev.events = EV_READ;
1320 62ee7d94 2023-01-10 thomas client->iev.handler_arg = NULL;
1321 62ee7d94 2023-01-10 thomas event_set(&client->iev.ev, client->iev.ibuf.fd, EV_READ,
1322 8cb46987 2023-02-07 thomas session_dispatch_client, &client->iev);
1323 62ee7d94 2023-01-10 thomas gotd_imsg_event_add(&client->iev);
1324 62ee7d94 2023-01-10 thomas evtimer_set(&client->tmo, gotd_request_timeout, client);
1325 62ee7d94 2023-01-10 thomas
1326 62ee7d94 2023-01-10 thomas return NULL;
1327 62ee7d94 2023-01-10 thomas }
1328 62ee7d94 2023-01-10 thomas
1329 62ee7d94 2023-01-10 thomas static const struct got_error *
1330 62ee7d94 2023-01-10 thomas recv_repo_child(struct imsg *imsg)
1331 62ee7d94 2023-01-10 thomas {
1332 62ee7d94 2023-01-10 thomas struct gotd_imsg_connect_repo_child ichild;
1333 62ee7d94 2023-01-10 thomas struct gotd_session_client *client = &gotd_session_client;
1334 62ee7d94 2023-01-10 thomas size_t datalen;
1335 62ee7d94 2023-01-10 thomas
1336 62ee7d94 2023-01-10 thomas if (client->state != GOTD_STATE_EXPECT_LIST_REFS)
1337 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
1338 62ee7d94 2023-01-10 thomas
1339 62ee7d94 2023-01-10 thomas /* We should already have received a pipe to the listener. */
1340 62ee7d94 2023-01-10 thomas if (client->fd == -1)
1341 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
1342 62ee7d94 2023-01-10 thomas
1343 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1344 62ee7d94 2023-01-10 thomas if (datalen != sizeof(ichild))
1345 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1346 62ee7d94 2023-01-10 thomas
1347 62ee7d94 2023-01-10 thomas memcpy(&ichild, imsg->data, sizeof(ichild));
1348 62ee7d94 2023-01-10 thomas
1349 62ee7d94 2023-01-10 thomas client->id = ichild.client_id;
1350 62ee7d94 2023-01-10 thomas if (ichild.proc_id == PROC_REPO_WRITE)
1351 62ee7d94 2023-01-10 thomas client->is_writing = 1;
1352 62ee7d94 2023-01-10 thomas else if (ichild.proc_id == PROC_REPO_READ)
1353 62ee7d94 2023-01-10 thomas client->is_writing = 0;
1354 62ee7d94 2023-01-10 thomas else
1355 62ee7d94 2023-01-10 thomas return got_error_msg(GOT_ERR_PRIVSEP_MSG,
1356 62ee7d94 2023-01-10 thomas "bad child process type");
1357 d4628c48 2023-02-03 thomas
1358 62ee7d94 2023-01-10 thomas if (imsg->fd == -1)
1359 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
1360 62ee7d94 2023-01-10 thomas
1361 62ee7d94 2023-01-10 thomas imsg_init(&client->repo_child_iev.ibuf, imsg->fd);
1362 62ee7d94 2023-01-10 thomas client->repo_child_iev.handler = session_dispatch_repo_child;
1363 62ee7d94 2023-01-10 thomas client->repo_child_iev.events = EV_READ;
1364 62ee7d94 2023-01-10 thomas client->repo_child_iev.handler_arg = NULL;
1365 62ee7d94 2023-01-10 thomas event_set(&client->repo_child_iev.ev, client->repo_child_iev.ibuf.fd,
1366 62ee7d94 2023-01-10 thomas EV_READ, session_dispatch_repo_child, &client->repo_child_iev);
1367 62ee7d94 2023-01-10 thomas gotd_imsg_event_add(&client->repo_child_iev);
1368 62ee7d94 2023-01-10 thomas
1369 62ee7d94 2023-01-10 thomas /* The "recvfd" pledge promise is no longer needed. */
1370 62ee7d94 2023-01-10 thomas if (pledge("stdio rpath wpath cpath sendfd fattr flock", NULL) == -1)
1371 62ee7d94 2023-01-10 thomas fatal("pledge");
1372 62ee7d94 2023-01-10 thomas
1373 62ee7d94 2023-01-10 thomas return NULL;
1374 62ee7d94 2023-01-10 thomas }
1375 62ee7d94 2023-01-10 thomas
1376 62ee7d94 2023-01-10 thomas static void
1377 62ee7d94 2023-01-10 thomas session_dispatch(int fd, short event, void *arg)
1378 62ee7d94 2023-01-10 thomas {
1379 62ee7d94 2023-01-10 thomas struct gotd_imsgev *iev = arg;
1380 62ee7d94 2023-01-10 thomas struct imsgbuf *ibuf = &iev->ibuf;
1381 62ee7d94 2023-01-10 thomas struct gotd_session_client *client = &gotd_session_client;
1382 62ee7d94 2023-01-10 thomas ssize_t n;
1383 62ee7d94 2023-01-10 thomas int shut = 0;
1384 62ee7d94 2023-01-10 thomas struct imsg imsg;
1385 62ee7d94 2023-01-10 thomas
1386 62ee7d94 2023-01-10 thomas if (event & EV_READ) {
1387 62ee7d94 2023-01-10 thomas if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1388 62ee7d94 2023-01-10 thomas fatal("imsg_read error");
1389 62ee7d94 2023-01-10 thomas if (n == 0) {
1390 62ee7d94 2023-01-10 thomas /* Connection closed. */
1391 62ee7d94 2023-01-10 thomas shut = 1;
1392 62ee7d94 2023-01-10 thomas goto done;
1393 62ee7d94 2023-01-10 thomas }
1394 62ee7d94 2023-01-10 thomas }
1395 62ee7d94 2023-01-10 thomas
1396 62ee7d94 2023-01-10 thomas if (event & EV_WRITE) {
1397 62ee7d94 2023-01-10 thomas n = msgbuf_write(&ibuf->w);
1398 62ee7d94 2023-01-10 thomas if (n == -1 && errno != EAGAIN)
1399 62ee7d94 2023-01-10 thomas fatal("msgbuf_write");
1400 62ee7d94 2023-01-10 thomas if (n == 0) {
1401 62ee7d94 2023-01-10 thomas /* Connection closed. */
1402 62ee7d94 2023-01-10 thomas shut = 1;
1403 62ee7d94 2023-01-10 thomas goto done;
1404 62ee7d94 2023-01-10 thomas }
1405 62ee7d94 2023-01-10 thomas }
1406 62ee7d94 2023-01-10 thomas
1407 62ee7d94 2023-01-10 thomas for (;;) {
1408 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
1409 62ee7d94 2023-01-10 thomas uint32_t client_id = 0;
1410 62ee7d94 2023-01-10 thomas int do_disconnect = 0, do_list_refs = 0;
1411 62ee7d94 2023-01-10 thomas
1412 62ee7d94 2023-01-10 thomas if ((n = imsg_get(ibuf, &imsg)) == -1)
1413 62ee7d94 2023-01-10 thomas fatal("%s: imsg_get error", __func__);
1414 62ee7d94 2023-01-10 thomas if (n == 0) /* No more messages. */
1415 62ee7d94 2023-01-10 thomas break;
1416 62ee7d94 2023-01-10 thomas
1417 62ee7d94 2023-01-10 thomas switch (imsg.hdr.type) {
1418 62ee7d94 2023-01-10 thomas case GOTD_IMSG_ERROR:
1419 62ee7d94 2023-01-10 thomas do_disconnect = 1;
1420 62ee7d94 2023-01-10 thomas err = gotd_imsg_recv_error(&client_id, &imsg);
1421 62ee7d94 2023-01-10 thomas break;
1422 62ee7d94 2023-01-10 thomas case GOTD_IMSG_CONNECT:
1423 62ee7d94 2023-01-10 thomas err = recv_connect(&imsg);
1424 62ee7d94 2023-01-10 thomas break;
1425 62ee7d94 2023-01-10 thomas case GOTD_IMSG_DISCONNECT:
1426 62ee7d94 2023-01-10 thomas do_disconnect = 1;
1427 62ee7d94 2023-01-10 thomas break;
1428 62ee7d94 2023-01-10 thomas case GOTD_IMSG_CONNECT_REPO_CHILD:
1429 62ee7d94 2023-01-10 thomas err = recv_repo_child(&imsg);
1430 62ee7d94 2023-01-10 thomas if (err)
1431 62ee7d94 2023-01-10 thomas break;
1432 62ee7d94 2023-01-10 thomas do_list_refs = 1;
1433 62ee7d94 2023-01-10 thomas break;
1434 62ee7d94 2023-01-10 thomas default:
1435 62ee7d94 2023-01-10 thomas log_debug("unexpected imsg %d", imsg.hdr.type);
1436 62ee7d94 2023-01-10 thomas break;
1437 62ee7d94 2023-01-10 thomas }
1438 62ee7d94 2023-01-10 thomas imsg_free(&imsg);
1439 62ee7d94 2023-01-10 thomas
1440 62ee7d94 2023-01-10 thomas if (do_disconnect) {
1441 62ee7d94 2023-01-10 thomas if (err)
1442 62ee7d94 2023-01-10 thomas disconnect_on_error(client, err);
1443 62ee7d94 2023-01-10 thomas else
1444 62ee7d94 2023-01-10 thomas disconnect(client);
1445 62ee7d94 2023-01-10 thomas } else if (do_list_refs)
1446 62ee7d94 2023-01-10 thomas err = list_refs_request();
1447 62ee7d94 2023-01-10 thomas
1448 62ee7d94 2023-01-10 thomas if (err)
1449 62ee7d94 2023-01-10 thomas log_warnx("uid %d: %s", client->euid, err->msg);
1450 62ee7d94 2023-01-10 thomas }
1451 62ee7d94 2023-01-10 thomas done:
1452 62ee7d94 2023-01-10 thomas if (!shut) {
1453 62ee7d94 2023-01-10 thomas gotd_imsg_event_add(iev);
1454 62ee7d94 2023-01-10 thomas } else {
1455 62ee7d94 2023-01-10 thomas /* This pipe is dead. Remove its event handler */
1456 62ee7d94 2023-01-10 thomas event_del(&iev->ev);
1457 62ee7d94 2023-01-10 thomas event_loopexit(NULL);
1458 d4628c48 2023-02-03 thomas }
1459 62ee7d94 2023-01-10 thomas }
1460 62ee7d94 2023-01-10 thomas
1461 62ee7d94 2023-01-10 thomas void
1462 62ee7d94 2023-01-10 thomas session_main(const char *title, const char *repo_path,
1463 7fed8fa4 2023-06-22 thomas int *pack_fds, int *temp_fds, struct timeval *request_timeout,
1464 7fed8fa4 2023-06-22 thomas enum gotd_procid proc_id)
1465 62ee7d94 2023-01-10 thomas {
1466 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
1467 62ee7d94 2023-01-10 thomas struct event evsigint, evsigterm, evsighup, evsigusr1;
1468 62ee7d94 2023-01-10 thomas
1469 62ee7d94 2023-01-10 thomas gotd_session.title = title;
1470 62ee7d94 2023-01-10 thomas gotd_session.pid = getpid();
1471 62ee7d94 2023-01-10 thomas gotd_session.pack_fds = pack_fds;
1472 62ee7d94 2023-01-10 thomas gotd_session.temp_fds = temp_fds;
1473 62ee7d94 2023-01-10 thomas memcpy(&gotd_session.request_timeout, request_timeout,
1474 62ee7d94 2023-01-10 thomas sizeof(gotd_session.request_timeout));
1475 7fed8fa4 2023-06-22 thomas gotd_session.proc_id = proc_id;
1476 62ee7d94 2023-01-10 thomas
1477 62ee7d94 2023-01-10 thomas err = got_repo_open(&gotd_session.repo, repo_path, NULL, pack_fds);
1478 62ee7d94 2023-01-10 thomas if (err)
1479 62ee7d94 2023-01-10 thomas goto done;
1480 62ee7d94 2023-01-10 thomas if (!got_repo_is_bare(gotd_session.repo)) {
1481 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1482 62ee7d94 2023-01-10 thomas "bare git repository required");
1483 62ee7d94 2023-01-10 thomas goto done;
1484 62ee7d94 2023-01-10 thomas }
1485 62ee7d94 2023-01-10 thomas
1486 62ee7d94 2023-01-10 thomas got_repo_temp_fds_set(gotd_session.repo, temp_fds);
1487 62ee7d94 2023-01-10 thomas
1488 62ee7d94 2023-01-10 thomas signal_set(&evsigint, SIGINT, gotd_session_sighdlr, NULL);
1489 62ee7d94 2023-01-10 thomas signal_set(&evsigterm, SIGTERM, gotd_session_sighdlr, NULL);
1490 62ee7d94 2023-01-10 thomas signal_set(&evsighup, SIGHUP, gotd_session_sighdlr, NULL);
1491 62ee7d94 2023-01-10 thomas signal_set(&evsigusr1, SIGUSR1, gotd_session_sighdlr, NULL);
1492 62ee7d94 2023-01-10 thomas signal(SIGPIPE, SIG_IGN);
1493 62ee7d94 2023-01-10 thomas
1494 62ee7d94 2023-01-10 thomas signal_add(&evsigint, NULL);
1495 62ee7d94 2023-01-10 thomas signal_add(&evsigterm, NULL);
1496 62ee7d94 2023-01-10 thomas signal_add(&evsighup, NULL);
1497 62ee7d94 2023-01-10 thomas signal_add(&evsigusr1, NULL);
1498 62ee7d94 2023-01-10 thomas
1499 62ee7d94 2023-01-10 thomas gotd_session_client.state = GOTD_STATE_EXPECT_LIST_REFS;
1500 62ee7d94 2023-01-10 thomas gotd_session_client.fd = -1;
1501 62ee7d94 2023-01-10 thomas gotd_session_client.nref_updates = -1;
1502 a6f25078 2023-01-10 thomas gotd_session_client.delta_cache_fd = -1;
1503 98c7fd82 2023-01-23 thomas gotd_session_client.accept_flush_pkt = 1;
1504 62ee7d94 2023-01-10 thomas
1505 62ee7d94 2023-01-10 thomas imsg_init(&gotd_session.parent_iev.ibuf, GOTD_FILENO_MSG_PIPE);
1506 62ee7d94 2023-01-10 thomas gotd_session.parent_iev.handler = session_dispatch;
1507 62ee7d94 2023-01-10 thomas gotd_session.parent_iev.events = EV_READ;
1508 62ee7d94 2023-01-10 thomas gotd_session.parent_iev.handler_arg = NULL;
1509 62ee7d94 2023-01-10 thomas event_set(&gotd_session.parent_iev.ev, gotd_session.parent_iev.ibuf.fd,
1510 62ee7d94 2023-01-10 thomas EV_READ, session_dispatch, &gotd_session.parent_iev);
1511 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&gotd_session.parent_iev,
1512 7fed8fa4 2023-06-22 thomas GOTD_IMSG_CLIENT_SESSION_READY, gotd_session.proc_id,
1513 7fed8fa4 2023-06-22 thomas -1, NULL, 0) == -1) {
1514 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose CLIENT_SESSION_READY");
1515 62ee7d94 2023-01-10 thomas goto done;
1516 62ee7d94 2023-01-10 thomas }
1517 62ee7d94 2023-01-10 thomas
1518 62ee7d94 2023-01-10 thomas event_dispatch();
1519 62ee7d94 2023-01-10 thomas done:
1520 62ee7d94 2023-01-10 thomas if (err)
1521 62ee7d94 2023-01-10 thomas log_warnx("%s: %s", title, err->msg);
1522 62ee7d94 2023-01-10 thomas gotd_session_shutdown();
1523 62ee7d94 2023-01-10 thomas }
1524 62ee7d94 2023-01-10 thomas
1525 62ee7d94 2023-01-10 thomas void
1526 62ee7d94 2023-01-10 thomas gotd_session_shutdown(void)
1527 62ee7d94 2023-01-10 thomas {
1528 5330ab76 2023-02-17 thomas log_debug("shutting down");
1529 62ee7d94 2023-01-10 thomas if (gotd_session.repo)
1530 62ee7d94 2023-01-10 thomas got_repo_close(gotd_session.repo);
1531 62ee7d94 2023-01-10 thomas got_repo_pack_fds_close(gotd_session.pack_fds);
1532 62ee7d94 2023-01-10 thomas got_repo_temp_fds_close(gotd_session.temp_fds);
1533 62ee7d94 2023-01-10 thomas exit(0);
1534 62ee7d94 2023-01-10 thomas }