| 1 |
# Copyright (c) 2018, Ansible Project |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
from __future__ import annotations |
| 5 |
|
| 6 |
DOCUMENTATION = r""" |
| 7 |
name: machinectl |
| 8 |
short_description: Systemd's machinectl privilege escalation |
| 9 |
description: |
| 10 |
- This become plugins allows your remote/login user to execute commands as another user using the C(machinectl) utility. |
| 11 |
author: Ansible Core Team |
| 12 |
options: |
| 13 |
become_user: |
| 14 |
description: User you 'become' to execute the task. |
| 15 |
type: string |
| 16 |
default: '' |
| 17 |
ini: |
| 18 |
- section: privilege_escalation |
| 19 |
key: become_user |
| 20 |
- section: machinectl_become_plugin |
| 21 |
key: user |
| 22 |
vars: |
| 23 |
- name: ansible_become_user |
| 24 |
- name: ansible_machinectl_user |
| 25 |
env: |
| 26 |
- name: ANSIBLE_BECOME_USER |
| 27 |
- name: ANSIBLE_MACHINECTL_USER |
| 28 |
become_exe: |
| 29 |
description: C(machinectl) executable. |
| 30 |
type: string |
| 31 |
default: machinectl |
| 32 |
ini: |
| 33 |
- section: privilege_escalation |
| 34 |
key: become_exe |
| 35 |
- section: machinectl_become_plugin |
| 36 |
key: executable |
| 37 |
vars: |
| 38 |
- name: ansible_become_exe |
| 39 |
- name: ansible_machinectl_exe |
| 40 |
env: |
| 41 |
- name: ANSIBLE_BECOME_EXE |
| 42 |
- name: ANSIBLE_MACHINECTL_EXE |
| 43 |
become_flags: |
| 44 |
description: Options to pass to C(machinectl). |
| 45 |
type: string |
| 46 |
default: '' |
| 47 |
ini: |
| 48 |
- section: privilege_escalation |
| 49 |
key: become_flags |
| 50 |
- section: machinectl_become_plugin |
| 51 |
key: flags |
| 52 |
vars: |
| 53 |
- name: ansible_become_flags |
| 54 |
- name: ansible_machinectl_flags |
| 55 |
env: |
| 56 |
- name: ANSIBLE_BECOME_FLAGS |
| 57 |
- name: ANSIBLE_MACHINECTL_FLAGS |
| 58 |
become_pass: |
| 59 |
description: Password for C(machinectl). |
| 60 |
type: string |
| 61 |
required: false |
| 62 |
vars: |
| 63 |
- name: ansible_become_password |
| 64 |
- name: ansible_become_pass |
| 65 |
- name: ansible_machinectl_pass |
| 66 |
env: |
| 67 |
- name: ANSIBLE_BECOME_PASS |
| 68 |
- name: ANSIBLE_MACHINECTL_PASS |
| 69 |
ini: |
| 70 |
- section: machinectl_become_plugin |
| 71 |
key: password |
| 72 |
notes: |
| 73 |
- When not using this plugin with user V(root), it only works correctly with a polkit rule which alters the behaviour |
| 74 |
of C(machinectl). This rule must alter the prompt behaviour to ask directly for the user credentials, if the user is allowed |
| 75 |
to perform the action (take a look at the examples section). If such a rule is not present the plugin only works if it |
| 76 |
is used in context with the root user, because then no further prompt is shown by C(machinectl). |
| 77 |
- This become plugin does not work when connection pipelining is enabled. With ansible-core 2.19+, using it automatically |
| 78 |
disables pipelining. On ansible-core 2.18 and before, pipelining must explicitly be disabled by the user. |
| 79 |
""" |
| 80 |
|
| 81 |
EXAMPLES = r""" |
| 82 |
# A polkit rule needed to use the module with a non-root user. |
| 83 |
# See the Notes section for details. |
| 84 |
/etc/polkit-1/rules.d/60-machinectl-fast-user-auth.rules: |- |
| 85 |
polkit.addRule(function(action, subject) { |
| 86 |
if(action.id == "org.freedesktop.machine1.host-shell" && |
| 87 |
subject.isInGroup("wheel")) { |
| 88 |
return polkit.Result.AUTH_SELF_KEEP; |
| 89 |
} |
| 90 |
}); |
| 91 |
""" |
| 92 |
|
| 93 |
from re import compile as re_compile |
| 94 |
|
| 95 |
from ansible.module_utils.common.text.converters import to_bytes |
| 96 |
from ansible.plugins.become import BecomeBase |
| 97 |
|
| 98 |
ansi_color_codes = re_compile(to_bytes(r"\x1B\[[0-9;]+m")) |
| 99 |
|
| 100 |
|
| 101 |
class BecomeModule(BecomeBase): |
| 102 |
name = "community.general.machinectl" |
| 103 |
|
| 104 |
prompt = "Password: " |
| 105 |
fail = ("==== AUTHENTICATION FAILED ====",) |
| 106 |
success = ("==== AUTHENTICATION COMPLETE ====",) |
| 107 |
require_tty = True # see https://github.com/ansible-collections/community.general/issues/6932 |
| 108 |
|
| 109 |
# See https://github.com/ansible/ansible/issues/81254, |
| 110 |
# https://github.com/ansible/ansible/pull/78111 |
| 111 |
pipelining = False |
| 112 |
|
| 113 |
@staticmethod |
| 114 |
def remove_ansi_codes(line): |
| 115 |
return ansi_color_codes.sub(b"", line) |
| 116 |
|
| 117 |
def build_become_command(self, cmd, shell): |
| 118 |
super().build_become_command(cmd, shell) |
| 119 |
|
| 120 |
if not cmd: |
| 121 |
return cmd |
| 122 |
|
| 123 |
become = self.get_option("become_exe") |
| 124 |
|
| 125 |
flags = self.get_option("become_flags") |
| 126 |
user = self.get_option("become_user") |
| 127 |
# SYSTEMD_COLORS=0 stops machinectl from appending ANSI reset |
| 128 |
# sequences (ESC[0m, ESC[J) after the child exits, which would |
| 129 |
# otherwise land after the module JSON and break result parsing. |
| 130 |
return f"SYSTEMD_COLORS=0 {become} -q shell {flags} {user}@ {self._build_success_command(cmd, shell)}" |
| 131 |
|
| 132 |
def check_success(self, b_output): |
| 133 |
b_output = self.remove_ansi_codes(b_output) |
| 134 |
return super().check_success(b_output) |
| 135 |
|
| 136 |
def check_incorrect_password(self, b_output): |
| 137 |
b_output = self.remove_ansi_codes(b_output) |
| 138 |
return super().check_incorrect_password(b_output) |
| 139 |
|
| 140 |
def check_missing_password(self, b_output): |
| 141 |
b_output = self.remove_ansi_codes(b_output) |
| 142 |
return super().check_missing_password(b_output) |