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 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
19 50b0790e 2020-09-11 stsp #include <sys/uio.h>
20 50b0790e 2020-09-11 stsp #include <sys/socket.h>
21 50b0790e 2020-09-11 stsp
22 50b0790e 2020-09-11 stsp #include <unistd.h>
23 50b0790e 2020-09-11 stsp #include <fcntl.h>
24 50b0790e 2020-09-11 stsp #include <errno.h>
25 50b0790e 2020-09-11 stsp #include <stdlib.h>
26 50b0790e 2020-09-11 stsp #include <stdio.h>
27 50b0790e 2020-09-11 stsp #include <stdint.h>
28 50b0790e 2020-09-11 stsp #include <limits.h>
29 50b0790e 2020-09-11 stsp
30 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
31 dd038bc6 2021-09-21 thomas.ad
32 50b0790e 2020-09-11 stsp #include "got_error.h"
33 50b0790e 2020-09-11 stsp #include "got_object.h"
34 50b0790e 2020-09-11 stsp #include "got_repository.h"
35 50b0790e 2020-09-11 stsp
36 50b0790e 2020-09-11 stsp #include "got_lib_delta.h"
37 50b0790e 2020-09-11 stsp #include "got_lib_object.h"
38 50b0790e 2020-09-11 stsp #include "got_lib_privsep.h"
39 50b0790e 2020-09-11 stsp #include "got_lib_gotconfig.h"
40 50b0790e 2020-09-11 stsp
41 ef20f542 2022-06-26 thomas #include "got_gotconfig.h"
42 ef20f542 2022-06-26 thomas
43 50b0790e 2020-09-11 stsp const struct got_error *
44 50b0790e 2020-09-11 stsp got_gotconfig_read(struct got_gotconfig **conf, const char *gotconfig_path)
45 50b0790e 2020-09-11 stsp {
46 50b0790e 2020-09-11 stsp const struct got_error *err = NULL, *child_err = NULL;
47 50b0790e 2020-09-11 stsp int fd = -1;
48 50b0790e 2020-09-11 stsp int imsg_fds[2] = { -1, -1 };
49 50b0790e 2020-09-11 stsp pid_t pid;
50 50b0790e 2020-09-11 stsp struct imsgbuf *ibuf;
51 50b0790e 2020-09-11 stsp
52 50b0790e 2020-09-11 stsp *conf = calloc(1, sizeof(**conf));
53 50b0790e 2020-09-11 stsp if (*conf == NULL)
54 50b0790e 2020-09-11 stsp return got_error_from_errno("calloc");
55 50b0790e 2020-09-11 stsp
56 06340621 2021-12-31 thomas fd = open(gotconfig_path, O_RDONLY | O_CLOEXEC);
57 50b0790e 2020-09-11 stsp if (fd == -1) {
58 50b0790e 2020-09-11 stsp if (errno == ENOENT)
59 50b0790e 2020-09-11 stsp return NULL;
60 50b0790e 2020-09-11 stsp return got_error_from_errno2("open", gotconfig_path);
61 50b0790e 2020-09-11 stsp }
62 50b0790e 2020-09-11 stsp
63 50b0790e 2020-09-11 stsp ibuf = calloc(1, sizeof(*ibuf));
64 50b0790e 2020-09-11 stsp if (ibuf == NULL) {
65 50b0790e 2020-09-11 stsp err = got_error_from_errno("calloc");
66 50b0790e 2020-09-11 stsp goto done;
67 50b0790e 2020-09-11 stsp }
68 50b0790e 2020-09-11 stsp
69 50b0790e 2020-09-11 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
70 50b0790e 2020-09-11 stsp err = got_error_from_errno("socketpair");
71 50b0790e 2020-09-11 stsp goto done;
72 50b0790e 2020-09-11 stsp }
73 50b0790e 2020-09-11 stsp
74 50b0790e 2020-09-11 stsp pid = fork();
75 50b0790e 2020-09-11 stsp if (pid == -1) {
76 50b0790e 2020-09-11 stsp err = got_error_from_errno("fork");
77 50b0790e 2020-09-11 stsp goto done;
78 50b0790e 2020-09-11 stsp } else if (pid == 0) {
79 50b0790e 2020-09-11 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GOTCONFIG,
80 50b0790e 2020-09-11 stsp gotconfig_path);
81 50b0790e 2020-09-11 stsp /* not reached */
82 50b0790e 2020-09-11 stsp }
83 50b0790e 2020-09-11 stsp
84 50b0790e 2020-09-11 stsp if (close(imsg_fds[1]) == -1) {
85 50b0790e 2020-09-11 stsp err = got_error_from_errno("close");
86 50b0790e 2020-09-11 stsp goto done;
87 50b0790e 2020-09-11 stsp }
88 50b0790e 2020-09-11 stsp imsg_fds[1] = -1;
89 50b0790e 2020-09-11 stsp imsg_init(ibuf, imsg_fds[0]);
90 50b0790e 2020-09-11 stsp
91 50b0790e 2020-09-11 stsp err = got_privsep_send_gotconfig_parse_req(ibuf, fd);
92 50b0790e 2020-09-11 stsp if (err)
93 50b0790e 2020-09-11 stsp goto done;
94 50b0790e 2020-09-11 stsp fd = -1;
95 50b0790e 2020-09-11 stsp
96 50b0790e 2020-09-11 stsp err = got_privsep_send_gotconfig_author_req(ibuf);
97 50b0790e 2020-09-11 stsp if (err)
98 50b0790e 2020-09-11 stsp goto done;
99 50b0790e 2020-09-11 stsp
100 50b0790e 2020-09-11 stsp err = got_privsep_recv_gotconfig_str(&(*conf)->author, ibuf);
101 50b0790e 2020-09-11 stsp if (err)
102 50b0790e 2020-09-11 stsp goto done;
103 50b0790e 2020-09-11 stsp
104 871bd038 2022-07-03 thomas err = got_privsep_send_gotconfig_allowed_signers_req(ibuf);
105 871bd038 2022-07-03 thomas if (err)
106 871bd038 2022-07-03 thomas goto done;
107 871bd038 2022-07-03 thomas
108 871bd038 2022-07-03 thomas err = got_privsep_recv_gotconfig_str(&(*conf)->allowed_signers_file,
109 871bd038 2022-07-03 thomas ibuf);
110 871bd038 2022-07-03 thomas if (err)
111 871bd038 2022-07-03 thomas goto done;
112 871bd038 2022-07-03 thomas
113 871bd038 2022-07-03 thomas err = got_privsep_send_gotconfig_revoked_signers_req(ibuf);
114 871bd038 2022-07-03 thomas if (err)
115 871bd038 2022-07-03 thomas goto done;
116 871bd038 2022-07-03 thomas
117 871bd038 2022-07-03 thomas err = got_privsep_recv_gotconfig_str(&(*conf)->revoked_signers_file,
118 871bd038 2022-07-03 thomas ibuf);
119 871bd038 2022-07-03 thomas if (err)
120 871bd038 2022-07-03 thomas goto done;
121 871bd038 2022-07-03 thomas
122 ff5e1f09 2022-07-06 thomas err = got_privsep_send_gotconfig_signer_id_req(ibuf);
123 ff5e1f09 2022-07-06 thomas if (err)
124 ff5e1f09 2022-07-06 thomas goto done;
125 ff5e1f09 2022-07-06 thomas
126 ff5e1f09 2022-07-06 thomas err = got_privsep_recv_gotconfig_str(&(*conf)->signer_id, ibuf);
127 ff5e1f09 2022-07-06 thomas if (err)
128 ff5e1f09 2022-07-06 thomas goto done;
129 ff5e1f09 2022-07-06 thomas
130 50b0790e 2020-09-11 stsp err = got_privsep_send_gotconfig_remotes_req(ibuf);
131 50b0790e 2020-09-11 stsp if (err)
132 50b0790e 2020-09-11 stsp goto done;
133 50b0790e 2020-09-11 stsp
134 50b0790e 2020-09-11 stsp err = got_privsep_recv_gotconfig_remotes(&(*conf)->remotes,
135 50b0790e 2020-09-11 stsp &(*conf)->nremotes, ibuf);
136 50b0790e 2020-09-11 stsp if (err)
137 50b0790e 2020-09-11 stsp goto done;
138 50b0790e 2020-09-11 stsp
139 50b0790e 2020-09-11 stsp err = got_privsep_send_stop(imsg_fds[0]);
140 50b0790e 2020-09-11 stsp child_err = got_privsep_wait_for_child(pid);
141 50b0790e 2020-09-11 stsp if (child_err && err == NULL)
142 50b0790e 2020-09-11 stsp err = child_err;
143 50b0790e 2020-09-11 stsp done:
144 50b0790e 2020-09-11 stsp if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
145 50b0790e 2020-09-11 stsp err = got_error_from_errno("close");
146 50b0790e 2020-09-11 stsp if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
147 50b0790e 2020-09-11 stsp err = got_error_from_errno("close");
148 50b0790e 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
149 50b0790e 2020-09-11 stsp err = got_error_from_errno2("close", gotconfig_path);
150 50b0790e 2020-09-11 stsp if (err) {
151 50b0790e 2020-09-11 stsp got_gotconfig_free(*conf);
152 50b0790e 2020-09-11 stsp *conf = NULL;
153 50b0790e 2020-09-11 stsp }
154 50b0790e 2020-09-11 stsp free(ibuf);
155 50b0790e 2020-09-11 stsp return err;
156 50b0790e 2020-09-11 stsp }
157 50b0790e 2020-09-11 stsp
158 50b0790e 2020-09-11 stsp void
159 50b0790e 2020-09-11 stsp got_gotconfig_free(struct got_gotconfig *conf)
160 50b0790e 2020-09-11 stsp {
161 50b0790e 2020-09-11 stsp int i;
162 50b0790e 2020-09-11 stsp
163 a9705505 2020-09-18 stsp if (conf == NULL)
164 a9705505 2020-09-18 stsp return;
165 a9705505 2020-09-18 stsp
166 50b0790e 2020-09-11 stsp free(conf->author);
167 50b0790e 2020-09-11 stsp
168 b8adfa55 2020-09-25 stsp for (i = 0; i < conf->nremotes; i++)
169 b8adfa55 2020-09-25 stsp got_repo_free_remote_repo_data(&conf->remotes[i]);
170 50b0790e 2020-09-11 stsp free(conf->remotes);
171 50b0790e 2020-09-11 stsp free(conf);
172 50b0790e 2020-09-11 stsp }
173 50b0790e 2020-09-11 stsp
174 50b0790e 2020-09-11 stsp const char *
175 50b0790e 2020-09-11 stsp got_gotconfig_get_author(const struct got_gotconfig *conf)
176 50b0790e 2020-09-11 stsp {
177 50b0790e 2020-09-11 stsp return conf->author;
178 50b0790e 2020-09-11 stsp }
179 50b0790e 2020-09-11 stsp
180 50b0790e 2020-09-11 stsp void
181 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(int *nremotes, const struct got_remote_repo **remotes,
182 50b0790e 2020-09-11 stsp const struct got_gotconfig *conf)
183 50b0790e 2020-09-11 stsp {
184 50b0790e 2020-09-11 stsp *nremotes = conf->nremotes;
185 50b0790e 2020-09-11 stsp *remotes = conf->remotes;
186 50b0790e 2020-09-11 stsp }
187 871bd038 2022-07-03 thomas
188 871bd038 2022-07-03 thomas const char *
189 871bd038 2022-07-03 thomas got_gotconfig_get_allowed_signers_file(const struct got_gotconfig *conf)
190 871bd038 2022-07-03 thomas {
191 871bd038 2022-07-03 thomas return conf->allowed_signers_file;
192 871bd038 2022-07-03 thomas }
193 871bd038 2022-07-03 thomas
194 871bd038 2022-07-03 thomas const char *
195 871bd038 2022-07-03 thomas got_gotconfig_get_revoked_signers_file(const struct got_gotconfig *conf)
196 871bd038 2022-07-03 thomas {
197 871bd038 2022-07-03 thomas return conf->revoked_signers_file;
198 871bd038 2022-07-03 thomas }
199 ff5e1f09 2022-07-06 thomas
200 ff5e1f09 2022-07-06 thomas const char *
201 ff5e1f09 2022-07-06 thomas got_gotconfig_get_signer_id(const struct got_gotconfig *conf)
202 ff5e1f09 2022-07-06 thomas {
203 ff5e1f09 2022-07-06 thomas return conf->signer_id;
204 ff5e1f09 2022-07-06 thomas }