Blame


1 aba9c984 2019-09-08 stsp /*
2 aba9c984 2019-09-08 stsp * Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
3 aba9c984 2019-09-08 stsp *
4 aba9c984 2019-09-08 stsp * Permission to use, copy, modify, and distribute this software for any
5 aba9c984 2019-09-08 stsp * purpose with or without fee is hereby granted, provided that the above
6 aba9c984 2019-09-08 stsp * copyright notice and this permission notice appear in all copies.
7 aba9c984 2019-09-08 stsp *
8 aba9c984 2019-09-08 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 aba9c984 2019-09-08 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 aba9c984 2019-09-08 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 aba9c984 2019-09-08 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 aba9c984 2019-09-08 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 aba9c984 2019-09-08 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 aba9c984 2019-09-08 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 aba9c984 2019-09-08 stsp */
16 aba9c984 2019-09-08 stsp
17 aba9c984 2019-09-08 stsp #include <sys/types.h>
18 aba9c984 2019-09-08 stsp #include <sys/queue.h>
19 aba9c984 2019-09-08 stsp #include <sys/uio.h>
20 aba9c984 2019-09-08 stsp #include <sys/time.h>
21 aba9c984 2019-09-08 stsp #include <sys/syslimits.h>
22 aba9c984 2019-09-08 stsp
23 aba9c984 2019-09-08 stsp #include <stdint.h>
24 aba9c984 2019-09-08 stsp #include <imsg.h>
25 aba9c984 2019-09-08 stsp #include <limits.h>
26 aba9c984 2019-09-08 stsp #include <signal.h>
27 aba9c984 2019-09-08 stsp #include <stdio.h>
28 aba9c984 2019-09-08 stsp #include <stdlib.h>
29 aba9c984 2019-09-08 stsp #include <string.h>
30 aba9c984 2019-09-08 stsp #include <sha1.h>
31 aba9c984 2019-09-08 stsp #include <zlib.h>
32 aba9c984 2019-09-08 stsp
33 aba9c984 2019-09-08 stsp #include "got_error.h"
34 aba9c984 2019-09-08 stsp #include "got_object.h"
35 cd95becd 2019-11-29 stsp #include "got_repository.h"
36 aba9c984 2019-09-08 stsp
37 aba9c984 2019-09-08 stsp #include "got_lib_delta.h"
38 aba9c984 2019-09-08 stsp #include "got_lib_object.h"
39 aba9c984 2019-09-08 stsp #include "got_lib_privsep.h"
40 aba9c984 2019-09-08 stsp #include "got_lib_gitconfig.h"
41 aba9c984 2019-09-08 stsp
42 aba9c984 2019-09-08 stsp static volatile sig_atomic_t sigint_received;
43 aba9c984 2019-09-08 stsp
44 aba9c984 2019-09-08 stsp static void
45 aba9c984 2019-09-08 stsp catch_sigint(int signo)
46 aba9c984 2019-09-08 stsp {
47 aba9c984 2019-09-08 stsp sigint_received = 1;
48 aba9c984 2019-09-08 stsp }
49 aba9c984 2019-09-08 stsp
50 aba9c984 2019-09-08 stsp static const struct got_error *
51 aba9c984 2019-09-08 stsp gitconfig_num_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
52 aba9c984 2019-09-08 stsp char *section, char *tag, int def)
53 aba9c984 2019-09-08 stsp {
54 aba9c984 2019-09-08 stsp int value;
55 aba9c984 2019-09-08 stsp
56 aba9c984 2019-09-08 stsp if (gitconfig == NULL)
57 aba9c984 2019-09-08 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
58 aba9c984 2019-09-08 stsp
59 aba9c984 2019-09-08 stsp value = got_gitconfig_get_num(gitconfig, section, tag, def);
60 aba9c984 2019-09-08 stsp return got_privsep_send_gitconfig_int(ibuf, value);
61 aba9c984 2019-09-08 stsp }
62 aba9c984 2019-09-08 stsp
63 aba9c984 2019-09-08 stsp static const struct got_error *
64 aba9c984 2019-09-08 stsp gitconfig_str_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig,
65 aba9c984 2019-09-08 stsp char *section, char *tag)
66 aba9c984 2019-09-08 stsp {
67 aba9c984 2019-09-08 stsp char *value;
68 aba9c984 2019-09-08 stsp
69 aba9c984 2019-09-08 stsp if (gitconfig == NULL)
70 aba9c984 2019-09-08 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
71 aba9c984 2019-09-08 stsp
72 aba9c984 2019-09-08 stsp value = got_gitconfig_get_str(gitconfig, section, tag);
73 aba9c984 2019-09-08 stsp return got_privsep_send_gitconfig_str(ibuf, value);
74 aba9c984 2019-09-08 stsp }
75 aba9c984 2019-09-08 stsp
76 cd95becd 2019-11-29 stsp static const struct got_error *
77 cd95becd 2019-11-29 stsp gitconfig_remotes_request(struct imsgbuf *ibuf, struct got_gitconfig *gitconfig)
78 cd95becd 2019-11-29 stsp {
79 cd95becd 2019-11-29 stsp const struct got_error *err = NULL;
80 cd95becd 2019-11-29 stsp struct got_gitconfig_list *sections;
81 cd95becd 2019-11-29 stsp struct got_gitconfig_list_node *node;
82 cd95becd 2019-11-29 stsp struct got_remote_repo *remotes = NULL;
83 cd95becd 2019-11-29 stsp int nremotes = 0, i;
84 cd95becd 2019-11-29 stsp
85 cd95becd 2019-11-29 stsp if (gitconfig == NULL)
86 cd95becd 2019-11-29 stsp return got_error(GOT_ERR_PRIVSEP_MSG);
87 cd95becd 2019-11-29 stsp
88 cd95becd 2019-11-29 stsp err = got_gitconfig_get_section_list(&sections, gitconfig);
89 cd95becd 2019-11-29 stsp if (err)
90 cd95becd 2019-11-29 stsp return err;
91 cd95becd 2019-11-29 stsp
92 cd95becd 2019-11-29 stsp TAILQ_FOREACH(node, &sections->fields, link) {
93 cd95becd 2019-11-29 stsp if (strncasecmp("remote \"", node->field, 8) != 0)
94 cd95becd 2019-11-29 stsp continue;
95 cd95becd 2019-11-29 stsp nremotes++;
96 cd95becd 2019-11-29 stsp }
97 cd95becd 2019-11-29 stsp
98 cd95becd 2019-11-29 stsp if (nremotes == 0) {
99 cd95becd 2019-11-29 stsp err = got_privsep_send_gitconfig_remotes(ibuf, NULL, 0);
100 cd95becd 2019-11-29 stsp goto done;
101 cd95becd 2019-11-29 stsp }
102 cd95becd 2019-11-29 stsp
103 cd95becd 2019-11-29 stsp remotes = recallocarray(NULL, 0, nremotes, sizeof(*remotes));
104 cd95becd 2019-11-29 stsp if (remotes == NULL) {
105 cd95becd 2019-11-29 stsp err = got_error_from_errno("recallocarray");
106 cd95becd 2019-11-29 stsp goto done;
107 cd95becd 2019-11-29 stsp }
108 cd95becd 2019-11-29 stsp
109 cd95becd 2019-11-29 stsp i = 0;
110 cd95becd 2019-11-29 stsp TAILQ_FOREACH(node, &sections->fields, link) {
111 cd95becd 2019-11-29 stsp char *name, *end;
112 cd95becd 2019-11-29 stsp
113 cd95becd 2019-11-29 stsp if (strncasecmp("remote \"", node->field, 8) != 0)
114 cd95becd 2019-11-29 stsp continue;
115 cd95becd 2019-11-29 stsp
116 cd95becd 2019-11-29 stsp name = strdup(node->field + 8);
117 cd95becd 2019-11-29 stsp if (name == NULL) {
118 cd95becd 2019-11-29 stsp err = got_error_from_errno("strdup");
119 cd95becd 2019-11-29 stsp goto done;
120 cd95becd 2019-11-29 stsp }
121 cd95becd 2019-11-29 stsp end = strrchr(name, '"');
122 cd95becd 2019-11-29 stsp if (end)
123 cd95becd 2019-11-29 stsp *end = '\0';
124 cd95becd 2019-11-29 stsp remotes[i].name = name;
125 cd95becd 2019-11-29 stsp
126 cd95becd 2019-11-29 stsp remotes[i].url = got_gitconfig_get_str(gitconfig,
127 cd95becd 2019-11-29 stsp node->field, "url");
128 cd95becd 2019-11-29 stsp if (remotes[i].url == NULL) {
129 cd95becd 2019-11-29 stsp err = got_error(GOT_ERR_GITCONFIG_SYNTAX);
130 cd95becd 2019-11-29 stsp goto done;
131 cd95becd 2019-11-29 stsp }
132 cd95becd 2019-11-29 stsp
133 cd95becd 2019-11-29 stsp i++;
134 cd95becd 2019-11-29 stsp }
135 cd95becd 2019-11-29 stsp
136 cd95becd 2019-11-29 stsp err = got_privsep_send_gitconfig_remotes(ibuf, remotes, nremotes);
137 cd95becd 2019-11-29 stsp done:
138 cd95becd 2019-11-29 stsp for (i = 0; i < nremotes; i++)
139 cd95becd 2019-11-29 stsp free(remotes[i].name);
140 cd95becd 2019-11-29 stsp free(remotes);
141 cd95becd 2019-11-29 stsp got_gitconfig_free_list(sections);
142 cd95becd 2019-11-29 stsp return err;
143 cd95becd 2019-11-29 stsp }
144 cd95becd 2019-11-29 stsp
145 aba9c984 2019-09-08 stsp int
146 aba9c984 2019-09-08 stsp main(int argc, char *argv[])
147 aba9c984 2019-09-08 stsp {
148 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
149 aba9c984 2019-09-08 stsp struct imsgbuf ibuf;
150 aba9c984 2019-09-08 stsp size_t datalen;
151 aba9c984 2019-09-08 stsp struct got_gitconfig *gitconfig = NULL;
152 aba9c984 2019-09-08 stsp #if 0
153 aba9c984 2019-09-08 stsp static int attached;
154 aba9c984 2019-09-08 stsp
155 aba9c984 2019-09-08 stsp while (!attached)
156 aba9c984 2019-09-08 stsp sleep(1);
157 aba9c984 2019-09-08 stsp #endif
158 aba9c984 2019-09-08 stsp signal(SIGINT, catch_sigint);
159 aba9c984 2019-09-08 stsp
160 aba9c984 2019-09-08 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
161 aba9c984 2019-09-08 stsp
162 aba9c984 2019-09-08 stsp #ifndef PROFILE
163 aba9c984 2019-09-08 stsp /* revoke access to most system calls */
164 aba9c984 2019-09-08 stsp if (pledge("stdio recvfd", NULL) == -1) {
165 aba9c984 2019-09-08 stsp err = got_error_from_errno("pledge");
166 aba9c984 2019-09-08 stsp got_privsep_send_error(&ibuf, err);
167 aba9c984 2019-09-08 stsp return 1;
168 aba9c984 2019-09-08 stsp }
169 aba9c984 2019-09-08 stsp #endif
170 aba9c984 2019-09-08 stsp
171 aba9c984 2019-09-08 stsp for (;;) {
172 aba9c984 2019-09-08 stsp struct imsg imsg;
173 aba9c984 2019-09-08 stsp
174 aba9c984 2019-09-08 stsp memset(&imsg, 0, sizeof(imsg));
175 aba9c984 2019-09-08 stsp imsg.fd = -1;
176 aba9c984 2019-09-08 stsp
177 aba9c984 2019-09-08 stsp if (sigint_received) {
178 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_CANCELLED);
179 aba9c984 2019-09-08 stsp break;
180 aba9c984 2019-09-08 stsp }
181 aba9c984 2019-09-08 stsp
182 aba9c984 2019-09-08 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
183 aba9c984 2019-09-08 stsp if (err) {
184 aba9c984 2019-09-08 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
185 aba9c984 2019-09-08 stsp err = NULL;
186 aba9c984 2019-09-08 stsp break;
187 aba9c984 2019-09-08 stsp }
188 aba9c984 2019-09-08 stsp
189 aba9c984 2019-09-08 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
190 aba9c984 2019-09-08 stsp break;
191 aba9c984 2019-09-08 stsp
192 aba9c984 2019-09-08 stsp switch (imsg.hdr.type) {
193 aba9c984 2019-09-08 stsp case GOT_IMSG_GITCONFIG_PARSE_REQUEST:
194 aba9c984 2019-09-08 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
195 aba9c984 2019-09-08 stsp if (datalen != 0) {
196 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
197 aba9c984 2019-09-08 stsp break;
198 aba9c984 2019-09-08 stsp }
199 aba9c984 2019-09-08 stsp if (imsg.fd == -1){
200 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
201 aba9c984 2019-09-08 stsp break;
202 aba9c984 2019-09-08 stsp }
203 aba9c984 2019-09-08 stsp
204 aba9c984 2019-09-08 stsp if (gitconfig)
205 aba9c984 2019-09-08 stsp got_gitconfig_close(gitconfig);
206 aba9c984 2019-09-08 stsp err = got_gitconfig_open(&gitconfig, imsg.fd);
207 aba9c984 2019-09-08 stsp break;
208 aba9c984 2019-09-08 stsp case GOT_IMSG_GITCONFIG_REPOSITORY_FORMAT_VERSION_REQUEST:
209 aba9c984 2019-09-08 stsp err = gitconfig_num_request(&ibuf, gitconfig, "core",
210 aba9c984 2019-09-08 stsp "repositoryformatversion", 0);
211 aba9c984 2019-09-08 stsp break;
212 aba9c984 2019-09-08 stsp case GOT_IMSG_GITCONFIG_AUTHOR_NAME_REQUEST:
213 aba9c984 2019-09-08 stsp err = gitconfig_str_request(&ibuf, gitconfig, "user",
214 aba9c984 2019-09-08 stsp "name");
215 aba9c984 2019-09-08 stsp break;
216 aba9c984 2019-09-08 stsp case GOT_IMSG_GITCONFIG_AUTHOR_EMAIL_REQUEST:
217 aba9c984 2019-09-08 stsp err = gitconfig_str_request(&ibuf, gitconfig, "user",
218 aba9c984 2019-09-08 stsp "email");
219 aba9c984 2019-09-08 stsp break;
220 cd95becd 2019-11-29 stsp case GOT_IMSG_GITCONFIG_REMOTES_REQUEST:
221 cd95becd 2019-11-29 stsp err = gitconfig_remotes_request(&ibuf, gitconfig);
222 cd95becd 2019-11-29 stsp break;
223 aba9c984 2019-09-08 stsp default:
224 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
225 aba9c984 2019-09-08 stsp break;
226 aba9c984 2019-09-08 stsp }
227 aba9c984 2019-09-08 stsp
228 aba9c984 2019-09-08 stsp if (imsg.fd != -1) {
229 aba9c984 2019-09-08 stsp if (close(imsg.fd) == -1 && err == NULL)
230 aba9c984 2019-09-08 stsp err = got_error_from_errno("close");
231 aba9c984 2019-09-08 stsp }
232 aba9c984 2019-09-08 stsp
233 aba9c984 2019-09-08 stsp imsg_free(&imsg);
234 aba9c984 2019-09-08 stsp if (err)
235 aba9c984 2019-09-08 stsp break;
236 aba9c984 2019-09-08 stsp }
237 aba9c984 2019-09-08 stsp
238 aba9c984 2019-09-08 stsp imsg_clear(&ibuf);
239 aba9c984 2019-09-08 stsp if (err) {
240 aba9c984 2019-09-08 stsp if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
241 aba9c984 2019-09-08 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
242 aba9c984 2019-09-08 stsp got_privsep_send_error(&ibuf, err);
243 aba9c984 2019-09-08 stsp }
244 aba9c984 2019-09-08 stsp }
245 aba9c984 2019-09-08 stsp if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
246 aba9c984 2019-09-08 stsp err = got_error_from_errno("close");
247 aba9c984 2019-09-08 stsp return err ? 1 : 0;
248 aba9c984 2019-09-08 stsp }