]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/ixfrdist.cc
ixfrdist: name threads
[thirdparty/pdns.git] / pdns / ixfrdist.cc
1 /*
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 #include <boost/program_options.hpp>
26 #include <arpa/inet.h>
27 #include <sys/types.h>
28 #include <grp.h>
29 #include <pwd.h>
30 #include <sys/stat.h>
31 #include <mutex>
32 #include <thread>
33 #include <pthread.h>
34 #include <dirent.h>
35 #include <queue>
36 #include <condition_variable>
37 #include "ixfr.hh"
38 #include "ixfrutils.hh"
39 #include "resolver.hh"
40 #include "dns_random.hh"
41 #include "sstuff.hh"
42 #include "mplexer.hh"
43 #include "misc.hh"
44 #include "iputils.hh"
45 #include "logger.hh"
46 #include <yaml-cpp/yaml.h>
47
48 /* BEGIN Needed because of deeper dependencies */
49 #include "arguments.hh"
50 #include "statbag.hh"
51 StatBag S;
52
53 ArgvMap &arg()
54 {
55 static ArgvMap theArg;
56 return theArg;
57 }
58 /* END Needed because of deeper dependencies */
59
60 // Allows reading/writing ComboAddresses and DNSNames in YAML-cpp
61 namespace YAML {
62 template<>
63 struct convert<ComboAddress> {
64 static Node encode(const ComboAddress& rhs) {
65 return Node(rhs.toStringWithPort());
66 }
67 static bool decode(const Node& node, ComboAddress& rhs) {
68 if (!node.IsScalar()) {
69 return false;
70 }
71 try {
72 rhs = ComboAddress(node.as<string>(), 53);
73 return true;
74 } catch(const runtime_error &e) {
75 return false;
76 } catch (const PDNSException &e) {
77 return false;
78 }
79 }
80 };
81
82 template<>
83 struct convert<DNSName> {
84 static Node encode(const DNSName& rhs) {
85 return Node(rhs.toStringRootDot());
86 }
87 static bool decode(const Node& node, DNSName& rhs) {
88 if (!node.IsScalar()) {
89 return false;
90 }
91 try {
92 rhs = DNSName(node.as<string>());
93 return true;
94 } catch(const runtime_error &e) {
95 return false;
96 } catch (const PDNSException &e) {
97 return false;
98 }
99 }
100 };
101 } // namespace YAML
102
103 struct ixfrdiff_t {
104 shared_ptr<SOARecordContent> oldSOA;
105 shared_ptr<SOARecordContent> newSOA;
106 vector<DNSRecord> removals;
107 vector<DNSRecord> additions;
108 };
109
110 struct ixfrinfo_t {
111 shared_ptr<SOARecordContent> soa; // The SOA of the latestAXFR
112 records_t latestAXFR; // The most recent AXFR
113 vector<std::shared_ptr<ixfrdiff_t>> ixfrDiffs;
114 };
115
116 // Why a struct? This way we can add more options to a domain in the future
117 struct ixfrdistdomain_t {
118 set<ComboAddress> masters; // A set so we can do multiple master addresses in the future
119 };
120
121 // This contains the configuration for each domain
122 static map<DNSName, ixfrdistdomain_t> g_domainConfigs;
123
124 // Map domains and their data
125 static std::map<DNSName, std::shared_ptr<ixfrinfo_t>> g_soas;
126 static std::mutex g_soas_mutex;
127
128 // Condition variable for TCP handling
129 static std::condition_variable g_tcpHandlerCV;
130 static std::queue<pair<int, ComboAddress>> g_tcpRequestFDs;
131 static std::mutex g_tcpRequestFDsMutex;
132
133 namespace po = boost::program_options;
134
135 static bool g_exiting = false;
136
137 static NetmaskGroup g_acl;
138 static bool g_compress = false;
139
140 static void handleSignal(int signum) {
141 g_log<<Logger::Notice<<"Got "<<strsignal(signum)<<" signal";
142 if (g_exiting) {
143 g_log<<Logger::Notice<<", this is the second time we were asked to stop, forcefully exiting"<<endl;
144 exit(EXIT_FAILURE);
145 }
146 g_log<<Logger::Notice<<", stopping, this may take a few second due to in-progress transfers and cleanup. Send this signal again to forcefully stop"<<endl;
147 g_exiting = true;
148 }
149
150 static void usage(po::options_description &desc) {
151 cerr << "Usage: ixfrdist [OPTION]..."<<endl;
152 cerr << desc << "\n";
153 }
154
155 // The compiler does not like using rfc1982LessThan in std::sort directly
156 static bool sortSOA(uint32_t i, uint32_t j) {
157 return rfc1982LessThan(i, j);
158 }
159
160 static void cleanUpDomain(const DNSName& domain, const uint16_t& keep, const string& workdir) {
161 string dir = workdir + "/" + domain.toString();
162 DIR *dp;
163 dp = opendir(dir.c_str());
164 if (dp == nullptr) {
165 return;
166 }
167 vector<uint32_t> zoneVersions;
168 struct dirent *d;
169 while ((d = readdir(dp)) != nullptr) {
170 if(!strcmp(d->d_name, ".") || !strcmp(d->d_name, "..")) {
171 continue;
172 }
173 zoneVersions.push_back(std::stoi(d->d_name));
174 }
175 closedir(dp);
176 g_log<<Logger::Info<<"Found "<<zoneVersions.size()<<" versions of "<<domain<<", asked to keep "<<keep<<", ";
177 if (zoneVersions.size() <= keep) {
178 g_log<<Logger::Info<<"not cleaning up"<<endl;
179 return;
180 }
181 g_log<<Logger::Info<<"cleaning up the oldest "<<zoneVersions.size() - keep<<endl;
182
183 // Sort the versions
184 std::sort(zoneVersions.begin(), zoneVersions.end(), sortSOA);
185
186 // And delete all the old ones
187 {
188 // Lock to ensure no one reads this.
189 std::lock_guard<std::mutex> guard(g_soas_mutex);
190 for (auto iter = zoneVersions.cbegin(); iter != zoneVersions.cend() - keep; ++iter) {
191 string fname = dir + "/" + std::to_string(*iter);
192 g_log<<Logger::Debug<<"Removing "<<fname<<endl;
193 unlink(fname.c_str());
194 }
195 }
196 }
197
198 static shared_ptr<SOARecordContent> getSOAFromRecords(const records_t& records) {
199 for (const auto& dnsrecord : records) {
200 if (dnsrecord.d_type == QType::SOA) {
201 auto soa = getRR<SOARecordContent>(dnsrecord);
202 if (soa == nullptr) {
203 throw PDNSException("Unable to determine SOARecordContent from old records");
204 }
205 return soa;
206 }
207 }
208 throw PDNSException("No SOA in supplied records");
209 }
210
211 static void makeIXFRDiff(const records_t& from, const records_t& to, std::shared_ptr<ixfrdiff_t>& diff, const shared_ptr<SOARecordContent>& fromSOA = nullptr, const shared_ptr<SOARecordContent>& toSOA = nullptr) {
212 set_difference(from.cbegin(), from.cend(), to.cbegin(), to.cend(), back_inserter(diff->removals), from.value_comp());
213 set_difference(to.cbegin(), to.cend(), from.cbegin(), from.cend(), back_inserter(diff->additions), from.value_comp());
214 diff->oldSOA = fromSOA;
215 if (fromSOA == nullptr) {
216 diff->oldSOA = getSOAFromRecords(from);
217 }
218 diff->newSOA = toSOA;
219 if (toSOA == nullptr) {
220 diff->newSOA = getSOAFromRecords(to);
221 }
222 }
223
224 /* you can _never_ alter the content of the resulting shared pointer */
225 static std::shared_ptr<ixfrinfo_t> getCurrentZoneInfo(const DNSName& domain)
226 {
227 std::lock_guard<std::mutex> guard(g_soas_mutex);
228 return g_soas[domain];
229 }
230
231 static void updateCurrentZoneInfo(const DNSName& domain, std::shared_ptr<ixfrinfo_t>& newInfo)
232 {
233 std::lock_guard<std::mutex> guard(g_soas_mutex);
234 g_soas[domain] = newInfo;
235 }
236
237 void updateThread(const string& workdir, const uint16_t& keep, const uint16_t& axfrTimeout) {
238 string threadName = "ixfrdist/update";
239 auto retval = pthread_setname_np(pthread_self(), const_cast<char*>(threadName.c_str()));
240 if (retval != 0) {
241 g_log<<Logger::Warning<<"Could not set thread name "<<threadName<<" for update thread: "<<strerror(retval)<<endl;
242 }
243 std::map<DNSName, time_t> lastCheck;
244
245 // Initialize the serials we have
246 for (const auto &domainConfig : g_domainConfigs) {
247 DNSName domain = domainConfig.first;
248 lastCheck[domain] = 0;
249 string dir = workdir + "/" + domain.toString();
250 try {
251 g_log<<Logger::Info<<"Trying to initially load domain "<<domain<<" from disk"<<endl;
252 auto serial = getSerialsFromDir(dir);
253 shared_ptr<SOARecordContent> soa;
254 {
255 string fname = workdir + "/" + domain.toString() + "/" + std::to_string(serial);
256 loadSOAFromDisk(domain, fname, soa);
257 records_t records;
258 if (soa != nullptr) {
259 loadZoneFromDisk(records, fname, domain);
260 }
261 auto zoneInfo = std::make_shared<ixfrinfo_t>();
262 zoneInfo->latestAXFR = std::move(records);
263 zoneInfo->soa = soa;
264 updateCurrentZoneInfo(domain, zoneInfo);
265 }
266 if (soa != nullptr) {
267 g_log<<Logger::Notice<<"Loaded zone "<<domain<<" with serial "<<soa->d_st.serial<<endl;
268 // Initial cleanup
269 cleanUpDomain(domain, keep, workdir);
270 }
271 } catch (runtime_error &e) {
272 // Most likely, the directory does not exist.
273 g_log<<Logger::Info<<e.what()<<", attempting to create"<<endl;
274 // Attempt to create it, if _that_ fails, there is no hope
275 if (mkdir(dir.c_str(), 0777) == -1 && errno != EEXIST) {
276 g_log<<Logger::Error<<"Could not create '"<<dir<<"': "<<strerror(errno)<<endl;
277 exit(EXIT_FAILURE);
278 }
279 }
280 }
281
282 g_log<<Logger::Notice<<"Update Thread started"<<endl;
283
284 while (true) {
285 if (g_exiting) {
286 g_log<<Logger::Notice<<"UpdateThread stopped"<<endl;
287 break;
288 }
289 time_t now = time(nullptr);
290 for (const auto &domainConfig : g_domainConfigs) {
291
292 if (g_exiting) {
293 break;
294 }
295
296 DNSName domain = domainConfig.first;
297 shared_ptr<SOARecordContent> current_soa;
298 const auto& zoneInfo = getCurrentZoneInfo(domain);
299 if (zoneInfo != nullptr) {
300 current_soa = zoneInfo->soa;
301 }
302
303 auto& zoneLastCheck = lastCheck[domain];
304 if ((current_soa != nullptr && now - zoneLastCheck < current_soa->d_st.refresh) || // Only check if we have waited `refresh` seconds
305 (current_soa == nullptr && now - zoneLastCheck < 30)) { // Or if we could not get an update at all still, every 30 seconds
306 continue;
307 }
308
309 // TODO Keep track of 'down' masters
310 set<ComboAddress>::const_iterator it(domainConfig.second.masters.begin());
311 std::advance(it, random() % domainConfig.second.masters.size());
312 ComboAddress master = *it;
313
314 string dir = workdir + "/" + domain.toString();
315 g_log<<Logger::Info<<"Attempting to retrieve SOA Serial update for '"<<domain<<"' from '"<<master.toStringWithPort()<<"'"<<endl;
316 shared_ptr<SOARecordContent> sr;
317 try {
318 zoneLastCheck = now;
319 auto newSerial = getSerialFromMaster(master, domain, sr); // TODO TSIG
320 if(current_soa != nullptr) {
321 g_log<<Logger::Info<<"Got SOA Serial for "<<domain<<" from "<<master.toStringWithPort()<<": "<< newSerial<<", had Serial: "<<current_soa->d_st.serial;
322 if (newSerial == current_soa->d_st.serial) {
323 g_log<<Logger::Info<<", not updating."<<endl;
324 continue;
325 }
326 g_log<<Logger::Info<<", will update."<<endl;
327 }
328 } catch (runtime_error &e) {
329 g_log<<Logger::Warning<<"Unable to get SOA serial update for '"<<domain<<"' from master "<<master.toStringWithPort()<<": "<<e.what()<<endl;
330 continue;
331 }
332 // Now get the full zone!
333 g_log<<Logger::Info<<"Attempting to receive full zonedata for '"<<domain<<"'"<<endl;
334 ComboAddress local = master.isIPv4() ? ComboAddress("0.0.0.0") : ComboAddress("::");
335 TSIGTriplet tt;
336
337 // The *new* SOA
338 shared_ptr<SOARecordContent> soa;
339 records_t records;
340 try {
341 AXFRRetriever axfr(master, domain, tt, &local);
342 unsigned int nrecords=0;
343 Resolver::res_t nop;
344 vector<DNSRecord> chunk;
345 time_t t_start = time(nullptr);
346 time_t axfr_now = time(nullptr);
347 while(axfr.getChunk(nop, &chunk, (axfr_now - t_start + axfrTimeout))) {
348 for(auto& dr : chunk) {
349 if(dr.d_type == QType::TSIG)
350 continue;
351 dr.d_name.makeUsRelative(domain);
352 records.insert(dr);
353 nrecords++;
354 if (dr.d_type == QType::SOA) {
355 soa = getRR<SOARecordContent>(dr);
356 }
357 }
358 axfr_now = time(nullptr);
359 if (axfr_now - t_start > axfrTimeout) {
360 throw PDNSException("Total AXFR time exceeded!");
361 }
362 }
363 if (soa == nullptr) {
364 g_log<<Logger::Warning<<"No SOA was found in the AXFR of "<<domain<<endl;
365 continue;
366 }
367 g_log<<Logger::Notice<<"Retrieved all zone data for "<<domain<<". Received "<<nrecords<<" records."<<endl;
368 } catch (PDNSException &e) {
369 g_log<<Logger::Warning<<"Could not retrieve AXFR for '"<<domain<<"': "<<e.reason<<endl;
370 continue;
371 } catch (runtime_error &e) {
372 g_log<<Logger::Warning<<"Could not retrieve AXFR for zone '"<<domain<<"': "<<e.what()<<endl;
373 continue;
374 }
375
376 try {
377
378 writeZoneToDisk(records, domain, dir);
379 g_log<<Logger::Notice<<"Wrote zonedata for "<<domain<<" with serial "<<soa->d_st.serial<<" to "<<dir<<endl;
380
381 const auto oldZoneInfo = getCurrentZoneInfo(domain);
382 auto zoneInfo = std::make_shared<ixfrinfo_t>();
383
384 if (oldZoneInfo && !oldZoneInfo->latestAXFR.empty()) {
385 auto diff = std::make_shared<ixfrdiff_t>();
386 zoneInfo->ixfrDiffs = oldZoneInfo->ixfrDiffs;
387 g_log<<Logger::Debug<<"Calculating diff for "<<domain<<endl;
388 makeIXFRDiff(oldZoneInfo->latestAXFR, records, diff, oldZoneInfo->soa, soa);
389 g_log<<Logger::Debug<<"Calculated diff for "<<domain<<", we had "<<diff->removals.size()<<" removals and "<<diff->additions.size()<<" additions"<<endl;
390 zoneInfo->ixfrDiffs.push_back(std::move(diff));
391 }
392
393 // Clean up the diffs
394 while (zoneInfo->ixfrDiffs.size() > keep) {
395 zoneInfo->ixfrDiffs.erase(zoneInfo->ixfrDiffs.begin());
396 }
397
398 g_log<<Logger::Debug<<"Zone "<<domain<<" previously contained "<<(oldZoneInfo ? oldZoneInfo->latestAXFR.size() : 0)<<" entries, "<<records.size()<<" now"<<endl;
399 zoneInfo->latestAXFR = std::move(records);
400 zoneInfo->soa = soa;
401 updateCurrentZoneInfo(domain, zoneInfo);
402 } catch (PDNSException &e) {
403 g_log<<Logger::Warning<<"Could not save zone '"<<domain<<"' to disk: "<<e.reason<<endl;
404 } catch (runtime_error &e) {
405 g_log<<Logger::Warning<<"Could not save zone '"<<domain<<"' to disk: "<<e.what()<<endl;
406 }
407
408 // Now clean up the directory
409 cleanUpDomain(domain, keep, workdir);
410 } /* for (const auto &domain : domains) */
411 sleep(1);
412 } /* while (true) */
413 } /* updateThread */
414
415 static bool checkQuery(const MOADNSParser& mdp, const ComboAddress& saddr, const bool udp = true, const string& logPrefix="") {
416 vector<string> info_msg;
417
418 g_log<<Logger::Debug<<logPrefix<<"Had "<<mdp.d_qname<<"|"<<QType(mdp.d_qtype).getName()<<" query from "<<saddr.toStringWithPort()<<endl;
419
420 if (udp && mdp.d_qtype != QType::SOA && mdp.d_qtype != QType::IXFR) {
421 info_msg.push_back("QType is unsupported (" + QType(mdp.d_qtype).getName() + " is not in {SOA,IXFR})");
422 }
423
424 if (!udp && mdp.d_qtype != QType::SOA && mdp.d_qtype != QType::IXFR && mdp.d_qtype != QType::AXFR) {
425 info_msg.push_back("QType is unsupported (" + QType(mdp.d_qtype).getName() + " is not in {SOA,IXFR,AXFR})");
426 }
427
428 {
429 if (g_domainConfigs.find(mdp.d_qname) == g_domainConfigs.end()) {
430 info_msg.push_back("Domain name '" + mdp.d_qname.toLogString() + "' is not configured for distribution");
431 }
432
433 const auto zoneInfo = getCurrentZoneInfo(mdp.d_qname);
434 if (zoneInfo == nullptr) {
435 info_msg.push_back("Domain has not been transferred yet");
436 }
437 }
438
439 if (!info_msg.empty()) {
440 g_log<<Logger::Warning<<logPrefix<<"Ignoring "<<mdp.d_qname<<"|"<<QType(mdp.d_qtype).getName()<<" query from "<<saddr.toStringWithPort();
441 g_log<<Logger::Warning<<": ";
442 bool first = true;
443 for (const auto& s : info_msg) {
444 if (!first) {
445 g_log<<Logger::Warning<<", ";
446 first = false;
447 }
448 g_log<<Logger::Warning<<s;
449 }
450 g_log<<Logger::Warning<<endl;
451 return false;
452 }
453
454 return true;
455 }
456
457 /*
458 * Returns a vector<uint8_t> that represents the full response to a SOA
459 * query. QNAME is read from mdp.
460 */
461 static bool makeSOAPacket(const MOADNSParser& mdp, vector<uint8_t>& packet) {
462
463 auto zoneInfo = getCurrentZoneInfo(mdp.d_qname);
464 if (zoneInfo == nullptr) {
465 return false;
466 }
467
468 DNSPacketWriter pw(packet, mdp.d_qname, mdp.d_qtype);
469 pw.getHeader()->id = mdp.d_header.id;
470 pw.getHeader()->rd = mdp.d_header.rd;
471 pw.getHeader()->qr = 1;
472
473 pw.startRecord(mdp.d_qname, QType::SOA);
474 zoneInfo->soa->toPacket(pw);
475 pw.commit();
476
477 return true;
478 }
479
480 static vector<uint8_t> getSOAPacket(const MOADNSParser& mdp, const shared_ptr<SOARecordContent>& soa) {
481 vector<uint8_t> packet;
482 DNSPacketWriter pw(packet, mdp.d_qname, mdp.d_qtype);
483 pw.getHeader()->id = mdp.d_header.id;
484 pw.getHeader()->rd = mdp.d_header.rd;
485 pw.getHeader()->qr = 1;
486
487 // Add the first SOA
488 pw.startRecord(mdp.d_qname, QType::SOA);
489 soa->toPacket(pw);
490 pw.commit();
491 return packet;
492 }
493
494 static bool sendPacketOverTCP(int fd, const std::vector<uint8_t>& packet)
495 {
496 char sendBuf[2];
497 sendBuf[0]=packet.size()/256;
498 sendBuf[1]=packet.size()%256;
499
500 ssize_t send = writen2(fd, sendBuf, 2);
501 send += writen2(fd, &packet[0], packet.size());
502 return true;
503 }
504
505 static bool addRecordToWriter(DNSPacketWriter& pw, const DNSName& zoneName, const DNSRecord& record, bool compress)
506 {
507 pw.startRecord(record.d_name + zoneName, record.d_type, record.d_ttl, QClass::IN, DNSResourceRecord::ANSWER, compress);
508 record.d_content->toPacket(pw);
509 if (pw.size() > 65535) {
510 pw.rollback();
511 return false;
512 }
513 return true;
514 }
515
516 template <typename T> static bool sendRecordsOverTCP(int fd, const MOADNSParser& mdp, const T& records)
517 {
518 vector<uint8_t> packet;
519
520 for (auto it = records.cbegin(); it != records.cend();) {
521 bool recordsAdded = false;
522 packet.clear();
523 DNSPacketWriter pw(packet, mdp.d_qname, mdp.d_qtype);
524 pw.getHeader()->id = mdp.d_header.id;
525 pw.getHeader()->rd = mdp.d_header.rd;
526 pw.getHeader()->qr = 1;
527
528 while (it != records.cend()) {
529 if (it->d_type == QType::SOA) {
530 it++;
531 continue;
532 }
533
534 if (addRecordToWriter(pw, mdp.d_qname, *it, g_compress)) {
535 recordsAdded = true;
536 it++;
537 }
538 else {
539 if (recordsAdded) {
540 pw.commit();
541 sendPacketOverTCP(fd, packet);
542 }
543 if (it == records.cbegin()) {
544 /* something is wrong */
545 return false;
546 }
547
548 break;
549 }
550 }
551
552 if (it == records.cend() && recordsAdded) {
553 pw.commit();
554 sendPacketOverTCP(fd, packet);
555 }
556 }
557
558 return true;
559 }
560
561
562 static bool handleAXFR(int fd, const MOADNSParser& mdp) {
563 /* we get a shared pointer of the zone info that we can't modify, ever.
564 A newer one may arise in the meantime, but this one will stay valid
565 until we release it.
566 */
567 auto zoneInfo = getCurrentZoneInfo(mdp.d_qname);
568 if (zoneInfo == nullptr) {
569 return false;
570 }
571
572 shared_ptr<SOARecordContent> soa = zoneInfo->soa;
573 const records_t& records = zoneInfo->latestAXFR;
574
575 // Initial SOA
576 const auto soaPacket = getSOAPacket(mdp, soa);
577 if (!sendPacketOverTCP(fd, soaPacket)) {
578 return false;
579 }
580
581 if (!sendRecordsOverTCP(fd, mdp, records)) {
582 return false;
583 }
584
585 // Final SOA
586 if (!sendPacketOverTCP(fd, soaPacket)) {
587 return false;
588 }
589
590 return true;
591 }
592
593 /* Produces an IXFR if one can be made according to the rules in RFC 1995 and
594 * creates a SOA or AXFR packet when required by the RFC.
595 */
596 static bool handleIXFR(int fd, const ComboAddress& destination, const MOADNSParser& mdp, const shared_ptr<SOARecordContent>& clientSOA) {
597 vector<std::shared_ptr<ixfrdiff_t>> toSend;
598
599 /* we get a shared pointer of the zone info that we can't modify, ever.
600 A newer one may arise in the meantime, but this one will stay valid
601 until we release it.
602 */
603 auto zoneInfo = getCurrentZoneInfo(mdp.d_qname);
604 if (zoneInfo == nullptr) {
605 return false;
606 }
607
608 uint32_t ourLatestSerial = zoneInfo->soa->d_st.serial;
609
610 if (rfc1982LessThan(ourLatestSerial, clientSOA->d_st.serial) || ourLatestSerial == clientSOA->d_st.serial) {
611 /* RFC 1995 Section 2
612 * If an IXFR query with the same or newer version number than that of
613 * the server is received, it is replied to with a single SOA record of
614 * the server's current version, just as in AXFR.
615 */
616 vector<uint8_t> packet;
617 bool ret = makeSOAPacket(mdp, packet);
618 if (ret) {
619 sendPacketOverTCP(fd, packet);
620 }
621 return ret;
622 }
623
624 // as we use push_back in the updater, we know the vector is sorted as oldest first
625 bool shouldAdd = false;
626 // Get all relevant IXFR differences
627 for (const auto& diff : zoneInfo->ixfrDiffs) {
628 if (shouldAdd) {
629 toSend.push_back(diff);
630 continue;
631 }
632 if (diff->oldSOA->d_st.serial == clientSOA->d_st.serial) {
633 toSend.push_back(diff);
634 // Add all consecutive diffs
635 shouldAdd = true;
636 }
637 }
638
639 if (toSend.empty()) {
640 g_log<<Logger::Warning<<"No IXFR available from serial "<<clientSOA->d_st.serial<<" for zone "<<mdp.d_qname<<", attempting to send AXFR"<<endl;
641 return handleAXFR(fd, mdp);
642 }
643
644 std::vector<std::vector<uint8_t>> packets;
645 for (const auto& diff : toSend) {
646 /* An IXFR packet's ANSWER section looks as follows:
647 * SOA new_serial
648 * SOA old_serial
649 * ... removed records ...
650 * SOA new_serial
651 * ... added records ...
652 * SOA new_serial
653 */
654
655 const auto newSOAPacket = getSOAPacket(mdp, diff->newSOA);
656 const auto oldSOAPacket = getSOAPacket(mdp, diff->oldSOA);
657
658 if (!sendPacketOverTCP(fd, newSOAPacket)) {
659 return false;
660 }
661
662 if (!sendPacketOverTCP(fd, oldSOAPacket)) {
663 return false;
664 }
665
666 if (!sendRecordsOverTCP(fd, mdp, diff->removals)) {
667 return false;
668 }
669
670 if (!sendPacketOverTCP(fd, newSOAPacket)) {
671 return false;
672 }
673
674 if (!sendRecordsOverTCP(fd, mdp, diff->additions)) {
675 return false;
676 }
677
678 if (!sendPacketOverTCP(fd, newSOAPacket)) {
679 return false;
680 }
681 }
682
683 return true;
684 }
685
686 static bool allowedByACL(const ComboAddress& addr) {
687 return g_acl.match(addr);
688 }
689
690 static void handleUDPRequest(int fd, boost::any&) {
691 // TODO make the buffer-size configurable
692 char buf[4096];
693 ComboAddress saddr;
694 socklen_t fromlen = sizeof(saddr);
695 int res = recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr*) &saddr, &fromlen);
696
697 if (res == 0) {
698 g_log<<Logger::Warning<<"Got an empty message from "<<saddr.toStringWithPort()<<endl;
699 return;
700 }
701
702 if(res < 0) {
703 auto savedErrno = errno;
704 g_log<<Logger::Warning<<"Could not read message from "<<saddr.toStringWithPort()<<": "<<strerror(savedErrno)<<endl;
705 return;
706 }
707
708 if (saddr == ComboAddress("0.0.0.0", 0)) {
709 g_log<<Logger::Warning<<"Could not determine source of message"<<endl;
710 return;
711 }
712
713 if (!allowedByACL(saddr)) {
714 g_log<<Logger::Warning<<"UDP query from "<<saddr.toString()<<" is not allowed, dropping"<<endl;
715 return;
716 }
717
718 MOADNSParser mdp(true, string(buf, res));
719 if (!checkQuery(mdp, saddr)) {
720 return;
721 }
722
723 /* RFC 1995 Section 2
724 * Transport of a query may be by either UDP or TCP. If an IXFR query
725 * is via UDP, the IXFR server may attempt to reply using UDP if the
726 * entire response can be contained in a single DNS packet. If the UDP
727 * reply does not fit, the query is responded to with a single SOA
728 * record of the server's current version to inform the client that a
729 * TCP query should be initiated.
730 *
731 * Let's not complicate this with IXFR over UDP (and looking if we need to truncate etc).
732 * Just send the current SOA and let the client try over TCP
733 */
734 vector<uint8_t> packet;
735 makeSOAPacket(mdp, packet);
736 if(sendto(fd, &packet[0], packet.size(), 0, (struct sockaddr*) &saddr, fromlen) < 0) {
737 auto savedErrno = errno;
738 g_log<<Logger::Warning<<"Could not send reply for "<<mdp.d_qname<<"|"<<QType(mdp.d_qtype).getName()<<" to "<<saddr.toStringWithPort()<<": "<<strerror(savedErrno)<<endl;
739 }
740 return;
741 }
742
743 static void handleTCPRequest(int fd, boost::any&) {
744 ComboAddress saddr;
745 int cfd = 0;
746
747 try {
748 cfd = SAccept(fd, saddr);
749 setBlocking(cfd);
750 } catch(runtime_error &e) {
751 g_log<<Logger::Error<<e.what()<<endl;
752 return;
753 }
754
755 if (saddr == ComboAddress("0.0.0.0", 0)) {
756 g_log<<Logger::Warning<<"Could not determine source of message"<<endl;
757 close(cfd);
758 return;
759 }
760
761 if (!allowedByACL(saddr)) {
762 g_log<<Logger::Warning<<"TCP query from "<<saddr.toString()<<" is not allowed, dropping"<<endl;
763 close(cfd);
764 return;
765 }
766
767 {
768 std::lock_guard<std::mutex> lg(g_tcpRequestFDsMutex);
769 g_tcpRequestFDs.push({cfd, saddr});
770 }
771 g_tcpHandlerCV.notify_one();
772 }
773
774 /* Thread to handle TCP traffic
775 */
776 static void tcpWorker(int tid) {
777 string threadName = "ixfrdist/tcpWor";
778 auto retval = pthread_setname_np(pthread_self(), const_cast<char*>(threadName.c_str()));
779 if (retval != 0) {
780 g_log<<Logger::Warning<<"Could not set thread name "<<threadName<<" for update thread: "<<strerror(retval)<<endl;
781 }
782 string prefix = "TCP Worker " + std::to_string(tid) + ": ";
783
784 while(true) {
785 g_log<<Logger::Debug<<prefix<<"ready for a new request!"<<endl;
786 std::unique_lock<std::mutex> lk(g_tcpRequestFDsMutex);
787 g_tcpHandlerCV.wait(lk, []{return g_tcpRequestFDs.size() || g_exiting ;});
788 if (g_exiting) {
789 g_log<<Logger::Debug<<prefix<<"Stopping thread"<<endl;
790 break;
791 }
792 g_log<<Logger::Debug<<prefix<<"Going to handle a query"<<endl;
793 auto request = g_tcpRequestFDs.front();
794 g_tcpRequestFDs.pop();
795 lk.unlock();
796
797 int cfd = request.first;
798 ComboAddress saddr = request.second;
799
800 char buf[4096];
801 ssize_t res;
802 try {
803 uint16_t toRead;
804 readn2(cfd, &toRead, sizeof(toRead));
805 toRead = std::min(ntohs(toRead), static_cast<uint16_t>(sizeof(buf)));
806 res = readn2WithTimeout(cfd, &buf, toRead, 2);
807 g_log<<Logger::Debug<<prefix<<"Had message of "<<std::to_string(toRead)<<" bytes from "<<saddr.toStringWithPort()<<endl;
808 } catch (runtime_error &e) {
809 g_log<<Logger::Warning<<prefix<<"Could not read message from "<<saddr.toStringWithPort()<<": "<<e.what()<<endl;
810 close(cfd);
811 continue;
812 }
813
814 try {
815 MOADNSParser mdp(true, string(buf, res));
816
817 if (!checkQuery(mdp, saddr, false, prefix)) {
818 close(cfd);
819 continue;
820 }
821
822 if (mdp.d_qtype == QType::SOA) {
823 vector<uint8_t> packet;
824 bool ret = makeSOAPacket(mdp, packet);
825 if (!ret) {
826 close(cfd);
827 continue;
828 }
829 sendPacketOverTCP(cfd, packet);
830 }
831 else if (mdp.d_qtype == QType::AXFR) {
832 if (!handleAXFR(cfd, mdp)) {
833 close(cfd);
834 continue;
835 }
836 }
837 else if (mdp.d_qtype == QType::IXFR) {
838 /* RFC 1995 section 3:
839 * The IXFR query packet format is the same as that of a normal DNS
840 * query, but with the query type being IXFR and the authority section
841 * containing the SOA record of client's version of the zone.
842 */
843 shared_ptr<SOARecordContent> clientSOA;
844 for (auto &answer : mdp.d_answers) {
845 // from dnsparser.hh:
846 // typedef vector<pair<DNSRecord, uint16_t > > answers_t;
847 if (answer.first.d_type == QType::SOA && answer.first.d_place == DNSResourceRecord::AUTHORITY) {
848 clientSOA = getRR<SOARecordContent>(answer.first);
849 if (clientSOA != nullptr) {
850 break;
851 }
852 }
853 } /* for (auto const &answer : mdp.d_answers) */
854
855 if (clientSOA == nullptr) {
856 g_log<<Logger::Warning<<prefix<<"IXFR request packet did not contain a SOA record in the AUTHORITY section"<<endl;
857 close(cfd);
858 continue;
859 }
860
861 if (!handleIXFR(cfd, saddr, mdp, clientSOA)) {
862 close(cfd);
863 continue;
864 }
865 } /* if (mdp.d_qtype == QType::IXFR) */
866
867 shutdown(cfd, 2);
868 } catch (MOADNSException &e) {
869 g_log<<Logger::Warning<<prefix<<"Could not parse DNS packet from "<<saddr.toStringWithPort()<<": "<<e.what()<<endl;
870 } catch (runtime_error &e) {
871 g_log<<Logger::Warning<<prefix<<"Could not write reply to "<<saddr.toStringWithPort()<<": "<<e.what()<<endl;
872 }
873 // bye!
874 close(cfd);
875
876 if (g_exiting) {
877 break;
878 }
879 }
880 }
881
882 /* Parses the configuration file in configpath into config, adding defaults for
883 * missing parameters (if applicable), returning true if the config file was
884 * good, false otherwise. Will log all issues with the config
885 */
886 static bool parseAndCheckConfig(const string& configpath, YAML::Node& config) {
887 g_log<<Logger::Info<<"Loading configuration file from "<<configpath<<endl;
888 try {
889 config = YAML::LoadFile(configpath);
890 } catch (const runtime_error &e) {
891 g_log<<Logger::Error<<"Unable to load configuration file '"<<configpath<<"': "<<e.what()<<endl;
892 return false;
893 }
894
895 bool retval = true;
896
897 if (config["keep"]) {
898 try {
899 config["keep"].as<uint16_t>();
900 } catch (const runtime_error &e) {
901 g_log<<Logger::Error<<"Unable to read 'keep' value: "<<e.what()<<endl;
902 retval = false;
903 }
904 } else {
905 config["keep"] = 20;
906 }
907
908 if (config["axfr-timeout"]) {
909 try {
910 config["axfr-timeout"].as<uint16_t>();
911 } catch (const runtime_error &e) {
912 g_log<<Logger::Error<<"Unable to read 'axfr-timeout' value: "<<e.what()<<endl;
913 }
914 } else {
915 config["axfr-timeout"] = 20;
916 }
917
918 if (config["tcp-in-threads"]) {
919 try {
920 config["tcp-in-threads"].as<uint16_t>();
921 } catch (const runtime_error &e) {
922 g_log<<Logger::Error<<"Unable to read 'tcp-in-threads' value: "<<e.what()<<endl;
923 }
924 } else {
925 config["tcp-in-threads"] = 10;
926 }
927
928 if (config["listen"]) {
929 try {
930 config["listen"].as<vector<ComboAddress>>();
931 } catch (const runtime_error &e) {
932 g_log<<Logger::Error<<"Unable to read 'listen' value: "<<e.what()<<endl;
933 retval = false;
934 }
935 } else {
936 config["listen"].push_back("127.0.0.1:53");
937 config["listen"].push_back("[::1]:53");
938 }
939
940 if (config["acl"]) {
941 try {
942 config["acl"].as<vector<string>>();
943 } catch (const runtime_error &e) {
944 g_log<<Logger::Error<<"Unable to read 'acl' value: "<<e.what()<<endl;
945 retval = false;
946 }
947 } else {
948 config["acl"].push_back("127.0.0.0/8");
949 config["acl"].push_back("::1/128");
950 }
951
952 if (config["work-dir"]) {
953 try {
954 config["work-dir"].as<string>();
955 } catch(const runtime_error &e) {
956 g_log<<Logger::Error<<"Unable to read 'work-dir' value: "<<e.what()<<endl;
957 retval = false;
958 }
959 } else {
960 char tmp[512];
961 config["work-dir"] = getcwd(tmp, sizeof(tmp)) ? string(tmp) : "";;
962 }
963
964 if (config["uid"]) {
965 try {
966 config["uid"].as<string>();
967 } catch(const runtime_error &e) {
968 g_log<<Logger::Error<<"Unable to read 'uid' value: "<<e.what()<<endl;
969 retval = false;
970 }
971 }
972
973 if (config["gid"]) {
974 try {
975 config["gid"].as<string>();
976 } catch(const runtime_error &e) {
977 g_log<<Logger::Error<<"Unable to read 'gid' value: "<<e.what()<<endl;
978 retval = false;
979 }
980 }
981
982 if (config["domains"]) {
983 if (config["domains"].size() == 0) {
984 g_log<<Logger::Error<<"No domains configured"<<endl;
985 retval = false;
986 }
987 for (auto const &domain : config["domains"]) {
988 try {
989 if (!domain["domain"]) {
990 g_log<<Logger::Error<<"An entry in 'domains' is missing a 'domain' key!"<<endl;
991 retval = false;
992 continue;
993 }
994 domain["domain"].as<DNSName>();
995 } catch (const runtime_error &e) {
996 g_log<<Logger::Error<<"Unable to read domain '"<<domain["domain"].as<string>()<<"': "<<e.what()<<endl;
997 }
998 try {
999 if (!domain["master"]) {
1000 g_log<<Logger::Error<<"Domain '"<<domain["domain"].as<string>()<<"' has no master configured!"<<endl;
1001 retval = false;
1002 continue;
1003 }
1004 domain["master"].as<ComboAddress>();
1005 } catch (const runtime_error &e) {
1006 g_log<<Logger::Error<<"Unable to read domain '"<<domain["domain"].as<string>()<<"' master address: "<<e.what()<<endl;
1007 retval = false;
1008 }
1009 }
1010 } else {
1011 g_log<<Logger::Error<<"No domains configured"<<endl;
1012 retval = false;
1013 }
1014
1015 if (config["compress"]) {
1016 try {
1017 config["compress"].as<bool>();
1018 }
1019 catch (const runtime_error &e) {
1020 g_log<<Logger::Error<<"Unable to read 'compress' value: "<<e.what()<<endl;
1021 retval = false;
1022 }
1023 }
1024 else {
1025 config["compress"] = false;
1026 }
1027
1028 return retval;
1029 }
1030
1031 int main(int argc, char** argv) {
1032 g_log.setLoglevel(Logger::Notice);
1033 g_log.toConsole(Logger::Notice);
1034 g_log.setPrefixed(true);
1035 g_log.disableSyslog(true);
1036 g_log.setTimestamps(false);
1037 po::variables_map g_vm;
1038 try {
1039 po::options_description desc("IXFR distribution tool");
1040 desc.add_options()
1041 ("help", "produce help message")
1042 ("version", "Display the version of ixfrdist")
1043 ("verbose", "Be verbose")
1044 ("debug", "Be even more verbose")
1045 ("config", po::value<string>()->default_value(SYSCONFDIR + string("/ixfrdist.yml")), "Configuration file to use")
1046 ;
1047
1048 po::store(po::command_line_parser(argc, argv).options(desc).run(), g_vm);
1049 po::notify(g_vm);
1050
1051 if (g_vm.count("help") > 0) {
1052 usage(desc);
1053 return EXIT_SUCCESS;
1054 }
1055
1056 if (g_vm.count("version") > 0) {
1057 cout<<"ixfrdist "<<VERSION<<endl;
1058 return EXIT_SUCCESS;
1059 }
1060 } catch (po::error &e) {
1061 g_log<<Logger::Error<<e.what()<<". See `ixfrdist --help` for valid options"<<endl;
1062 return(EXIT_FAILURE);
1063 }
1064
1065 bool had_error = false;
1066
1067 if (g_vm.count("verbose")) {
1068 g_log.setLoglevel(Logger::Info);
1069 g_log.toConsole(Logger::Info);
1070 }
1071
1072 if (g_vm.count("debug") > 0) {
1073 g_log.setLoglevel(Logger::Debug);
1074 g_log.toConsole(Logger::Debug);
1075 }
1076
1077 g_log<<Logger::Notice<<"IXFR distributor version "<<VERSION<<" starting up!"<<endl;
1078
1079 YAML::Node config;
1080 if (!parseAndCheckConfig(g_vm["config"].as<string>(), config)) {
1081 // parseAndCheckConfig already logged whatever was wrong
1082 return EXIT_FAILURE;
1083 }
1084
1085 /* From hereon out, we known that all the values in config are valid. */
1086
1087 for (auto const &domain : config["domains"]) {
1088 set<ComboAddress> s;
1089 s.insert(domain["master"].as<ComboAddress>());
1090 g_domainConfigs[domain["domain"].as<DNSName>()].masters = s;
1091 }
1092
1093 for (const auto &addr : config["acl"].as<vector<string>>()) {
1094 try {
1095 g_acl.addMask(addr);
1096 } catch (const NetmaskException &e) {
1097 g_log<<Logger::Error<<e.reason<<endl;
1098 had_error = true;
1099 }
1100 }
1101 g_log<<Logger::Notice<<"ACL set to "<<g_acl.toString()<<"."<<endl;
1102
1103 if (config["compress"]) {
1104 g_compress = config["compress"].as<bool>();
1105 if (g_compress) {
1106 g_log<<Logger::Notice<<"Record compression is enabled."<<endl;
1107 }
1108 }
1109
1110 FDMultiplexer* fdm = FDMultiplexer::getMultiplexerSilent();
1111 if (fdm == nullptr) {
1112 g_log<<Logger::Error<<"Could not enable a multiplexer for the listen sockets!"<<endl;
1113 return EXIT_FAILURE;
1114 }
1115
1116 set<int> allSockets;
1117 for (const auto& addr : config["listen"].as<vector<ComboAddress>>()) {
1118 for (const auto& stype : {SOCK_DGRAM, SOCK_STREAM}) {
1119 try {
1120 int s = SSocket(addr.sin4.sin_family, stype, 0);
1121 setNonBlocking(s);
1122 setReuseAddr(s);
1123 SBind(s, addr);
1124 if (stype == SOCK_STREAM) {
1125 SListen(s, 30); // TODO make this configurable
1126 }
1127 fdm->addReadFD(s, stype == SOCK_DGRAM ? handleUDPRequest : handleTCPRequest);
1128 allSockets.insert(s);
1129 } catch(runtime_error &e) {
1130 g_log<<Logger::Error<<e.what()<<endl;
1131 had_error = true;
1132 continue;
1133 }
1134 }
1135 }
1136
1137 int newgid = 0;
1138
1139 if (config["gid"]) {
1140 string gid = config["gid"].as<string>();
1141 if (!(newgid = atoi(gid.c_str()))) {
1142 struct group *gr = getgrnam(gid.c_str());
1143 if (gr == nullptr) {
1144 g_log<<Logger::Error<<"Can not determine group-id for gid "<<gid<<endl;
1145 had_error = true;
1146 } else {
1147 newgid = gr->gr_gid;
1148 }
1149 }
1150 g_log<<Logger::Notice<<"Dropping effective group-id to "<<newgid<<endl;
1151 if (setgid(newgid) < 0) {
1152 g_log<<Logger::Error<<"Could not set group id to "<<newgid<<": "<<stringerror()<<endl;
1153 had_error = true;
1154 }
1155 }
1156
1157 int newuid = 0;
1158
1159 if (config["uid"]) {
1160 string uid = config["uid"].as<string>();
1161 if (!(newuid = atoi(uid.c_str()))) {
1162 struct passwd *pw = getpwnam(uid.c_str());
1163 if (pw == nullptr) {
1164 g_log<<Logger::Error<<"Can not determine user-id for uid "<<uid<<endl;
1165 had_error = true;
1166 } else {
1167 newuid = pw->pw_uid;
1168 }
1169 }
1170
1171 struct passwd *pw = getpwuid(newuid);
1172 if (pw == nullptr) {
1173 if (setgroups(0, nullptr) < 0) {
1174 g_log<<Logger::Error<<"Unable to drop supplementary gids: "<<stringerror()<<endl;
1175 had_error = true;
1176 }
1177 } else {
1178 if (initgroups(pw->pw_name, newgid) < 0) {
1179 g_log<<Logger::Error<<"Unable to set supplementary groups: "<<stringerror()<<endl;
1180 had_error = true;
1181 }
1182 }
1183
1184 g_log<<Logger::Notice<<"Dropping effective user-id to "<<newuid<<endl;
1185 if (setuid(newuid) < 0) {
1186 g_log<<Logger::Error<<"Could not set user id to "<<newuid<<": "<<stringerror()<<endl;
1187 had_error = true;
1188 }
1189 }
1190
1191 if (had_error) {
1192 // We have already sent the errors to stderr, just die
1193 return EXIT_FAILURE;
1194 }
1195
1196 // It all starts here
1197 signal(SIGTERM, handleSignal);
1198 signal(SIGINT, handleSignal);
1199 signal(SIGPIPE, SIG_IGN);
1200
1201 // Init the things we need
1202 reportAllTypes();
1203
1204 dns_random_init();
1205
1206 std::thread ut(updateThread,
1207 config["work-dir"].as<string>(),
1208 config["keep"].as<uint16_t>(),
1209 config["axfr-timeout"].as<uint16_t>());
1210
1211 vector<std::thread> tcpHandlers;
1212 tcpHandlers.reserve(config["tcp-in-threads"].as<uint16_t>());
1213 for (size_t i = 0; i < tcpHandlers.capacity(); ++i) {
1214 tcpHandlers.push_back(std::thread(tcpWorker, i));
1215 }
1216
1217 struct timeval now;
1218 for(;;) {
1219 gettimeofday(&now, 0);
1220 fdm->run(&now);
1221 if (g_exiting) {
1222 g_log<<Logger::Debug<<"Closing listening sockets"<<endl;
1223 for (const int& fd : allSockets) {
1224 try {
1225 closesocket(fd);
1226 } catch(PDNSException &e) {
1227 g_log<<Logger::Error<<e.reason<<endl;
1228 }
1229 }
1230 break;
1231 }
1232 }
1233 g_log<<Logger::Debug<<"Waiting for al threads to stop"<<endl;
1234 g_tcpHandlerCV.notify_all();
1235 ut.join();
1236 for (auto &t : tcpHandlers) {
1237 t.join();
1238 }
1239 g_log<<Logger::Notice<<"IXFR distributor stopped"<<endl;
1240 return EXIT_SUCCESS;
1241 }