path.c 8.1 KB

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