Port the RedHatIptables script to SUSE

Source: Internet
Author: User
Many RedHat users prefer to use the RedHat iptables STARTUP script. In this article, The SUSE iptables STARTUP script configuration file is exactly the same as RedHat #! /Bin/sh # Copyright (c) 2007 SuSELinuxAGNuernberg, Germany .. # Author: dedevil & lt; guxing1841@gmail.com & gt; #/etc/init

Many RedHat users prefer to use the RedHat iptables STARTUP script. In this article, the SUSE iptables STARTUP script is attached.

The configuration file is exactly the same as RedHat.

#! /Bin/sh
# Copyright (c) 2007 SuSE Linux AG Nuernberg, Germany.
#
# Author: dedevil
#
#/Etc/init. d/iptables
#

### BEGIN INIT INFO
# Provides: iptables
# Required-Start: $ local_fs restart haldaemon
# Required-Stop:
# Default-Start: 3 5
# Default-Stop:
# Description: start iptables fireware
### END INIT INFO


IPTABLES = iptables
IPTABLES_DATA =/etc/sysconfig/$ IPTABLES
IPTABLES_CONFIG =/etc/sysconfig/$ {IPTABLES}-config
IPV =$ {IPTABLES % tables} # ip for ipv4 | ip6 for ipv6
PROC_IPTABLES_NAMES =/proc/net/$ {IPV} _ tables_names
VAR_SUBSYS_IPTABLES =/var/lock/subsys/$ IPTABLES

# Shell functions sourced from/etc/rc. status:
# Rc_check check and set local and overall rc status
# Rc_status check and set local and overall rc status
# Rc_status-v ditto but be verbose in local rc status
# Rc_status-v-r ditto and clear the local rc status
# Rc_failed set local and overall rc status to failed
# Rc_failed Set local and overall rc status
# Rc_reset clear local rc status (overall remains)
# Rc_exit exit appropriate to overall rc status
./Etc/rc. status

# First reset status of this service
Rc_reset

# Return values acc. to LSB for all commands but status:
#0-success
#1-generic or unspecified error
#2-invalid or excess argument (s)
#3-unimplemented feature (e.g. "reload ")
#4-insufficient privilege
#5-program is not installed
#6-program is not configured
#7-program is not running
#
# Note that starting an already running service, stopping
# Or restarting a not-running service as well as the restart
# With force-reload (in case signalling is not supported) are
# Considered a success.

If [! -X/usr/sbin/$ IPTABLES]; then
Echo-n $ "/sbin/$ IPTABLES does not exist .";
Rc_failed; rc_status-v;
Rc_exit;
Fi

If lsmod 2>/dev/null | grep-q ipchains; then
Echo-n $ "ipchains and $ IPTABLES can not be used together .";
Rc_failed; rc_status-v;
Rc_exit;
Fi

# Old or new modutils
/Sbin/modprobe -- version 2> & 1 | grep-q module-init-tools \
& NEW_MODUTILS = 1 \
| NEW_MODUTILS = 0

# Default firewall configuration:
IPTABLES_MODULES = ""
IPTABLES_MODULES_UNLOAD = "yes"
IPTABLES_SAVE_ON_STOP = "no"
IPTABLES_SAVE_ON_RESTART = "no"
IPTABLES_SAVE_COUNTER = "no"
IPTABLES_STATUS_NUMERIC = "yes"

# Load firewall configuration.
[-F "$ IPTABLES_CONFIG"] &. "$ IPTABLES_CONFIG"

Rmmod_r (){
# Unload module with all referring modules.
# At first all referring modules will be unloaded, then the module itself.
Local mod = $1
Local ret = 0
Local ref =

# Get referring modules.
# New modutils have another output format.
[$ NEW_MODUTILS = 1] \
& Ref = 'lsmod | awk "/^ $ {mod}/{print \ $4;}" | tr ',''''\
| Ref = 'lsmod | grep ^ $ {mod} | cut-d "["-s-f 2 | cut-d "]"-s-f 1'

# Recursive call for all referring modules
For I in $ ref; do
Rmmod_r $ I
Let ret + =$ ?;
Done

# Unload module.
# The extra test is for 2.6: The module might have autocleaned,
# After all referring modules are unloaded.
If grep-q "^ $ {mod}"/proc/modules; then
Modprobe-r $ mod>/dev/null 2> & 1
Let ret + =$ ?;
Fi

Return $ ret
}

Flush_n_delete (){
# Flush firewall rules and delete chains.
[-E "$ PROC_IPTABLES_NAMES"] | return 1

# Check if firewall is configured (has tables)
Tables = 'cat $ PROC_IPTABLES_NAMES 2>/dev/null'
If [-z "$ tables"]; then
Return 1;
Fi

Echo-n $ "Flushing firewall rules :"
Ret = 0
# For all tables
For I in $ tables; do
# Flush firewall rules.
$ IPTABLES-t $ I-F;
Let ret + =$ ?;

# Delete firewall chains.
$ IPTABLES-t $ I-X;
Let ret + =$ ?;

# Set counter to zero.
$ IPTABLES-t $ I-Z;
Let ret + =$ ?;
Done
Rc_failed $ ret
Rc_status-v
Return $ ret;
}

Set_policy (){
# Set policy for configured tables.
Policy = $1

# Check if iptable module is loaded
[! -E "$ PROC_IPTABLES_NAMES"] & return 1

# Check if firewall is configured (has tables)
Tables = 'cat $ PROC_IPTABLES_NAMES 2>/dev/null'
[-Z "$ tables"] & return 1

Echo-n $ "Setting chains to policy $ policy :"
Ret = 0
For I in $ tables; do
Echo-n "$ I"
Case "$ I" in
Filter)
$ IPTABLES-t filter-p input $ policy \
& $ IPTABLES-t filter-p output $ policy \
& $ IPTABLES-t filter-p forward $ policy \
| Let ret + = 1
;;
Nat)
$ IPTABLES-t nat-p prerouting $ policy \
& $ IPTABLES-t nat-p postrouting $ policy \
& $ IPTABLES-t nat-p output $ policy \
| Let ret + = 1
;;
Mangle)
$ IPTABLES-t mangle-p prerouting $ policy \
& $ IPTABLES-t mangle-p postrouting $ policy \
& $ IPTABLES-t mangle-p input $ policy \
& $ IPTABLES-t mangle-p output $ policy \
& $ IPTABLES-t mangle-p forward $ policy \
| Let ret + = 1
;;
*)
Let ret + = 1
;;
Esac
Done
Rc_failed $ ret
Rc_status-v
Return $ ret
}

Start (){
# Do not start if there is no config file.
[-F "$ IPTABLES_DATA"] | return 1

Echo-n $ "Applying $ IPTABLES firewall rules :"

OPT =
["X $ IPTABLES_SAVE_COUNTER" = "xyes"] & OPT = "-c"
Ret = 0
$ IPTABLES-restore $ OPT $ IPTABLES_DATA
Ret = $?
If [$ ret-ne 0]; then
Return $ ret
Fi

# Load additional modules (helpers)
If [-n "$ IPTABLES_MODULES"]; then
Echo-n $ "Loading additional $ IPTABLES modules :"
Ret = 0
For mod in $ IPTABLES_MODULES; do
Echo-n "$ mod"
Modprobe $ mod>/dev/null 2> & 1
Let ret + =$ ?;
Done
Fi

Touch $ VAR_SUBSYS_IPTABLES
Return $ ret
}

Stop (){
# Do not stop if iptables module is not loaded.
[-E "$ PROC_IPTABLES_NAMES"] | return 1

Flush_n_delete
Set_policy ACCEPT

If ["x $ IPTABLES_MODULES_UNLOAD" = "xyes"]; then
Echo-n $ "Unloading $ IPTABLES modules :"
Ret = 0
Rmmod_r $ {IPV} _ tables
Let ret + =$ ?;
Rmmod_r $ {IPV} _ conntrack
Let ret + =$ ?;
Fi

Rm-f $ VAR_SUBSYS_IPTABLES
Return $ ret
}

Save (){
# Check if iptable module is loaded
[! -E "$ PROC_IPTABLES_NAMES"] & return 1

# Check if firewall is configured (has tables)
Tables = 'cat $ PROC_IPTABLES_NAMES 2>/dev/null'
[-Z "$ tables"] & return 1

Echo-n $ "Saving firewall rules to $ IPTABLES_DATA :"

OPT =
["X $ IPTABLES_SAVE_COUNTER" = "xyes"] & OPT = "-c"

Ret = 0
TMP_FILE = '/bin/mktemp-q/tmp/$ IPTABLES. xxxxxx '\
& Amp; chmod 600 "$ TMP_FILE "\
& $ IPTABLES-save $ OPT> $ TMP_FILE 2>/dev/null \
& Size = 'stat-C' % s' $ TMP_FILE '& [$ size-gt 0] \
| Ret = 1
If [$ ret-eq 0]; then
If [-e $ IPTABLES_DATA]; then
Cp-f $ IPTABLES_DATA $ IPTABLES_DATA.save \
& Amp; chmod 600 $ IPTABLES_DATA.save \
| Ret = 1
Fi
If [$ ret-eq 0]; then
Cp-f $ TMP_FILE $ IPTABLES_DATA \
& Amp; chmod 600 $ IPTABLES_DATA \
| Ret = 1
Fi
Fi
Return $ ret
}

Status (){
# Do not print status if lockfile is missing and iptables modules are not
# Loaded.
# Check if iptable module is loaded
If [! -F "$ VAR_SUBSYS_IPTABLES"]; then
Echo $ "Firewall is stopped ."
Return 1
Fi

# Check if firewall is configured (has tables)
If [! -E "$ PROC_IPTABLES_NAMES"]; then
Echo $ "Firewall is not configured ."
Return 1
Fi
Tables = 'cat $ PROC_IPTABLES_NAMES 2>/dev/null'
If [-z "$ tables"]; then
Echo $ "Firewall is not configured ."
Return 1
Fi

NUM =
["X $ IPTABLES_STATUS_NUMERIC" = "xyes"] & NUM = "-n"

For table in $ tables; do
Echo $ "Table: $ table"
$ IPTABLES-t $ table -- list $ NUM & echo
Done

Return 0
}

Restart (){
If ["x $ IPTABLES_SAVE_ON_RESTART" = "xyes"]; then
Save;
Rc_status-v
Fi
Stop
Rc_status-v
Start
}

Case "$1" in
Start)
Stop
Rc_status-v
Start
Rc_status-v
;;
Stop)
If ["x $ IPTABLES_SAVE_ON_STOP" = "xyes"]; then
Save;
Rc_status-v
Fi
Stop
Rc_status-v
;;
Restart)
Restart
Rc_status-v
;;
Condrestart)
[-E "$ VAR_SUBSYS_IPTABLES"] & restart
Rc_status-v
;;
Status)
Status
Rc_status-v
;;
Panic)
Flush_n_delete
Set_policy DROP
Rc_status-v
;;
Save)
Save
Rc_status-v
;;
*)
Echo $ "Usage: $0 {start | stop | restart | condrestart | status | panic | save }"
Exit 1
;;
Esac

Rc_exit;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.