]> git.proxmox.com Git - pve-installer.git/commitdiff
low-level: change root password option to contain either plaintext or hash
authorChristoph Heiss <c.heiss@proxmox.com>
Mon, 15 Jul 2024 07:56:03 +0000 (09:56 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Mon, 22 Jul 2024 16:40:34 +0000 (18:40 +0200)
A hashed password can be created e.g. using the `mkpasswd(1)`.
This then will allow the auto-installer to pass along a
already-hashed password from the user, instead of simple plaintext.

Signed-off-by: Christoph Heiss <c.heiss@proxmox.com>
Tested-by: Theodor Fumics <theodor.fumics@gmx.net>
Proxmox/Install.pm
Proxmox/Install/Config.pm
proxinstall

index 19fd88de7043f481ec49c73bb7876340f053db7f..fa2702cbcb583c82980f31c00dbed527267c1b86 100644 (file)
@@ -654,6 +654,27 @@ sub prepare_grub_efi_boot_esp {
     die "failed to prepare EFI boot using Grub on '$espdev': $err" if $err;
 }
 
+my sub setup_root_password {
+    my ($targetdir) = @_;
+
+    my $plain = Proxmox::Install::Config::get_root_password('plain');
+    my $hashed = Proxmox::Install::Config::get_root_password('hashed');
+
+    die "root password must be set!\n"
+       if !defined($plain) && !defined($hashed);
+
+    die "plain and hashed root password cannot be set at the same time!\n"
+       if defined($plain) && defined($hashed);
+
+    if (defined($plain)) {
+       my $octets = encode("utf-8", $plain);
+       run_command("chroot $targetdir /usr/sbin/chpasswd", undef, "root:$octets\n");
+    } elsif (defined($hashed)) {
+       my $octets = encode("utf-8", $hashed);
+       run_command("chroot $targetdir /usr/sbin/chpasswd --encrypted", undef, "root:$octets\n");
+    }
+}
+
 sub extract_data {
     my $iso_env = Proxmox::Install::ISOEnv::get();
     my $run_env = Proxmox::Install::RunEnv::get();
@@ -1302,9 +1323,7 @@ _EOD
 
        diversion_remove($targetdir, "/sbin/start-stop-daemon");
 
-       # set root password
-       my $octets = encode("utf-8", Proxmox::Install::Config::get_password());
-       run_command("chroot $targetdir /usr/sbin/chpasswd", undef, "root:$octets\n");
+       setup_root_password($targetdir);
 
        # set root ssh keys
        my $ssh_keys = Proxmox::Install::Config::get_root_ssh_keys();
index e4490396e70be2bbfbf4f0882f71f8ef34999eb9..ae700930080de5f5ba39df71d0bd8360fa2355c0 100644 (file)
@@ -90,7 +90,7 @@ my sub init_cfg {
        keymap => 'en-us',
 
        # root credentials & details
-       password => undef,
+       root_password => undef,
        mailto => 'mail@example.invalid',
        root_ssh_keys => [],
 
@@ -196,8 +196,22 @@ sub get_timezone { return get('timezone'); }
 sub set_keymap { set_key('keymap', $_[0]); }
 sub get_keymap { return get('keymap'); }
 
-sub set_password { set_key('password', $_[0]); }
-sub get_password { return get('password'); }
+sub set_root_password {
+    my ($key) = @_;
+    croak "unknown root password option '$key'"
+       if $key ne 'plain' && $key ne 'hashed';
+
+    set_key('root_password', { $_[0] => $_[1] });
+}
+
+sub get_root_password {
+    my ($key) = @_;
+    croak "unknown root password option '$key'"
+       if $key ne 'plain' && $key ne 'hashed';
+
+    my $password = get('root_password');
+    return defined($password->{$key}) ? $password->{$key} : undef;
+}
 
 sub set_mailto { set_key('mailto', $_[0]); }
 sub get_mailto { return get('mailto'); }
index a6a4cfb55e4ae463ca2238f8f546c827c5c24465..12f3eaa6ffe8656df2ed4ad1faf4bb6924a350df 100755 (executable)
@@ -674,7 +674,7 @@ sub create_password_view {
 
     cleanup_view();
 
-    my $password = Proxmox::Install::Config::get_password();
+    my $password = Proxmox::Install::Config::get_root_password('plain');
 
     my $grid = &$create_basic_grid();
     $gtk_state->{inbox}->pack_start($grid, 0, 0, 0);
@@ -745,7 +745,7 @@ sub create_password_view {
            return;
        }
 
-       Proxmox::Install::Config::set_password($t1);
+       Proxmox::Install::Config::set_root_password('plain', $t1);
        Proxmox::Install::Config::set_mailto($t3);
 
        $step_number++;