/
tcpreceiver.cc
1408 lines (1220 loc) · 45.9 KB
/
tcpreceiver.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
PowerDNS Versatile Database Driven Nameserver
Copyright (C) 2002-2012 PowerDNS.COM BV
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
as published by the Free Software Foundation
Additionally, the license of this program contains a special
exception which allows to distribute the program in binary form when
it is linked against OpenSSL.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <boost/algorithm/string.hpp>
#include "auth-packetcache.hh"
#include "utility.hh"
#include "dnssecinfra.hh"
#include "dnsseckeeper.hh"
#include <cstdio>
#include "base32.hh"
#include <cstring>
#include <cstdlib>
#include <sys/types.h>
#include <netinet/tcp.h>
#include <iostream>
#include <string>
#include "tcpreceiver.hh"
#include "sstuff.hh"
#include <errno.h>
#include <signal.h>
#include "base64.hh"
#include "ueberbackend.hh"
#include "dnspacket.hh"
#include "nameserver.hh"
#include "distributor.hh"
#include "lock.hh"
#include "logger.hh"
#include "arguments.hh"
#include "common_startup.hh"
#include "packethandler.hh"
#include "statbag.hh"
#include "resolver.hh"
#include "communicator.hh"
#include "namespaces.hh"
#include "signingpipe.hh"
#include "stubresolver.hh"
extern AuthPacketCache PC;
extern StatBag S;
/**
\file tcpreceiver.cc
\brief This file implements the tcpreceiver that receives and answers questions over TCP/IP
*/
pthread_mutex_t TCPNameserver::s_plock = PTHREAD_MUTEX_INITIALIZER;
Semaphore *TCPNameserver::d_connectionroom_sem;
PacketHandler *TCPNameserver::s_P;
NetmaskGroup TCPNameserver::d_ng;
size_t TCPNameserver::d_maxTransactionsPerConn;
size_t TCPNameserver::d_maxConnectionsPerClient;
unsigned int TCPNameserver::d_idleTimeout;
unsigned int TCPNameserver::d_maxConnectionDuration;
std::mutex TCPNameserver::s_clientsCountMutex;
std::map<ComboAddress,size_t,ComboAddress::addressOnlyLessThan> TCPNameserver::s_clientsCount;
void TCPNameserver::go()
{
L<<Logger::Error<<"Creating backend connection for TCP"<<endl;
s_P=0;
try {
s_P=new PacketHandler;
}
catch(PDNSException &ae) {
L<<Logger::Error<<"TCP server is unable to launch backends - will try again when questions come in: "<<ae.reason<<endl;
}
pthread_create(&d_tid, 0, launcher, static_cast<void *>(this));
}
void *TCPNameserver::launcher(void *data)
{
static_cast<TCPNameserver *>(data)->thread();
return 0;
}
// throws PDNSException if things didn't go according to plan, returns 0 if really 0 bytes were read
static int readnWithTimeout(int fd, void* buffer, unsigned int n, unsigned int idleTimeout, bool throwOnEOF=true, unsigned int totalTimeout=0)
{
unsigned int bytes=n;
char *ptr = (char*)buffer;
int ret;
time_t start = 0;
unsigned int remainingTotal = totalTimeout;
if (totalTimeout) {
start = time(NULL);
}
while(bytes) {
ret=read(fd, ptr, bytes);
if(ret < 0) {
if(errno==EAGAIN) {
ret=waitForData(fd, (totalTimeout == 0 || idleTimeout <= remainingTotal) ? idleTimeout : remainingTotal);
if(ret < 0)
throw NetworkError("Waiting for data read");
if(!ret)
throw NetworkError("Timeout reading data");
continue;
}
else
throw NetworkError("Reading data: "+stringerror());
}
if(!ret) {
if(!throwOnEOF && n == bytes)
return 0;
else
throw NetworkError("Did not fulfill read from TCP due to EOF");
}
ptr += ret;
bytes -= ret;
if (totalTimeout) {
time_t now = time(NULL);
unsigned int elapsed = now - start;
if (elapsed >= remainingTotal) {
throw NetworkError("Timeout while reading data");
}
start = now;
remainingTotal -= elapsed;
}
}
return n;
}
// ditto
static void writenWithTimeout(int fd, const void *buffer, unsigned int n, unsigned int idleTimeout)
{
unsigned int bytes=n;
const char *ptr = (char*)buffer;
int ret;
while(bytes) {
ret=write(fd, ptr, bytes);
if(ret < 0) {
if(errno==EAGAIN) {
ret=waitForRWData(fd, false, idleTimeout, 0);
if(ret < 0)
throw NetworkError("Waiting for data write");
if(!ret)
throw NetworkError("Timeout writing data");
continue;
}
else
throw NetworkError("Writing data: "+stringerror());
}
if(!ret) {
throw NetworkError("Did not fulfill TCP write due to EOF");
}
ptr += ret;
bytes -= ret;
}
}
void connectWithTimeout(int fd, struct sockaddr* remote, size_t socklen)
{
int err;
Utility::socklen_t len=sizeof(err);
if((err=connect(fd, remote, socklen))<0 && errno!=EINPROGRESS)
throw NetworkError("connect: "+stringerror());
if(!err)
goto done;
err=waitForRWData(fd, false, 5, 0);
if(err == 0)
throw NetworkError("Timeout connecting to remote");
if(err < 0)
throw NetworkError("Error connecting to remote");
if(getsockopt(fd, SOL_SOCKET,SO_ERROR,(char *)&err,&len)<0)
throw NetworkError("Error connecting to remote: "+stringerror()); // Solaris
if(err)
throw NetworkError("Error connecting to remote: "+string(strerror(err)));
done:
;
}
void TCPNameserver::sendPacket(shared_ptr<DNSPacket> p, int outsock)
{
g_rs.submitResponse(*p, false);
uint16_t len=htons(p->getString().length());
string buffer((const char*)&len, 2);
buffer.append(p->getString());
writenWithTimeout(outsock, buffer.c_str(), buffer.length(), d_idleTimeout);
}
void TCPNameserver::getQuestion(int fd, char *mesg, int pktlen, const ComboAddress &remote, unsigned int totalTime)
try
{
readnWithTimeout(fd, mesg, pktlen, d_idleTimeout, true, totalTime);
}
catch(NetworkError& ae) {
throw NetworkError("Error reading DNS data from TCP client "+remote.toString()+": "+ae.what());
}
static void incTCPAnswerCount(const ComboAddress& remote)
{
S.inc("tcp-answers");
if(remote.sin4.sin_family == AF_INET6)
S.inc("tcp6-answers");
else
S.inc("tcp4-answers");
}
static bool maxConnectionDurationReached(unsigned int maxConnectionDuration, time_t start, unsigned int& remainingTime)
{
if (maxConnectionDuration) {
time_t elapsed = time(NULL) - start;
if (elapsed >= maxConnectionDuration) {
return true;
}
remainingTime = maxConnectionDuration - elapsed;
}
return false;
}
void TCPNameserver::decrementClientCount(const ComboAddress& remote)
{
if (d_maxConnectionsPerClient) {
std::lock_guard<std::mutex> lock(s_clientsCountMutex);
s_clientsCount[remote]--;
if (s_clientsCount[remote] == 0) {
s_clientsCount.erase(remote);
}
}
}
void *TCPNameserver::doConnection(void *data)
{
shared_ptr<DNSPacket> packet;
// Fix gcc-4.0 error (on AMD64)
int fd=(int)(long)data; // gotta love C (generates a harmless warning on opteron)
ComboAddress remote;
socklen_t remotelen=sizeof(remote);
size_t transactions = 0;
time_t start = 0;
if (d_maxConnectionDuration) {
start = time(NULL);
}
pthread_detach(pthread_self());
if(getpeername(fd, (struct sockaddr *)&remote, &remotelen) < 0) {
L<<Logger::Warning<<"Received question from socket which had no remote address, dropping ("<<stringerror()<<")"<<endl;
d_connectionroom_sem->post();
try {
closesocket(fd);
}
catch(const PDNSException& e) {
L<<Logger::Error<<"Error closing TCP socket: "<<e.reason<<endl;
}
return 0;
}
setNonBlocking(fd);
try {
int mesgsize=65535;
scoped_array<char> mesg(new char[mesgsize]);
DLOG(L<<"TCP Connection accepted on fd "<<fd<<endl);
bool logDNSQueries= ::arg().mustDo("log-dns-queries");
for(;;) {
unsigned int remainingTime = 0;
transactions++;
if (d_maxTransactionsPerConn && transactions > d_maxTransactionsPerConn) {
L << Logger::Notice<<"TCP Remote "<< remote <<" exceeded the number of transactions per connection, dropping.";
break;
}
if (maxConnectionDurationReached(d_maxConnectionDuration, start, remainingTime)) {
L << Logger::Notice<<"TCP Remote "<< remote <<" exceeded the maximum TCP connection duration, dropping.";
break;
}
uint16_t pktlen;
if(!readnWithTimeout(fd, &pktlen, 2, d_idleTimeout, false, remainingTime))
break;
else
pktlen=ntohs(pktlen);
// this check will always be false *if* no one touches
// the mesg array. pktlen can be maximum of 65535 as
// it is 2 byte unsigned variable. In getQuestion, we
// write to 0 up to pktlen-1 so 65535 is just right.
// do not remove this check as it will catch if someone
// decreases the mesg buffer size for some reason.
if(pktlen > mesgsize) {
L<<Logger::Warning<<"Received an overly large question from "<<remote.toString()<<", dropping"<<endl;
break;
}
if (maxConnectionDurationReached(d_maxConnectionDuration, start, remainingTime)) {
L << Logger::Notice<<"TCP Remote "<< remote <<" exceeded the maximum TCP connection duration, dropping.";
break;
}
getQuestion(fd, mesg.get(), pktlen, remote, remainingTime);
S.inc("tcp-queries");
if(remote.sin4.sin_family == AF_INET6)
S.inc("tcp6-queries");
else
S.inc("tcp4-queries");
packet=shared_ptr<DNSPacket>(new DNSPacket(true));
packet->setRemote(&remote);
packet->d_tcp=true;
packet->setSocket(fd);
if(packet->parse(mesg.get(), pktlen)<0)
break;
if(packet->qtype.getCode()==QType::AXFR) {
if(doAXFR(packet->qdomain, packet, fd))
incTCPAnswerCount(remote);
continue;
}
if(packet->qtype.getCode()==QType::IXFR) {
if(doIXFR(packet, fd))
incTCPAnswerCount(remote);
continue;
}
shared_ptr<DNSPacket> reply;
shared_ptr<DNSPacket> cached= shared_ptr<DNSPacket>(new DNSPacket(false));
if(logDNSQueries) {
string remote_text;
if(packet->hasEDNSSubnet())
remote_text = packet->getRemote().toString() + "<-" + packet->getRealRemote().toString();
else
remote_text = packet->getRemote().toString();
L << Logger::Notice<<"TCP Remote "<< remote_text <<" wants '" << packet->qdomain<<"|"<<packet->qtype.getName() <<
"', do = " <<packet->d_dnssecOk <<", bufsize = "<< packet->getMaxReplyLen()<<": ";
}
if(packet->couldBeCached() && PC.get(packet.get(), cached.get())) { // short circuit - does the PacketCache recognize this question?
if(logDNSQueries)
L<<"packetcache HIT"<<endl;
cached->setRemote(&packet->d_remote);
cached->d.id=packet->d.id;
cached->d.rd=packet->d.rd; // copy in recursion desired bit
cached->commitD(); // commit d to the packet inlined
sendPacket(cached, fd); // presigned, don't do it again
continue;
}
if(logDNSQueries)
L<<"packetcache MISS"<<endl;
{
Lock l(&s_plock);
if(!s_P) {
L<<Logger::Error<<"TCP server is without backend connections, launching"<<endl;
s_P=new PacketHandler;
}
reply=shared_ptr<DNSPacket>(s_P->doQuestion(packet.get())); // we really need to ask the backend :-)
}
if(!reply) // unable to write an answer?
break;
sendPacket(reply, fd);
}
}
catch(PDNSException &ae) {
Lock l(&s_plock);
delete s_P;
s_P = 0; // on next call, backend will be recycled
L<<Logger::Error<<"TCP nameserver had error, cycling backend: "<<ae.reason<<endl;
}
catch(NetworkError &e) {
L<<Logger::Info<<"TCP Connection Thread died because of network error: "<<e.what()<<endl;
}
catch(std::exception &e) {
L<<Logger::Error<<"TCP Connection Thread died because of STL error: "<<e.what()<<endl;
}
catch( ... )
{
L << Logger::Error << "TCP Connection Thread caught unknown exception." << endl;
}
d_connectionroom_sem->post();
try {
closesocket(fd);
}
catch(const PDNSException& e) {
L<<Logger::Error<<"Error closing TCP socket: "<<e.reason<<endl;
}
decrementClientCount(remote);
return 0;
}
// call this method with s_plock held!
bool TCPNameserver::canDoAXFR(shared_ptr<DNSPacket> q)
{
if(::arg().mustDo("disable-axfr"))
return false;
if(q->d_havetsig) { // if you have one, it must be good
TSIGRecordContent trc;
DNSName keyname;
string secret;
if(!q->checkForCorrectTSIG(s_P->getBackend(), &keyname, &secret, &trc)) {
return false;
} else {
getTSIGHashEnum(trc.d_algoName, q->d_tsig_algo);
if (q->d_tsig_algo == TSIG_GSS) {
GssContext gssctx(keyname);
if (!gssctx.getPeerPrincipal(q->d_peer_principal)) {
L<<Logger::Warning<<"Failed to extract peer principal from GSS context with keyname '"<<keyname<<"'"<<endl;
}
}
}
DNSSECKeeper dk(s_P->getBackend());
if (q->d_tsig_algo == TSIG_GSS) {
vector<string> princs;
s_P->getBackend()->getDomainMetadata(q->qdomain, "GSS-ALLOW-AXFR-PRINCIPAL", princs);
for(const std::string& princ : princs) {
if (q->d_peer_principal == princ) {
L<<Logger::Warning<<"AXFR of domain '"<<q->qdomain<<"' allowed: TSIG signed request with authorized principal '"<<q->d_peer_principal<<"' and algorithm 'gss-tsig'"<<endl;
return true;
}
}
L<<Logger::Warning<<"AXFR of domain '"<<q->qdomain<<"' denied: TSIG signed request with principal '"<<q->d_peer_principal<<"' and algorithm 'gss-tsig' is not permitted"<<endl;
return false;
}
if(!dk.TSIGGrantsAccess(q->qdomain, keyname)) {
L<<Logger::Error<<"AXFR '"<<q->qdomain<<"' denied: key with name '"<<keyname<<"' and algorithm '"<<getTSIGAlgoName(q->d_tsig_algo)<<"' does not grant access to zone"<<endl;
return false;
}
else {
L<<Logger::Warning<<"AXFR of domain '"<<q->qdomain<<"' allowed: TSIG signed request with authorized key '"<<keyname<<"' and algorithm '"<<getTSIGAlgoName(q->d_tsig_algo)<<"'"<<endl;
return true;
}
}
// cerr<<"checking allow-axfr-ips"<<endl;
if(!(::arg()["allow-axfr-ips"].empty()) && d_ng.match( (ComboAddress *) &q->d_remote )) {
L<<Logger::Warning<<"AXFR of domain '"<<q->qdomain<<"' allowed: client IP "<<q->getRemote()<<" is in allow-axfr-ips"<<endl;
return true;
}
FindNS fns;
// cerr<<"doing per-zone-axfr-acls"<<endl;
SOAData sd;
if(s_P->getBackend()->getSOAUncached(q->qdomain,sd)) {
// cerr<<"got backend and SOA"<<endl;
DNSBackend *B=sd.db;
vector<string> acl;
s_P->getBackend()->getDomainMetadata(q->qdomain, "ALLOW-AXFR-FROM", acl);
for (vector<string>::const_iterator i = acl.begin(); i != acl.end(); ++i) {
// cerr<<"matching against "<<*i<<endl;
if(pdns_iequals(*i, "AUTO-NS")) {
// cerr<<"AUTO-NS magic please!"<<endl;
DNSResourceRecord rr;
set<DNSName> nsset;
B->lookup(QType(QType::NS),q->qdomain);
while(B->get(rr))
nsset.insert(DNSName(rr.content));
for(const auto & j: nsset) {
vector<string> nsips=fns.lookup(j, s_P->getBackend(),q->qdomain);
for(vector<string>::const_iterator k=nsips.begin();k!=nsips.end();++k) {
// cerr<<"got "<<*k<<" from AUTO-NS"<<endl;
if(*k == q->getRemote().toString())
{
// cerr<<"got AUTO-NS hit"<<endl;
L<<Logger::Warning<<"AXFR of domain '"<<q->qdomain<<"' allowed: client IP "<<q->getRemote()<<" is in NSset"<<endl;
return true;
}
}
}
}
else
{
Netmask nm = Netmask(*i);
if(nm.match( (ComboAddress *) &q->d_remote ))
{
L<<Logger::Warning<<"AXFR of domain '"<<q->qdomain<<"' allowed: client IP "<<q->getRemote()<<" is in per-domain ACL"<<endl;
// cerr<<"hit!"<<endl;
return true;
}
}
}
}
extern CommunicatorClass Communicator;
if(Communicator.justNotified(q->qdomain, q->getRemote().toString())) { // we just notified this ip
L<<Logger::Warning<<"Approved AXFR of '"<<q->qdomain<<"' from recently notified slave "<<q->getRemote()<<endl;
return true;
}
L<<Logger::Error<<"AXFR of domain '"<<q->qdomain<<"' denied: client IP "<<q->getRemote()<<" has no permission"<<endl;
return false;
}
namespace {
struct NSECXEntry
{
set<uint16_t> d_set;
unsigned int d_ttl;
bool d_auth;
};
DNSResourceRecord makeDNSRRFromSOAData(const SOAData& sd)
{
DNSResourceRecord soa;
soa.qname= sd.qname;
soa.qtype=QType::SOA;
soa.content=serializeSOAData(sd);
soa.ttl=sd.ttl;
soa.domain_id=sd.domain_id;
soa.auth = true;
return soa;
}
shared_ptr<DNSPacket> getFreshAXFRPacket(shared_ptr<DNSPacket> q)
{
shared_ptr<DNSPacket> ret = shared_ptr<DNSPacket>(q->replyPacket());
ret->setCompress(false);
ret->d_dnssecOk=false; // RFC 5936, 2.2.5
ret->d_tcp = true;
return ret;
}
}
/** do the actual zone transfer. Return 0 in case of error, 1 in case of success */
int TCPNameserver::doAXFR(const DNSName &target, shared_ptr<DNSPacket> q, int outsock)
{
shared_ptr<DNSPacket> outpacket= getFreshAXFRPacket(q);
if(q->d_dnssecOk)
outpacket->d_dnssecOk=true; // RFC 5936, 2.2.5 'SHOULD'
L<<Logger::Error<<"AXFR of domain '"<<target<<"' initiated by "<<q->getRemote()<<endl;
// determine if zone exists and AXFR is allowed using existing backend before spawning a new backend.
SOAData sd;
{
Lock l(&s_plock);
DLOG(L<<"Looking for SOA"<<endl); // find domain_id via SOA and list complete domain. No SOA, no AXFR
if(!s_P) {
L<<Logger::Error<<"TCP server is without backend connections in doAXFR, launching"<<endl;
s_P=new PacketHandler;
}
// canDoAXFR does all the ACL checks, and has the if(disable-axfr) shortcut, call it first.
if (!canDoAXFR(q)) {
L<<Logger::Error<<"AXFR of domain '"<<target<<"' failed: "<<q->getRemote()<<" cannot request AXFR"<<endl;
outpacket->setRcode(RCode::NotAuth);
sendPacket(outpacket,outsock);
return 0;
}
if(!s_P->getBackend()->getSOAUncached(target, sd)) {
L<<Logger::Error<<"AXFR of domain '"<<target<<"' failed: not authoritative"<<endl;
outpacket->setRcode(RCode::NotAuth);
sendPacket(outpacket,outsock);
return 0;
}
}
UeberBackend db;
if(!db.getSOAUncached(target, sd)) {
L<<Logger::Error<<"AXFR of domain '"<<target<<"' failed: not authoritative in second instance"<<endl;
outpacket->setRcode(RCode::NotAuth);
sendPacket(outpacket,outsock);
return 0;
}
DNSSECKeeper dk(&db);
dk.clearCaches(target);
bool securedZone = dk.isSecuredZone(target);
bool presignedZone = dk.isPresigned(target);
bool noAXFRBecauseOfNSEC3Narrow=false;
NSEC3PARAMRecordContent ns3pr;
bool narrow;
bool NSEC3Zone=false;
if(securedZone && dk.getNSEC3PARAM(target, &ns3pr, &narrow)) {
NSEC3Zone=true;
if(narrow) {
L<<Logger::Error<<"Not doing AXFR of an NSEC3 narrow zone '"<<target<<"' for "<<q->getRemote()<<endl;
noAXFRBecauseOfNSEC3Narrow=true;
}
}
if(noAXFRBecauseOfNSEC3Narrow) {
L<<Logger::Error<<"AXFR of domain '"<<target<<"' denied to "<<q->getRemote()<<endl;
outpacket->setRcode(RCode::Refused);
// FIXME: should actually figure out if we are auth over a zone, and send out 9 if we aren't
sendPacket(outpacket,outsock);
return 0;
}
TSIGRecordContent trc;
DNSName tsigkeyname;
string tsigsecret;
bool haveTSIGDetails = q->getTSIGDetails(&trc, &tsigkeyname);
if(haveTSIGDetails && !tsigkeyname.empty()) {
string tsig64;
DNSName algorithm=trc.d_algoName; // FIXME400: check
if (algorithm == DNSName("hmac-md5.sig-alg.reg.int"))
algorithm = DNSName("hmac-md5");
if (algorithm != DNSName("gss-tsig")) {
if(!db.getTSIGKey(tsigkeyname, &algorithm, &tsig64)) {
L<<Logger::Error<<"TSIG key '"<<tsigkeyname<<"' for domain '"<<target<<"' not found"<<endl;
return 0;
}
if (B64Decode(tsig64, tsigsecret) == -1) {
L<<Logger::Error<<"Unable to Base-64 decode TSIG key '"<<tsigkeyname<<"' for domain '"<<target<<"'"<<endl;
return 0;
}
}
}
// SOA *must* go out first, our signing pipe might reorder
DLOG(L<<"Sending out SOA"<<endl);
DNSResourceRecord soa = makeDNSRRFromSOAData(sd);
DNSZoneRecord dzrsoa;
dzrsoa.auth=true;
dzrsoa.dr=DNSRecord(soa);
string kind;
dk.getSoaEdit(sd.qname, kind);
editSOARecord(dzrsoa, kind);
outpacket->addRecord(dzrsoa);
if(securedZone && !presignedZone) {
set<DNSName> authSet;
authSet.insert(target);
addRRSigs(dk, db, authSet, outpacket->getRRS());
}
if(haveTSIGDetails && !tsigkeyname.empty())
outpacket->setTSIGDetails(trc, tsigkeyname, tsigsecret, trc.d_mac); // first answer is 'normal'
sendPacket(outpacket, outsock);
trc.d_mac = outpacket->d_trc.d_mac;
outpacket = getFreshAXFRPacket(q);
ChunkedSigningPipe csp(target, (securedZone && !presignedZone), ::arg().asNum("signing-threads", 1));
typedef map<DNSName, NSECXEntry, CanonDNSNameCompare> nsecxrepo_t;
nsecxrepo_t nsecxrepo;
// this is where the DNSKEYs go in
DNSSECKeeper::keyset_t keys = dk.getKeys(target);
DNSZoneRecord zrr;
zrr.dr.d_name = target;
zrr.dr.d_ttl = sd.default_ttl;
zrr.auth = 1; // please sign!
string publishCDNSKEY, publishCDS;
dk.getFromMeta(q->qdomain, "PUBLISH-CDNSKEY", publishCDNSKEY);
dk.getFromMeta(q->qdomain, "PUBLISH-CDS", publishCDS);
vector<DNSZoneRecord> cds, cdnskey;
DNSSECKeeper::keyset_t entryPoints = dk.getEntryPoints(q->qdomain);
set<uint32_t> entryPointIds;
for (auto const& value : entryPoints)
entryPointIds.insert(value.second.id);
for(const DNSSECKeeper::keyset_t::value_type& value : keys) {
zrr.dr.d_type = QType::DNSKEY;
zrr.dr.d_content = std::make_shared<DNSKEYRecordContent>(value.first.getDNSKEY());
DNSName keyname = NSEC3Zone ? DNSName(toBase32Hex(hashQNameWithSalt(ns3pr, zrr.dr.d_name))) : zrr.dr.d_name;
NSECXEntry& ne = nsecxrepo[keyname];
ne.d_set.insert(zrr.dr.d_type);
ne.d_ttl = sd.default_ttl;
csp.submit(zrr);
// generate CDS and CDNSKEY records
if(entryPointIds.count(value.second.id) > 0){
if(publishCDNSKEY == "1") {
zrr.dr.d_type=QType::CDNSKEY;
zrr.dr.d_content = std::make_shared<DNSKEYRecordContent>(value.first.getDNSKEY());
cdnskey.push_back(zrr);
}
if(!publishCDS.empty()){
zrr.dr.d_type=QType::CDS;
vector<string> digestAlgos;
stringtok(digestAlgos, publishCDS, ", ");
for(auto const &digestAlgo : digestAlgos) {
zrr.dr.d_content=std::make_shared<DSRecordContent>(makeDSFromDNSKey(target, value.first.getDNSKEY(), pdns_stou(digestAlgo)));
cds.push_back(zrr);
}
}
}
}
if(::arg().mustDo("direct-dnskey")) {
sd.db->lookup(QType(QType::DNSKEY), target, NULL, sd.domain_id);
while(sd.db->get(zrr)) {
zrr.dr.d_ttl = sd.default_ttl;
csp.submit(zrr);
}
}
uint8_t flags;
if(NSEC3Zone) { // now stuff in the NSEC3PARAM
flags = ns3pr.d_flags;
zrr.dr.d_type = QType::NSEC3PARAM;
ns3pr.d_flags = 0;
zrr.dr.d_content = std::make_shared<NSEC3PARAMRecordContent>(ns3pr);
ns3pr.d_flags = flags;
DNSName keyname = DNSName(toBase32Hex(hashQNameWithSalt(ns3pr, zrr.dr.d_name)));
NSECXEntry& ne = nsecxrepo[keyname];
ne.d_set.insert(zrr.dr.d_type);
csp.submit(zrr);
}
// now start list zone
if(!(sd.db->list(target, sd.domain_id))) {
L<<Logger::Error<<"Backend signals error condition"<<endl;
outpacket->setRcode(RCode::ServFail);
sendPacket(outpacket,outsock);
return 0;
}
const bool rectify = !(presignedZone || ::arg().mustDo("disable-axfr-rectify"));
set<DNSName> qnames, nsset, terms;
vector<DNSZoneRecord> zrrs;
// Add the CDNSKEY and CDS records we created earlier
for (auto const &synth_zrr : cds)
zrrs.push_back(synth_zrr);
for (auto const &synth_zrr : cdnskey)
zrrs.push_back(synth_zrr);
while(sd.db->get(zrr)) {
zrr.dr.d_name.makeUsLowerCase();
if(zrr.dr.d_name.isPartOf(target)) {
if (zrr.dr.d_type == QType::ALIAS && ::arg().mustDo("outgoing-axfr-expand-alias")) {
vector<DNSZoneRecord> ips;
int ret1 = stubDoResolve(getRR<ALIASRecordContent>(zrr.dr)->d_content, QType::A, ips);
int ret2 = stubDoResolve(getRR<ALIASRecordContent>(zrr.dr)->d_content, QType::AAAA, ips);
if(ret1 != RCode::NoError || ret2 != RCode::NoError) {
L<<Logger::Error<<"Error resolving for ALIAS "<<zrr.dr.d_content->getZoneRepresentation()<<", aborting AXFR"<<endl;
outpacket->setRcode(RCode::ServFail);
sendPacket(outpacket,outsock);
return 0;
}
for(const auto& ip: ips) {
zrr.dr.d_type = ip.dr.d_type;
zrr.dr.d_content = ip.dr.d_content;
zrrs.push_back(zrr);
}
continue;
}
if (rectify) {
if (zrr.dr.d_type) {
qnames.insert(zrr.dr.d_name);
if(zrr.dr.d_type == QType::NS && zrr.dr.d_name!=target)
nsset.insert(zrr.dr.d_name);
} else {
// remove existing ents
continue;
}
}
zrrs.push_back(zrr);
} else {
if (zrr.dr.d_type)
L<<Logger::Warning<<"Zone '"<<target<<"' contains out-of-zone data '"<<zrr.dr.d_name<<"|"<<DNSRecordContent::NumberToType(zrr.dr.d_type)<<"', ignoring"<<endl;
}
}
// Group records by name and type, signpipe stumbles over interrupted rrsets
sort(zrrs.begin(), zrrs.end(), [](const DNSZoneRecord& a, const DNSZoneRecord& b) {
return tie(a.dr.d_name, a.dr.d_type) < tie(b.dr.d_name, b.dr.d_type);
});
if(rectify) {
// set auth
for(DNSZoneRecord &loopZRR : zrrs) {
loopZRR.auth=true;
if (loopZRR.dr.d_type != QType::NS || loopZRR.dr.d_name!=target) {
DNSName shorter(loopZRR.dr.d_name);
do {
if (shorter==target) // apex is always auth
break;
if(nsset.count(shorter) && !(loopZRR.dr.d_name==shorter && loopZRR.dr.d_type == QType::DS)) {
loopZRR.auth=false;
break;
}
} while(shorter.chopOff());
}
}
if(NSEC3Zone) {
// ents are only required for NSEC3 zones
uint32_t maxent = ::arg().asNum("max-ent-entries");
set<DNSName> nsec3set, nonterm;
for (auto &loopZRR: zrrs) {
bool skip=false;
DNSName shorter = loopZRR.dr.d_name;
if (shorter != target && shorter.chopOff() && shorter != target) {
do {
if(nsset.count(shorter)) {
skip=true;
break;
}
} while(shorter.chopOff() && shorter != target);
}
shorter = loopZRR.dr.d_name;
if(!skip && (loopZRR.dr.d_type != QType::NS || !ns3pr.d_flags)) {
do {
if(!nsec3set.count(shorter)) {
nsec3set.insert(shorter);
}
} while(shorter != target && shorter.chopOff());
}
}
for(DNSZoneRecord &loopZRR : zrrs) {
DNSName shorter(loopZRR.dr.d_name);
while(shorter != target && shorter.chopOff()) {
if(!qnames.count(shorter) && !nonterm.count(shorter) && nsec3set.count(shorter)) {
if(!(maxent)) {
L<<Logger::Warning<<"Zone '"<<target<<"' has too many empty non terminals."<<endl;
return 0;
}
nonterm.insert(shorter);
--maxent;
}
}
}
for(const auto& nt : nonterm) {
DNSZoneRecord tempRR;
tempRR.dr.d_name=nt;
tempRR.dr.d_type=QType::ENT;
tempRR.auth=true;
zrrs.push_back(tempRR);
}
}
}
/* now write all other records */
DNSName keyname;
unsigned int udiff;
DTime dt;
dt.set();
int records=0;
for(DNSZoneRecord &loopZRR : zrrs) {
if (!presignedZone && loopZRR.dr.d_type == QType::RRSIG)
continue;
// only skip the DNSKEY, CDNSKEY and CDS if direct-dnskey is enabled, to avoid changing behaviour
// when it is not enabled.
if(::arg().mustDo("direct-dnskey") && (loopZRR.dr.d_type == QType::DNSKEY || loopZRR.dr.d_type == QType::CDNSKEY || loopZRR.dr.d_type == QType::CDS))
continue;
records++;
if(securedZone && (loopZRR.auth || loopZRR.dr.d_type == QType::NS)) {
if (NSEC3Zone || loopZRR.dr.d_type) {
if (presignedZone && NSEC3Zone && loopZRR.dr.d_type == QType::RRSIG && getRR<RRSIGRecordContent>(loopZRR.dr)->d_type == QType::NSEC3) {
keyname = loopZRR.dr.d_name.makeRelative(sd.qname);
} else {
keyname = NSEC3Zone ? DNSName(toBase32Hex(hashQNameWithSalt(ns3pr, loopZRR.dr.d_name))) : loopZRR.dr.d_name;
}
NSECXEntry& ne = nsecxrepo[keyname];
ne.d_ttl = sd.default_ttl;
ne.d_auth = (ne.d_auth || loopZRR.auth || (NSEC3Zone && (!ns3pr.d_flags)));
if (loopZRR.dr.d_type && loopZRR.dr.d_type != QType::RRSIG) {
ne.d_set.insert(loopZRR.dr.d_type);
}
}
}
if (!loopZRR.dr.d_type)
continue; // skip empty non-terminals
if(loopZRR.dr.d_type == QType::SOA)
continue; // skip SOA - would indicate end of AXFR
if(csp.submit(loopZRR)) {
for(;;) {
outpacket->getRRS() = csp.getChunk();
if(!outpacket->getRRS().empty()) {
if(haveTSIGDetails && !tsigkeyname.empty())
outpacket->setTSIGDetails(trc, tsigkeyname, tsigsecret, trc.d_mac, true);
sendPacket(outpacket, outsock);
trc.d_mac=outpacket->d_trc.d_mac;
outpacket=getFreshAXFRPacket(q);
}
else
break;
}
}
}
/*
udiff=dt.udiffNoReset();
cerr<<"Starting NSEC: "<<csp.d_signed/(udiff/1000000.0)<<" sigs/s, "<<csp.d_signed<<" / "<<udiff/1000000.0<<endl;
cerr<<"Outstanding: "<<csp.d_outstanding<<", "<<csp.d_queued - csp.d_signed << endl;
cerr<<"Ready for consumption: "<<csp.getReady()<<endl;
*/
if(securedZone) {
if(NSEC3Zone) {
for(nsecxrepo_t::const_iterator iter = nsecxrepo.begin(); iter != nsecxrepo.end(); ++iter) {
if(iter->second.d_auth) {
NSEC3RecordContent n3rc;
n3rc.d_set = iter->second.d_set;
if (n3rc.d_set.size() && (n3rc.d_set.size() != 1 || !n3rc.d_set.count(QType::NS)))
n3rc.d_set.insert(QType::RRSIG);
n3rc.d_salt=ns3pr.d_salt;
n3rc.d_flags = ns3pr.d_flags;
n3rc.d_iterations = ns3pr.d_iterations;
n3rc.d_algorithm = 1; // SHA1, fixed in PowerDNS for now
nsecxrepo_t::const_iterator inext = iter;
inext++;
if(inext == nsecxrepo.end())
inext = nsecxrepo.begin();
while(!inext->second.d_auth && inext != iter)
{
inext++;
if(inext == nsecxrepo.end())
inext = nsecxrepo.begin();
}
n3rc.d_nexthash = fromBase32Hex(inext->first.toStringNoDot());
zrr.dr.d_name = iter->first+sd.qname;
zrr.dr.d_ttl = sd.default_ttl;
zrr.dr.d_content = std::make_shared<NSEC3RecordContent>(n3rc);
zrr.dr.d_type = QType::NSEC3;
zrr.dr.d_place = DNSResourceRecord::ANSWER;
zrr.auth=true;
if(csp.submit(zrr)) {
for(;;) {
outpacket->getRRS() = csp.getChunk();
if(!outpacket->getRRS().empty()) {
if(haveTSIGDetails && !tsigkeyname.empty())
outpacket->setTSIGDetails(trc, tsigkeyname, tsigsecret, trc.d_mac, true);
sendPacket(outpacket, outsock);
trc.d_mac=outpacket->d_trc.d_mac;
outpacket=getFreshAXFRPacket(q);
}
else
break;
}
}
}
}
}
else for(nsecxrepo_t::const_iterator iter = nsecxrepo.begin(); iter != nsecxrepo.end(); ++iter) {
NSECRecordContent nrc;
nrc.d_set = iter->second.d_set;
nrc.d_set.insert(QType::RRSIG);
nrc.d_set.insert(QType::NSEC);
if(boost::next(iter) != nsecxrepo.end())
nrc.d_next = boost::next(iter)->first;