]> git.ipfire.org Git - thirdparty/pdns.git/blame - modules/opendbxbackend/odbxbackend.hh
Logging: have a global g_log
[thirdparty/pdns.git] / modules / opendbxbackend / odbxbackend.hh
CommitLineData
790e7c1b 1/*
12471842
PL
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
4 * originally authored by Norbert Sendetzky
790e7c1b 5 *
12471842
PL
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of version 2 of the GNU General Public License as
8 * published by the Free Software Foundation.
790e7c1b 9 *
12471842
PL
10 * In addition, for the avoidance of any doubt, permission is granted to
11 * link this program with OpenSSL and to (re)distribute the binaries
12 * produced as the result of such linking.
fc3c07b4 13 *
12471842
PL
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
790e7c1b 18 *
12471842
PL
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
790e7c1b 22 */
790e7c1b
BH
23#include <string>
24#include <cstdlib>
25#include <sstream>
26#include <sys/time.h>
21e99384
RK
27#include "pdns/dns.hh"
28#include "pdns/utility.hh"
29#include "pdns/dnspacket.hh"
30#include "pdns/dnsbackend.hh"
21e99384
RK
31#include "pdns/pdnsexception.hh"
32#include "pdns/arguments.hh"
33#include "pdns/logger.hh"
790e7c1b 34#include <odbx.h>
790e7c1b
BH
35
36
37#ifndef ODBXBACKEND_HH
38#define ODBXBACKEND_HH
39
40
41#define BUFLEN 512
42
43
44using std::string;
45using std::vector;
46
47
48
e830c249
BH
49bool checkSlave( uint32_t last, uint32_t notified, SOAData* sd, DomainInfo* di );
50bool checkMaster( uint32_t last, uint32_t notified, SOAData* sd, DomainInfo* di );
790e7c1b
BH
51
52
53class OdbxBackend : public DNSBackend
54{
e0ec6e79 55 enum QueryType { READ, WRITE };
8e20e603 56
e0ec6e79 57 string m_myname;
ed56a9d7 58 DNSName m_qname;
e0ec6e79
BH
59 int m_default_ttl;
60 bool m_qlog;
61 odbx_t* m_handle[2];
62 odbx_result_t* m_result;
63 char m_escbuf[BUFLEN];
64 char m_buffer[2*BUFLEN];
65 vector<string> m_hosts[2];
790e7c1b 66
e0ec6e79
BH
67 string escape( const string& str, QueryType type );
68 bool connectTo( const vector<string>& host, QueryType type );
69 bool getDomainList( const string& query, vector<DomainInfo>* list, bool (*check_fcn)(uint32_t,uint32_t,SOAData*,DomainInfo*) );
70 bool execStmt( const char* stmt, unsigned long length, QueryType type );
71 bool getRecord( QueryType type );
790e7c1b
BH
72
73
74public:
75
e0ec6e79
BH
76 OdbxBackend( const string& suffix="" );
77 ~OdbxBackend();
790e7c1b 78
d57d7155 79 void lookup( const QType& qtype, const DNSName& qdomain, DNSPacket* p = 0, int zoneid = -1 ) override;
13f9e280 80 bool getSOA( const DNSName& domain, SOAData& sd, bool unmodifiedSerial ) override;
d57d7155
KM
81 bool list( const DNSName& target, int domain_id, bool include_disabled=false ) override;
82 bool get( DNSResourceRecord& rr ) override;
83
cf8c91f3 84 bool startTransaction( const DNSName& domain, int domain_id ) override;
d57d7155
KM
85 bool commitTransaction() override;
86 bool abortTransaction() override;
87
cf8c91f3
KM
88 bool isMaster( const DNSName& domain, const string& ip ) override;
89 bool getDomainInfo( const DNSName& domain, DomainInfo& di ) override;
d57d7155 90 bool feedRecord( const DNSResourceRecord& rr, const DNSName& ordername ) override;
cf8c91f3
KM
91 bool createSlaveDomain( const string& ip, const DNSName& domain, const string &nameserver, const string& account ) override;
92 bool superMasterBackend( const string& ip, const DNSName& domain, const vector<DNSResourceRecord>& nsset, string *nameserver, string* account, DNSBackend** ddb ) override;
d57d7155
KM
93
94 void getUpdatedMasters( vector<DomainInfo>* updated ) override;
95 void getUnfreshSlaveInfos( vector<DomainInfo>* unfresh ) override;
96
97 void setFresh( uint32_t domain_id ) override;
98 void setNotified( uint32_t domain_id, uint32_t serial ) override;
790e7c1b
BH
99};
100
101
102
103class OdbxFactory : public BackendFactory
104{
105
106public:
107
e0ec6e79 108 OdbxFactory() : BackendFactory( "opendbx" ) {}
790e7c1b
BH
109
110
e0ec6e79
BH
111 void declareArguments( const string &suffix="" )
112 {
113 declare( suffix, "backend", "OpenDBX backend","mysql" );
114 declare( suffix, "host-read", "Name or address of one or more DBMS server to read from","127.0.0.1" );
115 declare( suffix, "host-write", "Name or address of one or more DBMS server used for updates","127.0.0.1" );
116 declare( suffix, "port", "Port the DBMS server are listening to","" );
117 declare( suffix, "database", "Database name containing the DNS records","powerdns" );
118 declare( suffix, "username","User for connecting to the DBMS","powerdns");
119 declare( suffix, "password","Password for connecting to the DBMS","");
790e7c1b 120
e0ec6e79 121 declare( suffix, "sql-list", "AXFR query", "SELECT r.\"domain_id\", r.\"name\", r.\"type\", r.\"ttl\", r.\"prio\", r.\"content\" FROM \"records\" r WHERE r.\"domain_id\"=:id" );
790e7c1b 122
e0ec6e79
BH
123 declare( suffix, "sql-lookup", "Lookup query","SELECT r.\"domain_id\", r.\"name\", r.\"type\", r.\"ttl\", r.\"prio\", r.\"content\" FROM \"records\" r WHERE r.\"name\"=':name'" );
124 declare( suffix, "sql-lookupid", "Lookup query with id","SELECT r.\"domain_id\", r.\"name\", r.\"type\", r.\"ttl\", r.\"prio\", r.\"content\" FROM \"records\" r WHERE r.\"domain_id\"=:id AND r.\"name\"=':name'" );
125 declare( suffix, "sql-lookuptype", "Lookup query with type","SELECT r.\"domain_id\", r.\"name\", r.\"type\", r.\"ttl\", r.\"prio\", r.\"content\" FROM \"records\" r WHERE r.\"name\"=':name' AND r.\"type\"=':type'" );
126 declare( suffix, "sql-lookuptypeid", "Lookup query with type and id","SELECT r.\"domain_id\", r.\"name\", r.\"type\", r.\"ttl\", r.\"prio\", r.\"content\" FROM \"records\" r WHERE r.\"domain_id\"=:id AND r.\"name\"=':name' AND r.\"type\"=':type'" );
127 declare( suffix, "sql-lookupsoa","Lookup query for SOA record","SELECT d.\"id\", d.\"auto_serial\", r.\"ttl\", r.\"content\" FROM \"records\" r JOIN \"domains\" d ON r.\"domain_id\"=d.\"id\" WHERE r.\"name\"=':name' AND r.\"type\"='SOA' AND d.\"status\"='A'" );
790e7c1b 128
e0ec6e79
BH
129 declare( suffix, "sql-zonedelete","Delete all records for this zone","DELETE FROM \"records\" WHERE \"domain_id\"=:id" );
130 declare( suffix, "sql-zoneinfo","Get domain info","SELECT d.\"id\", d.\"name\", d.\"type\", d.\"master\", d.\"last_check\", d.\"auto_serial\", r.\"content\" FROM \"domains\" d LEFT JOIN \"records\" r ON ( d.\"id\"=r.\"domain_id\" AND r.\"type\"='SOA' ) WHERE d.\"name\"=':name' AND d.\"status\"='A'" );
790e7c1b 131
e0ec6e79
BH
132 declare( suffix, "sql-transactbegin", "Start transaction", "BEGIN" );
133 declare( suffix, "sql-transactend", "Finish transaction", "COMMIT" );
134 declare( suffix, "sql-transactabort", "Abort transaction", "ROLLBACK" );
790e7c1b 135
e0ec6e79
BH
136 declare( suffix, "sql-insert-slave","Add slave domain", "INSERT INTO \"domains\" ( \"name\", \"type\", \"master\", \"account\" ) VALUES ( '%s', 'SLAVE', '%s', '%s' )" );
137 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' )" );
8e20e603 138
e0ec6e79
BH
139 declare( suffix, "sql-update-serial", "Set zone to notified", "UPDATE \"domains\" SET \"notified_serial\"=%d WHERE \"id\"=%d" );
140 declare( suffix, "sql-update-lastcheck", "Set time of last check", "UPDATE \"domains\" SET \"last_check\"=%d WHERE \"id\"=%d" );
790e7c1b 141
e0ec6e79
BH
142 declare( suffix, "sql-master", "Get master record for zone", "SELECT d.\"master\" FROM \"domains\" d WHERE d.\"name\"=':name' AND d.\"status\"='A' AND d.\"type\"='SLAVE'" );
143 declare( suffix, "sql-supermaster","Get supermaster info", "SELECT s.\"account\" FROM \"supermasters\" s WHERE s.\"ip\"=':ip' AND s.\"nameserver\"=':ns'" );
790e7c1b 144
e0ec6e79
BH
145 declare( suffix, "sql-infoslaves", "Get all unfresh slaves", "SELECT d.\"id\", d.\"name\", d.\"master\", d.\"last_check\", d.\"notified_serial\", d.\"auto_serial\", r.\"content\" FROM \"domains\" d LEFT JOIN \"records\" r ON ( d.\"id\"=r.\"domain_id\" AND r.\"type\"='SOA' ) WHERE d.\"status\"='A' AND d.\"type\"='SLAVE'" );
146 declare( suffix, "sql-infomasters", "Get all updated masters", "SELECT d.\"id\", d.\"name\", d.\"master\", d.\"last_check\", d.\"notified_serial\", d.\"auto_serial\", r.\"content\" FROM \"domains\" d LEFT JOIN \"records\" r ON ( d.\"id\"=r.\"domain_id\" AND r.\"type\"='SOA' ) WHERE d.\"status\"='A' AND d.\"type\"='MASTER'" );
790e7c1b 147
abc1d928 148 declare( suffix, "host", "deprecated, use host-read and host-write instead","" );
e0ec6e79 149 }
790e7c1b
BH
150
151
e0ec6e79
BH
152 DNSBackend* make( const string &suffix="" )
153 {
154 return new OdbxBackend( suffix );
155 }
790e7c1b
BH
156};
157
158
159class OdbxLoader
160{
e0ec6e79 161 OdbxFactory factory;
790e7c1b
BH
162
163public:
164
e0ec6e79
BH
165 OdbxLoader()
166 {
167 BackendMakers().report( &factory );
e6a9dde5 168 g_log<< Logger::Info << "[opendbxbackend] This is the opendbx backend version " VERSION
5e6a3d93 169#ifndef REPRODUCIBLE
cf8c91f3 170 << " (" __DATE__ " " __TIME__ ")"
5e6a3d93 171#endif
cf8c91f3 172 << " reporting" << endl;
e0ec6e79 173 }
790e7c1b
BH
174};
175
176
177static OdbxLoader odbxloader;
178
179
180
181#endif /* ODBXBACKEND_HH */