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 struct gotd_repo {
58 TAILQ_ENTRY(gotd_repo) entry;
60 char name[NAME_MAX];
61 char path[PATH_MAX];
62 };
63 TAILQ_HEAD(gotd_repolist, gotd_repo);
65 enum gotd_client_state {
66 GOTD_STATE_EXPECT_LIST_REFS,
67 GOTD_STATE_EXPECT_CAPABILITIES,
68 GOTD_STATE_EXPECT_WANT,
69 GOTD_STATE_EXPECT_REF_UPDATE,
70 GOTD_STATE_EXPECT_MORE_REF_UPDATES,
71 GOTD_STATE_EXPECT_HAVE,
72 GOTD_STATE_EXPECT_PACKFILE,
73 GOTD_STATE_EXPECT_DONE,
74 GOTD_STATE_DONE,
75 };
77 struct gotd_client_capability {
78 char *key;
79 char *value;
80 };
82 struct gotd_object_id_array {
83 struct got_object_id **ids;
84 size_t nalloc;
85 size_t nids;
86 };
88 struct gotd {
89 pid_t pid;
90 char unix_socket_path[PATH_MAX];
91 char unix_group_name[32];
92 char user_name[32];
93 struct gotd_repolist repos;
94 int nrepos;
95 int verbosity;
96 struct event ev;
97 struct event pause;
98 struct gotd_child_proc *procs;
99 int nprocs;
100 };
102 enum gotd_imsg_type {
103 /* An error occured while processing a request. */
104 GOTD_IMSG_ERROR,
106 /* Commands used by gotctl(8). */
107 GOTD_IMSG_INFO,
108 GOTD_IMSG_INFO_REPO,
109 GOTD_IMSG_INFO_CLIENT,
110 GOTD_IMSG_STOP,
112 /* Request a list of references. */
113 GOTD_IMSG_LIST_REFS,
114 GOTD_IMSG_LIST_REFS_INTERNAL,
116 /* References. */
117 GOTD_IMSG_REFLIST,
118 GOTD_IMSG_REF,
119 GOTD_IMSG_SYMREF,
121 /* Git protocol capabilities. */
122 GOTD_IMSG_CAPABILITIES,
123 GOTD_IMSG_CAPABILITY,
125 /* Git protocol chatter. */
126 GOTD_IMSG_WANT, /* The client wants an object. */
127 GOTD_IMSG_HAVE, /* The client has an object. */
128 GOTD_IMSG_ACK, /* The server has an object or a reference. */
129 GOTD_IMSG_NAK, /* The server does not have an object/ref. */
130 GOTD_IMSG_REF_UPDATE, /* The client wants to update a reference. */
131 GOTD_IMSG_REF_DELETE, /* The client wants to delete a reference. */
132 GOTD_IMSG_FLUSH, /* The client sent a flush packet. */
133 GOTD_IMSG_DONE, /* The client is done chatting. */
135 /* Sending or receiving a pack file. */
136 GOTD_IMSG_SEND_PACKFILE, /* The server is sending a pack file. */
137 GOTD_IMSG_RECV_PACKFILE, /* The server is receiving a pack file. */
138 GOTD_IMSG_PACKIDX_FILE, /* Temporary file handle for new pack index. */
139 GOTD_IMSG_PACKFILE_PIPE, /* Pipe to send/receive a pack file stream. */
140 GOTD_IMSG_PACKFILE_PROGRESS, /* Progress reporting. */
141 GOTD_IMSG_PACKFILE_READY, /* Pack file is ready to be sent. */
142 GOTD_IMSG_PACKFILE_STATUS, /* Received pack success/failure status. */
143 GOTD_IMSG_PACKFILE_INSTALL, /* Received pack file can be installed. */
144 GOTD_IMSG_PACKFILE_DONE, /* Pack file has been sent/received. */
146 /* Reference updates. */
147 GOTD_IMSG_REF_UPDATES_START, /* Ref updates starting. */
148 GOTD_IMSG_REF_UPDATE_OK, /* Update went OK. */
149 GOTD_IMSG_REF_UPDATE_NG, /* Update was not good. */
150 GOTD_IMSG_REFS_UPDATED, /* The server proccessed all ref updates. */
152 /* Client is disconnecting. */
153 GOTD_IMSG_DISCONNECT,
154 };
156 /* Structure for GOTD_IMSG_ERROR. */
157 struct gotd_imsg_error {
158 int code; /* an error code from got_error.h */
159 int errno_code; /* in case code equals GOT_ERR_ERRNO */
160 uint32_t client_id;
161 char msg[GOT_ERR_MAX_MSG_SIZE];
162 } __attribute__((__packed__));
164 /* Structure for GOTD_IMSG_INFO. */
165 struct gotd_imsg_info {
166 pid_t pid;
167 int verbosity;
168 int nrepos;
169 int nclients;
171 /* Followed by nrepos GOTD_IMSG_INFO_REPO messages. */
172 /* Followed by nclients GOTD_IMSG_INFO_CLIENT messages. */
173 };
175 /* Structure for GOTD_IMSG_INFO_REPO. */
176 struct gotd_imsg_info_repo {
177 char repo_name[NAME_MAX];
178 char repo_path[PATH_MAX];
179 };
181 /* Structure for GOTD_IMSG_INFO_CLIENT */
182 struct gotd_imsg_info_client {
183 uid_t euid;
184 gid_t egid;
185 char repo_name[NAME_MAX];
186 int is_writing;
187 enum gotd_client_state state;
188 size_t ncapabilities;
190 /* Followed by ncapabilities GOTD_IMSG_CAPABILITY. */
191 };
193 /* Structure for GOTD_IMSG_LIST_REFS. */
194 struct gotd_imsg_list_refs {
195 char repo_name[NAME_MAX];
196 int client_is_reading; /* 1 if reading, 0 if writing */
197 };
199 /* Structure for GOTD_IMSG_LIST_REFS_INTERNAL. */
200 struct gotd_imsg_list_refs_internal {
201 uint32_t client_id;
202 };
204 /* Structure for GOTD_IMSG_REFLIST. */
205 struct gotd_imsg_reflist {
206 size_t nrefs;
208 /* Followed by nrefs times of gotd_imsg_ref/gotd_imsg_symref data. */
209 } __attribute__((__packed__));
211 /* Structure for GOTD_IMSG_REF data. */
212 struct gotd_imsg_ref {
213 uint8_t id[SHA1_DIGEST_LENGTH];
214 size_t name_len;
215 /* Followed by name_len data bytes. */
216 } __attribute__((__packed__));
218 /* Structure for GOTD_IMSG_SYMREF data. */
219 struct gotd_imsg_symref {
220 size_t name_len;
221 size_t target_len;
222 uint8_t target_id[SHA1_DIGEST_LENGTH];
224 /*
225 * Followed by name_len + target_len data bytes.
226 */
227 } __attribute__((__packed__));
229 /* Structure for GOTD_IMSG_CAPABILITIES data. */
230 struct gotd_imsg_capabilities {
231 size_t ncapabilities;
233 /*
234 * Followed by ncapabilities * GOTD_IMSG_CAPABILITY.
235 */
236 } __attribute__((__packed__));
238 /* Structure for GOTD_IMSG_CAPABILITY data. */
239 struct gotd_imsg_capability {
240 size_t key_len;
241 size_t value_len;
243 /*
244 * Followed by key_len + value_len data bytes.
245 */
246 } __attribute__((__packed__));
248 /* Structure for GOTD_IMSG_WANT data. */
249 struct gotd_imsg_want {
250 uint8_t object_id[SHA1_DIGEST_LENGTH];
251 uint32_t client_id;
252 } __attribute__((__packed__));
254 /* Structure for GOTD_IMSG_HAVE data. */
255 struct gotd_imsg_have {
256 uint8_t object_id[SHA1_DIGEST_LENGTH];
257 uint32_t client_id;
258 } __attribute__((__packed__));
260 /* Structure for GOTD_IMSG_ACK data. */
261 struct gotd_imsg_ack {
262 uint8_t object_id[SHA1_DIGEST_LENGTH];
263 uint32_t client_id;
264 } __attribute__((__packed__));
266 /* Structure for GOTD_IMSG_NAK data. */
267 struct gotd_imsg_nak {
268 uint8_t object_id[SHA1_DIGEST_LENGTH];
269 uint32_t client_id;
270 } __attribute__((__packed__));
272 /* Structure for GOTD_IMSG_PACKFILE_STATUS data. */
273 struct gotd_imsg_packfile_status {
274 size_t reason_len;
276 /* Followed by reason_len data bytes. */
277 } __attribute__((__packed__));
280 /* Structure for GOTD_IMSG_REF_UPDATE data. */
281 struct gotd_imsg_ref_update {
282 uint8_t old_id[SHA1_DIGEST_LENGTH];
283 uint8_t new_id[SHA1_DIGEST_LENGTH];
284 int ref_is_new;
285 uint32_t client_id;
286 size_t name_len;
288 /* Followed by name_len data bytes. */
289 } __attribute__((__packed__));
291 /* Structure for GOTD_IMSG_REF_UPDATES_START data. */
292 struct gotd_imsg_ref_updates_start {
293 int nref_updates;
294 uint32_t client_id;
296 /* Followed by nref_updates GOT_IMSG_REF_UPDATE_OK/NG messages. */
297 };
299 /* Structure for GOTD_IMSG_REF_UPDATE_OK data. */
300 struct gotd_imsg_ref_update_ok {
301 uint8_t old_id[SHA1_DIGEST_LENGTH];
302 uint8_t new_id[SHA1_DIGEST_LENGTH];
303 int ref_is_new;
304 uint32_t client_id;
305 size_t name_len;
307 /* Followed by name_len data bytes. */
308 } __attribute__((__packed__));
310 /* Structure for GOTD_IMSG_REF_UPDATE_NG data. */
311 struct gotd_imsg_ref_update_ng {
312 uint8_t old_id[SHA1_DIGEST_LENGTH];
313 uint8_t new_id[SHA1_DIGEST_LENGTH];
314 uint32_t client_id;
315 size_t name_len;
316 size_t reason_len;
318 /* Followed by name_len + reason_len data bytes. */
319 } __attribute__((__packed__));
321 /* Structure for GOTD_IMSG_SEND_PACKFILE data. */
322 struct gotd_imsg_send_packfile {
323 uint32_t client_id;
324 int report_progress;
326 /* delta cache file is sent as a file descriptor */
328 /* followed by two GOTD_IMSG_PACKFILE_PIPE messages */
329 };
331 /* Structure for GOTD_IMSG_RECV_PACKFILE data. */
332 struct gotd_imsg_recv_packfile {
333 uint32_t client_id;
334 int report_status;
336 /* pack destination temp file is sent as a file descriptor */
337 };
339 /* Structure for GOTD_IMSG_PACKFILE_PIPE data. */
340 struct gotd_imsg_packfile_pipe {
341 uint32_t client_id;
342 };
344 /* Structure for GOTD_IMSG_PACKIDX_FILE data. */
345 struct gotd_imsg_packidx_file {
346 uint32_t client_id;
347 };
350 /*
351 * Structure for GOTD_IMSG_PACKFILE_PROGRESS and
352 * GOTD_IMSG_PACKFILE_READY data.
353 */
354 struct gotd_imsg_packfile_progress {
355 uint32_t client_id;
356 int ncolored;
357 int nfound;
358 int ntrees;
359 off_t packfile_size;
360 int ncommits;
361 int nobj_total;
362 int nobj_deltify;
363 int nobj_written;
364 };
366 /* Structure for GOTD_IMSG_PACKFILE_INSTALL. */
367 struct gotd_imsg_packfile_install {
368 uint32_t client_id;
369 uint8_t pack_sha1[SHA1_DIGEST_LENGTH];
370 };
372 /* Structure for GOTD_IMSG_PACKFILE_DONE data. */
373 struct gotd_imsg_packfile_done {
374 uint32_t client_id;
375 };
377 /* Structure for GOTD_IMSG_DISCONNECT data. */
378 struct gotd_imsg_disconnect {
379 uint32_t client_id;
380 };
382 int parse_config(const char *, enum gotd_procid, struct gotd *);
384 /* imsg.c */
385 const struct got_error *gotd_imsg_flush(struct imsgbuf *);
386 const struct got_error *gotd_imsg_recv(struct imsg *, struct imsgbuf *, size_t);
387 const struct got_error *gotd_imsg_poll_recv(struct imsg *, struct imsgbuf *,
388 size_t);
389 const struct got_error *gotd_imsg_recv_error(uint32_t *client_id,
390 struct imsg *imsg);
391 int gotd_imsg_send_error(struct imsgbuf *ibuf, uint32_t, uint32_t,
392 const struct got_error *);
393 int gotd_imsg_send_error_event(struct gotd_imsgev *, uint32_t, uint32_t,
394 const struct got_error *);
395 void gotd_imsg_event_add(struct gotd_imsgev *);
396 int gotd_imsg_compose_event(struct gotd_imsgev *, uint16_t, uint32_t, int,
397 void *, uint16_t);
398 int gotd_imsg_forward(struct gotd_imsgev *, struct imsg *, int);
400 void gotd_imsg_send_ack(struct got_object_id *, struct imsgbuf *,
401 uint32_t, pid_t);
402 void gotd_imsg_send_nak(struct got_object_id *, struct imsgbuf *,
403 uint32_t, pid_t);