Patch to utils.py in certmaster

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Just to drop this out of the clear blue sky. While digging around for an
unrelated problem yesterday I came across the get_hostname function in
utils. Now honestly I don't even know if this is a necessary function
but as it was it didn't really appear to do what you wanted it to (it
returned the same hostname regardless of what the IP was, unless of
course it was 127.0.0.1 in which case it appeared to return nothing). It
was also limited to ipv4 only which is ok for now but hey if you can do
it more easily why not do it more easily and more portably.

So the attached patch basically does what you were intending to do (I
think). It gets the host name then runs a DNS query for that host name,
and returns the FQDN as the host name if it is possible to look up the
host name, if not it just returns the host name. getfqdn uses
gethostbyaddr which is able to handle both ipv4 and ipv6. I also made
the exception narrower so it didn't catch anything it isn't supposed to,
and made some changes to the comments, because well, the word lame is
kinda lame ;).

Let me know if I was missing something here,
-Erinn

Here is the patch:

diff --git a/certmaster/utils.py b/certmaster/utils.py
index b135e7d..8a0b1d9 100644
--- a/certmaster/utils.py
+++ b/certmaster/utils.py
@@ -101,23 +101,22 @@ def is_error(result):
 
 def get_hostname(talk_to_certmaster=True):
     """
-    "localhost" is a lame hostname to use for a key, so try to get
-    a more meaningful hostname. We do this by connecting to the certmaster
-    and seeing what interface/ip it uses to make that connection, and
looking
-    up the hostname for that.
+    "localhost" is a poor host name to use for a key, so we try to get
+    a more meaningful host name. We do this by taking the host name and
+    searching for the FQDN for that host name. This works both with ipv4
+    and ipv6.
     """
     # FIXME: this code ignores http proxies (which granted, we don't
     #      support elsewhere either.
-    hostname = None
     hostname = socket.gethostname()
     # print "DEBUG: HOSTNAME TRY1: %s" % hostname
     try:
-        ip = socket.gethostbyname(hostname)
-    except:
-        return hostname
-    if ip != "127.0.0.1":
+        hostname = socket.getfqdn(hostname)
+    except socket.herror:
         return hostname
 
+    return hostname
+
 
 # FIXME: move to requestor module and also create a verbose mode
 # prints to the screen for usage by /usr/bin/certmaster-request

_______________________________________________
Func-list mailing list
Func-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/func-list


[Index of Archives]     [Fedora Users]     [Linux Networking]     [Fedora Legacy List]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]

  Powered by Linux