feat: started switching to real queueing

This commit is contained in:
Dominik Meyer 2025-02-08 19:02:24 +01:00
parent 959dc8966e
commit 7ca06e4dc6
Signed by: byterazor
GPG Key ID: EABDA0FD5981BC97
2 changed files with 120 additions and 117 deletions

View File

@ -29,7 +29,7 @@ sub createStatements
$self->{fetch_all_sth} = $self->{dbh}->prepare("select username,dovecot_server from email_address where domain=? and isFetchAll=1"); $self->{fetch_all_sth} = $self->{dbh}->prepare("select username,dovecot_server from email_address where domain=? and isFetchAll=1");
$self->{fetch_dovecot_details_sth} = $self->{dbh}->prepare("select hostname, port from dovecot_server where name=?"); $self->{fetch_dovecot_details_sth} = $self->{dbh}->prepare("select username, hostname, port from dovecot_server RIGHT JOIN email_address ON dovecot_server.name = email_address.dovecot_server where alias=? and domain=?");
} }
@ -73,46 +73,93 @@ sub createDSN
$self->log(LOGDEBUG, "created DSN " . $self->{dsn}); $self->log(LOGDEBUG, "created DSN " . $self->{dsn});
} }
sub updateTransactionWithRecipientInfo sub updateTransactionWithSmtpInfo
{ {
my ($self, $transaction, $rcpt_row) = @_; my ($self, $transaction, $recipient) = @_;
return 0 unless defined($rcpt_row); my $queue = $transaction->notes("queue") || {};
return 0 unless defined($rcpt_row->{username}); my $rcpt = $recipient->user . "@" . $recipient->host;
return 0 unless defined($rcpt_row->{dovecot_server});
$queue->{$rcpt}->{destination} = "relay";
$queue->{$rcpt}->{protocol} = "smtp";
$queue->{$rcpt}->{host} = $self->qp->config("relay_server");
$transaction->notes("queue", $queue);
return 0;
}
my $dovecot_server = $rcpt_row->{dovecot_server}; sub updateTransactionWithLmtpInfo
my $username = $rcpt_row->{username}; {
my ($self, $transaction, $recipient) = @_;
$self->log(LOGNOTICE,"transaction update " . $dovecot_server . "/" . $rcpt_row->{dovecot_server} . " " . $username); my $result = $self->{fetch_dovecot_details_sth}->execute($recipient->user, $recipient->host);
my $result = $self->{fetch_dovecot_details_sth}->execute($dovecot_server);
if (!$result) if (!$result)
{ {
$self->log(LOGERROR, "Failed to fetch dovecot server information for user " . $username . " and dovecot server " . $dovecot_server ); $self->log(LOGERROR, "Failed to fetch dovecot information from the database"); return -1;
return 0;
} }
if ($self->{fetch_dovecot_details_sth}->rows == 0) if ($self->{fetch_dovecot_details_sth}->rows == 0)
{ {
$self->log(LOGERROR, "no dovecot server information found for user " . $username . " and dovecot server " . $dovecot_server ); $self->log(LOGERROR, "no dovecot information in database found");
return 0; return -1;
}
elsif ($self->{fetch_dovecot_details_sth}->rows > 1)
{
$self->log(LOGERROR, "too many dovecot entries in the database");
return -1;
} }
my $row = $self->{fetch_dovecot_details_sth}->fetchrow_hashref(); my $row = $self->{fetch_dovecot_details_sth}->fetchrow_hashref;
my $username = $row->{username}
my $hostname = $row->{hostname}; my $hostname = $row->{hostname};
my $port = $row->{port}; my $port = $row->{port};
$transaction->notes("queue", "lmtp://$hostname:$port"); my $queue = $transaction->notes("queue") || {};
$transaction->notes("destination-user",$username); my $rcpt = $recipient->user . "@" . $recipient->host;
$queue->{$rcpt}->{destination} = "local";
$queue->{$rcpt}->{protocol} = "lmtp";
$queue->{$rcpt}->{host} = $hostname;
$queue->{$rcpt}->{port} = $port;
$queue->{$rcpt}->{user} = $username;
$transaction->notes("queue", $queue);
$self->log(LOGNOTICE, "Setting LMTP server to dovecot on $hostname:$port for user: $username"); $self->log(LOGNOTICE, "Setting LMTP server to dovecot on $hostname:$port for user: $username");
return 1; return 1;
} }
sub CheckRecipient
{
my ($self, $recipient) = @_;
$self->connect();
$self->createStatements();
my $result = $self->{rcpt_sth}->execute($recipient->user, $recipient->host);
if (!$result)
{
$self->log(LOGERROR, "Failed to fetch recipient information from the database"); return -1;
}
if ($self->{rcpt_sth}->rows == 1)
{
$self->log(LOGDEBUG, " found recipient in database");
return 1;
}
elsif ($self->{rcpt_sth}->rows > 1)
{
$self->log(LOGERROR,"found multiple users for same recipient in database. Something wrong with database? (" . $recipient->user . '@' . $recipient->host . ")" );
return -2;
}
return 0;
}
sub hook_quit { sub hook_quit {
my ($self, $transaction) = @_; my ($self, $transaction) = @_;
@ -126,42 +173,24 @@ sub hook_rcpt {
return DECLINED unless $recipient->host && $recipient->user; return DECLINED unless $recipient->host && $recipient->user;
$self->connect();
$self->createStatements(); my $rcptValid = $self->CheckRecipient($recipient);
my $result = $self->{rcpt_sth}->execute($recipient->user, $recipient->host); if ($rcptValid == 1 )
if (!$result)
{ {
$self->log(LOGERROR, "Failed to fetch recipient information from the database"); return DECLINED; $self->updateTransactionWithLmtpInfo($transaction, $recipient) || return DENYSOFT, "Temporary failure, try again later";
}
if ($self->{rcpt_sth}->rows == 1)
{
$self->log(LOGDEBUG, " found recipient in database");
my $ret = $self->updateTransactionWithRecipientInfo($transaction, $self->{rcpt_sth}->fetchrow_hashref );
if (!$ret)
{
$self->log(LOGERROR, "Failed to update transaction with recipient information");
return DENYSOFT;
}
return OK; return OK;
} }
elsif ($self->{rcpt_sth}->rows > 1) elseif( $self->is_immune())
{ {
$self->log(LOGERROR,"found multiple users for same recipient in database. Something wrong with database? (" . $recipient->user . '@' . $recipient->host . ")" ); $self->updateTransactionWithSmtpInfo($transaction, $recipient) || return DENYSOFT, "Temporary failure, try again later";
return DENYSOFT;
}
$result = $self->{fetch_all_sth}->execute($recipient->host);
if ($self->{fetch_all_sth}->rows > 0)
{
$self->log(LOGDEBUG, " found fetchall for doamin in database");
return OK; return OK;
} }
elseif ($rcptValid == -1)
{
return DENYSOFT, "Temporary failure, try again later";
}
return DECLINED;
return Qpsmtpd::DSN->relaying_denied();
} }

View File

@ -4,92 +4,66 @@ use warnings;
use Qpsmtpd::Constants; use Qpsmtpd::Constants;
use Qpsmtpd::DSN; use Qpsmtpd::DSN;
use Net::LMTP; use Net::LMTP;
use Minion;
sub register { sub register {
my ($self, $qp) = (shift, shift); my ($self, $qp) = (shift, shift);
$self->{lmtp_rcpt_based} = $qp->config("lmtp_rcpt_based") || 0; # some default values
$self->{lmtp_host} = $qp->config("lmtp_host"); $self->{database} = $qp->config("queue_mysql_database") || "mail";
$self->{lmtp_port} = $qp->config("lmtp_port") || 24; $self->{host} = $qp->config("queue_mysql_host") || "localhost";
$self->{qp}= $qp; $self->{port} = $qp->config("queue_mysql_port") || "3306";
$self->{enabled} = 1; $self->{user} = $qp->config("queue_mysql_user") || "qpsmtpd";
$self->{pass} = $qp->config("queue_mysql_password");
$self->createDSN();
if (!$self->{lmtp_rcpt_based} && !$self->{lmtp_host}) {
$self->{enabled} = 0;
$self->log(LOGERROR, "No LMTP host configured, disabling plugin\n");
return;
}
} }
sub lmtp_transfer sub createDSN()
{ {
my ($self, $transaction, $lmtp_host, $lmtp_port, $lmtp_user) = @_; my $self = shift;
my $dsn = "mysql://" . $self->{user} . ":" . $self->{pass} . "@" . $self->{host} . ":" . $self->{port} . "/" . $self->{database};
my $lmtp = Net::LMTP->new( $self->{dsn} = $dsn;
$lmtp_host,
Port => $lmtp_port,
Timeout => 60,
Hello => $self->qp->config("me"),
) || die $!;
$lmtp->mail($transaction->sender->address || "")
or return DECLINED, "Unable to queue message during mail($!)";
$lmtp->to($lmtp_user) or return DECLINED, "Unable to queue message during to($!)";
$lmtp->data() or return DECLINED, "Unable to queue message during data($!)";
$lmtp->datasend($transaction->header->as_string) or return DECLINED, "Unable to queue message during datasend ($!) ";
$transaction->body_resetpos;
while (my $line = $transaction->body_getline) {
$lmtp->datasend($line)
or return DECLINED, "Unable to queue message during datasendbody($!)";
}
$lmtp->dataend() or return DECLINED, "Unable to queue message during dataend($!)";
my $qid = $lmtp->message();
my @list = split(' ', $qid);
$qid = pop(@list);
$lmtp->quit() or return DECLINED, "Unable to queue message during quit($!)";
$self->log(LOGINFO, "finished queueing");
return OK, "queued as $qid";
return DECLINED;
} }
sub hook_connect
{
my ($self, $transaction) = @_;
$self->{minion} = Minion->new(mysql => $self->{dsn});
}
sub hook_queue { sub hook_queue {
my ($self, $transaction) = @_; my ($self, $transaction) = @_;
return DECLINED unless $self->{enabled}; my @notQueued;
my $lmtp_host; my $queue = $transaction->notes("queue") || {};
my $lmtp_port;
my $lmtp_user;
if ($self->{lmtp_rcpt_based}) for my $rcpt (keys %{$queue})
{ {
my $queue = $transaction->notes("queue"); my $ret = $self->queue($transaction, $queue->{$rcpt});
$queue =~ /^lmtp:\/\/(.*):(\d+)$/; if (!$ret)
$lmtp_host=$1; {
$lmtp_port=$2; push(@notQueued,$rcpt);
$lmtp_user = $transaction->notes("destination-user") || ""; }
}
else
{
$lmtp_host = $self->{lmtp_host};
$lmtp_port = $self->{lmtp_port};
$lmtp_user = $transaction->sender->address || "";
}
if (!$lmtp_user) {
$self->log(LOGERROR, "No sender address found for transaction.\n");
return DECLINED;
} }
$self->log(LOGNOTICE,"forwarding mail to LMTP host: $lmtp_host:$lmtp_port\n"); return OK;
}
return $self->lmtp_transfer($transaction, $lmtp_host, $lmtp_port, $lmtp_user);
sub queue
{
my ($self, $transaction, $rcpt) = @_;
$minion->enqueue(transmit => [$rcpt] => {
attempts => 10,
expire => 60*60*24*2,
queue => $rcpt->{destination}
});
} }