blob: c008ceab206f2ca08528d876a8ede2bfb05cc570 [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
17#include <sdbusplus/bus.hpp>
18#include <sdbusplus/server/object.hpp>
19#include <xyz/openbmc_project/User/Manager/server.hpp>
Richard Marian Thomaiyar9164fd92018-06-13 16:51:00 +053020#include <xyz/openbmc_project/User/AccountPolicy/server.hpp>
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053021#include <unordered_map>
Ratan Guptaaeaf9412019-02-11 04:41:52 -060022#include <variant>
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053023#include "users.hpp"
24
25namespace phosphor
26{
27namespace user
28{
29
30using UserMgrIface = sdbusplus::xyz::openbmc_project::User::server::Manager;
31using UserSSHLists =
32 std::pair<std::vector<std::string>, std::vector<std::string>>;
Richard Marian Thomaiyar9164fd92018-06-13 16:51:00 +053033using AccountPolicyIface =
34 sdbusplus::xyz::openbmc_project::User::server::AccountPolicy;
35
Ratan Gupta1af12232018-11-03 00:35:38 +053036using Ifaces =
37 sdbusplus::server::object::object<UserMgrIface, AccountPolicyIface>;
38
Ratan Guptaaeaf9412019-02-11 04:41:52 -060039using Privilege = std::string;
40using GroupList = std::vector<std::string>;
41using UserEnabled = bool;
42using PropertyName = std::string;
Ravi Teja5fe724a2019-05-07 05:14:42 -050043using ServiceEnabled = bool;
Ratan Guptaaeaf9412019-02-11 04:41:52 -060044
45using UserInfo = std::variant<Privilege, GroupList, UserEnabled>;
46using UserInfoMap = std::map<PropertyName, UserInfo>;
47
48using DbusUserObjPath = sdbusplus::message::object_path;
49
Ravi Teja5fe724a2019-05-07 05:14:42 -050050using DbusUserPropVariant =
51 sdbusplus::message::variant<Privilege, ServiceEnabled>;
Ratan Guptaaeaf9412019-02-11 04:41:52 -060052
53using DbusUserObjProperties =
54 std::vector<std::pair<PropertyName, DbusUserPropVariant>>;
55
56using Interface = std::string;
57
58using DbusUserObjValue = std::map<Interface, DbusUserObjProperties>;
59
60using DbusUserObj = std::map<DbusUserObjPath, DbusUserObjValue>;
61
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053062/** @class UserMgr
63 * @brief Responsible for managing user accounts over the D-Bus interface.
64 */
Ratan Gupta1af12232018-11-03 00:35:38 +053065class UserMgr : public Ifaces
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +053066{
67 public:
68 UserMgr() = delete;
69 ~UserMgr() = default;
70 UserMgr(const UserMgr &) = delete;
71 UserMgr &operator=(const UserMgr &) = delete;
72 UserMgr(UserMgr &&) = delete;
73 UserMgr &operator=(UserMgr &&) = delete;
74
75 /** @brief Constructs UserMgr object.
76 *
77 * @param[in] bus - sdbusplus handler
78 * @param[in] path - D-Bus path
79 */
80 UserMgr(sdbusplus::bus::bus &bus, const char *path);
81
82 /** @brief create user method.
83 * This method creates a new user as requested
84 *
85 * @param[in] userName - Name of the user which has to be created
86 * @param[in] groupNames - Group names list, to which user has to be added.
87 * @param[in] priv - Privilege of the user.
88 * @param[in] enabled - State of the user enabled / disabled.
89 */
90 void createUser(std::string userName, std::vector<std::string> groupNames,
91 std::string priv, bool enabled) override;
92
93 /** @brief rename user method.
94 * This method renames the user as requested
95 *
96 * @param[in] userName - current name of the user
97 * @param[in] newUserName - new user name to which it has to be renamed.
98 */
99 void renameUser(std::string userName, std::string newUserName) override;
100
101 /** @brief delete user method.
102 * This method deletes the user as requested
103 *
104 * @param[in] userName - Name of the user which has to be deleted
105 */
106 void deleteUser(std::string userName);
107
108 /** @brief Update user groups & privilege.
109 * This method updates user groups & privilege
110 *
111 * @param[in] userName - user name, for which update is requested
112 * @param[in] groupName - Group to be updated..
113 * @param[in] priv - Privilege to be updated.
114 */
115 void updateGroupsAndPriv(const std::string &userName,
116 const std::vector<std::string> &groups,
117 const std::string &priv);
118
119 /** @brief Update user enabled state.
120 * This method enables / disables user
121 *
122 * @param[in] userName - user name, for which update is requested
123 * @param[in] enabled - enable / disable the user
124 */
125 void userEnable(const std::string &userName, bool enabled);
126
Richard Marian Thomaiyar9164fd92018-06-13 16:51:00 +0530127 /** @brief update minimum password length requirement
128 *
129 * @param[in] val - minimum password length
130 * @return - minimum password length
131 */
132 uint8_t minPasswordLength(uint8_t val) override;
133
134 /** @brief update old password history count
135 *
136 * @param[in] val - number of times old passwords has to be avoided
137 * @return - number of times old password has to be avoided
138 */
139 uint8_t rememberOldPasswordTimes(uint8_t val) override;
140
141 /** @brief update maximum number of failed login attempt before locked
142 * out.
143 *
144 * @param[in] val - number of allowed attempt
145 * @return - number of allowed attempt
146 */
147 uint16_t maxLoginAttemptBeforeLockout(uint16_t val) override;
148
149 /** @brief update timeout to unlock the account
150 *
151 * @param[in] val - value in seconds
152 * @return - value in seconds
153 */
154 uint32_t accountUnlockTimeout(uint32_t val) override;
155
Richard Marian Thomaiyarc7045192018-06-13 16:51:00 +0530156 /** @brief lists user locked state for failed attempt
157 *
158 * @param[in] - user name
159 * @return - true / false indicating user locked / un-locked
160 **/
raviteja-b8cc44052019-02-27 23:29:36 -0600161 virtual bool userLockedForFailedAttempt(const std::string &userName);
Richard Marian Thomaiyarc7045192018-06-13 16:51:00 +0530162
163 /** @brief lists user locked state for failed attempt
164 *
165 * @param[in]: user name
166 * @param[in]: value - false -unlock user account, true - no action taken
167 **/
168 bool userLockedForFailedAttempt(const std::string &userName,
169 const bool &value);
170
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600171 /** @brief returns user info
172 * Checks if user is local user, then returns map of properties of user.
173 * like user privilege, list of user groups, user enabled state and user
174 * locked state. If its not local user, then it checks if its a ldap user,
175 * then it gets the privilege mapping of the LDAP group.
176 *
177 * @param[in] - user name
178 * @return - map of user properties
179 **/
180 UserInfoMap getUserInfo(std::string userName) override;
181
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530182 private:
183 /** @brief sdbusplus handler */
184 sdbusplus::bus::bus &bus;
185
186 /** @brief object path */
187 const std::string path;
188
189 /** @brief privilege manager container */
190 std::vector<std::string> privMgr = {"priv-admin", "priv-operator",
Richard Marian Thomaiyar32be2962019-11-08 17:21:53 +0530191 "priv-user", "priv-noaccess"};
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530192
193 /** @brief groups manager container */
194 std::vector<std::string> groupsMgr = {"web", "redfish", "ipmi", "ssh"};
195
196 /** @brief map container to hold users object */
197 using UserName = std::string;
198 std::unordered_map<UserName, std::unique_ptr<phosphor::user::Users>>
199 usersList;
200
201 /** @brief get users in group
202 * method to get group user list
203 *
204 * @param[in] groupName - group name
205 *
206 * @return userList - list of users in the group.
207 */
208 std::vector<std::string> getUsersInGroup(const std::string &groupName);
209
210 /** @brief get user & SSH users list
211 * method to get the users and ssh users list.
212 *
213 *@return - vector of User & SSH user lists
214 */
215 UserSSHLists getUserAndSshGrpList(void);
216
217 /** @brief check for user presence
218 * method to check for user existence
219 *
220 * @param[in] userName - name of the user
221 * @return -true if user exists and false if not.
222 */
223 bool isUserExist(const std::string &userName);
224
225 /** @brief check user exists
226 * method to check whether user exist, and throw if not.
227 *
228 * @param[in] userName - name of the user
229 */
230 void throwForUserDoesNotExist(const std::string &userName);
231
232 /** @brief check user does not exist
233 * method to check whether does not exist, and throw if exists.
234 *
235 * @param[in] userName - name of the user
236 */
237 void throwForUserExists(const std::string &userName);
238
239 /** @brief check user name constraints
240 * method to check user name constraints and throw if failed.
241 *
242 * @param[in] userName - name of the user
243 * @param[in] groupNames - user groups
244 */
245 void
246 throwForUserNameConstraints(const std::string &userName,
247 const std::vector<std::string> &groupNames);
248
249 /** @brief check group user count
250 * method to check max group user count, and throw if limit reached
251 *
252 * @param[in] groupNames - group name
253 */
254 void throwForMaxGrpUserCount(const std::vector<std::string> &groupNames);
255
256 /** @brief check for valid privielge
257 * method to check valid privilege, and throw if invalid
258 *
259 * @param[in] priv - privilege of the user
260 */
261 void throwForInvalidPrivilege(const std::string &priv);
262
263 /** @brief check for valid groups
264 * method to check valid groups, and throw if invalid
265 *
266 * @param[in] groupNames - user groups
267 */
268 void throwForInvalidGroups(const std::vector<std::string> &groupName);
269
270 /** @brief get user enabled state
271 * method to get user enabled state.
272 *
273 * @param[in] userName - name of the user
274 * @return - user enabled status (true/false)
275 */
276 bool isUserEnabled(const std::string &userName);
277
278 /** @brief initialize the user manager objects
279 * method to initialize the user manager objects accordingly
280 *
281 */
282 void initUserObjects(void);
283
284 /** @brief get IPMI user count
285 * method to get IPMI user count
286 *
287 * @return - returns user count
288 */
289 size_t getIpmiUsersCount(void);
Richard Marian Thomaiyar9164fd92018-06-13 16:51:00 +0530290
291 /** @brief get pam argument value
292 * method to get argument value from pam configuration
293 *
294 * @param[in] moduleName - name of the module from where arg has to be read
295 * @param[in] argName - argument name
296 * @param[out] argValue - argument value
297 *
298 * @return 0 - success state of the function
299 */
300 int getPamModuleArgValue(const std::string &moduleName,
301 const std::string &argName, std::string &argValue);
302
303 /** @brief set pam argument value
304 * method to set argument value in pam configuration
305 *
306 * @param[in] moduleName - name of the module in which argument value has
307 * to be set
308 * @param[in] argName - argument name
309 * @param[out] argValue - argument value
310 *
311 * @return 0 - success state of the function
312 */
313 int setPamModuleArgValue(const std::string &moduleName,
314 const std::string &argName,
315 const std::string &argValue);
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600316
317 /** @brief get service name
318 * method to get dbus service name
319 *
320 * @param[in] path - object path
321 * @param[in] intf - interface
322 * @return - service name
323 */
324 std::string getServiceName(std::string &&path, std::string &&intf);
325
raviteja-b8cc44052019-02-27 23:29:36 -0600326 protected:
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600327 /** @brief get LDAP group name
328 * method to get LDAP group name for the given LDAP user
329 *
330 * @param[in] - userName
331 * @return - group name
332 */
raviteja-b8cc44052019-02-27 23:29:36 -0600333 virtual std::string getLdapGroupName(const std::string &userName);
Ratan Guptaaeaf9412019-02-11 04:41:52 -0600334
335 /** @brief get privilege mapper object
336 * method to get dbus privilege mapper object
337 *
338 * @return - map of user object
339 */
raviteja-b8cc44052019-02-27 23:29:36 -0600340 virtual DbusUserObj getPrivilegeMapperObject(void);
341
342 friend class TestUserMgr;
Richard Marian Thomaiyar9f630d92018-05-24 10:49:10 +0530343};
344
345} // namespace user
346} // namespace phosphor