curl_path.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, 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 connectdata *conn,
  31. char *homedir, /* when SFTP is used */
  32. char **path) /* returns the allocated
  33. real path to work with */
  34. {
  35. struct Curl_easy *data = conn->data;
  36. char *real_path = NULL;
  37. char *working_path;
  38. size_t working_path_len;
  39. CURLcode result =
  40. Curl_urldecode(data, data->state.up.path, 0, &working_path,
  41. &working_path_len, REJECT_ZERO);
  42. if(result)
  43. return result;
  44. /* Check for /~/, indicating relative to the user's home directory */
  45. if(conn->handler->protocol & CURLPROTO_SCP) {
  46. real_path = malloc(working_path_len + 1);
  47. if(real_path == NULL) {
  48. free(working_path);
  49. return CURLE_OUT_OF_MEMORY;
  50. }
  51. if((working_path_len > 3) && (!memcmp(working_path, "/~/", 3)))
  52. /* It is referenced to the home directory, so strip the leading '/~/' */
  53. memcpy(real_path, working_path + 3, working_path_len - 2);
  54. else
  55. memcpy(real_path, working_path, 1 + working_path_len);
  56. }
  57. else if(conn->handler->protocol & CURLPROTO_SFTP) {
  58. if((working_path_len > 1) && (working_path[1] == '~')) {
  59. size_t homelen = strlen(homedir);
  60. real_path = malloc(homelen + working_path_len + 1);
  61. if(real_path == NULL) {
  62. free(working_path);
  63. return CURLE_OUT_OF_MEMORY;
  64. }
  65. /* It is referenced to the home directory, so strip the
  66. leading '/' */
  67. memcpy(real_path, homedir, homelen);
  68. real_path[homelen] = '/';
  69. real_path[homelen + 1] = '\0';
  70. if(working_path_len > 3) {
  71. memcpy(real_path + homelen + 1, working_path + 3,
  72. 1 + working_path_len -3);
  73. }
  74. }
  75. else {
  76. real_path = malloc(working_path_len + 1);
  77. if(real_path == NULL) {
  78. free(working_path);
  79. return CURLE_OUT_OF_MEMORY;
  80. }
  81. memcpy(real_path, working_path, 1 + working_path_len);
  82. }
  83. }
  84. free(working_path);
  85. /* store the pointer for the caller to receive */
  86. *path = real_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. if(!*cp) {
  115. *cpp = NULL;
  116. *path = NULL;
  117. return CURLE_QUOTE_ERROR;
  118. }
  119. /* Ignore leading whitespace */
  120. cp += strspn(cp, WHITESPACE);
  121. /* Allocate enough space for home directory and filename + separator */
  122. fullPathLength = strlen(cp) + strlen(homedir) + 2;
  123. *path = malloc(fullPathLength);
  124. if(*path == NULL)
  125. return CURLE_OUT_OF_MEMORY;
  126. /* Check for quoted filenames */
  127. if(*cp == '\"' || *cp == '\'') {
  128. quot = *cp++;
  129. /* Search for terminating quote, unescape some chars */
  130. for(i = j = 0; i <= strlen(cp); i++) {
  131. if(cp[i] == quot) { /* Found quote */
  132. i++;
  133. (*path)[j] = '\0';
  134. break;
  135. }
  136. if(cp[i] == '\0') { /* End of string */
  137. /*error("Unterminated quote");*/
  138. goto fail;
  139. }
  140. if(cp[i] == '\\') { /* Escaped characters */
  141. i++;
  142. if(cp[i] != '\'' && cp[i] != '\"' &&
  143. cp[i] != '\\') {
  144. /*error("Bad escaped character '\\%c'",
  145. cp[i]);*/
  146. goto fail;
  147. }
  148. }
  149. (*path)[j++] = cp[i];
  150. }
  151. if(j == 0) {
  152. /*error("Empty quotes");*/
  153. goto fail;
  154. }
  155. *cpp = cp + i + strspn(cp + i, WHITESPACE);
  156. }
  157. else {
  158. /* Read to end of filename - either to whitespace or terminator */
  159. end = strpbrk(cp, WHITESPACE);
  160. if(end == NULL)
  161. end = strchr(cp, '\0');
  162. /* return pointer to second parameter if it exists */
  163. *cpp = end + strspn(end, WHITESPACE);
  164. pathLength = 0;
  165. relativePath = (cp[0] == '/' && cp[1] == '~' && cp[2] == '/');
  166. /* Handling for relative path - prepend home directory */
  167. if(relativePath) {
  168. strcpy(*path, homedir);
  169. pathLength = strlen(homedir);
  170. (*path)[pathLength++] = '/';
  171. (*path)[pathLength] = '\0';
  172. cp += 3;
  173. }
  174. /* Copy path name up until first "whitespace" */
  175. memcpy(&(*path)[pathLength], cp, (int)(end - cp));
  176. pathLength += (int)(end - cp);
  177. (*path)[pathLength] = '\0';
  178. }
  179. return CURLE_OK;
  180. fail:
  181. Curl_safefree(*path);
  182. return CURLE_QUOTE_ERROR;
  183. }
  184. #endif /* if SSH is used */