| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2021-08-31 19:32:46 -07:00
										 |  |  |  * Copyright (c) 2020, Peter Elliott <pelliott@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
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <AK/String.h>
 | 
					
						
							|  |  |  | #include <AK/Types.h>
 | 
					
						
							|  |  |  | #include <AK/Vector.h>
 | 
					
						
							| 
									
										
										
										
											2021-09-12 07:02:17 -07:00
										 |  |  | #include <LibCore/SecretString.h>
 | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  | #include <pwd.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-07 10:12:58 +02:00
										 |  |  | #ifndef AK_OS_BSD_GENERIC
 | 
					
						
							| 
									
										
										
										
											2021-05-01 13:08:25 +02:00
										 |  |  | #    include <shadow.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  | #include <sys/types.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Core { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-07 10:12:58 +02:00
										 |  |  | #ifdef AK_OS_BSD_GENERIC
 | 
					
						
							|  |  |  | struct spwd { | 
					
						
							|  |  |  |     char* sp_namp; | 
					
						
							|  |  |  |     char* sp_pwdp; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  | class Account { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2021-07-08 22:00:23 +02:00
										 |  |  |     enum class Read { | 
					
						
							|  |  |  |         All, | 
					
						
							|  |  |  |         PasswdOnly | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-11 20:53:39 +00:00
										 |  |  |     // FIXME: Convert the methods below to take StringViews instead.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-01 18:48:09 +01:00
										 |  |  |     static ErrorOr<Account> self(Read options = Read::All); | 
					
						
							| 
									
										
										
										
											2021-11-07 11:41:48 +01:00
										 |  |  |     static ErrorOr<Account> from_name(char const* username, Read options = Read::All); | 
					
						
							|  |  |  |     static ErrorOr<Account> from_uid(uid_t uid, Read options = Read::All); | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-12 07:02:17 -07:00
										 |  |  |     bool authenticate(SecretString const& password) const; | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  |     bool login() const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     String username() const { return m_username; } | 
					
						
							|  |  |  |     String password_hash() const { return m_password_hash; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Setters only affect in-memory copy of password.
 | 
					
						
							|  |  |  |     // You must call sync to apply changes.
 | 
					
						
							| 
									
										
										
										
											2021-10-19 09:32:47 -04:00
										 |  |  |     void set_password(SecretString const& password); | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  |     void set_password_enabled(bool enabled); | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |     void set_home_directory(char const* home_directory) { m_home_directory = home_directory; } | 
					
						
							| 
									
										
										
										
											2021-06-13 07:17:57 -04:00
										 |  |  |     void set_uid(uid_t uid) { m_uid = uid; } | 
					
						
							|  |  |  |     void set_gid(gid_t gid) { m_gid = gid; } | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |     void set_shell(char const* shell) { m_shell = shell; } | 
					
						
							|  |  |  |     void set_gecos(char const* gecos) { m_gecos = gecos; } | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  |     void delete_password(); | 
					
						
							| 
									
										
										
										
											2021-01-21 11:31:12 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // A null password means that this account was missing from /etc/shadow.
 | 
					
						
							|  |  |  |     // It's considered to have a password in that case, and authentication will always fail.
 | 
					
						
							|  |  |  |     bool has_password() const { return !m_password_hash.is_empty() || m_password_hash.is_null(); } | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     uid_t uid() const { return m_uid; } | 
					
						
							|  |  |  |     gid_t gid() const { return m_gid; } | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |     String const& gecos() const { return m_gecos; } | 
					
						
							|  |  |  |     String const& home_directory() const { return m_home_directory; } | 
					
						
							|  |  |  |     String const& shell() const { return m_shell; } | 
					
						
							|  |  |  |     Vector<gid_t> const& extra_gids() const { return m_extra_gids; } | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-16 21:41:57 +01:00
										 |  |  |     ErrorOr<void> sync(); | 
					
						
							| 
									
										
										
										
											2020-07-30 17:31:40 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2021-11-07 11:41:48 +01:00
										 |  |  |     static ErrorOr<Account> from_passwd(passwd const&, spwd const&); | 
					
						
							| 
									
										
										
										
											2021-01-09 17:44:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |     Account(passwd const& pwd, spwd const& spwd, Vector<gid_t> extra_gids); | 
					
						
							| 
									
										
										
										
											2021-01-09 17:44:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-16 21:41:57 +01:00
										 |  |  |     ErrorOr<String> generate_passwd_file() const; | 
					
						
							| 
									
										
										
										
											2021-06-07 10:12:58 +02:00
										 |  |  | #ifndef AK_OS_BSD_GENERIC
 | 
					
						
							| 
									
										
										
										
											2021-12-16 21:41:57 +01:00
										 |  |  |     ErrorOr<String> generate_shadow_file() const; | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     String m_username; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     String m_password_hash; | 
					
						
							|  |  |  |     uid_t m_uid { 0 }; | 
					
						
							|  |  |  |     gid_t m_gid { 0 }; | 
					
						
							|  |  |  |     String m_gecos; | 
					
						
							|  |  |  |     String m_home_directory; | 
					
						
							|  |  |  |     String m_shell; | 
					
						
							|  |  |  |     Vector<gid_t> m_extra_gids; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |