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.
17 #include "got_compat.h"
19 #define GOTD_UNIX_SOCKET "/var/run/gotd.sock"
20 #define GOTD_UNIX_SOCKET_BACKLOG 10
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_MAX_CONN_PER_UID 4
27 #define GOTD_FD_RESERVE 5
28 #define GOTD_FD_NEEDED 6
29 #define GOTD_FILENO_MSG_PIPE 3
31 #define GOTD_DEFAULT_REQUEST_TIMEOUT 3600
33 /* Client hash tables need some extra room. */
34 #define GOTD_CLIENT_TABLE_SIZE (GOTD_MAXCLIENTS * 4)
50 void (*handler
)(int, short, void *);
57 GOTD_ACCESS_PERMITTED
= 1,
61 struct gotd_access_rule
{
62 STAILQ_ENTRY(gotd_access_rule
) entry
;
64 enum gotd_access access
;
67 #define GOTD_AUTH_READ 0x1
68 #define GOTD_AUTH_WRITE 0x2
72 STAILQ_HEAD(gotd_access_rule_list
, gotd_access_rule
);
75 TAILQ_ENTRY(gotd_repo
) entry
;
80 struct gotd_access_rule_list rules
;
81 struct got_pathlist_head protected_tag_namespaces
;
82 struct got_pathlist_head protected_branch_namespaces
;
83 struct got_pathlist_head protected_branches
;
85 TAILQ_HEAD(gotd_repolist
, gotd_repo
);
87 enum gotd_session_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
;
110 struct gotd_uid_connection_limit
{
115 struct gotd_child_proc
;
119 char unix_socket_path
[PATH_MAX
];
121 struct gotd_repolist repos
;
123 struct gotd_child_proc
*listen_proc
;
124 struct timeval request_timeout
;
125 struct timeval auth_timeout
;
126 struct gotd_uid_connection_limit
*connection_limits
;
127 size_t nconnection_limits
;
130 const char *confpath
;
135 enum gotd_imsg_type
{
136 /* An error occured while processing a request. */
139 /* Commands used by gotctl(8). */
142 GOTD_IMSG_INFO_CLIENT
,
145 /* Request a list of references. */
147 GOTD_IMSG_LIST_REFS_INTERNAL
,
154 /* Git protocol capabilities. */
155 GOTD_IMSG_CAPABILITIES
,
156 GOTD_IMSG_CAPABILITY
,
158 /* Git protocol chatter. */
159 GOTD_IMSG_WANT
, /* The client wants an object. */
160 GOTD_IMSG_HAVE
, /* The client has an object. */
161 GOTD_IMSG_ACK
, /* The server has an object or a reference. */
162 GOTD_IMSG_NAK
, /* The server does not have an object/ref. */
163 GOTD_IMSG_REF_UPDATE
, /* The client wants to update a reference. */
164 GOTD_IMSG_REF_DELETE
, /* The client wants to delete a reference. */
165 GOTD_IMSG_FLUSH
, /* The client sent a flush packet. */
166 GOTD_IMSG_DONE
, /* The client is done chatting. */
168 /* Sending or receiving a pack file. */
169 GOTD_IMSG_SEND_PACKFILE
, /* The server is sending a pack file. */
170 GOTD_IMSG_RECV_PACKFILE
, /* The server is receiving a pack file. */
171 GOTD_IMSG_PACKIDX_FILE
, /* Temporary file handle for new pack index. */
172 GOTD_IMSG_PACKFILE_PIPE
, /* Pipe to send/receive a pack file stream. */
173 GOTD_IMSG_PACKFILE_PROGRESS
, /* Progress reporting. */
174 GOTD_IMSG_PACKFILE_READY
, /* Pack file is ready to be sent. */
175 GOTD_IMSG_PACKFILE_STATUS
, /* Received pack success/failure status. */
176 GOTD_IMSG_PACKFILE_INSTALL
, /* Received pack file can be installed. */
177 GOTD_IMSG_PACKFILE_DONE
, /* Pack file has been sent/received. */
179 /* Reference updates. */
180 GOTD_IMSG_REF_UPDATES_START
, /* Ref updates starting. */
181 GOTD_IMSG_REF_UPDATE_OK
, /* Update went OK. */
182 GOTD_IMSG_REF_UPDATE_NG
, /* Update was not good. */
183 GOTD_IMSG_REFS_UPDATED
, /* The server proccessed all ref updates. */
185 /* Client connections. */
186 GOTD_IMSG_DISCONNECT
,
189 /* Child process management. */
190 GOTD_IMSG_CLIENT_SESSION_READY
,
191 GOTD_IMSG_REPO_CHILD_READY
,
192 GOTD_IMSG_CONNECT_REPO_CHILD
,
194 /* Auth child process. */
195 GOTD_IMSG_AUTHENTICATE
,
196 GOTD_IMSG_ACCESS_GRANTED
,
199 /* Structure for GOTD_IMSG_ERROR. */
200 struct gotd_imsg_error
{
201 int code
; /* an error code from got_error.h */
202 int errno_code
; /* in case code equals GOT_ERR_ERRNO */
204 char msg
[GOT_ERR_MAX_MSG_SIZE
];
205 } __attribute__((__packed__
));
207 /* Structure for GOTD_IMSG_INFO. */
208 struct gotd_imsg_info
{
214 /* Followed by nrepos GOTD_IMSG_INFO_REPO messages. */
215 /* Followed by nclients GOTD_IMSG_INFO_CLIENT messages. */
218 /* Structure for GOTD_IMSG_INFO_REPO. */
219 struct gotd_imsg_info_repo
{
220 char repo_name
[NAME_MAX
];
221 char repo_path
[PATH_MAX
];
224 /* Structure for GOTD_IMSG_INFO_CLIENT */
225 struct gotd_imsg_info_client
{
228 char repo_name
[NAME_MAX
];
230 pid_t session_child_pid
;
231 pid_t repo_child_pid
;
234 /* Structure for GOTD_IMSG_LIST_REFS. */
235 struct gotd_imsg_list_refs
{
236 char repo_name
[NAME_MAX
];
237 int client_is_reading
; /* 1 if reading, 0 if writing */
240 /* Structure for GOTD_IMSG_LIST_REFS_INTERNAL. */
241 struct gotd_imsg_list_refs_internal
{
245 /* Structure for GOTD_IMSG_REFLIST. */
246 struct gotd_imsg_reflist
{
249 /* Followed by nrefs times of gotd_imsg_ref/gotd_imsg_symref data. */
250 } __attribute__((__packed__
));
252 /* Structure for GOTD_IMSG_REF data. */
253 struct gotd_imsg_ref
{
254 uint8_t id
[SHA1_DIGEST_LENGTH
];
256 /* Followed by name_len data bytes. */
257 } __attribute__((__packed__
));
259 /* Structure for GOTD_IMSG_SYMREF data. */
260 struct gotd_imsg_symref
{
263 uint8_t target_id
[SHA1_DIGEST_LENGTH
];
266 * Followed by name_len + target_len data bytes.
268 } __attribute__((__packed__
));
270 /* Structure for GOTD_IMSG_CAPABILITIES data. */
271 struct gotd_imsg_capabilities
{
272 size_t ncapabilities
;
275 * Followed by ncapabilities * GOTD_IMSG_CAPABILITY.
277 } __attribute__((__packed__
));
279 /* Structure for GOTD_IMSG_CAPABILITY data. */
280 struct gotd_imsg_capability
{
285 * Followed by key_len + value_len data bytes.
287 } __attribute__((__packed__
));
289 /* Structure for GOTD_IMSG_WANT data. */
290 struct gotd_imsg_want
{
291 uint8_t object_id
[SHA1_DIGEST_LENGTH
];
293 } __attribute__((__packed__
));
295 /* Structure for GOTD_IMSG_HAVE data. */
296 struct gotd_imsg_have
{
297 uint8_t object_id
[SHA1_DIGEST_LENGTH
];
299 } __attribute__((__packed__
));
301 /* Structure for GOTD_IMSG_ACK data. */
302 struct gotd_imsg_ack
{
303 uint8_t object_id
[SHA1_DIGEST_LENGTH
];
305 } __attribute__((__packed__
));
307 /* Structure for GOTD_IMSG_NAK data. */
308 struct gotd_imsg_nak
{
309 uint8_t object_id
[SHA1_DIGEST_LENGTH
];
311 } __attribute__((__packed__
));
313 /* Structure for GOTD_IMSG_PACKFILE_STATUS data. */
314 struct gotd_imsg_packfile_status
{
317 /* Followed by reason_len data bytes. */
318 } __attribute__((__packed__
));
321 /* Structure for GOTD_IMSG_REF_UPDATE data. */
322 struct gotd_imsg_ref_update
{
323 uint8_t old_id
[SHA1_DIGEST_LENGTH
];
324 uint8_t new_id
[SHA1_DIGEST_LENGTH
];
330 /* Followed by name_len data bytes. */
331 } __attribute__((__packed__
));
333 /* Structure for GOTD_IMSG_REF_UPDATES_START data. */
334 struct gotd_imsg_ref_updates_start
{
338 /* Followed by nref_updates GOT_IMSG_REF_UPDATE_OK/NG messages. */
341 /* Structure for GOTD_IMSG_REF_UPDATE_OK data. */
342 struct gotd_imsg_ref_update_ok
{
343 uint8_t old_id
[SHA1_DIGEST_LENGTH
];
344 uint8_t new_id
[SHA1_DIGEST_LENGTH
];
349 /* Followed by name_len data bytes. */
350 } __attribute__((__packed__
));
352 /* Structure for GOTD_IMSG_REF_UPDATE_NG data. */
353 struct gotd_imsg_ref_update_ng
{
354 uint8_t old_id
[SHA1_DIGEST_LENGTH
];
355 uint8_t new_id
[SHA1_DIGEST_LENGTH
];
360 /* Followed by name_len + reason_len data bytes. */
361 } __attribute__((__packed__
));
363 /* Structure for GOTD_IMSG_SEND_PACKFILE data. */
364 struct gotd_imsg_send_packfile
{
368 /* delta cache file is sent as a file descriptor */
370 /* followed by two GOTD_IMSG_PACKFILE_PIPE messages */
373 /* Structure for GOTD_IMSG_RECV_PACKFILE data. */
374 struct gotd_imsg_recv_packfile
{
378 /* pack destination temp file is sent as a file descriptor */
381 /* Structure for GOTD_IMSG_PACKFILE_PIPE data. */
382 struct gotd_imsg_packfile_pipe
{
386 /* Structure for GOTD_IMSG_PACKIDX_FILE data. */
387 struct gotd_imsg_packidx_file
{
393 * Structure for GOTD_IMSG_PACKFILE_PROGRESS and
394 * GOTD_IMSG_PACKFILE_READY data.
396 struct gotd_imsg_packfile_progress
{
408 /* Structure for GOTD_IMSG_PACKFILE_INSTALL. */
409 struct gotd_imsg_packfile_install
{
411 uint8_t pack_sha1
[SHA1_DIGEST_LENGTH
];
414 /* Structure for GOTD_IMSG_PACKFILE_DONE data. */
415 struct gotd_imsg_packfile_done
{
419 /* Structure for GOTD_IMSG_DISCONNECT data. */
420 struct gotd_imsg_disconnect
{
424 /* Structure for GOTD_IMSG_CONNECT. */
425 struct gotd_imsg_connect
{
431 /* Structure for GOTD_IMSG_CONNECT_REPO_CHILD. */
432 struct gotd_imsg_connect_repo_child
{
434 enum gotd_procid proc_id
;
436 /* repo child imsg pipe is passed via imsg fd */
439 /* Structure for GOTD_IMSG_AUTHENTICATE. */
440 struct gotd_imsg_auth
{
447 int parse_config(const char *, enum gotd_procid
, struct gotd
*);
448 struct gotd_repo
*gotd_find_repo_by_name(const char *, struct gotd
*);
449 struct gotd_repo
*gotd_find_repo_by_path(const char *, struct gotd
*);
450 struct gotd_uid_connection_limit
*gotd_find_uid_connection_limit(
451 struct gotd_uid_connection_limit
*limits
, size_t nlimits
, uid_t uid
);
452 int gotd_parseuid(const char *s
, uid_t
*uid
);
455 const struct got_error
*gotd_imsg_flush(struct imsgbuf
*);
456 const struct got_error
*gotd_imsg_recv(struct imsg
*, struct imsgbuf
*, size_t);
457 const struct got_error
*gotd_imsg_poll_recv(struct imsg
*, struct imsgbuf
*,
459 const struct got_error
*gotd_imsg_recv_error(uint32_t *client_id
,
461 int gotd_imsg_send_error(struct imsgbuf
*ibuf
, uint32_t, uint32_t,
462 const struct got_error
*);
463 int gotd_imsg_send_error_event(struct gotd_imsgev
*, uint32_t, uint32_t,
464 const struct got_error
*);
465 void gotd_imsg_event_add(struct gotd_imsgev
*);
466 int gotd_imsg_compose_event(struct gotd_imsgev
*, uint16_t, uint32_t, int,
468 int gotd_imsg_forward(struct gotd_imsgev
*, struct imsg
*, int);
470 void gotd_imsg_send_ack(struct got_object_id
*, struct imsgbuf
*,
472 void gotd_imsg_send_nak(struct got_object_id
*, struct imsgbuf
*,