]>
git.ipfire.org Git - people/stevee/pypdns.git/blob - backend.py
1 ###############################################################################
3 # pyPDNS - A PDNS administration tool, written in pure python. #
4 # Copyright (C) 2012 Pakfire development team #
6 # This program is free software: you can redistribute it and/or modify #
7 # it under the terms of the GNU General Public License as published by #
8 # the Free Software Foundation, either version 3 of the License, or #
9 # (at your option) any later version. #
11 # This program is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 ###############################################################################
21 # More details about the database tables and fields can be found here: #
22 # http://wiki.powerdns.com/trac/wiki/fields #
24 ###############################################################################
28 DB
= "/var/lib/pdns/pdns.db"
30 # Create the primary DNS class.
31 """Use Database class from imported database module to connect to the PDNS sqlite database."""
33 def __init__(self
, db
):
34 self
.db
= database
.Database(db
)
36 # Get all configured domains.
37 def get_domains(self
):
40 """Fetch all configured domains, line by line and add them to the previous created empty list."""
41 for row
in self
.db
.query("SELECT id FROM domains"):
42 domain
= Domain(self
, row
.id)
43 domains
.append(domain
)
47 # Get a domain by it's name.
48 def get_domain(self
, name
):
49 row
= self
.db
.get("SELECT id FROM domains WHERE name = ?", name
)
51 # Only do anything, if there is an existing domain.
53 domain
= Domain(self
, row
.id)
57 # Create Domain class.
58 """Use query method from database module to get all requested information about our domain."""
59 """The domain is specified by it's unique id."""
61 def __init__(self
, dns
, domain_id
):
69 # Determine the name of the zone by a given id.
72 row
= self
.db
.get("SELECT name FROM domains WHERE id = ?", self
.id)
75 # Get information of the master nameserver from which the domain should be slaved.
78 row
= self
.db
.get("SELECT master FROM domains WHERE id = ?", self
.id)
81 # Fetch data of the last check from the domain.
84 row
= self
.db
.get("SELECT last_check FROM domains WHERE id = ?", self
.id)
87 # Get the type of the domain.
90 row
= self
.db
.get("SELECT type FROM domains WHERE id = ?", self
.id)
93 # Get the last notified serial of a used master domain.
95 def notified_serial(self
):
96 row
= self
.db
.get("SELECT notified_serial FROM domains WHERE id = ?", self
.id)
97 return row
.notified_serial
99 # Gain if a certain host is a supermaster for a certain domain name.
102 row
= self
.db
.get("SELECT account FROM domains WHERE id = ?", self
.id)
105 # Get all records from zone.
110 """Fetch all records from domain, line by line and add them to the previous created empty list."""
111 for row
in self
.db
.query("SELECT id, type FROM records WHERE domain_id = ?", self
.id):
112 if row
.type == "SOA":
113 record
= SOARecord(self
, row
.id)
114 elif row
.type == "A":
115 record
= ARecord(self
, row
.id)
117 record
= Record(self
, row
.id)
118 records
.append(record
)
122 # Get records by a specified type.
123 def get_records_by_type(self
, type):
125 for record
in self
.records
:
126 if record
.type == type:
127 records
.append(record
)
131 # Quick function to get the first SOA record from the domain.
134 records
= self
.get_records_by_type("SOA")
139 # Create class for domain records.
140 """It is used to get more details about the configured records."""
141 """The domain is specified by it's unique id."""
142 class Record(object):
143 def __init__(self
, domain
, record_id
):
149 return self
.domain
.db
151 # Determine the type of the record.
154 row
= self
.db
.get("SELECT type FROM records WHERE id = ?", self
.id)
157 # Get the configured DNS name of the record.
160 row
= self
.db
.get("SELECT name FROM records WHERE id = ?", self
.id)
164 # Fetch content like the address to which the record points.
167 row
= self
.db
.get("SELECT content FROM records WHERE id = ?", self
.id)
171 # Get the "Time to live" for the record.
174 row
= self
.db
.get("SELECT ttl FROM records WHERE id = ?", self
.id)
177 # Gain the configured record priority.
180 row
= self
.db
.get("SELECT prio FROM records WHERE id = ?" , self
.id)
183 # Get the change_date.
185 def change_date(self
):
186 row
= self
.db
.get("SELECT change_date FROM records WHERE id = ?" , self
.id)
187 return row
.change_date
189 # Fetch the ordername.
192 row
= self
.db
.get("SELECT ordername FROM records WHERE id = ?" , self
.id)
195 # Gain all information about records authentication.
197 def authentication(self
):
198 row
= self
.db
.get("SELECT auth FROM records WHERE id = ?" , self
.id)
202 # Create an own class to deal with "SOA" records.
203 """Use splitt() to generate a list of the original content string from the database, and return the requested entries."""
204 class SOARecord(Record
):
205 def __init__(self
, domain
, record_id
):
206 Record
.__init
__(self
, domain
, record_id
)
208 self
.soa_attrs
= self
.content
.split()
210 # Check if the content from database is valid (It contains all 7 required information).
211 if not len(self
.soa_attrs
) == 7:
212 #XXX Add something like an error message or log output.
215 # Primary NS - the domain name of the name server that was the original source of the data.
218 return self
.soa_attrs
[0]
220 # E-mail address of the person which is responsible for this domain.
223 return self
.soa_attrs
[1]
225 # The serial which increases allways after a change on the domain has been made.
228 return self
.soa_attrs
[2]
230 # The number of seconds between the time that a secondary name server gets a copy of the domain.
233 return self
.soa_attrs
[3]
235 # The number of seconds during the next refresh attempt if the previous fails.
238 return self
.soa_attrs
[4]
240 # The number of seconds that lets the secondary name server(s) know how long they can hold the information.
243 return self
.soa_attrs
[5]
245 # The number of seconds that the records in the domain are valid.
248 return self
.soa_attrs
[6]
252 class ARecord(Record
):