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 */
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)
36 enum gotd_procid {
37 PROC_GOTD = 0,
38 PROC_LISTEN,
39 PROC_AUTH,
40 PROC_SESSION_READ,
41 PROC_SESSION_WRITE,
42 PROC_REPO_READ,
43 PROC_REPO_WRITE,
44 PROC_GITWRAPPER,
45 PROC_MAX,
46 };
48 struct gotd_imsgev {
49 struct imsgbuf ibuf;
50 void (*handler)(int, short, void *);
51 void *handler_arg;
52 struct event ev;
53 short events;
54 };
56 enum gotd_access {
57 GOTD_ACCESS_PERMITTED = 1,
58 GOTD_ACCESS_DENIED
59 };
61 struct gotd_access_rule {
62 STAILQ_ENTRY(gotd_access_rule) entry;
64 enum gotd_access access;
66 int authorization;
67 #define GOTD_AUTH_READ 0x1
68 #define GOTD_AUTH_WRITE 0x2
70 char *identifier;
71 };
72 STAILQ_HEAD(gotd_access_rule_list, gotd_access_rule);
74 struct gotd_repo {
75 TAILQ_ENTRY(gotd_repo) entry;
77 char name[NAME_MAX];
78 char path[PATH_MAX];
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;
84 };
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,
96 GOTD_STATE_DONE,
97 };
99 struct gotd_client_capability {
100 char *key;
101 char *value;
102 };
104 struct gotd_object_id_array {
105 struct got_object_id **ids;
106 size_t nalloc;
107 size_t nids;
108 };
110 struct gotd_uid_connection_limit {
111 uid_t uid;
112 int max_connections;
113 };
115 struct gotd_child_proc;
117 struct gotd {
118 pid_t pid;
119 char unix_socket_path[PATH_MAX];
120 char user_name[32];
121 struct gotd_repolist repos;
122 int nrepos;
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;
129 char *argv0;
130 const char *confpath;
131 int daemonize;
132 int verbosity;
133 };
135 enum gotd_imsg_type {
136 /* An error occured while processing a request. */
137 GOTD_IMSG_ERROR,
139 /* Commands used by gotctl(8). */
140 GOTD_IMSG_INFO,
141 GOTD_IMSG_INFO_REPO,
142 GOTD_IMSG_INFO_CLIENT,
143 GOTD_IMSG_STOP,
145 /* Request a list of references. */
146 GOTD_IMSG_LIST_REFS,
147 GOTD_IMSG_LIST_REFS_INTERNAL,
149 /* References. */
150 GOTD_IMSG_REFLIST,
151 GOTD_IMSG_REF,
152 GOTD_IMSG_SYMREF,
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,
187 GOTD_IMSG_CONNECT,
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,
197 };
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 */
203 uint32_t client_id;
204 char msg[GOT_ERR_MAX_MSG_SIZE];
205 } __attribute__((__packed__));
207 /* Structure for GOTD_IMSG_INFO. */
208 struct gotd_imsg_info {
209 pid_t pid;
210 int verbosity;
211 int nrepos;
212 int nclients;
214 /* Followed by nrepos GOTD_IMSG_INFO_REPO messages. */
215 /* Followed by nclients GOTD_IMSG_INFO_CLIENT messages. */
216 };
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];
222 };
224 /* Structure for GOTD_IMSG_INFO_CLIENT */
225 struct gotd_imsg_info_client {
226 uid_t euid;
227 gid_t egid;
228 char repo_name[NAME_MAX];
229 int is_writing;
230 pid_t session_child_pid;
231 pid_t repo_child_pid;
232 };
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 */
238 };
240 /* Structure for GOTD_IMSG_LIST_REFS_INTERNAL. */
241 struct gotd_imsg_list_refs_internal {
242 uint32_t client_id;
243 };
245 /* Structure for GOTD_IMSG_REFLIST. */
246 struct gotd_imsg_reflist {
247 size_t nrefs;
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];
255 size_t name_len;
256 /* Followed by name_len data bytes. */
257 } __attribute__((__packed__));
259 /* Structure for GOTD_IMSG_SYMREF data. */
260 struct gotd_imsg_symref {
261 size_t name_len;
262 size_t target_len;
263 uint8_t target_id[SHA1_DIGEST_LENGTH];
265 /*
266 * Followed by name_len + target_len data bytes.
267 */
268 } __attribute__((__packed__));
270 /* Structure for GOTD_IMSG_CAPABILITIES data. */
271 struct gotd_imsg_capabilities {
272 size_t ncapabilities;
274 /*
275 * Followed by ncapabilities * GOTD_IMSG_CAPABILITY.
276 */
277 } __attribute__((__packed__));
279 /* Structure for GOTD_IMSG_CAPABILITY data. */
280 struct gotd_imsg_capability {
281 size_t key_len;
282 size_t value_len;
284 /*
285 * Followed by key_len + value_len data bytes.
286 */
287 } __attribute__((__packed__));
289 /* Structure for GOTD_IMSG_WANT data. */
290 struct gotd_imsg_want {
291 uint8_t object_id[SHA1_DIGEST_LENGTH];
292 uint32_t client_id;
293 } __attribute__((__packed__));
295 /* Structure for GOTD_IMSG_HAVE data. */
296 struct gotd_imsg_have {
297 uint8_t object_id[SHA1_DIGEST_LENGTH];
298 uint32_t client_id;
299 } __attribute__((__packed__));
301 /* Structure for GOTD_IMSG_ACK data. */
302 struct gotd_imsg_ack {
303 uint8_t object_id[SHA1_DIGEST_LENGTH];
304 uint32_t client_id;
305 } __attribute__((__packed__));
307 /* Structure for GOTD_IMSG_NAK data. */
308 struct gotd_imsg_nak {
309 uint8_t object_id[SHA1_DIGEST_LENGTH];
310 uint32_t client_id;
311 } __attribute__((__packed__));
313 /* Structure for GOTD_IMSG_PACKFILE_STATUS data. */
314 struct gotd_imsg_packfile_status {
315 size_t reason_len;
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];
325 int ref_is_new;
326 int delete_ref;
327 uint32_t client_id;
328 size_t name_len;
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 {
335 int nref_updates;
336 uint32_t client_id;
338 /* Followed by nref_updates GOT_IMSG_REF_UPDATE_OK/NG messages. */
339 };
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];
345 int ref_is_new;
346 uint32_t client_id;
347 size_t name_len;
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];
356 uint32_t client_id;
357 size_t name_len;
358 size_t reason_len;
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 {
365 uint32_t client_id;
366 int report_progress;
368 /* delta cache file is sent as a file descriptor */
370 /* followed by two GOTD_IMSG_PACKFILE_PIPE messages */
371 };
373 /* Structure for GOTD_IMSG_RECV_PACKFILE data. */
374 struct gotd_imsg_recv_packfile {
375 uint32_t client_id;
376 int report_status;
378 /* pack destination temp file is sent as a file descriptor */
379 };
381 /* Structure for GOTD_IMSG_PACKFILE_PIPE data. */
382 struct gotd_imsg_packfile_pipe {
383 uint32_t client_id;
384 };
386 /* Structure for GOTD_IMSG_PACKIDX_FILE data. */
387 struct gotd_imsg_packidx_file {
388 uint32_t client_id;
389 };
392 /*
393 * Structure for GOTD_IMSG_PACKFILE_PROGRESS and
394 * GOTD_IMSG_PACKFILE_READY data.
395 */
396 struct gotd_imsg_packfile_progress {
397 uint32_t client_id;
398 int ncolored;
399 int nfound;
400 int ntrees;
401 off_t packfile_size;
402 int ncommits;
403 int nobj_total;
404 int nobj_deltify;
405 int nobj_written;
406 };
408 /* Structure for GOTD_IMSG_PACKFILE_INSTALL. */
409 struct gotd_imsg_packfile_install {
410 uint32_t client_id;
411 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
412 };
414 /* Structure for GOTD_IMSG_PACKFILE_DONE data. */
415 struct gotd_imsg_packfile_done {
416 uint32_t client_id;
417 };
419 /* Structure for GOTD_IMSG_DISCONNECT data. */
420 struct gotd_imsg_disconnect {
421 uint32_t client_id;
422 };
424 /* Structure for GOTD_IMSG_CONNECT. */
425 struct gotd_imsg_connect {
426 uint32_t client_id;
427 uid_t euid;
428 gid_t egid;
429 };
431 /* Structure for GOTD_IMSG_CONNECT_REPO_CHILD. */
432 struct gotd_imsg_connect_repo_child {
433 uint32_t client_id;
434 enum gotd_procid proc_id;
436 /* repo child imsg pipe is passed via imsg fd */
437 };
439 /* Structure for GOTD_IMSG_AUTHENTICATE. */
440 struct gotd_imsg_auth {
441 uid_t euid;
442 gid_t egid;
443 int required_auth;
444 uint32_t client_id;
445 };
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);
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);