Blame


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