Blame


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