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);
198 static const struct got_error *
199 gitconfig_remotes_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
201 const struct got_error *err = NULL;
202 struct got_gitconfig_list *sections;
203 struct got_gitconfig_list_node *node;
204 struct got_remote_repo *remotes = NULL;
207 if (gitconfig == NULL)
208 return got_error(GOT_ERR_PRIVSEP_MSG);
210 err = got_gitconfig_get_section_list(§ions, gitconfig);
214 TAILQ_FOREACH(node, §ions->fields, link) {
215 if (strncasecmp("remote \"", node->field, 8) != 0)
221 err = send_gitconfig_remotes(ibuf, NULL, 0);
225 remotes = recallocarray(NULL, 0, nremotes, sizeof(*remotes));
226 if (remotes == NULL) {
227 err = got_error_from_errno("recallocarray");
232 TAILQ_FOREACH(node, §ions->fields, link) {
233 char *name, *end, *mirror;
235 if (strncasecmp("remote \"", node->field, 8) != 0)
238 name = strdup(node->field + 8);
240 err = got_error_from_errno("strdup");
243 end = strrchr(name, '"');
246 remotes[i].name = name;
248 remotes[i].fetch_url = got_gitconfig_get_str(gitconfig,
250 if (remotes[i].fetch_url == NULL) {
251 err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
255 remotes[i].send_url = got_gitconfig_get_str(gitconfig,
256 node->field, "pushurl");
257 if (remotes[i].send_url == NULL)
258 remotes[i].send_url = got_gitconfig_get_str(gitconfig,
260 if (remotes[i].send_url == NULL) {
261 err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
265 remotes[i].mirror_references = 0;
266 mirror = got_gitconfig_get_str(gitconfig, node->field,
268 if (mirror != NULL && get_boolean_val(mirror))
269 remotes[i].mirror_references = 1;
274 err = send_gitconfig_remotes(ibuf, remotes, nremotes);
276 for (i = 0; i < nremotes; i++)
277 free(remotes[i].name);
279 got_gitconfig_free_list(sections);
283 static const struct got_error *
284 gitconfig_owner_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
288 if (gitconfig == NULL)
289 return got_error(GOT_ERR_PRIVSEP_MSG);
291 value = got_gitconfig_get_str(gitconfig, "gotweb", "owner");
293 return send_gitconfig_str(ibuf, value);
294 value = got_gitconfig_get_str(gitconfig, "gitweb", "owner");
295 return send_gitconfig_str(ibuf, value);
298 static const struct got_error *
299 gitconfig_extensions_request(struct imsgbuf *ibuf,
300 struct got_gitconfig *gitconfig)
302 const struct got_error *err = NULL;
303 struct got_gitconfig_list *tags;
304 struct got_gitconfig_list_node *node;
308 if (gitconfig == NULL)
309 return got_error(GOT_ERR_PRIVSEP_MSG);
311 tags = got_gitconfig_get_tag_list(gitconfig, "extensions");
313 return send_gitconfig_int(ibuf, 0);
315 TAILQ_FOREACH(node, &tags->fields, link)
318 err = send_gitconfig_int(ibuf, nextensions);
322 TAILQ_FOREACH(node, &tags->fields, link) {
323 val = got_gitconfig_get_str(gitconfig, "extensions",
325 err = send_gitconfig_pair(ibuf, node->field, val);
330 got_gitconfig_free_list(tags);
335 main(int argc, char *argv[])
337 const struct got_error *err = NULL;
340 struct got_gitconfig *gitconfig = NULL;
347 signal(SIGINT, catch_sigint);
349 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
352 /* revoke access to most system calls */
353 if (pledge("stdio recvfd", NULL) == -1) {
354 err = got_error_from_errno("pledge");
355 got_privsep_send_error(&ibuf, err);
363 memset(&imsg, 0, sizeof(imsg));
366 if (sigint_received) {
367 err = got_error(GOT_ERR_CANCELLED);
371 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
373 if (err->code == GOT_ERR_PRIVSEP_PIPE)
378 if (imsg.hdr.type == GOT_IMSG_STOP)
381 switch (imsg.hdr.type) {
382 case GOT_IMSG_GITCONFIG_PARSE_REQUEST:
383 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
385 err = got_error(GOT_ERR_PRIVSEP_LEN);
389 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
394 got_gitconfig_close(gitconfig);
395 err = got_gitconfig_open(&gitconfig, imsg.fd);
397 case GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST:
398 err = gitconfig_num_request(&ibuf, gitconfig, "core",
399 "repositoryformatversion", 0);
401 case GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST:
402 err = gitconfig_extensions_request(&ibuf, gitconfig);
404 case GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST:
405 err = gitconfig_str_request(&ibuf, gitconfig, "user",
408 case GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST:
409 err = gitconfig_str_request(&ibuf, gitconfig, "user",
412 case GOT_IMSG_GITCONFIG_REMOTES_REQUEST:
413 err = gitconfig_remotes_request(&ibuf, gitconfig);
415 case GOT_IMSG_GITCONFIG_OWNER_REQUEST:
416 err = gitconfig_owner_request(&ibuf, gitconfig);
419 err = got_error(GOT_ERR_PRIVSEP_MSG);
424 if (close(imsg.fd) == -1 && err == NULL)
425 err = got_error_from_errno("close");
435 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
436 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
437 got_privsep_send_error(&ibuf, err);
440 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
441 err = got_error_from_errno("close");