Account.cpp 10 KB

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