##
##  tacacs.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_dist  4.4beta2
%define       V_opkg  4.4b2

#   package information
Name:         tacacs
Summary:      TACACS+ Server
URL:          http://www.networkforums.net/
Vendor:       Cisco et al.
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        RADIUS
License:      BSD
Version:      %{V_opkg}
Release:      20080101

#   package options
%option       with_fsl     yes
%option       with_pam     no
%option       with_skey    no
%option       with_mysql   no
%option       with_pgsql   no
%option       with_radius  no

#   list of sources
Source0:      http://www.networkforums.net/downloads/tac_plus-%{V_dist}.tar.gz
Source1:      rc.tacacs
Source2:      fsl.tacacs
Patch0:       tacacs.patch
Patch1:       tacacs.patch.radius

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, make
PreReq:       OpenPKG, openpkg >= 20160101
%if "%{with_fsl}" == "yes"
BuildPreReq:  fsl
PreReq:       fsl
%endif
%if "%{with_pam}" == "yes"
BuildPreReq:  PAM
PreReq:       PAM
%endif
%if "%{with_skey}" == "yes"
BuildPreReq:  skey
PreReq:       skey
%endif
%if "%{with_mysql}" == "yes"
BuildPreReq:  mysql
PreReq:       mysql
%endif
%if "%{with_pgsql}" == "yes"
BuildPreReq:  postgresql, openssl
PreReq:       postgresql, openssl
%endif
%if "%{with_radius}" == "yes"
BuildPreReq:  libradius
PreReq:       libradius
%endif

%description
    This is a TACACS+ authentication server, derived from the original
    Cisco TACACS+ server implementation.

%track
    prog tacacs = {
        disabled
        comment   = "rse: no real chance for tracking AFAIK"
        version   = %{V_dist}
        url       = http://www.networkforums.net/modules.php?name=Downloads&d_op=viewdownload&cid=1
        regex     = .+
    }

%prep
    %setup -q -n tac_plus-%{V_dist}
    %patch -p0
    %patch -p0 -P 1
    chmod a+x install-sh
    %{l_shtool} subst \
        -e 's;/var/log/tac_acc.log;%{l_prefix}/var/tacacs/tac_plus.acc;' \
        sample-tac_plus.cfg

%build
    #   configure package
    export CC="%{l_cc}"
    export CFLAGS="%{l_cflags -O}"
    export CPPFLAGS="%{l_cppflags}"
    export LDFLAGS="%{l_ldflags}"
    export LIBS=""
%if "%{with_fsl}" == "yes"
    LDFLAGS="$LDFLAGS %{l_fsl_ldflags}"
    LIBS="$LIBS %{l_fsl_libs}"
%endif
%if "%{with_pam}" == "yes"
    CFLAGS="$CFLAGS -I`%{l_rc} --query pam_incdir`"
    LDFLAGS="$LDFLAGS -L`%{l_rc} --query pam_libdir`"
%endif
%if "%{with_pgsql}" == "yes"
    LIBS="$LIBS -lssl -lcrypto"
%endif
%if "%{with_radius}" == "yes"
    CFLAGS="$CFLAGS -DUSE_RADIUS"
    LIBS="$LIBS -lradius"
%endif
    ./configure \
        --prefix=%{l_prefix} \
%if "%{with_pam}" == "yes"
        --with-pam \
%else
        --without-pam \
%endif
%if "%{with_skey}" == "yes"
        --with-skey=%{l_prefix}/lib/libskey.a \
%else
        --without-skey \
%endif
%if "%{with_mysql}" == "yes" || "%{with_pgsql}" == "yes"
        --with-db \
%else
        --without-db \
%endif
%if "%{with_mysql}" == "yes"
        --with-mysql \
        --with-mysql-prefix=%{l_prefix} \
%else
        --without-mysql \
%endif
%if "%{with_pgsql}" == "yes"
        --with-pgsql \
        --with-pgsql-prefix=%{l_prefix} \
        --with-pgsql-include-dir=%{l_prefix}/include/postgresql \
        --with-pgsql-lib-dir=%{l_prefix}/lib \
%else
        --without-pgsql \
%endif
        --with-tacplus_pid=%{l_prefix}/var/tacacs

    #   build package
    %{l_make} %{l_mflags -O}

%install
    #   install package
    %{l_make} %{l_mflags} install \
        AM_MAKEFLAGS="DESTDIR=$RPM_BUILD_ROOT INSTALL=\"%{l_shtool} install\""

    #   post-adjust installation
    mv  $RPM_BUILD_ROOT%{l_prefix}/bin/generate_passwd \
        $RPM_BUILD_ROOT%{l_prefix}/bin/tacas_generate_passwd
    mv  $RPM_BUILD_ROOT%{l_prefix}/etc/tacacs/sample-tac_plus.cfg \
        $RPM_BUILD_ROOT%{l_prefix}/etc/tacacs/tac_plus.cfg
    strip $RPM_BUILD_ROOT%{l_prefix}/bin/* >/dev/null 2>&1 || true

%if "%{with_mysql}" == "yes" || "%{with_pgsql}" == "yes"
    #   install SQL database schema
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/share/tacacs
    %{l_shtool} install -c -m 644 \
        tac_plus.sql $RPM_BUILD_ROOT%{l_prefix}/share/tacacs/
%endif

    #   create pidfile and logfile directory
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/tacacs

    #   install run-command script
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE rc.tacacs} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

    #   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.tacacs} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/fsl/

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/etc/fsl/*' \
        '%config %{l_prefix}/etc/tacacs/tac_plus.cfg'

%files -f files

%clean

%post
%if "%{with_pam}" == "yes"
    #   add PAM configuration entry
    if [ $1 -eq 1 ]; then
        $RPM_INSTALL_PREFIX/sbin/pamtool --add --smart --name=tac_plus
    fi
%endif
    #   after upgrade, restart service
    [ $1 -eq 2 ] || exit 0
    eval `%{l_rc} tacacs status 2>/dev/null`
    [ ".$tacacs_active" = .yes ] && %{l_rc} tacacs restart
    exit 0

%preun
    [ $1 -eq 0 ] || exit 0
    #   before erase, stop service
    %{l_rc} tacacs stop 2>/dev/null
%if "%{with_pam}" == "yes"
    #   remove PAM configuration entry
    $RPM_INSTALL_PREFIX/sbin/pamtool --remove --smart --name=tac_plus
%endif
    exit 0

