| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2021-08-31 19:32:46 -07:00
										 |  |  |  * Copyright (c) 2020, Peter Elliott <pelliott@serenityos.org> | 
					
						
							| 
									
										
										
										
											2022-03-12 21:50:31 -08:00
										 |  |  |  * Copyright (c) 2021-2022, Brian Gianforcaro <bgianf@serenityos.org> | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <AK/Base64.h>
 | 
					
						
							| 
									
										
										
										
											2022-03-12 21:50:31 -08:00
										 |  |  | #include <AK/Memory.h>
 | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  | #include <AK/Random.h>
 | 
					
						
							| 
									
										
										
										
											2021-01-21 09:58:31 +01:00
										 |  |  | #include <AK/ScopeGuard.h>
 | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  | #include <LibCore/Account.h>
 | 
					
						
							| 
									
										
										
										
											2022-07-17 15:41:01 +02:00
										 |  |  | #include <LibCore/Directory.h>
 | 
					
						
							| 
									
										
										
										
											2021-12-16 21:41:57 +01:00
										 |  |  | #include <LibCore/System.h>
 | 
					
						
							| 
									
										
										
										
											2022-01-01 20:12:41 +01:00
										 |  |  | #include <LibCore/UmaskScope.h>
 | 
					
						
							| 
									
										
										
										
											2021-03-12 17:29:37 +01:00
										 |  |  | #include <errno.h>
 | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  | #include <grp.h>
 | 
					
						
							|  |  |  | #include <pwd.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-07 10:12:58 +02:00
										 |  |  | #ifndef AK_OS_BSD_GENERIC
 | 
					
						
							| 
									
										
										
										
											2021-11-10 08:17:21 -05:00
										 |  |  | #    include <crypt.h>
 | 
					
						
							| 
									
										
										
										
											2021-05-01 13:08:25 +02:00
										 |  |  | #    include <shadow.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							| 
									
										
										
										
											2021-01-21 09:58:31 +01:00
										 |  |  | #include <sys/stat.h>
 | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  | #include <unistd.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Core { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  | static DeprecatedString get_salt() | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  | { | 
					
						
							|  |  |  |     char random_data[12]; | 
					
						
							| 
									
										
										
										
											2021-02-25 21:10:47 +01:00
										 |  |  |     fill_with_random(random_data, sizeof(random_data)); | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     StringBuilder builder; | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |     builder.append("$5$"sv); | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  |     builder.append(encode_base64(ReadonlyBytes(random_data, sizeof(random_data)))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return builder.build(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  | static Vector<gid_t> get_extra_gids(passwd const& pwd) | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-07-11 19:53:29 +00:00
										 |  |  |     StringView username { pwd.pw_name, strlen(pwd.pw_name) }; | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  |     Vector<gid_t> extra_gids; | 
					
						
							| 
									
										
										
										
											2021-05-05 13:22:50 +02:00
										 |  |  |     setgrent(); | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  |     for (auto* group = getgrent(); group; group = getgrent()) { | 
					
						
							| 
									
										
										
										
											2021-05-04 15:11:44 +01:00
										 |  |  |         if (group->gr_gid == pwd.pw_gid) | 
					
						
							|  |  |  |             continue; | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  |         for (size_t i = 0; group->gr_mem[i]; ++i) { | 
					
						
							|  |  |  |             if (username == group->gr_mem[i]) { | 
					
						
							|  |  |  |                 extra_gids.append(group->gr_gid); | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     endgrent(); | 
					
						
							|  |  |  |     return extra_gids; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  | ErrorOr<Account> Account::from_passwd(passwd const& pwd, spwd const& spwd) | 
					
						
							| 
									
										
										
										
											2021-01-09 17:44:44 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-04 15:11:44 +01:00
										 |  |  |     Account account(pwd, spwd, get_extra_gids(pwd)); | 
					
						
							| 
									
										
										
										
											2021-01-09 17:44:44 +01:00
										 |  |  |     endpwent(); | 
					
						
							| 
									
										
										
										
											2021-06-07 10:12:58 +02:00
										 |  |  | #ifndef AK_OS_BSD_GENERIC
 | 
					
						
							| 
									
										
										
										
											2021-05-01 11:43:38 +02:00
										 |  |  |     endspent(); | 
					
						
							| 
									
										
										
										
											2021-05-01 13:08:25 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2021-01-09 17:44:44 +01:00
										 |  |  |     return account; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-02 21:59:54 +01:00
										 |  |  | ErrorOr<Account> Account::self([[maybe_unused]] Read options) | 
					
						
							| 
									
										
										
										
											2021-07-08 19:31:22 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-01-01 18:48:09 +01:00
										 |  |  |     Vector<gid_t> extra_gids = TRY(Core::System::getgroups()); | 
					
						
							| 
									
										
										
										
											2021-07-08 19:31:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-01 18:48:09 +01:00
										 |  |  |     auto pwd = TRY(Core::System::getpwuid(getuid())); | 
					
						
							|  |  |  |     if (!pwd.has_value()) | 
					
						
							| 
									
										
										
										
											2022-07-11 17:57:32 +00:00
										 |  |  |         return Error::from_string_literal("No such user"); | 
					
						
							| 
									
										
										
										
											2021-07-08 19:31:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-01 18:48:09 +01:00
										 |  |  |     spwd spwd = {}; | 
					
						
							| 
									
										
										
										
											2021-07-08 19:31:22 +02:00
										 |  |  | #ifndef AK_OS_BSD_GENERIC
 | 
					
						
							| 
									
										
										
										
											2022-01-01 18:48:09 +01:00
										 |  |  |     if (options != Read::PasswdOnly) { | 
					
						
							| 
									
										
										
										
											2022-07-11 19:53:29 +00:00
										 |  |  |         auto maybe_spwd = TRY(Core::System::getspnam({ pwd->pw_name, strlen(pwd->pw_name) })); | 
					
						
							| 
									
										
										
										
											2022-01-01 18:48:09 +01:00
										 |  |  |         if (!maybe_spwd.has_value()) | 
					
						
							| 
									
										
										
										
											2022-07-11 17:57:32 +00:00
										 |  |  |             return Error::from_string_literal("No shadow entry for user"); | 
					
						
							| 
									
										
										
										
											2022-01-01 18:48:09 +01:00
										 |  |  |         spwd = maybe_spwd.release_value(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-07-08 19:31:22 +02:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-01 18:48:09 +01:00
										 |  |  |     return Account(*pwd, spwd, extra_gids); | 
					
						
							| 
									
										
										
										
											2021-07-08 19:31:22 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-10 20:06:06 +02:00
										 |  |  | ErrorOr<Account> Account::from_name(StringView username, [[maybe_unused]] Read options) | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-06-10 20:06:06 +02:00
										 |  |  |     auto pwd = TRY(Core::System::getpwnam(username)); | 
					
						
							| 
									
										
										
										
											2022-01-01 18:48:09 +01:00
										 |  |  |     if (!pwd.has_value()) | 
					
						
							| 
									
										
										
										
											2022-07-11 17:57:32 +00:00
										 |  |  |         return Error::from_string_literal("No such user"); | 
					
						
							| 
									
										
										
										
											2022-01-01 18:48:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     spwd spwd = {}; | 
					
						
							| 
									
										
										
										
											2021-06-07 10:12:58 +02:00
										 |  |  | #ifndef AK_OS_BSD_GENERIC
 | 
					
						
							| 
									
										
										
										
											2022-01-01 18:48:09 +01:00
										 |  |  |     if (options != Read::PasswdOnly) { | 
					
						
							| 
									
										
										
										
											2022-07-11 19:53:29 +00:00
										 |  |  |         auto maybe_spwd = TRY(Core::System::getspnam({ pwd->pw_name, strlen(pwd->pw_name) })); | 
					
						
							| 
									
										
										
										
											2022-01-01 18:48:09 +01:00
										 |  |  |         if (!maybe_spwd.has_value()) | 
					
						
							| 
									
										
										
										
											2022-07-11 17:57:32 +00:00
										 |  |  |             return Error::from_string_literal("No shadow entry for user"); | 
					
						
							| 
									
										
										
										
											2022-01-01 18:48:09 +01:00
										 |  |  |         spwd = maybe_spwd.release_value(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-05-01 13:08:25 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2022-01-01 18:48:09 +01:00
										 |  |  |     return from_passwd(*pwd, spwd); | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-02 21:59:54 +01:00
										 |  |  | ErrorOr<Account> Account::from_uid(uid_t uid, [[maybe_unused]] Read options) | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-01-01 18:48:09 +01:00
										 |  |  |     auto pwd = TRY(Core::System::getpwuid(uid)); | 
					
						
							|  |  |  |     if (!pwd.has_value()) | 
					
						
							| 
									
										
										
										
											2022-07-11 17:57:32 +00:00
										 |  |  |         return Error::from_string_literal("No such user"); | 
					
						
							| 
									
										
										
										
											2022-01-01 18:48:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     spwd spwd = {}; | 
					
						
							| 
									
										
										
										
											2021-06-07 10:12:58 +02:00
										 |  |  | #ifndef AK_OS_BSD_GENERIC
 | 
					
						
							| 
									
										
										
										
											2022-01-01 18:48:09 +01:00
										 |  |  |     if (options != Read::PasswdOnly) { | 
					
						
							| 
									
										
										
										
											2022-07-11 19:53:29 +00:00
										 |  |  |         auto maybe_spwd = TRY(Core::System::getspnam({ pwd->pw_name, strlen(pwd->pw_name) })); | 
					
						
							| 
									
										
										
										
											2022-01-01 18:48:09 +01:00
										 |  |  |         if (!maybe_spwd.has_value()) | 
					
						
							| 
									
										
										
										
											2022-07-11 17:57:32 +00:00
										 |  |  |             return Error::from_string_literal("No shadow entry for user"); | 
					
						
							| 
									
										
										
										
											2022-01-01 18:48:09 +01:00
										 |  |  |         spwd = maybe_spwd.release_value(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-05-01 13:08:25 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2022-01-01 18:48:09 +01:00
										 |  |  |     return from_passwd(*pwd, spwd); | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-15 00:40:51 -04:00
										 |  |  | ErrorOr<Vector<Account>> Account::all([[maybe_unused]] Read options) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Vector<Account> accounts; | 
					
						
							|  |  |  |     char buffer[1024] = { 0 }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ScopeGuard pwent_guard([] { endpwent(); }); | 
					
						
							|  |  |  |     setpwent(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-13 14:49:32 -05:00
										 |  |  |     while (true) { | 
					
						
							|  |  |  |         auto pwd = TRY(Core::System::getpwent({ buffer, sizeof(buffer) })); | 
					
						
							|  |  |  |         if (!pwd.has_value()) | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-15 00:40:51 -04:00
										 |  |  |         spwd spwd = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifndef AK_OS_BSD_GENERIC
 | 
					
						
							|  |  |  |         ScopeGuard spent_guard([] { endspent(); }); | 
					
						
							|  |  |  |         if (options != Read::PasswdOnly) { | 
					
						
							| 
									
										
										
										
											2022-12-13 14:49:32 -05:00
										 |  |  |             auto maybe_spwd = TRY(Core::System::getspnam({ pwd->pw_name, strlen(pwd->pw_name) })); | 
					
						
							| 
									
										
										
										
											2022-10-15 00:40:51 -04:00
										 |  |  |             if (!maybe_spwd.has_value()) | 
					
						
							|  |  |  |                 return Error::from_string_literal("No shadow entry for user"); | 
					
						
							|  |  |  |             spwd = maybe_spwd.release_value(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-13 14:49:32 -05:00
										 |  |  |         accounts.append({ *pwd, spwd, get_extra_gids(*pwd) }); | 
					
						
							| 
									
										
										
										
											2022-10-15 00:40:51 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return accounts; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-12 07:02:17 -07:00
										 |  |  | bool Account::authenticate(SecretString const& password) const | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-01-21 11:31:12 +01:00
										 |  |  |     // If there was no shadow entry for this account, authentication always fails.
 | 
					
						
							|  |  |  |     if (m_password_hash.is_null()) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  |     // An empty passwd field indicates that no password is required to log in.
 | 
					
						
							|  |  |  |     if (m_password_hash.is_empty()) | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // FIXME: Use crypt_r if it can be built in lagom.
 | 
					
						
							| 
									
										
										
										
											2021-09-12 07:02:17 -07:00
										 |  |  |     char* hash = crypt(password.characters(), m_password_hash.characters()); | 
					
						
							| 
									
										
										
										
											2022-03-12 21:50:31 -08:00
										 |  |  |     return hash != nullptr && AK::timing_safe_compare(hash, m_password_hash.characters(), m_password_hash.length()); | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-09 14:53:04 +02:00
										 |  |  | ErrorOr<void> Account::login() const | 
					
						
							| 
									
										
										
										
											2022-08-15 14:05:54 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-07-09 14:53:04 +02:00
										 |  |  |     TRY(Core::System::setgroups(m_extra_gids)); | 
					
						
							|  |  |  |     TRY(Core::System::setgid(m_gid)); | 
					
						
							|  |  |  |     TRY(Core::System::setuid(m_uid)); | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-09 14:53:04 +02:00
										 |  |  |     return {}; | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-19 09:32:47 -04:00
										 |  |  | void Account::set_password(SecretString const& password) | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-10-19 09:32:47 -04:00
										 |  |  |     m_password_hash = crypt(password.characters(), get_salt().characters()); | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Account::set_password_enabled(bool enabled) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (enabled && m_password_hash != "" && m_password_hash[0] == '!') { | 
					
						
							|  |  |  |         m_password_hash = m_password_hash.substring(1, m_password_hash.length() - 1); | 
					
						
							|  |  |  |     } else if (!enabled && (m_password_hash == "" || m_password_hash[0] != '!')) { | 
					
						
							|  |  |  |         StringBuilder builder; | 
					
						
							|  |  |  |         builder.append('!'); | 
					
						
							|  |  |  |         builder.append(m_password_hash); | 
					
						
							|  |  |  |         m_password_hash = builder.build(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Account::delete_password() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_password_hash = ""; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  | Account::Account(passwd const& pwd, spwd const& spwd, Vector<gid_t> extra_gids) | 
					
						
							| 
									
										
										
										
											2021-01-21 11:17:06 +01:00
										 |  |  |     : m_username(pwd.pw_name) | 
					
						
							| 
									
										
										
										
											2021-05-01 11:43:38 +02:00
										 |  |  |     , m_password_hash(spwd.sp_pwdp) | 
					
						
							| 
									
										
										
										
											2021-01-09 17:44:44 +01:00
										 |  |  |     , m_uid(pwd.pw_uid) | 
					
						
							|  |  |  |     , m_gid(pwd.pw_gid) | 
					
						
							|  |  |  |     , m_gecos(pwd.pw_gecos) | 
					
						
							|  |  |  |     , m_home_directory(pwd.pw_dir) | 
					
						
							|  |  |  |     , m_shell(pwd.pw_shell) | 
					
						
							| 
									
										
										
										
											2021-05-04 15:15:23 +01:00
										 |  |  |     , m_extra_gids(move(extra_gids)) | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  | ErrorOr<DeprecatedString> Account::generate_passwd_file() const | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-01-09 17:44:44 +01:00
										 |  |  |     StringBuilder builder; | 
					
						
							| 
									
										
										
										
											2022-12-13 14:49:32 -05:00
										 |  |  |     char buffer[1024] = { 0 }; | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-13 14:49:32 -05:00
										 |  |  |     ScopeGuard pwent_guard([] { endpwent(); }); | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  |     setpwent(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-13 14:49:32 -05:00
										 |  |  |     while (true) { | 
					
						
							|  |  |  |         auto pwd = TRY(Core::System::getpwent({ buffer, sizeof(buffer) })); | 
					
						
							|  |  |  |         if (!pwd.has_value()) | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (pwd->pw_name == m_username) { | 
					
						
							| 
									
										
										
										
											2021-01-09 17:44:44 +01:00
										 |  |  |             builder.appendff("{}:!:{}:{}:{}:{}:{}\n", | 
					
						
							| 
									
										
										
										
											2020-10-15 13:21:23 +02:00
										 |  |  |                 m_username, | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  |                 m_uid, m_gid, | 
					
						
							| 
									
										
										
										
											2020-10-15 13:21:23 +02:00
										 |  |  |                 m_gecos, | 
					
						
							|  |  |  |                 m_home_directory, | 
					
						
							|  |  |  |                 m_shell); | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2021-01-09 17:44:44 +01:00
										 |  |  |             builder.appendff("{}:!:{}:{}:{}:{}:{}\n", | 
					
						
							| 
									
										
										
										
											2022-12-13 14:49:32 -05:00
										 |  |  |                 pwd->pw_name, pwd->pw_uid, | 
					
						
							|  |  |  |                 pwd->pw_gid, pwd->pw_gecos, pwd->pw_dir, | 
					
						
							|  |  |  |                 pwd->pw_shell); | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-01-09 17:44:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-06 01:12:49 +00:00
										 |  |  |     return builder.to_deprecated_string(); | 
					
						
							| 
									
										
										
										
											2021-01-09 17:44:44 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-07 10:12:58 +02:00
										 |  |  | #ifndef AK_OS_BSD_GENERIC
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  | ErrorOr<DeprecatedString> Account::generate_shadow_file() const | 
					
						
							| 
									
										
										
										
											2021-01-09 17:44:44 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-05-01 11:43:38 +02:00
										 |  |  |     StringBuilder builder; | 
					
						
							| 
									
										
										
										
											2021-01-09 17:44:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-01 11:43:38 +02:00
										 |  |  |     setspent(); | 
					
						
							| 
									
										
										
										
											2021-01-09 17:44:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-01 11:43:38 +02:00
										 |  |  |     struct spwd* p; | 
					
						
							|  |  |  |     errno = 0; | 
					
						
							|  |  |  |     while ((p = getspent())) { | 
					
						
							|  |  |  |         if (p->sp_namp == m_username) { | 
					
						
							|  |  |  |             builder.appendff("{}:{}:{}:{}:{}:{}:{}:{}:{}\n", | 
					
						
							|  |  |  |                 m_username, m_password_hash, | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |                 (p->sp_lstchg == -1) ? "" : DeprecatedString::formatted("{}", p->sp_lstchg), | 
					
						
							|  |  |  |                 (p->sp_min == -1) ? "" : DeprecatedString::formatted("{}", p->sp_min), | 
					
						
							|  |  |  |                 (p->sp_max == -1) ? "" : DeprecatedString::formatted("{}", p->sp_max), | 
					
						
							|  |  |  |                 (p->sp_warn == -1) ? "" : DeprecatedString::formatted("{}", p->sp_warn), | 
					
						
							|  |  |  |                 (p->sp_inact == -1) ? "" : DeprecatedString::formatted("{}", p->sp_inact), | 
					
						
							|  |  |  |                 (p->sp_expire == -1) ? "" : DeprecatedString::formatted("{}", p->sp_expire), | 
					
						
							|  |  |  |                 (p->sp_flag == 0) ? "" : DeprecatedString::formatted("{}", p->sp_flag)); | 
					
						
							| 
									
										
										
										
											2021-01-09 17:44:44 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2021-05-01 11:43:38 +02:00
										 |  |  |             builder.appendff("{}:{}:{}:{}:{}:{}:{}:{}:{}\n", | 
					
						
							|  |  |  |                 p->sp_namp, p->sp_pwdp, | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |                 (p->sp_lstchg == -1) ? "" : DeprecatedString::formatted("{}", p->sp_lstchg), | 
					
						
							|  |  |  |                 (p->sp_min == -1) ? "" : DeprecatedString::formatted("{}", p->sp_min), | 
					
						
							|  |  |  |                 (p->sp_max == -1) ? "" : DeprecatedString::formatted("{}", p->sp_max), | 
					
						
							|  |  |  |                 (p->sp_warn == -1) ? "" : DeprecatedString::formatted("{}", p->sp_warn), | 
					
						
							|  |  |  |                 (p->sp_inact == -1) ? "" : DeprecatedString::formatted("{}", p->sp_inact), | 
					
						
							|  |  |  |                 (p->sp_expire == -1) ? "" : DeprecatedString::formatted("{}", p->sp_expire), | 
					
						
							|  |  |  |                 (p->sp_flag == 0) ? "" : DeprecatedString::formatted("{}", p->sp_flag)); | 
					
						
							| 
									
										
										
										
											2021-01-09 17:44:44 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-05-01 11:43:38 +02:00
										 |  |  |     endspent(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-16 21:41:57 +01:00
										 |  |  |     if (errno) | 
					
						
							|  |  |  |         return Error::from_errno(errno); | 
					
						
							| 
									
										
										
										
											2021-05-01 11:43:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-06 01:12:49 +00:00
										 |  |  |     return builder.to_deprecated_string(); | 
					
						
							| 
									
										
										
										
											2021-01-09 17:44:44 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2021-05-01 13:08:25 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2021-01-09 17:44:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-16 21:41:57 +01:00
										 |  |  | ErrorOr<void> Account::sync() | 
					
						
							| 
									
										
										
										
											2021-01-09 17:44:44 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-01-01 20:12:41 +01:00
										 |  |  |     Core::UmaskScope umask_scope(0777); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-16 21:41:57 +01:00
										 |  |  |     auto new_passwd_file_content = TRY(generate_passwd_file()); | 
					
						
							| 
									
										
										
										
											2021-06-07 10:12:58 +02:00
										 |  |  | #ifndef AK_OS_BSD_GENERIC
 | 
					
						
							| 
									
										
										
										
											2021-12-16 21:41:57 +01:00
										 |  |  |     auto new_shadow_file_content = TRY(generate_shadow_file()); | 
					
						
							| 
									
										
										
										
											2021-05-01 13:08:25 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2021-01-09 17:44:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-11 19:53:29 +00:00
										 |  |  |     // FIXME: mkstemp taking Span<char> makes this code entirely un-AKable.
 | 
					
						
							|  |  |  |     //        Make this code less char-pointery.
 | 
					
						
							| 
									
										
										
										
											2021-01-21 09:58:31 +01:00
										 |  |  |     char new_passwd_name[] = "/etc/passwd.XXXXXX"; | 
					
						
							| 
									
										
										
										
											2022-07-11 19:53:29 +00:00
										 |  |  |     size_t new_passwd_name_length = strlen(new_passwd_name); | 
					
						
							| 
									
										
										
										
											2021-06-07 10:12:58 +02:00
										 |  |  | #ifndef AK_OS_BSD_GENERIC
 | 
					
						
							| 
									
										
										
										
											2021-01-21 09:58:31 +01:00
										 |  |  |     char new_shadow_name[] = "/etc/shadow.XXXXXX"; | 
					
						
							| 
									
										
										
										
											2022-07-11 19:53:29 +00:00
										 |  |  |     size_t new_shadow_name_length = strlen(new_shadow_name); | 
					
						
							| 
									
										
										
										
											2021-05-01 13:08:25 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2021-01-09 17:44:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-21 09:58:31 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-07-11 19:53:29 +00:00
										 |  |  |         auto new_passwd_fd = TRY(Core::System::mkstemp({ new_passwd_name, new_passwd_name_length })); | 
					
						
							| 
									
										
										
										
											2021-01-21 09:58:31 +01:00
										 |  |  |         ScopeGuard new_passwd_fd_guard = [new_passwd_fd] { close(new_passwd_fd); }; | 
					
						
							| 
									
										
										
										
											2022-01-01 20:12:41 +01:00
										 |  |  |         TRY(Core::System::fchmod(new_passwd_fd, 0644)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-07 10:12:58 +02:00
										 |  |  | #ifndef AK_OS_BSD_GENERIC
 | 
					
						
							| 
									
										
										
										
											2022-07-11 19:53:29 +00:00
										 |  |  |         auto new_shadow_fd = TRY(Core::System::mkstemp({ new_shadow_name, new_shadow_name_length })); | 
					
						
							| 
									
										
										
										
											2021-01-21 09:58:31 +01:00
										 |  |  |         ScopeGuard new_shadow_fd_guard = [new_shadow_fd] { close(new_shadow_fd); }; | 
					
						
							| 
									
										
										
										
											2022-01-01 20:12:41 +01:00
										 |  |  |         TRY(Core::System::fchmod(new_shadow_fd, 0600)); | 
					
						
							| 
									
										
										
										
											2021-05-01 13:08:25 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-16 21:41:57 +01:00
										 |  |  |         auto nwritten = TRY(Core::System::write(new_passwd_fd, new_passwd_file_content.bytes())); | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |         VERIFY(static_cast<size_t>(nwritten) == new_passwd_file_content.length()); | 
					
						
							| 
									
										
										
										
											2021-01-21 09:58:31 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-07 10:12:58 +02:00
										 |  |  | #ifndef AK_OS_BSD_GENERIC
 | 
					
						
							| 
									
										
										
										
											2021-12-16 21:41:57 +01:00
										 |  |  |         nwritten = TRY(Core::System::write(new_shadow_fd, new_shadow_file_content.bytes())); | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |         VERIFY(static_cast<size_t>(nwritten) == new_shadow_file_content.length()); | 
					
						
							| 
									
										
										
										
											2021-05-01 13:08:25 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2021-01-09 17:44:44 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-11 19:53:29 +00:00
										 |  |  |     TRY(Core::System::rename({ new_passwd_name, new_passwd_name_length }, "/etc/passwd"sv)); | 
					
						
							| 
									
										
										
										
											2021-06-07 10:12:58 +02:00
										 |  |  | #ifndef AK_OS_BSD_GENERIC
 | 
					
						
							| 
									
										
										
										
											2022-07-11 19:53:29 +00:00
										 |  |  |     TRY(Core::System::rename({ new_shadow_name, new_shadow_name_length }, "/etc/shadow"sv)); | 
					
						
							| 
									
										
										
										
											2021-05-01 13:08:25 +02:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-16 21:41:57 +01:00
										 |  |  |     return {}; | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  |     // FIXME: Sync extra groups.
 | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-01-09 17:44:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  | } |