curl_path.c 6.3 KB

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