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>
33 #include "got_error.h"
34 #include "got_object.h"
35 #include "got_repository.h"
37 #include "got_lib_delta.h"
38 #include "got_lib_object.h"
39 #include "got_lib_privsep.h"
40 #include "got_lib_gitconfig.h"
42 static volatile sig_atomic_t sigint_received;
45 catch_sigint(int signo)
50 static const struct got_error *
51 send_gitconfig_int(struct imsgbuf *ibuf, int value)
53 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
54 &value, sizeof(value)) == -1)
55 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
57 return got_privsep_flush_imsg(ibuf);
60 static const struct got_error *
61 gitconfig_num_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
62 const char *section, const char *tag, int def)
66 if (gitconfig == NULL)
67 return got_error(GOT_ERR_PRIVSEP_MSG);
69 value = got_gitconfig_get_num(gitconfig, section, tag, def);
70 return send_gitconfig_int(ibuf, value);
73 static const struct got_error *
74 send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
76 size_t len = value ? strlen(value) : 0;
78 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
80 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
82 return got_privsep_flush_imsg(ibuf);
85 static const struct got_error *
86 gitconfig_str_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
87 const char *section, const char *tag)
91 if (gitconfig == NULL)
92 return got_error(GOT_ERR_PRIVSEP_MSG);
94 value = got_gitconfig_get_str(gitconfig, section, tag);
95 return send_gitconfig_str(ibuf, value);
98 static const struct got_error *
99 send_gitconfig_remotes(struct imsgbuf *ibuf, struct got_remote_repo *remotes,
102 const struct got_error *err = NULL;
103 struct got_imsg_remotes iremotes;
106 iremotes.nremotes = nremotes;
107 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
108 &iremotes, sizeof(iremotes)) == -1)
109 return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
111 err = got_privsep_flush_imsg(ibuf);
116 for (i = 0; i < nremotes; i++) {
117 struct got_imsg_remote iremote;
118 size_t len = sizeof(iremote);
121 iremote.mirror_references = remotes[i].mirror_references;
122 iremote.name_len = strlen(remotes[i].name);
123 len += iremote.name_len;
124 iremote.fetch_url_len = strlen(remotes[i].fetch_url);
125 len += iremote.fetch_url_len;
126 iremote.send_url_len = strlen(remotes[i].send_url);
127 len += iremote.send_url_len;
129 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
131 return got_error_from_errno(
132 "imsg_create GITCONFIG_REMOTE");
134 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1) {
135 err = got_error_from_errno(
136 "imsg_add GITCONFIG_REMOTE");
141 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1) {
142 err = got_error_from_errno(
143 "imsg_add GITCONFIG_REMOTE");
147 if (imsg_add(wbuf, remotes[i].fetch_url, iremote.fetch_url_len) == -1) {
148 err = got_error_from_errno(
149 "imsg_add GITCONFIG_REMOTE");
153 if (imsg_add(wbuf, remotes[i].send_url, iremote.send_url_len) == -1) {
154 err = got_error_from_errno(
155 "imsg_add GITCONFIG_REMOTE");
161 imsg_close(ibuf, wbuf);
162 err = got_privsep_flush_imsg(ibuf);
171 get_boolean_val(char *val)
173 return (strcasecmp(val, "true") == 0 ||
174 strcasecmp(val, "on") == 0 ||
175 strcasecmp(val, "yes") == 0 ||
176 strcmp(val, "1") == 0);
179 static const struct got_error *
180 gitconfig_remotes_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
182 const struct got_error *err = NULL;
183 struct got_gitconfig_list *sections;
184 struct got_gitconfig_list_node *node;
185 struct got_remote_repo *remotes = NULL;
188 if (gitconfig == NULL)
189 return got_error(GOT_ERR_PRIVSEP_MSG);
191 err = got_gitconfig_get_section_list(§ions, gitconfig);
195 TAILQ_FOREACH(node, §ions->fields, link) {
196 if (strncasecmp("remote \"", node->field, 8) != 0)
202 err = send_gitconfig_remotes(ibuf, NULL, 0);
206 remotes = recallocarray(NULL, 0, nremotes, sizeof(*remotes));
207 if (remotes == NULL) {
208 err = got_error_from_errno("recallocarray");
213 TAILQ_FOREACH(node, §ions->fields, link) {
214 char *name, *end, *mirror;
216 if (strncasecmp("remote \"", node->field, 8) != 0)
219 name = strdup(node->field + 8);
221 err = got_error_from_errno("strdup");
224 end = strrchr(name, '"');
227 remotes[i].name = name;
229 remotes[i].fetch_url = got_gitconfig_get_str(gitconfig,
231 if (remotes[i].fetch_url == NULL) {
232 err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
236 remotes[i].send_url = got_gitconfig_get_str(gitconfig,
237 node->field, "pushurl");
238 if (remotes[i].send_url == NULL)
239 remotes[i].send_url = got_gitconfig_get_str(gitconfig,
241 if (remotes[i].send_url == NULL) {
242 err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
246 remotes[i].mirror_references = 0;
247 mirror = got_gitconfig_get_str(gitconfig, node->field,
249 if (mirror != NULL && get_boolean_val(mirror))
250 remotes[i].mirror_references = 1;
255 err = send_gitconfig_remotes(ibuf, remotes, nremotes);
257 for (i = 0; i < nremotes; i++)
258 free(remotes[i].name);
260 got_gitconfig_free_list(sections);
264 static const struct got_error *
265 gitconfig_owner_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
269 if (gitconfig == NULL)
270 return got_error(GOT_ERR_PRIVSEP_MSG);
272 value = got_gitconfig_get_str(gitconfig, "gotweb", "owner");
274 return send_gitconfig_str(ibuf, value);
275 value = got_gitconfig_get_str(gitconfig, "gitweb", "owner");
276 return send_gitconfig_str(ibuf, value);
279 static const struct got_error *
280 gitconfig_extensions_request(struct imsgbuf *ibuf,
281 struct got_gitconfig *gitconfig)
283 const struct got_error *err = NULL;
284 struct got_gitconfig_list *tags;
285 struct got_gitconfig_list_node *node;
289 if (gitconfig == NULL)
290 return got_error(GOT_ERR_PRIVSEP_MSG);
292 tags = got_gitconfig_get_tag_list(gitconfig, "extensions");
294 return send_gitconfig_int(ibuf, 0);
296 TAILQ_FOREACH(node, &tags->fields, link) {
297 val = got_gitconfig_get_str(gitconfig, "extensions",
299 if (get_boolean_val(val))
303 err = send_gitconfig_int(ibuf, nextensions);
307 TAILQ_FOREACH(node, &tags->fields, link) {
308 val = got_gitconfig_get_str(gitconfig, "extensions",
310 if (get_boolean_val(val)) {
311 err = send_gitconfig_str(ibuf, node->field);
317 got_gitconfig_free_list(tags);
322 main(int argc, char *argv[])
324 const struct got_error *err = NULL;
327 struct got_gitconfig *gitconfig = NULL;
334 signal(SIGINT, catch_sigint);
336 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
339 /* revoke access to most system calls */
340 if (pledge("stdio recvfd", NULL) == -1) {
341 err = got_error_from_errno("pledge");
342 got_privsep_send_error(&ibuf, err);
350 memset(&imsg, 0, sizeof(imsg));
353 if (sigint_received) {
354 err = got_error(GOT_ERR_CANCELLED);
358 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
360 if (err->code == GOT_ERR_PRIVSEP_PIPE)
365 if (imsg.hdr.type == GOT_IMSG_STOP)
368 switch (imsg.hdr.type) {
369 case GOT_IMSG_GITCONFIG_PARSE_REQUEST:
370 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
372 err = got_error(GOT_ERR_PRIVSEP_LEN);
376 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
381 got_gitconfig_close(gitconfig);
382 err = got_gitconfig_open(&gitconfig, imsg.fd);
384 case GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST:
385 err = gitconfig_num_request(&ibuf, gitconfig, "core",
386 "repositoryformatversion", 0);
388 case GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST:
389 err = gitconfig_extensions_request(&ibuf, gitconfig);
391 case GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST:
392 err = gitconfig_str_request(&ibuf, gitconfig, "user",
395 case GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST:
396 err = gitconfig_str_request(&ibuf, gitconfig, "user",
399 case GOT_IMSG_GITCONFIG_REMOTES_REQUEST:
400 err = gitconfig_remotes_request(&ibuf, gitconfig);
402 case GOT_IMSG_GITCONFIG_OWNER_REQUEST:
403 err = gitconfig_owner_request(&ibuf, gitconfig);
406 err = got_error(GOT_ERR_PRIVSEP_MSG);
411 if (close(imsg.fd) == -1 && err == NULL)
412 err = got_error_from_errno("close");
422 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
423 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
424 got_privsep_send_error(&ibuf, err);
427 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
428 err = got_error_from_errno("close");