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