path.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /* Copyright (c) 2003, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "lib/fs/path.h"
  6. #include "lib/malloc/util_malloc.h"
  7. #include "lib/log/torlog.h"
  8. #include "lib/log/util_bug.h"
  9. #include "lib/string/printf.h"
  10. #include "lib/string/util_string.h"
  11. #include "lib/string/compat_ctype.h"
  12. #include "lib/fs/userdb.h"
  13. #ifdef HAVE_UNISTD_H
  14. #include <unistd.h>
  15. #endif
  16. #include <errno.h>
  17. #include <string.h>
  18. /** Removes enclosing quotes from <b>path</b> and unescapes quotes between the
  19. * enclosing quotes. Backslashes are not unescaped. Return the unquoted
  20. * <b>path</b> on success or 0 if <b>path</b> is not quoted correctly. */
  21. char *
  22. get_unquoted_path(const char *path)
  23. {
  24. size_t len = strlen(path);
  25. if (len == 0) {
  26. return tor_strdup("");
  27. }
  28. int has_start_quote = (path[0] == '\"');
  29. int has_end_quote = (len > 0 && path[len-1] == '\"');
  30. if (has_start_quote != has_end_quote || (len == 1 && has_start_quote)) {
  31. return NULL;
  32. }
  33. char *unquoted_path = tor_malloc(len - has_start_quote - has_end_quote + 1);
  34. char *s = unquoted_path;
  35. size_t i;
  36. for (i = has_start_quote; i < len - has_end_quote; i++) {
  37. if (path[i] == '\"' && (i > 0 && path[i-1] == '\\')) {
  38. *(s-1) = path[i];
  39. } else if (path[i] != '\"') {
  40. *s++ = path[i];
  41. } else { /* unescaped quote */
  42. tor_free(unquoted_path);
  43. return NULL;
  44. }
  45. }
  46. *s = '\0';
  47. return unquoted_path;
  48. }
  49. /** Expand any homedir prefix on <b>filename</b>; return a newly allocated
  50. * string. */
  51. char *
  52. expand_filename(const char *filename)
  53. {
  54. tor_assert(filename);
  55. #ifdef _WIN32
  56. /* Might consider using GetFullPathName() as described here:
  57. * http://etutorials.org/Programming/secure+programming/
  58. * Chapter+3.+Input+Validation/3.7+Validating+Filenames+and+Paths/
  59. */
  60. return tor_strdup(filename);
  61. #else /* !(defined(_WIN32)) */
  62. if (*filename == '~') {
  63. char *home, *result=NULL;
  64. const char *rest;
  65. if (filename[1] == '/' || filename[1] == '\0') {
  66. home = getenv("HOME");
  67. if (!home) {
  68. log_warn(LD_CONFIG, "Couldn't find $HOME environment variable while "
  69. "expanding \"%s\"; defaulting to \"\".", filename);
  70. home = tor_strdup("");
  71. } else {
  72. home = tor_strdup(home);
  73. }
  74. rest = strlen(filename)>=2?(filename+2):"";
  75. } else {
  76. #ifdef HAVE_PWD_H
  77. char *username, *slash;
  78. slash = strchr(filename, '/');
  79. if (slash)
  80. username = tor_strndup(filename+1,slash-filename-1);
  81. else
  82. username = tor_strdup(filename+1);
  83. if (!(home = get_user_homedir(username))) {
  84. log_warn(LD_CONFIG,"Couldn't get homedir for \"%s\"",username);
  85. tor_free(username);
  86. return NULL;
  87. }
  88. tor_free(username);
  89. rest = slash ? (slash+1) : "";
  90. #else /* !(defined(HAVE_PWD_H)) */
  91. log_warn(LD_CONFIG, "Couldn't expand homedir on system without pwd.h");
  92. return tor_strdup(filename);
  93. #endif /* defined(HAVE_PWD_H) */
  94. }
  95. tor_assert(home);
  96. /* Remove trailing slash. */
  97. if (strlen(home)>1 && !strcmpend(home,PATH_SEPARATOR)) {
  98. home[strlen(home)-1] = '\0';
  99. }
  100. tor_asprintf(&result,"%s"PATH_SEPARATOR"%s",home,rest);
  101. tor_free(home);
  102. return result;
  103. } else {
  104. return tor_strdup(filename);
  105. }
  106. #endif /* defined(_WIN32) */
  107. }
  108. /** Return true iff <b>filename</b> is a relative path. */
  109. int
  110. path_is_relative(const char *filename)
  111. {
  112. if (filename && filename[0] == '/')
  113. return 0;
  114. #ifdef _WIN32
  115. else if (filename && filename[0] == '\\')
  116. return 0;
  117. else if (filename && strlen(filename)>3 && TOR_ISALPHA(filename[0]) &&
  118. filename[1] == ':' && filename[2] == '\\')
  119. return 0;
  120. #endif /* defined(_WIN32) */
  121. else
  122. return 1;
  123. }
  124. /** Clean up <b>name</b> so that we can use it in a call to "stat". On Unix,
  125. * we do nothing. On Windows, we remove a trailing slash, unless the path is
  126. * the root of a disk. */
  127. void
  128. clean_fname_for_stat(char *name)
  129. {
  130. #ifdef _WIN32
  131. size_t len = strlen(name);
  132. if (!len)
  133. return;
  134. if (name[len-1]=='\\' || name[len-1]=='/') {
  135. if (len == 1 || (len==3 && name[1]==':'))
  136. return;
  137. name[len-1]='\0';
  138. }
  139. #else /* !(defined(_WIN32)) */
  140. (void)name;
  141. #endif /* defined(_WIN32) */
  142. }
  143. /** Modify <b>fname</b> to contain the name of its parent directory. Doesn't
  144. * actually examine the filesystem; does a purely syntactic modification.
  145. *
  146. * The parent of the root director is considered to be iteself.
  147. *
  148. * Path separators are the forward slash (/) everywhere and additionally
  149. * the backslash (\) on Win32.
  150. *
  151. * Cuts off any number of trailing path separators but otherwise ignores
  152. * them for purposes of finding the parent directory.
  153. *
  154. * Returns 0 if a parent directory was successfully found, -1 otherwise (fname
  155. * did not have any path separators or only had them at the end).
  156. * */
  157. int
  158. get_parent_directory(char *fname)
  159. {
  160. char *cp;
  161. int at_end = 1;
  162. tor_assert(fname);
  163. #ifdef _WIN32
  164. /* If we start with, say, c:, then don't consider that the start of the path
  165. */
  166. if (fname[0] && fname[1] == ':') {
  167. fname += 2;
  168. }
  169. #endif /* defined(_WIN32) */
  170. /* Now we want to remove all path-separators at the end of the string,
  171. * and to remove the end of the string starting with the path separator
  172. * before the last non-path-separator. In perl, this would be
  173. * s#[/]*$##; s#/[^/]*$##;
  174. * on a unixy platform.
  175. */
  176. cp = fname + strlen(fname);
  177. at_end = 1;
  178. while (--cp >= fname) {
  179. int is_sep = (*cp == '/'
  180. #ifdef _WIN32
  181. || *cp == '\\'
  182. #endif
  183. );
  184. if (is_sep) {
  185. if (cp == fname) {
  186. /* This is the first separator in the file name; don't remove it! */
  187. cp[1] = '\0';
  188. return 0;
  189. }
  190. *cp = '\0';
  191. if (! at_end)
  192. return 0;
  193. } else {
  194. at_end = 0;
  195. }
  196. }
  197. return -1;
  198. }
  199. #ifndef _WIN32
  200. /** Return a newly allocated string containing the output of getcwd(). Return
  201. * NULL on failure. (We can't just use getcwd() into a PATH_MAX buffer, since
  202. * Hurd hasn't got a PATH_MAX.)
  203. */
  204. static char *
  205. alloc_getcwd(void)
  206. {
  207. #ifdef HAVE_GET_CURRENT_DIR_NAME
  208. /* Glibc makes this nice and simple for us. */
  209. char *cwd = get_current_dir_name();
  210. char *result = NULL;
  211. if (cwd) {
  212. /* We make a copy here, in case tor_malloc() is not malloc(). */
  213. result = tor_strdup(cwd);
  214. raw_free(cwd); // alias for free to avoid tripping check-spaces.
  215. }
  216. return result;
  217. #else /* !(defined(HAVE_GET_CURRENT_DIR_NAME)) */
  218. size_t size = 1024;
  219. char *buf = NULL;
  220. char *ptr = NULL;
  221. while (ptr == NULL) {
  222. buf = tor_realloc(buf, size);
  223. ptr = getcwd(buf, size);
  224. if (ptr == NULL && errno != ERANGE) {
  225. tor_free(buf);
  226. return NULL;
  227. }
  228. size *= 2;
  229. }
  230. return buf;
  231. #endif /* defined(HAVE_GET_CURRENT_DIR_NAME) */
  232. }
  233. #endif /* !defined(_WIN32) */
  234. /** Expand possibly relative path <b>fname</b> to an absolute path.
  235. * Return a newly allocated string, possibly equal to <b>fname</b>. */
  236. char *
  237. make_path_absolute(char *fname)
  238. {
  239. #ifdef _WIN32
  240. char *absfname_malloced = _fullpath(NULL, fname, 1);
  241. /* We don't want to assume that tor_free can free a string allocated
  242. * with malloc. On failure, return fname (it's better than nothing). */
  243. char *absfname = tor_strdup(absfname_malloced ? absfname_malloced : fname);
  244. if (absfname_malloced) raw_free(absfname_malloced);
  245. return absfname;
  246. #else /* !(defined(_WIN32)) */
  247. char *absfname = NULL, *path = NULL;
  248. tor_assert(fname);
  249. if (fname[0] == '/') {
  250. absfname = tor_strdup(fname);
  251. } else {
  252. path = alloc_getcwd();
  253. if (path) {
  254. tor_asprintf(&absfname, "%s/%s", path, fname);
  255. tor_free(path);
  256. } else {
  257. /* LCOV_EXCL_START Can't make getcwd fail. */
  258. /* If getcwd failed, the best we can do here is keep using the
  259. * relative path. (Perhaps / isn't readable by this UID/GID.) */
  260. log_warn(LD_GENERAL, "Unable to find current working directory: %s",
  261. strerror(errno));
  262. absfname = tor_strdup(fname);
  263. /* LCOV_EXCL_STOP */
  264. }
  265. }
  266. return absfname;
  267. #endif /* defined(_WIN32) */
  268. }