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
;
20 use base
qw(PVE::RESTHandler);
22 __PACKAGE__-
>register_method ({
23 subclass
=> "PVE::API2::User",
27 __PACKAGE__-
>register_method ({
28 subclass
=> "PVE::API2::Group",
32 __PACKAGE__-
>register_method ({
33 subclass
=> "PVE::API2::Role",
37 __PACKAGE__-
>register_method ({
38 subclass
=> "PVE::API2::ACL",
42 __PACKAGE__-
>register_method ({
43 subclass
=> "PVE::API2::Domains",
47 __PACKAGE__-
>register_method ({
48 subclass
=> "PVE::API2::Pool",
52 __PACKAGE__-
>register_method ({
56 description
=> "Directory index.",
61 additionalProperties
=> 0,
69 subdir
=> { type
=> 'string' },
72 links
=> [ { rel
=> 'child', href
=> "{subdir}" } ],
79 my $ma = __PACKAGE__-
>method_attributes();
81 foreach my $info (@$ma) {
82 next if !$info->{subclass
};
84 my $subpath = $info->{match_re
}->[0];
86 push @$res, { subdir
=> $subpath };
89 push @$res, { subdir
=> 'ticket' };
90 push @$res, { subdir
=> 'password' };
96 my $verify_auth = sub {
97 my ($rpcenv, $username, $pw_or_ticket, $path, $privs) = @_;
99 my $normpath = PVE
::AccessControl
::normalize_path
($path);
102 if (($ticketuser = PVE
::AccessControl
::verify_ticket
($pw_or_ticket, 1)) &&
103 ($ticketuser eq $username)) {
105 } elsif (PVE
::AccessControl
::verify_vnc_ticket
($pw_or_ticket, $username, $normpath, 1)) {
108 $username = PVE
::AccessControl
::authenticate_user
($username, $pw_or_ticket);
111 my $privlist = [ PVE
::Tools
::split_list
($privs) ];
112 if (!($normpath && scalar(@$privlist) && $rpcenv->check($username, $normpath, $privlist))) {
113 die "no permission ($path, $privs)\n";
116 return { username
=> $username };
119 my $create_ticket = sub {
120 my ($rpcenv, $username, $pw_or_ticket) = @_;
123 if (($ticketuser = PVE
::AccessControl
::verify_ticket
($pw_or_ticket, 1)) &&
124 ($ticketuser eq 'root@pam' || $ticketuser eq $username)) {
125 # valid ticket. Note: root@pam can create tickets for other users
127 $username = PVE
::AccessControl
::authenticate_user
($username, $pw_or_ticket);
130 my $ticket = PVE
::AccessControl
::assemble_ticket
($username);
131 my $csrftoken = PVE
::AccessControl
::assemble_csrf_prevention_token
($username);
135 username
=> $username,
136 CSRFPreventionToken
=> $csrftoken,
140 __PACKAGE__-
>register_method ({
141 name
=> 'create_ticket',
145 description
=> "You need to pass valid credientials.",
148 protected
=> 1, # else we can't access shadow files
149 description
=> "Create or verify authentication ticket.",
151 additionalProperties
=> 0,
154 description
=> "User name",
158 realm
=> get_standard_option
('realm', {
159 description
=> "You can optionally pass the realm using this parameter. Normally the realm is simply added to the username <username>\@<relam>.",
162 description
=> "The secret password. This can also be a valid ticket.",
166 description
=> "Verify ticket, and check if user have access 'privs' on 'path'",
173 description
=> "Verify ticket, and check if user have access 'privs' on 'path'",
174 type
=> 'string' , format
=> 'pve-priv-list',
184 username
=> { type
=> 'string' },
185 ticket
=> { type
=> 'string', optional
=> 1},
186 CSRFPreventionToken
=> { type
=> 'string', optional
=> 1 },
192 my $username = $param->{username
};
193 $username .= "\@$param->{realm}" if $param->{realm
};
195 my $rpcenv = PVE
::RPCEnvironment
::get
();
200 # test if user exists and is enabled
201 $rpcenv->check_user_enabled($username);
203 if ($param->{path
} && $param->{privs
}) {
204 $res = &$verify_auth($rpcenv, $username, $param->{password
},
205 $param->{path
}, $param->{privs
});
207 $res = &$create_ticket($rpcenv, $username, $param->{password
});
211 my $clientip = $rpcenv->get_client_ip() || '';
212 syslog
('err', "authentication failure; rhost=$clientip user=$username msg=$err");
216 PVE
::Cluster
::log_msg
('info', 'root@pam', "successful auth for user '$username'");
221 __PACKAGE__-
>register_method ({
222 name
=> 'change_passsword',
226 description
=> "Each user is allowed to change his own password. A user can change the password of another user if he has 'Realm.AllocateUser' (on the realm of user <userid>) and 'User.Modify' permission on /access/groups/<group> on a group where user <userid> is member of.",
228 ['userid-param', 'self'],
230 [ 'userid-param', 'Realm.AllocateUser'],
231 [ 'userid-group', ['User.Modify']]
235 protected
=> 1, # else we can't access shadow files
236 description
=> "Change user password.",
238 additionalProperties
=> 0,
240 userid
=> get_standard_option
('userid'),
242 description
=> "The new password.",
249 returns
=> { type
=> "null" },
253 my $rpcenv = PVE
::RPCEnvironment
::get
();
254 my $authuser = $rpcenv->get_user();
256 my ($userid, $ruid, $realm) = PVE
::AccessControl
::verify_username
($param->{userid
});
258 $rpcenv->check_user_exist($userid);
260 if ($authuser eq 'root@pam') {
261 # OK - root can change anything
263 if ($authuser eq $userid) {
264 $rpcenv->check_user_enabled($userid);
265 # OK - each user can change its own password
267 # only root may change root password
268 raise_perm_exc
() if $userid eq 'root@pam';
272 PVE
::AccessControl
::domain_set_password
($realm, $ruid, $param->{password
});
274 PVE
::Cluster
::log_msg
('info', 'root@pam', "changed password for user '$userid'");