| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright (c) 2021, Felix Fontein (@felixfontein) <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
|
| 11 |
import sys |
| 12 |
|
| 13 |
from ansible.module_utils.common.text.converters import to_native, to_bytes |
| 14 |
|
| 15 |
|
| 16 |
class ParseError(Exception): |
| 17 |
pass |
| 18 |
|
| 19 |
|
| 20 |
ESCAPE_SEQUENCES = { |
| 21 |
b'"': b'"', |
| 22 |
b'\\': b'\\', |
| 23 |
b'?': b'?', |
| 24 |
b'$': b'$', |
| 25 |
b'_': b' ', |
| 26 |
b'a': b'\a', |
| 27 |
b'b': b'\b', |
| 28 |
b'f': b'\xFF', |
| 29 |
b'n': b'\n', |
| 30 |
b'r': b'\r', |
| 31 |
b't': b'\t', |
| 32 |
b'v': b'\v', |
| 33 |
} |
| 34 |
|
| 35 |
ESCAPE_SEQUENCE_REVERSED = dict([(v, k) for k, v in ESCAPE_SEQUENCES.items()]) |
| 36 |
|
| 37 |
ESCAPE_DIGITS = b'0123456789ABCDEF' |
| 38 |
|
| 39 |
|
| 40 |
if sys.version_info[0] < 3: |
| 41 |
_int_to_byte = chr |
| 42 |
else: |
| 43 |
def _int_to_byte(value): |
| 44 |
return bytes((value, )) |
| 45 |
|
| 46 |
|
| 47 |
def parse_argument_value(line, start_index=0, must_match_everything=True): |
| 48 |
''' |
| 49 |
Parse an argument value (quoted or not quoted) from ``line``. |
| 50 |
|
| 51 |
Will start at offset ``start_index``. Returns pair ``(parsed_value, |
| 52 |
end_index)``, where ``end_index`` is the first character after the |
| 53 |
attribute. |
| 54 |
|
| 55 |
If ``must_match_everything`` is ``True`` (default), will fail if |
| 56 |
``end_index < len(line)``. |
| 57 |
''' |
| 58 |
line = to_bytes(line) |
| 59 |
length = len(line) |
| 60 |
index = start_index |
| 61 |
if index == length: |
| 62 |
raise ParseError('Expected value, but found end of string') |
| 63 |
quoted = False |
| 64 |
if line[index:index + 1] == b'"': |
| 65 |
quoted = True |
| 66 |
index += 1 |
| 67 |
current = [] |
| 68 |
while index < length: |
| 69 |
ch = line[index:index + 1] |
| 70 |
index += 1 |
| 71 |
if not quoted and ch == b' ': |
| 72 |
index -= 1 |
| 73 |
break |
| 74 |
elif ch == b'"': |
| 75 |
if quoted: |
| 76 |
quoted = False |
| 77 |
if line[index:index + 1] not in (b'', b' '): |
| 78 |
raise ParseError('Ending \'"\' must be followed by space or end of string') |
| 79 |
break |
| 80 |
raise ParseError('\'"\' must not appear in an unquoted value') |
| 81 |
elif ch == b'\\': |
| 82 |
if not quoted: |
| 83 |
raise ParseError('Escape sequences can only be used inside double quotes') |
| 84 |
if index == length: |
| 85 |
raise ParseError('\'\\\' must not be at the end of the line') |
| 86 |
ch = line[index:index + 1] |
| 87 |
index += 1 |
| 88 |
if ch in ESCAPE_SEQUENCES: |
| 89 |
current.append(ESCAPE_SEQUENCES[ch]) |
| 90 |
else: |
| 91 |
d1 = ESCAPE_DIGITS.find(ch) |
| 92 |
if d1 < 0: |
| 93 |
raise ParseError('Invalid escape sequence \'\\{0}\''.format(to_native(ch))) |
| 94 |
if index == length: |
| 95 |
raise ParseError('Hex escape sequence cut off at end of line') |
| 96 |
ch2 = line[index:index + 1] |
| 97 |
d2 = ESCAPE_DIGITS.find(ch2) |
| 98 |
index += 1 |
| 99 |
if d2 < 0: |
| 100 |
raise ParseError('Invalid hex escape sequence \'\\{0}\''.format(to_native(ch + ch2))) |
| 101 |
current.append(_int_to_byte(d1 * 16 + d2)) |
| 102 |
else: |
| 103 |
if not quoted and ch in (b"'", b'=', b'(', b')', b'$', b'[', b'{', b'`'): |
| 104 |
raise ParseError('"{0}" can only be used inside double quotes'.format(to_native(ch))) |
| 105 |
if ch == b'?': |
| 106 |
raise ParseError('"{0}" can only be used in escaped form'.format(to_native(ch))) |
| 107 |
current.append(ch) |
| 108 |
if quoted: |
| 109 |
raise ParseError('Unexpected end of string during escaped parameter') |
| 110 |
if must_match_everything and index < length: |
| 111 |
raise ParseError('Unexpected data at end of value') |
| 112 |
return to_native(b''.join(current)), index |
| 113 |
|
| 114 |
|
| 115 |
def split_routeros_command(line): |
| 116 |
line = to_bytes(line) |
| 117 |
result = [] |
| 118 |
current = [] |
| 119 |
index = 0 |
| 120 |
length = len(line) |
| 121 |
parsing_attribute_name = False |
| 122 |
while index < length: |
| 123 |
ch = line[index:index + 1] |
| 124 |
index += 1 |
| 125 |
if ch == b' ': |
| 126 |
if parsing_attribute_name: |
| 127 |
parsing_attribute_name = False |
| 128 |
result.append(b''.join(current)) |
| 129 |
current = [] |
| 130 |
elif ch == b'=' and parsing_attribute_name: |
| 131 |
current.append(ch) |
| 132 |
value, index = parse_argument_value(line, start_index=index, must_match_everything=False) |
| 133 |
current.append(to_bytes(value)) |
| 134 |
parsing_attribute_name = False |
| 135 |
result.append(b''.join(current)) |
| 136 |
current = [] |
| 137 |
elif ch in (b'"', b'\\', b"'", b'=', b'(', b')', b'$', b'[', b'{', b'`', b'?'): |
| 138 |
raise ParseError('Found unexpected "{0}"'.format(to_native(ch))) |
| 139 |
else: |
| 140 |
current.append(ch) |
| 141 |
parsing_attribute_name = True |
| 142 |
if parsing_attribute_name and current: |
| 143 |
result.append(b''.join(current)) |
| 144 |
return [to_native(part) for part in result] |
| 145 |
|
| 146 |
|
| 147 |
def quote_routeros_argument_value(argument): |
| 148 |
argument = to_bytes(argument) |
| 149 |
result = [] |
| 150 |
quote = False |
| 151 |
length = len(argument) |
| 152 |
index = 0 |
| 153 |
while index < length: |
| 154 |
letter = argument[index:index + 1] |
| 155 |
index += 1 |
| 156 |
if letter in ESCAPE_SEQUENCE_REVERSED: |
| 157 |
result.append(b'\\%s' % ESCAPE_SEQUENCE_REVERSED[letter]) |
| 158 |
quote = True |
| 159 |
continue |
| 160 |
elif ord(letter) < 32: |
| 161 |
v = ord(letter) |
| 162 |
v1 = v % 16 |
| 163 |
v2 = v // 16 |
| 164 |
result.append(b'\\%s%s' % (ESCAPE_DIGITS[v2:v2 + 1], ESCAPE_DIGITS[v1:v1 + 1])) |
| 165 |
quote = True |
| 166 |
continue |
| 167 |
elif letter in (b' ', b'=', b';', b"'"): |
| 168 |
quote = True |
| 169 |
result.append(letter) |
| 170 |
argument = to_native(b''.join(result)) |
| 171 |
if quote or not argument: |
| 172 |
argument = '"%s"' % argument |
| 173 |
return argument |
| 174 |
|
| 175 |
|
| 176 |
def quote_routeros_argument(argument): |
| 177 |
def check_attribute(attribute): |
| 178 |
if ' ' in attribute: |
| 179 |
raise ParseError('Attribute names must not contain spaces') |
| 180 |
return attribute |
| 181 |
|
| 182 |
if '=' not in argument: |
| 183 |
check_attribute(argument) |
| 184 |
return argument |
| 185 |
|
| 186 |
attribute, value = argument.split('=', 1) |
| 187 |
check_attribute(attribute) |
| 188 |
value = quote_routeros_argument_value(value) |
| 189 |
return '%s=%s' % (attribute, value) |
| 190 |
|
| 191 |
|
| 192 |
def join_routeros_command(arguments): |
| 193 |
return ' '.join([quote_routeros_argument(argument) for argument in arguments]) |
| 194 |
|
| 195 |
|
| 196 |
def convert_list_to_dictionary(string_list, require_assignment=True, skip_empty_values=False): |
| 197 |
dictionary = {} |
| 198 |
for p in string_list: |
| 199 |
if '=' not in p: |
| 200 |
if require_assignment: |
| 201 |
raise ParseError("missing '=' after '%s'" % p) |
| 202 |
dictionary[p] = None |
| 203 |
continue |
| 204 |
p = p.split('=', 1) |
| 205 |
if not skip_empty_values or p[1]: |
| 206 |
dictionary[p[0]] = p[1] |
| 207 |
return dictionary |