]> git.proxmox.com Git - pve-manager.git/blob - PVE/HTTPServer.pm
63b8583efbf86ff8e2159dd476112178a7e06d2f
[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
29 my $self = $class->SUPER::new(%args);
30
31 $self->{rpcenv} = PVE::RPCEnvironment->init(
32 $self->{trusted_env} ? 'priv' : 'pub', atfork => sub { $self-> atfork_handler() });
33
34 return $self;
35 }
36
37 sub verify_spice_connect_url {
38 my ($self, $connect_str) = @_;
39
40 my $rpcenv = $self->{rpcenv};
41
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
52 return PVE::AccessControl::assemble_csrf_prevention_token($username);
53 }
54
55 sub auth_handler {
56 my ($self, $method, $rel_uri, $ticket, $token, $api_token, $peer_host) = @_;
57
58 my $rpcenv = $self->{rpcenv};
59
60 # set environment variables
61 $rpcenv->set_user(undef);
62 $rpcenv->set_language('C');
63 $rpcenv->set_client_ip($peer_host);
64
65 eval { $rpcenv->init_request() };
66 raise("RPCEnvironment init request failed: $@\n") if $@;
67
68 my $require_auth = 1;
69
70 # explicitly allow some calls without auth
71 if (($rel_uri eq '/access/domains' && $method eq 'GET') ||
72 ($rel_uri eq '/access/ticket' && ($method eq 'GET' || $method eq 'POST'))) {
73 $require_auth = 0;
74 }
75
76 my ($username, $age);
77
78 my $isUpload = 0;
79
80 if ($require_auth) {
81 if ($api_token) {
82 # returns tokenid actually
83 $username = PVE::AccessControl::verify_token($api_token);
84 } else {
85 die "No ticket\n" if !$ticket;
86
87 ($username, $age, my $tfa_info) = PVE::AccessControl::verify_ticket($ticket);
88 $rpcenv->check_user_enabled($username);
89
90 if (defined($tfa_info)) {
91 if (defined(my $challenge = $tfa_info->{challenge})) {
92 $rpcenv->set_u2f_challenge($challenge);
93 }
94 die "No ticket\n"
95 if ($rel_uri ne '/access/tfa' || $method ne 'POST');
96 }
97 }
98
99 $rpcenv->set_user($username);
100
101 if ($method eq 'POST' && $rel_uri =~ m|^/nodes/([^/]+)/storage/([^/]+)/upload$|) {
102 my ($node, $storeid) = ($1, $2);
103 # we disable CSRF checks if $isUpload is set,
104 # to improve security we check user upload permission here
105 my $perm = { check => ['perm', "/storage/$storeid", ['Datastore.AllocateTemplate']] };
106 $rpcenv->check_api2_permissions($perm, $username, {});
107 $isUpload = 1;
108 }
109
110 # we skip CSRF check for file upload, because it is
111 # difficult to pass CSRF HTTP headers with native html forms,
112 # and it should not be necessary at all.
113 my $euid = $>;
114 PVE::AccessControl::verify_csrf_prevention_token($username, $token)
115 if !$isUpload && ($euid != 0) && ($method ne 'GET');
116 }
117
118 return {
119 ticket => $ticket,
120 token => $token,
121 userid => $username,
122 age => $age,
123 isUpload => $isUpload,
124 api_token => $api_token,
125 };
126 }
127
128 sub rest_handler {
129 my ($self, $clientip, $method, $rel_uri, $auth, $params) = @_;
130
131 my $rpcenv = $self->{rpcenv};
132
133 my $resp = {
134 status => HTTP_NOT_IMPLEMENTED,
135 message => "Method '$method $rel_uri' not implemented",
136 };
137
138 my ($handler, $info);
139
140 eval {
141 my $uri_param = {};
142 ($handler, $info) = PVE::API2->find_handler($method, $rel_uri, $uri_param);
143 return if !$handler || !$info;
144
145 foreach my $p (sort keys %{$params}) {
146 if (defined($uri_param->{$p}) && $uri_param->{$p} ne $params->{$p}) {
147 raise_param_exc({
148 $p => "duplicate parameter (already defined in URI) with conflicting values!"
149 });
150 }
151 $uri_param->{$p} = $params->{$p};
152 }
153
154 raise_perm_exc("URI '$rel_uri' not available with API token, need proper ticket.\n")
155 if $auth->{api_token} && !$info->{allowtoken};
156
157 # check access permissions
158 $rpcenv->check_api2_permissions($info->{permissions}, $auth->{userid}, $uri_param);
159
160 if ($info->{proxyto} || $info->{proxyto_callback}) {
161 my $node = PVE::API2Tools::resolve_proxyto(
162 $rpcenv, $info->{proxyto_callback}, $info->{proxyto}, $uri_param);
163
164 if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) {
165 die "unable to proxy file uploads" if $auth->{isUpload};
166 my $remip = $self->remote_node_ip($node);
167 $resp = { proxy => $remip, proxynode => $node, proxy_params => $params };
168 return;
169 }
170 }
171
172 my $euid = $>;
173 if ($info->{protected} && ($euid != 0)) {
174 $resp = { proxy => 'localhost' , proxy_params => $params };
175 return;
176 }
177
178 $resp = {
179 data => $handler->handle($info, $uri_param),
180 info => $info, # useful to format output
181 status => HTTP_OK,
182 };
183
184 if (my $count = $rpcenv->get_result_attrib('total')) {
185 $resp->{total} = $count;
186 }
187
188 if (my $diff = $rpcenv->get_result_attrib('changes')) {
189 $resp->{changes} = $diff;
190 }
191 };
192 my $err = $@;
193
194 $rpcenv->set_user(undef); # clear after request
195
196 if ($err) {
197 $resp = { info => $info };
198 if (ref($err) eq "PVE::Exception") {
199 $resp->{status} = $err->{code} || HTTP_INTERNAL_SERVER_ERROR;
200 $resp->{errors} = $err->{errors} if $err->{errors};
201 $resp->{message} = $err->{msg};
202 } else {
203 $resp->{status} = HTTP_INTERNAL_SERVER_ERROR;
204 $resp->{message} = $err;
205 }
206 }
207
208 return $resp;
209 }
210
211 sub check_cert_fingerprint {
212 my ($self, $cert) = @_;
213
214 return PVE::CertCache::check_cert_fingerprint($cert);
215 }
216
217 sub initialize_cert_cache {
218 my ($self, $node) = @_;
219
220 PVE::CertCache::initialize_cert_cache($node);
221 }
222
223 sub remote_node_ip {
224 my ($self, $node) = @_;
225
226 my $remip = PVE::Cluster::remote_node_ip($node);
227
228 die "unable to get remote IP address for node '$node'\n" if !$remip;
229
230 return $remip;
231 }
232
233 1;