forked from vedetta-com/dithematic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzoneadd
219 lines (190 loc) · 5.89 KB
/
zoneadd
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/bin/sh
# Add a zone: env DDNS=false zoneadd name
set -o errexit
set -o nounset
# Bail out if non-privileged UID
[ 0 -eq "$(id -u)" ] || exit 1
GH_PROJECT="dithematic"
PREFIX="/usr/local"
DOCDIR="${PREFIX}/share/doc/${GH_PROJECT}"
EXAMPLESDIR="${PREFIX}/share/examples/${GH_PROJECT}"
BASESYSCONFDIR="/etc"
VARBASE="/var"
ZONE_NAME="$1"
NSD_DIR="${VARBASE}/nsd"
ZONE_DIR="${NSD_DIR}/zones/master"
KEY_DIR="${KEY_DIR:-/etc/ssl/dns}"
KEY_HMAC="${2:-hmac-sha512}"
DDNS="${DDNS:-false}"
DOMAIN=$(hostname | sed "s/$(hostname -s)\.//")
# New keys if missing
KEY_NAME="${KEY_NAME:-tsig.${ZONE_NAME}}"
CSK="${CSK:-${ZONE_NAME}.CSK}"
DNSKEY="${DNSKEY:-${ZONE_NAME}.DNSKEY}"
# Role: master or slave
grep "^master=yes" ${BASESYSCONFDIR}/pdns/pdns.conf &&
MASTER="${MASTER:-true}" ||
MASTER="${MASTER:-false}"
# Dithematic IP
MASTER_IP="${MASTER_IP:-\
203.0.113.3 \
2001:0db8::3 \
}"
SLAVE_IP="${SLAVE_IP:-\
203.0.113.4 \
2001:0db8::4 \
}" # empty to disable
# Vendor
FREE_SLAVE="${FREE_SLAVE:-\
1984.is \
FreeDNS.afraid.org \
GratisDNS.com \
HE.net \
Puck.nether.net \
}" # empty to disable
umask 137
# NSD
#
# Old zone
ls -l ${VARBASE}/nsd/etc/nsd.conf.zone.${ZONE_NAME} && exit 1
# New zone
printf "%s\n" "zone:" > ${VARBASE}/nsd/etc/nsd.conf.zone.${ZONE_NAME}
printf "\t%s\n" \
"name: \"${ZONE_NAME}.\"" \
"zonefile: \"slave/${ZONE_NAME}.zone\"" \
"notify-retry: 2" \
>> ${VARBASE}/nsd/etc/nsd.conf.zone.${ZONE_NAME}
if "${MASTER}"
then
cp ${DOCDIR}/nsd.conf.master.PowerDNS ${VARBASE}/nsd/etc/
printf "\t%s\n" \
"# Super-Master" \
"include: ${VARBASE}/nsd/etc/nsd.conf.master.PowerDNS" \
>> ${VARBASE}/nsd/etc/nsd.conf.zone.${ZONE_NAME}
[ -n "${SLAVE_IP}" ] &&
printf "\t%s\n" \
"# Super Slave(s)" \
"include: ${VARBASE}/nsd/etc/nsd.conf.slave.${ZONE_NAME}" \
>> ${VARBASE}/nsd/etc/nsd.conf.zone.${ZONE_NAME}
[ -n "${FREE_SLAVE}" ] &&
printf "\t%s\n" \
"# Free Slave(s)" \
>> ${VARBASE}/nsd/etc/nsd.conf.zone.${ZONE_NAME}
for _FREE_SLAVE in ${FREE_SLAVE}
do
cp ${DOCDIR}/nsd.conf.slave.${_FREE_SLAVE} ${VARBASE}/nsd/etc/
printf "\t%s\n" \
"include: ${VARBASE}/nsd/etc/nsd.conf.slave.${_FREE_SLAVE}" \
>> ${VARBASE}/nsd/etc/nsd.conf.zone.${ZONE_NAME}
done
else
cp ${DOCDIR}/nsd.conf.slave.PowerDNS ${VARBASE}/nsd/etc/
printf "\t%s\n" \
"# Super Slave" \
"include: ${VARBASE}/nsd/etc/nsd.conf.slave.PowerDNS" \
"# Super Master(s)" \
"#multi-master-check: yes" \
"include: ${VARBASE}/nsd/etc/nsd.conf.master.${ZONE_NAME}" \
>> ${VARBASE}/nsd/etc/nsd.conf.zone.${ZONE_NAME}
fi
if "${MASTER}"
then
printf "\t%s\n" \
"# ${ZONE_NAME} slave" \
> ${VARBASE}/nsd/etc/nsd.conf.slave.${ZONE_NAME}
for _SLAVE_IP in ${SLAVE_IP}
do
printf "\t%s\n" \
"provide-xfr: ${_SLAVE_IP} tsig.${DOMAIN}." \
"notify: ${_SLAVE_IP} tsig.${DOMAIN}." \
>> ${VARBASE}/nsd/etc/nsd.conf.slave.${ZONE_NAME}
grep ${_SLAVE_IP} ${BASESYSCONFDIR}/pf.conf.table.dns ||
printf "%s\n" "${_SLAVE_IP}" >> ${BASESYSCONFDIR}/pf.conf.table.dns
done
else
printf "\t%s\n" \
"# ${ZONE_NAME} master" \
> ${VARBASE}/nsd/etc/nsd.conf.master.${ZONE_NAME}
for _MASTER_IP in ${MASTER_IP}
do
printf "\t%s\n" \
"request-xfr: AXFR ${_MASTER_IP} tsig.${DOMAIN}." \
"allow-notify: ${_MASTER_IP} tsig.${DOMAIN}." \
>> ${VARBASE}/nsd/etc/nsd.conf.master.${ZONE_NAME}
grep ${_MASTER_IP} ${BASESYSCONFDIR}/pf.conf.table.dns ||
printf "%s\n" "${_MASTER_IP}" >> ${BASESYSCONFDIR}/pf.conf.table.dns
done
fi
# pf table "dns"
pfctl -t dns -T replace -f ${BASESYSCONFDIR}/pf.conf.table.dns
# Include zone configuration
grep "^include: ${VARBASE}/nsd/etc/nsd.conf.zone.${ZONE_NAME}" \
${VARBASE}/nsd/etc/nsd.conf ||
printf "%s\n" \
"include: ${VARBASE}/nsd/etc/nsd.conf.zone.${ZONE_NAME}" \
>> ${VARBASE}/nsd/etc/nsd.conf
# Reload
rcctl reload nsd
# PowerDNS
#
# Old zone
pdnsutil list-zone ${ZONE_NAME} && exit 1
# Load zone template from ${VARBASE}/nsd/zones/master/${ZONE_NAME}.zone
[ -r ${ZONE_DIR}/${ZONE_NAME}.zone ] &&
pdnsutil load-zone ${ZONE_NAME} ${ZONE_DIR}/${ZONE_NAME}.zone ||
pdnsutil create-zone ${ZONE_NAME}
# DNSSEC
#
if "${MASTER}"
then
[ -r ${KEY_DIR}/private/${CSK} ] ||
(umask 077; pdnsutil generate-zone-key KSK ecdsa256 | sed '/Flags/d' \
> ${KEY_DIR}/private/${CSK})
pdnsutil set-nsec3 ${ZONE_NAME} "1 0 333 $(nsec3salt)" inclusive
pdnsutil import-zone-key ${ZONE_NAME} ${KEY_DIR}/private/${CSK}
local \
_id=$(pdnsutil list-keys ${ZONE_NAME} | awk -v name=${ZONE_NAME} '$0 ~ name { print $5 }')
pdnsutil activate-zone-key ${ZONE_NAME} "${_id}"
fi
# TSIG
#
if [ "${ZONE_NAME}" = "${DOMAIN}" -a "${MASTER}" ]
then
[ -r ${KEY_DIR}/private/${KEY_NAME} ] || tsig-secret ${KEY_NAME}
tsig-change ${KEY_NAME}
fi
if [ "${DDNS}" -a "${MASTER}" ]
then
[ -r ${KEY_DIR}/private/${KEY_NAME} ] || tsig-secret ${KEY_NAME}
pdnsutil import-tsig-key \
${KEY_NAME} ${KEY_HMAC} $(<${KEY_DIR}/private/${KEY_NAME})
fi
pdnsutil activate-tsig-key ${ZONE_NAME} tsig.${DOMAIN} master
pdnsutil activate-tsig-key ${ZONE_NAME} tsig.${DOMAIN} slave
# Master or Slave
if "${MASTER}"
then
pdnsutil set-kind ${ZONE_NAME} master
if ${DDNS}
then
pdnsutil set-meta ${ZONE_NAME} ALLOW-DNSUPDATE-FROM 0.0.0.0/0,::/0
pdnsutil set-meta ${ZONE_NAME} TSIG-ALLOW-DNSUPDATE ${KEY_NAME}
pdnsutil set-meta ${ZONE_NAME} NOTIFY-DNSUPDATE 1
fi
else
pdnsutil set-kind ${ZONE_NAME} slave
pdnsutil change-slave-zone-master ${ZONE_NAME} 127.0.0.1:10053 [::1]:10053
pdnsutil unset-presigned ${ZONE_NAME}
fi
pdnsutil set-meta ${ZONE_NAME} SOA-EDIT-DNSUPDATE SOA-EDIT-INCREASE
# Finish
#
if "${MASTER}"
then
pdnsutil export-zone-dnskey ${ZONE_NAME} "${_id}" > ${KEY_DIR}/${DNSKEY}
echo Send the DNSKEY to registrar:
cat ${KEY_DIR}/${DNSKEY}
fi
pdnsutil rectify-zone ${ZONE_NAME}
env EDITOR="${EDITOR:-vi}" pdnsutil edit-zone ${ZONE_NAME}
echo Add ${ZONE_NAME} to all nameservers and share the master key: tsig-share tsig.${DOMAIN}