%global nmconfdir %{_sysconfdir}/NetworkManager
%global dhcpconfdir %{_sysconfdir}/ub-dhcp

Name:      ub-dhcp
Version:   1.0.0
Release:   1
Summary:   Dynamic host configuration protocol software
#Please don't change the epoch on this package
Epoch:     12
License:   ISC
Source0:  ub-dhcp-%{version}.tar.gz
Source1:  ub-dhclient-script
Source2:  README.ub-dhclient.d
Source3:  11-ub-dhclient
Source5:  56ub-dhclient
Source6:  ub-dhcpd.service
Source7:  ub-dhcpd6.service
Source8:  ub-dhcrelay.service

BuildRequires: gcc autoconf automake libtool openldap-devel krb5-devel libcap-ng-devel
BuildRequires: systemd systemd-devel
# run tests need
BuildRequires: kyua atf-tests

Requires: shadow-utils coreutils grep sed systemd gawk ipcalc iproute iputils

Provides:  %{name}-common %{name}-libs %{name}-server %{name}-relay %{name}-compat %{name}-client
Obsoletes: %{name}-common %{name}-libs %{name}-server %{name}-relay %{name}-compat %{name}-client

Provides:  ub-dhcp = %{epoch}:%{version}-%{release}
Obsoletes: ub-dhcp < %{epoch}:%{version}-%{release}

Provides: ub-dhclient = %{epoch}:%{version}-%{release}
Obsoletes: ub-dhclient < %{epoch}:%{version}-%{release}


%description
The Dynamic Host Configuration Protocol (DHCP) is a network management protocol used on UDP/IP networks whereby a DHCP server dynamically assigns an IP address and other network configuration parameters to each device on a network so they can communicate with other IP networks.

%package devel
Summary: Development headers and libraries for interfacing to the DHCP server
Requires: %{name} = %{epoch}:%{version}-%{release}

%description devel
Header files for using the ISC DHCP libraries.  The
libub-dhcpctl and libub-omapi static libraries are also included in this package.

%package_help

%prep
%setup -n %{name}-%{version}
pushd bind
tar -xvf bind.tar.gz
ln -s bind-9* bind
popd
#rm bind/bind.tar.gz

sed -i -e 's|/var/db/|%{_localstatedir}/lib/ub-dhcpd/|g' contrib/ub-dhcp-lease-list.pl

%build

CFLAGS="%{optflags} -fno-strict-aliasing" \
%configure --with-srv-lease-file=%{_localstatedir}/lib/ub-dhcpd/ub-dhcpd.leases \
    --with-srv6-lease-file=%{_localstatedir}/lib/ub-dhcpd/ub-dhcpd6.leases \
    --with-cli-lease-file=%{_localstatedir}/lib/ub-dhclient/ub-dhclient.leases \
    --with-cli6-lease-file=%{_localstatedir}/lib/ub-dhclient/ub-dhclient6.leases \
    --with-srv-pid-file=%{_localstatedir}/run/ub-dhcpd.pid \
    --with-srv6-pid-file=%{_localstatedir}/run/ub-dhcpd6.pid \
    --with-cli-pid-file=%{_localstatedir}/run/ub-dhclient.pid \
    --with-cli6-pid-file=%{_localstatedir}/run/ub-dhclient6.pid \
    --with-relay-pid-file=%{_localstatedir}/run/ub-dhcrelay.pid \
    --with-ldap --with-ldapcrypto --with-ldap-gssapi --enable-log-pid --enable-paranoia --enable-early-chroot \
    --enable-binary-leases --with-systemd \
    --with-atf

# define LDAP_CONFIGURATION when run common tests
sed -i "s/ATF_CFLAGS =/ATF_CFLAGS = -DLDAP_CONFIGURATION/g" common/tests/Makefile

make

%install
%make_install

install -D -p -m 0755 %{SOURCE1} $RPM_BUILD_ROOT%{_sbindir}/ub-dhclient-script

install -p -m 0644 %{SOURCE2} .

mkdir -p $RPM_BUILD_ROOT%{dhcpconfdir}/ub-dhclient.d

mkdir -p $RPM_BUILD_ROOT%{nmconfdir}/dispatcher.d
install -p -m 0755 %{SOURCE3} $RPM_BUILD_ROOT%{nmconfdir}/dispatcher.d

install -D -p -m 0755 %{SOURCE5} $RPM_BUILD_ROOT%{_libdir}/pm-utils/sleep.d/56ub-dhclient

mkdir -p $RPM_BUILD_ROOT%{_unitdir}
install -m 644 %{SOURCE6} $RPM_BUILD_ROOT%{_unitdir}
install -m 644 %{SOURCE7} $RPM_BUILD_ROOT%{_unitdir}
install -m 644 %{SOURCE8} $RPM_BUILD_ROOT%{_unitdir}

mkdir -p $RPM_BUILD_ROOT%{_localstatedir}/lib/ub-dhcpd/
touch $RPM_BUILD_ROOT%{_localstatedir}/lib/ub-dhcpd/ub-dhcpd.leases
touch $RPM_BUILD_ROOT%{_localstatedir}/lib/ub-dhcpd/ub-dhcpd6.leases
mkdir -p $RPM_BUILD_ROOT%{_localstatedir}/lib/ub-dhclient/

mkdir -p $RPM_BUILD_ROOT%{_sysconfdir}/sysconfig
cat <<EOF > %{buildroot}%{_sysconfdir}/sysconfig/ub-dhcpd
# WARNING: This file is NOT used anymore.

# If you are here to restrict what interfaces should dhcpd listen on,
# be aware that dhcpd listens *only* on interfaces for which it finds subnet
# declaration in dhcpd.conf. It means that explicitly enumerating interfaces
# also on command line should not be required in most cases.

# If you still insist on adding some command line options,
# copy dhcpd.service from /lib/systemd/system to /etc/systemd/system and modify
# it there.
# https://fedoraproject.org/wiki/Systemd#How_do_I_customize_a_unit_file.2F_add_a_custom_unit_file.3F

# example:
# $ cp /usr/lib/systemd/system/ub-dhcpd.service /etc/systemd/system/
# $ vi /etc/systemd/system/ub-dhcpd.service
# $ ExecStart=/usr/sbin/ub-dhcpd -f -cf /etc/ub-dhcp/ub-dhcpd.conf -user ub-dhcpd -group ub-dhcpd --no-pid <your_interface_name(s)>
# $ systemctl --system daemon-reload
# $ systemctl restart ub-dhcpd.service
EOF

mkdir -p $RPM_BUILD_ROOT%{dhcpconfdir}
cat << EOF > %{buildroot}%{dhcpconfdir}/ub-dhcpd.conf
#
# DHCP Server Configuration file.
#   see /usr/share/doc/dhcp-server/ub-dhcpd.conf.example
#   see dhcpd.conf(5) man page
#
EOF
cat << EOF > %{buildroot}%{dhcpconfdir}/ub-dhcpd6.conf
#
# DHCPv6 Server Configuration file.
#   see /usr/share/doc/ub-dhcp-server/ub-dhcpd6.conf.example
#   see ub-dhcpd.conf(5) man page
#
EOF

rm -f $RPM_BUILD_ROOT/usr/lib/debug/usr/sbin/ub-dhcrelay-4.3.6-28.7.aarch64.debug

mkdir -p $RPM_BUILD_ROOT%{_datadir}/doc/ub-dhcp-client
mkdir -p $RPM_BUILD_ROOT%{_datadir}/doc/ub-dhcp-server

mv -f $RPM_BUILD_ROOT%{_sysconfdir}/ub-dhclient.conf.example doc/examples/ub-dhclient-dhcpv4.conf
mv -f $RPM_BUILD_ROOT%{_sysconfdir}/ub-dhcpd.conf.example doc/examples/ub-dhcpd-dhcpv4.conf
install -p -m 0755 doc/examples/ub-dhclient-dhcpv4.conf $RPM_BUILD_ROOT%{_datadir}/doc/ub-dhcp-client/ub-dhclient.conf.example
install -p -m 0755 doc/examples/ub-dhcpd-dhcpv4.conf $RPM_BUILD_ROOT%{_datadir}/doc/ub-dhcp-server/ub-dhcpd.conf.example

install -p -m 0755 doc/examples/ub-dhclient-dhcpv6.conf $RPM_BUILD_ROOT%{_datadir}/doc/ub-dhcp-client/ub-dhclient6.conf.example
install -p -m 0755 doc/examples/ub-dhcpd-dhcpv6.conf $RPM_BUILD_ROOT%{_datadir}/doc/ub-dhcp-server/ub-dhcpd6.conf.example

install -D -p -m 0644 contrib/ldap/ub-dhcp.schema $RPM_BUILD_ROOT%{_sysconfdir}/openldap/schema/ub-dhcp.schema

find $RPM_BUILD_ROOT -type f -name "*.la" -delete -print

%check
make check

%pre
%global gid_uid 177
if ! getent group dhcpd > /dev/null ; then
    groupadd --force --gid %{gid_uid} --system ub-dhcpd
fi

if ! getent passwd dhcpd >/dev/null ; then
    if ! getent passwd %{gid_uid} >/dev/null ; then
      useradd --system --uid %{gid_uid} --gid ub-dhcpd --home / --shell /sbin/nologin --comment "DHCP server" ub-dhcpd
    else
      useradd --system --gid ub-dhcpd --home / --shell /sbin/nologin --comment "DHCP server" ub-dhcpd
    fi
fi



exit 0

%preun
%systemd_preun ub-dhcpd.service ub-dhcpd6.service ub-dhcrelay.service


%post
/sbin/ldconfig
%systemd_post ub-dhcpd.service ub-dhcpd6.service ub-dhcrelay.service

for servicename in ub-dhcpd ub-dhcpd6 ub-dhcrelay; do
  etcservicefile=%{_sysconfdir}/systemd/system/${servicename}.service
  if [ -f ${etcservicefile} ]; then
    grep -q Type= ${etcservicefile} || sed -i '/\[Service\]/a Type=notify' ${etcservicefile}
    sed -i 's/After=network.target/Wants=network-online.target\nAfter=network-online.target/' ${etcservicefile}
  fi
done
exit 0

%postun
/sbin/ldconfig
%systemd_postun_with_restart ub-dhcpd.service ub-dhcpd6.service ub-dhcrelay.service

%files
%defattr(-,root,root)
%license LICENSE
%doc README RELNOTES doc/References.txt
%doc README.ub-dhclient.d
%doc contrib/ldap/ contrib/ub-dhcp-lease-list.pl
%{_datadir}/doc/ub-dhcp-client/ub-dhclient.conf.example
%{_datadir}/doc/ub-dhcp-server/ub-dhcpd.conf.example
%{_datadir}/doc/ub-dhcp-client/ub-dhclient6.conf.example
%{_datadir}/doc/ub-dhcp-server/ub-dhcpd6.conf.example
%dir %{_sysconfdir}/openldap/schema
%config(noreplace) %{_sysconfdir}/openldap/schema/ub-dhcp.schema
%attr(0750,root,root) %dir %{dhcpconfdir}
%dir %{_localstatedir}/lib/ub-dhclient
%dir %{dhcpconfdir}/ub-dhclient.d
%dir %{_sysconfdir}/NetworkManager
%dir %{_sysconfdir}/NetworkManager/dispatcher.d
%{_sysconfdir}/NetworkManager/dispatcher.d/11-ub-dhclient
%attr(0644,root,root)   %{_unitdir}/ub-dhcpd.service
%attr(0644,root,root)   %{_unitdir}/ub-dhcpd6.service
%attr(0644,root,root) %{_unitdir}/ub-dhcrelay.service
%attr(0755,dhcpd,dhcpd) %dir %{_localstatedir}/lib/ub-dhcpd
%attr(0644,dhcpd,dhcpd) %verify(mode) %config(noreplace) %{_localstatedir}/lib/ub-dhcpd/ub-dhcpd.leases
%attr(0644,dhcpd,dhcpd) %verify(mode) %config(noreplace) %{_localstatedir}/lib/ub-dhcpd/ub-dhcpd6.leases
%config(noreplace) %{_sysconfdir}/sysconfig/ub-dhcpd
%config(noreplace) %{dhcpconfdir}/ub-dhcpd.conf
%config(noreplace) %{dhcpconfdir}/ub-dhcpd6.conf
%{_sbindir}/ub-dhcpd
%{_sbindir}/ub-dhclient
%{_sbindir}/ub-dhclient-script
%{_sbindir}/ub-dhcrelay
%{_bindir}/ub-omshell
%attr(0755,root,root) %{_libdir}/pm-utils/sleep.d/56ub-dhclient

%files  devel
%defattr(-,root,root)
%doc doc/IANA-arp-parameters doc/api+protocol
%{_includedir}/dhcpctl
%{_includedir}/omapip
%{_libdir}/libub-dhcp*.a
%{_libdir}/libub-omapi.a


%files help
%defattr(644,root,root)
%doc doc/*
%{_mandir}/man1/ub-omshell.1.gz
%{_mandir}/man5/ub-dhcpd.conf.5.gz
%{_mandir}/man5/ub-dhcpd.leases.5.gz
%{_mandir}/man8/ub-dhcpd.8.gz
%{_mandir}/man5/ub-dhcp-options.5.gz
%{_mandir}/man5/ub-dhcp-eval.5.gz
%{_mandir}/man5/ub-dhclient.conf.5.gz
%{_mandir}/man5/ub-dhclient.leases.5.gz
%{_mandir}/man8/ub-dhclient.8.gz
%{_mandir}/man8/ub-dhclient-script.8.gz
%{_mandir}/man8/ub-dhcrelay.8.gz
%{_mandir}/man3/ub-dhcpctl.3.gz
%{_mandir}/man3/ub-omapi.3.gz

%changelog
* Fri Oct 13 2023 Jeiwei Li <lijiewei5@huawei.com> - 12:1.0.0-1
- Release based on dhcp 4.4.3.

* Thu Jun 29 2023 renmingshuai <renmingshuai@huawei.com> - 12:4.4.3-3
- Type:bugfix
- ID:
- SUG:restart
- DESC:revert the correction about the logic in dhclient

* Sat May 27 2023 renmingshuai <renmingshuai@huawei.com> - 12:4.4.3-2
- Type:bugfix
- ID:NA
- SUG:restart
- DESC:add Restart in dhcpd.service

* Tue Nov 1 2022 renmingshuai <renmingshuai@huawei.com> - 12:4.4.3-1
- Type:requirement
- ID:NA
- SUG:restart
- DESC:update to 4.4.3

* Thu Aug 25 2022 renmingshuai <renmingshuai@huawei.com> - 4.4.2-13
- Type:bugfix
- ID:NA
- SUG:restart
- DESC:add dhX.conf.example in doc

* Wed Aug 24 2022 renmingshuai <renmingshuai@huawei.com> - 4.4.2-12
- Type:bugfix
- ID:NA
- SUG:restart
- DESC:add a test case for PXE to support ipv6
       support lease time config for ipv6

* Tue Feb 8 2022 renmingshuai <renmingshuai@huawei.com> - 4.4.2-11
- Type:bugfix
- ID:NA
- SUG:restart
- DESC:fix error message display

* Mon Feb 7 2022 renmingshuai <renmingshuai@huawei.com> - 4.4.2-10
- Type:bugfix
- ID:NA
- SUG:restart
- DESC:fix coredump when client active is NULL

* Wed Jan 12 2022 renmingshuai <renmingshuai@huawei.com> - 4.4.2-9
- Type:bugfix
- ID:NA
- SUG:restart
- DESC:modofy upstream patches name and add reference

* Wed Jan 05 2022 renmingshuai <renmingshuai@huawei.com> - 4.4.2-8
- Type:bugfix
- ID:NA
- SUG:restart
- DESC:remove build require bind-export-devel and add buildin bind

* Fri Jul 30 2021 renmingshuai <renmingshuai@huawei.com> - 4.4.2-7
- Type:bugfix
- ID:NA
- SUG:restart
- DESC:fix multiple defination with gcc 10

* Mon May 31 2021 renmingshuai <renmingshuai@huawei.com> - 4.4.2-6
- Type:CVE
- ID:NA
- SUG:restart
- DESC:CVE-2021-25217

* Sat Feb 20 2021 hanzhijun <hanzhijun1@huawei.com> - 4.4.2-5
- Type:bugfix
- ID:NA
- SUG:restart
- DESC:dhcp remove buildin bind

* Tue Dec 29 2020 quanhongfei <quanhongfei@huawei.com> - 4.4.2-4
- Type:bugfix
- ID:NA
- SUG:restart
- DESC:fix dhcp 64_bit lease parse 

* Thu Sep 10 2020 gaihuiying <gaihuiying1@huawei.com> - 4.4.2-3
- Type:bugfix
- ID:NA
- SUG:restart
- DESC: change ownership of /var/lib/dhcpd/ to dhcpd:dhcpd

* Tue Sep 01 2020 yuboyun <yuboyun@huawei.com> - 4.4.2-2
- Type:NA
- ID:NA
- SUG:NA
- DESC: add yaml file

* Wed Jul 22 2020 gaihuiying<gaihuiying1@huawei.com> - 4.4.2-1
- Type:requirement
- ID:NA
- SUG:restart
- DESC: update to 4.4.2

* Tue Mar 3 2020 zhanglu<zhanglu37@huawei.com> - 4.3.6-37
- Type:bugfix
- ID:NA
- SUG:restart
- DESC: recheck if last pid was held by other process

* Thu Feb 27 2020 zhanglu<zhanglu37@huawei.com> - 4.3.6-36
- Type:bugfix
- ID:NA
- SUG:restart
- DESC: check if last pid when held by other process

* Wed Jan 22 2020 zhanglu<zhanglu37@huawei.com> - 4.3.6-35
- Type:bugfix
- ID:NA
- SUG:restart
- DESC: modify dhcpd coredump when discover interfaces

* Sat Jan 11 2020 openEuler Buildteam <buildteam@openeuler.org> - 4.3.6-34
- Type:enhancement
- ID:NA
- SUG:NA
- DESC: delete patches

* Tue Dec 24 2019 openEuler Buildteam <buildteam@openeuler.org> - 4.3.6-33
- rename doc subpackage as help subpackage

* Sat Dec 21 2019 openEuler Buildteam <buildteam@openeuler.org> - 4.3.6-32
- Type:bugfix
- ID:NA
- SUG:NA
- DESC:Fix dhcpd 2038 problem;
       Adds address prefix len to dhclient cli

* Wed Sep 25 2019 openEuler Buildteam <buildteam@openeuler.org> - 4.3.6-31
- Type:bugfix
- ID:NA
- SUG:restart
- DESC: reducing getifaddrs calls and improving code performance

* Mon Sep 9 2019 openEuler Buildteam <buildteam@openeuler.org> - 4.3.6-30
- Type:bugfix
- Id:NA
- SUG:NA
- DESC:Fix dhcp package installation failed

* Thu Sep 5 2019 hufeng <solar.hu@huawei.com> - 4.3.6-29
-Create dhcp spec
