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 400c1baa 2022-10-13 thomas struct imsgbuf *ibuf;
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 400c1baa 2022-10-13 thomas return got_error_from_errno2("open", gotconfig_path);
60 400c1baa 2022-10-13 thomas }
61 400c1baa 2022-10-13 thomas
62 400c1baa 2022-10-13 thomas ibuf = calloc(1, sizeof(*ibuf));
63 400c1baa 2022-10-13 thomas if (ibuf == NULL) {
64 400c1baa 2022-10-13 thomas err = got_error_from_errno("calloc");
65 400c1baa 2022-10-13 thomas goto done;
66 400c1baa 2022-10-13 thomas }
67 400c1baa 2022-10-13 thomas
68 400c1baa 2022-10-13 thomas if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
69 400c1baa 2022-10-13 thomas err = got_error_from_errno("socketpair");
70 400c1baa 2022-10-13 thomas goto done;
71 400c1baa 2022-10-13 thomas }
72 400c1baa 2022-10-13 thomas
73 400c1baa 2022-10-13 thomas pid = fork();
74 400c1baa 2022-10-13 thomas if (pid == -1) {
75 400c1baa 2022-10-13 thomas err = got_error_from_errno("fork");
76 400c1baa 2022-10-13 thomas goto done;
77 400c1baa 2022-10-13 thomas } else if (pid == 0) {
78 400c1baa 2022-10-13 thomas got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GOTCONFIG,
79 400c1baa 2022-10-13 thomas gotconfig_path);
80 400c1baa 2022-10-13 thomas /* not reached */
81 400c1baa 2022-10-13 thomas }
82 400c1baa 2022-10-13 thomas
83 400c1baa 2022-10-13 thomas if (close(imsg_fds[1]) == -1) {
84 400c1baa 2022-10-13 thomas err = got_error_from_errno("close");
85 400c1baa 2022-10-13 thomas goto done;
86 400c1baa 2022-10-13 thomas }
87 400c1baa 2022-10-13 thomas imsg_fds[1] = -1;
88 400c1baa 2022-10-13 thomas imsg_init(ibuf, imsg_fds[0]);
89 400c1baa 2022-10-13 thomas
90 400c1baa 2022-10-13 thomas err = got_privsep_send_gotconfig_parse_req(ibuf, fd);
91 400c1baa 2022-10-13 thomas if (err)
92 400c1baa 2022-10-13 thomas goto done;
93 400c1baa 2022-10-13 thomas fd = -1;
94 400c1baa 2022-10-13 thomas
95 400c1baa 2022-10-13 thomas err = got_privsep_send_gotconfig_author_req(ibuf);
96 400c1baa 2022-10-13 thomas if (err)
97 400c1baa 2022-10-13 thomas goto done;
98 400c1baa 2022-10-13 thomas
99 400c1baa 2022-10-13 thomas err = got_privsep_recv_gotconfig_str(&(*conf)->author, ibuf);
100 400c1baa 2022-10-13 thomas if (err)
101 400c1baa 2022-10-13 thomas goto done;
102 400c1baa 2022-10-13 thomas
103 400c1baa 2022-10-13 thomas err = got_privsep_send_gotconfig_allowed_signers_req(ibuf);
104 400c1baa 2022-10-13 thomas if (err)
105 400c1baa 2022-10-13 thomas goto done;
106 400c1baa 2022-10-13 thomas
107 400c1baa 2022-10-13 thomas err = got_privsep_recv_gotconfig_str(&(*conf)->allowed_signers_file,
108 400c1baa 2022-10-13 thomas ibuf);
109 400c1baa 2022-10-13 thomas if (err)
110 400c1baa 2022-10-13 thomas goto done;
111 400c1baa 2022-10-13 thomas
112 400c1baa 2022-10-13 thomas err = got_privsep_send_gotconfig_revoked_signers_req(ibuf);
113 400c1baa 2022-10-13 thomas if (err)
114 400c1baa 2022-10-13 thomas goto done;
115 400c1baa 2022-10-13 thomas
116 400c1baa 2022-10-13 thomas err = got_privsep_recv_gotconfig_str(&(*conf)->revoked_signers_file,
117 400c1baa 2022-10-13 thomas ibuf);
118 400c1baa 2022-10-13 thomas if (err)
119 400c1baa 2022-10-13 thomas goto done;
120 400c1baa 2022-10-13 thomas
121 400c1baa 2022-10-13 thomas err = got_privsep_send_gotconfig_signer_id_req(ibuf);
122 400c1baa 2022-10-13 thomas if (err)
123 400c1baa 2022-10-13 thomas goto done;
124 400c1baa 2022-10-13 thomas
125 400c1baa 2022-10-13 thomas err = got_privsep_recv_gotconfig_str(&(*conf)->signer_id, ibuf);
126 400c1baa 2022-10-13 thomas if (err)
127 400c1baa 2022-10-13 thomas goto done;
128 400c1baa 2022-10-13 thomas
129 400c1baa 2022-10-13 thomas err = got_privsep_send_gotconfig_remotes_req(ibuf);
130 400c1baa 2022-10-13 thomas if (err)
131 400c1baa 2022-10-13 thomas goto done;
132 400c1baa 2022-10-13 thomas
133 400c1baa 2022-10-13 thomas err = got_privsep_recv_gotconfig_remotes(&(*conf)->remotes,
134 400c1baa 2022-10-13 thomas &(*conf)->nremotes, ibuf);
135 400c1baa 2022-10-13 thomas if (err)
136 400c1baa 2022-10-13 thomas goto done;
137 400c1baa 2022-10-13 thomas
138 400c1baa 2022-10-13 thomas err = got_privsep_send_stop(imsg_fds[0]);
139 400c1baa 2022-10-13 thomas child_err = got_privsep_wait_for_child(pid);
140 400c1baa 2022-10-13 thomas if (child_err && err == NULL)
141 400c1baa 2022-10-13 thomas err = child_err;
142 400c1baa 2022-10-13 thomas done:
143 400c1baa 2022-10-13 thomas if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
144 400c1baa 2022-10-13 thomas err = got_error_from_errno("close");
145 400c1baa 2022-10-13 thomas if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
146 400c1baa 2022-10-13 thomas err = got_error_from_errno("close");
147 400c1baa 2022-10-13 thomas if (fd != -1 && close(fd) == -1 && err == NULL)
148 400c1baa 2022-10-13 thomas err = got_error_from_errno2("close", gotconfig_path);
149 400c1baa 2022-10-13 thomas if (err) {
150 400c1baa 2022-10-13 thomas got_gotconfig_free(*conf);
151 400c1baa 2022-10-13 thomas *conf = NULL;
152 400c1baa 2022-10-13 thomas }
153 400c1baa 2022-10-13 thomas free(ibuf);
154 400c1baa 2022-10-13 thomas return err;
155 400c1baa 2022-10-13 thomas }