1 package PVE
::AccessControl
;
6 use Crypt
::OpenSSL
::Random
;
7 use Crypt
::OpenSSL
::RSA
;
11 use Digest
::HMAC_SHA1
;
14 use PVE
::Tools
qw(run_command lock_file file_get_contents split_list safe_print);
15 use PVE
::Cluster
qw(cfs_register_file cfs_read_file cfs_write_file cfs_lock_file);
18 use PVE
::Auth
::Plugin
;
24 use Data
::Dumper
; # fixme: remove
26 # load and initialize all plugins
28 PVE
::Auth
::AD-
>register();
29 PVE
::Auth
::LDAP-
>register();
30 PVE
::Auth
::PVE-
>register();
31 PVE
::Auth
::PAM-
>register();
32 PVE
::Auth
::Plugin-
>init();
34 # $authdir must be writable by root only!
35 my $confdir = "/etc/pve";
36 my $authdir = "$confdir/priv";
37 my $authprivkeyfn = "$authdir/authkey.key";
38 my $authpubkeyfn = "$confdir/authkey.pub";
39 my $pve_www_key_fn = "$confdir/pve-www.key";
41 my $ticket_lifetime = 3600*2; # 2 hours
43 Crypt
::OpenSSL
::RSA-
>import_random_seed();
45 cfs_register_file
('user.cfg',
51 PVE
::Auth
::Plugin
::verify_username
(@_);
54 sub pve_verify_realm
{
55 PVE
::Auth
::Plugin
::pve_verify_realm
(@_);
58 sub lock_user_config
{
59 my ($code, $errmsg) = @_;
61 cfs_lock_file
("user.cfg", undef, $code);
63 $errmsg ?
die "$errmsg: $err" : die $err;
70 return $pve_auth_pub_key if $pve_auth_pub_key;
72 my $input = PVE
::Tools
::file_get_contents
($authpubkeyfn);
74 $pve_auth_pub_key = Crypt
::OpenSSL
::RSA-
>new_public_key($input);
76 return $pve_auth_pub_key;
79 my $csrf_prevention_secret;
80 my $get_csrfr_secret = sub {
81 if (!$csrf_prevention_secret) {
82 my $input = PVE
::Tools
::file_get_contents
($pve_www_key_fn);
83 $csrf_prevention_secret = Digest
::SHA
::sha1_base64
($input);
85 return $csrf_prevention_secret;
88 sub assemble_csrf_prevention_token
{
91 my $timestamp = sprintf("%08X", time());
93 my $digest = Digest
::SHA
::sha1_base64
("$timestamp:$username", &$get_csrfr_secret());
95 return "$timestamp:$digest";
98 sub verify_csrf_prevention_token
{
99 my ($username, $token, $noerr) = @_;
101 if ($token =~ m/^([A-Z0-9]{8}):(\S+)$/) {
104 my $ttime = hex($timestamp);
106 my $digest = Digest
::SHA
::sha1_base64
("$timestamp:$username", &$get_csrfr_secret());
108 my $age = time() - $ttime;
109 return if ($digest eq $sig) && ($age > -300) && ($age < $ticket_lifetime);
112 die "Permission denied - invalid csrf token\n" if !$noerr;
117 my $pve_auth_priv_key;
120 return $pve_auth_priv_key if $pve_auth_priv_key;
122 my $input = PVE
::Tools
::file_get_contents
($authprivkeyfn);
124 $pve_auth_priv_key = Crypt
::OpenSSL
::RSA-
>new_private_key($input);
126 return $pve_auth_priv_key;
129 sub assemble_ticket
{
132 my $rsa_priv = get_privkey
();
134 my $timestamp = sprintf("%08X", time());
136 my $plain = "PVE:$username:$timestamp";
138 my $ticket = $plain . "::" . encode_base64
($rsa_priv->sign($plain), '');
144 my ($ticket, $noerr) = @_;
146 if ($ticket && $ticket =~ m/^(PVE:\S+)::([^:\s]+)$/) {
150 my $rsa_pub = get_pubkey
();
151 if ($rsa_pub->verify($plain, decode_base64
($sig))) {
152 if ($plain =~ m/^PVE:(\S+):([A-Z0-9]{8})$/) {
155 my $ttime = hex($timestamp);
157 my $age = time() - $ttime;
159 if (PVE
::Auth
::Plugin
::verify_username
($username, 1) &&
160 ($age > -300) && ($age < $ticket_lifetime)) {
161 return wantarray ?
($username, $age) : $username;
167 die "permission denied - invalid ticket\n" if !$noerr;
173 # - they do not contain the username in plain text
174 # - they are restricted to a specific resource path (example: '/vms/100')
175 sub assemble_vnc_ticket
{
176 my ($username, $path) = @_;
178 my $rsa_priv = get_privkey
();
180 my $timestamp = sprintf("%08X", time());
182 my $plain = "PVEVNC:$timestamp";
184 $path = normalize_path
($path);
186 my $full = "$plain:$username:$path";
188 my $ticket = $plain . "::" . encode_base64
($rsa_priv->sign($full), '');
193 sub verify_vnc_ticket
{
194 my ($ticket, $username, $path, $noerr) = @_;
196 if ($ticket && $ticket =~ m/^(PVEVNC:\S+)::([^:\s]+)$/) {
199 my $full = "$plain:$username:$path";
201 my $rsa_pub = get_pubkey
();
202 # Note: sign only match if $username and $path is correct
203 if ($rsa_pub->verify($full, decode_base64
($sig))) {
204 if ($plain =~ m/^PVEVNC:([A-Z0-9]{8})$/) {
207 my $age = time() - $ttime;
209 if (($age > -20) && ($age < 40)) {
216 die "permission denied - invalid vnc ticket\n" if !$noerr;
221 sub assemble_spice_ticket
{
222 my ($username, $vmid, $node) = @_;
224 my $rsa_priv = get_privkey
();
226 my $timestamp = sprintf("%08x", time());
228 my $randomstr = "PVESPICE:$timestamp:$vmid:$node:" . rand(10);
230 # this should be uses as one-time password
231 # max length is 60 chars (spice limit)
232 # we pass this to qemu set_pasword and limit lifetime there
234 my $ticket = Digest
::SHA
::sha1_hex
($rsa_priv->sign($randomstr));
236 # Note: spice proxy connects with HTTP, so $proxyticket is exposed to public
237 # we use a signature/timestamp to make sure nobody can fake such ticket
238 # an attacker can use this $proxyticket, but he will fail because $ticket is
240 # The proxy need to be able to extract/verify the ticket
241 # Note: data needs to be lower case only, because virt-viewer needs that
242 # Note: RSA signature are too long (>=256 charaters) and makes problems with remote-viewer
244 my $secret = &$get_csrfr_secret();
245 my $plain = "pvespiceproxy:$timestamp:$vmid:" . lc($node);
247 # produces 40 characters
248 my $sig = unpack("H*", Digest
::SHA
::sha1
($plain, &$get_csrfr_secret()));
250 #my $sig = unpack("H*", $rsa_priv->sign($plain)); # this produce too long strings (512)
252 my $proxyticket = $plain . "::" . $sig;
254 return ($ticket, $proxyticket);
257 sub verify_spice_connect_url
{
258 my ($connect_str) = @_;
260 # Note: we pass the spice ticket as 'host', so the
261 # spice viewer connects with "$ticket:$port"
263 return undef if !$connect_str;
265 if ($connect_str =~m/^pvespiceproxy:([a-z0-9]{8}):(\d+):(\S+)::([a-z0-9]{40}):(\d+)$/) {
266 my ($timestamp, $vmid, $node, $hexsig, $port) = ($1, $2, $3, $4, $5, $6);
267 my $ttime = hex($timestamp);
268 my $age = time() - $ttime;
270 # use very limited lifetime - is this enough?
271 return undef if !(($age > -20) && ($age < 40));
273 my $plain = "pvespiceproxy:$timestamp:$vmid:$node";
274 my $sig = unpack("H*", Digest
::SHA
::sha1
($plain, &$get_csrfr_secret()));
276 if ($sig eq $hexsig) {
277 return ($vmid, $node, $port);
284 sub read_x509_subject_spice
{
288 my $bio = Net
::SSLeay
::BIO_new_file
($filename, 'r');
289 my $x509 = Net
::SSLeay
::PEM_read_bio_X509
($bio);
290 Net
::SSLeay
::BIO_free
($bio);
291 my $nameobj = Net
::SSLeay
::X509_get_subject_name
($x509);
292 my $subject = Net
::SSLeay
::X509_NAME_oneline
($nameobj);
293 Net
::SSLeay
::X509_free
($x509);
295 # remote-viewer wants comma as seperator (not '/')
297 $subject =~ s!/(\w+=)!,$1!g;
302 # helper to generate SPICE remote-viewer configuration
303 sub remote_viewer_config
{
304 my ($authuser, $vmid, $node, $proxy, $title, $port) = @_;
307 my $host = `hostname -f` || PVE
::INotify
::nodename
();
312 my ($ticket, $proxyticket) = assemble_spice_ticket
($authuser, $vmid, $node);
314 my $filename = "/etc/pve/local/pve-ssl.pem";
315 my $subject = read_x509_subject_spice
($filename);
317 my $cacert = PVE
::Tools
::file_get_contents
("/etc/pve/pve-root-ca.pem", 8192);
318 $cacert =~ s/\n/\\n/g;
321 'secure-attention' => "Ctrl+Alt+Ins",
322 'toggle-fullscreen' => "Shift+F11",
323 'release-cursor' => "Ctrl+Alt+R",
326 host
=> $proxyticket, # this break tls hostname verification, so we need to use 'host-subject'
327 proxy
=> "http://$proxy:3128",
329 'host-subject' => $subject,
332 'delete-this-file' => 1,
335 return ($ticket, $proxyticket, $config);
338 sub check_user_exist
{
339 my ($usercfg, $username, $noerr) = @_;
341 $username = PVE
::Auth
::Plugin
::verify_username
($username, $noerr);
342 return undef if !$username;
344 return $usercfg->{users
}->{$username} if $usercfg && $usercfg->{users
}->{$username};
346 die "no such user ('$username')\n" if !$noerr;
351 sub check_user_enabled
{
352 my ($usercfg, $username, $noerr) = @_;
354 my $data = check_user_exist
($usercfg, $username, $noerr);
355 return undef if !$data;
357 return 1 if $data->{enable
};
359 return 1 if $username eq 'root@pam'; # root is always enabled
361 die "user '$username' is disabled\n" if !$noerr;
366 sub verify_one_time_pw
{
367 my ($usercfg, $username, $tfa_cfg, $otp) = @_;
369 my $type = $tfa_cfg->{type
};
371 die "missing one time password for Factor-two authentication '$type'\n" if !$otp;
373 # fixme: proxy support?
376 if ($type eq 'yubico') {
377 my $keys = $usercfg->{users
}->{$username}->{keys};
378 yubico_verify_otp
($otp, $keys, $tfa_cfg->{url
}, $tfa_cfg->{id
}, $tfa_cfg->{key
}, $proxy);
379 } elsif ($type eq 'oath') {
380 my $keys = $usercfg->{users
}->{$username}->{keys};
381 oath_verify_otp
($otp, $keys);
383 die "unknown tfa type '$type'\n";
387 # password should be utf8 encoded
388 # Note: some pluging delay/sleep if auth fails
389 sub authenticate_user
{
390 my ($username, $password, $otp) = @_;
392 die "no username specified\n" if !$username;
396 ($username, $ruid, $realm) = PVE
::Auth
::Plugin
::verify_username
($username);
398 my $usercfg = cfs_read_file
('user.cfg');
400 check_user_enabled
($usercfg, $username);
403 my $expire = $usercfg->{users
}->{$username}->{expire
};
405 die "account expired\n" if $expire && ($expire < $ctime);
407 my $domain_cfg = cfs_read_file
('domains.cfg');
409 my $cfg = $domain_cfg->{ids
}->{$realm};
410 die "auth domain '$realm' does not exists\n" if !$cfg;
411 my $plugin = PVE
::Auth
::Plugin-
>lookup($cfg->{type
});
412 $plugin->authenticate_user($cfg, $realm, $ruid, $password);
415 my $tfa_cfg = PVE
::Auth
::Plugin
::parse_tfa_config
($cfg->{tfa
});
416 verify_one_time_pw
($usercfg, $username, $tfa_cfg, $otp);
422 sub domain_set_password
{
423 my ($realm, $username, $password) = @_;
425 die "no auth domain specified" if !$realm;
427 my $domain_cfg = cfs_read_file
('domains.cfg');
429 my $cfg = $domain_cfg->{ids
}->{$realm};
430 die "auth domain '$realm' does not exists\n" if !$cfg;
431 my $plugin = PVE
::Auth
::Plugin-
>lookup($cfg->{type
});
432 $plugin->store_password($cfg, $realm, $username, $password);
437 my ($username, $usercfg, $group) = @_;
438 $usercfg->{users
}->{$username}->{groups
}->{$group} = 1;
439 $usercfg->{groups
}->{$group}->{users
}->{$username} = 1;
442 sub delete_user_group
{
444 my ($username, $usercfg) = @_;
446 foreach my $group (keys %{$usercfg->{groups
}}) {
448 delete ($usercfg->{groups
}->{$group}->{users
}->{$username})
449 if $usercfg->{groups
}->{$group}->{users
}->{$username};
453 sub delete_user_acl
{
455 my ($username, $usercfg) = @_;
457 foreach my $acl (keys %{$usercfg->{acl
}}) {
459 delete ($usercfg->{acl
}->{$acl}->{users
}->{$username})
460 if $usercfg->{acl
}->{$acl}->{users
}->{$username};
464 sub delete_group_acl
{
466 my ($group, $usercfg) = @_;
468 foreach my $acl (keys %{$usercfg->{acl
}}) {
470 delete ($usercfg->{acl
}->{$acl}->{groups
}->{$group})
471 if $usercfg->{acl
}->{$acl}->{groups
}->{$group};
475 sub delete_pool_acl
{
477 my ($pool, $usercfg) = @_;
479 my $path = "/pool/$pool";
481 foreach my $aclpath (keys %{$usercfg->{acl
}}) {
482 delete ($usercfg->{acl
}->{$aclpath})
483 if $usercfg->{acl
}->{$aclpath} eq 'path';
487 # we automatically create some predefined roles by splitting privs
488 # into 3 groups (per category)
489 # root: only root is allowed to do that
490 # admin: an administrator can to that
491 # user: a normak user/customer can to that
501 'VM.Config.Options', # covers all other things
509 'VM.Config.CDROM', # change CDROM media
521 'Sys.Modify', # edit/change node settings
524 'Permissions.Modify',
536 'Datastore.Allocate',
537 'Datastore.AllocateTemplate',
540 'Datastore.AllocateSpace',
552 'Group.Allocate', # edit/change group settings
553 'Realm.AllocateUser',
561 'Pool.Allocate', # create/delete pools
568 my $valid_privs = {};
570 my $special_roles = {
571 'NoAccess' => {}, # no priviledges
572 'Administrator' => $valid_privs, # all priviledges
577 foreach my $cat (keys %$privgroups) {
578 my $cd = $privgroups->{$cat};
579 foreach my $p (@{$cd->{root
}}, @{$cd->{admin
}},
580 @{$cd->{user
}}, @{$cd->{audit
}}) {
581 $valid_privs->{$p} = 1;
583 foreach my $p (@{$cd->{admin
}}, @{$cd->{user
}}, @{$cd->{audit
}}) {
585 $special_roles->{"PVE${cat}Admin"}->{$p} = 1;
586 $special_roles->{"PVEAdmin"}->{$p} = 1;
588 if (scalar(@{$cd->{user
}})) {
589 foreach my $p (@{$cd->{user
}}, @{$cd->{audit
}}) {
590 $special_roles->{"PVE${cat}User"}->{$p} = 1;
593 foreach my $p (@{$cd->{audit
}}) {
594 $special_roles->{"PVEAuditor"}->{$p} = 1;
598 $special_roles->{"PVETemplateUser"} = { 'VM.Clone' => 1, 'VM.Audit' => 1 };
604 my ($role, $usercfg, $privs) = @_;
608 die "role '$role' does not exist\n" if !$usercfg->{roles
}->{$role};
610 foreach my $priv (split_list
($privs)) {
611 if (defined ($valid_privs->{$priv})) {
612 $usercfg->{roles
}->{$role}->{$priv} = 1;
614 die "invalid priviledge '$priv'\n";
626 $path = '/' if !$path;
628 $path = "/$path" if $path !~ m
|^/|;
630 return undef if $path !~ m
|^[[:alnum
:]\
.\
-\_\
/]+$|;
636 PVE
::JSONSchema
::register_format
('pve-groupid', \
&verify_groupname
);
637 sub verify_groupname
{
638 my ($groupname, $noerr) = @_;
640 if ($groupname !~ m/^[A-Za-z0-9\.\-_]+$/) {
642 die "group name '$groupname' contains invalid characters\n" if !$noerr;
650 PVE
::JSONSchema
::register_format
('pve-roleid', \
&verify_rolename
);
651 sub verify_rolename
{
652 my ($rolename, $noerr) = @_;
654 if ($rolename !~ m/^[A-Za-z0-9\.\-_]+$/) {
656 die "role name '$rolename' contains invalid characters\n" if !$noerr;
664 PVE
::JSONSchema
::register_format
('pve-poolid', \
&verify_groupname
);
665 sub verify_poolname
{
666 my ($poolname, $noerr) = @_;
668 if ($poolname !~ m/^[A-Za-z0-9\.\-_]+$/) {
670 die "pool name '$poolname' contains invalid characters\n" if !$noerr;
678 PVE
::JSONSchema
::register_format
('pve-priv', \
&verify_privname
);
679 sub verify_privname
{
680 my ($priv, $noerr) = @_;
682 if (!$valid_privs->{$priv}) {
683 die "invalid priviledge '$priv'\n" if !$noerr;
691 sub userconfig_force_defaults
{
694 foreach my $r (keys %$special_roles) {
695 $cfg->{roles
}->{$r} = $special_roles->{$r};
698 # fixme: remove 'root' group (not required)?
701 $cfg->{users
}->{'root@pam'}->{enable
} = 1;
704 sub parse_user_config
{
705 my ($filename, $raw) = @_;
709 userconfig_force_defaults
($cfg);
711 while ($raw && $raw =~ s/^(.*?)(\n|$)//) {
714 next if $line =~ m/^\s*$/; # skip empty lines
718 foreach my $d (split (/:/, $line)) {
724 my $et = shift @data;
727 my ($user, $enable, $expire, $firstname, $lastname, $email, $comment, $keys) = @data;
729 my (undef, undef, $realm) = PVE
::Auth
::Plugin
::verify_username
($user, 1);
731 warn "user config - ignore user '$user' - invalid user name\n";
735 $enable = $enable ?
1 : 0;
737 $expire = 0 if !$expire;
739 if ($expire !~ m/^\d+$/) {
740 warn "user config - ignore user '$user' - (illegal characters in expire '$expire')\n";
743 $expire = int($expire);
745 #if (!verify_groupname ($group, 1)) {
746 # warn "user config - ignore user '$user' - invalid characters in group name\n";
750 $cfg->{users
}->{$user} = {
754 $cfg->{users
}->{$user}->{firstname
} = PVE
::Tools
::decode_text
($firstname) if $firstname;
755 $cfg->{users
}->{$user}->{lastname
} = PVE
::Tools
::decode_text
($lastname) if $lastname;
756 $cfg->{users
}->{$user}->{email
} = $email;
757 $cfg->{users
}->{$user}->{comment
} = PVE
::Tools
::decode_text
($comment) if $comment;
758 $cfg->{users
}->{$user}->{expire
} = $expire;
759 # keys: allowed yubico key ids or oath secrets (base32 encoded)
760 $cfg->{users
}->{$user}->{keys} = $keys if $keys;
762 #$cfg->{users}->{$user}->{groups}->{$group} = 1;
763 #$cfg->{groups}->{$group}->{$user} = 1;
765 } elsif ($et eq 'group') {
766 my ($group, $userlist, $comment) = @data;
768 if (!verify_groupname
($group, 1)) {
769 warn "user config - ignore group '$group' - invalid characters in group name\n";
773 # make sure to add the group (even if there are no members)
774 $cfg->{groups
}->{$group} = { users
=> {} } if !$cfg->{groups
}->{$group};
776 $cfg->{groups
}->{$group}->{comment
} = PVE
::Tools
::decode_text
($comment) if $comment;
778 foreach my $user (split_list
($userlist)) {
780 if (!PVE
::Auth
::Plugin
::verify_username
($user, 1)) {
781 warn "user config - ignore invalid group member '$user'\n";
785 if ($cfg->{users
}->{$user}) { # user exists
786 $cfg->{users
}->{$user}->{groups
}->{$group} = 1;
787 $cfg->{groups
}->{$group}->{users
}->{$user} = 1;
789 warn "user config - ignore invalid group member '$user'\n";
793 } elsif ($et eq 'role') {
794 my ($role, $privlist) = @data;
796 if (!verify_rolename
($role, 1)) {
797 warn "user config - ignore role '$role' - invalid characters in role name\n";
801 # make sure to add the role (even if there are no privileges)
802 $cfg->{roles
}->{$role} = {} if !$cfg->{roles
}->{$role};
804 foreach my $priv (split_list
($privlist)) {
805 if (defined ($valid_privs->{$priv})) {
806 $cfg->{roles
}->{$role}->{$priv} = 1;
808 warn "user config - ignore invalid priviledge '$priv'\n";
812 } elsif ($et eq 'acl') {
813 my ($propagate, $pathtxt, $uglist, $rolelist) = @data;
815 if (my $path = normalize_path
($pathtxt)) {
816 foreach my $role (split_list
($rolelist)) {
818 if (!verify_rolename
($role, 1)) {
819 warn "user config - ignore invalid role name '$role' in acl\n";
823 foreach my $ug (split_list
($uglist)) {
824 if ($ug =~ m/^@(\S+)$/) {
826 if ($cfg->{groups
}->{$group}) { # group exists
827 $cfg->{acl
}->{$path}->{groups
}->{$group}->{$role} = $propagate;
829 warn "user config - ignore invalid acl group '$group'\n";
831 } elsif (PVE
::Auth
::Plugin
::verify_username
($ug, 1)) {
832 if ($cfg->{users
}->{$ug}) { # user exists
833 $cfg->{acl
}->{$path}->{users
}->{$ug}->{$role} = $propagate;
835 warn "user config - ignore invalid acl member '$ug'\n";
838 warn "user config - invalid user/group '$ug' in acl\n";
843 warn "user config - ignore invalid path in acl '$pathtxt'\n";
845 } elsif ($et eq 'pool') {
846 my ($pool, $comment, $vmlist, $storelist) = @data;
848 if (!verify_poolname
($pool, 1)) {
849 warn "user config - ignore pool '$pool' - invalid characters in pool name\n";
853 # make sure to add the pool (even if there are no members)
854 $cfg->{pools
}->{$pool} = { vms
=> {}, storage
=> {} } if !$cfg->{pools
}->{$pool};
856 $cfg->{pools
}->{$pool}->{comment
} = PVE
::Tools
::decode_text
($comment) if $comment;
858 foreach my $vmid (split_list
($vmlist)) {
859 if ($vmid !~ m/^\d+$/) {
860 warn "user config - ignore invalid vmid '$vmid' in pool '$pool'\n";
865 if ($cfg->{vms
}->{$vmid}) {
866 warn "user config - ignore duplicate vmid '$vmid' in pool '$pool'\n";
870 $cfg->{pools
}->{$pool}->{vms
}->{$vmid} = 1;
872 # record vmid ==> pool relation
873 $cfg->{vms
}->{$vmid} = $pool;
876 foreach my $storeid (split_list
($storelist)) {
877 if ($storeid !~ m/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i) {
878 warn "user config - ignore invalid storage '$storeid' in pool '$pool'\n";
881 $cfg->{pools
}->{$pool}->{storage
}->{$storeid} = 1;
884 warn "user config - ignore config line: $line\n";
888 userconfig_force_defaults
($cfg);
893 sub write_user_config
{
894 my ($filename, $cfg) = @_;
898 foreach my $user (keys %{$cfg->{users
}}) {
899 my $d = $cfg->{users
}->{$user};
900 my $firstname = $d->{firstname
} ? PVE
::Tools
::encode_text
($d->{firstname
}) : '';
901 my $lastname = $d->{lastname
} ? PVE
::Tools
::encode_text
($d->{lastname
}) : '';
902 my $email = $d->{email
} || '';
903 my $comment = $d->{comment
} ? PVE
::Tools
::encode_text
($d->{comment
}) : '';
904 my $expire = int($d->{expire
} || 0);
905 my $enable = $d->{enable
} ?
1 : 0;
906 my $keys = $d->{keys} ?
$d->{keys} : '';
907 $data .= "user:$user:$enable:$expire:$firstname:$lastname:$email:$comment:$keys:\n";
912 foreach my $group (keys %{$cfg->{groups
}}) {
913 my $d = $cfg->{groups
}->{$group};
914 my $list = join (',', keys %{$d->{users
}});
915 my $comment = $d->{comment
} ? PVE
::Tools
::encode_text
($d->{comment
}) : '';
916 $data .= "group:$group:$list:$comment:\n";
921 foreach my $pool (keys %{$cfg->{pools
}}) {
922 my $d = $cfg->{pools
}->{$pool};
923 my $vmlist = join (',', keys %{$d->{vms
}});
924 my $storelist = join (',', keys %{$d->{storage
}});
925 my $comment = $d->{comment
} ? PVE
::Tools
::encode_text
($d->{comment
}) : '';
926 $data .= "pool:$pool:$comment:$vmlist:$storelist:\n";
931 foreach my $role (keys %{$cfg->{roles
}}) {
932 next if $special_roles->{$role};
934 my $d = $cfg->{roles
}->{$role};
935 my $list = join (',', keys %$d);
936 $data .= "role:$role:$list:\n";
941 foreach my $path (sort keys %{$cfg->{acl
}}) {
942 my $d = $cfg->{acl
}->{$path};
946 foreach my $group (keys %{$d->{groups
}}) {
949 foreach my $role (sort keys %{$d->{groups
}->{$group}}) {
950 my $propagate = $d->{groups
}->{$group}->{$role};
959 $ra->{0}->{$l0}->{"\@$group"} = 1 if $l0;
960 $ra->{1}->{$l1}->{"\@$group"} = 1 if $l1;
963 foreach my $user (keys %{$d->{users
}}) {
964 # no need to save, because root is always 'Administartor'
965 next if $user eq 'root@pam';
969 foreach my $role (sort keys %{$d->{users
}->{$user}}) {
970 my $propagate = $d->{users
}->{$user}->{$role};
979 $ra->{0}->{$l0}->{$user} = 1 if $l0;
980 $ra->{1}->{$l1}->{$user} = 1 if $l1;
983 foreach my $rolelist (sort keys %{$ra->{0}}) {
984 my $uglist = join (',', keys %{$ra->{0}->{$rolelist}});
985 $data .= "acl:0:$path:$uglist:$rolelist:\n";
987 foreach my $rolelist (sort keys %{$ra->{1}}) {
988 my $uglist = join (',', keys %{$ra->{1}->{$rolelist}});
989 $data .= "acl:1:$path:$uglist:$rolelist:\n";
997 my ($cfg, $user, $path) = @_;
999 # NOTE: we do not consider pools here.
1000 # You need to use $rpcenv->roles() instead if you want that.
1002 return 'Administrator' if $user eq 'root@pam'; # root can do anything
1006 foreach my $p (sort keys %{$cfg->{acl
}}) {
1007 my $final = ($path eq $p);
1009 next if !(($p eq '/') || $final || ($path =~ m
|^$p/|));
1011 my $acl = $cfg->{acl
}->{$p};
1013 #print "CHECKACL $path $p\n";
1014 #print "ACL $path = " . Dumper ($acl);
1016 if (my $ri = $acl->{users
}->{$user}) {
1018 foreach my $role (keys %$ri) {
1019 my $propagate = $ri->{$role};
1020 if ($final || $propagate) {
1021 #print "APPLY ROLE $p $user $role\n";
1027 $perm = $new; # overwrite previous settings
1028 next; # user privs always override group privs
1033 foreach my $g (keys %{$acl->{groups
}}) {
1034 next if !$cfg->{groups
}->{$g}->{users
}->{$user};
1035 if (my $ri = $acl->{groups
}->{$g}) {
1036 foreach my $role (keys %$ri) {
1037 my $propagate = $ri->{$role};
1038 if ($final || $propagate) {
1039 #print "APPLY ROLE $p \@$g $role\n";
1047 $perm = $new; # overwrite previous settings
1052 return ('NoAccess') if defined ($perm->{NoAccess
});
1053 #return () if defined ($perm->{NoAccess});
1055 #print "permission $user $path = " . Dumper ($perm);
1057 my @ra = keys %$perm;
1059 #print "roles $user $path = " . join (',', @ra) . "\n";
1065 my ($cfg, $user, $path) = @_;
1067 $user = PVE
::Auth
::Plugin
::verify_username
($user, 1);
1068 return {} if !$user;
1070 my @ra = roles
($cfg, $user, $path);
1074 foreach my $role (@ra) {
1075 if (my $privset = $cfg->{roles
}->{$role}) {
1076 foreach my $p (keys %$privset) {
1082 #print "priviledges $user $path = " . Dumper ($privs);
1087 sub check_permissions
{
1088 my ($username, $path, $privlist) = @_;
1090 $path = normalize_path
($path);
1091 my $usercfg = cfs_read_file
('user.cfg');
1092 my $perm = permission
($usercfg, $username, $path);
1094 foreach my $priv (split_list
($privlist)) {
1095 return undef if !$perm->{$priv};
1101 sub add_vm_to_pool
{
1102 my ($vmid, $pool) = @_;
1104 my $addVMtoPoolFn = sub {
1105 my $usercfg = cfs_read_file
("user.cfg");
1106 if (my $data = $usercfg->{pools
}->{$pool}) {
1107 $data->{vms
}->{$vmid} = 1;
1108 $usercfg->{vms
}->{$vmid} = $pool;
1109 cfs_write_file
("user.cfg", $usercfg);
1113 lock_user_config
($addVMtoPoolFn, "can't add VM $vmid to pool '$pool'");
1116 sub remove_vm_from_pool
{
1119 my $delVMfromPoolFn = sub {
1120 my $usercfg = cfs_read_file
("user.cfg");
1121 if (my $pool = $usercfg->{vms
}->{$vmid}) {
1122 if (my $data = $usercfg->{pools
}->{$pool}) {
1123 delete $data->{vms
}->{$vmid};
1124 delete $usercfg->{vms
}->{$vmid};
1125 cfs_write_file
("user.cfg", $usercfg);
1130 lock_user_config
($delVMfromPoolFn, "pool cleanup for VM $vmid failed");
1133 # experimental code for yubico OTP verification
1135 sub yubico_compute_param_sig
{
1136 my ($param, $api_key) = @_;
1139 foreach my $key (sort keys %$param) {
1140 $paramstr .= '&' if $paramstr;
1141 $paramstr .= "$key=$param->{$key}";
1144 my $sig = uri_escape
(encode_base64
(Digest
::HMAC_SHA1
::hmac_sha1
($paramstr, decode_base64
($api_key || '')), ''));
1146 return ($paramstr, $sig);
1149 sub yubico_verify_otp
{
1150 my ($otp, $keys, $url, $api_id, $api_key, $proxy) = @_;
1152 die "yubico: missing password\n" if !defined($otp);
1153 die "yubico: missing API ID\n" if !defined($api_id);
1154 die "yubico: missing API KEY\n" if !defined($api_key);
1155 die "yubico: no associated yubico keys\n" if $keys =~ m/^\s+$/;
1157 die "yubico: wrong OTP lenght\n" if (length($otp) < 32) || (length($otp) > 48);
1159 # we always use http, because https cert verification always make problem, and
1160 # some proxies does not work with https.
1162 $url = 'http://api2.yubico.com/wsapi/2.0/verify' if !defined($url);
1165 nonce
=> Digest
::HMAC_SHA1
::hmac_sha1_hex
(time(), rand()),
1167 otp
=> uri_escape
($otp),
1171 my ($paramstr, $sig) = yubico_compute_param_sig
($params, $api_key);
1173 $paramstr .= "&h=$sig" if $api_key;
1175 my $req = HTTP
::Request-
>new('GET' => "$url?$paramstr");
1177 my $ua = LWP
::UserAgent-
>new(protocols_allowed
=> ['http'], timeout
=> 30);
1180 $ua->proxy(['http'], $proxy);
1185 my $response = $ua->request($req);
1186 my $code = $response->code;
1189 my $msg = $response->message || 'unknown';
1190 die "Invalid response from server: $code $msg\n";
1193 my $raw = $response->decoded_content;
1196 foreach my $kvpair (split(/\n/, $raw)) {
1198 if($kvpair =~ /^\S+=/) {
1199 my ($k, $v) = split(/=/, $kvpair, 2);
1205 my $rsig = $result->{h
};
1206 delete $result->{h
};
1209 my ($datastr, $vsig) = yubico_compute_param_sig
($result, $api_key);
1210 $vsig = uri_unescape
($vsig);
1211 die "yubico: result signature verification failed\n" if $rsig ne $vsig;
1214 die "yubico auth failed: $result->{status}\n" if $result->{status
} ne 'OK';
1216 my $publicid = $result->{publicid
} = substr(lc($result->{otp
}), 0, 12);
1219 foreach my $k (PVE
::Tools
::split_list
($keys)) {
1220 if ($k eq $publicid) {
1226 die "yubico auth failed: key does not belong to user\n" if !$found;
1231 sub oath_verify_otp
{
1232 my ($otp, $keys) = @_;
1234 die "oath: missing password\n" if !defined($otp);
1235 die "oath: no associated oath keys\n" if $keys =~ m/^\s+$/;
1244 if ($line =~ m/^\d{6}$/) {
1245 print "GOT:$line\n";
1246 $found = 1 if $otp eq $line;
1250 foreach my $k (PVE
::Tools
::split_list
($keys)) {
1251 # Note: we generate 3 values to allow small time drift
1252 my $now = localtime(time() - $step);
1253 my $cmd = ['oathtool', '--totp', '-N', $now, '-s', $step, '-w', '2', '-b', $k];
1254 eval { run_command
($cmd, outfunc
=> $parser, errfunc
=> sub {}); };
1258 die "oath auth failed\n" if !$found;