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