%bcond_with systemd

%{!?_initddir:%define _initddir /etc/rc.d/init.d}
%global rpcbind_user_group rpc

%define _unpackaged_files_terminate_build 1

Name:           rpcbind
Version:        1.2.5
Release:		2%{?_dist_release}%{?with_systemd:.systemd}
Summary:        Universal Addresses to RPC Program Number Mapper
Group:          System Environment/Daemons
BuildRoot:      %{_tmppath}/%{name}-%{version}-root
Vendor:         Project Vine
Distribution:   Vine Linux

License:        BSD
URL:            http://nfsv4.bullopensource.org
Source0:        http://downloads.sourceforge.net/rpcbind/%{name}-%{version}.tar.bz2
Source1:        rpcbind.init
Source2:        %{name}.sysconfig

Patch001:       rpcbind-1.2.5-rpcinfo-bufoverflow.patch

Patch100:       rpcbind-0.2.3-systemd-envfile.patch
Patch101:       rpcbind-0.2.3-systemd-tmpfiles.patch
Patch102:       rpcbind-0.2.4-runstatdir.patch
Patch103:       rpcbind-0.2.4-systemd-service.patch
Patch104:       rpcbind-0.2.4-systemd-rundir.patch

Requires: glibc-common setup
Conflicts: man-pages < 2.43-12
BuildRequires: automake, autoconf, libtool
BuildRequires: libtirpc-devel
BuildRequires: quota
BuildRequires: tcp_wrappers
#BuildRequires: quota-devel
#BuildRequires: tcp_wrappers-devel
Requires(pre): /usr/sbin/groupadd  /usr/sbin/groupdel
Requires(pre): /usr/sbin/useradd  /usr/sbin/userdel
Requires(pre): coreutils
Requires(pre): shadow-utils
%if %{with systemd}
%global rpcbind_state_dir /run/rpcbind
BuildRequires: systemd
BuildRequires: systemd-devel
%{?systemd_requires}
Requires(postun): coreutils
%else
%global rpcbind_state_dir %{_sharedstatedir}/rpcbind
Requires(post): /sbin/chkconfig
Requires(post): /sbin/service
Requires(preun): /sbin/chkconfig
Requires(preun): /sbin/service
Requires(postun): /sbin/service
%endif

#Provides: portmap = %{version}-%{release}
Provides: portmap = 4.0-66%{_dist_release}
Obsoletes: portmap <= 4.0-65.3

%description
The rpcbind utility is a server that converts RPC program numbers into
universal addresses.  It must be running on the host to be able to make
RPC calls on a server on that machine.


%prep
%setup -q
# 1637562 - rpcinfo: Fix stack buffer overflow
%patch001 -p1

%if %{with systemd}
%patch100 -p1
%patch101 -p1
%patch102 -p1
%patch103 -p1
%patch104 -p1
%endif


%build
%ifarch s390 s390x
PIE="-fPIE"
%else
PIE="-fpie"
%endif
export PIE

CFLAGS="`echo $RPM_OPT_FLAGS $ARCH_OPT_FLAGS $PIE`"

autoreconf -fisv
%configure CFLAGS="$CFLAGS" LDFLAGS="-pie" \
    --enable-warmstarts \
    --with-statedir="%rpcbind_state_dir" \
    --with-rpcuser="%rpcbind_user_group" \
    --with-nss-modules="files altfiles" \
%if !%{with systemd}
    --with-systemdsystemunitdir=no \
%endif
    --enable-debug

make all


%install
rm -rf %{buildroot}
mkdir -p %{buildroot}/sbin
mkdir -p %{buildroot}/usr/sbin
mkdir -p %{buildroot}%{_sysconfdir}/sysconfig
mkdir -p %{buildroot}%{_mandir}/man8
mkdir -p %{buildroot}%{rpcbind_state_dir}
%if %{with systemd}
mkdir -p %{buildroot}%{_unitdir}
mkdir -p %{buildroot}%{_tmpfilesdir}
%else
mkdir -p %{buildroot}%{_sysconfdir}/rc.d/init.d
install -m 755 %{SOURCE1} %{buildroot}%{_initddir}/rpcbind
%endif

make DESTDIR=$RPM_BUILD_ROOT install

install -m 644 %{SOURCE2} %{buildroot}/etc/sysconfig/rpcbind

mv %{buildroot}%{_sbindir}/rpcbind %{buildroot}/sbin/
mv %{buildroot}%{_bindir}/rpcinfo %{buildroot}%{_sbindir}/

%if %{with systemd}
sed -i -e 's|/usr/sbin/|/sbin/|' %{buildroot}%{_unitdir}/rpcbind.service
%endif


%clean
rm -rf %{buildroot}


%pre

# Check the validity of the rpc uid and gid.
# If they don't exist, create them
# If they exist but are the wrong value, remove them 
#   and recreate them with the correct value
# If they exist and are the correct value do nothing
rpcid=`getent passwd rpc | cut -d: -f 3`
if [ -n "$rpcid" -a "$rpcid" != "32" ]; then
	/usr/sbin/userdel  rpc 2> /dev/null || :
	/usr/sbin/groupdel rpc 2> /dev/null || : 
fi
if [ -z "$rpcid" -o "$rpcid" != "32" ]; then
	/usr/sbin/groupadd -o -g 32 rpc > /dev/null 2>&1
	/usr/sbin/useradd -o -l -c "Rpcbind Daemon" -d /var/lib/rpcbind -g 32 \
    	-M -s /sbin/nologin -u 32 rpc > /dev/null 2>&1
fi

%post 
%if %{with systemd}
%systemd_post rpcbind.service rpcbind.socket
%else
/sbin/chkconfig --add %{name}
%endif

%preun
%if %{with systemd}
%systemd_preun rpcbind.service rpcbind.socket
%else
if [ $1 -eq 0 -o -x /bin/systemctl ]; then
    service rpcbind stop > /dev/null 2>&1
    /sbin/chkconfig --del %{name}
	/usr/sbin/userdel  rpc 2>/dev/null || :
	/usr/sbin/groupdel rpc 2>/dev/null || :
	rm -rf /var/lib/rpcbind
fi
%endif

%postun
%if %{with systemd}
%systemd_postun_with_restart rpcbind.service rpcbind.socket
%else
if [ "$1" -ge "1" -a ! -x /bin/systemctl ]; then
    service rpcbind condrestart > /dev/null 2>&1
fi
%endif

%files
%defattr(-,root,root)
%license COPYING
%doc AUTHORS ChangeLog README
/sbin/rpcbind
%{_sbindir}/rpcinfo
%{_mandir}/man8/*
%config(noreplace) /etc/sysconfig/rpcbind
%if %{with systemd}
%{_unitdir}/%{name}.service
%{_unitdir}/%{name}.socket
%{_tmpfilesdir}/%{name}.conf
%else
%config %{_initddir}/rpcbind
%endif
%attr(0700, %{rpcbind_user_group}, %{rpcbind_user_group}) %dir %{rpcbind_state_dir}


%changelog
* Tue Apr 14 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> - 1.2.5-2
- added systemd support (disabled as default).

* Thu Nov 29 2018 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> - 1.2.5-1
- updated to 1.2.5.
- added Patch001.

* Mon Nov 23 2015 Yoji TOYODA <bsyamato@sea.plala.or.jp> - 0.2.1-1
- update to 0.2.1
- remove Patch100

* Sat Dec 28 2013 Yoji TOYODA <bsyamato@sea.plala.or.jp> - 0.2.0-6
- rebuild with VineSeed environment

* Sun Feb 14 2010 Ryoichi INAGAKI <ryo1@bc.wakwak.com> - 0.2.0-5
- Initial build for Vine Linux

* Sun Jul 26 2009 Fedora Release Engineering <rel-eng@lists.fedoraproject.org> - 0.2.0-4
- Rebuilt for https://fedoraproject.org/wiki/Fedora_12_Mass_Rebuild

* Mon Jul 06 2009 Adam Jackson <ajax@redhat.com> 0.2.0-3
- Requires(pre): coreutils for cut(1).

* Thu Jun 25 2009 Steve Dickson <steved@redhat.com> - 0.2.0-2
- Fixed pre scriptle failure during upgrades (bz 507364)
- Corrected the usage info to match what the rpcbind man
    page says. (bz 466332)
- Correct package issues (bz 503508)

* Fri May 29 2009 Steve Dickson <steved@redhat.com> - 0.2.0-1
- Updated to latest upstream release: 0.2.0

* Tue May 19 2009 Tom "spot" Callaway <tcallawa@redhat.com> - 0.1.7-3
- Replace the Sun RPC license with the BSD license, with the explicit permission of Sun Microsystems

* Wed Feb 25 2009 Fedora Release Engineering <rel-eng@lists.fedoraproject.org> - 0.1.7-2
- Rebuilt for https://fedoraproject.org/wiki/Fedora_11_Mass_Rebuild

* Wed Nov 19 2008 Steve Dickson <steved@redhat.com>  0.1.7-1
- Update to latest upstream release: 0.1.7

* Tue Sep 30 2008 Steve Dickson <steved@redhat.com>  0.1.6-3
- Fixed a typo in the rpcbind.init script that stop warm starts
  from happening with conrestarts
- Fixed scriptlet failure (bz 462533)

* Tue Sep 16 2008 Steve Dickson <steved@redhat.com> 0.1.6-2
- Added usptream patches 01 thru 03 that do:
    * Introduce helpers for ipprot/netid mapping
    * Change how we decide on the netids to use for portmap
    * Simplify port live check in pmap_svc.c

* Wed Jul  9 2008 Steve Dickson <steved@redhat.com> 0.1.6-1
- Updated to latest upstream release 0.1.6

* Wed Jul  2 2008 Steve Dickson <steved@redhat.com> 0.1.5-5
- Fixed SYNOPSIS section in the rpcinfo man page (bz 453729)

* Fri Jun 27 2008 Steve Dickson <steved@redhat.com> 0.1.5-4
- Removed the documentation about the non-existent 
  '-L' flag (bz 446915)

* Fri Jun 27 2008 Steve Dickson <steved@redhat.com> 0.1.5-3
- Set password and service lookups to be local (bz 447092)

* Mon Jun 23 2008 Steve Dickson <steved@redhat.com> 0.1.5-2
- rpcbind needs to downgrade to non-priviledgied group.

* Mon Jun 23 2008 Steve Dickson <steved@redhat.com> 0.1.5-1
- Updated to latest upstream release 0.1.5

* Mon Feb 11 2008 Steve Dickson <steved@redhat.com> 0.1.4-14
- Fixed a warning in pmap_svc.c
- Cleaned up warmstarts so uid are longer needed, also
  changed condrestarts to use warmstarts. (bz 428496)

* Thu Jan 24 2008 Steve Dickson <steved@redhat.com> 0.1.4-13
- Fixed connectivity with Mac OS clients by making sure handle_reply()
  sets the correct fromlen in its recvfrom() call (bz 244492)

* Mon Dec 17 2007 Steve Dickson <steved@redhat.com> 0.1.4-12
- Changed is_loopback() and check_access() see if the calling
  address is an address on a local interface, just not a loopback
  address (bz 358621).

* Wed Oct 17 2007 Steve Dickson <steved@redhat.com> 0.1.4-11
- Reworked logic in initscript so the correct exit is 
  used when networking does not exist or is set up
  incorrectly.

* Tue Oct 16 2007 Steve Dickson <steved@redhat.com> 0.1.4-10
- Corrected a typo in the initscript from previous 
  commit.

* Mon Oct 15 2007 Steve Dickson <steved@redhat.com> 0.1.4-9
- Fixed typo in Summary (bz 331811)
- Corrected init script (bz 247046)

* Sat Sep 15 2007 Steve Dickson <steved@redhat.com> 0.1.4-8
- Fixed typo in init script (bz 248285)
- Added autoconf rules to turn on secure host checking
  via libwrap. Also turned on host check by default (bz 248284)
- Changed init script to start service in runlevel 2 (bz 251568)
- Added a couple missing Requires(pre) (bz 247134)

* Fri May 25 2007 Steve Dickson <steved@redhat.com> 0.1.4-7
- Fixed condrestarts (bz 241332)

* Tue May 22 2007 Steve Dickson <steved@redhat.com> 0.1.4-6
- Fixed an ipv6 related segfault on startup (bz 240873)

* Wed Apr 18 2007 Steve Dickson <steved@redhat.com> 0.1.4-5
- Added dependency on setup which contains the correct
  rpcbind /etc/service entry which in turns stops 
  rpcbind from haning when NIS is enabled. (bz 236865)

* Wed Apr 11 2007 Jeremy Katz <katzj@redhat.com> - 0.1.4-4
- change man-pages requires into a conflicts as we don't have to have 
  man-pages installed, but if we do, we need the newer version

* Fri Apr  6 2007 Steve Dickson <steved@redhat.com> 0.1.4-3
- Fixed the Provides and Obsoletes statments to correctly
  obsolete the portmap package.
* Tue Apr  3 2007 Steve Dickson <steved@redhat.com> 0.1.4-2
- Added dependency on glibc-common which allows the
  rpcinfo command to be installed in the correct place.
- Added dependency on man-pages so the rpcinfo man 
  pages don't conflict.
- Added the creation of /var/lib/rpcbind which will be
  used to store state files.
- Make rpcbind run with the 'rpc' uid/gid when it exists.

* Wed Feb 21 2007 Steve Dickson <steved@redhat.com> 0.1.4-1
- Initial commit
- Spec reviewed (bz 228894)
- Added the Provides/Obsoletes which should
  cause rpcbind to replace portmapper
