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