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>
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.
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.
18 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
20 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
21 d65a88a2 2021-09-05 stsp #include <sys/types.h>
22 d65a88a2 2021-09-05 stsp #include <sys/socket.h>
23 37e7d69e 2024-04-25 thomas.ad #include <sys/uio.h>
24 d65a88a2 2021-09-05 stsp #include <netdb.h>
26 c10270f6 2021-09-06 naddy #include <assert.h>
27 d65a88a2 2021-09-05 stsp #include <err.h>
28 a928faa1 2023-03-10 thomas #include <limits.h>
29 37e7d69e 2024-04-25 thomas.ad #include <sha1.h>
30 37e7d69e 2024-04-25 thomas.ad #include <stdint.h>
31 37e7d69e 2024-04-25 thomas.ad #include <limits.h>
32 d65a88a2 2021-09-05 stsp #include <stdio.h>
33 d65a88a2 2021-09-05 stsp #include <stdlib.h>
34 d65a88a2 2021-09-05 stsp #include <string.h>
35 d65a88a2 2021-09-05 stsp #include <unistd.h>
36 37e7d69e 2024-04-25 thomas.ad #include <imsg.h>
38 d65a88a2 2021-09-05 stsp #include "got_error.h"
39 5e5da8c4 2021-09-05 stsp #include "got_path.h"
40 37e7d69e 2024-04-25 thomas.ad #include "got_object.h"
42 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
44 d65a88a2 2021-09-05 stsp #include "got_lib_dial.h"
45 37e7d69e 2024-04-25 thomas.ad #include "got_lib_delta.h"
46 37e7d69e 2024-04-25 thomas.ad #include "got_lib_object.h"
47 37e7d69e 2024-04-25 thomas.ad #include "got_lib_privsep.h"
48 ef20f542 2022-06-26 thomas #include "got_dial.h"
50 c10270f6 2021-09-06 naddy #ifndef nitems
51 c10270f6 2021-09-06 naddy #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 d65a88a2 2021-09-05 stsp #ifndef ssizeof
55 d65a88a2 2021-09-05 stsp #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
59 d65a88a2 2021-09-05 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 d65a88a2 2021-09-05 stsp #ifndef GOT_DIAL_PATH_SSH
63 d65a88a2 2021-09-05 stsp #define GOT_DIAL_PATH_SSH "/usr/bin/ssh"
66 d65a88a2 2021-09-05 stsp /* IANA assigned */
67 d65a88a2 2021-09-05 stsp #define GOT_DEFAULT_GIT_PORT 9418
68 d65a88a2 2021-09-05 stsp #define GOT_DEFAULT_GIT_PORT_STR "9418"
70 d65a88a2 2021-09-05 stsp const struct got_error *
71 d65a88a2 2021-09-05 stsp got_dial_apply_unveil(const char *proto)
73 d65a88a2 2021-09-05 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
74 d65a88a2 2021-09-05 stsp if (unveil(GOT_DIAL_PATH_SSH, "x") != 0) {
75 d65a88a2 2021-09-05 stsp return got_error_from_errno2("unveil",
76 d65a88a2 2021-09-05 stsp GOT_DIAL_PATH_SSH);
80 37e7d69e 2024-04-25 thomas.ad if (strstr(proto, "http") != NULL) {
81 09876a9d 2024-04-25 thomas.ad if (unveil(GOT_PATH_PROG_FETCH_HTTP, "x") != 0) {
82 37e7d69e 2024-04-25 thomas.ad return got_error_from_errno2("unveil",
83 09876a9d 2024-04-25 thomas.ad GOT_PATH_PROG_FETCH_HTTP);
87 d65a88a2 2021-09-05 stsp return NULL;
91 0c6f49ba 2022-07-01 thomas hassuffix(const char *base, const char *suf)
95 5e5da8c4 2021-09-05 stsp nb = strlen(base);
96 5e5da8c4 2021-09-05 stsp ns = strlen(suf);
97 5e5da8c4 2021-09-05 stsp if (ns <= nb && strcmp(base + (nb - ns), suf) == 0)
102 d65a88a2 2021-09-05 stsp const struct got_error *
103 5e5da8c4 2021-09-05 stsp got_dial_parse_uri(char **proto, char **host, char **port,
104 5e5da8c4 2021-09-05 stsp char **server_path, char **repo_name, const char *uri)
106 5e5da8c4 2021-09-05 stsp const struct got_error *err = NULL;
107 5e5da8c4 2021-09-05 stsp char *s, *p, *q;
109 5e5da8c4 2021-09-05 stsp *proto = *host = *port = *server_path = *repo_name = NULL;
111 5e5da8c4 2021-09-05 stsp p = strstr(uri, "://");
113 5e5da8c4 2021-09-05 stsp /* Try parsing Git's "scp" style URL syntax. */
114 5e5da8c4 2021-09-05 stsp *proto = strdup("ssh");
115 2996af60 2022-03-07 thomas if (*proto == NULL) {
116 5e5da8c4 2021-09-05 stsp err = got_error_from_errno("strdup");
119 5e5da8c4 2021-09-05 stsp s = (char *)uri;
120 5e5da8c4 2021-09-05 stsp q = strchr(s, ':');
121 5e5da8c4 2021-09-05 stsp if (q == NULL) {
122 5e5da8c4 2021-09-05 stsp err = got_error(GOT_ERR_PARSE_URI);
125 5e5da8c4 2021-09-05 stsp /* No slashes allowed before first colon. */
126 5e5da8c4 2021-09-05 stsp p = strchr(s, '/');
127 5e5da8c4 2021-09-05 stsp if (p && q > p) {
128 5e5da8c4 2021-09-05 stsp err = got_error(GOT_ERR_PARSE_URI);
131 5e5da8c4 2021-09-05 stsp *host = strndup(s, q - s);
132 5e5da8c4 2021-09-05 stsp if (*host == NULL) {
133 5e5da8c4 2021-09-05 stsp err = got_error_from_errno("strndup");
136 26ec43f5 2022-03-07 thomas if ((*host)[0] == '\0') {
137 26ec43f5 2022-03-07 thomas err = got_error(GOT_ERR_PARSE_URI);
138 26ec43f5 2022-03-07 thomas goto done;
142 5e5da8c4 2021-09-05 stsp *proto = strndup(uri, p - uri);
143 2996af60 2022-03-07 thomas if (*proto == NULL) {
144 5e5da8c4 2021-09-05 stsp err = got_error_from_errno("strndup");
149 5e5da8c4 2021-09-05 stsp p = strstr(s, "/");
150 5e5da8c4 2021-09-05 stsp if (p == NULL || strlen(p) == 1) {
151 5e5da8c4 2021-09-05 stsp err = got_error(GOT_ERR_PARSE_URI);
155 5e5da8c4 2021-09-05 stsp q = memchr(s, ':', p - s);
157 5e5da8c4 2021-09-05 stsp *host = strndup(s, q - s);
158 5e5da8c4 2021-09-05 stsp if (*host == NULL) {
159 5e5da8c4 2021-09-05 stsp err = got_error_from_errno("strndup");
162 26ec43f5 2022-03-07 thomas if ((*host)[0] == '\0') {
163 26ec43f5 2022-03-07 thomas err = got_error(GOT_ERR_PARSE_URI);
164 26ec43f5 2022-03-07 thomas goto done;
166 5e5da8c4 2021-09-05 stsp *port = strndup(q + 1, p - (q + 1));
167 5e5da8c4 2021-09-05 stsp if (*port == NULL) {
168 5e5da8c4 2021-09-05 stsp err = got_error_from_errno("strndup");
171 26ec43f5 2022-03-07 thomas if ((*port)[0] == '\0') {
172 26ec43f5 2022-03-07 thomas err = got_error(GOT_ERR_PARSE_URI);
173 26ec43f5 2022-03-07 thomas goto done;
176 5e5da8c4 2021-09-05 stsp *host = strndup(s, p - s);
177 5e5da8c4 2021-09-05 stsp if (*host == NULL) {
178 5e5da8c4 2021-09-05 stsp err = got_error_from_errno("strndup");
181 26ec43f5 2022-03-07 thomas if ((*host)[0] == '\0') {
182 26ec43f5 2022-03-07 thomas err = got_error(GOT_ERR_PARSE_URI);
183 26ec43f5 2022-03-07 thomas goto done;
188 5e5da8c4 2021-09-05 stsp while (p[0] == '/' && p[1] == '/')
190 5e5da8c4 2021-09-05 stsp *server_path = strdup(p);
191 5e5da8c4 2021-09-05 stsp if (*server_path == NULL) {
192 5e5da8c4 2021-09-05 stsp err = got_error_from_errno("strdup");
195 5e5da8c4 2021-09-05 stsp got_path_strip_trailing_slashes(*server_path);
196 26ec43f5 2022-03-07 thomas if ((*server_path)[0] == '\0') {
197 5e5da8c4 2021-09-05 stsp err = got_error(GOT_ERR_PARSE_URI);
201 26ec43f5 2022-03-07 thomas err = got_path_basename(repo_name, *server_path);
204 26ec43f5 2022-03-07 thomas if (hassuffix(*repo_name, ".git"))
205 26ec43f5 2022-03-07 thomas (*repo_name)[strlen(*repo_name) - 4] = '\0';
206 26ec43f5 2022-03-07 thomas if ((*repo_name)[0] == '\0')
207 26ec43f5 2022-03-07 thomas err = got_error(GOT_ERR_PARSE_URI);
210 5e5da8c4 2021-09-05 stsp free(*proto);
211 5e5da8c4 2021-09-05 stsp *proto = NULL;
212 5e5da8c4 2021-09-05 stsp free(*host);
213 5e5da8c4 2021-09-05 stsp *host = NULL;
214 5e5da8c4 2021-09-05 stsp free(*port);
215 5e5da8c4 2021-09-05 stsp *port = NULL;
216 5e5da8c4 2021-09-05 stsp free(*server_path);
217 5e5da8c4 2021-09-05 stsp *server_path = NULL;
218 5e5da8c4 2021-09-05 stsp free(*repo_name);
219 5e5da8c4 2021-09-05 stsp *repo_name = NULL;
221 5e5da8c4 2021-09-05 stsp return err;
225 a928faa1 2023-03-10 thomas * Escape a given path for the shell which will be started by sshd.
226 a928faa1 2023-03-10 thomas * In particular, git-shell is known to require single-quote characters
227 a928faa1 2023-03-10 thomas * around its repository path argument and will refuse to run otherwise.
229 a928faa1 2023-03-10 thomas static const struct got_error *
230 a928faa1 2023-03-10 thomas escape_path(char *buf, size_t bufsize, const char *path)
232 a928faa1 2023-03-10 thomas const char *p;
235 a928faa1 2023-03-10 thomas p = path;
238 a928faa1 2023-03-10 thomas if (bufsize > 1)
239 a928faa1 2023-03-10 thomas *q++ = '\'';
241 a928faa1 2023-03-10 thomas while (*p != '\0' && (q - buf < bufsize)) {
242 a928faa1 2023-03-10 thomas /* git escapes ! too */
243 a928faa1 2023-03-10 thomas if (*p != '\'' && *p != '!') {
244 a928faa1 2023-03-10 thomas *q++ = *p++;
245 a928faa1 2023-03-10 thomas continue;
248 a928faa1 2023-03-10 thomas if (q - buf + 4 >= bufsize)
250 a928faa1 2023-03-10 thomas *q++ = '\'';
251 a928faa1 2023-03-10 thomas *q++ = '\\';
252 a928faa1 2023-03-10 thomas *q++ = *p++;
253 a928faa1 2023-03-10 thomas *q++ = '\'';
256 a928faa1 2023-03-10 thomas if (*p == '\0' && (q - buf + 1 < bufsize)) {
257 a928faa1 2023-03-10 thomas *q++ = '\'';
258 a928faa1 2023-03-10 thomas *q = '\0';
259 a928faa1 2023-03-10 thomas return NULL;
262 a928faa1 2023-03-10 thomas return got_error_fmt(GOT_ERR_NO_SPACE, "overlong path: %s", path);
265 5e5da8c4 2021-09-05 stsp const struct got_error *
266 d65a88a2 2021-09-05 stsp got_dial_ssh(pid_t *newpid, int *newfd, const char *host,
267 5769f9a0 2023-04-22 thomas const char *port, const char *path, const char *command, int verbosity)
269 d65a88a2 2021-09-05 stsp const struct got_error *error = NULL;
270 d65a88a2 2021-09-05 stsp int pid, pfd[2];
271 d65a88a2 2021-09-05 stsp char cmd[64];
272 a928faa1 2023-03-10 thomas char escaped_path[PATH_MAX];
273 0c6f49ba 2022-07-01 thomas const char *argv[11];
274 d65a88a2 2021-09-05 stsp int i = 0, j;
276 d65a88a2 2021-09-05 stsp *newpid = -1;
277 d65a88a2 2021-09-05 stsp *newfd = -1;
279 a928faa1 2023-03-10 thomas error = escape_path(escaped_path, sizeof(escaped_path), path);
280 a928faa1 2023-03-10 thomas if (error)
281 a928faa1 2023-03-10 thomas return error;
283 d65a88a2 2021-09-05 stsp argv[i++] = GOT_DIAL_PATH_SSH;
284 d65a88a2 2021-09-05 stsp if (port != NULL) {
285 d65a88a2 2021-09-05 stsp argv[i++] = "-p";
286 d65a88a2 2021-09-05 stsp argv[i++] = (char *)port;
288 d65a88a2 2021-09-05 stsp if (verbosity == -1) {
289 d65a88a2 2021-09-05 stsp argv[i++] = "-q";
291 d65a88a2 2021-09-05 stsp /* ssh(1) allows up to 3 "-v" options. */
292 d65a88a2 2021-09-05 stsp for (j = 0; j < MIN(3, verbosity); j++)
293 d65a88a2 2021-09-05 stsp argv[i++] = "-v";
295 d65a88a2 2021-09-05 stsp argv[i++] = "--";
296 d65a88a2 2021-09-05 stsp argv[i++] = (char *)host;
297 d65a88a2 2021-09-05 stsp argv[i++] = (char *)cmd;
298 a928faa1 2023-03-10 thomas argv[i++] = (char *)escaped_path;
299 d65a88a2 2021-09-05 stsp argv[i++] = NULL;
300 c10270f6 2021-09-06 naddy assert(i <= nitems(argv));
302 d65a88a2 2021-09-05 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pfd) == -1)
303 d65a88a2 2021-09-05 stsp return got_error_from_errno("socketpair");
305 d65a88a2 2021-09-05 stsp pid = fork();
306 d65a88a2 2021-09-05 stsp if (pid == -1) {
307 d65a88a2 2021-09-05 stsp error = got_error_from_errno("fork");
308 d65a88a2 2021-09-05 stsp close(pfd[0]);
309 d65a88a2 2021-09-05 stsp close(pfd[1]);
310 d65a88a2 2021-09-05 stsp return error;
311 d65a88a2 2021-09-05 stsp } else if (pid == 0) {
312 d65a88a2 2021-09-05 stsp if (close(pfd[1]) == -1)
313 d65a88a2 2021-09-05 stsp err(1, "close");
314 d65a88a2 2021-09-05 stsp if (dup2(pfd[0], 0) == -1)
315 d65a88a2 2021-09-05 stsp err(1, "dup2");
316 d65a88a2 2021-09-05 stsp if (dup2(pfd[0], 1) == -1)
317 d65a88a2 2021-09-05 stsp err(1, "dup2");
318 5769f9a0 2023-04-22 thomas if (strlcpy(cmd, command, sizeof(cmd)) >= sizeof(cmd))
319 d65a88a2 2021-09-05 stsp err(1, "snprintf");
320 0c6f49ba 2022-07-01 thomas if (execv(GOT_DIAL_PATH_SSH, (char *const *)argv) == -1)
321 eb0f0005 2024-03-02 thomas err(1, "execv %s", GOT_DIAL_PATH_SSH);
322 d65a88a2 2021-09-05 stsp abort(); /* not reached */
324 d65a88a2 2021-09-05 stsp if (close(pfd[0]) == -1)
325 d65a88a2 2021-09-05 stsp return got_error_from_errno("close");
326 d65a88a2 2021-09-05 stsp *newpid = pid;
327 d65a88a2 2021-09-05 stsp *newfd = pfd[1];
328 d65a88a2 2021-09-05 stsp return NULL;
332 d65a88a2 2021-09-05 stsp const struct got_error *
333 d65a88a2 2021-09-05 stsp got_dial_git(int *newfd, const char *host, const char *port,
334 5769f9a0 2023-04-22 thomas const char *path, const char *command)
336 d65a88a2 2021-09-05 stsp const struct got_error *err = NULL;
337 d65a88a2 2021-09-05 stsp struct addrinfo hints, *servinfo, *p;
338 d65a88a2 2021-09-05 stsp char *cmd = NULL;
339 d65a88a2 2021-09-05 stsp int fd = -1, len, r, eaicode;
341 d65a88a2 2021-09-05 stsp *newfd = -1;
343 d65a88a2 2021-09-05 stsp if (port == NULL)
344 d65a88a2 2021-09-05 stsp port = GOT_DEFAULT_GIT_PORT_STR;
346 d65a88a2 2021-09-05 stsp memset(&hints, 0, sizeof hints);
347 d65a88a2 2021-09-05 stsp hints.ai_family = AF_UNSPEC;
348 d65a88a2 2021-09-05 stsp hints.ai_socktype = SOCK_STREAM;
349 d65a88a2 2021-09-05 stsp eaicode = getaddrinfo(host, port, &hints, &servinfo);
350 d65a88a2 2021-09-05 stsp if (eaicode) {
351 d65a88a2 2021-09-05 stsp char msg[512];
352 d65a88a2 2021-09-05 stsp snprintf(msg, sizeof(msg), "%s: %s", host,
353 d65a88a2 2021-09-05 stsp gai_strerror(eaicode));
354 d65a88a2 2021-09-05 stsp return got_error_msg(GOT_ERR_ADDRINFO, msg);
357 d65a88a2 2021-09-05 stsp for (p = servinfo; p != NULL; p = p->ai_next) {
358 d65a88a2 2021-09-05 stsp if ((fd = socket(p->ai_family, p->ai_socktype,
359 d65a88a2 2021-09-05 stsp p->ai_protocol)) == -1)
361 d65a88a2 2021-09-05 stsp if (connect(fd, p->ai_addr, p->ai_addrlen) == 0) {
362 d65a88a2 2021-09-05 stsp err = NULL;
365 d65a88a2 2021-09-05 stsp err = got_error_from_errno("connect");
368 7ee8c11a 2022-09-05 thomas freeaddrinfo(servinfo);
369 d65a88a2 2021-09-05 stsp if (p == NULL)
372 5769f9a0 2023-04-22 thomas if (asprintf(&cmd, "%s %s", command, path) == -1) {
373 d65a88a2 2021-09-05 stsp err = got_error_from_errno("asprintf");
376 d65a88a2 2021-09-05 stsp len = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
377 d65a88a2 2021-09-05 stsp r = dprintf(fd, "%04x%s%chost=%s%c", len, cmd, '\0', host, '\0');
379 d65a88a2 2021-09-05 stsp err = got_error_from_errno("dprintf");
383 d65a88a2 2021-09-05 stsp if (fd != -1)
386 d65a88a2 2021-09-05 stsp *newfd = fd;
387 d65a88a2 2021-09-05 stsp return err;
390 37e7d69e 2024-04-25 thomas.ad const struct got_error *
391 37e7d69e 2024-04-25 thomas.ad got_dial_http(pid_t *newpid, int *newfd, const char *host,
392 37e7d69e 2024-04-25 thomas.ad const char *port, const char *path, int verbosity, int tls)
394 37e7d69e 2024-04-25 thomas.ad const struct got_error *error = NULL;
395 37e7d69e 2024-04-25 thomas.ad int pid, pfd[2];
396 37e7d69e 2024-04-25 thomas.ad const char *argv[8];
397 37e7d69e 2024-04-25 thomas.ad int i = 0;
399 37e7d69e 2024-04-25 thomas.ad *newpid = -1;
400 37e7d69e 2024-04-25 thomas.ad *newfd = -1;
402 37e7d69e 2024-04-25 thomas.ad if (!port)
403 37e7d69e 2024-04-25 thomas.ad port = tls ? "443" : "80";
405 09876a9d 2024-04-25 thomas.ad argv[i++] = GOT_PATH_PROG_FETCH_HTTP;
406 37e7d69e 2024-04-25 thomas.ad if (verbosity == -1)
407 37e7d69e 2024-04-25 thomas.ad argv[i++] = "-q";
408 37e7d69e 2024-04-25 thomas.ad else if (verbosity > 0)
409 37e7d69e 2024-04-25 thomas.ad argv[i++] = "-v";
410 37e7d69e 2024-04-25 thomas.ad argv[i++] = "--";
411 37e7d69e 2024-04-25 thomas.ad argv[i++] = tls ? "https" : "http";
412 37e7d69e 2024-04-25 thomas.ad argv[i++] = host;
413 37e7d69e 2024-04-25 thomas.ad argv[i++] = port;
414 37e7d69e 2024-04-25 thomas.ad argv[i++] = path;
415 37e7d69e 2024-04-25 thomas.ad argv[i++] = NULL;
416 37e7d69e 2024-04-25 thomas.ad assert(i <= nitems(argv));
418 37e7d69e 2024-04-25 thomas.ad if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pfd) == -1)
419 37e7d69e 2024-04-25 thomas.ad return got_error_from_errno("socketpair");
421 37e7d69e 2024-04-25 thomas.ad pid = fork();
422 37e7d69e 2024-04-25 thomas.ad if (pid == -1) {
423 37e7d69e 2024-04-25 thomas.ad error = got_error_from_errno("fork");
424 37e7d69e 2024-04-25 thomas.ad close(pfd[0]);
425 37e7d69e 2024-04-25 thomas.ad close(pfd[1]);
426 37e7d69e 2024-04-25 thomas.ad return error;
427 37e7d69e 2024-04-25 thomas.ad } else if (pid == 0) {
428 37e7d69e 2024-04-25 thomas.ad if (close(pfd[1]) == -1)
429 37e7d69e 2024-04-25 thomas.ad err(1, "close");
430 37e7d69e 2024-04-25 thomas.ad if (dup2(pfd[0], 0) == -1)
431 37e7d69e 2024-04-25 thomas.ad err(1, "dup2");
432 37e7d69e 2024-04-25 thomas.ad if (dup2(pfd[0], 1) == -1)
433 37e7d69e 2024-04-25 thomas.ad err(1, "dup2");
434 09876a9d 2024-04-25 thomas.ad if (execv(GOT_PATH_PROG_FETCH_HTTP, (char *const *)argv) == -1)
435 37e7d69e 2024-04-25 thomas.ad err(1, "execv");
436 37e7d69e 2024-04-25 thomas.ad abort(); /* not reached */
437 37e7d69e 2024-04-25 thomas.ad } else {
438 37e7d69e 2024-04-25 thomas.ad if (close(pfd[0]) == -1)
439 37e7d69e 2024-04-25 thomas.ad return got_error_from_errno("close");
440 37e7d69e 2024-04-25 thomas.ad *newpid = pid;
441 37e7d69e 2024-04-25 thomas.ad *newfd = pfd[1];
442 37e7d69e 2024-04-25 thomas.ad return NULL;
446 5769f9a0 2023-04-22 thomas const struct got_error *
447 5769f9a0 2023-04-22 thomas got_dial_parse_command(char **command, char **repo_path, const char *gitcmd)
449 5769f9a0 2023-04-22 thomas const struct got_error *err = NULL;
450 5769f9a0 2023-04-22 thomas size_t len, cmdlen, pathlen;
451 5769f9a0 2023-04-22 thomas char *path0 = NULL, *path, *abspath = NULL, *canonpath = NULL;
452 5769f9a0 2023-04-22 thomas const char *relpath;
454 5769f9a0 2023-04-22 thomas *command = NULL;
455 5769f9a0 2023-04-22 thomas *repo_path = NULL;
457 5769f9a0 2023-04-22 thomas len = strlen(gitcmd);
459 5769f9a0 2023-04-22 thomas if (len >= strlen(GOT_DIAL_CMD_SEND) &&
460 5769f9a0 2023-04-22 thomas strncmp(gitcmd, GOT_DIAL_CMD_SEND,
461 5769f9a0 2023-04-22 thomas strlen(GOT_DIAL_CMD_SEND)) == 0)
462 5769f9a0 2023-04-22 thomas cmdlen = strlen(GOT_DIAL_CMD_SEND);
463 5769f9a0 2023-04-22 thomas else if (len >= strlen(GOT_DIAL_CMD_FETCH) &&
464 5769f9a0 2023-04-22 thomas strncmp(gitcmd, GOT_DIAL_CMD_FETCH,
465 5769f9a0 2023-04-22 thomas strlen(GOT_DIAL_CMD_FETCH)) == 0)
466 5769f9a0 2023-04-22 thomas cmdlen = strlen(GOT_DIAL_CMD_FETCH);
468 5769f9a0 2023-04-22 thomas return got_error(GOT_ERR_BAD_PACKET);
470 5769f9a0 2023-04-22 thomas if (len <= cmdlen + 1 || gitcmd[cmdlen] != ' ')
471 5769f9a0 2023-04-22 thomas return got_error(GOT_ERR_BAD_PACKET);
473 5769f9a0 2023-04-22 thomas if (memchr(&gitcmd[cmdlen + 1], '\0', len - cmdlen) == NULL)
474 5769f9a0 2023-04-22 thomas return got_error(GOT_ERR_BAD_PATH);
476 5769f9a0 2023-04-22 thomas /* Forbid linefeeds in paths, like Git does. */
477 5769f9a0 2023-04-22 thomas if (memchr(&gitcmd[cmdlen + 1], '\n', len - cmdlen) != NULL)
478 5769f9a0 2023-04-22 thomas return got_error(GOT_ERR_BAD_PATH);
480 5769f9a0 2023-04-22 thomas path0 = strdup(&gitcmd[cmdlen + 1]);
481 5769f9a0 2023-04-22 thomas if (path0 == NULL)
482 5769f9a0 2023-04-22 thomas return got_error_from_errno("strdup");
483 5769f9a0 2023-04-22 thomas path = path0;
484 5769f9a0 2023-04-22 thomas pathlen = strlen(path);
487 5769f9a0 2023-04-22 thomas * Git clients send a shell command.
488 5769f9a0 2023-04-22 thomas * Trim spaces and quotes around the path.
490 5769f9a0 2023-04-22 thomas while (path[0] == '\'' || path[0] == '\"' || path[0] == ' ') {
492 5769f9a0 2023-04-22 thomas pathlen--;
494 5769f9a0 2023-04-22 thomas while (pathlen > 0 &&
495 5769f9a0 2023-04-22 thomas (path[pathlen - 1] == '\'' || path[pathlen - 1] == '\"' ||
496 5769f9a0 2023-04-22 thomas path[pathlen - 1] == ' ')) {
497 5769f9a0 2023-04-22 thomas path[pathlen - 1] = '\0';
498 5769f9a0 2023-04-22 thomas pathlen--;
501 5769f9a0 2023-04-22 thomas /* Deny an empty repository path. */
502 5769f9a0 2023-04-22 thomas if (path[0] == '\0' || got_path_is_root_dir(path)) {
503 5769f9a0 2023-04-22 thomas err = got_error(GOT_ERR_NOT_GIT_REPO);
504 5769f9a0 2023-04-22 thomas goto done;
507 5769f9a0 2023-04-22 thomas if (asprintf(&abspath, "/%s", path) == -1) {
508 5769f9a0 2023-04-22 thomas err = got_error_from_errno("asprintf");
509 5769f9a0 2023-04-22 thomas goto done;
511 5769f9a0 2023-04-22 thomas pathlen = strlen(abspath);
512 5769f9a0 2023-04-22 thomas canonpath = malloc(pathlen + 1);
513 5769f9a0 2023-04-22 thomas if (canonpath == NULL) {
514 5769f9a0 2023-04-22 thomas err = got_error_from_errno("malloc");
515 5769f9a0 2023-04-22 thomas goto done;
517 5769f9a0 2023-04-22 thomas err = got_canonpath(abspath, canonpath, pathlen + 1);
519 5769f9a0 2023-04-22 thomas goto done;
521 5769f9a0 2023-04-22 thomas relpath = canonpath;
522 5769f9a0 2023-04-22 thomas while (relpath[0] == '/')
523 5769f9a0 2023-04-22 thomas relpath++;
524 5769f9a0 2023-04-22 thomas *repo_path = strdup(relpath);
525 5769f9a0 2023-04-22 thomas if (*repo_path == NULL) {
526 5769f9a0 2023-04-22 thomas err = got_error_from_errno("strdup");
527 5769f9a0 2023-04-22 thomas goto done;
529 5769f9a0 2023-04-22 thomas *command = strndup(gitcmd, cmdlen);
530 5769f9a0 2023-04-22 thomas if (*command == NULL)
531 5769f9a0 2023-04-22 thomas err = got_error_from_errno("strndup");
533 5769f9a0 2023-04-22 thomas free(path0);
534 5769f9a0 2023-04-22 thomas free(abspath);
535 5769f9a0 2023-04-22 thomas free(canonpath);
536 5769f9a0 2023-04-22 thomas if (err) {
537 5769f9a0 2023-04-22 thomas free(*repo_path);
538 5769f9a0 2023-04-22 thomas *repo_path = NULL;
540 5769f9a0 2023-04-22 thomas return err;