From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 61E9C81F4C for ; Fri, 26 Nov 2021 14:56:13 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 5835D191F8 for ; Fri, 26 Nov 2021 14:55:43 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id B5411191CC for ; Fri, 26 Nov 2021 14:55:39 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 871F646AA6 for ; Fri, 26 Nov 2021 14:55:39 +0100 (CET) From: Wolfgang Bumiller To: pmg-devel@lists.proxmox.com Date: Fri, 26 Nov 2021 14:55:06 +0100 Message-Id: <20211126135524.117846-3-w.bumiller@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211126135524.117846-1-w.bumiller@proxmox.com> References: <20211126135524.117846-1-w.bumiller@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.455 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [group.pm, rule.pm, report.pm, tfaconfig.pm, sacustom.pm, object.pm, quarantine.pm] Subject: [pmg-devel] [PATCH api 2/6] add PMG::TFAConfig module X-BeenThere: pmg-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Mail Gateway development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 26 Nov 2021 13:56:13 -0000 Signed-off-by: Wolfgang Bumiller --- src/Makefile | 1 + src/PMG/TFAConfig.pm | 80 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 src/PMG/TFAConfig.pm diff --git a/src/Makefile b/src/Makefile index eac682b..de05aa0 100644 --- a/src/Makefile +++ b/src/Makefile @@ -82,6 +82,7 @@ LIBSOURCES = \ PMG/Quarantine.pm \ PMG/Report.pm \ PMG/SACustom.pm \ + PMG/TFAConfig.pm \ PMG/RuleDB/Group.pm \ PMG/RuleDB/Rule.pm \ PMG/RuleDB/Object.pm \ diff --git a/src/PMG/TFAConfig.pm b/src/PMG/TFAConfig.pm new file mode 100644 index 0000000..998e266 --- /dev/null +++ b/src/PMG/TFAConfig.pm @@ -0,0 +1,80 @@ +package PMG::TFAConfig; + +use strict; +use warnings; + +use PVE::Tools; +use PVE::INotify; +use PVE::JSONSchema qw(get_standard_option); +use PVE::Exception qw(raise); + +use PMG::Utils; +use PMG::UserConfig; + +use base 'PMG::RS::TFA'; + +my $inotify_file_id = 'pmg-tfa.json'; +my $config_filename = '/etc/pmg/tfa.json'; + +sub new { + my ($type) = @_; + + my $class = ref($type) || $type; + + my $cfg = PVE::INotify::read_file($inotify_file_id); + + return bless $cfg, $class; +} + +sub write { + my ($self) = @_; + + PVE::INotify::write_file($inotify_file_id, $self); +} + +# This lives in `UserConfig` in order to enforce lock order. +sub lock_config { + return PMG::UserConfig::lock_tfa_config(@_); +} + +my sub read_tfa_conf : prototype($$) { + my ($filename, $fh) = @_; + + my $raw; + if ($fh) { + $raw = do { local $/ = undef; <$fh> }; + } else { + $raw = '{}'; + } + + my $cfg = PMG::RS::TFA->new($raw); + + # Purge invalid users: + my $usercfg = PMG::UserConfig->new(); + foreach my $user ($cfg->users()->@*) { + if (!$usercfg->lookup_user_data($user, 1)) { + $cfg->remove_user($user); + } + } + + return $cfg; +} + +my sub write_tfa_conf : prototype($$$) { + my ($filename, $fh, $cfg) = @_; + + chmod(0600, $fh); + + PVE::Tools::safe_print($filename, $fh, $cfg->SUPER::write()); +} + +PVE::INotify::register_file($inotify_file_id, $config_filename, + \&read_tfa_conf, + \&write_tfa_conf, + undef, + always_call_parser => 1, + # the parser produces a rust TfaConfig object, + # Clone::clone would break this + noclone => 1); + +1; -- 2.30.2