| 1 | /* |
|---|
| 2 | * PowerDNS OpenDBX Backend |
|---|
| 3 | * Copyright (C) 2005 Norbert Sendetzky <norbert@linuxnetworks.de> |
|---|
| 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 |
|---|
| 7 | * the Free Software Foundation |
|---|
| 8 | * any later version. |
|---|
| 9 | * |
|---|
| 10 | * This program is distributed in the hope that it will be useful, |
|---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | * GNU General Public License for more details. |
|---|
| 14 | * |
|---|
| 15 | * You should have received a copy of the GNU General Public License |
|---|
| 16 | * along with this program; if not, write to the Free Software |
|---|
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 18 | */ |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | #include <string> |
|---|
| 23 | #include <cstdlib> |
|---|
| 24 | #include <sstream> |
|---|
| 25 | #include <sys/time.h> |
|---|
| 26 | #include <pdns/dns.hh> |
|---|
| 27 | #include <pdns/utility.hh> |
|---|
| 28 | #include <pdns/dnspacket.hh> |
|---|
| 29 | #include <pdns/dnsbackend.hh> |
|---|
| 30 | #include <pdns/ueberbackend.hh> |
|---|
| 31 | #include <pdns/ahuexception.hh> |
|---|
| 32 | #include <pdns/arguments.hh> |
|---|
| 33 | #include <pdns/logger.hh> |
|---|
| 34 | #include <odbx.h> |
|---|
| 35 | #include "modules/ldapbackend/utils.hh" |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | #ifndef ODBXBACKEND_HH |
|---|
| 39 | #define ODBXBACKEND_HH |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | #define BUFLEN 512 |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | using std::string; |
|---|
| 46 | using std::vector; |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | bool checkSlave( u_int32_t last, u_int32_t notified, SOAData* sd, DomainInfo* di ); |
|---|
| 51 | bool checkMaster( u_int32_t last, u_int32_t notified, SOAData* sd, DomainInfo* di ); |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | class OdbxBackend : public DNSBackend |
|---|
| 55 | { |
|---|
| 56 | string m_myname; |
|---|
| 57 | string m_qname; |
|---|
| 58 | int m_default_ttl; |
|---|
| 59 | bool m_qlog; |
|---|
| 60 | odbx_t* m_handle; |
|---|
| 61 | odbx_result_t* m_result; |
|---|
| 62 | char m_escbuf[BUFLEN]; |
|---|
| 63 | char m_buffer[2*BUFLEN]; |
|---|
| 64 | |
|---|
| 65 | bool getRecord(); |
|---|
| 66 | void execStmt( const char* stmt, unsigned long length, bool select ); |
|---|
| 67 | void getDomainList( const string& query, vector<DomainInfo>* list, bool (*check_fcn)(u_int32_t,u_int32_t,SOAData*,DomainInfo*) ); |
|---|
| 68 | string escape( const string& str ); |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | public: |
|---|
| 72 | |
|---|
| 73 | OdbxBackend( const string& suffix="" ); |
|---|
| 74 | ~OdbxBackend(); |
|---|
| 75 | |
|---|
| 76 | void lookup( const QType& qtype, const string& qdomain, DNSPacket* p = 0, int zoneid = -1 ); |
|---|
| 77 | bool list( const string& target, int domain_id ); |
|---|
| 78 | bool get( DNSResourceRecord& rr ); |
|---|
| 79 | |
|---|
| 80 | bool startTransaction( const string& domain, int domain_id ); |
|---|
| 81 | bool commitTransaction(); |
|---|
| 82 | bool abortTransaction(); |
|---|
| 83 | |
|---|
| 84 | bool isMaster( const string& domain, const string& ip ); |
|---|
| 85 | bool getDomainInfo( const string& domain, DomainInfo& di ); |
|---|
| 86 | bool feedRecord( const DNSResourceRecord& rr ); |
|---|
| 87 | bool createSlaveDomain( const string& ip, const string& domain, const string& account ); |
|---|
| 88 | bool superMasterBackend( const string& ip, const string& domain, const vector<DNSResourceRecord>& nsset, string* account, DNSBackend** ddb ); |
|---|
| 89 | |
|---|
| 90 | void getUpdatedMasters( vector<DomainInfo>* updated ); |
|---|
| 91 | void getUnfreshSlaveInfos( vector<DomainInfo>* unfresh ); |
|---|
| 92 | |
|---|
| 93 | void setFresh( u_int32_t domain_id ); |
|---|
| 94 | void setNotified( u_int32_t domain_id, u_int32_t serial ); |
|---|
| 95 | }; |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | class OdbxFactory : public BackendFactory |
|---|
| 100 | { |
|---|
| 101 | |
|---|
| 102 | public: |
|---|
| 103 | |
|---|
| 104 | OdbxFactory() : BackendFactory( "opendbx" ) {} |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | void declareArguments( const string &suffix="" ) |
|---|
| 108 | { |
|---|
| 109 | declare( suffix, "backend", "OpenDBX backend","mysql" ); |
|---|
| 110 | declare( suffix, "host", "Name or address of one or more DBMS server","127.0.0.1" ); |
|---|
| 111 | declare( suffix, "port", "Port the DBMS server is listening to","" ); |
|---|
| 112 | declare( suffix, "database", "Database name containing the DNS records","powerdns" ); |
|---|
| 113 | declare( suffix, "username","User for connecting to the DBMS","powerdns"); |
|---|
| 114 | declare( suffix, "password","Password for connecting to the DBMS",""); |
|---|
| 115 | |
|---|
| 116 | declare( suffix, "sql-list", "AXFR query", "SELECT domain_id, name, type, ttl, prio, content FROM records WHERE domain_id=':id'" ); |
|---|
| 117 | |
|---|
| 118 | declare( suffix, "sql-lookup", "Lookup query","SELECT domain_id, name, type, ttl, prio, content FROM records WHERE name=':name'" ); |
|---|
| 119 | declare( suffix, "sql-lookupid", "Lookup query with id","SELECT domain_id, name, type, ttl, prio, content FROM records WHERE domain_id=':id' AND name=':name'" ); |
|---|
| 120 | declare( suffix, "sql-lookuptype", "Lookup query with type","SELECT domain_id, name, type, ttl, prio, content FROM records WHERE name=':name' AND type=':type'" ); |
|---|
| 121 | declare( suffix, "sql-lookuptypeid", "Lookup query with type and id","SELECT domain_id, name, type, ttl, prio, content FROM records WHERE domain_id=':id' AND name=':name' AND type=':type'" ); |
|---|
| 122 | |
|---|
| 123 | declare( suffix, "sql-zonedelete","Delete all records for this zone","DELETE FROM records WHERE domain_id=':id'" ); |
|---|
| 124 | declare( suffix, "sql-zoneinfo","Get domain info","SELECT d.id, d.name, d.type, d.master, d.last_check, r.content FROM domains AS d LEFT JOIN records AS r ON d.id=r.domain_id WHERE ( d.name=':name' AND r.type='SOA' ) OR ( d.name=':name' AND r.domain_id IS NULL )" ); |
|---|
| 125 | |
|---|
| 126 | declare( suffix, "sql-transactbegin", "Start transaction", "BEGIN" ); |
|---|
| 127 | declare( suffix, "sql-transactend", "Finish transaction", "COMMIT" ); |
|---|
| 128 | declare( suffix, "sql-transactabort", "Abort transaction", "ROLLBACK" ); |
|---|
| 129 | |
|---|
| 130 | declare( suffix, "sql-insert-slave","Add slave domain", "INSERT INTO domains ( name, type, master, account ) VALUES ( '%s', 'SLAVE', '%s', '%s' )" ); |
|---|
| 131 | declare( suffix, "sql-insert-record","Feed record into table", "INSERT INTO records ( domain_id, name, type, ttl, prio, content ) VALUES ( '%d', '%s', '%s', '%d', '%d', '%s' )" ); |
|---|
| 132 | |
|---|
| 133 | declare( suffix, "sql-update-serial", "Set zone to notified", "UPDATE domains SET notified_serial='%d' WHERE id='%d'" ); |
|---|
| 134 | declare( suffix, "sql-update-lastcheck", "Set time of last check", "UPDATE domains SET last_check='%d' WHERE id='%d'" ); |
|---|
| 135 | |
|---|
| 136 | declare( suffix, "sql-master", "Get master record for zone", "SELECT master FROM domains WHERE name=':name' AND type='SLAVE'" ); |
|---|
| 137 | declare( suffix, "sql-supermaster","Get supermaster info", "SELECT account FROM supermasters WHERE ip=':ip' AND nameserver=':ns'" ); |
|---|
| 138 | |
|---|
| 139 | declare( suffix, "sql-infoslaves", "Get all unfresh slaves", "SELECT d.id, d.name, d.master, d.notified_serial, d.last_check, r.change_date, r.content FROM domains AS d LEFT JOIN records AS r ON d.id=r.domain_id WHERE ( d.type='SLAVE' AND r.type='SOA' ) OR ( d.type='SLAVE' AND r.domain_id IS NULL )" ); |
|---|
| 140 | declare( suffix, "sql-infomasters", "Get all updated masters", "SELECT d.id, d.name, d.master, d.notified_serial, d.last_check, r.change_date, r.content FROM domains AS d, records AS r WHERE d.type='MASTER' AND d.id=r.domain_id AND r.type='SOA'" ); |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | DNSBackend* make( const string &suffix="" ) |
|---|
| 145 | { |
|---|
| 146 | return new OdbxBackend( suffix ); |
|---|
| 147 | } |
|---|
| 148 | }; |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | class OdbxLoader |
|---|
| 152 | { |
|---|
| 153 | OdbxFactory factory; |
|---|
| 154 | |
|---|
| 155 | public: |
|---|
| 156 | |
|---|
| 157 | OdbxLoader() |
|---|
| 158 | { |
|---|
| 159 | BackendMakers().report( &factory ); |
|---|
| 160 | L.log( " [OpendbxBackend] This is the opendbx module version "VERSION" ("__DATE__", "__TIME__") reporting", Logger::Info ); |
|---|
| 161 | } |
|---|
| 162 | }; |
|---|
| 163 | |
|---|
| 164 | |
|---|
| 165 | static OdbxLoader odbxloader; |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | #endif /* ODBXBACKEND_HH */ |
|---|