Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame^] | 1 | /* |
| 2 | // Copyright (c) 2018 Intel Corporation |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | */ |
| 16 | #pragma once |
| 17 | #include <sdbusplus/bus.hpp> |
| 18 | #include <sdbusplus/server/object.hpp> |
| 19 | #include <xyz/openbmc_project/User/Manager/server.hpp> |
| 20 | #include <unordered_map> |
| 21 | #include "users.hpp" |
| 22 | |
| 23 | namespace phosphor |
| 24 | { |
| 25 | namespace user |
| 26 | { |
| 27 | |
| 28 | using UserMgrIface = sdbusplus::xyz::openbmc_project::User::server::Manager; |
| 29 | using UserSSHLists = |
| 30 | std::pair<std::vector<std::string>, std::vector<std::string>>; |
| 31 | /** @class UserMgr |
| 32 | * @brief Responsible for managing user accounts over the D-Bus interface. |
| 33 | */ |
| 34 | class UserMgr : public UserMgrIface |
| 35 | { |
| 36 | public: |
| 37 | UserMgr() = delete; |
| 38 | ~UserMgr() = default; |
| 39 | UserMgr(const UserMgr &) = delete; |
| 40 | UserMgr &operator=(const UserMgr &) = delete; |
| 41 | UserMgr(UserMgr &&) = delete; |
| 42 | UserMgr &operator=(UserMgr &&) = delete; |
| 43 | |
| 44 | /** @brief Constructs UserMgr object. |
| 45 | * |
| 46 | * @param[in] bus - sdbusplus handler |
| 47 | * @param[in] path - D-Bus path |
| 48 | */ |
| 49 | UserMgr(sdbusplus::bus::bus &bus, const char *path); |
| 50 | |
| 51 | /** @brief create user method. |
| 52 | * This method creates a new user as requested |
| 53 | * |
| 54 | * @param[in] userName - Name of the user which has to be created |
| 55 | * @param[in] groupNames - Group names list, to which user has to be added. |
| 56 | * @param[in] priv - Privilege of the user. |
| 57 | * @param[in] enabled - State of the user enabled / disabled. |
| 58 | */ |
| 59 | void createUser(std::string userName, std::vector<std::string> groupNames, |
| 60 | std::string priv, bool enabled) override; |
| 61 | |
| 62 | /** @brief rename user method. |
| 63 | * This method renames the user as requested |
| 64 | * |
| 65 | * @param[in] userName - current name of the user |
| 66 | * @param[in] newUserName - new user name to which it has to be renamed. |
| 67 | */ |
| 68 | void renameUser(std::string userName, std::string newUserName) override; |
| 69 | |
| 70 | /** @brief delete user method. |
| 71 | * This method deletes the user as requested |
| 72 | * |
| 73 | * @param[in] userName - Name of the user which has to be deleted |
| 74 | */ |
| 75 | void deleteUser(std::string userName); |
| 76 | |
| 77 | /** @brief Update user groups & privilege. |
| 78 | * This method updates user groups & privilege |
| 79 | * |
| 80 | * @param[in] userName - user name, for which update is requested |
| 81 | * @param[in] groupName - Group to be updated.. |
| 82 | * @param[in] priv - Privilege to be updated. |
| 83 | */ |
| 84 | void updateGroupsAndPriv(const std::string &userName, |
| 85 | const std::vector<std::string> &groups, |
| 86 | const std::string &priv); |
| 87 | |
| 88 | /** @brief Update user enabled state. |
| 89 | * This method enables / disables user |
| 90 | * |
| 91 | * @param[in] userName - user name, for which update is requested |
| 92 | * @param[in] enabled - enable / disable the user |
| 93 | */ |
| 94 | void userEnable(const std::string &userName, bool enabled); |
| 95 | |
| 96 | private: |
| 97 | /** @brief sdbusplus handler */ |
| 98 | sdbusplus::bus::bus &bus; |
| 99 | |
| 100 | /** @brief object path */ |
| 101 | const std::string path; |
| 102 | |
| 103 | /** @brief privilege manager container */ |
| 104 | std::vector<std::string> privMgr = {"priv-admin", "priv-operator", |
| 105 | "priv-user", "priv-callback"}; |
| 106 | |
| 107 | /** @brief groups manager container */ |
| 108 | std::vector<std::string> groupsMgr = {"web", "redfish", "ipmi", "ssh"}; |
| 109 | |
| 110 | /** @brief map container to hold users object */ |
| 111 | using UserName = std::string; |
| 112 | std::unordered_map<UserName, std::unique_ptr<phosphor::user::Users>> |
| 113 | usersList; |
| 114 | |
| 115 | /** @brief get users in group |
| 116 | * method to get group user list |
| 117 | * |
| 118 | * @param[in] groupName - group name |
| 119 | * |
| 120 | * @return userList - list of users in the group. |
| 121 | */ |
| 122 | std::vector<std::string> getUsersInGroup(const std::string &groupName); |
| 123 | |
| 124 | /** @brief get user & SSH users list |
| 125 | * method to get the users and ssh users list. |
| 126 | * |
| 127 | *@return - vector of User & SSH user lists |
| 128 | */ |
| 129 | UserSSHLists getUserAndSshGrpList(void); |
| 130 | |
| 131 | /** @brief check for user presence |
| 132 | * method to check for user existence |
| 133 | * |
| 134 | * @param[in] userName - name of the user |
| 135 | * @return -true if user exists and false if not. |
| 136 | */ |
| 137 | bool isUserExist(const std::string &userName); |
| 138 | |
| 139 | /** @brief check user exists |
| 140 | * method to check whether user exist, and throw if not. |
| 141 | * |
| 142 | * @param[in] userName - name of the user |
| 143 | */ |
| 144 | void throwForUserDoesNotExist(const std::string &userName); |
| 145 | |
| 146 | /** @brief check user does not exist |
| 147 | * method to check whether does not exist, and throw if exists. |
| 148 | * |
| 149 | * @param[in] userName - name of the user |
| 150 | */ |
| 151 | void throwForUserExists(const std::string &userName); |
| 152 | |
| 153 | /** @brief check user name constraints |
| 154 | * method to check user name constraints and throw if failed. |
| 155 | * |
| 156 | * @param[in] userName - name of the user |
| 157 | * @param[in] groupNames - user groups |
| 158 | */ |
| 159 | void |
| 160 | throwForUserNameConstraints(const std::string &userName, |
| 161 | const std::vector<std::string> &groupNames); |
| 162 | |
| 163 | /** @brief check group user count |
| 164 | * method to check max group user count, and throw if limit reached |
| 165 | * |
| 166 | * @param[in] groupNames - group name |
| 167 | */ |
| 168 | void throwForMaxGrpUserCount(const std::vector<std::string> &groupNames); |
| 169 | |
| 170 | /** @brief check for valid privielge |
| 171 | * method to check valid privilege, and throw if invalid |
| 172 | * |
| 173 | * @param[in] priv - privilege of the user |
| 174 | */ |
| 175 | void throwForInvalidPrivilege(const std::string &priv); |
| 176 | |
| 177 | /** @brief check for valid groups |
| 178 | * method to check valid groups, and throw if invalid |
| 179 | * |
| 180 | * @param[in] groupNames - user groups |
| 181 | */ |
| 182 | void throwForInvalidGroups(const std::vector<std::string> &groupName); |
| 183 | |
| 184 | /** @brief get user enabled state |
| 185 | * method to get user enabled state. |
| 186 | * |
| 187 | * @param[in] userName - name of the user |
| 188 | * @return - user enabled status (true/false) |
| 189 | */ |
| 190 | bool isUserEnabled(const std::string &userName); |
| 191 | |
| 192 | /** @brief initialize the user manager objects |
| 193 | * method to initialize the user manager objects accordingly |
| 194 | * |
| 195 | */ |
| 196 | void initUserObjects(void); |
| 197 | |
| 198 | /** @brief get IPMI user count |
| 199 | * method to get IPMI user count |
| 200 | * |
| 201 | * @return - returns user count |
| 202 | */ |
| 203 | size_t getIpmiUsersCount(void); |
| 204 | }; |
| 205 | |
| 206 | } // namespace user |
| 207 | } // namespace phosphor |