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