Blame SOURCES/ipset.start-stop

7b50d1
#!/bin/sh
7b50d1
#
7b50d1
# ipset      Start and stop ipset firewall sets
7b50d1
#
7b50d1
# config: /etc/sysconfig/ipset-config
7b50d1
7b50d1
IPSET_BIN=/usr/sbin/ipset
7b50d1
IPSET_CONFIG=/etc/sysconfig/ipset-config
7b50d1
IPSET_DATA_COMPAT=/etc/sysconfig/ipset
7b50d1
IPSET_DATA_COMPAT_BACKUP=${IPSET_DATA_COMPAT}.save
7b50d1
IPSET_DATA_DIR=/etc/sysconfig/ipset.d
7b50d1
IPSET_DATA_DIR_BACKUP=${IPSET_DATA_DIR}.save
7b50d1
IPSET_DATA_SAVED_FLAG=${IPSET_DATA_DIR}/.saved
7b50d1
IPSET_LOCK=/run/ipset.lock
7b50d1
IPSET_RUN=/run/ipset.run
7b50d1
CLEAN_FILES=""
7b50d1
7b50d1
trap "rm -rf \${CLEAN_FILES}" EXIT
7b50d1
7b50d1
[ -x ${IPSET_BIN} ] || { echo "ipset: Cannot execute ${IPSET_BIN}" >&2; exit 5; }
7b50d1
7b50d1
# Source ipset configuration
7b50d1
[ -f ${IPSET_CONFIG} ] && . ${IPSET_CONFIG}
7b50d1
7b50d1
set -f
7b50d1
7b50d1
lock() {
7b50d1
	CLEAN_FILES="${CLEAN_FILES} ${IPSET_LOCK}"
7b50d1
	until mkdir ${IPSET_LOCK} 2>/dev/null; do :; done
7b50d1
}
7b50d1
7b50d1
save() {
7b50d1
	fail=0
7b50d1
7b50d1
	# Make backups of existing configuration first, if any
7b50d1
	[ -d ${IPSET_DATA_DIR} ] && mv -Tf ${IPSET_DATA_DIR} ${IPSET_DATA_DIR_BACKUP}
7b50d1
	[ -f ${IPSET_DATA_COMPAT} ] && mv -Tf ${IPSET_DATA_COMPAT} ${IPSET_DATA_COMPAT_BACKUP}
7b50d1
7b50d1
	rm -f ${IPSET_DATA_SAVED_FLAG}
7b50d1
7b50d1
	# Save each set in a separate file
7b50d1
	mkdir -pm 700 ${IPSET_DATA_DIR}
7b50d1
	IFS="
7b50d1
"
7b50d1
	for set in $(${IPSET_BIN} list -n -t); do
7b50d1
		# Empty name allowed, use ".set" as suffix. 'ipset save' doesn't
7b50d1
		# quote set names with spaces: if we have a space in the name,
7b50d1
		# work around this by quoting it ourselves in the output.
7b50d1
		if expr index "${set}" " " >/dev/null; then
7b50d1
			:> "${IPSET_DATA_DIR}/${set}.set"
7b50d1
			for line in $(${IPSET_BIN} save "${set}"); do
7b50d1
				create=0
7b50d1
				echo "${line}" | grep -q "^create " && create=1
7b50d1
				if [ $create -eq 1 ]; then
7b50d1
					line=${line#create *}
7b50d1
				else
7b50d1
					line=${line#add *}
7b50d1
				fi
7b50d1
				line=${line#${set} *}
7b50d1
				set="$(echo ${set} | sed 's/"/\\"/'g)"
7b50d1
				if [ $create -eq 1 ]; then
7b50d1
					echo "create \"${set}\" ${line}" >> "${IPSET_DATA_DIR}/${set}.set"
7b50d1
				else
7b50d1
					echo "add \"${set}\" ${line}" >> "${IPSET_DATA_DIR}/${set}.set"
7b50d1
				fi
7b50d1
			done
7b50d1
		else
7b50d1
			${IPSET_BIN} save "${set}" > "${IPSET_DATA_DIR}/${set}.set" || fail=1
7b50d1
		fi
7b50d1
		[ -f "${IPSET_DATA_DIR}/${set}.set" ] && chmod 600 "${IPSET_DATA_DIR}/${set}.set"
7b50d1
		[ $fail -eq 1 ] && echo "ipset: Cannot save set ${set}" >&2 && unset IFS && return 1
7b50d1
	done
7b50d1
	touch ${IPSET_DATA_SAVED_FLAG} || { unset IFS; return 1; }
7b50d1
	unset IFS
7b50d1
7b50d1
	# Done: remove backups
7b50d1
	rm -rf ${IPSET_DATA_DIR_BACKUP}
7b50d1
	rm -rf ${IPSET_DATA_COMPAT_BACKUP}
7b50d1
7b50d1
	return 0
7b50d1
}
7b50d1
7b50d1
load() {
7b50d1
	if [ -f ${IPSET_DATA_SAVED_FLAG} ]; then
7b50d1
		# If we have a cleanly saved directory with all sets, we can
7b50d1
		# delete any left-overs and use it
7b50d1
		rm -rf ${IPSET_DATA_DIR_BACKUP}
7b50d1
		rm -f ${IPSET_DATA_COMPAT_BACKUP}
7b50d1
	else
7b50d1
		# If sets weren't cleanly saved, restore from backups
7b50d1
		[ -d ${IPSET_DATA_DIR_BACKUP} ] && rm -rf ${IPSET_DATA_DIR} && mv -Tf ${IPSET_DATA_DIR_BACKUP} ${IPSET_DATA_DIR}
7b50d1
		[ -f ${IPSET_DATA_COMPAT_BACKUP} ] && rm -f ${IPSET_DATA_COMPAT} && mv -Tf ${IPSET_DATA_COMPAT_BACKUP} ${IPSET_DATA_COMPAT}
7b50d1
	fi
7b50d1
7b50d1
	if [ ! -d ${IPSET_DATA_DIR} -a ! -f ${IPSET_DATA_COMPAT} ]; then
7b50d1
		echo "ipset: No existing configuration available, none loaded"
7b50d1
		touch ${IPSET_RUN}
7b50d1
		return
7b50d1
	fi
7b50d1
7b50d1
	# Merge all sets into temporary file
7b50d1
	merged="$(mktemp -q /tmp/ipset.XXXXXX)"
7b50d1
	CLEAN_FILES="${CLEAN_FILES} ${merged}"
7b50d1
	chmod 600 "${merged}"
7b50d1
	set +f
7b50d1
	if [ -d ${IPSET_DATA_DIR} ]; then
7b50d1
		# Copy the first lines of each saved set first, as they create
7b50d1
		# the sets, then the rest: list:set entries depend on other
7b50d1
		# sets, so make sure they all get created first
7b50d1
		for f in ${IPSET_DATA_DIR}/*; do
7b50d1
			[ "${f}" = "${IPSET_DATA_DIR}/*" ] && break
7b50d1
			head -n1 "${f}" >> ${merged}
7b50d1
		done
7b50d1
		for f in ${IPSET_DATA_DIR}/*; do
7b50d1
			[ "${f}" = "${IPSET_DATA_DIR}/*" ] && break
7b50d1
			tail -n +2 "${f}" >> ${merged}
7b50d1
		done
7b50d1
	fi
7b50d1
	set -f
7b50d1
	[ -f ${IPSET_DATA_COMPAT} ] && cat ${IPSET_DATA_COMPAT} >> ${merged}
7b50d1
7b50d1
	# Drop sets that aren't in saved data, mark conflicts with existing sets
7b50d1
	conflicts=""
7b50d1
	IFS="
7b50d1
"
7b50d1
	for set in $(${IPSET_BIN} list -n -t); do
7b50d1
		grep -q "^create ${set} " ${merged} && conflicts="${conflicts}|${set}" && continue
7b50d1
		${IPSET_BIN} destroy "${set}" 2>/dev/null
7b50d1
		# We can't destroy the set if it's in use, flush it instead
7b50d1
		[ $? -ne 0 ] && ${IPSET_BIN} flush "${set}"
7b50d1
	done
7b50d1
	unset IFS
7b50d1
	conflicts="${conflicts#|*}"
7b50d1
7b50d1
	# Common case: if we have no conflicts, just restore in one shot
7b50d1
	if [ -z "${conflicts}" ]; then
7b50d1
		${IPSET_BIN} restore -! < ${merged}
7b50d1
		[ $? -ne 0 ] && echo "ipset: Failed to restore configured sets" >&2
7b50d1
		rm ${merged}
7b50d1
		CLEAN_FILES="${CLEAN_FILES%* ${merged}}"
7b50d1
		touch ${IPSET_RUN}
7b50d1
		return
7b50d1
	fi
7b50d1
7b50d1
	# Find a salt for md5sum that makes names of saved sets unique
7b50d1
	salt=0
7b50d1
	while true; do
7b50d1
		unique=1
7b50d1
		IFS="
7b50d1
"
7b50d1
		for set in $(${IPSET_BIN} list -n -t); do
7b50d1
			grep -q "^create $(echo ${salt}${set} | md5sum | head -c31) " ${merged}
7b50d1
			[ $? -eq 0 ] && unique=0 && break
7b50d1
		done
7b50d1
		unset IFS
7b50d1
		[ ${unique} -eq 1 ] && break
7b50d1
		salt=$((salt + 1))
7b50d1
	done
7b50d1
7b50d1
	# Add sets, mangling names for conflicting sets
7b50d1
	awk '/^(add|create) ('"${conflicts}"')/ { printf "%s ",$1; system("echo '${salt}'" $2 " | md5sum | head -c31"); $1=""; $2=""; print; next} {print}' ${merged} | ${IPSET_BIN} restore -!
7b50d1
7b50d1
	[ $? -ne 0 ] && echo "ipset: Failed to restore configured sets" >&2
7b50d1
7b50d1
	# Swap and delete old sets
7b50d1
	IFS='|'
7b50d1
	for set in ${conflicts}; do
7b50d1
		mangled="$(echo ${salt}${set} | md5sum | head -c31)"
7b50d1
		${IPSET_BIN} swap "${set}" "${mangled}" 2>/dev/null
7b50d1
		if [ $? -ne 0 ]; then
7b50d1
			# This fails if set types are different: try to destroy
7b50d1
			# existing set
7b50d1
			${IPSET_BIN} destroy "${set}" 2>/dev/null
7b50d1
			if [ $? -ne 0 ]; then
7b50d1
				# Conflicting set is in use, we can only warn
7b50d1
				# and flush the existing set
7b50d1
				echo "ipset: Cannot load set \"${set}\", set with same name and conflicting type in use" >&2
7b50d1
				${IPSET_BIN} flush "${set}"
7b50d1
				${IPSET_BIN} destroy "${mangled}"
7b50d1
			else
7b50d1
				${IPSET_BIN} rename "${mangled}" "${set}"
7b50d1
			fi
7b50d1
		else
7b50d1
			${IPSET_BIN} destroy "${mangled}"
7b50d1
		fi
7b50d1
	done
7b50d1
	unset IFS
7b50d1
7b50d1
	rm ${merged}
7b50d1
	CLEAN_FILES="${CLEAN_FILES%* ${merged}}"
7b50d1
	touch ${IPSET_RUN}
7b50d1
}
7b50d1
7b50d1
cleanup() {
7b50d1
	${IPSET_BIN} flush || echo "ipset: Failed to flush sets" >&2
7b50d1
7b50d1
	# Try to destroy all sets at once. This will fail if some are in use,
7b50d1
	# destroy all the other ones in that case
7b50d1
	${IPSET_BIN} destroy 2>/dev/null && return
7b50d1
	IFS="
7b50d1
"
7b50d1
	for set in $(${IPSET_BIN} list -n -t); do
7b50d1
		${IPSET_BIN} destroy "${set}" 2>/dev/null
7b50d1
	done
7b50d1
	unset IFS
7b50d1
}
7b50d1
7b50d1
stop() {
7b50d1
	[ -f ${IPSET_RUN} ] || { echo "ipset: not running"; return 0; }
7b50d1
	[ "${IPSET_SAVE_ON_STOP}" = "yes" ] && { save || echo "ipset: Failed to save sets"; }
7b50d1
7b50d1
	# Nothing to stop if the ip_set module is not loaded
7b50d1
	lsmod | grep -q "^ip_set " || { echo "ipset: not running"; rm ${IPSET_RUN}; return 0; }
7b50d1
7b50d1
	# If the xt_set module is in use, then iptables is using ipset, so
7b50d1
	# refuse to stop the service
7b50d1
	mod="$(lsmod | grep ^xt_set)"
7b50d1
	if [ $? -eq 0 ]; then
7b50d1
		if [ "$(echo ${mod} | tr -s ' ' | cut -d' ' -f3)" != "0" ]; then
7b50d1
			echo "ipset: Current iptables configuration requires ipset" >&2 && return 1
7b50d1
		fi
7b50d1
	fi
7b50d1
7b50d1
	cleanup
7b50d1
7b50d1
	rm ${IPSET_RUN}
7b50d1
	return 0
7b50d1
}
7b50d1
7b50d1
lock
7b50d1
case "$1" in
7b50d1
start)
7b50d1
	load
7b50d1
	;;
7b50d1
stop)
7b50d1
	stop
7b50d1
	;;
7b50d1
reload)
7b50d1
	cleanup
7b50d1
	load
7b50d1
	;;
7b50d1
save)
7b50d1
	save
7b50d1
	;;
7b50d1
*)
7b50d1
	echo "Usage: $0 {start|stop|reload|save}" >&2
7b50d1
	exit 1
7b50d1
esac
7b50d1
7b50d1
exit $?