Account.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Copyright (c) 2020, Peter Elliott <pelliott@serenityos.org>
  3. * Copyright (c) 2021-2022, Brian Gianforcaro <bgianf@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Base64.h>
  8. #include <AK/Memory.h>
  9. #include <AK/Random.h>
  10. #include <AK/ScopeGuard.h>
  11. #include <LibCore/Account.h>
  12. #include <LibCore/Directory.h>
  13. #include <LibCore/System.h>
  14. #include <LibCore/UmaskScope.h>
  15. #include <errno.h>
  16. #include <grp.h>
  17. #include <pwd.h>
  18. #ifndef AK_OS_BSD_GENERIC
  19. # include <crypt.h>
  20. # include <shadow.h>
  21. #endif
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <sys/stat.h>
  25. #include <unistd.h>
  26. namespace Core {
  27. static String get_salt()
  28. {
  29. char random_data[12];
  30. fill_with_random(random_data, sizeof(random_data));
  31. StringBuilder builder;
  32. builder.append("$5$"sv);
  33. builder.append(encode_base64(ReadonlyBytes(random_data, sizeof(random_data))));
  34. return builder.build();
  35. }
  36. static Vector<gid_t> get_extra_gids(passwd const& pwd)
  37. {
  38. StringView username { pwd.pw_name, strlen(pwd.pw_name) };
  39. Vector<gid_t> extra_gids;
  40. setgrent();
  41. for (auto* group = getgrent(); group; group = getgrent()) {
  42. if (group->gr_gid == pwd.pw_gid)
  43. continue;
  44. for (size_t i = 0; group->gr_mem[i]; ++i) {
  45. if (username == group->gr_mem[i]) {
  46. extra_gids.append(group->gr_gid);
  47. break;
  48. }
  49. }
  50. }
  51. endgrent();
  52. return extra_gids;
  53. }
  54. ErrorOr<Account> Account::from_passwd(passwd const& pwd, spwd const& spwd)
  55. {
  56. Account account(pwd, spwd, get_extra_gids(pwd));
  57. endpwent();
  58. #ifndef AK_OS_BSD_GENERIC
  59. endspent();
  60. #endif
  61. return account;
  62. }
  63. ErrorOr<Account> Account::self([[maybe_unused]] Read options)
  64. {
  65. Vector<gid_t> extra_gids = TRY(Core::System::getgroups());
  66. auto pwd = TRY(Core::System::getpwuid(getuid()));
  67. if (!pwd.has_value())
  68. return Error::from_string_literal("No such user");
  69. spwd spwd = {};
  70. #ifndef AK_OS_BSD_GENERIC
  71. if (options != Read::PasswdOnly) {
  72. auto maybe_spwd = TRY(Core::System::getspnam({ pwd->pw_name, strlen(pwd->pw_name) }));
  73. if (!maybe_spwd.has_value())
  74. return Error::from_string_literal("No shadow entry for user");
  75. spwd = maybe_spwd.release_value();
  76. }
  77. #endif
  78. return Account(*pwd, spwd, extra_gids);
  79. }
  80. ErrorOr<Account> Account::from_name(StringView username, [[maybe_unused]] Read options)
  81. {
  82. auto pwd = TRY(Core::System::getpwnam(username));
  83. if (!pwd.has_value())
  84. return Error::from_string_literal("No such user");
  85. spwd spwd = {};
  86. #ifndef AK_OS_BSD_GENERIC
  87. if (options != Read::PasswdOnly) {
  88. auto maybe_spwd = TRY(Core::System::getspnam({ pwd->pw_name, strlen(pwd->pw_name) }));
  89. if (!maybe_spwd.has_value())
  90. return Error::from_string_literal("No shadow entry for user");
  91. spwd = maybe_spwd.release_value();
  92. }
  93. #endif
  94. return from_passwd(*pwd, spwd);
  95. }
  96. ErrorOr<Account> Account::from_uid(uid_t uid, [[maybe_unused]] Read options)
  97. {
  98. auto pwd = TRY(Core::System::getpwuid(uid));
  99. if (!pwd.has_value())
  100. return Error::from_string_literal("No such user");
  101. spwd spwd = {};
  102. #ifndef AK_OS_BSD_GENERIC
  103. if (options != Read::PasswdOnly) {
  104. auto maybe_spwd = TRY(Core::System::getspnam({ pwd->pw_name, strlen(pwd->pw_name) }));
  105. if (!maybe_spwd.has_value())
  106. return Error::from_string_literal("No shadow entry for user");
  107. spwd = maybe_spwd.release_value();
  108. }
  109. #endif
  110. return from_passwd(*pwd, spwd);
  111. }
  112. bool Account::authenticate(SecretString const& password) const
  113. {
  114. // If there was no shadow entry for this account, authentication always fails.
  115. if (m_password_hash.is_null())
  116. return false;
  117. // An empty passwd field indicates that no password is required to log in.
  118. if (m_password_hash.is_empty())
  119. return true;
  120. // FIXME: Use crypt_r if it can be built in lagom.
  121. char* hash = crypt(password.characters(), m_password_hash.characters());
  122. return hash != nullptr && AK::timing_safe_compare(hash, m_password_hash.characters(), m_password_hash.length());
  123. }
  124. ErrorOr<void> Account::login() const
  125. {
  126. TRY(Core::System::setgroups(m_extra_gids));
  127. TRY(Core::System::setgid(m_gid));
  128. TRY(Core::System::setuid(m_uid));
  129. return {};
  130. }
  131. void Account::set_password(SecretString const& password)
  132. {
  133. m_password_hash = crypt(password.characters(), get_salt().characters());
  134. }
  135. void Account::set_password_enabled(bool enabled)
  136. {
  137. if (enabled && m_password_hash != "" && m_password_hash[0] == '!') {
  138. m_password_hash = m_password_hash.substring(1, m_password_hash.length() - 1);
  139. } else if (!enabled && (m_password_hash == "" || m_password_hash[0] != '!')) {
  140. StringBuilder builder;
  141. builder.append('!');
  142. builder.append(m_password_hash);
  143. m_password_hash = builder.build();
  144. }
  145. }
  146. void Account::delete_password()
  147. {
  148. m_password_hash = "";
  149. }
  150. Account::Account(passwd const& pwd, spwd const& spwd, Vector<gid_t> extra_gids)
  151. : m_username(pwd.pw_name)
  152. , m_password_hash(spwd.sp_pwdp)
  153. , m_uid(pwd.pw_uid)
  154. , m_gid(pwd.pw_gid)
  155. , m_gecos(pwd.pw_gecos)
  156. , m_home_directory(pwd.pw_dir)
  157. , m_shell(pwd.pw_shell)
  158. , m_extra_gids(move(extra_gids))
  159. {
  160. }
  161. ErrorOr<String> Account::generate_passwd_file() const
  162. {
  163. StringBuilder builder;
  164. setpwent();
  165. struct passwd* p;
  166. errno = 0;
  167. while ((p = getpwent())) {
  168. if (p->pw_name == m_username) {
  169. builder.appendff("{}:!:{}:{}:{}:{}:{}\n",
  170. m_username,
  171. m_uid, m_gid,
  172. m_gecos,
  173. m_home_directory,
  174. m_shell);
  175. } else {
  176. builder.appendff("{}:!:{}:{}:{}:{}:{}\n",
  177. p->pw_name, p->pw_uid,
  178. p->pw_gid, p->pw_gecos, p->pw_dir,
  179. p->pw_shell);
  180. }
  181. }
  182. endpwent();
  183. if (errno)
  184. return Error::from_errno(errno);
  185. return builder.to_string();
  186. }
  187. #ifndef AK_OS_BSD_GENERIC
  188. ErrorOr<String> Account::generate_shadow_file() const
  189. {
  190. StringBuilder builder;
  191. setspent();
  192. struct spwd* p;
  193. errno = 0;
  194. while ((p = getspent())) {
  195. if (p->sp_namp == m_username) {
  196. builder.appendff("{}:{}:{}:{}:{}:{}:{}:{}:{}\n",
  197. m_username, m_password_hash,
  198. (p->sp_lstchg == -1) ? "" : String::formatted("{}", p->sp_lstchg),
  199. (p->sp_min == -1) ? "" : String::formatted("{}", p->sp_min),
  200. (p->sp_max == -1) ? "" : String::formatted("{}", p->sp_max),
  201. (p->sp_warn == -1) ? "" : String::formatted("{}", p->sp_warn),
  202. (p->sp_inact == -1) ? "" : String::formatted("{}", p->sp_inact),
  203. (p->sp_expire == -1) ? "" : String::formatted("{}", p->sp_expire),
  204. (p->sp_flag == 0) ? "" : String::formatted("{}", p->sp_flag));
  205. } else {
  206. builder.appendff("{}:{}:{}:{}:{}:{}:{}:{}:{}\n",
  207. p->sp_namp, p->sp_pwdp,
  208. (p->sp_lstchg == -1) ? "" : String::formatted("{}", p->sp_lstchg),
  209. (p->sp_min == -1) ? "" : String::formatted("{}", p->sp_min),
  210. (p->sp_max == -1) ? "" : String::formatted("{}", p->sp_max),
  211. (p->sp_warn == -1) ? "" : String::formatted("{}", p->sp_warn),
  212. (p->sp_inact == -1) ? "" : String::formatted("{}", p->sp_inact),
  213. (p->sp_expire == -1) ? "" : String::formatted("{}", p->sp_expire),
  214. (p->sp_flag == 0) ? "" : String::formatted("{}", p->sp_flag));
  215. }
  216. }
  217. endspent();
  218. if (errno)
  219. return Error::from_errno(errno);
  220. return builder.to_string();
  221. }
  222. #endif
  223. ErrorOr<void> Account::sync()
  224. {
  225. Core::UmaskScope umask_scope(0777);
  226. auto new_passwd_file_content = TRY(generate_passwd_file());
  227. #ifndef AK_OS_BSD_GENERIC
  228. auto new_shadow_file_content = TRY(generate_shadow_file());
  229. #endif
  230. // FIXME: mkstemp taking Span<char> makes this code entirely un-AKable.
  231. // Make this code less char-pointery.
  232. char new_passwd_name[] = "/etc/passwd.XXXXXX";
  233. size_t new_passwd_name_length = strlen(new_passwd_name);
  234. #ifndef AK_OS_BSD_GENERIC
  235. char new_shadow_name[] = "/etc/shadow.XXXXXX";
  236. size_t new_shadow_name_length = strlen(new_shadow_name);
  237. #endif
  238. {
  239. auto new_passwd_fd = TRY(Core::System::mkstemp({ new_passwd_name, new_passwd_name_length }));
  240. ScopeGuard new_passwd_fd_guard = [new_passwd_fd] { close(new_passwd_fd); };
  241. TRY(Core::System::fchmod(new_passwd_fd, 0644));
  242. #ifndef AK_OS_BSD_GENERIC
  243. auto new_shadow_fd = TRY(Core::System::mkstemp({ new_shadow_name, new_shadow_name_length }));
  244. ScopeGuard new_shadow_fd_guard = [new_shadow_fd] { close(new_shadow_fd); };
  245. TRY(Core::System::fchmod(new_shadow_fd, 0600));
  246. #endif
  247. auto nwritten = TRY(Core::System::write(new_passwd_fd, new_passwd_file_content.bytes()));
  248. VERIFY(static_cast<size_t>(nwritten) == new_passwd_file_content.length());
  249. #ifndef AK_OS_BSD_GENERIC
  250. nwritten = TRY(Core::System::write(new_shadow_fd, new_shadow_file_content.bytes()));
  251. VERIFY(static_cast<size_t>(nwritten) == new_shadow_file_content.length());
  252. #endif
  253. }
  254. TRY(Core::System::rename({ new_passwd_name, new_passwd_name_length }, "/etc/passwd"sv));
  255. #ifndef AK_OS_BSD_GENERIC
  256. TRY(Core::System::rename({ new_shadow_name, new_shadow_name_length }, "/etc/shadow"sv));
  257. #endif
  258. return {};
  259. // FIXME: Sync extra groups.
  260. }
  261. }