Blame


1 e70fd952 2024-03-22 stsp /*
2 e70fd952 2024-03-22 stsp * Copyright (c) 2022, 2023 Stefan Sperling <stsp@openbsd.org>
3 e70fd952 2024-03-22 stsp *
4 e70fd952 2024-03-22 stsp * Permission to use, copy, modify, and distribute this software for any
5 e70fd952 2024-03-22 stsp * purpose with or without fee is hereby granted, provided that the above
6 e70fd952 2024-03-22 stsp * copyright notice and this permission notice appear in all copies.
7 e70fd952 2024-03-22 stsp *
8 e70fd952 2024-03-22 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 e70fd952 2024-03-22 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 e70fd952 2024-03-22 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 e70fd952 2024-03-22 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 e70fd952 2024-03-22 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 e70fd952 2024-03-22 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 e70fd952 2024-03-22 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 e70fd952 2024-03-22 stsp */
16 e70fd952 2024-03-22 stsp
17 e70fd952 2024-03-22 stsp #include <sys/types.h>
18 e70fd952 2024-03-22 stsp #include <sys/queue.h>
19 e70fd952 2024-03-22 stsp #include <sys/tree.h>
20 e70fd952 2024-03-22 stsp #include <sys/socket.h>
21 e70fd952 2024-03-22 stsp #include <sys/stat.h>
22 e70fd952 2024-03-22 stsp #include <sys/uio.h>
23 e70fd952 2024-03-22 stsp
24 e70fd952 2024-03-22 stsp #include <errno.h>
25 e70fd952 2024-03-22 stsp #include <event.h>
26 e70fd952 2024-03-22 stsp #include <limits.h>
27 e70fd952 2024-03-22 stsp #include <sha1.h>
28 e70fd952 2024-03-22 stsp #include <sha2.h>
29 e70fd952 2024-03-22 stsp #include <signal.h>
30 e70fd952 2024-03-22 stsp #include <stdint.h>
31 e70fd952 2024-03-22 stsp #include <stdio.h>
32 e70fd952 2024-03-22 stsp #include <stdlib.h>
33 e70fd952 2024-03-22 stsp #include <string.h>
34 e70fd952 2024-03-22 stsp #include <imsg.h>
35 e70fd952 2024-03-22 stsp #include <unistd.h>
36 e70fd952 2024-03-22 stsp
37 e70fd952 2024-03-22 stsp #include "got_error.h"
38 e70fd952 2024-03-22 stsp #include "got_repository.h"
39 e70fd952 2024-03-22 stsp #include "got_object.h"
40 e70fd952 2024-03-22 stsp #include "got_path.h"
41 e70fd952 2024-03-22 stsp #include "got_reference.h"
42 e70fd952 2024-03-22 stsp #include "got_opentemp.h"
43 e70fd952 2024-03-22 stsp
44 e70fd952 2024-03-22 stsp #include "got_lib_hash.h"
45 e70fd952 2024-03-22 stsp #include "got_lib_delta.h"
46 e70fd952 2024-03-22 stsp #include "got_lib_object.h"
47 e70fd952 2024-03-22 stsp #include "got_lib_object_cache.h"
48 e70fd952 2024-03-22 stsp #include "got_lib_pack.h"
49 e70fd952 2024-03-22 stsp #include "got_lib_repository.h"
50 e70fd952 2024-03-22 stsp #include "got_lib_gitproto.h"
51 e70fd952 2024-03-22 stsp
52 e70fd952 2024-03-22 stsp #include "gotd.h"
53 e70fd952 2024-03-22 stsp #include "log.h"
54 e70fd952 2024-03-22 stsp #include "session_read.h"
55 e70fd952 2024-03-22 stsp
56 e70fd952 2024-03-22 stsp enum gotd_session_read_state {
57 e70fd952 2024-03-22 stsp GOTD_STATE_EXPECT_LIST_REFS,
58 e70fd952 2024-03-22 stsp GOTD_STATE_EXPECT_CAPABILITIES,
59 e70fd952 2024-03-22 stsp GOTD_STATE_EXPECT_WANT,
60 c811fd62 2024-05-05 stsp GOTD_STATE_EXPECT_HAVE_OR_DONE,
61 e70fd952 2024-03-22 stsp GOTD_STATE_DONE,
62 e70fd952 2024-03-22 stsp };
63 e70fd952 2024-03-22 stsp
64 e70fd952 2024-03-22 stsp static struct gotd_session_read {
65 e70fd952 2024-03-22 stsp pid_t pid;
66 e70fd952 2024-03-22 stsp const char *title;
67 e70fd952 2024-03-22 stsp struct got_repository *repo;
68 e70fd952 2024-03-22 stsp struct gotd_repo *repo_cfg;
69 e70fd952 2024-03-22 stsp int *pack_fds;
70 e70fd952 2024-03-22 stsp int *temp_fds;
71 e70fd952 2024-03-22 stsp struct gotd_imsgev parent_iev;
72 e70fd952 2024-03-22 stsp struct gotd_imsgev notifier_iev;
73 e70fd952 2024-03-22 stsp struct timeval request_timeout;
74 e70fd952 2024-03-22 stsp enum gotd_session_read_state state;
75 e70fd952 2024-03-22 stsp struct gotd_imsgev repo_child_iev;
76 e70fd952 2024-03-22 stsp } gotd_session;
77 e70fd952 2024-03-22 stsp
78 e70fd952 2024-03-22 stsp static struct gotd_session_client {
79 e70fd952 2024-03-22 stsp struct gotd_client_capability *capabilities;
80 e70fd952 2024-03-22 stsp size_t ncapa_alloc;
81 e70fd952 2024-03-22 stsp size_t ncapabilities;
82 e70fd952 2024-03-22 stsp uint32_t id;
83 e70fd952 2024-03-22 stsp int fd;
84 e70fd952 2024-03-22 stsp int delta_cache_fd;
85 e70fd952 2024-03-22 stsp struct gotd_imsgev iev;
86 e70fd952 2024-03-22 stsp struct event tmo;
87 e70fd952 2024-03-22 stsp uid_t euid;
88 e70fd952 2024-03-22 stsp gid_t egid;
89 e70fd952 2024-03-22 stsp char *username;
90 e70fd952 2024-03-22 stsp char *packfile_path;
91 e70fd952 2024-03-22 stsp char *packidx_path;
92 e70fd952 2024-03-22 stsp int nref_updates;
93 e70fd952 2024-03-22 stsp int accept_flush_pkt;
94 e70fd952 2024-03-22 stsp int flush_disconnect;
95 e70fd952 2024-03-22 stsp } gotd_session_client;
96 e70fd952 2024-03-22 stsp
97 e70fd952 2024-03-22 stsp static void session_read_shutdown(void);
98 e70fd952 2024-03-22 stsp
99 e70fd952 2024-03-22 stsp static void
100 e70fd952 2024-03-22 stsp disconnect(struct gotd_session_client *client)
101 e70fd952 2024-03-22 stsp {
102 e70fd952 2024-03-22 stsp log_debug("uid %d: disconnecting", client->euid);
103 e70fd952 2024-03-22 stsp
104 e70fd952 2024-03-22 stsp if (gotd_imsg_compose_event(&gotd_session.parent_iev,
105 e70fd952 2024-03-22 stsp GOTD_IMSG_DISCONNECT, PROC_SESSION_READ, -1, NULL, 0) == -1)
106 e70fd952 2024-03-22 stsp log_warn("imsg compose DISCONNECT");
107 e70fd952 2024-03-22 stsp
108 e70fd952 2024-03-22 stsp imsg_clear(&gotd_session.repo_child_iev.ibuf);
109 e70fd952 2024-03-22 stsp event_del(&gotd_session.repo_child_iev.ev);
110 e70fd952 2024-03-22 stsp evtimer_del(&client->tmo);
111 e70fd952 2024-03-22 stsp close(client->fd);
112 e70fd952 2024-03-22 stsp if (client->delta_cache_fd != -1)
113 e70fd952 2024-03-22 stsp close(client->delta_cache_fd);
114 e70fd952 2024-03-22 stsp if (client->packfile_path) {
115 e70fd952 2024-03-22 stsp if (unlink(client->packfile_path) == -1 && errno != ENOENT)
116 e70fd952 2024-03-22 stsp log_warn("unlink %s: ", client->packfile_path);
117 e70fd952 2024-03-22 stsp free(client->packfile_path);
118 e70fd952 2024-03-22 stsp }
119 e70fd952 2024-03-22 stsp if (client->packidx_path) {
120 e70fd952 2024-03-22 stsp if (unlink(client->packidx_path) == -1 && errno != ENOENT)
121 e70fd952 2024-03-22 stsp log_warn("unlink %s: ", client->packidx_path);
122 e70fd952 2024-03-22 stsp free(client->packidx_path);
123 e70fd952 2024-03-22 stsp }
124 e70fd952 2024-03-22 stsp free(client->capabilities);
125 e70fd952 2024-03-22 stsp
126 e70fd952 2024-03-22 stsp session_read_shutdown();
127 e70fd952 2024-03-22 stsp }
128 e70fd952 2024-03-22 stsp
129 e70fd952 2024-03-22 stsp static void
130 e70fd952 2024-03-22 stsp disconnect_on_error(struct gotd_session_client *client,
131 e70fd952 2024-03-22 stsp const struct got_error *err)
132 e70fd952 2024-03-22 stsp {
133 e70fd952 2024-03-22 stsp struct imsgbuf ibuf;
134 e70fd952 2024-03-22 stsp
135 e70fd952 2024-03-22 stsp if (err->code != GOT_ERR_EOF) {
136 e70fd952 2024-03-22 stsp log_warnx("uid %d: %s", client->euid, err->msg);
137 e70fd952 2024-03-22 stsp imsg_init(&ibuf, client->fd);
138 e70fd952 2024-03-22 stsp gotd_imsg_send_error(&ibuf, 0, PROC_SESSION_READ, err);
139 e70fd952 2024-03-22 stsp imsg_clear(&ibuf);
140 e70fd952 2024-03-22 stsp }
141 e70fd952 2024-03-22 stsp
142 e70fd952 2024-03-22 stsp disconnect(client);
143 e70fd952 2024-03-22 stsp }
144 e70fd952 2024-03-22 stsp
145 e70fd952 2024-03-22 stsp static void
146 e70fd952 2024-03-22 stsp gotd_request_timeout(int fd, short events, void *arg)
147 e70fd952 2024-03-22 stsp {
148 e70fd952 2024-03-22 stsp struct gotd_session_client *client = arg;
149 e70fd952 2024-03-22 stsp
150 7268d461 2024-05-01 stsp log_warnx("disconnecting uid %d due to timeout", client->euid);
151 e70fd952 2024-03-22 stsp disconnect(client);
152 e70fd952 2024-03-22 stsp }
153 e70fd952 2024-03-22 stsp
154 e70fd952 2024-03-22 stsp static void
155 e70fd952 2024-03-22 stsp session_read_sighdlr(int sig, short event, void *arg)
156 e70fd952 2024-03-22 stsp {
157 e70fd952 2024-03-22 stsp /*
158 e70fd952 2024-03-22 stsp * Normal signal handler rules don't apply because libevent
159 e70fd952 2024-03-22 stsp * decouples for us.
160 e70fd952 2024-03-22 stsp */
161 e70fd952 2024-03-22 stsp
162 e70fd952 2024-03-22 stsp switch (sig) {
163 e70fd952 2024-03-22 stsp case SIGHUP:
164 e70fd952 2024-03-22 stsp log_info("%s: ignoring SIGHUP", __func__);
165 e70fd952 2024-03-22 stsp break;
166 e70fd952 2024-03-22 stsp case SIGUSR1:
167 e70fd952 2024-03-22 stsp log_info("%s: ignoring SIGUSR1", __func__);
168 e70fd952 2024-03-22 stsp break;
169 e70fd952 2024-03-22 stsp case SIGTERM:
170 e70fd952 2024-03-22 stsp case SIGINT:
171 e70fd952 2024-03-22 stsp session_read_shutdown();
172 e70fd952 2024-03-22 stsp /* NOTREACHED */
173 e70fd952 2024-03-22 stsp break;
174 e70fd952 2024-03-22 stsp default:
175 e70fd952 2024-03-22 stsp fatalx("unexpected signal");
176 e70fd952 2024-03-22 stsp }
177 e70fd952 2024-03-22 stsp }
178 e70fd952 2024-03-22 stsp
179 e70fd952 2024-03-22 stsp static const struct got_error *
180 e70fd952 2024-03-22 stsp recv_packfile_done(struct imsg *imsg)
181 e70fd952 2024-03-22 stsp {
182 e70fd952 2024-03-22 stsp size_t datalen;
183 e70fd952 2024-03-22 stsp
184 e70fd952 2024-03-22 stsp log_debug("packfile-done received");
185 e70fd952 2024-03-22 stsp
186 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
187 e70fd952 2024-03-22 stsp if (datalen != 0)
188 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
189 e70fd952 2024-03-22 stsp
190 e70fd952 2024-03-22 stsp return NULL;
191 e70fd952 2024-03-22 stsp }
192 e70fd952 2024-03-22 stsp
193 e70fd952 2024-03-22 stsp static void
194 e70fd952 2024-03-22 stsp session_dispatch_repo_child(int fd, short event, void *arg)
195 e70fd952 2024-03-22 stsp {
196 e70fd952 2024-03-22 stsp struct gotd_imsgev *iev = arg;
197 e70fd952 2024-03-22 stsp struct imsgbuf *ibuf = &iev->ibuf;
198 e70fd952 2024-03-22 stsp struct gotd_session_client *client = &gotd_session_client;
199 e70fd952 2024-03-22 stsp ssize_t n;
200 e70fd952 2024-03-22 stsp int shut = 0;
201 e70fd952 2024-03-22 stsp struct imsg imsg;
202 e70fd952 2024-03-22 stsp
203 e70fd952 2024-03-22 stsp if (event & EV_READ) {
204 e70fd952 2024-03-22 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
205 e70fd952 2024-03-22 stsp fatal("imsg_read error");
206 e70fd952 2024-03-22 stsp if (n == 0) {
207 e70fd952 2024-03-22 stsp /* Connection closed. */
208 e70fd952 2024-03-22 stsp shut = 1;
209 e70fd952 2024-03-22 stsp goto done;
210 e70fd952 2024-03-22 stsp }
211 e70fd952 2024-03-22 stsp }
212 e70fd952 2024-03-22 stsp
213 e70fd952 2024-03-22 stsp if (event & EV_WRITE) {
214 e70fd952 2024-03-22 stsp n = msgbuf_write(&ibuf->w);
215 e70fd952 2024-03-22 stsp if (n == -1 && errno != EAGAIN)
216 e70fd952 2024-03-22 stsp fatal("msgbuf_write");
217 e70fd952 2024-03-22 stsp if (n == 0) {
218 e70fd952 2024-03-22 stsp /* Connection closed. */
219 e70fd952 2024-03-22 stsp shut = 1;
220 e70fd952 2024-03-22 stsp goto done;
221 e70fd952 2024-03-22 stsp }
222 e70fd952 2024-03-22 stsp }
223 e70fd952 2024-03-22 stsp
224 e70fd952 2024-03-22 stsp for (;;) {
225 e70fd952 2024-03-22 stsp const struct got_error *err = NULL;
226 e70fd952 2024-03-22 stsp uint32_t client_id = 0;
227 e70fd952 2024-03-22 stsp int do_disconnect = 0;
228 e70fd952 2024-03-22 stsp
229 e70fd952 2024-03-22 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
230 e70fd952 2024-03-22 stsp fatal("%s: imsg_get error", __func__);
231 e70fd952 2024-03-22 stsp if (n == 0) /* No more messages. */
232 e70fd952 2024-03-22 stsp break;
233 e70fd952 2024-03-22 stsp
234 e70fd952 2024-03-22 stsp switch (imsg.hdr.type) {
235 e70fd952 2024-03-22 stsp case GOTD_IMSG_ERROR:
236 e70fd952 2024-03-22 stsp do_disconnect = 1;
237 e70fd952 2024-03-22 stsp err = gotd_imsg_recv_error(&client_id, &imsg);
238 e70fd952 2024-03-22 stsp break;
239 e70fd952 2024-03-22 stsp case GOTD_IMSG_PACKFILE_DONE:
240 e70fd952 2024-03-22 stsp do_disconnect = 1;
241 e70fd952 2024-03-22 stsp err = recv_packfile_done(&imsg);
242 e70fd952 2024-03-22 stsp break;
243 e70fd952 2024-03-22 stsp default:
244 e70fd952 2024-03-22 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
245 e70fd952 2024-03-22 stsp break;
246 e70fd952 2024-03-22 stsp }
247 e70fd952 2024-03-22 stsp
248 e70fd952 2024-03-22 stsp if (do_disconnect) {
249 e70fd952 2024-03-22 stsp if (err)
250 e70fd952 2024-03-22 stsp disconnect_on_error(client, err);
251 e70fd952 2024-03-22 stsp else
252 e70fd952 2024-03-22 stsp disconnect(client);
253 e70fd952 2024-03-22 stsp } else {
254 e70fd952 2024-03-22 stsp if (err)
255 e70fd952 2024-03-22 stsp log_warnx("uid %d: %s", client->euid, err->msg);
256 e70fd952 2024-03-22 stsp }
257 e70fd952 2024-03-22 stsp imsg_free(&imsg);
258 e70fd952 2024-03-22 stsp }
259 e70fd952 2024-03-22 stsp done:
260 e70fd952 2024-03-22 stsp if (!shut) {
261 e70fd952 2024-03-22 stsp gotd_imsg_event_add(iev);
262 e70fd952 2024-03-22 stsp } else {
263 e70fd952 2024-03-22 stsp /* This pipe is dead. Remove its event handler */
264 e70fd952 2024-03-22 stsp event_del(&iev->ev);
265 e70fd952 2024-03-22 stsp event_loopexit(NULL);
266 e70fd952 2024-03-22 stsp }
267 e70fd952 2024-03-22 stsp }
268 e70fd952 2024-03-22 stsp
269 e70fd952 2024-03-22 stsp static const struct got_error *
270 e70fd952 2024-03-22 stsp recv_capabilities(struct gotd_session_client *client, struct imsg *imsg)
271 e70fd952 2024-03-22 stsp {
272 e70fd952 2024-03-22 stsp struct gotd_imsg_capabilities icapas;
273 e70fd952 2024-03-22 stsp size_t datalen;
274 e70fd952 2024-03-22 stsp
275 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
276 e70fd952 2024-03-22 stsp if (datalen != sizeof(icapas))
277 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
278 e70fd952 2024-03-22 stsp memcpy(&icapas, imsg->data, sizeof(icapas));
279 e70fd952 2024-03-22 stsp
280 e70fd952 2024-03-22 stsp client->ncapa_alloc = icapas.ncapabilities;
281 e70fd952 2024-03-22 stsp client->capabilities = calloc(client->ncapa_alloc,
282 e70fd952 2024-03-22 stsp sizeof(*client->capabilities));
283 e70fd952 2024-03-22 stsp if (client->capabilities == NULL) {
284 e70fd952 2024-03-22 stsp client->ncapa_alloc = 0;
285 e70fd952 2024-03-22 stsp return got_error_from_errno("calloc");
286 e70fd952 2024-03-22 stsp }
287 e70fd952 2024-03-22 stsp
288 e70fd952 2024-03-22 stsp log_debug("expecting %zu capabilities from uid %d",
289 e70fd952 2024-03-22 stsp client->ncapa_alloc, client->euid);
290 e70fd952 2024-03-22 stsp return NULL;
291 e70fd952 2024-03-22 stsp }
292 e70fd952 2024-03-22 stsp
293 e70fd952 2024-03-22 stsp static const struct got_error *
294 e70fd952 2024-03-22 stsp recv_capability(struct gotd_session_client *client, struct imsg *imsg)
295 e70fd952 2024-03-22 stsp {
296 e70fd952 2024-03-22 stsp struct gotd_imsg_capability icapa;
297 e70fd952 2024-03-22 stsp struct gotd_client_capability *capa;
298 e70fd952 2024-03-22 stsp size_t datalen;
299 e70fd952 2024-03-22 stsp char *key, *value = NULL;
300 e70fd952 2024-03-22 stsp
301 e70fd952 2024-03-22 stsp if (client->capabilities == NULL ||
302 e70fd952 2024-03-22 stsp client->ncapabilities >= client->ncapa_alloc) {
303 e70fd952 2024-03-22 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
304 e70fd952 2024-03-22 stsp "unexpected capability received");
305 e70fd952 2024-03-22 stsp }
306 e70fd952 2024-03-22 stsp
307 e70fd952 2024-03-22 stsp memset(&icapa, 0, sizeof(icapa));
308 e70fd952 2024-03-22 stsp
309 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
310 e70fd952 2024-03-22 stsp if (datalen < sizeof(icapa))
311 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
312 e70fd952 2024-03-22 stsp memcpy(&icapa, imsg->data, sizeof(icapa));
313 e70fd952 2024-03-22 stsp
314 e70fd952 2024-03-22 stsp if (datalen != sizeof(icapa) + icapa.key_len + icapa.value_len)
315 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
316 e70fd952 2024-03-22 stsp
317 e70fd952 2024-03-22 stsp key = strndup(imsg->data + sizeof(icapa), icapa.key_len);
318 e70fd952 2024-03-22 stsp if (key == NULL)
319 e70fd952 2024-03-22 stsp return got_error_from_errno("strndup");
320 e70fd952 2024-03-22 stsp if (icapa.value_len > 0) {
321 e70fd952 2024-03-22 stsp value = strndup(imsg->data + sizeof(icapa) + icapa.key_len,
322 e70fd952 2024-03-22 stsp icapa.value_len);
323 e70fd952 2024-03-22 stsp if (value == NULL) {
324 e70fd952 2024-03-22 stsp free(key);
325 e70fd952 2024-03-22 stsp return got_error_from_errno("strndup");
326 e70fd952 2024-03-22 stsp }
327 e70fd952 2024-03-22 stsp }
328 e70fd952 2024-03-22 stsp
329 e70fd952 2024-03-22 stsp capa = &client->capabilities[client->ncapabilities++];
330 e70fd952 2024-03-22 stsp capa->key = key;
331 e70fd952 2024-03-22 stsp capa->value = value;
332 e70fd952 2024-03-22 stsp
333 e70fd952 2024-03-22 stsp if (value)
334 e70fd952 2024-03-22 stsp log_debug("uid %d: capability %s=%s", client->euid, key, value);
335 e70fd952 2024-03-22 stsp else
336 e70fd952 2024-03-22 stsp log_debug("uid %d: capability %s", client->euid, key);
337 e70fd952 2024-03-22 stsp
338 e70fd952 2024-03-22 stsp return NULL;
339 e70fd952 2024-03-22 stsp }
340 e70fd952 2024-03-22 stsp
341 e70fd952 2024-03-22 stsp static const struct got_error *
342 e70fd952 2024-03-22 stsp forward_want(struct gotd_session_client *client, struct imsg *imsg)
343 e70fd952 2024-03-22 stsp {
344 e70fd952 2024-03-22 stsp struct gotd_imsg_want ireq;
345 e70fd952 2024-03-22 stsp struct gotd_imsg_want iwant;
346 e70fd952 2024-03-22 stsp size_t datalen;
347 e70fd952 2024-03-22 stsp
348 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
349 e70fd952 2024-03-22 stsp if (datalen != sizeof(ireq))
350 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
351 e70fd952 2024-03-22 stsp
352 e70fd952 2024-03-22 stsp memcpy(&ireq, imsg->data, datalen);
353 e70fd952 2024-03-22 stsp
354 e70fd952 2024-03-22 stsp memset(&iwant, 0, sizeof(iwant));
355 e70fd952 2024-03-22 stsp memcpy(iwant.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
356 e70fd952 2024-03-22 stsp
357 e70fd952 2024-03-22 stsp if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
358 e70fd952 2024-03-22 stsp GOTD_IMSG_WANT, PROC_SESSION_READ, -1,
359 e70fd952 2024-03-22 stsp &iwant, sizeof(iwant)) == -1)
360 e70fd952 2024-03-22 stsp return got_error_from_errno("imsg compose WANT");
361 e70fd952 2024-03-22 stsp
362 e70fd952 2024-03-22 stsp return NULL;
363 e70fd952 2024-03-22 stsp }
364 e70fd952 2024-03-22 stsp
365 e70fd952 2024-03-22 stsp static const struct got_error *
366 e70fd952 2024-03-22 stsp forward_have(struct gotd_session_client *client, struct imsg *imsg)
367 e70fd952 2024-03-22 stsp {
368 e70fd952 2024-03-22 stsp struct gotd_imsg_have ireq;
369 e70fd952 2024-03-22 stsp struct gotd_imsg_have ihave;
370 e70fd952 2024-03-22 stsp size_t datalen;
371 e70fd952 2024-03-22 stsp
372 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
373 e70fd952 2024-03-22 stsp if (datalen != sizeof(ireq))
374 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
375 e70fd952 2024-03-22 stsp
376 e70fd952 2024-03-22 stsp memcpy(&ireq, imsg->data, datalen);
377 e70fd952 2024-03-22 stsp
378 e70fd952 2024-03-22 stsp memset(&ihave, 0, sizeof(ihave));
379 e70fd952 2024-03-22 stsp memcpy(ihave.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
380 e70fd952 2024-03-22 stsp
381 e70fd952 2024-03-22 stsp if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
382 e70fd952 2024-03-22 stsp GOTD_IMSG_HAVE, PROC_SESSION_READ, -1,
383 e70fd952 2024-03-22 stsp &ihave, sizeof(ihave)) == -1)
384 e70fd952 2024-03-22 stsp return got_error_from_errno("imsg compose HAVE");
385 e70fd952 2024-03-22 stsp
386 e70fd952 2024-03-22 stsp return NULL;
387 e70fd952 2024-03-22 stsp }
388 e70fd952 2024-03-22 stsp
389 e70fd952 2024-03-22 stsp static int
390 e70fd952 2024-03-22 stsp client_has_capability(struct gotd_session_client *client, const char *capastr)
391 e70fd952 2024-03-22 stsp {
392 e70fd952 2024-03-22 stsp struct gotd_client_capability *capa;
393 e70fd952 2024-03-22 stsp size_t i;
394 e70fd952 2024-03-22 stsp
395 e70fd952 2024-03-22 stsp if (client->ncapabilities == 0)
396 e70fd952 2024-03-22 stsp return 0;
397 e70fd952 2024-03-22 stsp
398 e70fd952 2024-03-22 stsp for (i = 0; i < client->ncapabilities; i++) {
399 e70fd952 2024-03-22 stsp capa = &client->capabilities[i];
400 e70fd952 2024-03-22 stsp if (strcmp(capa->key, capastr) == 0)
401 e70fd952 2024-03-22 stsp return 1;
402 e70fd952 2024-03-22 stsp }
403 e70fd952 2024-03-22 stsp
404 e70fd952 2024-03-22 stsp return 0;
405 e70fd952 2024-03-22 stsp }
406 e70fd952 2024-03-22 stsp
407 e70fd952 2024-03-22 stsp static const struct got_error *
408 e70fd952 2024-03-22 stsp send_packfile(struct gotd_session_client *client)
409 e70fd952 2024-03-22 stsp {
410 e70fd952 2024-03-22 stsp const struct got_error *err = NULL;
411 e70fd952 2024-03-22 stsp struct gotd_imsg_send_packfile ipack;
412 e70fd952 2024-03-22 stsp int pipe[2];
413 e70fd952 2024-03-22 stsp
414 e70fd952 2024-03-22 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
415 e70fd952 2024-03-22 stsp return got_error_from_errno("socketpair");
416 e70fd952 2024-03-22 stsp
417 e70fd952 2024-03-22 stsp memset(&ipack, 0, sizeof(ipack));
418 e70fd952 2024-03-22 stsp
419 e70fd952 2024-03-22 stsp if (client_has_capability(client, GOT_CAPA_SIDE_BAND_64K))
420 e70fd952 2024-03-22 stsp ipack.report_progress = 1;
421 e70fd952 2024-03-22 stsp
422 e70fd952 2024-03-22 stsp client->delta_cache_fd = got_opentempfd();
423 e70fd952 2024-03-22 stsp if (client->delta_cache_fd == -1)
424 e70fd952 2024-03-22 stsp return got_error_from_errno("got_opentempfd");
425 e70fd952 2024-03-22 stsp
426 e70fd952 2024-03-22 stsp if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
427 e70fd952 2024-03-22 stsp GOTD_IMSG_SEND_PACKFILE, PROC_GOTD, client->delta_cache_fd,
428 e70fd952 2024-03-22 stsp &ipack, sizeof(ipack)) == -1) {
429 e70fd952 2024-03-22 stsp err = got_error_from_errno("imsg compose SEND_PACKFILE");
430 e70fd952 2024-03-22 stsp close(pipe[0]);
431 e70fd952 2024-03-22 stsp close(pipe[1]);
432 e70fd952 2024-03-22 stsp return err;
433 e70fd952 2024-03-22 stsp }
434 e70fd952 2024-03-22 stsp
435 e70fd952 2024-03-22 stsp /* Send pack pipe end 0 to repo child process. */
436 e70fd952 2024-03-22 stsp if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
437 e70fd952 2024-03-22 stsp GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[0], NULL, 0) == -1) {
438 e70fd952 2024-03-22 stsp err = got_error_from_errno("imsg compose PACKFILE_PIPE");
439 e70fd952 2024-03-22 stsp close(pipe[1]);
440 e70fd952 2024-03-22 stsp return err;
441 e70fd952 2024-03-22 stsp }
442 e70fd952 2024-03-22 stsp
443 e70fd952 2024-03-22 stsp /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
444 e70fd952 2024-03-22 stsp if (gotd_imsg_compose_event(&client->iev,
445 e70fd952 2024-03-22 stsp GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1], NULL, 0) == -1)
446 e70fd952 2024-03-22 stsp err = got_error_from_errno("imsg compose PACKFILE_PIPE");
447 e70fd952 2024-03-22 stsp
448 e70fd952 2024-03-22 stsp return err;
449 e70fd952 2024-03-22 stsp }
450 e70fd952 2024-03-22 stsp
451 e70fd952 2024-03-22 stsp static void
452 e70fd952 2024-03-22 stsp session_dispatch_client(int fd, short events, void *arg)
453 e70fd952 2024-03-22 stsp {
454 e70fd952 2024-03-22 stsp struct gotd_imsgev *iev = arg;
455 e70fd952 2024-03-22 stsp struct imsgbuf *ibuf = &iev->ibuf;
456 e70fd952 2024-03-22 stsp struct gotd_session_client *client = &gotd_session_client;
457 e70fd952 2024-03-22 stsp const struct got_error *err = NULL;
458 e70fd952 2024-03-22 stsp struct imsg imsg;
459 e70fd952 2024-03-22 stsp ssize_t n;
460 e70fd952 2024-03-22 stsp
461 e70fd952 2024-03-22 stsp if (events & EV_WRITE) {
462 e70fd952 2024-03-22 stsp while (ibuf->w.queued) {
463 e70fd952 2024-03-22 stsp n = msgbuf_write(&ibuf->w);
464 bcb30926 2024-04-30 stsp if (n == -1 && errno != EAGAIN) {
465 e70fd952 2024-03-22 stsp err = got_error_from_errno("imsg_flush");
466 e70fd952 2024-03-22 stsp disconnect_on_error(client, err);
467 e70fd952 2024-03-22 stsp return;
468 e70fd952 2024-03-22 stsp }
469 e70fd952 2024-03-22 stsp if (n == 0) {
470 e70fd952 2024-03-22 stsp /* Connection closed. */
471 e70fd952 2024-03-22 stsp err = got_error(GOT_ERR_EOF);
472 e70fd952 2024-03-22 stsp disconnect_on_error(client, err);
473 e70fd952 2024-03-22 stsp return;
474 e70fd952 2024-03-22 stsp }
475 e70fd952 2024-03-22 stsp }
476 e70fd952 2024-03-22 stsp
477 e70fd952 2024-03-22 stsp if (client->flush_disconnect) {
478 e70fd952 2024-03-22 stsp disconnect(client);
479 e70fd952 2024-03-22 stsp return;
480 e70fd952 2024-03-22 stsp }
481 e70fd952 2024-03-22 stsp }
482 e70fd952 2024-03-22 stsp
483 e70fd952 2024-03-22 stsp if ((events & EV_READ) == 0)
484 e70fd952 2024-03-22 stsp return;
485 e70fd952 2024-03-22 stsp
486 e70fd952 2024-03-22 stsp memset(&imsg, 0, sizeof(imsg));
487 e70fd952 2024-03-22 stsp
488 e70fd952 2024-03-22 stsp while (err == NULL) {
489 e70fd952 2024-03-22 stsp err = gotd_imsg_recv(&imsg, ibuf, 0);
490 e70fd952 2024-03-22 stsp if (err) {
491 e70fd952 2024-03-22 stsp if (err->code == GOT_ERR_PRIVSEP_READ)
492 e70fd952 2024-03-22 stsp err = NULL;
493 e70fd952 2024-03-22 stsp else if (err->code == GOT_ERR_EOF &&
494 e70fd952 2024-03-22 stsp gotd_session.state ==
495 e70fd952 2024-03-22 stsp GOTD_STATE_EXPECT_CAPABILITIES) {
496 e70fd952 2024-03-22 stsp /*
497 e70fd952 2024-03-22 stsp * The client has closed its socket before
498 e70fd952 2024-03-22 stsp * sending its capability announcement.
499 e70fd952 2024-03-22 stsp * This can happen when Git clients have
500 e70fd952 2024-03-22 stsp * no ref-updates to send.
501 e70fd952 2024-03-22 stsp */
502 e70fd952 2024-03-22 stsp disconnect_on_error(client, err);
503 e70fd952 2024-03-22 stsp return;
504 e70fd952 2024-03-22 stsp }
505 e70fd952 2024-03-22 stsp break;
506 e70fd952 2024-03-22 stsp }
507 e70fd952 2024-03-22 stsp
508 e70fd952 2024-03-22 stsp evtimer_del(&client->tmo);
509 e70fd952 2024-03-22 stsp
510 e70fd952 2024-03-22 stsp switch (imsg.hdr.type) {
511 e70fd952 2024-03-22 stsp case GOTD_IMSG_CAPABILITIES:
512 e70fd952 2024-03-22 stsp if (gotd_session.state !=
513 e70fd952 2024-03-22 stsp GOTD_STATE_EXPECT_CAPABILITIES) {
514 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
515 e70fd952 2024-03-22 stsp "unexpected capabilities received");
516 e70fd952 2024-03-22 stsp break;
517 e70fd952 2024-03-22 stsp }
518 e70fd952 2024-03-22 stsp log_debug("receiving capabilities from uid %d",
519 e70fd952 2024-03-22 stsp client->euid);
520 e70fd952 2024-03-22 stsp err = recv_capabilities(client, &imsg);
521 e70fd952 2024-03-22 stsp break;
522 e70fd952 2024-03-22 stsp case GOTD_IMSG_CAPABILITY:
523 e70fd952 2024-03-22 stsp if (gotd_session.state != GOTD_STATE_EXPECT_CAPABILITIES) {
524 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
525 e70fd952 2024-03-22 stsp "unexpected capability received");
526 e70fd952 2024-03-22 stsp break;
527 e70fd952 2024-03-22 stsp }
528 e70fd952 2024-03-22 stsp err = recv_capability(client, &imsg);
529 e70fd952 2024-03-22 stsp if (err || client->ncapabilities < client->ncapa_alloc)
530 e70fd952 2024-03-22 stsp break;
531 e70fd952 2024-03-22 stsp gotd_session.state = GOTD_STATE_EXPECT_WANT;
532 e70fd952 2024-03-22 stsp client->accept_flush_pkt = 1;
533 e70fd952 2024-03-22 stsp log_debug("uid %d: expecting want-lines", client->euid);
534 e70fd952 2024-03-22 stsp break;
535 e70fd952 2024-03-22 stsp case GOTD_IMSG_WANT:
536 e70fd952 2024-03-22 stsp if (gotd_session.state != GOTD_STATE_EXPECT_WANT) {
537 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
538 e70fd952 2024-03-22 stsp "unexpected want-line received");
539 e70fd952 2024-03-22 stsp break;
540 e70fd952 2024-03-22 stsp }
541 e70fd952 2024-03-22 stsp log_debug("received want-line from uid %d",
542 e70fd952 2024-03-22 stsp client->euid);
543 e70fd952 2024-03-22 stsp client->accept_flush_pkt = 1;
544 e70fd952 2024-03-22 stsp err = forward_want(client, &imsg);
545 e70fd952 2024-03-22 stsp break;
546 e70fd952 2024-03-22 stsp case GOTD_IMSG_HAVE:
547 c811fd62 2024-05-05 stsp if (gotd_session.state !=
548 c811fd62 2024-05-05 stsp GOTD_STATE_EXPECT_HAVE_OR_DONE) {
549 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
550 e70fd952 2024-03-22 stsp "unexpected have-line received");
551 e70fd952 2024-03-22 stsp break;
552 e70fd952 2024-03-22 stsp }
553 e70fd952 2024-03-22 stsp log_debug("received have-line from uid %d",
554 e70fd952 2024-03-22 stsp client->euid);
555 e70fd952 2024-03-22 stsp err = forward_have(client, &imsg);
556 e70fd952 2024-03-22 stsp if (err)
557 e70fd952 2024-03-22 stsp break;
558 e70fd952 2024-03-22 stsp client->accept_flush_pkt = 1;
559 e70fd952 2024-03-22 stsp break;
560 e70fd952 2024-03-22 stsp case GOTD_IMSG_FLUSH:
561 e70fd952 2024-03-22 stsp if (gotd_session.state != GOTD_STATE_EXPECT_WANT &&
562 c811fd62 2024-05-05 stsp gotd_session.state !=
563 c811fd62 2024-05-05 stsp GOTD_STATE_EXPECT_HAVE_OR_DONE) {
564 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
565 e70fd952 2024-03-22 stsp "unexpected flush-pkt received");
566 e70fd952 2024-03-22 stsp break;
567 e70fd952 2024-03-22 stsp }
568 e70fd952 2024-03-22 stsp if (!client->accept_flush_pkt) {
569 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
570 e70fd952 2024-03-22 stsp "unexpected flush-pkt received");
571 e70fd952 2024-03-22 stsp break;
572 e70fd952 2024-03-22 stsp }
573 e70fd952 2024-03-22 stsp
574 e70fd952 2024-03-22 stsp /*
575 e70fd952 2024-03-22 stsp * Accept just one flush packet at a time.
576 e70fd952 2024-03-22 stsp * Future client state transitions will set this flag
577 e70fd952 2024-03-22 stsp * again if another flush packet is expected.
578 e70fd952 2024-03-22 stsp */
579 e70fd952 2024-03-22 stsp client->accept_flush_pkt = 0;
580 e70fd952 2024-03-22 stsp
581 e70fd952 2024-03-22 stsp log_debug("received flush-pkt from uid %d",
582 e70fd952 2024-03-22 stsp client->euid);
583 e70fd952 2024-03-22 stsp if (gotd_session.state == GOTD_STATE_EXPECT_WANT) {
584 c811fd62 2024-05-05 stsp gotd_session.state =
585 c811fd62 2024-05-05 stsp GOTD_STATE_EXPECT_HAVE_OR_DONE;
586 c811fd62 2024-05-05 stsp log_debug("uid %d: expecting have-lines "
587 c811fd62 2024-05-05 stsp "or 'done'", client->euid);
588 c811fd62 2024-05-05 stsp } else if (gotd_session.state ==
589 c811fd62 2024-05-05 stsp GOTD_STATE_EXPECT_HAVE_OR_DONE) {
590 e70fd952 2024-03-22 stsp client->accept_flush_pkt = 1;
591 c811fd62 2024-05-05 stsp log_debug("uid %d: expecting more have-lines "
592 c811fd62 2024-05-05 stsp "or 'done'", client->euid);
593 c811fd62 2024-05-05 stsp } else if (gotd_session.state !=
594 c811fd62 2024-05-05 stsp GOTD_STATE_EXPECT_HAVE_OR_DONE) {
595 e70fd952 2024-03-22 stsp /* should not happen, see above */
596 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
597 e70fd952 2024-03-22 stsp "unexpected client state");
598 e70fd952 2024-03-22 stsp break;
599 e70fd952 2024-03-22 stsp }
600 e70fd952 2024-03-22 stsp break;
601 e70fd952 2024-03-22 stsp case GOTD_IMSG_DONE:
602 c811fd62 2024-05-05 stsp if (gotd_session.state !=
603 c811fd62 2024-05-05 stsp GOTD_STATE_EXPECT_HAVE_OR_DONE) {
604 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
605 e70fd952 2024-03-22 stsp "unexpected flush-pkt received");
606 e70fd952 2024-03-22 stsp break;
607 e70fd952 2024-03-22 stsp }
608 e70fd952 2024-03-22 stsp log_debug("received 'done' from uid %d", client->euid);
609 e70fd952 2024-03-22 stsp gotd_session.state = GOTD_STATE_DONE;
610 e70fd952 2024-03-22 stsp client->accept_flush_pkt = 1;
611 e70fd952 2024-03-22 stsp err = send_packfile(client);
612 e70fd952 2024-03-22 stsp break;
613 e70fd952 2024-03-22 stsp default:
614 e70fd952 2024-03-22 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
615 e70fd952 2024-03-22 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
616 e70fd952 2024-03-22 stsp break;
617 e70fd952 2024-03-22 stsp }
618 e70fd952 2024-03-22 stsp
619 e70fd952 2024-03-22 stsp imsg_free(&imsg);
620 e70fd952 2024-03-22 stsp }
621 e70fd952 2024-03-22 stsp
622 e70fd952 2024-03-22 stsp if (err) {
623 e70fd952 2024-03-22 stsp if (err->code != GOT_ERR_EOF)
624 e70fd952 2024-03-22 stsp disconnect_on_error(client, err);
625 e70fd952 2024-03-22 stsp } else {
626 e70fd952 2024-03-22 stsp gotd_imsg_event_add(iev);
627 e70fd952 2024-03-22 stsp evtimer_add(&client->tmo, &gotd_session.request_timeout);
628 e70fd952 2024-03-22 stsp }
629 e70fd952 2024-03-22 stsp }
630 e70fd952 2024-03-22 stsp
631 e70fd952 2024-03-22 stsp static const struct got_error *
632 e70fd952 2024-03-22 stsp list_refs_request(void)
633 e70fd952 2024-03-22 stsp {
634 e70fd952 2024-03-22 stsp static const struct got_error *err;
635 e70fd952 2024-03-22 stsp struct gotd_session_client *client = &gotd_session_client;
636 e70fd952 2024-03-22 stsp struct gotd_imsgev *iev = &gotd_session.repo_child_iev;
637 e70fd952 2024-03-22 stsp int fd;
638 e70fd952 2024-03-22 stsp
639 e70fd952 2024-03-22 stsp if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
640 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
641 e70fd952 2024-03-22 stsp
642 e70fd952 2024-03-22 stsp fd = dup(client->fd);
643 e70fd952 2024-03-22 stsp if (fd == -1)
644 e70fd952 2024-03-22 stsp return got_error_from_errno("dup");
645 e70fd952 2024-03-22 stsp
646 e70fd952 2024-03-22 stsp if (gotd_imsg_compose_event(iev, GOTD_IMSG_LIST_REFS_INTERNAL,
647 e70fd952 2024-03-22 stsp PROC_SESSION_READ, fd, NULL, 0) == -1) {
648 e70fd952 2024-03-22 stsp err = got_error_from_errno("imsg compose LIST_REFS_INTERNAL");
649 e70fd952 2024-03-22 stsp close(fd);
650 e70fd952 2024-03-22 stsp return err;
651 e70fd952 2024-03-22 stsp }
652 e70fd952 2024-03-22 stsp
653 e70fd952 2024-03-22 stsp gotd_session.state = GOTD_STATE_EXPECT_CAPABILITIES;
654 e70fd952 2024-03-22 stsp log_debug("uid %d: expecting capabilities", client->euid);
655 e70fd952 2024-03-22 stsp return NULL;
656 e70fd952 2024-03-22 stsp }
657 e70fd952 2024-03-22 stsp
658 e70fd952 2024-03-22 stsp static const struct got_error *
659 e70fd952 2024-03-22 stsp recv_connect(struct imsg *imsg)
660 e70fd952 2024-03-22 stsp {
661 e70fd952 2024-03-22 stsp struct gotd_session_client *client = &gotd_session_client;
662 e70fd952 2024-03-22 stsp struct gotd_imsg_connect iconnect;
663 e70fd952 2024-03-22 stsp size_t datalen;
664 e70fd952 2024-03-22 stsp
665 e70fd952 2024-03-22 stsp if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
666 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
667 e70fd952 2024-03-22 stsp
668 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
669 e70fd952 2024-03-22 stsp if (datalen < sizeof(iconnect))
670 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
671 e70fd952 2024-03-22 stsp memcpy(&iconnect, imsg->data, sizeof(iconnect));
672 e70fd952 2024-03-22 stsp if (iconnect.username_len == 0 ||
673 e70fd952 2024-03-22 stsp datalen != sizeof(iconnect) + iconnect.username_len)
674 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
675 e70fd952 2024-03-22 stsp
676 e70fd952 2024-03-22 stsp client->euid = iconnect.euid;
677 e70fd952 2024-03-22 stsp client->egid = iconnect.egid;
678 e70fd952 2024-03-22 stsp client->fd = imsg_get_fd(imsg);
679 e70fd952 2024-03-22 stsp if (client->fd == -1)
680 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
681 e70fd952 2024-03-22 stsp
682 e70fd952 2024-03-22 stsp client->username = strndup(imsg->data + sizeof(iconnect),
683 e70fd952 2024-03-22 stsp iconnect.username_len);
684 e70fd952 2024-03-22 stsp if (client->username == NULL)
685 e70fd952 2024-03-22 stsp return got_error_from_errno("strndup");
686 e70fd952 2024-03-22 stsp
687 e70fd952 2024-03-22 stsp imsg_init(&client->iev.ibuf, client->fd);
688 e70fd952 2024-03-22 stsp client->iev.handler = session_dispatch_client;
689 e70fd952 2024-03-22 stsp client->iev.events = EV_READ;
690 e70fd952 2024-03-22 stsp client->iev.handler_arg = NULL;
691 e70fd952 2024-03-22 stsp event_set(&client->iev.ev, client->iev.ibuf.fd, EV_READ,
692 e70fd952 2024-03-22 stsp session_dispatch_client, &client->iev);
693 e70fd952 2024-03-22 stsp gotd_imsg_event_add(&client->iev);
694 e70fd952 2024-03-22 stsp evtimer_set(&client->tmo, gotd_request_timeout, client);
695 caa6cf11 2024-04-30 stsp evtimer_add(&client->tmo, &gotd_session.request_timeout);
696 e70fd952 2024-03-22 stsp
697 e70fd952 2024-03-22 stsp return NULL;
698 e70fd952 2024-03-22 stsp }
699 e70fd952 2024-03-22 stsp
700 e70fd952 2024-03-22 stsp static const struct got_error *
701 e70fd952 2024-03-22 stsp recv_repo_child(struct imsg *imsg)
702 e70fd952 2024-03-22 stsp {
703 e70fd952 2024-03-22 stsp struct gotd_imsg_connect_repo_child ichild;
704 e70fd952 2024-03-22 stsp struct gotd_session_client *client = &gotd_session_client;
705 e70fd952 2024-03-22 stsp size_t datalen;
706 e70fd952 2024-03-22 stsp int fd;
707 e70fd952 2024-03-22 stsp
708 e70fd952 2024-03-22 stsp if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
709 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
710 e70fd952 2024-03-22 stsp
711 e70fd952 2024-03-22 stsp /* We should already have received a pipe to the listener. */
712 e70fd952 2024-03-22 stsp if (client->fd == -1)
713 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
714 e70fd952 2024-03-22 stsp
715 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
716 e70fd952 2024-03-22 stsp if (datalen != sizeof(ichild))
717 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
718 e70fd952 2024-03-22 stsp
719 e70fd952 2024-03-22 stsp memcpy(&ichild, imsg->data, sizeof(ichild));
720 e70fd952 2024-03-22 stsp
721 e70fd952 2024-03-22 stsp if (ichild.proc_id != PROC_REPO_READ)
722 e70fd952 2024-03-22 stsp return got_error_msg(GOT_ERR_PRIVSEP_MSG,
723 e70fd952 2024-03-22 stsp "bad child process type");
724 e70fd952 2024-03-22 stsp
725 e70fd952 2024-03-22 stsp fd = imsg_get_fd(imsg);
726 e70fd952 2024-03-22 stsp if (fd == -1)
727 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
728 e70fd952 2024-03-22 stsp
729 e70fd952 2024-03-22 stsp imsg_init(&gotd_session.repo_child_iev.ibuf, fd);
730 e70fd952 2024-03-22 stsp gotd_session.repo_child_iev.handler = session_dispatch_repo_child;
731 e70fd952 2024-03-22 stsp gotd_session.repo_child_iev.events = EV_READ;
732 e70fd952 2024-03-22 stsp gotd_session.repo_child_iev.handler_arg = NULL;
733 e70fd952 2024-03-22 stsp event_set(&gotd_session.repo_child_iev.ev,
734 e70fd952 2024-03-22 stsp gotd_session.repo_child_iev.ibuf.fd, EV_READ,
735 e70fd952 2024-03-22 stsp session_dispatch_repo_child, &gotd_session.repo_child_iev);
736 e70fd952 2024-03-22 stsp gotd_imsg_event_add(&gotd_session.repo_child_iev);
737 e70fd952 2024-03-22 stsp
738 e70fd952 2024-03-22 stsp /* The "recvfd" pledge promise is no longer needed. */
739 e70fd952 2024-03-22 stsp if (pledge("stdio rpath wpath cpath sendfd fattr flock", NULL) == -1)
740 e70fd952 2024-03-22 stsp fatal("pledge");
741 e70fd952 2024-03-22 stsp
742 e70fd952 2024-03-22 stsp return NULL;
743 e70fd952 2024-03-22 stsp }
744 e70fd952 2024-03-22 stsp
745 e70fd952 2024-03-22 stsp static void
746 e70fd952 2024-03-22 stsp session_dispatch(int fd, short event, void *arg)
747 e70fd952 2024-03-22 stsp {
748 e70fd952 2024-03-22 stsp struct gotd_imsgev *iev = arg;
749 e70fd952 2024-03-22 stsp struct imsgbuf *ibuf = &iev->ibuf;
750 e70fd952 2024-03-22 stsp struct gotd_session_client *client = &gotd_session_client;
751 e70fd952 2024-03-22 stsp ssize_t n;
752 e70fd952 2024-03-22 stsp int shut = 0;
753 e70fd952 2024-03-22 stsp struct imsg imsg;
754 e70fd952 2024-03-22 stsp
755 e70fd952 2024-03-22 stsp if (event & EV_READ) {
756 e70fd952 2024-03-22 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
757 e70fd952 2024-03-22 stsp fatal("imsg_read error");
758 e70fd952 2024-03-22 stsp if (n == 0) {
759 e70fd952 2024-03-22 stsp /* Connection closed. */
760 e70fd952 2024-03-22 stsp shut = 1;
761 e70fd952 2024-03-22 stsp goto done;
762 e70fd952 2024-03-22 stsp }
763 e70fd952 2024-03-22 stsp }
764 e70fd952 2024-03-22 stsp
765 e70fd952 2024-03-22 stsp if (event & EV_WRITE) {
766 e70fd952 2024-03-22 stsp n = msgbuf_write(&ibuf->w);
767 e70fd952 2024-03-22 stsp if (n == -1 && errno != EAGAIN)
768 e70fd952 2024-03-22 stsp fatal("msgbuf_write");
769 e70fd952 2024-03-22 stsp if (n == 0) {
770 e70fd952 2024-03-22 stsp /* Connection closed. */
771 e70fd952 2024-03-22 stsp shut = 1;
772 e70fd952 2024-03-22 stsp goto done;
773 e70fd952 2024-03-22 stsp }
774 e70fd952 2024-03-22 stsp }
775 e70fd952 2024-03-22 stsp
776 e70fd952 2024-03-22 stsp for (;;) {
777 e70fd952 2024-03-22 stsp const struct got_error *err = NULL;
778 e70fd952 2024-03-22 stsp uint32_t client_id = 0;
779 e70fd952 2024-03-22 stsp int do_disconnect = 0, do_list_refs = 0;
780 e70fd952 2024-03-22 stsp
781 e70fd952 2024-03-22 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
782 e70fd952 2024-03-22 stsp fatal("%s: imsg_get error", __func__);
783 e70fd952 2024-03-22 stsp if (n == 0) /* No more messages. */
784 e70fd952 2024-03-22 stsp break;
785 e70fd952 2024-03-22 stsp
786 e70fd952 2024-03-22 stsp switch (imsg.hdr.type) {
787 e70fd952 2024-03-22 stsp case GOTD_IMSG_ERROR:
788 e70fd952 2024-03-22 stsp do_disconnect = 1;
789 e70fd952 2024-03-22 stsp err = gotd_imsg_recv_error(&client_id, &imsg);
790 e70fd952 2024-03-22 stsp break;
791 e70fd952 2024-03-22 stsp case GOTD_IMSG_CONNECT:
792 e70fd952 2024-03-22 stsp err = recv_connect(&imsg);
793 e70fd952 2024-03-22 stsp break;
794 e70fd952 2024-03-22 stsp case GOTD_IMSG_DISCONNECT:
795 e70fd952 2024-03-22 stsp do_disconnect = 1;
796 e70fd952 2024-03-22 stsp break;
797 e70fd952 2024-03-22 stsp case GOTD_IMSG_CONNECT_REPO_CHILD:
798 e70fd952 2024-03-22 stsp err = recv_repo_child(&imsg);
799 e70fd952 2024-03-22 stsp if (err)
800 e70fd952 2024-03-22 stsp break;
801 e70fd952 2024-03-22 stsp do_list_refs = 1;
802 e70fd952 2024-03-22 stsp break;
803 e70fd952 2024-03-22 stsp default:
804 e70fd952 2024-03-22 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
805 e70fd952 2024-03-22 stsp break;
806 e70fd952 2024-03-22 stsp }
807 e70fd952 2024-03-22 stsp imsg_free(&imsg);
808 e70fd952 2024-03-22 stsp
809 e70fd952 2024-03-22 stsp if (do_disconnect) {
810 e70fd952 2024-03-22 stsp if (err)
811 e70fd952 2024-03-22 stsp disconnect_on_error(client, err);
812 e70fd952 2024-03-22 stsp else
813 e70fd952 2024-03-22 stsp disconnect(client);
814 e70fd952 2024-03-22 stsp } else if (do_list_refs)
815 e70fd952 2024-03-22 stsp err = list_refs_request();
816 e70fd952 2024-03-22 stsp
817 e70fd952 2024-03-22 stsp if (err)
818 e70fd952 2024-03-22 stsp log_warnx("uid %d: %s", client->euid, err->msg);
819 e70fd952 2024-03-22 stsp }
820 e70fd952 2024-03-22 stsp done:
821 e70fd952 2024-03-22 stsp if (!shut) {
822 e70fd952 2024-03-22 stsp gotd_imsg_event_add(iev);
823 e70fd952 2024-03-22 stsp } else {
824 e70fd952 2024-03-22 stsp /* This pipe is dead. Remove its event handler */
825 e70fd952 2024-03-22 stsp event_del(&iev->ev);
826 e70fd952 2024-03-22 stsp event_loopexit(NULL);
827 e70fd952 2024-03-22 stsp }
828 e70fd952 2024-03-22 stsp }
829 e70fd952 2024-03-22 stsp
830 e70fd952 2024-03-22 stsp void
831 e70fd952 2024-03-22 stsp session_read_main(const char *title, const char *repo_path,
832 e70fd952 2024-03-22 stsp int *pack_fds, int *temp_fds, struct timeval *request_timeout,
833 e70fd952 2024-03-22 stsp struct gotd_repo *repo_cfg)
834 e70fd952 2024-03-22 stsp {
835 e70fd952 2024-03-22 stsp const struct got_error *err = NULL;
836 e70fd952 2024-03-22 stsp struct event evsigint, evsigterm, evsighup, evsigusr1;
837 e70fd952 2024-03-22 stsp
838 e70fd952 2024-03-22 stsp gotd_session.title = title;
839 e70fd952 2024-03-22 stsp gotd_session.pid = getpid();
840 e70fd952 2024-03-22 stsp gotd_session.pack_fds = pack_fds;
841 e70fd952 2024-03-22 stsp gotd_session.temp_fds = temp_fds;
842 e70fd952 2024-03-22 stsp memcpy(&gotd_session.request_timeout, request_timeout,
843 e70fd952 2024-03-22 stsp sizeof(gotd_session.request_timeout));
844 e70fd952 2024-03-22 stsp gotd_session.repo_cfg = repo_cfg;
845 e70fd952 2024-03-22 stsp
846 e70fd952 2024-03-22 stsp imsg_init(&gotd_session.notifier_iev.ibuf, -1);
847 e70fd952 2024-03-22 stsp
848 e70fd952 2024-03-22 stsp err = got_repo_open(&gotd_session.repo, repo_path, NULL, pack_fds);
849 e70fd952 2024-03-22 stsp if (err)
850 e70fd952 2024-03-22 stsp goto done;
851 e70fd952 2024-03-22 stsp if (!got_repo_is_bare(gotd_session.repo)) {
852 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
853 e70fd952 2024-03-22 stsp "bare git repository required");
854 e70fd952 2024-03-22 stsp goto done;
855 e70fd952 2024-03-22 stsp }
856 e70fd952 2024-03-22 stsp
857 e70fd952 2024-03-22 stsp got_repo_temp_fds_set(gotd_session.repo, temp_fds);
858 e70fd952 2024-03-22 stsp
859 e70fd952 2024-03-22 stsp signal_set(&evsigint, SIGINT, session_read_sighdlr, NULL);
860 e70fd952 2024-03-22 stsp signal_set(&evsigterm, SIGTERM, session_read_sighdlr, NULL);
861 e70fd952 2024-03-22 stsp signal_set(&evsighup, SIGHUP, session_read_sighdlr, NULL);
862 e70fd952 2024-03-22 stsp signal_set(&evsigusr1, SIGUSR1, session_read_sighdlr, NULL);
863 e70fd952 2024-03-22 stsp signal(SIGPIPE, SIG_IGN);
864 e70fd952 2024-03-22 stsp
865 e70fd952 2024-03-22 stsp signal_add(&evsigint, NULL);
866 e70fd952 2024-03-22 stsp signal_add(&evsigterm, NULL);
867 e70fd952 2024-03-22 stsp signal_add(&evsighup, NULL);
868 e70fd952 2024-03-22 stsp signal_add(&evsigusr1, NULL);
869 e70fd952 2024-03-22 stsp
870 e70fd952 2024-03-22 stsp gotd_session.state = GOTD_STATE_EXPECT_LIST_REFS;
871 e70fd952 2024-03-22 stsp
872 e70fd952 2024-03-22 stsp gotd_session_client.fd = -1;
873 e70fd952 2024-03-22 stsp gotd_session_client.nref_updates = -1;
874 e70fd952 2024-03-22 stsp gotd_session_client.delta_cache_fd = -1;
875 e70fd952 2024-03-22 stsp gotd_session_client.accept_flush_pkt = 1;
876 e70fd952 2024-03-22 stsp
877 e70fd952 2024-03-22 stsp imsg_init(&gotd_session.parent_iev.ibuf, GOTD_FILENO_MSG_PIPE);
878 e70fd952 2024-03-22 stsp gotd_session.parent_iev.handler = session_dispatch;
879 e70fd952 2024-03-22 stsp gotd_session.parent_iev.events = EV_READ;
880 e70fd952 2024-03-22 stsp gotd_session.parent_iev.handler_arg = NULL;
881 e70fd952 2024-03-22 stsp event_set(&gotd_session.parent_iev.ev, gotd_session.parent_iev.ibuf.fd,
882 e70fd952 2024-03-22 stsp EV_READ, session_dispatch, &gotd_session.parent_iev);
883 e70fd952 2024-03-22 stsp if (gotd_imsg_compose_event(&gotd_session.parent_iev,
884 e70fd952 2024-03-22 stsp GOTD_IMSG_CLIENT_SESSION_READY, PROC_SESSION_READ,
885 e70fd952 2024-03-22 stsp -1, NULL, 0) == -1) {
886 e70fd952 2024-03-22 stsp err = got_error_from_errno("imsg compose CLIENT_SESSION_READY");
887 e70fd952 2024-03-22 stsp goto done;
888 e70fd952 2024-03-22 stsp }
889 e70fd952 2024-03-22 stsp
890 e70fd952 2024-03-22 stsp event_dispatch();
891 e70fd952 2024-03-22 stsp done:
892 e70fd952 2024-03-22 stsp if (err)
893 e70fd952 2024-03-22 stsp log_warnx("%s: %s", title, err->msg);
894 e70fd952 2024-03-22 stsp session_read_shutdown();
895 e70fd952 2024-03-22 stsp }
896 e70fd952 2024-03-22 stsp
897 e70fd952 2024-03-22 stsp static void
898 e70fd952 2024-03-22 stsp session_read_shutdown(void)
899 e70fd952 2024-03-22 stsp {
900 e8d451cc 2024-03-22 stsp log_debug("%s: shutting down", gotd_session.title);
901 e70fd952 2024-03-22 stsp
902 e70fd952 2024-03-22 stsp if (gotd_session.repo)
903 e70fd952 2024-03-22 stsp got_repo_close(gotd_session.repo);
904 e70fd952 2024-03-22 stsp got_repo_pack_fds_close(gotd_session.pack_fds);
905 e70fd952 2024-03-22 stsp got_repo_temp_fds_close(gotd_session.temp_fds);
906 e70fd952 2024-03-22 stsp free(gotd_session_client.username);
907 e70fd952 2024-03-22 stsp exit(0);
908 e70fd952 2024-03-22 stsp }