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 #pragma once
17 
18 #include <bitset>
19 #include <ipmid/api.hpp>
20 #include <string>
21 
22 namespace ipmi
23 {
24 
25 // TODO: Has to be replaced with proper channel number assignment logic
26 /**
27  * @enum Channel Id
28  */
29 enum class EChannelID : uint8_t
30 {
31     chanLan1 = 0x01
32 };
33 
34 static constexpr uint8_t invalidUserId = 0xFF;
35 static constexpr uint8_t reservedUserId = 0x0;
36 static constexpr uint8_t ipmiMaxUserName = 16;
37 static constexpr uint8_t ipmiMaxUsers = 15;
38 static constexpr uint8_t ipmiMaxChannels = 16;
39 static constexpr uint8_t maxIpmi20PasswordSize = 20;
40 static constexpr uint8_t maxIpmi15PasswordSize = 16;
41 static constexpr uint8_t payloadsPerByte = 8;
42 
43 /** @struct PrivAccess
44  *
45  *  User privilege related access data as per IPMI specification.(refer spec
46  * sec 22.26)
47  */
48 struct PrivAccess
49 {
50 #if BYTE_ORDER == LITTLE_ENDIAN
51     uint8_t privilege : 4;
52     uint8_t ipmiEnabled : 1;
53     uint8_t linkAuthEnabled : 1;
54     uint8_t accessCallback : 1;
55     uint8_t reserved : 1;
56 #endif
57 #if BYTE_ORDER == BIG_ENDIAN
58     uint8_t reserved : 1;
59     uint8_t accessCallback : 1;
60     uint8_t linkAuthEnabled : 1;
61     uint8_t ipmiEnabled : 1;
62     uint8_t privilege : 4;
63 #endif
64 } __attribute__((packed));
65 
66 /** @struct UserPayloadAccess
67  *
68  *  Structure to denote payload access restrictions applicable for a
69  *  given user and channel. (refer spec sec 24.6)
70  */
71 struct PayloadAccess
72 {
73     std::bitset<payloadsPerByte> stdPayloadEnables1;
74     std::bitset<payloadsPerByte> stdPayloadEnables2Reserved;
75     std::bitset<payloadsPerByte> oemPayloadEnables1;
76     std::bitset<payloadsPerByte> oemPayloadEnables2Reserved;
77 };
78 
79 /** @brief initializes user management
80  *
81  *  @return ccSuccess for success, others for failure.
82  */
83 Cc ipmiUserInit();
84 
85 /** @brief The ipmi get user password layer call
86  *
87  *  @param[in] userName - user name
88  *
89  *  @return password or empty string
90  */
91 std::string ipmiUserGetPassword(const std::string& userName);
92 
93 /** @brief The IPMI call to clear password entry associated with specified
94  * username
95  *
96  *  @param[in] userName - user name to be removed
97  *
98  *  @return 0 on success, non-zero otherwise.
99  */
100 Cc ipmiClearUserEntryPassword(const std::string& userName);
101 
102 /** @brief The IPMI call to reuse password entry for the renamed user
103  *  to another one
104  *
105  *  @param[in] userName - user name which has to be renamed
106  *  @param[in] newUserName - new user name
107  *
108  *  @return 0 on success, non-zero otherwise.
109  */
110 Cc ipmiRenameUserEntryPassword(const std::string& userName,
111                                const std::string& newUserName);
112 
113 /** @brief determines valid userId
114  *
115  *  @param[in] userId - user id
116  *
117  *  @return true if valid, false otherwise
118  */
119 bool ipmiUserIsValidUserId(const uint8_t userId);
120 
121 /** @brief determines valid privilege level
122  *
123  *  @param[in] priv - privilege level
124  *
125  *  @return true if valid, false otherwise
126  */
127 bool ipmiUserIsValidPrivilege(const uint8_t priv);
128 
129 /** @brief get user id corresponding to the user name
130  *
131  *  @param[in] userName - user name
132  *
133  *  @return userid. Will return 0xff if no user id found
134  */
135 uint8_t ipmiUserGetUserId(const std::string& userName);
136 
137 /** @brief set's user name
138  *
139  *  @param[in] userId - user id
140  *  @param[in] userName - user name
141  *
142  *  @return ccSuccess for success, others for failure.
143  */
144 Cc ipmiUserSetUserName(const uint8_t userId, const std::string& userName);
145 
146 /** @brief set user password
147  *
148  *  @param[in] userId - user id
149  *  @param[in] userPassword - New Password
150  *
151  *  @return ccSuccess for success, others for failure.
152  */
153 Cc ipmiUserSetUserPassword(const uint8_t userId, const char* userPassword);
154 
155 /** @brief set special user password (non-ipmi accounts)
156  *
157  *  @param[in] userName - user name
158  *  @param[in] userPassword - New Password
159  *
160  *  @return ccSuccess for success, others for failure.
161  */
162 Cc ipmiSetSpecialUserPassword(const std::string& userName,
163                               const std::string& userPassword);
164 
165 /** @brief get user name
166  *
167  *  @param[in] userId - user id
168  *  @param[out] userName - user name
169  *
170  *  @return ccSuccess for success, others for failure.
171  */
172 Cc ipmiUserGetUserName(const uint8_t userId, std::string& userName);
173 
174 /** @brief provides available fixed, max, and enabled user counts
175  *
176  *  @param[out] maxChUsers - max channel users
177  *  @param[out] enabledUsers - enabled user count
178  *  @param[out] fixedUsers - fixed user count
179  *
180  *  @return ccSuccess for success, others for failure.
181  */
182 Cc ipmiUserGetAllCounts(uint8_t& maxChUsers, uint8_t& enabledUsers,
183                         uint8_t& fixedUsers);
184 
185 /** @brief function to update user enabled state
186  *
187  *  @param[in] userId - user id
188  *..@param[in] state - state of the user to be updated, true - user enabled.
189  *
190  *  @return ccSuccess for success, others for failure.
191  */
192 Cc ipmiUserUpdateEnabledState(const uint8_t userId, const bool& state);
193 
194 /** @brief determines whether user is enabled
195  *
196  *  @param[in] userId - user id
197  *..@param[out] state - state of the user
198  *
199  *  @return ccSuccess for success, others for failure.
200  */
201 Cc ipmiUserCheckEnabled(const uint8_t userId, bool& state);
202 
203 /** @brief provides user privilege access data
204  *
205  *  @param[in] userId - user id
206  *  @param[in] chNum - channel number
207  *  @param[out] privAccess - privilege access data
208  *
209  *  @return ccSuccess for success, others for failure.
210  */
211 Cc ipmiUserGetPrivilegeAccess(const uint8_t userId, const uint8_t chNum,
212                               PrivAccess& privAccess);
213 
214 /** @brief sets user privilege access data
215  *
216  *  @param[in] userId - user id
217  *  @param[in] chNum - channel number
218  *  @param[in] privAccess - privilege access data
219  *  @param[in] otherPrivUpdate - flags to indicate other fields update
220  *
221  *  @return ccSuccess for success, others for failure.
222  */
223 Cc ipmiUserSetPrivilegeAccess(const uint8_t userId, const uint8_t chNum,
224                               const PrivAccess& privAccess,
225                               const bool& otherPrivUpdate);
226 
227 /** @brief check for user pam authentication. This is to determine, whether user
228  * is already locked out for failed login attempt
229  *
230  *  @param[in] username - username
231  *  @param[in] password - password
232  *
233  *  @return status
234  */
235 bool ipmiUserPamAuthenticate(std::string_view userName,
236                              std::string_view userPassword);
237 
238 /** @brief sets user payload access data
239  *
240  *  @param[in] chNum - channel number
241  *  @param[in] operation - ENABLE / DISABLE operation
242  *  @param[in] userId - user id
243  *  @param[in] payloadAccess - payload access data
244  *
245  *  @return ccSuccess for success, others for failure.
246  */
247 Cc ipmiUserSetUserPayloadAccess(const uint8_t chNum, const uint8_t operation,
248                                 const uint8_t userId,
249                                 const PayloadAccess& payloadAccess);
250 
251 /** @brief provides user payload access data
252  *
253  *  @param[in] chNum - channel number
254  *  @param[in] userId - user id
255  *  @param[out] payloadAccess - payload access data
256  *
257  *  @return ccSuccess for success, others for failure.
258  */
259 Cc ipmiUserGetUserPayloadAccess(const uint8_t chNum, const uint8_t userId,
260                                 PayloadAccess& payloadAccess);
261 
262 } // namespace ipmi
263