curl_path.c 6.1 KB

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