diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-05-02 19:38:46 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-02 19:38:46 +0200 |
| commit | 997be16e5ac9a68d54882ab69529815860d62955 (patch) | |
| tree | 19d6129c2d776bb1edc5d4a7325e39ca176c3403 /dev/kernel/src/User.cc | |
| parent | 618104e74c195d7508a18450524f8ed7f9af8cc6 (diff) | |
| parent | b3b4b1ebdcd6adeac914869017c86d892b7a8ced (diff) | |
Merge pull request #28 from nekernel-org/dev
0.0.2
Diffstat (limited to 'dev/kernel/src/User.cc')
| -rw-r--r-- | dev/kernel/src/User.cc | 271 |
1 files changed, 123 insertions, 148 deletions
diff --git a/dev/kernel/src/User.cc b/dev/kernel/src/User.cc index d29ed112..3e6aeeba 100644 --- a/dev/kernel/src/User.cc +++ b/dev/kernel/src/User.cc @@ -2,194 +2,169 @@ * ======================================================== * * NeKernel - * Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. + * Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. * - * File: User.cc - * Purpose: User class, used to provide authentication and security. + * File: User.cc + * Purpose: User class, used to provide authentication and security. * * ======================================================== */ -#include <KernelKit/User.h> -#include <KernelKit/KPC.h> -#include <NewKit/KernelPanic.h> #include <KernelKit/FileMgr.h> +#include <KernelKit/KPC.h> #include <KernelKit/MemoryMgr.h> +#include <KernelKit/User.h> +#include <NewKit/KernelPanic.h> -#define kStdUserType (0xEE) +#define kStdUserType (0xEE) #define kSuperUserType (0xEF) /// @file User.cc /// @brief Multi-user support. -namespace Kernel -{ - namespace Detail - { - //////////////////////////////////////////////////////////// - /// \brief Constructs a password by hashing the password. - /// \param password password to hash. - /// \return the hashed password - //////////////////////////////////////////////////////////// - Int32 cred_construct_token(Char* password, const Char* in_password, User* user, SizeT length) - { - if (!password || !user) - return 1; +namespace Kernel { +namespace Detail { + //////////////////////////////////////////////////////////// + /// \brief Constructs a password by hashing the password. + /// \param password password to hash. + /// \return the hashed password + //////////////////////////////////////////////////////////// + Int32 user_standard_token_generator(Char* password, const Char* in_password, User* user, + SizeT length) { + if (!password || !user) return 1; + if (*password == 0) return 1; + + kout << "user_standard_token_generator: Hashing user password...\r"; + + for (SizeT i_pass = 0UL; i_pass < length; ++i_pass) { + Char cur_chr = in_password[i_pass]; + + password[i_pass] = cur_chr | (user->IsStdUser() ? kStdUserType : kSuperUserType); + } + + kout << "user_standard_token_generator: Hashed user password.\r"; + + return 0; + } +} // namespace Detail + +//////////////////////////////////////////////////////////// +/// @brief User ring constructor. +//////////////////////////////////////////////////////////// +User::User(const Int32& sel, const Char* user_name) : mUserRing((UserRingKind) sel) { + MUST_PASS(sel >= 0); + rt_copy_memory((VoidPtr) user_name, this->mUserName, rt_string_len(user_name)); +} + +//////////////////////////////////////////////////////////// +/// @brief User ring constructor. +//////////////////////////////////////////////////////////// +User::User(const UserRingKind& ring_kind, const Char* user_name) : mUserRing(ring_kind) { + rt_copy_memory((VoidPtr) user_name, this->mUserName, rt_string_len(user_name)); +} + +//////////////////////////////////////////////////////////// +/// @brief User destructor class. +//////////////////////////////////////////////////////////// +User::~User() = default; + +Bool User::Save(const UserPublicKey password_to_fill) noexcept { + if (!password_to_fill || *password_to_fill == 0) return No; + + SizeT len = rt_string_len(password_to_fill); - kout << "cred_construct_token: Hashing user password...\r"; + UserPublicKey password = new UserPublicKeyType[len]; - for (SizeT i_pass = 0UL; i_pass < length; ++i_pass) - { - Char cur_chr = in_password[i_pass]; + MUST_PASS(password); - if (cur_chr == 0) - break; + rt_set_memory(password, 0, len); - password[i_pass] = cur_chr | (user->IsStdUser() ? kStdUserType : kSuperUserType); - } + // fill data first, generate hash. + // return false on error. - kout << "cred_construct_token: Hashed user password.\r"; + rt_copy_memory((VoidPtr) password_to_fill, password, len); - return 0; - } - } // namespace Detail + if (!Detail::user_standard_token_generator(password, password_to_fill, this, len)) { + delete[] password; + password = nullptr; - //////////////////////////////////////////////////////////// - /// @brief User ring constructor. - //////////////////////////////////////////////////////////// - User::User(const Int32& sel, const Char* user_name) - : mUserRing((UserRingKind)sel) - { - MUST_PASS(sel >= 0); - rt_copy_memory((VoidPtr)user_name, this->mUserName, rt_string_len(user_name)); - } + return No; + } - //////////////////////////////////////////////////////////// - /// @brief User ring constructor. - //////////////////////////////////////////////////////////// - User::User(const UserRingKind& ring_kind, const Char* user_name) - : mUserRing(ring_kind) - { - rt_copy_memory((VoidPtr)user_name, this->mUserName, rt_string_len(user_name)); - } + // then store password. - //////////////////////////////////////////////////////////// - /// @brief User destructor class. - //////////////////////////////////////////////////////////// - User::~User() = default; + rt_copy_memory(password, this->mUserKey, rt_string_len(password_to_fill)); - Bool User::Save(const UserPublicKey password_to_fill) noexcept - { - if (!password_to_fill || - *password_to_fill == 0) - return No; + delete[] password; + password = nullptr; - SizeT len = rt_string_len(password_to_fill); + kout << "User::Save: Saved password successfully...\r"; - UserPublicKey password = new UserPublicKeyType[len]; + return Yes; +} - MUST_PASS(password); +Bool User::Matches(const UserPublicKey password_to_fill) noexcept { + if (!password_to_fill || *password_to_fill) return No; - rt_set_memory(password, 0, len); + SizeT len = rt_string_len(password_to_fill); - // fill data first, generate hash. - // return false on error. - - rt_copy_memory((VoidPtr)password_to_fill, password, len); - - if (!Detail::cred_construct_token(password, password_to_fill, this, len)) - { - delete[] password; - password = nullptr; - - return No; - } - - // then store password. - - rt_copy_memory(password, this->mUserKey, rt_string_len(password_to_fill)); - - delete[] password; - password = nullptr; - - kout << "User::Save: Saved password successfully...\r"; - - return Yes; - } - - Bool User::Matches(const UserPublicKey password_to_fill) noexcept - { - if (!password_to_fill || - *password_to_fill) - return No; - - SizeT len = rt_string_len(password_to_fill); - - Char* password = new Char[len]; - MUST_PASS(password); + Char* password = new Char[len]; + MUST_PASS(password); - // fill data first, generate hash. - // return false on error. + // fill data first, generate hash. + // return false on error. - rt_copy_memory((VoidPtr)password_to_fill, password, len); + rt_copy_memory((VoidPtr) password_to_fill, password, len); - if (!Detail::cred_construct_token(password, password_to_fill, this, len)) - { - delete[] password; - password = nullptr; + if (!Detail::user_standard_token_generator(password, password_to_fill, this, len)) { + delete[] password; + password = nullptr; - return No; - } + return No; + } - kout << "User::Matches: Validating hashed passwords...\r"; + kout << "User::Matches: Validating hashed passwords...\r"; - // now check if the password matches. - if (rt_string_cmp(password, this->mUserKey, rt_string_len(this->mUserKey)) == 0) - { - kout << "User::Matches: Password matches.\r"; - return Yes; - } + // now check if the password matches. + if (rt_string_cmp(password, this->mUserKey, rt_string_len(this->mUserKey)) == 0) { + kout << "User::Matches: Password matches.\r"; + return Yes; + } - kout << "User::Matches: Password doesn't match.\r"; - return No; - } + kout << "User::Matches: Password doesn't match.\r"; + return No; +} - Bool User::operator==(const User& lhs) - { - return lhs.mUserRing == this->mUserRing; - } +Bool User::operator==(const User& lhs) { + return lhs.mUserRing == this->mUserRing; +} - Bool User::operator!=(const User& lhs) - { - return lhs.mUserRing != this->mUserRing; - } +Bool User::operator!=(const User& lhs) { + return lhs.mUserRing != this->mUserRing; +} - //////////////////////////////////////////////////////////// - /// @brief Returns the user's name. - //////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////// +/// @brief Returns the user's name. +//////////////////////////////////////////////////////////// - Char* User::Name() noexcept - { - return this->mUserName; - } +Char* User::Name() noexcept { + return this->mUserName; +} - //////////////////////////////////////////////////////////// - /// @brief Returns the user's ring. - /// @return The king of ring the user is attached to. - //////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////// +/// @brief Returns the user's ring. +/// @return The king of ring the user is attached to. +//////////////////////////////////////////////////////////// - const UserRingKind& User::Ring() noexcept - { - return this->mUserRing; - } +const UserRingKind& User::Ring() noexcept { + return this->mUserRing; +} - Bool User::IsStdUser() noexcept - { - return this->Ring() == UserRingKind::kRingStdUser; - } +Bool User::IsStdUser() noexcept { + return this->Ring() == UserRingKind::kRingStdUser; +} - Bool User::IsSuperUser() noexcept - { - return this->Ring() == UserRingKind::kRingSuperUser; - } -} // namespace Kernel +Bool User::IsSuperUser() noexcept { + return this->Ring() == UserRingKind::kRingSuperUser; +} +} // namespace Kernel |
