1 package PVE
::APIClient
::LWP
;
6 use IO
::Socket
::SSL
; # important for SSL_verify_callback
11 use Data
::Dumper
; # fixme: remove
12 use HTTP
::Request
::Common
;
14 use PVE
::APIClient
::Exception
qw(raise);
16 my $extract_data = sub {
19 croak
"undefined result" if !defined($res);
20 croak
"undefined result data" if !exists($res->{data
});
26 my ($self, $path, $param) = @_;
28 return $self->call('GET', $path, $param);
32 my ($self, $path, $param) = @_;
34 return $extract_data->($self->call('GET', $path, $param));
38 my ($self, $path, $param) = @_;
40 return $self->call('POST', $path, $param);
44 my ($self, $path, $param) = @_;
46 return $extract_data->($self->call('POST', $path, $param));
50 my ($self, $path, $param) = @_;
52 return $self->call('PUT', $path, $param);
56 my ($self, $path, $param) = @_;
58 return $extract_data->($self->call('PUT', $path, $param));
62 my ($self, $path, $param) = @_;
64 return $self->call('DELETE', $path, $param);
68 my ($self, $path, $param) = @_;
70 return $extract_data->($self->call('DELETE', $path, $param));
73 sub update_csrftoken
{
74 my ($self, $csrftoken) = @_;
76 $self->{csrftoken
} = $csrftoken;
78 my $agent = $self->{useragent
};
80 $agent->default_header('CSRFPreventionToken', $self->{csrftoken
});
84 my ($self, $ticket) = @_;
86 my $agent = $self->{useragent
};
88 $self->{ticket
} = $ticket;
90 my $encticket = uri_escape
($ticket);
91 my $cookie = "$self->{cookie_name}=$encticket; path=/; secure;";
92 $agent->default_header('Cookie', $cookie);
99 $uri->scheme($self->{protocol
});
100 $uri->host($self->{host
});
101 $uri->port($self->{port
});
102 $uri->path('/api2/json/access/ticket');
104 my $ua = $self->{useragent
};
106 delete $self->{last_unknown_fingerprint
};
108 my $exec_login = sub {
109 return $ua->post($uri, {
110 username
=> $self->{username
} || 'unknown',
111 password
=> $self->{password
} || ''});
114 my $response = $exec_login->();
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
124 if (!$response->is_success) {
125 raise
($response->status_line ."\n", code
=> $response->code)
128 my $res = from_json
($response->decoded_content, {utf8
=> 1, allow_nonref
=> 1});
130 my $data = $extract_data->($res);
132 $self->update_ticket($data->{ticket
});
133 $self->update_csrftoken($data->{CSRFPreventionToken
});
138 sub manual_verify_fingerprint
{
139 my ($self, $fingerprint) = @_;
141 if (!$self->{manual_verification
}) {
142 warn "fingerprint: $fingerprint\n";
146 print "The authenticity of host '$self->{host}' can't be established.\n" .
147 "X509 SHA256 key fingerprint is $fingerprint.\n" .
148 "Are you sure you want to continue connecting (yes/no)? ";
152 my $valid = ($answer =~ m/^\s*yes\s*$/i) ?
1 : 0;
154 $self->{cached_fingerprints
}->{$fingerprint} = $valid;
156 if (my $cb = $self->{register_fingerprint_cb
}) {
157 $cb->($fingerprint) if $valid;
164 my ($self, $method, $path, $param) = @_;
166 delete $self->{last_unknown_fingerprint
};
168 my $ticket = $self->{ticket
};
170 my $ua = $self->{useragent
};
172 # fixme: check ticket lifetime?
174 if (!$ticket && $self->{username
} && $self->{password
}) {
178 my $uri = URI-
>new();
179 $uri->scheme($self->{protocol
});
180 $uri->host($self->{host
});
181 $uri->port($self->{port
});
185 if ($path !~ m!^api2/!) {
186 $uri->path("api2/json/$path");
191 #print "CALL $method : " . $uri->as_string() . "\n";
193 my $exec_method = sub {
196 if ($method eq 'GET') {
197 $uri->query_form($param);
198 $response = $ua->request(HTTP
::Request
::Common
::GET
($uri));
199 } elsif ($method eq 'POST') {
200 $response = $ua->request(HTTP
::Request
::Common
::POST
($uri, Content
=> $param));
201 } elsif ($method eq 'PUT') {
202 # We use another temporary URI object to format
203 # the application/x-www-form-urlencoded content.
205 my $tmpurl = URI-
>new('http:');
206 $tmpurl->query_form(%$param);
207 my $content = $tmpurl->query;
209 $response = $ua->request(HTTP
::Request
::Common
::PUT
($uri, 'Content-Type' => 'application/x-www-form-urlencoded', Content
=> $content));
211 } elsif ($method eq 'DELETE') {
212 $response = $ua->request(HTTP
::Request
::Common
::DELETE
($uri));
214 raise
("method $method not implemented\n");
219 my $response = $exec_method->();
221 if (my $fp = delete($self->{last_unknown_fingerprint
})) {
222 if ($self->manual_verify_fingerprint($fp)) {
223 $response = $exec_method->(); # try again
227 #print "RESP: " . Dumper($response) . "\n";
229 my $ct = $response->header('Content-Type') || '';
231 if ($response->is_success) {
233 raise
("got unexpected content type", code
=> $response->code)
234 if $ct !~ m
|application
/json
|;
236 return from_json
($response->decoded_content, {utf8
=> 1, allow_nonref
=> 1});
240 my $msg = $response->message;
242 return if $ct !~ m
|application
/json
|;
243 my $res = from_json
($response->decoded_content, {utf8
=> 1, allow_nonref
=> 1});
244 return $res->{errors
};
247 raise
("$msg\n", code
=> $response->code, errors
=> $errors);
251 my $verify_cert_callback = sub {
252 my ($self, $cert) = @_;
254 # check server certificate against cache of pinned FPs
255 # get fingerprint of server certificate
256 my $fp = Net
::SSLeay
::X509_get_fingerprint
($cert, 'sha256');
257 return 0 if !defined($fp) || $fp eq ''; # error
259 my $valid = $self->{cached_fingerprints
}->{$fp};
260 return $valid if defined($valid); # return cached result
262 if (my $cb = $self->{verify_fingerprint_cb
}) {
263 $valid = $cb->($cert);
264 $self->{cached_fingerprints
}->{$fp} = $valid;
268 $self->{last_unknown_fingerprint
} = $fp;
274 my ($class, %param) = @_;
276 my $ssl_default_opts = { verify_hostname
=> 0 };
277 my $ssl_opts = $param{ssl_opts
} || $ssl_default_opts;
280 username
=> $param{username
},
281 password
=> $param{password
},
282 host
=> $param{host
} || 'localhost',
283 port
=> $param{port
},
284 protocol
=> $param{protocol
},
285 cookie_name
=> $param{cookie_name
} // 'PVEAuthCookie',
286 manual_verification
=> $param{manual_verification
},
287 cached_fingerprints
=> $param{cached_fingerprints
} || {},
288 verify_fingerprint_cb
=> $param{verify_fingerprint_cb
},
289 register_fingerprint_cb
=> $param{register_fingerprint_cb
},
290 ssl_opts
=> $ssl_opts,
291 timeout
=> $param{timeout
} || 60,
295 if (!$ssl_opts->{SSL_verify_callback
}) {
296 $ssl_opts->{'SSL_verify_mode'} = SSL_VERIFY_PEER
;
297 $ssl_opts->{'SSL_verify_callback'} = sub {
298 my (undef, undef, undef, undef, $cert, $depth) = @_;
300 # we don't care about intermediate or root certificates
301 return 1 if $depth != 0;
303 return $verify_cert_callback->($self, $cert);
307 if (!$self->{port
}) {
308 $self->{port
} = $self->{host
} eq 'localhost' ?
85 : 8006;
310 if (!$self->{protocol
}) {
311 $self->{protocol
} = $self->{host
} eq 'localhost' ?
'http' : 'https';
314 $self->{useragent
} = LWP
::UserAgent-
>new(
315 protocols_allowed
=> [ 'http', 'https'],
316 ssl_opts
=> $ssl_opts,
317 timeout
=> $self->{timeout
},
318 keep_alive
=> $param{keep_alive
} // 50,
321 $self->{useragent
}->default_header('Accept-Encoding' => 'gzip'); # allow gzip
323 $self->update_ticket($param{ticket
}) if $param{ticket
};
324 $self->update_csrftoken($param{csrftoken
}) if $param{csrftoken
};