| 1 |
# Copyright: (c) 2021, Ansible Project |
| 2 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
|
| 4 |
from __future__ import annotations |
| 5 |
|
| 6 |
import os |
| 7 |
import sys |
| 8 |
|
| 9 |
from ansible.module_utils.common.text.converters import to_native, to_bytes |
| 10 |
from ctypes import CDLL, c_char_p, c_int, byref, POINTER, get_errno |
| 11 |
|
| 12 |
try: |
| 13 |
_selinux_lib = CDLL('libselinux.so.1', use_errno=True) |
| 14 |
except OSError as ex: |
| 15 |
raise ImportError('unable to load libselinux.so') from ex |
| 16 |
|
| 17 |
|
| 18 |
def _module_setup(): |
| 19 |
def _check_rc(rc): |
| 20 |
if rc < 0: |
| 21 |
errno = get_errno() |
| 22 |
raise OSError(errno, os.strerror(errno)) |
| 23 |
return rc |
| 24 |
|
| 25 |
binary_char_type = type(b'') |
| 26 |
|
| 27 |
class _to_char_p: |
| 28 |
@classmethod |
| 29 |
def from_param(cls, strvalue): |
| 30 |
if strvalue is not None and not isinstance(strvalue, binary_char_type): |
| 31 |
strvalue = to_bytes(strvalue) |
| 32 |
|
| 33 |
return strvalue |
| 34 |
|
| 35 |
# FIXME: swap restype to errcheck |
| 36 |
|
| 37 |
_funcmap = dict( |
| 38 |
is_selinux_enabled={}, |
| 39 |
is_selinux_mls_enabled={}, |
| 40 |
lgetfilecon_raw=dict(argtypes=[_to_char_p, POINTER(c_char_p)], restype=_check_rc), |
| 41 |
# NB: matchpathcon is deprecated and should be rewritten on selabel_lookup (but will be a PITA) |
| 42 |
matchpathcon=dict(argtypes=[_to_char_p, c_int, POINTER(c_char_p)], restype=_check_rc), |
| 43 |
security_policyvers={}, |
| 44 |
selinux_getenforcemode=dict(argtypes=[POINTER(c_int)]), |
| 45 |
security_getenforce={}, |
| 46 |
lsetfilecon=dict(argtypes=[_to_char_p, _to_char_p], restype=_check_rc), |
| 47 |
selinux_getpolicytype=dict(argtypes=[POINTER(c_char_p)], restype=_check_rc), |
| 48 |
) |
| 49 |
|
| 50 |
_thismod = sys.modules[__name__] |
| 51 |
|
| 52 |
for fname, cfg in _funcmap.items(): |
| 53 |
fn = getattr(_selinux_lib, fname, None) |
| 54 |
|
| 55 |
if not fn: |
| 56 |
raise ImportError('missing selinux function: {0}'.format(fname)) |
| 57 |
|
| 58 |
# all ctypes pointers share the same base type |
| 59 |
base_ptr_type = type(POINTER(c_int)) |
| 60 |
fn.argtypes = cfg.get('argtypes', None) |
| 61 |
fn.restype = cfg.get('restype', c_int) |
| 62 |
|
| 63 |
# just patch simple directly callable functions directly onto the module |
| 64 |
if not fn.argtypes or not any(argtype for argtype in fn.argtypes if type(argtype) is base_ptr_type): |
| 65 |
setattr(_thismod, fname, fn) |
| 66 |
continue |
| 67 |
|
| 68 |
# NB: this validation code must run after all the wrappers have been declared |
| 69 |
unimplemented_funcs = set(_funcmap).difference(dir(_thismod)) |
| 70 |
if unimplemented_funcs: |
| 71 |
raise NotImplementedError('implementation is missing functions: {0}'.format(unimplemented_funcs)) |
| 72 |
|
| 73 |
|
| 74 |
# begin wrapper function impls |
| 75 |
|
| 76 |
def selinux_getenforcemode(): |
| 77 |
enforcemode = c_int() |
| 78 |
rc = _selinux_lib.selinux_getenforcemode(byref(enforcemode)) |
| 79 |
return [rc, enforcemode.value] |
| 80 |
|
| 81 |
|
| 82 |
def selinux_getpolicytype(): |
| 83 |
con = c_char_p() |
| 84 |
try: |
| 85 |
rc = _selinux_lib.selinux_getpolicytype(byref(con)) |
| 86 |
return [rc, to_native(con.value)] |
| 87 |
finally: |
| 88 |
_selinux_lib.freecon(con) |
| 89 |
|
| 90 |
|
| 91 |
def lgetfilecon_raw(path): |
| 92 |
con = c_char_p() |
| 93 |
try: |
| 94 |
rc = _selinux_lib.lgetfilecon_raw(path, byref(con)) |
| 95 |
return [rc, to_native(con.value)] |
| 96 |
finally: |
| 97 |
_selinux_lib.freecon(con) |
| 98 |
|
| 99 |
|
| 100 |
def matchpathcon(path, mode): |
| 101 |
con = c_char_p() |
| 102 |
try: |
| 103 |
rc = _selinux_lib.matchpathcon(path, mode, byref(con)) |
| 104 |
return [rc, to_native(con.value)] |
| 105 |
finally: |
| 106 |
_selinux_lib.freecon(con) |
| 107 |
|
| 108 |
|
| 109 |
_module_setup() |
| 110 |
del _module_setup |
| 111 |
|
| 112 |
# end wrapper function impls |