File: dns_options.hpp

package info (click to toggle)
openvpn3-client 25%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,276 kB
  • sloc: cpp: 190,085; python: 7,218; ansic: 1,866; sh: 1,361; java: 402; lisp: 81; makefile: 17
file content (640 lines) | stat: -rw-r--r-- 18,761 bytes parent folder | download
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
//    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) 2022- OpenVPN Inc.
//
//    SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
//


#pragma once

#include <map>
#include <vector>
#include <algorithm>
#include <sstream>

#include <openvpn/common/number.hpp>
#include <openvpn/common/option_error.hpp>
#include <openvpn/common/jsonlib.hpp>
#include <openvpn/common/hostport.hpp>
#include <openvpn/addr/ip.hpp>

#ifdef HAVE_JSON
#ifndef OPENVPN_JSON
#include <json/json.h>
#endif
#include <openvpn/common/jsonhelper.hpp>
#endif

namespace openvpn {

/**
 * @class DnsAddress
 * @brief A name server address and optional port
 */
struct DnsAddress
{
    DnsAddress() = default;
    virtual ~DnsAddress() noexcept = default;

    /**
     *  Constructs the DnsAddress object by parsing the input string
     *  into separate "address" and "port" fields.  If "port" is missing,
     *  it will be set to 0.
     *
     *  Supported formats:
     *      192.168.0.1
     *      192.168.0.1:53
     *      [2001:db8:1234::1]
     *      [2001:db8:1234::1]:53
     */
    explicit DnsAddress(const std::string &address_input)
    {
        IP::Addr addr;
        std::string addr_str = address_input;
        port = 0;

        bool ipv6_bracket_encaps = address_input.length() > 2
                                   && address_input[0] == '['
                                   && address_input.rfind(']') != std::string::npos;

        auto first_colon_pos = address_input.find(':');
        auto last_colon_pos = address_input.rfind(':');
        const bool v6_port_found = ipv6_bracket_encaps
                                   && last_colon_pos > address_input.rfind(']');

        const bool v4_port_found = first_colon_pos != std::string::npos
                                   && first_colon_pos == addr_str.rfind(':');

        if (v6_port_found || v4_port_found)
        {
            std::string port_str;
            if (!HostPort::split_host_port(address_input, addr_str, port_str, "", false, &port))
            {
                OPENVPN_THROW_EXCEPTION("Invalid address:port format '" << address_input << "'");
            }

            // If it was an IPv6 address, the bracket encapsulation has been removed
            // by HostPort::split_host_port()
            ipv6_bracket_encaps = false;
        }

        try
        {
            if (ipv6_bracket_encaps)
            {
                addr_str = addr_str.substr(1, addr_str.length() - 2);
            }
            addr = IP::Addr(addr_str, "dns-ip-address");
        }
        catch (const IP::ip_exception &)
        {
            OPENVPN_THROW_EXCEPTION("Invalid address '" << addr_str << "'");
        }
        address = addr.to_string();
    }


    /**
     * @brief Return string representation of the IP address
     *        and port stored in the DnsAddress object.
     *
     *        The output of this method is expected to be
     *        parsable by this class constructor.
     *
     * @return std::string  the string representation generated
     */
    std::string to_string() const
    {
        std::ostringstream os;

        IP::Addr addr(address);
        os << (addr.is_ipv6() && port ? "[" : "")
           << address
           << (addr.is_ipv6() && port ? "]" : "");

        if (port)
        {
            os << ":" << port;
        }
        return os.str();
    }

    void validate(const std::string &title) const
    {
        IP::Addr::validate(address, title);
    }

#ifdef HAVE_JSON
    Json::Value to_json() const
    {
        Json::Value root(Json::objectValue);
        root["address"] = Json::Value(address);
        if (port)
        {
            root["port"] = Json::Value(port);
        }
        return root;
    }

    void from_json(const Json::Value &root, const std::string &title)
    {
        json::assert_dict(root, title);
        json::to_uint_optional(root, port, "port", 0u, title);

        std::string addr_str;
        json::to_string(root, addr_str, "address", title);
        address = IP::Addr::from_string(addr_str).to_string();
    }
#endif

    std::string address;
    unsigned int port = 0;
};

/**
 * @class DnsDomain
 * @brief A DNS domain name
 */
struct DnsDomain
{
    DnsDomain() = default;
    virtual ~DnsDomain() noexcept = default;

    explicit DnsDomain(const std::string &domain_)
    {
        domain = domain_;
    }

    /**
     * @brief Return string representation of the DnsDomain object
     *
     * @return std::string  the string representation generated
     */
    std::string to_string() const
    {
        return domain;
    }

    void validate(const std::string &title) const
    {
        HostPort::validate_host(domain, title);
    }

#ifdef HAVE_JSON
    Json::Value to_json() const
    {
        return Json::Value(domain);
    }

    void from_json(const Json::Value &value, const std::string &title)
    {
        if (!value.isString())
        {
            throw json::json_parse("string " + title + " is of incorrect type");
        }
        domain = value.asString();
    }
#endif

    std::string domain;
};

/**
 * @class DnsServer
 * @brief DNS settings for a name server
 */
struct DnsServer
{
    enum class Security
    {
        Unset,   ///<  Undefined setting; default value when not set
        No,      ///<  Do not use DNSSEC
        Yes,     ///<  Enforce using DNSSEC
        Optional ///<  Try to use DNSSEC opportunistically.  If it fails, the DNS resolver may ignore DNSSEC
    };

    /**
     * @brief Return string representation of a given DnsServer::Security
     *        value
     *
     * @param  dnssec DnsServer::Security value
     * @return std::string  the string representation generated
     */
    static std::string dnssec_string(const Security dnssec)
    {
        switch (dnssec)
        {
        case Security::No:
            return "No";
        case Security::Yes:
            return "Yes";
        case Security::Optional:
            return "Optional";
        default:
            return "Unset";
        }
    }


    /**
     * @brief Return string representation of the dnssec
     *        value in this DnsServer object
     *
     * @return std::string  the string representation generated
     */
    std::string dnssec_string() const
    {
        return dnssec_string(dnssec);
    }


    /**
     *  Parse the --dns server n dnssec VALUE into the
     *  internal DnsServer::Security representation.  This
     *  method is typically called from the option parser.
     *
     *  @param dnssec_value   std::string containing the DNSSEC setting to use
     *  @throws openvpn::Exception on invalid values
     */
    void parse_dnssec_value(const std::string &dnssec_value)
    {
        if (dnssec_value == "yes")
        {
            dnssec = DnsServer::Security::Yes;
        }
        else if (dnssec_value == "no")
        {
            dnssec = DnsServer::Security::No;
        }
        else if (dnssec_value == "optional")
        {
            dnssec = DnsServer::Security::Optional;
        }
        else
        {
            OPENVPN_THROW_EXCEPTION("Invalid DNSSEC value '" << dnssec_value << "'");
        }
    }

    enum class Transport
    {
        Unset, ///<  Undefined setting; default value when not set
        Plain, ///<  Use the classic unencrypted DNS protocol
        HTTPS, ///<  Use DNS-over-HTTPS (DoH)
        TLS    ///<  Use DNS-over-TLS (DoT)
    };

    /**
     * @brief Return string representation of a given DnsServer::Transport
     *        value
     *
     * @param  transport DnsServer::Transport value
     * @return std::string  the string representation generated
     */
    static std::string transport_string(const Transport transport)
    {
        switch (transport)
        {
        case Transport::Plain:
            return "Plain";
        case Transport::HTTPS:
            return "HTTPS";
        case Transport::TLS:
            return "TLS";
        default:
            return "Unset";
        }
    }

    /**
     * @brief Return string representation of a given DnsServer::Transport
     *        value
     *
     * @return std::string  the string representation generated
     */
    std::string transport_string() const
    {
        return transport_string(transport);
    }

    /**
     *  Parse the --dns server n transport VALUE into the
     *  internal DnsServer::Transport representation.  This
     *  method is typically called from the option parser.
     *
     *  @param transport_value   std::string containing the DNS transport setting to use
     *  @throws openvpn::Exception on invalid values
     */
    void parse_transport_value(const std::string &transport_value)
    {
        if (transport_value == "plain")
        {
            transport = DnsServer::Transport::Plain;
        }
        else if (transport_value == "DoH")
        {
            transport = DnsServer::Transport::HTTPS;
        }
        else if (transport_value == "DoT")
        {
            transport = DnsServer::Transport::TLS;
        }
        else
        {
            OPENVPN_THROW_EXCEPTION("Invalid transport value '" << transport_value << "'");
        }
    }

    DnsServer() = default;
    virtual ~DnsServer() noexcept = default;

#ifdef HAVE_JSON
    /**
     *  Instantiate a new DnsServer object with information from a JSON blob,
     *  typically exported using the DnsServer::to_json() method
     *
     *  @param root   The root Json::Value object to import
     *  @param title  std::string with details used for error logging
     */
    explicit DnsServer(const Json::Value &root, const std::string &title = "")
    {
        from_json(root, title);
    }
#endif

    /**
     *  Generate a human readable representation of the configured
     *  DnsServer variables
     *
     * @return std::string  the string representation generated
     */
    std::string to_string(const char *prefix = "") const
    {
        std::ostringstream os;
        os << prefix << "Addresses:\n";
        for (const auto &address : addresses)
        {
            os << prefix << "  " << address.to_string() << '\n';
        }
        if (!domains.empty())
        {
            os << prefix << "Domains:\n";
            for (const auto &domain : domains)
            {
                os << prefix << "  " << domain.to_string() << '\n';
            }
        }
        if (dnssec != Security::Unset)
        {
            os << prefix << "DNSSEC: " << dnssec_string(dnssec) << '\n';
        }
        if (transport != Transport::Unset)
        {
            os << prefix << "Transport: " << transport_string(transport) << '\n';
        }
        if (!sni.empty())
        {
            os << prefix << "SNI: " << sni << '\n';
        }
        return os.str();
    }

#ifdef HAVE_JSON
    /**
     *  Generate a JSON representation of the configured
     *  DnsServer variables.
     *
     *  The output of this function can be imported into
     *  another DnsServer object by passing the Json::Value
     *  to the DnsServer constructor or using the
     *  DnsServer::from_json() method.
     *
     * @return Json::Value of information this object carries
     */
    Json::Value to_json() const
    {
        Json::Value server(Json::objectValue);
        json::from_vector(server, addresses, "addresses");
        if (!domains.empty())
        {
            json::from_vector(server, domains, "domains");
        }
        if (dnssec != Security::Unset)
        {
            server["dnssec"] = Json::Value(dnssec_string(dnssec));
        }
        if (transport != Transport::Unset)
        {
            server["transport"] = Json::Value(transport_string(transport));
        }
        if (!sni.empty())
        {
            server["sni"] = Json::Value(sni);
        }
        return server;
    }

    /**
     *  Import a Json::Value, typically generated by DnsServer::to_json()
     *  which will reconfigure this object to carry the information from
     *  the JSON data.
     *
     *  @param root   The root Json::Value object to import
     *  @param title  std::string with details used for error logging
     */
    void from_json(const Json::Value &root, const std::string &title)
    {
        json::assert_dict(root, title);
        json::to_vector(root, addresses, "addresses", title);
        if (json::exists(root, "domains"))
        {
            json::to_vector(root, domains, "domains", title);
        }
        if (json::exists(root, "dnssec"))
        {
            std::string dnssec_str;
            json::to_string(root, dnssec_str, "dnssec", title);
            if (dnssec_str == "Optional")
            {
                dnssec = Security::Optional;
            }
            else if (dnssec_str == "Yes")
            {
                dnssec = Security::Yes;
            }
            else if (dnssec_str == "No")
            {
                dnssec = Security::No;
            }
            else
            {
                throw json::json_parse("dnssec value " + dnssec_str + "is unknown");
            }
        }
        if (json::exists(root, "transport"))
        {
            std::string transport_str;
            json::to_string(root, transport_str, "transport", title);
            if (transport_str == "Plain")
            {
                transport = Transport::Plain;
            }
            else if (transport_str == "HTTPS")
            {
                transport = Transport::HTTPS;
            }
            else if (transport_str == "TLS")
            {
                transport = Transport::TLS;
            }
            else
            {
                throw json::json_parse("transport value " + transport_str + "is unknown");
            }
        }
        json::to_string_optional(root, sni, "sni", "", title);
    }
#endif

    //! Parsed from --dns server n address ADDRESS[:PORT] [...] or --dhcp-option DNS/DNS6
    std::vector<DnsAddress> addresses;

    //! Parsed from --dns server n resolve-domains DOMAIN [...] or --dhcp-option DOMAIN/DOMAIN-SEARCH if use_search_as_split_domains is true
    std::vector<DnsDomain> domains;

    //! Parsed from --dns server n dnssec {yes,optional,no}
    Security dnssec = Security::Unset;

    //! Parsed from --dns server n transport {plain,DoT,DoH}
    Transport transport = Transport::Unset;

    //! Parsed from --dns server n sni
    std::string sni;
};

/**
 * @class DnsOptions
 * @brief All DNS options set with the --dns or --dhcp-option directive
 */
struct DnsOptions
{
    DnsOptions() = default;
    ~DnsOptions() noexcept = default;

#ifdef HAVE_JSON
    /**
     *  Instantiate a new DnsOptions object with information from a JSON blob,
     *  typically exported using the DnsOptions::to_json() method
     *
     *  @param root   The root Json::Value object to import
     *  @param title  std::string with details used for error logging
     */
    explicit DnsOptions(const Json::Value &root, const std::string &title = "")
    {
        from_json(root, title);
    }
#endif

    /**
     *  Generate a human readable representation of the configured
     *  DnsOptions variables
     *
     * @return std::string  the string representation generated
     */
    std::string to_string() const
    {
        std::ostringstream os;
        if (!servers.empty())
        {
            os << "DNS Servers:\n";
            for (const auto &[priority, server] : servers)
            {
                os << "  Priority: " << priority << '\n';
                os << server.to_string("  ");
            }
        }
        if (!search_domains.empty())
        {
            os << "DNS Search Domains:\n";
            for (const auto &domain : search_domains)
            {
                os << "  " << domain.to_string() << '\n';
            }
        }
        os << "Values from dhcp-options: " << (from_dhcp_options ? "true" : "false") << '\n';
        return os.str();
    }

#ifdef HAVE_JSON
    /**
     *  Generate a JSON representation of the configured
     *  DnsOptions variables.
     *
     *  The output of this function can be imported into
     *  another DnsOptions object by passing the Json::Value
     *  to the DnsOptions constructor or using the
     *  DnsOptions::from_json() method.
     *
     * @return Json::Value of information this object carries
     */
    Json::Value to_json() const
    {
        Json::Value root(Json::objectValue);
        Json::Value servers_json(Json::objectValue);
        for (const auto &[prio, server] : servers)
        {
            servers_json[std::to_string(prio)] = server.to_json();
        }
        root["servers"] = std::move(servers_json);
        json::from_vector(root, search_domains, "search_domains");
        root["from_dhcp_options"] = Json::Value(from_dhcp_options);
        return root;
    }

    /**
     *  Import a Json::Value, typically generated by DnsOptions::to_json()
     *  which will reconfigure this object to carry the information from
     *  the JSON data.
     *
     *  @param root   The root Json::Value object to import
     *  @param title  std::string with details used for error logging
     */
    void from_json(const Json::Value &root, const std::string &title)
    {
        json::assert_dict(root, title);
        json::assert_dict(root["servers"], title);
        for (const auto &prio : root["servers"].getMemberNames())
        {
            DnsServer server(root["servers"][prio], title);
            servers[std::stoi(prio)] = std::move(server);
        }
        json::to_vector(root, search_domains, "search_domains", title);
        json::to_bool(root, from_dhcp_options, "from_dhcp_options", title);
    }
#endif

    bool from_dhcp_options = false;        ///< Set to true if the DNS options comes from --dhcp-option options
    std::vector<DnsDomain> search_domains; ///< List of global DNS search domains to use
    std::map<int, DnsServer> servers;      ///< List of DNS servers to use, according to the list of priority

  protected:
    /**
     *  Instantiate a new DnsServer object for a given DNS server priority
     *  and add it to the DnsOptions server list.
     *
     *  @param priority   Priority value for the new DnsServer setting
     *  @return A new instantiated DnsServer object
     */
    DnsServer &get_server(const int priority)
    {
        auto it = servers.insert(std::make_pair(priority, DnsServer())).first;
        return (*it).second;
    }
};

} // namespace openvpn