rippled
Role.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #include <ripple/rpc/Role.h>
21 #include <boost/beast/core/string.hpp>
22 #include <boost/beast/http/field.hpp>
23 #include <boost/beast/http/rfc7230.hpp>
24 #include <boost/utility/string_view.hpp>
25 #include <algorithm>
26 
27 namespace ripple {
28 
29 bool
31  Json::Value const& params) {
32 
33  assert(! port.admin_ip.empty ());
34  bool const passwordRequired = (!port.admin_user.empty() ||
35  !port.admin_password.empty());
36 
37  return !passwordRequired ||
38  ((params["admin_password"].isString() &&
39  params["admin_password"].asString() == port.admin_password) &&
40  (params["admin_user"].isString() &&
41  params["admin_user"].asString() == port.admin_user));
42 }
43 
44 bool
45 ipAllowed (beast::IP::Address const& remoteIp,
46  std::vector<beast::IP::Address> const& adminIp)
47 {
48  return std::find_if (adminIp.begin (), adminIp.end (),
49  [&remoteIp](beast::IP::Address const& ip) { return ip.is_unspecified () ||
50  ip == remoteIp; }) != adminIp.end ();
51 }
52 
53 bool
54 isAdmin (Port const& port, Json::Value const& params,
55  beast::IP::Address const& remoteIp)
56 {
57  return ipAllowed (remoteIp, port.admin_ip) &&
58  passwordUnrequiredOrSentCorrect (port, params);
59 }
60 
61 Role
62 requestRole (Role const& required, Port const& port,
63  Json::Value const& params, beast::IP::Endpoint const& remoteIp,
64  boost::string_view const& user)
65 {
66  if (isAdmin(port, params, remoteIp.address()))
67  return Role::ADMIN;
68 
69  if (required == Role::ADMIN)
70  return Role::FORBID;
71 
72  if (ipAllowed(remoteIp.address(), port.secure_gateway_ip))
73  {
74  if (user.size())
75  return Role::IDENTIFIED;
76  return Role::PROXY;
77  }
78 
79  return Role::GUEST;
80 }
81 
85 bool
86 isUnlimited (Role const& role)
87 {
88  return role == Role::ADMIN || role == Role::IDENTIFIED;
89 }
90 
91 bool
92 isUnlimited (Role const& required, Port const& port,
93  Json::Value const& params, beast::IP::Endpoint const& remoteIp,
94  std::string const& user)
95 {
96  return isUnlimited(requestRole(required, port, params, remoteIp, user));
97 }
98 
99 Resource::Consumer
101  beast::IP::Endpoint const& remoteAddress, Role const& role,
102  boost::string_view const& user, boost::string_view const& forwardedFor)
103 {
104  if (isUnlimited(role))
105  return manager.newUnlimitedEndpoint (remoteAddress);
106 
107  return manager.newInboundEndpoint(remoteAddress, role == Role::PROXY,
108  forwardedFor);
109 }
110 
111 boost::string_view
113 {
114  auto it = request.find(boost::beast::http::field::forwarded);
115  if (it != request.end())
116  {
117  auto ascii_tolower = [](char c) -> char
118  {
119  return ((static_cast<unsigned>(c) - 65U) < 26) ?
120  c + 'a' - 'A' : c;
121  };
122 
123  static std::string const forStr{"for="};
124  auto found = std::search(it->value().begin(), it->value().end(),
125  forStr.begin(), forStr.end(),
126  [&ascii_tolower](char c1, char c2)
127  {
128  return ascii_tolower(c1) == ascii_tolower(c2);
129  }
130  );
131 
132  if (found == it->value().end())
133  return {};
134 
135  found += forStr.size();
136  std::size_t const pos ([&]()
137  {
138  std::size_t const pos{boost::string_view(
139  found, it->value().end() - found).find(';')};
140  if (pos == boost::string_view::npos)
141  return it->value().size() - forStr.size();
142  return pos;
143  }());
144 
145  return *boost::beast::http::token_list(
146  boost::string_view(found, pos)).begin();
147  }
148 
149  it = request.find("X-Forwarded-For");
150  if (it != request.end())
151  {
152  return *boost::beast::http::token_list(it->value()).begin();
153  }
154 
155  return {};
156 }
157 
158 }
ripple::Resource::Manager::newInboundEndpoint
virtual Consumer newInboundEndpoint(beast::IP::Endpoint const &address)=0
Create a new endpoint keyed by inbound IP address or the forwarded IP if proxied.
ripple::isAdmin
bool isAdmin(Port const &port, Json::Value const &params, beast::IP::Address const &remoteIp)
Definition: Role.cpp:54
ripple::Port::admin_ip
std::vector< beast::IP::Address > admin_ip
Definition: Port.h:46
std::string
STL class.
Json::Value::isString
bool isString() const
Definition: json_value.cpp:1049
ripple::http_request_type
boost::beast::http::request< boost::beast::http::dynamic_body > http_request_type
Definition: Handoff.h:31
ripple::passwordUnrequiredOrSentCorrect
bool passwordUnrequiredOrSentCorrect(Port const &port, Json::Value const &params)
Definition: Role.cpp:30
std::vector< beast::IP::Address >
std::find_if
T find_if(T... args)
ripple::requestInboundEndpoint
Resource::Consumer requestInboundEndpoint(Resource::Manager &manager, beast::IP::Endpoint const &remoteAddress, Role const &role, boost::string_view const &user, boost::string_view const &forwardedFor)
Definition: Role.cpp:100
beast::IP::Endpoint::address
Address const & address() const
Returns the address portion of this endpoint.
Definition: IPEndpoint.h:67
std::search
T search(T... args)
ripple::Role::IDENTIFIED
@ IDENTIFIED
ripple::Port::admin_user
std::string admin_user
Definition: Port.h:50
algorithm
beast::IP::Address
boost::asio::ip::address Address
Definition: IPAddress.h:41
ripple::Role::ADMIN
@ ADMIN
ripple::forwardedFor
boost::string_view forwardedFor(http_request_type const &request)
Definition: Role.cpp:112
ripple::Role::PROXY
@ PROXY
ripple::Port::secure_gateway_ip
std::vector< beast::IP::Address > secure_gateway_ip
Definition: Port.h:47
ripple::Role::FORBID
@ FORBID
ripple::requestRole
Role requestRole(Role const &required, Port const &port, Json::Value const &params, beast::IP::Endpoint const &remoteIp, boost::string_view const &user)
Return the allowed privilege role.
Definition: Role.cpp:62
ripple::Port
Configuration information for a Server listening port.
Definition: Port.h:38
ripple::Port::admin_password
std::string admin_password
Definition: Port.h:51
ripple::isUnlimited
bool isUnlimited(Role const &role)
ADMIN and IDENTIFIED roles shall have unlimited resources.
Definition: Role.cpp:86
ripple::Resource::Manager
Tracks load and resource consumption.
Definition: ResourceManager.h:36
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::Role::GUEST
@ GUEST
ripple::Resource::Manager::newUnlimitedEndpoint
virtual Consumer newUnlimitedEndpoint(beast::IP::Endpoint const &address)=0
Create a new unlimited endpoint keyed by forwarded IP.
std::vector::begin
T begin(T... args)
std::vector::empty
T empty(T... args)
ripple::ipAllowed
bool ipAllowed(beast::IP::Address const &remoteIp, std::vector< beast::IP::Address > const &adminIp)
True if remoteIp is in any of adminIp.
Definition: Role.cpp:45
std::size_t
beast::IP::Endpoint
A version-independent IP address and port combination.
Definition: IPEndpoint.h:39
std::vector::end
T end(T... args)
ripple::Role
Role
Indicates the level of administrative permission to grant.
Definition: Role.h:40
Json::Value
Represents a JSON value.
Definition: json_value.h:141
Json::Value::asString
std::string asString() const
Returns the unquoted string value.
Definition: json_value.cpp:482