]> git.proxmox.com Git - pve-manager.git/blame - PVE/HTTPServer.pm
http-server: fix for api token csrf token check
[pve-manager.git] / PVE / HTTPServer.pm
CommitLineData
57f93db1
DM
1package PVE::HTTPServer;
2
3use strict;
4use warnings;
d06a1c62 5
b996e6c0
DM
6use PVE::SafeSyslog;
7use PVE::INotify;
8use PVE::Tools;
9use PVE::APIServer::AnyEvent;
7af404b2 10use PVE::Exception qw(raise_param_exc raise_perm_exc raise);
c2e9823c 11
b996e6c0
DM
12use PVE::RPCEnvironment;
13use PVE::AccessControl;
0f9ac2df 14use PVE::CertCache;
b996e6c0 15use PVE::Cluster;
3c54bc91 16use PVE::API2Tools;
c2e9823c 17
b996e6c0 18use Data::Dumper;
57f93db1 19
b996e6c0 20use base('PVE::APIServer::AnyEvent');
353fef24 21
b996e6c0 22use HTTP::Status qw(:constants);
353fef24 23
57f93db1
DM
24sub new {
25 my ($this, %args) = @_;
26
27 my $class = ref($this) || $this;
b996e6c0 28 my $self = $class->SUPER::new(%args);
e45f98ef 29
f91072d5 30 $self->{rpcenv} = PVE::RPCEnvironment->init(
e45f98ef
TL
31 $self->{trusted_env} ? 'priv' : 'pub',
32 atfork => sub { $self->atfork_handler() },
33 );
f91072d5 34
b996e6c0
DM
35 return $self;
36}
02667982 37
b996e6c0
DM
38sub verify_spice_connect_url {
39 my ($self, $connect_str) = @_;
57f93db1 40
9d3f059f 41 my $rpcenv = $self->{rpcenv};
9d3f059f
DM
42 $rpcenv->init_request();
43
b996e6c0 44 my ($vmid, $node, $port) = PVE::AccessControl::verify_spice_connect_url($connect_str);
57f93db1 45
b996e6c0
DM
46 return ($vmid, $node, $port);
47}
57f93db1 48
b996e6c0
DM
49sub generate_csrf_prevention_token {
50 my ($username) = @_;
b996e6c0 51 return PVE::AccessControl::assemble_csrf_prevention_token($username);
57f93db1
DM
52}
53
430b554f 54sub auth_handler {
84811327 55 my ($self, $method, $rel_uri, $ticket, $token, $api_token, $peer_host) = @_;
430b554f
DM
56
57 my $rpcenv = $self->{rpcenv};
58
9d3f059f
DM
59 # set environment variables
60 $rpcenv->set_user(undef);
61 $rpcenv->set_language('C');
62 $rpcenv->set_client_ip($peer_host);
63
46095dd4
TL
64 eval { $rpcenv->init_request() };
65 raise("RPCEnvironment init request failed: $@\n") if $@;
a2e669fe 66
430b554f
DM
67 my $require_auth = 1;
68
69 # explicitly allow some calls without auth
70 if (($rel_uri eq '/access/domains' && $method eq 'GET') ||
71 ($rel_uri eq '/access/ticket' && ($method eq 'GET' || $method eq 'POST'))) {
72 $require_auth = 0;
73 }
74
75 my ($username, $age);
76
77 my $isUpload = 0;
78
79 if ($require_auth) {
84811327 80 if ($api_token) {
e45f98ef 81 # the token-ID `<user>@<realm>!<tokenname>` is the user for token based authentication
84811327 82 $username = PVE::AccessControl::verify_token($api_token);
84811327
FG
83 } else {
84 die "No ticket\n" if !$ticket;
85
86 ($username, $age, my $tfa_info) = PVE::AccessControl::verify_ticket($ticket);
86f64e0d 87 $rpcenv->check_user_enabled($username);
84811327
FG
88
89 if (defined($tfa_info)) {
90 if (defined(my $challenge = $tfa_info->{challenge})) {
91 $rpcenv->set_u2f_challenge($challenge);
92 }
e45f98ef 93 die "No ticket\n" if ($rel_uri ne '/access/tfa' || $method ne 'POST');
84811327 94 }
d9fc56ef 95 }
430b554f 96
d9fc56ef 97 $rpcenv->set_user($username);
430b554f 98
d9fc56ef
LS
99 if ($method eq 'POST' && $rel_uri =~ m|^/nodes/([^/]+)/storage/([^/]+)/upload$|) {
100 my ($node, $storeid) = ($1, $2);
e45f98ef 101 # CSRF check are omitted if $isUpload is set, so check user upload permission here
d9fc56ef
LS
102 my $perm = { check => ['perm', "/storage/$storeid", ['Datastore.AllocateTemplate']] };
103 $rpcenv->check_api2_permissions($perm, $username, {});
104 $isUpload = 1;
430b554f 105 }
d9fc56ef 106
7d11059e
LS
107 if (!$api_token) {
108 # we skip CSRF check for file upload, because it is difficult to pass CSRF HTTP headers
109 # with native html forms, and it should not be necessary at all.
110 my $euid = $>;
111 PVE::AccessControl::verify_csrf_prevention_token($username, $token)
112 if !$isUpload && ($euid != 0) && ($method ne 'GET');
113 }
430b554f
DM
114 }
115
116 return {
117 ticket => $ticket,
118 token => $token,
119 userid => $username,
120 age => $age,
121 isUpload => $isUpload,
84811327 122 api_token => $api_token,
430b554f
DM
123 };
124}
125
2261f249
DM
126sub rest_handler {
127 my ($self, $clientip, $method, $rel_uri, $auth, $params) = @_;
128
129 my $rpcenv = $self->{rpcenv};
130
70473e51
DM
131 my $resp = {
132 status => HTTP_NOT_IMPLEMENTED,
133 message => "Method '$method $rel_uri' not implemented",
134 };
135
136 my ($handler, $info);
137
138 eval {
139 my $uri_param = {};
140 ($handler, $info) = PVE::API2->find_handler($method, $rel_uri, $uri_param);
141 return if !$handler || !$info;
2261f249 142
e45f98ef 143 for my $p (sort keys %{$params}) {
a2beaf13
TL
144 if (defined($uri_param->{$p}) && $uri_param->{$p} ne $params->{$p}) {
145 raise_param_exc({
146 $p => "duplicate parameter (already defined in URI) with conflicting values!"
147 });
70473e51
DM
148 }
149 $uri_param->{$p} = $params->{$p};
2261f249 150 }
2261f249 151
7af404b2
FG
152 raise_perm_exc("URI '$rel_uri' not available with API token, need proper ticket.\n")
153 if $auth->{api_token} && !$info->{allowtoken};
154
70473e51
DM
155 # check access permissions
156 $rpcenv->check_api2_permissions($info->{permissions}, $auth->{userid}, $uri_param);
2261f249 157
3c54bc91
DM
158 if ($info->{proxyto} || $info->{proxyto_callback}) {
159 my $node = PVE::API2Tools::resolve_proxyto(
160 $rpcenv, $info->{proxyto_callback}, $info->{proxyto}, $uri_param);
2261f249
DM
161
162 if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) {
163 die "unable to proxy file uploads" if $auth->{isUpload};
70473e51
DM
164 my $remip = $self->remote_node_ip($node);
165 $resp = { proxy => $remip, proxynode => $node, proxy_params => $params };
166 return;
2261f249 167 }
2261f249 168 }
2261f249 169
70473e51
DM
170 my $euid = $>;
171 if ($info->{protected} && ($euid != 0)) {
172 $resp = { proxy => 'localhost' , proxy_params => $params };
173 return;
174 }
2261f249 175
70473e51
DM
176 $resp = {
177 data => $handler->handle($info, $uri_param),
178 info => $info, # useful to format output
179 status => HTTP_OK,
180 };
2261f249
DM
181
182 if (my $count = $rpcenv->get_result_attrib('total')) {
183 $resp->{total} = $count;
184 }
70473e51 185
2261f249
DM
186 if (my $diff = $rpcenv->get_result_attrib('changes')) {
187 $resp->{changes} = $diff;
188 }
189 };
70473e51 190 my $err = $@;
2261f249 191
9d3f059f 192 $rpcenv->set_user(undef); # clear after request
70473e51
DM
193
194 if ($err) {
195 $resp = { info => $info };
196 if (ref($err) eq "PVE::Exception") {
197 $resp->{status} = $err->{code} || HTTP_INTERNAL_SERVER_ERROR;
198 $resp->{errors} = $err->{errors} if $err->{errors};
199 $resp->{message} = $err->{msg};
200 } else {
201 $resp->{status} = HTTP_INTERNAL_SERVER_ERROR;
202 $resp->{message} = $err;
203 }
204 }
205
2261f249
DM
206 return $resp;
207}
430b554f 208
7e5f7c61
DM
209sub check_cert_fingerprint {
210 my ($self, $cert) = @_;
0f9ac2df 211 return PVE::CertCache::check_cert_fingerprint($cert);
7e5f7c61
DM
212}
213
214sub initialize_cert_cache {
215 my ($self, $node) = @_;
0f9ac2df 216 PVE::CertCache::initialize_cert_cache($node);
7e5f7c61
DM
217}
218
219sub remote_node_ip {
220 my ($self, $node) = @_;
221
222 my $remip = PVE::Cluster::remote_node_ip($node);
7e5f7c61
DM
223 die "unable to get remote IP address for node '$node'\n" if !$remip;
224
225 return $remip;
226}
227
57f93db1 2281;