2 50b0790e 2020-09-11 stsp * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
4 50b0790e 2020-09-11 stsp * Permission to use, copy, modify, and distribute this software for any
5 50b0790e 2020-09-11 stsp * purpose with or without fee is hereby granted, provided that the above
6 50b0790e 2020-09-11 stsp * copyright notice and this permission notice appear in all copies.
8 50b0790e 2020-09-11 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 50b0790e 2020-09-11 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 50b0790e 2020-09-11 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 50b0790e 2020-09-11 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 50b0790e 2020-09-11 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 50b0790e 2020-09-11 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 50b0790e 2020-09-11 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 50b0790e 2020-09-11 stsp #include <sys/types.h>
18 50b0790e 2020-09-11 stsp #include <sys/uio.h>
19 50b0790e 2020-09-11 stsp #include <sys/socket.h>
21 50b0790e 2020-09-11 stsp #include <unistd.h>
22 50b0790e 2020-09-11 stsp #include <fcntl.h>
23 50b0790e 2020-09-11 stsp #include <errno.h>
24 50b0790e 2020-09-11 stsp #include <stdlib.h>
25 50b0790e 2020-09-11 stsp #include <stdio.h>
26 50b0790e 2020-09-11 stsp #include <stdint.h>
27 50b0790e 2020-09-11 stsp #include <limits.h>
29 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
31 50b0790e 2020-09-11 stsp #include "got_error.h"
32 50b0790e 2020-09-11 stsp #include "got_object.h"
33 50b0790e 2020-09-11 stsp #include "got_repository.h"
35 50b0790e 2020-09-11 stsp #include "got_lib_delta.h"
36 50b0790e 2020-09-11 stsp #include "got_lib_object.h"
37 50b0790e 2020-09-11 stsp #include "got_lib_privsep.h"
38 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
40 ef20f542 2022-06-26 thomas #include "got_gotconfig.h"
42 50b0790e 2020-09-11 stsp const struct got_error *
43 50b0790e 2020-09-11 stsp got_gotconfig_read(struct got_gotconfig **conf, const char *gotconfig_path)
45 50b0790e 2020-09-11 stsp const struct got_error *err = NULL, *child_err = NULL;
46 50b0790e 2020-09-11 stsp int fd = -1;
47 50b0790e 2020-09-11 stsp int imsg_fds[2] = { -1, -1 };
49 50b0790e 2020-09-11 stsp struct imsgbuf *ibuf;
51 50b0790e 2020-09-11 stsp *conf = calloc(1, sizeof(**conf));
52 50b0790e 2020-09-11 stsp if (*conf == NULL)
53 50b0790e 2020-09-11 stsp return got_error_from_errno("calloc");
55 06340621 2021-12-31 thomas fd = open(gotconfig_path, O_RDONLY | O_CLOEXEC);
56 50b0790e 2020-09-11 stsp if (fd == -1) {
57 50b0790e 2020-09-11 stsp if (errno == ENOENT)
58 50b0790e 2020-09-11 stsp return NULL;
59 50b0790e 2020-09-11 stsp return got_error_from_errno2("open", gotconfig_path);
62 50b0790e 2020-09-11 stsp ibuf = calloc(1, sizeof(*ibuf));
63 50b0790e 2020-09-11 stsp if (ibuf == NULL) {
64 50b0790e 2020-09-11 stsp err = got_error_from_errno("calloc");
68 50b0790e 2020-09-11 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
69 50b0790e 2020-09-11 stsp err = got_error_from_errno("socketpair");
73 50b0790e 2020-09-11 stsp pid = fork();
74 50b0790e 2020-09-11 stsp if (pid == -1) {
75 50b0790e 2020-09-11 stsp err = got_error_from_errno("fork");
77 50b0790e 2020-09-11 stsp } else if (pid == 0) {
78 50b0790e 2020-09-11 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GOTCONFIG,
79 50b0790e 2020-09-11 stsp gotconfig_path);
80 50b0790e 2020-09-11 stsp /* not reached */
83 50b0790e 2020-09-11 stsp if (close(imsg_fds[1]) == -1) {
84 50b0790e 2020-09-11 stsp err = got_error_from_errno("close");
87 50b0790e 2020-09-11 stsp imsg_fds[1] = -1;
88 50b0790e 2020-09-11 stsp imsg_init(ibuf, imsg_fds[0]);
90 50b0790e 2020-09-11 stsp err = got_privsep_send_gotconfig_parse_req(ibuf, fd);
95 50b0790e 2020-09-11 stsp err = got_privsep_send_gotconfig_author_req(ibuf);
99 50b0790e 2020-09-11 stsp err = got_privsep_recv_gotconfig_str(&(*conf)->author, ibuf);
103 871bd038 2022-07-03 thomas err = got_privsep_send_gotconfig_allowed_signers_req(ibuf);
105 871bd038 2022-07-03 thomas goto done;
107 871bd038 2022-07-03 thomas err = got_privsep_recv_gotconfig_str(&(*conf)->allowed_signers_file,
110 871bd038 2022-07-03 thomas goto done;
112 871bd038 2022-07-03 thomas err = got_privsep_send_gotconfig_revoked_signers_req(ibuf);
114 871bd038 2022-07-03 thomas goto done;
116 871bd038 2022-07-03 thomas err = got_privsep_recv_gotconfig_str(&(*conf)->revoked_signers_file,
119 871bd038 2022-07-03 thomas goto done;
121 ff5e1f09 2022-07-06 thomas err = got_privsep_send_gotconfig_signer_id_req(ibuf);
123 ff5e1f09 2022-07-06 thomas goto done;
125 ff5e1f09 2022-07-06 thomas err = got_privsep_recv_gotconfig_str(&(*conf)->signer_id, ibuf);
127 ff5e1f09 2022-07-06 thomas goto done;
129 50b0790e 2020-09-11 stsp err = got_privsep_send_gotconfig_remotes_req(ibuf);
133 50b0790e 2020-09-11 stsp err = got_privsep_recv_gotconfig_remotes(&(*conf)->remotes,
134 50b0790e 2020-09-11 stsp &(*conf)->nremotes, ibuf);
138 50b0790e 2020-09-11 stsp err = got_privsep_send_stop(imsg_fds[0]);
139 50b0790e 2020-09-11 stsp child_err = got_privsep_wait_for_child(pid);
140 50b0790e 2020-09-11 stsp if (child_err && err == NULL)
141 50b0790e 2020-09-11 stsp err = child_err;
143 50b0790e 2020-09-11 stsp if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
144 50b0790e 2020-09-11 stsp err = got_error_from_errno("close");
145 50b0790e 2020-09-11 stsp if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
146 50b0790e 2020-09-11 stsp err = got_error_from_errno("close");
147 50b0790e 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
148 50b0790e 2020-09-11 stsp err = got_error_from_errno2("close", gotconfig_path);
150 50b0790e 2020-09-11 stsp got_gotconfig_free(*conf);
151 50b0790e 2020-09-11 stsp *conf = NULL;
153 50b0790e 2020-09-11 stsp free(ibuf);
154 50b0790e 2020-09-11 stsp return err;
158 50b0790e 2020-09-11 stsp got_gotconfig_free(struct got_gotconfig *conf)
162 a9705505 2020-09-18 stsp if (conf == NULL)
165 50b0790e 2020-09-11 stsp free(conf->author);
167 b8adfa55 2020-09-25 stsp for (i = 0; i < conf->nremotes; i++)
168 b8adfa55 2020-09-25 stsp got_repo_free_remote_repo_data(&conf->remotes[i]);
169 50b0790e 2020-09-11 stsp free(conf->remotes);
170 50b0790e 2020-09-11 stsp free(conf);
173 50b0790e 2020-09-11 stsp const char *
174 50b0790e 2020-09-11 stsp got_gotconfig_get_author(const struct got_gotconfig *conf)
176 50b0790e 2020-09-11 stsp return conf->author;
180 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(int *nremotes, const struct got_remote_repo **remotes,
181 50b0790e 2020-09-11 stsp const struct got_gotconfig *conf)
183 50b0790e 2020-09-11 stsp *nremotes = conf->nremotes;
184 50b0790e 2020-09-11 stsp *remotes = conf->remotes;
187 871bd038 2022-07-03 thomas const char *
188 871bd038 2022-07-03 thomas got_gotconfig_get_allowed_signers_file(const struct got_gotconfig *conf)
190 871bd038 2022-07-03 thomas return conf->allowed_signers_file;
193 871bd038 2022-07-03 thomas const char *
194 871bd038 2022-07-03 thomas got_gotconfig_get_revoked_signers_file(const struct got_gotconfig *conf)
196 871bd038 2022-07-03 thomas return conf->revoked_signers_file;
199 ff5e1f09 2022-07-06 thomas const char *
200 ff5e1f09 2022-07-06 thomas got_gotconfig_get_signer_id(const struct got_gotconfig *conf)
202 ff5e1f09 2022-07-06 thomas return conf->signer_id;