]> git.proxmox.com Git - pve-container.git/blob - src/PVE/LXC/Setup.pm
don't create deprecated rsa1 ssh keys
[pve-container.git] / src / PVE / LXC / Setup.pm
1 package PVE::LXC::Setup;
2
3 use strict;
4 use warnings;
5 use POSIX;
6 use PVE::Tools;
7
8 use PVE::LXC::Setup::Debian;
9 use PVE::LXC::Setup::Ubuntu;
10 use PVE::LXC::Setup::CentOS;
11 use PVE::LXC::Setup::Fedora;
12 use PVE::LXC::Setup::SUSE;
13 use PVE::LXC::Setup::ArchLinux;
14 use PVE::LXC::Setup::Alpine;
15 use PVE::LXC::Setup::Gentoo;
16
17 my $plugins = {
18 debian => 'PVE::LXC::Setup::Debian',
19 ubuntu => 'PVE::LXC::Setup::Ubuntu',
20 centos => 'PVE::LXC::Setup::CentOS',
21 fedora => 'PVE::LXC::Setup::Fedora',
22 opensuse => 'PVE::LXC::Setup::SUSE',
23 archlinux => 'PVE::LXC::Setup::ArchLinux',
24 alpine => 'PVE::LXC::Setup::Alpine',
25 gentoo => 'PVE::LXC::Setup::Gentoo',
26 };
27
28 my $autodetect_type = sub {
29 my ($rootdir) = @_;
30
31 my $lsb_fn = "$rootdir/etc/lsb-release";
32 if (-f $lsb_fn) {
33 my $data = PVE::Tools::file_get_contents($lsb_fn);
34 if ($data =~ m/^DISTRIB_ID=Ubuntu$/im) {
35 return 'ubuntu';
36 }
37 }
38
39 if (-f "$rootdir/etc/debian_version") {
40 return "debian";
41 } elsif (-f "$rootdir/etc/SuSE-brand" || -f "$rootdir/etc/SuSE-release") {
42 return "opensuse";
43 } elsif (-f "$rootdir/etc/fedora-release") {
44 return "fedora";
45 } elsif (-f "$rootdir/etc/centos-release" || -f "$rootdir/etc/redhat-release") {
46 return "centos";
47 } elsif (-f "$rootdir/etc/arch-release") {
48 return "archlinux";
49 } elsif (-f "$rootdir/etc/alpine-release") {
50 return "alpine";
51 } elsif (-f "$rootdir/etc/gentoo-release") {
52 return "gentoo";
53 }
54 die "unable to detect OS distribution\n";
55 };
56
57 sub new {
58 my ($class, $conf, $rootdir, $type) = @_;
59
60 die "no root directory\n" if !$rootdir || $rootdir eq '/';
61
62 my $self = bless { conf => $conf, rootdir => $rootdir};
63
64 if ($conf->{ostype} && $conf->{ostype} eq 'unmanaged') {
65 return $self;
66 } elsif (!defined($type)) {
67 # try to autodetect type
68 $type = &$autodetect_type($rootdir);
69 my $expected_type = $conf->{ostype} || $type;
70
71 die "got unexpected ostype ($type != $expected_type)\n"
72 if $type ne $expected_type;
73 }
74
75 my $plugin_class = $plugins->{$type} ||
76 "no such OS type '$type'\n";
77
78 my $plugin = $plugin_class->new($conf, $rootdir);
79 $self->{plugin} = $plugin;
80 $self->{in_chroot} = 0;
81
82 # Cache some host files we need access to:
83 $plugin->{host_resolv_conf} = PVE::INotify::read_file('resolvconf');
84
85 # pass on user namespace information:
86 my ($id_map, $rootuid, $rootgid) = PVE::LXC::parse_id_maps($conf);
87 if (@$id_map) {
88 $plugin->{id_map} = $id_map;
89 $plugin->{rootuid} = $rootuid;
90 $plugin->{rootgid} = $rootgid;
91 }
92
93 return $self;
94 }
95
96 sub protected_call {
97 my ($self, $sub) = @_;
98
99 die "internal error" if !$self->{plugin};
100
101 # avoid recursion:
102 return $sub->() if $self->{in_chroot};
103
104 my $rootdir = $self->{rootdir};
105 if (!-d "$rootdir/dev" && !mkdir("$rootdir/dev")) {
106 die "failed to create temporary /dev directory: $!\n";
107 }
108
109 my $child = fork();
110 die "fork failed: $!\n" if !defined($child);
111
112 if (!$child) {
113 # avoid recursive forks
114 $self->{in_chroot} = 1;
115 $self->{plugin}->{in_chroot} = 1;
116 eval {
117 chroot($rootdir) or die "failed to change root to: $rootdir: $!\n";
118 chdir('/') or die "failed to change to root directory\n";
119 $sub->();
120 };
121 if (my $err = $@) {
122 warn $err;
123 POSIX::_exit(1);
124 }
125 POSIX::_exit(0);
126 }
127 while (waitpid($child, 0) != $child) {}
128 if ($? != 0) {
129 my $method = (caller(1))[3];
130 die "error in setup task $method\n";
131 }
132 }
133
134 sub template_fixup {
135 my ($self) = @_;
136
137 return if !$self->{plugin}; # unmanaged
138
139 my $code = sub {
140 $self->{plugin}->template_fixup($self->{conf});
141 };
142 $self->protected_call($code);
143 }
144
145 sub setup_network {
146 my ($self) = @_;
147
148 return if !$self->{plugin}; # unmanaged
149
150 my $code = sub {
151 $self->{plugin}->setup_network($self->{conf});
152 };
153 $self->protected_call($code);
154 }
155
156 sub set_hostname {
157 my ($self) = @_;
158
159 return if !$self->{plugin}; # unmanaged
160
161 my $code = sub {
162 $self->{plugin}->set_hostname($self->{conf});
163 };
164 $self->protected_call($code);
165 }
166
167 sub set_dns {
168 my ($self) = @_;
169
170 return if !$self->{plugin}; # unmanaged
171
172 my $code = sub {
173 $self->{plugin}->set_dns($self->{conf});
174 };
175 $self->protected_call($code);
176 }
177
178 sub setup_init {
179 my ($self) = @_;
180
181 return if !$self->{plugin}; # unmanaged
182
183 my $code = sub {
184 $self->{plugin}->setup_init($self->{conf});
185 };
186 $self->protected_call($code);
187 }
188
189 sub set_user_password {
190 my ($self, $user, $pw) = @_;
191
192 return if !$self->{plugin}; # unmanaged
193
194 my $code = sub {
195 $self->{plugin}->set_user_password($self->{conf}, $user, $pw);
196 };
197 $self->protected_call($code);
198 }
199
200 sub rewrite_ssh_host_keys {
201 my ($self) = @_;
202
203 return if !$self->{plugin}; # unmanaged
204
205 my $conf = $self->{conf};
206 my $plugin = $self->{plugin};
207 my $rootdir = $self->{rootdir};
208
209 return if ! -d "$rootdir/etc/ssh";
210
211 my $keynames = {
212 rsa => 'ssh_host_rsa_key',
213 dsa => 'ssh_host_dsa_key',
214 ecdsa => 'ssh_host_ecdsa_key',
215 ed25519 => 'ssh_host_ed25519_key',
216 };
217
218 my $hostname = $conf->{hostname} || 'localhost';
219 $hostname =~ s/\..*$//;
220 my $ssh_comment = "root\@$hostname";
221
222 my $keygen_outfunc = sub {
223 my $line = shift;
224
225 print "done: $line\n"
226 if ($line =~ m/^([0-9a-f]{2}:)+[0-9a-f]{2}\s+\Q$ssh_comment\E$/i);
227 };
228
229 # Create temporary keys in /tmp on the host
230 my $keyfiles = {};
231 foreach my $keytype (keys %$keynames) {
232 my $basename = $keynames->{$keytype};
233 my $file = "/tmp/$$.$basename";
234 print "Creating SSH host key '$basename' - this may take some time ...\n";
235 my $cmd = ['ssh-keygen', '-f', $file, '-t', $keytype,
236 '-N', '', '-C', $ssh_comment];
237 PVE::Tools::run_command($cmd, outfunc => $keygen_outfunc);
238 $keyfiles->{"/etc/ssh/$basename"} = [PVE::Tools::file_get_contents($file), 0600];
239 $keyfiles->{"/etc/ssh/$basename.pub"} = [PVE::Tools::file_get_contents("$file.pub"), 0644];
240 unlink $file;
241 unlink "$file.pub";
242 }
243
244 # Write keys out in a protected call
245
246 my $code = sub {
247 foreach my $file (keys %$keyfiles) {
248 $plugin->ct_file_set_contents($file, @{$keyfiles->{$file}});
249 }
250 };
251 $self->protected_call($code);
252 }
253
254 sub pre_start_hook {
255 my ($self) = @_;
256
257 return if !$self->{plugin}; # unmanaged
258
259 my $code = sub {
260 # Create /fastboot to skip run fsck
261 $self->{plugin}->ct_file_set_contents('/fastboot', '');
262
263 $self->{plugin}->pre_start_hook($self->{conf});
264 };
265 $self->protected_call($code);
266 }
267
268 sub post_create_hook {
269 my ($self, $root_password, $ssh_keys) = @_;
270
271 return if !$self->{plugin}; # unmanaged
272
273 my $code = sub {
274 $self->{plugin}->post_create_hook($self->{conf}, $root_password, $ssh_keys);
275 };
276 $self->protected_call($code);
277 $self->rewrite_ssh_host_keys();
278 }
279
280 1;