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 | #include "user_mgmt.hpp" |
| 17 | |
| 18 | #include "apphandler.hpp" |
Saravanan Palanisamy | 77381f1 | 2019-05-15 22:33:17 +0000 | [diff] [blame] | 19 | #include "channel_layer.hpp" |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 20 | |
Suryakanth Sekar | 90b00c7 | 2019-01-16 10:37:57 +0530 | [diff] [blame] | 21 | #include <security/pam_appl.h> |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 22 | #include <sys/stat.h> |
| 23 | #include <unistd.h> |
| 24 | |
| 25 | #include <boost/interprocess/sync/named_recursive_mutex.hpp> |
| 26 | #include <boost/interprocess/sync/scoped_lock.hpp> |
| 27 | #include <cerrno> |
| 28 | #include <fstream> |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 29 | #include <nlohmann/json.hpp> |
| 30 | #include <phosphor-logging/elog-errors.hpp> |
| 31 | #include <phosphor-logging/log.hpp> |
| 32 | #include <regex> |
| 33 | #include <sdbusplus/bus/match.hpp> |
| 34 | #include <sdbusplus/server/object.hpp> |
Vernon Mauery | 16b8693 | 2019-05-01 08:36:11 -0700 | [diff] [blame] | 35 | #include <variant> |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 36 | #include <xyz/openbmc_project/Common/error.hpp> |
| 37 | #include <xyz/openbmc_project/User/Common/error.hpp> |
| 38 | |
| 39 | namespace ipmi |
| 40 | { |
| 41 | |
| 42 | // TODO: Move D-Bus & Object Manager related stuff, to common files |
| 43 | // D-Bus property related |
| 44 | static constexpr const char* dBusPropertiesInterface = |
| 45 | "org.freedesktop.DBus.Properties"; |
| 46 | static constexpr const char* getAllPropertiesMethod = "GetAll"; |
| 47 | static constexpr const char* propertiesChangedSignal = "PropertiesChanged"; |
| 48 | static constexpr const char* setPropertiesMethod = "Set"; |
| 49 | |
| 50 | // Object Manager related |
| 51 | static constexpr const char* dBusObjManager = |
| 52 | "org.freedesktop.DBus.ObjectManager"; |
| 53 | static constexpr const char* getManagedObjectsMethod = "GetManagedObjects"; |
| 54 | // Object Manager signals |
| 55 | static constexpr const char* intfAddedSignal = "InterfacesAdded"; |
| 56 | static constexpr const char* intfRemovedSignal = "InterfacesRemoved"; |
| 57 | |
| 58 | // Object Mapper related |
| 59 | static constexpr const char* objMapperService = |
| 60 | "xyz.openbmc_project.ObjectMapper"; |
| 61 | static constexpr const char* objMapperPath = |
| 62 | "/xyz/openbmc_project/object_mapper"; |
| 63 | static constexpr const char* objMapperInterface = |
| 64 | "xyz.openbmc_project.ObjectMapper"; |
| 65 | static constexpr const char* getSubTreeMethod = "GetSubTree"; |
| 66 | static constexpr const char* getObjectMethod = "GetObject"; |
| 67 | |
| 68 | static constexpr const char* ipmiUserMutex = "ipmi_usr_mutex"; |
| 69 | static constexpr const char* ipmiMutexCleanupLockFile = |
| 70 | "/var/lib/ipmi/ipmi_usr_mutex_cleanup"; |
| 71 | static constexpr const char* ipmiUserDataFile = "/var/lib/ipmi/ipmi_user.json"; |
| 72 | static constexpr const char* ipmiGrpName = "ipmi"; |
| 73 | static constexpr size_t privNoAccess = 0xF; |
| 74 | static constexpr size_t privMask = 0xF; |
| 75 | |
| 76 | // User manager related |
| 77 | static constexpr const char* userMgrObjBasePath = "/xyz/openbmc_project/user"; |
| 78 | static constexpr const char* userObjBasePath = "/xyz/openbmc_project/user"; |
| 79 | static constexpr const char* userMgrInterface = |
| 80 | "xyz.openbmc_project.User.Manager"; |
| 81 | static constexpr const char* usersInterface = |
| 82 | "xyz.openbmc_project.User.Attributes"; |
| 83 | static constexpr const char* deleteUserInterface = |
| 84 | "xyz.openbmc_project.Object.Delete"; |
| 85 | |
| 86 | static constexpr const char* createUserMethod = "CreateUser"; |
| 87 | static constexpr const char* deleteUserMethod = "Delete"; |
| 88 | static constexpr const char* renameUserMethod = "RenameUser"; |
| 89 | // User manager signal memebers |
| 90 | static constexpr const char* userRenamedSignal = "UserRenamed"; |
| 91 | // Mgr interface properties |
| 92 | static constexpr const char* allPrivProperty = "AllPrivileges"; |
| 93 | static constexpr const char* allGrpProperty = "AllGroups"; |
| 94 | // User interface properties |
| 95 | static constexpr const char* userPrivProperty = "UserPrivilege"; |
| 96 | static constexpr const char* userGrpProperty = "UserGroups"; |
| 97 | static constexpr const char* userEnabledProperty = "UserEnabled"; |
| 98 | |
| 99 | static std::array<std::string, (PRIVILEGE_OEM + 1)> ipmiPrivIndex = { |
| 100 | "priv-reserved", // PRIVILEGE_RESERVED - 0 |
| 101 | "priv-callback", // PRIVILEGE_CALLBACK - 1 |
| 102 | "priv-user", // PRIVILEGE_USER - 2 |
| 103 | "priv-operator", // PRIVILEGE_OPERATOR - 3 |
| 104 | "priv-admin", // PRIVILEGE_ADMIN - 4 |
| 105 | "priv-custom" // PRIVILEGE_OEM - 5 |
| 106 | }; |
| 107 | |
| 108 | using namespace phosphor::logging; |
| 109 | using Json = nlohmann::json; |
| 110 | |
Vernon Mauery | 16b8693 | 2019-05-01 08:36:11 -0700 | [diff] [blame] | 111 | using PrivAndGroupType = std::variant<std::string, std::vector<std::string>>; |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 112 | |
| 113 | using NoResource = |
| 114 | sdbusplus::xyz::openbmc_project::User::Common::Error::NoResource; |
| 115 | |
| 116 | using InternalFailure = |
| 117 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
| 118 | |
Lei YU | 4b0ddb6 | 2019-01-25 16:43:50 +0800 | [diff] [blame] | 119 | std::unique_ptr<sdbusplus::bus::match_t> userUpdatedSignal |
| 120 | __attribute__((init_priority(101))); |
| 121 | std::unique_ptr<sdbusplus::bus::match_t> userMgrRenamedSignal |
| 122 | __attribute__((init_priority(101))); |
| 123 | std::unique_ptr<sdbusplus::bus::match_t> userPropertiesSignal |
| 124 | __attribute__((init_priority(101))); |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 125 | |
| 126 | // TODO: Below code can be removed once it is moved to common layer libmiscutil |
| 127 | std::string getUserService(sdbusplus::bus::bus& bus, const std::string& intf, |
| 128 | const std::string& path) |
| 129 | { |
| 130 | auto mapperCall = bus.new_method_call(objMapperService, objMapperPath, |
| 131 | objMapperInterface, getObjectMethod); |
| 132 | |
| 133 | mapperCall.append(path); |
| 134 | mapperCall.append(std::vector<std::string>({intf})); |
| 135 | |
| 136 | auto mapperResponseMsg = bus.call(mapperCall); |
| 137 | |
| 138 | std::map<std::string, std::vector<std::string>> mapperResponse; |
| 139 | mapperResponseMsg.read(mapperResponse); |
| 140 | |
| 141 | if (mapperResponse.begin() == mapperResponse.end()) |
| 142 | { |
| 143 | throw sdbusplus::exception::SdBusError( |
| 144 | -EIO, "ERROR in reading the mapper response"); |
| 145 | } |
| 146 | |
| 147 | return mapperResponse.begin()->first; |
| 148 | } |
| 149 | |
| 150 | void setDbusProperty(sdbusplus::bus::bus& bus, const std::string& service, |
| 151 | const std::string& objPath, const std::string& interface, |
| 152 | const std::string& property, |
| 153 | const DbusUserPropVariant& value) |
| 154 | { |
| 155 | try |
| 156 | { |
| 157 | auto method = |
| 158 | bus.new_method_call(service.c_str(), objPath.c_str(), |
| 159 | dBusPropertiesInterface, setPropertiesMethod); |
| 160 | method.append(interface, property, value); |
| 161 | bus.call(method); |
| 162 | } |
| 163 | catch (const sdbusplus::exception::SdBusError& e) |
| 164 | { |
| 165 | log<level::ERR>("Failed to set property", |
| 166 | entry("PROPERTY=%s", property.c_str()), |
| 167 | entry("PATH=%s", objPath.c_str()), |
| 168 | entry("INTERFACE=%s", interface.c_str())); |
| 169 | throw; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | static std::string getUserServiceName() |
| 174 | { |
| 175 | static sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection()); |
| 176 | static std::string userMgmtService; |
| 177 | if (userMgmtService.empty()) |
| 178 | { |
| 179 | try |
| 180 | { |
| 181 | userMgmtService = |
| 182 | ipmi::getUserService(bus, userMgrInterface, userMgrObjBasePath); |
| 183 | } |
| 184 | catch (const sdbusplus::exception::SdBusError& e) |
| 185 | { |
| 186 | userMgmtService.clear(); |
| 187 | } |
| 188 | } |
| 189 | return userMgmtService; |
| 190 | } |
| 191 | |
| 192 | UserAccess& getUserAccessObject() |
| 193 | { |
| 194 | static UserAccess userAccess; |
| 195 | return userAccess; |
| 196 | } |
| 197 | |
| 198 | int getUserNameFromPath(const std::string& path, std::string& userName) |
| 199 | { |
| 200 | static size_t pos = strlen(userObjBasePath) + 1; |
| 201 | if (path.find(userObjBasePath) == std::string::npos) |
| 202 | { |
| 203 | return -EINVAL; |
| 204 | } |
| 205 | userName.assign(path, pos, path.size()); |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | void userUpdateHelper(UserAccess& usrAccess, const UserUpdateEvent& userEvent, |
| 210 | const std::string& userName, const std::string& priv, |
| 211 | const bool& enabled, const std::string& newUserName) |
| 212 | { |
| 213 | UsersTbl* userData = usrAccess.getUsersTblPtr(); |
| 214 | if (userEvent == UserUpdateEvent::userCreated) |
| 215 | { |
| 216 | if (usrAccess.addUserEntry(userName, priv, enabled) == false) |
| 217 | { |
| 218 | return; |
| 219 | } |
| 220 | } |
| 221 | else |
| 222 | { |
| 223 | // user index 0 is reserved, starts with 1 |
| 224 | size_t usrIndex = 1; |
| 225 | for (; usrIndex <= ipmiMaxUsers; ++usrIndex) |
| 226 | { |
| 227 | std::string curName( |
| 228 | reinterpret_cast<char*>(userData->user[usrIndex].userName), 0, |
| 229 | ipmiMaxUserName); |
| 230 | if (userName == curName) |
| 231 | { |
| 232 | break; // found the entry |
| 233 | } |
| 234 | } |
| 235 | if (usrIndex > ipmiMaxUsers) |
| 236 | { |
| 237 | log<level::DEBUG>("User not found for signal", |
| 238 | entry("USER_NAME=%s", userName.c_str()), |
| 239 | entry("USER_EVENT=%d", userEvent)); |
| 240 | return; |
| 241 | } |
| 242 | switch (userEvent) |
| 243 | { |
| 244 | case UserUpdateEvent::userDeleted: |
| 245 | { |
| 246 | usrAccess.deleteUserIndex(usrIndex); |
| 247 | break; |
| 248 | } |
| 249 | case UserUpdateEvent::userPrivUpdated: |
| 250 | { |
| 251 | uint8_t userPriv = |
| 252 | static_cast<uint8_t>( |
| 253 | UserAccess::convertToIPMIPrivilege(priv)) & |
| 254 | privMask; |
| 255 | // Update all channels privileges, only if it is not equivalent |
| 256 | // to getUsrMgmtSyncIndex() |
| 257 | if (userData->user[usrIndex] |
| 258 | .userPrivAccess[UserAccess::getUsrMgmtSyncIndex()] |
| 259 | .privilege != userPriv) |
| 260 | { |
| 261 | for (size_t chIndex = 0; chIndex < ipmiMaxChannels; |
| 262 | ++chIndex) |
| 263 | { |
| 264 | userData->user[usrIndex] |
| 265 | .userPrivAccess[chIndex] |
| 266 | .privilege = userPriv; |
| 267 | } |
| 268 | } |
| 269 | break; |
| 270 | } |
| 271 | case UserUpdateEvent::userRenamed: |
| 272 | { |
| 273 | std::fill( |
| 274 | static_cast<uint8_t*>(userData->user[usrIndex].userName), |
| 275 | static_cast<uint8_t*>(userData->user[usrIndex].userName) + |
| 276 | sizeof(userData->user[usrIndex].userName), |
| 277 | 0); |
| 278 | std::strncpy( |
| 279 | reinterpret_cast<char*>(userData->user[usrIndex].userName), |
| 280 | newUserName.c_str(), ipmiMaxUserName); |
| 281 | ipmiRenameUserEntryPassword(userName, newUserName); |
| 282 | break; |
| 283 | } |
| 284 | case UserUpdateEvent::userStateUpdated: |
| 285 | { |
| 286 | userData->user[usrIndex].userEnabled = enabled; |
| 287 | break; |
| 288 | } |
| 289 | default: |
| 290 | { |
| 291 | log<level::ERR>("Unhandled user event", |
| 292 | entry("USER_EVENT=%d", userEvent)); |
| 293 | return; |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | usrAccess.writeUserData(); |
| 298 | log<level::DEBUG>("User event handled successfully", |
| 299 | entry("USER_NAME=%s", userName.c_str()), |
| 300 | entry("USER_EVENT=%d", userEvent)); |
| 301 | |
| 302 | return; |
| 303 | } |
| 304 | |
| 305 | void userUpdatedSignalHandler(UserAccess& usrAccess, |
| 306 | sdbusplus::message::message& msg) |
| 307 | { |
| 308 | static sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection()); |
| 309 | std::string signal = msg.get_member(); |
Patrick Venture | 3a697ad | 2019-08-19 11:12:05 -0700 | [diff] [blame] | 310 | std::string userName, priv, newUserName; |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 311 | std::vector<std::string> groups; |
| 312 | bool enabled = false; |
| 313 | UserUpdateEvent userEvent = UserUpdateEvent::reservedEvent; |
| 314 | if (signal == intfAddedSignal) |
| 315 | { |
| 316 | DbusUserObjPath objPath; |
| 317 | DbusUserObjValue objValue; |
| 318 | msg.read(objPath, objValue); |
| 319 | getUserNameFromPath(objPath.str, userName); |
| 320 | if (usrAccess.getUserObjProperties(objValue, groups, priv, enabled) != |
| 321 | 0) |
| 322 | { |
| 323 | return; |
| 324 | } |
| 325 | if (std::find(groups.begin(), groups.end(), ipmiGrpName) == |
| 326 | groups.end()) |
| 327 | { |
| 328 | return; |
| 329 | } |
| 330 | userEvent = UserUpdateEvent::userCreated; |
| 331 | } |
| 332 | else if (signal == intfRemovedSignal) |
| 333 | { |
| 334 | DbusUserObjPath objPath; |
| 335 | std::vector<std::string> interfaces; |
| 336 | msg.read(objPath, interfaces); |
| 337 | getUserNameFromPath(objPath.str, userName); |
| 338 | userEvent = UserUpdateEvent::userDeleted; |
| 339 | } |
| 340 | else if (signal == userRenamedSignal) |
| 341 | { |
| 342 | msg.read(userName, newUserName); |
| 343 | userEvent = UserUpdateEvent::userRenamed; |
| 344 | } |
| 345 | else if (signal == propertiesChangedSignal) |
| 346 | { |
| 347 | getUserNameFromPath(msg.get_path(), userName); |
| 348 | } |
| 349 | else |
| 350 | { |
| 351 | log<level::ERR>("Unknown user update signal", |
| 352 | entry("SIGNAL=%s", signal.c_str())); |
| 353 | return; |
| 354 | } |
| 355 | |
| 356 | if (signal.empty() || userName.empty() || |
| 357 | (signal == userRenamedSignal && newUserName.empty())) |
| 358 | { |
| 359 | log<level::ERR>("Invalid inputs received"); |
| 360 | return; |
| 361 | } |
| 362 | |
| 363 | boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex> |
| 364 | userLock{*(usrAccess.userMutex)}; |
| 365 | usrAccess.checkAndReloadUserData(); |
| 366 | |
| 367 | if (signal == propertiesChangedSignal) |
| 368 | { |
| 369 | std::string intfName; |
| 370 | DbusUserObjProperties chProperties; |
| 371 | msg.read(intfName, chProperties); // skip reading 3rd argument. |
| 372 | for (const auto& prop : chProperties) |
| 373 | { |
| 374 | userEvent = UserUpdateEvent::reservedEvent; |
| 375 | std::string member = prop.first; |
| 376 | if (member == userPrivProperty) |
| 377 | { |
Vernon Mauery | f442e11 | 2019-04-09 11:44:36 -0700 | [diff] [blame] | 378 | priv = std::get<std::string>(prop.second); |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 379 | userEvent = UserUpdateEvent::userPrivUpdated; |
| 380 | } |
| 381 | else if (member == userGrpProperty) |
| 382 | { |
Vernon Mauery | f442e11 | 2019-04-09 11:44:36 -0700 | [diff] [blame] | 383 | groups = std::get<std::vector<std::string>>(prop.second); |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 384 | userEvent = UserUpdateEvent::userGrpUpdated; |
| 385 | } |
| 386 | else if (member == userEnabledProperty) |
| 387 | { |
Vernon Mauery | f442e11 | 2019-04-09 11:44:36 -0700 | [diff] [blame] | 388 | enabled = std::get<bool>(prop.second); |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 389 | userEvent = UserUpdateEvent::userStateUpdated; |
| 390 | } |
| 391 | // Process based on event type. |
| 392 | if (userEvent == UserUpdateEvent::userGrpUpdated) |
| 393 | { |
| 394 | if (std::find(groups.begin(), groups.end(), ipmiGrpName) == |
| 395 | groups.end()) |
| 396 | { |
| 397 | // remove user from ipmi user list. |
| 398 | userUpdateHelper(usrAccess, UserUpdateEvent::userDeleted, |
| 399 | userName, priv, enabled, newUserName); |
| 400 | } |
| 401 | else |
| 402 | { |
| 403 | DbusUserObjProperties properties; |
| 404 | try |
| 405 | { |
| 406 | auto method = bus.new_method_call( |
| 407 | getUserServiceName().c_str(), msg.get_path(), |
| 408 | dBusPropertiesInterface, getAllPropertiesMethod); |
| 409 | method.append(usersInterface); |
| 410 | auto reply = bus.call(method); |
| 411 | reply.read(properties); |
| 412 | } |
| 413 | catch (const sdbusplus::exception::SdBusError& e) |
| 414 | { |
| 415 | log<level::DEBUG>( |
| 416 | "Failed to excute method", |
| 417 | entry("METHOD=%s", getAllPropertiesMethod), |
| 418 | entry("PATH=%s", msg.get_path())); |
| 419 | return; |
| 420 | } |
| 421 | usrAccess.getUserProperties(properties, groups, priv, |
| 422 | enabled); |
| 423 | // add user to ipmi user list. |
| 424 | userUpdateHelper(usrAccess, UserUpdateEvent::userCreated, |
| 425 | userName, priv, enabled, newUserName); |
| 426 | } |
| 427 | } |
| 428 | else if (userEvent != UserUpdateEvent::reservedEvent) |
| 429 | { |
| 430 | userUpdateHelper(usrAccess, userEvent, userName, priv, enabled, |
| 431 | newUserName); |
| 432 | } |
| 433 | } |
| 434 | } |
| 435 | else if (userEvent != UserUpdateEvent::reservedEvent) |
| 436 | { |
| 437 | userUpdateHelper(usrAccess, userEvent, userName, priv, enabled, |
| 438 | newUserName); |
| 439 | } |
| 440 | return; |
| 441 | } |
| 442 | |
| 443 | UserAccess::~UserAccess() |
| 444 | { |
| 445 | if (signalHndlrObject) |
| 446 | { |
| 447 | userUpdatedSignal.reset(); |
| 448 | userMgrRenamedSignal.reset(); |
| 449 | userPropertiesSignal.reset(); |
| 450 | sigHndlrLock.unlock(); |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | UserAccess::UserAccess() : bus(ipmid_get_sd_bus_connection()) |
| 455 | { |
| 456 | std::ofstream mutexCleanUpFile; |
| 457 | mutexCleanUpFile.open(ipmiMutexCleanupLockFile, |
| 458 | std::ofstream::out | std::ofstream::app); |
| 459 | if (!mutexCleanUpFile.good()) |
| 460 | { |
| 461 | log<level::DEBUG>("Unable to open mutex cleanup file"); |
| 462 | return; |
| 463 | } |
| 464 | mutexCleanUpFile.close(); |
| 465 | mutexCleanupLock = boost::interprocess::file_lock(ipmiMutexCleanupLockFile); |
| 466 | if (mutexCleanupLock.try_lock()) |
| 467 | { |
| 468 | boost::interprocess::named_recursive_mutex::remove(ipmiUserMutex); |
| 469 | } |
| 470 | mutexCleanupLock.lock_sharable(); |
| 471 | userMutex = std::make_unique<boost::interprocess::named_recursive_mutex>( |
| 472 | boost::interprocess::open_or_create, ipmiUserMutex); |
| 473 | |
arun-pm | bbe728c | 2020-01-10 15:18:04 +0530 | [diff] [blame^] | 474 | cacheUserDataFile(); |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 475 | getSystemPrivAndGroups(); |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 476 | } |
| 477 | |
Richard Marian Thomaiyar | a45cb34 | 2018-12-03 15:08:59 +0530 | [diff] [blame] | 478 | UserInfo* UserAccess::getUserInfo(const uint8_t userId) |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 479 | { |
| 480 | checkAndReloadUserData(); |
| 481 | return &usersTbl.user[userId]; |
| 482 | } |
| 483 | |
Richard Marian Thomaiyar | a45cb34 | 2018-12-03 15:08:59 +0530 | [diff] [blame] | 484 | void UserAccess::setUserInfo(const uint8_t userId, UserInfo* userInfo) |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 485 | { |
| 486 | checkAndReloadUserData(); |
| 487 | std::copy(reinterpret_cast<uint8_t*>(userInfo), |
| 488 | reinterpret_cast<uint8_t*>(userInfo) + sizeof(*userInfo), |
| 489 | reinterpret_cast<uint8_t*>(&usersTbl.user[userId])); |
| 490 | writeUserData(); |
| 491 | } |
| 492 | |
Richard Marian Thomaiyar | a45cb34 | 2018-12-03 15:08:59 +0530 | [diff] [blame] | 493 | bool UserAccess::isValidChannel(const uint8_t chNum) |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 494 | { |
| 495 | return (chNum < ipmiMaxChannels); |
| 496 | } |
| 497 | |
Richard Marian Thomaiyar | a45cb34 | 2018-12-03 15:08:59 +0530 | [diff] [blame] | 498 | bool UserAccess::isValidUserId(const uint8_t userId) |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 499 | { |
| 500 | return ((userId <= ipmiMaxUsers) && (userId != reservedUserId)); |
| 501 | } |
| 502 | |
Richard Marian Thomaiyar | a45cb34 | 2018-12-03 15:08:59 +0530 | [diff] [blame] | 503 | bool UserAccess::isValidPrivilege(const uint8_t priv) |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 504 | { |
| 505 | return ((priv >= PRIVILEGE_CALLBACK && priv <= PRIVILEGE_OEM) || |
| 506 | priv == privNoAccess); |
| 507 | } |
| 508 | |
| 509 | uint8_t UserAccess::getUsrMgmtSyncIndex() |
| 510 | { |
| 511 | // TODO: Need to get LAN1 channel number dynamically, |
| 512 | // which has to be in sync with system user privilege |
| 513 | // level(Phosphor-user-manager). Note: For time being chanLan1 is marked as |
| 514 | // sync index to the user-manager privilege.. |
| 515 | return static_cast<uint8_t>(EChannelID::chanLan1); |
| 516 | } |
| 517 | |
| 518 | CommandPrivilege UserAccess::convertToIPMIPrivilege(const std::string& value) |
| 519 | { |
| 520 | auto iter = std::find(ipmiPrivIndex.begin(), ipmiPrivIndex.end(), value); |
| 521 | if (iter == ipmiPrivIndex.end()) |
| 522 | { |
| 523 | if (value == "") |
| 524 | { |
| 525 | return static_cast<CommandPrivilege>(privNoAccess); |
| 526 | } |
| 527 | log<level::ERR>("Error in converting to IPMI privilege", |
| 528 | entry("PRIV=%s", value.c_str())); |
| 529 | throw std::out_of_range("Out of range - convertToIPMIPrivilege"); |
| 530 | } |
| 531 | else |
| 532 | { |
| 533 | return static_cast<CommandPrivilege>( |
| 534 | std::distance(ipmiPrivIndex.begin(), iter)); |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | std::string UserAccess::convertToSystemPrivilege(const CommandPrivilege& value) |
| 539 | { |
| 540 | if (value == static_cast<CommandPrivilege>(privNoAccess)) |
| 541 | { |
| 542 | return ""; |
| 543 | } |
| 544 | try |
| 545 | { |
| 546 | return ipmiPrivIndex.at(value); |
| 547 | } |
| 548 | catch (const std::out_of_range& e) |
| 549 | { |
| 550 | log<level::ERR>("Error in converting to system privilege", |
| 551 | entry("PRIV=%d", static_cast<uint8_t>(value))); |
| 552 | throw std::out_of_range("Out of range - convertToSystemPrivilege"); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | bool UserAccess::isValidUserName(const char* userNameInChar) |
| 557 | { |
| 558 | if (!userNameInChar) |
| 559 | { |
| 560 | log<level::ERR>("null ptr"); |
| 561 | return false; |
| 562 | } |
| 563 | std::string userName(userNameInChar, 0, ipmiMaxUserName); |
| 564 | if (!std::regex_match(userName.c_str(), |
| 565 | std::regex("[a-zA-z_][a-zA-Z_0-9]*"))) |
| 566 | { |
| 567 | log<level::ERR>("Unsupported characters in user name"); |
| 568 | return false; |
| 569 | } |
| 570 | if (userName == "root") |
| 571 | { |
| 572 | log<level::ERR>("Invalid user name - root"); |
| 573 | return false; |
| 574 | } |
| 575 | std::map<DbusUserObjPath, DbusUserObjValue> properties; |
| 576 | try |
| 577 | { |
| 578 | auto method = bus.new_method_call(getUserServiceName().c_str(), |
| 579 | userMgrObjBasePath, dBusObjManager, |
| 580 | getManagedObjectsMethod); |
| 581 | auto reply = bus.call(method); |
| 582 | reply.read(properties); |
| 583 | } |
| 584 | catch (const sdbusplus::exception::SdBusError& e) |
| 585 | { |
| 586 | log<level::ERR>("Failed to excute method", |
| 587 | entry("METHOD=%s", getSubTreeMethod), |
| 588 | entry("PATH=%s", userMgrObjBasePath)); |
| 589 | return false; |
| 590 | } |
| 591 | |
| 592 | std::string usersPath = std::string(userObjBasePath) + "/" + userName; |
| 593 | if (properties.find(usersPath) != properties.end()) |
| 594 | { |
| 595 | log<level::DEBUG>("User name already exists", |
| 596 | entry("USER_NAME=%s", userName.c_str())); |
| 597 | return false; |
| 598 | } |
| 599 | |
| 600 | return true; |
| 601 | } |
| 602 | |
Suryakanth Sekar | 90b00c7 | 2019-01-16 10:37:57 +0530 | [diff] [blame] | 603 | /** @brief Information exchanged by pam module and application. |
| 604 | * |
| 605 | * @param[in] numMsg - length of the array of pointers,msg. |
| 606 | * |
| 607 | * @param[in] msg - pointer to an array of pointers to pam_message structure |
| 608 | * |
| 609 | * @param[out] resp - struct pam response array |
| 610 | * |
| 611 | * @param[in] appdataPtr - member of pam_conv structure |
| 612 | * |
| 613 | * @return the response in pam response structure. |
| 614 | */ |
| 615 | |
| 616 | static int pamFunctionConversation(int numMsg, const struct pam_message** msg, |
| 617 | struct pam_response** resp, void* appdataPtr) |
| 618 | { |
| 619 | if (appdataPtr == nullptr) |
| 620 | { |
| 621 | return PAM_AUTH_ERR; |
| 622 | } |
| 623 | size_t passSize = std::strlen(reinterpret_cast<char*>(appdataPtr)) + 1; |
| 624 | char* pass = reinterpret_cast<char*>(malloc(passSize)); |
| 625 | std::strncpy(pass, reinterpret_cast<char*>(appdataPtr), passSize); |
| 626 | |
| 627 | *resp = reinterpret_cast<pam_response*>( |
| 628 | calloc(numMsg, sizeof(struct pam_response))); |
| 629 | |
| 630 | for (int i = 0; i < numMsg; ++i) |
| 631 | { |
| 632 | if (msg[i]->msg_style != PAM_PROMPT_ECHO_OFF) |
| 633 | { |
| 634 | continue; |
| 635 | } |
| 636 | resp[i]->resp = pass; |
| 637 | } |
| 638 | return PAM_SUCCESS; |
| 639 | } |
| 640 | |
| 641 | /** @brief Updating the PAM password |
| 642 | * |
| 643 | * @param[in] username - username in string |
| 644 | * |
| 645 | * @param[in] password - new password in string |
| 646 | * |
| 647 | * @return status |
| 648 | */ |
| 649 | |
jayaprakash Mutyala | 9fc5fa1 | 2019-08-29 15:14:06 +0000 | [diff] [blame] | 650 | int pamUpdatePasswd(const char* username, const char* password) |
Suryakanth Sekar | 90b00c7 | 2019-01-16 10:37:57 +0530 | [diff] [blame] | 651 | { |
| 652 | const struct pam_conv localConversation = {pamFunctionConversation, |
| 653 | const_cast<char*>(password)}; |
| 654 | pam_handle_t* localAuthHandle = NULL; // this gets set by pam_start |
| 655 | |
jayaprakash Mutyala | 9fc5fa1 | 2019-08-29 15:14:06 +0000 | [diff] [blame] | 656 | int retval = |
| 657 | pam_start("passwd", username, &localConversation, &localAuthHandle); |
Suryakanth Sekar | 90b00c7 | 2019-01-16 10:37:57 +0530 | [diff] [blame] | 658 | |
| 659 | if (retval != PAM_SUCCESS) |
| 660 | { |
jayaprakash Mutyala | 9fc5fa1 | 2019-08-29 15:14:06 +0000 | [diff] [blame] | 661 | return retval; |
Suryakanth Sekar | 90b00c7 | 2019-01-16 10:37:57 +0530 | [diff] [blame] | 662 | } |
jayaprakash Mutyala | 9fc5fa1 | 2019-08-29 15:14:06 +0000 | [diff] [blame] | 663 | |
| 664 | retval = pam_chauthtok(localAuthHandle, PAM_SILENT); |
| 665 | if (retval != PAM_SUCCESS) |
Suryakanth Sekar | 90b00c7 | 2019-01-16 10:37:57 +0530 | [diff] [blame] | 666 | { |
jayaprakash Mutyala | 9fc5fa1 | 2019-08-29 15:14:06 +0000 | [diff] [blame] | 667 | pam_end(localAuthHandle, retval); |
| 668 | return retval; |
Suryakanth Sekar | 90b00c7 | 2019-01-16 10:37:57 +0530 | [diff] [blame] | 669 | } |
jayaprakash Mutyala | 9fc5fa1 | 2019-08-29 15:14:06 +0000 | [diff] [blame] | 670 | |
| 671 | return pam_end(localAuthHandle, PAM_SUCCESS); |
Suryakanth Sekar | 90b00c7 | 2019-01-16 10:37:57 +0530 | [diff] [blame] | 672 | } |
| 673 | |
Ayushi Smriti | 02650d5 | 2019-05-15 11:59:09 +0000 | [diff] [blame] | 674 | bool pamUserCheckAuthenticate(std::string_view username, |
| 675 | std::string_view password) |
| 676 | { |
| 677 | const struct pam_conv localConversation = { |
| 678 | pamFunctionConversation, const_cast<char*>(password.data())}; |
| 679 | |
| 680 | pam_handle_t* localAuthHandle = NULL; // this gets set by pam_start |
| 681 | |
| 682 | if (pam_start("dropbear", username.data(), &localConversation, |
| 683 | &localAuthHandle) != PAM_SUCCESS) |
| 684 | { |
| 685 | log<level::ERR>("User Authentication Failure"); |
| 686 | return false; |
| 687 | } |
| 688 | |
| 689 | int retval = pam_authenticate(localAuthHandle, |
| 690 | PAM_SILENT | PAM_DISALLOW_NULL_AUTHTOK); |
| 691 | |
| 692 | if (retval != PAM_SUCCESS) |
| 693 | { |
| 694 | log<level::DEBUG>("pam_authenticate returned failure", |
| 695 | entry("ERROR=%d", retval)); |
| 696 | |
| 697 | pam_end(localAuthHandle, retval); |
| 698 | return false; |
| 699 | } |
| 700 | |
| 701 | if (pam_acct_mgmt(localAuthHandle, PAM_DISALLOW_NULL_AUTHTOK) != |
| 702 | PAM_SUCCESS) |
| 703 | { |
| 704 | pam_end(localAuthHandle, PAM_SUCCESS); |
| 705 | return false; |
| 706 | } |
| 707 | |
| 708 | if (pam_end(localAuthHandle, PAM_SUCCESS) != PAM_SUCCESS) |
| 709 | { |
| 710 | return false; |
| 711 | } |
| 712 | return true; |
| 713 | } |
| 714 | |
Richard Marian Thomaiyar | 788362c | 2019-04-14 15:12:47 +0530 | [diff] [blame] | 715 | ipmi_ret_t UserAccess::setSpecialUserPassword(const std::string& userName, |
| 716 | const std::string& userPassword) |
| 717 | { |
jayaprakash Mutyala | 9fc5fa1 | 2019-08-29 15:14:06 +0000 | [diff] [blame] | 718 | if (pamUpdatePasswd(userName.c_str(), userPassword.c_str()) != PAM_SUCCESS) |
Richard Marian Thomaiyar | 788362c | 2019-04-14 15:12:47 +0530 | [diff] [blame] | 719 | { |
| 720 | log<level::DEBUG>("Failed to update password"); |
| 721 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 722 | } |
| 723 | return IPMI_CC_OK; |
| 724 | } |
| 725 | |
Suryakanth Sekar | 90b00c7 | 2019-01-16 10:37:57 +0530 | [diff] [blame] | 726 | ipmi_ret_t UserAccess::setUserPassword(const uint8_t userId, |
| 727 | const char* userPassword) |
| 728 | { |
| 729 | std::string userName; |
jayaprakash Mutyala | 9fc5fa1 | 2019-08-29 15:14:06 +0000 | [diff] [blame] | 730 | if (ipmiUserGetUserName(userId, userName) != ipmi::ccSuccess) |
Suryakanth Sekar | 90b00c7 | 2019-01-16 10:37:57 +0530 | [diff] [blame] | 731 | { |
| 732 | log<level::DEBUG>("User Name not found", |
Ayushi Smriti | 05ad341 | 2019-10-16 16:10:18 +0530 | [diff] [blame] | 733 | entry("USER-ID=%d", (uint8_t)userId)); |
jayaprakash Mutyala | 9fc5fa1 | 2019-08-29 15:14:06 +0000 | [diff] [blame] | 734 | return ipmi::ccParmOutOfRange; |
Suryakanth Sekar | 90b00c7 | 2019-01-16 10:37:57 +0530 | [diff] [blame] | 735 | } |
| 736 | std::string passwd; |
| 737 | passwd.assign(reinterpret_cast<const char*>(userPassword), 0, |
| 738 | maxIpmi20PasswordSize); |
jayaprakash Mutyala | 9fc5fa1 | 2019-08-29 15:14:06 +0000 | [diff] [blame] | 739 | |
| 740 | int retval = pamUpdatePasswd(userName.c_str(), passwd.c_str()); |
| 741 | |
| 742 | switch (retval) |
Suryakanth Sekar | 90b00c7 | 2019-01-16 10:37:57 +0530 | [diff] [blame] | 743 | { |
jayaprakash Mutyala | 9fc5fa1 | 2019-08-29 15:14:06 +0000 | [diff] [blame] | 744 | case PAM_SUCCESS: |
| 745 | { |
| 746 | return ipmi::ccSuccess; |
| 747 | } |
| 748 | case PAM_AUTHTOK_ERR: |
| 749 | { |
| 750 | log<level::DEBUG>("Bad authentication token"); |
| 751 | return ipmi::ccInvalidFieldRequest; |
| 752 | } |
| 753 | default: |
| 754 | { |
| 755 | log<level::DEBUG>("Failed to update password", |
| 756 | entry("USER-ID=%d", (uint8_t)userId)); |
| 757 | return ipmi::ccUnspecifiedError; |
| 758 | } |
Suryakanth Sekar | 90b00c7 | 2019-01-16 10:37:57 +0530 | [diff] [blame] | 759 | } |
Suryakanth Sekar | 90b00c7 | 2019-01-16 10:37:57 +0530 | [diff] [blame] | 760 | } |
| 761 | |
Richard Marian Thomaiyar | a45cb34 | 2018-12-03 15:08:59 +0530 | [diff] [blame] | 762 | ipmi_ret_t UserAccess::setUserEnabledState(const uint8_t userId, |
Richard Marian Thomaiyar | 282e79b | 2018-11-13 19:00:58 +0530 | [diff] [blame] | 763 | const bool& enabledState) |
| 764 | { |
| 765 | if (!isValidUserId(userId)) |
| 766 | { |
| 767 | return IPMI_CC_PARM_OUT_OF_RANGE; |
| 768 | } |
| 769 | boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex> |
| 770 | userLock{*userMutex}; |
| 771 | UserInfo* userInfo = getUserInfo(userId); |
| 772 | std::string userName; |
| 773 | userName.assign(reinterpret_cast<char*>(userInfo->userName), 0, |
| 774 | ipmiMaxUserName); |
| 775 | if (userName.empty()) |
| 776 | { |
| 777 | log<level::DEBUG>("User name not set / invalid"); |
| 778 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 779 | } |
| 780 | if (userInfo->userEnabled != enabledState) |
| 781 | { |
| 782 | std::string userPath = std::string(userObjBasePath) + "/" + userName; |
Patrick Venture | 99d1ba0 | 2019-02-21 15:11:24 -0800 | [diff] [blame] | 783 | setDbusProperty(bus, getUserServiceName(), userPath, usersInterface, |
| 784 | userEnabledProperty, enabledState); |
Richard Marian Thomaiyar | 2fe9282 | 2019-03-02 22:07:03 +0530 | [diff] [blame] | 785 | userInfo->userEnabled = enabledState; |
| 786 | try |
| 787 | { |
| 788 | writeUserData(); |
| 789 | } |
| 790 | catch (const std::exception& e) |
| 791 | { |
| 792 | log<level::DEBUG>("Write user data failed"); |
| 793 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 794 | } |
Richard Marian Thomaiyar | 282e79b | 2018-11-13 19:00:58 +0530 | [diff] [blame] | 795 | } |
| 796 | return IPMI_CC_OK; |
| 797 | } |
| 798 | |
Saravanan Palanisamy | 77381f1 | 2019-05-15 22:33:17 +0000 | [diff] [blame] | 799 | ipmi_ret_t UserAccess::setUserPayloadAccess(const uint8_t chNum, |
| 800 | const uint8_t operation, |
| 801 | const uint8_t userId, |
| 802 | const PayloadAccess& payloadAccess) |
| 803 | { |
| 804 | constexpr uint8_t enable = 0x0; |
| 805 | constexpr uint8_t disable = 0x1; |
| 806 | |
| 807 | if (!isValidChannel(chNum)) |
| 808 | { |
| 809 | return IPMI_CC_INVALID_FIELD_REQUEST; |
| 810 | } |
| 811 | if (!isValidUserId(userId)) |
| 812 | { |
| 813 | return IPMI_CC_PARM_OUT_OF_RANGE; |
| 814 | } |
| 815 | if (operation != enable && operation != disable) |
| 816 | { |
| 817 | return IPMI_CC_INVALID_FIELD_REQUEST; |
| 818 | } |
| 819 | // Check operation & payloadAccess if required. |
| 820 | boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex> |
| 821 | userLock{*userMutex}; |
| 822 | UserInfo* userInfo = getUserInfo(userId); |
| 823 | |
| 824 | if (operation == enable) |
| 825 | { |
| 826 | userInfo->payloadAccess[chNum].stdPayloadEnables1 |= |
| 827 | payloadAccess.stdPayloadEnables1; |
| 828 | |
| 829 | userInfo->payloadAccess[chNum].oemPayloadEnables1 |= |
| 830 | payloadAccess.oemPayloadEnables1; |
| 831 | } |
| 832 | else |
| 833 | { |
| 834 | userInfo->payloadAccess[chNum].stdPayloadEnables1 &= |
| 835 | ~(payloadAccess.stdPayloadEnables1); |
| 836 | |
| 837 | userInfo->payloadAccess[chNum].oemPayloadEnables1 &= |
| 838 | ~(payloadAccess.oemPayloadEnables1); |
| 839 | } |
| 840 | |
| 841 | try |
| 842 | { |
| 843 | writeUserData(); |
| 844 | } |
| 845 | catch (const std::exception& e) |
| 846 | { |
| 847 | log<level::ERR>("Write user data failed"); |
| 848 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 849 | } |
| 850 | return IPMI_CC_OK; |
| 851 | } |
| 852 | |
Richard Marian Thomaiyar | a45cb34 | 2018-12-03 15:08:59 +0530 | [diff] [blame] | 853 | ipmi_ret_t UserAccess::setUserPrivilegeAccess(const uint8_t userId, |
| 854 | const uint8_t chNum, |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 855 | const UserPrivAccess& privAccess, |
| 856 | const bool& otherPrivUpdates) |
| 857 | { |
| 858 | if (!isValidChannel(chNum)) |
| 859 | { |
| 860 | return IPMI_CC_INVALID_FIELD_REQUEST; |
| 861 | } |
| 862 | if (!isValidUserId(userId)) |
| 863 | { |
| 864 | return IPMI_CC_PARM_OUT_OF_RANGE; |
| 865 | } |
| 866 | boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex> |
| 867 | userLock{*userMutex}; |
| 868 | UserInfo* userInfo = getUserInfo(userId); |
| 869 | std::string userName; |
| 870 | userName.assign(reinterpret_cast<char*>(userInfo->userName), 0, |
| 871 | ipmiMaxUserName); |
| 872 | if (userName.empty()) |
| 873 | { |
| 874 | log<level::DEBUG>("User name not set / invalid"); |
| 875 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 876 | } |
| 877 | std::string priv = convertToSystemPrivilege( |
| 878 | static_cast<CommandPrivilege>(privAccess.privilege)); |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 879 | uint8_t syncIndex = getUsrMgmtSyncIndex(); |
| 880 | if (chNum == syncIndex && |
| 881 | privAccess.privilege != userInfo->userPrivAccess[syncIndex].privilege) |
| 882 | { |
| 883 | std::string userPath = std::string(userObjBasePath) + "/" + userName; |
Patrick Venture | 99d1ba0 | 2019-02-21 15:11:24 -0800 | [diff] [blame] | 884 | setDbusProperty(bus, getUserServiceName(), userPath, usersInterface, |
| 885 | userPrivProperty, priv); |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 886 | } |
| 887 | userInfo->userPrivAccess[chNum].privilege = privAccess.privilege; |
| 888 | |
| 889 | if (otherPrivUpdates) |
| 890 | { |
| 891 | userInfo->userPrivAccess[chNum].ipmiEnabled = privAccess.ipmiEnabled; |
| 892 | userInfo->userPrivAccess[chNum].linkAuthEnabled = |
| 893 | privAccess.linkAuthEnabled; |
| 894 | userInfo->userPrivAccess[chNum].accessCallback = |
| 895 | privAccess.accessCallback; |
| 896 | } |
| 897 | try |
| 898 | { |
| 899 | writeUserData(); |
| 900 | } |
| 901 | catch (const std::exception& e) |
| 902 | { |
| 903 | log<level::DEBUG>("Write user data failed"); |
| 904 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 905 | } |
| 906 | return IPMI_CC_OK; |
| 907 | } |
| 908 | |
| 909 | uint8_t UserAccess::getUserId(const std::string& userName) |
| 910 | { |
| 911 | boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex> |
| 912 | userLock{*userMutex}; |
| 913 | checkAndReloadUserData(); |
| 914 | // user index 0 is reserved, starts with 1 |
| 915 | size_t usrIndex = 1; |
| 916 | for (; usrIndex <= ipmiMaxUsers; ++usrIndex) |
| 917 | { |
| 918 | std::string curName( |
| 919 | reinterpret_cast<char*>(usersTbl.user[usrIndex].userName), 0, |
| 920 | ipmiMaxUserName); |
| 921 | if (userName == curName) |
| 922 | { |
| 923 | break; // found the entry |
| 924 | } |
| 925 | } |
| 926 | if (usrIndex > ipmiMaxUsers) |
| 927 | { |
| 928 | log<level::DEBUG>("User not found", |
| 929 | entry("USER_NAME=%s", userName.c_str())); |
| 930 | return invalidUserId; |
| 931 | } |
| 932 | |
| 933 | return usrIndex; |
| 934 | } |
| 935 | |
Richard Marian Thomaiyar | a45cb34 | 2018-12-03 15:08:59 +0530 | [diff] [blame] | 936 | ipmi_ret_t UserAccess::getUserName(const uint8_t userId, std::string& userName) |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 937 | { |
| 938 | if (!isValidUserId(userId)) |
| 939 | { |
| 940 | return IPMI_CC_PARM_OUT_OF_RANGE; |
| 941 | } |
| 942 | UserInfo* userInfo = getUserInfo(userId); |
| 943 | userName.assign(reinterpret_cast<char*>(userInfo->userName), 0, |
| 944 | ipmiMaxUserName); |
| 945 | return IPMI_CC_OK; |
| 946 | } |
| 947 | |
Richard Marian Thomaiyar | a45cb34 | 2018-12-03 15:08:59 +0530 | [diff] [blame] | 948 | ipmi_ret_t UserAccess::setUserName(const uint8_t userId, |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 949 | const char* userNameInChar) |
| 950 | { |
| 951 | if (!isValidUserId(userId)) |
| 952 | { |
| 953 | return IPMI_CC_PARM_OUT_OF_RANGE; |
| 954 | } |
| 955 | |
| 956 | boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex> |
| 957 | userLock{*userMutex}; |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 958 | std::string oldUser; |
| 959 | getUserName(userId, oldUser); |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 960 | |
| 961 | std::string newUser(userNameInChar, 0, ipmiMaxUserName); |
Richard Marian Thomaiyar | 8550b60 | 2018-12-06 13:20:38 +0530 | [diff] [blame] | 962 | if (oldUser == newUser) |
| 963 | { |
| 964 | // requesting to set the same user name, return success. |
| 965 | return IPMI_CC_OK; |
| 966 | } |
| 967 | bool validUser = isValidUserName(userNameInChar); |
| 968 | UserInfo* userInfo = getUserInfo(userId); |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 969 | if (newUser.empty() && !oldUser.empty()) |
| 970 | { |
| 971 | // Delete existing user |
| 972 | std::string userPath = std::string(userObjBasePath) + "/" + oldUser; |
| 973 | try |
| 974 | { |
| 975 | auto method = bus.new_method_call( |
| 976 | getUserServiceName().c_str(), userPath.c_str(), |
| 977 | deleteUserInterface, deleteUserMethod); |
| 978 | auto reply = bus.call(method); |
| 979 | } |
| 980 | catch (const sdbusplus::exception::SdBusError& e) |
| 981 | { |
| 982 | log<level::DEBUG>("Failed to excute method", |
| 983 | entry("METHOD=%s", deleteUserMethod), |
| 984 | entry("PATH=%s", userPath.c_str())); |
| 985 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 986 | } |
Richard Marian Thomaiyar | 02710bb | 2018-11-28 20:42:25 +0530 | [diff] [blame] | 987 | deleteUserIndex(userId); |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 988 | } |
| 989 | else if (oldUser.empty() && !newUser.empty() && validUser) |
| 990 | { |
| 991 | try |
| 992 | { |
| 993 | // Create new user |
| 994 | auto method = bus.new_method_call( |
| 995 | getUserServiceName().c_str(), userMgrObjBasePath, |
| 996 | userMgrInterface, createUserMethod); |
Richard Marian Thomaiyar | 02710bb | 2018-11-28 20:42:25 +0530 | [diff] [blame] | 997 | method.append(newUser.c_str(), availableGroups, "", false); |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 998 | auto reply = bus.call(method); |
| 999 | } |
| 1000 | catch (const sdbusplus::exception::SdBusError& e) |
| 1001 | { |
| 1002 | log<level::DEBUG>("Failed to excute method", |
| 1003 | entry("METHOD=%s", createUserMethod), |
| 1004 | entry("PATH=%s", userMgrObjBasePath)); |
| 1005 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 1006 | } |
Brad Bishop | 77ff3fe | 2018-11-21 15:24:44 -0500 | [diff] [blame] | 1007 | std::memcpy(userInfo->userName, userNameInChar, ipmiMaxUserName); |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 1008 | userInfo->userInSystem = true; |
| 1009 | } |
| 1010 | else if (oldUser != newUser && validUser) |
| 1011 | { |
| 1012 | try |
| 1013 | { |
| 1014 | // User rename |
| 1015 | auto method = bus.new_method_call( |
| 1016 | getUserServiceName().c_str(), userMgrObjBasePath, |
| 1017 | userMgrInterface, renameUserMethod); |
| 1018 | method.append(oldUser.c_str(), newUser.c_str()); |
| 1019 | auto reply = bus.call(method); |
| 1020 | } |
| 1021 | catch (const sdbusplus::exception::SdBusError& e) |
| 1022 | { |
| 1023 | log<level::DEBUG>("Failed to excute method", |
| 1024 | entry("METHOD=%s", renameUserMethod), |
| 1025 | entry("PATH=%s", userMgrObjBasePath)); |
| 1026 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 1027 | } |
| 1028 | std::fill(static_cast<uint8_t*>(userInfo->userName), |
| 1029 | static_cast<uint8_t*>(userInfo->userName) + |
| 1030 | sizeof(userInfo->userName), |
| 1031 | 0); |
Brad Bishop | 77ff3fe | 2018-11-21 15:24:44 -0500 | [diff] [blame] | 1032 | std::memcpy(userInfo->userName, userNameInChar, ipmiMaxUserName); |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 1033 | ipmiRenameUserEntryPassword(oldUser, newUser); |
| 1034 | userInfo->userInSystem = true; |
| 1035 | } |
| 1036 | else if (!validUser) |
| 1037 | { |
| 1038 | return IPMI_CC_INVALID_FIELD_REQUEST; |
| 1039 | } |
| 1040 | try |
| 1041 | { |
| 1042 | writeUserData(); |
| 1043 | } |
| 1044 | catch (const std::exception& e) |
| 1045 | { |
| 1046 | log<level::DEBUG>("Write user data failed"); |
| 1047 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 1048 | } |
| 1049 | return IPMI_CC_OK; |
| 1050 | } |
| 1051 | |
| 1052 | static constexpr const char* jsonUserName = "user_name"; |
| 1053 | static constexpr const char* jsonPriv = "privilege"; |
| 1054 | static constexpr const char* jsonIpmiEnabled = "ipmi_enabled"; |
| 1055 | static constexpr const char* jsonLinkAuthEnabled = "link_auth_enabled"; |
| 1056 | static constexpr const char* jsonAccCallbk = "access_callback"; |
| 1057 | static constexpr const char* jsonUserEnabled = "user_enabled"; |
| 1058 | static constexpr const char* jsonUserInSys = "user_in_system"; |
| 1059 | static constexpr const char* jsonFixedUser = "fixed_user_name"; |
Saravanan Palanisamy | 77381f1 | 2019-05-15 22:33:17 +0000 | [diff] [blame] | 1060 | static constexpr const char* payloadEnabledStr = "payload_enabled"; |
| 1061 | static constexpr const char* stdPayloadStr = "std_payload"; |
| 1062 | static constexpr const char* oemPayloadStr = "OEM_payload"; |
| 1063 | |
| 1064 | /** @brief to construct a JSON object from the given payload access details. |
| 1065 | * |
| 1066 | * @param[in] stdPayload - stdPayloadEnables1 in a 2D-array. (input) |
| 1067 | * @param[in] oemPayload - oemPayloadEnables1 in a 2D-array. (input) |
| 1068 | * |
| 1069 | * @details Sample output JSON object format : |
| 1070 | * "payload_enabled":{ |
| 1071 | * "OEM_payload0":[false,...<repeat 'ipmiMaxChannels - 1' times>], |
| 1072 | * "OEM_payload1":[false,...<repeat 'ipmiMaxChannels - 1' times>], |
| 1073 | * "OEM_payload2":[false,...<repeat 'ipmiMaxChannels - 1' times>], |
| 1074 | * "OEM_payload3":[false,...<repeat 'ipmiMaxChannels - 1' times>], |
| 1075 | * "OEM_payload4":[false,...<repeat 'ipmiMaxChannels - 1' times>], |
| 1076 | * "OEM_payload5":[false,...<repeat 'ipmiMaxChannels - 1' times>], |
| 1077 | * "OEM_payload6":[false,...<repeat 'ipmiMaxChannels - 1' times>], |
| 1078 | * "OEM_payload7":[false,...<repeat 'ipmiMaxChannels - 1' times>], |
| 1079 | * "std_payload0":[false,...<repeat 'ipmiMaxChannels - 1' times>], |
| 1080 | * "std_payload1":[false,...<repeat 'ipmiMaxChannels - 1' times>], |
| 1081 | * "std_payload2":[false,...<repeat 'ipmiMaxChannels - 1' times>], |
| 1082 | * "std_payload3":[false,...<repeat 'ipmiMaxChannels - 1' times>], |
| 1083 | * "std_payload4":[false,...<repeat 'ipmiMaxChannels - 1' times>], |
| 1084 | * "std_payload5":[false,...<repeat 'ipmiMaxChannels - 1' times>], |
| 1085 | * "std_payload6":[false,...<repeat 'ipmiMaxChannels - 1' times>], |
| 1086 | * "std_payload7":[false,...<repeat 'ipmiMaxChannels - 1' times>], |
| 1087 | * } |
| 1088 | */ |
| 1089 | static const Json constructJsonPayloadEnables( |
| 1090 | const std::array<std::array<bool, ipmiMaxChannels>, payloadsPerByte>& |
| 1091 | stdPayload, |
| 1092 | const std::array<std::array<bool, ipmiMaxChannels>, payloadsPerByte>& |
| 1093 | oemPayload) |
| 1094 | { |
| 1095 | Json jsonPayloadEnabled; |
| 1096 | |
| 1097 | for (auto payloadNum = 0; payloadNum < payloadsPerByte; payloadNum++) |
| 1098 | { |
| 1099 | std::ostringstream stdPayloadStream; |
| 1100 | std::ostringstream oemPayloadStream; |
| 1101 | |
| 1102 | stdPayloadStream << stdPayloadStr << payloadNum; |
| 1103 | oemPayloadStream << oemPayloadStr << payloadNum; |
| 1104 | |
| 1105 | jsonPayloadEnabled.push_back(Json::object_t::value_type( |
| 1106 | stdPayloadStream.str(), stdPayload[payloadNum])); |
| 1107 | |
| 1108 | jsonPayloadEnabled.push_back(Json::object_t::value_type( |
| 1109 | oemPayloadStream.str(), oemPayload[payloadNum])); |
| 1110 | } |
| 1111 | return jsonPayloadEnabled; |
| 1112 | } |
| 1113 | |
| 1114 | void UserAccess::readPayloadAccessFromUserInfo( |
| 1115 | const UserInfo& userInfo, |
| 1116 | std::array<std::array<bool, ipmiMaxChannels>, payloadsPerByte>& stdPayload, |
| 1117 | std::array<std::array<bool, ipmiMaxChannels>, payloadsPerByte>& oemPayload) |
| 1118 | { |
| 1119 | for (auto payloadNum = 0; payloadNum < payloadsPerByte; payloadNum++) |
| 1120 | { |
| 1121 | for (auto chIndex = 0; chIndex < ipmiMaxChannels; chIndex++) |
| 1122 | { |
| 1123 | stdPayload[payloadNum][chIndex] = |
| 1124 | userInfo.payloadAccess[chIndex].stdPayloadEnables1[payloadNum]; |
| 1125 | |
| 1126 | oemPayload[payloadNum][chIndex] = |
| 1127 | userInfo.payloadAccess[chIndex].oemPayloadEnables1[payloadNum]; |
| 1128 | } |
| 1129 | } |
| 1130 | } |
| 1131 | |
| 1132 | void UserAccess::updatePayloadAccessInUserInfo( |
| 1133 | const std::array<std::array<bool, ipmiMaxChannels>, payloadsPerByte>& |
| 1134 | stdPayload, |
| 1135 | const std::array<std::array<bool, ipmiMaxChannels>, payloadsPerByte>& |
| 1136 | oemPayload, |
| 1137 | UserInfo& userInfo) |
| 1138 | { |
| 1139 | for (size_t chIndex = 0; chIndex < ipmiMaxChannels; ++chIndex) |
| 1140 | { |
| 1141 | // Ensure that reserved/unsupported payloads are marked to zero. |
| 1142 | userInfo.payloadAccess[chIndex].stdPayloadEnables1.reset(); |
| 1143 | userInfo.payloadAccess[chIndex].oemPayloadEnables1.reset(); |
| 1144 | userInfo.payloadAccess[chIndex].stdPayloadEnables2Reserved.reset(); |
| 1145 | userInfo.payloadAccess[chIndex].oemPayloadEnables2Reserved.reset(); |
| 1146 | // Update SOL status as it is the only supported payload currently. |
| 1147 | userInfo.payloadAccess[chIndex] |
| 1148 | .stdPayloadEnables1[static_cast<uint8_t>(ipmi::PayloadType::SOL)] = |
| 1149 | stdPayload[static_cast<uint8_t>(ipmi::PayloadType::SOL)][chIndex]; |
| 1150 | } |
| 1151 | } |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 1152 | |
| 1153 | void UserAccess::readUserData() |
| 1154 | { |
| 1155 | boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex> |
| 1156 | userLock{*userMutex}; |
| 1157 | |
| 1158 | std::ifstream iUsrData(ipmiUserDataFile, std::ios::in | std::ios::binary); |
| 1159 | if (!iUsrData.good()) |
| 1160 | { |
| 1161 | log<level::ERR>("Error in reading IPMI user data file"); |
| 1162 | throw std::ios_base::failure("Error opening IPMI user data file"); |
| 1163 | } |
| 1164 | |
| 1165 | Json jsonUsersTbl = Json::array(); |
| 1166 | jsonUsersTbl = Json::parse(iUsrData, nullptr, false); |
| 1167 | |
| 1168 | if (jsonUsersTbl.size() != ipmiMaxUsers) |
| 1169 | { |
| 1170 | log<level::ERR>( |
| 1171 | "Error in reading IPMI user data file - User count issues"); |
| 1172 | throw std::runtime_error( |
| 1173 | "Corrupted IPMI user data file - invalid user count"); |
| 1174 | } |
Saravanan Palanisamy | 77381f1 | 2019-05-15 22:33:17 +0000 | [diff] [blame] | 1175 | |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 1176 | // user index 0 is reserved, starts with 1 |
| 1177 | for (size_t usrIndex = 1; usrIndex <= ipmiMaxUsers; ++usrIndex) |
| 1178 | { |
| 1179 | Json userInfo = jsonUsersTbl[usrIndex - 1]; // json array starts with 0. |
| 1180 | if (userInfo.is_null()) |
| 1181 | { |
| 1182 | log<level::ERR>("Error in reading IPMI user data file - " |
| 1183 | "user info corrupted"); |
| 1184 | throw std::runtime_error( |
| 1185 | "Corrupted IPMI user data file - invalid user info"); |
| 1186 | } |
| 1187 | std::string userName = userInfo[jsonUserName].get<std::string>(); |
| 1188 | std::strncpy(reinterpret_cast<char*>(usersTbl.user[usrIndex].userName), |
| 1189 | userName.c_str(), ipmiMaxUserName); |
| 1190 | |
| 1191 | std::vector<std::string> privilege = |
| 1192 | userInfo[jsonPriv].get<std::vector<std::string>>(); |
| 1193 | std::vector<bool> ipmiEnabled = |
| 1194 | userInfo[jsonIpmiEnabled].get<std::vector<bool>>(); |
| 1195 | std::vector<bool> linkAuthEnabled = |
| 1196 | userInfo[jsonLinkAuthEnabled].get<std::vector<bool>>(); |
| 1197 | std::vector<bool> accessCallback = |
| 1198 | userInfo[jsonAccCallbk].get<std::vector<bool>>(); |
Saravanan Palanisamy | 77381f1 | 2019-05-15 22:33:17 +0000 | [diff] [blame] | 1199 | |
| 1200 | // Payload Enables Processing. |
Saravanan Palanisamy | c86045c | 2019-07-26 22:52:40 +0000 | [diff] [blame] | 1201 | std::array<std::array<bool, ipmiMaxChannels>, payloadsPerByte> |
| 1202 | stdPayload = {}; |
| 1203 | std::array<std::array<bool, ipmiMaxChannels>, payloadsPerByte> |
| 1204 | oemPayload = {}; |
| 1205 | try |
Saravanan Palanisamy | 77381f1 | 2019-05-15 22:33:17 +0000 | [diff] [blame] | 1206 | { |
Saravanan Palanisamy | c86045c | 2019-07-26 22:52:40 +0000 | [diff] [blame] | 1207 | const auto jsonPayloadEnabled = userInfo.at(payloadEnabledStr); |
| 1208 | for (auto payloadNum = 0; payloadNum < payloadsPerByte; |
| 1209 | payloadNum++) |
Saravanan Palanisamy | 77381f1 | 2019-05-15 22:33:17 +0000 | [diff] [blame] | 1210 | { |
Saravanan Palanisamy | c86045c | 2019-07-26 22:52:40 +0000 | [diff] [blame] | 1211 | std::ostringstream stdPayloadStream; |
| 1212 | std::ostringstream oemPayloadStream; |
| 1213 | |
| 1214 | stdPayloadStream << stdPayloadStr << payloadNum; |
| 1215 | oemPayloadStream << oemPayloadStr << payloadNum; |
| 1216 | |
| 1217 | stdPayload[payloadNum] = |
| 1218 | jsonPayloadEnabled[stdPayloadStream.str()] |
| 1219 | .get<std::array<bool, ipmiMaxChannels>>(); |
| 1220 | oemPayload[payloadNum] = |
| 1221 | jsonPayloadEnabled[oemPayloadStream.str()] |
| 1222 | .get<std::array<bool, ipmiMaxChannels>>(); |
| 1223 | |
| 1224 | if (stdPayload[payloadNum].size() != ipmiMaxChannels || |
| 1225 | oemPayload[payloadNum].size() != ipmiMaxChannels) |
| 1226 | { |
| 1227 | log<level::ERR>("Error in reading IPMI user data file - " |
| 1228 | "payload properties corrupted"); |
| 1229 | throw std::runtime_error( |
| 1230 | "Corrupted IPMI user data file - payload properties"); |
| 1231 | } |
Saravanan Palanisamy | 77381f1 | 2019-05-15 22:33:17 +0000 | [diff] [blame] | 1232 | } |
| 1233 | } |
Saravanan Palanisamy | c86045c | 2019-07-26 22:52:40 +0000 | [diff] [blame] | 1234 | catch (Json::out_of_range& e) |
| 1235 | { |
| 1236 | // Key not found in 'userInfo'; possibly an old JSON file. Use |
| 1237 | // default values for all payloads, and SOL payload default is true. |
| 1238 | stdPayload[static_cast<uint8_t>(ipmi::PayloadType::SOL)].fill(true); |
| 1239 | } |
Saravanan Palanisamy | 77381f1 | 2019-05-15 22:33:17 +0000 | [diff] [blame] | 1240 | |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 1241 | if (privilege.size() != ipmiMaxChannels || |
| 1242 | ipmiEnabled.size() != ipmiMaxChannels || |
| 1243 | linkAuthEnabled.size() != ipmiMaxChannels || |
| 1244 | accessCallback.size() != ipmiMaxChannels) |
| 1245 | { |
| 1246 | log<level::ERR>("Error in reading IPMI user data file - " |
| 1247 | "properties corrupted"); |
| 1248 | throw std::runtime_error( |
| 1249 | "Corrupted IPMI user data file - properties"); |
| 1250 | } |
| 1251 | for (size_t chIndex = 0; chIndex < ipmiMaxChannels; ++chIndex) |
| 1252 | { |
| 1253 | usersTbl.user[usrIndex].userPrivAccess[chIndex].privilege = |
| 1254 | static_cast<uint8_t>( |
| 1255 | convertToIPMIPrivilege(privilege[chIndex])); |
| 1256 | usersTbl.user[usrIndex].userPrivAccess[chIndex].ipmiEnabled = |
| 1257 | ipmiEnabled[chIndex]; |
| 1258 | usersTbl.user[usrIndex].userPrivAccess[chIndex].linkAuthEnabled = |
| 1259 | linkAuthEnabled[chIndex]; |
| 1260 | usersTbl.user[usrIndex].userPrivAccess[chIndex].accessCallback = |
| 1261 | accessCallback[chIndex]; |
| 1262 | } |
Saravanan Palanisamy | 77381f1 | 2019-05-15 22:33:17 +0000 | [diff] [blame] | 1263 | updatePayloadAccessInUserInfo(stdPayload, oemPayload, |
| 1264 | usersTbl.user[usrIndex]); |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 1265 | usersTbl.user[usrIndex].userEnabled = |
| 1266 | userInfo[jsonUserEnabled].get<bool>(); |
| 1267 | usersTbl.user[usrIndex].userInSystem = |
| 1268 | userInfo[jsonUserInSys].get<bool>(); |
| 1269 | usersTbl.user[usrIndex].fixedUserName = |
| 1270 | userInfo[jsonFixedUser].get<bool>(); |
| 1271 | } |
| 1272 | |
| 1273 | log<level::DEBUG>("User data read from IPMI data file"); |
| 1274 | iUsrData.close(); |
| 1275 | // Update the timestamp |
| 1276 | fileLastUpdatedTime = getUpdatedFileTime(); |
| 1277 | return; |
| 1278 | } |
| 1279 | |
| 1280 | void UserAccess::writeUserData() |
| 1281 | { |
| 1282 | boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex> |
| 1283 | userLock{*userMutex}; |
| 1284 | |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 1285 | Json jsonUsersTbl = Json::array(); |
| 1286 | // user index 0 is reserved, starts with 1 |
| 1287 | for (size_t usrIndex = 1; usrIndex <= ipmiMaxUsers; ++usrIndex) |
| 1288 | { |
| 1289 | Json jsonUserInfo; |
| 1290 | jsonUserInfo[jsonUserName] = std::string( |
| 1291 | reinterpret_cast<char*>(usersTbl.user[usrIndex].userName), 0, |
| 1292 | ipmiMaxUserName); |
| 1293 | std::vector<std::string> privilege(ipmiMaxChannels); |
| 1294 | std::vector<bool> ipmiEnabled(ipmiMaxChannels); |
| 1295 | std::vector<bool> linkAuthEnabled(ipmiMaxChannels); |
| 1296 | std::vector<bool> accessCallback(ipmiMaxChannels); |
Saravanan Palanisamy | 77381f1 | 2019-05-15 22:33:17 +0000 | [diff] [blame] | 1297 | |
| 1298 | std::array<std::array<bool, ipmiMaxChannels>, payloadsPerByte> |
| 1299 | stdPayload; |
| 1300 | std::array<std::array<bool, ipmiMaxChannels>, payloadsPerByte> |
| 1301 | oemPayload; |
| 1302 | |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 1303 | for (size_t chIndex = 0; chIndex < ipmiMaxChannels; chIndex++) |
| 1304 | { |
| 1305 | privilege[chIndex] = |
| 1306 | convertToSystemPrivilege(static_cast<CommandPrivilege>( |
| 1307 | usersTbl.user[usrIndex].userPrivAccess[chIndex].privilege)); |
| 1308 | ipmiEnabled[chIndex] = |
| 1309 | usersTbl.user[usrIndex].userPrivAccess[chIndex].ipmiEnabled; |
| 1310 | linkAuthEnabled[chIndex] = |
| 1311 | usersTbl.user[usrIndex].userPrivAccess[chIndex].linkAuthEnabled; |
| 1312 | accessCallback[chIndex] = |
| 1313 | usersTbl.user[usrIndex].userPrivAccess[chIndex].accessCallback; |
| 1314 | } |
| 1315 | jsonUserInfo[jsonPriv] = privilege; |
| 1316 | jsonUserInfo[jsonIpmiEnabled] = ipmiEnabled; |
| 1317 | jsonUserInfo[jsonLinkAuthEnabled] = linkAuthEnabled; |
| 1318 | jsonUserInfo[jsonAccCallbk] = accessCallback; |
| 1319 | jsonUserInfo[jsonUserEnabled] = usersTbl.user[usrIndex].userEnabled; |
| 1320 | jsonUserInfo[jsonUserInSys] = usersTbl.user[usrIndex].userInSystem; |
| 1321 | jsonUserInfo[jsonFixedUser] = usersTbl.user[usrIndex].fixedUserName; |
Saravanan Palanisamy | 77381f1 | 2019-05-15 22:33:17 +0000 | [diff] [blame] | 1322 | |
| 1323 | readPayloadAccessFromUserInfo(usersTbl.user[usrIndex], stdPayload, |
| 1324 | oemPayload); |
| 1325 | Json jsonPayloadEnabledInfo = |
| 1326 | constructJsonPayloadEnables(stdPayload, oemPayload); |
| 1327 | jsonUserInfo[payloadEnabledStr] = jsonPayloadEnabledInfo; |
| 1328 | |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 1329 | jsonUsersTbl.push_back(jsonUserInfo); |
| 1330 | } |
| 1331 | |
Richard Marian Thomaiyar | 687df40 | 2019-05-09 00:16:53 +0530 | [diff] [blame] | 1332 | static std::string tmpFile{std::string(ipmiUserDataFile) + "_tmp"}; |
| 1333 | int fd = open(tmpFile.c_str(), O_CREAT | O_WRONLY | O_TRUNC | O_SYNC, |
| 1334 | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); |
| 1335 | if (fd < 0) |
| 1336 | { |
| 1337 | log<level::ERR>("Error in creating temporary IPMI user data file"); |
| 1338 | throw std::ios_base::failure( |
| 1339 | "Error in creating temporary IPMI user data file"); |
| 1340 | } |
| 1341 | const auto& writeStr = jsonUsersTbl.dump(); |
| 1342 | if (write(fd, writeStr.c_str(), writeStr.size()) != |
| 1343 | static_cast<ssize_t>(writeStr.size())) |
| 1344 | { |
| 1345 | close(fd); |
| 1346 | log<level::ERR>("Error in writing temporary IPMI user data file"); |
| 1347 | throw std::ios_base::failure( |
| 1348 | "Error in writing temporary IPMI user data file"); |
| 1349 | } |
| 1350 | close(fd); |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 1351 | |
| 1352 | if (std::rename(tmpFile.c_str(), ipmiUserDataFile) != 0) |
| 1353 | { |
| 1354 | log<level::ERR>("Error in renaming temporary IPMI user data file"); |
| 1355 | throw std::runtime_error("Error in renaming IPMI user data file"); |
| 1356 | } |
| 1357 | // Update the timestamp |
| 1358 | fileLastUpdatedTime = getUpdatedFileTime(); |
| 1359 | return; |
| 1360 | } |
| 1361 | |
| 1362 | bool UserAccess::addUserEntry(const std::string& userName, |
| 1363 | const std::string& sysPriv, const bool& enabled) |
| 1364 | { |
| 1365 | UsersTbl* userData = getUsersTblPtr(); |
| 1366 | size_t freeIndex = 0xFF; |
| 1367 | // user index 0 is reserved, starts with 1 |
| 1368 | for (size_t usrIndex = 1; usrIndex <= ipmiMaxUsers; ++usrIndex) |
| 1369 | { |
| 1370 | std::string curName( |
| 1371 | reinterpret_cast<char*>(userData->user[usrIndex].userName), 0, |
| 1372 | ipmiMaxUserName); |
| 1373 | if (userName == curName) |
| 1374 | { |
| 1375 | log<level::DEBUG>("User name exists", |
| 1376 | entry("USER_NAME=%s", userName.c_str())); |
| 1377 | return false; // user name exists. |
| 1378 | } |
| 1379 | |
| 1380 | if ((!userData->user[usrIndex].userInSystem) && |
| 1381 | (userData->user[usrIndex].userName[0] == '\0') && |
| 1382 | (freeIndex == 0xFF)) |
| 1383 | { |
| 1384 | freeIndex = usrIndex; |
| 1385 | } |
| 1386 | } |
| 1387 | if (freeIndex == 0xFF) |
| 1388 | { |
| 1389 | log<level::ERR>("No empty slots found"); |
| 1390 | return false; |
| 1391 | } |
| 1392 | std::strncpy(reinterpret_cast<char*>(userData->user[freeIndex].userName), |
| 1393 | userName.c_str(), ipmiMaxUserName); |
| 1394 | uint8_t priv = |
| 1395 | static_cast<uint8_t>(UserAccess::convertToIPMIPrivilege(sysPriv)) & |
| 1396 | privMask; |
| 1397 | for (size_t chIndex = 0; chIndex < ipmiMaxChannels; ++chIndex) |
| 1398 | { |
| 1399 | userData->user[freeIndex].userPrivAccess[chIndex].privilege = priv; |
| 1400 | userData->user[freeIndex].userPrivAccess[chIndex].ipmiEnabled = true; |
| 1401 | userData->user[freeIndex].userPrivAccess[chIndex].linkAuthEnabled = |
| 1402 | true; |
| 1403 | userData->user[freeIndex].userPrivAccess[chIndex].accessCallback = true; |
| 1404 | } |
| 1405 | userData->user[freeIndex].userInSystem = true; |
| 1406 | userData->user[freeIndex].userEnabled = enabled; |
| 1407 | |
| 1408 | return true; |
| 1409 | } |
| 1410 | |
| 1411 | void UserAccess::deleteUserIndex(const size_t& usrIdx) |
| 1412 | { |
| 1413 | UsersTbl* userData = getUsersTblPtr(); |
| 1414 | |
| 1415 | std::string userName( |
| 1416 | reinterpret_cast<char*>(userData->user[usrIdx].userName), 0, |
| 1417 | ipmiMaxUserName); |
| 1418 | ipmiClearUserEntryPassword(userName); |
| 1419 | std::fill(static_cast<uint8_t*>(userData->user[usrIdx].userName), |
| 1420 | static_cast<uint8_t*>(userData->user[usrIdx].userName) + |
| 1421 | sizeof(userData->user[usrIdx].userName), |
| 1422 | 0); |
| 1423 | for (size_t chIndex = 0; chIndex < ipmiMaxChannels; ++chIndex) |
| 1424 | { |
| 1425 | userData->user[usrIdx].userPrivAccess[chIndex].privilege = privNoAccess; |
| 1426 | userData->user[usrIdx].userPrivAccess[chIndex].ipmiEnabled = false; |
| 1427 | userData->user[usrIdx].userPrivAccess[chIndex].linkAuthEnabled = false; |
| 1428 | userData->user[usrIdx].userPrivAccess[chIndex].accessCallback = false; |
| 1429 | } |
| 1430 | userData->user[usrIdx].userInSystem = false; |
| 1431 | userData->user[usrIdx].userEnabled = false; |
| 1432 | return; |
| 1433 | } |
| 1434 | |
| 1435 | void UserAccess::checkAndReloadUserData() |
| 1436 | { |
| 1437 | std::time_t updateTime = getUpdatedFileTime(); |
| 1438 | if (updateTime != fileLastUpdatedTime || updateTime == -EIO) |
| 1439 | { |
| 1440 | std::fill(reinterpret_cast<uint8_t*>(&usersTbl), |
| 1441 | reinterpret_cast<uint8_t*>(&usersTbl) + sizeof(usersTbl), 0); |
| 1442 | readUserData(); |
| 1443 | } |
| 1444 | return; |
| 1445 | } |
| 1446 | |
| 1447 | UsersTbl* UserAccess::getUsersTblPtr() |
| 1448 | { |
| 1449 | // reload data before using it. |
| 1450 | checkAndReloadUserData(); |
| 1451 | return &usersTbl; |
| 1452 | } |
| 1453 | |
| 1454 | void UserAccess::getSystemPrivAndGroups() |
| 1455 | { |
| 1456 | std::map<std::string, PrivAndGroupType> properties; |
| 1457 | try |
| 1458 | { |
| 1459 | auto method = bus.new_method_call( |
| 1460 | getUserServiceName().c_str(), userMgrObjBasePath, |
| 1461 | dBusPropertiesInterface, getAllPropertiesMethod); |
| 1462 | method.append(userMgrInterface); |
| 1463 | |
| 1464 | auto reply = bus.call(method); |
| 1465 | reply.read(properties); |
| 1466 | } |
| 1467 | catch (const sdbusplus::exception::SdBusError& e) |
| 1468 | { |
| 1469 | log<level::DEBUG>("Failed to excute method", |
| 1470 | entry("METHOD=%s", getAllPropertiesMethod), |
| 1471 | entry("PATH=%s", userMgrObjBasePath)); |
| 1472 | return; |
| 1473 | } |
| 1474 | for (const auto& t : properties) |
| 1475 | { |
| 1476 | auto key = t.first; |
| 1477 | if (key == allPrivProperty) |
| 1478 | { |
Vernon Mauery | f442e11 | 2019-04-09 11:44:36 -0700 | [diff] [blame] | 1479 | availablePrivileges = std::get<std::vector<std::string>>(t.second); |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 1480 | } |
| 1481 | else if (key == allGrpProperty) |
| 1482 | { |
Vernon Mauery | f442e11 | 2019-04-09 11:44:36 -0700 | [diff] [blame] | 1483 | availableGroups = std::get<std::vector<std::string>>(t.second); |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 1484 | } |
| 1485 | } |
| 1486 | // TODO: Implement Supported Privilege & Groups verification logic |
| 1487 | return; |
| 1488 | } |
| 1489 | |
| 1490 | std::time_t UserAccess::getUpdatedFileTime() |
| 1491 | { |
| 1492 | struct stat fileStat; |
| 1493 | if (stat(ipmiUserDataFile, &fileStat) != 0) |
| 1494 | { |
| 1495 | log<level::DEBUG>("Error in getting last updated time stamp"); |
| 1496 | return -EIO; |
| 1497 | } |
| 1498 | return fileStat.st_mtime; |
| 1499 | } |
| 1500 | |
| 1501 | void UserAccess::getUserProperties(const DbusUserObjProperties& properties, |
| 1502 | std::vector<std::string>& usrGrps, |
| 1503 | std::string& usrPriv, bool& usrEnabled) |
| 1504 | { |
| 1505 | for (const auto& t : properties) |
| 1506 | { |
| 1507 | std::string key = t.first; |
| 1508 | if (key == userPrivProperty) |
| 1509 | { |
Vernon Mauery | f442e11 | 2019-04-09 11:44:36 -0700 | [diff] [blame] | 1510 | usrPriv = std::get<std::string>(t.second); |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 1511 | } |
| 1512 | else if (key == userGrpProperty) |
| 1513 | { |
Vernon Mauery | f442e11 | 2019-04-09 11:44:36 -0700 | [diff] [blame] | 1514 | usrGrps = std::get<std::vector<std::string>>(t.second); |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 1515 | } |
| 1516 | else if (key == userEnabledProperty) |
| 1517 | { |
Vernon Mauery | f442e11 | 2019-04-09 11:44:36 -0700 | [diff] [blame] | 1518 | usrEnabled = std::get<bool>(t.second); |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 1519 | } |
| 1520 | } |
| 1521 | return; |
| 1522 | } |
| 1523 | |
| 1524 | int UserAccess::getUserObjProperties(const DbusUserObjValue& userObjs, |
| 1525 | std::vector<std::string>& usrGrps, |
| 1526 | std::string& usrPriv, bool& usrEnabled) |
| 1527 | { |
| 1528 | auto usrObj = userObjs.find(usersInterface); |
| 1529 | if (usrObj != userObjs.end()) |
| 1530 | { |
| 1531 | getUserProperties(usrObj->second, usrGrps, usrPriv, usrEnabled); |
| 1532 | return 0; |
| 1533 | } |
| 1534 | return -EIO; |
| 1535 | } |
| 1536 | |
arun-pm | bbe728c | 2020-01-10 15:18:04 +0530 | [diff] [blame^] | 1537 | void UserAccess::cacheUserDataFile() |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 1538 | { |
| 1539 | boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex> |
| 1540 | userLock{*userMutex}; |
| 1541 | try |
| 1542 | { |
| 1543 | readUserData(); |
| 1544 | } |
| 1545 | catch (const std::ios_base::failure& e) |
| 1546 | { // File is empty, create it for the first time |
| 1547 | std::fill(reinterpret_cast<uint8_t*>(&usersTbl), |
| 1548 | reinterpret_cast<uint8_t*>(&usersTbl) + sizeof(usersTbl), 0); |
| 1549 | // user index 0 is reserved, starts with 1 |
| 1550 | for (size_t userIndex = 1; userIndex <= ipmiMaxUsers; ++userIndex) |
| 1551 | { |
| 1552 | for (size_t chIndex = 0; chIndex < ipmiMaxChannels; ++chIndex) |
| 1553 | { |
| 1554 | usersTbl.user[userIndex].userPrivAccess[chIndex].privilege = |
| 1555 | privNoAccess; |
Saravanan Palanisamy | 92d8119 | 2019-08-07 18:00:04 +0000 | [diff] [blame] | 1556 | usersTbl.user[userIndex] |
| 1557 | .payloadAccess[chIndex] |
| 1558 | .stdPayloadEnables1[static_cast<uint8_t>( |
| 1559 | ipmi::PayloadType::SOL)] = true; |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 1560 | } |
| 1561 | } |
| 1562 | writeUserData(); |
| 1563 | } |
arun-pm | bbe728c | 2020-01-10 15:18:04 +0530 | [diff] [blame^] | 1564 | sigHndlrLock = boost::interprocess::file_lock(ipmiUserDataFile); |
| 1565 | // Register it for single object and single process either netipimd / |
| 1566 | // host-ipmid |
| 1567 | if (userUpdatedSignal == nullptr && sigHndlrLock.try_lock()) |
| 1568 | { |
| 1569 | log<level::DEBUG>("Registering signal handler"); |
| 1570 | userUpdatedSignal = std::make_unique<sdbusplus::bus::match_t>( |
| 1571 | bus, |
| 1572 | sdbusplus::bus::match::rules::type::signal() + |
| 1573 | sdbusplus::bus::match::rules::interface(dBusObjManager) + |
| 1574 | sdbusplus::bus::match::rules::path(userMgrObjBasePath), |
| 1575 | [&](sdbusplus::message::message& msg) { |
| 1576 | userUpdatedSignalHandler(*this, msg); |
| 1577 | }); |
| 1578 | userMgrRenamedSignal = std::make_unique<sdbusplus::bus::match_t>( |
| 1579 | bus, |
| 1580 | sdbusplus::bus::match::rules::type::signal() + |
| 1581 | sdbusplus::bus::match::rules::interface(userMgrInterface) + |
| 1582 | sdbusplus::bus::match::rules::path(userMgrObjBasePath), |
| 1583 | [&](sdbusplus::message::message& msg) { |
| 1584 | userUpdatedSignalHandler(*this, msg); |
| 1585 | }); |
| 1586 | userPropertiesSignal = std::make_unique<sdbusplus::bus::match_t>( |
| 1587 | bus, |
| 1588 | sdbusplus::bus::match::rules::type::signal() + |
| 1589 | sdbusplus::bus::match::rules::path_namespace(userObjBasePath) + |
| 1590 | sdbusplus::bus::match::rules::interface( |
| 1591 | dBusPropertiesInterface) + |
| 1592 | sdbusplus::bus::match::rules::member(propertiesChangedSignal) + |
| 1593 | sdbusplus::bus::match::rules::argN(0, usersInterface), |
| 1594 | [&](sdbusplus::message::message& msg) { |
| 1595 | userUpdatedSignalHandler(*this, msg); |
| 1596 | }); |
| 1597 | signalHndlrObject = true; |
| 1598 | } |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 1599 | std::map<DbusUserObjPath, DbusUserObjValue> managedObjs; |
| 1600 | try |
| 1601 | { |
| 1602 | auto method = bus.new_method_call(getUserServiceName().c_str(), |
| 1603 | userMgrObjBasePath, dBusObjManager, |
| 1604 | getManagedObjectsMethod); |
| 1605 | auto reply = bus.call(method); |
| 1606 | reply.read(managedObjs); |
| 1607 | } |
| 1608 | catch (const sdbusplus::exception::SdBusError& e) |
| 1609 | { |
| 1610 | log<level::DEBUG>("Failed to excute method", |
| 1611 | entry("METHOD=%s", getSubTreeMethod), |
| 1612 | entry("PATH=%s", userMgrObjBasePath)); |
| 1613 | return; |
| 1614 | } |
Richard Marian Thomaiyar | e004e22 | 2019-05-09 00:37:55 +0530 | [diff] [blame] | 1615 | bool updateRequired = false; |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 1616 | UsersTbl* userData = &usersTbl; |
| 1617 | // user index 0 is reserved, starts with 1 |
| 1618 | for (size_t usrIdx = 1; usrIdx <= ipmiMaxUsers; ++usrIdx) |
| 1619 | { |
| 1620 | if ((userData->user[usrIdx].userInSystem) && |
| 1621 | (userData->user[usrIdx].userName[0] != '\0')) |
| 1622 | { |
| 1623 | std::vector<std::string> usrGrps; |
| 1624 | std::string usrPriv; |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 1625 | |
| 1626 | std::string userName( |
| 1627 | reinterpret_cast<char*>(userData->user[usrIdx].userName), 0, |
| 1628 | ipmiMaxUserName); |
| 1629 | std::string usersPath = |
| 1630 | std::string(userObjBasePath) + "/" + userName; |
| 1631 | |
| 1632 | auto usrObj = managedObjs.find(usersPath); |
| 1633 | if (usrObj != managedObjs.end()) |
| 1634 | { |
Chen,Yugang | 0e862fa | 2019-09-06 11:03:05 +0800 | [diff] [blame] | 1635 | bool usrEnabled = false; |
Patrick Venture | 3a697ad | 2019-08-19 11:12:05 -0700 | [diff] [blame] | 1636 | |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 1637 | // User exist. Lets check and update other fileds |
| 1638 | getUserObjProperties(usrObj->second, usrGrps, usrPriv, |
| 1639 | usrEnabled); |
| 1640 | if (std::find(usrGrps.begin(), usrGrps.end(), ipmiGrpName) == |
| 1641 | usrGrps.end()) |
| 1642 | { |
Richard Marian Thomaiyar | e004e22 | 2019-05-09 00:37:55 +0530 | [diff] [blame] | 1643 | updateRequired = true; |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 1644 | // Group "ipmi" is removed so lets remove user in IPMI |
| 1645 | deleteUserIndex(usrIdx); |
| 1646 | } |
| 1647 | else |
| 1648 | { |
| 1649 | // Group "ipmi" is present so lets update other properties |
| 1650 | // in IPMI |
| 1651 | uint8_t priv = |
| 1652 | UserAccess::convertToIPMIPrivilege(usrPriv) & privMask; |
| 1653 | // Update all channels priv, only if it is not equivalent to |
| 1654 | // getUsrMgmtSyncIndex() |
| 1655 | if (userData->user[usrIdx] |
| 1656 | .userPrivAccess[getUsrMgmtSyncIndex()] |
| 1657 | .privilege != priv) |
| 1658 | { |
Richard Marian Thomaiyar | e004e22 | 2019-05-09 00:37:55 +0530 | [diff] [blame] | 1659 | updateRequired = true; |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 1660 | for (size_t chIndex = 0; chIndex < ipmiMaxChannels; |
| 1661 | ++chIndex) |
| 1662 | { |
| 1663 | userData->user[usrIdx] |
| 1664 | .userPrivAccess[chIndex] |
| 1665 | .privilege = priv; |
| 1666 | } |
| 1667 | } |
| 1668 | if (userData->user[usrIdx].userEnabled != usrEnabled) |
| 1669 | { |
Richard Marian Thomaiyar | e004e22 | 2019-05-09 00:37:55 +0530 | [diff] [blame] | 1670 | updateRequired = true; |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 1671 | userData->user[usrIdx].userEnabled = usrEnabled; |
| 1672 | } |
| 1673 | } |
| 1674 | |
| 1675 | // We are done with this obj. lets delete from MAP |
| 1676 | managedObjs.erase(usrObj); |
| 1677 | } |
| 1678 | else |
| 1679 | { |
Richard Marian Thomaiyar | e004e22 | 2019-05-09 00:37:55 +0530 | [diff] [blame] | 1680 | updateRequired = true; |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 1681 | deleteUserIndex(usrIdx); |
| 1682 | } |
| 1683 | } |
| 1684 | } |
| 1685 | |
| 1686 | // Walk through remnaining managedObj users list |
| 1687 | // Add them to ipmi data base |
| 1688 | for (const auto& usrObj : managedObjs) |
| 1689 | { |
| 1690 | std::vector<std::string> usrGrps; |
| 1691 | std::string usrPriv, userName; |
Chen,Yugang | 0e862fa | 2019-09-06 11:03:05 +0800 | [diff] [blame] | 1692 | bool usrEnabled = false; |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 1693 | std::string usrObjPath = std::string(usrObj.first); |
| 1694 | if (getUserNameFromPath(usrObj.first.str, userName) != 0) |
| 1695 | { |
| 1696 | log<level::ERR>("Error in user object path"); |
| 1697 | continue; |
| 1698 | } |
| 1699 | getUserObjProperties(usrObj.second, usrGrps, usrPriv, usrEnabled); |
| 1700 | // Add 'ipmi' group users |
| 1701 | if (std::find(usrGrps.begin(), usrGrps.end(), ipmiGrpName) != |
| 1702 | usrGrps.end()) |
| 1703 | { |
Richard Marian Thomaiyar | e004e22 | 2019-05-09 00:37:55 +0530 | [diff] [blame] | 1704 | updateRequired = true; |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 1705 | // CREATE NEW USER |
| 1706 | if (true != addUserEntry(userName, usrPriv, usrEnabled)) |
| 1707 | { |
| 1708 | break; |
| 1709 | } |
| 1710 | } |
| 1711 | } |
| 1712 | |
Richard Marian Thomaiyar | e004e22 | 2019-05-09 00:37:55 +0530 | [diff] [blame] | 1713 | if (updateRequired) |
| 1714 | { |
| 1715 | // All userData slots update done. Lets write the data |
| 1716 | writeUserData(); |
| 1717 | } |
Richard Marian Thomaiyar | 5a6b636 | 2018-03-12 23:42:34 +0530 | [diff] [blame] | 1718 | |
| 1719 | return; |
| 1720 | } |
| 1721 | } // namespace ipmi |