]>
Commit | Line | Data |
---|---|---|
12c86877 | 1 | /* |
6edbf68a PL |
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 | */ | |
870a0fe4 AT |
22 | #ifdef HAVE_CONFIG_H |
23 | #include "config.h" | |
24 | #endif | |
9054d8a4 | 25 | #include "utility.hh" |
d267d1bf | 26 | #include "dynlistener.hh" |
2470b36e | 27 | #include "ws-auth.hh" |
e611a06c | 28 | #include "json.hh" |
12c86877 BH |
29 | #include "webserver.hh" |
30 | #include "logger.hh" | |
31 | #include "statbag.hh" | |
32 | #include "misc.hh" | |
33 | #include "arguments.hh" | |
34 | #include "dns.hh" | |
6cc98ddf | 35 | #include "comment.hh" |
e611a06c | 36 | #include "ueberbackend.hh" |
dcc65f25 | 37 | #include <boost/format.hpp> |
fa8fd4d2 | 38 | |
9ac4a7c6 | 39 | #include "namespaces.hh" |
6ec5e728 | 40 | #include "ws-api.hh" |
ba1a571d | 41 | #include "version.hh" |
d29d5db7 | 42 | #include "dnsseckeeper.hh" |
3c3c006b | 43 | #include <iomanip> |
0f0e73fe | 44 | #include "zoneparser-tng.hh" |
a426cb89 | 45 | #include "common_startup.hh" |
bf269e28 | 46 | #include "auth-caches.hh" |
8537b9f0 | 47 | |
24afabad | 48 | using json11::Json; |
12c86877 BH |
49 | |
50 | extern StatBag S; | |
51 | ||
f63168e6 | 52 | static void patchZone(HttpRequest* req, HttpResponse* resp); |
995473c8 | 53 | static void storeChangedPTRs(UeberBackend& B, vector<DNSResourceRecord>& new_ptrs); |
f63168e6 CH |
54 | static void makePtr(const DNSResourceRecord& rr, DNSResourceRecord* ptr); |
55 | ||
dea47634 | 56 | AuthWebServer::AuthWebServer() |
12c86877 BH |
57 | { |
58 | d_start=time(0); | |
96d299db | 59 | d_min10=d_min5=d_min1=0; |
c81c2ea8 | 60 | d_ws = 0; |
f17c93b4 | 61 | d_tid = 0; |
536ab56f | 62 | if(arg().mustDo("webserver") || arg().mustDo("api")) { |
bbef8f04 | 63 | d_ws = new WebServer(arg()["webserver-address"], arg().asNum("webserver-port")); |
825fa717 CH |
64 | d_ws->bind(); |
65 | } | |
12c86877 BH |
66 | } |
67 | ||
dea47634 | 68 | void AuthWebServer::go() |
12c86877 | 69 | { |
536ab56f CH |
70 | S.doRings(); |
71 | pthread_create(&d_tid, 0, webThreadHelper, this); | |
72 | pthread_create(&d_tid, 0, statThreadHelper, this); | |
12c86877 BH |
73 | } |
74 | ||
dea47634 | 75 | void AuthWebServer::statThread() |
12c86877 BH |
76 | { |
77 | try { | |
78 | for(;;) { | |
79 | d_queries.submit(S.read("udp-queries")); | |
80 | d_cachehits.submit(S.read("packetcache-hit")); | |
81 | d_cachemisses.submit(S.read("packetcache-miss")); | |
82 | d_qcachehits.submit(S.read("query-cache-hit")); | |
83 | d_qcachemisses.submit(S.read("query-cache-miss")); | |
84 | Utility::sleep(1); | |
85 | } | |
86 | } | |
87 | catch(...) { | |
88 | L<<Logger::Error<<"Webserver statThread caught an exception, dying"<<endl; | |
5bd2ea7b | 89 | _exit(1); |
12c86877 BH |
90 | } |
91 | } | |
92 | ||
dea47634 | 93 | void *AuthWebServer::statThreadHelper(void *p) |
12c86877 | 94 | { |
dea47634 CH |
95 | AuthWebServer *self=static_cast<AuthWebServer *>(p); |
96 | self->statThread(); | |
12c86877 BH |
97 | return 0; // never reached |
98 | } | |
99 | ||
dea47634 | 100 | void *AuthWebServer::webThreadHelper(void *p) |
12c86877 | 101 | { |
dea47634 CH |
102 | AuthWebServer *self=static_cast<AuthWebServer *>(p); |
103 | self->webThread(); | |
12c86877 BH |
104 | return 0; // never reached |
105 | } | |
106 | ||
9f3fdaa0 CH |
107 | static string htmlescape(const string &s) { |
108 | string result; | |
109 | for(string::const_iterator it=s.begin(); it!=s.end(); ++it) { | |
110 | switch (*it) { | |
111 | case '&': | |
c86a96f9 | 112 | result += "&"; |
9f3fdaa0 CH |
113 | break; |
114 | case '<': | |
115 | result += "<"; | |
116 | break; | |
117 | case '>': | |
118 | result += ">"; | |
119 | break; | |
c7f59d62 PL |
120 | case '"': |
121 | result += """; | |
122 | break; | |
9f3fdaa0 CH |
123 | default: |
124 | result += *it; | |
125 | } | |
126 | } | |
127 | return result; | |
128 | } | |
129 | ||
12c86877 BH |
130 | void printtable(ostringstream &ret, const string &ringname, const string &title, int limit=10) |
131 | { | |
132 | int tot=0; | |
133 | int entries=0; | |
101b5d5d | 134 | vector<pair <string,unsigned int> >ring=S.getRing(ringname); |
12c86877 | 135 | |
1071abdd | 136 | for(vector<pair<string, unsigned int> >::const_iterator i=ring.begin(); i!=ring.end();++i) { |
12c86877 BH |
137 | tot+=i->second; |
138 | entries++; | |
139 | } | |
140 | ||
1071abdd | 141 | ret<<"<div class=\"panel\">"; |
c7f59d62 | 142 | ret<<"<span class=resetring><i></i><a href=\"?resetring="<<htmlescape(ringname)<<"\">Reset</a></span>"<<endl; |
1071abdd CH |
143 | ret<<"<h2>"<<title<<"</h2>"<<endl; |
144 | ret<<"<div class=ringmeta>"; | |
c7f59d62 | 145 | ret<<"<a class=topXofY href=\"?ring="<<htmlescape(ringname)<<"\">Showing: Top "<<limit<<" of "<<entries<<"</a>"<<endl; |
1071abdd | 146 | ret<<"<span class=resizering>Resize: "; |
bb3c3f50 | 147 | unsigned int sizes[]={10,100,500,1000,10000,500000,0}; |
12c86877 BH |
148 | for(int i=0;sizes[i];++i) { |
149 | if(S.getRingSize(ringname)!=sizes[i]) | |
c7f59d62 | 150 | ret<<"<a href=\"?resizering="<<htmlescape(ringname)<<"&size="<<sizes[i]<<"\">"<<sizes[i]<<"</a> "; |
12c86877 BH |
151 | else |
152 | ret<<"("<<sizes[i]<<") "; | |
153 | } | |
1071abdd | 154 | ret<<"</span></div>"; |
12c86877 | 155 | |
1071abdd | 156 | ret<<"<table class=\"data\">"; |
12c86877 | 157 | int printed=0; |
f5cb7e61 | 158 | int total=max(1,tot); |
bb3c3f50 | 159 | for(vector<pair<string,unsigned int> >::const_iterator i=ring.begin();limit && i!=ring.end();++i,--limit) { |
dea47634 | 160 | ret<<"<tr><td>"<<htmlescape(i->first)<<"</td><td>"<<i->second<<"</td><td align=right>"<< AuthWebServer::makePercentage(i->second*100.0/total)<<"</td>"<<endl; |
12c86877 BH |
161 | printed+=i->second; |
162 | } | |
163 | ret<<"<tr><td colspan=3></td></tr>"<<endl; | |
164 | if(printed!=tot) | |
dea47634 | 165 | ret<<"<tr><td><b>Rest:</b></td><td><b>"<<tot-printed<<"</b></td><td align=right><b>"<< AuthWebServer::makePercentage((tot-printed)*100.0/total)<<"</b></td>"<<endl; |
12c86877 | 166 | |
e2a77e08 | 167 | ret<<"<tr><td><b>Total:</b></td><td><b>"<<tot<<"</b></td><td align=right><b>100%</b></td>"; |
1071abdd | 168 | ret<<"</table></div>"<<endl; |
12c86877 BH |
169 | } |
170 | ||
dea47634 | 171 | void AuthWebServer::printvars(ostringstream &ret) |
12c86877 | 172 | { |
1071abdd | 173 | ret<<"<div class=panel><h2>Variables</h2><table class=\"data\">"<<endl; |
12c86877 BH |
174 | |
175 | vector<string>entries=S.getEntries(); | |
176 | for(vector<string>::const_iterator i=entries.begin();i!=entries.end();++i) { | |
177 | ret<<"<tr><td>"<<*i<<"</td><td>"<<S.read(*i)<<"</td><td>"<<S.getDescrip(*i)<<"</td>"<<endl; | |
178 | } | |
e2a77e08 | 179 | |
1071abdd | 180 | ret<<"</table></div>"<<endl; |
12c86877 BH |
181 | } |
182 | ||
dea47634 | 183 | void AuthWebServer::printargs(ostringstream &ret) |
12c86877 | 184 | { |
e2a77e08 | 185 | ret<<"<table border=1><tr><td colspan=3 bgcolor=\"#0000ff\"><font color=\"#ffffff\">Arguments</font></td>"<<endl; |
12c86877 BH |
186 | |
187 | vector<string>entries=arg().list(); | |
188 | for(vector<string>::const_iterator i=entries.begin();i!=entries.end();++i) { | |
189 | ret<<"<tr><td>"<<*i<<"</td><td>"<<arg()[*i]<<"</td><td>"<<arg().getHelp(*i)<<"</td>"<<endl; | |
190 | } | |
191 | } | |
192 | ||
dea47634 | 193 | string AuthWebServer::makePercentage(const double& val) |
b6f57093 BH |
194 | { |
195 | return (boost::format("%.01f%%") % val).str(); | |
196 | } | |
197 | ||
dea47634 | 198 | void AuthWebServer::indexfunction(HttpRequest* req, HttpResponse* resp) |
12c86877 | 199 | { |
583ea80d CH |
200 | if(!req->getvars["resetring"].empty()) { |
201 | if (S.ringExists(req->getvars["resetring"])) | |
202 | S.resetRing(req->getvars["resetring"]); | |
d7b8730e | 203 | resp->status = 302; |
0665b7e6 | 204 | resp->headers["Location"] = req->url.path; |
80d59cd1 | 205 | return; |
12c86877 | 206 | } |
583ea80d | 207 | if(!req->getvars["resizering"].empty()){ |
335da0ba | 208 | int size=std::stoi(req->getvars["size"]); |
583ea80d | 209 | if (S.ringExists(req->getvars["resizering"]) && size > 0 && size <= 500000) |
335da0ba | 210 | S.resizeRing(req->getvars["resizering"], std::stoi(req->getvars["size"])); |
d7b8730e | 211 | resp->status = 302; |
0665b7e6 | 212 | resp->headers["Location"] = req->url.path; |
80d59cd1 | 213 | return; |
12c86877 BH |
214 | } |
215 | ||
216 | ostringstream ret; | |
217 | ||
1071abdd CH |
218 | ret<<"<!DOCTYPE html>"<<endl; |
219 | ret<<"<html><head>"<<endl; | |
220 | ret<<"<title>PowerDNS Authoritative Server Monitor</title>"<<endl; | |
221 | ret<<"<link rel=\"stylesheet\" href=\"style.css\"/>"<<endl; | |
222 | ret<<"</head><body>"<<endl; | |
223 | ||
224 | ret<<"<div class=\"row\">"<<endl; | |
225 | ret<<"<div class=\"headl columns\">"; | |
a1caa8b8 | 226 | ret<<"<a href=\"/\" id=\"appname\">PowerDNS "<<htmlescape(VERSION); |
1071abdd | 227 | if(!arg()["config-name"].empty()) { |
a1caa8b8 | 228 | ret<<" ["<<htmlescape(arg()["config-name"])<<"]"; |
1071abdd CH |
229 | } |
230 | ret<<"</a></div>"<<endl; | |
231 | ret<<"<div class=\"headr columns\"></div></div>"; | |
232 | ret<<"<div class=\"row\"><div class=\"all columns\">"; | |
12c86877 BH |
233 | |
234 | time_t passed=time(0)-s_starttime; | |
235 | ||
e2a77e08 KM |
236 | ret<<"<p>Uptime: "<< |
237 | humanDuration(passed)<< | |
238 | "<br>"<<endl; | |
12c86877 | 239 | |
395b07ea | 240 | ret<<"Queries/second, 1, 5, 10 minute averages: "<<std::setprecision(3)<< |
3e1cd1f4 | 241 | (int)d_queries.get1()<<", "<< |
242 | (int)d_queries.get5()<<", "<< | |
243 | (int)d_queries.get10()<<". Max queries/second: "<<(int)d_queries.getMax()<< | |
12c86877 | 244 | "<br>"<<endl; |
1d6b70f9 | 245 | |
f6154a3b | 246 | if(d_cachemisses.get10()+d_cachehits.get10()>0) |
b6f57093 | 247 | ret<<"Cache hitrate, 1, 5, 10 minute averages: "<< |
f6154a3b CH |
248 | makePercentage((d_cachehits.get1()*100.0)/((d_cachehits.get1())+(d_cachemisses.get1())))<<", "<< |
249 | makePercentage((d_cachehits.get5()*100.0)/((d_cachehits.get5())+(d_cachemisses.get5())))<<", "<< | |
250 | makePercentage((d_cachehits.get10()*100.0)/((d_cachehits.get10())+(d_cachemisses.get10())))<< | |
b6f57093 | 251 | "<br>"<<endl; |
12c86877 | 252 | |
f6154a3b | 253 | if(d_qcachemisses.get10()+d_qcachehits.get10()>0) |
395b07ea | 254 | ret<<"Backend query cache hitrate, 1, 5, 10 minute averages: "<<std::setprecision(2)<< |
f6154a3b CH |
255 | makePercentage((d_qcachehits.get1()*100.0)/((d_qcachehits.get1())+(d_qcachemisses.get1())))<<", "<< |
256 | makePercentage((d_qcachehits.get5()*100.0)/((d_qcachehits.get5())+(d_qcachemisses.get5())))<<", "<< | |
257 | makePercentage((d_qcachehits.get10()*100.0)/((d_qcachehits.get10())+(d_qcachemisses.get10())))<< | |
b6f57093 | 258 | "<br>"<<endl; |
12c86877 | 259 | |
395b07ea | 260 | ret<<"Backend query load, 1, 5, 10 minute averages: "<<std::setprecision(3)<< |
3e1cd1f4 | 261 | (int)d_qcachemisses.get1()<<", "<< |
262 | (int)d_qcachemisses.get5()<<", "<< | |
263 | (int)d_qcachemisses.get10()<<". Max queries/second: "<<(int)d_qcachemisses.getMax()<< | |
12c86877 BH |
264 | "<br>"<<endl; |
265 | ||
1071abdd | 266 | ret<<"Total queries: "<<S.read("udp-queries")<<". Question/answer latency: "<<S.read("latency")/1000.0<<"ms</p><br>"<<endl; |
583ea80d | 267 | if(req->getvars["ring"].empty()) { |
12c86877 BH |
268 | vector<string>entries=S.listRings(); |
269 | for(vector<string>::const_iterator i=entries.begin();i!=entries.end();++i) | |
270 | printtable(ret,*i,S.getRingTitle(*i)); | |
271 | ||
f6154a3b | 272 | printvars(ret); |
12c86877 | 273 | if(arg().mustDo("webserver-print-arguments")) |
f6154a3b | 274 | printargs(ret); |
12c86877 | 275 | } |
bea69e32 | 276 | else if(S.ringExists(req->getvars["ring"])) |
583ea80d | 277 | printtable(ret,req->getvars["ring"],S.getRingTitle(req->getvars["ring"]),100); |
12c86877 | 278 | |
1071abdd | 279 | ret<<"</div></div>"<<endl; |
a4167e57 | 280 | ret<<"<footer class=\"row\">"<<fullVersionString()<<"<br>© 2013 - 2018 <a href=\"http://www.powerdns.com/\">PowerDNS.COM BV</a>.</footer>"<<endl; |
12c86877 BH |
281 | ret<<"</body></html>"<<endl; |
282 | ||
80d59cd1 | 283 | resp->body = ret.str(); |
61f5d289 | 284 | resp->status = 200; |
12c86877 BH |
285 | } |
286 | ||
1d6b70f9 CH |
287 | /** Helper to build a record content as needed. */ |
288 | static inline string makeRecordContent(const QType& qtype, const string& content, bool noDot) { | |
289 | // noDot: for backend storage, pass true. for API users, pass false. | |
9a2c1e06 | 290 | auto drc = DNSRecordContent::makeunique(qtype.getCode(), QClass::IN, content); |
7fe1a82b | 291 | return drc->getZoneRepresentation(noDot); |
1d6b70f9 CH |
292 | } |
293 | ||
294 | /** "Normalize" record content for API consumers. */ | |
295 | static inline string makeApiRecordContent(const QType& qtype, const string& content) { | |
296 | return makeRecordContent(qtype, content, false); | |
297 | } | |
298 | ||
299 | /** "Normalize" record content for backend storage. */ | |
300 | static inline string makeBackendRecordContent(const QType& qtype, const string& content) { | |
301 | return makeRecordContent(qtype, content, true); | |
302 | } | |
303 | ||
ce846be6 | 304 | static Json::object getZoneInfo(const DomainInfo& di, DNSSECKeeper *dk) { |
290a083d | 305 | string zoneId = apiZoneNameToId(di.zone); |
62a9a74c CH |
306 | return Json::object { |
307 | // id is the canonical lookup key, which doesn't actually match the name (in some cases) | |
308 | { "id", zoneId }, | |
16e25450 | 309 | { "url", "/api/v1/servers/localhost/zones/" + zoneId }, |
62a9a74c CH |
310 | { "name", di.zone.toString() }, |
311 | { "kind", di.getKindString() }, | |
ce846be6 | 312 | { "dnssec", dk->isSecuredZone(di.zone) }, |
62a9a74c CH |
313 | { "account", di.account }, |
314 | { "masters", di.masters }, | |
315 | { "serial", (double)di.serial }, | |
316 | { "notified_serial", (double)di.notified_serial }, | |
317 | { "last_check", (double)di.last_check } | |
318 | }; | |
c04b5870 CH |
319 | } |
320 | ||
986e4858 PL |
321 | static bool shouldDoRRSets(HttpRequest* req) { |
322 | if (req->getvars.count("rrsets") == 0 || req->getvars["rrsets"] == "true") | |
323 | return true; | |
324 | if (req->getvars["rrsets"] == "false") | |
325 | return false; | |
326 | throw ApiException("'rrsets' request parameter value '"+req->getvars["rrsets"]+"' is not supported"); | |
327 | } | |
328 | ||
329 | static void fillZone(const DNSName& zonename, HttpResponse* resp, bool doRRSets) { | |
1abb81f4 | 330 | UeberBackend B; |
1abb81f4 | 331 | DomainInfo di; |
77bfe8de PL |
332 | if(!B.getDomainInfo(zonename, di)) { |
333 | throw HttpNotFoundException(); | |
334 | } | |
1abb81f4 | 335 | |
adef67eb | 336 | DNSSECKeeper dk(&B); |
ce846be6 | 337 | Json::object doc = getZoneInfo(di, &dk); |
62a9a74c | 338 | // extra stuff getZoneInfo doesn't do for us (more expensive) |
d29d5db7 CH |
339 | string soa_edit_api; |
340 | di.backend->getDomainMetadataOne(zonename, "SOA-EDIT-API", soa_edit_api); | |
62a9a74c | 341 | doc["soa_edit_api"] = soa_edit_api; |
6bb25159 MS |
342 | string soa_edit; |
343 | di.backend->getDomainMetadataOne(zonename, "SOA-EDIT", soa_edit); | |
62a9a74c | 344 | doc["soa_edit"] = soa_edit; |
986e4858 PL |
345 | string nsec3param; |
346 | di.backend->getDomainMetadataOne(zonename, "NSEC3PARAM", nsec3param); | |
347 | doc["nsec3param"] = nsec3param; | |
348 | string nsec3narrow; | |
349 | bool nsec3narrowbool = false; | |
350 | di.backend->getDomainMetadataOne(zonename, "NSEC3NARROW", nsec3narrow); | |
351 | if (nsec3narrow == "1") | |
352 | nsec3narrowbool = true; | |
353 | doc["nsec3narrow"] = nsec3narrowbool; | |
354 | ||
355 | string api_rectify; | |
356 | di.backend->getDomainMetadataOne(zonename, "API-RECTIFY", api_rectify); | |
357 | doc["api_rectify"] = (api_rectify == "1"); | |
358 | ||
359 | if (doRRSets) { | |
360 | vector<DNSResourceRecord> records; | |
361 | vector<Comment> comments; | |
362 | ||
363 | // load all records + sort | |
364 | { | |
365 | DNSResourceRecord rr; | |
366 | di.backend->list(zonename, di.id, true); // incl. disabled | |
367 | while(di.backend->get(rr)) { | |
368 | if (!rr.qtype.getCode()) | |
369 | continue; // skip empty non-terminals | |
370 | records.push_back(rr); | |
371 | } | |
372 | sort(records.begin(), records.end(), [](const DNSResourceRecord& a, const DNSResourceRecord& b) { | |
373 | if (a.qname == b.qname) { | |
374 | return b.qtype < a.qtype; | |
375 | } | |
376 | return b.qname < a.qname; | |
377 | }); | |
6754ef71 | 378 | } |
6754ef71 | 379 | |
986e4858 PL |
380 | // load all comments + sort |
381 | { | |
382 | Comment comment; | |
383 | di.backend->listComments(di.id); | |
384 | while(di.backend->getComment(comment)) { | |
385 | comments.push_back(comment); | |
386 | } | |
387 | sort(comments.begin(), comments.end(), [](const Comment& a, const Comment& b) { | |
388 | if (a.qname == b.qname) { | |
389 | return b.qtype < a.qtype; | |
390 | } | |
391 | return b.qname < a.qname; | |
392 | }); | |
6754ef71 | 393 | } |
6754ef71 | 394 | |
986e4858 PL |
395 | Json::array rrsets; |
396 | Json::object rrset; | |
397 | Json::array rrset_records; | |
398 | Json::array rrset_comments; | |
399 | DNSName current_qname; | |
400 | QType current_qtype; | |
401 | uint32_t ttl; | |
402 | auto rit = records.begin(); | |
403 | auto cit = comments.begin(); | |
404 | ||
405 | while (rit != records.end() || cit != comments.end()) { | |
406 | if (cit == comments.end() || (rit != records.end() && (cit->qname.toString() <= rit->qname.toString() || cit->qtype < rit->qtype || cit->qtype == rit->qtype))) { | |
407 | current_qname = rit->qname; | |
408 | current_qtype = rit->qtype; | |
409 | ttl = rit->ttl; | |
410 | } else { | |
411 | current_qname = cit->qname; | |
412 | current_qtype = cit->qtype; | |
413 | ttl = 0; | |
414 | } | |
6754ef71 | 415 | |
986e4858 PL |
416 | while(rit != records.end() && rit->qname == current_qname && rit->qtype == current_qtype) { |
417 | ttl = min(ttl, rit->ttl); | |
418 | rrset_records.push_back(Json::object { | |
419 | { "disabled", rit->disabled }, | |
420 | { "content", makeApiRecordContent(rit->qtype, rit->content) } | |
421 | }); | |
422 | rit++; | |
423 | } | |
424 | while (cit != comments.end() && cit->qname == current_qname && cit->qtype == current_qtype) { | |
425 | rrset_comments.push_back(Json::object { | |
426 | { "modified_at", (double)cit->modified_at }, | |
427 | { "account", cit->account }, | |
428 | { "content", cit->content } | |
429 | }); | |
430 | cit++; | |
431 | } | |
432 | ||
433 | rrset["name"] = current_qname.toString(); | |
434 | rrset["type"] = current_qtype.getName(); | |
435 | rrset["records"] = rrset_records; | |
436 | rrset["comments"] = rrset_comments; | |
437 | rrset["ttl"] = (double)ttl; | |
438 | rrsets.push_back(rrset); | |
439 | rrset.clear(); | |
440 | rrset_records.clear(); | |
441 | rrset_comments.clear(); | |
6754ef71 CH |
442 | } |
443 | ||
986e4858 | 444 | doc["rrsets"] = rrsets; |
6754ef71 CH |
445 | } |
446 | ||
669822d0 | 447 | resp->setBody(doc); |
1abb81f4 CH |
448 | } |
449 | ||
6ec5e728 CH |
450 | void productServerStatisticsFetch(map<string,string>& out) |
451 | { | |
a45303b8 | 452 | vector<string> items = S.getEntries(); |
ff05fd12 | 453 | for(const string& item : items) { |
335da0ba | 454 | out[item] = std::to_string(S.read(item)); |
a45303b8 CH |
455 | } |
456 | ||
457 | // add uptime | |
335da0ba | 458 | out["uptime"] = std::to_string(time(0) - s_starttime); |
c67bf8c5 CH |
459 | } |
460 | ||
6754ef71 | 461 | static void gatherRecords(const Json container, const DNSName& qname, const QType qtype, const int ttl, vector<DNSResourceRecord>& new_records, vector<DNSResourceRecord>& new_ptrs) { |
f63168e6 CH |
462 | UeberBackend B; |
463 | DNSResourceRecord rr; | |
6754ef71 CH |
464 | rr.qname = qname; |
465 | rr.qtype = qtype; | |
466 | rr.auth = 1; | |
467 | rr.ttl = ttl; | |
1f68b185 | 468 | for(auto record : container["records"].array_items()) { |
1f68b185 | 469 | string content = stringFromJson(record, "content"); |
1f68b185 CH |
470 | rr.disabled = boolFromJson(record, "disabled"); |
471 | ||
1f68b185 CH |
472 | // validate that the client sent something we can actually parse, and require that data to be dotted. |
473 | try { | |
474 | if (rr.qtype.getCode() != QType::AAAA) { | |
475 | string tmp = makeApiRecordContent(rr.qtype, content); | |
476 | if (!pdns_iequals(tmp, content)) { | |
477 | throw std::runtime_error("Not in expected format (parsed as '"+tmp+"')"); | |
478 | } | |
479 | } else { | |
480 | struct in6_addr tmpbuf; | |
481 | if (inet_pton(AF_INET6, content.c_str(), &tmpbuf) != 1 || content.find('.') != string::npos) { | |
482 | throw std::runtime_error("Invalid IPv6 address"); | |
1e5b9ab9 | 483 | } |
f63168e6 | 484 | } |
1f68b185 CH |
485 | rr.content = makeBackendRecordContent(rr.qtype, content); |
486 | } | |
487 | catch(std::exception& e) | |
488 | { | |
489 | throw ApiException("Record "+rr.qname.toString()+"/"+rr.qtype.getName()+" '"+content+"': "+e.what()); | |
490 | } | |
f63168e6 | 491 | |
1f68b185 CH |
492 | if ((rr.qtype.getCode() == QType::A || rr.qtype.getCode() == QType::AAAA) && |
493 | boolFromJson(record, "set-ptr", false) == true) { | |
494 | DNSResourceRecord ptr; | |
495 | makePtr(rr, &ptr); | |
f63168e6 | 496 | |
1f68b185 | 497 | // verify that there's a zone for the PTR |
1f68b185 | 498 | SOAData sd; |
cec52de6 | 499 | if (!B.getAuth(ptr.qname, QType(QType::PTR), &sd, false)) |
1f68b185 | 500 | throw ApiException("Could not find domain for PTR '"+ptr.qname.toString()+"' requested for '"+ptr.content+"'"); |
f63168e6 | 501 | |
1f68b185 CH |
502 | ptr.domain_id = sd.domain_id; |
503 | new_ptrs.push_back(ptr); | |
f63168e6 | 504 | } |
1f68b185 CH |
505 | |
506 | new_records.push_back(rr); | |
f63168e6 CH |
507 | } |
508 | } | |
509 | ||
6754ef71 | 510 | static void gatherComments(const Json container, const DNSName& qname, const QType qtype, vector<Comment>& new_comments) { |
f63168e6 | 511 | Comment c; |
6754ef71 CH |
512 | c.qname = qname; |
513 | c.qtype = qtype; | |
f63168e6 CH |
514 | |
515 | time_t now = time(0); | |
1f68b185 | 516 | for (auto comment : container["comments"].array_items()) { |
1f68b185 CH |
517 | c.modified_at = intFromJson(comment, "modified_at", now); |
518 | c.content = stringFromJson(comment, "content"); | |
519 | c.account = stringFromJson(comment, "account"); | |
520 | new_comments.push_back(c); | |
f63168e6 CH |
521 | } |
522 | } | |
6cc98ddf | 523 | |
986e4858 PL |
524 | static void checkDefaultDNSSECAlgos() { |
525 | int k_algo = DNSSECKeeper::shorthand2algorithm(::arg()["default-ksk-algorithm"]); | |
526 | int z_algo = DNSSECKeeper::shorthand2algorithm(::arg()["default-zsk-algorithm"]); | |
527 | int k_size = arg().asNum("default-ksk-size"); | |
528 | int z_size = arg().asNum("default-zsk-size"); | |
529 | ||
530 | // Sanity check DNSSEC parameters | |
531 | if (::arg()["default-zsk-algorithm"] != "") { | |
532 | if (k_algo == -1) | |
533 | throw ApiException("default-ksk-algorithm setting is set to unknown algorithm: " + ::arg()["default-ksk-algorithm"]); | |
534 | else if (k_algo <= 10 && k_size == 0) | |
535 | throw ApiException("default-ksk-algorithm is set to an algorithm("+::arg()["default-ksk-algorithm"]+") that requires a non-zero default-ksk-size!"); | |
536 | } | |
537 | ||
538 | if (::arg()["default-zsk-algorithm"] != "") { | |
539 | if (z_algo == -1) | |
540 | throw ApiException("default-zsk-algorithm setting is set to unknown algorithm: " + ::arg()["default-zsk-algorithm"]); | |
541 | else if (z_algo <= 10 && z_size == 0) | |
542 | throw ApiException("default-zsk-algorithm is set to an algorithm("+::arg()["default-zsk-algorithm"]+") that requires a non-zero default-zsk-size!"); | |
543 | } | |
544 | } | |
545 | ||
89a7e706 PL |
546 | static void throwUnableToSecure(const DNSName& zonename) { |
547 | throw ApiException("No backend was able to secure '" + zonename.toString() + "', most likely because no DNSSEC" | |
548 | + "capable backends are loaded, or because the backends have DNSSEC disabled. Check your configuration."); | |
549 | } | |
550 | ||
986e4858 | 551 | static void updateDomainSettingsFromDocument(UeberBackend& B, const DomainInfo& di, const DNSName& zonename, const Json document) { |
1f68b185 | 552 | string zonemaster; |
986e4858 | 553 | bool shouldRectify = false; |
1f68b185 CH |
554 | for(auto value : document["masters"].array_items()) { |
555 | string master = value.string_value(); | |
556 | if (master.empty()) | |
557 | throw ApiException("Master can not be an empty string"); | |
558 | zonemaster += master + " "; | |
bb9fd223 CH |
559 | } |
560 | ||
986e4858 PL |
561 | if (zonemaster != "") { |
562 | di.backend->setMaster(zonename, zonemaster); | |
563 | } | |
564 | if (document["kind"].is_string()) { | |
565 | di.backend->setKind(zonename, DomainInfo::stringToKind(stringFromJson(document, "kind"))); | |
566 | } | |
1f68b185 CH |
567 | if (document["soa_edit_api"].is_string()) { |
568 | di.backend->setDomainMetadataOne(zonename, "SOA-EDIT-API", document["soa_edit_api"].string_value()); | |
d29d5db7 | 569 | } |
1f68b185 CH |
570 | if (document["soa_edit"].is_string()) { |
571 | di.backend->setDomainMetadataOne(zonename, "SOA-EDIT", document["soa_edit"].string_value()); | |
6bb25159 | 572 | } |
c00908f1 PL |
573 | try { |
574 | bool api_rectify = boolFromJson(document, "api_rectify"); | |
575 | di.backend->setDomainMetadataOne(zonename, "API-RECTIFY", api_rectify ? "1" : "0"); | |
986e4858 | 576 | } |
c00908f1 PL |
577 | catch (JsonException) {} |
578 | ||
1f68b185 CH |
579 | if (document["account"].is_string()) { |
580 | di.backend->setAccount(zonename, document["account"].string_value()); | |
79532aa7 | 581 | } |
986e4858 PL |
582 | |
583 | DNSSECKeeper dk(&B); | |
584 | bool dnssecInJSON = false; | |
585 | bool dnssecDocVal = false; | |
586 | ||
587 | try { | |
588 | dnssecDocVal = boolFromJson(document, "dnssec"); | |
589 | dnssecInJSON = true; | |
590 | } | |
591 | catch (JsonException) {} | |
592 | ||
593 | bool isDNSSECZone = dk.isSecuredZone(zonename); | |
594 | ||
595 | if (dnssecInJSON) { | |
596 | if (dnssecDocVal) { | |
597 | if (!isDNSSECZone) { | |
598 | checkDefaultDNSSECAlgos(); | |
599 | ||
600 | int k_algo = DNSSECKeeper::shorthand2algorithm(::arg()["default-ksk-algorithm"]); | |
601 | int z_algo = DNSSECKeeper::shorthand2algorithm(::arg()["default-zsk-algorithm"]); | |
602 | int k_size = arg().asNum("default-ksk-size"); | |
603 | int z_size = arg().asNum("default-zsk-size"); | |
604 | ||
605 | if (k_algo != -1) { | |
606 | int64_t id; | |
607 | if (!dk.addKey(zonename, true, k_algo, id, k_size)) { | |
89a7e706 | 608 | throwUnableToSecure(zonename); |
986e4858 PL |
609 | } |
610 | } | |
611 | ||
612 | if (z_algo != -1) { | |
613 | int64_t id; | |
614 | if (!dk.addKey(zonename, false, z_algo, id, z_size)) { | |
89a7e706 | 615 | throwUnableToSecure(zonename); |
986e4858 PL |
616 | } |
617 | } | |
618 | ||
619 | // Used later for NSEC3PARAM | |
620 | isDNSSECZone = dk.isSecuredZone(zonename); | |
621 | ||
622 | if (!isDNSSECZone) { | |
89a7e706 | 623 | throwUnableToSecure(zonename); |
986e4858 PL |
624 | } |
625 | shouldRectify = true; | |
626 | } | |
627 | } else { | |
628 | // "dnssec": false in json | |
629 | if (isDNSSECZone) { | |
cbe8b186 PL |
630 | string info, error; |
631 | if (!dk.unSecureZone(zonename, error, info)) { | |
632 | throw ApiException("Error while un-securing zone '"+ zonename.toString()+"': " + error); | |
633 | } | |
634 | isDNSSECZone = dk.isSecuredZone(zonename); | |
635 | if (isDNSSECZone) { | |
636 | throw ApiException("Unable to un-secure zone '"+ zonename.toString()+"'"); | |
637 | } | |
638 | shouldRectify = true; | |
986e4858 PL |
639 | } |
640 | } | |
641 | } | |
642 | ||
643 | if(document["nsec3param"].string_value().length() > 0) { | |
644 | shouldRectify = true; | |
645 | NSEC3PARAMRecordContent ns3pr(document["nsec3param"].string_value()); | |
646 | string error_msg = ""; | |
647 | if (!isDNSSECZone) { | |
648 | throw ApiException("NSEC3PARAMs provided for zone '"+zonename.toString()+"', but zone is not DNSSEC secured."); | |
649 | } | |
650 | if (!dk.checkNSEC3PARAM(ns3pr, error_msg)) { | |
651 | throw ApiException("NSEC3PARAMs provided for zone '"+zonename.toString()+"' are invalid. " + error_msg); | |
652 | } | |
653 | if (!dk.setNSEC3PARAM(zonename, ns3pr, boolFromJson(document, "nsec3narrow", false))) { | |
654 | throw ApiException("NSEC3PARAMs provided for zone '" + zonename.toString() + | |
655 | "' passed our basic sanity checks, but cannot be used with the current backend."); | |
656 | } | |
657 | } | |
658 | ||
659 | string api_rectify; | |
660 | di.backend->getDomainMetadataOne(zonename, "API-RECTIFY", api_rectify); | |
661 | if (shouldRectify && dk.isSecuredZone(zonename) && !dk.isPresigned(zonename) && api_rectify == "1") { | |
59102608 | 662 | string info; |
986e4858 | 663 | string error_msg = ""; |
59102608 | 664 | if (!dk.rectifyZone(zonename, error_msg, info, true)) |
986e4858 PL |
665 | throw ApiException("Failed to rectify '" + zonename.toString() + "' " + error_msg); |
666 | } | |
bb9fd223 CH |
667 | } |
668 | ||
24e11043 CJ |
669 | static bool isValidMetadataKind(const string& kind, bool readonly) { |
670 | static vector<string> builtinOptions { | |
671 | "ALLOW-AXFR-FROM", | |
672 | "AXFR-SOURCE", | |
673 | "ALLOW-DNSUPDATE-FROM", | |
674 | "TSIG-ALLOW-DNSUPDATE", | |
675 | "FORWARD-DNSUPDATE", | |
676 | "SOA-EDIT-DNSUPDATE", | |
4c5b6925 | 677 | "NOTIFY-DNSUPDATE", |
24e11043 CJ |
678 | "ALSO-NOTIFY", |
679 | "AXFR-MASTER-TSIG", | |
680 | "GSS-ALLOW-AXFR-PRINCIPAL", | |
681 | "GSS-ACCEPTOR-PRINCIPAL", | |
682 | "IXFR", | |
683 | "LUA-AXFR-SCRIPT", | |
684 | "NSEC3NARROW", | |
685 | "NSEC3PARAM", | |
686 | "PRESIGNED", | |
687 | "PUBLISH-CDNSKEY", | |
688 | "PUBLISH-CDS", | |
689 | "SOA-EDIT", | |
690 | "TSIG-ALLOW-AXFR", | |
691 | "TSIG-ALLOW-DNSUPDATE" | |
692 | }; | |
693 | ||
694 | // the following options do not allow modifications via API | |
695 | static vector<string> protectedOptions { | |
986e4858 | 696 | "API-RECTIFY", |
24e11043 CJ |
697 | "NSEC3NARROW", |
698 | "NSEC3PARAM", | |
699 | "PRESIGNED", | |
700 | "LUA-AXFR-SCRIPT" | |
701 | }; | |
702 | ||
9ac4e6d5 PL |
703 | if (kind.find("X-") == 0) |
704 | return true; | |
705 | ||
24e11043 CJ |
706 | bool found = false; |
707 | ||
d8043c73 | 708 | for (const string& s : builtinOptions) { |
24e11043 | 709 | if (kind == s) { |
d8043c73 | 710 | for (const string& s2 : protectedOptions) { |
24e11043 CJ |
711 | if (!readonly && s == s2) |
712 | return false; | |
713 | } | |
714 | found = true; | |
715 | break; | |
716 | } | |
717 | } | |
718 | ||
719 | return found; | |
720 | } | |
721 | ||
722 | static void apiZoneMetadata(HttpRequest* req, HttpResponse *resp) { | |
723 | DNSName zonename = apiZoneIdToName(req->parameters["id"]); | |
d38e81e6 | 724 | |
24e11043 | 725 | UeberBackend B; |
d38e81e6 | 726 | DomainInfo di; |
77bfe8de PL |
727 | if (!B.getDomainInfo(zonename, di)) { |
728 | throw HttpNotFoundException(); | |
729 | } | |
24e11043 CJ |
730 | |
731 | if (req->method == "GET") { | |
732 | map<string, vector<string> > md; | |
733 | Json::array document; | |
734 | ||
735 | if (!B.getAllDomainMetadata(zonename, md)) | |
736 | throw HttpNotFoundException(); | |
737 | ||
738 | for (const auto& i : md) { | |
739 | Json::array entries; | |
740 | for (string j : i.second) | |
741 | entries.push_back(j); | |
742 | ||
743 | Json::object key { | |
744 | { "type", "Metadata" }, | |
745 | { "kind", i.first }, | |
746 | { "metadata", entries } | |
747 | }; | |
748 | ||
749 | document.push_back(key); | |
750 | } | |
751 | ||
752 | resp->setBody(document); | |
753 | } else if (req->method == "POST" && !::arg().mustDo("api-readonly")) { | |
754 | auto document = req->json(); | |
755 | string kind; | |
756 | vector<string> entries; | |
757 | ||
758 | try { | |
759 | kind = stringFromJson(document, "kind"); | |
760 | } catch (JsonException) { | |
761 | throw ApiException("kind is not specified or not a string"); | |
762 | } | |
763 | ||
764 | if (!isValidMetadataKind(kind, false)) | |
765 | throw ApiException("Unsupported metadata kind '" + kind + "'"); | |
766 | ||
767 | vector<string> vecMetadata; | |
c6720e79 CJ |
768 | |
769 | if (!B.getDomainMetadata(zonename, kind, vecMetadata)) | |
770 | throw ApiException("Could not retrieve metadata entries for domain '" + | |
771 | zonename.toString() + "'"); | |
772 | ||
24e11043 CJ |
773 | auto& metadata = document["metadata"]; |
774 | if (!metadata.is_array()) | |
775 | throw ApiException("metadata is not specified or not an array"); | |
776 | ||
777 | for (const auto& i : metadata.array_items()) { | |
778 | if (!i.is_string()) | |
779 | throw ApiException("metadata must be strings"); | |
c6720e79 CJ |
780 | else if (std::find(vecMetadata.cbegin(), |
781 | vecMetadata.cend(), | |
782 | i.string_value()) == vecMetadata.cend()) { | |
783 | vecMetadata.push_back(i.string_value()); | |
784 | } | |
24e11043 CJ |
785 | } |
786 | ||
787 | if (!B.setDomainMetadata(zonename, kind, vecMetadata)) | |
c6720e79 CJ |
788 | throw ApiException("Could not update metadata entries for domain '" + |
789 | zonename.toString() + "'"); | |
790 | ||
791 | Json::array respMetadata; | |
792 | for (const string& s : vecMetadata) | |
793 | respMetadata.push_back(s); | |
794 | ||
795 | Json::object key { | |
796 | { "type", "Metadata" }, | |
797 | { "kind", document["kind"] }, | |
798 | { "metadata", respMetadata } | |
799 | }; | |
24e11043 | 800 | |
24e11043 | 801 | resp->status = 201; |
c6720e79 | 802 | resp->setBody(key); |
24e11043 CJ |
803 | } else |
804 | throw HttpMethodNotAllowedException(); | |
805 | } | |
806 | ||
807 | static void apiZoneMetadataKind(HttpRequest* req, HttpResponse* resp) { | |
808 | DNSName zonename = apiZoneIdToName(req->parameters["id"]); | |
d38e81e6 | 809 | |
24e11043 | 810 | UeberBackend B; |
d38e81e6 | 811 | DomainInfo di; |
77bfe8de PL |
812 | if (!B.getDomainInfo(zonename, di)) { |
813 | throw HttpNotFoundException(); | |
814 | } | |
d38e81e6 PL |
815 | |
816 | string kind = req->parameters["kind"]; | |
24e11043 CJ |
817 | |
818 | if (req->method == "GET") { | |
819 | vector<string> metadata; | |
820 | Json::object document; | |
821 | Json::array entries; | |
822 | ||
823 | if (!B.getDomainMetadata(zonename, kind, metadata)) | |
824 | throw HttpNotFoundException(); | |
825 | else if (!isValidMetadataKind(kind, true)) | |
826 | throw ApiException("Unsupported metadata kind '" + kind + "'"); | |
827 | ||
828 | document["type"] = "Metadata"; | |
829 | document["kind"] = kind; | |
830 | ||
831 | for (const string& i : metadata) | |
832 | entries.push_back(i); | |
833 | ||
834 | document["metadata"] = entries; | |
835 | resp->setBody(document); | |
836 | } else if (req->method == "PUT" && !::arg().mustDo("api-readonly")) { | |
837 | auto document = req->json(); | |
838 | ||
839 | if (!isValidMetadataKind(kind, false)) | |
840 | throw ApiException("Unsupported metadata kind '" + kind + "'"); | |
841 | ||
842 | vector<string> vecMetadata; | |
843 | auto& metadata = document["metadata"]; | |
844 | if (!metadata.is_array()) | |
845 | throw ApiException("metadata is not specified or not an array"); | |
846 | ||
847 | for (const auto& i : metadata.array_items()) { | |
848 | if (!i.is_string()) | |
849 | throw ApiException("metadata must be strings"); | |
850 | vecMetadata.push_back(i.string_value()); | |
851 | } | |
852 | ||
853 | if (!B.setDomainMetadata(zonename, kind, vecMetadata)) | |
854 | throw ApiException("Could not update metadata entries for domain '" + zonename.toString() + "'"); | |
855 | ||
856 | Json::object key { | |
857 | { "type", "Metadata" }, | |
858 | { "kind", kind }, | |
859 | { "metadata", metadata } | |
860 | }; | |
861 | ||
862 | resp->setBody(key); | |
863 | } else if (req->method == "DELETE" && !::arg().mustDo("api-readonly")) { | |
864 | if (!isValidMetadataKind(kind, false)) | |
865 | throw ApiException("Unsupported metadata kind '" + kind + "'"); | |
866 | ||
867 | vector<string> md; // an empty vector will do it | |
868 | if (!B.setDomainMetadata(zonename, kind, md)) | |
869 | throw ApiException("Could not delete metadata for domain '" + zonename.toString() + "' (" + kind + ")"); | |
870 | } else | |
871 | throw HttpMethodNotAllowedException(); | |
872 | } | |
873 | ||
60b0a236 BZ |
874 | static void apiZoneCryptokeysGET(DNSName zonename, int inquireKeyId, HttpResponse *resp, DNSSECKeeper *dk) { |
875 | DNSSECKeeper::keyset_t keyset=dk->getKeys(zonename, false); | |
4b7f120a | 876 | |
997cab68 BZ |
877 | bool inquireSingleKey = inquireKeyId >= 0; |
878 | ||
24afabad | 879 | Json::array doc; |
29704f66 | 880 | for(const auto& value : keyset) { |
997cab68 | 881 | if (inquireSingleKey && (unsigned)inquireKeyId != value.second.id) { |
29704f66 | 882 | continue; |
38809e97 | 883 | } |
24afabad | 884 | |
b6bd795c | 885 | string keyType; |
60b0a236 | 886 | switch (value.second.keyType) { |
b6bd795c PL |
887 | case DNSSECKeeper::KSK: keyType="ksk"; break; |
888 | case DNSSECKeeper::ZSK: keyType="zsk"; break; | |
889 | case DNSSECKeeper::CSK: keyType="csk"; break; | |
890 | } | |
891 | ||
24afabad | 892 | Json::object key { |
997cab68 BZ |
893 | { "type", "Cryptokey" }, |
894 | { "id", (int)value.second.id }, | |
895 | { "active", value.second.active }, | |
896 | { "keytype", keyType }, | |
897 | { "flags", (uint16_t)value.first.d_flags }, | |
5d9c6182 PL |
898 | { "dnskey", value.first.getDNSKEY().getZoneRepresentation() }, |
899 | { "algorithm", DNSSECKeeper::algorithm2name(value.first.d_algorithm) }, | |
900 | { "bits", value.first.getKey()->getBits() } | |
24afabad CH |
901 | }; |
902 | ||
b6bd795c | 903 | if (value.second.keyType == DNSSECKeeper::KSK || value.second.keyType == DNSSECKeeper::CSK) { |
24afabad | 904 | Json::array dses; |
8455425c | 905 | for(const uint8_t keyid : { DNSSECKeeper::SHA1, DNSSECKeeper::SHA256, DNSSECKeeper::GOST, DNSSECKeeper::SHA384 }) |
997cab68 BZ |
906 | try { |
907 | dses.push_back(makeDSFromDNSKey(zonename, value.first.getDNSKEY(), keyid).getZoneRepresentation()); | |
908 | } catch (...) {} | |
24afabad | 909 | key["ds"] = dses; |
4b7f120a | 910 | } |
29704f66 CH |
911 | |
912 | if (inquireSingleKey) { | |
913 | key["privatekey"] = value.first.getKey()->convertToISC(); | |
914 | resp->setBody(key); | |
915 | return; | |
916 | } | |
24afabad | 917 | doc.push_back(key); |
4b7f120a MS |
918 | } |
919 | ||
29704f66 CH |
920 | if (inquireSingleKey) { |
921 | // we came here because we couldn't find the requested key. | |
922 | throw HttpNotFoundException(); | |
923 | } | |
4b7f120a | 924 | resp->setBody(doc); |
997cab68 BZ |
925 | |
926 | } | |
927 | ||
928 | /* | |
929 | * This method handles DELETE requests for URL /api/v1/servers/:server_id/zones/:zone_name/cryptokeys/:cryptokey_id . | |
930 | * It deletes a key from :zone_name specified by :cryptokey_id. | |
931 | * Server Answers: | |
60b0a236 BZ |
932 | * Case 1: the backend returns true on removal. This means the key is gone. |
933 | * The server returns 200 OK, no body. | |
955cbfd0 | 934 | * Case 2: the backend returns false on removal. An error occurred. |
60b0a236 | 935 | * The sever returns 422 Unprocessable Entity with message "Could not DELETE :cryptokey_id". |
997cab68 | 936 | * */ |
60b0a236 BZ |
937 | static void apiZoneCryptokeysDELETE(DNSName zonename, int inquireKeyId, HttpRequest *req, HttpResponse *resp, DNSSECKeeper *dk) { |
938 | if (dk->removeKey(zonename, inquireKeyId)) { | |
939 | resp->body = ""; | |
940 | resp->status = 200; | |
997cab68 BZ |
941 | } else { |
942 | resp->setErrorResult("Could not DELETE " + req->parameters["key_id"], 422); | |
943 | } | |
944 | } | |
945 | ||
946 | /* | |
947 | * This method adds a key to a zone by generate it or content parameter. | |
948 | * Parameter: | |
949 | * { | |
5d9c6182 | 950 | * "privatekey" : "key The format used is compatible with BIND and NSD/LDNS" <string> |
997cab68 BZ |
951 | * "keytype" : "ksk|zsk" <string> |
952 | * "active" : "true|false" <value> | |
5d9c6182 | 953 | * "algorithm" : "key generation algorithm name as default"<string> https://doc.powerdns.com/md/authoritative/dnssec/#supported-algorithms |
997cab68 BZ |
954 | * "bits" : number of bits <int> |
955 | * } | |
956 | * | |
957 | * Response: | |
958 | * Case 1: keytype isn't ksk|zsk | |
959 | * The server returns 422 Unprocessable Entity {"error" : "Invalid keytype 'keytype'"} | |
60b0a236 BZ |
960 | * Case 2: 'bits' must be a positive integer value. |
961 | * The server returns 422 Unprocessable Entity {"error" : "'bits' must be a positive integer value."} | |
5d9c6182 | 962 | * Case 3: The "algorithm" isn't supported |
997cab68 | 963 | * The server returns 422 Unprocessable Entity {"error" : "Unknown algorithm: 'algo'"} |
60b0a236 | 964 | * Case 4: Algorithm <= 10 and no bits were passed |
997cab68 | 965 | * The server returns 422 Unprocessable Entity {"error" : "Creating an algorithm algo key requires the size (in bits) to be passed"} |
60b0a236 BZ |
966 | * Case 5: The wrong keysize was passed |
967 | * The server returns 422 Unprocessable Entity {"error" : "The algorithm does not support the given bit size."} | |
968 | * Case 6: If the server cant guess the keysize | |
969 | * The server returns 422 Unprocessable Entity {"error" : "Can not guess key size for algorithm"} | |
970 | * Case 7: The key-creation failed | |
997cab68 | 971 | * The server returns 422 Unprocessable Entity {"error" : "Adding key failed, perhaps DNSSEC not enabled in configuration?"} |
60b0a236 BZ |
972 | * Case 8: The key in content has the wrong format |
973 | * The server returns 422 Unprocessable Entity {"error" : "Key could not be parsed. Make sure your key format is correct."} | |
974 | * Case 9: The wrong combination of fields is submitted | |
975 | * The server returns 422 Unprocessable Entity {"error" : "Either you submit just the 'content' field or you leave 'content' empty and submit the other fields."} | |
976 | * Case 10: No content and everything was fine | |
977 | * The server returns 201 Created and all public data about the new cryptokey | |
978 | * Case 11: With specified content | |
979 | * The server returns 201 Created and all public data about the added cryptokey | |
997cab68 BZ |
980 | */ |
981 | ||
60b0a236 | 982 | static void apiZoneCryptokeysPOST(DNSName zonename, HttpRequest *req, HttpResponse *resp, DNSSECKeeper *dk) { |
997cab68 | 983 | auto document = req->json(); |
5d9c6182 PL |
984 | string privatekey_fieldname = "privatekey"; |
985 | auto privatekey = document["privatekey"]; | |
986 | if (privatekey.is_null()) { | |
987 | // Fallback to the old "content" behaviour | |
988 | privatekey = document["content"]; | |
989 | privatekey_fieldname = "content"; | |
990 | } | |
997cab68 | 991 | bool active = boolFromJson(document, "active", false); |
997cab68 | 992 | bool keyOrZone; |
60b0a236 | 993 | |
eefd15b3 | 994 | if (stringFromJson(document, "keytype") == "ksk" || stringFromJson(document, "keytype") == "csk") { |
997cab68 BZ |
995 | keyOrZone = true; |
996 | } else if (stringFromJson(document, "keytype") == "zsk") { | |
997 | keyOrZone = false; | |
998 | } else { | |
999 | throw ApiException("Invalid keytype " + stringFromJson(document, "keytype")); | |
1000 | } | |
1001 | ||
b727e19b | 1002 | int64_t insertedId = -1; |
997cab68 | 1003 | |
5d9c6182 | 1004 | if (privatekey.is_null()) { |
43215ca6 | 1005 | int bits = keyOrZone ? ::arg().asNum("default-ksk-size") : ::arg().asNum("default-zsk-size"); |
60b0a236 BZ |
1006 | auto docbits = document["bits"]; |
1007 | if (!docbits.is_null()) { | |
1008 | if (!docbits.is_number() || (fmod(docbits.number_value(), 1.0) != 0) || docbits.int_value() < 0) { | |
1009 | throw ApiException("'bits' must be a positive integer value"); | |
1010 | } else { | |
1011 | bits = docbits.int_value(); | |
1012 | } | |
1013 | } | |
43215ca6 | 1014 | int algorithm = DNSSECKeeper::shorthand2algorithm(keyOrZone ? ::arg()["default-ksk-algorithm"] : ::arg()["default-zsk-algorithm"]); |
5d9c6182 | 1015 | auto providedAlgo = document["algorithm"]; |
997cab68 | 1016 | if (providedAlgo.is_string()) { |
60b0a236 BZ |
1017 | algorithm = DNSSECKeeper::shorthand2algorithm(providedAlgo.string_value()); |
1018 | if (algorithm == -1) | |
997cab68 | 1019 | throw ApiException("Unknown algorithm: " + providedAlgo.string_value()); |
997cab68 BZ |
1020 | } else if (providedAlgo.is_number()) { |
1021 | algorithm = providedAlgo.int_value(); | |
60b0a236 BZ |
1022 | } else if (!providedAlgo.is_null()) { |
1023 | throw ApiException("Unknown algorithm: " + providedAlgo.string_value()); | |
997cab68 BZ |
1024 | } |
1025 | ||
60b0a236 | 1026 | try { |
b727e19b RG |
1027 | if (!dk->addKey(zonename, keyOrZone, algorithm, insertedId, bits, active)) { |
1028 | throw ApiException("Adding key failed, perhaps DNSSEC not enabled in configuration?"); | |
1029 | } | |
60b0a236 | 1030 | } catch (std::runtime_error& error) { |
997cab68 BZ |
1031 | throw ApiException(error.what()); |
1032 | } | |
997cab68 BZ |
1033 | if (insertedId < 0) |
1034 | throw ApiException("Adding key failed, perhaps DNSSEC not enabled in configuration?"); | |
5d9c6182 PL |
1035 | } else if (document["bits"].is_null() && document["algorithm"].is_null()) { |
1036 | auto keyData = stringFromJson(document, privatekey_fieldname); | |
997cab68 BZ |
1037 | DNSKEYRecordContent dkrc; |
1038 | DNSSECPrivateKey dpk; | |
60b0a236 | 1039 | try { |
997cab68 BZ |
1040 | shared_ptr<DNSCryptoKeyEngine> dke(DNSCryptoKeyEngine::makeFromISCString(dkrc, keyData)); |
1041 | dpk.d_algorithm = dkrc.d_algorithm; | |
5d9c6182 | 1042 | // TODO remove in 4.2.0 |
902c4e9c CH |
1043 | if(dpk.d_algorithm == DNSSECKeeper::RSASHA1NSEC3SHA1) |
1044 | dpk.d_algorithm = DNSSECKeeper::RSASHA1; | |
997cab68 BZ |
1045 | |
1046 | if (keyOrZone) | |
1047 | dpk.d_flags = 257; | |
1048 | else | |
1049 | dpk.d_flags = 256; | |
1050 | ||
1051 | dpk.setKey(dke); | |
997cab68 | 1052 | } |
60b0a236 BZ |
1053 | catch (std::runtime_error& error) { |
1054 | throw ApiException("Key could not be parsed. Make sure your key format is correct."); | |
1055 | } try { | |
b727e19b RG |
1056 | if (!dk->addKey(zonename, dpk,insertedId, active)) { |
1057 | throw ApiException("Adding key failed, perhaps DNSSEC not enabled in configuration?"); | |
1058 | } | |
60b0a236 | 1059 | } catch (std::runtime_error& error) { |
997cab68 BZ |
1060 | throw ApiException(error.what()); |
1061 | } | |
1062 | if (insertedId < 0) | |
1063 | throw ApiException("Adding key failed, perhaps DNSSEC not enabled in configuration?"); | |
60b0a236 | 1064 | } else { |
5d9c6182 | 1065 | throw ApiException("Either you submit just the 'privatekey' field or you leave 'privatekey' empty and submit the other fields."); |
997cab68 | 1066 | } |
60b0a236 | 1067 | apiZoneCryptokeysGET(zonename, insertedId, resp, dk); |
997cab68 | 1068 | resp->status = 201; |
60b0a236 | 1069 | } |
997cab68 BZ |
1070 | |
1071 | /* | |
1072 | * This method handles PUT (execute) requests for URL /api/v1/servers/:server_id/zones/:zone_name/cryptokeys/:cryptokey_id . | |
1073 | * It de/activates a key from :zone_name specified by :cryptokey_id. | |
1074 | * Server Answers: | |
60b0a236 | 1075 | * Case 1: invalid JSON data |
997cab68 | 1076 | * The server returns 400 Bad Request |
60b0a236 BZ |
1077 | * Case 2: the backend returns true on de/activation. This means the key is de/active. |
1078 | * The server returns 204 No Content | |
955cbfd0 | 1079 | * Case 3: the backend returns false on de/activation. An error occurred. |
997cab68 BZ |
1080 | * The sever returns 422 Unprocessable Entity with message "Could not de/activate Key: :cryptokey_id in Zone: :zone_name" |
1081 | * */ | |
60b0a236 | 1082 | static void apiZoneCryptokeysPUT(DNSName zonename, int inquireKeyId, HttpRequest *req, HttpResponse *resp, DNSSECKeeper *dk) { |
997cab68 BZ |
1083 | //throws an exception if the Body is empty |
1084 | auto document = req->json(); | |
1085 | //throws an exception if the key does not exist or is not a bool | |
1086 | bool active = boolFromJson(document, "active"); | |
60b0a236 BZ |
1087 | if (active) { |
1088 | if (!dk->activateKey(zonename, inquireKeyId)) { | |
997cab68 BZ |
1089 | resp->setErrorResult("Could not activate Key: " + req->parameters["key_id"] + " in Zone: " + zonename.toString(), 422); |
1090 | return; | |
1091 | } | |
1092 | } else { | |
60b0a236 | 1093 | if (!dk->deactivateKey(zonename, inquireKeyId)) { |
997cab68 BZ |
1094 | resp->setErrorResult("Could not deactivate Key: " + req->parameters["key_id"] + " in Zone: " + zonename.toString(), 422); |
1095 | return; | |
1096 | } | |
1097 | } | |
60b0a236 BZ |
1098 | resp->body = ""; |
1099 | resp->status = 204; | |
1100 | return; | |
997cab68 BZ |
1101 | } |
1102 | ||
1103 | /* | |
1104 | * This method chooses the right functionality for the request. It also checks for a cryptokey_id which has to be passed | |
1105 | * by URL /api/v1/servers/:server_id/zones/:zone_name/cryptokeys/:cryptokey_id . | |
1106 | * If the the HTTP-request-method isn't supported, the function returns a response with the 405 code (method not allowed). | |
1107 | * */ | |
1108 | static void apiZoneCryptokeys(HttpRequest *req, HttpResponse *resp) { | |
1109 | DNSName zonename = apiZoneIdToName(req->parameters["id"]); | |
1110 | ||
60b0a236 BZ |
1111 | UeberBackend B; |
1112 | DNSSECKeeper dk(&B); | |
1113 | DomainInfo di; | |
77bfe8de PL |
1114 | if (!B.getDomainInfo(zonename, di)) { |
1115 | throw HttpNotFoundException(); | |
1116 | } | |
60b0a236 | 1117 | |
997cab68 BZ |
1118 | int inquireKeyId = -1; |
1119 | if (req->parameters.count("key_id")) { | |
1120 | inquireKeyId = std::stoi(req->parameters["key_id"]); | |
1121 | } | |
1122 | ||
1123 | if (req->method == "GET") { | |
60b0a236 | 1124 | apiZoneCryptokeysGET(zonename, inquireKeyId, resp, &dk); |
1abc1df5 | 1125 | } else if (req->method == "DELETE" && !::arg().mustDo("api-readonly")) { |
60b0a236 BZ |
1126 | if (inquireKeyId == -1) |
1127 | throw HttpBadRequestException(); | |
1128 | apiZoneCryptokeysDELETE(zonename, inquireKeyId, req, resp, &dk); | |
1abc1df5 | 1129 | } else if (req->method == "POST" && !::arg().mustDo("api-readonly")) { |
60b0a236 | 1130 | apiZoneCryptokeysPOST(zonename, req, resp, &dk); |
1abc1df5 | 1131 | } else if (req->method == "PUT" && !::arg().mustDo("api-readonly")) { |
60b0a236 BZ |
1132 | if (inquireKeyId == -1) |
1133 | throw HttpBadRequestException(); | |
1134 | apiZoneCryptokeysPUT(zonename, inquireKeyId, req, resp, &dk); | |
997cab68 BZ |
1135 | } else { |
1136 | throw HttpMethodNotAllowedException(); //Returns method not allowed | |
1137 | } | |
4b7f120a MS |
1138 | } |
1139 | ||
1f68b185 | 1140 | static void gatherRecordsFromZone(const std::string& zonestring, vector<DNSResourceRecord>& new_records, DNSName zonename) { |
0f0e73fe MS |
1141 | DNSResourceRecord rr; |
1142 | vector<string> zonedata; | |
1f68b185 | 1143 | stringtok(zonedata, zonestring, "\r\n"); |
0f0e73fe MS |
1144 | |
1145 | ZoneParserTNG zpt(zonedata, zonename); | |
1146 | ||
1147 | bool seenSOA=false; | |
1148 | ||
1149 | string comment = "Imported via the API"; | |
1150 | ||
1151 | try { | |
1152 | while(zpt.get(rr, &comment)) { | |
1153 | if(seenSOA && rr.qtype.getCode() == QType::SOA) | |
1154 | continue; | |
1155 | if(rr.qtype.getCode() == QType::SOA) | |
1156 | seenSOA=true; | |
1157 | ||
0f0e73fe MS |
1158 | new_records.push_back(rr); |
1159 | } | |
1160 | } | |
1161 | catch(std::exception& ae) { | |
1af62161 | 1162 | throw ApiException("An error occurred while parsing the zonedata: "+string(ae.what())); |
0f0e73fe MS |
1163 | } |
1164 | } | |
1165 | ||
e3675a8a CH |
1166 | /** Throws ApiException if records with duplicate name/type/content are present. |
1167 | * NOTE: sorts records in-place. | |
1168 | */ | |
1169 | static void checkDuplicateRecords(vector<DNSResourceRecord>& records) { | |
1170 | sort(records.begin(), records.end(), | |
1171 | [](const DNSResourceRecord& rec_a, const DNSResourceRecord& rec_b) -> bool { | |
1172 | return rec_a.qname.toString() > rec_b.qname.toString() || \ | |
1173 | rec_a.qtype.getCode() > rec_b.qtype.getCode() || \ | |
1174 | rec_a.content < rec_b.content; | |
1175 | } | |
1176 | ); | |
1177 | DNSResourceRecord previous; | |
1178 | for(const auto& rec : records) { | |
1179 | if (previous.qtype == rec.qtype && previous.qname == rec.qname && previous.content == rec.content) { | |
1180 | throw ApiException("Duplicate record in RRset " + rec.qname.toString() + " IN " + rec.qtype.getName() + " with content \"" + rec.content + "\""); | |
1181 | } | |
1182 | previous = rec; | |
1183 | } | |
1184 | } | |
1185 | ||
80d59cd1 | 1186 | static void apiServerZones(HttpRequest* req, HttpResponse* resp) { |
e2dba705 | 1187 | UeberBackend B; |
53942520 | 1188 | DNSSECKeeper dk(&B); |
d07bf7ff | 1189 | if (req->method == "POST" && !::arg().mustDo("api-readonly")) { |
e2dba705 | 1190 | DomainInfo di; |
1f68b185 | 1191 | auto document = req->json(); |
c576d0c5 | 1192 | DNSName zonename = apiNameToDNSName(stringFromJson(document, "name")); |
1d6b70f9 | 1193 | apiCheckNameAllowedCharacters(zonename.toString()); |
e3675a8a | 1194 | zonename.makeUsLowerCase(); |
4ebf78b1 | 1195 | |
1d6b70f9 | 1196 | bool exists = B.getDomainInfo(zonename, di); |
e2dba705 | 1197 | if(exists) |
1d6b70f9 | 1198 | throw ApiException("Domain '"+zonename.toString()+"' already exists"); |
e2dba705 | 1199 | |
bb9fd223 | 1200 | // validate 'kind' is set |
4bdff352 | 1201 | DomainInfo::DomainKind zonekind = DomainInfo::stringToKind(stringFromJson(document, "kind")); |
bb9fd223 | 1202 | |
6754ef71 CH |
1203 | string zonestring = document["zone"].string_value(); |
1204 | auto rrsets = document["rrsets"]; | |
1205 | if (rrsets.is_array() && zonestring != "") | |
1206 | throw ApiException("You cannot give rrsets AND zone data as text"); | |
0f0e73fe | 1207 | |
1f68b185 CH |
1208 | auto nameservers = document["nameservers"]; |
1209 | if (!nameservers.is_array() && zonekind != DomainInfo::Slave) | |
f63168e6 | 1210 | throw ApiException("Nameservers list must be given (but can be empty if NS records are supplied)"); |
e2dba705 | 1211 | |
f63168e6 | 1212 | string soa_edit_api_kind; |
1f68b185 CH |
1213 | if (document["soa_edit_api"].is_string()) { |
1214 | soa_edit_api_kind = document["soa_edit_api"].string_value(); | |
a6448d95 CH |
1215 | } |
1216 | else { | |
1217 | soa_edit_api_kind = "DEFAULT"; | |
1218 | } | |
1f68b185 | 1219 | string soa_edit_kind = document["soa_edit"].string_value(); |
e90b4e38 | 1220 | |
f63168e6 CH |
1221 | // if records/comments are given, load and check them |
1222 | bool have_soa = false; | |
33e6c3e9 | 1223 | bool have_zone_ns = false; |
f63168e6 CH |
1224 | vector<DNSResourceRecord> new_records; |
1225 | vector<Comment> new_comments; | |
1226 | vector<DNSResourceRecord> new_ptrs; | |
0f0e73fe | 1227 | |
6754ef71 CH |
1228 | if (rrsets.is_array()) { |
1229 | for (const auto& rrset : rrsets.array_items()) { | |
1230 | DNSName qname = apiNameToDNSName(stringFromJson(rrset, "name")); | |
1231 | apiCheckQNameAllowedCharacters(qname.toString()); | |
1232 | QType qtype; | |
1233 | qtype = stringFromJson(rrset, "type"); | |
1234 | if (qtype.getCode() == 0) { | |
1235 | throw ApiException("RRset "+qname.toString()+" IN "+stringFromJson(rrset, "type")+": unknown type given"); | |
1236 | } | |
1237 | if (rrset["records"].is_array()) { | |
1238 | int ttl = intFromJson(rrset, "ttl"); | |
1239 | gatherRecords(rrset, qname, qtype, ttl, new_records, new_ptrs); | |
1240 | } | |
1241 | if (rrset["comments"].is_array()) { | |
1242 | gatherComments(rrset, qname, qtype, new_comments); | |
1243 | } | |
1244 | } | |
0f0e73fe | 1245 | } else if (zonestring != "") { |
1f68b185 | 1246 | gatherRecordsFromZone(zonestring, new_records, zonename); |
0f0e73fe MS |
1247 | } |
1248 | ||
1f68b185 | 1249 | for(auto& rr : new_records) { |
e3675a8a | 1250 | rr.qname.makeUsLowerCase(); |
1d6b70f9 | 1251 | if (!rr.qname.isPartOf(zonename) && rr.qname != zonename) |
561434a6 | 1252 | throw ApiException("RRset "+rr.qname.toString()+" IN "+rr.qtype.getName()+": Name is out of zone"); |
cb9b5901 | 1253 | apiCheckQNameAllowedCharacters(rr.qname.toString()); |
f63168e6 | 1254 | |
1d6b70f9 | 1255 | if (rr.qtype.getCode() == QType::SOA && rr.qname==zonename) { |
f63168e6 | 1256 | have_soa = true; |
a6448d95 | 1257 | increaseSOARecord(rr, soa_edit_api_kind, soa_edit_kind); |
f63168e6 | 1258 | } |
33e6c3e9 CH |
1259 | if (rr.qtype.getCode() == QType::NS && rr.qname==zonename) { |
1260 | have_zone_ns = true; | |
1261 | } | |
f63168e6 | 1262 | } |
f7bfeb30 CH |
1263 | |
1264 | // synthesize RRs as needed | |
1265 | DNSResourceRecord autorr; | |
1d6b70f9 | 1266 | autorr.qname = zonename; |
f7bfeb30 CH |
1267 | autorr.auth = 1; |
1268 | autorr.ttl = ::arg().asNum("default-ttl"); | |
e2dba705 | 1269 | |
4de11a54 | 1270 | if (!have_soa && zonekind != DomainInfo::Slave) { |
f63168e6 | 1271 | // synthesize a SOA record so the zone "really" exists |
13f9e280 | 1272 | string soa = (boost::format("%s %s %ul") |
1d6b70f9 CH |
1273 | % ::arg()["default-soa-name"] |
1274 | % (::arg().isEmpty("default-soa-mail") ? (DNSName("hostmaster.") + zonename).toString() : ::arg()["default-soa-mail"]) | |
1f68b185 | 1275 | % document["serial"].int_value() |
1d6b70f9 | 1276 | ).str(); |
f63168e6 | 1277 | SOAData sd; |
1d6b70f9 | 1278 | fillSOAData(soa, sd); // fills out default values for us |
13f9e280 CH |
1279 | autorr.qtype = QType::SOA; |
1280 | autorr.content = makeSOAContent(sd)->getZoneRepresentation(true); | |
f7bfeb30 CH |
1281 | increaseSOARecord(autorr, soa_edit_api_kind, soa_edit_kind); |
1282 | new_records.push_back(autorr); | |
f63168e6 CH |
1283 | } |
1284 | ||
1285 | // create NS records if nameservers are given | |
1f68b185 CH |
1286 | for (auto value : nameservers.array_items()) { |
1287 | string nameserver = value.string_value(); | |
1288 | if (nameserver.empty()) | |
1289 | throw ApiException("Nameservers must be non-empty strings"); | |
1290 | if (!isCanonical(nameserver)) | |
1291 | throw ApiException("Nameserver is not canonical: '" + nameserver + "'"); | |
1292 | try { | |
1293 | // ensure the name parses | |
8f955653 | 1294 | autorr.content = DNSName(nameserver).toStringRootDot(); |
1f68b185 CH |
1295 | } catch (...) { |
1296 | throw ApiException("Unable to parse DNS Name for NS '" + nameserver + "'"); | |
4bdff352 | 1297 | } |
13f9e280 | 1298 | autorr.qtype = QType::NS; |
1f68b185 | 1299 | new_records.push_back(autorr); |
33e6c3e9 CH |
1300 | if (have_zone_ns) { |
1301 | throw ApiException("Nameservers list MUST NOT be mixed with zone-level NS in rrsets"); | |
1302 | } | |
e2dba705 CH |
1303 | } |
1304 | ||
e3675a8a CH |
1305 | checkDuplicateRecords(new_records); |
1306 | ||
986e4858 PL |
1307 | if (boolFromJson(document, "dnssec", false)) { |
1308 | checkDefaultDNSSECAlgos(); | |
1309 | ||
1310 | if(document["nsec3param"].string_value().length() > 0) { | |
1311 | NSEC3PARAMRecordContent ns3pr(document["nsec3param"].string_value()); | |
1312 | string error_msg = ""; | |
1313 | if (!dk.checkNSEC3PARAM(ns3pr, error_msg)) { | |
1314 | throw ApiException("NSEC3PARAMs provided for zone '"+zonename.toString()+"' are invalid. " + error_msg); | |
1315 | } | |
1316 | } | |
1317 | } | |
1318 | ||
f63168e6 | 1319 | // no going back after this |
1d6b70f9 CH |
1320 | if(!B.createDomain(zonename)) |
1321 | throw ApiException("Creating domain '"+zonename.toString()+"' failed"); | |
f63168e6 | 1322 | |
1d6b70f9 CH |
1323 | if(!B.getDomainInfo(zonename, di)) |
1324 | throw ApiException("Creating domain '"+zonename.toString()+"' failed: lookup of domain ID failed"); | |
f63168e6 | 1325 | |
9440a9f0 CH |
1326 | // updateDomainSettingsFromDocument does NOT fill out the default we've established above. |
1327 | if (!soa_edit_api_kind.empty()) { | |
1328 | di.backend->setDomainMetadataOne(zonename, "SOA-EDIT-API", soa_edit_api_kind); | |
1329 | } | |
1330 | ||
1d6b70f9 | 1331 | di.backend->startTransaction(zonename, di.id); |
f63168e6 | 1332 | |
abb873ee | 1333 | for(auto rr : new_records) { |
f63168e6 | 1334 | rr.domain_id = di.id; |
c9b43446 | 1335 | di.backend->feedRecord(rr, DNSName()); |
e2dba705 | 1336 | } |
1d6b70f9 | 1337 | for(Comment& c : new_comments) { |
f63168e6 CH |
1338 | c.domain_id = di.id; |
1339 | di.backend->feedComment(c); | |
1340 | } | |
e2dba705 | 1341 | |
986e4858 | 1342 | updateDomainSettingsFromDocument(B, di, zonename, document); |
e2dba705 | 1343 | |
f63168e6 CH |
1344 | di.backend->commitTransaction(); |
1345 | ||
3fe7c7d6 CH |
1346 | storeChangedPTRs(B, new_ptrs); |
1347 | ||
986e4858 | 1348 | fillZone(zonename, resp, shouldDoRRSets(req)); |
64a36f0d | 1349 | resp->status = 201; |
e2dba705 CH |
1350 | return; |
1351 | } | |
1352 | ||
c67bf8c5 CH |
1353 | if(req->method != "GET") |
1354 | throw HttpMethodNotAllowedException(); | |
1355 | ||
c67bf8c5 | 1356 | vector<DomainInfo> domains; |
cea26350 | 1357 | B.getAllDomains(&domains, true); // incl. disabled |
c67bf8c5 | 1358 | |
62a9a74c CH |
1359 | Json::array doc; |
1360 | for(const DomainInfo& di : domains) { | |
ce846be6 | 1361 | doc.push_back(getZoneInfo(di, &dk)); |
c67bf8c5 | 1362 | } |
669822d0 | 1363 | resp->setBody(doc); |
c67bf8c5 CH |
1364 | } |
1365 | ||
05776d2f | 1366 | static void apiServerZoneDetail(HttpRequest* req, HttpResponse* resp) { |
290a083d | 1367 | DNSName zonename = apiZoneIdToName(req->parameters["id"]); |
05776d2f | 1368 | |
77bfe8de PL |
1369 | UeberBackend B; |
1370 | DomainInfo di; | |
1371 | if (!B.getDomainInfo(zonename, di)) { | |
1372 | throw HttpNotFoundException(); | |
1373 | } | |
1374 | ||
d07bf7ff | 1375 | if(req->method == "PUT" && !::arg().mustDo("api-readonly")) { |
7c0ba3d2 | 1376 | // update domain settings |
7c0ba3d2 | 1377 | |
986e4858 | 1378 | updateDomainSettingsFromDocument(B, di, zonename, req->json()); |
7c0ba3d2 | 1379 | |
f0e76cee CH |
1380 | resp->body = ""; |
1381 | resp->status = 204; // No Content, but indicate success | |
7c0ba3d2 CH |
1382 | return; |
1383 | } | |
d07bf7ff | 1384 | else if(req->method == "DELETE" && !::arg().mustDo("api-readonly")) { |
a462a01d | 1385 | // delete domain |
a462a01d | 1386 | if(!di.backend->deleteDomain(zonename)) |
290a083d | 1387 | throw ApiException("Deleting domain '"+zonename.toString()+"' failed: backend delete failed/unsupported"); |
a462a01d CH |
1388 | |
1389 | // empty body on success | |
1390 | resp->body = ""; | |
37663c3b | 1391 | resp->status = 204; // No Content: declare that the zone is gone now |
a462a01d | 1392 | return; |
d07bf7ff | 1393 | } else if (req->method == "PATCH" && !::arg().mustDo("api-readonly")) { |
d708640f | 1394 | patchZone(req, resp); |
6cc98ddf CH |
1395 | return; |
1396 | } else if (req->method == "GET") { | |
986e4858 | 1397 | fillZone(zonename, resp, shouldDoRRSets(req)); |
6cc98ddf | 1398 | return; |
a462a01d | 1399 | } |
6cc98ddf | 1400 | throw HttpMethodNotAllowedException(); |
05776d2f CH |
1401 | } |
1402 | ||
a83004d3 | 1403 | static void apiServerZoneExport(HttpRequest* req, HttpResponse* resp) { |
290a083d | 1404 | DNSName zonename = apiZoneIdToName(req->parameters["id"]); |
a83004d3 CH |
1405 | |
1406 | if(req->method != "GET") | |
1407 | throw HttpMethodNotAllowedException(); | |
1408 | ||
1409 | ostringstream ss; | |
1410 | ||
1411 | UeberBackend B; | |
1412 | DomainInfo di; | |
77bfe8de PL |
1413 | if (!B.getDomainInfo(zonename, di)) { |
1414 | throw HttpNotFoundException(); | |
1415 | } | |
a83004d3 CH |
1416 | |
1417 | DNSResourceRecord rr; | |
1418 | SOAData sd; | |
1419 | di.backend->list(zonename, di.id); | |
1420 | while(di.backend->get(rr)) { | |
1421 | if (!rr.qtype.getCode()) | |
1422 | continue; // skip empty non-terminals | |
1423 | ||
a83004d3 | 1424 | ss << |
675fa24c | 1425 | rr.qname.toString() << "\t" << |
a83004d3 CH |
1426 | rr.ttl << "\t" << |
1427 | rr.qtype.getName() << "\t" << | |
1d6b70f9 | 1428 | makeApiRecordContent(rr.qtype, rr.content) << |
a83004d3 CH |
1429 | endl; |
1430 | } | |
1431 | ||
1432 | if (req->accept_json) { | |
41873e7c | 1433 | resp->setBody(Json::object { { "zone", ss.str() } }); |
a83004d3 CH |
1434 | } else { |
1435 | resp->headers["Content-Type"] = "text/plain; charset=us-ascii"; | |
1436 | resp->body = ss.str(); | |
1437 | } | |
1438 | } | |
1439 | ||
a426cb89 | 1440 | static void apiServerZoneAxfrRetrieve(HttpRequest* req, HttpResponse* resp) { |
290a083d | 1441 | DNSName zonename = apiZoneIdToName(req->parameters["id"]); |
a426cb89 | 1442 | |
136965be | 1443 | if(req->method != "PUT" || ::arg().mustDo("api-readonly")) |
a426cb89 CH |
1444 | throw HttpMethodNotAllowedException(); |
1445 | ||
1446 | UeberBackend B; | |
1447 | DomainInfo di; | |
77bfe8de PL |
1448 | if (!B.getDomainInfo(zonename, di)) { |
1449 | throw HttpNotFoundException(); | |
1450 | } | |
a426cb89 CH |
1451 | |
1452 | if(di.masters.empty()) | |
290a083d | 1453 | throw ApiException("Domain '"+zonename.toString()+"' is not a slave domain (or has no master defined)"); |
a426cb89 CH |
1454 | |
1455 | random_shuffle(di.masters.begin(), di.masters.end()); | |
1456 | Communicator.addSuckRequest(zonename, di.masters.front()); | |
692829aa | 1457 | resp->setSuccessResult("Added retrieval request for '"+zonename.toString()+"' from master "+di.masters.front()); |
a426cb89 CH |
1458 | } |
1459 | ||
1460 | static void apiServerZoneNotify(HttpRequest* req, HttpResponse* resp) { | |
290a083d | 1461 | DNSName zonename = apiZoneIdToName(req->parameters["id"]); |
a426cb89 | 1462 | |
136965be | 1463 | if(req->method != "PUT" || ::arg().mustDo("api-readonly")) |
a426cb89 CH |
1464 | throw HttpMethodNotAllowedException(); |
1465 | ||
1466 | UeberBackend B; | |
1467 | DomainInfo di; | |
77bfe8de PL |
1468 | if (!B.getDomainInfo(zonename, di)) { |
1469 | throw HttpNotFoundException(); | |
1470 | } | |
a426cb89 CH |
1471 | |
1472 | if(!Communicator.notifyDomain(zonename)) | |
1473 | throw ApiException("Failed to add to the queue - see server log"); | |
1474 | ||
692829aa | 1475 | resp->setSuccessResult("Notification queued"); |
a426cb89 CH |
1476 | } |
1477 | ||
4bc8379e PL |
1478 | static void apiServerZoneRectify(HttpRequest* req, HttpResponse* resp) { |
1479 | DNSName zonename = apiZoneIdToName(req->parameters["id"]); | |
1480 | ||
1481 | if(req->method != "PUT") | |
1482 | throw HttpMethodNotAllowedException(); | |
1483 | ||
1484 | UeberBackend B; | |
1485 | DomainInfo di; | |
77bfe8de PL |
1486 | if (!B.getDomainInfo(zonename, di)) { |
1487 | throw HttpNotFoundException(); | |
1488 | } | |
4bc8379e PL |
1489 | |
1490 | DNSSECKeeper dk(&B); | |
1491 | ||
1492 | if (!dk.isSecuredZone(zonename)) | |
1493 | throw ApiException("Zone '" + zonename.toString() + "' is not DNSSEC signed, not rectifying."); | |
1494 | ||
1495 | if (di.kind == DomainInfo::Slave) | |
1496 | throw ApiException("Zone '" + zonename.toString() + "' is a slave zone, not rectifying."); | |
1497 | ||
1498 | string error_msg = ""; | |
59102608 RG |
1499 | string info; |
1500 | if (!dk.rectifyZone(zonename, error_msg, info, true)) | |
4bc8379e PL |
1501 | throw ApiException("Failed to rectify '" + zonename.toString() + "' " + error_msg); |
1502 | ||
1503 | resp->setSuccessResult("Rectified"); | |
1504 | } | |
1505 | ||
d1587ceb CH |
1506 | static void makePtr(const DNSResourceRecord& rr, DNSResourceRecord* ptr) { |
1507 | if (rr.qtype.getCode() == QType::A) { | |
1508 | uint32_t ip; | |
1509 | if (!IpToU32(rr.content, &ip)) { | |
1510 | throw ApiException("PTR: Invalid IP address given"); | |
1511 | } | |
1d6b70f9 | 1512 | ptr->qname = DNSName((boost::format("%u.%u.%u.%u.in-addr.arpa.") |
d1587ceb CH |
1513 | % ((ip >> 24) & 0xff) |
1514 | % ((ip >> 16) & 0xff) | |
1515 | % ((ip >> 8) & 0xff) | |
1516 | % ((ip ) & 0xff) | |
1d6b70f9 | 1517 | ).str()); |
d1587ceb CH |
1518 | } else if (rr.qtype.getCode() == QType::AAAA) { |
1519 | ComboAddress ca(rr.content); | |
5fb3aa58 | 1520 | char buf[3]; |
d1587ceb | 1521 | ostringstream ss; |
5fb3aa58 CH |
1522 | for (int octet = 0; octet < 16; ++octet) { |
1523 | if (snprintf(buf, sizeof(buf), "%02x", ca.sin6.sin6_addr.s6_addr[octet]) != (sizeof(buf)-1)) { | |
1524 | // this should be impossible: no byte should give more than two digits in hex format | |
1525 | throw PDNSException("Formatting IPv6 address failed"); | |
1526 | } | |
1527 | ss << buf[0] << '.' << buf[1] << '.'; | |
d1587ceb | 1528 | } |
5fb3aa58 CH |
1529 | string tmp = ss.str(); |
1530 | tmp.resize(tmp.size()-1); // remove last dot | |
1531 | // reverse and append arpa domain | |
1d6b70f9 | 1532 | ptr->qname = DNSName(string(tmp.rbegin(), tmp.rend())) + DNSName("ip6.arpa."); |
d1587ceb | 1533 | } else { |
675fa24c | 1534 | throw ApiException("Unsupported PTR source '" + rr.qname.toString() + "' type '" + rr.qtype.getName() + "'"); |
d1587ceb CH |
1535 | } |
1536 | ||
1537 | ptr->qtype = "PTR"; | |
1538 | ptr->ttl = rr.ttl; | |
1539 | ptr->disabled = rr.disabled; | |
8f955653 | 1540 | ptr->content = rr.qname.toStringRootDot(); |
d1587ceb CH |
1541 | } |
1542 | ||
995473c8 CH |
1543 | static void storeChangedPTRs(UeberBackend& B, vector<DNSResourceRecord>& new_ptrs) { |
1544 | for(const DNSResourceRecord& rr : new_ptrs) { | |
995473c8 | 1545 | SOAData sd; |
cec52de6 | 1546 | if (!B.getAuth(rr.qname, QType(QType::PTR), &sd, false)) |
995473c8 CH |
1547 | throw ApiException("Could not find domain for PTR '"+rr.qname.toString()+"' requested for '"+rr.content+"' (while saving)"); |
1548 | ||
1549 | string soa_edit_api_kind; | |
1550 | string soa_edit_kind; | |
1551 | bool soa_changed = false; | |
1552 | DNSResourceRecord soarr; | |
1553 | sd.db->getDomainMetadataOne(sd.qname, "SOA-EDIT-API", soa_edit_api_kind); | |
1554 | sd.db->getDomainMetadataOne(sd.qname, "SOA-EDIT", soa_edit_kind); | |
1555 | if (!soa_edit_api_kind.empty()) { | |
13f9e280 | 1556 | soa_changed = makeIncreasedSOARecord(sd, soa_edit_api_kind, soa_edit_kind, soarr); |
995473c8 CH |
1557 | } |
1558 | ||
1559 | sd.db->startTransaction(sd.qname); | |
1560 | if (!sd.db->replaceRRSet(sd.domain_id, rr.qname, rr.qtype, vector<DNSResourceRecord>(1, rr))) { | |
1561 | sd.db->abortTransaction(); | |
1562 | throw ApiException("PTR-Hosting backend for "+rr.qname.toString()+"/"+rr.qtype.getName()+" does not support editing records."); | |
1563 | } | |
1564 | ||
1565 | if (soa_changed) { | |
1566 | sd.db->replaceRRSet(sd.domain_id, soarr.qname, soarr.qtype, vector<DNSResourceRecord>(1, soarr)); | |
1567 | } | |
1568 | ||
1569 | sd.db->commitTransaction(); | |
bf269e28 | 1570 | purgeAuthCachesExact(rr.qname); |
995473c8 CH |
1571 | } |
1572 | } | |
1573 | ||
d708640f | 1574 | static void patchZone(HttpRequest* req, HttpResponse* resp) { |
b3905a3d CH |
1575 | UeberBackend B; |
1576 | DomainInfo di; | |
290a083d | 1577 | DNSName zonename = apiZoneIdToName(req->parameters["id"]); |
77bfe8de PL |
1578 | if (!B.getDomainInfo(zonename, di)) { |
1579 | throw HttpNotFoundException(); | |
1580 | } | |
b3905a3d | 1581 | |
f63168e6 CH |
1582 | vector<DNSResourceRecord> new_records; |
1583 | vector<Comment> new_comments; | |
d708640f CH |
1584 | vector<DNSResourceRecord> new_ptrs; |
1585 | ||
1f68b185 | 1586 | Json document = req->json(); |
b3905a3d | 1587 | |
1f68b185 CH |
1588 | auto rrsets = document["rrsets"]; |
1589 | if (!rrsets.is_array()) | |
d708640f | 1590 | throw ApiException("No rrsets given in update request"); |
b3905a3d | 1591 | |
d708640f | 1592 | di.backend->startTransaction(zonename); |
6cc98ddf | 1593 | |
d708640f | 1594 | try { |
d29d5db7 | 1595 | string soa_edit_api_kind; |
a6448d95 | 1596 | string soa_edit_kind; |
d29d5db7 | 1597 | di.backend->getDomainMetadataOne(zonename, "SOA-EDIT-API", soa_edit_api_kind); |
a6448d95 | 1598 | di.backend->getDomainMetadataOne(zonename, "SOA-EDIT", soa_edit_kind); |
d29d5db7 CH |
1599 | bool soa_edit_done = false; |
1600 | ||
6754ef71 CH |
1601 | for (const auto& rrset : rrsets.array_items()) { |
1602 | string changetype = toUpper(stringFromJson(rrset, "changetype")); | |
c576d0c5 | 1603 | DNSName qname = apiNameToDNSName(stringFromJson(rrset, "name")); |
cb9b5901 | 1604 | apiCheckQNameAllowedCharacters(qname.toString()); |
6754ef71 | 1605 | QType qtype; |
d708640f | 1606 | qtype = stringFromJson(rrset, "type"); |
6754ef71 CH |
1607 | if (qtype.getCode() == 0) { |
1608 | throw ApiException("RRset "+qname.toString()+" IN "+stringFromJson(rrset, "type")+": unknown type given"); | |
1609 | } | |
d708640f | 1610 | |
d708640f | 1611 | if (changetype == "DELETE") { |
b7f21ab1 | 1612 | // delete all matching qname/qtype RRs (and, implicitly comments). |
d708640f CH |
1613 | if (!di.backend->replaceRRSet(di.id, qname, qtype, vector<DNSResourceRecord>())) { |
1614 | throw ApiException("Hosting backend does not support editing records."); | |
6cc98ddf | 1615 | } |
d708640f CH |
1616 | } |
1617 | else if (changetype == "REPLACE") { | |
1d6b70f9 | 1618 | // we only validate for REPLACE, as DELETE can be used to "fix" out of zone records. |
e325f20c | 1619 | if (!qname.isPartOf(zonename) && qname != zonename) |
edda67a2 | 1620 | throw ApiException("RRset "+qname.toString()+" IN "+qtype.getName()+": Name is out of zone"); |
34df6ecc | 1621 | |
6754ef71 CH |
1622 | bool replace_records = rrset["records"].is_array(); |
1623 | bool replace_comments = rrset["comments"].is_array(); | |
f63168e6 | 1624 | |
6754ef71 CH |
1625 | if (!replace_records && !replace_comments) { |
1626 | throw ApiException("No change for RRset " + qname.toString() + " IN " + qtype.getName()); | |
1627 | } | |
f63168e6 | 1628 | |
6754ef71 CH |
1629 | new_records.clear(); |
1630 | new_comments.clear(); | |
f63168e6 | 1631 | |
6754ef71 CH |
1632 | if (replace_records) { |
1633 | // ttl shouldn't be part of DELETE, and it shouldn't be required if we don't get new records. | |
1634 | int ttl = intFromJson(rrset, "ttl"); | |
1635 | // new_ptrs is merged. | |
1636 | gatherRecords(rrset, qname, qtype, ttl, new_records, new_ptrs); | |
1637 | ||
1638 | for(DNSResourceRecord& rr : new_records) { | |
1639 | rr.domain_id = di.id; | |
1640 | if (rr.qtype.getCode() == QType::SOA && rr.qname==zonename) { | |
1641 | soa_edit_done = increaseSOARecord(rr, soa_edit_api_kind, soa_edit_kind); | |
6754ef71 | 1642 | } |
d708640f | 1643 | } |
e3675a8a | 1644 | checkDuplicateRecords(new_records); |
6cc98ddf CH |
1645 | } |
1646 | ||
6754ef71 CH |
1647 | if (replace_comments) { |
1648 | gatherComments(rrset, qname, qtype, new_comments); | |
f63168e6 | 1649 | |
6754ef71 CH |
1650 | for(Comment& c : new_comments) { |
1651 | c.domain_id = di.id; | |
1652 | } | |
d708640f | 1653 | } |
b3905a3d | 1654 | |
d708640f | 1655 | if (replace_records) { |
8560f36a CH |
1656 | di.backend->lookup(QType(QType::ANY), qname); |
1657 | DNSResourceRecord rr; | |
1658 | while (di.backend->get(rr)) { | |
1659 | if (qtype.getCode() == QType::CNAME && rr.qtype.getCode() != QType::CNAME) { | |
1660 | throw ApiException("RRset "+qname.toString()+" IN "+qtype.getName()+": Conflicts with pre-existing non-CNAME RRset"); | |
1661 | } else if (qtype.getCode() != QType::CNAME && rr.qtype.getCode() == QType::CNAME) { | |
1662 | throw ApiException("RRset "+qname.toString()+" IN "+qtype.getName()+": Conflicts with pre-existing CNAME RRset"); | |
1663 | } | |
1664 | } | |
1665 | ||
d708640f CH |
1666 | if (!di.backend->replaceRRSet(di.id, qname, qtype, new_records)) { |
1667 | throw ApiException("Hosting backend does not support editing records."); | |
1668 | } | |
1669 | } | |
1670 | if (replace_comments) { | |
1671 | if (!di.backend->replaceComments(di.id, qname, qtype, new_comments)) { | |
1672 | throw ApiException("Hosting backend does not support editing comments."); | |
1673 | } | |
1674 | } | |
6cc98ddf | 1675 | } |
d708640f CH |
1676 | else |
1677 | throw ApiException("Changetype not understood"); | |
6cc98ddf | 1678 | } |
d29d5db7 CH |
1679 | |
1680 | // edit SOA (if needed) | |
1681 | if (!soa_edit_api_kind.empty() && !soa_edit_done) { | |
1682 | SOAData sd; | |
13f9e280 | 1683 | if (!B.getSOAUncached(zonename, sd, true)) |
290a083d | 1684 | throw ApiException("No SOA found for domain '"+zonename.toString()+"'"); |
d29d5db7 CH |
1685 | |
1686 | DNSResourceRecord rr; | |
13f9e280 CH |
1687 | if (makeIncreasedSOARecord(sd, soa_edit_api_kind, soa_edit_kind, rr)) { |
1688 | if (!di.backend->replaceRRSet(di.id, rr.qname, rr.qtype, vector<DNSResourceRecord>(1, rr))) { | |
1689 | throw ApiException("Hosting backend does not support editing records."); | |
1690 | } | |
d29d5db7 | 1691 | } |
3ae63ca8 | 1692 | |
478de03b KW |
1693 | // return old and new serials in headers |
1694 | resp->headers["X-PDNS-Old-Serial"] = std::to_string(sd.serial); | |
3ae63ca8 | 1695 | fillSOAData(rr.content, sd); |
478de03b | 1696 | resp->headers["X-PDNS-New-Serial"] = std::to_string(sd.serial); |
d29d5db7 CH |
1697 | } |
1698 | ||
d708640f CH |
1699 | } catch(...) { |
1700 | di.backend->abortTransaction(); | |
1701 | throw; | |
1702 | } | |
986e4858 | 1703 | |
b0486ea5 | 1704 | DNSSECKeeper dk(&B); |
986e4858 PL |
1705 | string api_rectify; |
1706 | di.backend->getDomainMetadataOne(zonename, "API-RECTIFY", api_rectify); | |
1707 | if (dk.isSecuredZone(zonename) && !dk.isPresigned(zonename) && api_rectify == "1") { | |
1708 | string error_msg = ""; | |
59102608 RG |
1709 | string info; |
1710 | if (!dk.rectifyZone(zonename, error_msg, info, false)) | |
986e4858 PL |
1711 | throw ApiException("Failed to rectify '" + zonename.toString() + "' " + error_msg); |
1712 | } | |
1713 | ||
d708640f | 1714 | di.backend->commitTransaction(); |
b3905a3d | 1715 | |
bf269e28 | 1716 | purgeAuthCachesExact(zonename); |
d1587ceb | 1717 | |
d708640f | 1718 | // now the PTRs |
995473c8 | 1719 | storeChangedPTRs(B, new_ptrs); |
b3905a3d | 1720 | |
f0e76cee CH |
1721 | resp->body = ""; |
1722 | resp->status = 204; // No Content, but indicate success | |
1723 | return; | |
b3905a3d CH |
1724 | } |
1725 | ||
b1902fab CH |
1726 | static void apiServerSearchData(HttpRequest* req, HttpResponse* resp) { |
1727 | if(req->method != "GET") | |
1728 | throw HttpMethodNotAllowedException(); | |
1729 | ||
583ea80d | 1730 | string q = req->getvars["q"]; |
720ed2bd AT |
1731 | string sMax = req->getvars["max"]; |
1732 | int maxEnts = 100; | |
1733 | int ents = 0; | |
1734 | ||
b1902fab CH |
1735 | if (q.empty()) |
1736 | throw ApiException("Query q can't be blank"); | |
606c8752 | 1737 | if (!sMax.empty()) |
335da0ba | 1738 | maxEnts = std::stoi(sMax); |
720ed2bd AT |
1739 | if (maxEnts < 1) |
1740 | throw ApiException("Maximum entries must be larger than 0"); | |
b1902fab | 1741 | |
720ed2bd | 1742 | SimpleMatch sm(q,true); |
b1902fab | 1743 | UeberBackend B; |
b1902fab | 1744 | vector<DomainInfo> domains; |
720ed2bd AT |
1745 | vector<DNSResourceRecord> result_rr; |
1746 | vector<Comment> result_c; | |
1d6b70f9 CH |
1747 | map<int,DomainInfo> zoneIdZone; |
1748 | map<int,DomainInfo>::iterator val; | |
00963dea | 1749 | Json::array doc; |
b1902fab | 1750 | |
720ed2bd | 1751 | B.getAllDomains(&domains, true); |
d2d194a9 | 1752 | |
720ed2bd | 1753 | for(const DomainInfo di: domains) |
1d6b70f9 | 1754 | { |
720ed2bd | 1755 | if (ents < maxEnts && sm.match(di.zone)) { |
00963dea CH |
1756 | doc.push_back(Json::object { |
1757 | { "object_type", "zone" }, | |
1758 | { "zone_id", apiZoneNameToId(di.zone) }, | |
1759 | { "name", di.zone.toString() } | |
1760 | }); | |
720ed2bd | 1761 | ents++; |
b1902fab | 1762 | } |
1d6b70f9 | 1763 | zoneIdZone[di.id] = di; // populate cache |
720ed2bd | 1764 | } |
b1902fab | 1765 | |
720ed2bd AT |
1766 | if (B.searchRecords(q, maxEnts, result_rr)) |
1767 | { | |
1768 | for(const DNSResourceRecord& rr: result_rr) | |
1769 | { | |
7cbc5255 CH |
1770 | if (!rr.qtype.getCode()) |
1771 | continue; // skip empty non-terminals | |
1772 | ||
00963dea CH |
1773 | auto object = Json::object { |
1774 | { "object_type", "record" }, | |
1775 | { "name", rr.qname.toString() }, | |
1776 | { "type", rr.qtype.getName() }, | |
1777 | { "ttl", (double)rr.ttl }, | |
1778 | { "disabled", rr.disabled }, | |
1779 | { "content", makeApiRecordContent(rr.qtype, rr.content) } | |
1780 | }; | |
720ed2bd | 1781 | if ((val = zoneIdZone.find(rr.domain_id)) != zoneIdZone.end()) { |
00963dea CH |
1782 | object["zone_id"] = apiZoneNameToId(val->second.zone); |
1783 | object["zone"] = val->second.zone.toString(); | |
720ed2bd | 1784 | } |
00963dea | 1785 | doc.push_back(object); |
b1902fab | 1786 | } |
720ed2bd | 1787 | } |
b1902fab | 1788 | |
720ed2bd AT |
1789 | if (B.searchComments(q, maxEnts, result_c)) |
1790 | { | |
1791 | for(const Comment &c: result_c) | |
1792 | { | |
00963dea CH |
1793 | auto object = Json::object { |
1794 | { "object_type", "comment" }, | |
25dcc05f | 1795 | { "name", c.qname.toString() }, |
00963dea CH |
1796 | { "content", c.content } |
1797 | }; | |
720ed2bd | 1798 | if ((val = zoneIdZone.find(c.domain_id)) != zoneIdZone.end()) { |
00963dea CH |
1799 | object["zone_id"] = apiZoneNameToId(val->second.zone); |
1800 | object["zone"] = val->second.zone.toString(); | |
720ed2bd | 1801 | } |
00963dea | 1802 | doc.push_back(object); |
b1902fab CH |
1803 | } |
1804 | } | |
4bd3d119 | 1805 | |
b1902fab CH |
1806 | resp->setBody(doc); |
1807 | } | |
1808 | ||
c0f6a1da | 1809 | void apiServerCacheFlush(HttpRequest* req, HttpResponse* resp) { |
136965be | 1810 | if(req->method != "PUT" || ::arg().mustDo("api-readonly")) |
a426cb89 | 1811 | throw HttpMethodNotAllowedException(); |
80d59cd1 | 1812 | |
c0f6a1da CH |
1813 | DNSName canon = apiNameToDNSName(req->getvars["domain"]); |
1814 | ||
bf269e28 | 1815 | uint64_t count = purgeAuthCachesExact(canon); |
f682752a | 1816 | resp->setBody(Json::object { |
bf269e28 RG |
1817 | { "count", (int) count }, |
1818 | { "result", "Flushed cache." } | |
f682752a | 1819 | }); |
ddc84d12 CH |
1820 | } |
1821 | ||
dea47634 | 1822 | void AuthWebServer::cssfunction(HttpRequest* req, HttpResponse* resp) |
c67bf8c5 | 1823 | { |
80d59cd1 CH |
1824 | resp->headers["Cache-Control"] = "max-age=86400"; |
1825 | resp->headers["Content-Type"] = "text/css"; | |
c67bf8c5 | 1826 | |
1071abdd | 1827 | ostringstream ret; |
1071abdd CH |
1828 | ret<<"* { box-sizing: border-box; margin: 0; padding: 0; }"<<endl; |
1829 | ret<<"body { color: black; background: white; margin-top: 1em; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 10pt; position: relative; }"<<endl; | |
1830 | ret<<"a { color: #0959c2; }"<<endl; | |
1831 | ret<<"a:hover { color: #3B8EC8; }"<<endl; | |
1832 | ret<<".row { width: 940px; max-width: 100%; min-width: 768px; margin: 0 auto; }"<<endl; | |
1833 | ret<<".row:before, .row:after { display: table; content:\" \"; }"<<endl; | |
1834 | ret<<".row:after { clear: both; }"<<endl; | |
1835 | ret<<".columns { position: relative; min-height: 1px; float: left; }"<<endl; | |
1836 | ret<<".all { width: 100%; }"<<endl; | |
1837 | ret<<".headl { width: 60%; }"<<endl; | |
1838 | ret<<".headr { width: 39.5%; float: right; background-repeat: no-repeat; margin-top: 7px; "; | |
1839 | ret<<"background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJoAAAAUCAYAAAB1RSS/AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAACtgAAArYBAHIqtQAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAABBTSURBVGiBtVp7cFRVmv9u3763b7/f9It00iFACBohgCEyQYgKI49CLV3cWaoEZBcfo2shu7KOtZbjrqOuVQtVWFuOrPqPRU3NgOIDlkgyJEYJwUAqjzEJedFJupN0p9/v+9o/mtve7r790HF+VbeSPue7555zz+98z4ucOXNmgWVZBH4AK5PJGIPBQBqNxpTNZkthGMZCCUxMTBCDg4PyiYkJWTQaRc1mc7Kuri7a1NQU4ssxDAOffPKJAQCynvnII494ESTddO3aNaXT6SS4TplMRj/44IM+7ndXV5dqfn5ewh9306ZNQZqmobu7W11qri0tLX6tVkv19vYqpqampPw+BEFYtVpNGQwG0mKxpJYsWUIKjTE6OiodGBhQ8NcgkUgYjUZDORyOhM1mSxV6fjAYFF+6dEnLb9NoNOR9990X4H53dHSovV4vzpfZvn27T6FQ0Py2sbExorOzU+N2uwmWZUGv15N33nlnuLGxMZy7byyVQEJ//nd9Yuz/lJR/HBdrHSlJ9baIuuV1L4LJ8/Y49pc/KcJX39WRC4MEgskY3Lourmn5rQdbckfe2ijfOBZo+40xNXtNysR9KLZkdVK+9oBf0fBkCABA3NraamTZwjxSKpXUAw884G1paQkUIty5c+f0Fy5cWMIfx+l0Snt6ejTt7e26AwcOuKxWawoAQCQSQW9vr3pxcTHrJTY3Nwe5Tb18+bJ2bGxMzvWhKMpu27bNj6IoCwDQ1tamd7lcRM79genpaaK1tdVQcDG3sXbt2rBWq6X6+/sV3d3d2mKyy5cvj+7cudO7atWqGL99bGxMWuxZOp0utX37du+9994b5A4Qh2AwiObei6Ioe/fdd4eVSiUNAHD16lX1+Pi4nC+zadOmIJ9oZ8+eNeTu3/T0tLSvr0/V3d0dPXr0qJNrZ+KL6MKpjZWUbyxzQMmFIYJcGCISw5+qjE9+M4UqLJmx/RdeWBK+elKfGTjuR+OhWSxx86JS/9D/zsrufDzMdSXGv5J5/vBYBZuKiLi25HS3LDndLUuMX1IYHjvtynQUQjgcFp89e9b8zjvv2BmGyepjWRbeffdd2/nz55cUIqvT6ZSeOHHC7vf7xVyb3W6P58rNzc1liOfxeLJISNM04na7Me63z+fD+P1SqZQupHn+Wty8eVN+4sSJyv7+fnlp6R/g8/nw06dPW0+ePLmUJEmklDxN08iVK1dU5Y7f0dGhvnjxYkElQVFU1jP9Xz5j4pMsSzYwifvPPWnhfsdHPpdnkYwHlk4ivi9/baFDM2IAACYZEi1++qSVTzI+YkN/VEe++726JNE4TE1Nyc6cOWPkt3322Wf6/v7+ki8nEAhgH3zwQWYhDoejINGSyaQoFAphuf2zs7MSAIBIJIImEgmU32ez2RLlruOngGVZ+Oijj6w+n09cWjobg4ODyg8//NBSWhLgu+++K4toJEkin376qancObBkFIl/f7bo2ImxC0om5kUBACK9pzTFZJlEAI0O/kEJABAf+UJOh115+8VH5MZHGkGimc3mRK66BwBoa2szBAIBMUB6w1tbW415QgUwOjqqGB4elgIA1NTU5BGN02IulwsXOqUul0sCADA/P5+3qIqKip+NaARBMBiGMbnt0Wg0z68qF729vepr164pS8k5nU7ZwsJC0U0DAOjp6VHGYjE0t10kEgmqt5TrOwIYqqRWTbmuSQAASM9fiFKy5Fx/Wnaur7Ss53tC8IQ+/fTTM/F4HH3rrbcc/E1nWRYmJyeJtWvXRr7++mt1rnoGANi6devipk2bgsePH7dHIpGs8Ts7O7W1tbXxqqqqJIZhLN+keDweDADA7XbjuWPebpcAACwsLOT1V1VVFSSayWRKvvLKK5P8tmLBTVNTk//hhx/2vv/++5aBgYEsLeB0OqWF7gMAsFqtiYqKivj169c1ueaytbVVv2HDhnChewHS7/fKlSuqPXv2LBaTyw1gAABqa2sjhw4dck1PT0vOnz9v4O+NWFNdlluBqispAABUYSEp/6TgPmRkVba0rGppybFRpZksaDodDkeioqIiT/M4nU4JAMDIyEiez1JTUxN9/PHHFyoqKpJbtmzx5faPj4/LANKOr9VqzRqbi7D4vhof8/PzOMAPhMyZa948OSAIAjiOs/xLSFvzIZFImO3bt+fNn9OqhaDRaMiDBw/Obd26NY8oTqdTWmhtfPT29paMmkOhUJ6CkEgkjFKppOvq6mIvvviis76+PkNqVF1BiQ21yWJjoiobiRlWpQAACMeWaKk5EMu2RQEAiOr7YyBCi2YliMrN0aI+Wjwez+vn/KOZmZk8lbl69eoI97+QeQwEAhgXFFRVVWX1+/1+nGVZyE1bcPB6vRKWZSE35JdKpbTJZCp4qiiKQmZmZnDuEiKqEITWTtN0SfMDALBjx45FiUSSZ35HRkaKakQAgPn5ecnU1FRRQuv1+rz0Qn9/v+ry5ctqgPTh2rFjR9ZB0e78Hzcgedb2NhDQ7vq9C24fQNXm3/gww8qCxJTX/4OfcGyJAwBgS+pSqo3/XFADo0oLqdn2lkeQaAzDIB0dHWqPx5O3YK1WSzIMA7lmEQDAaDSSQv/zEQwGUQCA6urqLKJRFIV4PB6MH3GqVCqS3z83N4cvLi5mEaVUIOD1evHXX399GXedOnXKWkweIJ3r++abb/IcYqPRWDA3xodUKmWEyMCZ/1IolQvMfXcAabN7+vRp68cff2wS8nElVVvihl99cQtV27PmhapspOHvzzmJ5Tsy6RtELGGX7G+7JV2xIysHiqAYq/rFv3h0e96f57drHnjTo2n57TwiJrIOl6SyOWo6cPmWiNAwgj7am2++6Ugmk4IkrK2tjUWjUVRoMXK5PJOHkclkdJ4AAESjURQAYPny5YKRJ59odXV1EX6ea2ZmRpKbf/s5AwEAgO+//17+8ssv1/j9/jzNt3HjxmC542g0GjI318etXQgoirKcxrx+/brKYDAUJPW6desiFy5ciM/MzORpyM7OTl04HEYPHz7synURiJpfxizPj4+T8/0S0jOEiw2rUrh5TRJE+TRAFWba+KvPZung9Hxy9iohwpUMvnRjQkSo8zQ1ICJQbX7Zp2h8LpCa7ZEwUY8Yt21IiHXLMopCkEyFSFZZWRmz2+0FVSqXUL39v6AM5yTr9XpKrVZnab2RkRFZKpXKPHvlypUxvuM+PT0tCQaDWW+lWCDwUzA3N0cIkay2tjbS0tLiL3ccoYNWzPRWVVXFcBxnAACCwSAmRCIOCILA/v373QqFghLqv3Hjhrq9vb1gioIFBNLFoLI8gbKBILdHRNi8ocvOC6nVavLw4cOzAAAKhYJGEARytRo/5A6Hw4JMk8lkmRNht9vjAwMDmU0dGhril3TAbDanDAZD0u12EwAAw8PDCoZhspZQLBD4KRBa17Zt27wPPfSQVyQqO+0IQumHQloeIB0Jr169Onzjxg01QOHDzqGioiJ55MiRW8ePH68UCg6+/PJLY0tLS4Cv1RJjF2W+z5+2UEFnxiqgKhup2/muW7pyV1YAQEfmUN9n/2SOj57PRN4IirHKphe86q2vLSIozktHMBDq+p0u3PkfRpZKZOYtqWyOavd86BZrlxWOOjMTQVH2jjvuCL/wwgtOvV5PAaQ3QyqV5r20SCSSebmhUEiQaCqVKnNfLkk4QnEwmUyk2WzOaNDp6emsU14qEABIO87Hjh2b5K79+/e7i8kLVS0UCgXF19blINfEAwCoVCpBDcShsbExVKw/FzabLXXs2LFJIT81Go2K+YFPYqpDuvDx7ko+yQAA6NAs5jn9sD1+84KMa2OpJLLw0X2VfJIBALA0iYS6/svoO/ePWcni4KWXjKH2V0x8kgEAJG99Lfd8uLmSSfiFj+j999/v3bt3r/vgwYMzb7zxxthzzz03w9UqOVit1rzFjY6OZiY7NDSUl/4gCIIxmUyZcZYtW1ZQG0mlUloul9Nmszkjn1sCK6cigGEY63A4EtxlsViKOvQOhyOm0WiyyNve3q4vN+IESKeAhKJnISeej/r6+ijfzy2Evr4+Oad19Xo9dejQoVkhbev1ejNE83/xjAXYfPcqDRZ8nz9lhdtjhjr/U0d6RwoGLtH+j7WJyctSAADSM4SHu/9bsFwFAECHXVjwq381ChKtubk50NLSEmhsbAxrNBrBU7hixYq8XMvg4KByamqKmJubw7799ts8H6GqqirGV+XV1dWJQppCq9WSAABWq7WgT/hzBwIAaW3d0NCQpVkCgQDW1dVVVnnI5XLhp06dsuW24zjO1NTUFJ0viqJsfX19Sa3W09Ojfu+996xcCkapVNIoiuaxyGAwkAAAdHBaXIw4AGnNRnqHcQCAxOTlknXdxHirHAAgOXFJBkzxQ5ic6pD/6Nodh9uRT1YxPRaLoW+//XaVWCxmhXyMe+65J8D/jeM4a7FYEkKOL5ceWLp0aUGiVVZWliSax+PBX3rppRp+27PPPjtdLKhpamoKtre3Z53Sr776yrB58+a8LzH4GB4eVr722muCpaaGhoYgQRCFVEoGGzduDF65cqVkqevGjRvqgYEBld1uj8/NzUlIMtsNwnGc4VJMlH+yrNwhFbglxoyrUnTEXVKeDs2K039nSstG5rDyvdscLF26NNnQ0JAX7tM0jQiRzGQyJdevXx/Jba+srBQ0J3q9ngRIBwRisVhQ65UTCNA0jQQCAYx/CZXO+LDb7UmLxZJFYo/Hg1+9erVovTLXtHMgCILevXt30bISh5UrV8ZzTXchUBSFTExMyIQCj7q6ugh3KHDbugSIhN8hHxLb+iQAAGasK+2SmOvTsuY1pWWNqxI/mWgAAI8++uiCTqcrmcTEMIzZt2+fW8hMFvJbuNMoEokEM+FSqZQ2m81/k0+DAADWr1+fZ8IuXrxY8lu3XKAoyu7bt8/NmbFSEDLdPxYSiYTZu3dvJqmKYHJWturhomNKa34ZFskMNACAYt2hQDFZEaGh5XfsDQMAECt2R1Glreja5GsOBP4qoul0Ouro0aO3TCZTQTOkUqnII0eO3FqxYoUgoYRKVQAA/ISl0Ph/60+Dmpqa8syky+Ui+vr6yv4uTavVks8///ytUsV0oWf/GHk+pFIp/cQTT8zqdLos31q36+S8WFcjuE9iTVVK99CpTDQuXbk7qmz8taAGRlAJq9t50o2qllIAACKJitHu+cCF4ApBdS5d/XdB+fqnguLq6upobm4Kx/GyQ3m9Xk+9+uqrk21tbZquri6t1+vFWZYFi8WSdDgcsV27di1qtdqCYb3ZbCZra2sjueaW/yl0XV1dNBwOZ/mT/KIxB6VSSTkcjlhuey44X8lkMqVy5TmC6/V6qrGx0Z8bPY6OjsrWrFkT1el0ec9CUZRVqVSUWq2mqqur4xs2bAgL+XQSiYTJvZcf9Njt9uRdd90Vys2PcQnd5ubmAMMwcPPmTXk0GhUDpCsRVVVVsccee2yBS0PxIZLqacszfZPBP7+qj4+1Kilf+lNuYtkDEU3La3mfcmsfPL4gqfxFrJxPuYll22Kmp/omgpf+zZia7ZEyCT+KGVcn5WsP+uUNh0IAAP8PaQRnE4MgdzkAAAAASUVORK5CYII=);"; | |
1840 | ret<<" width: 154px; height: 20px; }"<<endl; | |
1841 | ret<<"a#appname { margin: 0; font-size: 27px; color: #666; text-decoration: none; font-weight: bold; display: block; }"<<endl; | |
1842 | ret<<"footer { border-top: 1px solid #ddd; padding-top: 4px; font-size: 12px; }"<<endl; | |
1843 | ret<<"footer.row { margin-top: 1em; margin-bottom: 1em; }"<<endl; | |
1844 | ret<<".panel { background: #f2f2f2; border: 1px solid #e6e6e6; margin: 0 0 22px 0; padding: 20px; }"<<endl; | |
1845 | ret<<"table.data { width: 100%; border-spacing: 0; border-top: 1px solid #333; }"<<endl; | |
1846 | ret<<"table.data td { border-bottom: 1px solid #333; padding: 2px; }"<<endl; | |
1847 | ret<<"table.data tr:nth-child(2n) { background: #e2e2e2; }"<<endl; | |
1848 | ret<<"table.data tr:hover { background: white; }"<<endl; | |
1849 | ret<<".ringmeta { margin-bottom: 5px; }"<<endl; | |
1850 | ret<<".resetring {float: right; }"<<endl; | |
1851 | ret<<".resetring i { background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAA/klEQVQY01XPP04UUBgE8N/33vd2XZUWEuzYuMZEG4KFCQn2NhA4AIewAOMBPIG2xhNYeAcKGqkNCdmYlVBZGBIT4FHsbuE0U8xk/kAbqm9TOfI/nicfhmwgDNhvylUT58kxCp4l31L8SfH9IetJ2ev6PwyIwyZWsdb11/gbTK55Co+r8rmJaRPTFJcpZil+pTit7C5awMpA+Zpi1sRFE9MqflYOloYCjY2uP8EdYiGU4CVGUBubxKfOOLjrtOBmzvEilbVb/aQWvhRl0unBZVXe4XdnK+bprwqnhoyTsyZ+JG8Wk0apfExxlcp7PFruXH8gdxamWB4cyW2sIO4BG3czIp78jUIAAAAASUVORK5CYII=); width: 10px; height: 10px; margin-right: 2px; display: inline-block; background-repeat: no-repeat; }"<<endl; | |
1852 | ret<<".resetring:hover i { background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAA2ElEQVQY013PMUoDcRDF4c+kEzxCsNNCrBQvIGhnlcYm11EkBxAraw8gglgIoiJpAoKIYlBcgrgopsma3c3fwt1k9cHA480M8xvQp/nMjorOWY5ov7IAYlpjQk7aYxcuWBpwFQgJnUcaYk7GhEDIGL5w+MVpKLIRyR2b4JOjvGhUKzHTv2W7iuSN479Dvu9plf1awbQ6y3x1sU5tjpVJcMbakF6Ycoas8Dl5xEHJ160wRdfqzXfa6XQ4PLDlicWUjxHxZfndL/N+RhiwNzl/Q6PDhn/qsl76H7prcApk2B1aAAAAAElFTkSuQmCC);}"<<endl; | |
1853 | ret<<".resizering {float: right;}"<<endl; | |
80d59cd1 | 1854 | resp->body = ret.str(); |
c146576d | 1855 | resp->status = 200; |
1071abdd CH |
1856 | } |
1857 | ||
dea47634 | 1858 | void AuthWebServer::webThread() |
12c86877 BH |
1859 | { |
1860 | try { | |
479e0976 | 1861 | if(::arg().mustDo("api")) { |
c0f6a1da | 1862 | d_ws->registerApiHandler("/api/v1/servers/localhost/cache/flush", &apiServerCacheFlush); |
46d06a12 | 1863 | d_ws->registerApiHandler("/api/v1/servers/localhost/config", &apiServerConfig); |
46d06a12 PL |
1864 | d_ws->registerApiHandler("/api/v1/servers/localhost/search-log", &apiServerSearchLog); |
1865 | d_ws->registerApiHandler("/api/v1/servers/localhost/search-data", &apiServerSearchData); | |
1866 | d_ws->registerApiHandler("/api/v1/servers/localhost/statistics", &apiServerStatistics); | |
1867 | d_ws->registerApiHandler("/api/v1/servers/localhost/zones/<id>/axfr-retrieve", &apiServerZoneAxfrRetrieve); | |
1868 | d_ws->registerApiHandler("/api/v1/servers/localhost/zones/<id>/cryptokeys/<key_id>", &apiZoneCryptokeys); | |
1869 | d_ws->registerApiHandler("/api/v1/servers/localhost/zones/<id>/cryptokeys", &apiZoneCryptokeys); | |
1870 | d_ws->registerApiHandler("/api/v1/servers/localhost/zones/<id>/export", &apiServerZoneExport); | |
24e11043 CJ |
1871 | d_ws->registerApiHandler("/api/v1/servers/localhost/zones/<id>/metadata/<kind>", &apiZoneMetadataKind); |
1872 | d_ws->registerApiHandler("/api/v1/servers/localhost/zones/<id>/metadata", &apiZoneMetadata); | |
46d06a12 | 1873 | d_ws->registerApiHandler("/api/v1/servers/localhost/zones/<id>/notify", &apiServerZoneNotify); |
4bc8379e | 1874 | d_ws->registerApiHandler("/api/v1/servers/localhost/zones/<id>/rectify", &apiServerZoneRectify); |
46d06a12 PL |
1875 | d_ws->registerApiHandler("/api/v1/servers/localhost/zones/<id>", &apiServerZoneDetail); |
1876 | d_ws->registerApiHandler("/api/v1/servers/localhost/zones", &apiServerZones); | |
1877 | d_ws->registerApiHandler("/api/v1/servers/localhost", &apiServerDetail); | |
1878 | d_ws->registerApiHandler("/api/v1/servers", &apiServer); | |
9e6d2033 | 1879 | d_ws->registerApiHandler("/api", &apiDiscovery); |
c67bf8c5 | 1880 | } |
536ab56f CH |
1881 | if (::arg().mustDo("webserver")) { |
1882 | d_ws->registerWebHandler("/style.css", boost::bind(&AuthWebServer::cssfunction, this, _1, _2)); | |
1883 | d_ws->registerWebHandler("/", boost::bind(&AuthWebServer::indexfunction, this, _1, _2)); | |
1884 | } | |
96d299db | 1885 | d_ws->go(); |
12c86877 BH |
1886 | } |
1887 | catch(...) { | |
dea47634 | 1888 | L<<Logger::Error<<"AuthWebServer thread caught an exception, dying"<<endl; |
5bd2ea7b | 1889 | _exit(1); |
12c86877 BH |
1890 | } |
1891 | } |