| 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: ksu |
| 8 |
short_description: Kerberos substitute user |
| 9 |
description: |
| 10 |
- This become plugins allows your remote/login user to execute commands as another user using the C(ksu) utility. |
| 11 |
author: Ansible Core Team |
| 12 |
options: |
| 13 |
become_user: |
| 14 |
description: User you 'become' to execute the task. |
| 15 |
type: string |
| 16 |
ini: |
| 17 |
- section: privilege_escalation |
| 18 |
key: become_user |
| 19 |
- section: ksu_become_plugin |
| 20 |
key: user |
| 21 |
vars: |
| 22 |
- name: ansible_become_user |
| 23 |
- name: ansible_ksu_user |
| 24 |
env: |
| 25 |
- name: ANSIBLE_BECOME_USER |
| 26 |
- name: ANSIBLE_KSU_USER |
| 27 |
required: true |
| 28 |
become_exe: |
| 29 |
description: C(ksu) executable. |
| 30 |
type: string |
| 31 |
default: ksu |
| 32 |
ini: |
| 33 |
- section: privilege_escalation |
| 34 |
key: become_exe |
| 35 |
- section: ksu_become_plugin |
| 36 |
key: executable |
| 37 |
vars: |
| 38 |
- name: ansible_become_exe |
| 39 |
- name: ansible_ksu_exe |
| 40 |
env: |
| 41 |
- name: ANSIBLE_BECOME_EXE |
| 42 |
- name: ANSIBLE_KSU_EXE |
| 43 |
become_flags: |
| 44 |
description: Options to pass to C(ksu). |
| 45 |
type: string |
| 46 |
default: '' |
| 47 |
ini: |
| 48 |
- section: privilege_escalation |
| 49 |
key: become_flags |
| 50 |
- section: ksu_become_plugin |
| 51 |
key: flags |
| 52 |
vars: |
| 53 |
- name: ansible_become_flags |
| 54 |
- name: ansible_ksu_flags |
| 55 |
env: |
| 56 |
- name: ANSIBLE_BECOME_FLAGS |
| 57 |
- name: ANSIBLE_KSU_FLAGS |
| 58 |
become_pass: |
| 59 |
description: C(ksu) password. |
| 60 |
type: string |
| 61 |
required: false |
| 62 |
vars: |
| 63 |
- name: ansible_ksu_pass |
| 64 |
- name: ansible_become_pass |
| 65 |
- name: ansible_become_password |
| 66 |
env: |
| 67 |
- name: ANSIBLE_BECOME_PASS |
| 68 |
- name: ANSIBLE_KSU_PASS |
| 69 |
ini: |
| 70 |
- section: ksu_become_plugin |
| 71 |
key: password |
| 72 |
prompt_l10n: |
| 73 |
description: |
| 74 |
- List of localized strings to match for prompt detection. |
| 75 |
- If empty the plugin uses the built-in one. |
| 76 |
type: list |
| 77 |
elements: string |
| 78 |
default: [] |
| 79 |
ini: |
| 80 |
- section: ksu_become_plugin |
| 81 |
key: localized_prompts |
| 82 |
vars: |
| 83 |
- name: ansible_ksu_prompt_l10n |
| 84 |
env: |
| 85 |
- name: ANSIBLE_KSU_PROMPT_L10N |
| 86 |
""" |
| 87 |
|
| 88 |
import re |
| 89 |
|
| 90 |
from ansible.module_utils.common.text.converters import to_bytes |
| 91 |
from ansible.plugins.become import BecomeBase |
| 92 |
|
| 93 |
|
| 94 |
class BecomeModule(BecomeBase): |
| 95 |
name = "community.general.ksu" |
| 96 |
|
| 97 |
# messages for detecting prompted password issues |
| 98 |
fail = ("Password incorrect",) |
| 99 |
missing = ("No password given",) |
| 100 |
|
| 101 |
def check_password_prompt(self, b_output): |
| 102 |
"""checks if the expected password prompt exists in b_output""" |
| 103 |
|
| 104 |
prompts = self.get_option("prompt_l10n") or ["Kerberos password for .*@.*:"] |
| 105 |
b_prompt = b"|".join(to_bytes(p) for p in prompts) |
| 106 |
|
| 107 |
return bool(re.match(b_prompt, b_output)) |
| 108 |
|
| 109 |
def build_become_command(self, cmd, shell): |
| 110 |
super().build_become_command(cmd, shell) |
| 111 |
|
| 112 |
# Prompt handling for ``ksu`` is more complicated, this |
| 113 |
# is used to satisfy the connection plugin |
| 114 |
self.prompt = True |
| 115 |
|
| 116 |
if not cmd: |
| 117 |
return cmd |
| 118 |
|
| 119 |
exe = self.get_option("become_exe") |
| 120 |
|
| 121 |
flags = self.get_option("become_flags") |
| 122 |
user = self.get_option("become_user") |
| 123 |
return f"{exe} {user} {flags} -e {self._build_success_command(cmd, shell)} " |