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);
12 use PVE
::AccessControl
;
13 use PVE
::JSONSchema
qw(get_standard_option);
14 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 ({
51 description
=> "Directory index.",
56 additionalProperties
=> 0,
64 subdir
=> { type
=> 'string' },
67 links
=> [ { rel
=> 'child', href
=> "{subdir}" } ],
74 my $ma = __PACKAGE__-
>method_attributes();
76 foreach my $info (@$ma) {
77 next if !$info->{subclass
};
79 my $subpath = $info->{match_re
}->[0];
81 push @$res, { subdir
=> $subpath };
84 push @$res, { subdir
=> 'ticket' };
85 push @$res, { subdir
=> 'password' };
91 my $verify_auth = sub {
92 my ($rpcenv, $username, $pw_or_ticket, $otp, $path, $privs) = @_;
94 my $normpath = PVE
::AccessControl
::normalize_path
($path);
97 if (($ticketuser = PVE
::AccessControl
::verify_ticket
($pw_or_ticket, 1)) &&
98 ($ticketuser eq $username)) {
100 } elsif (PVE
::AccessControl
::verify_vnc_ticket
($pw_or_ticket, $username, $normpath, 1)) {
103 $username = PVE
::AccessControl
::authenticate_user
($username, $pw_or_ticket, $otp);
106 my $privlist = [ PVE
::Tools
::split_list
($privs) ];
107 if (!($normpath && scalar(@$privlist) && $rpcenv->check($username, $normpath, $privlist))) {
108 die "no permission ($path, $privs)\n";
111 return { username
=> $username };
114 my $create_ticket = sub {
115 my ($rpcenv, $username, $pw_or_ticket, $otp) = @_;
118 if (($ticketuser = PVE
::AccessControl
::verify_ticket
($pw_or_ticket, 1)) &&
119 ($ticketuser eq 'root@pam' || $ticketuser eq $username)) {
120 # valid ticket. Note: root@pam can create tickets for other users
122 $username = PVE
::AccessControl
::authenticate_user
($username, $pw_or_ticket, $otp);
125 my $ticket = PVE
::AccessControl
::assemble_ticket
($username);
126 my $csrftoken = PVE
::AccessControl
::assemble_csrf_prevention_token
($username);
130 username
=> $username,
131 CSRFPreventionToken
=> $csrftoken,
135 my $compute_api_permission = sub {
136 my ($rpcenv, $authuser) = @_;
138 my $usercfg = $rpcenv->{user_cfg
};
142 vms
=> qr/VM\.|Permissions\.Modify/,
143 access
=> qr/(User|Group)\.|Permissions\.Modify/,
144 storage
=> qr/Datastore\.|Permissions\.Modify/,
145 nodes
=> qr/Sys\.|Permissions\.Modify/,
146 dc
=> qr/Sys\.Audit/,
148 map { $res->{$_} = {} } keys %$priv_re_map;
150 my $required_paths = ['/', '/nodes', '/access/groups', '/vms', '/storage'];
152 my $checked_paths = {};
153 foreach my $path (@$required_paths, keys %{$usercfg->{acl
}}) {
154 next if $checked_paths->{$path};
155 $checked_paths->{$path} = 1;
157 my $path_perm = $rpcenv->permissions($authuser, $path);
159 my $toplevel = ($path =~ /^\/(\w
+)/) ?
$1 : 'dc';
160 if ($toplevel eq 'pool') {
161 foreach my $priv (keys %$path_perm) {
162 if ($priv =~ m/^VM\./) {
163 $res->{vms
}->{$priv} = 1;
164 } elsif ($priv =~ m/^Datastore\./) {
165 $res->{storage
}->{$priv} = 1;
166 } elsif ($priv eq 'Permissions.Modify') {
167 $res->{storage
}->{$priv} = 1;
168 $res->{vms
}->{$priv} = 1;
172 my $priv_regex = $priv_re_map->{$toplevel} // next;
173 foreach my $priv (keys %$path_perm) {
174 next if $priv !~ m/^($priv_regex)/;
175 $res->{$toplevel}->{$priv} = 1;
183 __PACKAGE__-
>register_method ({
184 name
=> 'get_ticket',
187 permissions
=> { user
=> 'world' },
188 description
=> "Dummy. Useful for formatters which want to provide a login page.",
190 additionalProperties
=> 0,
192 returns
=> { type
=> "null" },
193 code
=> sub { return undef; }});
195 __PACKAGE__-
>register_method ({
196 name
=> 'create_ticket',
200 description
=> "You need to pass valid credientials.",
203 protected
=> 1, # else we can't access shadow files
204 description
=> "Create or verify authentication ticket.",
206 additionalProperties
=> 0,
209 description
=> "User name",
212 completion
=> \
&PVE
::AccessControl
::complete_username
,
214 realm
=> get_standard_option
('realm', {
215 description
=> "You can optionally pass the realm using this parameter. Normally the realm is simply added to the username <username>\@<relam>.",
217 completion
=> \
&PVE
::AccessControl
::complete_realm
,
220 description
=> "The secret password. This can also be a valid ticket.",
224 description
=> "One-time password for Two-factor authentication.",
229 description
=> "Verify ticket, and check if user have access 'privs' on 'path'",
236 description
=> "Verify ticket, and check if user have access 'privs' on 'path'",
237 type
=> 'string' , format
=> 'pve-priv-list',
247 username
=> { type
=> 'string' },
248 ticket
=> { type
=> 'string', optional
=> 1},
249 CSRFPreventionToken
=> { type
=> 'string', optional
=> 1 },
250 clustername
=> { type
=> 'string', optional
=> 1 },
256 my $username = $param->{username
};
257 $username .= "\@$param->{realm}" if $param->{realm
};
259 my $rpcenv = PVE
::RPCEnvironment
::get
();
263 # test if user exists and is enabled
264 $rpcenv->check_user_enabled($username);
266 if ($param->{path
} && $param->{privs
}) {
267 $res = &$verify_auth($rpcenv, $username, $param->{password
}, $param->{otp
},
268 $param->{path
}, $param->{privs
});
270 $res = &$create_ticket($rpcenv, $username, $param->{password
}, $param->{otp
});
274 my $clientip = $rpcenv->get_client_ip() || '';
275 syslog
('err', "authentication failure; rhost=$clientip user=$username msg=$err");
276 # do not return any info to prevent user enumeration attacks
277 die PVE
::Exception-
>new("authentication failure\n", code
=> 401);
280 $res->{cap
} = &$compute_api_permission($rpcenv, $username);
282 if (PVE
::Corosync
::check_conf_exists
(1)) {
283 if ($rpcenv->check($username, '/', ['Sys.Audit'], 1)) {
285 my $conf = cfs_read_file
('corosync.conf');
286 my $totem = PVE
::Corosync
::totem_config
($conf);
287 if ($totem->{cluster_name
}) {
288 $res->{clustername
} = $totem->{cluster_name
};
295 PVE
::Cluster
::log_msg
('info', 'root@pam', "successful auth for user '$username'");
300 __PACKAGE__-
>register_method ({
301 name
=> 'change_password',
305 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.",
307 ['userid-param', 'self'],
309 [ 'userid-param', 'Realm.AllocateUser'],
310 [ 'userid-group', ['User.Modify']]
314 protected
=> 1, # else we can't access shadow files
315 description
=> "Change user password.",
317 additionalProperties
=> 0,
319 userid
=> get_standard_option
('userid-completed'),
321 description
=> "The new password.",
328 returns
=> { type
=> "null" },
332 my $rpcenv = PVE
::RPCEnvironment
::get
();
333 my $authuser = $rpcenv->get_user();
335 my ($userid, $ruid, $realm) = PVE
::AccessControl
::verify_username
($param->{userid
});
337 $rpcenv->check_user_exist($userid);
339 if ($authuser eq 'root@pam') {
340 # OK - root can change anything
342 if ($authuser eq $userid) {
343 $rpcenv->check_user_enabled($userid);
344 # OK - each user can change its own password
346 # only root may change root password
347 raise_perm_exc
() if $userid eq 'root@pam';
348 # do not allow to change system user passwords
349 raise_perm_exc
() if $realm eq 'pam';
353 PVE
::AccessControl
::domain_set_password
($realm, $ruid, $param->{password
});
355 PVE
::Cluster
::log_msg
('info', 'root@pam', "changed password for user '$userid'");