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