]> git.ipfire.org Git - thirdparty/pdns.git/blob - modules/gpgsqlbackend/gpgsqlbackend.cc
API: allow writing to domains.account field
[thirdparty/pdns.git] / modules / gpgsqlbackend / gpgsqlbackend.cc
1 #include <string>
2 #include <map>
3 #include "pdns/namespaces.hh"
4 #include "pdns/dns.hh"
5 #include "pdns/dnsbackend.hh"
6 #include "pdns/dnspacket.hh"
7 #include "pdns/ueberbackend.hh"
8 #include "pdns/pdnsexception.hh"
9 #include "pdns/logger.hh"
10 #include "pdns/arguments.hh"
11 #include "gpgsqlbackend.hh"
12 #include "spgsql.hh"
13 #include <sstream>
14
15 gPgSQLBackend::gPgSQLBackend(const string &mode, const string &suffix) : GSQLBackend(mode,suffix)
16 {
17 try {
18 setDB(new SPgSQL(getArg("dbname"),
19 getArg("host"),
20 getArg("port"),
21 getArg("user"),
22 getArg("password")));
23 }
24
25 catch(SSqlException &e) {
26 L<<Logger::Error<<mode<<" Connection failed: "<<e.txtReason()<<endl;
27 throw PDNSException("Unable to launch "+mode+" connection: "+e.txtReason());
28 }
29 L<<Logger::Info<<mode<<" Connection successful. Connected to database '"<<getArg("dbname")<<"' on '"<<getArg("host")<<"'."<<endl;
30 }
31
32 class gPgSQLFactory : public BackendFactory
33 {
34 public:
35 gPgSQLFactory(const string &mode) : BackendFactory(mode),d_mode(mode) {}
36
37 void declareArguments(const string &suffix="")
38 {
39 declare(suffix,"dbname","Pdns backend database name to connect to","");
40 declare(suffix,"user","Pdns backend user to connect as","");
41 declare(suffix,"host","Pdns backend host to connect to","");
42 declare(suffix,"port","Database backend port to connect to","");
43 declare(suffix,"password","Pdns backend password to connect with","");
44
45 declare(suffix,"dnssec","Enable DNSSEC processing","no");
46
47 string record_query = "SELECT content,ttl,prio,type,domain_id,disabled::int,name,auth::int FROM records WHERE";
48
49 declare(suffix, "basic-query", "Basic query", record_query+" disabled=false and type=$1 and name=$2");
50 declare(suffix, "id-query", "Basic with ID query", record_query+" disabled=false and type=$1 and name=$2 and domain_id=$3");
51 declare(suffix, "any-query", "Any query", record_query+" disabled=false and name=$1");
52 declare(suffix, "any-id-query", "Any with ID query", record_query+" disabled=false and name=$1 and domain_id=$2");
53
54 declare(suffix, "list-query", "AXFR query", record_query+" (disabled=false OR $1) and domain_id=$2 order by name, type");
55 declare(suffix, "list-subzone-query", "Subzone listing", record_query+" disabled=false and (name=$1 OR name like $2) and domain_id=$3");
56
57 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");
58 declare(suffix, "insert-empty-non-terminal-query", "insert empty non-terminal in zone", "insert into records (domain_id,name,type,disabled,auth) values ($1,$2,null,false,true)");
59 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");
60
61 declare(suffix,"master-zone-query","Data", "select master from domains where name=$1 and type='SLAVE'");
62
63 declare(suffix,"info-zone-query","","select id,name,master,last_check,notified_serial,type,account from domains where name=$1");
64
65 declare(suffix,"info-all-slaves-query","","select id,name,master,last_check,type from domains where type='SLAVE'");
66 declare(suffix,"supermaster-query","", "select account from supermasters where ip=$1 and nameserver=$2");
67 declare(suffix,"supermaster-name-to-ips", "", "select ip,account from supermasters where nameserver=$1 and account=$2");
68
69 declare(suffix,"insert-zone-query","", "insert into domains (type,name,account) values('NATIVE',$1,$2)");
70 declare(suffix,"insert-slave-query","", "insert into domains (type,name,master,account) values('SLAVE',$1,$2,$3)");
71
72 declare(suffix, "insert-record-query", "", "insert into records (content,ttl,prio,type,domain_id,disabled,name,auth) values ($1,$2,$3,$4,$5,$6,$7,$8)");
73 declare(suffix, "insert-record-order-query", "", "insert into records (content,ttl,prio,type,domain_id,disabled,name,ordername,auth) values ($1,$2,$3,$4,$5,$6,$7,$8,$9)");
74 declare(suffix, "insert-ent-query", "insert empty non-terminal in zone", "insert into records (type,domain_id,disabled,name,auth) values (null,$1,false,$2,$3)");
75 declare(suffix, "insert-ent-order-query", "insert empty non-terminal in zone", "insert into records (type,domain_id,disabled,name,ordername,auth) values (null,$1,false,$2,$3,$4)");
76
77 declare(suffix, "get-order-first-query", "DNSSEC Ordering Query, last", "select ordername, name 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 declare(suffix, "set-order-and-auth-query", "DNSSEC set ordering query", "update records set ordername=$1,auth=$2 where name=$3 and domain_id=$4 and disabled=false");
82 declare(suffix, "set-auth-on-ds-record-query", "DNSSEC set auth on a DS record", "update records set auth=true where domain_id=$1 and name=$2 and type='DS' and disabled=false");
83
84 declare(suffix, "nullify-ordername-and-update-auth-query", "DNSSEC nullify ordername and update auth query", "update records set ordername=NULL,auth=$1 where domain_id=$2 and name=$3 and disabled=false");
85 declare(suffix, "nullify-ordername-and-auth-query", "DNSSEC nullify ordername and auth query", "update records set ordername=NULL,auth=false where name=$1 and type=$2 and domain_id=$3 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 }
122
123 DNSBackend *make(const string &suffix="")
124 {
125 return new gPgSQLBackend(d_mode,suffix);
126 }
127 private:
128 const string d_mode;
129 };
130
131
132 //! Magic class that is activated when the dynamic library is loaded
133 class gPgSQLLoader
134 {
135 public:
136 //! This reports us to the main UeberBackend class
137 gPgSQLLoader()
138 {
139 BackendMakers().report(new gPgSQLFactory("gpgsql"));
140 BackendMakers().report(new gPgSQLFactory("gpgsql2"));
141 L << Logger::Info << "[gpgsqlbackend] This is the gpgsql backend version " VERSION " (" __DATE__ ", " __TIME__ ") reporting" << endl;
142 }
143 };
144 static gPgSQLLoader gpgsqlloader;