blob: d298bf05e8bedc982e47f163b71d0e4dcea2acfe [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
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053019#include <sdbusplus/bus.hpp>
20#include <sdbusplus/server/object.hpp>
Richard Marian Thomaiyar9164fd92018-06-13 16:51:00 +053021#include <xyz/openbmc_project/User/AccountPolicy/server.hpp>
Patrick Williams9638afb2021-02-22 17:16:24 -060022#include <xyz/openbmc_project/User/Manager/server.hpp>
23
Nan Zhoue47c09d2022-10-25 00:06:41 +000024#include <span>
25#include <string>
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053026#include <unordered_map>
Ratan Guptaaeaf9412019-02-11 04:41:52 -060027#include <variant>
Nan Zhoue47c09d2022-10-25 00:06:41 +000028#include <vector>
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053029
30namespace phosphor
31{
32namespace user
33{
34
35using UserMgrIface = sdbusplus::xyz::openbmc_project::User::server::Manager;
36using UserSSHLists =
37 std::pair<std::vector<std::string>, std::vector<std::string>>;
Richard Marian Thomaiyar9164fd92018-06-13 16:51:00 +053038using AccountPolicyIface =
39 sdbusplus::xyz::openbmc_project::User::server::AccountPolicy;
40
Patrick Williamsb3ef4e12022-07-22 19:26:55 -050041using Ifaces = sdbusplus::server::object_t<UserMgrIface, AccountPolicyIface>;
Ratan Gupta1af12232018-11-03 00:35:38 +053042
Ratan Guptaaeaf9412019-02-11 04:41:52 -060043using Privilege = std::string;
44using GroupList = std::vector<std::string>;
45using UserEnabled = bool;
46using PropertyName = std::string;
Ravi Teja5fe724a2019-05-07 05:14:42 -050047using ServiceEnabled = bool;
Ratan Guptaaeaf9412019-02-11 04:41:52 -060048
49using UserInfo = std::variant<Privilege, GroupList, UserEnabled>;
50using UserInfoMap = std::map<PropertyName, UserInfo>;
51
52using DbusUserObjPath = sdbusplus::message::object_path;
53
Patrick Williamsfdf09372020-05-13 18:01:45 -050054using DbusUserPropVariant = std::variant<Privilege, ServiceEnabled>;
Ratan Guptaaeaf9412019-02-11 04:41:52 -060055
56using DbusUserObjProperties =
57 std::vector<std::pair<PropertyName, DbusUserPropVariant>>;
58
59using Interface = std::string;
60
61using DbusUserObjValue = std::map<Interface, DbusUserObjProperties>;
62
63using DbusUserObj = std::map<DbusUserObjPath, DbusUserObjValue>;
64
Nan Zhoue47c09d2022-10-25 00:06:41 +000065std::string getCSVFromVector(std::span<const std::string> vec);
66
Nan Zhou332fb9d2022-10-25 00:07:03 +000067bool removeStringFromCSV(std::string& csvStr, const std::string& delStr);
68
Nan Zhou8a11d992022-10-25 00:07:06 +000069template <typename... ArgTypes>
70static std::vector<std::string> executeCmd(const char* path,
71 ArgTypes&&... tArgs);
72
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053073/** @class UserMgr
74 * @brief Responsible for managing user accounts over the D-Bus interface.
75 */
Ratan Gupta1af12232018-11-03 00:35:38 +053076class UserMgr : public Ifaces
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053077{
78 public:
79 UserMgr() = delete;
80 ~UserMgr() = default;
Patrick Williams9638afb2021-02-22 17:16:24 -060081 UserMgr(const UserMgr&) = delete;
82 UserMgr& operator=(const UserMgr&) = delete;
83 UserMgr(UserMgr&&) = delete;
84 UserMgr& operator=(UserMgr&&) = delete;
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053085
86 /** @brief Constructs UserMgr object.
87 *
88 * @param[in] bus - sdbusplus handler
89 * @param[in] path - D-Bus path
90 */
Patrick Williamsb3ef4e12022-07-22 19:26:55 -050091 UserMgr(sdbusplus::bus_t& bus, const char* path);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053092
93 /** @brief create user method.
94 * This method creates a new user as requested
95 *
96 * @param[in] userName - Name of the user which has to be created
97 * @param[in] groupNames - Group names list, to which user has to be added.
98 * @param[in] priv - Privilege of the user.
99 * @param[in] enabled - State of the user enabled / disabled.
100 */
101 void createUser(std::string userName, std::vector<std::string> groupNames,
102 std::string priv, bool enabled) override;
103
104 /** @brief rename user method.
105 * This method renames the user as requested
106 *
107 * @param[in] userName - current name of the user
108 * @param[in] newUserName - new user name to which it has to be renamed.
109 */
110 void renameUser(std::string userName, std::string newUserName) override;
111
112 /** @brief delete user method.
113 * This method deletes the user as requested
114 *
115 * @param[in] userName - Name of the user which has to be deleted
116 */
117 void deleteUser(std::string userName);
118
119 /** @brief Update user groups & privilege.
120 * This method updates user groups & privilege
121 *
122 * @param[in] userName - user name, for which update is requested
123 * @param[in] groupName - Group to be updated..
124 * @param[in] priv - Privilege to be updated.
125 */
Patrick Williams9638afb2021-02-22 17:16:24 -0600126 void updateGroupsAndPriv(const std::string& userName,
127 const std::vector<std::string>& groups,
128 const std::string& priv);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530129
130 /** @brief Update user enabled state.
131 * This method enables / disables user
132 *
133 * @param[in] userName - user name, for which update is requested
134 * @param[in] enabled - enable / disable the user
135 */
Patrick Williams9638afb2021-02-22 17:16:24 -0600136 void userEnable(const std::string& userName, bool enabled);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530137
Richard Marian Thomaiyar9164fd92018-06-13 16:51:00 +0530138 /** @brief update minimum password length requirement
139 *
140 * @param[in] val - minimum password length
141 * @return - minimum password length
142 */
143 uint8_t minPasswordLength(uint8_t val) override;
144
145 /** @brief update old password history count
146 *
147 * @param[in] val - number of times old passwords has to be avoided
148 * @return - number of times old password has to be avoided
149 */
150 uint8_t rememberOldPasswordTimes(uint8_t val) override;
151
152 /** @brief update maximum number of failed login attempt before locked
153 * out.
154 *
155 * @param[in] val - number of allowed attempt
156 * @return - number of allowed attempt
157 */
158 uint16_t maxLoginAttemptBeforeLockout(uint16_t val) override;
159
160 /** @brief update timeout to unlock the account
161 *
162 * @param[in] val - value in seconds
163 * @return - value in seconds
164 */
165 uint32_t accountUnlockTimeout(uint32_t val) override;
166
Richard Marian Thomaiyarc7045192018-06-13 16:51:00 +0530167 /** @brief lists user locked state for failed attempt
168 *
169 * @param[in] - user name
170 * @return - true / false indicating user locked / un-locked
171 **/
Patrick Williams9638afb2021-02-22 17:16:24 -0600172 virtual bool userLockedForFailedAttempt(const std::string& userName);
Richard Marian Thomaiyarc7045192018-06-13 16:51:00 +0530173
174 /** @brief lists user locked state for failed attempt
175 *
176 * @param[in]: user name
177 * @param[in]: value - false -unlock user account, true - no action taken
178 **/
Patrick Williams9638afb2021-02-22 17:16:24 -0600179 bool userLockedForFailedAttempt(const std::string& userName,
180 const bool& value);
Richard Marian Thomaiyarc7045192018-06-13 16:51:00 +0530181
Joseph Reynolds3ab6cc22020-03-03 14:09:03 -0600182 /** @brief shows if the user's password is expired
183 *
184 * @param[in]: user name
185 * @return - true / false indicating user password expired
186 **/
Patrick Williams9638afb2021-02-22 17:16:24 -0600187 virtual bool userPasswordExpired(const std::string& userName);
Joseph Reynolds3ab6cc22020-03-03 14:09:03 -0600188
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600189 /** @brief returns user info
190 * Checks if user is local user, then returns map of properties of user.
191 * like user privilege, list of user groups, user enabled state and user
192 * locked state. If its not local user, then it checks if its a ldap user,
193 * then it gets the privilege mapping of the LDAP group.
194 *
195 * @param[in] - user name
196 * @return - map of user properties
197 **/
198 UserInfoMap getUserInfo(std::string userName) override;
199
Nan Zhoue48085d2022-10-25 00:07:04 +0000200 protected:
201 /** @brief get pam argument value
202 * method to get argument value from pam configuration
203 *
204 * @param[in] moduleName - name of the module from where arg has to be read
205 * @param[in] argName - argument name
206 * @param[out] argValue - argument value
207 *
208 * @return 0 - success state of the function
209 */
210 int getPamModuleArgValue(const std::string& moduleName,
211 const std::string& argName, std::string& argValue);
212
213 /** @brief set pam argument value
214 * method to set argument value in pam configuration
215 *
216 * @param[in] moduleName - name of the module in which argument value has
217 * to be set
218 * @param[in] argName - argument name
219 * @param[out] argValue - argument value
220 *
221 * @return 0 - success state of the function
222 */
223 int setPamModuleArgValue(const std::string& moduleName,
224 const std::string& argName,
225 const std::string& argValue);
226
Nan Zhou8a11d992022-10-25 00:07:06 +0000227 /** @brief check for user presence
228 * method to check for user existence
229 *
230 * @param[in] userName - name of the user
231 * @return -true if user exists and false if not.
232 */
233 bool isUserExist(const std::string& userName);
234
235 /** @brief check user exists
236 * method to check whether user exist, and throw if not.
237 *
238 * @param[in] userName - name of the user
239 */
240 void throwForUserDoesNotExist(const std::string& userName);
241
242 /** @brief check user does not exist
243 * method to check whether does not exist, and throw if exists.
244 *
245 * @param[in] userName - name of the user
246 */
247 void throwForUserExists(const std::string& userName);
248
Nan Zhou40e44972022-10-25 00:07:07 +0000249 /** @brief check user name constraints
250 * method to check user name constraints and throw if failed.
251 *
252 * @param[in] userName - name of the user
253 * @param[in] groupNames - user groups
254 */
255 void
256 throwForUserNameConstraints(const std::string& userName,
257 const std::vector<std::string>& groupNames);
258
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530259 private:
260 /** @brief sdbusplus handler */
Patrick Williamsb3ef4e12022-07-22 19:26:55 -0500261 sdbusplus::bus_t& bus;
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530262
263 /** @brief object path */
264 const std::string path;
265
266 /** @brief privilege manager container */
267 std::vector<std::string> privMgr = {"priv-admin", "priv-operator",
Richard Marian Thomaiyar32be2962019-11-08 17:21:53 +0530268 "priv-user", "priv-noaccess"};
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530269
270 /** @brief groups manager container */
271 std::vector<std::string> groupsMgr = {"web", "redfish", "ipmi", "ssh"};
272
273 /** @brief map container to hold users object */
274 using UserName = std::string;
275 std::unordered_map<UserName, std::unique_ptr<phosphor::user::Users>>
276 usersList;
277
278 /** @brief get users in group
279 * method to get group user list
280 *
281 * @param[in] groupName - group name
282 *
283 * @return userList - list of users in the group.
284 */
Patrick Williams9638afb2021-02-22 17:16:24 -0600285 std::vector<std::string> getUsersInGroup(const std::string& groupName);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530286
287 /** @brief get user & SSH users list
288 * method to get the users and ssh users list.
289 *
290 *@return - vector of User & SSH user lists
291 */
292 UserSSHLists getUserAndSshGrpList(void);
293
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530294 /** @brief check group user count
295 * method to check max group user count, and throw if limit reached
296 *
297 * @param[in] groupNames - group name
298 */
Patrick Williams9638afb2021-02-22 17:16:24 -0600299 void throwForMaxGrpUserCount(const std::vector<std::string>& groupNames);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530300
301 /** @brief check for valid privielge
302 * method to check valid privilege, and throw if invalid
303 *
304 * @param[in] priv - privilege of the user
305 */
Patrick Williams9638afb2021-02-22 17:16:24 -0600306 void throwForInvalidPrivilege(const std::string& priv);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530307
308 /** @brief check for valid groups
309 * method to check valid groups, and throw if invalid
310 *
311 * @param[in] groupNames - user groups
312 */
Patrick Williams9638afb2021-02-22 17:16:24 -0600313 void throwForInvalidGroups(const std::vector<std::string>& groupName);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530314
315 /** @brief get user enabled state
316 * method to get user enabled state.
317 *
318 * @param[in] userName - name of the user
319 * @return - user enabled status (true/false)
320 */
Patrick Williams9638afb2021-02-22 17:16:24 -0600321 bool isUserEnabled(const std::string& userName);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530322
323 /** @brief initialize the user manager objects
324 * method to initialize the user manager objects accordingly
325 *
326 */
327 void initUserObjects(void);
328
329 /** @brief get IPMI user count
330 * method to get IPMI user count
331 *
332 * @return - returns user count
333 */
334 size_t getIpmiUsersCount(void);
Richard Marian Thomaiyar9164fd92018-06-13 16:51:00 +0530335
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600336 /** @brief get service name
337 * method to get dbus service name
338 *
339 * @param[in] path - object path
340 * @param[in] intf - interface
341 * @return - service name
342 */
Patrick Williams9638afb2021-02-22 17:16:24 -0600343 std::string getServiceName(std::string&& path, std::string&& intf);
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600344
raviteja-b8cc44052019-02-27 23:29:36 -0600345 protected:
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600346 /** @brief get LDAP group name
347 * method to get LDAP group name for the given LDAP user
348 *
349 * @param[in] - userName
350 * @return - group name
351 */
Patrick Williams9638afb2021-02-22 17:16:24 -0600352 virtual std::string getLdapGroupName(const std::string& userName);
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600353
354 /** @brief get privilege mapper object
355 * method to get dbus privilege mapper object
356 *
357 * @return - map of user object
358 */
raviteja-b8cc44052019-02-27 23:29:36 -0600359 virtual DbusUserObj getPrivilegeMapperObject(void);
360
361 friend class TestUserMgr;
Nan Zhoue48085d2022-10-25 00:07:04 +0000362
363 std::string pamPasswdConfigFile;
364 std::string pamAuthConfigFile;
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530365};
366
367} // namespace user
368} // namespace phosphor