2 * Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
18 #include <sys/queue.h>
34 #include "got_error.h"
35 #include "got_object.h"
36 #include "got_repository.h"
38 #include "got_lib_delta.h"
39 #include "got_lib_object.h"
40 #include "got_lib_privsep.h"
41 #include "got_lib_gitconfig.h"
43 static volatile sig_atomic_t sigint_received;
46 catch_sigint(int signo)
51 static const struct got_error *
52 send_gitconfig_int(struct imsgbuf *ibuf, int value)
54 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
55 &value, sizeof(value)) == -1)
56 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
58 return got_privsep_flush_imsg(ibuf);
61 static const struct got_error *
62 gitconfig_num_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
63 const char *section, const char *tag, int def)
67 if (gitconfig == NULL)
68 return got_error(GOT_ERR_PRIVSEP_MSG);
70 value = got_gitconfig_get_num(gitconfig, section, tag, def);
71 return send_gitconfig_int(ibuf, value);
74 static const struct got_error *
75 send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
77 size_t len = value ? strlen(value) : 0;
79 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
81 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
83 return got_privsep_flush_imsg(ibuf);
86 static const struct got_error *
87 send_gitconfig_pair(struct imsgbuf *ibuf, const char *key, const char *val)
90 size_t klen = key ? strlen(key) : 0;
91 size_t vlen = val ? strlen(val) : 0;
92 size_t tot = sizeof(klen) + sizeof(vlen) + klen + vlen;
94 if (tot > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
95 return got_error(GOT_ERR_NO_SPACE);
97 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_PAIR, 0, 0, tot);
99 return got_error_from_errno("imsg_create GITCONFIG_PAIR");
101 /* Keep in sync with got_imsg_gitconfig_pair */
102 if (imsg_add(wbuf, &klen, sizeof(klen)) == -1)
103 return got_error_from_errno("imsg_add GITCONFIG_PAIR");
104 if (imsg_add(wbuf, &vlen, sizeof(vlen)) == -1)
105 return got_error_from_errno("imsg_add GITCONFIG_PAIR");
106 if (imsg_add(wbuf, key, klen) == -1)
107 return got_error_from_errno("imsg_add GITCONFIG_PAIR");
108 if (imsg_add(wbuf, val, vlen) == -1)
109 return got_error_from_errno("imsg_add GITCONFIG_PAIR");
112 imsg_close(ibuf, wbuf);
113 return got_privsep_flush_imsg(ibuf);
116 static const struct got_error *
117 gitconfig_str_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
118 const char *section, const char *tag)
122 if (gitconfig == NULL)
123 return got_error(GOT_ERR_PRIVSEP_MSG);
125 value = got_gitconfig_get_str(gitconfig, section, tag);
126 return send_gitconfig_str(ibuf, value);
129 static const struct got_error *
130 send_gitconfig_remotes(struct imsgbuf *ibuf, struct got_remote_repo *remotes,
133 const struct got_error *err = NULL;
134 struct got_imsg_remotes iremotes;
137 iremotes.nremotes = nremotes;
138 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
139 &iremotes, sizeof(iremotes)) == -1)
140 return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
142 err = got_privsep_flush_imsg(ibuf);
147 for (i = 0; i < nremotes; i++) {
148 struct got_imsg_remote iremote;
149 size_t len = sizeof(iremote);
152 iremote.mirror_references = remotes[i].mirror_references;
153 iremote.name_len = strlen(remotes[i].name);
154 len += iremote.name_len;
155 iremote.fetch_url_len = strlen(remotes[i].fetch_url);
156 len += iremote.fetch_url_len;
157 iremote.send_url_len = strlen(remotes[i].send_url);
158 len += iremote.send_url_len;
160 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
162 return got_error_from_errno(
163 "imsg_create GITCONFIG_REMOTE");
165 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1)
166 return got_error_from_errno(
167 "imsg_add GITCONFIG_REMOTE");
169 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1)
170 return got_error_from_errno(
171 "imsg_add GITCONFIG_REMOTE");
172 if (imsg_add(wbuf, remotes[i].fetch_url, iremote.fetch_url_len) == -1)
173 return got_error_from_errno(
174 "imsg_add GITCONFIG_REMOTE");
175 if (imsg_add(wbuf, remotes[i].send_url, iremote.send_url_len) == -1)
176 return got_error_from_errno(
177 "imsg_add GITCONFIG_REMOTE");
180 imsg_close(ibuf, wbuf);
181 err = got_privsep_flush_imsg(ibuf);
190 get_boolean_val(char *val)
192 return (strcasecmp(val, "true") == 0 ||
193 strcasecmp(val, "on") == 0 ||
194 strcasecmp(val, "yes") == 0 ||
195 strcmp(val, "1") == 0);
199 skip_node(struct got_gitconfig *gitconfig, struct got_gitconfig_list_node *node)
202 * Skip config nodes which do not describe remotes, and remotes
203 * which do not have a fetch URL defined (as used by git-annex).
205 return (strncasecmp("remote \"", node->field, 8) != 0 ||
206 got_gitconfig_get_str(gitconfig, node->field, "url") == NULL);
209 static const struct got_error *
210 gitconfig_remotes_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
212 const struct got_error *err = NULL;
213 struct got_gitconfig_list *sections;
214 struct got_gitconfig_list_node *node;
215 struct got_remote_repo *remotes = NULL;
218 if (gitconfig == NULL)
219 return got_error(GOT_ERR_PRIVSEP_MSG);
221 err = got_gitconfig_get_section_list(§ions, gitconfig);
225 TAILQ_FOREACH(node, §ions->fields, link) {
226 if (skip_node(gitconfig, node))
232 err = send_gitconfig_remotes(ibuf, NULL, 0);
236 remotes = recallocarray(NULL, 0, nremotes, sizeof(*remotes));
237 if (remotes == NULL) {
238 err = got_error_from_errno("recallocarray");
243 TAILQ_FOREACH(node, §ions->fields, link) {
244 char *name, *end, *mirror;
246 if (skip_node(gitconfig, node))
249 name = strdup(node->field + 8);
251 err = got_error_from_errno("strdup");
254 end = strrchr(name, '"');
257 remotes[i].name = name;
259 remotes[i].fetch_url = got_gitconfig_get_str(gitconfig,
262 remotes[i].send_url = got_gitconfig_get_str(gitconfig,
263 node->field, "pushurl");
264 if (remotes[i].send_url == NULL)
265 remotes[i].send_url = remotes[i].fetch_url;
267 remotes[i].mirror_references = 0;
268 mirror = got_gitconfig_get_str(gitconfig, node->field,
270 if (mirror != NULL && get_boolean_val(mirror))
271 remotes[i].mirror_references = 1;
276 err = send_gitconfig_remotes(ibuf, remotes, nremotes);
278 for (i = 0; i < nremotes; i++)
279 free(remotes[i].name);
281 got_gitconfig_free_list(sections);
285 static const struct got_error *
286 gitconfig_owner_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
290 if (gitconfig == NULL)
291 return got_error(GOT_ERR_PRIVSEP_MSG);
293 value = got_gitconfig_get_str(gitconfig, "gotweb", "owner");
295 return send_gitconfig_str(ibuf, value);
296 value = got_gitconfig_get_str(gitconfig, "gitweb", "owner");
297 return send_gitconfig_str(ibuf, value);
300 static const struct got_error *
301 gitconfig_extensions_request(struct imsgbuf *ibuf,
302 struct got_gitconfig *gitconfig)
304 const struct got_error *err = NULL;
305 struct got_gitconfig_list *tags;
306 struct got_gitconfig_list_node *node;
310 if (gitconfig == NULL)
311 return got_error(GOT_ERR_PRIVSEP_MSG);
313 tags = got_gitconfig_get_tag_list(gitconfig, "extensions");
315 return send_gitconfig_int(ibuf, 0);
317 TAILQ_FOREACH(node, &tags->fields, link)
320 err = send_gitconfig_int(ibuf, nextensions);
324 TAILQ_FOREACH(node, &tags->fields, link) {
325 val = got_gitconfig_get_str(gitconfig, "extensions",
327 err = send_gitconfig_pair(ibuf, node->field, val);
332 got_gitconfig_free_list(tags);
337 main(int argc, char *argv[])
339 const struct got_error *err = NULL;
342 struct got_gitconfig *gitconfig = NULL;
349 signal(SIGINT, catch_sigint);
351 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
354 /* revoke access to most system calls */
355 if (pledge("stdio recvfd", NULL) == -1) {
356 err = got_error_from_errno("pledge");
357 got_privsep_send_error(&ibuf, err);
365 memset(&imsg, 0, sizeof(imsg));
368 if (sigint_received) {
369 err = got_error(GOT_ERR_CANCELLED);
373 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
375 if (err->code == GOT_ERR_PRIVSEP_PIPE)
380 if (imsg.hdr.type == GOT_IMSG_STOP)
383 switch (imsg.hdr.type) {
384 case GOT_IMSG_GITCONFIG_PARSE_REQUEST:
385 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
387 err = got_error(GOT_ERR_PRIVSEP_LEN);
391 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
396 got_gitconfig_close(gitconfig);
397 err = got_gitconfig_open(&gitconfig, imsg.fd);
399 case GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST:
400 err = gitconfig_num_request(&ibuf, gitconfig, "core",
401 "repositoryformatversion", 0);
403 case GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST:
404 err = gitconfig_extensions_request(&ibuf, gitconfig);
406 case GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST:
407 err = gitconfig_str_request(&ibuf, gitconfig, "user",
410 case GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST:
411 err = gitconfig_str_request(&ibuf, gitconfig, "user",
414 case GOT_IMSG_GITCONFIG_REMOTES_REQUEST:
415 err = gitconfig_remotes_request(&ibuf, gitconfig);
417 case GOT_IMSG_GITCONFIG_OWNER_REQUEST:
418 err = gitconfig_owner_request(&ibuf, gitconfig);
421 err = got_error(GOT_ERR_PRIVSEP_MSG);
426 if (close(imsg.fd) == -1 && err == NULL)
427 err = got_error_from_errno("close");
437 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
438 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
439 got_privsep_send_error(&ibuf, err);
442 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
443 err = got_error_from_errno("close");