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.",
58 additionalProperties
=> 0,
66 subdir
=> { type
=> 'string' },
69 links
=> [ { rel
=> 'child', href
=> "{subdir}" } ],
76 my $ma = __PACKAGE__-
>method_attributes();
78 foreach my $info (@$ma) {
79 next if !$info->{subclass
};
81 my $subpath = $info->{match_re
}->[0];
83 push @$res, { subdir
=> $subpath };
86 push @$res, { subdir
=> 'ticket' };
87 push @$res, { subdir
=> 'password' };
93 my $verify_auth = sub {
94 my ($rpcenv, $username, $pw_or_ticket, $path, $privs) = @_;
96 my $normpath = PVE
::AccessControl
::normalize_path
($path);
99 if (($ticketuser = PVE
::AccessControl
::verify_ticket
($pw_or_ticket, 1)) &&
100 ($ticketuser eq $username)) {
102 } elsif (PVE
::AccessControl
::verify_vnc_ticket
($pw_or_ticket, $username, $normpath, 1)) {
105 $username = PVE
::AccessControl
::authenticate_user
($username, $pw_or_ticket);
108 my $privlist = [ PVE
::Tools
::split_list
($privs) ];
109 if (!($normpath && scalar(@$privlist) && $rpcenv->check($username, $normpath, $privlist))) {
110 die "no permission ($path, $privs)\n";
113 return { username
=> $username };
116 my $create_ticket = sub {
117 my ($rpcenv, $username, $pw_or_ticket) = @_;
120 if (($ticketuser = PVE
::AccessControl
::verify_ticket
($pw_or_ticket, 1)) &&
121 ($ticketuser eq 'root@pam' || $ticketuser eq $username)) {
122 # valid ticket. Note: root@pam can create tickets for other users
124 $username = PVE
::AccessControl
::authenticate_user
($username, $pw_or_ticket);
127 my $ticket = PVE
::AccessControl
::assemble_ticket
($username);
128 my $csrftoken = PVE
::AccessControl
::assemble_csrf_prevention_token
($username);
132 username
=> $username,
133 CSRFPreventionToken
=> $csrftoken,
137 __PACKAGE__-
>register_method ({
138 name
=> 'create_ticket',
142 description
=> "You need to pass valid credientials.",
145 protected
=> 1, # else we can't access shadow files
146 description
=> "Create or verify authentication ticket.",
148 additionalProperties
=> 0,
151 description
=> "User name",
155 realm
=> get_standard_option
('realm', {
156 description
=> "You can optionally pass the realm using this parameter. Normally the realm is simply added to the username <username>\@<relam>.",
159 description
=> "The secret password. This can also be a valid ticket.",
163 description
=> "Verify ticket, and check if user have access 'privs' on 'path'",
170 description
=> "Verify ticket, and check if user have access 'privs' on 'path'",
171 type
=> 'string' , format
=> 'pve-priv-list',
181 username
=> { type
=> 'string' },
182 ticket
=> { type
=> 'string', optional
=> 1},
183 CSRFPreventionToken
=> { type
=> 'string', optional
=> 1 },
189 my $username = $param->{username
};
190 $username .= "\@$param->{realm}" if $param->{realm
};
192 my $rpcenv = PVE
::RPCEnvironment
::get
();
197 # test if user exists and is enabled
198 $rpcenv->check_user_enabled($username);
200 if ($param->{path
} && $param->{privs
}) {
201 $res = &$verify_auth($rpcenv, $username, $param->{password
},
202 $param->{path
}, $param->{privs
});
204 $res = &$create_ticket($rpcenv, $username, $param->{password
});
208 my $clientip = $rpcenv->get_client_ip() || '';
209 syslog
('err', "authentication failure; rhost=$clientip user=$username msg=$err");
213 PVE
::Cluster
::log_msg
('info', 'root@pam', "successful auth for user '$username'");
218 __PACKAGE__-
>register_method ({
219 name
=> 'change_passsword',
223 description
=> "Each user is allowed to change his own password. A user can change the password of another user if he has modify permission on /access/groups/<group> on a group where user <userid> is member of.",
225 ['userid-param', 'self'],
226 ['userid-group', ['User.Modify']],
229 protected
=> 1, # else we can't access shadow files
230 description
=> "Change user password.",
232 additionalProperties
=> 0,
234 userid
=> get_standard_option
('userid'),
236 description
=> "The new password.",
243 returns
=> { type
=> "null" },
247 my $rpcenv = PVE
::RPCEnvironment
::get
();
248 my $authuser = $rpcenv->get_user();
250 my ($userid, $ruid, $realm) = PVE
::AccessControl
::verify_username
($param->{userid
});
252 $rpcenv->check_user_exist($userid);
254 if ($authuser eq 'root@pam') {
255 # OK - root can change anything
257 if ($authuser eq $userid) {
258 $rpcenv->check_user_enabled($userid);
259 # OK - each user can change its own password
261 # only root may change root password
262 raise_perm_exc
() if $userid eq 'root@pam';
266 PVE
::AccessControl
::domain_set_password
($realm, $ruid, $param->{password
});
268 PVE
::Cluster
::log_msg
('info', 'root@pam', "changed password for user '$userid'");