|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "curl_setup.h" |
|
|
|
|
|
#if defined(USE_SSH) |
|
|
|
|
|
#include "curl_path.h" |
|
|
#include <curl/curl.h> |
|
|
#include "curl_memory.h" |
|
|
#include "escape.h" |
|
|
#include "memdebug.h" |
|
|
|
|
|
#define MAX_SSHPATH_LEN 100000 |
|
|
|
|
|
|
|
|
CURLcode Curl_getworkingpath(struct Curl_easy *data, |
|
|
char *homedir, |
|
|
char **path) |
|
|
|
|
|
{ |
|
|
char *working_path; |
|
|
size_t working_path_len; |
|
|
struct dynbuf npath; |
|
|
CURLcode result = |
|
|
Curl_urldecode(data->state.up.path, 0, &working_path, |
|
|
&working_path_len, REJECT_ZERO); |
|
|
if(result) |
|
|
return result; |
|
|
|
|
|
|
|
|
Curl_dyn_init(&npath, MAX_SSHPATH_LEN); |
|
|
|
|
|
|
|
|
if((data->conn->handler->protocol & CURLPROTO_SCP) && |
|
|
(working_path_len > 3) && (!memcmp(working_path, "/~/", 3))) { |
|
|
|
|
|
if(Curl_dyn_addn(&npath, &working_path[3], working_path_len - 3)) { |
|
|
free(working_path); |
|
|
return CURLE_OUT_OF_MEMORY; |
|
|
} |
|
|
} |
|
|
else if((data->conn->handler->protocol & CURLPROTO_SFTP) && |
|
|
(!strcmp("/~", working_path) || |
|
|
((working_path_len > 2) && !memcmp(working_path, "/~/", 3)))) { |
|
|
if(Curl_dyn_add(&npath, homedir)) { |
|
|
free(working_path); |
|
|
return CURLE_OUT_OF_MEMORY; |
|
|
} |
|
|
if(working_path_len > 2) { |
|
|
size_t len; |
|
|
const char *p; |
|
|
int copyfrom = 3; |
|
|
|
|
|
len = Curl_dyn_len(&npath); |
|
|
p = Curl_dyn_ptr(&npath); |
|
|
if(len && (p[len-1] != '/')) |
|
|
copyfrom = 2; |
|
|
|
|
|
if(Curl_dyn_addn(&npath, |
|
|
&working_path[copyfrom], working_path_len - copyfrom)) { |
|
|
free(working_path); |
|
|
return CURLE_OUT_OF_MEMORY; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
if(Curl_dyn_len(&npath)) { |
|
|
free(working_path); |
|
|
|
|
|
|
|
|
*path = Curl_dyn_ptr(&npath); |
|
|
} |
|
|
else |
|
|
*path = working_path; |
|
|
|
|
|
return CURLE_OK; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define MAX_PATHLENGTH 65535 |
|
|
|
|
|
CURLcode Curl_get_pathname(const char **cpp, char **path, const char *homedir) |
|
|
{ |
|
|
const char *cp = *cpp, *end; |
|
|
char quot; |
|
|
unsigned int i; |
|
|
static const char WHITESPACE[] = " \t\r\n"; |
|
|
struct dynbuf out; |
|
|
CURLcode result; |
|
|
|
|
|
DEBUGASSERT(homedir); |
|
|
*path = NULL; |
|
|
*cpp = NULL; |
|
|
if(!*cp || !homedir) |
|
|
return CURLE_QUOTE_ERROR; |
|
|
|
|
|
Curl_dyn_init(&out, MAX_PATHLENGTH); |
|
|
|
|
|
|
|
|
cp += strspn(cp, WHITESPACE); |
|
|
|
|
|
|
|
|
if(*cp == '\"' || *cp == '\'') { |
|
|
quot = *cp++; |
|
|
|
|
|
|
|
|
for(i = 0; i <= strlen(cp); i++) { |
|
|
if(cp[i] == quot) { |
|
|
i++; |
|
|
break; |
|
|
} |
|
|
if(cp[i] == '\0') { |
|
|
goto fail; |
|
|
} |
|
|
if(cp[i] == '\\') { |
|
|
i++; |
|
|
if(cp[i] != '\'' && cp[i] != '\"' && |
|
|
cp[i] != '\\') { |
|
|
goto fail; |
|
|
} |
|
|
} |
|
|
result = Curl_dyn_addn(&out, &cp[i], 1); |
|
|
if(result) |
|
|
return result; |
|
|
} |
|
|
|
|
|
if(!Curl_dyn_len(&out)) |
|
|
goto fail; |
|
|
|
|
|
|
|
|
*cpp = &cp[i] + strspn(&cp[i], WHITESPACE); |
|
|
} |
|
|
else { |
|
|
|
|
|
end = strpbrk(cp, WHITESPACE); |
|
|
if(!end) |
|
|
end = strchr(cp, '\0'); |
|
|
|
|
|
|
|
|
*cpp = end + strspn(end, WHITESPACE); |
|
|
|
|
|
|
|
|
if(cp[0] == '/' && cp[1] == '~' && cp[2] == '/') { |
|
|
result = Curl_dyn_add(&out, homedir); |
|
|
if(!result) |
|
|
result = Curl_dyn_addn(&out, "/", 1); |
|
|
if(result) |
|
|
return result; |
|
|
cp += 3; |
|
|
} |
|
|
|
|
|
result = Curl_dyn_addn(&out, cp, (end - cp)); |
|
|
if(result) |
|
|
return result; |
|
|
} |
|
|
*path = Curl_dyn_ptr(&out); |
|
|
return CURLE_OK; |
|
|
|
|
|
fail: |
|
|
Curl_dyn_free(&out); |
|
|
return CURLE_QUOTE_ERROR; |
|
|
} |
|
|
|
|
|
#endif |
|
|
|