1 package PVE
::API2
::AccessControl
;
7 use PVE
::RPCEnvironment
;
8 use PVE
::Cluster
qw(cfs_read_file);
10 use PVE
::AccessControl
;
11 use PVE
::JSONSchema
qw(get_standard_option);
12 use PVE
::API2
::Domains
;
18 use base
qw(PVE::RESTHandler);
20 __PACKAGE__-
>register_method ({
21 subclass
=> "PVE::API2::User",
25 __PACKAGE__-
>register_method ({
26 subclass
=> "PVE::API2::Group",
30 __PACKAGE__-
>register_method ({
31 subclass
=> "PVE::API2::Role",
35 __PACKAGE__-
>register_method ({
36 subclass
=> "PVE::API2::ACL",
40 __PACKAGE__-
>register_method ({
41 subclass
=> "PVE::API2::Domains",
45 __PACKAGE__-
>register_method ({
49 description
=> "Directory index.",
51 additionalProperties
=> 0,
59 subdir
=> { type
=> 'string' },
62 links
=> [ { rel
=> 'child', href
=> "{subdir}" } ],
69 my $ma = __PACKAGE__-
>method_attributes();
71 foreach my $info (@$ma) {
72 next if !$info->{subclass
};
74 my $subpath = $info->{match_re
}->[0];
76 push @$res, { subdir
=> $subpath };
79 push @$res, { subdir
=> 'ticket' };
85 my $verify_auth = sub {
86 my ($rpcenv, $username, $pw_or_ticket, $path, $privs) = @_;
88 my $normpath = PVE
::AccessControl
::normalize_path
($path);
91 if (($ticketuser = PVE
::AccessControl
::verify_ticket
($pw_or_ticket, 1)) &&
92 ($ticketuser eq $username)) {
94 } elsif (PVE
::AccessControl
::verify_vnc_ticket
($pw_or_ticket, $username, $normpath, 1)) {
97 $username = PVE
::AccessControl
::authenticate_user
($username, $pw_or_ticket);
100 my $privlist = [ PVE
::Tools
::split_list
($privs) ];
101 if (!($normpath && scalar(@$privlist) && $rpcenv->check($username, $normpath, $privlist))) {
102 die "no permission ($path, $privs)\n";
105 return { username
=> $username };
108 my $create_ticket = sub {
109 my ($rpcenv, $username, $pw_or_ticket) = @_;
112 if (($ticketuser = PVE
::AccessControl
::verify_ticket
($pw_or_ticket, 1)) &&
113 ($ticketuser eq 'root@pam' || $ticketuser eq $username)) {
114 # valid ticket. Note: root@pam can create tickets for other users
116 $username = PVE
::AccessControl
::authenticate_user
($username, $pw_or_ticket);
119 my $ticket = PVE
::AccessControl
::assemble_ticket
($username);
120 my $csrftoken = PVE
::AccessControl
::assemble_csrf_prevention_token
($username);
124 username
=> $username,
125 CSRFPreventionToken
=> $csrftoken,
129 __PACKAGE__-
>register_method ({
130 name
=> 'create_ticket',
133 permissions
=> { user
=> 'world' },
134 protected
=> 1, # else we can't access shadow files
135 description
=> "Create or verify authentication ticket.",
137 additionalProperties
=> 0,
140 description
=> "User name",
144 realm
=> get_standard_option
('realm', {
145 description
=> "You can optionally pass the realm using this parameter. Normally the realm is simply added to the username <username>\@<relam>.",
148 description
=> "The secret password. This can also be a valid ticket.",
152 description
=> "Verify ticket, and check if user have access 'privs' on 'path'",
159 description
=> "Verify ticket, and check if user have access 'privs' on 'path'",
160 type
=> 'string' , format
=> 'pve-priv-list',
170 username
=> { type
=> 'string' },
171 ticket
=> { type
=> 'string', optional
=> 1},
172 CSRFPreventionToken
=> { type
=> 'string', optional
=> 1 },
178 my $username = $param->{username
};
179 $username .= "\@$param->{realm}" if $param->{realm
};
181 my $rpcenv = PVE
::RPCEnvironment
::get
();
186 # test if user exists and is enabled
187 $rpcenv->check_user_enabled($username);
189 if ($param->{path
} && $param->{privs
}) {
190 $res = &$verify_auth($rpcenv, $username, $param->{password
},
191 $param->{path
}, $param->{privs
});
193 $res = &$create_ticket($rpcenv, $username, $param->{password
});
197 my $clientip = $rpcenv->get_client_ip() || '';
198 syslog
('err', "authentication failure; rhost=$clientip user=$username msg=$err");
202 PVE
::Cluster
::log_msg
('info', 'root@pam', "successful auth for user '$username'");