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