Blame


1 62ee7d94 2023-01-10 thomas /*
2 62ee7d94 2023-01-10 thomas * Copyright (c) 2022, 2023 Stefan Sperling <stsp@openbsd.org>
3 62ee7d94 2023-01-10 thomas *
4 62ee7d94 2023-01-10 thomas * Permission to use, copy, modify, and distribute this software for any
5 62ee7d94 2023-01-10 thomas * purpose with or without fee is hereby granted, provided that the above
6 62ee7d94 2023-01-10 thomas * copyright notice and this permission notice appear in all copies.
7 62ee7d94 2023-01-10 thomas *
8 62ee7d94 2023-01-10 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 62ee7d94 2023-01-10 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 62ee7d94 2023-01-10 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 62ee7d94 2023-01-10 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 62ee7d94 2023-01-10 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 62ee7d94 2023-01-10 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 62ee7d94 2023-01-10 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 62ee7d94 2023-01-10 thomas */
16 4efc8dcb 2023-08-29 thomas
17 4efc8dcb 2023-08-29 thomas #include "got_compat.h"
18 62ee7d94 2023-01-10 thomas
19 62ee7d94 2023-01-10 thomas #include <sys/types.h>
20 62ee7d94 2023-01-10 thomas #include <sys/queue.h>
21 62ee7d94 2023-01-10 thomas #include <sys/socket.h>
22 851a5b48 2023-02-03 thomas #include <sys/stat.h>
23 62ee7d94 2023-01-10 thomas #include <sys/uio.h>
24 62ee7d94 2023-01-10 thomas
25 62ee7d94 2023-01-10 thomas #include <errno.h>
26 62ee7d94 2023-01-10 thomas #include <event.h>
27 62ee7d94 2023-01-10 thomas #include <limits.h>
28 62ee7d94 2023-01-10 thomas #include <signal.h>
29 62ee7d94 2023-01-10 thomas #include <stdint.h>
30 62ee7d94 2023-01-10 thomas #include <stdio.h>
31 62ee7d94 2023-01-10 thomas #include <stdlib.h>
32 62ee7d94 2023-01-10 thomas #include <string.h>
33 62ee7d94 2023-01-10 thomas #include <imsg.h>
34 62ee7d94 2023-01-10 thomas #include <unistd.h>
35 febe25b7 2023-08-29 thomas
36 febe25b7 2023-08-29 thomas #include "got_compat.h"
37 62ee7d94 2023-01-10 thomas
38 62ee7d94 2023-01-10 thomas #include "got_error.h"
39 62ee7d94 2023-01-10 thomas #include "got_repository.h"
40 62ee7d94 2023-01-10 thomas #include "got_object.h"
41 62ee7d94 2023-01-10 thomas #include "got_path.h"
42 62ee7d94 2023-01-10 thomas #include "got_reference.h"
43 62ee7d94 2023-01-10 thomas #include "got_opentemp.h"
44 62ee7d94 2023-01-10 thomas
45 be288a59 2023-02-23 thomas #include "got_lib_hash.h"
46 62ee7d94 2023-01-10 thomas #include "got_lib_delta.h"
47 62ee7d94 2023-01-10 thomas #include "got_lib_object.h"
48 62ee7d94 2023-01-10 thomas #include "got_lib_object_cache.h"
49 62ee7d94 2023-01-10 thomas #include "got_lib_pack.h"
50 62ee7d94 2023-01-10 thomas #include "got_lib_repository.h"
51 62ee7d94 2023-01-10 thomas #include "got_lib_gitproto.h"
52 62ee7d94 2023-01-10 thomas
53 62ee7d94 2023-01-10 thomas #include "gotd.h"
54 62ee7d94 2023-01-10 thomas #include "log.h"
55 62ee7d94 2023-01-10 thomas #include "session.h"
56 62ee7d94 2023-01-10 thomas
57 ce1bfad9 2024-03-30 thomas struct gotd_session_notif {
58 ce1bfad9 2024-03-30 thomas STAILQ_ENTRY(gotd_session_notif) entry;
59 ce1bfad9 2024-03-30 thomas int fd;
60 ce1bfad9 2024-03-30 thomas enum gotd_notification_action action;
61 ce1bfad9 2024-03-30 thomas char *refname;
62 ce1bfad9 2024-03-30 thomas struct got_object_id old_id;
63 ce1bfad9 2024-03-30 thomas struct got_object_id new_id;
64 ce1bfad9 2024-03-30 thomas };
65 ce1bfad9 2024-03-30 thomas STAILQ_HEAD(gotd_session_notifications, gotd_session_notif) notifications;
66 62ee7d94 2023-01-10 thomas
67 62ee7d94 2023-01-10 thomas static struct gotd_session {
68 62ee7d94 2023-01-10 thomas pid_t pid;
69 62ee7d94 2023-01-10 thomas const char *title;
70 62ee7d94 2023-01-10 thomas struct got_repository *repo;
71 ce1bfad9 2024-03-30 thomas struct gotd_repo *repo_cfg;
72 62ee7d94 2023-01-10 thomas int *pack_fds;
73 62ee7d94 2023-01-10 thomas int *temp_fds;
74 62ee7d94 2023-01-10 thomas struct gotd_imsgev parent_iev;
75 ce1bfad9 2024-03-30 thomas struct gotd_imsgev notifier_iev;
76 62ee7d94 2023-01-10 thomas struct timeval request_timeout;
77 7fed8fa4 2023-06-22 thomas enum gotd_procid proc_id;
78 176350bf 2024-03-30 thomas enum gotd_session_state state;
79 74e85132 2024-03-30 thomas struct gotd_imsgev repo_child_iev;
80 62ee7d94 2023-01-10 thomas } gotd_session;
81 62ee7d94 2023-01-10 thomas
82 62ee7d94 2023-01-10 thomas static struct gotd_session_client {
83 62ee7d94 2023-01-10 thomas int is_writing;
84 62ee7d94 2023-01-10 thomas struct gotd_client_capability *capabilities;
85 62ee7d94 2023-01-10 thomas size_t ncapa_alloc;
86 62ee7d94 2023-01-10 thomas size_t ncapabilities;
87 62ee7d94 2023-01-10 thomas uint32_t id;
88 62ee7d94 2023-01-10 thomas int fd;
89 62ee7d94 2023-01-10 thomas int delta_cache_fd;
90 62ee7d94 2023-01-10 thomas struct gotd_imsgev iev;
91 62ee7d94 2023-01-10 thomas struct event tmo;
92 62ee7d94 2023-01-10 thomas uid_t euid;
93 62ee7d94 2023-01-10 thomas gid_t egid;
94 ce1bfad9 2024-03-30 thomas char *username;
95 62ee7d94 2023-01-10 thomas char *packfile_path;
96 62ee7d94 2023-01-10 thomas char *packidx_path;
97 62ee7d94 2023-01-10 thomas int nref_updates;
98 98c7fd82 2023-01-23 thomas int accept_flush_pkt;
99 c8b73ac1 2023-08-23 thomas int flush_disconnect;
100 62ee7d94 2023-01-10 thomas } gotd_session_client;
101 62ee7d94 2023-01-10 thomas
102 62ee7d94 2023-01-10 thomas void gotd_session_sighdlr(int sig, short event, void *arg);
103 62ee7d94 2023-01-10 thomas static void gotd_session_shutdown(void);
104 62ee7d94 2023-01-10 thomas
105 62ee7d94 2023-01-10 thomas static void
106 62ee7d94 2023-01-10 thomas disconnect(struct gotd_session_client *client)
107 62ee7d94 2023-01-10 thomas {
108 62ee7d94 2023-01-10 thomas log_debug("uid %d: disconnecting", client->euid);
109 62ee7d94 2023-01-10 thomas
110 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&gotd_session.parent_iev,
111 7fed8fa4 2023-06-22 thomas GOTD_IMSG_DISCONNECT, gotd_session.proc_id, -1, NULL, 0) == -1)
112 62ee7d94 2023-01-10 thomas log_warn("imsg compose DISCONNECT");
113 62ee7d94 2023-01-10 thomas
114 74e85132 2024-03-30 thomas imsg_clear(&gotd_session.repo_child_iev.ibuf);
115 74e85132 2024-03-30 thomas event_del(&gotd_session.repo_child_iev.ev);
116 62ee7d94 2023-01-10 thomas evtimer_del(&client->tmo);
117 62ee7d94 2023-01-10 thomas close(client->fd);
118 62ee7d94 2023-01-10 thomas if (client->delta_cache_fd != -1)
119 62ee7d94 2023-01-10 thomas close(client->delta_cache_fd);
120 62ee7d94 2023-01-10 thomas if (client->packfile_path) {
121 62ee7d94 2023-01-10 thomas if (unlink(client->packfile_path) == -1 && errno != ENOENT)
122 62ee7d94 2023-01-10 thomas log_warn("unlink %s: ", client->packfile_path);
123 62ee7d94 2023-01-10 thomas free(client->packfile_path);
124 62ee7d94 2023-01-10 thomas }
125 62ee7d94 2023-01-10 thomas if (client->packidx_path) {
126 62ee7d94 2023-01-10 thomas if (unlink(client->packidx_path) == -1 && errno != ENOENT)
127 62ee7d94 2023-01-10 thomas log_warn("unlink %s: ", client->packidx_path);
128 62ee7d94 2023-01-10 thomas free(client->packidx_path);
129 62ee7d94 2023-01-10 thomas }
130 62ee7d94 2023-01-10 thomas free(client->capabilities);
131 62ee7d94 2023-01-10 thomas
132 62ee7d94 2023-01-10 thomas gotd_session_shutdown();
133 62ee7d94 2023-01-10 thomas }
134 62ee7d94 2023-01-10 thomas
135 62ee7d94 2023-01-10 thomas static void
136 62ee7d94 2023-01-10 thomas disconnect_on_error(struct gotd_session_client *client,
137 62ee7d94 2023-01-10 thomas const struct got_error *err)
138 62ee7d94 2023-01-10 thomas {
139 62ee7d94 2023-01-10 thomas struct imsgbuf ibuf;
140 62ee7d94 2023-01-10 thomas
141 62ee7d94 2023-01-10 thomas if (err->code != GOT_ERR_EOF) {
142 a6153ffb 2023-08-12 thomas log_warnx("uid %d: %s", client->euid, err->msg);
143 62ee7d94 2023-01-10 thomas imsg_init(&ibuf, client->fd);
144 7fed8fa4 2023-06-22 thomas gotd_imsg_send_error(&ibuf, 0, gotd_session.proc_id, err);
145 62ee7d94 2023-01-10 thomas imsg_clear(&ibuf);
146 62ee7d94 2023-01-10 thomas }
147 62ee7d94 2023-01-10 thomas
148 62ee7d94 2023-01-10 thomas disconnect(client);
149 62ee7d94 2023-01-10 thomas }
150 62ee7d94 2023-01-10 thomas
151 62ee7d94 2023-01-10 thomas static void
152 62ee7d94 2023-01-10 thomas gotd_request_timeout(int fd, short events, void *arg)
153 62ee7d94 2023-01-10 thomas {
154 62ee7d94 2023-01-10 thomas struct gotd_session_client *client = arg;
155 62ee7d94 2023-01-10 thomas
156 62ee7d94 2023-01-10 thomas log_debug("disconnecting uid %d due to timeout", client->euid);
157 62ee7d94 2023-01-10 thomas disconnect(client);
158 62ee7d94 2023-01-10 thomas }
159 62ee7d94 2023-01-10 thomas
160 62ee7d94 2023-01-10 thomas void
161 62ee7d94 2023-01-10 thomas gotd_session_sighdlr(int sig, short event, void *arg)
162 62ee7d94 2023-01-10 thomas {
163 62ee7d94 2023-01-10 thomas /*
164 62ee7d94 2023-01-10 thomas * Normal signal handler rules don't apply because libevent
165 62ee7d94 2023-01-10 thomas * decouples for us.
166 62ee7d94 2023-01-10 thomas */
167 62ee7d94 2023-01-10 thomas
168 62ee7d94 2023-01-10 thomas switch (sig) {
169 62ee7d94 2023-01-10 thomas case SIGHUP:
170 62ee7d94 2023-01-10 thomas log_info("%s: ignoring SIGHUP", __func__);
171 62ee7d94 2023-01-10 thomas break;
172 62ee7d94 2023-01-10 thomas case SIGUSR1:
173 62ee7d94 2023-01-10 thomas log_info("%s: ignoring SIGUSR1", __func__);
174 62ee7d94 2023-01-10 thomas break;
175 62ee7d94 2023-01-10 thomas case SIGTERM:
176 62ee7d94 2023-01-10 thomas case SIGINT:
177 62ee7d94 2023-01-10 thomas gotd_session_shutdown();
178 62ee7d94 2023-01-10 thomas /* NOTREACHED */
179 62ee7d94 2023-01-10 thomas break;
180 62ee7d94 2023-01-10 thomas default:
181 62ee7d94 2023-01-10 thomas fatalx("unexpected signal");
182 62ee7d94 2023-01-10 thomas }
183 62ee7d94 2023-01-10 thomas }
184 62ee7d94 2023-01-10 thomas
185 62ee7d94 2023-01-10 thomas static const struct got_error *
186 b92adf10 2024-03-30 thomas recv_packfile_done(struct imsg *imsg)
187 62ee7d94 2023-01-10 thomas {
188 62ee7d94 2023-01-10 thomas size_t datalen;
189 62ee7d94 2023-01-10 thomas
190 62ee7d94 2023-01-10 thomas log_debug("packfile-done received");
191 62ee7d94 2023-01-10 thomas
192 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
193 b92adf10 2024-03-30 thomas if (datalen != 0)
194 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
195 62ee7d94 2023-01-10 thomas
196 62ee7d94 2023-01-10 thomas return NULL;
197 62ee7d94 2023-01-10 thomas }
198 62ee7d94 2023-01-10 thomas
199 62ee7d94 2023-01-10 thomas static const struct got_error *
200 b92adf10 2024-03-30 thomas recv_packfile_install(struct imsg *imsg)
201 62ee7d94 2023-01-10 thomas {
202 62ee7d94 2023-01-10 thomas struct gotd_imsg_packfile_install inst;
203 62ee7d94 2023-01-10 thomas size_t datalen;
204 62ee7d94 2023-01-10 thomas
205 62ee7d94 2023-01-10 thomas log_debug("packfile-install received");
206 62ee7d94 2023-01-10 thomas
207 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
208 62ee7d94 2023-01-10 thomas if (datalen != sizeof(inst))
209 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
210 62ee7d94 2023-01-10 thomas memcpy(&inst, imsg->data, sizeof(inst));
211 62ee7d94 2023-01-10 thomas
212 62ee7d94 2023-01-10 thomas return NULL;
213 62ee7d94 2023-01-10 thomas }
214 62ee7d94 2023-01-10 thomas
215 62ee7d94 2023-01-10 thomas static const struct got_error *
216 b92adf10 2024-03-30 thomas recv_ref_updates_start(struct imsg *imsg)
217 62ee7d94 2023-01-10 thomas {
218 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_updates_start istart;
219 62ee7d94 2023-01-10 thomas size_t datalen;
220 62ee7d94 2023-01-10 thomas
221 62ee7d94 2023-01-10 thomas log_debug("ref-updates-start received");
222 62ee7d94 2023-01-10 thomas
223 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
224 62ee7d94 2023-01-10 thomas if (datalen != sizeof(istart))
225 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
226 62ee7d94 2023-01-10 thomas memcpy(&istart, imsg->data, sizeof(istart));
227 62ee7d94 2023-01-10 thomas
228 62ee7d94 2023-01-10 thomas return NULL;
229 62ee7d94 2023-01-10 thomas }
230 62ee7d94 2023-01-10 thomas
231 62ee7d94 2023-01-10 thomas static const struct got_error *
232 b92adf10 2024-03-30 thomas recv_ref_update(struct imsg *imsg)
233 62ee7d94 2023-01-10 thomas {
234 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_update iref;
235 62ee7d94 2023-01-10 thomas size_t datalen;
236 62ee7d94 2023-01-10 thomas
237 62ee7d94 2023-01-10 thomas log_debug("ref-update received");
238 62ee7d94 2023-01-10 thomas
239 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
240 62ee7d94 2023-01-10 thomas if (datalen < sizeof(iref))
241 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
242 62ee7d94 2023-01-10 thomas memcpy(&iref, imsg->data, sizeof(iref));
243 62ee7d94 2023-01-10 thomas
244 62ee7d94 2023-01-10 thomas return NULL;
245 62ee7d94 2023-01-10 thomas }
246 62ee7d94 2023-01-10 thomas
247 62ee7d94 2023-01-10 thomas static const struct got_error *
248 62ee7d94 2023-01-10 thomas send_ref_update_ok(struct gotd_session_client *client,
249 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_update *iref, const char *refname)
250 62ee7d94 2023-01-10 thomas {
251 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_update_ok iok;
252 62ee7d94 2023-01-10 thomas struct gotd_imsgev *iev = &client->iev;
253 62ee7d94 2023-01-10 thomas struct ibuf *wbuf;
254 62ee7d94 2023-01-10 thomas size_t len;
255 62ee7d94 2023-01-10 thomas
256 62ee7d94 2023-01-10 thomas memset(&iok, 0, sizeof(iok));
257 62ee7d94 2023-01-10 thomas memcpy(iok.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
258 62ee7d94 2023-01-10 thomas memcpy(iok.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
259 62ee7d94 2023-01-10 thomas iok.name_len = strlen(refname);
260 62ee7d94 2023-01-10 thomas
261 62ee7d94 2023-01-10 thomas len = sizeof(iok) + iok.name_len;
262 62ee7d94 2023-01-10 thomas wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE_OK,
263 7fed8fa4 2023-06-22 thomas gotd_session.proc_id, gotd_session.pid, len);
264 62ee7d94 2023-01-10 thomas if (wbuf == NULL)
265 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg_create REF_UPDATE_OK");
266 62ee7d94 2023-01-10 thomas
267 62ee7d94 2023-01-10 thomas if (imsg_add(wbuf, &iok, sizeof(iok)) == -1)
268 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg_add REF_UPDATE_OK");
269 62ee7d94 2023-01-10 thomas if (imsg_add(wbuf, refname, iok.name_len) == -1)
270 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg_add REF_UPDATE_OK");
271 62ee7d94 2023-01-10 thomas
272 62ee7d94 2023-01-10 thomas imsg_close(&iev->ibuf, wbuf);
273 62ee7d94 2023-01-10 thomas gotd_imsg_event_add(iev);
274 62ee7d94 2023-01-10 thomas return NULL;
275 62ee7d94 2023-01-10 thomas }
276 62ee7d94 2023-01-10 thomas
277 62ee7d94 2023-01-10 thomas static void
278 62ee7d94 2023-01-10 thomas send_refs_updated(struct gotd_session_client *client)
279 62ee7d94 2023-01-10 thomas {
280 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&client->iev, GOTD_IMSG_REFS_UPDATED,
281 7fed8fa4 2023-06-22 thomas gotd_session.proc_id, -1, NULL, 0) == -1)
282 62ee7d94 2023-01-10 thomas log_warn("imsg compose REFS_UPDATED");
283 62ee7d94 2023-01-10 thomas }
284 62ee7d94 2023-01-10 thomas
285 62ee7d94 2023-01-10 thomas static const struct got_error *
286 62ee7d94 2023-01-10 thomas send_ref_update_ng(struct gotd_session_client *client,
287 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_update *iref, const char *refname,
288 62ee7d94 2023-01-10 thomas const char *reason)
289 62ee7d94 2023-01-10 thomas {
290 62ee7d94 2023-01-10 thomas const struct got_error *ng_err;
291 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_update_ng ing;
292 62ee7d94 2023-01-10 thomas struct gotd_imsgev *iev = &client->iev;
293 62ee7d94 2023-01-10 thomas struct ibuf *wbuf;
294 62ee7d94 2023-01-10 thomas size_t len;
295 62ee7d94 2023-01-10 thomas
296 62ee7d94 2023-01-10 thomas memset(&ing, 0, sizeof(ing));
297 62ee7d94 2023-01-10 thomas memcpy(ing.old_id, iref->old_id, SHA1_DIGEST_LENGTH);
298 62ee7d94 2023-01-10 thomas memcpy(ing.new_id, iref->new_id, SHA1_DIGEST_LENGTH);
299 62ee7d94 2023-01-10 thomas ing.name_len = strlen(refname);
300 62ee7d94 2023-01-10 thomas
301 62ee7d94 2023-01-10 thomas ng_err = got_error_fmt(GOT_ERR_REF_BUSY, "%s", reason);
302 62ee7d94 2023-01-10 thomas ing.reason_len = strlen(ng_err->msg);
303 62ee7d94 2023-01-10 thomas
304 62ee7d94 2023-01-10 thomas len = sizeof(ing) + ing.name_len + ing.reason_len;
305 62ee7d94 2023-01-10 thomas wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_REF_UPDATE_NG,
306 7fed8fa4 2023-06-22 thomas gotd_session.proc_id, gotd_session.pid, len);
307 62ee7d94 2023-01-10 thomas if (wbuf == NULL)
308 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg_create REF_UPDATE_NG");
309 62ee7d94 2023-01-10 thomas
310 62ee7d94 2023-01-10 thomas if (imsg_add(wbuf, &ing, sizeof(ing)) == -1)
311 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg_add REF_UPDATE_NG");
312 62ee7d94 2023-01-10 thomas if (imsg_add(wbuf, refname, ing.name_len) == -1)
313 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg_add REF_UPDATE_NG");
314 62ee7d94 2023-01-10 thomas if (imsg_add(wbuf, ng_err->msg, ing.reason_len) == -1)
315 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg_add REF_UPDATE_NG");
316 62ee7d94 2023-01-10 thomas
317 62ee7d94 2023-01-10 thomas imsg_close(&iev->ibuf, wbuf);
318 62ee7d94 2023-01-10 thomas gotd_imsg_event_add(iev);
319 62ee7d94 2023-01-10 thomas return NULL;
320 62ee7d94 2023-01-10 thomas }
321 62ee7d94 2023-01-10 thomas
322 62ee7d94 2023-01-10 thomas static const struct got_error *
323 62ee7d94 2023-01-10 thomas install_pack(struct gotd_session_client *client, const char *repo_path,
324 62ee7d94 2023-01-10 thomas struct imsg *imsg)
325 62ee7d94 2023-01-10 thomas {
326 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
327 62ee7d94 2023-01-10 thomas struct gotd_imsg_packfile_install inst;
328 62ee7d94 2023-01-10 thomas char hex[SHA1_DIGEST_STRING_LENGTH];
329 62ee7d94 2023-01-10 thomas size_t datalen;
330 62ee7d94 2023-01-10 thomas char *packfile_path = NULL, *packidx_path = NULL;
331 62ee7d94 2023-01-10 thomas
332 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
333 62ee7d94 2023-01-10 thomas if (datalen != sizeof(inst))
334 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
335 62ee7d94 2023-01-10 thomas memcpy(&inst, imsg->data, sizeof(inst));
336 62ee7d94 2023-01-10 thomas
337 62ee7d94 2023-01-10 thomas if (client->packfile_path == NULL)
338 62ee7d94 2023-01-10 thomas return got_error_msg(GOT_ERR_BAD_REQUEST,
339 62ee7d94 2023-01-10 thomas "client has no pack file");
340 62ee7d94 2023-01-10 thomas if (client->packidx_path == NULL)
341 62ee7d94 2023-01-10 thomas return got_error_msg(GOT_ERR_BAD_REQUEST,
342 62ee7d94 2023-01-10 thomas "client has no pack file index");
343 62ee7d94 2023-01-10 thomas
344 62ee7d94 2023-01-10 thomas if (got_sha1_digest_to_str(inst.pack_sha1, hex, sizeof(hex)) == NULL)
345 62ee7d94 2023-01-10 thomas return got_error_msg(GOT_ERR_NO_SPACE,
346 62ee7d94 2023-01-10 thomas "could not convert pack file SHA1 to hex");
347 62ee7d94 2023-01-10 thomas
348 62ee7d94 2023-01-10 thomas if (asprintf(&packfile_path, "/%s/%s/pack-%s.pack",
349 62ee7d94 2023-01-10 thomas repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
350 62ee7d94 2023-01-10 thomas err = got_error_from_errno("asprintf");
351 62ee7d94 2023-01-10 thomas goto done;
352 62ee7d94 2023-01-10 thomas }
353 62ee7d94 2023-01-10 thomas
354 62ee7d94 2023-01-10 thomas if (asprintf(&packidx_path, "/%s/%s/pack-%s.idx",
355 62ee7d94 2023-01-10 thomas repo_path, GOT_OBJECTS_PACK_DIR, hex) == -1) {
356 62ee7d94 2023-01-10 thomas err = got_error_from_errno("asprintf");
357 62ee7d94 2023-01-10 thomas goto done;
358 62ee7d94 2023-01-10 thomas }
359 62ee7d94 2023-01-10 thomas
360 62ee7d94 2023-01-10 thomas if (rename(client->packfile_path, packfile_path) == -1) {
361 62ee7d94 2023-01-10 thomas err = got_error_from_errno3("rename", client->packfile_path,
362 62ee7d94 2023-01-10 thomas packfile_path);
363 62ee7d94 2023-01-10 thomas goto done;
364 62ee7d94 2023-01-10 thomas }
365 62ee7d94 2023-01-10 thomas
366 62ee7d94 2023-01-10 thomas free(client->packfile_path);
367 62ee7d94 2023-01-10 thomas client->packfile_path = NULL;
368 62ee7d94 2023-01-10 thomas
369 62ee7d94 2023-01-10 thomas if (rename(client->packidx_path, packidx_path) == -1) {
370 62ee7d94 2023-01-10 thomas err = got_error_from_errno3("rename", client->packidx_path,
371 62ee7d94 2023-01-10 thomas packidx_path);
372 62ee7d94 2023-01-10 thomas goto done;
373 62ee7d94 2023-01-10 thomas }
374 342fdad2 2024-03-19 thomas
375 342fdad2 2024-03-19 thomas /* Ensure we re-read the pack index list upon next access. */
376 342fdad2 2024-03-19 thomas gotd_session.repo->pack_path_mtime.tv_sec = 0;
377 342fdad2 2024-03-19 thomas gotd_session.repo->pack_path_mtime.tv_nsec = 0;
378 62ee7d94 2023-01-10 thomas
379 62ee7d94 2023-01-10 thomas free(client->packidx_path);
380 62ee7d94 2023-01-10 thomas client->packidx_path = NULL;
381 62ee7d94 2023-01-10 thomas done:
382 62ee7d94 2023-01-10 thomas free(packfile_path);
383 62ee7d94 2023-01-10 thomas free(packidx_path);
384 62ee7d94 2023-01-10 thomas return err;
385 62ee7d94 2023-01-10 thomas }
386 62ee7d94 2023-01-10 thomas
387 62ee7d94 2023-01-10 thomas static const struct got_error *
388 62ee7d94 2023-01-10 thomas begin_ref_updates(struct gotd_session_client *client, struct imsg *imsg)
389 62ee7d94 2023-01-10 thomas {
390 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_updates_start istart;
391 62ee7d94 2023-01-10 thomas size_t datalen;
392 62ee7d94 2023-01-10 thomas
393 62ee7d94 2023-01-10 thomas if (client->nref_updates != -1)
394 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
395 62ee7d94 2023-01-10 thomas
396 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
397 62ee7d94 2023-01-10 thomas if (datalen != sizeof(istart))
398 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
399 62ee7d94 2023-01-10 thomas memcpy(&istart, imsg->data, sizeof(istart));
400 62ee7d94 2023-01-10 thomas
401 62ee7d94 2023-01-10 thomas if (istart.nref_updates <= 0)
402 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
403 62ee7d94 2023-01-10 thomas
404 62ee7d94 2023-01-10 thomas client->nref_updates = istart.nref_updates;
405 ce1bfad9 2024-03-30 thomas return NULL;
406 ce1bfad9 2024-03-30 thomas }
407 ce1bfad9 2024-03-30 thomas
408 ce1bfad9 2024-03-30 thomas static const struct got_error *
409 ce1bfad9 2024-03-30 thomas validate_namespace(const char *namespace)
410 ce1bfad9 2024-03-30 thomas {
411 ce1bfad9 2024-03-30 thomas size_t len = strlen(namespace);
412 ce1bfad9 2024-03-30 thomas
413 ce1bfad9 2024-03-30 thomas if (len < 5 || strncmp("refs/", namespace, 5) != 0 ||
414 ce1bfad9 2024-03-30 thomas namespace[len - 1] != '/') {
415 ce1bfad9 2024-03-30 thomas return got_error_fmt(GOT_ERR_BAD_REF_NAME,
416 ce1bfad9 2024-03-30 thomas "reference namespace '%s'", namespace);
417 ce1bfad9 2024-03-30 thomas }
418 ce1bfad9 2024-03-30 thomas
419 62ee7d94 2023-01-10 thomas return NULL;
420 62ee7d94 2023-01-10 thomas }
421 62ee7d94 2023-01-10 thomas
422 62ee7d94 2023-01-10 thomas static const struct got_error *
423 ce1bfad9 2024-03-30 thomas queue_notification(struct got_object_id *old_id, struct got_object_id *new_id,
424 ce1bfad9 2024-03-30 thomas struct got_repository *repo, struct got_reference *ref)
425 ce1bfad9 2024-03-30 thomas {
426 ce1bfad9 2024-03-30 thomas const struct got_error *err = NULL;
427 74e85132 2024-03-30 thomas struct gotd_repo *repo_cfg = gotd_session.repo_cfg;
428 74e85132 2024-03-30 thomas struct gotd_imsgev *iev = &gotd_session.repo_child_iev;
429 ce1bfad9 2024-03-30 thomas struct got_pathlist_entry *pe;
430 ce1bfad9 2024-03-30 thomas struct gotd_session_notif *notif;
431 ce1bfad9 2024-03-30 thomas
432 ce1bfad9 2024-03-30 thomas if (iev->ibuf.fd == -1 ||
433 ce1bfad9 2024-03-30 thomas STAILQ_EMPTY(&repo_cfg->notification_targets))
434 ce1bfad9 2024-03-30 thomas return NULL; /* notifications unused */
435 ce1bfad9 2024-03-30 thomas
436 ce1bfad9 2024-03-30 thomas TAILQ_FOREACH(pe, &repo_cfg->notification_refs, entry) {
437 ce1bfad9 2024-03-30 thomas const char *refname = pe->path;
438 ce1bfad9 2024-03-30 thomas if (strcmp(got_ref_get_name(ref), refname) == 0)
439 ce1bfad9 2024-03-30 thomas break;
440 ce1bfad9 2024-03-30 thomas }
441 ce1bfad9 2024-03-30 thomas if (pe == NULL) {
442 ce1bfad9 2024-03-30 thomas TAILQ_FOREACH(pe, &repo_cfg->notification_ref_namespaces,
443 ce1bfad9 2024-03-30 thomas entry) {
444 ce1bfad9 2024-03-30 thomas const char *namespace = pe->path;
445 ce1bfad9 2024-03-30 thomas
446 ce1bfad9 2024-03-30 thomas err = validate_namespace(namespace);
447 ce1bfad9 2024-03-30 thomas if (err)
448 ce1bfad9 2024-03-30 thomas return err;
449 ce1bfad9 2024-03-30 thomas if (strncmp(namespace, got_ref_get_name(ref),
450 ce1bfad9 2024-03-30 thomas strlen(namespace)) == 0)
451 ce1bfad9 2024-03-30 thomas break;
452 ce1bfad9 2024-03-30 thomas }
453 ce1bfad9 2024-03-30 thomas }
454 ce1bfad9 2024-03-30 thomas
455 ce1bfad9 2024-03-30 thomas /*
456 ce1bfad9 2024-03-30 thomas * If a branch or a reference namespace was specified in the
457 ce1bfad9 2024-03-30 thomas * configuration file then only send notifications if a match
458 ce1bfad9 2024-03-30 thomas * was found.
459 ce1bfad9 2024-03-30 thomas */
460 ce1bfad9 2024-03-30 thomas if (pe == NULL && (!TAILQ_EMPTY(&repo_cfg->notification_refs) ||
461 ce1bfad9 2024-03-30 thomas !TAILQ_EMPTY(&repo_cfg->notification_ref_namespaces)))
462 ce1bfad9 2024-03-30 thomas return NULL;
463 ce1bfad9 2024-03-30 thomas
464 ce1bfad9 2024-03-30 thomas notif = calloc(1, sizeof(*notif));
465 ce1bfad9 2024-03-30 thomas if (notif == NULL)
466 ce1bfad9 2024-03-30 thomas return got_error_from_errno("calloc");
467 ce1bfad9 2024-03-30 thomas
468 ce1bfad9 2024-03-30 thomas notif->fd = -1;
469 ce1bfad9 2024-03-30 thomas
470 ce1bfad9 2024-03-30 thomas if (old_id == NULL)
471 ce1bfad9 2024-03-30 thomas notif->action = GOTD_NOTIF_ACTION_CREATED;
472 ce1bfad9 2024-03-30 thomas else if (new_id == NULL)
473 ce1bfad9 2024-03-30 thomas notif->action = GOTD_NOTIF_ACTION_REMOVED;
474 ce1bfad9 2024-03-30 thomas else
475 ce1bfad9 2024-03-30 thomas notif->action = GOTD_NOTIF_ACTION_CHANGED;
476 ce1bfad9 2024-03-30 thomas
477 ce1bfad9 2024-03-30 thomas if (old_id != NULL)
478 ce1bfad9 2024-03-30 thomas memcpy(&notif->old_id, old_id, sizeof(notif->old_id));
479 ce1bfad9 2024-03-30 thomas if (new_id != NULL)
480 ce1bfad9 2024-03-30 thomas memcpy(&notif->new_id, new_id, sizeof(notif->new_id));
481 ce1bfad9 2024-03-30 thomas
482 ce1bfad9 2024-03-30 thomas notif->refname = strdup(got_ref_get_name(ref));
483 ce1bfad9 2024-03-30 thomas if (notif->refname == NULL) {
484 ce1bfad9 2024-03-30 thomas err = got_error_from_errno("strdup");
485 ce1bfad9 2024-03-30 thomas goto done;
486 ce1bfad9 2024-03-30 thomas }
487 ce1bfad9 2024-03-30 thomas
488 ce1bfad9 2024-03-30 thomas STAILQ_INSERT_TAIL(&notifications, notif, entry);
489 ce1bfad9 2024-03-30 thomas done:
490 ce1bfad9 2024-03-30 thomas if (err && notif) {
491 ce1bfad9 2024-03-30 thomas free(notif->refname);
492 ce1bfad9 2024-03-30 thomas free(notif);
493 ce1bfad9 2024-03-30 thomas }
494 ce1bfad9 2024-03-30 thomas return err;
495 ce1bfad9 2024-03-30 thomas }
496 ce1bfad9 2024-03-30 thomas
497 ce1bfad9 2024-03-30 thomas /* Forward notification content to the NOTIFY process. */
498 ce1bfad9 2024-03-30 thomas static const struct got_error *
499 ce1bfad9 2024-03-30 thomas forward_notification(struct gotd_session_client *client, struct imsg *imsg)
500 ce1bfad9 2024-03-30 thomas {
501 ce1bfad9 2024-03-30 thomas const struct got_error *err = NULL;
502 ce1bfad9 2024-03-30 thomas struct gotd_imsgev *iev = &gotd_session.notifier_iev;
503 ce1bfad9 2024-03-30 thomas struct gotd_session_notif *notif;
504 ce1bfad9 2024-03-30 thomas struct gotd_imsg_notification_content icontent;
505 ce1bfad9 2024-03-30 thomas char *refname = NULL;
506 ce1bfad9 2024-03-30 thomas size_t datalen;
507 ce1bfad9 2024-03-30 thomas struct gotd_imsg_notify inotify;
508 ce1bfad9 2024-03-30 thomas const char *action;
509 ce1bfad9 2024-03-30 thomas
510 ce1bfad9 2024-03-30 thomas memset(&inotify, 0, sizeof(inotify));
511 ce1bfad9 2024-03-30 thomas
512 ce1bfad9 2024-03-30 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
513 ce1bfad9 2024-03-30 thomas if (datalen < sizeof(icontent))
514 ce1bfad9 2024-03-30 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
515 ce1bfad9 2024-03-30 thomas memcpy(&icontent, imsg->data, sizeof(icontent));
516 ce1bfad9 2024-03-30 thomas if (datalen != sizeof(icontent) + icontent.refname_len)
517 ce1bfad9 2024-03-30 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
518 ce1bfad9 2024-03-30 thomas refname = strndup(imsg->data + sizeof(icontent), icontent.refname_len);
519 ce1bfad9 2024-03-30 thomas if (refname == NULL)
520 ce1bfad9 2024-03-30 thomas return got_error_from_errno("strndup");
521 ce1bfad9 2024-03-30 thomas
522 ce1bfad9 2024-03-30 thomas notif = STAILQ_FIRST(&notifications);
523 ce1bfad9 2024-03-30 thomas if (notif == NULL)
524 ce1bfad9 2024-03-30 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
525 ce1bfad9 2024-03-30 thomas
526 ce1bfad9 2024-03-30 thomas STAILQ_REMOVE(&notifications, notif, gotd_session_notif, entry);
527 ce1bfad9 2024-03-30 thomas
528 ce1bfad9 2024-03-30 thomas if (notif->action != icontent.action || notif->fd == -1 ||
529 ce1bfad9 2024-03-30 thomas strcmp(notif->refname, refname) != 0) {
530 ce1bfad9 2024-03-30 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
531 ce1bfad9 2024-03-30 thomas goto done;
532 ce1bfad9 2024-03-30 thomas }
533 ce1bfad9 2024-03-30 thomas if (notif->action == GOTD_NOTIF_ACTION_CREATED) {
534 ce1bfad9 2024-03-30 thomas if (memcmp(notif->new_id.sha1, icontent.new_id,
535 ce1bfad9 2024-03-30 thomas SHA1_DIGEST_LENGTH) != 0) {
536 ce1bfad9 2024-03-30 thomas err = got_error_msg(GOT_ERR_PRIVSEP_MSG,
537 ce1bfad9 2024-03-30 thomas "received notification content for unknown event");
538 ce1bfad9 2024-03-30 thomas goto done;
539 ce1bfad9 2024-03-30 thomas }
540 ce1bfad9 2024-03-30 thomas } else if (notif->action == GOTD_NOTIF_ACTION_REMOVED) {
541 ce1bfad9 2024-03-30 thomas if (memcmp(notif->old_id.sha1, icontent.old_id,
542 ce1bfad9 2024-03-30 thomas SHA1_DIGEST_LENGTH) != 0) {
543 ce1bfad9 2024-03-30 thomas err = got_error_msg(GOT_ERR_PRIVSEP_MSG,
544 ce1bfad9 2024-03-30 thomas "received notification content for unknown event");
545 ce1bfad9 2024-03-30 thomas goto done;
546 ce1bfad9 2024-03-30 thomas }
547 ce1bfad9 2024-03-30 thomas } else if (memcmp(notif->old_id.sha1, icontent.old_id,
548 ce1bfad9 2024-03-30 thomas SHA1_DIGEST_LENGTH) != 0 ||
549 ce1bfad9 2024-03-30 thomas memcmp(notif->new_id.sha1, icontent.new_id,
550 ce1bfad9 2024-03-30 thomas SHA1_DIGEST_LENGTH) != 0) {
551 ce1bfad9 2024-03-30 thomas err = got_error_msg(GOT_ERR_PRIVSEP_MSG,
552 ce1bfad9 2024-03-30 thomas "received notification content for unknown event");
553 ce1bfad9 2024-03-30 thomas goto done;
554 ce1bfad9 2024-03-30 thomas }
555 ce1bfad9 2024-03-30 thomas
556 ce1bfad9 2024-03-30 thomas switch (notif->action) {
557 ce1bfad9 2024-03-30 thomas case GOTD_NOTIF_ACTION_CREATED:
558 ce1bfad9 2024-03-30 thomas action = "created";
559 ce1bfad9 2024-03-30 thomas break;
560 ce1bfad9 2024-03-30 thomas case GOTD_NOTIF_ACTION_REMOVED:
561 ce1bfad9 2024-03-30 thomas action = "removed";
562 ce1bfad9 2024-03-30 thomas break;
563 ce1bfad9 2024-03-30 thomas case GOTD_NOTIF_ACTION_CHANGED:
564 ce1bfad9 2024-03-30 thomas action = "changed";
565 ce1bfad9 2024-03-30 thomas break;
566 ce1bfad9 2024-03-30 thomas default:
567 ce1bfad9 2024-03-30 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
568 ce1bfad9 2024-03-30 thomas goto done;
569 ce1bfad9 2024-03-30 thomas }
570 ce1bfad9 2024-03-30 thomas
571 ce1bfad9 2024-03-30 thomas strlcpy(inotify.repo_name, gotd_session.repo_cfg->name,
572 ce1bfad9 2024-03-30 thomas sizeof(inotify.repo_name));
573 ce1bfad9 2024-03-30 thomas
574 ce1bfad9 2024-03-30 thomas snprintf(inotify.subject_line, sizeof(inotify.subject_line),
575 ce1bfad9 2024-03-30 thomas "%s: %s %s %s", gotd_session.repo_cfg->name,
576 ce1bfad9 2024-03-30 thomas client->username, action, notif->refname);
577 ce1bfad9 2024-03-30 thomas
578 ce1bfad9 2024-03-30 thomas if (gotd_imsg_compose_event(iev, GOTD_IMSG_NOTIFY,
579 ce1bfad9 2024-03-30 thomas PROC_SESSION_WRITE, notif->fd, &inotify, sizeof(inotify))
580 ce1bfad9 2024-03-30 thomas == -1) {
581 ce1bfad9 2024-03-30 thomas err = got_error_from_errno("imsg compose NOTIFY");
582 ce1bfad9 2024-03-30 thomas goto done;
583 ce1bfad9 2024-03-30 thomas }
584 ce1bfad9 2024-03-30 thomas notif->fd = -1;
585 ce1bfad9 2024-03-30 thomas done:
586 ce1bfad9 2024-03-30 thomas if (notif->fd != -1)
587 ce1bfad9 2024-03-30 thomas close(notif->fd);
588 ce1bfad9 2024-03-30 thomas free(notif);
589 ce1bfad9 2024-03-30 thomas free(refname);
590 ce1bfad9 2024-03-30 thomas return err;
591 ce1bfad9 2024-03-30 thomas }
592 ce1bfad9 2024-03-30 thomas
593 ce1bfad9 2024-03-30 thomas /* Request notification content from REPO_WRITE process. */
594 ce1bfad9 2024-03-30 thomas static const struct got_error *
595 ce1bfad9 2024-03-30 thomas request_notification(struct gotd_session_notif *notif)
596 ce1bfad9 2024-03-30 thomas {
597 ce1bfad9 2024-03-30 thomas const struct got_error *err = NULL;
598 74e85132 2024-03-30 thomas struct gotd_imsgev *iev = &gotd_session.repo_child_iev;
599 ce1bfad9 2024-03-30 thomas struct gotd_imsg_notification_content icontent;
600 ce1bfad9 2024-03-30 thomas struct ibuf *wbuf;
601 ce1bfad9 2024-03-30 thomas size_t len;
602 ce1bfad9 2024-03-30 thomas int fd;
603 ce1bfad9 2024-03-30 thomas
604 ce1bfad9 2024-03-30 thomas fd = got_opentempfd();
605 ce1bfad9 2024-03-30 thomas if (fd == -1)
606 ce1bfad9 2024-03-30 thomas return got_error_from_errno("got_opentemp");
607 ce1bfad9 2024-03-30 thomas
608 ce1bfad9 2024-03-30 thomas memset(&icontent, 0, sizeof(icontent));
609 ce1bfad9 2024-03-30 thomas
610 ce1bfad9 2024-03-30 thomas icontent.action = notif->action;
611 ce1bfad9 2024-03-30 thomas memcpy(&icontent.old_id, &notif->old_id, sizeof(notif->old_id));
612 ce1bfad9 2024-03-30 thomas memcpy(&icontent.new_id, &notif->new_id, sizeof(notif->new_id));
613 ce1bfad9 2024-03-30 thomas icontent.refname_len = strlen(notif->refname);
614 ce1bfad9 2024-03-30 thomas
615 ce1bfad9 2024-03-30 thomas len = sizeof(icontent) + icontent.refname_len;
616 ce1bfad9 2024-03-30 thomas wbuf = imsg_create(&iev->ibuf, GOTD_IMSG_NOTIFY,
617 ce1bfad9 2024-03-30 thomas gotd_session.proc_id, gotd_session.pid, len);
618 ce1bfad9 2024-03-30 thomas if (wbuf == NULL) {
619 ce1bfad9 2024-03-30 thomas err = got_error_from_errno("imsg_create NOTIFY");
620 ce1bfad9 2024-03-30 thomas goto done;
621 ce1bfad9 2024-03-30 thomas }
622 ce1bfad9 2024-03-30 thomas if (imsg_add(wbuf, &icontent, sizeof(icontent)) == -1) {
623 ce1bfad9 2024-03-30 thomas err = got_error_from_errno("imsg_add NOTIFY");
624 ce1bfad9 2024-03-30 thomas goto done;
625 ce1bfad9 2024-03-30 thomas }
626 ce1bfad9 2024-03-30 thomas if (imsg_add(wbuf, notif->refname, icontent.refname_len) == -1) {
627 ce1bfad9 2024-03-30 thomas err = got_error_from_errno("imsg_add NOTIFY");
628 ce1bfad9 2024-03-30 thomas goto done;
629 ce1bfad9 2024-03-30 thomas }
630 ce1bfad9 2024-03-30 thomas
631 ce1bfad9 2024-03-30 thomas notif->fd = dup(fd);
632 ce1bfad9 2024-03-30 thomas if (notif->fd == -1) {
633 ce1bfad9 2024-03-30 thomas err = got_error_from_errno("dup");
634 ce1bfad9 2024-03-30 thomas goto done;
635 ce1bfad9 2024-03-30 thomas }
636 ce1bfad9 2024-03-30 thomas
637 ce1bfad9 2024-03-30 thomas ibuf_fd_set(wbuf, fd);
638 ce1bfad9 2024-03-30 thomas fd = -1;
639 ce1bfad9 2024-03-30 thomas
640 ce1bfad9 2024-03-30 thomas imsg_close(&iev->ibuf, wbuf);
641 ce1bfad9 2024-03-30 thomas gotd_imsg_event_add(iev);
642 ce1bfad9 2024-03-30 thomas done:
643 ce1bfad9 2024-03-30 thomas if (err && fd != -1)
644 ce1bfad9 2024-03-30 thomas close(fd);
645 ce1bfad9 2024-03-30 thomas return err;
646 ce1bfad9 2024-03-30 thomas }
647 ce1bfad9 2024-03-30 thomas
648 ce1bfad9 2024-03-30 thomas static const struct got_error *
649 d98779cd 2023-01-19 thomas update_ref(int *shut, struct gotd_session_client *client,
650 d98779cd 2023-01-19 thomas const char *repo_path, struct imsg *imsg)
651 62ee7d94 2023-01-10 thomas {
652 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
653 e8c5d4f8 2024-03-19 thomas struct got_repository *repo = gotd_session.repo;
654 62ee7d94 2023-01-10 thomas struct got_reference *ref = NULL;
655 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_update iref;
656 62ee7d94 2023-01-10 thomas struct got_object_id old_id, new_id;
657 ce1bfad9 2024-03-30 thomas struct gotd_session_notif *notif;
658 62ee7d94 2023-01-10 thomas struct got_object_id *id = NULL;
659 62ee7d94 2023-01-10 thomas char *refname = NULL;
660 62ee7d94 2023-01-10 thomas size_t datalen;
661 62ee7d94 2023-01-10 thomas int locked = 0;
662 8a9c582b 2023-06-22 thomas char hex1[SHA1_DIGEST_STRING_LENGTH];
663 8a9c582b 2023-06-22 thomas char hex2[SHA1_DIGEST_STRING_LENGTH];
664 62ee7d94 2023-01-10 thomas
665 62ee7d94 2023-01-10 thomas log_debug("update-ref from uid %d", client->euid);
666 62ee7d94 2023-01-10 thomas
667 62ee7d94 2023-01-10 thomas if (client->nref_updates <= 0)
668 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
669 62ee7d94 2023-01-10 thomas
670 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
671 62ee7d94 2023-01-10 thomas if (datalen < sizeof(iref))
672 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
673 62ee7d94 2023-01-10 thomas memcpy(&iref, imsg->data, sizeof(iref));
674 62ee7d94 2023-01-10 thomas if (datalen != sizeof(iref) + iref.name_len)
675 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
676 fcbb06bf 2023-01-14 thomas refname = strndup(imsg->data + sizeof(iref), iref.name_len);
677 62ee7d94 2023-01-10 thomas if (refname == NULL)
678 fcbb06bf 2023-01-14 thomas return got_error_from_errno("strndup");
679 62ee7d94 2023-01-10 thomas
680 62ee7d94 2023-01-10 thomas log_debug("updating ref %s for uid %d", refname, client->euid);
681 62ee7d94 2023-01-10 thomas
682 62ee7d94 2023-01-10 thomas memcpy(old_id.sha1, iref.old_id, SHA1_DIGEST_LENGTH);
683 62ee7d94 2023-01-10 thomas memcpy(new_id.sha1, iref.new_id, SHA1_DIGEST_LENGTH);
684 44f875ad 2024-01-18 thomas err = got_repo_find_object_id(iref.delete_ref ? &old_id : &new_id,
685 44f875ad 2024-01-18 thomas repo);
686 62ee7d94 2023-01-10 thomas if (err)
687 62ee7d94 2023-01-10 thomas goto done;
688 62ee7d94 2023-01-10 thomas
689 62ee7d94 2023-01-10 thomas if (iref.ref_is_new) {
690 62ee7d94 2023-01-10 thomas err = got_ref_open(&ref, repo, refname, 0);
691 62ee7d94 2023-01-10 thomas if (err) {
692 62ee7d94 2023-01-10 thomas if (err->code != GOT_ERR_NOT_REF)
693 62ee7d94 2023-01-10 thomas goto done;
694 62ee7d94 2023-01-10 thomas err = got_ref_alloc(&ref, refname, &new_id);
695 62ee7d94 2023-01-10 thomas if (err)
696 62ee7d94 2023-01-10 thomas goto done;
697 62ee7d94 2023-01-10 thomas err = got_ref_write(ref, repo); /* will lock/unlock */
698 62ee7d94 2023-01-10 thomas if (err)
699 62ee7d94 2023-01-10 thomas goto done;
700 ce1bfad9 2024-03-30 thomas err = queue_notification(NULL, &new_id, repo, ref);
701 ce1bfad9 2024-03-30 thomas if (err)
702 ce1bfad9 2024-03-30 thomas goto done;
703 62ee7d94 2023-01-10 thomas } else {
704 8a9c582b 2023-06-22 thomas err = got_ref_resolve(&id, repo, ref);
705 8a9c582b 2023-06-22 thomas if (err)
706 8a9c582b 2023-06-22 thomas goto done;
707 8a9c582b 2023-06-22 thomas got_object_id_hex(&new_id, hex1, sizeof(hex1));
708 8a9c582b 2023-06-22 thomas got_object_id_hex(id, hex2, sizeof(hex2));
709 62ee7d94 2023-01-10 thomas err = got_error_fmt(GOT_ERR_REF_BUSY,
710 8a9c582b 2023-06-22 thomas "Addition %s: %s failed; %s: %s has been "
711 8a9c582b 2023-06-22 thomas "created by someone else while transaction "
712 8a9c582b 2023-06-22 thomas "was in progress",
713 8a9c582b 2023-06-22 thomas got_ref_get_name(ref), hex1,
714 8a9c582b 2023-06-22 thomas got_ref_get_name(ref), hex2);
715 62ee7d94 2023-01-10 thomas goto done;
716 62ee7d94 2023-01-10 thomas }
717 49563dfb 2023-01-28 thomas } else if (iref.delete_ref) {
718 49563dfb 2023-01-28 thomas err = got_ref_open(&ref, repo, refname, 1 /* lock */);
719 49563dfb 2023-01-28 thomas if (err)
720 49563dfb 2023-01-28 thomas goto done;
721 49563dfb 2023-01-28 thomas locked = 1;
722 49563dfb 2023-01-28 thomas
723 49563dfb 2023-01-28 thomas err = got_ref_resolve(&id, repo, ref);
724 49563dfb 2023-01-28 thomas if (err)
725 49563dfb 2023-01-28 thomas goto done;
726 49563dfb 2023-01-28 thomas
727 49563dfb 2023-01-28 thomas if (got_object_id_cmp(id, &old_id) != 0) {
728 8a9c582b 2023-06-22 thomas got_object_id_hex(&old_id, hex1, sizeof(hex1));
729 8a9c582b 2023-06-22 thomas got_object_id_hex(id, hex2, sizeof(hex2));
730 49563dfb 2023-01-28 thomas err = got_error_fmt(GOT_ERR_REF_BUSY,
731 8a9c582b 2023-06-22 thomas "Deletion %s: %s failed; %s: %s has been "
732 8a9c582b 2023-06-22 thomas "created by someone else while transaction "
733 8a9c582b 2023-06-22 thomas "was in progress",
734 8a9c582b 2023-06-22 thomas got_ref_get_name(ref), hex1,
735 8a9c582b 2023-06-22 thomas got_ref_get_name(ref), hex2);
736 49563dfb 2023-01-28 thomas goto done;
737 49563dfb 2023-01-28 thomas }
738 49563dfb 2023-01-28 thomas
739 49563dfb 2023-01-28 thomas err = got_ref_delete(ref, repo);
740 49563dfb 2023-01-28 thomas if (err)
741 49563dfb 2023-01-28 thomas goto done;
742 ce1bfad9 2024-03-30 thomas err = queue_notification(&old_id, NULL, repo, ref);
743 ce1bfad9 2024-03-30 thomas if (err)
744 ce1bfad9 2024-03-30 thomas goto done;
745 49563dfb 2023-01-28 thomas free(id);
746 49563dfb 2023-01-28 thomas id = NULL;
747 62ee7d94 2023-01-10 thomas } else {
748 62ee7d94 2023-01-10 thomas err = got_ref_open(&ref, repo, refname, 1 /* lock */);
749 62ee7d94 2023-01-10 thomas if (err)
750 62ee7d94 2023-01-10 thomas goto done;
751 62ee7d94 2023-01-10 thomas locked = 1;
752 62ee7d94 2023-01-10 thomas
753 62ee7d94 2023-01-10 thomas err = got_ref_resolve(&id, repo, ref);
754 62ee7d94 2023-01-10 thomas if (err)
755 62ee7d94 2023-01-10 thomas goto done;
756 62ee7d94 2023-01-10 thomas
757 62ee7d94 2023-01-10 thomas if (got_object_id_cmp(id, &old_id) != 0) {
758 8a9c582b 2023-06-22 thomas got_object_id_hex(&old_id, hex1, sizeof(hex1));
759 8a9c582b 2023-06-22 thomas got_object_id_hex(id, hex2, sizeof(hex2));
760 62ee7d94 2023-01-10 thomas err = got_error_fmt(GOT_ERR_REF_BUSY,
761 8a9c582b 2023-06-22 thomas "Update %s: %s failed; %s: %s has been "
762 8a9c582b 2023-06-22 thomas "created by someone else while transaction "
763 8a9c582b 2023-06-22 thomas "was in progress",
764 8a9c582b 2023-06-22 thomas got_ref_get_name(ref), hex1,
765 8a9c582b 2023-06-22 thomas got_ref_get_name(ref), hex2);
766 62ee7d94 2023-01-10 thomas goto done;
767 62ee7d94 2023-01-10 thomas }
768 62ee7d94 2023-01-10 thomas
769 169def41 2023-06-22 thomas if (got_object_id_cmp(&new_id, &old_id) != 0) {
770 169def41 2023-06-22 thomas err = got_ref_change_ref(ref, &new_id);
771 169def41 2023-06-22 thomas if (err)
772 169def41 2023-06-22 thomas goto done;
773 169def41 2023-06-22 thomas err = got_ref_write(ref, repo);
774 169def41 2023-06-22 thomas if (err)
775 169def41 2023-06-22 thomas goto done;
776 ce1bfad9 2024-03-30 thomas err = queue_notification(&old_id, &new_id, repo, ref);
777 ce1bfad9 2024-03-30 thomas if (err)
778 ce1bfad9 2024-03-30 thomas goto done;
779 169def41 2023-06-22 thomas }
780 62ee7d94 2023-01-10 thomas
781 62ee7d94 2023-01-10 thomas free(id);
782 62ee7d94 2023-01-10 thomas id = NULL;
783 62ee7d94 2023-01-10 thomas }
784 62ee7d94 2023-01-10 thomas done:
785 62ee7d94 2023-01-10 thomas if (err) {
786 62ee7d94 2023-01-10 thomas if (err->code == GOT_ERR_LOCKFILE_TIMEOUT) {
787 62ee7d94 2023-01-10 thomas err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
788 62ee7d94 2023-01-10 thomas "could not acquire exclusive file lock for %s",
789 62ee7d94 2023-01-10 thomas refname);
790 62ee7d94 2023-01-10 thomas }
791 62ee7d94 2023-01-10 thomas send_ref_update_ng(client, &iref, refname, err->msg);
792 62ee7d94 2023-01-10 thomas } else
793 62ee7d94 2023-01-10 thomas send_ref_update_ok(client, &iref, refname);
794 62ee7d94 2023-01-10 thomas
795 62ee7d94 2023-01-10 thomas if (client->nref_updates > 0) {
796 62ee7d94 2023-01-10 thomas client->nref_updates--;
797 d98779cd 2023-01-19 thomas if (client->nref_updates == 0) {
798 62ee7d94 2023-01-10 thomas send_refs_updated(client);
799 ce1bfad9 2024-03-30 thomas notif = STAILQ_FIRST(&notifications);
800 ce1bfad9 2024-03-30 thomas if (notif) {
801 176350bf 2024-03-30 thomas gotd_session.state = GOTD_STATE_NOTIFY;
802 ce1bfad9 2024-03-30 thomas err = request_notification(notif);
803 ce1bfad9 2024-03-30 thomas if (err) {
804 ce1bfad9 2024-03-30 thomas log_warn("could not send notification: "
805 ce1bfad9 2024-03-30 thomas "%s", err->msg);
806 ce1bfad9 2024-03-30 thomas client->flush_disconnect = 1;
807 ce1bfad9 2024-03-30 thomas }
808 ce1bfad9 2024-03-30 thomas } else
809 ce1bfad9 2024-03-30 thomas client->flush_disconnect = 1;
810 d98779cd 2023-01-19 thomas }
811 62ee7d94 2023-01-10 thomas
812 62ee7d94 2023-01-10 thomas }
813 62ee7d94 2023-01-10 thomas if (locked) {
814 62ee7d94 2023-01-10 thomas const struct got_error *unlock_err;
815 62ee7d94 2023-01-10 thomas unlock_err = got_ref_unlock(ref);
816 62ee7d94 2023-01-10 thomas if (unlock_err && err == NULL)
817 62ee7d94 2023-01-10 thomas err = unlock_err;
818 62ee7d94 2023-01-10 thomas }
819 62ee7d94 2023-01-10 thomas if (ref)
820 62ee7d94 2023-01-10 thomas got_ref_close(ref);
821 62ee7d94 2023-01-10 thomas free(refname);
822 62ee7d94 2023-01-10 thomas free(id);
823 62ee7d94 2023-01-10 thomas return err;
824 62ee7d94 2023-01-10 thomas }
825 62ee7d94 2023-01-10 thomas
826 ce1bfad9 2024-03-30 thomas static const struct got_error *
827 b92adf10 2024-03-30 thomas recv_notification_content(struct imsg *imsg)
828 ce1bfad9 2024-03-30 thomas {
829 ce1bfad9 2024-03-30 thomas struct gotd_imsg_notification_content inotif;
830 ce1bfad9 2024-03-30 thomas size_t datalen;
831 ce1bfad9 2024-03-30 thomas
832 ce1bfad9 2024-03-30 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
833 ce1bfad9 2024-03-30 thomas if (datalen < sizeof(inotif))
834 ce1bfad9 2024-03-30 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
835 ce1bfad9 2024-03-30 thomas memcpy(&inotif, imsg->data, sizeof(inotif));
836 ce1bfad9 2024-03-30 thomas
837 ce1bfad9 2024-03-30 thomas return NULL;
838 ce1bfad9 2024-03-30 thomas }
839 ce1bfad9 2024-03-30 thomas
840 62ee7d94 2023-01-10 thomas static void
841 62ee7d94 2023-01-10 thomas session_dispatch_repo_child(int fd, short event, void *arg)
842 62ee7d94 2023-01-10 thomas {
843 62ee7d94 2023-01-10 thomas struct gotd_imsgev *iev = arg;
844 62ee7d94 2023-01-10 thomas struct imsgbuf *ibuf = &iev->ibuf;
845 62ee7d94 2023-01-10 thomas struct gotd_session_client *client = &gotd_session_client;
846 62ee7d94 2023-01-10 thomas ssize_t n;
847 62ee7d94 2023-01-10 thomas int shut = 0;
848 62ee7d94 2023-01-10 thomas struct imsg imsg;
849 62ee7d94 2023-01-10 thomas
850 62ee7d94 2023-01-10 thomas if (event & EV_READ) {
851 62ee7d94 2023-01-10 thomas if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
852 62ee7d94 2023-01-10 thomas fatal("imsg_read error");
853 62ee7d94 2023-01-10 thomas if (n == 0) {
854 62ee7d94 2023-01-10 thomas /* Connection closed. */
855 62ee7d94 2023-01-10 thomas shut = 1;
856 62ee7d94 2023-01-10 thomas goto done;
857 62ee7d94 2023-01-10 thomas }
858 62ee7d94 2023-01-10 thomas }
859 62ee7d94 2023-01-10 thomas
860 62ee7d94 2023-01-10 thomas if (event & EV_WRITE) {
861 62ee7d94 2023-01-10 thomas n = msgbuf_write(&ibuf->w);
862 62ee7d94 2023-01-10 thomas if (n == -1 && errno != EAGAIN)
863 62ee7d94 2023-01-10 thomas fatal("msgbuf_write");
864 62ee7d94 2023-01-10 thomas if (n == 0) {
865 62ee7d94 2023-01-10 thomas /* Connection closed. */
866 62ee7d94 2023-01-10 thomas shut = 1;
867 62ee7d94 2023-01-10 thomas goto done;
868 62ee7d94 2023-01-10 thomas }
869 62ee7d94 2023-01-10 thomas }
870 62ee7d94 2023-01-10 thomas
871 62ee7d94 2023-01-10 thomas for (;;) {
872 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
873 62ee7d94 2023-01-10 thomas uint32_t client_id = 0;
874 62ee7d94 2023-01-10 thomas int do_disconnect = 0;
875 62ee7d94 2023-01-10 thomas int do_ref_updates = 0, do_ref_update = 0;
876 ce1bfad9 2024-03-30 thomas int do_packfile_install = 0, do_notify = 0;
877 62ee7d94 2023-01-10 thomas
878 62ee7d94 2023-01-10 thomas if ((n = imsg_get(ibuf, &imsg)) == -1)
879 62ee7d94 2023-01-10 thomas fatal("%s: imsg_get error", __func__);
880 62ee7d94 2023-01-10 thomas if (n == 0) /* No more messages. */
881 62ee7d94 2023-01-10 thomas break;
882 62ee7d94 2023-01-10 thomas
883 62ee7d94 2023-01-10 thomas switch (imsg.hdr.type) {
884 62ee7d94 2023-01-10 thomas case GOTD_IMSG_ERROR:
885 62ee7d94 2023-01-10 thomas do_disconnect = 1;
886 62ee7d94 2023-01-10 thomas err = gotd_imsg_recv_error(&client_id, &imsg);
887 62ee7d94 2023-01-10 thomas break;
888 62ee7d94 2023-01-10 thomas case GOTD_IMSG_PACKFILE_DONE:
889 62ee7d94 2023-01-10 thomas do_disconnect = 1;
890 b92adf10 2024-03-30 thomas err = recv_packfile_done(&imsg);
891 62ee7d94 2023-01-10 thomas break;
892 62ee7d94 2023-01-10 thomas case GOTD_IMSG_PACKFILE_INSTALL:
893 b92adf10 2024-03-30 thomas err = recv_packfile_install(&imsg);
894 62ee7d94 2023-01-10 thomas if (err == NULL)
895 62ee7d94 2023-01-10 thomas do_packfile_install = 1;
896 62ee7d94 2023-01-10 thomas break;
897 62ee7d94 2023-01-10 thomas case GOTD_IMSG_REF_UPDATES_START:
898 b92adf10 2024-03-30 thomas err = recv_ref_updates_start(&imsg);
899 62ee7d94 2023-01-10 thomas if (err == NULL)
900 62ee7d94 2023-01-10 thomas do_ref_updates = 1;
901 62ee7d94 2023-01-10 thomas break;
902 62ee7d94 2023-01-10 thomas case GOTD_IMSG_REF_UPDATE:
903 b92adf10 2024-03-30 thomas err = recv_ref_update(&imsg);
904 62ee7d94 2023-01-10 thomas if (err == NULL)
905 62ee7d94 2023-01-10 thomas do_ref_update = 1;
906 62ee7d94 2023-01-10 thomas break;
907 ce1bfad9 2024-03-30 thomas case GOTD_IMSG_NOTIFY:
908 b92adf10 2024-03-30 thomas err = recv_notification_content(&imsg);
909 ce1bfad9 2024-03-30 thomas if (err == NULL)
910 ce1bfad9 2024-03-30 thomas do_notify = 1;
911 ce1bfad9 2024-03-30 thomas break;
912 62ee7d94 2023-01-10 thomas default:
913 62ee7d94 2023-01-10 thomas log_debug("unexpected imsg %d", imsg.hdr.type);
914 62ee7d94 2023-01-10 thomas break;
915 62ee7d94 2023-01-10 thomas }
916 62ee7d94 2023-01-10 thomas
917 62ee7d94 2023-01-10 thomas if (do_disconnect) {
918 62ee7d94 2023-01-10 thomas if (err)
919 62ee7d94 2023-01-10 thomas disconnect_on_error(client, err);
920 62ee7d94 2023-01-10 thomas else
921 62ee7d94 2023-01-10 thomas disconnect(client);
922 62ee7d94 2023-01-10 thomas } else {
923 ce1bfad9 2024-03-30 thomas struct gotd_session_notif *notif;
924 ce1bfad9 2024-03-30 thomas
925 62ee7d94 2023-01-10 thomas if (do_packfile_install)
926 62ee7d94 2023-01-10 thomas err = install_pack(client,
927 62ee7d94 2023-01-10 thomas gotd_session.repo->path, &imsg);
928 62ee7d94 2023-01-10 thomas else if (do_ref_updates)
929 62ee7d94 2023-01-10 thomas err = begin_ref_updates(client, &imsg);
930 62ee7d94 2023-01-10 thomas else if (do_ref_update)
931 d98779cd 2023-01-19 thomas err = update_ref(&shut, client,
932 62ee7d94 2023-01-10 thomas gotd_session.repo->path, &imsg);
933 ce1bfad9 2024-03-30 thomas else if (do_notify)
934 ce1bfad9 2024-03-30 thomas err = forward_notification(client, &imsg);
935 62ee7d94 2023-01-10 thomas if (err)
936 62ee7d94 2023-01-10 thomas log_warnx("uid %d: %s", client->euid, err->msg);
937 ce1bfad9 2024-03-30 thomas
938 ce1bfad9 2024-03-30 thomas notif = STAILQ_FIRST(&notifications);
939 ce1bfad9 2024-03-30 thomas if (notif && do_notify) {
940 ce1bfad9 2024-03-30 thomas /* Request content for next notification. */
941 ce1bfad9 2024-03-30 thomas err = request_notification(notif);
942 ce1bfad9 2024-03-30 thomas if (err) {
943 ce1bfad9 2024-03-30 thomas log_warn("could not send notification: "
944 ce1bfad9 2024-03-30 thomas "%s", err->msg);
945 ce1bfad9 2024-03-30 thomas shut = 1;
946 ce1bfad9 2024-03-30 thomas }
947 ce1bfad9 2024-03-30 thomas }
948 62ee7d94 2023-01-10 thomas }
949 62ee7d94 2023-01-10 thomas imsg_free(&imsg);
950 62ee7d94 2023-01-10 thomas }
951 62ee7d94 2023-01-10 thomas done:
952 62ee7d94 2023-01-10 thomas if (!shut) {
953 62ee7d94 2023-01-10 thomas gotd_imsg_event_add(iev);
954 62ee7d94 2023-01-10 thomas } else {
955 62ee7d94 2023-01-10 thomas /* This pipe is dead. Remove its event handler */
956 62ee7d94 2023-01-10 thomas event_del(&iev->ev);
957 62ee7d94 2023-01-10 thomas event_loopexit(NULL);
958 62ee7d94 2023-01-10 thomas }
959 62ee7d94 2023-01-10 thomas }
960 62ee7d94 2023-01-10 thomas
961 62ee7d94 2023-01-10 thomas static const struct got_error *
962 62ee7d94 2023-01-10 thomas recv_capabilities(struct gotd_session_client *client, struct imsg *imsg)
963 62ee7d94 2023-01-10 thomas {
964 62ee7d94 2023-01-10 thomas struct gotd_imsg_capabilities icapas;
965 62ee7d94 2023-01-10 thomas size_t datalen;
966 62ee7d94 2023-01-10 thomas
967 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
968 62ee7d94 2023-01-10 thomas if (datalen != sizeof(icapas))
969 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
970 62ee7d94 2023-01-10 thomas memcpy(&icapas, imsg->data, sizeof(icapas));
971 62ee7d94 2023-01-10 thomas
972 62ee7d94 2023-01-10 thomas client->ncapa_alloc = icapas.ncapabilities;
973 62ee7d94 2023-01-10 thomas client->capabilities = calloc(client->ncapa_alloc,
974 62ee7d94 2023-01-10 thomas sizeof(*client->capabilities));
975 62ee7d94 2023-01-10 thomas if (client->capabilities == NULL) {
976 62ee7d94 2023-01-10 thomas client->ncapa_alloc = 0;
977 62ee7d94 2023-01-10 thomas return got_error_from_errno("calloc");
978 62ee7d94 2023-01-10 thomas }
979 62ee7d94 2023-01-10 thomas
980 62ee7d94 2023-01-10 thomas log_debug("expecting %zu capabilities from uid %d",
981 62ee7d94 2023-01-10 thomas client->ncapa_alloc, client->euid);
982 62ee7d94 2023-01-10 thomas return NULL;
983 62ee7d94 2023-01-10 thomas }
984 62ee7d94 2023-01-10 thomas
985 62ee7d94 2023-01-10 thomas static const struct got_error *
986 62ee7d94 2023-01-10 thomas recv_capability(struct gotd_session_client *client, struct imsg *imsg)
987 62ee7d94 2023-01-10 thomas {
988 62ee7d94 2023-01-10 thomas struct gotd_imsg_capability icapa;
989 62ee7d94 2023-01-10 thomas struct gotd_client_capability *capa;
990 62ee7d94 2023-01-10 thomas size_t datalen;
991 62ee7d94 2023-01-10 thomas char *key, *value = NULL;
992 62ee7d94 2023-01-10 thomas
993 62ee7d94 2023-01-10 thomas if (client->capabilities == NULL ||
994 62ee7d94 2023-01-10 thomas client->ncapabilities >= client->ncapa_alloc) {
995 62ee7d94 2023-01-10 thomas return got_error_msg(GOT_ERR_BAD_REQUEST,
996 62ee7d94 2023-01-10 thomas "unexpected capability received");
997 62ee7d94 2023-01-10 thomas }
998 62ee7d94 2023-01-10 thomas
999 62ee7d94 2023-01-10 thomas memset(&icapa, 0, sizeof(icapa));
1000 62ee7d94 2023-01-10 thomas
1001 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1002 62ee7d94 2023-01-10 thomas if (datalen < sizeof(icapa))
1003 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1004 62ee7d94 2023-01-10 thomas memcpy(&icapa, imsg->data, sizeof(icapa));
1005 62ee7d94 2023-01-10 thomas
1006 62ee7d94 2023-01-10 thomas if (datalen != sizeof(icapa) + icapa.key_len + icapa.value_len)
1007 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1008 62ee7d94 2023-01-10 thomas
1009 fcbb06bf 2023-01-14 thomas key = strndup(imsg->data + sizeof(icapa), icapa.key_len);
1010 62ee7d94 2023-01-10 thomas if (key == NULL)
1011 fcbb06bf 2023-01-14 thomas return got_error_from_errno("strndup");
1012 62ee7d94 2023-01-10 thomas if (icapa.value_len > 0) {
1013 fcbb06bf 2023-01-14 thomas value = strndup(imsg->data + sizeof(icapa) + icapa.key_len,
1014 fcbb06bf 2023-01-14 thomas icapa.value_len);
1015 62ee7d94 2023-01-10 thomas if (value == NULL) {
1016 62ee7d94 2023-01-10 thomas free(key);
1017 fcbb06bf 2023-01-14 thomas return got_error_from_errno("strndup");
1018 62ee7d94 2023-01-10 thomas }
1019 62ee7d94 2023-01-10 thomas }
1020 62ee7d94 2023-01-10 thomas
1021 62ee7d94 2023-01-10 thomas capa = &client->capabilities[client->ncapabilities++];
1022 62ee7d94 2023-01-10 thomas capa->key = key;
1023 62ee7d94 2023-01-10 thomas capa->value = value;
1024 62ee7d94 2023-01-10 thomas
1025 62ee7d94 2023-01-10 thomas if (value)
1026 62ee7d94 2023-01-10 thomas log_debug("uid %d: capability %s=%s", client->euid, key, value);
1027 62ee7d94 2023-01-10 thomas else
1028 62ee7d94 2023-01-10 thomas log_debug("uid %d: capability %s", client->euid, key);
1029 62ee7d94 2023-01-10 thomas
1030 62ee7d94 2023-01-10 thomas return NULL;
1031 62ee7d94 2023-01-10 thomas }
1032 62ee7d94 2023-01-10 thomas
1033 62ee7d94 2023-01-10 thomas static const struct got_error *
1034 62ee7d94 2023-01-10 thomas ensure_client_is_reading(struct gotd_session_client *client)
1035 62ee7d94 2023-01-10 thomas {
1036 62ee7d94 2023-01-10 thomas if (client->is_writing) {
1037 62ee7d94 2023-01-10 thomas return got_error_fmt(GOT_ERR_BAD_PACKET,
1038 62ee7d94 2023-01-10 thomas "uid %d made a read-request but is not reading from "
1039 62ee7d94 2023-01-10 thomas "a repository", client->euid);
1040 62ee7d94 2023-01-10 thomas }
1041 62ee7d94 2023-01-10 thomas
1042 62ee7d94 2023-01-10 thomas return NULL;
1043 62ee7d94 2023-01-10 thomas }
1044 62ee7d94 2023-01-10 thomas
1045 62ee7d94 2023-01-10 thomas static const struct got_error *
1046 62ee7d94 2023-01-10 thomas ensure_client_is_writing(struct gotd_session_client *client)
1047 62ee7d94 2023-01-10 thomas {
1048 62ee7d94 2023-01-10 thomas if (!client->is_writing) {
1049 62ee7d94 2023-01-10 thomas return got_error_fmt(GOT_ERR_BAD_PACKET,
1050 62ee7d94 2023-01-10 thomas "uid %d made a write-request but is not writing to "
1051 62ee7d94 2023-01-10 thomas "a repository", client->euid);
1052 62ee7d94 2023-01-10 thomas }
1053 62ee7d94 2023-01-10 thomas
1054 62ee7d94 2023-01-10 thomas return NULL;
1055 62ee7d94 2023-01-10 thomas }
1056 62ee7d94 2023-01-10 thomas
1057 62ee7d94 2023-01-10 thomas static const struct got_error *
1058 62ee7d94 2023-01-10 thomas forward_want(struct gotd_session_client *client, struct imsg *imsg)
1059 62ee7d94 2023-01-10 thomas {
1060 62ee7d94 2023-01-10 thomas struct gotd_imsg_want ireq;
1061 62ee7d94 2023-01-10 thomas struct gotd_imsg_want iwant;
1062 62ee7d94 2023-01-10 thomas size_t datalen;
1063 62ee7d94 2023-01-10 thomas
1064 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1065 62ee7d94 2023-01-10 thomas if (datalen != sizeof(ireq))
1066 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1067 62ee7d94 2023-01-10 thomas
1068 62ee7d94 2023-01-10 thomas memcpy(&ireq, imsg->data, datalen);
1069 62ee7d94 2023-01-10 thomas
1070 62ee7d94 2023-01-10 thomas memset(&iwant, 0, sizeof(iwant));
1071 62ee7d94 2023-01-10 thomas memcpy(iwant.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
1072 62ee7d94 2023-01-10 thomas
1073 74e85132 2024-03-30 thomas if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1074 74e85132 2024-03-30 thomas GOTD_IMSG_WANT, gotd_session.proc_id, -1,
1075 74e85132 2024-03-30 thomas &iwant, sizeof(iwant)) == -1)
1076 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg compose WANT");
1077 62ee7d94 2023-01-10 thomas
1078 62ee7d94 2023-01-10 thomas return NULL;
1079 62ee7d94 2023-01-10 thomas }
1080 62ee7d94 2023-01-10 thomas
1081 62ee7d94 2023-01-10 thomas static const struct got_error *
1082 62ee7d94 2023-01-10 thomas forward_ref_update(struct gotd_session_client *client, struct imsg *imsg)
1083 62ee7d94 2023-01-10 thomas {
1084 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
1085 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_update ireq;
1086 62ee7d94 2023-01-10 thomas struct gotd_imsg_ref_update *iref = NULL;
1087 62ee7d94 2023-01-10 thomas size_t datalen;
1088 62ee7d94 2023-01-10 thomas
1089 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1090 62ee7d94 2023-01-10 thomas if (datalen < sizeof(ireq))
1091 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1092 62ee7d94 2023-01-10 thomas memcpy(&ireq, imsg->data, sizeof(ireq));
1093 62ee7d94 2023-01-10 thomas if (datalen != sizeof(ireq) + ireq.name_len)
1094 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1095 62ee7d94 2023-01-10 thomas
1096 62ee7d94 2023-01-10 thomas iref = malloc(datalen);
1097 62ee7d94 2023-01-10 thomas if (iref == NULL)
1098 62ee7d94 2023-01-10 thomas return got_error_from_errno("malloc");
1099 62ee7d94 2023-01-10 thomas memcpy(iref, imsg->data, datalen);
1100 62ee7d94 2023-01-10 thomas
1101 74e85132 2024-03-30 thomas if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1102 7fed8fa4 2023-06-22 thomas GOTD_IMSG_REF_UPDATE, gotd_session.proc_id, -1,
1103 7fed8fa4 2023-06-22 thomas iref, datalen) == -1)
1104 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose REF_UPDATE");
1105 62ee7d94 2023-01-10 thomas free(iref);
1106 62ee7d94 2023-01-10 thomas return err;
1107 62ee7d94 2023-01-10 thomas }
1108 62ee7d94 2023-01-10 thomas
1109 62ee7d94 2023-01-10 thomas static const struct got_error *
1110 62ee7d94 2023-01-10 thomas forward_have(struct gotd_session_client *client, struct imsg *imsg)
1111 62ee7d94 2023-01-10 thomas {
1112 62ee7d94 2023-01-10 thomas struct gotd_imsg_have ireq;
1113 62ee7d94 2023-01-10 thomas struct gotd_imsg_have ihave;
1114 62ee7d94 2023-01-10 thomas size_t datalen;
1115 62ee7d94 2023-01-10 thomas
1116 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1117 62ee7d94 2023-01-10 thomas if (datalen != sizeof(ireq))
1118 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1119 62ee7d94 2023-01-10 thomas
1120 62ee7d94 2023-01-10 thomas memcpy(&ireq, imsg->data, datalen);
1121 62ee7d94 2023-01-10 thomas
1122 62ee7d94 2023-01-10 thomas memset(&ihave, 0, sizeof(ihave));
1123 62ee7d94 2023-01-10 thomas memcpy(ihave.object_id, ireq.object_id, SHA1_DIGEST_LENGTH);
1124 62ee7d94 2023-01-10 thomas
1125 74e85132 2024-03-30 thomas if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1126 74e85132 2024-03-30 thomas GOTD_IMSG_HAVE, gotd_session.proc_id, -1,
1127 74e85132 2024-03-30 thomas &ihave, sizeof(ihave)) == -1)
1128 62ee7d94 2023-01-10 thomas return got_error_from_errno("imsg compose HAVE");
1129 62ee7d94 2023-01-10 thomas
1130 62ee7d94 2023-01-10 thomas return NULL;
1131 62ee7d94 2023-01-10 thomas }
1132 62ee7d94 2023-01-10 thomas
1133 62ee7d94 2023-01-10 thomas static int
1134 62ee7d94 2023-01-10 thomas client_has_capability(struct gotd_session_client *client, const char *capastr)
1135 62ee7d94 2023-01-10 thomas {
1136 62ee7d94 2023-01-10 thomas struct gotd_client_capability *capa;
1137 62ee7d94 2023-01-10 thomas size_t i;
1138 62ee7d94 2023-01-10 thomas
1139 62ee7d94 2023-01-10 thomas if (client->ncapabilities == 0)
1140 62ee7d94 2023-01-10 thomas return 0;
1141 62ee7d94 2023-01-10 thomas
1142 62ee7d94 2023-01-10 thomas for (i = 0; i < client->ncapabilities; i++) {
1143 62ee7d94 2023-01-10 thomas capa = &client->capabilities[i];
1144 62ee7d94 2023-01-10 thomas if (strcmp(capa->key, capastr) == 0)
1145 62ee7d94 2023-01-10 thomas return 1;
1146 62ee7d94 2023-01-10 thomas }
1147 62ee7d94 2023-01-10 thomas
1148 62ee7d94 2023-01-10 thomas return 0;
1149 62ee7d94 2023-01-10 thomas }
1150 62ee7d94 2023-01-10 thomas
1151 62ee7d94 2023-01-10 thomas static const struct got_error *
1152 62ee7d94 2023-01-10 thomas recv_packfile(struct gotd_session_client *client)
1153 62ee7d94 2023-01-10 thomas {
1154 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
1155 62ee7d94 2023-01-10 thomas struct gotd_imsg_recv_packfile ipack;
1156 62ee7d94 2023-01-10 thomas char *basepath = NULL, *pack_path = NULL, *idx_path = NULL;
1157 62ee7d94 2023-01-10 thomas int packfd = -1, idxfd = -1;
1158 62ee7d94 2023-01-10 thomas int pipe[2] = { -1, -1 };
1159 62ee7d94 2023-01-10 thomas
1160 62ee7d94 2023-01-10 thomas if (client->packfile_path) {
1161 62ee7d94 2023-01-10 thomas return got_error_fmt(GOT_ERR_PRIVSEP_MSG,
1162 62ee7d94 2023-01-10 thomas "uid %d already has a pack file", client->euid);
1163 62ee7d94 2023-01-10 thomas }
1164 62ee7d94 2023-01-10 thomas
1165 62ee7d94 2023-01-10 thomas if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
1166 62ee7d94 2023-01-10 thomas return got_error_from_errno("socketpair");
1167 62ee7d94 2023-01-10 thomas
1168 62ee7d94 2023-01-10 thomas /* Send pack pipe end 0 to repo child process. */
1169 74e85132 2024-03-30 thomas if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1170 7fed8fa4 2023-06-22 thomas GOTD_IMSG_PACKFILE_PIPE, gotd_session.proc_id, pipe[0],
1171 b92adf10 2024-03-30 thomas NULL, 0) == -1) {
1172 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose PACKFILE_PIPE");
1173 62ee7d94 2023-01-10 thomas pipe[0] = -1;
1174 62ee7d94 2023-01-10 thomas goto done;
1175 62ee7d94 2023-01-10 thomas }
1176 62ee7d94 2023-01-10 thomas pipe[0] = -1;
1177 62ee7d94 2023-01-10 thomas
1178 62ee7d94 2023-01-10 thomas /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
1179 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&client->iev,
1180 7fed8fa4 2023-06-22 thomas GOTD_IMSG_PACKFILE_PIPE, gotd_session.proc_id, pipe[1],
1181 7fed8fa4 2023-06-22 thomas NULL, 0) == -1)
1182 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose PACKFILE_PIPE");
1183 62ee7d94 2023-01-10 thomas pipe[1] = -1;
1184 62ee7d94 2023-01-10 thomas
1185 62ee7d94 2023-01-10 thomas if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.pack",
1186 62ee7d94 2023-01-10 thomas got_repo_get_path(gotd_session.repo), GOT_OBJECTS_PACK_DIR,
1187 62ee7d94 2023-01-10 thomas client->euid) == -1) {
1188 62ee7d94 2023-01-10 thomas err = got_error_from_errno("asprintf");
1189 62ee7d94 2023-01-10 thomas goto done;
1190 62ee7d94 2023-01-10 thomas }
1191 62ee7d94 2023-01-10 thomas
1192 62ee7d94 2023-01-10 thomas err = got_opentemp_named_fd(&pack_path, &packfd, basepath, "");
1193 62ee7d94 2023-01-10 thomas if (err)
1194 62ee7d94 2023-01-10 thomas goto done;
1195 851a5b48 2023-02-03 thomas if (fchmod(packfd, GOT_DEFAULT_PACK_MODE) == -1) {
1196 851a5b48 2023-02-03 thomas err = got_error_from_errno2("fchmod", pack_path);
1197 851a5b48 2023-02-03 thomas goto done;
1198 851a5b48 2023-02-03 thomas }
1199 62ee7d94 2023-01-10 thomas
1200 62ee7d94 2023-01-10 thomas free(basepath);
1201 62ee7d94 2023-01-10 thomas if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.idx",
1202 62ee7d94 2023-01-10 thomas got_repo_get_path(gotd_session.repo), GOT_OBJECTS_PACK_DIR,
1203 62ee7d94 2023-01-10 thomas client->euid) == -1) {
1204 62ee7d94 2023-01-10 thomas err = got_error_from_errno("asprintf");
1205 62ee7d94 2023-01-10 thomas basepath = NULL;
1206 62ee7d94 2023-01-10 thomas goto done;
1207 62ee7d94 2023-01-10 thomas }
1208 62ee7d94 2023-01-10 thomas err = got_opentemp_named_fd(&idx_path, &idxfd, basepath, "");
1209 62ee7d94 2023-01-10 thomas if (err)
1210 62ee7d94 2023-01-10 thomas goto done;
1211 851a5b48 2023-02-03 thomas if (fchmod(idxfd, GOT_DEFAULT_PACK_MODE) == -1) {
1212 851a5b48 2023-02-03 thomas err = got_error_from_errno2("fchmod", idx_path);
1213 851a5b48 2023-02-03 thomas goto done;
1214 851a5b48 2023-02-03 thomas }
1215 62ee7d94 2023-01-10 thomas
1216 74e85132 2024-03-30 thomas if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1217 7fed8fa4 2023-06-22 thomas GOTD_IMSG_PACKIDX_FILE, gotd_session.proc_id,
1218 b92adf10 2024-03-30 thomas idxfd, NULL, 0) == -1) {
1219 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose PACKIDX_FILE");
1220 62ee7d94 2023-01-10 thomas idxfd = -1;
1221 62ee7d94 2023-01-10 thomas goto done;
1222 62ee7d94 2023-01-10 thomas }
1223 62ee7d94 2023-01-10 thomas idxfd = -1;
1224 62ee7d94 2023-01-10 thomas
1225 62ee7d94 2023-01-10 thomas memset(&ipack, 0, sizeof(ipack));
1226 62ee7d94 2023-01-10 thomas if (client_has_capability(client, GOT_CAPA_REPORT_STATUS))
1227 62ee7d94 2023-01-10 thomas ipack.report_status = 1;
1228 62ee7d94 2023-01-10 thomas
1229 74e85132 2024-03-30 thomas if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1230 7fed8fa4 2023-06-22 thomas GOTD_IMSG_RECV_PACKFILE, gotd_session.proc_id, packfd,
1231 62ee7d94 2023-01-10 thomas &ipack, sizeof(ipack)) == -1) {
1232 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose RECV_PACKFILE");
1233 62ee7d94 2023-01-10 thomas packfd = -1;
1234 62ee7d94 2023-01-10 thomas goto done;
1235 62ee7d94 2023-01-10 thomas }
1236 62ee7d94 2023-01-10 thomas packfd = -1;
1237 62ee7d94 2023-01-10 thomas
1238 62ee7d94 2023-01-10 thomas done:
1239 62ee7d94 2023-01-10 thomas free(basepath);
1240 62ee7d94 2023-01-10 thomas if (pipe[0] != -1 && close(pipe[0]) == -1 && err == NULL)
1241 62ee7d94 2023-01-10 thomas err = got_error_from_errno("close");
1242 62ee7d94 2023-01-10 thomas if (pipe[1] != -1 && close(pipe[1]) == -1 && err == NULL)
1243 62ee7d94 2023-01-10 thomas err = got_error_from_errno("close");
1244 62ee7d94 2023-01-10 thomas if (packfd != -1 && close(packfd) == -1 && err == NULL)
1245 62ee7d94 2023-01-10 thomas err = got_error_from_errno("close");
1246 62ee7d94 2023-01-10 thomas if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
1247 62ee7d94 2023-01-10 thomas err = got_error_from_errno("close");
1248 62ee7d94 2023-01-10 thomas if (err) {
1249 62ee7d94 2023-01-10 thomas free(pack_path);
1250 62ee7d94 2023-01-10 thomas free(idx_path);
1251 62ee7d94 2023-01-10 thomas } else {
1252 62ee7d94 2023-01-10 thomas client->packfile_path = pack_path;
1253 62ee7d94 2023-01-10 thomas client->packidx_path = idx_path;
1254 62ee7d94 2023-01-10 thomas }
1255 62ee7d94 2023-01-10 thomas return err;
1256 62ee7d94 2023-01-10 thomas }
1257 62ee7d94 2023-01-10 thomas
1258 62ee7d94 2023-01-10 thomas static const struct got_error *
1259 62ee7d94 2023-01-10 thomas send_packfile(struct gotd_session_client *client)
1260 62ee7d94 2023-01-10 thomas {
1261 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
1262 62ee7d94 2023-01-10 thomas struct gotd_imsg_send_packfile ipack;
1263 62ee7d94 2023-01-10 thomas int pipe[2];
1264 62ee7d94 2023-01-10 thomas
1265 62ee7d94 2023-01-10 thomas if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
1266 62ee7d94 2023-01-10 thomas return got_error_from_errno("socketpair");
1267 62ee7d94 2023-01-10 thomas
1268 62ee7d94 2023-01-10 thomas memset(&ipack, 0, sizeof(ipack));
1269 62ee7d94 2023-01-10 thomas
1270 62ee7d94 2023-01-10 thomas if (client_has_capability(client, GOT_CAPA_SIDE_BAND_64K))
1271 62ee7d94 2023-01-10 thomas ipack.report_progress = 1;
1272 62ee7d94 2023-01-10 thomas
1273 62ee7d94 2023-01-10 thomas client->delta_cache_fd = got_opentempfd();
1274 62ee7d94 2023-01-10 thomas if (client->delta_cache_fd == -1)
1275 62ee7d94 2023-01-10 thomas return got_error_from_errno("got_opentempfd");
1276 62ee7d94 2023-01-10 thomas
1277 74e85132 2024-03-30 thomas if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1278 62ee7d94 2023-01-10 thomas GOTD_IMSG_SEND_PACKFILE, PROC_GOTD, client->delta_cache_fd,
1279 62ee7d94 2023-01-10 thomas &ipack, sizeof(ipack)) == -1) {
1280 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose SEND_PACKFILE");
1281 62ee7d94 2023-01-10 thomas close(pipe[0]);
1282 62ee7d94 2023-01-10 thomas close(pipe[1]);
1283 62ee7d94 2023-01-10 thomas return err;
1284 62ee7d94 2023-01-10 thomas }
1285 62ee7d94 2023-01-10 thomas
1286 62ee7d94 2023-01-10 thomas /* Send pack pipe end 0 to repo child process. */
1287 74e85132 2024-03-30 thomas if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1288 b92adf10 2024-03-30 thomas GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[0], NULL, 0) == -1) {
1289 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose PACKFILE_PIPE");
1290 62ee7d94 2023-01-10 thomas close(pipe[1]);
1291 62ee7d94 2023-01-10 thomas return err;
1292 62ee7d94 2023-01-10 thomas }
1293 62ee7d94 2023-01-10 thomas
1294 62ee7d94 2023-01-10 thomas /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
1295 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&client->iev,
1296 62ee7d94 2023-01-10 thomas GOTD_IMSG_PACKFILE_PIPE, PROC_GOTD, pipe[1], NULL, 0) == -1)
1297 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose PACKFILE_PIPE");
1298 62ee7d94 2023-01-10 thomas
1299 62ee7d94 2023-01-10 thomas return err;
1300 62ee7d94 2023-01-10 thomas }
1301 62ee7d94 2023-01-10 thomas
1302 62ee7d94 2023-01-10 thomas static void
1303 8cb46987 2023-02-07 thomas session_dispatch_client(int fd, short events, void *arg)
1304 62ee7d94 2023-01-10 thomas {
1305 62ee7d94 2023-01-10 thomas struct gotd_imsgev *iev = arg;
1306 62ee7d94 2023-01-10 thomas struct imsgbuf *ibuf = &iev->ibuf;
1307 62ee7d94 2023-01-10 thomas struct gotd_session_client *client = &gotd_session_client;
1308 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
1309 62ee7d94 2023-01-10 thomas struct imsg imsg;
1310 62ee7d94 2023-01-10 thomas ssize_t n;
1311 62ee7d94 2023-01-10 thomas
1312 62ee7d94 2023-01-10 thomas if (events & EV_WRITE) {
1313 62ee7d94 2023-01-10 thomas while (ibuf->w.queued) {
1314 62ee7d94 2023-01-10 thomas n = msgbuf_write(&ibuf->w);
1315 62ee7d94 2023-01-10 thomas if (n == -1 && errno == EPIPE) {
1316 62ee7d94 2023-01-10 thomas /*
1317 62ee7d94 2023-01-10 thomas * The client has closed its socket.
1318 62ee7d94 2023-01-10 thomas * This can happen when Git clients are
1319 62ee7d94 2023-01-10 thomas * done sending pack file data.
1320 62ee7d94 2023-01-10 thomas */
1321 62ee7d94 2023-01-10 thomas msgbuf_clear(&ibuf->w);
1322 62ee7d94 2023-01-10 thomas continue;
1323 62ee7d94 2023-01-10 thomas } else if (n == -1 && errno != EAGAIN) {
1324 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg_flush");
1325 62ee7d94 2023-01-10 thomas disconnect_on_error(client, err);
1326 62ee7d94 2023-01-10 thomas return;
1327 62ee7d94 2023-01-10 thomas }
1328 62ee7d94 2023-01-10 thomas if (n == 0) {
1329 62ee7d94 2023-01-10 thomas /* Connection closed. */
1330 62ee7d94 2023-01-10 thomas err = got_error(GOT_ERR_EOF);
1331 62ee7d94 2023-01-10 thomas disconnect_on_error(client, err);
1332 62ee7d94 2023-01-10 thomas return;
1333 62ee7d94 2023-01-10 thomas }
1334 c8b73ac1 2023-08-23 thomas }
1335 c8b73ac1 2023-08-23 thomas
1336 c8b73ac1 2023-08-23 thomas if (client->flush_disconnect) {
1337 c8b73ac1 2023-08-23 thomas disconnect(client);
1338 c8b73ac1 2023-08-23 thomas return;
1339 62ee7d94 2023-01-10 thomas }
1340 62ee7d94 2023-01-10 thomas }
1341 62ee7d94 2023-01-10 thomas
1342 62ee7d94 2023-01-10 thomas if ((events & EV_READ) == 0)
1343 62ee7d94 2023-01-10 thomas return;
1344 62ee7d94 2023-01-10 thomas
1345 62ee7d94 2023-01-10 thomas memset(&imsg, 0, sizeof(imsg));
1346 62ee7d94 2023-01-10 thomas
1347 62ee7d94 2023-01-10 thomas while (err == NULL) {
1348 62ee7d94 2023-01-10 thomas err = gotd_imsg_recv(&imsg, ibuf, 0);
1349 62ee7d94 2023-01-10 thomas if (err) {
1350 62ee7d94 2023-01-10 thomas if (err->code == GOT_ERR_PRIVSEP_READ)
1351 62ee7d94 2023-01-10 thomas err = NULL;
1352 8e92c55c 2023-06-08 thomas else if (err->code == GOT_ERR_EOF &&
1353 176350bf 2024-03-30 thomas gotd_session.state ==
1354 176350bf 2024-03-30 thomas GOTD_STATE_EXPECT_CAPABILITIES) {
1355 8e92c55c 2023-06-08 thomas /*
1356 8e92c55c 2023-06-08 thomas * The client has closed its socket before
1357 8e92c55c 2023-06-08 thomas * sending its capability announcement.
1358 8e92c55c 2023-06-08 thomas * This can happen when Git clients have
1359 8e92c55c 2023-06-08 thomas * no ref-updates to send.
1360 8e92c55c 2023-06-08 thomas */
1361 8e92c55c 2023-06-08 thomas disconnect_on_error(client, err);
1362 8e92c55c 2023-06-08 thomas return;
1363 8e92c55c 2023-06-08 thomas }
1364 62ee7d94 2023-01-10 thomas break;
1365 62ee7d94 2023-01-10 thomas }
1366 62ee7d94 2023-01-10 thomas
1367 62ee7d94 2023-01-10 thomas evtimer_del(&client->tmo);
1368 62ee7d94 2023-01-10 thomas
1369 62ee7d94 2023-01-10 thomas switch (imsg.hdr.type) {
1370 62ee7d94 2023-01-10 thomas case GOTD_IMSG_CAPABILITIES:
1371 176350bf 2024-03-30 thomas if (gotd_session.state !=
1372 176350bf 2024-03-30 thomas GOTD_STATE_EXPECT_CAPABILITIES) {
1373 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1374 62ee7d94 2023-01-10 thomas "unexpected capabilities received");
1375 62ee7d94 2023-01-10 thomas break;
1376 62ee7d94 2023-01-10 thomas }
1377 62ee7d94 2023-01-10 thomas log_debug("receiving capabilities from uid %d",
1378 62ee7d94 2023-01-10 thomas client->euid);
1379 62ee7d94 2023-01-10 thomas err = recv_capabilities(client, &imsg);
1380 62ee7d94 2023-01-10 thomas break;
1381 62ee7d94 2023-01-10 thomas case GOTD_IMSG_CAPABILITY:
1382 176350bf 2024-03-30 thomas if (gotd_session.state != GOTD_STATE_EXPECT_CAPABILITIES) {
1383 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1384 62ee7d94 2023-01-10 thomas "unexpected capability received");
1385 62ee7d94 2023-01-10 thomas break;
1386 62ee7d94 2023-01-10 thomas }
1387 62ee7d94 2023-01-10 thomas err = recv_capability(client, &imsg);
1388 62ee7d94 2023-01-10 thomas if (err || client->ncapabilities < client->ncapa_alloc)
1389 62ee7d94 2023-01-10 thomas break;
1390 62ee7d94 2023-01-10 thomas if (!client->is_writing) {
1391 176350bf 2024-03-30 thomas gotd_session.state = GOTD_STATE_EXPECT_WANT;
1392 98c7fd82 2023-01-23 thomas client->accept_flush_pkt = 1;
1393 62ee7d94 2023-01-10 thomas log_debug("uid %d: expecting want-lines",
1394 62ee7d94 2023-01-10 thomas client->euid);
1395 62ee7d94 2023-01-10 thomas } else if (client->is_writing) {
1396 176350bf 2024-03-30 thomas gotd_session.state = GOTD_STATE_EXPECT_REF_UPDATE;
1397 98c7fd82 2023-01-23 thomas client->accept_flush_pkt = 1;
1398 62ee7d94 2023-01-10 thomas log_debug("uid %d: expecting ref-update-lines",
1399 62ee7d94 2023-01-10 thomas client->euid);
1400 62ee7d94 2023-01-10 thomas } else
1401 62ee7d94 2023-01-10 thomas fatalx("client %d is both reading and writing",
1402 62ee7d94 2023-01-10 thomas client->euid);
1403 62ee7d94 2023-01-10 thomas break;
1404 62ee7d94 2023-01-10 thomas case GOTD_IMSG_WANT:
1405 176350bf 2024-03-30 thomas if (gotd_session.state != GOTD_STATE_EXPECT_WANT) {
1406 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1407 62ee7d94 2023-01-10 thomas "unexpected want-line received");
1408 62ee7d94 2023-01-10 thomas break;
1409 62ee7d94 2023-01-10 thomas }
1410 62ee7d94 2023-01-10 thomas log_debug("received want-line from uid %d",
1411 62ee7d94 2023-01-10 thomas client->euid);
1412 62ee7d94 2023-01-10 thomas err = ensure_client_is_reading(client);
1413 62ee7d94 2023-01-10 thomas if (err)
1414 62ee7d94 2023-01-10 thomas break;
1415 98c7fd82 2023-01-23 thomas client->accept_flush_pkt = 1;
1416 62ee7d94 2023-01-10 thomas err = forward_want(client, &imsg);
1417 62ee7d94 2023-01-10 thomas break;
1418 62ee7d94 2023-01-10 thomas case GOTD_IMSG_REF_UPDATE:
1419 176350bf 2024-03-30 thomas if (gotd_session.state != GOTD_STATE_EXPECT_REF_UPDATE &&
1420 176350bf 2024-03-30 thomas gotd_session.state !=
1421 0445d8ec 2023-01-19 thomas GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1422 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1423 62ee7d94 2023-01-10 thomas "unexpected ref-update-line received");
1424 62ee7d94 2023-01-10 thomas break;
1425 62ee7d94 2023-01-10 thomas }
1426 62ee7d94 2023-01-10 thomas log_debug("received ref-update-line from uid %d",
1427 62ee7d94 2023-01-10 thomas client->euid);
1428 62ee7d94 2023-01-10 thomas err = ensure_client_is_writing(client);
1429 62ee7d94 2023-01-10 thomas if (err)
1430 62ee7d94 2023-01-10 thomas break;
1431 62ee7d94 2023-01-10 thomas err = forward_ref_update(client, &imsg);
1432 62ee7d94 2023-01-10 thomas if (err)
1433 62ee7d94 2023-01-10 thomas break;
1434 176350bf 2024-03-30 thomas gotd_session.state = GOTD_STATE_EXPECT_MORE_REF_UPDATES;
1435 98c7fd82 2023-01-23 thomas client->accept_flush_pkt = 1;
1436 62ee7d94 2023-01-10 thomas break;
1437 62ee7d94 2023-01-10 thomas case GOTD_IMSG_HAVE:
1438 176350bf 2024-03-30 thomas if (gotd_session.state != GOTD_STATE_EXPECT_HAVE) {
1439 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1440 62ee7d94 2023-01-10 thomas "unexpected have-line received");
1441 62ee7d94 2023-01-10 thomas break;
1442 62ee7d94 2023-01-10 thomas }
1443 62ee7d94 2023-01-10 thomas log_debug("received have-line from uid %d",
1444 62ee7d94 2023-01-10 thomas client->euid);
1445 62ee7d94 2023-01-10 thomas err = ensure_client_is_reading(client);
1446 62ee7d94 2023-01-10 thomas if (err)
1447 62ee7d94 2023-01-10 thomas break;
1448 62ee7d94 2023-01-10 thomas err = forward_have(client, &imsg);
1449 62ee7d94 2023-01-10 thomas if (err)
1450 62ee7d94 2023-01-10 thomas break;
1451 98c7fd82 2023-01-23 thomas client->accept_flush_pkt = 1;
1452 62ee7d94 2023-01-10 thomas break;
1453 62ee7d94 2023-01-10 thomas case GOTD_IMSG_FLUSH:
1454 176350bf 2024-03-30 thomas if (gotd_session.state == GOTD_STATE_EXPECT_WANT ||
1455 176350bf 2024-03-30 thomas gotd_session.state == GOTD_STATE_EXPECT_HAVE) {
1456 62ee7d94 2023-01-10 thomas err = ensure_client_is_reading(client);
1457 62ee7d94 2023-01-10 thomas if (err)
1458 62ee7d94 2023-01-10 thomas break;
1459 176350bf 2024-03-30 thomas } else if (gotd_session.state ==
1460 62ee7d94 2023-01-10 thomas GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1461 62ee7d94 2023-01-10 thomas err = ensure_client_is_writing(client);
1462 62ee7d94 2023-01-10 thomas if (err)
1463 62ee7d94 2023-01-10 thomas break;
1464 176350bf 2024-03-30 thomas } else if (gotd_session.state != GOTD_STATE_EXPECT_DONE) {
1465 98c7fd82 2023-01-23 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1466 98c7fd82 2023-01-23 thomas "unexpected flush-pkt received");
1467 98c7fd82 2023-01-23 thomas break;
1468 98c7fd82 2023-01-23 thomas }
1469 98c7fd82 2023-01-23 thomas if (!client->accept_flush_pkt) {
1470 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1471 62ee7d94 2023-01-10 thomas "unexpected flush-pkt received");
1472 62ee7d94 2023-01-10 thomas break;
1473 62ee7d94 2023-01-10 thomas }
1474 98c7fd82 2023-01-23 thomas
1475 98c7fd82 2023-01-23 thomas /*
1476 98c7fd82 2023-01-23 thomas * Accept just one flush packet at a time.
1477 98c7fd82 2023-01-23 thomas * Future client state transitions will set this flag
1478 98c7fd82 2023-01-23 thomas * again if another flush packet is expected.
1479 98c7fd82 2023-01-23 thomas */
1480 98c7fd82 2023-01-23 thomas client->accept_flush_pkt = 0;
1481 98c7fd82 2023-01-23 thomas
1482 62ee7d94 2023-01-10 thomas log_debug("received flush-pkt from uid %d",
1483 62ee7d94 2023-01-10 thomas client->euid);
1484 176350bf 2024-03-30 thomas if (gotd_session.state == GOTD_STATE_EXPECT_WANT) {
1485 176350bf 2024-03-30 thomas gotd_session.state = GOTD_STATE_EXPECT_HAVE;
1486 62ee7d94 2023-01-10 thomas log_debug("uid %d: expecting have-lines",
1487 62ee7d94 2023-01-10 thomas client->euid);
1488 176350bf 2024-03-30 thomas } else if (gotd_session.state == GOTD_STATE_EXPECT_HAVE) {
1489 176350bf 2024-03-30 thomas gotd_session.state = GOTD_STATE_EXPECT_DONE;
1490 98c7fd82 2023-01-23 thomas client->accept_flush_pkt = 1;
1491 62ee7d94 2023-01-10 thomas log_debug("uid %d: expecting 'done'",
1492 62ee7d94 2023-01-10 thomas client->euid);
1493 176350bf 2024-03-30 thomas } else if (gotd_session.state ==
1494 62ee7d94 2023-01-10 thomas GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1495 176350bf 2024-03-30 thomas gotd_session.state = GOTD_STATE_EXPECT_PACKFILE;
1496 62ee7d94 2023-01-10 thomas log_debug("uid %d: expecting packfile",
1497 62ee7d94 2023-01-10 thomas client->euid);
1498 62ee7d94 2023-01-10 thomas err = recv_packfile(client);
1499 176350bf 2024-03-30 thomas } else if (gotd_session.state != GOTD_STATE_EXPECT_DONE) {
1500 62ee7d94 2023-01-10 thomas /* should not happen, see above */
1501 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1502 62ee7d94 2023-01-10 thomas "unexpected client state");
1503 62ee7d94 2023-01-10 thomas break;
1504 62ee7d94 2023-01-10 thomas }
1505 62ee7d94 2023-01-10 thomas break;
1506 62ee7d94 2023-01-10 thomas case GOTD_IMSG_DONE:
1507 176350bf 2024-03-30 thomas if (gotd_session.state != GOTD_STATE_EXPECT_HAVE &&
1508 176350bf 2024-03-30 thomas gotd_session.state != GOTD_STATE_EXPECT_DONE) {
1509 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_BAD_REQUEST,
1510 62ee7d94 2023-01-10 thomas "unexpected flush-pkt received");
1511 62ee7d94 2023-01-10 thomas break;
1512 62ee7d94 2023-01-10 thomas }
1513 62ee7d94 2023-01-10 thomas log_debug("received 'done' from uid %d", client->euid);
1514 62ee7d94 2023-01-10 thomas err = ensure_client_is_reading(client);
1515 62ee7d94 2023-01-10 thomas if (err)
1516 62ee7d94 2023-01-10 thomas break;
1517 176350bf 2024-03-30 thomas gotd_session.state = GOTD_STATE_DONE;
1518 98c7fd82 2023-01-23 thomas client->accept_flush_pkt = 1;
1519 62ee7d94 2023-01-10 thomas err = send_packfile(client);
1520 62ee7d94 2023-01-10 thomas break;
1521 62ee7d94 2023-01-10 thomas default:
1522 62ee7d94 2023-01-10 thomas log_debug("unexpected imsg %d", imsg.hdr.type);
1523 62ee7d94 2023-01-10 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
1524 62ee7d94 2023-01-10 thomas break;
1525 62ee7d94 2023-01-10 thomas }
1526 62ee7d94 2023-01-10 thomas
1527 62ee7d94 2023-01-10 thomas imsg_free(&imsg);
1528 62ee7d94 2023-01-10 thomas }
1529 62ee7d94 2023-01-10 thomas
1530 62ee7d94 2023-01-10 thomas if (err) {
1531 62ee7d94 2023-01-10 thomas if (err->code != GOT_ERR_EOF ||
1532 176350bf 2024-03-30 thomas gotd_session.state != GOTD_STATE_EXPECT_PACKFILE)
1533 62ee7d94 2023-01-10 thomas disconnect_on_error(client, err);
1534 62ee7d94 2023-01-10 thomas } else {
1535 62ee7d94 2023-01-10 thomas gotd_imsg_event_add(iev);
1536 62ee7d94 2023-01-10 thomas evtimer_add(&client->tmo, &gotd_session.request_timeout);
1537 62ee7d94 2023-01-10 thomas }
1538 62ee7d94 2023-01-10 thomas }
1539 62ee7d94 2023-01-10 thomas
1540 62ee7d94 2023-01-10 thomas static const struct got_error *
1541 62ee7d94 2023-01-10 thomas list_refs_request(void)
1542 62ee7d94 2023-01-10 thomas {
1543 62ee7d94 2023-01-10 thomas static const struct got_error *err;
1544 62ee7d94 2023-01-10 thomas struct gotd_session_client *client = &gotd_session_client;
1545 74e85132 2024-03-30 thomas struct gotd_imsgev *iev = &gotd_session.repo_child_iev;
1546 62ee7d94 2023-01-10 thomas int fd;
1547 62ee7d94 2023-01-10 thomas
1548 176350bf 2024-03-30 thomas if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
1549 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
1550 62ee7d94 2023-01-10 thomas
1551 62ee7d94 2023-01-10 thomas fd = dup(client->fd);
1552 62ee7d94 2023-01-10 thomas if (fd == -1)
1553 62ee7d94 2023-01-10 thomas return got_error_from_errno("dup");
1554 62ee7d94 2023-01-10 thomas
1555 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(iev, GOTD_IMSG_LIST_REFS_INTERNAL,
1556 b92adf10 2024-03-30 thomas gotd_session.proc_id, fd, NULL, 0) == -1) {
1557 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose LIST_REFS_INTERNAL");
1558 62ee7d94 2023-01-10 thomas close(fd);
1559 62ee7d94 2023-01-10 thomas return err;
1560 62ee7d94 2023-01-10 thomas }
1561 62ee7d94 2023-01-10 thomas
1562 176350bf 2024-03-30 thomas gotd_session.state = GOTD_STATE_EXPECT_CAPABILITIES;
1563 62ee7d94 2023-01-10 thomas log_debug("uid %d: expecting capabilities", client->euid);
1564 62ee7d94 2023-01-10 thomas return NULL;
1565 62ee7d94 2023-01-10 thomas }
1566 62ee7d94 2023-01-10 thomas
1567 62ee7d94 2023-01-10 thomas static const struct got_error *
1568 62ee7d94 2023-01-10 thomas recv_connect(struct imsg *imsg)
1569 62ee7d94 2023-01-10 thomas {
1570 62ee7d94 2023-01-10 thomas struct gotd_session_client *client = &gotd_session_client;
1571 62ee7d94 2023-01-10 thomas struct gotd_imsg_connect iconnect;
1572 62ee7d94 2023-01-10 thomas size_t datalen;
1573 62ee7d94 2023-01-10 thomas
1574 176350bf 2024-03-30 thomas if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
1575 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
1576 62ee7d94 2023-01-10 thomas
1577 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1578 ce1bfad9 2024-03-30 thomas if (datalen < sizeof(iconnect))
1579 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1580 62ee7d94 2023-01-10 thomas memcpy(&iconnect, imsg->data, sizeof(iconnect));
1581 ce1bfad9 2024-03-30 thomas if (iconnect.username_len == 0 ||
1582 ce1bfad9 2024-03-30 thomas datalen != sizeof(iconnect) + iconnect.username_len)
1583 ce1bfad9 2024-03-30 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1584 62ee7d94 2023-01-10 thomas
1585 62ee7d94 2023-01-10 thomas client->euid = iconnect.euid;
1586 62ee7d94 2023-01-10 thomas client->egid = iconnect.egid;
1587 3d97effa 2024-01-31 thomas client->fd = imsg_get_fd(imsg);
1588 3d97effa 2024-01-31 thomas if (client->fd == -1)
1589 3d97effa 2024-01-31 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
1590 62ee7d94 2023-01-10 thomas
1591 ce1bfad9 2024-03-30 thomas client->username = strndup(imsg->data + sizeof(iconnect),
1592 ce1bfad9 2024-03-30 thomas iconnect.username_len);
1593 ce1bfad9 2024-03-30 thomas if (client->username == NULL)
1594 ce1bfad9 2024-03-30 thomas return got_error_from_errno("strndup");
1595 ce1bfad9 2024-03-30 thomas
1596 62ee7d94 2023-01-10 thomas imsg_init(&client->iev.ibuf, client->fd);
1597 8cb46987 2023-02-07 thomas client->iev.handler = session_dispatch_client;
1598 62ee7d94 2023-01-10 thomas client->iev.events = EV_READ;
1599 62ee7d94 2023-01-10 thomas client->iev.handler_arg = NULL;
1600 62ee7d94 2023-01-10 thomas event_set(&client->iev.ev, client->iev.ibuf.fd, EV_READ,
1601 8cb46987 2023-02-07 thomas session_dispatch_client, &client->iev);
1602 62ee7d94 2023-01-10 thomas gotd_imsg_event_add(&client->iev);
1603 62ee7d94 2023-01-10 thomas evtimer_set(&client->tmo, gotd_request_timeout, client);
1604 62ee7d94 2023-01-10 thomas
1605 62ee7d94 2023-01-10 thomas return NULL;
1606 62ee7d94 2023-01-10 thomas }
1607 62ee7d94 2023-01-10 thomas
1608 ce1bfad9 2024-03-30 thomas static void
1609 ce1bfad9 2024-03-30 thomas session_dispatch_notifier(int fd, short event, void *arg)
1610 ce1bfad9 2024-03-30 thomas {
1611 ce1bfad9 2024-03-30 thomas const struct got_error *err;
1612 ce1bfad9 2024-03-30 thomas struct gotd_session_client *client = &gotd_session_client;
1613 ce1bfad9 2024-03-30 thomas struct gotd_imsgev *iev = arg;
1614 ce1bfad9 2024-03-30 thomas struct imsgbuf *ibuf = &iev->ibuf;
1615 ce1bfad9 2024-03-30 thomas ssize_t n;
1616 ce1bfad9 2024-03-30 thomas int shut = 0;
1617 ce1bfad9 2024-03-30 thomas struct imsg imsg;
1618 ce1bfad9 2024-03-30 thomas struct gotd_session_notif *notif;
1619 ce1bfad9 2024-03-30 thomas
1620 ce1bfad9 2024-03-30 thomas if (event & EV_READ) {
1621 ce1bfad9 2024-03-30 thomas if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1622 ce1bfad9 2024-03-30 thomas fatal("imsg_read error");
1623 ce1bfad9 2024-03-30 thomas if (n == 0) {
1624 ce1bfad9 2024-03-30 thomas /* Connection closed. */
1625 ce1bfad9 2024-03-30 thomas shut = 1;
1626 ce1bfad9 2024-03-30 thomas goto done;
1627 ce1bfad9 2024-03-30 thomas }
1628 ce1bfad9 2024-03-30 thomas }
1629 ce1bfad9 2024-03-30 thomas
1630 ce1bfad9 2024-03-30 thomas if (event & EV_WRITE) {
1631 ce1bfad9 2024-03-30 thomas n = msgbuf_write(&ibuf->w);
1632 ce1bfad9 2024-03-30 thomas if (n == -1 && errno != EAGAIN)
1633 ce1bfad9 2024-03-30 thomas fatal("msgbuf_write");
1634 ce1bfad9 2024-03-30 thomas if (n == 0) {
1635 ce1bfad9 2024-03-30 thomas /* Connection closed. */
1636 ce1bfad9 2024-03-30 thomas shut = 1;
1637 ce1bfad9 2024-03-30 thomas goto done;
1638 ce1bfad9 2024-03-30 thomas }
1639 ce1bfad9 2024-03-30 thomas }
1640 ce1bfad9 2024-03-30 thomas
1641 ce1bfad9 2024-03-30 thomas for (;;) {
1642 ce1bfad9 2024-03-30 thomas if ((n = imsg_get(ibuf, &imsg)) == -1)
1643 ce1bfad9 2024-03-30 thomas fatal("%s: imsg_get error", __func__);
1644 ce1bfad9 2024-03-30 thomas if (n == 0) /* No more messages. */
1645 ce1bfad9 2024-03-30 thomas break;
1646 ce1bfad9 2024-03-30 thomas
1647 ce1bfad9 2024-03-30 thomas switch (imsg.hdr.type) {
1648 ce1bfad9 2024-03-30 thomas case GOTD_IMSG_NOTIFICATION_SENT:
1649 176350bf 2024-03-30 thomas if (gotd_session.state != GOTD_STATE_NOTIFY) {
1650 ce1bfad9 2024-03-30 thomas log_warn("unexpected imsg %d", imsg.hdr.type);
1651 ce1bfad9 2024-03-30 thomas break;
1652 ce1bfad9 2024-03-30 thomas }
1653 ce1bfad9 2024-03-30 thomas notif = STAILQ_FIRST(&notifications);
1654 ce1bfad9 2024-03-30 thomas if (notif == NULL) {
1655 ce1bfad9 2024-03-30 thomas disconnect(client);
1656 ce1bfad9 2024-03-30 thomas break; /* NOTREACHED */
1657 ce1bfad9 2024-03-30 thomas }
1658 ce1bfad9 2024-03-30 thomas /* Request content for the next notification. */
1659 ce1bfad9 2024-03-30 thomas err = request_notification(notif);
1660 ce1bfad9 2024-03-30 thomas if (err) {
1661 ce1bfad9 2024-03-30 thomas log_warn("could not send notification: %s",
1662 ce1bfad9 2024-03-30 thomas err->msg);
1663 ce1bfad9 2024-03-30 thomas disconnect(client);
1664 ce1bfad9 2024-03-30 thomas }
1665 ce1bfad9 2024-03-30 thomas break;
1666 ce1bfad9 2024-03-30 thomas default:
1667 ce1bfad9 2024-03-30 thomas log_debug("unexpected imsg %d", imsg.hdr.type);
1668 ce1bfad9 2024-03-30 thomas break;
1669 ce1bfad9 2024-03-30 thomas }
1670 ce1bfad9 2024-03-30 thomas
1671 ce1bfad9 2024-03-30 thomas imsg_free(&imsg);
1672 ce1bfad9 2024-03-30 thomas }
1673 ce1bfad9 2024-03-30 thomas done:
1674 ce1bfad9 2024-03-30 thomas if (!shut) {
1675 ce1bfad9 2024-03-30 thomas gotd_imsg_event_add(iev);
1676 ce1bfad9 2024-03-30 thomas } else {
1677 ce1bfad9 2024-03-30 thomas /* This pipe is dead. Remove its event handler */
1678 ce1bfad9 2024-03-30 thomas event_del(&iev->ev);
1679 ce1bfad9 2024-03-30 thomas imsg_clear(&iev->ibuf);
1680 ce1bfad9 2024-03-30 thomas imsg_init(&iev->ibuf, -1);
1681 ce1bfad9 2024-03-30 thomas }
1682 ce1bfad9 2024-03-30 thomas }
1683 ce1bfad9 2024-03-30 thomas
1684 62ee7d94 2023-01-10 thomas static const struct got_error *
1685 ce1bfad9 2024-03-30 thomas recv_notifier(struct imsg *imsg)
1686 ce1bfad9 2024-03-30 thomas {
1687 ce1bfad9 2024-03-30 thomas struct gotd_imsgev *iev = &gotd_session.notifier_iev;
1688 ce1bfad9 2024-03-30 thomas struct gotd_session_client *client = &gotd_session_client;
1689 ce1bfad9 2024-03-30 thomas size_t datalen;
1690 ce1bfad9 2024-03-30 thomas int fd;
1691 ce1bfad9 2024-03-30 thomas
1692 176350bf 2024-03-30 thomas if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
1693 ce1bfad9 2024-03-30 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
1694 ce1bfad9 2024-03-30 thomas
1695 ce1bfad9 2024-03-30 thomas /* We should already have received a pipe to the listener. */
1696 ce1bfad9 2024-03-30 thomas if (client->fd == -1)
1697 ce1bfad9 2024-03-30 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
1698 ce1bfad9 2024-03-30 thomas
1699 ce1bfad9 2024-03-30 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1700 ce1bfad9 2024-03-30 thomas if (datalen != 0)
1701 ce1bfad9 2024-03-30 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1702 ce1bfad9 2024-03-30 thomas
1703 ce1bfad9 2024-03-30 thomas fd = imsg_get_fd(imsg);
1704 ce1bfad9 2024-03-30 thomas if (fd == -1)
1705 ce1bfad9 2024-03-30 thomas return NULL; /* notifications unused */
1706 ce1bfad9 2024-03-30 thomas
1707 ce1bfad9 2024-03-30 thomas imsg_init(&iev->ibuf, fd);
1708 ce1bfad9 2024-03-30 thomas iev->handler = session_dispatch_notifier;
1709 ce1bfad9 2024-03-30 thomas iev->events = EV_READ;
1710 ce1bfad9 2024-03-30 thomas iev->handler_arg = NULL;
1711 ce1bfad9 2024-03-30 thomas event_set(&iev->ev, iev->ibuf.fd, EV_READ,
1712 ce1bfad9 2024-03-30 thomas session_dispatch_notifier, iev);
1713 ce1bfad9 2024-03-30 thomas gotd_imsg_event_add(iev);
1714 ce1bfad9 2024-03-30 thomas
1715 ce1bfad9 2024-03-30 thomas return NULL;
1716 ce1bfad9 2024-03-30 thomas }
1717 ce1bfad9 2024-03-30 thomas
1718 ce1bfad9 2024-03-30 thomas static const struct got_error *
1719 62ee7d94 2023-01-10 thomas recv_repo_child(struct imsg *imsg)
1720 62ee7d94 2023-01-10 thomas {
1721 62ee7d94 2023-01-10 thomas struct gotd_imsg_connect_repo_child ichild;
1722 62ee7d94 2023-01-10 thomas struct gotd_session_client *client = &gotd_session_client;
1723 62ee7d94 2023-01-10 thomas size_t datalen;
1724 3d97effa 2024-01-31 thomas int fd;
1725 62ee7d94 2023-01-10 thomas
1726 176350bf 2024-03-30 thomas if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
1727 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
1728 62ee7d94 2023-01-10 thomas
1729 62ee7d94 2023-01-10 thomas /* We should already have received a pipe to the listener. */
1730 62ee7d94 2023-01-10 thomas if (client->fd == -1)
1731 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
1732 62ee7d94 2023-01-10 thomas
1733 62ee7d94 2023-01-10 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1734 62ee7d94 2023-01-10 thomas if (datalen != sizeof(ichild))
1735 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
1736 62ee7d94 2023-01-10 thomas
1737 62ee7d94 2023-01-10 thomas memcpy(&ichild, imsg->data, sizeof(ichild));
1738 62ee7d94 2023-01-10 thomas
1739 62ee7d94 2023-01-10 thomas if (ichild.proc_id == PROC_REPO_WRITE)
1740 62ee7d94 2023-01-10 thomas client->is_writing = 1;
1741 62ee7d94 2023-01-10 thomas else if (ichild.proc_id == PROC_REPO_READ)
1742 62ee7d94 2023-01-10 thomas client->is_writing = 0;
1743 62ee7d94 2023-01-10 thomas else
1744 62ee7d94 2023-01-10 thomas return got_error_msg(GOT_ERR_PRIVSEP_MSG,
1745 62ee7d94 2023-01-10 thomas "bad child process type");
1746 d4628c48 2023-02-03 thomas
1747 3d97effa 2024-01-31 thomas fd = imsg_get_fd(imsg);
1748 3d97effa 2024-01-31 thomas if (fd == -1)
1749 62ee7d94 2023-01-10 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
1750 62ee7d94 2023-01-10 thomas
1751 74e85132 2024-03-30 thomas imsg_init(&gotd_session.repo_child_iev.ibuf, fd);
1752 74e85132 2024-03-30 thomas gotd_session.repo_child_iev.handler = session_dispatch_repo_child;
1753 74e85132 2024-03-30 thomas gotd_session.repo_child_iev.events = EV_READ;
1754 74e85132 2024-03-30 thomas gotd_session.repo_child_iev.handler_arg = NULL;
1755 74e85132 2024-03-30 thomas event_set(&gotd_session.repo_child_iev.ev,
1756 74e85132 2024-03-30 thomas gotd_session.repo_child_iev.ibuf.fd, EV_READ,
1757 74e85132 2024-03-30 thomas session_dispatch_repo_child, &gotd_session.repo_child_iev);
1758 74e85132 2024-03-30 thomas gotd_imsg_event_add(&gotd_session.repo_child_iev);
1759 62ee7d94 2023-01-10 thomas
1760 62ee7d94 2023-01-10 thomas /* The "recvfd" pledge promise is no longer needed. */
1761 62ee7d94 2023-01-10 thomas if (pledge("stdio rpath wpath cpath sendfd fattr flock", NULL) == -1)
1762 62ee7d94 2023-01-10 thomas fatal("pledge");
1763 62ee7d94 2023-01-10 thomas
1764 62ee7d94 2023-01-10 thomas return NULL;
1765 62ee7d94 2023-01-10 thomas }
1766 62ee7d94 2023-01-10 thomas
1767 62ee7d94 2023-01-10 thomas static void
1768 62ee7d94 2023-01-10 thomas session_dispatch(int fd, short event, void *arg)
1769 62ee7d94 2023-01-10 thomas {
1770 62ee7d94 2023-01-10 thomas struct gotd_imsgev *iev = arg;
1771 62ee7d94 2023-01-10 thomas struct imsgbuf *ibuf = &iev->ibuf;
1772 62ee7d94 2023-01-10 thomas struct gotd_session_client *client = &gotd_session_client;
1773 62ee7d94 2023-01-10 thomas ssize_t n;
1774 62ee7d94 2023-01-10 thomas int shut = 0;
1775 62ee7d94 2023-01-10 thomas struct imsg imsg;
1776 62ee7d94 2023-01-10 thomas
1777 62ee7d94 2023-01-10 thomas if (event & EV_READ) {
1778 62ee7d94 2023-01-10 thomas if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1779 62ee7d94 2023-01-10 thomas fatal("imsg_read error");
1780 62ee7d94 2023-01-10 thomas if (n == 0) {
1781 62ee7d94 2023-01-10 thomas /* Connection closed. */
1782 62ee7d94 2023-01-10 thomas shut = 1;
1783 62ee7d94 2023-01-10 thomas goto done;
1784 62ee7d94 2023-01-10 thomas }
1785 62ee7d94 2023-01-10 thomas }
1786 62ee7d94 2023-01-10 thomas
1787 62ee7d94 2023-01-10 thomas if (event & EV_WRITE) {
1788 62ee7d94 2023-01-10 thomas n = msgbuf_write(&ibuf->w);
1789 62ee7d94 2023-01-10 thomas if (n == -1 && errno != EAGAIN)
1790 62ee7d94 2023-01-10 thomas fatal("msgbuf_write");
1791 62ee7d94 2023-01-10 thomas if (n == 0) {
1792 62ee7d94 2023-01-10 thomas /* Connection closed. */
1793 62ee7d94 2023-01-10 thomas shut = 1;
1794 62ee7d94 2023-01-10 thomas goto done;
1795 62ee7d94 2023-01-10 thomas }
1796 62ee7d94 2023-01-10 thomas }
1797 62ee7d94 2023-01-10 thomas
1798 62ee7d94 2023-01-10 thomas for (;;) {
1799 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
1800 62ee7d94 2023-01-10 thomas uint32_t client_id = 0;
1801 62ee7d94 2023-01-10 thomas int do_disconnect = 0, do_list_refs = 0;
1802 62ee7d94 2023-01-10 thomas
1803 62ee7d94 2023-01-10 thomas if ((n = imsg_get(ibuf, &imsg)) == -1)
1804 62ee7d94 2023-01-10 thomas fatal("%s: imsg_get error", __func__);
1805 62ee7d94 2023-01-10 thomas if (n == 0) /* No more messages. */
1806 62ee7d94 2023-01-10 thomas break;
1807 62ee7d94 2023-01-10 thomas
1808 62ee7d94 2023-01-10 thomas switch (imsg.hdr.type) {
1809 62ee7d94 2023-01-10 thomas case GOTD_IMSG_ERROR:
1810 62ee7d94 2023-01-10 thomas do_disconnect = 1;
1811 62ee7d94 2023-01-10 thomas err = gotd_imsg_recv_error(&client_id, &imsg);
1812 62ee7d94 2023-01-10 thomas break;
1813 62ee7d94 2023-01-10 thomas case GOTD_IMSG_CONNECT:
1814 62ee7d94 2023-01-10 thomas err = recv_connect(&imsg);
1815 62ee7d94 2023-01-10 thomas break;
1816 62ee7d94 2023-01-10 thomas case GOTD_IMSG_DISCONNECT:
1817 62ee7d94 2023-01-10 thomas do_disconnect = 1;
1818 62ee7d94 2023-01-10 thomas break;
1819 ce1bfad9 2024-03-30 thomas case GOTD_IMSG_CONNECT_NOTIFIER:
1820 ce1bfad9 2024-03-30 thomas err = recv_notifier(&imsg);
1821 ce1bfad9 2024-03-30 thomas break;
1822 62ee7d94 2023-01-10 thomas case GOTD_IMSG_CONNECT_REPO_CHILD:
1823 62ee7d94 2023-01-10 thomas err = recv_repo_child(&imsg);
1824 62ee7d94 2023-01-10 thomas if (err)
1825 62ee7d94 2023-01-10 thomas break;
1826 62ee7d94 2023-01-10 thomas do_list_refs = 1;
1827 62ee7d94 2023-01-10 thomas break;
1828 62ee7d94 2023-01-10 thomas default:
1829 62ee7d94 2023-01-10 thomas log_debug("unexpected imsg %d", imsg.hdr.type);
1830 62ee7d94 2023-01-10 thomas break;
1831 62ee7d94 2023-01-10 thomas }
1832 62ee7d94 2023-01-10 thomas imsg_free(&imsg);
1833 62ee7d94 2023-01-10 thomas
1834 62ee7d94 2023-01-10 thomas if (do_disconnect) {
1835 62ee7d94 2023-01-10 thomas if (err)
1836 62ee7d94 2023-01-10 thomas disconnect_on_error(client, err);
1837 62ee7d94 2023-01-10 thomas else
1838 62ee7d94 2023-01-10 thomas disconnect(client);
1839 62ee7d94 2023-01-10 thomas } else if (do_list_refs)
1840 62ee7d94 2023-01-10 thomas err = list_refs_request();
1841 62ee7d94 2023-01-10 thomas
1842 62ee7d94 2023-01-10 thomas if (err)
1843 62ee7d94 2023-01-10 thomas log_warnx("uid %d: %s", client->euid, err->msg);
1844 62ee7d94 2023-01-10 thomas }
1845 62ee7d94 2023-01-10 thomas done:
1846 62ee7d94 2023-01-10 thomas if (!shut) {
1847 62ee7d94 2023-01-10 thomas gotd_imsg_event_add(iev);
1848 62ee7d94 2023-01-10 thomas } else {
1849 62ee7d94 2023-01-10 thomas /* This pipe is dead. Remove its event handler */
1850 62ee7d94 2023-01-10 thomas event_del(&iev->ev);
1851 62ee7d94 2023-01-10 thomas event_loopexit(NULL);
1852 d4628c48 2023-02-03 thomas }
1853 62ee7d94 2023-01-10 thomas }
1854 62ee7d94 2023-01-10 thomas
1855 62ee7d94 2023-01-10 thomas void
1856 62ee7d94 2023-01-10 thomas session_main(const char *title, const char *repo_path,
1857 7fed8fa4 2023-06-22 thomas int *pack_fds, int *temp_fds, struct timeval *request_timeout,
1858 ce1bfad9 2024-03-30 thomas struct gotd_repo *repo_cfg, enum gotd_procid proc_id)
1859 62ee7d94 2023-01-10 thomas {
1860 62ee7d94 2023-01-10 thomas const struct got_error *err = NULL;
1861 62ee7d94 2023-01-10 thomas struct event evsigint, evsigterm, evsighup, evsigusr1;
1862 62ee7d94 2023-01-10 thomas
1863 ce1bfad9 2024-03-30 thomas STAILQ_INIT(&notifications);
1864 ce1bfad9 2024-03-30 thomas
1865 62ee7d94 2023-01-10 thomas gotd_session.title = title;
1866 62ee7d94 2023-01-10 thomas gotd_session.pid = getpid();
1867 62ee7d94 2023-01-10 thomas gotd_session.pack_fds = pack_fds;
1868 62ee7d94 2023-01-10 thomas gotd_session.temp_fds = temp_fds;
1869 62ee7d94 2023-01-10 thomas memcpy(&gotd_session.request_timeout, request_timeout,
1870 62ee7d94 2023-01-10 thomas sizeof(gotd_session.request_timeout));
1871 ce1bfad9 2024-03-30 thomas gotd_session.repo_cfg = repo_cfg;
1872 7fed8fa4 2023-06-22 thomas gotd_session.proc_id = proc_id;
1873 62ee7d94 2023-01-10 thomas
1874 ce1bfad9 2024-03-30 thomas imsg_init(&gotd_session.notifier_iev.ibuf, -1);
1875 ce1bfad9 2024-03-30 thomas
1876 62ee7d94 2023-01-10 thomas err = got_repo_open(&gotd_session.repo, repo_path, NULL, pack_fds);
1877 62ee7d94 2023-01-10 thomas if (err)
1878 62ee7d94 2023-01-10 thomas goto done;
1879 62ee7d94 2023-01-10 thomas if (!got_repo_is_bare(gotd_session.repo)) {
1880 62ee7d94 2023-01-10 thomas err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1881 62ee7d94 2023-01-10 thomas "bare git repository required");
1882 62ee7d94 2023-01-10 thomas goto done;
1883 62ee7d94 2023-01-10 thomas }
1884 62ee7d94 2023-01-10 thomas
1885 62ee7d94 2023-01-10 thomas got_repo_temp_fds_set(gotd_session.repo, temp_fds);
1886 62ee7d94 2023-01-10 thomas
1887 62ee7d94 2023-01-10 thomas signal_set(&evsigint, SIGINT, gotd_session_sighdlr, NULL);
1888 62ee7d94 2023-01-10 thomas signal_set(&evsigterm, SIGTERM, gotd_session_sighdlr, NULL);
1889 62ee7d94 2023-01-10 thomas signal_set(&evsighup, SIGHUP, gotd_session_sighdlr, NULL);
1890 62ee7d94 2023-01-10 thomas signal_set(&evsigusr1, SIGUSR1, gotd_session_sighdlr, NULL);
1891 62ee7d94 2023-01-10 thomas signal(SIGPIPE, SIG_IGN);
1892 62ee7d94 2023-01-10 thomas
1893 62ee7d94 2023-01-10 thomas signal_add(&evsigint, NULL);
1894 62ee7d94 2023-01-10 thomas signal_add(&evsigterm, NULL);
1895 62ee7d94 2023-01-10 thomas signal_add(&evsighup, NULL);
1896 62ee7d94 2023-01-10 thomas signal_add(&evsigusr1, NULL);
1897 62ee7d94 2023-01-10 thomas
1898 176350bf 2024-03-30 thomas gotd_session.state = GOTD_STATE_EXPECT_LIST_REFS;
1899 176350bf 2024-03-30 thomas
1900 62ee7d94 2023-01-10 thomas gotd_session_client.fd = -1;
1901 62ee7d94 2023-01-10 thomas gotd_session_client.nref_updates = -1;
1902 a6f25078 2023-01-10 thomas gotd_session_client.delta_cache_fd = -1;
1903 98c7fd82 2023-01-23 thomas gotd_session_client.accept_flush_pkt = 1;
1904 62ee7d94 2023-01-10 thomas
1905 62ee7d94 2023-01-10 thomas imsg_init(&gotd_session.parent_iev.ibuf, GOTD_FILENO_MSG_PIPE);
1906 62ee7d94 2023-01-10 thomas gotd_session.parent_iev.handler = session_dispatch;
1907 62ee7d94 2023-01-10 thomas gotd_session.parent_iev.events = EV_READ;
1908 62ee7d94 2023-01-10 thomas gotd_session.parent_iev.handler_arg = NULL;
1909 62ee7d94 2023-01-10 thomas event_set(&gotd_session.parent_iev.ev, gotd_session.parent_iev.ibuf.fd,
1910 62ee7d94 2023-01-10 thomas EV_READ, session_dispatch, &gotd_session.parent_iev);
1911 62ee7d94 2023-01-10 thomas if (gotd_imsg_compose_event(&gotd_session.parent_iev,
1912 7fed8fa4 2023-06-22 thomas GOTD_IMSG_CLIENT_SESSION_READY, gotd_session.proc_id,
1913 7fed8fa4 2023-06-22 thomas -1, NULL, 0) == -1) {
1914 62ee7d94 2023-01-10 thomas err = got_error_from_errno("imsg compose CLIENT_SESSION_READY");
1915 62ee7d94 2023-01-10 thomas goto done;
1916 62ee7d94 2023-01-10 thomas }
1917 62ee7d94 2023-01-10 thomas
1918 62ee7d94 2023-01-10 thomas event_dispatch();
1919 62ee7d94 2023-01-10 thomas done:
1920 62ee7d94 2023-01-10 thomas if (err)
1921 62ee7d94 2023-01-10 thomas log_warnx("%s: %s", title, err->msg);
1922 62ee7d94 2023-01-10 thomas gotd_session_shutdown();
1923 62ee7d94 2023-01-10 thomas }
1924 62ee7d94 2023-01-10 thomas
1925 62ee7d94 2023-01-10 thomas void
1926 62ee7d94 2023-01-10 thomas gotd_session_shutdown(void)
1927 62ee7d94 2023-01-10 thomas {
1928 ce1bfad9 2024-03-30 thomas struct gotd_session_notif *notif;
1929 ce1bfad9 2024-03-30 thomas
1930 5330ab76 2023-02-17 thomas log_debug("shutting down");
1931 ce1bfad9 2024-03-30 thomas
1932 ce1bfad9 2024-03-30 thomas while (!STAILQ_EMPTY(&notifications)) {
1933 ce1bfad9 2024-03-30 thomas notif = STAILQ_FIRST(&notifications);
1934 ce1bfad9 2024-03-30 thomas STAILQ_REMOVE_HEAD(&notifications, entry);
1935 ce1bfad9 2024-03-30 thomas if (notif->fd != -1)
1936 ce1bfad9 2024-03-30 thomas close(notif->fd);
1937 ce1bfad9 2024-03-30 thomas free(notif->refname);
1938 ce1bfad9 2024-03-30 thomas free(notif);
1939 ce1bfad9 2024-03-30 thomas }
1940 ce1bfad9 2024-03-30 thomas
1941 62ee7d94 2023-01-10 thomas if (gotd_session.repo)
1942 62ee7d94 2023-01-10 thomas got_repo_close(gotd_session.repo);
1943 62ee7d94 2023-01-10 thomas got_repo_pack_fds_close(gotd_session.pack_fds);
1944 62ee7d94 2023-01-10 thomas got_repo_temp_fds_close(gotd_session.temp_fds);
1945 ce1bfad9 2024-03-30 thomas free(gotd_session_client.username);
1946 62ee7d94 2023-01-10 thomas exit(0);
1947 62ee7d94 2023-01-10 thomas }