blob: 8ba4a94d65604ac67aea0328b940b08eaee89425 [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
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530511UserInfo* UserAccess::getUserInfo(const uint8_t userId)
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530512{
513 checkAndReloadUserData();
514 return &usersTbl.user[userId];
515}
516
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530517void UserAccess::setUserInfo(const uint8_t userId, UserInfo* userInfo)
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530518{
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
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530526bool UserAccess::isValidChannel(const uint8_t chNum)
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530527{
528 return (chNum < ipmiMaxChannels);
529}
530
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530531bool UserAccess::isValidUserId(const uint8_t userId)
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530532{
533 return ((userId <= ipmiMaxUsers) && (userId != reservedUserId));
534}
535
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530536bool UserAccess::isValidPrivilege(const uint8_t priv)
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530537{
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 Thomaiyara45cb342018-12-03 15:08:59 +0530636ipmi_ret_t UserAccess::setUserEnabledState(const uint8_t userId,
Richard Marian Thomaiyar282e79b2018-11-13 19:00:58 +0530637 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 Thomaiyara45cb342018-12-03 15:08:59 +0530663ipmi_ret_t UserAccess::setUserPrivilegeAccess(const uint8_t userId,
664 const uint8_t chNum,
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530665 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
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530750ipmi_ret_t UserAccess::getUserName(const uint8_t userId, std::string& userName)
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530751{
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
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530762ipmi_ret_t UserAccess::setUserName(const uint8_t userId,
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530763 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 }
Brad Bishop77ff3fe2018-11-21 15:24:44 -0500822 std::memcpy(userInfo->userName, userNameInChar, ipmiMaxUserName);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530823 userInfo->userInSystem = true;
824 }
825 else if (oldUser != newUser && validUser)
826 {
827 try
828 {
829 // User rename
830 auto method = bus.new_method_call(
831 getUserServiceName().c_str(), userMgrObjBasePath,
832 userMgrInterface, renameUserMethod);
833 method.append(oldUser.c_str(), newUser.c_str());
834 auto reply = bus.call(method);
835 }
836 catch (const sdbusplus::exception::SdBusError& e)
837 {
838 log<level::DEBUG>("Failed to excute method",
839 entry("METHOD=%s", renameUserMethod),
840 entry("PATH=%s", userMgrObjBasePath));
841 return IPMI_CC_UNSPECIFIED_ERROR;
842 }
843 std::fill(static_cast<uint8_t*>(userInfo->userName),
844 static_cast<uint8_t*>(userInfo->userName) +
845 sizeof(userInfo->userName),
846 0);
Brad Bishop77ff3fe2018-11-21 15:24:44 -0500847 std::memcpy(userInfo->userName, userNameInChar, ipmiMaxUserName);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530848 ipmiRenameUserEntryPassword(oldUser, newUser);
849 userInfo->userInSystem = true;
850 }
851 else if (!validUser)
852 {
853 return IPMI_CC_INVALID_FIELD_REQUEST;
854 }
855 try
856 {
857 writeUserData();
858 }
859 catch (const std::exception& e)
860 {
861 log<level::DEBUG>("Write user data failed");
862 return IPMI_CC_UNSPECIFIED_ERROR;
863 }
864 return IPMI_CC_OK;
865}
866
867static constexpr const char* jsonUserName = "user_name";
868static constexpr const char* jsonPriv = "privilege";
869static constexpr const char* jsonIpmiEnabled = "ipmi_enabled";
870static constexpr const char* jsonLinkAuthEnabled = "link_auth_enabled";
871static constexpr const char* jsonAccCallbk = "access_callback";
872static constexpr const char* jsonUserEnabled = "user_enabled";
873static constexpr const char* jsonUserInSys = "user_in_system";
874static constexpr const char* jsonFixedUser = "fixed_user_name";
875
876void UserAccess::readUserData()
877{
878 boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex>
879 userLock{*userMutex};
880
881 std::ifstream iUsrData(ipmiUserDataFile, std::ios::in | std::ios::binary);
882 if (!iUsrData.good())
883 {
884 log<level::ERR>("Error in reading IPMI user data file");
885 throw std::ios_base::failure("Error opening IPMI user data file");
886 }
887
888 Json jsonUsersTbl = Json::array();
889 jsonUsersTbl = Json::parse(iUsrData, nullptr, false);
890
891 if (jsonUsersTbl.size() != ipmiMaxUsers)
892 {
893 log<level::ERR>(
894 "Error in reading IPMI user data file - User count issues");
895 throw std::runtime_error(
896 "Corrupted IPMI user data file - invalid user count");
897 }
898 // user index 0 is reserved, starts with 1
899 for (size_t usrIndex = 1; usrIndex <= ipmiMaxUsers; ++usrIndex)
900 {
901 Json userInfo = jsonUsersTbl[usrIndex - 1]; // json array starts with 0.
902 if (userInfo.is_null())
903 {
904 log<level::ERR>("Error in reading IPMI user data file - "
905 "user info corrupted");
906 throw std::runtime_error(
907 "Corrupted IPMI user data file - invalid user info");
908 }
909 std::string userName = userInfo[jsonUserName].get<std::string>();
910 std::strncpy(reinterpret_cast<char*>(usersTbl.user[usrIndex].userName),
911 userName.c_str(), ipmiMaxUserName);
912
913 std::vector<std::string> privilege =
914 userInfo[jsonPriv].get<std::vector<std::string>>();
915 std::vector<bool> ipmiEnabled =
916 userInfo[jsonIpmiEnabled].get<std::vector<bool>>();
917 std::vector<bool> linkAuthEnabled =
918 userInfo[jsonLinkAuthEnabled].get<std::vector<bool>>();
919 std::vector<bool> accessCallback =
920 userInfo[jsonAccCallbk].get<std::vector<bool>>();
921 if (privilege.size() != ipmiMaxChannels ||
922 ipmiEnabled.size() != ipmiMaxChannels ||
923 linkAuthEnabled.size() != ipmiMaxChannels ||
924 accessCallback.size() != ipmiMaxChannels)
925 {
926 log<level::ERR>("Error in reading IPMI user data file - "
927 "properties corrupted");
928 throw std::runtime_error(
929 "Corrupted IPMI user data file - properties");
930 }
931 for (size_t chIndex = 0; chIndex < ipmiMaxChannels; ++chIndex)
932 {
933 usersTbl.user[usrIndex].userPrivAccess[chIndex].privilege =
934 static_cast<uint8_t>(
935 convertToIPMIPrivilege(privilege[chIndex]));
936 usersTbl.user[usrIndex].userPrivAccess[chIndex].ipmiEnabled =
937 ipmiEnabled[chIndex];
938 usersTbl.user[usrIndex].userPrivAccess[chIndex].linkAuthEnabled =
939 linkAuthEnabled[chIndex];
940 usersTbl.user[usrIndex].userPrivAccess[chIndex].accessCallback =
941 accessCallback[chIndex];
942 }
943 usersTbl.user[usrIndex].userEnabled =
944 userInfo[jsonUserEnabled].get<bool>();
945 usersTbl.user[usrIndex].userInSystem =
946 userInfo[jsonUserInSys].get<bool>();
947 usersTbl.user[usrIndex].fixedUserName =
948 userInfo[jsonFixedUser].get<bool>();
949 }
950
951 log<level::DEBUG>("User data read from IPMI data file");
952 iUsrData.close();
953 // Update the timestamp
954 fileLastUpdatedTime = getUpdatedFileTime();
955 return;
956}
957
958void UserAccess::writeUserData()
959{
960 boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex>
961 userLock{*userMutex};
962
963 static std::string tmpFile{std::string(ipmiUserDataFile) + "_tmp"};
964 std::ofstream oUsrData(tmpFile, std::ios::out | std::ios::binary);
965 if (!oUsrData.good())
966 {
967 log<level::ERR>("Error in creating temporary IPMI user data file");
968 throw std::ios_base::failure(
969 "Error in creating temporary IPMI user data file");
970 }
971
972 Json jsonUsersTbl = Json::array();
973 // user index 0 is reserved, starts with 1
974 for (size_t usrIndex = 1; usrIndex <= ipmiMaxUsers; ++usrIndex)
975 {
976 Json jsonUserInfo;
977 jsonUserInfo[jsonUserName] = std::string(
978 reinterpret_cast<char*>(usersTbl.user[usrIndex].userName), 0,
979 ipmiMaxUserName);
980 std::vector<std::string> privilege(ipmiMaxChannels);
981 std::vector<bool> ipmiEnabled(ipmiMaxChannels);
982 std::vector<bool> linkAuthEnabled(ipmiMaxChannels);
983 std::vector<bool> accessCallback(ipmiMaxChannels);
984 for (size_t chIndex = 0; chIndex < ipmiMaxChannels; chIndex++)
985 {
986 privilege[chIndex] =
987 convertToSystemPrivilege(static_cast<CommandPrivilege>(
988 usersTbl.user[usrIndex].userPrivAccess[chIndex].privilege));
989 ipmiEnabled[chIndex] =
990 usersTbl.user[usrIndex].userPrivAccess[chIndex].ipmiEnabled;
991 linkAuthEnabled[chIndex] =
992 usersTbl.user[usrIndex].userPrivAccess[chIndex].linkAuthEnabled;
993 accessCallback[chIndex] =
994 usersTbl.user[usrIndex].userPrivAccess[chIndex].accessCallback;
995 }
996 jsonUserInfo[jsonPriv] = privilege;
997 jsonUserInfo[jsonIpmiEnabled] = ipmiEnabled;
998 jsonUserInfo[jsonLinkAuthEnabled] = linkAuthEnabled;
999 jsonUserInfo[jsonAccCallbk] = accessCallback;
1000 jsonUserInfo[jsonUserEnabled] = usersTbl.user[usrIndex].userEnabled;
1001 jsonUserInfo[jsonUserInSys] = usersTbl.user[usrIndex].userInSystem;
1002 jsonUserInfo[jsonFixedUser] = usersTbl.user[usrIndex].fixedUserName;
1003 jsonUsersTbl.push_back(jsonUserInfo);
1004 }
1005
1006 oUsrData << jsonUsersTbl;
1007 oUsrData.flush();
1008 oUsrData.close();
1009
1010 if (std::rename(tmpFile.c_str(), ipmiUserDataFile) != 0)
1011 {
1012 log<level::ERR>("Error in renaming temporary IPMI user data file");
1013 throw std::runtime_error("Error in renaming IPMI user data file");
1014 }
1015 // Update the timestamp
1016 fileLastUpdatedTime = getUpdatedFileTime();
1017 return;
1018}
1019
1020bool UserAccess::addUserEntry(const std::string& userName,
1021 const std::string& sysPriv, const bool& enabled)
1022{
1023 UsersTbl* userData = getUsersTblPtr();
1024 size_t freeIndex = 0xFF;
1025 // user index 0 is reserved, starts with 1
1026 for (size_t usrIndex = 1; usrIndex <= ipmiMaxUsers; ++usrIndex)
1027 {
1028 std::string curName(
1029 reinterpret_cast<char*>(userData->user[usrIndex].userName), 0,
1030 ipmiMaxUserName);
1031 if (userName == curName)
1032 {
1033 log<level::DEBUG>("User name exists",
1034 entry("USER_NAME=%s", userName.c_str()));
1035 return false; // user name exists.
1036 }
1037
1038 if ((!userData->user[usrIndex].userInSystem) &&
1039 (userData->user[usrIndex].userName[0] == '\0') &&
1040 (freeIndex == 0xFF))
1041 {
1042 freeIndex = usrIndex;
1043 }
1044 }
1045 if (freeIndex == 0xFF)
1046 {
1047 log<level::ERR>("No empty slots found");
1048 return false;
1049 }
1050 std::strncpy(reinterpret_cast<char*>(userData->user[freeIndex].userName),
1051 userName.c_str(), ipmiMaxUserName);
1052 uint8_t priv =
1053 static_cast<uint8_t>(UserAccess::convertToIPMIPrivilege(sysPriv)) &
1054 privMask;
1055 for (size_t chIndex = 0; chIndex < ipmiMaxChannels; ++chIndex)
1056 {
1057 userData->user[freeIndex].userPrivAccess[chIndex].privilege = priv;
1058 userData->user[freeIndex].userPrivAccess[chIndex].ipmiEnabled = true;
1059 userData->user[freeIndex].userPrivAccess[chIndex].linkAuthEnabled =
1060 true;
1061 userData->user[freeIndex].userPrivAccess[chIndex].accessCallback = true;
1062 }
1063 userData->user[freeIndex].userInSystem = true;
1064 userData->user[freeIndex].userEnabled = enabled;
1065
1066 return true;
1067}
1068
1069void UserAccess::deleteUserIndex(const size_t& usrIdx)
1070{
1071 UsersTbl* userData = getUsersTblPtr();
1072
1073 std::string userName(
1074 reinterpret_cast<char*>(userData->user[usrIdx].userName), 0,
1075 ipmiMaxUserName);
1076 ipmiClearUserEntryPassword(userName);
1077 std::fill(static_cast<uint8_t*>(userData->user[usrIdx].userName),
1078 static_cast<uint8_t*>(userData->user[usrIdx].userName) +
1079 sizeof(userData->user[usrIdx].userName),
1080 0);
1081 for (size_t chIndex = 0; chIndex < ipmiMaxChannels; ++chIndex)
1082 {
1083 userData->user[usrIdx].userPrivAccess[chIndex].privilege = privNoAccess;
1084 userData->user[usrIdx].userPrivAccess[chIndex].ipmiEnabled = false;
1085 userData->user[usrIdx].userPrivAccess[chIndex].linkAuthEnabled = false;
1086 userData->user[usrIdx].userPrivAccess[chIndex].accessCallback = false;
1087 }
1088 userData->user[usrIdx].userInSystem = false;
1089 userData->user[usrIdx].userEnabled = false;
1090 return;
1091}
1092
1093void UserAccess::checkAndReloadUserData()
1094{
1095 std::time_t updateTime = getUpdatedFileTime();
1096 if (updateTime != fileLastUpdatedTime || updateTime == -EIO)
1097 {
1098 std::fill(reinterpret_cast<uint8_t*>(&usersTbl),
1099 reinterpret_cast<uint8_t*>(&usersTbl) + sizeof(usersTbl), 0);
1100 readUserData();
1101 }
1102 return;
1103}
1104
1105UsersTbl* UserAccess::getUsersTblPtr()
1106{
1107 // reload data before using it.
1108 checkAndReloadUserData();
1109 return &usersTbl;
1110}
1111
1112void UserAccess::getSystemPrivAndGroups()
1113{
1114 std::map<std::string, PrivAndGroupType> properties;
1115 try
1116 {
1117 auto method = bus.new_method_call(
1118 getUserServiceName().c_str(), userMgrObjBasePath,
1119 dBusPropertiesInterface, getAllPropertiesMethod);
1120 method.append(userMgrInterface);
1121
1122 auto reply = bus.call(method);
1123 reply.read(properties);
1124 }
1125 catch (const sdbusplus::exception::SdBusError& e)
1126 {
1127 log<level::DEBUG>("Failed to excute method",
1128 entry("METHOD=%s", getAllPropertiesMethod),
1129 entry("PATH=%s", userMgrObjBasePath));
1130 return;
1131 }
1132 for (const auto& t : properties)
1133 {
1134 auto key = t.first;
1135 if (key == allPrivProperty)
1136 {
William A. Kennington IIIdfad4862018-11-19 17:45:35 -08001137 availablePrivileges =
1138 variant_ns::get<std::vector<std::string>>(t.second);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301139 }
1140 else if (key == allGrpProperty)
1141 {
William A. Kennington IIIdfad4862018-11-19 17:45:35 -08001142 availableGroups =
1143 variant_ns::get<std::vector<std::string>>(t.second);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301144 }
1145 }
1146 // TODO: Implement Supported Privilege & Groups verification logic
1147 return;
1148}
1149
1150std::time_t UserAccess::getUpdatedFileTime()
1151{
1152 struct stat fileStat;
1153 if (stat(ipmiUserDataFile, &fileStat) != 0)
1154 {
1155 log<level::DEBUG>("Error in getting last updated time stamp");
1156 return -EIO;
1157 }
1158 return fileStat.st_mtime;
1159}
1160
1161void UserAccess::getUserProperties(const DbusUserObjProperties& properties,
1162 std::vector<std::string>& usrGrps,
1163 std::string& usrPriv, bool& usrEnabled)
1164{
1165 for (const auto& t : properties)
1166 {
1167 std::string key = t.first;
1168 if (key == userPrivProperty)
1169 {
William A. Kennington IIIdfad4862018-11-19 17:45:35 -08001170 usrPriv = variant_ns::get<std::string>(t.second);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301171 }
1172 else if (key == userGrpProperty)
1173 {
William A. Kennington IIIdfad4862018-11-19 17:45:35 -08001174 usrGrps = variant_ns::get<std::vector<std::string>>(t.second);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301175 }
1176 else if (key == userEnabledProperty)
1177 {
William A. Kennington IIIdfad4862018-11-19 17:45:35 -08001178 usrEnabled = variant_ns::get<bool>(t.second);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301179 }
1180 }
1181 return;
1182}
1183
1184int UserAccess::getUserObjProperties(const DbusUserObjValue& userObjs,
1185 std::vector<std::string>& usrGrps,
1186 std::string& usrPriv, bool& usrEnabled)
1187{
1188 auto usrObj = userObjs.find(usersInterface);
1189 if (usrObj != userObjs.end())
1190 {
1191 getUserProperties(usrObj->second, usrGrps, usrPriv, usrEnabled);
1192 return 0;
1193 }
1194 return -EIO;
1195}
1196
1197void UserAccess::initUserDataFile()
1198{
1199 boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex>
1200 userLock{*userMutex};
1201 try
1202 {
1203 readUserData();
1204 }
1205 catch (const std::ios_base::failure& e)
1206 { // File is empty, create it for the first time
1207 std::fill(reinterpret_cast<uint8_t*>(&usersTbl),
1208 reinterpret_cast<uint8_t*>(&usersTbl) + sizeof(usersTbl), 0);
1209 // user index 0 is reserved, starts with 1
1210 for (size_t userIndex = 1; userIndex <= ipmiMaxUsers; ++userIndex)
1211 {
1212 for (size_t chIndex = 0; chIndex < ipmiMaxChannels; ++chIndex)
1213 {
1214 usersTbl.user[userIndex].userPrivAccess[chIndex].privilege =
1215 privNoAccess;
1216 }
1217 }
1218 writeUserData();
1219 }
1220 std::map<DbusUserObjPath, DbusUserObjValue> managedObjs;
1221 try
1222 {
1223 auto method = bus.new_method_call(getUserServiceName().c_str(),
1224 userMgrObjBasePath, dBusObjManager,
1225 getManagedObjectsMethod);
1226 auto reply = bus.call(method);
1227 reply.read(managedObjs);
1228 }
1229 catch (const sdbusplus::exception::SdBusError& e)
1230 {
1231 log<level::DEBUG>("Failed to excute method",
1232 entry("METHOD=%s", getSubTreeMethod),
1233 entry("PATH=%s", userMgrObjBasePath));
1234 return;
1235 }
1236
1237 UsersTbl* userData = &usersTbl;
1238 // user index 0 is reserved, starts with 1
1239 for (size_t usrIdx = 1; usrIdx <= ipmiMaxUsers; ++usrIdx)
1240 {
1241 if ((userData->user[usrIdx].userInSystem) &&
1242 (userData->user[usrIdx].userName[0] != '\0'))
1243 {
1244 std::vector<std::string> usrGrps;
1245 std::string usrPriv;
1246 bool usrEnabled;
1247
1248 std::string userName(
1249 reinterpret_cast<char*>(userData->user[usrIdx].userName), 0,
1250 ipmiMaxUserName);
1251 std::string usersPath =
1252 std::string(userObjBasePath) + "/" + userName;
1253
1254 auto usrObj = managedObjs.find(usersPath);
1255 if (usrObj != managedObjs.end())
1256 {
1257 // User exist. Lets check and update other fileds
1258 getUserObjProperties(usrObj->second, usrGrps, usrPriv,
1259 usrEnabled);
1260 if (std::find(usrGrps.begin(), usrGrps.end(), ipmiGrpName) ==
1261 usrGrps.end())
1262 {
1263 // Group "ipmi" is removed so lets remove user in IPMI
1264 deleteUserIndex(usrIdx);
1265 }
1266 else
1267 {
1268 // Group "ipmi" is present so lets update other properties
1269 // in IPMI
1270 uint8_t priv =
1271 UserAccess::convertToIPMIPrivilege(usrPriv) & privMask;
1272 // Update all channels priv, only if it is not equivalent to
1273 // getUsrMgmtSyncIndex()
1274 if (userData->user[usrIdx]
1275 .userPrivAccess[getUsrMgmtSyncIndex()]
1276 .privilege != priv)
1277 {
1278 for (size_t chIndex = 0; chIndex < ipmiMaxChannels;
1279 ++chIndex)
1280 {
1281 userData->user[usrIdx]
1282 .userPrivAccess[chIndex]
1283 .privilege = priv;
1284 }
1285 }
1286 if (userData->user[usrIdx].userEnabled != usrEnabled)
1287 {
1288 userData->user[usrIdx].userEnabled = usrEnabled;
1289 }
1290 }
1291
1292 // We are done with this obj. lets delete from MAP
1293 managedObjs.erase(usrObj);
1294 }
1295 else
1296 {
1297 deleteUserIndex(usrIdx);
1298 }
1299 }
1300 }
1301
1302 // Walk through remnaining managedObj users list
1303 // Add them to ipmi data base
1304 for (const auto& usrObj : managedObjs)
1305 {
1306 std::vector<std::string> usrGrps;
1307 std::string usrPriv, userName;
1308 bool usrEnabled;
1309 std::string usrObjPath = std::string(usrObj.first);
1310 if (getUserNameFromPath(usrObj.first.str, userName) != 0)
1311 {
1312 log<level::ERR>("Error in user object path");
1313 continue;
1314 }
1315 getUserObjProperties(usrObj.second, usrGrps, usrPriv, usrEnabled);
1316 // Add 'ipmi' group users
1317 if (std::find(usrGrps.begin(), usrGrps.end(), ipmiGrpName) !=
1318 usrGrps.end())
1319 {
1320 // CREATE NEW USER
1321 if (true != addUserEntry(userName, usrPriv, usrEnabled))
1322 {
1323 break;
1324 }
1325 }
1326 }
1327
1328 // All userData slots update done. Lets write the data
1329 writeUserData();
1330
1331 return;
1332}
1333} // namespace ipmi