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.",
55 additionalProperties
=> 0,
63 subdir
=> { type
=> 'string' },
66 links
=> [ { rel
=> 'child', href
=> "{subdir}" } ],
73 my $ma = __PACKAGE__-
>method_attributes();
75 foreach my $info (@$ma) {
76 next if !$info->{subclass
};
78 my $subpath = $info->{match_re
}->[0];
80 push @$res, { subdir
=> $subpath };
83 push @$res, { subdir
=> 'ticket' };
84 push @$res, { subdir
=> 'password' };
90 my $verify_auth = sub {
91 my ($rpcenv, $username, $pw_or_ticket, $otp, $path, $privs) = @_;
93 my $normpath = PVE
::AccessControl
::normalize_path
($path);
96 if (($ticketuser = PVE
::AccessControl
::verify_ticket
($pw_or_ticket, 1)) &&
97 ($ticketuser eq $username)) {
99 } elsif (PVE
::AccessControl
::verify_vnc_ticket
($pw_or_ticket, $username, $normpath, 1)) {
102 $username = PVE
::AccessControl
::authenticate_user
($username, $pw_or_ticket, $otp);
105 my $privlist = [ PVE
::Tools
::split_list
($privs) ];
106 if (!($normpath && scalar(@$privlist) && $rpcenv->check($username, $normpath, $privlist))) {
107 die "no permission ($path, $privs)\n";
110 return { username
=> $username };
113 my $create_ticket = sub {
114 my ($rpcenv, $username, $pw_or_ticket, $otp) = @_;
117 if (($ticketuser = PVE
::AccessControl
::verify_ticket
($pw_or_ticket, 1)) &&
118 ($ticketuser eq 'root@pam' || $ticketuser eq $username)) {
119 # valid ticket. Note: root@pam can create tickets for other users
121 $username = PVE
::AccessControl
::authenticate_user
($username, $pw_or_ticket, $otp);
124 my $ticket = PVE
::AccessControl
::assemble_ticket
($username);
125 my $csrftoken = PVE
::AccessControl
::assemble_csrf_prevention_token
($username);
129 username
=> $username,
130 CSRFPreventionToken
=> $csrftoken,
134 my $compute_api_permission = sub {
135 my ($rpcenv, $authuser) = @_;
137 my $usercfg = $rpcenv->{user_cfg
};
141 vms
=> qr/VM\.|Permissions\.Modify/,
142 access
=> qr/(User|Group)\.|Permissions\.Modify/,
143 storage
=> qr/Datastore\.|Permissions\.Modify/,
144 nodes
=> qr/Sys\.|Permissions\.Modify/,
145 dc
=> qr/Sys\.Audit/,
147 map { $res->{$_} = {} } keys %$priv_re_map;
149 my $required_paths = ['/', '/nodes', '/access/groups', '/vms', '/storage'];
151 my $checked_paths = {};
152 foreach my $path (@$required_paths, keys %{$usercfg->{acl
}}) {
153 next if $checked_paths->{$path};
154 $checked_paths->{$path} = 1;
156 my $path_perm = $rpcenv->permissions($authuser, $path);
158 my $toplevel = ($path =~ /^\/(\w
+)/) ?
$1 : 'dc';
159 if ($toplevel eq 'pool') {
160 foreach my $priv (keys %$path_perm) {
161 if ($priv =~ m/^VM\./) {
162 $res->{vms
}->{$priv} = 1;
163 } elsif ($priv =~ m/^Datastore\./) {
164 $res->{storage
}->{$priv} = 1;
165 } elsif ($priv eq 'Permissions.Modify') {
166 $res->{storage
}->{$priv} = 1;
167 $res->{vms
}->{$priv} = 1;
171 my $priv_regex = $priv_re_map->{$toplevel} // next;
172 foreach my $priv (keys %$path_perm) {
173 next if $priv !~ m/^($priv_regex)/;
174 $res->{$toplevel}->{$priv} = 1;
182 __PACKAGE__-
>register_method ({
183 name
=> 'get_ticket',
186 permissions
=> { user
=> 'world' },
187 description
=> "Dummy. Useful for formatters which want to provide a login page.",
189 additionalProperties
=> 0,
191 returns
=> { type
=> "null" },
192 code
=> sub { return undef; }});
194 __PACKAGE__-
>register_method ({
195 name
=> 'create_ticket',
199 description
=> "You need to pass valid credientials.",
202 protected
=> 1, # else we can't access shadow files
203 description
=> "Create or verify authentication ticket.",
205 additionalProperties
=> 0,
208 description
=> "User name",
211 completion
=> \
&PVE
::AccessControl
::complete_username
,
213 realm
=> get_standard_option
('realm', {
214 description
=> "You can optionally pass the realm using this parameter. Normally the realm is simply added to the username <username>\@<relam>.",
216 completion
=> \
&PVE
::AccessControl
::complete_realm
,
219 description
=> "The secret password. This can also be a valid ticket.",
223 description
=> "One-time password for Two-factor authentication.",
228 description
=> "Verify ticket, and check if user have access 'privs' on 'path'",
235 description
=> "Verify ticket, and check if user have access 'privs' on 'path'",
236 type
=> 'string' , format
=> 'pve-priv-list',
246 username
=> { type
=> 'string' },
247 ticket
=> { type
=> 'string', optional
=> 1},
248 CSRFPreventionToken
=> { type
=> 'string', optional
=> 1 },
254 my $username = $param->{username
};
255 $username .= "\@$param->{realm}" if $param->{realm
};
257 my $rpcenv = PVE
::RPCEnvironment
::get
();
261 # test if user exists and is enabled
262 $rpcenv->check_user_enabled($username);
264 if ($param->{path
} && $param->{privs
}) {
265 $res = &$verify_auth($rpcenv, $username, $param->{password
}, $param->{otp
},
266 $param->{path
}, $param->{privs
});
268 $res = &$create_ticket($rpcenv, $username, $param->{password
}, $param->{otp
});
272 my $clientip = $rpcenv->get_client_ip() || '';
273 syslog
('err', "authentication failure; rhost=$clientip user=$username msg=$err");
274 # do not return any info to prevent user enumeration attacks
275 die PVE
::Exception-
>new("authentication failure\n", code
=> 401);
278 $res->{cap
} = &$compute_api_permission($rpcenv, $username);
280 PVE
::Cluster
::log_msg
('info', 'root@pam', "successful auth for user '$username'");
285 __PACKAGE__-
>register_method ({
286 name
=> 'change_passsword',
290 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.",
292 ['userid-param', 'self'],
294 [ 'userid-param', 'Realm.AllocateUser'],
295 [ 'userid-group', ['User.Modify']]
299 protected
=> 1, # else we can't access shadow files
300 description
=> "Change user password.",
302 additionalProperties
=> 0,
304 userid
=> get_standard_option
('userid', {
305 completion
=> \
&PVE
::AccessControl
::complete_username
,
308 description
=> "The new password.",
315 returns
=> { type
=> "null" },
319 my $rpcenv = PVE
::RPCEnvironment
::get
();
320 my $authuser = $rpcenv->get_user();
322 my ($userid, $ruid, $realm) = PVE
::AccessControl
::verify_username
($param->{userid
});
324 $rpcenv->check_user_exist($userid);
326 if ($authuser eq 'root@pam') {
327 # OK - root can change anything
329 if ($authuser eq $userid) {
330 $rpcenv->check_user_enabled($userid);
331 # OK - each user can change its own password
333 # only root may change root password
334 raise_perm_exc
() if $userid eq 'root@pam';
335 # do not allow to change system user passwords
336 raise_perm_exc
() if $realm eq 'pam';
340 PVE
::AccessControl
::domain_set_password
($realm, $ruid, $param->{password
});
342 PVE
::Cluster
::log_msg
('info', 'root@pam', "changed password for user '$userid'");