Canonicalize hostname following MIT-krb5 behavior.
(hostname: str, option: str | bool)
| 179 | |
| 180 | |
| 181 | async def _canonicalize_hostname(hostname: str, option: str | bool) -> str: |
| 182 | """Canonicalize hostname following MIT-krb5 behavior.""" |
| 183 | # https://github.com/krb5/krb5/blob/d406afa363554097ac48646a29249c04f498c88e/src/util/k5test.py#L505-L520 |
| 184 | if option in [False, "none"]: |
| 185 | return hostname |
| 186 | |
| 187 | af, socktype, proto, canonname, sockaddr = ( |
| 188 | await _getaddrinfo( |
| 189 | hostname, |
| 190 | None, |
| 191 | family=0, |
| 192 | type=0, |
| 193 | proto=socket.IPPROTO_TCP, |
| 194 | flags=socket.AI_CANONNAME, |
| 195 | ) |
| 196 | )[0] # type: ignore[index] |
| 197 | |
| 198 | # For forward just to resolve the cname as dns.lookup() will not return it. |
| 199 | if option == "forward": |
| 200 | return canonname.lower() |
| 201 | |
| 202 | try: |
| 203 | name = socket.getnameinfo(sockaddr, socket.NI_NAMEREQD) |
| 204 | except socket.gaierror: |
| 205 | return canonname.lower() |
| 206 | |
| 207 | return name[0].lower() |
| 208 | |
| 209 | |
| 210 | async def _authenticate_gssapi(credentials: MongoCredential, conn: AsyncConnection) -> None: |
no test coverage detected