blob: 338f2492e6652642840c0f2bc1eae9eee7d0564a [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
Vernon Mauery1e22a0f2021-07-30 13:36:54 -070019#include <ipmid/types.hpp>
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -050020
21#include <ctime>
Andrew Geisslerecc03422020-05-16 15:08:22 -050022#include <string>
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +053023#include <unordered_map>
AppaRao Pulib29b5ab2018-05-17 10:28:48 +053024#include <vector>
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +053025
26namespace ipmi
27{
28
29class PasswdMgr
30{
31 public:
32 ~PasswdMgr() = default;
33 PasswdMgr(const PasswdMgr&) = delete;
34 PasswdMgr& operator=(const PasswdMgr&) = delete;
35 PasswdMgr(PasswdMgr&&) = delete;
36 PasswdMgr& operator=(PasswdMgr&&) = delete;
37
38 /** @brief Constructs user password list
39 *
40 */
41 PasswdMgr();
42
43 /** @brief Get password for the user
44 *
45 * @param[in] userName - user name
46 *
47 * @return password string. will return empty string, if unable to locate
48 * the user
49 */
Vernon Mauery1e22a0f2021-07-30 13:36:54 -070050 SecureString getPasswdByUserName(const std::string& userName);
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +053051
Richard Marian Thomaiyar42bed642018-09-21 12:28:57 +053052 /** @brief Update / clear username and password entry for the specified
53 * user
AppaRao Pulib29b5ab2018-05-17 10:28:48 +053054 *
Richard Marian Thomaiyar42bed642018-09-21 12:28:57 +053055 * @param[in] userName - user name that has to be renamed / deleted
56 * @param[in] newUserName - new user name. If empty, userName will be
57 * deleted.
AppaRao Pulib29b5ab2018-05-17 10:28:48 +053058 *
59 * @return error response
60 */
Richard Marian Thomaiyar42bed642018-09-21 12:28:57 +053061 int updateUserEntry(const std::string& userName,
62 const std::string& newUserName);
AppaRao Pulib29b5ab2018-05-17 10:28:48 +053063
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +053064 private:
65 using UserName = std::string;
Vernon Mauery1e22a0f2021-07-30 13:36:54 -070066 using Password = SecureString;
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +053067 std::unordered_map<UserName, Password> passwdMapList;
68 std::time_t fileLastUpdatedTime;
Richard Marian Thomaiyar6ba8d312020-04-10 23:52:50 +053069
70 /** @brief restrict file permission
71 *
72 */
73 void restrictFilesPermission(void);
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +053074 /** @brief check timestamp and reload password map if required
75 *
76 */
77 void checkAndReload(void);
78 /** @brief initializes passwdMapList by reading the encrypted file
79 *
80 * Initializes the passwordMapList members after decrypting the
81 * password file. passwordMapList will be used further in IPMI
82 * authentication.
83 */
84 void initPasswordMap(void);
AppaRao Pulib29b5ab2018-05-17 10:28:48 +053085
86 /** @brief Function to read the encrypted password file data
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +053087 *
AppaRao Pulib29b5ab2018-05-17 10:28:48 +053088 * @param[out] outBytes - vector to hold decrypted password file data
89 *
90 * @return error response
91 */
Vernon Mauery1e22a0f2021-07-30 13:36:54 -070092 int readPasswdFileData(SecureString& outBytes);
AppaRao Pulib29b5ab2018-05-17 10:28:48 +053093 /** @brief Updates special password file by clearing the password entry
94 * for the user specified.
95 *
Richard Marian Thomaiyar42bed642018-09-21 12:28:57 +053096 * @param[in] userName - user name that has to be renamed / deleted
97 * @param[in] newUserName - new user name. If empty, userName will be
98 * deleted.
AppaRao Pulib29b5ab2018-05-17 10:28:48 +053099 *
100 * @return error response
101 */
Richard Marian Thomaiyar42bed642018-09-21 12:28:57 +0530102 int updatePasswdSpecialFile(const std::string& userName,
103 const std::string& newUserName);
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530104 /** @brief encrypts or decrypt the data provided
105 *
106 * @param[in] doEncrypt - do encrypt if set to true, else do decrypt.
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530107 * @param[in] cipher - cipher to be used
108 * @param[in] key - pointer to the key
109 * @param[in] keyLen - Length of the key to be used
110 * @param[in] iv - pointer to initialization vector
111 * @param[in] ivLen - Length of the iv
112 * @param[in] inBytes - input data to be encrypted / decrypted
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530113 * @param[in] inBytesLen - input size to be encrypted / decrypted
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530114 * @param[in] mac - message authentication code - to figure out corruption
115 * @param[in] macLen - size of MAC
116 * @param[in] outBytes - ptr to store output bytes
117 * @param[in] outBytesLen - outbut data length.
118 *
119 * @return error response
120 */
AppaRao Pulib29b5ab2018-05-17 10:28:48 +0530121 int encryptDecryptData(bool doEncrypt, const EVP_CIPHER* cipher,
122 uint8_t* key, size_t keyLen, uint8_t* iv,
123 size_t ivLen, uint8_t* inBytes, size_t inBytesLen,
124 uint8_t* mac, size_t* macLen, uint8_t* outBytes,
125 size_t* outBytesLen);
126
127 /** @brief returns updated file time of passwd file entry.
128 *
129 * @return timestamp or -1 for error.
130 */
131 std::time_t getUpdatedFileTime();
Richard Marian Thomaiyar4654d992018-04-19 05:38:37 +0530132};
133
134} // namespace ipmi