Account.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (c) 2020, Peter Elliott <pelliott@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/ByteString.h>
  8. #include <AK/Types.h>
  9. #include <AK/Vector.h>
  10. #include <LibCore/SecretString.h>
  11. #include <pwd.h>
  12. #ifndef AK_OS_BSD_GENERIC
  13. # include <shadow.h>
  14. #endif
  15. #include <sys/types.h>
  16. namespace Core {
  17. #ifdef AK_OS_BSD_GENERIC
  18. struct spwd {
  19. char* sp_namp;
  20. char* sp_pwdp;
  21. };
  22. #endif
  23. class Account {
  24. public:
  25. enum class Read {
  26. All,
  27. PasswdOnly
  28. };
  29. static ErrorOr<Account> self(Read options = Read::All);
  30. static ErrorOr<Account> from_name(StringView username, Read options = Read::All);
  31. static ErrorOr<Account> from_uid(uid_t uid, Read options = Read::All);
  32. static ErrorOr<Vector<Account>> all(Read options = Read::All);
  33. bool authenticate(SecretString const& password) const;
  34. ErrorOr<void> login() const;
  35. ByteString username() const { return m_username; }
  36. ByteString password_hash() const { return m_password_hash.value_or(ByteString::empty()); }
  37. // Setters only affect in-memory copy of password.
  38. // You must call sync to apply changes.
  39. void set_password(SecretString const& password);
  40. void set_password_enabled(bool enabled);
  41. void set_home_directory(StringView home_directory) { m_home_directory = home_directory; }
  42. void set_uid(uid_t uid) { m_uid = uid; }
  43. void set_gid(gid_t gid) { m_gid = gid; }
  44. void set_shell(StringView shell) { m_shell = shell; }
  45. void set_gecos(StringView gecos) { m_gecos = gecos; }
  46. void set_deleted() { m_deleted = true; }
  47. void set_extra_gids(Vector<gid_t> extra_gids) { m_extra_gids = move(extra_gids); }
  48. void delete_password();
  49. // A nonexistent password means that this account was missing from /etc/shadow.
  50. // It's considered to have a password in that case, and authentication will always fail.
  51. bool has_password() const { return !m_password_hash.has_value() || !m_password_hash->is_empty(); }
  52. uid_t uid() const { return m_uid; }
  53. gid_t gid() const { return m_gid; }
  54. ByteString const& gecos() const { return m_gecos; }
  55. ByteString const& home_directory() const { return m_home_directory; }
  56. ByteString const& shell() const { return m_shell; }
  57. Vector<gid_t> const& extra_gids() const { return m_extra_gids; }
  58. ErrorOr<void> sync();
  59. private:
  60. static ErrorOr<Account> from_passwd(passwd const&, spwd const&);
  61. Account(passwd const& pwd, spwd const& spwd, Vector<gid_t> extra_gids);
  62. ErrorOr<ByteString> generate_passwd_file() const;
  63. ErrorOr<ByteString> generate_group_file() const;
  64. #ifndef AK_OS_BSD_GENERIC
  65. ErrorOr<ByteString> generate_shadow_file() const;
  66. #endif
  67. ByteString m_username;
  68. Optional<ByteString> m_password_hash;
  69. uid_t m_uid { 0 };
  70. gid_t m_gid { 0 };
  71. ByteString m_gecos;
  72. ByteString m_home_directory;
  73. ByteString m_shell;
  74. Vector<gid_t> m_extra_gids;
  75. bool m_deleted { false };
  76. };
  77. }