Blame


1 aba9c984 2019-09-08 stsp /*
2 aba9c984 2019-09-08 stsp * Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
3 aba9c984 2019-09-08 stsp *
4 aba9c984 2019-09-08 stsp * Permission to use, copy, modify, and distribute this software for any
5 aba9c984 2019-09-08 stsp * purpose with or without fee is hereby granted, provided that the above
6 aba9c984 2019-09-08 stsp * copyright notice and this permission notice appear in all copies.
7 aba9c984 2019-09-08 stsp *
8 aba9c984 2019-09-08 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 aba9c984 2019-09-08 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 aba9c984 2019-09-08 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 aba9c984 2019-09-08 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 aba9c984 2019-09-08 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 aba9c984 2019-09-08 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 aba9c984 2019-09-08 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 aba9c984 2019-09-08 stsp */
16 aba9c984 2019-09-08 stsp
17 aba9c984 2019-09-08 stsp #include <sys/types.h>
18 aba9c984 2019-09-08 stsp #include <sys/uio.h>
19 aba9c984 2019-09-08 stsp #include <sys/time.h>
20 aba9c984 2019-09-08 stsp
21 aba9c984 2019-09-08 stsp #include <stdint.h>
22 aba9c984 2019-09-08 stsp #include <limits.h>
23 aba9c984 2019-09-08 stsp #include <signal.h>
24 aba9c984 2019-09-08 stsp #include <stdio.h>
25 aba9c984 2019-09-08 stsp #include <stdlib.h>
26 aba9c984 2019-09-08 stsp #include <string.h>
27 81a12da5 2020-09-09 naddy #include <unistd.h>
28 aba9c984 2019-09-08 stsp #include <zlib.h>
29 dd038bc6 2021-09-21 thomas.ad
30 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
31 aba9c984 2019-09-08 stsp
32 aba9c984 2019-09-08 stsp #include "got_error.h"
33 aba9c984 2019-09-08 stsp #include "got_object.h"
34 cd95becd 2019-11-29 stsp #include "got_repository.h"
35 aba9c984 2019-09-08 stsp
36 aba9c984 2019-09-08 stsp #include "got_lib_delta.h"
37 aba9c984 2019-09-08 stsp #include "got_lib_object.h"
38 aba9c984 2019-09-08 stsp #include "got_lib_privsep.h"
39 aba9c984 2019-09-08 stsp #include "got_lib_gitconfig.h"
40 aba9c984 2019-09-08 stsp
41 aba9c984 2019-09-08 stsp static volatile sig_atomic_t sigint_received;
42 aba9c984 2019-09-08 stsp
43 aba9c984 2019-09-08 stsp static void
44 aba9c984 2019-09-08 stsp catch_sigint(int signo)
45 aba9c984 2019-09-08 stsp {
46 aba9c984 2019-09-08 stsp sigint_received = 1;
47 aba9c984 2019-09-08 stsp }
48 aba9c984 2019-09-08 stsp
49 aba9c984 2019-09-08 stsp static const struct got_error *
50 e70bf110 2020-03-22 stsp send_gitconfig_int(struct imsgbuf *ibuf, int value)
51 e70bf110 2020-03-22 stsp {
52 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
53 e70bf110 2020-03-22 stsp &value, sizeof(value)) == -1)
54 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
55 e70bf110 2020-03-22 stsp
56 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
57 e70bf110 2020-03-22 stsp }
58 e70bf110 2020-03-22 stsp
59 e70bf110 2020-03-22 stsp static const struct got_error *
60 aba9c984 2019-09-08 stsp gitconfig_num_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
61 aba9c984 2019-09-08 stsp char *section, char *tag, int def)
62 aba9c984 2019-09-08 stsp {
63 aba9c984 2019-09-08 stsp int value;
64 aba9c984 2019-09-08 stsp
65 aba9c984 2019-09-08 stsp if (gitconfig == NULL)
66 aba9c984 2019-09-08 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
67 aba9c984 2019-09-08 stsp
68 aba9c984 2019-09-08 stsp value = got_gitconfig_get_num(gitconfig, section, tag, def);
69 e70bf110 2020-03-22 stsp return send_gitconfig_int(ibuf, value);
70 aba9c984 2019-09-08 stsp }
71 aba9c984 2019-09-08 stsp
72 aba9c984 2019-09-08 stsp static const struct got_error *
73 e70bf110 2020-03-22 stsp send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
74 e70bf110 2020-03-22 stsp {
75 6c13dcd2 2020-09-18 stsp size_t len = value ? strlen(value) : 0;
76 e70bf110 2020-03-22 stsp
77 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
78 e70bf110 2020-03-22 stsp value, len) == -1)
79 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
80 e70bf110 2020-03-22 stsp
81 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
82 e70bf110 2020-03-22 stsp }
83 e70bf110 2020-03-22 stsp
84 e70bf110 2020-03-22 stsp static const struct got_error *
85 aba9c984 2019-09-08 stsp gitconfig_str_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
86 aba9c984 2019-09-08 stsp char *section, char *tag)
87 aba9c984 2019-09-08 stsp {
88 aba9c984 2019-09-08 stsp char *value;
89 aba9c984 2019-09-08 stsp
90 aba9c984 2019-09-08 stsp if (gitconfig == NULL)
91 aba9c984 2019-09-08 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
92 aba9c984 2019-09-08 stsp
93 aba9c984 2019-09-08 stsp value = got_gitconfig_get_str(gitconfig, section, tag);
94 e70bf110 2020-03-22 stsp return send_gitconfig_str(ibuf, value);
95 aba9c984 2019-09-08 stsp }
96 aba9c984 2019-09-08 stsp
97 cd95becd 2019-11-29 stsp static const struct got_error *
98 e70bf110 2020-03-22 stsp send_gitconfig_remotes(struct imsgbuf *ibuf, struct got_remote_repo *remotes,
99 e70bf110 2020-03-22 stsp int nremotes)
100 e70bf110 2020-03-22 stsp {
101 e70bf110 2020-03-22 stsp const struct got_error *err = NULL;
102 e70bf110 2020-03-22 stsp struct got_imsg_remotes iremotes;
103 e70bf110 2020-03-22 stsp int i;
104 e70bf110 2020-03-22 stsp
105 e70bf110 2020-03-22 stsp iremotes.nremotes = nremotes;
106 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
107 e70bf110 2020-03-22 stsp &iremotes, sizeof(iremotes)) == -1)
108 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
109 e70bf110 2020-03-22 stsp
110 e70bf110 2020-03-22 stsp err = got_privsep_flush_imsg(ibuf);
111 e70bf110 2020-03-22 stsp imsg_clear(ibuf);
112 e70bf110 2020-03-22 stsp if (err)
113 e70bf110 2020-03-22 stsp return err;
114 e70bf110 2020-03-22 stsp
115 e70bf110 2020-03-22 stsp for (i = 0; i < nremotes; i++) {
116 e70bf110 2020-03-22 stsp struct got_imsg_remote iremote;
117 e70bf110 2020-03-22 stsp size_t len = sizeof(iremote);
118 e70bf110 2020-03-22 stsp struct ibuf *wbuf;
119 e70bf110 2020-03-22 stsp
120 e70bf110 2020-03-22 stsp iremote.mirror_references = remotes[i].mirror_references;
121 e70bf110 2020-03-22 stsp iremote.name_len = strlen(remotes[i].name);
122 e70bf110 2020-03-22 stsp len += iremote.name_len;
123 6480c871 2021-08-30 stsp iremote.fetch_url_len = strlen(remotes[i].fetch_url);
124 6480c871 2021-08-30 stsp len += iremote.fetch_url_len;
125 6480c871 2021-08-30 stsp iremote.send_url_len = strlen(remotes[i].send_url);
126 6480c871 2021-08-30 stsp len += iremote.send_url_len;
127 e70bf110 2020-03-22 stsp
128 e70bf110 2020-03-22 stsp wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
129 e70bf110 2020-03-22 stsp if (wbuf == NULL)
130 e70bf110 2020-03-22 stsp return got_error_from_errno(
131 e70bf110 2020-03-22 stsp "imsg_create GITCONFIG_REMOTE");
132 e70bf110 2020-03-22 stsp
133 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
134 e70bf110 2020-03-22 stsp err = got_error_from_errno(
135 e70bf110 2020-03-22 stsp "imsg_add GITCONFIG_REMOTE");
136 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
137 e70bf110 2020-03-22 stsp return err;
138 e70bf110 2020-03-22 stsp }
139 e70bf110 2020-03-22 stsp
140 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
141 e70bf110 2020-03-22 stsp err = got_error_from_errno(
142 e70bf110 2020-03-22 stsp "imsg_add GITCONFIG_REMOTE");
143 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
144 e70bf110 2020-03-22 stsp return err;
145 e70bf110 2020-03-22 stsp }
146 6480c871 2021-08-30 stsp if (imsg_add(wbuf, remotes[i].fetch_url, iremote.fetch_url_len) == -1) {
147 e70bf110 2020-03-22 stsp err = got_error_from_errno(
148 e70bf110 2020-03-22 stsp "imsg_add GITCONFIG_REMOTE");
149 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
150 e70bf110 2020-03-22 stsp return err;
151 e70bf110 2020-03-22 stsp }
152 6480c871 2021-08-30 stsp if (imsg_add(wbuf, remotes[i].send_url, iremote.send_url_len) == -1) {
153 6480c871 2021-08-30 stsp err = got_error_from_errno(
154 6480c871 2021-08-30 stsp "imsg_add GITCONFIG_REMOTE");
155 6480c871 2021-08-30 stsp ibuf_free(wbuf);
156 6480c871 2021-08-30 stsp return err;
157 6480c871 2021-08-30 stsp }
158 e70bf110 2020-03-22 stsp
159 e70bf110 2020-03-22 stsp wbuf->fd = -1;
160 e70bf110 2020-03-22 stsp imsg_close(ibuf, wbuf);
161 e70bf110 2020-03-22 stsp err = got_privsep_flush_imsg(ibuf);
162 e70bf110 2020-03-22 stsp if (err)
163 e70bf110 2020-03-22 stsp return err;
164 e70bf110 2020-03-22 stsp }
165 e70bf110 2020-03-22 stsp
166 e70bf110 2020-03-22 stsp return NULL;
167 e70bf110 2020-03-22 stsp }
168 e70bf110 2020-03-22 stsp
169 20b7abb3 2020-10-22 stsp static int
170 20b7abb3 2020-10-22 stsp get_boolean_val(char *val)
171 20b7abb3 2020-10-22 stsp {
172 20b7abb3 2020-10-22 stsp return (strcasecmp(val, "true") == 0 ||
173 20b7abb3 2020-10-22 stsp strcasecmp(val, "on") == 0 ||
174 20b7abb3 2020-10-22 stsp strcasecmp(val, "yes") == 0 ||
175 20b7abb3 2020-10-22 stsp strcmp(val, "1") == 0);
176 20b7abb3 2020-10-22 stsp }
177 e70bf110 2020-03-22 stsp
178 e70bf110 2020-03-22 stsp static const struct got_error *
179 cd95becd 2019-11-29 stsp gitconfig_remotes_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
180 cd95becd 2019-11-29 stsp {
181 cd95becd 2019-11-29 stsp const struct got_error *err = NULL;
182 cd95becd 2019-11-29 stsp struct got_gitconfig_list *sections;
183 cd95becd 2019-11-29 stsp struct got_gitconfig_list_node *node;
184 cd95becd 2019-11-29 stsp struct got_remote_repo *remotes = NULL;
185 cd95becd 2019-11-29 stsp int nremotes = 0, i;
186 cd95becd 2019-11-29 stsp
187 cd95becd 2019-11-29 stsp if (gitconfig == NULL)
188 cd95becd 2019-11-29 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
189 cd95becd 2019-11-29 stsp
190 cd95becd 2019-11-29 stsp err = got_gitconfig_get_section_list(&sections, gitconfig);
191 cd95becd 2019-11-29 stsp if (err)
192 cd95becd 2019-11-29 stsp return err;
193 cd95becd 2019-11-29 stsp
194 cd95becd 2019-11-29 stsp TAILQ_FOREACH(node, &sections->fields, link) {
195 cd95becd 2019-11-29 stsp if (strncasecmp("remote \"", node->field, 8) != 0)
196 cd95becd 2019-11-29 stsp continue;
197 cd95becd 2019-11-29 stsp nremotes++;
198 cd95becd 2019-11-29 stsp }
199 cd95becd 2019-11-29 stsp
200 cd95becd 2019-11-29 stsp if (nremotes == 0) {
201 e70bf110 2020-03-22 stsp err = send_gitconfig_remotes(ibuf, NULL, 0);
202 cd95becd 2019-11-29 stsp goto done;
203 cd95becd 2019-11-29 stsp }
204 cd95becd 2019-11-29 stsp
205 cd95becd 2019-11-29 stsp remotes = recallocarray(NULL, 0, nremotes, sizeof(*remotes));
206 cd95becd 2019-11-29 stsp if (remotes == NULL) {
207 cd95becd 2019-11-29 stsp err = got_error_from_errno("recallocarray");
208 cd95becd 2019-11-29 stsp goto done;
209 cd95becd 2019-11-29 stsp }
210 cd95becd 2019-11-29 stsp
211 cd95becd 2019-11-29 stsp i = 0;
212 cd95becd 2019-11-29 stsp TAILQ_FOREACH(node, &sections->fields, link) {
213 469dd726 2020-03-20 stsp char *name, *end, *mirror;
214 cd95becd 2019-11-29 stsp
215 cd95becd 2019-11-29 stsp if (strncasecmp("remote \"", node->field, 8) != 0)
216 cd95becd 2019-11-29 stsp continue;
217 cd95becd 2019-11-29 stsp
218 cd95becd 2019-11-29 stsp name = strdup(node->field + 8);
219 cd95becd 2019-11-29 stsp if (name == NULL) {
220 cd95becd 2019-11-29 stsp err = got_error_from_errno("strdup");
221 cd95becd 2019-11-29 stsp goto done;
222 cd95becd 2019-11-29 stsp }
223 cd95becd 2019-11-29 stsp end = strrchr(name, '"');
224 cd95becd 2019-11-29 stsp if (end)
225 cd95becd 2019-11-29 stsp *end = '\0';
226 cd95becd 2019-11-29 stsp remotes[i].name = name;
227 cd95becd 2019-11-29 stsp
228 6480c871 2021-08-30 stsp remotes[i].fetch_url = got_gitconfig_get_str(gitconfig,
229 cd95becd 2019-11-29 stsp node->field, "url");
230 6480c871 2021-08-30 stsp if (remotes[i].fetch_url == NULL) {
231 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
232 cd95becd 2019-11-29 stsp goto done;
233 cd95becd 2019-11-29 stsp }
234 469dd726 2020-03-20 stsp
235 6480c871 2021-08-30 stsp remotes[i].send_url = got_gitconfig_get_str(gitconfig,
236 6480c871 2021-08-30 stsp node->field, "pushurl");
237 6480c871 2021-08-30 stsp if (remotes[i].send_url == NULL)
238 6480c871 2021-08-30 stsp remotes[i].send_url = got_gitconfig_get_str(gitconfig,
239 6480c871 2021-08-30 stsp node->field, "url");
240 6480c871 2021-08-30 stsp if (remotes[i].send_url == NULL) {
241 6480c871 2021-08-30 stsp err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
242 6480c871 2021-08-30 stsp goto done;
243 6480c871 2021-08-30 stsp }
244 6480c871 2021-08-30 stsp
245 469dd726 2020-03-20 stsp remotes[i].mirror_references = 0;
246 469dd726 2020-03-20 stsp mirror = got_gitconfig_get_str(gitconfig, node->field,
247 469dd726 2020-03-20 stsp "mirror");
248 20b7abb3 2020-10-22 stsp if (mirror != NULL && get_boolean_val(mirror))
249 469dd726 2020-03-20 stsp remotes[i].mirror_references = 1;
250 cd95becd 2019-11-29 stsp
251 cd95becd 2019-11-29 stsp i++;
252 cd95becd 2019-11-29 stsp }
253 cd95becd 2019-11-29 stsp
254 e70bf110 2020-03-22 stsp err = send_gitconfig_remotes(ibuf, remotes, nremotes);
255 cd95becd 2019-11-29 stsp done:
256 cd95becd 2019-11-29 stsp for (i = 0; i < nremotes; i++)
257 cd95becd 2019-11-29 stsp free(remotes[i].name);
258 cd95becd 2019-11-29 stsp free(remotes);
259 cd95becd 2019-11-29 stsp got_gitconfig_free_list(sections);
260 cd95becd 2019-11-29 stsp return err;
261 9a1cc63f 2020-02-03 stsp }
262 9a1cc63f 2020-02-03 stsp
263 9a1cc63f 2020-02-03 stsp static const struct got_error *
264 9a1cc63f 2020-02-03 stsp gitconfig_owner_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
265 9a1cc63f 2020-02-03 stsp {
266 9a1cc63f 2020-02-03 stsp char *value;
267 9a1cc63f 2020-02-03 stsp
268 9a1cc63f 2020-02-03 stsp if (gitconfig == NULL)
269 9a1cc63f 2020-02-03 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
270 9a1cc63f 2020-02-03 stsp
271 9a1cc63f 2020-02-03 stsp value = got_gitconfig_get_str(gitconfig, "gotweb", "owner");
272 9a1cc63f 2020-02-03 stsp if (value)
273 e70bf110 2020-03-22 stsp return send_gitconfig_str(ibuf, value);
274 9a1cc63f 2020-02-03 stsp value = got_gitconfig_get_str(gitconfig, "gitweb", "owner");
275 e70bf110 2020-03-22 stsp return send_gitconfig_str(ibuf, value);
276 cd95becd 2019-11-29 stsp }
277 cd95becd 2019-11-29 stsp
278 20b7abb3 2020-10-22 stsp static const struct got_error *
279 20b7abb3 2020-10-22 stsp gitconfig_extensions_request(struct imsgbuf *ibuf,
280 20b7abb3 2020-10-22 stsp struct got_gitconfig *gitconfig)
281 20b7abb3 2020-10-22 stsp {
282 20b7abb3 2020-10-22 stsp const struct got_error *err = NULL;
283 20b7abb3 2020-10-22 stsp struct got_gitconfig_list *tags;
284 20b7abb3 2020-10-22 stsp struct got_gitconfig_list_node *node;
285 20b7abb3 2020-10-22 stsp int nextensions = 0;
286 20b7abb3 2020-10-22 stsp char *val;
287 20b7abb3 2020-10-22 stsp
288 20b7abb3 2020-10-22 stsp if (gitconfig == NULL)
289 20b7abb3 2020-10-22 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
290 20b7abb3 2020-10-22 stsp
291 20b7abb3 2020-10-22 stsp tags = got_gitconfig_get_tag_list(gitconfig, "extensions");
292 20b7abb3 2020-10-22 stsp if (tags == NULL)
293 20b7abb3 2020-10-22 stsp return send_gitconfig_int(ibuf, 0);
294 20b7abb3 2020-10-22 stsp
295 20b7abb3 2020-10-22 stsp TAILQ_FOREACH(node, &tags->fields, link) {
296 20b7abb3 2020-10-22 stsp val = got_gitconfig_get_str(gitconfig, "extensions",
297 20b7abb3 2020-10-22 stsp node->field);
298 20b7abb3 2020-10-22 stsp if (get_boolean_val(val))
299 20b7abb3 2020-10-22 stsp nextensions++;
300 20b7abb3 2020-10-22 stsp }
301 20b7abb3 2020-10-22 stsp
302 20b7abb3 2020-10-22 stsp err = send_gitconfig_int(ibuf, nextensions);
303 20b7abb3 2020-10-22 stsp if (err)
304 20b7abb3 2020-10-22 stsp goto done;
305 20b7abb3 2020-10-22 stsp
306 20b7abb3 2020-10-22 stsp TAILQ_FOREACH(node, &tags->fields, link) {
307 20b7abb3 2020-10-22 stsp val = got_gitconfig_get_str(gitconfig, "extensions",
308 20b7abb3 2020-10-22 stsp node->field);
309 20b7abb3 2020-10-22 stsp if (get_boolean_val(val)) {
310 20b7abb3 2020-10-22 stsp err = send_gitconfig_str(ibuf, node->field);
311 20b7abb3 2020-10-22 stsp if (err)
312 20b7abb3 2020-10-22 stsp goto done;
313 20b7abb3 2020-10-22 stsp }
314 20b7abb3 2020-10-22 stsp }
315 20b7abb3 2020-10-22 stsp done:
316 20b7abb3 2020-10-22 stsp got_gitconfig_free_list(tags);
317 20b7abb3 2020-10-22 stsp return err;
318 20b7abb3 2020-10-22 stsp }
319 20b7abb3 2020-10-22 stsp
320 aba9c984 2019-09-08 stsp int
321 aba9c984 2019-09-08 stsp main(int argc, char *argv[])
322 aba9c984 2019-09-08 stsp {
323 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
324 aba9c984 2019-09-08 stsp struct imsgbuf ibuf;
325 aba9c984 2019-09-08 stsp size_t datalen;
326 aba9c984 2019-09-08 stsp struct got_gitconfig *gitconfig = NULL;
327 aba9c984 2019-09-08 stsp #if 0
328 aba9c984 2019-09-08 stsp static int attached;
329 aba9c984 2019-09-08 stsp
330 aba9c984 2019-09-08 stsp while (!attached)
331 aba9c984 2019-09-08 stsp sleep(1);
332 aba9c984 2019-09-08 stsp #endif
333 aba9c984 2019-09-08 stsp signal(SIGINT, catch_sigint);
334 aba9c984 2019-09-08 stsp
335 aba9c984 2019-09-08 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
336 aba9c984 2019-09-08 stsp
337 aba9c984 2019-09-08 stsp #ifndef PROFILE
338 aba9c984 2019-09-08 stsp /* revoke access to most system calls */
339 aba9c984 2019-09-08 stsp if (pledge("stdio recvfd", NULL) == -1) {
340 aba9c984 2019-09-08 stsp err = got_error_from_errno("pledge");
341 2c6298d5 2021-09-24 thomas got_privsep_send_error(&ibuf, err);
342 2c6298d5 2021-09-24 thomas return 1;
343 2c6298d5 2021-09-24 thomas }
344 2c6298d5 2021-09-24 thomas #endif
345 2c6298d5 2021-09-24 thomas #ifdef HAVE_LINUX_LANDLOCK_H
346 2c6298d5 2021-09-24 thomas /* revoke fs access */
347 2c6298d5 2021-09-24 thomas if (landlock_no_fs() == -1) {
348 2c6298d5 2021-09-24 thomas err = got_error_from_errno("landlock_no_fs");
349 aba9c984 2019-09-08 stsp got_privsep_send_error(&ibuf, err);
350 aba9c984 2019-09-08 stsp return 1;
351 aba9c984 2019-09-08 stsp }
352 aba9c984 2019-09-08 stsp #endif
353 aba9c984 2019-09-08 stsp
354 aba9c984 2019-09-08 stsp for (;;) {
355 aba9c984 2019-09-08 stsp struct imsg imsg;
356 aba9c984 2019-09-08 stsp
357 aba9c984 2019-09-08 stsp memset(&imsg, 0, sizeof(imsg));
358 aba9c984 2019-09-08 stsp imsg.fd = -1;
359 aba9c984 2019-09-08 stsp
360 aba9c984 2019-09-08 stsp if (sigint_received) {
361 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_CANCELLED);
362 aba9c984 2019-09-08 stsp break;
363 aba9c984 2019-09-08 stsp }
364 aba9c984 2019-09-08 stsp
365 aba9c984 2019-09-08 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
366 aba9c984 2019-09-08 stsp if (err) {
367 aba9c984 2019-09-08 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
368 aba9c984 2019-09-08 stsp err = NULL;
369 aba9c984 2019-09-08 stsp break;
370 aba9c984 2019-09-08 stsp }
371 aba9c984 2019-09-08 stsp
372 aba9c984 2019-09-08 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
373 aba9c984 2019-09-08 stsp break;
374 aba9c984 2019-09-08 stsp
375 aba9c984 2019-09-08 stsp switch (imsg.hdr.type) {
376 aba9c984 2019-09-08 stsp case GOT_IMSG_GITCONFIG_PARSE_REQUEST:
377 aba9c984 2019-09-08 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
378 aba9c984 2019-09-08 stsp if (datalen != 0) {
379 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
380 aba9c984 2019-09-08 stsp break;
381 aba9c984 2019-09-08 stsp }
382 aba9c984 2019-09-08 stsp if (imsg.fd == -1){
383 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
384 aba9c984 2019-09-08 stsp break;
385 aba9c984 2019-09-08 stsp }
386 aba9c984 2019-09-08 stsp
387 aba9c984 2019-09-08 stsp if (gitconfig)
388 aba9c984 2019-09-08 stsp got_gitconfig_close(gitconfig);
389 aba9c984 2019-09-08 stsp err = got_gitconfig_open(&gitconfig, imsg.fd);
390 aba9c984 2019-09-08 stsp break;
391 aba9c984 2019-09-08 stsp case GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST:
392 aba9c984 2019-09-08 stsp err = gitconfig_num_request(&ibuf, gitconfig, "core",
393 aba9c984 2019-09-08 stsp "repositoryformatversion", 0);
394 aba9c984 2019-09-08 stsp break;
395 20b7abb3 2020-10-22 stsp case GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST:
396 20b7abb3 2020-10-22 stsp err = gitconfig_extensions_request(&ibuf, gitconfig);
397 20b7abb3 2020-10-22 stsp break;
398 aba9c984 2019-09-08 stsp case GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST:
399 aba9c984 2019-09-08 stsp err = gitconfig_str_request(&ibuf, gitconfig, "user",
400 aba9c984 2019-09-08 stsp "name");
401 aba9c984 2019-09-08 stsp break;
402 aba9c984 2019-09-08 stsp case GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST:
403 aba9c984 2019-09-08 stsp err = gitconfig_str_request(&ibuf, gitconfig, "user",
404 aba9c984 2019-09-08 stsp "email");
405 aba9c984 2019-09-08 stsp break;
406 cd95becd 2019-11-29 stsp case GOT_IMSG_GITCONFIG_REMOTES_REQUEST:
407 cd95becd 2019-11-29 stsp err = gitconfig_remotes_request(&ibuf, gitconfig);
408 cd95becd 2019-11-29 stsp break;
409 9a1cc63f 2020-02-03 stsp case GOT_IMSG_GITCONFIG_OWNER_REQUEST:
410 9a1cc63f 2020-02-03 stsp err = gitconfig_owner_request(&ibuf, gitconfig);
411 9a1cc63f 2020-02-03 stsp break;
412 aba9c984 2019-09-08 stsp default:
413 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
414 aba9c984 2019-09-08 stsp break;
415 aba9c984 2019-09-08 stsp }
416 aba9c984 2019-09-08 stsp
417 aba9c984 2019-09-08 stsp if (imsg.fd != -1) {
418 aba9c984 2019-09-08 stsp if (close(imsg.fd) == -1 && err == NULL)
419 aba9c984 2019-09-08 stsp err = got_error_from_errno("close");
420 aba9c984 2019-09-08 stsp }
421 aba9c984 2019-09-08 stsp
422 aba9c984 2019-09-08 stsp imsg_free(&imsg);
423 aba9c984 2019-09-08 stsp if (err)
424 aba9c984 2019-09-08 stsp break;
425 aba9c984 2019-09-08 stsp }
426 aba9c984 2019-09-08 stsp
427 aba9c984 2019-09-08 stsp imsg_clear(&ibuf);
428 aba9c984 2019-09-08 stsp if (err) {
429 aba9c984 2019-09-08 stsp if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
430 aba9c984 2019-09-08 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
431 aba9c984 2019-09-08 stsp got_privsep_send_error(&ibuf, err);
432 aba9c984 2019-09-08 stsp }
433 aba9c984 2019-09-08 stsp }
434 08578a35 2021-01-22 stsp if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
435 aba9c984 2019-09-08 stsp err = got_error_from_errno("close");
436 aba9c984 2019-09-08 stsp return err ? 1 : 0;
437 aba9c984 2019-09-08 stsp }