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 1369069764 for ; Tue, 19 Jan 2021 11:39:01 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id A1D2A1CE3F for ; Tue, 19 Jan 2021 11:38:30 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [212.186.127.180]) (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 F22EA1CE0D for ; Tue, 19 Jan 2021 11:38:28 +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 BA931440B4 for ; Tue, 19 Jan 2021 11:38:28 +0100 (CET) From: Stoiko Ivanov To: pmg-devel@lists.proxmox.com Date: Tue, 19 Jan 2021 11:38:14 +0100 Message-Id: <20210119103815.15934-6-s.ivanov@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210119103815.15934-1-s.ivanov@proxmox.com> References: <20210119103815.15934-1-s.ivanov@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.068 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust 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. [spamassassin.pm, utils.pm] Subject: [pmg-devel] [PATCH pmg-api v3 5/6] api: spamassassin: update local channels 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: Tue, 19 Jan 2021 10:39:01 -0000 This patch adds a helper to loop over all present Spamassassin channels files in /etc/mail/spamassassin/channel.d and: * import the included gpg key into sa-update's keyring * run sa-update for each channel separately the verbose argument of the helper is for reusing the code in pmg-daily (where we only want to log errors and be less informative) the $SA_UPDATE variable hardcoding the path of /usr/bin/sa-update was dropped in favor of using 'sa-update' without path since we do have a sensible setting of PATH everywhere, and hardcoding paths is problematic (especially in usr-merged systems). The choice of invoking sa-update for each channel separately, instead of providing multiple '--channel' and '--gpgkey' options to a single command was made to prevent downloading signatures, which were signed by a key not configured for the channel. Importing gpg-keys is also done with individual sa-update invocations, because sa-update only imports the last present --import argument (wrong use of Getopt::Long) Signed-off-by: Stoiko Ivanov --- src/PMG/API2/SpamAssassin.pm | 8 ++++---- src/PMG/Utils.pm | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/PMG/API2/SpamAssassin.pm b/src/PMG/API2/SpamAssassin.pm index 6b9f8f9..441fd7e 100644 --- a/src/PMG/API2/SpamAssassin.pm +++ b/src/PMG/API2/SpamAssassin.pm @@ -18,8 +18,6 @@ use Mail::SpamAssassin; use base qw(PVE::RESTHandler); -my $SAUPDATE = '/usr/bin/sa-update'; - __PACKAGE__->register_method ({ name => 'index', path => '', @@ -108,7 +106,7 @@ __PACKAGE__->register_method({ } # call sa-update to see if updates are available - my $cmd = "$SAUPDATE -v --checkonly --channel $channel"; + my $cmd = "sa-update -v --checkonly --channel $channel"; PVE::Tools::run_command($cmd, noerr => 1, logfunc => sub { my ($line) = @_; @@ -171,9 +169,11 @@ __PACKAGE__->register_method({ $ENV{http_proxy} = $http_proxy; } - my $cmd = "$SAUPDATE -v"; + my $cmd = "sa-update -v"; PVE::Tools::run_command($cmd, noerr => 1); + + PMG::Utils::update_local_spamassassin_channels(1); }; return $rpcenv->fork_worker('saupdate', undef, $authuser, $realcmd); diff --git a/src/PMG/Utils.pm b/src/PMG/Utils.pm index e3863b0..149bcdc 100644 --- a/src/PMG/Utils.pm +++ b/src/PMG/Utils.pm @@ -1475,4 +1475,32 @@ sub local_spamassassin_channels { return $res; } +sub update_local_spamassassin_channels { + my ($verbose) = @_; + # import all configured channel's gpg-keys to sa-update's keyring + my $localchannels = PMG::Utils::local_spamassassin_channels(); + for my $channel (@$localchannels) { + my $importcmd = ['sa-update', '--import', $channel->{filename}]; + push @$importcmd, '-v' if $verbose; + + print "Importing gpg key from $channel->{filename}\n" if $verbose; + PVE::Tools::run_command($importcmd); + } + + my $fresh_updates = 0; + + for my $channel (@$localchannels) { + my $cmd = ['sa-update', '--channel', $channel->{channelurl}, '--gpgkey', $channel->{keyid}]; + push @$cmd, '-v' if $verbose; + + print "Updating $channel->{channelurl}\n" if $verbose; + my $ret = PVE::Tools::run_command($cmd, noerr => 1); + die "updating $channel->{channelurl} failed - sa-update exited with $ret\n" if $ret >= 2; + + $fresh_updates = 1 if $ret == 0; + } + + return $fresh_updates +} + 1; -- 2.20.1