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