Account.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * Copyright (c) 2020, Peter Elliott <pelliott@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Base64.h>
  7. #include <AK/Random.h>
  8. #include <AK/ScopeGuard.h>
  9. #include <LibCore/Account.h>
  10. #include <LibCore/System.h>
  11. #include <LibCore/UmaskScope.h>
  12. #include <errno.h>
  13. #include <grp.h>
  14. #include <pwd.h>
  15. #ifndef AK_OS_BSD_GENERIC
  16. # include <crypt.h>
  17. # include <shadow.h>
  18. #endif
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <sys/stat.h>
  22. #include <unistd.h>
  23. namespace Core {
  24. static String get_salt()
  25. {
  26. char random_data[12];
  27. fill_with_random(random_data, sizeof(random_data));
  28. StringBuilder builder;
  29. builder.append("$5$");
  30. builder.append(encode_base64(ReadonlyBytes(random_data, sizeof(random_data))));
  31. return builder.build();
  32. }
  33. static Vector<gid_t> get_extra_gids(const passwd& pwd)
  34. {
  35. StringView username { pwd.pw_name };
  36. Vector<gid_t> extra_gids;
  37. setgrent();
  38. for (auto* group = getgrent(); group; group = getgrent()) {
  39. if (group->gr_gid == pwd.pw_gid)
  40. continue;
  41. for (size_t i = 0; group->gr_mem[i]; ++i) {
  42. if (username == group->gr_mem[i]) {
  43. extra_gids.append(group->gr_gid);
  44. break;
  45. }
  46. }
  47. }
  48. endgrent();
  49. return extra_gids;
  50. }
  51. ErrorOr<Account> Account::from_passwd(const passwd& pwd, const spwd& spwd)
  52. {
  53. Account account(pwd, spwd, get_extra_gids(pwd));
  54. endpwent();
  55. #ifndef AK_OS_BSD_GENERIC
  56. endspent();
  57. #endif
  58. return account;
  59. }
  60. ErrorOr<Account> Account::self(Read options)
  61. {
  62. Vector<gid_t> extra_gids = TRY(Core::System::getgroups());
  63. auto pwd = TRY(Core::System::getpwuid(getuid()));
  64. if (!pwd.has_value())
  65. return Error::from_string_literal("No such user"sv);
  66. spwd spwd = {};
  67. #ifndef AK_OS_BSD_GENERIC
  68. if (options != Read::PasswdOnly) {
  69. auto maybe_spwd = TRY(Core::System::getspnam(pwd->pw_name));
  70. if (!maybe_spwd.has_value())
  71. return Error::from_string_literal("No shadow entry for user"sv);
  72. spwd = maybe_spwd.release_value();
  73. }
  74. #endif
  75. return Account(*pwd, spwd, extra_gids);
  76. }
  77. ErrorOr<Account> Account::from_name(const char* username, Read options)
  78. {
  79. auto pwd = TRY(Core::System::getpwnam(username));
  80. if (!pwd.has_value())
  81. return Error::from_string_literal("No such user"sv);
  82. spwd spwd = {};
  83. #ifndef AK_OS_BSD_GENERIC
  84. if (options != Read::PasswdOnly) {
  85. auto maybe_spwd = TRY(Core::System::getspnam(pwd->pw_name));
  86. if (!maybe_spwd.has_value())
  87. return Error::from_string_literal("No shadow entry for user"sv);
  88. spwd = maybe_spwd.release_value();
  89. }
  90. #endif
  91. return from_passwd(*pwd, spwd);
  92. }
  93. ErrorOr<Account> Account::from_uid(uid_t uid, Read options)
  94. {
  95. auto pwd = TRY(Core::System::getpwuid(uid));
  96. if (!pwd.has_value())
  97. return Error::from_string_literal("No such user"sv);
  98. spwd spwd = {};
  99. #ifndef AK_OS_BSD_GENERIC
  100. if (options != Read::PasswdOnly) {
  101. auto maybe_spwd = TRY(Core::System::getspnam(pwd->pw_name));
  102. if (!maybe_spwd.has_value())
  103. return Error::from_string_literal("No shadow entry for user"sv);
  104. spwd = maybe_spwd.release_value();
  105. }
  106. #endif
  107. return from_passwd(*pwd, spwd);
  108. }
  109. bool Account::authenticate(SecretString const& password) const
  110. {
  111. // If there was no shadow entry for this account, authentication always fails.
  112. if (m_password_hash.is_null())
  113. return false;
  114. // An empty passwd field indicates that no password is required to log in.
  115. if (m_password_hash.is_empty())
  116. return true;
  117. // FIXME: Use crypt_r if it can be built in lagom.
  118. char* hash = crypt(password.characters(), m_password_hash.characters());
  119. return hash != nullptr && strcmp(hash, m_password_hash.characters()) == 0;
  120. }
  121. bool Account::login() const
  122. {
  123. if (setgroups(m_extra_gids.size(), m_extra_gids.data()) < 0)
  124. return false;
  125. if (setgid(m_gid) < 0)
  126. return false;
  127. if (setuid(m_uid) < 0)
  128. return false;
  129. return true;
  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(const passwd& pwd, const spwd& 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. char new_passwd_name[] = "/etc/passwd.XXXXXX";
  231. #ifndef AK_OS_BSD_GENERIC
  232. char new_shadow_name[] = "/etc/shadow.XXXXXX";
  233. #endif
  234. {
  235. auto new_passwd_fd = TRY(Core::System::mkstemp(new_passwd_name));
  236. ScopeGuard new_passwd_fd_guard = [new_passwd_fd] { close(new_passwd_fd); };
  237. TRY(Core::System::fchmod(new_passwd_fd, 0644));
  238. #ifndef AK_OS_BSD_GENERIC
  239. auto new_shadow_fd = TRY(Core::System::mkstemp(new_shadow_name));
  240. ScopeGuard new_shadow_fd_guard = [new_shadow_fd] { close(new_shadow_fd); };
  241. TRY(Core::System::fchmod(new_shadow_fd, 0600));
  242. #endif
  243. auto nwritten = TRY(Core::System::write(new_passwd_fd, new_passwd_file_content.bytes()));
  244. VERIFY(static_cast<size_t>(nwritten) == new_passwd_file_content.length());
  245. #ifndef AK_OS_BSD_GENERIC
  246. nwritten = TRY(Core::System::write(new_shadow_fd, new_shadow_file_content.bytes()));
  247. VERIFY(static_cast<size_t>(nwritten) == new_shadow_file_content.length());
  248. #endif
  249. }
  250. TRY(Core::System::rename(new_passwd_name, "/etc/passwd"));
  251. #ifndef AK_OS_BSD_GENERIC
  252. TRY(Core::System::rename(new_shadow_name, "/etc/shadow"));
  253. #endif
  254. return {};
  255. // FIXME: Sync extra groups.
  256. }
  257. }