diff --git a/SOURCES/ipset-config b/SOURCES/ipset-config new file mode 100644 index 00000000..decb760c --- /dev/null +++ b/SOURCES/ipset-config @@ -0,0 +1,5 @@ +# Save current ipsets on stop. +# Value: yes|no, default: no +# Saves all ipsets to /etc/ipset/ipset if service gets stopped +# (e.g. on system shutdown). +IPSET_SAVE_ON_STOP="no" diff --git a/SOURCES/ipset.save-legacy b/SOURCES/ipset.save-legacy new file mode 100644 index 00000000..39db1cf9 --- /dev/null +++ b/SOURCES/ipset.save-legacy @@ -0,0 +1,2 @@ +#!/bin/bash +exec /usr/libexec/ipset/ipset.start-stop save diff --git a/SOURCES/ipset.service b/SOURCES/ipset.service new file mode 100644 index 00000000..65c31210 --- /dev/null +++ b/SOURCES/ipset.service @@ -0,0 +1,19 @@ +[Unit] +Description=IP sets for iptables +Before=iptables.service +Before=ip6tables.service + +[Service] +Type=oneshot +RemainAfterExit=yes +ExecStart=/usr/libexec/ipset/ipset.start-stop start +ExecStop=/usr/libexec/ipset/ipset.start-stop stop +ExecReload=/usr/libexec/ipset/ipset.start-stop reload +# Save current ipset entries on stop. +# Value: yes|no, default: no +# Saves all ipsets to /etc/sysconfig/ipset if ipset gets stopped +Environment=IPSET_SAVE_ON_STOP=no +EnvironmentFile=-/etc/sysconfig/ipset-config + +[Install] +WantedBy=basic.target diff --git a/SOURCES/ipset.start-stop b/SOURCES/ipset.start-stop new file mode 100644 index 00000000..6c84bd3e --- /dev/null +++ b/SOURCES/ipset.start-stop @@ -0,0 +1,243 @@ +#!/bin/sh +# +# ipset Start and stop ipset firewall sets +# +# config: /etc/sysconfig/ipset-config + +IPSET_BIN=/usr/sbin/ipset +IPSET_CONFIG=/etc/sysconfig/ipset-config +IPSET_DATA_COMPAT=/etc/sysconfig/ipset +IPSET_DATA_COMPAT_BACKUP=${IPSET_DATA_COMPAT}.save +IPSET_DATA_DIR=/etc/sysconfig/ipset.d +IPSET_DATA_DIR_BACKUP=${IPSET_DATA_DIR}.save +IPSET_DATA_SAVED_FLAG=${IPSET_DATA_DIR}/.saved +IPSET_LOCK=/run/ipset.lock +IPSET_RUN=/run/ipset.run +CLEAN_FILES="" + +trap "rm -rf \${CLEAN_FILES}" EXIT + +[ -x ${IPSET_BIN} ] || { echo "ipset: Cannot execute ${IPSET_BIN}" >&2; exit 5; } + +# Source ipset configuration +[ -f ${IPSET_CONFIG} ] && . ${IPSET_CONFIG} + +set -f + +lock() { + CLEAN_FILES="${CLEAN_FILES} ${IPSET_LOCK}" + until mkdir ${IPSET_LOCK} 2>/dev/null; do :; done +} + +save() { + fail=0 + + # Make backups of existing configuration first, if any + [ -d ${IPSET_DATA_DIR} ] && mv -Tf ${IPSET_DATA_DIR} ${IPSET_DATA_DIR_BACKUP} + [ -f ${IPSET_DATA_COMPAT} ] && mv -Tf ${IPSET_DATA_COMPAT} ${IPSET_DATA_COMPAT_BACKUP} + + rm -f ${IPSET_DATA_SAVED_FLAG} + + # Save each set in a separate file + mkdir -pm 700 ${IPSET_DATA_DIR} + IFS=" +" + for set in $(${IPSET_BIN} list -n -t); do + # Empty name allowed, use ".set" as suffix. 'ipset save' doesn't + # quote set names with spaces: if we have a space in the name, + # work around this by quoting it ourselves in the output. + if expr index "${set}" " " >/dev/null; then + :> "${IPSET_DATA_DIR}/${set}.set" + for line in $(${IPSET_BIN} save "${set}"); do + create=0 + echo "${line}" | grep -q "^create " && create=1 + if [ $create -eq 1 ]; then + line=${line#create *} + else + line=${line#add *} + fi + line=${line#${set} *} + set="$(echo ${set} | sed 's/"/\\"/'g)" + if [ $create -eq 1 ]; then + echo "create \"${set}\" ${line}" >> "${IPSET_DATA_DIR}/${set}.set" + else + echo "add \"${set}\" ${line}" >> "${IPSET_DATA_DIR}/${set}.set" + fi + done + else + ${IPSET_BIN} save "${set}" > "${IPSET_DATA_DIR}/${set}.set" || fail=1 + fi + [ -f "${IPSET_DATA_DIR}/${set}.set" ] && chmod 600 "${IPSET_DATA_DIR}/${set}.set" + [ $fail -eq 1 ] && echo "ipset: Cannot save set ${set}" >&2 && unset IFS && return 1 + done + touch ${IPSET_DATA_SAVED_FLAG} || { unset IFS; return 1; } + unset IFS + + # Done: remove backups + rm -rf ${IPSET_DATA_DIR_BACKUP} + rm -rf ${IPSET_DATA_COMPAT_BACKUP} + + return 0 +} + +load() { + if [ -f ${IPSET_DATA_SAVED_FLAG} ]; then + # If we have a cleanly saved directory with all sets, we can + # delete any left-overs and use it + rm -rf ${IPSET_DATA_DIR_BACKUP} + rm -f ${IPSET_DATA_COMPAT_BACKUP} + else + # If sets weren't cleanly saved, restore from backups + [ -d ${IPSET_DATA_DIR_BACKUP} ] && rm -rf ${IPSET_DATA_DIR} && mv -Tf ${IPSET_DATA_DIR_BACKUP} ${IPSET_DATA_DIR} + [ -f ${IPSET_DATA_COMPAT_BACKUP} ] && rm -f ${IPSET_DATA_COMPAT} && mv -Tf ${IPSET_DATA_COMPAT_BACKUP} ${IPSET_DATA_COMPAT} + fi + + if [ ! -d ${IPSET_DATA_DIR} -a ! -f ${IPSET_DATA_COMPAT} ]; then + echo "ipset: No existing configuration available, none loaded" + touch ${IPSET_RUN} + return + fi + + # Merge all sets into temporary file + merged="$(mktemp -q /tmp/ipset.XXXXXX)" + CLEAN_FILES="${CLEAN_FILES} ${merged}" + chmod 600 "${merged}" + set +f + if [ -d ${IPSET_DATA_DIR} ]; then + for f in ${IPSET_DATA_DIR}/*; do + [ "${f}" = "${IPSET_DATA_DIR}/*" ] && break + cat "${f}" >> ${merged} + done + fi + set -f + [ -f ${IPSET_DATA_COMPAT} ] && cat ${IPSET_DATA_COMPAT} >> ${merged} + + # Drop sets that aren't in saved data, mark conflicts with existing sets + conflicts="" + IFS=" +" + for set in $(${IPSET_BIN} list -n -t); do + grep -q "^create ${set} " ${merged} && conflicts="${conflicts}|${set}" && continue + ${IPSET_BIN} destroy "${set}" 2>/dev/null + # We can't destroy the set if it's in use, flush it instead + [ $? -ne 0 ] && ${IPSET_BIN} flush "${set}" + done + unset IFS + conflicts="${conflicts#|*}" + + # Common case: if we have no conflicts, just restore in one shot + if [ -z "${conflicts}" ]; then + ${IPSET_BIN} restore -! < ${merged} + [ $? -ne 0 ] && echo "ipset: Failed to restore configured sets" >&2 + rm ${merged} + CLEAN_FILES="${CLEAN_FILES%* ${merged}}" + touch ${IPSET_RUN} + return + fi + + # Find a salt for md5sum that makes names of saved sets unique + salt=0 + while true; do + unique=1 + IFS=" +" + for set in $(${IPSET_BIN} list -n -t); do + grep -q "^create $(echo ${salt}${set} | md5sum | head -c31) " ${merged} + [ $? -eq 0 ] && unique=0 && break + done + unset IFS + [ ${unique} -eq 1 ] && break + salt=$((salt + 1)) + done + + # Add sets, mangling names for conflicting sets + awk '/^(add|create) ('"${conflicts}"')/ { printf "%s ",$1; system("echo '${salt}'" $2 " | md5sum | head -c31"); $1=""; $2=""; print; next} {print}' ${merged} | ${IPSET_BIN} restore -! + + [ $? -ne 0 ] && echo "ipset: Failed to restore configured sets" >&2 + + # Swap and delete old sets + IFS='|' + for set in ${conflicts}; do + mangled="$(echo ${salt}${set} | md5sum | head -c31)" + ${IPSET_BIN} swap "${set}" "${mangled}" 2>/dev/null + if [ $? -ne 0 ]; then + # This fails if set types are different: try to destroy + # existing set + ${IPSET_BIN} destroy "${set}" 2>/dev/null + if [ $? -ne 0 ]; then + # Conflicting set is in use, we can only warn + # and flush the existing set + echo "ipset: Cannot load set \"${set}\", set with same name and conflicting type in use" >&2 + ${IPSET_BIN} flush "${set}" + ${IPSET_BIN} destroy "${mangled}" + else + ${IPSET_BIN} rename "${mangled}" "${set}" + fi + else + ${IPSET_BIN} destroy "${mangled}" + fi + done + unset IFS + + rm ${merged} + CLEAN_FILES="${CLEAN_FILES%* ${merged}}" + touch ${IPSET_RUN} +} + +cleanup() { + ${IPSET_BIN} flush || echo "ipset: Failed to flush sets" >&2 + + # Try to destroy all sets at once. This will fail if some are in use, + # destroy all the other ones in that case + ${IPSET_BIN} destroy 2>/dev/null && return + IFS=" +" + for set in $(${IPSET_BIN} list -n -t); do + ${IPSET_BIN} destroy "${set}" 2>/dev/null + done + unset IFS +} + +stop() { + [ -f ${IPSET_RUN} ] || { echo "ipset: not running"; return 0; } + [ "${IPSET_SAVE_ON_STOP}" = "yes" ] && { save || echo "ipset: Failed to save sets"; } + + # Nothing to stop if the ip_set module is not loaded + lsmod | grep -q "^ip_set " || { echo "ipset: not running"; rm ${IPSET_RUN}; return 0; } + + # If the xt_set module is in use, then iptables is using ipset, so + # refuse to stop the service + mod="$(lsmod | grep ^xt_set)" + if [ $? -eq 0 ]; then + if [ "$(echo ${mod} | tr -s ' ' | cut -d' ' -f3)" != "0" ]; then + echo "ipset: Current iptables configuration requires ipset" >&2 && return 1 + fi + fi + + cleanup + + rm ${IPSET_RUN} + return 0 +} + +lock +case "$1" in +start) + load + ;; +stop) + stop + ;; +reload) + cleanup + load + ;; +save) + save + ;; +*) + echo "Usage: $0 {start|stop|reload|save}" >&2 + exit 1 +esac + +exit $? diff --git a/SPECS/ipset.spec b/SPECS/ipset.spec new file mode 100644 index 00000000..bbcf667f --- /dev/null +++ b/SPECS/ipset.spec @@ -0,0 +1,255 @@ +# service legacy actions +%define legacy_actions %{_libexecdir}/initscripts/legacy-actions + +Name: ipset +Version: 6.38 +Release: 2%{?dist} +Summary: Manage Linux IP sets + +License: GPLv2 +URL: http://ipset.netfilter.org/ +Source0: http://ipset.netfilter.org/%{name}-%{version}.tar.bz2 +Source1: %{name}.service +Source2: %{name}.start-stop +Source3: %{name}-config +Source4: %{name}.save-legacy + +BuildRequires: libmnl-devel + +# An explicit requirement is needed here, to avoid cases where a user would +# explicitly update only one of the two (e.g 'yum update ipset') +Requires: %{name}-libs%{?_isa} = %{version}-%{release} + +%description +IP sets are a framework inside the Linux kernel since version 2.4.x, which can +be administered by the ipset utility. Depending on the type, currently an IP +set may store IP addresses, (TCP/UDP) port numbers or IP addresses with MAC +addresses in a way, which ensures lightning speed when matching an entry +against a set. + +If you want to: + - store multiple IP addresses or port numbers and match against the collection + by iptables at one swoop; + - dynamically update iptables rules against IP addresses or ports without + performance penalty; + - express complex IP address and ports based rulesets with one single iptables + rule and benefit from the speed of IP sets +then ipset may be the proper tool for you. + + +%package libs +Summary: Shared library providing the IP sets functionality + +%description libs +This package contains the libraries which provide the IP sets funcionality. + + +%package devel +Summary: Development files for %{name} +Requires: %{name}-libs%{?_isa} == %{version}-%{release} +Requires: kernel-devel + +%description devel +This package contains the files required to develop software using the %{name} +libraries. + + +%package service +Summary: %{name} service for %{name}s +Requires: %{name} = %{version}-%{release} +BuildRequires: systemd +Requires: iptables-services +Requires(post): systemd +Requires(preun): systemd +Requires(postun): systemd +BuildArch: noarch + +%description service +This package provides the service %{name} that is split +out of the base package since it is not active by default. + + +%prep +%setup -q + + +%build +%configure --enable-static=no --with-kmod=no + +# Just to make absolutely sure we are not building the bundled kernel module +# I have to do it after the configure run unfortunately +rm -fr kernel + +# Prevent libtool from defining rpath +sed -i 's|^hardcode_libdir_flag_spec=.*|hardcode_libdir_flag_spec=""|g' libtool +sed -i 's|^runpath_var=LD_RUN_PATH|runpath_var=DIE_RPATH_DIE|g' libtool + +make %{?_smp_mflags} + + +%install +make install DESTDIR=%{buildroot} +find %{buildroot} -name '*.la' -exec rm -f '{}' \; + +# install systemd unit file +install -d -m 755 %{buildroot}/%{_unitdir} +install -c -m 644 %{SOURCE1} %{buildroot}/%{_unitdir} + +# install supporting script +install -d -m 755 %{buildroot}%{_libexecdir}/%{name} +install -c -m 755 %{SOURCE2} %{buildroot}%{_libexecdir}/%{name} + +# install ipset-config +install -d -m 755 %{buildroot}%{_sysconfdir}/sysconfig +install -c -m 600 %{SOURCE3} %{buildroot}%{_sysconfdir}/sysconfig/%{name}-config + +# install legacy actions for service command +install -d %{buildroot}/%{legacy_actions}/ipset +install -c -m 755 %{SOURCE4} %{buildroot}/%{legacy_actions}/ipset/save + +# Create directory for configuration +mkdir -p %{buildroot}%{_sysconfdir}/%{name} + + +%preun +if [[ $1 -eq 0 && -n $(lsmod | grep "^xt_set ") ]]; then + rmmod xt_set 2>/dev/null + [[ $? -ne 0 ]] && echo Current iptables configuration requires ipsets && exit 1 +fi + + +%post libs -p /sbin/ldconfig + +%postun libs -p /sbin/ldconfig + + +%post service +%systemd_post %{name}.service + +%preun service +if [[ $1 -eq 0 && -n $(lsmod | grep "^xt_set ") ]]; then + rmmod xt_set 2>/dev/null + [[ $? -ne 0 ]] && echo Current iptables configuration requires ipsets && exit 1 +fi +%systemd_preun %{name}.service + +%postun service +%systemd_postun_with_restart %{name}.service + +%triggerin service -- ipset-service < 6.38-1%{?dist} +# Before 6.38-1, ipset.start-stop keeps a backup of previously saved sets, but +# doesn't touch the /etc/sysconfig/ipset.d/.saved flag. Remove the backup on +# upgrade, so that we use the current version of saved sets +rm -f /etc/sysconfig/ipset.save || : +exit 0 + +%triggerun service -- ipset-service < 6.38-1%{?dist} +# Up to 6.29-1, ipset.start-stop uses a single data file +for f in /etc/sysconfig/ipset.d/*; do + [ "${f}" = "/etc/sysconfig/ipset.d/*" ] && break + cat ${f} >> /etc/sysconfig/ipset || : +done +exit 0 + +%files +%doc COPYING ChangeLog +%doc %{_mandir}/man8/%{name}.8.gz +%{_sbindir}/%{name} + +%files libs +%doc COPYING +%{_libdir}/lib%{name}.so.11* + +%files devel +%{_includedir}/lib%{name} +%{_libdir}/lib%{name}.so +%{_libdir}/pkgconfig/lib%{name}.pc + +%files service +%{_unitdir}/%{name}.service +%dir %{_libexecdir}/%{name} +%config(noreplace) %attr(0600,root,root) %{_sysconfdir}/sysconfig/ipset-config +%ghost %config(noreplace) %attr(0600,root,root) %{_sysconfdir}/sysconfig/ipset +%attr(0755,root,root) %{_libexecdir}/%{name}/%{name}.start-stop +%dir %{legacy_actions}/ipset +%{legacy_actions}/ipset/save + + +%changelog +* Wed Jun 27 2018 Stefano Brivio - 6.38-2 +- Fix upgrade and downgrade triggers in specfile (RHBZ#1594722) + +* Mon Apr 16 2018 Stefano Brivio - 6.38-1 +- Rebase to 6.38 (RHBZ#1557600): + - hash:ipmac type support added to ipset, userspace part (Tomasz Chilinski) +- Refactor /etc/sysconfig/ipset.start-stop +- Fixes: + - IPSet Service Monolithic Operation (RHBZ#1440741) + - "systemctl start ipset" doesn't handle existing ipset's having counters + (RHBZ#1502212) + +* Wed Feb 1 2017 Thomas Woerner - 6.29-1 +- Rebase to 6.29 (RHBZ#1351299) +- Fixes: + - Backport ipset capability to run in namespaces (RHBZ#1226051) + - Fix service save with empty ipset list and existing ipset save file + (RHBZ#1377621) + - Fix internal error at printing to output buffer (RHBZ#1395865) + +* Wed Aug 17 2016 Thomas Woerner - 6.19-6 +- Use /etc/sysconfig/ipset-config in service as EnvironmentFile (RHBZ#1136257) +- Use /etc/sysconfig/ipset for data as in RHEL-6 (RHBZ#1136257) +- No save on reload, but legacy save action (RHBZ#1136257) + +* Wed Jun 29 2016 Thomas Woerner - 6.19-5 +- New service sub package to provide the ipset service (RHBZ#1136257) + Service and start-stop script from F-24 +- Fixed ipset package summary (RHBZ#1195171) + Spec file derived from F-24 + +* Fri Jan 24 2014 Daniel Mach - 6.19-4 +- Mass rebuild 2014-01-24 + +* Tue Jan 14 2014 Thomas Woerner - 6.19-3 +- fixed failed rmdiff testing (RHBZ#884500) + +* Fri Dec 27 2013 Daniel Mach - 6.19-2 +- Mass rebuild 2013-12-27 + +* Thu Aug 15 2013 Mathieu Bridon - 6.19 +- New upstream release. + +* Sat Aug 03 2013 Fedora Release Engineering - 6.16.1-3 +- Rebuilt for https://fedoraproject.org/wiki/Fedora_20_Mass_Rebuild + +* Thu Feb 14 2013 Fedora Release Engineering - 6.16.1-2 +- Rebuilt for https://fedoraproject.org/wiki/Fedora_19_Mass_Rebuild + +* Wed Sep 26 2012 Mathieu Bridon - 6.16.1-1 +- New upstream release. +- Fix a requirement. + +* Wed Sep 26 2012 Mathieu Bridon - 6.14-1 +- New upstream release. +- Fix scriptlets, ldconfig is needed for the libs subpackage, not the main one. + +* Mon Jul 30 2012 Mathieu Bridon - 6.13-1 +- New upstream release. +- Split out the library in its own subpackage. + +* Thu Jul 19 2012 Fedora Release Engineering - 6.11-2 +- Rebuilt for https://fedoraproject.org/wiki/Fedora_18_Mass_Rebuild + +* Mon Feb 06 2012 Mathieu Bridon - 6.11-1 +- New upstream release. +- Removed our patch, it has been integrated upstream. As such, we also don't + need to re-run autoreconf any more. + +* Fri Jan 13 2012 Fedora Release Engineering - 6.9.1-3 +- Rebuilt for https://fedoraproject.org/wiki/Fedora_17_Mass_Rebuild + +* Fri Sep 16 2011 Mathieu Bridon - 6.9.1-2 +- Some fixes based on Pierre-Yves' review feedback. + +* Wed Sep 14 2011 Mathieu Bridon - 6.9.1-1 +- Initial packaging.