ud-host: use subprocess.Popen instead of os.popen.
authorJulien Cristau <jcristau@debian.org>
Fri, 11 Oct 2019 15:44:33 +0000 (17:44 +0200)
committerJulien Cristau <jcristau@debian.org>
Fri, 11 Oct 2019 15:47:21 +0000 (17:47 +0200)
debian/changelog
ud-host

index 936ed15..6982cc3 100644 (file)
@@ -23,6 +23,7 @@ userdir-ldap (0.3.97) UNRELEASED; urgency=medium
   * Delete unmaintained/gpgwrapper.  8 years of non-maintenance ought to be enough.
   * Delete ud-emailmatcher.  Looks broken and unused.
   * ud-mailgate: use subprocess.Popen instead of os.popen.
+  * ud-host: use subprocess.Popen instead of os.popen.
 
  -- Peter Palfrader <weasel@debian.org>  Sat, 06 Apr 2019 22:04:34 +0200
 
diff --git a/ud-host b/ud-host
index 30d6c36..79a9bd1 100755 (executable)
--- a/ud-host
+++ b/ud-host
@@ -33,8 +33,7 @@
 #    -l    list all hosts and their status
 #    -f    list all SSH fingerprints
 
-import time, os, pwd, sys, getopt, ldap, crypt, readline, copy
-from tempfile import mktemp
+import time, os, pwd, sys, getopt, ldap, crypt, readline, copy, subprocess
 from os import O_CREAT, O_EXCL, O_WRONLY
 from userdir_ldap import *
 
@@ -213,18 +212,6 @@ def MultiChangeAttr(Attrs,Attr):
    Attrs[1][Attr].append(NewValue)
    print
 
-def CalcTempFile():
-   unique = 0
-   while unique == 0:
-      name = mktemp()
-      try:
-         fd = os.open(name, O_CREAT | O_EXCL | O_WRONLY, 0600)
-      except OSError:
-         continue
-      os.close(fd)
-      unique = 1
-   return name
-
 
 # Main program starts here
 User = pwd.getpwuid(os.getuid())[0]
@@ -285,21 +272,19 @@ elif FingerPrints == 1:
       hosts.append(hAttrs[1]['host'][0])
    hosts.sort()
 
-   tmpfile = CalcTempFile()
    for host in hosts:
       for hAttrs in Attrs:
          if host == hAttrs[1]['host'][0]:
             if 'sshRSAHostKey' in hAttrs[1].keys():
                for key in hAttrs[1]['sshRSAHostKey']:
-                  tmp = open(tmpfile, 'w')
-                  tmp.write(key + '\n')
-                  tmp.close()
-                  fp = os.popen('/usr/bin/ssh-keygen -l -f ' + tmpfile, "r")
-                  input = fp.readline()
-                  fp.close()
+                  keygen = subprocess.Popen(['/usr/bin/ssh-keygen', '-l', '-f', '-'],
+                                            stdin=subprocess.PIPE, stdout=subprocess.PIPE)
+                  keygen.stdin.write(key + '\n')
+                  keygen.stdin.close()
+                  input = keygen.stdout.readline()
+                  keygen.wait()
                   fingerprint = input.split(' ')
                   print "%s %s root@%s" % (fingerprint[0], fingerprint[1], host)
-   os.unlink(tmpfile)
    sys.exit(0)
 
 HostDn = "host=" + Host + "," + HostBaseDn