Blob


1 /*
2 * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/uio.h>
20 #include <sys/socket.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <errno.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <stdint.h>
28 #include <imsg.h>
29 #include <sha1.h>
30 #include <limits.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_gotconfig.h"
41 #include "got_gotconfig.h"
43 const struct got_error *
44 got_gotconfig_read(struct got_gotconfig **conf, const char *gotconfig_path)
45 {
46 const struct got_error *err = NULL, *child_err = NULL;
47 int fd = -1;
48 int imsg_fds[2] = { -1, -1 };
49 pid_t pid;
50 struct imsgbuf *ibuf;
52 *conf = calloc(1, sizeof(**conf));
53 if (*conf == NULL)
54 return got_error_from_errno("calloc");
56 fd = open(gotconfig_path, O_RDONLY | O_CLOEXEC);
57 if (fd == -1) {
58 if (errno == ENOENT)
59 return NULL;
60 return got_error_from_errno2("open", gotconfig_path);
61 }
63 ibuf = calloc(1, sizeof(*ibuf));
64 if (ibuf == NULL) {
65 err = got_error_from_errno("calloc");
66 goto done;
67 }
69 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
70 err = got_error_from_errno("socketpair");
71 goto done;
72 }
74 pid = fork();
75 if (pid == -1) {
76 err = got_error_from_errno("fork");
77 goto done;
78 } else if (pid == 0) {
79 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GOTCONFIG,
80 gotconfig_path);
81 /* not reached */
82 }
84 if (close(imsg_fds[1]) == -1) {
85 err = got_error_from_errno("close");
86 goto done;
87 }
88 imsg_fds[1] = -1;
89 imsg_init(ibuf, imsg_fds[0]);
91 err = got_privsep_send_gotconfig_parse_req(ibuf, fd);
92 if (err)
93 goto done;
94 fd = -1;
96 err = got_privsep_send_gotconfig_author_req(ibuf);
97 if (err)
98 goto done;
100 err = got_privsep_recv_gotconfig_str(&(*conf)->author, ibuf);
101 if (err)
102 goto done;
104 err = got_privsep_send_gotconfig_allowed_signers_req(ibuf);
105 if (err)
106 goto done;
108 err = got_privsep_recv_gotconfig_str(&(*conf)->allowed_signers_file,
109 ibuf);
110 if (err)
111 goto done;
113 err = got_privsep_send_gotconfig_revoked_signers_req(ibuf);
114 if (err)
115 goto done;
117 err = got_privsep_recv_gotconfig_str(&(*conf)->revoked_signers_file,
118 ibuf);
119 if (err)
120 goto done;
122 err = got_privsep_send_gotconfig_signer_id_req(ibuf);
123 if (err)
124 goto done;
126 err = got_privsep_recv_gotconfig_str(&(*conf)->signer_id, ibuf);
127 if (err)
128 goto done;
130 err = got_privsep_send_gotconfig_remotes_req(ibuf);
131 if (err)
132 goto done;
134 err = got_privsep_recv_gotconfig_remotes(&(*conf)->remotes,
135 &(*conf)->nremotes, ibuf);
136 if (err)
137 goto done;
139 err = got_privsep_send_stop(imsg_fds[0]);
140 child_err = got_privsep_wait_for_child(pid);
141 if (child_err && err == NULL)
142 err = child_err;
143 done:
144 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
145 err = got_error_from_errno("close");
146 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
147 err = got_error_from_errno("close");
148 if (fd != -1 && close(fd) == -1 && err == NULL)
149 err = got_error_from_errno2("close", gotconfig_path);
150 if (err) {
151 got_gotconfig_free(*conf);
152 *conf = NULL;
154 free(ibuf);
155 return err;
158 void
159 got_gotconfig_free(struct got_gotconfig *conf)
161 int i;
163 if (conf == NULL)
164 return;
166 free(conf->author);
168 for (i = 0; i < conf->nremotes; i++)
169 got_repo_free_remote_repo_data(&conf->remotes[i]);
170 free(conf->remotes);
171 free(conf);
174 const char *
175 got_gotconfig_get_author(const struct got_gotconfig *conf)
177 return conf->author;
180 void
181 got_gotconfig_get_remotes(int *nremotes, const struct got_remote_repo **remotes,
182 const struct got_gotconfig *conf)
184 *nremotes = conf->nremotes;
185 *remotes = conf->remotes;
188 const char *
189 got_gotconfig_get_allowed_signers_file(const struct got_gotconfig *conf)
191 return conf->allowed_signers_file;
194 const char *
195 got_gotconfig_get_revoked_signers_file(const struct got_gotconfig *conf)
197 return conf->revoked_signers_file;
200 const char *
201 got_gotconfig_get_signer_id(const struct got_gotconfig *conf)
203 return conf->signer_id;