Account.cpp 9.4 KB

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