Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Ed Tanous | c94ad49 | 2019-10-10 15:39:33 -0700 | [diff] [blame] | 3 | #include <app.h> |
| 4 | #include <http_request.h> |
| 5 | #include <http_response.h> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 6 | |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 7 | #include <boost/container/flat_map.hpp> |
| 8 | #include <boost/uuid/uuid.hpp> |
| 9 | #include <boost/uuid/uuid_generators.hpp> |
| 10 | #include <boost/uuid/uuid_io.hpp> |
Ratan Gupta | 845cb7d | 2019-07-12 00:32:25 +0530 | [diff] [blame] | 11 | #include <filesystem> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 12 | #include <nlohmann/json.hpp> |
| 13 | #include <pam_authenticate.hpp> |
| 14 | #include <random> |
| 15 | #include <sessions.hpp> |
| 16 | #include <webassets.hpp> |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 17 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 18 | namespace crow |
| 19 | { |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 20 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 21 | namespace persistent_data |
| 22 | { |
Borawski.Lukasz | 9d8fd30 | 2018-01-05 14:56:09 +0100 | [diff] [blame] | 23 | |
Ratan Gupta | 845cb7d | 2019-07-12 00:32:25 +0530 | [diff] [blame] | 24 | namespace fs = std::filesystem; |
| 25 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 26 | class Middleware |
| 27 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 28 | uint64_t jsonRevision = 1; |
Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 29 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 30 | public: |
Ratan Gupta | 845cb7d | 2019-07-12 00:32:25 +0530 | [diff] [blame] | 31 | // todo(ed) should read this from a fixed location somewhere, not CWD |
| 32 | static constexpr const char* filename = "bmcweb_persistent_data.json"; |
| 33 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 34 | struct Context |
| 35 | { |
| 36 | }; |
Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 37 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 38 | Middleware() |
| 39 | { |
| 40 | readData(); |
Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 41 | } |
Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 42 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 43 | ~Middleware() |
| 44 | { |
| 45 | if (persistent_data::SessionStore::getInstance().needsWrite()) |
| 46 | { |
| 47 | writeData(); |
Kowalski, Kamil | 5cef0f7 | 2018-02-15 15:26:51 +0100 | [diff] [blame] | 48 | } |
Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 49 | } |
Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 50 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 51 | void beforeHandle(crow::Request& req, Response& res, Context& ctx) |
| 52 | { |
Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 53 | } |
Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 54 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 55 | void afterHandle(Request& req, Response& res, Context& ctx) |
| 56 | { |
| 57 | } |
Ed Tanous | c963aa4 | 2017-10-27 16:00:19 -0700 | [diff] [blame] | 58 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 59 | // TODO(ed) this should really use protobuf, or some other serialization |
| 60 | // library, but adding another dependency is somewhat outside the scope of |
| 61 | // this application for the moment |
| 62 | void readData() |
| 63 | { |
| 64 | std::ifstream persistentFile(filename); |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 65 | uint64_t fileRevision = 0; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 66 | if (persistentFile.is_open()) |
| 67 | { |
| 68 | // call with exceptions disabled |
| 69 | auto data = nlohmann::json::parse(persistentFile, nullptr, false); |
| 70 | if (data.is_discarded()) |
| 71 | { |
| 72 | BMCWEB_LOG_ERROR |
| 73 | << "Error parsing persistent data in json file."; |
| 74 | } |
| 75 | else |
| 76 | { |
| 77 | for (const auto& item : data.items()) |
| 78 | { |
| 79 | if (item.key() == "revision") |
| 80 | { |
| 81 | fileRevision = 0; |
| 82 | |
| 83 | const uint64_t* uintPtr = |
| 84 | item.value().get_ptr<const uint64_t*>(); |
| 85 | if (uintPtr == nullptr) |
| 86 | { |
| 87 | BMCWEB_LOG_ERROR << "Failed to read revision flag"; |
| 88 | } |
| 89 | else |
| 90 | { |
| 91 | fileRevision = *uintPtr; |
| 92 | } |
| 93 | } |
| 94 | else if (item.key() == "system_uuid") |
| 95 | { |
| 96 | const std::string* jSystemUuid = |
| 97 | item.value().get_ptr<const std::string*>(); |
| 98 | if (jSystemUuid != nullptr) |
| 99 | { |
| 100 | systemUuid = *jSystemUuid; |
| 101 | } |
| 102 | } |
Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 103 | else if (item.key() == "auth_config") |
| 104 | { |
| 105 | SessionStore::getInstance() |
| 106 | .getAuthMethodsConfig() |
| 107 | .fromJson(item.value()); |
| 108 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 109 | else if (item.key() == "sessions") |
| 110 | { |
| 111 | for (const auto& elem : item.value()) |
| 112 | { |
| 113 | std::shared_ptr<UserSession> newSession = |
| 114 | UserSession::fromJson(elem); |
| 115 | |
| 116 | if (newSession == nullptr) |
| 117 | { |
| 118 | BMCWEB_LOG_ERROR << "Problem reading session " |
| 119 | "from persistent store"; |
| 120 | continue; |
| 121 | } |
| 122 | |
| 123 | BMCWEB_LOG_DEBUG |
| 124 | << "Restored session: " << newSession->csrfToken |
| 125 | << " " << newSession->uniqueId << " " |
| 126 | << newSession->sessionToken; |
| 127 | SessionStore::getInstance().authTokens.emplace( |
| 128 | newSession->sessionToken, newSession); |
| 129 | } |
| 130 | } |
| 131 | else |
| 132 | { |
| 133 | // Do nothing in the case of extra fields. We may have |
| 134 | // cases where fields are added in the future, and we |
| 135 | // want to at least attempt to gracefully support |
| 136 | // downgrades in that case, even if we don't officially |
| 137 | // support it |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | bool needWrite = false; |
| 143 | |
| 144 | if (systemUuid.empty()) |
| 145 | { |
| 146 | systemUuid = |
| 147 | boost::uuids::to_string(boost::uuids::random_generator()()); |
| 148 | needWrite = true; |
| 149 | } |
| 150 | if (fileRevision < jsonRevision) |
| 151 | { |
| 152 | needWrite = true; |
| 153 | } |
| 154 | // write revision changes or system uuid changes immediately |
| 155 | if (needWrite) |
| 156 | { |
| 157 | writeData(); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | void writeData() |
| 162 | { |
| 163 | std::ofstream persistentFile(filename); |
Ratan Gupta | 845cb7d | 2019-07-12 00:32:25 +0530 | [diff] [blame] | 164 | |
| 165 | // set the permission of the file to 640 |
| 166 | fs::perms permission = fs::perms::owner_read | fs::perms::owner_write | |
| 167 | fs::perms::group_read; |
| 168 | fs::permissions(filename, permission); |
| 169 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 170 | nlohmann::json data{ |
| 171 | {"sessions", SessionStore::getInstance().authTokens}, |
Zbigniew Kurzynski | 7815863 | 2019-11-05 12:57:37 +0100 | [diff] [blame] | 172 | {"auth_config", SessionStore::getInstance().getAuthMethodsConfig()}, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 173 | {"system_uuid", systemUuid}, |
| 174 | {"revision", jsonRevision}}; |
| 175 | persistentFile << data; |
| 176 | } |
| 177 | |
| 178 | std::string systemUuid{""}; |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 179 | }; |
| 180 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 181 | } // namespace persistent_data |
| 182 | } // namespace crow |