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 |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 17 | #include "users.hpp" |
| 18 | |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 19 | #include <sdbusplus/bus.hpp> |
| 20 | #include <sdbusplus/server/object.hpp> |
Richard Marian Thomaiyar | 9164fd9 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 21 | #include <xyz/openbmc_project/User/AccountPolicy/server.hpp> |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 22 | #include <xyz/openbmc_project/User/Manager/server.hpp> |
| 23 | |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 24 | #include <unordered_map> |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 25 | #include <variant> |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 26 | |
| 27 | namespace phosphor |
| 28 | { |
| 29 | namespace user |
| 30 | { |
| 31 | |
| 32 | using UserMgrIface = sdbusplus::xyz::openbmc_project::User::server::Manager; |
| 33 | using UserSSHLists = |
| 34 | std::pair<std::vector<std::string>, std::vector<std::string>>; |
Richard Marian Thomaiyar | 9164fd9 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 35 | using AccountPolicyIface = |
| 36 | sdbusplus::xyz::openbmc_project::User::server::AccountPolicy; |
| 37 | |
Ratan Gupta | 1af1223 | 2018-11-03 00:35:38 +0530 | [diff] [blame] | 38 | using Ifaces = |
| 39 | sdbusplus::server::object::object<UserMgrIface, AccountPolicyIface>; |
| 40 | |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 41 | using Privilege = std::string; |
| 42 | using GroupList = std::vector<std::string>; |
| 43 | using UserEnabled = bool; |
| 44 | using PropertyName = std::string; |
Ravi Teja | 5fe724a | 2019-05-07 05:14:42 -0500 | [diff] [blame] | 45 | using ServiceEnabled = bool; |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 46 | |
| 47 | using UserInfo = std::variant<Privilege, GroupList, UserEnabled>; |
| 48 | using UserInfoMap = std::map<PropertyName, UserInfo>; |
| 49 | |
| 50 | using DbusUserObjPath = sdbusplus::message::object_path; |
| 51 | |
Patrick Williams | fdf0937 | 2020-05-13 18:01:45 -0500 | [diff] [blame] | 52 | using DbusUserPropVariant = std::variant<Privilege, ServiceEnabled>; |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 53 | |
| 54 | using DbusUserObjProperties = |
| 55 | std::vector<std::pair<PropertyName, DbusUserPropVariant>>; |
| 56 | |
| 57 | using Interface = std::string; |
| 58 | |
| 59 | using DbusUserObjValue = std::map<Interface, DbusUserObjProperties>; |
| 60 | |
| 61 | using DbusUserObj = std::map<DbusUserObjPath, DbusUserObjValue>; |
| 62 | |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 63 | /** @class UserMgr |
| 64 | * @brief Responsible for managing user accounts over the D-Bus interface. |
| 65 | */ |
Ratan Gupta | 1af1223 | 2018-11-03 00:35:38 +0530 | [diff] [blame] | 66 | class UserMgr : public Ifaces |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 67 | { |
| 68 | public: |
| 69 | UserMgr() = delete; |
| 70 | ~UserMgr() = default; |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 71 | UserMgr(const UserMgr&) = delete; |
| 72 | UserMgr& operator=(const UserMgr&) = delete; |
| 73 | UserMgr(UserMgr&&) = delete; |
| 74 | UserMgr& operator=(UserMgr&&) = delete; |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 75 | |
| 76 | /** @brief Constructs UserMgr object. |
| 77 | * |
| 78 | * @param[in] bus - sdbusplus handler |
| 79 | * @param[in] path - D-Bus path |
| 80 | */ |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 81 | UserMgr(sdbusplus::bus::bus& bus, const char* path); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 82 | |
| 83 | /** @brief create user method. |
| 84 | * This method creates a new user as requested |
| 85 | * |
| 86 | * @param[in] userName - Name of the user which has to be created |
| 87 | * @param[in] groupNames - Group names list, to which user has to be added. |
| 88 | * @param[in] priv - Privilege of the user. |
| 89 | * @param[in] enabled - State of the user enabled / disabled. |
| 90 | */ |
| 91 | void createUser(std::string userName, std::vector<std::string> groupNames, |
| 92 | std::string priv, bool enabled) override; |
| 93 | |
| 94 | /** @brief rename user method. |
| 95 | * This method renames the user as requested |
| 96 | * |
| 97 | * @param[in] userName - current name of the user |
| 98 | * @param[in] newUserName - new user name to which it has to be renamed. |
| 99 | */ |
| 100 | void renameUser(std::string userName, std::string newUserName) override; |
| 101 | |
| 102 | /** @brief delete user method. |
| 103 | * This method deletes the user as requested |
| 104 | * |
| 105 | * @param[in] userName - Name of the user which has to be deleted |
| 106 | */ |
| 107 | void deleteUser(std::string userName); |
| 108 | |
| 109 | /** @brief Update user groups & privilege. |
| 110 | * This method updates user groups & privilege |
| 111 | * |
| 112 | * @param[in] userName - user name, for which update is requested |
| 113 | * @param[in] groupName - Group to be updated.. |
| 114 | * @param[in] priv - Privilege to be updated. |
| 115 | */ |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 116 | void updateGroupsAndPriv(const std::string& userName, |
| 117 | const std::vector<std::string>& groups, |
| 118 | const std::string& priv); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 119 | |
| 120 | /** @brief Update user enabled state. |
| 121 | * This method enables / disables user |
| 122 | * |
| 123 | * @param[in] userName - user name, for which update is requested |
| 124 | * @param[in] enabled - enable / disable the user |
| 125 | */ |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 126 | void userEnable(const std::string& userName, bool enabled); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 127 | |
Richard Marian Thomaiyar | 9164fd9 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 128 | /** @brief update minimum password length requirement |
| 129 | * |
| 130 | * @param[in] val - minimum password length |
| 131 | * @return - minimum password length |
| 132 | */ |
| 133 | uint8_t minPasswordLength(uint8_t val) override; |
| 134 | |
| 135 | /** @brief update old password history count |
| 136 | * |
| 137 | * @param[in] val - number of times old passwords has to be avoided |
| 138 | * @return - number of times old password has to be avoided |
| 139 | */ |
| 140 | uint8_t rememberOldPasswordTimes(uint8_t val) override; |
| 141 | |
| 142 | /** @brief update maximum number of failed login attempt before locked |
| 143 | * out. |
| 144 | * |
| 145 | * @param[in] val - number of allowed attempt |
| 146 | * @return - number of allowed attempt |
| 147 | */ |
| 148 | uint16_t maxLoginAttemptBeforeLockout(uint16_t val) override; |
| 149 | |
| 150 | /** @brief update timeout to unlock the account |
| 151 | * |
| 152 | * @param[in] val - value in seconds |
| 153 | * @return - value in seconds |
| 154 | */ |
| 155 | uint32_t accountUnlockTimeout(uint32_t val) override; |
| 156 | |
Richard Marian Thomaiyar | c704519 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 157 | /** @brief lists user locked state for failed attempt |
| 158 | * |
| 159 | * @param[in] - user name |
| 160 | * @return - true / false indicating user locked / un-locked |
| 161 | **/ |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 162 | virtual bool userLockedForFailedAttempt(const std::string& userName); |
Richard Marian Thomaiyar | c704519 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 163 | |
| 164 | /** @brief lists user locked state for failed attempt |
| 165 | * |
| 166 | * @param[in]: user name |
| 167 | * @param[in]: value - false -unlock user account, true - no action taken |
| 168 | **/ |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 169 | bool userLockedForFailedAttempt(const std::string& userName, |
| 170 | const bool& value); |
Richard Marian Thomaiyar | c704519 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 171 | |
Joseph Reynolds | 3ab6cc2 | 2020-03-03 14:09:03 -0600 | [diff] [blame] | 172 | /** @brief shows if the user's password is expired |
| 173 | * |
| 174 | * @param[in]: user name |
| 175 | * @return - true / false indicating user password expired |
| 176 | **/ |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 177 | virtual bool userPasswordExpired(const std::string& userName); |
Joseph Reynolds | 3ab6cc2 | 2020-03-03 14:09:03 -0600 | [diff] [blame] | 178 | |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 179 | /** @brief returns user info |
| 180 | * Checks if user is local user, then returns map of properties of user. |
| 181 | * like user privilege, list of user groups, user enabled state and user |
| 182 | * locked state. If its not local user, then it checks if its a ldap user, |
| 183 | * then it gets the privilege mapping of the LDAP group. |
| 184 | * |
| 185 | * @param[in] - user name |
| 186 | * @return - map of user properties |
| 187 | **/ |
| 188 | UserInfoMap getUserInfo(std::string userName) override; |
| 189 | |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 190 | private: |
| 191 | /** @brief sdbusplus handler */ |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 192 | sdbusplus::bus::bus& bus; |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 193 | |
| 194 | /** @brief object path */ |
| 195 | const std::string path; |
| 196 | |
| 197 | /** @brief privilege manager container */ |
| 198 | std::vector<std::string> privMgr = {"priv-admin", "priv-operator", |
Richard Marian Thomaiyar | 32be296 | 2019-11-08 17:21:53 +0530 | [diff] [blame] | 199 | "priv-user", "priv-noaccess"}; |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 200 | |
| 201 | /** @brief groups manager container */ |
| 202 | std::vector<std::string> groupsMgr = {"web", "redfish", "ipmi", "ssh"}; |
| 203 | |
| 204 | /** @brief map container to hold users object */ |
| 205 | using UserName = std::string; |
| 206 | std::unordered_map<UserName, std::unique_ptr<phosphor::user::Users>> |
| 207 | usersList; |
| 208 | |
| 209 | /** @brief get users in group |
| 210 | * method to get group user list |
| 211 | * |
| 212 | * @param[in] groupName - group name |
| 213 | * |
| 214 | * @return userList - list of users in the group. |
| 215 | */ |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 216 | std::vector<std::string> getUsersInGroup(const std::string& groupName); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 217 | |
| 218 | /** @brief get user & SSH users list |
| 219 | * method to get the users and ssh users list. |
| 220 | * |
| 221 | *@return - vector of User & SSH user lists |
| 222 | */ |
| 223 | UserSSHLists getUserAndSshGrpList(void); |
| 224 | |
| 225 | /** @brief check for user presence |
| 226 | * method to check for user existence |
| 227 | * |
| 228 | * @param[in] userName - name of the user |
| 229 | * @return -true if user exists and false if not. |
| 230 | */ |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 231 | bool isUserExist(const std::string& userName); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 232 | |
| 233 | /** @brief check user exists |
| 234 | * method to check whether user exist, and throw if not. |
| 235 | * |
| 236 | * @param[in] userName - name of the user |
| 237 | */ |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 238 | void throwForUserDoesNotExist(const std::string& userName); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 239 | |
| 240 | /** @brief check user does not exist |
| 241 | * method to check whether does not exist, and throw if exists. |
| 242 | * |
| 243 | * @param[in] userName - name of the user |
| 244 | */ |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 245 | void throwForUserExists(const std::string& userName); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 246 | |
| 247 | /** @brief check user name constraints |
| 248 | * method to check user name constraints and throw if failed. |
| 249 | * |
| 250 | * @param[in] userName - name of the user |
| 251 | * @param[in] groupNames - user groups |
| 252 | */ |
| 253 | void |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 254 | throwForUserNameConstraints(const std::string& userName, |
| 255 | const std::vector<std::string>& groupNames); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 256 | |
| 257 | /** @brief check group user count |
| 258 | * method to check max group user count, and throw if limit reached |
| 259 | * |
| 260 | * @param[in] groupNames - group name |
| 261 | */ |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 262 | void throwForMaxGrpUserCount(const std::vector<std::string>& groupNames); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 263 | |
| 264 | /** @brief check for valid privielge |
| 265 | * method to check valid privilege, and throw if invalid |
| 266 | * |
| 267 | * @param[in] priv - privilege of the user |
| 268 | */ |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 269 | void throwForInvalidPrivilege(const std::string& priv); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 270 | |
| 271 | /** @brief check for valid groups |
| 272 | * method to check valid groups, and throw if invalid |
| 273 | * |
| 274 | * @param[in] groupNames - user groups |
| 275 | */ |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 276 | void throwForInvalidGroups(const std::vector<std::string>& groupName); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 277 | |
| 278 | /** @brief get user enabled state |
| 279 | * method to get user enabled state. |
| 280 | * |
| 281 | * @param[in] userName - name of the user |
| 282 | * @return - user enabled status (true/false) |
| 283 | */ |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 284 | bool isUserEnabled(const std::string& userName); |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 285 | |
| 286 | /** @brief initialize the user manager objects |
| 287 | * method to initialize the user manager objects accordingly |
| 288 | * |
| 289 | */ |
| 290 | void initUserObjects(void); |
| 291 | |
| 292 | /** @brief get IPMI user count |
| 293 | * method to get IPMI user count |
| 294 | * |
| 295 | * @return - returns user count |
| 296 | */ |
| 297 | size_t getIpmiUsersCount(void); |
Richard Marian Thomaiyar | 9164fd9 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 298 | |
| 299 | /** @brief get pam argument value |
| 300 | * method to get argument value from pam configuration |
| 301 | * |
| 302 | * @param[in] moduleName - name of the module from where arg has to be read |
| 303 | * @param[in] argName - argument name |
| 304 | * @param[out] argValue - argument value |
| 305 | * |
| 306 | * @return 0 - success state of the function |
| 307 | */ |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 308 | int getPamModuleArgValue(const std::string& moduleName, |
| 309 | const std::string& argName, std::string& argValue); |
Richard Marian Thomaiyar | 9164fd9 | 2018-06-13 16:51:00 +0530 | [diff] [blame] | 310 | |
| 311 | /** @brief set pam argument value |
| 312 | * method to set argument value in pam configuration |
| 313 | * |
| 314 | * @param[in] moduleName - name of the module in which argument value has |
| 315 | * to be set |
| 316 | * @param[in] argName - argument name |
| 317 | * @param[out] argValue - argument value |
| 318 | * |
| 319 | * @return 0 - success state of the function |
| 320 | */ |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 321 | int setPamModuleArgValue(const std::string& moduleName, |
| 322 | const std::string& argName, |
| 323 | const std::string& argValue); |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 324 | |
| 325 | /** @brief get service name |
| 326 | * method to get dbus service name |
| 327 | * |
| 328 | * @param[in] path - object path |
| 329 | * @param[in] intf - interface |
| 330 | * @return - service name |
| 331 | */ |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 332 | std::string getServiceName(std::string&& path, std::string&& intf); |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 333 | |
raviteja-b | 8cc4405 | 2019-02-27 23:29:36 -0600 | [diff] [blame] | 334 | protected: |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 335 | /** @brief get LDAP group name |
| 336 | * method to get LDAP group name for the given LDAP user |
| 337 | * |
| 338 | * @param[in] - userName |
| 339 | * @return - group name |
| 340 | */ |
Patrick Williams | 9638afb | 2021-02-22 17:16:24 -0600 | [diff] [blame] | 341 | virtual std::string getLdapGroupName(const std::string& userName); |
Ratan Gupta | aeaf941 | 2019-02-11 04:41:52 -0600 | [diff] [blame] | 342 | |
| 343 | /** @brief get privilege mapper object |
| 344 | * method to get dbus privilege mapper object |
| 345 | * |
| 346 | * @return - map of user object |
| 347 | */ |
raviteja-b | 8cc4405 | 2019-02-27 23:29:36 -0600 | [diff] [blame] | 348 | virtual DbusUserObj getPrivilegeMapperObject(void); |
| 349 | |
| 350 | friend class TestUserMgr; |
Richard Marian Thomaiyar | 9f630d9 | 2018-05-24 10:49:10 +0530 | [diff] [blame] | 351 | }; |
| 352 | |
| 353 | } // namespace user |
| 354 | } // namespace phosphor |