]> git.proxmox.com Git - pmg-api.git/blobdiff - PMG/API2/Config.pm
add PMG::API2::DestinationTLSPolicy
[pmg-api.git] / PMG / API2 / Config.pm
index f07cb489e38eb2e8b8282452f6c88d6eac6f4647..3b688faafbdbd41b4ab50073ca5e0f3e01908600 100644 (file)
@@ -10,13 +10,77 @@ use HTTP::Status qw(:constants);
 use Storable qw(dclone);
 use PVE::JSONSchema qw(get_standard_option);
 use PVE::RESTHandler;
+use Time::HiRes qw();
 
 use PMG::Config;
+use PMG::API2::RuleDB;
+use PMG::API2::LDAP;
+use PMG::API2::Domains;
+use PMG::API2::Transport;
+use PMG::API2::Cluster;
+use PMG::API2::MyNetworks;
+use PMG::API2::SMTPWhitelist;
+use PMG::API2::MimeTypes;
+use PMG::API2::Fetchmail;
+use PMG::API2::DestinationTLSPolicy;
 
 use base qw(PVE::RESTHandler);
 
 my $section_type_enum = PMG::Config::Base->lookup_types();
 
+__PACKAGE__->register_method ({
+    subclass => "PMG::API2::RuleDB",
+    path => 'ruledb',
+});
+
+__PACKAGE__->register_method ({
+    subclass => "PMG::API2::SMTPWhitelist",
+    path => 'whitelist',
+});
+
+__PACKAGE__->register_method ({
+    subclass => "PMG::API2::LDAP",
+    path => 'ldap',
+});
+
+__PACKAGE__->register_method ({
+    subclass => "PMG::API2::Domains",
+    path => 'domains',
+                             });
+
+__PACKAGE__->register_method ({
+    subclass => "PMG::API2::Fetchmail",
+    path => 'fetchmail',
+});
+
+__PACKAGE__->register_method ({
+    subclass => "PMG::API2::Transport",
+    path => 'transport',
+});
+
+__PACKAGE__->register_method ({
+    subclass => "PMG::API2::MyNetworks",
+    # set fragment delimiter (no subdirs) - we need that, because CIDRs
+    # contain a slash '/'
+    fragmentDelimiter => '',
+    path => 'mynetworks',
+});
+
+__PACKAGE__->register_method ({
+    subclass => "PMG::API2::Cluster",
+    path => 'cluster',
+});
+
+__PACKAGE__->register_method ({
+    subclass => "PMG::API2::MimeTypes",
+    path => 'mimetypes',
+});
+
+__PACKAGE__->register_method ({
+    subclass => "PMG::API2::DestinationTLSPolicy",
+    path => 'tlspolicy',
+});
+
 __PACKAGE__->register_method ({
     name => 'index', 
     path => '',
@@ -40,7 +104,21 @@ __PACKAGE__->register_method ({
        my $res = [];
        foreach my $section (@$section_type_enum) {
            push @$res, { section => $section };
-       }   
+       }
+
+       push @$res, { section => 'ldap' };
+       push @$res, { section => 'mynetworks' };
+       push @$res, { section => 'mimetypes' };
+       push @$res, { section => 'users' };
+       push @$res, { section => 'domains' };
+       push @$res, { section => 'fetchmail' };
+       push @$res, { section => 'cluster' };
+       push @$res, { section => 'ruledb' };
+       push @$res, { section => 'transport' };
+       push @$res, { section => 'whitelist' };
+       push @$res, { section => 'regextest' };
+       push @$res, { section => 'tlspolicy' };
+
        return $res;
     }});
 
@@ -49,7 +127,7 @@ my $api_read_config_section = sub {
 
     my $cfg = PMG::Config->new();
 
-    my $data = dclone($cfg->{ids}->{"section_$section"});
+    my $data = dclone($cfg->{ids}->{$section} // {});
     $data->{digest} = $cfg->{digest};
     delete $data->{type};
 
@@ -71,17 +149,19 @@ my $api_update_config_section = sub {
           if !$delete_str && !scalar(keys %$param);
 
        foreach my $opt (PVE::Tools::split_list($delete_str)) {
-          delete $ids->{"section_$section"}->{$opt};
+          delete $ids->{$section}->{$opt};
        }
 
        my $plugin = PMG::Config::Base->lookup($section);
        my $config = $plugin->check_config($section, $param, 0, 1);
 
        foreach my $p (keys %$config) {
-          $ids->{"section_$section"}->{$p} = $config->{$p};
+          $ids->{$section}->{$p} = $config->{$p};
        }
 
        $cfg->write();
+
+       $cfg->rewrite_config(undef, 1);
    };
 
    PMG::Config::lock_config($code, "update config section '$section' failed");
@@ -89,14 +169,14 @@ my $api_update_config_section = sub {
 
 foreach my $section (@$section_type_enum) {
 
-    next if $section eq 'ldap'; # fixme
-
     my $plugin = PMG::Config::Base->lookup($section);
 
     __PACKAGE__->register_method ({
        name => "read_${section}_section",
        path => $section,
        method => 'GET',
+       proxyto => 'master',
+       permissions => { check => [ 'admin', 'audit' ] },
        description => "Read $section configuration properties.",
        parameters => {
            additionalProperties => 0,
@@ -113,6 +193,9 @@ foreach my $section (@$section_type_enum) {
        name => "update_${section}_section",
        path => $section,
        method => 'PUT',
+       proxyto => 'master',
+       protected => 1,
+       permissions => { check => [ 'admin' ] },
        description => "Update $section configuration properties.",
        parameters => $plugin->updateSchema(1),
        returns => { type => 'null' },
@@ -125,4 +208,55 @@ foreach my $section (@$section_type_enum) {
        }});
 }
 
+__PACKAGE__->register_method({
+    name => 'regextest',
+    path => 'regextest',
+    method => 'POST',
+    protected => 0,
+    permissions => { check => [ 'admin', 'qmanager', 'audit' ] },
+    description => "Test Regex",
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           regex => {
+               type => 'string',
+               description => 'The Regex to test',
+               maxLength => 1024,
+           },
+           text => {
+               type => 'string',
+               description => 'The String to test',
+               maxLength => 1024,
+           }
+       },
+    },
+    returns => {
+       type => 'number',
+    },
+    code => sub {
+       my ($param) = @_;
+
+       my $text = $param->{text};
+       my $regex = $param->{regex};
+
+       my $regex_check = sub {
+           my $start_time = [Time::HiRes::gettimeofday];
+           my $match = 0;
+           if ($text =~ /$regex/) {
+               $match = 1;
+           }
+           my $elapsed = Time::HiRes::tv_interval($start_time) * 1000;
+           die "The Regular Expression '$regex' did not match the text '$text' (elapsed time: $elapsed ms)\n"
+               if !$match;
+           return $elapsed;
+       };
+
+       my $elapsed = PVE::Tools::run_fork_with_timeout(2, $regex_check);
+       if ($elapsed eq '') {
+           die "The Regular Expression timed out\n";
+       }
+
+       return $elapsed;
+    }});
+
 1;