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"
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 /* Client hash tables need some extra room. */
32 #define GOTD_CLIENT_TABLE_SIZE (GOTD_MAXCLIENTS * 4)
34 enum gotd_procid {
35 PROC_GOTD = 0,
36 PROC_LISTEN,
37 PROC_AUTH,
38 PROC_REPO_READ,
39 PROC_REPO_WRITE,
40 PROC_MAX,
41 };
43 struct gotd_imsgev {
44 struct imsgbuf ibuf;
45 void (*handler)(int, short, void *);
46 void *handler_arg;
47 struct event ev;
48 short events;
49 };
51 struct gotd_child_proc {
52 pid_t pid;
53 enum gotd_procid type;
54 char repo_name[NAME_MAX];
55 char repo_path[PATH_MAX];
56 int pipe[2];
57 struct gotd_imsgev iev;
58 size_t nhelpers;
59 };
61 enum gotd_access {
62 GOTD_ACCESS_PERMITTED = 1,
63 GOTD_ACCESS_DENIED
64 };
66 struct gotd_access_rule {
67 STAILQ_ENTRY(gotd_access_rule) entry;
69 enum gotd_access access;
71 int authorization;
72 #define GOTD_AUTH_READ 0x1
73 #define GOTD_AUTH_WRITE 0x2
75 char *identifier;
76 };
77 STAILQ_HEAD(gotd_access_rule_list, gotd_access_rule);
79 struct gotd_repo {
80 TAILQ_ENTRY(gotd_repo) entry;
82 char name[NAME_MAX];
83 char path[PATH_MAX];
85 struct gotd_access_rule_list rules;
86 };
87 TAILQ_HEAD(gotd_repolist, gotd_repo);
89 enum gotd_client_state {
90 GOTD_STATE_EXPECT_LIST_REFS,
91 GOTD_STATE_EXPECT_CAPABILITIES,
92 GOTD_STATE_EXPECT_WANT,
93 GOTD_STATE_EXPECT_REF_UPDATE,
94 GOTD_STATE_EXPECT_MORE_REF_UPDATES,
95 GOTD_STATE_EXPECT_HAVE,
96 GOTD_STATE_EXPECT_PACKFILE,
97 GOTD_STATE_EXPECT_DONE,
98 GOTD_STATE_DONE,
99 };
101 struct gotd_client_capability {
102 char *key;
103 char *value;
104 };
106 struct gotd_object_id_array {
107 struct got_object_id **ids;
108 size_t nalloc;
109 size_t nids;
110 };
112 struct gotd {
113 pid_t pid;
114 char unix_socket_path[PATH_MAX];
115 char unix_group_name[32];
116 char user_name[32];
117 struct gotd_repolist repos;
118 int nrepos;
119 struct gotd_child_proc listen_proc;
121 char *argv0;
122 const char *confpath;
123 int daemonize;
124 int verbosity;
125 };
127 enum gotd_imsg_type {
128 /* An error occured while processing a request. */
129 GOTD_IMSG_ERROR,
131 /* Commands used by gotctl(8). */
132 GOTD_IMSG_INFO,
133 GOTD_IMSG_INFO_REPO,
134 GOTD_IMSG_INFO_CLIENT,
135 GOTD_IMSG_STOP,
137 /* Request a list of references. */
138 GOTD_IMSG_LIST_REFS,
139 GOTD_IMSG_LIST_REFS_INTERNAL,
141 /* References. */
142 GOTD_IMSG_REFLIST,
143 GOTD_IMSG_REF,
144 GOTD_IMSG_SYMREF,
146 /* Git protocol capabilities. */
147 GOTD_IMSG_CAPABILITIES,
148 GOTD_IMSG_CAPABILITY,
150 /* Git protocol chatter. */
151 GOTD_IMSG_WANT, /* The client wants an object. */
152 GOTD_IMSG_HAVE, /* The client has an object. */
153 GOTD_IMSG_ACK, /* The server has an object or a reference. */
154 GOTD_IMSG_NAK, /* The server does not have an object/ref. */
155 GOTD_IMSG_REF_UPDATE, /* The client wants to update a reference. */
156 GOTD_IMSG_REF_DELETE, /* The client wants to delete a reference. */
157 GOTD_IMSG_FLUSH, /* The client sent a flush packet. */
158 GOTD_IMSG_DONE, /* The client is done chatting. */
160 /* Sending or receiving a pack file. */
161 GOTD_IMSG_SEND_PACKFILE, /* The server is sending a pack file. */
162 GOTD_IMSG_RECV_PACKFILE, /* The server is receiving a pack file. */
163 GOTD_IMSG_PACKIDX_FILE, /* Temporary file handle for new pack index. */
164 GOTD_IMSG_PACKFILE_PIPE, /* Pipe to send/receive a pack file stream. */
165 GOTD_IMSG_PACKFILE_PROGRESS, /* Progress reporting. */
166 GOTD_IMSG_PACKFILE_READY, /* Pack file is ready to be sent. */
167 GOTD_IMSG_PACKFILE_STATUS, /* Received pack success/failure status. */
168 GOTD_IMSG_PACKFILE_INSTALL, /* Received pack file can be installed. */
169 GOTD_IMSG_PACKFILE_DONE, /* Pack file has been sent/received. */
171 /* Reference updates. */
172 GOTD_IMSG_REF_UPDATES_START, /* Ref updates starting. */
173 GOTD_IMSG_REF_UPDATE_OK, /* Update went OK. */
174 GOTD_IMSG_REF_UPDATE_NG, /* Update was not good. */
175 GOTD_IMSG_REFS_UPDATED, /* The server proccessed all ref updates. */
177 /* Client connections. */
178 GOTD_IMSG_DISCONNECT,
179 GOTD_IMSG_CONNECT,
181 /* Child process management. */
182 GOTD_IMSG_REPO_CHILD_READY,
184 /* Auth child process. */
185 GOTD_IMSG_AUTHENTICATE,
186 GOTD_IMSG_ACCESS_GRANTED,
187 };
189 /* Structure for GOTD_IMSG_ERROR. */
190 struct gotd_imsg_error {
191 int code; /* an error code from got_error.h */
192 int errno_code; /* in case code equals GOT_ERR_ERRNO */
193 uint32_t client_id;
194 char msg[GOT_ERR_MAX_MSG_SIZE];
195 } __attribute__((__packed__));
197 /* Structure for GOTD_IMSG_INFO. */
198 struct gotd_imsg_info {
199 pid_t pid;
200 int verbosity;
201 int nrepos;
202 int nclients;
204 /* Followed by nrepos GOTD_IMSG_INFO_REPO messages. */
205 /* Followed by nclients GOTD_IMSG_INFO_CLIENT messages. */
206 };
208 /* Structure for GOTD_IMSG_INFO_REPO. */
209 struct gotd_imsg_info_repo {
210 char repo_name[NAME_MAX];
211 char repo_path[PATH_MAX];
212 };
214 /* Structure for GOTD_IMSG_INFO_CLIENT */
215 struct gotd_imsg_info_client {
216 uid_t euid;
217 gid_t egid;
218 char repo_name[NAME_MAX];
219 int is_writing;
220 enum gotd_client_state state;
221 size_t ncapabilities;
223 /* Followed by ncapabilities GOTD_IMSG_CAPABILITY. */
224 };
226 /* Structure for GOTD_IMSG_LIST_REFS. */
227 struct gotd_imsg_list_refs {
228 char repo_name[NAME_MAX];
229 int client_is_reading; /* 1 if reading, 0 if writing */
230 };
232 /* Structure for GOTD_IMSG_LIST_REFS_INTERNAL. */
233 struct gotd_imsg_list_refs_internal {
234 uint32_t client_id;
235 };
237 /* Structure for GOTD_IMSG_REFLIST. */
238 struct gotd_imsg_reflist {
239 size_t nrefs;
241 /* Followed by nrefs times of gotd_imsg_ref/gotd_imsg_symref data. */
242 } __attribute__((__packed__));
244 /* Structure for GOTD_IMSG_REF data. */
245 struct gotd_imsg_ref {
246 uint8_t id[SHA1_DIGEST_LENGTH];
247 size_t name_len;
248 /* Followed by name_len data bytes. */
249 } __attribute__((__packed__));
251 /* Structure for GOTD_IMSG_SYMREF data. */
252 struct gotd_imsg_symref {
253 size_t name_len;
254 size_t target_len;
255 uint8_t target_id[SHA1_DIGEST_LENGTH];
257 /*
258 * Followed by name_len + target_len data bytes.
259 */
260 } __attribute__((__packed__));
262 /* Structure for GOTD_IMSG_CAPABILITIES data. */
263 struct gotd_imsg_capabilities {
264 size_t ncapabilities;
266 /*
267 * Followed by ncapabilities * GOTD_IMSG_CAPABILITY.
268 */
269 } __attribute__((__packed__));
271 /* Structure for GOTD_IMSG_CAPABILITY data. */
272 struct gotd_imsg_capability {
273 size_t key_len;
274 size_t value_len;
276 /*
277 * Followed by key_len + value_len data bytes.
278 */
279 } __attribute__((__packed__));
281 /* Structure for GOTD_IMSG_WANT data. */
282 struct gotd_imsg_want {
283 uint8_t object_id[SHA1_DIGEST_LENGTH];
284 uint32_t client_id;
285 } __attribute__((__packed__));
287 /* Structure for GOTD_IMSG_HAVE data. */
288 struct gotd_imsg_have {
289 uint8_t object_id[SHA1_DIGEST_LENGTH];
290 uint32_t client_id;
291 } __attribute__((__packed__));
293 /* Structure for GOTD_IMSG_ACK data. */
294 struct gotd_imsg_ack {
295 uint8_t object_id[SHA1_DIGEST_LENGTH];
296 uint32_t client_id;
297 } __attribute__((__packed__));
299 /* Structure for GOTD_IMSG_NAK data. */
300 struct gotd_imsg_nak {
301 uint8_t object_id[SHA1_DIGEST_LENGTH];
302 uint32_t client_id;
303 } __attribute__((__packed__));
305 /* Structure for GOTD_IMSG_PACKFILE_STATUS data. */
306 struct gotd_imsg_packfile_status {
307 size_t reason_len;
309 /* Followed by reason_len data bytes. */
310 } __attribute__((__packed__));
313 /* Structure for GOTD_IMSG_REF_UPDATE data. */
314 struct gotd_imsg_ref_update {
315 uint8_t old_id[SHA1_DIGEST_LENGTH];
316 uint8_t new_id[SHA1_DIGEST_LENGTH];
317 int ref_is_new;
318 uint32_t client_id;
319 size_t name_len;
321 /* Followed by name_len data bytes. */
322 } __attribute__((__packed__));
324 /* Structure for GOTD_IMSG_REF_UPDATES_START data. */
325 struct gotd_imsg_ref_updates_start {
326 int nref_updates;
327 uint32_t client_id;
329 /* Followed by nref_updates GOT_IMSG_REF_UPDATE_OK/NG messages. */
330 };
332 /* Structure for GOTD_IMSG_REF_UPDATE_OK data. */
333 struct gotd_imsg_ref_update_ok {
334 uint8_t old_id[SHA1_DIGEST_LENGTH];
335 uint8_t new_id[SHA1_DIGEST_LENGTH];
336 int ref_is_new;
337 uint32_t client_id;
338 size_t name_len;
340 /* Followed by name_len data bytes. */
341 } __attribute__((__packed__));
343 /* Structure for GOTD_IMSG_REF_UPDATE_NG data. */
344 struct gotd_imsg_ref_update_ng {
345 uint8_t old_id[SHA1_DIGEST_LENGTH];
346 uint8_t new_id[SHA1_DIGEST_LENGTH];
347 uint32_t client_id;
348 size_t name_len;
349 size_t reason_len;
351 /* Followed by name_len + reason_len data bytes. */
352 } __attribute__((__packed__));
354 /* Structure for GOTD_IMSG_SEND_PACKFILE data. */
355 struct gotd_imsg_send_packfile {
356 uint32_t client_id;
357 int report_progress;
359 /* delta cache file is sent as a file descriptor */
361 /* followed by two GOTD_IMSG_PACKFILE_PIPE messages */
362 };
364 /* Structure for GOTD_IMSG_RECV_PACKFILE data. */
365 struct gotd_imsg_recv_packfile {
366 uint32_t client_id;
367 int report_status;
369 /* pack destination temp file is sent as a file descriptor */
370 };
372 /* Structure for GOTD_IMSG_PACKFILE_PIPE data. */
373 struct gotd_imsg_packfile_pipe {
374 uint32_t client_id;
375 };
377 /* Structure for GOTD_IMSG_PACKIDX_FILE data. */
378 struct gotd_imsg_packidx_file {
379 uint32_t client_id;
380 };
383 /*
384 * Structure for GOTD_IMSG_PACKFILE_PROGRESS and
385 * GOTD_IMSG_PACKFILE_READY data.
386 */
387 struct gotd_imsg_packfile_progress {
388 uint32_t client_id;
389 int ncolored;
390 int nfound;
391 int ntrees;
392 off_t packfile_size;
393 int ncommits;
394 int nobj_total;
395 int nobj_deltify;
396 int nobj_written;
397 };
399 /* Structure for GOTD_IMSG_PACKFILE_INSTALL. */
400 struct gotd_imsg_packfile_install {
401 uint32_t client_id;
402 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
403 };
405 /* Structure for GOTD_IMSG_PACKFILE_DONE data. */
406 struct gotd_imsg_packfile_done {
407 uint32_t client_id;
408 };
410 /* Structure for GOTD_IMSG_DISCONNECT data. */
411 struct gotd_imsg_disconnect {
412 uint32_t client_id;
413 };
415 /* Structure for GOTD_IMSG_CONNECT. */
416 struct gotd_imsg_connect {
417 uint32_t client_id;
418 uid_t euid;
419 gid_t egid;
420 };
422 /* Structure for GOTD_IMSG_AUTHENTICATE. */
423 struct gotd_imsg_auth {
424 uid_t euid;
425 gid_t egid;
426 int required_auth;
427 uint32_t client_id;
428 };
430 int parse_config(const char *, enum gotd_procid, struct gotd *);
432 /* imsg.c */
433 const struct got_error *gotd_imsg_flush(struct imsgbuf *);
434 const struct got_error *gotd_imsg_recv(struct imsg *, struct imsgbuf *, size_t);
435 const struct got_error *gotd_imsg_poll_recv(struct imsg *, struct imsgbuf *,
436 size_t);
437 const struct got_error *gotd_imsg_recv_error(uint32_t *client_id,
438 struct imsg *imsg);
439 int gotd_imsg_send_error(struct imsgbuf *ibuf, uint32_t, uint32_t,
440 const struct got_error *);
441 int gotd_imsg_send_error_event(struct gotd_imsgev *, uint32_t, uint32_t,
442 const struct got_error *);
443 void gotd_imsg_event_add(struct gotd_imsgev *);
444 int gotd_imsg_compose_event(struct gotd_imsgev *, uint16_t, uint32_t, int,
445 void *, uint16_t);
446 int gotd_imsg_forward(struct gotd_imsgev *, struct imsg *, int);
448 void gotd_imsg_send_ack(struct got_object_id *, struct imsgbuf *,
449 uint32_t, pid_t);
450 void gotd_imsg_send_nak(struct got_object_id *, struct imsgbuf *,
451 uint32_t, pid_t);