Blob


1 /*
2 * Copyright (c) 2018, 2019 Ori Bernstein <ori@openbsd.org>
3 * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
4 *
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.
8 *
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.
16 */
18 #include <sys/queue.h>
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <netdb.h>
23 #include <err.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
29 #include "got_error.h"
30 #include "got_path.h"
32 #include "got_lib_dial.h"
34 #ifndef ssizeof
35 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
36 #endif
38 #ifndef MIN
39 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
40 #endif
42 #ifndef GOT_DIAL_PATH_SSH
43 #define GOT_DIAL_PATH_SSH "/usr/bin/ssh"
44 #endif
46 /* IANA assigned */
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)
52 {
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",
56 GOT_DIAL_PATH_SSH);
57 }
58 }
60 return NULL;
61 }
63 static int
64 hassuffix(char *base, char *suf)
65 {
66 int nb, ns;
68 nb = strlen(base);
69 ns = strlen(suf);
70 if (ns <= nb && strcmp(base + (nb - ns), suf) == 0)
71 return 1;
72 return 0;
73 }
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)
78 {
79 const struct got_error *err = NULL;
80 char *s, *p, *q;
81 int n;
83 *proto = *host = *port = *server_path = *repo_name = NULL;
85 p = strstr(uri, "://");
86 if (!p) {
87 /* Try parsing Git's "scp" style URL syntax. */
88 *proto = strdup("ssh");
89 if (proto == NULL) {
90 err = got_error_from_errno("strdup");
91 goto done;
92 }
93 s = (char *)uri;
94 q = strchr(s, ':');
95 if (q == NULL) {
96 err = got_error(GOT_ERR_PARSE_URI);
97 goto done;
98 }
99 /* No slashes allowed before first colon. */
100 p = strchr(s, '/');
101 if (p && q > p) {
102 err = got_error(GOT_ERR_PARSE_URI);
103 goto done;
105 *host = strndup(s, q - s);
106 if (*host == NULL) {
107 err = got_error_from_errno("strndup");
108 goto done;
110 p = q + 1;
111 } else {
112 *proto = strndup(uri, p - uri);
113 if (proto == NULL) {
114 err = got_error_from_errno("strndup");
115 goto done;
117 s = p + 3;
119 p = strstr(s, "/");
120 if (p == NULL || strlen(p) == 1) {
121 err = got_error(GOT_ERR_PARSE_URI);
122 goto done;
125 q = memchr(s, ':', p - s);
126 if (q) {
127 *host = strndup(s, q - s);
128 if (*host == NULL) {
129 err = got_error_from_errno("strndup");
130 goto done;
132 *port = strndup(q + 1, p - (q + 1));
133 if (*port == NULL) {
134 err = got_error_from_errno("strndup");
135 goto done;
137 } else {
138 *host = strndup(s, p - s);
139 if (*host == NULL) {
140 err = got_error_from_errno("strndup");
141 goto done;
146 while (p[0] == '/' && p[1] == '/')
147 p++;
148 *server_path = strdup(p);
149 if (*server_path == NULL) {
150 err = got_error_from_errno("strdup");
151 goto done;
153 got_path_strip_trailing_slashes(*server_path);
155 p = strrchr(p, '/');
156 if (!p || strlen(p) <= 1) {
157 err = got_error(GOT_ERR_PARSE_URI);
158 goto done;
160 p++;
161 n = strlen(p);
162 if (n == 0) {
163 err = got_error(GOT_ERR_PARSE_URI);
164 goto done;
166 if (hassuffix(p, ".git"))
167 n -= 4;
168 *repo_name = strndup(p, (p + n) - p);
169 if (*repo_name == NULL) {
170 err = got_error_from_errno("strndup");
171 goto done;
173 done:
174 if (err) {
175 free(*proto);
176 *proto = NULL;
177 free(*host);
178 *host = NULL;
179 free(*port);
180 *port = NULL;
181 free(*server_path);
182 *server_path = NULL;
183 free(*repo_name);
184 *repo_name = NULL;
186 return err;
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;
194 int pid, pfd[2];
195 char cmd[64];
196 char *argv[11];
197 int i = 0, j;
199 *newpid = -1;
200 *newfd = -1;
202 argv[i++] = GOT_DIAL_PATH_SSH;
203 if (port != NULL) {
204 argv[i++] = "-p";
205 argv[i++] = (char *)port;
207 if (verbosity == -1) {
208 argv[i++] = "-q";
209 } else {
210 /* ssh(1) allows up to 3 "-v" options. */
211 for (j = 0; j < MIN(3, verbosity); j++)
212 argv[i++] = "-v";
214 argv[i++] = "--";
215 argv[i++] = (char *)host;
216 argv[i++] = (char *)cmd;
217 argv[i++] = (char *)path;
218 argv[i++] = NULL;
220 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pfd) == -1)
221 return got_error_from_errno("socketpair");
223 pid = fork();
224 if (pid == -1) {
225 error = got_error_from_errno("fork");
226 close(pfd[0]);
227 close(pfd[1]);
228 return error;
229 } else if (pid == 0) {
230 int n;
231 if (close(pfd[1]) == -1)
232 err(1, "close");
233 if (dup2(pfd[0], 0) == -1)
234 err(1, "dup2");
235 if (dup2(pfd[0], 1) == -1)
236 err(1, "dup2");
237 n = snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
238 if (n < 0 || n >= ssizeof(cmd))
239 err(1, "snprintf");
240 if (execv(GOT_DIAL_PATH_SSH, argv) == -1)
241 err(1, "execv");
242 abort(); /* not reached */
243 } else {
244 if (close(pfd[0]) == -1)
245 return got_error_from_errno("close");
246 *newpid = pid;
247 *newfd = pfd[1];
248 return NULL;
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;
258 char *cmd = NULL;
259 int fd = -1, len, r, eaicode;
261 *newfd = -1;
263 if (port == NULL)
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);
270 if (eaicode) {
271 char msg[512];
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)
280 continue;
281 if (connect(fd, p->ai_addr, p->ai_addrlen) == 0) {
282 err = NULL;
283 break;
285 err = got_error_from_errno("connect");
286 close(fd);
288 if (p == NULL)
289 goto done;
291 if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
292 err = got_error_from_errno("asprintf");
293 goto done;
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');
297 if (r < 0)
298 err = got_error_from_errno("dprintf");
299 done:
300 free(cmd);
301 if (err) {
302 if (fd != -1)
303 close(fd);
304 } else
305 *newfd = fd;
306 return err;