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>
22 #include <sys/types.h>
23 #include <sys/socket.h>
37 #include "got_error.h"
39 #include "got_object.h"
41 #include "got_compat.h"
43 #include "got_lib_dial.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_hash.h"
46 #include "got_lib_object.h"
47 #include "got_lib_privsep.h"
51 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
55 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 #ifndef GOT_DIAL_PATH_SSH
63 #define GOT_DIAL_PATH_SSH "/usr/bin/ssh"
67 #define GOT_DEFAULT_GIT_PORT 9418
68 #define GOT_DEFAULT_GIT_PORT_STR "9418"
70 const struct got_error
*
71 got_dial_apply_unveil(const char *proto
)
73 if (strcmp(proto
, "git+ssh") == 0 || strcmp(proto
, "ssh") == 0) {
74 if (unveil(GOT_DIAL_PATH_SSH
, "x") != 0) {
75 return got_error_from_errno2("unveil",
80 if (strstr(proto
, "http") != NULL
) {
81 if (unveil(GOT_PATH_PROG_FETCH_HTTP
, "x") != 0) {
82 return got_error_from_errno2("unveil",
83 GOT_PATH_PROG_FETCH_HTTP
);
91 hassuffix(const char *base
, const char *suf
)
97 if (ns
<= nb
&& strcmp(base
+ (nb
- ns
), suf
) == 0)
102 const struct got_error
*
103 got_dial_parse_uri(char **proto
, char **host
, char **port
,
104 char **server_path
, char **repo_name
, const char *uri
)
106 const struct got_error
*err
= NULL
;
109 *proto
= *host
= *port
= *server_path
= *repo_name
= NULL
;
111 p
= strstr(uri
, "://");
113 /* Try parsing Git's "scp" style URL syntax. */
114 *proto
= strdup("ssh");
115 if (*proto
== NULL
) {
116 err
= got_error_from_errno("strdup");
122 err
= got_error(GOT_ERR_PARSE_URI
);
125 /* No slashes allowed before first colon. */
128 err
= got_error(GOT_ERR_PARSE_URI
);
131 *host
= strndup(s
, q
- s
);
133 err
= got_error_from_errno("strndup");
136 if ((*host
)[0] == '\0') {
137 err
= got_error(GOT_ERR_PARSE_URI
);
142 *proto
= strndup(uri
, p
- uri
);
143 if (*proto
== NULL
) {
144 err
= got_error_from_errno("strndup");
150 if (p
== NULL
|| strlen(p
) == 1) {
151 err
= got_error(GOT_ERR_PARSE_URI
);
155 q
= memchr(s
, ':', p
- s
);
157 *host
= strndup(s
, q
- s
);
159 err
= got_error_from_errno("strndup");
162 if ((*host
)[0] == '\0') {
163 err
= got_error(GOT_ERR_PARSE_URI
);
166 *port
= strndup(q
+ 1, p
- (q
+ 1));
168 err
= got_error_from_errno("strndup");
171 if ((*port
)[0] == '\0') {
172 err
= got_error(GOT_ERR_PARSE_URI
);
176 *host
= strndup(s
, p
- s
);
178 err
= got_error_from_errno("strndup");
181 if ((*host
)[0] == '\0') {
182 err
= got_error(GOT_ERR_PARSE_URI
);
188 while (p
[0] == '/' && (p
[1] == '/' || p
[1] == '~'))
190 *server_path
= strdup(p
);
191 if (*server_path
== NULL
) {
192 err
= got_error_from_errno("strdup");
195 got_path_strip_trailing_slashes(*server_path
);
196 if ((*server_path
)[0] == '\0') {
197 err
= got_error(GOT_ERR_PARSE_URI
);
201 err
= got_path_basename(repo_name
, *server_path
);
204 if (hassuffix(*repo_name
, ".git"))
205 (*repo_name
)[strlen(*repo_name
) - 4] = '\0';
206 if ((*repo_name
)[0] == '\0')
207 err
= got_error(GOT_ERR_PARSE_URI
);
225 * Escape a given path for the shell which will be started by sshd.
226 * In particular, git-shell is known to require single-quote characters
227 * around its repository path argument and will refuse to run otherwise.
229 static const struct got_error
*
230 escape_path(char *buf
, size_t bufsize
, const char *path
)
241 while (*p
!= '\0' && (q
- buf
< bufsize
)) {
242 /* git escapes ! too */
243 if (*p
!= '\'' && *p
!= '!') {
248 if (q
- buf
+ 4 >= bufsize
)
256 if (*p
== '\0' && (q
- buf
+ 1 < bufsize
)) {
262 return got_error_fmt(GOT_ERR_NO_SPACE
, "overlong path: %s", path
);
265 const struct got_error
*
266 got_dial_ssh(pid_t
*newpid
, int *newfd
, const char *host
,
267 const char *port
, const char *path
, const char *jumphost
,
268 const char *command
, int verbosity
)
270 const struct got_error
*error
= NULL
;
273 char escaped_path
[PATH_MAX
];
274 const char *argv
[13];
280 error
= escape_path(escaped_path
, sizeof(escaped_path
), path
);
284 argv
[i
++] = GOT_DIAL_PATH_SSH
;
287 argv
[i
++] = (char *)port
;
289 if (verbosity
<= 0) {
291 } else if (verbosity
> 1) {
292 /* ssh(1) allows up to 3 "-v" options. */
293 for (j
= 0; j
< MIN(3, verbosity
); j
++)
298 argv
[i
++] = jumphost
;
301 argv
[i
++] = (char *)host
;
302 argv
[i
++] = (char *)cmd
;
303 argv
[i
++] = (char *)escaped_path
;
305 assert(i
<= nitems(argv
));
307 if (socketpair(AF_UNIX
, SOCK_STREAM
, PF_UNSPEC
, pfd
) == -1)
308 return got_error_from_errno("socketpair");
312 error
= got_error_from_errno("fork");
316 } else if (pid
== 0) {
317 if (close(pfd
[1]) == -1)
319 if (dup2(pfd
[0], 0) == -1)
321 if (dup2(pfd
[0], 1) == -1)
323 if (strlcpy(cmd
, command
, sizeof(cmd
)) >= sizeof(cmd
))
325 if (execv(GOT_DIAL_PATH_SSH
, (char *const *)argv
) == -1)
326 err(1, "execv %s", GOT_DIAL_PATH_SSH
);
327 abort(); /* not reached */
329 if (close(pfd
[0]) == -1)
330 return got_error_from_errno("close");
337 const struct got_error
*
338 got_dial_git(int *newfd
, const char *host
, const char *port
,
339 const char *path
, const char *command
)
341 const struct got_error
*err
= NULL
;
342 struct addrinfo hints
, *servinfo
, *p
;
344 int fd
= -1, len
, r
, eaicode
;
349 port
= GOT_DEFAULT_GIT_PORT_STR
;
351 memset(&hints
, 0, sizeof hints
);
352 hints
.ai_family
= AF_UNSPEC
;
353 hints
.ai_socktype
= SOCK_STREAM
;
354 eaicode
= getaddrinfo(host
, port
, &hints
, &servinfo
);
357 snprintf(msg
, sizeof(msg
), "%s: %s", host
,
358 gai_strerror(eaicode
));
359 return got_error_msg(GOT_ERR_ADDRINFO
, msg
);
362 for (p
= servinfo
; p
!= NULL
; p
= p
->ai_next
) {
363 if ((fd
= socket(p
->ai_family
, p
->ai_socktype
,
364 p
->ai_protocol
)) == -1)
366 if (connect(fd
, p
->ai_addr
, p
->ai_addrlen
) == 0) {
370 err
= got_error_from_errno("connect");
373 freeaddrinfo(servinfo
);
377 if (asprintf(&cmd
, "%s %s", command
, path
) == -1) {
378 err
= got_error_from_errno("asprintf");
381 len
= 4 + strlen(cmd
) + 1 + strlen("host=") + strlen(host
) + 1;
382 r
= dprintf(fd
, "%04x%s%chost=%s%c", len
, cmd
, '\0', host
, '\0');
384 err
= got_error_from_errno("dprintf");
395 const struct got_error
*
396 got_dial_http(pid_t
*newpid
, int *newfd
, const char *host
,
397 const char *port
, const char *path
, int verbosity
, int tls
)
399 const struct got_error
*error
= NULL
;
408 port
= tls
? "443" : "80";
410 argv
[i
++] = GOT_PATH_PROG_FETCH_HTTP
;
413 else if (verbosity
> 0)
416 argv
[i
++] = tls
? "https" : "http";
421 assert(i
<= nitems(argv
));
423 if (socketpair(AF_UNIX
, SOCK_STREAM
, PF_UNSPEC
, pfd
) == -1)
424 return got_error_from_errno("socketpair");
428 error
= got_error_from_errno("fork");
432 } else if (pid
== 0) {
433 if (close(pfd
[1]) == -1)
435 if (dup2(pfd
[0], 0) == -1)
437 if (dup2(pfd
[0], 1) == -1)
439 if (execv(GOT_PATH_PROG_FETCH_HTTP
, (char *const *)argv
) == -1)
440 err(1, "execv %s", GOT_PATH_PROG_FETCH_HTTP
);
441 abort(); /* not reached */
443 if (close(pfd
[0]) == -1)
444 return got_error_from_errno("close");
451 const struct got_error
*
452 got_dial_parse_command(char **command
, char **repo_path
, const char *gitcmd
)
454 const struct got_error
*err
= NULL
;
455 size_t len
, cmdlen
, pathlen
;
456 char *path0
= NULL
, *path
, *abspath
= NULL
, *canonpath
= NULL
;
462 len
= strlen(gitcmd
);
464 if (len
>= strlen(GOT_DIAL_CMD_SEND
) &&
465 strncmp(gitcmd
, GOT_DIAL_CMD_SEND
,
466 strlen(GOT_DIAL_CMD_SEND
)) == 0)
467 cmdlen
= strlen(GOT_DIAL_CMD_SEND
);
468 else if (len
>= strlen(GOT_DIAL_CMD_FETCH
) &&
469 strncmp(gitcmd
, GOT_DIAL_CMD_FETCH
,
470 strlen(GOT_DIAL_CMD_FETCH
)) == 0)
471 cmdlen
= strlen(GOT_DIAL_CMD_FETCH
);
473 return got_error(GOT_ERR_BAD_PACKET
);
475 if (len
<= cmdlen
+ 1 || gitcmd
[cmdlen
] != ' ')
476 return got_error(GOT_ERR_BAD_PACKET
);
478 if (memchr(&gitcmd
[cmdlen
+ 1], '\0', len
- cmdlen
) == NULL
)
479 return got_error(GOT_ERR_BAD_PATH
);
481 /* Forbid linefeeds in paths, like Git does. */
482 if (memchr(&gitcmd
[cmdlen
+ 1], '\n', len
- cmdlen
) != NULL
)
483 return got_error(GOT_ERR_BAD_PATH
);
485 path0
= strdup(&gitcmd
[cmdlen
+ 1]);
487 return got_error_from_errno("strdup");
489 pathlen
= strlen(path
);
492 * Git clients send a shell command.
493 * Trim spaces and quotes around the path.
495 while (path
[0] == '\'' || path
[0] == '\"' || path
[0] == ' ') {
499 while (pathlen
> 0 &&
500 (path
[pathlen
- 1] == '\'' || path
[pathlen
- 1] == '\"' ||
501 path
[pathlen
- 1] == ' ')) {
502 path
[pathlen
- 1] = '\0';
506 /* Deny an empty repository path. */
507 if (path
[0] == '\0' || got_path_is_root_dir(path
)) {
508 err
= got_error(GOT_ERR_NOT_GIT_REPO
);
512 if (asprintf(&abspath
, "/%s", path
) == -1) {
513 err
= got_error_from_errno("asprintf");
516 pathlen
= strlen(abspath
);
517 canonpath
= malloc(pathlen
+ 1);
518 if (canonpath
== NULL
) {
519 err
= got_error_from_errno("malloc");
522 err
= got_canonpath(abspath
, canonpath
, pathlen
+ 1);
527 while (relpath
[0] == '/')
529 *repo_path
= strdup(relpath
);
530 if (*repo_path
== NULL
) {
531 err
= got_error_from_errno("strdup");
534 *command
= strndup(gitcmd
, cmdlen
);
535 if (*command
== NULL
)
536 err
= got_error_from_errno("strndup");