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 {
32  assert(!port.admin_ip.empty());
33  bool const passwordRequired =
34  (!port.admin_user.empty() || !port.admin_password.empty());
35 
36  return !passwordRequired ||
37  ((params["admin_password"].isString() &&
38  params["admin_password"].asString() == port.admin_password) &&
39  (params["admin_user"].isString() &&
40  params["admin_user"].asString() == port.admin_user));
41 }
42 
43 bool
45  beast::IP::Address const& remoteIp,
46  std::vector<beast::IP::Address> const& adminIp)
47 {
48  return std::find_if(
49  adminIp.begin(),
50  adminIp.end(),
51  [&remoteIp](beast::IP::Address const& ip) {
52  return ip.is_unspecified() || ip == remoteIp;
53  }) != adminIp.end();
54 }
55 
56 bool
58  Port const& port,
59  Json::Value const& params,
60  beast::IP::Address const& remoteIp)
61 {
62  return ipAllowed(remoteIp, port.admin_ip) &&
63  passwordUnrequiredOrSentCorrect(port, params);
64 }
65 
66 Role
68  Role const& required,
69  Port const& port,
70  Json::Value const& params,
71  beast::IP::Endpoint const& remoteIp,
72  boost::string_view const& user)
73 {
74  if (isAdmin(port, params, remoteIp.address()))
75  return Role::ADMIN;
76 
77  if (required == Role::ADMIN)
78  return Role::FORBID;
79 
80  if (ipAllowed(remoteIp.address(), port.secure_gateway_ip))
81  {
82  if (user.size())
83  return Role::IDENTIFIED;
84  return Role::PROXY;
85  }
86 
87  return Role::GUEST;
88 }
89 
93 bool
94 isUnlimited(Role const& role)
95 {
96  return role == Role::ADMIN || role == Role::IDENTIFIED;
97 }
98 
99 bool
101  Role const& required,
102  Port const& port,
103  Json::Value const& params,
104  beast::IP::Endpoint const& remoteIp,
105  std::string const& user)
106 {
107  return isUnlimited(requestRole(required, port, params, remoteIp, user));
108 }
109 
110 Resource::Consumer
112  Resource::Manager& manager,
113  beast::IP::Endpoint const& remoteAddress,
114  Role const& role,
115  boost::string_view const& user,
116  boost::string_view const& forwardedFor)
117 {
118  if (isUnlimited(role))
119  return manager.newUnlimitedEndpoint(remoteAddress);
120 
121  return manager.newInboundEndpoint(
122  remoteAddress, role == Role::PROXY, forwardedFor);
123 }
124 
125 boost::string_view
127 {
128  auto it = request.find(boost::beast::http::field::forwarded);
129  if (it != request.end())
130  {
131  auto ascii_tolower = [](char c) -> char {
132  return ((static_cast<unsigned>(c) - 65U) < 26) ? c + 'a' - 'A' : c;
133  };
134 
135  static std::string const forStr{"for="};
136  auto found = std::search(
137  it->value().begin(),
138  it->value().end(),
139  forStr.begin(),
140  forStr.end(),
141  [&ascii_tolower](char c1, char c2) {
142  return ascii_tolower(c1) == ascii_tolower(c2);
143  });
144 
145  if (found == it->value().end())
146  return {};
147 
148  found += forStr.size();
149  std::size_t const pos([&]() {
150  std::size_t const pos{
151  boost::string_view(found, it->value().end() - found).find(';')};
152  if (pos == boost::string_view::npos)
153  return it->value().size() - forStr.size();
154  return pos;
155  }());
156 
157  return *boost::beast::http::token_list(boost::string_view(found, pos))
158  .begin();
159  }
160 
161  it = request.find("X-Forwarded-For");
162  if (it != request.end())
163  {
164  return *boost::beast::http::token_list(it->value()).begin();
165  }
166 
167  return {};
168 }
169 
170 } // namespace ripple
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:57
ripple::Port::admin_ip
std::vector< beast::IP::Address > admin_ip
Definition: Port.h:52
std::string
STL class.
Json::Value::isString
bool isString() const
Definition: json_value.cpp:1009
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:111
beast::IP::Endpoint::address
Address const & address() const
Returns the address portion of this endpoint.
Definition: IPEndpoint.h:77
std::search
T search(T... args)
ripple::Role::IDENTIFIED
@ IDENTIFIED
ripple::Port::admin_user
std::string admin_user
Definition: Port.h:56
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:126
ripple::Role::PROXY
@ PROXY
ripple::Port::secure_gateway_ip
std::vector< beast::IP::Address > secure_gateway_ip
Definition: Port.h:53
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:67
ripple::Port
Configuration information for a Server listening port.
Definition: Port.h:44
ripple::Port::admin_password
std::string admin_password
Definition: Port.h:57
ripple::isUnlimited
bool isUnlimited(Role const &role)
ADMIN and IDENTIFIED roles shall have unlimited resources.
Definition: Role.cpp:94
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:44
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::http_request_type
boost::beast::http::request< boost::beast::http::dynamic_body > http_request_type
Definition: Handshake.h:47
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:145
Json::Value::asString
std::string asString() const
Returns the unquoted string value.
Definition: json_value.cpp:469