1 package PVE::API2::AccessControl;
6 use PVE::Exception qw(raise raise_perm_exc);
8 use PVE::RPCEnvironment;
9 use PVE::Cluster qw(cfs_read_file);
11 use PVE::AccessControl;
12 use PVE::JSONSchema qw(get_standard_option);
13 use PVE::API2::Domains;
19 use base qw(PVE::RESTHandler);
21 __PACKAGE__->register_method ({
22 subclass => "PVE::API2::User",
26 __PACKAGE__->register_method ({
27 subclass => "PVE::API2::Group",
31 __PACKAGE__->register_method ({
32 subclass => "PVE::API2::Role",
36 __PACKAGE__->register_method ({
37 subclass => "PVE::API2::ACL",
41 __PACKAGE__->register_method ({
42 subclass => "PVE::API2::Domains",
46 __PACKAGE__->register_method ({
50 description => "Directory index.",
52 additionalProperties => 0,
60 subdir => { type => 'string' },
63 links => [ { rel => 'child', href => "{subdir}" } ],
70 my $ma = __PACKAGE__->method_attributes();
72 foreach my $info (@$ma) {
73 next if !$info->{subclass};
75 my $subpath = $info->{match_re}->[0];
77 push @$res, { subdir => $subpath };
80 push @$res, { subdir => 'ticket' };
81 push @$res, { subdir => 'password' };
87 my $verify_auth = sub {
88 my ($rpcenv, $username, $pw_or_ticket, $path, $privs) = @_;
90 my $normpath = PVE::AccessControl::normalize_path($path);
93 if (($ticketuser = PVE::AccessControl::verify_ticket($pw_or_ticket, 1)) &&
94 ($ticketuser eq $username)) {
96 } elsif (PVE::AccessControl::verify_vnc_ticket($pw_or_ticket, $username, $normpath, 1)) {
99 $username = PVE::AccessControl::authenticate_user($username, $pw_or_ticket);
102 my $privlist = [ PVE::Tools::split_list($privs) ];
103 if (!($normpath && scalar(@$privlist) && $rpcenv->check($username, $normpath, $privlist))) {
104 die "no permission ($path, $privs)\n";
107 return { username => $username };
110 my $create_ticket = sub {
111 my ($rpcenv, $username, $pw_or_ticket) = @_;
114 if (($ticketuser = PVE::AccessControl::verify_ticket($pw_or_ticket, 1)) &&
115 ($ticketuser eq 'root@pam' || $ticketuser eq $username)) {
116 # valid ticket. Note: root@pam can create tickets for other users
118 $username = PVE::AccessControl::authenticate_user($username, $pw_or_ticket);
121 my $ticket = PVE::AccessControl::assemble_ticket($username);
122 my $csrftoken = PVE::AccessControl::assemble_csrf_prevention_token($username);
126 username => $username,
127 CSRFPreventionToken => $csrftoken,
131 __PACKAGE__->register_method ({
132 name => 'create_ticket',
135 permissions => { user => 'world' },
136 protected => 1, # else we can't access shadow files
137 description => "Create or verify authentication ticket.",
139 additionalProperties => 0,
142 description => "User name",
146 realm => get_standard_option('realm', {
147 description => "You can optionally pass the realm using this parameter. Normally the realm is simply added to the username <username>\@<relam>.",
150 description => "The secret password. This can also be a valid ticket.",
154 description => "Verify ticket, and check if user have access 'privs' on 'path'",
161 description => "Verify ticket, and check if user have access 'privs' on 'path'",
162 type => 'string' , format => 'pve-priv-list',
172 username => { type => 'string' },
173 ticket => { type => 'string', optional => 1},
174 CSRFPreventionToken => { type => 'string', optional => 1 },
180 my $username = $param->{username};
181 $username .= "\@$param->{realm}" if $param->{realm};
183 my $rpcenv = PVE::RPCEnvironment::get();
188 # test if user exists and is enabled
189 $rpcenv->check_user_enabled($username);
191 if ($param->{path} && $param->{privs}) {
192 $res = &$verify_auth($rpcenv, $username, $param->{password},
193 $param->{path}, $param->{privs});
195 $res = &$create_ticket($rpcenv, $username, $param->{password});
199 my $clientip = $rpcenv->get_client_ip() || '';
200 syslog('err', "authentication failure; rhost=$clientip user=$username msg=$err");
204 PVE::Cluster::log_msg('info', 'root@pam', "successful auth for user '$username'");
209 __PACKAGE__->register_method ({
210 name => 'change_passsword',
213 permissions => { user => 'all' },
214 protected => 1, # else we can't access shadow files
215 description => "Change user password.",
217 additionalProperties => 0,
219 userid => get_standard_option('userid'),
221 description => "The new password.",
228 returns => { type => "null" },
232 my $rpcenv = PVE::RPCEnvironment::get();
233 my $authuser = $rpcenv->get_user();
235 my ($userid, $ruid, $realm) = PVE::AccessControl::verify_username($param->{userid});
237 my $usercfg = $rpcenv->{user_cfg};
238 PVE::AccessControl::check_user_exist($usercfg, $userid);
240 if ($authuser eq 'root@pam') {
241 # OK - root can change anything
243 if ($authuser eq $userid) {
244 $rpcenv->check_user_enabled($userid);
245 # OK - each user can change its own password
247 raise_perm_exc() if $userid eq 'root@pam';
249 my $privs = [ 'Sys.UserMod', 'Sys.UserAdd' ];
250 if (!$rpcenv->check_any($authuser, "/access", $privs, 1)) {
251 my $groups = $rpcenv->filter_groups($authuser, $privs, 1);
252 my $allowed_users = $rpcenv->group_member_join([keys %$groups]);
253 raise_perm_exc() if !$allowed_users->{$userid};
258 PVE::AccessControl::domain_set_password($realm, $ruid, $param->{password});
260 PVE::Cluster::log_msg('info', 'root@pam', "changed password for user '$userid'");