]> git.proxmox.com Git - pve-container.git/blob - src/PVE/LXC/Setup/Base.pm
65b5466ab80ecd006b59e2f86360f919092ea1d4
[pve-container.git] / src / PVE / LXC / Setup / Base.pm
1 package PVE::LXC::Setup::Base;
2
3 use strict;
4 use warnings;
5
6 use File::stat;
7 use Digest::SHA;
8 use IO::File;
9 use Encode;
10 use Fcntl;
11 use File::Path;
12 use File::Spec;
13 use File::Basename;
14
15 use PVE::INotify;
16 use PVE::Tools;
17 use PVE::Network;
18
19 sub new {
20 my ($class, $conf, $rootdir, $os_release) = @_;
21
22 return bless { conf => $conf, rootdir => $rootdir, os_release => $os_release }, $class;
23 }
24
25 sub lookup_dns_conf {
26 my ($self, $conf) = @_;
27
28 my $nameserver = $conf->{nameserver};
29 my $searchdomains = $conf->{searchdomain};
30
31 if ($conf->{'testmode'}) {
32 return ('proxmox.com', '8.8.8.8 8.8.8.9');
33 }
34
35 my $host_resolv_conf = $self->{host_resolv_conf};
36
37 if (!defined($nameserver)) {
38 my @list = ();
39 foreach my $k ("dns1", "dns2", "dns3") {
40 if (my $ns = $host_resolv_conf->{$k}) {
41 push @list, $ns;
42 }
43 }
44 $nameserver = join(' ', @list);
45 }
46
47 if (!defined($searchdomains)) {
48 $searchdomains = $host_resolv_conf->{search};
49 }
50
51 return ($searchdomains, $nameserver);
52 }
53
54 sub update_etc_hosts {
55 my ($self, $hostip, $oldname, $newname, $searchdomains) = @_;
56
57 my $hosts_fn = '/etc/hosts';
58 return if $self->ct_is_file_ignored($hosts_fn);
59
60 my $namepart = ($newname =~ s/\..*$//r);
61
62 my $all_names = '';
63 if ($newname =~ /\./) {
64 $all_names .= "$newname $namepart";
65 } else {
66 foreach my $domain (PVE::Tools::split_list($searchdomains)) {
67 $all_names .= ' ' if $all_names;
68 $all_names .= "$newname.$domain";
69 }
70 $all_names .= ' ' if $all_names;
71 $all_names .= $newname;
72 }
73
74 # Prepare section:
75 my $section = '';
76
77 my $lo4 = "127.0.0.1 localhost.localnet localhost\n";
78 my $lo6 = "::1 localhost.localnet localhost\n";
79 if ($self->ct_file_exists($hosts_fn)) {
80 my $data = $self->ct_file_get_contents($hosts_fn);
81 # don't take localhost entries within our hosts sections into account
82 $data = remove_pve_sections($data);
83
84 # check for existing localhost entries
85 $section .= $lo4 if $data !~ /^\h*127\.0\.0\.1\h+/m;
86 $section .= $lo6 if $data !~ /^\h*::1\h+/m;
87 } else {
88 $section .= $lo4 . $lo6;
89 }
90
91 if (defined($hostip)) {
92 $section .= "$hostip $all_names\n";
93 } elsif ($namepart ne 'localhost') {
94 $section .= "127.0.1.1 $all_names\n";
95 } else {
96 $section .= "127.0.1.1 $namepart\n";
97 }
98
99 $self->ct_modify_file($hosts_fn, $section);
100 }
101
102 sub template_fixup {
103 my ($self, $conf) = @_;
104
105 # do nothing by default
106 }
107
108 sub set_dns {
109 my ($self, $conf) = @_;
110
111 my ($searchdomains, $nameserver) = $self->lookup_dns_conf($conf);
112
113 my $data = '';
114
115 $data .= "search " . join(' ', PVE::Tools::split_list($searchdomains)) . "\n"
116 if $searchdomains;
117
118 foreach my $ns ( PVE::Tools::split_list($nameserver)) {
119 $data .= "nameserver $ns\n";
120 }
121
122 $self->ct_modify_file("/etc/resolv.conf", $data, replace => 1);
123 }
124
125 sub set_hostname {
126 my ($self, $conf) = @_;
127
128 my $hostname = $conf->{hostname} || 'localhost';
129
130 my $namepart = ($hostname =~ s/\..*$//r);
131
132 my $hostname_fn = "/etc/hostname";
133
134 my $oldname = $self->ct_file_read_firstline($hostname_fn) || 'localhost';
135
136 my ($ipv4, $ipv6) = PVE::LXC::get_primary_ips($conf);
137 my $hostip = $ipv4 || $ipv6;
138
139 my ($searchdomains) = $self->lookup_dns_conf($conf);
140
141 $self->update_etc_hosts($hostip, $oldname, $hostname, $searchdomains);
142
143 $self->ct_file_set_contents($hostname_fn, "$namepart\n");
144 }
145
146 sub setup_network {
147 my ($self, $conf) = @_;
148
149 die "please implement this inside subclass"
150 }
151
152 sub setup_init {
153 my ($self, $conf) = @_;
154
155 die "please implement this inside subclass"
156 }
157
158 # A few distros as well as unprivileged containers cannot deal with the
159 # /dev/lxc/ tty subdirectory.
160 sub devttydir {
161 my ($self, $conf) = @_;
162 return $conf->{unprivileged} ? '' : 'lxc/';
163 }
164
165 sub fixup_old_getty {
166 my ($self) = @_;
167
168 my $sd_dir_rel = $self->ct_is_executable("/lib/systemd/systemd") ?
169 "/lib/systemd/system" : "/usr/lib/systemd/system";
170
171 my $sd_getty_service_rel = "$sd_dir_rel/getty\@.service";
172 return if !$self->ct_file_exists($sd_getty_service_rel);
173
174 my $raw = $self->ct_file_get_contents($sd_getty_service_rel);
175
176 my $sd_container_getty_service_rel = "$sd_dir_rel/container-getty\@.service";
177 # systemd on CenoOS 7.1 is too old (version 205), so there is no
178 # container-getty service
179 if (!$self->ct_file_exists($sd_container_getty_service_rel)) {
180 if ($raw =~ s!^ConditionPathExists=/dev/tty0$!ConditionPathExists=/dev/tty!m) {
181 $self->ct_file_set_contents($sd_getty_service_rel, $raw);
182 }
183 } else {
184 # undo above change (in case someone updated systemd)
185 if ($raw =~ s!^ConditionPathExists=/dev/tty$!ConditionPathExists=/dev/tty0!m) {
186 $self->ct_file_set_contents($sd_getty_service_rel, $raw);
187 }
188 }
189 }
190
191 sub setup_container_getty_service {
192 my ($self, $conf) = @_;
193
194 my $sd_dir = $self->ct_is_executable("/lib/systemd/systemd") ?
195 "/lib/systemd/system" : "/usr/lib/systemd/system";
196
197 # prefer container-getty.service shipped by newer systemd versions
198 # fallback to getty.service and just return if that doesn't exists either..
199 my $template_base = "container-getty\@";
200 my $template_path = "${sd_dir}/${template_base}.service";
201 my $instance_base = $template_base;
202
203 if (!$self->ct_file_exists($template_path)) {
204 $template_base = "getty\@";
205 $template_path = "${template_base}.service";
206 $instance_base = "{$template_base}tty";
207 return if !$self->ct_file_exists($template_path);
208 }
209
210 my $raw = $self->ct_file_get_contents($template_path);
211 my $ttyname = $self->devttydir($conf) . 'tty%I';
212 if ($raw =~ s@pts/%I|lxc/tty%I@$ttyname@g) {
213 $self->ct_file_set_contents($template_path, $raw);
214 }
215
216 my $getty_target_fn = "/etc/systemd/system/getty.target.wants/";
217 my $ttycount = PVE::LXC::Config->get_tty_count($conf);
218
219 for (my $i = 1; $i < 7; $i++) {
220 # ensure that not two gettys are using the same tty!
221 $self->ct_unlink("$getty_target_fn/getty\@tty$i.service");
222 $self->ct_unlink("$getty_target_fn/container-getty\@$i.service");
223
224 # re-enable only those requested
225 if ($i <= $ttycount) {
226 my $tty_service = "${instance_base}${i}.service";
227
228 $self->ct_symlink($template_path, "$getty_target_fn/$tty_service");
229 }
230 }
231 }
232
233 sub setup_systemd_networkd {
234 my ($self, $conf) = @_;
235
236 foreach my $k (keys %$conf) {
237 next if $k !~ m/^net(\d+)$/;
238 my $d = PVE::LXC::Config->parse_lxc_network($conf->{$k});
239 next if !$d->{name};
240
241 my $filename = "/etc/systemd/network/$d->{name}.network";
242
243 my $data = <<"DATA";
244 [Match]
245 Name = $d->{name}
246
247 [Network]
248 Description = Interface $d->{name} autoconfigured by PVE
249 DATA
250
251 my $routes = '';
252 my ($has_ipv4, $has_ipv6);
253
254 # DHCP bitflags:
255 my @DHCPMODES = ('none', 'v4', 'v6', 'both');
256 my ($NONE, $DHCP4, $DHCP6, $BOTH) = (0, 1, 2, 3);
257 my $dhcp = $NONE;
258 my $accept_ra = 'false';
259
260 if (defined(my $ip = $d->{ip})) {
261 if ($ip eq 'dhcp') {
262 $dhcp |= $DHCP4;
263 } elsif ($ip ne 'manual') {
264 $has_ipv4 = 1;
265 $data .= "Address = $ip\n";
266 }
267 }
268 if (defined(my $gw = $d->{gw})) {
269 $data .= "Gateway = $gw\n";
270 if ($has_ipv4 && !PVE::Network::is_ip_in_cidr($gw, $d->{ip}, 4)) {
271 $routes .= "\n[Route]\nDestination = $gw/32\nScope = link\n";
272 }
273 }
274
275 if (defined(my $ip = $d->{ip6})) {
276 if ($ip eq 'dhcp') {
277 $dhcp |= $DHCP6;
278 } elsif ($ip eq 'auto') {
279 $accept_ra = 'true';
280 } elsif ($ip ne 'manual') {
281 $has_ipv6 = 1;
282 $data .= "Address = $ip\n";
283 }
284 }
285 if (defined(my $gw = $d->{gw6})) {
286 $accept_ra = 'false';
287 $data .= "Gateway = $gw\n";
288 if ($has_ipv6 && !PVE::Network::is_ip_in_cidr($gw, $d->{ip6}, 6) &&
289 !PVE::Network::is_ip_in_cidr($gw, 'fe80::/10', 6)) {
290 $routes .= "\n[Route]\nDestination = $gw/128\nScope = link\n";
291 }
292 }
293
294 $data .= "DHCP = $DHCPMODES[$dhcp]\n";
295 $data .= "IPv6AcceptRA = $accept_ra\n";
296 $data .= $routes if $routes;
297
298 $self->ct_file_set_contents($filename, $data);
299 }
300 }
301
302 sub setup_securetty {
303 my ($self, $conf, @add) = @_;
304
305 my $filename = "/etc/securetty";
306 # root login is already allowed on every device if no securetty present
307 return if !$self->ct_file_exists($filename);
308
309 if (!scalar(@add)) {
310 @add = qw(console tty1 tty2 tty3 tty4);
311 if (my $dir = $self->devttydir($conf)) {
312 @add = map { "${dir}$_" } @add;
313 }
314 }
315
316 my $data = $self->ct_file_get_contents($filename);
317 chomp $data; $data .= "\n";
318 foreach my $dev (@add) {
319 if ($data !~ m!^\Q$dev\E\s*$!m) {
320 $data .= "$dev\n";
321 }
322 }
323 $self->ct_file_set_contents($filename, $data);
324 }
325
326 my $replacepw = sub {
327 my ($self, $file, $user, $epw, $shadow) = @_;
328
329 my $tmpfile = "$file.$$";
330
331 eval {
332 my $src = $self->ct_open_file_read($file) ||
333 die "unable to open file '$file' - $!";
334
335 my $st = $self->ct_stat($src) ||
336 die "unable to stat file - $!";
337
338 my $dst = $self->ct_open_file_write($tmpfile) ||
339 die "unable to open file '$tmpfile' - $!";
340
341 # copy owner and permissions
342 chmod $st->mode, $dst;
343 chown $st->uid, $st->gid, $dst;
344
345 my $last_change = int(time()/(60*60*24));
346
347 while (defined (my $line = <$src>)) {
348 if ($shadow) {
349 $line =~ s/^${user}:[^:]*:[^:]*:/${user}:${epw}:${last_change}:/;
350 } else {
351 $line =~ s/^${user}:[^:]*:/${user}:${epw}:/;
352 }
353 print $dst $line;
354 }
355
356 $src->close() || die "close '$file' failed - $!\n";
357 $dst->close() || die "close '$tmpfile' failed - $!\n";
358 };
359 if (my $err = $@) {
360 $self->ct_unlink($tmpfile);
361 } else {
362 $self->ct_rename($tmpfile, $file);
363 $self->ct_unlink($tmpfile); # in case rename fails
364 }
365 };
366
367 sub set_user_password {
368 my ($self, $conf, $user, $opt_password) = @_;
369
370 my $pwfile = "/etc/passwd";
371
372 return if !$self->ct_file_exists($pwfile);
373
374 my $shadow = "/etc/shadow";
375
376 if (defined($opt_password)) {
377 if ($opt_password !~ m/^\$(?:1|2[axy]?|5|6)\$[a-zA-Z0-9.\/]{1,16}\$[a-zA-Z0-9.\/]+$/) {
378 my $time = substr (Digest::SHA::sha1_base64 (time), 0, 8);
379 $opt_password = crypt(encode("utf8", $opt_password), "\$6\$$time\$");
380 };
381 } else {
382 $opt_password = '*';
383 }
384
385 if ($self->ct_file_exists($shadow)) {
386 &$replacepw ($self, $shadow, $user, $opt_password, 1);
387 &$replacepw ($self, $pwfile, $user, 'x');
388 } else {
389 &$replacepw ($self, $pwfile, $user, $opt_password);
390 }
391 }
392
393 my $parse_home_dir = sub {
394 my ($self, $passwdfile, $user) = @_;
395
396 my $fh = $self->ct_open_file_read($passwdfile);
397 while (defined (my $line = <$fh>)) {
398 return $2
399 if $line =~ m/^${user}:([^:]*:){4}([^:]*):/;
400 }
401 };
402
403 sub set_user_authorized_ssh_keys {
404 my ($self, $conf, $user, $ssh_keys) = @_;
405
406 my $passwd = "/etc/passwd";
407 my $home = $user eq "root" ? "/root/" : "/home/$user/";
408
409 $home = &$parse_home_dir($self, $passwd, $user)
410 if $self->ct_file_exists($passwd);
411
412 die "home directory '$home' of $user does not exist!"
413 if ! ($self->ct_is_directory($home) || $self->ct_is_symlink($home));
414
415 $self->ct_mkdir("$home/.ssh", 0700)
416 if ! $self->ct_is_directory("$home/.ssh");
417
418 $self->ct_modify_file("$home/.ssh/authorized_keys", $ssh_keys, perms => 0700);
419 }
420
421 my $randomize_crontab = sub {
422 my ($self, $conf) = @_;
423
424 my @files;
425 # Note: dir_glob_foreach() untaints filenames!
426 PVE::Tools::dir_glob_foreach("/etc/cron.d", qr/[A-Z\-\_a-z0-9]+/, sub {
427 my ($name) = @_;
428 push @files, "/etc/cron.d/$name";
429 });
430
431 my $crontab_fn = "/etc/crontab";
432 unshift @files, $crontab_fn if $self->ct_file_exists($crontab_fn);
433
434 foreach my $filename (@files) {
435 my $data = $self->ct_file_get_contents($filename);
436 my $new = '';
437 foreach my $line (split(/\n/, $data)) {
438 # we only randomize minutes for root crontab entries
439 if ($line =~ m/^\d+(\s+\S+\s+\S+\s+\S+\s+\S+\s+root\s+\S.*)$/) {
440 my $rest = $1;
441 my $min = int(rand()*59);
442 $new .= "$min$rest\n";
443 } else {
444 $new .= "$line\n";
445 }
446 }
447 $self->ct_file_set_contents($filename, $new);
448 }
449 };
450
451 sub pre_start_hook {
452 my ($self, $conf) = @_;
453
454 $self->setup_init($conf);
455 $self->setup_network($conf);
456 $self->set_hostname($conf);
457 $self->set_dns($conf);
458
459 # fixme: what else ?
460 }
461
462 sub post_create_hook {
463 my ($self, $conf, $root_password, $ssh_keys) = @_;
464
465 $self->template_fixup($conf);
466
467 &$randomize_crontab($self, $conf);
468
469 $self->set_user_password($conf, 'root', $root_password);
470 $self->set_user_authorized_ssh_keys($conf, 'root', $ssh_keys) if $ssh_keys;
471 $self->setup_init($conf);
472 $self->setup_network($conf);
473 $self->set_hostname($conf);
474 $self->set_dns($conf);
475
476 # fixme: what else ?
477 }
478
479 # File access wrappers for container setup code.
480 # For user-namespace support these might need to take uid and gid maps into account.
481
482 sub ct_is_file_ignored {
483 my ($self, $file) = @_;
484 my ($name, $path) = fileparse($file);
485 return -f "$path/.pve-ignore.$name";
486 }
487
488 sub ct_reset_ownership {
489 my ($self, @files) = @_;
490 my $conf = $self->{conf};
491 return if !$self->{id_map};
492
493 @files = grep { !$self->ct_is_file_ignored($_) } @files;
494 return if !@files;
495
496 my $uid = $self->{rootuid};
497 my $gid = $self->{rootgid};
498 chown($uid, $gid, @files);
499 }
500
501 sub ct_mkdir {
502 my ($self, $file, $mask) = @_;
503 # mkdir goes by parameter count - an `undef' mode acts like a mode of 0000
504 if (defined($mask)) {
505 return CORE::mkdir($file, $mask) && $self->ct_reset_ownership($file);
506 } else {
507 return CORE::mkdir($file) && $self->ct_reset_ownership($file);
508 }
509 }
510
511 sub ct_unlink {
512 my ($self, @files) = @_;
513 foreach my $file (@files) {
514 next if $self->ct_is_file_ignored($file);
515 CORE::unlink($file);
516 }
517 }
518
519 sub ct_rename {
520 my ($self, $old, $new) = @_;
521 return if $self->ct_is_file_ignored($new);
522 CORE::rename($old, $new);
523 }
524
525 sub ct_open_file_read {
526 my $self = shift;
527 my $file = shift;
528 return IO::File->new($file, O_RDONLY, @_);
529 }
530
531 sub ct_open_file_write {
532 my $self = shift;
533 my $file = shift;
534 $file = '/dev/null' if $self->ct_is_file_ignored($file);
535 my $fh = IO::File->new($file, O_WRONLY | O_CREAT, @_);
536 $self->ct_reset_ownership($fh);
537 return $fh;
538 }
539
540 sub ct_make_path {
541 my $self = shift;
542 if ($self->{id_map}) {
543 my $opts = pop;
544 if (ref($opts) eq 'HASH') {
545 $opts->{owner} = $self->{rootuid} if !defined($self->{owner});
546 $opts->{group} = $self->{rootgid} if !defined($self->{group});
547 }
548 File::Path::make_path(@_, $opts);
549 } else {
550 File::Path::make_path(@_);
551 }
552 }
553
554 sub ct_symlink {
555 my ($self, $old, $new) = @_;
556 return if $self->ct_is_file_ignored($new);
557 return CORE::symlink($old, $new);
558 }
559
560 sub ct_readlink {
561 my ($self, $name) = @_;
562 return CORE::readlink($name);
563 }
564
565 sub ct_file_exists {
566 my ($self, $file) = @_;
567 return -f $file;
568 }
569
570 sub ct_is_directory {
571 my ($self, $file) = @_;
572 return -d $file;
573 }
574
575 sub ct_is_symlink {
576 my ($self, $file) = @_;
577 return -l $file;
578 }
579
580 sub ct_is_executable {
581 my ($self, $file) = @_;
582 return -x $file
583 }
584
585 sub ct_stat {
586 my ($self, $file) = @_;
587 return File::stat::stat($file);
588 }
589
590 sub ct_file_read_firstline {
591 my ($self, $file) = @_;
592 return PVE::Tools::file_read_firstline($file);
593 }
594
595 sub ct_file_get_contents {
596 my ($self, $file) = @_;
597 return PVE::Tools::file_get_contents($file);
598 }
599
600 sub ct_file_set_contents {
601 my ($self, $file, $data, $perms) = @_;
602 return if $self->ct_is_file_ignored($file);
603 PVE::Tools::file_set_contents($file, $data, $perms);
604 $self->ct_reset_ownership($file);
605 }
606
607 # Modify a marked portion of a file.
608 # Optionally if the file becomes empty it will be deleted.
609 sub ct_modify_file {
610 my ($self, $file, $data, %options) = @_;
611 return if $self->ct_is_file_ignored($file);
612
613 my $head = "# --- BEGIN PVE ---\n";
614 my $tail = "# --- END PVE ---\n";
615 my $perms = $options{perms};
616 $data .= "\n" if $data && $data !~ /\n$/;
617
618 if (!$self->ct_file_exists($file)) {
619 $self->ct_file_set_contents($file, $head.$data.$tail, $perms) if $data;
620 return;
621 }
622
623 my $old = $self->ct_file_get_contents($file);
624 my @lines = split(/\n/, $old);
625
626 my ($beg, $end);
627 foreach my $i (0..(@lines-1)) {
628 my $line = $lines[$i];
629 $beg = $i if !defined($beg) &&
630 $line =~ /^#\s*---\s*BEGIN\s*PVE\s*/;
631 $end = $i if !defined($end) && defined($beg) &&
632 $line =~ /^#\s*---\s*END\s*PVE\s*/i;
633 last if defined($beg) && defined($end);
634 }
635
636 if (defined($beg) && defined($end)) {
637 # Found a section
638 if ($data) {
639 chomp $tail;
640 splice @lines, $beg, $end-$beg+1, $head.$data.$tail;
641 } else {
642 if ($beg == 0 && $end == (@lines-1)) {
643 $self->ct_unlink($file) if $options{delete};
644 return;
645 }
646 splice @lines, $beg, $end-$beg+1, $head.$data.$tail;
647 }
648 $self->ct_file_set_contents($file, join("\n", @lines) . "\n");
649 } elsif ($data) {
650 # No section found
651 my $content = join("\n", @lines);
652 chomp $content;
653 if (!$content && !$data && $options{delete}) {
654 $self->ct_unlink($file);
655 return;
656 }
657 $content .= "\n";
658 $data = $head.$data.$tail;
659 if ($options{replace}) {
660 $self->ct_file_set_contents($file, $data, $perms);
661 } elsif ($options{prepend}) {
662 $self->ct_file_set_contents($file, $data . $content, $perms);
663 } else { # append
664 $self->ct_file_set_contents($file, $content . $data, $perms);
665 }
666 }
667 }
668
669 sub remove_pve_sections {
670 my ($data) = @_;
671
672 my $head = "# --- BEGIN PVE ---";
673 my $tail = "# --- END PVE ---";
674
675 # Remove the sections enclosed with the above headers and footers.
676 # from a line (^) starting with '\h*$head'
677 # to a line (the other ^) starting with '\h*$tail' up to including that
678 # line's end (.*?$).
679 return $data =~ s/^\h*\Q$head\E.*^\h*\Q$tail\E.*?$//rgms;
680 }
681
682 1;