%global __provides_exclude_from ^%{_libdir}/sane/.*\.so.*$
%global __requires_exclude ^libsane-.*\.so\.[0-9]*(\(\).*)?+$

Name:              sane-backends
Version:           1.2.1
Release:           2
Summary:           Scanner access software
License:           GPLv2+ and GPLv2+ and Public Domain and IJG and LGPLv2+ and MIT
URL:               http://www.sane-project.org
Source0:           https://gitlab.com/sane-project/backends/uploads/9e718daff347826f4cfe21126c8d5091/%{name}-%{version}.tar.gz
Source1:           saned.socket
Source2:           saned@.service.in
Source3:           66-saned.rules

BuildRequires:     gcc-c++ texlive-latex libusbx-devel libieee1284-devel libjpeg-devel libpng-devel
BuildRequires:     libtiff-devel libv4l-devel gettext systemd-devel gphoto2-devel systemd
Requires:          libpng systemd >= 196 systemd-udev >= 196
Requires:          sane-backends-libs = %{version}-%{release}


Patch0000:         CVE-2020-12861-CVE-2020-12866-CVE-2020-12864.patch
Patch0001:         Add-check-for-ports-to-avoid-Segmentation-fault.patch
# https://gitlab.com/sane-project/backends/-/commit/fd7b83c8f7b4da4a9e1fb715d070aa2fd96832ff
Patch0002:         CVE-2023-46047.patch
# https://gitlab.com/sane-project/backends/-/commit/a92ffb3d978329c29513b0acb98ae7987ec1bed7
Patch0003:         CVE-2023-46052.patch

%description
SANE (Scanner Access Now Easy) is a sane and simple interface to both local and networked scanners
and other image acquisition devices like digital still and video cameras.

%package           help
Summary:           Help documents for sane-backends
BuildArch:         noarch
Provides:          sane-backends-doc = %{version}-%{release}
Obsoletes:         sane-backends < 1.0.23-10 sane-backends-doc < %{version}-%{release}
Conflicts:         sane-backends < 1.0.23-10

%description       help
The sane-backends-help package conatins manual pages and documents for sane-backends.

%package           libs
Summary:           SANE libraries
Recommends:        sane-backends-drivers-cameras = %{version}-%{release}
Recommends:        sane-backends-drivers-scanners = %{version}-%{release}

%description       libs
This package contains the SANE libraries which are needed by applications that want to access scanners.

%package           devel
Summary:           SANE development toolkit
Requires:          sane-backends = %{version}-%{release} sane-backends-libs = %{version}-%{release}
Requires:          sane-backends-drivers-scanners = %{version}-%{release} sane-backends-drivers-cameras = %{version}-%{release}
Requires:          libusbx-devel libieee1284-devel libjpeg-devel libtiff-devel pkgconfig

%description       devel
This package contains libraries and header files for writing Scanner Access Now Easy (SANE) modules.

%package           drivers-scanners
Summary:           SANE backend drivers for scanners
Requires:          sane-backends = %{version}-%{release} sane-backends-libs = %{version}-%{release}

%description       drivers-scanners
This package contains backend drivers to access scanner hardware through SANE.

%package           drivers-cameras
Summary:           Scanner backend drivers for digital cameras
Requires:          sane-backends = %{version}-%{release} sane-backends-libs = %{version}-%{release}

%description       drivers-cameras
This package contains backend drivers to access digital cameras through SANE.

%package           daemon
Summary:           Scanner network daemon
Requires:          sane-backends = %{version}-%{release} sane-backends-libs = %{version}-%{release}
Requires(pre):     shadow-utils
%{?systemd_requires}

%description       daemon
This package contains saned which is the daemon that allows remote clients to access image acquisition devices available on the local host.

%prep
%autosetup -p1

%build
CFLAGS="%optflags -fno-strict-aliasing"
%configure  --with-gphoto2=%{_prefix} --with-docdir=%{_docdir}/sane-backends --with-systemd \
            --disable-locking --disable-rpath --with-usb --enable-pthread
%make_build

_topdir="$PWD"
cd tools
./sane-desc -m udev+hwdb -s "${_topdir}/doc/descriptions:${_topdir}/doc/descriptions-external" -d0 > udev/sane-backends.rules
./sane-desc -m hwdb -s "${_topdir}/doc/descriptions:${_topdir}/doc/descriptions-external" -d0 > udev/sane-backends.hwdb
cd ../

%install
%make_install

install -d %{buildroot}%{_datadir}/pixmaps
rm -f %{buildroot}%{_bindir}/gamma4scanimage
%delete_la_and_a

install -d %{buildroot}%{_prefix}/lib/udev/rules.d
install -d %{buildroot}%{_prefix}/lib/udev/hwdb.d
install -m 0644 tools/udev/sane-backends.rules %{buildroot}%{_prefix}/lib/udev/rules.d/65-sane-backends.rules
install -m 0644 tools/udev/sane-backends.hwdb %{buildroot}%{_prefix}/lib/udev/hwdb.d/20-sane-backends.hwdb
install -m 0644 %{SOURCE3} %{buildroot}%{_prefix}/lib/udev/rules.d/66-saned.rules

install -d %{buildroot}%{_libdir}/pkgconfig
install -m 0644 tools/sane-backends.pc %{buildroot}%{_libdir}/pkgconfig/

install -d %{buildroot}%{_docdir}/sane-backends-help
cd %{buildroot}%{_docdir}/sane-backends
for f in *; do
    if [ -d "$f" ]; then
        mv "$f" "%{buildroot}%{_docdir}/sane-backends-help/${f}"
    else
        case "$f" in
        AUTHORS|ChangeLog|COPYING|LICENSE|NEWS|PROBLEMS|README|README.linux)
            ;;
        backend-writing.txt|PROJECTS|sane-*.html)
            mv "$f" "%{buildroot}%{_docdir}/sane-backends-help/${f}"
            ;;
        *)
            rm -rf "$f"
            ;;
        esac
    fi
done
cd -

install -m 755 -d %{buildroot}%{_unitdir}
install -m 644 %{SOURCE1} %{buildroot}%{_unitdir}
sed 's|@CONFIGDIR@|%{_sysconfdir}/sane.d|g' < %{SOURCE2} > saned@.service
install -m 644 saned@.service %{buildroot}%{_unitdir}

%find_lang sane-backends

%post
udevadm hwdb --update >/dev/null 2>&1 || :

%postun
udevadm hwdb --update >/dev/null 2>&1 || :

%post libs
/sbin/ldconfig

%postun libs
/sbin/ldconfig

%pre daemon
getent group saned >/dev/null || groupadd -r saned
getent passwd saned >/dev/null || \
    useradd -r -g saned -d %{_datadir}/sane -s /sbin/nologin \
               -c "SANE scanner daemon user" saned
exit 0

%post daemon
%systemd_post saned.socket

%preun daemon
%systemd_preun saned.socket

%postun daemon
%systemd_postun saned.socket

%files -f sane-backends.lang
%dir %{_docdir}/sane-backends
%dir %{_libdir}/sane
%dir %{_datadir}/sane
%dir /etc/sane.d
%dir /etc/sane.d/dll.d
%doc %{_docdir}/sane-backends/*
%license %{_docdir}/sane-backends/LICENSE
%config(noreplace) /etc/sane.d/*.conf
%{_prefix}/lib/udev/*
%{_bindir}/*
%exclude %{_prefix}/lib/udev/rules.d/66-saned.rules
%exclude %{_bindir}/sane-config

%files help
%doc %{_docdir}/sane-backends-help
%{_mandir}/*/*
%exclude %{_mandir}/man1/gamma4scanimage.1*

%files libs
%{_libdir}/libsane.so.*

%files devel
%{_bindir}/sane-config
%{_includedir}/sane
%{_libdir}/libsane.so
%{_libdir}/pkgconfig/sane-backends.pc

%files drivers-scanners
%{_libdir}/sane/*
%exclude %{_libdir}/sane/libsane-gphoto2.*

%files drivers-cameras
%{_libdir}/sane/libsane-gphoto2.*

%files daemon
%{_sbindir}/saned
%{_prefix}/lib/udev/rules.d/66-saned.rules
%{_unitdir}/*

%changelog
* Fri May 10 2024 yaoxin <yao_xin001@hoperun.com> - 1.2.1-2
- Fix CVE-2023-46047 and CVE-2023-46052

* Sat Oct 07 2023 wulei <wu_lei@hoperun.com> - 1.2.1-1
- Update to 1.2.1

* Sat Oct 09 2021 houyingchao <houyingchao@huawei.com> - 1.0.28-11
- Fix CVE-2020-12863

* Fri Feb 05 2021 wangyue <wangyue92@huawei.com> - 1.0.28-10
- Fix CVE-2020-12862 CVE-2020-12865

* Wed Jan 27 2021 lingsheng <lingsheng@huawei.com> - 1.0.28-9
- Add check for ports to avoid Segmentation fault

* Mon Jan 18 2021 zhanghua <zhanghua40@huawei.com> - 1.0.28-8
- fix CVE-2020-12867

* Wed Dec 16 2020 zhanghua <zhanghua40@huawei.com> - 1.0.28-7
- fix CVE-2020-12861, CVE-2020-12866, CVE-2020-12864

* Fri Feb 14 2020 Senlin Xia <xiasenlin1@huawei.com> - 1.0.28-6
- Package init
