blob: 2e57702e48a31018b1ac50f9cda3e2080301f0a7 [file] [log] [blame]
Vishwanatha Subbannad20225f2017-09-06 11:36:04 +05301#pragma once
2
Vishwanatha Subbannabdb298f2017-09-06 11:39:22 +05303#include <cstring>
4#include <experimental/filesystem>
Vishwanatha Subbannad20225f2017-09-06 11:36:04 +05305#include <sdbusplus/bus.hpp>
6#include <sdbusplus/server/object.hpp>
7#include <xyz/openbmc_project/User/Password/server.hpp>
8namespace phosphor
9{
10namespace user
11{
12
Vishwanatha Subbannabdb298f2017-09-06 11:39:22 +053013using CryptAlgo = std::string;
14
15namespace fs = std::experimental::filesystem;
Vishwanatha Subbannad20225f2017-09-06 11:36:04 +053016namespace Base = sdbusplus::xyz::openbmc_project::User::server;
17using 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 */
24class User : public Interface
25{
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053026 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 Subbannad20225f2017-09-06 11:36:04 +053033
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053034 /** @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 Subbannad20225f2017-09-06 11:36:04 +053045
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053046 /** @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 Subbannad20225f2017-09-06 11:36:04 +053055
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053056 private:
57 /** @brief sdbusplus handler */
58 sdbusplus::bus::bus& bus;
Vishwanatha Subbanna035a9692017-09-15 18:50:43 +053059
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053060 /** @brief object path */
61 const std::string& path;
Vishwanatha Subbannad20225f2017-09-06 11:36:04 +053062
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053063 /** @brief User id extracted from object path */
64 const std::string user;
Vishwanatha Subbannabdb298f2017-09-06 11:39:22 +053065
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053066 /** @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 Subbannabdb298f2017-09-06 11:39:22 +053072
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053073 /** @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 Subbanna070a3e42017-09-06 11:40:45 +053082
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053083 /** @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 Subbanna070a3e42017-09-06 11:40:45 +053088
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053089 /** @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 Subbannabdb298f2017-09-06 11:39:22 +053098
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053099 /** @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 Subbannabdb298f2017-09-06 11:39:22 +0530107
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530108 /** @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 Subbanna070a3e42017-09-06 11:40:45 +0530117
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530118 /** @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 Subbanna36218e62017-09-06 17:19:56 +0530124
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530125 /** @brief For enabling test cases */
126 friend class UserTest;
Vishwanatha Subbannad20225f2017-09-06 11:36:04 +0530127};
128
129} // namespace user
130} // namespace phosphor