File apache2.spec of Package apache2

# spec file for package apache2%{psuffix}
# Copyright (c) 2021 SUSE LLC
# All modifications and additions to the file contributed by third parties
# remain the property of their copyright owners, unless otherwise agreed
# upon. The license for this file, and modifications and additions to the
# file, is the same license as for the pristine package itself (unless the
# license for the pristine package is not an Open Source License, in which
# case the license is the MIT License). An "Open Source License" is a
# license that conforms to the Open Source Definition (Version 1.9)
# published by the Open Source Initiative.

# Please submit bugfixes or comments via

%global upstream_name   httpd
%global testsuite_name  %{upstream_name}-framework
%global tversion        svn1878849
%global flavor          @BUILD_FLAVOR@%{nil}
%define mpm             %{nil}
%if "%{flavor}" == "prefork" || "%{flavor}" == "test_prefork"
%define mpm             prefork
%if "%{flavor}" == "worker" || "%{flavor}" == "test_worker"
%define mpm             worker
%if "%{flavor}" == "event" || "%{flavor}" == "test_event"
%define mpm             event
%define test            0
%define unittest        0
%if "%{flavor}" == "test_prefork" || "%{flavor}" == "test_worker" || "%{flavor}" == "test_event" || "%{flavor}" == "test_devel" || "%{flavor}" == "test_main"
%define test            1
%if "%{flavor}" == "test_prefork" || "%{flavor}" == "test_worker" || "%{flavor}" == "test_event"
%define unittest        1
%if "%{mpm}" == "prefork"
%define mpm_alt_prio    10
%if "%{mpm}" == "worker"
%define mpm_alt_prio    20
%if "%{mpm}" == "event"
%define mpm_alt_prio    30
%define default_mpm     prefork
%define suse_maintenance_mmn  0
%define apache_mmn      %(test -s %{SOURCE0} && \
                          { echo -n apache_mmn_; bzcat %{SOURCE0} | \
                            awk '/^#define MODULE_MAGIC_NUMBER_MAJOR/ {printf "%d", $3}'; } || \
                          echo apache_mmn_notfound)
%define static_modules  unixd systemd
%define dynamic_modules authz_core access_compat actions alias allowmethods asis auth_basic auth_digest \\\
                        auth_form authn_anon authn_core authn_dbd authn_dbm authn_file authn_socache authnz_ldap \\\
                        authnz_fcgi authz_core authz_dbd authz_dbm authz_groupfile authz_host authz_owner authz_user \\\
                        autoindex bucketeer buffer cache cache_disk cache_socache case_filter case_filter_in cgid cgi \\\
                        charset_lite data dav dav_fs dav_lock dbd deflate brotli dialup dir dumpio echo env expires \\\
                        ext_filter file_cache filter headers heartmonitor http2 imagemap include info lbmethod_bybusyness \\\
                        lbmethod_byrequests lbmethod_bytraffic lbmethod_heartbeat ldap log_config log_debug log_forensic \\\
                        logio lua macro mime mime_magic negotiation optional_fn_export optional_fn_import \\\
                        optional_hook_export optional_hook_import proxy proxy_ajp proxy_balancer proxy_connect \\\
                        proxy_express proxy_fcgi proxy_fdpass proxy_ftp proxy_hcheck proxy_html proxy_http proxy_http2 \\\
                        proxy_scgi proxy_uwsgi proxy_wstunnel ratelimit reflector remoteip reqtimeout request rewrite \\\
                        sed session session_cookie session_crypto session_dbd setenvif slotmem_plain slotmem_shm \\\
                        socache_dbm socache_memcache socache_redis socache_shmcb speling ssl status substitute suexec \\\
                        unique_id userdir usertrack version vhost_alias watchdog xml2enc
%define support_bin     ab check_forensic dbmmanage find_directives gensslcert htdbm htdigest htpasswd httxt2dbm \\\
                        log_server_status logresolve split-logfile
%define support_sbin    apachectl htcacheclean fcgistarter rotatelogs

%define platform_string Linux/SUSE
%define httpduser       wwwrun
%define httpdgroup      www

%define datadir         /srv/www
%define htdocsdir       %{datadir}/htdocs
%define manualdir       %{_datadir}/apache2/manual
%define errordir        %{_datadir}/apache2/error
%define iconsdir        %{_datadir}/apache2/icons
%define cgidir          %{datadir}/cgi-bin
%define localstatedir   %{_localstatedir}/lib/apache2
%define runtimedir      /run
%define proxycachedir   %{_localstatedir}/cache/apache2
%define logfiledir      %{_localstatedir}/log/apache2
%define sysconfdir      %{_sysconfdir}/apache2
%define includedir      %{_includedir}/apache2
%if "%{mpm}" != ""
%define libexecdir      %{_libdir}/apache2-%{mpm}
%define libexecdir      %{_libdir}/apache2
%define installbuilddir %{_datadir}/apache2/build
%define userdir         public_html

%define suexec_safepath %{_prefix}/local/bin:%{_prefix}/bin:/bin

%define psuffix       %{nil}
%if "%{flavor}" != ""
%define psuffix      -%{flavor}

%if 0%{?suse_version} >= 1500
%define use_firewalld 1
%define use_firewalld 0
%if 0%{?suse_version} >= 1500 || 0%{?is_opensuse}
%define build_http2 1
%define build_http2 0

Name:           apache2%{psuffix}
Version:        2.4.48
Release:        0
Summary:        The Apache HTTPD Server
License:        Apache-2.0
Group:          Productivity/Networking/Web/Servers
# essential sources
Source2:        apache2.keyring
Source3:        apache2.service
Source4:        apache2@.service
# Add file to take mtime from it in prep section
Source6:        apache2.changes
Source10:       apache2-ssl-dirs.tar.bz2
# test
# svn checkout httpd-framework
Source20:       %{testsuite_name}-%{tversion}.tar.bz2
Source21:       apache2-mod_example.c
# integration settings
Source30:       sysconfig.apache2
Source31:       logrotate.apache2
Source32:       permissions.apache2
Source33:       firewalld.apache2
Source34:       firewalld-ssl.apache2
Source35:       susefirewall.apache2
Source36:       susefirewall-ssl.apache2
# scripts
Source100:      apache2-a2enmod
Source101:      apache2-a2enflag
Source102:      apache2-systemd-ask-pass
Source103:      apache2-start_apache2
Source104:      apache2-script-helpers
# additional support
Source130:      apache2-gensslcert
Source131:      apache2-check_forensic
Source132:      apache2-find_directives
# configuration
Source150:      apache2-httpd.conf
Source151:      apache2-errors.conf
Source152:      apache2-default-server.conf
Source153:      apache2-listen.conf
Source154:      apache2-manual.conf
Source155:      apache2-mod_autoindex-defaults.conf
Source156:      apache2-mod_info.conf
Source157:      apache2-mod_log_config.conf
Source158:      apache2-mod_mime-defaults.conf
Source159:      apache2-mod_status.conf
Source160:      apache2-mod_userdir.conf
Source161:      apache2-server-tuning.conf
Source163:      apache2-ssl-global.conf
Source164:      apache2-mod_usertrack.conf
Source165:      apache2-mod_reqtimeout.conf
Source166:      apache2-loadmodule.conf
Source167:      apache2-global.conf
Source168:      apache2-mod_cgid-timeout.conf
Source169:      apache2-protocols.conf
Source190:      apache2-vhost.template
Source191:      apache2-vhost-ssl.template
# READMEs and other documentation
Source200:      apache2-README-access_compat.txt
Source201:      apache2-README-instances.txt
Source202:      apache2-README-configuration.txt
# layout of system dirs configuration, may be upstreamed
Patch0:         apache2-system-dirs-layout.patch
# apachectl is frontend for start_apache2, suse specific
Patch1:         apache2-apachectl.patch
# [bnc#210904] perhaps to be upstreamed
Patch2:         apache2-logresolve-tmp-security.patch
# [bnc#690734] TODO, to be upstreamed
Patch3:         apache2-LimitRequestFieldSize-limits-headers.patch
# [fate317766] backport of an upstream commit
Patch4:         apache2-HttpContentLengthHeadZero-HttpExpectStrict.patch
Patch100:       apache-test-application-xml-type.patch
# PATCH:  /test_ssl_var_lookup?SSL_SERVER_SAN_DNS_0 returns <build-host-name>
#         /test_ssl_var_lookup?SSL_SERVER_SAN_OTHER_dnsSRV_0 _https.<build-host-name>
# but Apache::Test::vars()->{servername} returns 'localhost' instead of <build-host-name>
# (see $san_dns and $san_dnssrv variables in t/ssl/varlookup.t)
# even if in live system I do not experience this inconsistency, let's turn off
# these variables from the test
Patch101:       apache-test-turn-off-variables-in-ssl-var-lookup.patch
BuildRequires:  apache-rpm-macros-control
#Since 2.4.7 the event MPM requires apr 1.5.0 or later.
BuildRequires:  apr-devel >= 1.5.0
BuildRequires:  apr-util-devel
BuildRequires:  automake
# for basic testing
BuildRequires:  curl
BuildRequires:  db-devel
%if %{use_firewalld}
BuildRequires:  firewall-macros
BuildRequires:  libbrotli-devel
%if %{build_http2}
BuildRequires:  pkgconfig(libnghttp2) >= 1.2.1
BuildRequires:  libcap-devel
BuildRequires:  libxml2-devel
BuildRequires:  lua-devel
BuildRequires:  openldap2-devel
BuildRequires:  openssl-devel >= 0.9.8a
BuildRequires:  pcre-devel
BuildRequires:  pkgconfig
BuildRequires:  systemd-rpm-macros
BuildRequires:  xz
BuildRequires:  zlib-devel
BuildRequires:  pkgconfig(libsystemd)
BuildRequires:  pkgconfig(systemd)
# SECTION test requirements
%if %{test}
BuildRequires:  apache-rpm-macros
BuildRequires:  apache2
BuildRequires:  apache2-devel
BuildRequires:  apache2-manual
BuildRequires:  apache2-utils
%if "%{flavor}" == "test_prefork"
BuildRequires:  apache2-prefork
%if "%{flavor}" == "test_worker"
BuildRequires:  apache2-worker
%if "%{flavor}" == "test_event"
BuildRequires:  apache2-event
%if %{unittest}
BuildRequires:  mod_php_any
# perl-doc is assumed by t/filter/case.t (/usr/lib/perl5/*/pod/perlsub.pod)
BuildRequires:  perl-doc
BuildRequires:  perl(Crypt::SSLeay)
BuildRequires:  perl(DateTime)
BuildRequires:  perl(Devel::CoreStack)
BuildRequires:  perl(Devel::Symdump)
BuildRequires:  perl(Digest::MD5)
BuildRequires:  perl(HTML::HeadParser)
BuildRequires:  perl(HTML::Parser)
BuildRequires:  perl(HTML::Tagset)
BuildRequires:  perl(HTTP::DAV)
BuildRequires:  perl(LWP)
BuildRequires:  perl(LWP::Protocol::https)
BuildRequires:  perl(MIME::Base64)
BuildRequires:  perl(Net::Cmd)
BuildRequires:  perl(URI)
%if %{build_http2}
BuildRequires:  perl(AnyEvent)
BuildRequires:  perl(Protocol::HTTP2::Client)
BuildRequires:  netcfg
%if "%{mpm}" != ""
Provides:       apache2-MPM
%if "%{flavor}" == ""
Requires:       %{_sysconfdir}/mime.types
Requires:       apache2-MPM
Suggests:       apache2-%{default_mpm}
Recommends:     apache2-utils
Requires:       logrotate
Provides:       %{apache_mmn}
Provides:       http_daemon
Provides:       httpd
Provides:       suse_maintenance_mmn_%{suse_maintenance_mmn}
Obsoletes:      apache2-example-pages
Requires(pre):  group(www)
Requires(pre):  user(wwwrun)
%if "%{flavor}" == "utils"
Requires:       /usr/bin/which
Recommends:     w3m
%if "%{flavor}" == "devel"
Requires:       apache2 = %{version}
Requires:       apr-devel
Requires:       apr-util-devel
Requires:       gcc
Provides:       httpd-devel = %{version}
%if "%{flavor}" == "manual"
Provides:       apache2-doc = %{version}
Obsoletes:      apache2-doc <= %{version}
%if "%{mpm}" != ""
Requires(pre):  permissions
Requires(post): %fillup_prereq
Requires(post): grep
Requires(post): update-alternatives
%if %{test} || "%{flavor}" == "manual"
BuildArch:      noarch

The Apache HTTP Server Project is an effort to develop and
maintain an open-source HTTP server for modern operating
systems including UNIX and Windows. The goal of this project
is to provide a secure, efficient and extensible server that
provides HTTP services in sync with the current HTTP standards.

%setup -q -n %{upstream_name}-%{version} -a20
%patch0 -p1
%patch1 -p1
%patch2 -p1
%patch3 -p1
%patch4 -p1
%patch100 -p1
%patch101 -p1


%if ! %{test} && "%{flavor}" != "manual"
echo "================== BUILDING [%{flavor}] flavor"
# configuration (autoreconf, configure) common for all flavors,
# except test and manual

autoreconf --force --install --verbose

# replace PLATFORM string that's seen in the "Server:" header
sed -i -e 's,(" PLATFORM "),(%{platform_string}),' server/core.c
# use mtime of .changes for build time
CHANGES_MTIME=`stat --format="%%y" %{SOURCE6}`
sed -i -e "s/__DATE__ \" \" __TIME__;/\"$CHANGES_MTIME\";/" server/buildmark.c

export CFLAGS="%{optflags} -fPIC -Wall
cat > config.layout <<-EOF
# SUSE Layout
<Layout SUSE>
Prefix:         %{datadir}
    exec_prefix:   %{_prefix}
    bindir:        %{_bindir}
    sbindir:       %{_sbindir}
    libdir:        %{_libdir}
    libexecdir:    %{libexecdir}
    mandir:        %{_mandir}
    sysconfdir:    %{sysconfdir}
    datadir:       %{datadir}
    installbuilddir: %{installbuilddir}
    errordir:      %{errordir}
    iconsdir:      %{iconsdir}
    htdocsdir:     %{htdocsdir}
    manualdir:     %{manualdir}
    cgidir:        %{cgidir}
    includedir:    %{includedir}
    localstatedir: %{localstatedir}
    runtimedir:    %{runtimedir}
    logfiledir:    %{logfiledir}
    proxycachedir: %{proxycachedir}
# %%configure (e. g --libexecdir) switches override
# layout paths (use layout because some of variables
# there does not exist as configure switches), so
# override them back
%configure \
        --prefix=%{datadir} \
        --libexecdir=%{libexecdir} \
        --includedir=%{includedir} \
        --sysconfdir=%{sysconfdir} \
        --enable-layout=SUSE \
        --libexecdir=%{libexecdir} \
%if "%{mpm}" != ""
        --with-program-name=httpd-%{mpm} \
        --with-apr=%{_bindir}/apr-1-config \
        --with-apr-util=%{_bindir}/apu-1-config \
%if "%{mpm}" != ""
        --with-mpm="%{mpm}" \
%if "%{mpm}" == "worker" || "%{mpm}" == "event"
%ifarch %ix86
%ifnarch i386
                --enable-nonportable-atomics=yes \
        --enable-exception-hook \
        --with-pcre \
        --enable-pie \
        --enable-mods-shared=all \
        --enable-mods-static="%{static_modules}" \
        --enable-ssl=shared \
        --disable-isapi \
        --enable-deflate \
        --enable-brotli \
        --enable-echo \
        --enable-filter \
        --enable-ext-filter \
        --enable-charset-lite \
        --enable-file-cache \
        --enable-logio \
        --enable-dumpio \
        --enable-bucketeer \
        --enable-case_filter \
        --enable-case_filter_in \
        --enable-imagemap \
%if %{build_http2}
        --enable-http2 \
        --with-ldap \
        --enable-ldap \
        --enable-authnz_ldap \
        --enable-authnz-fcgi \
        --enable-proxy \
        --enable-proxy-connect \
        --enable-proxy-ftp \
        --enable-proxy-http \
%if %{build_http2}
        --enable-proxy-http2 \
        --enable-proxy-fdpass \
        --enable-cache \
        --enable-disk-cache \
        --enable-mem-cache \
        --enable-version \
        --enable-dav-lock \
        --enable-authn-alias \
        --enable-optional-hook-export \
        --enable-optional-hook-import \
        --enable-optional-fn-import \
        --enable-optional-fn-export \
        --enable-suexec \
        --with-suexec-bin=%{_sbindir}/suexec \
        --with-suexec-caller=%{httpduser} \
        --with-suexec-docroot=%{datadir} \
        --with-suexec-logfile=%{logfiledir}/suexec.log \
        --with-suexec-userdir=%{userdir} \
        --with-suexec-uidmin=96 \
        --with-suexec-gidmin=96 \
        --with-suexec-safepath=%{suexec_safepath} \

# MPMs build
%if "%{mpm}" != ""
sed -i "s:httpd-%{mpm}.conf:httpd.conf:" include/ap_config_auto.h
make %{?_smp_mflags}

# main package build
%if "%{flavor}" == ""
pushd support
make %{?_smp_flags} suexec

# utils build
%if "%{flavor}" == "utils"
pushd support
make %{?_smp_mflags}
cp %{SOURCE130} gensslcert
cp %{SOURCE131} check_forensic
cp %{SOURCE132} find_directives


%if ! %{test}
echo "================== INSTALLING [%{flavor}] flavor"

# MPMs install
%if "%{mpm}" != ""
# install httpd binary
make DESTDIR=%{buildroot} program-install
# install modules
pushd modules
make DESTDIR=%{buildroot} install -j1
# install alternative links (httpd binary, modules)
mkdir -p %{buildroot}%{_sysconfdir}/alternatives
ln -sf %{_sysconfdir}/alternatives/httpd %{buildroot}%{_sbindir}/httpd
mkdir -p %{buildroot}%{_libdir}/apache2/
for module in %{dynamic_modules}; do
  if [ -e %{buildroot}%{libexecdir}/mod_$ ]; then
    ln -sf %{_sysconfdir}/alternatives/mod_$ %{buildroot}%{_libdir}/apache2/mod_$

# main packge install
%if "%{flavor}" == ""
mkdir -p %{buildroot}%{logfiledir} \
         %{buildroot}%{proxycachedir} \
         %{buildroot}%{localstatedir} \

mkdir -p %{buildroot}/%{_libexecdir}
cat > %{buildroot}/%{_libexecdir}/apache2_MMN <<-EOF
echo %{apache_mmn}

mkdir -p %{buildroot}%{_sysconfdir}/logrotate.d
install -m 644 %{SOURCE31} %{buildroot}/%{_sysconfdir}/logrotate.d/apache2

make DESTDIR=%{buildroot} install-suexec -j1

mkdir -p %{buildroot}%{_unitdir}
install -m 644 %{SOURCE3} %{buildroot}%{_unitdir}/apache2.service
install -m 644 %{SOURCE4} %{buildroot}%{_unitdir}/apache2@.service
install -m 644 %{SOURCE5} %{buildroot}%{_unitdir}/
ln -sf service %{buildroot}/%{_sbindir}/rcapache2

install -m 700 %{SOURCE102} %{buildroot}%{_sbindir}/apache2-systemd-ask-pass

install -m 755 %{SOURCE100} %{buildroot}%{_sbindir}/a2enmod
ln -s a2enmod %{buildroot}%{_sbindir}/a2dismod
install -m 755 %{SOURCE101} %{buildroot}%{_sbindir}/a2enflag
ln -s a2enflag %{buildroot}%{_sbindir}/a2disflag
install -m 744 %{SOURCE103} %{buildroot}%{_sbindir}/start_apache2
mkdir -p %{buildroot}/%{_datadir}/apache2/
install -m 644 %{SOURCE104} %{buildroot}/%{_datadir}/apache2/script-helpers

%if %{use_firewalld}
install -D -m 644 %{SOURCE33} %{buildroot}%{_prefix}/lib/firewalld/services/apache2.xml
install -D -m 644 %{SOURCE34} %{buildroot}%{_prefix}/lib/firewalld/services/apache2-ssl.xml
install -d %{buildroot}%{_sysconfdir}/sysconfig/SuSEfirewall2.d/services/
install -m 644 %{SOURCE35} %{buildroot}%{_sysconfdir}/sysconfig/SuSEfirewall2.d/services/apache2
install -m 644 %{SOURCE36} %{buildroot}%{_sysconfdir}/sysconfig/SuSEfirewall2.d/services/apache2-ssl

mkdir -p %{buildroot}%{_sysconfdir}
mkdir -p %{buildroot}%{_sysconfdir}/apache2/sysconfig.d

mkdir -p %{buildroot}/%{_fillupdir}
install -m 644 %{SOURCE30} %{buildroot}%{_fillupdir}/sysconfig.apache2

mkdir -p %{buildroot}%{sysconfdir}
mkdir -p %{buildroot}%{sysconfdir}/conf.d
for c in default-server.conf \
         errors.conf \
         global.conf \
         httpd.conf \
         listen.conf \
         loadmodule.conf \
         mod_autoindex-defaults.conf \
         mod_info.conf \
         mod_log_config.conf \
         mod_mime-defaults.conf \
         mod_status.conf \
         mod_userdir.conf \
         mod_usertrack.conf \
         server-tuning.conf \
         mod_reqtimeout.conf \
         mod_cgid-timeout.conf \
         ssl-global.conf \
        install -m 644 %{_sourcedir}/apache2-$c %{buildroot}/%{sysconfdir}/$c
cat > %{buildroot}/%{sysconfdir}/uid.conf <<-EOF
        User %{httpduser}
        Group %{httpdgroup}
tar -xjf %{SOURCE10} -C %{buildroot}/%{sysconfdir}
# fixup libdir
%if "%{_lib}" != "lib64"
sed -e 's/lib64/%{_lib}/' -i \
  %{buildroot}/%{sysconfdir}/loadmodule.conf \
mkdir %{buildroot}/%{sysconfdir}/vhosts.d
install -m 644 %{SOURCE190} %{buildroot}/%{sysconfdir}/vhosts.d/vhost.template
install -m 644 %{SOURCE191} %{buildroot}/%{sysconfdir}/vhosts.d/vhost-ssl.template
install -m 644 docs/conf/charset.conv %{buildroot}/%{sysconfdir}/
install -m 644 docs/conf/magic %{buildroot}/%{sysconfdir}/
ln -sf ../mime.types %{buildroot}/%{sysconfdir}/mime.types

make DESTDIR=%{buildroot} install-icons
make DESTDIR=%{buildroot} install-error
make DESTDIR=%{buildroot} sysconfdir=%{_docdir}/apache2/conf install-conf

cp -r docs/server-status %{buildroot}%{_datadir}/apache2/lua-server-status

mkdir -p %{buildroot}%{_mandir}/man8/
install -D -m 644 docs/man/suexec.8 %{buildroot}%{_mandir}/man8/
install -D -m 644 docs/man/httpd.8 %{buildroot}%{_mandir}/man8/

cp %{SOURCE200} README-access_compat.txt
cp %{SOURCE201} README-instances.txt
cp %{SOURCE202} README-configuration.txt

# utils install
%if "%{flavor}" == "utils"
> utils-filelist
for utility in %{support_bin}; do
  install -D -m 755 support/$utility %{buildroot}%{_bindir}/$utility
  echo %{_bindir}/$utility >> utils-filelist
  if [ -f docs/man/$utility.1 ]; then
    install -D -m 644 docs/man/$utility.1 %{buildroot}%{_mandir}/man1/$utility.1
    echo %{_mandir}/man1/$utility.1.* >> utils-filelist
for utility in %{support_sbin}; do
  install -D -m 755 support/$utility %{buildroot}%{_sbindir}/$utility
  echo %{_sbindir}/$utility >> utils-filelist
  if [ -f docs/man/$utility.8 ]; then
    install -D -m 644 docs/man/$utility.8 %{buildroot}%{_mandir}/man8/$utility.8
    echo %{_mandir}/man8/$utility.8.* >> utils-filelist

# devel install
%if "%{flavor}" == "devel"
mkdir -p %{buildroot}/%{_bindir}
install -D -m 755 support/apxs %{buildroot}%{_bindir}/
mkdir -p %{buildroot}/%{_mandir}/man1/
install -D -m 644 docs/man/apxs.1 %{buildroot}%{_mandir}/man1/
make DESTDIR=%{buildroot} install-build -j1
make DESTDIR=%{buildroot} install-include -j1

# manual install
%if "%{flavor}" == "manual"
mkdir -p %{buildroot}%{manualdir}
cp -ra docs/manual/* %{buildroot}%{manualdir}
mkdir -p %{buildroot}/%{sysconfdir}/conf.d/
install -m 644 %{SOURCE154} %{buildroot}/%{sysconfdir}/conf.d/manual.conf



# test basic function of just built MPMs
%if ! %{test} && "%{mpm}" != ""
echo "Testing ./httpd-%{mpm}"
mkdir $test_dir
cat > $test_dir/httpd.conf << EOF
ServerName my_test
ErrorLog $test_dir/error_log
PidFile $test_dir/
User $(id -un)
Group $(id -gn)
Listen 60080
DocumentRoot $test_dir
LoadModule authz_core_module $PWD/modules/aaa/.libs/
./httpd-%{mpm} -k start -f $test_dir/httpd.conf
sleep 2
echo 'HTTPD HELLO' > $test_dir/hello.html
curl -s http://localhost:60080/hello.html | grep 'HTTPD HELLO' || exit_code=1
./httpd-%{mpm} -k stop -f $test_dir/httpd.conf
sleep 1
# do not continue %%check phase
exit $exit_code

# test just built utils
%if "%{flavor}" == "utils"
# htpasswd
echo "Testing htpasswd"
support/htpasswd -bc htpasswd foo_user foo_password
support/htpasswd -bv htpasswd foo_user foo_password || exit_code=1
# htpasswd
echo "Testing htpasswd"
support/htdbm -bc htpasswd bar_user bar_password
support/htdbm -bv htpasswd bar_user bar_password || exit_code=2
# do not continue %%check phase
exit $exit_code

# test _installed_ packages (via test_* flavors)
%if %{test}

%if "%{flavor}" == "test_main"
# create test configuration, based on default distro one
# with minimum changes to see it is working
mkdir -p $PWD{%{_sysconfdir}/sysconfig,%{localstatedir},%{runtimedir},%{logfiledir}}
# adjust sysconfig file
cp %{_sysconfdir}/sysconfig/apache2 $PWD%{_sysconfdir}/sysconfig/
sed -i -e "s:\(APACHE_HTTPD_CONF=\).*:\1$PWD%{sysconfdir}/httpd.conf:" \
       -e "s:\(%{_localstatedir}\):$PWD\1:" $PWD%{_sysconfdir}/sysconfig/apache2
sed -i 's:\(APACHE_MPM=\).*:\1"prefork":' $PWD%{_sysconfdir}/sysconfig/apache2
# copy and adjust configuration (paths and Listen)
cp -r %{_sysconfdir}/apache2/ %{_sysconfdir}/mime.types etc 2>/dev/null || true
find etc/apache2 -name *.conf | xargs sed -i "s:\(%{_localstatedir}\):$PWD\1:"
find etc/apache2 -name *.conf | xargs sed -i "s:/etc:$PWD/etc:"
sed -i -e 's:80:60080:' -e 's:443:60443:' etc/apache2/listen.conf
# /usr/sbin/start_apache2 is 744
cp %{_sbindir}/start_apache2 .
export START_APACHE_SYSCONFIG_FILE=$PWD/etc/sysconfig/apache2
./start_apache2 -k start
sleep 2
curl -s http://localhost:60080/manual/ | grep 'Apache.*HTTP Server.*Documentation' || exit_code=1
curl -s http://localhost:60080/manual/de/ | grep 'Neue Funktionen' || exit_code=2
./start_apache2 -k stop
sleep 1
# do not continue %%check phase
exit $exit_code

# test of devel package
%if "%{flavor}" == "test_devel"
# apxs test
echo "Testing apxs, compiling example module"
apxs -q CFLAGS | grep "\\%{optflags}"
cp %{SOURCE21} mod_example.c
apxs -c mod_example.c
echo "Try to load example module"
mkdir $test_dir
cat > $test_dir/httpd.conf << EOF
ServerName my_test
ErrorLog $test_dir/error_log
PidFile $test_dir/
User $(id -un)
Group $(id -gn)
Listen 60080
DocumentRoot $test_dir
LoadModule authz_core_module %{libexecdir}-%{default_mpm}/
LoadModule example_module $PWD/.libs/
<Location /hello>
  SetHandler example-handler
%{_sbindir}/httpd-%{default_mpm} -k start -f $test_dir/httpd.conf
sleep 2
echo "Use example module"
curl -s http://localhost:60080/hello | grep 'Hello, world!' || exit_code=1
%{_sbindir}/httpd-%{default_mpm} -k stop -f $test_dir/httpd.conf
sleep 1
# do not continue %%check phase
exit $exit_code

# unittest run in test_$MPM flavors
echo "Run httpd-framework unittests"
cd httpd-framework
perl Makefile.PL -apxs %{apache_apxs}
function dep()
  # requirement has to be loaded before dependee;
  # there can be duplicite entries in $modules
  # string, will be added only once
  # in load-all-modules.conf
  if echo "$modules" | grep -q "$dependee"; then
    modules="$requirement $modules"
# create a conf loading all MPM's modules
echo >  $PWD/load-all-modules.conf
# hack: sort -u to load mod_proxy before mod_proxy_http, mod_cache before mod_cache_disk, etc.
modules=$(find %{_libdir}/apache2-%{mpm}/ %{_libdir}/apache2/ -name *.so | sed 's:.*/mod_\(.*\).so:\1:' | sort -u)
# fix up dependencies
dep "lbmethod_bybusyness" "proxy"
dep "lbmethod_byrequests" "proxy"
dep "lbmethod_bytraffic"  "proxy"
dep "lbmethod_heartbeat"  "proxy"
for m in $modules; do
  echo "$m" | grep -q 'php' && [ "%{mpm}" == 'worker' -o "%{mpm}" == 'event' ] && continue
  path=$(find %{_libdir}/apache2-%{mpm}/ %{_libdir}/apache2/ -name mod_$ | head -n 1)
  if ! grep -q "mod_$m.c" $PWD/load-all-modules.conf; then
    echo "<IfModule !mod_$m.c>"           >> $PWD/load-all-modules.conf
    echo "  LoadModule ${m}_module $path" >> $PWD/load-all-modules.conf
    echo "</IfModule>"                    >> $PWD/load-all-modules.conf
# run the testsuite
echo '#####################################################'
echo "# TESTING %{mpm}"
echo '#'
t/TEST -clean
t/TEST -httpd /usr/sbin/httpd-%{mpm} -httpd_conf $PWD/load-all-modules.conf -start
t/TEST -run-tests || exit_code=1
t/TEST -stop
exit $exit_code

# end of installed packages test
exit 0


# MPMs files
%if ! %{test} && "%{mpm}" != ""
%ghost %{_sysconfdir}/alternatives/httpd
# %%ghost %%{_sysconfdir}/alternatives/mod_*.so does not work
%(for module in %{dynamic_modules}; do echo "%ghost %{_sysconfdir}/alternatives/mod_$"; done)
%dir %{_libdir}/apache2-%{mpm}
%dir %{_libdir}/apache2

# main package files
%if "%{flavor}" == ""
%license LICENSE
%attr(750,root,root) %dir %{logfiledir}
%attr(750,%{httpduser},root) %dir %{proxycachedir}
%attr(750,%{httpduser},root) %dir %{localstatedir}
%dir %{libexecdir}
%dir %{_libexecdir}
%attr(755,root,root) %{_libexecdir}/apache2_MMN
%dir %{sysconfdir}
%config %{sysconfdir}/magic
%config %{sysconfdir}/mime.types
%config (noreplace) %{sysconfdir}/*.conf
%config (noreplace) %{sysconfdir}/charset.conv
%dir %{sysconfdir}/ssl.crl
%dir %{sysconfdir}/ssl.crt
%dir %{sysconfdir}/ssl.csr
%dir %attr(700,root,root) %{sysconfdir}/ssl.key
%dir %{sysconfdir}/ssl.prm
%dir %{sysconfdir}/conf.d
%dir %{sysconfdir}/vhosts.d
%config(noreplace) %{_sysconfdir}/logrotate.d/apache2
%verify(not mode) %attr(0755,root,root) %{_sbindir}/suexec
%if %{use_firewalld}
%dir %{_prefix}/lib/firewalld
%dir %{_prefix}/lib/firewalld/services
%config %{_sysconfdir}/sysconfig/SuSEfirewall2.d/services/apache2
%config %{_sysconfdir}/sysconfig/SuSEfirewall2.d/services/apache2-ssl
%doc support/SHA1

# utils files
%if "%{flavor}" == "utils"
%files -f utils-filelist

# devel files
%if "%{flavor}" == "devel"

# manual files
%if "%{flavor}" == "manual"
%dir %{_datadir}/apache2
%dir %{sysconfdir}
%dir %{sysconfdir}/conf.d/
%config %{sysconfdir}/conf.d/manual.conf


# MPMs scriptlets
%if ! %{test} && "%{mpm}" != ""
%{_sbindir}/update-alternatives --quiet --force \
    --install %{_sbindir}/httpd httpd %{_sbindir}/httpd-%{mpm} %{mpm_alt_prio}
for module in %{dynamic_modules}; do
  if [ -e %{libexecdir}/mod_$ ]; then
    %{_sbindir}/update-alternatives --quiet --force \
        --install %{_libdir}/apache2/mod_$ mod_$ %{libexecdir}/mod_$ %{mpm_alt_prio}
exit 0

if [ "$1" = 1 ]; then
if [ "$1" = 0 ]; then
  %{_sbindir}/update-alternatives --quiet --force --remove httpd %{_sbindir}/httpd
  for module in %{dynamic_modules}; do
    %{_sbindir}/update-alternatives --quiet --force --remove mod_$ %{_libdir}/apache2/mod_$
exit 0

exit 0

# main package scriptlets
%if "%{flavor}" == ""
%service_add_pre apache2.service apache2@.service
exit 0

# wwwadmin group existed in past remove after openSUSE-13.2 out of support scope
if grep -q "^wwwadmin:" %{_sysconfdir}/group; then
        groupmod -n www wwwadmin 2>/dev/null ||:
        usermod -g %{httpdgroup} %{httpduser} 2>/dev/null ||:
        usermod -s /bin/false %{httpduser} 2>/dev/null ||:
%service_add_post apache2.service apache2@.service
%set_permissions %{_sbindir}/suexec || \
  echo "Please check %{_sysconfdir}/permissions.local for settings of %{_sbindir}/suexec ."
%{fillup_only apache2}
%if %{use_firewalld}
exit 0


%verify_permissions -e %{_sbindir}/suexec

%service_del_preun apache2.service apache2@.service
exit 0

%if %{defined service_del_postun_without_restart}
%service_del_postun_without_restart apache2.service apache2@.service
%service_del_postun apache2.service apache2@.service
if [ "$1" = 1 ]; then

openSUSE Build Service is sponsored by