##
##  openssh.spec -- OpenPKG RPM Specification
##  Copyright (c) 2000-2003 Cable & Wireless Deutschland GmbH
##  Copyright (c) 2000-2003 The OpenPKG Project <http://www.openpkg.org/>
##  Copyright (c) 2000-2003 Ralf S. Engelschall <rse@engelschall.com>
##
##  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 versions
%define       V_base       3.5
%define       V_portable   p1
%define       V_chroot     3.5

#   package information
Name:         openssh
Summary:      Secure Shell (SSH)
URL:          http://www.openssh.com/
Vendor:       The OpenBSD Project
Packager:     The OpenPKG Project
Distribution: OpenPKG [CORE]
Group:        Security
License:      BSD
Version:      %{V_base}%{V_portable}
Release:      20030103

#   package options
%option       with_pam     no
%option       with_pcre    no
%option       with_skey    no
%option       with_x11     %{l_test -p x11}
%option       with_chroot  no

#   list of sources
Source0:      ftp://ftp.openssh.com/pub/OpenBSD/OpenSSH/portable/openssh-%{version}.tar.gz
Source1:      rc.openssh
Source2:      fsl.openssh
Source3:      sshd_config
Source4:      ssh_config
Source5:      ssh-askpass
Source6:      ssh-keyman
Source7:      ssh-keyman.1
Source8:      ssh-keyman.pod
Source9:      http://chrootssh.sourceforge.net/patches/osshChroot-%{V_chroot}.diff

#   build information
Prefix:       %{l_prefix}
BuildRoot:    %{l_buildroot}
BuildPreReq:  OpenPKG, openpkg >= 20030103, fsl, perl, openssl, zlib
PreReq:       OpenPKG, openpkg >= 20030103, fsl
%if "%{with_pam}" == "yes"
BuildPreReq:  PAM
PreReq:       PAM
%endif
%if "%{with_pcre}" == "yes"
BuildPreReq:  pcre
%endif
%if "%{with_skey}" == "yes"
BuildPreReq:  skey
%endif
%if "%{with_x11}" == "yes"
BuildPreReq:  X11
PreReq:       X11
%endif
AutoReq:      no
AutoReqProv:  no

%description
    Secure Shell (SSH) is a facility for logging into a remote machine
    and for remotely executing commands on a remote machine. It is
    intended to replace rlogin(1) and rsh(1), and provide secure
    encrypted communications between two untrusted hosts over an
    insecure network. X11 connections and arbitrary TCP/IP ports can
    also be forwarded over the secure channel. OpenSSH is OpenBSD's
    rework of the last free version of SSH, bringing it up to date in
    terms of security and features, as well as removing all patented
    algorithms to separate libraries (OpenSSL).

%prep
    #   unpack distribution
    %setup -q

    #   optionally apply chroot(2) patch
%if "%{with_chroot}" == "yes"
    cd openssh-%{version}
    %{l_patch} -p1 <%{SOURCE osshChroot-%{V_chroot}.diff}
%endif

    #   prevent in advance "make install" from running sshd
    %{l_shtool} subst \
        -e 's;^\(install:.*\)check-config\(.*\)$;\1 \2;' \
        Makefile.in

%build
    #   configure package
    inc="%{l_cppflags}"
    lib="%{l_ldflags}"
%if "%{with_pam}" == "yes"
    inc="$inc -I`%{l_prefix}/etc/rc --query pam_incdir`"
    lib="$lib -L`%{l_prefix}/etc/rc --query pam_libdir`"
%endif
    CC="%{l_cc}" \
    CFLAGS="%{l_cflags -O} `%{l_prefix}/bin/fsl-config --all --cflags` $inc" \
    LDFLAGS="%{l_ldflags} `%{l_prefix}/bin/fsl-config --all --ldflags` $lib" \
    LIBS="`%{l_prefix}/bin/fsl-config --all --libs`" \
    ./configure \
        --prefix=%{l_prefix} \
        --sbindir=%{l_prefix}/bin \
        --mandir=%{l_prefix}/man \
        --infodir=%{l_prefix}/info \
        --sysconfdir=%{l_prefix}/etc/openssh \
        --libexecdir=%{l_prefix}/libexec/openssh \
        --localstatedir=%{l_prefix}/var/openssh \
        --with-pid-dir=%{l_prefix}/var/openssh \
        --with-ssl-dir=%{l_prefix} \
%if "%{with_pcre}" == "yes"
        --with-pcre=%{l_prefix} \
%endif
%if "%{with_skey}" == "yes"
        --with-skey=%{l_prefix} \
%endif
%if "%{with_pam}" == "yes"
        --with-pam \
%else
        --without-pam \
%endif
%if "%{with_x11}" == "yes"
        --with-xauth="`%{l_prefix}/etc/rc --query x11_bindir`/xauth" \
%endif
        --with-md5-passwords \
        --with-zlib=%{l_prefix} \
        --with-ipv4-default \
        --without-smartcard \
        --without-kerberos4 \
        --disable-suid-ssh \
        --without-rsh \
        --with-mantype=man \
        --with-default-path=%{l_prefix}/bin:/bin:/usr/bin:/usr/local/bin \
        --with-privsep-user=%{l_nusr} \
        --with-privsep-path=%{l_prefix}/var/openssh/empty

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

%install
    rm -rf $RPM_BUILD_ROOT

    #   perform standard installation procedure of OpenSSH
    %{l_make} %{l_mflags} install DESTDIR=$RPM_BUILD_ROOT

    #   strip down installation tree
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/share
    rm -f $RPM_BUILD_ROOT%{l_prefix}/bin/slogin
    rm -f $RPM_BUILD_ROOT%{l_prefix}/man/man1/slogin.1
    strip $RPM_BUILD_ROOT%{l_prefix}/bin/* 2> /dev/null || true
    strip $RPM_BUILD_ROOT%{l_prefix}/libexec/openssh/* 2> /dev/null || true

    #   install ssh-askpass wrapper
    %{l_shtool} mkdir -f -p -m 755 $RPM_BUILD_ROOT%{l_prefix}/libexec/openssh
    %{l_shtool} install -c -m 755 -e 's;@l_prefix@;%{l_prefix};g' \
        %{SOURCE ssh-askpass} $RPM_BUILD_ROOT%{l_prefix}/libexec/openssh/

    #   make sure the state directory exists
    %{l_shtool} mkdir -f -p -m 755 \
	    $RPM_BUILD_ROOT%{l_prefix}/var/openssh \
	    $RPM_BUILD_ROOT%{l_prefix}/var/openssh/empty

    #   install addons
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/bin \
        $RPM_BUILD_ROOT%{l_prefix}/man/man1
    %{l_shtool} install -c -m 755 -e 's;@l_prefix@;%{l_prefix};g' \
        %{SOURCE ssh-keyman} $RPM_BUILD_ROOT%{l_prefix}/bin/
    %{l_shtool} install -c -m 644 -e 's;@l_prefix@;%{l_prefix};g' \
        %{SOURCE ssh-keyman.1} $RPM_BUILD_ROOT%{l_prefix}/man/man1/

    #   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 -e 's;@l_prefix@;%{l_prefix};g' \
        -e 's;@l_musr@;%{l_musr};g' -e 's;@l_mgrp@;%{l_mgrp};g' \
        %{SOURCE rc.openssh} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

    #   install fsl configuration file
    %{l_shtool} mkdir -f -p -m 755 $RPM_BUILD_ROOT%{l_prefix}/etc/fsl
    %{l_shtool} install -c -m 644 -e 's;@l_prefix@;%{l_prefix};g' \
        %{SOURCE fsl.openssh} $RPM_BUILD_ROOT%{l_prefix}/etc/fsl/

    #   install reasonable ssh server and client configuration files
    %{l_shtool} mkdir -f -p -m 755 $RPM_BUILD_ROOT%{l_prefix}/etc/openssh
    %{l_shtool} install -c -m 644 \
        -e 's;@l_prefix@;%{l_prefix};g' \
        -e 's;@l_x11forwarding@;%{with_x11};' \
        %{SOURCE sshd_config} $RPM_BUILD_ROOT%{l_prefix}/etc/openssh/
    %{l_shtool} install -c -m 644 -e 's;@l_prefix@;%{l_prefix};g' \
        %{SOURCE ssh_config} $RPM_BUILD_ROOT%{l_prefix}/etc/openssh/

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/etc/openssh/*' \
        '%attr(4711,root,%{l_mgrp}) %{l_prefix}/libexec/openssh/ssh-keysign' \
        '%dir %attr(700,root,root) %{l_prefix}/var/openssh/empty' \
        '%config %{l_prefix}/etc/fsl/fsl.openssh' \
        '%not %dir %{l_prefix}/etc/fsl'

%files -f files

%clean
    rm -rf $RPM_BUILD_ROOT

%post
    #   generate server RSA1 (SSHv1) key
    if [ ! -f "$RPM_INSTALL_PREFIX/etc/openssh/ssh_host_key" -o \
         ! -s "$RPM_INSTALL_PREFIX/etc/openssh/ssh_host_key" ] ; then
        $RPM_INSTALL_PREFIX/bin/ssh-keygen -t rsa1 -b 2048 \
            -f $RPM_INSTALL_PREFIX/etc/openssh/ssh_host_key \
            -N '' -C `hostname` 1>&2
    fi

    #   generate server RSA (SSHv2) key
    if [ ! -f "$RPM_INSTALL_PREFIX/etc/openssh/ssh_host_rsa_key" -o \
         ! -s "$RPM_INSTALL_PREFIX/etc/openssh/ssh_host_rsa_key" ] ; then
        $RPM_INSTALL_PREFIX/bin/ssh-keygen -t rsa -b 2048 \
            -f $RPM_INSTALL_PREFIX/etc/openssh/ssh_host_rsa_key \
            -N '' -C `hostname` 1>&2
    fi

    #   generate server DSA (SSHv2) key
    if [ ! -f "$RPM_INSTALL_PREFIX/etc/openssh/ssh_host_dsa_key" -o \
         ! -s "$RPM_INSTALL_PREFIX/etc/openssh/ssh_host_dsa_key" ] ; then
        $RPM_INSTALL_PREFIX/bin/ssh-keygen -t dsa -b 2048 \
            -f $RPM_INSTALL_PREFIX/etc/openssh/ssh_host_dsa_key \
            -N '' -C `hostname` 1>&2
    fi

    #   add PAM configuration entry
%if "%{with_pam}" == "yes"
    $RPM_INSTALL_PREFIX/sbin/pamtool --add --smart --name=openssh
%endif

%preun
    #   remove PAM configuration entry
%if "%{with_pam}" == "yes"
    $RPM_INSTALL_PREFIX/sbin/pamtool --remove --smart --name=openssh
%endif

