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 <sha2.h>
31 #include <limits.h>
33 #include "got_error.h"
34 #include "got_object.h"
35 #include "got_repository.h"
37 #include "got_lib_delta.h"
38 #include "got_lib_object.h"
39 #include "got_lib_privsep.h"
40 #include "got_lib_gotconfig.h"
42 #include "got_gotconfig.h"
44 const struct got_error *
45 got_gotconfig_read(struct got_gotconfig **conf, const char *gotconfig_path)
46 {
47 const struct got_error *err = NULL, *child_err = NULL;
48 int fd = -1;
49 int imsg_fds[2] = { -1, -1 };
50 pid_t pid;
51 struct imsgbuf *ibuf = NULL;
53 *conf = calloc(1, sizeof(**conf));
54 if (*conf == NULL)
55 return got_error_from_errno("calloc");
57 fd = open(gotconfig_path, O_RDONLY | O_CLOEXEC);
58 if (fd == -1) {
59 if (errno == ENOENT)
60 return NULL;
61 err = got_error_from_errno2("open", gotconfig_path);
62 goto done;
63 }
65 ibuf = calloc(1, sizeof(*ibuf));
66 if (ibuf == NULL) {
67 err = got_error_from_errno("calloc");
68 goto done;
69 }
71 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
72 err = got_error_from_errno("socketpair");
73 goto done;
74 }
76 pid = fork();
77 if (pid == -1) {
78 err = got_error_from_errno("fork");
79 goto done;
80 } else if (pid == 0) {
81 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GOTCONFIG,
82 gotconfig_path);
83 /* not reached */
84 }
86 if (close(imsg_fds[1]) == -1) {
87 err = got_error_from_errno("close");
88 goto done;
89 }
90 imsg_fds[1] = -1;
91 imsg_init(ibuf, imsg_fds[0]);
93 err = got_privsep_send_gotconfig_parse_req(ibuf, fd);
94 if (err)
95 goto done;
96 fd = -1;
98 err = got_privsep_send_gotconfig_author_req(ibuf);
99 if (err)
100 goto done;
102 err = got_privsep_recv_gotconfig_str(&(*conf)->author, ibuf);
103 if (err)
104 goto done;
106 err = got_privsep_send_gotconfig_allowed_signers_req(ibuf);
107 if (err)
108 goto done;
110 err = got_privsep_recv_gotconfig_str(&(*conf)->allowed_signers_file,
111 ibuf);
112 if (err)
113 goto done;
115 err = got_privsep_send_gotconfig_revoked_signers_req(ibuf);
116 if (err)
117 goto done;
119 err = got_privsep_recv_gotconfig_str(&(*conf)->revoked_signers_file,
120 ibuf);
121 if (err)
122 goto done;
124 err = got_privsep_send_gotconfig_signer_id_req(ibuf);
125 if (err)
126 goto done;
128 err = got_privsep_recv_gotconfig_str(&(*conf)->signer_id, ibuf);
129 if (err)
130 goto done;
132 err = got_privsep_send_gotconfig_remotes_req(ibuf);
133 if (err)
134 goto done;
136 err = got_privsep_recv_gotconfig_remotes(&(*conf)->remotes,
137 &(*conf)->nremotes, ibuf);
138 if (err)
139 goto done;
141 err = got_privsep_send_stop(imsg_fds[0]);
142 child_err = got_privsep_wait_for_child(pid);
143 if (child_err && err == NULL)
144 err = child_err;
145 done:
146 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
147 err = got_error_from_errno("close");
148 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
149 err = got_error_from_errno("close");
150 if (fd != -1 && close(fd) == -1 && err == NULL)
151 err = got_error_from_errno2("close", gotconfig_path);
152 if (err) {
153 got_gotconfig_free(*conf);
154 *conf = NULL;
156 free(ibuf);
157 return err;