| 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: doas |
| 8 |
short_description: Do As user |
| 9 |
description: |
| 10 |
- This become plugins allows your remote/login user to execute commands as another user using the C(doas) 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: doas_become_plugin |
| 20 |
key: user |
| 21 |
vars: |
| 22 |
- name: ansible_become_user |
| 23 |
- name: ansible_doas_user |
| 24 |
env: |
| 25 |
- name: ANSIBLE_BECOME_USER |
| 26 |
- name: ANSIBLE_DOAS_USER |
| 27 |
become_exe: |
| 28 |
description: C(doas) executable. |
| 29 |
type: string |
| 30 |
default: doas |
| 31 |
ini: |
| 32 |
- section: privilege_escalation |
| 33 |
key: become_exe |
| 34 |
- section: doas_become_plugin |
| 35 |
key: executable |
| 36 |
vars: |
| 37 |
- name: ansible_become_exe |
| 38 |
- name: ansible_doas_exe |
| 39 |
env: |
| 40 |
- name: ANSIBLE_BECOME_EXE |
| 41 |
- name: ANSIBLE_DOAS_EXE |
| 42 |
become_flags: |
| 43 |
description: Options to pass to C(doas). |
| 44 |
type: string |
| 45 |
default: '' |
| 46 |
ini: |
| 47 |
- section: privilege_escalation |
| 48 |
key: become_flags |
| 49 |
- section: doas_become_plugin |
| 50 |
key: flags |
| 51 |
vars: |
| 52 |
- name: ansible_become_flags |
| 53 |
- name: ansible_doas_flags |
| 54 |
env: |
| 55 |
- name: ANSIBLE_BECOME_FLAGS |
| 56 |
- name: ANSIBLE_DOAS_FLAGS |
| 57 |
become_pass: |
| 58 |
description: Password for C(doas) prompt. |
| 59 |
type: string |
| 60 |
required: false |
| 61 |
vars: |
| 62 |
- name: ansible_become_password |
| 63 |
- name: ansible_become_pass |
| 64 |
- name: ansible_doas_pass |
| 65 |
env: |
| 66 |
- name: ANSIBLE_BECOME_PASS |
| 67 |
- name: ANSIBLE_DOAS_PASS |
| 68 |
ini: |
| 69 |
- section: doas_become_plugin |
| 70 |
key: password |
| 71 |
prompt_l10n: |
| 72 |
description: |
| 73 |
- List of localized strings to match for prompt detection. |
| 74 |
- If empty the plugin uses the built-in one. |
| 75 |
type: list |
| 76 |
elements: string |
| 77 |
default: [] |
| 78 |
ini: |
| 79 |
- section: doas_become_plugin |
| 80 |
key: localized_prompts |
| 81 |
vars: |
| 82 |
- name: ansible_doas_prompt_l10n |
| 83 |
env: |
| 84 |
- name: ANSIBLE_DOAS_PROMPT_L10N |
| 85 |
allow_pipelining: |
| 86 |
description: |
| 87 |
- When set to V(true), do allow pipelining with ansible-core 2.19+. |
| 88 |
- This should only be used when doas is configured to not ask for a password (C(nopass)). |
| 89 |
type: boolean |
| 90 |
default: false |
| 91 |
version_added: 12.4.0 |
| 92 |
ini: |
| 93 |
- section: doas_become_plugin |
| 94 |
key: allow_pipelining |
| 95 |
vars: |
| 96 |
- name: ansible_doas_allow_pipelining |
| 97 |
env: |
| 98 |
- name: ANSIBLE_DOAS_ALLOW_PIPELINING |
| 99 |
notes: |
| 100 |
- This become plugin does not work when connection pipelining is enabled |
| 101 |
and doas requests a password. |
| 102 |
With ansible-core 2.19+, using this plugin automatically disables pipelining, |
| 103 |
unless O(allow_pipelining=true) is explicitly set by the user. |
| 104 |
On ansible-core 2.18 and before, pipelining must explicitly be disabled by the user. |
| 105 |
""" |
| 106 |
|
| 107 |
import re |
| 108 |
|
| 109 |
from ansible.module_utils.common.text.converters import to_bytes |
| 110 |
from ansible.plugins.become import BecomeBase |
| 111 |
|
| 112 |
|
| 113 |
class BecomeModule(BecomeBase): |
| 114 |
name = "community.general.doas" |
| 115 |
|
| 116 |
# messages for detecting prompted password issues |
| 117 |
fail = ("Permission denied",) |
| 118 |
missing = ("Authorization required",) |
| 119 |
|
| 120 |
# See https://github.com/ansible-collections/community.general/issues/9977, |
| 121 |
# https://github.com/ansible/ansible/pull/78111, |
| 122 |
# https://github.com/ansible-collections/community.general/issues/11411 |
| 123 |
@property |
| 124 |
def pipelining(self) -> bool: # type: ignore[override] |
| 125 |
return self.get_option("allow_pipelining") |
| 126 |
|
| 127 |
def check_password_prompt(self, b_output): |
| 128 |
"""checks if the expected password prompt exists in b_output""" |
| 129 |
|
| 130 |
# FIXME: more accurate would be: 'doas (%s@' % remote_user |
| 131 |
# however become plugins don't have that information currently |
| 132 |
b_prompts = [to_bytes(p) for p in self.get_option("prompt_l10n")] or [rb"doas \(", rb"Password:"] |
| 133 |
b_prompt = b"|".join(b_prompts) |
| 134 |
|
| 135 |
return bool(re.match(b_prompt, b_output)) |
| 136 |
|
| 137 |
def build_become_command(self, cmd, shell): |
| 138 |
super().build_become_command(cmd, shell) |
| 139 |
|
| 140 |
if not cmd: |
| 141 |
return cmd |
| 142 |
|
| 143 |
self.prompt = True |
| 144 |
|
| 145 |
become_exe = self.get_option("become_exe") |
| 146 |
|
| 147 |
flags = self.get_option("become_flags") |
| 148 |
if not self.get_option("become_pass") and "-n" not in flags: |
| 149 |
flags += " -n" |
| 150 |
|
| 151 |
become_user = self.get_option("become_user") |
| 152 |
user = f"-u {become_user}" if become_user else "" |
| 153 |
|
| 154 |
success_cmd = self._build_success_command(cmd, shell, noexe=True) |
| 155 |
executable = getattr(shell, "executable", shell.SHELL_FAMILY) |
| 156 |
|
| 157 |
return f"{become_exe} {flags} {user} {executable} -c {success_cmd}" |