blob: 81e409e76167a6e68104ab0566711d8a845ccfe1 [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"
Saravanan Palanisamy77381f12019-05-15 22:33:17 +000019#include "channel_layer.hpp"
Johnathan Manteyfd61fc32021-04-08 11:05:38 -070020#include "channel_mgmt.hpp"
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +053021
Suryakanth Sekar90b00c72019-01-16 10:37:57 +053022#include <security/pam_appl.h>
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +053023#include <sys/stat.h>
24#include <unistd.h>
25
26#include <boost/interprocess/sync/named_recursive_mutex.hpp>
27#include <boost/interprocess/sync/scoped_lock.hpp>
Snehalatha Venkatesh745164c2021-06-25 10:02:25 +000028#include <ipmid/types.hpp>
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +053029#include <nlohmann/json.hpp>
30#include <phosphor-logging/elog-errors.hpp>
George Liu82844ef2024-07-17 17:03:56 +080031#include <phosphor-logging/lg2.hpp>
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +053032#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
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -050037#include <cerrno>
38#include <fstream>
39#include <regex>
40#include <variant>
41
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +053042namespace ipmi
43{
44
45// TODO: Move D-Bus & Object Manager related stuff, to common files
46// D-Bus property related
47static constexpr const char* dBusPropertiesInterface =
48 "org.freedesktop.DBus.Properties";
49static constexpr const char* getAllPropertiesMethod = "GetAll";
50static constexpr const char* propertiesChangedSignal = "PropertiesChanged";
51static constexpr const char* setPropertiesMethod = "Set";
52
53// Object Manager related
54static constexpr const char* dBusObjManager =
55 "org.freedesktop.DBus.ObjectManager";
56static constexpr const char* getManagedObjectsMethod = "GetManagedObjects";
57// Object Manager signals
58static constexpr const char* intfAddedSignal = "InterfacesAdded";
59static constexpr const char* intfRemovedSignal = "InterfacesRemoved";
60
61// Object Mapper related
62static constexpr const char* objMapperService =
63 "xyz.openbmc_project.ObjectMapper";
64static constexpr const char* objMapperPath =
65 "/xyz/openbmc_project/object_mapper";
66static constexpr const char* objMapperInterface =
67 "xyz.openbmc_project.ObjectMapper";
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +053068static constexpr const char* getObjectMethod = "GetObject";
69
70static constexpr const char* ipmiUserMutex = "ipmi_usr_mutex";
71static constexpr const char* ipmiMutexCleanupLockFile =
72 "/var/lib/ipmi/ipmi_usr_mutex_cleanup";
Jayaprakash Mutyala08d3d062021-10-01 16:01:57 +000073static constexpr const char* ipmiUserSignalLockFile =
74 "/var/lib/ipmi/ipmi_usr_signal_mutex";
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +053075static constexpr const char* ipmiUserDataFile = "/var/lib/ipmi/ipmi_user.json";
76static constexpr const char* ipmiGrpName = "ipmi";
77static constexpr size_t privNoAccess = 0xF;
78static constexpr size_t privMask = 0xF;
79
80// User manager related
81static constexpr const char* userMgrObjBasePath = "/xyz/openbmc_project/user";
82static constexpr const char* userObjBasePath = "/xyz/openbmc_project/user";
83static constexpr const char* userMgrInterface =
84 "xyz.openbmc_project.User.Manager";
85static constexpr const char* usersInterface =
86 "xyz.openbmc_project.User.Attributes";
87static constexpr const char* deleteUserInterface =
88 "xyz.openbmc_project.Object.Delete";
89
90static constexpr const char* createUserMethod = "CreateUser";
91static constexpr const char* deleteUserMethod = "Delete";
92static constexpr const char* renameUserMethod = "RenameUser";
93// User manager signal memebers
94static constexpr const char* userRenamedSignal = "UserRenamed";
95// Mgr interface properties
96static constexpr const char* allPrivProperty = "AllPrivileges";
97static constexpr const char* allGrpProperty = "AllGroups";
98// User interface properties
99static constexpr const char* userPrivProperty = "UserPrivilege";
100static constexpr const char* userGrpProperty = "UserGroups";
101static constexpr const char* userEnabledProperty = "UserEnabled";
102
103static std::array<std::string, (PRIVILEGE_OEM + 1)> ipmiPrivIndex = {
104 "priv-reserved", // PRIVILEGE_RESERVED - 0
105 "priv-callback", // PRIVILEGE_CALLBACK - 1
106 "priv-user", // PRIVILEGE_USER - 2
107 "priv-operator", // PRIVILEGE_OPERATOR - 3
108 "priv-admin", // PRIVILEGE_ADMIN - 4
109 "priv-custom" // PRIVILEGE_OEM - 5
110};
111
112using namespace phosphor::logging;
113using Json = nlohmann::json;
114
Vernon Mauery16b86932019-05-01 08:36:11 -0700115using PrivAndGroupType = std::variant<std::string, std::vector<std::string>>;
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530116
117using NoResource =
Willy Tu523e2d12023-09-05 11:36:48 -0700118 sdbusplus::error::xyz::openbmc_project::user::common::NoResource;
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530119
120using InternalFailure =
Willy Tu523e2d12023-09-05 11:36:48 -0700121 sdbusplus::error::xyz::openbmc_project::common::InternalFailure;
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530122
Lei YU4b0ddb62019-01-25 16:43:50 +0800123std::unique_ptr<sdbusplus::bus::match_t> userUpdatedSignal
124 __attribute__((init_priority(101)));
125std::unique_ptr<sdbusplus::bus::match_t> userMgrRenamedSignal
126 __attribute__((init_priority(101)));
127std::unique_ptr<sdbusplus::bus::match_t> userPropertiesSignal
128 __attribute__((init_priority(101)));
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530129
130// TODO: Below code can be removed once it is moved to common layer libmiscutil
Patrick Williams5d82f472022-07-22 19:26:53 -0500131std::string getUserService(sdbusplus::bus_t& bus, const std::string& intf,
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530132 const std::string& path)
133{
134 auto mapperCall = bus.new_method_call(objMapperService, objMapperPath,
135 objMapperInterface, getObjectMethod);
136
137 mapperCall.append(path);
138 mapperCall.append(std::vector<std::string>({intf}));
139
140 auto mapperResponseMsg = bus.call(mapperCall);
141
142 std::map<std::string, std::vector<std::string>> mapperResponse;
143 mapperResponseMsg.read(mapperResponse);
144
145 if (mapperResponse.begin() == mapperResponse.end())
146 {
147 throw sdbusplus::exception::SdBusError(
148 -EIO, "ERROR in reading the mapper response");
149 }
150
151 return mapperResponse.begin()->first;
152}
153
Patrick Williams5d82f472022-07-22 19:26:53 -0500154void setDbusProperty(sdbusplus::bus_t& bus, const std::string& service,
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530155 const std::string& objPath, const std::string& interface,
156 const std::string& property,
157 const DbusUserPropVariant& value)
158{
159 try
160 {
Patrick Williams1318a5e2024-08-16 15:19:54 -0400161 auto method =
162 bus.new_method_call(service.c_str(), objPath.c_str(),
163 dBusPropertiesInterface, setPropertiesMethod);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530164 method.append(interface, property, value);
165 bus.call(method);
166 }
Patrick Williams5d82f472022-07-22 19:26:53 -0500167 catch (const sdbusplus::exception_t& e)
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530168 {
George Liu82844ef2024-07-17 17:03:56 +0800169 lg2::error("Failed to set {PROPERTY}, path: {PATH}, "
170 "interface: {INTERFACE}",
171 "PROPERTY", property, "PATH", objPath, "INTERFACE",
172 interface);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530173 throw;
174 }
175}
176
Willy Tu523e2d12023-09-05 11:36:48 -0700177std::string getUserServiceName()
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530178{
Patrick Williams5d82f472022-07-22 19:26:53 -0500179 static sdbusplus::bus_t bus(ipmid_get_sd_bus_connection());
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530180 static std::string userMgmtService;
181 if (userMgmtService.empty())
182 {
183 try
184 {
Patrick Williams1318a5e2024-08-16 15:19:54 -0400185 userMgmtService =
186 ipmi::getUserService(bus, userMgrInterface, userMgrObjBasePath);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530187 }
Patrick Williams5d82f472022-07-22 19:26:53 -0500188 catch (const sdbusplus::exception_t& e)
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530189 {
190 userMgmtService.clear();
191 }
192 }
193 return userMgmtService;
194}
195
196UserAccess& getUserAccessObject()
197{
198 static UserAccess userAccess;
199 return userAccess;
200}
201
202int getUserNameFromPath(const std::string& path, std::string& userName)
203{
P Dheeraj Srujan Kumar0ce6a572021-12-13 09:01:55 +0530204 sdbusplus::message::object_path objPath(path);
205 userName.assign(objPath.filename());
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530206 return 0;
207}
208
209void 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 {
George Liu82844ef2024-07-17 17:03:56 +0800237 lg2::debug("User not found for signal, user name: {USER_NAME}, "
238 "user event: {USER_EVENT}",
239 "USER_NAME", userName, "USER_EVENT", userEvent);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530240 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 {
George Liu82844ef2024-07-17 17:03:56 +0800291 lg2::error("Unhandled user event: {USER_EVENT}", "USER_EVENT",
292 userEvent);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530293 return;
294 }
295 }
296 }
297 usrAccess.writeUserData();
George Liu82844ef2024-07-17 17:03:56 +0800298 lg2::debug("User event handled successfully, user name: {USER_NAME}, "
299 "user event: {USER_EVENT}",
300 "USER_NAME", userName.c_str(), "USER_EVENT", userEvent);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530301
302 return;
303}
304
Patrick Williams5d82f472022-07-22 19:26:53 -0500305void userUpdatedSignalHandler(UserAccess& usrAccess, sdbusplus::message_t& msg)
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530306{
Patrick Williams5d82f472022-07-22 19:26:53 -0500307 static sdbusplus::bus_t bus(ipmid_get_sd_bus_connection());
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530308 std::string signal = msg.get_member();
Patrick Venture3a697ad2019-08-19 11:12:05 -0700309 std::string userName, priv, newUserName;
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530310 std::vector<std::string> groups;
311 bool enabled = false;
312 UserUpdateEvent userEvent = UserUpdateEvent::reservedEvent;
313 if (signal == intfAddedSignal)
314 {
315 DbusUserObjPath objPath;
316 DbusUserObjValue objValue;
317 msg.read(objPath, objValue);
318 getUserNameFromPath(objPath.str, userName);
319 if (usrAccess.getUserObjProperties(objValue, groups, priv, enabled) !=
320 0)
321 {
322 return;
323 }
324 if (std::find(groups.begin(), groups.end(), ipmiGrpName) ==
325 groups.end())
326 {
327 return;
328 }
329 userEvent = UserUpdateEvent::userCreated;
330 }
331 else if (signal == intfRemovedSignal)
332 {
333 DbusUserObjPath objPath;
334 std::vector<std::string> interfaces;
335 msg.read(objPath, interfaces);
336 getUserNameFromPath(objPath.str, userName);
337 userEvent = UserUpdateEvent::userDeleted;
338 }
339 else if (signal == userRenamedSignal)
340 {
341 msg.read(userName, newUserName);
342 userEvent = UserUpdateEvent::userRenamed;
343 }
344 else if (signal == propertiesChangedSignal)
345 {
346 getUserNameFromPath(msg.get_path(), userName);
347 }
348 else
349 {
George Liu82844ef2024-07-17 17:03:56 +0800350 lg2::error("Unknown user update signal: {SIGNAL}", "SIGNAL", signal);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530351 return;
352 }
353
354 if (signal.empty() || userName.empty() ||
355 (signal == userRenamedSignal && newUserName.empty()))
356 {
George Liu82844ef2024-07-17 17:03:56 +0800357 lg2::error("Invalid inputs received");
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530358 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 {
Vernon Maueryf442e112019-04-09 11:44:36 -0700376 priv = std::get<std::string>(prop.second);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530377 userEvent = UserUpdateEvent::userPrivUpdated;
378 }
379 else if (member == userGrpProperty)
380 {
Vernon Maueryf442e112019-04-09 11:44:36 -0700381 groups = std::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 {
Vernon Maueryf442e112019-04-09 11:44:36 -0700386 enabled = std::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 }
Patrick Williams5d82f472022-07-22 19:26:53 -0500411 catch (const sdbusplus::exception_t& e)
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530412 {
George Liu82844ef2024-07-17 17:03:56 +0800413 lg2::debug("Failed to excute {METHOD}, path: {PATH}",
414 "METHOD", getAllPropertiesMethod, "PATH",
415 msg.get_path());
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530416 return;
417 }
418 usrAccess.getUserProperties(properties, groups, priv,
419 enabled);
420 // add user to ipmi user list.
421 userUpdateHelper(usrAccess, UserUpdateEvent::userCreated,
422 userName, priv, enabled, newUserName);
423 }
424 }
425 else if (userEvent != UserUpdateEvent::reservedEvent)
426 {
427 userUpdateHelper(usrAccess, userEvent, userName, priv, enabled,
428 newUserName);
429 }
430 }
431 }
432 else if (userEvent != UserUpdateEvent::reservedEvent)
433 {
434 userUpdateHelper(usrAccess, userEvent, userName, priv, enabled,
435 newUserName);
436 }
437 return;
438}
439
440UserAccess::~UserAccess()
441{
442 if (signalHndlrObject)
443 {
444 userUpdatedSignal.reset();
445 userMgrRenamedSignal.reset();
446 userPropertiesSignal.reset();
447 sigHndlrLock.unlock();
448 }
449}
450
451UserAccess::UserAccess() : bus(ipmid_get_sd_bus_connection())
452{
453 std::ofstream mutexCleanUpFile;
454 mutexCleanUpFile.open(ipmiMutexCleanupLockFile,
455 std::ofstream::out | std::ofstream::app);
456 if (!mutexCleanUpFile.good())
457 {
George Liu82844ef2024-07-17 17:03:56 +0800458 lg2::debug("Unable to open mutex cleanup file");
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530459 return;
460 }
461 mutexCleanUpFile.close();
462 mutexCleanupLock = boost::interprocess::file_lock(ipmiMutexCleanupLockFile);
463 if (mutexCleanupLock.try_lock())
464 {
465 boost::interprocess::named_recursive_mutex::remove(ipmiUserMutex);
466 }
467 mutexCleanupLock.lock_sharable();
468 userMutex = std::make_unique<boost::interprocess::named_recursive_mutex>(
469 boost::interprocess::open_or_create, ipmiUserMutex);
470
arun-pmbbe728c2020-01-10 15:18:04 +0530471 cacheUserDataFile();
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530472 getSystemPrivAndGroups();
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530473}
474
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530475UserInfo* UserAccess::getUserInfo(const uint8_t userId)
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530476{
477 checkAndReloadUserData();
478 return &usersTbl.user[userId];
479}
480
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530481void UserAccess::setUserInfo(const uint8_t userId, UserInfo* userInfo)
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530482{
483 checkAndReloadUserData();
484 std::copy(reinterpret_cast<uint8_t*>(userInfo),
485 reinterpret_cast<uint8_t*>(userInfo) + sizeof(*userInfo),
486 reinterpret_cast<uint8_t*>(&usersTbl.user[userId]));
487 writeUserData();
488}
489
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530490bool UserAccess::isValidChannel(const uint8_t chNum)
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530491{
492 return (chNum < ipmiMaxChannels);
493}
494
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530495bool UserAccess::isValidUserId(const uint8_t userId)
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530496{
497 return ((userId <= ipmiMaxUsers) && (userId != reservedUserId));
498}
499
Richard Marian Thomaiyara45cb342018-12-03 15:08:59 +0530500bool UserAccess::isValidPrivilege(const uint8_t priv)
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530501{
jayaprakash Mutyala0e2dbee2019-12-26 13:03:04 +0000502 // Callback privilege is deprecated in OpenBMC
Alexander Filippovfc24fa52022-02-01 14:57:59 +0300503 return isValidPrivLimit(priv);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530504}
505
506uint8_t UserAccess::getUsrMgmtSyncIndex()
507{
Johnathan Manteyfd61fc32021-04-08 11:05:38 -0700508 // Identify the IPMI channel used to assign system user privilege levels
509 // in phosphor-user-manager. The default value is IPMI Channel 1. To
510 // assign a different channel add:
511 // "is_management_nic" : true
512 // into the channel_config.json file describing the assignment of the IPMI
513 // channels. It is only necessary to add the string above to ONE record in
514 // the channel_config.json file. All other records will be automatically
515 // assigned a "false" value.
516 return getChannelConfigObject().getManagementNICID();
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530517}
518
519CommandPrivilege UserAccess::convertToIPMIPrivilege(const std::string& value)
520{
521 auto iter = std::find(ipmiPrivIndex.begin(), ipmiPrivIndex.end(), value);
522 if (iter == ipmiPrivIndex.end())
523 {
524 if (value == "")
525 {
526 return static_cast<CommandPrivilege>(privNoAccess);
527 }
George Liu82844ef2024-07-17 17:03:56 +0800528 lg2::error("Error in converting to IPMI privilege: {PRIV}", "PRIV",
529 value);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530530 throw std::out_of_range("Out of range - convertToIPMIPrivilege");
531 }
532 else
533 {
534 return static_cast<CommandPrivilege>(
535 std::distance(ipmiPrivIndex.begin(), iter));
536 }
537}
538
539std::string UserAccess::convertToSystemPrivilege(const CommandPrivilege& value)
540{
541 if (value == static_cast<CommandPrivilege>(privNoAccess))
542 {
543 return "";
544 }
545 try
546 {
547 return ipmiPrivIndex.at(value);
548 }
549 catch (const std::out_of_range& e)
550 {
George Liu82844ef2024-07-17 17:03:56 +0800551 lg2::error("Error in converting to system privilege: {PRIV}", "PRIV",
552 value);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530553 throw std::out_of_range("Out of range - convertToSystemPrivilege");
554 }
555}
556
jayaprakash Mutyala76363302020-02-14 23:50:38 +0000557bool UserAccess::isValidUserName(const std::string& userName)
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530558{
jayaprakash Mutyala76363302020-02-14 23:50:38 +0000559 if (userName.empty())
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530560 {
George Liu82844ef2024-07-17 17:03:56 +0800561 lg2::error("userName is empty");
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530562 return false;
563 }
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530564 if (!std::regex_match(userName.c_str(),
nichanghao.nch0c96fdf2024-01-17 22:13:35 +0800565 std::regex("[a-zA-Z_][a-zA-Z_0-9]*")))
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530566 {
George Liu82844ef2024-07-17 17:03:56 +0800567 lg2::error("Unsupported characters in user name");
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530568 return false;
569 }
570 if (userName == "root")
571 {
George Liu82844ef2024-07-17 17:03:56 +0800572 lg2::error("Invalid user name - root");
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530573 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 }
Patrick Williams5d82f472022-07-22 19:26:53 -0500584 catch (const sdbusplus::exception_t& e)
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530585 {
George Liu82844ef2024-07-17 17:03:56 +0800586 lg2::error("Failed to excute {METHOD}, path: {PATH}", "METHOD",
587 getManagedObjectsMethod, "PATH", userMgrObjBasePath);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530588 return false;
589 }
590
P Dheeraj Srujan Kumar0ce6a572021-12-13 09:01:55 +0530591 sdbusplus::message::object_path tempUserPath(userObjBasePath);
592 tempUserPath /= userName;
593 std::string usersPath(tempUserPath);
594
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530595 if (properties.find(usersPath) != properties.end())
596 {
George Liu82844ef2024-07-17 17:03:56 +0800597 lg2::debug("Username {USER_NAME} already exists", "USER_NAME",
598 userName);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530599 return false;
600 }
601
602 return true;
603}
604
Suryakanth Sekar90b00c72019-01-16 10:37:57 +0530605/** @brief Information exchanged by pam module and application.
606 *
607 * @param[in] numMsg - length of the array of pointers,msg.
608 *
609 * @param[in] msg - pointer to an array of pointers to pam_message structure
610 *
611 * @param[out] resp - struct pam response array
612 *
613 * @param[in] appdataPtr - member of pam_conv structure
614 *
615 * @return the response in pam response structure.
616 */
617
618static int pamFunctionConversation(int numMsg, const struct pam_message** msg,
619 struct pam_response** resp, void* appdataPtr)
620{
621 if (appdataPtr == nullptr)
622 {
P Dheeraj Srujan Kumar2aeb1c12021-07-20 04:26:13 +0530623 return PAM_CONV_ERR;
Suryakanth Sekar90b00c72019-01-16 10:37:57 +0530624 }
Suryakanth Sekar90b00c72019-01-16 10:37:57 +0530625
P Dheeraj Srujan Kumar2aeb1c12021-07-20 04:26:13 +0530626 if (numMsg <= 0 || numMsg >= PAM_MAX_NUM_MSG)
627 {
628 return PAM_CONV_ERR;
629 }
Suryakanth Sekar90b00c72019-01-16 10:37:57 +0530630
631 for (int i = 0; i < numMsg; ++i)
632 {
P Dheeraj Srujan Kumar2aeb1c12021-07-20 04:26:13 +0530633 /* Ignore all PAM messages except prompting for hidden input */
Suryakanth Sekar90b00c72019-01-16 10:37:57 +0530634 if (msg[i]->msg_style != PAM_PROMPT_ECHO_OFF)
635 {
636 continue;
637 }
P Dheeraj Srujan Kumar2aeb1c12021-07-20 04:26:13 +0530638
639 /* Assume PAM is only prompting for the password as hidden input */
640 /* Allocate memory only when PAM_PROMPT_ECHO_OFF is encounterred */
641
642 char* appPass = reinterpret_cast<char*>(appdataPtr);
643 size_t appPassSize = std::strlen(appPass);
644
645 if (appPassSize >= PAM_MAX_RESP_SIZE)
646 {
647 return PAM_CONV_ERR;
648 }
649
650 char* pass = reinterpret_cast<char*>(malloc(appPassSize + 1));
651 if (pass == nullptr)
652 {
653 return PAM_BUF_ERR;
654 }
655
Patrick Williams1318a5e2024-08-16 15:19:54 -0400656 void* ptr =
657 calloc(static_cast<size_t>(numMsg), sizeof(struct pam_response));
P Dheeraj Srujan Kumar2aeb1c12021-07-20 04:26:13 +0530658 if (ptr == nullptr)
659 {
660 free(pass);
661 return PAM_BUF_ERR;
662 }
663
664 std::strncpy(pass, appPass, appPassSize + 1);
665
666 *resp = reinterpret_cast<pam_response*>(ptr);
Suryakanth Sekar90b00c72019-01-16 10:37:57 +0530667 resp[i]->resp = pass;
P Dheeraj Srujan Kumar2aeb1c12021-07-20 04:26:13 +0530668
669 return PAM_SUCCESS;
Suryakanth Sekar90b00c72019-01-16 10:37:57 +0530670 }
P Dheeraj Srujan Kumar2aeb1c12021-07-20 04:26:13 +0530671
672 return PAM_CONV_ERR;
Suryakanth Sekar90b00c72019-01-16 10:37:57 +0530673}
674
675/** @brief Updating the PAM password
676 *
677 * @param[in] username - username in string
678 *
679 * @param[in] password - new password in string
680 *
681 * @return status
682 */
683
jayaprakash Mutyala9fc5fa12019-08-29 15:14:06 +0000684int pamUpdatePasswd(const char* username, const char* password)
Suryakanth Sekar90b00c72019-01-16 10:37:57 +0530685{
686 const struct pam_conv localConversation = {pamFunctionConversation,
687 const_cast<char*>(password)};
688 pam_handle_t* localAuthHandle = NULL; // this gets set by pam_start
689
Patrick Williams1318a5e2024-08-16 15:19:54 -0400690 int retval =
691 pam_start("passwd", username, &localConversation, &localAuthHandle);
Suryakanth Sekar90b00c72019-01-16 10:37:57 +0530692
693 if (retval != PAM_SUCCESS)
694 {
jayaprakash Mutyala9fc5fa12019-08-29 15:14:06 +0000695 return retval;
Suryakanth Sekar90b00c72019-01-16 10:37:57 +0530696 }
jayaprakash Mutyala9fc5fa12019-08-29 15:14:06 +0000697
698 retval = pam_chauthtok(localAuthHandle, PAM_SILENT);
699 if (retval != PAM_SUCCESS)
Suryakanth Sekar90b00c72019-01-16 10:37:57 +0530700 {
jayaprakash Mutyala9fc5fa12019-08-29 15:14:06 +0000701 pam_end(localAuthHandle, retval);
702 return retval;
Suryakanth Sekar90b00c72019-01-16 10:37:57 +0530703 }
jayaprakash Mutyala9fc5fa12019-08-29 15:14:06 +0000704
705 return pam_end(localAuthHandle, PAM_SUCCESS);
Suryakanth Sekar90b00c72019-01-16 10:37:57 +0530706}
707
Ayushi Smriti02650d52019-05-15 11:59:09 +0000708bool pamUserCheckAuthenticate(std::string_view username,
709 std::string_view password)
710{
711 const struct pam_conv localConversation = {
712 pamFunctionConversation, const_cast<char*>(password.data())};
713
714 pam_handle_t* localAuthHandle = NULL; // this gets set by pam_start
715
716 if (pam_start("dropbear", username.data(), &localConversation,
717 &localAuthHandle) != PAM_SUCCESS)
718 {
George Liu82844ef2024-07-17 17:03:56 +0800719 lg2::error("User Authentication Failure");
Ayushi Smriti02650d52019-05-15 11:59:09 +0000720 return false;
721 }
722
723 int retval = pam_authenticate(localAuthHandle,
724 PAM_SILENT | PAM_DISALLOW_NULL_AUTHTOK);
725
726 if (retval != PAM_SUCCESS)
727 {
George Liu82844ef2024-07-17 17:03:56 +0800728 lg2::debug("pam_authenticate returned failure: {ERROR}", "ERROR",
729 retval);
Ayushi Smriti02650d52019-05-15 11:59:09 +0000730
731 pam_end(localAuthHandle, retval);
732 return false;
733 }
734
735 if (pam_acct_mgmt(localAuthHandle, PAM_DISALLOW_NULL_AUTHTOK) !=
736 PAM_SUCCESS)
737 {
738 pam_end(localAuthHandle, PAM_SUCCESS);
739 return false;
740 }
741
742 if (pam_end(localAuthHandle, PAM_SUCCESS) != PAM_SUCCESS)
743 {
744 return false;
745 }
746 return true;
747}
748
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000749Cc UserAccess::setSpecialUserPassword(const std::string& userName,
Vernon Mauery1e22a0f2021-07-30 13:36:54 -0700750 const SecureString& userPassword)
Richard Marian Thomaiyar788362c2019-04-14 15:12:47 +0530751{
jayaprakash Mutyala9fc5fa12019-08-29 15:14:06 +0000752 if (pamUpdatePasswd(userName.c_str(), userPassword.c_str()) != PAM_SUCCESS)
Richard Marian Thomaiyar788362c2019-04-14 15:12:47 +0530753 {
George Liu82844ef2024-07-17 17:03:56 +0800754 lg2::debug("Failed to update password");
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000755 return ccUnspecifiedError;
Richard Marian Thomaiyar788362c2019-04-14 15:12:47 +0530756 }
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000757 return ccSuccess;
Richard Marian Thomaiyar788362c2019-04-14 15:12:47 +0530758}
759
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000760Cc UserAccess::setUserPassword(const uint8_t userId, const char* userPassword)
Suryakanth Sekar90b00c72019-01-16 10:37:57 +0530761{
762 std::string userName;
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000763 if (ipmiUserGetUserName(userId, userName) != ccSuccess)
Suryakanth Sekar90b00c72019-01-16 10:37:57 +0530764 {
George Liu82844ef2024-07-17 17:03:56 +0800765 lg2::debug("User Name not found, user Id: {USER_ID}", "USER_ID",
766 userId);
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000767 return ccParmOutOfRange;
Suryakanth Sekar90b00c72019-01-16 10:37:57 +0530768 }
Snehalatha Venkatesh61024d72021-04-08 16:24:39 +0000769
770 ipmi::SecureString passwd;
Suryakanth Sekar90b00c72019-01-16 10:37:57 +0530771 passwd.assign(reinterpret_cast<const char*>(userPassword), 0,
772 maxIpmi20PasswordSize);
jayaprakash Mutyala9fc5fa12019-08-29 15:14:06 +0000773 int retval = pamUpdatePasswd(userName.c_str(), passwd.c_str());
774
775 switch (retval)
Suryakanth Sekar90b00c72019-01-16 10:37:57 +0530776 {
jayaprakash Mutyala9fc5fa12019-08-29 15:14:06 +0000777 case PAM_SUCCESS:
778 {
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000779 return ccSuccess;
jayaprakash Mutyala9fc5fa12019-08-29 15:14:06 +0000780 }
781 case PAM_AUTHTOK_ERR:
782 {
George Liu82844ef2024-07-17 17:03:56 +0800783 lg2::debug("Bad authentication token");
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000784 return ccInvalidFieldRequest;
jayaprakash Mutyala9fc5fa12019-08-29 15:14:06 +0000785 }
786 default:
787 {
George Liu82844ef2024-07-17 17:03:56 +0800788 lg2::debug("Failed to update password, user Id: {USER_ID}",
789 "USER_ID", userId);
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000790 return ccUnspecifiedError;
jayaprakash Mutyala9fc5fa12019-08-29 15:14:06 +0000791 }
Suryakanth Sekar90b00c72019-01-16 10:37:57 +0530792 }
Suryakanth Sekar90b00c72019-01-16 10:37:57 +0530793}
794
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000795Cc UserAccess::setUserEnabledState(const uint8_t userId,
796 const bool& enabledState)
Richard Marian Thomaiyar282e79b2018-11-13 19:00:58 +0530797{
798 if (!isValidUserId(userId))
799 {
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000800 return ccParmOutOfRange;
Richard Marian Thomaiyar282e79b2018-11-13 19:00:58 +0530801 }
802 boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex>
803 userLock{*userMutex};
804 UserInfo* userInfo = getUserInfo(userId);
805 std::string userName;
806 userName.assign(reinterpret_cast<char*>(userInfo->userName), 0,
807 ipmiMaxUserName);
808 if (userName.empty())
809 {
George Liu82844ef2024-07-17 17:03:56 +0800810 lg2::debug("User name not set / invalid");
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000811 return ccUnspecifiedError;
Richard Marian Thomaiyar282e79b2018-11-13 19:00:58 +0530812 }
813 if (userInfo->userEnabled != enabledState)
814 {
P Dheeraj Srujan Kumar0ce6a572021-12-13 09:01:55 +0530815 sdbusplus::message::object_path tempUserPath(userObjBasePath);
816 tempUserPath /= userName;
817 std::string userPath(tempUserPath);
Patrick Venture99d1ba02019-02-21 15:11:24 -0800818 setDbusProperty(bus, getUserServiceName(), userPath, usersInterface,
819 userEnabledProperty, enabledState);
Richard Marian Thomaiyar2fe92822019-03-02 22:07:03 +0530820 userInfo->userEnabled = enabledState;
821 try
822 {
823 writeUserData();
824 }
825 catch (const std::exception& e)
826 {
George Liu82844ef2024-07-17 17:03:56 +0800827 lg2::debug("Write user data failed");
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000828 return ccUnspecifiedError;
Richard Marian Thomaiyar2fe92822019-03-02 22:07:03 +0530829 }
Richard Marian Thomaiyar282e79b2018-11-13 19:00:58 +0530830 }
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000831 return ccSuccess;
Richard Marian Thomaiyar282e79b2018-11-13 19:00:58 +0530832}
833
Patrick Williams1318a5e2024-08-16 15:19:54 -0400834Cc UserAccess::setUserPayloadAccess(
835 const uint8_t chNum, const uint8_t operation, const uint8_t userId,
836 const PayloadAccess& payloadAccess)
Saravanan Palanisamy77381f12019-05-15 22:33:17 +0000837{
838 constexpr uint8_t enable = 0x0;
839 constexpr uint8_t disable = 0x1;
840
841 if (!isValidChannel(chNum))
842 {
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000843 return ccInvalidFieldRequest;
Saravanan Palanisamy77381f12019-05-15 22:33:17 +0000844 }
845 if (!isValidUserId(userId))
846 {
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000847 return ccParmOutOfRange;
Saravanan Palanisamy77381f12019-05-15 22:33:17 +0000848 }
849 if (operation != enable && operation != disable)
850 {
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000851 return ccInvalidFieldRequest;
Saravanan Palanisamy77381f12019-05-15 22:33:17 +0000852 }
853 // Check operation & payloadAccess if required.
854 boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex>
855 userLock{*userMutex};
856 UserInfo* userInfo = getUserInfo(userId);
857
858 if (operation == enable)
859 {
860 userInfo->payloadAccess[chNum].stdPayloadEnables1 |=
861 payloadAccess.stdPayloadEnables1;
862
863 userInfo->payloadAccess[chNum].oemPayloadEnables1 |=
864 payloadAccess.oemPayloadEnables1;
865 }
866 else
867 {
868 userInfo->payloadAccess[chNum].stdPayloadEnables1 &=
869 ~(payloadAccess.stdPayloadEnables1);
870
871 userInfo->payloadAccess[chNum].oemPayloadEnables1 &=
872 ~(payloadAccess.oemPayloadEnables1);
873 }
874
875 try
876 {
877 writeUserData();
878 }
879 catch (const std::exception& e)
880 {
George Liu82844ef2024-07-17 17:03:56 +0800881 lg2::error("Write user data failed");
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000882 return ccUnspecifiedError;
Saravanan Palanisamy77381f12019-05-15 22:33:17 +0000883 }
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000884 return ccSuccess;
Saravanan Palanisamy77381f12019-05-15 22:33:17 +0000885}
886
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000887Cc UserAccess::setUserPrivilegeAccess(const uint8_t userId, const uint8_t chNum,
888 const UserPrivAccess& privAccess,
889 const bool& otherPrivUpdates)
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530890{
891 if (!isValidChannel(chNum))
892 {
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000893 return ccInvalidFieldRequest;
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530894 }
895 if (!isValidUserId(userId))
896 {
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000897 return ccParmOutOfRange;
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530898 }
899 boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex>
900 userLock{*userMutex};
901 UserInfo* userInfo = getUserInfo(userId);
902 std::string userName;
903 userName.assign(reinterpret_cast<char*>(userInfo->userName), 0,
904 ipmiMaxUserName);
905 if (userName.empty())
906 {
George Liu82844ef2024-07-17 17:03:56 +0800907 lg2::debug("User name not set / invalid");
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000908 return ccUnspecifiedError;
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530909 }
910 std::string priv = convertToSystemPrivilege(
911 static_cast<CommandPrivilege>(privAccess.privilege));
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530912 uint8_t syncIndex = getUsrMgmtSyncIndex();
913 if (chNum == syncIndex &&
914 privAccess.privilege != userInfo->userPrivAccess[syncIndex].privilege)
915 {
P Dheeraj Srujan Kumar0ce6a572021-12-13 09:01:55 +0530916 sdbusplus::message::object_path tempUserPath(userObjBasePath);
917 tempUserPath /= userName;
918 std::string userPath(tempUserPath);
Patrick Venture99d1ba02019-02-21 15:11:24 -0800919 setDbusProperty(bus, getUserServiceName(), userPath, usersInterface,
920 userPrivProperty, priv);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530921 }
922 userInfo->userPrivAccess[chNum].privilege = privAccess.privilege;
923
924 if (otherPrivUpdates)
925 {
926 userInfo->userPrivAccess[chNum].ipmiEnabled = privAccess.ipmiEnabled;
927 userInfo->userPrivAccess[chNum].linkAuthEnabled =
928 privAccess.linkAuthEnabled;
929 userInfo->userPrivAccess[chNum].accessCallback =
930 privAccess.accessCallback;
931 }
932 try
933 {
934 writeUserData();
935 }
936 catch (const std::exception& e)
937 {
George Liu82844ef2024-07-17 17:03:56 +0800938 lg2::debug("Write user data failed");
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000939 return ccUnspecifiedError;
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530940 }
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000941 return ccSuccess;
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530942}
943
944uint8_t UserAccess::getUserId(const std::string& userName)
945{
946 boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex>
947 userLock{*userMutex};
948 checkAndReloadUserData();
949 // user index 0 is reserved, starts with 1
950 size_t usrIndex = 1;
951 for (; usrIndex <= ipmiMaxUsers; ++usrIndex)
952 {
953 std::string curName(
954 reinterpret_cast<char*>(usersTbl.user[usrIndex].userName), 0,
955 ipmiMaxUserName);
956 if (userName == curName)
957 {
958 break; // found the entry
959 }
960 }
961 if (usrIndex > ipmiMaxUsers)
962 {
George Liu82844ef2024-07-17 17:03:56 +0800963 lg2::debug("Username {USER_NAME} not found", "USER_NAME", userName);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530964 return invalidUserId;
965 }
966
967 return usrIndex;
968}
969
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000970Cc UserAccess::getUserName(const uint8_t userId, std::string& userName)
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530971{
972 if (!isValidUserId(userId))
973 {
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000974 return ccParmOutOfRange;
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530975 }
976 UserInfo* userInfo = getUserInfo(userId);
977 userName.assign(reinterpret_cast<char*>(userInfo->userName), 0,
978 ipmiMaxUserName);
NITIN SHARMAb541a5a2019-07-18 12:46:59 +0000979 return ccSuccess;
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +0530980}
981
Richard Marian Thomaiyar489a4ed2020-01-17 11:48:40 +0530982bool UserAccess::isIpmiInAvailableGroupList()
983{
984 if (std::find(availableGroups.begin(), availableGroups.end(),
985 ipmiGrpName) != availableGroups.end())
986 {
987 return true;
988 }
989 if (availableGroups.empty())
990 {
991 // available groups shouldn't be empty, re-query
992 getSystemPrivAndGroups();
993 if (std::find(availableGroups.begin(), availableGroups.end(),
994 ipmiGrpName) != availableGroups.end())
995 {
996 return true;
997 }
998 }
999 return false;
1000}
1001
jayaprakash Mutyala76363302020-02-14 23:50:38 +00001002Cc UserAccess::setUserName(const uint8_t userId, const std::string& userName)
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301003{
1004 if (!isValidUserId(userId))
1005 {
NITIN SHARMAb541a5a2019-07-18 12:46:59 +00001006 return ccParmOutOfRange;
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301007 }
1008
1009 boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex>
1010 userLock{*userMutex};
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301011 std::string oldUser;
1012 getUserName(userId, oldUser);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301013
jayaprakash Mutyala76363302020-02-14 23:50:38 +00001014 if (oldUser == userName)
Richard Marian Thomaiyar8550b602018-12-06 13:20:38 +05301015 {
1016 // requesting to set the same user name, return success.
NITIN SHARMAb541a5a2019-07-18 12:46:59 +00001017 return ccSuccess;
Richard Marian Thomaiyar8550b602018-12-06 13:20:38 +05301018 }
jayaprakash Mutyala76363302020-02-14 23:50:38 +00001019
1020 bool validUser = isValidUserName(userName);
Richard Marian Thomaiyar8550b602018-12-06 13:20:38 +05301021 UserInfo* userInfo = getUserInfo(userId);
jayaprakash Mutyala76363302020-02-14 23:50:38 +00001022 if (userName.empty() && !oldUser.empty())
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301023 {
1024 // Delete existing user
P Dheeraj Srujan Kumar0ce6a572021-12-13 09:01:55 +05301025 sdbusplus::message::object_path tempUserPath(userObjBasePath);
1026 tempUserPath /= oldUser;
1027 std::string userPath(tempUserPath);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301028 try
1029 {
1030 auto method = bus.new_method_call(
1031 getUserServiceName().c_str(), userPath.c_str(),
1032 deleteUserInterface, deleteUserMethod);
1033 auto reply = bus.call(method);
1034 }
Patrick Williams5d82f472022-07-22 19:26:53 -05001035 catch (const sdbusplus::exception_t& e)
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301036 {
George Liu82844ef2024-07-17 17:03:56 +08001037 lg2::debug("Failed to excute {METHOD}, path:{PATH}", "METHOD",
1038 deleteUserMethod, "PATH", userPath);
NITIN SHARMAb541a5a2019-07-18 12:46:59 +00001039 return ccUnspecifiedError;
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301040 }
Richard Marian Thomaiyar02710bb2018-11-28 20:42:25 +05301041 deleteUserIndex(userId);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301042 }
jayaprakash Mutyala76363302020-02-14 23:50:38 +00001043 else if (oldUser.empty() && !userName.empty() && validUser)
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301044 {
1045 try
1046 {
Richard Marian Thomaiyar489a4ed2020-01-17 11:48:40 +05301047 if (!isIpmiInAvailableGroupList())
1048 {
NITIN SHARMAb541a5a2019-07-18 12:46:59 +00001049 return ccUnspecifiedError;
Richard Marian Thomaiyar489a4ed2020-01-17 11:48:40 +05301050 }
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301051 // Create new user
1052 auto method = bus.new_method_call(
1053 getUserServiceName().c_str(), userMgrObjBasePath,
1054 userMgrInterface, createUserMethod);
Alexander Filippovf6f3bb02022-02-01 14:38:40 +03001055 method.append(userName.c_str(), availableGroups,
1056 ipmiPrivIndex[PRIVILEGE_USER], false);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301057 auto reply = bus.call(method);
1058 }
Patrick Williams5d82f472022-07-22 19:26:53 -05001059 catch (const sdbusplus::exception_t& e)
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301060 {
George Liu82844ef2024-07-17 17:03:56 +08001061 lg2::debug("Failed to excute {METHOD}, path: {PATH}", "METHOD",
1062 createUserMethod, "PATH", userMgrObjBasePath);
NITIN SHARMAb541a5a2019-07-18 12:46:59 +00001063 return ccUnspecifiedError;
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301064 }
jayaprakash Mutyala76363302020-02-14 23:50:38 +00001065
1066 std::memset(userInfo->userName, 0, sizeof(userInfo->userName));
1067 std::memcpy(userInfo->userName,
1068 static_cast<const void*>(userName.data()), userName.size());
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301069 userInfo->userInSystem = true;
Alexander Filippovf6f3bb02022-02-01 14:38:40 +03001070 for (size_t chIndex = 0; chIndex < ipmiMaxChannels; chIndex++)
1071 {
1072 userInfo->userPrivAccess[chIndex].privilege =
1073 static_cast<uint8_t>(PRIVILEGE_USER);
1074 }
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301075 }
jayaprakash Mutyala76363302020-02-14 23:50:38 +00001076 else if (oldUser != userName && validUser)
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301077 {
1078 try
1079 {
1080 // User rename
1081 auto method = bus.new_method_call(
1082 getUserServiceName().c_str(), userMgrObjBasePath,
1083 userMgrInterface, renameUserMethod);
jayaprakash Mutyala76363302020-02-14 23:50:38 +00001084 method.append(oldUser.c_str(), userName.c_str());
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301085 auto reply = bus.call(method);
1086 }
Patrick Williams5d82f472022-07-22 19:26:53 -05001087 catch (const sdbusplus::exception_t& e)
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301088 {
George Liu82844ef2024-07-17 17:03:56 +08001089 lg2::debug("Failed to excute {METHOD}, path: {PATH}", "METHOD",
1090 renameUserMethod, "PATH", userMgrObjBasePath);
NITIN SHARMAb541a5a2019-07-18 12:46:59 +00001091 return ccUnspecifiedError;
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301092 }
1093 std::fill(static_cast<uint8_t*>(userInfo->userName),
1094 static_cast<uint8_t*>(userInfo->userName) +
1095 sizeof(userInfo->userName),
1096 0);
jayaprakash Mutyala76363302020-02-14 23:50:38 +00001097
1098 std::memset(userInfo->userName, 0, sizeof(userInfo->userName));
1099 std::memcpy(userInfo->userName,
1100 static_cast<const void*>(userName.data()), userName.size());
1101
1102 ipmiRenameUserEntryPassword(oldUser, userName);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301103 userInfo->userInSystem = true;
1104 }
1105 else if (!validUser)
1106 {
NITIN SHARMAb541a5a2019-07-18 12:46:59 +00001107 return ccInvalidFieldRequest;
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301108 }
1109 try
1110 {
1111 writeUserData();
1112 }
1113 catch (const std::exception& e)
1114 {
George Liu82844ef2024-07-17 17:03:56 +08001115 lg2::debug("Write user data failed");
NITIN SHARMAb541a5a2019-07-18 12:46:59 +00001116 return ccUnspecifiedError;
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301117 }
NITIN SHARMAb541a5a2019-07-18 12:46:59 +00001118 return ccSuccess;
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301119}
1120
1121static constexpr const char* jsonUserName = "user_name";
1122static constexpr const char* jsonPriv = "privilege";
1123static constexpr const char* jsonIpmiEnabled = "ipmi_enabled";
1124static constexpr const char* jsonLinkAuthEnabled = "link_auth_enabled";
1125static constexpr const char* jsonAccCallbk = "access_callback";
1126static constexpr const char* jsonUserEnabled = "user_enabled";
1127static constexpr const char* jsonUserInSys = "user_in_system";
1128static constexpr const char* jsonFixedUser = "fixed_user_name";
Saravanan Palanisamy77381f12019-05-15 22:33:17 +00001129static constexpr const char* payloadEnabledStr = "payload_enabled";
1130static constexpr const char* stdPayloadStr = "std_payload";
1131static constexpr const char* oemPayloadStr = "OEM_payload";
1132
1133/** @brief to construct a JSON object from the given payload access details.
1134 *
1135 * @param[in] stdPayload - stdPayloadEnables1 in a 2D-array. (input)
1136 * @param[in] oemPayload - oemPayloadEnables1 in a 2D-array. (input)
1137 *
1138 * @details Sample output JSON object format :
1139 * "payload_enabled":{
1140 * "OEM_payload0":[false,...<repeat 'ipmiMaxChannels - 1' times>],
1141 * "OEM_payload1":[false,...<repeat 'ipmiMaxChannels - 1' times>],
1142 * "OEM_payload2":[false,...<repeat 'ipmiMaxChannels - 1' times>],
1143 * "OEM_payload3":[false,...<repeat 'ipmiMaxChannels - 1' times>],
1144 * "OEM_payload4":[false,...<repeat 'ipmiMaxChannels - 1' times>],
1145 * "OEM_payload5":[false,...<repeat 'ipmiMaxChannels - 1' times>],
1146 * "OEM_payload6":[false,...<repeat 'ipmiMaxChannels - 1' times>],
1147 * "OEM_payload7":[false,...<repeat 'ipmiMaxChannels - 1' times>],
1148 * "std_payload0":[false,...<repeat 'ipmiMaxChannels - 1' times>],
1149 * "std_payload1":[false,...<repeat 'ipmiMaxChannels - 1' times>],
1150 * "std_payload2":[false,...<repeat 'ipmiMaxChannels - 1' times>],
1151 * "std_payload3":[false,...<repeat 'ipmiMaxChannels - 1' times>],
1152 * "std_payload4":[false,...<repeat 'ipmiMaxChannels - 1' times>],
1153 * "std_payload5":[false,...<repeat 'ipmiMaxChannels - 1' times>],
1154 * "std_payload6":[false,...<repeat 'ipmiMaxChannels - 1' times>],
1155 * "std_payload7":[false,...<repeat 'ipmiMaxChannels - 1' times>],
1156 * }
1157 */
1158static const Json constructJsonPayloadEnables(
1159 const std::array<std::array<bool, ipmiMaxChannels>, payloadsPerByte>&
1160 stdPayload,
1161 const std::array<std::array<bool, ipmiMaxChannels>, payloadsPerByte>&
1162 oemPayload)
1163{
1164 Json jsonPayloadEnabled;
1165
1166 for (auto payloadNum = 0; payloadNum < payloadsPerByte; payloadNum++)
1167 {
1168 std::ostringstream stdPayloadStream;
1169 std::ostringstream oemPayloadStream;
1170
1171 stdPayloadStream << stdPayloadStr << payloadNum;
1172 oemPayloadStream << oemPayloadStr << payloadNum;
1173
1174 jsonPayloadEnabled.push_back(Json::object_t::value_type(
1175 stdPayloadStream.str(), stdPayload[payloadNum]));
1176
1177 jsonPayloadEnabled.push_back(Json::object_t::value_type(
1178 oemPayloadStream.str(), oemPayload[payloadNum]));
1179 }
1180 return jsonPayloadEnabled;
1181}
1182
1183void UserAccess::readPayloadAccessFromUserInfo(
1184 const UserInfo& userInfo,
1185 std::array<std::array<bool, ipmiMaxChannels>, payloadsPerByte>& stdPayload,
1186 std::array<std::array<bool, ipmiMaxChannels>, payloadsPerByte>& oemPayload)
1187{
1188 for (auto payloadNum = 0; payloadNum < payloadsPerByte; payloadNum++)
1189 {
1190 for (auto chIndex = 0; chIndex < ipmiMaxChannels; chIndex++)
1191 {
1192 stdPayload[payloadNum][chIndex] =
1193 userInfo.payloadAccess[chIndex].stdPayloadEnables1[payloadNum];
1194
1195 oemPayload[payloadNum][chIndex] =
1196 userInfo.payloadAccess[chIndex].oemPayloadEnables1[payloadNum];
1197 }
1198 }
1199}
1200
1201void UserAccess::updatePayloadAccessInUserInfo(
1202 const std::array<std::array<bool, ipmiMaxChannels>, payloadsPerByte>&
1203 stdPayload,
Willy Tu11d68892022-01-20 10:37:34 -08001204 const std::array<std::array<bool, ipmiMaxChannels>, payloadsPerByte>&,
Saravanan Palanisamy77381f12019-05-15 22:33:17 +00001205 UserInfo& userInfo)
1206{
1207 for (size_t chIndex = 0; chIndex < ipmiMaxChannels; ++chIndex)
1208 {
1209 // Ensure that reserved/unsupported payloads are marked to zero.
1210 userInfo.payloadAccess[chIndex].stdPayloadEnables1.reset();
1211 userInfo.payloadAccess[chIndex].oemPayloadEnables1.reset();
1212 userInfo.payloadAccess[chIndex].stdPayloadEnables2Reserved.reset();
1213 userInfo.payloadAccess[chIndex].oemPayloadEnables2Reserved.reset();
1214 // Update SOL status as it is the only supported payload currently.
1215 userInfo.payloadAccess[chIndex]
1216 .stdPayloadEnables1[static_cast<uint8_t>(ipmi::PayloadType::SOL)] =
1217 stdPayload[static_cast<uint8_t>(ipmi::PayloadType::SOL)][chIndex];
1218 }
1219}
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301220
1221void UserAccess::readUserData()
1222{
1223 boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex>
1224 userLock{*userMutex};
1225
1226 std::ifstream iUsrData(ipmiUserDataFile, std::ios::in | std::ios::binary);
1227 if (!iUsrData.good())
1228 {
George Liu82844ef2024-07-17 17:03:56 +08001229 lg2::error("Error in reading IPMI user data file");
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301230 throw std::ios_base::failure("Error opening IPMI user data file");
1231 }
1232
1233 Json jsonUsersTbl = Json::array();
1234 jsonUsersTbl = Json::parse(iUsrData, nullptr, false);
1235
1236 if (jsonUsersTbl.size() != ipmiMaxUsers)
1237 {
George Liu82844ef2024-07-17 17:03:56 +08001238 lg2::error("Error in reading IPMI user data file - User count issues");
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301239 throw std::runtime_error(
1240 "Corrupted IPMI user data file - invalid user count");
1241 }
Saravanan Palanisamy77381f12019-05-15 22:33:17 +00001242
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301243 // user index 0 is reserved, starts with 1
1244 for (size_t usrIndex = 1; usrIndex <= ipmiMaxUsers; ++usrIndex)
1245 {
1246 Json userInfo = jsonUsersTbl[usrIndex - 1]; // json array starts with 0.
1247 if (userInfo.is_null())
1248 {
George Liu82844ef2024-07-17 17:03:56 +08001249 lg2::error("Error in reading IPMI user data file - "
1250 "user info corrupted");
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301251 throw std::runtime_error(
1252 "Corrupted IPMI user data file - invalid user info");
1253 }
1254 std::string userName = userInfo[jsonUserName].get<std::string>();
1255 std::strncpy(reinterpret_cast<char*>(usersTbl.user[usrIndex].userName),
1256 userName.c_str(), ipmiMaxUserName);
1257
1258 std::vector<std::string> privilege =
1259 userInfo[jsonPriv].get<std::vector<std::string>>();
1260 std::vector<bool> ipmiEnabled =
1261 userInfo[jsonIpmiEnabled].get<std::vector<bool>>();
1262 std::vector<bool> linkAuthEnabled =
1263 userInfo[jsonLinkAuthEnabled].get<std::vector<bool>>();
1264 std::vector<bool> accessCallback =
1265 userInfo[jsonAccCallbk].get<std::vector<bool>>();
Saravanan Palanisamy77381f12019-05-15 22:33:17 +00001266
1267 // Payload Enables Processing.
Saravanan Palanisamyc86045c2019-07-26 22:52:40 +00001268 std::array<std::array<bool, ipmiMaxChannels>, payloadsPerByte>
1269 stdPayload = {};
1270 std::array<std::array<bool, ipmiMaxChannels>, payloadsPerByte>
1271 oemPayload = {};
1272 try
Saravanan Palanisamy77381f12019-05-15 22:33:17 +00001273 {
Saravanan Palanisamyc86045c2019-07-26 22:52:40 +00001274 const auto jsonPayloadEnabled = userInfo.at(payloadEnabledStr);
1275 for (auto payloadNum = 0; payloadNum < payloadsPerByte;
1276 payloadNum++)
Saravanan Palanisamy77381f12019-05-15 22:33:17 +00001277 {
Saravanan Palanisamyc86045c2019-07-26 22:52:40 +00001278 std::ostringstream stdPayloadStream;
1279 std::ostringstream oemPayloadStream;
1280
1281 stdPayloadStream << stdPayloadStr << payloadNum;
1282 oemPayloadStream << oemPayloadStr << payloadNum;
1283
1284 stdPayload[payloadNum] =
1285 jsonPayloadEnabled[stdPayloadStream.str()]
1286 .get<std::array<bool, ipmiMaxChannels>>();
1287 oemPayload[payloadNum] =
1288 jsonPayloadEnabled[oemPayloadStream.str()]
1289 .get<std::array<bool, ipmiMaxChannels>>();
1290
1291 if (stdPayload[payloadNum].size() != ipmiMaxChannels ||
1292 oemPayload[payloadNum].size() != ipmiMaxChannels)
1293 {
George Liu82844ef2024-07-17 17:03:56 +08001294 lg2::error("Error in reading IPMI user data file - "
1295 "payload properties corrupted");
Saravanan Palanisamyc86045c2019-07-26 22:52:40 +00001296 throw std::runtime_error(
1297 "Corrupted IPMI user data file - payload properties");
1298 }
Saravanan Palanisamy77381f12019-05-15 22:33:17 +00001299 }
1300 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -05001301 catch (const Json::out_of_range& e)
Saravanan Palanisamyc86045c2019-07-26 22:52:40 +00001302 {
1303 // Key not found in 'userInfo'; possibly an old JSON file. Use
1304 // default values for all payloads, and SOL payload default is true.
1305 stdPayload[static_cast<uint8_t>(ipmi::PayloadType::SOL)].fill(true);
1306 }
Saravanan Palanisamy77381f12019-05-15 22:33:17 +00001307
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301308 if (privilege.size() != ipmiMaxChannels ||
1309 ipmiEnabled.size() != ipmiMaxChannels ||
1310 linkAuthEnabled.size() != ipmiMaxChannels ||
1311 accessCallback.size() != ipmiMaxChannels)
1312 {
George Liu82844ef2024-07-17 17:03:56 +08001313 lg2::error("Error in reading IPMI user data file - "
1314 "properties corrupted");
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301315 throw std::runtime_error(
1316 "Corrupted IPMI user data file - properties");
1317 }
1318 for (size_t chIndex = 0; chIndex < ipmiMaxChannels; ++chIndex)
1319 {
1320 usersTbl.user[usrIndex].userPrivAccess[chIndex].privilege =
1321 static_cast<uint8_t>(
1322 convertToIPMIPrivilege(privilege[chIndex]));
1323 usersTbl.user[usrIndex].userPrivAccess[chIndex].ipmiEnabled =
1324 ipmiEnabled[chIndex];
1325 usersTbl.user[usrIndex].userPrivAccess[chIndex].linkAuthEnabled =
1326 linkAuthEnabled[chIndex];
1327 usersTbl.user[usrIndex].userPrivAccess[chIndex].accessCallback =
1328 accessCallback[chIndex];
1329 }
Saravanan Palanisamy77381f12019-05-15 22:33:17 +00001330 updatePayloadAccessInUserInfo(stdPayload, oemPayload,
1331 usersTbl.user[usrIndex]);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301332 usersTbl.user[usrIndex].userEnabled =
1333 userInfo[jsonUserEnabled].get<bool>();
1334 usersTbl.user[usrIndex].userInSystem =
1335 userInfo[jsonUserInSys].get<bool>();
1336 usersTbl.user[usrIndex].fixedUserName =
1337 userInfo[jsonFixedUser].get<bool>();
1338 }
1339
George Liu82844ef2024-07-17 17:03:56 +08001340 lg2::debug("User data read from IPMI data file");
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301341 iUsrData.close();
1342 // Update the timestamp
1343 fileLastUpdatedTime = getUpdatedFileTime();
1344 return;
1345}
1346
1347void UserAccess::writeUserData()
1348{
1349 boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex>
1350 userLock{*userMutex};
1351
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301352 Json jsonUsersTbl = Json::array();
1353 // user index 0 is reserved, starts with 1
1354 for (size_t usrIndex = 1; usrIndex <= ipmiMaxUsers; ++usrIndex)
1355 {
1356 Json jsonUserInfo;
1357 jsonUserInfo[jsonUserName] = std::string(
1358 reinterpret_cast<char*>(usersTbl.user[usrIndex].userName), 0,
1359 ipmiMaxUserName);
1360 std::vector<std::string> privilege(ipmiMaxChannels);
1361 std::vector<bool> ipmiEnabled(ipmiMaxChannels);
1362 std::vector<bool> linkAuthEnabled(ipmiMaxChannels);
1363 std::vector<bool> accessCallback(ipmiMaxChannels);
Saravanan Palanisamy77381f12019-05-15 22:33:17 +00001364
1365 std::array<std::array<bool, ipmiMaxChannels>, payloadsPerByte>
1366 stdPayload;
1367 std::array<std::array<bool, ipmiMaxChannels>, payloadsPerByte>
1368 oemPayload;
1369
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301370 for (size_t chIndex = 0; chIndex < ipmiMaxChannels; chIndex++)
1371 {
1372 privilege[chIndex] =
1373 convertToSystemPrivilege(static_cast<CommandPrivilege>(
1374 usersTbl.user[usrIndex].userPrivAccess[chIndex].privilege));
1375 ipmiEnabled[chIndex] =
1376 usersTbl.user[usrIndex].userPrivAccess[chIndex].ipmiEnabled;
1377 linkAuthEnabled[chIndex] =
1378 usersTbl.user[usrIndex].userPrivAccess[chIndex].linkAuthEnabled;
1379 accessCallback[chIndex] =
1380 usersTbl.user[usrIndex].userPrivAccess[chIndex].accessCallback;
1381 }
1382 jsonUserInfo[jsonPriv] = privilege;
1383 jsonUserInfo[jsonIpmiEnabled] = ipmiEnabled;
1384 jsonUserInfo[jsonLinkAuthEnabled] = linkAuthEnabled;
1385 jsonUserInfo[jsonAccCallbk] = accessCallback;
1386 jsonUserInfo[jsonUserEnabled] = usersTbl.user[usrIndex].userEnabled;
1387 jsonUserInfo[jsonUserInSys] = usersTbl.user[usrIndex].userInSystem;
1388 jsonUserInfo[jsonFixedUser] = usersTbl.user[usrIndex].fixedUserName;
Saravanan Palanisamy77381f12019-05-15 22:33:17 +00001389
1390 readPayloadAccessFromUserInfo(usersTbl.user[usrIndex], stdPayload,
1391 oemPayload);
Patrick Williams1318a5e2024-08-16 15:19:54 -04001392 Json jsonPayloadEnabledInfo =
1393 constructJsonPayloadEnables(stdPayload, oemPayload);
Saravanan Palanisamy77381f12019-05-15 22:33:17 +00001394 jsonUserInfo[payloadEnabledStr] = jsonPayloadEnabledInfo;
1395
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301396 jsonUsersTbl.push_back(jsonUserInfo);
1397 }
1398
Richard Marian Thomaiyar687df402019-05-09 00:16:53 +05301399 static std::string tmpFile{std::string(ipmiUserDataFile) + "_tmp"};
1400 int fd = open(tmpFile.c_str(), O_CREAT | O_WRONLY | O_TRUNC | O_SYNC,
1401 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
1402 if (fd < 0)
1403 {
George Liu82844ef2024-07-17 17:03:56 +08001404 lg2::error("Error in creating temporary IPMI user data file");
Richard Marian Thomaiyar687df402019-05-09 00:16:53 +05301405 throw std::ios_base::failure(
1406 "Error in creating temporary IPMI user data file");
1407 }
1408 const auto& writeStr = jsonUsersTbl.dump();
1409 if (write(fd, writeStr.c_str(), writeStr.size()) !=
1410 static_cast<ssize_t>(writeStr.size()))
1411 {
1412 close(fd);
George Liu82844ef2024-07-17 17:03:56 +08001413 lg2::error("Error in writing temporary IPMI user data file");
Richard Marian Thomaiyar687df402019-05-09 00:16:53 +05301414 throw std::ios_base::failure(
1415 "Error in writing temporary IPMI user data file");
1416 }
1417 close(fd);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301418
1419 if (std::rename(tmpFile.c_str(), ipmiUserDataFile) != 0)
1420 {
George Liu82844ef2024-07-17 17:03:56 +08001421 lg2::error("Error in renaming temporary IPMI user data file");
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301422 throw std::runtime_error("Error in renaming IPMI user data file");
1423 }
1424 // Update the timestamp
1425 fileLastUpdatedTime = getUpdatedFileTime();
1426 return;
1427}
1428
1429bool UserAccess::addUserEntry(const std::string& userName,
1430 const std::string& sysPriv, const bool& enabled)
1431{
1432 UsersTbl* userData = getUsersTblPtr();
1433 size_t freeIndex = 0xFF;
1434 // user index 0 is reserved, starts with 1
1435 for (size_t usrIndex = 1; usrIndex <= ipmiMaxUsers; ++usrIndex)
1436 {
1437 std::string curName(
1438 reinterpret_cast<char*>(userData->user[usrIndex].userName), 0,
1439 ipmiMaxUserName);
1440 if (userName == curName)
1441 {
George Liu82844ef2024-07-17 17:03:56 +08001442 lg2::debug("Username {USER_NAME} exists", "USER_NAME", userName);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301443 return false; // user name exists.
1444 }
1445
1446 if ((!userData->user[usrIndex].userInSystem) &&
1447 (userData->user[usrIndex].userName[0] == '\0') &&
1448 (freeIndex == 0xFF))
1449 {
1450 freeIndex = usrIndex;
1451 }
1452 }
1453 if (freeIndex == 0xFF)
1454 {
George Liu82844ef2024-07-17 17:03:56 +08001455 lg2::error("No empty slots found");
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301456 return false;
1457 }
1458 std::strncpy(reinterpret_cast<char*>(userData->user[freeIndex].userName),
1459 userName.c_str(), ipmiMaxUserName);
1460 uint8_t priv =
1461 static_cast<uint8_t>(UserAccess::convertToIPMIPrivilege(sysPriv)) &
1462 privMask;
1463 for (size_t chIndex = 0; chIndex < ipmiMaxChannels; ++chIndex)
1464 {
1465 userData->user[freeIndex].userPrivAccess[chIndex].privilege = priv;
1466 userData->user[freeIndex].userPrivAccess[chIndex].ipmiEnabled = true;
1467 userData->user[freeIndex].userPrivAccess[chIndex].linkAuthEnabled =
1468 true;
1469 userData->user[freeIndex].userPrivAccess[chIndex].accessCallback = true;
1470 }
1471 userData->user[freeIndex].userInSystem = true;
1472 userData->user[freeIndex].userEnabled = enabled;
1473
1474 return true;
1475}
1476
1477void UserAccess::deleteUserIndex(const size_t& usrIdx)
1478{
1479 UsersTbl* userData = getUsersTblPtr();
1480
1481 std::string userName(
1482 reinterpret_cast<char*>(userData->user[usrIdx].userName), 0,
1483 ipmiMaxUserName);
1484 ipmiClearUserEntryPassword(userName);
1485 std::fill(static_cast<uint8_t*>(userData->user[usrIdx].userName),
1486 static_cast<uint8_t*>(userData->user[usrIdx].userName) +
1487 sizeof(userData->user[usrIdx].userName),
1488 0);
1489 for (size_t chIndex = 0; chIndex < ipmiMaxChannels; ++chIndex)
1490 {
1491 userData->user[usrIdx].userPrivAccess[chIndex].privilege = privNoAccess;
1492 userData->user[usrIdx].userPrivAccess[chIndex].ipmiEnabled = false;
1493 userData->user[usrIdx].userPrivAccess[chIndex].linkAuthEnabled = false;
1494 userData->user[usrIdx].userPrivAccess[chIndex].accessCallback = false;
1495 }
1496 userData->user[usrIdx].userInSystem = false;
1497 userData->user[usrIdx].userEnabled = false;
1498 return;
1499}
1500
1501void UserAccess::checkAndReloadUserData()
1502{
Jayaprakash Mutyala08d3d062021-10-01 16:01:57 +00001503 std::timespec updateTime = getUpdatedFileTime();
1504 if ((updateTime.tv_sec != fileLastUpdatedTime.tv_sec ||
1505 updateTime.tv_nsec != fileLastUpdatedTime.tv_nsec) ||
1506 (updateTime.tv_sec == 0 && updateTime.tv_nsec == 0))
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301507 {
1508 std::fill(reinterpret_cast<uint8_t*>(&usersTbl),
1509 reinterpret_cast<uint8_t*>(&usersTbl) + sizeof(usersTbl), 0);
1510 readUserData();
1511 }
1512 return;
1513}
1514
1515UsersTbl* UserAccess::getUsersTblPtr()
1516{
1517 // reload data before using it.
1518 checkAndReloadUserData();
1519 return &usersTbl;
1520}
1521
1522void UserAccess::getSystemPrivAndGroups()
1523{
1524 std::map<std::string, PrivAndGroupType> properties;
1525 try
1526 {
1527 auto method = bus.new_method_call(
1528 getUserServiceName().c_str(), userMgrObjBasePath,
1529 dBusPropertiesInterface, getAllPropertiesMethod);
1530 method.append(userMgrInterface);
1531
1532 auto reply = bus.call(method);
1533 reply.read(properties);
1534 }
Patrick Williams5d82f472022-07-22 19:26:53 -05001535 catch (const sdbusplus::exception_t& e)
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301536 {
George Liu82844ef2024-07-17 17:03:56 +08001537 lg2::debug("Failed to excute {METHOD}, path: {PATH}", "METHOD",
1538 getAllPropertiesMethod, "PATH", userMgrObjBasePath);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301539 return;
1540 }
1541 for (const auto& t : properties)
1542 {
1543 auto key = t.first;
1544 if (key == allPrivProperty)
1545 {
Vernon Maueryf442e112019-04-09 11:44:36 -07001546 availablePrivileges = std::get<std::vector<std::string>>(t.second);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301547 }
1548 else if (key == allGrpProperty)
1549 {
Vernon Maueryf442e112019-04-09 11:44:36 -07001550 availableGroups = std::get<std::vector<std::string>>(t.second);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301551 }
1552 }
1553 // TODO: Implement Supported Privilege & Groups verification logic
1554 return;
1555}
1556
Jayaprakash Mutyala08d3d062021-10-01 16:01:57 +00001557std::timespec UserAccess::getUpdatedFileTime()
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301558{
1559 struct stat fileStat;
1560 if (stat(ipmiUserDataFile, &fileStat) != 0)
1561 {
George Liu82844ef2024-07-17 17:03:56 +08001562 lg2::debug("Error in getting last updated time stamp");
Jayaprakash Mutyala08d3d062021-10-01 16:01:57 +00001563 return std::timespec{0, 0};
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301564 }
Jayaprakash Mutyala08d3d062021-10-01 16:01:57 +00001565 return fileStat.st_mtim;
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301566}
1567
1568void UserAccess::getUserProperties(const DbusUserObjProperties& properties,
1569 std::vector<std::string>& usrGrps,
1570 std::string& usrPriv, bool& usrEnabled)
1571{
1572 for (const auto& t : properties)
1573 {
1574 std::string key = t.first;
1575 if (key == userPrivProperty)
1576 {
Vernon Maueryf442e112019-04-09 11:44:36 -07001577 usrPriv = std::get<std::string>(t.second);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301578 }
1579 else if (key == userGrpProperty)
1580 {
Vernon Maueryf442e112019-04-09 11:44:36 -07001581 usrGrps = std::get<std::vector<std::string>>(t.second);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301582 }
1583 else if (key == userEnabledProperty)
1584 {
Vernon Maueryf442e112019-04-09 11:44:36 -07001585 usrEnabled = std::get<bool>(t.second);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301586 }
1587 }
1588 return;
1589}
1590
1591int UserAccess::getUserObjProperties(const DbusUserObjValue& userObjs,
1592 std::vector<std::string>& usrGrps,
1593 std::string& usrPriv, bool& usrEnabled)
1594{
1595 auto usrObj = userObjs.find(usersInterface);
1596 if (usrObj != userObjs.end())
1597 {
1598 getUserProperties(usrObj->second, usrGrps, usrPriv, usrEnabled);
1599 return 0;
1600 }
1601 return -EIO;
1602}
1603
arun-pmbbe728c2020-01-10 15:18:04 +05301604void UserAccess::cacheUserDataFile()
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301605{
1606 boost::interprocess::scoped_lock<boost::interprocess::named_recursive_mutex>
1607 userLock{*userMutex};
1608 try
1609 {
1610 readUserData();
1611 }
1612 catch (const std::ios_base::failure& e)
1613 { // File is empty, create it for the first time
1614 std::fill(reinterpret_cast<uint8_t*>(&usersTbl),
1615 reinterpret_cast<uint8_t*>(&usersTbl) + sizeof(usersTbl), 0);
1616 // user index 0 is reserved, starts with 1
1617 for (size_t userIndex = 1; userIndex <= ipmiMaxUsers; ++userIndex)
1618 {
1619 for (size_t chIndex = 0; chIndex < ipmiMaxChannels; ++chIndex)
1620 {
1621 usersTbl.user[userIndex].userPrivAccess[chIndex].privilege =
1622 privNoAccess;
Saravanan Palanisamy92d81192019-08-07 18:00:04 +00001623 usersTbl.user[userIndex]
1624 .payloadAccess[chIndex]
1625 .stdPayloadEnables1[static_cast<uint8_t>(
1626 ipmi::PayloadType::SOL)] = true;
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301627 }
1628 }
1629 writeUserData();
1630 }
Jayaprakash Mutyala08d3d062021-10-01 16:01:57 +00001631 // Create lock file if it does not exist
1632 int fd = open(ipmiUserSignalLockFile, O_CREAT | O_TRUNC | O_SYNC,
1633 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
1634 if (fd < 0)
1635 {
George Liu82844ef2024-07-17 17:03:56 +08001636 lg2::error("Error in creating IPMI user signal lock file");
Jayaprakash Mutyala08d3d062021-10-01 16:01:57 +00001637 throw std::ios_base::failure(
1638 "Error in creating temporary IPMI user signal lock file");
1639 }
1640 close(fd);
1641
1642 sigHndlrLock = boost::interprocess::file_lock(ipmiUserSignalLockFile);
George Liu1a2e1502022-07-08 12:20:19 +08001643 // Register it for single object and single process either netipmid /
arun-pmbbe728c2020-01-10 15:18:04 +05301644 // host-ipmid
1645 if (userUpdatedSignal == nullptr && sigHndlrLock.try_lock())
1646 {
George Liu82844ef2024-07-17 17:03:56 +08001647 lg2::debug("Registering signal handler");
arun-pmbbe728c2020-01-10 15:18:04 +05301648 userUpdatedSignal = std::make_unique<sdbusplus::bus::match_t>(
1649 bus,
1650 sdbusplus::bus::match::rules::type::signal() +
1651 sdbusplus::bus::match::rules::interface(dBusObjManager) +
1652 sdbusplus::bus::match::rules::path(userMgrObjBasePath),
Patrick Williams5d82f472022-07-22 19:26:53 -05001653 [&](sdbusplus::message_t& msg) {
Patrick Williams1318a5e2024-08-16 15:19:54 -04001654 userUpdatedSignalHandler(*this, msg);
1655 });
arun-pmbbe728c2020-01-10 15:18:04 +05301656 userMgrRenamedSignal = std::make_unique<sdbusplus::bus::match_t>(
1657 bus,
1658 sdbusplus::bus::match::rules::type::signal() +
1659 sdbusplus::bus::match::rules::interface(userMgrInterface) +
1660 sdbusplus::bus::match::rules::path(userMgrObjBasePath),
Patrick Williams5d82f472022-07-22 19:26:53 -05001661 [&](sdbusplus::message_t& msg) {
Patrick Williams1318a5e2024-08-16 15:19:54 -04001662 userUpdatedSignalHandler(*this, msg);
1663 });
arun-pmbbe728c2020-01-10 15:18:04 +05301664 userPropertiesSignal = std::make_unique<sdbusplus::bus::match_t>(
1665 bus,
1666 sdbusplus::bus::match::rules::type::signal() +
1667 sdbusplus::bus::match::rules::path_namespace(userObjBasePath) +
1668 sdbusplus::bus::match::rules::interface(
1669 dBusPropertiesInterface) +
1670 sdbusplus::bus::match::rules::member(propertiesChangedSignal) +
1671 sdbusplus::bus::match::rules::argN(0, usersInterface),
Patrick Williams5d82f472022-07-22 19:26:53 -05001672 [&](sdbusplus::message_t& msg) {
Patrick Williams1318a5e2024-08-16 15:19:54 -04001673 userUpdatedSignalHandler(*this, msg);
1674 });
arun-pmbbe728c2020-01-10 15:18:04 +05301675 signalHndlrObject = true;
1676 }
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301677 std::map<DbusUserObjPath, DbusUserObjValue> managedObjs;
1678 try
1679 {
1680 auto method = bus.new_method_call(getUserServiceName().c_str(),
1681 userMgrObjBasePath, dBusObjManager,
1682 getManagedObjectsMethod);
1683 auto reply = bus.call(method);
1684 reply.read(managedObjs);
1685 }
Patrick Williams5d82f472022-07-22 19:26:53 -05001686 catch (const sdbusplus::exception_t& e)
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301687 {
George Liu82844ef2024-07-17 17:03:56 +08001688 lg2::debug("Failed to excute {METHOD}, path: {PATH}", "METHOD",
1689 getManagedObjectsMethod, "PATH", userMgrObjBasePath);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301690 return;
1691 }
Richard Marian Thomaiyare004e222019-05-09 00:37:55 +05301692 bool updateRequired = false;
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301693 UsersTbl* userData = &usersTbl;
1694 // user index 0 is reserved, starts with 1
1695 for (size_t usrIdx = 1; usrIdx <= ipmiMaxUsers; ++usrIdx)
1696 {
1697 if ((userData->user[usrIdx].userInSystem) &&
1698 (userData->user[usrIdx].userName[0] != '\0'))
1699 {
1700 std::vector<std::string> usrGrps;
1701 std::string usrPriv;
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301702
1703 std::string userName(
1704 reinterpret_cast<char*>(userData->user[usrIdx].userName), 0,
1705 ipmiMaxUserName);
P Dheeraj Srujan Kumar0ce6a572021-12-13 09:01:55 +05301706 sdbusplus::message::object_path tempUserPath(userObjBasePath);
1707 tempUserPath /= userName;
1708 std::string usersPath(tempUserPath);
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301709
1710 auto usrObj = managedObjs.find(usersPath);
1711 if (usrObj != managedObjs.end())
1712 {
Chen,Yugang0e862fa2019-09-06 11:03:05 +08001713 bool usrEnabled = false;
Patrick Venture3a697ad2019-08-19 11:12:05 -07001714
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301715 // User exist. Lets check and update other fileds
1716 getUserObjProperties(usrObj->second, usrGrps, usrPriv,
1717 usrEnabled);
1718 if (std::find(usrGrps.begin(), usrGrps.end(), ipmiGrpName) ==
1719 usrGrps.end())
1720 {
Richard Marian Thomaiyare004e222019-05-09 00:37:55 +05301721 updateRequired = true;
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301722 // Group "ipmi" is removed so lets remove user in IPMI
1723 deleteUserIndex(usrIdx);
1724 }
1725 else
1726 {
1727 // Group "ipmi" is present so lets update other properties
1728 // in IPMI
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -05001729 uint8_t priv = UserAccess::convertToIPMIPrivilege(usrPriv) &
1730 privMask;
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301731 // Update all channels priv, only if it is not equivalent to
1732 // getUsrMgmtSyncIndex()
1733 if (userData->user[usrIdx]
1734 .userPrivAccess[getUsrMgmtSyncIndex()]
1735 .privilege != priv)
1736 {
Richard Marian Thomaiyare004e222019-05-09 00:37:55 +05301737 updateRequired = true;
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301738 for (size_t chIndex = 0; chIndex < ipmiMaxChannels;
1739 ++chIndex)
1740 {
1741 userData->user[usrIdx]
1742 .userPrivAccess[chIndex]
1743 .privilege = priv;
1744 }
1745 }
1746 if (userData->user[usrIdx].userEnabled != usrEnabled)
1747 {
Richard Marian Thomaiyare004e222019-05-09 00:37:55 +05301748 updateRequired = true;
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301749 userData->user[usrIdx].userEnabled = usrEnabled;
1750 }
1751 }
1752
1753 // We are done with this obj. lets delete from MAP
1754 managedObjs.erase(usrObj);
1755 }
1756 else
1757 {
Richard Marian Thomaiyare004e222019-05-09 00:37:55 +05301758 updateRequired = true;
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301759 deleteUserIndex(usrIdx);
1760 }
1761 }
1762 }
1763
1764 // Walk through remnaining managedObj users list
1765 // Add them to ipmi data base
1766 for (const auto& usrObj : managedObjs)
1767 {
1768 std::vector<std::string> usrGrps;
1769 std::string usrPriv, userName;
Chen,Yugang0e862fa2019-09-06 11:03:05 +08001770 bool usrEnabled = false;
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301771 std::string usrObjPath = std::string(usrObj.first);
1772 if (getUserNameFromPath(usrObj.first.str, userName) != 0)
1773 {
George Liu82844ef2024-07-17 17:03:56 +08001774 lg2::error("Error in user object path");
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301775 continue;
1776 }
1777 getUserObjProperties(usrObj.second, usrGrps, usrPriv, usrEnabled);
1778 // Add 'ipmi' group users
1779 if (std::find(usrGrps.begin(), usrGrps.end(), ipmiGrpName) !=
1780 usrGrps.end())
1781 {
Richard Marian Thomaiyare004e222019-05-09 00:37:55 +05301782 updateRequired = true;
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301783 // CREATE NEW USER
1784 if (true != addUserEntry(userName, usrPriv, usrEnabled))
1785 {
1786 break;
1787 }
1788 }
1789 }
1790
Richard Marian Thomaiyare004e222019-05-09 00:37:55 +05301791 if (updateRequired)
1792 {
1793 // All userData slots update done. Lets write the data
1794 writeUserData();
1795 }
Richard Marian Thomaiyar5a6b6362018-03-12 23:42:34 +05301796
1797 return;
1798}
1799} // namespace ipmi