Vishwanatha Subbanna | d20225f | 2017-09-06 11:36:04 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Vishwanatha Subbanna | bdb298f | 2017-09-06 11:39:22 +0530 | [diff] [blame] | 3 | #include <cstring> |
| 4 | #include <experimental/filesystem> |
Vishwanatha Subbanna | d20225f | 2017-09-06 11:36:04 +0530 | [diff] [blame] | 5 | #include <sdbusplus/bus.hpp> |
| 6 | #include <sdbusplus/server/object.hpp> |
| 7 | #include <xyz/openbmc_project/User/Password/server.hpp> |
| 8 | namespace phosphor |
| 9 | { |
| 10 | namespace user |
| 11 | { |
| 12 | |
Vishwanatha Subbanna | bdb298f | 2017-09-06 11:39:22 +0530 | [diff] [blame] | 13 | using CryptAlgo = std::string; |
| 14 | |
| 15 | namespace fs = std::experimental::filesystem; |
Vishwanatha Subbanna | d20225f | 2017-09-06 11:36:04 +0530 | [diff] [blame] | 16 | namespace Base = sdbusplus::xyz::openbmc_project::User::server; |
| 17 | using Interface = sdbusplus::server::object::object<Base::Password>; |
| 18 | |
| 19 | /** @class User |
| 20 | * @brief Responsible for managing a specific user account. |
| 21 | * It is implementing just the Password interface |
| 22 | * for now. |
| 23 | */ |
| 24 | class User : public Interface |
| 25 | { |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 26 | public: |
| 27 | User() = delete; |
| 28 | ~User() = default; |
| 29 | User(const User&) = delete; |
| 30 | User& operator=(const User&) = delete; |
| 31 | User(User&&) = delete; |
| 32 | User& operator=(User&&) = delete; |
Vishwanatha Subbanna | d20225f | 2017-09-06 11:36:04 +0530 | [diff] [blame] | 33 | |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 34 | /** @brief Constructs User object. |
| 35 | * |
| 36 | * @param[in] bus - sdbusplus handler |
| 37 | * @param[in] path - D-Bus path |
| 38 | */ |
| 39 | User(sdbusplus::bus::bus& bus, const char* path) : |
| 40 | Interface(bus, path), bus(bus), path(path), |
| 41 | user(fs::path(path).filename()) |
| 42 | { |
| 43 | // Do nothing |
| 44 | } |
Vishwanatha Subbanna | d20225f | 2017-09-06 11:36:04 +0530 | [diff] [blame] | 45 | |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 46 | /** @brief user password set method. If this is called for |
| 47 | * a user ID that already has the password, the password |
| 48 | * would be updated, else password would be created. |
| 49 | * Since this needs an already authenticated session, |
| 50 | * old password is not needed. |
| 51 | * |
| 52 | * @param[in] newPassword - New password |
| 53 | */ |
| 54 | void setPassword(std::string newPassword) override; |
Vishwanatha Subbanna | d20225f | 2017-09-06 11:36:04 +0530 | [diff] [blame] | 55 | |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 56 | private: |
| 57 | /** @brief sdbusplus handler */ |
| 58 | sdbusplus::bus::bus& bus; |
Vishwanatha Subbanna | 035a969 | 2017-09-15 18:50:43 +0530 | [diff] [blame] | 59 | |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 60 | /** @brief object path */ |
| 61 | const std::string& path; |
Vishwanatha Subbanna | d20225f | 2017-09-06 11:36:04 +0530 | [diff] [blame] | 62 | |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 63 | /** @brief User id extracted from object path */ |
| 64 | const std::string user; |
Vishwanatha Subbanna | bdb298f | 2017-09-06 11:39:22 +0530 | [diff] [blame] | 65 | |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 66 | /** @brief Returns a random string from set [A-Za-z0-9./] |
| 67 | * of length size |
| 68 | * |
| 69 | * @param[in] numChars - length of string |
| 70 | */ |
| 71 | static const std::string randomString(int length); |
Vishwanatha Subbanna | bdb298f | 2017-09-06 11:39:22 +0530 | [diff] [blame] | 72 | |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 73 | /** @brief Returns password hash created with crypt algo, |
| 74 | * salt and password |
| 75 | * |
| 76 | * @param[in] spPwdp - sp_pwdp of struct spwd |
| 77 | * @param[in] password - clear text password |
| 78 | * @param[in] salt - Random salt |
| 79 | */ |
| 80 | std::string hashPassword(char* spPwdp, const std::string& password, |
| 81 | const std::string& salt); |
Vishwanatha Subbanna | 070a3e4 | 2017-09-06 11:40:45 +0530 | [diff] [blame] | 82 | |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 83 | /** @brief Extracts crypto number from the shadow entry for user |
| 84 | * |
| 85 | * @param[in] spPwdp - sp_pwdp of struct spwd |
| 86 | */ |
| 87 | static CryptAlgo getCryptField(char* spPwdp); |
Vishwanatha Subbanna | 070a3e4 | 2017-09-06 11:40:45 +0530 | [diff] [blame] | 88 | |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 89 | /** @brief Generates one-way hash based on salt and password |
| 90 | * |
| 91 | * @param[in] password - clear text password |
| 92 | * @param[in] salt - Combination of crypto method and salt |
| 93 | * Eg: $1$HELLO$, where in 1 is crypto method |
| 94 | * and HELLO is salt |
| 95 | */ |
| 96 | static std::string generateHash(const std::string& password, |
| 97 | const std::string& salt); |
Vishwanatha Subbanna | bdb298f | 2017-09-06 11:39:22 +0530 | [diff] [blame] | 98 | |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 99 | /** @brief Returns salt string with $ delimiter. |
| 100 | * Eg: If crypt is 1 and salt is HELLO, returns $1$HELLO$ |
| 101 | * |
| 102 | * @param[in] crypt - Crypt number in string |
| 103 | * @param[in] salt - salt |
| 104 | */ |
| 105 | static std::string getSaltString(const std::string& crypt, |
| 106 | const std::string& salt); |
Vishwanatha Subbanna | bdb298f | 2017-09-06 11:39:22 +0530 | [diff] [blame] | 107 | |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 108 | /** @brief Applies the password for a given user. |
| 109 | * Writes shadow entries into a temp file |
| 110 | * |
| 111 | * @param[in] shadowFile - shadow password file |
| 112 | * @param[in] password - clear text password |
| 113 | * @param[in] salt - salt |
| 114 | */ |
| 115 | void applyPassword(const std::string& shadowFile, |
| 116 | const std::string& password, const std::string& salt); |
Vishwanatha Subbanna | 070a3e4 | 2017-09-06 11:40:45 +0530 | [diff] [blame] | 117 | |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 118 | /** @brief Wrapper for raising exception |
| 119 | * |
| 120 | * @param[in] errNo - errno |
| 121 | * @param[in] errMsg - Error message |
| 122 | */ |
| 123 | void raiseException(int errNo, const std::string& errMsg); |
Vishwanatha Subbanna | 36218e6 | 2017-09-06 17:19:56 +0530 | [diff] [blame] | 124 | |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 125 | /** @brief For enabling test cases */ |
| 126 | friend class UserTest; |
Vishwanatha Subbanna | d20225f | 2017-09-06 11:36:04 +0530 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | } // namespace user |
| 130 | } // namespace phosphor |