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>
30 #include "got_compat.h"
32 #include "got_error.h"
33 #include "got_object.h"
34 #include "got_repository.h"
36 #include "got_lib_delta.h"
37 #include "got_lib_object.h"
38 #include "got_lib_privsep.h"
39 #include "got_lib_gitconfig.h"
41 static volatile sig_atomic_t sigint_received;
44 catch_sigint(int signo)
49 static const struct got_error *
50 send_gitconfig_int(struct imsgbuf *ibuf, int value)
52 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_INT_VAL, 0, 0, -1,
53 &value, sizeof(value)) == -1)
54 return got_error_from_errno("imsg_compose GITCONFIG_INT_VAL");
56 return got_privsep_flush_imsg(ibuf);
59 static const struct got_error *
60 gitconfig_num_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
61 const char *section, const char *tag, int def)
65 if (gitconfig == NULL)
66 return got_error(GOT_ERR_PRIVSEP_MSG);
68 value = got_gitconfig_get_num(gitconfig, section, tag, def);
69 return send_gitconfig_int(ibuf, value);
72 static const struct got_error *
73 send_gitconfig_str(struct imsgbuf *ibuf, const char *value)
75 size_t len = value ? strlen(value) : 0;
77 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_STR_VAL, 0, 0, -1,
79 return got_error_from_errno("imsg_compose GITCONFIG_STR_VAL");
81 return got_privsep_flush_imsg(ibuf);
84 static const struct got_error *
85 gitconfig_str_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
86 const char *section, const char *tag)
90 if (gitconfig == NULL)
91 return got_error(GOT_ERR_PRIVSEP_MSG);
93 value = got_gitconfig_get_str(gitconfig, section, tag);
94 return send_gitconfig_str(ibuf, value);
97 static const struct got_error *
98 send_gitconfig_remotes(struct imsgbuf *ibuf, struct got_remote_repo *remotes,
101 const struct got_error *err = NULL;
102 struct got_imsg_remotes iremotes;
105 iremotes.nremotes = nremotes;
106 if (imsg_compose(ibuf, GOT_IMSG_GITCONFIG_REMOTES, 0, 0, -1,
107 &iremotes, sizeof(iremotes)) == -1)
108 return got_error_from_errno("imsg_compose GITCONFIG_REMOTES");
110 err = got_privsep_flush_imsg(ibuf);
115 for (i = 0; i < nremotes; i++) {
116 struct got_imsg_remote iremote;
117 size_t len = sizeof(iremote);
120 iremote.mirror_references = remotes[i].mirror_references;
121 iremote.name_len = strlen(remotes[i].name);
122 len += iremote.name_len;
123 iremote.fetch_url_len = strlen(remotes[i].fetch_url);
124 len += iremote.fetch_url_len;
125 iremote.send_url_len = strlen(remotes[i].send_url);
126 len += iremote.send_url_len;
128 wbuf = imsg_create(ibuf, GOT_IMSG_GITCONFIG_REMOTE, 0, 0, len);
130 return got_error_from_errno(
131 "imsg_create GITCONFIG_REMOTE");
133 if (imsg_add(wbuf, &iremote, sizeof(iremote)) == -1)
134 return got_error_from_errno(
135 "imsg_add GITCONFIG_REMOTE");
137 if (imsg_add(wbuf, remotes[i].name, iremote.name_len) == -1)
138 return got_error_from_errno(
139 "imsg_add GITCONFIG_REMOTE");
140 if (imsg_add(wbuf, remotes[i].fetch_url, iremote.fetch_url_len) == -1)
141 return got_error_from_errno(
142 "imsg_add GITCONFIG_REMOTE");
143 if (imsg_add(wbuf, remotes[i].send_url, iremote.send_url_len) == -1)
144 return got_error_from_errno(
145 "imsg_add GITCONFIG_REMOTE");
148 imsg_close(ibuf, wbuf);
149 err = got_privsep_flush_imsg(ibuf);
158 get_boolean_val(char *val)
160 return (strcasecmp(val, "true") == 0 ||
161 strcasecmp(val, "on") == 0 ||
162 strcasecmp(val, "yes") == 0 ||
163 strcmp(val, "1") == 0);
166 static const struct got_error *
167 gitconfig_remotes_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
169 const struct got_error *err = NULL;
170 struct got_gitconfig_list *sections;
171 struct got_gitconfig_list_node *node;
172 struct got_remote_repo *remotes = NULL;
175 if (gitconfig == NULL)
176 return got_error(GOT_ERR_PRIVSEP_MSG);
178 err = got_gitconfig_get_section_list(§ions, gitconfig);
182 TAILQ_FOREACH(node, §ions->fields, link) {
183 if (strncasecmp("remote \"", node->field, 8) != 0)
189 err = send_gitconfig_remotes(ibuf, NULL, 0);
193 remotes = recallocarray(NULL, 0, nremotes, sizeof(*remotes));
194 if (remotes == NULL) {
195 err = got_error_from_errno("recallocarray");
200 TAILQ_FOREACH(node, §ions->fields, link) {
201 char *name, *end, *mirror;
203 if (strncasecmp("remote \"", node->field, 8) != 0)
206 name = strdup(node->field + 8);
208 err = got_error_from_errno("strdup");
211 end = strrchr(name, '"');
214 remotes[i].name = name;
216 remotes[i].fetch_url = got_gitconfig_get_str(gitconfig,
218 if (remotes[i].fetch_url == NULL) {
219 err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
223 remotes[i].send_url = got_gitconfig_get_str(gitconfig,
224 node->field, "pushurl");
225 if (remotes[i].send_url == NULL)
226 remotes[i].send_url = got_gitconfig_get_str(gitconfig,
228 if (remotes[i].send_url == NULL) {
229 err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
233 remotes[i].mirror_references = 0;
234 mirror = got_gitconfig_get_str(gitconfig, node->field,
236 if (mirror != NULL && get_boolean_val(mirror))
237 remotes[i].mirror_references = 1;
242 err = send_gitconfig_remotes(ibuf, remotes, nremotes);
244 for (i = 0; i < nremotes; i++)
245 free(remotes[i].name);
247 got_gitconfig_free_list(sections);
251 static const struct got_error *
252 gitconfig_owner_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
256 if (gitconfig == NULL)
257 return got_error(GOT_ERR_PRIVSEP_MSG);
259 value = got_gitconfig_get_str(gitconfig, "gotweb", "owner");
261 return send_gitconfig_str(ibuf, value);
262 value = got_gitconfig_get_str(gitconfig, "gitweb", "owner");
263 return send_gitconfig_str(ibuf, value);
266 static const struct got_error *
267 gitconfig_extensions_request(struct imsgbuf *ibuf,
268 struct got_gitconfig *gitconfig)
270 const struct got_error *err = NULL;
271 struct got_gitconfig_list *tags;
272 struct got_gitconfig_list_node *node;
276 if (gitconfig == NULL)
277 return got_error(GOT_ERR_PRIVSEP_MSG);
279 tags = got_gitconfig_get_tag_list(gitconfig, "extensions");
281 return send_gitconfig_int(ibuf, 0);
283 TAILQ_FOREACH(node, &tags->fields, link) {
284 val = got_gitconfig_get_str(gitconfig, "extensions",
286 if (get_boolean_val(val))
290 err = send_gitconfig_int(ibuf, nextensions);
294 TAILQ_FOREACH(node, &tags->fields, link) {
295 val = got_gitconfig_get_str(gitconfig, "extensions",
297 if (get_boolean_val(val)) {
298 err = send_gitconfig_str(ibuf, node->field);
304 got_gitconfig_free_list(tags);
309 main(int argc, char *argv[])
311 const struct got_error *err = NULL;
314 struct got_gitconfig *gitconfig = NULL;
321 signal(SIGINT, catch_sigint);
323 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
326 /* revoke access to most system calls */
327 if (pledge("stdio recvfd", NULL) == -1) {
328 err = got_error_from_errno("pledge");
329 got_privsep_send_error(&ibuf, err);
333 /* revoke fs access */
334 if (landlock_no_fs() == -1) {
335 err = got_error_from_errno("landlock_no_fs");
336 got_privsep_send_error(&ibuf, err);
339 if (cap_enter() == -1) {
340 err = got_error_from_errno("cap_enter");
341 got_privsep_send_error(&ibuf, err);
349 memset(&imsg, 0, sizeof(imsg));
352 if (sigint_received) {
353 err = got_error(GOT_ERR_CANCELLED);
357 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
359 if (err->code == GOT_ERR_PRIVSEP_PIPE)
364 if (imsg.hdr.type == GOT_IMSG_STOP)
367 switch (imsg.hdr.type) {
368 case GOT_IMSG_GITCONFIG_PARSE_REQUEST:
369 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
371 err = got_error(GOT_ERR_PRIVSEP_LEN);
375 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
380 got_gitconfig_close(gitconfig);
381 err = got_gitconfig_open(&gitconfig, imsg.fd);
383 case GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST:
384 err = gitconfig_num_request(&ibuf, gitconfig, "core",
385 "repositoryformatversion", 0);
387 case GOT_IMSG_GITCONFIG_REPOSITORY_EXTENSIONS_REQUEST:
388 err = gitconfig_extensions_request(&ibuf, gitconfig);
390 case GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST:
391 err = gitconfig_str_request(&ibuf, gitconfig, "user",
394 case GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST:
395 err = gitconfig_str_request(&ibuf, gitconfig, "user",
398 case GOT_IMSG_GITCONFIG_REMOTES_REQUEST:
399 err = gitconfig_remotes_request(&ibuf, gitconfig);
401 case GOT_IMSG_GITCONFIG_OWNER_REQUEST:
402 err = gitconfig_owner_request(&ibuf, gitconfig);
405 err = got_error(GOT_ERR_PRIVSEP_MSG);
410 if (close(imsg.fd) == -1 && err == NULL)
411 err = got_error_from_errno("close");
421 if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
422 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
423 got_privsep_send_error(&ibuf, err);
426 if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
427 err = got_error_from_errno("close");