Account.cpp 7.3 KB

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