VirtualBox

source: vbox/trunk/src/libs/curl-7.87.0/lib/curl_path.c@ 99005

Last change on this file since 99005 was 98326, checked in by vboxsync, 2 years ago

curl-7.87.0: Applied and adjusted our curl changes to 7.83.1. bugref:10356

  • Property svn:eol-style set to native
File size: 6.4 KB
Line 
1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2022, Daniel Stenberg, <[email protected]>, 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
25#include "curl_setup.h"
26
27#if defined(USE_SSH)
28
29#include <curl/curl.h>
30#include "curl_memory.h"
31#include "curl_path.h"
32#include "escape.h"
33#include "memdebug.h"
34
35/* figure out the path to work with in this particular request */
36CURLcode 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 */
40{
41 char *real_path = NULL;
42 char *working_path;
43 size_t working_path_len;
44 CURLcode result =
45 Curl_urldecode(data->state.up.path, 0, &working_path,
46 &working_path_len, REJECT_ZERO);
47 if(result)
48 return result;
49
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);
53 if(!real_path) {
54 free(working_path);
55 return CURLE_OUT_OF_MEMORY;
56 }
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);
60 else
61 memcpy(real_path, working_path, 1 + working_path_len);
62 }
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);
67 if(!real_path) {
68 free(working_path);
69 return CURLE_OUT_OF_MEMORY;
70 }
71 /* It is referenced to the home directory, so strip the
72 leading '/' */
73 memcpy(real_path, homedir, homelen);
74 /* Only add a trailing '/' if homedir does not end with one */
75 if(homelen == 0 || real_path[homelen - 1] != '/') {
76 real_path[homelen] = '/';
77 homelen++;
78 real_path[homelen] = '\0';
79 }
80 if(working_path_len > 3) {
81 memcpy(real_path + homelen, working_path + 3,
82 1 + working_path_len -3);
83 }
84 }
85 else {
86 real_path = malloc(working_path_len + 1);
87 if(!real_path) {
88 free(working_path);
89 return CURLE_OUT_OF_MEMORY;
90 }
91 memcpy(real_path, working_path, 1 + working_path_len);
92 }
93 }
94
95 free(working_path);
96
97 /* store the pointer for the caller to receive */
98 *path = real_path;
99
100 return CURLE_OK;
101}
102
103/* The get_pathname() function is being borrowed from OpenSSH sftp.c
104 version 4.6p1. */
105/*
106 * Copyright (c) 2001-2004 Damien Miller <[email protected]>
107 *
108 * Permission to use, copy, modify, and distribute this software for any
109 * purpose with or without fee is hereby granted, provided that the above
110 * copyright notice and this permission notice appear in all copies.
111 *
112 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
113 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
114 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
115 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
116 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
117 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
118 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
119 */
120CURLcode Curl_get_pathname(const char **cpp, char **path, char *homedir)
121{
122 const char *cp = *cpp, *end;
123 char quot;
124 unsigned int i, j;
125 size_t fullPathLength, pathLength;
126 bool relativePath = false;
127 static const char WHITESPACE[] = " \t\r\n";
128
129 DEBUGASSERT(homedir);
130 if(!*cp || !homedir) {
131 *cpp = NULL;
132 *path = NULL;
133 return CURLE_QUOTE_ERROR;
134 }
135 /* Ignore leading whitespace */
136 cp += strspn(cp, WHITESPACE);
137 /* Allocate enough space for home directory and filename + separator */
138 fullPathLength = strlen(cp) + strlen(homedir) + 2;
139 *path = malloc(fullPathLength);
140 if(!*path)
141 return CURLE_OUT_OF_MEMORY;
142
143 /* Check for quoted filenames */
144 if(*cp == '\"' || *cp == '\'') {
145 quot = *cp++;
146
147 /* Search for terminating quote, unescape some chars */
148 for(i = j = 0; i <= strlen(cp); i++) {
149 if(cp[i] == quot) { /* Found quote */
150 i++;
151 (*path)[j] = '\0';
152 break;
153 }
154 if(cp[i] == '\0') { /* End of string */
155 goto fail;
156 }
157 if(cp[i] == '\\') { /* Escaped characters */
158 i++;
159 if(cp[i] != '\'' && cp[i] != '\"' &&
160 cp[i] != '\\') {
161 goto fail;
162 }
163 }
164 (*path)[j++] = cp[i];
165 }
166
167 if(j == 0) {
168 goto fail;
169 }
170 *cpp = cp + i + strspn(cp + i, WHITESPACE);
171 }
172 else {
173 /* Read to end of filename - either to whitespace or terminator */
174 end = strpbrk(cp, WHITESPACE);
175 if(!end)
176 end = strchr(cp, '\0');
177 /* return pointer to second parameter if it exists */
178 *cpp = end + strspn(end, WHITESPACE);
179 pathLength = 0;
180 relativePath = (cp[0] == '/' && cp[1] == '~' && cp[2] == '/');
181 /* Handling for relative path - prepend home directory */
182 if(relativePath) {
183 strcpy(*path, homedir);
184 pathLength = strlen(homedir);
185 (*path)[pathLength++] = '/';
186 (*path)[pathLength] = '\0';
187 cp += 3;
188 }
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';
193 }
194 return CURLE_OK;
195
196 fail:
197 Curl_safefree(*path);
198 return CURLE_QUOTE_ERROR;
199}
200
201#endif /* if SSH is used */
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette