2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
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.
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.
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.
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.
26 #include <boost/tokenizer.hpp>
27 #include "namespaces.hh"
31 #include "arguments.hh"
32 #include "dnsparser.hh"
33 #include "responsestats.hh"
40 #include <sys/types.h>
45 extern string s_programname
;
46 extern ResponseStats g_rs
;
51 #ifndef HAVE_STRCASESTR
54 * strcasestr() locates the first occurrence in the string s1 of the
55 * sequence of characters (excluding the terminating null character)
56 * in the string s2, ignoring case. strcasestr() returns a pointer
57 * to the located string, or a null pointer if the string is not found.
58 * If s2 is empty, the function returns s1.
62 strcasestr(const char *s1
, const char *s2
)
64 int *cm
= __trans_lower
;
65 const uchar_t
*us1
= (const uchar_t
*)s1
;
66 const uchar_t
*us2
= (const uchar_t
*)s2
;
70 if (us2
== NULL
|| *us2
== '\0')
74 while (*us1
!= '\0') {
75 if (c
== cm
[*us1
++]) {
77 while (cm
[c
= *++us2
] == cm
[*us1
++] && c
!= '\0')
80 return ((char *)tptr
- 1);
82 us2
= (const uchar_t
*)s2
;
90 #endif // HAVE_STRCASESTR
92 static Json
getServerDetail() {
95 { "id", "localhost" },
96 { "url", "/api/v1/servers/localhost" },
97 { "daemon_type", productTypeApiType() },
98 { "version", getPDNSVersion() },
99 { "config_url", "/api/v1/servers/localhost/config{/config_setting}" },
100 { "zones_url", "/api/v1/servers/localhost/zones{/zone}" }
104 /* Return information about the supported API versions.
105 * The format of this MUST NEVER CHANGE at it's not versioned.
107 void apiDiscovery(HttpRequest
* req
, HttpResponse
* resp
) {
108 if(req
->method
!= "GET")
109 throw HttpMethodNotAllowedException();
111 Json version1
= Json::object
{
115 Json doc
= Json::array
{ version1
};
120 void apiServer(HttpRequest
* req
, HttpResponse
* resp
) {
121 if(req
->method
!= "GET")
122 throw HttpMethodNotAllowedException();
124 Json doc
= Json::array
{getServerDetail()};
128 void apiServerDetail(HttpRequest
* req
, HttpResponse
* resp
) {
129 if(req
->method
!= "GET")
130 throw HttpMethodNotAllowedException();
132 resp
->setBody(getServerDetail());
135 void apiServerConfig(HttpRequest
* req
, HttpResponse
* resp
) {
136 if(req
->method
!= "GET")
137 throw HttpMethodNotAllowedException();
139 vector
<string
> items
= ::arg().list();
142 for(const string
& item
: items
) {
143 if(item
.find("password") != string::npos
|| item
.find("api-key") != string::npos
)
146 value
= ::arg()[item
];
148 doc
.push_back(Json::object
{
149 { "type", "ConfigSetting" },
157 void apiServerStatistics(HttpRequest
* req
, HttpResponse
* resp
) {
158 if(req
->method
!= "GET")
159 throw HttpMethodNotAllowedException();
162 string name
= req
->getvars
["statistic"];
164 auto stat
= productServerStatisticsFetch(name
);
166 throw ApiException("Unknown statistic name");
169 doc
.push_back(Json::object
{
170 { "type", "StatisticItem" },
172 { "value", std::to_string(*stat
) },
180 typedef map
<string
, string
> stat_items_t
;
181 stat_items_t general_stats
;
182 productServerStatisticsFetch(general_stats
);
184 for(const auto& item
: general_stats
) {
185 doc
.push_back(Json::object
{
186 { "type", "StatisticItem" },
187 { "name", item
.first
},
188 { "value", item
.second
},
192 auto resp_qtype_stats
= g_rs
.getQTypeResponseCounts();
193 auto resp_size_stats
= g_rs
.getSizeResponseCounts();
194 auto resp_rcode_stats
= g_rs
.getRCodeResponseCounts();
197 for(const auto& item
: resp_qtype_stats
) {
198 if (item
.second
== 0)
200 values
.push_back(Json::object
{
201 { "name", DNSRecordContent::NumberToType(item
.first
) },
202 { "value", std::to_string(item
.second
) },
206 doc
.push_back(Json::object
{
207 { "type", "MapStatisticItem" },
208 { "name", "response-by-qtype" },
215 for(const auto& item
: resp_size_stats
) {
216 if (item
.second
== 0)
219 values
.push_back(Json::object
{
220 { "name", std::to_string(item
.first
) },
221 { "value", std::to_string(item
.second
) },
225 doc
.push_back(Json::object
{
226 { "type", "MapStatisticItem" },
227 { "name", "response-sizes" },
234 for(const auto& item
: resp_rcode_stats
) {
235 if (item
.second
== 0)
237 values
.push_back(Json::object
{
238 { "name", RCode::to_s(item
.first
) },
239 { "value", std::to_string(item
.second
) },
243 doc
.push_back(Json::object
{
244 { "type", "MapStatisticItem" },
245 { "name", "response-by-rcode" },
251 for(const auto& ringName
: S
.listRings()) {
253 const auto& ring
= S
.getRing(ringName
);
254 for(const auto& item
: ring
) {
255 if (item
.second
== 0)
258 values
.push_back(Json::object
{
259 { "name", item
.first
},
260 { "value", std::to_string(item
.second
) },
264 doc
.push_back(Json::object
{
265 { "type", "RingStatisticItem" },
266 { "name", ringName
},
267 { "size", std::to_string(S
.getRingSize(ringName
)) },
276 DNSName
apiNameToDNSName(const string
& name
) {
277 if (!isCanonical(name
)) {
278 throw ApiException("DNS Name '" + name
+ "' is not canonical");
281 return DNSName(name
);
283 throw ApiException("Unable to parse DNS Name '" + name
+ "'");
287 DNSName
apiZoneIdToName(const string
& id
) {
292 throw HttpBadRequestException();
294 std::size_t lastpos
= 0, pos
= 0;
295 while ((pos
= id
.find('=', lastpos
)) != string::npos
) {
296 ss
<< id
.substr(lastpos
, pos
-lastpos
);
299 if (id
[pos
+1] >= '0' && id
[pos
+1] <= '9') {
301 } else if (id
[pos
+1] >= 'A' && id
[pos
+1] <= 'F') {
302 c
= id
[pos
+1] - 'A' + 10;
304 throw HttpBadRequestException();
309 if (id
[pos
+2] >= '0' && id
[pos
+2] <= '9') {
310 c
+= id
[pos
+2] - '0';
311 } else if (id
[pos
+2] >= 'A' && id
[pos
+2] <= 'F') {
312 c
+= id
[pos
+2] - 'A' + 10;
314 throw HttpBadRequestException();
322 ss
<< id
.substr(lastpos
, pos
-lastpos
);
328 return DNSName(zonename
);
330 throw ApiException("Unable to parse DNS Name '" + zonename
+ "'");
334 string
apiZoneNameToId(const DNSName
& dname
) {
335 string name
=dname
.toString();
338 for(string::const_iterator iter
= name
.begin(); iter
!= name
.end(); ++iter
) {
339 if ((*iter
>= 'A' && *iter
<= 'Z') ||
340 (*iter
>= 'a' && *iter
<= 'z') ||
341 (*iter
>= '0' && *iter
<= '9') ||
342 (*iter
== '.') || (*iter
== '-')) {
345 ss
<< (boost::format("=%02X") % (int)(*iter
));
349 string id
= ss
.str();
352 if (id
.size() == 0 || id
.substr(id
.size()-1) != ".") {
356 // special handling for the root zone, as a dot on it's own doesn't work
359 id
= (boost::format("=%02X") % (int)('.')).str();
364 void apiCheckNameAllowedCharacters(const string
& name
) {
365 if (name
.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890_/.-") != std::string::npos
)
366 throw ApiException("Name '"+name
+"' contains unsupported characters");
369 void apiCheckQNameAllowedCharacters(const string
& qname
) {
370 if (qname
.compare(0, 2, "*.") == 0) apiCheckNameAllowedCharacters(qname
.substr(2));
371 else apiCheckNameAllowedCharacters(qname
);