blob: 7a11ff30444e5afe9b45d3cb8743a793368eadb8 [file] [log] [blame]
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301/*
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"
19
20#include <sys/stat.h>
21#include <unistd.h>
22
23#include <boost/interprocess/sync/named_recursive_mutex.hpp>
24#include <boost/interprocess/sync/scoped_lock.hpp>
25#include <cerrno>
26#include <fstream>
27#include <host-ipmid/ipmid-host-cmd.hpp>
28#include <nlohmann/json.hpp>
29#include <phosphor-logging/elog-errors.hpp>
30#include <phosphor-logging/log.hpp>
31#include <regex>
32#include <sdbusplus/bus/match.hpp>
33#include <sdbusplus/server/object.hpp>
34#include <xyz/openbmc_project/Common/error.hpp>
35#include <xyz/openbmc_project/User/Common/error.hpp>
36
37namespace ipmi
38{
39
40// TODO: Move D-Bus & Object Manager related stuff, to common files
41// D-Bus property related
42static constexpr const char* dBusPropertiesInterface =
43 "org.freedesktop.DBus.Properties";
44static constexpr const char* getAllPropertiesMethod = "GetAll";
45static constexpr const char* propertiesChangedSignal = "PropertiesChanged";
46static constexpr const char* setPropertiesMethod = "Set";
47
48// Object Manager related
49static constexpr const char* dBusObjManager =
50 "org.freedesktop.DBus.ObjectManager";
51static constexpr const char* getManagedObjectsMethod = "GetManagedObjects";
52// Object Manager signals
53static constexpr const char* intfAddedSignal = "InterfacesAdded";
54static constexpr const char* intfRemovedSignal = "InterfacesRemoved";
55
56// Object Mapper related
57static constexpr const char* objMapperService =
58 "xyz.openbmc_project.ObjectMapper";
59static constexpr const char* objMapperPath =
60 "/xyz/openbmc_project/object_mapper";
61static constexpr const char* objMapperInterface =
62 "xyz.openbmc_project.ObjectMapper";
63static constexpr const char* getSubTreeMethod = "GetSubTree";
64static constexpr const char* getObjectMethod = "GetObject";
65
66static constexpr const char* ipmiUserMutex = "ipmi_usr_mutex";
67static constexpr const char* ipmiMutexCleanupLockFile =
68 "/var/lib/ipmi/ipmi_usr_mutex_cleanup";
69static constexpr const char* ipmiUserDataFile = "/var/lib/ipmi/ipmi_user.json";
70static constexpr const char* ipmiGrpName = "ipmi";
71static constexpr size_t privNoAccess = 0xF;
72static constexpr size_t privMask = 0xF;
73
74// User manager related
75static constexpr const char* userMgrObjBasePath = "/xyz/openbmc_project/user";
76static constexpr const char* userObjBasePath = "/xyz/openbmc_project/user";
77static constexpr const char* userMgrInterface =
78 "xyz.openbmc_project.User.Manager";
79static constexpr const char* usersInterface =
80 "xyz.openbmc_project.User.Attributes";
81static constexpr const char* deleteUserInterface =
82 "xyz.openbmc_project.Object.Delete";
83
84static constexpr const char* createUserMethod = "CreateUser";
85static constexpr const char* deleteUserMethod = "Delete";
86static constexpr const char* renameUserMethod = "RenameUser";
87// User manager signal memebers
88static constexpr const char* userRenamedSignal = "UserRenamed";
89// Mgr interface properties
90static constexpr const char* allPrivProperty = "AllPrivileges";
91static constexpr const char* allGrpProperty = "AllGroups";
92// User interface properties
93static constexpr const char* userPrivProperty = "UserPrivilege";
94static constexpr const char* userGrpProperty = "UserGroups";
95static constexpr const char* userEnabledProperty = "UserEnabled";
96
97static std::array<std::string, (PRIVILEGE_OEM + 1)> ipmiPrivIndex = {
98 "priv-reserved", // PRIVILEGE_RESERVED - 0
99 "priv-callback", // PRIVILEGE_CALLBACK - 1
100 "priv-user", // PRIVILEGE_USER - 2
101 "priv-operator", // PRIVILEGE_OPERATOR - 3
102 "priv-admin", // PRIVILEGE_ADMIN - 4
103 "priv-custom" // PRIVILEGE_OEM - 5
104};
105
William A. Kennington IIIdfad4862018-11-19 17:45:35 -0800106namespace variant_ns = sdbusplus::message::variant_ns;
107
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530108using namespace phosphor::logging;
109using Json = nlohmann::json;
110
111using PrivAndGroupType =
112 sdbusplus::message::variant<std::string, std::vector<std::string>>;
113
114using NoResource =
115 sdbusplus::xyz::openbmc_project::User::Common::Error::NoResource;
116
117using InternalFailure =
118 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
119
120std::unique_ptr<sdbusplus::bus::match_t> userUpdatedSignal(nullptr);
121std::unique_ptr<sdbusplus::bus::match_t> userMgrRenamedSignal(nullptr);
122std::unique_ptr<sdbusplus::bus::match_t> userPropertiesSignal(nullptr);
123
124// TODO: Below code can be removed once it is moved to common layer libmiscutil
125std::string getUserService(sdbusplus::bus::bus& bus, const std::string& intf,
126 const std::string& path)
127{
128 auto mapperCall = bus.new_method_call(objMapperService, objMapperPath,
129 objMapperInterface, getObjectMethod);
130
131 mapperCall.append(path);
132 mapperCall.append(std::vector<std::string>({intf}));
133
134 auto mapperResponseMsg = bus.call(mapperCall);
135
136 std::map<std::string, std::vector<std::string>> mapperResponse;
137 mapperResponseMsg.read(mapperResponse);
138
139 if (mapperResponse.begin() == mapperResponse.end())
140 {
141 throw sdbusplus::exception::SdBusError(
142 -EIO, "ERROR in reading the mapper response");
143 }
144
145 return mapperResponse.begin()->first;
146}
147
148void setDbusProperty(sdbusplus::bus::bus& bus, const std::string& service,
149 const std::string& objPath, const std::string& interface,
150 const std::string& property,
151 const DbusUserPropVariant& value)
152{
153 try
154 {
155 auto method =
156 bus.new_method_call(service.c_str(), objPath.c_str(),
157 dBusPropertiesInterface, setPropertiesMethod);
158 method.append(interface, property, value);
159 bus.call(method);
160 }
161 catch (const sdbusplus::exception::SdBusError& e)
162 {
163 log<level::ERR>("Failed to set property",
164 entry("PROPERTY=%s", property.c_str()),
165 entry("PATH=%s", objPath.c_str()),
166 entry("INTERFACE=%s", interface.c_str()));
167 throw;
168 }
169}
170
171static std::string getUserServiceName()
172{
173 static sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
174 static std::string userMgmtService;
175 if (userMgmtService.empty())
176 {
177 try
178 {
179 userMgmtService =
180 ipmi::getUserService(bus, userMgrInterface, userMgrObjBasePath);
181 }
182 catch (const sdbusplus::exception::SdBusError& e)
183 {
184 userMgmtService.clear();
185 }
186 }
187 return userMgmtService;
188}
189
190UserAccess& getUserAccessObject()
191{
192 static UserAccess userAccess;
193 return userAccess;
194}
195
196int getUserNameFromPath(const std::string& path, std::string& userName)
197{
198 static size_t pos = strlen(userObjBasePath) + 1;
199 if (path.find(userObjBasePath) == std::string::npos)
200 {
201 return -EINVAL;
202 }
203 userName.assign(path, pos, path.size());
204 return 0;
205}
206
207void userUpdateHelper(UserAccess& usrAccess, const UserUpdateEvent& userEvent,
208 const std::string& userName, const std::string& priv,
209 const bool& enabled, const std::string& newUserName)
210{
211 UsersTbl* userData = usrAccess.getUsersTblPtr();
212 if (userEvent == UserUpdateEvent::userCreated)
213 {
214 if (usrAccess.addUserEntry(userName, priv, enabled) == false)
215 {
216 return;
217 }
218 }
219 else
220 {
221 // user index 0 is reserved, starts with 1
222 size_t usrIndex = 1;
223 for (; usrIndex <= ipmiMaxUsers; ++usrIndex)
224 {
225 std::string curName(
226 reinterpret_cast<char*>(userData->user[usrIndex].userName), 0,
227 ipmiMaxUserName);
228 if (userName == curName)
229 {
230 break; // found the entry
231 }
232 }
233 if (usrIndex > ipmiMaxUsers)
234 {
235 log<level::DEBUG>("User not found for signal",
236 entry("USER_NAME=%s", userName.c_str()),
237 entry("USER_EVENT=%d", userEvent));
238 return;
239 }
240 switch (userEvent)
241 {
242 case UserUpdateEvent::userDeleted:
243 {
244 usrAccess.deleteUserIndex(usrIndex);
245 break;
246 }
247 case UserUpdateEvent::userPrivUpdated:
248 {
249 uint8_t userPriv =
250 static_cast<uint8_t>(
251 UserAccess::convertToIPMIPrivilege(priv)) &
252 privMask;
253 // Update all channels privileges, only if it is not equivalent
254 // to getUsrMgmtSyncIndex()
255 if (userData->user[usrIndex]
256 .userPrivAccess[UserAccess::getUsrMgmtSyncIndex()]
257 .privilege != userPriv)
258 {
259 for (size_t chIndex = 0; chIndex < ipmiMaxChannels;
260 ++chIndex)
261 {
262 userData->user[usrIndex]
263 .userPrivAccess[chIndex]
264 .privilege = userPriv;
265 }
266 }
267 break;
268 }
269 case UserUpdateEvent::userRenamed:
270 {
271 std::fill(
272 static_cast<uint8_t*>(userData->user[usrIndex].userName),
273 static_cast<uint8_t*>(userData->user[usrIndex].userName) +
274 sizeof(userData->user[usrIndex].userName),
275 0);
276 std::strncpy(
277 reinterpret_cast<char*>(userData->user[usrIndex].userName),
278 newUserName.c_str(), ipmiMaxUserName);
279 ipmiRenameUserEntryPassword(userName, newUserName);
280 break;
281 }
282 case UserUpdateEvent::userStateUpdated:
283 {
284 userData->user[usrIndex].userEnabled = enabled;
285 break;
286 }
287 default:
288 {
289 log<level::ERR>("Unhandled user event",
290 entry("USER_EVENT=%d", userEvent));
291 return;
292 }
293 }
294 }
295 usrAccess.writeUserData();
296 log<level::DEBUG>("User event handled successfully",
297 entry("USER_NAME=%s", userName.c_str()),
298 entry("USER_EVENT=%d", userEvent));
299
300 return;
301}
302
303void userUpdatedSignalHandler(UserAccess& usrAccess,
304 sdbusplus::message::message& msg)
305{
306 static sdbusplus::bus::bus bus(ipmid_get_sd_bus_connection());
307 std::string signal = msg.get_member();
308 std::string userName, update, priv, newUserName;
309 std::vector<std::string> groups;
310 bool enabled = false;
311 UserUpdateEvent userEvent = UserUpdateEvent::reservedEvent;
312 if (signal == intfAddedSignal)
313 {
314 DbusUserObjPath objPath;
315 DbusUserObjValue objValue;
316 msg.read(objPath, objValue);
317 getUserNameFromPath(objPath.str, userName);
318 if (usrAccess.getUserObjProperties(objValue, groups, priv, enabled) !=
319 0)
320 {
321 return;
322 }
323 if (std::find(groups.begin(), groups.end(), ipmiGrpName) ==
324 groups.end())
325 {
326 return;
327 }
328 userEvent = UserUpdateEvent::userCreated;
329 }
330 else if (signal == intfRemovedSignal)
331 {
332 DbusUserObjPath objPath;
333 std::vector<std::string> interfaces;
334 msg.read(objPath, interfaces);
335 getUserNameFromPath(objPath.str, userName);
336 userEvent = UserUpdateEvent::userDeleted;
337 }
338 else if (signal == userRenamedSignal)
339 {
340 msg.read(userName, newUserName);
341 userEvent = UserUpdateEvent::userRenamed;
342 }
343 else if (signal == propertiesChangedSignal)
344 {
345 getUserNameFromPath(msg.get_path(), userName);
346 }
347 else
348 {
349 log<level::ERR>("Unknown user update signal",
350 entry("SIGNAL=%s", signal.c_str()));
351 return;
352 }
353
354 if (signal.empty() || userName.empty() ||
355 (signal == userRenamedSignal && newUserName.empty()))
356 {
357 log<level::ERR>("Invalid inputs received");
358 return;
359 }
360
361 boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex>
362 userLock{*(usrAccess.userMutex)};
363 usrAccess.checkAndReloadUserData();
364
365 if (signal == propertiesChangedSignal)
366 {
367 std::string intfName;
368 DbusUserObjProperties chProperties;
369 msg.read(intfName, chProperties); // skip reading 3rd argument.
370 for (const auto& prop : chProperties)
371 {
372 userEvent = UserUpdateEvent::reservedEvent;
373 std::string member = prop.first;
374 if (member == userPrivProperty)
375 {
William A. Kennington IIIdfad4862018-11-19 17:45:35 -0800376 priv = variant_ns::get<std::string>(prop.second);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530377 userEvent = UserUpdateEvent::userPrivUpdated;
378 }
379 else if (member == userGrpProperty)
380 {
William A. Kennington IIIdfad4862018-11-19 17:45:35 -0800381 groups = variant_ns::get<std::vector<std::string>>(prop.second);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530382 userEvent = UserUpdateEvent::userGrpUpdated;
383 }
384 else if (member == userEnabledProperty)
385 {
William A. Kennington IIIdfad4862018-11-19 17:45:35 -0800386 enabled = variant_ns::get<bool>(prop.second);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530387 userEvent = UserUpdateEvent::userStateUpdated;
388 }
389 // Process based on event type.
390 if (userEvent == UserUpdateEvent::userGrpUpdated)
391 {
392 if (std::find(groups.begin(), groups.end(), ipmiGrpName) ==
393 groups.end())
394 {
395 // remove user from ipmi user list.
396 userUpdateHelper(usrAccess, UserUpdateEvent::userDeleted,
397 userName, priv, enabled, newUserName);
398 }
399 else
400 {
401 DbusUserObjProperties properties;
402 try
403 {
404 auto method = bus.new_method_call(
405 getUserServiceName().c_str(), msg.get_path(),
406 dBusPropertiesInterface, getAllPropertiesMethod);
407 method.append(usersInterface);
408 auto reply = bus.call(method);
409 reply.read(properties);
410 }
411 catch (const sdbusplus::exception::SdBusError& e)
412 {
413 log<level::DEBUG>(
414 "Failed to excute method",
415 entry("METHOD=%s", getAllPropertiesMethod),
416 entry("PATH=%s", msg.get_path()));
417 return;
418 }
419 usrAccess.getUserProperties(properties, groups, priv,
420 enabled);
421 // add user to ipmi user list.
422 userUpdateHelper(usrAccess, UserUpdateEvent::userCreated,
423 userName, priv, enabled, newUserName);
424 }
425 }
426 else if (userEvent != UserUpdateEvent::reservedEvent)
427 {
428 userUpdateHelper(usrAccess, userEvent, userName, priv, enabled,
429 newUserName);
430 }
431 }
432 }
433 else if (userEvent != UserUpdateEvent::reservedEvent)
434 {
435 userUpdateHelper(usrAccess, userEvent, userName, priv, enabled,
436 newUserName);
437 }
438 return;
439}
440
441UserAccess::~UserAccess()
442{
443 if (signalHndlrObject)
444 {
445 userUpdatedSignal.reset();
446 userMgrRenamedSignal.reset();
447 userPropertiesSignal.reset();
448 sigHndlrLock.unlock();
449 }
450}
451
452UserAccess::UserAccess() : bus(ipmid_get_sd_bus_connection())
453{
454 std::ofstream mutexCleanUpFile;
455 mutexCleanUpFile.open(ipmiMutexCleanupLockFile,
456 std::ofstream::out | std::ofstream::app);
457 if (!mutexCleanUpFile.good())
458 {
459 log<level::DEBUG>("Unable to open mutex cleanup file");
460 return;
461 }
462 mutexCleanUpFile.close();
463 mutexCleanupLock = boost::interprocess::file_lock(ipmiMutexCleanupLockFile);
464 if (mutexCleanupLock.try_lock())
465 {
466 boost::interprocess::named_recursive_mutex::remove(ipmiUserMutex);
467 }
468 mutexCleanupLock.lock_sharable();
469 userMutex = std::make_unique<boost::interprocess::named_recursive_mutex>(
470 boost::interprocess::open_or_create, ipmiUserMutex);
471
472 initUserDataFile();
473 getSystemPrivAndGroups();
474 sigHndlrLock = boost::interprocess::file_lock(ipmiUserDataFile);
475 // Register it for single object and single process either netipimd /
476 // host-ipmid
477 if (userUpdatedSignal == nullptr && sigHndlrLock.try_lock())
478 {
479 log<level::DEBUG>("Registering signal handler");
480 userUpdatedSignal = std::make_unique<sdbusplus::bus::match_t>(
481 bus,
482 sdbusplus::bus::match::rules::type::signal() +
483 sdbusplus::bus::match::rules::interface(dBusObjManager) +
484 sdbusplus::bus::match::rules::path(userMgrObjBasePath),
485 [&](sdbusplus::message::message& msg) {
486 userUpdatedSignalHandler(*this, msg);
487 });
488 userMgrRenamedSignal = std::make_unique<sdbusplus::bus::match_t>(
489 bus,
490 sdbusplus::bus::match::rules::type::signal() +
491 sdbusplus::bus::match::rules::interface(userMgrInterface) +
492 sdbusplus::bus::match::rules::path(userMgrObjBasePath),
493 [&](sdbusplus::message::message& msg) {
494 userUpdatedSignalHandler(*this, msg);
495 });
496 userPropertiesSignal = std::make_unique<sdbusplus::bus::match_t>(
497 bus,
498 sdbusplus::bus::match::rules::type::signal() +
499 sdbusplus::bus::match::rules::path_namespace(userObjBasePath) +
500 sdbusplus::bus::match::rules::interface(
501 dBusPropertiesInterface) +
502 sdbusplus::bus::match::rules::member(propertiesChangedSignal) +
503 sdbusplus::bus::match::rules::argN(0, usersInterface),
504 [&](sdbusplus::message::message& msg) {
505 userUpdatedSignalHandler(*this, msg);
506 });
507 signalHndlrObject = true;
508 }
509}
510
511UserInfo* UserAccess::getUserInfo(const uint8_t& userId)
512{
513 checkAndReloadUserData();
514 return &usersTbl.user[userId];
515}
516
517void UserAccess::setUserInfo(const uint8_t& userId, UserInfo* userInfo)
518{
519 checkAndReloadUserData();
520 std::copy(reinterpret_cast<uint8_t*>(userInfo),
521 reinterpret_cast<uint8_t*>(userInfo) + sizeof(*userInfo),
522 reinterpret_cast<uint8_t*>(&usersTbl.user[userId]));
523 writeUserData();
524}
525
526bool UserAccess::isValidChannel(const uint8_t& chNum)
527{
528 return (chNum < ipmiMaxChannels);
529}
530
531bool UserAccess::isValidUserId(const uint8_t& userId)
532{
533 return ((userId <= ipmiMaxUsers) && (userId != reservedUserId));
534}
535
536bool UserAccess::isValidPrivilege(const uint8_t& priv)
537{
538 return ((priv >= PRIVILEGE_CALLBACK && priv <= PRIVILEGE_OEM) ||
539 priv == privNoAccess);
540}
541
542uint8_t UserAccess::getUsrMgmtSyncIndex()
543{
544 // TODO: Need to get LAN1 channel number dynamically,
545 // which has to be in sync with system user privilege
546 // level(Phosphor-user-manager). Note: For time being chanLan1 is marked as
547 // sync index to the user-manager privilege..
548 return static_cast<uint8_t>(EChannelID::chanLan1);
549}
550
551CommandPrivilege UserAccess::convertToIPMIPrivilege(const std::string& value)
552{
553 auto iter = std::find(ipmiPrivIndex.begin(), ipmiPrivIndex.end(), value);
554 if (iter == ipmiPrivIndex.end())
555 {
556 if (value == "")
557 {
558 return static_cast<CommandPrivilege>(privNoAccess);
559 }
560 log<level::ERR>("Error in converting to IPMI privilege",
561 entry("PRIV=%s", value.c_str()));
562 throw std::out_of_range("Out of range - convertToIPMIPrivilege");
563 }
564 else
565 {
566 return static_cast<CommandPrivilege>(
567 std::distance(ipmiPrivIndex.begin(), iter));
568 }
569}
570
571std::string UserAccess::convertToSystemPrivilege(const CommandPrivilege& value)
572{
573 if (value == static_cast<CommandPrivilege>(privNoAccess))
574 {
575 return "";
576 }
577 try
578 {
579 return ipmiPrivIndex.at(value);
580 }
581 catch (const std::out_of_range& e)
582 {
583 log<level::ERR>("Error in converting to system privilege",
584 entry("PRIV=%d", static_cast<uint8_t>(value)));
585 throw std::out_of_range("Out of range - convertToSystemPrivilege");
586 }
587}
588
589bool UserAccess::isValidUserName(const char* userNameInChar)
590{
591 if (!userNameInChar)
592 {
593 log<level::ERR>("null ptr");
594 return false;
595 }
596 std::string userName(userNameInChar, 0, ipmiMaxUserName);
597 if (!std::regex_match(userName.c_str(),
598 std::regex("[a-zA-z_][a-zA-Z_0-9]*")))
599 {
600 log<level::ERR>("Unsupported characters in user name");
601 return false;
602 }
603 if (userName == "root")
604 {
605 log<level::ERR>("Invalid user name - root");
606 return false;
607 }
608 std::map<DbusUserObjPath, DbusUserObjValue> properties;
609 try
610 {
611 auto method = bus.new_method_call(getUserServiceName().c_str(),
612 userMgrObjBasePath, dBusObjManager,
613 getManagedObjectsMethod);
614 auto reply = bus.call(method);
615 reply.read(properties);
616 }
617 catch (const sdbusplus::exception::SdBusError& e)
618 {
619 log<level::ERR>("Failed to excute method",
620 entry("METHOD=%s", getSubTreeMethod),
621 entry("PATH=%s", userMgrObjBasePath));
622 return false;
623 }
624
625 std::string usersPath = std::string(userObjBasePath) + "/" + userName;
626 if (properties.find(usersPath) != properties.end())
627 {
628 log<level::DEBUG>("User name already exists",
629 entry("USER_NAME=%s", userName.c_str()));
630 return false;
631 }
632
633 return true;
634}
635
Richard Marian Thomaiyar282e79b2018-11-13 19:00:58 +0530636ipmi_ret_t UserAccess::setUserEnabledState(const uint8_t& userId,
637 const bool& enabledState)
638{
639 if (!isValidUserId(userId))
640 {
641 return IPMI_CC_PARM_OUT_OF_RANGE;
642 }
643 boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex>
644 userLock{*userMutex};
645 UserInfo* userInfo = getUserInfo(userId);
646 std::string userName;
647 userName.assign(reinterpret_cast<char*>(userInfo->userName), 0,
648 ipmiMaxUserName);
649 if (userName.empty())
650 {
651 log<level::DEBUG>("User name not set / invalid");
652 return IPMI_CC_UNSPECIFIED_ERROR;
653 }
654 if (userInfo->userEnabled != enabledState)
655 {
656 std::string userPath = std::string(userObjBasePath) + "/" + userName;
657 setDbusProperty(bus, getUserServiceName().c_str(), userPath.c_str(),
658 usersInterface, userEnabledProperty, enabledState);
659 }
660 return IPMI_CC_OK;
661}
662
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530663ipmi_ret_t UserAccess::setUserPrivilegeAccess(const uint8_t& userId,
664 const uint8_t& chNum,
665 const UserPrivAccess& privAccess,
666 const bool& otherPrivUpdates)
667{
668 if (!isValidChannel(chNum))
669 {
670 return IPMI_CC_INVALID_FIELD_REQUEST;
671 }
672 if (!isValidUserId(userId))
673 {
674 return IPMI_CC_PARM_OUT_OF_RANGE;
675 }
676 boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex>
677 userLock{*userMutex};
678 UserInfo* userInfo = getUserInfo(userId);
679 std::string userName;
680 userName.assign(reinterpret_cast<char*>(userInfo->userName), 0,
681 ipmiMaxUserName);
682 if (userName.empty())
683 {
684 log<level::DEBUG>("User name not set / invalid");
685 return IPMI_CC_UNSPECIFIED_ERROR;
686 }
687 std::string priv = convertToSystemPrivilege(
688 static_cast<CommandPrivilege>(privAccess.privilege));
689 if (priv.empty())
690 {
691 return IPMI_CC_PARM_OUT_OF_RANGE;
692 }
693 uint8_t syncIndex = getUsrMgmtSyncIndex();
694 if (chNum == syncIndex &&
695 privAccess.privilege != userInfo->userPrivAccess[syncIndex].privilege)
696 {
697 std::string userPath = std::string(userObjBasePath) + "/" + userName;
698 setDbusProperty(bus, getUserServiceName().c_str(), userPath.c_str(),
699 usersInterface, userPrivProperty, priv);
700 }
701 userInfo->userPrivAccess[chNum].privilege = privAccess.privilege;
702
703 if (otherPrivUpdates)
704 {
705 userInfo->userPrivAccess[chNum].ipmiEnabled = privAccess.ipmiEnabled;
706 userInfo->userPrivAccess[chNum].linkAuthEnabled =
707 privAccess.linkAuthEnabled;
708 userInfo->userPrivAccess[chNum].accessCallback =
709 privAccess.accessCallback;
710 }
711 try
712 {
713 writeUserData();
714 }
715 catch (const std::exception& e)
716 {
717 log<level::DEBUG>("Write user data failed");
718 return IPMI_CC_UNSPECIFIED_ERROR;
719 }
720 return IPMI_CC_OK;
721}
722
723uint8_t UserAccess::getUserId(const std::string& userName)
724{
725 boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex>
726 userLock{*userMutex};
727 checkAndReloadUserData();
728 // user index 0 is reserved, starts with 1
729 size_t usrIndex = 1;
730 for (; usrIndex <= ipmiMaxUsers; ++usrIndex)
731 {
732 std::string curName(
733 reinterpret_cast<char*>(usersTbl.user[usrIndex].userName), 0,
734 ipmiMaxUserName);
735 if (userName == curName)
736 {
737 break; // found the entry
738 }
739 }
740 if (usrIndex > ipmiMaxUsers)
741 {
742 log<level::DEBUG>("User not found",
743 entry("USER_NAME=%s", userName.c_str()));
744 return invalidUserId;
745 }
746
747 return usrIndex;
748}
749
750ipmi_ret_t UserAccess::getUserName(const uint8_t& userId, std::string& userName)
751{
752 if (!isValidUserId(userId))
753 {
754 return IPMI_CC_PARM_OUT_OF_RANGE;
755 }
756 UserInfo* userInfo = getUserInfo(userId);
757 userName.assign(reinterpret_cast<char*>(userInfo->userName), 0,
758 ipmiMaxUserName);
759 return IPMI_CC_OK;
760}
761
762ipmi_ret_t UserAccess::setUserName(const uint8_t& userId,
763 const char* userNameInChar)
764{
765 if (!isValidUserId(userId))
766 {
767 return IPMI_CC_PARM_OUT_OF_RANGE;
768 }
769
770 boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex>
771 userLock{*userMutex};
772 bool validUser = isValidUserName(userNameInChar);
773 std::string oldUser;
774 getUserName(userId, oldUser);
775 UserInfo* userInfo = getUserInfo(userId);
776
777 std::string newUser(userNameInChar, 0, ipmiMaxUserName);
778 if (newUser.empty() && !oldUser.empty())
779 {
780 // Delete existing user
781 std::string userPath = std::string(userObjBasePath) + "/" + oldUser;
782 try
783 {
784 auto method = bus.new_method_call(
785 getUserServiceName().c_str(), userPath.c_str(),
786 deleteUserInterface, deleteUserMethod);
787 auto reply = bus.call(method);
788 }
789 catch (const sdbusplus::exception::SdBusError& e)
790 {
791 log<level::DEBUG>("Failed to excute method",
792 entry("METHOD=%s", deleteUserMethod),
793 entry("PATH=%s", userPath.c_str()));
794 return IPMI_CC_UNSPECIFIED_ERROR;
795 }
796 std::fill(userInfo->userName,
797 userInfo->userName + sizeof(userInfo->userName), 0);
798 ipmiClearUserEntryPassword(oldUser);
799 userInfo->userInSystem = false;
800 }
801 else if (oldUser.empty() && !newUser.empty() && validUser)
802 {
803 try
804 {
805 // Create new user
806 auto method = bus.new_method_call(
807 getUserServiceName().c_str(), userMgrObjBasePath,
808 userMgrInterface, createUserMethod);
809 // TODO: Fetch proper privilege & enable state once set User access
810 // is implemented if LAN Channel specified, then create user for all
811 // groups follow channel privilege for user creation.
812 method.append(newUser.c_str(), availableGroups, "priv-admin", true);
813 auto reply = bus.call(method);
814 }
815 catch (const sdbusplus::exception::SdBusError& e)
816 {
817 log<level::DEBUG>("Failed to excute method",
818 entry("METHOD=%s", createUserMethod),
819 entry("PATH=%s", userMgrObjBasePath));
820 return IPMI_CC_UNSPECIFIED_ERROR;
821 }
822 std::strncpy(reinterpret_cast<char*>(userInfo->userName),
823 userNameInChar, ipmiMaxUserName);
824 userInfo->userInSystem = true;
825 }
826 else if (oldUser != newUser && validUser)
827 {
828 try
829 {
830 // User rename
831 auto method = bus.new_method_call(
832 getUserServiceName().c_str(), userMgrObjBasePath,
833 userMgrInterface, renameUserMethod);
834 method.append(oldUser.c_str(), newUser.c_str());
835 auto reply = bus.call(method);
836 }
837 catch (const sdbusplus::exception::SdBusError& e)
838 {
839 log<level::DEBUG>("Failed to excute method",
840 entry("METHOD=%s", renameUserMethod),
841 entry("PATH=%s", userMgrObjBasePath));
842 return IPMI_CC_UNSPECIFIED_ERROR;
843 }
844 std::fill(static_cast<uint8_t*>(userInfo->userName),
845 static_cast<uint8_t*>(userInfo->userName) +
846 sizeof(userInfo->userName),
847 0);
848 std::strncpy(reinterpret_cast<char*>(userInfo->userName),
849 userNameInChar, ipmiMaxUserName);
850 ipmiRenameUserEntryPassword(oldUser, newUser);
851 userInfo->userInSystem = true;
852 }
853 else if (!validUser)
854 {
855 return IPMI_CC_INVALID_FIELD_REQUEST;
856 }
857 try
858 {
859 writeUserData();
860 }
861 catch (const std::exception& e)
862 {
863 log<level::DEBUG>("Write user data failed");
864 return IPMI_CC_UNSPECIFIED_ERROR;
865 }
866 return IPMI_CC_OK;
867}
868
869static constexpr const char* jsonUserName = "user_name";
870static constexpr const char* jsonPriv = "privilege";
871static constexpr const char* jsonIpmiEnabled = "ipmi_enabled";
872static constexpr const char* jsonLinkAuthEnabled = "link_auth_enabled";
873static constexpr const char* jsonAccCallbk = "access_callback";
874static constexpr const char* jsonUserEnabled = "user_enabled";
875static constexpr const char* jsonUserInSys = "user_in_system";
876static constexpr const char* jsonFixedUser = "fixed_user_name";
877
878void UserAccess::readUserData()
879{
880 boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex>
881 userLock{*userMutex};
882
883 std::ifstream iUsrData(ipmiUserDataFile, std::ios::in | std::ios::binary);
884 if (!iUsrData.good())
885 {
886 log<level::ERR>("Error in reading IPMI user data file");
887 throw std::ios_base::failure("Error opening IPMI user data file");
888 }
889
890 Json jsonUsersTbl = Json::array();
891 jsonUsersTbl = Json::parse(iUsrData, nullptr, false);
892
893 if (jsonUsersTbl.size() != ipmiMaxUsers)
894 {
895 log<level::ERR>(
896 "Error in reading IPMI user data file - User count issues");
897 throw std::runtime_error(
898 "Corrupted IPMI user data file - invalid user count");
899 }
900 // user index 0 is reserved, starts with 1
901 for (size_t usrIndex = 1; usrIndex <= ipmiMaxUsers; ++usrIndex)
902 {
903 Json userInfo = jsonUsersTbl[usrIndex - 1]; // json array starts with 0.
904 if (userInfo.is_null())
905 {
906 log<level::ERR>("Error in reading IPMI user data file - "
907 "user info corrupted");
908 throw std::runtime_error(
909 "Corrupted IPMI user data file - invalid user info");
910 }
911 std::string userName = userInfo[jsonUserName].get<std::string>();
912 std::strncpy(reinterpret_cast<char*>(usersTbl.user[usrIndex].userName),
913 userName.c_str(), ipmiMaxUserName);
914
915 std::vector<std::string> privilege =
916 userInfo[jsonPriv].get<std::vector<std::string>>();
917 std::vector<bool> ipmiEnabled =
918 userInfo[jsonIpmiEnabled].get<std::vector<bool>>();
919 std::vector<bool> linkAuthEnabled =
920 userInfo[jsonLinkAuthEnabled].get<std::vector<bool>>();
921 std::vector<bool> accessCallback =
922 userInfo[jsonAccCallbk].get<std::vector<bool>>();
923 if (privilege.size() != ipmiMaxChannels ||
924 ipmiEnabled.size() != ipmiMaxChannels ||
925 linkAuthEnabled.size() != ipmiMaxChannels ||
926 accessCallback.size() != ipmiMaxChannels)
927 {
928 log<level::ERR>("Error in reading IPMI user data file - "
929 "properties corrupted");
930 throw std::runtime_error(
931 "Corrupted IPMI user data file - properties");
932 }
933 for (size_t chIndex = 0; chIndex < ipmiMaxChannels; ++chIndex)
934 {
935 usersTbl.user[usrIndex].userPrivAccess[chIndex].privilege =
936 static_cast<uint8_t>(
937 convertToIPMIPrivilege(privilege[chIndex]));
938 usersTbl.user[usrIndex].userPrivAccess[chIndex].ipmiEnabled =
939 ipmiEnabled[chIndex];
940 usersTbl.user[usrIndex].userPrivAccess[chIndex].linkAuthEnabled =
941 linkAuthEnabled[chIndex];
942 usersTbl.user[usrIndex].userPrivAccess[chIndex].accessCallback =
943 accessCallback[chIndex];
944 }
945 usersTbl.user[usrIndex].userEnabled =
946 userInfo[jsonUserEnabled].get<bool>();
947 usersTbl.user[usrIndex].userInSystem =
948 userInfo[jsonUserInSys].get<bool>();
949 usersTbl.user[usrIndex].fixedUserName =
950 userInfo[jsonFixedUser].get<bool>();
951 }
952
953 log<level::DEBUG>("User data read from IPMI data file");
954 iUsrData.close();
955 // Update the timestamp
956 fileLastUpdatedTime = getUpdatedFileTime();
957 return;
958}
959
960void UserAccess::writeUserData()
961{
962 boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex>
963 userLock{*userMutex};
964
965 static std::string tmpFile{std::string(ipmiUserDataFile) + "_tmp"};
966 std::ofstream oUsrData(tmpFile, std::ios::out | std::ios::binary);
967 if (!oUsrData.good())
968 {
969 log<level::ERR>("Error in creating temporary IPMI user data file");
970 throw std::ios_base::failure(
971 "Error in creating temporary IPMI user data file");
972 }
973
974 Json jsonUsersTbl = Json::array();
975 // user index 0 is reserved, starts with 1
976 for (size_t usrIndex = 1; usrIndex <= ipmiMaxUsers; ++usrIndex)
977 {
978 Json jsonUserInfo;
979 jsonUserInfo[jsonUserName] = std::string(
980 reinterpret_cast<char*>(usersTbl.user[usrIndex].userName), 0,
981 ipmiMaxUserName);
982 std::vector<std::string> privilege(ipmiMaxChannels);
983 std::vector<bool> ipmiEnabled(ipmiMaxChannels);
984 std::vector<bool> linkAuthEnabled(ipmiMaxChannels);
985 std::vector<bool> accessCallback(ipmiMaxChannels);
986 for (size_t chIndex = 0; chIndex < ipmiMaxChannels; chIndex++)
987 {
988 privilege[chIndex] =
989 convertToSystemPrivilege(static_cast<CommandPrivilege>(
990 usersTbl.user[usrIndex].userPrivAccess[chIndex].privilege));
991 ipmiEnabled[chIndex] =
992 usersTbl.user[usrIndex].userPrivAccess[chIndex].ipmiEnabled;
993 linkAuthEnabled[chIndex] =
994 usersTbl.user[usrIndex].userPrivAccess[chIndex].linkAuthEnabled;
995 accessCallback[chIndex] =
996 usersTbl.user[usrIndex].userPrivAccess[chIndex].accessCallback;
997 }
998 jsonUserInfo[jsonPriv] = privilege;
999 jsonUserInfo[jsonIpmiEnabled] = ipmiEnabled;
1000 jsonUserInfo[jsonLinkAuthEnabled] = linkAuthEnabled;
1001 jsonUserInfo[jsonAccCallbk] = accessCallback;
1002 jsonUserInfo[jsonUserEnabled] = usersTbl.user[usrIndex].userEnabled;
1003 jsonUserInfo[jsonUserInSys] = usersTbl.user[usrIndex].userInSystem;
1004 jsonUserInfo[jsonFixedUser] = usersTbl.user[usrIndex].fixedUserName;
1005 jsonUsersTbl.push_back(jsonUserInfo);
1006 }
1007
1008 oUsrData << jsonUsersTbl;
1009 oUsrData.flush();
1010 oUsrData.close();
1011
1012 if (std::rename(tmpFile.c_str(), ipmiUserDataFile) != 0)
1013 {
1014 log<level::ERR>("Error in renaming temporary IPMI user data file");
1015 throw std::runtime_error("Error in renaming IPMI user data file");
1016 }
1017 // Update the timestamp
1018 fileLastUpdatedTime = getUpdatedFileTime();
1019 return;
1020}
1021
1022bool UserAccess::addUserEntry(const std::string& userName,
1023 const std::string& sysPriv, const bool& enabled)
1024{
1025 UsersTbl* userData = getUsersTblPtr();
1026 size_t freeIndex = 0xFF;
1027 // user index 0 is reserved, starts with 1
1028 for (size_t usrIndex = 1; usrIndex <= ipmiMaxUsers; ++usrIndex)
1029 {
1030 std::string curName(
1031 reinterpret_cast<char*>(userData->user[usrIndex].userName), 0,
1032 ipmiMaxUserName);
1033 if (userName == curName)
1034 {
1035 log<level::DEBUG>("User name exists",
1036 entry("USER_NAME=%s", userName.c_str()));
1037 return false; // user name exists.
1038 }
1039
1040 if ((!userData->user[usrIndex].userInSystem) &&
1041 (userData->user[usrIndex].userName[0] == '\0') &&
1042 (freeIndex == 0xFF))
1043 {
1044 freeIndex = usrIndex;
1045 }
1046 }
1047 if (freeIndex == 0xFF)
1048 {
1049 log<level::ERR>("No empty slots found");
1050 return false;
1051 }
1052 std::strncpy(reinterpret_cast<char*>(userData->user[freeIndex].userName),
1053 userName.c_str(), ipmiMaxUserName);
1054 uint8_t priv =
1055 static_cast<uint8_t>(UserAccess::convertToIPMIPrivilege(sysPriv)) &
1056 privMask;
1057 for (size_t chIndex = 0; chIndex < ipmiMaxChannels; ++chIndex)
1058 {
1059 userData->user[freeIndex].userPrivAccess[chIndex].privilege = priv;
1060 userData->user[freeIndex].userPrivAccess[chIndex].ipmiEnabled = true;
1061 userData->user[freeIndex].userPrivAccess[chIndex].linkAuthEnabled =
1062 true;
1063 userData->user[freeIndex].userPrivAccess[chIndex].accessCallback = true;
1064 }
1065 userData->user[freeIndex].userInSystem = true;
1066 userData->user[freeIndex].userEnabled = enabled;
1067
1068 return true;
1069}
1070
1071void UserAccess::deleteUserIndex(const size_t& usrIdx)
1072{
1073 UsersTbl* userData = getUsersTblPtr();
1074
1075 std::string userName(
1076 reinterpret_cast<char*>(userData->user[usrIdx].userName), 0,
1077 ipmiMaxUserName);
1078 ipmiClearUserEntryPassword(userName);
1079 std::fill(static_cast<uint8_t*>(userData->user[usrIdx].userName),
1080 static_cast<uint8_t*>(userData->user[usrIdx].userName) +
1081 sizeof(userData->user[usrIdx].userName),
1082 0);
1083 for (size_t chIndex = 0; chIndex < ipmiMaxChannels; ++chIndex)
1084 {
1085 userData->user[usrIdx].userPrivAccess[chIndex].privilege = privNoAccess;
1086 userData->user[usrIdx].userPrivAccess[chIndex].ipmiEnabled = false;
1087 userData->user[usrIdx].userPrivAccess[chIndex].linkAuthEnabled = false;
1088 userData->user[usrIdx].userPrivAccess[chIndex].accessCallback = false;
1089 }
1090 userData->user[usrIdx].userInSystem = false;
1091 userData->user[usrIdx].userEnabled = false;
1092 return;
1093}
1094
1095void UserAccess::checkAndReloadUserData()
1096{
1097 std::time_t updateTime = getUpdatedFileTime();
1098 if (updateTime != fileLastUpdatedTime || updateTime == -EIO)
1099 {
1100 std::fill(reinterpret_cast<uint8_t*>(&usersTbl),
1101 reinterpret_cast<uint8_t*>(&usersTbl) + sizeof(usersTbl), 0);
1102 readUserData();
1103 }
1104 return;
1105}
1106
1107UsersTbl* UserAccess::getUsersTblPtr()
1108{
1109 // reload data before using it.
1110 checkAndReloadUserData();
1111 return &usersTbl;
1112}
1113
1114void UserAccess::getSystemPrivAndGroups()
1115{
1116 std::map<std::string, PrivAndGroupType> properties;
1117 try
1118 {
1119 auto method = bus.new_method_call(
1120 getUserServiceName().c_str(), userMgrObjBasePath,
1121 dBusPropertiesInterface, getAllPropertiesMethod);
1122 method.append(userMgrInterface);
1123
1124 auto reply = bus.call(method);
1125 reply.read(properties);
1126 }
1127 catch (const sdbusplus::exception::SdBusError& e)
1128 {
1129 log<level::DEBUG>("Failed to excute method",
1130 entry("METHOD=%s", getAllPropertiesMethod),
1131 entry("PATH=%s", userMgrObjBasePath));
1132 return;
1133 }
1134 for (const auto& t : properties)
1135 {
1136 auto key = t.first;
1137 if (key == allPrivProperty)
1138 {
William A. Kennington IIIdfad4862018-11-19 17:45:35 -08001139 availablePrivileges =
1140 variant_ns::get<std::vector<std::string>>(t.second);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301141 }
1142 else if (key == allGrpProperty)
1143 {
William A. Kennington IIIdfad4862018-11-19 17:45:35 -08001144 availableGroups =
1145 variant_ns::get<std::vector<std::string>>(t.second);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301146 }
1147 }
1148 // TODO: Implement Supported Privilege & Groups verification logic
1149 return;
1150}
1151
1152std::time_t UserAccess::getUpdatedFileTime()
1153{
1154 struct stat fileStat;
1155 if (stat(ipmiUserDataFile, &fileStat) != 0)
1156 {
1157 log<level::DEBUG>("Error in getting last updated time stamp");
1158 return -EIO;
1159 }
1160 return fileStat.st_mtime;
1161}
1162
1163void UserAccess::getUserProperties(const DbusUserObjProperties& properties,
1164 std::vector<std::string>& usrGrps,
1165 std::string& usrPriv, bool& usrEnabled)
1166{
1167 for (const auto& t : properties)
1168 {
1169 std::string key = t.first;
1170 if (key == userPrivProperty)
1171 {
William A. Kennington IIIdfad4862018-11-19 17:45:35 -08001172 usrPriv = variant_ns::get<std::string>(t.second);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301173 }
1174 else if (key == userGrpProperty)
1175 {
William A. Kennington IIIdfad4862018-11-19 17:45:35 -08001176 usrGrps = variant_ns::get<std::vector<std::string>>(t.second);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301177 }
1178 else if (key == userEnabledProperty)
1179 {
William A. Kennington IIIdfad4862018-11-19 17:45:35 -08001180 usrEnabled = variant_ns::get<bool>(t.second);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301181 }
1182 }
1183 return;
1184}
1185
1186int UserAccess::getUserObjProperties(const DbusUserObjValue& userObjs,
1187 std::vector<std::string>& usrGrps,
1188 std::string& usrPriv, bool& usrEnabled)
1189{
1190 auto usrObj = userObjs.find(usersInterface);
1191 if (usrObj != userObjs.end())
1192 {
1193 getUserProperties(usrObj->second, usrGrps, usrPriv, usrEnabled);
1194 return 0;
1195 }
1196 return -EIO;
1197}
1198
1199void UserAccess::initUserDataFile()
1200{
1201 boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex>
1202 userLock{*userMutex};
1203 try
1204 {
1205 readUserData();
1206 }
1207 catch (const std::ios_base::failure& e)
1208 { // File is empty, create it for the first time
1209 std::fill(reinterpret_cast<uint8_t*>(&usersTbl),
1210 reinterpret_cast<uint8_t*>(&usersTbl) + sizeof(usersTbl), 0);
1211 // user index 0 is reserved, starts with 1
1212 for (size_t userIndex = 1; userIndex <= ipmiMaxUsers; ++userIndex)
1213 {
1214 for (size_t chIndex = 0; chIndex < ipmiMaxChannels; ++chIndex)
1215 {
1216 usersTbl.user[userIndex].userPrivAccess[chIndex].privilege =
1217 privNoAccess;
1218 }
1219 }
1220 writeUserData();
1221 }
1222 std::map<DbusUserObjPath, DbusUserObjValue> managedObjs;
1223 try
1224 {
1225 auto method = bus.new_method_call(getUserServiceName().c_str(),
1226 userMgrObjBasePath, dBusObjManager,
1227 getManagedObjectsMethod);
1228 auto reply = bus.call(method);
1229 reply.read(managedObjs);
1230 }
1231 catch (const sdbusplus::exception::SdBusError& e)
1232 {
1233 log<level::DEBUG>("Failed to excute method",
1234 entry("METHOD=%s", getSubTreeMethod),
1235 entry("PATH=%s", userMgrObjBasePath));
1236 return;
1237 }
1238
1239 UsersTbl* userData = &usersTbl;
1240 // user index 0 is reserved, starts with 1
1241 for (size_t usrIdx = 1; usrIdx <= ipmiMaxUsers; ++usrIdx)
1242 {
1243 if ((userData->user[usrIdx].userInSystem) &&
1244 (userData->user[usrIdx].userName[0] != '\0'))
1245 {
1246 std::vector<std::string> usrGrps;
1247 std::string usrPriv;
1248 bool usrEnabled;
1249
1250 std::string userName(
1251 reinterpret_cast<char*>(userData->user[usrIdx].userName), 0,
1252 ipmiMaxUserName);
1253 std::string usersPath =
1254 std::string(userObjBasePath) + "/" + userName;
1255
1256 auto usrObj = managedObjs.find(usersPath);
1257 if (usrObj != managedObjs.end())
1258 {
1259 // User exist. Lets check and update other fileds
1260 getUserObjProperties(usrObj->second, usrGrps, usrPriv,
1261 usrEnabled);
1262 if (std::find(usrGrps.begin(), usrGrps.end(), ipmiGrpName) ==
1263 usrGrps.end())
1264 {
1265 // Group "ipmi" is removed so lets remove user in IPMI
1266 deleteUserIndex(usrIdx);
1267 }
1268 else
1269 {
1270 // Group "ipmi" is present so lets update other properties
1271 // in IPMI
1272 uint8_t priv =
1273 UserAccess::convertToIPMIPrivilege(usrPriv) & privMask;
1274 // Update all channels priv, only if it is not equivalent to
1275 // getUsrMgmtSyncIndex()
1276 if (userData->user[usrIdx]
1277 .userPrivAccess[getUsrMgmtSyncIndex()]
1278 .privilege != priv)
1279 {
1280 for (size_t chIndex = 0; chIndex < ipmiMaxChannels;
1281 ++chIndex)
1282 {
1283 userData->user[usrIdx]
1284 .userPrivAccess[chIndex]
1285 .privilege = priv;
1286 }
1287 }
1288 if (userData->user[usrIdx].userEnabled != usrEnabled)
1289 {
1290 userData->user[usrIdx].userEnabled = usrEnabled;
1291 }
1292 }
1293
1294 // We are done with this obj. lets delete from MAP
1295 managedObjs.erase(usrObj);
1296 }
1297 else
1298 {
1299 deleteUserIndex(usrIdx);
1300 }
1301 }
1302 }
1303
1304 // Walk through remnaining managedObj users list
1305 // Add them to ipmi data base
1306 for (const auto& usrObj : managedObjs)
1307 {
1308 std::vector<std::string> usrGrps;
1309 std::string usrPriv, userName;
1310 bool usrEnabled;
1311 std::string usrObjPath = std::string(usrObj.first);
1312 if (getUserNameFromPath(usrObj.first.str, userName) != 0)
1313 {
1314 log<level::ERR>("Error in user object path");
1315 continue;
1316 }
1317 getUserObjProperties(usrObj.second, usrGrps, usrPriv, usrEnabled);
1318 // Add 'ipmi' group users
1319 if (std::find(usrGrps.begin(), usrGrps.end(), ipmiGrpName) !=
1320 usrGrps.end())
1321 {
1322 // CREATE NEW USER
1323 if (true != addUserEntry(userName, usrPriv, usrEnabled))
1324 {
1325 break;
1326 }
1327 }
1328 }
1329
1330 // All userData slots update done. Lets write the data
1331 writeUserData();
1332
1333 return;
1334}
1335} // namespace ipmi