]> xenbits.xensource.com Git - people/dariof/xen-tools.git/commitdiff
Encrypt root password with sha256 by default.
authorStéphane Jourdois <sjourdois@gmail.com>
Fri, 23 Jul 2010 21:42:43 +0000 (23:42 +0200)
committerDmitry Nedospasov <dmitry@nedos.net>
Sun, 25 Jul 2010 20:10:59 +0000 (22:10 +0200)
- Use perl crypt with some magic rather than openssl to hash root
password, either generated or provided by user.
- Remove the undocumented dependency on openssl.
- Add a hash_method option to configure the hashing algorithm.
- Permit md5, sha256 and sha512, and use sha256 as default.
- Remove TODO entry about "more random" passwords.
- Remove an ugly system() call.

TODO
bin/xen-create-image
etc/xen-tools.conf

diff --git a/TODO b/TODO
index b20199d807ac8d2f3d904e954aeaad8204e1a4cf..e9b490a3632f6399d0d22b74e5aafb4fefe3269c 100644 (file)
--- a/TODO
+++ b/TODO
@@ -62,12 +62,6 @@ Maybe for a 4.3 or 5.0 release
    install Can't remove open logical volume "acromantula-domu1-disk"
    this should be a matter of unmounting the mounted volume from /tmp.
 
-* Create "more random" passwords and salts
-
-   Currently passwords are using the passwd function of OpenSSL which
-   at the moment can only generate md5 hashes. The passwords can
-   still be made quite strong though.
-
 * Generic grub support
 
    This will generate a much nicer menu.lst as a side effect, as its
index 6d63e3a385e7836fcbedb2c71a0794e49bce72f7..fcaaffac991106baadb46d7e3844c0d489b7b8bc 100755 (executable)
@@ -114,6 +114,10 @@ xen-create-image - Easily create new Xen instances with networking and OpenSSH.
    --password=passphrase
                 Set the root password for the new guest.
 
+   --hash_method=algorithm
+                Override the default hashing method of sha256 and use the
+                provided algorithm. Can be : md5, sha256 or sha512
+
    --passwd     Ask for a root password interactively during setup.
                 NOTE:  This overrides --genpass --password
 
@@ -1381,6 +1385,7 @@ sub setupDefaultOptions
     $CONFIG{ 'genpass' }     = 1;
     $CONFIG{ 'genpass_len' } = 8;
     $CONFIG{ 'password' }    = '';
+    $CONFIG{ 'hash_method' } = 'sha256';
 
     #
     #  The program to run to create a filesystem.
@@ -1575,6 +1580,10 @@ sub checkOption
             check   => qr/^(?:[0-9a-f]{2}:){5}[0-9a-f]{2}$/i,
             message => "must be a valid ethernet mac address.\n",
         },
+        hashMethod => {
+            check   => qr/^md5|sha256|sha512$/i,
+            message => "must be md5, sha256 or sha512.\n",
+        },
     );
 
     # Define what argument each option accepts.
@@ -1608,6 +1617,7 @@ sub checkOption
         extension     => 'filename',
         mac           => 'mac',
         ip            => 'ipv4',
+        hash_method   => 'hashMethod',
     );
 
     # If given option does not exists in optionsTypes,
@@ -1737,6 +1747,7 @@ sub parseCommandLineArguments
             "genpass-len=i", \&checkOption,
             "genpass_len=i", \&checkOption,
             "password=s",   \&checkOption,
+            "hash_method=s",\&checkOption,
             "partitions=s", \&checkOption,
             "role=s",       \&checkOption,
             "role-args=s",  \&checkOption,
@@ -3964,9 +3975,28 @@ sub setupRootPassword
             {
                 $PASSWORD = $CONFIG { 'password' };
             }
+
             my $salt = generatePassword(8);
-            my $hash = `echo -n $PASSWORD | openssl passwd -stdin -1 -salt $salt`;
-            $hash =~ s/\s+$//;
+
+            my $hash_method;
+            if ($CONFIG{ 'hash_method' } eq 'md5')
+            {
+                $hash_method = '$1$';
+            }
+            elsif ($CONFIG{ 'hash_method' } eq 'sha256')
+            {
+                $hash_method = '$5$';
+            }
+            elsif ($CONFIG{ 'hash_method' } eq 'sha512')
+            {
+                $hash_method = '$6$';
+            }
+            else
+            {
+                die "oops... unknown hashing method, should not happen!";
+            }
+
+            my $hash = crypt($PASSWORD, $hash_method . $salt);
 
             #
             #  Copy the file to ensure the original retains the correct
@@ -3979,9 +4009,8 @@ sub setupRootPassword
             my $line;
             while(defined($line = <TMP>))
             {
-                chomp $line;
                 $line =~ s#^root:[^:]*:#root:$hash:#;
-                print SHADOW "$line\n";
+                print SHADOW $line;
             }
 
             #
index 455345531fe297bd307964862fac18f8e33fa6f1..0b9f19765790c4404eb3d2393f2b9d147bf347d5 100644 (file)
@@ -218,6 +218,14 @@ image  = sparse   # Specify sparse vs. full disk images.
 # genpass_len = 8
 #
 
+#
+# You can yet change the hashing method to encrypt the generated
+# password by changing the line below.
+# Valid values : md5, sha256 and sha512.
+#
+# hash_method = sha256
+#
+
 #
 # Alternatively, Uncomment the following line if you wish to
 # interactively setup a new root password for images.