Debian Codegraph @

scripts/sbin/drbl-nat-rules

raw symbols SCIP
1 #!/bin/bashscip-shell filesystem . . `/bin/bash`.
2 # Authors: Steven Shiau <steven _at_ clonezilla org>, Ceasar Sun <ceasar _at_ nchc org tw>
3 # License: GPL
4 #
5 # set the iptables NAT
6 # clean the old tables
7
8 #
9 DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}"
10 . $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions
11
12
13 # Borrow some codes from /etc/init.d/iptables in RedHat.
14 IPTABLES=iptables
15 IPV=${IPTABLES%tables} # ip for ipv4 | ip6 for ipv6
16 PROC_IPTABLES_NAMES=/proc/net/${IPV}_tables_names
17 NAT_RULES_FOR_RH=$SYSCONF_PATH/iptables
18 NAT_RULES_FOR_DEBIAN=$SYSCONF_PATH/drbl-nat.up.rules
19 NAT_RULES_FOR_SUSE=$SYSCONF_PATH/SuSEfirewall2
20
21 #
22 flush_n_deletescip-shell shell shell-project 0.0.0 flush_n_delete().() {
23 # Flush firewall rules and delete chains.
24 [scip-shell system . . `[`. -e "$PROC_IPTABLES_NAMES" ] || return 1
25
26 # Check if firewall is configured (has tables)
27 tables=`catscip-shell system . . cat. $PROC_IPTABLES_NAMES 2>/dev/nullscip-shell filesystem . . `/dev/null`.`
28 [scip-shell system . . `[`. -z "$tables" ] && return 1
29
30 echoscip-shell system . . echo. -n $"Flushing firewall rules: "
31 ret=0
32 # For all tables
33 for i in $tables; do
34 # Flush firewall rules.
35 $IPTABLES -t $i -F;
36 let ret+=$?;
37
38 # Delete firewall chains.
39 $IPTABLES -t $i -X;
40 let ret+=$?;
41
42 # Set counter to zero.
43 $IPTABLES -t $i -Z;
44 let ret+=$?;
45 done
46
47 [scip-shell system . . `[`. $ret -eq 0 ] && echoscip-shell system . . echo. "success" || echoscip-shell system . . echo. "failure"
48 }
49
50 # save rules
51 save_rulesscip-shell shell shell-project 0.0.0 save_rules().() {
52 # Save the rules.
53 if [scip-shell system . . `[`. -e /etc/debian_versionscip-shell filesystem . . `/etc/debian_version`. ]; then
54 # Debian
55 # We have to save our rules.
56 iptables-save > $NAT_RULES_FOR_DEBIAN
57 chmodscip-shell system . . chmod. 600 $NAT_RULES_FOR_DEBIAN
58 elif [scip-shell system . . `[`. -e /etc/SuSE-releasescip-shell filesystem . . `/etc/SuSE-release`. ]; then
59 # SuSE
60 # we already set that in /etc/sysconfig/SuSEfirewall2, so it not necessary
61 continue
62 elif [scip-shell system . . `[`. -n "$(command -v systemctl 2>/dev/nullscip-shell filesystem . . `/dev/null`.)" -a -e "/lib/systemd/system/iptables.servicescip-shell filesystem . . `/lib/systemd/system/iptables.service`." ]; then
63 # for FC 17, add by Ceasar
64 echoscip-shell system . . echo. "Save iptables service ,run :iptables-save"
65 iptables-save > $NAT_RULES_FOR_RH
66 else
67 # RH-like, we use its service
68 /etc/init.d/iptablesscip-shell filesystem . . `/etc/init.d/iptables`. save
69 fi
70 }
71
72 # set not to unload iptables module
73 set_not_unload_iptables_module_RHscip-shell shell shell-project 0.0.0 set_not_unload_iptables_module_RH().() {
74 if [scip-shell system . . `[`. ! -f $SYSCONF_PATH/iptables-config ]; then
75 echoscip-shell system . . echo. 'IPTABLES_MODULES_UNLOAD="no"' > $SYSCONF_PATH/iptables-config
76 else
77 if grepscip-shell system . . grep. -E -q "^[#\s]*IPTABLES_MODULES_UNLOAD=.*" $SYSCONF_PATH/iptables-config; then
78 perlscip-shell system . . perl. -p -i -e "s/^[#\s]*IPTABLES_MODULES_UNLOAD=.*/IPTABLES_MODULES_UNLOAD=\"no\"/" $SYSCONF_PATH/iptables-config
79 else
80 echoscip-shell system . . echo. 'IPTABLES_MODULES_UNLOAD="no"' >> $SYSCONF_PATH/iptables-config
81 fi
82 fi
83 }
84
85 do_stop_iptablesscip-shell shell shell-project 0.0.0 do_stop_iptables().() {
86 echoscip-shell system . . echo. "Now stop the NAT service..."
87 if [scip-shell system . . `[`. -e /etc/debian_versionscip-shell filesystem . . `/etc/debian_version`. ]; then
88 # Debian
89 # We have to use the code borrowed from RH.
90 flush_n_deletescip-shell shell shell-project 0.0.0 flush_n_delete().
91 elif [scip-shell system . . `[`. -e /etc/SuSE-releasescip-shell filesystem . . `/etc/SuSE-release`. ]; then
92 # SuSE
93 # TODO, can it (the cleaned one) save back to system in SuSE ?
94 # Now this seems to do nothing.
95 /etc/init.d/SuSEfirewall2_setupscip-shell filesystem . . `/etc/init.d/SuSEfirewall2_setup`. stop
96 elif [scip-shell system . . `[`. -n "$(command -v systemctl 2>/dev/nullscip-shell filesystem . . `/dev/null`.)" -a -e "/lib/systemd/system/iptables.servicescip-shell filesystem . . `/lib/systemd/system/iptables.service`." ]; then
97 # for FC 18/21, add by Ceasar
98 if [scip-shell system . . `[`. "$(LC_ALL=C firewall-cmd --state 2>/dev/nullscip-shell filesystem . . `/dev/null`.)" = "running" ]; then
99 echoscip-shell system . . echo. "Stop firewalld service ,run :systemctl stop firewalld.service"
100 systemctl stop firewalld.service
101 fi
102 echoscip-shell system . . echo. "Stop iptables service ,run :systemctl stop iptables.service"
103 systemctl stop iptables.service
104 else
105 # RH-like
106 /etc/init.d/iptablesscip-shell filesystem . . `/etc/init.d/iptables`. stop
107 fi
108 }
109 #
110 check_if_root
111
112 # main
113 usagescip-shell shell shell-project 0.0.0 usage().() {
114 echoscip-shell system . . echo. "To generate or clean NAT setting for DRBL clients to access"
115 echoscip-shell system . . echo. "Usage: $0 [Options] {generate|clean|regenerate|stop|status}"
116 echoscip-shell system . . echo. "Options:"
117 echoscip-shell system . . echo. "-a, --all-subnet: Make all subnet can access to this NAT server."
118 echoscip-shell system . . echo. "-v, --verbose: Verbose mode."
119 echoscip-shell system . . echo. "Example: To generate NAT setting for DRBL clients to access"
120 echoscip-shell system . . echo. "$0 generate"
121 }
122
123 # default setting
124 all_subnet="no"
125
126 while [scip-shell system . . `[`. $# -gt 0 ]; do
127 case "$1" in
128 -a|--all-subnet)
129 all_subnet="yes"
130 shift;;
131 -v|--verbose)
132 shift; verbose="on"
133 ;;
134 -*) echoscip-shell system . . echo. "${0}: ${1}: invalid option" >&2
135 usagescip-shell shell shell-project 0.0.0 usage(). >& 2
136 exit 2 ;;
137 *) break ;;
138 esac
139 done
140 switch=$1
141
142 #
143 [scip-shell system . . `[`. -z "$switch" ] && usagescip-shell shell shell-project 0.0.0 usage(). && exit 1
144
145 #
146 case "$switch" in
147 generate)
148 echoscip-shell system . . echo. "Now generate the firewall rules for NAT service..."
149
150 if [scip-shell system . . `[`. -f /etc/SuSE-releasescip-shell filesystem . . `/etc/SuSE-release`. ]; then
151 # SuSE
152 # SuSE has its own wrapper program, we use that.
153 [scip-shell system . . `[`. -f $SYSCONF_PATH/SuSEfirewall2 ] && cpscip-shell system . . cp. -f $SYSCONF_PATH/SuSEfirewall2 $SYSCONF_PATH/SuSEfirewall2.drblsave
154 client_ip_nets=""
155 if [scip-shell system . . `[`. "$all_subnet" = "yes" ]; then
156 # Exporting whole subnet to clients
157 subnet_list="$(get-client-ip-list | awkscip-shell system . . awk. -F"." '{print $1"."$2"."$3}' | sortscip-shell system . . sort. | uniqscip-shell system . . uniq. )"
158 for subnet in $subnet_list; do
159 client_ip_nets="$client_ip_nets ${subnet}.0/24"
160 done
161 else
162 # line by line
163 for ip in `get-client-ip-list`; do
164 client_ip_nets="$client_ip_nets $ip/32"
165 done
166 fi
167 DEV_INT="$(grepscip-shell system . . grep. "^interface=" /etc/drbl/drblpush.confscip-shell filesystem . . `/etc/drbl/drblpush.conf`. | sedscip-shell system . . sed. -e "s/interface=//g")"
168 # use echo to convert them into one line instead of many lines.
169 DEV_INT="$(echoscip-shell system . . echo. $DEV_INT)"
170 # TODO, eth0 ? auto ? or other variable ?
171 perlscip-shell system . . perl. -pi -e "s/^FW_DEV_EXT=.*/FW_DEV_EXT=auto/g" $SYSCONF_PATH/SuSEfirewall2
172 perlscip-shell system . . perl. -pi -e "s/^FW_DEV_INT=.*/FW_DEV_INT=\"$DEV_INT\"/g" $SYSCONF_PATH/SuSEfirewall2
173 perlscip-shell system . . perl. -pi -e "s/^FW_ROUTE=.*/FW_ROUTE=\"yes\"/g" $SYSCONF_PATH/SuSEfirewall2
174 perlscip-shell system . . perl. -pi -e "s/^FW_MASQUERADE=.*/FW_MASQUERADE=\"yes\"/g" $SYSCONF_PATH/SuSEfirewall2
175 perlscip-shell system . . perl. -pi -e "s|^FW_MASQ_NETS=.*|FW_MASQ_NETS=\"$client_ip_nets\"|g" $SYSCONF_PATH/SuSEfirewall2
176 # at least we have to turn on ssh so that dcs can send commands
177 perlscip-shell system . . perl. -pi -e "s|^FW_SERVICES_EXT_TCP=.*|FW_SERVICES_EXT_TCP=\"ssh\"|g" $SYSCONF_PATH/SuSEfirewall2
178 else
179 # RH-like & Debian
180 # backup the orig files for RH-like distribution if necessary
181 # Since it may not only consist of NAT, but also other rules.
182 # For Debian's $NAT_RULES_FOR_DEBIAN, we are sure it only for NAT. We will overwrite it.
183 if [scip-shell system . . `[`. -f $SYSCONF_PATH/iptables ]; then
184 [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
185 echoscip-shell system . . echo. "Warning! $SYSCONF_PATH/iptables is renamed as iptables.drblsave!"
186 echoscip-shell system . . echo. "Your firewall rules is overwritten!!! We set the NAT for clients to access this DRBL server!"
187 [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
188 mvscip-shell system . . mv. -f $SYSCONF_PATH/iptables $SYSCONF_PATH/iptables.drblsave
189 fi
190
191 echoscip-shell system . . echo. "Stop the NAT service first..."
192 do_stop_iptablesscip-shell shell shell-project 0.0.0 do_stop_iptables().
193
194 iptables -P FORWARD ACCEPT
195 iptables -P INPUT ACCEPT
196 iptables -P OUTPUT ACCEPT
197
198 if [scip-shell system . . `[`. "$all_subnet" = "yes" ]; then
199 # Exporting whole subnet to clients
200 subnet_list="$(get-client-ip-list | awkscip-shell system . . awk. -F"." '{print $1"."$2"."$3}' | sortscip-shell system . . sort. | uniqscip-shell system . . uniq. )"
201 for subnet in $subnet_list; do
202 iptables -t nat -A POSTROUTING -s ${subnet}.0/24 -j MASQUERADE
203 done
204 else
205 # line by line
206 for ip in `get-client-ip-list`; do
207 iptables -t nat -A POSTROUTING -s $ip/32 -j MASQUERADE
208 done
209 fi
210
211 # Save the rules.
212 save_rulesscip-shell shell shell-project 0.0.0 save_rules().
213
214 # Some bug in kernel 2.6.9, so we do not want to remove the module before
215 # restart the iptables
216 # Only for RH-like.
217 if [scip-shell system . . `[`. ! -f /etc/debian_versionscip-shell filesystem . . `/etc/debian_version`. ]; then
218 set_not_unload_iptables_module_RHscip-shell shell shell-project 0.0.0 set_not_unload_iptables_module_RH().
219 fi
220 fi
221
222 turn_on_ipv4_forward
223 ;;
224
225 stop)
226 do_stop_iptablesscip-shell shell shell-project 0.0.0 do_stop_iptables().
227 ;;
228
229 clean)
230 do_stop_iptablesscip-shell shell shell-project 0.0.0 do_stop_iptables().
231 # Save the empty rules to system.
232 save_rulesscip-shell shell shell-project 0.0.0 save_rules().
233 ;;
234
235 regenerate)
236 $0 clean
237 $0 genereate
238 ;;
239
240 status)
241 if [scip-shell system . . `[`. -e /etc/debian_versionscip-shell filesystem . . `/etc/debian_version`. ]; then
242 # Debian
243 catscip-shell system . . cat. $NAT_RULES_FOR_DEBIAN
244 elif [scip-shell system . . `[`. -e /etc/SuSE-releasescip-shell filesystem . . `/etc/SuSE-release`. ]; then
245 # SuSE
246 catscip-shell system . . cat. $NAT_RULES_FOR_SUSE
247 else
248 # RH-like
249 catscip-shell system . . cat. $NAT_RULES_FOR_RH
250 fi
251 ;;
252 *)
253 usagescip-shell shell shell-project 0.0.0 usage().
254 exit 1
255 ;;
256 esac