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