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