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/types.h>
19 #include <sys/socket.h>
29 #include "got_error.h"
32 #include "got_compat.h"
34 #include "got_lib_dial.h"
38 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
42 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
46 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
49 #ifndef GOT_DIAL_PATH_SSH
50 #define GOT_DIAL_PATH_SSH "/usr/bin/ssh"
54 #define GOT_DEFAULT_GIT_PORT 9418
55 #define GOT_DEFAULT_GIT_PORT_STR "9418"
57 const struct got_error *
58 got_dial_apply_unveil(const char *proto)
60 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
61 if (unveil(GOT_DIAL_PATH_SSH, "x") != 0) {
62 return got_error_from_errno2("unveil",
71 hassuffix(const char *base, const char *suf)
77 if (ns <= nb && strcmp(base + (nb - ns), suf) == 0)
82 const struct got_error *
83 got_dial_parse_uri(char **proto, char **host, char **port,
84 char **server_path, char **repo_name, const char *uri)
86 const struct got_error *err = NULL;
89 *proto = *host = *port = *server_path = *repo_name = NULL;
91 p = strstr(uri, "://");
93 /* Try parsing Git's "scp" style URL syntax. */
94 *proto = strdup("ssh");
96 err = got_error_from_errno("strdup");
102 err = got_error(GOT_ERR_PARSE_URI);
105 /* No slashes allowed before first colon. */
108 err = got_error(GOT_ERR_PARSE_URI);
111 *host = strndup(s, q - s);
113 err = got_error_from_errno("strndup");
116 if ((*host)[0] == '\0') {
117 err = got_error(GOT_ERR_PARSE_URI);
122 *proto = strndup(uri, p - uri);
123 if (*proto == NULL) {
124 err = got_error_from_errno("strndup");
130 if (p == NULL || strlen(p) == 1) {
131 err = got_error(GOT_ERR_PARSE_URI);
135 q = memchr(s, ':', p - s);
137 *host = strndup(s, q - s);
139 err = got_error_from_errno("strndup");
142 if ((*host)[0] == '\0') {
143 err = got_error(GOT_ERR_PARSE_URI);
146 *port = strndup(q + 1, p - (q + 1));
148 err = got_error_from_errno("strndup");
151 if ((*port)[0] == '\0') {
152 err = got_error(GOT_ERR_PARSE_URI);
156 *host = strndup(s, p - s);
158 err = got_error_from_errno("strndup");
161 if ((*host)[0] == '\0') {
162 err = got_error(GOT_ERR_PARSE_URI);
168 while (p[0] == '/' && p[1] == '/')
170 *server_path = strdup(p);
171 if (*server_path == NULL) {
172 err = got_error_from_errno("strdup");
175 got_path_strip_trailing_slashes(*server_path);
176 if ((*server_path)[0] == '\0') {
177 err = got_error(GOT_ERR_PARSE_URI);
181 err = got_path_basename(repo_name, *server_path);
184 if (hassuffix(*repo_name, ".git"))
185 (*repo_name)[strlen(*repo_name) - 4] = '\0';
186 if ((*repo_name)[0] == '\0')
187 err = got_error(GOT_ERR_PARSE_URI);
204 const struct got_error *
205 got_dial_ssh(pid_t *newpid, int *newfd, const char *host,
206 const char *port, const char *path, const char *direction, int verbosity)
208 const struct got_error *error = NULL;
211 const char *argv[11];
217 argv[i++] = GOT_DIAL_PATH_SSH;
220 argv[i++] = (char *)port;
222 if (verbosity == -1) {
225 /* ssh(1) allows up to 3 "-v" options. */
226 for (j = 0; j < MIN(3, verbosity); j++)
230 argv[i++] = (char *)host;
231 argv[i++] = (char *)cmd;
232 argv[i++] = (char *)path;
234 assert(i <= nitems(argv));
236 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pfd) == -1)
237 return got_error_from_errno("socketpair");
241 error = got_error_from_errno("fork");
245 } else if (pid == 0) {
247 if (close(pfd[1]) == -1)
249 if (dup2(pfd[0], 0) == -1)
251 if (dup2(pfd[0], 1) == -1)
253 n = snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
254 if (n < 0 || n >= ssizeof(cmd))
256 if (execv(GOT_DIAL_PATH_SSH, (char *const *)argv) == -1)
258 abort(); /* not reached */
260 if (close(pfd[0]) == -1)
261 return got_error_from_errno("close");
268 const struct got_error *
269 got_dial_git(int *newfd, const char *host, const char *port,
270 const char *path, const char *direction)
272 const struct got_error *err = NULL;
273 struct addrinfo hints, *servinfo, *p;
275 int fd = -1, len, r, eaicode;
280 port = GOT_DEFAULT_GIT_PORT_STR;
282 memset(&hints, 0, sizeof hints);
283 hints.ai_family = AF_UNSPEC;
284 hints.ai_socktype = SOCK_STREAM;
285 eaicode = getaddrinfo(host, port, &hints, &servinfo);
288 snprintf(msg, sizeof(msg), "%s: %s", host,
289 gai_strerror(eaicode));
290 return got_error_msg(GOT_ERR_ADDRINFO, msg);
293 for (p = servinfo; p != NULL; p = p->ai_next) {
294 if ((fd = socket(p->ai_family, p->ai_socktype,
295 p->ai_protocol)) == -1)
297 if (connect(fd, p->ai_addr, p->ai_addrlen) == 0) {
301 err = got_error_from_errno("connect");
307 if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
308 err = got_error_from_errno("asprintf");
311 len = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
312 r = dprintf(fd, "%04x%s%chost=%s%c", len, cmd, '\0', host, '\0');
314 err = got_error_from_errno("dprintf");