Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +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 "user_layer.hpp" |
| 18 | |
William A. Kennington III | 194375f | 2018-12-14 02:14:33 -0800 | [diff] [blame] | 19 | #include <ipmid/api.h> |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 20 | |
| 21 | #include <boost/interprocess/sync/file_lock.hpp> |
| 22 | #include <boost/interprocess/sync/named_recursive_mutex.hpp> |
| 23 | #include <cstdint> |
| 24 | #include <ctime> |
| 25 | #include <sdbusplus/bus.hpp> |
| 26 | |
| 27 | namespace ipmi |
| 28 | { |
| 29 | |
| 30 | using DbusUserPropVariant = |
| 31 | sdbusplus::message::variant<std::vector<std::string>, std::string, bool>; |
| 32 | |
| 33 | using DbusUserObjPath = sdbusplus::message::object_path; |
| 34 | |
| 35 | using DbusUserObjProperties = |
| 36 | std::vector<std::pair<std::string, DbusUserPropVariant>>; |
| 37 | |
| 38 | using DbusUserObjValue = std::map<std::string, DbusUserObjProperties>; |
| 39 | |
Richard Marian Thomaiyar | 6e1ba9e | 2018-11-29 06:29:21 +0530 | [diff] [blame] | 40 | /** |
| 41 | * @enum User update events. |
| 42 | */ |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 43 | enum class UserUpdateEvent |
| 44 | { |
| 45 | reservedEvent, |
| 46 | userCreated, |
| 47 | userDeleted, |
| 48 | userRenamed, |
| 49 | userGrpUpdated, |
| 50 | userPrivUpdated, |
| 51 | userStateUpdated |
| 52 | }; |
| 53 | |
Richard Marian Thomaiyar | 6e1ba9e | 2018-11-29 06:29:21 +0530 | [diff] [blame] | 54 | /** @struct UserPrivAccess |
| 55 | * |
| 56 | * Structure for user privilege access (refer spec sec 22.22) |
| 57 | */ |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 58 | struct UserPrivAccess |
| 59 | { |
| 60 | uint8_t privilege; |
| 61 | bool ipmiEnabled; |
| 62 | bool linkAuthEnabled; |
| 63 | bool accessCallback; |
| 64 | }; |
| 65 | |
Richard Marian Thomaiyar | 6e1ba9e | 2018-11-29 06:29:21 +0530 | [diff] [blame] | 66 | /** @struct UserInfo |
| 67 | * |
| 68 | * Structure for user related information |
| 69 | */ |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 70 | struct UserInfo |
| 71 | { |
| 72 | uint8_t userName[ipmiMaxUserName]; |
| 73 | UserPrivAccess userPrivAccess[ipmiMaxChannels]; |
| 74 | bool userEnabled; |
| 75 | bool userInSystem; |
| 76 | bool fixedUserName; |
| 77 | }; |
| 78 | |
Richard Marian Thomaiyar | 6e1ba9e | 2018-11-29 06:29:21 +0530 | [diff] [blame] | 79 | /** @struct UsersTbl |
| 80 | * |
| 81 | * Structure for array of user related information |
| 82 | */ |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 83 | struct UsersTbl |
| 84 | { |
| 85 | //+1 to map with UserId directly. UserId 0 is reserved. |
| 86 | UserInfo user[ipmiMaxUsers + 1]; |
| 87 | }; |
| 88 | |
| 89 | class UserAccess; |
| 90 | |
| 91 | UserAccess& getUserAccessObject(); |
| 92 | |
| 93 | class UserAccess |
| 94 | { |
| 95 | public: |
| 96 | UserAccess(const UserAccess&) = delete; |
| 97 | UserAccess& operator=(const UserAccess&) = delete; |
| 98 | UserAccess(UserAccess&&) = delete; |
| 99 | UserAccess& operator=(UserAccess&&) = delete; |
| 100 | |
| 101 | ~UserAccess(); |
| 102 | UserAccess(); |
| 103 | |
| 104 | /** @brief determines valid channel |
| 105 | * |
| 106 | * @param[in] chNum - channel number |
| 107 | * |
| 108 | * @return true if valid, false otherwise |
| 109 | */ |
Richard Marian Thomaiyar | a45cb34 | 2018-12-03 15:08:59 +0530 | [diff] [blame] | 110 | static bool isValidChannel(const uint8_t chNum); |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 111 | |
| 112 | /** @brief determines valid userId |
| 113 | * |
| 114 | * @param[in] userId - user id |
| 115 | * |
| 116 | * @return true if valid, false otherwise |
| 117 | */ |
Richard Marian Thomaiyar | a45cb34 | 2018-12-03 15:08:59 +0530 | [diff] [blame] | 118 | static bool isValidUserId(const uint8_t userId); |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 119 | |
| 120 | /** @brief determines valid user privilege |
| 121 | * |
| 122 | * @param[in] priv - Privilege |
| 123 | * |
| 124 | * @return true if valid, false otherwise |
| 125 | */ |
Richard Marian Thomaiyar | a45cb34 | 2018-12-03 15:08:59 +0530 | [diff] [blame] | 126 | static bool isValidPrivilege(const uint8_t priv); |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 127 | |
| 128 | /** @brief determines sync index to be mapped with common-user-management |
| 129 | * |
| 130 | * @return Index which will be used as sync index |
| 131 | */ |
| 132 | static uint8_t getUsrMgmtSyncIndex(); |
| 133 | |
| 134 | /** @brief Converts system privilege to IPMI privilege |
| 135 | * |
| 136 | * @param[in] value - Privilege in string |
| 137 | * |
| 138 | * @return CommandPrivilege - IPMI privilege type |
| 139 | */ |
| 140 | static CommandPrivilege convertToIPMIPrivilege(const std::string& value); |
| 141 | |
| 142 | /** @brief Converts IPMI privilege to system privilege |
| 143 | * |
| 144 | * @param[in] value - IPMI privilege |
| 145 | * |
| 146 | * @return System privilege in string |
| 147 | */ |
| 148 | static std::string convertToSystemPrivilege(const CommandPrivilege& value); |
| 149 | |
| 150 | /** @brief determines whether user name is valid |
| 151 | * |
| 152 | * @param[in] userNameInChar - user name |
| 153 | * |
| 154 | * @return true if valid, false otherwise |
| 155 | */ |
| 156 | bool isValidUserName(const char* userNameInChar); |
| 157 | |
| 158 | /** @brief provides user id of the user |
| 159 | * |
| 160 | * @param[in] userName - user name |
| 161 | * |
| 162 | * @return user id of the user, else invalid user id (0xFF), if user not |
| 163 | * found |
| 164 | */ |
| 165 | uint8_t getUserId(const std::string& userName); |
| 166 | |
| 167 | /** @brief provides user information |
| 168 | * |
| 169 | * @param[in] userId - user id |
| 170 | * |
| 171 | * @return UserInfo for the specified user id |
| 172 | */ |
Richard Marian Thomaiyar | a45cb34 | 2018-12-03 15:08:59 +0530 | [diff] [blame] | 173 | UserInfo* getUserInfo(const uint8_t userId); |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 174 | |
| 175 | /** @brief sets user information |
| 176 | * |
| 177 | * @param[in] userId - user id |
| 178 | * @param[in] userInfo - user information |
| 179 | * |
| 180 | */ |
Richard Marian Thomaiyar | a45cb34 | 2018-12-03 15:08:59 +0530 | [diff] [blame] | 181 | void setUserInfo(const uint8_t userId, UserInfo* userInfo); |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 182 | |
| 183 | /** @brief provides user name |
| 184 | * |
| 185 | * @param[in] userId - user id |
| 186 | * @param[out] userName - user name |
| 187 | * |
| 188 | * @return IPMI_CC_OK for success, others for failure. |
| 189 | */ |
Richard Marian Thomaiyar | a45cb34 | 2018-12-03 15:08:59 +0530 | [diff] [blame] | 190 | ipmi_ret_t getUserName(const uint8_t userId, std::string& userName); |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 191 | |
| 192 | /** @brief to set user name |
| 193 | * |
| 194 | * @param[in] userId - user id |
| 195 | * @param[in] userNameInChar - user name |
| 196 | * |
| 197 | * @return IPMI_CC_OK for success, others for failure. |
| 198 | */ |
Richard Marian Thomaiyar | a45cb34 | 2018-12-03 15:08:59 +0530 | [diff] [blame] | 199 | ipmi_ret_t setUserName(const uint8_t userId, const char* userNameInChar); |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 200 | |
Richard Marian Thomaiyar | 282e79b | 2018-11-13 19:00:58 +0530 | [diff] [blame] | 201 | /** @brief to set user enabled state |
| 202 | * |
| 203 | * @param[in] userId - user id |
| 204 | * @param[in] enabledState - enabled state of the user |
| 205 | * |
| 206 | * @return IPMI_CC_OK for success, others for failure. |
| 207 | */ |
Richard Marian Thomaiyar | a45cb34 | 2018-12-03 15:08:59 +0530 | [diff] [blame] | 208 | ipmi_ret_t setUserEnabledState(const uint8_t userId, |
Richard Marian Thomaiyar | 282e79b | 2018-11-13 19:00:58 +0530 | [diff] [blame] | 209 | const bool& enabledState); |
| 210 | |
Suryakanth Sekar | 90b00c7 | 2019-01-16 10:37:57 +0530 | [diff] [blame] | 211 | /** @brief to set user password |
| 212 | * |
| 213 | * @param[in] userId - user id |
| 214 | * @param[in] userPassword - new password of the user |
| 215 | * |
| 216 | * @return IPMI_CC_OK for success, others for failure. |
| 217 | */ |
| 218 | ipmi_ret_t setUserPassword(const uint8_t userId, const char* userPassword); |
| 219 | |
Richard Marian Thomaiyar | 788362c | 2019-04-14 15:12:47 +0530 | [diff] [blame] | 220 | /** @brief to set special user password |
| 221 | * |
| 222 | * @param[in] userName - user name |
| 223 | * @param[in] userPassword - new password of the user |
| 224 | * |
| 225 | * @return IPMI_CC_OK for success, others for failure. |
| 226 | */ |
| 227 | ipmi_ret_t setSpecialUserPassword(const std::string& userName, |
| 228 | const std::string& userPassword); |
| 229 | |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 230 | /** @brief to set user privilege and access details |
| 231 | * |
| 232 | * @param[in] userId - user id |
| 233 | * @param[in] chNum - channel number |
| 234 | * @param[in] privAccess - privilege access |
| 235 | * @param[in] otherPrivUpdates - other privilege update flag to update ipmi |
| 236 | * enable, link authentication and access callback |
| 237 | * |
| 238 | * @return IPMI_CC_OK for success, others for failure. |
| 239 | */ |
Richard Marian Thomaiyar | a45cb34 | 2018-12-03 15:08:59 +0530 | [diff] [blame] | 240 | ipmi_ret_t setUserPrivilegeAccess(const uint8_t userId, const uint8_t chNum, |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 241 | const UserPrivAccess& privAccess, |
| 242 | const bool& otherPrivUpdates); |
| 243 | |
| 244 | /** @brief reads user management related data from configuration file |
| 245 | * |
| 246 | */ |
| 247 | void readUserData(); |
| 248 | |
| 249 | /** @brief writes user management related data to configuration file |
| 250 | * |
| 251 | */ |
| 252 | void writeUserData(); |
| 253 | |
| 254 | /** @brief Funtion which checks and reload configuration file data if |
| 255 | * needed. |
| 256 | * |
| 257 | */ |
| 258 | void checkAndReloadUserData(); |
| 259 | |
| 260 | /** @brief provides user details from D-Bus user property data |
| 261 | * |
| 262 | * @param[in] properties - D-Bus user property |
| 263 | * @param[out] usrGrps - user group details |
| 264 | * @param[out] usrPriv - user privilege |
| 265 | * @param[out] usrEnabled - enabled state of the user. |
| 266 | * |
| 267 | * @return 0 for success, -errno for failure. |
| 268 | */ |
| 269 | void getUserProperties(const DbusUserObjProperties& properties, |
| 270 | std::vector<std::string>& usrGrps, |
| 271 | std::string& usrPriv, bool& usrEnabled); |
| 272 | |
| 273 | /** @brief provides user details from D-Bus user object data |
| 274 | * |
| 275 | * @param[in] userObjs - D-Bus user object |
| 276 | * @param[out] usrGrps - user group details |
| 277 | * @param[out] usrPriv - user privilege |
| 278 | * @param[out] usrEnabled - enabled state of the user. |
| 279 | * |
| 280 | * @return 0 for success, -errno for failure. |
| 281 | */ |
| 282 | int getUserObjProperties(const DbusUserObjValue& userObjs, |
| 283 | std::vector<std::string>& usrGrps, |
| 284 | std::string& usrPriv, bool& usrEnabled); |
| 285 | |
| 286 | /** @brief function to add user entry information to the configuration |
| 287 | * |
| 288 | * @param[in] userName - user name |
| 289 | * @param[in] priv - privilege of the user |
| 290 | * @param[in] enabled - enabled state of the user |
| 291 | * |
| 292 | * @return true for success, false for failure |
| 293 | */ |
| 294 | bool addUserEntry(const std::string& userName, const std::string& priv, |
| 295 | const bool& enabled); |
| 296 | |
| 297 | /** @brief function to delete user entry based on user index |
| 298 | * |
| 299 | * @param[in] usrIdx - user index |
| 300 | * |
| 301 | */ |
| 302 | void deleteUserIndex(const size_t& usrIdx); |
| 303 | |
| 304 | /** @brief function to get users table |
| 305 | * |
| 306 | */ |
| 307 | UsersTbl* getUsersTblPtr(); |
| 308 | |
| 309 | std::unique_ptr<boost::interprocess::named_recursive_mutex> userMutex{ |
| 310 | nullptr}; |
| 311 | |
| 312 | private: |
| 313 | UsersTbl usersTbl; |
| 314 | std::vector<std::string> availablePrivileges; |
| 315 | std::vector<std::string> availableGroups; |
| 316 | sdbusplus::bus::bus bus; |
| 317 | std::time_t fileLastUpdatedTime; |
| 318 | bool signalHndlrObject = false; |
| 319 | boost::interprocess::file_lock sigHndlrLock; |
| 320 | boost::interprocess::file_lock mutexCleanupLock; |
| 321 | |
| 322 | /** @brief function to get user configuration file timestamp |
| 323 | * |
| 324 | * @return time stamp or -EIO for failure |
| 325 | */ |
| 326 | std::time_t getUpdatedFileTime(); |
| 327 | |
| 328 | /** @brief function to available system privileges and groups |
| 329 | * |
| 330 | */ |
| 331 | void getSystemPrivAndGroups(); |
| 332 | |
| 333 | /** @brief function to init user data from configuration & D-Bus objects |
| 334 | * |
| 335 | */ |
| 336 | void initUserDataFile(); |
| 337 | }; |
| 338 | } // namespace ipmi |