1
# Copyright 2006 Massachusetts Institute of Technology.
4
# Export of this software from the United States of America may
5
# require a specific license from the United States Government.
6
# It is the responsibility of any person or organization contemplating
7
# export to obtain such a license before exporting.
9
# WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
10
# distribute this software and its documentation for any purpose and
11
# without fee is hereby granted, provided that the above copyright
12
# notice appear in all copies and that both that copyright notice and
13
# this permission notice appear in supporting documentation, and that
14
# the name of M.I.T. not be used in advertising or publicity pertaining
15
# to distribution of the software without specific, written prior
16
# permission. Furthermore if you modify this software you must label
17
# your software as modified software and not distribute it in such a
18
# fashion that it might be confused with the original M.I.T. software.
19
# M.I.T. makes no representations about the suitability of
20
# this software for any purpose. It is provided "as is" without express
21
# or implied warranty.
23
# possible return values:
24
# False: request not handled by this script, try another means
25
# empty list: no server available, e.g., TCP KDC in realm with only UDP
26
# ordered list of (ip-addr-string, port-number-or-string, socket-type)
28
# Field ip-addr-string is a numeric representation of the IPv4 or IPv6
29
# address. Field port-number-or-string is, for example, "88" or 88. The
30
# socket type is also expressed numerically, SOCK_DGRAM or SOCK_STREAM.
31
# It must agree with the supplied socktype value if that is non-zero, but
32
# zero must not be used in the returned list.
34
# service enum values: kdc=1, master_kdc, kadmin, krb524, kpasswd
36
from socket import getaddrinfo, SOCK_STREAM, SOCK_DGRAM, AF_INET, AF_INET6
37
def locate1 (service, realm, socktype, family):
38
if (service == 1 or service == 2) and realm == "ATHENA.MIT.EDU":
39
if socktype == SOCK_STREAM: return []
42
hlist = (("kerberos.mit.edu", 88), ("kerberos-1.mit.edu", 88),
43
("some-random-name-that-does-not-exist.mit.edu", 12345),
44
("kerberos.mit.edu", 750))
45
if service == 2: hlist = (hlist[0],)
46
for (hname,hport) in hlist:
48
alist = getaddrinfo(hname, hport, family, socktype)
50
(fam, stype, proto, canonname, sa) = a
51
if fam == AF_INET or fam == AF_INET6:
54
result = result + [(addr, port, stype)]
55
except Exception, inst:
56
# print "getaddrinfo error for " + hname + ":", inst
57
pass # Enh, this is just a demo.
59
if realm == "BOBO.MIT.EDU": return []
63
servicenames = { 1: "kdc", 2: "master_kdc", 3: "kadmin", 4: "krb524", 5: "kpasswd" }
64
socktypenames = { SOCK_STREAM: "STREAM", SOCK_DGRAM: "DGRAM" }
65
familynames = { 0: "UNSPEC", AF_INET: "INET", AF_INET6: "INET6" }
67
def locate (service, realm, socktype, family):
68
socktypename = socktype
69
if socktype in socktypenames: socktypename = "%s(%d)" % (socktypenames[socktype], socktype)
71
if family in familynames: familyname = "%s(%d)" % (familynames[family], family)
73
if service in servicenames: servicename = "%s(%d)" % (servicenames[service], service)
74
if verbose: print "locate called with service", servicename, "realm", realm, "socktype", socktypename, "family", familyname
75
result = locate1 (service, realm, socktype, family)
76
if verbose: print "locate result is", result