blob: f5aac223df449ca14bc57f82a926ed1ce2bf5203 [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
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053024#include <unordered_map>
Ratan Guptaaeaf9412019-02-11 04:41:52 -060025#include <variant>
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053026
27namespace phosphor
28{
29namespace user
30{
31
32using UserMgrIface = sdbusplus::xyz::openbmc_project::User::server::Manager;
33using UserSSHLists =
34 std::pair<std::vector<std::string>, std::vector<std::string>>;
Richard Marian Thomaiyar9164fd92018-06-13 16:51:00 +053035using AccountPolicyIface =
36 sdbusplus::xyz::openbmc_project::User::server::AccountPolicy;
37
Ratan Gupta1af12232018-11-03 00:35:38 +053038using Ifaces =
39 sdbusplus::server::object::object<UserMgrIface, AccountPolicyIface>;
40
Ratan Guptaaeaf9412019-02-11 04:41:52 -060041using Privilege = std::string;
42using GroupList = std::vector<std::string>;
43using UserEnabled = bool;
44using PropertyName = std::string;
Ravi Teja5fe724a2019-05-07 05:14:42 -050045using ServiceEnabled = bool;
Ratan Guptaaeaf9412019-02-11 04:41:52 -060046
47using UserInfo = std::variant<Privilege, GroupList, UserEnabled>;
48using UserInfoMap = std::map<PropertyName, UserInfo>;
49
50using DbusUserObjPath = sdbusplus::message::object_path;
51
Patrick Williamsfdf09372020-05-13 18:01:45 -050052using DbusUserPropVariant = std::variant<Privilege, ServiceEnabled>;
Ratan Guptaaeaf9412019-02-11 04:41:52 -060053
54using DbusUserObjProperties =
55 std::vector<std::pair<PropertyName, DbusUserPropVariant>>;
56
57using Interface = std::string;
58
59using DbusUserObjValue = std::map<Interface, DbusUserObjProperties>;
60
61using DbusUserObj = std::map<DbusUserObjPath, DbusUserObjValue>;
62
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053063/** @class UserMgr
64 * @brief Responsible for managing user accounts over the D-Bus interface.
65 */
Ratan Gupta1af12232018-11-03 00:35:38 +053066class UserMgr : public Ifaces
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053067{
68 public:
69 UserMgr() = delete;
70 ~UserMgr() = default;
Patrick Williams9638afb2021-02-22 17:16:24 -060071 UserMgr(const UserMgr&) = delete;
72 UserMgr& operator=(const UserMgr&) = delete;
73 UserMgr(UserMgr&&) = delete;
74 UserMgr& operator=(UserMgr&&) = delete;
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053075
76 /** @brief Constructs UserMgr object.
77 *
78 * @param[in] bus - sdbusplus handler
79 * @param[in] path - D-Bus path
80 */
Patrick Williams9638afb2021-02-22 17:16:24 -060081 UserMgr(sdbusplus::bus::bus& bus, const char* path);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053082
83 /** @brief create user method.
84 * This method creates a new user as requested
85 *
86 * @param[in] userName - Name of the user which has to be created
87 * @param[in] groupNames - Group names list, to which user has to be added.
88 * @param[in] priv - Privilege of the user.
89 * @param[in] enabled - State of the user enabled / disabled.
90 */
91 void createUser(std::string userName, std::vector<std::string> groupNames,
92 std::string priv, bool enabled) override;
93
94 /** @brief rename user method.
95 * This method renames the user as requested
96 *
97 * @param[in] userName - current name of the user
98 * @param[in] newUserName - new user name to which it has to be renamed.
99 */
100 void renameUser(std::string userName, std::string newUserName) override;
101
102 /** @brief delete user method.
103 * This method deletes the user as requested
104 *
105 * @param[in] userName - Name of the user which has to be deleted
106 */
107 void deleteUser(std::string userName);
108
109 /** @brief Update user groups & privilege.
110 * This method updates user groups & privilege
111 *
112 * @param[in] userName - user name, for which update is requested
113 * @param[in] groupName - Group to be updated..
114 * @param[in] priv - Privilege to be updated.
115 */
Patrick Williams9638afb2021-02-22 17:16:24 -0600116 void updateGroupsAndPriv(const std::string& userName,
117 const std::vector<std::string>& groups,
118 const std::string& priv);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530119
120 /** @brief Update user enabled state.
121 * This method enables / disables user
122 *
123 * @param[in] userName - user name, for which update is requested
124 * @param[in] enabled - enable / disable the user
125 */
Patrick Williams9638afb2021-02-22 17:16:24 -0600126 void userEnable(const std::string& userName, bool enabled);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530127
Richard Marian Thomaiyar9164fd92018-06-13 16:51:00 +0530128 /** @brief update minimum password length requirement
129 *
130 * @param[in] val - minimum password length
131 * @return - minimum password length
132 */
133 uint8_t minPasswordLength(uint8_t val) override;
134
135 /** @brief update old password history count
136 *
137 * @param[in] val - number of times old passwords has to be avoided
138 * @return - number of times old password has to be avoided
139 */
140 uint8_t rememberOldPasswordTimes(uint8_t val) override;
141
142 /** @brief update maximum number of failed login attempt before locked
143 * out.
144 *
145 * @param[in] val - number of allowed attempt
146 * @return - number of allowed attempt
147 */
148 uint16_t maxLoginAttemptBeforeLockout(uint16_t val) override;
149
150 /** @brief update timeout to unlock the account
151 *
152 * @param[in] val - value in seconds
153 * @return - value in seconds
154 */
155 uint32_t accountUnlockTimeout(uint32_t val) override;
156
Richard Marian Thomaiyarc7045192018-06-13 16:51:00 +0530157 /** @brief lists user locked state for failed attempt
158 *
159 * @param[in] - user name
160 * @return - true / false indicating user locked / un-locked
161 **/
Patrick Williams9638afb2021-02-22 17:16:24 -0600162 virtual bool userLockedForFailedAttempt(const std::string& userName);
Richard Marian Thomaiyarc7045192018-06-13 16:51:00 +0530163
164 /** @brief lists user locked state for failed attempt
165 *
166 * @param[in]: user name
167 * @param[in]: value - false -unlock user account, true - no action taken
168 **/
Patrick Williams9638afb2021-02-22 17:16:24 -0600169 bool userLockedForFailedAttempt(const std::string& userName,
170 const bool& value);
Richard Marian Thomaiyarc7045192018-06-13 16:51:00 +0530171
Joseph Reynolds3ab6cc22020-03-03 14:09:03 -0600172 /** @brief shows if the user's password is expired
173 *
174 * @param[in]: user name
175 * @return - true / false indicating user password expired
176 **/
Patrick Williams9638afb2021-02-22 17:16:24 -0600177 virtual bool userPasswordExpired(const std::string& userName);
Joseph Reynolds3ab6cc22020-03-03 14:09:03 -0600178
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600179 /** @brief returns user info
180 * Checks if user is local user, then returns map of properties of user.
181 * like user privilege, list of user groups, user enabled state and user
182 * locked state. If its not local user, then it checks if its a ldap user,
183 * then it gets the privilege mapping of the LDAP group.
184 *
185 * @param[in] - user name
186 * @return - map of user properties
187 **/
188 UserInfoMap getUserInfo(std::string userName) override;
189
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530190 private:
191 /** @brief sdbusplus handler */
Patrick Williams9638afb2021-02-22 17:16:24 -0600192 sdbusplus::bus::bus& bus;
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530193
194 /** @brief object path */
195 const std::string path;
196
197 /** @brief privilege manager container */
198 std::vector<std::string> privMgr = {"priv-admin", "priv-operator",
Richard Marian Thomaiyar32be2962019-11-08 17:21:53 +0530199 "priv-user", "priv-noaccess"};
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530200
201 /** @brief groups manager container */
202 std::vector<std::string> groupsMgr = {"web", "redfish", "ipmi", "ssh"};
203
204 /** @brief map container to hold users object */
205 using UserName = std::string;
206 std::unordered_map<UserName, std::unique_ptr<phosphor::user::Users>>
207 usersList;
208
209 /** @brief get users in group
210 * method to get group user list
211 *
212 * @param[in] groupName - group name
213 *
214 * @return userList - list of users in the group.
215 */
Patrick Williams9638afb2021-02-22 17:16:24 -0600216 std::vector<std::string> getUsersInGroup(const std::string& groupName);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530217
218 /** @brief get user & SSH users list
219 * method to get the users and ssh users list.
220 *
221 *@return - vector of User & SSH user lists
222 */
223 UserSSHLists getUserAndSshGrpList(void);
224
225 /** @brief check for user presence
226 * method to check for user existence
227 *
228 * @param[in] userName - name of the user
229 * @return -true if user exists and false if not.
230 */
Patrick Williams9638afb2021-02-22 17:16:24 -0600231 bool isUserExist(const std::string& userName);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530232
233 /** @brief check user exists
234 * method to check whether user exist, and throw if not.
235 *
236 * @param[in] userName - name of the user
237 */
Patrick Williams9638afb2021-02-22 17:16:24 -0600238 void throwForUserDoesNotExist(const std::string& userName);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530239
240 /** @brief check user does not exist
241 * method to check whether does not exist, and throw if exists.
242 *
243 * @param[in] userName - name of the user
244 */
Patrick Williams9638afb2021-02-22 17:16:24 -0600245 void throwForUserExists(const std::string& userName);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530246
247 /** @brief check user name constraints
248 * method to check user name constraints and throw if failed.
249 *
250 * @param[in] userName - name of the user
251 * @param[in] groupNames - user groups
252 */
253 void
Patrick Williams9638afb2021-02-22 17:16:24 -0600254 throwForUserNameConstraints(const std::string& userName,
255 const std::vector<std::string>& groupNames);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530256
257 /** @brief check group user count
258 * method to check max group user count, and throw if limit reached
259 *
260 * @param[in] groupNames - group name
261 */
Patrick Williams9638afb2021-02-22 17:16:24 -0600262 void throwForMaxGrpUserCount(const std::vector<std::string>& groupNames);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530263
264 /** @brief check for valid privielge
265 * method to check valid privilege, and throw if invalid
266 *
267 * @param[in] priv - privilege of the user
268 */
Patrick Williams9638afb2021-02-22 17:16:24 -0600269 void throwForInvalidPrivilege(const std::string& priv);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530270
271 /** @brief check for valid groups
272 * method to check valid groups, and throw if invalid
273 *
274 * @param[in] groupNames - user groups
275 */
Patrick Williams9638afb2021-02-22 17:16:24 -0600276 void throwForInvalidGroups(const std::vector<std::string>& groupName);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530277
278 /** @brief get user enabled state
279 * method to get user enabled state.
280 *
281 * @param[in] userName - name of the user
282 * @return - user enabled status (true/false)
283 */
Patrick Williams9638afb2021-02-22 17:16:24 -0600284 bool isUserEnabled(const std::string& userName);
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530285
286 /** @brief initialize the user manager objects
287 * method to initialize the user manager objects accordingly
288 *
289 */
290 void initUserObjects(void);
291
292 /** @brief get IPMI user count
293 * method to get IPMI user count
294 *
295 * @return - returns user count
296 */
297 size_t getIpmiUsersCount(void);
Richard Marian Thomaiyar9164fd92018-06-13 16:51:00 +0530298
299 /** @brief get pam argument value
300 * method to get argument value from pam configuration
301 *
302 * @param[in] moduleName - name of the module from where arg has to be read
303 * @param[in] argName - argument name
304 * @param[out] argValue - argument value
305 *
306 * @return 0 - success state of the function
307 */
Patrick Williams9638afb2021-02-22 17:16:24 -0600308 int getPamModuleArgValue(const std::string& moduleName,
309 const std::string& argName, std::string& argValue);
Richard Marian Thomaiyar9164fd92018-06-13 16:51:00 +0530310
311 /** @brief set pam argument value
312 * method to set argument value in pam configuration
313 *
314 * @param[in] moduleName - name of the module in which argument value has
315 * to be set
316 * @param[in] argName - argument name
317 * @param[out] argValue - argument value
318 *
319 * @return 0 - success state of the function
320 */
Patrick Williams9638afb2021-02-22 17:16:24 -0600321 int setPamModuleArgValue(const std::string& moduleName,
322 const std::string& argName,
323 const std::string& argValue);
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600324
325 /** @brief get service name
326 * method to get dbus service name
327 *
328 * @param[in] path - object path
329 * @param[in] intf - interface
330 * @return - service name
331 */
Patrick Williams9638afb2021-02-22 17:16:24 -0600332 std::string getServiceName(std::string&& path, std::string&& intf);
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600333
raviteja-b8cc44052019-02-27 23:29:36 -0600334 protected:
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600335 /** @brief get LDAP group name
336 * method to get LDAP group name for the given LDAP user
337 *
338 * @param[in] - userName
339 * @return - group name
340 */
Patrick Williams9638afb2021-02-22 17:16:24 -0600341 virtual std::string getLdapGroupName(const std::string& userName);
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600342
343 /** @brief get privilege mapper object
344 * method to get dbus privilege mapper object
345 *
346 * @return - map of user object
347 */
raviteja-b8cc44052019-02-27 23:29:36 -0600348 virtual DbusUserObj getPrivilegeMapperObject(void);
349
350 friend class TestUserMgr;
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530351};
352
353} // namespace user
354} // namespace phosphor