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 1b1a386d 2024-07-09 op if (memcmp(&notif->new_id, &icontent.new_id,
527 1b1a386d 2024-07-09 op sizeof(notif->new_id)) != 0) {
528 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_PRIVSEP_MSG,
529 e70fd952 2024-03-22 stsp "received notification content for unknown event");
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 1b1a386d 2024-07-09 op if (memcmp(&notif->old_id, &icontent.old_id,
534 1b1a386d 2024-07-09 op sizeof(notif->old_id)) != 0) {
535 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_PRIVSEP_MSG,
536 e70fd952 2024-03-22 stsp "received notification content for unknown event");
537 e70fd952 2024-03-22 stsp goto done;
538 e70fd952 2024-03-22 stsp }
539 1b1a386d 2024-07-09 op } else if (memcmp(&notif->old_id, &icontent.old_id,
540 1b1a386d 2024-07-09 op sizeof(notif->old_id)) != 0 ||
541 1b1a386d 2024-07-09 op memcmp(&notif->new_id, &icontent.new_id,
542 1b1a386d 2024-07-09 op sizeof(notif->old_id)) != 0) {
543 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_PRIVSEP_MSG,
544 e70fd952 2024-03-22 stsp "received notification content for unknown event");
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 be12ea2c 2024-07-11 op memset(&old_id, 0, sizeof(old_id));
700 aaa34732 2024-07-13 op memcpy(old_id.hash, iref.old_id, SHA1_DIGEST_LENGTH);
701 be12ea2c 2024-07-11 op memset(&new_id, 0, sizeof(new_id));
702 aaa34732 2024-07-13 op memcpy(new_id.hash, iref.new_id, SHA1_DIGEST_LENGTH);
703 e70fd952 2024-03-22 stsp err = got_repo_find_object_id(iref.delete_ref ? &old_id : &new_id,
704 e70fd952 2024-03-22 stsp repo);
705 e70fd952 2024-03-22 stsp if (err)
706 e70fd952 2024-03-22 stsp goto done;
707 e70fd952 2024-03-22 stsp
708 e70fd952 2024-03-22 stsp if (iref.ref_is_new) {
709 e70fd952 2024-03-22 stsp err = got_ref_open(&ref, repo, refname, 0);
710 e70fd952 2024-03-22 stsp if (err) {
711 e70fd952 2024-03-22 stsp if (err->code != GOT_ERR_NOT_REF)
712 e70fd952 2024-03-22 stsp goto done;
713 e70fd952 2024-03-22 stsp err = got_ref_alloc(&ref, refname, &new_id);
714 e70fd952 2024-03-22 stsp if (err)
715 e70fd952 2024-03-22 stsp goto done;
716 e70fd952 2024-03-22 stsp err = got_ref_write(ref, repo); /* will lock/unlock */
717 e70fd952 2024-03-22 stsp if (err)
718 e70fd952 2024-03-22 stsp goto done;
719 e70fd952 2024-03-22 stsp err = queue_notification(NULL, &new_id, repo, ref);
720 e70fd952 2024-03-22 stsp if (err)
721 e70fd952 2024-03-22 stsp goto done;
722 e70fd952 2024-03-22 stsp } else {
723 e70fd952 2024-03-22 stsp err = got_ref_resolve(&id, repo, ref);
724 e70fd952 2024-03-22 stsp if (err)
725 e70fd952 2024-03-22 stsp goto done;
726 e70fd952 2024-03-22 stsp got_object_id_hex(&new_id, hex1, sizeof(hex1));
727 e70fd952 2024-03-22 stsp got_object_id_hex(id, hex2, sizeof(hex2));
728 e70fd952 2024-03-22 stsp err = got_error_fmt(GOT_ERR_REF_BUSY,
729 e70fd952 2024-03-22 stsp "Addition %s: %s failed; %s: %s has been "
730 e70fd952 2024-03-22 stsp "created by someone else while transaction "
731 e70fd952 2024-03-22 stsp "was in progress",
732 e70fd952 2024-03-22 stsp got_ref_get_name(ref), hex1,
733 e70fd952 2024-03-22 stsp got_ref_get_name(ref), hex2);
734 e70fd952 2024-03-22 stsp goto done;
735 e70fd952 2024-03-22 stsp }
736 e70fd952 2024-03-22 stsp } else if (iref.delete_ref) {
737 e70fd952 2024-03-22 stsp err = got_ref_open(&ref, repo, refname, 1 /* lock */);
738 e70fd952 2024-03-22 stsp if (err)
739 e70fd952 2024-03-22 stsp goto done;
740 e70fd952 2024-03-22 stsp locked = 1;
741 e70fd952 2024-03-22 stsp
742 e70fd952 2024-03-22 stsp err = got_ref_resolve(&id, repo, ref);
743 e70fd952 2024-03-22 stsp if (err)
744 e70fd952 2024-03-22 stsp goto done;
745 e70fd952 2024-03-22 stsp
746 e70fd952 2024-03-22 stsp if (got_object_id_cmp(id, &old_id) != 0) {
747 e70fd952 2024-03-22 stsp got_object_id_hex(&old_id, hex1, sizeof(hex1));
748 e70fd952 2024-03-22 stsp got_object_id_hex(id, hex2, sizeof(hex2));
749 e70fd952 2024-03-22 stsp err = got_error_fmt(GOT_ERR_REF_BUSY,
750 e70fd952 2024-03-22 stsp "Deletion %s: %s failed; %s: %s has been "
751 e70fd952 2024-03-22 stsp "created by someone else while transaction "
752 e70fd952 2024-03-22 stsp "was in progress",
753 e70fd952 2024-03-22 stsp got_ref_get_name(ref), hex1,
754 e70fd952 2024-03-22 stsp got_ref_get_name(ref), hex2);
755 e70fd952 2024-03-22 stsp goto done;
756 e70fd952 2024-03-22 stsp }
757 e70fd952 2024-03-22 stsp
758 e70fd952 2024-03-22 stsp err = got_ref_delete(ref, repo);
759 e70fd952 2024-03-22 stsp if (err)
760 e70fd952 2024-03-22 stsp goto done;
761 e70fd952 2024-03-22 stsp err = queue_notification(&old_id, NULL, repo, ref);
762 e70fd952 2024-03-22 stsp if (err)
763 e70fd952 2024-03-22 stsp goto done;
764 e70fd952 2024-03-22 stsp free(id);
765 e70fd952 2024-03-22 stsp id = NULL;
766 e70fd952 2024-03-22 stsp } else {
767 e70fd952 2024-03-22 stsp err = got_ref_open(&ref, repo, refname, 1 /* lock */);
768 e70fd952 2024-03-22 stsp if (err)
769 e70fd952 2024-03-22 stsp goto done;
770 e70fd952 2024-03-22 stsp locked = 1;
771 e70fd952 2024-03-22 stsp
772 e70fd952 2024-03-22 stsp err = got_ref_resolve(&id, repo, ref);
773 e70fd952 2024-03-22 stsp if (err)
774 e70fd952 2024-03-22 stsp goto done;
775 e70fd952 2024-03-22 stsp
776 e70fd952 2024-03-22 stsp if (got_object_id_cmp(id, &old_id) != 0) {
777 e70fd952 2024-03-22 stsp got_object_id_hex(&old_id, hex1, sizeof(hex1));
778 e70fd952 2024-03-22 stsp got_object_id_hex(id, hex2, sizeof(hex2));
779 e70fd952 2024-03-22 stsp err = got_error_fmt(GOT_ERR_REF_BUSY,
780 e70fd952 2024-03-22 stsp "Update %s: %s failed; %s: %s has been "
781 e70fd952 2024-03-22 stsp "created by someone else while transaction "
782 e70fd952 2024-03-22 stsp "was in progress",
783 e70fd952 2024-03-22 stsp got_ref_get_name(ref), hex1,
784 e70fd952 2024-03-22 stsp got_ref_get_name(ref), hex2);
785 e70fd952 2024-03-22 stsp goto done;
786 e70fd952 2024-03-22 stsp }
787 e70fd952 2024-03-22 stsp
788 e70fd952 2024-03-22 stsp if (got_object_id_cmp(&new_id, &old_id) != 0) {
789 e70fd952 2024-03-22 stsp err = got_ref_change_ref(ref, &new_id);
790 e70fd952 2024-03-22 stsp if (err)
791 e70fd952 2024-03-22 stsp goto done;
792 e70fd952 2024-03-22 stsp err = got_ref_write(ref, repo);
793 e70fd952 2024-03-22 stsp if (err)
794 e70fd952 2024-03-22 stsp goto done;
795 e70fd952 2024-03-22 stsp err = queue_notification(&old_id, &new_id, repo, ref);
796 e70fd952 2024-03-22 stsp if (err)
797 e70fd952 2024-03-22 stsp goto done;
798 e70fd952 2024-03-22 stsp }
799 e70fd952 2024-03-22 stsp
800 e70fd952 2024-03-22 stsp free(id);
801 e70fd952 2024-03-22 stsp id = NULL;
802 e70fd952 2024-03-22 stsp }
803 e70fd952 2024-03-22 stsp done:
804 e70fd952 2024-03-22 stsp if (err) {
805 e70fd952 2024-03-22 stsp if (err->code == GOT_ERR_LOCKFILE_TIMEOUT) {
806 e70fd952 2024-03-22 stsp err = got_error_fmt(GOT_ERR_LOCKFILE_TIMEOUT,
807 e70fd952 2024-03-22 stsp "could not acquire exclusive file lock for %s",
808 e70fd952 2024-03-22 stsp refname);
809 e70fd952 2024-03-22 stsp }
810 e70fd952 2024-03-22 stsp send_ref_update_ng(client, &iref, refname, err->msg);
811 e70fd952 2024-03-22 stsp } else
812 e70fd952 2024-03-22 stsp send_ref_update_ok(client, &iref, refname);
813 e70fd952 2024-03-22 stsp
814 e70fd952 2024-03-22 stsp if (client->nref_updates > 0) {
815 e70fd952 2024-03-22 stsp client->nref_updates--;
816 e70fd952 2024-03-22 stsp if (client->nref_updates == 0) {
817 e70fd952 2024-03-22 stsp send_refs_updated(client);
818 e70fd952 2024-03-22 stsp notif = STAILQ_FIRST(&notifications);
819 e70fd952 2024-03-22 stsp if (notif) {
820 e70fd952 2024-03-22 stsp gotd_session.state = GOTD_STATE_NOTIFY;
821 e70fd952 2024-03-22 stsp err = request_notification(notif);
822 e70fd952 2024-03-22 stsp if (err) {
823 e70fd952 2024-03-22 stsp log_warn("could not send notification: "
824 e70fd952 2024-03-22 stsp "%s", err->msg);
825 e70fd952 2024-03-22 stsp client->flush_disconnect = 1;
826 e70fd952 2024-03-22 stsp }
827 e70fd952 2024-03-22 stsp } else
828 e70fd952 2024-03-22 stsp client->flush_disconnect = 1;
829 e70fd952 2024-03-22 stsp }
830 e70fd952 2024-03-22 stsp
831 e70fd952 2024-03-22 stsp }
832 e70fd952 2024-03-22 stsp if (locked) {
833 e70fd952 2024-03-22 stsp const struct got_error *unlock_err;
834 e70fd952 2024-03-22 stsp unlock_err = got_ref_unlock(ref);
835 e70fd952 2024-03-22 stsp if (unlock_err && err == NULL)
836 e70fd952 2024-03-22 stsp err = unlock_err;
837 e70fd952 2024-03-22 stsp }
838 e70fd952 2024-03-22 stsp if (ref)
839 e70fd952 2024-03-22 stsp got_ref_close(ref);
840 e70fd952 2024-03-22 stsp free(refname);
841 e70fd952 2024-03-22 stsp free(id);
842 e70fd952 2024-03-22 stsp return err;
843 e70fd952 2024-03-22 stsp }
844 e70fd952 2024-03-22 stsp
845 e70fd952 2024-03-22 stsp static const struct got_error *
846 e70fd952 2024-03-22 stsp recv_notification_content(struct imsg *imsg)
847 e70fd952 2024-03-22 stsp {
848 e70fd952 2024-03-22 stsp struct gotd_imsg_notification_content inotif;
849 e70fd952 2024-03-22 stsp size_t datalen;
850 e70fd952 2024-03-22 stsp
851 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
852 e70fd952 2024-03-22 stsp if (datalen < sizeof(inotif))
853 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
854 e70fd952 2024-03-22 stsp memcpy(&inotif, imsg->data, sizeof(inotif));
855 e70fd952 2024-03-22 stsp
856 e70fd952 2024-03-22 stsp return NULL;
857 e70fd952 2024-03-22 stsp }
858 e70fd952 2024-03-22 stsp
859 e70fd952 2024-03-22 stsp static void
860 e70fd952 2024-03-22 stsp session_dispatch_repo_child(int fd, short event, void *arg)
861 e70fd952 2024-03-22 stsp {
862 e70fd952 2024-03-22 stsp struct gotd_imsgev *iev = arg;
863 e70fd952 2024-03-22 stsp struct imsgbuf *ibuf = &iev->ibuf;
864 e70fd952 2024-03-22 stsp struct gotd_session_client *client = &gotd_session_client;
865 e70fd952 2024-03-22 stsp ssize_t n;
866 e70fd952 2024-03-22 stsp int shut = 0;
867 e70fd952 2024-03-22 stsp struct imsg imsg;
868 e70fd952 2024-03-22 stsp
869 e70fd952 2024-03-22 stsp if (event & EV_READ) {
870 e70fd952 2024-03-22 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
871 e70fd952 2024-03-22 stsp fatal("imsg_read error");
872 e70fd952 2024-03-22 stsp if (n == 0) {
873 e70fd952 2024-03-22 stsp /* Connection closed. */
874 e70fd952 2024-03-22 stsp shut = 1;
875 e70fd952 2024-03-22 stsp goto done;
876 e70fd952 2024-03-22 stsp }
877 e70fd952 2024-03-22 stsp }
878 e70fd952 2024-03-22 stsp
879 e70fd952 2024-03-22 stsp if (event & EV_WRITE) {
880 e70fd952 2024-03-22 stsp n = msgbuf_write(&ibuf->w);
881 e70fd952 2024-03-22 stsp if (n == -1 && errno != EAGAIN)
882 e70fd952 2024-03-22 stsp fatal("msgbuf_write");
883 e70fd952 2024-03-22 stsp if (n == 0) {
884 e70fd952 2024-03-22 stsp /* Connection closed. */
885 e70fd952 2024-03-22 stsp shut = 1;
886 e70fd952 2024-03-22 stsp goto done;
887 e70fd952 2024-03-22 stsp }
888 e70fd952 2024-03-22 stsp }
889 e70fd952 2024-03-22 stsp
890 e70fd952 2024-03-22 stsp for (;;) {
891 e70fd952 2024-03-22 stsp const struct got_error *err = NULL;
892 e70fd952 2024-03-22 stsp uint32_t client_id = 0;
893 e70fd952 2024-03-22 stsp int do_disconnect = 0;
894 e70fd952 2024-03-22 stsp int do_ref_updates = 0, do_ref_update = 0;
895 e70fd952 2024-03-22 stsp int do_packfile_install = 0, do_notify = 0;
896 e70fd952 2024-03-22 stsp
897 e70fd952 2024-03-22 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
898 e70fd952 2024-03-22 stsp fatal("%s: imsg_get error", __func__);
899 e70fd952 2024-03-22 stsp if (n == 0) /* No more messages. */
900 e70fd952 2024-03-22 stsp break;
901 e70fd952 2024-03-22 stsp
902 e70fd952 2024-03-22 stsp switch (imsg.hdr.type) {
903 e70fd952 2024-03-22 stsp case GOTD_IMSG_ERROR:
904 e70fd952 2024-03-22 stsp do_disconnect = 1;
905 e70fd952 2024-03-22 stsp err = gotd_imsg_recv_error(&client_id, &imsg);
906 e70fd952 2024-03-22 stsp break;
907 e70fd952 2024-03-22 stsp case GOTD_IMSG_PACKFILE_INSTALL:
908 e70fd952 2024-03-22 stsp err = recv_packfile_install(&imsg);
909 e70fd952 2024-03-22 stsp if (err == NULL)
910 e70fd952 2024-03-22 stsp do_packfile_install = 1;
911 e70fd952 2024-03-22 stsp break;
912 e70fd952 2024-03-22 stsp case GOTD_IMSG_REF_UPDATES_START:
913 e70fd952 2024-03-22 stsp err = recv_ref_updates_start(&imsg);
914 e70fd952 2024-03-22 stsp if (err == NULL)
915 e70fd952 2024-03-22 stsp do_ref_updates = 1;
916 e70fd952 2024-03-22 stsp break;
917 e70fd952 2024-03-22 stsp case GOTD_IMSG_REF_UPDATE:
918 e70fd952 2024-03-22 stsp err = recv_ref_update(&imsg);
919 e70fd952 2024-03-22 stsp if (err == NULL)
920 e70fd952 2024-03-22 stsp do_ref_update = 1;
921 e70fd952 2024-03-22 stsp break;
922 e70fd952 2024-03-22 stsp case GOTD_IMSG_NOTIFY:
923 e70fd952 2024-03-22 stsp err = recv_notification_content(&imsg);
924 e70fd952 2024-03-22 stsp if (err == NULL)
925 e70fd952 2024-03-22 stsp do_notify = 1;
926 e70fd952 2024-03-22 stsp break;
927 e70fd952 2024-03-22 stsp default:
928 e70fd952 2024-03-22 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
929 e70fd952 2024-03-22 stsp break;
930 e70fd952 2024-03-22 stsp }
931 e70fd952 2024-03-22 stsp
932 3bdb5066 2024-04-16 op if (do_disconnect || err) {
933 e70fd952 2024-03-22 stsp if (err)
934 e70fd952 2024-03-22 stsp disconnect_on_error(client, err);
935 e70fd952 2024-03-22 stsp else
936 e70fd952 2024-03-22 stsp disconnect(client);
937 e70fd952 2024-03-22 stsp } else {
938 e70fd952 2024-03-22 stsp struct gotd_session_notif *notif;
939 e70fd952 2024-03-22 stsp
940 e70fd952 2024-03-22 stsp if (do_packfile_install)
941 e70fd952 2024-03-22 stsp err = install_pack(client,
942 e70fd952 2024-03-22 stsp gotd_session.repo->path, &imsg);
943 e70fd952 2024-03-22 stsp else if (do_ref_updates)
944 e70fd952 2024-03-22 stsp err = begin_ref_updates(client, &imsg);
945 e70fd952 2024-03-22 stsp else if (do_ref_update)
946 e70fd952 2024-03-22 stsp err = update_ref(&shut, client,
947 e70fd952 2024-03-22 stsp gotd_session.repo->path, &imsg);
948 e70fd952 2024-03-22 stsp else if (do_notify)
949 e70fd952 2024-03-22 stsp err = forward_notification(client, &imsg);
950 e70fd952 2024-03-22 stsp if (err)
951 e70fd952 2024-03-22 stsp log_warnx("uid %d: %s", client->euid, err->msg);
952 e70fd952 2024-03-22 stsp
953 e70fd952 2024-03-22 stsp notif = STAILQ_FIRST(&notifications);
954 e70fd952 2024-03-22 stsp if (notif && do_notify) {
955 e70fd952 2024-03-22 stsp /* Request content for next notification. */
956 e70fd952 2024-03-22 stsp err = request_notification(notif);
957 e70fd952 2024-03-22 stsp if (err) {
958 e70fd952 2024-03-22 stsp log_warn("could not send notification: "
959 e70fd952 2024-03-22 stsp "%s", err->msg);
960 e70fd952 2024-03-22 stsp shut = 1;
961 e70fd952 2024-03-22 stsp }
962 e70fd952 2024-03-22 stsp }
963 e70fd952 2024-03-22 stsp }
964 e70fd952 2024-03-22 stsp imsg_free(&imsg);
965 e70fd952 2024-03-22 stsp }
966 e70fd952 2024-03-22 stsp done:
967 e70fd952 2024-03-22 stsp if (!shut) {
968 e70fd952 2024-03-22 stsp gotd_imsg_event_add(iev);
969 e70fd952 2024-03-22 stsp } else {
970 e70fd952 2024-03-22 stsp /* This pipe is dead. Remove its event handler */
971 e70fd952 2024-03-22 stsp event_del(&iev->ev);
972 e70fd952 2024-03-22 stsp event_loopexit(NULL);
973 e70fd952 2024-03-22 stsp }
974 e70fd952 2024-03-22 stsp }
975 e70fd952 2024-03-22 stsp
976 e70fd952 2024-03-22 stsp static const struct got_error *
977 e70fd952 2024-03-22 stsp recv_capabilities(struct gotd_session_client *client, struct imsg *imsg)
978 e70fd952 2024-03-22 stsp {
979 e70fd952 2024-03-22 stsp struct gotd_imsg_capabilities icapas;
980 e70fd952 2024-03-22 stsp size_t datalen;
981 e70fd952 2024-03-22 stsp
982 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
983 e70fd952 2024-03-22 stsp if (datalen != sizeof(icapas))
984 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
985 e70fd952 2024-03-22 stsp memcpy(&icapas, imsg->data, sizeof(icapas));
986 e70fd952 2024-03-22 stsp
987 e70fd952 2024-03-22 stsp client->ncapa_alloc = icapas.ncapabilities;
988 e70fd952 2024-03-22 stsp client->capabilities = calloc(client->ncapa_alloc,
989 e70fd952 2024-03-22 stsp sizeof(*client->capabilities));
990 e70fd952 2024-03-22 stsp if (client->capabilities == NULL) {
991 e70fd952 2024-03-22 stsp client->ncapa_alloc = 0;
992 e70fd952 2024-03-22 stsp return got_error_from_errno("calloc");
993 e70fd952 2024-03-22 stsp }
994 e70fd952 2024-03-22 stsp
995 e70fd952 2024-03-22 stsp log_debug("expecting %zu capabilities from uid %d",
996 e70fd952 2024-03-22 stsp client->ncapa_alloc, client->euid);
997 e70fd952 2024-03-22 stsp return NULL;
998 e70fd952 2024-03-22 stsp }
999 e70fd952 2024-03-22 stsp
1000 e70fd952 2024-03-22 stsp static const struct got_error *
1001 e70fd952 2024-03-22 stsp recv_capability(struct gotd_session_client *client, struct imsg *imsg)
1002 e70fd952 2024-03-22 stsp {
1003 e70fd952 2024-03-22 stsp struct gotd_imsg_capability icapa;
1004 e70fd952 2024-03-22 stsp struct gotd_client_capability *capa;
1005 e70fd952 2024-03-22 stsp size_t datalen;
1006 e70fd952 2024-03-22 stsp char *key, *value = NULL;
1007 e70fd952 2024-03-22 stsp
1008 e70fd952 2024-03-22 stsp if (client->capabilities == NULL ||
1009 e70fd952 2024-03-22 stsp client->ncapabilities >= client->ncapa_alloc) {
1010 e70fd952 2024-03-22 stsp return got_error_msg(GOT_ERR_BAD_REQUEST,
1011 e70fd952 2024-03-22 stsp "unexpected capability received");
1012 e70fd952 2024-03-22 stsp }
1013 e70fd952 2024-03-22 stsp
1014 e70fd952 2024-03-22 stsp memset(&icapa, 0, sizeof(icapa));
1015 e70fd952 2024-03-22 stsp
1016 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1017 e70fd952 2024-03-22 stsp if (datalen < sizeof(icapa))
1018 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1019 e70fd952 2024-03-22 stsp memcpy(&icapa, imsg->data, sizeof(icapa));
1020 e70fd952 2024-03-22 stsp
1021 e70fd952 2024-03-22 stsp if (datalen != sizeof(icapa) + icapa.key_len + icapa.value_len)
1022 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1023 e70fd952 2024-03-22 stsp
1024 e70fd952 2024-03-22 stsp key = strndup(imsg->data + sizeof(icapa), icapa.key_len);
1025 e70fd952 2024-03-22 stsp if (key == NULL)
1026 e70fd952 2024-03-22 stsp return got_error_from_errno("strndup");
1027 e70fd952 2024-03-22 stsp if (icapa.value_len > 0) {
1028 e70fd952 2024-03-22 stsp value = strndup(imsg->data + sizeof(icapa) + icapa.key_len,
1029 e70fd952 2024-03-22 stsp icapa.value_len);
1030 e70fd952 2024-03-22 stsp if (value == NULL) {
1031 e70fd952 2024-03-22 stsp free(key);
1032 e70fd952 2024-03-22 stsp return got_error_from_errno("strndup");
1033 e70fd952 2024-03-22 stsp }
1034 e70fd952 2024-03-22 stsp }
1035 e70fd952 2024-03-22 stsp
1036 e70fd952 2024-03-22 stsp capa = &client->capabilities[client->ncapabilities++];
1037 e70fd952 2024-03-22 stsp capa->key = key;
1038 e70fd952 2024-03-22 stsp capa->value = value;
1039 e70fd952 2024-03-22 stsp
1040 e70fd952 2024-03-22 stsp if (value)
1041 e70fd952 2024-03-22 stsp log_debug("uid %d: capability %s=%s", client->euid, key, value);
1042 e70fd952 2024-03-22 stsp else
1043 e70fd952 2024-03-22 stsp log_debug("uid %d: capability %s", client->euid, key);
1044 e70fd952 2024-03-22 stsp
1045 e70fd952 2024-03-22 stsp return NULL;
1046 e70fd952 2024-03-22 stsp }
1047 e70fd952 2024-03-22 stsp
1048 e70fd952 2024-03-22 stsp static const struct got_error *
1049 e70fd952 2024-03-22 stsp forward_ref_update(struct gotd_session_client *client, struct imsg *imsg)
1050 e70fd952 2024-03-22 stsp {
1051 e70fd952 2024-03-22 stsp const struct got_error *err = NULL;
1052 e70fd952 2024-03-22 stsp struct gotd_imsg_ref_update ireq;
1053 e70fd952 2024-03-22 stsp struct gotd_imsg_ref_update *iref = NULL;
1054 e70fd952 2024-03-22 stsp size_t datalen;
1055 e70fd952 2024-03-22 stsp
1056 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1057 e70fd952 2024-03-22 stsp if (datalen < sizeof(ireq))
1058 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1059 e70fd952 2024-03-22 stsp memcpy(&ireq, imsg->data, sizeof(ireq));
1060 e70fd952 2024-03-22 stsp if (datalen != sizeof(ireq) + ireq.name_len)
1061 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1062 e70fd952 2024-03-22 stsp
1063 e70fd952 2024-03-22 stsp iref = malloc(datalen);
1064 e70fd952 2024-03-22 stsp if (iref == NULL)
1065 e70fd952 2024-03-22 stsp return got_error_from_errno("malloc");
1066 e70fd952 2024-03-22 stsp memcpy(iref, imsg->data, datalen);
1067 e70fd952 2024-03-22 stsp
1068 e70fd952 2024-03-22 stsp if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1069 e70fd952 2024-03-22 stsp GOTD_IMSG_REF_UPDATE, PROC_SESSION_WRITE, -1,
1070 e70fd952 2024-03-22 stsp iref, datalen) == -1)
1071 e70fd952 2024-03-22 stsp err = got_error_from_errno("imsg compose REF_UPDATE");
1072 e70fd952 2024-03-22 stsp free(iref);
1073 e70fd952 2024-03-22 stsp return err;
1074 e70fd952 2024-03-22 stsp }
1075 e70fd952 2024-03-22 stsp
1076 e70fd952 2024-03-22 stsp static int
1077 e70fd952 2024-03-22 stsp client_has_capability(struct gotd_session_client *client, const char *capastr)
1078 e70fd952 2024-03-22 stsp {
1079 e70fd952 2024-03-22 stsp struct gotd_client_capability *capa;
1080 e70fd952 2024-03-22 stsp size_t i;
1081 e70fd952 2024-03-22 stsp
1082 e70fd952 2024-03-22 stsp if (client->ncapabilities == 0)
1083 e70fd952 2024-03-22 stsp return 0;
1084 e70fd952 2024-03-22 stsp
1085 e70fd952 2024-03-22 stsp for (i = 0; i < client->ncapabilities; i++) {
1086 e70fd952 2024-03-22 stsp capa = &client->capabilities[i];
1087 e70fd952 2024-03-22 stsp if (strcmp(capa->key, capastr) == 0)
1088 e70fd952 2024-03-22 stsp return 1;
1089 e70fd952 2024-03-22 stsp }
1090 e70fd952 2024-03-22 stsp
1091 e70fd952 2024-03-22 stsp return 0;
1092 e70fd952 2024-03-22 stsp }
1093 e70fd952 2024-03-22 stsp
1094 e70fd952 2024-03-22 stsp static const struct got_error *
1095 e70fd952 2024-03-22 stsp recv_packfile(struct gotd_session_client *client)
1096 e70fd952 2024-03-22 stsp {
1097 e70fd952 2024-03-22 stsp const struct got_error *err = NULL;
1098 e70fd952 2024-03-22 stsp struct gotd_imsg_recv_packfile ipack;
1099 e70fd952 2024-03-22 stsp char *basepath = NULL, *pack_path = NULL, *idx_path = NULL;
1100 e70fd952 2024-03-22 stsp int packfd = -1, idxfd = -1;
1101 e70fd952 2024-03-22 stsp int pipe[2] = { -1, -1 };
1102 e70fd952 2024-03-22 stsp
1103 e70fd952 2024-03-22 stsp if (client->packfile_path) {
1104 e70fd952 2024-03-22 stsp return got_error_fmt(GOT_ERR_PRIVSEP_MSG,
1105 e70fd952 2024-03-22 stsp "uid %d already has a pack file", client->euid);
1106 e70fd952 2024-03-22 stsp }
1107 e70fd952 2024-03-22 stsp
1108 e70fd952 2024-03-22 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1)
1109 e70fd952 2024-03-22 stsp return got_error_from_errno("socketpair");
1110 e70fd952 2024-03-22 stsp
1111 e70fd952 2024-03-22 stsp /* Send pack pipe end 0 to repo child process. */
1112 e70fd952 2024-03-22 stsp if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1113 e70fd952 2024-03-22 stsp GOTD_IMSG_PACKFILE_PIPE, PROC_SESSION_WRITE, pipe[0],
1114 e70fd952 2024-03-22 stsp NULL, 0) == -1) {
1115 e70fd952 2024-03-22 stsp err = got_error_from_errno("imsg compose PACKFILE_PIPE");
1116 e70fd952 2024-03-22 stsp pipe[0] = -1;
1117 e70fd952 2024-03-22 stsp goto done;
1118 e70fd952 2024-03-22 stsp }
1119 e70fd952 2024-03-22 stsp pipe[0] = -1;
1120 e70fd952 2024-03-22 stsp
1121 e70fd952 2024-03-22 stsp /* Send pack pipe end 1 to gotsh(1) (expects just an fd, no data). */
1122 e70fd952 2024-03-22 stsp if (gotd_imsg_compose_event(&client->iev,
1123 e70fd952 2024-03-22 stsp GOTD_IMSG_PACKFILE_PIPE, PROC_SESSION_WRITE, pipe[1],
1124 e70fd952 2024-03-22 stsp NULL, 0) == -1)
1125 e70fd952 2024-03-22 stsp err = got_error_from_errno("imsg compose PACKFILE_PIPE");
1126 e70fd952 2024-03-22 stsp pipe[1] = -1;
1127 e70fd952 2024-03-22 stsp
1128 e70fd952 2024-03-22 stsp if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.pack",
1129 e70fd952 2024-03-22 stsp got_repo_get_path(gotd_session.repo), GOT_OBJECTS_PACK_DIR,
1130 e70fd952 2024-03-22 stsp client->euid) == -1) {
1131 e70fd952 2024-03-22 stsp err = got_error_from_errno("asprintf");
1132 e70fd952 2024-03-22 stsp goto done;
1133 e70fd952 2024-03-22 stsp }
1134 e70fd952 2024-03-22 stsp
1135 e70fd952 2024-03-22 stsp err = got_opentemp_named_fd(&pack_path, &packfd, basepath, "");
1136 e70fd952 2024-03-22 stsp if (err)
1137 e70fd952 2024-03-22 stsp goto done;
1138 e70fd952 2024-03-22 stsp if (fchmod(packfd, GOT_DEFAULT_PACK_MODE) == -1) {
1139 e70fd952 2024-03-22 stsp err = got_error_from_errno2("fchmod", pack_path);
1140 e70fd952 2024-03-22 stsp goto done;
1141 e70fd952 2024-03-22 stsp }
1142 e70fd952 2024-03-22 stsp
1143 e70fd952 2024-03-22 stsp free(basepath);
1144 e70fd952 2024-03-22 stsp if (asprintf(&basepath, "%s/%s/receiving-from-uid-%d.idx",
1145 e70fd952 2024-03-22 stsp got_repo_get_path(gotd_session.repo), GOT_OBJECTS_PACK_DIR,
1146 e70fd952 2024-03-22 stsp client->euid) == -1) {
1147 e70fd952 2024-03-22 stsp err = got_error_from_errno("asprintf");
1148 e70fd952 2024-03-22 stsp basepath = NULL;
1149 e70fd952 2024-03-22 stsp goto done;
1150 e70fd952 2024-03-22 stsp }
1151 e70fd952 2024-03-22 stsp err = got_opentemp_named_fd(&idx_path, &idxfd, basepath, "");
1152 e70fd952 2024-03-22 stsp if (err)
1153 e70fd952 2024-03-22 stsp goto done;
1154 e70fd952 2024-03-22 stsp if (fchmod(idxfd, GOT_DEFAULT_PACK_MODE) == -1) {
1155 e70fd952 2024-03-22 stsp err = got_error_from_errno2("fchmod", idx_path);
1156 e70fd952 2024-03-22 stsp goto done;
1157 e70fd952 2024-03-22 stsp }
1158 e70fd952 2024-03-22 stsp
1159 e70fd952 2024-03-22 stsp if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1160 e70fd952 2024-03-22 stsp GOTD_IMSG_PACKIDX_FILE, PROC_SESSION_WRITE,
1161 e70fd952 2024-03-22 stsp idxfd, NULL, 0) == -1) {
1162 e70fd952 2024-03-22 stsp err = got_error_from_errno("imsg compose PACKIDX_FILE");
1163 e70fd952 2024-03-22 stsp idxfd = -1;
1164 e70fd952 2024-03-22 stsp goto done;
1165 e70fd952 2024-03-22 stsp }
1166 e70fd952 2024-03-22 stsp idxfd = -1;
1167 e70fd952 2024-03-22 stsp
1168 e70fd952 2024-03-22 stsp memset(&ipack, 0, sizeof(ipack));
1169 e70fd952 2024-03-22 stsp if (client_has_capability(client, GOT_CAPA_REPORT_STATUS))
1170 e70fd952 2024-03-22 stsp ipack.report_status = 1;
1171 e70fd952 2024-03-22 stsp
1172 e70fd952 2024-03-22 stsp if (gotd_imsg_compose_event(&gotd_session.repo_child_iev,
1173 e70fd952 2024-03-22 stsp GOTD_IMSG_RECV_PACKFILE, PROC_SESSION_WRITE, packfd,
1174 e70fd952 2024-03-22 stsp &ipack, sizeof(ipack)) == -1) {
1175 e70fd952 2024-03-22 stsp err = got_error_from_errno("imsg compose RECV_PACKFILE");
1176 e70fd952 2024-03-22 stsp packfd = -1;
1177 e70fd952 2024-03-22 stsp goto done;
1178 e70fd952 2024-03-22 stsp }
1179 e70fd952 2024-03-22 stsp packfd = -1;
1180 e70fd952 2024-03-22 stsp
1181 e70fd952 2024-03-22 stsp done:
1182 e70fd952 2024-03-22 stsp free(basepath);
1183 e70fd952 2024-03-22 stsp if (pipe[0] != -1 && close(pipe[0]) == -1 && err == NULL)
1184 e70fd952 2024-03-22 stsp err = got_error_from_errno("close");
1185 e70fd952 2024-03-22 stsp if (pipe[1] != -1 && close(pipe[1]) == -1 && err == NULL)
1186 e70fd952 2024-03-22 stsp err = got_error_from_errno("close");
1187 e70fd952 2024-03-22 stsp if (packfd != -1 && close(packfd) == -1 && err == NULL)
1188 e70fd952 2024-03-22 stsp err = got_error_from_errno("close");
1189 e70fd952 2024-03-22 stsp if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
1190 e70fd952 2024-03-22 stsp err = got_error_from_errno("close");
1191 e70fd952 2024-03-22 stsp if (err) {
1192 e70fd952 2024-03-22 stsp free(pack_path);
1193 e70fd952 2024-03-22 stsp free(idx_path);
1194 e70fd952 2024-03-22 stsp } else {
1195 e70fd952 2024-03-22 stsp client->packfile_path = pack_path;
1196 e70fd952 2024-03-22 stsp client->packidx_path = idx_path;
1197 e70fd952 2024-03-22 stsp }
1198 e70fd952 2024-03-22 stsp return err;
1199 e70fd952 2024-03-22 stsp }
1200 e70fd952 2024-03-22 stsp
1201 e70fd952 2024-03-22 stsp static void
1202 e70fd952 2024-03-22 stsp session_dispatch_client(int fd, short events, void *arg)
1203 e70fd952 2024-03-22 stsp {
1204 e70fd952 2024-03-22 stsp struct gotd_imsgev *iev = arg;
1205 e70fd952 2024-03-22 stsp struct imsgbuf *ibuf = &iev->ibuf;
1206 e70fd952 2024-03-22 stsp struct gotd_session_client *client = &gotd_session_client;
1207 e70fd952 2024-03-22 stsp const struct got_error *err = NULL;
1208 e70fd952 2024-03-22 stsp struct imsg imsg;
1209 e70fd952 2024-03-22 stsp ssize_t n;
1210 e70fd952 2024-03-22 stsp
1211 e70fd952 2024-03-22 stsp if (events & EV_WRITE) {
1212 e70fd952 2024-03-22 stsp while (ibuf->w.queued) {
1213 e70fd952 2024-03-22 stsp n = msgbuf_write(&ibuf->w);
1214 e70fd952 2024-03-22 stsp if (n == -1 && errno == EPIPE) {
1215 e70fd952 2024-03-22 stsp /*
1216 e70fd952 2024-03-22 stsp * The client has closed its socket.
1217 e70fd952 2024-03-22 stsp * This can happen when Git clients are
1218 e70fd952 2024-03-22 stsp * done sending pack file data.
1219 e70fd952 2024-03-22 stsp */
1220 e70fd952 2024-03-22 stsp msgbuf_clear(&ibuf->w);
1221 e70fd952 2024-03-22 stsp continue;
1222 e70fd952 2024-03-22 stsp } else if (n == -1 && errno != EAGAIN) {
1223 e70fd952 2024-03-22 stsp err = got_error_from_errno("imsg_flush");
1224 e70fd952 2024-03-22 stsp disconnect_on_error(client, err);
1225 e70fd952 2024-03-22 stsp return;
1226 e70fd952 2024-03-22 stsp }
1227 e70fd952 2024-03-22 stsp if (n == 0) {
1228 e70fd952 2024-03-22 stsp /* Connection closed. */
1229 e70fd952 2024-03-22 stsp err = got_error(GOT_ERR_EOF);
1230 e70fd952 2024-03-22 stsp disconnect_on_error(client, err);
1231 e70fd952 2024-03-22 stsp return;
1232 e70fd952 2024-03-22 stsp }
1233 e70fd952 2024-03-22 stsp }
1234 e70fd952 2024-03-22 stsp
1235 e70fd952 2024-03-22 stsp if (client->flush_disconnect) {
1236 e70fd952 2024-03-22 stsp disconnect(client);
1237 e70fd952 2024-03-22 stsp return;
1238 e70fd952 2024-03-22 stsp }
1239 e70fd952 2024-03-22 stsp }
1240 e70fd952 2024-03-22 stsp
1241 e70fd952 2024-03-22 stsp if ((events & EV_READ) == 0)
1242 e70fd952 2024-03-22 stsp return;
1243 e70fd952 2024-03-22 stsp
1244 e70fd952 2024-03-22 stsp memset(&imsg, 0, sizeof(imsg));
1245 e70fd952 2024-03-22 stsp
1246 e70fd952 2024-03-22 stsp while (err == NULL) {
1247 e70fd952 2024-03-22 stsp err = gotd_imsg_recv(&imsg, ibuf, 0);
1248 e70fd952 2024-03-22 stsp if (err) {
1249 e70fd952 2024-03-22 stsp if (err->code == GOT_ERR_PRIVSEP_READ)
1250 e70fd952 2024-03-22 stsp err = NULL;
1251 e70fd952 2024-03-22 stsp else if (err->code == GOT_ERR_EOF &&
1252 e70fd952 2024-03-22 stsp gotd_session.state ==
1253 e70fd952 2024-03-22 stsp GOTD_STATE_EXPECT_CAPABILITIES) {
1254 e70fd952 2024-03-22 stsp /*
1255 e70fd952 2024-03-22 stsp * The client has closed its socket before
1256 e70fd952 2024-03-22 stsp * sending its capability announcement.
1257 e70fd952 2024-03-22 stsp * This can happen when Git clients have
1258 e70fd952 2024-03-22 stsp * no ref-updates to send.
1259 e70fd952 2024-03-22 stsp */
1260 e70fd952 2024-03-22 stsp disconnect_on_error(client, err);
1261 e70fd952 2024-03-22 stsp return;
1262 e70fd952 2024-03-22 stsp }
1263 e70fd952 2024-03-22 stsp break;
1264 e70fd952 2024-03-22 stsp }
1265 e70fd952 2024-03-22 stsp
1266 e70fd952 2024-03-22 stsp evtimer_del(&client->tmo);
1267 e70fd952 2024-03-22 stsp
1268 e70fd952 2024-03-22 stsp switch (imsg.hdr.type) {
1269 e70fd952 2024-03-22 stsp case GOTD_IMSG_CAPABILITIES:
1270 e70fd952 2024-03-22 stsp if (gotd_session.state !=
1271 e70fd952 2024-03-22 stsp GOTD_STATE_EXPECT_CAPABILITIES) {
1272 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1273 e70fd952 2024-03-22 stsp "unexpected capabilities received");
1274 e70fd952 2024-03-22 stsp break;
1275 e70fd952 2024-03-22 stsp }
1276 e70fd952 2024-03-22 stsp log_debug("receiving capabilities from uid %d",
1277 e70fd952 2024-03-22 stsp client->euid);
1278 e70fd952 2024-03-22 stsp err = recv_capabilities(client, &imsg);
1279 e70fd952 2024-03-22 stsp break;
1280 e70fd952 2024-03-22 stsp case GOTD_IMSG_CAPABILITY:
1281 e70fd952 2024-03-22 stsp if (gotd_session.state != GOTD_STATE_EXPECT_CAPABILITIES) {
1282 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1283 e70fd952 2024-03-22 stsp "unexpected capability received");
1284 e70fd952 2024-03-22 stsp break;
1285 e70fd952 2024-03-22 stsp }
1286 e70fd952 2024-03-22 stsp err = recv_capability(client, &imsg);
1287 e70fd952 2024-03-22 stsp if (err || client->ncapabilities < client->ncapa_alloc)
1288 e70fd952 2024-03-22 stsp break;
1289 e70fd952 2024-03-22 stsp gotd_session.state = GOTD_STATE_EXPECT_REF_UPDATE;
1290 e70fd952 2024-03-22 stsp client->accept_flush_pkt = 1;
1291 e70fd952 2024-03-22 stsp log_debug("uid %d: expecting ref-update-lines",
1292 e70fd952 2024-03-22 stsp client->euid);
1293 e70fd952 2024-03-22 stsp break;
1294 e70fd952 2024-03-22 stsp case GOTD_IMSG_REF_UPDATE:
1295 e70fd952 2024-03-22 stsp if (gotd_session.state != GOTD_STATE_EXPECT_REF_UPDATE &&
1296 e70fd952 2024-03-22 stsp gotd_session.state !=
1297 e70fd952 2024-03-22 stsp GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1298 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1299 e70fd952 2024-03-22 stsp "unexpected ref-update-line received");
1300 e70fd952 2024-03-22 stsp break;
1301 e70fd952 2024-03-22 stsp }
1302 e70fd952 2024-03-22 stsp log_debug("received ref-update-line from uid %d",
1303 e70fd952 2024-03-22 stsp client->euid);
1304 e70fd952 2024-03-22 stsp err = forward_ref_update(client, &imsg);
1305 e70fd952 2024-03-22 stsp if (err)
1306 e70fd952 2024-03-22 stsp break;
1307 e70fd952 2024-03-22 stsp gotd_session.state = GOTD_STATE_EXPECT_MORE_REF_UPDATES;
1308 e70fd952 2024-03-22 stsp client->accept_flush_pkt = 1;
1309 e70fd952 2024-03-22 stsp break;
1310 e70fd952 2024-03-22 stsp case GOTD_IMSG_FLUSH:
1311 e70fd952 2024-03-22 stsp if (gotd_session.state !=
1312 e70fd952 2024-03-22 stsp GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1313 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1314 e70fd952 2024-03-22 stsp "unexpected flush-pkt received");
1315 e70fd952 2024-03-22 stsp break;
1316 e70fd952 2024-03-22 stsp }
1317 e70fd952 2024-03-22 stsp if (!client->accept_flush_pkt) {
1318 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1319 e70fd952 2024-03-22 stsp "unexpected flush-pkt received");
1320 e70fd952 2024-03-22 stsp break;
1321 e70fd952 2024-03-22 stsp }
1322 e70fd952 2024-03-22 stsp
1323 e70fd952 2024-03-22 stsp /*
1324 e70fd952 2024-03-22 stsp * Accept just one flush packet at a time.
1325 e70fd952 2024-03-22 stsp * Future client state transitions will set this flag
1326 e70fd952 2024-03-22 stsp * again if another flush packet is expected.
1327 e70fd952 2024-03-22 stsp */
1328 e70fd952 2024-03-22 stsp client->accept_flush_pkt = 0;
1329 e70fd952 2024-03-22 stsp
1330 e70fd952 2024-03-22 stsp log_debug("received flush-pkt from uid %d",
1331 e70fd952 2024-03-22 stsp client->euid);
1332 e70fd952 2024-03-22 stsp if (gotd_session.state ==
1333 e70fd952 2024-03-22 stsp GOTD_STATE_EXPECT_MORE_REF_UPDATES) {
1334 e70fd952 2024-03-22 stsp gotd_session.state = GOTD_STATE_EXPECT_PACKFILE;
1335 e70fd952 2024-03-22 stsp log_debug("uid %d: expecting packfile",
1336 e70fd952 2024-03-22 stsp client->euid);
1337 e70fd952 2024-03-22 stsp err = recv_packfile(client);
1338 e70fd952 2024-03-22 stsp } else {
1339 e70fd952 2024-03-22 stsp /* should not happen, see above */
1340 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_BAD_REQUEST,
1341 e70fd952 2024-03-22 stsp "unexpected client state");
1342 e70fd952 2024-03-22 stsp break;
1343 e70fd952 2024-03-22 stsp }
1344 e70fd952 2024-03-22 stsp break;
1345 e70fd952 2024-03-22 stsp default:
1346 e70fd952 2024-03-22 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1347 e70fd952 2024-03-22 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1348 e70fd952 2024-03-22 stsp break;
1349 e70fd952 2024-03-22 stsp }
1350 e70fd952 2024-03-22 stsp
1351 e70fd952 2024-03-22 stsp imsg_free(&imsg);
1352 e70fd952 2024-03-22 stsp }
1353 e70fd952 2024-03-22 stsp
1354 e70fd952 2024-03-22 stsp if (err) {
1355 e70fd952 2024-03-22 stsp if (err->code != GOT_ERR_EOF ||
1356 c3cc85a3 2024-04-30 stsp (gotd_session.state != GOTD_STATE_EXPECT_PACKFILE &&
1357 c3cc85a3 2024-04-30 stsp gotd_session.state != GOTD_STATE_NOTIFY))
1358 e70fd952 2024-03-22 stsp disconnect_on_error(client, err);
1359 e70fd952 2024-03-22 stsp } else {
1360 e70fd952 2024-03-22 stsp gotd_imsg_event_add(iev);
1361 e70fd952 2024-03-22 stsp evtimer_add(&client->tmo, &gotd_session.request_timeout);
1362 e70fd952 2024-03-22 stsp }
1363 e70fd952 2024-03-22 stsp }
1364 e70fd952 2024-03-22 stsp
1365 e70fd952 2024-03-22 stsp static const struct got_error *
1366 e70fd952 2024-03-22 stsp list_refs_request(void)
1367 e70fd952 2024-03-22 stsp {
1368 e70fd952 2024-03-22 stsp static const struct got_error *err;
1369 e70fd952 2024-03-22 stsp struct gotd_session_client *client = &gotd_session_client;
1370 e70fd952 2024-03-22 stsp struct gotd_imsgev *iev = &gotd_session.repo_child_iev;
1371 e70fd952 2024-03-22 stsp int fd;
1372 e70fd952 2024-03-22 stsp
1373 e70fd952 2024-03-22 stsp if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
1374 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1375 e70fd952 2024-03-22 stsp
1376 e70fd952 2024-03-22 stsp fd = dup(client->fd);
1377 e70fd952 2024-03-22 stsp if (fd == -1)
1378 e70fd952 2024-03-22 stsp return got_error_from_errno("dup");
1379 e70fd952 2024-03-22 stsp
1380 e70fd952 2024-03-22 stsp if (gotd_imsg_compose_event(iev, GOTD_IMSG_LIST_REFS_INTERNAL,
1381 e70fd952 2024-03-22 stsp PROC_SESSION_WRITE, fd, NULL, 0) == -1) {
1382 e70fd952 2024-03-22 stsp err = got_error_from_errno("imsg compose LIST_REFS_INTERNAL");
1383 e70fd952 2024-03-22 stsp close(fd);
1384 e70fd952 2024-03-22 stsp return err;
1385 e70fd952 2024-03-22 stsp }
1386 e70fd952 2024-03-22 stsp
1387 e70fd952 2024-03-22 stsp gotd_session.state = GOTD_STATE_EXPECT_CAPABILITIES;
1388 e70fd952 2024-03-22 stsp log_debug("uid %d: expecting capabilities", client->euid);
1389 e70fd952 2024-03-22 stsp return NULL;
1390 e70fd952 2024-03-22 stsp }
1391 e70fd952 2024-03-22 stsp
1392 e70fd952 2024-03-22 stsp static const struct got_error *
1393 e70fd952 2024-03-22 stsp recv_connect(struct imsg *imsg)
1394 e70fd952 2024-03-22 stsp {
1395 e70fd952 2024-03-22 stsp struct gotd_session_client *client = &gotd_session_client;
1396 e70fd952 2024-03-22 stsp struct gotd_imsg_connect iconnect;
1397 e70fd952 2024-03-22 stsp size_t datalen;
1398 e70fd952 2024-03-22 stsp
1399 e70fd952 2024-03-22 stsp if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
1400 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1401 e70fd952 2024-03-22 stsp
1402 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1403 e70fd952 2024-03-22 stsp if (datalen < sizeof(iconnect))
1404 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1405 e70fd952 2024-03-22 stsp memcpy(&iconnect, imsg->data, sizeof(iconnect));
1406 e70fd952 2024-03-22 stsp if (iconnect.username_len == 0 ||
1407 e70fd952 2024-03-22 stsp datalen != sizeof(iconnect) + iconnect.username_len)
1408 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1409 e70fd952 2024-03-22 stsp
1410 e70fd952 2024-03-22 stsp client->euid = iconnect.euid;
1411 e70fd952 2024-03-22 stsp client->egid = iconnect.egid;
1412 e70fd952 2024-03-22 stsp client->fd = imsg_get_fd(imsg);
1413 e70fd952 2024-03-22 stsp if (client->fd == -1)
1414 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
1415 e70fd952 2024-03-22 stsp
1416 e70fd952 2024-03-22 stsp client->username = strndup(imsg->data + sizeof(iconnect),
1417 e70fd952 2024-03-22 stsp iconnect.username_len);
1418 e70fd952 2024-03-22 stsp if (client->username == NULL)
1419 e70fd952 2024-03-22 stsp return got_error_from_errno("strndup");
1420 e70fd952 2024-03-22 stsp
1421 e70fd952 2024-03-22 stsp imsg_init(&client->iev.ibuf, client->fd);
1422 e70fd952 2024-03-22 stsp client->iev.handler = session_dispatch_client;
1423 e70fd952 2024-03-22 stsp client->iev.events = EV_READ;
1424 e70fd952 2024-03-22 stsp client->iev.handler_arg = NULL;
1425 e70fd952 2024-03-22 stsp event_set(&client->iev.ev, client->iev.ibuf.fd, EV_READ,
1426 e70fd952 2024-03-22 stsp session_dispatch_client, &client->iev);
1427 e70fd952 2024-03-22 stsp gotd_imsg_event_add(&client->iev);
1428 e70fd952 2024-03-22 stsp evtimer_set(&client->tmo, gotd_request_timeout, client);
1429 caa6cf11 2024-04-30 stsp evtimer_add(&client->tmo, &gotd_session.request_timeout);
1430 e70fd952 2024-03-22 stsp
1431 e70fd952 2024-03-22 stsp return NULL;
1432 e70fd952 2024-03-22 stsp }
1433 e70fd952 2024-03-22 stsp
1434 e70fd952 2024-03-22 stsp static void
1435 e70fd952 2024-03-22 stsp session_dispatch_notifier(int fd, short event, void *arg)
1436 e70fd952 2024-03-22 stsp {
1437 e70fd952 2024-03-22 stsp const struct got_error *err;
1438 e70fd952 2024-03-22 stsp struct gotd_session_client *client = &gotd_session_client;
1439 e70fd952 2024-03-22 stsp struct gotd_imsgev *iev = arg;
1440 e70fd952 2024-03-22 stsp struct imsgbuf *ibuf = &iev->ibuf;
1441 e70fd952 2024-03-22 stsp ssize_t n;
1442 e70fd952 2024-03-22 stsp int shut = 0;
1443 e70fd952 2024-03-22 stsp struct imsg imsg;
1444 e70fd952 2024-03-22 stsp struct gotd_session_notif *notif;
1445 e70fd952 2024-03-22 stsp
1446 e70fd952 2024-03-22 stsp if (event & EV_READ) {
1447 e70fd952 2024-03-22 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1448 e70fd952 2024-03-22 stsp fatal("imsg_read error");
1449 e70fd952 2024-03-22 stsp if (n == 0) {
1450 e70fd952 2024-03-22 stsp /* Connection closed. */
1451 e70fd952 2024-03-22 stsp shut = 1;
1452 e70fd952 2024-03-22 stsp goto done;
1453 e70fd952 2024-03-22 stsp }
1454 e70fd952 2024-03-22 stsp }
1455 e70fd952 2024-03-22 stsp
1456 e70fd952 2024-03-22 stsp if (event & EV_WRITE) {
1457 e70fd952 2024-03-22 stsp n = msgbuf_write(&ibuf->w);
1458 e70fd952 2024-03-22 stsp if (n == -1 && errno != EAGAIN)
1459 e70fd952 2024-03-22 stsp fatal("msgbuf_write");
1460 e70fd952 2024-03-22 stsp if (n == 0) {
1461 e70fd952 2024-03-22 stsp /* Connection closed. */
1462 e70fd952 2024-03-22 stsp shut = 1;
1463 e70fd952 2024-03-22 stsp goto done;
1464 e70fd952 2024-03-22 stsp }
1465 e70fd952 2024-03-22 stsp }
1466 e70fd952 2024-03-22 stsp
1467 e70fd952 2024-03-22 stsp for (;;) {
1468 e70fd952 2024-03-22 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
1469 e70fd952 2024-03-22 stsp fatal("%s: imsg_get error", __func__);
1470 e70fd952 2024-03-22 stsp if (n == 0) /* No more messages. */
1471 e70fd952 2024-03-22 stsp break;
1472 e70fd952 2024-03-22 stsp
1473 e70fd952 2024-03-22 stsp switch (imsg.hdr.type) {
1474 e70fd952 2024-03-22 stsp case GOTD_IMSG_NOTIFICATION_SENT:
1475 e70fd952 2024-03-22 stsp if (gotd_session.state != GOTD_STATE_NOTIFY) {
1476 e70fd952 2024-03-22 stsp log_warn("unexpected imsg %d", imsg.hdr.type);
1477 e70fd952 2024-03-22 stsp break;
1478 e70fd952 2024-03-22 stsp }
1479 e70fd952 2024-03-22 stsp notif = STAILQ_FIRST(&notifications);
1480 e70fd952 2024-03-22 stsp if (notif == NULL) {
1481 e70fd952 2024-03-22 stsp disconnect(client);
1482 e70fd952 2024-03-22 stsp break; /* NOTREACHED */
1483 e70fd952 2024-03-22 stsp }
1484 e70fd952 2024-03-22 stsp /* Request content for the next notification. */
1485 e70fd952 2024-03-22 stsp err = request_notification(notif);
1486 e70fd952 2024-03-22 stsp if (err) {
1487 e70fd952 2024-03-22 stsp log_warn("could not send notification: %s",
1488 e70fd952 2024-03-22 stsp err->msg);
1489 e70fd952 2024-03-22 stsp disconnect(client);
1490 e70fd952 2024-03-22 stsp }
1491 e70fd952 2024-03-22 stsp break;
1492 e70fd952 2024-03-22 stsp default:
1493 e70fd952 2024-03-22 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1494 e70fd952 2024-03-22 stsp break;
1495 e70fd952 2024-03-22 stsp }
1496 e70fd952 2024-03-22 stsp
1497 e70fd952 2024-03-22 stsp imsg_free(&imsg);
1498 e70fd952 2024-03-22 stsp }
1499 e70fd952 2024-03-22 stsp done:
1500 e70fd952 2024-03-22 stsp if (!shut) {
1501 e70fd952 2024-03-22 stsp gotd_imsg_event_add(iev);
1502 e70fd952 2024-03-22 stsp } else {
1503 e70fd952 2024-03-22 stsp /* This pipe is dead. Remove its event handler */
1504 e70fd952 2024-03-22 stsp event_del(&iev->ev);
1505 e70fd952 2024-03-22 stsp imsg_clear(&iev->ibuf);
1506 e70fd952 2024-03-22 stsp imsg_init(&iev->ibuf, -1);
1507 e70fd952 2024-03-22 stsp }
1508 e70fd952 2024-03-22 stsp }
1509 e70fd952 2024-03-22 stsp
1510 e70fd952 2024-03-22 stsp static const struct got_error *
1511 e70fd952 2024-03-22 stsp recv_notifier(struct imsg *imsg)
1512 e70fd952 2024-03-22 stsp {
1513 e70fd952 2024-03-22 stsp struct gotd_imsgev *iev = &gotd_session.notifier_iev;
1514 e70fd952 2024-03-22 stsp struct gotd_session_client *client = &gotd_session_client;
1515 e70fd952 2024-03-22 stsp size_t datalen;
1516 e70fd952 2024-03-22 stsp int fd;
1517 e70fd952 2024-03-22 stsp
1518 e70fd952 2024-03-22 stsp if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
1519 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1520 e70fd952 2024-03-22 stsp
1521 e70fd952 2024-03-22 stsp /* We should already have received a pipe to the listener. */
1522 e70fd952 2024-03-22 stsp if (client->fd == -1)
1523 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1524 e70fd952 2024-03-22 stsp
1525 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1526 e70fd952 2024-03-22 stsp if (datalen != 0)
1527 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1528 e70fd952 2024-03-22 stsp
1529 e70fd952 2024-03-22 stsp fd = imsg_get_fd(imsg);
1530 e70fd952 2024-03-22 stsp if (fd == -1)
1531 e70fd952 2024-03-22 stsp return NULL; /* notifications unused */
1532 e70fd952 2024-03-22 stsp
1533 e70fd952 2024-03-22 stsp imsg_init(&iev->ibuf, fd);
1534 e70fd952 2024-03-22 stsp iev->handler = session_dispatch_notifier;
1535 e70fd952 2024-03-22 stsp iev->events = EV_READ;
1536 e70fd952 2024-03-22 stsp iev->handler_arg = NULL;
1537 e70fd952 2024-03-22 stsp event_set(&iev->ev, iev->ibuf.fd, EV_READ,
1538 e70fd952 2024-03-22 stsp session_dispatch_notifier, iev);
1539 e70fd952 2024-03-22 stsp gotd_imsg_event_add(iev);
1540 e70fd952 2024-03-22 stsp
1541 e70fd952 2024-03-22 stsp return NULL;
1542 e70fd952 2024-03-22 stsp }
1543 e70fd952 2024-03-22 stsp
1544 e70fd952 2024-03-22 stsp static const struct got_error *
1545 e70fd952 2024-03-22 stsp recv_repo_child(struct imsg *imsg)
1546 e70fd952 2024-03-22 stsp {
1547 e70fd952 2024-03-22 stsp struct gotd_imsg_connect_repo_child ichild;
1548 e70fd952 2024-03-22 stsp struct gotd_session_client *client = &gotd_session_client;
1549 e70fd952 2024-03-22 stsp size_t datalen;
1550 e70fd952 2024-03-22 stsp int fd;
1551 e70fd952 2024-03-22 stsp
1552 e70fd952 2024-03-22 stsp if (gotd_session.state != GOTD_STATE_EXPECT_LIST_REFS)
1553 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1554 e70fd952 2024-03-22 stsp
1555 e70fd952 2024-03-22 stsp /* We should already have received a pipe to the listener. */
1556 e70fd952 2024-03-22 stsp if (client->fd == -1)
1557 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
1558 e70fd952 2024-03-22 stsp
1559 e70fd952 2024-03-22 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1560 e70fd952 2024-03-22 stsp if (datalen != sizeof(ichild))
1561 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1562 e70fd952 2024-03-22 stsp
1563 e70fd952 2024-03-22 stsp memcpy(&ichild, imsg->data, sizeof(ichild));
1564 e70fd952 2024-03-22 stsp
1565 e70fd952 2024-03-22 stsp if (ichild.proc_id != PROC_REPO_WRITE)
1566 e70fd952 2024-03-22 stsp return got_error_msg(GOT_ERR_PRIVSEP_MSG,
1567 e70fd952 2024-03-22 stsp "bad child process type");
1568 e70fd952 2024-03-22 stsp
1569 e70fd952 2024-03-22 stsp fd = imsg_get_fd(imsg);
1570 e70fd952 2024-03-22 stsp if (fd == -1)
1571 e70fd952 2024-03-22 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
1572 e70fd952 2024-03-22 stsp
1573 e70fd952 2024-03-22 stsp imsg_init(&gotd_session.repo_child_iev.ibuf, fd);
1574 e70fd952 2024-03-22 stsp gotd_session.repo_child_iev.handler = session_dispatch_repo_child;
1575 e70fd952 2024-03-22 stsp gotd_session.repo_child_iev.events = EV_READ;
1576 e70fd952 2024-03-22 stsp gotd_session.repo_child_iev.handler_arg = NULL;
1577 e70fd952 2024-03-22 stsp event_set(&gotd_session.repo_child_iev.ev,
1578 e70fd952 2024-03-22 stsp gotd_session.repo_child_iev.ibuf.fd, EV_READ,
1579 e70fd952 2024-03-22 stsp session_dispatch_repo_child, &gotd_session.repo_child_iev);
1580 e70fd952 2024-03-22 stsp gotd_imsg_event_add(&gotd_session.repo_child_iev);
1581 e70fd952 2024-03-22 stsp
1582 e70fd952 2024-03-22 stsp /* The "recvfd" pledge promise is no longer needed. */
1583 e70fd952 2024-03-22 stsp if (pledge("stdio rpath wpath cpath sendfd fattr flock", NULL) == -1)
1584 e70fd952 2024-03-22 stsp fatal("pledge");
1585 e70fd952 2024-03-22 stsp
1586 e70fd952 2024-03-22 stsp return NULL;
1587 e70fd952 2024-03-22 stsp }
1588 e70fd952 2024-03-22 stsp
1589 e70fd952 2024-03-22 stsp static void
1590 e70fd952 2024-03-22 stsp session_dispatch(int fd, short event, void *arg)
1591 e70fd952 2024-03-22 stsp {
1592 e70fd952 2024-03-22 stsp struct gotd_imsgev *iev = arg;
1593 e70fd952 2024-03-22 stsp struct imsgbuf *ibuf = &iev->ibuf;
1594 e70fd952 2024-03-22 stsp struct gotd_session_client *client = &gotd_session_client;
1595 e70fd952 2024-03-22 stsp ssize_t n;
1596 e70fd952 2024-03-22 stsp int shut = 0;
1597 e70fd952 2024-03-22 stsp struct imsg imsg;
1598 e70fd952 2024-03-22 stsp
1599 e70fd952 2024-03-22 stsp if (event & EV_READ) {
1600 e70fd952 2024-03-22 stsp if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
1601 e70fd952 2024-03-22 stsp fatal("imsg_read error");
1602 e70fd952 2024-03-22 stsp if (n == 0) {
1603 e70fd952 2024-03-22 stsp /* Connection closed. */
1604 e70fd952 2024-03-22 stsp shut = 1;
1605 e70fd952 2024-03-22 stsp goto done;
1606 e70fd952 2024-03-22 stsp }
1607 e70fd952 2024-03-22 stsp }
1608 e70fd952 2024-03-22 stsp
1609 e70fd952 2024-03-22 stsp if (event & EV_WRITE) {
1610 e70fd952 2024-03-22 stsp n = msgbuf_write(&ibuf->w);
1611 e70fd952 2024-03-22 stsp if (n == -1 && errno != EAGAIN)
1612 e70fd952 2024-03-22 stsp fatal("msgbuf_write");
1613 e70fd952 2024-03-22 stsp if (n == 0) {
1614 e70fd952 2024-03-22 stsp /* Connection closed. */
1615 e70fd952 2024-03-22 stsp shut = 1;
1616 e70fd952 2024-03-22 stsp goto done;
1617 e70fd952 2024-03-22 stsp }
1618 e70fd952 2024-03-22 stsp }
1619 e70fd952 2024-03-22 stsp
1620 e70fd952 2024-03-22 stsp for (;;) {
1621 e70fd952 2024-03-22 stsp const struct got_error *err = NULL;
1622 e70fd952 2024-03-22 stsp uint32_t client_id = 0;
1623 e70fd952 2024-03-22 stsp int do_disconnect = 0, do_list_refs = 0;
1624 e70fd952 2024-03-22 stsp
1625 e70fd952 2024-03-22 stsp if ((n = imsg_get(ibuf, &imsg)) == -1)
1626 e70fd952 2024-03-22 stsp fatal("%s: imsg_get error", __func__);
1627 e70fd952 2024-03-22 stsp if (n == 0) /* No more messages. */
1628 e70fd952 2024-03-22 stsp break;
1629 e70fd952 2024-03-22 stsp
1630 e70fd952 2024-03-22 stsp switch (imsg.hdr.type) {
1631 e70fd952 2024-03-22 stsp case GOTD_IMSG_ERROR:
1632 e70fd952 2024-03-22 stsp do_disconnect = 1;
1633 e70fd952 2024-03-22 stsp err = gotd_imsg_recv_error(&client_id, &imsg);
1634 e70fd952 2024-03-22 stsp break;
1635 e70fd952 2024-03-22 stsp case GOTD_IMSG_CONNECT:
1636 e70fd952 2024-03-22 stsp err = recv_connect(&imsg);
1637 e70fd952 2024-03-22 stsp break;
1638 e70fd952 2024-03-22 stsp case GOTD_IMSG_DISCONNECT:
1639 e70fd952 2024-03-22 stsp do_disconnect = 1;
1640 e70fd952 2024-03-22 stsp break;
1641 e70fd952 2024-03-22 stsp case GOTD_IMSG_CONNECT_NOTIFIER:
1642 e70fd952 2024-03-22 stsp err = recv_notifier(&imsg);
1643 e70fd952 2024-03-22 stsp break;
1644 e70fd952 2024-03-22 stsp case GOTD_IMSG_CONNECT_REPO_CHILD:
1645 e70fd952 2024-03-22 stsp err = recv_repo_child(&imsg);
1646 e70fd952 2024-03-22 stsp if (err)
1647 e70fd952 2024-03-22 stsp break;
1648 e70fd952 2024-03-22 stsp do_list_refs = 1;
1649 e70fd952 2024-03-22 stsp break;
1650 e70fd952 2024-03-22 stsp default:
1651 e70fd952 2024-03-22 stsp log_debug("unexpected imsg %d", imsg.hdr.type);
1652 e70fd952 2024-03-22 stsp break;
1653 e70fd952 2024-03-22 stsp }
1654 e70fd952 2024-03-22 stsp imsg_free(&imsg);
1655 e70fd952 2024-03-22 stsp
1656 e70fd952 2024-03-22 stsp if (do_disconnect) {
1657 e70fd952 2024-03-22 stsp if (err)
1658 e70fd952 2024-03-22 stsp disconnect_on_error(client, err);
1659 e70fd952 2024-03-22 stsp else
1660 e70fd952 2024-03-22 stsp disconnect(client);
1661 e70fd952 2024-03-22 stsp } else if (do_list_refs)
1662 e70fd952 2024-03-22 stsp err = list_refs_request();
1663 e70fd952 2024-03-22 stsp
1664 e70fd952 2024-03-22 stsp if (err)
1665 e70fd952 2024-03-22 stsp log_warnx("uid %d: %s", client->euid, err->msg);
1666 e70fd952 2024-03-22 stsp }
1667 e70fd952 2024-03-22 stsp done:
1668 e70fd952 2024-03-22 stsp if (!shut) {
1669 e70fd952 2024-03-22 stsp gotd_imsg_event_add(iev);
1670 e70fd952 2024-03-22 stsp } else {
1671 e70fd952 2024-03-22 stsp /* This pipe is dead. Remove its event handler */
1672 e70fd952 2024-03-22 stsp event_del(&iev->ev);
1673 e70fd952 2024-03-22 stsp event_loopexit(NULL);
1674 e70fd952 2024-03-22 stsp }
1675 e70fd952 2024-03-22 stsp }
1676 e70fd952 2024-03-22 stsp
1677 e70fd952 2024-03-22 stsp void
1678 e70fd952 2024-03-22 stsp session_write_main(const char *title, const char *repo_path,
1679 e70fd952 2024-03-22 stsp int *pack_fds, int *temp_fds, struct timeval *request_timeout,
1680 e70fd952 2024-03-22 stsp struct gotd_repo *repo_cfg)
1681 e70fd952 2024-03-22 stsp {
1682 e70fd952 2024-03-22 stsp const struct got_error *err = NULL;
1683 e70fd952 2024-03-22 stsp struct event evsigint, evsigterm, evsighup, evsigusr1;
1684 e70fd952 2024-03-22 stsp
1685 e70fd952 2024-03-22 stsp STAILQ_INIT(&notifications);
1686 e70fd952 2024-03-22 stsp
1687 e70fd952 2024-03-22 stsp gotd_session.title = title;
1688 e70fd952 2024-03-22 stsp gotd_session.pid = getpid();
1689 e70fd952 2024-03-22 stsp gotd_session.pack_fds = pack_fds;
1690 e70fd952 2024-03-22 stsp gotd_session.temp_fds = temp_fds;
1691 e70fd952 2024-03-22 stsp memcpy(&gotd_session.request_timeout, request_timeout,
1692 e70fd952 2024-03-22 stsp sizeof(gotd_session.request_timeout));
1693 e70fd952 2024-03-22 stsp gotd_session.repo_cfg = repo_cfg;
1694 e70fd952 2024-03-22 stsp
1695 e70fd952 2024-03-22 stsp imsg_init(&gotd_session.notifier_iev.ibuf, -1);
1696 e70fd952 2024-03-22 stsp
1697 e70fd952 2024-03-22 stsp err = got_repo_open(&gotd_session.repo, repo_path, NULL, pack_fds);
1698 e70fd952 2024-03-22 stsp if (err)
1699 e70fd952 2024-03-22 stsp goto done;
1700 e70fd952 2024-03-22 stsp if (!got_repo_is_bare(gotd_session.repo)) {
1701 e70fd952 2024-03-22 stsp err = got_error_msg(GOT_ERR_NOT_GIT_REPO,
1702 e70fd952 2024-03-22 stsp "bare git repository required");
1703 e70fd952 2024-03-22 stsp goto done;
1704 e70fd952 2024-03-22 stsp }
1705 c214ca4f 2024-08-01 op if (got_repo_get_object_format(gotd_session.repo) != GOT_HASH_SHA1) {
1706 c214ca4f 2024-08-01 op err = got_error_msg(GOT_ERR_NOT_IMPL,
1707 c214ca4f 2024-08-01 op "sha256 object IDs unsupported in network protocol");
1708 c214ca4f 2024-08-01 op goto done;
1709 c214ca4f 2024-08-01 op }
1710 e70fd952 2024-03-22 stsp
1711 e70fd952 2024-03-22 stsp got_repo_temp_fds_set(gotd_session.repo, temp_fds);
1712 e70fd952 2024-03-22 stsp
1713 e70fd952 2024-03-22 stsp signal_set(&evsigint, SIGINT, session_write_sighdlr, NULL);
1714 e70fd952 2024-03-22 stsp signal_set(&evsigterm, SIGTERM, session_write_sighdlr, NULL);
1715 e70fd952 2024-03-22 stsp signal_set(&evsighup, SIGHUP, session_write_sighdlr, NULL);
1716 e70fd952 2024-03-22 stsp signal_set(&evsigusr1, SIGUSR1, session_write_sighdlr, NULL);
1717 e70fd952 2024-03-22 stsp signal(SIGPIPE, SIG_IGN);
1718 e70fd952 2024-03-22 stsp
1719 e70fd952 2024-03-22 stsp signal_add(&evsigint, NULL);
1720 e70fd952 2024-03-22 stsp signal_add(&evsigterm, NULL);
1721 e70fd952 2024-03-22 stsp signal_add(&evsighup, NULL);
1722 e70fd952 2024-03-22 stsp signal_add(&evsigusr1, NULL);
1723 e70fd952 2024-03-22 stsp
1724 e70fd952 2024-03-22 stsp gotd_session.state = GOTD_STATE_EXPECT_LIST_REFS;
1725 e70fd952 2024-03-22 stsp
1726 e70fd952 2024-03-22 stsp gotd_session_client.fd = -1;
1727 e70fd952 2024-03-22 stsp gotd_session_client.nref_updates = -1;
1728 e70fd952 2024-03-22 stsp gotd_session_client.delta_cache_fd = -1;
1729 e70fd952 2024-03-22 stsp gotd_session_client.accept_flush_pkt = 1;
1730 e70fd952 2024-03-22 stsp
1731 e70fd952 2024-03-22 stsp imsg_init(&gotd_session.parent_iev.ibuf, GOTD_FILENO_MSG_PIPE);
1732 e70fd952 2024-03-22 stsp gotd_session.parent_iev.handler = session_dispatch;
1733 e70fd952 2024-03-22 stsp gotd_session.parent_iev.events = EV_READ;
1734 e70fd952 2024-03-22 stsp gotd_session.parent_iev.handler_arg = NULL;
1735 e70fd952 2024-03-22 stsp event_set(&gotd_session.parent_iev.ev, gotd_session.parent_iev.ibuf.fd,
1736 e70fd952 2024-03-22 stsp EV_READ, session_dispatch, &gotd_session.parent_iev);
1737 e70fd952 2024-03-22 stsp if (gotd_imsg_compose_event(&gotd_session.parent_iev,
1738 e70fd952 2024-03-22 stsp GOTD_IMSG_CLIENT_SESSION_READY, PROC_SESSION_WRITE,
1739 e70fd952 2024-03-22 stsp -1, NULL, 0) == -1) {
1740 e70fd952 2024-03-22 stsp err = got_error_from_errno("imsg compose CLIENT_SESSION_READY");
1741 e70fd952 2024-03-22 stsp goto done;
1742 e70fd952 2024-03-22 stsp }
1743 e70fd952 2024-03-22 stsp
1744 e70fd952 2024-03-22 stsp event_dispatch();
1745 e70fd952 2024-03-22 stsp done:
1746 e70fd952 2024-03-22 stsp if (err)
1747 e70fd952 2024-03-22 stsp log_warnx("%s: %s", title, err->msg);
1748 e70fd952 2024-03-22 stsp session_write_shutdown();
1749 e70fd952 2024-03-22 stsp }
1750 e70fd952 2024-03-22 stsp
1751 e70fd952 2024-03-22 stsp static void
1752 e70fd952 2024-03-22 stsp session_write_shutdown(void)
1753 e70fd952 2024-03-22 stsp {
1754 e70fd952 2024-03-22 stsp struct gotd_session_notif *notif;
1755 e70fd952 2024-03-22 stsp
1756 e8d451cc 2024-03-22 stsp log_debug("%s: shutting down", gotd_session.title);
1757 e70fd952 2024-03-22 stsp
1758 e70fd952 2024-03-22 stsp while (!STAILQ_EMPTY(&notifications)) {
1759 e70fd952 2024-03-22 stsp notif = STAILQ_FIRST(&notifications);
1760 e70fd952 2024-03-22 stsp STAILQ_REMOVE_HEAD(&notifications, entry);
1761 e70fd952 2024-03-22 stsp if (notif->fd != -1)
1762 e70fd952 2024-03-22 stsp close(notif->fd);
1763 e70fd952 2024-03-22 stsp free(notif->refname);
1764 e70fd952 2024-03-22 stsp free(notif);
1765 e70fd952 2024-03-22 stsp }
1766 e70fd952 2024-03-22 stsp
1767 e70fd952 2024-03-22 stsp if (gotd_session.repo)
1768 e70fd952 2024-03-22 stsp got_repo_close(gotd_session.repo);
1769 e70fd952 2024-03-22 stsp got_repo_pack_fds_close(gotd_session.pack_fds);
1770 e70fd952 2024-03-22 stsp got_repo_temp_fds_close(gotd_session.temp_fds);
1771 e70fd952 2024-03-22 stsp free(gotd_session_client.username);
1772 e70fd952 2024-03-22 stsp exit(0);
1773 e70fd952 2024-03-22 stsp }