]>
Commit | Line | Data |
---|---|---|
c4f78bb7 FG |
1 | package PVE::NodeConfig; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | use PVE::CertHelpers; | |
7 | use PVE::JSONSchema qw(get_standard_option); | |
8 | use PVE::Tools qw(file_get_contents file_set_contents lock_file); | |
cc442d3e WL |
9 | use PVE::ACME; |
10 | ||
de9aa678 TL |
11 | use PVE::API2::ACMEPlugin; |
12 | ||
667198c4 TL |
13 | # register up to 5 domain names per node for now |
14 | my $MAXDOMAINS = 5; | |
c4f78bb7 FG |
15 | |
16 | my $node_config_lock = '/var/lock/pvenode.lock'; | |
17 | ||
18 | PVE::JSONSchema::register_format('pve-acme-domain', sub { | |
19 | my ($domain, $noerr) = @_; | |
20 | ||
4dab82e3 | 21 | my $label = qr/[a-z0-9][a-z0-9_-]*/i; |
c4f78bb7 FG |
22 | |
23 | return $domain if $domain =~ /^$label(?:\.$label)+$/; | |
24 | return undef if $noerr; | |
85b275b1 | 25 | die "value '$domain' does not look like a valid domain name!\n"; |
c4f78bb7 FG |
26 | }); |
27 | ||
d1dd680b | 28 | PVE::JSONSchema::register_format('pve-acme-alias', sub { |
b3fbf2ac | 29 | my ($alias, $noerr) = @_; |
d1dd680b FM |
30 | |
31 | my $label = qr/[a-z0-9_][a-z0-9_-]*/i; | |
32 | ||
b3fbf2ac | 33 | return $alias if $alias =~ /^$label(?:\.$label)+$/; |
d1dd680b | 34 | return undef if $noerr; |
b3fbf2ac | 35 | die "value '$alias' does not look like a valid alias name!\n"; |
d1dd680b FM |
36 | }); |
37 | ||
c4f78bb7 FG |
38 | sub config_file { |
39 | my ($node) = @_; | |
40 | ||
41 | return "/etc/pve/nodes/${node}/config"; | |
42 | } | |
43 | ||
44 | sub load_config { | |
45 | my ($node) = @_; | |
46 | ||
47 | my $filename = config_file($node); | |
48 | my $raw = eval { PVE::Tools::file_get_contents($filename); }; | |
49 | return {} if !$raw; | |
50 | ||
9556af61 | 51 | return parse_node_config($raw, $filename); |
c4f78bb7 FG |
52 | } |
53 | ||
54 | sub write_config { | |
55 | my ($node, $conf) = @_; | |
56 | ||
57 | my $filename = config_file($node); | |
58 | ||
59 | my $raw = write_node_config($conf); | |
60 | ||
61 | PVE::Tools::file_set_contents($filename, $raw); | |
62 | } | |
63 | ||
64 | sub lock_config { | |
3c194d9e FG |
65 | my ($node, $realcode, @param) = @_; |
66 | ||
67 | # make sure configuration file is up-to-date | |
68 | my $code = sub { | |
69 | PVE::Cluster::cfs_update(); | |
70 | $realcode->(@_); | |
71 | }; | |
c4f78bb7 FG |
72 | |
73 | my $res = lock_file($node_config_lock, 10, $code, @param); | |
74 | ||
75 | die $@ if $@; | |
76 | ||
77 | return $res; | |
78 | } | |
79 | ||
80 | my $confdesc = { | |
81 | description => { | |
82 | type => 'string', | |
e466f896 TL |
83 | description => "Description for the Node. Shown in the web-interface node notes panel." |
84 | ." This is saved as comment inside the configuration file.", | |
85 | maxLength => 64 * 1024, | |
c4f78bb7 FG |
86 | optional => 1, |
87 | }, | |
1ff3fef0 TL |
88 | 'startall-onboot-delay' => { |
89 | description => 'Initial delay in seconds, before starting all the Virtual Guests with on-boot enabled.', | |
90 | type => 'integer', | |
91 | minimum => 0, | |
92 | maximum => 300, | |
93 | default => 0, | |
94 | optional => 1, | |
95 | }, | |
c4f78bb7 FG |
96 | }; |
97 | ||
3f83a033 CE |
98 | my $wakeonlan_desc = { |
99 | mac => { | |
100 | type => 'string', | |
101 | description => 'MAC address for wake on LAN', | |
102 | format => 'mac-addr', | |
103 | format_description => 'MAC address', | |
104 | default_key => 1, | |
105 | }, | |
869c155c CE |
106 | 'bind-interface' => { |
107 | type => 'string', | |
108 | description => 'Bind to this interface when sending wake on LAN packet', | |
109 | format => 'pve-iface', | |
110 | format_description => 'bind interface', | |
111 | optional => 1, | |
112 | }, | |
a967ff65 CE |
113 | 'broadcast-address' => { |
114 | type => 'string', | |
115 | description => 'IPv4 broadcast address to use when sending wake on LAN packet', | |
116 | format => 'ipv4', | |
117 | format_description => 'IPv4 broadcast address', | |
118 | optional => 1, | |
119 | }, | |
3f83a033 CE |
120 | }; |
121 | ||
122 | $confdesc->{wakeonlan} = { | |
123 | type => 'string', | |
124 | description => 'Node specific wake on LAN settings.', | |
125 | format => $wakeonlan_desc, | |
126 | optional => 1, | |
127 | }; | |
128 | ||
ab53e139 | 129 | my $acme_domain_desc = { |
cc442d3e WL |
130 | domain => { |
131 | type => 'string', | |
132 | format => 'pve-acme-domain', | |
133 | format_description => 'domain', | |
134 | description => 'domain for this node\'s ACME certificate', | |
dcfb9f2c | 135 | default_key => 1, |
cc442d3e WL |
136 | }, |
137 | plugin => { | |
138 | type => 'string', | |
139 | format => 'pve-configid', | |
9d17b945 | 140 | description => 'The ACME plugin ID', |
cc442d3e | 141 | format_description => 'name of the plugin configuration', |
9d17b945 FG |
142 | optional => 1, |
143 | default => 'standalone', | |
cc442d3e WL |
144 | }, |
145 | alias => { | |
146 | type => 'string', | |
d1dd680b | 147 | format => 'pve-acme-alias', |
cc442d3e WL |
148 | format_description => 'domain', |
149 | description => 'Alias for the Domain to verify ACME Challenge over DNS', | |
150 | optional => 1, | |
151 | }, | |
152 | }; | |
cc442d3e | 153 | |
c4f78bb7 FG |
154 | my $acmedesc = { |
155 | account => get_standard_option('pve-acme-account-name'), | |
156 | domains => { | |
157 | type => 'string', | |
158 | format => 'pve-acme-domain-list', | |
159 | format_description => 'domain[;domain;...]', | |
160 | description => 'List of domains for this node\'s ACME certificate', | |
cc442d3e | 161 | optional => 1, |
c4f78bb7 FG |
162 | }, |
163 | }; | |
c4f78bb7 FG |
164 | |
165 | $confdesc->{acme} = { | |
166 | type => 'string', | |
167 | description => 'Node specific ACME settings.', | |
168 | format => $acmedesc, | |
169 | optional => 1, | |
170 | }; | |
171 | ||
cc442d3e | 172 | for my $i (0..$MAXDOMAINS) { |
ab53e139 | 173 | $confdesc->{"acmedomain$i"} = { |
cc442d3e | 174 | type => 'string', |
ab53e139 FG |
175 | description => 'ACME domain and validation plugin', |
176 | format => $acme_domain_desc, | |
cc442d3e WL |
177 | optional => 1, |
178 | }; | |
179 | }; | |
180 | ||
9556af61 WB |
181 | my $conf_schema = { |
182 | type => 'object', | |
183 | properties => $confdesc, | |
184 | }; | |
e79894eb | 185 | |
9556af61 WB |
186 | sub parse_node_config : prototype($$) { |
187 | my ($content, $filename) = @_; | |
c4f78bb7 FG |
188 | |
189 | return undef if !defined($content); | |
9556af61 | 190 | my $digest = Digest::SHA::sha1_hex($content); |
c4f78bb7 | 191 | |
9556af61 WB |
192 | my $conf = PVE::JSONSchema::parse_config($conf_schema, $filename, $content, 'description'); |
193 | $conf->{digest} = $digest; | |
c4f78bb7 FG |
194 | |
195 | return $conf; | |
196 | } | |
197 | ||
198 | sub write_node_config { | |
199 | my ($conf) = @_; | |
200 | ||
201 | my $raw = ''; | |
202 | # add description as comment to top of file | |
203 | my $descr = $conf->{description} || ''; | |
204 | foreach my $cl (split(/\n/, $descr)) { | |
205 | $raw .= '#' . PVE::Tools::encode_text($cl) . "\n"; | |
206 | } | |
207 | ||
208 | for my $key (sort keys %$conf) { | |
209 | next if ($key eq 'description'); | |
210 | next if ($key eq 'digest'); | |
211 | ||
212 | my $value = $conf->{$key}; | |
213 | die "detected invalid newline inside property '$key'\n" | |
214 | if $value =~ m/\n/; | |
215 | $raw .= "$key: $value\n"; | |
216 | } | |
217 | ||
218 | return $raw; | |
219 | } | |
220 | ||
3f83a033 CE |
221 | sub get_wakeonlan_config { |
222 | my ($node_conf) = @_; | |
223 | ||
224 | $node_conf //= {}; | |
225 | ||
226 | my $res = {}; | |
227 | if (defined($node_conf->{wakeonlan})) { | |
228 | $res = eval { | |
229 | PVE::JSONSchema::parse_property_string($wakeonlan_desc, $node_conf->{wakeonlan}) | |
230 | }; | |
231 | die $@ if $@; | |
232 | } | |
233 | ||
234 | return $res; | |
235 | } | |
236 | ||
4aa89cc0 FG |
237 | # we always convert domain values to lower case, since DNS entries are not case |
238 | # sensitive and ACME implementations might convert the ordered identifiers | |
239 | # to lower case | |
c30e112e | 240 | sub get_acme_conf { |
7ffd1550 | 241 | my ($node_conf, $noerr) = @_; |
c4f78bb7 | 242 | |
7ffd1550 | 243 | $node_conf //= {}; |
c4f78bb7 | 244 | |
c30e112e | 245 | my $res = {}; |
7ffd1550 | 246 | if (defined($node_conf->{acme})) { |
b232807d | 247 | $res = eval { |
7ffd1550 | 248 | PVE::JSONSchema::parse_property_string($acmedesc, $node_conf->{acme}) |
c30e112e | 249 | }; |
7ffd1550 | 250 | if (my $err = $@) { |
c30e112e | 251 | return undef if $noerr; |
7ffd1550 | 252 | die $err; |
c30e112e | 253 | } |
b232807d | 254 | my $standalone_domains = delete($res->{domains}) // ''; |
b7def515 | 255 | $res->{domains} = {}; |
7ffd1550 | 256 | for my $domain (split(";", $standalone_domains)) { |
4aa89cc0 FG |
257 | $domain = lc($domain); |
258 | die "duplicate domain '$domain' in ACME config properties\n" | |
259 | if defined($res->{domains}->{$domain}); | |
260 | ||
b232807d | 261 | $res->{domains}->{$domain}->{plugin} = 'standalone'; |
75afd54a | 262 | $res->{domains}->{$domain}->{_configkey} = 'acme'; |
b232807d | 263 | } |
c4f78bb7 | 264 | } |
b232807d FG |
265 | |
266 | $res->{account} //= 'default'; | |
c30e112e WL |
267 | |
268 | for my $index (0..$MAXDOMAINS) { | |
7ffd1550 | 269 | my $domain_rec = $node_conf->{"acmedomain$index"}; |
c30e112e WL |
270 | next if !defined($domain_rec); |
271 | ||
b232807d | 272 | my $parsed = eval { |
7ffd1550 | 273 | PVE::JSONSchema::parse_property_string($acme_domain_desc, $domain_rec) |
c30e112e | 274 | }; |
7ffd1550 | 275 | if (my $err = $@) { |
c30e112e | 276 | return undef if $noerr; |
7ffd1550 | 277 | die $err; |
c30e112e | 278 | } |
4aa89cc0 | 279 | my $domain = lc(delete $parsed->{domain}); |
75afd54a | 280 | if (my $exists = $res->{domains}->{$domain}) { |
b232807d | 281 | return undef if $noerr; |
75afd54a TL |
282 | die "duplicate domain '$domain' in ACME config properties" |
283 | ." 'acmedomain$index' and '$exists->{_configkey}'\n"; | |
b232807d | 284 | } |
9d17b945 | 285 | $parsed->{plugin} //= 'standalone'; |
de9aa678 TL |
286 | |
287 | my $plugin_id = $parsed->{plugin}; | |
288 | if ($plugin_id ne 'standalone') { | |
289 | my $plugins = PVE::API2::ACMEPlugin::load_config(); | |
290 | die "plugin '$plugin_id' for domain '$domain' not found!\n" | |
291 | if !$plugins->{ids}->{$plugin_id}; | |
292 | } | |
293 | ||
75afd54a | 294 | $parsed->{_configkey} = "acmedomain$index"; |
b232807d | 295 | $res->{domains}->{$domain} = $parsed; |
c30e112e WL |
296 | } |
297 | ||
c4f78bb7 FG |
298 | return $res; |
299 | } | |
300 | ||
75afd54a TL |
301 | # expects that basic format verification was already done, this is more higher |
302 | # level verification | |
303 | sub verify_conf { | |
304 | my ($node_conf) = @_; | |
305 | ||
306 | # verify ACME domain uniqueness | |
307 | my $tmp = get_acme_conf($node_conf); | |
308 | ||
309 | # TODO: what else? | |
310 | ||
311 | return 1; # OK | |
312 | } | |
313 | ||
c4f78bb7 FG |
314 | sub get_nodeconfig_schema { |
315 | return $confdesc; | |
316 | } | |
317 | ||
318 | 1; |