blob: 09d82b10305093bd73a1fc85249e48d8a60f3d89 [file] [log] [blame]
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +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
Patrick Williams9638afb2021-02-22 17:16:24 -060017#include "config.h"
18
19#include "user_mgr.hpp"
20
21#include "file.hpp"
22#include "shadowlock.hpp"
23#include "users.hpp"
24
25#include <grp.h>
26#include <pwd.h>
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053027#include <shadow.h>
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053028#include <sys/types.h>
29#include <sys/wait.h>
Joseph Reynolds3ab6cc22020-03-03 14:09:03 -060030#include <time.h>
Patrick Williams9638afb2021-02-22 17:16:24 -060031#include <unistd.h>
32
33#include <boost/algorithm/string/split.hpp>
Patrick Williams9638afb2021-02-22 17:16:24 -060034#include <phosphor-logging/elog-errors.hpp>
35#include <phosphor-logging/elog.hpp>
Jiaqing Zhao11ec6662022-07-05 20:55:34 +080036#include <phosphor-logging/lg2.hpp>
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053037#include <xyz/openbmc_project/Common/error.hpp>
38#include <xyz/openbmc_project/User/Common/error.hpp>
Patrick Williams9638afb2021-02-22 17:16:24 -060039
40#include <algorithm>
Nan Zhouda401fe2022-10-25 00:07:18 +000041#include <array>
Jiaqing Zhaofba4bb12022-06-24 01:30:57 +080042#include <ctime>
Patrick Williams9638afb2021-02-22 17:16:24 -060043#include <fstream>
44#include <numeric>
45#include <regex>
Nan Zhoue47c09d2022-10-25 00:06:41 +000046#include <span>
47#include <string>
48#include <string_view>
49#include <vector>
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053050
51namespace phosphor
52{
53namespace user
54{
55
Patrick Williams9638afb2021-02-22 17:16:24 -060056static constexpr const char* passwdFileName = "/etc/passwd";
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053057static constexpr size_t ipmiMaxUserNameLen = 16;
58static constexpr size_t systemMaxUserNameLen = 30;
Patrick Williams9638afb2021-02-22 17:16:24 -060059static constexpr const char* grpSsh = "ssh";
Richard Marian Thomaiyar9164fd92018-06-13 16:51:00 +053060static constexpr int success = 0;
61static constexpr int failure = -1;
62
63// pam modules related
Patrick Williams9638afb2021-02-22 17:16:24 -060064static constexpr const char* minPasswdLenProp = "minlen";
65static constexpr const char* remOldPasswdCount = "remember";
66static constexpr const char* maxFailedAttempt = "deny";
67static constexpr const char* unlockTimeout = "unlock_time";
Jason M. Bills2d042d12023-03-28 15:32:45 -070068static constexpr const char* defaultFaillockConfigFile =
69 "/etc/security/faillock.conf";
Jason M. Bills3b280ec2023-08-15 16:15:48 -070070static constexpr const char* defaultPWHistoryConfigFile =
71 "/etc/security/pwhistory.conf";
Jason M. Bills2d042d12023-03-28 15:32:45 -070072static constexpr const char* defaultPWQualityConfigFile =
73 "/etc/security/pwquality.conf";
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053074
Ratan Guptaaeaf9412019-02-11 04:41:52 -060075// Object Manager related
Patrick Williams9638afb2021-02-22 17:16:24 -060076static constexpr const char* ldapMgrObjBasePath =
Ratan Guptaaeaf9412019-02-11 04:41:52 -060077 "/xyz/openbmc_project/user/ldap";
78
79// Object Mapper related
Patrick Williams9638afb2021-02-22 17:16:24 -060080static constexpr const char* objMapperService =
Ratan Guptaaeaf9412019-02-11 04:41:52 -060081 "xyz.openbmc_project.ObjectMapper";
Patrick Williams9638afb2021-02-22 17:16:24 -060082static constexpr const char* objMapperPath =
Ratan Guptaaeaf9412019-02-11 04:41:52 -060083 "/xyz/openbmc_project/object_mapper";
Patrick Williams9638afb2021-02-22 17:16:24 -060084static constexpr const char* objMapperInterface =
Ratan Guptaaeaf9412019-02-11 04:41:52 -060085 "xyz.openbmc_project.ObjectMapper";
86
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053087using namespace phosphor::logging;
88using InsufficientPermission =
89 sdbusplus::xyz::openbmc_project::Common::Error::InsufficientPermission;
90using InternalFailure =
91 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
92using InvalidArgument =
93 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument;
94using UserNameExists =
95 sdbusplus::xyz::openbmc_project::User::Common::Error::UserNameExists;
96using UserNameDoesNotExist =
97 sdbusplus::xyz::openbmc_project::User::Common::Error::UserNameDoesNotExist;
98using UserNameGroupFail =
99 sdbusplus::xyz::openbmc_project::User::Common::Error::UserNameGroupFail;
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530100using NoResource =
101 sdbusplus::xyz::openbmc_project::User::Common::Error::NoResource;
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530102using Argument = xyz::openbmc_project::Common::InvalidArgument;
Nan Zhouda401fe2022-10-25 00:07:18 +0000103using GroupNameExists =
104 sdbusplus::xyz::openbmc_project::User::Common::Error::GroupNameExists;
105using GroupNameDoesNotExists =
106 sdbusplus::xyz::openbmc_project::User::Common::Error::GroupNameDoesNotExist;
107
108namespace
109{
110
111// The hardcoded groups in OpenBMC projects
Patrick Williams16c2b682024-08-16 15:20:56 -0400112constexpr std::array<const char*, 4> predefinedGroups = {
113 "redfish", "ipmi", "ssh", "hostconsole"};
Nan Zhouda401fe2022-10-25 00:07:18 +0000114
115// These prefixes are for Dynamic Redfish authorization. See
116// https://github.com/openbmc/docs/blob/master/designs/redfish-authorization.md
117
118// Base role and base privileges are added by Redfish implementation (e.g.,
119// BMCWeb) at compile time
120constexpr std::array<const char*, 4> allowedGroupPrefix = {
121 "openbmc_rfr_", // OpenBMC Redfish Base Role
122 "openbmc_rfp_", // OpenBMC Redfish Base Privileges
123 "openbmc_orfr_", // OpenBMC Redfish OEM Role
124 "openbmc_orfp_", // OpenBMC Redfish OEM Privileges
125};
126
127void checkAndThrowsForGroupChangeAllowed(const std::string& groupName)
128{
129 bool allowed = false;
130 for (std::string_view prefix : allowedGroupPrefix)
131 {
132 if (groupName.starts_with(prefix))
133 {
134 allowed = true;
135 break;
136 }
137 }
138 if (!allowed)
139 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +0800140 lg2::error("Group name '{GROUP}' is not in the allowed list", "GROUP",
141 groupName);
Nan Zhouda401fe2022-10-25 00:07:18 +0000142 elog<InvalidArgument>(Argument::ARGUMENT_NAME("Group Name"),
143 Argument::ARGUMENT_VALUE(groupName.c_str()));
144 }
145}
146
147} // namespace
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530148
Nan Zhoue47c09d2022-10-25 00:06:41 +0000149std::string getCSVFromVector(std::span<const std::string> vec)
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530150{
Nan Zhoue47c09d2022-10-25 00:06:41 +0000151 if (vec.empty())
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530152 {
Nan Zhoue47c09d2022-10-25 00:06:41 +0000153 return "";
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530154 }
Nan Zhoue47c09d2022-10-25 00:06:41 +0000155 return std::accumulate(std::next(vec.begin()), vec.end(), vec[0],
156 [](std::string&& val, std::string_view element) {
Patrick Williams16c2b682024-08-16 15:20:56 -0400157 val += ',';
158 val += element;
159 return val;
160 });
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530161}
162
Nan Zhou332fb9d2022-10-25 00:07:03 +0000163bool removeStringFromCSV(std::string& csvStr, const std::string& delStr)
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530164{
165 std::string::size_type delStrPos = csvStr.find(delStr);
166 if (delStrPos != std::string::npos)
167 {
168 // need to also delete the comma char
169 if (delStrPos == 0)
170 {
171 csvStr.erase(delStrPos, delStr.size() + 1);
172 }
173 else
174 {
175 csvStr.erase(delStrPos - 1, delStr.size() + 1);
176 }
177 return true;
178 }
179 return false;
180}
181
Patrick Williams9638afb2021-02-22 17:16:24 -0600182bool UserMgr::isUserExist(const std::string& userName)
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530183{
184 if (userName.empty())
185 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +0800186 lg2::error("User name is empty");
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530187 elog<InvalidArgument>(Argument::ARGUMENT_NAME("User name"),
188 Argument::ARGUMENT_VALUE("Null"));
189 }
190 if (usersList.find(userName) == usersList.end())
191 {
192 return false;
193 }
194 return true;
195}
196
Patrick Williams9638afb2021-02-22 17:16:24 -0600197void UserMgr::throwForUserDoesNotExist(const std::string& userName)
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530198{
Nan Zhou8a11d992022-10-25 00:07:06 +0000199 if (!isUserExist(userName))
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530200 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +0800201 lg2::error("User '{USERNAME}' does not exist", "USERNAME", userName);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530202 elog<UserNameDoesNotExist>();
203 }
204}
205
Nan Zhouda401fe2022-10-25 00:07:18 +0000206void UserMgr::checkAndThrowForDisallowedGroupCreation(
207 const std::string& groupName)
208{
209 if (groupName.size() > maxSystemGroupNameLength ||
210 !std::regex_match(groupName.c_str(),
nichanghao.nchd9adc732024-01-17 20:24:17 +0800211 std::regex("[a-zA-Z_][a-zA-Z_0-9]*")))
Nan Zhouda401fe2022-10-25 00:07:18 +0000212 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +0800213 lg2::error("Invalid group name '{GROUP}'", "GROUP", groupName);
Nan Zhouda401fe2022-10-25 00:07:18 +0000214 elog<InvalidArgument>(Argument::ARGUMENT_NAME("Group Name"),
215 Argument::ARGUMENT_VALUE(groupName.c_str()));
216 }
217 checkAndThrowsForGroupChangeAllowed(groupName);
218}
219
Patrick Williams9638afb2021-02-22 17:16:24 -0600220void UserMgr::throwForUserExists(const std::string& userName)
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530221{
Nan Zhou8a11d992022-10-25 00:07:06 +0000222 if (isUserExist(userName))
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530223 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +0800224 lg2::error("User '{USERNAME}' already exists", "USERNAME", userName);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530225 elog<UserNameExists>();
226 }
227}
228
229void UserMgr::throwForUserNameConstraints(
Patrick Williams9638afb2021-02-22 17:16:24 -0600230 const std::string& userName, const std::vector<std::string>& groupNames)
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530231{
232 if (std::find(groupNames.begin(), groupNames.end(), "ipmi") !=
233 groupNames.end())
234 {
235 if (userName.length() > ipmiMaxUserNameLen)
236 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +0800237 lg2::error("User '{USERNAME}' exceeds IPMI username length limit "
238 "({LENGTH} > {LIMIT})",
239 "USERNAME", userName, "LENGTH", userName.length(),
240 "LIMIT", ipmiMaxUserNameLen);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530241 elog<UserNameGroupFail>(
242 xyz::openbmc_project::User::Common::UserNameGroupFail::REASON(
243 "IPMI length"));
244 }
245 }
246 if (userName.length() > systemMaxUserNameLen)
247 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +0800248 lg2::error("User '{USERNAME}' exceeds system username length limit "
249 "({LENGTH} > {LIMIT})",
250 "USERNAME", userName, "LENGTH", userName.length(), "LIMIT",
251 systemMaxUserNameLen);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530252 elog<InvalidArgument>(Argument::ARGUMENT_NAME("User name"),
253 Argument::ARGUMENT_VALUE("Invalid length"));
254 }
255 if (!std::regex_match(userName.c_str(),
nichanghao.nchd9adc732024-01-17 20:24:17 +0800256 std::regex("[a-zA-Z_][a-zA-Z_0-9]*")))
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530257 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +0800258 lg2::error("Invalid username '{USERNAME}'", "USERNAME", userName);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530259 elog<InvalidArgument>(Argument::ARGUMENT_NAME("User name"),
260 Argument::ARGUMENT_VALUE("Invalid data"));
261 }
262}
263
264void UserMgr::throwForMaxGrpUserCount(
Patrick Williams9638afb2021-02-22 17:16:24 -0600265 const std::vector<std::string>& groupNames)
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530266{
267 if (std::find(groupNames.begin(), groupNames.end(), "ipmi") !=
268 groupNames.end())
269 {
270 if (getIpmiUsersCount() >= ipmiMaxUsers)
271 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +0800272 lg2::error("IPMI user limit reached");
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530273 elog<NoResource>(
274 xyz::openbmc_project::User::Common::NoResource::REASON(
Jiaqing Zhao11ec6662022-07-05 20:55:34 +0800275 "IPMI user limit reached"));
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530276 }
277 }
278 else
279 {
280 if (usersList.size() > 0 && (usersList.size() - getIpmiUsersCount()) >=
281 (maxSystemUsers - ipmiMaxUsers))
282 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +0800283 lg2::error("Non-ipmi User limit reached");
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530284 elog<NoResource>(
285 xyz::openbmc_project::User::Common::NoResource::REASON(
Jiaqing Zhao11ec6662022-07-05 20:55:34 +0800286 "Non-ipmi user limit reached"));
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530287 }
288 }
289 return;
290}
291
Patrick Williams9638afb2021-02-22 17:16:24 -0600292void UserMgr::throwForInvalidPrivilege(const std::string& priv)
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530293{
294 if (!priv.empty() &&
295 (std::find(privMgr.begin(), privMgr.end(), priv) == privMgr.end()))
296 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +0800297 lg2::error("Invalid privilege '{PRIVILEGE}'", "PRIVILEGE", priv);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530298 elog<InvalidArgument>(Argument::ARGUMENT_NAME("Privilege"),
299 Argument::ARGUMENT_VALUE(priv.c_str()));
300 }
301}
302
Patrick Williams9638afb2021-02-22 17:16:24 -0600303void UserMgr::throwForInvalidGroups(const std::vector<std::string>& groupNames)
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530304{
Patrick Williams9638afb2021-02-22 17:16:24 -0600305 for (auto& group : groupNames)
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530306 {
307 if (std::find(groupsMgr.begin(), groupsMgr.end(), group) ==
308 groupsMgr.end())
309 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +0800310 lg2::error("Invalid Group Name '{GROUPNAME}'", "GROUPNAME", group);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530311 elog<InvalidArgument>(Argument::ARGUMENT_NAME("GroupName"),
312 Argument::ARGUMENT_VALUE(group.c_str()));
313 }
314 }
315}
316
Nan Zhouda401fe2022-10-25 00:07:18 +0000317std::vector<std::string> UserMgr::readAllGroupsOnSystem()
318{
319 std::vector<std::string> allGroups = {predefinedGroups.begin(),
320 predefinedGroups.end()};
321 // rewinds to the beginning of the group database
322 setgrent();
323 struct group* gr = getgrent();
324 while (gr != nullptr)
325 {
326 std::string group(gr->gr_name);
327 for (std::string_view prefix : allowedGroupPrefix)
328 {
329 if (group.starts_with(prefix))
330 {
331 allGroups.push_back(gr->gr_name);
332 }
333 }
334 gr = getgrent();
335 }
336 // close the group database
337 endgrent();
338 return allGroups;
339}
340
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530341void UserMgr::createUser(std::string userName,
342 std::vector<std::string> groupNames, std::string priv,
343 bool enabled)
344{
345 throwForInvalidPrivilege(priv);
346 throwForInvalidGroups(groupNames);
347 // All user management lock has to be based on /etc/shadow
Andrew Geisslera260f182021-05-14 12:20:12 -0500348 // TODO phosphor-user-manager#10 phosphor::user::shadow::Lock lock{};
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530349 throwForUserExists(userName);
350 throwForUserNameConstraints(userName, groupNames);
351 throwForMaxGrpUserCount(groupNames);
352
353 std::string groups = getCSVFromVector(groupNames);
354 bool sshRequested = removeStringFromCSV(groups, grpSsh);
355
356 // treat privilege as a group - This is to avoid using different file to
357 // store the same.
Richard Marian Thomaiyar2cb2e722018-09-27 14:22:42 +0530358 if (!priv.empty())
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530359 {
Richard Marian Thomaiyar2cb2e722018-09-27 14:22:42 +0530360 if (groups.size() != 0)
361 {
362 groups += ",";
363 }
364 groups += priv;
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530365 }
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530366 try
367 {
Nan Zhou49c81362022-10-25 00:07:08 +0000368 executeUserAdd(userName.c_str(), groups.c_str(), sshRequested, enabled);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530369 }
Patrick Williams9638afb2021-02-22 17:16:24 -0600370 catch (const InternalFailure& e)
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530371 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +0800372 lg2::error("Unable to create new user '{USERNAME}'", "USERNAME",
373 userName);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530374 elog<InternalFailure>();
375 }
376
377 // Add the users object before sending out the signal
P Dheeraj Srujan Kumarb01e2fe2021-12-13 09:43:28 +0530378 sdbusplus::message::object_path tempObjPath(usersObjPath);
379 tempObjPath /= userName;
380 std::string userObj(tempObjPath);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530381 std::sort(groupNames.begin(), groupNames.end());
382 usersList.emplace(
Nan Zhou78d85042022-08-29 17:50:22 +0000383 userName, std::make_unique<phosphor::user::Users>(
384 bus, userObj.c_str(), groupNames, priv, enabled, *this));
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530385
Jiaqing Zhao11ec6662022-07-05 20:55:34 +0800386 lg2::info("User '{USERNAME}' created successfully", "USERNAME", userName);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530387 return;
388}
389
390void UserMgr::deleteUser(std::string userName)
391{
392 // All user management lock has to be based on /etc/shadow
Andrew Geisslera260f182021-05-14 12:20:12 -0500393 // TODO phosphor-user-manager#10 phosphor::user::shadow::Lock lock{};
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530394 throwForUserDoesNotExist(userName);
395 try
396 {
Jayanth Othayothac921a52023-07-21 03:48:55 -0500397 // Clear user fail records
398 executeUserClearFailRecords(userName.c_str());
Patrick Williams4b294622023-08-08 10:19:18 -0500399
400 executeUserDelete(userName.c_str());
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530401 }
Patrick Williams9638afb2021-02-22 17:16:24 -0600402 catch (const InternalFailure& e)
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530403 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +0800404 lg2::error("Delete User '{USERNAME}' failed", "USERNAME", userName);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530405 elog<InternalFailure>();
406 }
407
408 usersList.erase(userName);
409
Jiaqing Zhao11ec6662022-07-05 20:55:34 +0800410 lg2::info("User '{USERNAME}' deleted successfully", "USERNAME", userName);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530411 return;
412}
413
Nan Zhouda401fe2022-10-25 00:07:18 +0000414void UserMgr::checkDeleteGroupConstraints(const std::string& groupName)
415{
416 if (std::find(groupsMgr.begin(), groupsMgr.end(), groupName) ==
417 groupsMgr.end())
418 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +0800419 lg2::error("Group '{GROUP}' already exists", "GROUP", groupName);
Nan Zhouda401fe2022-10-25 00:07:18 +0000420 elog<GroupNameDoesNotExists>();
421 }
422 checkAndThrowsForGroupChangeAllowed(groupName);
423}
424
425void UserMgr::deleteGroup(std::string groupName)
426{
427 checkDeleteGroupConstraints(groupName);
428 try
429 {
430 executeGroupDeletion(groupName.c_str());
431 }
432 catch (const InternalFailure& e)
433 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +0800434 lg2::error("Failed to delete group '{GROUP}'", "GROUP", groupName);
Nan Zhouda401fe2022-10-25 00:07:18 +0000435 elog<InternalFailure>();
436 }
437
438 groupsMgr.erase(std::find(groupsMgr.begin(), groupsMgr.end(), groupName));
439 UserMgrIface::allGroups(groupsMgr);
Jiaqing Zhao11ec6662022-07-05 20:55:34 +0800440 lg2::info("Successfully deleted group '{GROUP}'", "GROUP", groupName);
Nan Zhouda401fe2022-10-25 00:07:18 +0000441}
442
443void UserMgr::checkCreateGroupConstraints(const std::string& groupName)
444{
445 if (std::find(groupsMgr.begin(), groupsMgr.end(), groupName) !=
446 groupsMgr.end())
447 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +0800448 lg2::error("Group '{GROUP}' already exists", "GROUP", groupName);
Nan Zhouda401fe2022-10-25 00:07:18 +0000449 elog<GroupNameExists>();
450 }
451 checkAndThrowForDisallowedGroupCreation(groupName);
452 if (groupsMgr.size() >= maxSystemGroupCount)
453 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +0800454 lg2::error("Group limit reached");
Nan Zhouda401fe2022-10-25 00:07:18 +0000455 elog<NoResource>(xyz::openbmc_project::User::Common::NoResource::REASON(
456 "Group limit reached"));
457 }
458}
459
460void UserMgr::createGroup(std::string groupName)
461{
462 checkCreateGroupConstraints(groupName);
463 try
464 {
465 executeGroupCreation(groupName.c_str());
466 }
467 catch (const InternalFailure& e)
468 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +0800469 lg2::error("Failed to create group '{GROUP}'", "GROUP", groupName);
Nan Zhouda401fe2022-10-25 00:07:18 +0000470 elog<InternalFailure>();
471 }
472 groupsMgr.push_back(groupName);
473 UserMgrIface::allGroups(groupsMgr);
474}
475
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530476void UserMgr::renameUser(std::string userName, std::string newUserName)
477{
478 // All user management lock has to be based on /etc/shadow
Andrew Geisslera260f182021-05-14 12:20:12 -0500479 // TODO phosphor-user-manager#10 phosphor::user::shadow::Lock lock{};
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530480 throwForUserDoesNotExist(userName);
481 throwForUserExists(newUserName);
482 throwForUserNameConstraints(newUserName,
483 usersList[userName].get()->userGroups());
484 try
485 {
Nan Zhouf25443e2022-10-25 00:07:11 +0000486 executeUserRename(userName.c_str(), newUserName.c_str());
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530487 }
Patrick Williams9638afb2021-02-22 17:16:24 -0600488 catch (const InternalFailure& e)
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530489 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +0800490 lg2::error("Rename '{USERNAME}' to '{NEWUSERNAME}' failed", "USERNAME",
491 userName, "NEWUSERNAME", newUserName);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530492 elog<InternalFailure>();
493 }
Patrick Williams9638afb2021-02-22 17:16:24 -0600494 const auto& user = usersList[userName];
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530495 std::string priv = user.get()->userPrivilege();
496 std::vector<std::string> groupNames = user.get()->userGroups();
497 bool enabled = user.get()->userEnabled();
P Dheeraj Srujan Kumarb01e2fe2021-12-13 09:43:28 +0530498 sdbusplus::message::object_path tempObjPath(usersObjPath);
499 tempObjPath /= newUserName;
500 std::string newUserObj(tempObjPath);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530501 // Special group 'ipmi' needs a way to identify user renamed, in order to
502 // update encrypted password. It can't rely only on InterfacesRemoved &
503 // InterfacesAdded. So first send out userRenamed signal.
504 this->userRenamed(userName, newUserName);
505 usersList.erase(userName);
Nan Zhou78d85042022-08-29 17:50:22 +0000506 usersList.emplace(newUserName, std::make_unique<phosphor::user::Users>(
507 bus, newUserObj.c_str(), groupNames,
508 priv, enabled, *this));
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530509 return;
510}
511
Patrick Williams9638afb2021-02-22 17:16:24 -0600512void UserMgr::updateGroupsAndPriv(const std::string& userName,
Nan Zhoufef63032022-10-25 00:07:12 +0000513 std::vector<std::string> groupNames,
Patrick Williams9638afb2021-02-22 17:16:24 -0600514 const std::string& priv)
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530515{
516 throwForInvalidPrivilege(priv);
517 throwForInvalidGroups(groupNames);
518 // All user management lock has to be based on /etc/shadow
Andrew Geisslera260f182021-05-14 12:20:12 -0500519 // TODO phosphor-user-manager#10 phosphor::user::shadow::Lock lock{};
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530520 throwForUserDoesNotExist(userName);
Patrick Williams9638afb2021-02-22 17:16:24 -0600521 const std::vector<std::string>& oldGroupNames =
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530522 usersList[userName].get()->userGroups();
523 std::vector<std::string> groupDiff;
524 // Note: already dealing with sorted group lists.
525 std::set_symmetric_difference(oldGroupNames.begin(), oldGroupNames.end(),
526 groupNames.begin(), groupNames.end(),
527 std::back_inserter(groupDiff));
528 if (std::find(groupDiff.begin(), groupDiff.end(), "ipmi") !=
529 groupDiff.end())
530 {
531 throwForUserNameConstraints(userName, groupNames);
532 throwForMaxGrpUserCount(groupNames);
533 }
534
535 std::string groups = getCSVFromVector(groupNames);
536 bool sshRequested = removeStringFromCSV(groups, grpSsh);
537
538 // treat privilege as a group - This is to avoid using different file to
539 // store the same.
Richard Marian Thomaiyar2cb2e722018-09-27 14:22:42 +0530540 if (!priv.empty())
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530541 {
Richard Marian Thomaiyar2cb2e722018-09-27 14:22:42 +0530542 if (groups.size() != 0)
543 {
544 groups += ",";
545 }
546 groups += priv;
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530547 }
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530548 try
549 {
Nan Zhoufef63032022-10-25 00:07:12 +0000550 executeUserModify(userName.c_str(), groups.c_str(), sshRequested);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530551 }
Patrick Williams9638afb2021-02-22 17:16:24 -0600552 catch (const InternalFailure& e)
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530553 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +0800554 lg2::error(
555 "Unable to modify user privilege / groups for user '{USERNAME}'",
556 "USERNAME", userName);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530557 elog<InternalFailure>();
558 }
559
Nan Zhoufef63032022-10-25 00:07:12 +0000560 std::sort(groupNames.begin(), groupNames.end());
561 usersList[userName]->setUserGroups(groupNames);
562 usersList[userName]->setUserPrivilege(priv);
Jiaqing Zhao11ec6662022-07-05 20:55:34 +0800563 lg2::info("User '{USERNAME}' groups / privilege updated successfully",
564 "USERNAME", userName);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530565}
566
Richard Marian Thomaiyar9164fd92018-06-13 16:51:00 +0530567uint8_t UserMgr::minPasswordLength(uint8_t value)
568{
569 if (value == AccountPolicyIface::minPasswordLength())
570 {
571 return value;
572 }
573 if (value < minPasswdLength)
574 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +0800575 lg2::error("Attempting to set minPasswordLength to {VALUE}, less than "
576 "{MINVALUE}",
577 "VALUE", value, "MINVALUE", minPasswdLength);
Paul Fertser6dc7ed92022-01-21 19:36:34 +0000578 elog<InvalidArgument>(
579 Argument::ARGUMENT_NAME("minPasswordLength"),
580 Argument::ARGUMENT_VALUE(std::to_string(value).c_str()));
Richard Marian Thomaiyar9164fd92018-06-13 16:51:00 +0530581 }
Jason M. Bills2d042d12023-03-28 15:32:45 -0700582 if (setPamModuleConfValue(pwQualityConfigFile, minPasswdLenProp,
583 std::to_string(value)) != success)
Richard Marian Thomaiyar9164fd92018-06-13 16:51:00 +0530584 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +0800585 lg2::error("Unable to set minPasswordLength to {VALUE}", "VALUE",
586 value);
Richard Marian Thomaiyar9164fd92018-06-13 16:51:00 +0530587 elog<InternalFailure>();
588 }
589 return AccountPolicyIface::minPasswordLength(value);
590}
591
592uint8_t UserMgr::rememberOldPasswordTimes(uint8_t value)
593{
594 if (value == AccountPolicyIface::rememberOldPasswordTimes())
595 {
596 return value;
597 }
Jason M. Bills3b280ec2023-08-15 16:15:48 -0700598 if (setPamModuleConfValue(pwHistoryConfigFile, remOldPasswdCount,
599 std::to_string(value)) != success)
Richard Marian Thomaiyar9164fd92018-06-13 16:51:00 +0530600 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +0800601 lg2::error("Unable to set rememberOldPasswordTimes to {VALUE}", "VALUE",
602 value);
Richard Marian Thomaiyar9164fd92018-06-13 16:51:00 +0530603 elog<InternalFailure>();
604 }
605 return AccountPolicyIface::rememberOldPasswordTimes(value);
606}
607
608uint16_t UserMgr::maxLoginAttemptBeforeLockout(uint16_t value)
609{
610 if (value == AccountPolicyIface::maxLoginAttemptBeforeLockout())
611 {
612 return value;
613 }
Jason M. Bills2d042d12023-03-28 15:32:45 -0700614 if (setPamModuleConfValue(faillockConfigFile, maxFailedAttempt,
615 std::to_string(value)) != success)
Richard Marian Thomaiyar9164fd92018-06-13 16:51:00 +0530616 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +0800617 lg2::error("Unable to set maxLoginAttemptBeforeLockout to {VALUE}",
618 "VALUE", value);
Richard Marian Thomaiyar9164fd92018-06-13 16:51:00 +0530619 elog<InternalFailure>();
620 }
621 return AccountPolicyIface::maxLoginAttemptBeforeLockout(value);
622}
623
624uint32_t UserMgr::accountUnlockTimeout(uint32_t value)
625{
626 if (value == AccountPolicyIface::accountUnlockTimeout())
627 {
628 return value;
629 }
Jason M. Bills2d042d12023-03-28 15:32:45 -0700630 if (setPamModuleConfValue(faillockConfigFile, unlockTimeout,
631 std::to_string(value)) != success)
Richard Marian Thomaiyar9164fd92018-06-13 16:51:00 +0530632 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +0800633 lg2::error("Unable to set accountUnlockTimeout to {VALUE}", "VALUE",
634 value);
Richard Marian Thomaiyar9164fd92018-06-13 16:51:00 +0530635 elog<InternalFailure>();
636 }
637 return AccountPolicyIface::accountUnlockTimeout(value);
638}
639
Jason M. Bills2d042d12023-03-28 15:32:45 -0700640int UserMgr::getPamModuleConfValue(const std::string& confFile,
641 const std::string& argName,
642 std::string& argValue)
643{
644 std::ifstream fileToRead(confFile, std::ios::in);
645 if (!fileToRead.is_open())
646 {
647 lg2::error("Failed to open pam configuration file {FILENAME}",
648 "FILENAME", confFile);
649 return failure;
650 }
651 std::string line;
652 auto argSearch = argName + "=";
653 size_t startPos = 0;
654 size_t endPos = 0;
655 while (getline(fileToRead, line))
656 {
657 // skip comments section starting with #
658 if ((startPos = line.find('#')) != std::string::npos)
659 {
660 if (startPos == 0)
661 {
662 continue;
663 }
664 // skip comments after meaningful section and process those
665 line = line.substr(0, startPos);
666 }
667 if ((startPos = line.find(argSearch)) != std::string::npos)
668 {
669 if ((endPos = line.find(' ', startPos)) == std::string::npos)
670 {
671 endPos = line.size();
672 }
673 startPos += argSearch.size();
674 argValue = line.substr(startPos, endPos - startPos);
675 return success;
676 }
677 }
678 return failure;
679}
680
Jason M. Bills2d042d12023-03-28 15:32:45 -0700681int UserMgr::setPamModuleConfValue(const std::string& confFile,
682 const std::string& argName,
683 const std::string& argValue)
684{
685 std::string tmpConfFile = confFile + "_tmp";
686 std::ifstream fileToRead(confFile, std::ios::in);
687 std::ofstream fileToWrite(tmpConfFile, std::ios::out);
688 if (!fileToRead.is_open() || !fileToWrite.is_open())
689 {
690 lg2::error("Failed to open pam configuration file {FILENAME}",
691 "FILENAME", confFile);
Jason M. Bills17b88272023-05-22 14:24:02 -0700692 // Delete the unused tmp file
693 std::remove(tmpConfFile.c_str());
Jason M. Bills2d042d12023-03-28 15:32:45 -0700694 return failure;
695 }
696 std::string line;
697 auto argSearch = argName + "=";
698 size_t startPos = 0;
699 size_t endPos = 0;
700 bool found = false;
701 while (getline(fileToRead, line))
702 {
703 // skip comments section starting with #
704 if ((startPos = line.find('#')) != std::string::npos)
705 {
706 if (startPos == 0)
707 {
708 fileToWrite << line << std::endl;
709 continue;
710 }
711 // skip comments after meaningful section and process those
712 line = line.substr(0, startPos);
713 }
714 if ((startPos = line.find(argSearch)) != std::string::npos)
715 {
716 if ((endPos = line.find(' ', startPos)) == std::string::npos)
717 {
718 endPos = line.size();
719 }
720 startPos += argSearch.size();
721 fileToWrite << line.substr(0, startPos) << argValue
722 << line.substr(endPos, line.size() - endPos)
723 << std::endl;
724 found = true;
725 continue;
726 }
727 fileToWrite << line << std::endl;
728 }
729 fileToWrite.close();
730 fileToRead.close();
731 if (found)
732 {
733 if (std::rename(tmpConfFile.c_str(), confFile.c_str()) == 0)
734 {
735 return success;
736 }
737 }
Jason M. Bills17b88272023-05-22 14:24:02 -0700738 // No changes, so delete the unused tmp file
739 std::remove(tmpConfFile.c_str());
Jason M. Bills2d042d12023-03-28 15:32:45 -0700740 return failure;
741}
742
Patrick Williams9638afb2021-02-22 17:16:24 -0600743void UserMgr::userEnable(const std::string& userName, bool enabled)
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530744{
745 // All user management lock has to be based on /etc/shadow
Andrew Geisslera260f182021-05-14 12:20:12 -0500746 // TODO phosphor-user-manager#10 phosphor::user::shadow::Lock lock{};
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530747 throwForUserDoesNotExist(userName);
748 try
749 {
Nan Zhou6b6f2d82022-10-25 00:07:17 +0000750 executeUserModifyUserEnable(userName.c_str(), enabled);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530751 }
Patrick Williams9638afb2021-02-22 17:16:24 -0600752 catch (const InternalFailure& e)
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530753 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +0800754 lg2::error("Unable to modify user enabled state for '{USERNAME}'",
755 "USERNAME", userName);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530756 elog<InternalFailure>();
757 }
758
Nan Zhou6b6f2d82022-10-25 00:07:17 +0000759 usersList[userName]->setUserEnabled(enabled);
Jiaqing Zhao11ec6662022-07-05 20:55:34 +0800760 lg2::info("User '{USERNAME}' has been {STATUS}", "USERNAME", userName,
761 "STATUS", enabled ? "Enabled" : "Disabled");
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530762}
763
Richard Marian Thomaiyarc7045192018-06-13 16:51:00 +0530764/**
Jason M. Bills2d042d12023-03-28 15:32:45 -0700765 * faillock app will provide the user failed login list with when the attempt
766 * was made, the type, the source, and if it's valid.
767 *
768 * Valid in this case means that the attempt was made within the fail_interval
769 * time. So, we can check this list for the number of valid entries (lines
770 * ending with 'V') compared to the maximum allowed to determine if the user is
771 * locked out.
772 *
773 * This data is only refreshed when an attempt is made, so if the user appears
774 * to be locked out, we must also check if the most recent attempt was older
775 * than the unlock_time to know if the user has since been unlocked.
Richard Marian Thomaiyarc7045192018-06-13 16:51:00 +0530776 **/
Jason M. Bills2d042d12023-03-28 15:32:45 -0700777bool UserMgr::parseFaillockForLockout(
778 const std::vector<std::string>& faillockOutput)
779{
780 uint16_t failAttempts = 0;
781 time_t lastFailedAttempt{};
782 for (const std::string& line : faillockOutput)
783 {
784 if (!line.ends_with("V"))
785 {
786 continue;
787 }
Richard Marian Thomaiyarc7045192018-06-13 16:51:00 +0530788
Jason M. Bills2d042d12023-03-28 15:32:45 -0700789 // Count this failed attempt
790 failAttempts++;
791
792 // Update the last attempt time
793 // First get the "when" which is the first two words (date and time)
794 size_t pos = line.find(" ");
795 if (pos == std::string::npos)
796 {
797 continue;
798 }
799 pos = line.find(" ", pos + 1);
800 if (pos == std::string::npos)
801 {
802 continue;
803 }
804 std::string failDateTime = line.substr(0, pos);
805
806 // NOTE: Cannot use std::get_time() here as the implementation of %y in
807 // libstdc++ does not match POSIX strptime() before gcc 12.1.0
808 // https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=a8d3c98746098e2784be7144c1ccc9fcc34a0888
809 std::tm tmStruct = {};
810 if (!strptime(failDateTime.c_str(), "%F %T", &tmStruct))
811 {
812 lg2::error("Failed to parse latest failure date/time");
813 elog<InternalFailure>();
814 }
815
816 time_t failTimestamp = std::mktime(&tmStruct);
817 lastFailedAttempt = std::max(failTimestamp, lastFailedAttempt);
818 }
819
820 if (failAttempts < AccountPolicyIface::maxLoginAttemptBeforeLockout())
821 {
822 return false;
823 }
824
825 if (lastFailedAttempt +
826 static_cast<time_t>(AccountPolicyIface::accountUnlockTimeout()) <=
827 std::time(NULL))
828 {
829 return false;
830 }
831
832 return true;
833}
Richard Marian Thomaiyarc7045192018-06-13 16:51:00 +0530834
Patrick Williams9638afb2021-02-22 17:16:24 -0600835bool UserMgr::userLockedForFailedAttempt(const std::string& userName)
Richard Marian Thomaiyarc7045192018-06-13 16:51:00 +0530836{
837 // All user management lock has to be based on /etc/shadow
Andrew Geisslera260f182021-05-14 12:20:12 -0500838 // TODO phosphor-user-manager#10 phosphor::user::shadow::Lock lock{};
Jiaqing Zhaofba4bb12022-06-24 01:30:57 +0800839 if (AccountPolicyIface::maxLoginAttemptBeforeLockout() == 0)
840 {
841 return false;
842 }
843
Richard Marian Thomaiyarc7045192018-06-13 16:51:00 +0530844 std::vector<std::string> output;
Jiaqing Zhao8557d322022-06-23 23:00:01 +0800845 try
846 {
Nan Zhoua2953032022-11-11 21:50:32 +0000847 output = getFailedAttempt(userName.c_str());
Jiaqing Zhao8557d322022-06-23 23:00:01 +0800848 }
849 catch (const InternalFailure& e)
850 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +0800851 lg2::error("Unable to read login failure counter");
Jiaqing Zhao8557d322022-06-23 23:00:01 +0800852 elog<InternalFailure>();
853 }
Richard Marian Thomaiyarc7045192018-06-13 16:51:00 +0530854
Jason M. Bills2d042d12023-03-28 15:32:45 -0700855 return parseFaillockForLockout(output);
Richard Marian Thomaiyarc7045192018-06-13 16:51:00 +0530856}
857
Patrick Williams9638afb2021-02-22 17:16:24 -0600858bool UserMgr::userLockedForFailedAttempt(const std::string& userName,
859 const bool& value)
Richard Marian Thomaiyarc7045192018-06-13 16:51:00 +0530860{
861 // All user management lock has to be based on /etc/shadow
Andrew Geisslera260f182021-05-14 12:20:12 -0500862 // TODO phosphor-user-manager#10 phosphor::user::shadow::Lock lock{};
Richard Marian Thomaiyarc7045192018-06-13 16:51:00 +0530863 if (value == true)
864 {
865 return userLockedForFailedAttempt(userName);
866 }
Richard Marian Thomaiyarc7045192018-06-13 16:51:00 +0530867
Jiaqing Zhao8557d322022-06-23 23:00:01 +0800868 try
869 {
Jayanth Othayothac921a52023-07-21 03:48:55 -0500870 // Clear user fail records
871 executeUserClearFailRecords(userName.c_str());
Jiaqing Zhao8557d322022-06-23 23:00:01 +0800872 }
873 catch (const InternalFailure& e)
874 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +0800875 lg2::error("Unable to reset login failure counter");
Jiaqing Zhao8557d322022-06-23 23:00:01 +0800876 elog<InternalFailure>();
877 }
Richard Marian Thomaiyarc7045192018-06-13 16:51:00 +0530878
Richard Marian Thomaiyarf5c2df52018-11-22 23:24:25 +0530879 return userLockedForFailedAttempt(userName);
Richard Marian Thomaiyarc7045192018-06-13 16:51:00 +0530880}
881
Patrick Williams9638afb2021-02-22 17:16:24 -0600882bool UserMgr::userPasswordExpired(const std::string& userName)
Joseph Reynolds3ab6cc22020-03-03 14:09:03 -0600883{
884 // All user management lock has to be based on /etc/shadow
Andrew Geisslera260f182021-05-14 12:20:12 -0500885 // TODO phosphor-user-manager#10 phosphor::user::shadow::Lock lock{};
Joseph Reynolds3ab6cc22020-03-03 14:09:03 -0600886
887 struct spwd spwd
Patrick Williams9638afb2021-02-22 17:16:24 -0600888 {};
889 struct spwd* spwdPtr = nullptr;
Joseph Reynolds3ab6cc22020-03-03 14:09:03 -0600890 auto buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
891 if (buflen < -1)
892 {
893 // Use a default size if there is no hard limit suggested by sysconf()
894 buflen = 1024;
895 }
896 std::vector<char> buffer(buflen);
Patrick Williams16c2b682024-08-16 15:20:56 -0400897 auto status =
898 getspnam_r(userName.c_str(), &spwd, buffer.data(), buflen, &spwdPtr);
Joseph Reynolds3ab6cc22020-03-03 14:09:03 -0600899 // On success, getspnam_r() returns zero, and sets *spwdPtr to spwd.
900 // If no matching password record was found, these functions return 0
901 // and store NULL in *spwdPtr
902 if ((status == 0) && (&spwd == spwdPtr))
903 {
904 // Determine password validity per "chage" docs, where:
905 // spwd.sp_lstchg == 0 means password is expired, and
906 // spwd.sp_max == -1 means the password does not expire.
Nan Zhou78d85042022-08-29 17:50:22 +0000907 constexpr long secondsPerDay = 60 * 60 * 24;
908 long today = static_cast<long>(time(NULL)) / secondsPerDay;
Joseph Reynolds3ab6cc22020-03-03 14:09:03 -0600909 if ((spwd.sp_lstchg == 0) ||
910 ((spwd.sp_max != -1) && ((spwd.sp_max + spwd.sp_lstchg) < today)))
911 {
912 return true;
913 }
914 }
915 else
916 {
Jayaprakash Mutyala75be4e62020-09-18 15:59:06 +0000917 // User entry is missing in /etc/shadow, indicating no SHA password.
918 // Treat this as new user without password entry in /etc/shadow
919 // TODO: Add property to indicate user password was not set yet
920 // https://github.com/openbmc/phosphor-user-manager/issues/8
921 return false;
Joseph Reynolds3ab6cc22020-03-03 14:09:03 -0600922 }
923
924 return false;
925}
926
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530927UserSSHLists UserMgr::getUserAndSshGrpList()
928{
929 // All user management lock has to be based on /etc/shadow
Andrew Geisslera260f182021-05-14 12:20:12 -0500930 // TODO phosphor-user-manager#10 phosphor::user::shadow::Lock lock{};
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530931
932 std::vector<std::string> userList;
933 std::vector<std::string> sshUsersList;
934 struct passwd pw, *pwp = nullptr;
935 std::array<char, 1024> buffer{};
936
937 phosphor::user::File passwd(passwdFileName, "r");
938 if ((passwd)() == NULL)
939 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +0800940 lg2::error("Error opening {FILENAME}", "FILENAME", passwdFileName);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530941 elog<InternalFailure>();
942 }
943
944 while (true)
945 {
946 auto r = fgetpwent_r((passwd)(), &pw, buffer.data(), buffer.max_size(),
947 &pwp);
948 if ((r != 0) || (pwp == NULL))
949 {
950 // Any error, break the loop.
951 break;
952 }
Richard Marian Thomaiyard4d65502019-11-02 21:02:03 +0530953#ifdef ENABLE_ROOT_USER_MGMT
Richard Marian Thomaiyar7ba3c712018-07-31 13:41:36 +0530954 // Add all users whose UID >= 1000 and < 65534
955 // and special UID 0.
956 if ((pwp->pw_uid == 0) ||
957 ((pwp->pw_uid >= 1000) && (pwp->pw_uid < 65534)))
Richard Marian Thomaiyard4d65502019-11-02 21:02:03 +0530958#else
959 // Add all users whose UID >=1000 and < 65534
960 if ((pwp->pw_uid >= 1000) && (pwp->pw_uid < 65534))
961#endif
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530962 {
963 std::string userName(pwp->pw_name);
964 userList.emplace_back(userName);
965
966 // ssh doesn't have separate group. Check login shell entry to
967 // get all users list which are member of ssh group.
968 std::string loginShell(pwp->pw_shell);
969 if (loginShell == "/bin/sh")
970 {
971 sshUsersList.emplace_back(userName);
972 }
973 }
974 }
975 endpwent();
976 return std::make_pair(std::move(userList), std::move(sshUsersList));
977}
978
979size_t UserMgr::getIpmiUsersCount()
980{
981 std::vector<std::string> userList = getUsersInGroup("ipmi");
982 return userList.size();
983}
984
Nan Zhou49c81362022-10-25 00:07:08 +0000985size_t UserMgr::getNonIpmiUsersCount()
986{
987 std::vector<std::string> ipmiUsers = getUsersInGroup("ipmi");
988 return usersList.size() - ipmiUsers.size();
989}
990
Patrick Williams9638afb2021-02-22 17:16:24 -0600991bool UserMgr::isUserEnabled(const std::string& userName)
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530992{
993 // All user management lock has to be based on /etc/shadow
Andrew Geisslera260f182021-05-14 12:20:12 -0500994 // TODO phosphor-user-manager#10 phosphor::user::shadow::Lock lock{};
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530995 std::array<char, 4096> buffer{};
996 struct spwd spwd;
Patrick Williams9638afb2021-02-22 17:16:24 -0600997 struct spwd* resultPtr = nullptr;
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530998 int status = getspnam_r(userName.c_str(), &spwd, buffer.data(),
999 buffer.max_size(), &resultPtr);
1000 if (!status && (&spwd == resultPtr))
1001 {
1002 if (resultPtr->sp_expire >= 0)
1003 {
1004 return false; // user locked out
1005 }
1006 return true;
1007 }
1008 return false; // assume user is disabled for any error.
1009}
1010
Patrick Williams9638afb2021-02-22 17:16:24 -06001011std::vector<std::string> UserMgr::getUsersInGroup(const std::string& groupName)
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +05301012{
1013 std::vector<std::string> usersInGroup;
1014 // Should be more than enough to get the pwd structure.
1015 std::array<char, 4096> buffer{};
1016 struct group grp;
Patrick Williams9638afb2021-02-22 17:16:24 -06001017 struct group* resultPtr = nullptr;
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +05301018
1019 int status = getgrnam_r(groupName.c_str(), &grp, buffer.data(),
1020 buffer.max_size(), &resultPtr);
1021
1022 if (!status && (&grp == resultPtr))
1023 {
1024 for (; *(grp.gr_mem) != NULL; ++(grp.gr_mem))
1025 {
1026 usersInGroup.emplace_back(*(grp.gr_mem));
1027 }
1028 }
1029 else
1030 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +08001031 lg2::error("Group '{GROUPNAME}' not found", "GROUPNAME", groupName);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +05301032 // Don't throw error, just return empty userList - fallback
1033 }
1034 return usersInGroup;
1035}
1036
Ratan Guptaaeaf9412019-02-11 04:41:52 -06001037DbusUserObj UserMgr::getPrivilegeMapperObject(void)
1038{
1039 DbusUserObj objects;
1040 try
1041 {
Ravi Teja5fe724a2019-05-07 05:14:42 -05001042 std::string basePath = "/xyz/openbmc_project/user/ldap/openldap";
1043 std::string interface = "xyz.openbmc_project.User.Ldap.Config";
Ratan Guptaaeaf9412019-02-11 04:41:52 -06001044
Patrick Williams16c2b682024-08-16 15:20:56 -04001045 auto ldapMgmtService =
1046 getServiceName(std::move(basePath), std::move(interface));
Ratan Guptaaeaf9412019-02-11 04:41:52 -06001047 auto method = bus.new_method_call(
1048 ldapMgmtService.c_str(), ldapMgrObjBasePath,
1049 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1050
1051 auto reply = bus.call(method);
1052 reply.read(objects);
1053 }
Patrick Williams9638afb2021-02-22 17:16:24 -06001054 catch (const InternalFailure& e)
Ratan Guptaaeaf9412019-02-11 04:41:52 -06001055 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +08001056 lg2::error("Unable to get the User Service: {ERR}", "ERR", e);
Ratan Guptaaeaf9412019-02-11 04:41:52 -06001057 throw;
1058 }
Patrick Williamsb3ef4e12022-07-22 19:26:55 -05001059 catch (const sdbusplus::exception_t& e)
Ratan Guptaaeaf9412019-02-11 04:41:52 -06001060 {
Manojkiran Eda46e773a2024-06-17 14:45:33 +05301061 lg2::error("Failed to execute GetManagedObjects at {PATH}: {ERR}",
Jiaqing Zhao11ec6662022-07-05 20:55:34 +08001062 "PATH", ldapMgrObjBasePath, "ERR", e);
Ratan Guptaaeaf9412019-02-11 04:41:52 -06001063 throw;
1064 }
1065 return objects;
1066}
1067
Patrick Williams9638afb2021-02-22 17:16:24 -06001068std::string UserMgr::getServiceName(std::string&& path, std::string&& intf)
Ratan Guptaaeaf9412019-02-11 04:41:52 -06001069{
1070 auto mapperCall = bus.new_method_call(objMapperService, objMapperPath,
1071 objMapperInterface, "GetObject");
1072
1073 mapperCall.append(std::move(path));
1074 mapperCall.append(std::vector<std::string>({std::move(intf)}));
1075
1076 auto mapperResponseMsg = bus.call(mapperCall);
1077
1078 if (mapperResponseMsg.is_method_error())
1079 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +08001080 lg2::error("Error in mapper call");
Ratan Guptaaeaf9412019-02-11 04:41:52 -06001081 elog<InternalFailure>();
1082 }
1083
1084 std::map<std::string, std::vector<std::string>> mapperResponse;
1085 mapperResponseMsg.read(mapperResponse);
1086
1087 if (mapperResponse.begin() == mapperResponse.end())
1088 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +08001089 lg2::error("Invalid response from mapper");
Ratan Guptaaeaf9412019-02-11 04:41:52 -06001090 elog<InternalFailure>();
1091 }
1092
1093 return mapperResponse.begin()->first;
1094}
1095
Alexander Filippov75626582022-02-09 18:42:37 +03001096gid_t UserMgr::getPrimaryGroup(const std::string& userName) const
1097{
1098 static auto buflen = sysconf(_SC_GETPW_R_SIZE_MAX);
1099 if (buflen <= 0)
1100 {
1101 // Use a default size if there is no hard limit suggested by sysconf()
1102 buflen = 1024;
1103 }
1104
1105 struct passwd pwd;
1106 struct passwd* pwdPtr = nullptr;
1107 std::vector<char> buffer(buflen);
1108
1109 auto status = getpwnam_r(userName.c_str(), &pwd, buffer.data(),
1110 buffer.size(), &pwdPtr);
1111 // On success, getpwnam_r() returns zero, and set *pwdPtr to pwd.
1112 // If no matching password record was found, these functions return 0
1113 // and store NULL in *pwdPtr
1114 if (!status && (&pwd == pwdPtr))
1115 {
1116 return pwd.pw_gid;
1117 }
1118
Jiaqing Zhao11ec6662022-07-05 20:55:34 +08001119 lg2::error("User {USERNAME} does not exist", "USERNAME", userName);
Alexander Filippov75626582022-02-09 18:42:37 +03001120 elog<UserNameDoesNotExist>();
1121}
1122
1123bool UserMgr::isGroupMember(const std::string& userName, gid_t primaryGid,
1124 const std::string& groupName) const
1125{
1126 static auto buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
1127 if (buflen <= 0)
1128 {
1129 // Use a default size if there is no hard limit suggested by sysconf()
1130 buflen = 1024;
1131 }
1132
1133 struct group grp;
1134 struct group* grpPtr = nullptr;
1135 std::vector<char> buffer(buflen);
1136
1137 auto status = getgrnam_r(groupName.c_str(), &grp, buffer.data(),
1138 buffer.size(), &grpPtr);
1139
1140 // Groups with a lot of members may require a buffer of bigger size than
1141 // suggested by _SC_GETGR_R_SIZE_MAX.
1142 // 32K should be enough for about 2K members.
1143 constexpr auto maxBufferLength = 32 * 1024;
1144 while (status == ERANGE && buflen < maxBufferLength)
1145 {
1146 buflen *= 2;
1147 buffer.resize(buflen);
1148
Jiaqing Zhao11ec6662022-07-05 20:55:34 +08001149 lg2::debug("Increase buffer for getgrnam_r() to {SIZE}", "SIZE",
1150 buflen);
Alexander Filippov75626582022-02-09 18:42:37 +03001151
1152 status = getgrnam_r(groupName.c_str(), &grp, buffer.data(),
1153 buffer.size(), &grpPtr);
1154 }
1155
1156 // On success, getgrnam_r() returns zero, and set *grpPtr to grp.
1157 // If no matching group record was found, these functions return 0
1158 // and store NULL in *grpPtr
1159 if (!status && (&grp == grpPtr))
1160 {
1161 if (primaryGid == grp.gr_gid)
1162 {
1163 return true;
1164 }
1165
1166 for (auto i = 0; grp.gr_mem && grp.gr_mem[i]; ++i)
1167 {
1168 if (userName == grp.gr_mem[i])
1169 {
1170 return true;
1171 }
1172 }
1173 }
1174 else if (status == ERANGE)
1175 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +08001176 lg2::error("Group info of {GROUP} requires too much memory", "GROUP",
1177 groupName);
Alexander Filippov75626582022-02-09 18:42:37 +03001178 }
1179 else
1180 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +08001181 lg2::error("Group {GROUP} does not exist", "GROUP", groupName);
Alexander Filippov75626582022-02-09 18:42:37 +03001182 }
1183
1184 return false;
1185}
1186
Nan Zhouda401fe2022-10-25 00:07:18 +00001187void UserMgr::executeGroupCreation(const char* groupName)
1188{
1189 executeCmd("/usr/sbin/groupadd", groupName);
1190}
1191
1192void UserMgr::executeGroupDeletion(const char* groupName)
1193{
1194 executeCmd("/usr/sbin/groupdel", groupName);
1195}
1196
Ratan Guptaaeaf9412019-02-11 04:41:52 -06001197UserInfoMap UserMgr::getUserInfo(std::string userName)
1198{
1199 UserInfoMap userInfo;
1200 // Check whether the given user is local user or not.
Nan Zhou8a11d992022-10-25 00:07:06 +00001201 if (isUserExist(userName))
Ratan Guptaaeaf9412019-02-11 04:41:52 -06001202 {
Patrick Williams9638afb2021-02-22 17:16:24 -06001203 const auto& user = usersList[userName];
Ratan Guptaaeaf9412019-02-11 04:41:52 -06001204 userInfo.emplace("UserPrivilege", user.get()->userPrivilege());
1205 userInfo.emplace("UserGroups", user.get()->userGroups());
1206 userInfo.emplace("UserEnabled", user.get()->userEnabled());
1207 userInfo.emplace("UserLockedForFailedAttempt",
1208 user.get()->userLockedForFailedAttempt());
Joseph Reynolds3ab6cc22020-03-03 14:09:03 -06001209 userInfo.emplace("UserPasswordExpired",
1210 user.get()->userPasswordExpired());
Ratan Guptaaeaf9412019-02-11 04:41:52 -06001211 userInfo.emplace("RemoteUser", false);
1212 }
1213 else
1214 {
Alexander Filippov75626582022-02-09 18:42:37 +03001215 auto primaryGid = getPrimaryGroup(userName);
Ratan Guptaaeaf9412019-02-11 04:41:52 -06001216
1217 DbusUserObj objects = getPrivilegeMapperObject();
1218
Ravi Teja5fe724a2019-05-07 05:14:42 -05001219 std::string ldapConfigPath;
Jiaqing Zhao745ce2e2022-05-31 17:11:31 +08001220 std::string userPrivilege;
Ratan Guptaaeaf9412019-02-11 04:41:52 -06001221
1222 try
1223 {
Alexander Filippov75626582022-02-09 18:42:37 +03001224 for (const auto& [path, interfaces] : objects)
Ratan Guptaaeaf9412019-02-11 04:41:52 -06001225 {
Alexander Filippov75626582022-02-09 18:42:37 +03001226 auto it = interfaces.find("xyz.openbmc_project.Object.Enable");
1227 if (it != interfaces.end())
Ratan Guptaaeaf9412019-02-11 04:41:52 -06001228 {
Alexander Filippov75626582022-02-09 18:42:37 +03001229 auto propIt = it->second.find("Enabled");
1230 if (propIt != it->second.end() &&
1231 std::get<bool>(propIt->second))
Ravi Teja5fe724a2019-05-07 05:14:42 -05001232 {
Alexander Filippov75626582022-02-09 18:42:37 +03001233 ldapConfigPath = path.str + '/';
1234 break;
Ravi Teja5fe724a2019-05-07 05:14:42 -05001235 }
Ratan Guptaaeaf9412019-02-11 04:41:52 -06001236 }
Ravi Teja5fe724a2019-05-07 05:14:42 -05001237 }
1238
1239 if (ldapConfigPath.empty())
1240 {
1241 return userInfo;
1242 }
1243
Alexander Filippov75626582022-02-09 18:42:37 +03001244 for (const auto& [path, interfaces] : objects)
Ravi Teja5fe724a2019-05-07 05:14:42 -05001245 {
Alexander Filippov75626582022-02-09 18:42:37 +03001246 if (!path.str.starts_with(ldapConfigPath))
Ravi Teja5fe724a2019-05-07 05:14:42 -05001247 {
Alexander Filippov75626582022-02-09 18:42:37 +03001248 continue;
1249 }
Ravi Teja5fe724a2019-05-07 05:14:42 -05001250
Alexander Filippov75626582022-02-09 18:42:37 +03001251 auto it = interfaces.find(
1252 "xyz.openbmc_project.User.PrivilegeMapperEntry");
1253 if (it != interfaces.end())
1254 {
1255 std::string privilege;
1256 std::string groupName;
1257
1258 for (const auto& [propName, propValue] : it->second)
1259 {
1260 if (propName == "GroupName")
Ravi Teja5fe724a2019-05-07 05:14:42 -05001261 {
Alexander Filippov75626582022-02-09 18:42:37 +03001262 groupName = std::get<std::string>(propValue);
Jiaqing Zhao745ce2e2022-05-31 17:11:31 +08001263 }
Alexander Filippov75626582022-02-09 18:42:37 +03001264 else if (propName == "Privilege")
Jiaqing Zhao745ce2e2022-05-31 17:11:31 +08001265 {
Alexander Filippov75626582022-02-09 18:42:37 +03001266 privilege = std::get<std::string>(propValue);
Ravi Teja5fe724a2019-05-07 05:14:42 -05001267 }
Ratan Guptaaeaf9412019-02-11 04:41:52 -06001268 }
Alexander Filippov75626582022-02-09 18:42:37 +03001269
1270 if (!groupName.empty() && !privilege.empty() &&
1271 isGroupMember(userName, primaryGid, groupName))
1272 {
1273 userPrivilege = privilege;
1274 break;
1275 }
Ratan Guptaaeaf9412019-02-11 04:41:52 -06001276 }
Jiaqing Zhao745ce2e2022-05-31 17:11:31 +08001277 if (!userPrivilege.empty())
1278 {
1279 break;
1280 }
Ratan Guptaaeaf9412019-02-11 04:41:52 -06001281 }
Ravi Teja5fe724a2019-05-07 05:14:42 -05001282
Jiaqing Zhao56862062022-05-31 19:16:09 +08001283 if (!userPrivilege.empty())
Ratan Guptaaeaf9412019-02-11 04:41:52 -06001284 {
Jiaqing Zhao56862062022-05-31 19:16:09 +08001285 userInfo.emplace("UserPrivilege", userPrivilege);
Ratan Guptaaeaf9412019-02-11 04:41:52 -06001286 }
Jiaqing Zhao56862062022-05-31 19:16:09 +08001287 else
1288 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +08001289 lg2::warning("LDAP group privilege mapping does not exist, "
1290 "default \"priv-user\" is used");
Jiaqing Zhao56862062022-05-31 19:16:09 +08001291 userInfo.emplace("UserPrivilege", "priv-user");
1292 }
Ratan Guptaaeaf9412019-02-11 04:41:52 -06001293 }
Patrick Williams9638afb2021-02-22 17:16:24 -06001294 catch (const std::bad_variant_access& e)
Ratan Guptaaeaf9412019-02-11 04:41:52 -06001295 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +08001296 lg2::error("Error while accessing variant: {ERR}", "ERR", e);
Ratan Guptaaeaf9412019-02-11 04:41:52 -06001297 elog<InternalFailure>();
1298 }
1299 userInfo.emplace("RemoteUser", true);
1300 }
1301
1302 return userInfo;
1303}
1304
Nan Zhou4bc69812022-10-25 00:07:13 +00001305void UserMgr::initializeAccountPolicy()
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +05301306{
Richard Marian Thomaiyar9164fd92018-06-13 16:51:00 +05301307 std::string valueStr;
1308 auto value = minPasswdLength;
1309 unsigned long tmp = 0;
Jason M. Bills2d042d12023-03-28 15:32:45 -07001310 if (getPamModuleConfValue(pwQualityConfigFile, minPasswdLenProp,
1311 valueStr) != success)
Richard Marian Thomaiyar9164fd92018-06-13 16:51:00 +05301312 {
1313 AccountPolicyIface::minPasswordLength(minPasswdLength);
1314 }
1315 else
1316 {
1317 try
1318 {
1319 tmp = std::stoul(valueStr, nullptr);
1320 if (tmp > std::numeric_limits<decltype(value)>::max())
1321 {
1322 throw std::out_of_range("Out of range");
1323 }
1324 value = static_cast<decltype(value)>(tmp);
1325 }
Patrick Williams9638afb2021-02-22 17:16:24 -06001326 catch (const std::exception& e)
Richard Marian Thomaiyar9164fd92018-06-13 16:51:00 +05301327 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +08001328 lg2::error("Exception for MinPasswordLength: {ERR}", "ERR", e);
Patrick Venture045b1122018-10-16 15:59:29 -07001329 throw;
Richard Marian Thomaiyar9164fd92018-06-13 16:51:00 +05301330 }
1331 AccountPolicyIface::minPasswordLength(value);
1332 }
1333 valueStr.clear();
Jason M. Bills3b280ec2023-08-15 16:15:48 -07001334 if (getPamModuleConfValue(pwHistoryConfigFile, remOldPasswdCount,
1335 valueStr) != success)
Richard Marian Thomaiyar9164fd92018-06-13 16:51:00 +05301336 {
1337 AccountPolicyIface::rememberOldPasswordTimes(0);
1338 }
1339 else
1340 {
1341 value = 0;
1342 try
1343 {
1344 tmp = std::stoul(valueStr, nullptr);
1345 if (tmp > std::numeric_limits<decltype(value)>::max())
1346 {
1347 throw std::out_of_range("Out of range");
1348 }
1349 value = static_cast<decltype(value)>(tmp);
1350 }
Patrick Williams9638afb2021-02-22 17:16:24 -06001351 catch (const std::exception& e)
Richard Marian Thomaiyar9164fd92018-06-13 16:51:00 +05301352 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +08001353 lg2::error("Exception for RememberOldPasswordTimes: {ERR}", "ERR",
1354 e);
Patrick Venture045b1122018-10-16 15:59:29 -07001355 throw;
Richard Marian Thomaiyar9164fd92018-06-13 16:51:00 +05301356 }
1357 AccountPolicyIface::rememberOldPasswordTimes(value);
1358 }
1359 valueStr.clear();
Jason M. Bills2d042d12023-03-28 15:32:45 -07001360 if (getPamModuleConfValue(faillockConfigFile, maxFailedAttempt, valueStr) !=
1361 success)
Richard Marian Thomaiyar9164fd92018-06-13 16:51:00 +05301362 {
1363 AccountPolicyIface::maxLoginAttemptBeforeLockout(0);
1364 }
1365 else
1366 {
1367 uint16_t value16 = 0;
1368 try
1369 {
1370 tmp = std::stoul(valueStr, nullptr);
1371 if (tmp > std::numeric_limits<decltype(value16)>::max())
1372 {
1373 throw std::out_of_range("Out of range");
1374 }
1375 value16 = static_cast<decltype(value16)>(tmp);
1376 }
Patrick Williams9638afb2021-02-22 17:16:24 -06001377 catch (const std::exception& e)
Richard Marian Thomaiyar9164fd92018-06-13 16:51:00 +05301378 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +08001379 lg2::error("Exception for MaxLoginAttemptBeforLockout: {ERR}",
1380 "ERR", e);
Patrick Venture045b1122018-10-16 15:59:29 -07001381 throw;
Richard Marian Thomaiyar9164fd92018-06-13 16:51:00 +05301382 }
1383 AccountPolicyIface::maxLoginAttemptBeforeLockout(value16);
1384 }
1385 valueStr.clear();
Jason M. Bills2d042d12023-03-28 15:32:45 -07001386 if (getPamModuleConfValue(faillockConfigFile, unlockTimeout, valueStr) !=
1387 success)
Richard Marian Thomaiyar9164fd92018-06-13 16:51:00 +05301388 {
1389 AccountPolicyIface::accountUnlockTimeout(0);
1390 }
1391 else
1392 {
1393 uint32_t value32 = 0;
1394 try
1395 {
1396 tmp = std::stoul(valueStr, nullptr);
1397 if (tmp > std::numeric_limits<decltype(value32)>::max())
1398 {
1399 throw std::out_of_range("Out of range");
1400 }
1401 value32 = static_cast<decltype(value32)>(tmp);
1402 }
Patrick Williams9638afb2021-02-22 17:16:24 -06001403 catch (const std::exception& e)
Richard Marian Thomaiyar9164fd92018-06-13 16:51:00 +05301404 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +08001405 lg2::error("Exception for AccountUnlockTimeout: {ERR}", "ERR", e);
Patrick Venture045b1122018-10-16 15:59:29 -07001406 throw;
Richard Marian Thomaiyar9164fd92018-06-13 16:51:00 +05301407 }
1408 AccountPolicyIface::accountUnlockTimeout(value32);
1409 }
Nan Zhou4bc69812022-10-25 00:07:13 +00001410}
1411
1412void UserMgr::initUserObjects(void)
1413{
1414 // All user management lock has to be based on /etc/shadow
1415 // TODO phosphor-user-manager#10 phosphor::user::shadow::Lock lock{};
1416 std::vector<std::string> userNameList;
1417 std::vector<std::string> sshGrpUsersList;
1418 UserSSHLists userSSHLists = getUserAndSshGrpList();
1419 userNameList = std::move(userSSHLists.first);
1420 sshGrpUsersList = std::move(userSSHLists.second);
1421
1422 if (!userNameList.empty())
1423 {
1424 std::map<std::string, std::vector<std::string>> groupLists;
Nan Zhouda401fe2022-10-25 00:07:18 +00001425 // We only track users that are in the |predefinedGroups|
1426 // The other groups don't contain real BMC users.
1427 for (const char* grp : predefinedGroups)
Nan Zhou4bc69812022-10-25 00:07:13 +00001428 {
1429 if (grp == grpSsh)
1430 {
1431 groupLists.emplace(grp, sshGrpUsersList);
1432 }
1433 else
1434 {
1435 std::vector<std::string> grpUsersList = getUsersInGroup(grp);
1436 groupLists.emplace(grp, grpUsersList);
1437 }
1438 }
1439 for (auto& grp : privMgr)
1440 {
1441 std::vector<std::string> grpUsersList = getUsersInGroup(grp);
1442 groupLists.emplace(grp, grpUsersList);
1443 }
1444
1445 for (auto& user : userNameList)
1446 {
1447 std::vector<std::string> userGroups;
1448 std::string userPriv;
1449 for (const auto& grp : groupLists)
1450 {
1451 std::vector<std::string> tempGrp = grp.second;
1452 if (std::find(tempGrp.begin(), tempGrp.end(), user) !=
1453 tempGrp.end())
1454 {
1455 if (std::find(privMgr.begin(), privMgr.end(), grp.first) !=
1456 privMgr.end())
1457 {
1458 userPriv = grp.first;
1459 }
1460 else
1461 {
1462 userGroups.emplace_back(grp.first);
1463 }
1464 }
1465 }
1466 // Add user objects to the Users path.
1467 sdbusplus::message::object_path tempObjPath(usersObjPath);
1468 tempObjPath /= user;
1469 std::string objPath(tempObjPath);
1470 std::sort(userGroups.begin(), userGroups.end());
1471 usersList.emplace(user, std::make_unique<phosphor::user::Users>(
1472 bus, objPath.c_str(), userGroups,
1473 userPriv, isUserEnabled(user), *this));
1474 }
1475 }
1476}
1477
1478UserMgr::UserMgr(sdbusplus::bus_t& bus, const char* path) :
1479 Ifaces(bus, path, Ifaces::action::defer_emit), bus(bus), path(path),
Jason M. Bills2d042d12023-03-28 15:32:45 -07001480 faillockConfigFile(defaultFaillockConfigFile),
Jason M. Bills3b280ec2023-08-15 16:15:48 -07001481 pwHistoryConfigFile(defaultPWHistoryConfigFile),
Jason M. Bills2d042d12023-03-28 15:32:45 -07001482 pwQualityConfigFile(defaultPWQualityConfigFile)
Nan Zhou4bc69812022-10-25 00:07:13 +00001483{
1484 UserMgrIface::allPrivileges(privMgr);
Nan Zhouda401fe2022-10-25 00:07:18 +00001485 groupsMgr = readAllGroupsOnSystem();
Nan Zhou4bc69812022-10-25 00:07:13 +00001486 std::sort(groupsMgr.begin(), groupsMgr.end());
1487 UserMgrIface::allGroups(groupsMgr);
1488 initializeAccountPolicy();
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +05301489 initUserObjects();
Ratan Gupta1af12232018-11-03 00:35:38 +05301490
1491 // emit the signal
1492 this->emit_object_added();
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +05301493}
1494
Nan Zhou49c81362022-10-25 00:07:08 +00001495void UserMgr::executeUserAdd(const char* userName, const char* groups,
1496 bool sshRequested, bool enabled)
1497{
1498 // set EXPIRE_DATE to 0 to disable user, PAM takes 0 as expire on
1499 // 1970-01-01, that's an implementation-defined behavior
1500 executeCmd("/usr/sbin/useradd", userName, "-G", groups, "-m", "-N", "-s",
Tang Yiwei75ea3e42022-04-25 10:48:15 +08001501 (sshRequested ? "/bin/sh" : "/sbin/nologin"), "-e",
Nan Zhou49c81362022-10-25 00:07:08 +00001502 (enabled ? "" : "1970-01-01"));
1503}
1504
1505void UserMgr::executeUserDelete(const char* userName)
1506{
1507 executeCmd("/usr/sbin/userdel", userName, "-r");
1508}
1509
Jayanth Othayothac921a52023-07-21 03:48:55 -05001510void UserMgr::executeUserClearFailRecords(const char* userName)
1511{
1512 executeCmd("/usr/sbin/faillock", "--user", userName, "--reset");
1513}
1514
Nan Zhouf25443e2022-10-25 00:07:11 +00001515void UserMgr::executeUserRename(const char* userName, const char* newUserName)
1516{
1517 std::string newHomeDir = "/home/";
1518 newHomeDir += newUserName;
1519 executeCmd("/usr/sbin/usermod", "-l", newUserName, userName, "-d",
1520 newHomeDir.c_str(), "-m");
1521}
1522
Nan Zhoufef63032022-10-25 00:07:12 +00001523void UserMgr::executeUserModify(const char* userName, const char* newGroups,
1524 bool sshRequested)
1525{
1526 executeCmd("/usr/sbin/usermod", userName, "-G", newGroups, "-s",
Tang Yiwei75ea3e42022-04-25 10:48:15 +08001527 (sshRequested ? "/bin/sh" : "/sbin/nologin"));
Nan Zhoufef63032022-10-25 00:07:12 +00001528}
1529
Nan Zhou6b6f2d82022-10-25 00:07:17 +00001530void UserMgr::executeUserModifyUserEnable(const char* userName, bool enabled)
1531{
1532 // set EXPIRE_DATE to 0 to disable user, PAM takes 0 as expire on
1533 // 1970-01-01, that's an implementation-defined behavior
1534 executeCmd("/usr/sbin/usermod", userName, "-e",
1535 (enabled ? "" : "1970-01-01"));
1536}
1537
Nan Zhoua2953032022-11-11 21:50:32 +00001538std::vector<std::string> UserMgr::getFailedAttempt(const char* userName)
1539{
Jason M. Bills2d042d12023-03-28 15:32:45 -07001540 return executeCmd("/usr/sbin/faillock", "--user", userName);
Nan Zhoua2953032022-11-11 21:50:32 +00001541}
1542
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +05301543} // namespace user
1544} // namespace phosphor