curl_path.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. #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. if(!*cp) {
  116. *cpp = NULL;
  117. *path = NULL;
  118. return CURLE_QUOTE_ERROR;
  119. }
  120. /* Ignore leading whitespace */
  121. cp += strspn(cp, WHITESPACE);
  122. /* Allocate enough space for home directory and filename + separator */
  123. fullPathLength = strlen(cp) + strlen(homedir) + 2;
  124. *path = malloc(fullPathLength);
  125. if(!*path)
  126. return CURLE_OUT_OF_MEMORY;
  127. /* Check for quoted filenames */
  128. if(*cp == '\"' || *cp == '\'') {
  129. quot = *cp++;
  130. /* Search for terminating quote, unescape some chars */
  131. for(i = j = 0; i <= strlen(cp); i++) {
  132. if(cp[i] == quot) { /* Found quote */
  133. i++;
  134. (*path)[j] = '\0';
  135. break;
  136. }
  137. if(cp[i] == '\0') { /* End of string */
  138. /*error("Unterminated quote");*/
  139. goto fail;
  140. }
  141. if(cp[i] == '\\') { /* Escaped characters */
  142. i++;
  143. if(cp[i] != '\'' && cp[i] != '\"' &&
  144. cp[i] != '\\') {
  145. /*error("Bad escaped character '\\%c'",
  146. cp[i]);*/
  147. goto fail;
  148. }
  149. }
  150. (*path)[j++] = cp[i];
  151. }
  152. if(j == 0) {
  153. /*error("Empty quotes");*/
  154. goto fail;
  155. }
  156. *cpp = cp + i + strspn(cp + i, WHITESPACE);
  157. }
  158. else {
  159. /* Read to end of filename - either to whitespace or terminator */
  160. end = strpbrk(cp, WHITESPACE);
  161. if(!end)
  162. end = strchr(cp, '\0');
  163. /* return pointer to second parameter if it exists */
  164. *cpp = end + strspn(end, WHITESPACE);
  165. pathLength = 0;
  166. relativePath = (cp[0] == '/' && cp[1] == '~' && cp[2] == '/');
  167. /* Handling for relative path - prepend home directory */
  168. if(relativePath) {
  169. strcpy(*path, homedir);
  170. pathLength = strlen(homedir);
  171. (*path)[pathLength++] = '/';
  172. (*path)[pathLength] = '\0';
  173. cp += 3;
  174. }
  175. /* Copy path name up until first "whitespace" */
  176. memcpy(&(*path)[pathLength], cp, (int)(end - cp));
  177. pathLength += (int)(end - cp);
  178. (*path)[pathLength] = '\0';
  179. }
  180. return CURLE_OK;
  181. fail:
  182. Curl_safefree(*path);
  183. return CURLE_QUOTE_ERROR;
  184. }
  185. #endif /* if SSH is used */