]> git.proxmox.com Git - pve-manager.git/blobdiff - PVE/NodeConfig.pm
api: backup: update: check permissions of delete params too
[pve-manager.git] / PVE / NodeConfig.pm
index 2de9015e46e8c9b006832439597159a049d333c0..941e6009e8cbd9b681c3f69f341bcf30be8b7ed0 100644 (file)
@@ -25,6 +25,16 @@ PVE::JSONSchema::register_format('pve-acme-domain', sub {
     die "value '$domain' does not look like a valid domain name!\n";
 });
 
+PVE::JSONSchema::register_format('pve-acme-alias', sub {
+    my ($alias, $noerr) = @_;
+
+    my $label = qr/[a-z0-9_][a-z0-9_-]*/i;
+
+    return $alias if $alias =~ /^$label(?:\.$label)+$/;
+    return undef if $noerr;
+    die "value '$alias' does not look like a valid alias name!\n";
+});
+
 sub config_file {
     my ($node) = @_;
 
@@ -38,7 +48,7 @@ sub load_config {
     my $raw = eval { PVE::Tools::file_get_contents($filename); };
     return {} if !$raw;
 
-    return parse_node_config($raw);
+    return parse_node_config($raw, $filename);
 }
 
 sub write_config {
@@ -70,7 +80,9 @@ sub lock_config {
 my $confdesc = {
     description => {
        type => 'string',
-       description => 'Node description/comment.',
+       description => "Description for the Node. Shown in the web-interface node notes panel."
+           ." This is saved as comment inside the configuration file.",
+       maxLength => 64 * 1024,
        optional => 1,
     },
     wakeonlan => {
@@ -107,7 +119,7 @@ my $acme_domain_desc = {
     },
     alias => {
        type => 'string',
-       format => 'pve-acme-domain',
+       format => 'pve-acme-alias',
        format_description => 'domain',
        description => 'Alias for the Domain to verify ACME Challenge over DNS',
        optional => 1,
@@ -141,74 +153,19 @@ for my $i (0..$MAXDOMAINS) {
     };
 };
 
-sub check_type {
-    my ($key, $value) = @_;
-
-    die "unknown setting '$key'\n" if !$confdesc->{$key};
-
-    my $type = $confdesc->{$key}->{type};
-
-    if (!defined($value)) {
-       die "got undefined value\n";
-    }
-
-    if ($value =~ m/[\n\r]/) {
-       die "property contains a line feed\n";
-    }
-
-    if ($type eq 'boolean') {
-       return 1 if ($value eq '1') || ($value =~ m/^(on|yes|true)$/i);
-       return 0 if ($value eq '0') || ($value =~ m/^(off|no|false)$/i);
-       die "type check ('boolean') failed - got '$value'\n";
-    } elsif ($type eq 'integer') {
-       return int($1) if $value =~ m/^(\d+)$/;
-       die "type check ('integer') failed - got '$value'\n";
-    } elsif ($type eq 'number') {
-       return $value if $value =~ m/^(\d+)(\.\d+)?$/;
-       die "type check ('number') failed - got '$value'\n";
-    } elsif ($type eq 'string') {
-       if (my $fmt = $confdesc->{$key}->{format}) {
-           PVE::JSONSchema::check_format($fmt, $value);
-           return $value;
-       } elsif (my $pattern = $confdesc->{$key}->{pattern}) {
-           if ($value !~ m/^$pattern$/) {
-               die "value does not match the regex pattern\n";
-           }
-       }
-       return $value;
-    } else {
-       die "internal error"
-    }
-}
+my $conf_schema = {
+    type => 'object',
+    properties => $confdesc,
+};
 
-sub parse_node_config {
-    my ($content) = @_;
+sub parse_node_config : prototype($$) {
+    my ($content, $filename) = @_;
 
     return undef if !defined($content);
+    my $digest = Digest::SHA::sha1_hex($content);
 
-    my $conf = {
-       digest => Digest::SHA::sha1_hex($content),
-    };
-    my $descr = '';
-
-    my @lines = split(/\n/, $content);
-    foreach my $line (@lines) {
-       if ($line =~ /^\#(.*)\s*$/ || $line =~ /^description:\s*(.*\S)\s*$/) {
-           $descr .= PVE::Tools::decode_text($1) . "\n";
-           next;
-       }
-       if ($line =~ /^([a-z][a-z-_]*\d*):\s*(\S.*)\s*$/) {
-           my $key = $1;
-           my $value = $2;
-           $value = eval { check_type($key, $value) };
-           die "cannot parse value of '$key' in node config: $@" if $@;
-           $conf->{$key} = $value;
-       } else {
-           warn "cannot parse line '$line' in node config\n";
-       }
-    }
-
-    $conf->{description} = $descr if $descr;
+    my $conf = PVE::JSONSchema::parse_config($conf_schema, $filename, $content, 'description');
+    $conf->{digest} = $digest;
 
     return $conf;
 }
@@ -236,6 +193,9 @@ sub write_node_config {
     return $raw;
 }
 
+# we always convert domain values to lower case, since DNS entries are not case
+# sensitive and ACME implementations might convert the ordered identifiers
+# to lower case
 sub get_acme_conf {
     my ($node_conf, $noerr) = @_;
 
@@ -251,7 +211,12 @@ sub get_acme_conf {
            die $err;
        }
        my $standalone_domains = delete($res->{domains}) // '';
+       $res->{domains} = {};
        for my $domain (split(";", $standalone_domains)) {
+           $domain = lc($domain);
+           die "duplicate domain '$domain' in ACME config properties\n"
+               if defined($res->{domains}->{$domain});
+
            $res->{domains}->{$domain}->{plugin} = 'standalone';
            $res->{domains}->{$domain}->{_configkey} = 'acme';
        }
@@ -270,7 +235,7 @@ sub get_acme_conf {
            return undef if $noerr;
            die $err;
        }
-       my $domain = delete $parsed->{domain};
+       my $domain = lc(delete $parsed->{domain});
        if (my $exists = $res->{domains}->{$domain}) {
            return undef if $noerr;
            die "duplicate domain '$domain' in ACME config properties"