1 package PVE
::APIClient
::LWP
;
7 use HTTP
::Request
::Common
;
8 use IO
::Socket
::SSL
; # important for SSL_verify_callback
15 use PVE
::APIClient
::Exception
qw(raise);
17 my $extract_data = sub {
20 croak
"undefined result" if !defined($res);
21 croak
"undefined result data" if !exists($res->{data
});
27 my ($self, $path, $param) = @_;
29 return $self->call('GET', $path, $param);
33 my ($self, $path, $param) = @_;
35 return $extract_data->($self->call('GET', $path, $param));
39 my ($self, $path, $param) = @_;
41 return $self->call('POST', $path, $param);
45 my ($self, $path, $param) = @_;
47 return $extract_data->($self->call('POST', $path, $param));
51 my ($self, $path, $param) = @_;
53 return $self->call('PUT', $path, $param);
57 my ($self, $path, $param) = @_;
59 return $extract_data->($self->call('PUT', $path, $param));
63 my ($self, $path, $param) = @_;
65 return $self->call('DELETE', $path, $param);
69 my ($self, $path, $param) = @_;
71 return $extract_data->($self->call('DELETE', $path, $param));
74 sub update_csrftoken
{
75 my ($self, $csrftoken) = @_;
77 $self->{csrftoken
} = $csrftoken;
79 my $agent = $self->{useragent
};
81 $agent->default_header('CSRFPreventionToken', $self->{csrftoken
});
85 my ($self, $ticket) = @_;
87 my $agent = $self->{useragent
};
89 $self->{ticket
} = $ticket;
91 my $encticket = uri_escape
($ticket);
92 my $cookie = "$self->{cookie_name}=$encticket; path=/; secure;";
93 $agent->default_header('Cookie', $cookie);
96 sub two_factor_auth_login
{
97 my ($self, $type, $challenge) = @_;
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
>;
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.");
109 raise
("Authentication type '$type' not recognized, aborting!");
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');
122 my $ua = $self->{useragent
};
123 my $username = $self->{username
} // 'unknown',
125 delete $self->{fingerprint
}->{last_unknown
};
127 my $exec_login = sub {
128 return $ua->post($uri, {
129 username
=> $username,
130 password
=> $self->{password
} || ''});
133 my $response = $exec_login->();
135 if (!$response->is_success) {
136 if (my $fp = delete($self->{fingerprint
}->{last_unknown
})) {
137 if ($self->manual_verify_fingerprint($fp)) {
138 $response = $exec_login->(); # try again
143 if (!$response->is_success) {
144 raise
($response->status_line ."\n", code
=> $response->code)
147 my $res = from_json
($response->decoded_content, {utf8
=> 1, allow_nonref
=> 1});
149 my $data = $extract_data->($res);
150 $self->update_ticket($data->{ticket
});
151 $self->update_csrftoken($data->{CSRFPreventionToken
});
153 # handle two-factor login
154 my $tfa_ticket_re = qr/^([^\s!]+)![^!]*(!([0-9a-zA-Z\/.=_\
-+]+))?
$/;
155 if ($data->{ticket
} =~ m/$tfa_ticket_re/) {
156 my ($type, $challenge) = ($1, $2);
157 $data = $self->two_factor_auth_login($type, $challenge);
158 $self->update_ticket($data->{ticket
});
164 sub manual_verify_fingerprint
{
165 my ($self, $fingerprint) = @_;
167 if (!$self->{manual_verification
}) {
168 raise
("fingerprint '$fingerprint' not verified, abort!\n");
171 print "The authenticity of host '$self->{host}' can't be established.\n" .
172 "X509 SHA256 key fingerprint is $fingerprint.\n" .
173 "Are you sure you want to continue connecting (yes/no)? ";
175 my $answer = <STDIN
>;
177 my $valid = ($answer =~ m/^\s*yes\s*$/i) ?
1 : 0;
179 $self->{fingerprint
}->{cache
}->{$fingerprint} = $valid;
181 raise
("Fingerprint not verified, abort!\n") if !$valid;
183 if (my $cb = $self->{register_fingerprint_cb
}) {
184 $cb->($fingerprint) if $valid;
191 my ($self, $method, $path, $param) = @_;
193 delete $self->{fingerprint
}->{last_unknown
};
195 my $ticket = $self->{ticket
};
196 my $apitoken = $self->{apitoken
};
198 my $ua = $self->{useragent
};
200 # fixme: check ticket lifetime?
202 if (!$ticket && !$apitoken && $self->{username
} && $self->{password
}) {
206 my $uri = URI-
>new();
207 $uri->scheme($self->{protocol
});
208 $uri->host($self->{host
});
209 $uri->port($self->{port
});
213 if ($path !~ m!^api2/!) {
214 $uri->path("api2/json/$path");
219 #print "CALL $method : " . $uri->as_string() . "\n";
221 my $exec_method = sub {
224 if ($method eq 'GET') {
225 $uri->query_form($param);
226 $response = $ua->request(HTTP
::Request
::Common
::GET
($uri));
227 } elsif ($method eq 'POST') {
228 $response = $ua->request(HTTP
::Request
::Common
::POST
($uri, Content
=> $param));
229 } elsif ($method eq 'PUT') {
230 # We use another temporary URI object to format
231 # the application/x-www-form-urlencoded content.
233 my $tmpurl = URI-
>new('http:');
234 $tmpurl->query_form(%$param);
235 my $content = $tmpurl->query;
237 $response = $ua->request(HTTP
::Request
::Common
::PUT
($uri, 'Content-Type' => 'application/x-www-form-urlencoded', Content
=> $content));
239 } elsif ($method eq 'DELETE') {
240 $response = $ua->request(HTTP
::Request
::Common
::DELETE
($uri));
242 raise
("method $method not implemented\n");
247 my $response = $exec_method->();
249 if (my $fp = delete($self->{fingerprint
}->{last_unknown
})) {
250 if ($self->manual_verify_fingerprint($fp)) {
251 $response = $exec_method->(); # try again
255 my $ct = $response->header('Content-Type') || '';
257 if ($response->is_success) {
259 raise
("got unexpected content type", code
=> $response->code)
260 if $ct !~ m
|application
/json
|;
262 return from_json
($response->decoded_content, {utf8
=> 1, allow_nonref
=> 1});
266 my $msg = $response->message;
268 return if $ct !~ m
|application
/json
|;
269 my $res = from_json
($response->decoded_content, {utf8
=> 1, allow_nonref
=> 1});
270 return $res->{errors
};
273 raise
("$msg\n", code
=> $response->code, errors
=> $errors);
277 my sub verify_cert_callback
{
278 my ($fingerprint, $cert, $verify_cb) = @_;
280 # check server certificate against cache of pinned FPs
281 # get fingerprint of server certificate
282 my $fp = Net
::SSLeay
::X509_get_fingerprint
($cert, 'sha256');
283 return 0 if !defined($fp) || $fp eq ''; # error
285 my $valid = $fingerprint->{cache
}->{$fp};
286 return $valid if defined($valid); # return cached result
289 $valid = $verify_cb->($cert);
290 $fingerprint->{cache
}->{$fp} = $valid;
294 $fingerprint->{last_unknown
} = $fp;
300 my ($class, %param) = @_;
302 my $ssl_default_opts = { verify_hostname
=> 0 };
303 my $ssl_opts = $param{ssl_opts
} || $ssl_default_opts;
306 username
=> $param{username
},
307 password
=> $param{password
},
308 host
=> $param{host
} || 'localhost',
309 port
=> $param{port
},
310 protocol
=> $param{protocol
},
311 cookie_name
=> $param{cookie_name
} // 'PVEAuthCookie',
312 manual_verification
=> $param{manual_verification
},
314 cache
=> $param{cached_fingerprints
} || {},
315 last_unknown
=> undef,
317 register_fingerprint_cb
=> $param{register_fingerprint_cb
},
318 timeout
=> $param{timeout
} || 60,
322 if (!$ssl_opts->{SSL_verify_callback
}) {
323 $ssl_opts->{'SSL_verify_mode'} = SSL_VERIFY_PEER
;
325 my $fingerprint = $self->{fingerprint
}; # avoid passing $self, that's a RC cycle!
326 my $verify_fingerprint_cb = $param{verify_fingerprint_cb
};
327 $ssl_opts->{'SSL_verify_callback'} = sub {
328 my (undef, undef, undef, undef, $cert, $depth) = @_;
330 # we don't care about intermediate or root certificates
331 return 1 if $depth != 0;
333 return verify_cert_callback
($fingerprint, $cert, $verify_fingerprint_cb);
337 if (!$self->{port
}) {
338 $self->{port
} = $self->{host
} eq 'localhost' ?
85 : 8006;
340 if (!$self->{protocol
}) {
341 $self->{protocol
} = $self->{host
} eq 'localhost' ?
'http' : 'https';
344 $self->{useragent
} = LWP
::UserAgent-
>new(
345 protocols_allowed
=> [ 'http', 'https'],
346 ssl_opts
=> $ssl_opts,
347 timeout
=> $self->{timeout
},
348 keep_alive
=> $param{keep_alive
} // 50,
351 $self->{useragent
}->default_header('Accept-Encoding' => 'gzip'); # allow gzip
353 if ($param{apitoken
} && $param{password
}) {
354 warn "password will be ignored in favor of API token\n";
355 delete $self->{password
};
357 if ($param{ticket
}) {
358 if ($param{apitoken
}) {
359 warn "ticket will be ignored in favor of API token\n";
361 $self->update_ticket($param{ticket
});
364 $self->update_csrftoken($param{csrftoken
}) if $param{csrftoken
};
366 if ($param{apitoken
}) {
367 my $agent = $self->{useragent
};
369 $self->{apitoken
} = $param{apitoken
};
371 $agent->default_header('Authorization', $param{apitoken
});