Blame SOURCES/ipset.start-stop

a0fbec
#!/bin/bash
a0fbec
#
a0fbec
# ipset      Start and stop ipset firewall sets
a0fbec
#
a0fbec
# config: /etc/ipset/ipset
a0fbec
#
a0fbec
a0fbec
IPSET=ipset
a0fbec
IPSET_BIN=/usr/sbin/${IPSET}
a0fbec
IPSET_DATA=/etc/${IPSET}/${IPSET}
a0fbec
a0fbec
IPTABLES_CONFIG=/etc/sysconfig/iptables-config
a0fbec
IP6TABLES_CONFIG=${IPTABLES_CONFIG/iptables/ip6tables}
a0fbec
a0fbec
TMP_FIFO=/tmp/${IPSET}.$$
a0fbec
a0fbec
if [[ ! -x ${IPSET_BIN} ]]; then
a0fbec
	echo "${IPSET_BIN} does not exist."
a0fbec
	exit 5
a0fbec
fi
a0fbec
a0fbec
CLEAN_FILES=TMP_FIFO
a0fbec
trap "rm -f \$CLEAN_FILES" EXIT
a0fbec
a0fbec
# Default ipset configuration:
a0fbec
[[ -z $IPSET_SAVE_ON_STOP ]] && IPSET_SAVE_ON_STOP=no		# Overridden by ip(6)tables IP(6)TABLES_SAVE_ON_STOP
a0fbec
[[ -z $IPSET_SAVE_ON_RESTART ]] && IPSET_SAVE_ON_RESTART=no	# Overridden by ip(6)tables IP(6)TABLES_SAVE_ON_RESTART
a0fbec
a0fbec
# Load iptables configuration(s)
a0fbec
[[ -f "$IPTABLES_CONFIG" ]] && . "$IPTABLES_CONFIG"
a0fbec
[[ -f "$IP6TABLES_CONFIG" ]] && . "$IP6TABLES_CONFIG"
a0fbec
a0fbec
# It doesn't make sense to save iptables config and not our config
a0fbec
[[ ${IPTABLES_SAVE_ON_STOP} = yes || ${IP6TABLES_SAVE_ON_STOP} = yes ]] && IPSET_SAVE_ON_STOP=yes
a0fbec
[[ ${IPTABLES_SAVE_ON_RESTART} = yes || ${IP6TABLES_SAVE_ON_RESTART} = yes ]] && IPSET_SAVE_ON_RESTART=yes
a0fbec
a0fbec
check_can_unload() {
a0fbec
    # If the xt_set module is loaded and can't be unloaded, then iptables is
a0fbec
    # using ipsets, so refuse to stop the service.
a0fbec
    if [[ -n $(lsmod | grep "^xt_set ") ]]; then
a0fbec
	rmmod xt_set 2>/dev/null
a0fbec
	[[ $? -ne 0 ]] && echo Current iptables configuration requires ipsets && return 1
a0fbec
    fi
a0fbec
a0fbec
    return 0
a0fbec
}
a0fbec
a0fbec
flush_n_delete() {
a0fbec
    local ret=0 set
a0fbec
a0fbec
    # Flush sets
a0fbec
    ${IPSET_BIN} flush
a0fbec
    let ret+=$?
a0fbec
a0fbec
    # Delete ipset sets. If we don't do them individually, then none
a0fbec
    # will be deleted unless they all can be.
a0fbec
    for set in $(${IPSET_BIN} list -name); do
a0fbec
	    ${IPSET_BIN} destroy 2>/dev/null
a0fbec
	    [[ $? -ne 0 ]] && ret=1
a0fbec
    done
a0fbec
a0fbec
    return $ret
a0fbec
}
a0fbec
a0fbec
start_clean()
a0fbec
{
a0fbec
    mkfifo -m go= "${TMP_FIFO}"
a0fbec
    [[ $? -ne 0 ]] && return 1
a0fbec
a0fbec
    # Get the lists of sets in current(old) config and new config
a0fbec
    old_sets="$(${IPSET_BIN} list -name | sort -u)"
a0fbec
    new_sets="$(grep ^create "${IPSET_DATA}" | cut -d " " -f 2 | sort -u)"
a0fbec
a0fbec
    # List of sets no longer wanted
a0fbec
    drop_sets="$( printf "%s\n" "${old_sets}" > "${TMP_FIFO}"  &
a0fbec
		  printf "%s\n" "${new_sets}" | comm -23 "${TMP_FIFO}" -
a0fbec
		)"
a0fbec
a0fbec
    # Get rid of sets no longer needed
a0fbec
    # Unfortunately -! doesn't work for destroy, so we have to do it a command at a time
a0fbec
    for dset in $drop_sets; do
a0fbec
	ipset destroy $dset 2>/dev/null
a0fbec
	# If it won't go - ? in use by iptables, just clear it
a0fbec
	[[ $? -ne 0 ]] && ipset flush $dset
a0fbec
    done
a0fbec
a0fbec
    # Now delete the set members no longer required
a0fbec
    ${IPSET_BIN} save | grep "^add " | sort >${TMP_FIFO} &
a0fbec
      grep "^add " ${IPSET_DATA} | sort | comm -23 ${TMP_FIFO} - | sed -e "s/^add /del /" \
a0fbec
      | ${IPSET_BIN} restore -!
a0fbec
a0fbec
    # At last we can add the set members we haven't got
a0fbec
    ipset restore -! <${IPSET_DATA}
a0fbec
a0fbec
    rm ${TMP_FIFO}
a0fbec
a0fbec
    return 0
a0fbec
}
a0fbec
a0fbec
start() {
a0fbec
    # Do not start if there is no config file.
a0fbec
    [[ ! -f "$IPSET_DATA" ]] && echo "Loaded with no configuration" && return 0
a0fbec
a0fbec
    # We can skip the first bit and do a simple load if
a0fbec
    # there is no current ipset configuration
a0fbec
    res=1
a0fbec
    if [[ -n $(${IPSET_BIN} list -name) ]]; then
a0fbec
	# The following may fail for some bizarre reason
a0fbec
	start_clean
a0fbec
	res=$?
a0fbec
a0fbec
	[[ $res -ne 0 ]] && echo "Some old configuration may remain"
a0fbec
    fi
a0fbec
a0fbec
    # res -ne 0 => either start_clean failed, or we didn't need to run it
a0fbec
    if [[ $res -ne 0 ]]; then
a0fbec
	# This is the easy way to start but would leave any old
a0fbec
	# entries still configured. Still, better than nothing -
a0fbec
	# but fine if we had no config
a0fbec
	${IPSET_BIN} restore -! <${IPSET_DATA}
a0fbec
	res=$?
a0fbec
    fi
a0fbec
a0fbec
    if [[ $res -ne 0 ]]; then
a0fbec
	return 1
a0fbec
    fi
a0fbec
a0fbec
    return 0
a0fbec
}
a0fbec
a0fbec
stop() {
a0fbec
    # Nothing to stop if ip_set module is not loaded.
a0fbec
    lsmod | grep -q "^ip_set "
a0fbec
    [[ $? -ne 0 ]] && return 6
a0fbec
a0fbec
    flush_n_delete
a0fbec
    [[ $? -ne 0 ]] && echo Warning: Not all sets were flushed/deleted
a0fbec
a0fbec
    return 0
a0fbec
}
a0fbec
a0fbec
save() {
a0fbec
    # Do not save if ip_set module is not loaded.
a0fbec
    lsmod | grep -q "^ip_set "
a0fbec
    [[ $? -ne 0 ]] && return 6
a0fbec
a0fbec
    [[ -z $(${IPSET_BIN} list -name) ]] && return 0
a0fbec
a0fbec
    ret=0
a0fbec
    TMP_FILE=$(/bin/mktemp -q /tmp/$IPSET.XXXXXX) \
a0fbec
	&& CLEAN_FILES+=" $TMP_FILE" \
a0fbec
	&& chmod 600 "$TMP_FILE" \
a0fbec
	&& ${IPSET_BIN} save > $TMP_FILE 2>/dev/null \
a0fbec
	&& [[ -s $TMP_FILE ]] \
a0fbec
	|| ret=1
a0fbec
a0fbec
    if [[ $ret -eq 0 ]]; then
a0fbec
	# No need to do anything if the files are the same
a0fbec
	if [[ ! -f $IPSET_DATA ]]; then
a0fbec
	    mv $TMP_FILE $IPSET_DATA && chmod 600 $IPSET_DATA || ret=1
a0fbec
	else
a0fbec
	    diff -q $TMP_FILE $IPSET_DATA >/dev/null
a0fbec
	
a0fbec
	    if [[ $? -ne 0 ]]; then
a0fbec
		if [[ -f $IPSET_DATA ]]; then
a0fbec
		    cp -f --preserve=timestamps $IPSET_DATA $IPSET_DATA.save \
a0fbec
			&& chmod 600 $IPSET_DATA.save \
a0fbec
			|| ret=1
a0fbec
		fi
a0fbec
		if [[ $ret -eq 0 ]]; then
a0fbec
		    cp -f --preserve=timestamps $TMP_FILE $IPSET_DATA \
a0fbec
			&& chmod 600 $IPSET_DATA \
a0fbec
			|| ret=1
a0fbec
		fi
a0fbec
	    fi
a0fbec
	fi
a0fbec
    fi
a0fbec
a0fbec
    rm -f $TMP_FILE
a0fbec
    return $ret
a0fbec
}
a0fbec
a0fbec
a0fbec
case "$1" in
a0fbec
    start)
a0fbec
	start
a0fbec
	RETVAL=$?
a0fbec
	;;
a0fbec
    stop)
a0fbec
	check_can_unload || exit 1
a0fbec
	[[ $IPSET_SAVE_ON_STOP = yes ]] && save
a0fbec
	stop
a0fbec
	RETVAL=$?
a0fbec
	[[ $RETVAL -eq 6 ]] && echo "${IPSET}: not running" && exit 0
a0fbec
	;;
a0fbec
    reload)
a0fbec
	[[ $IPSET_SAVE_ON_RESTART = yes ]] && save
a0fbec
	stop
a0fbec
	RETVAL=$?
a0fbec
	[[ $RETVAL -eq 6 ]] && echo "${IPSET}: not running" && exit 0
a0fbec
	start
a0fbec
	RETVAL=$?
a0fbec
	;;
a0fbec
    *)
a0fbec
	echo "Usage: $IPSET {start|stop|reload}" >&2
a0fbec
	exit 1
a0fbec
esac
a0fbec
a0fbec
exit $RETVAL