source: genesis/tools/config-node.sh@ 4988

Last change on this file since 4988 was 4988, checked in by rick, 19 years ago

Fixed diff functie

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