##
##  honeyd.spec -- OpenPKG RPM Package Specification
##  Copyright (c) 2000-2025 OpenPKG Project <http://openpkg.org/>
##
##  Permission to use, copy, modify, and distribute this software for
##  any purpose with or without fee is hereby granted, provided that
##  the above copyright notice and this permission notice appear in all
##  copies.
##
##  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
##  SUCH DAMAGE.
##

#   package version
%define       V_honeyd    1.5b
%define       V_libdnsres 0.1a

#   package information
Name:         honeyd
Summary:      Creates a Virtual Host on Network
URL:          http://www.honeyd.org/
Vendor:       Niels Provos
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Security
License:      BSD
Version:      %{V_honeyd}
Release:      20080101

#   package options
%option       with_fsl  yes
%option       with_gui  no

#   list of sources
Source0:      http://www.citi.umich.edu/u/provos/honeyd/honeyd-%{V_honeyd}.tar.gz
Source1:      http://www.citi.umich.edu/u/provos/honeyd/contrib/mael/ftp.sh
Source2:      http://www.citi.umich.edu/u/provos/honeyd/contrib/mael/pop3.sh
Source3:      http://www.citi.umich.edu/u/provos/honeyd/contrib/mael/smtp.sh
Source4:      http://www.citi.umich.edu/u/provos/papers/honeyd-eabstract.pdf
Source5:      http://www.citi.umich.edu/u/provos/papers/honeyd-eabstract.ps
Source6:      http://www.citi.umich.edu/u/provos/honeyd/ch01-results/1/honeydGUI.tar.gz
Source7:      honey
Source8:      svcs.sh
Source9:      rc.honeyd
Source10:     fsl.honeyd
Source11:     honeyd.conf
Source12:     cdefs.h
Source13:     setenv.h
Source14:     setenv.c
Source15:     vasprintf.c
Source16:     vasprintf.h
Source17:     http://www.monkey.org/~provos/libdnsres-%{V_libdnsres}.tar.gz
Patch0:       honeyd.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, make
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  libdnet, libpcap, libevent, libedit, zlib, pcre
PreReq:       libdnet, libpcap, libevent, libedit, zlib, pcre
%if "%{with_fsl}" == "yes"
BuildPreReq:  fsl
PreReq:       fsl
%endif
%if "%{with_gui}" == "yes"
PreReq:       java, JAVA-JDK
%endif

%description
    Honeyd is a small daemon that creates virtual hosts on a network.
    The hosts can be configured to run arbitrary services, and their
    TCP personality can be adapted so that they appear to be running
    certain versions of operating systems. Honeyd enables a single host
    to claim multiple addresses on a LAN for network simulation. It is
    possible to ping the virtual machines, or to traceroute them. Any
    type of service on the virtual machine can be simulated according to
    a simple configuration file. Instead of simulating a service, it is
    also possible to proxy it to another machine. The package arpd will
    most certainly be useful as well, although it is not a technical
    requirement for this package.

%track
    prog honeyd:honeyd = {
        version   = %{V_honeyd}
        url       = http://www.citi.umich.edu/u/provos/honeyd/
        regex     = honeyd-(__VER__)\.tar\.gz
    }
    prog honeyd:libdnsres = {
        version   = %{V_libdnsres}
        url       = http://www.monkey.org/~provos/libdnsres/
        regex     = libdnsres-(__VER__)\.tar\.gz
    }

%prep
    %setup -q
    %setup -q -D -T -a 17
    %patch -p0
    %{l_shtool} subst \
        -e 's;AF_LOCAL;AF_UNIX;' \
        *.c
    %{l_shtool} subst \
        -e 's;^\(honeyd_SOURCES *=[^\\]*\);\1 setenv.c vasprintf.c;' \
        -e 's;^\(honeyd_OBJECTS *=[^\\]*\);\1 setenv.o vasprintf.o ;' \
        Makefile.in
    cp -f %{SOURCE setenv.c} .
    cp -f %{SOURCE setenv.h} .
    cp -f %{SOURCE vasprintf.c} .
    cp -f %{SOURCE vasprintf.h} .
    cp -f %{SOURCE cdefs.h} .
    %{l_shtool} subst \
        -e 's;/var/run/honeyd.pid;%{l_prefix}/var/honeyd/honeyd.pid;' \
        honeyd.h
    %{l_shtool} subst \
        -e 's/\(user_target=no\)/\1; pic_mode=no;/' \
        -e 's;test "*$\(hardcode_into_libs\)"* *\([!=]*\) *"*\([a-zA-Z_][a-zA-Z_]*\)"*;test ".$\1" \2 ".$\3";g' \
        -e 's;test "*$\(build_libtool_need_lc\)"* *\([!=]*\) *"*\([a-zA-Z_][a-zA-Z_]*\)"*;test ".$\1" \2 ".$\3";g' \
        ltmain.sh
    case "%{l_platform -t}" in
        *-sunos* )
            %{l_shtool} subst \
                -e 's;-levent;-levent -lsocket -lnsl;g' \
                configure
            %{l_shtool} subst \
                -e 's;\(\$(honeydctl_LDADD)\);\1 -lsocket -lnsl;g' \
                Makefile.in
            ;;
    esac

%build
    ( cd libdnsres-%{V_libdnsres}
      CC="%{l_cc}" \
      CFLAGS="%{l_cflags -O}" \
      CPPFLAGS="%{l_cppflags}" \
      ./configure \
          --with-libevent=%{l_prefix} \
          --disable-shared
      %{l_make} %{l_mflags}
      ln .libs/libdnsres.a .
    ) || exit $?
    CC="%{l_cc}" \
    CFLAGS="%{l_cflags -O}" \
    CPPFLAGS="%{l_cppflags} -DREPLACE_GETOPT" \
    LDFLAGS="%{l_fsl_ldflags}" \
    LIBS="%{l_fsl_libs}" \
    ./configure \
        --prefix=%{l_prefix} \
        --with-libevent=%{l_prefix} \
        --with-libdnet=%{l_prefix} \
        --with-libdnsres=`pwd`/libdnsres-%{V_libdnsres} \
        --with-libpcre=%{l_prefix} \
        --without-python
    %{l_make} %{l_mflags}

%install

    #   create directories
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d \
        $RPM_BUILD_ROOT%{l_prefix}/etc/honeyd \
        $RPM_BUILD_ROOT%{l_prefix}/lib/honeyd \
        $RPM_BUILD_ROOT%{l_prefix}/var/honeyd \
        $RPM_BUILD_ROOT%{l_prefix}/share/honeyd \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/honeyd

    #   install files
    %{l_make} %{l_mflags} install DESTDIR=$RPM_BUILD_ROOT
    %{l_shtool} install -c -m 750 %{l_value -s -a} \
        -e 's;^log=/\(.*/\)*\(.*\)-.*\.log;log=%{l_prefix}/var/honeyd/\2.log;g' \
        -e 's;^\(host=\).*;\1`%{l_shtool} echo -e %h`;g' \
        -e 's;^\(domain=\).*;\1`%{l_shtool} echo -e %d | cut -c2-`;g' \
        -e 's; gawk ; awk ;g' \
        %{SOURCE ftp.sh} \
        %{SOURCE pop3.sh} \
        %{SOURCE smtp.sh} \
        %{SOURCE svcs.sh} \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/honeyd/
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE honeyd.conf} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/honeyd/
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE rc.honeyd} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/
    %{l_shtool} install -c -m 644 \
        %{SOURCE honeyd-eabstract.ps} \
        $RPM_BUILD_ROOT%{l_prefix}/share/honeyd/honeyd.ps
    %{l_shtool} install -c -m 644 \
        %{SOURCE honeyd-eabstract.pdf} \
        $RPM_BUILD_ROOT%{l_prefix}/share/honeyd/honeyd.pdf

    #   optionally install the Java GUI
%if "%{with_gui}" == "yes"
    %{l_tar} zxf %{SOURCE honeydGUI.tar.gz}
    mv -f honeydGUI/exec $RPM_BUILD_ROOT%{l_prefix}/lib/honeyd/javagui
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE honey} \
        $RPM_BUILD_ROOT%{l_prefix}/bin/
%endif

    #   install OSSP fsl configuration
    %{l_shtool} mkdir -f -p -m 755 $RPM_BUILD_ROOT%{l_prefix}/etc/fsl
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE fsl.honeyd} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/fsl/

    #   remove unwanted files
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/include
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/lib/honeyd
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/share/honeyd/README
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/share/honeyd/config.sample

    #   determine file list
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %attr(0750,%{l_susr},%{l_mgrp}) %{l_prefix}/var/honeyd' \
        '%config %{l_prefix}/etc/fsl/fsl.honeyd' \
        '%config %attr(0750,%{l_musr},%{l_mgrp}) %{l_prefix}/etc/honeyd/honeyd.conf'

%files -f files

%clean

%pre
    #   before upgrade, save status and stop service
    [ $1 -eq 2 ] || exit 0
    eval `%{l_rc} honeyd status 2>/dev/null | tee %{l_tmpfile}`
    %{l_rc} honeyd stop 2>/dev/null
    exit 0

%post
    if [ $1 -eq 1 ]; then
        #   display final hints on initial installation
        ( echo "Before starting Honey daemon, please set the configuration variable"
          echo "\"honeyd_if\" in $RPM_INSTALL_PREFIX/etc/rc.conf to the name of the"
          echo "used network interface."
        ) | %{l_rpmtool} msg -b -t notice
    fi
    if [ $1 -eq 2 ]; then
        #   after upgrade, restore status
        eval `cat %{l_tmpfile}`; rm -f %{l_tmpfile}
        [ ".$honeyd_active" = .yes ] && %{l_rc} honeyd start
    fi
    exit 0

%preun
    #   before erase, stop service and remove log files
    [ $1 -eq 0 ] || exit 0
    %{l_rc} honeyd stop 2>/dev/null
    rm -f $RPM_INSTALL_PREFIX/var/honeyd/*.log* >/dev/null 2>&1 || true
    exit 0

