]> git.ipfire.org Git - thirdparty/pdns.git/blob - modules/goraclebackend/goraclebackend.cc
auth: remove autoserial
[thirdparty/pdns.git] / modules / goraclebackend / goraclebackend.cc
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 */
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 #include <string>
26 #include <map>
27 #include "pdns/namespaces.hh"
28 #include "pdns/dns.hh"
29 #include "pdns/dnsbackend.hh"
30 #include "pdns/dnspacket.hh"
31 #include "pdns/pdnsexception.hh"
32 #include "pdns/logger.hh"
33 #include "pdns/arguments.hh"
34 #include "pdns/lock.hh"
35 #include "goraclebackend.hh"
36 #include "soracle.hh"
37 #include <sstream>
38
39 static OCIEnv* d_environmentHandle = 0;
40 static pthread_mutex_t s_goracle_lock=PTHREAD_MUTEX_INITIALIZER;
41
42 gOracleBackend::gOracleBackend(const string &mode, const string &suffix) : GSQLBackend(mode, suffix)
43 {
44 Lock gl(&s_goracle_lock);
45 if (d_environmentHandle == 0) {
46 setenv("ORACLE_HOME", getArg("home").c_str(), 1);
47 setenv("ORACLE_SID", getArg("sid").c_str(), 1);
48 setenv("NLS_LANG", getArg("nls-lang").c_str(), 1);
49
50 int err = OCIEnvCreate(&d_environmentHandle, OCI_THREADED, NULL, NULL, NULL, NULL, 0, NULL);
51
52 if (err) {
53 throw PDNSException("OCIEnvCreate failed");
54 }
55 }
56
57 try {
58 // set Oracle environment variables
59 setDB(new SOracle(getArg("tnsname"),
60 getArg("user"),
61 getArg("password"),
62 mustDo("release-statements"),
63 d_environmentHandle));
64 }
65
66 catch (SSqlException &e) {
67 g_log<<Logger::Error << mode << " Connection failed: " << e.txtReason() << endl;
68 throw PDNSException("Unable to launch " + mode + " connection: " + e.txtReason());
69 }
70 g_log<<Logger::Info << mode << " Connection successful" << endl;
71 }
72
73 class gOracleFactory : public BackendFactory
74 {
75 public:
76 gOracleFactory(const string &mode) : BackendFactory(mode), d_mode(mode) {}
77
78 void declareArguments(const string &suffix="") {
79 declare(suffix,"home", "Oracle home path", "");
80 declare(suffix,"sid", "Oracle sid", "XE");
81 declare(suffix,"nls-lang", "Oracle language", "AMERICAN_AMERICA.AL32UTF8");
82
83 declare(suffix,"tnsname","Generic Oracle backend TNSNAME to connect to","powerdns");
84 declare(suffix,"user","Database backend user to connect as","powerdns");
85 declare(suffix,"password","Pdns backend password to connect with","");
86
87 declare(suffix,"dnssec","Enable DNSSEC processing","no");
88 declare(suffix,"release-statements","Release statements between executions, uses less resources","no");
89
90 string record_query = "SELECT content,ttl,prio,type,domain_id,disabled,name,auth FROM records WHERE";
91
92 declare(suffix, "basic-query", "Basic query", record_query+" disabled=0 and type=:qtype and name=:qname");
93 declare(suffix, "id-query", "Basic with ID query", record_query+" disabled=0 and type=:qtype and name=:qname and domain_id=:domain_id");
94 declare(suffix, "any-query", "Any query", record_query+" disabled=0 and name=:qname");
95 declare(suffix, "any-id-query", "Any with ID query", record_query+" disabled=0 and name=:qname and domain_id=:domain_id");
96
97 declare(suffix, "list-query", "AXFR query", record_query+" (disabled=0 OR disabled=:include_disabled) and domain_id=:domain_id order by name, type");
98 declare(suffix, "list-subzone-query", "Subzone listing", record_query+" disabled=0 and (name=:zone OR name like :wildzone) and domain_id=:domain_id");
99
100 declare(suffix, "remove-empty-non-terminals-from-zone-query", "remove all empty non-terminals from zone", "delete from records where domain_id=:domain_id and type is null");
101 declare(suffix, "delete-empty-non-terminal-query", "delete empty non-terminal from zone", "delete from records where domain_id=:domain_id and name=:qname and type is null");
102
103 declare(suffix, "info-zone-query", "","select id,name,master,last_check,notified_serial,type,account from domains where name=:domain");
104
105 declare(suffix, "info-all-slaves-query", "","select id,name,master,last_check from domains where type='SLAVE'");
106 declare(suffix, "supermaster-query", "", "select account from supermasters where ip=:ip and nameserver=:nameserver");
107 declare(suffix, "supermaster-name-to-ips", "", "select ip,account from supermasters where nameserver=:nameserver and account=:account");
108 declare(suffix, "insert-zone-query", "", "insert into domains (id,type,name,master,account,last_check_notified_serial) values(domains_id_sequence.nextval,:type,:domain,:masters,:account, null, null)");
109 declare(suffix, "insert-record-query", "", "insert into records (id,content,ttl,prio,type,domain_id,disabled,name,ordername,auth) values (records_id_sequence.nextval,:content,:ttl,:priority,:qtype,:domain_id,:disabled,:qname,:ordername || ' ',:auth)");
110 declare(suffix, "insert-empty-non-terminal-order-query", "insert empty non-terminal in zone", "insert into records (id,type,domain_id,disabled,name,ordername,auth,ttl,prio,content) values (records_id_sequence.nextval,null,:domain_id,0,:qname,:ordername,:auth,null,null,null)");
111
112 declare(suffix, "get-order-first-query", "DNSSEC Ordering Query, first", "select * FROM (select trim(ordername) from records where disabled=0 and domain_id=:domain_id and ordername is not null order by ordername asc) where rownum=1");
113 declare(suffix, "get-order-before-query", "DNSSEC Ordering Query, before", "select * FROM (select trim(ordername), name from records where disabled=0 and ordername <= :ordername || ' ' and domain_id=:domain_id and ordername is not null order by ordername desc) where rownum=1");
114 declare(suffix, "get-order-after-query", "DNSSEC Ordering Query, after", "select trim(min(ordername)) from records where disabled=0 and ordername > :ordername || ' ' and domain_id=:domain_id and ordername is not null");
115 declare(suffix, "get-order-last-query", "DNSSEC Ordering Query, last", "select * from (select trim(ordername), name from records where disabled=0 and ordername != ' ' and domain_id=:domain_id and ordername is not null order by ordername desc) where rownum=1");
116
117 declare(suffix, "update-ordername-and-auth-query", "DNSSEC update ordername and auth for a qname query", "update records set ordername=:ordername || ' ',auth=:auth where domain_id=:domain_id and name=:qname and disabled=0");
118 declare(suffix, "update-ordername-and-auth-type-query", "DNSSEC update ordername and auth for a rrset query", "update records set ordername=:ordername || ' ',auth=:auth where domain_id=:domain_id and name=:qname and type=:qtype and disabled=0");
119 declare(suffix, "nullify-ordername-and-update-auth-query", "DNSSEC nullify ordername and update auth for a qname query", "update records set ordername=NULL,auth=:auth where domain_id=:domain_id and name=:qname and disabled=0");
120 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=:auth where domain_id=:domain_id and name=:qname and type=:qtype and disabled=0");
121
122 declare(suffix, "update-master-query", "", "update domains set master=:master where name=:domain");
123 declare(suffix, "update-kind-query", "", "update domains set type=:kind where name=:domain");
124 declare(suffix, "update-account-query", "", "update domains set account=:account where name=:domain");
125 declare(suffix, "update-serial-query", "", "update domains set notified_serial=:serial where id=:domain_id");
126 declare(suffix, "update-lastcheck-query", "", "update domains set last_check=:last_check where id=:domain_id");
127 declare(suffix, "info-all-master-query", "", "select id,name,master,last_check,notified_serial,type from domains where type='MASTER'");
128 declare(suffix, "delete-domain-query","", "delete from domains where name=:domain");
129 declare(suffix, "delete-zone-query", "", "delete from records where domain_id=:domain_id");
130 declare(suffix, "delete-rrset-query", "", "delete from records where domain_id=:domain_id and name=:qname and type=:qtype");
131 declare(suffix, "delete-names-query", "", "delete from records where domain_id=:domain_id and name=:qname");
132
133 declare(suffix, "add-domain-key-query","", "insert into cryptokeys (id, domain_id, flags, active, content) select cryptokeys_id_sequence.nextval, id, :flags,:active, :content from domains where name=:domain");
134 declare(suffix, "get-last-inserted-key-id-query", "", "select cryptokeys_id_sequence.currval from DUAL");
135 declare(suffix, "list-domain-keys-query","", "select cryptokeys.id, flags, active, content from domains, cryptokeys where cryptokeys.domain_id=domains.id and name=:domain");
136 declare(suffix, "get-all-domain-metadata-query","", "select kind,content from domains, domainmetadata where domainmetadata.domain_id=domains.id and name=:domain");
137 declare(suffix, "get-domain-metadata-query","", "select content from domains, domainmetadata where domainmetadata.domain_id=domains.id and name=:domain and domainmetadata.kind=:kind");
138 declare(suffix, "clear-domain-metadata-query","", "delete from domainmetadata where domain_id=(select id from domains where name=:domain) and domainmetadata.kind=:kind");
139 declare(suffix, "clear-domain-all-metadata-query","", "delete from domainmetadata where domain_id=(select id from domains where name=:domain)");
140 declare(suffix, "set-domain-metadata-query","", "insert into domainmetadata (id, domain_id, kind, content) select domainmetadata_id_sequence.nextval, id, :kind, :content from domains where name=:domain");
141 declare(suffix, "activate-domain-key-query","", "update cryptokeys set active=1 where domain_id=(select id from domains where name=:domain) and cryptokeys.id=:key_id");
142 declare(suffix, "deactivate-domain-key-query","", "update cryptokeys set active=0 where domain_id=(select id from domains where name=:domain) and cryptokeys.id=:key_id");
143 declare(suffix, "remove-domain-key-query","", "delete from cryptokeys where domain_id=(select id from domains where name=:domain) and cryptokeys.id=:key_id");
144 declare(suffix, "clear-domain-all-keys-query","", "delete from cryptokeys where domain_id=(select id from domains where name=:domain)");
145 declare(suffix, "get-tsig-key-query","", "select algorithm, secret from tsigkeys where name=:key_name");
146 declare(suffix, "set-tsig-key-query","", "merge into tsigkeys tk using dual on (name = :key_name and algorithm = :algorithm) when not matched then insert (id, name, algorithm, secret) values(tsigkeys_id_sequence.nextval, :key_name, :algorithm, :content) when matched then update set secret = :content");
147 declare(suffix, "delete-tsig-key-query","", "delete from tsigkeys where name=:key_name");
148 declare(suffix, "get-tsig-keys-query","", "select name,algorithm, secret from tsigkeys");
149
150 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, domain.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=:include_disabled");
151
152 declare(suffix, "list-comments-query", "", "SELECT domain_id,name,type,modified_at,account,\"comment\" FROM comments WHERE domain_id=:domain_id");
153 declare(suffix, "insert-comment-query", "", "INSERT INTO comments (id, domain_id, name, type, modified_at, account, \"comment\") VALUES (comments_id_sequence.nextval, :domain_id, :qname, :qtype, :modified_at, :account, :content)");
154 declare(suffix, "delete-comment-rrset-query", "", "DELETE FROM comments WHERE domain_id=:domain_id AND name=:qname AND type=:qtype");
155 declare(suffix, "delete-comments-query", "", "DELETE FROM comments WHERE domain_id=:domain_id");
156 declare(suffix, "search-records-query", "", record_query+" name LIKE :value OR content LIKE :value2 LIMIT :limit");
157 declare(suffix, "search-comments-query", "", "SELECT domain_id,name,type,modified_at,account,comment FROM comments WHERE name LIKE :value OR comment LIKE :value2 LIMIT :limit");
158
159 }
160
161 DNSBackend* make(const string &suffix="") {
162 return new gOracleBackend(d_mode,suffix);
163 }
164
165 private:
166 const string d_mode;
167 };
168
169
170 //! Magic class that is activated when the dynamic library is loaded
171 class gOracleLoader
172 {
173 public:
174 //! This reports us to the main UeberBackend class
175 gOracleLoader() {
176 BackendMakers().report(new gOracleFactory("goracle"));
177 g_log << Logger::Info << "[goraclebackend] This is the goracle backend version " VERSION
178 #ifndef REPRODUCIBLE
179 << " (" __DATE__ " " __TIME__ ")"
180 #endif
181 << " reporting" << endl;
182 }
183 };
184
185 //! Reports the backendloader to the UeberBackend.
186 static gOracleLoader goracleloader;