patch for reading domains from an sql database Hi, see the attachment for an updated version. changes: - §DOMAIN§ from the --sqlstmt parameter will be substituted with the current sender domain. from no on only one domain is fetched from the database and no longer all. - the mysql connetion will be reopend for every mail. had some problems with connection idle timeouts. - the mysql password will be substituted with ***** (5x*, regardless how long the original password is) so it is no longer visible on the commandline via 'ps' Greets Sven Index: dkfilter/scripts/dkfilter.out =================================================================== --- dkfilter.orig/scripts/dkfilter.out +++ dkfilter/scripts/dkfilter.out @@ -26,6 +26,7 @@ use Getopt::Long; use Pod::Usage; use IO::File; use Sys::Syslog; +use DBI; use DKMessage; use MySmtpServer; @@ -46,6 +47,11 @@ my $daemonize; my $pidfile; my $debug; my $help; +my $sqlStmt; +my @domains = (); +my ($dbs, $dbu, $dbp); +my $zero = join(" ",@ARGV); + GetOptions( "reject-fail" => \$reject_fail, "reject-error" => \$reject_error, @@ -60,6 +66,7 @@ GetOptions( "daemonize" => \$daemonize, "pidfile=s" => \$pidfile, "debug" => \$debug, + "sqlstmt=s" => \$sqlStmt, "help|?" => \$help) or pod2usage(2); pod2usage(1) if $help; @@ -89,7 +96,20 @@ unless (defined $domain_arg) { pod2usage("Error: domain not specified"); } -my @domains = split(/,\s*/, $domain_arg); + + +if ($domain_arg =~ /^dbi:/) +{ + ($dbs, $dbu, $dbp) = split(/#/,$domain_arg); + $zero =~ s/$dbp/*****/; + $0 .= " $zero"; + push @domains, "localdomain"; +} +else +{ + @domains = split(/,\s*/, $domain_arg); +} + unless (@domains) { pod2usage("Error: domain not specified"); @@ -110,6 +130,21 @@ main->run( pid_file => $pidfile, ); +sub reread_domains +{ + my $senderDomain = $_[0]; + my $dbh = DBI->connect($dbs, $dbu, $dbp) || die("DBI->connect(): ".DBI->errstr."\n"); + my $tmpSqlStmt = $sqlStmt; + $tmpSqlStmt =~ s/§DOMAIN§/$senderDomain/; + my $result = $dbh->selectcol_arrayref($tmpSqlStmt); + die "reread_domains(): ",DBI->errstr,"\n" if DBI->errstr; + @domains = @$result; + $dbh->disconnect(); + unless (@domains) { + push @domains, "localdomain"; + } +} + sub setup_client_socket { # create an object for sending the outgoing SMTP commands @@ -161,6 +196,10 @@ sub handle_end_of_data $mess = DKMessage->new_from_handle($fh); # determine what domain to use + if ($domain_arg =~ /^dbi:/) + { + reread_domains(lc($mess->senderdomain)); + } my $domain; if ($domain = lc $mess->senderdomain) {