curl_path.c 6.4 KB

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