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"
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)
41 void (*handler)(int, short, void *);
47 struct gotd_child_proc {
49 enum gotd_procid type;
50 char repo_name[NAME_MAX];
51 char chroot_path[PATH_MAX];
53 struct gotd_imsgev iev;
58 TAILQ_ENTRY(gotd_repo) entry;
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,
77 struct gotd_client_capability {
82 struct gotd_object_id_array {
83 struct got_object_id **ids;
90 char unix_socket_path[PATH_MAX];
91 char unix_group_name[32];
93 struct gotd_repolist repos;
98 struct gotd_child_proc *procs;
102 enum gotd_imsg_type {
103 /* An error occured while processing a request. */
106 /* Commands used by gotctl(8). */
109 GOTD_IMSG_INFO_CLIENT,
112 /* Request a list of references. */
114 GOTD_IMSG_LIST_REFS_INTERNAL,
121 /* Git protocol capabilities. */
122 GOTD_IMSG_CAPABILITIES,
123 GOTD_IMSG_CAPABILITY,
125 /* Git protocol chatter. */
126 GOTD_IMSG_WANT, /* The client wants an object. */
127 GOTD_IMSG_HAVE, /* The client has an object. */
128 GOTD_IMSG_ACK, /* The server has an object or a reference. */
129 GOTD_IMSG_NAK, /* The server does not have an object/ref. */
130 GOTD_IMSG_REF_UPDATE, /* The client wants to update a reference. */
131 GOTD_IMSG_REF_DELETE, /* The client wants to delete a reference. */
132 GOTD_IMSG_FLUSH, /* The client sent a flush packet. */
133 GOTD_IMSG_DONE, /* The client is done chatting. */
135 /* Sending or receiving a pack file. */
136 GOTD_IMSG_SEND_PACKFILE, /* The server is sending a pack file. */
137 GOTD_IMSG_RECV_PACKFILE, /* The server is receiving a pack file. */
138 GOTD_IMSG_PACKIDX_FILE, /* Temporary file handle for new pack index. */
139 GOTD_IMSG_PACKFILE_PIPE, /* Pipe to send/receive a pack file stream. */
140 GOTD_IMSG_PACKFILE_PROGRESS, /* Progress reporting. */
141 GOTD_IMSG_PACKFILE_READY, /* Pack file is ready to be sent. */
142 GOTD_IMSG_PACKFILE_STATUS, /* Received pack success/failure status. */
143 GOTD_IMSG_PACKFILE_INSTALL, /* Received pack file can be installed. */
144 GOTD_IMSG_PACKFILE_DONE, /* Pack file has been sent/received. */
146 /* Reference updates. */
147 GOTD_IMSG_REF_UPDATES_START, /* Ref updates starting. */
148 GOTD_IMSG_REF_UPDATE_OK, /* Update went OK. */
149 GOTD_IMSG_REF_UPDATE_NG, /* Update was not good. */
150 GOTD_IMSG_REFS_UPDATED, /* The server proccessed all ref updates. */
152 /* Client is disconnecting. */
153 GOTD_IMSG_DISCONNECT,
156 /* Structure for GOTD_IMSG_ERROR. */
157 struct gotd_imsg_error {
158 int code; /* an error code from got_error.h */
159 int errno_code; /* in case code equals GOT_ERR_ERRNO */
161 char msg[GOT_ERR_MAX_MSG_SIZE];
162 } __attribute__((__packed__));
164 /* Structure for GOTD_IMSG_INFO. */
165 struct gotd_imsg_info {
171 /* Followed by nrepos GOTD_IMSG_INFO_REPO messages. */
172 /* Followed by nclients GOTD_IMSG_INFO_CLIENT messages. */
175 /* Structure for GOTD_IMSG_INFO_REPO. */
176 struct gotd_imsg_info_repo {
177 char repo_name[NAME_MAX];
178 char repo_path[PATH_MAX];
181 /* Structure for GOTD_IMSG_INFO_CLIENT */
182 struct gotd_imsg_info_client {
185 char repo_name[NAME_MAX];
187 enum gotd_client_state state;
188 size_t ncapabilities;
190 /* Followed by ncapabilities GOTD_IMSG_CAPABILITY. */
193 /* Structure for GOTD_IMSG_LIST_REFS. */
194 struct gotd_imsg_list_refs {
195 char repo_name[NAME_MAX];
196 int client_is_reading; /* 1 if reading, 0 if writing */
199 /* Structure for GOTD_IMSG_LIST_REFS_INTERNAL. */
200 struct gotd_imsg_list_refs_internal {
204 /* Structure for GOTD_IMSG_REFLIST. */
205 struct gotd_imsg_reflist {
208 /* Followed by nrefs times of gotd_imsg_ref/gotd_imsg_symref data. */
209 } __attribute__((__packed__));
211 /* Structure for GOTD_IMSG_REF data. */
212 struct gotd_imsg_ref {
213 uint8_t id[SHA1_DIGEST_LENGTH];
215 /* Followed by name_len data bytes. */
216 } __attribute__((__packed__));
218 /* Structure for GOTD_IMSG_SYMREF data. */
219 struct gotd_imsg_symref {
222 uint8_t target_id[SHA1_DIGEST_LENGTH];
225 * Followed by name_len + target_len data bytes.
227 } __attribute__((__packed__));
229 /* Structure for GOTD_IMSG_CAPABILITIES data. */
230 struct gotd_imsg_capabilities {
231 size_t ncapabilities;
234 * Followed by ncapabilities * GOTD_IMSG_CAPABILITY.
236 } __attribute__((__packed__));
238 /* Structure for GOTD_IMSG_CAPABILITY data. */
239 struct gotd_imsg_capability {
244 * Followed by key_len + value_len data bytes.
246 } __attribute__((__packed__));
248 /* Structure for GOTD_IMSG_WANT data. */
249 struct gotd_imsg_want {
250 uint8_t object_id[SHA1_DIGEST_LENGTH];
252 } __attribute__((__packed__));
254 /* Structure for GOTD_IMSG_HAVE data. */
255 struct gotd_imsg_have {
256 uint8_t object_id[SHA1_DIGEST_LENGTH];
258 } __attribute__((__packed__));
260 /* Structure for GOTD_IMSG_ACK data. */
261 struct gotd_imsg_ack {
262 uint8_t object_id[SHA1_DIGEST_LENGTH];
264 } __attribute__((__packed__));
266 /* Structure for GOTD_IMSG_NAK data. */
267 struct gotd_imsg_nak {
268 uint8_t object_id[SHA1_DIGEST_LENGTH];
270 } __attribute__((__packed__));
272 /* Structure for GOTD_IMSG_PACKFILE_STATUS data. */
273 struct gotd_imsg_packfile_status {
276 /* Followed by reason_len data bytes. */
277 } __attribute__((__packed__));
280 /* Structure for GOTD_IMSG_REF_UPDATE data. */
281 struct gotd_imsg_ref_update {
282 uint8_t old_id[SHA1_DIGEST_LENGTH];
283 uint8_t new_id[SHA1_DIGEST_LENGTH];
288 /* Followed by name_len data bytes. */
289 } __attribute__((__packed__));
291 /* Structure for GOTD_IMSG_REF_UPDATES_START data. */
292 struct gotd_imsg_ref_updates_start {
296 /* Followed by nref_updates GOT_IMSG_REF_UPDATE_OK/NG messages. */
299 /* Structure for GOTD_IMSG_REF_UPDATE_OK data. */
300 struct gotd_imsg_ref_update_ok {
301 uint8_t old_id[SHA1_DIGEST_LENGTH];
302 uint8_t new_id[SHA1_DIGEST_LENGTH];
307 /* Followed by name_len data bytes. */
308 } __attribute__((__packed__));
310 /* Structure for GOTD_IMSG_REF_UPDATE_NG data. */
311 struct gotd_imsg_ref_update_ng {
312 uint8_t old_id[SHA1_DIGEST_LENGTH];
313 uint8_t new_id[SHA1_DIGEST_LENGTH];
318 /* Followed by name_len + reason_len data bytes. */
319 } __attribute__((__packed__));
321 /* Structure for GOTD_IMSG_SEND_PACKFILE data. */
322 struct gotd_imsg_send_packfile {
326 /* delta cache file is sent as a file descriptor */
328 /* followed by two GOTD_IMSG_PACKFILE_PIPE messages */
331 /* Structure for GOTD_IMSG_RECV_PACKFILE data. */
332 struct gotd_imsg_recv_packfile {
336 /* pack destination temp file is sent as a file descriptor */
339 /* Structure for GOTD_IMSG_PACKFILE_PIPE data. */
340 struct gotd_imsg_packfile_pipe {
344 /* Structure for GOTD_IMSG_PACKIDX_FILE data. */
345 struct gotd_imsg_packidx_file {
351 * Structure for GOTD_IMSG_PACKFILE_PROGRESS and
352 * GOTD_IMSG_PACKFILE_READY data.
354 struct gotd_imsg_packfile_progress {
366 /* Structure for GOTD_IMSG_PACKFILE_INSTALL. */
367 struct gotd_imsg_packfile_install {
369 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
372 /* Structure for GOTD_IMSG_PACKFILE_DONE data. */
373 struct gotd_imsg_packfile_done {
377 /* Structure for GOTD_IMSG_DISCONNECT data. */
378 struct gotd_imsg_disconnect {
382 int parse_config(const char *, enum gotd_procid, struct gotd *);
385 const struct got_error *gotd_imsg_flush(struct imsgbuf *);
386 const struct got_error *gotd_imsg_recv(struct imsg *, struct imsgbuf *, size_t);
387 const struct got_error *gotd_imsg_poll_recv(struct imsg *, struct imsgbuf *,
389 const struct got_error *gotd_imsg_recv_error(uint32_t *client_id,
391 int gotd_imsg_send_error(struct imsgbuf *ibuf, uint32_t, uint32_t,
392 const struct got_error *);
393 int gotd_imsg_send_error_event(struct gotd_imsgev *, uint32_t, uint32_t,
394 const struct got_error *);
395 void gotd_imsg_event_add(struct gotd_imsgev *);
396 int gotd_imsg_compose_event(struct gotd_imsgev *, uint16_t, uint32_t, int,
398 int gotd_imsg_forward(struct gotd_imsgev *, struct imsg *, int);
400 void gotd_imsg_send_ack(struct got_object_id *, struct imsgbuf *,
402 void gotd_imsg_send_nak(struct got_object_id *, struct imsgbuf *,