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