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 | b483830 | 2022-07-22 13:51:16 -0700 | [diff] [blame] | 44 | * @param[in] partNumber - part number for the storage device |
| 45 | * @param[in] serialNumber - serial number for the storage device |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 46 | * @param[in] cryptInterface - (optional) pointer to CryptsetupInterface |
| 47 | * object |
| 48 | * @param[in] fsInterface - (optional) pointer to FilesystemInterface |
| 49 | * object |
| 50 | */ |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 51 | EStoraged(sdbusplus::asio::object_server& server, |
John Wedig | 6c0d8ce | 2022-04-22 14:00:43 -0700 | [diff] [blame] | 52 | const std::string& configPath, const std::string& devPath, |
| 53 | const std::string& luksName, uint64_t size, uint8_t lifeTime, |
John Wedig | b483830 | 2022-07-22 13:51:16 -0700 | [diff] [blame] | 54 | const std::string& partNumber, const std::string& serialNumber, |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 55 | std::unique_ptr<CryptsetupInterface> cryptInterface = |
| 56 | std::make_unique<Cryptsetup>(), |
| 57 | std::unique_ptr<FilesystemInterface> fsInterface = |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 58 | std::make_unique<Filesystem>()); |
| 59 | |
| 60 | /** @brief Destructor for eStoraged. */ |
| 61 | ~EStoraged(); |
| 62 | |
| 63 | EStoraged& operator=(const EStoraged&) = delete; |
| 64 | EStoraged(const EStoraged&) = delete; |
| 65 | EStoraged(EStoraged&&) = default; |
John Wedig | 61cf426 | 2023-03-17 14:32:04 -0700 | [diff] [blame] | 66 | EStoraged& operator=(EStoraged&&) = delete; |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 67 | |
| 68 | /** @brief Format the LUKS encrypted device and create empty filesystem. |
| 69 | * |
| 70 | * @param[in] password - password to set for the LUKS device. |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 71 | * @param[in] type - filesystem type, e.g. ext4 |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 72 | */ |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 73 | void formatLuks(const std::vector<uint8_t>& password, |
| 74 | Volume::FilesystemType type); |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 75 | |
| 76 | /** @brief Erase the contents of the storage device. |
| 77 | * |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 78 | * @param[in] eraseType - type of erase operation. |
| 79 | */ |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 80 | void erase(Volume::EraseMethod eraseType); |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 81 | |
| 82 | /** @brief Unmount filesystem and lock the LUKS device. |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 83 | */ |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 84 | void lock(); |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 85 | |
| 86 | /** @brief Unlock device and mount the filesystem. |
| 87 | * |
| 88 | * @param[in] password - password for the LUKS device. |
| 89 | */ |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 90 | void unlock(std::vector<uint8_t> password); |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 91 | |
| 92 | /** @brief Change the password for the LUKS device. |
| 93 | * |
| 94 | * @param[in] oldPassword - old password for the LUKS device. |
| 95 | * @param[in] newPassword - new password for the LUKS device. |
| 96 | */ |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 97 | void changePassword(const std::vector<uint8_t>& oldPassword, |
| 98 | const std::vector<uint8_t>& newPassword); |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 99 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 100 | /** @brief Check if the LUKS device is currently locked. */ |
| 101 | bool isLocked() const; |
| 102 | |
| 103 | /** @brief Get the mount point for the filesystem on the LUKS device. */ |
| 104 | std::string_view getMountPoint() const; |
| 105 | |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 106 | /** @brief Get the path to the mapped crypt device. */ |
| 107 | std::string_view getCryptDevicePath() const; |
| 108 | |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 109 | private: |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 110 | /** @brief Full path of the device file, e.g. /dev/mmcblk0. */ |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 111 | std::string devPath; |
| 112 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 113 | /** @brief Name of the LUKS container. */ |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 114 | std::string containerName; |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 115 | |
| 116 | /** @brief Mount point for the filesystem. */ |
| 117 | std::string mountPoint; |
| 118 | |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 119 | /** @brief Indicates whether the LUKS device is currently locked. */ |
John Edward Broadbent | 6771c69 | 2022-06-22 19:49:27 -0700 | [diff] [blame] | 120 | bool lockedProperty{false}; |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 121 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 122 | /** @brief Pointer to cryptsetup interface object. |
| 123 | * @details This is used to mock out the cryptsetup functions. |
| 124 | */ |
| 125 | std::unique_ptr<CryptsetupInterface> cryptIface; |
| 126 | |
| 127 | /** @brief Pointer to filesystem interface object. |
| 128 | * @details This is used to mock out filesystem operations. |
| 129 | */ |
| 130 | std::unique_ptr<FilesystemInterface> fsIface; |
| 131 | |
John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 132 | /** @brief Path where the mapped crypt device gets created. */ |
| 133 | const std::string cryptDevicePath; |
| 134 | |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 135 | /** @brief D-Bus object server. */ |
| 136 | sdbusplus::asio::object_server& objectServer; |
| 137 | |
| 138 | /** @brief D-Bus interface for the logical volume. */ |
| 139 | std::shared_ptr<sdbusplus::asio::dbus_interface> volumeInterface; |
| 140 | |
| 141 | /** @brief D-Bus interface for the physical drive. */ |
| 142 | std::shared_ptr<sdbusplus::asio::dbus_interface> driveInterface; |
| 143 | |
John Edward Broadbent | 740e94b | 2022-06-10 19:42:30 -0700 | [diff] [blame] | 144 | /** @brief D-Bus interface for the location of the drive. */ |
John Edward Broadbent | 4979641 | 2022-06-22 18:31:52 -0700 | [diff] [blame] | 145 | std::shared_ptr<sdbusplus::asio::dbus_interface> embeddedLocationInterface; |
John Edward Broadbent | 740e94b | 2022-06-10 19:42:30 -0700 | [diff] [blame] | 146 | |
John Wedig | b483830 | 2022-07-22 13:51:16 -0700 | [diff] [blame] | 147 | /** @brief D-Bus interface for the asset information. */ |
| 148 | std::shared_ptr<sdbusplus::asio::dbus_interface> assetInterface; |
| 149 | |
John Wedig | 6c0d8ce | 2022-04-22 14:00:43 -0700 | [diff] [blame] | 150 | /** @brief Association between chassis and drive. */ |
| 151 | std::shared_ptr<sdbusplus::asio::dbus_interface> association; |
| 152 | |
John Edward Broadbent | 91c1ec1 | 2022-05-20 16:51:43 -0700 | [diff] [blame] | 153 | /** @brief Indicates whether the LUKS header is on the disk. */ |
John Edward Broadbent | 6771c69 | 2022-06-22 19:49:27 -0700 | [diff] [blame] | 154 | Drive::DriveEncryptionState encryptionStatus{ |
| 155 | Drive::DriveEncryptionState::Unknown}; |
John Edward Broadbent | 91c1ec1 | 2022-05-20 16:51:43 -0700 | [diff] [blame] | 156 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 157 | /** @brief Format LUKS encrypted device. |
| 158 | * |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 159 | * @param[in] password - password to set for the LUKS device. |
| 160 | */ |
John Edward Broadbent | b2c86be | 2022-04-15 11:45:53 -0700 | [diff] [blame] | 161 | void formatLuksDev(std::vector<uint8_t> password); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 162 | |
John Edward Broadbent | 91c1ec1 | 2022-05-20 16:51:43 -0700 | [diff] [blame] | 163 | /** @brief check the LUKS header, for devPath |
| 164 | * |
| 165 | * @returns a CryptHandle to the LUKS drive |
| 166 | */ |
| 167 | CryptHandle loadLuksHeader(); |
| 168 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 169 | /** @brief Unlock the device. |
| 170 | * |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 171 | * @param[in] password - password to activate the LUKS device. |
| 172 | */ |
John Edward Broadbent | 91c1ec1 | 2022-05-20 16:51:43 -0700 | [diff] [blame] | 173 | |
| 174 | Drive::DriveEncryptionState findEncryptionStatus(); |
| 175 | |
John Edward Broadbent | b2c86be | 2022-04-15 11:45:53 -0700 | [diff] [blame] | 176 | void activateLuksDev(std::vector<uint8_t> password); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 177 | |
| 178 | /** @brief Create the filesystem on the LUKS device. |
| 179 | * @details The LUKS device should already be activated, i.e. unlocked. |
| 180 | */ |
| 181 | void createFilesystem(); |
| 182 | |
| 183 | /** @brief Deactivate the LUKS device. |
| 184 | * @details The filesystem is assumed to be unmounted already. |
| 185 | */ |
| 186 | void deactivateLuksDev(); |
| 187 | |
| 188 | /** @brief Mount the filesystem. |
| 189 | * @details The filesystem should already exist and the LUKS device should |
| 190 | * be unlocked already. |
| 191 | */ |
| 192 | void mountFilesystem(); |
| 193 | |
| 194 | /** @brief Unmount the filesystem. */ |
| 195 | void unmountFilesystem(); |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 196 | }; |
| 197 | |
| 198 | } // namespace estoraged |