curl_path.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, 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. #error #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. real_path[homelen] = '/';
  70. real_path[homelen + 1] = '\0';
  71. if(working_path_len > 3) {
  72. memcpy(real_path + homelen + 1, working_path + 3,
  73. 1 + working_path_len -3);
  74. }
  75. }
  76. else {
  77. real_path = malloc(working_path_len + 1);
  78. if(!real_path) {
  79. free(working_path);
  80. return CURLE_OUT_OF_MEMORY;
  81. }
  82. memcpy(real_path, working_path, 1 + working_path_len);
  83. }
  84. }
  85. free(working_path);
  86. /* store the pointer for the caller to receive */
  87. *path = real_path;
  88. return CURLE_OK;
  89. }
  90. /* The get_pathname() function is being borrowed from OpenSSH sftp.c
  91. version 4.6p1. */
  92. /*
  93. * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
  94. *
  95. * Permission to use, copy, modify, and distribute this software for any
  96. * purpose with or without fee is hereby granted, provided that the above
  97. * copyright notice and this permission notice appear in all copies.
  98. *
  99. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  100. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  101. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  102. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  103. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  104. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  105. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  106. */
  107. CURLcode Curl_get_pathname(const char **cpp, char **path, char *homedir)
  108. {
  109. const char *cp = *cpp, *end;
  110. char quot;
  111. unsigned int i, j;
  112. size_t fullPathLength, pathLength;
  113. bool relativePath = false;
  114. static const char WHITESPACE[] = " \t\r\n";
  115. DEBUGASSERT(homedir);
  116. if(!*cp || !homedir) {
  117. *cpp = NULL;
  118. *path = NULL;
  119. return CURLE_QUOTE_ERROR;
  120. }
  121. /* Ignore leading whitespace */
  122. cp += strspn(cp, WHITESPACE);
  123. /* Allocate enough space for home directory and filename + separator */
  124. fullPathLength = strlen(cp) + strlen(homedir) + 2;
  125. *path = malloc(fullPathLength);
  126. if(!*path)
  127. return CURLE_OUT_OF_MEMORY;
  128. /* Check for quoted filenames */
  129. if(*cp == '\"' || *cp == '\'') {
  130. quot = *cp++;
  131. /* Search for terminating quote, unescape some chars */
  132. for(i = j = 0; i <= strlen(cp); i++) {
  133. if(cp[i] == quot) { /* Found quote */
  134. i++;
  135. (*path)[j] = '\0';
  136. break;
  137. }
  138. if(cp[i] == '\0') { /* End of string */
  139. /*error("Unterminated quote");*/
  140. goto fail;
  141. }
  142. if(cp[i] == '\\') { /* Escaped characters */
  143. i++;
  144. if(cp[i] != '\'' && cp[i] != '\"' &&
  145. cp[i] != '\\') {
  146. /*error("Bad escaped character '\\%c'",
  147. cp[i]);*/
  148. goto fail;
  149. }
  150. }
  151. (*path)[j++] = cp[i];
  152. }
  153. if(j == 0) {
  154. /*error("Empty quotes");*/
  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 */