| 1 |
from typing import Optional, Type |
| 2 |
|
| 3 |
|
| 4 |
class LDAPError(Exception): |
| 5 |
"""General LDAP error.""" |
| 6 |
|
| 7 |
code = 0 |
| 8 |
|
| 9 |
@classmethod |
| 10 |
def create(cls, code: int) -> Type["LDAPError"]: |
| 11 |
""" Create a new LDAPError type with `code` error code. """ |
| 12 |
cls.code = code |
| 13 |
return cls |
| 14 |
|
| 15 |
@property |
| 16 |
def hexcode(self) -> int: |
| 17 |
""" Error code in 16 bit length hexadecimal format. """ |
| 18 |
return (self.code + (1 << 16)) % (1 << 16) |
| 19 |
|
| 20 |
def __str__(self) -> str: |
| 21 |
return "{} (0x{:04X} [{:d}])".format( |
| 22 |
self.args[0] if self.args else "", self.hexcode, self.code |
| 23 |
) |
| 24 |
|
| 25 |
|
| 26 |
class InvalidDN(LDAPError): |
| 27 |
"""Raised, when dn string is not a valid distinguished name.""" |
| 28 |
|
| 29 |
code = 0x22 |
| 30 |
|
| 31 |
|
| 32 |
class ConnectionError(LDAPError): |
| 33 |
"""Raised, when client is not able to connect to the server.""" |
| 34 |
|
| 35 |
code = -1 |
| 36 |
|
| 37 |
|
| 38 |
class AuthenticationError(LDAPError): |
| 39 |
"""Raised, when authentication is failed with the server.""" |
| 40 |
|
| 41 |
code = 0x31 |
| 42 |
|
| 43 |
|
| 44 |
class AuthMethodNotSupported(LDAPError): |
| 45 |
"""Raised, when the chosen authentication method is not supported. """ |
| 46 |
|
| 47 |
code = 0x07 |
| 48 |
|
| 49 |
|
| 50 |
class ObjectClassViolation(LDAPError): |
| 51 |
"""Raised, when try to add or modify an LDAP entry and it violates the |
| 52 |
object class rules.""" |
| 53 |
|
| 54 |
code = 0x41 |
| 55 |
|
| 56 |
|
| 57 |
class AlreadyExists(LDAPError): |
| 58 |
"""Raised, when try to add an entry and it already exists in the |
| 59 |
dictionary. """ |
| 60 |
|
| 61 |
code = 0x44 |
| 62 |
|
| 63 |
|
| 64 |
class InvalidMessageID(LDAPError): |
| 65 |
"""Raised, when try to get the result with a message ID that belongs to an |
| 66 |
unpending or already finished operation.""" |
| 67 |
|
| 68 |
code = -100 |
| 69 |
|
| 70 |
|
| 71 |
class ClosedConnection(LDAPError): |
| 72 |
"""Raised, when try to perform LDAP operation with closed connection.""" |
| 73 |
|
| 74 |
code = -101 |
| 75 |
|
| 76 |
|
| 77 |
class InsufficientAccess(LDAPError): |
| 78 |
"""Raised, when the user has insufficient access rights.""" |
| 79 |
|
| 80 |
code = 0x32 |
| 81 |
|
| 82 |
|
| 83 |
class TimeoutError(LDAPError): |
| 84 |
"""Raised, when the specified timeout is exceeded. """ |
| 85 |
|
| 86 |
code = -5 |
| 87 |
|
| 88 |
|
| 89 |
class ProtocolError(LDAPError): |
| 90 |
"""Raised, when protocol error is happened.""" |
| 91 |
|
| 92 |
code = 0x02 |
| 93 |
|
| 94 |
|
| 95 |
class UnwillingToPerform(LDAPError): |
| 96 |
"""Raised, when the server is not willing to handle requests.""" |
| 97 |
|
| 98 |
code = 0x35 |
| 99 |
|
| 100 |
|
| 101 |
class NoSuchObjectError(LDAPError): |
| 102 |
""" |
| 103 |
Raised, when operation (except search) is performed on |
| 104 |
an entry that is not found in the directory. |
| 105 |
""" |
| 106 |
|
| 107 |
code = 0x20 |
| 108 |
|
| 109 |
|
| 110 |
class AffectsMultipleDSA(LDAPError): |
| 111 |
"""Raised, when multiple directory server agents are affected. """ |
| 112 |
|
| 113 |
code = 0x47 |
| 114 |
|
| 115 |
|
| 116 |
class SizeLimitError(LDAPError): |
| 117 |
""" |
| 118 |
Raised, when the search operation exceeds the client side size |
| 119 |
limit or server side size limit that's applied to the bound user. |
| 120 |
""" |
| 121 |
|
| 122 |
code = 0x04 |
| 123 |
|
| 124 |
|
| 125 |
class NotAllowedOnNonleaf(LDAPError): |
| 126 |
"""Raised, when the operation is not allowed on a nonleaf object.""" |
| 127 |
|
| 128 |
code = 0x42 |
| 129 |
|
| 130 |
|
| 131 |
class NoSuchAttribute(LDAPError): |
| 132 |
"""Raised, when the given attribute of an entry does not exist.""" |
| 133 |
|
| 134 |
code = 0x10 |
| 135 |
|
| 136 |
|
| 137 |
class TypeOrValueExists(LDAPError): |
| 138 |
""" |
| 139 |
Raised, when the attribute already exists or the value |
| 140 |
has been already assigned. |
| 141 |
""" |
| 142 |
|
| 143 |
code = 0x14 |
| 144 |
|
| 145 |
|
| 146 |
class PasswordPolicyError(LDAPError): |
| 147 |
""" General exception for password policy errors. """ |
| 148 |
|
| 149 |
_dflt_args = ("Password policy error.",) |
| 150 |
|
| 151 |
def __init__(self, msg: Optional[str] = None) -> None: |
| 152 |
super().__init__(msg) |
| 153 |
self.args = self._dflt_args if msg is None else (msg,) |
| 154 |
|
| 155 |
|
| 156 |
class PasswordExpired(PasswordPolicyError, AuthenticationError): |
| 157 |
""" |
| 158 |
Raised, when the password policy is set, available on the server |
| 159 |
and the user's password is expired. |
| 160 |
""" |
| 161 |
|
| 162 |
code = -200 |
| 163 |
_dflt_args = ("User's password is expired.",) |
| 164 |
|
| 165 |
|
| 166 |
class AccountLocked(PasswordPolicyError, AuthenticationError): |
| 167 |
""" |
| 168 |
Raised, when the password policy is set, available on the server |
| 169 |
and the user's account is locked. |
| 170 |
""" |
| 171 |
|
| 172 |
code = -201 |
| 173 |
_dflt_args = ("User's account is locked.",) |
| 174 |
|
| 175 |
|
| 176 |
class ChangeAfterReset(PasswordPolicyError): |
| 177 |
""" |
| 178 |
Raised, when the password policy is set, available on the server |
| 179 |
and it signifies that the password must be changed before the user |
| 180 |
will be allowed to perform any operation (except bind and modify). |
| 181 |
""" |
| 182 |
|
| 183 |
code = -202 |
| 184 |
_dflt_args = ("User's password is expired.",) |
| 185 |
|
| 186 |
|
| 187 |
class PasswordModNotAllowed(PasswordPolicyError): |
| 188 |
""" |
| 189 |
Raised, when the password policy is set, available on the server |
| 190 |
and the user is restricted from changing her password. |
| 191 |
""" |
| 192 |
|
| 193 |
code = -203 |
| 194 |
_dflt_args = ("Password modification is not allowed.",) |
| 195 |
|
| 196 |
|
| 197 |
class MustSupplyOldPassword(PasswordPolicyError): |
| 198 |
""" |
| 199 |
Raised, when the password policy is set, available on the server |
| 200 |
and the existing password is not specified. |
| 201 |
""" |
| 202 |
|
| 203 |
code = -204 |
| 204 |
_dflt_args = ("Old password must be provided.",) |
| 205 |
|
| 206 |
|
| 207 |
class InsufficientPasswordQuality(PasswordPolicyError): |
| 208 |
""" |
| 209 |
Raised, when the password policy is set, available on the server |
| 210 |
and the user's password is not strong enough. |
| 211 |
""" |
| 212 |
|
| 213 |
code = -205 |
| 214 |
_dflt_args = ("Password does not pass quality checking.",) |
| 215 |
|
| 216 |
|
| 217 |
class PasswordTooShort(PasswordPolicyError): |
| 218 |
""" |
| 219 |
Raised, when the password policy is set, available on the server |
| 220 |
and the user's password is too short to be set. |
| 221 |
""" |
| 222 |
|
| 223 |
code = -206 |
| 224 |
_dflt_args = ("Password is too short.",) |
| 225 |
|
| 226 |
|
| 227 |
class PasswordTooYoung(PasswordPolicyError): |
| 228 |
""" |
| 229 |
Raised, when the password policy is set, available on the server |
| 230 |
and the user's password is too young to be modified. |
| 231 |
""" |
| 232 |
|
| 233 |
code = -207 |
| 234 |
_dflt_args = ("Password is too young to be modified.",) |
| 235 |
|
| 236 |
|
| 237 |
class PasswordInHistory(PasswordPolicyError): |
| 238 |
""" |
| 239 |
Raised, when the password policy is set, available on the server |
| 240 |
and the user's password is in the history. |
| 241 |
""" |
| 242 |
|
| 243 |
code = -208 |
| 244 |
_dflt_args = ("User's password is in the history.",) |
| 245 |
|
| 246 |
|
| 247 |
def _get_error(code: int) -> type: |
| 248 |
""" Return an error by code number. """ |
| 249 |
if code == -1 or code == 0x51 or code == -11: |
| 250 |
|
| 251 |
|
| 252 |
return ConnectionError.create(code) |
| 253 |
elif code == 0x02: |
| 254 |
return ProtocolError |
| 255 |
elif code == 0x04: |
| 256 |
return SizeLimitError |
| 257 |
elif code == 0x07: |
| 258 |
return AuthMethodNotSupported |
| 259 |
elif code == 0x10: |
| 260 |
return NoSuchAttribute |
| 261 |
elif code == 0x14: |
| 262 |
return TypeOrValueExists |
| 263 |
elif code == 0x20: |
| 264 |
return NoSuchObjectError |
| 265 |
elif code == 0x22: |
| 266 |
return InvalidDN |
| 267 |
elif code == 0x31: |
| 268 |
return AuthenticationError |
| 269 |
elif code == 0x32: |
| 270 |
return InsufficientAccess |
| 271 |
elif code == 0x35: |
| 272 |
return UnwillingToPerform |
| 273 |
elif code == 0x41: |
| 274 |
return ObjectClassViolation |
| 275 |
elif code == 0x42: |
| 276 |
return NotAllowedOnNonleaf |
| 277 |
elif code == 0x44: |
| 278 |
return AlreadyExists |
| 279 |
elif code == 0x47: |
| 280 |
return AffectsMultipleDSA |
| 281 |
elif code == -5 or code == 0x55: |
| 282 |
return TimeoutError.create(code) |
| 283 |
elif code == -100: |
| 284 |
return InvalidMessageID |
| 285 |
elif code == -101: |
| 286 |
return ClosedConnection |
| 287 |
elif code == -200: |
| 288 |
return PasswordExpired |
| 289 |
elif code == -201: |
| 290 |
return AccountLocked |
| 291 |
elif code == -202: |
| 292 |
return ChangeAfterReset |
| 293 |
elif code == -203: |
| 294 |
return PasswordModNotAllowed |
| 295 |
elif code == -204: |
| 296 |
return MustSupplyOldPassword |
| 297 |
elif code == -205: |
| 298 |
return InsufficientPasswordQuality |
| 299 |
elif code == -206: |
| 300 |
return PasswordTooShort |
| 301 |
elif code == -207: |
| 302 |
return PasswordTooYoung |
| 303 |
elif code == -208: |
| 304 |
return PasswordInHistory |
| 305 |
else: |
| 306 |
return LDAPError.create(code) |