]>
Commit | Line | Data |
---|---|---|
12471842 PL |
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 | */ | |
4f983d1b PD |
22 | #include "pdns/utility.hh" |
23 | #include <map> | |
24 | #include <sstream> | |
25 | #include <string> | |
26 | ||
27 | #include "pdns/dns.hh" | |
28 | #include "pdns/dnsbackend.hh" | |
29 | #include "pdns/dnspacket.hh" | |
4f983d1b PD |
30 | #include "pdns/pdnsexception.hh" |
31 | #include "pdns/logger.hh" | |
32 | #include "pdns/arguments.hh" | |
33 | #include "sodbc.hh" | |
34 | #include "godbcbackend.hh" | |
35 | ||
36 | ||
37 | // Connects to the database. | |
38 | gODBCBackend::gODBCBackend (const std::string & mode, const std::string & suffix) : GSQLBackend( mode, suffix ) | |
39 | { | |
40 | try | |
41 | { | |
42 | setDB( new SODBC( getArg( "datasource" ), getArg( "username" ), getArg( "password" ))); | |
43 | } | |
44 | catch( SSqlException & e ) | |
45 | { | |
e6a9dde5 | 46 | g_log<<Logger::Error<< mode << " Connection failed: " << e.txtReason() << std::endl; |
4f983d1b PD |
47 | throw PDNSException( "Unable to launch " + mode + " connection: " + e.txtReason()); |
48 | } | |
49 | ||
e6a9dde5 | 50 | g_log << Logger::Warning << mode << " Connection successful" << std::endl; |
4f983d1b PD |
51 | } |
52 | ||
53 | ||
54 | //! Constructs a gODBCBackend | |
55 | class gODBCFactory : public BackendFactory | |
56 | { | |
57 | public: | |
58 | //! Constructor. | |
59 | gODBCFactory( const std::string & mode ) : BackendFactory( mode ), d_mode( mode ) | |
60 | { | |
61 | } | |
62 | ||
63 | //! Declares all needed arguments. | |
64 | void declareArguments( const std::string & suffix = "" ) | |
65 | { | |
66 | declare( suffix, "datasource", "Datasource (DSN) to use","PowerDNS"); | |
67 | declare( suffix, "username", "User to connect as","powerdns"); | |
68 | declare( suffix, "password", "Password to connect with",""); | |
69 | declare(suffix,"dnssec","Enable DNSSEC processing","no"); | |
70 | ||
71 | string record_query = "SELECT content,ttl,prio,type,domain_id,disabled,name,auth FROM records WHERE"; | |
72 | ||
73 | declare(suffix, "basic-query", "Basic query", record_query+" disabled=0 and type=? and name=?"); | |
74 | declare(suffix, "id-query", "Basic with ID query", record_query+" disabled=0 and type=? and name=? and domain_id=?"); | |
75 | declare(suffix, "any-query", "Any query", record_query+" disabled=0 and name=?"); | |
76 | declare(suffix, "any-id-query", "Any with ID query", record_query+" disabled=0 and name=? and domain_id=?"); | |
77 | ||
78 | declare(suffix, "list-query", "AXFR query", record_query+" (disabled=0 OR disabled=?) and domain_id=? order by name, type"); | |
79 | declare(suffix, "list-subzone-query", "Subzone listing", record_query+" disabled=0 and (name=? OR name like ?) and domain_id=?"); | |
80 | ||
81 | declare(suffix, "remove-empty-non-terminals-from-zone-query", "remove all empty non-terminals from zone", "delete from records where domain_id=? and type is null"); | |
4f983d1b PD |
82 | declare(suffix, "delete-empty-non-terminal-query", "delete empty non-terminal from zone", "delete from records where domain_id=? and name=? and type is null"); |
83 | ||
84 | declare(suffix,"master-zone-query","Data", "select master from domains where name=? and type='SLAVE'"); | |
85 | ||
86 | declare(suffix,"info-zone-query","","select id,name,master,last_check,notified_serial,type,account from domains where name=?"); | |
87 | ||
88 | declare(suffix,"info-all-slaves-query","","select id,name,master,last_check from domains where type='SLAVE'"); | |
89 | declare(suffix,"supermaster-query","", "select account from supermasters where ip=? and nameserver=?"); | |
90 | declare(suffix,"supermaster-name-to-ips", "", "select ip,account from supermasters where nameserver=? and account=?"); | |
91 | ||
b90323a9 | 92 | declare(suffix,"insert-zone-query","", "insert into domains (type,name,master,account,last_check,notified_serial) values(?,?,?,?,null,null)"); |
4f983d1b | 93 | |
b90323a9 PL |
94 | declare(suffix, "insert-record-query", "", "insert into records (content,ttl,prio,type,domain_id,disabled,name,ordername,auth,change_date) values (?,?,?,?,?,?,?,convert(varbinary(255),?),?,null)"); |
95 | declare(suffix, "insert-empty-non-terminal-order-query", "insert empty non-terminal in zone", "insert into records (type,domain_id,disabled,name,ordername,auth,ttl,prio,change_date,content) values (null,?,0,?,convert(varbinary(255),?),?,null,null,null,null)"); | |
4f983d1b PD |
96 | |
97 | declare(suffix, "get-order-first-query", "DNSSEC Ordering Query, first", "select top 1 convert(varchar(255), ordername) from records where domain_id=? and disabled=0 and ordername is not null order by 1 asc"); | |
98 | declare(suffix, "get-order-before-query", "DNSSEC Ordering Query, before", "select top 1 convert(varchar(255), ordername), name from records where ordername <= convert(varbinary(255),?) and domain_id=? and disabled=0 and ordername is not null order by 1 desc"); | |
99 | declare(suffix, "get-order-after-query", "DNSSEC Ordering Query, after", "select convert(varchar(255), min(ordername)) from records where ordername > convert(varbinary(255),?) and domain_id=? and disabled=0 and ordername is not null"); | |
100 | declare(suffix, "get-order-last-query", "DNSSEC Ordering Query, last", "select top 1 convert(varchar(255), ordername), name from records where ordername != convert(varbinary(255),'') and domain_id=? and disabled=0 and ordername is not null order by 1 desc"); | |
101 | ||
102 | declare(suffix, "update-ordername-and-auth-query", "DNSSEC update ordername and auth for a qname query", "update records set ordername=convert(varbinary(255),?),auth=? where domain_id=? and name=? and disabled=0"); | |
103 | declare(suffix, "update-ordername-and-auth-type-query", "DNSSEC update ordername and auth for a rrset query", "update records set ordername=convert(varbinary(255),?),auth=? where domain_id=? and name=? and type=? and disabled=0"); | |
104 | declare(suffix, "nullify-ordername-and-update-auth-query", "DNSSEC nullify ordername and update auth for a qname query", "update records set ordername=NULL,auth=? where domain_id=? and name=? and disabled=0"); | |
105 | declare(suffix, "nullify-ordername-and-update-auth-type-query", "DNSSEC nullify ordername and update auth for a rrset query", "update records set ordername=NULL,auth=? where domain_id=? and name=? and type=? and disabled=0"); | |
106 | ||
107 | declare(suffix,"update-master-query","", "update domains set master=? where name=?"); | |
108 | declare(suffix,"update-kind-query","", "update domains set type=? where name=?"); | |
109 | declare(suffix,"update-account-query","", "update domains set account=? where name=?"); | |
110 | declare(suffix,"update-serial-query","", "update domains set notified_serial=? where id=?"); | |
111 | declare(suffix,"update-lastcheck-query","", "update domains set last_check=? where id=?"); | |
112 | declare(suffix,"zone-lastchange-query", "", "select max(change_date) from records where domain_id=?"); | |
113 | declare(suffix,"info-all-master-query","", "select id,name,master,last_check,notified_serial,type from domains where type='MASTER'"); | |
114 | declare(suffix,"delete-domain-query","", "delete from domains where name=?"); | |
115 | declare(suffix,"delete-zone-query","", "delete from records where domain_id=?"); | |
116 | declare(suffix,"delete-rrset-query","","delete from records where domain_id=? and name=? and type=?"); | |
117 | declare(suffix,"delete-names-query","","delete from records where domain_id=? and name=?"); | |
118 | ||
119 | declare(suffix,"add-domain-key-query","", "insert into cryptokeys (domain_id, flags, active, content) select id, ?, ?, ? from domains where name=?"); | |
1331e955 | 120 | declare(suffix,"get-last-inserted-key-id-query", "", "select ident_current('cryptokeys')"); |
4f983d1b PD |
121 | declare(suffix,"list-domain-keys-query","", "select cryptokeys.id, flags, active, content from domains, cryptokeys where cryptokeys.domain_id=domains.id and name=?"); |
122 | declare(suffix,"get-all-domain-metadata-query","", "select kind,content from domains, domainmetadata where domainmetadata.domain_id=domains.id and name=?"); | |
123 | declare(suffix,"get-domain-metadata-query","", "select content from domains, domainmetadata where domainmetadata.domain_id=domains.id and name=? and domainmetadata.kind=?"); | |
124 | declare(suffix,"clear-domain-metadata-query","", "delete from domainmetadata where domain_id=(select id from domains where name=?) and domainmetadata.kind=?"); | |
125 | declare(suffix,"clear-domain-all-metadata-query","", "delete from domainmetadata where domain_id=(select id from domains where name=?)"); | |
126 | declare(suffix,"set-domain-metadata-query","", "insert into domainmetadata (domain_id, kind, content) select id, ?, ? from domains where name=?"); | |
127 | declare(suffix,"activate-domain-key-query","", "update cryptokeys set active=1 where domain_id=(select id from domains where name=?) and cryptokeys.id=?"); | |
128 | declare(suffix,"deactivate-domain-key-query","", "update cryptokeys set active=0 where domain_id=(select id from domains where name=?) and cryptokeys.id=?"); | |
129 | declare(suffix,"remove-domain-key-query","", "delete from cryptokeys where domain_id=(select id from domains where name=?) and cryptokeys.id=?"); | |
130 | declare(suffix,"clear-domain-all-keys-query","", "delete from cryptokeys where domain_id=(select id from domains where name=?)"); | |
131 | declare(suffix,"get-tsig-key-query","", "select algorithm, secret from tsigkeys where name=?"); | |
132 | /* FIXME: set-tsig-key-query only works on an empty database right now. For MySQL we use the "update into" statement.. | |
133 | According to the internet, we need to construct a pretty hefty "merge" query: https://msdn.microsoft.com/en-us/library/bb510625.aspx | |
134 | */ | |
135 | declare(suffix,"set-tsig-key-query","", "insert into tsigkeys (name,algorithm,secret) values(?,?,?)"); | |
136 | declare(suffix,"delete-tsig-key-query","", "delete from tsigkeys where name=?"); | |
137 | declare(suffix,"get-tsig-keys-query","", "select name,algorithm, secret from tsigkeys"); | |
138 | ||
139 | declare(suffix, "get-all-domains-query", "Retrieve all domains", "select domains.id, domains.name, records.content, domains.type, domains.master, domains.notified_serial, domains.last_check, domains.account from domains LEFT JOIN records ON records.domain_id=domains.id AND records.type='SOA' AND records.name=domains.name WHERE records.disabled=0 OR records.disabled=?"); | |
140 | ||
141 | declare(suffix, "list-comments-query", "", "SELECT domain_id,name,type,modified_at,account,comment FROM comments WHERE domain_id=?"); | |
142 | declare(suffix, "insert-comment-query", "", "INSERT INTO comments (domain_id, name, type, modified_at, account, comment) VALUES (?, ?, ?, ?, ?, ?)"); | |
143 | declare(suffix, "delete-comment-rrset-query", "", "DELETE FROM comments WHERE domain_id=? AND name=? AND type=?"); | |
144 | declare(suffix, "delete-comments-query", "", "DELETE FROM comments WHERE domain_id=?"); | |
145 | declare(suffix, "search-records-query", "", record_query+" name LIKE ? OR content LIKE ? LIMIT ?"); | |
146 | declare(suffix, "search-comments-query", "", "SELECT domain_id,name,type,modified_at,account,comment FROM comments WHERE name LIKE ? OR comment LIKE ? LIMIT ?"); | |
147 | } | |
148 | ||
149 | //! Constructs a new gODBCBackend object. | |
150 | DNSBackend *make(const string & suffix = "" ) | |
151 | { | |
152 | return new gODBCBackend( d_mode, suffix ); | |
153 | } | |
154 | ||
155 | private: | |
156 | const string d_mode; | |
157 | }; | |
158 | ||
159 | ||
160 | //! Magic class that is activated when the dynamic library is loaded | |
161 | class gODBCLoader | |
162 | { | |
163 | public: | |
164 | //! This reports us to the main UeberBackend class | |
165 | gODBCLoader() | |
166 | { | |
167 | BackendMakers().report( new gODBCFactory("godbc")); | |
e6a9dde5 | 168 | g_log<<Logger::Warning << "This is module godbcbackend reporting" << std::endl; |
4f983d1b PD |
169 | } |
170 | }; | |
171 | ||
172 | //! Reports the backendloader to the UeberBackend. | |
173 | static gODBCLoader godbcloader; |