%global __os_install_post %(echo '%{__os_install_post}' | sed -e 's!/usr/lib[^[:space:]]*/brp-python-bytecompile[[:space:]].*$!!g')
%global docdir %{?_pkgdocdir}%{!?_pkgdocdir:%{_docdir}/%{name}-%{version}}

Name:           mailman
Version:        2.1.29
Release:        7
Epoch:          3
Summary:        GNU Mailing List Manager
License:        GPLv2+
URL:            http://www.list.org/
Source0:        ftp://ftp.gnu.org/pub/gnu/mailman/mailman-%{version}.tgz
Source1:        mm_cfg.py
Source3:        httpd-mailman.conf
Source4:        mailman.logrotate
Source6:        mailman-crontab-edit
Source8:        mailman-update-cfg
Source9:        mailman-tmpfiles.conf
Source10:       mailman.service

Patch1:         mailman-2.1.12-multimail.patch
Patch2:         mailman-2.1-build.patch
Patch3:         mailman-2.1-mailmanctl-status.patch
Patch4:         mailman-2.1.11-cron.patch
Patch5:         mailman-2.1.13-FHS.patch
Patch6:         mailman-python-compile.patch
Patch7:         mailman-2.1.13-archive-reply.patch
Patch13:        mailman-2.1.9-unicode.patch
Patch21:        mailman-2.1.13-env-python.patch
Patch22:        mailman-2.1.15-check_perms.patch
Patch23:        CVE-2020-12108.patch
Patch24:        CVE-2020-12137.patch
Patch25:        CVE-2020-15011.patch

BuildRequires:  automake gcc python2-devel systemd python2-dns
Requires(pre):  shadow-utils
Requires:       cronie httpd python2 coreutils python2-dns
%{?systemd_requires}

%description
Mailman is free software for managing electronic mail discussion and
e-newsletter lists. Mailman is integrated with the web, making it easy
for users to manage their accounts and for list owners to administer
their lists. Mailman supports built-in archiving, automatic bounce
processing, content filtering, digest delivery, spam filters, and more.

%prep
%autosetup -n %{name}-%{version} -p1

rm -f contrib/redhat_fhs.patch
mv contrib/sitemapgen contrib/sitemapgen.in

%build
CFLAGS="$RPM_OPT_FLAGS -fPIE -DPIE"; export CFLAGS
export LDFLAGS="$LDFLAGS -pie -Wl,-z,relro -Wl,-z,now"

rm -f configure
aclocal
autoconf

./configure \
        --libdir=/usr/lib \
        --prefix=/usr/lib/%{name} \
        --with-var-prefix=/var/lib/%{name} \
        --with-config-dir=/etc/%{name} \
        --with-lock-dir=/var/lock/%{name} \
        --with-log-dir=/var/log/%{name} \
        --with-pid-dir=/var/run/%{name} \
        --with-queue-dir=/var/spool/%{name} \
        --with-template-dir=/etc/%{name}/templates \
        --with-python=%{__python2} \
        --with-mail-gid="mail postfix mailman nobody daemon" \
        --with-cgi-id=apache \
        --with-cgi-gid=apache \
        --with-mailhost=localhost.localdomain \
        --with-urlhost=localhost.localdomain \
        --without-permcheck

function SubstituteParameters()
{
sed -e 's|@VAR_PREFIX@|/var/lib/%{name}|g' \
    -e 's|@VARMMDIR@|/var/lib/%{name}|g' \
    -e 's|@prefix@|/usr/lib/%{name}|g' \
    -e 's|@MMDIR@|/usr/lib/%{name}|g' \
    -e 's|@CONFIG_DIR@|/etc/%{name}|g' \
    -e 's|@DATA_DIR@|/var/lib/%{name}/data|g' \
    -e 's|@LOCK_DIR@|/var/lock/%{name}|g' \
    -e 's|@LOG_DIR@|/var/log/%{name}|g' \
    -e 's|@PID_DIR@|/var/run/%{name}|g' \
    -e 's|@QUEUE_DIR@|/var/spool/%{name}|g' \
    -e 's|@DOC_DIR@|%{docdir}|g' \
    -e 's|@HTTPD_CONF_DIR@|/etc/httpd/conf.d|g' \
    -e 's|@HTTPD_CONF_FILE@|%{name}.conf|g' \
    $1 > $2
}

SubstituteParameters "%{SOURCE1}" "Mailman/mm_cfg.py.dist"
SubstituteParameters "%{SOURCE3}" "httpd-mailman.conf"
SubstituteParameters "%{SOURCE4}" "mailman.logrotate"
rm -f contrib/*.in

%make_build

%install
%make_install

mkdir -p %{buildroot}/etc/cron.d
cat > %{buildroot}/etc/cron.d/%{name} <<EOF
EOF

mkdir -p %{buildroot}/usr/share/httpd/icons
cp %{buildroot}//usr/lib/%{name}/icons/* %{buildroot}/usr/share/httpd/icons

mkdir -p %{buildroot}/etc/smrsh
ln -s ../../usr/lib/%{name}/mail/%{name} %{buildroot}/etc/smrsh

ln -s /etc/%{name}/sitelist.cfg %{buildroot}/var/lib/%{name}/data

mkdir -p %{buildroot}/etc/logrotate.d
install -m644 %{_builddir}/%{name}-%{version}/mailman.logrotate %{buildroot}/etc/logrotate.d/%{name}

install -m755 -d %{buildroot}/etc/httpd/conf.d
install -m644 %{_builddir}/%{name}-%{version}/httpd-mailman.conf %{buildroot}/etc/httpd/conf.d/%{name}.conf

install -m 755 -d %{buildroot}%{docdir}
install -m 644 %{_builddir}/%{name}-%{version}/ACKNOWLEDGMENTS  %{buildroot}%{docdir}
install -m 644 %{_builddir}/%{name}-%{version}/FAQ              %{buildroot}%{docdir}
install -m 644 %{_builddir}/%{name}-%{version}/NEWS             %{buildroot}%{docdir}
install -m 644 %{_builddir}/%{name}-%{version}/README           %{buildroot}%{docdir}
install -m 644 %{_builddir}/%{name}-%{version}/README.CONTRIB   %{buildroot}%{docdir}
install -m 644 %{_builddir}/%{name}-%{version}/README-I18N.en   %{buildroot}%{docdir}
install -m 644 %{_builddir}/%{name}-%{version}/README.NETSCAPE  %{buildroot}%{docdir}
install -m 644 %{_builddir}/%{name}-%{version}/README.USERAGENT %{buildroot}%{docdir}
install -m 644 %{_builddir}/%{name}-%{version}/STYLEGUIDE.txt   %{buildroot}%{docdir}
install -m 644 %{_builddir}/%{name}-%{version}/UPGRADING        %{buildroot}%{docdir}

cp -r %{_builddir}/%{name}-%{version}/contrib %{buildroot}%{docdir}
install -m 755 -d %{buildroot}%{docdir}/admin
cp -r %{_builddir}/%{name}-%{version}/doc %{buildroot}%{docdir}/admin

mkdir -p %{buildroot}/usr/lib/%{name}/bin/
install -m755 %{SOURCE8} %{buildroot}/usr/lib/%{name}/bin/

sed -i 's,@mmdir@,/usr/lib/%{name},g' %{buildroot}/usr/lib/%{name}/bin/mailman-update-cfg

rm -rf %{buildroot}/var/lib/%{name}/icons

chmod 0644 %{buildroot}//usr/lib/%{name}/tests/fblast.py

mkdir -p %{buildroot}//var/lock/%{name}
mkdir -p %{buildroot}//var/log/%{name}
mkdir -p %{buildroot}//var/run/%{name}
mkdir -p %{buildroot}//var/spool/%{name}
mkdir -p %{buildroot}/%{_prefix}/lib/tmpfiles.d

install -m 644 -p %{SOURCE9} %{buildroot}%{_prefix}/lib/tmpfiles.d/mailman.conf

mkdir -p %{buildroot}%{_unitdir}
install -m 644 %{SOURCE10} %{buildroot}%{_unitdir}

mkdir -p %{buildroot}/etc/%{name}
mv %{buildroot}/usr/lib/%{name}/Mailman/mm_cfg.py %{buildroot}/etc/%{name}
ln -s /etc/%{name}/mm_cfg.py %{buildroot}/usr/lib/%{name}/Mailman/

cat > %{buildroot}/usr/lib/%{name}/templates/README <<EOF
Templates have been moved to /etc/%{name}/templates.
EOF

# byte-compile /usr/lib/%{name}
#py_byte_compile %{__python2} %{buildroot}/usr/lib/%{name}
find %{buildroot}/usr/lib/%{name} -type f -a -name "*.py" -print0 | xargs -0 %{__python2} -c 'import py_compile, sys; [py_compile.compile(f, dfile=f.partition("$RPM_BUILD_ROOT")[2]) for f in sys.argv[1:]]' || :
find %{buildroot}/usr/lib/%{name} -type f -a -name "*.py" -print0 | xargs -0 %{__python2} -O -c 'import py_compile, sys; [py_compile.compile(f, dfile=f.partition("$RPM_BUILD_ROOT")[2]) for f in sys.argv[1:]]' || :


find %{buildroot}//usr/lib/%{name} -type d -exec chmod 755 {} \;
find %{buildroot}//etc/%{name}/templates -type d -exec chmod 755 {} \;

chmod %{buildroot}//usr/lib/%{name} -s -R

chmod g+s %{buildroot}//usr/lib/%{name}/cgi-bin/*
chmod g+s %{buildroot}//usr/lib/%{name}/mail/mailman

chmod %{buildroot}//etc/%{name} -s -R

%pre
if grep -q "^mailman:" /etc/group 2> /dev/null ; then
  /usr/sbin/groupmod -g 41 -n %{name} %{name} 2> /dev/null || :
else
  /usr/sbin/groupadd -g 41 %{name} 2> /dev/null || :
fi
if grep -q "^mailman:" /etc/passwd 2> /dev/null ; then
  /usr/sbin/usermod -s /sbin/nologin -c "GNU Mailing List Manager" -d /usr/lib/%{name} -u 41 -g 41       %{name} 2> /dev/null || :
else
  /usr/sbin/useradd -s /sbin/nologin -c "GNU Mailing List Manager" -d /usr/lib/%{name} -u 41 -g 41 -M -r %{name} 2> /dev/null || :
fi

if [ -d /var/lock/%{name} ]; then
  rm -f /var/run/%{name}-restart-after-rpm-install
  /sbin/systemctl status %{name}>/dev/null 2>&1
  if [ $? -eq 0 ]; then
      touch /var/run/%{name}-restart-after-rpm-install
      /sbin/systemctl stop %{name} >/dev/null 2>&1
  fi
fi

if [ -d %{_localstatedir}/lib/rpm-state/mailman ]; then
  rm -rf %{_localstatedir}/lib/rpm-state/mailman
fi

mkdir -p %{_localstatedir}/lib/rpm-state/mailman/
if [ -L /etc/%{name}/mm_cfg.py -a ! -L /usr/lib/%{name}/Mailman/mm_cfg.py -a -d /etc/%{name} ]; then
  mv /usr/lib/%{name}/Mailman/mm_cfg.py %{_localstatedir}/lib/rpm-state/mailman/mm_cfg.py
fi

if [ ! -d /etc/%{name}/templates -a -d /usr/lib/%{name}/templates ]; then
  mv /usr/lib/%{name}/templates %{_localstatedir}/lib/rpm-state/mailman/
fi

exit 0

%post
if [ -f /var/spool/cron/%{name} ]; then
  crontab -u %{name} -r
fi

if [ -e /var/run/%{name}-restart-after-rpm-install ]; then
  rm /var/run/%{name}-restart-after-rpm-install
  /sbin/systemctl start %{name} >/dev/null 2>&1
fi

if [ -f %{_localstatedir}/lib/rpm-state/mailman/mm_cfg.py ]; then
  cp -p %{_localstatedir}/lib/rpm-state/mailman/mm_cfg.py /etc/%{name}/mm_cfg.py
fi

if [ -d %{_localstatedir}/lib/rpm-state/mailman/templates ]; then
  cp -pr %{_localstatedir}/lib/rpm-state/mailman/templates/* /etc/%{name}/templates
  rm -rf %{_localstatedir}/lib/rpm-state/mailman/templates/
fi

if [ -d %{_localstatedir}/lib/rpm-state/mailman ]; then
  rm -rf %{_localstatedir}/lib/rpm-state/mailman
fi

%systemd_post mailman.service
exit 0

%preun
%systemd_preun mailman.service
exit 0

%postun
if [ $1 = 0 ]; then
  crontab -u %{name} -r 2>/dev/null
fi
%systemd_postun_with_restart mailman.service
exit 0

%triggerun -- mailman < 3:2.1.14-9
%{_bindir}/systemd-sysv-convert --save mailman >/dev/null 2>&1 ||:
/bin/systemctl enable mailman.service >/dev/null 2>&1
/sbin/chkconfig --del mailman >/dev/null 2>&1 || :
/bin/systemctl try-restart mailman.service >/dev/null 2>&1 || :

%files
%defattr(-,root,%{name})
%dir /usr/lib/%{name}
%dir /usr/lib/%{name}/Mailman
/usr/lib/%{name}/templates
/usr/lib/%{name}/bin
%dir /usr/lib/%{name}/cgi-bin/
%attr(2755, root, %{name}) /usr/lib/%{name}/cgi-bin/*
%dir /usr/lib/%{name}/cron
/usr/lib/%{name}/icons
%dir /usr/lib/%{name}/mail
%attr(2755, root, %{name}) /usr/lib/%{name}/mail/mailman
/usr/lib/%{name}/messages
/usr/lib/%{name}/pythonlib
/usr/lib/%{name}/scripts
# rpmlint will complain here about config files being in /usr
# but these are both data files -parts of mailman's web UI-
# and config files - user can change them to match the design
# and/or content of their web pages
%config(noreplace) /etc/%{name}/templates
/usr/lib/%{name}/tests
%dir /var/lib/%{name}
/var/lib/%{name}/data
/var/lib/%{name}/lists
/var/lib/%{name}/spam
%dir /var/lib/%{name}/archives
/var/lib/%{name}/archives/public
# fix for security issue #459530
%attr(2770,apache,%{name}) /var/lib/%{name}/archives/private
#cron dir minus one file which is listed later
/usr/lib/%{name}/cron/bumpdigests
/usr/lib/%{name}/cron/checkdbs
/usr/lib/%{name}/cron/cull_bad_shunt
/usr/lib/%{name}/cron/disabled
/usr/lib/%{name}/cron/gate_news
/usr/lib/%{name}/cron/mailpasswds
/usr/lib/%{name}/cron/nightly_gzip
/usr/lib/%{name}/cron/paths.py
/usr/lib/%{name}/cron/paths.pyc
/usr/lib/%{name}/cron/paths.pyo
/usr/lib/%{name}/cron/senddigests
#Mailman dir minus one file which is listed later
/usr/lib/%{name}/Mailman/Archiver
/usr/lib/%{name}/Mailman/Autoresponder.py
/usr/lib/%{name}/Mailman/Autoresponder.pyc
/usr/lib/%{name}/Mailman/Autoresponder.pyo
/usr/lib/%{name}/Mailman/Bouncer.py
/usr/lib/%{name}/Mailman/Bouncer.pyc
/usr/lib/%{name}/Mailman/Bouncer.pyo
/usr/lib/%{name}/Mailman/Bouncers
/usr/lib/%{name}/Mailman/Cgi
/usr/lib/%{name}/Mailman/Commands
/usr/lib/%{name}/Mailman/CSRFcheck.py
/usr/lib/%{name}/Mailman/CSRFcheck.pyc
/usr/lib/%{name}/Mailman/CSRFcheck.pyo
/usr/lib/%{name}/Mailman/Defaults.py
/usr/lib/%{name}/Mailman/Defaults.pyc
/usr/lib/%{name}/Mailman/Defaults.pyo
/usr/lib/%{name}/Mailman/Deliverer.py
/usr/lib/%{name}/Mailman/Deliverer.pyc
/usr/lib/%{name}/Mailman/Deliverer.pyo
/usr/lib/%{name}/Mailman/Digester.py
/usr/lib/%{name}/Mailman/Digester.pyc
/usr/lib/%{name}/Mailman/Digester.pyo
/usr/lib/%{name}/Mailman/Errors.py
/usr/lib/%{name}/Mailman/Errors.pyc
/usr/lib/%{name}/Mailman/Errors.pyo
/usr/lib/%{name}/Mailman/GatewayManager.py
/usr/lib/%{name}/Mailman/GatewayManager.pyc
/usr/lib/%{name}/Mailman/GatewayManager.pyo
#%/usr/lib/%{name}/Mailman/Generator.py
#%/usr/lib/%{name}/Mailman/Generator.pyc
#%/usr/lib/%{name}/Mailman/Generator.pyo
/usr/lib/%{name}/Mailman/Gui
/usr/lib/%{name}/Mailman/Handlers
/usr/lib/%{name}/Mailman/htmlformat.py
/usr/lib/%{name}/Mailman/htmlformat.pyc
/usr/lib/%{name}/Mailman/htmlformat.pyo
/usr/lib/%{name}/Mailman/HTMLFormatter.py
/usr/lib/%{name}/Mailman/HTMLFormatter.pyc
/usr/lib/%{name}/Mailman/HTMLFormatter.pyo
/usr/lib/%{name}/Mailman/i18n.py
/usr/lib/%{name}/Mailman/i18n.pyc
/usr/lib/%{name}/Mailman/i18n.pyo
/usr/lib/%{name}/Mailman/__init__.py
/usr/lib/%{name}/Mailman/__init__.pyc
/usr/lib/%{name}/Mailman/__init__.pyo
/usr/lib/%{name}/Mailman/ListAdmin.py
/usr/lib/%{name}/Mailman/ListAdmin.pyc
/usr/lib/%{name}/Mailman/ListAdmin.pyo
/usr/lib/%{name}/Mailman/LockFile.py
/usr/lib/%{name}/Mailman/LockFile.pyc
/usr/lib/%{name}/Mailman/LockFile.pyo
/usr/lib/%{name}/Mailman/Logging
/usr/lib/%{name}/Mailman/Mailbox.py
/usr/lib/%{name}/Mailman/Mailbox.pyc
/usr/lib/%{name}/Mailman/Mailbox.pyo
/usr/lib/%{name}/Mailman/MailList.py
/usr/lib/%{name}/Mailman/MailList.pyc
/usr/lib/%{name}/Mailman/MailList.pyo
/usr/lib/%{name}/Mailman/MemberAdaptor.py
/usr/lib/%{name}/Mailman/MemberAdaptor.pyc
/usr/lib/%{name}/Mailman/MemberAdaptor.pyo
/usr/lib/%{name}/Mailman/Message.py
/usr/lib/%{name}/Mailman/Message.pyc
/usr/lib/%{name}/Mailman/Message.pyo
/usr/lib/%{name}/Mailman/mm_cfg.py.dist
/usr/lib/%{name}/Mailman/MTA
/usr/lib/%{name}/Mailman/OldStyleMemberships.py
/usr/lib/%{name}/Mailman/OldStyleMemberships.pyc
/usr/lib/%{name}/Mailman/OldStyleMemberships.pyo
/usr/lib/%{name}/Mailman/Pending.py
/usr/lib/%{name}/Mailman/Pending.pyc
/usr/lib/%{name}/Mailman/Pending.pyo
/usr/lib/%{name}/Mailman/Post.py
/usr/lib/%{name}/Mailman/Post.pyc
/usr/lib/%{name}/Mailman/Post.pyo
/usr/lib/%{name}/Mailman/Queue
/usr/lib/%{name}/Mailman/SafeDict.py
/usr/lib/%{name}/Mailman/SafeDict.pyc
/usr/lib/%{name}/Mailman/SafeDict.pyo
/usr/lib/%{name}/Mailman/SecurityManager.py
/usr/lib/%{name}/Mailman/SecurityManager.pyc
/usr/lib/%{name}/Mailman/SecurityManager.pyo
/usr/lib/%{name}/Mailman/Site.py
/usr/lib/%{name}/Mailman/Site.pyc
/usr/lib/%{name}/Mailman/Site.pyo
/usr/lib/%{name}/Mailman/TopicMgr.py
/usr/lib/%{name}/Mailman/TopicMgr.pyc
/usr/lib/%{name}/Mailman/TopicMgr.pyo
/usr/lib/%{name}/Mailman/UserDesc.py
/usr/lib/%{name}/Mailman/UserDesc.pyc
/usr/lib/%{name}/Mailman/UserDesc.pyo
/usr/lib/%{name}/Mailman/Utils.py
/usr/lib/%{name}/Mailman/Utils.pyc
/usr/lib/%{name}/Mailman/Utils.pyo
/usr/lib/%{name}/Mailman/Version.py
/usr/lib/%{name}/Mailman/Version.pyc
/usr/lib/%{name}/Mailman/Version.pyo
/usr/lib/%{name}/Mailman/versions.py
/usr/lib/%{name}/Mailman/versions.pyc
/usr/lib/%{name}/Mailman/versions.pyo
%{_unitdir}/mailman.service
%doc %{docdir}
%dir %attr(0755,root,root) /usr/share/httpd/icons
%attr(0644,root,root) /usr/share/httpd/icons/*
%attr(0644, root, %{name}) %config(noreplace) %verify(not md5 size mtime) /etc/%{name}/mm_cfg.py
%attr(0644, root, %{name}) %config(noreplace) %verify(not md5 size mtime) /usr/lib/%{name}/Mailman/mm_cfg.py
%verify(not md5 size mtime) /usr/lib/%{name}/Mailman/mm_cfg.py?
%config(noreplace) /etc/httpd/conf.d/%{name}.conf
%config(noreplace) /etc/logrotate.d/%{name}
/etc/smrsh/%{name}
%dir %attr(755,root,%{name}) /etc/%{name}
%attr(0644, root, %{name}) %config(noreplace) %verify(not md5 size mtime) /etc/%{name}/sitelist.cfg
%attr(775,root,%{name}) /var/log/%{name}
%{_prefix}/lib/tmpfiles.d/mailman.conf
%attr(2775,root,%{name}) /var/spool/%{name}
%attr(0644,root,root) %config(noreplace) %verify(not md5 size mtime) /etc/cron.d/mailman
%attr(0644,root,%{name}) %config(noreplace) /usr/lib/%{name}/cron/crontab.in
%attr(0755,root,root) /usr/lib/%{name}/bin/mailman-update-cfg
%dir %attr(775,root,%{name}) /var/run/%{name}
%dir %attr(775,root,%{name}) /var/lock/%{name}

%changelog
* Mon Oct 25 2021 yaoxin <yaoxin30@huawei.com> - 2.1.29-7
- Fix CVE-2020-15011

* Tue Oct 19 2021 liwu <liwu13@huawei.com> - 2.1.29-6
- Fix CVE-2020-12108 CVE-2020-12137

* Fri Feb 14 2020 lihao <lihao129@huawei.com> - 2.1.29-5
- Package init
