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_FD_RESERVE 5
27 #define GOTD_FD_NEEDED 6
28 #define GOTD_FILENO_MSG_PIPE 3
30 /* Client hash tables need some extra room. */
31 #define GOTD_CLIENT_TABLE_SIZE (GOTD_MAXCLIENTS * 4)
33 enum gotd_procid {
34 PROC_GOTD = 0,
35 PROC_LISTEN,
36 PROC_REPO_READ,
37 PROC_REPO_WRITE,
38 PROC_MAX,
39 };
41 struct gotd_imsgev {
42 struct imsgbuf ibuf;
43 void (*handler)(int, short, void *);
44 void *handler_arg;
45 struct event ev;
46 short events;
47 };
49 struct gotd_child_proc {
50 pid_t pid;
51 enum gotd_procid type;
52 char repo_name[NAME_MAX];
53 char repo_path[PATH_MAX];
54 int pipe[2];
55 struct gotd_imsgev iev;
56 size_t nhelpers;
57 };
59 enum gotd_access {
60 GOTD_ACCESS_PERMITTED = 1,
61 GOTD_ACCESS_DENIED
62 };
64 struct gotd_access_rule {
65 STAILQ_ENTRY(gotd_access_rule) entry;
67 enum gotd_access access;
69 int authorization;
70 #define GOTD_AUTH_READ 0x1
71 #define GOTD_AUTH_WRITE 0x2
73 char *identifier;
74 };
75 STAILQ_HEAD(gotd_access_rule_list, gotd_access_rule);
77 struct gotd_repo {
78 TAILQ_ENTRY(gotd_repo) entry;
80 char name[NAME_MAX];
81 char path[PATH_MAX];
83 struct gotd_access_rule_list rules;
84 };
85 TAILQ_HEAD(gotd_repolist, gotd_repo);
87 enum gotd_client_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 {
111 pid_t pid;
112 char unix_socket_path[PATH_MAX];
113 char unix_group_name[32];
114 char user_name[32];
115 struct gotd_repolist repos;
116 int nrepos;
117 struct gotd_child_proc listen_proc;
119 char *argv0;
120 const char *confpath;
121 int daemonize;
122 int verbosity;
123 };
125 enum gotd_imsg_type {
126 /* An error occured while processing a request. */
127 GOTD_IMSG_ERROR,
129 /* Commands used by gotctl(8). */
130 GOTD_IMSG_INFO,
131 GOTD_IMSG_INFO_REPO,
132 GOTD_IMSG_INFO_CLIENT,
133 GOTD_IMSG_STOP,
135 /* Request a list of references. */
136 GOTD_IMSG_LIST_REFS,
137 GOTD_IMSG_LIST_REFS_INTERNAL,
139 /* References. */
140 GOTD_IMSG_REFLIST,
141 GOTD_IMSG_REF,
142 GOTD_IMSG_SYMREF,
144 /* Git protocol capabilities. */
145 GOTD_IMSG_CAPABILITIES,
146 GOTD_IMSG_CAPABILITY,
148 /* Git protocol chatter. */
149 GOTD_IMSG_WANT, /* The client wants an object. */
150 GOTD_IMSG_HAVE, /* The client has an object. */
151 GOTD_IMSG_ACK, /* The server has an object or a reference. */
152 GOTD_IMSG_NAK, /* The server does not have an object/ref. */
153 GOTD_IMSG_REF_UPDATE, /* The client wants to update a reference. */
154 GOTD_IMSG_REF_DELETE, /* The client wants to delete a reference. */
155 GOTD_IMSG_FLUSH, /* The client sent a flush packet. */
156 GOTD_IMSG_DONE, /* The client is done chatting. */
158 /* Sending or receiving a pack file. */
159 GOTD_IMSG_SEND_PACKFILE, /* The server is sending a pack file. */
160 GOTD_IMSG_RECV_PACKFILE, /* The server is receiving a pack file. */
161 GOTD_IMSG_PACKIDX_FILE, /* Temporary file handle for new pack index. */
162 GOTD_IMSG_PACKFILE_PIPE, /* Pipe to send/receive a pack file stream. */
163 GOTD_IMSG_PACKFILE_PROGRESS, /* Progress reporting. */
164 GOTD_IMSG_PACKFILE_READY, /* Pack file is ready to be sent. */
165 GOTD_IMSG_PACKFILE_STATUS, /* Received pack success/failure status. */
166 GOTD_IMSG_PACKFILE_INSTALL, /* Received pack file can be installed. */
167 GOTD_IMSG_PACKFILE_DONE, /* Pack file has been sent/received. */
169 /* Reference updates. */
170 GOTD_IMSG_REF_UPDATES_START, /* Ref updates starting. */
171 GOTD_IMSG_REF_UPDATE_OK, /* Update went OK. */
172 GOTD_IMSG_REF_UPDATE_NG, /* Update was not good. */
173 GOTD_IMSG_REFS_UPDATED, /* The server proccessed all ref updates. */
175 /* Client connections. */
176 GOTD_IMSG_DISCONNECT,
177 GOTD_IMSG_CONNECT,
179 /* Child process management. */
180 GOTD_IMSG_REPO_CHILD_READY,
181 };
183 /* Structure for GOTD_IMSG_ERROR. */
184 struct gotd_imsg_error {
185 int code; /* an error code from got_error.h */
186 int errno_code; /* in case code equals GOT_ERR_ERRNO */
187 uint32_t client_id;
188 char msg[GOT_ERR_MAX_MSG_SIZE];
189 } __attribute__((__packed__));
191 /* Structure for GOTD_IMSG_INFO. */
192 struct gotd_imsg_info {
193 pid_t pid;
194 int verbosity;
195 int nrepos;
196 int nclients;
198 /* Followed by nrepos GOTD_IMSG_INFO_REPO messages. */
199 /* Followed by nclients GOTD_IMSG_INFO_CLIENT messages. */
200 };
202 /* Structure for GOTD_IMSG_INFO_REPO. */
203 struct gotd_imsg_info_repo {
204 char repo_name[NAME_MAX];
205 char repo_path[PATH_MAX];
206 };
208 /* Structure for GOTD_IMSG_INFO_CLIENT */
209 struct gotd_imsg_info_client {
210 uid_t euid;
211 gid_t egid;
212 char repo_name[NAME_MAX];
213 int is_writing;
214 enum gotd_client_state state;
215 size_t ncapabilities;
217 /* Followed by ncapabilities GOTD_IMSG_CAPABILITY. */
218 };
220 /* Structure for GOTD_IMSG_LIST_REFS. */
221 struct gotd_imsg_list_refs {
222 char repo_name[NAME_MAX];
223 int client_is_reading; /* 1 if reading, 0 if writing */
224 };
226 /* Structure for GOTD_IMSG_LIST_REFS_INTERNAL. */
227 struct gotd_imsg_list_refs_internal {
228 uint32_t client_id;
229 };
231 /* Structure for GOTD_IMSG_REFLIST. */
232 struct gotd_imsg_reflist {
233 size_t nrefs;
235 /* Followed by nrefs times of gotd_imsg_ref/gotd_imsg_symref data. */
236 } __attribute__((__packed__));
238 /* Structure for GOTD_IMSG_REF data. */
239 struct gotd_imsg_ref {
240 uint8_t id[SHA1_DIGEST_LENGTH];
241 size_t name_len;
242 /* Followed by name_len data bytes. */
243 } __attribute__((__packed__));
245 /* Structure for GOTD_IMSG_SYMREF data. */
246 struct gotd_imsg_symref {
247 size_t name_len;
248 size_t target_len;
249 uint8_t target_id[SHA1_DIGEST_LENGTH];
251 /*
252 * Followed by name_len + target_len data bytes.
253 */
254 } __attribute__((__packed__));
256 /* Structure for GOTD_IMSG_CAPABILITIES data. */
257 struct gotd_imsg_capabilities {
258 size_t ncapabilities;
260 /*
261 * Followed by ncapabilities * GOTD_IMSG_CAPABILITY.
262 */
263 } __attribute__((__packed__));
265 /* Structure for GOTD_IMSG_CAPABILITY data. */
266 struct gotd_imsg_capability {
267 size_t key_len;
268 size_t value_len;
270 /*
271 * Followed by key_len + value_len data bytes.
272 */
273 } __attribute__((__packed__));
275 /* Structure for GOTD_IMSG_WANT data. */
276 struct gotd_imsg_want {
277 uint8_t object_id[SHA1_DIGEST_LENGTH];
278 uint32_t client_id;
279 } __attribute__((__packed__));
281 /* Structure for GOTD_IMSG_HAVE data. */
282 struct gotd_imsg_have {
283 uint8_t object_id[SHA1_DIGEST_LENGTH];
284 uint32_t client_id;
285 } __attribute__((__packed__));
287 /* Structure for GOTD_IMSG_ACK data. */
288 struct gotd_imsg_ack {
289 uint8_t object_id[SHA1_DIGEST_LENGTH];
290 uint32_t client_id;
291 } __attribute__((__packed__));
293 /* Structure for GOTD_IMSG_NAK data. */
294 struct gotd_imsg_nak {
295 uint8_t object_id[SHA1_DIGEST_LENGTH];
296 uint32_t client_id;
297 } __attribute__((__packed__));
299 /* Structure for GOTD_IMSG_PACKFILE_STATUS data. */
300 struct gotd_imsg_packfile_status {
301 size_t reason_len;
303 /* Followed by reason_len data bytes. */
304 } __attribute__((__packed__));
307 /* Structure for GOTD_IMSG_REF_UPDATE data. */
308 struct gotd_imsg_ref_update {
309 uint8_t old_id[SHA1_DIGEST_LENGTH];
310 uint8_t new_id[SHA1_DIGEST_LENGTH];
311 int ref_is_new;
312 uint32_t client_id;
313 size_t name_len;
315 /* Followed by name_len data bytes. */
316 } __attribute__((__packed__));
318 /* Structure for GOTD_IMSG_REF_UPDATES_START data. */
319 struct gotd_imsg_ref_updates_start {
320 int nref_updates;
321 uint32_t client_id;
323 /* Followed by nref_updates GOT_IMSG_REF_UPDATE_OK/NG messages. */
324 };
326 /* Structure for GOTD_IMSG_REF_UPDATE_OK data. */
327 struct gotd_imsg_ref_update_ok {
328 uint8_t old_id[SHA1_DIGEST_LENGTH];
329 uint8_t new_id[SHA1_DIGEST_LENGTH];
330 int ref_is_new;
331 uint32_t client_id;
332 size_t name_len;
334 /* Followed by name_len data bytes. */
335 } __attribute__((__packed__));
337 /* Structure for GOTD_IMSG_REF_UPDATE_NG data. */
338 struct gotd_imsg_ref_update_ng {
339 uint8_t old_id[SHA1_DIGEST_LENGTH];
340 uint8_t new_id[SHA1_DIGEST_LENGTH];
341 uint32_t client_id;
342 size_t name_len;
343 size_t reason_len;
345 /* Followed by name_len + reason_len data bytes. */
346 } __attribute__((__packed__));
348 /* Structure for GOTD_IMSG_SEND_PACKFILE data. */
349 struct gotd_imsg_send_packfile {
350 uint32_t client_id;
351 int report_progress;
353 /* delta cache file is sent as a file descriptor */
355 /* followed by two GOTD_IMSG_PACKFILE_PIPE messages */
356 };
358 /* Structure for GOTD_IMSG_RECV_PACKFILE data. */
359 struct gotd_imsg_recv_packfile {
360 uint32_t client_id;
361 int report_status;
363 /* pack destination temp file is sent as a file descriptor */
364 };
366 /* Structure for GOTD_IMSG_PACKFILE_PIPE data. */
367 struct gotd_imsg_packfile_pipe {
368 uint32_t client_id;
369 };
371 /* Structure for GOTD_IMSG_PACKIDX_FILE data. */
372 struct gotd_imsg_packidx_file {
373 uint32_t client_id;
374 };
377 /*
378 * Structure for GOTD_IMSG_PACKFILE_PROGRESS and
379 * GOTD_IMSG_PACKFILE_READY data.
380 */
381 struct gotd_imsg_packfile_progress {
382 uint32_t client_id;
383 int ncolored;
384 int nfound;
385 int ntrees;
386 off_t packfile_size;
387 int ncommits;
388 int nobj_total;
389 int nobj_deltify;
390 int nobj_written;
391 };
393 /* Structure for GOTD_IMSG_PACKFILE_INSTALL. */
394 struct gotd_imsg_packfile_install {
395 uint32_t client_id;
396 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
397 };
399 /* Structure for GOTD_IMSG_PACKFILE_DONE data. */
400 struct gotd_imsg_packfile_done {
401 uint32_t client_id;
402 };
404 /* Structure for GOTD_IMSG_DISCONNECT data. */
405 struct gotd_imsg_disconnect {
406 uint32_t client_id;
407 };
409 /* Structure for GOTD_IMSG_CONNECT. */
410 struct gotd_imsg_connect {
411 uint32_t client_id;
412 };
414 int parse_config(const char *, enum gotd_procid, struct gotd *);
416 /* imsg.c */
417 const struct got_error *gotd_imsg_flush(struct imsgbuf *);
418 const struct got_error *gotd_imsg_recv(struct imsg *, struct imsgbuf *, size_t);
419 const struct got_error *gotd_imsg_poll_recv(struct imsg *, struct imsgbuf *,
420 size_t);
421 const struct got_error *gotd_imsg_recv_error(uint32_t *client_id,
422 struct imsg *imsg);
423 int gotd_imsg_send_error(struct imsgbuf *ibuf, uint32_t, uint32_t,
424 const struct got_error *);
425 int gotd_imsg_send_error_event(struct gotd_imsgev *, uint32_t, uint32_t,
426 const struct got_error *);
427 void gotd_imsg_event_add(struct gotd_imsgev *);
428 int gotd_imsg_compose_event(struct gotd_imsgev *, uint16_t, uint32_t, int,
429 void *, uint16_t);
430 int gotd_imsg_forward(struct gotd_imsgev *, struct imsg *, int);
432 void gotd_imsg_send_ack(struct got_object_id *, struct imsgbuf *,
433 uint32_t, pid_t);
434 void gotd_imsg_send_nak(struct got_object_id *, struct imsgbuf *,
435 uint32_t, pid_t);