]>
git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/serialtweaker.cc
2 PowerDNS Versatile Database Driven Nameserver
3 Copyright (C) 2002-2011 PowerDNS.COM BV
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2 as
7 published by the Free Software Foundation
9 Additionally, the license of this program contains a special
10 exception which allows to distribute the program in binary form when
11 it is linked against OpenSSL.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "dnsseckeeper.hh"
27 #include "dnspacket.hh"
28 #include "namespaces.hh"
30 uint32_t localtime_format_YYYYMMDDSS(time_t t
, uint32_t seq
)
35 (uint32_t)(tm
.tm_year
+1900) * 1000000u
36 + (uint32_t)(tm
.tm_mon
+ 1) * 10000u
37 + (uint32_t)tm
.tm_mday
* 100u
41 uint32_t calculateEditSOA(uint32_t old_serial
, const string
& kind
, const DNSName
& zonename
)
43 if(pdns_iequals(kind
,"INCEPTION-INCREMENT")) {
44 time_t inception
= getStartOfWeek();
45 uint32_t inception_serial
= localtime_format_YYYYMMDDSS(inception
, 1);
46 uint32_t dont_increment_after
= localtime_format_YYYYMMDDSS(inception
+ 2*86400, 99);
48 if(old_serial
< inception_serial
- 1) { /* less than <inceptionday>00 */
49 return inception_serial
; /* return <inceptionday>01 (skipping <inceptionday>00 as possible value) */
50 } else if (old_serial
< inception_serial
+1) {
51 /* "<inceptionday>00" and "<inceptionday>01" are reserved for inception increasing, so jump to "<inceptionday>02" */
52 return inception_serial
+1;
53 } else if(old_serial
<= dont_increment_after
) { /* >= <inceptionday>00 but <= <inceptionday+2>99 */
54 return old_serial
+ 1;
57 else if(pdns_iequals(kind
,"INCREMENT-WEEKS")) {
58 time_t inception
= getStartOfWeek();
59 return (old_serial
+ (inception
/ (7*86400)));
61 else if(pdns_iequals(kind
,"EPOCH")) {
64 else if(pdns_iequals(kind
,"INCEPTION-EPOCH")) {
65 uint32_t inception
= getStartOfWeek();
66 if (old_serial
< inception
)
69 else if(pdns_iequals(kind
,"NONE")) {
70 // do nothing to serial. needed because a metadata of "" will use the default-soa-edit setting instead.
72 else if(!kind
.empty()) {
73 g_log
<<Logger::Warning
<<"SOA-EDIT type '"<<kind
<<"' for zone "<<zonename
<<" is unknown."<<endl
;
75 // Seen strictly, this is a broken config: we can only come here if
76 // both SOA-EDIT and default-soa-edit are set to "", but the latter
77 // should be set to "NONE" instead.
81 uint32_t calculateEditSOA(uint32_t old_serial
, DNSSECKeeper
& dk
, const DNSName
& zonename
) {
83 dk
.getSoaEdit(zonename
, kind
);
84 return calculateEditSOA(old_serial
, kind
, zonename
);
87 /** Used for SOA-EDIT-DNSUPDATE and SOA-EDIT-API. */
88 static uint32_t calculateIncreaseSOA(uint32_t old_serial
, const string
& increaseKind
, const string
& editKind
, const DNSName
& zonename
) {
89 if (pdns_iequals(increaseKind
, "SOA-EDIT-INCREASE")) {
90 uint32_t new_serial
= old_serial
;
91 if (!editKind
.empty()) {
92 new_serial
= calculateEditSOA(old_serial
, editKind
, zonename
);
94 if (new_serial
<= old_serial
) {
95 new_serial
= old_serial
+ 1;
99 else if (pdns_iequals(increaseKind
, "SOA-EDIT")) {
100 return calculateEditSOA(old_serial
, editKind
, zonename
);
102 else if (pdns_iequals(increaseKind
, "INCREASE")) {
103 return old_serial
+ 1;
105 else if (pdns_iequals(increaseKind
, "EPOCH")) {
108 else if (pdns_iequals(increaseKind
, "DEFAULT")) {
109 time_t now
= time(0);
110 uint32_t new_serial
= localtime_format_YYYYMMDDSS(now
, 1);
111 if (new_serial
<= old_serial
) {
112 new_serial
= old_serial
+ 1;
115 } else if(!increaseKind
.empty()) {
116 g_log
<<Logger::Warning
<<"SOA-EDIT-API/DNSUPDATE type '"<<increaseKind
<<"' for zone "<<zonename
<<" is unknown."<<endl
;
121 /** Used for SOA-EDIT-DNSUPDATE and SOA-EDIT-API.
122 * Good if you already *have* a DNSResourceRecord.
123 * Content in rr is suitable for writing into a backend.
125 * @return true if changes may have been made
127 bool increaseSOARecord(DNSResourceRecord
& rr
, const string
& increaseKind
, const string
& editKind
) {
128 if (increaseKind
.empty())
132 fillSOAData(rr
.content
, sd
);
134 sd
.serial
= calculateIncreaseSOA(sd
.serial
, increaseKind
, editKind
, rr
.qname
);
135 rr
.content
= makeSOAContent(sd
)->getZoneRepresentation(true);
139 /** Used for SOA-EDIT-DNSUPDATE and SOA-EDIT-API.
140 * Makes a mostly reset DNSResourceRecord for you in @param rrout.
141 * Content in rrout is suitable for writing into a backend.
143 * @return true if rrout is now valid
145 bool makeIncreasedSOARecord(SOAData
& sd
, const string
& increaseKind
, const string
& editKind
, DNSResourceRecord
& rrout
) {
146 if (increaseKind
.empty())
149 sd
.serial
= calculateIncreaseSOA(sd
.serial
, increaseKind
, editKind
, sd
.qname
);
150 rrout
.qname
= sd
.qname
;
151 rrout
.content
= makeSOAContent(sd
)->getZoneRepresentation(true);
152 rrout
.qtype
= QType::SOA
;
153 rrout
.domain_id
= sd
.domain_id
;