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 <sys/queue.h>
19 #include <sys/types.h>
20 #include <sys/socket.h>
29 #include "got_error.h"
32 #include "got_lib_dial.h"
35 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
39 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
42 #ifndef GOT_DIAL_PATH_SSH
43 #define GOT_DIAL_PATH_SSH "/usr/bin/ssh"
47 #define GOT_DEFAULT_GIT_PORT 9418
48 #define GOT_DEFAULT_GIT_PORT_STR "9418"
50 const struct got_error *
51 got_dial_apply_unveil(const char *proto)
53 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
54 if (unveil(GOT_DIAL_PATH_SSH, "x") != 0) {
55 return got_error_from_errno2("unveil",
64 hassuffix(char *base, char *suf)
70 if (ns <= nb && strcmp(base + (nb - ns), suf) == 0)
75 const struct got_error *
76 got_dial_parse_uri(char **proto, char **host, char **port,
77 char **server_path, char **repo_name, const char *uri)
79 const struct got_error *err = NULL;
83 *proto = *host = *port = *server_path = *repo_name = NULL;
85 p = strstr(uri, "://");
87 /* Try parsing Git's "scp" style URL syntax. */
88 *proto = strdup("ssh");
90 err = got_error_from_errno("strdup");
96 err = got_error(GOT_ERR_PARSE_URI);
99 /* No slashes allowed before first colon. */
102 err = got_error(GOT_ERR_PARSE_URI);
105 *host = strndup(s, q - s);
107 err = got_error_from_errno("strndup");
112 *proto = strndup(uri, p - uri);
114 err = got_error_from_errno("strndup");
120 if (p == NULL || strlen(p) == 1) {
121 err = got_error(GOT_ERR_PARSE_URI);
125 q = memchr(s, ':', p - s);
127 *host = strndup(s, q - s);
129 err = got_error_from_errno("strndup");
132 *port = strndup(q + 1, p - (q + 1));
134 err = got_error_from_errno("strndup");
138 *host = strndup(s, p - s);
140 err = got_error_from_errno("strndup");
146 while (p[0] == '/' && p[1] == '/')
148 *server_path = strdup(p);
149 if (*server_path == NULL) {
150 err = got_error_from_errno("strdup");
153 got_path_strip_trailing_slashes(*server_path);
156 if (!p || strlen(p) <= 1) {
157 err = got_error(GOT_ERR_PARSE_URI);
163 err = got_error(GOT_ERR_PARSE_URI);
166 if (hassuffix(p, ".git"))
168 *repo_name = strndup(p, (p + n) - p);
169 if (*repo_name == NULL) {
170 err = got_error_from_errno("strndup");
189 const struct got_error *
190 got_dial_ssh(pid_t *newpid, int *newfd, const char *host,
191 const char *port, const char *path, const char *direction, int verbosity)
193 const struct got_error *error = NULL;
202 argv[i++] = GOT_DIAL_PATH_SSH;
205 argv[i++] = (char *)port;
207 if (verbosity == -1) {
210 /* ssh(1) allows up to 3 "-v" options. */
211 for (j = 0; j < MIN(3, verbosity); j++)
215 argv[i++] = (char *)host;
216 argv[i++] = (char *)cmd;
217 argv[i++] = (char *)path;
220 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pfd) == -1)
221 return got_error_from_errno("socketpair");
225 error = got_error_from_errno("fork");
229 } else if (pid == 0) {
231 if (close(pfd[1]) == -1)
233 if (dup2(pfd[0], 0) == -1)
235 if (dup2(pfd[0], 1) == -1)
237 n = snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
238 if (n < 0 || n >= ssizeof(cmd))
240 if (execv(GOT_DIAL_PATH_SSH, argv) == -1)
242 abort(); /* not reached */
244 if (close(pfd[0]) == -1)
245 return got_error_from_errno("close");
252 const struct got_error *
253 got_dial_git(int *newfd, const char *host, const char *port,
254 const char *path, const char *direction)
256 const struct got_error *err = NULL;
257 struct addrinfo hints, *servinfo, *p;
259 int fd = -1, len, r, eaicode;
264 port = GOT_DEFAULT_GIT_PORT_STR;
266 memset(&hints, 0, sizeof hints);
267 hints.ai_family = AF_UNSPEC;
268 hints.ai_socktype = SOCK_STREAM;
269 eaicode = getaddrinfo(host, port, &hints, &servinfo);
272 snprintf(msg, sizeof(msg), "%s: %s", host,
273 gai_strerror(eaicode));
274 return got_error_msg(GOT_ERR_ADDRINFO, msg);
277 for (p = servinfo; p != NULL; p = p->ai_next) {
278 if ((fd = socket(p->ai_family, p->ai_socktype,
279 p->ai_protocol)) == -1)
281 if (connect(fd, p->ai_addr, p->ai_addrlen) == 0) {
285 err = got_error_from_errno("connect");
291 if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
292 err = got_error_from_errno("asprintf");
295 len = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
296 r = dprintf(fd, "%04x%s%chost=%s%c", len, cmd, '\0', host, '\0');
298 err = got_error_from_errno("dprintf");