Account.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Copyright (c) 2020, Peter Elliott <pelliott@ualberta.ca>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Base64.h>
  27. #include <AK/Random.h>
  28. #include <AK/ScopeGuard.h>
  29. #include <LibCore/Account.h>
  30. #include <LibCore/File.h>
  31. #include <errno.h>
  32. #include <grp.h>
  33. #include <pwd.h>
  34. #include <stdio.h>
  35. #include <string.h>
  36. #include <sys/stat.h>
  37. #include <unistd.h>
  38. namespace Core {
  39. static String get_salt()
  40. {
  41. char random_data[12];
  42. fill_with_random(random_data, sizeof(random_data));
  43. StringBuilder builder;
  44. builder.append("$5$");
  45. builder.append(encode_base64(ReadonlyBytes(random_data, sizeof(random_data))));
  46. return builder.build();
  47. }
  48. static Vector<gid_t> get_gids(const StringView& username)
  49. {
  50. Vector<gid_t> extra_gids;
  51. for (auto* group = getgrent(); group; group = getgrent()) {
  52. for (size_t i = 0; group->gr_mem[i]; ++i) {
  53. if (username == group->gr_mem[i]) {
  54. extra_gids.append(group->gr_gid);
  55. break;
  56. }
  57. }
  58. }
  59. endgrent();
  60. return extra_gids;
  61. }
  62. Result<Account, String> Account::from_passwd(const passwd& pwd)
  63. {
  64. Account account(pwd, get_gids(pwd.pw_name));
  65. endpwent();
  66. return account;
  67. }
  68. Result<Account, String> Account::from_name(const char* username)
  69. {
  70. struct passwd* pwd = nullptr;
  71. errno = 0;
  72. pwd = getpwnam(username);
  73. if (!pwd) {
  74. if (errno == 0)
  75. return String("No such user");
  76. return String(strerror(errno));
  77. }
  78. return from_passwd(*pwd);
  79. }
  80. Result<Account, String> Account::from_uid(uid_t uid)
  81. {
  82. struct passwd* pwd = nullptr;
  83. errno = 0;
  84. pwd = getpwuid(uid);
  85. if (!pwd) {
  86. if (errno == 0)
  87. return String("No such user");
  88. return String(strerror(errno));
  89. }
  90. return from_passwd(*pwd);
  91. }
  92. bool Account::authenticate(const char* password) const
  93. {
  94. // If there was no shadow entry for this account, authentication always fails.
  95. if (m_password_hash.is_null())
  96. return false;
  97. // An empty passwd field indicates that no password is required to log in.
  98. if (m_password_hash.is_empty())
  99. return true;
  100. // FIXME: Use crypt_r if it can be built in lagom.
  101. char* hash = crypt(password, m_password_hash.characters());
  102. return hash != nullptr && strcmp(hash, m_password_hash.characters()) == 0;
  103. }
  104. bool Account::login() const
  105. {
  106. if (setgroups(m_extra_gids.size(), m_extra_gids.data()) < 0)
  107. return false;
  108. if (setgid(m_gid) < 0)
  109. return false;
  110. if (setuid(m_uid) < 0)
  111. return false;
  112. return true;
  113. }
  114. void Account::set_password(const char* password)
  115. {
  116. m_password_hash = crypt(password, get_salt().characters());
  117. }
  118. void Account::set_password_enabled(bool enabled)
  119. {
  120. if (enabled && m_password_hash != "" && m_password_hash[0] == '!') {
  121. m_password_hash = m_password_hash.substring(1, m_password_hash.length() - 1);
  122. } else if (!enabled && (m_password_hash == "" || m_password_hash[0] != '!')) {
  123. StringBuilder builder;
  124. builder.append('!');
  125. builder.append(m_password_hash);
  126. m_password_hash = builder.build();
  127. }
  128. }
  129. void Account::delete_password()
  130. {
  131. m_password_hash = "";
  132. }
  133. Account::Account(const passwd& pwd, Vector<gid_t> extra_gids)
  134. : m_username(pwd.pw_name)
  135. , m_uid(pwd.pw_uid)
  136. , m_gid(pwd.pw_gid)
  137. , m_gecos(pwd.pw_gecos)
  138. , m_home_directory(pwd.pw_dir)
  139. , m_shell(pwd.pw_shell)
  140. , m_extra_gids(extra_gids)
  141. {
  142. load_shadow_file();
  143. }
  144. String Account::generate_passwd_file() const
  145. {
  146. StringBuilder builder;
  147. setpwent();
  148. struct passwd* p;
  149. errno = 0;
  150. while ((p = getpwent())) {
  151. if (p->pw_uid == m_uid) {
  152. builder.appendff("{}:!:{}:{}:{}:{}:{}\n",
  153. m_username,
  154. m_uid, m_gid,
  155. m_gecos,
  156. m_home_directory,
  157. m_shell);
  158. } else {
  159. builder.appendff("{}:!:{}:{}:{}:{}:{}\n",
  160. p->pw_name, p->pw_uid,
  161. p->pw_gid, p->pw_gecos, p->pw_dir,
  162. p->pw_shell);
  163. }
  164. }
  165. endpwent();
  166. if (errno) {
  167. dbgln("errno was non-zero after generating new passwd file.");
  168. return {};
  169. }
  170. return builder.to_string();
  171. }
  172. void Account::load_shadow_file()
  173. {
  174. auto file_or_error = Core::File::open("/etc/shadow", Core::File::ReadOnly);
  175. VERIFY(!file_or_error.is_error());
  176. auto shadow_file = file_or_error.release_value();
  177. VERIFY(shadow_file->is_open());
  178. Vector<ShadowEntry> entries;
  179. for (;;) {
  180. auto line = shadow_file->read_line();
  181. if (line.is_null())
  182. break;
  183. auto parts = line.split(':', true);
  184. if (parts.size() != 2) {
  185. dbgln("Malformed shadow entry, ignoring.");
  186. continue;
  187. }
  188. const auto& username = parts[0];
  189. const auto& password_hash = parts[1];
  190. entries.append({ username, password_hash });
  191. if (username == m_username) {
  192. m_password_hash = password_hash;
  193. }
  194. }
  195. m_shadow_entries = move(entries);
  196. }
  197. String Account::generate_shadow_file() const
  198. {
  199. StringBuilder builder;
  200. bool updated_entry_in_place = false;
  201. for (auto& entry : m_shadow_entries) {
  202. if (entry.username == m_username) {
  203. updated_entry_in_place = true;
  204. builder.appendff("{}:{}\n", m_username, m_password_hash);
  205. } else {
  206. builder.appendff("{}:{}\n", entry.username, entry.password_hash);
  207. }
  208. }
  209. if (!updated_entry_in_place)
  210. builder.appendff("{}:{}\n", m_username, m_password_hash);
  211. return builder.to_string();
  212. }
  213. bool Account::sync()
  214. {
  215. auto new_passwd_file_content = generate_passwd_file();
  216. auto new_shadow_file_content = generate_shadow_file();
  217. if (new_passwd_file_content.is_null() || new_shadow_file_content.is_null()) {
  218. VERIFY_NOT_REACHED();
  219. }
  220. char new_passwd_name[] = "/etc/passwd.XXXXXX";
  221. char new_shadow_name[] = "/etc/shadow.XXXXXX";
  222. {
  223. auto new_passwd_fd = mkstemp(new_passwd_name);
  224. if (new_passwd_fd < 0) {
  225. perror("mkstemp");
  226. VERIFY_NOT_REACHED();
  227. }
  228. ScopeGuard new_passwd_fd_guard = [new_passwd_fd] { close(new_passwd_fd); };
  229. auto new_shadow_fd = mkstemp(new_shadow_name);
  230. if (new_shadow_fd < 0) {
  231. perror("mkstemp");
  232. VERIFY_NOT_REACHED();
  233. }
  234. ScopeGuard new_shadow_fd_guard = [new_shadow_fd] { close(new_shadow_fd); };
  235. if (fchmod(new_passwd_fd, 0644) < 0) {
  236. perror("fchmod");
  237. VERIFY_NOT_REACHED();
  238. }
  239. auto nwritten = write(new_passwd_fd, new_passwd_file_content.characters(), new_passwd_file_content.length());
  240. if (nwritten < 0) {
  241. perror("write");
  242. VERIFY_NOT_REACHED();
  243. }
  244. VERIFY(static_cast<size_t>(nwritten) == new_passwd_file_content.length());
  245. nwritten = write(new_shadow_fd, new_shadow_file_content.characters(), new_shadow_file_content.length());
  246. if (nwritten < 0) {
  247. perror("write");
  248. VERIFY_NOT_REACHED();
  249. }
  250. VERIFY(static_cast<size_t>(nwritten) == new_shadow_file_content.length());
  251. }
  252. if (rename(new_passwd_name, "/etc/passwd") < 0) {
  253. perror("Failed to install new /etc/passwd");
  254. return false;
  255. }
  256. if (rename(new_shadow_name, "/etc/shadow") < 0) {
  257. perror("Failed to install new /etc/shadow");
  258. return false;
  259. }
  260. return true;
  261. // FIXME: Sync extra groups.
  262. }
  263. }