mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 04:32:44 +00:00 
			
		
		
		
	LibCore: Use StringView instead of char * in Account
				
					
				
			This commit is contained in:
		
							parent
							
								
									0396b6da82
								
							
						
					
					
						commit
						507cb411c2
					
				
					 10 changed files with 33 additions and 36 deletions
				
			
		|  | @ -98,9 +98,9 @@ ErrorOr<Account> Account::self([[maybe_unused]] Read options) | ||||||
|     return Account(*pwd, spwd, extra_gids); |     return Account(*pwd, spwd, extra_gids); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| ErrorOr<Account> Account::from_name(char const* username, [[maybe_unused]] Read options) | ErrorOr<Account> Account::from_name(StringView username, [[maybe_unused]] Read options) | ||||||
| { | { | ||||||
|     auto pwd = TRY(Core::System::getpwnam({ username, strlen(username) })); |     auto pwd = TRY(Core::System::getpwnam(username)); | ||||||
|     if (!pwd.has_value()) |     if (!pwd.has_value()) | ||||||
|         return Error::from_string_literal("No such user"); |         return Error::from_string_literal("No such user"); | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -32,11 +32,9 @@ public: | ||||||
|         PasswdOnly |         PasswdOnly | ||||||
|     }; |     }; | ||||||
| 
 | 
 | ||||||
|     // FIXME: Convert the methods below to take StringViews instead.
 |  | ||||||
| 
 |  | ||||||
|     static String parse_path_with_uid(StringView general_path, Optional<uid_t> force_uid = {}); |     static String parse_path_with_uid(StringView general_path, Optional<uid_t> force_uid = {}); | ||||||
|     static ErrorOr<Account> self(Read options = Read::All); |     static ErrorOr<Account> self(Read options = Read::All); | ||||||
|     static ErrorOr<Account> from_name(char const* username, Read options = Read::All); |     static ErrorOr<Account> from_name(StringView username, Read options = Read::All); | ||||||
|     static ErrorOr<Account> from_uid(uid_t uid, Read options = Read::All); |     static ErrorOr<Account> from_uid(uid_t uid, Read options = Read::All); | ||||||
| 
 | 
 | ||||||
|     bool authenticate(SecretString const& password) const; |     bool authenticate(SecretString const& password) const; | ||||||
|  | @ -51,11 +49,11 @@ public: | ||||||
|     // You must call sync to apply changes.
 |     // You must call sync to apply changes.
 | ||||||
|     void set_password(SecretString const& password); |     void set_password(SecretString const& password); | ||||||
|     void set_password_enabled(bool enabled); |     void set_password_enabled(bool enabled); | ||||||
|     void set_home_directory(char const* home_directory) { m_home_directory = home_directory; } |     void set_home_directory(StringView home_directory) { m_home_directory = home_directory; } | ||||||
|     void set_uid(uid_t uid) { m_uid = uid; } |     void set_uid(uid_t uid) { m_uid = uid; } | ||||||
|     void set_gid(gid_t gid) { m_gid = gid; } |     void set_gid(gid_t gid) { m_gid = gid; } | ||||||
|     void set_shell(char const* shell) { m_shell = shell; } |     void set_shell(StringView shell) { m_shell = shell; } | ||||||
|     void set_gecos(char const* gecos) { m_gecos = gecos; } |     void set_gecos(StringView gecos) { m_gecos = gecos; } | ||||||
|     void delete_password(); |     void delete_password(); | ||||||
| 
 | 
 | ||||||
|     // A null password means that this account was missing from /etc/shadow.
 |     // A null password means that this account was missing from /etc/shadow.
 | ||||||
|  |  | ||||||
|  | @ -80,7 +80,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) | ||||||
| 
 | 
 | ||||||
|         auto fail_message = "Can't log in: invalid username or password."sv; |         auto fail_message = "Can't log in: invalid username or password."sv; | ||||||
| 
 | 
 | ||||||
|         auto account = Core::Account::from_name(username.characters()); |         auto account = Core::Account::from_name(username); | ||||||
|         if (account.is_error()) { |         if (account.is_error()) { | ||||||
|             window->set_fail_message(fail_message); |             window->set_fail_message(fail_message); | ||||||
|             dbgln("failed graphical login for user {}: {}", username, account.error()); |             dbgln("failed graphical login for user {}: {}", username, account.error()); | ||||||
|  | @ -99,13 +99,13 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) | ||||||
|         login(account.value(), *window); |         login(account.value(), *window); | ||||||
|     }; |     }; | ||||||
| 
 | 
 | ||||||
|     char const* auto_login = nullptr; |     StringView auto_login; | ||||||
| 
 | 
 | ||||||
|     Core::ArgsParser args_parser; |     Core::ArgsParser args_parser; | ||||||
|     args_parser.add_option(auto_login, "automatically log in with no prompt", "auto-login", 'a', "username"); |     args_parser.add_option(auto_login, "automatically log in with no prompt", "auto-login", 'a', "username"); | ||||||
|     args_parser.parse(arguments); |     args_parser.parse(arguments); | ||||||
| 
 | 
 | ||||||
|     if (!auto_login) { |     if (auto_login.is_empty()) { | ||||||
|         window->show(); |         window->show(); | ||||||
|     } else { |     } else { | ||||||
|         auto account = Core::Account::from_name(auto_login); |         auto account = Core::Account::from_name(auto_login); | ||||||
|  |  | ||||||
|  | @ -299,7 +299,7 @@ Service::Service(Core::ConfigFile const& config, StringView name) | ||||||
| 
 | 
 | ||||||
|     m_user = config.read_entry(name, "User"); |     m_user = config.read_entry(name, "User"); | ||||||
|     if (!m_user.is_null()) { |     if (!m_user.is_null()) { | ||||||
|         auto result = Core::Account::from_name(m_user.characters(), Core::Account::Read::PasswdOnly); |         auto result = Core::Account::from_name(m_user, Core::Account::Read::PasswdOnly); | ||||||
|         if (result.is_error()) |         if (result.is_error()) | ||||||
|             warnln("Failed to resolve user {}: {}", m_user, result.error()); |             warnln("Failed to resolve user {}: {}", m_user, result.error()); | ||||||
|         else |         else | ||||||
|  |  | ||||||
|  | @ -47,8 +47,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) | ||||||
|         print_account_gids(account); |         print_account_gids(account); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     for (auto username : usernames) { |     for (auto const& username : usernames) { | ||||||
|         auto result = Core::Account::from_name(username.characters(), Core::Account::Read::PasswdOnly); |         auto result = Core::Account::from_name(username, Core::Account::Read::PasswdOnly); | ||||||
|         if (result.is_error()) { |         if (result.is_error()) { | ||||||
|             warnln("{} '{}'", result.error(), username); |             warnln("{} '{}'", result.error(), username); | ||||||
|             continue; |             continue; | ||||||
|  |  | ||||||
|  | @ -53,7 +53,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) | ||||||
|         if (auto user_id = user_str.to_uint(); user_id.has_value()) |         if (auto user_id = user_str.to_uint(); user_id.has_value()) | ||||||
|             account = TRY(Core::Account::from_uid(user_id.value(), Core::Account::Read::PasswdOnly)); |             account = TRY(Core::Account::from_uid(user_id.value(), Core::Account::Read::PasswdOnly)); | ||||||
|         else |         else | ||||||
|             account = TRY(Core::Account::from_name(user_str.characters(), Core::Account::Read::PasswdOnly)); |             account = TRY(Core::Account::from_name(user_str, Core::Account::Read::PasswdOnly)); | ||||||
|     } else { |     } else { | ||||||
|         account = TRY(Core::Account::self(Core::Account::Read::PasswdOnly)); |         account = TRY(Core::Account::self(Core::Account::Read::PasswdOnly)); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  | @ -46,7 +46,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) | ||||||
| 
 | 
 | ||||||
|     // target_account is the account we are changing the password of.
 |     // target_account is the account we are changing the password of.
 | ||||||
|     auto target_account = TRY(!username.is_empty() |     auto target_account = TRY(!username.is_empty() | ||||||
|             ? Core::Account::from_name(username.characters()) |             ? Core::Account::from_name(username) | ||||||
|             : Core::Account::from_uid(current_uid)); |             : Core::Account::from_uid(current_uid)); | ||||||
| 
 | 
 | ||||||
|     setpwent(); |     setpwent(); | ||||||
|  |  | ||||||
|  | @ -21,7 +21,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) | ||||||
|     if (!TRY(Core::System::isatty(STDIN_FILENO))) |     if (!TRY(Core::System::isatty(STDIN_FILENO))) | ||||||
|         return Error::from_string_literal("Standard input is not a terminal"); |         return Error::from_string_literal("Standard input is not a terminal"); | ||||||
| 
 | 
 | ||||||
|     char const* user = nullptr; |     StringView user; | ||||||
| 
 | 
 | ||||||
|     Core::ArgsParser args_parser; |     Core::ArgsParser args_parser; | ||||||
|     args_parser.add_positional_argument(user, "User to switch to (defaults to user with UID 0)", "user", Core::ArgsParser::Required::No); |     args_parser.add_positional_argument(user, "User to switch to (defaults to user with UID 0)", "user", Core::ArgsParser::Required::No); | ||||||
|  | @ -30,7 +30,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) | ||||||
|     if (geteuid() != 0) |     if (geteuid() != 0) | ||||||
|         return Error::from_string_literal("Not running as root :("); |         return Error::from_string_literal("Not running as root :("); | ||||||
| 
 | 
 | ||||||
|     auto account = TRY(user ? Core::Account::from_name(user) : Core::Account::from_uid(0)); |     auto account = TRY(user.is_empty() ? Core::Account::from_uid(0) : Core::Account::from_name(user)); | ||||||
| 
 | 
 | ||||||
|     TRY(Core::System::pledge("stdio tty exec id")); |     TRY(Core::System::pledge("stdio tty exec id")); | ||||||
| 
 | 
 | ||||||
|  | @ -51,7 +51,6 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) | ||||||
| 
 | 
 | ||||||
|     TRY(Core::System::setenv("HOME"sv, account.home_directory(), true)); |     TRY(Core::System::setenv("HOME"sv, account.home_directory(), true)); | ||||||
| 
 | 
 | ||||||
|     execl(account.shell().characters(), account.shell().characters(), nullptr); |     TRY(Core::System::exec(account.shell(), Array<StringView, 1> { account.shell().view() }, Core::System::SearchInPath::No)); | ||||||
|     perror("execl"); |  | ||||||
|     return 1; |     return 1; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -33,7 +33,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) | ||||||
|     TRY(Core::System::unveil("/etc/", "rwc")); |     TRY(Core::System::unveil("/etc/", "rwc")); | ||||||
|     TRY(Core::System::unveil("/bin/rm", "x")); |     TRY(Core::System::unveil("/bin/rm", "x")); | ||||||
| 
 | 
 | ||||||
|     char const* username = nullptr; |     StringView username; | ||||||
|     bool remove_home = false; |     bool remove_home = false; | ||||||
| 
 | 
 | ||||||
|     Core::ArgsParser args_parser; |     Core::ArgsParser args_parser; | ||||||
|  |  | ||||||
|  | @ -30,11 +30,11 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) | ||||||
|     int gid = 0; |     int gid = 0; | ||||||
|     bool lock = false; |     bool lock = false; | ||||||
|     bool unlock = false; |     bool unlock = false; | ||||||
|     char const* new_home_directory = nullptr; |     StringView new_home_directory; | ||||||
|     bool move_home = false; |     bool move_home = false; | ||||||
|     char const* shell = nullptr; |     StringView shell; | ||||||
|     char const* gecos = nullptr; |     StringView gecos; | ||||||
|     char const* username = nullptr; |     StringView username; | ||||||
| 
 | 
 | ||||||
|     auto args_parser = Core::ArgsParser(); |     auto args_parser = Core::ArgsParser(); | ||||||
|     args_parser.set_general_help("Modify a user account"); |     args_parser.set_general_help("Modify a user account"); | ||||||
|  | @ -62,7 +62,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) | ||||||
| 
 | 
 | ||||||
|     if (move_home) { |     if (move_home) { | ||||||
|         TRY(Core::System::unveil(target_account.home_directory(), "c"sv)); |         TRY(Core::System::unveil(target_account.home_directory(), "c"sv)); | ||||||
|         TRY(Core::System::unveil({ new_home_directory, strlen(new_home_directory) }, "wc"sv)); |         TRY(Core::System::unveil(new_home_directory, "wc"sv)); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     unveil(nullptr, nullptr); |     unveil(nullptr, nullptr); | ||||||
|  | @ -98,11 +98,11 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) | ||||||
|         target_account.set_password_enabled(true); |         target_account.set_password_enabled(true); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     if (new_home_directory) { |     if (!new_home_directory.is_empty()) { | ||||||
|         if (move_home) { |         if (move_home) { | ||||||
|             int rc = rename(target_account.home_directory().characters(), new_home_directory); |             auto maybe_error = Core::System::rename(target_account.home_directory(), new_home_directory); | ||||||
|             if (rc < 0) { |             if (maybe_error.is_error()) { | ||||||
|                 if (errno == EXDEV) { |                 if (maybe_error.error().code() == EXDEV) { | ||||||
|                     auto result = Core::File::copy_file_or_directory( |                     auto result = Core::File::copy_file_or_directory( | ||||||
|                         new_home_directory, target_account.home_directory().characters(), |                         new_home_directory, target_account.home_directory().characters(), | ||||||
|                         Core::File::RecursionMode::Allowed, |                         Core::File::RecursionMode::Allowed, | ||||||
|  | @ -113,11 +113,11 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) | ||||||
|                         warnln("usermod: could not move directory {} : {}", target_account.home_directory().characters(), static_cast<Error const&>(result.error())); |                         warnln("usermod: could not move directory {} : {}", target_account.home_directory().characters(), static_cast<Error const&>(result.error())); | ||||||
|                         return 1; |                         return 1; | ||||||
|                     } |                     } | ||||||
|                     rc = unlink(target_account.home_directory().characters()); |                     maybe_error = Core::System::unlink(target_account.home_directory()); | ||||||
|                     if (rc < 0) |                     if (maybe_error.is_error()) | ||||||
|                         warnln("usermod: unlink {} : {}", target_account.home_directory().characters(), strerror(errno)); |                         warnln("usermod: unlink {} : {}", target_account.home_directory(), maybe_error.error().code()); | ||||||
|                 } else { |                 } else { | ||||||
|                     warnln("usermod: could not move directory {} : {}", target_account.home_directory().characters(), strerror(errno)); |                     warnln("usermod: could not move directory {} : {}", target_account.home_directory(), maybe_error.error().code()); | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|  | @ -125,11 +125,11 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) | ||||||
|         target_account.set_home_directory(new_home_directory); |         target_account.set_home_directory(new_home_directory); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     if (shell) { |     if (!shell.is_empty()) { | ||||||
|         target_account.set_shell(shell); |         target_account.set_shell(shell); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     if (gecos) { |     if (!gecos.is_empty()) { | ||||||
|         target_account.set_gecos(gecos); |         target_account.set_gecos(gecos); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Lucas CHOLLET
						Lucas CHOLLET