-
Notifications
You must be signed in to change notification settings - Fork 3
/
do.sh
executable file
·445 lines (355 loc) · 9.95 KB
/
do.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#!/bin/bash -e
BASE=$(dirname "$0")
MAIN="python $BASE/main.py"
SERIAL="python $BASE/util/serial.py"
ROUTER_ATTRS="$BASE/util/router-attrs.sh"
DATA=/var/lib/cfg
FACTS=/var/lib/puppet/yaml/facts
CFGDIR=$BASE/cfg
export CFG=$CFGDIR/conf.yaml
ROUTER_HOST=dijkstra.urgu.org
ROUTER_IP=194.226.244.126
cmp_files() {
set +e; cmp -s $1 $2; local rv=$?; set -e
return $rv
}
gen_amtredird() {
local IMAGES="$DATA/keys_images"
local OUTPUT="/root/amtredird/amtredird.ini"
touch "$OUTPUT"
chmod 600 "$OUTPUT"
$MAIN amtredird $CFGDIR/amtredird.template $CFGDIR/amtpwd "$IMAGES" \
> "$OUTPUT"
}
gen_dhcp() {
local CUR=/etc/dhcp/dhcpd.conf
local NEW=$DATA/dhcpd.conf
$MAIN dhcp $CFGDIR/dhcp.template > $NEW
if ! cmp_files $CUR $NEW; then
mv $NEW $CUR
/etc/init.d/isc-dhcp-server restart
else
rm $NEW
fi
}
gen_dns() {
local ZONES="urgu.org"
local RELOAD=0 RESTART=0
gen_zone() {
local ZONE=$1 CMD=$2 TEMPLATE=$3
local SFILE=$DATA/$ZONE.serial
local CUR=$DATA/$ZONE.master
local OLD=$CUR.old NEW=$CUR.new
set +e; SOLD=$($SERIAL get $SFILE 2>/dev/null); rv=$?; set -e
if [[ "$rv" -ne 0 ]] || [[ "$TEMPLATE" -nt "$CUR" ]]; then
RELOAD=1
local SNEW=$($SERIAL inc $SFILE)
$MAIN $CMD $TEMPLATE $SNEW $ZONE > $CUR
else
$MAIN $CMD $TEMPLATE $SOLD $ZONE > $OLD
if ! cmp_files $CUR $OLD; then
RELOAD=1
rm $OLD
SNEW=$($SERIAL inc $SFILE)
$MAIN $CMD $TEMPLATE $SNEW $ZONE > $NEW
mv $NEW $CUR
else
rm $OLD
fi
fi
}
for ZONE in $ZONES; do
gen_zone $ZONE dns $CFGDIR/$ZONE.template
done
for NET in $($MAIN rdns_nets); do
gen_zone $NET rdns $CFGDIR/reverse.template
done
local CUR=$DATA/reverse.config
local NEW=$DATA/reverse.config.new
$MAIN rdns_cfg $CFGDIR/rdns_cfg.template $DATA/%s.master /etc/bind/db.empty > $NEW
if ! cmp_files $CUR $NEW; then
mv $NEW $CUR
RESTART=1
else
rm $NEW
fi
if [ $RESTART -ne 0 ]; then
/etc/init.d/bind9 restart
elif [ $RELOAD -ne 0 ]; then
/etc/init.d/bind9 reload
fi
}
gen_iptables_ports() {
local CUR=$DATA/ipt_ports.sh
local NEW=$CUR.new
$MAIN ipt_ports $ROUTER_IP server > $NEW
if ! cmp_files $CUR $NEW; then
mv $NEW $CUR
bash $CUR
/etc/init.d/iptables save active
else
rm $NEW
fi
}
gen_puppet_cfg() {
local DIR=$DATA/puppet
mkdir -p $DIR
local CUR=/etc/puppet/manifests/site.pp
local NEW=$DIR/site.pp
$MAIN puppet_cfg $CFGDIR/puppet_cfg.template > $NEW
mv $NEW $CUR
}
gen_puppet_fileserver() {
local DIR=$DATA/puppet
mkdir -p $DIR
local CUR=/etc/puppet/fileserver.conf
local NEW=$DIR/fileserver.conf
$MAIN puppet_fileserver $CFGDIR/puppet_fileserver.template $DATA/ssh > $NEW
mv $NEW $CUR
}
gen_puppet_ssh() {
local hosts=$($MAIN puppet_managed)
for host in $hosts; do
local DIR=$DATA/ssh/$host
if ! [ -d $DIR ]; then
(umask 077; mkdir -p $DIR)
ssh-keygen -t rsa -C "root@$host" -f $DIR/ssh_host_rsa_key -N ""
ssh-keygen -t dsa -C "root@$host" -f $DIR/ssh_host_dsa_key -N ""
chown -R puppet.puppet $DIR
fi
done
}
gen_nagios() {
local DIR=$DATA/nagios
mkdir -p $DIR
local CUR=$DIR/nagios.cfg
local NEW=$DIR/nagios.cfg.new
$MAIN nagios $CFGDIR/nagios.template > $NEW
if ! cmp_files $CUR $NEW; then
mv $NEW $CUR
chown nagios: $CUR
/etc/init.d/nagios3 restart
else
rm $NEW
fi
}
gen_ext_nagios() {
local DIR=$DATA/nagios
$MAIN ext_nagios $CFGDIR/nagios.template > $DIR/ext_nagios.cfg
}
gen_ssh_known_hosts() {
$MAIN ssh_known_hosts $FACTS $DATA > /etc/puppet/files/ssh_known_hosts
}
gen_ssh_known_hosts_updater() {
(
cat <<END
#!/bin/bash
# usage (e.g. in crontab): [curl|wget -O-] https://ssl.urgu.org/known_hosts.sh | bash -s -- -y
args=\$(getopt yf: \$*)
set -- \$args
auto=0
if [ \$(whoami) == root ]; then
known_hosts_file=/etc/ssh/ssh_known_hosts
else
known_hosts_file=~/.ssh/known_hosts
fi
for arg in \$args; do
case "\$arg" in
-y)
auto=1
shift ;;
-f)
known_hosts_file=\$2
shift ;;
esac
done
echo >&2 working with \$known_hosts_file
temp_file=\${known_hosts_file}.new
if [ -r \$known_hosts_file ]; then
cp \$known_hosts_file \$temp_file
else
touch \$temp_file
fi
declare -a script
END
declare -Al types
types[ssh-rsa]=ssh-rsa
types[ssh-dsa]=ssh-dss
types[ssh-ecdsa]=ecdsa-sha2-nistp256
types[ssh-ed25519]=ssh-ed25519
$MAIN ssh_known_hosts $FACTS $DATA | while read name type key; do
cat <<END
# echo working with $name
script[\${#script[@]}]=\$(
ssh-keygen -F $name -f \$temp_file | grep -v '#' | (
changed=0; seen=0;
while read host type key comment; do
seen=1
if [ \$type == $type ] && [ \$key != $key ]; then
changed=1
fi
done
if [ \$seen -ne 0 ]; then
if [ \$changed -ne 0 ]; then
echo >&2 "warning: some $type key(s) for $name changed, replacing";
echo "ssh-keygen -R $name -f \$temp_file 2>/dev/null; rm \${temp_file}.old; \
echo $name ${types[$type]} $key >> \$temp_file; "
fi
else
echo >&2 "info: adding new $type key for $name"
echo "echo $name ${types[$type]} $key >> \$temp_file; "
fi
)
)
END
done
cat <<END
for i in \$(seq 1 \${#script[@]}); do
bash -ec "\${script[i-1]}"
done
if [ \$auto -ne 0 ]; then
mv \$temp_file \$known_hosts_file
chmod 644 \$known_hosts_file
else
read -p 'Commit? ' y
case \$y in
[Yy]*) mv \$temp_file \$known_hosts_file
chmod 644 \$known_hosts_file
;;
*) rm \$temp_file
;;
esac
fi
END
) > $DATA/known_hosts.sh
chmod +x $DATA/known_hosts.sh
cp -f $DATA/known_hosts.sh /etc/puppet/files
}
gen_slurm() {
$MAIN slurm $CFGDIR/slurm.template $FACTS > $DATA/slurm.conf
cp -f $DATA/slurm.conf /etc/puppet/files
}
gen_ext_http() {
local CUR=$DATA/ext_http
local NEW=$DATA/ext_http.new
local KEYPATH=$DATA/https
ln -sf $CUR /etc/nginx/sites-enabled
$MAIN ext_http $CFGDIR/ext_http.template $KEYPATH > $NEW
if ! cmp_files $CUR $NEW; then
mv $NEW $CUR
/etc/init.d/nginx restart
else
rm $NEW
fi
}
gen_http_back() {
local CUR=$DATA/http_back
local NEW=$DATA/http_back.new
local KEYPATH=$DATA/https
ln -sf $CUR /etc/nginx/sites-enabled
$MAIN http_back $CFGDIR/http_back.template $KEYPATH > $NEW
if ! cmp_files $CUR $NEW; then
mv $NEW $CUR
/etc/init.d/nginx restart
else
rm $NEW
fi
}
reset_https_ca() {
local DIR="$DATA/https"
local KEY="$DIR/ca.key" CRT="$DIR/ca.crt"
local PWD
read -s -p 'Password:' PWD
local CN="$ROUTER_HOST"
rm -rf "$DIR"
mkdir -p "$DIR/"{,certs,crl,newcerts,private}
echo '01' > "$DIR/serial"
touch "$DIR/index.txt"
chown www-data: "$DIR"
chmod 500 "$DIR"
openssl genrsa -des3 -out "$KEY" -passout env:PWD 2048
chmod 600 "$KEY"
openssl req -new -x509 -days $((365*3)) -key "$KEY" -passin env:PWD -config \
<($MAIN https_req $CFGDIR/https_req.template "$ROUTER_HOST" "$CN") \
-out "$CRT" -batch
cp "$CRT" /etc/puppet/files
}
gen_https_certs() {
local DIR="$DATA/https"
local CA_KEY="$DIR/ca.key" CA_CRT="$DIR/ca.crt"
local PWD
read -s -p 'Password:' PWD
$MAIN https | while read hostspec; do
local hostname=${hostspec%%,*} cn=${hostspec##*,}
local KEY="$DIR/$cn.key" CRT="$DIR/$cn.crt" REQ="$DIR/$cn.req"
if [[ ! -f "$CRT" ]]; then
openssl req -nodes -newkey rsa:1024 -keyout "$KEY" -out "$REQ" -batch \
-config <($MAIN https_req $CFGDIR/https_req.template "$hostname" "$cn")
openssl ca -out "$CRT" -passin env:PWD -in "$REQ" -keyfile "$CA_KEY" \
-config <($MAIN https_ca $CFGDIR/https_ca.template "$DIR") -batch
fi
done
}
gen_ups_mrtg() {
local OUTPUT="$DATA"/ups_mrtg.cfg
$MAIN ups_mrtg $CFGDIR/mrtg.template > $OUTPUT
}
gen_user_chains() {
$MAIN ipt_users /etc/openvpn/runc/ipp.txt tun0 UaA | sh -s
}
gen_iptables_access() {
$MAIN ipt_access access $FACTS | sh -s
}
all_exist() {
for file in "$@"; do if ! [[ -f "$file" ]]; then return 1; fi; done
}
gen_keys_images() {
local IMAGES="$DATA/keys_images"
local MP="$IMAGES/mp"
local SIZE=256K
local UUID=5c4b0ee9-e5b6-44ce-9247-43103b07a95a
umask 077
mkdir -p "$MP"
for host in $($MAIN puppet_managed); do
local DEST="$IMAGES/$host"
local CERTS=(/var/lib/puppet/ssl/certs/{ca,"$host"}.pem)
local KEYS=("/var/lib/puppet/ssl/private_keys/$host.pem")
if ! [[ -f "$DEST" ]] && all_exist "${CERTS[@]}" "${KEYS[@]}"; then
dd if=/dev/zero "of=$DEST" "bs=$SIZE" count=1
mkfs.ext2 "$DEST"
tune2fs -U "$UUID" "$DEST"
mount -o loop "$DEST" "$MP"
mkdir -p "$MP"/puppet/{certs,private_keys}
cp -a "${CERTS[@]}" "$MP/puppet/certs"
cp -a "${KEYS[@]}" "$MP/puppet/private_keys"
umount "$MP"
fi
done
}
mkdir -p $DATA
export ROUTER_DEV=$($ROUTER_ATTRS dev)
export ROUTER_SRC=$($ROUTER_ATTRS src)
all() {
gen_amtredird
gen_dhcp
gen_dns
gen_iptables_ports
gen_puppet_cfg
gen_puppet_fileserver
gen_puppet_ssh
gen_keys_images
gen_ssh_known_hosts
gen_ssh_known_hosts_updater
gen_nagios
gen_ext_nagios
gen_slurm
gen_ext_http
gen_http_back
gen_ups_mrtg
}
if [ $# -eq 0 ]; then
all
else
cmd=$1; shift
$cmd "$@"
fi