blob: 4a82950ffad67efb0db793e8f4205ab566ba411b [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
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053069/** @class UserMgr
70 * @brief Responsible for managing user accounts over the D-Bus interface.
71 */
Ratan Gupta1af12232018-11-03 00:35:38 +053072class UserMgr : public Ifaces
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053073{
74 public:
75 UserMgr() = delete;
76 ~UserMgr() = default;
Patrick Williams9638afb2021-02-22 17:16:24 -060077 UserMgr(const UserMgr&) = delete;
78 UserMgr& operator=(const UserMgr&) = delete;
79 UserMgr(UserMgr&&) = delete;
80 UserMgr& operator=(UserMgr&&) = delete;
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053081
82 /** @brief Constructs UserMgr object.
83 *
84 * @param[in] bus - sdbusplus handler
85 * @param[in] path - D-Bus path
86 */
Patrick Williamsb3ef4e12022-07-22 19:26:55 -050087 UserMgr(sdbusplus::bus_t& bus, const char* path);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053088
89 /** @brief create user method.
90 * This method creates a new user as requested
91 *
92 * @param[in] userName - Name of the user which has to be created
93 * @param[in] groupNames - Group names list, to which user has to be added.
94 * @param[in] priv - Privilege of the user.
95 * @param[in] enabled - State of the user enabled / disabled.
96 */
97 void createUser(std::string userName, std::vector<std::string> groupNames,
98 std::string priv, bool enabled) override;
99
100 /** @brief rename user method.
101 * This method renames the user as requested
102 *
103 * @param[in] userName - current name of the user
104 * @param[in] newUserName - new user name to which it has to be renamed.
105 */
106 void renameUser(std::string userName, std::string newUserName) override;
107
108 /** @brief delete user method.
109 * This method deletes the user as requested
110 *
111 * @param[in] userName - Name of the user which has to be deleted
112 */
113 void deleteUser(std::string userName);
114
115 /** @brief Update user groups & privilege.
116 * This method updates user groups & privilege
117 *
118 * @param[in] userName - user name, for which update is requested
119 * @param[in] groupName - Group to be updated..
120 * @param[in] priv - Privilege to be updated.
121 */
Patrick Williams9638afb2021-02-22 17:16:24 -0600122 void updateGroupsAndPriv(const std::string& userName,
123 const std::vector<std::string>& groups,
124 const std::string& priv);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530125
126 /** @brief Update user enabled state.
127 * This method enables / disables user
128 *
129 * @param[in] userName - user name, for which update is requested
130 * @param[in] enabled - enable / disable the user
131 */
Patrick Williams9638afb2021-02-22 17:16:24 -0600132 void userEnable(const std::string& userName, bool enabled);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530133
Richard Marian Thomaiyar9164fd92018-06-13 16:51:00 +0530134 /** @brief update minimum password length requirement
135 *
136 * @param[in] val - minimum password length
137 * @return - minimum password length
138 */
139 uint8_t minPasswordLength(uint8_t val) override;
140
141 /** @brief update old password history count
142 *
143 * @param[in] val - number of times old passwords has to be avoided
144 * @return - number of times old password has to be avoided
145 */
146 uint8_t rememberOldPasswordTimes(uint8_t val) override;
147
148 /** @brief update maximum number of failed login attempt before locked
149 * out.
150 *
151 * @param[in] val - number of allowed attempt
152 * @return - number of allowed attempt
153 */
154 uint16_t maxLoginAttemptBeforeLockout(uint16_t val) override;
155
156 /** @brief update timeout to unlock the account
157 *
158 * @param[in] val - value in seconds
159 * @return - value in seconds
160 */
161 uint32_t accountUnlockTimeout(uint32_t val) override;
162
Richard Marian Thomaiyarc7045192018-06-13 16:51:00 +0530163 /** @brief lists user locked state for failed attempt
164 *
165 * @param[in] - user name
166 * @return - true / false indicating user locked / un-locked
167 **/
Patrick Williams9638afb2021-02-22 17:16:24 -0600168 virtual bool userLockedForFailedAttempt(const std::string& userName);
Richard Marian Thomaiyarc7045192018-06-13 16:51:00 +0530169
170 /** @brief lists user locked state for failed attempt
171 *
172 * @param[in]: user name
173 * @param[in]: value - false -unlock user account, true - no action taken
174 **/
Patrick Williams9638afb2021-02-22 17:16:24 -0600175 bool userLockedForFailedAttempt(const std::string& userName,
176 const bool& value);
Richard Marian Thomaiyarc7045192018-06-13 16:51:00 +0530177
Joseph Reynolds3ab6cc22020-03-03 14:09:03 -0600178 /** @brief shows if the user's password is expired
179 *
180 * @param[in]: user name
181 * @return - true / false indicating user password expired
182 **/
Patrick Williams9638afb2021-02-22 17:16:24 -0600183 virtual bool userPasswordExpired(const std::string& userName);
Joseph Reynolds3ab6cc22020-03-03 14:09:03 -0600184
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600185 /** @brief returns user info
186 * Checks if user is local user, then returns map of properties of user.
187 * like user privilege, list of user groups, user enabled state and user
188 * locked state. If its not local user, then it checks if its a ldap user,
189 * then it gets the privilege mapping of the LDAP group.
190 *
191 * @param[in] - user name
192 * @return - map of user properties
193 **/
194 UserInfoMap getUserInfo(std::string userName) override;
195
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530196 private:
197 /** @brief sdbusplus handler */
Patrick Williamsb3ef4e12022-07-22 19:26:55 -0500198 sdbusplus::bus_t& bus;
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530199
200 /** @brief object path */
201 const std::string path;
202
203 /** @brief privilege manager container */
204 std::vector<std::string> privMgr = {"priv-admin", "priv-operator",
Richard Marian Thomaiyar32be2962019-11-08 17:21:53 +0530205 "priv-user", "priv-noaccess"};
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530206
207 /** @brief groups manager container */
208 std::vector<std::string> groupsMgr = {"web", "redfish", "ipmi", "ssh"};
209
210 /** @brief map container to hold users object */
211 using UserName = std::string;
212 std::unordered_map<UserName, std::unique_ptr<phosphor::user::Users>>
213 usersList;
214
215 /** @brief get users in group
216 * method to get group user list
217 *
218 * @param[in] groupName - group name
219 *
220 * @return userList - list of users in the group.
221 */
Patrick Williams9638afb2021-02-22 17:16:24 -0600222 std::vector<std::string> getUsersInGroup(const std::string& groupName);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530223
224 /** @brief get user & SSH users list
225 * method to get the users and ssh users list.
226 *
227 *@return - vector of User & SSH user lists
228 */
229 UserSSHLists getUserAndSshGrpList(void);
230
231 /** @brief check for user presence
232 * method to check for user existence
233 *
234 * @param[in] userName - name of the user
235 * @return -true if user exists and false if not.
236 */
Patrick Williams9638afb2021-02-22 17:16:24 -0600237 bool isUserExist(const std::string& userName);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530238
239 /** @brief check user exists
240 * method to check whether user exist, and throw if not.
241 *
242 * @param[in] userName - name of the user
243 */
Patrick Williams9638afb2021-02-22 17:16:24 -0600244 void throwForUserDoesNotExist(const std::string& userName);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530245
246 /** @brief check user does not exist
247 * method to check whether does not exist, and throw if exists.
248 *
249 * @param[in] userName - name of the user
250 */
Patrick Williams9638afb2021-02-22 17:16:24 -0600251 void throwForUserExists(const std::string& userName);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530252
253 /** @brief check user name constraints
254 * method to check user name constraints and throw if failed.
255 *
256 * @param[in] userName - name of the user
257 * @param[in] groupNames - user groups
258 */
259 void
Patrick Williams9638afb2021-02-22 17:16:24 -0600260 throwForUserNameConstraints(const std::string& userName,
261 const std::vector<std::string>& groupNames);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530262
263 /** @brief check group user count
264 * method to check max group user count, and throw if limit reached
265 *
266 * @param[in] groupNames - group name
267 */
Patrick Williams9638afb2021-02-22 17:16:24 -0600268 void throwForMaxGrpUserCount(const std::vector<std::string>& groupNames);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530269
270 /** @brief check for valid privielge
271 * method to check valid privilege, and throw if invalid
272 *
273 * @param[in] priv - privilege of the user
274 */
Patrick Williams9638afb2021-02-22 17:16:24 -0600275 void throwForInvalidPrivilege(const std::string& priv);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530276
277 /** @brief check for valid groups
278 * method to check valid groups, and throw if invalid
279 *
280 * @param[in] groupNames - user groups
281 */
Patrick Williams9638afb2021-02-22 17:16:24 -0600282 void throwForInvalidGroups(const std::vector<std::string>& groupName);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530283
284 /** @brief get user enabled state
285 * method to get user enabled state.
286 *
287 * @param[in] userName - name of the user
288 * @return - user enabled status (true/false)
289 */
Patrick Williams9638afb2021-02-22 17:16:24 -0600290 bool isUserEnabled(const std::string& userName);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530291
292 /** @brief initialize the user manager objects
293 * method to initialize the user manager objects accordingly
294 *
295 */
296 void initUserObjects(void);
297
298 /** @brief get IPMI user count
299 * method to get IPMI user count
300 *
301 * @return - returns user count
302 */
303 size_t getIpmiUsersCount(void);
Richard Marian Thomaiyar9164fd92018-06-13 16:51:00 +0530304
305 /** @brief get pam argument value
306 * method to get argument value from pam configuration
307 *
308 * @param[in] moduleName - name of the module from where arg has to be read
309 * @param[in] argName - argument name
310 * @param[out] argValue - argument value
311 *
312 * @return 0 - success state of the function
313 */
Patrick Williams9638afb2021-02-22 17:16:24 -0600314 int getPamModuleArgValue(const std::string& moduleName,
315 const std::string& argName, std::string& argValue);
Richard Marian Thomaiyar9164fd92018-06-13 16:51:00 +0530316
317 /** @brief set pam argument value
318 * method to set argument value in pam configuration
319 *
320 * @param[in] moduleName - name of the module in which argument value has
321 * to be set
322 * @param[in] argName - argument name
323 * @param[out] argValue - argument value
324 *
325 * @return 0 - success state of the function
326 */
Patrick Williams9638afb2021-02-22 17:16:24 -0600327 int setPamModuleArgValue(const std::string& moduleName,
328 const std::string& argName,
329 const std::string& argValue);
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600330
331 /** @brief get service name
332 * method to get dbus service name
333 *
334 * @param[in] path - object path
335 * @param[in] intf - interface
336 * @return - service name
337 */
Patrick Williams9638afb2021-02-22 17:16:24 -0600338 std::string getServiceName(std::string&& path, std::string&& intf);
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600339
raviteja-b8cc44052019-02-27 23:29:36 -0600340 protected:
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600341 /** @brief get LDAP group name
342 * method to get LDAP group name for the given LDAP user
343 *
344 * @param[in] - userName
345 * @return - group name
346 */
Patrick Williams9638afb2021-02-22 17:16:24 -0600347 virtual std::string getLdapGroupName(const std::string& userName);
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600348
349 /** @brief get privilege mapper object
350 * method to get dbus privilege mapper object
351 *
352 * @return - map of user object
353 */
raviteja-b8cc44052019-02-27 23:29:36 -0600354 virtual DbusUserObj getPrivilegeMapperObject(void);
355
356 friend class TestUserMgr;
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530357};
358
359} // namespace user
360} // namespace phosphor