]> git.proxmox.com Git - pve-manager.git/blame - PVE/HTTPServer.pm
Revert "ui: always show otp field"
[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;
46095dd4 10use PVE::Exception qw(raise_param_exc raise);
c2e9823c 11
b996e6c0
DM
12use PVE::RPCEnvironment;
13use PVE::AccessControl;
14use PVE::Cluster;
3c54bc91 15use PVE::API2Tools;
c2e9823c 16
b996e6c0 17use Data::Dumper;
57f93db1 18
b996e6c0 19use base('PVE::APIServer::AnyEvent');
353fef24 20
b996e6c0 21use HTTP::Status qw(:constants);
353fef24 22
57f93db1
DM
23sub new {
24 my ($this, %args) = @_;
25
26 my $class = ref($this) || $this;
27
b996e6c0
DM
28 my $self = $class->SUPER::new(%args);
29
f91072d5 30 $self->{rpcenv} = PVE::RPCEnvironment->init(
353fef24 31 $self->{trusted_env} ? 'priv' : 'pub', atfork => sub { $self-> atfork_handler() });
f91072d5 32
b996e6c0
DM
33 return $self;
34}
02667982 35
b996e6c0
DM
36sub verify_spice_connect_url {
37 my ($self, $connect_str) = @_;
57f93db1 38
9d3f059f
DM
39 my $rpcenv = $self->{rpcenv};
40
41 $rpcenv->init_request();
42
b996e6c0 43 my ($vmid, $node, $port) = PVE::AccessControl::verify_spice_connect_url($connect_str);
57f93db1 44
b996e6c0
DM
45 return ($vmid, $node, $port);
46}
57f93db1 47
b996e6c0
DM
48sub generate_csrf_prevention_token {
49 my ($username) = @_;
f91072d5 50
b996e6c0 51 return PVE::AccessControl::assemble_csrf_prevention_token($username);
57f93db1
DM
52}
53
430b554f 54sub auth_handler {
9d3f059f 55 my ($self, $method, $rel_uri, $ticket, $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) {
80
81 die "No ticket\n" if !$ticket;
82
7541d6a7
WB
83 ($username, $age, my $challenge) = PVE::AccessControl::verify_ticket($ticket);
84
85 if (defined($challenge)) {
86 $rpcenv->set_u2f_challenge($challenge);
87 die "No ticket\n"
24d2ed8c 88 if ($rel_uri ne '/access/tfa' || $method ne 'POST');
7541d6a7 89 }
430b554f
DM
90
91 $rpcenv->set_user($username);
92
93 if ($method eq 'POST' && $rel_uri =~ m|^/nodes/([^/]+)/storage/([^/]+)/upload$|) {
94 my ($node, $storeid) = ($1, $2);
95 # we disable CSRF checks if $isUpload is set,
96 # to improve security we check user upload permission here
97 my $perm = { check => ['perm', "/storage/$storeid", ['Datastore.AllocateTemplate']] };
98 $rpcenv->check_api2_permissions($perm, $username, {});
99 $isUpload = 1;
100 }
101
102 # we skip CSRF check for file upload, because it is
103 # difficult to pass CSRF HTTP headers with native html forms,
104 # and it should not be necessary at all.
105 my $euid = $>;
106 PVE::AccessControl::verify_csrf_prevention_token($username, $token)
107 if !$isUpload && ($euid != 0) && ($method ne 'GET');
108 }
109
110 return {
111 ticket => $ticket,
112 token => $token,
113 userid => $username,
114 age => $age,
115 isUpload => $isUpload,
116 };
117}
118
2261f249
DM
119sub rest_handler {
120 my ($self, $clientip, $method, $rel_uri, $auth, $params) = @_;
121
122 my $rpcenv = $self->{rpcenv};
123
70473e51
DM
124 my $resp = {
125 status => HTTP_NOT_IMPLEMENTED,
126 message => "Method '$method $rel_uri' not implemented",
127 };
128
129 my ($handler, $info);
130
131 eval {
132 my $uri_param = {};
133 ($handler, $info) = PVE::API2->find_handler($method, $rel_uri, $uri_param);
134 return if !$handler || !$info;
2261f249 135
70473e51
DM
136 foreach my $p (keys %{$params}) {
137 if (defined($uri_param->{$p})) {
138 raise_param_exc({$p => "duplicate parameter (already defined in URI)"});
139 }
140 $uri_param->{$p} = $params->{$p};
2261f249 141 }
2261f249 142
70473e51
DM
143 # check access permissions
144 $rpcenv->check_api2_permissions($info->{permissions}, $auth->{userid}, $uri_param);
2261f249 145
3c54bc91
DM
146 if ($info->{proxyto} || $info->{proxyto_callback}) {
147 my $node = PVE::API2Tools::resolve_proxyto(
148 $rpcenv, $info->{proxyto_callback}, $info->{proxyto}, $uri_param);
2261f249
DM
149
150 if ($node ne 'localhost' && $node ne PVE::INotify::nodename()) {
151 die "unable to proxy file uploads" if $auth->{isUpload};
70473e51
DM
152 my $remip = $self->remote_node_ip($node);
153 $resp = { proxy => $remip, proxynode => $node, proxy_params => $params };
154 return;
2261f249 155 }
2261f249 156 }
2261f249 157
70473e51
DM
158 my $euid = $>;
159 if ($info->{protected} && ($euid != 0)) {
160 $resp = { proxy => 'localhost' , proxy_params => $params };
161 return;
162 }
2261f249 163
70473e51
DM
164 $resp = {
165 data => $handler->handle($info, $uri_param),
166 info => $info, # useful to format output
167 status => HTTP_OK,
168 };
2261f249
DM
169
170 if (my $count = $rpcenv->get_result_attrib('total')) {
171 $resp->{total} = $count;
172 }
70473e51 173
2261f249
DM
174 if (my $diff = $rpcenv->get_result_attrib('changes')) {
175 $resp->{changes} = $diff;
176 }
177 };
70473e51 178 my $err = $@;
2261f249 179
9d3f059f 180 $rpcenv->set_user(undef); # clear after request
70473e51
DM
181
182 if ($err) {
183 $resp = { info => $info };
184 if (ref($err) eq "PVE::Exception") {
185 $resp->{status} = $err->{code} || HTTP_INTERNAL_SERVER_ERROR;
186 $resp->{errors} = $err->{errors} if $err->{errors};
187 $resp->{message} = $err->{msg};
188 } else {
189 $resp->{status} = HTTP_INTERNAL_SERVER_ERROR;
190 $resp->{message} = $err;
191 }
192 }
193
2261f249
DM
194 return $resp;
195}
430b554f 196
7e5f7c61
DM
197sub check_cert_fingerprint {
198 my ($self, $cert) = @_;
199
200 return PVE::Cluster::check_cert_fingerprint($cert);
201}
202
203sub initialize_cert_cache {
204 my ($self, $node) = @_;
205
206 PVE::Cluster::initialize_cert_cache($node);
207}
208
209sub remote_node_ip {
210 my ($self, $node) = @_;
211
212 my $remip = PVE::Cluster::remote_node_ip($node);
213
214 die "unable to get remote IP address for node '$node'\n" if !$remip;
215
216 return $remip;
217}
218
57f93db1 2191;