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