style(9) only; no functional changes
[got-portable.git] / lib / read_gitconfig_privsep.c
blob5812d28e8ba566715c752902f20c0ffd4216b56c
1 /*
2 * Copyright (c) 2019, 2022 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/time.h>
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <sys/queue.h>
21 #include <sys/uio.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <limits.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdint.h>
29 #include <string.h>
30 #include <imsg.h>
31 #include <unistd.h>
33 #include "got_compat.h"
34 #include "got_error.h"
35 #include "got_object.h"
36 #include "got_repository.h"
37 #include "got_path.h"
39 #include "got_lib_delta.h"
40 #include "got_lib_hash.h"
41 #include "got_lib_object.h"
42 #include "got_lib_object_cache.h"
43 #include "got_lib_privsep.h"
44 #include "got_lib_pack.h"
45 #include "got_lib_repository.h"
47 const struct got_error *
48 got_repo_read_gitconfig(int *gitconfig_repository_format_version,
49 char **gitconfig_author_name, char **gitconfig_author_email,
50 struct got_remote_repo **remotes, int *nremotes,
51 char **gitconfig_owner, char ***extnames, char ***extvals,
52 int *nextensions, const char *gitconfig_path)
54 const struct got_error *err = NULL, *child_err = NULL;
55 int fd = -1;
56 int imsg_fds[2] = { -1, -1 };
57 pid_t pid;
58 struct imsgbuf ibuf;
60 memset(&ibuf, 0, sizeof(ibuf));
62 *gitconfig_repository_format_version = 0;
63 if (extnames)
64 *extnames = NULL;
65 if (extvals)
66 *extvals = NULL;
67 if (nextensions)
68 *nextensions = 0;
69 *gitconfig_author_name = NULL;
70 *gitconfig_author_email = NULL;
71 if (remotes)
72 *remotes = NULL;
73 if (nremotes)
74 *nremotes = 0;
75 if (gitconfig_owner)
76 *gitconfig_owner = NULL;
78 fd = open(gitconfig_path, O_RDONLY | O_CLOEXEC);
79 if (fd == -1) {
80 if (errno == ENOENT)
81 return NULL;
82 return got_error_from_errno2("open", gitconfig_path);
85 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
86 err = got_error_from_errno("socketpair");
87 goto done;
90 pid = fork();
91 if (pid == -1) {
92 err = got_error_from_errno("fork");
93 goto done;
94 } else if (pid == 0) {
95 got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG,
96 gitconfig_path);
97 /* not reached */
100 if (close(imsg_fds[1]) == -1) {
101 err = got_error_from_errno("close");
102 goto wait;
104 imsg_fds[1] = -1;
105 if (imsgbuf_init(&ibuf, imsg_fds[0]) == -1) {
106 err = got_error_from_errno("imsgbuf_init");
107 goto wait;
109 imsgbuf_allow_fdpass(&ibuf);
111 err = got_privsep_send_gitconfig_parse_req(&ibuf, fd);
112 if (err)
113 goto wait;
114 fd = -1;
116 err = got_privsep_send_gitconfig_repository_format_version_req(&ibuf);
117 if (err)
118 goto wait;
120 err = got_privsep_recv_gitconfig_int(
121 gitconfig_repository_format_version, &ibuf);
122 if (err)
123 goto wait;
125 if (extnames && extvals && nextensions) {
126 err = got_privsep_send_gitconfig_repository_extensions_req(
127 &ibuf);
128 if (err)
129 goto wait;
130 err = got_privsep_recv_gitconfig_int(nextensions, &ibuf);
131 if (err)
132 goto wait;
133 if (*nextensions > 0) {
134 int i;
135 *extnames = calloc(*nextensions, sizeof(char *));
136 if (*extnames == NULL) {
137 err = got_error_from_errno("calloc");
138 goto wait;
140 *extvals = calloc(*nextensions, sizeof(char *));
141 if (*extvals == NULL) {
142 err = got_error_from_errno("calloc");
143 goto wait;
145 for (i = 0; i < *nextensions; i++) {
146 char *ext, *val;
147 err = got_privsep_recv_gitconfig_pair(&ext,
148 &val, &ibuf);
149 if (err)
150 goto wait;
151 (*extnames)[i] = ext;
152 (*extvals)[i] = val;
157 err = got_privsep_send_gitconfig_author_name_req(&ibuf);
158 if (err)
159 goto wait;
161 err = got_privsep_recv_gitconfig_str(gitconfig_author_name, &ibuf);
162 if (err)
163 goto wait;
165 err = got_privsep_send_gitconfig_author_email_req(&ibuf);
166 if (err)
167 goto wait;
169 err = got_privsep_recv_gitconfig_str(gitconfig_author_email, &ibuf);
170 if (err)
171 goto wait;
173 if (remotes && nremotes) {
174 err = got_privsep_send_gitconfig_remotes_req(&ibuf);
175 if (err)
176 goto wait;
178 err = got_privsep_recv_gitconfig_remotes(remotes,
179 nremotes, &ibuf);
180 if (err)
181 goto wait;
184 if (gitconfig_owner) {
185 err = got_privsep_send_gitconfig_owner_req(&ibuf);
186 if (err)
187 goto wait;
188 err = got_privsep_recv_gitconfig_str(gitconfig_owner, &ibuf);
189 if (err)
190 goto wait;
192 wait:
193 if (imsg_fds[0] != -1)
194 got_privsep_send_stop(imsg_fds[0]);
195 child_err = got_privsep_wait_for_child(pid);
196 if (child_err && err == NULL)
197 err = child_err;
198 done:
199 if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL)
200 err = got_error_from_errno("close");
201 if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL)
202 err = got_error_from_errno("close");
203 if (fd != -1 && close(fd) == -1 && err == NULL)
204 err = got_error_from_errno2("close", gitconfig_path);
205 if (ibuf.w)
206 imsgbuf_clear(&ibuf);
207 return err;