#include "../curl_setup.h"
#ifdef USE_SSH
#include "curl_path.h"
#include <curl/curl.h>
#include "../curlx/strparse.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;
curlx_dyn_init(&npath, MAX_SSHPATH_LEN);
if((data->conn->handler->protocol & CURLPROTO_SCP) &&
(working_path_len > 3) && (!memcmp(working_path, "/~/", 3))) {
if(curlx_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(curlx_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 = curlx_dyn_len(&npath);
p = curlx_dyn_ptr(&npath);
if(len && (p[len-1] != '/'))
copyfrom = 2;
if(curlx_dyn_addn(&npath, &working_path[copyfrom],
working_path_len - copyfrom)) {
free(working_path);
return CURLE_OUT_OF_MEMORY;
}
}
else {
if(curlx_dyn_add(&npath, "/")) {
free(working_path);
return CURLE_OUT_OF_MEMORY;
}
}
}
if(curlx_dyn_len(&npath)) {
free(working_path);
*path = curlx_dyn_ptr(&npath);
}
else
*path = working_path;
DEBUGASSERT(*path && (*path)[0]);
return CURLE_OK;
}
#define MAX_PATHLENGTH 65535
CURLcode Curl_get_pathname(const char **cpp, char **path, const char *homedir)
{
const char *cp = *cpp;
struct dynbuf out;
CURLcode result;
DEBUGASSERT(homedir);
*path = NULL;
*cpp = NULL;
if(!*cp || !homedir)
return CURLE_QUOTE_ERROR;
curlx_dyn_init(&out, MAX_PATHLENGTH);
curlx_str_passblanks(&cp);
if(*cp == '\"' || *cp == '\'') {
char quot = *cp++;
while(*cp != quot) {
if(!*cp)
goto fail;
if(*cp == '\\') {
cp++;
if(*cp != '\'' && *cp != '\"' && *cp != '\\')
goto fail;
}
result = curlx_dyn_addn(&out, cp, 1);
if(result)
return result;
cp++;
}
cp++;
if(!curlx_dyn_len(&out))
goto fail;
}
else {
struct Curl_str word;
bool content = FALSE;
int rc;
if(cp[0] == '/' && cp[1] == '~' && cp[2] == '/') {
result = curlx_dyn_add(&out, homedir);
if(!result)
result = curlx_dyn_addn(&out, "/", 1);
if(result)
return result;
cp += 3;
content = TRUE;
}
rc = curlx_str_word(&cp, &word, MAX_PATHLENGTH);
if(rc) {
if(rc == STRE_BIG) {
curlx_dyn_free(&out);
return CURLE_TOO_LARGE;
}
else if(!content)
goto fail;
}
else {
result = curlx_dyn_addn(&out, curlx_str(&word), curlx_strlen(&word));
if(result)
return result;
}
}
curlx_str_passblanks(&cp);
*cpp = cp;
*path = curlx_dyn_ptr(&out);
return CURLE_OK;
fail:
curlx_dyn_free(&out);
return CURLE_QUOTE_ERROR;
}
#endif