##
##  sendmail.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 information
Name:         sendmail
Summary:      Mail Transfer Agent
URL:          http://www.sendmail.org/
Vendor:       Eric Allman
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        BASE
Group:        Mail
License:      BSD
Version:      8.18.1
Release:      20240204

#   package options
%option       with_fsl       yes
%option       with_milter    yes
%option       with_ssl       no
%option       with_sasl      no
%option       with_ldap      no
%option       with_mysql     no
%option       with_ipv6      no
%option       with_socketmap no

#   list of sources
Source0:      ftp://ftp.sendmail.org/pub/sendmail/sendmail.%{version}.tar.gz
Source1:      sendmail.etc.txt
Source2:      sendmail.cf.txt
Source3:      fsl.sendmail
Source4:      rc.sendmail
Patch0:       sendmail.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, m4, gcc, make
PreReq:       OpenPKG, openpkg >= 20160101, m4, make, perl, procmail
BuildPreReq:  db >= 4.1.25
PreReq:       db >= 4.1.25
%if "%{with_fsl}" == "yes"
BuildPreReq:  fsl
PreReq:       fsl
%endif
%if "%{with_ssl}" == "yes"
BuildPreReq:  openssl
PreReq:       openssl
%endif
%if "%{with_sasl}" == "yes"
BuildPreReq:  sasl, openssl
PreReq:       sasl, openssl
%endif
%if "%{with_ldap}" == "yes"
BuildPreReq:  openldap, openssl
PreReq:       openldap, openssl
%endif
%if "%{with_mysql}" == "yes"
BuildPreReq:  mysql
PreReq:       mysql
%endif
Provides:     MTA
Conflicts:    exim, postfix, ssmtp

%description
    Sendmail is the most popular Mail Transfer Agent (MTA). This copy
    contains features specific to the OpenPKG distribution including
    fsl(3) logging rather than syslog(3) and optional STARTTLS,
    SMTPAUTH, Milter and LDAP support.

%track
    prog sendmail = {
        version   = %{version}
        url       = ftp://ftp.sendmail.org/pub/sendmail/
        regex     = sendmail.(\d+\.\d+\.\d+)\.tar\.gz
    }

%prep
    #   unpack standard tarballs
    %setup -q
    %patch -p0

    #   make install process unprivileged
    %{l_shtool} subst \
        -e 's;ln -s *;$(LN) $(LNOPTS) $(DESTDIR);' \
        -e 's;chown;true;g' \
        -e 's;chgrp;true;g' \
        Makefile */Makefile */Makefile.m4 */*/Makefile
    %{l_shtool} subst \
        -e 's;\$chown;true;g' \
        -e 's;chgrp;true;g' \
        -e 's;chmod;true;g' \
        devtools/bin/install.sh

    #   add own M4 stuff to Sendmail configuration set
    for name in `grep "^<file" %{SOURCE sendmail.cf.txt} | sed -e 's;^.*name=";;' -e 's;".*$;;'`; do
        %{l_shtool} mkdir -f -p -m 755 `echo "$name" | sed -e 's;/[^/]*$;;'`
        pattern=`echo "$name" | sed -e 's;/;\\\\/;g'`
        (echo ""; cat %{SOURCE sendmail.cf.txt}; echo "") |\
            sed -e "1,/^<file name=\"$pattern\">/d" -e "/<\/file>/,\$d" >$name
    done

    #   override platform-specific definitions
    ( echo "#ifdef _PATH_SENDMAIL"
      echo "#undef _PATH_SENDMAIL"
      echo "#endif"
    ) >sed.include
    %{l_shtool} subst \
        -e 's;\(define *SMRSH_CMDDIR *\).*;\1 "%{l_prefix}/var/sendmail/sm.bin";g' \
        -e 's;\(define *_PATH_SENDMAIL *\).*;\1 "%{l_prefix}/sbin/sendmail";g' \
        -e '/# *include *<paths.h>.*/r sed.include' \
        -e '/undef *_PATH_SENDMAILPID/d' \
        include/sendmail/pathnames.h include/sm/conf.h
    rm -f sed.include

%build
    #   indicate with a plus sign that we use a patched version
    %{l_shtool} subst \
        -e 's;\(Version\[\][^"]*\)"[^"]*";\1"%{version}+";' \
        sendmail/version.c

    #   correctly specify missing realtime functions library
    extlibs="%{l_fsl_libs} -ldb"
    case "%{l_platform -t}" in
        *-sunos* ) extlibs="$extlibs -lrt" ;;
    esac

    #   configure Sendmail
    (   echo "define(\`confCC', \`%{l_cc} %{l_cflags}')dnl"
        echo "define(\`confMAPDEF', \`-DMAP_REGEX')dnl"
        echo "define(\`confSBINOWN', \`%{l_musr}')dnl"
        echo "define(\`confSBINGRP', \`%{l_mgrp}')dnl"
        echo "define(\`confUBINOWN', \`%{l_musr}')dnl"
        echo "define(\`confUBINGRP', \`%{l_mgrp}')dnl"
        echo "define(\`confMANOWN', \`%{l_musr}')dnl"
        echo "define(\`confMANGRP', \`%{l_mgrp}')dnl"
        echo "define(\`confEBINDIR', \`%{l_prefix}/libexec/sendmail')dnl"
        echo "define(\`confHFDIR', \`%{l_prefix}/etc/sendmail')dnl"
        echo "define(\`confHFFILE', \`sendmail.help')dnl"
        echo "define(\`confINCLUDEDIR', \`%{l_prefix}/include/sendmail')dnl"
        echo "define(\`confINSTALL_RAWMAN', \`1')dnl"
        echo "define(\`confLIBDIR', \`%{l_prefix}/lib/sendmail')dnl"
        echo "define(\`confMANROOT', \`%{l_prefix}/man/cat')dnl"
        echo "define(\`confMANROOTMAN', \`%{l_prefix}/man/man')dnl"
        echo "define(\`confMBINDIR', \`%{l_prefix}/sbin')dnl"
        echo "define(\`confSBINDIR', \`%{l_prefix}/sbin')dnl"
        echo "define(\`confSHAREDLIBDIR', \`%{l_prefix}/lib')dnl"
        echo "define(\`confSTDIR', \`%{l_prefix}/var/sendmail')dnl"
        echo "define(\`confSTFILE', \`sendmail.stat')dnl"
        echo "define(\`confUBINDIR', \`%{l_prefix}/bin')dnl"
        echo "define(\`confLNOPTS', \`-f')dnl"
        echo "define(\`confFORCE_RMAIL', \`yes')dnl"
        echo "APPENDDEF(\`confMSP_QUEUE_DIR',\`%{l_prefix}/var/sendmail/clientmqueue')dnl"
        echo "APPENDDEF(\`confENVDEF', \`-D_PATH_SENDMAILCF=\\\"%{l_prefix}/etc/sendmail/sendmail.cf\\\"')dnl"
        echo "APPENDDEF(\`confENVDEF', \`-D_PATH_SENDMAILPID=\\\"%{l_prefix}/var/sendmail/sendmail.pid\\\"')dnl"
        echo "APPENDDEF(\`confENVDEF', \`-D_DIR_SENDMAILCF=\\\"%{l_prefix}/etc/sendmail/\\\"')dnl"
        echo "APPENDDEF(\`confENVDEF', \`-DNEWDB')dnl"
        echo "APPENDDEF(\`confINCDIRS', \`%{l_cppflags}')dnl"
        echo "APPENDDEF(\`confLIBDIRS', \`%{l_ldflags}')dnl"
        echo "APPENDDEF(\`confLIBS', \`$extlibs')dnl"
        echo "define(\`confSTDIO_TYPE', \`portable')dnl"
        echo "APPENDDEF(\`confENVDEF', \`-DDNSMAP')dnl"
%if "%{with_ssl}" == "yes"
        echo "APPENDDEF(\`conf_sendmail_ENVDEF', \`-DSTARTTLS')dnl"
        echo "APPENDDEF(\`conf_sendmail_ENVDEF', \`-DHASURANDOMDEV')dnl"
        echo "APPENDDEF(\`conf_sendmail_LIBS', \`-lssl -lcrypto')dnl"
%endif
%if "%{with_sasl}" == "yes"
        echo "APPENDDEF(\`confENVDEF', \`-DSASL=1')dnl"
        echo "APPENDDEF(\`confINCDIRS', \`%{l_cppflags sasl .}')dnl"
        echo "APPENDDEF(\`confLIBS', \`-lsasl2 -lcrypto')dnl"
%endif
%if "%{with_milter}" == "yes"
        echo "APPENDDEF(\`conf_sendmail_ENVDEF', \`-DMILTER=1')dnl"
%else
        echo "APPENDDEF(\`conf_sendmail_ENVDEF', \`-DMILTER=0')dnl"
%endif
%if "%{with_ldap}" == "yes"
        echo "APPENDDEF(\`confMAPDEF', \`-DLDAPMAP')dnl"
        echo "APPENDDEF(\`confENVDEF', \`-DSM_CONF_LDAP_MEMFREE=1')dnl"
        echo "APPENDDEF(\`confLIBS', \`-lldap -llber -lssl -lcrypto')dnl"
%endif
%if "%{with_mysql}" == "yes"
        echo "APPENDDEF(\`confMAPDEF',\`-DMYSQLMAP')dnl"
        echo "APPENDDEF(\`confINCDIRS',\`%{l_cppflags mysql .}')dnl"
        echo "APPENDDEF(\`confLIBS',\`%{l_ldflags mysql .} -lmysqlclient -lz -lm')dnl"
%endif
%if "%{with_ipv6}" == "yes"
        echo "APPENDDEF(\`conf_sendmail_ENVDEF', \`-DNETINET6')dnl"
%endif
        echo "APPENDDEF(\`conf_sendmail_ENVDEF', \`-DPICKY_HELO_CHECK')dnl"
%if "%{with_socketmap}" == "yes"
        echo "APPENDDEF(\`conf_sendmail_ENVDEF', \`-DSOCKETMAP')dnl"
%endif
    ) >devtools/Site/site.config.m4

    #   build Sendmail package
    %{l_make} %{l_mflags}

%install

    #   manually create filesystem hierarchy, because Sendmail's
    #   install target doesn't properly create it itself.
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/sendmail \
        $RPM_BUILD_ROOT%{l_prefix}/bin \
        $RPM_BUILD_ROOT%{l_prefix}/sbin \
        $RPM_BUILD_ROOT%{l_prefix}/lib \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/sendmail \
        $RPM_BUILD_ROOT%{l_prefix}/man \
        $RPM_BUILD_ROOT%{l_prefix}/man/man1 \
        $RPM_BUILD_ROOT%{l_prefix}/man/man5 \
        $RPM_BUILD_ROOT%{l_prefix}/man/man8 \
        $RPM_BUILD_ROOT%{l_prefix}/man/cat1 \
        $RPM_BUILD_ROOT%{l_prefix}/man/cat5 \
        $RPM_BUILD_ROOT%{l_prefix}/man/cat8

    #   install Sendmail
    %{l_make} %{l_mflags} install DESTDIR=$RPM_BUILD_ROOT \
       INSTALL=`pwd`/devtools/bin/install.sh \
       MAILDIR=%{l_prefix}/etc/sendmail
    mv $RPM_BUILD_ROOT%{l_prefix}/bin/rmail \
       $RPM_BUILD_ROOT%{l_prefix}/sbin/rmail

    #   remember the Sendmail M4 stuff for re-configuration
    ( %{l_tar} cf $RPM_BUILD_ROOT%{l_prefix}/etc/sendmail/cf.tar cf ) || exit $?
    %{l_gzip} $RPM_BUILD_ROOT%{l_prefix}/etc/sendmail/cf.tar
    %{l_shtool} install -c -m 644 cf/README \
        $RPM_BUILD_ROOT%{l_prefix}/etc/sendmail/cf.README
    %{l_shtool} install -c -m 755 \
      -e 's;^#!.*;#!%{l_prefix}/bin/perl;' \
        contrib/re-mqueue.pl $RPM_BUILD_ROOT%{l_prefix}/sbin/re-mqueue
    %{l_shtool} install -c -m 755 \
      -e 's;^#!.*;#!%{l_prefix}/bin/perl;' \
        contrib/qtool.pl $RPM_BUILD_ROOT%{l_prefix}/sbin/qtool
    %{l_shtool} install -c -m 755 \
      -e 's;^#!.*;#!%{l_prefix}/bin/perl;' \
        contrib/qtool.8 $RPM_BUILD_ROOT%{l_prefix}/man/man8/qtool.8

    #   install a reasonable default configuration
    l_hostname=`%{l_shtool} echo -e %h`
    l_domainname=`%{l_shtool} echo -e %d | cut -c2-`
    l_ostype=bsd4.4
    case "%{l_platform -t}" in
        *-freebsd[6-9]*              ) l_ostype=freebsd6 ;;
        *-freebsd5*                  ) l_ostype=freebsd5 ;;
        *-freebsd4*                  ) l_ostype=freebsd4 ;;
        *-freebsd*                   ) l_ostype=bsd4.4   ;;
        *-sunos5.[8-9] | *-sunos5.10 ) l_ostype=solaris8 ;;
        *-sunos*                     ) l_ostype=solaris2 ;;
        *-linux*                     ) l_ostype=linux    ;;
    esac
    for name in `grep "^<file" %{SOURCE sendmail.etc.txt} | sed -e 's;^.*name=";;' -e 's;".*$;;'`; do
        (echo ""; cat %{SOURCE sendmail.etc.txt}; echo "") |\
            sed -e "1,/^<file name=\"$name\">/d" -e "/<\/file>/,\$d" >$name
        %{l_shtool} install -c -m 644 %{l_value -s -a} \
            -e "s;@l_hostname@;${l_hostname};g" \
            -e "s;@l_domainname@;${l_domainname};g" \
            -e "s;@l_ostype@;${l_ostype};g" \
            $name $RPM_BUILD_ROOT%{l_prefix}/etc/sendmail/
    done

    #   install the 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.sendmail} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

    #   create important files which have to be present from the beginning
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/sendmail/sm.bin \
        $RPM_BUILD_ROOT%{l_prefix}/var/sendmail/mqueue \
        $RPM_BUILD_ROOT%{l_prefix}/var/sendmail/mqueue/.hoststat
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/sendmail/clientmqueue \
        $RPM_BUILD_ROOT%{l_prefix}/var/sendmail/clientmqueue/.hoststat
    touch $RPM_BUILD_ROOT%{l_prefix}/var/sendmail/sendmail.log

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

    #   adjust installation tree
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/man/cat*
    rm -f $RPM_BUILD_ROOT%{l_prefix}/etc/sendmail/submit.cf
    strip $RPM_BUILD_ROOT%{l_prefix}/bin/*              2>/dev/null || true
    strip $RPM_BUILD_ROOT%{l_prefix}/sbin/*             2>/dev/null || true
    strip $RPM_BUILD_ROOT%{l_prefix}/libexec/sendmail/* 2>/dev/null || true

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/etc/fsl/fsl.sendmail' \
        '%attr(2755,%{l_susr},%{l_rgrp})        %{l_prefix}/sbin/sendmail' \
        '%attr(4755,%{l_susr},%{l_mgrp})        %{l_prefix}/bin/hoststat' \
        '%attr(4755,%{l_susr},%{l_mgrp})        %{l_prefix}/bin/mailq' \
        '%attr(4755,%{l_susr},%{l_mgrp})        %{l_prefix}/bin/newaliases' \
        '%attr(4755,%{l_susr},%{l_mgrp})        %{l_prefix}/bin/purgestat' \
        '%attr(4755,%{l_susr},%{l_mgrp})        %{l_prefix}/bin/vacation' \
        '%attr(4755,%{l_susr},%{l_mgrp})        %{l_prefix}/libexec/sendmail/smrsh' \
        '%attr(755,%{l_susr},%{l_mgrp})         %{l_prefix}/etc/sendmail' \
        '%config                                %{l_prefix}/etc/sendmail/*' \
        '%config %attr(644,%{l_susr},%{l_mgrp}) %{l_prefix}/etc/sendmail/ca.crt' \
        '%config %attr(644,%{l_susr},%{l_mgrp}) %{l_prefix}/etc/sendmail/sendmail.crt' \
        '%config %attr(600,%{l_susr},%{l_mgrp}) %{l_prefix}/etc/sendmail/sendmail.key' \
        '%attr(700,%{l_susr},%{l_mgrp})         %{l_prefix}/var/sendmail/mqueue' \
        '%attr(700,%{l_susr},%{l_mgrp})         %{l_prefix}/var/sendmail/mqueue/.hoststat' \
        '%attr(770,%{l_rusr},%{l_rgrp})         %{l_prefix}/var/sendmail/clientmqueue' \
        '%attr(750,%{l_rusr},%{l_rgrp})         %{l_prefix}/var/sendmail/clientmqueue/.hoststat' \
        '%attr(644,%{l_susr},%{l_mgrp})         %{l_prefix}/var/sendmail/sendmail.log'

%files -f files

%clean

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

%post
    if [ $1 -eq 1 ]; then
        ( cd $RPM_INSTALL_PREFIX/etc/sendmail; %{l_make} all >/dev/null 2>&1 || true ) || true
    elif [ $1 -gt 1 ]; then
        ( cd $RPM_INSTALL_PREFIX/etc/sendmail; %{l_make} clean all >/dev/null 2>&1 || true ) || true
        $RPM_INSTALL_PREFIX/bin/openpkg rc sendmail reload >/dev/null 2>&1 || true
    fi
    if [ $1 -eq 2 ]; then
        #   after upgrade, restore status
        eval `cat %{l_tmpfile}`; rm -f %{l_tmpfile}
        [ ".$sendmail_active" = .yes ] && %{l_rc} sendmail start
    fi
    exit 0

%preun
    if [ $1 -eq 0 ]; then
        #   before erase, stop service and remove residual files
        %{l_rc} sendmail stop 2>/dev/null
        rm -f $RPM_INSTALL_PREFIX/var/sendmail/*.log* >/dev/null 2>&1 || true
        ( cd $RPM_INSTALL_PREFIX/etc/sendmail/;
          %{l_make} distclean >/dev/null 2>&1 || true
        ) || true
        d=$RPM_INSTALL_PREFIX/var/sendmail/mqueue/.hoststat/*
        if [ ."$d" != . ]; then
            rm -rf $d
        fi
        d=$RPM_INSTALL_PREFIX/var/sendmail/clientmqueue/.hoststat/*
        if [ ."$d" != . ]; then
            rm -rf $d
        fi
    fi
    exit 0

