]> git.proxmox.com Git - pve-access-control.git/blobdiff - PVE/AccessControl.pm
check_volume_access : use parse_volname instead path
[pve-access-control.git] / PVE / AccessControl.pm
index 44299ce07c634d3f407e8f74cbde2e2bd1d87228..f0c37c7651a6eda6d51ced1bda18022d9ff63068 100644 (file)
@@ -5,7 +5,6 @@ use Encode;
 use Crypt::OpenSSL::Random;
 use Crypt::OpenSSL::RSA;
 use MIME::Base64;
-use MIME::Base32 qw( RFC );
 use Digest::SHA;
 use PVE::Tools qw(run_command lock_file file_get_contents split_list safe_print);
 use PVE::Cluster qw(cfs_register_file cfs_read_file cfs_write_file cfs_lock_file);
@@ -215,20 +214,66 @@ sub verify_vnc_ticket {
 }
 
 sub assemble_spice_ticket {
-    my ($username, $path) = @_;
+    my ($username, $vmid, $node) = @_;
 
     my $rsa_priv = get_privkey();
 
-    my $timestamp = sprintf("%08X", time());
+    my $timestamp = sprintf("%08x", time());
 
-    my $plain = "PVESPICE:$timestamp";
+    my $randomstr = "PVESPICE:$timestamp:$vmid:$node:" . rand(10);
 
-    $path = normalize_path($path);
+    # this should be uses as one-time password
+    # max length is 60 chars (spice limit)
+    # we pass this to qemu set_pasword and limit lifetime there
+    # keep this secret
+    my $ticket = Digest::SHA::sha1_hex($rsa_priv->sign($randomstr));
 
-    my $full = "$plain:$path";
+    # Note: spice proxy connects with HTTP, so $proxyticket is exposed to public
+    # we use a signature/timestamp to make sure nobody can fake such ticket
+    # an attacker can use this $proxyticket, but he will fail because $ticket is
+    # private.
+    # The proxy need to be able to extract/verify the ticket
+    # Note: data needs to be lower case only, because virt-viewer needs that
+    # Note: RSA signature are too long (>=256 charaters) and makes problems with remote-viewer
 
-    my $ticket = $plain . "::" . encode_base64($rsa_priv->sign($full), '');
-    return MIME::Base32::encode($ticket."::".$full);
+    my $secret = &$get_csrfr_secret();
+    my $plain = "pvespiceproxy:$timestamp:$vmid:$node";
+
+    # produces 40 characters
+    my $sig = unpack("H*", Digest::SHA::sha1($plain, &$get_csrfr_secret()));
+
+    #my $sig =  unpack("H*", $rsa_priv->sign($plain)); # this produce too long strings (512)
+
+    my $proxyticket = $plain . "::" . $sig;
+
+    return ($ticket, $proxyticket);
+}
+
+sub verify_spice_connect_url {
+    my ($connect_str) = @_;
+
+    # Note: we pass the spice ticket as 'host', so the
+    # spice viewer connects with "$ticket:$port"
+
+    return undef if !$connect_str;
+
+    if ($connect_str =~m/^pvespiceproxy:([a-z0-9]{8}):(\d+):(\S+)::([a-z0-9]{40}):(\d+)$/) {
+       my ($timestamp, $vmid, $node, $hexsig, $port) = ($1, $2, $3, $4, $5, $6);
+       my $ttime = hex($timestamp);
+       my $age = time() - $ttime;
+
+       # use very limited lifetime - is this enough?
+       return undef if !(($age > -20) && ($age < 40));
+
+       my $plain = "pvespiceproxy:$timestamp:$vmid:$node";
+       my $sig = unpack("H*", Digest::SHA::sha1($plain, &$get_csrfr_secret()));
+
+       if ($sig eq $hexsig) {
+           return ($vmid, $node, $port);
+       } 
+    }
+
+    return undef;
 }
 
 sub check_user_exist {