Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2018 Intel Corporation |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | */ |
| 16 | #pragma once |
| 17 | |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 18 | #include "logging.hpp" |
Ninad Palsule | 3e72c20 | 2023-03-27 17:19:55 -0500 | [diff] [blame] | 19 | #include "sessions.hpp" |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 20 | |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 21 | #include <boost/beast/http/verb.hpp> |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 22 | #include <boost/container/flat_map.hpp> |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 23 | #include <boost/container/vector.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 24 | |
| 25 | #include <array> |
| 26 | #include <bitset> |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 27 | #include <cstddef> |
| 28 | #include <functional> |
| 29 | #include <initializer_list> |
| 30 | #include <string> |
| 31 | #include <string_view> |
| 32 | #include <utility> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 33 | #include <vector> |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 34 | |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 35 | // IWYU pragma: no_include <stddef.h> |
| 36 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 37 | namespace redfish |
| 38 | { |
| 39 | |
| 40 | enum class PrivilegeType |
| 41 | { |
| 42 | BASE, |
| 43 | OEM |
| 44 | }; |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 45 | |
Ed Tanous | a692779 | 2018-03-06 10:01:57 -0800 | [diff] [blame] | 46 | /** @brief A fixed array of compile time privileges */ |
Ed Tanous | ac25adb | 2024-04-13 11:57:35 -0700 | [diff] [blame] | 47 | constexpr std::array<std::string_view, 5> basePrivileges{ |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 48 | "Login", "ConfigureManager", "ConfigureComponents", "ConfigureSelf", |
| 49 | "ConfigureUsers"}; |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame] | 50 | |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 51 | constexpr const size_t basePrivilegeCount = basePrivileges.size(); |
Ed Tanous | a692779 | 2018-03-06 10:01:57 -0800 | [diff] [blame] | 52 | |
| 53 | /** @brief Max number of privileges per type */ |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 54 | constexpr const size_t maxPrivilegeCount = 32; |
Ed Tanous | a692779 | 2018-03-06 10:01:57 -0800 | [diff] [blame] | 55 | |
Ninad Palsule | 3e72c20 | 2023-03-27 17:19:55 -0500 | [diff] [blame] | 56 | /** |
| 57 | * @brief A vector of all privilege names and their indexes |
| 58 | * The privilege "OpenBMCHostConsole" is added to users who are members of the |
| 59 | * "hostconsole" user group. This privilege is required to access the host |
| 60 | * console. |
| 61 | */ |
Ed Tanous | ac25adb | 2024-04-13 11:57:35 -0700 | [diff] [blame] | 62 | constexpr std::array<std::string_view, maxPrivilegeCount> privilegeNames{ |
Ninad Palsule | 3e72c20 | 2023-03-27 17:19:55 -0500 | [diff] [blame] | 63 | "Login", "ConfigureManager", "ConfigureComponents", |
| 64 | "ConfigureSelf", "ConfigureUsers", "OpenBMCHostConsole"}; |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame] | 65 | |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 66 | /** |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 67 | * @brief Redfish privileges |
| 68 | * |
Joseph Reynolds | 900f949 | 2019-11-25 15:37:29 -0600 | [diff] [blame] | 69 | * This implements a set of Redfish privileges. These directly represent |
| 70 | * user privileges and help represent entity privileges. |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 71 | * |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 72 | * Each incoming Connection requires a comparison between privileges held |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 73 | * by the user issuing a request and the target entity's privileges. |
| 74 | * |
| 75 | * To ensure best runtime performance of this comparison, privileges |
| 76 | * are represented as bitsets. Each bit in the bitset corresponds to a |
| 77 | * unique privilege name. |
| 78 | * |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 79 | * A bit is set if the privilege is required (entity domain) or granted |
| 80 | * (user domain) and false otherwise. |
| 81 | * |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 82 | */ |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 83 | class Privileges |
| 84 | { |
| 85 | public: |
| 86 | /** |
| 87 | * @brief Constructs object without any privileges active |
| 88 | * |
| 89 | */ |
| 90 | Privileges() = default; |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame] | 91 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 92 | /** |
| 93 | * @brief Constructs object with given privileges active |
| 94 | * |
| 95 | * @param[in] privilegeList List of privileges to be activated |
| 96 | * |
| 97 | */ |
| 98 | Privileges(std::initializer_list<const char*> privilegeList) |
| 99 | { |
| 100 | for (const char* privilege : privilegeList) |
| 101 | { |
| 102 | if (!setSinglePrivilege(privilege)) |
| 103 | { |
Ed Tanous | ac25adb | 2024-04-13 11:57:35 -0700 | [diff] [blame] | 104 | BMCWEB_LOG_CRITICAL("Unable to set privilege {} in constructor", |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 105 | privilege); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 106 | } |
| 107 | } |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 108 | } |
| 109 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 110 | /** |
| 111 | * @brief Sets given privilege in the bitset |
| 112 | * |
| 113 | * @param[in] privilege Privilege to be set |
| 114 | * |
| 115 | * @return None |
| 116 | * |
| 117 | */ |
Ed Tanous | 26ccae3 | 2023-02-16 10:28:44 -0800 | [diff] [blame] | 118 | bool setSinglePrivilege(std::string_view privilege) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 119 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 120 | for (size_t searchIndex = 0; searchIndex < privilegeNames.size(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 121 | searchIndex++) |
| 122 | { |
| 123 | if (privilege == privilegeNames[searchIndex]) |
| 124 | { |
| 125 | privilegeBitset.set(searchIndex); |
| 126 | return true; |
| 127 | } |
| 128 | } |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 129 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 130 | return false; |
Ed Tanous | a692779 | 2018-03-06 10:01:57 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 133 | /** |
Joseph Reynolds | 900f949 | 2019-11-25 15:37:29 -0600 | [diff] [blame] | 134 | * @brief Resets the given privilege in the bitset |
| 135 | * |
| 136 | * @param[in] privilege Privilege to be reset |
| 137 | * |
| 138 | * @return None |
| 139 | * |
| 140 | */ |
| 141 | bool resetSinglePrivilege(const char* privilege) |
| 142 | { |
| 143 | for (size_t searchIndex = 0; searchIndex < privilegeNames.size(); |
| 144 | searchIndex++) |
| 145 | { |
| 146 | if (privilege == privilegeNames[searchIndex]) |
| 147 | { |
| 148 | privilegeBitset.reset(searchIndex); |
| 149 | return true; |
| 150 | } |
| 151 | } |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | /** |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 156 | * @brief Retrieves names of all active privileges for a given type |
| 157 | * |
| 158 | * @param[in] type Base or OEM |
| 159 | * |
| 160 | * @return Vector of active privileges. Pointers are valid until |
| 161 | * the setSinglePrivilege is called, or the Privilege structure is destroyed |
| 162 | * |
| 163 | */ |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 164 | std::vector<std::string> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 165 | getActivePrivilegeNames(const PrivilegeType type) const |
| 166 | { |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 167 | std::vector<std::string> activePrivileges; |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 168 | |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 169 | size_t searchIndex = 0; |
| 170 | size_t endIndex = basePrivilegeCount; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 171 | if (type == PrivilegeType::OEM) |
| 172 | { |
Joseph Reynolds | 8770446 | 2021-08-24 14:42:39 -0500 | [diff] [blame] | 173 | searchIndex = basePrivilegeCount; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 174 | endIndex = privilegeNames.size(); |
| 175 | } |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 176 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 177 | for (; searchIndex < endIndex; searchIndex++) |
| 178 | { |
| 179 | if (privilegeBitset.test(searchIndex)) |
| 180 | { |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 181 | activePrivileges.emplace_back(privilegeNames[searchIndex]); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
| 185 | return activePrivileges; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * @brief Determines if this Privilege set is a superset of the given |
| 190 | * privilege set |
| 191 | * |
| 192 | * @param[in] privilege Privilege to be checked |
| 193 | * |
| 194 | * @return None |
| 195 | * |
| 196 | */ |
| 197 | bool isSupersetOf(const Privileges& p) const |
| 198 | { |
| 199 | return (privilegeBitset & p.privilegeBitset) == p.privilegeBitset; |
| 200 | } |
| 201 | |
Joseph Reynolds | 3bf4e63 | 2020-02-06 14:44:32 -0600 | [diff] [blame] | 202 | /** |
| 203 | * @brief Returns the intersection of two Privilege sets. |
| 204 | * |
| 205 | * @param[in] privilege Privilege set to intersect with. |
| 206 | * |
| 207 | * @return The new Privilege set. |
| 208 | * |
| 209 | */ |
| 210 | Privileges intersection(const Privileges& p) const |
| 211 | { |
| 212 | return Privileges{privilegeBitset & p.privilegeBitset}; |
| 213 | } |
| 214 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 215 | private: |
Ed Tanous | 4e23a44 | 2022-06-06 09:57:26 -0700 | [diff] [blame] | 216 | explicit Privileges(const std::bitset<maxPrivilegeCount>& p) : |
| 217 | privilegeBitset{p} |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 218 | {} |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 219 | std::bitset<maxPrivilegeCount> privilegeBitset = 0; |
Borawski.Lukasz | 86e1b66 | 2018-01-19 14:22:14 +0100 | [diff] [blame] | 220 | }; |
| 221 | |
Ninad Palsule | 3e72c20 | 2023-03-27 17:19:55 -0500 | [diff] [blame] | 222 | inline Privileges getUserPrivileges(const persistent_data::UserSession& session) |
Ratan Gupta | 6f35956 | 2019-04-03 10:39:08 +0530 | [diff] [blame] | 223 | { |
Ninad Palsule | 3e72c20 | 2023-03-27 17:19:55 -0500 | [diff] [blame] | 224 | // default to no access |
| 225 | Privileges privs; |
| 226 | |
| 227 | // Check if user is member of hostconsole group |
| 228 | for (const auto& userGroup : session.userGroups) |
Ratan Gupta | 6f35956 | 2019-04-03 10:39:08 +0530 | [diff] [blame] | 229 | { |
Ninad Palsule | 3e72c20 | 2023-03-27 17:19:55 -0500 | [diff] [blame] | 230 | if (userGroup == "hostconsole") |
| 231 | { |
| 232 | // Redfish privilege : host console access |
| 233 | privs.setSinglePrivilege("OpenBMCHostConsole"); |
| 234 | break; |
| 235 | } |
Ratan Gupta | 6f35956 | 2019-04-03 10:39:08 +0530 | [diff] [blame] | 236 | } |
Ninad Palsule | 3e72c20 | 2023-03-27 17:19:55 -0500 | [diff] [blame] | 237 | |
| 238 | if (session.userRole == "priv-admin") |
| 239 | { |
| 240 | // Redfish privilege : Administrator |
| 241 | privs.setSinglePrivilege("Login"); |
| 242 | privs.setSinglePrivilege("ConfigureManager"); |
| 243 | privs.setSinglePrivilege("ConfigureSelf"); |
| 244 | privs.setSinglePrivilege("ConfigureUsers"); |
| 245 | privs.setSinglePrivilege("ConfigureComponents"); |
| 246 | } |
| 247 | else if (session.userRole == "priv-operator") |
Ratan Gupta | 6f35956 | 2019-04-03 10:39:08 +0530 | [diff] [blame] | 248 | { |
| 249 | // Redfish privilege : Operator |
Ninad Palsule | 3e72c20 | 2023-03-27 17:19:55 -0500 | [diff] [blame] | 250 | privs.setSinglePrivilege("Login"); |
| 251 | privs.setSinglePrivilege("ConfigureSelf"); |
| 252 | privs.setSinglePrivilege("ConfigureComponents"); |
Ratan Gupta | 6f35956 | 2019-04-03 10:39:08 +0530 | [diff] [blame] | 253 | } |
Ninad Palsule | 3e72c20 | 2023-03-27 17:19:55 -0500 | [diff] [blame] | 254 | else if (session.userRole == "priv-user") |
Ratan Gupta | 6f35956 | 2019-04-03 10:39:08 +0530 | [diff] [blame] | 255 | { |
| 256 | // Redfish privilege : Readonly |
Ninad Palsule | 3e72c20 | 2023-03-27 17:19:55 -0500 | [diff] [blame] | 257 | privs.setSinglePrivilege("Login"); |
| 258 | privs.setSinglePrivilege("ConfigureSelf"); |
Ratan Gupta | 6f35956 | 2019-04-03 10:39:08 +0530 | [diff] [blame] | 259 | } |
Ninad Palsule | 3e72c20 | 2023-03-27 17:19:55 -0500 | [diff] [blame] | 260 | |
| 261 | return privs; |
Ratan Gupta | 6f35956 | 2019-04-03 10:39:08 +0530 | [diff] [blame] | 262 | } |
| 263 | |
Joseph Reynolds | 900f949 | 2019-11-25 15:37:29 -0600 | [diff] [blame] | 264 | /** |
| 265 | * @brief The OperationMap represents the privileges required for a |
| 266 | * single entity (URI). It maps from the allowable verbs to the |
| 267 | * privileges required to use that operation. |
| 268 | * |
| 269 | * This represents the Redfish "Privilege AND and OR syntax" as given |
| 270 | * in the spec and shown in the Privilege Registry. This does not |
| 271 | * implement any Redfish property overrides, subordinate overrides, or |
| 272 | * resource URI overrides. This does not implement the limitation of |
| 273 | * the ConfigureSelf privilege to operate only on your own account or |
| 274 | * session. |
| 275 | **/ |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 276 | using OperationMap = boost::container::flat_map<boost::beast::http::verb, |
| 277 | std::vector<Privileges>>; |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame] | 278 | |
Joseph Reynolds | 900f949 | 2019-11-25 15:37:29 -0600 | [diff] [blame] | 279 | /* @brief Checks if user is allowed to call an operation |
| 280 | * |
| 281 | * @param[in] operationPrivilegesRequired Privileges required |
| 282 | * @param[in] userPrivileges Privileges the user has |
| 283 | * |
| 284 | * @return True if operation is allowed, false otherwise |
| 285 | */ |
| 286 | inline bool isOperationAllowedWithPrivileges( |
| 287 | const std::vector<Privileges>& operationPrivilegesRequired, |
| 288 | const Privileges& userPrivileges) |
| 289 | { |
| 290 | // If there are no privileges assigned, there are no privileges required |
| 291 | if (operationPrivilegesRequired.empty()) |
| 292 | { |
| 293 | return true; |
| 294 | } |
Ed Tanous | 9eb808c | 2022-01-25 10:19:23 -0800 | [diff] [blame] | 295 | for (const auto& requiredPrivileges : operationPrivilegesRequired) |
Joseph Reynolds | 900f949 | 2019-11-25 15:37:29 -0600 | [diff] [blame] | 296 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 297 | BMCWEB_LOG_DEBUG("Checking operation privileges..."); |
Joseph Reynolds | 900f949 | 2019-11-25 15:37:29 -0600 | [diff] [blame] | 298 | if (userPrivileges.isSupersetOf(requiredPrivileges)) |
| 299 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 300 | BMCWEB_LOG_DEBUG("...success"); |
Joseph Reynolds | 900f949 | 2019-11-25 15:37:29 -0600 | [diff] [blame] | 301 | return true; |
| 302 | } |
| 303 | } |
| 304 | return false; |
| 305 | } |
| 306 | |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame] | 307 | /** |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 308 | * @brief Checks if given privileges allow to call an HTTP method |
| 309 | * |
| 310 | * @param[in] method HTTP method |
| 311 | * @param[in] user Privileges |
| 312 | * |
| 313 | * @return True if method allowed, false otherwise |
Borawski.Lukasz | 43a095a | 2018-02-19 15:39:01 +0100 | [diff] [blame] | 314 | * |
| 315 | */ |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 316 | inline bool isMethodAllowedWithPrivileges(const boost::beast::http::verb method, |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 317 | const OperationMap& operationMap, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 318 | const Privileges& userPrivileges) |
| 319 | { |
| 320 | const auto& it = operationMap.find(method); |
| 321 | if (it == operationMap.end()) |
| 322 | { |
| 323 | return false; |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 324 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 325 | |
Joseph Reynolds | 900f949 | 2019-11-25 15:37:29 -0600 | [diff] [blame] | 326 | return isOperationAllowedWithPrivileges(it->second, userPrivileges); |
Ed Tanous | 3ebd75f | 2018-03-05 18:20:01 -0800 | [diff] [blame] | 327 | } |
Borawski.Lukasz | aecb47a | 2018-01-25 12:14:14 +0100 | [diff] [blame] | 328 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 329 | } // namespace redfish |