| 1 |
# Copyright 2015 Abhijit Menon-Sen <[email protected]> |
| 2 |
# |
| 3 |
# This file is part of Ansible |
| 4 |
# |
| 5 |
# Ansible is free software: you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation, either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# Ansible is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
from __future__ import annotations |
| 19 |
|
| 20 |
import re |
| 21 |
from ansible.errors import AnsibleParserError, AnsibleError |
| 22 |
|
| 23 |
# Components that match a numeric or alphanumeric begin:end or begin:end:step |
| 24 |
# range expression inside square brackets. |
| 25 |
|
| 26 |
numeric_range = r""" |
| 27 |
\[ |
| 28 |
(?:[0-9]+:[0-9]+) # numeric begin:end |
| 29 |
(?::[0-9]+)? # numeric :step (optional) |
| 30 |
\] |
| 31 |
""" |
| 32 |
|
| 33 |
hexadecimal_range = r""" |
| 34 |
\[ |
| 35 |
(?:[0-9a-f]+:[0-9a-f]+) # hexadecimal begin:end |
| 36 |
(?::[0-9]+)? # numeric :step (optional) |
| 37 |
\] |
| 38 |
""" |
| 39 |
|
| 40 |
alphanumeric_range = r""" |
| 41 |
\[ |
| 42 |
(?: |
| 43 |
[a-z]:[a-z]| # one-char alphabetic range |
| 44 |
[0-9]+:[0-9]+ # ...or a numeric one |
| 45 |
) |
| 46 |
(?::[0-9]+)? # numeric :step (optional) |
| 47 |
\] |
| 48 |
""" |
| 49 |
|
| 50 |
# Components that match a 16-bit portion of an IPv6 address in hexadecimal |
| 51 |
# notation (0..ffff) or an 8-bit portion of an IPv4 address in decimal notation |
| 52 |
# (0..255) or an [x:y(:z)] numeric range. |
| 53 |
|
| 54 |
ipv6_component = r""" |
| 55 |
(?: |
| 56 |
[0-9a-f]{{1,4}}| # 0..ffff |
| 57 |
{range} # or a numeric range |
| 58 |
) |
| 59 |
""".format(range=hexadecimal_range) |
| 60 |
|
| 61 |
ipv4_component = r""" |
| 62 |
(?: |
| 63 |
[01]?[0-9]{{1,2}}| # 0..199 |
| 64 |
2[0-4][0-9]| # 200..249 |
| 65 |
25[0-5]| # 250..255 |
| 66 |
{range} # or a numeric range |
| 67 |
) |
| 68 |
""".format(range=numeric_range) |
| 69 |
|
| 70 |
# A hostname label, e.g. 'foo' in 'foo.example.com'. Consists of alphanumeric |
| 71 |
# characters plus dashes (and underscores) or valid ranges. The label may not |
| 72 |
# start or end with a hyphen or an underscore. This is interpolated into the |
| 73 |
# hostname pattern below. We don't try to enforce the 63-char length limit. |
| 74 |
|
| 75 |
label = r""" |
| 76 |
(?:[\w]|{range}) # Starts with an alphanumeric or a range |
| 77 |
(?:[\w_-]|{range})* # Then zero or more of the same or [_-] |
| 78 |
(?<![_-]) # ...as long as it didn't end with [_-] |
| 79 |
""".format(range=alphanumeric_range) |
| 80 |
|
| 81 |
patterns = { |
| 82 |
# This matches a square-bracketed expression with a port specification. What |
| 83 |
# is inside the square brackets is validated later. |
| 84 |
|
| 85 |
'bracketed_hostport': re.compile( |
| 86 |
r"""^ |
| 87 |
\[(.+)\] # [host identifier] |
| 88 |
:([0-9]+) # :port number |
| 89 |
$ |
| 90 |
""", re.X |
| 91 |
), |
| 92 |
|
| 93 |
# This matches a bare IPv4 address or hostname (or host pattern including |
| 94 |
# [x:y(:z)] ranges) with a port specification. |
| 95 |
|
| 96 |
'hostport': re.compile( |
| 97 |
r"""^ |
| 98 |
((?: # We want to match: |
| 99 |
[^:\[\]] # (a non-range character |
| 100 |
| # ...or... |
| 101 |
\[[^\]]*\] # a complete bracketed expression) |
| 102 |
)*) # repeated as many times as possible |
| 103 |
:([0-9]+) # followed by a port number |
| 104 |
$ |
| 105 |
""", re.X |
| 106 |
), |
| 107 |
|
| 108 |
# This matches an IPv4 address, but also permits range expressions. |
| 109 |
|
| 110 |
'ipv4': re.compile( |
| 111 |
r"""^ |
| 112 |
(?:{i4}\.){{3}}{i4} # Three parts followed by dots plus one |
| 113 |
$ |
| 114 |
""".format(i4=ipv4_component), re.X | re.I |
| 115 |
), |
| 116 |
|
| 117 |
# This matches an IPv6 address, but also permits range expressions. |
| 118 |
# |
| 119 |
# This expression looks complex, but it really only spells out the various |
| 120 |
# combinations in which the basic unit of an IPv6 address (0..ffff) can be |
| 121 |
# written, from :: to 1:2:3:4:5:6:7:8, plus the IPv4-in-IPv6 variants such |
| 122 |
# as ::ffff:192.0.2.3. |
| 123 |
# |
| 124 |
# Note that we can't just use ipaddress.ip_address() because we also have to |
| 125 |
# accept ranges in place of each component. |
| 126 |
|
| 127 |
'ipv6': re.compile( |
| 128 |
r"""^ |
| 129 |
((?:{0}:){{7}}{0}| # uncompressed: 1:2:3:4:5:6:7:8 |
| 130 |
(?:{0}:){{1,6}}:| # compressed variants, which are all |
| 131 |
(?:{0}:)(?::{0}){{1,6}}| # a::b for various lengths of a,b |
| 132 |
(?:{0}:){{2}}(?::{0}){{1,5}}| |
| 133 |
(?:{0}:){{3}}(?::{0}){{1,4}}| |
| 134 |
(?:{0}:){{4}}(?::{0}){{1,3}}| |
| 135 |
(?:{0}:){{5}}(?::{0}){{1,2}}| |
| 136 |
(?:{0}:){{6}}(?::{0})| # ...all with 2 <= a+b <= 7 |
| 137 |
:(?::{0}){{1,6}}| # ::ffff(:ffff...) |
| 138 |
{0}?::| # ffff::, :: |
| 139 |
# ipv4-in-ipv6 variants |
| 140 |
(?:0:){{6}}(?:{0}\.){{3}}{0}| |
| 141 |
::(?:ffff:)?(?:{0}\.){{3}}{0}| |
| 142 |
(?:0:){{5}}ffff:(?:{0}\.){{3}}{0}) |
| 143 |
$ |
| 144 |
""".format(ipv6_component), re.X | re.I |
| 145 |
), |
| 146 |
|
| 147 |
# This matches a hostname or host pattern including [x:y(:z)] ranges. |
| 148 |
# |
| 149 |
# We roughly follow DNS rules here, but also allow ranges (and underscores). |
| 150 |
# In the past, no systematic rules were enforced about inventory hostnames, |
| 151 |
# but the parsing context (e.g. shlex.split(), fnmatch.fnmatch()) excluded |
| 152 |
# various metacharacters anyway. |
| 153 |
# |
| 154 |
# We don't enforce DNS length restrictions here (63 characters per label, |
| 155 |
# 253 characters total) or make any attempt to process IDNs. |
| 156 |
|
| 157 |
'hostname': re.compile( |
| 158 |
r"""^ |
| 159 |
{label} # We must have at least one label |
| 160 |
(?:\.{label})* # Followed by zero or more .labels |
| 161 |
$ |
| 162 |
""".format(label=label), re.X | re.I | re.UNICODE |
| 163 |
), |
| 164 |
|
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
def parse_address(address, allow_ranges=False): |
| 169 |
""" |
| 170 |
Takes a string and returns a (host, port) tuple. If the host is None, then |
| 171 |
the string could not be parsed as a host identifier with an optional port |
| 172 |
specification. If the port is None, then no port was specified. |
| 173 |
|
| 174 |
The host identifier may be a hostname (qualified or not), an IPv4 address, |
| 175 |
or an IPv6 address. If allow_ranges is True, then any of those may contain |
| 176 |
[x:y] range specifications, e.g. foo[1:3] or foo[0:5]-bar[x-z]. |
| 177 |
|
| 178 |
The port number is an optional :NN suffix on an IPv4 address or host name, |
| 179 |
or a mandatory :NN suffix on any square-bracketed expression: IPv6 address, |
| 180 |
IPv4 address, or host name. (This means the only way to specify a port for |
| 181 |
an IPv6 address is to enclose it in square brackets.) |
| 182 |
""" |
| 183 |
|
| 184 |
# First, we extract the port number if one is specified. |
| 185 |
|
| 186 |
port = None |
| 187 |
for matching in ['bracketed_hostport', 'hostport']: |
| 188 |
m = patterns[matching].match(address) |
| 189 |
if m: |
| 190 |
(address, port) = m.groups() |
| 191 |
port = int(port) |
| 192 |
continue |
| 193 |
|
| 194 |
# What we're left with now must be an IPv4 or IPv6 address, possibly with |
| 195 |
# numeric ranges, or a hostname with alphanumeric ranges. |
| 196 |
|
| 197 |
host = None |
| 198 |
for matching in ['ipv4', 'ipv6', 'hostname']: |
| 199 |
m = patterns[matching].match(address) |
| 200 |
if m: |
| 201 |
host = address |
| 202 |
continue |
| 203 |
|
| 204 |
# If it isn't any of the above, we don't understand it. |
| 205 |
if not host: |
| 206 |
raise AnsibleError("Not a valid network hostname: %s" % address) |
| 207 |
|
| 208 |
# If we get to this point, we know that any included ranges are valid. |
| 209 |
# If the caller is prepared to handle them, all is well. |
| 210 |
# Otherwise we treat it as a parse failure. |
| 211 |
if not allow_ranges and '[' in host: |
| 212 |
raise AnsibleParserError("Detected range in host but was asked to ignore ranges") |
| 213 |
|
| 214 |
return (host, port) |