]> git.ipfire.org Git - thirdparty/pdns.git/blob - modules/gpgsqlbackend/gpgsqlbackend.cc
9cb993a717c6d3676d8303ac58584d9a76e3789b
[thirdparty/pdns.git] / modules / gpgsqlbackend / gpgsqlbackend.cc
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4 #include <string>
5 #include <map>
6 #include "pdns/namespaces.hh"
7 #include "pdns/dns.hh"
8 #include "pdns/dnsbackend.hh"
9 #include "pdns/dnspacket.hh"
10 #include "pdns/ueberbackend.hh"
11 #include "pdns/pdnsexception.hh"
12 #include "pdns/logger.hh"
13 #include "pdns/arguments.hh"
14 #include "gpgsqlbackend.hh"
15 #include "spgsql.hh"
16 #include <sys/time.h>
17 #include <sstream>
18
19 gPgSQLBackend::gPgSQLBackend(const string &mode, const string &suffix) : GSQLBackend(mode,suffix)
20 {
21 try {
22 setDB(new SPgSQL(getArg("dbname"),
23 getArg("host"),
24 getArg("port"),
25 getArg("user"),
26 getArg("password")));
27 }
28
29 catch(SSqlException &e) {
30 L<<Logger::Error<<mode<<" Connection failed: "<<e.txtReason()<<endl;
31 throw PDNSException("Unable to launch "+mode+" connection: "+e.txtReason());
32 }
33 L<<Logger::Info<<mode<<" Connection successful. Connected to database '"<<getArg("dbname")<<"' on '"<<getArg("host")<<"'."<<endl;
34 }
35
36 class gPgSQLFactory : public BackendFactory
37 {
38 public:
39 gPgSQLFactory(const string &mode) : BackendFactory(mode),d_mode(mode) {}
40
41 void declareArguments(const string &suffix="")
42 {
43 declare(suffix,"dbname","Pdns backend database name to connect to","");
44 declare(suffix,"user","Pdns backend user to connect as","");
45 declare(suffix,"host","Pdns backend host to connect to","");
46 declare(suffix,"port","Database backend port to connect to","");
47 declare(suffix,"password","Pdns backend password to connect with","");
48
49 declare(suffix,"dnssec","Enable DNSSEC processing","no");
50
51 string record_query = "SELECT content,ttl,prio,type,domain_id,disabled::int,name,auth::int FROM records WHERE";
52
53 declare(suffix, "basic-query", "Basic query", record_query+" disabled=false and type=$1 and name=$2");
54 declare(suffix, "id-query", "Basic with ID query", record_query+" disabled=false and type=$1 and name=$2 and domain_id=$3");
55 declare(suffix, "any-query", "Any query", record_query+" disabled=false and name=$1");
56 declare(suffix, "any-id-query", "Any with ID query", record_query+" disabled=false and name=$1 and domain_id=$2");
57
58 declare(suffix, "list-query", "AXFR query", record_query+" (disabled=false OR $1) and domain_id=$2 order by name, type");
59 declare(suffix, "list-subzone-query", "Subzone listing", record_query+" disabled=false and (name=$1 OR name like $2) and domain_id=$3");
60
61 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");
62 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");
63
64 declare(suffix,"master-zone-query","Data", "select master from domains where name=$1 and type='SLAVE'");
65
66 declare(suffix,"info-zone-query","","select id,name,master,last_check,notified_serial,type,account from domains where name=$1");
67
68 declare(suffix,"info-all-slaves-query","","select id,name,master,last_check from domains where type='SLAVE'");
69 declare(suffix,"supermaster-query","", "select account from supermasters where ip=$1 and nameserver=$2");
70 declare(suffix,"supermaster-name-to-ips", "", "select ip,account from supermasters where nameserver=$1 and account=$2");
71
72 declare(suffix,"insert-zone-query","", "insert into domains (type,name,master,account,last_check, notified_serial) values($1,$2,$3,$4,null,null)");
73
74 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)");
75 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)");
76
77 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");
78 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");
79 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");
80 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");
81
82 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");
83 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");
84 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");
85 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");
86
87 declare(suffix,"update-master-query","", "update domains set master=$1 where name=$2");
88 declare(suffix,"update-kind-query","", "update domains set type=$1 where name=$2");
89 declare(suffix,"update-account-query","", "update domains set account=$1 where name=$2");
90 declare(suffix,"update-serial-query","", "update domains set notified_serial=$1 where id=$2");
91 declare(suffix,"update-lastcheck-query","", "update domains set last_check=$1 where id=$2");
92 declare(suffix,"zone-lastchange-query", "", "select max(change_date) from records where domain_id=$1");
93 declare(suffix,"info-all-master-query","", "select id,name,master,last_check,notified_serial,type from domains where type='MASTER'");
94 declare(suffix,"delete-domain-query","", "delete from domains where name=$1");
95 declare(suffix,"delete-zone-query","", "delete from records where domain_id=$1");
96 declare(suffix,"delete-rrset-query","","delete from records where domain_id=$1 and name=$2 and type=$3");
97 declare(suffix,"delete-names-query","","delete from records where domain_id=$1 and name=$2");
98
99 declare(suffix,"add-domain-key-query","", "insert into cryptokeys (domain_id, flags, active, content) select id, $1, $2, $3 from domains where name=$4");
100 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");
101 declare(suffix,"get-all-domain-metadata-query","", "select kind,content from domains, domainmetadata where domainmetadata.domain_id=domains.id and name=$1");
102 declare(suffix,"get-domain-metadata-query","", "select content from domains, domainmetadata where domainmetadata.domain_id=domains.id and name=$1 and domainmetadata.kind=$2");
103 declare(suffix,"clear-domain-metadata-query","", "delete from domainmetadata where domain_id=(select id from domains where name=$1) and domainmetadata.kind=$2");
104 declare(suffix,"clear-domain-all-metadata-query","", "delete from domainmetadata where domain_id=(select id from domains where name=$1)");
105 declare(suffix,"set-domain-metadata-query","", "insert into domainmetadata (domain_id, kind, content) select id, $1, $2 from domains where name=$3");
106 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");
107 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");
108 declare(suffix,"remove-domain-key-query","", "delete from cryptokeys where domain_id=(select id from domains where name=$1) and cryptokeys.id=$2");
109 declare(suffix,"clear-domain-all-keys-query","", "delete from cryptokeys where domain_id=(select id from domains where name=$1)");
110 declare(suffix,"get-tsig-key-query","", "select algorithm, secret from tsigkeys where name=$1");
111 declare(suffix,"set-tsig-key-query","", "insert into tsigkeys (name,algorithm,secret) values($1,$2,$3)");
112 declare(suffix,"delete-tsig-key-query","", "delete from tsigkeys where name=$1");
113 declare(suffix,"get-tsig-keys-query","", "select name,algorithm, secret from tsigkeys");
114
115 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");
116
117 declare(suffix, "list-comments-query", "", "SELECT domain_id,name,type,modified_at,account,comment FROM comments WHERE domain_id=$1");
118 declare(suffix, "insert-comment-query", "", "INSERT INTO comments (domain_id, name, type, modified_at, account, comment) VALUES ($1, $2, $3, $4, $5, $6)");
119 declare(suffix, "delete-comment-rrset-query", "", "DELETE FROM comments WHERE domain_id=$1 AND name=$2 AND type=$3");
120 declare(suffix, "delete-comments-query", "", "DELETE FROM comments WHERE domain_id=$1");
121 declare(suffix, "search-records-query", "", record_query+" name LIKE $1 OR content LIKE $2 LIMIT $3");
122 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");
123
124 }
125
126 DNSBackend *make(const string &suffix="")
127 {
128 return new gPgSQLBackend(d_mode,suffix);
129 }
130 private:
131 const string d_mode;
132 };
133
134
135 //! Magic class that is activated when the dynamic library is loaded
136 class gPgSQLLoader
137 {
138 public:
139 //! This reports us to the main UeberBackend class
140 gPgSQLLoader()
141 {
142 BackendMakers().report(new gPgSQLFactory("gpgsql"));
143 L << Logger::Info << "[gpgsqlbackend] This is the gpgsql backend version " VERSION
144 #ifndef REPRODUCIBLE
145 << " (" __DATE__ " " __TIME__ ")"
146 #endif
147 << " reporting" << endl;
148 }
149 };
150 static gPgSQLLoader gpgsqlloader;