2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
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.
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.
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"
23 #define GOTD_EMPTY_PATH "/var/empty"
25 #define GOTD_MAXCLIENTS 1024
26 #define GOTD_FD_RESERVE 5
27 #define GOTD_FD_NEEDED 6
28 #define GOTD_FILENO_MSG_PIPE 3
30 /* Client hash tables need some extra room. */
31 #define GOTD_CLIENT_TABLE_SIZE (GOTD_MAXCLIENTS * 4)
43 void (*handler)(int, short, void *);
49 struct gotd_child_proc {
51 enum gotd_procid type;
52 char repo_name[NAME_MAX];
53 char repo_path[PATH_MAX];
55 struct gotd_imsgev iev;
60 GOTD_ACCESS_PERMITTED = 1,
64 struct gotd_access_rule {
65 STAILQ_ENTRY(gotd_access_rule) entry;
67 enum gotd_access access;
70 #define GOTD_AUTH_READ 0x1
71 #define GOTD_AUTH_WRITE 0x2
75 STAILQ_HEAD(gotd_access_rule_list, gotd_access_rule);
78 TAILQ_ENTRY(gotd_repo) entry;
83 struct gotd_access_rule_list rules;
85 TAILQ_HEAD(gotd_repolist, gotd_repo);
87 enum gotd_client_state {
88 GOTD_STATE_EXPECT_LIST_REFS,
89 GOTD_STATE_EXPECT_CAPABILITIES,
90 GOTD_STATE_EXPECT_WANT,
91 GOTD_STATE_EXPECT_REF_UPDATE,
92 GOTD_STATE_EXPECT_MORE_REF_UPDATES,
93 GOTD_STATE_EXPECT_HAVE,
94 GOTD_STATE_EXPECT_PACKFILE,
95 GOTD_STATE_EXPECT_DONE,
99 struct gotd_client_capability {
104 struct gotd_object_id_array {
105 struct got_object_id **ids;
112 char unix_socket_path[PATH_MAX];
113 char unix_group_name[32];
115 struct gotd_repolist repos;
118 struct gotd_child_proc *procs;
122 enum gotd_imsg_type {
123 /* An error occured while processing a request. */
126 /* Commands used by gotctl(8). */
129 GOTD_IMSG_INFO_CLIENT,
132 /* Request a list of references. */
134 GOTD_IMSG_LIST_REFS_INTERNAL,
141 /* Git protocol capabilities. */
142 GOTD_IMSG_CAPABILITIES,
143 GOTD_IMSG_CAPABILITY,
145 /* Git protocol chatter. */
146 GOTD_IMSG_WANT, /* The client wants an object. */
147 GOTD_IMSG_HAVE, /* The client has an object. */
148 GOTD_IMSG_ACK, /* The server has an object or a reference. */
149 GOTD_IMSG_NAK, /* The server does not have an object/ref. */
150 GOTD_IMSG_REF_UPDATE, /* The client wants to update a reference. */
151 GOTD_IMSG_REF_DELETE, /* The client wants to delete a reference. */
152 GOTD_IMSG_FLUSH, /* The client sent a flush packet. */
153 GOTD_IMSG_DONE, /* The client is done chatting. */
155 /* Sending or receiving a pack file. */
156 GOTD_IMSG_SEND_PACKFILE, /* The server is sending a pack file. */
157 GOTD_IMSG_RECV_PACKFILE, /* The server is receiving a pack file. */
158 GOTD_IMSG_PACKIDX_FILE, /* Temporary file handle for new pack index. */
159 GOTD_IMSG_PACKFILE_PIPE, /* Pipe to send/receive a pack file stream. */
160 GOTD_IMSG_PACKFILE_PROGRESS, /* Progress reporting. */
161 GOTD_IMSG_PACKFILE_READY, /* Pack file is ready to be sent. */
162 GOTD_IMSG_PACKFILE_STATUS, /* Received pack success/failure status. */
163 GOTD_IMSG_PACKFILE_INSTALL, /* Received pack file can be installed. */
164 GOTD_IMSG_PACKFILE_DONE, /* Pack file has been sent/received. */
166 /* Reference updates. */
167 GOTD_IMSG_REF_UPDATES_START, /* Ref updates starting. */
168 GOTD_IMSG_REF_UPDATE_OK, /* Update went OK. */
169 GOTD_IMSG_REF_UPDATE_NG, /* Update was not good. */
170 GOTD_IMSG_REFS_UPDATED, /* The server proccessed all ref updates. */
172 /* Client connections. */
173 GOTD_IMSG_DISCONNECT,
177 /* Structure for GOTD_IMSG_ERROR. */
178 struct gotd_imsg_error {
179 int code; /* an error code from got_error.h */
180 int errno_code; /* in case code equals GOT_ERR_ERRNO */
182 char msg[GOT_ERR_MAX_MSG_SIZE];
183 } __attribute__((__packed__));
185 /* Structure for GOTD_IMSG_INFO. */
186 struct gotd_imsg_info {
192 /* Followed by nrepos GOTD_IMSG_INFO_REPO messages. */
193 /* Followed by nclients GOTD_IMSG_INFO_CLIENT messages. */
196 /* Structure for GOTD_IMSG_INFO_REPO. */
197 struct gotd_imsg_info_repo {
198 char repo_name[NAME_MAX];
199 char repo_path[PATH_MAX];
202 /* Structure for GOTD_IMSG_INFO_CLIENT */
203 struct gotd_imsg_info_client {
206 char repo_name[NAME_MAX];
208 enum gotd_client_state state;
209 size_t ncapabilities;
211 /* Followed by ncapabilities GOTD_IMSG_CAPABILITY. */
214 /* Structure for GOTD_IMSG_LIST_REFS. */
215 struct gotd_imsg_list_refs {
216 char repo_name[NAME_MAX];
217 int client_is_reading; /* 1 if reading, 0 if writing */
220 /* Structure for GOTD_IMSG_LIST_REFS_INTERNAL. */
221 struct gotd_imsg_list_refs_internal {
225 /* Structure for GOTD_IMSG_REFLIST. */
226 struct gotd_imsg_reflist {
229 /* Followed by nrefs times of gotd_imsg_ref/gotd_imsg_symref data. */
230 } __attribute__((__packed__));
232 /* Structure for GOTD_IMSG_REF data. */
233 struct gotd_imsg_ref {
234 uint8_t id[SHA1_DIGEST_LENGTH];
236 /* Followed by name_len data bytes. */
237 } __attribute__((__packed__));
239 /* Structure for GOTD_IMSG_SYMREF data. */
240 struct gotd_imsg_symref {
243 uint8_t target_id[SHA1_DIGEST_LENGTH];
246 * Followed by name_len + target_len data bytes.
248 } __attribute__((__packed__));
250 /* Structure for GOTD_IMSG_CAPABILITIES data. */
251 struct gotd_imsg_capabilities {
252 size_t ncapabilities;
255 * Followed by ncapabilities * GOTD_IMSG_CAPABILITY.
257 } __attribute__((__packed__));
259 /* Structure for GOTD_IMSG_CAPABILITY data. */
260 struct gotd_imsg_capability {
265 * Followed by key_len + value_len data bytes.
267 } __attribute__((__packed__));
269 /* Structure for GOTD_IMSG_WANT data. */
270 struct gotd_imsg_want {
271 uint8_t object_id[SHA1_DIGEST_LENGTH];
273 } __attribute__((__packed__));
275 /* Structure for GOTD_IMSG_HAVE data. */
276 struct gotd_imsg_have {
277 uint8_t object_id[SHA1_DIGEST_LENGTH];
279 } __attribute__((__packed__));
281 /* Structure for GOTD_IMSG_ACK data. */
282 struct gotd_imsg_ack {
283 uint8_t object_id[SHA1_DIGEST_LENGTH];
285 } __attribute__((__packed__));
287 /* Structure for GOTD_IMSG_NAK data. */
288 struct gotd_imsg_nak {
289 uint8_t object_id[SHA1_DIGEST_LENGTH];
291 } __attribute__((__packed__));
293 /* Structure for GOTD_IMSG_PACKFILE_STATUS data. */
294 struct gotd_imsg_packfile_status {
297 /* Followed by reason_len data bytes. */
298 } __attribute__((__packed__));
301 /* Structure for GOTD_IMSG_REF_UPDATE data. */
302 struct gotd_imsg_ref_update {
303 uint8_t old_id[SHA1_DIGEST_LENGTH];
304 uint8_t new_id[SHA1_DIGEST_LENGTH];
309 /* Followed by name_len data bytes. */
310 } __attribute__((__packed__));
312 /* Structure for GOTD_IMSG_REF_UPDATES_START data. */
313 struct gotd_imsg_ref_updates_start {
317 /* Followed by nref_updates GOT_IMSG_REF_UPDATE_OK/NG messages. */
320 /* Structure for GOTD_IMSG_REF_UPDATE_OK data. */
321 struct gotd_imsg_ref_update_ok {
322 uint8_t old_id[SHA1_DIGEST_LENGTH];
323 uint8_t new_id[SHA1_DIGEST_LENGTH];
328 /* Followed by name_len data bytes. */
329 } __attribute__((__packed__));
331 /* Structure for GOTD_IMSG_REF_UPDATE_NG data. */
332 struct gotd_imsg_ref_update_ng {
333 uint8_t old_id[SHA1_DIGEST_LENGTH];
334 uint8_t new_id[SHA1_DIGEST_LENGTH];
339 /* Followed by name_len + reason_len data bytes. */
340 } __attribute__((__packed__));
342 /* Structure for GOTD_IMSG_SEND_PACKFILE data. */
343 struct gotd_imsg_send_packfile {
347 /* delta cache file is sent as a file descriptor */
349 /* followed by two GOTD_IMSG_PACKFILE_PIPE messages */
352 /* Structure for GOTD_IMSG_RECV_PACKFILE data. */
353 struct gotd_imsg_recv_packfile {
357 /* pack destination temp file is sent as a file descriptor */
360 /* Structure for GOTD_IMSG_PACKFILE_PIPE data. */
361 struct gotd_imsg_packfile_pipe {
365 /* Structure for GOTD_IMSG_PACKIDX_FILE data. */
366 struct gotd_imsg_packidx_file {
372 * Structure for GOTD_IMSG_PACKFILE_PROGRESS and
373 * GOTD_IMSG_PACKFILE_READY data.
375 struct gotd_imsg_packfile_progress {
387 /* Structure for GOTD_IMSG_PACKFILE_INSTALL. */
388 struct gotd_imsg_packfile_install {
390 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
393 /* Structure for GOTD_IMSG_PACKFILE_DONE data. */
394 struct gotd_imsg_packfile_done {
398 /* Structure for GOTD_IMSG_DISCONNECT data. */
399 struct gotd_imsg_disconnect {
403 /* Structure for GOTD_IMSG_CONNECT. */
404 struct gotd_imsg_connect {
408 int parse_config(const char *, enum gotd_procid, struct gotd *);
411 const struct got_error *gotd_imsg_flush(struct imsgbuf *);
412 const struct got_error *gotd_imsg_recv(struct imsg *, struct imsgbuf *, size_t);
413 const struct got_error *gotd_imsg_poll_recv(struct imsg *, struct imsgbuf *,
415 const struct got_error *gotd_imsg_recv_error(uint32_t *client_id,
417 int gotd_imsg_send_error(struct imsgbuf *ibuf, uint32_t, uint32_t,
418 const struct got_error *);
419 int gotd_imsg_send_error_event(struct gotd_imsgev *, uint32_t, uint32_t,
420 const struct got_error *);
421 void gotd_imsg_event_add(struct gotd_imsgev *);
422 int gotd_imsg_compose_event(struct gotd_imsgev *, uint16_t, uint32_t, int,
424 int gotd_imsg_forward(struct gotd_imsgev *, struct imsg *, int);
426 void gotd_imsg_send_ack(struct got_object_id *, struct imsgbuf *,
428 void gotd_imsg_send_nak(struct got_object_id *, struct imsgbuf *,