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