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