Skip to content

Commit

Permalink
Bugfix for newer policycoreutils-python (eg. RHEL7) (ansible#3569)
Browse files Browse the repository at this point in the history
The policycoreutils python API for RHEL6 and RHEL7 are sufficiently
different, requiring some additional definitions and specific conversion
that works on old and new implementations.

It also implements a fix for non-ascii error messages (like when using a
French locale configuration).

This fixes ansible#3551.
  • Loading branch information
dagwieers authored and mattclay committed Dec 8, 2016
1 parent 335a284 commit 367a0c5
Showing 1 changed file with 28 additions and 16 deletions.
44 changes: 28 additions & 16 deletions lib/ansible/modules/extras/system/sefcontext.py
Expand Up @@ -81,6 +81,7 @@

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.pycompat24 import get_exception
from ansible.module_utils._text import to_native

try:
import selinux
Expand All @@ -94,21 +95,35 @@
except ImportError:
HAVE_SEOBJECT=False

### Add missing entries (backward compatible)
seobject.file_types.update(dict(
a = seobject.SEMANAGE_FCONTEXT_ALL,
b = seobject.SEMANAGE_FCONTEXT_BLOCK,
c = seobject.SEMANAGE_FCONTEXT_CHAR,
d = seobject.SEMANAGE_FCONTEXT_DIR,
f = seobject.SEMANAGE_FCONTEXT_REG,
l = seobject.SEMANAGE_FCONTEXT_LINK,
p = seobject.SEMANAGE_FCONTEXT_PIPE,
s = seobject.SEMANAGE_FCONTEXT_SOCK,
))

### Make backward compatible
option_to_file_type_str = {
'a': 'all files',
'b': 'block device',
'c': 'character device',
'd': 'directory',
'f': 'regular file',
'l': 'symbolic link',
's': 'socket file',
'p': 'named pipe',
}
option_to_file_type_str = dict(
a = 'all files',
b = 'block device',
c = 'character device',
d = 'directory',
f = 'regular file',
l = 'symbolic link',
p = 'named pipe',
s = 'socket file',
)

def semanage_fcontext_exists(sefcontext, target, ftype):
''' Get the SELinux file context mapping definition from policy. Return None if it does not exist. '''
record = (target, ftype)

# Beware that records comprise of a string representation of the file_type
record = (target, option_to_file_type_str[ftype])
records = sefcontext.get_all()
try:
return records[record]
Expand Down Expand Up @@ -160,7 +175,7 @@ def semanage_fcontext_modify(module, result, target, ftype, setype, do_reload, s

except Exception:
e = get_exception()
module.fail_json(msg="%s: %s\n" % (e.__class__.__name__, str(e)))
module.fail_json(msg="%s: %s\n" % (e.__class__.__name__, to_native(e)))

if module._diff and prepared_diff:
result['diff'] = dict(prepared=prepared_diff)
Expand Down Expand Up @@ -191,7 +206,7 @@ def semanage_fcontext_delete(module, result, target, ftype, do_reload, sestore='

except Exception:
e = get_exception()
module.fail_json(msg="%s: %s\n" % (e.__class__.__name__, str(e)))
module.fail_json(msg="%s: %s\n" % (e.__class__.__name__, to_native(e)))

if module._diff and prepared_diff:
result['diff'] = dict(prepared=prepared_diff)
Expand Down Expand Up @@ -231,9 +246,6 @@ def main():

result = dict(target=target, ftype=ftype, setype=setype, state=state)

# Convert file types to (internally used) strings
ftype = option_to_file_type_str[ftype]

if state == 'present':
semanage_fcontext_modify(module, result, target, ftype, setype, do_reload, serange, seuser)
elif state == 'absent':
Expand Down

0 comments on commit 367a0c5

Please sign in to comment.