• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2#
3# Copyright 2017, 2018 Tomas Popela <tpopela@redhat.com>
4#
5# Permission is hereby granted, free of charge, to any person obtaining
6# a copy of this software and associated documentation files (the
7# "Software"), to deal in the Software without restriction, including
8# without limitation the rights to use, copy, modify, merge, publish,
9# distribute, sublicense, and/or sell copies of the Software, and to
10# permit persons to whom the Software is furnished to do so, subject to
11# the following conditions:
12#
13# The above copyright notice and this permission notice shall be included
14# in all copies or substantial portions of the Software.
15#
16# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
24import re
25import subprocess
26import sys
27import os
28import glob
29
30def check_php_module(modules_path):
31    php_modules = glob.glob(os.path.join(modules_path, 'libphp7*.so'));
32    if len(php_modules):
33        # The last one in the sorted output will be the desired php module.
34        return sorted(php_modules)[-1];
35
36
37def check_module(modules_path, module):
38     module_path = os.path.join(modules_path, module)
39     return os.path.isfile(module_path)
40
41
42def check_required_basic_modules(modules_path):
43
44    apache_required_modules = [
45        'mod_alias',
46        'mod_auth_basic',
47        'mod_auth_digest',
48        'mod_authn_core',
49        'mod_authn_file',
50        'mod_authz_core',
51        'mod_authz_host',
52        'mod_authz_user',
53        'mod_dir',
54        'mod_mime',
55        'mod_mpm_prefork',
56        'mod_proxy',
57        'mod_proxy_http',
58        'mod_proxy_connect'
59    ]
60
61    found = 0
62    not_found = []
63    for module_name in apache_required_modules:
64        if not check_module(modules_path, module_name + '.so'):
65            if found == 0:
66                return False
67            # If we found at least one module, continue and later report all the
68            # modules that we didn't find.
69            not_found.append(module_name)
70        else:
71            found += 1
72
73    if found < len(apache_required_modules):
74        print('Failed to find required Apache modules for running tests: ' + ', '.join(not_found), file=sys.stderr)
75        return False
76
77    return True
78
79
80def main():
81    """Checks whether the required Apache modules are available and prints their
82       paths to stdout (values are separated by colons).
83
84       Only one argument is required - path to the Apache's apachectl executable"""
85
86    if len(sys.argv) != 2:
87        print('Only one argument with path to the Apache apachectl executable expected!', file=sys.stderr)
88        sys.exit(1)
89
90    apachectl_executable = sys.argv[1]
91
92    if not os.path.isfile(apachectl_executable):
93        print('The passed Apache apachectl executable does not exist!', file=sys.stderr)
94        sys.exit(1)
95
96    apache_prefix = os.path.dirname(os.path.dirname(apachectl_executable))
97    apachectl_output = subprocess.run(
98        [apachectl_executable, '-V', '-C', 'ServerName localhost'], stdout=subprocess.PIPE)
99    if apachectl_output.returncode != 0:
100        print('Something went wrong when calling ' + apachectl_executable + '!', file=sys.stderr)
101        sys.exit(1)
102
103    mpm_regex = re.compile(r'\nServer MPM:[\s]+([\w]+)\n')
104    mpm = mpm_regex.search(apachectl_output.stdout.decode('utf-8')).group(1).lower()
105
106    apache_modules_dir = ''
107    apache_ssl_module_dir = ''
108    apache_php_module_file = ''
109    apache_mod_unixd_module_file = ''
110
111    for lib_dir in ['lib', 'lib64']:
112        for httpd_dir in ['apache', 'apache2', 'http', 'http2', 'httpd']:
113            for mpm_suffix in ['', '-' + mpm]:
114                for modules_dir in ['', 'modules']:
115                    modules_path = os.path.join(apache_prefix, lib_dir, httpd_dir + mpm_suffix, modules_dir)
116                    if check_required_basic_modules(modules_path):
117                        apache_modules_dir = modules_path
118                    if check_module(modules_path, 'mod_ssl.so'):
119                        apache_ssl_module_dir = modules_path
120                    php_module = check_php_module(modules_path)
121                    if (php_module):
122                        apache_php_module_file = php_module
123                    if check_module(modules_path, 'mod_unixd.so'):
124                        apache_mod_unixd_module_file = modules_path
125
126    # These two are mandatory for having properly configured Apache
127    if apache_modules_dir == '' or apache_ssl_module_dir == '':
128        sys.exit(1)
129
130    print(apache_modules_dir + ":" +
131          apache_ssl_module_dir + ":" +
132          apache_php_module_file + ":" +
133          apache_mod_unixd_module_file, end='')
134
135if __name__ == "__main__":
136    main()
137