1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
// OpenVPN -- An application to securely tunnel IP networks
// over a single port, with support for SSL/TLS-based
// session authentication and key exchange,
// packet encryption, packet authentication, and
// packet compression.
//
// Copyright (C) 2012- OpenVPN Inc.
//
// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
//
#ifndef OPENVPN_CLIENT_OPTFILT_H
#define OPENVPN_CLIENT_OPTFILT_H
#include <vector>
#include <openvpn/common/options.hpp>
#include <openvpn/common/numeric_util.hpp>
#include <openvpn/common/string.hpp>
#include <openvpn/client/dns.hpp>
// Options filters, consumes the route-nopull and pull-filter client options
namespace openvpn {
class PushedOptionsFilter : public OptionList::FilterBase
{
public:
PushedOptionsFilter(const OptionList &opt)
: route_nopull_(opt.exists("route-nopull"))
{
if (!opt.exists("pull-filter"))
return;
for (auto i : opt.get_index("pull-filter"))
{
FilterAction action = None;
auto &o = opt[i];
o.exact_args(3);
o.touch();
const auto &action_str = o.get(1, -1);
if (action_str == "accept")
action = Accept;
else if (action_str == "ignore")
action = Ignore;
else if (action_str == "reject")
action = Reject;
else
throw option_error(ERR_INVALID_OPTION_VAL, "invalid pull-filter action: " + action_str);
Option match = OptionList::parse_option_from_line(o.get(2, -1), nullptr);
pull_filter_list_.push_back({action, match});
}
}
bool filter(const Option &opt) override
{
return filter_(opt) == Accept ? true : false;
}
private:
enum FilterAction
{
None,
Accept,
Ignore,
Reject
};
struct PullFilter
{
FilterAction action;
Option match;
};
FilterAction filter_(const Option &opt)
{
static_filter_(opt);
FilterAction action = pull_filter_(opt);
if (action == None)
{
if (route_nopull_)
action = route_nopull_filter_(opt);
else
action = Accept;
}
return action;
}
void static_filter_(const Option &o)
{
// Reject pushed DNS servers with priority < 0
if (o.size() >= 3
&& o.ref(0) == "dns"
&& o.ref(1) == "server"
&& DnsOptionsParser::parse_priority(o.ref(2)) < 0)
{
throw option_error(ERR_INVALID_CONFIG, o.escape(false));
}
}
// Return an action if a pull-filter directive matches the pushed option
FilterAction pull_filter_(const Option &pushed)
{
for (const auto &pull_filter : pull_filter_list_)
{
if (!pull_filter_matches_(pushed, pull_filter.match))
continue;
if (pull_filter.action != Accept)
{
OPENVPN_LOG((pull_filter.action == Ignore ? "Ignored" : "Rejected")
<< " due to pull-filter: "
<< pushed.render(Option::RENDER_TRUNC_64 | Option::RENDER_BRACKET));
if (pull_filter.action == Reject)
throw Option::RejectedException(pushed.escape(false));
}
return pull_filter.action;
}
return None;
}
bool pull_filter_matches_(const Option &pushed, const Option &match)
{
if (pushed.size() < match.size())
return false;
if (!is_safe_conversion<int>(match.size() - 1))
return false;
int i = static_cast<int>(match.size() - 1);
if (!string::starts_with(pushed.get(i, -1), match.get(i, -1)))
return false;
while (--i >= 0)
{
if (pushed.get(i, -1) != match.get(i, -1))
return false;
}
return true;
}
// return action if pushed option should be ignored due to route-nopull directive.
FilterAction route_nopull_filter_(const Option &opt)
{
FilterAction action = Accept;
if (opt.size() >= 1)
{
const std::string &directive = opt.ref(0);
if (directive.length() >= 1)
{
switch (directive[0])
{
case 'b':
if (directive == "block-ipv6")
{
action = Ignore;
}
break;
case 'c':
if (directive == "client-nat")
{
action = Ignore;
}
break;
case 'd':
if (directive == "dhcp-option"
|| directive == "dhcp-renew"
|| directive == "dhcp-pre-release" || directive == "dhcp-release")
{
action = Ignore;
}
break;
case 'i':
if (directive == "ip-win32")
{
action = Ignore;
}
break;
case 'r':
if (directive == "route"
|| directive == "route-ipv6"
|| directive == "route-metric"
|| directive == "redirect-gateway"
|| directive == "redirect-private"
|| directive == "register-dns"
|| directive == "route-delay"
|| directive == "route-method")
{
action = Ignore;
}
break;
case 't':
if (directive == "tap-sleep")
{
action = Ignore;
}
break;
}
if (action == Ignore)
{
OPENVPN_LOG("Ignored due to route-nopull: "
<< opt.render(Option::RENDER_TRUNC_64 | Option::RENDER_BRACKET));
}
}
}
return action;
}
bool route_nopull_;
std::vector<PullFilter> pull_filter_list_;
};
} // namespace openvpn
#endif
|