Account.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. Account Account::self(Read options)
  62. {
  63. struct passwd fallback;
  64. fallback.pw_name = const_cast<char*>("(unknown)");
  65. fallback.pw_uid = getuid();
  66. fallback.pw_gid = getgid();
  67. fallback.pw_gecos = const_cast<char*>("");
  68. fallback.pw_dir = const_cast<char*>("(unknown)");
  69. fallback.pw_shell = const_cast<char*>("(unknown)");
  70. Vector<gid_t> extra_gids;
  71. int extra_gid_count = getgroups(0, nullptr);
  72. if (extra_gid_count) {
  73. extra_gids.resize(extra_gid_count);
  74. int rc = getgroups(extra_gid_count, extra_gids.data());
  75. if (rc < 0)
  76. extra_gids.resize(0);
  77. }
  78. struct passwd* pwd = getpwuid(fallback.pw_uid);
  79. if (!pwd)
  80. pwd = &fallback;
  81. else
  82. pwd->pw_gid = fallback.pw_gid;
  83. spwd spwd_dummy = {};
  84. spwd_dummy.sp_namp = pwd->pw_name;
  85. spwd_dummy.sp_pwdp = const_cast<char*>("");
  86. #ifndef AK_OS_BSD_GENERIC
  87. spwd* spwd = nullptr;
  88. if (options != Read::PasswdOnly)
  89. spwd = getspnam(pwd->pw_name);
  90. if (!spwd)
  91. spwd = &spwd_dummy;
  92. #else
  93. (void)options;
  94. auto* spwd = &spwd_dummy;
  95. #endif
  96. return Account(*pwd, *spwd, extra_gids);
  97. }
  98. Result<Account, String> Account::from_name(const char* username, Read options)
  99. {
  100. errno = 0;
  101. auto* pwd = getpwnam(username);
  102. if (!pwd) {
  103. if (errno == 0)
  104. return String("No such user");
  105. return String(strerror(errno));
  106. }
  107. spwd spwd_dummy = {};
  108. spwd_dummy.sp_namp = const_cast<char*>(username);
  109. spwd_dummy.sp_pwdp = const_cast<char*>("");
  110. #ifndef AK_OS_BSD_GENERIC
  111. spwd* spwd = nullptr;
  112. if (options != Read::PasswdOnly)
  113. spwd = getspnam(pwd->pw_name);
  114. if (!spwd)
  115. spwd = &spwd_dummy;
  116. #else
  117. (void)options;
  118. auto* spwd = &spwd_dummy;
  119. #endif
  120. return from_passwd(*pwd, *spwd);
  121. }
  122. Result<Account, String> Account::from_uid(uid_t uid, Read options)
  123. {
  124. errno = 0;
  125. auto* pwd = getpwuid(uid);
  126. if (!pwd) {
  127. if (errno == 0)
  128. return String("No such user");
  129. return String(strerror(errno));
  130. }
  131. spwd spwd_dummy = {};
  132. spwd_dummy.sp_namp = pwd->pw_name;
  133. spwd_dummy.sp_pwdp = const_cast<char*>("");
  134. #ifndef AK_OS_BSD_GENERIC
  135. spwd* spwd = nullptr;
  136. if (options != Read::PasswdOnly)
  137. spwd = getspnam(pwd->pw_name);
  138. if (!spwd)
  139. spwd = &spwd_dummy;
  140. #else
  141. (void)options;
  142. auto* spwd = &spwd_dummy;
  143. #endif
  144. return from_passwd(*pwd, *spwd);
  145. }
  146. bool Account::authenticate(const char* password) const
  147. {
  148. // If there was no shadow entry for this account, authentication always fails.
  149. if (m_password_hash.is_null())
  150. return false;
  151. // An empty passwd field indicates that no password is required to log in.
  152. if (m_password_hash.is_empty())
  153. return true;
  154. // FIXME: Use crypt_r if it can be built in lagom.
  155. char* hash = crypt(password, m_password_hash.characters());
  156. return hash != nullptr && strcmp(hash, m_password_hash.characters()) == 0;
  157. }
  158. bool Account::login() const
  159. {
  160. if (setgroups(m_extra_gids.size(), m_extra_gids.data()) < 0)
  161. return false;
  162. if (setgid(m_gid) < 0)
  163. return false;
  164. if (setuid(m_uid) < 0)
  165. return false;
  166. return true;
  167. }
  168. void Account::set_password(const char* password)
  169. {
  170. m_password_hash = crypt(password, get_salt().characters());
  171. }
  172. void Account::set_password_enabled(bool enabled)
  173. {
  174. if (enabled && m_password_hash != "" && m_password_hash[0] == '!') {
  175. m_password_hash = m_password_hash.substring(1, m_password_hash.length() - 1);
  176. } else if (!enabled && (m_password_hash == "" || m_password_hash[0] != '!')) {
  177. StringBuilder builder;
  178. builder.append('!');
  179. builder.append(m_password_hash);
  180. m_password_hash = builder.build();
  181. }
  182. }
  183. void Account::delete_password()
  184. {
  185. m_password_hash = "";
  186. }
  187. Account::Account(const passwd& pwd, const spwd& spwd, Vector<gid_t> extra_gids)
  188. : m_username(pwd.pw_name)
  189. , m_password_hash(spwd.sp_pwdp)
  190. , m_uid(pwd.pw_uid)
  191. , m_gid(pwd.pw_gid)
  192. , m_gecos(pwd.pw_gecos)
  193. , m_home_directory(pwd.pw_dir)
  194. , m_shell(pwd.pw_shell)
  195. , m_extra_gids(move(extra_gids))
  196. {
  197. }
  198. String Account::generate_passwd_file() const
  199. {
  200. StringBuilder builder;
  201. setpwent();
  202. struct passwd* p;
  203. errno = 0;
  204. while ((p = getpwent())) {
  205. if (p->pw_uid == m_uid) {
  206. builder.appendff("{}:!:{}:{}:{}:{}:{}\n",
  207. m_username,
  208. m_uid, m_gid,
  209. m_gecos,
  210. m_home_directory,
  211. m_shell);
  212. } else {
  213. builder.appendff("{}:!:{}:{}:{}:{}:{}\n",
  214. p->pw_name, p->pw_uid,
  215. p->pw_gid, p->pw_gecos, p->pw_dir,
  216. p->pw_shell);
  217. }
  218. }
  219. endpwent();
  220. if (errno) {
  221. dbgln("errno was non-zero after generating new passwd file.");
  222. return {};
  223. }
  224. return builder.to_string();
  225. }
  226. #ifndef AK_OS_BSD_GENERIC
  227. String Account::generate_shadow_file() const
  228. {
  229. StringBuilder builder;
  230. setspent();
  231. struct spwd* p;
  232. errno = 0;
  233. while ((p = getspent())) {
  234. if (p->sp_namp == m_username) {
  235. builder.appendff("{}:{}:{}:{}:{}:{}:{}:{}:{}\n",
  236. m_username, m_password_hash,
  237. (p->sp_lstchg == -1) ? "" : String::formatted("{}", p->sp_lstchg),
  238. (p->sp_min == -1) ? "" : String::formatted("{}", p->sp_min),
  239. (p->sp_max == -1) ? "" : String::formatted("{}", p->sp_max),
  240. (p->sp_warn == -1) ? "" : String::formatted("{}", p->sp_warn),
  241. (p->sp_inact == -1) ? "" : String::formatted("{}", p->sp_inact),
  242. (p->sp_expire == -1) ? "" : String::formatted("{}", p->sp_expire),
  243. (p->sp_flag == 0) ? "" : String::formatted("{}", p->sp_flag));
  244. } else {
  245. builder.appendff("{}:{}:{}:{}:{}:{}:{}:{}:{}\n",
  246. p->sp_namp, p->sp_pwdp,
  247. (p->sp_lstchg == -1) ? "" : String::formatted("{}", p->sp_lstchg),
  248. (p->sp_min == -1) ? "" : String::formatted("{}", p->sp_min),
  249. (p->sp_max == -1) ? "" : String::formatted("{}", p->sp_max),
  250. (p->sp_warn == -1) ? "" : String::formatted("{}", p->sp_warn),
  251. (p->sp_inact == -1) ? "" : String::formatted("{}", p->sp_inact),
  252. (p->sp_expire == -1) ? "" : String::formatted("{}", p->sp_expire),
  253. (p->sp_flag == 0) ? "" : String::formatted("{}", p->sp_flag));
  254. }
  255. }
  256. endspent();
  257. if (errno) {
  258. dbgln("errno was non-zero after generating new passwd file.");
  259. return {};
  260. }
  261. return builder.to_string();
  262. }
  263. #endif
  264. bool Account::sync()
  265. {
  266. auto new_passwd_file_content = generate_passwd_file();
  267. VERIFY(!new_passwd_file_content.is_null());
  268. #ifndef AK_OS_BSD_GENERIC
  269. auto new_shadow_file_content = generate_shadow_file();
  270. VERIFY(!new_shadow_file_content.is_null());
  271. #endif
  272. char new_passwd_name[] = "/etc/passwd.XXXXXX";
  273. #ifndef AK_OS_BSD_GENERIC
  274. char new_shadow_name[] = "/etc/shadow.XXXXXX";
  275. #endif
  276. {
  277. auto new_passwd_fd = mkstemp(new_passwd_name);
  278. if (new_passwd_fd < 0) {
  279. perror("mkstemp");
  280. VERIFY_NOT_REACHED();
  281. }
  282. ScopeGuard new_passwd_fd_guard = [new_passwd_fd] { close(new_passwd_fd); };
  283. #ifndef AK_OS_BSD_GENERIC
  284. auto new_shadow_fd = mkstemp(new_shadow_name);
  285. if (new_shadow_fd < 0) {
  286. perror("mkstemp");
  287. VERIFY_NOT_REACHED();
  288. }
  289. ScopeGuard new_shadow_fd_guard = [new_shadow_fd] { close(new_shadow_fd); };
  290. #endif
  291. if (fchmod(new_passwd_fd, 0644) < 0) {
  292. perror("fchmod");
  293. VERIFY_NOT_REACHED();
  294. }
  295. auto nwritten = write(new_passwd_fd, new_passwd_file_content.characters(), new_passwd_file_content.length());
  296. if (nwritten < 0) {
  297. perror("write");
  298. VERIFY_NOT_REACHED();
  299. }
  300. VERIFY(static_cast<size_t>(nwritten) == new_passwd_file_content.length());
  301. #ifndef AK_OS_BSD_GENERIC
  302. nwritten = write(new_shadow_fd, new_shadow_file_content.characters(), new_shadow_file_content.length());
  303. if (nwritten < 0) {
  304. perror("write");
  305. VERIFY_NOT_REACHED();
  306. }
  307. VERIFY(static_cast<size_t>(nwritten) == new_shadow_file_content.length());
  308. #endif
  309. }
  310. if (rename(new_passwd_name, "/etc/passwd") < 0) {
  311. perror("Failed to install new /etc/passwd");
  312. return false;
  313. }
  314. #ifndef AK_OS_BSD_GENERIC
  315. if (rename(new_shadow_name, "/etc/shadow") < 0) {
  316. perror("Failed to install new /etc/shadow");
  317. return false;
  318. }
  319. #endif
  320. return true;
  321. // FIXME: Sync extra groups.
  322. }
  323. }