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 <assert.h>
24 #include <err.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
30 #include "got_error.h"
31 #include "got_path.h"
33 #include "got_lib_dial.h"
34 #include "got_dial.h"
36 #ifndef nitems
37 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
38 #endif
40 #ifndef ssizeof
41 #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
42 #endif
44 #ifndef MIN
45 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
46 #endif
48 #ifndef GOT_DIAL_PATH_SSH
49 #define GOT_DIAL_PATH_SSH "/usr/bin/ssh"
50 #endif
52 /* IANA assigned */
53 #define GOT_DEFAULT_GIT_PORT 9418
54 #define GOT_DEFAULT_GIT_PORT_STR "9418"
56 const struct got_error *
57 got_dial_apply_unveil(const char *proto)
58 {
59 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
60 if (unveil(GOT_DIAL_PATH_SSH, "x") != 0) {
61 return got_error_from_errno2("unveil",
62 GOT_DIAL_PATH_SSH);
63 }
64 }
66 return NULL;
67 }
69 static int
70 hassuffix(const char *base, const char *suf)
71 {
72 int nb, ns;
74 nb = strlen(base);
75 ns = strlen(suf);
76 if (ns <= nb && strcmp(base + (nb - ns), suf) == 0)
77 return 1;
78 return 0;
79 }
81 const struct got_error *
82 got_dial_parse_uri(char **proto, char **host, char **port,
83 char **server_path, char **repo_name, const char *uri)
84 {
85 const struct got_error *err = NULL;
86 char *s, *p, *q;
88 *proto = *host = *port = *server_path = *repo_name = NULL;
90 p = strstr(uri, "://");
91 if (!p) {
92 /* Try parsing Git's "scp" style URL syntax. */
93 *proto = strdup("ssh");
94 if (*proto == NULL) {
95 err = got_error_from_errno("strdup");
96 goto done;
97 }
98 s = (char *)uri;
99 q = strchr(s, ':');
100 if (q == NULL) {
101 err = got_error(GOT_ERR_PARSE_URI);
102 goto done;
104 /* No slashes allowed before first colon. */
105 p = strchr(s, '/');
106 if (p && q > p) {
107 err = got_error(GOT_ERR_PARSE_URI);
108 goto done;
110 *host = strndup(s, q - s);
111 if (*host == NULL) {
112 err = got_error_from_errno("strndup");
113 goto done;
115 if ((*host)[0] == '\0') {
116 err = got_error(GOT_ERR_PARSE_URI);
117 goto done;
119 p = q + 1;
120 } else {
121 *proto = strndup(uri, p - uri);
122 if (*proto == NULL) {
123 err = got_error_from_errno("strndup");
124 goto done;
126 s = p + 3;
128 p = strstr(s, "/");
129 if (p == NULL || strlen(p) == 1) {
130 err = got_error(GOT_ERR_PARSE_URI);
131 goto done;
134 q = memchr(s, ':', p - s);
135 if (q) {
136 *host = strndup(s, q - s);
137 if (*host == NULL) {
138 err = got_error_from_errno("strndup");
139 goto done;
141 if ((*host)[0] == '\0') {
142 err = got_error(GOT_ERR_PARSE_URI);
143 goto done;
145 *port = strndup(q + 1, p - (q + 1));
146 if (*port == NULL) {
147 err = got_error_from_errno("strndup");
148 goto done;
150 if ((*port)[0] == '\0') {
151 err = got_error(GOT_ERR_PARSE_URI);
152 goto done;
154 } else {
155 *host = strndup(s, p - s);
156 if (*host == NULL) {
157 err = got_error_from_errno("strndup");
158 goto done;
160 if ((*host)[0] == '\0') {
161 err = got_error(GOT_ERR_PARSE_URI);
162 goto done;
167 while (p[0] == '/' && p[1] == '/')
168 p++;
169 *server_path = strdup(p);
170 if (*server_path == NULL) {
171 err = got_error_from_errno("strdup");
172 goto done;
174 got_path_strip_trailing_slashes(*server_path);
175 if ((*server_path)[0] == '\0') {
176 err = got_error(GOT_ERR_PARSE_URI);
177 goto done;
180 err = got_path_basename(repo_name, *server_path);
181 if (err)
182 goto done;
183 if (hassuffix(*repo_name, ".git"))
184 (*repo_name)[strlen(*repo_name) - 4] = '\0';
185 if ((*repo_name)[0] == '\0')
186 err = got_error(GOT_ERR_PARSE_URI);
187 done:
188 if (err) {
189 free(*proto);
190 *proto = NULL;
191 free(*host);
192 *host = NULL;
193 free(*port);
194 *port = NULL;
195 free(*server_path);
196 *server_path = NULL;
197 free(*repo_name);
198 *repo_name = NULL;
200 return err;
203 const struct got_error *
204 got_dial_ssh(pid_t *newpid, int *newfd, const char *host,
205 const char *port, const char *path, const char *direction, int verbosity)
207 const struct got_error *error = NULL;
208 int pid, pfd[2];
209 char cmd[64];
210 const char *argv[11];
211 int i = 0, j;
213 *newpid = -1;
214 *newfd = -1;
216 argv[i++] = GOT_DIAL_PATH_SSH;
217 if (port != NULL) {
218 argv[i++] = "-p";
219 argv[i++] = (char *)port;
221 if (verbosity == -1) {
222 argv[i++] = "-q";
223 } else {
224 /* ssh(1) allows up to 3 "-v" options. */
225 for (j = 0; j < MIN(3, verbosity); j++)
226 argv[i++] = "-v";
228 argv[i++] = "--";
229 argv[i++] = (char *)host;
230 argv[i++] = (char *)cmd;
231 argv[i++] = (char *)path;
232 argv[i++] = NULL;
233 assert(i <= nitems(argv));
235 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pfd) == -1)
236 return got_error_from_errno("socketpair");
238 pid = fork();
239 if (pid == -1) {
240 error = got_error_from_errno("fork");
241 close(pfd[0]);
242 close(pfd[1]);
243 return error;
244 } else if (pid == 0) {
245 int n;
246 if (close(pfd[1]) == -1)
247 err(1, "close");
248 if (dup2(pfd[0], 0) == -1)
249 err(1, "dup2");
250 if (dup2(pfd[0], 1) == -1)
251 err(1, "dup2");
252 n = snprintf(cmd, sizeof(cmd), "git-%s-pack", direction);
253 if (n < 0 || n >= ssizeof(cmd))
254 err(1, "snprintf");
255 if (execv(GOT_DIAL_PATH_SSH, (char *const *)argv) == -1)
256 err(1, "execv");
257 abort(); /* not reached */
258 } else {
259 if (close(pfd[0]) == -1)
260 return got_error_from_errno("close");
261 *newpid = pid;
262 *newfd = pfd[1];
263 return NULL;
267 const struct got_error *
268 got_dial_git(int *newfd, const char *host, const char *port,
269 const char *path, const char *direction)
271 const struct got_error *err = NULL;
272 struct addrinfo hints, *servinfo, *p;
273 char *cmd = NULL;
274 int fd = -1, len, r, eaicode;
276 *newfd = -1;
278 if (port == NULL)
279 port = GOT_DEFAULT_GIT_PORT_STR;
281 memset(&hints, 0, sizeof hints);
282 hints.ai_family = AF_UNSPEC;
283 hints.ai_socktype = SOCK_STREAM;
284 eaicode = getaddrinfo(host, port, &hints, &servinfo);
285 if (eaicode) {
286 char msg[512];
287 snprintf(msg, sizeof(msg), "%s: %s", host,
288 gai_strerror(eaicode));
289 return got_error_msg(GOT_ERR_ADDRINFO, msg);
292 for (p = servinfo; p != NULL; p = p->ai_next) {
293 if ((fd = socket(p->ai_family, p->ai_socktype,
294 p->ai_protocol)) == -1)
295 continue;
296 if (connect(fd, p->ai_addr, p->ai_addrlen) == 0) {
297 err = NULL;
298 break;
300 err = got_error_from_errno("connect");
301 close(fd);
303 freeaddrinfo(servinfo);
304 if (p == NULL)
305 goto done;
307 if (asprintf(&cmd, "git-%s-pack %s", direction, path) == -1) {
308 err = got_error_from_errno("asprintf");
309 goto done;
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');
313 if (r < 0)
314 err = got_error_from_errno("dprintf");
315 done:
316 free(cmd);
317 if (err) {
318 if (fd != -1)
319 close(fd);
320 } else
321 *newfd = fd;
322 return err;