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 descriptors are used in cases where this is impractical,
25 * such as when accessing pack files or when transferring large blobs.
26 *
27 * We exec(2) after a fork(2). Parts of our library functionality are
28 * accessible via separate executables in a libexec directory.
29 */
31 #define GOT_IMSG_FD_CHILD (STDERR_FILENO + 1)
33 #ifndef GOT_LIBEXECDIR
34 #define GOT_LIBEXECDIR /usr/libexec
35 #endif
37 /* Names of helper programs in libexec directory */
38 #define GOT_PROG_READ_OBJECT got-read-object
39 #define GOT_PROG_READ_TREE got-read-tree
40 #define GOT_PROG_READ_COMMIT got-read-commit
41 #define GOT_PROG_READ_BLOB got-read-blob
43 #define GOT_STRINGIFY(x) #x
44 #define GOT_STRINGVAL(x) GOT_STRINGIFY(x)
46 /* Paths to helper programs in libexec directory */
47 #define GOT_PATH_PROG_READ_OBJECT \
48 GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_OBJECT)
49 #define GOT_PATH_PROG_READ_TREE \
50 GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_TREE)
51 #define GOT_PATH_PROG_READ_COMMIT \
52 GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_COMMIT)
53 #define GOT_PATH_PROG_READ_BLOB \
54 GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_BLOB)
56 enum got_imsg_type {
57 /* An error occured while processing a request. */
58 GOT_IMSG_ERROR,
60 /* Stop the child process. */
61 GOT_IMSG_STOP,
63 /*
64 * Messages concerned with read access to objects in a repository.
65 * Object and pack files are opened by the main process, where
66 * data may be read as a byte string but without any interpretation.
67 * Decompression and parsing of object and pack files occurs in a
68 * separate process which runs under pledge("stdio").
69 * This sandboxes our own repository parsing code, as well as zlib.
70 */
71 GOT_IMSG_OBJECT_REQUEST,
72 GOT_IMSG_OBJECT,
73 GOT_IMSG_COMMIT_REQUEST,
74 GOT_IMSG_COMMIT,
75 GOT_IMSG_TREE_REQUEST,
76 GOT_IMSG_TREE,
77 GOT_IMSG_TREE_ENTRY,
78 GOT_IMSG_BLOB_REQUEST,
79 GOT_IMSG_BLOB_OUTFD,
80 GOT_IMSG_BLOB,
81 /* Messages for transmitting deltas and associated delta streams: */
82 GOT_IMSG_DELTA,
83 GOT_IMSG_DELTA_STREAM,
84 };
86 /* Structure for GOT_IMSG_ERROR. */
87 struct got_imsg_error {
88 int code; /* an error code from got_error.h */
89 int errno_code; /* in case code equals GOT_ERR_ERRNO */
90 };
92 /* Structure for GOT_IMSG_DELTA data. */
93 struct got_imsg_delta {
94 /* These fields are the same as in struct got_delta. */
95 off_t offset;
96 size_t tslen;
97 int type;
98 size_t size;
99 off_t data_offset;
100 size_t delta_len;
102 /*
103 * Followed by delta stream in remaining bytes of imsg buffer.
104 * If delta_len exceeds imsg buffer length, followed by one or
105 * more DELTA_STREAM messages until delta_len bytes of delta
106 * stream have been transmitted.
107 */
108 };
110 /* Structure for GOT_IMSG_DELTA_STREAM data. */
111 struct got_imsg_delta_stream {
112 /*
113 * Empty since the following is implied:
114 * Read additional delta stream data from imsg buffer.
115 */
116 };
118 /*
119 * Structure for GOT_IMSG_TREE_REQUEST, GOT_IMSG_COMMIT_REQUEST,
120 * and GOT_IMSG_OBJECT data.
121 */
122 struct got_imsg_object {
123 /* These fields are the same as in struct got_object. */
124 int type;
125 int flags;
126 size_t hdrlen;
127 size_t size;
129 int ndeltas; /* this many GOT_IMSG_DELTA messages follow */
130 };
132 /* Structure for GOT_IMSG_COMMIT data. */
133 struct got_imsg_commit_object {
134 uint8_t tree_id[SHA1_DIGEST_LENGTH];
135 size_t author_len;
136 struct tm tm_author;
137 size_t committer_len;
138 struct tm tm_committer;
139 size_t logmsg_len;
140 int nparents;
142 /*
143 * Followed by author_len + committer_len + logmsg_len data bytes
144 */
146 /* Followed by 'nparents' SHA1_DIGEST_LENGTH length strings */
148 /* XXX should use more messages to support very large log messages */
149 } __attribute__((__packed__));
152 /* Structure for GOT_IMSG_TREE_ENTRY. */
153 struct got_imsg_tree_entry {
154 char id[SHA1_DIGEST_LENGTH];
155 mode_t mode;
156 /* Followed by entry's name in remaining data of imsg buffer. */
157 } __attribute__((__packed__));
159 /* Structure for GOT_IMSG_TREE_OBJECT_REPLY data. */
160 struct got_imsg_tree_object {
161 int nentries; /* This many TREE_ENTRY messages follow. */
162 };
164 /* Structure for GOT_IMSG_BLOB. */
165 struct got_imsg_blob {
166 size_t size;
167 };
169 const struct got_error *got_privsep_send_stop(int);
170 const struct got_error *got_privsep_recv_imsg(struct imsg *, struct imsgbuf *,
171 size_t);
172 void got_privsep_send_error(struct imsgbuf *, const struct got_error *);
173 const struct got_error *got_privsep_send_obj_req(struct imsgbuf *, int,
174 struct got_object *);
175 const struct got_error *got_privsep_send_blob_req(struct imsgbuf *, int, int);
176 const struct got_error *got_privsep_send_obj(struct imsgbuf *,
177 struct got_object *, int);
178 const struct got_error *got_privsep_recv_obj(struct got_object **,
179 struct imsgbuf *);
180 const struct got_error *got_privsep_send_commit(struct imsgbuf *,
181 struct got_commit_object *);
182 const struct got_error *got_privsep_recv_commit(struct got_commit_object **,
183 struct imsgbuf *);
184 const struct got_error *got_privsep_recv_tree(struct got_tree_object **,
185 struct imsgbuf *);
186 const struct got_error *got_privsep_send_tree(struct imsgbuf *,
187 struct got_tree_object *);
188 const struct got_error *got_privsep_send_blob(struct imsgbuf *, size_t);
189 const struct got_error *got_privsep_recv_blob(size_t *, struct imsgbuf *);
191 /* TODO: Implement the above, and then add more message data types here. */