##
##  proftpd.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_proftpd         1.3.6rc4
%define       V_mod_otp         0.9.1
%define       V_mod_shaper      0.6.5
%define       V_mod_sql_sqlite  0.2
%define       V_mod_sql_odbc    0.3.2
%define       V_mod_time        2.2.1
%define       V_mod_vroot       0.9.2
%define       V_mod_gss         1.3.6
%define       V_mod_sftp        0.9.6
%define       V_mod_sftp_sql    0.1
%define       V_mod_md5         0.3.5

#   package information
Name:         proftpd
Summary:      Professional FTP Daemon
URL:          http://www.proftpd.org/
Vendor:       The ProFTPD Project
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        BASE
Group:        FTP
License:      GPL
Version:      %{V_proftpd}
Release:      20170325

#   package options
%option       with_exec       no
%option       with_time       no
%option       with_vroot      no
%option       with_ifsession  no
%option       with_ctrls      no
%option       with_radius     no
%option       with_pam        no
%option       with_mysql      no
%option       with_pgsql      no
%option       with_sqlite     no
%option       with_odbc       no
%option       with_ldap       no
%option       with_skey       no
%option       with_opie       no
%option       with_kerberos   no
%option       with_ssl        no
%option       with_sftp       no
%option       with_md5        no

#   checking option conflicts
%if "%{with_mysql}" == "yes" && "%{with_pgsql}" == "yes"
    %{error:build-time options 'with_mysql' and 'with_pgsql' conflict}
%endif

#   list of sources
Source0:      ftp://ftp.proftpd.org/distrib/source/proftpd-%{V_proftpd}.tar.gz
Source1:      http://www.castaglia.net/proftpd/modules/proftpd-mod-otp-%{V_mod_otp}.tar.gz
Source2:      http://www.castaglia.net/proftpd/modules/proftpd-mod-shaper-%{V_mod_shaper}.tar.gz
Source3:      http://www.castaglia.net/proftpd/modules/proftpd-mod-sql-sqlite-%{V_mod_sql_sqlite}.tar.gz
Source4:      http://www.castaglia.net/proftpd/modules/proftpd-mod-sql-odbc-%{V_mod_sql_odbc}.tar.gz
Source5:      http://www.castaglia.net/proftpd/modules/proftpd-mod-time-%{V_mod_time}.tar.gz
Source6:      http://www.castaglia.net/proftpd/modules/proftpd-mod-vroot-%{V_mod_vroot}.tar.gz
Source7:      http://download.sourceforge.net/gssmod/mod_gss-%{V_mod_gss}.tar.gz
Source8:      http://www.castaglia.net/proftpd/modules/proftpd-mod-sftp-%{V_mod_sftp}.tar.gz
Source9:      http://www.castaglia.net/proftpd/modules/proftpd-mod-sftp-sql-%{V_mod_sftp_sql}.tar.gz
Source10:     http://www.castaglia.net/proftpd/modules/proftpd-mod-md5-%{V_mod_md5}.tar.gz
Source11:     proftpd.conf
Source12:     proftpd.msg.goaway
Source13:     proftpd.msg.login
Source14:     rc.proftpd
Patch0:       proftpd.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, make, gcc
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  ncurses, getopt
PreReq:       ncurses, getopt
%if "%{with_mysql}" == "yes"
BuildPreReq:  mysql
PreReq:       mysql
%endif
%if "%{with_pam}" == "yes"
BuildPreReq:  PAM
PreReq:       PAM
%endif
%if "%{with_pgsql}" == "yes"
BuildPreReq:  postgresql, openssl
PreReq:       postgresql, openssl
%endif
%if "%{with_sqlite}" == "yes"
BuildPreReq:  sqlite
PreReq:       sqlite
%endif
%if "%{with_odbc}" == "yes"
BuildPreReq:  ODBC
PreReq:       ODBC
%endif
%if "%{with_ldap}" == "yes"
BuildPreReq:  openldap, openssl
PreReq:       openldap, openssl
%endif
%if "%{with_skey}" == "yes"
BuildPreReq:  skey
PreReq:       skey
%endif
%if "%{with_opie}" == "yes"
BuildPreReq:  opie
PreReq:       opie
%endif
%if "%{with_kerberos}" == "yes"
BuildPreReq:  KERBEROS
PreReq:       KERBEROS
%endif
%if "%{with_ssl}" == "yes" || "%{with_sftp}" == "yes"
BuildPreReq:  openssl
PreReq:       openssl
%endif

%description
    ProFTPD grew out of the desire to have a secure and configurable
    FTP server, and out of a significant admiration of the Apache web
    server. There are currently a very limited number of FTP servers
    running on unix (or unix-like) hosts. The most commonly used server
    is probably wu-ftpd. While wu-ftpd provides excellent performance
    and is generally a good product, it lacks numerous features found
    in newer Win32 FTP servers, and has a poor security history. Many
    people, including the developers who work on ProFTPD have spent a
    great deal of time fixing bugs and hacking features into wu-ftpd.
    Unfortunately, it quickly became clear that a complete redesign was
    necessary in order to implement the configurability and features
    desired. ProFTPD is not a hack based on any other server, it's an
    independent source tree from the ground up. Click here for a small
    list of some of the sites ProFTPD powers -- many of them handling
    large volumes of traffic on a daily basis.

%track
    prog proftpd = {
        version   = %{V_proftpd}
        url       = ftp://ftp.proftpd.org/distrib/source/
        regex     = proftpd-(\d+\.\d+\.\d+(?:rc\d+|[a-z]?))\.tar\.bz2
    }
    prog proftpd:mod_shaper = {
        version   = %{V_mod_shaper}
        url       = http://www.castaglia.net/proftpd/
        regex     = proftpd-mod-shaper-(__VER__)\.tar\.gz
    }
    prog proftpd:mod_sql_sqlite = {
        version   = %{V_mod_sql_sqlite}
        url       = http://www.castaglia.net/proftpd/
        regex     = proftpd-mod-sql-sqlite-(__VER__)\.tar\.gz
    }
    prog proftpd:mod_sql_odbc = {
        version   = %{V_mod_sql_odbc}
        url       = http://www.castaglia.net/proftpd/
        regex     = proftpd-mod-sql-odbc-(__VER__)\.tar\.gz
    }
    prog proftpd:mod_time = {
        version   = %{V_mod_time}
        url       = http://www.castaglia.net/proftpd/
        regex     = proftpd-mod-time-(__VER__)\.tar\.gz
    }
    prog proftpd:mod_vroot = {
        version   = %{V_mod_vroot}
        url       = http://www.castaglia.net/proftpd/
        regex     = proftpd-mod-vroot-(__VER__)\.tar\.gz
    }
    prog proftpd:mod_gss = {
        version   = %{V_mod_gss}
        url       = http://sourceforge.net/projects/gssmod/files/
        regex     = mod_gss-(__VER__)\.tar\.gz
    }
    prog proftpd:mod_sftp = {
        version   = %{V_mod_sftp}
        url       = http://www.castaglia.net/proftpd/
        regex     = proftpd-mod-sftp-(__VER__)\.tar\.gz
    }
    prog proftpd:mod_sftp_sql = {
        version   = %{V_mod_sftp_sql}
        url       = http://www.castaglia.net/proftpd/
        regex     = proftpd-mod-sftp-sql-(__VER__)\.tar\.gz
    }
    prog proftpd:mod_md5 = {
        version   = %{V_mod_md5}
        url       = http://www.castaglia.net/proftpd/
        regex     = proftpd-mod-md5-(__VER__)\.tar\.gz
    }

%prep
    %setup -q
    %setup -q -D -T -a 1
    %setup -q -D -T -a 2
    %setup -q -D -T -a 3
    %setup -q -D -T -a 4
    %setup -q -D -T -a 5
    %setup -q -D -T -a 6
    %setup -q -D -T -a 7
    %setup -q -D -T -a 8
    %setup -q -D -T -a 9
    %setup -q -D -T -a 10
    %patch -p0

    %{l_shtool} subst \
        -e '/LINENO: error: C[+]* preprocessor/{N;N;N;N;s/.*/:/;}' \
        configure

    #   utils require to link with getopt
    %{l_shtool} subst \
        -e 's;\(\$(BUILD_FTPCOUNT_OBJS).*\);\1 %{l_ldflags} -lgetopt;g' \
        -e 's;\(\$(BUILD_FTPSHUT_OBJS).*\);\1 %{l_ldflags} -lgetopt;g' \
        -e 's;\(\$(BUILD_FTPTOP_OBJS).*\);\1 %{l_ldflags} -lgetopt;g' \
        -e 's;\(\$(BUILD_FTPWHO_OBJS).*\);\1 %{l_ldflags} -lgetopt;g' \
        Makefile.in
%if "%{with_mysql}" == "yes"
    libs=`mysql_config --libs`
    #   libmysqlclient may require more libraries
    %{l_shtool} subst \
        -e "s;\(-lmysqlclient\);$libs;g" \
        contrib/mod_sql_mysql.c
%endif

    #   libpq also requires openssl
    #   postgresql also doesn't provide its own include subdirectory
    %{l_shtool} subst \
        -e 's;\(-lpq\);\1 -lssl -lcrypto;g' \
        -e 's;^\(#include <\)pgsql/\(libpq-fe.h>.*\)$;\1postgresql/\2;' \
        contrib/mod_sql_postgres.c

    #   libldap also requires openssl
    %{l_shtool} subst \
        -e 's;\(-llber\);\1 -lssl -lcrypto;g' \
        contrib/mod_ldap.c

    #   prepare mod_otp module
%if "%{with_skey}" == "yes" || "%{with_opie}" == "yes"
    ( cd mod_otp
      CC="%{l_cc}" \
      CFLAGS="%{l_cflags -O}" \
      CPPFLAGS="%{l_cppflags}" \
      LDFLAGS="%{l_ldflags}" \
      ./configure \
%if "%{with_skey}" == "yes"
          --with-skey \
%endif
%if "%{with_opie}" == "yes"
          --with-opie \
%endif
          %{l_nil}
      cp mod_otp.[ch] ../contrib/
    ) || exit $?
%endif

    #   prepare mod_sql_odbc module
%if "%{with_odbc}" == "yes"
    ( cd mod_sql_odbc
      if [ -f %{l_prefix}/etc/unixodbc/odbc.ini ]; then
          ./configure --enable-unixodbc
      else
          ./configure --enable-iodbc
      fi
    ) || exit $?
%endif

    #   prepare optional add-on modules
%if "%{with_time}" == "yes"
    cp mod_time/mod_time.c contrib/
%endif
%if "%{with_vroot}" == "yes"
    cp mod_vroot/mod_vroot.c contrib/
%endif
%if "%{with_sqlite}" == "yes"
    cp mod_sql_sqlite/mod_sql_sqlite.c contrib/
%endif
%if "%{with_odbc}" == "yes"
    cp mod_sql_odbc/mod_sql_odbc.[ch] contrib/
%endif
%if "%{with_ctrls}" == "yes"
    cp mod_shaper/mod_shaper.c contrib/
%endif
%if "%{with_sftp}" == "yes"
    cp -r mod_sftp contrib/
%if "%{with_mysql}" == "yes" || "%{with_pgsql}" == "yes" || "%{with_sqlite}" == "yes" || "%{with_odbc}" == "yes"
    cp mod_sftp_sql/mod_sftp_sql.c contrib/
%endif
%endif
%if "%{with_md5}" == "yes"
    cp mod_md5/mod_md5.c contrib/
%endif

    #   prepare mod_gss module
%if "%{with_kerberos}" == "yes"
    ( cd mod_gss-*
      CC="%{l_cc}" \
      CFLAGS="%{l_cflags -O}" \
      CPPFLAGS="%{l_cppflags}" \
      LDFLAGS="%{l_ldflags}" \
      ./configure \
          --prefix=%{l_prefix} \
          `if [ -d %{l_prefix}/include/heimdal ]; then \
              echo "--enable-heimdal"; \
           else \
              echo "--enable-mit"; \
          fi`
      cp mod_gss.h ../include/
      cp mod_gss.c ../contrib/
    ) || exit $?
%endif

%build
    #   write shutdown message file to OpenPKG instance, not outside
    %{l_shtool} subst \
        -e 's;\(#define PR_SHUTMSG_PATH.*"\)\(/etc/\)\(shutmsg\)";\1%{l_prefix}\2proftpd/\3";' \
        include/default_paths.h

    #   configure the ProFTPd source tree
    MOD="mod_ratio:mod_readme"
    MOD_INC="%{l_prefix}/include"
    MOD_LIB="%{l_prefix}/lib"
%if "%{with_exec}" == "yes"
    MOD="$MOD:mod_exec"
%endif
%if "%{with_time}" == "yes"
    MOD="$MOD:mod_time"
%endif
%if "%{with_vroot}" == "yes"
    MOD="$MOD:mod_vroot"
%endif
%if "%{with_ifsession}" == "yes"
    MOD="$MOD:mod_ifsession"
%endif
%if "%{with_ctrls}" == "yes"
    MOD="$MOD:mod_shaper"
    MOD="$MOD:mod_ctrls_admin"
%endif
%if "%{with_radius}" == "yes"
    MOD="$MOD:mod_radius"
%endif
%if "%{with_mysql}" == "yes" || "%{with_pgsql}" == "yes" || "%{with_sqlite}" == "yes" || "%{with_odbc}" == "yes"
    MOD="$MOD:mod_sql"
%if "%{with_mysql}" == "yes"
    MOD="$MOD:mod_sql_mysql"
    MOD_INC=":%{l_prefix}/include/mysql"
%endif
%if "%{with_pgsql}" == "yes"
    MOD="$MOD:mod_sql_postgres"
    MOD_INC=":%{l_prefix}/include/postgresql"
%endif
%if "%{with_sqlite}" == "yes"
    MOD="$MOD:mod_sql_sqlite"
%endif
%if "%{with_odbc}" == "yes"
    MOD="$MOD:mod_sql_odbc"
%endif
%endif
%if "%{with_ldap}" == "yes"
    MOD="$MOD:mod_ldap"
%endif
%if "%{with_skey}" == "yes" || "%{with_opie}" == "yes"
    MOD="$MOD:mod_otp"
%endif
%if "%{with_kerberos}" == "yes"
    MOD="$MOD:mod_gss"
%endif
%if "%{with_ssl}" == "yes"
    MOD="$MOD:mod_tls"
%endif
%if "%{with_sftp}" == "yes"
    MOD="$MOD:mod_sftp"
%if "%{with_mysql}" == "yes" || "%{with_pgsql}" == "yes" || "%{with_sqlite}" == "yes" || "%{with_odbc}" == "yes"
    MOD="$MOD:mod_sftp_sql"
%endif
%endif
%if "%{with_md5}" == "yes"
    MOD="$MOD:mod_md5"
%endif
    export CC="%{l_cc}"
    export CFLAGS="%{l_cflags -O} -Wno-unused-but-set-variable"
    export CPPFLAGS="%{l_cppflags ncurses}"
    export LDFLAGS="%{l_ldflags}"
    export LIBS="-lgetopt"
%if "%{with_pam}" == "yes"
    CPPFLAGS="$CPPFLAGS -I`%{l_rc} --query pam_incdir`"
    LDFLAGS="$LDFLAGS -L`%{l_rc} --query pam_libdir`"
%endif
%if "%{with_kerberos}" == "yes"
    CPPFLAGS="$CPPFLAGS `krb5-config --cflags gssapi`"
    LIBS="$LIBS `krb5-config --libs gssapi`"
%endif
    ./configure \
        --prefix=%{l_prefix} \
        --sysconfdir=%{l_prefix}/etc/proftpd \
        --localstatedir=%{l_prefix}/var/proftpd \
        --with-modules="$MOD" \
        --with-includes="$MOD_INC" \
        --with-libraries="$MOD_LIB" \
        --enable-buffer-size=1024 \
%if "%{with_ctrls}" == "yes"
        --enable-ctrls \
%endif
%if "%{with_pam}" == "yes"
        --enable-auth-pam
%else
        --disable-auth-pam
%endif

    #   build ProFTPd programs
    %{l_make} %{l_mflags}

%install

    #   make sure the "install" procedure does not try
    #   to perform explicit ownership assignments
    %{l_shtool} subst -v \
        -e 's;-o $(INSTALL_USER) -g $(INSTALL_GROUP);;g' \
        -e 's;chown;true;g' \
        Makefile Make.rules

    #   perform the "install" procedure while redirecting
    #   it to the temporarily install area
    %{l_make} %{l_mflags} \
        install-proftpd install-utils install-man \
        prefix=$RPM_BUILD_ROOT%{l_prefix} \
        bindir=$RPM_BUILD_ROOT%{l_prefix}/bin \
        sbindir=$RPM_BUILD_ROOT%{l_prefix}/sbin \
        sysconfdir=$RPM_BUILD_ROOT%{l_prefix}/etc/proftpd \
        localstatedir=$RPM_BUILD_ROOT%{l_prefix}/var/proftpd \
        rundir=$RPM_BUILD_ROOT%{l_prefix}/var/proftpd

    #   strip installation
    rm -f $RPM_BUILD_ROOT%{l_prefix}/sbin/in.proftpd

    #   install more stuff manually
    %{l_shtool} mkdir -f -p -m 755 $RPM_BUILD_ROOT%{l_prefix}/share/proftpd
    %{l_shtool} install -c -m 644 doc/faq.html \
        $RPM_BUILD_ROOT%{l_prefix}/share/proftpd/faq.html
    %{l_shtool} install -c -m 644 doc/Configuration.html \
        $RPM_BUILD_ROOT%{l_prefix}/share/proftpd/cfg.html

    #   extended installation with own stuff
    l_pam="#"
%if "%{with_pam}" == "yes"
    l_pam=""
%endif
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        -e "s;@l_pam@;${l_pam};g" \
        %{SOURCE proftpd.conf} $RPM_BUILD_ROOT%{l_prefix}/etc/proftpd/
    %{l_shtool} install -c -m 644 \
        %{SOURCE proftpd.msg.goaway} \
        %{SOURCE proftpd.msg.login} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/proftpd/
    %{l_shtool} install -c -m 644 \
        %{SOURCE proftpd.msg.login} \
        $RPM_BUILD_ROOT%{l_prefix}/share/proftpd/.msg.login
    %{l_shtool} install -c -m 644 \
        %{SOURCE proftpd.msg.goaway} \
        $RPM_BUILD_ROOT%{l_prefix}/share/proftpd/.msg.goaway
    %{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.proftpd} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

    #   determine the package ingredients
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %attr(0600,%{l_susr},%{l_sgrp}) %{l_prefix}/etc/proftpd/proftpd.conf' \
        '%config %{l_prefix}/etc/proftpd/proftpd.msg.*'

%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=proftpd
    fi
%endif

    #   after upgrade, restart service
    [ $1 -eq 2 ] || exit 0
    eval `%{l_rc} proftpd status 2>/dev/null`
    [ ".$proftpd_active" = .yes ] && %{l_rc} proftpd restart
    exit 0

%preun
    #   before erase, stop service and remove log files
    [ $1 -eq 0 ] || exit 0
    %{l_rc} proftpd stop 2>/dev/null
    rm -f $RPM_INSTALL_PREFIX/var/proftpd/*.log* >/dev/null 2>&1 || true
    rm -f $RPM_INSTALL_PREFIX/var/proftpd/*.pid  >/dev/null 2>&1 || true
%if "%{with_pam}" == "yes"
    #   remove PAM configuration entry
    $RPM_INSTALL_PREFIX/sbin/pamtool --remove --smart --name=proftpd
%endif
    exit 0

