| 1 | /* |
|---|
| 2 | PowerDNS Versatile Database Driven Nameserver |
|---|
| 3 | Copyright (C) 2002-2005 PowerDNS.COM BV |
|---|
| 4 | |
|---|
| 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; |
|---|
| 8 | |
|---|
| 9 | This program is distributed in the hope that it will be useful, |
|---|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 12 | GNU General Public License for more details. |
|---|
| 13 | |
|---|
| 14 | You should have received a copy of the GNU General Public License |
|---|
| 15 | along with this program; if not, write to the Free Software |
|---|
| 16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 17 | */ |
|---|
| 18 | |
|---|
| 19 | #include <errno.h> |
|---|
| 20 | #include <string> |
|---|
| 21 | #include <map> |
|---|
| 22 | #include <set> |
|---|
| 23 | #include <sys/types.h> |
|---|
| 24 | #include <sys/stat.h> |
|---|
| 25 | #include <unistd.h> |
|---|
| 26 | #include <fstream> |
|---|
| 27 | #include <sstream> |
|---|
| 28 | |
|---|
| 29 | using namespace std; |
|---|
| 30 | |
|---|
| 31 | #include "dns.hh" |
|---|
| 32 | #include "dnsbackend.hh" |
|---|
| 33 | #include "bindbackend2.hh" |
|---|
| 34 | #include "dnspacket.hh" |
|---|
| 35 | |
|---|
| 36 | #include "zoneparser.hh" |
|---|
| 37 | #include "bindparser.hh" |
|---|
| 38 | #include "logger.hh" |
|---|
| 39 | #include "arguments.hh" |
|---|
| 40 | #include "qtype.hh" |
|---|
| 41 | #include "misc.hh" |
|---|
| 42 | #include "dynlistener.hh" |
|---|
| 43 | #include "lock.hh" |
|---|
| 44 | using namespace std; |
|---|
| 45 | |
|---|
| 46 | /** new scheme of things: |
|---|
| 47 | we have zone-id map |
|---|
| 48 | a zone-id has a vector of DNSResourceRecords |
|---|
| 49 | on start of query, we find the best zone to answer from |
|---|
| 50 | */ |
|---|
| 51 | |
|---|
| 52 | map<string,int> Bind2Backend::s_name_id_map; |
|---|
| 53 | |
|---|
| 54 | // this map contains BB2DomainInfo structs, each of which contains a *pointer* to domain data |
|---|
| 55 | Bind2Backend::id_zone_map_t Bind2Backend::s_id_zone_map, Bind2Backend::s_staging_zone_map; |
|---|
| 56 | |
|---|
| 57 | /* the model is that everything works from the s_id_zone_map, which is left alone most of the time. |
|---|
| 58 | |
|---|
| 59 | Any updates which might fail, build a new zone map in s_staging_zone_map |
|---|
| 60 | Then we swap atomically, and clear the copy. |
|---|
| 61 | |
|---|
| 62 | Zone id's are kept constant so s_name_id_map doesn't need to be updated, except on removal or addition. |
|---|
| 63 | */ |
|---|
| 64 | |
|---|
| 65 | int Bind2Backend::s_first=1; |
|---|
| 66 | |
|---|
| 67 | pthread_mutex_t Bind2Backend::s_startup_lock=PTHREAD_MUTEX_INITIALIZER; |
|---|
| 68 | pthread_mutex_t Bind2Backend::s_zonemap_lock=PTHREAD_MUTEX_INITIALIZER; |
|---|
| 69 | |
|---|
| 70 | /* when a query comes in, we find the most appropriate zone and answer from that */ |
|---|
| 71 | |
|---|
| 72 | BB2DomainInfo::BB2DomainInfo() |
|---|
| 73 | { |
|---|
| 74 | d_loaded=false; |
|---|
| 75 | d_last_check=0; |
|---|
| 76 | d_checknow=false; |
|---|
| 77 | d_status="Unknown"; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | void BB2DomainInfo::setCheckInterval(time_t seconds) |
|---|
| 81 | { |
|---|
| 82 | d_checkinterval=seconds; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | bool BB2DomainInfo::current() |
|---|
| 86 | { |
|---|
| 87 | if(d_checknow) |
|---|
| 88 | return false; |
|---|
| 89 | |
|---|
| 90 | if(!d_checknow && !d_checkinterval || (time(0)-d_lastcheck<d_checkinterval) || d_filename.empty()) |
|---|
| 91 | return true; |
|---|
| 92 | |
|---|
| 93 | return (getCtime()==d_ctime); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | time_t BB2DomainInfo::getCtime() |
|---|
| 97 | { |
|---|
| 98 | struct stat buf; |
|---|
| 99 | |
|---|
| 100 | if(d_filename.empty() || stat(d_filename.c_str(),&buf)<0) |
|---|
| 101 | return 0; |
|---|
| 102 | d_lastcheck=time(0); |
|---|
| 103 | return buf.st_ctime; |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | void BB2DomainInfo::setCtime() |
|---|
| 107 | { |
|---|
| 108 | struct stat buf; |
|---|
| 109 | if(stat(d_filename.c_str(),&buf)<0) |
|---|
| 110 | return; |
|---|
| 111 | d_ctime=buf.st_ctime; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | void Bind2Backend::setNotified(uint32_t id, uint32_t serial) |
|---|
| 115 | { |
|---|
| 116 | s_id_zone_map[id].d_lastnotified=serial; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | void Bind2Backend::setFresh(uint32_t domain_id) |
|---|
| 120 | { |
|---|
| 121 | s_id_zone_map[domain_id].d_last_check=time(0); |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | bool Bind2Backend::startTransaction(const string &qname, int id) |
|---|
| 125 | { |
|---|
| 126 | BB2DomainInfo &bbd=s_id_zone_map[d_transaction_id=id]; |
|---|
| 127 | d_transaction_tmpname=bbd.d_filename+"."+itoa(random()); |
|---|
| 128 | d_of=new ofstream(d_transaction_tmpname.c_str()); |
|---|
| 129 | if(!*d_of) { |
|---|
| 130 | throw DBException("Unable to open temporary zonefile '"+d_transaction_tmpname+"': "+stringerror()); |
|---|
| 131 | unlink(d_transaction_tmpname.c_str()); |
|---|
| 132 | delete d_of; |
|---|
| 133 | d_of=0; |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | *d_of<<"; Written by PowerDNS, don't edit!"<<endl; |
|---|
| 137 | *d_of<<"; Zone '"+bbd.d_name+"' retrieved from master "<<bbd.d_master<<endl<<"; at "<<nowTime()<<endl; |
|---|
| 138 | |
|---|
| 139 | return true; |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | bool Bind2Backend::commitTransaction() |
|---|
| 143 | { |
|---|
| 144 | delete d_of; |
|---|
| 145 | d_of=0; |
|---|
| 146 | if(rename(d_transaction_tmpname.c_str(),s_id_zone_map[d_transaction_id].d_filename.c_str())<0) |
|---|
| 147 | throw DBException("Unable to commit (rename to: '"+s_id_zone_map[d_transaction_id].d_filename+"') AXFRed zone: "+stringerror()); |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | queueReload(&s_id_zone_map[d_transaction_id]); |
|---|
| 151 | |
|---|
| 152 | d_transaction_id=0; |
|---|
| 153 | |
|---|
| 154 | return true; |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | bool Bind2Backend::abortTransaction() |
|---|
| 158 | { |
|---|
| 159 | if(d_transaction_id) { |
|---|
| 160 | delete d_of; |
|---|
| 161 | d_of=0; |
|---|
| 162 | unlink(d_transaction_tmpname.c_str()); |
|---|
| 163 | d_transaction_id=0; |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | return true; |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | bool Bind2Backend::feedRecord(const DNSResourceRecord &r) |
|---|
| 170 | { |
|---|
| 171 | string qname=r.qname; |
|---|
| 172 | string domain=s_id_zone_map[d_transaction_id].d_name; |
|---|
| 173 | |
|---|
| 174 | if(!stripDomainSuffix(&qname,domain)) |
|---|
| 175 | throw DBException("out-of-zone data '"+qname+"' during AXFR of zone '"+domain+"'"); |
|---|
| 176 | |
|---|
| 177 | string content=r.content; |
|---|
| 178 | |
|---|
| 179 | // SOA needs stripping too! XXX FIXME - also, this should not be here I think |
|---|
| 180 | switch(r.qtype.getCode()) { |
|---|
| 181 | case QType::TXT: |
|---|
| 182 | *d_of<<qname<<"\t"<<r.ttl<<"\t"<<r.qtype.getName()<<"\t\""<<r.content<<"\""<<endl; |
|---|
| 183 | break; |
|---|
| 184 | case QType::MX: |
|---|
| 185 | if(!stripDomainSuffix(&content,domain)) |
|---|
| 186 | content+="."; |
|---|
| 187 | *d_of<<qname<<"\t"<<r.ttl<<"\t"<<r.qtype.getName()<<"\t"<<r.priority<<"\t"<<content<<endl; |
|---|
| 188 | break; |
|---|
| 189 | case QType::CNAME: |
|---|
| 190 | case QType::NS: |
|---|
| 191 | if(!stripDomainSuffix(&content,domain)) |
|---|
| 192 | content+="."; |
|---|
| 193 | *d_of<<qname<<"\t"<<r.ttl<<"\t"<<r.qtype.getName()<<"\t"<<content<<endl; |
|---|
| 194 | break; |
|---|
| 195 | default: |
|---|
| 196 | *d_of<<qname<<"\t"<<r.ttl<<"\t"<<r.qtype.getName()<<"\t"<<r.content<<endl; |
|---|
| 197 | break; |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | return true; |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | void Bind2Backend::getUpdatedMasters(vector<DomainInfo> *changedDomains) |
|---|
| 204 | { |
|---|
| 205 | SOAData soadata; |
|---|
| 206 | for(id_zone_map_t::iterator i=s_id_zone_map.begin();i!=s_id_zone_map.end();++i) { |
|---|
| 207 | if(!i->second.d_master.empty()) |
|---|
| 208 | continue; |
|---|
| 209 | soadata.serial=0; |
|---|
| 210 | try { |
|---|
| 211 | getSOA(i->second.d_name,soadata); // we might not *have* a SOA yet |
|---|
| 212 | } |
|---|
| 213 | catch(...){} |
|---|
| 214 | DomainInfo di; |
|---|
| 215 | di.id=i->first; |
|---|
| 216 | di.serial=soadata.serial; |
|---|
| 217 | di.zone=i->second.d_name; |
|---|
| 218 | di.last_check=i->second.d_last_check; |
|---|
| 219 | di.backend=this; |
|---|
| 220 | di.kind=DomainInfo::Master; |
|---|
| 221 | if(!i->second.d_lastnotified) // don't do notification storm on startup |
|---|
| 222 | i->second.d_lastnotified=soadata.serial; |
|---|
| 223 | else |
|---|
| 224 | if(soadata.serial!=i->second.d_lastnotified) |
|---|
| 225 | changedDomains->push_back(di); |
|---|
| 226 | } |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | void Bind2Backend::getUnfreshSlaveInfos(vector<DomainInfo> *unfreshDomains) |
|---|
| 230 | { |
|---|
| 231 | for(id_zone_map_t::const_iterator i=s_id_zone_map.begin();i!=s_id_zone_map.end();++i) { |
|---|
| 232 | if(i->second.d_master.empty()) |
|---|
| 233 | continue; |
|---|
| 234 | DomainInfo sd; |
|---|
| 235 | sd.id=i->first; |
|---|
| 236 | sd.zone=i->second.d_name; |
|---|
| 237 | sd.master=i->second.d_master; |
|---|
| 238 | sd.last_check=i->second.d_last_check; |
|---|
| 239 | sd.backend=this; |
|---|
| 240 | sd.kind=DomainInfo::Slave; |
|---|
| 241 | SOAData soadata; |
|---|
| 242 | soadata.serial=0; |
|---|
| 243 | soadata.refresh=0; |
|---|
| 244 | soadata.serial=0; |
|---|
| 245 | soadata.db=(DNSBackend *)-1; // not sure if this is useful, inhibits any caches that might be around |
|---|
| 246 | try { |
|---|
| 247 | getSOA(i->second.d_name,soadata); // we might not *have* a SOA yet |
|---|
| 248 | } |
|---|
| 249 | catch(...){} |
|---|
| 250 | sd.serial=soadata.serial; |
|---|
| 251 | if(sd.last_check+soadata.refresh<(unsigned int)time(0)) |
|---|
| 252 | unfreshDomains->push_back(sd); |
|---|
| 253 | } |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| 256 | bool Bind2Backend::getDomainInfo(const string &domain, DomainInfo &di) |
|---|
| 257 | { |
|---|
| 258 | for(id_zone_map_t::const_iterator i=s_id_zone_map.begin();i!=s_id_zone_map.end();++i) { |
|---|
| 259 | if(i->second.d_name==domain) { |
|---|
| 260 | di.id=i->first; |
|---|
| 261 | di.zone=domain; |
|---|
| 262 | di.master=i->second.d_master; |
|---|
| 263 | di.last_check=i->second.d_last_check; |
|---|
| 264 | di.backend=this; |
|---|
| 265 | di.kind=i->second.d_master.empty() ? DomainInfo::Master : DomainInfo::Slave; |
|---|
| 266 | di.serial=0; |
|---|
| 267 | try { |
|---|
| 268 | SOAData sd; |
|---|
| 269 | sd.serial=0; |
|---|
| 270 | |
|---|
| 271 | getSOA(i->second.d_name,sd); // we might not *have* a SOA yet |
|---|
| 272 | di.serial=sd.serial; |
|---|
| 273 | } |
|---|
| 274 | catch(...){} |
|---|
| 275 | |
|---|
| 276 | return true; |
|---|
| 277 | } |
|---|
| 278 | } |
|---|
| 279 | return false; |
|---|
| 280 | } |
|---|
| 281 | |
|---|
| 282 | |
|---|
| 283 | //! lowercase, strip trailing . |
|---|
| 284 | static string canonic(string ret) |
|---|
| 285 | { |
|---|
| 286 | string::iterator i; |
|---|
| 287 | |
|---|
| 288 | for(i=ret.begin(); |
|---|
| 289 | i!=ret.end(); |
|---|
| 290 | ++i) |
|---|
| 291 | *i=tolower(*i); |
|---|
| 292 | |
|---|
| 293 | |
|---|
| 294 | if(*(i-1)=='.') |
|---|
| 295 | ret.resize(i-ret.begin()-1); |
|---|
| 296 | return ret; |
|---|
| 297 | } |
|---|
| 298 | |
|---|
| 299 | static Bind2Backend *us; |
|---|
| 300 | |
|---|
| 301 | static void InsertionCallback(unsigned int domain_id, const string &domain, const string &qtype, const string &content, int ttl, int prio) |
|---|
| 302 | { |
|---|
| 303 | us->insert(domain_id, domain, qtype, content, ttl, prio); |
|---|
| 304 | } |
|---|
| 305 | |
|---|
| 306 | set<string> contents; |
|---|
| 307 | |
|---|
| 308 | /** This function adds a record to a domain with a certain id. |
|---|
| 309 | Much of the complication is due to the efforts to benefit from std::string reference counting copy on write semantics */ |
|---|
| 310 | void Bind2Backend::insert(int id, const string &qnameu, const string &qtype, const string &content, int ttl=300, int prio=25) |
|---|
| 311 | { |
|---|
| 312 | Bind2DNSRecord bdr; |
|---|
| 313 | |
|---|
| 314 | BB2DomainInfo& bb2=s_staging_zone_map[id]; |
|---|
| 315 | |
|---|
| 316 | vector<Bind2DNSRecord>& records=*bb2.d_records; |
|---|
| 317 | |
|---|
| 318 | bdr.qname=toLower(canonic(qnameu)); |
|---|
| 319 | if(bdr.qname==bb2.d_name) |
|---|
| 320 | bdr.qname.clear(); |
|---|
| 321 | else if(bdr.qname.length() > bb2.d_name.length()) |
|---|
| 322 | bdr.qname.resize(bdr.qname.length() - (bb2.d_name.length() + 1)); |
|---|
| 323 | else |
|---|
| 324 | throw AhuException("Trying to insert non-zone data, name='"+bdr.qname+"', zone='"+s_staging_zone_map[id].d_name+"'"); |
|---|
| 325 | |
|---|
| 326 | bdr.qname.swap(bdr.qname); |
|---|
| 327 | |
|---|
| 328 | if(!records.empty() && bdr.qname==(records.end()-1)->qname) |
|---|
| 329 | bdr.qname=(records.end()-1)->qname; |
|---|
| 330 | |
|---|
| 331 | bdr.qtype=QType(qtype.c_str()).getCode(); |
|---|
| 332 | bdr.content=canonic(content); // I think this is wrong, the zoneparser should not come up with . terminated stuff XXX FIXME |
|---|
| 333 | set<string>::const_iterator i=contents.find(bdr.content); |
|---|
| 334 | if(i!=contents.end()) |
|---|
| 335 | bdr.content=*i; |
|---|
| 336 | else { |
|---|
| 337 | contents.insert(bdr.content); |
|---|
| 338 | } |
|---|
| 339 | |
|---|
| 340 | bdr.ttl=ttl; |
|---|
| 341 | bdr.priority=prio; |
|---|
| 342 | |
|---|
| 343 | records.push_back(bdr); |
|---|
| 344 | } |
|---|
| 345 | |
|---|
| 346 | |
|---|
| 347 | |
|---|
| 348 | void Bind2Backend::reload() |
|---|
| 349 | { |
|---|
| 350 | for(id_zone_map_t::iterator i=us->s_id_zone_map.begin();i!=us->s_id_zone_map.end();++i) |
|---|
| 351 | i->second.d_checknow=true; |
|---|
| 352 | } |
|---|
| 353 | |
|---|
| 354 | string Bind2Backend::DLReloadNowHandler(const vector<string>&parts, Utility::pid_t ppid) |
|---|
| 355 | { |
|---|
| 356 | ostringstream ret; |
|---|
| 357 | |
|---|
| 358 | for(vector<string>::const_iterator i=parts.begin()+1;i<parts.end();++i) { |
|---|
| 359 | if(s_name_id_map.count(*i)) { |
|---|
| 360 | BB2DomainInfo& bbd=s_id_zone_map[s_name_id_map[*i]]; |
|---|
| 361 | |
|---|
| 362 | us->queueReload(&bbd); |
|---|
| 363 | ret<< *i << ": "<< (bbd.d_loaded ? "": "[rejected]") <<"\t"<<bbd.d_status<<"\n"; |
|---|
| 364 | } |
|---|
| 365 | else |
|---|
| 366 | ret<< *i << " no such domain\n"; |
|---|
| 367 | } |
|---|
| 368 | if(ret.str().empty()) |
|---|
| 369 | ret<<"no domains reloaded"; |
|---|
| 370 | return ret.str(); |
|---|
| 371 | } |
|---|
| 372 | |
|---|
| 373 | |
|---|
| 374 | string Bind2Backend::DLDomStatusHandler(const vector<string>&parts, Utility::pid_t ppid) |
|---|
| 375 | { |
|---|
| 376 | ostringstream ret; |
|---|
| 377 | if(parts.size() > 1) { |
|---|
| 378 | for(vector<string>::const_iterator i=parts.begin()+1;i<parts.end();++i) { |
|---|
| 379 | if(s_name_id_map.count(*i)) { |
|---|
| 380 | BB2DomainInfo& bbd=s_id_zone_map[s_name_id_map[*i]]; |
|---|
| 381 | ret<< *i << ": "<< (bbd.d_loaded ? "": "[rejected]") <<"\t"<<bbd.d_status<<"\n"; |
|---|
| 382 | } |
|---|
| 383 | else |
|---|
| 384 | ret<< *i << " no such domain\n"; |
|---|
| 385 | } |
|---|
| 386 | } |
|---|
| 387 | else |
|---|
| 388 | for(id_zone_map_t::iterator i=us->s_id_zone_map.begin(); i!=us->s_id_zone_map.end(); ++i) |
|---|
| 389 | ret<< i->second.d_name << ": "<< (i->second.d_loaded ? "": "[rejected]") <<"\t"<<i->second.d_status<<"\n"; |
|---|
| 390 | |
|---|
| 391 | if(ret.str().empty()) |
|---|
| 392 | ret<<"no domains passed"; |
|---|
| 393 | |
|---|
| 394 | return ret.str(); |
|---|
| 395 | } |
|---|
| 396 | |
|---|
| 397 | |
|---|
| 398 | string Bind2Backend::DLListRejectsHandler(const vector<string>&parts, Utility::pid_t ppid) |
|---|
| 399 | { |
|---|
| 400 | ostringstream ret; |
|---|
| 401 | for(id_zone_map_t::iterator j=us->s_id_zone_map.begin();j!=us->s_id_zone_map.end();++j) |
|---|
| 402 | if(!j->second.d_loaded) |
|---|
| 403 | ret<<j->second.d_name<<"\t"<<j->second.d_status<<endl; |
|---|
| 404 | |
|---|
| 405 | return ret.str(); |
|---|
| 406 | } |
|---|
| 407 | |
|---|
| 408 | |
|---|
| 409 | Bind2Backend::Bind2Backend(const string &suffix) |
|---|
| 410 | { |
|---|
| 411 | #if __GNUC__ >= 3 |
|---|
| 412 | ios_base::sync_with_stdio(false); |
|---|
| 413 | #endif |
|---|
| 414 | d_logprefix="[bind2"+suffix+"backend]"; |
|---|
| 415 | setArgPrefix("bind2"+suffix); |
|---|
| 416 | Lock l(&s_startup_lock); |
|---|
| 417 | |
|---|
| 418 | d_transaction_id=0; |
|---|
| 419 | if(!s_first) { |
|---|
| 420 | return; |
|---|
| 421 | } |
|---|
| 422 | |
|---|
| 423 | s_first=0; |
|---|
| 424 | |
|---|
| 425 | loadConfig(); |
|---|
| 426 | |
|---|
| 427 | extern DynListener *dl; |
|---|
| 428 | us=this; |
|---|
| 429 | dl->registerFunc("BIND2-RELOAD-NOW", &DLReloadNowHandler); |
|---|
| 430 | dl->registerFunc("BIND2-DOMAIN-STATUS", &DLDomStatusHandler); |
|---|
| 431 | dl->registerFunc("BIND2-LIST-REJECTS", &DLListRejectsHandler); |
|---|
| 432 | } |
|---|
| 433 | |
|---|
| 434 | void Bind2Backend::rediscover(string *status) |
|---|
| 435 | { |
|---|
| 436 | loadConfig(status); |
|---|
| 437 | } |
|---|
| 438 | |
|---|
| 439 | void Bind2Backend::loadConfig(string* status) |
|---|
| 440 | { |
|---|
| 441 | // Interference with createSlaveDomain() |
|---|
| 442 | Lock l(&s_zonemap_lock); |
|---|
| 443 | |
|---|
| 444 | static int domain_id=1; |
|---|
| 445 | s_staging_zone_map.clear(); |
|---|
| 446 | if(!getArg("config").empty()) { |
|---|
| 447 | BindParser BP; |
|---|
| 448 | try { |
|---|
| 449 | BP.parse(getArg("config")); |
|---|
| 450 | } |
|---|
| 451 | catch(AhuException &ae) { |
|---|
| 452 | L<<Logger::Error<<"Error parsing bind configuration: "<<ae.reason<<endl; |
|---|
| 453 | throw; |
|---|
| 454 | } |
|---|
| 455 | |
|---|
| 456 | ZoneParser ZP; |
|---|
| 457 | |
|---|
| 458 | vector<BindDomainInfo> domains=BP.getDomains(); |
|---|
| 459 | |
|---|
| 460 | us=this; |
|---|
| 461 | |
|---|
| 462 | d_binddirectory=BP.getDirectory(); |
|---|
| 463 | ZP.setDirectory(d_binddirectory); |
|---|
| 464 | ZP.setCallback(&InsertionCallback); |
|---|
| 465 | |
|---|
| 466 | L<<Logger::Warning<<d_logprefix<<" Parsing "<<domains.size()<<" domain(s), will report when done"<<endl; |
|---|
| 467 | |
|---|
| 468 | int rejected=0; |
|---|
| 469 | int newdomains=0; |
|---|
| 470 | |
|---|
| 471 | for(vector<BindDomainInfo>::const_iterator i=domains.begin(); |
|---|
| 472 | i!=domains.end(); |
|---|
| 473 | ++i) |
|---|
| 474 | { |
|---|
| 475 | if(i->type!="master" && i->type!="slave") { |
|---|
| 476 | L<<Logger::Warning<<d_logprefix<<" Warning! Skipping '"<<i->type<<"' zone '"<<i->name<<"'"<<endl; |
|---|
| 477 | continue; |
|---|
| 478 | } |
|---|
| 479 | |
|---|
| 480 | BB2DomainInfo* bbd=0; |
|---|
| 481 | |
|---|
| 482 | if(!s_name_id_map.count(i->name)) { // is it fully new? |
|---|
| 483 | bbd=&s_staging_zone_map[domain_id]; |
|---|
| 484 | bbd->d_id=domain_id++; |
|---|
| 485 | s_name_id_map[i->name]=bbd->d_id; |
|---|
| 486 | |
|---|
| 487 | // this isn't necessary, we do this on the actual load |
|---|
| 488 | // bbd->d_records=shared_ptr<vector<Bind2DNSRecord> > (new vector<Bind2DNSRecord>); |
|---|
| 489 | |
|---|
| 490 | bbd->setCheckInterval(getArgAsNum("check-interval")); |
|---|
| 491 | bbd->d_lastnotified=0; |
|---|
| 492 | bbd->d_loaded=false; |
|---|
| 493 | } |
|---|
| 494 | else { // no, we knew about it already |
|---|
| 495 | s_staging_zone_map[s_name_id_map[i->name]]=s_id_zone_map[s_name_id_map[i->name]]; |
|---|
| 496 | bbd=&s_staging_zone_map[s_name_id_map[i->name]]; |
|---|
| 497 | } |
|---|
| 498 | |
|---|
| 499 | |
|---|
| 500 | // overwrite what we knew about the domain |
|---|
| 501 | bbd->d_name=i->name; |
|---|
| 502 | bbd->d_filename=i->filename; |
|---|
| 503 | bbd->d_master=i->master; |
|---|
| 504 | |
|---|
| 505 | if(!bbd->d_loaded || !bbd->current()) { |
|---|
| 506 | L<<Logger::Info<<d_logprefix<<" parsing '"<<i->name<<"' from file '"<<i->filename<<"'"<<endl; |
|---|
| 507 | |
|---|
| 508 | try { |
|---|
| 509 | // we need to allocate a new vector so we don't kill the original |
|---|
| 510 | bbd->d_records=shared_ptr<vector<Bind2DNSRecord> > (new vector<Bind2DNSRecord>); |
|---|
| 511 | |
|---|
| 512 | ZP.parse(i->filename, i->name, bbd->d_id); // calls callback for us |
|---|
| 513 | L<<Logger::Info<<d_logprefix<<" sorting '"<<i->name<<"'"<<endl; |
|---|
| 514 | |
|---|
| 515 | sort(s_staging_zone_map[bbd->d_id].d_records->begin(), s_staging_zone_map[bbd->d_id].d_records->end()); |
|---|
| 516 | |
|---|
| 517 | s_staging_zone_map[bbd->d_id].setCtime(); |
|---|
| 518 | s_staging_zone_map[bbd->d_id].d_loaded=true; |
|---|
| 519 | s_staging_zone_map[bbd->d_id].d_status="parsed into memory at "+nowTime(); |
|---|
| 520 | |
|---|
| 521 | contents.clear(); |
|---|
| 522 | // s_staging_zone_map[bbd->d_id].d_records->swap(*s_staging_zone_map[bbd->d_id].d_records); |
|---|
| 523 | } |
|---|
| 524 | catch(AhuException &ae) { |
|---|
| 525 | ostringstream msg; |
|---|
| 526 | msg<<" error at "+nowTime()+" parsing '"<<i->name<<"' from file '"<<i->filename<<"': "<<ae.reason; |
|---|
| 527 | |
|---|
| 528 | if(status) |
|---|
| 529 | *status+=msg.str(); |
|---|
| 530 | s_staging_zone_map[bbd->d_id].d_status=msg.str(); |
|---|
| 531 | L<<Logger::Warning<<d_logprefix<<msg.str()<<endl; |
|---|
| 532 | rejected++; |
|---|
| 533 | } |
|---|
| 534 | } |
|---|
| 535 | /* |
|---|
| 536 | vector<vector<BBResourceRecord> *>&tmp=d_zone_id_map[bbd.d_id]; // shrink trick |
|---|
| 537 | vector<vector<BBResourceRecord> *>(tmp).swap(tmp); |
|---|
| 538 | */ |
|---|
| 539 | } |
|---|
| 540 | |
|---|
| 541 | // figure out which domains were new and which vanished |
|---|
| 542 | int remdomains=0; |
|---|
| 543 | set<string> oldnames, newnames; |
|---|
| 544 | for(id_zone_map_t::const_iterator j=s_id_zone_map.begin();j!=s_id_zone_map.end();++j) { |
|---|
| 545 | oldnames.insert(j->second.d_name); |
|---|
| 546 | } |
|---|
| 547 | for(id_zone_map_t::const_iterator j=s_staging_zone_map.begin();j!=s_staging_zone_map.end();++j) { |
|---|
| 548 | newnames.insert(j->second.d_name); |
|---|
| 549 | } |
|---|
| 550 | |
|---|
| 551 | vector<string> diff; |
|---|
| 552 | set_difference(oldnames.begin(), oldnames.end(), newnames.begin(), newnames.end(), back_inserter(diff)); |
|---|
| 553 | remdomains=diff.size(); |
|---|
| 554 | |
|---|
| 555 | // remove domains from the *name* map, delete their pointer |
|---|
| 556 | for(vector<string>::const_iterator k=diff.begin();k!=diff.end(); ++k) { |
|---|
| 557 | L<<Logger::Error<<"Removing domain: "<<*k<<endl; |
|---|
| 558 | s_name_id_map.erase(*k); |
|---|
| 559 | } |
|---|
| 560 | |
|---|
| 561 | // now remove from the s_id_zone_map |
|---|
| 562 | for(id_zone_map_t::iterator j=s_id_zone_map.begin();j!=s_id_zone_map.end();++j) { // O(N*M) |
|---|
| 563 | for(vector<string>::const_iterator k=diff.begin();k!=diff.end();++k) |
|---|
| 564 | if(j->second.d_name==*k) { |
|---|
| 565 | L<<Logger::Error<<"Removing records from zone '"<<j->second.d_name<<"' from memory"<<endl; |
|---|
| 566 | |
|---|
| 567 | j->second.d_loaded=false; |
|---|
| 568 | nukeZoneRecords(&j->second); |
|---|
| 569 | |
|---|
| 570 | break; |
|---|
| 571 | } |
|---|
| 572 | } |
|---|
| 573 | |
|---|
| 574 | // count number of entirely new domains |
|---|
| 575 | vector<string> diff2; |
|---|
| 576 | set_difference(newnames.begin(), newnames.end(), oldnames.begin(), oldnames.end(), back_inserter(diff2)); |
|---|
| 577 | newdomains=diff2.size(); |
|---|
| 578 | |
|---|
| 579 | s_id_zone_map.swap(s_staging_zone_map); // commit |
|---|
| 580 | s_staging_zone_map.clear(); // and cleanup |
|---|
| 581 | |
|---|
| 582 | // report |
|---|
| 583 | ostringstream msg; |
|---|
| 584 | msg<<" Done parsing domains, "<<rejected<<" rejected, "<<newdomains<<" new, "<<remdomains<<" removed"; |
|---|
| 585 | if(status) |
|---|
| 586 | *status=msg.str(); |
|---|
| 587 | |
|---|
| 588 | L<<Logger::Error<<d_logprefix<<msg.str()<<endl; |
|---|
| 589 | } |
|---|
| 590 | } |
|---|
| 591 | |
|---|
| 592 | /** nuke all records from memory, keep bbd intact though. */ |
|---|
| 593 | void Bind2Backend::nukeZoneRecords(BB2DomainInfo *bbd) |
|---|
| 594 | { |
|---|
| 595 | bbd->d_loaded=0; // block further access |
|---|
| 596 | bbd->d_records->clear(); // empty the vector of Bind2DNSRecords |
|---|
| 597 | } |
|---|
| 598 | |
|---|
| 599 | |
|---|
| 600 | void Bind2Backend::queueReload(BB2DomainInfo *bbd) |
|---|
| 601 | { |
|---|
| 602 | s_staging_zone_map.clear(); |
|---|
| 603 | |
|---|
| 604 | // we reload *now* for the time being |
|---|
| 605 | |
|---|
| 606 | try { |
|---|
| 607 | nukeZoneRecords(bbd); |
|---|
| 608 | |
|---|
| 609 | ZoneParser ZP; |
|---|
| 610 | us=this; |
|---|
| 611 | |
|---|
| 612 | ZP.setDirectory(d_binddirectory); |
|---|
| 613 | ZP.setCallback(&InsertionCallback); |
|---|
| 614 | |
|---|
| 615 | // XXX FIXME - I think this is highly bogus as we are copying pointers around here, so we are only creating aliases |
|---|
| 616 | |
|---|
| 617 | s_staging_zone_map[bbd->d_id]=s_id_zone_map[bbd->d_id]; |
|---|
| 618 | s_staging_zone_map[bbd->d_id].d_records=shared_ptr<vector<Bind2DNSRecord> > (new vector<Bind2DNSRecord>); // nuke it |
|---|
| 619 | |
|---|
| 620 | ZP.parse(bbd->d_filename, bbd->d_name, bbd->d_id); |
|---|
| 621 | |
|---|
| 622 | sort(s_staging_zone_map[bbd->d_id].d_records->begin(), s_staging_zone_map[bbd->d_id].d_records->end()); |
|---|
| 623 | s_staging_zone_map[bbd->d_id].setCtime(); |
|---|
| 624 | |
|---|
| 625 | contents.clear(); |
|---|
| 626 | s_id_zone_map[bbd->d_id]=s_staging_zone_map[bbd->d_id]; |
|---|
| 627 | |
|---|
| 628 | bbd->setCtime(); |
|---|
| 629 | // and raise d_loaded again! |
|---|
| 630 | bbd->d_loaded=1; |
|---|
| 631 | bbd->d_checknow=0; |
|---|
| 632 | bbd->d_status="parsed into memory at "+nowTime(); |
|---|
| 633 | L<<Logger::Warning<<"Zone '"<<bbd->d_name<<"' ("<<bbd->d_filename<<") reloaded"<<endl; |
|---|
| 634 | } |
|---|
| 635 | catch(AhuException &ae) { |
|---|
| 636 | ostringstream msg; |
|---|
| 637 | msg<<" error at "+nowTime()+" parsing '"<<bbd->d_name<<"' from file '"<<bbd->d_filename<<"': "<<ae.reason; |
|---|
| 638 | bbd->d_status=msg.str(); |
|---|
| 639 | } |
|---|
| 640 | } |
|---|
| 641 | |
|---|
| 642 | bool operator<(const Bind2DNSRecord &a, const string &b) |
|---|
| 643 | { |
|---|
| 644 | return a.qname < b; |
|---|
| 645 | } |
|---|
| 646 | |
|---|
| 647 | bool operator<(const string &a, const Bind2DNSRecord &b) |
|---|
| 648 | { |
|---|
| 649 | return a < b.qname; |
|---|
| 650 | } |
|---|
| 651 | |
|---|
| 652 | |
|---|
| 653 | void Bind2Backend::lookup(const QType &qtype, const string &qname, DNSPacket *pkt_p, int zoneId ) |
|---|
| 654 | { |
|---|
| 655 | d_handle.reset(); |
|---|
| 656 | |
|---|
| 657 | string domain=toLower(qname); |
|---|
| 658 | |
|---|
| 659 | if(arg().mustDo("query-logging")) |
|---|
| 660 | L<<"Lookup for '"<<qtype.getName()<<"' of '"<<domain<<"'"<<endl; |
|---|
| 661 | |
|---|
| 662 | while(!s_name_id_map.count(domain) && chopOff(domain)); |
|---|
| 663 | |
|---|
| 664 | if(!s_name_id_map.count(domain)) { |
|---|
| 665 | d_handle.d_list=false; |
|---|
| 666 | return; |
|---|
| 667 | } |
|---|
| 668 | unsigned int id=s_name_id_map[domain]; |
|---|
| 669 | |
|---|
| 670 | d_handle.id=id; |
|---|
| 671 | |
|---|
| 672 | DLOG(L<<"Bind2Backend constructing handle for search for "<<qtype.getName()<<" for "<< |
|---|
| 673 | qname<<endl); |
|---|
| 674 | |
|---|
| 675 | if(strcasecmp(qname.c_str(),domain.c_str())) |
|---|
| 676 | d_handle.qname=toLower(qname.substr(0,qname.size()-domain.length()-1)); // strip domain name |
|---|
| 677 | |
|---|
| 678 | d_handle.parent=this; |
|---|
| 679 | d_handle.qtype=qtype; |
|---|
| 680 | d_handle.domain=domain; |
|---|
| 681 | d_handle.d_records=s_id_zone_map[id].d_records; // give it a copy |
|---|
| 682 | if(!d_handle.d_records->empty()) { |
|---|
| 683 | BB2DomainInfo& bbd=s_id_zone_map[id]; |
|---|
| 684 | if(!bbd.d_loaded) { |
|---|
| 685 | d_handle.reset(); |
|---|
| 686 | throw DBException("Zone temporarily not available (file missing, or master dead)"); // fsck |
|---|
| 687 | } |
|---|
| 688 | |
|---|
| 689 | |
|---|
| 690 | if(!bbd.current()) { |
|---|
| 691 | L<<Logger::Warning<<"Zone '"<<bbd.d_name<<"' ("<<bbd.d_filename<<") needs reloading"<<endl; |
|---|
| 692 | queueReload(&bbd); |
|---|
| 693 | } |
|---|
| 694 | } |
|---|
| 695 | else { |
|---|
| 696 | DLOG(L<<"Query with no results"<<endl); |
|---|
| 697 | } |
|---|
| 698 | |
|---|
| 699 | pair<vector<Bind2DNSRecord>::const_iterator, vector<Bind2DNSRecord>::const_iterator> range; |
|---|
| 700 | |
|---|
| 701 | // cout<<"starting equal range for: '"<<d_handle.qname<<"'"<<endl; |
|---|
| 702 | range=equal_range(d_handle.d_records->begin(), d_handle.d_records->end(), d_handle.qname); |
|---|
| 703 | |
|---|
| 704 | if(range.first==range.second) { |
|---|
| 705 | d_handle.d_list=false; |
|---|
| 706 | return; |
|---|
| 707 | } |
|---|
| 708 | else { |
|---|
| 709 | d_handle.d_iter=range.first; |
|---|
| 710 | d_handle.d_end_iter=range.second; |
|---|
| 711 | } |
|---|
| 712 | |
|---|
| 713 | d_handle.d_list=false; |
|---|
| 714 | } |
|---|
| 715 | |
|---|
| 716 | Bind2Backend::handle::handle() |
|---|
| 717 | { |
|---|
| 718 | // d_records=0; |
|---|
| 719 | count=0; |
|---|
| 720 | } |
|---|
| 721 | |
|---|
| 722 | bool Bind2Backend::get(DNSResourceRecord &r) |
|---|
| 723 | { |
|---|
| 724 | if(!d_handle.d_records) |
|---|
| 725 | return false; |
|---|
| 726 | |
|---|
| 727 | if(!d_handle.get(r)) { |
|---|
| 728 | d_handle.reset(); |
|---|
| 729 | |
|---|
| 730 | if(arg().mustDo("query-logging")) |
|---|
| 731 | L<<"End of answers"<<endl; |
|---|
| 732 | |
|---|
| 733 | return false; |
|---|
| 734 | } |
|---|
| 735 | if(arg().mustDo("query-logging")) |
|---|
| 736 | L<<"Returning: '"<<r.qtype.getName()<<"' of '"<<r.qname<<"', content: '"<<r.content<<"'"<<endl; |
|---|
| 737 | return true; |
|---|
| 738 | } |
|---|
| 739 | |
|---|
| 740 | bool Bind2Backend::handle::get(DNSResourceRecord &r) |
|---|
| 741 | { |
|---|
| 742 | if(d_list) |
|---|
| 743 | return get_list(r); |
|---|
| 744 | else |
|---|
| 745 | return get_normal(r); |
|---|
| 746 | } |
|---|
| 747 | |
|---|
| 748 | bool Bind2Backend::handle::get_normal(DNSResourceRecord &r) |
|---|
| 749 | { |
|---|
| 750 | DLOG(L << "Bind2Backend get() was called for "<<qtype.getName() << " record for "<< |
|---|
| 751 | qname<<"- "<<d_records->size()<<" available!"<<endl); |
|---|
| 752 | |
|---|
| 753 | if(d_iter==d_end_iter) { |
|---|
| 754 | |
|---|
| 755 | return false; |
|---|
| 756 | } |
|---|
| 757 | |
|---|
| 758 | while(d_iter!=d_end_iter && !(qtype.getCode()==QType::ANY || (d_iter)->qtype==qtype.getCode())) { |
|---|
| 759 | DLOG(L<<Logger::Warning<<"Skipped "<<qname<<"/"<<QType(d_iter->qtype).getName()<<": '"<<d_iter->content<<"'"<<endl); |
|---|
| 760 | d_iter++; |
|---|
| 761 | } |
|---|
| 762 | if(d_iter==d_end_iter) { |
|---|
| 763 | |
|---|
| 764 | return false; |
|---|
| 765 | } |
|---|
| 766 | DLOG(L << "Bind2Backend get() returning a rr with a "<<QType(d_iter->qtype).getCode()<<endl); |
|---|
| 767 | |
|---|
| 768 | r.qname=qname.empty() ? domain : (qname+"."+domain); |
|---|
| 769 | r.domain_id=id; |
|---|
| 770 | r.content=(d_iter)->content; |
|---|
| 771 | // r.domain_id=(d_iter)->domain_id; |
|---|
| 772 | r.qtype=(d_iter)->qtype; |
|---|
| 773 | r.ttl=(d_iter)->ttl; |
|---|
| 774 | r.priority=(d_iter)->priority; |
|---|
| 775 | d_iter++; |
|---|
| 776 | |
|---|
| 777 | return true; |
|---|
| 778 | } |
|---|
| 779 | |
|---|
| 780 | bool Bind2Backend::list(const string &target, int id) |
|---|
| 781 | { |
|---|
| 782 | if(!s_id_zone_map.count(id)) |
|---|
| 783 | return false; |
|---|
| 784 | |
|---|
| 785 | d_handle.reset(); |
|---|
| 786 | DLOG(L<<"Bind2Backend constructing handle for list of "<<id<<endl); |
|---|
| 787 | |
|---|
| 788 | d_handle.d_qname_iter=s_id_zone_map[id].d_records->begin(); |
|---|
| 789 | d_handle.d_qname_end=s_id_zone_map[id].d_records->end(); // iter now points to a vector of pointers to vector<BBResourceRecords> |
|---|
| 790 | |
|---|
| 791 | d_handle.d_records=s_id_zone_map[id].d_records; // give it a copy --- WHY??? XXX FIXME |
|---|
| 792 | |
|---|
| 793 | d_handle.parent=this; |
|---|
| 794 | d_handle.id=id; |
|---|
| 795 | d_handle.d_list=true; |
|---|
| 796 | return true; |
|---|
| 797 | |
|---|
| 798 | } |
|---|
| 799 | |
|---|
| 800 | bool Bind2Backend::handle::get_list(DNSResourceRecord &r) |
|---|
| 801 | { |
|---|
| 802 | if(d_qname_iter!=d_qname_end) { |
|---|
| 803 | r.qname=d_qname_iter->qname.empty() ? domain : (d_qname_iter->qname+"."+domain); |
|---|
| 804 | r.domain_id=id; |
|---|
| 805 | r.content=(d_qname_iter)->content; |
|---|
| 806 | r.qtype=(d_qname_iter)->qtype; |
|---|
| 807 | r.ttl=(d_qname_iter)->ttl; |
|---|
| 808 | r.priority=(d_qname_iter)->priority; |
|---|
| 809 | d_qname_iter++; |
|---|
| 810 | return true; |
|---|
| 811 | } |
|---|
| 812 | return false; |
|---|
| 813 | |
|---|
| 814 | } |
|---|
| 815 | |
|---|
| 816 | bool Bind2Backend::isMaster(const string &name, const string &ip) |
|---|
| 817 | { |
|---|
| 818 | for(id_zone_map_t::iterator j=us->s_id_zone_map.begin();j!=us->s_id_zone_map.end();++j) |
|---|
| 819 | if(j->second.d_name==name) |
|---|
| 820 | return j->second.d_master==ip; |
|---|
| 821 | return false; |
|---|
| 822 | } |
|---|
| 823 | |
|---|
| 824 | bool Bind2Backend::superMasterBackend(const string &ip, const string &domain, const vector<DNSResourceRecord>&nsset, string *account, DNSBackend **db) |
|---|
| 825 | { |
|---|
| 826 | // Check whether we have a configfile available. |
|---|
| 827 | if (getArg("supermaster-config").empty()) |
|---|
| 828 | return false; |
|---|
| 829 | |
|---|
| 830 | ifstream c_if(getArg("supermasters").c_str(), ios::in); // this was nocreate? |
|---|
| 831 | if (!c_if) { |
|---|
| 832 | L << Logger::Error << "Unable to open supermasters file for read: " << stringerror() << endl; |
|---|
| 833 | return false; |
|---|
| 834 | } |
|---|
| 835 | |
|---|
| 836 | // Format: |
|---|
| 837 | // <ip> <accountname> |
|---|
| 838 | string line, sip, saccount; |
|---|
| 839 | while (getline(c_if, line)) { |
|---|
| 840 | istringstream ii(line); |
|---|
| 841 | ii >> sip; |
|---|
| 842 | if (sip == ip) { |
|---|
| 843 | ii >> saccount; |
|---|
| 844 | break; |
|---|
| 845 | } |
|---|
| 846 | } |
|---|
| 847 | c_if.close(); |
|---|
| 848 | |
|---|
| 849 | if (sip != ip) // ip not found in authorization list - reject |
|---|
| 850 | return false; |
|---|
| 851 | |
|---|
| 852 | // ip authorized as supermaster - accept |
|---|
| 853 | *db = this; |
|---|
| 854 | if (saccount.length() > 0) |
|---|
| 855 | *account = saccount.c_str(); |
|---|
| 856 | |
|---|
| 857 | return true; |
|---|
| 858 | } |
|---|
| 859 | |
|---|
| 860 | bool Bind2Backend::createSlaveDomain(const string &ip, const string &domain, const string &account) |
|---|
| 861 | { |
|---|
| 862 | // Interference with loadConfig(), use locking |
|---|
| 863 | Lock l(&s_zonemap_lock); |
|---|
| 864 | |
|---|
| 865 | string filename = getArg("supermaster-destdir")+'/'+domain; |
|---|
| 866 | |
|---|
| 867 | L << Logger::Warning << d_logprefix |
|---|
| 868 | << " Writing bind config zone statement for superslave zone '" << domain |
|---|
| 869 | << "' from supermaster " << ip << endl; |
|---|
| 870 | |
|---|
| 871 | ofstream c_of(getArg("supermaster-config").c_str(), ios::app); |
|---|
| 872 | if (!c_of) { |
|---|
| 873 | L << Logger::Error << "Unable to open supermaster configfile for append: " << stringerror() << endl; |
|---|
| 874 | throw DBException("Unable to open supermaster configfile for append: "+stringerror()); |
|---|
| 875 | } |
|---|
| 876 | |
|---|
| 877 | c_of << endl; |
|---|
| 878 | c_of << "# Superslave zone " << domain << " (added: " << nowTime() << ") (account: " << account << ')' << endl; |
|---|
| 879 | c_of << "zone \"" << domain << "\" {" << endl; |
|---|
| 880 | c_of << "\ttype slave;" << endl; |
|---|
| 881 | c_of << "\tfile \"" << filename << "\";" << endl; |
|---|
| 882 | c_of << "\tmasters { " << ip << "; };" << endl; |
|---|
| 883 | c_of << "};" << endl; |
|---|
| 884 | c_of.close(); |
|---|
| 885 | |
|---|
| 886 | int newid=0; |
|---|
| 887 | // Find a free zone id nr. |
|---|
| 888 | |
|---|
| 889 | if (!s_id_zone_map.empty()) { |
|---|
| 890 | id_zone_map_t::reverse_iterator i = s_id_zone_map.rbegin(); |
|---|
| 891 | newid = i->second.d_id + 1; |
|---|
| 892 | } |
|---|
| 893 | |
|---|
| 894 | BB2DomainInfo &bbd = s_id_zone_map[newid]; |
|---|
| 895 | |
|---|
| 896 | bbd.d_records = shared_ptr<vector<Bind2DNSRecord> >(new vector<Bind2DNSRecord>); |
|---|
| 897 | bbd.d_name = domain; |
|---|
| 898 | bbd.setCheckInterval(getArgAsNum("check-interval")); |
|---|
| 899 | bbd.d_master = ip; |
|---|
| 900 | bbd.d_filename = filename; |
|---|
| 901 | |
|---|
| 902 | s_name_id_map[domain] = bbd.d_id; |
|---|
| 903 | |
|---|
| 904 | return true; |
|---|
| 905 | } |
|---|
| 906 | |
|---|
| 907 | class Bind2Factory : public BackendFactory |
|---|
| 908 | { |
|---|
| 909 | public: |
|---|
| 910 | Bind2Factory() : BackendFactory("bind2") {} |
|---|
| 911 | |
|---|
| 912 | void declareArguments(const string &suffix="") |
|---|
| 913 | { |
|---|
| 914 | declare(suffix,"config","Location of named.conf",""); |
|---|
| 915 | declare(suffix,"example-zones","Install example zones","no"); |
|---|
| 916 | declare(suffix,"check-interval","Interval for zonefile changes","0"); |
|---|
| 917 | declare(suffix,"supermaster-config","Location of (part of) named.conf where pdns can write zone-statements to",""); |
|---|
| 918 | declare(suffix,"supermasters","List of IP-addresses of supermasters",""); |
|---|
| 919 | declare(suffix,"supermaster-destdir","Destination directory for newly added slave zones",arg()["config-dir"]); |
|---|
| 920 | } |
|---|
| 921 | |
|---|
| 922 | DNSBackend *make(const string &suffix="") |
|---|
| 923 | { |
|---|
| 924 | return new Bind2Backend(suffix); |
|---|
| 925 | } |
|---|
| 926 | }; |
|---|
| 927 | |
|---|
| 928 | //! Magic class that is activated when the dynamic library is loaded |
|---|
| 929 | class Bind2Loader |
|---|
| 930 | { |
|---|
| 931 | public: |
|---|
| 932 | Bind2Loader() |
|---|
| 933 | { |
|---|
| 934 | BackendMakers().report(new Bind2Factory); |
|---|
| 935 | L<<Logger::Notice<<"[Bind2Backend] This is the bind backend version "VERSION" ("__DATE__", "__TIME__") reporting"<<endl; |
|---|
| 936 | } |
|---|
| 937 | }; |
|---|
| 938 | static Bind2Loader bind2loader; |
|---|