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