]> git.proxmox.com Git - pve-container.git/commitdiff
allow to setup root password
authorDietmar Maurer <dietmar@proxmox.com>
Thu, 23 Apr 2015 07:28:24 +0000 (09:28 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Thu, 23 Apr 2015 07:31:14 +0000 (09:31 +0200)
13 files changed:
src/PVE/API2/LXC.pm
src/PVE/LXCSetup.pm
src/PVE/LXCSetup/Base.pm
src/lxc-pve
src/test/run_tests.pl
src/test/test9/config [new file with mode: 0644]
src/test/test9/etc/debian_version [new file with mode: 0644]
src/test/test9/etc/hostname.exp [new file with mode: 0644]
src/test/test9/etc/hosts.exp [new file with mode: 0644]
src/test/test9/etc/passwd [new file with mode: 0644]
src/test/test9/etc/passwd.exp [new file with mode: 0644]
src/test/test9/etc/shadow [new file with mode: 0644]
src/test/test9/etc/shadow.exp [new file with mode: 0644]

index ad84d66c4a6291244e689e3125dc0fbefccf4169..0006960736ed17ba85f2386babb74784bda8d440 100644 (file)
@@ -126,6 +126,7 @@ __PACKAGE__->register_method({
                optional => 1,
                type => 'string',
                description => "Sets root password inside container.",
+               minLength => 5,
            },
            storage => get_standard_option('pve-storage-id', {
                description => "Target storage.",
@@ -244,6 +245,10 @@ __PACKAGE__->register_method({
            my $cmd = ['lxc-create', '-f', $temp_conf_fn, '-t', 'pve', '-n', $vmid,
                       '--', '--archive', $archive];
 
+           if (defined($password)) {
+               push $cmd, '--password', $password 
+           }
+           
            eval { PVE::Tools::run_command($cmd); };
            my $err = $@;
 
index 26ce16a8604c90e08f1ab7c349f5604fe38d0b51..0ebb47b7e7f589623e98f73ba53181388f6c62ef 100644 (file)
@@ -63,8 +63,10 @@ sub setup_init {
     $self->{plugin}->setup_init($self->{conf});
 }
 
-sub set_user_passwort {
-    die "fixme";
+sub set_user_password {
+    my ($self, $user, $pw) = @_;
+    
+    $self->{plugin}->set_user_password($self->{conf}, $user, $pw);
 }
 
 sub pre_start_hook {
@@ -74,9 +76,9 @@ sub pre_start_hook {
 }
 
 sub post_create_hook {
-    my ($self) = @_;
+    my ($self, $root_password) = @_;
 
-    $self->{plugin}->post_create_hook($self->{conf});
+    $self->{plugin}->post_create_hook($self->{conf}, $root_password);
 }
 
 1;
index 079276cad1258044dd5a8c21cde683e809aa5239..5365006d66eb9f629ec4ce8dcfef8b7525b4b96e 100644 (file)
@@ -3,6 +3,11 @@ package PVE::LXCSetup::Base;
 use strict;
 use warnings;
 
+use File::stat;
+use Digest::SHA;
+use IO::File;
+use Encode;
+
 use PVE::Tools;
 
 my $update_etc_hosts = sub {
@@ -158,6 +163,69 @@ sub setup_init {
     die "please implement this inside subclass"
 }
 
+my $replacepw  = sub {
+    my ($file, $user, $epw) = @_;
+
+    my $tmpfile = "$file.$$";
+
+    eval  {
+       my $src = IO::File->new("<$file") ||
+           die "unable to open file '$file' - $!";
+
+       my $st = File::stat::stat($src) ||
+           die "unable to stat file - $!";
+
+       my $dst = IO::File->new(">$tmpfile") ||
+           die "unable to open file '$tmpfile' - $!";
+
+       # copy owner and permissions
+       chmod $st->mode, $dst;
+       chown $st->uid, $st->gid, $dst;
+       
+       while (defined (my $line = <$src>)) {
+           $line =~ s/^${user}:[^:]*:/${user}:${epw}:/;
+           print $dst $line;
+       }
+
+       $src->close() || die "close '$file' failed - $!\n";
+       $dst->close() || die "close '$tmpfile' failed - $!\n";
+    };
+    if (my $err = $@) {
+       unlink $tmpfile;
+    } else {
+       rename $tmpfile, $file;
+       unlink $tmpfile; # in case rename fails
+    }  
+};
+
+sub set_user_password {
+    my ($class, $conf, $user, $opt_password) = @_;
+
+    my $rootfs = $conf->{'lxc.rootfs'};
+
+    my $pwfile = "$rootfs/etc/passwd";
+
+    return if ! -f $pwfile;
+
+    my $shadow = "$rootfs/etc/shadow";
+    
+    if (defined($opt_password)) {
+       if ($opt_password !~ m/^\$/) {
+           my $time = substr (Digest::SHA::sha1_base64 (time), 0, 8);
+           $opt_password = crypt(encode("utf8", $opt_password), "\$1\$$time\$");
+       };
+    } else {
+       $opt_password = '*';
+    }
+    
+    if (-f $shadow) {
+       &$replacepw ($shadow, $user, $opt_password);
+       &$replacepw ($pwfile, $user, 'x');
+    } else {
+       &$replacepw ($pwfile, $user, $opt_password);
+    }
+}
+
 sub pre_start_hook {
     my ($class, $conf) = @_;
 
@@ -170,13 +238,14 @@ sub pre_start_hook {
 }
 
 sub post_create_hook {
-    my ($class, $conf) = @_;
+    my ($class, $conf, $root_password) = @_;
 
+    $class->set_user_password($conf, 'root', $root_password);
     $class->setup_init($conf);
     $class->setup_network($conf);
     $class->set_hostname($conf);
     $class->set_dns($conf);
-
+    
     # fixme: what else ?
 }
 
index 17b3e5e6289e9bc1fb462e7282af793e6cf59236..a0d33a96643fd5166e017d01c26b020984ed9d04 100755 (executable)
@@ -55,6 +55,11 @@ __PACKAGE__->register_method ({
                description => "Path to the template tar file.",
                type => 'string',               
            },
+           password => {
+               optional => 1,
+               type => 'string',
+               description => "Sets root password inside container.",
+           },
            'mapped-uid' => {
                description => " A uid map (user namespaces - LXC internal argument - do not pass manually!)",
                type => 'string',
@@ -133,7 +138,7 @@ __PACKAGE__->register_method ({
 
        # fixme: use correct dist
        my $lxc_setup = PVE::LXCSetup->new($conf);
-       $lxc_setup->post_create_hook();
+       $lxc_setup->post_create_hook($param->{password});
        
        return undef;
     }});
index 05c7be0df1fb6f4ad59ca399e0996f8d7e2dbdd7..9d52393ee3d8662eb9155c2a10c71b9ddeaf194b 100755 (executable)
@@ -40,9 +40,9 @@ sub run_test {
     for (my $i = 0; $i < 2; $i++) {
        # run tests twice, to make sure scripts are idempotent
        
-       $lxc_setup->post_create_hook();
+       $lxc_setup->post_create_hook('$TEST$ABCDEF');
 
-       my @testfiles = qw(/etc/hostname /etc/hosts /etc/inittab /etc/network/interfaces /etc/resolv.conf);
+       my @testfiles = qw(/etc/hostname /etc/hosts /etc/inittab /etc/network/interfaces /etc/resolv.conf /etc/passwd /etc/shadow);
        foreach my $fn (@testfiles) {
            next if !-f "$testdir/$fn.exp";
            test_file("$testdir/$fn.exp", "$rootfs/$fn");
diff --git a/src/test/test9/config b/src/test/test9/config
new file mode 100644 (file)
index 0000000..ddbccca
--- /dev/null
@@ -0,0 +1 @@
+lxc.utsname = test9
diff --git a/src/test/test9/etc/debian_version b/src/test/test9/etc/debian_version
new file mode 100644 (file)
index 0000000..4fedf1d
--- /dev/null
@@ -0,0 +1 @@
+7.0
diff --git a/src/test/test9/etc/hostname.exp b/src/test/test9/etc/hostname.exp
new file mode 100644 (file)
index 0000000..c372db4
--- /dev/null
@@ -0,0 +1 @@
+test9
diff --git a/src/test/test9/etc/hosts.exp b/src/test/test9/etc/hosts.exp
new file mode 100644 (file)
index 0000000..41eed1f
--- /dev/null
@@ -0,0 +1,2 @@
+127.0.0.1 localhost.localnet localhost
+127.0.1.1 test9
diff --git a/src/test/test9/etc/passwd b/src/test/test9/etc/passwd
new file mode 100644 (file)
index 0000000..e505613
--- /dev/null
@@ -0,0 +1,21 @@
+root:x:0:0:root:/root:/bin/bash
+daemon:x:1:1:daemon:/usr/sbin:/bin/sh
+bin:x:2:2:bin:/bin:/bin/sh
+sys:x:3:3:sys:/dev:/bin/sh
+sync:x:4:65534:sync:/bin:/bin/sync
+games:x:5:60:games:/usr/games:/bin/sh
+man:x:6:12:man:/var/cache/man:/bin/sh
+lp:x:7:7:lp:/var/spool/lpd:/bin/sh
+mail:x:8:8:mail:/var/mail:/bin/sh
+news:x:9:9:news:/var/spool/news:/bin/sh
+uucp:x:10:10:uucp:/var/spool/uucp:/bin/sh
+proxy:x:13:13:proxy:/bin:/bin/sh
+www-data:x:33:33:www-data:/var/www:/bin/sh
+backup:x:34:34:backup:/var/backups:/bin/sh
+list:x:38:38:Mailing List Manager:/var/list:/bin/sh
+irc:x:39:39:ircd:/var/run/ircd:/bin/sh
+gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/bin/sh
+nobody:x:65534:65534:nobody:/nonexistent:/bin/sh
+libuuid:x:100:101::/var/lib/libuuid:/bin/sh
+postfix:x:101:104::/var/spool/postfix:/bin/false
+sshd:x:102:65534::/var/run/sshd:/usr/sbin/nologin
diff --git a/src/test/test9/etc/passwd.exp b/src/test/test9/etc/passwd.exp
new file mode 100644 (file)
index 0000000..e505613
--- /dev/null
@@ -0,0 +1,21 @@
+root:x:0:0:root:/root:/bin/bash
+daemon:x:1:1:daemon:/usr/sbin:/bin/sh
+bin:x:2:2:bin:/bin:/bin/sh
+sys:x:3:3:sys:/dev:/bin/sh
+sync:x:4:65534:sync:/bin:/bin/sync
+games:x:5:60:games:/usr/games:/bin/sh
+man:x:6:12:man:/var/cache/man:/bin/sh
+lp:x:7:7:lp:/var/spool/lpd:/bin/sh
+mail:x:8:8:mail:/var/mail:/bin/sh
+news:x:9:9:news:/var/spool/news:/bin/sh
+uucp:x:10:10:uucp:/var/spool/uucp:/bin/sh
+proxy:x:13:13:proxy:/bin:/bin/sh
+www-data:x:33:33:www-data:/var/www:/bin/sh
+backup:x:34:34:backup:/var/backups:/bin/sh
+list:x:38:38:Mailing List Manager:/var/list:/bin/sh
+irc:x:39:39:ircd:/var/run/ircd:/bin/sh
+gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/bin/sh
+nobody:x:65534:65534:nobody:/nonexistent:/bin/sh
+libuuid:x:100:101::/var/lib/libuuid:/bin/sh
+postfix:x:101:104::/var/spool/postfix:/bin/false
+sshd:x:102:65534::/var/run/sshd:/usr/sbin/nologin
diff --git a/src/test/test9/etc/shadow b/src/test/test9/etc/shadow
new file mode 100644 (file)
index 0000000..7d4be16
--- /dev/null
@@ -0,0 +1,21 @@
+root:!*:15908:0:99999:7:::
+daemon:*:15908:0:99999:7:::
+bin:*:15908:0:99999:7:::
+sys:*:15908:0:99999:7:::
+sync:*:15908:0:99999:7:::
+games:*:15908:0:99999:7:::
+man:*:15908:0:99999:7:::
+lp:*:15908:0:99999:7:::
+mail:*:15908:0:99999:7:::
+news:*:15908:0:99999:7:::
+uucp:*:15908:0:99999:7:::
+proxy:*:15908:0:99999:7:::
+www-data:*:15908:0:99999:7:::
+backup:*:15908:0:99999:7:::
+list:*:15908:0:99999:7:::
+irc:*:15908:0:99999:7:::
+gnats:*:15908:0:99999:7:::
+nobody:*:15908:0:99999:7:::
+libuuid:!:15908:0:99999:7:::
+postfix:*:15908:0:99999:7:::
+sshd:*:15908:0:99999:7:::
diff --git a/src/test/test9/etc/shadow.exp b/src/test/test9/etc/shadow.exp
new file mode 100644 (file)
index 0000000..4349711
--- /dev/null
@@ -0,0 +1,21 @@
+root:$TEST$ABCDEF:15908:0:99999:7:::
+daemon:*:15908:0:99999:7:::
+bin:*:15908:0:99999:7:::
+sys:*:15908:0:99999:7:::
+sync:*:15908:0:99999:7:::
+games:*:15908:0:99999:7:::
+man:*:15908:0:99999:7:::
+lp:*:15908:0:99999:7:::
+mail:*:15908:0:99999:7:::
+news:*:15908:0:99999:7:::
+uucp:*:15908:0:99999:7:::
+proxy:*:15908:0:99999:7:::
+www-data:*:15908:0:99999:7:::
+backup:*:15908:0:99999:7:::
+list:*:15908:0:99999:7:::
+irc:*:15908:0:99999:7:::
+gnats:*:15908:0:99999:7:::
+nobody:*:15908:0:99999:7:::
+libuuid:!:15908:0:99999:7:::
+postfix:*:15908:0:99999:7:::
+sshd:*:15908:0:99999:7:::