blob: ed76efa6f332f80b7afd305e3039f6660b054354 [file] [log] [blame]
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +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 <openssl/evp.h>
18
19#include <ctime>
20#include <unordered_map>
AppaRao Pulib29b5ab2018-05-17 10:28:48 +053021#include <vector>
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +053022
23namespace ipmi
24{
25
26class PasswdMgr
27{
28 public:
29 ~PasswdMgr() = default;
30 PasswdMgr(const PasswdMgr&) = delete;
31 PasswdMgr& operator=(const PasswdMgr&) = delete;
32 PasswdMgr(PasswdMgr&&) = delete;
33 PasswdMgr& operator=(PasswdMgr&&) = delete;
34
35 /** @brief Constructs user password list
36 *
37 */
38 PasswdMgr();
39
40 /** @brief Get password for the user
41 *
42 * @param[in] userName - user name
43 *
44 * @return password string. will return empty string, if unable to locate
45 * the user
46 */
47 std::string getPasswdByUserName(const std::string& userName);
48
Richard Marian Thomaiyar42bed642018-09-21 12:28:57 +053049 /** @brief Update / clear username and password entry for the specified
50 * user
AppaRao Pulib29b5ab2018-05-17 10:28:48 +053051 *
Richard Marian Thomaiyar42bed642018-09-21 12:28:57 +053052 * @param[in] userName - user name that has to be renamed / deleted
53 * @param[in] newUserName - new user name. If empty, userName will be
54 * deleted.
AppaRao Pulib29b5ab2018-05-17 10:28:48 +053055 *
56 * @return error response
57 */
Richard Marian Thomaiyar42bed642018-09-21 12:28:57 +053058 int updateUserEntry(const std::string& userName,
59 const std::string& newUserName);
AppaRao Pulib29b5ab2018-05-17 10:28:48 +053060
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +053061 private:
62 using UserName = std::string;
63 using Password = std::string;
64 std::unordered_map<UserName, Password> passwdMapList;
65 std::time_t fileLastUpdatedTime;
Richard Marian Thomaiyar6ba8d312020-04-10 23:52:50 +053066
67 /** @brief restrict file permission
68 *
69 */
70 void restrictFilesPermission(void);
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +053071 /** @brief check timestamp and reload password map if required
72 *
73 */
74 void checkAndReload(void);
75 /** @brief initializes passwdMapList by reading the encrypted file
76 *
77 * Initializes the passwordMapList members after decrypting the
78 * password file. passwordMapList will be used further in IPMI
79 * authentication.
80 */
81 void initPasswordMap(void);
AppaRao Pulib29b5ab2018-05-17 10:28:48 +053082
83 /** @brief Function to read the encrypted password file data
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +053084 *
AppaRao Pulib29b5ab2018-05-17 10:28:48 +053085 * @param[out] outBytes - vector to hold decrypted password file data
86 *
87 * @return error response
88 */
89 int readPasswdFileData(std::vector<uint8_t>& outBytes);
90 /** @brief Updates special password file by clearing the password entry
91 * for the user specified.
92 *
Richard Marian Thomaiyar42bed642018-09-21 12:28:57 +053093 * @param[in] userName - user name that has to be renamed / deleted
94 * @param[in] newUserName - new user name. If empty, userName will be
95 * deleted.
AppaRao Pulib29b5ab2018-05-17 10:28:48 +053096 *
97 * @return error response
98 */
Richard Marian Thomaiyar42bed642018-09-21 12:28:57 +053099 int updatePasswdSpecialFile(const std::string& userName,
100 const std::string& newUserName);
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530101 /** @brief encrypts or decrypt the data provided
102 *
103 * @param[in] doEncrypt - do encrypt if set to true, else do decrypt.
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530104 * @param[in] cipher - cipher to be used
105 * @param[in] key - pointer to the key
106 * @param[in] keyLen - Length of the key to be used
107 * @param[in] iv - pointer to initialization vector
108 * @param[in] ivLen - Length of the iv
109 * @param[in] inBytes - input data to be encrypted / decrypted
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530110 * @param[in] inBytesLen - input size to be encrypted / decrypted
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530111 * @param[in] mac - message authentication code - to figure out corruption
112 * @param[in] macLen - size of MAC
113 * @param[in] outBytes - ptr to store output bytes
114 * @param[in] outBytesLen - outbut data length.
115 *
116 * @return error response
117 */
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530118 int encryptDecryptData(bool doEncrypt, const EVP_CIPHER* cipher,
119 uint8_t* key, size_t keyLen, uint8_t* iv,
120 size_t ivLen, uint8_t* inBytes, size_t inBytesLen,
121 uint8_t* mac, size_t* macLen, uint8_t* outBytes,
122 size_t* outBytesLen);
123
124 /** @brief returns updated file time of passwd file entry.
125 *
126 * @return timestamp or -1 for error.
127 */
128 std::time_t getUpdatedFileTime();
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530129};
130
131} // namespace ipmi