2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
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.
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.
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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "pdns/namespaces.hh"
28 #include "pdns/dns.hh"
29 #include "pdns/dnsbackend.hh"
30 #include "pdns/dnspacket.hh"
31 #include "pdns/ueberbackend.hh"
32 #include "pdns/pdnsexception.hh"
33 #include "pdns/logger.hh"
34 #include "pdns/arguments.hh"
35 #include "gpgsqlbackend.hh"
40 gPgSQLBackend::gPgSQLBackend(const string
&mode
, const string
&suffix
) : GSQLBackend(mode
,suffix
)
43 setDB(new SPgSQL(getArg("dbname"),
50 catch(SSqlException
&e
) {
51 L
<<Logger::Error
<<mode
<<" Connection failed: "<<e
.txtReason()<<endl
;
52 throw PDNSException("Unable to launch "+mode
+" connection: "+e
.txtReason());
54 L
<<Logger::Info
<<mode
<<" Connection successful. Connected to database '"<<getArg("dbname")<<"' on '"<<getArg("host")<<"'."<<endl
;
57 class gPgSQLFactory
: public BackendFactory
60 gPgSQLFactory(const string
&mode
) : BackendFactory(mode
),d_mode(mode
) {}
62 void declareArguments(const string
&suffix
="")
64 declare(suffix
,"dbname","Pdns backend database name to connect to","");
65 declare(suffix
,"user","Pdns backend user to connect as","");
66 declare(suffix
,"host","Pdns backend host to connect to","");
67 declare(suffix
,"port","Database backend port to connect to","");
68 declare(suffix
,"password","Pdns backend password to connect with","");
70 declare(suffix
,"dnssec","Enable DNSSEC processing","no");
72 string record_query
= "SELECT content,ttl,prio,type,domain_id,disabled::int,name,auth::int FROM records WHERE";
74 declare(suffix
, "basic-query", "Basic query", record_query
+" disabled=false and type=$1 and name=$2");
75 declare(suffix
, "id-query", "Basic with ID query", record_query
+" disabled=false and type=$1 and name=$2 and domain_id=$3");
76 declare(suffix
, "any-query", "Any query", record_query
+" disabled=false and name=$1");
77 declare(suffix
, "any-id-query", "Any with ID query", record_query
+" disabled=false and name=$1 and domain_id=$2");
79 declare(suffix
, "list-query", "AXFR query", record_query
+" (disabled=false OR $1) and domain_id=$2 order by name, type");
80 declare(suffix
, "list-subzone-query", "Subzone listing", record_query
+" disabled=false and (name=$1 OR name like $2) and domain_id=$3");
82 declare(suffix
,"remove-empty-non-terminals-from-zone-query", "remove all empty non-terminals from zone", "delete from records where domain_id=$1 and type is null");
83 declare(suffix
,"delete-empty-non-terminal-query", "delete empty non-terminal from zone", "delete from records where domain_id=$1 and name=$2 and type is null");
85 declare(suffix
,"master-zone-query","Data", "select master from domains where name=$1 and type='SLAVE'");
87 declare(suffix
,"info-zone-query","","select id,name,master,last_check,notified_serial,type,account from domains where name=$1");
89 declare(suffix
,"info-all-slaves-query","","select id,name,master,last_check from domains where type='SLAVE'");
90 declare(suffix
,"supermaster-query","", "select account from supermasters where ip=$1 and nameserver=$2");
91 declare(suffix
,"supermaster-name-to-ips", "", "select ip,account from supermasters where nameserver=$1 and account=$2");
93 declare(suffix
,"insert-zone-query","", "insert into domains (type,name,master,account,last_check, notified_serial) values($1,$2,$3,$4,null,null)");
95 declare(suffix
, "insert-record-query", "", "insert into records (content,ttl,prio,type,domain_id,disabled,name,ordername,auth,change_date) values ($1,$2,$3,$4,$5,$6,$7,$8,$9,null)");
96 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,$1,false,$2,$3,$4,null,null,null,null)");
98 declare(suffix
, "get-order-first-query", "DNSSEC Ordering Query, last", "select ordername from records where disabled=false and domain_id=$1 and ordername is not null order by 1 using ~<~ limit 1");
99 declare(suffix
, "get-order-before-query", "DNSSEC Ordering Query, before", "select ordername, name from records where disabled=false and ordername ~<=~ $1 and domain_id=$2 and ordername is not null order by 1 using ~>~ limit 1");
100 declare(suffix
, "get-order-after-query", "DNSSEC Ordering Query, after", "select ordername from records where disabled=false and ordername ~>~ $1 and domain_id=$2 and ordername is not null order by 1 using ~<~ limit 1");
101 declare(suffix
, "get-order-last-query", "DNSSEC Ordering Query, last", "select ordername, name from records where disabled=false and ordername != '' and domain_id=$1 and ordername is not null order by 1 using ~>~ limit 1");
103 declare(suffix
, "update-ordername-and-auth-query", "DNSSEC update ordername and auth for a qname query", "update records set ordername=$1,auth=$2 where domain_id=$3 and name=$4 and disabled=false");
104 declare(suffix
, "update-ordername-and-auth-type-query", "DNSSEC update ordername and auth for a rrset query", "update records set ordername=$1,auth=$2 where domain_id=$3 and name=$4 and type=$5 and disabled=false");
105 declare(suffix
, "nullify-ordername-and-update-auth-query", "DNSSEC nullify ordername and update auth for a qname query", "update records set ordername=NULL,auth=$1 where domain_id=$2 and name=$3 and disabled=false");
106 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=$1 where domain_id=$2 and name=$3 and type=$4 and disabled=false");
108 declare(suffix
,"update-master-query","", "update domains set master=$1 where name=$2");
109 declare(suffix
,"update-kind-query","", "update domains set type=$1 where name=$2");
110 declare(suffix
,"update-account-query","", "update domains set account=$1 where name=$2");
111 declare(suffix
,"update-serial-query","", "update domains set notified_serial=$1 where id=$2");
112 declare(suffix
,"update-lastcheck-query","", "update domains set last_check=$1 where id=$2");
113 declare(suffix
,"zone-lastchange-query", "", "select max(change_date) from records where domain_id=$1");
114 declare(suffix
,"info-all-master-query","", "select id,name,master,last_check,notified_serial,type from domains where type='MASTER'");
115 declare(suffix
,"delete-domain-query","", "delete from domains where name=$1");
116 declare(suffix
,"delete-zone-query","", "delete from records where domain_id=$1");
117 declare(suffix
,"delete-rrset-query","","delete from records where domain_id=$1 and name=$2 and type=$3");
118 declare(suffix
,"delete-names-query","","delete from records where domain_id=$1 and name=$2");
120 declare(suffix
,"add-domain-key-query","", "insert into cryptokeys (domain_id, flags, active, content) select id, $1, $2, $3 from domains where name=$4");
121 declare(suffix
,"list-domain-keys-query","", "select cryptokeys.id, flags, case when active then 1 else 0 end as active, content from domains, cryptokeys where cryptokeys.domain_id=domains.id and name=$1");
122 declare(suffix
,"get-all-domain-metadata-query","", "select kind,content from domains, domainmetadata where domainmetadata.domain_id=domains.id and name=$1");
123 declare(suffix
,"get-domain-metadata-query","", "select content from domains, domainmetadata where domainmetadata.domain_id=domains.id and name=$1 and domainmetadata.kind=$2");
124 declare(suffix
,"clear-domain-metadata-query","", "delete from domainmetadata where domain_id=(select id from domains where name=$1) and domainmetadata.kind=$2");
125 declare(suffix
,"clear-domain-all-metadata-query","", "delete from domainmetadata where domain_id=(select id from domains where name=$1)");
126 declare(suffix
,"set-domain-metadata-query","", "insert into domainmetadata (domain_id, kind, content) select id, $1, $2 from domains where name=$3");
127 declare(suffix
,"activate-domain-key-query","", "update cryptokeys set active=true where domain_id=(select id from domains where name=$1) and cryptokeys.id=$2");
128 declare(suffix
,"deactivate-domain-key-query","", "update cryptokeys set active=false where domain_id=(select id from domains where name=$1) and cryptokeys.id=$2");
129 declare(suffix
,"remove-domain-key-query","", "delete from cryptokeys where domain_id=(select id from domains where name=$1) and cryptokeys.id=$2");
130 declare(suffix
,"clear-domain-all-keys-query","", "delete from cryptokeys where domain_id=(select id from domains where name=$1)");
131 declare(suffix
,"get-tsig-key-query","", "select algorithm, secret from tsigkeys where name=$1");
132 declare(suffix
,"set-tsig-key-query","", "insert into tsigkeys (name,algorithm,secret) values($1,$2,$3)");
133 declare(suffix
,"delete-tsig-key-query","", "delete from tsigkeys where name=$1");
134 declare(suffix
,"get-tsig-keys-query","", "select name,algorithm, secret from tsigkeys");
136 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=false OR $1");
138 declare(suffix
, "list-comments-query", "", "SELECT domain_id,name,type,modified_at,account,comment FROM comments WHERE domain_id=$1");
139 declare(suffix
, "insert-comment-query", "", "INSERT INTO comments (domain_id, name, type, modified_at, account, comment) VALUES ($1, $2, $3, $4, $5, $6)");
140 declare(suffix
, "delete-comment-rrset-query", "", "DELETE FROM comments WHERE domain_id=$1 AND name=$2 AND type=$3");
141 declare(suffix
, "delete-comments-query", "", "DELETE FROM comments WHERE domain_id=$1");
142 declare(suffix
, "search-records-query", "", record_query
+" name LIKE $1 OR content LIKE $2 LIMIT $3");
143 declare(suffix
, "search-comments-query", "", "SELECT domain_id,name,type,modified_at,account,comment FROM comments WHERE name LIKE $1 OR comment LIKE $2 LIMIT $3");
147 DNSBackend
*make(const string
&suffix
="")
149 return new gPgSQLBackend(d_mode
,suffix
);
156 //! Magic class that is activated when the dynamic library is loaded
160 //! This reports us to the main UeberBackend class
163 BackendMakers().report(new gPgSQLFactory("gpgsql"));
164 L
<< Logger::Info
<< "[gpgsqlbackend] This is the gpgsql backend version " VERSION
166 << " (" __DATE__
" " __TIME__
")"
168 << " reporting" << endl
;
171 static gPgSQLLoader gpgsqlloader
;