User Tools

Site Tools


pages:scripts:postfixdeletemailsfromsasluser

postfixdeletemailsfromsasluser.pl

#!/usr/bin/perl -w
##
## This script was created by modifying pfdel. Unfortunately, pfdel had no license
## information and no contact information to contribute the script back.
##
## This script is public domain with no copyright claimed.
##
## pfsasl - deletes messages containing specified sasl_username from
## Postfix queue. Matches either sender or recipient address.
##
## Usage: pfsasl <sasl_username>
##
use strict;
# Change these paths if necessary.
my $LISTQ = "/usr/sbin/postqueue -p";
my $POSTCAT = "/usr/sbin/postcat -q";
my $POSTSUPER = "/usr/sbin/postsuper";
my $sasl_user = "";
my $qid = "";
my $euid = $>;
if ( @ARGV !=  1 ) {
	die "Usage: pfsasl <sasl_username>\n";
} else {
	$sasl_user = $ARGV[0];
}
if ( $euid != 0 ) {
	die "You must be root to delete queue files.\n";
}
open(QUEUE, "$LISTQ |") ||
  die "Can't get pipe to $LISTQ: $!\n";
my $entry = <QUEUE>;# skip single header line
  $/ = "";# Rest of queue entries print on
# multiple lines.
while ( $entry = <QUEUE> ) {
	($qid) = split(/\s+/, $entry, 2);$qid =~ s/[\*\!]//;
    open(MESSAGE, "$POSTCAT $qid |") ||
          die "Can't get pipe to $POSTCAT: $!\n";
    my $msg = '';#create variable
    $/ = ""; # multiline thingie from above
    while ( $msg = <MESSAGE> ) {
    	if($msg =~ /sasl_username=$sasl_user/m ) {
        	#we have a match!
            print "Found match in $qid...\n";

            # delete message here after debugging the rest is finished
            #
            # Execute postsuper -d with the queue id.
            # postsuper provides feedback when it deletes
            # messages. Let its output go through.
            #
            if ( system($POSTSUPER, "-d", $qid) != 0 ) {
            	# If postsuper has a problem, bail.
                die "Error executing $POSTSUPER: error " .
            	    "code " .  ($?/256) . "\n";
            }
     	next;
     	}
	}
} # end of main WHILE loop
close(QUEUE);
if (! $qid ) {
     	die "No messages with the sasl_username <$sasl_user> " .
       	"found in queue.\n";
}
exit 0;

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
pages/scripts/postfixdeletemailsfromsasluser.txt · Last modified: 2022/12/31 01:02 by Heiko Mischer