]>
Commit | Line | Data |
---|---|---|
2d40d42b PL |
1 | /* |
2 | * This file is part of PowerDNS or dnsdist. | |
3 | * Copyright -- PowerDNS.COM B.V. and its contributors | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of version 2 of the GNU General Public License as | |
7 | * published by the Free Software Foundation. | |
8 | * | |
9 | * In addition, for the avoidance of any doubt, permission is granted to | |
10 | * link this program with OpenSSL and to (re)distribute the binaries | |
11 | * produced as the result of such linking. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
21 | */ | |
22 | ||
23 | #include <string> | |
24 | #include <vector> | |
25 | #include "dnsrecords.hh" | |
26 | #include "pdnsexception.hh" | |
27 | #include "misc.hh" | |
050e6877 | 28 | #include "secpoll.hh" |
2d40d42b PL |
29 | |
30 | bool isReleaseVersion(const std::string &version) { | |
31 | return std::count(version.begin(), version.end(), '.') == 2; | |
32 | } | |
33 | ||
050e6877 | 34 | static void setSecPollToUnknownOnOK(int &secPollStatus) { |
7dcdce8c | 35 | if (secPollStatus == 1) { // it was ok, now it is unknown |
a4fd2ec2 | 36 | secPollStatus = 0; |
7dcdce8c | 37 | } |
a4fd2ec2 PL |
38 | } |
39 | ||
2d40d42b PL |
40 | void processSecPoll(const int res, const std::vector<DNSRecord> &ret, int &secPollStatus, std::string &secPollMessage) { |
41 | secPollMessage.clear(); | |
42 | if (res != 0) { // not NOERROR | |
a4fd2ec2 | 43 | setSecPollToUnknownOnOK(secPollStatus); |
0590f3a3 | 44 | throw PDNSException("RCODE was " + RCode::to_s(res)); |
2d40d42b PL |
45 | } |
46 | ||
47 | if (ret.empty()) { // empty NOERROR... wat? | |
7dcdce8c | 48 | if (secPollStatus == 1) { // it was ok, now it is unknown |
2d40d42b | 49 | secPollStatus = 0; |
7dcdce8c | 50 | } |
2d40d42b PL |
51 | throw PDNSException("Had empty answer on NOERROR RCODE"); |
52 | } | |
53 | ||
54 | DNSRecord record; | |
7dcdce8c OM |
55 | for (auto const &records: ret) { |
56 | if (records.d_type == QType::TXT && records.d_place == DNSResourceRecord::Place::ANSWER) { | |
57 | record = records; | |
2d40d42b PL |
58 | break; |
59 | } | |
60 | } | |
61 | ||
62 | if (record.d_name.empty()) { | |
a4fd2ec2 | 63 | setSecPollToUnknownOnOK(secPollStatus); |
2d40d42b PL |
64 | throw PDNSException("No TXT record found in response"); |
65 | } | |
66 | ||
67 | auto recordContent = getRR<TXTRecordContent>(record); | |
68 | if (recordContent == nullptr) { | |
a4fd2ec2 | 69 | setSecPollToUnknownOnOK(secPollStatus); |
2d40d42b PL |
70 | throw PDNSException("Could not parse TXT record content"); |
71 | } | |
72 | string content = recordContent->d_text; | |
73 | ||
74 | pair<string, string> split = splitField(unquotify(content), ' '); | |
75 | ||
76 | try { | |
77 | secPollStatus = std::stoi(split.first); | |
78 | } catch (const std::exception &e) { | |
a4fd2ec2 | 79 | setSecPollToUnknownOnOK(secPollStatus); |
2d40d42b PL |
80 | throw PDNSException(std::string("Could not parse status number: ") + e.what()); |
81 | } | |
82 | secPollMessage = split.second; | |
83 | } |