| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
|
| 4 |
# Copyright (c) 2019, Jon Ellis (@JonEllis) <[email protected]> |
| 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: sudoers |
| 12 |
short_description: Manage sudoers files |
| 13 |
version_added: "4.3.0" |
| 14 |
description: |
| 15 |
- This module allows for the manipulation of sudoers files. |
| 16 |
author: |
| 17 |
- "Jon Ellis (@JonEllis) <[email protected]>" |
| 18 |
extends_documentation_fragment: |
| 19 |
- community.general._attributes |
| 20 |
attributes: |
| 21 |
check_mode: |
| 22 |
support: full |
| 23 |
diff_mode: |
| 24 |
support: none |
| 25 |
options: |
| 26 |
commands: |
| 27 |
description: |
| 28 |
- The commands allowed by the sudoers rule. |
| 29 |
- Multiple can be added by passing a list of commands. |
| 30 |
- Use V(ALL) for all commands. |
| 31 |
type: list |
| 32 |
elements: str |
| 33 |
group: |
| 34 |
description: |
| 35 |
- The name of the group for the sudoers rule. |
| 36 |
- This option cannot be used in conjunction with O(user). |
| 37 |
type: str |
| 38 |
name: |
| 39 |
required: true |
| 40 |
description: |
| 41 |
- The name of the sudoers rule. |
| 42 |
- This is used for the filename for the sudoers file managed by this rule. |
| 43 |
type: str |
| 44 |
noexec: |
| 45 |
description: |
| 46 |
- Whether a command is prevented to run further commands itself. |
| 47 |
default: false |
| 48 |
type: bool |
| 49 |
version_added: 8.4.0 |
| 50 |
nopassword: |
| 51 |
description: |
| 52 |
- Whether a password is not required when command is run with sudo. |
| 53 |
default: true |
| 54 |
type: bool |
| 55 |
setenv: |
| 56 |
description: |
| 57 |
- Whether to allow keeping the environment when command is run with sudo. |
| 58 |
default: false |
| 59 |
type: bool |
| 60 |
version_added: 6.3.0 |
| 61 |
host: |
| 62 |
description: |
| 63 |
- Specify the host the rule is for. |
| 64 |
default: ALL |
| 65 |
type: str |
| 66 |
version_added: 6.2.0 |
| 67 |
runas: |
| 68 |
description: |
| 69 |
- Specify the target user the command(s) runs as. |
| 70 |
type: str |
| 71 |
version_added: 4.7.0 |
| 72 |
sudoers_path: |
| 73 |
description: |
| 74 |
- The path which sudoers config files are managed in. |
| 75 |
default: /etc/sudoers.d |
| 76 |
type: str |
| 77 |
state: |
| 78 |
default: "present" |
| 79 |
choices: |
| 80 |
- present |
| 81 |
- absent |
| 82 |
description: |
| 83 |
- Whether the rule should exist or not. |
| 84 |
type: str |
| 85 |
user: |
| 86 |
description: |
| 87 |
- The name of the user for the sudoers rule. |
| 88 |
- This option cannot be used in conjunction with O(group). |
| 89 |
type: str |
| 90 |
validation: |
| 91 |
description: |
| 92 |
- If V(absent), the sudoers rule is added without validation. |
| 93 |
- If V(detect) and C(visudo) is available, then the sudoers rule is validated by C(visudo). |
| 94 |
- If V(required), C(visudo) must be available to validate the sudoers rule. |
| 95 |
type: str |
| 96 |
default: detect |
| 97 |
choices: [absent, detect, required] |
| 98 |
version_added: 5.2.0 |
| 99 |
""" |
| 100 |
|
| 101 |
EXAMPLES = r""" |
| 102 |
- name: Allow the backup user to sudo /usr/local/bin/backup |
| 103 |
community.general.sudoers: |
| 104 |
name: allow-backup |
| 105 |
state: present |
| 106 |
user: backup |
| 107 |
commands: /usr/local/bin/backup |
| 108 |
|
| 109 |
- name: Allow the bob user to run any commands as alice with sudo -u alice |
| 110 |
community.general.sudoers: |
| 111 |
name: bob-do-as-alice |
| 112 |
state: present |
| 113 |
user: bob |
| 114 |
runas: alice |
| 115 |
commands: ALL |
| 116 |
|
| 117 |
- name: >- |
| 118 |
Allow the monitoring group to run sudo /usr/local/bin/gather-app-metrics |
| 119 |
without requiring a password on the host called webserver |
| 120 |
community.general.sudoers: |
| 121 |
name: monitor-app |
| 122 |
group: monitoring |
| 123 |
host: webserver |
| 124 |
commands: /usr/local/bin/gather-app-metrics |
| 125 |
|
| 126 |
- name: >- |
| 127 |
Allow the alice user to run sudo /bin/systemctl restart my-service or |
| 128 |
sudo /bin/systemctl reload my-service, but a password is required |
| 129 |
community.general.sudoers: |
| 130 |
name: alice-service |
| 131 |
user: alice |
| 132 |
commands: |
| 133 |
- /bin/systemctl restart my-service |
| 134 |
- /bin/systemctl reload my-service |
| 135 |
nopassword: false |
| 136 |
|
| 137 |
- name: Revoke the previous sudo grants given to the alice user |
| 138 |
community.general.sudoers: |
| 139 |
name: alice-service |
| 140 |
state: absent |
| 141 |
|
| 142 |
- name: Allow alice to sudo /usr/local/bin/upload and keep env variables |
| 143 |
community.general.sudoers: |
| 144 |
name: allow-alice-upload |
| 145 |
user: alice |
| 146 |
commands: /usr/local/bin/upload |
| 147 |
setenv: true |
| 148 |
|
| 149 |
- name: >- |
| 150 |
Allow alice to sudo /usr/bin/less but prevent less from |
| 151 |
running further commands itself |
| 152 |
community.general.sudoers: |
| 153 |
name: allow-alice-restricted-less |
| 154 |
user: alice |
| 155 |
commands: /usr/bin/less |
| 156 |
noexec: true |
| 157 |
""" |
| 158 |
|
| 159 |
import os |
| 160 |
|
| 161 |
from ansible.module_utils.basic import AnsibleModule |
| 162 |
|
| 163 |
|
| 164 |
class Sudoers: |
| 165 |
FILE_MODE = 0o440 |
| 166 |
|
| 167 |
def __init__(self, module): |
| 168 |
self.module = module |
| 169 |
|
| 170 |
self.check_mode = module.check_mode |
| 171 |
self.name = module.params["name"] |
| 172 |
self.user = module.params["user"] |
| 173 |
self.group = module.params["group"] |
| 174 |
self.state = module.params["state"] |
| 175 |
self.noexec = module.params["noexec"] |
| 176 |
self.nopassword = module.params["nopassword"] |
| 177 |
self.setenv = module.params["setenv"] |
| 178 |
self.host = module.params["host"] |
| 179 |
self.runas = module.params["runas"] |
| 180 |
self.sudoers_path = module.params["sudoers_path"] |
| 181 |
self.file = os.path.join(self.sudoers_path, self.name) |
| 182 |
self.commands = module.params["commands"] |
| 183 |
self.validation = module.params["validation"] |
| 184 |
|
| 185 |
def write(self): |
| 186 |
if self.check_mode: |
| 187 |
return |
| 188 |
|
| 189 |
with open(self.file, "w") as f: |
| 190 |
f.write(self.content()) |
| 191 |
|
| 192 |
os.chmod(self.file, self.FILE_MODE) |
| 193 |
|
| 194 |
def delete(self): |
| 195 |
if self.check_mode: |
| 196 |
return |
| 197 |
|
| 198 |
os.remove(self.file) |
| 199 |
|
| 200 |
def exists(self): |
| 201 |
return os.path.exists(self.file) |
| 202 |
|
| 203 |
def matches(self): |
| 204 |
with open(self.file) as f: |
| 205 |
content_matches = f.read() == self.content() |
| 206 |
|
| 207 |
current_mode = os.stat(self.file).st_mode & 0o777 |
| 208 |
mode_matches = current_mode == self.FILE_MODE |
| 209 |
|
| 210 |
return content_matches and mode_matches |
| 211 |
|
| 212 |
def content(self): |
| 213 |
if self.user: |
| 214 |
owner = self.user |
| 215 |
elif self.group: |
| 216 |
owner = f"%{self.group}" |
| 217 |
|
| 218 |
commands_str = ", ".join(self.commands) |
| 219 |
noexec_str = "NOEXEC:" if self.noexec else "" |
| 220 |
nopasswd_str = "NOPASSWD:" if self.nopassword else "" |
| 221 |
setenv_str = "SETENV:" if self.setenv else "" |
| 222 |
runas_str = f"({self.runas})" if self.runas is not None else "" |
| 223 |
return f"{owner} {self.host}={runas_str}{noexec_str}{nopasswd_str}{setenv_str} {commands_str}\n" |
| 224 |
|
| 225 |
def validate(self): |
| 226 |
if self.validation == "absent": |
| 227 |
return |
| 228 |
|
| 229 |
visudo_path = self.module.get_bin_path("visudo", required=self.validation == "required") |
| 230 |
if visudo_path is None: |
| 231 |
return |
| 232 |
|
| 233 |
check_command = [visudo_path, "-c", "-f", "-"] |
| 234 |
rc, stdout, stderr = self.module.run_command(check_command, data=self.content()) |
| 235 |
|
| 236 |
if rc != 0: |
| 237 |
self.module.fail_json( |
| 238 |
msg=f"Failed to validate sudoers rule:\n{stdout or stderr}", stdout=stdout, stderr=stderr |
| 239 |
) |
| 240 |
|
| 241 |
def run(self): |
| 242 |
if self.state == "absent": |
| 243 |
if self.exists(): |
| 244 |
self.delete() |
| 245 |
return True |
| 246 |
else: |
| 247 |
return False |
| 248 |
|
| 249 |
self.validate() |
| 250 |
|
| 251 |
if self.exists() and self.matches(): |
| 252 |
return False |
| 253 |
|
| 254 |
self.write() |
| 255 |
return True |
| 256 |
|
| 257 |
|
| 258 |
def main(): |
| 259 |
argument_spec = { |
| 260 |
"commands": { |
| 261 |
"type": "list", |
| 262 |
"elements": "str", |
| 263 |
}, |
| 264 |
"group": {}, |
| 265 |
"name": { |
| 266 |
"required": True, |
| 267 |
}, |
| 268 |
"noexec": { |
| 269 |
"type": "bool", |
| 270 |
"default": False, |
| 271 |
}, |
| 272 |
"nopassword": { |
| 273 |
"type": "bool", |
| 274 |
"default": True, |
| 275 |
}, |
| 276 |
"setenv": { |
| 277 |
"type": "bool", |
| 278 |
"default": False, |
| 279 |
}, |
| 280 |
"host": { |
| 281 |
"type": "str", |
| 282 |
"default": "ALL", |
| 283 |
}, |
| 284 |
"runas": { |
| 285 |
"type": "str", |
| 286 |
"default": None, |
| 287 |
}, |
| 288 |
"sudoers_path": { |
| 289 |
"type": "str", |
| 290 |
"default": "/etc/sudoers.d", |
| 291 |
}, |
| 292 |
"state": { |
| 293 |
"default": "present", |
| 294 |
"choices": ["present", "absent"], |
| 295 |
}, |
| 296 |
"user": {}, |
| 297 |
"validation": {"default": "detect", "choices": ["absent", "detect", "required"]}, |
| 298 |
} |
| 299 |
|
| 300 |
module = AnsibleModule( |
| 301 |
argument_spec=argument_spec, |
| 302 |
mutually_exclusive=[["user", "group"]], |
| 303 |
supports_check_mode=True, |
| 304 |
required_if=[("state", "present", ["commands"])], |
| 305 |
) |
| 306 |
|
| 307 |
sudoers = Sudoers(module) |
| 308 |
|
| 309 |
try: |
| 310 |
changed = sudoers.run() |
| 311 |
module.exit_json(changed=changed) |
| 312 |
except Exception as e: |
| 313 |
module.fail_json(msg=f"{e}") |
| 314 |
|
| 315 |
|
| 316 |
if __name__ == "__main__": |
| 317 |
main() |