2 e70fd952 2024-03-22 stsp * Copyright (c) 2022, 2023 Stefan Sperling <stsp@openbsd.org>
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.
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.
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>
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>
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"
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"
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_write.h"
56 e70fd952 2024-03-22 stsp struct gotd_session_notif {
57 e70fd952 2024-03-22 stsp STAILQ_ENTRY(gotd_session_notif) entry;
59 e70fd952 2024-03-22 stsp enum gotd_notification_action action;
60 e70fd952 2024-03-22 stsp char *refname;
61 e70fd952 2024-03-22 stsp struct got_object_id old_id;
62 e70fd952 2024-03-22 stsp struct got_object_id new_id;
64 e70fd952 2024-03-22 stsp STAILQ_HEAD(gotd_session_notifications, gotd_session_notif) notifications;
66 e70fd952 2024-03-22 stsp enum gotd_session_write_state {
67 e70fd952 2024-03-22 stsp GOTD_STATE_EXPECT_LIST_REFS,
68 e70fd952 2024-03-22 stsp GOTD_STATE_EXPECT_CAPABILITIES,
69 e70fd952 2024-03-22 stsp GOTD_STATE_EXPECT_REF_UPDATE,
70 e70fd952 2024-03-22 stsp GOTD_STATE_EXPECT_MORE_REF_UPDATES,
71 e70fd952 2024-03-22 stsp GOTD_STATE_EXPECT_PACKFILE,
72 e70fd952 2024-03-22 stsp GOTD_STATE_NOTIFY,
75 e70fd952 2024-03-22 stsp static struct gotd_session_write {
77 e70fd952 2024-03-22 stsp const char *title;
78 e70fd952 2024-03-22 stsp struct got_repository *repo;
79 e70fd952 2024-03-22 stsp struct gotd_repo *repo_cfg;
80 e70fd952 2024-03-22 stsp int *pack_fds;
81 e70fd952 2024-03-22 stsp int *temp_fds;
82 e70fd952 2024-03-22 stsp struct gotd_imsgev parent_iev;
83 e70fd952 2024-03-22 stsp struct gotd_imsgev notifier_iev;
84 e70fd952 2024-03-22 stsp struct timeval request_timeout;
85 e70fd952 2024-03-22 stsp enum gotd_session_write_state state;
86 e70fd952 2024-03-22 stsp struct gotd_imsgev repo_child_iev;
87 e70fd952 2024-03-22 stsp } gotd_session;
89 e70fd952 2024-03-22 stsp static struct gotd_session_client {
90 e70fd952 2024-03-22 stsp struct gotd_client_capability *capabilities;
91 e70fd952 2024-03-22 stsp size_t ncapa_alloc;
92 e70fd952 2024-03-22 stsp size_t ncapabilities;
93 e70fd952 2024-03-22 stsp uint32_t id;
95 e70fd952 2024-03-22 stsp int delta_cache_fd;
96 e70fd952 2024-03-22 stsp struct gotd_imsgev iev;
97 e70fd952 2024-03-22 stsp struct event tmo;
100 e70fd952 2024-03-22 stsp char *username;
101 e70fd952 2024-03-22 stsp char *packfile_path;
102 e70fd952 2024-03-22 stsp char *packidx_path;
103 e70fd952 2024-03-22 stsp int nref_updates;
104 e70fd952 2024-03-22 stsp int accept_flush_pkt;
105 e70fd952 2024-03-22 stsp int flush_disconnect;
106 e70fd952 2024-03-22 stsp } gotd_session_client;
108 e70fd952 2024-03-22 stsp static void session_write_shutdown(void);
110 e70fd952 2024-03-22 stsp static void
111 e70fd952 2024-03-22 stsp disconnect(struct gotd_session_client *client)
113 e70fd952 2024-03-22 stsp log_debug("uid %d: disconnecting", client->euid);
115 e70fd952 2024-03-22 stsp if (gotd_imsg_compose_event(&gotd_session.parent_iev,
116 e70fd952 2024-03-22 stsp GOTD_IMSG_DISCONNECT, PROC_SESSION_WRITE, -1, NULL, 0) == -1)
117 e70fd952 2024-03-22 stsp log_warn("imsg compose DISCONNECT");
119 e70fd952 2024-03-22 stsp imsg_clear(&gotd_session.repo_child_iev.ibuf);
120 e70fd952 2024-03-22 stsp event_del(&gotd_session.repo_child_iev.ev);
121 e70fd952 2024-03-22 stsp evtimer_del(&client->tmo);
122 e70fd952 2024-03-22 stsp close(client->fd);
123 e70fd952 2024-03-22 stsp if (client->delta_cache_fd != -1)
124 e70fd952 2024-03-22 stsp close(client->delta_cache_fd);
125 e70fd952 2024-03-22 stsp if (client->packfile_path) {
126 e70fd952 2024-03-22 stsp if (unlink(client->packfile_path) == -1 && errno != ENOENT)
127 e70fd952 2024-03-22 stsp log_warn("unlink %s: ", client->packfile_path);
128 e70fd952 2024-03-22 stsp free(client->packfile_path);
130 e70fd952 2024-03-22 stsp if (client->packidx_path) {
131 e70fd952 2024-03-22 stsp if (unlink(client->packidx_path) == -1 && errno != ENOENT)
132 e70fd952 2024-03-22 stsp log_warn("unlink %s: ", client->packidx_path);
133 e70fd952 2024-03-22 stsp free(client->packidx_path);
135 e70fd952 2024-03-22 stsp free(client->capabilities);
137 e70fd952 2024-03-22 stsp session_write_shutdown();
140 e70fd952 2024-03-22 stsp static void
141 e70fd952 2024-03-22 stsp disconnect_on_error(struct gotd_session_client *client,
142 e70fd952 2024-03-22 stsp const struct got_error *err)
144 e70fd952 2024-03-22 stsp struct imsgbuf ibuf;
146 e70fd952 2024-03-22 stsp if (err->code != GOT_ERR_EOF) {
147 e70fd952 2024-03-22 stsp log_warnx("uid %d: %s", client->euid, err->msg);
148 e70fd952 2024-03-22 stsp imsg_init(&ibuf, client->fd);
149 e70fd952 2024-03-22 stsp gotd_imsg_send_error(&ibuf, 0, PROC_SESSION_WRITE, err);
150 e70fd952 2024-03-22 stsp imsg_clear(&ibuf);
153 e70fd952 2024-03-22 stsp disconnect(client);
156 e70fd952 2024-03-22 stsp static void
157 e70fd952 2024-03-22 stsp gotd_request_timeout(int fd, short events, void *arg)
159 e70fd952 2024-03-22 stsp struct gotd_session_client *client = arg;
161 7268d461 2024-05-01 stsp log_warnx("disconnecting uid %d due to timeout", client->euid);
162 e70fd952 2024-03-22 stsp disconnect(client);
165 e70fd952 2024-03-22 stsp static void
166 e70fd952 2024-03-22 stsp session_write_sighdlr(int sig, short event, void *arg)
169 e70fd952 2024-03-22 stsp * Normal signal handler rules don't apply because libevent
170 e70fd952 2024-03-22 stsp * decouples for us.
173 e70fd952 2024-03-22 stsp switch (sig) {
174 e70fd952 2024-03-22 stsp case SIGHUP:
175 e70fd952 2024-03-22 stsp log_info("%s: ignoring SIGHUP", __func__);
177 e70fd952 2024-03-22 stsp case SIGUSR1:
178 e70fd952 2024-03-22 stsp log_info("%s: ignoring SIGUSR1", __func__);
180 e70fd952 2024-03-22 stsp case SIGTERM:
181 e70fd952 2024-03-22 stsp case SIGINT:
182 e70fd952 2024-03-22 stsp session_write_shutdown();
183 e70fd952 2024-03-22 stsp /* NOTREACHED */
186 e70fd952 2024-03-22 stsp fatalx("unexpected signal");
190 e70fd952 2024-03-22 stsp static const struct got_error *
191 e70fd952 2024-03-22 stsp recv_packfile_install(struct imsg *imsg)
193 e70fd952 2024-03-22 stsp struct gotd_imsg_packfile_install inst;
194 e70fd952 2024-03-22 stsp size_t datalen;
196 e70fd952 2024-03-22 stsp log_debug("packfile-install received");
198 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
199 e70fd952 2024-03-22 stsp if (datalen != sizeof(inst))
200 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
201 e70fd952 2024-03-22 stsp memcpy(&inst, imsg->data, sizeof(inst));
203 e70fd952 2024-03-22 stsp return NULL;
206 e70fd952 2024-03-22 stsp static const struct got_error *
207 e70fd952 2024-03-22 stsp recv_ref_updates_start(struct imsg *imsg)
209 e70fd952 2024-03-22 stsp struct gotd_imsg_ref_updates_start istart;
210 e70fd952 2024-03-22 stsp size_t datalen;
212 e70fd952 2024-03-22 stsp log_debug("ref-updates-start received");
214 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
215 e70fd952 2024-03-22 stsp if (datalen != sizeof(istart))
216 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
217 e70fd952 2024-03-22 stsp memcpy(&istart, imsg->data, sizeof(istart));
219 e70fd952 2024-03-22 stsp return NULL;
222 e70fd952 2024-03-22 stsp static const struct got_error *
223 e70fd952 2024-03-22 stsp recv_ref_update(struct imsg *imsg)
225 e70fd952 2024-03-22 stsp struct gotd_imsg_ref_update iref;
226 e70fd952 2024-03-22 stsp size_t datalen;
228 e70fd952 2024-03-22 stsp log_debug("ref-update received");
230 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
231 e70fd952 2024-03-22 stsp if (datalen < sizeof(iref))
232 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
233 e70fd952 2024-03-22 stsp memcpy(&iref, imsg->data, sizeof(iref));
235 e70fd952 2024-03-22 stsp return NULL;
238 e70fd952 2024-03-22 stsp static const struct got_error *
239 e70fd952 2024-03-22 stsp send_ref_update_ok(struct gotd_session_client *client,
240 e70fd952 2024-03-22 stsp struct gotd_imsg_ref_update *iref, const char *refname)
242 e70fd952 2024-03-22 stsp struct gotd_imsg_ref_update_ok iok;
243 e70fd952 2024-03-22 stsp struct gotd_imsgev *iev = &client->iev;
244 e70fd952 2024-03-22 stsp struct ibuf *wbuf;
245 e70fd952 2024-03-22 stsp size_t len;
247 e70fd952 2024-03-22 stsp memset(&iok, 0, sizeof(iok));
248 e70fd952 2024-03-22 stsp memcpy(iok.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
249 e70fd952 2024-03-22 stsp memcpy(iok.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
250 e70fd952 2024-03-22 stsp iok.name_len = strlen(refname);
252 e70fd952 2024-03-22 stsp len = sizeof(iok) + iok.name_len;
253 e70fd952 2024-03-22 stsp wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE_OK,
254 e70fd952 2024-03-22 stsp PROC_SESSION_WRITE, gotd_session.pid, len);
255 e70fd952 2024-03-22 stsp if (wbuf == NULL)
256 e70fd952 2024-03-22 stsp return got_error_from_errno("imsg_create REF_UPDATE_OK");
258 e70fd952 2024-03-22 stsp if (imsg_add(wbuf, &iok, sizeof(iok)) == -1)
259 e70fd952 2024-03-22 stsp return got_error_from_errno("imsg_add REF_UPDATE_OK");
260 e70fd952 2024-03-22 stsp if (imsg_add(wbuf, refname, iok.name_len) == -1)
261 e70fd952 2024-03-22 stsp return got_error_from_errno("imsg_add REF_UPDATE_OK");
263 e70fd952 2024-03-22 stsp imsg_close(&iev->ibuf, wbuf);
264 e70fd952 2024-03-22 stsp gotd_imsg_event_add(iev);
265 e70fd952 2024-03-22 stsp return NULL;
268 e70fd952 2024-03-22 stsp static void
269 e70fd952 2024-03-22 stsp send_refs_updated(struct gotd_session_client *client)
271 e70fd952 2024-03-22 stsp if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_REFS_UPDATED,
272 e70fd952 2024-03-22 stsp PROC_SESSION_WRITE, -1, NULL, 0) == -1)
273 e70fd952 2024-03-22 stsp log_warn("imsg compose REFS_UPDATED");
276 e70fd952 2024-03-22 stsp static const struct got_error *
277 e70fd952 2024-03-22 stsp send_ref_update_ng(struct gotd_session_client *client,
278 e70fd952 2024-03-22 stsp struct gotd_imsg_ref_update *iref, const char *refname,
279 e70fd952 2024-03-22 stsp const char *reason)
281 e70fd952 2024-03-22 stsp const struct got_error *ng_err;
282 e70fd952 2024-03-22 stsp struct gotd_imsg_ref_update_ng ing;
283 e70fd952 2024-03-22 stsp struct gotd_imsgev *iev = &client->iev;
284 e70fd952 2024-03-22 stsp struct ibuf *wbuf;
285 e70fd952 2024-03-22 stsp size_t len;
287 e70fd952 2024-03-22 stsp memset(&ing, 0, sizeof(ing));
288 e70fd952 2024-03-22 stsp memcpy(ing.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
289 e70fd952 2024-03-22 stsp memcpy(ing.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
290 e70fd952 2024-03-22 stsp ing.name_len = strlen(refname);
292 e70fd952 2024-03-22 stsp ng_err = got_error_fmt(GOT_ERR_REF_BUSY, "%s", reason);
293 e70fd952 2024-03-22 stsp ing.reason_len = strlen(ng_err->msg);
295 e70fd952 2024-03-22 stsp len = sizeof(ing) + ing.name_len + ing.reason_len;
296 e70fd952 2024-03-22 stsp wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE_NG,
297 e70fd952 2024-03-22 stsp PROC_SESSION_WRITE, gotd_session.pid, len);
298 e70fd952 2024-03-22 stsp if (wbuf == NULL)
299 e70fd952 2024-03-22 stsp return got_error_from_errno("imsg_create REF_UPDATE_NG");
301 e70fd952 2024-03-22 stsp if (imsg_add(wbuf, &ing, sizeof(ing)) == -1)
302 e70fd952 2024-03-22 stsp return got_error_from_errno("imsg_add REF_UPDATE_NG");
303 e70fd952 2024-03-22 stsp if (imsg_add(wbuf, refname, ing.name_len) == -1)
304 e70fd952 2024-03-22 stsp return got_error_from_errno("imsg_add REF_UPDATE_NG");
305 e70fd952 2024-03-22 stsp if (imsg_add(wbuf, ng_err->msg, ing.reason_len) == -1)
306 e70fd952 2024-03-22 stsp return got_error_from_errno("imsg_add REF_UPDATE_NG");
308 e70fd952 2024-03-22 stsp imsg_close(&iev->ibuf, wbuf);
309 e70fd952 2024-03-22 stsp gotd_imsg_event_add(iev);
310 e70fd952 2024-03-22 stsp return NULL;
313 e70fd952 2024-03-22 stsp static const struct got_error *
314 e70fd952 2024-03-22 stsp install_pack(struct gotd_session_client *client, const char *repo_path,
315 e70fd952 2024-03-22 stsp struct imsg *imsg)
317 e70fd952 2024-03-22 stsp const struct got_error *err = NULL;
318 e70fd952 2024-03-22 stsp struct gotd_imsg_packfile_install inst;
319 e70fd952 2024-03-22 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
320 e70fd952 2024-03-22 stsp size_t datalen;
321 e70fd952 2024-03-22 stsp char *packfile_path = NULL, *packidx_path = NULL;
323 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
324 e70fd952 2024-03-22 stsp if (datalen != sizeof(inst))
325 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
326 e70fd952 2024-03-22 stsp memcpy(&inst, imsg->data, sizeof(inst));
328 e70fd952 2024-03-22 stsp if (client->packfile_path == NULL)
329 e70fd952 2024-03-22 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
330 e70fd952 2024-03-22 stsp "client has no pack file");
331 e70fd952 2024-03-22 stsp if (client->packidx_path == NULL)
332 e70fd952 2024-03-22 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
333 e70fd952 2024-03-22 stsp "client has no pack file index");
335 e70fd952 2024-03-22 stsp if (got_sha1_digest_to_str(inst.pack_sha1, hex, sizeof(hex)) == NULL)
336 e70fd952 2024-03-22 stsp return got_error_msg(GOT_ERR_NO_SPACE,
337 e70fd952 2024-03-22 stsp "could not convert pack file SHA1 to hex");
339 e70fd952 2024-03-22 stsp if (asprintf(&packfile_path, "/%s/%s/pack-%s.pack",
340 e70fd952 2024-03-22 stsp repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
341 e70fd952 2024-03-22 stsp err = got_error_from_errno("asprintf");
345 e70fd952 2024-03-22 stsp if (asprintf(&packidx_path, "/%s/%s/pack-%s.idx",
346 e70fd952 2024-03-22 stsp repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
347 e70fd952 2024-03-22 stsp err = got_error_from_errno("asprintf");
351 e70fd952 2024-03-22 stsp if (rename(client->packfile_path, packfile_path) == -1) {
352 e70fd952 2024-03-22 stsp err = got_error_from_errno3("rename", client->packfile_path,
353 e70fd952 2024-03-22 stsp packfile_path);
357 e70fd952 2024-03-22 stsp free(client->packfile_path);
358 e70fd952 2024-03-22 stsp client->packfile_path = NULL;
360 e70fd952 2024-03-22 stsp if (rename(client->packidx_path, packidx_path) == -1) {
361 e70fd952 2024-03-22 stsp err = got_error_from_errno3("rename", client->packidx_path,
362 e70fd952 2024-03-22 stsp packidx_path);
366 e70fd952 2024-03-22 stsp /* Ensure we re-read the pack index list upon next access. */
367 e70fd952 2024-03-22 stsp gotd_session.repo->pack_path_mtime.tv_sec = 0;
368 e70fd952 2024-03-22 stsp gotd_session.repo->pack_path_mtime.tv_nsec = 0;
370 e70fd952 2024-03-22 stsp free(client->packidx_path);
371 e70fd952 2024-03-22 stsp client->packidx_path = NULL;
373 e70fd952 2024-03-22 stsp free(packfile_path);
374 e70fd952 2024-03-22 stsp free(packidx_path);
375 e70fd952 2024-03-22 stsp return err;
378 e70fd952 2024-03-22 stsp static const struct got_error *
379 e70fd952 2024-03-22 stsp begin_ref_updates(struct gotd_session_client *client, struct imsg *imsg)
381 e70fd952 2024-03-22 stsp struct gotd_imsg_ref_updates_start istart;
382 e70fd952 2024-03-22 stsp size_t datalen;
384 e70fd952 2024-03-22 stsp if (client->nref_updates != -1)
385 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
387 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
388 e70fd952 2024-03-22 stsp if (datalen != sizeof(istart))
389 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
390 e70fd952 2024-03-22 stsp memcpy(&istart, imsg->data, sizeof(istart));
392 e70fd952 2024-03-22 stsp if (istart.nref_updates <= 0)
393 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
395 e70fd952 2024-03-22 stsp client->nref_updates = istart.nref_updates;
396 e70fd952 2024-03-22 stsp return NULL;
399 e70fd952 2024-03-22 stsp static const struct got_error *
400 e70fd952 2024-03-22 stsp validate_namespace(const char *namespace)
402 e70fd952 2024-03-22 stsp size_t len = strlen(namespace);
404 e70fd952 2024-03-22 stsp if (len < 5 || strncmp("refs/", namespace, 5) != 0 ||
405 e70fd952 2024-03-22 stsp namespace[len - 1] != '/') {
406 e70fd952 2024-03-22 stsp return got_error_fmt(GOT_ERR_BAD_REF_NAME,
407 e70fd952 2024-03-22 stsp "reference namespace '%s'", namespace);
410 e70fd952 2024-03-22 stsp return NULL;
413 e70fd952 2024-03-22 stsp static const struct got_error *
414 e70fd952 2024-03-22 stsp queue_notification(struct got_object_id *old_id, struct got_object_id *new_id,
415 e70fd952 2024-03-22 stsp struct got_repository *repo, struct got_reference *ref)
417 e70fd952 2024-03-22 stsp const struct got_error *err = NULL;
418 e70fd952 2024-03-22 stsp struct gotd_repo *repo_cfg = gotd_session.repo_cfg;
419 e70fd952 2024-03-22 stsp struct gotd_imsgev *iev = &gotd_session.repo_child_iev;
420 e70fd952 2024-03-22 stsp struct got_pathlist_entry *pe;
421 e70fd952 2024-03-22 stsp struct gotd_session_notif *notif;
423 e70fd952 2024-03-22 stsp if (iev->ibuf.fd == -1 ||
424 e70fd952 2024-03-22 stsp STAILQ_EMPTY(&repo_cfg->notification_targets))
425 e70fd952 2024-03-22 stsp return NULL; /* notifications unused */
427 e70fd952 2024-03-22 stsp TAILQ_FOREACH(pe, &repo_cfg->notification_refs, entry) {
428 e70fd952 2024-03-22 stsp const char *refname = pe->path;
429 e70fd952 2024-03-22 stsp if (strcmp(got_ref_get_name(ref), refname) == 0)
432 e70fd952 2024-03-22 stsp if (pe == NULL) {
433 e70fd952 2024-03-22 stsp TAILQ_FOREACH(pe, &repo_cfg->notification_ref_namespaces,
435 e70fd952 2024-03-22 stsp const char *namespace = pe->path;
437 e70fd952 2024-03-22 stsp err = validate_namespace(namespace);
439 e70fd952 2024-03-22 stsp return err;
440 e70fd952 2024-03-22 stsp if (strncmp(namespace, got_ref_get_name(ref),
441 e70fd952 2024-03-22 stsp strlen(namespace)) == 0)
447 e70fd952 2024-03-22 stsp * If a branch or a reference namespace was specified in the
448 e70fd952 2024-03-22 stsp * configuration file then only send notifications if a match
449 e70fd952 2024-03-22 stsp * was found.
451 e70fd952 2024-03-22 stsp if (pe == NULL && (!TAILQ_EMPTY(&repo_cfg->notification_refs) ||
452 e70fd952 2024-03-22 stsp !TAILQ_EMPTY(&repo_cfg->notification_ref_namespaces)))
453 e70fd952 2024-03-22 stsp return NULL;
455 e70fd952 2024-03-22 stsp notif = calloc(1, sizeof(*notif));
456 e70fd952 2024-03-22 stsp if (notif == NULL)
457 e70fd952 2024-03-22 stsp return got_error_from_errno("calloc");
459 e70fd952 2024-03-22 stsp notif->fd = -1;
461 e70fd952 2024-03-22 stsp if (old_id == NULL)
462 e70fd952 2024-03-22 stsp notif->action = GOTD_NOTIF_ACTION_CREATED;
463 e70fd952 2024-03-22 stsp else if (new_id == NULL)
464 e70fd952 2024-03-22 stsp notif->action = GOTD_NOTIF_ACTION_REMOVED;
466 e70fd952 2024-03-22 stsp notif->action = GOTD_NOTIF_ACTION_CHANGED;
468 e70fd952 2024-03-22 stsp if (old_id != NULL)
469 e70fd952 2024-03-22 stsp memcpy(¬if->old_id, old_id, sizeof(notif->old_id));
470 e70fd952 2024-03-22 stsp if (new_id != NULL)
471 e70fd952 2024-03-22 stsp memcpy(¬if->new_id, new_id, sizeof(notif->new_id));
473 e70fd952 2024-03-22 stsp notif->refname = strdup(got_ref_get_name(ref));
474 e70fd952 2024-03-22 stsp if (notif->refname == NULL) {
475 e70fd952 2024-03-22 stsp err = got_error_from_errno("strdup");
479 e70fd952 2024-03-22 stsp STAILQ_INSERT_TAIL(¬ifications, notif, entry);
481 e70fd952 2024-03-22 stsp if (err && notif) {
482 e70fd952 2024-03-22 stsp free(notif->refname);
483 e70fd952 2024-03-22 stsp free(notif);
485 e70fd952 2024-03-22 stsp return err;
488 e70fd952 2024-03-22 stsp /* Forward notification content to the NOTIFY process. */
489 e70fd952 2024-03-22 stsp static const struct got_error *
490 e70fd952 2024-03-22 stsp forward_notification(struct gotd_session_client *client, struct imsg *imsg)
492 e70fd952 2024-03-22 stsp const struct got_error *err = NULL;
493 e70fd952 2024-03-22 stsp struct gotd_imsgev *iev = &gotd_session.notifier_iev;
494 e70fd952 2024-03-22 stsp struct gotd_session_notif *notif;
495 e70fd952 2024-03-22 stsp struct gotd_imsg_notification_content icontent;
496 1ddd9d55 2024-05-10 stsp char *refname = NULL, *id_str = NULL;
497 e70fd952 2024-03-22 stsp size_t datalen;
498 e70fd952 2024-03-22 stsp struct gotd_imsg_notify inotify;
499 e70fd952 2024-03-22 stsp const char *action;
500 d36998ae 2024-04-29 stsp struct ibuf *wbuf;
502 e70fd952 2024-03-22 stsp memset(&inotify, 0, sizeof(inotify));
504 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
505 e70fd952 2024-03-22 stsp if (datalen < sizeof(icontent))
506 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
507 e70fd952 2024-03-22 stsp memcpy(&icontent, imsg->data, sizeof(icontent));
508 e70fd952 2024-03-22 stsp if (datalen != sizeof(icontent) + icontent.refname_len)
509 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
510 e70fd952 2024-03-22 stsp refname = strndup(imsg->data + sizeof(icontent), icontent.refname_len);
511 e70fd952 2024-03-22 stsp if (refname == NULL)
512 e70fd952 2024-03-22 stsp return got_error_from_errno("strndup");
514 e70fd952 2024-03-22 stsp notif = STAILQ_FIRST(¬ifications);
515 e70fd952 2024-03-22 stsp if (notif == NULL)
516 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
518 e70fd952 2024-03-22 stsp STAILQ_REMOVE(¬ifications, notif, gotd_session_notif, entry);
520 e70fd952 2024-03-22 stsp if (notif->action != icontent.action || notif->fd == -1 ||
521 e70fd952 2024-03-22 stsp strcmp(notif->refname, refname) != 0) {
522 e70fd952 2024-03-22 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
525 e70fd952 2024-03-22 stsp if (notif->action == GOTD_NOTIF_ACTION_CREATED) {
526 1b1a386d 2024-07-09 op if (memcmp(¬if->new_id, &icontent.new_id,
527 1b1a386d 2024-07-09 op sizeof(notif->new_id)) != 0) {
528 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_PRIVSEP_MSG,
529 e70fd952 2024-03-22 stsp "received notification content for unknown event");
532 e70fd952 2024-03-22 stsp } else if (notif->action == GOTD_NOTIF_ACTION_REMOVED) {
533 1b1a386d 2024-07-09 op if (memcmp(¬if->old_id, &icontent.old_id,
534 1b1a386d 2024-07-09 op sizeof(notif->old_id)) != 0) {
535 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_PRIVSEP_MSG,
536 e70fd952 2024-03-22 stsp "received notification content for unknown event");
539 1b1a386d 2024-07-09 op } else if (memcmp(¬if->old_id, &icontent.old_id,
540 1b1a386d 2024-07-09 op sizeof(notif->old_id)) != 0 ||
541 1b1a386d 2024-07-09 op memcmp(¬if->new_id, &icontent.new_id,
542 1b1a386d 2024-07-09 op sizeof(notif->old_id)) != 0) {
543 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_PRIVSEP_MSG,
544 e70fd952 2024-03-22 stsp "received notification content for unknown event");
548 e70fd952 2024-03-22 stsp switch (notif->action) {
549 e70fd952 2024-03-22 stsp case GOTD_NOTIF_ACTION_CREATED:
550 e70fd952 2024-03-22 stsp action = "created";
551 1ddd9d55 2024-05-10 stsp err = got_object_id_str(&id_str, ¬if->new_id);
555 e70fd952 2024-03-22 stsp case GOTD_NOTIF_ACTION_REMOVED:
556 e70fd952 2024-03-22 stsp action = "removed";
557 1ddd9d55 2024-05-10 stsp err = got_object_id_str(&id_str, ¬if->old_id);
561 e70fd952 2024-03-22 stsp case GOTD_NOTIF_ACTION_CHANGED:
562 e70fd952 2024-03-22 stsp action = "changed";
563 1ddd9d55 2024-05-10 stsp err = got_object_id_str(&id_str, ¬if->new_id);
568 e70fd952 2024-03-22 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
572 e70fd952 2024-03-22 stsp strlcpy(inotify.repo_name, gotd_session.repo_cfg->name,
573 e70fd952 2024-03-22 stsp sizeof(inotify.repo_name));
575 e70fd952 2024-03-22 stsp snprintf(inotify.subject_line, sizeof(inotify.subject_line),
576 1ddd9d55 2024-05-10 stsp "%s: %s %s %s: %.12s", gotd_session.repo_cfg->name,
577 1ddd9d55 2024-05-10 stsp client->username, action, notif->refname, id_str);
579 d36998ae 2024-04-29 stsp inotify.username_len = strlen(client->username);
580 d36998ae 2024-04-29 stsp wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_NOTIFY,
581 d36998ae 2024-04-29 stsp PROC_SESSION_WRITE, gotd_session.pid,
582 d36998ae 2024-04-29 stsp sizeof(inotify) + inotify.username_len);
583 d36998ae 2024-04-29 stsp if (wbuf == NULL) {
584 d36998ae 2024-04-29 stsp err = got_error_from_errno("imsg_create NOTIFY");
587 d36998ae 2024-04-29 stsp if (imsg_add(wbuf, &inotify, sizeof(inotify)) == -1) {
588 d36998ae 2024-04-29 stsp err = got_error_from_errno("imsg_add NOTIFY");
591 d36998ae 2024-04-29 stsp if (imsg_add(wbuf, client->username, inotify.username_len) == -1) {
592 d36998ae 2024-04-29 stsp err = got_error_from_errno("imsg_add NOTIFY");
596 d36998ae 2024-04-29 stsp ibuf_fd_set(wbuf, notif->fd);
597 e70fd952 2024-03-22 stsp notif->fd = -1;
599 d36998ae 2024-04-29 stsp imsg_close(&iev->ibuf, wbuf);
600 d36998ae 2024-04-29 stsp gotd_imsg_event_add(iev);
602 e70fd952 2024-03-22 stsp if (notif->fd != -1)
603 e70fd952 2024-03-22 stsp close(notif->fd);
604 e70fd952 2024-03-22 stsp free(notif);
605 e70fd952 2024-03-22 stsp free(refname);
606 1ddd9d55 2024-05-10 stsp free(id_str);
607 e70fd952 2024-03-22 stsp return err;
610 e70fd952 2024-03-22 stsp /* Request notification content from REPO_WRITE process. */
611 e70fd952 2024-03-22 stsp static const struct got_error *
612 e70fd952 2024-03-22 stsp request_notification(struct gotd_session_notif *notif)
614 e70fd952 2024-03-22 stsp const struct got_error *err = NULL;
615 e70fd952 2024-03-22 stsp struct gotd_imsgev *iev = &gotd_session.repo_child_iev;
616 e70fd952 2024-03-22 stsp struct gotd_imsg_notification_content icontent;
617 e70fd952 2024-03-22 stsp struct ibuf *wbuf;
618 e70fd952 2024-03-22 stsp size_t len;
621 e70fd952 2024-03-22 stsp fd = got_opentempfd();
622 e70fd952 2024-03-22 stsp if (fd == -1)
623 e70fd952 2024-03-22 stsp return got_error_from_errno("got_opentemp");
625 e70fd952 2024-03-22 stsp memset(&icontent, 0, sizeof(icontent));
627 e70fd952 2024-03-22 stsp icontent.action = notif->action;
628 e70fd952 2024-03-22 stsp memcpy(&icontent.old_id, ¬if->old_id, sizeof(notif->old_id));
629 e70fd952 2024-03-22 stsp memcpy(&icontent.new_id, ¬if->new_id, sizeof(notif->new_id));
630 e70fd952 2024-03-22 stsp icontent.refname_len = strlen(notif->refname);
632 e70fd952 2024-03-22 stsp len = sizeof(icontent) + icontent.refname_len;
633 e70fd952 2024-03-22 stsp wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_NOTIFY,
634 e70fd952 2024-03-22 stsp PROC_SESSION_WRITE, gotd_session.pid, len);
635 e70fd952 2024-03-22 stsp if (wbuf == NULL) {
636 e70fd952 2024-03-22 stsp err = got_error_from_errno("imsg_create NOTIFY");
639 e70fd952 2024-03-22 stsp if (imsg_add(wbuf, &icontent, sizeof(icontent)) == -1) {
640 e70fd952 2024-03-22 stsp err = got_error_from_errno("imsg_add NOTIFY");
643 e70fd952 2024-03-22 stsp if (imsg_add(wbuf, notif->refname, icontent.refname_len) == -1) {
644 e70fd952 2024-03-22 stsp err = got_error_from_errno("imsg_add NOTIFY");
648 e70fd952 2024-03-22 stsp notif->fd = dup(fd);
649 e70fd952 2024-03-22 stsp if (notif->fd == -1) {
650 e70fd952 2024-03-22 stsp err = got_error_from_errno("dup");
654 e70fd952 2024-03-22 stsp ibuf_fd_set(wbuf, fd);
657 e70fd952 2024-03-22 stsp imsg_close(&iev->ibuf, wbuf);
658 e70fd952 2024-03-22 stsp gotd_imsg_event_add(iev);
660 e70fd952 2024-03-22 stsp if (err && fd != -1)
662 e70fd952 2024-03-22 stsp return err;
665 e70fd952 2024-03-22 stsp static const struct got_error *
666 e70fd952 2024-03-22 stsp update_ref(int *shut, struct gotd_session_client *client,
667 e70fd952 2024-03-22 stsp const char *repo_path, struct imsg *imsg)
669 e70fd952 2024-03-22 stsp const struct got_error *err = NULL;
670 e70fd952 2024-03-22 stsp struct got_repository *repo = gotd_session.repo;
671 e70fd952 2024-03-22 stsp struct got_reference *ref = NULL;
672 e70fd952 2024-03-22 stsp struct gotd_imsg_ref_update iref;
673 e70fd952 2024-03-22 stsp struct got_object_id old_id, new_id;
674 e70fd952 2024-03-22 stsp struct gotd_session_notif *notif;
675 e70fd952 2024-03-22 stsp struct got_object_id *id = NULL;
676 e70fd952 2024-03-22 stsp char *refname = NULL;
677 e70fd952 2024-03-22 stsp size_t datalen;
678 e70fd952 2024-03-22 stsp int locked = 0;
679 e70fd952 2024-03-22 stsp char hex1[SHA1_DIGEST_STRING_LENGTH];
680 e70fd952 2024-03-22 stsp char hex2[SHA1_DIGEST_STRING_LENGTH];
682 e70fd952 2024-03-22 stsp log_debug("update-ref from uid %d", client->euid);
684 e70fd952 2024-03-22 stsp if (client->nref_updates <= 0)
685 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
687 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
688 e70fd952 2024-03-22 stsp if (datalen < sizeof(iref))
689 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
690 e70fd952 2024-03-22 stsp memcpy(&iref, imsg->data, sizeof(iref));
691 e70fd952 2024-03-22 stsp if (datalen != sizeof(iref) + iref.name_len)
692 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
693 e70fd952 2024-03-22 stsp refname = strndup(imsg->data + sizeof(iref), iref.name_len);
694 e70fd952 2024-03-22 stsp if (refname == NULL)
695 e70fd952 2024-03-22 stsp return got_error_from_errno("strndup");
697 e70fd952 2024-03-22 stsp log_debug("updating ref %s for uid %d", refname, client->euid);
699 be12ea2c 2024-07-11 op memset(&old_id, 0, sizeof(old_id));
700 aaa34732 2024-07-13 op memcpy(old_id.hash, iref.old_id, SHA1_DIGEST_LENGTH);
701 be12ea2c 2024-07-11 op memset(&new_id, 0, sizeof(new_id));
702 aaa34732 2024-07-13 op memcpy(new_id.hash, iref.new_id, SHA1_DIGEST_LENGTH);
703 e70fd952 2024-03-22 stsp err = got_repo_find_object_id(iref.delete_ref ? &old_id : &new_id,
708 e70fd952 2024-03-22 stsp if (iref.ref_is_new) {
709 e70fd952 2024-03-22 stsp err = got_ref_open(&ref, repo, refname, 0);
711 e70fd952 2024-03-22 stsp if (err->code != GOT_ERR_NOT_REF)
713 e70fd952 2024-03-22 stsp err = got_ref_alloc(&ref, refname, &new_id);
716 e70fd952 2024-03-22 stsp err = got_ref_write(ref, repo); /* will lock/unlock */
719 e70fd952 2024-03-22 stsp err = queue_notification(NULL, &new_id, repo, ref);
723 e70fd952 2024-03-22 stsp err = got_ref_resolve(&id, repo, ref);
726 e70fd952 2024-03-22 stsp got_object_id_hex(&new_id, hex1, sizeof(hex1));
727 e70fd952 2024-03-22 stsp got_object_id_hex(id, hex2, sizeof(hex2));
728 e70fd952 2024-03-22 stsp err = got_error_fmt(GOT_ERR_REF_BUSY,
729 e70fd952 2024-03-22 stsp "Addition %s: %s failed; %s: %s has been "
730 e70fd952 2024-03-22 stsp "created by someone else while transaction "
731 e70fd952 2024-03-22 stsp "was in progress",
732 e70fd952 2024-03-22 stsp got_ref_get_name(ref), hex1,
733 e70fd952 2024-03-22 stsp got_ref_get_name(ref), hex2);
736 e70fd952 2024-03-22 stsp } else if (iref.delete_ref) {
737 e70fd952 2024-03-22 stsp err = got_ref_open(&ref, repo, refname, 1 /* lock */);
740 e70fd952 2024-03-22 stsp locked = 1;
742 e70fd952 2024-03-22 stsp err = got_ref_resolve(&id, repo, ref);
746 e70fd952 2024-03-22 stsp if (got_object_id_cmp(id, &old_id) != 0) {
747 e70fd952 2024-03-22 stsp got_object_id_hex(&old_id, hex1, sizeof(hex1));
748 e70fd952 2024-03-22 stsp got_object_id_hex(id, hex2, sizeof(hex2));
749 e70fd952 2024-03-22 stsp err = got_error_fmt(GOT_ERR_REF_BUSY,
750 e70fd952 2024-03-22 stsp "Deletion %s: %s failed; %s: %s has been "
751 e70fd952 2024-03-22 stsp "created by someone else while transaction "
752 e70fd952 2024-03-22 stsp "was in progress",
753 e70fd952 2024-03-22 stsp got_ref_get_name(ref), hex1,
754 e70fd952 2024-03-22 stsp got_ref_get_name(ref), hex2);
758 e70fd952 2024-03-22 stsp err = got_ref_delete(ref, repo);
761 e70fd952 2024-03-22 stsp err = queue_notification(&old_id, NULL, repo, ref);
767 e70fd952 2024-03-22 stsp err = got_ref_open(&ref, repo, refname, 1 /* lock */);
770 e70fd952 2024-03-22 stsp locked = 1;
772 e70fd952 2024-03-22 stsp err = got_ref_resolve(&id, repo, ref);
776 e70fd952 2024-03-22 stsp if (got_object_id_cmp(id, &old_id) != 0) {
777 e70fd952 2024-03-22 stsp got_object_id_hex(&old_id, hex1, sizeof(hex1));
778 e70fd952 2024-03-22 stsp got_object_id_hex(id, hex2, sizeof(hex2));
779 e70fd952 2024-03-22 stsp err = got_error_fmt(GOT_ERR_REF_BUSY,
780 e70fd952 2024-03-22 stsp "Update %s: %s failed; %s: %s has been "
781 e70fd952 2024-03-22 stsp "created by someone else while transaction "
782 e70fd952 2024-03-22 stsp "was in progress",
783 e70fd952 2024-03-22 stsp got_ref_get_name(ref), hex1,
784 e70fd952 2024-03-22 stsp got_ref_get_name(ref), hex2);
788 e70fd952 2024-03-22 stsp if (got_object_id_cmp(&new_id, &old_id) != 0) {
789 e70fd952 2024-03-22 stsp err = got_ref_change_ref(ref, &new_id);
792 e70fd952 2024-03-22 stsp err = got_ref_write(ref, repo);
795 e70fd952 2024-03-22 stsp err = queue_notification(&old_id, &new_id, repo, ref);
805 e70fd952 2024-03-22 stsp if (err->code == GOT_ERR_LOCKFILE_TIMEOUT) {
806 e70fd952 2024-03-22 stsp err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
807 e70fd952 2024-03-22 stsp "could not acquire exclusive file lock for %s",
810 e70fd952 2024-03-22 stsp send_ref_update_ng(client, &iref, refname, err->msg);
812 e70fd952 2024-03-22 stsp send_ref_update_ok(client, &iref, refname);
814 e70fd952 2024-03-22 stsp if (client->nref_updates > 0) {
815 e70fd952 2024-03-22 stsp client->nref_updates--;
816 e70fd952 2024-03-22 stsp if (client->nref_updates == 0) {
817 e70fd952 2024-03-22 stsp send_refs_updated(client);
818 e70fd952 2024-03-22 stsp notif = STAILQ_FIRST(¬ifications);
819 e70fd952 2024-03-22 stsp if (notif) {
820 e70fd952 2024-03-22 stsp gotd_session.state = GOTD_STATE_NOTIFY;
821 e70fd952 2024-03-22 stsp err = request_notification(notif);
823 e70fd952 2024-03-22 stsp log_warn("could not send notification: "
824 e70fd952 2024-03-22 stsp "%s", err->msg);
825 e70fd952 2024-03-22 stsp client->flush_disconnect = 1;
828 e70fd952 2024-03-22 stsp client->flush_disconnect = 1;
832 e70fd952 2024-03-22 stsp if (locked) {
833 e70fd952 2024-03-22 stsp const struct got_error *unlock_err;
834 e70fd952 2024-03-22 stsp unlock_err = got_ref_unlock(ref);
835 e70fd952 2024-03-22 stsp if (unlock_err && err == NULL)
836 e70fd952 2024-03-22 stsp err = unlock_err;
839 e70fd952 2024-03-22 stsp got_ref_close(ref);
840 e70fd952 2024-03-22 stsp free(refname);
842 e70fd952 2024-03-22 stsp return err;
845 e70fd952 2024-03-22 stsp static const struct got_error *
846 e70fd952 2024-03-22 stsp recv_notification_content(struct imsg *imsg)
848 e70fd952 2024-03-22 stsp struct gotd_imsg_notification_content inotif;
849 e70fd952 2024-03-22 stsp size_t datalen;
851 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
852 e70fd952 2024-03-22 stsp if (datalen < sizeof(inotif))
853 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
854 e70fd952 2024-03-22 stsp memcpy(&inotif, imsg->data, sizeof(inotif));
856 e70fd952 2024-03-22 stsp return NULL;
859 e70fd952 2024-03-22 stsp static void
860 e70fd952 2024-03-22 stsp session_dispatch_repo_child(int fd, short event, void *arg)
862 e70fd952 2024-03-22 stsp struct gotd_imsgev *iev = arg;
863 e70fd952 2024-03-22 stsp struct imsgbuf *ibuf = &iev->ibuf;
864 e70fd952 2024-03-22 stsp struct gotd_session_client *client = &gotd_session_client;
866 e70fd952 2024-03-22 stsp int shut = 0;
867 e70fd952 2024-03-22 stsp struct imsg imsg;
869 e70fd952 2024-03-22 stsp if (event & EV_READ) {
870 e70fd952 2024-03-22 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
871 e70fd952 2024-03-22 stsp fatal("imsg_read error");
872 e70fd952 2024-03-22 stsp if (n == 0) {
873 e70fd952 2024-03-22 stsp /* Connection closed. */
879 e70fd952 2024-03-22 stsp if (event & EV_WRITE) {
880 e70fd952 2024-03-22 stsp n = msgbuf_write(&ibuf->w);
881 e70fd952 2024-03-22 stsp if (n == -1 && errno != EAGAIN)
882 e70fd952 2024-03-22 stsp fatal("msgbuf_write");
883 e70fd952 2024-03-22 stsp if (n == 0) {
884 e70fd952 2024-03-22 stsp /* Connection closed. */
891 e70fd952 2024-03-22 stsp const struct got_error *err = NULL;
892 e70fd952 2024-03-22 stsp uint32_t client_id = 0;
893 e70fd952 2024-03-22 stsp int do_disconnect = 0;
894 e70fd952 2024-03-22 stsp int do_ref_updates = 0, do_ref_update = 0;
895 e70fd952 2024-03-22 stsp int do_packfile_install = 0, do_notify = 0;
897 e70fd952 2024-03-22 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
898 e70fd952 2024-03-22 stsp fatal("%s: imsg_get error", __func__);
899 e70fd952 2024-03-22 stsp if (n == 0) /* No more messages. */
902 e70fd952 2024-03-22 stsp switch (imsg.hdr.type) {
903 e70fd952 2024-03-22 stsp case GOTD_IMSG_ERROR:
904 e70fd952 2024-03-22 stsp do_disconnect = 1;
905 e70fd952 2024-03-22 stsp err = gotd_imsg_recv_error(&client_id, &imsg);
907 e70fd952 2024-03-22 stsp case GOTD_IMSG_PACKFILE_INSTALL:
908 e70fd952 2024-03-22 stsp err = recv_packfile_install(&imsg);
909 e70fd952 2024-03-22 stsp if (err == NULL)
910 e70fd952 2024-03-22 stsp do_packfile_install = 1;
912 e70fd952 2024-03-22 stsp case GOTD_IMSG_REF_UPDATES_START:
913 e70fd952 2024-03-22 stsp err = recv_ref_updates_start(&imsg);
914 e70fd952 2024-03-22 stsp if (err == NULL)
915 e70fd952 2024-03-22 stsp do_ref_updates = 1;
917 e70fd952 2024-03-22 stsp case GOTD_IMSG_REF_UPDATE:
918 e70fd952 2024-03-22 stsp err = recv_ref_update(&imsg);
919 e70fd952 2024-03-22 stsp if (err == NULL)
920 e70fd952 2024-03-22 stsp do_ref_update = 1;
922 e70fd952 2024-03-22 stsp case GOTD_IMSG_NOTIFY:
923 e70fd952 2024-03-22 stsp err = recv_notification_content(&imsg);
924 e70fd952 2024-03-22 stsp if (err == NULL)
925 e70fd952 2024-03-22 stsp do_notify = 1;
928 e70fd952 2024-03-22 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
932 3bdb5066 2024-04-16 op if (do_disconnect || err) {
934 e70fd952 2024-03-22 stsp disconnect_on_error(client, err);
936 e70fd952 2024-03-22 stsp disconnect(client);
938 e70fd952 2024-03-22 stsp struct gotd_session_notif *notif;
940 e70fd952 2024-03-22 stsp if (do_packfile_install)
941 e70fd952 2024-03-22 stsp err = install_pack(client,
942 e70fd952 2024-03-22 stsp gotd_session.repo->path, &imsg);
943 e70fd952 2024-03-22 stsp else if (do_ref_updates)
944 e70fd952 2024-03-22 stsp err = begin_ref_updates(client, &imsg);
945 e70fd952 2024-03-22 stsp else if (do_ref_update)
946 e70fd952 2024-03-22 stsp err = update_ref(&shut, client,
947 e70fd952 2024-03-22 stsp gotd_session.repo->path, &imsg);
948 e70fd952 2024-03-22 stsp else if (do_notify)
949 e70fd952 2024-03-22 stsp err = forward_notification(client, &imsg);
951 e70fd952 2024-03-22 stsp log_warnx("uid %d: %s", client->euid, err->msg);
953 e70fd952 2024-03-22 stsp notif = STAILQ_FIRST(¬ifications);
954 e70fd952 2024-03-22 stsp if (notif && do_notify) {
955 e70fd952 2024-03-22 stsp /* Request content for next notification. */
956 e70fd952 2024-03-22 stsp err = request_notification(notif);
958 e70fd952 2024-03-22 stsp log_warn("could not send notification: "
959 e70fd952 2024-03-22 stsp "%s", err->msg);
964 e70fd952 2024-03-22 stsp imsg_free(&imsg);
967 e70fd952 2024-03-22 stsp if (!shut) {
968 e70fd952 2024-03-22 stsp gotd_imsg_event_add(iev);
970 e70fd952 2024-03-22 stsp /* This pipe is dead. Remove its event handler */
971 e70fd952 2024-03-22 stsp event_del(&iev->ev);
972 e70fd952 2024-03-22 stsp event_loopexit(NULL);
976 e70fd952 2024-03-22 stsp static const struct got_error *
977 e70fd952 2024-03-22 stsp recv_capabilities(struct gotd_session_client *client, struct imsg *imsg)
979 e70fd952 2024-03-22 stsp struct gotd_imsg_capabilities icapas;
980 e70fd952 2024-03-22 stsp size_t datalen;
982 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
983 e70fd952 2024-03-22 stsp if (datalen != sizeof(icapas))
984 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
985 e70fd952 2024-03-22 stsp memcpy(&icapas, imsg->data, sizeof(icapas));
987 e70fd952 2024-03-22 stsp client->ncapa_alloc = icapas.ncapabilities;
988 e70fd952 2024-03-22 stsp client->capabilities = calloc(client->ncapa_alloc,
989 e70fd952 2024-03-22 stsp sizeof(*client->capabilities));
990 e70fd952 2024-03-22 stsp if (client->capabilities == NULL) {
991 e70fd952 2024-03-22 stsp client->ncapa_alloc = 0;
992 e70fd952 2024-03-22 stsp return got_error_from_errno("calloc");
995 e70fd952 2024-03-22 stsp log_debug("expecting %zu capabilities from uid %d",
996 e70fd952 2024-03-22 stsp client->ncapa_alloc, client->euid);
997 e70fd952 2024-03-22 stsp return NULL;
1000 e70fd952 2024-03-22 stsp static const struct got_error *
1001 e70fd952 2024-03-22 stsp recv_capability(struct gotd_session_client *client, struct imsg *imsg)
1003 e70fd952 2024-03-22 stsp struct gotd_imsg_capability icapa;
1004 e70fd952 2024-03-22 stsp struct gotd_client_capability *capa;
1005 e70fd952 2024-03-22 stsp size_t datalen;
1006 e70fd952 2024-03-22 stsp char *key, *value = NULL;
1008 e70fd952 2024-03-22 stsp if (client->capabilities == NULL ||
1009 e70fd952 2024-03-22 stsp client->ncapabilities >= client->ncapa_alloc) {
1010 e70fd952 2024-03-22 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
1011 e70fd952 2024-03-22 stsp "unexpected capability received");
1014 e70fd952 2024-03-22 stsp memset(&icapa, 0, sizeof(icapa));
1016 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1017 e70fd952 2024-03-22 stsp if (datalen < sizeof(icapa))
1018 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1019 e70fd952 2024-03-22 stsp memcpy(&icapa, imsg->data, sizeof(icapa));
1021 e70fd952 2024-03-22 stsp if (datalen != sizeof(icapa) + icapa.key_len + icapa.value_len)
1022 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1024 e70fd952 2024-03-22 stsp key = strndup(imsg->data + sizeof(icapa), icapa.key_len);
1025 e70fd952 2024-03-22 stsp if (key == NULL)
1026 e70fd952 2024-03-22 stsp return got_error_from_errno("strndup");
1027 e70fd952 2024-03-22 stsp if (icapa.value_len > 0) {
1028 e70fd952 2024-03-22 stsp value = strndup(imsg->data + sizeof(icapa) + icapa.key_len,
1029 e70fd952 2024-03-22 stsp icapa.value_len);
1030 e70fd952 2024-03-22 stsp if (value == NULL) {
1031 e70fd952 2024-03-22 stsp free(key);
1032 e70fd952 2024-03-22 stsp return got_error_from_errno("strndup");
1036 e70fd952 2024-03-22 stsp capa = &client->capabilities[client->ncapabilities++];
1037 e70fd952 2024-03-22 stsp capa->key = key;
1038 e70fd952 2024-03-22 stsp capa->value = value;
1040 e70fd952 2024-03-22 stsp if (value)
1041 e70fd952 2024-03-22 stsp log_debug("uid %d: capability %s=%s", client->euid, key, value);
1043 e70fd952 2024-03-22 stsp log_debug("uid %d: capability %s", client->euid, key);
1045 e70fd952 2024-03-22 stsp return NULL;
1048 e70fd952 2024-03-22 stsp static const struct got_error *
1049 e70fd952 2024-03-22 stsp forward_ref_update(struct gotd_session_client *client, struct imsg *imsg)
1051 e70fd952 2024-03-22 stsp const struct got_error *err = NULL;
1052 e70fd952 2024-03-22 stsp struct gotd_imsg_ref_update ireq;
1053 e70fd952 2024-03-22 stsp struct gotd_imsg_ref_update *iref = NULL;
1054 e70fd952 2024-03-22 stsp size_t datalen;
1056 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1057 e70fd952 2024-03-22 stsp if (datalen < sizeof(ireq))
1058 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1059 e70fd952 2024-03-22 stsp memcpy(&ireq, imsg->data, sizeof(ireq));
1060 e70fd952 2024-03-22 stsp if (datalen != sizeof(ireq) + ireq.name_len)
1061 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1063 e70fd952 2024-03-22 stsp iref = malloc(datalen);
1064 e70fd952 2024-03-22 stsp if (iref == NULL)
1065 e70fd952 2024-03-22 stsp return got_error_from_errno("malloc");
1066 e70fd952 2024-03-22 stsp memcpy(iref, imsg->data, datalen);
1068 e70fd952 2024-03-22 stsp if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1069 e70fd952 2024-03-22 stsp GOTD_IMSG_REF_UPDATE, PROC_SESSION_WRITE, -1,
1070 e70fd952 2024-03-22 stsp iref, datalen) == -1)
1071 e70fd952 2024-03-22 stsp err = got_error_from_errno("imsg compose REF_UPDATE");
1072 e70fd952 2024-03-22 stsp free(iref);
1073 e70fd952 2024-03-22 stsp return err;
1076 e70fd952 2024-03-22 stsp static int
1077 e70fd952 2024-03-22 stsp client_has_capability(struct gotd_session_client *client, const char *capastr)
1079 e70fd952 2024-03-22 stsp struct gotd_client_capability *capa;
1082 e70fd952 2024-03-22 stsp if (client->ncapabilities == 0)
1085 e70fd952 2024-03-22 stsp for (i = 0; i < client->ncapabilities; i++) {
1086 e70fd952 2024-03-22 stsp capa = &client->capabilities[i];
1087 e70fd952 2024-03-22 stsp if (strcmp(capa->key, capastr) == 0)
1094 e70fd952 2024-03-22 stsp static const struct got_error *
1095 e70fd952 2024-03-22 stsp recv_packfile(struct gotd_session_client *client)
1097 e70fd952 2024-03-22 stsp const struct got_error *err = NULL;
1098 e70fd952 2024-03-22 stsp struct gotd_imsg_recv_packfile ipack;
1099 e70fd952 2024-03-22 stsp char *basepath = NULL, *pack_path = NULL, *idx_path = NULL;
1100 e70fd952 2024-03-22 stsp int packfd = -1, idxfd = -1;
1101 e70fd952 2024-03-22 stsp int pipe[2] = { -1, -1 };
1103 e70fd952 2024-03-22 stsp if (client->packfile_path) {
1104 e70fd952 2024-03-22 stsp return got_error_fmt(GOT_ERR_PRIVSEP_MSG,
1105 e70fd952 2024-03-22 stsp "uid %d already has a pack file", client->euid);
1108 e70fd952 2024-03-22 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
1109 e70fd952 2024-03-22 stsp return got_error_from_errno("socketpair");
1111 e70fd952 2024-03-22 stsp /* Send pack pipe end 0 to repo child process. */
1112 e70fd952 2024-03-22 stsp if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1113 e70fd952 2024-03-22 stsp GOTD_IMSG_PACKFILE_PIPE, PROC_SESSION_WRITE, pipe[0],
1114 e70fd952 2024-03-22 stsp NULL, 0) == -1) {
1115 e70fd952 2024-03-22 stsp err = got_error_from_errno("imsg compose PACKFILE_PIPE");
1116 e70fd952 2024-03-22 stsp pipe[0] = -1;
1117 e70fd952 2024-03-22 stsp goto done;
1119 e70fd952 2024-03-22 stsp pipe[0] = -1;
1121 e70fd952 2024-03-22 stsp /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
1122 e70fd952 2024-03-22 stsp if (gotd_imsg_compose_event(&client->iev,
1123 e70fd952 2024-03-22 stsp GOTD_IMSG_PACKFILE_PIPE, PROC_SESSION_WRITE, pipe[1],
1124 e70fd952 2024-03-22 stsp NULL, 0) == -1)
1125 e70fd952 2024-03-22 stsp err = got_error_from_errno("imsg compose PACKFILE_PIPE");
1126 e70fd952 2024-03-22 stsp pipe[1] = -1;
1128 e70fd952 2024-03-22 stsp if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.pack",
1129 e70fd952 2024-03-22 stsp got_repo_get_path(gotd_session.repo), GOT_OBJECTS_PACK_DIR,
1130 e70fd952 2024-03-22 stsp client->euid) == -1) {
1131 e70fd952 2024-03-22 stsp err = got_error_from_errno("asprintf");
1132 e70fd952 2024-03-22 stsp goto done;
1135 e70fd952 2024-03-22 stsp err = got_opentemp_named_fd(&pack_path, &packfd, basepath, "");
1137 e70fd952 2024-03-22 stsp goto done;
1138 e70fd952 2024-03-22 stsp if (fchmod(packfd, GOT_DEFAULT_PACK_MODE) == -1) {
1139 e70fd952 2024-03-22 stsp err = got_error_from_errno2("fchmod", pack_path);
1140 e70fd952 2024-03-22 stsp goto done;
1143 e70fd952 2024-03-22 stsp free(basepath);
1144 e70fd952 2024-03-22 stsp if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.idx",
1145 e70fd952 2024-03-22 stsp got_repo_get_path(gotd_session.repo), GOT_OBJECTS_PACK_DIR,
1146 e70fd952 2024-03-22 stsp client->euid) == -1) {
1147 e70fd952 2024-03-22 stsp err = got_error_from_errno("asprintf");
1148 e70fd952 2024-03-22 stsp basepath = NULL;
1149 e70fd952 2024-03-22 stsp goto done;
1151 e70fd952 2024-03-22 stsp err = got_opentemp_named_fd(&idx_path, &idxfd, basepath, "");
1153 e70fd952 2024-03-22 stsp goto done;
1154 e70fd952 2024-03-22 stsp if (fchmod(idxfd, GOT_DEFAULT_PACK_MODE) == -1) {
1155 e70fd952 2024-03-22 stsp err = got_error_from_errno2("fchmod", idx_path);
1156 e70fd952 2024-03-22 stsp goto done;
1159 e70fd952 2024-03-22 stsp if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1160 e70fd952 2024-03-22 stsp GOTD_IMSG_PACKIDX_FILE, PROC_SESSION_WRITE,
1161 e70fd952 2024-03-22 stsp idxfd, NULL, 0) == -1) {
1162 e70fd952 2024-03-22 stsp err = got_error_from_errno("imsg compose PACKIDX_FILE");
1163 e70fd952 2024-03-22 stsp idxfd = -1;
1164 e70fd952 2024-03-22 stsp goto done;
1166 e70fd952 2024-03-22 stsp idxfd = -1;
1168 e70fd952 2024-03-22 stsp memset(&ipack, 0, sizeof(ipack));
1169 e70fd952 2024-03-22 stsp if (client_has_capability(client, GOT_CAPA_REPORT_STATUS))
1170 e70fd952 2024-03-22 stsp ipack.report_status = 1;
1172 e70fd952 2024-03-22 stsp if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1173 e70fd952 2024-03-22 stsp GOTD_IMSG_RECV_PACKFILE, PROC_SESSION_WRITE, packfd,
1174 e70fd952 2024-03-22 stsp &ipack, sizeof(ipack)) == -1) {
1175 e70fd952 2024-03-22 stsp err = got_error_from_errno("imsg compose RECV_PACKFILE");
1176 e70fd952 2024-03-22 stsp packfd = -1;
1177 e70fd952 2024-03-22 stsp goto done;
1179 e70fd952 2024-03-22 stsp packfd = -1;
1182 e70fd952 2024-03-22 stsp free(basepath);
1183 e70fd952 2024-03-22 stsp if (pipe[0] != -1 && close(pipe[0]) == -1 && err == NULL)
1184 e70fd952 2024-03-22 stsp err = got_error_from_errno("close");
1185 e70fd952 2024-03-22 stsp if (pipe[1] != -1 && close(pipe[1]) == -1 && err == NULL)
1186 e70fd952 2024-03-22 stsp err = got_error_from_errno("close");
1187 e70fd952 2024-03-22 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
1188 e70fd952 2024-03-22 stsp err = got_error_from_errno("close");
1189 e70fd952 2024-03-22 stsp if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
1190 e70fd952 2024-03-22 stsp err = got_error_from_errno("close");
1191 e70fd952 2024-03-22 stsp if (err) {
1192 e70fd952 2024-03-22 stsp free(pack_path);
1193 e70fd952 2024-03-22 stsp free(idx_path);
1195 e70fd952 2024-03-22 stsp client->packfile_path = pack_path;
1196 e70fd952 2024-03-22 stsp client->packidx_path = idx_path;
1198 e70fd952 2024-03-22 stsp return err;
1201 e70fd952 2024-03-22 stsp static void
1202 e70fd952 2024-03-22 stsp session_dispatch_client(int fd, short events, void *arg)
1204 e70fd952 2024-03-22 stsp struct gotd_imsgev *iev = arg;
1205 e70fd952 2024-03-22 stsp struct imsgbuf *ibuf = &iev->ibuf;
1206 e70fd952 2024-03-22 stsp struct gotd_session_client *client = &gotd_session_client;
1207 e70fd952 2024-03-22 stsp const struct got_error *err = NULL;
1208 e70fd952 2024-03-22 stsp struct imsg imsg;
1209 e70fd952 2024-03-22 stsp ssize_t n;
1211 e70fd952 2024-03-22 stsp if (events & EV_WRITE) {
1212 e70fd952 2024-03-22 stsp while (ibuf->w.queued) {
1213 e70fd952 2024-03-22 stsp n = msgbuf_write(&ibuf->w);
1214 e70fd952 2024-03-22 stsp if (n == -1 && errno == EPIPE) {
1216 e70fd952 2024-03-22 stsp * The client has closed its socket.
1217 e70fd952 2024-03-22 stsp * This can happen when Git clients are
1218 e70fd952 2024-03-22 stsp * done sending pack file data.
1220 e70fd952 2024-03-22 stsp msgbuf_clear(&ibuf->w);
1222 e70fd952 2024-03-22 stsp } else if (n == -1 && errno != EAGAIN) {
1223 e70fd952 2024-03-22 stsp err = got_error_from_errno("imsg_flush");
1224 e70fd952 2024-03-22 stsp disconnect_on_error(client, err);
1227 e70fd952 2024-03-22 stsp if (n == 0) {
1228 e70fd952 2024-03-22 stsp /* Connection closed. */
1229 e70fd952 2024-03-22 stsp err = got_error(GOT_ERR_EOF);
1230 e70fd952 2024-03-22 stsp disconnect_on_error(client, err);
1235 e70fd952 2024-03-22 stsp if (client->flush_disconnect) {
1236 e70fd952 2024-03-22 stsp disconnect(client);
1241 e70fd952 2024-03-22 stsp if ((events & EV_READ) == 0)
1244 e70fd952 2024-03-22 stsp memset(&imsg, 0, sizeof(imsg));
1246 e70fd952 2024-03-22 stsp while (err == NULL) {
1247 e70fd952 2024-03-22 stsp err = gotd_imsg_recv(&imsg, ibuf, 0);
1248 e70fd952 2024-03-22 stsp if (err) {
1249 e70fd952 2024-03-22 stsp if (err->code == GOT_ERR_PRIVSEP_READ)
1250 e70fd952 2024-03-22 stsp err = NULL;
1251 e70fd952 2024-03-22 stsp else if (err->code == GOT_ERR_EOF &&
1252 e70fd952 2024-03-22 stsp gotd_session.state ==
1253 e70fd952 2024-03-22 stsp GOTD_STATE_EXPECT_CAPABILITIES) {
1255 e70fd952 2024-03-22 stsp * The client has closed its socket before
1256 e70fd952 2024-03-22 stsp * sending its capability announcement.
1257 e70fd952 2024-03-22 stsp * This can happen when Git clients have
1258 e70fd952 2024-03-22 stsp * no ref-updates to send.
1260 e70fd952 2024-03-22 stsp disconnect_on_error(client, err);
1266 e70fd952 2024-03-22 stsp evtimer_del(&client->tmo);
1268 e70fd952 2024-03-22 stsp switch (imsg.hdr.type) {
1269 e70fd952 2024-03-22 stsp case GOTD_IMSG_CAPABILITIES:
1270 e70fd952 2024-03-22 stsp if (gotd_session.state !=
1271 e70fd952 2024-03-22 stsp GOTD_STATE_EXPECT_CAPABILITIES) {
1272 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1273 e70fd952 2024-03-22 stsp "unexpected capabilities received");
1276 e70fd952 2024-03-22 stsp log_debug("receiving capabilities from uid %d",
1277 e70fd952 2024-03-22 stsp client->euid);
1278 e70fd952 2024-03-22 stsp err = recv_capabilities(client, &imsg);
1280 e70fd952 2024-03-22 stsp case GOTD_IMSG_CAPABILITY:
1281 e70fd952 2024-03-22 stsp if (gotd_session.state != GOTD_STATE_EXPECT_CAPABILITIES) {
1282 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1283 e70fd952 2024-03-22 stsp "unexpected capability received");
1286 e70fd952 2024-03-22 stsp err = recv_capability(client, &imsg);
1287 e70fd952 2024-03-22 stsp if (err || client->ncapabilities < client->ncapa_alloc)
1289 e70fd952 2024-03-22 stsp gotd_session.state = GOTD_STATE_EXPECT_REF_UPDATE;
1290 e70fd952 2024-03-22 stsp client->accept_flush_pkt = 1;
1291 e70fd952 2024-03-22 stsp log_debug("uid %d: expecting ref-update-lines",
1292 e70fd952 2024-03-22 stsp client->euid);
1294 e70fd952 2024-03-22 stsp case GOTD_IMSG_REF_UPDATE:
1295 e70fd952 2024-03-22 stsp if (gotd_session.state != GOTD_STATE_EXPECT_REF_UPDATE &&
1296 e70fd952 2024-03-22 stsp gotd_session.state !=
1297 e70fd952 2024-03-22 stsp GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1298 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1299 e70fd952 2024-03-22 stsp "unexpected ref-update-line received");
1302 e70fd952 2024-03-22 stsp log_debug("received ref-update-line from uid %d",
1303 e70fd952 2024-03-22 stsp client->euid);
1304 e70fd952 2024-03-22 stsp err = forward_ref_update(client, &imsg);
1307 e70fd952 2024-03-22 stsp gotd_session.state = GOTD_STATE_EXPECT_MORE_REF_UPDATES;
1308 e70fd952 2024-03-22 stsp client->accept_flush_pkt = 1;
1310 e70fd952 2024-03-22 stsp case GOTD_IMSG_FLUSH:
1311 e70fd952 2024-03-22 stsp if (gotd_session.state !=
1312 e70fd952 2024-03-22 stsp GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1313 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1314 e70fd952 2024-03-22 stsp "unexpected flush-pkt received");
1317 e70fd952 2024-03-22 stsp if (!client->accept_flush_pkt) {
1318 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1319 e70fd952 2024-03-22 stsp "unexpected flush-pkt received");
1324 e70fd952 2024-03-22 stsp * Accept just one flush packet at a time.
1325 e70fd952 2024-03-22 stsp * Future client state transitions will set this flag
1326 e70fd952 2024-03-22 stsp * again if another flush packet is expected.
1328 e70fd952 2024-03-22 stsp client->accept_flush_pkt = 0;
1330 e70fd952 2024-03-22 stsp log_debug("received flush-pkt from uid %d",
1331 e70fd952 2024-03-22 stsp client->euid);
1332 e70fd952 2024-03-22 stsp if (gotd_session.state ==
1333 e70fd952 2024-03-22 stsp GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1334 e70fd952 2024-03-22 stsp gotd_session.state = GOTD_STATE_EXPECT_PACKFILE;
1335 e70fd952 2024-03-22 stsp log_debug("uid %d: expecting packfile",
1336 e70fd952 2024-03-22 stsp client->euid);
1337 e70fd952 2024-03-22 stsp err = recv_packfile(client);
1339 e70fd952 2024-03-22 stsp /* should not happen, see above */
1340 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1341 e70fd952 2024-03-22 stsp "unexpected client state");
1346 e70fd952 2024-03-22 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1347 e70fd952 2024-03-22 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1351 e70fd952 2024-03-22 stsp imsg_free(&imsg);
1354 e70fd952 2024-03-22 stsp if (err) {
1355 e70fd952 2024-03-22 stsp if (err->code != GOT_ERR_EOF ||
1356 c3cc85a3 2024-04-30 stsp (gotd_session.state != GOTD_STATE_EXPECT_PACKFILE &&
1357 c3cc85a3 2024-04-30 stsp gotd_session.state != GOTD_STATE_NOTIFY))
1358 e70fd952 2024-03-22 stsp disconnect_on_error(client, err);
1360 e70fd952 2024-03-22 stsp gotd_imsg_event_add(iev);
1361 e70fd952 2024-03-22 stsp evtimer_add(&client->tmo, &gotd_session.request_timeout);
1365 e70fd952 2024-03-22 stsp static const struct got_error *
1366 e70fd952 2024-03-22 stsp list_refs_request(void)
1368 e70fd952 2024-03-22 stsp static const struct got_error *err;
1369 e70fd952 2024-03-22 stsp struct gotd_session_client *client = &gotd_session_client;
1370 e70fd952 2024-03-22 stsp struct gotd_imsgev *iev = &gotd_session.repo_child_iev;
1373 e70fd952 2024-03-22 stsp if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
1374 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1376 e70fd952 2024-03-22 stsp fd = dup(client->fd);
1377 e70fd952 2024-03-22 stsp if (fd == -1)
1378 e70fd952 2024-03-22 stsp return got_error_from_errno("dup");
1380 e70fd952 2024-03-22 stsp if (gotd_imsg_compose_event(iev, GOTD_IMSG_LIST_REFS_INTERNAL,
1381 e70fd952 2024-03-22 stsp PROC_SESSION_WRITE, fd, NULL, 0) == -1) {
1382 e70fd952 2024-03-22 stsp err = got_error_from_errno("imsg compose LIST_REFS_INTERNAL");
1383 e70fd952 2024-03-22 stsp close(fd);
1384 e70fd952 2024-03-22 stsp return err;
1387 e70fd952 2024-03-22 stsp gotd_session.state = GOTD_STATE_EXPECT_CAPABILITIES;
1388 e70fd952 2024-03-22 stsp log_debug("uid %d: expecting capabilities", client->euid);
1389 e70fd952 2024-03-22 stsp return NULL;
1392 e70fd952 2024-03-22 stsp static const struct got_error *
1393 e70fd952 2024-03-22 stsp recv_connect(struct imsg *imsg)
1395 e70fd952 2024-03-22 stsp struct gotd_session_client *client = &gotd_session_client;
1396 e70fd952 2024-03-22 stsp struct gotd_imsg_connect iconnect;
1397 e70fd952 2024-03-22 stsp size_t datalen;
1399 e70fd952 2024-03-22 stsp if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
1400 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1402 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1403 e70fd952 2024-03-22 stsp if (datalen < sizeof(iconnect))
1404 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1405 e70fd952 2024-03-22 stsp memcpy(&iconnect, imsg->data, sizeof(iconnect));
1406 e70fd952 2024-03-22 stsp if (iconnect.username_len == 0 ||
1407 e70fd952 2024-03-22 stsp datalen != sizeof(iconnect) + iconnect.username_len)
1408 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1410 e70fd952 2024-03-22 stsp client->euid = iconnect.euid;
1411 e70fd952 2024-03-22 stsp client->egid = iconnect.egid;
1412 e70fd952 2024-03-22 stsp client->fd = imsg_get_fd(imsg);
1413 e70fd952 2024-03-22 stsp if (client->fd == -1)
1414 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
1416 e70fd952 2024-03-22 stsp client->username = strndup(imsg->data + sizeof(iconnect),
1417 e70fd952 2024-03-22 stsp iconnect.username_len);
1418 e70fd952 2024-03-22 stsp if (client->username == NULL)
1419 e70fd952 2024-03-22 stsp return got_error_from_errno("strndup");
1421 e70fd952 2024-03-22 stsp imsg_init(&client->iev.ibuf, client->fd);
1422 e70fd952 2024-03-22 stsp client->iev.handler = session_dispatch_client;
1423 e70fd952 2024-03-22 stsp client->iev.events = EV_READ;
1424 e70fd952 2024-03-22 stsp client->iev.handler_arg = NULL;
1425 e70fd952 2024-03-22 stsp event_set(&client->iev.ev, client->iev.ibuf.fd, EV_READ,
1426 e70fd952 2024-03-22 stsp session_dispatch_client, &client->iev);
1427 e70fd952 2024-03-22 stsp gotd_imsg_event_add(&client->iev);
1428 e70fd952 2024-03-22 stsp evtimer_set(&client->tmo, gotd_request_timeout, client);
1429 caa6cf11 2024-04-30 stsp evtimer_add(&client->tmo, &gotd_session.request_timeout);
1431 e70fd952 2024-03-22 stsp return NULL;
1434 e70fd952 2024-03-22 stsp static void
1435 e70fd952 2024-03-22 stsp session_dispatch_notifier(int fd, short event, void *arg)
1437 e70fd952 2024-03-22 stsp const struct got_error *err;
1438 e70fd952 2024-03-22 stsp struct gotd_session_client *client = &gotd_session_client;
1439 e70fd952 2024-03-22 stsp struct gotd_imsgev *iev = arg;
1440 e70fd952 2024-03-22 stsp struct imsgbuf *ibuf = &iev->ibuf;
1441 e70fd952 2024-03-22 stsp ssize_t n;
1442 e70fd952 2024-03-22 stsp int shut = 0;
1443 e70fd952 2024-03-22 stsp struct imsg imsg;
1444 e70fd952 2024-03-22 stsp struct gotd_session_notif *notif;
1446 e70fd952 2024-03-22 stsp if (event & EV_READ) {
1447 e70fd952 2024-03-22 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1448 e70fd952 2024-03-22 stsp fatal("imsg_read error");
1449 e70fd952 2024-03-22 stsp if (n == 0) {
1450 e70fd952 2024-03-22 stsp /* Connection closed. */
1452 e70fd952 2024-03-22 stsp goto done;
1456 e70fd952 2024-03-22 stsp if (event & EV_WRITE) {
1457 e70fd952 2024-03-22 stsp n = msgbuf_write(&ibuf->w);
1458 e70fd952 2024-03-22 stsp if (n == -1 && errno != EAGAIN)
1459 e70fd952 2024-03-22 stsp fatal("msgbuf_write");
1460 e70fd952 2024-03-22 stsp if (n == 0) {
1461 e70fd952 2024-03-22 stsp /* Connection closed. */
1463 e70fd952 2024-03-22 stsp goto done;
1467 e70fd952 2024-03-22 stsp for (;;) {
1468 e70fd952 2024-03-22 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
1469 e70fd952 2024-03-22 stsp fatal("%s: imsg_get error", __func__);
1470 e70fd952 2024-03-22 stsp if (n == 0) /* No more messages. */
1473 e70fd952 2024-03-22 stsp switch (imsg.hdr.type) {
1474 e70fd952 2024-03-22 stsp case GOTD_IMSG_NOTIFICATION_SENT:
1475 e70fd952 2024-03-22 stsp if (gotd_session.state != GOTD_STATE_NOTIFY) {
1476 e70fd952 2024-03-22 stsp log_warn("unexpected imsg %d", imsg.hdr.type);
1479 e70fd952 2024-03-22 stsp notif = STAILQ_FIRST(¬ifications);
1480 e70fd952 2024-03-22 stsp if (notif == NULL) {
1481 e70fd952 2024-03-22 stsp disconnect(client);
1482 e70fd952 2024-03-22 stsp break; /* NOTREACHED */
1484 e70fd952 2024-03-22 stsp /* Request content for the next notification. */
1485 e70fd952 2024-03-22 stsp err = request_notification(notif);
1486 e70fd952 2024-03-22 stsp if (err) {
1487 e70fd952 2024-03-22 stsp log_warn("could not send notification: %s",
1488 e70fd952 2024-03-22 stsp err->msg);
1489 e70fd952 2024-03-22 stsp disconnect(client);
1493 e70fd952 2024-03-22 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1497 e70fd952 2024-03-22 stsp imsg_free(&imsg);
1500 e70fd952 2024-03-22 stsp if (!shut) {
1501 e70fd952 2024-03-22 stsp gotd_imsg_event_add(iev);
1503 e70fd952 2024-03-22 stsp /* This pipe is dead. Remove its event handler */
1504 e70fd952 2024-03-22 stsp event_del(&iev->ev);
1505 e70fd952 2024-03-22 stsp imsg_clear(&iev->ibuf);
1506 e70fd952 2024-03-22 stsp imsg_init(&iev->ibuf, -1);
1510 e70fd952 2024-03-22 stsp static const struct got_error *
1511 e70fd952 2024-03-22 stsp recv_notifier(struct imsg *imsg)
1513 e70fd952 2024-03-22 stsp struct gotd_imsgev *iev = &gotd_session.notifier_iev;
1514 e70fd952 2024-03-22 stsp struct gotd_session_client *client = &gotd_session_client;
1515 e70fd952 2024-03-22 stsp size_t datalen;
1518 e70fd952 2024-03-22 stsp if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
1519 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1521 e70fd952 2024-03-22 stsp /* We should already have received a pipe to the listener. */
1522 e70fd952 2024-03-22 stsp if (client->fd == -1)
1523 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1525 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1526 e70fd952 2024-03-22 stsp if (datalen != 0)
1527 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1529 e70fd952 2024-03-22 stsp fd = imsg_get_fd(imsg);
1530 e70fd952 2024-03-22 stsp if (fd == -1)
1531 e70fd952 2024-03-22 stsp return NULL; /* notifications unused */
1533 e70fd952 2024-03-22 stsp imsg_init(&iev->ibuf, fd);
1534 e70fd952 2024-03-22 stsp iev->handler = session_dispatch_notifier;
1535 e70fd952 2024-03-22 stsp iev->events = EV_READ;
1536 e70fd952 2024-03-22 stsp iev->handler_arg = NULL;
1537 e70fd952 2024-03-22 stsp event_set(&iev->ev, iev->ibuf.fd, EV_READ,
1538 e70fd952 2024-03-22 stsp session_dispatch_notifier, iev);
1539 e70fd952 2024-03-22 stsp gotd_imsg_event_add(iev);
1541 e70fd952 2024-03-22 stsp return NULL;
1544 e70fd952 2024-03-22 stsp static const struct got_error *
1545 e70fd952 2024-03-22 stsp recv_repo_child(struct imsg *imsg)
1547 e70fd952 2024-03-22 stsp struct gotd_imsg_connect_repo_child ichild;
1548 e70fd952 2024-03-22 stsp struct gotd_session_client *client = &gotd_session_client;
1549 e70fd952 2024-03-22 stsp size_t datalen;
1552 e70fd952 2024-03-22 stsp if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
1553 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1555 e70fd952 2024-03-22 stsp /* We should already have received a pipe to the listener. */
1556 e70fd952 2024-03-22 stsp if (client->fd == -1)
1557 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1559 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1560 e70fd952 2024-03-22 stsp if (datalen != sizeof(ichild))
1561 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1563 e70fd952 2024-03-22 stsp memcpy(&ichild, imsg->data, sizeof(ichild));
1565 e70fd952 2024-03-22 stsp if (ichild.proc_id != PROC_REPO_WRITE)
1566 e70fd952 2024-03-22 stsp return got_error_msg(GOT_ERR_PRIVSEP_MSG,
1567 e70fd952 2024-03-22 stsp "bad child process type");
1569 e70fd952 2024-03-22 stsp fd = imsg_get_fd(imsg);
1570 e70fd952 2024-03-22 stsp if (fd == -1)
1571 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
1573 e70fd952 2024-03-22 stsp imsg_init(&gotd_session.repo_child_iev.ibuf, fd);
1574 e70fd952 2024-03-22 stsp gotd_session.repo_child_iev.handler = session_dispatch_repo_child;
1575 e70fd952 2024-03-22 stsp gotd_session.repo_child_iev.events = EV_READ;
1576 e70fd952 2024-03-22 stsp gotd_session.repo_child_iev.handler_arg = NULL;
1577 e70fd952 2024-03-22 stsp event_set(&gotd_session.repo_child_iev.ev,
1578 e70fd952 2024-03-22 stsp gotd_session.repo_child_iev.ibuf.fd, EV_READ,
1579 e70fd952 2024-03-22 stsp session_dispatch_repo_child, &gotd_session.repo_child_iev);
1580 e70fd952 2024-03-22 stsp gotd_imsg_event_add(&gotd_session.repo_child_iev);
1582 e70fd952 2024-03-22 stsp /* The "recvfd" pledge promise is no longer needed. */
1583 e70fd952 2024-03-22 stsp if (pledge("stdio rpath wpath cpath sendfd fattr flock", NULL) == -1)
1584 e70fd952 2024-03-22 stsp fatal("pledge");
1586 e70fd952 2024-03-22 stsp return NULL;
1589 e70fd952 2024-03-22 stsp static void
1590 e70fd952 2024-03-22 stsp session_dispatch(int fd, short event, void *arg)
1592 e70fd952 2024-03-22 stsp struct gotd_imsgev *iev = arg;
1593 e70fd952 2024-03-22 stsp struct imsgbuf *ibuf = &iev->ibuf;
1594 e70fd952 2024-03-22 stsp struct gotd_session_client *client = &gotd_session_client;
1595 e70fd952 2024-03-22 stsp ssize_t n;
1596 e70fd952 2024-03-22 stsp int shut = 0;
1597 e70fd952 2024-03-22 stsp struct imsg imsg;
1599 e70fd952 2024-03-22 stsp if (event & EV_READ) {
1600 e70fd952 2024-03-22 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1601 e70fd952 2024-03-22 stsp fatal("imsg_read error");
1602 e70fd952 2024-03-22 stsp if (n == 0) {
1603 e70fd952 2024-03-22 stsp /* Connection closed. */
1605 e70fd952 2024-03-22 stsp goto done;
1609 e70fd952 2024-03-22 stsp if (event & EV_WRITE) {
1610 e70fd952 2024-03-22 stsp n = msgbuf_write(&ibuf->w);
1611 e70fd952 2024-03-22 stsp if (n == -1 && errno != EAGAIN)
1612 e70fd952 2024-03-22 stsp fatal("msgbuf_write");
1613 e70fd952 2024-03-22 stsp if (n == 0) {
1614 e70fd952 2024-03-22 stsp /* Connection closed. */
1616 e70fd952 2024-03-22 stsp goto done;
1620 e70fd952 2024-03-22 stsp for (;;) {
1621 e70fd952 2024-03-22 stsp const struct got_error *err = NULL;
1622 e70fd952 2024-03-22 stsp uint32_t client_id = 0;
1623 e70fd952 2024-03-22 stsp int do_disconnect = 0, do_list_refs = 0;
1625 e70fd952 2024-03-22 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
1626 e70fd952 2024-03-22 stsp fatal("%s: imsg_get error", __func__);
1627 e70fd952 2024-03-22 stsp if (n == 0) /* No more messages. */
1630 e70fd952 2024-03-22 stsp switch (imsg.hdr.type) {
1631 e70fd952 2024-03-22 stsp case GOTD_IMSG_ERROR:
1632 e70fd952 2024-03-22 stsp do_disconnect = 1;
1633 e70fd952 2024-03-22 stsp err = gotd_imsg_recv_error(&client_id, &imsg);
1635 e70fd952 2024-03-22 stsp case GOTD_IMSG_CONNECT:
1636 e70fd952 2024-03-22 stsp err = recv_connect(&imsg);
1638 e70fd952 2024-03-22 stsp case GOTD_IMSG_DISCONNECT:
1639 e70fd952 2024-03-22 stsp do_disconnect = 1;
1641 e70fd952 2024-03-22 stsp case GOTD_IMSG_CONNECT_NOTIFIER:
1642 e70fd952 2024-03-22 stsp err = recv_notifier(&imsg);
1644 e70fd952 2024-03-22 stsp case GOTD_IMSG_CONNECT_REPO_CHILD:
1645 e70fd952 2024-03-22 stsp err = recv_repo_child(&imsg);
1648 e70fd952 2024-03-22 stsp do_list_refs = 1;
1651 e70fd952 2024-03-22 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1654 e70fd952 2024-03-22 stsp imsg_free(&imsg);
1656 e70fd952 2024-03-22 stsp if (do_disconnect) {
1658 e70fd952 2024-03-22 stsp disconnect_on_error(client, err);
1660 e70fd952 2024-03-22 stsp disconnect(client);
1661 e70fd952 2024-03-22 stsp } else if (do_list_refs)
1662 e70fd952 2024-03-22 stsp err = list_refs_request();
1665 e70fd952 2024-03-22 stsp log_warnx("uid %d: %s", client->euid, err->msg);
1668 e70fd952 2024-03-22 stsp if (!shut) {
1669 e70fd952 2024-03-22 stsp gotd_imsg_event_add(iev);
1671 e70fd952 2024-03-22 stsp /* This pipe is dead. Remove its event handler */
1672 e70fd952 2024-03-22 stsp event_del(&iev->ev);
1673 e70fd952 2024-03-22 stsp event_loopexit(NULL);
1678 e70fd952 2024-03-22 stsp session_write_main(const char *title, const char *repo_path,
1679 e70fd952 2024-03-22 stsp int *pack_fds, int *temp_fds, struct timeval *request_timeout,
1680 e70fd952 2024-03-22 stsp struct gotd_repo *repo_cfg)
1682 e70fd952 2024-03-22 stsp const struct got_error *err = NULL;
1683 e70fd952 2024-03-22 stsp struct event evsigint, evsigterm, evsighup, evsigusr1;
1685 e70fd952 2024-03-22 stsp STAILQ_INIT(¬ifications);
1687 e70fd952 2024-03-22 stsp gotd_session.title = title;
1688 e70fd952 2024-03-22 stsp gotd_session.pid = getpid();
1689 e70fd952 2024-03-22 stsp gotd_session.pack_fds = pack_fds;
1690 e70fd952 2024-03-22 stsp gotd_session.temp_fds = temp_fds;
1691 e70fd952 2024-03-22 stsp memcpy(&gotd_session.request_timeout, request_timeout,
1692 e70fd952 2024-03-22 stsp sizeof(gotd_session.request_timeout));
1693 e70fd952 2024-03-22 stsp gotd_session.repo_cfg = repo_cfg;
1695 e70fd952 2024-03-22 stsp imsg_init(&gotd_session.notifier_iev.ibuf, -1);
1697 e70fd952 2024-03-22 stsp err = got_repo_open(&gotd_session.repo, repo_path, NULL, pack_fds);
1699 e70fd952 2024-03-22 stsp goto done;
1700 e70fd952 2024-03-22 stsp if (!got_repo_is_bare(gotd_session.repo)) {
1701 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1702 e70fd952 2024-03-22 stsp "bare git repository required");
1703 e70fd952 2024-03-22 stsp goto done;
1705 c214ca4f 2024-08-01 op if (got_repo_get_object_format(gotd_session.repo) != GOT_HASH_SHA1) {
1706 c214ca4f 2024-08-01 op err = got_error_msg(GOT_ERR_NOT_IMPL,
1707 c214ca4f 2024-08-01 op "sha256 object IDs unsupported in network protocol");
1711 e70fd952 2024-03-22 stsp got_repo_temp_fds_set(gotd_session.repo, temp_fds);
1713 e70fd952 2024-03-22 stsp signal_set(&evsigint, SIGINT, session_write_sighdlr, NULL);
1714 e70fd952 2024-03-22 stsp signal_set(&evsigterm, SIGTERM, session_write_sighdlr, NULL);
1715 e70fd952 2024-03-22 stsp signal_set(&evsighup, SIGHUP, session_write_sighdlr, NULL);
1716 e70fd952 2024-03-22 stsp signal_set(&evsigusr1, SIGUSR1, session_write_sighdlr, NULL);
1717 e70fd952 2024-03-22 stsp signal(SIGPIPE, SIG_IGN);
1719 e70fd952 2024-03-22 stsp signal_add(&evsigint, NULL);
1720 e70fd952 2024-03-22 stsp signal_add(&evsigterm, NULL);
1721 e70fd952 2024-03-22 stsp signal_add(&evsighup, NULL);
1722 e70fd952 2024-03-22 stsp signal_add(&evsigusr1, NULL);
1724 e70fd952 2024-03-22 stsp gotd_session.state = GOTD_STATE_EXPECT_LIST_REFS;
1726 e70fd952 2024-03-22 stsp gotd_session_client.fd = -1;
1727 e70fd952 2024-03-22 stsp gotd_session_client.nref_updates = -1;
1728 e70fd952 2024-03-22 stsp gotd_session_client.delta_cache_fd = -1;
1729 e70fd952 2024-03-22 stsp gotd_session_client.accept_flush_pkt = 1;
1731 e70fd952 2024-03-22 stsp imsg_init(&gotd_session.parent_iev.ibuf, GOTD_FILENO_MSG_PIPE);
1732 e70fd952 2024-03-22 stsp gotd_session.parent_iev.handler = session_dispatch;
1733 e70fd952 2024-03-22 stsp gotd_session.parent_iev.events = EV_READ;
1734 e70fd952 2024-03-22 stsp gotd_session.parent_iev.handler_arg = NULL;
1735 e70fd952 2024-03-22 stsp event_set(&gotd_session.parent_iev.ev, gotd_session.parent_iev.ibuf.fd,
1736 e70fd952 2024-03-22 stsp EV_READ, session_dispatch, &gotd_session.parent_iev);
1737 e70fd952 2024-03-22 stsp if (gotd_imsg_compose_event(&gotd_session.parent_iev,
1738 e70fd952 2024-03-22 stsp GOTD_IMSG_CLIENT_SESSION_READY, PROC_SESSION_WRITE,
1739 e70fd952 2024-03-22 stsp -1, NULL, 0) == -1) {
1740 e70fd952 2024-03-22 stsp err = got_error_from_errno("imsg compose CLIENT_SESSION_READY");
1741 e70fd952 2024-03-22 stsp goto done;
1744 e70fd952 2024-03-22 stsp event_dispatch();
1747 e70fd952 2024-03-22 stsp log_warnx("%s: %s", title, err->msg);
1748 e70fd952 2024-03-22 stsp session_write_shutdown();
1751 e70fd952 2024-03-22 stsp static void
1752 e70fd952 2024-03-22 stsp session_write_shutdown(void)
1754 e70fd952 2024-03-22 stsp struct gotd_session_notif *notif;
1756 e8d451cc 2024-03-22 stsp log_debug("%s: shutting down", gotd_session.title);
1758 e70fd952 2024-03-22 stsp while (!STAILQ_EMPTY(¬ifications)) {
1759 e70fd952 2024-03-22 stsp notif = STAILQ_FIRST(¬ifications);
1760 e70fd952 2024-03-22 stsp STAILQ_REMOVE_HEAD(¬ifications, entry);
1761 e70fd952 2024-03-22 stsp if (notif->fd != -1)
1762 e70fd952 2024-03-22 stsp close(notif->fd);
1763 e70fd952 2024-03-22 stsp free(notif->refname);
1764 e70fd952 2024-03-22 stsp free(notif);
1767 e70fd952 2024-03-22 stsp if (gotd_session.repo)
1768 e70fd952 2024-03-22 stsp got_repo_close(gotd_session.repo);
1769 e70fd952 2024-03-22 stsp got_repo_pack_fds_close(gotd_session.pack_fds);
1770 e70fd952 2024-03-22 stsp got_repo_temp_fds_close(gotd_session.temp_fds);
1771 e70fd952 2024-03-22 stsp free(gotd_session_client.username);