Blame


1 7be7cc45 2018-04-02 stsp /*
2 7be7cc45 2018-04-02 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 7be7cc45 2018-04-02 stsp *
4 7be7cc45 2018-04-02 stsp * Permission to use, copy, modify, and distribute this software for any
5 7be7cc45 2018-04-02 stsp * purpose with or without fee is hereby granted, provided that the above
6 7be7cc45 2018-04-02 stsp * copyright notice and this permission notice appear in all copies.
7 7be7cc45 2018-04-02 stsp *
8 7be7cc45 2018-04-02 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 7be7cc45 2018-04-02 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 7be7cc45 2018-04-02 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 7be7cc45 2018-04-02 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 7be7cc45 2018-04-02 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 7be7cc45 2018-04-02 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 7be7cc45 2018-04-02 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 7be7cc45 2018-04-02 stsp */
16 7be7cc45 2018-04-02 stsp
17 7be7cc45 2018-04-02 stsp /*
18 7be7cc45 2018-04-02 stsp * All code runs under the same UID but sensitive code paths are
19 7be7cc45 2018-04-02 stsp * run in a separate process with tighter pledge(2) promises.
20 2ca3a24b 2018-04-02 stsp * Data is communicated between processes via imsg_flush(3) and imsg_read(3).
21 7be7cc45 2018-04-02 stsp * This behaviour is transparent to users of the library.
22 7be7cc45 2018-04-02 stsp *
23 1e4880cb 2018-04-02 stsp * We generally transmit data in imsg buffers, split across several messages
24 ff6b18f8 2018-04-24 stsp * if necessary. File descriptors are used in cases where this is impractical,
25 ff6b18f8 2018-04-24 stsp * such as when accessing pack files or when transferring large blobs.
26 7be7cc45 2018-04-02 stsp *
27 ad242220 2018-09-08 stsp * We exec(2) after a fork(2). Parts of our library functionality are
28 ad242220 2018-09-08 stsp * accessible via separate executables in a libexec directory.
29 7be7cc45 2018-04-02 stsp */
30 7be7cc45 2018-04-02 stsp
31 ad242220 2018-09-08 stsp #define GOT_IMSG_FD_CHILD (STDERR_FILENO + 1)
32 ad242220 2018-09-08 stsp
33 ad242220 2018-09-08 stsp #ifndef GOT_LIBEXECDIR
34 ad242220 2018-09-08 stsp #define GOT_LIBEXECDIR /usr/libexec
35 ad242220 2018-09-08 stsp #endif
36 ad242220 2018-09-08 stsp
37 ad242220 2018-09-08 stsp /* Names of helper programs in libexec directory */
38 ad242220 2018-09-08 stsp #define GOT_PROG_READ_OBJECT got-read-object
39 ad242220 2018-09-08 stsp #define GOT_PROG_READ_TREE got-read-tree
40 ad242220 2018-09-08 stsp #define GOT_PROG_READ_COMMIT got-read-commit
41 ad242220 2018-09-08 stsp #define GOT_PROG_READ_BLOB got-read-blob
42 ad242220 2018-09-08 stsp
43 ad242220 2018-09-08 stsp #define GOT_STRINGIFY(x) #x
44 ad242220 2018-09-08 stsp #define GOT_STRINGVAL(x) GOT_STRINGIFY(x)
45 ad242220 2018-09-08 stsp
46 ad242220 2018-09-08 stsp /* Paths to helper programs in libexec directory */
47 ad242220 2018-09-08 stsp #define GOT_PATH_PROG_READ_OBJECT \
48 ad242220 2018-09-08 stsp GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_OBJECT)
49 ad242220 2018-09-08 stsp #define GOT_PATH_PROG_READ_TREE \
50 ad242220 2018-09-08 stsp GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_TREE)
51 ad242220 2018-09-08 stsp #define GOT_PATH_PROG_READ_COMMIT \
52 ad242220 2018-09-08 stsp GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_COMMIT)
53 ad242220 2018-09-08 stsp #define GOT_PATH_PROG_READ_BLOB \
54 ad242220 2018-09-08 stsp GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_BLOB)
55 ad242220 2018-09-08 stsp
56 7be7cc45 2018-04-02 stsp enum got_imsg_type {
57 c025a41e 2018-04-02 stsp /* An error occured while processing a request. */
58 c025a41e 2018-04-02 stsp GOT_IMSG_ERROR,
59 c025a41e 2018-04-02 stsp
60 ad242220 2018-09-08 stsp /* Stop the child process. */
61 ad242220 2018-09-08 stsp GOT_IMSG_STOP,
62 1e4880cb 2018-04-02 stsp
63 7be7cc45 2018-04-02 stsp /*
64 7be7cc45 2018-04-02 stsp * Messages concerned with read access to objects in a repository.
65 7be7cc45 2018-04-02 stsp * Object and pack files are opened by the main process, where
66 7be7cc45 2018-04-02 stsp * data may be read as a byte string but without any interpretation.
67 7be7cc45 2018-04-02 stsp * Decompression and parsing of object and pack files occurs in a
68 7be7cc45 2018-04-02 stsp * separate process which runs under pledge("stdio").
69 7be7cc45 2018-04-02 stsp * This sandboxes our own repository parsing code, as well as zlib.
70 7be7cc45 2018-04-02 stsp */
71 ad242220 2018-09-08 stsp GOT_IMSG_OBJECT_REQUEST,
72 2178c42e 2018-04-22 stsp GOT_IMSG_OBJECT,
73 ad242220 2018-09-08 stsp GOT_IMSG_COMMIT_REQUEST,
74 bff6ca00 2018-04-23 stsp GOT_IMSG_COMMIT,
75 ad242220 2018-09-08 stsp GOT_IMSG_TREE_REQUEST,
76 366d86ca 2018-04-23 stsp GOT_IMSG_TREE,
77 d80ab12b 2018-04-02 stsp GOT_IMSG_TREE_ENTRY,
78 ad242220 2018-09-08 stsp GOT_IMSG_BLOB_REQUEST,
79 ad242220 2018-09-08 stsp GOT_IMSG_BLOB_OUTFD,
80 366d86ca 2018-04-23 stsp GOT_IMSG_BLOB,
81 ad242220 2018-09-08 stsp /* Messages for transmitting deltas and associated delta streams: */
82 ad242220 2018-09-08 stsp GOT_IMSG_DELTA,
83 ad242220 2018-09-08 stsp GOT_IMSG_DELTA_STREAM,
84 7be7cc45 2018-04-02 stsp };
85 7be7cc45 2018-04-02 stsp
86 c025a41e 2018-04-02 stsp /* Structure for GOT_IMSG_ERROR. */
87 c025a41e 2018-04-02 stsp struct got_imsg_error {
88 c025a41e 2018-04-02 stsp int code; /* an error code from got_error.h */
89 2178c42e 2018-04-22 stsp int errno_code; /* in case code equals GOT_ERR_ERRNO */
90 c025a41e 2018-04-02 stsp };
91 c025a41e 2018-04-02 stsp
92 1e4880cb 2018-04-02 stsp /* Structure for GOT_IMSG_DELTA data. */
93 1e4880cb 2018-04-02 stsp struct got_imsg_delta {
94 1e4880cb 2018-04-02 stsp /* These fields are the same as in struct got_delta. */
95 1e4880cb 2018-04-02 stsp off_t offset;
96 1e4880cb 2018-04-02 stsp size_t tslen;
97 1e4880cb 2018-04-02 stsp int type;
98 1e4880cb 2018-04-02 stsp size_t size;
99 1e4880cb 2018-04-02 stsp off_t data_offset;
100 1e4880cb 2018-04-02 stsp size_t delta_len;
101 1e4880cb 2018-04-02 stsp
102 1e4880cb 2018-04-02 stsp /*
103 48f392b2 2018-04-02 stsp * Followed by delta stream in remaining bytes of imsg buffer.
104 48f392b2 2018-04-02 stsp * If delta_len exceeds imsg buffer length, followed by one or
105 48f392b2 2018-04-02 stsp * more DELTA_STREAM messages until delta_len bytes of delta
106 48f392b2 2018-04-02 stsp * stream have been transmitted.
107 1e4880cb 2018-04-02 stsp */
108 1e4880cb 2018-04-02 stsp };
109 1e4880cb 2018-04-02 stsp
110 1e4880cb 2018-04-02 stsp /* Structure for GOT_IMSG_DELTA_STREAM data. */
111 1e4880cb 2018-04-02 stsp struct got_imsg_delta_stream {
112 1e4880cb 2018-04-02 stsp /*
113 1e4880cb 2018-04-02 stsp * Empty since the following is implied:
114 48f392b2 2018-04-02 stsp * Read additional delta stream data from imsg buffer.
115 1e4880cb 2018-04-02 stsp */
116 1e4880cb 2018-04-02 stsp };
117 1e4880cb 2018-04-02 stsp
118 ad242220 2018-09-08 stsp /*
119 ad242220 2018-09-08 stsp * Structure for GOT_IMSG_TREE_REQUEST, GOT_IMSG_COMMIT_REQUEST,
120 ad242220 2018-09-08 stsp * and GOT_IMSG_OBJECT data.
121 ad242220 2018-09-08 stsp */
122 f7171542 2018-04-02 stsp struct got_imsg_object {
123 7be7cc45 2018-04-02 stsp /* These fields are the same as in struct got_object. */
124 7be7cc45 2018-04-02 stsp int type;
125 7be7cc45 2018-04-02 stsp int flags;
126 7be7cc45 2018-04-02 stsp size_t hdrlen;
127 7be7cc45 2018-04-02 stsp size_t size;
128 7be7cc45 2018-04-02 stsp
129 7be7cc45 2018-04-02 stsp int ndeltas; /* this many GOT_IMSG_DELTA messages follow */
130 94fbf93a 2018-04-22 stsp };
131 7be7cc45 2018-04-02 stsp
132 366d86ca 2018-04-23 stsp /* Structure for GOT_IMSG_COMMIT data. */
133 bff6ca00 2018-04-23 stsp struct got_imsg_commit_object {
134 86acc566 2018-04-23 stsp uint8_t tree_id[SHA1_DIGEST_LENGTH];
135 bff6ca00 2018-04-23 stsp size_t author_len;
136 788c352e 2018-06-16 stsp struct tm tm_author;
137 bff6ca00 2018-04-23 stsp size_t committer_len;
138 788c352e 2018-06-16 stsp struct tm tm_committer;
139 bff6ca00 2018-04-23 stsp size_t logmsg_len;
140 bff6ca00 2018-04-23 stsp int nparents;
141 bff6ca00 2018-04-23 stsp
142 6c281f94 2018-06-11 stsp /*
143 788c352e 2018-06-16 stsp * Followed by author_len + committer_len + logmsg_len data bytes
144 6c281f94 2018-06-11 stsp */
145 bff6ca00 2018-04-23 stsp
146 86acc566 2018-04-23 stsp /* Followed by 'nparents' SHA1_DIGEST_LENGTH length strings */
147 bff6ca00 2018-04-23 stsp
148 bff6ca00 2018-04-23 stsp /* XXX should use more messages to support very large log messages */
149 bff6ca00 2018-04-23 stsp } __attribute__((__packed__));
150 bff6ca00 2018-04-23 stsp
151 f7171542 2018-04-02 stsp
152 48f392b2 2018-04-02 stsp /* Structure for GOT_IMSG_TREE_ENTRY. */
153 48f392b2 2018-04-02 stsp struct got_imsg_tree_entry {
154 e033d803 2018-04-23 stsp char id[SHA1_DIGEST_LENGTH];
155 48f392b2 2018-04-02 stsp mode_t mode;
156 48f392b2 2018-04-02 stsp /* Followed by entry's name in remaining data of imsg buffer. */
157 8d98bcfb 2018-04-02 stsp } __attribute__((__packed__));
158 48f392b2 2018-04-02 stsp
159 d80ab12b 2018-04-02 stsp /* Structure for GOT_IMSG_TREE_OBJECT_REPLY data. */
160 48f392b2 2018-04-02 stsp struct got_imsg_tree_object {
161 48f392b2 2018-04-02 stsp int nentries; /* This many TREE_ENTRY messages follow. */
162 48f392b2 2018-04-02 stsp };
163 48f392b2 2018-04-02 stsp
164 2967a784 2018-04-24 stsp /* Structure for GOT_IMSG_BLOB. */
165 2967a784 2018-04-24 stsp struct got_imsg_blob {
166 2967a784 2018-04-24 stsp size_t size;
167 2967a784 2018-04-24 stsp };
168 2967a784 2018-04-24 stsp
169 ad242220 2018-09-08 stsp const struct got_error *got_privsep_send_stop(int);
170 ad242220 2018-09-08 stsp const struct got_error *got_privsep_recv_imsg(struct imsg *, struct imsgbuf *,
171 ad242220 2018-09-08 stsp size_t);
172 2178c42e 2018-04-22 stsp void got_privsep_send_error(struct imsgbuf *, const struct got_error *);
173 ad242220 2018-09-08 stsp const struct got_error *got_privsep_send_obj_req(struct imsgbuf *, int,
174 ad242220 2018-09-08 stsp struct got_object *);
175 ad242220 2018-09-08 stsp const struct got_error *got_privsep_send_blob_req(struct imsgbuf *, int, int);
176 2178c42e 2018-04-22 stsp const struct got_error *got_privsep_send_obj(struct imsgbuf *,
177 2178c42e 2018-04-22 stsp struct got_object *, int);
178 2178c42e 2018-04-22 stsp const struct got_error *got_privsep_recv_obj(struct got_object **,
179 2178c42e 2018-04-22 stsp struct imsgbuf *);
180 068fd2bf 2018-04-24 stsp const struct got_error *got_privsep_send_commit(struct imsgbuf *,
181 bff6ca00 2018-04-23 stsp struct got_commit_object *);
182 068fd2bf 2018-04-24 stsp const struct got_error *got_privsep_recv_commit(struct got_commit_object **,
183 bff6ca00 2018-04-23 stsp struct imsgbuf *);
184 068fd2bf 2018-04-24 stsp const struct got_error *got_privsep_recv_tree(struct got_tree_object **,
185 e033d803 2018-04-23 stsp struct imsgbuf *);
186 068fd2bf 2018-04-24 stsp const struct got_error *got_privsep_send_tree(struct imsgbuf *,
187 e033d803 2018-04-23 stsp struct got_tree_object *);
188 2967a784 2018-04-24 stsp const struct got_error *got_privsep_send_blob(struct imsgbuf *, size_t);
189 2967a784 2018-04-24 stsp const struct got_error *got_privsep_recv_blob(size_t *, struct imsgbuf *);
190 2178c42e 2018-04-22 stsp
191 7be7cc45 2018-04-02 stsp /* TODO: Implement the above, and then add more message data types here. */