| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2014, Nate Coraor <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: capabilities |
| 11 |
short_description: Manage Linux capabilities |
| 12 |
description: |
| 13 |
- This module manipulates files privileges using the Linux capabilities(7) system. |
| 14 |
extends_documentation_fragment: |
| 15 |
- community.general._attributes |
| 16 |
attributes: |
| 17 |
check_mode: |
| 18 |
support: full |
| 19 |
diff_mode: |
| 20 |
support: none |
| 21 |
options: |
| 22 |
path: |
| 23 |
description: |
| 24 |
- Specifies the path to the file to be managed. |
| 25 |
type: str |
| 26 |
required: true |
| 27 |
aliases: [key] |
| 28 |
capability: |
| 29 |
description: |
| 30 |
- Desired capability to set (with operator and flags, if O(state=present)) or remove (if O(state=absent)). |
| 31 |
type: str |
| 32 |
required: true |
| 33 |
aliases: [cap] |
| 34 |
state: |
| 35 |
description: |
| 36 |
- Whether the entry should be present or absent in the file's capabilities. |
| 37 |
type: str |
| 38 |
choices: [absent, present] |
| 39 |
default: present |
| 40 |
notes: |
| 41 |
- The capabilities system automatically transforms operators and flags into the effective set, so for example, C(cap_foo=ep) |
| 42 |
probably becomes C(cap_foo+ep). |
| 43 |
- This module does not attempt to determine the final operator and flags to compare, so you want to ensure that your capabilities |
| 44 |
argument matches the final capabilities. |
| 45 |
author: |
| 46 |
- Nate Coraor (@natefoo) |
| 47 |
""" |
| 48 |
|
| 49 |
EXAMPLES = r""" |
| 50 |
- name: Set cap_sys_chroot+ep on /foo |
| 51 |
community.general.capabilities: |
| 52 |
path: /foo |
| 53 |
capability: cap_sys_chroot+ep |
| 54 |
state: present |
| 55 |
|
| 56 |
- name: Remove cap_net_bind_service from /bar |
| 57 |
community.general.capabilities: |
| 58 |
path: /bar |
| 59 |
capability: cap_net_bind_service |
| 60 |
state: absent |
| 61 |
""" |
| 62 |
|
| 63 |
from ansible.module_utils.basic import AnsibleModule |
| 64 |
|
| 65 |
OPS = ("=", "-", "+") |
| 66 |
|
| 67 |
|
| 68 |
class CapabilitiesModule: |
| 69 |
platform = "Linux" |
| 70 |
distribution = None |
| 71 |
|
| 72 |
def __init__(self, module): |
| 73 |
self.module = module |
| 74 |
self.path = module.params["path"].strip() |
| 75 |
self.capability = module.params["capability"].strip().lower() |
| 76 |
self.state = module.params["state"] |
| 77 |
self.getcap_cmd = module.get_bin_path("getcap", required=True) |
| 78 |
self.setcap_cmd = module.get_bin_path("setcap", required=True) |
| 79 |
self.capability_tup = self._parse_cap(self.capability, op_required=self.state == "present") |
| 80 |
|
| 81 |
self.run() |
| 82 |
|
| 83 |
def run(self): |
| 84 |
current = self.getcap(self.path) |
| 85 |
caps = [cap[0] for cap in current] |
| 86 |
|
| 87 |
if self.state == "present" and self.capability_tup not in current: |
| 88 |
# need to add capability |
| 89 |
if self.module.check_mode: |
| 90 |
self.module.exit_json(changed=True, msg="capabilities changed") |
| 91 |
else: |
| 92 |
# remove from current cap list if it is already set (but op/flags differ) |
| 93 |
current = [x for x in current if x[0] != self.capability_tup[0]] |
| 94 |
# add new cap with correct op/flags |
| 95 |
current.append(self.capability_tup) |
| 96 |
self.module.exit_json( |
| 97 |
changed=True, state=self.state, msg="capabilities changed", stdout=self.setcap(self.path, current) |
| 98 |
) |
| 99 |
elif self.state == "absent" and self.capability_tup[0] in caps: |
| 100 |
# need to remove capability |
| 101 |
if self.module.check_mode: |
| 102 |
self.module.exit_json(changed=True, msg="capabilities changed") |
| 103 |
else: |
| 104 |
# remove from current cap list and then set current list |
| 105 |
current = [x for x in current if x[0] != self.capability_tup[0]] |
| 106 |
self.module.exit_json( |
| 107 |
changed=True, state=self.state, msg="capabilities changed", stdout=self.setcap(self.path, current) |
| 108 |
) |
| 109 |
self.module.exit_json(changed=False, state=self.state) |
| 110 |
|
| 111 |
def getcap(self, path): |
| 112 |
rval = [] |
| 113 |
cmd = [self.getcap_cmd, "-v", path] |
| 114 |
rc, stdout, stderr = self.module.run_command(cmd) |
| 115 |
# If file xattrs are set but no caps are set the output will be: |
| 116 |
# '/foo =' |
| 117 |
# If file xattrs are unset the output will be: |
| 118 |
# '/foo' |
| 119 |
# If the file does not exist, the stderr will be (with rc == 0...): |
| 120 |
# '/foo (No such file or directory)' |
| 121 |
if rc != 0 or stderr != "": |
| 122 |
self.module.fail_json(msg=f"Unable to get capabilities of {path}", stdout=stdout.strip(), stderr=stderr) |
| 123 |
if stdout.strip() != path: |
| 124 |
if " =" in stdout: |
| 125 |
# process output of an older version of libcap |
| 126 |
caps = stdout.split(" =")[1].strip().split() |
| 127 |
elif stdout.strip().endswith(")"): # '/foo (Error Message)' |
| 128 |
self.module.fail_json(msg=f"Unable to get capabilities of {path}", stdout=stdout.strip(), stderr=stderr) |
| 129 |
else: |
| 130 |
# otherwise, we have a newer version here |
| 131 |
# see original commit message of cap/v0.2.40-18-g177cd41 in libcap.git |
| 132 |
caps = stdout.split()[1].strip().split() |
| 133 |
for cap in caps: |
| 134 |
cap = cap.lower() |
| 135 |
# getcap condenses capabilities with the same op/flags into a |
| 136 |
# comma-separated list, so we have to parse that |
| 137 |
if "," in cap: |
| 138 |
cap_group = cap.split(",") |
| 139 |
cap_group[-1], op, flags = self._parse_cap(cap_group[-1]) |
| 140 |
for subcap in cap_group: |
| 141 |
rval.append((subcap, op, flags)) |
| 142 |
else: |
| 143 |
rval.append(self._parse_cap(cap)) |
| 144 |
return rval |
| 145 |
|
| 146 |
def setcap(self, path, caps): |
| 147 |
caps = " ".join(["".join(cap) for cap in caps]) |
| 148 |
cmd = [self.setcap_cmd, caps, path] |
| 149 |
rc, stdout, stderr = self.module.run_command(cmd) |
| 150 |
if rc != 0: |
| 151 |
self.module.fail_json(msg=f"Unable to set capabilities of {path}", stdout=stdout, stderr=stderr) |
| 152 |
else: |
| 153 |
return stdout |
| 154 |
|
| 155 |
def _parse_cap(self, cap, op_required=True): |
| 156 |
opind = -1 |
| 157 |
try: |
| 158 |
i = 0 |
| 159 |
while opind == -1: |
| 160 |
opind = cap.find(OPS[i]) |
| 161 |
i += 1 |
| 162 |
except Exception: |
| 163 |
if op_required: |
| 164 |
self.module.fail_json(msg=f"Couldn't find operator (one of: {OPS})") |
| 165 |
else: |
| 166 |
return (cap, None, None) |
| 167 |
op = cap[opind] |
| 168 |
cap, flags = cap.split(op) |
| 169 |
return (cap, op, flags) |
| 170 |
|
| 171 |
|
| 172 |
# ============================================================== |
| 173 |
# main |
| 174 |
|
| 175 |
|
| 176 |
def main(): |
| 177 |
# defining module |
| 178 |
module = AnsibleModule( |
| 179 |
argument_spec=dict( |
| 180 |
path=dict(type="str", required=True, aliases=["key"]), |
| 181 |
capability=dict(type="str", required=True, aliases=["cap"]), |
| 182 |
state=dict(type="str", default="present", choices=["absent", "present"]), |
| 183 |
), |
| 184 |
supports_check_mode=True, |
| 185 |
) |
| 186 |
module.run_command_environ_update = {"LANGUAGE": "C", "LC_ALL": "C"} |
| 187 |
|
| 188 |
CapabilitiesModule(module) |
| 189 |
|
| 190 |
|
| 191 |
if __name__ == "__main__": |
| 192 |
main() |