blob: 6addc49d2942db02f3d218196f622bd5b8b6e0d7 [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#pragma once
Patrick Williams9638afb2021-02-22 17:16:24 -060017#include "users.hpp"
18
Nan Zhou49c81362022-10-25 00:07:08 +000019#include <boost/process/child.hpp>
20#include <boost/process/io.hpp>
21#include <phosphor-logging/elog-errors.hpp>
22#include <phosphor-logging/elog.hpp>
Jiaqing Zhao11ec6662022-07-05 20:55:34 +080023#include <phosphor-logging/lg2.hpp>
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053024#include <sdbusplus/bus.hpp>
25#include <sdbusplus/server/object.hpp>
Nan Zhou49c81362022-10-25 00:07:08 +000026#include <xyz/openbmc_project/Common/error.hpp>
Richard Marian Thomaiyar9164fd92018-06-13 16:51:00 +053027#include <xyz/openbmc_project/User/AccountPolicy/server.hpp>
Patrick Williams9638afb2021-02-22 17:16:24 -060028#include <xyz/openbmc_project/User/Manager/server.hpp>
29
Nan Zhoue47c09d2022-10-25 00:06:41 +000030#include <span>
31#include <string>
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053032#include <unordered_map>
Ratan Guptaaeaf9412019-02-11 04:41:52 -060033#include <variant>
Nan Zhoue47c09d2022-10-25 00:06:41 +000034#include <vector>
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053035
36namespace phosphor
37{
38namespace user
39{
40
Nan Zhou49c81362022-10-25 00:07:08 +000041inline constexpr size_t ipmiMaxUsers = 15;
42inline constexpr size_t maxSystemUsers = 30;
Nan Zhou4bc69812022-10-25 00:07:13 +000043inline constexpr uint8_t minPasswdLength = 8;
Nan Zhouda401fe2022-10-25 00:07:18 +000044inline constexpr size_t maxSystemGroupNameLength = 32;
45inline constexpr size_t maxSystemGroupCount = 64;
Nan Zhou49c81362022-10-25 00:07:08 +000046
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053047using UserMgrIface = sdbusplus::xyz::openbmc_project::User::server::Manager;
48using UserSSHLists =
49 std::pair<std::vector<std::string>, std::vector<std::string>>;
Richard Marian Thomaiyar9164fd92018-06-13 16:51:00 +053050using AccountPolicyIface =
51 sdbusplus::xyz::openbmc_project::User::server::AccountPolicy;
52
Patrick Williamsb3ef4e12022-07-22 19:26:55 -050053using Ifaces = sdbusplus::server::object_t<UserMgrIface, AccountPolicyIface>;
Ratan Gupta1af12232018-11-03 00:35:38 +053054
Ratan Guptaaeaf9412019-02-11 04:41:52 -060055using Privilege = std::string;
56using GroupList = std::vector<std::string>;
57using UserEnabled = bool;
58using PropertyName = std::string;
Ravi Teja5fe724a2019-05-07 05:14:42 -050059using ServiceEnabled = bool;
Ratan Guptaaeaf9412019-02-11 04:41:52 -060060
61using UserInfo = std::variant<Privilege, GroupList, UserEnabled>;
62using UserInfoMap = std::map<PropertyName, UserInfo>;
63
64using DbusUserObjPath = sdbusplus::message::object_path;
65
Patrick Williamsfdf09372020-05-13 18:01:45 -050066using DbusUserPropVariant = std::variant<Privilege, ServiceEnabled>;
Ratan Guptaaeaf9412019-02-11 04:41:52 -060067
Alexander Filippov75626582022-02-09 18:42:37 +030068using DbusUserObjProperties = std::map<PropertyName, DbusUserPropVariant>;
Ratan Guptaaeaf9412019-02-11 04:41:52 -060069
70using Interface = std::string;
71
72using DbusUserObjValue = std::map<Interface, DbusUserObjProperties>;
73
74using DbusUserObj = std::map<DbusUserObjPath, DbusUserObjValue>;
75
Nan Zhoue47c09d2022-10-25 00:06:41 +000076std::string getCSVFromVector(std::span<const std::string> vec);
77
Nan Zhou332fb9d2022-10-25 00:07:03 +000078bool removeStringFromCSV(std::string& csvStr, const std::string& delStr);
79
Nan Zhou8a11d992022-10-25 00:07:06 +000080template <typename... ArgTypes>
Nan Zhou49c81362022-10-25 00:07:08 +000081std::vector<std::string> executeCmd(const char* path, ArgTypes&&... tArgs)
82{
83 std::vector<std::string> stdOutput;
84 boost::process::ipstream stdOutStream;
85 boost::process::child execProg(path, const_cast<char*>(tArgs)...,
86 boost::process::std_out > stdOutStream);
87 std::string stdOutLine;
88
89 while (stdOutStream && std::getline(stdOutStream, stdOutLine) &&
90 !stdOutLine.empty())
91 {
92 stdOutput.emplace_back(stdOutLine);
93 }
94
95 execProg.wait();
96
97 int retCode = execProg.exit_code();
98 if (retCode)
99 {
Jiaqing Zhao11ec6662022-07-05 20:55:34 +0800100 lg2::error("Command {PATH} execution failed, return code {RETCODE}",
101 "PATH", path, "RETCODE", retCode);
Nan Zhou49c81362022-10-25 00:07:08 +0000102 phosphor::logging::elog<
103 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure>();
104 }
105
106 return stdOutput;
107}
Nan Zhou8a11d992022-10-25 00:07:06 +0000108
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530109/** @class UserMgr
110 * @brief Responsible for managing user accounts over the D-Bus interface.
111 */
Ratan Gupta1af12232018-11-03 00:35:38 +0530112class UserMgr : public Ifaces
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530113{
114 public:
115 UserMgr() = delete;
116 ~UserMgr() = default;
Patrick Williams9638afb2021-02-22 17:16:24 -0600117 UserMgr(const UserMgr&) = delete;
118 UserMgr& operator=(const UserMgr&) = delete;
119 UserMgr(UserMgr&&) = delete;
120 UserMgr& operator=(UserMgr&&) = delete;
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530121
122 /** @brief Constructs UserMgr object.
123 *
124 * @param[in] bus - sdbusplus handler
125 * @param[in] path - D-Bus path
126 */
Patrick Williamsb3ef4e12022-07-22 19:26:55 -0500127 UserMgr(sdbusplus::bus_t& bus, const char* path);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530128
129 /** @brief create user method.
130 * This method creates a new user as requested
131 *
132 * @param[in] userName - Name of the user which has to be created
133 * @param[in] groupNames - Group names list, to which user has to be added.
134 * @param[in] priv - Privilege of the user.
135 * @param[in] enabled - State of the user enabled / disabled.
136 */
137 void createUser(std::string userName, std::vector<std::string> groupNames,
138 std::string priv, bool enabled) override;
139
140 /** @brief rename user method.
141 * This method renames the user as requested
142 *
143 * @param[in] userName - current name of the user
144 * @param[in] newUserName - new user name to which it has to be renamed.
145 */
146 void renameUser(std::string userName, std::string newUserName) override;
147
148 /** @brief delete user method.
149 * This method deletes the user as requested
150 *
151 * @param[in] userName - Name of the user which has to be deleted
152 */
153 void deleteUser(std::string userName);
154
155 /** @brief Update user groups & privilege.
156 * This method updates user groups & privilege
157 *
158 * @param[in] userName - user name, for which update is requested
159 * @param[in] groupName - Group to be updated..
160 * @param[in] priv - Privilege to be updated.
161 */
Patrick Williams9638afb2021-02-22 17:16:24 -0600162 void updateGroupsAndPriv(const std::string& userName,
Nan Zhoufef63032022-10-25 00:07:12 +0000163 std::vector<std::string> groups,
Patrick Williams9638afb2021-02-22 17:16:24 -0600164 const std::string& priv);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530165
166 /** @brief Update user enabled state.
167 * This method enables / disables user
168 *
169 * @param[in] userName - user name, for which update is requested
170 * @param[in] enabled - enable / disable the user
171 */
Patrick Williams9638afb2021-02-22 17:16:24 -0600172 void userEnable(const std::string& userName, bool enabled);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530173
Denis Zlobine8edab52023-09-06 12:26:45 +0000174 /** @brief get user enabled state
175 * method to get user enabled state.
176 *
177 * @param[in] userName - name of the user
178 * @return - user enabled status (true/false)
179 */
180 virtual bool isUserEnabled(const std::string& userName);
181
Richard Marian Thomaiyar9164fd92018-06-13 16:51:00 +0530182 /** @brief update minimum password length requirement
183 *
184 * @param[in] val - minimum password length
185 * @return - minimum password length
186 */
187 uint8_t minPasswordLength(uint8_t val) override;
188
189 /** @brief update old password history count
190 *
191 * @param[in] val - number of times old passwords has to be avoided
192 * @return - number of times old password has to be avoided
193 */
194 uint8_t rememberOldPasswordTimes(uint8_t val) override;
195
196 /** @brief update maximum number of failed login attempt before locked
197 * out.
198 *
199 * @param[in] val - number of allowed attempt
200 * @return - number of allowed attempt
201 */
202 uint16_t maxLoginAttemptBeforeLockout(uint16_t val) override;
203
204 /** @brief update timeout to unlock the account
205 *
206 * @param[in] val - value in seconds
207 * @return - value in seconds
208 */
209 uint32_t accountUnlockTimeout(uint32_t val) override;
210
Jason M. Bills2d042d12023-03-28 15:32:45 -0700211 /** @brief parses the faillock output for locked user status
212 *
213 * @param[in] - output from faillock for the user
214 * @return - true / false indicating user locked / un-locked
215 **/
216 bool
217 parseFaillockForLockout(const std::vector<std::string>& faillockOutput);
218
Richard Marian Thomaiyarc7045192018-06-13 16:51:00 +0530219 /** @brief lists user locked state for failed attempt
220 *
221 * @param[in] - user name
222 * @return - true / false indicating user locked / un-locked
223 **/
Patrick Williams9638afb2021-02-22 17:16:24 -0600224 virtual bool userLockedForFailedAttempt(const std::string& userName);
Richard Marian Thomaiyarc7045192018-06-13 16:51:00 +0530225
226 /** @brief lists user locked state for failed attempt
227 *
228 * @param[in]: user name
229 * @param[in]: value - false -unlock user account, true - no action taken
230 **/
Patrick Williams9638afb2021-02-22 17:16:24 -0600231 bool userLockedForFailedAttempt(const std::string& userName,
232 const bool& value);
Richard Marian Thomaiyarc7045192018-06-13 16:51:00 +0530233
Joseph Reynolds3ab6cc22020-03-03 14:09:03 -0600234 /** @brief shows if the user's password is expired
235 *
236 * @param[in]: user name
237 * @return - true / false indicating user password expired
238 **/
Patrick Williams9638afb2021-02-22 17:16:24 -0600239 virtual bool userPasswordExpired(const std::string& userName);
Joseph Reynolds3ab6cc22020-03-03 14:09:03 -0600240
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600241 /** @brief returns user info
242 * Checks if user is local user, then returns map of properties of user.
243 * like user privilege, list of user groups, user enabled state and user
244 * locked state. If its not local user, then it checks if its a ldap user,
245 * then it gets the privilege mapping of the LDAP group.
246 *
247 * @param[in] - user name
248 * @return - map of user properties
249 **/
250 UserInfoMap getUserInfo(std::string userName) override;
251
Nan Zhou49c81362022-10-25 00:07:08 +0000252 /** @brief get IPMI user count
253 * method to get IPMI user count
254 *
255 * @return - returns user count
256 */
257 virtual size_t getIpmiUsersCount(void);
258
Nan Zhouda401fe2022-10-25 00:07:18 +0000259 void createGroup(std::string groupName) override;
260
261 void deleteGroup(std::string groupName) override;
262
263 static std::vector<std::string> readAllGroupsOnSystem();
264
Nan Zhoue48085d2022-10-25 00:07:04 +0000265 protected:
266 /** @brief get pam argument value
267 * method to get argument value from pam configuration
268 *
269 * @param[in] moduleName - name of the module from where arg has to be read
270 * @param[in] argName - argument name
271 * @param[out] argValue - argument value
272 *
273 * @return 0 - success state of the function
274 */
275 int getPamModuleArgValue(const std::string& moduleName,
276 const std::string& argName, std::string& argValue);
277
Jason M. Bills2d042d12023-03-28 15:32:45 -0700278 /** @brief get pam argument value
279 * method to get argument value from pam configuration
280 *
281 * @param[in] confFile - path of the module config file from where arg has
282 * to be read
283 * @param[in] argName - argument name
284 * @param[out] argValue - argument value
285 *
286 * @return 0 - success state of the function
287 */
288 int getPamModuleConfValue(const std::string& confFile,
289 const std::string& argName,
290 std::string& argValue);
291
Nan Zhoue48085d2022-10-25 00:07:04 +0000292 /** @brief set pam argument value
293 * method to set argument value in pam configuration
294 *
295 * @param[in] moduleName - name of the module in which argument value has
296 * to be set
297 * @param[in] argName - argument name
298 * @param[out] argValue - argument value
299 *
300 * @return 0 - success state of the function
301 */
302 int setPamModuleArgValue(const std::string& moduleName,
303 const std::string& argName,
304 const std::string& argValue);
305
Jason M. Bills2d042d12023-03-28 15:32:45 -0700306 /** @brief set pam argument value
307 * method to set argument value in pam configuration
308 *
309 * @param[in] confFile - path of the module config file in which argument
310 * value has to be set
311 * @param[in] argName - argument name
312 * @param[out] argValue - argument value
313 *
314 * @return 0 - success state of the function
315 */
316 int setPamModuleConfValue(const std::string& confFile,
317 const std::string& argName,
318 const std::string& argValue);
319
Nan Zhou8a11d992022-10-25 00:07:06 +0000320 /** @brief check for user presence
321 * method to check for user existence
322 *
323 * @param[in] userName - name of the user
324 * @return -true if user exists and false if not.
325 */
326 bool isUserExist(const std::string& userName);
327
Nan Zhou49c81362022-10-25 00:07:08 +0000328 size_t getNonIpmiUsersCount();
329
Nan Zhou8a11d992022-10-25 00:07:06 +0000330 /** @brief check user exists
331 * method to check whether user exist, and throw if not.
332 *
333 * @param[in] userName - name of the user
334 */
335 void throwForUserDoesNotExist(const std::string& userName);
336
337 /** @brief check user does not exist
338 * method to check whether does not exist, and throw if exists.
339 *
340 * @param[in] userName - name of the user
341 */
342 void throwForUserExists(const std::string& userName);
343
Nan Zhou40e44972022-10-25 00:07:07 +0000344 /** @brief check user name constraints
345 * method to check user name constraints and throw if failed.
346 *
347 * @param[in] userName - name of the user
348 * @param[in] groupNames - user groups
349 */
350 void
351 throwForUserNameConstraints(const std::string& userName,
352 const std::vector<std::string>& groupNames);
353
Nan Zhou49c81362022-10-25 00:07:08 +0000354 /** @brief check group user count
355 * method to check max group user count, and throw if limit reached
356 *
357 * @param[in] groupNames - group name
358 */
359 void throwForMaxGrpUserCount(const std::vector<std::string>& groupNames);
360
361 virtual void executeUserAdd(const char* userName, const char* groups,
362 bool sshRequested, bool enabled);
363
364 virtual void executeUserDelete(const char* userName);
365
Jayanth Othayothac921a52023-07-21 03:48:55 -0500366 /** @brief clear user's failure records
367 * method to clear user fail records and throw if failed.
368 *
369 * @param[in] userName - name of the user
370 */
371 virtual void executeUserClearFailRecords(const char* userName);
372
Nan Zhouf25443e2022-10-25 00:07:11 +0000373 virtual void executeUserRename(const char* userName,
374 const char* newUserName);
375
Nan Zhoufef63032022-10-25 00:07:12 +0000376 virtual void executeUserModify(const char* userName, const char* newGroups,
377 bool sshRequested);
378
Nan Zhou6b6f2d82022-10-25 00:07:17 +0000379 virtual void executeUserModifyUserEnable(const char* userName,
380 bool enabled);
381
Nan Zhouda401fe2022-10-25 00:07:18 +0000382 virtual void executeGroupCreation(const char* groupName);
Nan Zhou86040c22022-11-17 02:08:24 +0000383
Nan Zhouda401fe2022-10-25 00:07:18 +0000384 virtual void executeGroupDeletion(const char* groupName);
Nan Zhou86040c22022-11-17 02:08:24 +0000385
Nan Zhoua2953032022-11-11 21:50:32 +0000386 virtual std::vector<std::string> getFailedAttempt(const char* userName);
387
Nan Zhou589aeb42022-10-25 00:07:09 +0000388 /** @brief check for valid privielge
389 * method to check valid privilege, and throw if invalid
390 *
391 * @param[in] priv - privilege of the user
392 */
393 void throwForInvalidPrivilege(const std::string& priv);
394
Nan Zhouecf88762022-10-25 00:07:10 +0000395 /** @brief check for valid groups
396 * method to check valid groups, and throw if invalid
397 *
398 * @param[in] groupNames - user groups
399 */
400 void throwForInvalidGroups(const std::vector<std::string>& groupName);
401
Nan Zhou4bc69812022-10-25 00:07:13 +0000402 void initializeAccountPolicy();
403
Nan Zhouda401fe2022-10-25 00:07:18 +0000404 /** @brief checks if the group creation meets all constraints
405 * @param groupName - group to check
406 */
407 void checkCreateGroupConstraints(const std::string& groupName);
408
409 /** @brief checks if the group deletion meets all constraints
410 * @param groupName - group to check
411 */
412 void checkDeleteGroupConstraints(const std::string& groupName);
413
414 /** @brief checks if the group name is legal and whether it's allowed to
415 * change. The daemon doesn't allow arbitrary group to be created
416 * @param groupName - group to check
417 */
418 void checkAndThrowForDisallowedGroupCreation(const std::string& groupName);
419
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530420 private:
421 /** @brief sdbusplus handler */
Patrick Williamsb3ef4e12022-07-22 19:26:55 -0500422 sdbusplus::bus_t& bus;
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530423
424 /** @brief object path */
425 const std::string path;
426
427 /** @brief privilege manager container */
Nan Zhouda401fe2022-10-25 00:07:18 +0000428 const std::vector<std::string> privMgr = {"priv-admin", "priv-operator",
429 "priv-user"};
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530430
431 /** @brief groups manager container */
Nan Zhouda401fe2022-10-25 00:07:18 +0000432 std::vector<std::string> groupsMgr;
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530433
434 /** @brief map container to hold users object */
435 using UserName = std::string;
436 std::unordered_map<UserName, std::unique_ptr<phosphor::user::Users>>
437 usersList;
438
439 /** @brief get users in group
440 * method to get group user list
441 *
442 * @param[in] groupName - group name
443 *
444 * @return userList - list of users in the group.
445 */
Patrick Williams9638afb2021-02-22 17:16:24 -0600446 std::vector<std::string> getUsersInGroup(const std::string& groupName);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530447
448 /** @brief get user & SSH users list
449 * method to get the users and ssh users list.
450 *
451 *@return - vector of User & SSH user lists
452 */
453 UserSSHLists getUserAndSshGrpList(void);
454
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530455 /** @brief initialize the user manager objects
456 * method to initialize the user manager objects accordingly
457 *
458 */
459 void initUserObjects(void);
460
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600461 /** @brief get service name
462 * method to get dbus service name
463 *
464 * @param[in] path - object path
465 * @param[in] intf - interface
466 * @return - service name
467 */
Patrick Williams9638afb2021-02-22 17:16:24 -0600468 std::string getServiceName(std::string&& path, std::string&& intf);
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600469
Alexander Filippov75626582022-02-09 18:42:37 +0300470 /** @brief get primary group ID of specified user
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600471 *
Alexander Filippov75626582022-02-09 18:42:37 +0300472 * @param[in] - userName
473 * @return - primary group ID
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600474 */
Alexander Filippov75626582022-02-09 18:42:37 +0300475 virtual gid_t getPrimaryGroup(const std::string& userName) const;
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600476
Alexander Filippov75626582022-02-09 18:42:37 +0300477 /** @brief check whether if the user is a member of the group
478 *
479 * @param[in] - userName
480 * @param[in] - ID of the user's primary group
481 * @param[in] - groupName
482 * @return - true if the user is a member of the group
483 */
484 virtual bool isGroupMember(const std::string& userName, gid_t primaryGid,
485 const std::string& groupName) const;
486
487 protected:
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600488 /** @brief get privilege mapper object
489 * method to get dbus privilege mapper object
490 *
491 * @return - map of user object
492 */
raviteja-b8cc44052019-02-27 23:29:36 -0600493 virtual DbusUserObj getPrivilegeMapperObject(void);
494
495 friend class TestUserMgr;
Nan Zhoue48085d2022-10-25 00:07:04 +0000496
Jason M. Bills2d042d12023-03-28 15:32:45 -0700497 std::string faillockConfigFile;
Jason M. Bills3b280ec2023-08-15 16:15:48 -0700498 std::string pwHistoryConfigFile;
Jason M. Bills2d042d12023-03-28 15:32:45 -0700499 std::string pwQualityConfigFile;
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530500};
501
502} // namespace user
503} // namespace phosphor