]> git.proxmox.com Git - pmg-api.git/blobdiff - PMG/Config.pm
add pmg_verify_tls_policy_strict and use it in API
[pmg-api.git] / PMG / Config.pm
index b3b6643a33d05bd66d3dd6f27089f021fb4d6a81..a3fa4ace73d59979fd12e057776123ab6717cf93 100755 (executable)
@@ -92,12 +92,24 @@ sub properties {
            type => 'string',
            pattern => "http://.*",
        },
+       avast => {
+           description => "Use Avast Virus Scanner (/bin/scan). You need to buy and install 'Avast Core Security' before you can enable this feature.",
+           type => 'boolean',
+           default => 0,
+       },
+       clamav => {
+           description => "Use ClamAV Virus Scanner. This is the default virus scanner and is enabled by default.",
+           type => 'boolean',
+           default => 1,
+       },
     };
 }
 
 sub options {
     return {
        advfilter => { optional => 1 },
+       avast => { optional => 1 },
+       clamav => { optional => 1 },
        statlifetime => { optional => 1 },
        dailyreport => { optional => 1 },
        demo => { optional => 1 },
@@ -144,6 +156,13 @@ sub properties {
            description => "Whitelist legitimate bounce relays.",
            type => 'string',
        },
+       clamav_heuristic_score => {
+           description => "Score for ClamaAV heuristics (Google Safe Browsing database, PhishingScanURLs, ...).",
+           type => 'integer',
+           minimum => 0,
+           maximum => 1000,
+           default => 3,
+       },
        bounce_score => {
            description => "Additional score for bounce mails.",
            type => 'integer',
@@ -160,7 +179,7 @@ sub properties {
            description => "Maximum size of spam messages in bytes.",
            type => 'integer',
            minimum => 64,
-           default => 200*1024,
+           default => 256*1024,
        },
     };
 }
@@ -172,6 +191,7 @@ sub options {
        wl_bounce_relays => { optional => 1 },
        languages => { optional => 1 },
        use_bayes => { optional => 1 },
+       clamav_heuristic_score => { optional => 1 },
        bounce_score => { optional => 1 },
        rbl_checks => { optional => 1 },
        maxspamsize => { optional => 1 },
@@ -220,9 +240,22 @@ sub properties {
            default => 1,
        },
        hostname => {
-           description => "Quarantine Host. Usefule if you run a Cluster and want users to connect to a specific host.",
+           description => "Quarantine Host. Useful if you run a Cluster and want users to connect to a specific host.",
            type => 'string', format => 'address',
        },
+       port => {
+           description => "Quarantine Port. Useful if you have a reverse proxy or port forwarding for the webinterface. Only used for the generated Spam report.",
+           type => 'integer',
+           minimum => 1,
+           maximum => 65535,
+           default => 8006,
+       },
+       protocol => {
+           description => "Quarantine Webinterface Protocol. Useful if you have a reverse proxy for the webinterface. Only used for the generated Spam report.",
+           type => 'string',
+           enum => [qw(http https)],
+           default => 'https',
+       },
        mailfrom => {
            description => "Text for 'From' header in daily spam report mails.",
            type => 'string',
@@ -239,6 +272,8 @@ sub options {
        reportstyle => { optional => 1 },
        viewimages => { optional => 1 },
        allowhrefs => { optional => 1 },
+       port => { optional => 1 },
+       protocol => { optional => 1 },
     };
 }
 
@@ -403,14 +438,14 @@ sub properties {
            type => 'integer',
            minimum => 1,
            maximum => 65535,
-           default => 25,
+           default => 26,
        },
        ext_port => {
            description => "SMTP port number for incoming mail (untrusted). This must be a different number than 'int_port'.",
            type => 'integer',
            minimum => 1,
            maximum => 65535,
-           default => 26,
+           default => 25,
        },
        relay => {
            description => "The default mail delivery transport (incoming mails).",
@@ -432,6 +467,13 @@ sub properties {
            description => "When set, all outgoing mails are deliverd to the specified smarthost.",
            type => 'string', format => 'address',
        },
+       smarthostport => {
+           description => "SMTP port number for smarthost.",
+           type => 'integer',
+           minimum => 1,
+           maximum => 65535,
+           default => 25,
+       },
        banner => {
            description => "ESMTP banner.",
            type => 'string',
@@ -501,11 +543,6 @@ sub properties {
            minimum => 0,
            default => 4,
        },
-       use_rbl => {
-           description => "Use Realtime Blacklists.",
-           type => 'boolean',
-           default => 1,
-       },
        tls => {
            description => "Enable TLS.",
            type => 'boolean',
@@ -553,7 +590,13 @@ sub properties {
        },
        dnsbl_sites => {
            description => "Optional list of DNS white/blacklist domains (see postscreen_dnsbl_sites parameter).",
-           type => 'string',
+           type => 'string', format => 'dnsbl-entry-list',
+       },
+       dnsbl_threshold => {
+           description => "The inclusive lower bound for blocking a remote SMTP client, based on its combined DNSBL score (see postscreen_dnsbl_threshold parameter).",
+           type => 'integer',
+           minimum => 0,
+           default => 1
        },
     };
 }
@@ -563,6 +606,7 @@ sub options {
        int_port => { optional => 1 },
        ext_port => { optional => 1 },
        smarthost => { optional => 1 },
+       smarthostport => { optional => 1 },
        relay => { optional => 1 },
        relayport => { optional => 1 },
        relaynomx => { optional => 1 },
@@ -571,7 +615,6 @@ sub options {
        max_smtpd_out => { optional => 1 },
        greylist => { optional => 1 },
        helotests => { optional => 1 },
-       use_rbl => { optional => 1 },
        tls => { optional => 1 },
        tlslog => { optional => 1 },
        tlsheader => { optional => 1 },
@@ -588,6 +631,7 @@ sub options {
        message_rate_limit => { optional => 1 },
        verifyreceivers => { optional => 1 },
        dnsbl_sites => { optional => 1 },
+       dnsbl_threshold => { optional => 1 },
     };
 }
 
@@ -618,6 +662,7 @@ PMG::Config::Base->init();
 
 PVE::JSONSchema::register_format(
     'transport-domain', \&pmg_verify_transport_domain);
+
 sub pmg_verify_transport_domain {
     my ($name, $noerr) = @_;
 
@@ -631,6 +676,43 @@ sub pmg_verify_transport_domain {
     return $name;
 }
 
+PVE::JSONSchema::register_format(
+    'transport-domain-or-email', \&pmg_verify_transport_domain_or_email);
+
+sub pmg_verify_transport_domain_or_email {
+    my ($name, $noerr) = @_;
+
+    my $namere = "([a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?)";
+
+    # email address
+    if ($name =~ m/^(?:[^\s\/\@]+\@)(${namere}\.)*${namere}$/) {
+       return $name;
+    }
+
+    # like dns-name, but can contain leading dot
+    if ($name !~ /^\.?(${namere}\.)*${namere}$/) {
+          return undef if $noerr;
+          die "value does not look like a valid transport domain or email address\n";
+    }
+    return $name;
+}
+
+PVE::JSONSchema::register_format(
+    'dnsbl-entry', \&pmg_verify_dnsbl_entry);
+
+sub pmg_verify_dnsbl_entry {
+    my ($name, $noerr) = @_;
+
+    # like dns-name, but can contain trailing weight: 'domain*<WEIGHT>'
+    my $namere = "([a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?)";
+
+    if ($name !~ /^(${namere}\.)*${namere}(\*\-?\d+)?$/) {
+          return undef if $noerr;
+          die "value '$name' does not look like a valid dnsbl entry\n";
+    }
+    return $name;
+}
+
 sub new {
     my ($type) = @_;
 
@@ -819,10 +901,6 @@ PVE::INotify::register_file('domains', $domainsfilename,
 
 my $mynetworks_filename = "/etc/pmg/mynetworks";
 
-sub postmap_pmg_mynetworks {
-    PMG::Utils::run_postmap($mynetworks_filename);
-}
-
 sub read_pmg_mynetworks {
     my ($filename, $fh) = @_;
 
@@ -866,7 +944,88 @@ PVE::INotify::register_file('mynetworks', $mynetworks_filename,
                            \&write_pmg_mynetworks,
                            undef, always_call_parser => 1);
 
+PVE::JSONSchema::register_format(
+    'tls-policy', \&pmg_verify_tls_policy);
+
+# TODO: extend to parse attributes of the policy
+my $VALID_TLS_POLICY_RE = qr/none|may|encrypt|dane|dane-only|fingerprint|verify|secure/;
+sub pmg_verify_tls_policy {
+    my ($policy, $noerr) = @_;
+
+    if ($policy !~ /^$VALID_TLS_POLICY_RE\b/) {
+          return undef if $noerr;
+          die "value '$policy' does not look like a valid tls policy\n";
+    }
+    return $policy;
+}
+
+sub pmg_verify_tls_policy_strict {
+    my ($policy) = @_;
+
+    return $policy
+       if ($policy =~ /^$VALID_TLS_POLICY_RE$/);
+
+    return undef;
+}
+
+sub read_tls_policy {
+    my ($filename, $fh) = @_;
+
+    return {} if !defined($fh);
+
+    my $tls_policy = {};
+
+    while (defined(my $line = <$fh>)) {
+       chomp $line;
+       next if $line =~ m/^\s*$/;
+       next if $line =~ m/^#(.*)\s*$/;
+
+       my $parse_error = sub {
+           my ($err) = @_;
+           die "parse error in '$filename': $line - $err";
+       };
+
+       if ($line =~ m/^(\S+)\s+(.+)\s*$/) {
+           my ($domain, $policy) = ($1, $2);
+
+           eval {
+               pmg_verify_transport_domain($domain);
+               pmg_verify_tls_policy($policy);
+           };
+           if (my $err = $@) {
+               $parse_error->($err);
+               next;
+           }
+
+           $tls_policy->{$domain} = {
+                   domain => $domain,
+                   policy => $policy,
+           };
+       } else {
+           $parse_error->('wrong format');
+       }
+    }
+
+    return $tls_policy;
+}
+
+sub write_tls_policy {
+    my ($filename, $fh, $tls_policy) = @_;
+
+    return if !$tls_policy;
+
+    foreach my $domain (sort keys %$tls_policy) {
+       my $entry = $tls_policy->{$domain};
+       PVE::Tools::safe_print(
+           $filename, $fh, "$entry->{domain} $entry->{policy}\n");
+    }
+}
+
 my $tls_policy_map_filename = "/etc/pmg/tls_policy";
+PVE::INotify::register_file('tls_policy', $tls_policy_map_filename,
+                           \&read_tls_policy,
+                           \&write_tls_policy,
+                           undef, always_call_parser => 1);
 
 sub postmap_tls_policy {
     PMG::Utils::run_postmap($tls_policy_map_filename);
@@ -904,7 +1063,7 @@ sub read_transport_map {
        if ($line =~ m/^(\S+)\s+smtp:(\S+):(\d+)\s*$/) {
            my ($domain, $host, $port) = ($1, $2, $3);
 
-           eval { pmg_verify_transport_domain($domain); };
+           eval { pmg_verify_transport_domain_or_email($domain); };
            if (my $err = $@) {
                $parse_error->($err);
                next;
@@ -977,9 +1136,7 @@ sub get_template_vars {
 
     my $nodename = PVE::INotify::nodename();
     my $int_ip = PMG::Cluster::remote_node_ip($nodename);
-    my $int_net_cidr = PMG::Utils::find_local_network_for_ip($int_ip);
     $vars->{ipconfig}->{int_ip} = $int_ip;
-    # $vars->{ipconfig}->{int_net_cidr} = $int_net_cidr;
 
     my $transportnets = [];
 
@@ -998,11 +1155,32 @@ sub get_template_vars {
     $vars->{postfix}->{transportnets} = join(' ', @$transportnets);
 
     my $mynetworks = [ '127.0.0.0/8', '[::1]/128' ];
-    push @$mynetworks, @$transportnets;
-    push @$mynetworks, $int_net_cidr;
-    push @$mynetworks, 'hash:/etc/pmg/mynetworks';
+
+    if (my $int_net_cidr = PMG::Utils::find_local_network_for_ip($int_ip, 1)) {
+       if ($int_net_cidr =~ m/^($IPV6RE)\/(\d+)$/) {
+           push @$mynetworks, "[$1]/$2";
+       } else {
+           push @$mynetworks, $int_net_cidr;
+       }
+    } else {
+       if ($int_ip =~ m/^$IPV6RE$/) {
+           push @$mynetworks, "[$int_ip]/128";
+       } else {
+           push @$mynetworks, "$int_ip/32";
+       }
+    }
 
     my $netlist = PVE::INotify::read_file('mynetworks');
+    foreach my $cidr (keys %$netlist) {
+       if ($cidr =~ m/^($IPV6RE)\/(\d+)$/) {
+           push @$mynetworks, "[$1]/$2";
+       } else {
+           push @$mynetworks, $cidr;
+       }
+    }
+
+    push @$mynetworks, @$transportnets;
+
     # add default relay to mynetworks
     if (my $relay = $self->get('mail', 'relay')) {
        if ($relay =~ m/^$IPV4RE$/) {
@@ -1022,16 +1200,26 @@ sub get_template_vars {
        $vars->{postfix}->{dnsbl_sites} = join(',', @dnsbl_sites);
     }
 
+    $vars->{postfix}->{dnsbl_threshold} = $self->get('mail', 'dnsbl_threshold');
+
     my $usepolicy = 0;
     $usepolicy = 1 if $self->get('mail', 'greylist') ||
-       $self->get('mail', 'spf') ||  $self->get('mail', 'use_rbl');
+       $self->get('mail', 'spf');
     $vars->{postfix}->{usepolicy} = $usepolicy;
 
+    if ($int_ip =~ m/^$IPV6RE$/) {
+        $vars->{postfix}->{int_ip} = "[$int_ip]";
+    } else {
+        $vars->{postfix}->{int_ip} = $int_ip;
+    }
+
     my $resolv = PVE::INotify::read_file('resolvconf');
     $vars->{dns}->{hostname} = $nodename;
-    $vars->{dns}->{domain} = $resolv->{search};
 
-    my $wlbr = "$nodename.$resolv->{search}";
+    my $domain = $resolv->{search} // 'localdomain';
+    $vars->{dns}->{domain} = $domain;
+
+    my $wlbr = "$nodename.$domain";
     foreach my $r (PVE::Tools::split_list($vars->{pmg}->{spam}->{wl_bounce_relays})) {
        $wlbr .= " $r"
     }
@@ -1305,11 +1493,6 @@ sub rewrite_config_postfix {
     my ($self, $rulecache) = @_;
 
     # make sure we have required files (else postfix start fails)
-    postmap_pmg_domains();
-    postmap_pmg_transport();
-    postmap_pmg_mynetworks();
-    postmap_tls_policy();
-
     IO::File->new($transport_map_filename, 'a', 0644);
 
     my $changes = 0;
@@ -1327,6 +1510,12 @@ sub rewrite_config_postfix {
     $changes = 1 if $self->rewrite_config_file(
        'master.cf.in', '/etc/postfix/master.cf');
 
+    # make sure we have required files (else postfix start fails)
+    # Note: postmap need a valid /etc/postfix/main.cf configuration
+    postmap_pmg_domains();
+    postmap_pmg_transport();
+    postmap_tls_policy();
+
     rewrite_postfix_whitelist($rulecache) if $rulecache;
 
     # make sure aliases.db is up to date