%global _hardened_build 1

Name:          usbguard
Version:       0.7.2
Release:       7
Summary:       A tool for computer usb guard
License:       GPLv2+
URL:           https://usbguard.github.io/
Source0:       https://github.com/USBGuard/usbguard/releases/download/usbguard-%{version}/usbguard-%{version}.tar.gz
Source1:       usbguard-daemon.conf
Patch0000:     0001-Update-to-latest-PEGTL-API.patch
Patch0001:     CVE-2019-25058-1.patch
Patch0002:     CVE-2019-25058-2.patch
Patch0003:     CVE-2019-25058-3.patch
Patch0004:     CVE-2019-25058-4.patch
Patch0005:     CVE-2019-25058-5.patch
Patch0006:     CVE-2019-25058-6.patch
Patch0007:     CVE-2019-25058-7.patch
BuildRequires: libqb-devel libgcrypt-devel libstdc++-devel protobuf-devel protobuf-compiler PEGTL-static
BuildRequires: catch1-devel autoconf automake libtool bash-completion asciidoctor audit-libs-devel systemd
BuildRequires: qt5-qtbase-devel qt5-qtsvg-devel qt5-linguist dbus-glib-devel dbus-devel glib2-devel
BuildRequires: polkit-devel libxslt libxml2
Requires:      systemd

%description
USBGuard helps to protect your computer against rogue USB devices.

%package       devel
Summary:       Development files for usbguard
Requires:      usbguard = %{version}-%{release} pkgconfig libstdc++-devel

%description   devel
Development libraries and header files for usbguard.

%package       tools
Summary:       USBGuard Tools
Requires:      usbguard = %{version}-%{release}

%description   tools
Optional tools from the USBGuard software framework.

%package       applet-qt
Summary:       USBGuard Qt 5.x Applet
Requires:      usbguard = %{version}-%{release}
Obsoletes:     usbguard-applet-qt <= 0.3

%description   applet-qt
Optional Qt 5.x desktop applet for interacting with the USBGuard daemon component.

%package       dbus
Summary:       USBGuard D-Bus Service
Requires:      usbguard = %{version}-%{release} dbus polkit

%description   dbus
Optional component that provides a D-Bus interface to the USBGuard daemon component.

%package       help
Summary:       Documentation for usbguard
Requires:      usbguard = %{version}-%{release}

%description   help
Documentation for usbguard

%prep
%autosetup -n usbguard-%{version} -p1
rm -rf src/ThirdParty/{Catch,PEGTL}

%build
install -d ./m4
autoreconf -i -v --no-recursive ./
%configure --disable-silent-rules --without-bundled-catch --without-bundled-pegtl \
           --enable-systemd --with-gui-qt=qt5 --with-dbus --with-polkit \
           --with-crypto-library=gcrypt

%make_build

%check
make check

%install
%make_install

install -d %{buildroot}%{_sysconfdir}/usbguard/IPCAccessControl.d
cp %{SOURCE1} %{buildroot}%{_sysconfdir}/usbguard/usbguard-daemon.conf
chmod 644 %{buildroot}%{_sysconfdir}/usbguard/usbguard-daemon.conf

%delete_la

%preun
%systemd_preun usbguard.service

%post
/sbin/ldconfig
%systemd_post usbguard.service

%postun
/sbin/ldconfig
%systemd_postun usbguard.service

%preun         dbus
%systemd_preun usbguard-dbus.service

%post          dbus
%systemd_post usbguard-dbus.service

%postun        dbus
%systemd_postun_with_restart usbguard-dbus.service

%files
%doc LICENSE
%exclude %{_libdir}/*.a
%{_libdir}/*.so.*
%{_sbindir}/usbguard-daemon
%{_bindir}/usbguard
%dir %{_localstatedir}/log/usbguard
%dir %{_sysconfdir}/usbguard
%dir %{_sysconfdir}/usbguard/IPCAccessControl.d
%config(noreplace) %attr(0600,-,-) %{_sysconfdir}/usbguard/usbguard-daemon.conf
%config(noreplace) %attr(0600,-,-) %{_sysconfdir}/usbguard/rules.conf
%{_unitdir}/usbguard.service
%{_datadir}/bash-completion/completions/usbguard

%files         devel
%{_includedir}/*
%{_libdir}/*.so
%{_libdir}/pkgconfig/*.pc

%files         tools
%{_bindir}/usbguard-rule-parser

%files         applet-qt
%{_bindir}/usbguard-applet-qt
%{_datadir}/applications/usbguard-applet-qt.desktop
%{_datadir}/icons/hicolor/scalable/apps/usbguard-icon.svg

%files         dbus
%{_sbindir}/usbguard-dbus
%{_datadir}/dbus-1/system-services/org.usbguard.service
%{_datadir}/dbus-1/system.d/org.usbguard.conf
%{_datadir}/polkit-1/actions/org.usbguard.policy
%{_unitdir}/usbguard-dbus.service

%files         help
%doc README.adoc CHANGELOG.md
%{_mandir}/*/*

%changelog
* Mon Mar 07 2022 houyingchao <houyingchao@huawei.com> - 0.7.2-7
- Fix CVE-2019-25058

* Mon Apr 27 2020 lizhenhua <lizhenhua21@huawei.com> - 0.7.2-6
- Package init
