userdb.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* Copyright (c) 2003-2004, 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/userdb.h"
  6. #ifndef _WIN32
  7. #include "lib/malloc/util_malloc.h"
  8. #include "lib/log/torlog.h"
  9. #include "lib/log/util_bug.h"
  10. #include <pwd.h>
  11. #include <stddef.h>
  12. #include <string.h>
  13. /** Cached struct from the last getpwname() call we did successfully. */
  14. static struct passwd *passwd_cached = NULL;
  15. /** Helper: copy a struct passwd object.
  16. *
  17. * We only copy the fields pw_uid, pw_gid, pw_name, pw_dir. Tor doesn't use
  18. * any others, and I don't want to run into incompatibilities.
  19. */
  20. static struct passwd *
  21. tor_passwd_dup(const struct passwd *pw)
  22. {
  23. struct passwd *new_pw = tor_malloc_zero(sizeof(struct passwd));
  24. if (pw->pw_name)
  25. new_pw->pw_name = tor_strdup(pw->pw_name);
  26. if (pw->pw_dir)
  27. new_pw->pw_dir = tor_strdup(pw->pw_dir);
  28. new_pw->pw_uid = pw->pw_uid;
  29. new_pw->pw_gid = pw->pw_gid;
  30. return new_pw;
  31. }
  32. #define tor_passwd_free(pw) \
  33. FREE_AND_NULL(struct passwd, tor_passwd_free_, (pw))
  34. /** Helper: free one of our cached 'struct passwd' values. */
  35. static void
  36. tor_passwd_free_(struct passwd *pw)
  37. {
  38. if (!pw)
  39. return;
  40. tor_free(pw->pw_name);
  41. tor_free(pw->pw_dir);
  42. tor_free(pw);
  43. }
  44. /** Wrapper around getpwnam() that caches result. Used so that we don't need
  45. * to give the sandbox access to /etc/passwd.
  46. *
  47. * The following fields alone will definitely be copied in the output: pw_uid,
  48. * pw_gid, pw_name, pw_dir. Other fields are not present in cached values.
  49. *
  50. * When called with a NULL argument, this function clears storage associated
  51. * with static variables it uses.
  52. **/
  53. const struct passwd *
  54. tor_getpwnam(const char *username)
  55. {
  56. struct passwd *pw;
  57. if (username == NULL) {
  58. tor_passwd_free(passwd_cached);
  59. passwd_cached = NULL;
  60. return NULL;
  61. }
  62. if ((pw = getpwnam(username))) {
  63. tor_passwd_free(passwd_cached);
  64. passwd_cached = tor_passwd_dup(pw);
  65. log_info(LD_GENERAL, "Caching new entry %s for %s",
  66. passwd_cached->pw_name, username);
  67. return pw;
  68. }
  69. /* Lookup failed */
  70. if (! passwd_cached || ! passwd_cached->pw_name)
  71. return NULL;
  72. if (! strcmp(username, passwd_cached->pw_name))
  73. return passwd_cached; // LCOV_EXCL_LINE - would need to make getpwnam flaky
  74. return NULL;
  75. }
  76. /** Wrapper around getpwnam() that can use cached result from
  77. * tor_getpwnam(). Used so that we don't need to give the sandbox access to
  78. * /etc/passwd.
  79. *
  80. * The following fields alone will definitely be copied in the output: pw_uid,
  81. * pw_gid, pw_name, pw_dir. Other fields are not present in cached values.
  82. */
  83. const struct passwd *
  84. tor_getpwuid(uid_t uid)
  85. {
  86. struct passwd *pw;
  87. if ((pw = getpwuid(uid))) {
  88. return pw;
  89. }
  90. /* Lookup failed */
  91. if (! passwd_cached)
  92. return NULL;
  93. if (uid == passwd_cached->pw_uid)
  94. return passwd_cached; // LCOV_EXCL_LINE - would need to make getpwnam flaky
  95. return NULL;
  96. }
  97. /** Allocate and return a string containing the home directory for the
  98. * user <b>username</b>. Only works on posix-like systems. */
  99. char *
  100. get_user_homedir(const char *username)
  101. {
  102. const struct passwd *pw;
  103. tor_assert(username);
  104. if (!(pw = tor_getpwnam(username))) {
  105. log_err(LD_CONFIG,"User \"%s\" not found.", username);
  106. return NULL;
  107. }
  108. return tor_strdup(pw->pw_dir);
  109. }
  110. #endif /* !defined(_WIN32) */