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

Last change on this file since 4996 was 4990, checked in by dirkx, 20 years ago

+Cope with whost having port numbers

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