blob: ca8673fe5d2db03600126524efd0953ab03e57e8 [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{
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;
33
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),
41 bus(bus),
Vishwanatha Subbannabdb298f2017-09-06 11:39:22 +053042 path(path),
43 user(fs::path(path).filename())
Vishwanatha Subbannad20225f2017-09-06 11:36:04 +053044 {
45 // Do nothing
46 }
47
48 /** @brief user password set method. If this is called for
49 * a user ID that already has the password, the password
50 * would be updated, else password would be created.
51 * Since this needs an already authenticated session,
52 * old password is not needed.
53 *
54 * @param[in] newPassword - New password
55 */
56 void setPassword(std::string newPassword) override;
57
Vishwanatha Subbanna035a9692017-09-15 18:50:43 +053058
Vishwanatha Subbannad20225f2017-09-06 11:36:04 +053059 private:
60 /** @brief sdbusplus handler */
61 sdbusplus::bus::bus& bus;
62
63 /** @brief object path */
64 const std::string& path;
Vishwanatha Subbannabdb298f2017-09-06 11:39:22 +053065
66 /** @brief User id extracted from object path */
67 const std::string user;
68
Vishwanatha Subbanna070a3e42017-09-06 11:40:45 +053069 /** @brief Returns a random string from set [A-Za-z0-9./]
70 * of length size
71 *
72 * @param[in] numChars - length of string
73 */
74 static const std::string randomString(int length);
75
76 /** @brief Returns password hash created with crypt algo,
77 * salt and password
78 *
79 * @param[in] spPwdp - sp_pwdp of struct spwd
80 * @param[in] password - clear text password
81 * @param[in] salt - Random salt
82 */
83 std::string hashPassword(char* spPwdp,
84 const std::string& password,
85 const std::string& salt);
86
Vishwanatha Subbannabdb298f2017-09-06 11:39:22 +053087 /** @brief Extracts crypto number from the shadow entry for user
88 *
89 * @param[in] spPwdp - sp_pwdp of struct spwd
90 */
91 static CryptAlgo getCryptField(char* spPwdp);
92
93 /** @brief Generates one-way hash based on salt and password
94 *
95 * @param[in] password - clear text password
96 * @param[in] salt - Combination of crypto method and salt
97 * Eg: $1$HELLO$, where in 1 is crypto method
98 * and HELLO is salt
99 */
100 static std::string generateHash(const std::string& password,
101 const std::string& salt);
102
Vishwanatha Subbanna070a3e42017-09-06 11:40:45 +0530103 /** @brief Returns salt string with $ delimiter.
Vishwanatha Subbannabdb298f2017-09-06 11:39:22 +0530104 * Eg: If crypt is 1 and salt is HELLO, returns $1$HELLO$
105 *
106 * @param[in] crypt - Crypt number in string
107 * @param[in] salt - salt
108 */
109 static std::string getSaltString(const std::string& crypt,
110 const std::string& salt);
Vishwanatha Subbanna070a3e42017-09-06 11:40:45 +0530111
112 /** @brief Applies the password for a given user.
113 * Writes shadow entries into a temp file
114 *
115 * @param[in] shadowFile - shadow password file
Vishwanatha Subbanna070a3e42017-09-06 11:40:45 +0530116 * @param[in] password - clear text password
117 * @param[in] salt - salt
118 */
119 void applyPassword(const std::string& shadowFile,
Vishwanatha Subbanna070a3e42017-09-06 11:40:45 +0530120 const std::string& password,
121 const std::string& salt);
Vishwanatha Subbanna36218e62017-09-06 17:19:56 +0530122
123 /** @brief Wrapper for raising exception
124 *
125 * @param[in] errNo - errno
126 * @param[in] errMsg - Error message
127 */
128 void raiseException(int errNo,
129 const std::string& errMsg);
Vishwanatha Subbanna035a9692017-09-15 18:50:43 +0530130
131 /** @brief For enabling test cases */
132 friend class UserTest;
Vishwanatha Subbannad20225f2017-09-06 11:36:04 +0530133};
134
135} // namespace user
136} // namespace phosphor