| 1 |
# |
| 2 |
# Copyright (C) 2016 FreeIPA Contributors see COPYING for license |
| 3 |
# |
| 4 |
|
| 5 |
""" |
| 6 |
classes/utils for Kerberos principal name validation/manipulation |
| 7 |
""" |
| 8 |
import re |
| 9 |
import six |
| 10 |
|
| 11 |
from ipapython.ipautil import escape_seq, unescape_seq |
| 12 |
|
| 13 |
if six.PY3: |
| 14 |
unicode = str |
| 15 |
|
| 16 |
REALM_SPLIT_RE = re.compile(r'(?<!\\)@') |
| 17 |
COMPONENT_SPLIT_RE = re.compile(r'(?<!\\)/') |
| 18 |
|
| 19 |
|
| 20 |
def parse_princ_name_and_realm(principal, realm=None): |
| 21 |
""" |
| 22 |
split principal to the <principal_name>, <realm> components |
| 23 |
|
| 24 |
:param principal: unicode representation of principal |
| 25 |
:param realm: if not None, replace the parsed realm with the specified one |
| 26 |
|
| 27 |
:returns: tuple containing the principal name and realm |
| 28 |
realm will be `None` if no realm was found in the input string |
| 29 |
""" |
| 30 |
realm_and_name = REALM_SPLIT_RE.split(principal) |
| 31 |
if len(realm_and_name) > 2: |
| 32 |
raise ValueError( |
| 33 |
"Principal is not in <name>@<realm> format") |
| 34 |
|
| 35 |
principal_name = realm_and_name[0] |
| 36 |
|
| 37 |
try: |
| 38 |
parsed_realm = realm_and_name[1] |
| 39 |
except IndexError: |
| 40 |
parsed_realm = None if realm is None else realm |
| 41 |
|
| 42 |
return principal_name, parsed_realm |
| 43 |
|
| 44 |
|
| 45 |
def split_principal_name(principal_name): |
| 46 |
""" |
| 47 |
Split principal name (without realm) into the components |
| 48 |
|
| 49 |
NOTE: operates on the following RFC 1510 types: |
| 50 |
* NT-PRINCIPAL |
| 51 |
* NT-SRV-INST |
| 52 |
* NT-SRV-HST |
| 53 |
|
| 54 |
Enterprise principals (NT-ENTERPRISE, see RFC 6806) are also handled |
| 55 |
|
| 56 |
:param principal_name: unicode representation of principal name |
| 57 |
:returns: tuple of individual components (i.e. primary name for |
| 58 |
NT-PRINCIPAL and NT-ENTERPRISE, primary name and instance for others) |
| 59 |
""" |
| 60 |
return tuple(COMPONENT_SPLIT_RE.split(principal_name)) |
| 61 |
|
| 62 |
|
| 63 |
@six.python_2_unicode_compatible |
| 64 |
class Principal: |
| 65 |
""" |
| 66 |
Container for the principal name and realm according to RFC 1510 |
| 67 |
""" |
| 68 |
def __init__(self, components, realm=None): |
| 69 |
if isinstance(components, bytes): |
| 70 |
raise TypeError( |
| 71 |
"Cannot create a principal object from bytes: {!r}".format( |
| 72 |
components) |
| 73 |
) |
| 74 |
elif isinstance(components, str): |
| 75 |
# parse principal components from realm |
| 76 |
self.components, self.realm = self._parse_from_text( |
| 77 |
components, realm) |
| 78 |
|
| 79 |
elif isinstance(components, Principal): |
| 80 |
self.components = components.components |
| 81 |
self.realm = components.realm if realm is None else realm |
| 82 |
else: |
| 83 |
self.components = tuple(components) |
| 84 |
self.realm = realm |
| 85 |
|
| 86 |
def __eq__(self, other): |
| 87 |
if not isinstance(other, Principal): |
| 88 |
return False |
| 89 |
|
| 90 |
return (self.components == other.components and |
| 91 |
self.realm == other.realm) |
| 92 |
|
| 93 |
def __ne__(self, other): |
| 94 |
return not self.__eq__(other) |
| 95 |
|
| 96 |
def __lt__(self, other): |
| 97 |
return unicode(self) < unicode(other) |
| 98 |
|
| 99 |
def __le__(self, other): |
| 100 |
return self.__lt__(other) or self.__eq__(other) |
| 101 |
|
| 102 |
def __gt__(self, other): |
| 103 |
return not self.__le__(other) |
| 104 |
|
| 105 |
def __ge__(self, other): |
| 106 |
return self.__gt__(other) or self.__eq__(other) |
| 107 |
|
| 108 |
def __hash__(self): |
| 109 |
return hash(self.components + (self.realm,)) |
| 110 |
|
| 111 |
def _parse_from_text(self, principal, realm=None): |
| 112 |
r""" |
| 113 |
parse individual principal name components from the string |
| 114 |
representation of the principal. This is done in three steps: |
| 115 |
1.) split the string at the unescaped '@' |
| 116 |
2.) unescape any leftover '\@' sequences |
| 117 |
3.) split the primary at the unescaped '/' |
| 118 |
4.) unescape leftover '\/' |
| 119 |
:param principal: unicode representation of the principal name |
| 120 |
:param realm: if not None, this realm name will be used instead of the |
| 121 |
one parsed from `principal` |
| 122 |
|
| 123 |
:returns: tuple containing the principal name components and realm |
| 124 |
""" |
| 125 |
principal_name, parsed_realm = parse_princ_name_and_realm( |
| 126 |
principal, realm=realm) |
| 127 |
|
| 128 |
(principal_name,) = unescape_seq(u'@', principal_name) |
| 129 |
|
| 130 |
if parsed_realm is not None: |
| 131 |
(parsed_realm,) = unescape_seq(u'@', parsed_realm) |
| 132 |
|
| 133 |
name_components = split_principal_name(principal_name) |
| 134 |
name_components = unescape_seq(u'/', *name_components) |
| 135 |
|
| 136 |
return name_components, parsed_realm |
| 137 |
|
| 138 |
@property |
| 139 |
def is_user(self): |
| 140 |
return len(self.components) == 1 |
| 141 |
|
| 142 |
@property |
| 143 |
def is_enterprise(self): |
| 144 |
return self.is_user and u'@' in self.components[0] |
| 145 |
|
| 146 |
@property |
| 147 |
def is_service(self): |
| 148 |
return len(self.components) > 1 |
| 149 |
|
| 150 |
@property |
| 151 |
def is_host(self): |
| 152 |
return (self.is_service and len(self.components) == 2 and |
| 153 |
self.components[0] == u'host') |
| 154 |
|
| 155 |
@property |
| 156 |
def username(self): |
| 157 |
if self.is_user: |
| 158 |
return self.components[0] |
| 159 |
else: |
| 160 |
raise ValueError( |
| 161 |
"User name is defined only for user and enterprise principals") |
| 162 |
|
| 163 |
@property |
| 164 |
def upn_suffix(self): |
| 165 |
if not self.is_enterprise: |
| 166 |
raise ValueError("Only enterprise principals have UPN suffix") |
| 167 |
|
| 168 |
return self.components[0].split(u'@')[1] |
| 169 |
|
| 170 |
@property |
| 171 |
def hostname(self): |
| 172 |
if not (self.is_host or self.is_service): |
| 173 |
raise ValueError( |
| 174 |
"hostname is defined for host and service principals") |
| 175 |
return self.components[-1] |
| 176 |
|
| 177 |
@property |
| 178 |
def service_name(self): |
| 179 |
if not self.is_service: |
| 180 |
raise ValueError( |
| 181 |
"Only service principals have meaningful service name") |
| 182 |
|
| 183 |
return u'/'.join(c for c in escape_seq('/', *self.components[:-1])) |
| 184 |
|
| 185 |
def __str__(self): |
| 186 |
""" |
| 187 |
return the unicode representation of principal |
| 188 |
|
| 189 |
works in reverse of the `from_text` class method |
| 190 |
""" |
| 191 |
name_components = escape_seq(u'/', *self.components) |
| 192 |
name_components = escape_seq(u'@', *name_components) |
| 193 |
|
| 194 |
principal_string = u'/'.join(name_components) |
| 195 |
|
| 196 |
if self.realm is not None: |
| 197 |
(realm,) = escape_seq(u'@', self.realm) |
| 198 |
principal_string = u'@'.join([principal_string, realm]) |
| 199 |
|
| 200 |
return principal_string |
| 201 |
|
| 202 |
def __repr__(self): |
| 203 |
return "{0.__module__}.{0.__name__}('{1}')".format( |
| 204 |
self.__class__, self) |