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 gid_t groups[NGROUPS_MAX];
121 int ngroups;
122 };
124 enum gotd_imsg_type {
125 /* An error occured while processing a request. */
126 GOTD_IMSG_ERROR,
128 /* Commands used by gotctl(8). */
129 GOTD_IMSG_INFO,
130 GOTD_IMSG_INFO_REPO,
131 GOTD_IMSG_INFO_CLIENT,
132 GOTD_IMSG_STOP,
134 /* Request a list of references. */
135 GOTD_IMSG_LIST_REFS,
136 GOTD_IMSG_LIST_REFS_INTERNAL,
138 /* References. */
139 GOTD_IMSG_REFLIST,
140 GOTD_IMSG_REF,
141 GOTD_IMSG_SYMREF,
143 /* Git protocol capabilities. */
144 GOTD_IMSG_CAPABILITIES,
145 GOTD_IMSG_CAPABILITY,
147 /* Git protocol chatter. */
148 GOTD_IMSG_WANT, /* The client wants an object. */
149 GOTD_IMSG_HAVE, /* The client has an object. */
150 GOTD_IMSG_ACK, /* The server has an object or a reference. */
151 GOTD_IMSG_NAK, /* The server does not have an object/ref. */
152 GOTD_IMSG_REF_UPDATE, /* The client wants to update a reference. */
153 GOTD_IMSG_REF_DELETE, /* The client wants to delete a reference. */
154 GOTD_IMSG_FLUSH, /* The client sent a flush packet. */
155 GOTD_IMSG_DONE, /* The client is done chatting. */
157 /* Sending or receiving a pack file. */
158 GOTD_IMSG_SEND_PACKFILE, /* The server is sending a pack file. */
159 GOTD_IMSG_RECV_PACKFILE, /* The server is receiving a pack file. */
160 GOTD_IMSG_PACKIDX_FILE, /* Temporary file handle for new pack index. */
161 GOTD_IMSG_PACKFILE_PIPE, /* Pipe to send/receive a pack file stream. */
162 GOTD_IMSG_PACKFILE_PROGRESS, /* Progress reporting. */
163 GOTD_IMSG_PACKFILE_READY, /* Pack file is ready to be sent. */
164 GOTD_IMSG_PACKFILE_STATUS, /* Received pack success/failure status. */
165 GOTD_IMSG_PACKFILE_INSTALL, /* Received pack file can be installed. */
166 GOTD_IMSG_PACKFILE_DONE, /* Pack file has been sent/received. */
168 /* Reference updates. */
169 GOTD_IMSG_REF_UPDATES_START, /* Ref updates starting. */
170 GOTD_IMSG_REF_UPDATE_OK, /* Update went OK. */
171 GOTD_IMSG_REF_UPDATE_NG, /* Update was not good. */
172 GOTD_IMSG_REFS_UPDATED, /* The server proccessed all ref updates. */
174 /* Client is disconnecting. */
175 GOTD_IMSG_DISCONNECT,
176 };
178 /* Structure for GOTD_IMSG_ERROR. */
179 struct gotd_imsg_error {
180 int code; /* an error code from got_error.h */
181 int errno_code; /* in case code equals GOT_ERR_ERRNO */
182 uint32_t client_id;
183 char msg[GOT_ERR_MAX_MSG_SIZE];
184 } __attribute__((__packed__));
186 /* Structure for GOTD_IMSG_INFO. */
187 struct gotd_imsg_info {
188 pid_t pid;
189 int verbosity;
190 int nrepos;
191 int nclients;
193 /* Followed by nrepos GOTD_IMSG_INFO_REPO messages. */
194 /* Followed by nclients GOTD_IMSG_INFO_CLIENT messages. */
195 };
197 /* Structure for GOTD_IMSG_INFO_REPO. */
198 struct gotd_imsg_info_repo {
199 char repo_name[NAME_MAX];
200 char repo_path[PATH_MAX];
201 };
203 /* Structure for GOTD_IMSG_INFO_CLIENT */
204 struct gotd_imsg_info_client {
205 uid_t euid;
206 gid_t egid;
207 char repo_name[NAME_MAX];
208 int is_writing;
209 enum gotd_client_state state;
210 size_t ncapabilities;
212 /* Followed by ncapabilities GOTD_IMSG_CAPABILITY. */
213 };
215 /* Structure for GOTD_IMSG_LIST_REFS. */
216 struct gotd_imsg_list_refs {
217 char repo_name[NAME_MAX];
218 int client_is_reading; /* 1 if reading, 0 if writing */
219 };
221 /* Structure for GOTD_IMSG_LIST_REFS_INTERNAL. */
222 struct gotd_imsg_list_refs_internal {
223 uint32_t client_id;
224 };
226 /* Structure for GOTD_IMSG_REFLIST. */
227 struct gotd_imsg_reflist {
228 size_t nrefs;
230 /* Followed by nrefs times of gotd_imsg_ref/gotd_imsg_symref data. */
231 } __attribute__((__packed__));
233 /* Structure for GOTD_IMSG_REF data. */
234 struct gotd_imsg_ref {
235 uint8_t id[SHA1_DIGEST_LENGTH];
236 size_t name_len;
237 /* Followed by name_len data bytes. */
238 } __attribute__((__packed__));
240 /* Structure for GOTD_IMSG_SYMREF data. */
241 struct gotd_imsg_symref {
242 size_t name_len;
243 size_t target_len;
244 uint8_t target_id[SHA1_DIGEST_LENGTH];
246 /*
247 * Followed by name_len + target_len data bytes.
248 */
249 } __attribute__((__packed__));
251 /* Structure for GOTD_IMSG_CAPABILITIES data. */
252 struct gotd_imsg_capabilities {
253 size_t ncapabilities;
255 /*
256 * Followed by ncapabilities * GOTD_IMSG_CAPABILITY.
257 */
258 } __attribute__((__packed__));
260 /* Structure for GOTD_IMSG_CAPABILITY data. */
261 struct gotd_imsg_capability {
262 size_t key_len;
263 size_t value_len;
265 /*
266 * Followed by key_len + value_len data bytes.
267 */
268 } __attribute__((__packed__));
270 /* Structure for GOTD_IMSG_WANT data. */
271 struct gotd_imsg_want {
272 uint8_t object_id[SHA1_DIGEST_LENGTH];
273 uint32_t client_id;
274 } __attribute__((__packed__));
276 /* Structure for GOTD_IMSG_HAVE data. */
277 struct gotd_imsg_have {
278 uint8_t object_id[SHA1_DIGEST_LENGTH];
279 uint32_t client_id;
280 } __attribute__((__packed__));
282 /* Structure for GOTD_IMSG_ACK data. */
283 struct gotd_imsg_ack {
284 uint8_t object_id[SHA1_DIGEST_LENGTH];
285 uint32_t client_id;
286 } __attribute__((__packed__));
288 /* Structure for GOTD_IMSG_NAK data. */
289 struct gotd_imsg_nak {
290 uint8_t object_id[SHA1_DIGEST_LENGTH];
291 uint32_t client_id;
292 } __attribute__((__packed__));
294 /* Structure for GOTD_IMSG_PACKFILE_STATUS data. */
295 struct gotd_imsg_packfile_status {
296 size_t reason_len;
298 /* Followed by reason_len data bytes. */
299 } __attribute__((__packed__));
302 /* Structure for GOTD_IMSG_REF_UPDATE data. */
303 struct gotd_imsg_ref_update {
304 uint8_t old_id[SHA1_DIGEST_LENGTH];
305 uint8_t new_id[SHA1_DIGEST_LENGTH];
306 int ref_is_new;
307 uint32_t client_id;
308 size_t name_len;
310 /* Followed by name_len data bytes. */
311 } __attribute__((__packed__));
313 /* Structure for GOTD_IMSG_REF_UPDATES_START data. */
314 struct gotd_imsg_ref_updates_start {
315 int nref_updates;
316 uint32_t client_id;
318 /* Followed by nref_updates GOT_IMSG_REF_UPDATE_OK/NG messages. */
319 };
321 /* Structure for GOTD_IMSG_REF_UPDATE_OK data. */
322 struct gotd_imsg_ref_update_ok {
323 uint8_t old_id[SHA1_DIGEST_LENGTH];
324 uint8_t new_id[SHA1_DIGEST_LENGTH];
325 int ref_is_new;
326 uint32_t client_id;
327 size_t name_len;
329 /* Followed by name_len data bytes. */
330 } __attribute__((__packed__));
332 /* Structure for GOTD_IMSG_REF_UPDATE_NG data. */
333 struct gotd_imsg_ref_update_ng {
334 uint8_t old_id[SHA1_DIGEST_LENGTH];
335 uint8_t new_id[SHA1_DIGEST_LENGTH];
336 uint32_t client_id;
337 size_t name_len;
338 size_t reason_len;
340 /* Followed by name_len + reason_len data bytes. */
341 } __attribute__((__packed__));
343 /* Structure for GOTD_IMSG_SEND_PACKFILE data. */
344 struct gotd_imsg_send_packfile {
345 uint32_t client_id;
346 int report_progress;
348 /* delta cache file is sent as a file descriptor */
350 /* followed by two GOTD_IMSG_PACKFILE_PIPE messages */
351 };
353 /* Structure for GOTD_IMSG_RECV_PACKFILE data. */
354 struct gotd_imsg_recv_packfile {
355 uint32_t client_id;
356 int report_status;
358 /* pack destination temp file is sent as a file descriptor */
359 };
361 /* Structure for GOTD_IMSG_PACKFILE_PIPE data. */
362 struct gotd_imsg_packfile_pipe {
363 uint32_t client_id;
364 };
366 /* Structure for GOTD_IMSG_PACKIDX_FILE data. */
367 struct gotd_imsg_packidx_file {
368 uint32_t client_id;
369 };
372 /*
373 * Structure for GOTD_IMSG_PACKFILE_PROGRESS and
374 * GOTD_IMSG_PACKFILE_READY data.
375 */
376 struct gotd_imsg_packfile_progress {
377 uint32_t client_id;
378 int ncolored;
379 int nfound;
380 int ntrees;
381 off_t packfile_size;
382 int ncommits;
383 int nobj_total;
384 int nobj_deltify;
385 int nobj_written;
386 };
388 /* Structure for GOTD_IMSG_PACKFILE_INSTALL. */
389 struct gotd_imsg_packfile_install {
390 uint32_t client_id;
391 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
392 };
394 /* Structure for GOTD_IMSG_PACKFILE_DONE data. */
395 struct gotd_imsg_packfile_done {
396 uint32_t client_id;
397 };
399 /* Structure for GOTD_IMSG_DISCONNECT data. */
400 struct gotd_imsg_disconnect {
401 uint32_t client_id;
402 };
404 int parse_config(const char *, enum gotd_procid, struct gotd *);
406 /* imsg.c */
407 const struct got_error *gotd_imsg_flush(struct imsgbuf *);
408 const struct got_error *gotd_imsg_recv(struct imsg *, struct imsgbuf *, size_t);
409 const struct got_error *gotd_imsg_poll_recv(struct imsg *, struct imsgbuf *,
410 size_t);
411 const struct got_error *gotd_imsg_recv_error(uint32_t *client_id,
412 struct imsg *imsg);
413 int gotd_imsg_send_error(struct imsgbuf *ibuf, uint32_t, uint32_t,
414 const struct got_error *);
415 int gotd_imsg_send_error_event(struct gotd_imsgev *, uint32_t, uint32_t,
416 const struct got_error *);
417 void gotd_imsg_event_add(struct gotd_imsgev *);
418 int gotd_imsg_compose_event(struct gotd_imsgev *, uint16_t, uint32_t, int,
419 void *, uint16_t);
420 int gotd_imsg_forward(struct gotd_imsgev *, struct imsg *, int);
422 void gotd_imsg_send_ack(struct got_object_id *, struct imsgbuf *,
423 uint32_t, pid_t);
424 void gotd_imsg_send_nak(struct got_object_id *, struct imsgbuf *,
425 uint32_t, pid_t);