QPSMTPD-MailserverInterface/federationhq_rcpt

162 lines
4.3 KiB
Plaintext

use strict;
use warnings;
use Qpsmtpd::Constants;
use Qpsmtpd::DSN;
use DBI;
sub register {
my ($self, $qp) = (shift, shift);
# some default values
$self->{database} = $qp->config("fedhq_mysql_database") || "mail";
$self->{host} = $qp->config("fedhq_mysql_host") || "localhost";
$self->{port} = $qp->config("fedhq_mysql_port") || "3306";
$self->{user} = $qp->config("fedhq_mysql_user") || "qpsmtpd";
$self->{pass} = $qp->config("fedhq_mysql_password");
$self->createDSN();
}
sub createStatements
{
my $self = shift;
$self->{rcpt_sth} = $self->{dbh}->prepare("select username from email_address where alias=? and domain=?");
$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=?");
}
sub connect
{
my $self = shift;
$self->{dbh} = DBI->connect($self->{dsn}, $self->{user}, $self->{pass}, { RaiseError => 0}) ||
$self->log(LOGERROR, "error connecting to DB " . $DBI::errstr);
if ($self->{dbh}->err())
{
$self->log(LOGERROR, "error connecting to DB: " . $self->{dbh}->errstr());
$self->{dbh}=undef;
}
}
sub disconnect
{
my $self = shift;
$self->{dbh}->disconnect() if ($self->{dbh});
}
sub createDSN
{
my $self = shift;
my $dsn = "DBI:MariaDB:database=" . $self->{database} . ";host=" . $self->{host} . ";port=" . $self->{port};
$self->{dsn} = $dsn;
# try to parse the dsn to ensure it is valid
my @data = DBI->parse_dsn($self->{dsn});
if (@data == 0)
{
$self->log(LOGERROR, "DSN " . $self->{dsn} . " not valid");
$self->{dsn}="";
}
$self->log(LOGDEBUG, "created DSN " . $self->{dsn});
}
sub updateTransactionWithRecipientInfo
{
my ($self, $transaction, $sth) = @_;
return unless $sth->rows == 1;
my $row = $sth->fetchrow_hashref;
my $dovecot_server = $row->{dovecot_server};
my $username = $row->{username};
my $result = $self->{fetch_dovecot_details_sth}->execute($dovecot_server);
if (!$result)
{
$self->log(LOGERROR, "Failed to fetch dovecot server information for user " . $username . " and dovecot server " . $dovecot_server );
return 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 );
return 0;
}
$row = $self->{fetch_dovecot_details_sth}->fetchrow_hashref();
my $hostname = $row->{hostname};
my $port = $row->{port};
$transaction->notes("queue", "lmtp://$hostname:$port");
$self->log(LOGNOTICE, "Setting LMTP server to dovecot on $hostname:$port for user: $username");
return 1;
}
sub hook_quit {
my ($self, $transaction) = @_;
$self->disconnect();
}
sub hook_rcpt {
my ($self, $transaction, $recipient) = @_;
$self->log(LOGNOTICE, "Recipient: " . $recipient->user . "@" . $recipient->host);
return DECLINED unless $recipient->host && $recipient->user;
$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 DECLINED;
}
if ($self->{rcpt_sth}->rows == 1)
{
$self->log(LOGDEBUG, " found recipient in database");
my $ret = $self->updateTransactionWithRecipientInfo($transaction, $self->{rcpt_sth});
if (!$ret)
{
$self->log(LOGERROR, "Failed to update transaction with recipient information");
return DENYSOFT;
}
return OK;
}
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 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 DECLINED;
}