blob: aa408209208fbd3d66c55706101ff3bf20b49443 [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
58 private:
59 /** @brief sdbusplus handler */
60 sdbusplus::bus::bus& bus;
61
62 /** @brief object path */
63 const std::string& path;
Vishwanatha Subbannabdb298f2017-09-06 11:39:22 +053064
65 /** @brief User id extracted from object path */
66 const std::string user;
67
68 /** @brief Extracts crypto number from the shadow entry for user
69 *
70 * @param[in] spPwdp - sp_pwdp of struct spwd
71 */
72 static CryptAlgo getCryptField(char* spPwdp);
73
74 /** @brief Generates one-way hash based on salt and password
75 *
76 * @param[in] password - clear text password
77 * @param[in] salt - Combination of crypto method and salt
78 * Eg: $1$HELLO$, where in 1 is crypto method
79 * and HELLO is salt
80 */
81 static std::string generateHash(const std::string& password,
82 const std::string& salt);
83
84 /** @brief returns salt string with $ delimiter.
85 * Eg: If crypt is 1 and salt is HELLO, returns $1$HELLO$
86 *
87 * @param[in] crypt - Crypt number in string
88 * @param[in] salt - salt
89 */
90 static std::string getSaltString(const std::string& crypt,
91 const std::string& salt);
Vishwanatha Subbannad20225f2017-09-06 11:36:04 +053092};
93
94} // namespace user
95} // namespace phosphor