|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import subprocess |
| 4 | + |
| 5 | + |
| 6 | +# general use |
| 7 | +def get_output(*args, **kwargs): |
| 8 | + res = subprocess.check_output(*args, shell=True, **kwargs) |
| 9 | + decoded = res.decode('utf-8') |
| 10 | + return decoded.strip() |
| 11 | + |
| 12 | + |
| 13 | +# python-gssapi related |
| 14 | +def import_gssapi_extension(name): |
| 15 | + """Import a GSSAPI extension module |
| 16 | +
|
| 17 | + This method imports a GSSAPI extension module based |
| 18 | + on the name of the extension (not including the |
| 19 | + 'ext_' prefix). If the extension is not available, |
| 20 | + the method retuns None. |
| 21 | +
|
| 22 | + Args: |
| 23 | + name (str): the name of the extension |
| 24 | +
|
| 25 | + Returns: |
| 26 | + module: Either the extension module or None |
| 27 | + """ |
| 28 | + |
| 29 | + try: |
| 30 | + path = 'gssapi.raw.ext_{0}'.format(name) |
| 31 | + __import__(path) |
| 32 | + return sys.modules[path] |
| 33 | + except ImportError: |
| 34 | + return None |
| 35 | + |
| 36 | + |
| 37 | +# krb5-plugin related |
| 38 | +_PLUGIN_DIR = None |
| 39 | + |
| 40 | + |
| 41 | +def find_plugin_dir(): |
| 42 | + global _PLUGIN_DIR |
| 43 | + if _PLUGIN_DIR is not None: |
| 44 | + return _PLUGIN_DIR |
| 45 | + |
| 46 | + # if we've set a LD_LIBRARY_PATH, use that first |
| 47 | + ld_path_raw = os.environ.get('LD_LIBRARY_PATH') |
| 48 | + if ld_path_raw is not None: |
| 49 | + # first, try assuming it's just a normal install |
| 50 | + |
| 51 | + ld_paths = [path for path in ld_path_raw.split(':') if path] |
| 52 | + |
| 53 | + for ld_path in ld_paths: |
| 54 | + if not os.path.exists(ld_path): |
| 55 | + continue |
| 56 | + |
| 57 | + _PLUGIN_DIR = _decide_plugin_dir( |
| 58 | + _find_plugin_dirs_installed(ld_path)) |
| 59 | + if _PLUGIN_DIR is None: |
| 60 | + _PLUGIN_DIR = _decide_plugin_dir( |
| 61 | + _find_plugin_dirs_src(ld_path)) |
| 62 | + |
| 63 | + if _PLUGIN_DIR is not None: |
| 64 | + break |
| 65 | + |
| 66 | + # if there was no LD_LIBRARY_PATH, or the above failed |
| 67 | + if _PLUGIN_DIR is None: |
| 68 | + # if we don't have a LD_LIBRARY_PATH, just search in |
| 69 | + # $prefix/lib |
| 70 | + |
| 71 | + lib_dir = os.path.join(get_output('krb5-config --prefix'), 'lib') |
| 72 | + _PLUGIN_DIR = _decide_plugin_dir(_find_plugin_dirs_installed(lib_dir)) |
| 73 | + |
| 74 | + if _PLUGIN_DIR is not None: |
| 75 | + _PLUGIN_DIR = os.path.normpath(_PLUGIN_DIR) |
| 76 | + return _PLUGIN_DIR |
| 77 | + else: |
| 78 | + return None |
| 79 | + |
| 80 | + |
| 81 | +def _decide_plugin_dir(dirs): |
| 82 | + if dirs is None: |
| 83 | + return None |
| 84 | + |
| 85 | + # the shortest path is probably more correct |
| 86 | + shortest_first = sorted(dirs, key=len) |
| 87 | + |
| 88 | + for path in shortest_first: |
| 89 | + # check to see if it actually contains .so files |
| 90 | + if get_output('find %s -name "*.so"' % path): |
| 91 | + return path |
| 92 | + |
| 93 | + return None |
| 94 | + |
| 95 | + |
| 96 | +def _find_plugin_dirs_installed(search_path): |
| 97 | + options_raw = get_output('find %s/ -type d ' |
| 98 | + '-path "*/krb5/plugins"' % search_path) |
| 99 | + |
| 100 | + if options_raw: |
| 101 | + return options_raw.split('\n') |
| 102 | + else: |
| 103 | + return None |
| 104 | + |
| 105 | + |
| 106 | +def _find_plugin_dirs_src(search_path): |
| 107 | + options_raw = get_output('find %s/../ -type d -name plugins' % search_path) |
| 108 | + |
| 109 | + if options_raw: |
| 110 | + return options_raw.split('\n') |
| 111 | + else: |
| 112 | + return None |
0 commit comments