1 /*
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 
17 #include "user_layer.hpp"
18 
19 #include "passwd_mgr.hpp"
20 #include "user_mgmt.hpp"
21 
22 namespace
23 {
24 ipmi::PasswdMgr passwdMgr;
25 }
26 
27 namespace ipmi
28 {
29 
30 ipmi_ret_t ipmiUserInit()
31 {
32     getUserAccessObject();
33     return IPMI_CC_OK;
34 }
35 
36 std::string ipmiUserGetPassword(const std::string& userName)
37 {
38     return passwdMgr.getPasswdByUserName(userName);
39 }
40 
41 ipmi_ret_t ipmiClearUserEntryPassword(const std::string& userName)
42 {
43     if (passwdMgr.updateUserEntry(userName, "") != 0)
44     {
45         return IPMI_CC_UNSPECIFIED_ERROR;
46     }
47     return IPMI_CC_OK;
48 }
49 
50 ipmi_ret_t ipmiRenameUserEntryPassword(const std::string& userName,
51                                        const std::string& newUserName)
52 {
53     if (passwdMgr.updateUserEntry(userName, newUserName) != 0)
54     {
55         return IPMI_CC_UNSPECIFIED_ERROR;
56     }
57     return IPMI_CC_OK;
58 }
59 
60 bool ipmiUserIsValidUserId(const uint8_t userId)
61 {
62     return UserAccess::isValidUserId(userId);
63 }
64 
65 bool ipmiUserIsValidPrivilege(const uint8_t priv)
66 {
67     return UserAccess::isValidPrivilege(priv);
68 }
69 
70 uint8_t ipmiUserGetUserId(const std::string& userName)
71 {
72     return getUserAccessObject().getUserId(userName);
73 }
74 
75 ipmi_ret_t ipmiUserSetUserName(const uint8_t userId, const char* userName)
76 {
77     return getUserAccessObject().setUserName(userId, userName);
78 }
79 
80 ipmi_ret_t ipmiUserGetUserName(const uint8_t userId, std::string& userName)
81 {
82     return getUserAccessObject().getUserName(userId, userName);
83 }
84 
85 ipmi_ret_t ipmiUserGetAllCounts(uint8_t& maxChUsers, uint8_t& enabledUsers,
86                                 uint8_t& fixedUsers)
87 {
88     maxChUsers = ipmiMaxUsers;
89     UsersTbl* userData = getUserAccessObject().getUsersTblPtr();
90     enabledUsers = 0;
91     fixedUsers = 0;
92     // user index 0 is reserved, starts with 1
93     for (size_t count = 1; count <= ipmiMaxUsers; ++count)
94     {
95         if (userData->user[count].userEnabled)
96         {
97             enabledUsers++;
98         }
99         if (userData->user[count].fixedUserName)
100         {
101             fixedUsers++;
102         }
103     }
104     return IPMI_CC_OK;
105 }
106 
107 ipmi_ret_t ipmiUserUpdateEnabledState(const uint8_t userId, const bool& state)
108 {
109     return getUserAccessObject().setUserEnabledState(userId, state);
110 }
111 
112 ipmi_ret_t ipmiUserCheckEnabled(const uint8_t userId, bool& state)
113 {
114     if (!UserAccess::isValidUserId(userId))
115     {
116         return IPMI_CC_PARM_OUT_OF_RANGE;
117     }
118     UserInfo* userInfo = getUserAccessObject().getUserInfo(userId);
119     state = userInfo->userEnabled;
120     return IPMI_CC_OK;
121 }
122 
123 ipmi_ret_t ipmiUserGetPrivilegeAccess(const uint8_t userId, const uint8_t chNum,
124                                       PrivAccess& privAccess)
125 {
126 
127     if (!UserAccess::isValidChannel(chNum))
128     {
129         return IPMI_CC_INVALID_FIELD_REQUEST;
130     }
131     if (!UserAccess::isValidUserId(userId))
132     {
133         return IPMI_CC_PARM_OUT_OF_RANGE;
134     }
135     UserInfo* userInfo = getUserAccessObject().getUserInfo(userId);
136     privAccess.privilege = userInfo->userPrivAccess[chNum].privilege;
137     privAccess.ipmiEnabled = userInfo->userPrivAccess[chNum].ipmiEnabled;
138     privAccess.linkAuthEnabled =
139         userInfo->userPrivAccess[chNum].linkAuthEnabled;
140     privAccess.accessCallback = userInfo->userPrivAccess[chNum].accessCallback;
141 
142     return IPMI_CC_OK;
143 }
144 
145 ipmi_ret_t ipmiUserSetPrivilegeAccess(const uint8_t userId, const uint8_t chNum,
146                                       const PrivAccess& privAccess,
147                                       const bool& otherPrivUpdates)
148 {
149     UserPrivAccess userPrivAccess;
150     userPrivAccess.privilege = privAccess.privilege;
151     if (otherPrivUpdates)
152     {
153         userPrivAccess.ipmiEnabled = privAccess.ipmiEnabled;
154         userPrivAccess.linkAuthEnabled = privAccess.linkAuthEnabled;
155         userPrivAccess.accessCallback = privAccess.accessCallback;
156     }
157     return getUserAccessObject().setUserPrivilegeAccess(
158         userId, chNum, userPrivAccess, otherPrivUpdates);
159 }
160 
161 } // namespace ipmi
162