]> git.ipfire.org Git - people/stevee/pypdns.git/blame - backend.py
Fix different spelling of tool name.
[people/stevee/pypdns.git] / backend.py
CommitLineData
1336cd4e
SS
1###############################################################################
2# #
3# pyPDNS - A PDNS administration tool, written in pure python. #
9deb9e05 4# Copyright (C) 2012 IPFire development team #
1336cd4e
SS
5# #
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. #
10# #
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. #
15# #
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/>. #
18# #
19###############################################################################
20# #
21# More details about the database tables and fields can be found here: #
22# http://wiki.powerdns.com/trac/wiki/fields #
23# #
24###############################################################################
25
26import database
27
28DB = "/var/lib/pdns/pdns.db"
29
30# Create the primary DNS class.
31"""Use Database class from imported database module to connect to the PDNS sqlite database."""
32class DNS(object):
33 def __init__(self, db):
34 self.db = database.Database(db)
35
36 # Get all configured domains.
37 def get_domains(self):
38 domains = []
39
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)
44
45 return domains
46
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)
1336cd4e 50
d267bece
SS
51 # Only do anything, if there is an existing domain.
52 if row:
53 domain = Domain(self, row.id)
54
55 return domain
1336cd4e
SS
56
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."""
60class Domain(object):
61 def __init__(self, dns, domain_id):
62 self.dns = dns
63 self.id = domain_id
64
65 @property
66 def db(self):
67 return self.dns.db
68
69 # Determine the name of the zone by a given id.
70 @property
71 def name(self):
72 row = self.db.get("SELECT name FROM domains WHERE id = ?", self.id)
73 return row.name
74
75 # Get information of the master nameserver from which the domain should be slaved.
76 @property
77 def master(self):
78 row = self.db.get("SELECT master FROM domains WHERE id = ?", self.id)
79 return row.master
80
81 # Fetch data of the last check from the domain.
82 @property
83 def last_check(self):
84 row = self.db.get("SELECT last_check FROM domains WHERE id = ?", self.id)
85 return row.last_check
86
87 # Get the type of the domain.
88 @property
89 def type(self):
90 row = self.db.get("SELECT type FROM domains WHERE id = ?", self.id)
91 return row.type
92
93 # Get the last notified serial of a used master domain.
94 @property
95 def notified_serial(self):
96 row = self.db.get("SELECT notified_serial FROM domains WHERE id = ?", self.id)
97 return row.notified_serial
98
99 # Gain if a certain host is a supermaster for a certain domain name.
100 @property
101 def account(self):
102 row = self.db.get("SELECT account FROM domains WHERE id = ?", self.id)
103 return row.account
104
105 # Get all records from zone.
106 @property
107 def records(self):
108 records = []
109
110 """Fetch all records from domain, line by line and add them to the previous created empty list."""
abccf64a
SS
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)
116 else:
117 record = Record(self, row.id)
1336cd4e
SS
118 records.append(record)
119
120 return records
121
abccf64a
SS
122 # Get records by a specified type.
123 def get_records_by_type(self, type):
124 records = []
125 for record in self.records:
126 if record.type == type:
127 records.append(record)
128
129 return records
130
131 # Quick function to get the first SOA record from the domain.
132 @property
133 def SOA(self):
134 records = self.get_records_by_type("SOA")
135 if records:
136 return records[0]
1336cd4e
SS
137
138
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."""
142class Record(object):
143 def __init__(self, domain, record_id):
144 self.domain = domain
145 self.id = record_id
146
147 @property
148 def db(self):
149 return self.domain.db
150
151 # Determine the type of the record.
152 @property
153 def type(self):
154 row = self.db.get("SELECT type FROM records WHERE id = ?", self.id)
155 return row.type
156
157 # Get the configured DNS name of the record.
158 @property
159 def dnsname(self):
160 row = self.db.get("SELECT name FROM records WHERE id = ?", self.id)
161 return row.name
162
163
164 # Fetch content like the address to which the record points.
165 @property
166 def content(self):
167 row = self.db.get("SELECT content FROM records WHERE id = ?", self.id)
168 return row.content
169
170
171 # Get the "Time to live" for the record.
172 @property
173 def ttl(self):
174 row = self.db.get("SELECT ttl FROM records WHERE id = ?", self.id)
175 return row.ttl
176
177 # Gain the configured record priority.
178 @property
179 def priority(self):
180 row = self.db.get("SELECT prio FROM records WHERE id = ?" , self.id)
181 return row.prio
182
183 # Get the change_date.
184 @property
185 def change_date(self):
186 row = self.db.get("SELECT change_date FROM records WHERE id = ?" , self.id)
187 return row.change_date
188
189 # Fetch the ordername.
190 @property
191 def ordername(self):
192 row = self.db.get("SELECT ordername FROM records WHERE id = ?" , self.id)
193 return row.ordername
194
abccf64a 195 # Gain all information about records authentication.
1336cd4e 196 @property
abccf64a 197 def authentication(self):
1336cd4e
SS
198 row = self.db.get("SELECT auth FROM records WHERE id = ?" , self.id)
199 return row.auth
abccf64a
SS
200
201
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."""
204class SOARecord(Record):
205 def __init__(self, domain, record_id):
206 Record.__init__(self, domain, record_id)
207
208 self.soa_attrs = self.content.split()
209
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.
213 pass
214
215 # Primary NS - the domain name of the name server that was the original source of the data.
216 @property
217 def mname(self):
218 return self.soa_attrs[0]
219
220 # E-mail address of the person which is responsible for this domain.
221 @property
222 def email(self):
223 return self.soa_attrs[1]
224
225 # The serial which increases allways after a change on the domain has been made.
226 @property
227 def serial(self):
228 return self.soa_attrs[2]
229
230 # The number of seconds between the time that a secondary name server gets a copy of the domain.
231 @property
232 def refresh(self):
233 return self.soa_attrs[3]
234
235 # The number of seconds during the next refresh attempt if the previous fails.
236 @property
237 def retry(self):
238 return self.soa_attrs[4]
239
240 # The number of seconds that lets the secondary name server(s) know how long they can hold the information.
241 @property
242 def expire(self):
243 return self.soa_attrs[5]
244
245 # The number of seconds that the records in the domain are valid.
246 @property
247 def minimum(self):
248 return self.soa_attrs[6]
249
250
251
252class ARecord(Record):
253 pass
254