svn rev #25122: trunk/src/util/

ghudson@MIT.EDU ghudson at MIT.EDU
Thu Sep 1 13:33:11 EDT 2011


http://src.mit.edu/fisheye/changelog/krb5/?cs=25122
Commit By: ghudson
Log Message:
Get local hostname more precisely in k5test.py.

socket.getfqdn() tries to produce a result containing a period, so it
may disagree with krb5_sname_to_principal's result--for example, in
Fedora's default DHCP configuration.  Use getaddrinfo and getnameinfo
calls mirroring krb5_sname_to_principal's logic instead.


Changed Files:
U   trunk/src/util/k5test.py
Modified: trunk/src/util/k5test.py
===================================================================
--- trunk/src/util/k5test.py	2011-09-01 16:21:25 UTC (rev 25121)
+++ trunk/src/util/k5test.py	2011-09-01 17:33:11 UTC (rev 25122)
@@ -408,6 +408,23 @@
             return dir
     fail('Cannot locate plugins; run "make fake-install" at %s.' % buildtop)
 
+# Return the local hostname as it will be canonicalized by
+# krb5_sname_to_principal.  We can't simply use socket.getfqdn()
+# because it explicitly prefers results containing periods and
+# krb5_sname_to_principal doesn't care.
+def _get_hostname():
+    hostname = socket.gethostname()
+    try:
+        ai = socket.getaddrinfo(hostname, None, 0, 0, 0,
+                                socket.AI_CANONNAME | socket.AI_ADDRCONFIG)
+    except socket.gaierror, (error, errstr):
+        fail('Local hostname "%s" does not resolve: %s.' % (hostname, errstr))
+    (family, socktype, proto, canonname, sockaddr) = ai[0]
+    try:
+        name = socket.getnameinfo(sockaddr, socket.NI_NAMEREQD)
+    except socket.gaierror:
+        return canonname.lower()
+    return name[0].lower()
 
 # Parse command line arguments, setting global option variables.  Also
 # sets the global variable args to the positional arguments, which may
@@ -1046,8 +1063,7 @@
 srctop = _find_srctop()
 plugins = _find_plugins()
 _runenv = _import_runenv()
-# This gets used for principal names, so force it to lower case.
-hostname = socket.getfqdn().lower()
+hostname = _get_hostname()
 null_input = open(os.devnull, 'r')
 
 krb5kdc = os.path.join(buildtop, 'kdc', 'krb5kdc')




More information about the cvs-krb5 mailing list