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 };
89 TAILQ_HEAD(gotd_repolist, gotd_repo);
91 enum gotd_client_state {
92 GOTD_STATE_EXPECT_LIST_REFS,
93 GOTD_STATE_EXPECT_CAPABILITIES,
94 GOTD_STATE_EXPECT_WANT,
95 GOTD_STATE_EXPECT_REF_UPDATE,
96 GOTD_STATE_EXPECT_MORE_REF_UPDATES,
97 GOTD_STATE_EXPECT_HAVE,
98 GOTD_STATE_EXPECT_PACKFILE,
99 GOTD_STATE_EXPECT_DONE,
100 GOTD_STATE_DONE,
101 };
103 struct gotd_client_capability {
104 char *key;
105 char *value;
106 };
108 struct gotd_object_id_array {
109 struct got_object_id **ids;
110 size_t nalloc;
111 size_t nids;
112 };
114 struct gotd_uid_connection_limit {
115 uid_t uid;
116 int max_connections;
117 };
119 struct gotd {
120 pid_t pid;
121 char unix_socket_path[PATH_MAX];
122 char user_name[32];
123 struct gotd_repolist repos;
124 int nrepos;
125 struct gotd_child_proc listen_proc;
126 struct timeval request_timeout;
127 struct timeval auth_timeout;
128 struct gotd_uid_connection_limit *connection_limits;
129 size_t nconnection_limits;
131 char *argv0;
132 const char *confpath;
133 int daemonize;
134 int verbosity;
135 };
137 enum gotd_imsg_type {
138 /* An error occured while processing a request. */
139 GOTD_IMSG_ERROR,
141 /* Commands used by gotctl(8). */
142 GOTD_IMSG_INFO,
143 GOTD_IMSG_INFO_REPO,
144 GOTD_IMSG_INFO_CLIENT,
145 GOTD_IMSG_STOP,
147 /* Request a list of references. */
148 GOTD_IMSG_LIST_REFS,
149 GOTD_IMSG_LIST_REFS_INTERNAL,
151 /* References. */
152 GOTD_IMSG_REFLIST,
153 GOTD_IMSG_REF,
154 GOTD_IMSG_SYMREF,
156 /* Git protocol capabilities. */
157 GOTD_IMSG_CAPABILITIES,
158 GOTD_IMSG_CAPABILITY,
160 /* Git protocol chatter. */
161 GOTD_IMSG_WANT, /* The client wants an object. */
162 GOTD_IMSG_HAVE, /* The client has an object. */
163 GOTD_IMSG_ACK, /* The server has an object or a reference. */
164 GOTD_IMSG_NAK, /* The server does not have an object/ref. */
165 GOTD_IMSG_REF_UPDATE, /* The client wants to update a reference. */
166 GOTD_IMSG_REF_DELETE, /* The client wants to delete a reference. */
167 GOTD_IMSG_FLUSH, /* The client sent a flush packet. */
168 GOTD_IMSG_DONE, /* The client is done chatting. */
170 /* Sending or receiving a pack file. */
171 GOTD_IMSG_SEND_PACKFILE, /* The server is sending a pack file. */
172 GOTD_IMSG_RECV_PACKFILE, /* The server is receiving a pack file. */
173 GOTD_IMSG_PACKIDX_FILE, /* Temporary file handle for new pack index. */
174 GOTD_IMSG_PACKFILE_PIPE, /* Pipe to send/receive a pack file stream. */
175 GOTD_IMSG_PACKFILE_PROGRESS, /* Progress reporting. */
176 GOTD_IMSG_PACKFILE_READY, /* Pack file is ready to be sent. */
177 GOTD_IMSG_PACKFILE_STATUS, /* Received pack success/failure status. */
178 GOTD_IMSG_PACKFILE_INSTALL, /* Received pack file can be installed. */
179 GOTD_IMSG_PACKFILE_DONE, /* Pack file has been sent/received. */
181 /* Reference updates. */
182 GOTD_IMSG_REF_UPDATES_START, /* Ref updates starting. */
183 GOTD_IMSG_REF_UPDATE_OK, /* Update went OK. */
184 GOTD_IMSG_REF_UPDATE_NG, /* Update was not good. */
185 GOTD_IMSG_REFS_UPDATED, /* The server proccessed all ref updates. */
187 /* Client connections. */
188 GOTD_IMSG_DISCONNECT,
189 GOTD_IMSG_CONNECT,
191 /* Child process management. */
192 GOTD_IMSG_CLIENT_SESSION_READY,
193 GOTD_IMSG_REPO_CHILD_READY,
194 GOTD_IMSG_CONNECT_REPO_CHILD,
196 /* Auth child process. */
197 GOTD_IMSG_AUTHENTICATE,
198 GOTD_IMSG_ACCESS_GRANTED,
199 };
201 /* Structure for GOTD_IMSG_ERROR. */
202 struct gotd_imsg_error {
203 int code; /* an error code from got_error.h */
204 int errno_code; /* in case code equals GOT_ERR_ERRNO */
205 uint32_t client_id;
206 char msg[GOT_ERR_MAX_MSG_SIZE];
207 } __attribute__((__packed__));
209 /* Structure for GOTD_IMSG_INFO. */
210 struct gotd_imsg_info {
211 pid_t pid;
212 int verbosity;
213 int nrepos;
214 int nclients;
216 /* Followed by nrepos GOTD_IMSG_INFO_REPO messages. */
217 /* Followed by nclients GOTD_IMSG_INFO_CLIENT messages. */
218 };
220 /* Structure for GOTD_IMSG_INFO_REPO. */
221 struct gotd_imsg_info_repo {
222 char repo_name[NAME_MAX];
223 char repo_path[PATH_MAX];
224 };
226 /* Structure for GOTD_IMSG_INFO_CLIENT */
227 struct gotd_imsg_info_client {
228 uid_t euid;
229 gid_t egid;
230 char repo_name[NAME_MAX];
231 int is_writing;
232 enum gotd_client_state state;
233 pid_t session_child_pid;
234 pid_t repo_child_pid;
235 size_t ncapabilities;
237 /* Followed by ncapabilities GOTD_IMSG_CAPABILITY. */
238 };
240 /* Structure for GOTD_IMSG_LIST_REFS. */
241 struct gotd_imsg_list_refs {
242 char repo_name[NAME_MAX];
243 int client_is_reading; /* 1 if reading, 0 if writing */
244 };
246 /* Structure for GOTD_IMSG_LIST_REFS_INTERNAL. */
247 struct gotd_imsg_list_refs_internal {
248 uint32_t client_id;
249 };
251 /* Structure for GOTD_IMSG_REFLIST. */
252 struct gotd_imsg_reflist {
253 size_t nrefs;
255 /* Followed by nrefs times of gotd_imsg_ref/gotd_imsg_symref data. */
256 } __attribute__((__packed__));
258 /* Structure for GOTD_IMSG_REF data. */
259 struct gotd_imsg_ref {
260 uint8_t id[SHA1_DIGEST_LENGTH];
261 size_t name_len;
262 /* Followed by name_len data bytes. */
263 } __attribute__((__packed__));
265 /* Structure for GOTD_IMSG_SYMREF data. */
266 struct gotd_imsg_symref {
267 size_t name_len;
268 size_t target_len;
269 uint8_t target_id[SHA1_DIGEST_LENGTH];
271 /*
272 * Followed by name_len + target_len data bytes.
273 */
274 } __attribute__((__packed__));
276 /* Structure for GOTD_IMSG_CAPABILITIES data. */
277 struct gotd_imsg_capabilities {
278 size_t ncapabilities;
280 /*
281 * Followed by ncapabilities * GOTD_IMSG_CAPABILITY.
282 */
283 } __attribute__((__packed__));
285 /* Structure for GOTD_IMSG_CAPABILITY data. */
286 struct gotd_imsg_capability {
287 size_t key_len;
288 size_t value_len;
290 /*
291 * Followed by key_len + value_len data bytes.
292 */
293 } __attribute__((__packed__));
295 /* Structure for GOTD_IMSG_WANT data. */
296 struct gotd_imsg_want {
297 uint8_t object_id[SHA1_DIGEST_LENGTH];
298 uint32_t client_id;
299 } __attribute__((__packed__));
301 /* Structure for GOTD_IMSG_HAVE data. */
302 struct gotd_imsg_have {
303 uint8_t object_id[SHA1_DIGEST_LENGTH];
304 uint32_t client_id;
305 } __attribute__((__packed__));
307 /* Structure for GOTD_IMSG_ACK data. */
308 struct gotd_imsg_ack {
309 uint8_t object_id[SHA1_DIGEST_LENGTH];
310 uint32_t client_id;
311 } __attribute__((__packed__));
313 /* Structure for GOTD_IMSG_NAK data. */
314 struct gotd_imsg_nak {
315 uint8_t object_id[SHA1_DIGEST_LENGTH];
316 uint32_t client_id;
317 } __attribute__((__packed__));
319 /* Structure for GOTD_IMSG_PACKFILE_STATUS data. */
320 struct gotd_imsg_packfile_status {
321 size_t reason_len;
323 /* Followed by reason_len data bytes. */
324 } __attribute__((__packed__));
327 /* Structure for GOTD_IMSG_REF_UPDATE data. */
328 struct gotd_imsg_ref_update {
329 uint8_t old_id[SHA1_DIGEST_LENGTH];
330 uint8_t new_id[SHA1_DIGEST_LENGTH];
331 int ref_is_new;
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 *);
454 /* imsg.c */
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 *,
458 size_t);
459 const struct got_error *gotd_imsg_recv_error(uint32_t *client_id,
460 struct imsg *imsg);
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,
467 void *, uint16_t);
468 int gotd_imsg_forward(struct gotd_imsgev *, struct imsg *, int);
470 void gotd_imsg_send_ack(struct got_object_id *, struct imsgbuf *,
471 uint32_t, pid_t);
472 void gotd_imsg_send_nak(struct got_object_id *, struct imsgbuf *,
473 uint32_t, pid_t);