1# Copyright: (c) 2019, Ansible Project
2# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3
4from __future__ import (absolute_import, division, print_function)
5__metaclass__ = type
6
7import re
8
9from ansible.plugins.terminal import TerminalBase
10from ansible.errors import AnsibleConnectionFailure
11from ansible.module_utils._text import to_text, to_bytes
12import json
13
14
15class TerminalModule(TerminalBase):
16
17    terminal_stdout_re = [
18        re.compile(br"[\r\n]?[\w\+\-\.:\/\[\]]+(?:\([^\)]+\)){0,3}(?:[>#]) ?$")
19    ]
20
21    terminal_stderr_re = [
22        re.compile(br"% ?Error"),
23        re.compile(br"% ?Bad secret"),
24        re.compile(br"[\r\n%] Bad passwords"),
25        re.compile(br"invalid input", re.I),
26        re.compile(br"(?:incomplete|ambiguous) command", re.I),
27        re.compile(br"connection timed out", re.I),
28        re.compile(br"[^\r\n]+ not found"),
29        re.compile(br"'[^']' +returned error code: ?\d+"),
30        re.compile(br"Bad mask", re.I),
31        re.compile(br"% ?(\S+) ?overlaps with ?(\S+)", re.I),
32        re.compile(br"[%\S] ?Error: ?[\s]+", re.I),
33        re.compile(br"[%\S] ?Informational: ?[\s]+", re.I),
34        re.compile(br"Command authorization failed"),
35        re.compile(br"Error - *"),
36        re.compile(br"Error - Incorrect username or password."),
37        re.compile(br"Invalid input"),
38        re.compile(br"Already a http operation is in progress"),
39        re.compile(br"Flash access in progress. Please try later"),
40        re.compile(br"Error: .*"),
41        re.compile(br"^Error: .*", re.I),
42        re.compile(br"^Ambiguous input"),
43        re.compile(br"Errno")
44    ]
45
46    def on_open_shell(self):
47        pass
48
49    def __del__(self):
50        try:
51            self.close()
52        except AnsibleConnectionFailure:
53            raise AnsibleConnectionFailure('unable to set terminal parameters')
54
55    def on_become(self, passwd=None):
56        if self._get_prompt().endswith(b'#'):
57            return
58
59        cmd = {u'command': u'enable'}
60        cmd[u'prompt'] = to_text(r"[\r\n](?:Local_)?[Pp]assword: ?$", errors='surrogate_or_strict')
61        cmd[u'answer'] = passwd
62        cmd[u'prompt_retry_check'] = True
63        try:
64            self._exec_cli_command(to_bytes(json.dumps(cmd), errors='surrogate_or_strict'))
65            prompt = self._get_prompt()
66            if prompt is None or not prompt.endswith(b'#'):
67                raise AnsibleConnectionFailure('failed to elevate privilege to enable mode still at prompt [%s]' % prompt)
68        except AnsibleConnectionFailure as e:
69            prompt = self._get_prompt()
70            raise AnsibleConnectionFailure('unable to elevate privilege to enable mode, at prompt [%s] with error: %s' % (prompt, e.message))
71
72    def on_unbecome(self):
73        prompt = self._get_prompt()
74        if prompt is None:
75            return
76
77        if b'(config' in prompt:
78            self._exec_cli_command(b'exit')
79
80        elif prompt.endswith(b'#'):
81            self._exec_cli_command(b'exit')
82