Blob


1 /*
2 * Copyright (c) 2018 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 */
17 /*
18 * All code runs under the same UID but sensitive code paths are
19 * run in a separate process with tighter pledge(2) promises.
20 * Data is communicated between processes via imsg_flush(3) and imsg_read(3).
21 * This behaviour is transparent to users of the library.
22 *
23 * We generally transmit data in imsg buffers, split across several messages
24 * if necessary. File descriptor passing is used in cases where this is
25 * impractical, such as when accessing pack files or when transferring
26 * large blobs.
27 *
28 * We currently do not exec(2) after a fork(2).
29 * To achieve fork+exec, relevant parts of our library functionality could
30 * be made accessible via separate executables in a libexec directory.
31 */
33 enum got_imsg_type {
34 /* An error occured while processing a request. */
35 GOT_IMSG_ERROR,
37 /* Messages for transmitting deltas and associated delta streams. */
38 GOT_IMSG_DELTA,
39 GOT_IMSG_DELTA_STREAM,
41 /*
42 * Messages concerned with read access to objects in a repository.
43 * Object and pack files are opened by the main process, where
44 * data may be read as a byte string but without any interpretation.
45 * Decompression and parsing of object and pack files occurs in a
46 * separate process which runs under pledge("stdio").
47 * This sandboxes our own repository parsing code, as well as zlib.
48 */
49 GOT_IMSG_OBJECT,
50 GOT_IMSG_COMMIT,
51 GOT_IMSG_OBJ_ID,
52 GOT_IMSG_LOOSE_BLOB_OBJECT_REQUEST,
53 GOT_IMSG_LOOSE_TREE_OBJECT_REQUEST,
54 GOT_IMSG_PACKED_BLOB_OBJECT_REQUEST,
55 GOT_IMSG_PACKED_TREE_OBJECT_REQUEST,
56 GOT_IMSG_PACKED_COMMIT_OBJECT_REQUEST,
57 GOT_IMSG_BLOB_OBJECT_REQUEST_OUTPUT,
58 GOT_IMSG_BLOB_OBJECT_REPLY,
59 GOT_IMSG_TREE_OBJECT_REPLY,
60 GOT_IMSG_TREE_ENTRY,
61 GOT_IMSG_COMMIT_OBJECT_REPLY
62 };
64 /* Structure for GOT_IMSG_ERROR. */
65 struct got_imsg_error {
66 int code; /* an error code from got_error.h */
67 int errno_code; /* in case code equals GOT_ERR_ERRNO */
68 };
70 /* Structure for GOT_IMSG_DELTA data. */
71 struct got_imsg_delta {
72 /* These fields are the same as in struct got_delta. */
73 off_t offset;
74 size_t tslen;
75 int type;
76 size_t size;
77 off_t data_offset;
78 size_t delta_len;
80 /*
81 * Followed by delta stream in remaining bytes of imsg buffer.
82 * If delta_len exceeds imsg buffer length, followed by one or
83 * more DELTA_STREAM messages until delta_len bytes of delta
84 * stream have been transmitted.
85 */
86 };
88 /* Structure for GOT_IMSG_DELTA_STREAM data. */
89 struct got_imsg_delta_stream {
90 /*
91 * Empty since the following is implied:
92 * Read additional delta stream data from imsg buffer.
93 */
94 };
96 /* Structure for GOT_IMSG_OBJECT data. */
97 struct got_imsg_object {
98 /* These fields are the same as in struct got_object. */
99 int type;
100 int flags;
101 size_t hdrlen;
102 size_t size;
104 int ndeltas; /* this many GOT_IMSG_DELTA messages follow */
105 };
107 struct got_imsg_commit_object {
108 uint8_t tree_id[SHA1_DIGEST_STRING_LENGTH];
109 size_t author_len;
110 size_t committer_len;
111 size_t logmsg_len;
112 int nparents;
114 /* Followed by author_len + committer_len + logmsg_len data bytes */
116 /* Followed by 'nparents' SHA1_DIGEST_STRING_LENGTH length strings */
118 /* XXX should use more messages to support very large log messages */
119 } __attribute__((__packed__));
121 /* Structure for GOT_IMSG_LOOSE_OBJECT_HEADER_REPLY data. */
122 struct got_imsg_loose_object_header_reply {
123 struct got_imsg_object iobj;
124 };
126 /* Structure for GOT_IMSG_LOOSE_BLOB_OBJECT_REQUEST data. */
127 struct got_imsg_loose_blob_object_request {
128 struct got_imsg_object iobj;
130 /*
131 * The following is implied: If imsg fd == -1 then read raw
132 * blob data from imsg buffer, else read from fd.
134 * This message is followed by an OBJECT_REQUEST_OUTPUT message.
135 */
136 };
138 /* Structure for GOT_IMSG_BLOB_OBJECT_REQUEST_OUTPUT data. */
139 struct got_imsg_blob_object_request_output {
140 /*
141 * Empty since the following is implied: If imsg fd == -1 then
142 * respond with blob data in imsg buffer, else write to fd.
143 */
144 };
146 /* Structure for GOT_IMSG_LOOSE_TREE_OBJECT_REQUEST data. */
147 struct got_imsg_loose_tree_object_request {
148 struct got_imsg_object iobj;
150 /*
151 * The following is implied: If imsg fd == -1 then read raw tree
152 * data from imsg buffer, else read from fd.
153 */
154 };
156 /* Structure for GOT_IMSG_TREE_ENTRY. */
157 struct got_imsg_tree_entry {
158 struct got_object_id id;
159 mode_t mode;
160 /* Followed by entry's name in remaining data of imsg buffer. */
161 } __attribute__((__packed__));
163 /* Structure for GOT_IMSG_TREE_OBJECT_REPLY data. */
164 struct got_imsg_tree_object {
165 int nentries; /* This many TREE_ENTRY messages follow. */
166 };
168 void got_privsep_send_error(struct imsgbuf *, const struct got_error *);
169 const struct got_error *got_privsep_send_obj(struct imsgbuf *,
170 struct got_object *, int);
171 const struct got_error *got_privsep_recv_obj(struct got_object **,
172 struct imsgbuf *);
173 const struct got_error *got_privsep_send_commit_obj(struct imsgbuf *,
174 struct got_commit_object *);
175 const struct got_error *got_privsep_recv_commit_obj(struct got_commit_object **,
176 struct imsgbuf *);
178 /* TODO: Implement the above, and then add more message data types here. */