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 #ifndef GOT_LIBEXECDIR
25 #define GOT_LIBEXECDIR /usr/libexec
26 #endif
28 #define GOTD_STRINGIFY(x) #x
29 #define GOTD_STRINGVAL(x) GOTD_STRINGIFY(x)
31 #define GOTD_PROG_NOTIFY_EMAIL got-notify-email
32 #define GOTD_PROG_NOTIFY_HTTP got-notify-http
34 #define GOTD_PATH_PROG_NOTIFY_EMAIL \
35 GOTD_STRINGVAL(GOT_LIBEXECDIR) "/" \
36 GOTD_STRINGVAL(GOTD_PROG_NOTIFY_EMAIL)
37 #define GOTD_PATH_PROG_NOTIFY_HTTP \
38 GOTD_STRINGVAL(GOT_LIBEXECDIR) "/" \
39 GOTD_STRINGVAL(GOTD_PROG_NOTIFY_HTTP)
41 #define GOTD_MAXCLIENTS 1024
42 #define GOTD_MAX_CONN_PER_UID 4
43 #define GOTD_FD_RESERVE 5
44 #define GOTD_FD_NEEDED 6
45 #define GOTD_FILENO_MSG_PIPE 3
47 #define GOTD_DEFAULT_REQUEST_TIMEOUT 3600
49 /* Client hash tables need some extra room. */
50 #define GOTD_CLIENT_TABLE_SIZE (GOTD_MAXCLIENTS * 4)
52 enum gotd_procid {
53 PROC_GOTD = 0,
54 PROC_LISTEN,
55 PROC_AUTH,
56 PROC_SESSION_READ,
57 PROC_SESSION_WRITE,
58 PROC_REPO_READ,
59 PROC_REPO_WRITE,
60 PROC_GITWRAPPER,
61 PROC_NOTIFY,
62 PROC_MAX,
63 };
65 struct gotd_imsgev {
66 struct imsgbuf ibuf;
67 void (*handler)(int, short, void *);
68 void *handler_arg;
69 struct event ev;
70 short events;
71 };
73 enum gotd_access {
74 GOTD_ACCESS_PERMITTED = 1,
75 GOTD_ACCESS_DENIED
76 };
78 struct gotd_access_rule {
79 STAILQ_ENTRY(gotd_access_rule) entry;
81 enum gotd_access access;
83 int authorization;
84 #define GOTD_AUTH_READ 0x1
85 #define GOTD_AUTH_WRITE 0x2
87 char *identifier;
88 };
89 STAILQ_HEAD(gotd_access_rule_list, gotd_access_rule);
91 enum gotd_notification_target_type {
92 GOTD_NOTIFICATION_VIA_EMAIL,
93 GOTD_NOTIFICATION_VIA_HTTP
94 };
96 struct gotd_notification_target {
97 STAILQ_ENTRY(gotd_notification_target) entry;
99 enum gotd_notification_target_type type;
100 union {
101 struct {
102 char *sender;
103 char *recipient;
104 char *responder;
105 char *hostname;
106 char *port;
107 } email;
108 struct {
109 int tls;
110 char *hostname;
111 char *port;
112 char *path;
113 char *user;
114 char *password;
115 } http;
116 } conf;
117 };
118 STAILQ_HEAD(gotd_notification_targets, gotd_notification_target);
120 struct gotd_repo {
121 TAILQ_ENTRY(gotd_repo) entry;
123 char name[NAME_MAX];
124 char path[PATH_MAX];
126 struct gotd_access_rule_list rules;
127 struct got_pathlist_head protected_tag_namespaces;
128 struct got_pathlist_head protected_branch_namespaces;
129 struct got_pathlist_head protected_branches;
131 struct got_pathlist_head notification_refs;
132 struct got_pathlist_head notification_ref_namespaces;
133 struct gotd_notification_targets notification_targets;
134 };
135 TAILQ_HEAD(gotd_repolist, gotd_repo);
137 struct gotd_client_capability {
138 char *key;
139 char *value;
140 };
142 struct gotd_object_id_array {
143 struct got_object_id **ids;
144 size_t nalloc;
145 size_t nids;
146 };
148 struct gotd_uid_connection_limit {
149 uid_t uid;
150 int max_connections;
151 };
153 struct gotd_child_proc;
155 struct gotd {
156 pid_t pid;
157 char unix_socket_path[PATH_MAX];
158 char user_name[32];
159 struct gotd_repolist repos;
160 int nrepos;
161 struct gotd_child_proc *listen_proc;
162 struct gotd_child_proc *notify_proc;
163 int notifications_enabled;
164 struct timeval request_timeout;
165 struct timeval auth_timeout;
166 struct gotd_uid_connection_limit *connection_limits;
167 size_t nconnection_limits;
169 char *argv0;
170 const char *confpath;
171 int daemonize;
172 int verbosity;
173 };
175 enum gotd_imsg_type {
176 /* An error occured while processing a request. */
177 GOTD_IMSG_ERROR,
179 /* Commands used by gotctl(8). */
180 GOTD_IMSG_INFO,
181 GOTD_IMSG_INFO_REPO,
182 GOTD_IMSG_INFO_CLIENT,
183 GOTD_IMSG_STOP,
185 /* Request a list of references. */
186 GOTD_IMSG_LIST_REFS,
187 GOTD_IMSG_LIST_REFS_INTERNAL,
189 /* References. */
190 GOTD_IMSG_REFLIST,
191 GOTD_IMSG_REF,
192 GOTD_IMSG_SYMREF,
194 /* Git protocol capabilities. */
195 GOTD_IMSG_CAPABILITIES,
196 GOTD_IMSG_CAPABILITY,
198 /* Git protocol chatter. */
199 GOTD_IMSG_WANT, /* The client wants an object. */
200 GOTD_IMSG_HAVE, /* The client has an object. */
201 GOTD_IMSG_ACK, /* The server has an object or a reference. */
202 GOTD_IMSG_NAK, /* The server does not have an object/ref. */
203 GOTD_IMSG_REF_UPDATE, /* The client wants to update a reference. */
204 GOTD_IMSG_REF_DELETE, /* The client wants to delete a reference. */
205 GOTD_IMSG_FLUSH, /* The client sent a flush packet. */
206 GOTD_IMSG_DONE, /* The client is done chatting. */
208 /* Sending or receiving a pack file. */
209 GOTD_IMSG_SEND_PACKFILE, /* The server is sending a pack file. */
210 GOTD_IMSG_RECV_PACKFILE, /* The server is receiving a pack file. */
211 GOTD_IMSG_PACKIDX_FILE, /* Temporary file handle for new pack index. */
212 GOTD_IMSG_PACKFILE_PIPE, /* Pipe to send/receive a pack file stream. */
213 GOTD_IMSG_PACKFILE_PROGRESS, /* Progress reporting. */
214 GOTD_IMSG_PACKFILE_READY, /* Pack file is ready to be sent. */
215 GOTD_IMSG_PACKFILE_STATUS, /* Received pack success/failure status. */
216 GOTD_IMSG_PACKFILE_INSTALL, /* Received pack file can be installed. */
217 GOTD_IMSG_PACKFILE_DONE, /* Pack file has been sent/received. */
219 /* Reference updates. */
220 GOTD_IMSG_REF_UPDATES_START, /* Ref updates starting. */
221 GOTD_IMSG_REF_UPDATE_OK, /* Update went OK. */
222 GOTD_IMSG_REF_UPDATE_NG, /* Update was not good. */
223 GOTD_IMSG_REFS_UPDATED, /* The server proccessed all ref updates. */
225 /* Client connections. */
226 GOTD_IMSG_DISCONNECT,
227 GOTD_IMSG_CONNECT,
229 /* Child process management. */
230 GOTD_IMSG_CLIENT_SESSION_READY,
231 GOTD_IMSG_REPO_CHILD_READY,
232 GOTD_IMSG_CONNECT_REPO_CHILD,
234 /* Auth child process. */
235 GOTD_IMSG_AUTHENTICATE,
236 GOTD_IMSG_ACCESS_GRANTED,
238 /* Notify child process. */
239 GOTD_IMSG_CONNECT_NOTIFIER,
240 GOTD_IMSG_CONNECT_SESSION,
241 GOTD_IMSG_NOTIFY,
242 GOTD_IMSG_NOTIFICATION_SENT
243 };
245 /* Structure for GOTD_IMSG_ERROR. */
246 struct gotd_imsg_error {
247 int code; /* an error code from got_error.h */
248 int errno_code; /* in case code equals GOT_ERR_ERRNO */
249 uint32_t client_id;
250 char msg[GOT_ERR_MAX_MSG_SIZE];
251 } __attribute__((__packed__));
253 /* Structure for GOTD_IMSG_INFO. */
254 struct gotd_imsg_info {
255 pid_t pid;
256 int verbosity;
257 int nrepos;
258 int nclients;
260 /* Followed by nrepos GOTD_IMSG_INFO_REPO messages. */
261 /* Followed by nclients GOTD_IMSG_INFO_CLIENT messages. */
262 };
264 /* Structure for GOTD_IMSG_INFO_REPO. */
265 struct gotd_imsg_info_repo {
266 char repo_name[NAME_MAX];
267 char repo_path[PATH_MAX];
268 };
270 /* Structure for GOTD_IMSG_INFO_CLIENT */
271 struct gotd_imsg_info_client {
272 uid_t euid;
273 gid_t egid;
274 char repo_name[NAME_MAX];
275 int is_writing;
276 pid_t session_child_pid;
277 pid_t repo_child_pid;
278 };
280 /* Structure for GOTD_IMSG_LIST_REFS. */
281 struct gotd_imsg_list_refs {
282 char repo_name[NAME_MAX];
283 int client_is_reading; /* 1 if reading, 0 if writing */
284 };
286 /* Structure for GOTD_IMSG_REFLIST. */
287 struct gotd_imsg_reflist {
288 size_t nrefs;
290 /* Followed by nrefs times of gotd_imsg_ref/gotd_imsg_symref data. */
291 } __attribute__((__packed__));
293 /* Structure for GOTD_IMSG_REF data. */
294 struct gotd_imsg_ref {
295 uint8_t id[SHA1_DIGEST_LENGTH];
296 size_t name_len;
297 /* Followed by name_len data bytes. */
298 } __attribute__((__packed__));
300 /* Structure for GOTD_IMSG_SYMREF data. */
301 struct gotd_imsg_symref {
302 size_t name_len;
303 size_t target_len;
304 uint8_t target_id[SHA1_DIGEST_LENGTH];
306 /*
307 * Followed by name_len + target_len data bytes.
308 */
309 } __attribute__((__packed__));
311 /* Structure for GOTD_IMSG_CAPABILITIES data. */
312 struct gotd_imsg_capabilities {
313 size_t ncapabilities;
315 /*
316 * Followed by ncapabilities * GOTD_IMSG_CAPABILITY.
317 */
318 } __attribute__((__packed__));
320 /* Structure for GOTD_IMSG_CAPABILITY data. */
321 struct gotd_imsg_capability {
322 size_t key_len;
323 size_t value_len;
325 /*
326 * Followed by key_len + value_len data bytes.
327 */
328 } __attribute__((__packed__));
330 /* Structure for GOTD_IMSG_WANT data. */
331 struct gotd_imsg_want {
332 uint8_t object_id[SHA1_DIGEST_LENGTH];
333 } __attribute__((__packed__));
335 /* Structure for GOTD_IMSG_HAVE data. */
336 struct gotd_imsg_have {
337 uint8_t object_id[SHA1_DIGEST_LENGTH];
338 } __attribute__((__packed__));
340 /* Structure for GOTD_IMSG_ACK data. */
341 struct gotd_imsg_ack {
342 uint8_t object_id[SHA1_DIGEST_LENGTH];
343 } __attribute__((__packed__));
345 /* Structure for GOTD_IMSG_NAK data. */
346 struct gotd_imsg_nak {
347 uint8_t object_id[SHA1_DIGEST_LENGTH];
348 } __attribute__((__packed__));
350 /* Structure for GOTD_IMSG_PACKFILE_STATUS data. */
351 struct gotd_imsg_packfile_status {
352 size_t reason_len;
354 /* Followed by reason_len data bytes. */
355 } __attribute__((__packed__));
358 /* Structure for GOTD_IMSG_REF_UPDATE data. */
359 struct gotd_imsg_ref_update {
360 uint8_t old_id[SHA1_DIGEST_LENGTH];
361 uint8_t new_id[SHA1_DIGEST_LENGTH];
362 int ref_is_new;
363 int delete_ref;
364 size_t name_len;
366 /* Followed by name_len data bytes. */
367 } __attribute__((__packed__));
369 /* Structure for GOTD_IMSG_REF_UPDATES_START data. */
370 struct gotd_imsg_ref_updates_start {
371 int nref_updates;
373 /* Followed by nref_updates GOT_IMSG_REF_UPDATE_OK/NG messages. */
374 };
376 /* Structure for GOTD_IMSG_REF_UPDATE_OK data. */
377 struct gotd_imsg_ref_update_ok {
378 uint8_t old_id[SHA1_DIGEST_LENGTH];
379 uint8_t new_id[SHA1_DIGEST_LENGTH];
380 int ref_is_new;
381 size_t name_len;
383 /* Followed by name_len data bytes. */
384 } __attribute__((__packed__));
386 /* Structure for GOTD_IMSG_REF_UPDATE_NG data. */
387 struct gotd_imsg_ref_update_ng {
388 uint8_t old_id[SHA1_DIGEST_LENGTH];
389 uint8_t new_id[SHA1_DIGEST_LENGTH];
390 size_t name_len;
391 size_t reason_len;
393 /* Followed by name_len + reason_len data bytes. */
394 } __attribute__((__packed__));
396 /* Structure for GOTD_IMSG_SEND_PACKFILE data. */
397 struct gotd_imsg_send_packfile {
398 int report_progress;
400 /* delta cache file is sent as a file descriptor */
402 /* followed by two GOTD_IMSG_PACKFILE_PIPE messages */
403 };
405 /* Structure for GOTD_IMSG_RECV_PACKFILE data. */
406 struct gotd_imsg_recv_packfile {
407 int report_status;
409 /* pack destination temp file is sent as a file descriptor */
410 };
412 /*
413 * Structure for GOTD_IMSG_PACKFILE_PROGRESS and
414 * GOTD_IMSG_PACKFILE_READY data.
415 */
416 struct gotd_imsg_packfile_progress {
417 int ncolored;
418 int nfound;
419 int ntrees;
420 off_t packfile_size;
421 int ncommits;
422 int nobj_total;
423 int nobj_deltify;
424 int nobj_written;
425 };
427 /* Structure for GOTD_IMSG_PACKFILE_INSTALL. */
428 struct gotd_imsg_packfile_install {
429 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
430 };
432 /* Structure for GOTD_IMSG_DISCONNECT data. */
433 struct gotd_imsg_disconnect {
434 uint32_t client_id;
435 };
437 /* Structure for GOTD_IMSG_CONNECT. */
438 struct gotd_imsg_connect {
439 uint32_t client_id;
440 uid_t euid;
441 gid_t egid;
442 size_t username_len;
444 /* Followed by username_len data bytes. */
445 };
447 /* Structure for GOTD_IMSG_CONNECT_REPO_CHILD. */
448 struct gotd_imsg_connect_repo_child {
449 enum gotd_procid proc_id;
451 /* repo child imsg pipe is passed via imsg fd */
452 };
454 /* Structure for GOTD_IMSG_AUTHENTICATE. */
455 struct gotd_imsg_auth {
456 uid_t euid;
457 gid_t egid;
458 int required_auth;
459 uint32_t client_id;
460 };
462 /* Structures for GOTD_IMSG_NOTIFY. */
463 enum gotd_notification_action {
464 GOTD_NOTIF_ACTION_CREATED,
465 GOTD_NOTIF_ACTION_REMOVED,
466 GOTD_NOTIF_ACTION_CHANGED
467 };
468 /* IMSG_NOTIFY session <-> repo_write */
469 struct gotd_imsg_notification_content {
470 enum gotd_notification_action action;
471 uint8_t old_id[SHA1_DIGEST_LENGTH];
472 uint8_t new_id[SHA1_DIGEST_LENGTH];
473 size_t refname_len;
474 /* Followed by refname_len data bytes. */
475 };
476 /* IMSG_NOTIFY session -> notify*/
477 struct gotd_imsg_notify {
478 char repo_name[NAME_MAX];
479 char subject_line[64];
480 };
482 int parse_config(const char *, enum gotd_procid, struct gotd *);
483 struct gotd_repo *gotd_find_repo_by_name(const char *, struct gotd_repolist *);
484 struct gotd_repo *gotd_find_repo_by_path(const char *, struct gotd *);
485 struct gotd_uid_connection_limit *gotd_find_uid_connection_limit(
486 struct gotd_uid_connection_limit *limits, size_t nlimits, uid_t uid);
487 int gotd_parseuid(const char *s, uid_t *uid);
488 const struct got_error *gotd_parse_url(char **, char **, char **,
489 char **, const char *);
491 /* imsg.c */
492 const struct got_error *gotd_imsg_flush(struct imsgbuf *);
493 const struct got_error *gotd_imsg_recv(struct imsg *, struct imsgbuf *, size_t);
494 const struct got_error *gotd_imsg_poll_recv(struct imsg *, struct imsgbuf *,
495 size_t);
496 const struct got_error *gotd_imsg_recv_error(uint32_t *client_id,
497 struct imsg *imsg);
498 int gotd_imsg_send_error(struct imsgbuf *ibuf, uint32_t, uint32_t,
499 const struct got_error *);
500 int gotd_imsg_send_error_event(struct gotd_imsgev *, uint32_t, uint32_t,
501 const struct got_error *);
502 void gotd_imsg_event_add(struct gotd_imsgev *);
503 int gotd_imsg_compose_event(struct gotd_imsgev *, uint16_t, uint32_t, int,
504 void *, uint16_t);
505 int gotd_imsg_forward(struct gotd_imsgev *, struct imsg *, int);
507 void gotd_imsg_send_ack(struct got_object_id *, struct imsgbuf *,
508 uint32_t, pid_t);
509 void gotd_imsg_send_nak(struct got_object_id *, struct imsgbuf *,
510 uint32_t, pid_t);