From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <d.csapak@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id 6905570775
 for <pve-devel@lists.proxmox.com>; Thu, 24 Jun 2021 16:25:43 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 5C6D4D82A
 for <pve-devel@lists.proxmox.com>; Thu, 24 Jun 2021 16:25:13 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [94.136.29.106])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS id BD486D820
 for <pve-devel@lists.proxmox.com>; Thu, 24 Jun 2021 16:25:12 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 925F44679E
 for <pve-devel@lists.proxmox.com>; Thu, 24 Jun 2021 16:25:12 +0200 (CEST)
From: Dominik Csapak <d.csapak@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Thu, 24 Jun 2021 16:25:10 +0200
Message-Id: <20210624142511.17077-1-d.csapak@proxmox.com>
X-Mailer: git-send-email 2.20.1
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.735 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
Subject: [pve-devel] [PATCH qemu-server 1/2] PVE/API2/Qemu/update_vm_api:
 refactor drive permission check
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Thu, 24 Jun 2021 14:25:43 -0000

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 PVE/API2/Qemu.pm | 26 +++++++++++++-------------
 1 file changed, 13 insertions(+), 13 deletions(-)

diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm
index 005e655..057b8ff 100644
--- a/PVE/API2/Qemu.pm
+++ b/PVE/API2/Qemu.pm
@@ -1263,6 +1263,17 @@ my $update_vm_api  = sub {
 	    }
 	    my $bootorder_deleted = grep {$_ eq 'bootorder'} @delete;
 
+	    my $check_drive_perms = sub {
+		my ($opt, $val) = @_;
+		my $drive = PVE::QemuServer::parse_drive($opt, $val);
+		# FIXME: cloudinit: CDROM or Disk?
+		if (PVE::QemuServer::drive_is_cdrom($drive)) { # CDROM
+		    $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.CDROM']);
+		} else {
+		    $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
+		}
+	    };
+
 	    foreach my $opt (@delete) {
 		$modified->{$opt} = 1;
 		$conf = PVE::QemuConfig->load_config($vmid); # update/reload
@@ -1300,12 +1311,7 @@ my $update_vm_api  = sub {
 		    }
 		} elsif (PVE::QemuServer::is_valid_drivename($opt)) {
 		    PVE::QemuConfig->check_protection($conf, "can't remove drive '$opt'");
-		    my $drive = PVE::QemuServer::parse_drive($opt, $val);
-		    if (PVE::QemuServer::drive_is_cdrom($drive)) {
-			$rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.CDROM']);
-		    } else {
-			$rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
-		    }
+		    $check_drive_perms->($opt, $val);
 		    PVE::QemuServer::vmconfig_register_unused_drive($storecfg, $vmid, $conf, PVE::QemuServer::parse_drive($opt, $val))
 			if $is_pending_val;
 		    PVE::QemuConfig->add_to_pending_delete($conf, $opt, $force);
@@ -1340,13 +1346,7 @@ my $update_vm_api  = sub {
 		my $arch = PVE::QemuServer::get_vm_arch($conf);
 
 		if (PVE::QemuServer::is_valid_drivename($opt)) {
-		    my $drive = PVE::QemuServer::parse_drive($opt, $param->{$opt});
-		    # FIXME: cloudinit: CDROM or Disk?
-		    if (PVE::QemuServer::drive_is_cdrom($drive)) { # CDROM
-			$rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.CDROM']);
-		    } else {
-			$rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk']);
-		    }
+		    $check_drive_perms->($opt, $param->{$opt});
 		    PVE::QemuServer::vmconfig_register_unused_drive($storecfg, $vmid, $conf, PVE::QemuServer::parse_drive($opt, $conf->{pending}->{$opt}))
 			if defined($conf->{pending}->{$opt});
 
-- 
2.20.1