John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 3 | #include "cryptsetupInterface.hpp" |
| 4 | #include "filesystemInterface.hpp" |
John Edward Broadbent | 5d799bb | 2022-03-22 16:14:24 -0700 | [diff] [blame] | 5 | #include "util.hpp" |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 6 | |
| 7 | #include <libcryptsetup.h> |
| 8 | |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 9 | #include <sdbusplus/asio/object_server.hpp> |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 10 | #include <sdbusplus/bus.hpp> |
| 11 | #include <sdbusplus/exception.hpp> |
| 12 | #include <sdbusplus/server/object.hpp> |
John Edward Broadbent | e35e736 | 2022-03-22 16:14:24 -0700 | [diff] [blame] | 13 | #include <util.hpp> |
John Edward Broadbent | 86dfb24 | 2022-03-14 11:04:36 -0700 | [diff] [blame] | 14 | #include <xyz/openbmc_project/Inventory/Item/Drive/server.hpp> |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 15 | #include <xyz/openbmc_project/Inventory/Item/Volume/server.hpp> |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 16 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 17 | #include <filesystem> |
| 18 | #include <memory> |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 19 | #include <string> |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 20 | #include <string_view> |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 21 | #include <vector> |
| 22 | |
| 23 | namespace estoraged |
| 24 | { |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 25 | using estoraged::Cryptsetup; |
| 26 | using estoraged::Filesystem; |
John Edward Broadbent | 91c1ec1 | 2022-05-20 16:51:43 -0700 | [diff] [blame] | 27 | using sdbusplus::xyz::openbmc_project::Inventory::Item::server::Drive; |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 28 | using sdbusplus::xyz::openbmc_project::Inventory::Item::server::Volume; |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 29 | |
| 30 | /** @class eStoraged |
| 31 | * @brief eStoraged object to manage a LUKS encrypted storage device. |
| 32 | */ |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 33 | class EStoraged |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 34 | { |
| 35 | public: |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 36 | /** @brief Constructor for eStoraged |
| 37 | * |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 38 | * @param[in] server - sdbusplus asio object server |
John Wedig | 6c0d8ce | 2022-04-22 14:00:43 -0700 | [diff] [blame] | 39 | * @param[in] configPath - path of the config object from Entity Manager |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 40 | * @param[in] devPath - path to device file, e.g. /dev/mmcblk0 |
| 41 | * @param[in] luksName - name for the LUKS container |
John Edward Broadbent | 5d799bb | 2022-03-22 16:14:24 -0700 | [diff] [blame] | 42 | * @param[in] size - size of the drive in bytes |
| 43 | * @param[in] lifeTime - percent of lifetime remaining for a drive |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 44 | * @param[in] cryptInterface - (optional) pointer to CryptsetupInterface |
| 45 | * object |
| 46 | * @param[in] fsInterface - (optional) pointer to FilesystemInterface |
| 47 | * object |
| 48 | */ |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 49 | EStoraged(sdbusplus::asio::object_server& server, |
John Wedig | 6c0d8ce | 2022-04-22 14:00:43 -0700 | [diff] [blame] | 50 | const std::string& configPath, const std::string& devPath, |
| 51 | const std::string& luksName, uint64_t size, uint8_t lifeTime, |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 52 | std::unique_ptr<CryptsetupInterface> cryptInterface = |
| 53 | std::make_unique<Cryptsetup>(), |
| 54 | std::unique_ptr<FilesystemInterface> fsInterface = |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 55 | std::make_unique<Filesystem>()); |
| 56 | |
| 57 | /** @brief Destructor for eStoraged. */ |
| 58 | ~EStoraged(); |
| 59 | |
| 60 | EStoraged& operator=(const EStoraged&) = delete; |
| 61 | EStoraged(const EStoraged&) = delete; |
| 62 | EStoraged(EStoraged&&) = default; |
| 63 | EStoraged& operator=(EStoraged&&) = default; |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 64 | |
| 65 | /** @brief Format the LUKS encrypted device and create empty filesystem. |
| 66 | * |
| 67 | * @param[in] password - password to set for the LUKS device. |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 68 | * @param[in] type - filesystem type, e.g. ext4 |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 69 | */ |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 70 | void formatLuks(const std::vector<uint8_t>& password, |
| 71 | Volume::FilesystemType type); |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 72 | |
| 73 | /** @brief Erase the contents of the storage device. |
| 74 | * |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 75 | * @param[in] eraseType - type of erase operation. |
| 76 | */ |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 77 | void erase(Volume::EraseMethod eraseType); |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 78 | |
| 79 | /** @brief Unmount filesystem and lock the LUKS device. |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 80 | */ |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 81 | void lock(); |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 82 | |
| 83 | /** @brief Unlock device and mount the filesystem. |
| 84 | * |
| 85 | * @param[in] password - password for the LUKS device. |
| 86 | */ |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 87 | void unlock(std::vector<uint8_t> password); |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 88 | |
| 89 | /** @brief Change the password for the LUKS device. |
| 90 | * |
| 91 | * @param[in] oldPassword - old password for the LUKS device. |
| 92 | * @param[in] newPassword - new password for the LUKS device. |
| 93 | */ |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 94 | void changePassword(const std::vector<uint8_t>& oldPassword, |
| 95 | const std::vector<uint8_t>& newPassword); |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 96 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 97 | /** @brief Check if the LUKS device is currently locked. */ |
| 98 | bool isLocked() const; |
| 99 | |
| 100 | /** @brief Get the mount point for the filesystem on the LUKS device. */ |
| 101 | std::string_view getMountPoint() const; |
| 102 | |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 103 | private: |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 104 | /** @brief Full path of the device file, e.g. /dev/mmcblk0. */ |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 105 | std::string devPath; |
| 106 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 107 | /** @brief Name of the LUKS container. */ |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 108 | std::string containerName; |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 109 | |
| 110 | /** @brief Mount point for the filesystem. */ |
| 111 | std::string mountPoint; |
| 112 | |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 113 | /** @brief Indicates whether the LUKS device is currently locked. */ |
| 114 | bool lockedProperty; |
| 115 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 116 | /** @brief Pointer to cryptsetup interface object. |
| 117 | * @details This is used to mock out the cryptsetup functions. |
| 118 | */ |
| 119 | std::unique_ptr<CryptsetupInterface> cryptIface; |
| 120 | |
| 121 | /** @brief Pointer to filesystem interface object. |
| 122 | * @details This is used to mock out filesystem operations. |
| 123 | */ |
| 124 | std::unique_ptr<FilesystemInterface> fsIface; |
| 125 | |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 126 | /** @brief D-Bus object server. */ |
| 127 | sdbusplus::asio::object_server& objectServer; |
| 128 | |
| 129 | /** @brief D-Bus interface for the logical volume. */ |
| 130 | std::shared_ptr<sdbusplus::asio::dbus_interface> volumeInterface; |
| 131 | |
| 132 | /** @brief D-Bus interface for the physical drive. */ |
| 133 | std::shared_ptr<sdbusplus::asio::dbus_interface> driveInterface; |
| 134 | |
John Wedig | 6c0d8ce | 2022-04-22 14:00:43 -0700 | [diff] [blame] | 135 | /** @brief Association between chassis and drive. */ |
| 136 | std::shared_ptr<sdbusplus::asio::dbus_interface> association; |
| 137 | |
John Edward Broadbent | 91c1ec1 | 2022-05-20 16:51:43 -0700 | [diff] [blame] | 138 | /** @brief Indicates whether the LUKS header is on the disk. */ |
| 139 | Drive::DriveEncryptionState encryptionStatus; |
| 140 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 141 | /** @brief Format LUKS encrypted device. |
| 142 | * |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 143 | * @param[in] password - password to set for the LUKS device. |
| 144 | */ |
John Edward Broadbent | b2c86be | 2022-04-15 11:45:53 -0700 | [diff] [blame] | 145 | void formatLuksDev(std::vector<uint8_t> password); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 146 | |
John Edward Broadbent | 91c1ec1 | 2022-05-20 16:51:43 -0700 | [diff] [blame] | 147 | /** @brief check the LUKS header, for devPath |
| 148 | * |
| 149 | * @returns a CryptHandle to the LUKS drive |
| 150 | */ |
| 151 | CryptHandle loadLuksHeader(); |
| 152 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 153 | /** @brief Unlock the device. |
| 154 | * |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 155 | * @param[in] password - password to activate the LUKS device. |
| 156 | */ |
John Edward Broadbent | 91c1ec1 | 2022-05-20 16:51:43 -0700 | [diff] [blame] | 157 | |
| 158 | Drive::DriveEncryptionState findEncryptionStatus(); |
| 159 | |
John Edward Broadbent | b2c86be | 2022-04-15 11:45:53 -0700 | [diff] [blame] | 160 | void activateLuksDev(std::vector<uint8_t> password); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 161 | |
| 162 | /** @brief Create the filesystem on the LUKS device. |
| 163 | * @details The LUKS device should already be activated, i.e. unlocked. |
| 164 | */ |
| 165 | void createFilesystem(); |
| 166 | |
| 167 | /** @brief Deactivate the LUKS device. |
| 168 | * @details The filesystem is assumed to be unmounted already. |
| 169 | */ |
| 170 | void deactivateLuksDev(); |
| 171 | |
| 172 | /** @brief Mount the filesystem. |
| 173 | * @details The filesystem should already exist and the LUKS device should |
| 174 | * be unlocked already. |
| 175 | */ |
| 176 | void mountFilesystem(); |
| 177 | |
| 178 | /** @brief Unmount the filesystem. */ |
| 179 | void unmountFilesystem(); |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 180 | |
| 181 | /** @brief Set the locked property. |
| 182 | * |
| 183 | * @param[in] isLocked - indicates whether the LUKS device is locked. |
| 184 | */ |
| 185 | void locked(bool isLocked); |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | } // namespace estoraged |