1 #!/bin/bash |
1 #!/bin/bash |
2 # vim: set ft=sh : |
2 # vim: set ft=sh : |
3 |
3 |
4 set -ue |
4 set -ue |
5 |
5 |
6 # resolve $0 -> bin/update |
6 if [ $0 == './update' ]; then |
7 self=$0 |
7 SRV=$(pwd) |
8 while [ -L $self ]; do |
8 OPT=./opt |
9 tgt=$(readlink $self) |
9 else |
|
10 SRV=${SRV:-/srv/dns} |
|
11 OPT=${SRV:-/srv/dns/opt} |
|
12 cd $SRV |
|
13 fi |
10 |
14 |
11 if [ "${tgt:0:1}" == "/" ]; then |
15 # charset for files under etc/ |
12 self=$tgt |
16 CHARSET='utf-8' |
13 else |
|
14 self=$(dirname $self)/$tgt |
|
15 fi |
|
16 done |
|
17 |
17 |
18 # root dir |
18 # External bins |
19 ROOT=$(dirname $(dirname $self)) |
19 NAMED_CHECKZONE=/usr/sbin/named-checkzone |
20 |
20 |
21 BIN=$ROOT/bin |
|
22 LIB=$ROOT/lib |
|
23 VAR=$ROOT/var |
|
24 |
|
25 ## Data paths |
|
26 # absolute path to data files; can be changed using -d |
|
27 ROOT=$(pwd) |
|
28 |
|
29 DATA=settings |
|
30 ZONES=$VAR/zones |
|
31 SERIALS=$VAR/serials |
|
32 |
|
33 DHCP=$VAR/dhcp |
|
34 DHCP_DATA=$DATA/dhcp |
|
35 |
|
36 # global DHCP conf to test |
|
37 DHCPD=/usr/sbin/dhcpd |
21 DHCPD=/usr/sbin/dhcpd |
38 DHCPD_CONF=/etc/dhcp/dhcpd.conf |
22 DHCPD_CONF=/etc/dhcp/dhcpd.conf |
39 DHCPD_INIT=/etc/init.d/isc-dhcp-server |
23 DHCPD_INIT=/etc/init.d/isc-dhcp-server |
40 |
|
41 # hg repo to commit |
|
42 REPO=$DATA |
|
43 |
|
44 ## Settings used in lib |
|
45 # Hide files under repo in commit diff output.. |
|
46 REPO_HIDE='*.serial' |
|
47 |
|
48 # data input charsets; arguments to ./bin/... python scripts |
|
49 HOSTS_FILE_ARGS='--input-charset utf-8' |
|
50 DHCP_FILE_ARGS='--input-charset utf-8' |
|
51 |
|
52 # External bins |
|
53 NAMED_CHECKZONE=/usr/sbin/named-checkzone |
|
54 |
24 |
55 HG=/usr/bin/hg |
25 HG=/usr/bin/hg |
56 HG_ARGS=(--config trusted.users=root) |
26 HG_ARGS=(--config trusted.users=root) |
57 |
27 |
58 RNDC=/usr/sbin/rndc |
28 RNDC=/usr/sbin/rndc |
59 |
|
60 # Path to rndc key, must be readable to run.. |
|
61 RNDC_KEY=/etc/bind/rndc.key |
29 RNDC_KEY=/etc/bind/rndc.key |
62 |
30 |
63 ## Library includes |
31 # Library includes |
64 # Command-line argument handling |
32 source lib/update |
65 source $LIB/update.args |
|
66 |
|
67 # Logging |
|
68 source $LIB/update.logging |
|
69 |
|
70 # Utility functions |
|
71 source $LIB/update.utils |
|
72 |
|
73 # Dependency-based updates |
|
74 source $LIB/update.updates |
|
75 |
|
76 # Operations; the functions called from run() |
|
77 source $LIB/update.operations |
|
78 |
33 |
79 ## Flags |
34 ## Flags |
80 # set by do_reload_zone if zone data has actually been reloaded |
35 # set by do_reload_zone if zone data has actually been reloaded |
81 RELOAD_ZONES= |
36 RELOAD_ZONES= |
82 |
37 |
83 ## Site settings, used as arguments to scripts |
38 ## Site settings, used as arguments to scripts |
84 # MX record to generate in hosts --forward-zone |
|
85 FORWARD_MX=mx0 |
|
86 |
|
87 # IP network to generate reverse records for in --reverse-zone |
|
88 REVERSE_ZONE=194.197.235 |
|
89 |
|
90 # Origin domain to generate reverse records for in --reverse-zone |
39 # Origin domain to generate reverse records for in --reverse-zone |
91 REVERSE_DOMAIN=paivola.fi |
40 REVERSE_DOMAIN=paivola.fi |
92 |
41 |
93 # Views used |
42 ## Do things |
94 VIEWS=(internal external) |
|
95 |
|
96 # Base domain zone for domains |
|
97 DOMAIN_BASE=paivola |
|
98 |
|
99 # List of actual domains used; will be linked to $DOMAIN_BASE |
|
100 DOMAINS=(paivola.fi paivola.net paivola.org paivola.info paivola.mobi xn--pivl-load8j.fi) |
|
101 |
|
102 # Names of dhcp conf file names |
|
103 DHCP_CONFS=( $(list_files $DHCP_DATA *.conf) ) |
|
104 |
|
105 ## Operate! |
|
106 # these functions are all defined in lib/update.operations |
|
107 |
|
108 # Update $ZONES/$DHCP host-files from $DATA |
|
109 function run_hosts { |
43 function run_hosts { |
110 ## Hosts |
|
111 # test |
44 # test |
112 log "Testing hosts..." |
45 log "Testing hosts..." |
113 # data args... |
46 for hosts in $(list_files etc/hosts); do |
114 check_hosts $DATA/paivola.txt --check-exempt ufc |
47 log_warn "TODO: check_hosts $hosts" |
|
48 done |
115 |
49 |
116 # update |
50 log "Updating host zones..." |
117 log "Generating host zones..." |
51 for hosts in $(list_files etc/hosts); do |
118 # hosts data args... |
52 update_hosts_forward var/zones/hosts/$hosts etc/hosts/$hosts |
119 update_hosts $ZONES/hosts/paivola:internal $DATA/paivola.txt --forward-zone --forward-txt |
53 done |
120 update_hosts $ZONES/hosts/paivola:external $DATA/paivola.txt --forward-zone |
|
121 update_hosts $ZONES/hosts/194.197.235 $DATA/paivola.txt --reverse-zone $REVERSE_ZONE --reverse-domain $REVERSE_DOMAIN |
|
122 |
54 |
123 |
55 log "Updating DHCP hosts..." |
124 update_hosts $ZONES/hosts/10 $DATA/pvl.txt --reverse-zone 10 --reverse-domain pvl -q |
56 for hosts in $(list_files etc/hosts); do |
125 update_hosts $ZONES/hosts/10.0 $DATA/test.pvl.txt --reverse-zone 10.0 --reverse-domain test.pvl -q |
57 update_hosts_dhcp var/dhcp/$hosts.conf etc/hosts/$hosts |
126 update_hosts $ZONES/hosts/fdc4:4cef:395a $DATA/test.pvl.txt --reverse-zone fdc4:4cef:395a --reverse-domain test.pvl -q |
58 done |
127 update_hosts $ZONES/hosts/192.168 $DATA/pvl.txt --reverse-zone 192.168 --reverse-domain pvl -q |
|
128 |
|
129 # XXX: unsupported --forward-zone with pvl.txt |
|
130 # update_hosts $ZONES/hosts/pvl $DATA/pvl.txt --forward-zone |
|
131 copy_hosts $ZONES/hosts/pvl $DATA/pvl.txt |
|
132 copy_hosts $ZONES/hosts/test.pvl $DATA/test.pvl.txt |
|
133 } |
59 } |
134 |
60 |
135 # Update $ZONES files |
|
136 function run_zones { |
61 function run_zones { |
137 ## Includes |
|
138 log "Copying zone includes..." |
62 log "Copying zone includes..." |
139 # view zone base |
63 for zone in $(list_files etc/zones/includes); do |
140 copy_zone includes paivola:internal paivola.zone.internal |
64 copy var/zones/includes/$zone etc/zones/includes/$zone |
141 copy_zone includes paivola:external paivola.zone.external |
65 done |
142 copy_zone includes paivola.auto paivola.zone.auto |
|
143 copy_zone includes paivola.services paivola.zone.services |
|
144 copy_zone includes paivola.aux paivola.zone.aux |
|
145 |
66 |
146 ## Serials |
67 log "Updating zone serials..." |
147 log "Updating serials..." |
68 for zone in $(list_files etc/zones); do |
|
69 update_serial var/serials/$zone etc/zones/$zone |
|
70 done |
148 |
71 |
149 # zone deps... |
72 log "Updating zones..." |
150 # includes... |
73 for zone in $(list_files etc/zones); do |
151 update_serial pvl $ZONES/hosts/pvl $DATA/pvl.zone |
74 update_zone var/zones/$zone etc/zones/$zone var/serials/$zone |
152 update_serial test.pvl $ZONES/hosts/test.pvl $DATA/test.pvl.zone |
75 done |
153 update_serial 10 $ZONES/hosts/10 $DATA/10.zone |
|
154 update_serial 10.0 $ZONES/hosts/10.0 $DATA/10.0.zone |
|
155 update_serial fdc4:4cef:395a $ZONES/hosts/fdc4:4cef:395a $DATA/fdc4:4cef:395a.zone |
|
156 update_serial 192.168 $ZONES/hosts/192.168 $DATA/192.168.zone |
|
157 |
76 |
158 update_serial paivola $ZONES/hosts/paivola:* $DATA/paivola.zone \ |
|
159 $ZONES/includes/paivola:* \ |
|
160 $ZONES/includes/paivola.* |
|
161 |
|
162 update_serial 194.197.235 $ZONES/hosts/194.197.235 $DATA/194.197.235.zone |
|
163 |
|
164 ## Zones |
|
165 log "Updating zones..." |
|
166 # view zone base |
|
167 update_zone internal pvl |
|
168 update_zone internal test.pvl |
|
169 |
|
170 update_zone internal 10 |
|
171 update_zone internal 10.0 |
|
172 update_zone internal fdc4:4cef:395a |
|
173 update_zone internal 192.168 |
|
174 |
|
175 update_zone common 194.197.235 |
|
176 link_zone internal 194.197.235 |
|
177 link_zone external 194.197.235 |
|
178 |
|
179 ## Test |
|
180 log "Testing zones..." |
77 log "Testing zones..." |
181 # view zone origin |
78 for zone in $(list_files etc/zones); do |
182 check_zone internal 10 10.in-addr.arpa |
79 # check_zone var/zones/$zone $zone |
183 check_zone internal 10.0 0.10.in-addr.arpa |
80 log_warn "TODO: check_zone $zone" |
184 check_zone internal fdc4:4cef:395a a.5.9.3.f.e.c.4.4.c.d.f.ip6.arpa |
81 done |
185 |
|
186 check_zone internal 192.168 192.168.in-addr.arpa |
|
187 check_zone common 194.197.235 235.197.194.in-addr.arpa |
|
188 |
|
189 ## Domains... |
|
190 log "Linking domains..." |
|
191 for view in "${VIEWS[@]}"; do |
|
192 for zone in "${DOMAINS[@]}"; do |
|
193 # choose input .zone to use |
|
194 base=$(choose_zone $zone $DOMAIN_BASE) |
|
195 |
|
196 if [ $base != $DOMAIN_BASE ]; then |
|
197 # serial |
|
198 # XXX: not all zones use all these includes? |
|
199 update_serial $base $DATA/$base.zone \ |
|
200 $ZONES/hosts/paivola:* \ |
|
201 $ZONES/includes/paivola:* \ |
|
202 $ZONES/includes/paivola.* |
|
203 fi |
|
204 |
|
205 # link |
|
206 update_zone $view $zone $base |
|
207 |
|
208 # test |
|
209 check_zone $view $zone $zone |
|
210 done |
|
211 done |
|
212 } |
|
213 |
|
214 # Update $DHCP files from $DATA/dhcp |
|
215 function run_dhcp { |
|
216 log_debug "DHCP_CONFS: ${DHCP_CONFS[*]}" |
|
217 |
|
218 log "Copying DHCP configs..." |
|
219 for conf in "${DHCP_CONFS[@]}"; do |
|
220 # XXX: ei toimi, koska conf:it riippuu toisistaan include:ien takia |
|
221 # check_dhcp_conf $conf |
|
222 |
|
223 # conf base |
|
224 copy_dhcp_conf $conf |
|
225 done |
|
226 |
|
227 log "Testing dhcp..." |
|
228 # checks the whole dhcpd.conf, with all includes.. |
|
229 check_dhcp |
|
230 } |
|
231 |
|
232 # Runs DHCP checks, once DNS hosts have been updated |
|
233 function run_dhcp_check { |
|
234 log "Testing dhcp hosts..." |
|
235 for conf in "${DHCP_CONFS[@]}"; do |
|
236 check_dhcp_hosts $DHCP/$conf.conf |
|
237 done |
|
238 } |
82 } |
239 |
83 |
240 function run_deploy { |
84 function run_deploy { |
241 ## Reload zones |
|
242 log "Reload zones..." |
85 log "Reload zones..." |
243 reload_zones |
86 reload_zones |
244 |
|
245 ## DHCP |
|
246 run_dhcp_check |
|
247 |
87 |
248 log "Reload dhcp..." |
88 log "Reload dhcp..." |
249 reload_dhcp |
89 reload_dhcp |
250 |
90 |
251 ## Commit |
91 log "Commit etc..." |
252 log "Commit data..." |
92 commit_data etc |
253 commit_data |
|
254 } |
93 } |
255 |
94 |
256 ## Main entry point |
95 ## Main entry point |
257 function main { |
96 function main { |
258 # test tty |
|
259 [ -t 1 ] && IS_TTY=y |
|
260 |
|
261 parse_args "$@" |
97 parse_args "$@" |
262 |
98 |
263 ## Input dirs |
99 ## Input dirs |
264 [ -d $ROOT/$DATA ] || die "Missing data: $ROOT/$DATA" |
100 for dir in etc etc/dhcp etc/zones; do |
|
101 [ -d $dir ] || die "Missing directory: $dir" |
|
102 done |
265 |
103 |
266 ## Output dirs |
104 ## Output dirs |
267 for dir in $VAR $DHCP $ZONES $SERIALS; do |
105 for dir in var var/dhcp var/zones var/serials; do |
268 ensure_dir $dir |
106 ensure_dir $dir |
269 done |
107 done |
270 |
108 |
271 # sub-$ZONES |
109 # sub-$ZONES |
272 for dir in "common" "hosts" "includes" "${VIEWS[@]}"; do |
110 for dir in var/zones/hosts var/zones/includes; do |
273 ensure_dir $ZONES/$dir |
111 ensure_dir $dir |
274 done |
112 done |
275 |
113 |
276 ## Go |
114 ## Go |
277 run_hosts |
115 run_hosts |
278 run_zones |
116 run_zones |
279 run_dhcp |
|
280 run_deploy |
117 run_deploy |
281 } |
118 } |
282 |
119 |
283 main "$@" |
120 main "$@" |