Blob


1 /*
2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
18 #define GOTD_UNIX_SOCKET "/var/run/gotd.sock"
19 #define GOTD_UNIX_SOCKET_BACKLOG 10
20 #define GOTD_UNIX_GROUP "_gotsh"
21 #define GOTD_USER "_gotd"
22 #define GOTD_CONF_PATH "/etc/gotd.conf"
24 #define GOTD_MAXCLIENTS 1024
25 #define GOTD_FD_RESERVE 5
26 #define GOTD_FD_NEEDED 6
27 #define GOTD_SOCK_FILENO 3
29 /* Client hash tables need some extra room. */
30 #define GOTD_CLIENT_TABLE_SIZE (GOTD_MAXCLIENTS * 4)
32 enum gotd_procid {
33 PROC_GOTD = 0,
34 PROC_REPO_READ,
35 PROC_REPO_WRITE,
36 PROC_MAX,
37 };
39 struct gotd_imsgev {
40 struct imsgbuf ibuf;
41 void (*handler)(int, short, void *);
42 void *handler_arg;
43 struct event ev;
44 short events;
45 };
47 struct gotd_child_proc {
48 pid_t pid;
49 enum gotd_procid type;
50 char repo_name[NAME_MAX];
51 char chroot_path[PATH_MAX];
52 int pipe[2];
53 struct gotd_imsgev iev;
54 size_t nhelpers;
55 };
57 struct gotd_repo {
58 TAILQ_ENTRY(gotd_repo) entry;
60 char name[NAME_MAX];
61 char path[PATH_MAX];
62 };
63 TAILQ_HEAD(gotd_repolist, gotd_repo);
65 enum gotd_client_state {
66 GOTD_STATE_EXPECT_LIST_REFS,
67 GOTD_STATE_EXPECT_CAPABILITIES,
68 GOTD_STATE_EXPECT_WANT,
69 GOTD_STATE_EXPECT_REF_UPDATE,
70 GOTD_STATE_EXPECT_MORE_REF_UPDATES,
71 GOTD_STATE_EXPECT_HAVE,
72 GOTD_STATE_EXPECT_PACKFILE,
73 GOTD_STATE_EXPECT_DONE,
74 GOTD_STATE_DONE,
75 };
77 struct gotd_client_capability {
78 char *key;
79 char *value;
80 };
82 struct gotd_object_id_array {
83 struct got_object_id **ids;
84 size_t nalloc;
85 size_t nids;
86 };
88 struct gotd {
89 pid_t pid;
90 char unix_socket_path[PATH_MAX];
91 char unix_group_name[32];
92 char user_name[32];
93 struct gotd_repolist repos;
94 int nrepos;
95 int verbosity;
96 struct event ev;
97 struct event pause;
98 struct gotd_child_proc *procs;
99 int nprocs;
100 };
102 enum gotd_imsg_type {
103 /* An error occured while processing a request. */
104 GOTD_IMSG_ERROR,
106 /* Request a list of references. */
107 GOTD_IMSG_LIST_REFS,
108 GOTD_IMSG_LIST_REFS_INTERNAL,
110 /* References. */
111 GOTD_IMSG_REFLIST,
112 GOTD_IMSG_REF,
113 GOTD_IMSG_SYMREF,
115 /* Git protocol capabilities. */
116 GOTD_IMSG_CAPABILITIES,
117 GOTD_IMSG_CAPABILITY,
119 /* Git protocol chatter. */
120 GOTD_IMSG_WANT, /* The client wants an object. */
121 GOTD_IMSG_HAVE, /* The client has an object. */
122 GOTD_IMSG_ACK, /* The server has an object or a reference. */
123 GOTD_IMSG_NAK, /* The server does not have an object/ref. */
124 GOTD_IMSG_REF_UPDATE, /* The client wants to update a reference. */
125 GOTD_IMSG_REF_DELETE, /* The client wants to delete a reference. */
126 GOTD_IMSG_FLUSH, /* The client sent a flush packet. */
127 GOTD_IMSG_DONE, /* The client is done chatting. */
129 /* Sending or receiving a pack file. */
130 GOTD_IMSG_SEND_PACKFILE, /* The server is sending a pack file. */
131 GOTD_IMSG_RECV_PACKFILE, /* The server is receiving a pack file. */
132 GOTD_IMSG_PACKIDX_FILE, /* Temporary file handle for new pack index. */
133 GOTD_IMSG_PACKFILE_PIPE, /* Pipe to send/receive a pack file stream. */
134 GOTD_IMSG_PACKFILE_PROGRESS, /* Progress reporting. */
135 GOTD_IMSG_PACKFILE_READY, /* Pack file is ready to be sent. */
136 GOTD_IMSG_PACKFILE_STATUS, /* Received pack success/failure status. */
137 GOTD_IMSG_PACKFILE_INSTALL, /* Received pack file can be installed. */
138 GOTD_IMSG_PACKFILE_DONE, /* Pack file has been sent/received. */
140 /* Reference updates. */
141 GOTD_IMSG_REF_UPDATES_START, /* Ref updates starting. */
142 GOTD_IMSG_REF_UPDATE_OK, /* Update went OK. */
143 GOTD_IMSG_REF_UPDATE_NG, /* Update was not good. */
144 GOTD_IMSG_REFS_UPDATED, /* The server proccessed all ref updates. */
146 /* Client is disconnecting. */
147 GOTD_IMSG_DISCONNECT,
148 };
150 /* Structure for GOTD_IMSG_ERROR. */
151 struct gotd_imsg_error {
152 int code; /* an error code from got_error.h */
153 int errno_code; /* in case code equals GOT_ERR_ERRNO */
154 uint32_t client_id;
155 char msg[GOT_ERR_MAX_MSG_SIZE];
156 } __attribute__((__packed__));
158 /* Structure for GOTD_IMSG_LIST_REFS. */
159 struct gotd_imsg_list_refs {
160 char repo_name[NAME_MAX];
161 int client_is_reading; /* 1 if reading, 0 if writing */
162 };
164 /* Structure for GOTD_IMSG_LIST_REFS_INTERNAL. */
165 struct gotd_imsg_list_refs_internal {
166 uint32_t client_id;
167 };
169 /* Structure for GOTD_IMSG_REFLIST. */
170 struct gotd_imsg_reflist {
171 size_t nrefs;
173 /* Followed by nrefs times of gotd_imsg_ref/gotd_imsg_symref data. */
174 } __attribute__((__packed__));
176 /* Structure for GOTD_IMSG_REF data. */
177 struct gotd_imsg_ref {
178 uint8_t id[SHA1_DIGEST_LENGTH];
179 size_t name_len;
180 /* Followed by name_len data bytes. */
181 } __attribute__((__packed__));
183 /* Structure for GOTD_IMSG_SYMREF data. */
184 struct gotd_imsg_symref {
185 size_t name_len;
186 size_t target_len;
187 uint8_t target_id[SHA1_DIGEST_LENGTH];
189 /*
190 * Followed by name_len + target_len data bytes.
191 */
192 } __attribute__((__packed__));
194 /* Structure for GOTD_IMSG_CAPABILITIES data. */
195 struct gotd_imsg_capabilities {
196 size_t ncapabilities;
198 /*
199 * Followed by ncapabilities * GOTD_IMSG_CAPABILITY.
200 */
201 } __attribute__((__packed__));
203 /* Structure for GOTD_IMSG_CAPABILITY data. */
204 struct gotd_imsg_capability {
205 size_t key_len;
206 size_t value_len;
208 /*
209 * Followed by key_len + value_len data bytes.
210 */
211 } __attribute__((__packed__));
213 /* Structure for GOTD_IMSG_WANT data. */
214 struct gotd_imsg_want {
215 uint8_t object_id[SHA1_DIGEST_LENGTH];
216 uint32_t client_id;
217 } __attribute__((__packed__));
219 /* Structure for GOTD_IMSG_HAVE data. */
220 struct gotd_imsg_have {
221 uint8_t object_id[SHA1_DIGEST_LENGTH];
222 uint32_t client_id;
223 } __attribute__((__packed__));
225 /* Structure for GOTD_IMSG_ACK data. */
226 struct gotd_imsg_ack {
227 uint8_t object_id[SHA1_DIGEST_LENGTH];
228 uint32_t client_id;
229 } __attribute__((__packed__));
231 /* Structure for GOTD_IMSG_NAK data. */
232 struct gotd_imsg_nak {
233 uint8_t object_id[SHA1_DIGEST_LENGTH];
234 uint32_t client_id;
235 } __attribute__((__packed__));
237 /* Structure for GOTD_IMSG_PACKFILE_STATUS data. */
238 struct gotd_imsg_packfile_status {
239 size_t reason_len;
241 /* Followed by reason_len data bytes. */
242 } __attribute__((__packed__));
245 /* Structure for GOTD_IMSG_REF_UPDATE data. */
246 struct gotd_imsg_ref_update {
247 uint8_t old_id[SHA1_DIGEST_LENGTH];
248 uint8_t new_id[SHA1_DIGEST_LENGTH];
249 int ref_is_new;
250 uint32_t client_id;
251 size_t name_len;
253 /* Followed by name_len data bytes. */
254 } __attribute__((__packed__));
256 /* Structure for GOTD_IMSG_REF_UPDATES_START data. */
257 struct gotd_imsg_ref_updates_start {
258 int nref_updates;
259 uint32_t client_id;
261 /* Followed by nref_updates GOT_IMSG_REF_UPDATE_OK/NG messages. */
262 };
264 /* Structure for GOTD_IMSG_REF_UPDATE_OK data. */
265 struct gotd_imsg_ref_update_ok {
266 uint8_t old_id[SHA1_DIGEST_LENGTH];
267 uint8_t new_id[SHA1_DIGEST_LENGTH];
268 int ref_is_new;
269 uint32_t client_id;
270 size_t name_len;
272 /* Followed by name_len data bytes. */
273 } __attribute__((__packed__));
275 /* Structure for GOTD_IMSG_REF_UPDATE_NG data. */
276 struct gotd_imsg_ref_update_ng {
277 uint8_t old_id[SHA1_DIGEST_LENGTH];
278 uint8_t new_id[SHA1_DIGEST_LENGTH];
279 uint32_t client_id;
280 size_t name_len;
281 size_t reason_len;
283 /* Followed by name_len + reason_len data bytes. */
284 } __attribute__((__packed__));
286 /* Structure for GOTD_IMSG_SEND_PACKFILE data. */
287 struct gotd_imsg_send_packfile {
288 uint32_t client_id;
289 int report_progress;
291 /* delta cache file is sent as a file descriptor */
293 /* followed by two GOTD_IMSG_PACKFILE_PIPE messages */
294 };
296 /* Structure for GOTD_IMSG_RECV_PACKFILE data. */
297 struct gotd_imsg_recv_packfile {
298 uint32_t client_id;
299 int report_status;
301 /* pack destination temp file is sent as a file descriptor */
302 };
304 /* Structure for GOTD_IMSG_PACKFILE_PIPE data. */
305 struct gotd_imsg_packfile_pipe {
306 uint32_t client_id;
307 };
309 /* Structure for GOTD_IMSG_PACKIDX_FILE data. */
310 struct gotd_imsg_packidx_file {
311 uint32_t client_id;
312 };
315 /*
316 * Structure for GOTD_IMSG_PACKFILE_PROGRESS and
317 * GOTD_IMSG_PACKFILE_READY data.
318 */
319 struct gotd_imsg_packfile_progress {
320 uint32_t client_id;
321 int ncolored;
322 int nfound;
323 int ntrees;
324 off_t packfile_size;
325 int ncommits;
326 int nobj_total;
327 int nobj_deltify;
328 int nobj_written;
329 };
331 /* Structure for GOTD_IMSG_PACKFILE_INSTALL. */
332 struct gotd_imsg_packfile_install {
333 uint32_t client_id;
334 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
335 };
337 /* Structure for GOTD_IMSG_PACKFILE_DONE data. */
338 struct gotd_imsg_packfile_done {
339 uint32_t client_id;
340 };
342 /* Structure for GOTD_IMSG_DISCONNECT data. */
343 struct gotd_imsg_disconnect {
344 uint32_t client_id;
345 };
347 int parse_config(const char *, enum gotd_procid, struct gotd *);
349 /* imsg.c */
350 const struct got_error *gotd_imsg_flush(struct imsgbuf *);
351 const struct got_error *gotd_imsg_recv(struct imsg *, struct imsgbuf *, size_t);
352 const struct got_error *gotd_imsg_poll_recv(struct imsg *, struct imsgbuf *,
353 size_t);
354 const struct got_error *gotd_imsg_recv_error(uint32_t *client_id,
355 struct imsg *imsg);
356 int gotd_imsg_send_error(struct imsgbuf *ibuf, uint32_t, uint32_t,
357 const struct got_error *);
358 int gotd_imsg_send_error_event(struct gotd_imsgev *, uint32_t, uint32_t,
359 const struct got_error *);
360 void gotd_imsg_event_add(struct gotd_imsgev *);
361 int gotd_imsg_compose_event(struct gotd_imsgev *, uint16_t, uint32_t, int,
362 void *, uint16_t);
363 int gotd_imsg_forward(struct gotd_imsgev *, struct imsg *, int);
365 void gotd_imsg_send_ack(struct got_object_id *, struct imsgbuf *,
366 uint32_t, pid_t);
367 void gotd_imsg_send_nak(struct got_object_id *, struct imsgbuf *,
368 uint32_t, pid_t);