master
1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * SPDX-License-Identifier: curl AND ISC
22 *
23 ***************************************************************************/
24#include "curl_setup.h"
25#include "vssh/ssh.h"
27#ifdef USE_SSH
29#include "vssh/vssh.h"
30#include "curlx/strparse.h"
31#include "curl_trc.h"
32#include "escape.h"
33#include "select.h" /* for Curl_pollset_change() */
34#include "url.h" /* for Curl_conn_meta_get() */
35#include "curlx/win32-fopen.h"
37#ifdef CURLVERBOSE
38const char *Curl_ssh_statename(sshstate state)
39{
40 static const char * const names[] = {
41 "SSH_STOP",
42 "SSH_INIT",
43 "SSH_S_STARTUP",
44 "SSH_HOSTKEY",
45 "SSH_AUTHLIST",
46 "SSH_AUTH_PKEY_INIT",
47 "SSH_AUTH_PKEY",
48 "SSH_AUTH_PKEY_AUTO",
49 "SSH_AUTH_PASS_INIT",
50 "SSH_AUTH_PASS",
51 "SSH_AUTH_AGENT_INIT",
52 "SSH_AUTH_AGENT_LIST",
53 "SSH_AUTH_AGENT",
54 "SSH_AUTH_HOST_INIT",
55 "SSH_AUTH_HOST",
56 "SSH_AUTH_KEY_INIT",
57 "SSH_AUTH_KEY",
58 "SSH_AUTH_GSSAPI",
59 "SSH_AUTH_DONE",
60 "SSH_SFTP_INIT",
61 "SSH_SFTP_REALPATH",
62 "SSH_SFTP_QUOTE_INIT",
63 "SSH_SFTP_POSTQUOTE_INIT",
64 "SSH_SFTP_QUOTE",
65 "SSH_SFTP_NEXT_QUOTE",
66 "SSH_SFTP_QUOTE_STAT",
67 "SSH_SFTP_QUOTE_SETSTAT",
68 "SSH_SFTP_QUOTE_SYMLINK",
69 "SSH_SFTP_QUOTE_MKDIR",
70 "SSH_SFTP_QUOTE_RENAME",
71 "SSH_SFTP_QUOTE_RMDIR",
72 "SSH_SFTP_QUOTE_UNLINK",
73 "SSH_SFTP_QUOTE_STATVFS",
74 "SSH_SFTP_GETINFO",
75 "SSH_SFTP_FILETIME",
76 "SSH_SFTP_TRANS_INIT",
77 "SSH_SFTP_UPLOAD_INIT",
78 "SSH_SFTP_CREATE_DIRS_INIT",
79 "SSH_SFTP_CREATE_DIRS",
80 "SSH_SFTP_CREATE_DIRS_MKDIR",
81 "SSH_SFTP_READDIR_INIT",
82 "SSH_SFTP_READDIR",
83 "SSH_SFTP_READDIR_LINK",
84 "SSH_SFTP_READDIR_BOTTOM",
85 "SSH_SFTP_READDIR_DONE",
86 "SSH_SFTP_DOWNLOAD_INIT",
87 "SSH_SFTP_DOWNLOAD_STAT",
88 "SSH_SFTP_CLOSE",
89 "SSH_SFTP_SHUTDOWN",
90 "SSH_SCP_TRANS_INIT",
91 "SSH_SCP_UPLOAD_INIT",
92 "SSH_SCP_DOWNLOAD_INIT",
93 "SSH_SCP_DOWNLOAD",
94 "SSH_SCP_DONE",
95 "SSH_SCP_SEND_EOF",
96 "SSH_SCP_WAIT_EOF",
97 "SSH_SCP_WAIT_CLOSE",
98 "SSH_SCP_CHANNEL_FREE",
99 "SSH_SESSION_DISCONNECT",
100 "SSH_SESSION_FREE",
101 "QUIT"
102 };
103 /* a precaution to make sure the lists are in sync */
104 DEBUGASSERT(CURL_ARRAYSIZE(names) == SSH_LAST);
105 return ((size_t)state < CURL_ARRAYSIZE(names)) ? names[state] : "";
106}
107#endif /* CURLVERBOSE */
109/*
110 * SSH State machine related code
111 */
112/* This is the ONLY way to change SSH state! */
113void Curl_ssh_set_state(struct Curl_easy *data,
114 struct ssh_conn *sshc,
115 sshstate nowstate)
116{
117#ifdef CURLVERBOSE
118 if(sshc->state != nowstate) {
119 CURL_TRC_SSH(data, "[%s] -> [%s]",
120 Curl_ssh_statename(sshc->state),
121 Curl_ssh_statename(nowstate));
122 }
123#else
124 (void)data;
125#endif
126 sshc->state = nowstate;
127}
129#define MAX_SSHPATH_LEN 100000 /* arbitrary */
131/* figure out the path to work with in this particular request */
132CURLcode Curl_getworkingpath(struct Curl_easy *data,
133 const char *homedir, /* when SFTP is used */
134 char **path) /* returns the allocated
135 real path to work with */
136{
137 char *working_path;
138 size_t working_path_len;
139 struct dynbuf npath;
140 CURLcode result =
141 Curl_urldecode(data->state.up.path, 0, &working_path,
142 &working_path_len, REJECT_ZERO);
143 if(result)
144 return result;
146 /* new path to switch to in case we need to */
147 curlx_dyn_init(&npath, MAX_SSHPATH_LEN);
149 /* Check for /~/, indicating relative to the user's home directory */
150 if((data->conn->scheme->protocol & CURLPROTO_SCP) &&
151 (working_path_len > 3) && (!memcmp(working_path, "/~/", 3))) {
152 /* It is referenced to the home directory, so strip the leading '/~/' */
153 if(curlx_dyn_addn(&npath, &working_path[3], working_path_len - 3)) {
154 curlx_free(working_path);
155 return CURLE_OUT_OF_MEMORY;
156 }
157 }
158 else if((data->conn->scheme->protocol & CURLPROTO_SFTP) &&
159 (!strcmp("/~", working_path) ||
160 ((working_path_len > 2) && !memcmp(working_path, "/~/", 3)))) {
161 if(curlx_dyn_add(&npath, homedir)) {
162 curlx_free(working_path);
163 return CURLE_OUT_OF_MEMORY;
164 }
165 if(working_path_len > 2) {
166 size_t len;
167 const char *p;
168 int copyfrom = 3;
169 /* Copy a separating '/' if homedir does not end with one */
170 len = curlx_dyn_len(&npath);
171 p = curlx_dyn_ptr(&npath);
172 if(len && (p[len - 1] != '/'))
173 copyfrom = 2;
175 if(curlx_dyn_addn(&npath, &working_path[copyfrom],
176 working_path_len - copyfrom)) {
177 curlx_free(working_path);
178 return CURLE_OUT_OF_MEMORY;
179 }
180 }
181 else {
182 if(curlx_dyn_add(&npath, "/")) {
183 curlx_free(working_path);
184 return CURLE_OUT_OF_MEMORY;
185 }
186 }
187 }
189 if(curlx_dyn_len(&npath)) {
190 curlx_free(working_path);
192 /* store the pointer for the caller to receive */
193 *path = curlx_dyn_ptr(&npath);
194 }
195 else
196 *path = working_path;
197 DEBUGASSERT(*path && (*path)[0]);
199 return CURLE_OK;
200}
202#define MAX_PATHLENGTH 65535 /* arbitrary long */
204CURLcode Curl_get_pathname(const char **cpp, char **path, const char *homedir)
205{
206 const char *cp = *cpp;
207 struct dynbuf out;
208 CURLcode result;
210 DEBUGASSERT(homedir);
211 *path = NULL;
212 *cpp = NULL;
213 if(!*cp || !homedir)
214 return CURLE_QUOTE_ERROR;
216 curlx_dyn_init(&out, MAX_PATHLENGTH);
218 /* Ignore leading whitespace */
219 curlx_str_passblanks(&cp);
221 /* Check for quoted filenames */
222 if(*cp == '\"' || *cp == '\'') {
223 char quot = *cp++;
225 /* Search for terminating quote, unescape some chars */
226 while(*cp != quot) {
227 if(!*cp) /* End of string */
228 goto fail;
230 if(*cp == '\\') { /* Escaped characters */
231 cp++;
232 if(*cp != '\'' && *cp != '\"' && *cp != '\\')
233 goto fail;
234 }
235 result = curlx_dyn_addn(&out, cp, 1);
236 if(result)
237 return result;
238 cp++;
239 }
240 cp++; /* pass the end quote */
242 if(!curlx_dyn_len(&out))
243 goto fail;
244 }
245 else {
246 struct Curl_str word;
247 bool content = FALSE;
248 int rc;
249 /* Handling for relative path - prepend home directory */
250 if(cp[0] == '/' && cp[1] == '~' && cp[2] == '/') {
251 result = curlx_dyn_add(&out, homedir);
252 if(!result)
253 result = curlx_dyn_addn(&out, "/", 1);
254 if(result)
255 return result;
256 cp += 3;
257 content = TRUE;
258 }
259 /* Read to end of filename - either to whitespace or terminator */
260 rc = curlx_str_word(&cp, &word, MAX_PATHLENGTH);
261 if(rc) {
262 if(rc == STRE_BIG) {
263 curlx_dyn_free(&out);
264 return CURLE_TOO_LARGE;
265 }
266 else if(!content)
267 /* no path, no word, this is incorrect */
268 goto fail;
269 }
270 else {
271 /* append the word */
272 result = curlx_dyn_addn(&out, curlx_str(&word), curlx_strlen(&word));
273 if(result)
274 return result;
275 }
276 }
277 /* skip whitespace */
278 curlx_str_passblanks(&cp);
280 /* return pointer to second parameter if it exists */
281 *cpp = cp;
283 *path = curlx_dyn_ptr(&out);
284 return CURLE_OK;
286fail:
287 curlx_dyn_free(&out);
288 return CURLE_QUOTE_ERROR;
289}
291CURLcode Curl_ssh_range(struct Curl_easy *data,
292 const char *range, curl_off_t filesize,
293 curl_off_t *startp, curl_off_t *sizep)
294{
295 curl_off_t from, to;
296 int to_t;
297 int from_t = curlx_str_number(&range, &from, CURL_OFF_T_MAX);
298 if(from_t == STRE_OVERFLOW)
299 return CURLE_RANGE_ERROR;
300 curlx_str_passblanks(&range);
301 (void)curlx_str_single(&range, '-');
303 to_t = curlx_str_numblanks(&range, &to);
304 if((to_t == STRE_OVERFLOW) || (to_t && from_t) || *range)
305 return CURLE_RANGE_ERROR;
307 if(from_t) {
308 /* no start point given, set from relative to end of file */
309 if(!to)
310 /* "-0" is not a fine range */
311 return CURLE_RANGE_ERROR;
312 else if(to > filesize)
313 to = filesize;
314 from = filesize - to;
315 to = filesize - 1;
316 }
317 else if(from > filesize) {
318 failf(data, "Offset (%" FMT_OFF_T ") was beyond file size (%"
319 FMT_OFF_T ")", from, filesize);
320 return CURLE_RANGE_ERROR;
321 }
322 else if((to_t == STRE_NO_NUM) || (to >= filesize))
323 to = filesize - 1;
325 if(from > to) {
326 failf(data, "Bad range: start offset larger than end offset");
327 return CURLE_RANGE_ERROR;
328 }
329 if((to - from) == CURL_OFF_T_MAX)
330 return CURLE_RANGE_ERROR;
332 *startp = from;
333 *sizep = to - from + 1;
334 return CURLE_OK;
335}
337/* called by the multi interface to figure out what socket(s) to wait for and
338 for what actions in the DO_DONE, PERFORM and WAITPERFORM states */
339CURLcode Curl_ssh_pollset(struct Curl_easy *data, struct easy_pollset *ps)
340{
341 struct connectdata *conn = data->conn;
342 struct ssh_conn *sshc = Curl_conn_meta_get(conn, CURL_META_SSH_CONN);
343 curl_socket_t sock = conn->sock[FIRSTSOCKET];
344 int waitfor;
346 if(!sshc || (sock == CURL_SOCKET_BAD))
347 return CURLE_FAILED_INIT;
349 waitfor = sshc->waitfor ? sshc->waitfor : data->req.io_flags;
350 if(waitfor) {
351 int flags = 0;
352 if(waitfor & REQ_IO_RECV)
353 flags |= CURL_POLL_IN;
354 if(waitfor & REQ_IO_SEND)
355 flags |= CURL_POLL_OUT;
356 DEBUGASSERT(flags);
357 CURL_TRC_SSH(data, "pollset, flags=%x", (unsigned int)flags);
358 return Curl_pollset_change(data, ps, sock, flags, 0);
359 }
360 /* While we still have a session, we listen incoming data. */
361 if(sshc->ssh_session)
362 return Curl_pollset_change(data, ps, sock, CURL_POLL_IN, 0);
363 return CURLE_OK;
364}
366CURLcode Curl_ssh_setup_pkey(struct Curl_easy *data, struct ssh_conn *sshc)
367{
368 char *home = NULL;
369 if(data->set.ssh_auth_types & CURLSSH_AUTH_PUBLICKEY) {
370 const char *str;
372 sshc->pub_key = sshc->priv_key = NULL;
374 if(CURL_EASY_STR(data, STRING_SSH_PRIVATE_KEY)) {
375 sshc->priv_key = curlx_strdup(
376 CURL_EASY_STR(data, STRING_SSH_PRIVATE_KEY));
377 if(!sshc->priv_key)
378 goto fail;
379 }
380 else {
381 /* To ponder about: should really the lib be messing about with the HOME
382 environment variable etc? */
383 curlx_struct_stat sbuf;
384 home = curl_getenv("HOME");
386 /* If no private key file is specified, try some common paths. */
387 if(home) {
388 /* Try ~/.ssh first. */
389 sshc->priv_key = curl_maprintf("%s/.ssh/id_rsa", home);
390 if(!sshc->priv_key)
391 goto fail;
392 else if(curlx_stat(sshc->priv_key, &sbuf)) {
393 curlx_free(sshc->priv_key);
394 sshc->priv_key = curl_maprintf("%s/.ssh/id_dsa", home);
395 if(!sshc->priv_key)
396 goto fail;
397 else if(curlx_stat(sshc->priv_key, &sbuf)) {
398 curlx_safefree(sshc->priv_key);
399 }
400 }
401 curlx_safefree(home);
402 }
403 if(!sshc->priv_key) {
404 /* Nothing found; try the current dir. */
405 sshc->priv_key = curlx_strdup("id_rsa");
406 if(sshc->priv_key && curlx_stat(sshc->priv_key, &sbuf)) {
407 curlx_free(sshc->priv_key);
408 sshc->priv_key = curlx_strdup("id_dsa");
409 if(sshc->priv_key && curlx_stat(sshc->priv_key, &sbuf)) {
410 curlx_free(sshc->priv_key);
411 /* Out of guesses. Set to the empty string to avoid
412 * surprising info messages. */
413 sshc->priv_key = curlx_strdup("");
414 }
415 }
416 }
417 }
419 /*
420 * Unless the user explicitly specifies a public key file, let the SSH
421 * library extract the public key from the private key file. This is done
422 * by passing sshc->pub_key = NULL.
423 */
424 str = CURL_EASY_STR(data, STRING_SSH_PUBLIC_KEY);
425 if(str && *str) { /* treat empty string the same way as NULL */
426 sshc->pub_key = curlx_strdup(str);
427 if(!sshc->pub_key)
428 goto fail;
429 }
431 {
432 const char *keypasswd = CURL_EASY_STR(data, STRING_KEY_PASSWD);
433 if(keypasswd) {
434 sshc->passphrase = curlx_strdup(keypasswd);
435 if(!sshc->passphrase)
436 goto fail;
437 }
438 }
440 if(sshc->pub_key)
441 infof(data, "SSH: public key file '%s'", sshc->pub_key);
442 if(sshc->priv_key)
443 infof(data, "SSH: private key file '%s'", sshc->priv_key);
444 else
445 infof(data, "SSH: public key auth without private key set!");
446 }
447 return CURLE_OK;
449fail:
450 curlx_safefree(home);
451 curlx_safefree(sshc->priv_key);
452 curlx_safefree(sshc->pub_key);
453 return CURLE_OUT_OF_MEMORY;
454}
456#endif /* USE_SSH */
Diagnostics4 git calls · 34 ms
CallStatusSizeTime034 ms
discover200152 B4 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=4 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200149 B · hit4 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=4 git.response.bytes=149 git.response.cache=HIT git.response.duration_ms=4 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=149 git.pkt.payload.max=78 git.ls_refs.count=2 git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit6 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=5 git.response.bytes=293470 git.response.cache=HIT git.response.duration_ms=6 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=10 git.pkt.data=8 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=293470 git.pkt.payload.max=65516 git.sideband.band1.bytes=293354 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=87 git.pack.bytes=293354 git.pack.ofs_delta=0 git.pack.ref_delta=1 git.pack.chain.max=1 git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0 git.pack.full=86Open pack map
fetch2004.5 KiB · hit24 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=24 git.response.bytes=4616 git.response.cache=HIT git.response.duration_ms=24 git.fetch.wants=6bb8a460eb404040eeb9e7a4f764e955654df526 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=4616 git.pkt.payload.max=4595 git.sideband.band1.bytes=4594 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
- Ref
- refs/heads/master
- Commit
- 31c911efe5652725e930f4d2e60c9060e5f6646d
- cf-ray
- a5eaf41f9dbbb36e
- Request
- a52c76d5…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format