Changeset 1154

Show
Ignore:
Timestamp:
03/09/08 16:31:37 (2 years ago)
Author:
ahu
Message:

Norbert sends:
Here's an update for the OpenDBX backend:
- Fix for SQLite2/3 locking problem on concurrent reads/writes
- Fixes compilation on Solaris (u_int vs. uint)
- Support for autoserial (needs triggers)

Location:
trunk/pdns/modules/opendbxbackend
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/pdns/modules/opendbxbackend/odbxbackend.cc

    r1018 r1154  
    107107                        di.serial = 0; 
    108108 
    109                         if( ( tmp = odbx_field_value( m_result, 0 ) ) != NULL ) 
    110                         { 
    111                                 di.id = strtol( tmp, NULL, 10 ); 
    112                         } 
    113  
    114                         if( ( tmp = odbx_field_value( m_result, 1 ) ) != NULL ) 
    115                         { 
    116                                 di.zone = string( tmp, odbx_field_length( m_result, 1 ) ); 
     109                        if( ( tmp = odbx_field_value( m_result, 6 ) ) != NULL ) 
     110                        { 
     111                                SOAData sd; 
     112 
     113                                sd.serial = 0; 
     114                                fillSOAData( string( tmp, odbx_field_length( m_result, 6 ) ), sd ); 
     115 
     116                                if( sd.serial == 0 && ( tmp = odbx_field_value( m_result, 5 ) ) != NULL ) 
     117                                { 
     118                                        sd.serial = strtol( tmp, NULL, 10 ); 
     119                                } 
     120 
     121                                di.serial = sd.serial; 
     122                        } 
     123 
     124                        if( ( tmp = odbx_field_value( m_result, 4 ) ) != NULL ) 
     125                        { 
     126                                di.last_check = strtol( tmp, NULL, 10 ); 
     127                        } 
     128 
     129                        if( ( tmp = odbx_field_value( m_result, 3 ) ) != NULL ) 
     130                        { 
     131                                stringtok(di.masters, string( tmp, odbx_field_length( m_result, 3 ) ), ", \t"); 
    117132                        } 
    118133 
     
    129144                        } 
    130145 
    131                         if( ( tmp = odbx_field_value( m_result, 3 ) ) != NULL ) 
    132                         { 
    133                                 string masters = string( tmp, odbx_field_length( m_result, 3 ) ); 
    134                                 stringtok(di.masters, masters, ", \t"); 
    135                         } 
    136  
    137                         if( ( tmp = odbx_field_value( m_result, 5 ) ) != NULL ) 
    138                         { 
    139                                 di.last_check = strtol( tmp, NULL, 10 ); 
    140                         } 
    141  
    142                         if( ( tmp = odbx_field_value( m_result, 6 ) ) != NULL ) 
    143                         { 
    144                                 SOAData sd; 
    145  
    146                                 sd.serial = 0; 
    147                                 fillSOAData( string( tmp, odbx_field_length( m_result, 6 ) ), sd ); 
    148                                 di.serial = sd.serial; 
     146                        if( ( tmp = odbx_field_value( m_result, 1 ) ) != NULL ) 
     147                        { 
     148                                di.zone = string( tmp, odbx_field_length( m_result, 1 ) ); 
     149                        } 
     150 
     151                        if( ( tmp = odbx_field_value( m_result, 0 ) ) != NULL ) 
     152                        { 
     153                                di.id = strtol( tmp, NULL, 10 ); 
    149154                        } 
    150155                } 
     
    154159        { 
    155160                L.log( m_myname + " getDomainInfo: Caught STL exception - " + e.what(),  Logger::Error ); 
     161                return false; 
     162        } 
     163 
     164        return true; 
     165} 
     166 
     167 
     168 
     169bool OdbxBackend::getSOA( const string& domain, SOAData& sd, DNSPacket* p ) 
     170{ 
     171        const char* tmp; 
     172 
     173 
     174        try 
     175        { 
     176                DLOG( L.log( m_myname + " getSOA()", Logger::Debug ) ); 
     177 
     178                string stmt = getArg( "sql-lookupsoa" ); 
     179                string& stmtref = strbind( ":name", escape( toLower( domain ), READ ), stmt ); 
     180 
     181                if( !execStmt( stmtref.c_str(), stmtref.size(), READ ) ) { return false; } 
     182                if( !getRecord( READ ) ) { return false; } 
     183 
     184                do 
     185                { 
     186                        sd.serial = 0; 
     187 
     188                        if( ( tmp = odbx_field_value( m_result, 2 ) ) != NULL ) 
     189                        { 
     190                                fillSOAData( string( tmp, odbx_field_length( m_result, 2 ) ), sd ); 
     191                        } 
     192 
     193                        if( sd.serial == 0 && ( tmp = odbx_field_value( m_result, 1 ) ) != NULL ) 
     194                        { 
     195                                sd.serial = strtol( tmp, NULL, 10 ); 
     196                        } 
     197 
     198                        if( ( tmp = odbx_field_value( m_result, 0 ) ) != NULL ) 
     199                        { 
     200                                sd.domain_id = strtol( tmp, NULL, 10 ); 
     201                        } 
     202 
     203                        if( sd.nameserver.empty() ) 
     204                        { 
     205                                sd.nameserver = arg()["default-soa-name"]; 
     206                        } 
     207 
     208                        if( sd.hostmaster.empty() ) 
     209                        { 
     210                                sd.hostmaster = "hostmaster." + domain; 
     211                        } 
     212 
     213                        sd.db = this; 
     214                } 
     215                while( getRecord( READ ) ); 
     216        } 
     217        catch( exception& e ) 
     218        { 
     219                L.log( m_myname + " getSOA: Caught STL exception - " + e.what(),  Logger::Error ); 
    156220                return false; 
    157221        } 
     
    212276                m_result = NULL; 
    213277                m_qname = qname; 
    214                  
     278 
    215279                if( zoneid < 0 ) 
    216280                { 
     
    232296                                stmtref = strbind( ":type", qtype.getName(), stmt ); 
    233297                        } 
    234                          
     298 
    235299                        size_t len = snprintf( m_buffer, sizeof( m_buffer ) - 1, "%d", zoneid ); 
    236300 
     
    307371                        if( ( tmp = odbx_field_value( m_result, 4 ) ) != NULL ) 
    308372                        { 
    309                                 rr.priority = (u_int16_t) strtoul( tmp, NULL, 10 ); 
     373                                rr.priority = (uint16_t) strtoul( tmp, NULL, 10 ); 
    310374                        } 
    311375 
     
    328392 
    329393 
    330 void OdbxBackend::setFresh( u_int32_t domain_id ) 
     394void OdbxBackend::setFresh( uint32_t domain_id ) 
    331395{ 
    332396        size_t len; 
     
    371435 
    372436 
    373 void OdbxBackend::setNotified( u_int32_t domain_id, u_int32_t serial ) 
     437void OdbxBackend::setNotified( uint32_t domain_id, uint32_t serial ) 
    374438{ 
    375439        try 
  • trunk/pdns/modules/opendbxbackend/odbxbackend.hh

    r977 r1154  
    4646 
    4747 
    48 bool checkSlave( u_int32_t last, u_int32_t notified, SOAData* sd, DomainInfo* di ); 
    49 bool checkMaster( u_int32_t last, u_int32_t notified, SOAData* sd, DomainInfo* di ); 
     48bool checkSlave( uint32_t last, uint32_t notified, SOAData* sd, DomainInfo* di ); 
     49bool checkMaster( uint32_t last, uint32_t notified, SOAData* sd, DomainInfo* di ); 
    5050 
    5151 
     
    6666        string escape( const string& str, QueryType type ); 
    6767        bool connectTo( const vector<string>& host, QueryType type ); 
    68         bool getDomainList( const string& query, vector<DomainInfo>* list, bool (*check_fcn)(u_int32_t,u_int32_t,SOAData*,DomainInfo*) ); 
     68        bool getDomainList( const string& query, vector<DomainInfo>* list, bool (*check_fcn)(uint32_t,uint32_t,SOAData*,DomainInfo*) ); 
    6969        bool execStmt( const char* stmt, unsigned long length, QueryType type ); 
    7070        bool getRecord( QueryType type ); 
     
    7777 
    7878        void lookup( const QType& qtype, const string& qdomain, DNSPacket* p = 0, int zoneid = -1 ); 
     79        bool getSOA( const string& domain, SOAData& sd, DNSPacket* p ); 
    7980        bool list( const string& target, int domain_id ); 
    8081        bool get( DNSResourceRecord& rr ); 
     
    9394        void getUnfreshSlaveInfos( vector<DomainInfo>* unfresh ); 
    9495 
    95         void setFresh( u_int32_t domain_id ); 
    96         void setNotified( u_int32_t domain_id, u_int32_t serial ); 
     96        void setFresh( uint32_t domain_id ); 
     97        void setNotified( uint32_t domain_id, uint32_t serial ); 
    9798}; 
    9899 
     
    117118                declare( suffix, "password","Password for connecting to the DBMS",""); 
    118119 
    119                 declare( suffix, "sql-list", "AXFR query", "SELECT \"domain_id\", \"name\", \"type\", \"ttl\", \"prio\", \"content\" FROM \"records\" WHERE \"domain_id\"=:id" ); 
     120                declare( suffix, "sql-list", "AXFR query", "SELECT r.\"domain_id\", r.\"name\", r.\"type\", r.\"ttl\", r.\"prio\", r.\"content\" FROM \"records\" r WHERE r.\"domain_id\"=:id" ); 
    120121 
    121                 declare( suffix, "sql-lookup", "Lookup query","SELECT \"domain_id\", \"name\", \"type\", \"ttl\", \"prio\", \"content\" FROM \"records\" WHERE \"name\"=':name'" ); 
    122                 declare( suffix, "sql-lookupid", "Lookup query with id","SELECT \"domain_id\", \"name\", \"type\", \"ttl\", \"prio\", \"content\" FROM \"records\" WHERE \"domain_id\"=:id AND \"name\"=':name'" ); 
    123                 declare( suffix, "sql-lookuptype", "Lookup query with type","SELECT \"domain_id\", \"name\", \"type\", \"ttl\", \"prio\", \"content\" FROM \"records\" WHERE \"name\"=':name' AND \"type\"=':type'" ); 
    124                 declare( suffix, "sql-lookuptypeid", "Lookup query with type and id","SELECT \"domain_id\", \"name\", \"type\", \"ttl\", \"prio\", \"content\" FROM \"records\" WHERE \"domain_id\"=:id AND \"name\"=':name' AND \"type\"=':type'" ); 
     122                declare( suffix, "sql-lookup", "Lookup query","SELECT r.\"domain_id\", r.\"name\", r.\"type\", r.\"ttl\", r.\"prio\", r.\"content\" FROM \"records\" r WHERE r.\"name\"=':name'" ); 
     123                declare( suffix, "sql-lookupid", "Lookup query with id","SELECT r.\"domain_id\", r.\"name\", r.\"type\", r.\"ttl\", r.\"prio\", r.\"content\" FROM \"records\" r WHERE r.\"domain_id\"=:id AND r.\"name\"=':name'" ); 
     124                declare( suffix, "sql-lookuptype", "Lookup query with type","SELECT r.\"domain_id\", r.\"name\", r.\"type\", r.\"ttl\", r.\"prio\", r.\"content\" FROM \"records\" r WHERE r.\"name\"=':name' AND r.\"type\"=':type'" ); 
     125                declare( suffix, "sql-lookuptypeid", "Lookup query with type and id","SELECT r.\"domain_id\", r.\"name\", r.\"type\", r.\"ttl\", r.\"prio\", r.\"content\" FROM \"records\" r WHERE r.\"domain_id\"=:id AND r.\"name\"=':name' AND r.\"type\"=':type'" ); 
     126                declare( suffix, "sql-lookupsoa","Lookup query for SOA record","SELECT d.\"id\", d.\"auto_serial\", r.\"content\" FROM \"records\" r JOIN \"domains\" d ON r.\"domain_id\"=d.\"id\" WHERE r.\"name\"=':name' AND r.\"type\"='SOA' AND d.\"status\"='A'" ); 
    125127 
    126                 declare( suffix, "sql-zonedelete","Delete all records for this zone","DELETE FROM \"records\" WHERE \"domain_id\"=:id" ); 
    127                 declare( suffix, "sql-zoneinfo","Get domain info","SELECT d.\"id\", d.\"name\", d.\"type\", d.\"master\", d.\"last_check\", r.\"content\" FROM \"domains\" d LEFT JOIN \"records\" r ON ( d.\"id\"=r.\"domain_id\" AND r.\"type\"='SOA' ) WHERE d.\"name\"=':name' AND d.\"status\"='A'" ); 
     128                declare( suffix, "sql-zonedelete","Delete all records for this zone","DELETE FROM \"records\" r WHERE r.\"domain_id\"=:id" ); 
     129                declare( suffix, "sql-zoneinfo","Get domain info","SELECT d.\"id\", d.\"name\", d.\"type\", d.\"master\", d.\"last_check\", d.\"auto_serial\", r.\"content\" FROM \"domains\" d LEFT JOIN \"records\" r ON ( d.\"id\"=r.\"domain_id\" AND r.\"type\"='SOA' ) WHERE d.\"name\"=':name' AND d.\"status\"='A'" ); 
    128130 
    129131                declare( suffix, "sql-transactbegin", "Start transaction", "BEGIN" ); 
     
    134136                declare( suffix, "sql-insert-record","Feed record into table", "INSERT INTO \"records\" ( \"domain_id\", \"name\", \"type\", \"ttl\", \"prio\", \"content\" ) VALUES ( %d, '%s', '%s', %d, %d, '%s' )" ); 
    135137 
    136                 declare( suffix, "sql-update-serial", "Set zone to notified", "UPDATE \"domains\" SET \"notified_serial\"=%d WHERE \"id\"=%d" ); 
    137                 declare( suffix, "sql-update-lastcheck", "Set time of last check", "UPDATE \"domains\" SET \"last_check\"=%d WHERE \"id\"=%d" ); 
     138                declare( suffix, "sql-update-serial", "Set zone to notified", "UPDATE \"domains\" d SET d.\"notified_serial\"=%d WHERE d.\"id\"=%d" ); 
     139                declare( suffix, "sql-update-lastcheck", "Set time of last check", "UPDATE \"domains\" d SET d.\"last_check\"=%d WHERE d.\"id\"=%d" ); 
    138140 
    139                 declare( suffix, "sql-master", "Get master record for zone", "SELECT \"master\" FROM \"domains\" WHERE \"name\"=':name' AND \"status\"='A' AND \"type\"='SLAVE'" ); 
    140                 declare( suffix, "sql-supermaster","Get supermaster info", "SELECT \"account\" FROM \"supermasters\" WHERE \"ip\"=':ip' AND \"nameserver\"=':ns'" ); 
     141                declare( suffix, "sql-master", "Get master record for zone", "SELECT d.\"master\" FROM \"domains\" d WHERE d.\"name\"=':name' AND d.\"status\"='A' AND d.\"type\"='SLAVE'" ); 
     142                declare( suffix, "sql-supermaster","Get supermaster info", "SELECT s.\"account\" FROM \"supermasters\" s WHERE s.\"ip\"=':ip' AND s.\"nameserver\"=':ns'" ); 
    141143 
    142                 declare( suffix, "sql-infoslaves", "Get all unfresh slaves", "SELECT d.\"id\", d.\"name\", d.\"master\", d.\"notified_serial\", d.\"last_check\", r.\"change_date\", r.\"content\" FROM \"domains\" d LEFT JOIN \"records\" r ON ( d.\"id\"=r.\"domain_id\" AND r.\"type\"='SOA' ) WHERE d.\"status\"='A' AND d.\"type\"='SLAVE'" ); 
    143                 declare( suffix, "sql-infomasters", "Get all updated masters", "SELECT d.\"id\", d.\"name\", d.\"master\", d.\"notified_serial\", d.\"last_check\", r.\"change_date\", r.\"content\" FROM \"domains\" d JOIN \"records\" r ON d.\"id\"=r.\"domain_id\" WHERE d.\"status\"='A' AND d.\"type\"='MASTER' AND r.\"type\"='SOA'" ); 
     144                declare( suffix, "sql-infoslaves", "Get all unfresh slaves", "SELECT d.\"id\", d.\"name\", d.\"master\", d.\"last_check\", d.\"notified_serial\", d.\"auto_serial\", r.\"content\" FROM \"domains\" d LEFT JOIN \"records\" r ON ( d.\"id\"=r.\"domain_id\" AND r.\"type\"='SOA' ) WHERE d.\"status\"='A' AND d.\"type\"='SLAVE'" ); 
     145                declare( suffix, "sql-infomasters", "Get all updated masters", "SELECT d.\"id\", d.\"name\", d.\"master\", d.\"last_check\", d.\"notified_serial\", d.\"auto_serial\", r.\"content\" FROM \"domains\" d LEFT JOIN \"records\" r ON ( d.\"id\"=r.\"domain_id\" AND r.\"type\"='SOA' ) WHERE d.\"status\"='A' AND d.\"type\"='MASTER'" ); 
    144146 
    145147                declare( suffix, "host", "depricated, use host-read and host-write instead","" ); 
  • trunk/pdns/modules/opendbxbackend/odbxprivate.cc

    r1024 r1154  
    1919                odbx_finish( m_handle[type] ); 
    2020                m_handle[type] = NULL; 
     21        } 
     22 
     23        if( type == WRITE && getArg( "backend" ) == "sqlite" ) 
     24        { 
     25                L.log( m_myname + " Using same SQLite connection for reading and writeing to '" + hosts[odbx_host_index[READ]] + "'", Logger::Notice ); 
     26                m_handle[WRITE] = m_handle[READ]; 
     27                return true; 
    2128        } 
    2229 
     
    157164 
    158165 
    159 bool OdbxBackend::getDomainList( const string& stmt, vector<DomainInfo>* list, bool (*check_fcn)(u_int32_t,u_int32_t,SOAData*,DomainInfo*) ) 
     166bool OdbxBackend::getDomainList( const string& stmt, vector<DomainInfo>* list, bool (*check_fcn)(uint32_t,uint32_t,SOAData*,DomainInfo*) ) 
    160167{ 
    161168        const char* tmp; 
    162         u_int32_t nlast, nserial; 
     169        uint32_t nlast, nserial; 
    163170        DomainInfo di; 
    164171        SOAData sd; 
     
    189196                if( ( tmp = odbx_field_value( m_result, 4 ) ) != NULL ) 
    190197                { 
     198                        nserial = strtol( tmp, NULL, 10 ); 
     199                } 
     200 
     201                if( ( tmp = odbx_field_value( m_result, 3 ) ) != NULL ) 
     202                { 
    191203                        nlast = strtol( tmp, NULL, 10 ); 
    192                 } 
    193  
    194                 if( ( tmp = odbx_field_value( m_result, 3 ) ) != NULL ) 
    195                 { 
    196                         nserial = strtol( tmp, NULL, 10 ); 
    197204                } 
    198205 
     
    229236 
    230237 
    231 bool checkSlave( u_int32_t nlast, u_int32_t nserial, SOAData* sd, DomainInfo* di ) 
    232 { 
    233         if( nlast + sd->refresh < (u_int32_t) time( 0 ) ) 
     238bool checkSlave( uint32_t nlast, uint32_t nserial, SOAData* sd, DomainInfo* di ) 
     239{ 
     240        if( nlast + sd->refresh < (uint32_t) time( 0 ) ) 
    234241        { 
    235242                di->kind = DomainInfo::Slave; 
     
    242249 
    243250 
    244 bool checkMaster( u_int32_t nlast, u_int32_t nserial, SOAData* sd, DomainInfo* di ) 
     251bool checkMaster( uint32_t nlast, uint32_t nserial, SOAData* sd, DomainInfo* di ) 
    245252{ 
    246253        if( nserial != sd->serial )