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