curl_path.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. #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 get_pathname() function is being borrowed from OpenSSH sftp.c
  90. version 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. CURLcode Curl_get_pathname(const char **cpp, char **path, char *homedir)
  107. {
  108. const char *cp = *cpp, *end;
  109. char quot;
  110. unsigned int i, j;
  111. size_t fullPathLength, pathLength;
  112. bool relativePath = false;
  113. static const char WHITESPACE[] = " \t\r\n";
  114. DEBUGASSERT(homedir);
  115. if(!*cp || !homedir) {
  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. goto fail;
  139. }
  140. if(cp[i] == '\\') { /* Escaped characters */
  141. i++;
  142. if(cp[i] != '\'' && cp[i] != '\"' &&
  143. cp[i] != '\\') {
  144. goto fail;
  145. }
  146. }
  147. (*path)[j++] = cp[i];
  148. }
  149. if(j == 0) {
  150. goto fail;
  151. }
  152. *cpp = cp + i + strspn(cp + i, WHITESPACE);
  153. }
  154. else {
  155. /* Read to end of filename - either to whitespace or terminator */
  156. end = strpbrk(cp, WHITESPACE);
  157. if(!end)
  158. end = strchr(cp, '\0');
  159. /* return pointer to second parameter if it exists */
  160. *cpp = end + strspn(end, WHITESPACE);
  161. pathLength = 0;
  162. relativePath = (cp[0] == '/' && cp[1] == '~' && cp[2] == '/');
  163. /* Handling for relative path - prepend home directory */
  164. if(relativePath) {
  165. strcpy(*path, homedir);
  166. pathLength = strlen(homedir);
  167. (*path)[pathLength++] = '/';
  168. (*path)[pathLength] = '\0';
  169. cp += 3;
  170. }
  171. /* Copy path name up until first "whitespace" */
  172. memcpy(&(*path)[pathLength], cp, (int)(end - cp));
  173. pathLength += (int)(end - cp);
  174. (*path)[pathLength] = '\0';
  175. }
  176. return CURLE_OK;
  177. fail:
  178. Curl_safefree(*path);
  179. return CURLE_QUOTE_ERROR;
  180. }
  181. #endif /* if SSH is used */