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 return got_error_from_errno(
136 "imsg_add GITCONFIG_REMOTE");
138 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1)
139 return got_error_from_errno(
140 "imsg_add GITCONFIG_REMOTE");
141 if (imsg_add(wbuf, remotes[i].fetch_url, iremote.fetch_url_len) == -1)
142 return got_error_from_errno(
143 "imsg_add GITCONFIG_REMOTE");
144 if (imsg_add(wbuf, remotes[i].send_url, iremote.send_url_len) == -1)
145 return got_error_from_errno(
146 "imsg_add GITCONFIG_REMOTE");
149 imsg_close(ibuf, wbuf);
150 err = got_privsep_flush_imsg(ibuf);
159 get_boolean_val(char *val)
161 return (strcasecmp(val, "true") == 0 ||
162 strcasecmp(val, "on") == 0 ||
163 strcasecmp(val, "yes") == 0 ||
164 strcmp(val, "1") == 0);
167 static const struct got_error *
168 gitconfig_remotes_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
170 const struct got_error *err = NULL;
171 struct got_gitconfig_list *sections;
172 struct got_gitconfig_list_node *node;
173 struct got_remote_repo *remotes = NULL;
176 if (gitconfig == NULL)
177 return got_error(GOT_ERR_PRIVSEP_MSG);
179 err = got_gitconfig_get_section_list(§ions, gitconfig);
183 TAILQ_FOREACH(node, §ions->fields, link) {
184 if (strncasecmp("remote \"", node->field, 8) != 0)
190 err = send_gitconfig_remotes(ibuf, NULL, 0);
194 remotes = recallocarray(NULL, 0, nremotes, sizeof(*remotes));
195 if (remotes == NULL) {
196 err = got_error_from_errno("recallocarray");
201 TAILQ_FOREACH(node, §ions->fields, link) {
202 char *name, *end, *mirror;
204 if (strncasecmp("remote \"", node->field, 8) != 0)
207 name = strdup(node->field + 8);
209 err = got_error_from_errno("strdup");
212 end = strrchr(name, '"');
215 remotes[i].name = name;
217 remotes[i].fetch_url = got_gitconfig_get_str(gitconfig,
219 if (remotes[i].fetch_url == NULL) {
220 err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
224 remotes[i].send_url = got_gitconfig_get_str(gitconfig,
225 node->field, "pushurl");
226 if (remotes[i].send_url == NULL)
227 remotes[i].send_url = got_gitconfig_get_str(gitconfig,
229 if (remotes[i].send_url == NULL) {
230 err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
234 remotes[i].mirror_references = 0;
235 mirror = got_gitconfig_get_str(gitconfig, node->field,
237 if (mirror != NULL && get_boolean_val(mirror))
238 remotes[i].mirror_references = 1;
243 err = send_gitconfig_remotes(ibuf, remotes, nremotes);
245 for (i = 0; i < nremotes; i++)
246 free(remotes[i].name);
248 got_gitconfig_free_list(sections);
252 static const struct got_error *
253 gitconfig_owner_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
257 if (gitconfig == NULL)
258 return got_error(GOT_ERR_PRIVSEP_MSG);
260 value = got_gitconfig_get_str(gitconfig, "gotweb", "owner");
262 return send_gitconfig_str(ibuf, value);
263 value = got_gitconfig_get_str(gitconfig, "gitweb", "owner");
264 return send_gitconfig_str(ibuf, value);
267 static const struct got_error *
268 gitconfig_extensions_request(struct imsgbuf *ibuf,
269 struct got_gitconfig *gitconfig)
271 const struct got_error *err = NULL;
272 struct got_gitconfig_list *tags;
273 struct got_gitconfig_list_node *node;
277 if (gitconfig == NULL)
278 return got_error(GOT_ERR_PRIVSEP_MSG);
280 tags = got_gitconfig_get_tag_list(gitconfig, "extensions");
282 return send_gitconfig_int(ibuf, 0);
284 TAILQ_FOREACH(node, &tags->fields, link) {
285 val = got_gitconfig_get_str(gitconfig, "extensions",
287 if (get_boolean_val(val))
291 err = send_gitconfig_int(ibuf, nextensions);
295 TAILQ_FOREACH(node, &tags->fields, link) {
296 val = got_gitconfig_get_str(gitconfig, "extensions",
298 if (get_boolean_val(val)) {
299 err = send_gitconfig_str(ibuf, node->field);
305 got_gitconfig_free_list(tags);
310 main(int argc, char *argv[])
312 const struct got_error *err = NULL;
315 struct got_gitconfig *gitconfig = NULL;
322 signal(SIGINT, catch_sigint);
324 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
327 /* revoke access to most system calls */
328 if (pledge("stdio recvfd", NULL) == -1) {
329 err = got_error_from_errno("pledge");
330 got_privsep_send_error(&ibuf, err);
338 memset(&imsg, 0, sizeof(imsg));
341 if (sigint_received) {
342 err = got_error(GOT_ERR_CANCELLED);
346 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
348 if (err->code == GOT_ERR_PRIVSEP_PIPE)
353 if (imsg.hdr.type == GOT_IMSG_STOP)
356 switch (imsg.hdr.type) {
357 case GOT_IMSG_GITCONFIG_PARSE_REQUEST:
358 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
360 err = got_error(GOT_ERR_PRIVSEP_LEN);
364 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
369 got_gitconfig_close(gitconfig);
370 err = got_gitconfig_open(&gitconfig, imsg.fd);
372 case GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST:
373 err = gitconfig_num_request(&ibuf, gitconfig, "core",
374 "repositoryformatversion", 0);
376 case GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST:
377 err = gitconfig_extensions_request(&ibuf, gitconfig);
379 case GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST:
380 err = gitconfig_str_request(&ibuf, gitconfig, "user",
383 case GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST:
384 err = gitconfig_str_request(&ibuf, gitconfig, "user",
387 case GOT_IMSG_GITCONFIG_REMOTES_REQUEST:
388 err = gitconfig_remotes_request(&ibuf, gitconfig);
390 case GOT_IMSG_GITCONFIG_OWNER_REQUEST:
391 err = gitconfig_owner_request(&ibuf, gitconfig);
394 err = got_error(GOT_ERR_PRIVSEP_MSG);
399 if (close(imsg.fd) == -1 && err == NULL)
400 err = got_error_from_errno("close");
410 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
411 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
412 got_privsep_send_error(&ibuf, err);
415 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
416 err = got_error_from_errno("close");