]>
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) { |
a4fd2ec2 PL |
35 | if(secPollStatus == 1) // it was ok, now it is unknown |
36 | secPollStatus = 0; | |
37 | } | |
38 | ||
2d40d42b PL |
39 | void processSecPoll(const int res, const std::vector<DNSRecord> &ret, int &secPollStatus, std::string &secPollMessage) { |
40 | secPollMessage.clear(); | |
41 | if (res != 0) { // not NOERROR | |
a4fd2ec2 | 42 | setSecPollToUnknownOnOK(secPollStatus); |
2d40d42b PL |
43 | throw PDNSException("RCODE was not NOERROR but " + RCode::to_s(res)); |
44 | } | |
45 | ||
46 | if (ret.empty()) { // empty NOERROR... wat? | |
47 | if(secPollStatus == 1) // it was ok, now it is unknown | |
48 | secPollStatus = 0; | |
49 | throw PDNSException("Had empty answer on NOERROR RCODE"); | |
50 | } | |
51 | ||
52 | DNSRecord record; | |
53 | for (auto const &r: ret) { | |
54 | if (r.d_type == QType::TXT && r.d_place == DNSResourceRecord::Place::ANSWER) { | |
55 | record = r; | |
56 | break; | |
57 | } | |
58 | } | |
59 | ||
60 | if (record.d_name.empty()) { | |
a4fd2ec2 | 61 | setSecPollToUnknownOnOK(secPollStatus); |
2d40d42b PL |
62 | throw PDNSException("No TXT record found in response"); |
63 | } | |
64 | ||
65 | auto recordContent = getRR<TXTRecordContent>(record); | |
66 | if (recordContent == nullptr) { | |
a4fd2ec2 | 67 | setSecPollToUnknownOnOK(secPollStatus); |
2d40d42b PL |
68 | throw PDNSException("Could not parse TXT record content"); |
69 | } | |
70 | string content = recordContent->d_text; | |
71 | ||
72 | pair<string, string> split = splitField(unquotify(content), ' '); | |
73 | ||
74 | try { | |
75 | secPollStatus = std::stoi(split.first); | |
76 | } catch (const std::exception &e) { | |
a4fd2ec2 | 77 | setSecPollToUnknownOnOK(secPollStatus); |
2d40d42b PL |
78 | throw PDNSException(std::string("Could not parse status number: ") + e.what()); |
79 | } | |
80 | secPollMessage = split.second; | |
81 | } |