forked from arno-iptables-firewall/aif
-
Notifications
You must be signed in to change notification settings - Fork 1
/
install.sh
executable file
·513 lines (421 loc) · 15.9 KB
/
install.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
#!/bin/bash
MY_VERSION="1.14d"
# ------------------------------------------------------------------------------------------
# -= Arno's Iptables Firewall(AIF) =-
# Single- & multi-homed firewall script with DSL/ADSL support
#
# ~ In memory of my dear parents ~
#
# (C) Copyright 2001-2023 by Arno van Amersfoort
# Web : https://github.com/arno-iptables-firewall/aif
# Email : a r n o DOT v a n DOT a m e r s f o o r t AT g m a i l DOT c o m
# (note: you must remove all spaces and substitute the @ and the .
# at the proper locations!)
# ------------------------------------------------------------------------------------------
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# version 2 as published by the Free Software Foundation.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# ------------------------------------------------------------------------------------------
EOL='
'
# Check if the environment file exists and if so, load it
#########################################################
if [ -f ./share/arno-iptables-firewall/environment ]; then
. ./share/arno-iptables-firewall/environment
else
printf "\033[40m\033[1;31mERROR: Could not read environment file ./share/arno-iptables-firewall/environment!\033[0m\n" >&2
exit 2
fi
sanity_check()
{
# root check
if [ "$(id -u)" != "0" ]; then
printf "\033[40m\033[1;31mERROR: Root check FAILED (you MUST be root to use this script)! Quitting...\033[0m\n" >&2
exit 1
fi
check_command_error iptables
if [ "$IPV6_DETECTED" = "1" ]; then
check_command_error ip6tables
fi
check_command_error awk
check_command_error tr
check_command_error ip
check_command_error cut
check_command_error uname
check_command_error sed
check_command_error cat
check_command_error date
check_command_error modprobe
check_command_error sysctl
check_command_error head
check_command_error tail
check_command_error wc
check_command_error gzip
check_command_error logger
check_command_error chmod
check_command_error chown
check_command_error find
check_command_error cp
check_command_error rm
check_command_error mkdir
check_command_error rmdir
check_command_error ln
check_command_warning dig nslookup
}
shell_diff()
{
local FILE1_DATA="$(cat "$1")"
local FILE2_DATA="$(cat "$2")"
if [ "$FILE1_DATA" != "$FILE2_DATA" ]; then
# If mismatch, check whether it's only the comments that differ
if [ "${FILE1_DATA%\#*}" = "${FILE2_DATA%\#*}" ]; then
return 1 # Only comments differ
fi
return 2 # Full mismatch
fi
return 0 # Match
}
copy_ask_if_exist()
{
local DIFF_RETVAL=-1
local RETVAL
local DEFAULT_YN="${3:-'n'}" # Default to n(o)
local BACKUP_EXT="$4"
local FALLBACK_EXT="$5"
if [ -z "$(find "$1" -type f)" ]; then
echo "ERROR: Missing source file(s) \"$1\"" >&2
exit 2
fi
unset IFS
for SOURCE in `find "$1" -type f |grep -v -e '/\.svn/' -e '/\.git/'`; do
if echo "$2" |grep -q '/$'; then
FN="${SOURCE#$1}"
if [ -z "$FN" ]; then
TARGET="${2}$(basename "$1")"
else
TARGET="${2}${FN}"
fi
TARGET_DIR="$2"
else
TARGET="$2"
TARGET_DIR="$(dirname "$2")"
fi
if [ ! -d "$TARGET_DIR" ]; then
printf "\033[40m\033[1;31m* WARNING: Target directory $TARGET_DIR does not exist. Skipping copy of $SOURCE!\033[0m\n" >&2
continue
fi
if [ -f "$SOURCE" -a -f "$TARGET" ]; then
# Ignore files that are the same in the target
shell_diff "$SOURCE" "$TARGET"
DIFF_RETVAL=$? # 0 = full match, 1 = match (excluding comments), 2 = full mismatch (including comments)
if [ $DIFF_RETVAL -eq 2 ] && ! get_user_yn "File \"$TARGET\" already exists. Overwrite" "$DEFAULT_YN"; then
if [ -z "$FALLBACK_EXT" ]; then
echo "Skipped..."
continue
else
# Copy as e.g. .dist-file:
TARGET="${TARGET}.${FALLBACK_EXT}"
rm -f "$TARGET"
fi
fi
fi
RETVAL=0
if [ $DIFF_RETVAL -eq 2 -a -n "$BACKUP_EXT" ]; then
# copy file & create backup of old file if exists
cp -v --suffix=".${BACKUP_EXT}" --backup=simple --preserve=mode,timestamps "$SOURCE" "$TARGET"
RETVAL=$?
else
# Only comments mismatch, so no point in keeping a backup file
cp -v --preserve=mode,timestamps "$SOURCE" "$TARGET"
RETVAL=$?
fi
if [ $RETVAL -ne 0 ]; then
echo "ERROR: Copy of \"$SOURCE\" to \"$TARGET\" failed!" >&2
exit 3
fi
chown 0:0 "$TARGET"
done
return 0
}
copy_skip_if_exist()
{
if [ -z "$(find "$1" -type f)" ]; then
echo "ERROR: Missing source file(s) \"$1\"" >&2
exit 2
fi
unset IFS
for SOURCE in `find "$1" -type f |grep -v -e '/\.svn/' -e '/\.git/'`; do
if echo "$2" |grep -q '/$'; then
FN="${SOURCE#$1}"
if [ -z "$FN" ]; then
TARGET="$2$(basename "$1")"
else
TARGET="$2$FN"
fi
TARGET_DIR="$2"
else
TARGET="$2"
TARGET_DIR="$(dirname "$2")"
fi
if [ ! -d "$TARGET_DIR" ]; then
printf "\033[40m\033[1;31m* WARNING: Target directory $TARGET_DIR does not exist. Skipping copy of $SOURCE!\033[0m\n" >&2
continue
fi
if [ -f "$TARGET" ]; then
if [ -z "$3" ]; then
echo "* File \"$TARGET\" already exists. Skipping copy of $SOURCE"
continue
else
# Copy as e.g. .dist-file:
TARGET="${TARGET}.${3}"
rm -f "$TARGET"
fi
fi
# NOTE: Always copy, even if contents is the same to make sure permissions are updated
if ! cp -v --preserve=mode,timestamps "$SOURCE" "$TARGET"; then
echo "ERROR: Copy of \"$SOURCE\" to \"$TARGET!\" failed!" >&2
exit 3
fi
chown 0:0 "$TARGET"
done
return 0
}
copy_overwrite()
{
if [ -z "$(find "$1" -type f)" ]; then
echo "ERROR: Missing source file(s) \"$1\"" >&2
exit 2
fi
unset IFS
for SOURCE in `find "$1" -type f |grep -v -e '/\.svn/' -e '/\.git/'`; do
if echo "$2" |grep -q '/$'; then
FN="${SOURCE#$1}"
if [ -z "$FN" ]; then
TARGET="$2$(basename "$1")"
else
TARGET="$2$FN"
fi
TARGET_DIR="$2"
else
TARGET="$2"
TARGET_DIR="$(dirname "$2")"
fi
if [ ! -d "$TARGET_DIR" ]; then
printf "\033[40m\033[1;31m* WARNING: Target directory $TARGET_DIR does not exist. Skipping copy of $SOURCE!\033[0m\n" >&2
continue
fi
# NOTE: Always copy, even if contents is the same to make sure permissions are updated
if ! cp -fv --preserve=mode,timestamps "$SOURCE" "$TARGET"; then
echo "ERROR: Copy of \"$SOURCE\" to \"$TARGET\" failed!" >&2
exit 3
fi
chown 0:0 "$TARGET"
done
return 0
}
get_user_yn()
{
if [ "$2" = "y" ]; then
printf "$1 (Y/n)? "
else
printf "$1 (y/N)? "
fi
read answer_with_case
ANSWER=`echo "$answer_with_case" |tr A-Z a-z`
if [ "$ANSWER" = "y" -o "$ANSWER" = "yes" ]; then
return 0
fi
if [ "$ANSWER" = "n" -o "$ANSWER" = "no" ]; then
return 1
fi
# Fallback to default
if [ "$2" = "y" ]; then
return 0
else
return 1
fi
}
check_18_version()
{
if grep -q "^MY_VERSION=" "/etc/init.d/arno-iptables-firewall" 2>/dev/null; then
if get_user_yn "WARNING: An old version is still installed. Removing it first is *STRONGLY* recommended. Remove" "y"; then
rm -fv /etc/init.d/arno-iptables-firewall
mv -fv /etc/arno-iptables-firewall/custom-rules /etc/arno-iptables-firewall/custom-rules.old
mv -fv /etc/arno-iptables-firewall/firewall.conf /etc/arno-iptables-firewall/firewall.conf.old
rm -fv /etc/arno-iptables-firewall/plugins/*.plugin
rm -fv /etc/rc*.d/*arno-iptables-firewall
fi
fi
}
check_dist_version()
{
if [ -f /usr/sbin/arno-iptables-firewall ]; then
if ! get_user_yn "WARNING: It seems a distribution version is already installed. It's *STRONGLY* recommended to remove it first. Continue anyway" "n"; then
return 1
fi
fi
return 0
}
# Check plugins for (old) versions with different priority
check_plugins()
{
if [ -d /usr/local/share/arno-iptables-firewall/plugins ] && ls /usr/local/share/arno-iptables-firewall/plugins/*.plugin >/dev/null 2>&1; then
unset IFS
for PLUGIN_FILE in ./share/arno-iptables-firewall/plugins/*.plugin; do
PLUGIN_NAME="$(basename "$PLUGIN_FILE" |sed 's/^[0-9]*//')"
ls /usr/local/share/arno-iptables-firewall/plugins/*.plugin 2>/dev/null |grep "/[0-9]*${PLUGIN_NAME}$" |grep -v "/$(basename "$PLUGIN_FILE")$" |while IFS=$EOL read PLUGIN_OLD; do
echo "* Removing old plugin: $PLUGIN_OLD"
rm -fv "$PLUGIN_OLD"
done
done
fi
}
setup_start_scripts()
{
# Install init.d script, but only if init.d folder exists
if [ -d "/etc/init.d" ]; then
copy_overwrite ./etc/init.d/arno-iptables-firewall /etc/init.d/
fi
# Make sure only one service file exists in /lib/.. or /usr/lib/ where we prefer /lib/
rm -f /usr/lib/systemd/system/arno-iptables-firewall.service
# Install service file if systemd directory is available, use fallbacks to support different systems
if [ -d "/lib/systemd/system" ]; then
copy_overwrite ./lib/systemd/system/arno-iptables-firewall.service /lib/systemd/system/
elif [ -d "/usr/lib/systemd/system" ]; then
copy_overwrite ./lib/systemd/system/arno-iptables-firewall.service /usr/lib/systemd/system/
elif [ -d "/etc/systemd/system" ]; then
copy_ask_if_exist ./lib/systemd/system/arno-iptables-firewall.service /etc/systemd/system/ "y"
else
echo "NOTE: Could not find any systemd/system directory, skipping systemd configuration" >&2
fi
RC_PATH="/etc"
# Check for Redhat/SUSE rc.d
if [ -d "/etc/rc.d" ]; then
RC_PATH="/etc/rc.d"
fi
# Remove any symlinks in rc*.d out of the way
rm -f $RC_PATH/rc0.d/*arno-iptables-firewall
rm -f $RC_PATH/rc1.d/*arno-iptables-firewall
rm -f $RC_PATH/rc2.d/*arno-iptables-firewall
rm -f $RC_PATH/rc3.d/*arno-iptables-firewall
rm -f $RC_PATH/rc4.d/*arno-iptables-firewall
rm -f $RC_PATH/rc5.d/*arno-iptables-firewall
rm -f $RC_PATH/rc6.d/*arno-iptables-firewall
rm -f $RC_PATH/rcS.d/*arno-iptables-firewall
if get_user_yn "Do you want to start the firewall at boot" "y"; then
DONE=0
if check_command systemctl; then
if systemctl enable arno-iptables-firewall; then
echo "* Successfully enabled service with systemctl"
DONE=1
fi
elif check_command update-rc.d; then
# Note: Currently update-rc.d doesn't seem to properly use the init script's LSB header, so specify explicitly
if update-rc.d -f arno-iptables-firewall start 11 S . stop 10 0 6 .; then
echo "* Successfully enabled service with update-rc.d"
DONE=1
fi
elif check_command chkconfig; then
if chkconfig --add arno-iptables-firewall && chkconfig arno-iptables-firewall on; then
echo "* Successfully enabled service with chkconfig"
DONE=1
fi
else
if [ -d "$RC_PATH/rcS.d" ]; then
if ln -sv /etc/init.d/arno-iptables-firewall "$RC_PATH/rcS.d/S11arno-iptables-firewall" &&
ln -sv /etc/init.d/arno-iptables-firewall "$RC_PATH/rc0.d/K10arno-iptables-firewall" &&
ln -sv /etc/init.d/arno-iptables-firewall "$RC_PATH/rc6.d/K10arno-iptables-firewall"; then
echo "* Successfully enabled service through $RC_PATH/rcS.d/ symlink"
DONE=1
fi
elif [ -d "$RC_PATH/rc2.d" ]; then
if ln -sv /etc/init.d/arno-iptables-firewall "$RC_PATH/rc2.d/S09arno-iptables-firewall" &&
ln -sv /etc/init.d/arno-iptables-firewall "$RC_PATH/rc0.d/K91arno-iptables-firewall" &&
ln -sv /etc/init.d/arno-iptables-firewall "$RC_PATH/rc6.d/K91arno-iptables-firewall"; then
echo "* Successfully enabled service through $RC_PATH/rc2.d/ symlink"
DONE=1
fi
else
echo "WARNING: Unable to detect /rc2.d or /rcS.d directories. Skipping runlevel symlinks" >&2
fi
fi
if [ $DONE -eq 0 ]; then
echo "ERROR: Unable to setup automatic start at boot. Please investigate" >&2
fi
fi
}
# main line:
AIF_VERSION="$(grep "MY_VERSION=" ./bin/arno-iptables-firewall |sed -e "s/^MY_VERSION=\"//" -e "s/\"$//")"
printf "\033[40m\033[1;32mArno's Iptables Firewall Script(AIF) v$AIF_VERSION\033[0m\n"
printf "Install Script v$MY_VERSION\n"
echo "-------------------------------------------------------------------------------"
sanity_check
# We want to run in the dir the install script is in
cd "$(dirname $0)" || exit 1
if ! get_user_yn "Continue install" "n"; then
echo "*Install aborted"
exit 1
fi
# Make sure an old version is not still installed
check_18_version
# Make sure a dist version is not already installed
if ! check_dist_version; then
echo "*Install aborted"
exit 1
fi
copy_overwrite ./bin/arno-iptables-firewall /usr/local/sbin/
copy_overwrite ./bin/arno-fwfilter /usr/local/bin/
# Remove old version:
rm -f /usr/local/sbin/arno-fwfilter
mkdir -pv /usr/local/share/arno-iptables-firewall/plugins || exit 1
copy_overwrite ./share/arno-iptables-firewall/ /usr/local/share/arno-iptables-firewall/
if [ ! -f /usr/local/sbin/traffic-accounting-show ]; then
ln -sv /usr/local/share/arno-iptables-firewall/plugins/traffic-accounting-show /usr/local/sbin/traffic-accounting-show
fi
mkdir -pv /usr/local/share/man/man1 || exit 1
mkdir -pv /usr/local/share/man/man8 || exit 1
gzip -c -v ./share/man/man8/arno-iptables-firewall.8 >/usr/local/share/man/man8/arno-iptables-firewall.8.gz
gzip -c -v ./share/man/man1/arno-fwfilter.1 >/usr/local/share/man/man8/arno-fwfilter.1.gz
mkdir -pv /usr/local/share/doc/arno-iptables-firewall || exit 1
copy_overwrite ./README /usr/local/share/doc/arno-iptables-firewall/
# Install rsyslog config file (if rsyslog is available)
if [ -d "/etc/rsyslog.d" ]; then
copy_ask_if_exist ./etc/rsyslog.d/arno-iptables-firewall.conf /etc/rsyslog.d/ "y" "old" "dist"
fi
copy_ask_if_exist ./etc/logrotate.d/arno-iptables-firewall /etc/logrotate.d/ "y"
mkdir -pv /etc/arno-iptables-firewall || exit 1
copy_overwrite ./etc/arno-iptables-firewall/firewall.conf /etc/arno-iptables-firewall/firewall.conf.dist
copy_ask_if_exist ./etc/arno-iptables-firewall/firewall.conf /etc/arno-iptables-firewall/
copy_skip_if_exist ./etc/arno-iptables-firewall/custom-rules /etc/arno-iptables-firewall/
mkdir -pv /etc/arno-iptables-firewall/plugins || exit 1
copy_ask_if_exist ./etc/arno-iptables-firewall/plugins/ /etc/arno-iptables-firewall/plugins/ "n" "old" "dist"
mkdir -pv /etc/arno-iptables-firewall/conf.d || exit 1
echo "Files with a .conf extension in this directory will be sourced by the environment file" >/etc/arno-iptables-firewall/conf.d/README
# Check old plugins
check_plugins
setup_start_scripts
echo ""
echo "** Install done **"
echo ""
if get_user_yn "Do you want to run the configuration script"; then
./configure.sh
fi
echo ""
echo "-------------------------------------------------------------------------------"
echo "** NOTE: You can now (manually) start the firewall by executing **"
echo "** \"/usr/local/sbin/arno-iptables-firewall start\" **"
echo "** It is recommended however to first review the settings in **"
echo "** /etc/arno-iptables-firewall/firewall.conf! **"
echo "-------------------------------------------------------------------------------"
echo ""
if get_user_yn "(Re)start firewall"; then
/usr/local/sbin/arno-iptables-firewall restart
fi
exit 0