2 * Copyright (c) 2018, 2019 Ori Bernstein <ori@openbsd.org>
3 * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include "got_compat.h"
20 #include <sys/queue.h>
21 #include <sys/types.h>
22 #include <sys/socket.h>
36 #include "got_error.h"
38 #include "got_object.h"
40 #include "got_compat.h"
42 #include "got_lib_dial.h"
43 #include "got_lib_delta.h"
44 #include "got_lib_hash.h"
45 #include "got_lib_object.h"
46 #include "got_lib_privsep.h"
50 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
58 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 #ifndef GOT_DIAL_PATH_SSH
62 #define GOT_DIAL_PATH_SSH "/usr/bin/ssh"
66 #define GOT_DEFAULT_GIT_PORT 9418
67 #define GOT_DEFAULT_GIT_PORT_STR "9418"
69 const struct got_error
*
70 got_dial_apply_unveil(const char *proto
)
72 if (strcmp(proto
, "git+ssh") == 0 || strcmp(proto
, "ssh") == 0) {
73 if (unveil(GOT_DIAL_PATH_SSH
, "x") != 0) {
74 return got_error_from_errno2("unveil",
79 if (strstr(proto
, "http") != NULL
) {
80 if (unveil(GOT_PATH_PROG_FETCH_HTTP
, "x") != 0) {
81 return got_error_from_errno2("unveil",
82 GOT_PATH_PROG_FETCH_HTTP
);
90 hassuffix(const char *base
, const char *suf
)
96 if (ns
<= nb
&& strcmp(base
+ (nb
- ns
), suf
) == 0)
101 const struct got_error
*
102 got_dial_parse_uri(char **proto
, char **host
, char **port
,
103 char **server_path
, char **repo_name
, const char *uri
)
105 const struct got_error
*err
= NULL
;
108 *proto
= *host
= *port
= *server_path
= *repo_name
= NULL
;
110 p
= strstr(uri
, "://");
112 /* Try parsing Git's "scp" style URL syntax. */
113 *proto
= strdup("ssh");
114 if (*proto
== NULL
) {
115 err
= got_error_from_errno("strdup");
121 err
= got_error(GOT_ERR_PARSE_URI
);
124 /* No slashes allowed before first colon. */
127 err
= got_error(GOT_ERR_PARSE_URI
);
130 *host
= strndup(s
, q
- s
);
132 err
= got_error_from_errno("strndup");
135 if ((*host
)[0] == '\0') {
136 err
= got_error(GOT_ERR_PARSE_URI
);
141 *proto
= strndup(uri
, p
- uri
);
142 if (*proto
== NULL
) {
143 err
= got_error_from_errno("strndup");
149 if (p
== NULL
|| strlen(p
) == 1) {
150 err
= got_error(GOT_ERR_PARSE_URI
);
154 q
= memchr(s
, ':', p
- s
);
156 *host
= strndup(s
, q
- s
);
158 err
= got_error_from_errno("strndup");
161 if ((*host
)[0] == '\0') {
162 err
= got_error(GOT_ERR_PARSE_URI
);
165 *port
= strndup(q
+ 1, p
- (q
+ 1));
167 err
= got_error_from_errno("strndup");
170 if ((*port
)[0] == '\0') {
171 err
= got_error(GOT_ERR_PARSE_URI
);
175 *host
= strndup(s
, p
- s
);
177 err
= got_error_from_errno("strndup");
180 if ((*host
)[0] == '\0') {
181 err
= got_error(GOT_ERR_PARSE_URI
);
187 while (p
[0] == '/' && (p
[1] == '/' || p
[1] == '~'))
189 *server_path
= strdup(p
);
190 if (*server_path
== NULL
) {
191 err
= got_error_from_errno("strdup");
194 got_path_strip_trailing_slashes(*server_path
);
195 if ((*server_path
)[0] == '\0') {
196 err
= got_error(GOT_ERR_PARSE_URI
);
200 err
= got_path_basename(repo_name
, *server_path
);
203 if (hassuffix(*repo_name
, ".git"))
204 (*repo_name
)[strlen(*repo_name
) - 4] = '\0';
205 if ((*repo_name
)[0] == '\0')
206 err
= got_error(GOT_ERR_PARSE_URI
);
224 * Escape a given path for the shell which will be started by sshd.
225 * In particular, git-shell is known to require single-quote characters
226 * around its repository path argument and will refuse to run otherwise.
228 static const struct got_error
*
229 escape_path(char *buf
, size_t bufsize
, const char *path
)
240 while (*p
!= '\0' && (q
- buf
< bufsize
)) {
241 /* git escapes ! too */
242 if (*p
!= '\'' && *p
!= '!') {
247 if (q
- buf
+ 4 >= bufsize
)
255 if (*p
== '\0' && (q
- buf
+ 1 < bufsize
)) {
261 return got_error_fmt(GOT_ERR_NO_SPACE
, "overlong path: %s", path
);
264 const struct got_error
*
265 got_dial_ssh(pid_t
*newpid
, int *newfd
, const char *host
,
266 const char *port
, const char *path
, const char *jumphost
,
267 const char *identity_file
, const char *command
, int verbosity
)
269 const struct got_error
*error
= NULL
;
272 char escaped_path
[PATH_MAX
];
273 const char *argv
[15];
279 error
= escape_path(escaped_path
, sizeof(escaped_path
), path
);
283 argv
[i
++] = GOT_DIAL_PATH_SSH
;
286 argv
[i
++] = (char *)port
;
288 if (verbosity
<= 0) {
290 } else if (verbosity
> 1) {
291 /* ssh(1) allows up to 3 "-v" options. */
292 for (j
= 0; j
< MIN(3, verbosity
); j
++)
297 argv
[i
++] = identity_file
;
301 argv
[i
++] = jumphost
;
304 argv
[i
++] = (char *)host
;
305 argv
[i
++] = (char *)cmd
;
306 argv
[i
++] = (char *)escaped_path
;
308 assert(i
<= nitems(argv
));
310 if (socketpair(AF_UNIX
, SOCK_STREAM
, PF_UNSPEC
, pfd
) == -1)
311 return got_error_from_errno("socketpair");
315 error
= got_error_from_errno("fork");
319 } else if (pid
== 0) {
320 if (close(pfd
[1]) == -1)
322 if (dup2(pfd
[0], 0) == -1)
324 if (dup2(pfd
[0], 1) == -1)
326 if (strlcpy(cmd
, command
, sizeof(cmd
)) >= sizeof(cmd
))
328 if (execv(GOT_DIAL_PATH_SSH
, (char *const *)argv
) == -1)
329 err(1, "execv %s", GOT_DIAL_PATH_SSH
);
330 abort(); /* not reached */
332 if (close(pfd
[0]) == -1)
333 return got_error_from_errno("close");
340 const struct got_error
*
341 got_dial_git(int *newfd
, const char *host
, const char *port
,
342 const char *path
, const char *command
)
344 const struct got_error
*err
= NULL
;
345 struct addrinfo hints
, *servinfo
, *p
;
347 int fd
= -1, len
, r
, eaicode
;
352 port
= GOT_DEFAULT_GIT_PORT_STR
;
354 memset(&hints
, 0, sizeof hints
);
355 hints
.ai_family
= AF_UNSPEC
;
356 hints
.ai_socktype
= SOCK_STREAM
;
357 eaicode
= getaddrinfo(host
, port
, &hints
, &servinfo
);
360 snprintf(msg
, sizeof(msg
), "%s: %s", host
,
361 gai_strerror(eaicode
));
362 return got_error_msg(GOT_ERR_ADDRINFO
, msg
);
365 for (p
= servinfo
; p
!= NULL
; p
= p
->ai_next
) {
366 if ((fd
= socket(p
->ai_family
, p
->ai_socktype
,
367 p
->ai_protocol
)) == -1)
369 if (connect(fd
, p
->ai_addr
, p
->ai_addrlen
) == 0) {
373 err
= got_error_from_errno("connect");
376 freeaddrinfo(servinfo
);
380 if (asprintf(&cmd
, "%s %s", command
, path
) == -1) {
381 err
= got_error_from_errno("asprintf");
384 len
= 4 + strlen(cmd
) + 1 + strlen("host=") + strlen(host
) + 1;
385 r
= dprintf(fd
, "%04x%s%chost=%s%c", len
, cmd
, '\0', host
, '\0');
387 err
= got_error_from_errno("dprintf");
398 const struct got_error
*
399 got_dial_http(pid_t
*newpid
, int *newfd
, const char *host
,
400 const char *port
, const char *path
, int verbosity
, int tls
)
402 const struct got_error
*error
= NULL
;
411 port
= tls
? "443" : "80";
413 argv
[i
++] = GOT_PATH_PROG_FETCH_HTTP
;
416 else if (verbosity
> 0)
419 argv
[i
++] = tls
? "https" : "http";
424 assert(i
<= nitems(argv
));
426 if (socketpair(AF_UNIX
, SOCK_STREAM
, PF_UNSPEC
, pfd
) == -1)
427 return got_error_from_errno("socketpair");
431 error
= got_error_from_errno("fork");
435 } else if (pid
== 0) {
436 if (close(pfd
[1]) == -1)
438 if (dup2(pfd
[0], 0) == -1)
440 if (dup2(pfd
[0], 1) == -1)
442 if (execv(GOT_PATH_PROG_FETCH_HTTP
, (char *const *)argv
) == -1)
443 err(1, "execv %s", GOT_PATH_PROG_FETCH_HTTP
);
444 abort(); /* not reached */
446 if (close(pfd
[0]) == -1)
447 return got_error_from_errno("close");
454 const struct got_error
*
455 got_dial_parse_command(char **command
, char **repo_path
, const char *gitcmd
)
457 const struct got_error
*err
= NULL
;
458 size_t len
, cmdlen
, pathlen
;
459 char *path0
= NULL
, *path
, *abspath
= NULL
, *canonpath
= NULL
;
465 len
= strlen(gitcmd
);
467 if (len
>= strlen(GOT_DIAL_CMD_SEND
) &&
468 strncmp(gitcmd
, GOT_DIAL_CMD_SEND
,
469 strlen(GOT_DIAL_CMD_SEND
)) == 0)
470 cmdlen
= strlen(GOT_DIAL_CMD_SEND
);
471 else if (len
>= strlen(GOT_DIAL_CMD_FETCH
) &&
472 strncmp(gitcmd
, GOT_DIAL_CMD_FETCH
,
473 strlen(GOT_DIAL_CMD_FETCH
)) == 0)
474 cmdlen
= strlen(GOT_DIAL_CMD_FETCH
);
476 return got_error(GOT_ERR_BAD_PACKET
);
478 if (len
<= cmdlen
+ 1 || gitcmd
[cmdlen
] != ' ')
479 return got_error(GOT_ERR_BAD_PACKET
);
481 if (memchr(&gitcmd
[cmdlen
+ 1], '\0', len
- cmdlen
) == NULL
)
482 return got_error(GOT_ERR_BAD_PATH
);
484 /* Forbid linefeeds in paths, like Git does. */
485 if (memchr(&gitcmd
[cmdlen
+ 1], '\n', len
- cmdlen
) != NULL
)
486 return got_error(GOT_ERR_BAD_PATH
);
488 path0
= strdup(&gitcmd
[cmdlen
+ 1]);
490 return got_error_from_errno("strdup");
492 pathlen
= strlen(path
);
495 * Git clients send a shell command.
496 * Trim spaces and quotes around the path.
498 while (path
[0] == '\'' || path
[0] == '\"' || path
[0] == ' ') {
502 while (pathlen
> 0 &&
503 (path
[pathlen
- 1] == '\'' || path
[pathlen
- 1] == '\"' ||
504 path
[pathlen
- 1] == ' ')) {
505 path
[pathlen
- 1] = '\0';
509 /* Deny an empty repository path. */
510 if (path
[0] == '\0' || got_path_is_root_dir(path
)) {
511 err
= got_error(GOT_ERR_NOT_GIT_REPO
);
515 if (asprintf(&abspath
, "/%s", path
) == -1) {
516 err
= got_error_from_errno("asprintf");
519 pathlen
= strlen(abspath
);
520 canonpath
= malloc(pathlen
+ 1);
521 if (canonpath
== NULL
) {
522 err
= got_error_from_errno("malloc");
525 err
= got_canonpath(abspath
, canonpath
, pathlen
+ 1);
530 while (relpath
[0] == '/')
532 *repo_path
= strdup(relpath
);
533 if (*repo_path
== NULL
) {
534 err
= got_error_from_errno("strdup");
537 *command
= strndup(gitcmd
, cmdlen
);
538 if (*command
== NULL
)
539 err
= got_error_from_errno("strndup");