package PVE::AccessControl;
use strict;
+use warnings;
use Encode;
use Crypt::OpenSSL::Random;
use Crypt::OpenSSL::RSA;
+use Net::SSLeay;
use MIME::Base64;
use Digest::SHA;
+use Digest::HMAC_SHA1;
+use URI::Escape;
+use LWP::UserAgent;
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);
use PVE::JSONSchema;
# 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
- my $plain = "pvespiceproxy:$timestamp:$vmid:$node";
- my $sig = unpack("H*", $rsa_priv->sign($plain));
+ # Note: RSA signature are too long (>=256 charaters) and makes problems with remote-viewer
+
+ my $secret = &$get_csrfr_secret();
+ my $plain = "pvespiceproxy:$timestamp:$vmid:" . lc($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 undef if !$connect_str;
- if ($connect_str =~m/^pvespiceproxy:([a-z0-9]{8}):(\d+):(\S+)::([a-z0-9]{512}):(\d+)$/) {
+ 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 $sig = pack("H*", $hexsig);
-
- my $rsa_pub = get_pubkey();
-
my $plain = "pvespiceproxy:$timestamp:$vmid:$node";
+ my $sig = unpack("H*", Digest::SHA::sha1($plain, &$get_csrfr_secret()));
- if ($rsa_pub->verify($plain, $sig)) {
+ if ($sig eq $hexsig) {
return ($vmid, $node, $port);
}
}
return undef;
}
+sub read_x509_subject_spice {
+ my ($filename) = @_;
+
+ # read x509 subject
+ my $bio = Net::SSLeay::BIO_new_file($filename, 'r');
+ my $x509 = Net::SSLeay::PEM_read_bio_X509($bio);
+ Net::SSLeay::BIO_free($bio);
+ my $nameobj = Net::SSLeay::X509_get_subject_name($x509);
+ my $subject = Net::SSLeay::X509_NAME_oneline($nameobj);
+ Net::SSLeay::X509_free($x509);
+
+ # remote-viewer wants comma as seperator (not '/')
+ $subject =~ s!^/!!;
+ $subject =~ s!/(\w+=)!,$1!g;
+
+ return $subject;
+}
+
+# helper to generate SPICE remote-viewer configuration
+sub remote_viewer_config {
+ my ($authuser, $vmid, $node, $proxy, $title, $port) = @_;
+
+ if (!$proxy) {
+ my $host = `hostname -f` || PVE::INotify::nodename();
+ chomp $host;
+ $proxy = $host;
+ }
+
+ my ($ticket, $proxyticket) = assemble_spice_ticket($authuser, $vmid, $node);
+
+ my $filename = "/etc/pve/local/pve-ssl.pem";
+ my $subject = read_x509_subject_spice($filename);
+
+ my $cacert = PVE::Tools::file_get_contents("/etc/pve/pve-root-ca.pem", 8192);
+ $cacert =~ s/\n/\\n/g;
+
+ my $config = {
+ 'secure-attention' => "Ctrl+Alt+Ins",
+ 'toggle-fullscreen' => "Shift+F11",
+ 'release-cursor' => "Ctrl+Alt+R",
+ type => 'spice',
+ title => $title,
+ host => $proxyticket, # this break tls hostname verification, so we need to use 'host-subject'
+ proxy => "http://$proxy:3128",
+ 'tls-port' => $port,
+ 'host-subject' => $subject,
+ ca => $cacert,
+ password => $ticket,
+ 'delete-this-file' => 1,
+ };
+
+ return ($ticket, $proxyticket, $config);
+}
+
sub check_user_exist {
my ($usercfg, $username, $noerr) = @_;
return undef;
}
+sub verify_one_time_pw {
+ my ($usercfg, $username, $tfa_cfg, $otp) = @_;
+
+ my $type = $tfa_cfg->{type};
+
+ die "missing one time password for Factor-two authentication '$type'\n" if !$otp;
+
+ # fixme: proxy support?
+ my $proxy;
+
+ if ($type eq 'yubico') {
+ my $keys = $usercfg->{users}->{$username}->{keys};
+ yubico_verify_otp($otp, $keys, $tfa_cfg->{url}, $tfa_cfg->{id}, $tfa_cfg->{key}, $proxy);
+ } else {
+ die "unknown tfa type '$type'\n";
+ }
+}
+
# password should be utf8 encoded
+# Note: some pluging delay/sleep if auth fails
sub authenticate_user {
- my ($username, $password) = @_;
+ my ($username, $password, $otp) = @_;
die "no username specified\n" if !$username;
my $usercfg = cfs_read_file('user.cfg');
- eval { check_user_enabled($usercfg, $username); };
- if (my $err = $@) {
- sleep(2);
- die $err;
- }
+ check_user_enabled($usercfg, $username);
my $ctime = time();
my $expire = $usercfg->{users}->{$username}->{expire};
- if ($expire && ($expire < $ctime)) {
- sleep(2);
- die "account expired\n"
- }
+ die "account expired\n" if $expire && ($expire < $ctime);
my $domain_cfg = cfs_read_file('domains.cfg');
- eval {
- my $cfg = $domain_cfg->{ids}->{$realm};
- die "auth domain '$realm' does not exists\n" if !$cfg;
- my $plugin = PVE::Auth::Plugin->lookup($cfg->{type});
- $plugin->authenticate_user($cfg, $realm, $ruid, $password);
- };
- if (my $err = $@) {
- sleep(2); # timeout after failed auth
- die $err;
+ my $cfg = $domain_cfg->{ids}->{$realm};
+ die "auth domain '$realm' does not exists\n" if !$cfg;
+ my $plugin = PVE::Auth::Plugin->lookup($cfg->{type});
+ $plugin->authenticate_user($cfg, $realm, $ruid, $password);
+
+ if ($cfg->{tfa}) {
+ my $tfa_cfg = PVE::Auth::Plugin::parse_tfa_config($cfg->{tfa});
+ verify_one_time_pw($usercfg, $username, $tfa_cfg, $otp);
}
return $username;
$path = "/$path" if $path !~ m|^/|;
- return undef if $path !~ m|^[[:alnum:]\-\_\/]+$|;
+ return undef if $path !~ m|^[[:alnum:]\.\-\_\/]+$|;
return $path;
}
my $et = shift @data;
if ($et eq 'user') {
- my ($user, $enable, $expire, $firstname, $lastname, $email, $comment) = @data;
+ my ($user, $enable, $expire, $firstname, $lastname, $email, $comment, $keys) = @data;
my (undef, undef, $realm) = PVE::Auth::Plugin::verify_username($user, 1);
if (!$realm) {
$cfg->{users}->{$user}->{email} = $email;
$cfg->{users}->{$user}->{comment} = PVE::Tools::decode_text($comment) if $comment;
$cfg->{users}->{$user}->{expire} = $expire;
+ $cfg->{users}->{$user}->{keys} = $keys if $keys; # allowed yubico key ids
#$cfg->{users}->{$user}->{groups}->{$group} = 1;
#$cfg->{groups}->{$group}->{$user} = 1;
my $comment = $d->{comment} ? PVE::Tools::encode_text($d->{comment}) : '';
my $expire = int($d->{expire} || 0);
my $enable = $d->{enable} ? 1 : 0;
- $data .= "user:$user:$enable:$expire:$firstname:$lastname:$email:$comment:\n";
+ my $keys = $d->{keys} ? $d->{keys} : '';
+ $data .= "user:$user:$enable:$expire:$firstname:$lastname:$email:$comment:$keys:\n";
}
$data .= "\n";
lock_user_config($delVMfromPoolFn, "pool cleanup for VM $vmid failed");
}
+# experimental code for yubico OTP verification
+
+sub yubico_compute_param_sig {
+ my ($param, $api_key) = @_;
+
+ my $paramstr = '';
+ foreach my $key (sort keys %$param) {
+ $paramstr .= '&' if $paramstr;
+ $paramstr .= "$key=$param->{$key}";
+ }
+
+ my $sig = uri_escape(encode_base64(Digest::HMAC_SHA1::hmac_sha1($paramstr, decode_base64($api_key || '')), ''));
+
+ return ($paramstr, $sig);
+}
+
+sub yubico_verify_otp {
+ my ($otp, $keys, $url, $api_id, $api_key, $proxy) = @_;
+
+ die "yubico: missing password\n" if !defined($otp);
+ die "yubico: missing API ID\n" if !defined($api_id);
+ die "yubico: missing API KEY\n" if !defined($api_key);
+ die "yubico: no associated yubico keys\n" if $keys =~ m/^\s+$/;
+
+ die "yubico: wrong OTP lenght\n" if (length($otp) < 32) || (length($otp) > 48);
+
+ # we always use http, because https cert verification always make problem, and
+ # some proxies does not work with https.
+
+ $url = 'http://api2.yubico.com/wsapi/2.0/verify' if !defined($url);
+
+ my $params = {
+ nonce => Digest::HMAC_SHA1::hmac_sha1_hex(time(), rand()),
+ id => $api_id,
+ otp => uri_escape($otp),
+ timestamp => 1,
+ };
+
+ my ($paramstr, $sig) = yubico_compute_param_sig($params, $api_key);
+
+ $paramstr .= "&h=$sig" if $api_key;
+
+ my $req = HTTP::Request->new('GET' => "$url?$paramstr");
+
+ my $ua = LWP::UserAgent->new(protocols_allowed => ['http'], timeout => 30);
+
+ if ($proxy) {
+ $ua->proxy(['http'], $proxy);
+ } else {
+ $ua->env_proxy;
+ }
+
+ my $response = $ua->request($req);
+ my $code = $response->code;
+
+ if ($code != 200) {
+ my $msg = $response->message || 'unknown';
+ die "Invalid response from server: $code $msg\n";
+ }
+
+ my $raw = $response->decoded_content;
+
+ my $result = {};
+ foreach my $kvpair (split(/\n/, $raw)) {
+ chomp $kvpair;
+ if($kvpair =~ /^\S+=/) {
+ my ($k, $v) = split(/=/, $kvpair, 2);
+ $v =~ s/\s//g;
+ $result->{$k} = $v;
+ }
+ }
+
+ my $rsig = $result->{h};
+ delete $result->{h};
+
+ if ($api_key) {
+ my ($datastr, $vsig) = yubico_compute_param_sig($result, $api_key);
+ $vsig = uri_unescape($vsig);
+ die "yubico: result signature verification failed\n" if $rsig ne $vsig;
+ }
+
+ die "yubico auth failed: $result->{status}\n" if $result->{status} ne 'OK';
+
+ my $publicid = $result->{publicid} = substr(lc($result->{otp}), 0, 12);
+
+ my $found;
+ foreach my $k (PVE::Tools::split_list($keys)) {
+ if ($k eq $publicid) {
+ $found = 1;
+ last;
+ }
+ }
+
+ die "yubico auth failed: key does not belong to user\n" if !$found;
+
+ return $result;
+}
+
1;