curl_path.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. (working_path_len > 2) && !memcmp(working_path, "/~/", 3)) {
  59. size_t len;
  60. const char *p;
  61. int copyfrom = 3;
  62. if(Curl_dyn_add(&npath, homedir)) {
  63. free(working_path);
  64. return CURLE_OUT_OF_MEMORY;
  65. }
  66. /* Copy a separating '/' if homedir does not end with one */
  67. len = Curl_dyn_len(&npath);
  68. p = Curl_dyn_ptr(&npath);
  69. if(len && (p[len-1] != '/'))
  70. copyfrom = 2;
  71. if(Curl_dyn_addn(&npath,
  72. &working_path[copyfrom], working_path_len - copyfrom)) {
  73. free(working_path);
  74. return CURLE_OUT_OF_MEMORY;
  75. }
  76. }
  77. if(Curl_dyn_len(&npath)) {
  78. free(working_path);
  79. /* store the pointer for the caller to receive */
  80. *path = Curl_dyn_ptr(&npath);
  81. }
  82. else
  83. *path = working_path;
  84. return CURLE_OK;
  85. }
  86. /* The get_pathname() function is being borrowed from OpenSSH sftp.c
  87. version 4.6p1. */
  88. /*
  89. * Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
  90. *
  91. * Permission to use, copy, modify, and distribute this software for any
  92. * purpose with or without fee is hereby granted, provided that the above
  93. * copyright notice and this permission notice appear in all copies.
  94. *
  95. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  96. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  97. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  98. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  99. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  100. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  101. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  102. */
  103. CURLcode Curl_get_pathname(const char **cpp, char **path, char *homedir)
  104. {
  105. const char *cp = *cpp, *end;
  106. char quot;
  107. unsigned int i, j;
  108. size_t fullPathLength, pathLength;
  109. bool relativePath = false;
  110. static const char WHITESPACE[] = " \t\r\n";
  111. DEBUGASSERT(homedir);
  112. if(!*cp || !homedir) {
  113. *cpp = NULL;
  114. *path = NULL;
  115. return CURLE_QUOTE_ERROR;
  116. }
  117. /* Ignore leading whitespace */
  118. cp += strspn(cp, WHITESPACE);
  119. /* Allocate enough space for home directory and filename + separator */
  120. fullPathLength = strlen(cp) + strlen(homedir) + 2;
  121. *path = malloc(fullPathLength);
  122. if(!*path)
  123. return CURLE_OUT_OF_MEMORY;
  124. /* Check for quoted filenames */
  125. if(*cp == '\"' || *cp == '\'') {
  126. quot = *cp++;
  127. /* Search for terminating quote, unescape some chars */
  128. for(i = j = 0; i <= strlen(cp); i++) {
  129. if(cp[i] == quot) { /* Found quote */
  130. i++;
  131. (*path)[j] = '\0';
  132. break;
  133. }
  134. if(cp[i] == '\0') { /* End of string */
  135. goto fail;
  136. }
  137. if(cp[i] == '\\') { /* Escaped characters */
  138. i++;
  139. if(cp[i] != '\'' && cp[i] != '\"' &&
  140. cp[i] != '\\') {
  141. goto fail;
  142. }
  143. }
  144. (*path)[j++] = cp[i];
  145. }
  146. if(j == 0) {
  147. goto fail;
  148. }
  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. pathLength = 0;
  159. relativePath = (cp[0] == '/' && cp[1] == '~' && cp[2] == '/');
  160. /* Handling for relative path - prepend home directory */
  161. if(relativePath) {
  162. strcpy(*path, homedir);
  163. pathLength = strlen(homedir);
  164. (*path)[pathLength++] = '/';
  165. (*path)[pathLength] = '\0';
  166. cp += 3;
  167. }
  168. /* Copy path name up until first "whitespace" */
  169. memcpy(&(*path)[pathLength], cp, (int)(end - cp));
  170. pathLength += (int)(end - cp);
  171. (*path)[pathLength] = '\0';
  172. }
  173. return CURLE_OK;
  174. fail:
  175. Curl_safefree(*path);
  176. return CURLE_QUOTE_ERROR;
  177. }
  178. #endif /* if SSH is used */