| 1 | #include <string> |
|---|
| 2 | #include <map> |
|---|
| 3 | |
|---|
| 4 | using namespace std; |
|---|
| 5 | |
|---|
| 6 | #include "pdns/dns.hh" |
|---|
| 7 | #include "pdns/dnsbackend.hh" |
|---|
| 8 | #include "gpgsqlbackend.hh" |
|---|
| 9 | #include "pdns/dnspacket.hh" |
|---|
| 10 | #include "pdns/ueberbackend.hh" |
|---|
| 11 | #include "pdns/ahuexception.hh" |
|---|
| 12 | #include "pdns/logger.hh" |
|---|
| 13 | #include "pdns/arguments.hh" |
|---|
| 14 | |
|---|
| 15 | #include "spgsql.hh" |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | #include <sstream> |
|---|
| 19 | |
|---|
| 20 | gPgSQLBackend::gPgSQLBackend(const string &mode, const string &suffix) : GSQLBackend(mode,suffix) |
|---|
| 21 | { |
|---|
| 22 | try { |
|---|
| 23 | setDB(new SPgSQL(getArg("dbname"), |
|---|
| 24 | getArg("host"), |
|---|
| 25 | getArg("port"), |
|---|
| 26 | getArg("socket"), |
|---|
| 27 | getArg("user"), |
|---|
| 28 | getArg("password"))); |
|---|
| 29 | |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | catch(SSqlException &e) { |
|---|
| 33 | L<<Logger::Error<<mode<<" Connection failed: "<<e.txtReason()<<endl; |
|---|
| 34 | throw AhuException("Unable to launch "+mode+" connection: "+e.txtReason()); |
|---|
| 35 | } |
|---|
| 36 | L<<Logger::Warning<<mode<<" Connection succesful"<<endl; |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | class gPgSQLFactory : public BackendFactory |
|---|
| 40 | { |
|---|
| 41 | public: |
|---|
| 42 | gPgSQLFactory(const string &mode) : BackendFactory(mode),d_mode(mode) {} |
|---|
| 43 | |
|---|
| 44 | // XXX FIXME this stuff is duplicate with gmysqlbackend |
|---|
| 45 | void declareArguments(const string &suffix="") |
|---|
| 46 | { |
|---|
| 47 | declare(suffix,"dbname","Pdns backend database name to connect to","powerdns"); |
|---|
| 48 | declare(suffix,"user","Pdns backend user to connect as","powerdns"); |
|---|
| 49 | declare(suffix,"host","Pdns backend host to connect to",""); |
|---|
| 50 | declare(suffix,"port","Database backend port to connect to",""); |
|---|
| 51 | declare(suffix,"socket","Pdns backend socket to connect to",""); |
|---|
| 52 | declare(suffix,"password","Pdns backend password to connect with",""); |
|---|
| 53 | |
|---|
| 54 | declare(suffix,"basic-query","Basic query","select content,ttl,prio,type,domain_id,name from records where type='%s' and name='%s'"); |
|---|
| 55 | declare(suffix,"id-query","Basic with ID query","select content,ttl,prio,type,domain_id,name from records where type='%s' and name='%s' and domain_id=%d"); |
|---|
| 56 | declare(suffix,"wildcard-query","Wildcard query","select content,ttl,prio,type,domain_id,name from records where type='%s' and name like '%s'"); |
|---|
| 57 | declare(suffix,"wildcard-id-query","Wildcard with ID query","select content,ttl,prio,type,domain_id,name from records where type='%s' and name like '%s' and domain_id='%d'"); |
|---|
| 58 | |
|---|
| 59 | declare(suffix,"any-query","Any query","select content,ttl,prio,type,domain_id,name from records where name='%s'"); |
|---|
| 60 | declare(suffix,"any-id-query","Any with ID query","select content,ttl,prio,type,domain_id,name from records where name='%s' and domain_id=%d"); |
|---|
| 61 | declare(suffix,"wildcard-any-query","Wildcard ANY query","select content,ttl,prio,type,domain_id,name from records where name like '%s'"); |
|---|
| 62 | declare(suffix,"wildcard-any-id-query","Wildcard ANY with ID query","select content,ttl,prio,type,domain_id,name from records where name like '%s' and domain_id='%d'"); |
|---|
| 63 | |
|---|
| 64 | declare(suffix,"list-query","AXFR query", "select content,ttl,prio,type,domain_id,name from records where domain_id='%d'"); |
|---|
| 65 | declare(suffix,"master-zone-query","Data", "select master from domains where name='%s' and type='SLAVE'"); |
|---|
| 66 | |
|---|
| 67 | declare(suffix,"info-zone-query","","select id,name,master,last_check,notified_serial,type from domains where name='%s'"); |
|---|
| 68 | |
|---|
| 69 | declare(suffix,"info-all-slaves-query","","select id,name,master,last_check,type from domains where type='SLAVE'"); |
|---|
| 70 | declare(suffix,"supermaster-query","", "select account from supermasters where ip='%s' and nameserver='%s'"); |
|---|
| 71 | declare(suffix,"insert-slave-query","", "insert into domains (type,name,master,account) values('SLAVE','%s','%s','%s')"); |
|---|
| 72 | declare(suffix,"insert-record-query","", "insert into records (content,ttl,prio,type,domain_id,name) values ('%s',%d,%d,'%s',%d,'%s')"); |
|---|
| 73 | declare(suffix,"update-serial-query","", "update domains set notified_serial=%d where id=%d"); |
|---|
| 74 | declare(suffix,"update-lastcheck-query","", "update domains set last_check=%d where id=%d"); |
|---|
| 75 | declare(suffix,"info-all-master-query","", "select id,name,master,last_check,notified_serial,type from domains where type='MASTER'"); |
|---|
| 76 | declare(suffix,"delete-zone-query","", "delete from records where domain_id=%d"); |
|---|
| 77 | declare(suffix,"check-acl-query","", "select value from acls where acl_type='%s' and acl_key='%s'"); |
|---|
| 78 | |
|---|
| 79 | declare(suffix,"add-domain-key-query","", "insert into cryptokeys (domain_id, flags, active, content) select id, %d, %d, '%s' from domains where name='%s'"); |
|---|
| 80 | declare(suffix,"list-domain-keys-query","", "select cryptokeys.id, flags, active, content from domains, cryptokeys where domain_id=domains.id and name='%s'"); |
|---|
| 81 | declare(suffix,"get-domain-metadata-query","", "select content from domains, domainmetadata where domain_id=domains.id and name='%s' and domainmetadata.kind='%s'"); |
|---|
| 82 | declare(suffix,"clear-domain-metadata-query","", "delete from domainmetadata where domain_id=(select id from domains where name='%s') and domainmetadata.kind='%s'"); |
|---|
| 83 | declare(suffix,"set-domain-metadata-query","", "insert into domainmetadata (domain_id, kind, content) select id, '%s', '%s' from domains where name='%s'"); |
|---|
| 84 | declare(suffix,"activate-domain-key-query","", "update cryptokeys set active=1 where domain_id=(select id from domains where name='%s') and cryptokeys.id=%d"); |
|---|
| 85 | declare(suffix,"deactivate-domain-key-query","", "update cryptokeys set active=0 where domain_id=(select id from domains where name='%s') and cryptokeys.id=%d"); |
|---|
| 86 | declare(suffix,"remove-domain-key-query","", "delete from cryptokeys where domain_id=(select id from domains where name='%s') and cryptokeys.id=%d"); |
|---|
| 87 | |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | DNSBackend *make(const string &suffix="") |
|---|
| 91 | { |
|---|
| 92 | return new gPgSQLBackend(d_mode,suffix); |
|---|
| 93 | } |
|---|
| 94 | private: |
|---|
| 95 | const string d_mode; |
|---|
| 96 | }; |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | //! Magic class that is activated when the dynamic library is loaded |
|---|
| 100 | class gPgSQLLoader |
|---|
| 101 | { |
|---|
| 102 | public: |
|---|
| 103 | //! This reports us to the main UeberBackend class |
|---|
| 104 | gPgSQLLoader() |
|---|
| 105 | { |
|---|
| 106 | BackendMakers().report(new gPgSQLFactory("gpgsql")); |
|---|
| 107 | BackendMakers().report(new gPgSQLFactory("gpgsql2")); |
|---|
| 108 | L<<Logger::Warning<<"This is module gpgsqlbackend.so reporting"<<endl; |
|---|
| 109 | } |
|---|
| 110 | }; |
|---|
| 111 | static gPgSQLLoader gpgsqlloader; |
|---|