1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
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.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
27 #include <curl/curl.h>
28 #include "curl_memory.h"
29 #include "curl_path.h"
33 /* figure out the path to work with in this particular request */
34 CURLcode Curl_getworkingpath(struct connectdata *conn,
35 char *homedir, /* when SFTP is used */
36 char **path) /* returns the allocated
37 real path to work with */
39 struct Curl_easy *data = conn->data;
40 char *real_path = NULL;
42 size_t working_path_len;
44 Curl_urldecode(data, data->state.up.path, 0, &working_path,
45 &working_path_len, REJECT_ZERO);
49 /* Check for /~/, indicating relative to the user's home directory */
50 if(conn->handler->protocol & CURLPROTO_SCP) {
51 real_path = malloc(working_path_len + 1);
52 if(real_path == NULL) {
54 return CURLE_OUT_OF_MEMORY;
56 if((working_path_len > 3) && (!memcmp(working_path, "/~/", 3)))
57 /* It is referenced to the home directory, so strip the leading '/~/' */
58 memcpy(real_path, working_path + 3, working_path_len - 2);
60 memcpy(real_path, working_path, 1 + working_path_len);
62 else if(conn->handler->protocol & CURLPROTO_SFTP) {
63 if((working_path_len > 1) && (working_path[1] == '~')) {
64 size_t homelen = strlen(homedir);
65 real_path = malloc(homelen + working_path_len + 1);
66 if(real_path == NULL) {
68 return CURLE_OUT_OF_MEMORY;
70 /* It is referenced to the home directory, so strip the
72 memcpy(real_path, homedir, homelen);
73 real_path[homelen] = '/';
74 real_path[homelen + 1] = '\0';
75 if(working_path_len > 3) {
76 memcpy(real_path + homelen + 1, working_path + 3,
77 1 + working_path_len -3);
81 real_path = malloc(working_path_len + 1);
82 if(real_path == NULL) {
84 return CURLE_OUT_OF_MEMORY;
86 memcpy(real_path, working_path, 1 + working_path_len);
92 /* store the pointer for the caller to receive */
98 /* The get_pathname() function is being borrowed from OpenSSH sftp.c
101 * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
103 * Permission to use, copy, modify, and distribute this software for any
104 * purpose with or without fee is hereby granted, provided that the above
105 * copyright notice and this permission notice appear in all copies.
107 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
108 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
109 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
110 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
111 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
112 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
113 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
115 CURLcode Curl_get_pathname(const char **cpp, char **path, char *homedir)
117 const char *cp = *cpp, *end;
120 size_t fullPathLength, pathLength;
121 bool relativePath = false;
122 static const char WHITESPACE[] = " \t\r\n";
127 return CURLE_QUOTE_ERROR;
129 /* Ignore leading whitespace */
130 cp += strspn(cp, WHITESPACE);
131 /* Allocate enough space for home directory and filename + separator */
132 fullPathLength = strlen(cp) + strlen(homedir) + 2;
133 *path = malloc(fullPathLength);
135 return CURLE_OUT_OF_MEMORY;
137 /* Check for quoted filenames */
138 if(*cp == '\"' || *cp == '\'') {
141 /* Search for terminating quote, unescape some chars */
142 for(i = j = 0; i <= strlen(cp); i++) {
143 if(cp[i] == quot) { /* Found quote */
148 if(cp[i] == '\0') { /* End of string */
149 /*error("Unterminated quote");*/
152 if(cp[i] == '\\') { /* Escaped characters */
154 if(cp[i] != '\'' && cp[i] != '\"' &&
156 /*error("Bad escaped character '\\%c'",
161 (*path)[j++] = cp[i];
165 /*error("Empty quotes");*/
168 *cpp = cp + i + strspn(cp + i, WHITESPACE);
171 /* Read to end of filename - either to whitespace or terminator */
172 end = strpbrk(cp, WHITESPACE);
174 end = strchr(cp, '\0');
175 /* return pointer to second parameter if it exists */
176 *cpp = end + strspn(end, WHITESPACE);
178 relativePath = (cp[0] == '/' && cp[1] == '~' && cp[2] == '/');
179 /* Handling for relative path - prepend home directory */
181 strcpy(*path, homedir);
182 pathLength = strlen(homedir);
183 (*path)[pathLength++] = '/';
184 (*path)[pathLength] = '\0';
187 /* Copy path name up until first "whitespace" */
188 memcpy(&(*path)[pathLength], cp, (int)(end - cp));
189 pathLength += (int)(end - cp);
190 (*path)[pathLength] = '\0';
195 Curl_safefree(*path);
196 return CURLE_QUOTE_ERROR;
199 #endif /* if SSH is used */