Blame


1 50b0790e 2020-09-11 stsp /*
2 50b0790e 2020-09-11 stsp * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
3 50b0790e 2020-09-11 stsp *
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.
7 50b0790e 2020-09-11 stsp *
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.
15 50b0790e 2020-09-11 stsp */
16 50b0790e 2020-09-11 stsp
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>
20 50b0790e 2020-09-11 stsp
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>
28 50b0790e 2020-09-11 stsp
29 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
30 dd038bc6 2021-09-21 thomas.ad
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"
34 50b0790e 2020-09-11 stsp
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"
39 50b0790e 2020-09-11 stsp
40 ef20f542 2022-06-26 thomas #include "got_gotconfig.h"
41 ef20f542 2022-06-26 thomas
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)
44 50b0790e 2020-09-11 stsp {
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 };
48 50b0790e 2020-09-11 stsp pid_t pid;
49 50b0790e 2020-09-11 stsp struct imsgbuf *ibuf;
50 50b0790e 2020-09-11 stsp
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");
54 50b0790e 2020-09-11 stsp
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);
60 50b0790e 2020-09-11 stsp }
61 50b0790e 2020-09-11 stsp
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");
65 50b0790e 2020-09-11 stsp goto done;
66 50b0790e 2020-09-11 stsp }
67 50b0790e 2020-09-11 stsp
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");
70 50b0790e 2020-09-11 stsp goto done;
71 50b0790e 2020-09-11 stsp }
72 50b0790e 2020-09-11 stsp
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");
76 50b0790e 2020-09-11 stsp goto done;
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 */
81 50b0790e 2020-09-11 stsp }
82 50b0790e 2020-09-11 stsp
83 50b0790e 2020-09-11 stsp if (close(imsg_fds[1]) == -1) {
84 50b0790e 2020-09-11 stsp err = got_error_from_errno("close");
85 50b0790e 2020-09-11 stsp goto done;
86 50b0790e 2020-09-11 stsp }
87 50b0790e 2020-09-11 stsp imsg_fds[1] = -1;
88 50b0790e 2020-09-11 stsp imsg_init(ibuf, imsg_fds[0]);
89 50b0790e 2020-09-11 stsp
90 50b0790e 2020-09-11 stsp err = got_privsep_send_gotconfig_parse_req(ibuf, fd);
91 50b0790e 2020-09-11 stsp if (err)
92 50b0790e 2020-09-11 stsp goto done;
93 50b0790e 2020-09-11 stsp fd = -1;
94 50b0790e 2020-09-11 stsp
95 50b0790e 2020-09-11 stsp err = got_privsep_send_gotconfig_author_req(ibuf);
96 50b0790e 2020-09-11 stsp if (err)
97 50b0790e 2020-09-11 stsp goto done;
98 50b0790e 2020-09-11 stsp
99 50b0790e 2020-09-11 stsp err = got_privsep_recv_gotconfig_str(&(*conf)->author, ibuf);
100 50b0790e 2020-09-11 stsp if (err)
101 50b0790e 2020-09-11 stsp goto done;
102 50b0790e 2020-09-11 stsp
103 871bd038 2022-07-03 thomas err = got_privsep_send_gotconfig_allowed_signers_req(ibuf);
104 871bd038 2022-07-03 thomas if (err)
105 871bd038 2022-07-03 thomas goto done;
106 871bd038 2022-07-03 thomas
107 871bd038 2022-07-03 thomas err = got_privsep_recv_gotconfig_str(&(*conf)->allowed_signers_file,
108 871bd038 2022-07-03 thomas ibuf);
109 871bd038 2022-07-03 thomas if (err)
110 871bd038 2022-07-03 thomas goto done;
111 871bd038 2022-07-03 thomas
112 871bd038 2022-07-03 thomas err = got_privsep_send_gotconfig_revoked_signers_req(ibuf);
113 871bd038 2022-07-03 thomas if (err)
114 871bd038 2022-07-03 thomas goto done;
115 871bd038 2022-07-03 thomas
116 871bd038 2022-07-03 thomas err = got_privsep_recv_gotconfig_str(&(*conf)->revoked_signers_file,
117 871bd038 2022-07-03 thomas ibuf);
118 871bd038 2022-07-03 thomas if (err)
119 871bd038 2022-07-03 thomas goto done;
120 871bd038 2022-07-03 thomas
121 ff5e1f09 2022-07-06 thomas err = got_privsep_send_gotconfig_signer_id_req(ibuf);
122 ff5e1f09 2022-07-06 thomas if (err)
123 ff5e1f09 2022-07-06 thomas goto done;
124 ff5e1f09 2022-07-06 thomas
125 ff5e1f09 2022-07-06 thomas err = got_privsep_recv_gotconfig_str(&(*conf)->signer_id, ibuf);
126 ff5e1f09 2022-07-06 thomas if (err)
127 ff5e1f09 2022-07-06 thomas goto done;
128 ff5e1f09 2022-07-06 thomas
129 50b0790e 2020-09-11 stsp err = got_privsep_send_gotconfig_remotes_req(ibuf);
130 50b0790e 2020-09-11 stsp if (err)
131 50b0790e 2020-09-11 stsp goto done;
132 50b0790e 2020-09-11 stsp
133 50b0790e 2020-09-11 stsp err = got_privsep_recv_gotconfig_remotes(&(*conf)->remotes,
134 50b0790e 2020-09-11 stsp &(*conf)->nremotes, ibuf);
135 50b0790e 2020-09-11 stsp if (err)
136 50b0790e 2020-09-11 stsp goto done;
137 50b0790e 2020-09-11 stsp
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;
142 50b0790e 2020-09-11 stsp done:
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);
149 50b0790e 2020-09-11 stsp if (err) {
150 50b0790e 2020-09-11 stsp got_gotconfig_free(*conf);
151 50b0790e 2020-09-11 stsp *conf = NULL;
152 50b0790e 2020-09-11 stsp }
153 50b0790e 2020-09-11 stsp free(ibuf);
154 50b0790e 2020-09-11 stsp return err;
155 50b0790e 2020-09-11 stsp }
156 50b0790e 2020-09-11 stsp
157 50b0790e 2020-09-11 stsp void
158 50b0790e 2020-09-11 stsp got_gotconfig_free(struct got_gotconfig *conf)
159 50b0790e 2020-09-11 stsp {
160 50b0790e 2020-09-11 stsp int i;
161 50b0790e 2020-09-11 stsp
162 a9705505 2020-09-18 stsp if (conf == NULL)
163 a9705505 2020-09-18 stsp return;
164 a9705505 2020-09-18 stsp
165 50b0790e 2020-09-11 stsp free(conf->author);
166 50b0790e 2020-09-11 stsp
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);
171 50b0790e 2020-09-11 stsp }
172 50b0790e 2020-09-11 stsp
173 50b0790e 2020-09-11 stsp const char *
174 50b0790e 2020-09-11 stsp got_gotconfig_get_author(const struct got_gotconfig *conf)
175 50b0790e 2020-09-11 stsp {
176 50b0790e 2020-09-11 stsp return conf->author;
177 50b0790e 2020-09-11 stsp }
178 50b0790e 2020-09-11 stsp
179 50b0790e 2020-09-11 stsp void
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)
182 50b0790e 2020-09-11 stsp {
183 50b0790e 2020-09-11 stsp *nremotes = conf->nremotes;
184 50b0790e 2020-09-11 stsp *remotes = conf->remotes;
185 50b0790e 2020-09-11 stsp }
186 871bd038 2022-07-03 thomas
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)
189 871bd038 2022-07-03 thomas {
190 871bd038 2022-07-03 thomas return conf->allowed_signers_file;
191 871bd038 2022-07-03 thomas }
192 871bd038 2022-07-03 thomas
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)
195 871bd038 2022-07-03 thomas {
196 871bd038 2022-07-03 thomas return conf->revoked_signers_file;
197 871bd038 2022-07-03 thomas }
198 ff5e1f09 2022-07-06 thomas
199 ff5e1f09 2022-07-06 thomas const char *
200 ff5e1f09 2022-07-06 thomas got_gotconfig_get_signer_id(const struct got_gotconfig *conf)
201 ff5e1f09 2022-07-06 thomas {
202 ff5e1f09 2022-07-06 thomas return conf->signer_id;
203 ff5e1f09 2022-07-06 thomas }