]> gitweb.michael.orlitzky.com - mjo-overlay.git/blob - net-dns/djbdns/files/djbdns-setup
Add net-dns containing djbdns.
[mjo-overlay.git] / net-dns / djbdns / files / djbdns-setup
1 #!/bin/bash
2 #
3 # djbdns-setup
4 #
5 # Copyright (C) 2004 Kalin Kozhuharov <kalin@ThinRope.net>
6 #
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License
9 # as published by the Free Software Foundation; either version 2
10 # of the License, or (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 # http://www.gnu.org/copyleft/gpl.html
17 #
18
19 . /sbin/functions.sh
20 # void ewarn(char* message)
21 #
22 # show a warning message and do NOT log it
23 ewarn() {
24 if [ "${RC_QUIET_STDOUT}" = "yes" ]
25 then
26 echo " ${*}"
27 else
28 echo -e " ${WARN}*${NORMAL} ${*}"
29 fi
30
31 return 0
32 }
33
34 S_SEPARATOR="--------------------------------------------------------------------------------"
35 D_SEPARATOR="================================================================================"
36
37 REQ_GROUP="nofiles"
38 REQ_USERS="tinydns dnscache dnslog"
39
40 IPs[0]=""
41 IPs[1]=""
42 IPs[2]=""
43 dnscache=0
44 tinydns=1
45 axfrdns=2
46
47 check_group_users()
48 {
49 echo ": Checking for required group (${REQ_GROUP}) :"
50 grep ${REQ_GROUP} /etc/group &> /dev/null
51 if [ $? -ne 0 ]
52 then
53 ebegin "Adding group ${REQ_GROUP}"
54 /usr/sbin/groupadd ${REQ_GROUP} &>/dev/null && eend 0 || eend 1
55 fi
56
57 echo ": Checking for required users (${REQ_USERS}) :"
58 for user in ${REQ_USERS};
59 do
60 grep ${user} /etc/passwd &> /dev/null
61 if [ $? -ne 0 ]
62 then
63 ebegin "Adding user ${user}"
64 /usr/sbin/useradd -d /dev/null -s /bin/false -g ${REQ_GROUP} ${user} &>/dev/null && eend 0 || eend 1
65 fi
66 done
67 return 0
68 }
69
70 start_services()
71 {
72 local services="$1"
73
74 echo "${SEPARATOR}"
75 echo ": Start services :"
76 echo
77 echo " Your services (${services// /, }) are ready for startup!"
78 echo
79 ewarn " The following requires daemontools to be running!"
80 local answer=""
81 read -p " Would you like ${services// /, } to be started and supervised by daemontools now? [Y|n]> " answer
82 if [ "${answer}" == "Y" ] || [ "${answer}" == "" ]
83 then
84
85 ebegin "Checking if daemontools are running"
86 ps -A |grep svscanboot &>/dev/null && eend 0 || eend 1
87
88 ebegin "Linking services in /service"
89 # Don't make symbolic links to / !
90 # use ../ instead as it gives trouble in chrooted environments
91 local fixedroot_path=`echo ${mypath} | sed -e 's#^/#../#'`
92 for service in ${services};
93 do
94 for ip in ${IPs[${service}]};
95 do
96 ln -sf ${fixedroot_path}/${service}/${ip} /service/${service}_${ip}
97 done
98 done
99
100 eend 0
101
102 echo
103 ls -l --color=auto /service/
104 echo
105 ebegin "Waiting 5 seconds for services to start"
106 sleep 5 && eend 0
107
108 echo "${SEPARATOR}"
109 echo ": Check services status :"
110 echo
111 for service in ${services};
112 do
113 for ip in ${IPs[${service}]};
114 do
115 svstat /service/${service}_${ip} /service/${service}_${ip}/log
116 done
117 done
118 fi
119 return 0
120 }
121
122 tinydns_setup()
123 {
124 return 0
125 }
126
127 axfrdns_setup()
128 {
129 echo "${S_SEPARATOR}"
130 echo ": Grant access to axfrdns :"
131 echo
132 TCPRULES_DIR="${mypath}/axfrdns/${myip}/control"
133 echo " axfrdns is accessed by your secondary servers and when response cannot fit UDP packet"
134 echo " You have to specify their which IP addresses are allowed to access it"
135 echo " in ${TCPRULES_DIR}/tcp.axfrdns"
136 echo
137 echo " Example:"
138 echo " 1.2.3.4 would allow the host 1.2.3.4"
139 echo " 1.2.3. would allow ALL hosts 1.2.3.x (like 1.2.3.4, 1.2.3.100, etc.)"
140 ewarn "Do NOT forget the trailing dot!"
141 echo
142 echo " Press Enter if you do not want to allow any access now."
143 echo
144
145 sed -i -e "s#-x tcp.cdb#-x control/tcp.axfrdns.cdb#g" ${mypath}/axfrdns/${myip}/run
146 if [ -e ${TCPRULES_DIR}/tcp.axfrdns ]
147 then
148 ewarn "${TCPRULES_DIR}/tcp.axfrdns exists."
149 read -p " Do you want it cleared? [Y|n]: " answer
150 if [ "${answer}" == "Y" ] || [ "${answer}" == "" ]
151 then
152 echo '# sample line: 1.2.3.4:allow,AXFR="heaven.af.mil/3.2.1.in-addr.arpa"' > ${TCPRULES_DIR}/tcp.axfrdns
153 fi
154 fi
155
156 read -p " IP to allow (press Enter to end)> " ipallow
157
158 while [ "$ipallow" != "" ]
159 do
160 echo "${ipallow}:allow" >> ${TCPRULES_DIR}/tcp.axfrdns
161 read -p " IP to allow (press Enter to end)> " ipallow
162 done
163 echo ":deny" >> ${TCPRULES_DIR}/tcp.axfrdns
164
165 echo " Here is the tcprules file created so far:"
166 echo
167 cat ${TCPRULES_DIR}/tcp.axfrdns
168 echo
169 local answer=""
170 read -p " Would you like ${TCPRULES_DIR}/tcp.axfrdns.cdb updated? [Y|n]: " answer
171 if [ "${answer}" == "Y" ] || [ "${answer}" == "" ]
172 then
173 ebegin "Updating ${TCPRULES_DIR}/tcp.axfrdns.cdb"
174 bash -c "cd ${TCPRULES_DIR} && make" && eend 0 || eend 1
175 fi
176 return 0
177 }
178
179 dnscache_setup()
180 {
181 echo ": Configure forwarding :"
182 echo
183 echo " dnscache can be configured to forward queries to another DNS cache"
184 echo " (such as the one your ISP provides) rather than perform the lookups itself."
185 echo
186 echo " To enable this forwarding-only mode (a good idea most of the time),"
187 echo " provide the IPs of the caches to forward to."
188 echo " To have dnscache perform the lookups itself, just press Enter."
189 echo
190 read -p " forward-to IP> " myforward
191 echo
192 if [ "$myforward" != "" ]
193 then
194 echo $myforward > ${mypath}/dnscache/${myip}/root/servers/\@
195 echo -n "1" > ${mypath}/dnscache/${myip}/env/FORWARDONLY
196
197 read -p " forward-to IP (press Enter to end)> " myforward
198 while [ "$myforward" != "" ]
199 do
200 echo $myforward >> ${mypath}/dnscache/${myip}/root/servers/\@
201 read -p " forward-to IP (press Enter to end)> " myforward
202 done
203
204 echo
205 echo " Currently all queries will be forwarded to:"
206 echo
207 cat ${mypath}/dnscache/${myip}/root/servers/\@
208 echo
209 fi
210
211 echo "${SEPARATOR}"
212 echo ": Configuring clients :"
213 echo
214 echo " By default dnscache allows only localhost (127.0.0.1) to access it."
215 echo " You have to specify the IP addresses of the clients that shall be allowed to use it."
216 echo
217 echo " Example:"
218 echo " 1.2.3.4 would allow only the host 1.2.3.4"
219 echo " 1.2.3 would allow all hosts 1.2.3.x (like 1.2.3.4, 1.2.3.100, etc.)"
220 echo
221 echo " Press Enter if you do not want to allow external clients!"
222 echo
223
224 read -p " Allowed IP> " myclientip
225
226 while [ "$myclientip" != "" ]
227 do
228 touch ${mypath}/dnscache/${myip}/root/ip/${myclientip}
229 read -p " Allowed IP (press Enter to end)> " myclientip
230 done
231
232 echo
233 echo " All queries from the hosts below will be answered:"
234 echo
235 ls -1 ${mypath}/dnscache/${myip}/root/ip
236 echo
237
238 #TODO
239 #configure cachsize - $mypath/env/CACHESIZE
240
241 #TODO
242 #configure datalimit - $mypath/env/DATALIMIT
243 return 0
244 }
245
246 common_setup()
247 {
248 local service_human="$1"
249 local service_machine="$2"
250 local services="$3"
251
252 echo ": ${service_human} setup :"
253 echo
254
255 for service in ${services};
256 do
257 if [ ! -e ${mypath}/${service} ]
258 then
259 ebegin "Creating ${mypath}/${service}"
260 mkdir -p $mypath/${service} && eend 0 || eend 1
261 fi
262 done
263
264 echo "${SEPARATOR}"
265 echo ": IP address to bind to :"
266 echo
267 echo " Specify an address to which the ${service_human} should bind."
268 echo " Currently accessible IPs:"
269 local addrs=`ifconfig -a | grep "inet addr" | cut -f2 -d":" | cut -f1 -d" "`
270 echo " "$addrs
271 echo
272
273 while [ "${myip}" == "" ]
274 do
275 read -p " IP to bind to> " myip
276 done
277 echo
278
279 for service in ${services};
280 do
281 IPs[${service}]="${IPs[${service}]} ${myip}"
282 done
283
284 local dnscache_INSTALL="/usr/bin/dnscache-conf dnscache dnslog ${mypath}/dnscache/${myip} $myip"
285 local tinydns_INSTALL="/usr/bin/tinydns-conf tinydns dnslog ${mypath}/tinydns/${myip} $myip"
286 local axfrdns_INSTALL="\
287 /usr/bin/axfrdns-conf tinydns dnslog ${mypath}/axfrdns/${myip} ${mypath}/tinydns/${myip} $myip &&\
288 mkdir -p ${mypath}/axfrdns/${myip}/control &&\
289 echo -e \"tcp.axfrdns.cdb:\ttcp.axfrdns\n\ttcprules tcp.axfrdns.cdb .tcp.axfrdns.cdb.tmp < tcp.axfrdns\" > ${mypath}/axfrdns/${myip}/control/Makefile &&\
290 rm -f ${mypath}/axfrdns/${myip}/tcp ${mypath}/axfrdns/${myip}/Makefile"
291
292 for service in ${services};
293 do
294 if [ ! -e ${mypath}/${service}/${myip} ]
295 then
296 ebegin "Setting up ${service} in ${mypath}/${service}/${myip}"
297 eval command=\$${service}_INSTALL
298 /bin/bash -c "${command}" && eend 0 || eend 1
299 else
300 ewarn "${service} directory ${mypath}/${service}/${myip} exists, nothing done."
301 fi
302 done
303
304 }
305
306
307
308
309
310
311 if [ `id -u` -ne 0 ]
312 then
313 eerror "${0}: You must be root."
314 exit 1
315 else
316
317 echo "${D_SEPARATOR}"
318 echo ": DJB DNS setup :"
319 echo
320 echo " This script will help you setup the following:"
321 echo
322 echo " DNS server(s): to publish addresses of Internet hosts"
323 echo
324 echo " DNS cache(s) : to find addresses of Internet hosts"
325 echo
326 echo " For further information see:"
327 echo " http://cr.yp.to/djbdns/blurb/overview.html"
328 echo
329 ewarn "If you have already setup your services,"
330 ewarn "either exit now, or setup in different directories."
331 echo
332
333 answer=""
334 read -p " Would you like to continue with setup? [Y|n]> " answer
335 if [ "${answer}" == "n" ] || [ "${answer}" == "N" ]
336 then
337 ewarn "Aborting setup"
338 exit 1
339 fi
340
341 echo "${D_SEPARATOR}"
342 echo ": Choose install location :"
343 echo
344 default_path="/var"
345 echo " The default (${default_path}) will install them"
346 echo " in ${default_path}/\${service}/\${IP_ADDRESS}"
347 echo
348 echo " For example:"
349 echo " /var/tinydns /1.2.3.4"
350 echo " /192.168.33.1"
351 echo " /axfrdns /1.2.3.4"
352 echo " /192.168.33.1"
353 echo " /dnscache/127.0.0.1"
354 echo
355 ewarn "Do NOT enter trailing slash"
356 echo " Where do you want services installed?"
357 read -p "[${default_path}] > " mypath
358 echo
359
360 if [ "${mypath}" == "" ]
361 then
362 mypath=${default_path}
363 fi
364
365 echo "${D_SEPARATOR}"
366 check_group_users
367
368 answer=""
369 another=""
370 until [ "$answer" == "n" ]
371 do
372 echo "${D_SEPARATOR}"
373 answer=""
374 read -p " Would you like to setup ${another}dnscache? [Y|n]> " answer
375 if [ "${answer}" == "Y" ] || [ "${answer}" == "" ]
376 then
377 myip=""
378 echo "${S_SEPARATOR}"
379 common_setup "DNS cache" "dnscache" "dnscache"
380 if [ $? == 0 ]
381 then
382 dnscache_setup
383 else
384 ewarn "Skipping dnscache specific setup."
385 fi
386 fi
387 another="another "
388 done
389
390 answer=""
391 another=""
392 until [ "$answer" == "n" ]
393 do
394 echo "${D_SEPARATOR}"
395 answer=""
396 read -p " Would you like to setup ${another}DNS server? [Y|n]> " answer
397 if [ "${answer}" == "Y" ] || [ "${answer}" == "" ]
398 then
399 myip=""
400 echo "${S_SEPARATOR}"
401 common_setup "DNS server" "{tinydns,afxrdns}" "tinydns axfrdns"
402 if [ $? == 0 ]
403 then
404 tinydns_setup
405 axfrdns_setup
406 else
407 ewarn "Skipping tinydns and axfrdns specific setup."
408 fi
409 fi
410 another="another "
411 done
412
413 echo "${D_SEPARATOR}"
414
415 start_services "tinydns axfrdns dnscache"
416
417 echo "${D_SEPARATOR}"
418 fi