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_UNIX_GROUP "_gotsh"
21 #define GOTD_USER "_gotd"
22 #define GOTD_CONF_PATH "/etc/gotd.conf"
24 #define GOTD_MAXCLIENTS 1024
25 #define GOTD_FD_RESERVE 5
26 #define GOTD_FD_NEEDED 6
27 #define GOTD_SOCK_FILENO 3
29 /* Client hash tables need some extra room. */
30 #define GOTD_CLIENT_TABLE_SIZE (GOTD_MAXCLIENTS * 4)
32 enum gotd_procid {
33 PROC_GOTD = 0,
34 PROC_REPO_READ,
35 PROC_REPO_WRITE,
36 PROC_MAX,
37 };
39 struct gotd_imsgev {
40 struct imsgbuf ibuf;
41 void (*handler)(int, short, void *);
42 void *handler_arg;
43 struct event ev;
44 short events;
45 };
47 struct gotd_child_proc {
48 pid_t pid;
49 enum gotd_procid type;
50 char repo_name[NAME_MAX];
51 char chroot_path[PATH_MAX];
52 int pipe[2];
53 struct gotd_imsgev iev;
54 size_t nhelpers;
55 };
57 enum gotd_access {
58 GOTD_ACCESS_PERMITTED = 1,
59 GOTD_ACCESS_DENIED
60 };
62 struct gotd_access_rule {
63 STAILQ_ENTRY(gotd_access_rule) entry;
65 enum gotd_access access;
67 int authorization;
68 #define GOTD_AUTH_READ 0x1
69 #define GOTD_AUTH_WRITE 0x2
71 char *identifier;
72 };
73 STAILQ_HEAD(gotd_access_rule_list, gotd_access_rule);
75 struct gotd_repo {
76 TAILQ_ENTRY(gotd_repo) entry;
78 char name[NAME_MAX];
79 char path[PATH_MAX];
81 struct gotd_access_rule_list rules;
82 };
83 TAILQ_HEAD(gotd_repolist, gotd_repo);
85 enum gotd_client_state {
86 GOTD_STATE_EXPECT_LIST_REFS,
87 GOTD_STATE_EXPECT_CAPABILITIES,
88 GOTD_STATE_EXPECT_WANT,
89 GOTD_STATE_EXPECT_REF_UPDATE,
90 GOTD_STATE_EXPECT_MORE_REF_UPDATES,
91 GOTD_STATE_EXPECT_HAVE,
92 GOTD_STATE_EXPECT_PACKFILE,
93 GOTD_STATE_EXPECT_DONE,
94 GOTD_STATE_DONE,
95 };
97 struct gotd_client_capability {
98 char *key;
99 char *value;
100 };
102 struct gotd_object_id_array {
103 struct got_object_id **ids;
104 size_t nalloc;
105 size_t nids;
106 };
108 struct gotd {
109 pid_t pid;
110 char unix_socket_path[PATH_MAX];
111 char unix_group_name[32];
112 char user_name[32];
113 struct gotd_repolist repos;
114 int nrepos;
115 int verbosity;
116 struct event ev;
117 struct event pause;
118 struct gotd_child_proc *procs;
119 int nprocs;
120 };
122 enum gotd_imsg_type {
123 /* An error occured while processing a request. */
124 GOTD_IMSG_ERROR,
126 /* Commands used by gotctl(8). */
127 GOTD_IMSG_INFO,
128 GOTD_IMSG_INFO_REPO,
129 GOTD_IMSG_INFO_CLIENT,
130 GOTD_IMSG_STOP,
132 /* Request a list of references. */
133 GOTD_IMSG_LIST_REFS,
134 GOTD_IMSG_LIST_REFS_INTERNAL,
136 /* References. */
137 GOTD_IMSG_REFLIST,
138 GOTD_IMSG_REF,
139 GOTD_IMSG_SYMREF,
141 /* Git protocol capabilities. */
142 GOTD_IMSG_CAPABILITIES,
143 GOTD_IMSG_CAPABILITY,
145 /* Git protocol chatter. */
146 GOTD_IMSG_WANT, /* The client wants an object. */
147 GOTD_IMSG_HAVE, /* The client has an object. */
148 GOTD_IMSG_ACK, /* The server has an object or a reference. */
149 GOTD_IMSG_NAK, /* The server does not have an object/ref. */
150 GOTD_IMSG_REF_UPDATE, /* The client wants to update a reference. */
151 GOTD_IMSG_REF_DELETE, /* The client wants to delete a reference. */
152 GOTD_IMSG_FLUSH, /* The client sent a flush packet. */
153 GOTD_IMSG_DONE, /* The client is done chatting. */
155 /* Sending or receiving a pack file. */
156 GOTD_IMSG_SEND_PACKFILE, /* The server is sending a pack file. */
157 GOTD_IMSG_RECV_PACKFILE, /* The server is receiving a pack file. */
158 GOTD_IMSG_PACKIDX_FILE, /* Temporary file handle for new pack index. */
159 GOTD_IMSG_PACKFILE_PIPE, /* Pipe to send/receive a pack file stream. */
160 GOTD_IMSG_PACKFILE_PROGRESS, /* Progress reporting. */
161 GOTD_IMSG_PACKFILE_READY, /* Pack file is ready to be sent. */
162 GOTD_IMSG_PACKFILE_STATUS, /* Received pack success/failure status. */
163 GOTD_IMSG_PACKFILE_INSTALL, /* Received pack file can be installed. */
164 GOTD_IMSG_PACKFILE_DONE, /* Pack file has been sent/received. */
166 /* Reference updates. */
167 GOTD_IMSG_REF_UPDATES_START, /* Ref updates starting. */
168 GOTD_IMSG_REF_UPDATE_OK, /* Update went OK. */
169 GOTD_IMSG_REF_UPDATE_NG, /* Update was not good. */
170 GOTD_IMSG_REFS_UPDATED, /* The server proccessed all ref updates. */
172 /* Client is disconnecting. */
173 GOTD_IMSG_DISCONNECT,
174 };
176 /* Structure for GOTD_IMSG_ERROR. */
177 struct gotd_imsg_error {
178 int code; /* an error code from got_error.h */
179 int errno_code; /* in case code equals GOT_ERR_ERRNO */
180 uint32_t client_id;
181 char msg[GOT_ERR_MAX_MSG_SIZE];
182 } __attribute__((__packed__));
184 /* Structure for GOTD_IMSG_INFO. */
185 struct gotd_imsg_info {
186 pid_t pid;
187 int verbosity;
188 int nrepos;
189 int nclients;
191 /* Followed by nrepos GOTD_IMSG_INFO_REPO messages. */
192 /* Followed by nclients GOTD_IMSG_INFO_CLIENT messages. */
193 };
195 /* Structure for GOTD_IMSG_INFO_REPO. */
196 struct gotd_imsg_info_repo {
197 char repo_name[NAME_MAX];
198 char repo_path[PATH_MAX];
199 };
201 /* Structure for GOTD_IMSG_INFO_CLIENT */
202 struct gotd_imsg_info_client {
203 uid_t euid;
204 gid_t egid;
205 char repo_name[NAME_MAX];
206 int is_writing;
207 enum gotd_client_state state;
208 size_t ncapabilities;
210 /* Followed by ncapabilities GOTD_IMSG_CAPABILITY. */
211 };
213 /* Structure for GOTD_IMSG_LIST_REFS. */
214 struct gotd_imsg_list_refs {
215 char repo_name[NAME_MAX];
216 int client_is_reading; /* 1 if reading, 0 if writing */
217 };
219 /* Structure for GOTD_IMSG_LIST_REFS_INTERNAL. */
220 struct gotd_imsg_list_refs_internal {
221 uint32_t client_id;
222 };
224 /* Structure for GOTD_IMSG_REFLIST. */
225 struct gotd_imsg_reflist {
226 size_t nrefs;
228 /* Followed by nrefs times of gotd_imsg_ref/gotd_imsg_symref data. */
229 } __attribute__((__packed__));
231 /* Structure for GOTD_IMSG_REF data. */
232 struct gotd_imsg_ref {
233 uint8_t id[SHA1_DIGEST_LENGTH];
234 size_t name_len;
235 /* Followed by name_len data bytes. */
236 } __attribute__((__packed__));
238 /* Structure for GOTD_IMSG_SYMREF data. */
239 struct gotd_imsg_symref {
240 size_t name_len;
241 size_t target_len;
242 uint8_t target_id[SHA1_DIGEST_LENGTH];
244 /*
245 * Followed by name_len + target_len data bytes.
246 */
247 } __attribute__((__packed__));
249 /* Structure for GOTD_IMSG_CAPABILITIES data. */
250 struct gotd_imsg_capabilities {
251 size_t ncapabilities;
253 /*
254 * Followed by ncapabilities * GOTD_IMSG_CAPABILITY.
255 */
256 } __attribute__((__packed__));
258 /* Structure for GOTD_IMSG_CAPABILITY data. */
259 struct gotd_imsg_capability {
260 size_t key_len;
261 size_t value_len;
263 /*
264 * Followed by key_len + value_len data bytes.
265 */
266 } __attribute__((__packed__));
268 /* Structure for GOTD_IMSG_WANT data. */
269 struct gotd_imsg_want {
270 uint8_t object_id[SHA1_DIGEST_LENGTH];
271 uint32_t client_id;
272 } __attribute__((__packed__));
274 /* Structure for GOTD_IMSG_HAVE data. */
275 struct gotd_imsg_have {
276 uint8_t object_id[SHA1_DIGEST_LENGTH];
277 uint32_t client_id;
278 } __attribute__((__packed__));
280 /* Structure for GOTD_IMSG_ACK data. */
281 struct gotd_imsg_ack {
282 uint8_t object_id[SHA1_DIGEST_LENGTH];
283 uint32_t client_id;
284 } __attribute__((__packed__));
286 /* Structure for GOTD_IMSG_NAK data. */
287 struct gotd_imsg_nak {
288 uint8_t object_id[SHA1_DIGEST_LENGTH];
289 uint32_t client_id;
290 } __attribute__((__packed__));
292 /* Structure for GOTD_IMSG_PACKFILE_STATUS data. */
293 struct gotd_imsg_packfile_status {
294 size_t reason_len;
296 /* Followed by reason_len data bytes. */
297 } __attribute__((__packed__));
300 /* Structure for GOTD_IMSG_REF_UPDATE data. */
301 struct gotd_imsg_ref_update {
302 uint8_t old_id[SHA1_DIGEST_LENGTH];
303 uint8_t new_id[SHA1_DIGEST_LENGTH];
304 int ref_is_new;
305 uint32_t client_id;
306 size_t name_len;
308 /* Followed by name_len data bytes. */
309 } __attribute__((__packed__));
311 /* Structure for GOTD_IMSG_REF_UPDATES_START data. */
312 struct gotd_imsg_ref_updates_start {
313 int nref_updates;
314 uint32_t client_id;
316 /* Followed by nref_updates GOT_IMSG_REF_UPDATE_OK/NG messages. */
317 };
319 /* Structure for GOTD_IMSG_REF_UPDATE_OK data. */
320 struct gotd_imsg_ref_update_ok {
321 uint8_t old_id[SHA1_DIGEST_LENGTH];
322 uint8_t new_id[SHA1_DIGEST_LENGTH];
323 int ref_is_new;
324 uint32_t client_id;
325 size_t name_len;
327 /* Followed by name_len data bytes. */
328 } __attribute__((__packed__));
330 /* Structure for GOTD_IMSG_REF_UPDATE_NG data. */
331 struct gotd_imsg_ref_update_ng {
332 uint8_t old_id[SHA1_DIGEST_LENGTH];
333 uint8_t new_id[SHA1_DIGEST_LENGTH];
334 uint32_t client_id;
335 size_t name_len;
336 size_t reason_len;
338 /* Followed by name_len + reason_len data bytes. */
339 } __attribute__((__packed__));
341 /* Structure for GOTD_IMSG_SEND_PACKFILE data. */
342 struct gotd_imsg_send_packfile {
343 uint32_t client_id;
344 int report_progress;
346 /* delta cache file is sent as a file descriptor */
348 /* followed by two GOTD_IMSG_PACKFILE_PIPE messages */
349 };
351 /* Structure for GOTD_IMSG_RECV_PACKFILE data. */
352 struct gotd_imsg_recv_packfile {
353 uint32_t client_id;
354 int report_status;
356 /* pack destination temp file is sent as a file descriptor */
357 };
359 /* Structure for GOTD_IMSG_PACKFILE_PIPE data. */
360 struct gotd_imsg_packfile_pipe {
361 uint32_t client_id;
362 };
364 /* Structure for GOTD_IMSG_PACKIDX_FILE data. */
365 struct gotd_imsg_packidx_file {
366 uint32_t client_id;
367 };
370 /*
371 * Structure for GOTD_IMSG_PACKFILE_PROGRESS and
372 * GOTD_IMSG_PACKFILE_READY data.
373 */
374 struct gotd_imsg_packfile_progress {
375 uint32_t client_id;
376 int ncolored;
377 int nfound;
378 int ntrees;
379 off_t packfile_size;
380 int ncommits;
381 int nobj_total;
382 int nobj_deltify;
383 int nobj_written;
384 };
386 /* Structure for GOTD_IMSG_PACKFILE_INSTALL. */
387 struct gotd_imsg_packfile_install {
388 uint32_t client_id;
389 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
390 };
392 /* Structure for GOTD_IMSG_PACKFILE_DONE data. */
393 struct gotd_imsg_packfile_done {
394 uint32_t client_id;
395 };
397 /* Structure for GOTD_IMSG_DISCONNECT data. */
398 struct gotd_imsg_disconnect {
399 uint32_t client_id;
400 };
402 int parse_config(const char *, enum gotd_procid, struct gotd *);
404 /* imsg.c */
405 const struct got_error *gotd_imsg_flush(struct imsgbuf *);
406 const struct got_error *gotd_imsg_recv(struct imsg *, struct imsgbuf *, size_t);
407 const struct got_error *gotd_imsg_poll_recv(struct imsg *, struct imsgbuf *,
408 size_t);
409 const struct got_error *gotd_imsg_recv_error(uint32_t *client_id,
410 struct imsg *imsg);
411 int gotd_imsg_send_error(struct imsgbuf *ibuf, uint32_t, uint32_t,
412 const struct got_error *);
413 int gotd_imsg_send_error_event(struct gotd_imsgev *, uint32_t, uint32_t,
414 const struct got_error *);
415 void gotd_imsg_event_add(struct gotd_imsgev *);
416 int gotd_imsg_compose_event(struct gotd_imsgev *, uint16_t, uint32_t, int,
417 void *, uint16_t);
418 int gotd_imsg_forward(struct gotd_imsgev *, struct imsg *, int);
420 void gotd_imsg_send_ack(struct got_object_id *, struct imsgbuf *,
421 uint32_t, pid_t);
422 void gotd_imsg_send_nak(struct got_object_id *, struct imsgbuf *,
423 uint32_t, pid_t);