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_USER "_gotd"
21 #define GOTD_CONF_PATH "/etc/gotd.conf"
22 #define GOTD_EMPTY_PATH "/var/empty"
24 #define GOTD_MAXCLIENTS 1024
25 #define GOTD_MAX_CONN_PER_UID 4
26 #define GOTD_FD_RESERVE 5
27 #define GOTD_FD_NEEDED 6
28 #define GOTD_FILENO_MSG_PIPE 3
30 #define GOTD_DEFAULT_REQUEST_TIMEOUT 3600
32 /* Client hash tables need some extra room. */
33 #define GOTD_CLIENT_TABLE_SIZE (GOTD_MAXCLIENTS * 4)
35 enum gotd_procid {
36 PROC_GOTD = 0,
37 PROC_LISTEN,
38 PROC_AUTH,
39 PROC_SESSION,
40 PROC_REPO_READ,
41 PROC_REPO_WRITE,
42 PROC_MAX,
43 };
45 struct gotd_imsgev {
46 struct imsgbuf ibuf;
47 void (*handler)(int, short, void *);
48 void *handler_arg;
49 struct event ev;
50 short events;
51 };
53 struct gotd_child_proc {
54 pid_t pid;
55 enum gotd_procid type;
56 char repo_name[NAME_MAX];
57 char repo_path[PATH_MAX];
58 int pipe[2];
59 struct gotd_imsgev iev;
60 size_t nhelpers;
61 };
63 enum gotd_access {
64 GOTD_ACCESS_PERMITTED = 1,
65 GOTD_ACCESS_DENIED
66 };
68 struct gotd_access_rule {
69 STAILQ_ENTRY(gotd_access_rule) entry;
71 enum gotd_access access;
73 int authorization;
74 #define GOTD_AUTH_READ 0x1
75 #define GOTD_AUTH_WRITE 0x2
77 char *identifier;
78 };
79 STAILQ_HEAD(gotd_access_rule_list, gotd_access_rule);
81 struct gotd_repo {
82 TAILQ_ENTRY(gotd_repo) entry;
84 char name[NAME_MAX];
85 char path[PATH_MAX];
87 struct gotd_access_rule_list rules;
88 struct got_pathlist_head protected_tag_namespaces;
89 struct got_pathlist_head protected_branch_namespaces;
90 struct got_pathlist_head protected_branches;
91 };
92 TAILQ_HEAD(gotd_repolist, gotd_repo);
94 enum gotd_session_state {
95 GOTD_STATE_EXPECT_LIST_REFS,
96 GOTD_STATE_EXPECT_CAPABILITIES,
97 GOTD_STATE_EXPECT_WANT,
98 GOTD_STATE_EXPECT_REF_UPDATE,
99 GOTD_STATE_EXPECT_MORE_REF_UPDATES,
100 GOTD_STATE_EXPECT_HAVE,
101 GOTD_STATE_EXPECT_PACKFILE,
102 GOTD_STATE_EXPECT_DONE,
103 GOTD_STATE_DONE,
104 };
106 struct gotd_client_capability {
107 char *key;
108 char *value;
109 };
111 struct gotd_object_id_array {
112 struct got_object_id **ids;
113 size_t nalloc;
114 size_t nids;
115 };
117 struct gotd_uid_connection_limit {
118 uid_t uid;
119 int max_connections;
120 };
122 struct gotd {
123 pid_t pid;
124 char unix_socket_path[PATH_MAX];
125 char user_name[32];
126 struct gotd_repolist repos;
127 int nrepos;
128 struct gotd_child_proc listen_proc;
129 struct timeval request_timeout;
130 struct timeval auth_timeout;
131 struct gotd_uid_connection_limit *connection_limits;
132 size_t nconnection_limits;
134 char *argv0;
135 const char *confpath;
136 int daemonize;
137 int verbosity;
138 };
140 enum gotd_imsg_type {
141 /* An error occured while processing a request. */
142 GOTD_IMSG_ERROR,
144 /* Commands used by gotctl(8). */
145 GOTD_IMSG_INFO,
146 GOTD_IMSG_INFO_REPO,
147 GOTD_IMSG_INFO_CLIENT,
148 GOTD_IMSG_STOP,
150 /* Request a list of references. */
151 GOTD_IMSG_LIST_REFS,
152 GOTD_IMSG_LIST_REFS_INTERNAL,
154 /* References. */
155 GOTD_IMSG_REFLIST,
156 GOTD_IMSG_REF,
157 GOTD_IMSG_SYMREF,
159 /* Git protocol capabilities. */
160 GOTD_IMSG_CAPABILITIES,
161 GOTD_IMSG_CAPABILITY,
163 /* Git protocol chatter. */
164 GOTD_IMSG_WANT, /* The client wants an object. */
165 GOTD_IMSG_HAVE, /* The client has an object. */
166 GOTD_IMSG_ACK, /* The server has an object or a reference. */
167 GOTD_IMSG_NAK, /* The server does not have an object/ref. */
168 GOTD_IMSG_REF_UPDATE, /* The client wants to update a reference. */
169 GOTD_IMSG_REF_DELETE, /* The client wants to delete a reference. */
170 GOTD_IMSG_FLUSH, /* The client sent a flush packet. */
171 GOTD_IMSG_DONE, /* The client is done chatting. */
173 /* Sending or receiving a pack file. */
174 GOTD_IMSG_SEND_PACKFILE, /* The server is sending a pack file. */
175 GOTD_IMSG_RECV_PACKFILE, /* The server is receiving a pack file. */
176 GOTD_IMSG_PACKIDX_FILE, /* Temporary file handle for new pack index. */
177 GOTD_IMSG_PACKFILE_PIPE, /* Pipe to send/receive a pack file stream. */
178 GOTD_IMSG_PACKFILE_PROGRESS, /* Progress reporting. */
179 GOTD_IMSG_PACKFILE_READY, /* Pack file is ready to be sent. */
180 GOTD_IMSG_PACKFILE_STATUS, /* Received pack success/failure status. */
181 GOTD_IMSG_PACKFILE_INSTALL, /* Received pack file can be installed. */
182 GOTD_IMSG_PACKFILE_DONE, /* Pack file has been sent/received. */
184 /* Reference updates. */
185 GOTD_IMSG_REF_UPDATES_START, /* Ref updates starting. */
186 GOTD_IMSG_REF_UPDATE_OK, /* Update went OK. */
187 GOTD_IMSG_REF_UPDATE_NG, /* Update was not good. */
188 GOTD_IMSG_REFS_UPDATED, /* The server proccessed all ref updates. */
190 /* Client connections. */
191 GOTD_IMSG_DISCONNECT,
192 GOTD_IMSG_CONNECT,
194 /* Child process management. */
195 GOTD_IMSG_CLIENT_SESSION_READY,
196 GOTD_IMSG_REPO_CHILD_READY,
197 GOTD_IMSG_CONNECT_REPO_CHILD,
199 /* Auth child process. */
200 GOTD_IMSG_AUTHENTICATE,
201 GOTD_IMSG_ACCESS_GRANTED,
202 };
204 /* Structure for GOTD_IMSG_ERROR. */
205 struct gotd_imsg_error {
206 int code; /* an error code from got_error.h */
207 int errno_code; /* in case code equals GOT_ERR_ERRNO */
208 uint32_t client_id;
209 char msg[GOT_ERR_MAX_MSG_SIZE];
210 } __attribute__((__packed__));
212 /* Structure for GOTD_IMSG_INFO. */
213 struct gotd_imsg_info {
214 pid_t pid;
215 int verbosity;
216 int nrepos;
217 int nclients;
219 /* Followed by nrepos GOTD_IMSG_INFO_REPO messages. */
220 /* Followed by nclients GOTD_IMSG_INFO_CLIENT messages. */
221 };
223 /* Structure for GOTD_IMSG_INFO_REPO. */
224 struct gotd_imsg_info_repo {
225 char repo_name[NAME_MAX];
226 char repo_path[PATH_MAX];
227 };
229 /* Structure for GOTD_IMSG_INFO_CLIENT */
230 struct gotd_imsg_info_client {
231 uid_t euid;
232 gid_t egid;
233 char repo_name[NAME_MAX];
234 int is_writing;
235 pid_t session_child_pid;
236 pid_t repo_child_pid;
237 };
239 /* Structure for GOTD_IMSG_LIST_REFS. */
240 struct gotd_imsg_list_refs {
241 char repo_name[NAME_MAX];
242 int client_is_reading; /* 1 if reading, 0 if writing */
243 };
245 /* Structure for GOTD_IMSG_LIST_REFS_INTERNAL. */
246 struct gotd_imsg_list_refs_internal {
247 uint32_t client_id;
248 };
250 /* Structure for GOTD_IMSG_REFLIST. */
251 struct gotd_imsg_reflist {
252 size_t nrefs;
254 /* Followed by nrefs times of gotd_imsg_ref/gotd_imsg_symref data. */
255 } __attribute__((__packed__));
257 /* Structure for GOTD_IMSG_REF data. */
258 struct gotd_imsg_ref {
259 uint8_t id[SHA1_DIGEST_LENGTH];
260 size_t name_len;
261 /* Followed by name_len data bytes. */
262 } __attribute__((__packed__));
264 /* Structure for GOTD_IMSG_SYMREF data. */
265 struct gotd_imsg_symref {
266 size_t name_len;
267 size_t target_len;
268 uint8_t target_id[SHA1_DIGEST_LENGTH];
270 /*
271 * Followed by name_len + target_len data bytes.
272 */
273 } __attribute__((__packed__));
275 /* Structure for GOTD_IMSG_CAPABILITIES data. */
276 struct gotd_imsg_capabilities {
277 size_t ncapabilities;
279 /*
280 * Followed by ncapabilities * GOTD_IMSG_CAPABILITY.
281 */
282 } __attribute__((__packed__));
284 /* Structure for GOTD_IMSG_CAPABILITY data. */
285 struct gotd_imsg_capability {
286 size_t key_len;
287 size_t value_len;
289 /*
290 * Followed by key_len + value_len data bytes.
291 */
292 } __attribute__((__packed__));
294 /* Structure for GOTD_IMSG_WANT data. */
295 struct gotd_imsg_want {
296 uint8_t object_id[SHA1_DIGEST_LENGTH];
297 uint32_t client_id;
298 } __attribute__((__packed__));
300 /* Structure for GOTD_IMSG_HAVE data. */
301 struct gotd_imsg_have {
302 uint8_t object_id[SHA1_DIGEST_LENGTH];
303 uint32_t client_id;
304 } __attribute__((__packed__));
306 /* Structure for GOTD_IMSG_ACK data. */
307 struct gotd_imsg_ack {
308 uint8_t object_id[SHA1_DIGEST_LENGTH];
309 uint32_t client_id;
310 } __attribute__((__packed__));
312 /* Structure for GOTD_IMSG_NAK data. */
313 struct gotd_imsg_nak {
314 uint8_t object_id[SHA1_DIGEST_LENGTH];
315 uint32_t client_id;
316 } __attribute__((__packed__));
318 /* Structure for GOTD_IMSG_PACKFILE_STATUS data. */
319 struct gotd_imsg_packfile_status {
320 size_t reason_len;
322 /* Followed by reason_len data bytes. */
323 } __attribute__((__packed__));
326 /* Structure for GOTD_IMSG_REF_UPDATE data. */
327 struct gotd_imsg_ref_update {
328 uint8_t old_id[SHA1_DIGEST_LENGTH];
329 uint8_t new_id[SHA1_DIGEST_LENGTH];
330 int ref_is_new;
331 int delete_ref;
332 uint32_t client_id;
333 size_t name_len;
335 /* Followed by name_len data bytes. */
336 } __attribute__((__packed__));
338 /* Structure for GOTD_IMSG_REF_UPDATES_START data. */
339 struct gotd_imsg_ref_updates_start {
340 int nref_updates;
341 uint32_t client_id;
343 /* Followed by nref_updates GOT_IMSG_REF_UPDATE_OK/NG messages. */
344 };
346 /* Structure for GOTD_IMSG_REF_UPDATE_OK data. */
347 struct gotd_imsg_ref_update_ok {
348 uint8_t old_id[SHA1_DIGEST_LENGTH];
349 uint8_t new_id[SHA1_DIGEST_LENGTH];
350 int ref_is_new;
351 uint32_t client_id;
352 size_t name_len;
354 /* Followed by name_len data bytes. */
355 } __attribute__((__packed__));
357 /* Structure for GOTD_IMSG_REF_UPDATE_NG data. */
358 struct gotd_imsg_ref_update_ng {
359 uint8_t old_id[SHA1_DIGEST_LENGTH];
360 uint8_t new_id[SHA1_DIGEST_LENGTH];
361 uint32_t client_id;
362 size_t name_len;
363 size_t reason_len;
365 /* Followed by name_len + reason_len data bytes. */
366 } __attribute__((__packed__));
368 /* Structure for GOTD_IMSG_SEND_PACKFILE data. */
369 struct gotd_imsg_send_packfile {
370 uint32_t client_id;
371 int report_progress;
373 /* delta cache file is sent as a file descriptor */
375 /* followed by two GOTD_IMSG_PACKFILE_PIPE messages */
376 };
378 /* Structure for GOTD_IMSG_RECV_PACKFILE data. */
379 struct gotd_imsg_recv_packfile {
380 uint32_t client_id;
381 int report_status;
383 /* pack destination temp file is sent as a file descriptor */
384 };
386 /* Structure for GOTD_IMSG_PACKFILE_PIPE data. */
387 struct gotd_imsg_packfile_pipe {
388 uint32_t client_id;
389 };
391 /* Structure for GOTD_IMSG_PACKIDX_FILE data. */
392 struct gotd_imsg_packidx_file {
393 uint32_t client_id;
394 };
397 /*
398 * Structure for GOTD_IMSG_PACKFILE_PROGRESS and
399 * GOTD_IMSG_PACKFILE_READY data.
400 */
401 struct gotd_imsg_packfile_progress {
402 uint32_t client_id;
403 int ncolored;
404 int nfound;
405 int ntrees;
406 off_t packfile_size;
407 int ncommits;
408 int nobj_total;
409 int nobj_deltify;
410 int nobj_written;
411 };
413 /* Structure for GOTD_IMSG_PACKFILE_INSTALL. */
414 struct gotd_imsg_packfile_install {
415 uint32_t client_id;
416 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
417 };
419 /* Structure for GOTD_IMSG_PACKFILE_DONE data. */
420 struct gotd_imsg_packfile_done {
421 uint32_t client_id;
422 };
424 /* Structure for GOTD_IMSG_DISCONNECT data. */
425 struct gotd_imsg_disconnect {
426 uint32_t client_id;
427 };
429 /* Structure for GOTD_IMSG_CONNECT. */
430 struct gotd_imsg_connect {
431 uint32_t client_id;
432 uid_t euid;
433 gid_t egid;
434 };
436 /* Structure for GOTD_IMSG_CONNECT_REPO_CHILD. */
437 struct gotd_imsg_connect_repo_child {
438 uint32_t client_id;
439 enum gotd_procid proc_id;
441 /* repo child imsg pipe is passed via imsg fd */
442 };
444 /* Structure for GOTD_IMSG_AUTHENTICATE. */
445 struct gotd_imsg_auth {
446 uid_t euid;
447 gid_t egid;
448 int required_auth;
449 uint32_t client_id;
450 };
452 int parse_config(const char *, enum gotd_procid, struct gotd *, int);
453 struct gotd_repo *gotd_find_repo_by_name(const char *, struct gotd *);
454 struct gotd_repo *gotd_find_repo_by_path(const char *, struct gotd *);
455 struct gotd_uid_connection_limit *gotd_find_uid_connection_limit(
456 struct gotd_uid_connection_limit *limits, size_t nlimits, uid_t uid);
457 int gotd_parseuid(const char *s, uid_t *uid);
459 /* imsg.c */
460 const struct got_error *gotd_imsg_flush(struct imsgbuf *);
461 const struct got_error *gotd_imsg_recv(struct imsg *, struct imsgbuf *, size_t);
462 const struct got_error *gotd_imsg_poll_recv(struct imsg *, struct imsgbuf *,
463 size_t);
464 const struct got_error *gotd_imsg_recv_error(uint32_t *client_id,
465 struct imsg *imsg);
466 int gotd_imsg_send_error(struct imsgbuf *ibuf, uint32_t, uint32_t,
467 const struct got_error *);
468 int gotd_imsg_send_error_event(struct gotd_imsgev *, uint32_t, uint32_t,
469 const struct got_error *);
470 void gotd_imsg_event_add(struct gotd_imsgev *);
471 int gotd_imsg_compose_event(struct gotd_imsgev *, uint16_t, uint32_t, int,
472 void *, uint16_t);
473 int gotd_imsg_forward(struct gotd_imsgev *, struct imsg *, int);
475 void gotd_imsg_send_ack(struct got_object_id *, struct imsgbuf *,
476 uint32_t, pid_t);
477 void gotd_imsg_send_nak(struct got_object_id *, struct imsgbuf *,
478 uint32_t, pid_t);