]>
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 IPFire 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 # Basic information about the database layout can be found here: #
22 # http://doc.powerdns.com/gsqlite.html #
24 # More details about the database tables and fields can be found here: #
25 # http://wiki.powerdns.com/trac/wiki/fields #
27 ###############################################################################
31 DB
= "/var/lib/pdns/pdns.db"
33 # Create the primary DNS class.
34 """Use Database class from imported database module to connect to the PDNS sqlite database."""
36 def __init__(self
, db
):
37 self
.db
= database
.Database(db
)
39 # Get all configured domains.
40 def get_domains(self
):
43 """Fetch all configured domains, line by line and add them to the previous created empty list."""
44 for row
in self
.db
.query("SELECT id FROM domains"):
45 domain
= Domain(self
, row
.id)
46 domains
.append(domain
)
50 # Get a domain by it's name.
51 def get_domain(self
, name
):
52 row
= self
.db
.get("SELECT id FROM domains WHERE name = ?", name
)
54 # Only do anything, if there is an existing domain.
56 domain
= Domain(self
, row
.id)
60 # Create Domain class.
61 """Use query method from database module to get all requested information about our domain."""
62 """The domain is specified by it's unique id."""
64 def __init__(self
, dns
, domain_id
):
72 # Determine the name of the zone by a given id.
75 row
= self
.db
.get("SELECT name FROM domains WHERE id = ?", self
.id)
78 # Get information of the master nameserver from which the domain should be slaved.
81 row
= self
.db
.get("SELECT master FROM domains WHERE id = ?", self
.id)
84 # Fetch data of the last check from the domain.
87 row
= self
.db
.get("SELECT last_check FROM domains WHERE id = ?", self
.id)
90 # Get the type of the domain.
93 row
= self
.db
.get("SELECT type FROM domains WHERE id = ?", self
.id)
96 # Get the last notified serial of a used master domain.
98 def notified_serial(self
):
99 row
= self
.db
.get("SELECT notified_serial FROM domains WHERE id = ?", self
.id)
100 return row
.notified_serial
102 # Gain if a certain host is a supermaster for a certain domain name.
105 row
= self
.db
.get("SELECT account FROM domains WHERE id = ?", self
.id)
108 # Get all records from zone.
113 """Fetch all records from domain, line by line and add them to the previous created empty list."""
114 for row
in self
.db
.query("SELECT id, type FROM records WHERE domain_id = ?", self
.id):
115 if row
.type == "SOA":
116 record
= SOARecord(self
, row
.id)
117 elif row
.type == "A":
118 record
= ARecord(self
, row
.id)
120 record
= Record(self
, row
.id)
121 records
.append(record
)
125 # Get records by a specified type.
126 def get_records_by_type(self
, type):
128 for record
in self
.records
:
129 if record
.type == type:
130 records
.append(record
)
134 # Quick function to get the first SOA record from the domain.
137 records
= self
.get_records_by_type("SOA")
142 # Create class for domain records.
143 """It is used to get more details about the configured records."""
144 """The domain is specified by it's unique id."""
145 class Record(object):
146 def __init__(self
, domain
, record_id
):
152 return self
.domain
.db
154 # Determine the type of the record.
157 row
= self
.db
.get("SELECT type FROM records WHERE id = ?", self
.id)
160 # Get the configured DNS name of the record.
163 row
= self
.db
.get("SELECT name FROM records WHERE id = ?", self
.id)
167 # Fetch content like the address to which the record points.
170 row
= self
.db
.get("SELECT content FROM records WHERE id = ?", self
.id)
174 # Get the "Time to live" for the record.
177 row
= self
.db
.get("SELECT ttl FROM records WHERE id = ?", self
.id)
180 # Gain the configured record priority.
183 row
= self
.db
.get("SELECT prio FROM records WHERE id = ?" , self
.id)
186 # Get the change_date.
188 def change_date(self
):
189 row
= self
.db
.get("SELECT change_date FROM records WHERE id = ?" , self
.id)
190 return row
.change_date
192 # Fetch the ordername.
195 row
= self
.db
.get("SELECT ordername FROM records WHERE id = ?" , self
.id)
198 # Gain all information about records authentication.
200 def authentication(self
):
201 row
= self
.db
.get("SELECT auth FROM records WHERE id = ?" , self
.id)
205 # Create an own class to deal with "SOA" records.
206 """Use splitt() to generate a list of the original content string from the database, and return the requested entries."""
207 class SOARecord(Record
):
208 def __init__(self
, domain
, record_id
):
209 Record
.__init
__(self
, domain
, record_id
)
211 self
.soa_attrs
= self
.content
.split()
213 # Check if the content from database is valid (It contains all 7 required information).
214 if not len(self
.soa_attrs
) == 7:
215 #XXX Add something like an error message or log output.
218 # Primary NS - the domain name of the name server that was the original source of the data.
221 return self
.soa_attrs
[0]
223 # E-mail address of the person which is responsible for this domain.
226 return self
.soa_attrs
[1]
228 # The serial which increases allways after a change on the domain has been made.
231 return self
.soa_attrs
[2]
233 # The number of seconds between the time that a secondary name server gets a copy of the domain.
236 return self
.soa_attrs
[3]
238 # The number of seconds during the next refresh attempt if the previous fails.
241 return self
.soa_attrs
[4]
243 # The number of seconds that lets the secondary name server(s) know how long they can hold the information.
246 return self
.soa_attrs
[5]
248 # The number of seconds that the records in the domain are valid.
251 return self
.soa_attrs
[6]
255 class ARecord(Record
):