1*40e9b92eSEd Tanous // SPDX-License-Identifier: Apache-2.0 2*40e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright OpenBMC Authors 308bbe119SEd Tanous #pragma once 408bbe119SEd Tanous 508bbe119SEd Tanous #include "async_resp.hpp" 608bbe119SEd Tanous #include "http_request.hpp" 708bbe119SEd Tanous #include "privileges.hpp" 808bbe119SEd Tanous #include "verb.hpp" 908bbe119SEd Tanous 10003301a2SEd Tanous #include <boost/asio/ip/tcp.hpp> 11003301a2SEd Tanous #include <boost/asio/ssl/stream.hpp> 1208bbe119SEd Tanous 1308bbe119SEd Tanous #include <memory> 1408bbe119SEd Tanous #include <string> 1508bbe119SEd Tanous 1608bbe119SEd Tanous namespace crow 1708bbe119SEd Tanous { 1808bbe119SEd Tanous class BaseRule 1908bbe119SEd Tanous { 2008bbe119SEd Tanous public: BaseRule(const std::string & thisRule)2108bbe119SEd Tanous explicit BaseRule(const std::string& thisRule) : rule(thisRule) {} 2208bbe119SEd Tanous 2308bbe119SEd Tanous virtual ~BaseRule() = default; 2408bbe119SEd Tanous 2508bbe119SEd Tanous BaseRule(const BaseRule&) = delete; 2608bbe119SEd Tanous BaseRule(BaseRule&&) = delete; 2708bbe119SEd Tanous BaseRule& operator=(const BaseRule&) = delete; 2808bbe119SEd Tanous BaseRule& operator=(const BaseRule&&) = delete; 2908bbe119SEd Tanous 3008bbe119SEd Tanous virtual void validate() = 0; upgrade()3108bbe119SEd Tanous std::unique_ptr<BaseRule> upgrade() 3208bbe119SEd Tanous { 3308bbe119SEd Tanous if (ruleToUpgrade) 3408bbe119SEd Tanous { 3508bbe119SEd Tanous return std::move(ruleToUpgrade); 3608bbe119SEd Tanous } 3708bbe119SEd Tanous return {}; 3808bbe119SEd Tanous } 3908bbe119SEd Tanous 4008bbe119SEd Tanous virtual void handle(const Request& /*req*/, 4108bbe119SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>&, 4208bbe119SEd Tanous const std::vector<std::string>&) = 0; 4308bbe119SEd Tanous virtual void handleUpgrade(const Request &,const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,boost::asio::ip::tcp::socket &&)4408bbe119SEd Tanous handleUpgrade(const Request& /*req*/, 4508bbe119SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 4608bbe119SEd Tanous boost::asio::ip::tcp::socket&& /*adaptor*/) 4708bbe119SEd Tanous { 4808bbe119SEd Tanous asyncResp->res.result(boost::beast::http::status::not_found); 4908bbe119SEd Tanous } 508db83747SEd Tanous handleUpgrade(const Request &,const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,boost::asio::ssl::stream<boost::asio::ip::tcp::socket> &&)5108bbe119SEd Tanous virtual void handleUpgrade( 5208bbe119SEd Tanous const Request& /*req*/, 5308bbe119SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 54003301a2SEd Tanous boost::asio::ssl::stream<boost::asio::ip::tcp::socket>&& /*adaptor*/) 5508bbe119SEd Tanous { 5608bbe119SEd Tanous asyncResp->res.result(boost::beast::http::status::not_found); 5708bbe119SEd Tanous } 5808bbe119SEd Tanous getMethods() const5908bbe119SEd Tanous size_t getMethods() const 6008bbe119SEd Tanous { 6108bbe119SEd Tanous return methodsBitfield; 6208bbe119SEd Tanous } 6308bbe119SEd Tanous checkPrivileges(const redfish::Privileges & userPrivileges)6408bbe119SEd Tanous bool checkPrivileges(const redfish::Privileges& userPrivileges) 6508bbe119SEd Tanous { 6608bbe119SEd Tanous // If there are no privileges assigned, assume no privileges 6708bbe119SEd Tanous // required 6808bbe119SEd Tanous if (privilegesSet.empty()) 6908bbe119SEd Tanous { 7008bbe119SEd Tanous return true; 7108bbe119SEd Tanous } 7208bbe119SEd Tanous 7308bbe119SEd Tanous for (const redfish::Privileges& requiredPrivileges : privilegesSet) 7408bbe119SEd Tanous { 7508bbe119SEd Tanous if (userPrivileges.isSupersetOf(requiredPrivileges)) 7608bbe119SEd Tanous { 7708bbe119SEd Tanous return true; 7808bbe119SEd Tanous } 7908bbe119SEd Tanous } 8008bbe119SEd Tanous return false; 8108bbe119SEd Tanous } 8208bbe119SEd Tanous 8308bbe119SEd Tanous size_t methodsBitfield{1 << static_cast<size_t>(HttpVerb::Get)}; 8408bbe119SEd Tanous static_assert(std::numeric_limits<decltype(methodsBitfield)>::digits > 85a3b9eb98SEd Tanous static_cast<int>(HttpVerb::Max), 8608bbe119SEd Tanous "Not enough bits to store bitfield"); 8708bbe119SEd Tanous 88a3b9eb98SEd Tanous bool isNotFound = false; 89a3b9eb98SEd Tanous bool isMethodNotAllowed = false; 90a3b9eb98SEd Tanous bool isUpgrade = false; 91a3b9eb98SEd Tanous 9208bbe119SEd Tanous std::vector<redfish::Privileges> privilegesSet; 9308bbe119SEd Tanous 9408bbe119SEd Tanous std::string rule; 9508bbe119SEd Tanous 9608bbe119SEd Tanous std::unique_ptr<BaseRule> ruleToUpgrade; 9708bbe119SEd Tanous 9808bbe119SEd Tanous friend class Router; 9908bbe119SEd Tanous template <typename T> 10008bbe119SEd Tanous friend struct RuleParameterTraits; 10108bbe119SEd Tanous }; 10208bbe119SEd Tanous 10308bbe119SEd Tanous } // namespace crow 104