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