1 | #!/bin/sh
|
---|
2 | #
|
---|
3 | # (c) Copyright 2002, 2003, 2005 Stichting Wireless Leiden, all
|
---|
4 | # rights reserved. More information can be found on
|
---|
5 | # http://wwww.wirelessleiden.nl and the license is at:
|
---|
6 | # http://wleiden.webweaving.org:8080/svn/node-config/LICENSE
|
---|
7 | #
|
---|
8 | # 1.00
|
---|
9 | # ?? Marten Vijn 24-03-03
|
---|
10 | # ?? new version 14-11-2003
|
---|
11 | # 1.03 proxy cleanup, detect faulty files, generalize
|
---|
12 | # file list, check node name to be valid, '-n' mode.
|
---|
13 | # make moving of final files a bit safer. (dirkx)
|
---|
14 | # 1.04 Add auto read-only detection. (dirkx)
|
---|
15 | # 1.05 Cope with WHOST containing a port number. (dirkx).
|
---|
16 | # 1.06 Better diff (dirkx)
|
---|
17 | # 1.07 Check versions of OS and Script
|
---|
18 | #
|
---|
19 | # If there is a global system configuration file, suck it in.
|
---|
20 | #
|
---|
21 |
|
---|
22 | PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
|
---|
23 | TMPDIR=${TMPDIR:-/tmp}
|
---|
24 | TMPPREFIX=${TMPDIR}/wl-tmp-$$
|
---|
25 | WHOST=${WHOST:-rambo.wleiden.net}
|
---|
26 | HTTP_PROXY_DEFAULT=${HTTP_PROXY:-http://proxy.wleiden.net:3128}
|
---|
27 | HTTP_USER_AGENT=${HTTP_USER_AGENT:-curl.faked.fetch/0.0}
|
---|
28 | VERSION=1.06
|
---|
29 | QUIET=${QUIET:-}
|
---|
30 | PRETEND=no
|
---|
31 | FORCE=no
|
---|
32 | CMD=do_move
|
---|
33 | DLV=no
|
---|
34 |
|
---|
35 | # Genesis master location.
|
---|
36 | link=${GENESIS:-http://${WHOST}/cgi-bin/g_list.pl/}
|
---|
37 | list=filelist
|
---|
38 |
|
---|
39 | # Location for private config
|
---|
40 | lcd=${LCDIR:-/lcd}
|
---|
41 | dir=${lcd}
|
---|
42 |
|
---|
43 | FETCH=${FETCH:-/usr/bin/fetch}
|
---|
44 | test -e ${FETCH} || FETCH="curl"
|
---|
45 |
|
---|
46 | if echo ${FETCH} | grep -q curl; then
|
---|
47 | FETCH="${FETCH} --silent"
|
---|
48 | else
|
---|
49 | FETCH="${FETCH} -q"
|
---|
50 | fi
|
---|
51 |
|
---|
52 | usage() {
|
---|
53 | echo Usage $0 [-q] [-p proxy] [-d] [-D] [-n] [hostname]
|
---|
54 | echo "-p <proxy> Set a proxy"
|
---|
55 | echo "-P Use ${HTTP_PROXY_DEFAULT} as a proxy"
|
---|
56 | echo "-D Use DHCP to get an address"
|
---|
57 | echo "-n Show what would happen - but do not do it"
|
---|
58 | echo "-d Diff met hudiige config (maar doe niets)"
|
---|
59 | echo "-q Suppress all output and user interaction"
|
---|
60 | echo "-F Force disk to write"
|
---|
61 | echo "-i Ignore all safety checks checks"
|
---|
62 | exit 1
|
---|
63 | }
|
---|
64 |
|
---|
65 | for i in $*
|
---|
66 | do
|
---|
67 | case "$i"
|
---|
68 | in
|
---|
69 | -D) connset || exit 1
|
---|
70 | ;;
|
---|
71 | -q)
|
---|
72 | QUIET=yes
|
---|
73 | ;;
|
---|
74 | -i)
|
---|
75 | DLV=
|
---|
76 | ;;
|
---|
77 | -d)
|
---|
78 | CMD=do_diff
|
---|
79 | dir=${TMPDIR}
|
---|
80 | ;;
|
---|
81 | -p)
|
---|
82 | shift;
|
---|
83 | HTTP_PROXY=$1
|
---|
84 | export HTTP_PROXY
|
---|
85 | ;;
|
---|
86 | -p)
|
---|
87 | HTTP_PROXY=HTTP_PROXY_DEFAULT
|
---|
88 | export HTTP_PROXY
|
---|
89 | ;;
|
---|
90 | -n)
|
---|
91 | PRETEND=yes
|
---|
92 | ;;
|
---|
93 | -F)
|
---|
94 | FORCE=yes
|
---|
95 | ;;
|
---|
96 | *)
|
---|
97 | test $# -eq 1 || usage
|
---|
98 | nodename=$i
|
---|
99 | break;
|
---|
100 | ;;
|
---|
101 | esac
|
---|
102 | shift
|
---|
103 | done
|
---|
104 |
|
---|
105 | HOST=`echo ${WHOST} | sed -e 's/:.*//'`
|
---|
106 | set `echo $VERSION | sed -e 's/\./ /'`
|
---|
107 | VERSION_MAJOR=$1
|
---|
108 | VERSION_MINOR=$2
|
---|
109 | VERSION_OTHER=$3
|
---|
110 |
|
---|
111 | # connection test function
|
---|
112 | connset()
|
---|
113 | {
|
---|
114 | if [ `ps ax | grep -c dhclient` != "1" ] ; then
|
---|
115 | killall dhclient
|
---|
116 | fi
|
---|
117 |
|
---|
118 | echo "Enter an IP address of a nearby Nameserver or use:"
|
---|
119 | echo " 1 to use COPE \(on Wleiden\)"
|
---|
120 | echo " 2 to use XS4All \(on the internet\)"
|
---|
121 | echo " 3 to use the LCP server \(on the internal LCP networ\)"
|
---|
122 |
|
---|
123 | echo -n "IP address or 1/2/3: "
|
---|
124 | read dns_list
|
---|
125 |
|
---|
126 | case $dns_list in
|
---|
127 | 1)
|
---|
128 | resolver="172.17.8.1"
|
---|
129 | ;;
|
---|
130 | 2)
|
---|
131 | resolver="194.109.9.99"
|
---|
132 | ;;
|
---|
133 | 3)
|
---|
134 | resolver="10.0.0.1"
|
---|
135 | ;;
|
---|
136 | *)
|
---|
137 | resolver=$dns_list
|
---|
138 | ;;
|
---|
139 | esac
|
---|
140 |
|
---|
141 | cp /etc/resolv.conf /etc/resolv.bak || exit 1
|
---|
142 | echo "nameserver ${resolver}" > /etc/resolv.conf
|
---|
143 |
|
---|
144 |
|
---|
145 | for nic in `ifconfig -l`
|
---|
146 | do
|
---|
147 | case ${nic} in
|
---|
148 | lo0 | wi*)
|
---|
149 | ;;
|
---|
150 | *)
|
---|
151 | if ping -qnoc ${HOST}; then
|
---|
152 | echo Connection on interface ${nic} to ${HOST} ok.
|
---|
153 | else
|
---|
154 | killall dhclient
|
---|
155 | echo Trying to get a DHCP lease on ${nic}
|
---|
156 | dhclient -1 ${nic}
|
---|
157 | fi
|
---|
158 | ;;
|
---|
159 | esac
|
---|
160 | done
|
---|
161 | }
|
---|
162 |
|
---|
163 | log()
|
---|
164 | {
|
---|
165 | if [ -z ${QUIET} ]; then
|
---|
166 | echo "$*"
|
---|
167 | fi
|
---|
168 | }
|
---|
169 | lognlr()
|
---|
170 | {
|
---|
171 | if [ -z ${QUIET} ]; then
|
---|
172 | echo -n "$*"
|
---|
173 | fi
|
---|
174 | }
|
---|
175 |
|
---|
176 | cleanse()
|
---|
177 | {
|
---|
178 | rm -f ${TMPPREFIX}.?
|
---|
179 | }
|
---|
180 |
|
---|
181 | # Normal exit; but make sure
|
---|
182 | # we also clean up any tmp files
|
---|
183 | #
|
---|
184 | cleanexit()
|
---|
185 | {
|
---|
186 | E=1
|
---|
187 | if [ $# -gt 0 ]; then
|
---|
188 | E=$1
|
---|
189 | fi
|
---|
190 | cleanse
|
---|
191 | log Exit
|
---|
192 | exit $E
|
---|
193 | # Trap any weird exit codes.
|
---|
194 | exit 1
|
---|
195 | }
|
---|
196 |
|
---|
197 | safefetch()
|
---|
198 | {
|
---|
199 | url=$1
|
---|
200 | file=$2
|
---|
201 | ${FETCH} -o - ${url} > ${TMPPREFIX}.x \
|
---|
202 | || cleanexit 1
|
---|
203 |
|
---|
204 | # Genesis can provide us with corrupted/empty files
|
---|
205 | # with a 200 OK - so insist that they are at least
|
---|
206 | # a few lines long.
|
---|
207 | #
|
---|
208 | set `wc -l ${TMPPREFIX}.x`
|
---|
209 | if [ $1 -lt 2 ]; then
|
---|
210 | echo File ${link}${nodename} is less than 2 lines long.
|
---|
211 | echo Assuming a problem with Genesis.
|
---|
212 | cleanexit 2
|
---|
213 | fi
|
---|
214 |
|
---|
215 | cp ${TMPPREFIX}.x ${file} \
|
---|
216 | || cleanexit 1
|
---|
217 | rm -f ${TMPPREFIX}.x
|
---|
218 |
|
---|
219 | return 0
|
---|
220 | }
|
---|
221 |
|
---|
222 | getvalidnodenames()
|
---|
223 | {
|
---|
224 | log Fetching list of nodes from ${link}
|
---|
225 | safefetch ${link} ${nlist} || cleanexit 1
|
---|
226 | }
|
---|
227 |
|
---|
228 | getvalidnodename()
|
---|
229 | {
|
---|
230 | while ! grep -q "^${nodename}\$" ${nlist}
|
---|
231 | do
|
---|
232 | echo Nodes:
|
---|
233 | if [ -x /usr/bin/column ]; then
|
---|
234 | column ${nlist}
|
---|
235 | else
|
---|
236 | cat ${nlist}
|
---|
237 | fi
|
---|
238 | echo
|
---|
239 | echo -n enter nodename \[default: ${default}\]:
|
---|
240 | if [ -z ${QUIET} ]; then
|
---|
241 | read nodename
|
---|
242 | else
|
---|
243 | nodename=${default}
|
---|
244 | fi
|
---|
245 |
|
---|
246 | if [ "x${nodename}" = "x" ]; then
|
---|
247 | nodename=${default}
|
---|
248 | fi
|
---|
249 | done
|
---|
250 |
|
---|
251 | echo Node Selected: ${nodename}
|
---|
252 | }
|
---|
253 |
|
---|
254 |
|
---|
255 | do_diff() {
|
---|
256 | if test -e $lcd/$1; then
|
---|
257 | diff -uwbB $lcd/$1 $dir/$1.new
|
---|
258 | else
|
---|
259 | echo Warning: $lcd/$1 does not yet exist - no DIFF
|
---|
260 | fi
|
---|
261 | }
|
---|
262 |
|
---|
263 | do_move() {
|
---|
264 | if [ -e $dir/$1 ]; then
|
---|
265 | mv $dir/$1 $dir/$1.bak || cleanexit 1
|
---|
266 | fi
|
---|
267 | cp $dir/$1.new $dir/$1 || cleanexit 1
|
---|
268 | rm $dir/$1.new || cleanexit 1
|
---|
269 | }
|
---|
270 |
|
---|
271 | linkin() {
|
---|
272 | symdir=$1
|
---|
273 | file=$2
|
---|
274 |
|
---|
275 | if [ ${PRETEND} = 'yes' ]; then
|
---|
276 | echo "** $CMD $*"
|
---|
277 | else
|
---|
278 | $CMD $file || exit 1
|
---|
279 | fi
|
---|
280 |
|
---|
281 | test -e $symdir/$file || (
|
---|
282 | echo WARNING: Symlink $symdir/$file not in place.
|
---|
283 | echo use: ln -s $dir/$file $symdir/$file
|
---|
284 | echo to fix if appropriate.
|
---|
285 | )
|
---|
286 | }
|
---|
287 |
|
---|
288 | log Config Node -- Version: $VERSION '$Rev: 5003 $'
|
---|
289 |
|
---|
290 | # Make sure we clean up our mess when needed.
|
---|
291 | trap "rm -f ${TMPPREFIX}.?; echo Failed; exit 1;" 2 3
|
---|
292 |
|
---|
293 | if [ ${PRETEND} != 'yes' -a ${CMD} != 'do_diff' ]; then
|
---|
294 | if mount | grep "on / " | grep -q read-only; then
|
---|
295 | if [ ${FORCE} = "yes" ]; then
|
---|
296 | echo Forcing read-only disk into rw.
|
---|
297 | fsck / || exit 2
|
---|
298 | mount -o noatime -u -w / || exit 2 || exit 1
|
---|
299 | trap "mount -u -r /; rm -f ${TMPPREFIX}.?; echo Failed; exit 1;" 2 3
|
---|
300 | FORCE=rw
|
---|
301 | else
|
---|
302 | echo ERROR - disk / is mounted read only. Aborting.
|
---|
303 | exit 1
|
---|
304 | fi
|
---|
305 | fi
|
---|
306 | fi
|
---|
307 |
|
---|
308 | export HTTP_USER_AGENT
|
---|
309 | export TMPDIR
|
---|
310 |
|
---|
311 |
|
---|
312 | #check config dir
|
---|
313 | #
|
---|
314 | if [ ! -d ${dir} ]; then
|
---|
315 | mkdir -p ${dir} || cleanexit 1
|
---|
316 | fi
|
---|
317 |
|
---|
318 | if [ -z ${HTTP_PROXY} ]; then
|
---|
319 | (
|
---|
320 | log Checking DNS for ${HOST}
|
---|
321 | host ${HOST} > /dev/null || exit 1
|
---|
322 | log Checking if ${HOST} can be reached
|
---|
323 | ping -qnoc 1 ${HOST} > /dev/null || exit 1
|
---|
324 | log Connection: Ok
|
---|
325 | exit 0
|
---|
326 | ) || connset
|
---|
327 | else
|
---|
328 | log Connection not checked because there is an http proxy configured: ${HTTP_PROXY}.
|
---|
329 | fi
|
---|
330 |
|
---|
331 | if [ -r ${lcd}/myname ]; then
|
---|
332 | default=`cat ${lcd}/myname`
|
---|
333 | else
|
---|
334 | default=`hostname -s`
|
---|
335 | test -z $default && default=none
|
---|
336 | fi
|
---|
337 |
|
---|
338 | if [ ! -z ${QUIET} ]; then
|
---|
339 | if [ -z ${nodename} ]; then
|
---|
340 | nodename=${default}
|
---|
341 | fi
|
---|
342 | fi
|
---|
343 |
|
---|
344 |
|
---|
345 | nlist=${TMPPREFIX}.l
|
---|
346 |
|
---|
347 | test -z $default && default=$nodename
|
---|
348 |
|
---|
349 | getvalidnodenames || exit 1
|
---|
350 |
|
---|
351 | test -z $nodename && getvalidnodename
|
---|
352 |
|
---|
353 | while test -z $nodename || ! grep -q ${nodename} ${nlist}
|
---|
354 | do
|
---|
355 | echo
|
---|
356 | echo Error: Node named \"$nodename\" not known.
|
---|
357 | if [ -z ${QUIET} ]; then
|
---|
358 | exit 1
|
---|
359 | fi
|
---|
360 | echo Please select one from the list.
|
---|
361 | echo
|
---|
362 | getvalidnodename
|
---|
363 | done
|
---|
364 |
|
---|
365 | lognlr "Checking release and OS versions: "
|
---|
366 |
|
---|
367 | safefetch ${link}${nodename}/info $dir/info.last \
|
---|
368 | || cleanexit 1
|
---|
369 |
|
---|
370 | OS=`uname -s`
|
---|
371 | REL=`uname -r`
|
---|
372 | set -- `head -1 $dir/info.last`
|
---|
373 |
|
---|
374 | # FreeBSD 5.0-RELEASE 1 YES
|
---|
375 | if [ $# != 4 ]; then
|
---|
376 | echo Info verification failed.
|
---|
377 | cleanexit 1
|
---|
378 | fi
|
---|
379 | if [ x$4 != 'xYES' ]; then
|
---|
380 | echo Genesis marked as disabled for this machine.
|
---|
381 | test -z ${DLV} || cleanexit 1
|
---|
382 | fi
|
---|
383 | if [ 0$3 -ne $VERSION_MAJOR ]; then
|
---|
384 | echo This script is version $VERSION, genesis info is for version $3.xx
|
---|
385 | test -z ${DLV} || cleanexit 1
|
---|
386 | fi
|
---|
387 | if [ x$1 != x$OS ]; then
|
---|
388 | echo Operating system mismatch; this machine: $OS, but config is for $1
|
---|
389 | test -z ${DLV} || cleanexit 1
|
---|
390 | fi
|
---|
391 | if [ x$2 != x$REL ]; then
|
---|
392 | echo This machine runs $REL, but the configuration is for $2
|
---|
393 | test -z ${DLV} || cleanexit 2
|
---|
394 | fi
|
---|
395 |
|
---|
396 | log Ok
|
---|
397 |
|
---|
398 | log Fetching file list from $link for $nodename
|
---|
399 |
|
---|
400 | safefetch ${link}${nodename} ${dir}/${list}
|
---|
401 |
|
---|
402 | lognlr "Fetching:"
|
---|
403 | for i in `cat ${dir}/${list}`
|
---|
404 | do
|
---|
405 | lognlr " ${i}"
|
---|
406 | # $FETCH -o - ${link}${nodename}/${i} > ${dir}/${i}.new || cleanexit 1
|
---|
407 | safefetch ${link}${nodename}/${i} ${dir}/${i}.new
|
---|
408 | done
|
---|
409 | log .
|
---|
410 |
|
---|
411 | for i in `cat ${dir}/${list}`
|
---|
412 | do
|
---|
413 | case ${i} in
|
---|
414 | linux.sh | config | txtconfig )
|
---|
415 | # log obsolete file: ${i} - skipped
|
---|
416 | ;;
|
---|
417 | resolv.conf | rc.node.local | rc.local)
|
---|
418 | linkin /etc ${i}
|
---|
419 | ;;
|
---|
420 | snmpd.local.conf)
|
---|
421 | linkin /usr/local/share/snmp ${i}
|
---|
422 | ;;
|
---|
423 | named.conf)
|
---|
424 | linkin /etc/namedb ${i}
|
---|
425 | ;;
|
---|
426 | dhcpd.conf)
|
---|
427 | linkin /usr/local/etc ${i}
|
---|
428 | ;;
|
---|
429 | zebra.conf | ospfd.conf)
|
---|
430 | linkin /usr/local/etc/zebra ${i}
|
---|
431 | ;;
|
---|
432 | authorized_keys)
|
---|
433 | linkin /root/.ssh ${i}
|
---|
434 | ;;
|
---|
435 | ssh_known_hosts)
|
---|
436 | linkin /etc/ssh ${i}
|
---|
437 | ;;
|
---|
438 | daemons.sh)
|
---|
439 | linkin /wl ${i}
|
---|
440 | ;;
|
---|
441 | *)
|
---|
442 | echo Script cannot cope with ${i} - ignoring..
|
---|
443 | ;;
|
---|
444 | esac
|
---|
445 | done
|
---|
446 |
|
---|
447 | if [ -e /etc/rc.local ]; then
|
---|
448 | # See if we are in rc.local
|
---|
449 | if grep -q /config-node.sh /etc/rc.local; then
|
---|
450 |
|
---|
451 | echo As this node now has real configs - do enter a root password
|
---|
452 | echo
|
---|
453 | passwd \
|
---|
454 | || cleanexit 1
|
---|
455 |
|
---|
456 | echo Removing /etc/rc.local
|
---|
457 | rm -f /etc/rc.local
|
---|
458 |
|
---|
459 | echo Will drop write perms on the next reboot.
|
---|
460 | fi
|
---|
461 | fi
|
---|
462 |
|
---|
463 | # Record our name.
|
---|
464 | echo ${nodename} > ${dir}/myname
|
---|
465 |
|
---|
466 | # Rebuild reverse lookups
|
---|
467 | if test -e /etc/rc.node.local; then
|
---|
468 | H=`cat /etc/rc.node.local | grep hostname | sed -e s/hostname=// | sed -e s/[\"\']//g`
|
---|
469 | hostname $H
|
---|
470 | else
|
---|
471 | echo Warning: rc.node.local missing.
|
---|
472 | fi
|
---|
473 |
|
---|
474 | if [ -r /etc/namedb/make-localhost -a ${CMD} != 'do_diff' ]; then
|
---|
475 | (
|
---|
476 | cd /etc/namedb || exit 1
|
---|
477 | sh /etc/namedb/make-localhost || exit 1
|
---|
478 | ) || exit 1
|
---|
479 | fi
|
---|
480 |
|
---|
481 | cleanse || exit 1
|
---|
482 |
|
---|
483 | if [ -e /etc/rc.empty.conf ] ; then
|
---|
484 | rm /etc/rc.empty.conf || exit 1
|
---|
485 | echo removed /etc/rc.empty.conf - and rebooting in 30 seconds \(or press ctrl-C to abort\)
|
---|
486 | read -t 30 DUMMY
|
---|
487 | reboot
|
---|
488 | fi
|
---|
489 |
|
---|
490 | test ${FORCE} = 'rw' && mount -u -r /
|
---|
491 | exit 0
|
---|