-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpdns.sql
204 lines (178 loc) · 6.44 KB
/
pdns.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
create extension citext;
drop table if exists domains cascade;
create table domains (
id INTEGER PRIMARY KEY,
name citext,
master text,
last_check INTEGER DEFAULT NULL,
type text,
notified_serial INTEGER DEFAULT NULL,
account text
);
drop sequence domain_id_seq;
create sequence domain_id_seq start 1;
CREATE UNIQUE INDEX name_index ON domains(name);
drop table if exists records cascade;
CREATE TABLE records (
id INTEGER PRIMARY KEY,
domain_id INTEGER DEFAULT NULL,
name text,
type text,
content text,
ttl INTEGER DEFAULT NULL,
prio INTEGER DEFAULT NULL,
change_date INTEGER DEFAULT NULL
);
CREATE INDEX rec_name_index ON records(name);
CREATE INDEX nametype_index ON records(name,type);
CREATE INDEX domain_id ON records(domain_id);
drop sequence record_id_seq;
create sequence record_id_seq start 1;
drop table if exists supermasters cascade;
create table supermasters (
ip text,
nameserver citext,
account text
);
GRANT SELECT ON supermasters TO pdns;
GRANT ALL ON domains TO pdns;
GRANT ALL ON records TO pdns;
-- Define the domain
create or replace function create_domain(_name text, _type text, _account text) returns boolean as $$
begin
INSERT INTO domains (id,name,type,account) VALUES (nextval('domain_id_seq'),_name,_type,_account);
return found;
end
$$ language plpgsql;
-- SOA records
create or replace function create_soa( _domain text, _content text) returns boolean as $$
declare
_domain_id integer;
begin
select into _domain_id id from domains where name = _domain;
insert into records (id, domain_id, name,type,content,ttl) values ( nextval('record_id_seq'), _domain_id, _domain, 'SOA', _content, 3600);
return found;
end
$$ language plpgsql;
create or replace function delete_soa( _domain text ) returns boolean as $$
declare
_domain_id integer;
begin
select into _domain_id id from domains;
delete from records where domain_id = id and type = 'SOA';
return found;
end
$$ language plpgsql;
-- A records
create or replace function create_a( _domain text, _host text, _ipaddr text) returns boolean as $$
declare
_domain_id integer;
begin
perform delete_a(_domain,_host,_ipaddr);
select into _domain_id id from domains where name = _domain;
insert into records(id,domain_id, name,type, content, ttl) values (nextval('record_id_seq'), _domain_id, _host || '.' || _domain, 'A', _ipaddr, 3600);
return found;
end
$$ language plpgsql;
create or replace function delete_a( _domain text, _host text, _ipaddr text) returns boolean as $$
declare
_domain_id integer;
begin
select into _domain_id id from domains where name = _domain;
delete from records where domain_id = _domain_id and name = _host || '.' || _domain and content = _ipaddr and type = 'A';
return found;
end
$$ language plpgsql;
-- NS records
create or replace function create_ns( _domain text, _host text) returns boolean as $$
declare
_domain_id integer;
begin
perform delete_ns(_domain,_host);
select into _domain_id id from domains where name = _domain;
insert into records(id,domain_id, name,type, content, ttl) values (nextval('record_id_seq'), _domain_id, _domain, 'NS', _host || '.' || _domain, 3600);
return found;
end
$$ language plpgsql;
create or replace function delete_ns( _domain text, _host text) returns boolean as $$
declare
_domain_id integer;
begin
select into _domain_id id from domains where name = _domain;
delete from records where domain_id = domain_id and type = 'NS' and content = _host || '.' || _domain;
return found;
end
$$ language plpgsql;
-- CNAME records
create or replace function create_cname( _domain text, _host text, _alias text ) returns boolean as $$
declare
_domain_id integer;
begin
perform delete_cname(_domain,_host,_alias);
select into _domain_id id from domains where name = _domain;
insert into records(id,domain_id,name,type,content,ttl) values (nextval('record_id_seq'), _domain_id, _host || '.' || _domain , 'CNAME', _alias, 3600);
return found;
end
$$ language plpgsql;
create or replace function delete_cname( _domain text, _host text, _alias text) returns boolean as $$
declare
_domain_id integer;
begin
select into _domain_id id from domains where name = _domain;
delete from records where domain_id = _domain_id and name = _host || '.' || _domain and content = _alias and type = 'CNAME';
return found;
end
$$ language plpgsql;
-- TXT records
-- http://www.ietf.org/rfc/rfc1464.txt
create or replace function create_txt( _domain text, _host text, _txt text ) returns boolean as $$
declare
_domain_id integer;
begin
perform delete_txt(_domain,_host,_txt);
select into _domain_id id from domains where name = _domain;
insert into records(id,domain_id,name,type,content,ttl) values (nextval('record_id_seq'), _domain_id, _host || '.' || _domain, 'TXT', _txt, 3600);
return found;
end
$$ language plpgsql;
create or replace function delete_txt( _domain text, _host text, _txt text ) returns boolean as $$
declare
_domain_id integer;
begin
select into _domain_id id from domains where name = _domain;
delete from records where domain_id = _domain_id and name = _host || '.' || _domain and content = _txt and type = 'TXT';
return found;
end
$$ language plpgsql;
-- SRV records
-- https://www.ietf.org/rfc/rfc2782.txt
create or replace function create_srv( _domain text, _host text, _txt text ) returns boolean as $$
declare
_domain_id integer;
begin
perform delete_srv(_domain,_host,_txt);
select into _domain_id id from domains where name = _domain;
insert into records(id,domain_id,name,type,content,ttl) values (nextval('record_id_seq'), _domain_id, _host || '.' || _domain, 'SRV', _txt, 3600);
return found;
end
$$ language plpgsql;
create or replace function delete_srv( _domain text, _host text, _txt text ) returns boolean as $$
declare
_domain_id integer;
begin
select into _domain_id id from domains where name = _domain;
delete from records where domain_id = _domain_id and name = _host || '.' || _domain and content = _txt and type = 'SRV';
return found;
end
$$ language plpgsql;
-- list domain
create or replace function list_domain(_domain text) returns json as $$
declare
_domain_id integer;
_json json;
begin
select into _domain_id id from domains where name = _domain;
select into _json array_to_json(array_agg(foo)) as domain from ( select name,type,content from records where domain_id = _domain_id) as foo;
return _json;
end
$$ language plpgsql;