fix confusing error message from 'got commit' upon uncommitable paths
[got-portable.git] / lib / read_gotconfig_privsep.c
blob5ba07c04fa0d8b21bddc184dae05a45bfac49cf3
1 /*
2 * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
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.
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.
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 <limits.h>
31 #include "got_compat.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)
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 = NULL;
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 err = got_error_from_errno2("open", gotconfig_path);
61 goto done;
64 ibuf = calloc(1, sizeof(*ibuf));
65 if (ibuf == NULL) {
66 err = got_error_from_errno("calloc");
67 goto done;
70 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
71 err = got_error_from_errno("socketpair");
72 goto done;
75 pid = fork();
76 if (pid == -1) {
77 err = got_error_from_errno("fork");
78 goto done;
79 } else if (pid == 0) {
80 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GOTCONFIG,
81 gotconfig_path);
82 /* not reached */
85 if (close(imsg_fds[1]) == -1) {
86 err = got_error_from_errno("close");
87 goto done;
89 imsg_fds[1] = -1;
90 imsg_init(ibuf, imsg_fds[0]);
92 err = got_privsep_send_gotconfig_parse_req(ibuf, fd);
93 if (err)
94 goto done;
95 fd = -1;
97 err = got_privsep_send_gotconfig_author_req(ibuf);
98 if (err)
99 goto done;
101 err = got_privsep_recv_gotconfig_str(&(*conf)->author, ibuf);
102 if (err)
103 goto done;
105 err = got_privsep_send_gotconfig_allowed_signers_req(ibuf);
106 if (err)
107 goto done;
109 err = got_privsep_recv_gotconfig_str(&(*conf)->allowed_signers_file,
110 ibuf);
111 if (err)
112 goto done;
114 err = got_privsep_send_gotconfig_revoked_signers_req(ibuf);
115 if (err)
116 goto done;
118 err = got_privsep_recv_gotconfig_str(&(*conf)->revoked_signers_file,
119 ibuf);
120 if (err)
121 goto done;
123 err = got_privsep_send_gotconfig_signer_id_req(ibuf);
124 if (err)
125 goto done;
127 err = got_privsep_recv_gotconfig_str(&(*conf)->signer_id, ibuf);
128 if (err)
129 goto done;
131 err = got_privsep_send_gotconfig_remotes_req(ibuf);
132 if (err)
133 goto done;
135 err = got_privsep_recv_gotconfig_remotes(&(*conf)->remotes,
136 &(*conf)->nremotes, ibuf);
137 if (err)
138 goto done;
140 err = got_privsep_send_stop(imsg_fds[0]);
141 child_err = got_privsep_wait_for_child(pid);
142 if (child_err && err == NULL)
143 err = child_err;
144 done:
145 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
146 err = got_error_from_errno("close");
147 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
148 err = got_error_from_errno("close");
149 if (fd != -1 && close(fd) == -1 && err == NULL)
150 err = got_error_from_errno2("close", gotconfig_path);
151 if (err) {
152 got_gotconfig_free(*conf);
153 *conf = NULL;
155 free(ibuf);
156 return err;