| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Copyright: (c) 2018, Ansible Project |
| 3 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
from __future__ import annotations |
| 5 |
|
| 6 |
DOCUMENTATION = """ |
| 7 |
name: su |
| 8 |
short_description: Substitute User |
| 9 |
description: |
| 10 |
- This become plugin allows your remote/login user to execute commands as another user via the su utility. |
| 11 |
author: ansible (@core) |
| 12 |
version_added: "2.8" |
| 13 |
options: |
| 14 |
become_user: |
| 15 |
description: User you 'become' to execute the task |
| 16 |
default: root |
| 17 |
ini: |
| 18 |
- section: privilege_escalation |
| 19 |
key: become_user |
| 20 |
- section: su_become_plugin |
| 21 |
key: user |
| 22 |
vars: |
| 23 |
- name: ansible_become_user |
| 24 |
- name: ansible_su_user |
| 25 |
env: |
| 26 |
- name: ANSIBLE_BECOME_USER |
| 27 |
- name: ANSIBLE_SU_USER |
| 28 |
keyword: |
| 29 |
- name: become_user |
| 30 |
become_exe: |
| 31 |
description: Su executable |
| 32 |
default: su |
| 33 |
ini: |
| 34 |
- section: privilege_escalation |
| 35 |
key: become_exe |
| 36 |
- section: su_become_plugin |
| 37 |
key: executable |
| 38 |
vars: |
| 39 |
- name: ansible_become_exe |
| 40 |
- name: ansible_su_exe |
| 41 |
env: |
| 42 |
- name: ANSIBLE_BECOME_EXE |
| 43 |
- name: ANSIBLE_SU_EXE |
| 44 |
keyword: |
| 45 |
- name: become_exe |
| 46 |
become_flags: |
| 47 |
description: Options to pass to su |
| 48 |
default: '' |
| 49 |
ini: |
| 50 |
- section: privilege_escalation |
| 51 |
key: become_flags |
| 52 |
- section: su_become_plugin |
| 53 |
key: flags |
| 54 |
vars: |
| 55 |
- name: ansible_become_flags |
| 56 |
- name: ansible_su_flags |
| 57 |
env: |
| 58 |
- name: ANSIBLE_BECOME_FLAGS |
| 59 |
- name: ANSIBLE_SU_FLAGS |
| 60 |
keyword: |
| 61 |
- name: become_flags |
| 62 |
become_pass: |
| 63 |
description: Password to pass to su |
| 64 |
type: str |
| 65 |
secret: true |
| 66 |
required: False |
| 67 |
vars: |
| 68 |
- name: ansible_become_password |
| 69 |
- name: ansible_become_pass |
| 70 |
- name: ansible_su_pass |
| 71 |
env: |
| 72 |
- name: ANSIBLE_BECOME_PASS |
| 73 |
- name: ANSIBLE_SU_PASS |
| 74 |
ini: |
| 75 |
- section: su_become_plugin |
| 76 |
key: password |
| 77 |
prompt_l10n: |
| 78 |
description: |
| 79 |
- List of localized strings to match for prompt detection |
| 80 |
- If empty we'll use the built in one |
| 81 |
- Do NOT add a colon (:) to your custom entries. Ansible adds a colon at the end of each prompt; |
| 82 |
if you add another one in your string, your prompt will fail with a "Timeout" error. |
| 83 |
default: [] |
| 84 |
type: list |
| 85 |
elements: string |
| 86 |
ini: |
| 87 |
- section: su_become_plugin |
| 88 |
key: localized_prompts |
| 89 |
vars: |
| 90 |
- name: ansible_su_prompt_l10n |
| 91 |
env: |
| 92 |
- name: ANSIBLE_SU_PROMPT_L10N |
| 93 |
""" |
| 94 |
|
| 95 |
import re |
| 96 |
import shlex |
| 97 |
|
| 98 |
from ansible.module_utils.common.text.converters import to_text |
| 99 |
from ansible.plugins.become import BecomeBase |
| 100 |
|
| 101 |
|
| 102 |
class BecomeModule(BecomeBase): |
| 103 |
|
| 104 |
name = 'su' |
| 105 |
|
| 106 |
pipelining = False |
| 107 |
|
| 108 |
# messages for detecting prompted password issues |
| 109 |
fail = ( |
| 110 |
'Authentication failure', |
| 111 |
'incorrect password', # BusyBox |
| 112 |
) |
| 113 |
|
| 114 |
SU_PROMPT_LOCALIZATIONS = [ |
| 115 |
'Password', |
| 116 |
'암호', |
| 117 |
'パスワード', |
| 118 |
'Adgangskode', |
| 119 |
'Contraseña', |
| 120 |
'Contrasenya', |
| 121 |
'Hasło', |
| 122 |
'Heslo', |
| 123 |
'Jelszó', |
| 124 |
'Lösenord', |
| 125 |
'Mật khẩu', |
| 126 |
'Mot de passe', |
| 127 |
'Parola', |
| 128 |
'Parool', |
| 129 |
'Pasahitza', |
| 130 |
'Passord', |
| 131 |
'Passwort', |
| 132 |
'Salasana', |
| 133 |
'Sandi', |
| 134 |
'Senha', |
| 135 |
'Wachtwoord', |
| 136 |
'ססמה', |
| 137 |
'Лозинка', |
| 138 |
'Парола', |
| 139 |
'Пароль', |
| 140 |
'गुप्तशब्द', |
| 141 |
'शब्दकूट', |
| 142 |
'సంకేతపదము', |
| 143 |
'හස්පදය', |
| 144 |
'密码', |
| 145 |
'密碼', |
| 146 |
'口令', |
| 147 |
] |
| 148 |
|
| 149 |
def check_password_prompt(self, b_output: bytes) -> bool: |
| 150 |
""" checks if the expected password prompt exists in b_output """ |
| 151 |
prompts = self.get_option('prompt_l10n') or self.SU_PROMPT_LOCALIZATIONS |
| 152 |
password_prompt_strings = "|".join(re.escape(p) for p in prompts) |
| 153 |
# Colon or unicode fullwidth colon |
| 154 |
prompt_pattern = rf"(?:{password_prompt_strings})\s*[::]" |
| 155 |
match = re.search(prompt_pattern, to_text(b_output), flags=re.IGNORECASE) |
| 156 |
|
| 157 |
if match: |
| 158 |
self.prompt = match.group(0) # preserve the actual matched string so we can scrub the output |
| 159 |
|
| 160 |
return bool(match) |
| 161 |
|
| 162 |
def build_become_command(self, cmd, shell): |
| 163 |
super(BecomeModule, self).build_become_command(cmd, shell) |
| 164 |
|
| 165 |
# Prompt handling for ``su`` is more complicated, this |
| 166 |
# is used to satisfy the connection plugin |
| 167 |
self.prompt = True |
| 168 |
|
| 169 |
if not cmd: |
| 170 |
return cmd |
| 171 |
|
| 172 |
exe = self.get_option('become_exe') or self.name |
| 173 |
flags = self.get_option('become_flags') or '' |
| 174 |
user = self.get_option('become_user') or '' |
| 175 |
success_cmd = self._build_success_command(cmd, shell) |
| 176 |
|
| 177 |
return "%s %s %s -c %s" % (exe, flags, user, shlex.quote(success_cmd)) |