# Tests by default. No tests: rpm -ba --define 'dotests 0' nmap*.spec
%{!?dotests: %define dotests 1}
# To build a static RPM, add
#     --define "static 1"
# to the rpmbuild command line. To build without Ncat, add
#     --define "buildncat 0"
# To build without Nping, add
#     --define "buildnping 0"
#
# To specify openssl dir, add something like:
#     --define "openssl /usr/local/ssl"

%define name nmap
%define version 7.94
%define release 1
%define _prefix /opt/freeware

# Find where Python modules are installed. See
# http://fedoraproject.org/wiki/Packaging/Python.
%{!?python_sitelib: %define python_sitelib %(%{__python} -c "from distutils.sysconfig import get_python_lib; print get_python_lib()")}

Summary: Network exploration tool and security scanner
Name: %{name}
Version: %{version}
Release: %{release}
Epoch: 2
License: https://nmap.org/man/man-legal.html
Group: Applications/System
Source0: https://nmap.org/dist/%{name}-%{version}.tar.bz2
Patch0: %{name}-7.94-1.aix.patch
Patch1: %{name}-7.60-aix.patch
Patch2: dnet-stripped.diff
Patch5: %{name}-7.60-nping.patch

URL: https://nmap.org

# Disable automatic dependency calculation because we want to provide
# packages for more than one version of Python. Without this, RPM will
# grep through the installed files and automatically add things like
#   Requires: python(abi) = 2.4
# setup.py takes care of adjusting sys.path to point to wherever the
# modules were installed so it's not dependent on any particular ABI.
#AutoReqProv: no
# For Ndiff.

# RPM can't be relocatable until I stop storing path info in the binary.
# Prefix: %{_prefix}

BuildRequires: patch, make
BuildRequires: gcc >= 10.3.0, gcc-c++ >= 10.3.0 
BuildRequires: libpcap-devel >= 1.10.0
BuildRequires: libstdc++-devel >= 10.3.0
BuildRequires: lua-devel >= 5.4.1
BuildRequires: pcre-devel >= 8.44
BuildRequires: python3-devel >= 3.7.12 
BuildRequires: libssh2-devel >= 1.10.0

Requires: libssh2 >= 1.10.0
Requires: libgcc >= 10.3.0
Requires: libpcap >= 1.10.0
Requires: libstdc++ >= 10.3.0
Requires: lua >= 5.4.1
Requires: pcre >= 8.44
Requires: python3 >= 3.7.12 
Requires: zlib >= 1.2.3

%description

Nmap ("Network Mapper") is a free and open source utility
for network exploration or security auditing. Many systems and network
administrators also find it useful for tasks such as network
inventory, managing service upgrade schedules, and monitoring host or
service uptime. Nmap uses raw IP packets in novel ways to determine
what hosts are available on the network, what services (application
name and version) those hosts are offering, what operating systems
(and OS versions) they are running, what type of packet
filters/firewalls are in use, and dozens of other characteristics. It
was designed to rapidly scan large networks, but works fine against
single hosts. Nmap runs on all major computer operating systems, and
both console and graphical versions are available.

# Ncat subpackage
%if "%{buildncat}" != "0"
%package -n ncat
Summary: Nmap's Netcat replacement
Group: Applications/System
BuildRequires: libssh2-devel >= 1.10.0
BuildRequires: libpcap-devel >= 1.10.0

Requires: %{name} = %{epoch}:%{version}-%{release}
Requires: libssh2 >= 1.10.0
Requires: libpcap >= 1.10.0
Requires: lua >= 5.4.1

%description -n ncat
Ncat is a feature packed networking utility which will read and
write data across a network from the command line.  It uses both
TCP and UDP for communication and is designed to be a reliable
back-end tool to instantly provide network connectivity to other
applications and users. Ncat will not only work with IPv4 and IPv6
but provides the user with a virtually limitless number of potential
uses.
%endif

# Nping subpackage
%if "%{buildnping}" != "0"
%package -n nping
# If this 0. prefix is removed it must also be removed from Makefile.in.
Version: 0.%{version}
Summary: Nping packet generator
Group: Applications/System
BuildRequires: libssh2-devel >= 1.10.0
BuildRequires: libpcap-devel >= 1.10.0

Requires: %{name} = %{epoch}:7.94-%{release}
Requires: libgcc >= 10.3.0
Requires: libstdc++ >= 10.3.0
Requires: libssh2 >= 1.10.0
Requires: libpcap >= 1.10.0

%description -n nping
Nping is an open source tool for network packet generation, response
analysis and response time measurement. Nping allows to generate network
packets of a wide range of protocols, letting users to tune virtually
any field of the protocol headers. While Nping can be used as a simple
ping utility to detect active hosts, it can also be used as a raw packet
generator for network stack stress tests, ARP poisoning, Denial of
Service attacks, route tracing, etc.
%endif

%prep
%setup -q
%patch0 -p1
%patch1 -p0
%patch2 -p0
%patch5 -p0

rm -rf   /tmp/%{name}-%{version}-32bit
cp -pr . /tmp/%{name}-%{version}-32bit
rm -fr *
mv       /tmp/%{name}-%{version}-32bit 32bit
cp -pr 32bit 64bit

%build
export CONFIG_SHELL=/opt/freeware/bin/bash
export CC="gcc -D_LARGE_FILES=1 -maix32"
export CXX="g++ -D_LARGE_FILES=1 -maix32"

#Build on 32bit mode
export OBJECT_MODE=32
cd 32bit
export PYTHON="/opt/freeware/bin/python3.7_32"
export CFLAGS="-I/opt/freeware/include -DSYSV -D_AIX -D_AIX32 -D_AIX41 -D_AIX43 -D_AIX51 -D_ALL_SOURCE -DFUNCPROTO=15 -O2 -I/opt/freeware/include"
export CXXFLAGS="$CFLAGS "

export LDFLAGS="-L/opt/freeware/lib  -lssh2 -Wl,-brtl -Wl,-blibpath:/opt/freeware/lib:/usr/lib:/lib -Wl,-bmaxdata:0x80000000"
%configure --prefix=%{_prefix} --libdir=%{_libdir} --mandir=%{_mandir} --with-openssl=/usr --without-zenmap --without-ndiff --with-libdnet=included --with-libpcap=%{_prefix} --with-libpcre=%{_prefix} --with-liblua=%{_prefix}  --with-libz=%{_prefix} --with-libssh2=%{_prefix} 
%if "%{buildncat}" == "0"
%configure --without-ncat
%endif
%if "%{buildnping}" == "0"
%configure --without-nping
%endif
%if "%{static}" == "1"
make static
%else
make
%endif

if [ "%{dotests}" == 1 ]
then
    (gmake -k check || true)
fi

#Build on 64bit mode
export OBJECT_MODE=64
cd ../64bit
export CC="gcc -maix64"
export CXX="g++ -maix64"
export PYTHON="/opt/freeware/bin/python3.7"
export CFLAGS="-I/opt/freeware/include -DSYSV -D_AIX -D_AIX32 -D_AIX41 -D_AIX43 -D_AIX51 -D_ALL_SOURCE -DFUNCPROTO=15 -O2 -I/opt/freeware/include"
export CXXFLAGS="$CFLAGS"
export LDFLAGS="-L/opt/freeware/lib64 -L/opt/freeware/lib -lssh2 -Wl,-brtl -Wl,-blibpath:/opt/freeware/lib64:/opt/freeware/lib:/usr/lib:/lib"
%configure --prefix=%{_prefix} --libdir=%{_libdir}64 --mandir=%{_mandir} --with-openssl=/usr --without-zenmap --without-ndiff --with-libdnet=included --with-libpcap=%{_prefix} --with-libpcre=%{_prefix}  --with-liblua=%{_prefix}  --with-libz=%{_prefix}  --with-libssh2=%{_prefix}
%if "%{buildncat}" == "0"
%configure --without-ncat
%endif
%if "%{buildnping}" == "0"
%configure --without-nping
%endif
%if "%{static}" == "1"
make static
%else
make
%endif

if [ "%{dotests}" == 1 ]
then
    (gmake -k check || true)
fi

%install
[ "$RPM_BUILD_ROOT" != "/" ] && rm -rf $RPM_BUILD_ROOT

#Install on 64bit mode
export OBJECT_MODE=64
cd 64bit
make install DESTDIR=$RPM_BUILD_ROOT
rm -f $RPM_BUILD_ROOT%{_bindir}/uninstall_ndiff
(
    cd  ${RPM_BUILD_ROOT}/%{_prefix}/bin
    for fic in $(ls -1| grep -v -e _32 -e _64)
    do
	mv $fic "$fic"_64
    done
)

#Install on 32bit mode
export OBJECT_MODE=32
cd ../32bit
export LDFLAGS="-L/opt/freeware/lib -Wl,-blibpath:/opt/freeware/lib:/usr/lib:/lib -Wl,-bmaxdata:0x80000000"
export LD_LIBRARY_PATH="/opt/freeware/lib"

make install DESTDIR=$RPM_BUILD_ROOT
# Don't package the Ndiff uninstaller with the RPM.
rm -f $RPM_BUILD_ROOT%{_bindir}/uninstall_ndiff
(
    cd  ${RPM_BUILD_ROOT}/%{_prefix}/bin
    for fic in $(ls -1| grep -v -e _32 -e _64)
    do
	mv $fic "$fic"_32
	ln -sf "$fic"_64 $fic
    done
)

strip -X32_64 $RPM_BUILD_ROOT%{_bindir}/* || :
gzip $RPM_BUILD_ROOT%{_mandir}/man1/* || :


%clean
[ "$RPM_BUILD_ROOT" != "/" ] && rm -rf $RPM_BUILD_ROOT

%files 
%defattr(-,root,system)
%doc 32bit/COPYING
%doc 32bit/docs/README
%doc 32bit/docs/nmap.usage.txt
%{_mandir}/man1/nmap.1.gz
%{_mandir}/*/man1/n*
%{_bindir}/nmap*
%{_datadir}/nmap

%if "%{buildncat}" != "0"
%files -n ncat
%defattr(-,root,system)
%doc %{_mandir}/man1/ncat.1.gz
%{_bindir}/ncat*
%{_datadir}/ncat
%endif


%if "%{buildnping}" != "0"
%files -n nping
%defattr(-,root,system)
%doc %{_mandir}/man1/nping.1.gz
%{_bindir}/nping*
%endif


%changelog
* Mon Aug 22 2022 Neha Jain <nehajain29@in.ibm.com> 7.92-1
- Update to 7.92 version

* Wed Sep 16 2020 Reshma V Kumar <reskumar@in.ibm.com> 7.80-1
- Update to 7.80

* Thu Jul 18 2019 Reshma V Kumar <reskumar@in.ibm.com> 7.60-4
- Rebuild to fix nping (invalid argument) issue

* Fri Jan 04 2019 Reshma V Kumar <reskumar@in.ibm.com> 7.60-3
- Rebuild with libpcap-1.9.0

* Wed Oct 24 2018 Reshma V Kumar <reskumar@in.ibm.com> 7.60-2
- Rebuild after adding proper dependency for ncat and nping

* Wed Jan 24 2018 Reshma V Kumar <reskumar@in.ibm.com> 7.60-1
- Initial port for AIX

* Sat Jun 06 2009 Fyodor (fyodor(a)insecure.org)
- Removed changelog entries as SVN is a more authoritative source. Execute:
- svn log --username guest --password "" svn://svn.insecure.org/nmap/nmap.spec.in

