Debian Codegraph @

iptb-expflush-plugin

raw
1 #!/bin/sh
2
3 # This file is part of netfilter-persistent
4 # (was iptables-persistent)
5 # Copyright (C) 2024, Emmanuel BENOÎT <[email protected]>
6 #
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License
9 # as published by the Free Software Foundation, either version 3
10 # of the License, or (at your option) any later version.
11 #
12 # This script manages flushing some chains either explicitly or
13 # based on the contents of /etc/iptables/rules.v[46] when the
14 # --no-flush option is being used for ip{6,}tables-restore.
15
16 set -e
17
18 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
19
20 # Source configuration
21 if [ -f "/etc/default/netfilter-persistent" ]; then
22 . /etc/default/netfilter-persistent
23 fi
24
25 case "`basename $0`" in
26 *iptb4*)
27 RULES_FILE=/etc/iptables/rules.v4
28 IPTB_CMD=iptables
29 RESTORE_NOFLUSH="${IPTABLES_RESTORE_NOFLUSH}"
30 EXPLICIT_FLUSH="${IPTABLES_EXPLICIT_FLUSH}"
31 ;;
32 *iptb6*)
33 RULES_FILE=/etc/iptables/rules.v6
34 IPTB_CMD=ip6tables
35 RESTORE_NOFLUSH="${IP6TABLES_RESTORE_NOFLUSH}"
36 EXPLICIT_FLUSH="${IP6TABLES_EXPLICIT_FLUSH}"
37 ;;
38 *)
39 echo "Unexpected file name, must contain either iptb4 or iptb6" >&2
40 exit 1
41 esac
42
43 read_existing_rules()
44 {
45 if [ ! -f $RULES_FILE ]; then
46 return
47 fi
48
49 local cur_table=""
50 local found=""
51 while read file_line; do
52 case "$file_line" in
53 \**)
54 cur_table="`echo "$file_line" | cut -c2-`"
55 ;;
56 -A*)
57 local chain="`echo "x$file_line" | cut -f2 -d' '`"
58 if [ "${cur_table}x" = "x" ]; then
59 echo "Found chain \`$chain' before table start" >&2
60 else
61 local must_add="${cur_table}.${chain}"
62 if ! echo "$found" | grep -qw "$must_add" ; then
63 if [ "${found}x" != "x" ]; then
64 found="${found},"
65 fi
66 found="${found}${must_add}"
67 fi
68 fi
69 ;;
70 *)
71 ;;
72 esac
73 done < $RULES_FILE
74
75 echo "$found"
76 }
77
78 flush_rules()
79 {
80 if [ "${RESTORE_NOFLUSH}x" != "yesx" ]; then
81 return
82 fi
83
84 case "${EXPLICIT_FLUSH}" in
85 auto)
86 FLUSH_RULES="`read_existing_rules`"
87 ;;
88
89 yes:*)
90 FLUSH_RULES="`echo "${EXPLICIT_FLUSH}" | cut -d: -f2`"
91 ;;
92 *)
93 return
94 ;;
95 esac
96
97 IFS=","
98 for target in ${FLUSH_RULES}
99 do
100 local table="`echo "$target" | cut -f1 -d.`"
101 local chain="`echo "$target" | cut -f2 -d.`"
102 local err="`LANG=C ${IPTB_CMD} -t $table -F $chain 2>&1`"
103 # Don't stop on missing chain errors, as it could just be that
104 # the chain hasn't been created yet.
105 if [ "x$err" != "x" ] && [ "x$err" != "x${IPTB_CMD}: No chain/target/match by that name." ]; then
106 echo "When flushing $target: $err" >&2
107 return 1
108 fi
109 done
110 unset IFS
111 }
112
113 # Rules must be flushed before starting, when restarting, or when explicitly
114 # requested.
115 case "$1" in
116 start|restart|reload|force-reload|flush)
117 flush_rules
118 ;;
119 save|stop)
120 ;;
121 *)
122 echo "Usage: $0 {start|restart|reload|force-reload|save|flush}" >&2
123 exit 1
124 ;;
125 esac