]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/doh.hh
Merge pull request #8388 from rgacogne/dnsdist-doh-rotation-key-clear
[thirdparty/pdns.git] / pdns / doh.hh
1 #pragma once
2 #include "iputils.hh"
3 #include "libssl.hh"
4
5 struct DOHServerConfig;
6
7 class DOHResponseMapEntry
8 {
9 public:
10 DOHResponseMapEntry(const std::string& regex, uint16_t status, const std::string& content, const boost::optional<std::vector<std::pair<std::string, std::string>>>& headers): d_regex(regex), d_customHeaders(headers), d_content(content), d_status(status)
11 {
12 }
13
14 bool matches(const std::string& path) const
15 {
16 return d_regex.match(path);
17 }
18
19 uint16_t getStatusCode() const
20 {
21 return d_status;
22 }
23
24 const std::string& getContent() const
25 {
26 return d_content;
27 }
28
29 const boost::optional<std::vector<std::pair<std::string, std::string>>>& getHeaders() const
30 {
31 return d_customHeaders;
32 }
33
34 private:
35 Regex d_regex;
36 boost::optional<std::vector<std::pair<std::string, std::string>>> d_customHeaders;
37 std::string d_content;
38 uint16_t d_status;
39 };
40
41 struct DOHFrontend
42 {
43 DOHFrontend()
44 {
45 d_rotatingTicketsKey.clear();
46 }
47
48 std::shared_ptr<DOHServerConfig> d_dsc{nullptr};
49 std::vector<std::pair<std::string, std::string>> d_certKeyPairs;
50 std::vector<std::string> d_ocspFiles;
51 std::vector<std::shared_ptr<DOHResponseMapEntry>> d_responsesMap;
52 std::string d_ciphers;
53 std::string d_ciphers13;
54 std::string d_serverTokens{"h2o/dnsdist"};
55 LibsslTLSVersion d_minTLSVersion{LibsslTLSVersion::TLS10};
56 #ifdef HAVE_DNS_OVER_HTTPS
57 std::unique_ptr<OpenSSLTLSTicketKeysRing> d_ticketKeys{nullptr};
58 #endif
59 std::vector<std::pair<std::string, std::string>> d_customResponseHeaders;
60 ComboAddress d_local;
61
62 uint32_t d_idleTimeout{30}; // HTTP idle timeout in seconds
63 std::vector<std::string> d_urls;
64 std::string d_ticketKeyFile;
65
66 time_t d_ticketsKeyRotationDelay{43200};
67 size_t d_maxStoredSessions{20480};
68 uint8_t d_numberOfTicketsKeys{5};
69 bool d_enableTickets{true};
70 bool d_preferServerCiphers{false};
71
72 std::atomic<uint64_t> d_httpconnects; // number of TCP/IP connections established
73 std::atomic<uint64_t> d_tls10queries; // valid DNS queries received via TLSv1.0
74 std::atomic<uint64_t> d_tls11queries; // valid DNS queries received via TLSv1.1
75 std::atomic<uint64_t> d_tls12queries; // valid DNS queries received via TLSv1.2
76 std::atomic<uint64_t> d_tls13queries; // valid DNS queries received via TLSv1.3
77 std::atomic<uint64_t> d_tlsUnknownqueries; // valid DNS queries received via unknown TLS version
78
79 std::atomic<uint64_t> d_getqueries; // valid DNS queries received via GET
80 std::atomic<uint64_t> d_postqueries; // valid DNS queries received via POST
81 std::atomic<uint64_t> d_badrequests; // request could not be converted to dns query
82 std::atomic<uint64_t> d_errorresponses; // dnsdist set 'error' on response
83 std::atomic<uint64_t> d_redirectresponses; // dnsdist set 'redirect' on response
84 std::atomic<uint64_t> d_validresponses; // valid responses sent out
85
86 struct HTTPVersionStats
87 {
88 std::atomic<uint64_t> d_nbQueries{0}; // valid DNS queries received
89 std::atomic<uint64_t> d_nb200Responses{0};
90 std::atomic<uint64_t> d_nb400Responses{0};
91 std::atomic<uint64_t> d_nb403Responses{0};
92 std::atomic<uint64_t> d_nb500Responses{0};
93 std::atomic<uint64_t> d_nb502Responses{0};
94 std::atomic<uint64_t> d_nbOtherResponses{0};
95 };
96
97 HTTPVersionStats d_http1Stats;
98 HTTPVersionStats d_http2Stats;
99
100
101 #ifndef HAVE_DNS_OVER_HTTPS
102 void setup()
103 {
104 }
105
106 void reloadCertificates()
107 {
108 }
109
110 void rotateTicketsKey(time_t now)
111 {
112 }
113
114 void loadTicketsKeys(const std::string& keyFile)
115 {
116 }
117
118 void handleTicketsKeyRotation()
119 {
120 }
121
122 #else
123 void setup();
124 void reloadCertificates();
125
126 void rotateTicketsKey(time_t now);
127 void loadTicketsKeys(const std::string& keyFile);
128 void handleTicketsKeyRotation();
129
130 #endif /* HAVE_DNS_OVER_HTTPS */
131
132 private:
133 time_t d_ticketsKeyNextRotation{0};
134 std::atomic_flag d_rotatingTicketsKey;
135 };
136
137 #ifndef HAVE_DNS_OVER_HTTPS
138 struct DOHUnit
139 {
140 };
141
142 #else /* HAVE_DNS_OVER_HTTPS */
143 #include <unordered_map>
144
145 struct st_h2o_req_t;
146
147 struct DOHUnit
148 {
149 std::string query;
150 std::string response;
151 ComboAddress remote;
152 ComboAddress dest;
153 st_h2o_req_t* req{nullptr};
154 DOHUnit** self{nullptr};
155 std::string contentType;
156 int rsock;
157 uint16_t qtype;
158 /* the status_code is set from
159 processDOHQuery() (which is executed in
160 the DOH client thread) so that the correct
161 response can be sent in on_dnsdist(),
162 after the DOHUnit has been passed back to
163 the main DoH thread.
164 */
165 uint16_t status_code{200};
166 bool ednsAdded{false};
167
168 std::string getHTTPPath() const;
169 std::string getHTTPHost() const;
170 std::string getHTTPScheme() const;
171 std::string getHTTPQueryString() const;
172 std::unordered_map<std::string, std::string> getHTTPHeaders() const;
173 void setHTTPResponse(uint16_t statusCode, const std::string& body, const std::string& contentType="");
174 };
175
176 #endif /* HAVE_DNS_OVER_HTTPS */
177
178 void handleDOHTimeout(DOHUnit* oldDU);