From 79532aa7e793c5219adb2880576a1d27c2d802f2 Mon Sep 17 00:00:00 2001 From: Christian Hofstaedtler Date: Sat, 7 Feb 2015 15:20:42 +0100 Subject: [PATCH] API: allow writing to domains.account field --- modules/gmysqlbackend/gmysqlbackend.cc | 3 ++- modules/gpgsqlbackend/gpgsqlbackend.cc | 3 ++- modules/gsqlite3backend/gsqlite3backend.cc | 3 ++- pdns/backends/gsql/gsqlbackend.cc | 17 +++++++++++++++++ pdns/backends/gsql/gsqlbackend.hh | 7 ++++++- pdns/dnsbackend.hh | 6 ++++++ pdns/ws-auth.cc | 3 +++ regression-tests.api/test_Zones.py | 9 +++++++++ 8 files changed, 47 insertions(+), 4 deletions(-) diff --git a/modules/gmysqlbackend/gmysqlbackend.cc b/modules/gmysqlbackend/gmysqlbackend.cc index 6320c3170..793ade9af 100644 --- a/modules/gmysqlbackend/gmysqlbackend.cc +++ b/modules/gmysqlbackend/gmysqlbackend.cc @@ -72,7 +72,7 @@ public: declare(suffix,"supermaster-query","", "select account from supermasters where ip=? and nameserver=?"); declare(suffix,"supermaster-name-to-ips", "", "select ip,account from supermasters where nameserver=? and account=?"); - declare(suffix,"insert-zone-query","", "insert into domains (type,name) values('NATIVE',?)"); + declare(suffix,"insert-zone-query","", "insert into domains (type,name,account) values('NATIVE',?,?)"); declare(suffix,"insert-slave-query","", "insert into domains (type,name,master,account) values('SLAVE',?,?,?)"); declare(suffix, "insert-record-query", "", "insert into records (content,ttl,prio,type,domain_id,disabled,name,auth) values (?,?,?,?,?,?,?,?)"); @@ -92,6 +92,7 @@ public: declare(suffix,"update-master-query","", "update domains set master=? where name=?"); declare(suffix,"update-kind-query","", "update domains set type=? where name=?"); + declare(suffix,"update-account-query","", "update domains set account=? where name=?"); declare(suffix,"update-serial-query","", "update domains set notified_serial=? where id=?"); declare(suffix,"update-lastcheck-query","", "update domains set last_check=? where id=?"); declare(suffix,"zone-lastchange-query", "", "select max(change_date) from records where domain_id=?"); diff --git a/modules/gpgsqlbackend/gpgsqlbackend.cc b/modules/gpgsqlbackend/gpgsqlbackend.cc index 25d65e8d3..74b6e0518 100644 --- a/modules/gpgsqlbackend/gpgsqlbackend.cc +++ b/modules/gpgsqlbackend/gpgsqlbackend.cc @@ -66,7 +66,7 @@ public: declare(suffix,"supermaster-query","", "select account from supermasters where ip=$1 and nameserver=$2"); declare(suffix,"supermaster-name-to-ips", "", "select ip,account from supermasters where nameserver=$1 and account=$2"); - declare(suffix,"insert-zone-query","", "insert into domains (type,name) values('NATIVE',$1)"); + declare(suffix,"insert-zone-query","", "insert into domains (type,name,account) values('NATIVE',$1,$2)"); declare(suffix,"insert-slave-query","", "insert into domains (type,name,master,account) values('SLAVE',$1,$2,$3)"); 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)"); @@ -86,6 +86,7 @@ public: declare(suffix,"update-master-query","", "update domains set master=$1 where name=$2"); declare(suffix,"update-kind-query","", "update domains set type=$1 where name=$2"); + declare(suffix,"update-account-query","", "update domains set account=$1 where name=$2"); declare(suffix,"update-serial-query","", "update domains set notified_serial=$1 where id=$2"); declare(suffix,"update-lastcheck-query","", "update domains set last_check=$1 where id=$2"); declare(suffix,"zone-lastchange-query", "", "select max(change_date) from records where domain_id=$1"); diff --git a/modules/gsqlite3backend/gsqlite3backend.cc b/modules/gsqlite3backend/gsqlite3backend.cc index a8bbf231c..3c8726206 100644 --- a/modules/gsqlite3backend/gsqlite3backend.cc +++ b/modules/gsqlite3backend/gsqlite3backend.cc @@ -83,7 +83,7 @@ public: declare(suffix, "supermaster-query", "", "select account from supermasters where ip=:ip and nameserver=:nameserver"); declare(suffix, "supermaster-name-to-ips", "", "select ip,account from supermasters where nameserver=:nameserver and account=:account"); - declare(suffix, "insert-zone-query", "", "insert into domains (type,name) values('NATIVE',:domain)"); + declare(suffix, "insert-zone-query", "", "insert into domains (type,name,account) values('NATIVE',:domain,:account)"); declare(suffix, "insert-slave-query", "", "insert into domains (type,name,master,account) values('SLAVE',:domain,:masters,:account)"); declare(suffix, "insert-record-query", "", "insert into records (content,ttl,prio,type,domain_id,disabled,name,auth) values (:content,:ttl,:priority,:qtype,:domain_id,:disabled,:qname,:auth)"); @@ -103,6 +103,7 @@ public: declare(suffix, "update-master-query", "", "update domains set master=:master where name=:domain"); declare(suffix, "update-kind-query", "", "update domains set type=:kind where name=:domain"); + declare(suffix, "update-account-query","", "update domains set account=:account where name=:domain"); declare(suffix, "update-serial-query", "", "update domains set notified_serial=:serial where id=:domain_id"); declare(suffix, "update-lastcheck-query", "", "update domains set last_check=:last_check where id=:domain_id"); declare(suffix, "zone-lastchange-query", "", "select max(change_date) from records where domain_id=:domain_id"); diff --git a/pdns/backends/gsql/gsqlbackend.cc b/pdns/backends/gsql/gsqlbackend.cc index c45aa2ac4..42b9c1781 100644 --- a/pdns/backends/gsql/gsqlbackend.cc +++ b/pdns/backends/gsql/gsqlbackend.cc @@ -72,6 +72,7 @@ GSQLBackend::GSQLBackend(const string &mode, const string &suffix) d_UpdateKindOfZoneQuery=getArg("update-kind-query"); d_UpdateSerialOfZoneQuery=getArg("update-serial-query"); d_UpdateLastCheckofZoneQuery=getArg("update-lastcheck-query"); + d_UpdateAccountOfZoneQuery=getArg("update-account-query"); d_ZoneLastChangeQuery=getArg("zone-lastchange-query"); d_InfoOfAllMasterDomainsQuery=getArg("info-all-master-query"); d_DeleteDomainQuery=getArg("delete-domain-query"); @@ -142,6 +143,7 @@ GSQLBackend::GSQLBackend(const string &mode, const string &suffix) d_UpdateKindOfZoneQuery_stmt = NULL; d_UpdateSerialOfZoneQuery_stmt = NULL; d_UpdateLastCheckofZoneQuery_stmt = NULL; + d_UpdateAccountOfZoneQuery_stmt = NULL; d_InfoOfAllMasterDomainsQuery_stmt = NULL; d_DeleteDomainQuery_stmt = NULL; d_DeleteZoneQuery_stmt = NULL; @@ -270,6 +272,21 @@ bool GSQLBackend::setKind(const string &domain, const DomainInfo::DomainKind kin return true; } +bool GSQLBackend::setAccount(const string &domain, const string &account) +{ + try { + d_UpdateAccountOfZoneQuery_stmt-> + bind("account", account)-> + bind("domain", toLower(domain))-> + execute()-> + reset(); + } + catch (SSqlException &e) { + throw PDNSException("GSQLBackend unable to set account of domain \""+domain+"\": "+e.txtReason()); + } + return true; +} + bool GSQLBackend::getDomainInfo(const string &domain, DomainInfo &di) { /* fill DomainInfo from database info: diff --git a/pdns/backends/gsql/gsqlbackend.hh b/pdns/backends/gsql/gsqlbackend.hh index ed091e110..b2e8d0fec 100644 --- a/pdns/backends/gsql/gsqlbackend.hh +++ b/pdns/backends/gsql/gsqlbackend.hh @@ -39,7 +39,7 @@ public: d_InfoOfAllSlaveDomainsQuery_stmt = d_db->prepare(d_InfoOfAllSlaveDomainsQuery, 0); d_SuperMasterInfoQuery_stmt = d_db->prepare(d_SuperMasterInfoQuery, 2); d_GetSuperMasterIPs_stmt = d_db->prepare(d_GetSuperMasterIPs, 2); - d_InsertZoneQuery_stmt = d_db->prepare(d_InsertZoneQuery, 1); + d_InsertZoneQuery_stmt = d_db->prepare(d_InsertZoneQuery, 2); d_InsertSlaveZoneQuery_stmt = d_db->prepare(d_InsertSlaveZoneQuery, 3); d_InsertRecordQuery_stmt = d_db->prepare(d_InsertRecordQuery, 8); d_InsertEntQuery_stmt = d_db->prepare(d_InsertEntQuery, 3); @@ -47,6 +47,7 @@ public: d_InsertEntOrderQuery_stmt = d_db->prepare(d_InsertEntOrderQuery, 4); d_UpdateMasterOfZoneQuery_stmt = d_db->prepare(d_UpdateMasterOfZoneQuery, 2); d_UpdateKindOfZoneQuery_stmt = d_db->prepare(d_UpdateKindOfZoneQuery, 2); + d_UpdateAccountOfZoneQuery_stmt = d_db->prepare(d_UpdateAccountOfZoneQuery, 2); d_UpdateSerialOfZoneQuery_stmt = d_db->prepare(d_UpdateSerialOfZoneQuery, 2); d_UpdateLastCheckofZoneQuery_stmt = d_db->prepare(d_UpdateLastCheckofZoneQuery, 2); d_InfoOfAllMasterDomainsQuery_stmt = d_db->prepare(d_InfoOfAllMasterDomainsQuery, 0); @@ -115,6 +116,7 @@ public: release(&d_InsertEntOrderQuery_stmt); release(&d_UpdateMasterOfZoneQuery_stmt); release(&d_UpdateKindOfZoneQuery_stmt); + release(&d_UpdateAccountOfZoneQuery_stmt); release(&d_UpdateSerialOfZoneQuery_stmt); release(&d_UpdateLastCheckofZoneQuery_stmt); release(&d_InfoOfAllMasterDomainsQuery_stmt); @@ -180,6 +182,7 @@ public: void setNotified(uint32_t domain_id, uint32_t serial); bool setMaster(const string &domain, const string &ip); bool setKind(const string &domain, const DomainInfo::DomainKind kind); + bool setAccount(const string &domain, const string &account); virtual bool getBeforeAndAfterNamesAbsolute(uint32_t id, const std::string& qname, std::string& unhashed, std::string& before, std::string& after); bool updateDNSSECOrderAndAuth(uint32_t domain_id, const std::string& zonename, const std::string& qname, bool auth); @@ -244,6 +247,7 @@ private: string d_InsertEntOrderQuery; string d_UpdateMasterOfZoneQuery; string d_UpdateKindOfZoneQuery; + string d_UpdateAccountOfZoneQuery; string d_UpdateSerialOfZoneQuery; string d_UpdateLastCheckofZoneQuery; string d_InfoOfAllMasterDomainsQuery; @@ -312,6 +316,7 @@ private: SSqlStatement* d_InsertEntOrderQuery_stmt; SSqlStatement* d_UpdateMasterOfZoneQuery_stmt; SSqlStatement* d_UpdateKindOfZoneQuery_stmt; + SSqlStatement* d_UpdateAccountOfZoneQuery_stmt; SSqlStatement* d_UpdateSerialOfZoneQuery_stmt; SSqlStatement* d_UpdateLastCheckofZoneQuery_stmt; SSqlStatement* d_InfoOfAllMasterDomainsQuery_stmt; diff --git a/pdns/dnsbackend.hh b/pdns/dnsbackend.hh index c07a27b05..b4122b96e 100644 --- a/pdns/dnsbackend.hh +++ b/pdns/dnsbackend.hh @@ -337,6 +337,12 @@ public: return false; } + //! Called when the Account of a domain should be changed + virtual bool setAccount(const string &domain, const string &account) + { + return false; + } + //! Can be called to seed the getArg() function with a prefix void setArgPrefix(const string &prefix); diff --git a/pdns/ws-auth.cc b/pdns/ws-auth.cc index 828542201..40a118448 100644 --- a/pdns/ws-auth.cc +++ b/pdns/ws-auth.cc @@ -470,6 +470,9 @@ static void updateDomainSettingsFromDocument(const DomainInfo& di, const string& if (document["soa_edit"].IsString()) { di.backend->setDomainMetadataOne(zonename, "SOA-EDIT", document["soa_edit"].GetString()); } + if (document["account"].IsString()) { + di.backend->setAccount(zonename, document["account"].GetString()); + } } static void apiZoneCryptokeys(HttpRequest* req, HttpResponse* resp) { diff --git a/regression-tests.api/test_Zones.py b/regression-tests.api/test_Zones.py index 3daf9ddb3..8223c3e7a 100644 --- a/regression-tests.api/test_Zones.py +++ b/regression-tests.api/test_Zones.py @@ -75,6 +75,15 @@ class AuthZones(ApiTestCase): self.assertGreater(soa_serial, payload['serial']) self.assertEquals(soa_serial, data['serial']) + def test_create_zone_with_account(self): + # soa_edit_api wins over serial + payload, data = self.create_zone(account='anaccount', serial=10) + print data + for k in ('account', ): + self.assertIn(k, data) + if k in payload: + self.assertEquals(data[k], payload[k]) + def test_create_zone_with_records(self): name = unique_zone_name() records = [ -- 2.40.0