1 | #!/bin/sh
|
---|
2 | # Wireless Leiden config-update script for FreeBSD 11.0 (nanobsd)
|
---|
3 | # Based on the 'API' of Jasper
|
---|
4 | # Rick van der Zwet
|
---|
5 | # XXX: TODO, some proper error checking for fetch
|
---|
6 |
|
---|
7 |
|
---|
8 | # Slow connection = no connection
|
---|
9 | export HTTP_TIMEOUT=20
|
---|
10 |
|
---|
11 |
|
---|
12 | check_access() {
|
---|
13 | # Direct Access - External
|
---|
14 | BASEURL="http://wirelessleiden.nl/config/"
|
---|
15 | fetch -o /dev/null -q $BASEURL > /dev/null && return
|
---|
16 | echo "# WARN: Fetch via external $BASEURL failed"
|
---|
17 |
|
---|
18 | # Direct Access - Internal IP
|
---|
19 | BASEURL="http://172.16.4.46/wleiden/config/"
|
---|
20 | # Connectivity check
|
---|
21 | fetch -o /dev/null -q $BASEURL > /dev/null && return
|
---|
22 | echo "# WARN: Fetch via internal $BASEURL failed"
|
---|
23 |
|
---|
24 | # Direct Access - External DNS
|
---|
25 | BASEURL="http://132.229.112.21/wleiden/config/"
|
---|
26 | fetch -o /dev/null -q $BASEURL > /dev/null && return
|
---|
27 | echo "# CRIT: Fetch via external $BASEURL failed"
|
---|
28 |
|
---|
29 | exit 1
|
---|
30 | }
|
---|
31 | check_access
|
---|
32 |
|
---|
33 |
|
---|
34 | # Default config to fetch
|
---|
35 | CONFIG=`hostname -s`
|
---|
36 |
|
---|
37 | # Determine it's statup and running location and some other hints
|
---|
38 | # Ordering is important! resolv.conf is fetched as last file, as it could
|
---|
39 | # potentially break the downloading of other files.
|
---|
40 | FILES="authorized_keys dhcpd.conf dnsmasq.conf motd rc.conf.local pf.hybrid.conf.local unbound.wleiden.conf wleiden.yaml resolv.conf"
|
---|
41 | file_details() {
|
---|
42 | POST_CMD=""
|
---|
43 | FILE_HINT=""
|
---|
44 |
|
---|
45 | case "$1" in
|
---|
46 | 'authorized_keys')
|
---|
47 | STARTUP_LOC="/cfg/ssh/${FILE}"
|
---|
48 | RUNNING_LOC="/etc/ssh/${FILE}"
|
---|
49 | ;;
|
---|
50 | 'dhcpd.conf')
|
---|
51 | STARTUP_LOC="/cfg/local/${FILE}"
|
---|
52 | RUNNING_LOC="/usr/local/etc/${FILE}"
|
---|
53 | POST_CMD="service isc-dhcpd restart"
|
---|
54 | ;;
|
---|
55 | 'dnsmasq.conf')
|
---|
56 | STARTUP_LOC="/cfg/local/${FILE}"
|
---|
57 | RUNNING_LOC="/usr/local/etc/${FILE}"
|
---|
58 | POST_CMD="service dnsmasq restart"
|
---|
59 | ;;
|
---|
60 | 'motd')
|
---|
61 | STARTUP_LOC="/cfg/${FILE}"
|
---|
62 | RUNNING_LOC="/etc/${FILE}"
|
---|
63 | POST_CMD="/etc/rc.d/motd onestart"
|
---|
64 | ;;
|
---|
65 | 'named.conf')
|
---|
66 | STARTUP_LOC="/cfg/namedb/${FILE}"
|
---|
67 | RUNNING_LOC="/etc/namedb/${FILE}"
|
---|
68 | POST_CMD="service named restart"
|
---|
69 | ;;
|
---|
70 | 'rc.conf.local')
|
---|
71 | STARTUP_LOC="/cfg/${FILE}"
|
---|
72 | RUNNING_LOC="/etc/${FILE}"
|
---|
73 | FILE_HINT="Restart interfaces with: nohup service netif restart"
|
---|
74 | ;;
|
---|
75 | 'resolv.conf')
|
---|
76 | STARTUP_LOC="/cfg/${FILE}"
|
---|
77 | RUNNING_LOC="/etc/${FILE}"
|
---|
78 | FILE_HINT="To get the ordering right run: nameserver-shuffle"
|
---|
79 | ;;
|
---|
80 | 'pf.hybrid.conf.local')
|
---|
81 | STARTUP_LOC="/cfg/${FILE}"
|
---|
82 | RUNNING_LOC="/etc/${FILE}"
|
---|
83 | POST_CMD="service pf reload"
|
---|
84 | ;;
|
---|
85 | 'unbound.wleiden.conf')
|
---|
86 | STARTUP_LOC="/cfg/local/${FILE}"
|
---|
87 | RUNNING_LOC="/usr/local/etc/${FILE}"
|
---|
88 | POST_CMD="service unbound reload"
|
---|
89 | ;;
|
---|
90 | 'wleiden.yaml')
|
---|
91 | STARTUP_LOC="/cfg/local/${FILE}"
|
---|
92 | RUNNING_LOC="/usr/local/etc/${FILE}"
|
---|
93 | ;;
|
---|
94 | esac
|
---|
95 | }
|
---|
96 |
|
---|
97 | usage() {
|
---|
98 | (
|
---|
99 | echo "Usage: $0 [-bpn] [-c <config>] [-m <all|startup|testing|running>]"
|
---|
100 | echo " -b = batch mode, no user input"
|
---|
101 | echo " -c <config> = default configuration to fetch"
|
---|
102 | echo " -d = do not run the POST_CMD commands [default]"
|
---|
103 | echo " -p = run the POST_CMD commands to activate the services right-away"
|
---|
104 | echo " -n = do not mount config partition"
|
---|
105 | echo " -m all = copy config files to running & config partition [default]"
|
---|
106 | echo " -m startup = copy config files to config partition"
|
---|
107 | echo " -m testing = do not copy config files"
|
---|
108 | echo " -m running = copy config files to running partition"
|
---|
109 | echo " -m hack = copy running files to config partition"
|
---|
110 | ) 1>&2
|
---|
111 | exit 2
|
---|
112 | }
|
---|
113 |
|
---|
114 | # Argument parsing using getopts
|
---|
115 | USE_API=1 # Whether or not to use the webinterface
|
---|
116 | OPT_MOUNT=1
|
---|
117 | OPT_RUNNING=1
|
---|
118 | OPT_STARTUP=1
|
---|
119 | OPT_HACK=0 # Hack for people without configuration managment and testing
|
---|
120 | OPT_BATCH=0
|
---|
121 | OPT_POSTCMD=false
|
---|
122 |
|
---|
123 | parse_options() {
|
---|
124 | while getopts "bc:nm:dp" OPT; do
|
---|
125 | case "$OPT" in
|
---|
126 | b) OPT_BATCH=1;;
|
---|
127 | c) CONFIG="${OPTARG}";;
|
---|
128 | d) OPT_POSTCMD=false;;
|
---|
129 | n) OPT_MOUNT=0;;
|
---|
130 | m) case "$OPTARG" in
|
---|
131 | all) true;;
|
---|
132 | live) OPT_STARTUP=0;;
|
---|
133 | startup) OPT_RUNNING=0;;
|
---|
134 | testing) OPT_RUNNING=0; OPT_STARTUP=0; OPT_MOUNT=0;;
|
---|
135 | hack) OPT_RUNNING=0; OPT_STARTUP=0; OPT_HACK=1; USE_API=0;;
|
---|
136 | *) usage;;
|
---|
137 | esac;;
|
---|
138 | h) usage;;
|
---|
139 | p) OPT_POSTCMD=true;;
|
---|
140 | \?) usage;;
|
---|
141 | esac
|
---|
142 | done
|
---|
143 | # Allow to override automatic mounting, in case of external mount 'managment'
|
---|
144 | if [ "$1" = "-n" ]; then
|
---|
145 | OPT_MOUNT=0
|
---|
146 | fi
|
---|
147 |
|
---|
148 | if [ "${OPT_RUNNING}" -eq 1 ]; then
|
---|
149 | echo "# INFO: Storing new config files in running configuration"
|
---|
150 | fi
|
---|
151 |
|
---|
152 | if [ "${OPT_STARTUP}" -eq 1 ]; then
|
---|
153 | echo "# INFO: Storing new config files in startup configuration"
|
---|
154 | fi
|
---|
155 |
|
---|
156 | if [ "${OPT_HACK}" -eq 1 ]; then
|
---|
157 | echo "# WARN: Copy running configuration to startup configuration"
|
---|
158 | echo "# WARN: Please do mind to document/mention this changes somewhere"
|
---|
159 | fi
|
---|
160 |
|
---|
161 | if /bin/df / | grep -q "^/dev/md[0-9]"; then
|
---|
162 | OPT_MOUNT=0
|
---|
163 | echo "# WARN: Mount operations disabled as we are running in a md(4) image"
|
---|
164 | fi
|
---|
165 |
|
---|
166 | # New line before the real work gets started
|
---|
167 | echo ""
|
---|
168 | }
|
---|
169 |
|
---|
170 |
|
---|
171 |
|
---|
172 |
|
---|
173 | # test validity of input
|
---|
174 | config_validator() {
|
---|
175 | INPUT="$1"
|
---|
176 | `grep -q "^${INPUT}\$" ${TMPDIR}/node_list.txt`
|
---|
177 | if [ $? -eq 0 ]; then
|
---|
178 | return 0
|
---|
179 | else
|
---|
180 | echo "WARNING: Input '${INPUT}' is not valid, some hints..."
|
---|
181 | grep -i "${INPUT}" ${TMPDIR}/node_list.txt
|
---|
182 | return 1
|
---|
183 | fi
|
---|
184 | }
|
---|
185 |
|
---|
186 |
|
---|
187 |
|
---|
188 | select_node() {
|
---|
189 | # List of all available nodes
|
---|
190 | fetch -q -o ${TMPDIR}/node_list.txt ${BASEURL} || exit 1
|
---|
191 |
|
---|
192 | if [ ${OPT_BATCH} -eq 1 ]; then
|
---|
193 | config_validator "${CONFIG}"
|
---|
194 | if [ $? -eq 1 ]; then
|
---|
195 | echo "ERROR: Please provide valid config" 1>&2
|
---|
196 | exit 1
|
---|
197 | fi
|
---|
198 | else
|
---|
199 | # Provide Nodelist and feedback
|
---|
200 | cat ${TMPDIR}/node_list.txt | column
|
---|
201 | echo ' THIS script adds the config from GENESIS to this operating system'
|
---|
202 | echo ' make sure you know what you are doing, if not press control-C'
|
---|
203 | echo ' ENTER CONFIG NAME ......(and press enter)'
|
---|
204 |
|
---|
205 | # Have the user to select the right node
|
---|
206 | INVALID_CONFIG=1
|
---|
207 | while [ ${INVALID_CONFIG} -eq 1 ]; do
|
---|
208 | # Ask for node name, play around with prev option
|
---|
209 | echo -n "Name [${CONFIG}]: "
|
---|
210 | read INPUT
|
---|
211 | if [ -z "${INPUT}" ]; then
|
---|
212 | INPUT=${CONFIG}
|
---|
213 | else
|
---|
214 | CONFIG=${INPUT}
|
---|
215 | fi
|
---|
216 |
|
---|
217 | config_validator "${INPUT}"
|
---|
218 | if [ $? -eq 0 ]; then
|
---|
219 | INVALID_CONFIG=0
|
---|
220 | fi
|
---|
221 | done
|
---|
222 | fi
|
---|
223 | }
|
---|
224 |
|
---|
225 |
|
---|
226 |
|
---|
227 |
|
---|
228 | # Copy file, saving some bits if no change needed
|
---|
229 | copy_file() {
|
---|
230 | SOURCE=$1
|
---|
231 | TARGET=$2
|
---|
232 | diff -I '^FreeBSD ' -I '^# Generated at ' ${TARGET} ${SOURCE} 2>/dev/null
|
---|
233 | if [ $? -ne 0 ]; then
|
---|
234 | mkdir -p `dirname ${TARGET}` || exit 1
|
---|
235 | cp ${SOURCE} ${TARGET} || exit 1
|
---|
236 | return $?
|
---|
237 | fi
|
---|
238 | return 1
|
---|
239 | }
|
---|
240 |
|
---|
241 | # Main function
|
---|
242 | main() {
|
---|
243 | TMPDIR=`mktemp -d -t $(basename $0)`
|
---|
244 | # Clear out tempdir when done
|
---|
245 | if [ ${OPT_MOUNT} -eq 1 ]; then
|
---|
246 | trap "rm -Rf ${TMPDIR}; umount /cfg; exit" 0 1 2 3 15
|
---|
247 | else
|
---|
248 | trap "rm -Rf ${TMPDIR}; exit" 0 1 2 3 15
|
---|
249 |
|
---|
250 | fi
|
---|
251 |
|
---|
252 | # Mount if requested
|
---|
253 | if [ ${OPT_MOUNT} -eq 1 ]; then
|
---|
254 | mount /cfg
|
---|
255 | fi
|
---|
256 |
|
---|
257 | # Select node from web-interface
|
---|
258 | if [ ${USE_API} -eq 1 ]; then
|
---|
259 | select_node
|
---|
260 | fi
|
---|
261 |
|
---|
262 | # Worker, place all files in required directory
|
---|
263 | for FILE in ${FILES}; do
|
---|
264 | if [ ${USE_API} -eq 1 ]; then
|
---|
265 | # Fetch needed file
|
---|
266 | FRESH_LOC=${TMPDIR}/${FILE}
|
---|
267 | fetch -q -o ${FRESH_LOC} ${BASEURL}/${CONFIG}/${FILE} || exit 1
|
---|
268 | fi
|
---|
269 |
|
---|
270 | # Needed file details, like locations and hints
|
---|
271 | file_details ${FILE}
|
---|
272 |
|
---|
273 | echo "# INFO: Working on file: '${FILE}'"
|
---|
274 | # Copy file boot location
|
---|
275 | if [ ${OPT_STARTUP} -eq 1 ]; then
|
---|
276 | copy_file ${FRESH_LOC} ${STARTUP_LOC}
|
---|
277 | fi
|
---|
278 |
|
---|
279 | # Copy file running location
|
---|
280 | if [ ${OPT_RUNNING} -eq 1 ]; then
|
---|
281 | copy_file ${FRESH_LOC} ${RUNNING_LOC}
|
---|
282 | if [ $? -eq 0 ]; then
|
---|
283 | echo "# INFO: '${FILE}' changed"
|
---|
284 | if [ -n "${POST_CMD}" ]; then
|
---|
285 | if $OPT_POSTCMD; then
|
---|
286 | echo "## Running post_cmd: $POST_CMD"
|
---|
287 | $POST_CMD
|
---|
288 | else
|
---|
289 | echo "## To activate run the post_cmd: $POST_CMD"
|
---|
290 | fi
|
---|
291 | fi
|
---|
292 | if [ -n "${FILE_HINT}" ]; then
|
---|
293 | echo "# INFO: ${FILE_HINT}"
|
---|
294 | echo ""
|
---|
295 | fi
|
---|
296 | fi
|
---|
297 | fi
|
---|
298 |
|
---|
299 | # Direct copy
|
---|
300 | if [ ${OPT_HACK} -eq 1 ]; then
|
---|
301 | # No checking, just dumb try to copy mode
|
---|
302 | cp -v ${RUNNING_LOC} ${STARTUP_LOC}
|
---|
303 | fi
|
---|
304 | done
|
---|
305 |
|
---|
306 | exit 0
|
---|
307 | }
|
---|
308 |
|
---|
309 | parse_options $*
|
---|
310 | main
|
---|