Account.cpp 8.7 KB

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