Blame


1 400c1baa 2022-10-13 thomas /*
2 400c1baa 2022-10-13 thomas * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
3 400c1baa 2022-10-13 thomas *
4 400c1baa 2022-10-13 thomas * Permission to use, copy, modify, and distribute this software for any
5 400c1baa 2022-10-13 thomas * purpose with or without fee is hereby granted, provided that the above
6 400c1baa 2022-10-13 thomas * copyright notice and this permission notice appear in all copies.
7 400c1baa 2022-10-13 thomas *
8 400c1baa 2022-10-13 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 400c1baa 2022-10-13 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 400c1baa 2022-10-13 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 400c1baa 2022-10-13 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 400c1baa 2022-10-13 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 400c1baa 2022-10-13 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 400c1baa 2022-10-13 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 400c1baa 2022-10-13 thomas */
16 400c1baa 2022-10-13 thomas
17 400c1baa 2022-10-13 thomas #include <sys/types.h>
18 400c1baa 2022-10-13 thomas #include <sys/queue.h>
19 400c1baa 2022-10-13 thomas #include <sys/uio.h>
20 400c1baa 2022-10-13 thomas #include <sys/socket.h>
21 400c1baa 2022-10-13 thomas
22 400c1baa 2022-10-13 thomas #include <unistd.h>
23 400c1baa 2022-10-13 thomas #include <fcntl.h>
24 400c1baa 2022-10-13 thomas #include <errno.h>
25 400c1baa 2022-10-13 thomas #include <stdlib.h>
26 400c1baa 2022-10-13 thomas #include <stdio.h>
27 400c1baa 2022-10-13 thomas #include <stdint.h>
28 400c1baa 2022-10-13 thomas #include <imsg.h>
29 400c1baa 2022-10-13 thomas #include <limits.h>
30 400c1baa 2022-10-13 thomas
31 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
32 400c1baa 2022-10-13 thomas #include "got_error.h"
33 400c1baa 2022-10-13 thomas #include "got_object.h"
34 400c1baa 2022-10-13 thomas #include "got_repository.h"
35 400c1baa 2022-10-13 thomas
36 400c1baa 2022-10-13 thomas #include "got_lib_delta.h"
37 400c1baa 2022-10-13 thomas #include "got_lib_object.h"
38 400c1baa 2022-10-13 thomas #include "got_lib_privsep.h"
39 400c1baa 2022-10-13 thomas #include "got_lib_gotconfig.h"
40 400c1baa 2022-10-13 thomas
41 400c1baa 2022-10-13 thomas #include "got_gotconfig.h"
42 400c1baa 2022-10-13 thomas
43 400c1baa 2022-10-13 thomas const struct got_error *
44 400c1baa 2022-10-13 thomas got_gotconfig_read(struct got_gotconfig **conf, const char *gotconfig_path)
45 400c1baa 2022-10-13 thomas {
46 400c1baa 2022-10-13 thomas const struct got_error *err = NULL, *child_err = NULL;
47 400c1baa 2022-10-13 thomas int fd = -1;
48 400c1baa 2022-10-13 thomas int imsg_fds[2] = { -1, -1 };
49 400c1baa 2022-10-13 thomas pid_t pid;
50 2aef7b23 2022-10-27 thomas struct imsgbuf *ibuf = NULL;
51 400c1baa 2022-10-13 thomas
52 400c1baa 2022-10-13 thomas *conf = calloc(1, sizeof(**conf));
53 400c1baa 2022-10-13 thomas if (*conf == NULL)
54 400c1baa 2022-10-13 thomas return got_error_from_errno("calloc");
55 400c1baa 2022-10-13 thomas
56 400c1baa 2022-10-13 thomas fd = open(gotconfig_path, O_RDONLY | O_CLOEXEC);
57 400c1baa 2022-10-13 thomas if (fd == -1) {
58 400c1baa 2022-10-13 thomas if (errno == ENOENT)
59 400c1baa 2022-10-13 thomas return NULL;
60 2aef7b23 2022-10-27 thomas err = got_error_from_errno2("open", gotconfig_path);
61 2aef7b23 2022-10-27 thomas goto done;
62 400c1baa 2022-10-13 thomas }
63 400c1baa 2022-10-13 thomas
64 400c1baa 2022-10-13 thomas ibuf = calloc(1, sizeof(*ibuf));
65 400c1baa 2022-10-13 thomas if (ibuf == NULL) {
66 400c1baa 2022-10-13 thomas err = got_error_from_errno("calloc");
67 400c1baa 2022-10-13 thomas goto done;
68 400c1baa 2022-10-13 thomas }
69 400c1baa 2022-10-13 thomas
70 400c1baa 2022-10-13 thomas if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
71 400c1baa 2022-10-13 thomas err = got_error_from_errno("socketpair");
72 400c1baa 2022-10-13 thomas goto done;
73 400c1baa 2022-10-13 thomas }
74 400c1baa 2022-10-13 thomas
75 400c1baa 2022-10-13 thomas pid = fork();
76 400c1baa 2022-10-13 thomas if (pid == -1) {
77 400c1baa 2022-10-13 thomas err = got_error_from_errno("fork");
78 400c1baa 2022-10-13 thomas goto done;
79 400c1baa 2022-10-13 thomas } else if (pid == 0) {
80 400c1baa 2022-10-13 thomas got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GOTCONFIG,
81 400c1baa 2022-10-13 thomas gotconfig_path);
82 400c1baa 2022-10-13 thomas /* not reached */
83 400c1baa 2022-10-13 thomas }
84 400c1baa 2022-10-13 thomas
85 400c1baa 2022-10-13 thomas if (close(imsg_fds[1]) == -1) {
86 400c1baa 2022-10-13 thomas err = got_error_from_errno("close");
87 400c1baa 2022-10-13 thomas goto done;
88 400c1baa 2022-10-13 thomas }
89 400c1baa 2022-10-13 thomas imsg_fds[1] = -1;
90 400c1baa 2022-10-13 thomas imsg_init(ibuf, imsg_fds[0]);
91 400c1baa 2022-10-13 thomas
92 400c1baa 2022-10-13 thomas err = got_privsep_send_gotconfig_parse_req(ibuf, fd);
93 400c1baa 2022-10-13 thomas if (err)
94 400c1baa 2022-10-13 thomas goto done;
95 400c1baa 2022-10-13 thomas fd = -1;
96 400c1baa 2022-10-13 thomas
97 400c1baa 2022-10-13 thomas err = got_privsep_send_gotconfig_author_req(ibuf);
98 400c1baa 2022-10-13 thomas if (err)
99 400c1baa 2022-10-13 thomas goto done;
100 400c1baa 2022-10-13 thomas
101 400c1baa 2022-10-13 thomas err = got_privsep_recv_gotconfig_str(&(*conf)->author, ibuf);
102 400c1baa 2022-10-13 thomas if (err)
103 400c1baa 2022-10-13 thomas goto done;
104 400c1baa 2022-10-13 thomas
105 400c1baa 2022-10-13 thomas err = got_privsep_send_gotconfig_allowed_signers_req(ibuf);
106 400c1baa 2022-10-13 thomas if (err)
107 400c1baa 2022-10-13 thomas goto done;
108 400c1baa 2022-10-13 thomas
109 400c1baa 2022-10-13 thomas err = got_privsep_recv_gotconfig_str(&(*conf)->allowed_signers_file,
110 400c1baa 2022-10-13 thomas ibuf);
111 400c1baa 2022-10-13 thomas if (err)
112 400c1baa 2022-10-13 thomas goto done;
113 400c1baa 2022-10-13 thomas
114 400c1baa 2022-10-13 thomas err = got_privsep_send_gotconfig_revoked_signers_req(ibuf);
115 400c1baa 2022-10-13 thomas if (err)
116 400c1baa 2022-10-13 thomas goto done;
117 400c1baa 2022-10-13 thomas
118 400c1baa 2022-10-13 thomas err = got_privsep_recv_gotconfig_str(&(*conf)->revoked_signers_file,
119 400c1baa 2022-10-13 thomas ibuf);
120 400c1baa 2022-10-13 thomas if (err)
121 400c1baa 2022-10-13 thomas goto done;
122 400c1baa 2022-10-13 thomas
123 400c1baa 2022-10-13 thomas err = got_privsep_send_gotconfig_signer_id_req(ibuf);
124 400c1baa 2022-10-13 thomas if (err)
125 400c1baa 2022-10-13 thomas goto done;
126 400c1baa 2022-10-13 thomas
127 400c1baa 2022-10-13 thomas err = got_privsep_recv_gotconfig_str(&(*conf)->signer_id, ibuf);
128 400c1baa 2022-10-13 thomas if (err)
129 400c1baa 2022-10-13 thomas goto done;
130 400c1baa 2022-10-13 thomas
131 400c1baa 2022-10-13 thomas err = got_privsep_send_gotconfig_remotes_req(ibuf);
132 400c1baa 2022-10-13 thomas if (err)
133 400c1baa 2022-10-13 thomas goto done;
134 400c1baa 2022-10-13 thomas
135 400c1baa 2022-10-13 thomas err = got_privsep_recv_gotconfig_remotes(&(*conf)->remotes,
136 400c1baa 2022-10-13 thomas &(*conf)->nremotes, ibuf);
137 400c1baa 2022-10-13 thomas if (err)
138 400c1baa 2022-10-13 thomas goto done;
139 400c1baa 2022-10-13 thomas
140 400c1baa 2022-10-13 thomas err = got_privsep_send_stop(imsg_fds[0]);
141 400c1baa 2022-10-13 thomas child_err = got_privsep_wait_for_child(pid);
142 400c1baa 2022-10-13 thomas if (child_err && err == NULL)
143 400c1baa 2022-10-13 thomas err = child_err;
144 400c1baa 2022-10-13 thomas done:
145 400c1baa 2022-10-13 thomas if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
146 400c1baa 2022-10-13 thomas err = got_error_from_errno("close");
147 400c1baa 2022-10-13 thomas if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
148 400c1baa 2022-10-13 thomas err = got_error_from_errno("close");
149 400c1baa 2022-10-13 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
150 400c1baa 2022-10-13 thomas err = got_error_from_errno2("close", gotconfig_path);
151 400c1baa 2022-10-13 thomas if (err) {
152 400c1baa 2022-10-13 thomas got_gotconfig_free(*conf);
153 400c1baa 2022-10-13 thomas *conf = NULL;
154 400c1baa 2022-10-13 thomas }
155 400c1baa 2022-10-13 thomas free(ibuf);
156 400c1baa 2022-10-13 thomas return err;
157 400c1baa 2022-10-13 thomas }