]> git.proxmox.com Git - pve-apiclient.git/blob - PVE/APIClient/LWP.pm
492f64d84f5017886b5625f7a7e08abd7de47206
[pve-apiclient.git] / PVE / APIClient / LWP.pm
1 package PVE::APIClient::LWP;
2
3 use strict;
4 use warnings;
5 use URI;
6 use IO::Socket::SSL; # important for SSL_verify_callback
7 use LWP::UserAgent;
8 use URI::Escape;
9 use Net::SSLeay;
10 use JSON;
11 use Data::Dumper; # fixme: remove
12 use HTTP::Request::Common;
13 use Carp;
14 use PVE::APIClient::Exception qw(raise);
15
16 my $extract_data = sub {
17 my ($res) = @_;
18
19 croak "undefined result" if !defined($res);
20 croak "undefined result data" if !exists($res->{data});
21
22 return $res->{data};
23 };
24
25 sub get_raw {
26 my ($self, $path, $param) = @_;
27
28 return $self->call('GET', $path, $param);
29 }
30
31 sub get {
32 my ($self, $path, $param) = @_;
33
34 return $extract_data->($self->call('GET', $path, $param));
35 }
36
37 sub post_raw {
38 my ($self, $path, $param) = @_;
39
40 return $self->call('POST', $path, $param);
41 }
42
43 sub post {
44 my ($self, $path, $param) = @_;
45
46 return $extract_data->($self->call('POST', $path, $param));
47 }
48
49 sub put_raw {
50 my ($self, $path, $param) = @_;
51
52 return $self->call('PUT', $path, $param);
53 }
54
55 sub put {
56 my ($self, $path, $param) = @_;
57
58 return $extract_data->($self->call('PUT', $path, $param));
59 }
60
61 sub delete_raw {
62 my ($self, $path, $param) = @_;
63
64 return $self->call('DELETE', $path, $param);
65 }
66
67 sub delete {
68 my ($self, $path, $param) = @_;
69
70 return $extract_data->($self->call('DELETE', $path, $param));
71 }
72
73 sub update_csrftoken {
74 my ($self, $csrftoken) = @_;
75
76 $self->{csrftoken} = $csrftoken;
77
78 my $agent = $self->{useragent};
79
80 $agent->default_header('CSRFPreventionToken', $self->{csrftoken});
81 }
82
83 sub update_ticket {
84 my ($self, $ticket) = @_;
85
86 my $agent = $self->{useragent};
87
88 $self->{ticket} = $ticket;
89
90 my $encticket = uri_escape($ticket);
91 my $cookie = "$self->{cookie_name}=$encticket; path=/; secure;";
92 $agent->default_header('Cookie', $cookie);
93 }
94
95 sub login {
96 my ($self) = @_;
97
98 my $uri = URI->new();
99 $uri->scheme($self->{protocol});
100 $uri->host($self->{host});
101 $uri->port($self->{port});
102 $uri->path('/api2/json/access/ticket');
103
104 my $ua = $self->{useragent};
105
106 delete $self->{last_unknown_fingerprint};
107
108 my $exec_login = sub {
109 return $ua->post($uri, {
110 username => $self->{username} || 'unknown',
111 password => $self->{password} || ''});
112 };
113
114 my $response = $exec_login->();
115
116 if (!$response->is_success) {
117 if (my $fp = delete($self->{last_unknown_fingerprint})) {
118 if ($self->manual_verify_fingerprint($fp)) {
119 $response = $exec_login->(); # try again
120 }
121 }
122 }
123
124 if (!$response->is_success) {
125 raise($response->status_line ."\n", code => $response->code)
126 }
127
128 my $res = from_json($response->decoded_content, {utf8 => 1, allow_nonref => 1});
129
130 my $data = $extract_data->($res);
131
132 # TODO: make it possible to use tfa
133 if ($data->{ticket} =~ m/^PVE:tfa!/) {
134 die "TFA in API is not yet implemented! Try disabling TFA for the user.\n";
135 }
136
137 $self->update_ticket($data->{ticket});
138 $self->update_csrftoken($data->{CSRFPreventionToken});
139
140 return $data;
141 }
142
143 sub manual_verify_fingerprint {
144 my ($self, $fingerprint) = @_;
145
146 if (!$self->{manual_verification}) {
147 raise("fingerprint '$fingerprint' not verified, abort!\n");
148 }
149
150 print "The authenticity of host '$self->{host}' can't be established.\n" .
151 "X509 SHA256 key fingerprint is $fingerprint.\n" .
152 "Are you sure you want to continue connecting (yes/no)? ";
153
154 my $answer = <STDIN>;
155
156 my $valid = ($answer =~ m/^\s*yes\s*$/i) ? 1 : 0;
157
158 $self->{cached_fingerprints}->{$fingerprint} = $valid;
159
160 raise("Fingerprint not verified, abort!\n") if !$valid;
161
162 if (my $cb = $self->{register_fingerprint_cb}) {
163 $cb->($fingerprint) if $valid;
164 }
165
166 return $valid;
167 }
168
169 sub call {
170 my ($self, $method, $path, $param) = @_;
171
172 delete $self->{last_unknown_fingerprint};
173
174 my $ticket = $self->{ticket};
175
176 my $ua = $self->{useragent};
177
178 # fixme: check ticket lifetime?
179
180 if (!$ticket && $self->{username} && $self->{password}) {
181 $self->login();
182 }
183
184 my $uri = URI->new();
185 $uri->scheme($self->{protocol});
186 $uri->host($self->{host});
187 $uri->port($self->{port});
188
189 $path =~ s!^/+!!;
190
191 if ($path !~ m!^api2/!) {
192 $uri->path("api2/json/$path");
193 } else {
194 $uri->path($path);
195 }
196
197 #print "CALL $method : " . $uri->as_string() . "\n";
198
199 my $exec_method = sub {
200
201 my $response;
202 if ($method eq 'GET') {
203 $uri->query_form($param);
204 $response = $ua->request(HTTP::Request::Common::GET($uri));
205 } elsif ($method eq 'POST') {
206 $response = $ua->request(HTTP::Request::Common::POST($uri, Content => $param));
207 } elsif ($method eq 'PUT') {
208 # We use another temporary URI object to format
209 # the application/x-www-form-urlencoded content.
210
211 my $tmpurl = URI->new('http:');
212 $tmpurl->query_form(%$param);
213 my $content = $tmpurl->query;
214
215 $response = $ua->request(HTTP::Request::Common::PUT($uri, 'Content-Type' => 'application/x-www-form-urlencoded', Content => $content));
216
217 } elsif ($method eq 'DELETE') {
218 $response = $ua->request(HTTP::Request::Common::DELETE($uri));
219 } else {
220 raise("method $method not implemented\n");
221 }
222 return $response;
223 };
224
225 my $response = $exec_method->();
226
227 if (my $fp = delete($self->{last_unknown_fingerprint})) {
228 if ($self->manual_verify_fingerprint($fp)) {
229 $response = $exec_method->(); # try again
230 }
231 }
232
233 #print "RESP: " . Dumper($response) . "\n";
234
235 my $ct = $response->header('Content-Type') || '';
236
237 if ($response->is_success) {
238
239 raise("got unexpected content type", code => $response->code)
240 if $ct !~ m|application/json|;
241
242 return from_json($response->decoded_content, {utf8 => 1, allow_nonref => 1});
243
244 } else {
245
246 my $msg = $response->message;
247 my $errors = eval {
248 return if $ct !~ m|application/json|;
249 my $res = from_json($response->decoded_content, {utf8 => 1, allow_nonref => 1});
250 return $res->{errors};
251 };
252
253 raise("$msg\n", code => $response->code, errors => $errors);
254 }
255 }
256
257 my $verify_cert_callback = sub {
258 my ($self, $cert) = @_;
259
260 # check server certificate against cache of pinned FPs
261 # get fingerprint of server certificate
262 my $fp = Net::SSLeay::X509_get_fingerprint($cert, 'sha256');
263 return 0 if !defined($fp) || $fp eq ''; # error
264
265 my $valid = $self->{cached_fingerprints}->{$fp};
266 return $valid if defined($valid); # return cached result
267
268 if (my $cb = $self->{verify_fingerprint_cb}) {
269 $valid = $cb->($cert);
270 $self->{cached_fingerprints}->{$fp} = $valid;
271 return $valid;
272 }
273
274 $self->{last_unknown_fingerprint} = $fp;
275
276 return 0;
277 };
278
279 sub new {
280 my ($class, %param) = @_;
281
282 my $ssl_default_opts = { verify_hostname => 0 };
283 my $ssl_opts = $param{ssl_opts} || $ssl_default_opts;
284
285 my $self = {
286 username => $param{username},
287 password => $param{password},
288 host => $param{host} || 'localhost',
289 port => $param{port},
290 protocol => $param{protocol},
291 cookie_name => $param{cookie_name} // 'PVEAuthCookie',
292 manual_verification => $param{manual_verification},
293 cached_fingerprints => $param{cached_fingerprints} || {},
294 verify_fingerprint_cb => $param{verify_fingerprint_cb},
295 register_fingerprint_cb => $param{register_fingerprint_cb},
296 ssl_opts => $ssl_opts,
297 timeout => $param{timeout} || 60,
298 };
299 bless $self;
300
301 if (!$ssl_opts->{SSL_verify_callback}) {
302 $ssl_opts->{'SSL_verify_mode'} = SSL_VERIFY_PEER;
303 $ssl_opts->{'SSL_verify_callback'} = sub {
304 my (undef, undef, undef, undef, $cert, $depth) = @_;
305
306 # we don't care about intermediate or root certificates
307 return 1 if $depth != 0;
308
309 return $verify_cert_callback->($self, $cert);
310 }
311 }
312
313 if (!$self->{port}) {
314 $self->{port} = $self->{host} eq 'localhost' ? 85 : 8006;
315 }
316 if (!$self->{protocol}) {
317 $self->{protocol} = $self->{host} eq 'localhost' ? 'http' : 'https';
318 }
319
320 $self->{useragent} = LWP::UserAgent->new(
321 protocols_allowed => [ 'http', 'https'],
322 ssl_opts => $ssl_opts,
323 timeout => $self->{timeout},
324 keep_alive => $param{keep_alive} // 50,
325 );
326
327 $self->{useragent}->default_header('Accept-Encoding' => 'gzip'); # allow gzip
328
329 $self->update_ticket($param{ticket}) if $param{ticket};
330 $self->update_csrftoken($param{csrftoken}) if $param{csrftoken};
331
332
333 return $self;
334 }
335
336 1;