Blame


1 d65a88a2 2021-09-05 stsp /*
2 d65a88a2 2021-09-05 stsp * Copyright (c) 2018, 2019 Ori Bernstein <ori@openbsd.org>
3 d65a88a2 2021-09-05 stsp * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
4 d65a88a2 2021-09-05 stsp *
5 d65a88a2 2021-09-05 stsp * Permission to use, copy, modify, and distribute this software for any
6 d65a88a2 2021-09-05 stsp * purpose with or without fee is hereby granted, provided that the above
7 d65a88a2 2021-09-05 stsp * copyright notice and this permission notice appear in all copies.
8 d65a88a2 2021-09-05 stsp *
9 d65a88a2 2021-09-05 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 d65a88a2 2021-09-05 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 d65a88a2 2021-09-05 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 d65a88a2 2021-09-05 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 d65a88a2 2021-09-05 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 d65a88a2 2021-09-05 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 d65a88a2 2021-09-05 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 d65a88a2 2021-09-05 stsp */
17 d65a88a2 2021-09-05 stsp
18 5e5da8c4 2021-09-05 stsp #include <sys/queue.h>
19 d65a88a2 2021-09-05 stsp #include <sys/types.h>
20 d65a88a2 2021-09-05 stsp #include <sys/socket.h>
21 d65a88a2 2021-09-05 stsp #include <netdb.h>
22 d65a88a2 2021-09-05 stsp
23 c10270f6 2021-09-06 naddy #include <assert.h>
24 d65a88a2 2021-09-05 stsp #include <err.h>
25 6cc8a118 2023-03-10 op #include <limits.h>
26 d65a88a2 2021-09-05 stsp #include <stdio.h>
27 d65a88a2 2021-09-05 stsp #include <stdlib.h>
28 d65a88a2 2021-09-05 stsp #include <string.h>
29 d65a88a2 2021-09-05 stsp #include <unistd.h>
30 d65a88a2 2021-09-05 stsp
31 d65a88a2 2021-09-05 stsp #include "got_error.h"
32 5e5da8c4 2021-09-05 stsp #include "got_path.h"
33 d65a88a2 2021-09-05 stsp
34 d65a88a2 2021-09-05 stsp #include "got_lib_dial.h"
35 336075a4 2022-06-25 op #include "got_dial.h"
36 c10270f6 2021-09-06 naddy
37 c10270f6 2021-09-06 naddy #ifndef nitems
38 c10270f6 2021-09-06 naddy #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
39 c10270f6 2021-09-06 naddy #endif
40 d65a88a2 2021-09-05 stsp
41 d65a88a2 2021-09-05 stsp #ifndef ssizeof
42 d65a88a2 2021-09-05 stsp #define ssizeof(_x) ((ssize_t)(sizeof(_x)))
43 d65a88a2 2021-09-05 stsp #endif
44 d65a88a2 2021-09-05 stsp
45 d65a88a2 2021-09-05 stsp #ifndef MIN
46 d65a88a2 2021-09-05 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
47 d65a88a2 2021-09-05 stsp #endif
48 d65a88a2 2021-09-05 stsp
49 d65a88a2 2021-09-05 stsp #ifndef GOT_DIAL_PATH_SSH
50 d65a88a2 2021-09-05 stsp #define GOT_DIAL_PATH_SSH "/usr/bin/ssh"
51 d65a88a2 2021-09-05 stsp #endif
52 d65a88a2 2021-09-05 stsp
53 d65a88a2 2021-09-05 stsp /* IANA assigned */
54 d65a88a2 2021-09-05 stsp #define GOT_DEFAULT_GIT_PORT 9418
55 d65a88a2 2021-09-05 stsp #define GOT_DEFAULT_GIT_PORT_STR "9418"
56 d65a88a2 2021-09-05 stsp
57 d65a88a2 2021-09-05 stsp const struct got_error *
58 d65a88a2 2021-09-05 stsp got_dial_apply_unveil(const char *proto)
59 d65a88a2 2021-09-05 stsp {
60 d65a88a2 2021-09-05 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
61 d65a88a2 2021-09-05 stsp if (unveil(GOT_DIAL_PATH_SSH, "x") != 0) {
62 d65a88a2 2021-09-05 stsp return got_error_from_errno2("unveil",
63 d65a88a2 2021-09-05 stsp GOT_DIAL_PATH_SSH);
64 d65a88a2 2021-09-05 stsp }
65 d65a88a2 2021-09-05 stsp }
66 d65a88a2 2021-09-05 stsp
67 d65a88a2 2021-09-05 stsp return NULL;
68 d65a88a2 2021-09-05 stsp }
69 d65a88a2 2021-09-05 stsp
70 5e5da8c4 2021-09-05 stsp static int
71 58e31a80 2022-06-27 op hassuffix(const char *base, const char *suf)
72 5e5da8c4 2021-09-05 stsp {
73 5e5da8c4 2021-09-05 stsp int nb, ns;
74 5e5da8c4 2021-09-05 stsp
75 5e5da8c4 2021-09-05 stsp nb = strlen(base);
76 5e5da8c4 2021-09-05 stsp ns = strlen(suf);
77 5e5da8c4 2021-09-05 stsp if (ns <= nb && strcmp(base + (nb - ns), suf) == 0)
78 5e5da8c4 2021-09-05 stsp return 1;
79 5e5da8c4 2021-09-05 stsp return 0;
80 5e5da8c4 2021-09-05 stsp }
81 5e5da8c4 2021-09-05 stsp
82 d65a88a2 2021-09-05 stsp const struct got_error *
83 5e5da8c4 2021-09-05 stsp got_dial_parse_uri(char **proto, char **host, char **port,
84 5e5da8c4 2021-09-05 stsp char **server_path, char **repo_name, const char *uri)
85 5e5da8c4 2021-09-05 stsp {
86 5e5da8c4 2021-09-05 stsp const struct got_error *err = NULL;
87 5e5da8c4 2021-09-05 stsp char *s, *p, *q;
88 5e5da8c4 2021-09-05 stsp
89 5e5da8c4 2021-09-05 stsp *proto = *host = *port = *server_path = *repo_name = NULL;
90 5e5da8c4 2021-09-05 stsp
91 5e5da8c4 2021-09-05 stsp p = strstr(uri, "://");
92 5e5da8c4 2021-09-05 stsp if (!p) {
93 5e5da8c4 2021-09-05 stsp /* Try parsing Git's "scp" style URL syntax. */
94 5e5da8c4 2021-09-05 stsp *proto = strdup("ssh");
95 805253d5 2022-03-07 naddy if (*proto == NULL) {
96 5e5da8c4 2021-09-05 stsp err = got_error_from_errno("strdup");
97 5e5da8c4 2021-09-05 stsp goto done;
98 5e5da8c4 2021-09-05 stsp }
99 5e5da8c4 2021-09-05 stsp s = (char *)uri;
100 5e5da8c4 2021-09-05 stsp q = strchr(s, ':');
101 5e5da8c4 2021-09-05 stsp if (q == NULL) {
102 5e5da8c4 2021-09-05 stsp err = got_error(GOT_ERR_PARSE_URI);
103 5e5da8c4 2021-09-05 stsp goto done;
104 5e5da8c4 2021-09-05 stsp }
105 5e5da8c4 2021-09-05 stsp /* No slashes allowed before first colon. */
106 5e5da8c4 2021-09-05 stsp p = strchr(s, '/');
107 5e5da8c4 2021-09-05 stsp if (p && q > p) {
108 5e5da8c4 2021-09-05 stsp err = got_error(GOT_ERR_PARSE_URI);
109 5e5da8c4 2021-09-05 stsp goto done;
110 5e5da8c4 2021-09-05 stsp }
111 5e5da8c4 2021-09-05 stsp *host = strndup(s, q - s);
112 5e5da8c4 2021-09-05 stsp if (*host == NULL) {
113 5e5da8c4 2021-09-05 stsp err = got_error_from_errno("strndup");
114 5e5da8c4 2021-09-05 stsp goto done;
115 5e5da8c4 2021-09-05 stsp }
116 3a12860c 2022-03-07 stsp if ((*host)[0] == '\0') {
117 3a12860c 2022-03-07 stsp err = got_error(GOT_ERR_PARSE_URI);
118 3a12860c 2022-03-07 stsp goto done;
119 3a12860c 2022-03-07 stsp }
120 5e5da8c4 2021-09-05 stsp p = q + 1;
121 5e5da8c4 2021-09-05 stsp } else {
122 5e5da8c4 2021-09-05 stsp *proto = strndup(uri, p - uri);
123 805253d5 2022-03-07 naddy if (*proto == NULL) {
124 5e5da8c4 2021-09-05 stsp err = got_error_from_errno("strndup");
125 5e5da8c4 2021-09-05 stsp goto done;
126 5e5da8c4 2021-09-05 stsp }
127 5e5da8c4 2021-09-05 stsp s = p + 3;
128 5e5da8c4 2021-09-05 stsp
129 5e5da8c4 2021-09-05 stsp p = strstr(s, "/");
130 5e5da8c4 2021-09-05 stsp if (p == NULL || strlen(p) == 1) {
131 5e5da8c4 2021-09-05 stsp err = got_error(GOT_ERR_PARSE_URI);
132 5e5da8c4 2021-09-05 stsp goto done;
133 5e5da8c4 2021-09-05 stsp }
134 5e5da8c4 2021-09-05 stsp
135 5e5da8c4 2021-09-05 stsp q = memchr(s, ':', p - s);
136 5e5da8c4 2021-09-05 stsp if (q) {
137 5e5da8c4 2021-09-05 stsp *host = strndup(s, q - s);
138 5e5da8c4 2021-09-05 stsp if (*host == NULL) {
139 5e5da8c4 2021-09-05 stsp err = got_error_from_errno("strndup");
140 5e5da8c4 2021-09-05 stsp goto done;
141 5e5da8c4 2021-09-05 stsp }
142 3a12860c 2022-03-07 stsp if ((*host)[0] == '\0') {
143 3a12860c 2022-03-07 stsp err = got_error(GOT_ERR_PARSE_URI);
144 3a12860c 2022-03-07 stsp goto done;
145 3a12860c 2022-03-07 stsp }
146 5e5da8c4 2021-09-05 stsp *port = strndup(q + 1, p - (q + 1));
147 5e5da8c4 2021-09-05 stsp if (*port == NULL) {
148 5e5da8c4 2021-09-05 stsp err = got_error_from_errno("strndup");
149 5e5da8c4 2021-09-05 stsp goto done;
150 5e5da8c4 2021-09-05 stsp }
151 3a12860c 2022-03-07 stsp if ((*port)[0] == '\0') {
152 3a12860c 2022-03-07 stsp err = got_error(GOT_ERR_PARSE_URI);
153 3a12860c 2022-03-07 stsp goto done;
154 3a12860c 2022-03-07 stsp }
155 5e5da8c4 2021-09-05 stsp } else {
156 5e5da8c4 2021-09-05 stsp *host = strndup(s, p - s);
157 5e5da8c4 2021-09-05 stsp if (*host == NULL) {
158 5e5da8c4 2021-09-05 stsp err = got_error_from_errno("strndup");
159 5e5da8c4 2021-09-05 stsp goto done;
160 5e5da8c4 2021-09-05 stsp }
161 3a12860c 2022-03-07 stsp if ((*host)[0] == '\0') {
162 3a12860c 2022-03-07 stsp err = got_error(GOT_ERR_PARSE_URI);
163 3a12860c 2022-03-07 stsp goto done;
164 3a12860c 2022-03-07 stsp }
165 5e5da8c4 2021-09-05 stsp }
166 5e5da8c4 2021-09-05 stsp }
167 5e5da8c4 2021-09-05 stsp
168 5e5da8c4 2021-09-05 stsp while (p[0] == '/' && p[1] == '/')
169 5e5da8c4 2021-09-05 stsp p++;
170 5e5da8c4 2021-09-05 stsp *server_path = strdup(p);
171 5e5da8c4 2021-09-05 stsp if (*server_path == NULL) {
172 5e5da8c4 2021-09-05 stsp err = got_error_from_errno("strdup");
173 5e5da8c4 2021-09-05 stsp goto done;
174 5e5da8c4 2021-09-05 stsp }
175 5e5da8c4 2021-09-05 stsp got_path_strip_trailing_slashes(*server_path);
176 3a12860c 2022-03-07 stsp if ((*server_path)[0] == '\0') {
177 5e5da8c4 2021-09-05 stsp err = got_error(GOT_ERR_PARSE_URI);
178 5e5da8c4 2021-09-05 stsp goto done;
179 5e5da8c4 2021-09-05 stsp }
180 3a12860c 2022-03-07 stsp
181 3a12860c 2022-03-07 stsp err = got_path_basename(repo_name, *server_path);
182 3a12860c 2022-03-07 stsp if (err)
183 5e5da8c4 2021-09-05 stsp goto done;
184 3a12860c 2022-03-07 stsp if (hassuffix(*repo_name, ".git"))
185 3a12860c 2022-03-07 stsp (*repo_name)[strlen(*repo_name) - 4] = '\0';
186 3a12860c 2022-03-07 stsp if ((*repo_name)[0] == '\0')
187 3a12860c 2022-03-07 stsp err = got_error(GOT_ERR_PARSE_URI);
188 5e5da8c4 2021-09-05 stsp done:
189 5e5da8c4 2021-09-05 stsp if (err) {
190 5e5da8c4 2021-09-05 stsp free(*proto);
191 5e5da8c4 2021-09-05 stsp *proto = NULL;
192 5e5da8c4 2021-09-05 stsp free(*host);
193 5e5da8c4 2021-09-05 stsp *host = NULL;
194 5e5da8c4 2021-09-05 stsp free(*port);
195 5e5da8c4 2021-09-05 stsp *port = NULL;
196 5e5da8c4 2021-09-05 stsp free(*server_path);
197 5e5da8c4 2021-09-05 stsp *server_path = NULL;
198 5e5da8c4 2021-09-05 stsp free(*repo_name);
199 5e5da8c4 2021-09-05 stsp *repo_name = NULL;
200 5e5da8c4 2021-09-05 stsp }
201 5e5da8c4 2021-09-05 stsp return err;
202 5e5da8c4 2021-09-05 stsp }
203 5e5da8c4 2021-09-05 stsp
204 6cc8a118 2023-03-10 op /*
205 6cc8a118 2023-03-10 op * Escape a given path for the shell which will be started by sshd.
206 6cc8a118 2023-03-10 op * In particular, git-shell is known to require single-quote characters
207 6cc8a118 2023-03-10 op * around its repository path argument and will refuse to run otherwise.
208 6cc8a118 2023-03-10 op */
209 6cc8a118 2023-03-10 op static const struct got_error *
210 6cc8a118 2023-03-10 op escape_path(char *buf, size_t bufsize, const char *path)
211 6cc8a118 2023-03-10 op {
212 6cc8a118 2023-03-10 op const char *p;
213 6cc8a118 2023-03-10 op char *q;
214 6cc8a118 2023-03-10 op
215 6cc8a118 2023-03-10 op p = path;
216 6cc8a118 2023-03-10 op q = buf;
217 6cc8a118 2023-03-10 op
218 6cc8a118 2023-03-10 op if (bufsize > 1)
219 6cc8a118 2023-03-10 op *q++ = '\'';
220 6cc8a118 2023-03-10 op
221 6cc8a118 2023-03-10 op while (*p != '\0' && (q - buf < bufsize)) {
222 6cc8a118 2023-03-10 op /* git escapes ! too */
223 6cc8a118 2023-03-10 op if (*p != '\'' && *p != '!') {
224 6cc8a118 2023-03-10 op *q++ = *p++;
225 6cc8a118 2023-03-10 op continue;
226 6cc8a118 2023-03-10 op }
227 6cc8a118 2023-03-10 op
228 6cc8a118 2023-03-10 op if (q - buf + 4 >= bufsize)
229 6cc8a118 2023-03-10 op break;
230 6cc8a118 2023-03-10 op *q++ = '\'';
231 6cc8a118 2023-03-10 op *q++ = '\\';
232 6cc8a118 2023-03-10 op *q++ = *p++;
233 6cc8a118 2023-03-10 op *q++ = '\'';
234 6cc8a118 2023-03-10 op }
235 6cc8a118 2023-03-10 op
236 6cc8a118 2023-03-10 op if (*p == '\0' && (q - buf + 1 < bufsize)) {
237 6cc8a118 2023-03-10 op *q++ = '\'';
238 6cc8a118 2023-03-10 op *q = '\0';
239 6cc8a118 2023-03-10 op return NULL;
240 6cc8a118 2023-03-10 op }
241 6cc8a118 2023-03-10 op
242 6cc8a118 2023-03-10 op return got_error_fmt(GOT_ERR_NO_SPACE, "overlong path: %s", path);
243 6cc8a118 2023-03-10 op }
244 6cc8a118 2023-03-10 op
245 5e5da8c4 2021-09-05 stsp const struct got_error *
246 d65a88a2 2021-09-05 stsp got_dial_ssh(pid_t *newpid, int *newfd, const char *host,
247 1eb38992 2023-04-14 stsp const char *port, const char *path, const char *command, int verbosity)
248 d65a88a2 2021-09-05 stsp {
249 d65a88a2 2021-09-05 stsp const struct got_error *error = NULL;
250 d65a88a2 2021-09-05 stsp int pid, pfd[2];
251 d65a88a2 2021-09-05 stsp char cmd[64];
252 6cc8a118 2023-03-10 op char escaped_path[PATH_MAX];
253 58e31a80 2022-06-27 op const char *argv[11];
254 d65a88a2 2021-09-05 stsp int i = 0, j;
255 d65a88a2 2021-09-05 stsp
256 d65a88a2 2021-09-05 stsp *newpid = -1;
257 d65a88a2 2021-09-05 stsp *newfd = -1;
258 d65a88a2 2021-09-05 stsp
259 6cc8a118 2023-03-10 op error = escape_path(escaped_path, sizeof(escaped_path), path);
260 6cc8a118 2023-03-10 op if (error)
261 6cc8a118 2023-03-10 op return error;
262 6cc8a118 2023-03-10 op
263 d65a88a2 2021-09-05 stsp argv[i++] = GOT_DIAL_PATH_SSH;
264 d65a88a2 2021-09-05 stsp if (port != NULL) {
265 d65a88a2 2021-09-05 stsp argv[i++] = "-p";
266 d65a88a2 2021-09-05 stsp argv[i++] = (char *)port;
267 d65a88a2 2021-09-05 stsp }
268 d65a88a2 2021-09-05 stsp if (verbosity == -1) {
269 d65a88a2 2021-09-05 stsp argv[i++] = "-q";
270 d65a88a2 2021-09-05 stsp } else {
271 d65a88a2 2021-09-05 stsp /* ssh(1) allows up to 3 "-v" options. */
272 d65a88a2 2021-09-05 stsp for (j = 0; j < MIN(3, verbosity); j++)
273 d65a88a2 2021-09-05 stsp argv[i++] = "-v";
274 d65a88a2 2021-09-05 stsp }
275 d65a88a2 2021-09-05 stsp argv[i++] = "--";
276 d65a88a2 2021-09-05 stsp argv[i++] = (char *)host;
277 d65a88a2 2021-09-05 stsp argv[i++] = (char *)cmd;
278 6cc8a118 2023-03-10 op argv[i++] = (char *)escaped_path;
279 d65a88a2 2021-09-05 stsp argv[i++] = NULL;
280 c10270f6 2021-09-06 naddy assert(i <= nitems(argv));
281 d65a88a2 2021-09-05 stsp
282 d65a88a2 2021-09-05 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pfd) == -1)
283 d65a88a2 2021-09-05 stsp return got_error_from_errno("socketpair");
284 d65a88a2 2021-09-05 stsp
285 d65a88a2 2021-09-05 stsp pid = fork();
286 d65a88a2 2021-09-05 stsp if (pid == -1) {
287 d65a88a2 2021-09-05 stsp error = got_error_from_errno("fork");
288 d65a88a2 2021-09-05 stsp close(pfd[0]);
289 d65a88a2 2021-09-05 stsp close(pfd[1]);
290 d65a88a2 2021-09-05 stsp return error;
291 d65a88a2 2021-09-05 stsp } else if (pid == 0) {
292 d65a88a2 2021-09-05 stsp if (close(pfd[1]) == -1)
293 d65a88a2 2021-09-05 stsp err(1, "close");
294 d65a88a2 2021-09-05 stsp if (dup2(pfd[0], 0) == -1)
295 d65a88a2 2021-09-05 stsp err(1, "dup2");
296 d65a88a2 2021-09-05 stsp if (dup2(pfd[0], 1) == -1)
297 d65a88a2 2021-09-05 stsp err(1, "dup2");
298 1eb38992 2023-04-14 stsp if (strlcpy(cmd, command, sizeof(cmd)) >= sizeof(cmd))
299 d65a88a2 2021-09-05 stsp err(1, "snprintf");
300 58e31a80 2022-06-27 op if (execv(GOT_DIAL_PATH_SSH, (char *const *)argv) == -1)
301 d65a88a2 2021-09-05 stsp err(1, "execv");
302 d65a88a2 2021-09-05 stsp abort(); /* not reached */
303 d65a88a2 2021-09-05 stsp } else {
304 d65a88a2 2021-09-05 stsp if (close(pfd[0]) == -1)
305 d65a88a2 2021-09-05 stsp return got_error_from_errno("close");
306 d65a88a2 2021-09-05 stsp *newpid = pid;
307 d65a88a2 2021-09-05 stsp *newfd = pfd[1];
308 d65a88a2 2021-09-05 stsp return NULL;
309 d65a88a2 2021-09-05 stsp }
310 d65a88a2 2021-09-05 stsp }
311 d65a88a2 2021-09-05 stsp
312 d65a88a2 2021-09-05 stsp const struct got_error *
313 d65a88a2 2021-09-05 stsp got_dial_git(int *newfd, const char *host, const char *port,
314 1eb38992 2023-04-14 stsp const char *path, const char *command)
315 d65a88a2 2021-09-05 stsp {
316 d65a88a2 2021-09-05 stsp const struct got_error *err = NULL;
317 d65a88a2 2021-09-05 stsp struct addrinfo hints, *servinfo, *p;
318 d65a88a2 2021-09-05 stsp char *cmd = NULL;
319 d65a88a2 2021-09-05 stsp int fd = -1, len, r, eaicode;
320 d65a88a2 2021-09-05 stsp
321 d65a88a2 2021-09-05 stsp *newfd = -1;
322 d65a88a2 2021-09-05 stsp
323 d65a88a2 2021-09-05 stsp if (port == NULL)
324 d65a88a2 2021-09-05 stsp port = GOT_DEFAULT_GIT_PORT_STR;
325 d65a88a2 2021-09-05 stsp
326 d65a88a2 2021-09-05 stsp memset(&hints, 0, sizeof hints);
327 d65a88a2 2021-09-05 stsp hints.ai_family = AF_UNSPEC;
328 d65a88a2 2021-09-05 stsp hints.ai_socktype = SOCK_STREAM;
329 d65a88a2 2021-09-05 stsp eaicode = getaddrinfo(host, port, &hints, &servinfo);
330 d65a88a2 2021-09-05 stsp if (eaicode) {
331 d65a88a2 2021-09-05 stsp char msg[512];
332 d65a88a2 2021-09-05 stsp snprintf(msg, sizeof(msg), "%s: %s", host,
333 d65a88a2 2021-09-05 stsp gai_strerror(eaicode));
334 d65a88a2 2021-09-05 stsp return got_error_msg(GOT_ERR_ADDRINFO, msg);
335 d65a88a2 2021-09-05 stsp }
336 d65a88a2 2021-09-05 stsp
337 d65a88a2 2021-09-05 stsp for (p = servinfo; p != NULL; p = p->ai_next) {
338 d65a88a2 2021-09-05 stsp if ((fd = socket(p->ai_family, p->ai_socktype,
339 d65a88a2 2021-09-05 stsp p->ai_protocol)) == -1)
340 d65a88a2 2021-09-05 stsp continue;
341 d65a88a2 2021-09-05 stsp if (connect(fd, p->ai_addr, p->ai_addrlen) == 0) {
342 d65a88a2 2021-09-05 stsp err = NULL;
343 d65a88a2 2021-09-05 stsp break;
344 d65a88a2 2021-09-05 stsp }
345 d65a88a2 2021-09-05 stsp err = got_error_from_errno("connect");
346 d65a88a2 2021-09-05 stsp close(fd);
347 d65a88a2 2021-09-05 stsp }
348 9ea55f08 2022-09-05 op freeaddrinfo(servinfo);
349 d65a88a2 2021-09-05 stsp if (p == NULL)
350 d65a88a2 2021-09-05 stsp goto done;
351 d65a88a2 2021-09-05 stsp
352 1eb38992 2023-04-14 stsp if (asprintf(&cmd, "%s %s", command, path) == -1) {
353 d65a88a2 2021-09-05 stsp err = got_error_from_errno("asprintf");
354 d65a88a2 2021-09-05 stsp goto done;
355 d65a88a2 2021-09-05 stsp }
356 d65a88a2 2021-09-05 stsp len = 4 + strlen(cmd) + 1 + strlen("host=") + strlen(host) + 1;
357 d65a88a2 2021-09-05 stsp r = dprintf(fd, "%04x%s%chost=%s%c", len, cmd, '\0', host, '\0');
358 d65a88a2 2021-09-05 stsp if (r < 0)
359 d65a88a2 2021-09-05 stsp err = got_error_from_errno("dprintf");
360 d65a88a2 2021-09-05 stsp done:
361 d65a88a2 2021-09-05 stsp free(cmd);
362 d65a88a2 2021-09-05 stsp if (err) {
363 d65a88a2 2021-09-05 stsp if (fd != -1)
364 d65a88a2 2021-09-05 stsp close(fd);
365 d65a88a2 2021-09-05 stsp } else
366 d65a88a2 2021-09-05 stsp *newfd = fd;
367 d65a88a2 2021-09-05 stsp return err;
368 d65a88a2 2021-09-05 stsp }
369 1eb38992 2023-04-14 stsp
370 1eb38992 2023-04-14 stsp const struct got_error *
371 1eb38992 2023-04-14 stsp got_dial_parse_command(char **command, char **repo_path, const char *gitcmd)
372 1eb38992 2023-04-14 stsp {
373 1eb38992 2023-04-14 stsp const struct got_error *err = NULL;
374 1eb38992 2023-04-14 stsp size_t len, cmdlen, pathlen;
375 1eb38992 2023-04-14 stsp char *path0 = NULL, *path, *abspath = NULL, *canonpath = NULL;
376 1eb38992 2023-04-14 stsp const char *relpath;
377 1eb38992 2023-04-14 stsp
378 1eb38992 2023-04-14 stsp *command = NULL;
379 1eb38992 2023-04-14 stsp *repo_path = NULL;
380 1eb38992 2023-04-14 stsp
381 1eb38992 2023-04-14 stsp len = strlen(gitcmd);
382 1eb38992 2023-04-14 stsp
383 1eb38992 2023-04-14 stsp if (len >= strlen(GOT_DIAL_CMD_SEND) &&
384 1eb38992 2023-04-14 stsp strncmp(gitcmd, GOT_DIAL_CMD_SEND,
385 1eb38992 2023-04-14 stsp strlen(GOT_DIAL_CMD_SEND)) == 0)
386 1eb38992 2023-04-14 stsp cmdlen = strlen(GOT_DIAL_CMD_SEND);
387 1eb38992 2023-04-14 stsp else if (len >= strlen(GOT_DIAL_CMD_FETCH) &&
388 1eb38992 2023-04-14 stsp strncmp(gitcmd, GOT_DIAL_CMD_FETCH,
389 1eb38992 2023-04-14 stsp strlen(GOT_DIAL_CMD_FETCH)) == 0)
390 1eb38992 2023-04-14 stsp cmdlen = strlen(GOT_DIAL_CMD_FETCH);
391 1eb38992 2023-04-14 stsp else
392 1eb38992 2023-04-14 stsp return got_error(GOT_ERR_BAD_PACKET);
393 1eb38992 2023-04-14 stsp
394 1eb38992 2023-04-14 stsp if (len <= cmdlen + 1 || gitcmd[cmdlen] != ' ')
395 1eb38992 2023-04-14 stsp return got_error(GOT_ERR_BAD_PACKET);
396 1eb38992 2023-04-14 stsp
397 1eb38992 2023-04-14 stsp if (memchr(&gitcmd[cmdlen + 1], '\0', len - cmdlen) == NULL)
398 1eb38992 2023-04-14 stsp return got_error(GOT_ERR_BAD_PATH);
399 1eb38992 2023-04-14 stsp
400 1eb38992 2023-04-14 stsp /* Forbid linefeeds in paths, like Git does. */
401 1eb38992 2023-04-14 stsp if (memchr(&gitcmd[cmdlen + 1], '\n', len - cmdlen) != NULL)
402 1eb38992 2023-04-14 stsp return got_error(GOT_ERR_BAD_PATH);
403 1eb38992 2023-04-14 stsp
404 1eb38992 2023-04-14 stsp path0 = strdup(&gitcmd[cmdlen + 1]);
405 1eb38992 2023-04-14 stsp if (path0 == NULL)
406 1eb38992 2023-04-14 stsp return got_error_from_errno("strdup");
407 1eb38992 2023-04-14 stsp path = path0;
408 1eb38992 2023-04-14 stsp pathlen = strlen(path);
409 1eb38992 2023-04-14 stsp
410 1eb38992 2023-04-14 stsp /*
411 1eb38992 2023-04-14 stsp * Git clients send a shell command.
412 1eb38992 2023-04-14 stsp * Trim spaces and quotes around the path.
413 1eb38992 2023-04-14 stsp */
414 1eb38992 2023-04-14 stsp while (path[0] == '\'' || path[0] == '\"' || path[0] == ' ') {
415 1eb38992 2023-04-14 stsp path++;
416 1eb38992 2023-04-14 stsp pathlen--;
417 1eb38992 2023-04-14 stsp }
418 1eb38992 2023-04-14 stsp while (pathlen > 0 &&
419 1eb38992 2023-04-14 stsp (path[pathlen - 1] == '\'' || path[pathlen - 1] == '\"' ||
420 1eb38992 2023-04-14 stsp path[pathlen - 1] == ' ')) {
421 1eb38992 2023-04-14 stsp path[pathlen - 1] = '\0';
422 1eb38992 2023-04-14 stsp pathlen--;
423 1eb38992 2023-04-14 stsp }
424 1eb38992 2023-04-14 stsp
425 1eb38992 2023-04-14 stsp /* Deny an empty repository path. */
426 1eb38992 2023-04-14 stsp if (path[0] == '\0' || got_path_is_root_dir(path)) {
427 1eb38992 2023-04-14 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
428 1eb38992 2023-04-14 stsp goto done;
429 1eb38992 2023-04-14 stsp }
430 1eb38992 2023-04-14 stsp
431 1eb38992 2023-04-14 stsp if (asprintf(&abspath, "/%s", path) == -1) {
432 1eb38992 2023-04-14 stsp err = got_error_from_errno("asprintf");
433 1eb38992 2023-04-14 stsp goto done;
434 1eb38992 2023-04-14 stsp }
435 1eb38992 2023-04-14 stsp pathlen = strlen(abspath);
436 1eb38992 2023-04-14 stsp canonpath = malloc(pathlen + 1);
437 1eb38992 2023-04-14 stsp if (canonpath == NULL) {
438 1eb38992 2023-04-14 stsp err = got_error_from_errno("malloc");
439 1eb38992 2023-04-14 stsp goto done;
440 1eb38992 2023-04-14 stsp }
441 1eb38992 2023-04-14 stsp err = got_canonpath(abspath, canonpath, pathlen + 1);
442 1eb38992 2023-04-14 stsp if (err)
443 1eb38992 2023-04-14 stsp goto done;
444 1eb38992 2023-04-14 stsp
445 1eb38992 2023-04-14 stsp relpath = canonpath;
446 1eb38992 2023-04-14 stsp while (relpath[0] == '/')
447 1eb38992 2023-04-14 stsp relpath++;
448 1eb38992 2023-04-14 stsp *repo_path = strdup(relpath);
449 1eb38992 2023-04-14 stsp if (*repo_path == NULL) {
450 1eb38992 2023-04-14 stsp err = got_error_from_errno("strdup");
451 1eb38992 2023-04-14 stsp goto done;
452 1eb38992 2023-04-14 stsp }
453 1eb38992 2023-04-14 stsp *command = strndup(gitcmd, cmdlen);
454 1eb38992 2023-04-14 stsp if (*command == NULL)
455 1eb38992 2023-04-14 stsp err = got_error_from_errno("strndup");
456 1eb38992 2023-04-14 stsp done:
457 1eb38992 2023-04-14 stsp free(path0);
458 1eb38992 2023-04-14 stsp free(abspath);
459 1eb38992 2023-04-14 stsp free(canonpath);
460 1eb38992 2023-04-14 stsp if (err) {
461 1eb38992 2023-04-14 stsp free(*repo_path);
462 1eb38992 2023-04-14 stsp *repo_path = NULL;
463 1eb38992 2023-04-14 stsp }
464 1eb38992 2023-04-14 stsp return err;
465 1eb38992 2023-04-14 stsp }