]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/API2/Storage/Config.pm
storage add: always extract password from parameters
[pve-storage.git] / PVE / API2 / Storage / Config.pm
index 6c9b3b141727f0bfc7b27f2bef8c64422ad1eacc..95ca9b86d9f637ea5d41e8c0142064dad7c3428d 100755 (executable)
@@ -8,6 +8,8 @@ use PVE::Tools qw(extract_param);
 use PVE::Cluster qw(cfs_read_file cfs_write_file);
 use PVE::Storage;
 use PVE::Storage::Plugin;
+use PVE::Storage::LVMPlugin;
+use PVE::Storage::CIFSPlugin;
 use HTTP::Status qw(:constants);
 use Storable qw(dclone);
 use PVE::JSONSchema qw(get_standard_option);
@@ -101,7 +103,7 @@ __PACKAGE__->register_method ({
            storage => get_standard_option('pve-storage-id'),
        },
     },
-    returns => {},
+    returns => { type => 'object' },
     code => sub {
        my ($param) = @_;
 
@@ -127,6 +129,20 @@ __PACKAGE__->register_method ({
        my $type = extract_param($param, 'type');
        my $storeid = extract_param($param, 'storage');
 
+       # revent an empty nodelist.
+       # fix me in section config create never need an empty entity.
+       delete $param->{nodes} if !$param->{nodes};
+
+       my $password;
+       # always extract pw, else it gets written to the www-data readable scfg
+       if (my $tmp_pw = extract_param($param, 'password')) {
+           if ($type eq 'cifs' && $param->{username}) {
+               $password = $tmp_pw;
+           } else {
+               warn "ignore password parameter\n";
+           }
+       }
+
        if ($param->{portal}) {
            $param->{portal} = PVE::Storage::resolv_portal($param->{portal});
        }
@@ -145,6 +161,8 @@ __PACKAGE__->register_method ({
 
                $cfg->{ids}->{$storeid} = $opts;
 
+               my $cred_file = undef;
+
                if ($type eq 'lvm' && $opts->{base}) {
 
                    my ($baseid, $volname) = PVE::Storage::parse_volume_id($opts->{base});
@@ -170,18 +188,29 @@ __PACKAGE__->register_method ({
                        if -e $ceph_storage_keyring;
 
                    eval {
+                       mkdir '/etc/pve/priv/ceph';
                        PVE::Tools::file_copy($ceph_admin_keyring, $ceph_storage_keyring);
                    };
                    if (my $err = $@) {
                        unlink $ceph_storage_keyring;
                        die "failed to copy ceph authx keyring for storage '$storeid': $err\n";
                    }
+               } elsif ($type eq 'cifs' && defined($password)) {
+                   # create a password file in /etc/pve/priv,
+                   # this file is used as a cert_file at mount time.
+                   $cred_file = PVE::Storage::CIFSPlugin::cifs_set_credentials($password, $storeid);
                }
 
-               # try to activate if enabled on local node,
-               # we only do this to detect errors/problems sooner
-               if (PVE::Storage::storage_check_enabled($cfg, $storeid, undef, 1)) {
-                   PVE::Storage::activate_storage($cfg, $storeid);
+               eval {
+                   # try to activate if enabled on local node,
+                   # we only do this to detect errors/problems sooner
+                   if (PVE::Storage::storage_check_enabled($cfg, $storeid, undef, 1)) {
+                       PVE::Storage::activate_storage($cfg, $storeid);
+                   }
+               };
+               if(my $err = $@) {
+                   unlink $cred_file if defined($cred_file);
+                   die $err;
                }
 
                PVE::Storage::write_config($cfg);
@@ -264,7 +293,12 @@ __PACKAGE__->register_method ({
                die "can't remove storage - storage is used as base of another storage\n"
                    if PVE::Storage::storage_is_used($cfg, $storeid);
 
-               if ($scfg->{type} eq 'rbd' && !defined($scfg->{monhost})) {
+               if ($scfg->{type} eq 'cifs')  {
+                   my $cred_file = PVE::Storage::CIFSPlugin::cifs_cred_file_name($storeid);
+                   if (-f $cred_file) {
+                       unlink($cred_file) or warn "removing cifs credientials '$cred_file' failed: $!\n";
+                   }
+               } elsif ($scfg->{type} eq 'rbd' && !defined($scfg->{monhost})) {
                    my $ceph_storage_keyring = "/etc/pve/priv/ceph/${storeid}.keyring";
                    if (-f $ceph_storage_keyring) {
                        unlink($ceph_storage_keyring) or warn "removing keyring of storage failed: $!\n";