Blame


1 d65a88a2 2021-09-05 stsp /*
2 d65a88a2 2021-09-05 stsp * Copyright (c) 2018, 2019 Ori Bernstein <ori@openbsd.org>
3 d65a88a2 2021-09-05 stsp * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
4 d65a88a2 2021-09-05 stsp *
5 d65a88a2 2021-09-05 stsp * Permission to use, copy, modify, and distribute this software for any
6 d65a88a2 2021-09-05 stsp * purpose with or without fee is hereby granted, provided that the above
7 d65a88a2 2021-09-05 stsp * copyright notice and this permission notice appear in all copies.
8 d65a88a2 2021-09-05 stsp *
9 d65a88a2 2021-09-05 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 d65a88a2 2021-09-05 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 d65a88a2 2021-09-05 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 d65a88a2 2021-09-05 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 d65a88a2 2021-09-05 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 d65a88a2 2021-09-05 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 d65a88a2 2021-09-05 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 d65a88a2 2021-09-05 stsp */
17 d65a88a2 2021-09-05 stsp
18 5e5da8c4 2021-09-05 stsp #include <sys/queue.h>
19 d65a88a2 2021-09-05 stsp #include <sys/types.h>
20 d65a88a2 2021-09-05 stsp #include <sys/socket.h>
21 ced242c2 2024-04-14 me #include <sys/uio.h>
22 d65a88a2 2021-09-05 stsp #include <netdb.h>
23 d65a88a2 2021-09-05 stsp
24 c10270f6 2021-09-06 naddy #include <assert.h>
25 d65a88a2 2021-09-05 stsp #include <err.h>
26 6cc8a118 2023-03-10 op #include <limits.h>
27 ced242c2 2024-04-14 me #include <sha1.h>
28 ced242c2 2024-04-14 me #include <stdint.h>
29 ced242c2 2024-04-14 me #include <limits.h>
30 d65a88a2 2021-09-05 stsp #include <stdio.h>
31 d65a88a2 2021-09-05 stsp #include <stdlib.h>
32 d65a88a2 2021-09-05 stsp #include <string.h>
33 d65a88a2 2021-09-05 stsp #include <unistd.h>
34 ced242c2 2024-04-14 me #include <imsg.h>
35 d65a88a2 2021-09-05 stsp
36 d65a88a2 2021-09-05 stsp #include "got_error.h"
37 5e5da8c4 2021-09-05 stsp #include "got_path.h"
38 ced242c2 2024-04-14 me #include "got_object.h"
39 d65a88a2 2021-09-05 stsp
40 d65a88a2 2021-09-05 stsp #include "got_lib_dial.h"
41 ced242c2 2024-04-14 me #include "got_lib_delta.h"
42 ced242c2 2024-04-14 me #include "got_lib_object.h"
43 ced242c2 2024-04-14 me #include "got_lib_privsep.h"
44 336075a4 2022-06-25 op #include "got_dial.h"
45 c10270f6 2021-09-06 naddy
46 c10270f6 2021-09-06 naddy #ifndef nitems
47 c10270f6 2021-09-06 naddy #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
48 c10270f6 2021-09-06 naddy #endif
49 d65a88a2 2021-09-05 stsp
50 d65a88a2 2021-09-05 stsp #ifndef ssizeof
51 d65a88a2 2021-09-05 stsp #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
52 d65a88a2 2021-09-05 stsp #endif
53 d65a88a2 2021-09-05 stsp
54 d65a88a2 2021-09-05 stsp #ifndef MIN
55 d65a88a2 2021-09-05 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
56 d65a88a2 2021-09-05 stsp #endif
57 d65a88a2 2021-09-05 stsp
58 d65a88a2 2021-09-05 stsp #ifndef GOT_DIAL_PATH_SSH
59 d65a88a2 2021-09-05 stsp #define GOT_DIAL_PATH_SSH "/usr/bin/ssh"
60 d65a88a2 2021-09-05 stsp #endif
61 d65a88a2 2021-09-05 stsp
62 d65a88a2 2021-09-05 stsp /* IANA assigned */
63 d65a88a2 2021-09-05 stsp #define GOT_DEFAULT_GIT_PORT 9418
64 d65a88a2 2021-09-05 stsp #define GOT_DEFAULT_GIT_PORT_STR "9418"
65 d65a88a2 2021-09-05 stsp
66 d65a88a2 2021-09-05 stsp const struct got_error *
67 d65a88a2 2021-09-05 stsp got_dial_apply_unveil(const char *proto)
68 d65a88a2 2021-09-05 stsp {
69 d65a88a2 2021-09-05 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
70 d65a88a2 2021-09-05 stsp if (unveil(GOT_DIAL_PATH_SSH, "x") != 0) {
71 d65a88a2 2021-09-05 stsp return got_error_from_errno2("unveil",
72 d65a88a2 2021-09-05 stsp GOT_DIAL_PATH_SSH);
73 d65a88a2 2021-09-05 stsp }
74 d65a88a2 2021-09-05 stsp }
75 d65a88a2 2021-09-05 stsp
76 ced242c2 2024-04-14 me if (strstr(proto, "http") != NULL) {
77 ad3b5b58 2024-04-14 me if (unveil(GOT_PATH_PROG_FETCH_HTTP, "x") != 0) {
78 ced242c2 2024-04-14 me return got_error_from_errno2("unveil",
79 ad3b5b58 2024-04-14 me GOT_PATH_PROG_FETCH_HTTP);
80 ced242c2 2024-04-14 me }
81 ced242c2 2024-04-14 me }
82 ced242c2 2024-04-14 me
83 d65a88a2 2021-09-05 stsp return NULL;
84 d65a88a2 2021-09-05 stsp }
85 d65a88a2 2021-09-05 stsp
86 5e5da8c4 2021-09-05 stsp static int
87 58e31a80 2022-06-27 op hassuffix(const char *base, const char *suf)
88 5e5da8c4 2021-09-05 stsp {
89 5e5da8c4 2021-09-05 stsp int nb, ns;
90 5e5da8c4 2021-09-05 stsp
91 5e5da8c4 2021-09-05 stsp nb = strlen(base);
92 5e5da8c4 2021-09-05 stsp ns = strlen(suf);
93 5e5da8c4 2021-09-05 stsp if (ns <= nb && strcmp(base + (nb - ns), suf) == 0)
94 5e5da8c4 2021-09-05 stsp return 1;
95 5e5da8c4 2021-09-05 stsp return 0;
96 5e5da8c4 2021-09-05 stsp }
97 5e5da8c4 2021-09-05 stsp
98 d65a88a2 2021-09-05 stsp const struct got_error *
99 5e5da8c4 2021-09-05 stsp got_dial_parse_uri(char **proto, char **host, char **port,
100 5e5da8c4 2021-09-05 stsp char **server_path, char **repo_name, const char *uri)
101 5e5da8c4 2021-09-05 stsp {
102 5e5da8c4 2021-09-05 stsp const struct got_error *err = NULL;
103 5e5da8c4 2021-09-05 stsp char *s, *p, *q;
104 5e5da8c4 2021-09-05 stsp
105 5e5da8c4 2021-09-05 stsp *proto = *host = *port = *server_path = *repo_name = NULL;
106 5e5da8c4 2021-09-05 stsp
107 5e5da8c4 2021-09-05 stsp p = strstr(uri, "://");
108 5e5da8c4 2021-09-05 stsp if (!p) {
109 5e5da8c4 2021-09-05 stsp /* Try parsing Git's "scp" style URL syntax. */
110 5e5da8c4 2021-09-05 stsp *proto = strdup("ssh");
111 805253d5 2022-03-07 naddy if (*proto == NULL) {
112 5e5da8c4 2021-09-05 stsp err = got_error_from_errno("strdup");
113 5e5da8c4 2021-09-05 stsp goto done;
114 5e5da8c4 2021-09-05 stsp }
115 5e5da8c4 2021-09-05 stsp s = (char *)uri;
116 5e5da8c4 2021-09-05 stsp q = strchr(s, ':');
117 5e5da8c4 2021-09-05 stsp if (q == NULL) {
118 5e5da8c4 2021-09-05 stsp err = got_error(GOT_ERR_PARSE_URI);
119 5e5da8c4 2021-09-05 stsp goto done;
120 5e5da8c4 2021-09-05 stsp }
121 5e5da8c4 2021-09-05 stsp /* No slashes allowed before first colon. */
122 5e5da8c4 2021-09-05 stsp p = strchr(s, '/');
123 5e5da8c4 2021-09-05 stsp if (p && q > p) {
124 5e5da8c4 2021-09-05 stsp err = got_error(GOT_ERR_PARSE_URI);
125 5e5da8c4 2021-09-05 stsp goto done;
126 5e5da8c4 2021-09-05 stsp }
127 5e5da8c4 2021-09-05 stsp *host = strndup(s, q - s);
128 5e5da8c4 2021-09-05 stsp if (*host == NULL) {
129 5e5da8c4 2021-09-05 stsp err = got_error_from_errno("strndup");
130 5e5da8c4 2021-09-05 stsp goto done;
131 5e5da8c4 2021-09-05 stsp }
132 3a12860c 2022-03-07 stsp if ((*host)[0] == '\0') {
133 3a12860c 2022-03-07 stsp err = got_error(GOT_ERR_PARSE_URI);
134 3a12860c 2022-03-07 stsp goto done;
135 3a12860c 2022-03-07 stsp }
136 5e5da8c4 2021-09-05 stsp p = q + 1;
137 5e5da8c4 2021-09-05 stsp } else {
138 5e5da8c4 2021-09-05 stsp *proto = strndup(uri, p - uri);
139 805253d5 2022-03-07 naddy if (*proto == NULL) {
140 5e5da8c4 2021-09-05 stsp err = got_error_from_errno("strndup");
141 5e5da8c4 2021-09-05 stsp goto done;
142 5e5da8c4 2021-09-05 stsp }
143 5e5da8c4 2021-09-05 stsp s = p + 3;
144 5e5da8c4 2021-09-05 stsp
145 5e5da8c4 2021-09-05 stsp p = strstr(s, "/");
146 5e5da8c4 2021-09-05 stsp if (p == NULL || strlen(p) == 1) {
147 5e5da8c4 2021-09-05 stsp err = got_error(GOT_ERR_PARSE_URI);
148 5e5da8c4 2021-09-05 stsp goto done;
149 5e5da8c4 2021-09-05 stsp }
150 5e5da8c4 2021-09-05 stsp
151 5e5da8c4 2021-09-05 stsp q = memchr(s, ':', p - s);
152 5e5da8c4 2021-09-05 stsp if (q) {
153 5e5da8c4 2021-09-05 stsp *host = strndup(s, q - s);
154 5e5da8c4 2021-09-05 stsp if (*host == NULL) {
155 5e5da8c4 2021-09-05 stsp err = got_error_from_errno("strndup");
156 5e5da8c4 2021-09-05 stsp goto done;
157 5e5da8c4 2021-09-05 stsp }
158 3a12860c 2022-03-07 stsp if ((*host)[0] == '\0') {
159 3a12860c 2022-03-07 stsp err = got_error(GOT_ERR_PARSE_URI);
160 3a12860c 2022-03-07 stsp goto done;
161 3a12860c 2022-03-07 stsp }
162 5e5da8c4 2021-09-05 stsp *port = strndup(q + 1, p - (q + 1));
163 5e5da8c4 2021-09-05 stsp if (*port == NULL) {
164 5e5da8c4 2021-09-05 stsp err = got_error_from_errno("strndup");
165 5e5da8c4 2021-09-05 stsp goto done;
166 5e5da8c4 2021-09-05 stsp }
167 3a12860c 2022-03-07 stsp if ((*port)[0] == '\0') {
168 3a12860c 2022-03-07 stsp err = got_error(GOT_ERR_PARSE_URI);
169 3a12860c 2022-03-07 stsp goto done;
170 3a12860c 2022-03-07 stsp }
171 5e5da8c4 2021-09-05 stsp } else {
172 5e5da8c4 2021-09-05 stsp *host = strndup(s, p - s);
173 5e5da8c4 2021-09-05 stsp if (*host == NULL) {
174 5e5da8c4 2021-09-05 stsp err = got_error_from_errno("strndup");
175 5e5da8c4 2021-09-05 stsp goto done;
176 5e5da8c4 2021-09-05 stsp }
177 3a12860c 2022-03-07 stsp if ((*host)[0] == '\0') {
178 3a12860c 2022-03-07 stsp err = got_error(GOT_ERR_PARSE_URI);
179 3a12860c 2022-03-07 stsp goto done;
180 3a12860c 2022-03-07 stsp }
181 5e5da8c4 2021-09-05 stsp }
182 5e5da8c4 2021-09-05 stsp }
183 5e5da8c4 2021-09-05 stsp
184 5e5da8c4 2021-09-05 stsp while (p[0] == '/' && p[1] == '/')
185 5e5da8c4 2021-09-05 stsp p++;
186 5e5da8c4 2021-09-05 stsp *server_path = strdup(p);
187 5e5da8c4 2021-09-05 stsp if (*server_path == NULL) {
188 5e5da8c4 2021-09-05 stsp err = got_error_from_errno("strdup");
189 5e5da8c4 2021-09-05 stsp goto done;
190 5e5da8c4 2021-09-05 stsp }
191 5e5da8c4 2021-09-05 stsp got_path_strip_trailing_slashes(*server_path);
192 3a12860c 2022-03-07 stsp if ((*server_path)[0] == '\0') {
193 5e5da8c4 2021-09-05 stsp err = got_error(GOT_ERR_PARSE_URI);
194 5e5da8c4 2021-09-05 stsp goto done;
195 5e5da8c4 2021-09-05 stsp }
196 3a12860c 2022-03-07 stsp
197 3a12860c 2022-03-07 stsp err = got_path_basename(repo_name, *server_path);
198 3a12860c 2022-03-07 stsp if (err)
199 5e5da8c4 2021-09-05 stsp goto done;
200 3a12860c 2022-03-07 stsp if (hassuffix(*repo_name, ".git"))
201 3a12860c 2022-03-07 stsp (*repo_name)[strlen(*repo_name) - 4] = '\0';
202 3a12860c 2022-03-07 stsp if ((*repo_name)[0] == '\0')
203 3a12860c 2022-03-07 stsp err = got_error(GOT_ERR_PARSE_URI);
204 5e5da8c4 2021-09-05 stsp done:
205 5e5da8c4 2021-09-05 stsp if (err) {
206 5e5da8c4 2021-09-05 stsp free(*proto);
207 5e5da8c4 2021-09-05 stsp *proto = NULL;
208 5e5da8c4 2021-09-05 stsp free(*host);
209 5e5da8c4 2021-09-05 stsp *host = NULL;
210 5e5da8c4 2021-09-05 stsp free(*port);
211 5e5da8c4 2021-09-05 stsp *port = NULL;
212 5e5da8c4 2021-09-05 stsp free(*server_path);
213 5e5da8c4 2021-09-05 stsp *server_path = NULL;
214 5e5da8c4 2021-09-05 stsp free(*repo_name);
215 5e5da8c4 2021-09-05 stsp *repo_name = NULL;
216 5e5da8c4 2021-09-05 stsp }
217 5e5da8c4 2021-09-05 stsp return err;
218 5e5da8c4 2021-09-05 stsp }
219 5e5da8c4 2021-09-05 stsp
220 6cc8a118 2023-03-10 op /*
221 6cc8a118 2023-03-10 op * Escape a given path for the shell which will be started by sshd.
222 6cc8a118 2023-03-10 op * In particular, git-shell is known to require single-quote characters
223 6cc8a118 2023-03-10 op * around its repository path argument and will refuse to run otherwise.
224 6cc8a118 2023-03-10 op */
225 6cc8a118 2023-03-10 op static const struct got_error *
226 6cc8a118 2023-03-10 op escape_path(char *buf, size_t bufsize, const char *path)
227 6cc8a118 2023-03-10 op {
228 6cc8a118 2023-03-10 op const char *p;
229 6cc8a118 2023-03-10 op char *q;
230 6cc8a118 2023-03-10 op
231 6cc8a118 2023-03-10 op p = path;
232 6cc8a118 2023-03-10 op q = buf;
233 6cc8a118 2023-03-10 op
234 6cc8a118 2023-03-10 op if (bufsize > 1)
235 6cc8a118 2023-03-10 op *q++ = '\'';
236 6cc8a118 2023-03-10 op
237 6cc8a118 2023-03-10 op while (*p != '\0' && (q - buf < bufsize)) {
238 6cc8a118 2023-03-10 op /* git escapes ! too */
239 6cc8a118 2023-03-10 op if (*p != '\'' && *p != '!') {
240 6cc8a118 2023-03-10 op *q++ = *p++;
241 6cc8a118 2023-03-10 op continue;
242 6cc8a118 2023-03-10 op }
243 6cc8a118 2023-03-10 op
244 6cc8a118 2023-03-10 op if (q - buf + 4 >= bufsize)
245 6cc8a118 2023-03-10 op break;
246 6cc8a118 2023-03-10 op *q++ = '\'';
247 6cc8a118 2023-03-10 op *q++ = '\\';
248 6cc8a118 2023-03-10 op *q++ = *p++;
249 6cc8a118 2023-03-10 op *q++ = '\'';
250 6cc8a118 2023-03-10 op }
251 6cc8a118 2023-03-10 op
252 6cc8a118 2023-03-10 op if (*p == '\0' && (q - buf + 1 < bufsize)) {
253 6cc8a118 2023-03-10 op *q++ = '\'';
254 6cc8a118 2023-03-10 op *q = '\0';
255 6cc8a118 2023-03-10 op return NULL;
256 6cc8a118 2023-03-10 op }
257 6cc8a118 2023-03-10 op
258 6cc8a118 2023-03-10 op return got_error_fmt(GOT_ERR_NO_SPACE, "overlong path: %s", path);
259 6cc8a118 2023-03-10 op }
260 6cc8a118 2023-03-10 op
261 5e5da8c4 2021-09-05 stsp const struct got_error *
262 d65a88a2 2021-09-05 stsp got_dial_ssh(pid_t *newpid, int *newfd, const char *host,
263 1eb38992 2023-04-14 stsp const char *port, const char *path, const char *command, int verbosity)
264 d65a88a2 2021-09-05 stsp {
265 d65a88a2 2021-09-05 stsp const struct got_error *error = NULL;
266 d65a88a2 2021-09-05 stsp int pid, pfd[2];
267 d65a88a2 2021-09-05 stsp char cmd[64];
268 6cc8a118 2023-03-10 op char escaped_path[PATH_MAX];
269 58e31a80 2022-06-27 op const char *argv[11];
270 d65a88a2 2021-09-05 stsp int i = 0, j;
271 d65a88a2 2021-09-05 stsp
272 d65a88a2 2021-09-05 stsp *newpid = -1;
273 d65a88a2 2021-09-05 stsp *newfd = -1;
274 d65a88a2 2021-09-05 stsp
275 6cc8a118 2023-03-10 op error = escape_path(escaped_path, sizeof(escaped_path), path);
276 6cc8a118 2023-03-10 op if (error)
277 6cc8a118 2023-03-10 op return error;
278 6cc8a118 2023-03-10 op
279 d65a88a2 2021-09-05 stsp argv[i++] = GOT_DIAL_PATH_SSH;
280 d65a88a2 2021-09-05 stsp if (port != NULL) {
281 d65a88a2 2021-09-05 stsp argv[i++] = "-p";
282 d65a88a2 2021-09-05 stsp argv[i++] = (char *)port;
283 d65a88a2 2021-09-05 stsp }
284 d65a88a2 2021-09-05 stsp if (verbosity == -1) {
285 d65a88a2 2021-09-05 stsp argv[i++] = "-q";
286 d65a88a2 2021-09-05 stsp } else {
287 d65a88a2 2021-09-05 stsp /* ssh(1) allows up to 3 "-v" options. */
288 d65a88a2 2021-09-05 stsp for (j = 0; j < MIN(3, verbosity); j++)
289 d65a88a2 2021-09-05 stsp argv[i++] = "-v";
290 d65a88a2 2021-09-05 stsp }
291 d65a88a2 2021-09-05 stsp argv[i++] = "--";
292 d65a88a2 2021-09-05 stsp argv[i++] = (char *)host;
293 d65a88a2 2021-09-05 stsp argv[i++] = (char *)cmd;
294 6cc8a118 2023-03-10 op argv[i++] = (char *)escaped_path;
295 d65a88a2 2021-09-05 stsp argv[i++] = NULL;
296 c10270f6 2021-09-06 naddy assert(i <= nitems(argv));
297 d65a88a2 2021-09-05 stsp
298 d65a88a2 2021-09-05 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pfd) == -1)
299 d65a88a2 2021-09-05 stsp return got_error_from_errno("socketpair");
300 d65a88a2 2021-09-05 stsp
301 d65a88a2 2021-09-05 stsp pid = fork();
302 d65a88a2 2021-09-05 stsp if (pid == -1) {
303 d65a88a2 2021-09-05 stsp error = got_error_from_errno("fork");
304 d65a88a2 2021-09-05 stsp close(pfd[0]);
305 d65a88a2 2021-09-05 stsp close(pfd[1]);
306 d65a88a2 2021-09-05 stsp return error;
307 d65a88a2 2021-09-05 stsp } else if (pid == 0) {
308 d65a88a2 2021-09-05 stsp if (close(pfd[1]) == -1)
309 d65a88a2 2021-09-05 stsp err(1, "close");
310 d65a88a2 2021-09-05 stsp if (dup2(pfd[0], 0) == -1)
311 d65a88a2 2021-09-05 stsp err(1, "dup2");
312 d65a88a2 2021-09-05 stsp if (dup2(pfd[0], 1) == -1)
313 d65a88a2 2021-09-05 stsp err(1, "dup2");
314 1eb38992 2023-04-14 stsp if (strlcpy(cmd, command, sizeof(cmd)) >= sizeof(cmd))
315 d65a88a2 2021-09-05 stsp err(1, "snprintf");
316 58e31a80 2022-06-27 op if (execv(GOT_DIAL_PATH_SSH, (char *const *)argv) == -1)
317 7a86002d 2024-02-16 op err(1, "execv %s", GOT_DIAL_PATH_SSH);
318 d65a88a2 2021-09-05 stsp abort(); /* not reached */
319 d65a88a2 2021-09-05 stsp } else {
320 d65a88a2 2021-09-05 stsp if (close(pfd[0]) == -1)
321 d65a88a2 2021-09-05 stsp return got_error_from_errno("close");
322 d65a88a2 2021-09-05 stsp *newpid = pid;
323 d65a88a2 2021-09-05 stsp *newfd = pfd[1];
324 d65a88a2 2021-09-05 stsp return NULL;
325 d65a88a2 2021-09-05 stsp }
326 d65a88a2 2021-09-05 stsp }
327 d65a88a2 2021-09-05 stsp
328 d65a88a2 2021-09-05 stsp const struct got_error *
329 d65a88a2 2021-09-05 stsp got_dial_git(int *newfd, const char *host, const char *port,
330 1eb38992 2023-04-14 stsp const char *path, const char *command)
331 d65a88a2 2021-09-05 stsp {
332 d65a88a2 2021-09-05 stsp const struct got_error *err = NULL;
333 d65a88a2 2021-09-05 stsp struct addrinfo hints, *servinfo, *p;
334 d65a88a2 2021-09-05 stsp char *cmd = NULL;
335 d65a88a2 2021-09-05 stsp int fd = -1, len, r, eaicode;
336 d65a88a2 2021-09-05 stsp
337 d65a88a2 2021-09-05 stsp *newfd = -1;
338 d65a88a2 2021-09-05 stsp
339 d65a88a2 2021-09-05 stsp if (port == NULL)
340 d65a88a2 2021-09-05 stsp port = GOT_DEFAULT_GIT_PORT_STR;
341 d65a88a2 2021-09-05 stsp
342 d65a88a2 2021-09-05 stsp memset(&hints, 0, sizeof hints);
343 d65a88a2 2021-09-05 stsp hints.ai_family = AF_UNSPEC;
344 d65a88a2 2021-09-05 stsp hints.ai_socktype = SOCK_STREAM;
345 d65a88a2 2021-09-05 stsp eaicode = getaddrinfo(host, port, &hints, &servinfo);
346 d65a88a2 2021-09-05 stsp if (eaicode) {
347 d65a88a2 2021-09-05 stsp char msg[512];
348 d65a88a2 2021-09-05 stsp snprintf(msg, sizeof(msg), "%s: %s", host,
349 d65a88a2 2021-09-05 stsp gai_strerror(eaicode));
350 d65a88a2 2021-09-05 stsp return got_error_msg(GOT_ERR_ADDRINFO, msg);
351 d65a88a2 2021-09-05 stsp }
352 d65a88a2 2021-09-05 stsp
353 d65a88a2 2021-09-05 stsp for (p = servinfo; p != NULL; p = p->ai_next) {
354 d65a88a2 2021-09-05 stsp if ((fd = socket(p->ai_family, p->ai_socktype,
355 d65a88a2 2021-09-05 stsp p->ai_protocol)) == -1)
356 d65a88a2 2021-09-05 stsp continue;
357 d65a88a2 2021-09-05 stsp if (connect(fd, p->ai_addr, p->ai_addrlen) == 0) {
358 d65a88a2 2021-09-05 stsp err = NULL;
359 d65a88a2 2021-09-05 stsp break;
360 d65a88a2 2021-09-05 stsp }
361 d65a88a2 2021-09-05 stsp err = got_error_from_errno("connect");
362 d65a88a2 2021-09-05 stsp close(fd);
363 d65a88a2 2021-09-05 stsp }
364 9ea55f08 2022-09-05 op freeaddrinfo(servinfo);
365 d65a88a2 2021-09-05 stsp if (p == NULL)
366 d65a88a2 2021-09-05 stsp goto done;
367 d65a88a2 2021-09-05 stsp
368 1eb38992 2023-04-14 stsp if (asprintf(&cmd, "%s %s", command, path) == -1) {
369 d65a88a2 2021-09-05 stsp err = got_error_from_errno("asprintf");
370 d65a88a2 2021-09-05 stsp goto done;
371 d65a88a2 2021-09-05 stsp }
372 d65a88a2 2021-09-05 stsp len = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
373 d65a88a2 2021-09-05 stsp r = dprintf(fd, "%04x%s%chost=%s%c", len, cmd, '\0', host, '\0');
374 d65a88a2 2021-09-05 stsp if (r < 0)
375 d65a88a2 2021-09-05 stsp err = got_error_from_errno("dprintf");
376 d65a88a2 2021-09-05 stsp done:
377 d65a88a2 2021-09-05 stsp free(cmd);
378 d65a88a2 2021-09-05 stsp if (err) {
379 d65a88a2 2021-09-05 stsp if (fd != -1)
380 d65a88a2 2021-09-05 stsp close(fd);
381 d65a88a2 2021-09-05 stsp } else
382 d65a88a2 2021-09-05 stsp *newfd = fd;
383 d65a88a2 2021-09-05 stsp return err;
384 ced242c2 2024-04-14 me }
385 ced242c2 2024-04-14 me
386 ced242c2 2024-04-14 me const struct got_error *
387 ced242c2 2024-04-14 me got_dial_http(pid_t *newpid, int *newfd, const char *host,
388 ced242c2 2024-04-14 me const char *port, const char *path, int verbosity, int tls)
389 ced242c2 2024-04-14 me {
390 ced242c2 2024-04-14 me const struct got_error *error = NULL;
391 ced242c2 2024-04-14 me int pid, pfd[2];
392 ced242c2 2024-04-14 me const char *argv[8];
393 ced242c2 2024-04-14 me int i = 0;
394 ced242c2 2024-04-14 me
395 ced242c2 2024-04-14 me *newpid = -1;
396 ced242c2 2024-04-14 me *newfd = -1;
397 ced242c2 2024-04-14 me
398 ced242c2 2024-04-14 me if (!port)
399 ced242c2 2024-04-14 me port = tls ? "443" : "80";
400 ced242c2 2024-04-14 me
401 ad3b5b58 2024-04-14 me argv[i++] = GOT_PATH_PROG_FETCH_HTTP;
402 ced242c2 2024-04-14 me if (verbosity == -1)
403 ced242c2 2024-04-14 me argv[i++] = "-q";
404 ced242c2 2024-04-14 me else if (verbosity > 0)
405 ced242c2 2024-04-14 me argv[i++] = "-v";
406 ced242c2 2024-04-14 me argv[i++] = "--";
407 ced242c2 2024-04-14 me argv[i++] = tls ? "https" : "http";
408 ced242c2 2024-04-14 me argv[i++] = host;
409 ced242c2 2024-04-14 me argv[i++] = port;
410 ced242c2 2024-04-14 me argv[i++] = path;
411 ced242c2 2024-04-14 me argv[i++] = NULL;
412 ced242c2 2024-04-14 me assert(i <= nitems(argv));
413 ced242c2 2024-04-14 me
414 ced242c2 2024-04-14 me if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pfd) == -1)
415 ced242c2 2024-04-14 me return got_error_from_errno("socketpair");
416 ced242c2 2024-04-14 me
417 ced242c2 2024-04-14 me pid = fork();
418 ced242c2 2024-04-14 me if (pid == -1) {
419 ced242c2 2024-04-14 me error = got_error_from_errno("fork");
420 ced242c2 2024-04-14 me close(pfd[0]);
421 ced242c2 2024-04-14 me close(pfd[1]);
422 ced242c2 2024-04-14 me return error;
423 ced242c2 2024-04-14 me } else if (pid == 0) {
424 ced242c2 2024-04-14 me if (close(pfd[1]) == -1)
425 ced242c2 2024-04-14 me err(1, "close");
426 ced242c2 2024-04-14 me if (dup2(pfd[0], 0) == -1)
427 ced242c2 2024-04-14 me err(1, "dup2");
428 ced242c2 2024-04-14 me if (dup2(pfd[0], 1) == -1)
429 ced242c2 2024-04-14 me err(1, "dup2");
430 ad3b5b58 2024-04-14 me if (execv(GOT_PATH_PROG_FETCH_HTTP, (char *const *)argv) == -1)
431 ced242c2 2024-04-14 me err(1, "execv");
432 ced242c2 2024-04-14 me abort(); /* not reached */
433 ced242c2 2024-04-14 me } else {
434 ced242c2 2024-04-14 me if (close(pfd[0]) == -1)
435 ced242c2 2024-04-14 me return got_error_from_errno("close");
436 ced242c2 2024-04-14 me *newpid = pid;
437 ced242c2 2024-04-14 me *newfd = pfd[1];
438 ced242c2 2024-04-14 me return NULL;
439 ced242c2 2024-04-14 me }
440 d65a88a2 2021-09-05 stsp }
441 1eb38992 2023-04-14 stsp
442 1eb38992 2023-04-14 stsp const struct got_error *
443 1eb38992 2023-04-14 stsp got_dial_parse_command(char **command, char **repo_path, const char *gitcmd)
444 1eb38992 2023-04-14 stsp {
445 1eb38992 2023-04-14 stsp const struct got_error *err = NULL;
446 1eb38992 2023-04-14 stsp size_t len, cmdlen, pathlen;
447 1eb38992 2023-04-14 stsp char *path0 = NULL, *path, *abspath = NULL, *canonpath = NULL;
448 1eb38992 2023-04-14 stsp const char *relpath;
449 1eb38992 2023-04-14 stsp
450 1eb38992 2023-04-14 stsp *command = NULL;
451 1eb38992 2023-04-14 stsp *repo_path = NULL;
452 1eb38992 2023-04-14 stsp
453 1eb38992 2023-04-14 stsp len = strlen(gitcmd);
454 1eb38992 2023-04-14 stsp
455 1eb38992 2023-04-14 stsp if (len >= strlen(GOT_DIAL_CMD_SEND) &&
456 1eb38992 2023-04-14 stsp strncmp(gitcmd, GOT_DIAL_CMD_SEND,
457 1eb38992 2023-04-14 stsp strlen(GOT_DIAL_CMD_SEND)) == 0)
458 1eb38992 2023-04-14 stsp cmdlen = strlen(GOT_DIAL_CMD_SEND);
459 1eb38992 2023-04-14 stsp else if (len >= strlen(GOT_DIAL_CMD_FETCH) &&
460 1eb38992 2023-04-14 stsp strncmp(gitcmd, GOT_DIAL_CMD_FETCH,
461 1eb38992 2023-04-14 stsp strlen(GOT_DIAL_CMD_FETCH)) == 0)
462 1eb38992 2023-04-14 stsp cmdlen = strlen(GOT_DIAL_CMD_FETCH);
463 1eb38992 2023-04-14 stsp else
464 1eb38992 2023-04-14 stsp return got_error(GOT_ERR_BAD_PACKET);
465 1eb38992 2023-04-14 stsp
466 1eb38992 2023-04-14 stsp if (len <= cmdlen + 1 || gitcmd[cmdlen] != ' ')
467 1eb38992 2023-04-14 stsp return got_error(GOT_ERR_BAD_PACKET);
468 1eb38992 2023-04-14 stsp
469 1eb38992 2023-04-14 stsp if (memchr(&gitcmd[cmdlen + 1], '\0', len - cmdlen) == NULL)
470 1eb38992 2023-04-14 stsp return got_error(GOT_ERR_BAD_PATH);
471 1eb38992 2023-04-14 stsp
472 1eb38992 2023-04-14 stsp /* Forbid linefeeds in paths, like Git does. */
473 1eb38992 2023-04-14 stsp if (memchr(&gitcmd[cmdlen + 1], '\n', len - cmdlen) != NULL)
474 1eb38992 2023-04-14 stsp return got_error(GOT_ERR_BAD_PATH);
475 1eb38992 2023-04-14 stsp
476 1eb38992 2023-04-14 stsp path0 = strdup(&gitcmd[cmdlen + 1]);
477 1eb38992 2023-04-14 stsp if (path0 == NULL)
478 1eb38992 2023-04-14 stsp return got_error_from_errno("strdup");
479 1eb38992 2023-04-14 stsp path = path0;
480 1eb38992 2023-04-14 stsp pathlen = strlen(path);
481 1eb38992 2023-04-14 stsp
482 1eb38992 2023-04-14 stsp /*
483 1eb38992 2023-04-14 stsp * Git clients send a shell command.
484 1eb38992 2023-04-14 stsp * Trim spaces and quotes around the path.
485 1eb38992 2023-04-14 stsp */
486 1eb38992 2023-04-14 stsp while (path[0] == '\'' || path[0] == '\"' || path[0] == ' ') {
487 1eb38992 2023-04-14 stsp path++;
488 1eb38992 2023-04-14 stsp pathlen--;
489 1eb38992 2023-04-14 stsp }
490 1eb38992 2023-04-14 stsp while (pathlen > 0 &&
491 1eb38992 2023-04-14 stsp (path[pathlen - 1] == '\'' || path[pathlen - 1] == '\"' ||
492 1eb38992 2023-04-14 stsp path[pathlen - 1] == ' ')) {
493 1eb38992 2023-04-14 stsp path[pathlen - 1] = '\0';
494 1eb38992 2023-04-14 stsp pathlen--;
495 1eb38992 2023-04-14 stsp }
496 1eb38992 2023-04-14 stsp
497 1eb38992 2023-04-14 stsp /* Deny an empty repository path. */
498 1eb38992 2023-04-14 stsp if (path[0] == '\0' || got_path_is_root_dir(path)) {
499 1eb38992 2023-04-14 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
500 1eb38992 2023-04-14 stsp goto done;
501 1eb38992 2023-04-14 stsp }
502 1eb38992 2023-04-14 stsp
503 1eb38992 2023-04-14 stsp if (asprintf(&abspath, "/%s", path) == -1) {
504 1eb38992 2023-04-14 stsp err = got_error_from_errno("asprintf");
505 1eb38992 2023-04-14 stsp goto done;
506 1eb38992 2023-04-14 stsp }
507 1eb38992 2023-04-14 stsp pathlen = strlen(abspath);
508 1eb38992 2023-04-14 stsp canonpath = malloc(pathlen + 1);
509 1eb38992 2023-04-14 stsp if (canonpath == NULL) {
510 1eb38992 2023-04-14 stsp err = got_error_from_errno("malloc");
511 1eb38992 2023-04-14 stsp goto done;
512 1eb38992 2023-04-14 stsp }
513 1eb38992 2023-04-14 stsp err = got_canonpath(abspath, canonpath, pathlen + 1);
514 1eb38992 2023-04-14 stsp if (err)
515 1eb38992 2023-04-14 stsp goto done;
516 1eb38992 2023-04-14 stsp
517 1eb38992 2023-04-14 stsp relpath = canonpath;
518 1eb38992 2023-04-14 stsp while (relpath[0] == '/')
519 1eb38992 2023-04-14 stsp relpath++;
520 1eb38992 2023-04-14 stsp *repo_path = strdup(relpath);
521 1eb38992 2023-04-14 stsp if (*repo_path == NULL) {
522 1eb38992 2023-04-14 stsp err = got_error_from_errno("strdup");
523 1eb38992 2023-04-14 stsp goto done;
524 1eb38992 2023-04-14 stsp }
525 1eb38992 2023-04-14 stsp *command = strndup(gitcmd, cmdlen);
526 1eb38992 2023-04-14 stsp if (*command == NULL)
527 1eb38992 2023-04-14 stsp err = got_error_from_errno("strndup");
528 1eb38992 2023-04-14 stsp done:
529 1eb38992 2023-04-14 stsp free(path0);
530 1eb38992 2023-04-14 stsp free(abspath);
531 1eb38992 2023-04-14 stsp free(canonpath);
532 1eb38992 2023-04-14 stsp if (err) {
533 1eb38992 2023-04-14 stsp free(*repo_path);
534 1eb38992 2023-04-14 stsp *repo_path = NULL;
535 1eb38992 2023-04-14 stsp }
536 1eb38992 2023-04-14 stsp return err;
537 1eb38992 2023-04-14 stsp }