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>
30 #include "got_error.h"
33 #include "got_lib_dial.h"
36 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
40 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
44 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
47 #ifndef GOT_DIAL_PATH_SSH
48 #define GOT_DIAL_PATH_SSH "/usr/bin/ssh"
52 #define GOT_DEFAULT_GIT_PORT 9418
53 #define GOT_DEFAULT_GIT_PORT_STR "9418"
55 const struct got_error *
56 got_dial_apply_unveil(const char *proto)
58 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
59 if (unveil(GOT_DIAL_PATH_SSH, "x") != 0) {
60 return got_error_from_errno2("unveil",
69 hassuffix(char *base, char *suf)
75 if (ns <= nb && strcmp(base + (nb - ns), suf) == 0)
80 const struct got_error *
81 got_dial_parse_uri(char **proto, char **host, char **port,
82 char **server_path, char **repo_name, const char *uri)
84 const struct got_error *err = NULL;
87 *proto = *host = *port = *server_path = *repo_name = NULL;
89 p = strstr(uri, "://");
91 /* Try parsing Git's "scp" style URL syntax. */
92 *proto = strdup("ssh");
94 err = got_error_from_errno("strdup");
100 err = got_error(GOT_ERR_PARSE_URI);
103 /* No slashes allowed before first colon. */
106 err = got_error(GOT_ERR_PARSE_URI);
109 *host = strndup(s, q - s);
111 err = got_error_from_errno("strndup");
114 if ((*host)[0] == '\0') {
115 err = got_error(GOT_ERR_PARSE_URI);
120 *proto = strndup(uri, p - uri);
121 if (*proto == NULL) {
122 err = got_error_from_errno("strndup");
128 if (p == NULL || strlen(p) == 1) {
129 err = got_error(GOT_ERR_PARSE_URI);
133 q = memchr(s, ':', p - s);
135 *host = strndup(s, q - s);
137 err = got_error_from_errno("strndup");
140 if ((*host)[0] == '\0') {
141 err = got_error(GOT_ERR_PARSE_URI);
144 *port = strndup(q + 1, p - (q + 1));
146 err = got_error_from_errno("strndup");
149 if ((*port)[0] == '\0') {
150 err = got_error(GOT_ERR_PARSE_URI);
154 *host = strndup(s, p - s);
156 err = got_error_from_errno("strndup");
159 if ((*host)[0] == '\0') {
160 err = got_error(GOT_ERR_PARSE_URI);
166 while (p[0] == '/' && p[1] == '/')
168 *server_path = strdup(p);
169 if (*server_path == NULL) {
170 err = got_error_from_errno("strdup");
173 got_path_strip_trailing_slashes(*server_path);
174 if ((*server_path)[0] == '\0') {
175 err = got_error(GOT_ERR_PARSE_URI);
179 err = got_path_basename(repo_name, *server_path);
182 if (hassuffix(*repo_name, ".git"))
183 (*repo_name)[strlen(*repo_name) - 4] = '\0';
184 if ((*repo_name)[0] == '\0')
185 err = got_error(GOT_ERR_PARSE_URI);
202 const struct got_error *
203 got_dial_ssh(pid_t *newpid, int *newfd, const char *host,
204 const char *port, const char *path, const char *direction, int verbosity)
206 const struct got_error *error = NULL;
215 argv[i++] = GOT_DIAL_PATH_SSH;
218 argv[i++] = (char *)port;
220 if (verbosity == -1) {
223 /* ssh(1) allows up to 3 "-v" options. */
224 for (j = 0; j < MIN(3, verbosity); j++)
228 argv[i++] = (char *)host;
229 argv[i++] = (char *)cmd;
230 argv[i++] = (char *)path;
232 assert(i <= nitems(argv));
234 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pfd) == -1)
235 return got_error_from_errno("socketpair");
239 error = got_error_from_errno("fork");
243 } else if (pid == 0) {
245 if (close(pfd[1]) == -1)
247 if (dup2(pfd[0], 0) == -1)
249 if (dup2(pfd[0], 1) == -1)
251 n = snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
252 if (n < 0 || n >= ssizeof(cmd))
254 if (execv(GOT_DIAL_PATH_SSH, argv) == -1)
256 abort(); /* not reached */
258 if (close(pfd[0]) == -1)
259 return got_error_from_errno("close");
266 const struct got_error *
267 got_dial_git(int *newfd, const char *host, const char *port,
268 const char *path, const char *direction)
270 const struct got_error *err = NULL;
271 struct addrinfo hints, *servinfo, *p;
273 int fd = -1, len, r, eaicode;
278 port = GOT_DEFAULT_GIT_PORT_STR;
280 memset(&hints, 0, sizeof hints);
281 hints.ai_family = AF_UNSPEC;
282 hints.ai_socktype = SOCK_STREAM;
283 eaicode = getaddrinfo(host, port, &hints, &servinfo);
286 snprintf(msg, sizeof(msg), "%s: %s", host,
287 gai_strerror(eaicode));
288 return got_error_msg(GOT_ERR_ADDRINFO, msg);
291 for (p = servinfo; p != NULL; p = p->ai_next) {
292 if ((fd = socket(p->ai_family, p->ai_socktype,
293 p->ai_protocol)) == -1)
295 if (connect(fd, p->ai_addr, p->ai_addrlen) == 0) {
299 err = got_error_from_errno("connect");
305 if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
306 err = got_error_from_errno("asprintf");
309 len = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
310 r = dprintf(fd, "%04x%s%chost=%s%c", len, cmd, '\0', host, '\0');
312 err = got_error_from_errno("dprintf");