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