~ubuntu-branches/ubuntu/maverick/krb5/maverick

« back to all changes in this revision

Viewing changes to src/plugins/locate/python/locate-service.py

  • Committer: Bazaar Package Importer
  • Author(s): Sam Hartman, Russ Allbery, Sam Hartman
  • Date: 2008-08-21 10:41:41 UTC
  • mfrom: (11.1.15 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080821104141-a0f9c4o4cpo8xd0o
Tags: 1.6.dfsg.4~beta1-4
[ Russ Allbery ]
* Translation updates:
  - Swedish, thanks Martin Bagge.  (Closes: #487669, #491774)
  - Italian, thanks Luca Monducci.  (Closes: #493962)

[ Sam Hartman ]
* Translation Updates:
    - Dutch, Thanks Vincent Zweije, Closes: #495733

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2006 Massachusetts Institute of Technology.
 
2
# All Rights Reserved.
 
3
#
 
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.
 
8
 
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.
 
22
 
 
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)
 
27
#
 
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.
 
33
#
 
34
# service enum values: kdc=1, master_kdc, kadmin, krb524, kpasswd
 
35
 
 
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 []
 
40
      socktype = SOCK_DGRAM
 
41
      result = []
 
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:
 
47
         try:
 
48
            alist = getaddrinfo(hname, hport, family, socktype)
 
49
            for a in alist:
 
50
               (fam, stype, proto, canonname, sa) = a
 
51
               if fam == AF_INET or fam == AF_INET6:
 
52
                  addr = sa[0]
 
53
                  port = sa[1]
 
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.
 
58
      return result
 
59
   if realm == "BOBO.MIT.EDU": return []
 
60
   return False
 
61
 
 
62
verbose = 0
 
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" }
 
66
 
 
67
def locate (service, realm, socktype, family):
 
68
   socktypename = socktype
 
69
   if socktype in socktypenames: socktypename = "%s(%d)" % (socktypenames[socktype], socktype)
 
70
   familyname = family
 
71
   if family in familynames: familyname = "%s(%d)" % (familynames[family], family)
 
72
   servicename = service
 
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
 
77
   return result