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