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 /* Request a list of references. */
108 GOTD_IMSG_LIST_REFS_INTERNAL
,
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
,
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 */
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 */
164 /* Structure for GOTD_IMSG_LIST_REFS_INTERNAL. */
165 struct gotd_imsg_list_refs_internal
{
169 /* Structure for GOTD_IMSG_REFLIST. */
170 struct gotd_imsg_reflist
{
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
];
180 /* Followed by name_len data bytes. */
181 } __attribute__((__packed__
));
183 /* Structure for GOTD_IMSG_SYMREF data. */
184 struct gotd_imsg_symref
{
187 uint8_t target_id
[SHA1_DIGEST_LENGTH
];
190 * Followed by name_len + target_len data bytes.
192 } __attribute__((__packed__
));
194 /* Structure for GOTD_IMSG_CAPABILITIES data. */
195 struct gotd_imsg_capabilities
{
196 size_t ncapabilities
;
199 * Followed by ncapabilities * GOTD_IMSG_CAPABILITY.
201 } __attribute__((__packed__
));
203 /* Structure for GOTD_IMSG_CAPABILITY data. */
204 struct gotd_imsg_capability
{
209 * Followed by key_len + value_len data bytes.
211 } __attribute__((__packed__
));
213 /* Structure for GOTD_IMSG_WANT data. */
214 struct gotd_imsg_want
{
215 uint8_t object_id
[SHA1_DIGEST_LENGTH
];
217 } __attribute__((__packed__
));
219 /* Structure for GOTD_IMSG_HAVE data. */
220 struct gotd_imsg_have
{
221 uint8_t object_id
[SHA1_DIGEST_LENGTH
];
223 } __attribute__((__packed__
));
225 /* Structure for GOTD_IMSG_ACK data. */
226 struct gotd_imsg_ack
{
227 uint8_t object_id
[SHA1_DIGEST_LENGTH
];
229 } __attribute__((__packed__
));
231 /* Structure for GOTD_IMSG_NAK data. */
232 struct gotd_imsg_nak
{
233 uint8_t object_id
[SHA1_DIGEST_LENGTH
];
235 } __attribute__((__packed__
));
237 /* Structure for GOTD_IMSG_PACKFILE_STATUS data. */
238 struct gotd_imsg_packfile_status
{
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
];
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
{
261 /* Followed by nref_updates GOT_IMSG_REF_UPDATE_OK/NG messages. */
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
];
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
];
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
{
291 /* delta cache file is sent as a file descriptor */
293 /* followed by two GOTD_IMSG_PACKFILE_PIPE messages */
296 /* Structure for GOTD_IMSG_RECV_PACKFILE data. */
297 struct gotd_imsg_recv_packfile
{
301 /* pack destination temp file is sent as a file descriptor */
304 /* Structure for GOTD_IMSG_PACKFILE_PIPE data. */
305 struct gotd_imsg_packfile_pipe
{
309 /* Structure for GOTD_IMSG_PACKIDX_FILE data. */
310 struct gotd_imsg_packidx_file
{
316 * Structure for GOTD_IMSG_PACKFILE_PROGRESS and
317 * GOTD_IMSG_PACKFILE_READY data.
319 struct gotd_imsg_packfile_progress
{
331 /* Structure for GOTD_IMSG_PACKFILE_INSTALL. */
332 struct gotd_imsg_packfile_install
{
334 uint8_t pack_sha1
[SHA1_DIGEST_LENGTH
];
337 /* Structure for GOTD_IMSG_PACKFILE_DONE data. */
338 struct gotd_imsg_packfile_done
{
342 /* Structure for GOTD_IMSG_DISCONNECT data. */
343 struct gotd_imsg_disconnect
{
347 int parse_config(const char *, enum gotd_procid
, struct gotd
*);
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
*,
354 const struct got_error
*gotd_imsg_recv_error(uint32_t *client_id
,
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,
363 int gotd_imsg_forward(struct gotd_imsgev
*, struct imsg
*, int);
365 void gotd_imsg_send_ack(struct got_object_id
*, struct imsgbuf
*,
367 void gotd_imsg_send_nak(struct got_object_id
*, struct imsgbuf
*,