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 876c234b 2018-09-10 stsp #define GOT_PROG_READ_PACK got-read-pack
43 ad242220 2018-09-08 stsp
44 ad242220 2018-09-08 stsp #define GOT_STRINGIFY(x) #x
45 ad242220 2018-09-08 stsp #define GOT_STRINGVAL(x) GOT_STRINGIFY(x)
46 ad242220 2018-09-08 stsp
47 ad242220 2018-09-08 stsp /* Paths to helper programs in libexec directory */
48 ad242220 2018-09-08 stsp #define GOT_PATH_PROG_READ_OBJECT \
49 ad242220 2018-09-08 stsp GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_OBJECT)
50 ad242220 2018-09-08 stsp #define GOT_PATH_PROG_READ_TREE \
51 ad242220 2018-09-08 stsp GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_TREE)
52 ad242220 2018-09-08 stsp #define GOT_PATH_PROG_READ_COMMIT \
53 ad242220 2018-09-08 stsp GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_COMMIT)
54 ad242220 2018-09-08 stsp #define GOT_PATH_PROG_READ_BLOB \
55 ad242220 2018-09-08 stsp GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_BLOB)
56 876c234b 2018-09-10 stsp #define GOT_PATH_PROG_READ_PACK \
57 876c234b 2018-09-10 stsp GOT_STRINGVAL(GOT_LIBEXECDIR) "/" GOT_STRINGVAL(GOT_PROG_READ_PACK)
58 ad242220 2018-09-08 stsp
59 876c234b 2018-09-10 stsp struct got_privsep_child {
60 876c234b 2018-09-10 stsp int imsg_fd;
61 876c234b 2018-09-10 stsp pid_t pid;
62 876c234b 2018-09-10 stsp struct imsgbuf *ibuf;
63 876c234b 2018-09-10 stsp };
64 876c234b 2018-09-10 stsp
65 7be7cc45 2018-04-02 stsp enum got_imsg_type {
66 c025a41e 2018-04-02 stsp /* An error occured while processing a request. */
67 c025a41e 2018-04-02 stsp GOT_IMSG_ERROR,
68 c025a41e 2018-04-02 stsp
69 ad242220 2018-09-08 stsp /* Stop the child process. */
70 ad242220 2018-09-08 stsp GOT_IMSG_STOP,
71 1e4880cb 2018-04-02 stsp
72 7be7cc45 2018-04-02 stsp /*
73 7be7cc45 2018-04-02 stsp * Messages concerned with read access to objects in a repository.
74 7be7cc45 2018-04-02 stsp * Object and pack files are opened by the main process, where
75 7be7cc45 2018-04-02 stsp * data may be read as a byte string but without any interpretation.
76 7be7cc45 2018-04-02 stsp * Decompression and parsing of object and pack files occurs in a
77 f0b0c746 2018-09-09 stsp * separate process which runs under pledge("stdio recvfd").
78 7be7cc45 2018-04-02 stsp * This sandboxes our own repository parsing code, as well as zlib.
79 7be7cc45 2018-04-02 stsp */
80 ad242220 2018-09-08 stsp GOT_IMSG_OBJECT_REQUEST,
81 2178c42e 2018-04-22 stsp GOT_IMSG_OBJECT,
82 ad242220 2018-09-08 stsp GOT_IMSG_COMMIT_REQUEST,
83 bff6ca00 2018-04-23 stsp GOT_IMSG_COMMIT,
84 c75f7264 2018-09-11 stsp GOT_IMSG_COMMIT_LOGMSG,
85 ad242220 2018-09-08 stsp GOT_IMSG_TREE_REQUEST,
86 366d86ca 2018-04-23 stsp GOT_IMSG_TREE,
87 d80ab12b 2018-04-02 stsp GOT_IMSG_TREE_ENTRY,
88 ad242220 2018-09-08 stsp GOT_IMSG_BLOB_REQUEST,
89 ad242220 2018-09-08 stsp GOT_IMSG_BLOB_OUTFD,
90 366d86ca 2018-04-23 stsp GOT_IMSG_BLOB,
91 876c234b 2018-09-10 stsp
92 876c234b 2018-09-10 stsp /* Messages related to pack files. */
93 876c234b 2018-09-10 stsp GOT_IMSG_PACKIDX,
94 876c234b 2018-09-10 stsp GOT_IMSG_PACK,
95 876c234b 2018-09-10 stsp GOT_IMSG_PACKED_OBJECT_REQUEST,
96 7be7cc45 2018-04-02 stsp };
97 7be7cc45 2018-04-02 stsp
98 c025a41e 2018-04-02 stsp /* Structure for GOT_IMSG_ERROR. */
99 c025a41e 2018-04-02 stsp struct got_imsg_error {
100 c025a41e 2018-04-02 stsp int code; /* an error code from got_error.h */
101 2178c42e 2018-04-22 stsp int errno_code; /* in case code equals GOT_ERR_ERRNO */
102 c025a41e 2018-04-02 stsp };
103 c025a41e 2018-04-02 stsp
104 ad242220 2018-09-08 stsp /*
105 ad242220 2018-09-08 stsp * Structure for GOT_IMSG_TREE_REQUEST, GOT_IMSG_COMMIT_REQUEST,
106 ad242220 2018-09-08 stsp * and GOT_IMSG_OBJECT data.
107 ad242220 2018-09-08 stsp */
108 f7171542 2018-04-02 stsp struct got_imsg_object {
109 c59b3346 2018-09-11 stsp uint8_t id[SHA1_DIGEST_LENGTH];
110 c59b3346 2018-09-11 stsp
111 7be7cc45 2018-04-02 stsp /* These fields are the same as in struct got_object. */
112 7be7cc45 2018-04-02 stsp int type;
113 7be7cc45 2018-04-02 stsp int flags;
114 7be7cc45 2018-04-02 stsp size_t hdrlen;
115 7be7cc45 2018-04-02 stsp size_t size;
116 876c234b 2018-09-10 stsp off_t pack_offset;
117 c59b3346 2018-09-11 stsp int pack_idx;
118 94fbf93a 2018-04-22 stsp };
119 7be7cc45 2018-04-02 stsp
120 366d86ca 2018-04-23 stsp /* Structure for GOT_IMSG_COMMIT data. */
121 bff6ca00 2018-04-23 stsp struct got_imsg_commit_object {
122 86acc566 2018-04-23 stsp uint8_t tree_id[SHA1_DIGEST_LENGTH];
123 bff6ca00 2018-04-23 stsp size_t author_len;
124 788c352e 2018-06-16 stsp struct tm tm_author;
125 bff6ca00 2018-04-23 stsp size_t committer_len;
126 788c352e 2018-06-16 stsp struct tm tm_committer;
127 bff6ca00 2018-04-23 stsp size_t logmsg_len;
128 bff6ca00 2018-04-23 stsp int nparents;
129 bff6ca00 2018-04-23 stsp
130 6c281f94 2018-06-11 stsp /*
131 c75f7264 2018-09-11 stsp * Followed by author_len + committer_len data bytes
132 6c281f94 2018-06-11 stsp */
133 bff6ca00 2018-04-23 stsp
134 86acc566 2018-04-23 stsp /* Followed by 'nparents' SHA1_DIGEST_LENGTH length strings */
135 bff6ca00 2018-04-23 stsp
136 c75f7264 2018-09-11 stsp /*
137 c75f7264 2018-09-11 stsp * Followed by 'logmsg_len' bytes of commit log message data in
138 c75f7264 2018-09-11 stsp * one or more GOT_IMSG_COMMIT_LOGMSG messages.
139 c75f7264 2018-09-11 stsp */
140 bff6ca00 2018-04-23 stsp } __attribute__((__packed__));
141 bff6ca00 2018-04-23 stsp
142 f7171542 2018-04-02 stsp
143 48f392b2 2018-04-02 stsp /* Structure for GOT_IMSG_TREE_ENTRY. */
144 48f392b2 2018-04-02 stsp struct got_imsg_tree_entry {
145 e033d803 2018-04-23 stsp char id[SHA1_DIGEST_LENGTH];
146 48f392b2 2018-04-02 stsp mode_t mode;
147 48f392b2 2018-04-02 stsp /* Followed by entry's name in remaining data of imsg buffer. */
148 8d98bcfb 2018-04-02 stsp } __attribute__((__packed__));
149 48f392b2 2018-04-02 stsp
150 d80ab12b 2018-04-02 stsp /* Structure for GOT_IMSG_TREE_OBJECT_REPLY data. */
151 48f392b2 2018-04-02 stsp struct got_imsg_tree_object {
152 48f392b2 2018-04-02 stsp int nentries; /* This many TREE_ENTRY messages follow. */
153 48f392b2 2018-04-02 stsp };
154 48f392b2 2018-04-02 stsp
155 2967a784 2018-04-24 stsp /* Structure for GOT_IMSG_BLOB. */
156 2967a784 2018-04-24 stsp struct got_imsg_blob {
157 2967a784 2018-04-24 stsp size_t size;
158 2967a784 2018-04-24 stsp };
159 2967a784 2018-04-24 stsp
160 876c234b 2018-09-10 stsp /* Structure for GOT_IMSG_PACKIDX. */
161 876c234b 2018-09-10 stsp struct got_imsg_packidx {
162 876c234b 2018-09-10 stsp size_t len;
163 876c234b 2018-09-10 stsp /* Additionally, a file desciptor is passed via imsg. */
164 876c234b 2018-09-10 stsp };
165 876c234b 2018-09-10 stsp
166 876c234b 2018-09-10 stsp /* Structure for GOT_IMSG_PACK. */
167 876c234b 2018-09-10 stsp struct got_imsg_pack {
168 876c234b 2018-09-10 stsp char path_packfile[PATH_MAX];
169 876c234b 2018-09-10 stsp size_t filesize;
170 876c234b 2018-09-10 stsp /* Additionally, a file desciptor is passed via imsg. */
171 876c234b 2018-09-10 stsp };
172 876c234b 2018-09-10 stsp
173 876c234b 2018-09-10 stsp /*
174 876c234b 2018-09-10 stsp * Structure for GOT_IMSG_PACKED_OBJECT_REQUEST data.
175 876c234b 2018-09-10 stsp */
176 876c234b 2018-09-10 stsp struct got_imsg_packed_object {
177 876c234b 2018-09-10 stsp int idx;
178 876c234b 2018-09-10 stsp };
179 876c234b 2018-09-10 stsp
180 876c234b 2018-09-10 stsp struct got_pack;
181 876c234b 2018-09-10 stsp struct got_packidx;
182 876c234b 2018-09-10 stsp
183 876c234b 2018-09-10 stsp const struct got_error *got_privsep_wait_for_child(pid_t);
184 ad242220 2018-09-08 stsp const struct got_error *got_privsep_send_stop(int);
185 ad242220 2018-09-08 stsp const struct got_error *got_privsep_recv_imsg(struct imsg *, struct imsgbuf *,
186 ad242220 2018-09-08 stsp size_t);
187 2178c42e 2018-04-22 stsp void got_privsep_send_error(struct imsgbuf *, const struct got_error *);
188 ad242220 2018-09-08 stsp const struct got_error *got_privsep_send_obj_req(struct imsgbuf *, int,
189 ad242220 2018-09-08 stsp struct got_object *);
190 55da3778 2018-09-10 stsp const struct got_error *got_privsep_send_blob_req(struct imsgbuf *, int);
191 55da3778 2018-09-10 stsp const struct got_error *got_privsep_send_blob_outfd(struct imsgbuf *, int);
192 2178c42e 2018-04-22 stsp const struct got_error *got_privsep_send_obj(struct imsgbuf *,
193 876c234b 2018-09-10 stsp struct got_object *);
194 cfd633c2 2018-09-10 stsp const struct got_error *got_privsep_get_imsg_obj(struct got_object **,
195 cfd633c2 2018-09-10 stsp struct imsg *, struct imsgbuf *);
196 2178c42e 2018-04-22 stsp const struct got_error *got_privsep_recv_obj(struct got_object **,
197 2178c42e 2018-04-22 stsp struct imsgbuf *);
198 068fd2bf 2018-04-24 stsp const struct got_error *got_privsep_send_commit(struct imsgbuf *,
199 bff6ca00 2018-04-23 stsp struct got_commit_object *);
200 068fd2bf 2018-04-24 stsp const struct got_error *got_privsep_recv_commit(struct got_commit_object **,
201 bff6ca00 2018-04-23 stsp struct imsgbuf *);
202 068fd2bf 2018-04-24 stsp const struct got_error *got_privsep_recv_tree(struct got_tree_object **,
203 e033d803 2018-04-23 stsp struct imsgbuf *);
204 068fd2bf 2018-04-24 stsp const struct got_error *got_privsep_send_tree(struct imsgbuf *,
205 e033d803 2018-04-23 stsp struct got_tree_object *);
206 2967a784 2018-04-24 stsp const struct got_error *got_privsep_send_blob(struct imsgbuf *, size_t);
207 2967a784 2018-04-24 stsp const struct got_error *got_privsep_recv_blob(size_t *, struct imsgbuf *);
208 876c234b 2018-09-10 stsp const struct got_error *got_privsep_init_pack_child(struct imsgbuf *,
209 876c234b 2018-09-10 stsp struct got_pack *, struct got_packidx *);
210 876c234b 2018-09-10 stsp const struct got_error *got_privsep_send_packed_obj_req(struct imsgbuf *, int);
211 876c234b 2018-09-10 stsp const struct got_error *got_privsep_send_pack_child_ready(struct imsgbuf *);