1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2022, 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.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 * SPDX-License-Identifier: curl AND ISC
23 ***************************************************************************/
25 #include "curl_setup.h"
29 #include <curl/curl.h>
30 #include "curl_memory.h"
31 #include "curl_path.h"
35 /* figure out the path to work with in this particular request */
36 CURLcode Curl_getworkingpath(struct Curl_easy *data,
37 char *homedir, /* when SFTP is used */
38 char **path) /* returns the allocated
39 real path to work with */
41 char *real_path = NULL;
43 size_t working_path_len;
45 Curl_urldecode(data->state.up.path, 0, &working_path,
46 &working_path_len, REJECT_ZERO);
50 /* Check for /~/, indicating relative to the user's home directory */
51 if(data->conn->handler->protocol & CURLPROTO_SCP) {
52 real_path = malloc(working_path_len + 1);
55 return CURLE_OUT_OF_MEMORY;
57 if((working_path_len > 3) && (!memcmp(working_path, "/~/", 3)))
58 /* It is referenced to the home directory, so strip the leading '/~/' */
59 memcpy(real_path, working_path + 3, working_path_len - 2);
61 memcpy(real_path, working_path, 1 + working_path_len);
63 else if(data->conn->handler->protocol & CURLPROTO_SFTP) {
64 if((working_path_len > 1) && (working_path[1] == '~')) {
65 size_t homelen = strlen(homedir);
66 real_path = malloc(homelen + working_path_len + 1);
69 return CURLE_OUT_OF_MEMORY;
71 /* It is referenced to the home directory, so strip the
73 memcpy(real_path, homedir, homelen);
74 real_path[homelen] = '/';
75 real_path[homelen + 1] = '\0';
76 if(working_path_len > 3) {
77 memcpy(real_path + homelen + 1, working_path + 3,
78 1 + working_path_len -3);
82 real_path = malloc(working_path_len + 1);
85 return CURLE_OUT_OF_MEMORY;
87 memcpy(real_path, working_path, 1 + working_path_len);
93 /* store the pointer for the caller to receive */
99 /* The get_pathname() function is being borrowed from OpenSSH sftp.c
102 * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
104 * Permission to use, copy, modify, and distribute this software for any
105 * purpose with or without fee is hereby granted, provided that the above
106 * copyright notice and this permission notice appear in all copies.
108 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
109 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
110 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
111 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
112 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
113 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
114 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
116 CURLcode Curl_get_pathname(const char **cpp, char **path, char *homedir)
118 const char *cp = *cpp, *end;
121 size_t fullPathLength, pathLength;
122 bool relativePath = false;
123 static const char WHITESPACE[] = " \t\r\n";
125 DEBUGASSERT(homedir);
126 if(!*cp || !homedir) {
129 return CURLE_QUOTE_ERROR;
131 /* Ignore leading whitespace */
132 cp += strspn(cp, WHITESPACE);
133 /* Allocate enough space for home directory and filename + separator */
134 fullPathLength = strlen(cp) + strlen(homedir) + 2;
135 *path = malloc(fullPathLength);
137 return CURLE_OUT_OF_MEMORY;
139 /* Check for quoted filenames */
140 if(*cp == '\"' || *cp == '\'') {
143 /* Search for terminating quote, unescape some chars */
144 for(i = j = 0; i <= strlen(cp); i++) {
145 if(cp[i] == quot) { /* Found quote */
150 if(cp[i] == '\0') { /* End of string */
151 /*error("Unterminated quote");*/
154 if(cp[i] == '\\') { /* Escaped characters */
156 if(cp[i] != '\'' && cp[i] != '\"' &&
158 /*error("Bad escaped character '\\%c'",
163 (*path)[j++] = cp[i];
167 /*error("Empty quotes");*/
170 *cpp = cp + i + strspn(cp + i, WHITESPACE);
173 /* Read to end of filename - either to whitespace or terminator */
174 end = strpbrk(cp, WHITESPACE);
176 end = strchr(cp, '\0');
177 /* return pointer to second parameter if it exists */
178 *cpp = end + strspn(end, WHITESPACE);
180 relativePath = (cp[0] == '/' && cp[1] == '~' && cp[2] == '/');
181 /* Handling for relative path - prepend home directory */
183 strcpy(*path, homedir);
184 pathLength = strlen(homedir);
185 (*path)[pathLength++] = '/';
186 (*path)[pathLength] = '\0';
189 /* Copy path name up until first "whitespace" */
190 memcpy(&(*path)[pathLength], cp, (int)(end - cp));
191 pathLength += (int)(end - cp);
192 (*path)[pathLength] = '\0';
197 Curl_safefree(*path);
198 return CURLE_QUOTE_ERROR;
201 #endif /* if SSH is used */