]>
Commit | Line | Data |
---|---|---|
fe000966 DM |
1 | package PVE::Cluster; |
2 | ||
3 | use strict; | |
7181f622 | 4 | use warnings; |
62613060 | 5 | use POSIX qw(EEXIST); |
fe000966 DM |
6 | use File::stat qw(); |
7 | use Socket; | |
8 | use Storable qw(dclone); | |
9 | use IO::File; | |
10 | use MIME::Base64; | |
440121dc | 11 | use Digest::SHA; |
fe000966 | 12 | use Digest::HMAC_SHA1; |
26784563 | 13 | use Net::SSLeay; |
fe000966 DM |
14 | use PVE::Tools; |
15 | use PVE::INotify; | |
16 | use PVE::IPCC; | |
17 | use PVE::SafeSyslog; | |
d0ad18e8 | 18 | use PVE::JSONSchema; |
54d487bf | 19 | use PVE::Network; |
fe000966 DM |
20 | use JSON; |
21 | use RRDs; | |
22 | use Encode; | |
ed8eb70d | 23 | use UUID; |
fe000966 DM |
24 | use base 'Exporter'; |
25 | ||
26 | our @EXPORT_OK = qw( | |
27 | cfs_read_file | |
28 | cfs_write_file | |
29 | cfs_register_file | |
30 | cfs_lock_file); | |
31 | ||
32 | use Data::Dumper; # fixme: remove | |
33 | ||
34 | # x509 certificate utils | |
35 | ||
36 | my $basedir = "/etc/pve"; | |
37 | my $authdir = "$basedir/priv"; | |
38 | my $lockdir = "/etc/pve/priv/lock"; | |
39 | ||
40 | my $authprivkeyfn = "$authdir/authkey.key"; | |
41 | my $authpubkeyfn = "$basedir/authkey.pub"; | |
42 | my $pveca_key_fn = "$authdir/pve-root-ca.key"; | |
43 | my $pveca_srl_fn = "$authdir/pve-root-ca.srl"; | |
44 | my $pveca_cert_fn = "$basedir/pve-root-ca.pem"; | |
45 | # this is just a secret accessable by the web browser | |
46 | # and is used for CSRF prevention | |
47 | my $pvewww_key_fn = "$basedir/pve-www.key"; | |
48 | ||
49 | # ssh related files | |
50 | my $ssh_rsa_id_priv = "/root/.ssh/id_rsa"; | |
51 | my $ssh_rsa_id = "/root/.ssh/id_rsa.pub"; | |
52 | my $ssh_host_rsa_id = "/etc/ssh/ssh_host_rsa_key.pub"; | |
53 | my $sshglobalknownhosts = "/etc/ssh/ssh_known_hosts"; | |
54 | my $sshknownhosts = "/etc/pve/priv/known_hosts"; | |
55 | my $sshauthkeys = "/etc/pve/priv/authorized_keys"; | |
ac50b36d | 56 | my $sshd_config_fn = "/etc/ssh/sshd_config"; |
fe000966 | 57 | my $rootsshauthkeys = "/root/.ssh/authorized_keys"; |
6056578e | 58 | my $rootsshauthkeysbackup = "${rootsshauthkeys}.org"; |
f666cdde | 59 | my $rootsshconfig = "/root/.ssh/config"; |
fe000966 DM |
60 | |
61 | my $observed = { | |
e1735a61 | 62 | 'vzdump.cron' => 1, |
fe000966 DM |
63 | 'storage.cfg' => 1, |
64 | 'datacenter.cfg' => 1, | |
f6de131a | 65 | 'replication.cfg' => 1, |
cafc7309 DM |
66 | 'corosync.conf' => 1, |
67 | 'corosync.conf.new' => 1, | |
fe000966 DM |
68 | 'user.cfg' => 1, |
69 | 'domains.cfg' => 1, | |
70 | 'priv/shadow.cfg' => 1, | |
71 | '/qemu-server/' => 1, | |
f71eee41 | 72 | '/openvz/' => 1, |
7f66b436 | 73 | '/lxc/' => 1, |
5a5417e6 DM |
74 | 'ha/crm_commands' => 1, |
75 | 'ha/manager_status' => 1, | |
76 | 'ha/resources.cfg' => 1, | |
77 | 'ha/groups.cfg' => 1, | |
e9af3eb7 | 78 | 'ha/fence.cfg' => 1, |
9d4f69ff | 79 | 'status.cfg' => 1, |
fe000966 DM |
80 | }; |
81 | ||
82 | # only write output if something fails | |
83 | sub run_silent_cmd { | |
84 | my ($cmd) = @_; | |
85 | ||
86 | my $outbuf = ''; | |
87 | ||
88 | my $record_output = sub { | |
89 | $outbuf .= shift; | |
90 | $outbuf .= "\n"; | |
91 | }; | |
92 | ||
93 | eval { | |
c53b111f | 94 | PVE::Tools::run_command($cmd, outfunc => $record_output, |
fe000966 DM |
95 | errfunc => $record_output); |
96 | }; | |
97 | ||
98 | my $err = $@; | |
99 | ||
100 | if ($err) { | |
101 | print STDERR $outbuf; | |
102 | die $err; | |
103 | } | |
104 | } | |
105 | ||
106 | sub check_cfs_quorum { | |
01dddfb9 DM |
107 | my ($noerr) = @_; |
108 | ||
fe000966 DM |
109 | # note: -w filename always return 1 for root, so wee need |
110 | # to use File::lstat here | |
111 | my $st = File::stat::lstat("$basedir/local"); | |
01dddfb9 DM |
112 | my $quorate = ($st && (($st->mode & 0200) != 0)); |
113 | ||
114 | die "cluster not ready - no quorum?\n" if !$quorate && !$noerr; | |
115 | ||
116 | return $quorate; | |
fe000966 DM |
117 | } |
118 | ||
119 | sub check_cfs_is_mounted { | |
120 | my ($noerr) = @_; | |
121 | ||
122 | my $res = -l "$basedir/local"; | |
123 | ||
124 | die "pve configuration filesystem not mounted\n" | |
125 | if !$res && !$noerr; | |
126 | ||
127 | return $res; | |
128 | } | |
129 | ||
130 | sub gen_local_dirs { | |
131 | my ($nodename) = @_; | |
132 | ||
133 | check_cfs_is_mounted(); | |
134 | ||
135 | my @required_dirs = ( | |
136 | "$basedir/priv", | |
c53b111f | 137 | "$basedir/nodes", |
fe000966 | 138 | "$basedir/nodes/$nodename", |
7f66b436 | 139 | "$basedir/nodes/$nodename/lxc", |
a1c08cfa DM |
140 | "$basedir/nodes/$nodename/qemu-server", |
141 | "$basedir/nodes/$nodename/openvz", | |
fe000966 | 142 | "$basedir/nodes/$nodename/priv"); |
c53b111f | 143 | |
fe000966 DM |
144 | foreach my $dir (@required_dirs) { |
145 | if (! -d $dir) { | |
62613060 | 146 | mkdir($dir) || $! == EEXIST || die "unable to create directory '$dir' - $!\n"; |
fe000966 DM |
147 | } |
148 | } | |
149 | } | |
150 | ||
151 | sub gen_auth_key { | |
152 | ||
153 | return if -f "$authprivkeyfn"; | |
154 | ||
155 | check_cfs_is_mounted(); | |
156 | ||
62613060 | 157 | mkdir $authdir || $! == EEXIST || die "unable to create dir '$authdir' - $!\n"; |
fe000966 | 158 | |
2d899b19 | 159 | run_silent_cmd(['openssl', 'genrsa', '-out', $authprivkeyfn, '2048']); |
fe000966 | 160 | |
de4b4155 | 161 | run_silent_cmd(['openssl', 'rsa', '-in', $authprivkeyfn, '-pubout', '-out', $authpubkeyfn]); |
fe000966 DM |
162 | } |
163 | ||
164 | sub gen_pveca_key { | |
165 | ||
166 | return if -f $pveca_key_fn; | |
167 | ||
168 | eval { | |
147661a8 | 169 | run_silent_cmd(['openssl', 'genrsa', '-out', $pveca_key_fn, '4096']); |
fe000966 DM |
170 | }; |
171 | ||
172 | die "unable to generate pve ca key:\n$@" if $@; | |
173 | } | |
174 | ||
175 | sub gen_pveca_cert { | |
176 | ||
177 | if (-f $pveca_key_fn && -f $pveca_cert_fn) { | |
178 | return 0; | |
179 | } | |
180 | ||
181 | gen_pveca_key(); | |
182 | ||
183 | # we try to generate an unique 'subject' to avoid browser problems | |
184 | # (reused serial numbers, ..) | |
ed8eb70d FG |
185 | my $uuid; |
186 | UUID::generate($uuid); | |
187 | my $uuid_str; | |
188 | UUID::unparse($uuid, $uuid_str); | |
fe000966 DM |
189 | |
190 | eval { | |
f5566fc6 FG |
191 | # wrap openssl with faketime to prevent bug #904 |
192 | run_silent_cmd(['faketime', 'yesterday', 'openssl', 'req', '-batch', | |
193 | '-days', '3650', '-new', '-x509', '-nodes', '-key', | |
fe000966 | 194 | $pveca_key_fn, '-out', $pveca_cert_fn, '-subj', |
ed8eb70d | 195 | "/CN=Proxmox Virtual Environment/OU=$uuid_str/O=PVE Cluster Manager CA/"]); |
fe000966 DM |
196 | }; |
197 | ||
198 | die "generating pve root certificate failed:\n$@" if $@; | |
199 | ||
200 | return 1; | |
201 | } | |
202 | ||
203 | sub gen_pve_ssl_key { | |
204 | my ($nodename) = @_; | |
205 | ||
206 | die "no node name specified" if !$nodename; | |
207 | ||
208 | my $pvessl_key_fn = "$basedir/nodes/$nodename/pve-ssl.key"; | |
209 | ||
210 | return if -f $pvessl_key_fn; | |
211 | ||
212 | eval { | |
213 | run_silent_cmd(['openssl', 'genrsa', '-out', $pvessl_key_fn, '2048']); | |
214 | }; | |
215 | ||
216 | die "unable to generate pve ssl key for node '$nodename':\n$@" if $@; | |
217 | } | |
218 | ||
219 | sub gen_pve_www_key { | |
220 | ||
221 | return if -f $pvewww_key_fn; | |
222 | ||
223 | eval { | |
224 | run_silent_cmd(['openssl', 'genrsa', '-out', $pvewww_key_fn, '2048']); | |
225 | }; | |
226 | ||
227 | die "unable to generate pve www key:\n$@" if $@; | |
228 | } | |
229 | ||
230 | sub update_serial { | |
231 | my ($serial) = @_; | |
232 | ||
233 | PVE::Tools::file_set_contents($pveca_srl_fn, $serial); | |
234 | } | |
235 | ||
236 | sub gen_pve_ssl_cert { | |
237 | my ($force, $nodename, $ip) = @_; | |
238 | ||
239 | die "no node name specified" if !$nodename; | |
240 | die "no IP specified" if !$ip; | |
241 | ||
242 | my $pvessl_cert_fn = "$basedir/nodes/$nodename/pve-ssl.pem"; | |
243 | ||
244 | return if !$force && -f $pvessl_cert_fn; | |
245 | ||
8acde170 | 246 | my $names = "IP:127.0.0.1,IP:::1,DNS:localhost"; |
fe000966 DM |
247 | |
248 | my $rc = PVE::INotify::read_file('resolvconf'); | |
249 | ||
250 | $names .= ",IP:$ip"; | |
c53b111f | 251 | |
fe000966 DM |
252 | my $fqdn = $nodename; |
253 | ||
254 | $names .= ",DNS:$nodename"; | |
255 | ||
256 | if ($rc && $rc->{search}) { | |
257 | $fqdn = $nodename . "." . $rc->{search}; | |
258 | $names .= ",DNS:$fqdn"; | |
259 | } | |
260 | ||
261 | my $sslconf = <<__EOD; | |
262 | RANDFILE = /root/.rnd | |
263 | extensions = v3_req | |
264 | ||
265 | [ req ] | |
266 | default_bits = 2048 | |
267 | distinguished_name = req_distinguished_name | |
268 | req_extensions = v3_req | |
269 | prompt = no | |
270 | string_mask = nombstr | |
271 | ||
272 | [ req_distinguished_name ] | |
273 | organizationalUnitName = PVE Cluster Node | |
274 | organizationName = Proxmox Virtual Environment | |
275 | commonName = $fqdn | |
276 | ||
277 | [ v3_req ] | |
278 | basicConstraints = CA:FALSE | |
e544d064 | 279 | extendedKeyUsage = serverAuth |
fe000966 DM |
280 | subjectAltName = $names |
281 | __EOD | |
282 | ||
283 | my $cfgfn = "/tmp/pvesslconf-$$.tmp"; | |
284 | my $fh = IO::File->new ($cfgfn, "w"); | |
285 | print $fh $sslconf; | |
286 | close ($fh); | |
287 | ||
288 | my $reqfn = "/tmp/pvecertreq-$$.tmp"; | |
289 | unlink $reqfn; | |
290 | ||
291 | my $pvessl_key_fn = "$basedir/nodes/$nodename/pve-ssl.key"; | |
292 | eval { | |
293 | run_silent_cmd(['openssl', 'req', '-batch', '-new', '-config', $cfgfn, | |
294 | '-key', $pvessl_key_fn, '-out', $reqfn]); | |
295 | }; | |
296 | ||
297 | if (my $err = $@) { | |
298 | unlink $reqfn; | |
299 | unlink $cfgfn; | |
300 | die "unable to generate pve certificate request:\n$err"; | |
301 | } | |
302 | ||
303 | update_serial("0000000000000000") if ! -f $pveca_srl_fn; | |
304 | ||
305 | eval { | |
f5566fc6 FG |
306 | # wrap openssl with faketime to prevent bug #904 |
307 | run_silent_cmd(['faketime', 'yesterday', 'openssl', 'x509', '-req', | |
308 | '-in', $reqfn, '-days', '3650', '-out', $pvessl_cert_fn, | |
309 | '-CAkey', $pveca_key_fn, '-CA', $pveca_cert_fn, | |
310 | '-CAserial', $pveca_srl_fn, '-extfile', $cfgfn]); | |
fe000966 DM |
311 | }; |
312 | ||
313 | if (my $err = $@) { | |
314 | unlink $reqfn; | |
315 | unlink $cfgfn; | |
316 | die "unable to generate pve ssl certificate:\n$err"; | |
317 | } | |
318 | ||
319 | unlink $cfgfn; | |
320 | unlink $reqfn; | |
321 | } | |
322 | ||
323 | sub gen_pve_node_files { | |
324 | my ($nodename, $ip, $opt_force) = @_; | |
325 | ||
326 | gen_local_dirs($nodename); | |
327 | ||
328 | gen_auth_key(); | |
329 | ||
330 | # make sure we have a (cluster wide) secret | |
331 | # for CSRFR prevention | |
332 | gen_pve_www_key(); | |
333 | ||
334 | # make sure we have a (per node) private key | |
335 | gen_pve_ssl_key($nodename); | |
336 | ||
337 | # make sure we have a CA | |
338 | my $force = gen_pveca_cert(); | |
339 | ||
340 | $force = 1 if $opt_force; | |
341 | ||
342 | gen_pve_ssl_cert($force, $nodename, $ip); | |
343 | } | |
344 | ||
bd0ae7ff DM |
345 | my $vzdump_cron_dummy = <<__EOD; |
346 | # cluster wide vzdump cron schedule | |
347 | # Atomatically generated file - do not edit | |
348 | ||
349 | PATH="/usr/sbin:/usr/bin:/sbin:/bin" | |
350 | ||
351 | __EOD | |
352 | ||
353 | sub gen_pve_vzdump_symlink { | |
354 | ||
e1735a61 | 355 | my $filename = "/etc/pve/vzdump.cron"; |
bd0ae7ff DM |
356 | |
357 | my $link_fn = "/etc/cron.d/vzdump"; | |
358 | ||
359 | if ((-f $filename) && (! -l $link_fn)) { | |
360 | rename($link_fn, "/root/etc_cron_vzdump.org"); # make backup if file exists | |
361 | symlink($filename, $link_fn); | |
362 | } | |
363 | } | |
364 | ||
365 | sub gen_pve_vzdump_files { | |
366 | ||
e1735a61 | 367 | my $filename = "/etc/pve/vzdump.cron"; |
bd0ae7ff DM |
368 | |
369 | PVE::Tools::file_set_contents($filename, $vzdump_cron_dummy) | |
370 | if ! -f $filename; | |
371 | ||
372 | gen_pve_vzdump_symlink(); | |
373 | }; | |
374 | ||
fe000966 DM |
375 | my $versions = {}; |
376 | my $vmlist = {}; | |
377 | my $clinfo = {}; | |
378 | ||
379 | my $ipcc_send_rec = sub { | |
380 | my ($msgid, $data) = @_; | |
381 | ||
382 | my $res = PVE::IPCC::ipcc_send_rec($msgid, $data); | |
383 | ||
384 | die "ipcc_send_rec failed: $!\n" if !defined($res) && ($! != 0); | |
385 | ||
386 | return $res; | |
387 | }; | |
388 | ||
389 | my $ipcc_send_rec_json = sub { | |
390 | my ($msgid, $data) = @_; | |
391 | ||
392 | my $res = PVE::IPCC::ipcc_send_rec($msgid, $data); | |
393 | ||
394 | die "ipcc_send_rec failed: $!\n" if !defined($res) && ($! != 0); | |
395 | ||
396 | return decode_json($res); | |
397 | }; | |
398 | ||
399 | my $ipcc_get_config = sub { | |
400 | my ($path) = @_; | |
401 | ||
402 | my $bindata = pack "Z*", $path; | |
2db32d95 DM |
403 | my $res = PVE::IPCC::ipcc_send_rec(6, $bindata); |
404 | if (!defined($res)) { | |
405 | return undef if ($! != 0); | |
406 | return ''; | |
407 | } | |
408 | ||
409 | return $res; | |
fe000966 DM |
410 | }; |
411 | ||
412 | my $ipcc_get_status = sub { | |
413 | my ($name, $nodename) = @_; | |
414 | ||
415 | my $bindata = pack "Z[256]Z[256]", $name, ($nodename || ""); | |
416 | return PVE::IPCC::ipcc_send_rec(5, $bindata); | |
417 | }; | |
418 | ||
419 | my $ipcc_update_status = sub { | |
420 | my ($name, $data) = @_; | |
421 | ||
422 | my $raw = ref($data) ? encode_json($data) : $data; | |
423 | # update status | |
424 | my $bindata = pack "Z[256]Z*", $name, $raw; | |
425 | ||
426 | return &$ipcc_send_rec(4, $bindata); | |
427 | }; | |
428 | ||
429 | my $ipcc_log = sub { | |
430 | my ($priority, $ident, $tag, $msg) = @_; | |
431 | ||
432 | my $bindata = pack "CCCZ*Z*Z*", $priority, bytes::length($ident) + 1, | |
433 | bytes::length($tag) + 1, $ident, $tag, $msg; | |
434 | ||
435 | return &$ipcc_send_rec(7, $bindata); | |
436 | }; | |
437 | ||
438 | my $ipcc_get_cluster_log = sub { | |
439 | my ($user, $max) = @_; | |
440 | ||
441 | $max = 0 if !defined($max); | |
442 | ||
443 | my $bindata = pack "VVVVZ*", $max, 0, 0, 0, ($user || ""); | |
444 | return &$ipcc_send_rec(8, $bindata); | |
445 | }; | |
446 | ||
447 | my $ccache = {}; | |
448 | ||
449 | sub cfs_update { | |
450 | eval { | |
451 | my $res = &$ipcc_send_rec_json(1); | |
452 | #warn "GOT1: " . Dumper($res); | |
453 | die "no starttime\n" if !$res->{starttime}; | |
454 | ||
455 | if (!$res->{starttime} || !$versions->{starttime} || | |
456 | $res->{starttime} != $versions->{starttime}) { | |
457 | #print "detected changed starttime\n"; | |
458 | $vmlist = {}; | |
459 | $clinfo = {}; | |
460 | $ccache = {}; | |
461 | } | |
462 | ||
463 | $versions = $res; | |
464 | }; | |
465 | my $err = $@; | |
466 | if ($err) { | |
467 | $versions = {}; | |
468 | $vmlist = {}; | |
469 | $clinfo = {}; | |
470 | $ccache = {}; | |
471 | warn $err; | |
472 | } | |
473 | ||
474 | eval { | |
475 | if (!$clinfo->{version} || $clinfo->{version} != $versions->{clinfo}) { | |
476 | #warn "detected new clinfo\n"; | |
477 | $clinfo = &$ipcc_send_rec_json(2); | |
478 | } | |
479 | }; | |
480 | $err = $@; | |
481 | if ($err) { | |
482 | $clinfo = {}; | |
483 | warn $err; | |
484 | } | |
485 | ||
486 | eval { | |
487 | if (!$vmlist->{version} || $vmlist->{version} != $versions->{vmlist}) { | |
488 | #warn "detected new vmlist1\n"; | |
489 | $vmlist = &$ipcc_send_rec_json(3); | |
490 | } | |
491 | }; | |
492 | $err = $@; | |
493 | if ($err) { | |
494 | $vmlist = {}; | |
495 | warn $err; | |
496 | } | |
497 | } | |
498 | ||
499 | sub get_vmlist { | |
500 | return $vmlist; | |
501 | } | |
502 | ||
503 | sub get_clinfo { | |
504 | return $clinfo; | |
505 | } | |
506 | ||
9ddd4ae9 DM |
507 | sub get_members { |
508 | return $clinfo->{nodelist}; | |
509 | } | |
510 | ||
fe000966 DM |
511 | sub get_nodelist { |
512 | ||
513 | my $nodelist = $clinfo->{nodelist}; | |
514 | ||
515 | my $result = []; | |
516 | ||
517 | my $nodename = PVE::INotify::nodename(); | |
518 | ||
519 | if (!$nodelist || !$nodelist->{$nodename}) { | |
520 | return [ $nodename ]; | |
521 | } | |
522 | ||
523 | return [ keys %$nodelist ]; | |
524 | } | |
525 | ||
526 | sub broadcast_tasklist { | |
527 | my ($data) = @_; | |
528 | ||
529 | eval { | |
530 | &$ipcc_update_status("tasklist", $data); | |
531 | }; | |
532 | ||
533 | warn $@ if $@; | |
534 | } | |
535 | ||
536 | my $tasklistcache = {}; | |
537 | ||
538 | sub get_tasklist { | |
539 | my ($nodename) = @_; | |
540 | ||
541 | my $kvstore = $versions->{kvstore} || {}; | |
542 | ||
543 | my $nodelist = get_nodelist(); | |
544 | ||
545 | my $res = []; | |
546 | foreach my $node (@$nodelist) { | |
547 | next if $nodename && ($nodename ne $node); | |
548 | eval { | |
549 | my $ver = $kvstore->{$node}->{tasklist} if $kvstore->{$node}; | |
550 | my $cd = $tasklistcache->{$node}; | |
c53b111f | 551 | if (!$cd || !$ver || !$cd->{version} || |
cebe16ec | 552 | ($cd->{version} != $ver)) { |
fe000966 DM |
553 | my $raw = &$ipcc_get_status("tasklist", $node) || '[]'; |
554 | my $data = decode_json($raw); | |
555 | push @$res, @$data; | |
556 | $cd = $tasklistcache->{$node} = { | |
557 | data => $data, | |
558 | version => $ver, | |
559 | }; | |
560 | } elsif ($cd && $cd->{data}) { | |
561 | push @$res, @{$cd->{data}}; | |
562 | } | |
563 | }; | |
564 | my $err = $@; | |
565 | syslog('err', $err) if $err; | |
566 | } | |
567 | ||
568 | return $res; | |
569 | } | |
570 | ||
571 | sub broadcast_rrd { | |
572 | my ($rrdid, $data) = @_; | |
573 | ||
574 | eval { | |
575 | &$ipcc_update_status("rrd/$rrdid", $data); | |
576 | }; | |
577 | my $err = $@; | |
578 | ||
579 | warn $err if $err; | |
580 | } | |
581 | ||
582 | my $last_rrd_dump = 0; | |
583 | my $last_rrd_data = ""; | |
584 | ||
585 | sub rrd_dump { | |
586 | ||
587 | my $ctime = time(); | |
588 | ||
589 | my $diff = $ctime - $last_rrd_dump; | |
590 | if ($diff < 2) { | |
591 | return $last_rrd_data; | |
592 | } | |
593 | ||
594 | my $raw; | |
595 | eval { | |
596 | $raw = &$ipcc_send_rec(10); | |
597 | }; | |
598 | my $err = $@; | |
599 | ||
600 | if ($err) { | |
601 | warn $err; | |
602 | return {}; | |
603 | } | |
604 | ||
605 | my $res = {}; | |
606 | ||
c3fabca7 DM |
607 | if ($raw) { |
608 | while ($raw =~ s/^(.*)\n//) { | |
609 | my ($key, @ela) = split(/:/, $1); | |
610 | next if !$key; | |
611 | next if !(scalar(@ela) > 1); | |
d2aae33e | 612 | $res->{$key} = [ map { $_ eq 'U' ? undef : $_ } @ela ]; |
c3fabca7 | 613 | } |
fe000966 DM |
614 | } |
615 | ||
616 | $last_rrd_dump = $ctime; | |
617 | $last_rrd_data = $res; | |
618 | ||
619 | return $res; | |
620 | } | |
621 | ||
622 | sub create_rrd_data { | |
623 | my ($rrdname, $timeframe, $cf) = @_; | |
624 | ||
625 | my $rrddir = "/var/lib/rrdcached/db"; | |
626 | ||
627 | my $rrd = "$rrddir/$rrdname"; | |
628 | ||
629 | my $setup = { | |
630 | hour => [ 60, 70 ], | |
631 | day => [ 60*30, 70 ], | |
632 | week => [ 60*180, 70 ], | |
633 | month => [ 60*720, 70 ], | |
634 | year => [ 60*10080, 70 ], | |
635 | }; | |
636 | ||
637 | my ($reso, $count) = @{$setup->{$timeframe}}; | |
638 | my $ctime = $reso*int(time()/$reso); | |
639 | my $req_start = $ctime - $reso*$count; | |
640 | ||
641 | $cf = "AVERAGE" if !$cf; | |
642 | ||
643 | my @args = ( | |
644 | "-s" => $req_start, | |
645 | "-e" => $ctime - 1, | |
646 | "-r" => $reso, | |
647 | ); | |
648 | ||
649 | my $socket = "/var/run/rrdcached.sock"; | |
650 | push @args, "--daemon" => "unix:$socket" if -S $socket; | |
651 | ||
652 | my ($start, $step, $names, $data) = RRDs::fetch($rrd, $cf, @args); | |
653 | ||
654 | my $err = RRDs::error; | |
655 | die "RRD error: $err\n" if $err; | |
c53b111f DM |
656 | |
657 | die "got wrong time resolution ($step != $reso)\n" | |
fe000966 DM |
658 | if $step != $reso; |
659 | ||
660 | my $res = []; | |
661 | my $fields = scalar(@$names); | |
662 | for my $line (@$data) { | |
663 | my $entry = { 'time' => $start }; | |
664 | $start += $step; | |
fe000966 DM |
665 | for (my $i = 0; $i < $fields; $i++) { |
666 | my $name = $names->[$i]; | |
667 | if (defined(my $val = $line->[$i])) { | |
668 | $entry->{$name} = $val; | |
669 | } else { | |
fba7c78c DC |
670 | # leave empty fields undefined |
671 | # maybe make this configurable? | |
fe000966 DM |
672 | } |
673 | } | |
fba7c78c | 674 | push @$res, $entry; |
fe000966 DM |
675 | } |
676 | ||
677 | return $res; | |
678 | } | |
679 | ||
680 | sub create_rrd_graph { | |
681 | my ($rrdname, $timeframe, $ds, $cf) = @_; | |
682 | ||
683 | # Using RRD graph is clumsy - maybe it | |
684 | # is better to simply fetch the data, and do all display | |
685 | # related things with javascript (new extjs html5 graph library). | |
c53b111f | 686 | |
fe000966 DM |
687 | my $rrddir = "/var/lib/rrdcached/db"; |
688 | ||
689 | my $rrd = "$rrddir/$rrdname"; | |
690 | ||
31938ad4 DM |
691 | my @ids = PVE::Tools::split_list($ds); |
692 | ||
693 | my $ds_txt = join('_', @ids); | |
694 | ||
695 | my $filename = "${rrd}_${ds_txt}.png"; | |
fe000966 DM |
696 | |
697 | my $setup = { | |
698 | hour => [ 60, 60 ], | |
699 | day => [ 60*30, 70 ], | |
700 | week => [ 60*180, 70 ], | |
701 | month => [ 60*720, 70 ], | |
702 | year => [ 60*10080, 70 ], | |
703 | }; | |
704 | ||
705 | my ($reso, $count) = @{$setup->{$timeframe}}; | |
706 | ||
707 | my @args = ( | |
708 | "--imgformat" => "PNG", | |
709 | "--border" => 0, | |
710 | "--height" => 200, | |
711 | "--width" => 800, | |
712 | "--start" => - $reso*$count, | |
713 | "--end" => 'now' , | |
8daa8f04 | 714 | "--lower-limit" => 0, |
fe000966 DM |
715 | ); |
716 | ||
717 | my $socket = "/var/run/rrdcached.sock"; | |
718 | push @args, "--daemon" => "unix:$socket" if -S $socket; | |
719 | ||
fe000966 DM |
720 | my @coldef = ('#00ddff', '#ff0000'); |
721 | ||
722 | $cf = "AVERAGE" if !$cf; | |
723 | ||
724 | my $i = 0; | |
725 | foreach my $id (@ids) { | |
726 | my $col = $coldef[$i++] || die "fixme: no color definition"; | |
727 | push @args, "DEF:${id}=$rrd:${id}:$cf"; | |
728 | my $dataid = $id; | |
729 | if ($id eq 'cpu' || $id eq 'iowait') { | |
730 | push @args, "CDEF:${id}_per=${id},100,*"; | |
731 | $dataid = "${id}_per"; | |
732 | } | |
733 | push @args, "LINE2:${dataid}${col}:${id}"; | |
734 | } | |
735 | ||
a665376e DM |
736 | push @args, '--full-size-mode'; |
737 | ||
31938ad4 | 738 | # we do not really store data into the file |
b871db9c | 739 | my $res = RRDs::graphv('', @args); |
fe000966 DM |
740 | |
741 | my $err = RRDs::error; | |
742 | die "RRD error: $err\n" if $err; | |
743 | ||
31938ad4 | 744 | return { filename => $filename, image => $res->{image} }; |
fe000966 DM |
745 | } |
746 | ||
747 | # a fast way to read files (avoid fuse overhead) | |
748 | sub get_config { | |
749 | my ($path) = @_; | |
750 | ||
d3a92ba7 | 751 | return &$ipcc_get_config($path); |
fe000966 DM |
752 | } |
753 | ||
754 | sub get_cluster_log { | |
755 | my ($user, $max) = @_; | |
756 | ||
757 | return &$ipcc_get_cluster_log($user, $max); | |
758 | } | |
759 | ||
760 | my $file_info = {}; | |
761 | ||
762 | sub cfs_register_file { | |
763 | my ($filename, $parser, $writer) = @_; | |
764 | ||
765 | $observed->{$filename} || die "unknown file '$filename'"; | |
766 | ||
767 | die "file '$filename' already registered" if $file_info->{$filename}; | |
768 | ||
769 | $file_info->{$filename} = { | |
770 | parser => $parser, | |
771 | writer => $writer, | |
772 | }; | |
773 | } | |
774 | ||
775 | my $ccache_read = sub { | |
776 | my ($filename, $parser, $version) = @_; | |
777 | ||
778 | $ccache->{$filename} = {} if !$ccache->{$filename}; | |
779 | ||
780 | my $ci = $ccache->{$filename}; | |
781 | ||
d3a92ba7 DM |
782 | if (!$ci->{version} || !$version || $ci->{version} != $version) { |
783 | # we always call the parser, even when the file does not exists | |
784 | # (in that case $data is undef) | |
fe000966 | 785 | my $data = get_config($filename); |
fe000966 DM |
786 | $ci->{data} = &$parser("/etc/pve/$filename", $data); |
787 | $ci->{version} = $version; | |
788 | } | |
789 | ||
790 | my $res = ref($ci->{data}) ? dclone($ci->{data}) : $ci->{data}; | |
791 | ||
792 | return $res; | |
793 | }; | |
794 | ||
795 | sub cfs_file_version { | |
796 | my ($filename) = @_; | |
797 | ||
798 | my $version; | |
799 | my $infotag; | |
6e73d5c2 | 800 | if ($filename =~ m!^nodes/[^/]+/(openvz|lxc|qemu-server)/(\d+)\.conf$!) { |
f71eee41 | 801 | my ($type, $vmid) = ($1, $2); |
fe000966 DM |
802 | if ($vmlist && $vmlist->{ids} && $vmlist->{ids}->{$vmid}) { |
803 | $version = $vmlist->{ids}->{$vmid}->{version}; | |
804 | } | |
f71eee41 | 805 | $infotag = "/$type/"; |
fe000966 DM |
806 | } else { |
807 | $infotag = $filename; | |
808 | $version = $versions->{$filename}; | |
809 | } | |
810 | ||
811 | my $info = $file_info->{$infotag} || | |
812 | die "unknown file type '$filename'\n"; | |
813 | ||
814 | return wantarray ? ($version, $info) : $version; | |
815 | } | |
816 | ||
817 | sub cfs_read_file { | |
818 | my ($filename) = @_; | |
819 | ||
c53b111f | 820 | my ($version, $info) = cfs_file_version($filename); |
fe000966 DM |
821 | my $parser = $info->{parser}; |
822 | ||
823 | return &$ccache_read($filename, $parser, $version); | |
824 | } | |
825 | ||
826 | sub cfs_write_file { | |
827 | my ($filename, $data) = @_; | |
828 | ||
c53b111f | 829 | my ($version, $info) = cfs_file_version($filename); |
fe000966 DM |
830 | |
831 | my $writer = $info->{writer} || die "no writer defined"; | |
832 | ||
833 | my $fsname = "/etc/pve/$filename"; | |
834 | ||
835 | my $raw = &$writer($fsname, $data); | |
836 | ||
837 | if (my $ci = $ccache->{$filename}) { | |
838 | $ci->{version} = undef; | |
839 | } | |
840 | ||
841 | PVE::Tools::file_set_contents($fsname, $raw); | |
842 | } | |
843 | ||
844 | my $cfs_lock = sub { | |
845 | my ($lockid, $timeout, $code, @param) = @_; | |
846 | ||
847 | my $res; | |
848 | ||
849 | # this timeout is for aquire the lock | |
850 | $timeout = 10 if !$timeout; | |
851 | ||
852 | my $filename = "$lockdir/$lockid"; | |
853 | ||
854 | my $msg = "can't aquire cfs lock '$lockid'"; | |
855 | ||
856 | eval { | |
857 | ||
858 | mkdir $lockdir; | |
859 | ||
860 | if (! -d $lockdir) { | |
861 | die "$msg: pve cluster filesystem not online.\n"; | |
862 | } | |
863 | ||
864 | local $SIG{ALRM} = sub { die "got lock request timeout\n"; }; | |
865 | ||
866 | alarm ($timeout); | |
867 | ||
868 | if (!(mkdir $filename)) { | |
869 | print STDERR "trying to aquire cfs lock '$lockid' ..."; | |
870 | while (1) { | |
871 | if (!(mkdir $filename)) { | |
872 | (utime 0, 0, $filename); # cfs unlock request | |
873 | } else { | |
874 | print STDERR " OK\n"; | |
875 | last; | |
876 | } | |
877 | sleep(1); | |
878 | } | |
879 | } | |
880 | ||
881 | # fixed command timeout: cfs locks have a timeout of 120 | |
882 | # using 60 gives us another 60 seconds to abort the task | |
883 | alarm(60); | |
884 | local $SIG{ALRM} = sub { die "got lock timeout - aborting command\n"; }; | |
885 | ||
9c206b2b DM |
886 | cfs_update(); # make sure we read latest versions inside code() |
887 | ||
fe000966 DM |
888 | $res = &$code(@param); |
889 | ||
890 | alarm(0); | |
891 | }; | |
892 | ||
893 | my $err = $@; | |
894 | ||
895 | alarm(0); | |
896 | ||
897 | if ($err && ($err eq "got lock request timeout\n") && | |
898 | !check_cfs_quorum()){ | |
899 | $err = "$msg: no quorum!\n"; | |
c53b111f | 900 | } |
fe000966 DM |
901 | |
902 | if (!$err || $err !~ /^got lock timeout -/) { | |
903 | rmdir $filename; # cfs unlock | |
904 | } | |
905 | ||
906 | if ($err) { | |
907 | $@ = $err; | |
908 | return undef; | |
909 | } | |
910 | ||
911 | $@ = undef; | |
912 | ||
913 | return $res; | |
914 | }; | |
915 | ||
916 | sub cfs_lock_file { | |
917 | my ($filename, $timeout, $code, @param) = @_; | |
918 | ||
919 | my $info = $observed->{$filename} || die "unknown file '$filename'"; | |
920 | ||
921 | my $lockid = "file-$filename"; | |
922 | $lockid =~ s/[.\/]/_/g; | |
923 | ||
924 | &$cfs_lock($lockid, $timeout, $code, @param); | |
925 | } | |
926 | ||
927 | sub cfs_lock_storage { | |
928 | my ($storeid, $timeout, $code, @param) = @_; | |
929 | ||
930 | my $lockid = "storage-$storeid"; | |
931 | ||
932 | &$cfs_lock($lockid, $timeout, $code, @param); | |
933 | } | |
934 | ||
78897707 TL |
935 | sub cfs_lock_domain { |
936 | my ($domainname, $timeout, $code, @param) = @_; | |
937 | ||
938 | my $lockid = "domain-$domainname"; | |
939 | ||
940 | &$cfs_lock($lockid, $timeout, $code, @param); | |
941 | } | |
942 | ||
fe000966 DM |
943 | my $log_levels = { |
944 | "emerg" => 0, | |
945 | "alert" => 1, | |
946 | "crit" => 2, | |
947 | "critical" => 2, | |
948 | "err" => 3, | |
949 | "error" => 3, | |
950 | "warn" => 4, | |
951 | "warning" => 4, | |
952 | "notice" => 5, | |
953 | "info" => 6, | |
954 | "debug" => 7, | |
955 | }; | |
956 | ||
957 | sub log_msg { | |
958 | my ($priority, $ident, $msg) = @_; | |
959 | ||
960 | if (my $tmp = $log_levels->{$priority}) { | |
961 | $priority = $tmp; | |
962 | } | |
963 | ||
964 | die "need numeric log priority" if $priority !~ /^\d+$/; | |
965 | ||
966 | my $tag = PVE::SafeSyslog::tag(); | |
967 | ||
968 | $msg = "empty message" if !$msg; | |
969 | ||
970 | $ident = "" if !$ident; | |
8f2d54ff | 971 | $ident = encode("ascii", $ident, |
fe000966 DM |
972 | sub { sprintf "\\u%04x", shift }); |
973 | ||
8f2d54ff | 974 | my $ascii = encode("ascii", $msg, sub { sprintf "\\u%04x", shift }); |
fe000966 DM |
975 | |
976 | if ($ident) { | |
977 | syslog($priority, "<%s> %s", $ident, $ascii); | |
978 | } else { | |
979 | syslog($priority, "%s", $ascii); | |
980 | } | |
981 | ||
982 | eval { &$ipcc_log($priority, $ident, $tag, $ascii); }; | |
983 | ||
984 | syslog("err", "writing cluster log failed: $@") if $@; | |
985 | } | |
986 | ||
9d76a1bb DM |
987 | sub check_vmid_unused { |
988 | my ($vmid, $noerr) = @_; | |
c53b111f | 989 | |
9d76a1bb DM |
990 | my $vmlist = get_vmlist(); |
991 | ||
992 | my $d = $vmlist->{ids}->{$vmid}; | |
993 | return 1 if !defined($d); | |
c53b111f | 994 | |
9d76a1bb DM |
995 | return undef if $noerr; |
996 | ||
4f66b109 | 997 | my $vmtypestr = $d->{type} eq 'qemu' ? 'VM' : 'CT'; |
e75ccbee | 998 | die "$vmtypestr $vmid already exists on node '$d->{node}'\n"; |
9d76a1bb DM |
999 | } |
1000 | ||
65ff467f DM |
1001 | sub check_node_exists { |
1002 | my ($nodename, $noerr) = @_; | |
1003 | ||
1004 | my $nodelist = $clinfo->{nodelist}; | |
1005 | return 1 if $nodelist && $nodelist->{$nodename}; | |
1006 | ||
1007 | return undef if $noerr; | |
1008 | ||
1009 | die "no such cluster node '$nodename'\n"; | |
1010 | } | |
1011 | ||
fe000966 DM |
1012 | # this is also used to get the IP of the local node |
1013 | sub remote_node_ip { | |
1014 | my ($nodename, $noerr) = @_; | |
1015 | ||
1016 | my $nodelist = $clinfo->{nodelist}; | |
1017 | if ($nodelist && $nodelist->{$nodename}) { | |
1018 | if (my $ip = $nodelist->{$nodename}->{ip}) { | |
fc31f517 WB |
1019 | return $ip if !wantarray; |
1020 | my $family = $nodelist->{$nodename}->{address_family}; | |
1021 | if (!$family) { | |
1022 | $nodelist->{$nodename}->{address_family} = | |
1023 | $family = | |
1024 | PVE::Tools::get_host_address_family($ip); | |
1025 | } | |
14830160 | 1026 | return wantarray ? ($ip, $family) : $ip; |
fe000966 DM |
1027 | } |
1028 | } | |
1029 | ||
1030 | # fallback: try to get IP by other means | |
e064c9b0 | 1031 | return PVE::Network::get_ip_from_hostname($nodename, $noerr); |
fe000966 DM |
1032 | } |
1033 | ||
54d487bf TL |
1034 | sub get_local_migration_ip { |
1035 | my ($migration_network, $noerr) = @_; | |
1036 | ||
1037 | my $cidr = $migration_network; | |
1038 | ||
1039 | if (!defined($cidr)) { | |
1040 | my $dc_conf = cfs_read_file('datacenter.cfg'); | |
1041 | $cidr = $dc_conf->{migration}->{network} | |
1042 | if defined($dc_conf->{migration}->{network}); | |
1043 | } | |
1044 | ||
1045 | if (defined($cidr)) { | |
1046 | my $ips = PVE::Network::get_local_ip_from_cidr($cidr); | |
1047 | ||
cb8c3bc6 TL |
1048 | die "could not get migration ip: no IP address configured on local " . |
1049 | "node for network '$cidr'\n" if !$noerr && (scalar(@$ips) == 0); | |
54d487bf | 1050 | |
cb8c3bc6 TL |
1051 | die "could not get migration ip: multiple IP address configured for " . |
1052 | "network '$cidr'\n" if !$noerr && (scalar(@$ips) > 1); | |
54d487bf TL |
1053 | |
1054 | return @$ips[0]; | |
1055 | } | |
1056 | ||
1057 | return undef; | |
1058 | }; | |
1059 | ||
fe000966 DM |
1060 | # ssh related utility functions |
1061 | ||
1062 | sub ssh_merge_keys { | |
1063 | # remove duplicate keys in $sshauthkeys | |
1064 | # ssh-copy-id simply add keys, so the file can grow to large | |
1065 | ||
1066 | my $data = ''; | |
1067 | if (-f $sshauthkeys) { | |
1068 | $data = PVE::Tools::file_get_contents($sshauthkeys, 128*1024); | |
1069 | chomp($data); | |
1070 | } | |
1071 | ||
6056578e DM |
1072 | my $found_backup; |
1073 | if (-f $rootsshauthkeysbackup) { | |
404343d7 | 1074 | $data .= "\n"; |
6056578e DM |
1075 | $data .= PVE::Tools::file_get_contents($rootsshauthkeysbackup, 128*1024); |
1076 | chomp($data); | |
1077 | $found_backup = 1; | |
1078 | } | |
1079 | ||
fe000966 DM |
1080 | # always add ourself |
1081 | if (-f $ssh_rsa_id) { | |
1082 | my $pub = PVE::Tools::file_get_contents($ssh_rsa_id); | |
1083 | chomp($pub); | |
1084 | $data .= "\n$pub\n"; | |
1085 | } | |
1086 | ||
1087 | my $newdata = ""; | |
1088 | my $vhash = {}; | |
2055b0a9 DM |
1089 | my @lines = split(/\n/, $data); |
1090 | foreach my $line (@lines) { | |
7eb37d8d SP |
1091 | if ($line !~ /^#/ && $line =~ m/(^|\s)ssh-(rsa|dsa)\s+(\S+)\s+\S+$/) { |
1092 | next if $vhash->{$3}++; | |
fe000966 | 1093 | } |
2055b0a9 | 1094 | $newdata .= "$line\n"; |
fe000966 | 1095 | } |
fe000966 DM |
1096 | |
1097 | PVE::Tools::file_set_contents($sshauthkeys, $newdata, 0600); | |
6056578e DM |
1098 | |
1099 | if ($found_backup && -l $rootsshauthkeys) { | |
1100 | # everything went well, so we can remove the backup | |
1101 | unlink $rootsshauthkeysbackup; | |
1102 | } | |
fe000966 DM |
1103 | } |
1104 | ||
ac50b36d | 1105 | sub setup_sshd_config { |
6c0e95b3 | 1106 | my ($start_sshd) = @_; |
ac50b36d DM |
1107 | |
1108 | my $conf = PVE::Tools::file_get_contents($sshd_config_fn); | |
c53b111f | 1109 | |
ac50b36d DM |
1110 | return if $conf =~ m/^PermitRootLogin\s+yes\s*$/m; |
1111 | ||
1112 | if ($conf !~ s/^#?PermitRootLogin.*$/PermitRootLogin yes/m) { | |
1113 | chomp $conf; | |
1114 | $conf .= "\nPermitRootLogin yes\n"; | |
c53b111f | 1115 | } |
ac50b36d DM |
1116 | |
1117 | PVE::Tools::file_set_contents($sshd_config_fn, $conf); | |
1118 | ||
6c0e95b3 DM |
1119 | my $cmd = $start_sshd ? 'reload-or-restart' : 'reload-or-try-restart'; |
1120 | PVE::Tools::run_command(['systemctl', $cmd, 'sshd']); | |
ac50b36d DM |
1121 | } |
1122 | ||
f666cdde SP |
1123 | sub setup_rootsshconfig { |
1124 | ||
39df71df DM |
1125 | # create ssh key if it does not exist |
1126 | if (! -f $ssh_rsa_id) { | |
1127 | mkdir '/root/.ssh/'; | |
1128 | system ("echo|ssh-keygen -t rsa -N '' -b 2048 -f ${ssh_rsa_id_priv}"); | |
1129 | } | |
1130 | ||
f666cdde SP |
1131 | # create ssh config if it does not exist |
1132 | if (! -f $rootsshconfig) { | |
9aabc24b DM |
1133 | mkdir '/root/.ssh'; |
1134 | if (my $fh = IO::File->new($rootsshconfig, O_CREAT|O_WRONLY|O_EXCL, 0640)) { | |
f666cdde | 1135 | # this is the default ciphers list from debian openssl0.9.8 except blowfish is added as prefered |
9aabc24b | 1136 | print $fh "Ciphers blowfish-cbc,aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc\n"; |
f666cdde SP |
1137 | close($fh); |
1138 | } | |
1139 | } | |
1140 | } | |
1141 | ||
fe000966 DM |
1142 | sub setup_ssh_keys { |
1143 | ||
fe000966 DM |
1144 | mkdir $authdir; |
1145 | ||
6056578e DM |
1146 | my $import_ok; |
1147 | ||
fe000966 | 1148 | if (! -f $sshauthkeys) { |
6056578e DM |
1149 | my $old; |
1150 | if (-f $rootsshauthkeys) { | |
1151 | $old = PVE::Tools::file_get_contents($rootsshauthkeys, 128*1024); | |
1152 | } | |
fe000966 | 1153 | if (my $fh = IO::File->new ($sshauthkeys, O_CREAT|O_WRONLY|O_EXCL, 0400)) { |
6056578e | 1154 | PVE::Tools::safe_print($sshauthkeys, $fh, $old) if $old; |
fe000966 | 1155 | close($fh); |
6056578e | 1156 | $import_ok = 1; |
fe000966 DM |
1157 | } |
1158 | } | |
1159 | ||
c53b111f | 1160 | warn "can't create shared ssh key database '$sshauthkeys'\n" |
fe000966 DM |
1161 | if ! -f $sshauthkeys; |
1162 | ||
404343d7 | 1163 | if (-f $rootsshauthkeys && ! -l $rootsshauthkeys) { |
6056578e DM |
1164 | if (!rename($rootsshauthkeys , $rootsshauthkeysbackup)) { |
1165 | warn "rename $rootsshauthkeys failed - $!\n"; | |
1166 | } | |
fe000966 DM |
1167 | } |
1168 | ||
1169 | if (! -l $rootsshauthkeys) { | |
1170 | symlink $sshauthkeys, $rootsshauthkeys; | |
1171 | } | |
fe000966 | 1172 | |
6056578e DM |
1173 | if (! -l $rootsshauthkeys) { |
1174 | warn "can't create symlink for ssh keys '$rootsshauthkeys' -> '$sshauthkeys'\n"; | |
1175 | } else { | |
1176 | unlink $rootsshauthkeysbackup if $import_ok; | |
1177 | } | |
fe000966 DM |
1178 | } |
1179 | ||
1180 | sub ssh_unmerge_known_hosts { | |
1181 | return if ! -l $sshglobalknownhosts; | |
1182 | ||
1183 | my $old = ''; | |
1184 | $old = PVE::Tools::file_get_contents($sshknownhosts, 128*1024) | |
1185 | if -f $sshknownhosts; | |
1186 | ||
1187 | PVE::Tools::file_set_contents($sshglobalknownhosts, $old); | |
1188 | } | |
1189 | ||
1190 | sub ssh_merge_known_hosts { | |
1191 | my ($nodename, $ip_address, $createLink) = @_; | |
1192 | ||
1193 | die "no node name specified" if !$nodename; | |
1194 | die "no ip address specified" if !$ip_address; | |
c53b111f | 1195 | |
fe000966 DM |
1196 | mkdir $authdir; |
1197 | ||
1198 | if (! -f $sshknownhosts) { | |
1199 | if (my $fh = IO::File->new($sshknownhosts, O_CREAT|O_WRONLY|O_EXCL, 0600)) { | |
1200 | close($fh); | |
1201 | } | |
1202 | } | |
1203 | ||
c53b111f DM |
1204 | my $old = PVE::Tools::file_get_contents($sshknownhosts, 128*1024); |
1205 | ||
fe000966 | 1206 | my $new = ''; |
c53b111f | 1207 | |
fe000966 DM |
1208 | if ((! -l $sshglobalknownhosts) && (-f $sshglobalknownhosts)) { |
1209 | $new = PVE::Tools::file_get_contents($sshglobalknownhosts, 128*1024); | |
1210 | } | |
1211 | ||
1212 | my $hostkey = PVE::Tools::file_get_contents($ssh_host_rsa_id); | |
1d182ad3 DM |
1213 | # Note: file sometimes containe emty lines at start, so we use multiline match |
1214 | die "can't parse $ssh_host_rsa_id" if $hostkey !~ m/^(ssh-rsa\s\S+)(\s.*)?$/m; | |
fe000966 DM |
1215 | $hostkey = $1; |
1216 | ||
1217 | my $data = ''; | |
1218 | my $vhash = {}; | |
1219 | ||
1220 | my $found_nodename; | |
1221 | my $found_local_ip; | |
1222 | ||
1223 | my $merge_line = sub { | |
1224 | my ($line, $all) = @_; | |
1225 | ||
1226 | if ($line =~ m/^(\S+)\s(ssh-rsa\s\S+)(\s.*)?$/) { | |
1227 | my $key = $1; | |
1228 | my $rsakey = $2; | |
1229 | if (!$vhash->{$key}) { | |
1230 | $vhash->{$key} = 1; | |
1231 | if ($key =~ m/\|1\|([^\|\s]+)\|([^\|\s]+)$/) { | |
1232 | my $salt = decode_base64($1); | |
1233 | my $digest = $2; | |
1234 | my $hmac = Digest::HMAC_SHA1->new($salt); | |
1235 | $hmac->add($nodename); | |
1236 | my $hd = $hmac->b64digest . '='; | |
1237 | if ($digest eq $hd) { | |
1238 | if ($rsakey eq $hostkey) { | |
1239 | $found_nodename = 1; | |
1240 | $data .= $line; | |
1241 | } | |
1242 | return; | |
1243 | } | |
1244 | $hmac = Digest::HMAC_SHA1->new($salt); | |
1245 | $hmac->add($ip_address); | |
1246 | $hd = $hmac->b64digest . '='; | |
1247 | if ($digest eq $hd) { | |
1248 | if ($rsakey eq $hostkey) { | |
1249 | $found_local_ip = 1; | |
1250 | $data .= $line; | |
1251 | } | |
1252 | return; | |
1253 | } | |
1254 | } | |
1255 | $data .= $line; | |
1256 | } | |
1257 | } elsif ($all) { | |
1258 | $data .= $line; | |
1259 | } | |
1260 | }; | |
1261 | ||
1262 | while ($old && $old =~ s/^((.*?)(\n|$))//) { | |
1263 | my $line = "$2\n"; | |
1264 | next if $line =~ m/^\s*$/; # skip empty lines | |
1265 | next if $line =~ m/^#/; # skip comments | |
1266 | &$merge_line($line, 1); | |
1267 | } | |
1268 | ||
1269 | while ($new && $new =~ s/^((.*?)(\n|$))//) { | |
1270 | my $line = "$2\n"; | |
1271 | next if $line =~ m/^\s*$/; # skip empty lines | |
1272 | next if $line =~ m/^#/; # skip comments | |
1273 | &$merge_line($line); | |
1274 | } | |
1275 | ||
1276 | my $addIndex = $$; | |
1277 | my $add_known_hosts_entry = sub { | |
1278 | my ($name, $hostkey) = @_; | |
1279 | $addIndex++; | |
1280 | my $hmac = Digest::HMAC_SHA1->new("$addIndex" . time()); | |
1281 | my $b64salt = $hmac->b64digest . '='; | |
1282 | $hmac = Digest::HMAC_SHA1->new(decode_base64($b64salt)); | |
1283 | $hmac->add($name); | |
1284 | my $digest = $hmac->b64digest . '='; | |
1285 | $data .= "|1|$b64salt|$digest $hostkey\n"; | |
1286 | }; | |
1287 | ||
1288 | if (!$found_nodename || !$found_local_ip) { | |
1289 | &$add_known_hosts_entry($nodename, $hostkey) if !$found_nodename; | |
1290 | &$add_known_hosts_entry($ip_address, $hostkey) if !$found_local_ip; | |
1291 | } | |
1292 | ||
1293 | PVE::Tools::file_set_contents($sshknownhosts, $data); | |
1294 | ||
1295 | return if !$createLink; | |
1296 | ||
1297 | unlink $sshglobalknownhosts; | |
1298 | symlink $sshknownhosts, $sshglobalknownhosts; | |
c53b111f DM |
1299 | |
1300 | warn "can't create symlink for ssh known hosts '$sshglobalknownhosts' -> '$sshknownhosts'\n" | |
fe000966 DM |
1301 | if ! -l $sshglobalknownhosts; |
1302 | ||
1303 | } | |
1304 | ||
bba12ad7 TL |
1305 | my $migration_format = { |
1306 | type => { | |
1307 | default_key => 1, | |
1308 | type => 'string', | |
1309 | enum => ['secure', 'insecure'], | |
1310 | description => "Migration traffic is encrypted using an SSH tunnel by " . | |
1311 | "default. On secure, completely private networks this can be " . | |
1312 | "disabled to increase performance.", | |
1313 | default => 'secure', | |
bba12ad7 TL |
1314 | }, |
1315 | network => { | |
1316 | optional => 1, | |
1317 | type => 'string', format => 'CIDR', | |
1318 | format_description => 'CIDR', | |
1319 | description => "CIDR of the (sub) network that is used for migration." | |
1320 | }, | |
1321 | }; | |
1322 | ||
fe000966 DM |
1323 | my $datacenter_schema = { |
1324 | type => "object", | |
1325 | additionalProperties => 0, | |
1326 | properties => { | |
1327 | keyboard => { | |
1328 | optional => 1, | |
1329 | type => 'string', | |
1330 | description => "Default keybord layout for vnc server.", | |
c59334cb | 1331 | enum => PVE::Tools::kvmkeymaplist(), |
fe000966 DM |
1332 | }, |
1333 | language => { | |
1334 | optional => 1, | |
1335 | type => 'string', | |
1336 | description => "Default GUI language.", | |
1337 | enum => [ 'en', 'de' ], | |
1338 | }, | |
1339 | http_proxy => { | |
1340 | optional => 1, | |
1341 | type => 'string', | |
1342 | description => "Specify external http proxy which is used for downloads (example: 'http://username:password\@host:port/')", | |
1343 | pattern => "http://.*", | |
1344 | }, | |
a9323ef0 SP |
1345 | migration_unsecure => { |
1346 | optional => 1, | |
1347 | type => 'boolean', | |
bba12ad7 TL |
1348 | description => "Migration is secure using SSH tunnel by default. " . |
1349 | "For secure private networks you can disable it to speed up " . | |
1350 | "migration. Deprecated, use the 'migration' property instead!", | |
1351 | }, | |
1352 | migration => { | |
1353 | optional => 1, | |
1354 | type => 'string', format => $migration_format, | |
1355 | description => "For cluster wide migration settings.", | |
a9323ef0 | 1356 | }, |
dce47328 DM |
1357 | console => { |
1358 | optional => 1, | |
1359 | type => 'string', | |
66a15f27 DM |
1360 | description => "Select the default Console viewer. You can either use the builtin java applet (VNC), an external virt-viewer comtatible application (SPICE), or an HTML5 based viewer (noVNC).", |
1361 | enum => ['applet', 'vv', 'html5'], | |
dce47328 | 1362 | }, |
8548bd87 SGE |
1363 | email_from => { |
1364 | optional => 1, | |
1365 | type => 'string', | |
a05baf53 | 1366 | format => 'email-opt', |
8548bd87 SGE |
1367 | description => "Specify email address to send notification from (default is root@\$hostname)", |
1368 | }, | |
66c2b1e9 TL |
1369 | max_workers => { |
1370 | optional => 1, | |
1371 | type => 'integer', | |
1372 | minimum => 1, | |
1373 | description => "Defines how many workers (per node) are maximal started ". | |
1374 | " on actions like 'stopall VMs' or task from the ha-manager.", | |
1375 | }, | |
8d762bd6 TL |
1376 | fencing => { |
1377 | optional => 1, | |
1378 | type => 'string', | |
1379 | default => 'watchdog', | |
1380 | enum => [ 'watchdog', 'hardware', 'both' ], | |
1381 | description => "Set the fencing mode of the HA cluster. Hardware mode " . | |
1382 | "needs a valid configuration of fence devices in /etc/pve/ha/fence.cfg." . | |
bd0c003a FG |
1383 | " With both all two modes are used." . |
1384 | "\n\nWARNING: 'hardware' and 'both' are EXPERIMENTAL & WIP", | |
8d762bd6 | 1385 | }, |
329da63d WB |
1386 | mac_prefix => { |
1387 | optional => 1, | |
1388 | type => 'string', | |
1389 | pattern => qr/[a-f0-9]{2}(?::[a-f0-9]{2}){0,2}:?/i, | |
1390 | description => 'Prefix for autogenerated MAC addresses.', | |
1391 | }, | |
fe000966 DM |
1392 | }, |
1393 | }; | |
1394 | ||
1395 | # make schema accessible from outside (for documentation) | |
1396 | sub get_datacenter_schema { return $datacenter_schema }; | |
1397 | ||
1398 | sub parse_datacenter_config { | |
1399 | my ($filename, $raw) = @_; | |
1400 | ||
bba12ad7 TL |
1401 | my $res = PVE::JSONSchema::parse_config($datacenter_schema, $filename, $raw // ''); |
1402 | ||
1403 | if (my $migration = $res->{migration}) { | |
1404 | $res->{migration} = PVE::JSONSchema::parse_property_string($migration_format, $migration); | |
1405 | } | |
1406 | ||
1407 | # for backwards compatibility only, new migration property has precedence | |
1408 | if (defined($res->{migration_unsecure})) { | |
1409 | if (defined($res->{migration}->{type})) { | |
1410 | warn "deprecated setting 'migration_unsecure' and new 'migration: type' " . | |
1411 | "set at same time! Ignore 'migration_unsecure'\n"; | |
1412 | } else { | |
1413 | $res->{migration}->{type} = ($res->{migration_unsecure}) ? 'insecure' : 'secure'; | |
1414 | } | |
1415 | } | |
1416 | ||
1417 | return $res; | |
fe000966 DM |
1418 | } |
1419 | ||
1420 | sub write_datacenter_config { | |
1421 | my ($filename, $cfg) = @_; | |
bba12ad7 TL |
1422 | |
1423 | # map deprecated setting to new one | |
8f706517 | 1424 | if (defined($cfg->{migration_unsecure}) && !defined($cfg->{migration})) { |
bba12ad7 TL |
1425 | my $migration_unsecure = delete $cfg->{migration_unsecure}; |
1426 | $cfg->{migration}->{type} = ($migration_unsecure) ? 'insecure' : 'secure'; | |
1427 | } | |
1428 | ||
fe000966 DM |
1429 | return PVE::JSONSchema::dump_config($datacenter_schema, $filename, $cfg); |
1430 | } | |
1431 | ||
c53b111f DM |
1432 | cfs_register_file('datacenter.cfg', |
1433 | \&parse_datacenter_config, | |
fe000966 | 1434 | \&write_datacenter_config); |
ec48ec22 | 1435 | |
cafc7309 DM |
1436 | # a very simply parser ... |
1437 | sub parse_corosync_conf { | |
ec48ec22 DM |
1438 | my ($filename, $raw) = @_; |
1439 | ||
cafc7309 | 1440 | return {} if !$raw; |
ec48ec22 | 1441 | |
440121dc | 1442 | my $digest = Digest::SHA::sha1_hex(defined($raw) ? $raw : ''); |
ec48ec22 | 1443 | |
cafc7309 DM |
1444 | $raw =~ s/#.*$//mg; |
1445 | $raw =~ s/\r?\n/ /g; | |
1446 | $raw =~ s/\s+/ /g; | |
1447 | $raw =~ s/^\s+//; | |
1448 | $raw =~ s/\s*$//; | |
c53b111f | 1449 | |
cafc7309 | 1450 | my @tokens = split(/\s/, $raw); |
c53b111f | 1451 | |
cafc7309 | 1452 | my $conf = { section => 'main', children => [] }; |
1d01c3f6 | 1453 | |
cafc7309 DM |
1454 | my $stack = []; |
1455 | my $section = $conf; | |
c53b111f | 1456 | |
cafc7309 DM |
1457 | while (defined(my $token = shift @tokens)) { |
1458 | my $nexttok = $tokens[0]; | |
1459 | ||
1460 | if ($nexttok && ($nexttok eq '{')) { | |
1461 | shift @tokens; # skip '{' | |
1462 | my $new_section = { | |
1463 | section => $token, | |
1464 | children => [], | |
1465 | }; | |
1466 | push @{$section->{children}}, $new_section; | |
1467 | push @$stack, $section; | |
1468 | $section = $new_section; | |
1469 | next; | |
1d01c3f6 | 1470 | } |
1d01c3f6 | 1471 | |
cafc7309 DM |
1472 | if ($token eq '}') { |
1473 | $section = pop @$stack; | |
1474 | die "parse error - uncexpected '}'\n" if !$section; | |
1475 | next; | |
1476 | } | |
1d01c3f6 | 1477 | |
cafc7309 DM |
1478 | my $key = $token; |
1479 | die "missing ':' after key '$key'\n" if ! ($key =~ s/:$//); | |
c53b111f | 1480 | |
cafc7309 DM |
1481 | die "parse error - no value for '$key'\n" if !defined($nexttok); |
1482 | my $value = shift @tokens; | |
1d01c3f6 | 1483 | |
cafc7309 DM |
1484 | push @{$section->{children}}, { key => $key, value => $value }; |
1485 | } | |
1d01c3f6 | 1486 | |
cafc7309 | 1487 | $conf->{digest} = $digest; |
1d01c3f6 | 1488 | |
cafc7309 | 1489 | return $conf; |
1d01c3f6 DM |
1490 | } |
1491 | ||
cafc7309 DM |
1492 | my $dump_corosync_section; |
1493 | $dump_corosync_section = sub { | |
1494 | my ($section, $prefix) = @_; | |
1d01c3f6 | 1495 | |
cafc7309 | 1496 | my $raw = $prefix . $section->{section} . " {\n"; |
c53b111f | 1497 | |
cafc7309 DM |
1498 | my @list = grep { defined($_->{key}) } @{$section->{children}}; |
1499 | foreach my $child (sort {$a->{key} cmp $b->{key}} @list) { | |
1500 | $raw .= $prefix . " $child->{key}: $child->{value}\n"; | |
1d01c3f6 | 1501 | } |
c53b111f | 1502 | |
cafc7309 DM |
1503 | @list = grep { defined($_->{section}) } @{$section->{children}}; |
1504 | foreach my $child (sort {$a->{section} cmp $b->{section}} @list) { | |
1505 | $raw .= &$dump_corosync_section($child, "$prefix "); | |
1d01c3f6 DM |
1506 | } |
1507 | ||
cafc7309 | 1508 | $raw .= $prefix . "}\n\n"; |
c53b111f | 1509 | |
cafc7309 | 1510 | return $raw; |
c53b111f | 1511 | |
cafc7309 | 1512 | }; |
1d01c3f6 | 1513 | |
cafc7309 DM |
1514 | sub write_corosync_conf { |
1515 | my ($filename, $conf) = @_; | |
ec48ec22 | 1516 | |
cafc7309 | 1517 | my $raw = ''; |
ec48ec22 | 1518 | |
cafc7309 | 1519 | my $prefix = ''; |
c53b111f | 1520 | |
cafc7309 | 1521 | die "no main section" if $conf->{section} ne 'main'; |
ec48ec22 | 1522 | |
cafc7309 DM |
1523 | my @list = grep { defined($_->{key}) } @{$conf->{children}}; |
1524 | foreach my $child (sort {$a->{key} cmp $b->{key}} @list) { | |
1525 | $raw .= "$child->{key}: $child->{value}\n"; | |
1d01c3f6 DM |
1526 | } |
1527 | ||
cafc7309 DM |
1528 | @list = grep { defined($_->{section}) } @{$conf->{children}}; |
1529 | foreach my $child (sort {$a->{section} cmp $b->{section}} @list) { | |
1530 | $raw .= &$dump_corosync_section($child, $prefix); | |
1531 | } | |
ec48ec22 | 1532 | |
cafc7309 | 1533 | return $raw; |
ec48ec22 DM |
1534 | } |
1535 | ||
cafc7309 DM |
1536 | sub corosync_conf_version { |
1537 | my ($conf, $noerr, $new_value) = @_; | |
1538 | ||
1539 | foreach my $child (@{$conf->{children}}) { | |
1540 | next if !defined($child->{section}); | |
1541 | if ($child->{section} eq 'totem') { | |
1542 | foreach my $e (@{$child->{children}}) { | |
1543 | next if !defined($e->{key}); | |
1544 | if ($e->{key} eq 'config_version') { | |
1545 | if ($new_value) { | |
1546 | $e->{value} = $new_value; | |
1547 | return $new_value; | |
1548 | } elsif (my $version = int($e->{value})) { | |
1549 | return $version; | |
1550 | } | |
1551 | last; | |
1552 | } | |
1553 | } | |
ec48ec22 | 1554 | } |
ec48ec22 | 1555 | } |
c53b111f | 1556 | |
cafc7309 | 1557 | return undef if $noerr; |
ec48ec22 | 1558 | |
cafc7309 | 1559 | die "invalid corosync config - unable to read version\n"; |
ec48ec22 DM |
1560 | } |
1561 | ||
cafc7309 DM |
1562 | # read only - use "rename corosync.conf.new corosync.conf" to write |
1563 | PVE::Cluster::cfs_register_file('corosync.conf', \&parse_corosync_conf); | |
ec48ec22 | 1564 | # this is read/write |
c53b111f | 1565 | PVE::Cluster::cfs_register_file('corosync.conf.new', \&parse_corosync_conf, |
cafc7309 | 1566 | \&write_corosync_conf); |
2c66fb58 | 1567 | |
eb51b829 FG |
1568 | sub check_corosync_conf_exists { |
1569 | my ($silent) = @_; | |
1570 | ||
1571 | $silent = $silent // 0; | |
1572 | ||
1573 | my $exists = -f "$basedir/corosync.conf"; | |
1574 | ||
1575 | warn "Corosync config '$basedir/corosync.conf' does not exist - is this node part of a cluster?\n" | |
c50900f2 | 1576 | if !$silent && !$exists; |
eb51b829 FG |
1577 | |
1578 | return $exists; | |
1579 | } | |
1580 | ||
4ed5e654 DM |
1581 | sub corosync_update_nodelist { |
1582 | my ($conf, $nodelist) = @_; | |
1583 | ||
1584 | delete $conf->{digest}; | |
1585 | ||
1586 | my $version = corosync_conf_version($conf); | |
1587 | corosync_conf_version($conf, undef, $version + 1); | |
1588 | ||
1589 | my $children = []; | |
1590 | foreach my $v (values %$nodelist) { | |
1591 | next if !($v->{ring0_addr} || $v->{name}); | |
1592 | my $kv = []; | |
1593 | foreach my $k (keys %$v) { | |
1594 | push @$kv, { key => $k, value => $v->{$k} }; | |
1595 | } | |
1596 | my $ns = { section => 'node', children => $kv }; | |
1597 | push @$children, $ns; | |
1598 | } | |
1599 | ||
1600 | foreach my $main (@{$conf->{children}}) { | |
1601 | next if !defined($main->{section}); | |
1602 | if ($main->{section} eq 'nodelist') { | |
1603 | $main->{children} = $children; | |
1604 | last; | |
1605 | } | |
1606 | } | |
1607 | ||
1608 | ||
1609 | cfs_write_file("corosync.conf.new", $conf); | |
1610 | ||
1611 | rename("/etc/pve/corosync.conf.new", "/etc/pve/corosync.conf") | |
1612 | || die "activate corosync.conf.new failed - $!\n"; | |
1613 | } | |
1614 | ||
1615 | sub corosync_nodelist { | |
1616 | my ($conf) = @_; | |
1617 | ||
1618 | my $nodelist = {}; | |
1619 | ||
1620 | foreach my $main (@{$conf->{children}}) { | |
1621 | next if !defined($main->{section}); | |
1622 | if ($main->{section} eq 'nodelist') { | |
1623 | foreach my $ne (@{$main->{children}}) { | |
1624 | next if !defined($ne->{section}) || ($ne->{section} ne 'node'); | |
1625 | my $node = { quorum_votes => 1 }; | |
1626 | my $name; | |
1627 | foreach my $child (@{$ne->{children}}) { | |
1628 | next if !defined($child->{key}); | |
1629 | $node->{$child->{key}} = $child->{value}; | |
1630 | # use 'name' over 'ring0_addr' if set | |
1631 | if ($child->{key} eq 'name') { | |
1632 | delete $nodelist->{$name} if $name; | |
1633 | $name = $child->{value}; | |
1634 | $nodelist->{$name} = $node; | |
1635 | } elsif(!$name && $child->{key} eq 'ring0_addr') { | |
1636 | $name = $child->{value}; | |
1637 | $nodelist->{$name} = $node; | |
1638 | } | |
1639 | } | |
1640 | } | |
1641 | } | |
1642 | } | |
1643 | ||
1644 | return $nodelist; | |
1645 | } | |
1646 | ||
1647 | # get a hash representation of the corosync config totem section | |
1648 | sub corosync_totem_config { | |
1649 | my ($conf) = @_; | |
1650 | ||
1651 | my $res = {}; | |
1652 | ||
1653 | foreach my $main (@{$conf->{children}}) { | |
1654 | next if !defined($main->{section}) || | |
1655 | $main->{section} ne 'totem'; | |
1656 | ||
1657 | foreach my $e (@{$main->{children}}) { | |
1658 | ||
1659 | if ($e->{section} && $e->{section} eq 'interface') { | |
1660 | my $entry = {}; | |
1661 | ||
1662 | $res->{interface} = {}; | |
1663 | ||
1664 | foreach my $child (@{$e->{children}}) { | |
1665 | next if !defined($child->{key}); | |
1666 | $entry->{$child->{key}} = $child->{value}; | |
1667 | if($child->{key} eq 'ringnumber') { | |
1668 | $res->{interface}->{$child->{value}} = $entry; | |
1669 | } | |
1670 | } | |
1671 | ||
1672 | } elsif ($e->{key}) { | |
1673 | $res->{$e->{key}} = $e->{value}; | |
1674 | } | |
1675 | } | |
1676 | } | |
1677 | ||
1678 | return $res; | |
1679 | } | |
1680 | ||
26784563 DM |
1681 | # X509 Certificate cache helper |
1682 | ||
1683 | my $cert_cache_nodes = {}; | |
1684 | my $cert_cache_timestamp = time(); | |
1685 | my $cert_cache_fingerprints = {}; | |
1686 | ||
1687 | sub update_cert_cache { | |
1688 | my ($update_node, $clear) = @_; | |
1689 | ||
1690 | syslog('info', "Clearing outdated entries from certificate cache") | |
1691 | if $clear; | |
1692 | ||
1693 | $cert_cache_timestamp = time() if !defined($update_node); | |
1694 | ||
1695 | my $node_list = defined($update_node) ? | |
1696 | [ $update_node ] : [ keys %$cert_cache_nodes ]; | |
1697 | ||
1698 | foreach my $node (@$node_list) { | |
1699 | my $clear_old = sub { | |
1700 | if (my $old_fp = $cert_cache_nodes->{$node}) { | |
1701 | # distrust old fingerprint | |
1702 | delete $cert_cache_fingerprints->{$old_fp}; | |
1703 | # ensure reload on next proxied request | |
1704 | delete $cert_cache_nodes->{$node}; | |
1705 | } | |
1706 | }; | |
1707 | ||
1708 | my $cert_path = "/etc/pve/nodes/$node/pve-ssl.pem"; | |
1709 | my $custom_cert_path = "/etc/pve/nodes/$node/pveproxy-ssl.pem"; | |
1710 | ||
1711 | $cert_path = $custom_cert_path if -f $custom_cert_path; | |
1712 | ||
1713 | my $cert; | |
1714 | eval { | |
1715 | my $bio = Net::SSLeay::BIO_new_file($cert_path, 'r'); | |
1716 | $cert = Net::SSLeay::PEM_read_bio_X509($bio); | |
1717 | Net::SSLeay::BIO_free($bio); | |
1718 | }; | |
1719 | my $err = $@; | |
1720 | if ($err || !defined($cert)) { | |
1721 | &$clear_old() if $clear; | |
1722 | next; | |
1723 | } | |
1724 | ||
1725 | my $fp; | |
1726 | eval { | |
1727 | $fp = Net::SSLeay::X509_get_fingerprint($cert, 'sha256'); | |
1728 | }; | |
1729 | $err = $@; | |
1730 | if ($err || !defined($fp) || $fp eq '') { | |
1731 | &$clear_old() if $clear; | |
1732 | next; | |
1733 | } | |
1734 | ||
1735 | my $old_fp = $cert_cache_nodes->{$node}; | |
1736 | $cert_cache_fingerprints->{$fp} = 1; | |
1737 | $cert_cache_nodes->{$node} = $fp; | |
1738 | ||
1739 | if (defined($old_fp) && $fp ne $old_fp) { | |
1740 | delete $cert_cache_fingerprints->{$old_fp}; | |
1741 | } | |
1742 | } | |
1743 | } | |
1744 | ||
ab224148 DM |
1745 | # load and cache cert fingerprint once |
1746 | sub initialize_cert_cache { | |
1747 | my ($node) = @_; | |
1748 | ||
1749 | update_cert_cache($node) | |
1750 | if defined($node) && !defined($cert_cache_nodes->{$node}); | |
1751 | } | |
1752 | ||
26784563 DM |
1753 | sub check_cert_fingerprint { |
1754 | my ($cert) = @_; | |
1755 | ||
1756 | # clear cache every 30 minutes at least | |
1757 | update_cert_cache(undef, 1) if time() - $cert_cache_timestamp >= 60*30; | |
1758 | ||
1759 | # get fingerprint of server certificate | |
1760 | my $fp; | |
1761 | eval { | |
1762 | $fp = Net::SSLeay::X509_get_fingerprint($cert, 'sha256'); | |
1763 | }; | |
1764 | return 0 if $@ || !defined($fp) || $fp eq ''; # error | |
1765 | ||
1766 | my $check = sub { | |
1767 | for my $expected (keys %$cert_cache_fingerprints) { | |
1768 | return 1 if $fp eq $expected; | |
1769 | } | |
1770 | return 0; | |
1771 | }; | |
1772 | ||
1773 | return 1 if &$check(); | |
1774 | ||
1775 | # clear cache and retry at most once every minute | |
1776 | if (time() - $cert_cache_timestamp >= 60) { | |
1777 | syslog ('info', "Could not verify remote node certificate '$fp' with list of pinned certificates, refreshing cache"); | |
1778 | update_cert_cache(); | |
1779 | return &$check(); | |
1780 | } | |
1781 | ||
1782 | return 0; | |
1783 | } | |
1784 | ||
15df58e6 DM |
1785 | # bash completion helpers |
1786 | ||
1787 | sub complete_next_vmid { | |
1788 | ||
1789 | my $vmlist = get_vmlist() || {}; | |
1790 | my $idlist = $vmlist->{ids} || {}; | |
1791 | ||
1792 | for (my $i = 100; $i < 10000; $i++) { | |
1793 | return [$i] if !defined($idlist->{$i}); | |
1794 | } | |
1795 | ||
1796 | return []; | |
1797 | } | |
1798 | ||
87515b25 DM |
1799 | sub complete_vmid { |
1800 | ||
1801 | my $vmlist = get_vmlist(); | |
1802 | my $ids = $vmlist->{ids} || {}; | |
1803 | ||
1804 | return [ keys %$ids ]; | |
1805 | } | |
1806 | ||
15df58e6 DM |
1807 | sub complete_local_vmid { |
1808 | ||
1809 | my $vmlist = get_vmlist(); | |
1810 | my $ids = $vmlist->{ids} || {}; | |
1811 | ||
1812 | my $nodename = PVE::INotify::nodename(); | |
1813 | ||
1814 | my $res = []; | |
1815 | foreach my $vmid (keys %$ids) { | |
1816 | my $d = $ids->{$vmid}; | |
1817 | next if !$d->{node} || $d->{node} ne $nodename; | |
1818 | push @$res, $vmid; | |
1819 | } | |
1820 | ||
1821 | return $res; | |
1822 | } | |
1823 | ||
4dd189df DM |
1824 | sub complete_migration_target { |
1825 | ||
1826 | my $res = []; | |
1827 | ||
1828 | my $nodename = PVE::INotify::nodename(); | |
1829 | ||
1830 | my $nodelist = get_nodelist(); | |
1831 | foreach my $node (@$nodelist) { | |
1832 | next if $node eq $nodename; | |
1833 | push @$res, $node; | |
1834 | } | |
1835 | ||
1836 | return $res; | |
1837 | } | |
1838 | ||
aabeedfb WB |
1839 | sub get_ssh_info { |
1840 | my ($node, $network_cidr) = @_; | |
1841 | ||
1842 | my $ip; | |
1843 | if (defined($network_cidr)) { | |
1844 | # Use mtunnel via to get the remote node's ip inside $network_cidr. | |
1845 | # This goes over the regular network (iow. uses get_ssh_info() with | |
1846 | # $network_cidr undefined. | |
1847 | # FIXME: Use the REST API client for this after creating an API entry | |
1848 | # for get_migration_ip. | |
1849 | my $default_remote = get_ssh_info($node, undef); | |
1850 | my $default_ssh = ssh_info_to_command($default_remote); | |
1851 | my $cmd =[@$default_ssh, 'pvecm', 'mtunnel', | |
1852 | '-migration_network', $network_cidr, | |
1853 | '-get_migration_ip' | |
1854 | ]; | |
1855 | PVE::Tools::run_command($cmd, outfunc => sub { | |
1856 | my ($line) = @_; | |
1857 | chomp $line; | |
1858 | die "internal error: unexpected output from mtunnel\n" | |
1859 | if defined($ip); | |
1860 | if ($line =~ /^ip: '(.*)'$/) { | |
1861 | $ip = $1; | |
1862 | } else { | |
1863 | die "internal error: bad output from mtunnel\n" | |
1864 | if defined($ip); | |
1865 | } | |
1866 | }); | |
1867 | die "failed to get ip for node '$node' in network '$network_cidr'\n" | |
1868 | if !defined($ip); | |
1869 | } else { | |
1870 | $ip = remote_node_ip($node); | |
1871 | } | |
1872 | ||
1873 | return { | |
1874 | ip => $ip, | |
d7d4c5b8 WB |
1875 | name => $node, |
1876 | network => $network_cidr, | |
aabeedfb WB |
1877 | }; |
1878 | } | |
1879 | ||
1f1aef8b | 1880 | sub ssh_info_to_command_base { |
aabeedfb WB |
1881 | my ($info, @extra_options) = @_; |
1882 | return [ | |
1883 | '/usr/bin/ssh', | |
1884 | '-o', 'BatchMode=yes', | |
1885 | '-o', 'HostKeyAlias='.$info->{name}, | |
1f1aef8b | 1886 | @extra_options |
aabeedfb WB |
1887 | ]; |
1888 | } | |
1889 | ||
1f1aef8b WB |
1890 | sub ssh_info_to_command { |
1891 | my ($info, @extra_options) = @_; | |
1892 | my $cmd = ssh_info_to_command_base($info, @extra_options); | |
1893 | push @$cmd, "root\@$info->{ip}"; | |
1894 | return $cmd; | |
1895 | } | |
1896 | ||
ac68281b | 1897 | 1; |