| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2015, Michael Scherer <[email protected]> |
| 4 |
# inspired by code of github.com/dandiker/ |
| 5 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 7 |
|
| 8 |
from __future__ import annotations |
| 9 |
|
| 10 |
DOCUMENTATION = r""" |
| 11 |
module: selinux_permissive |
| 12 |
short_description: Change permissive domain in SELinux policy |
| 13 |
description: |
| 14 |
- Add and remove a domain from the list of permissive domains. |
| 15 |
extends_documentation_fragment: |
| 16 |
- community.general._attributes |
| 17 |
attributes: |
| 18 |
check_mode: |
| 19 |
support: full |
| 20 |
diff_mode: |
| 21 |
support: none |
| 22 |
options: |
| 23 |
domain: |
| 24 |
description: |
| 25 |
- The domain that is added or removed from the list of permissive domains. |
| 26 |
type: str |
| 27 |
required: true |
| 28 |
aliases: [name] |
| 29 |
permissive: |
| 30 |
description: |
| 31 |
- Indicate if the domain should or should not be set as permissive. |
| 32 |
type: bool |
| 33 |
required: true |
| 34 |
no_reload: |
| 35 |
description: |
| 36 |
- Disable reloading of the SELinux policy after making change to a domain's permissive setting. |
| 37 |
- The default is V(false), which causes policy to be reloaded when a domain changes state. |
| 38 |
- Reloading the policy does not work on older versions of the C(policycoreutils-python) library, for example in EL 6.". |
| 39 |
type: bool |
| 40 |
default: false |
| 41 |
store: |
| 42 |
description: |
| 43 |
- Name of the SELinux policy store to use. |
| 44 |
type: str |
| 45 |
default: '' |
| 46 |
notes: |
| 47 |
- Requires a recent version of SELinux and C(policycoreutils-python) (EL 6 or newer). |
| 48 |
requirements: [policycoreutils-python] |
| 49 |
author: |
| 50 |
- Michael Scherer (@mscherer) <[email protected]> |
| 51 |
""" |
| 52 |
|
| 53 |
EXAMPLES = r""" |
| 54 |
- name: Change the httpd_t domain to permissive |
| 55 |
community.general.selinux_permissive: |
| 56 |
name: httpd_t |
| 57 |
permissive: true |
| 58 |
""" |
| 59 |
|
| 60 |
import traceback |
| 61 |
|
| 62 |
HAVE_SEOBJECT = False |
| 63 |
SEOBJECT_IMP_ERR = None |
| 64 |
try: |
| 65 |
import seobject |
| 66 |
|
| 67 |
HAVE_SEOBJECT = True |
| 68 |
except ImportError: |
| 69 |
SEOBJECT_IMP_ERR = traceback.format_exc() |
| 70 |
|
| 71 |
from ansible.module_utils.basic import AnsibleModule, missing_required_lib |
| 72 |
|
| 73 |
|
| 74 |
def main(): |
| 75 |
module = AnsibleModule( |
| 76 |
argument_spec=dict( |
| 77 |
domain=dict(type="str", required=True, aliases=["name"]), |
| 78 |
store=dict(type="str", default=""), |
| 79 |
permissive=dict(type="bool", required=True), |
| 80 |
no_reload=dict(type="bool", default=False), |
| 81 |
), |
| 82 |
supports_check_mode=True, |
| 83 |
) |
| 84 |
|
| 85 |
# global vars |
| 86 |
changed = False |
| 87 |
store = module.params["store"] |
| 88 |
permissive = module.params["permissive"] |
| 89 |
domain = module.params["domain"] |
| 90 |
no_reload = module.params["no_reload"] |
| 91 |
|
| 92 |
if not HAVE_SEOBJECT: |
| 93 |
module.fail_json(changed=False, msg=missing_required_lib("policycoreutils-python"), exception=SEOBJECT_IMP_ERR) |
| 94 |
|
| 95 |
try: |
| 96 |
permissive_domains = seobject.permissiveRecords(store) |
| 97 |
except ValueError as e: |
| 98 |
module.fail_json(domain=domain, msg=f"{e}", exception=traceback.format_exc()) |
| 99 |
|
| 100 |
# not supported on EL 6 |
| 101 |
if "set_reload" in dir(permissive_domains): |
| 102 |
permissive_domains.set_reload(not no_reload) |
| 103 |
|
| 104 |
try: |
| 105 |
all_domains = permissive_domains.get_all() |
| 106 |
except ValueError as e: |
| 107 |
module.fail_json(domain=domain, msg=f"{e}", exception=traceback.format_exc()) |
| 108 |
|
| 109 |
if permissive: |
| 110 |
if domain not in all_domains: |
| 111 |
if not module.check_mode: |
| 112 |
try: |
| 113 |
permissive_domains.add(domain) |
| 114 |
except ValueError as e: |
| 115 |
module.fail_json(domain=domain, msg=f"{e}", exception=traceback.format_exc()) |
| 116 |
changed = True |
| 117 |
else: |
| 118 |
if domain in all_domains: |
| 119 |
if not module.check_mode: |
| 120 |
try: |
| 121 |
permissive_domains.delete(domain) |
| 122 |
except ValueError as e: |
| 123 |
module.fail_json(domain=domain, msg=f"{e}", exception=traceback.format_exc()) |
| 124 |
changed = True |
| 125 |
|
| 126 |
module.exit_json(changed=changed, store=store, permissive=permissive, domain=domain) |
| 127 |
|
| 128 |
|
| 129 |
if __name__ == "__main__": |
| 130 |
main() |