]>
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; | |
ec48ec22 | 11 | use XML::Parser; |
440121dc | 12 | use Digest::SHA; |
fe000966 DM |
13 | use Digest::HMAC_SHA1; |
14 | use PVE::Tools; | |
15 | use PVE::INotify; | |
16 | use PVE::IPCC; | |
17 | use PVE::SafeSyslog; | |
d0ad18e8 | 18 | use PVE::JSONSchema; |
fe000966 DM |
19 | use JSON; |
20 | use RRDs; | |
21 | use Encode; | |
22 | use base 'Exporter'; | |
23 | ||
24 | our @EXPORT_OK = qw( | |
25 | cfs_read_file | |
26 | cfs_write_file | |
27 | cfs_register_file | |
28 | cfs_lock_file); | |
29 | ||
30 | use Data::Dumper; # fixme: remove | |
31 | ||
32 | # x509 certificate utils | |
33 | ||
34 | my $basedir = "/etc/pve"; | |
35 | my $authdir = "$basedir/priv"; | |
36 | my $lockdir = "/etc/pve/priv/lock"; | |
37 | ||
38 | my $authprivkeyfn = "$authdir/authkey.key"; | |
39 | my $authpubkeyfn = "$basedir/authkey.pub"; | |
40 | my $pveca_key_fn = "$authdir/pve-root-ca.key"; | |
41 | my $pveca_srl_fn = "$authdir/pve-root-ca.srl"; | |
42 | my $pveca_cert_fn = "$basedir/pve-root-ca.pem"; | |
43 | # this is just a secret accessable by the web browser | |
44 | # and is used for CSRF prevention | |
45 | my $pvewww_key_fn = "$basedir/pve-www.key"; | |
46 | ||
47 | # ssh related files | |
48 | my $ssh_rsa_id_priv = "/root/.ssh/id_rsa"; | |
49 | my $ssh_rsa_id = "/root/.ssh/id_rsa.pub"; | |
50 | my $ssh_host_rsa_id = "/etc/ssh/ssh_host_rsa_key.pub"; | |
51 | my $sshglobalknownhosts = "/etc/ssh/ssh_known_hosts"; | |
52 | my $sshknownhosts = "/etc/pve/priv/known_hosts"; | |
53 | my $sshauthkeys = "/etc/pve/priv/authorized_keys"; | |
ac50b36d | 54 | my $sshd_config_fn = "/etc/ssh/sshd_config"; |
fe000966 | 55 | my $rootsshauthkeys = "/root/.ssh/authorized_keys"; |
6056578e | 56 | my $rootsshauthkeysbackup = "${rootsshauthkeys}.org"; |
f666cdde | 57 | my $rootsshconfig = "/root/.ssh/config"; |
fe000966 DM |
58 | |
59 | my $observed = { | |
e1735a61 | 60 | 'vzdump.cron' => 1, |
fe000966 DM |
61 | 'storage.cfg' => 1, |
62 | 'datacenter.cfg' => 1, | |
cafc7309 DM |
63 | 'corosync.conf' => 1, |
64 | 'corosync.conf.new' => 1, | |
fe000966 DM |
65 | 'user.cfg' => 1, |
66 | 'domains.cfg' => 1, | |
67 | 'priv/shadow.cfg' => 1, | |
68 | '/qemu-server/' => 1, | |
f71eee41 | 69 | '/openvz/' => 1, |
7f66b436 | 70 | '/lxc/' => 1, |
5a5417e6 DM |
71 | 'ha/crm_commands' => 1, |
72 | 'ha/manager_status' => 1, | |
73 | 'ha/resources.cfg' => 1, | |
74 | 'ha/groups.cfg' => 1, | |
e9af3eb7 | 75 | 'ha/fence.cfg' => 1, |
9d4f69ff | 76 | 'status.cfg' => 1, |
fe000966 DM |
77 | }; |
78 | ||
79 | # only write output if something fails | |
80 | sub run_silent_cmd { | |
81 | my ($cmd) = @_; | |
82 | ||
83 | my $outbuf = ''; | |
84 | ||
85 | my $record_output = sub { | |
86 | $outbuf .= shift; | |
87 | $outbuf .= "\n"; | |
88 | }; | |
89 | ||
90 | eval { | |
91 | PVE::Tools::run_command($cmd, outfunc => $record_output, | |
92 | errfunc => $record_output); | |
93 | }; | |
94 | ||
95 | my $err = $@; | |
96 | ||
97 | if ($err) { | |
98 | print STDERR $outbuf; | |
99 | die $err; | |
100 | } | |
101 | } | |
102 | ||
103 | sub check_cfs_quorum { | |
01dddfb9 DM |
104 | my ($noerr) = @_; |
105 | ||
fe000966 DM |
106 | # note: -w filename always return 1 for root, so wee need |
107 | # to use File::lstat here | |
108 | my $st = File::stat::lstat("$basedir/local"); | |
01dddfb9 DM |
109 | my $quorate = ($st && (($st->mode & 0200) != 0)); |
110 | ||
111 | die "cluster not ready - no quorum?\n" if !$quorate && !$noerr; | |
112 | ||
113 | return $quorate; | |
fe000966 DM |
114 | } |
115 | ||
116 | sub check_cfs_is_mounted { | |
117 | my ($noerr) = @_; | |
118 | ||
119 | my $res = -l "$basedir/local"; | |
120 | ||
121 | die "pve configuration filesystem not mounted\n" | |
122 | if !$res && !$noerr; | |
123 | ||
124 | return $res; | |
125 | } | |
126 | ||
127 | sub gen_local_dirs { | |
128 | my ($nodename) = @_; | |
129 | ||
130 | check_cfs_is_mounted(); | |
131 | ||
132 | my @required_dirs = ( | |
133 | "$basedir/priv", | |
134 | "$basedir/nodes", | |
135 | "$basedir/nodes/$nodename", | |
7f66b436 | 136 | "$basedir/nodes/$nodename/lxc", |
a1c08cfa DM |
137 | "$basedir/nodes/$nodename/qemu-server", |
138 | "$basedir/nodes/$nodename/openvz", | |
fe000966 DM |
139 | "$basedir/nodes/$nodename/priv"); |
140 | ||
141 | foreach my $dir (@required_dirs) { | |
142 | if (! -d $dir) { | |
62613060 | 143 | mkdir($dir) || $! == EEXIST || die "unable to create directory '$dir' - $!\n"; |
fe000966 DM |
144 | } |
145 | } | |
146 | } | |
147 | ||
148 | sub gen_auth_key { | |
149 | ||
150 | return if -f "$authprivkeyfn"; | |
151 | ||
152 | check_cfs_is_mounted(); | |
153 | ||
62613060 | 154 | mkdir $authdir || $! == EEXIST || die "unable to create dir '$authdir' - $!\n"; |
fe000966 DM |
155 | |
156 | my $cmd = "openssl genrsa -out '$authprivkeyfn' 2048"; | |
157 | run_silent_cmd($cmd); | |
158 | ||
159 | $cmd = "openssl rsa -in '$authprivkeyfn' -pubout -out '$authpubkeyfn'"; | |
160 | run_silent_cmd($cmd) | |
161 | } | |
162 | ||
163 | sub gen_pveca_key { | |
164 | ||
165 | return if -f $pveca_key_fn; | |
166 | ||
167 | eval { | |
147661a8 | 168 | run_silent_cmd(['openssl', 'genrsa', '-out', $pveca_key_fn, '4096']); |
fe000966 DM |
169 | }; |
170 | ||
171 | die "unable to generate pve ca key:\n$@" if $@; | |
172 | } | |
173 | ||
174 | sub gen_pveca_cert { | |
175 | ||
176 | if (-f $pveca_key_fn && -f $pveca_cert_fn) { | |
177 | return 0; | |
178 | } | |
179 | ||
180 | gen_pveca_key(); | |
181 | ||
182 | # we try to generate an unique 'subject' to avoid browser problems | |
183 | # (reused serial numbers, ..) | |
184 | my $nid = (split (/\s/, `md5sum '$pveca_key_fn'`))[0] || time(); | |
185 | ||
186 | eval { | |
187 | run_silent_cmd(['openssl', 'req', '-batch', '-days', '3650', '-new', | |
188 | '-x509', '-nodes', '-key', | |
189 | $pveca_key_fn, '-out', $pveca_cert_fn, '-subj', | |
190 | "/CN=Proxmox Virtual Environment/OU=$nid/O=PVE Cluster Manager CA/"]); | |
191 | }; | |
192 | ||
193 | die "generating pve root certificate failed:\n$@" if $@; | |
194 | ||
195 | return 1; | |
196 | } | |
197 | ||
198 | sub gen_pve_ssl_key { | |
199 | my ($nodename) = @_; | |
200 | ||
201 | die "no node name specified" if !$nodename; | |
202 | ||
203 | my $pvessl_key_fn = "$basedir/nodes/$nodename/pve-ssl.key"; | |
204 | ||
205 | return if -f $pvessl_key_fn; | |
206 | ||
207 | eval { | |
208 | run_silent_cmd(['openssl', 'genrsa', '-out', $pvessl_key_fn, '2048']); | |
209 | }; | |
210 | ||
211 | die "unable to generate pve ssl key for node '$nodename':\n$@" if $@; | |
212 | } | |
213 | ||
214 | sub gen_pve_www_key { | |
215 | ||
216 | return if -f $pvewww_key_fn; | |
217 | ||
218 | eval { | |
219 | run_silent_cmd(['openssl', 'genrsa', '-out', $pvewww_key_fn, '2048']); | |
220 | }; | |
221 | ||
222 | die "unable to generate pve www key:\n$@" if $@; | |
223 | } | |
224 | ||
225 | sub update_serial { | |
226 | my ($serial) = @_; | |
227 | ||
228 | PVE::Tools::file_set_contents($pveca_srl_fn, $serial); | |
229 | } | |
230 | ||
231 | sub gen_pve_ssl_cert { | |
232 | my ($force, $nodename, $ip) = @_; | |
233 | ||
234 | die "no node name specified" if !$nodename; | |
235 | die "no IP specified" if !$ip; | |
236 | ||
237 | my $pvessl_cert_fn = "$basedir/nodes/$nodename/pve-ssl.pem"; | |
238 | ||
239 | return if !$force && -f $pvessl_cert_fn; | |
240 | ||
8acde170 | 241 | my $names = "IP:127.0.0.1,IP:::1,DNS:localhost"; |
fe000966 DM |
242 | |
243 | my $rc = PVE::INotify::read_file('resolvconf'); | |
244 | ||
245 | $names .= ",IP:$ip"; | |
246 | ||
247 | my $fqdn = $nodename; | |
248 | ||
249 | $names .= ",DNS:$nodename"; | |
250 | ||
251 | if ($rc && $rc->{search}) { | |
252 | $fqdn = $nodename . "." . $rc->{search}; | |
253 | $names .= ",DNS:$fqdn"; | |
254 | } | |
255 | ||
256 | my $sslconf = <<__EOD; | |
257 | RANDFILE = /root/.rnd | |
258 | extensions = v3_req | |
259 | ||
260 | [ req ] | |
261 | default_bits = 2048 | |
262 | distinguished_name = req_distinguished_name | |
263 | req_extensions = v3_req | |
264 | prompt = no | |
265 | string_mask = nombstr | |
266 | ||
267 | [ req_distinguished_name ] | |
268 | organizationalUnitName = PVE Cluster Node | |
269 | organizationName = Proxmox Virtual Environment | |
270 | commonName = $fqdn | |
271 | ||
272 | [ v3_req ] | |
273 | basicConstraints = CA:FALSE | |
274 | nsCertType = server | |
275 | keyUsage = nonRepudiation, digitalSignature, keyEncipherment | |
276 | subjectAltName = $names | |
277 | __EOD | |
278 | ||
279 | my $cfgfn = "/tmp/pvesslconf-$$.tmp"; | |
280 | my $fh = IO::File->new ($cfgfn, "w"); | |
281 | print $fh $sslconf; | |
282 | close ($fh); | |
283 | ||
284 | my $reqfn = "/tmp/pvecertreq-$$.tmp"; | |
285 | unlink $reqfn; | |
286 | ||
287 | my $pvessl_key_fn = "$basedir/nodes/$nodename/pve-ssl.key"; | |
288 | eval { | |
289 | run_silent_cmd(['openssl', 'req', '-batch', '-new', '-config', $cfgfn, | |
290 | '-key', $pvessl_key_fn, '-out', $reqfn]); | |
291 | }; | |
292 | ||
293 | if (my $err = $@) { | |
294 | unlink $reqfn; | |
295 | unlink $cfgfn; | |
296 | die "unable to generate pve certificate request:\n$err"; | |
297 | } | |
298 | ||
299 | update_serial("0000000000000000") if ! -f $pveca_srl_fn; | |
300 | ||
301 | eval { | |
302 | run_silent_cmd(['openssl', 'x509', '-req', '-in', $reqfn, '-days', '3650', | |
303 | '-out', $pvessl_cert_fn, '-CAkey', $pveca_key_fn, | |
304 | '-CA', $pveca_cert_fn, '-CAserial', $pveca_srl_fn, | |
305 | '-extfile', $cfgfn]); | |
306 | }; | |
307 | ||
308 | if (my $err = $@) { | |
309 | unlink $reqfn; | |
310 | unlink $cfgfn; | |
311 | die "unable to generate pve ssl certificate:\n$err"; | |
312 | } | |
313 | ||
314 | unlink $cfgfn; | |
315 | unlink $reqfn; | |
316 | } | |
317 | ||
318 | sub gen_pve_node_files { | |
319 | my ($nodename, $ip, $opt_force) = @_; | |
320 | ||
321 | gen_local_dirs($nodename); | |
322 | ||
323 | gen_auth_key(); | |
324 | ||
325 | # make sure we have a (cluster wide) secret | |
326 | # for CSRFR prevention | |
327 | gen_pve_www_key(); | |
328 | ||
329 | # make sure we have a (per node) private key | |
330 | gen_pve_ssl_key($nodename); | |
331 | ||
332 | # make sure we have a CA | |
333 | my $force = gen_pveca_cert(); | |
334 | ||
335 | $force = 1 if $opt_force; | |
336 | ||
337 | gen_pve_ssl_cert($force, $nodename, $ip); | |
338 | } | |
339 | ||
bd0ae7ff DM |
340 | my $vzdump_cron_dummy = <<__EOD; |
341 | # cluster wide vzdump cron schedule | |
342 | # Atomatically generated file - do not edit | |
343 | ||
344 | PATH="/usr/sbin:/usr/bin:/sbin:/bin" | |
345 | ||
346 | __EOD | |
347 | ||
348 | sub gen_pve_vzdump_symlink { | |
349 | ||
e1735a61 | 350 | my $filename = "/etc/pve/vzdump.cron"; |
bd0ae7ff DM |
351 | |
352 | my $link_fn = "/etc/cron.d/vzdump"; | |
353 | ||
354 | if ((-f $filename) && (! -l $link_fn)) { | |
355 | rename($link_fn, "/root/etc_cron_vzdump.org"); # make backup if file exists | |
356 | symlink($filename, $link_fn); | |
357 | } | |
358 | } | |
359 | ||
360 | sub gen_pve_vzdump_files { | |
361 | ||
e1735a61 | 362 | my $filename = "/etc/pve/vzdump.cron"; |
bd0ae7ff DM |
363 | |
364 | PVE::Tools::file_set_contents($filename, $vzdump_cron_dummy) | |
365 | if ! -f $filename; | |
366 | ||
367 | gen_pve_vzdump_symlink(); | |
368 | }; | |
369 | ||
fe000966 DM |
370 | my $versions = {}; |
371 | my $vmlist = {}; | |
372 | my $clinfo = {}; | |
373 | ||
374 | my $ipcc_send_rec = sub { | |
375 | my ($msgid, $data) = @_; | |
376 | ||
377 | my $res = PVE::IPCC::ipcc_send_rec($msgid, $data); | |
378 | ||
379 | die "ipcc_send_rec failed: $!\n" if !defined($res) && ($! != 0); | |
380 | ||
381 | return $res; | |
382 | }; | |
383 | ||
384 | my $ipcc_send_rec_json = sub { | |
385 | my ($msgid, $data) = @_; | |
386 | ||
387 | my $res = PVE::IPCC::ipcc_send_rec($msgid, $data); | |
388 | ||
389 | die "ipcc_send_rec failed: $!\n" if !defined($res) && ($! != 0); | |
390 | ||
391 | return decode_json($res); | |
392 | }; | |
393 | ||
394 | my $ipcc_get_config = sub { | |
395 | my ($path) = @_; | |
396 | ||
397 | my $bindata = pack "Z*", $path; | |
2db32d95 DM |
398 | my $res = PVE::IPCC::ipcc_send_rec(6, $bindata); |
399 | if (!defined($res)) { | |
400 | return undef if ($! != 0); | |
401 | return ''; | |
402 | } | |
403 | ||
404 | return $res; | |
fe000966 DM |
405 | }; |
406 | ||
407 | my $ipcc_get_status = sub { | |
408 | my ($name, $nodename) = @_; | |
409 | ||
410 | my $bindata = pack "Z[256]Z[256]", $name, ($nodename || ""); | |
411 | return PVE::IPCC::ipcc_send_rec(5, $bindata); | |
412 | }; | |
413 | ||
414 | my $ipcc_update_status = sub { | |
415 | my ($name, $data) = @_; | |
416 | ||
417 | my $raw = ref($data) ? encode_json($data) : $data; | |
418 | # update status | |
419 | my $bindata = pack "Z[256]Z*", $name, $raw; | |
420 | ||
421 | return &$ipcc_send_rec(4, $bindata); | |
422 | }; | |
423 | ||
424 | my $ipcc_log = sub { | |
425 | my ($priority, $ident, $tag, $msg) = @_; | |
426 | ||
427 | my $bindata = pack "CCCZ*Z*Z*", $priority, bytes::length($ident) + 1, | |
428 | bytes::length($tag) + 1, $ident, $tag, $msg; | |
429 | ||
430 | return &$ipcc_send_rec(7, $bindata); | |
431 | }; | |
432 | ||
433 | my $ipcc_get_cluster_log = sub { | |
434 | my ($user, $max) = @_; | |
435 | ||
436 | $max = 0 if !defined($max); | |
437 | ||
438 | my $bindata = pack "VVVVZ*", $max, 0, 0, 0, ($user || ""); | |
439 | return &$ipcc_send_rec(8, $bindata); | |
440 | }; | |
441 | ||
442 | my $ccache = {}; | |
443 | ||
444 | sub cfs_update { | |
445 | eval { | |
446 | my $res = &$ipcc_send_rec_json(1); | |
447 | #warn "GOT1: " . Dumper($res); | |
448 | die "no starttime\n" if !$res->{starttime}; | |
449 | ||
450 | if (!$res->{starttime} || !$versions->{starttime} || | |
451 | $res->{starttime} != $versions->{starttime}) { | |
452 | #print "detected changed starttime\n"; | |
453 | $vmlist = {}; | |
454 | $clinfo = {}; | |
455 | $ccache = {}; | |
456 | } | |
457 | ||
458 | $versions = $res; | |
459 | }; | |
460 | my $err = $@; | |
461 | if ($err) { | |
462 | $versions = {}; | |
463 | $vmlist = {}; | |
464 | $clinfo = {}; | |
465 | $ccache = {}; | |
466 | warn $err; | |
467 | } | |
468 | ||
469 | eval { | |
470 | if (!$clinfo->{version} || $clinfo->{version} != $versions->{clinfo}) { | |
471 | #warn "detected new clinfo\n"; | |
472 | $clinfo = &$ipcc_send_rec_json(2); | |
473 | } | |
474 | }; | |
475 | $err = $@; | |
476 | if ($err) { | |
477 | $clinfo = {}; | |
478 | warn $err; | |
479 | } | |
480 | ||
481 | eval { | |
482 | if (!$vmlist->{version} || $vmlist->{version} != $versions->{vmlist}) { | |
483 | #warn "detected new vmlist1\n"; | |
484 | $vmlist = &$ipcc_send_rec_json(3); | |
485 | } | |
486 | }; | |
487 | $err = $@; | |
488 | if ($err) { | |
489 | $vmlist = {}; | |
490 | warn $err; | |
491 | } | |
492 | } | |
493 | ||
494 | sub get_vmlist { | |
495 | return $vmlist; | |
496 | } | |
497 | ||
498 | sub get_clinfo { | |
499 | return $clinfo; | |
500 | } | |
501 | ||
9ddd4ae9 DM |
502 | sub get_members { |
503 | return $clinfo->{nodelist}; | |
504 | } | |
505 | ||
fe000966 DM |
506 | sub get_nodelist { |
507 | ||
508 | my $nodelist = $clinfo->{nodelist}; | |
509 | ||
510 | my $result = []; | |
511 | ||
512 | my $nodename = PVE::INotify::nodename(); | |
513 | ||
514 | if (!$nodelist || !$nodelist->{$nodename}) { | |
515 | return [ $nodename ]; | |
516 | } | |
517 | ||
518 | return [ keys %$nodelist ]; | |
519 | } | |
520 | ||
521 | sub broadcast_tasklist { | |
522 | my ($data) = @_; | |
523 | ||
524 | eval { | |
525 | &$ipcc_update_status("tasklist", $data); | |
526 | }; | |
527 | ||
528 | warn $@ if $@; | |
529 | } | |
530 | ||
531 | my $tasklistcache = {}; | |
532 | ||
533 | sub get_tasklist { | |
534 | my ($nodename) = @_; | |
535 | ||
536 | my $kvstore = $versions->{kvstore} || {}; | |
537 | ||
538 | my $nodelist = get_nodelist(); | |
539 | ||
540 | my $res = []; | |
541 | foreach my $node (@$nodelist) { | |
542 | next if $nodename && ($nodename ne $node); | |
543 | eval { | |
544 | my $ver = $kvstore->{$node}->{tasklist} if $kvstore->{$node}; | |
545 | my $cd = $tasklistcache->{$node}; | |
cebe16ec DM |
546 | if (!$cd || !$ver || !$cd->{version} || |
547 | ($cd->{version} != $ver)) { | |
fe000966 DM |
548 | my $raw = &$ipcc_get_status("tasklist", $node) || '[]'; |
549 | my $data = decode_json($raw); | |
550 | push @$res, @$data; | |
551 | $cd = $tasklistcache->{$node} = { | |
552 | data => $data, | |
553 | version => $ver, | |
554 | }; | |
555 | } elsif ($cd && $cd->{data}) { | |
556 | push @$res, @{$cd->{data}}; | |
557 | } | |
558 | }; | |
559 | my $err = $@; | |
560 | syslog('err', $err) if $err; | |
561 | } | |
562 | ||
563 | return $res; | |
564 | } | |
565 | ||
566 | sub broadcast_rrd { | |
567 | my ($rrdid, $data) = @_; | |
568 | ||
569 | eval { | |
570 | &$ipcc_update_status("rrd/$rrdid", $data); | |
571 | }; | |
572 | my $err = $@; | |
573 | ||
574 | warn $err if $err; | |
575 | } | |
576 | ||
577 | my $last_rrd_dump = 0; | |
578 | my $last_rrd_data = ""; | |
579 | ||
580 | sub rrd_dump { | |
581 | ||
582 | my $ctime = time(); | |
583 | ||
584 | my $diff = $ctime - $last_rrd_dump; | |
585 | if ($diff < 2) { | |
586 | return $last_rrd_data; | |
587 | } | |
588 | ||
589 | my $raw; | |
590 | eval { | |
591 | $raw = &$ipcc_send_rec(10); | |
592 | }; | |
593 | my $err = $@; | |
594 | ||
595 | if ($err) { | |
596 | warn $err; | |
597 | return {}; | |
598 | } | |
599 | ||
600 | my $res = {}; | |
601 | ||
c3fabca7 DM |
602 | if ($raw) { |
603 | while ($raw =~ s/^(.*)\n//) { | |
604 | my ($key, @ela) = split(/:/, $1); | |
605 | next if !$key; | |
606 | next if !(scalar(@ela) > 1); | |
607 | $res->{$key} = \@ela; | |
608 | } | |
fe000966 DM |
609 | } |
610 | ||
611 | $last_rrd_dump = $ctime; | |
612 | $last_rrd_data = $res; | |
613 | ||
614 | return $res; | |
615 | } | |
616 | ||
617 | sub create_rrd_data { | |
618 | my ($rrdname, $timeframe, $cf) = @_; | |
619 | ||
620 | my $rrddir = "/var/lib/rrdcached/db"; | |
621 | ||
622 | my $rrd = "$rrddir/$rrdname"; | |
623 | ||
624 | my $setup = { | |
625 | hour => [ 60, 70 ], | |
626 | day => [ 60*30, 70 ], | |
627 | week => [ 60*180, 70 ], | |
628 | month => [ 60*720, 70 ], | |
629 | year => [ 60*10080, 70 ], | |
630 | }; | |
631 | ||
632 | my ($reso, $count) = @{$setup->{$timeframe}}; | |
633 | my $ctime = $reso*int(time()/$reso); | |
634 | my $req_start = $ctime - $reso*$count; | |
635 | ||
636 | $cf = "AVERAGE" if !$cf; | |
637 | ||
638 | my @args = ( | |
639 | "-s" => $req_start, | |
640 | "-e" => $ctime - 1, | |
641 | "-r" => $reso, | |
642 | ); | |
643 | ||
644 | my $socket = "/var/run/rrdcached.sock"; | |
645 | push @args, "--daemon" => "unix:$socket" if -S $socket; | |
646 | ||
647 | my ($start, $step, $names, $data) = RRDs::fetch($rrd, $cf, @args); | |
648 | ||
649 | my $err = RRDs::error; | |
650 | die "RRD error: $err\n" if $err; | |
651 | ||
652 | die "got wrong time resolution ($step != $reso)\n" | |
653 | if $step != $reso; | |
654 | ||
655 | my $res = []; | |
656 | my $fields = scalar(@$names); | |
657 | for my $line (@$data) { | |
658 | my $entry = { 'time' => $start }; | |
659 | $start += $step; | |
660 | my $found_undefs; | |
661 | for (my $i = 0; $i < $fields; $i++) { | |
662 | my $name = $names->[$i]; | |
663 | if (defined(my $val = $line->[$i])) { | |
664 | $entry->{$name} = $val; | |
665 | } else { | |
666 | # we only add entryies with all data defined | |
667 | # extjs chart has problems with undefined values | |
668 | $found_undefs = 1; | |
669 | } | |
670 | } | |
671 | push @$res, $entry if !$found_undefs; | |
672 | } | |
673 | ||
674 | return $res; | |
675 | } | |
676 | ||
677 | sub create_rrd_graph { | |
678 | my ($rrdname, $timeframe, $ds, $cf) = @_; | |
679 | ||
680 | # Using RRD graph is clumsy - maybe it | |
681 | # is better to simply fetch the data, and do all display | |
682 | # related things with javascript (new extjs html5 graph library). | |
683 | ||
684 | my $rrddir = "/var/lib/rrdcached/db"; | |
685 | ||
686 | my $rrd = "$rrddir/$rrdname"; | |
687 | ||
31938ad4 DM |
688 | my @ids = PVE::Tools::split_list($ds); |
689 | ||
690 | my $ds_txt = join('_', @ids); | |
691 | ||
692 | my $filename = "${rrd}_${ds_txt}.png"; | |
fe000966 DM |
693 | |
694 | my $setup = { | |
695 | hour => [ 60, 60 ], | |
696 | day => [ 60*30, 70 ], | |
697 | week => [ 60*180, 70 ], | |
698 | month => [ 60*720, 70 ], | |
699 | year => [ 60*10080, 70 ], | |
700 | }; | |
701 | ||
702 | my ($reso, $count) = @{$setup->{$timeframe}}; | |
703 | ||
704 | my @args = ( | |
705 | "--imgformat" => "PNG", | |
706 | "--border" => 0, | |
707 | "--height" => 200, | |
708 | "--width" => 800, | |
709 | "--start" => - $reso*$count, | |
710 | "--end" => 'now' , | |
711 | ); | |
712 | ||
713 | my $socket = "/var/run/rrdcached.sock"; | |
714 | push @args, "--daemon" => "unix:$socket" if -S $socket; | |
715 | ||
fe000966 DM |
716 | my @coldef = ('#00ddff', '#ff0000'); |
717 | ||
718 | $cf = "AVERAGE" if !$cf; | |
719 | ||
720 | my $i = 0; | |
721 | foreach my $id (@ids) { | |
722 | my $col = $coldef[$i++] || die "fixme: no color definition"; | |
723 | push @args, "DEF:${id}=$rrd:${id}:$cf"; | |
724 | my $dataid = $id; | |
725 | if ($id eq 'cpu' || $id eq 'iowait') { | |
726 | push @args, "CDEF:${id}_per=${id},100,*"; | |
727 | $dataid = "${id}_per"; | |
728 | } | |
729 | push @args, "LINE2:${dataid}${col}:${id}"; | |
730 | } | |
731 | ||
a665376e DM |
732 | push @args, '--full-size-mode'; |
733 | ||
31938ad4 | 734 | # we do not really store data into the file |
b871db9c | 735 | my $res = RRDs::graphv('', @args); |
fe000966 DM |
736 | |
737 | my $err = RRDs::error; | |
738 | die "RRD error: $err\n" if $err; | |
739 | ||
31938ad4 | 740 | return { filename => $filename, image => $res->{image} }; |
fe000966 DM |
741 | } |
742 | ||
743 | # a fast way to read files (avoid fuse overhead) | |
744 | sub get_config { | |
745 | my ($path) = @_; | |
746 | ||
d3a92ba7 | 747 | return &$ipcc_get_config($path); |
fe000966 DM |
748 | } |
749 | ||
750 | sub get_cluster_log { | |
751 | my ($user, $max) = @_; | |
752 | ||
753 | return &$ipcc_get_cluster_log($user, $max); | |
754 | } | |
755 | ||
756 | my $file_info = {}; | |
757 | ||
758 | sub cfs_register_file { | |
759 | my ($filename, $parser, $writer) = @_; | |
760 | ||
761 | $observed->{$filename} || die "unknown file '$filename'"; | |
762 | ||
763 | die "file '$filename' already registered" if $file_info->{$filename}; | |
764 | ||
765 | $file_info->{$filename} = { | |
766 | parser => $parser, | |
767 | writer => $writer, | |
768 | }; | |
769 | } | |
770 | ||
771 | my $ccache_read = sub { | |
772 | my ($filename, $parser, $version) = @_; | |
773 | ||
774 | $ccache->{$filename} = {} if !$ccache->{$filename}; | |
775 | ||
776 | my $ci = $ccache->{$filename}; | |
777 | ||
d3a92ba7 DM |
778 | if (!$ci->{version} || !$version || $ci->{version} != $version) { |
779 | # we always call the parser, even when the file does not exists | |
780 | # (in that case $data is undef) | |
fe000966 | 781 | my $data = get_config($filename); |
fe000966 DM |
782 | $ci->{data} = &$parser("/etc/pve/$filename", $data); |
783 | $ci->{version} = $version; | |
784 | } | |
785 | ||
786 | my $res = ref($ci->{data}) ? dclone($ci->{data}) : $ci->{data}; | |
787 | ||
788 | return $res; | |
789 | }; | |
790 | ||
791 | sub cfs_file_version { | |
792 | my ($filename) = @_; | |
793 | ||
794 | my $version; | |
795 | my $infotag; | |
6e73d5c2 | 796 | if ($filename =~ m!^nodes/[^/]+/(openvz|lxc|qemu-server)/(\d+)\.conf$!) { |
f71eee41 | 797 | my ($type, $vmid) = ($1, $2); |
fe000966 DM |
798 | if ($vmlist && $vmlist->{ids} && $vmlist->{ids}->{$vmid}) { |
799 | $version = $vmlist->{ids}->{$vmid}->{version}; | |
800 | } | |
f71eee41 | 801 | $infotag = "/$type/"; |
fe000966 DM |
802 | } else { |
803 | $infotag = $filename; | |
804 | $version = $versions->{$filename}; | |
805 | } | |
806 | ||
807 | my $info = $file_info->{$infotag} || | |
808 | die "unknown file type '$filename'\n"; | |
809 | ||
810 | return wantarray ? ($version, $info) : $version; | |
811 | } | |
812 | ||
813 | sub cfs_read_file { | |
814 | my ($filename) = @_; | |
815 | ||
816 | my ($version, $info) = cfs_file_version($filename); | |
817 | my $parser = $info->{parser}; | |
818 | ||
819 | return &$ccache_read($filename, $parser, $version); | |
820 | } | |
821 | ||
822 | sub cfs_write_file { | |
823 | my ($filename, $data) = @_; | |
824 | ||
adb84d35 | 825 | my ($version, $info) = cfs_file_version($filename); |
fe000966 DM |
826 | |
827 | my $writer = $info->{writer} || die "no writer defined"; | |
828 | ||
829 | my $fsname = "/etc/pve/$filename"; | |
830 | ||
831 | my $raw = &$writer($fsname, $data); | |
832 | ||
833 | if (my $ci = $ccache->{$filename}) { | |
834 | $ci->{version} = undef; | |
835 | } | |
836 | ||
837 | PVE::Tools::file_set_contents($fsname, $raw); | |
838 | } | |
839 | ||
840 | my $cfs_lock = sub { | |
841 | my ($lockid, $timeout, $code, @param) = @_; | |
842 | ||
843 | my $res; | |
844 | ||
845 | # this timeout is for aquire the lock | |
846 | $timeout = 10 if !$timeout; | |
847 | ||
848 | my $filename = "$lockdir/$lockid"; | |
849 | ||
850 | my $msg = "can't aquire cfs lock '$lockid'"; | |
851 | ||
852 | eval { | |
853 | ||
854 | mkdir $lockdir; | |
855 | ||
856 | if (! -d $lockdir) { | |
857 | die "$msg: pve cluster filesystem not online.\n"; | |
858 | } | |
859 | ||
860 | local $SIG{ALRM} = sub { die "got lock request timeout\n"; }; | |
861 | ||
862 | alarm ($timeout); | |
863 | ||
864 | if (!(mkdir $filename)) { | |
865 | print STDERR "trying to aquire cfs lock '$lockid' ..."; | |
866 | while (1) { | |
867 | if (!(mkdir $filename)) { | |
868 | (utime 0, 0, $filename); # cfs unlock request | |
869 | } else { | |
870 | print STDERR " OK\n"; | |
871 | last; | |
872 | } | |
873 | sleep(1); | |
874 | } | |
875 | } | |
876 | ||
877 | # fixed command timeout: cfs locks have a timeout of 120 | |
878 | # using 60 gives us another 60 seconds to abort the task | |
879 | alarm(60); | |
880 | local $SIG{ALRM} = sub { die "got lock timeout - aborting command\n"; }; | |
881 | ||
9c206b2b DM |
882 | cfs_update(); # make sure we read latest versions inside code() |
883 | ||
fe000966 DM |
884 | $res = &$code(@param); |
885 | ||
886 | alarm(0); | |
887 | }; | |
888 | ||
889 | my $err = $@; | |
890 | ||
891 | alarm(0); | |
892 | ||
893 | if ($err && ($err eq "got lock request timeout\n") && | |
894 | !check_cfs_quorum()){ | |
895 | $err = "$msg: no quorum!\n"; | |
896 | } | |
897 | ||
898 | if (!$err || $err !~ /^got lock timeout -/) { | |
899 | rmdir $filename; # cfs unlock | |
900 | } | |
901 | ||
902 | if ($err) { | |
903 | $@ = $err; | |
904 | return undef; | |
905 | } | |
906 | ||
907 | $@ = undef; | |
908 | ||
909 | return $res; | |
910 | }; | |
911 | ||
912 | sub cfs_lock_file { | |
913 | my ($filename, $timeout, $code, @param) = @_; | |
914 | ||
915 | my $info = $observed->{$filename} || die "unknown file '$filename'"; | |
916 | ||
917 | my $lockid = "file-$filename"; | |
918 | $lockid =~ s/[.\/]/_/g; | |
919 | ||
920 | &$cfs_lock($lockid, $timeout, $code, @param); | |
921 | } | |
922 | ||
923 | sub cfs_lock_storage { | |
924 | my ($storeid, $timeout, $code, @param) = @_; | |
925 | ||
926 | my $lockid = "storage-$storeid"; | |
927 | ||
928 | &$cfs_lock($lockid, $timeout, $code, @param); | |
929 | } | |
930 | ||
78897707 TL |
931 | sub cfs_lock_domain { |
932 | my ($domainname, $timeout, $code, @param) = @_; | |
933 | ||
934 | my $lockid = "domain-$domainname"; | |
935 | ||
936 | &$cfs_lock($lockid, $timeout, $code, @param); | |
937 | } | |
938 | ||
fe000966 DM |
939 | my $log_levels = { |
940 | "emerg" => 0, | |
941 | "alert" => 1, | |
942 | "crit" => 2, | |
943 | "critical" => 2, | |
944 | "err" => 3, | |
945 | "error" => 3, | |
946 | "warn" => 4, | |
947 | "warning" => 4, | |
948 | "notice" => 5, | |
949 | "info" => 6, | |
950 | "debug" => 7, | |
951 | }; | |
952 | ||
953 | sub log_msg { | |
954 | my ($priority, $ident, $msg) = @_; | |
955 | ||
956 | if (my $tmp = $log_levels->{$priority}) { | |
957 | $priority = $tmp; | |
958 | } | |
959 | ||
960 | die "need numeric log priority" if $priority !~ /^\d+$/; | |
961 | ||
962 | my $tag = PVE::SafeSyslog::tag(); | |
963 | ||
964 | $msg = "empty message" if !$msg; | |
965 | ||
966 | $ident = "" if !$ident; | |
8f2d54ff | 967 | $ident = encode("ascii", $ident, |
fe000966 DM |
968 | sub { sprintf "\\u%04x", shift }); |
969 | ||
8f2d54ff | 970 | my $ascii = encode("ascii", $msg, sub { sprintf "\\u%04x", shift }); |
fe000966 DM |
971 | |
972 | if ($ident) { | |
973 | syslog($priority, "<%s> %s", $ident, $ascii); | |
974 | } else { | |
975 | syslog($priority, "%s", $ascii); | |
976 | } | |
977 | ||
978 | eval { &$ipcc_log($priority, $ident, $tag, $ascii); }; | |
979 | ||
980 | syslog("err", "writing cluster log failed: $@") if $@; | |
981 | } | |
982 | ||
9d76a1bb DM |
983 | sub check_vmid_unused { |
984 | my ($vmid, $noerr) = @_; | |
985 | ||
986 | my $vmlist = get_vmlist(); | |
987 | ||
988 | my $d = $vmlist->{ids}->{$vmid}; | |
989 | return 1 if !defined($d); | |
990 | ||
991 | return undef if $noerr; | |
992 | ||
4f66b109 | 993 | my $vmtypestr = $d->{type} eq 'qemu' ? 'VM' : 'CT'; |
e75ccbee | 994 | die "$vmtypestr $vmid already exists on node '$d->{node}'\n"; |
9d76a1bb DM |
995 | } |
996 | ||
65ff467f DM |
997 | sub check_node_exists { |
998 | my ($nodename, $noerr) = @_; | |
999 | ||
1000 | my $nodelist = $clinfo->{nodelist}; | |
1001 | return 1 if $nodelist && $nodelist->{$nodename}; | |
1002 | ||
1003 | return undef if $noerr; | |
1004 | ||
1005 | die "no such cluster node '$nodename'\n"; | |
1006 | } | |
1007 | ||
fe000966 DM |
1008 | # this is also used to get the IP of the local node |
1009 | sub remote_node_ip { | |
1010 | my ($nodename, $noerr) = @_; | |
1011 | ||
1012 | my $nodelist = $clinfo->{nodelist}; | |
1013 | if ($nodelist && $nodelist->{$nodename}) { | |
1014 | if (my $ip = $nodelist->{$nodename}->{ip}) { | |
fc31f517 WB |
1015 | return $ip if !wantarray; |
1016 | my $family = $nodelist->{$nodename}->{address_family}; | |
1017 | if (!$family) { | |
1018 | $nodelist->{$nodename}->{address_family} = | |
1019 | $family = | |
1020 | PVE::Tools::get_host_address_family($ip); | |
1021 | } | |
1022 | return ($ip, $family); | |
fe000966 DM |
1023 | } |
1024 | } | |
1025 | ||
1026 | # fallback: try to get IP by other means | |
5720a852 | 1027 | my ($family, $packed_ip); |
fe000966 | 1028 | |
5720a852 WB |
1029 | eval { |
1030 | my @res = PVE::Tools::getaddrinfo_all($nodename); | |
1031 | $family = $res[0]->{family}; | |
1032 | $packed_ip = (PVE::Tools::unpack_sockaddr_in46($res[0]->{addr}))[2]; | |
1033 | }; | |
fe000966 | 1034 | |
5720a852 WB |
1035 | if ($@) { |
1036 | die "hostname lookup failed:\n$@" if !$noerr; | |
1037 | return undef; | |
fe000966 DM |
1038 | } |
1039 | ||
5720a852 WB |
1040 | my $ip = Socket::inet_ntop($family, $packed_ip); |
1041 | if ($ip =~ m/^127\.|^::1$/) { | |
1042 | die "hostname lookup failed - got local IP address ($nodename = $ip)\n" if !$noerr; | |
1043 | return undef; | |
1044 | } | |
fe000966 | 1045 | |
5720a852 | 1046 | return wantarray ? ($ip, $family) : $ip; |
fe000966 DM |
1047 | } |
1048 | ||
1049 | # ssh related utility functions | |
1050 | ||
1051 | sub ssh_merge_keys { | |
1052 | # remove duplicate keys in $sshauthkeys | |
1053 | # ssh-copy-id simply add keys, so the file can grow to large | |
1054 | ||
1055 | my $data = ''; | |
1056 | if (-f $sshauthkeys) { | |
1057 | $data = PVE::Tools::file_get_contents($sshauthkeys, 128*1024); | |
1058 | chomp($data); | |
1059 | } | |
1060 | ||
6056578e DM |
1061 | my $found_backup; |
1062 | if (-f $rootsshauthkeysbackup) { | |
404343d7 | 1063 | $data .= "\n"; |
6056578e DM |
1064 | $data .= PVE::Tools::file_get_contents($rootsshauthkeysbackup, 128*1024); |
1065 | chomp($data); | |
1066 | $found_backup = 1; | |
1067 | } | |
1068 | ||
fe000966 DM |
1069 | # always add ourself |
1070 | if (-f $ssh_rsa_id) { | |
1071 | my $pub = PVE::Tools::file_get_contents($ssh_rsa_id); | |
1072 | chomp($pub); | |
1073 | $data .= "\n$pub\n"; | |
1074 | } | |
1075 | ||
1076 | my $newdata = ""; | |
1077 | my $vhash = {}; | |
2055b0a9 DM |
1078 | my @lines = split(/\n/, $data); |
1079 | foreach my $line (@lines) { | |
7eb37d8d SP |
1080 | if ($line !~ /^#/ && $line =~ m/(^|\s)ssh-(rsa|dsa)\s+(\S+)\s+\S+$/) { |
1081 | next if $vhash->{$3}++; | |
fe000966 | 1082 | } |
2055b0a9 | 1083 | $newdata .= "$line\n"; |
fe000966 | 1084 | } |
fe000966 DM |
1085 | |
1086 | PVE::Tools::file_set_contents($sshauthkeys, $newdata, 0600); | |
6056578e DM |
1087 | |
1088 | if ($found_backup && -l $rootsshauthkeys) { | |
1089 | # everything went well, so we can remove the backup | |
1090 | unlink $rootsshauthkeysbackup; | |
1091 | } | |
fe000966 DM |
1092 | } |
1093 | ||
ac50b36d DM |
1094 | sub setup_sshd_config { |
1095 | ||
1096 | my $conf = PVE::Tools::file_get_contents($sshd_config_fn); | |
1097 | ||
1098 | return if $conf =~ m/^PermitRootLogin\s+yes\s*$/m; | |
1099 | ||
1100 | if ($conf !~ s/^#?PermitRootLogin.*$/PermitRootLogin yes/m) { | |
1101 | chomp $conf; | |
1102 | $conf .= "\nPermitRootLogin yes\n"; | |
1103 | } | |
1104 | ||
1105 | PVE::Tools::file_set_contents($sshd_config_fn, $conf); | |
1106 | ||
1107 | PVE::Tools::run_command(['systemctl', 'reload-or-restart', 'sshd']); | |
1108 | } | |
1109 | ||
f666cdde SP |
1110 | sub setup_rootsshconfig { |
1111 | ||
39df71df DM |
1112 | # create ssh key if it does not exist |
1113 | if (! -f $ssh_rsa_id) { | |
1114 | mkdir '/root/.ssh/'; | |
1115 | system ("echo|ssh-keygen -t rsa -N '' -b 2048 -f ${ssh_rsa_id_priv}"); | |
1116 | } | |
1117 | ||
f666cdde SP |
1118 | # create ssh config if it does not exist |
1119 | if (! -f $rootsshconfig) { | |
9aabc24b DM |
1120 | mkdir '/root/.ssh'; |
1121 | if (my $fh = IO::File->new($rootsshconfig, O_CREAT|O_WRONLY|O_EXCL, 0640)) { | |
f666cdde | 1122 | # this is the default ciphers list from debian openssl0.9.8 except blowfish is added as prefered |
9aabc24b | 1123 | print $fh "Ciphers blowfish-cbc,aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc\n"; |
f666cdde SP |
1124 | close($fh); |
1125 | } | |
1126 | } | |
1127 | } | |
1128 | ||
fe000966 DM |
1129 | sub setup_ssh_keys { |
1130 | ||
fe000966 DM |
1131 | mkdir $authdir; |
1132 | ||
6056578e DM |
1133 | my $import_ok; |
1134 | ||
fe000966 | 1135 | if (! -f $sshauthkeys) { |
6056578e DM |
1136 | my $old; |
1137 | if (-f $rootsshauthkeys) { | |
1138 | $old = PVE::Tools::file_get_contents($rootsshauthkeys, 128*1024); | |
1139 | } | |
fe000966 | 1140 | if (my $fh = IO::File->new ($sshauthkeys, O_CREAT|O_WRONLY|O_EXCL, 0400)) { |
6056578e | 1141 | PVE::Tools::safe_print($sshauthkeys, $fh, $old) if $old; |
fe000966 | 1142 | close($fh); |
6056578e | 1143 | $import_ok = 1; |
fe000966 DM |
1144 | } |
1145 | } | |
1146 | ||
1147 | warn "can't create shared ssh key database '$sshauthkeys'\n" | |
1148 | if ! -f $sshauthkeys; | |
1149 | ||
404343d7 | 1150 | if (-f $rootsshauthkeys && ! -l $rootsshauthkeys) { |
6056578e DM |
1151 | if (!rename($rootsshauthkeys , $rootsshauthkeysbackup)) { |
1152 | warn "rename $rootsshauthkeys failed - $!\n"; | |
1153 | } | |
fe000966 DM |
1154 | } |
1155 | ||
1156 | if (! -l $rootsshauthkeys) { | |
1157 | symlink $sshauthkeys, $rootsshauthkeys; | |
1158 | } | |
fe000966 | 1159 | |
6056578e DM |
1160 | if (! -l $rootsshauthkeys) { |
1161 | warn "can't create symlink for ssh keys '$rootsshauthkeys' -> '$sshauthkeys'\n"; | |
1162 | } else { | |
1163 | unlink $rootsshauthkeysbackup if $import_ok; | |
1164 | } | |
fe000966 DM |
1165 | } |
1166 | ||
1167 | sub ssh_unmerge_known_hosts { | |
1168 | return if ! -l $sshglobalknownhosts; | |
1169 | ||
1170 | my $old = ''; | |
1171 | $old = PVE::Tools::file_get_contents($sshknownhosts, 128*1024) | |
1172 | if -f $sshknownhosts; | |
1173 | ||
1174 | PVE::Tools::file_set_contents($sshglobalknownhosts, $old); | |
1175 | } | |
1176 | ||
1177 | sub ssh_merge_known_hosts { | |
1178 | my ($nodename, $ip_address, $createLink) = @_; | |
1179 | ||
1180 | die "no node name specified" if !$nodename; | |
1181 | die "no ip address specified" if !$ip_address; | |
1182 | ||
1183 | mkdir $authdir; | |
1184 | ||
1185 | if (! -f $sshknownhosts) { | |
1186 | if (my $fh = IO::File->new($sshknownhosts, O_CREAT|O_WRONLY|O_EXCL, 0600)) { | |
1187 | close($fh); | |
1188 | } | |
1189 | } | |
1190 | ||
1191 | my $old = PVE::Tools::file_get_contents($sshknownhosts, 128*1024); | |
1192 | ||
1193 | my $new = ''; | |
1194 | ||
1195 | if ((! -l $sshglobalknownhosts) && (-f $sshglobalknownhosts)) { | |
1196 | $new = PVE::Tools::file_get_contents($sshglobalknownhosts, 128*1024); | |
1197 | } | |
1198 | ||
1199 | my $hostkey = PVE::Tools::file_get_contents($ssh_host_rsa_id); | |
1d182ad3 DM |
1200 | # Note: file sometimes containe emty lines at start, so we use multiline match |
1201 | die "can't parse $ssh_host_rsa_id" if $hostkey !~ m/^(ssh-rsa\s\S+)(\s.*)?$/m; | |
fe000966 DM |
1202 | $hostkey = $1; |
1203 | ||
1204 | my $data = ''; | |
1205 | my $vhash = {}; | |
1206 | ||
1207 | my $found_nodename; | |
1208 | my $found_local_ip; | |
1209 | ||
1210 | my $merge_line = sub { | |
1211 | my ($line, $all) = @_; | |
1212 | ||
1213 | if ($line =~ m/^(\S+)\s(ssh-rsa\s\S+)(\s.*)?$/) { | |
1214 | my $key = $1; | |
1215 | my $rsakey = $2; | |
1216 | if (!$vhash->{$key}) { | |
1217 | $vhash->{$key} = 1; | |
1218 | if ($key =~ m/\|1\|([^\|\s]+)\|([^\|\s]+)$/) { | |
1219 | my $salt = decode_base64($1); | |
1220 | my $digest = $2; | |
1221 | my $hmac = Digest::HMAC_SHA1->new($salt); | |
1222 | $hmac->add($nodename); | |
1223 | my $hd = $hmac->b64digest . '='; | |
1224 | if ($digest eq $hd) { | |
1225 | if ($rsakey eq $hostkey) { | |
1226 | $found_nodename = 1; | |
1227 | $data .= $line; | |
1228 | } | |
1229 | return; | |
1230 | } | |
1231 | $hmac = Digest::HMAC_SHA1->new($salt); | |
1232 | $hmac->add($ip_address); | |
1233 | $hd = $hmac->b64digest . '='; | |
1234 | if ($digest eq $hd) { | |
1235 | if ($rsakey eq $hostkey) { | |
1236 | $found_local_ip = 1; | |
1237 | $data .= $line; | |
1238 | } | |
1239 | return; | |
1240 | } | |
1241 | } | |
1242 | $data .= $line; | |
1243 | } | |
1244 | } elsif ($all) { | |
1245 | $data .= $line; | |
1246 | } | |
1247 | }; | |
1248 | ||
1249 | while ($old && $old =~ s/^((.*?)(\n|$))//) { | |
1250 | my $line = "$2\n"; | |
1251 | next if $line =~ m/^\s*$/; # skip empty lines | |
1252 | next if $line =~ m/^#/; # skip comments | |
1253 | &$merge_line($line, 1); | |
1254 | } | |
1255 | ||
1256 | while ($new && $new =~ s/^((.*?)(\n|$))//) { | |
1257 | my $line = "$2\n"; | |
1258 | next if $line =~ m/^\s*$/; # skip empty lines | |
1259 | next if $line =~ m/^#/; # skip comments | |
1260 | &$merge_line($line); | |
1261 | } | |
1262 | ||
1263 | my $addIndex = $$; | |
1264 | my $add_known_hosts_entry = sub { | |
1265 | my ($name, $hostkey) = @_; | |
1266 | $addIndex++; | |
1267 | my $hmac = Digest::HMAC_SHA1->new("$addIndex" . time()); | |
1268 | my $b64salt = $hmac->b64digest . '='; | |
1269 | $hmac = Digest::HMAC_SHA1->new(decode_base64($b64salt)); | |
1270 | $hmac->add($name); | |
1271 | my $digest = $hmac->b64digest . '='; | |
1272 | $data .= "|1|$b64salt|$digest $hostkey\n"; | |
1273 | }; | |
1274 | ||
1275 | if (!$found_nodename || !$found_local_ip) { | |
1276 | &$add_known_hosts_entry($nodename, $hostkey) if !$found_nodename; | |
1277 | &$add_known_hosts_entry($ip_address, $hostkey) if !$found_local_ip; | |
1278 | } | |
1279 | ||
1280 | PVE::Tools::file_set_contents($sshknownhosts, $data); | |
1281 | ||
1282 | return if !$createLink; | |
1283 | ||
1284 | unlink $sshglobalknownhosts; | |
1285 | symlink $sshknownhosts, $sshglobalknownhosts; | |
1286 | ||
1287 | warn "can't create symlink for ssh known hosts '$sshglobalknownhosts' -> '$sshknownhosts'\n" | |
1288 | if ! -l $sshglobalknownhosts; | |
1289 | ||
1290 | } | |
1291 | ||
fe000966 DM |
1292 | my $datacenter_schema = { |
1293 | type => "object", | |
1294 | additionalProperties => 0, | |
1295 | properties => { | |
1296 | keyboard => { | |
1297 | optional => 1, | |
1298 | type => 'string', | |
1299 | description => "Default keybord layout for vnc server.", | |
c59334cb | 1300 | enum => PVE::Tools::kvmkeymaplist(), |
fe000966 DM |
1301 | }, |
1302 | language => { | |
1303 | optional => 1, | |
1304 | type => 'string', | |
1305 | description => "Default GUI language.", | |
1306 | enum => [ 'en', 'de' ], | |
1307 | }, | |
1308 | http_proxy => { | |
1309 | optional => 1, | |
1310 | type => 'string', | |
1311 | description => "Specify external http proxy which is used for downloads (example: 'http://username:password\@host:port/')", | |
1312 | pattern => "http://.*", | |
1313 | }, | |
a9323ef0 SP |
1314 | migration_unsecure => { |
1315 | optional => 1, | |
1316 | type => 'boolean', | |
1317 | description => "Migration is secure using SSH tunnel by default. For secure private networks you can disable it to speed up migration.", | |
1318 | }, | |
dce47328 DM |
1319 | console => { |
1320 | optional => 1, | |
1321 | type => 'string', | |
66a15f27 DM |
1322 | 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).", |
1323 | enum => ['applet', 'vv', 'html5'], | |
dce47328 | 1324 | }, |
8548bd87 SGE |
1325 | email_from => { |
1326 | optional => 1, | |
1327 | type => 'string', | |
a05baf53 | 1328 | format => 'email-opt', |
8548bd87 SGE |
1329 | description => "Specify email address to send notification from (default is root@\$hostname)", |
1330 | }, | |
fe000966 DM |
1331 | }, |
1332 | }; | |
1333 | ||
1334 | # make schema accessible from outside (for documentation) | |
1335 | sub get_datacenter_schema { return $datacenter_schema }; | |
1336 | ||
1337 | sub parse_datacenter_config { | |
1338 | my ($filename, $raw) = @_; | |
1339 | ||
9ef7896d | 1340 | return PVE::JSONSchema::parse_config($datacenter_schema, $filename, $raw // ''); |
fe000966 DM |
1341 | } |
1342 | ||
1343 | sub write_datacenter_config { | |
1344 | my ($filename, $cfg) = @_; | |
1345 | ||
1346 | return PVE::JSONSchema::dump_config($datacenter_schema, $filename, $cfg); | |
1347 | } | |
1348 | ||
1349 | cfs_register_file('datacenter.cfg', | |
1350 | \&parse_datacenter_config, | |
1351 | \&write_datacenter_config); | |
ec48ec22 | 1352 | |
cafc7309 DM |
1353 | # a very simply parser ... |
1354 | sub parse_corosync_conf { | |
ec48ec22 DM |
1355 | my ($filename, $raw) = @_; |
1356 | ||
cafc7309 | 1357 | return {} if !$raw; |
ec48ec22 | 1358 | |
440121dc | 1359 | my $digest = Digest::SHA::sha1_hex(defined($raw) ? $raw : ''); |
ec48ec22 | 1360 | |
cafc7309 DM |
1361 | $raw =~ s/#.*$//mg; |
1362 | $raw =~ s/\r?\n/ /g; | |
1363 | $raw =~ s/\s+/ /g; | |
1364 | $raw =~ s/^\s+//; | |
1365 | $raw =~ s/\s*$//; | |
1366 | ||
cafc7309 DM |
1367 | my @tokens = split(/\s/, $raw); |
1368 | ||
1369 | my $conf = { section => 'main', children => [] }; | |
1d01c3f6 | 1370 | |
cafc7309 DM |
1371 | my $stack = []; |
1372 | my $section = $conf; | |
1373 | ||
1374 | while (defined(my $token = shift @tokens)) { | |
1375 | my $nexttok = $tokens[0]; | |
1376 | ||
1377 | if ($nexttok && ($nexttok eq '{')) { | |
1378 | shift @tokens; # skip '{' | |
1379 | my $new_section = { | |
1380 | section => $token, | |
1381 | children => [], | |
1382 | }; | |
1383 | push @{$section->{children}}, $new_section; | |
1384 | push @$stack, $section; | |
1385 | $section = $new_section; | |
1386 | next; | |
1d01c3f6 | 1387 | } |
1d01c3f6 | 1388 | |
cafc7309 DM |
1389 | if ($token eq '}') { |
1390 | $section = pop @$stack; | |
1391 | die "parse error - uncexpected '}'\n" if !$section; | |
1392 | next; | |
1393 | } | |
1d01c3f6 | 1394 | |
cafc7309 DM |
1395 | my $key = $token; |
1396 | die "missing ':' after key '$key'\n" if ! ($key =~ s/:$//); | |
1397 | ||
1398 | die "parse error - no value for '$key'\n" if !defined($nexttok); | |
1399 | my $value = shift @tokens; | |
1d01c3f6 | 1400 | |
cafc7309 DM |
1401 | push @{$section->{children}}, { key => $key, value => $value }; |
1402 | } | |
1d01c3f6 | 1403 | |
cafc7309 | 1404 | $conf->{digest} = $digest; |
1d01c3f6 | 1405 | |
cafc7309 | 1406 | return $conf; |
1d01c3f6 DM |
1407 | } |
1408 | ||
cafc7309 DM |
1409 | my $dump_corosync_section; |
1410 | $dump_corosync_section = sub { | |
1411 | my ($section, $prefix) = @_; | |
1d01c3f6 | 1412 | |
cafc7309 DM |
1413 | my $raw = $prefix . $section->{section} . " {\n"; |
1414 | ||
1415 | my @list = grep { defined($_->{key}) } @{$section->{children}}; | |
1416 | foreach my $child (sort {$a->{key} cmp $b->{key}} @list) { | |
1417 | $raw .= $prefix . " $child->{key}: $child->{value}\n"; | |
1d01c3f6 | 1418 | } |
cafc7309 DM |
1419 | |
1420 | @list = grep { defined($_->{section}) } @{$section->{children}}; | |
1421 | foreach my $child (sort {$a->{section} cmp $b->{section}} @list) { | |
1422 | $raw .= &$dump_corosync_section($child, "$prefix "); | |
1d01c3f6 DM |
1423 | } |
1424 | ||
cafc7309 DM |
1425 | $raw .= $prefix . "}\n\n"; |
1426 | ||
1427 | return $raw; | |
1428 | ||
1429 | }; | |
1d01c3f6 | 1430 | |
cafc7309 DM |
1431 | sub write_corosync_conf { |
1432 | my ($filename, $conf) = @_; | |
ec48ec22 | 1433 | |
cafc7309 | 1434 | my $raw = ''; |
ec48ec22 | 1435 | |
cafc7309 DM |
1436 | my $prefix = ''; |
1437 | ||
1438 | die "no main section" if $conf->{section} ne 'main'; | |
ec48ec22 | 1439 | |
cafc7309 DM |
1440 | my @list = grep { defined($_->{key}) } @{$conf->{children}}; |
1441 | foreach my $child (sort {$a->{key} cmp $b->{key}} @list) { | |
1442 | $raw .= "$child->{key}: $child->{value}\n"; | |
1d01c3f6 DM |
1443 | } |
1444 | ||
cafc7309 DM |
1445 | @list = grep { defined($_->{section}) } @{$conf->{children}}; |
1446 | foreach my $child (sort {$a->{section} cmp $b->{section}} @list) { | |
1447 | $raw .= &$dump_corosync_section($child, $prefix); | |
1448 | } | |
ec48ec22 | 1449 | |
cafc7309 | 1450 | return $raw; |
ec48ec22 DM |
1451 | } |
1452 | ||
cafc7309 DM |
1453 | sub corosync_conf_version { |
1454 | my ($conf, $noerr, $new_value) = @_; | |
1455 | ||
1456 | foreach my $child (@{$conf->{children}}) { | |
1457 | next if !defined($child->{section}); | |
1458 | if ($child->{section} eq 'totem') { | |
1459 | foreach my $e (@{$child->{children}}) { | |
1460 | next if !defined($e->{key}); | |
1461 | if ($e->{key} eq 'config_version') { | |
1462 | if ($new_value) { | |
1463 | $e->{value} = $new_value; | |
1464 | return $new_value; | |
1465 | } elsif (my $version = int($e->{value})) { | |
1466 | return $version; | |
1467 | } | |
1468 | last; | |
1469 | } | |
1470 | } | |
ec48ec22 | 1471 | } |
ec48ec22 | 1472 | } |
cafc7309 DM |
1473 | |
1474 | return undef if $noerr; | |
ec48ec22 | 1475 | |
cafc7309 | 1476 | die "invalid corosync config - unable to read version\n"; |
ec48ec22 DM |
1477 | } |
1478 | ||
cafc7309 DM |
1479 | # read only - use "rename corosync.conf.new corosync.conf" to write |
1480 | PVE::Cluster::cfs_register_file('corosync.conf', \&parse_corosync_conf); | |
ec48ec22 | 1481 | # this is read/write |
cafc7309 DM |
1482 | PVE::Cluster::cfs_register_file('corosync.conf.new', \&parse_corosync_conf, |
1483 | \&write_corosync_conf); | |
2c66fb58 | 1484 | |
15df58e6 DM |
1485 | # bash completion helpers |
1486 | ||
1487 | sub complete_next_vmid { | |
1488 | ||
1489 | my $vmlist = get_vmlist() || {}; | |
1490 | my $idlist = $vmlist->{ids} || {}; | |
1491 | ||
1492 | for (my $i = 100; $i < 10000; $i++) { | |
1493 | return [$i] if !defined($idlist->{$i}); | |
1494 | } | |
1495 | ||
1496 | return []; | |
1497 | } | |
1498 | ||
87515b25 DM |
1499 | sub complete_vmid { |
1500 | ||
1501 | my $vmlist = get_vmlist(); | |
1502 | my $ids = $vmlist->{ids} || {}; | |
1503 | ||
1504 | return [ keys %$ids ]; | |
1505 | } | |
1506 | ||
15df58e6 DM |
1507 | sub complete_local_vmid { |
1508 | ||
1509 | my $vmlist = get_vmlist(); | |
1510 | my $ids = $vmlist->{ids} || {}; | |
1511 | ||
1512 | my $nodename = PVE::INotify::nodename(); | |
1513 | ||
1514 | my $res = []; | |
1515 | foreach my $vmid (keys %$ids) { | |
1516 | my $d = $ids->{$vmid}; | |
1517 | next if !$d->{node} || $d->{node} ne $nodename; | |
1518 | push @$res, $vmid; | |
1519 | } | |
1520 | ||
1521 | return $res; | |
1522 | } | |
1523 | ||
4dd189df DM |
1524 | sub complete_migration_target { |
1525 | ||
1526 | my $res = []; | |
1527 | ||
1528 | my $nodename = PVE::INotify::nodename(); | |
1529 | ||
1530 | my $nodelist = get_nodelist(); | |
1531 | foreach my $node (@$nodelist) { | |
1532 | next if $node eq $nodename; | |
1533 | push @$res, $node; | |
1534 | } | |
1535 | ||
1536 | return $res; | |
1537 | } | |
1538 | ||
ac68281b | 1539 | 1; |