Net::Server::Mail::ESMTP::XFORWARD - A module to add support to the XFORWARD command in Net::Server::Mail::ESMTP


Net-Server-Mail documentation Contained in the Net-Server-Mail distribution.

Index


Code Index:

NAME

Top

Net::Server::Mail::ESMTP::XFORWARD - A module to add support to the XFORWARD command in Net::Server::Mail::ESMTP

SYNOPSIS

Top

    use Net::Server::Mail::ESMTP;

    my @local_domains = qw(example.com example.org);
    my $server = new IO::Socket::INET Listen => 1, LocalPort => 25;

    my $conn;
    while($conn = $server->accept)
    {
        my $esmtp = new Net::Server::Mail::ESMTP socket => $conn;
        # activate some extensions
        $esmtp->register('Net::Server::Mail::ESMTP::XFORWARD');
        # adding some handlers
        $esmtp->set_callback(RCPT => \&validate_recipient);
        $esmtp->process();
	$conn->close()
    }

    sub validate_recipient
    {
        my($session, $recipient) = @_;

        my $domain;
        if($recipient =~ /@(.*)>\s*$/)
        {
            $domain = $1;
        }

        if(not defined $domain)
        {
            return(0, 513, 'Syntax error.');
        }
        elsif(not(grep $domain eq $_, @local_domains) && $session->get_forwarded_addr != "10.1.1.1")
        {
            return(0, 554, "$recipient: Recipient address rejected: Relay access denied");
        }

        return(1);
    }

DESCRIPTION

Top

When using a Net::Server::Mail::ESMTP script inside a MTA and not in front of Internet, values like client IP address are not accessible to the script and when the script returns mail to an other instance of smtpd daemon, it logs "localhost" as incoming address. To solve this problem, some administrators use the XFORWARD command. This module gives the ability to read and store XFORWARD informations.

METHODS

These methods return the values set by the upstream MTA without modifying them so they can be set to undef or "[UNVAILABLE]". See Postfix documentation for more.

* get_forwarded_values : returns a hash reference containing all values forwarded (keys in lower case).
* get_forwarded_name : returns the up-stream hostname. The hostname may be a non-DNS hostname.
* get_forwarded_address : returns the up-stream network address. Address information is not enclosed with []. The address may be a non-IP address.
* get_forwarded_source : returns LOCAL or REMOTE.
* get_forwarded_helo : returns the hostname that the up-stream host announced itself. It may be a non-DNS hostname.
* get_forwarded_proto : returns the mail protocol for receiving mail from the up-stream host. This may be an SMTP or non-SMTP protocol name of up to 64 characters.

SEE ALSO

Top

Net::Server::Mail::ESMTP, http://www.postfix.org/XFORWARD_README.html

AUTHOR

Top

Xavier Guimard, <x.guimard@free.fr>

COPYRIGHT AND LICENSE

Top


Net-Server-Mail documentation Contained in the Net-Server-Mail distribution.

package Net::Server::Mail::ESMTP::XFORWARD;

use 5.006;
use strict;

our $VERSION = '0.05';

use base qw(Net::Server::Mail::ESMTP::Extension);

sub init {
    my ( $self, $parent ) = @_;
    $self->{parent} = $parent;
    return $self;
}

sub verb {
    my $self = shift;
    return [ 'XFORWARD' => 'xforward' ];
}

sub keyword {
    return 'XFORWARD';
}

sub parameter {
    my $self = shift;
    return "NAME ADDR PROTO HELO SOURCE";
}

sub xforward {
    my $self = shift;
    my $args = shift;
    my %h    = ( $args =~ /(NAME|ADDR|PROTO|HELO|SOURCE)=([^\s]+)\s*/g );
    $args =~ s/(NAME|ADDR|PROTO|HELO|SOURCE)=[^\s]+\s*//g;
    if ( $args !~ /^\s*$/ ) {
        $args =~ s/=.*$//;
        $self->reply( 501, "5.5.4 Bad XFORWARD attribute name: $args" );
    }
    else {
        $self->{"xforward"}->{ lc($_) } = $h{$_} foreach ( keys %h );
        $self->reply( 250, "2.0.0 Ok" );
    }
    return;
}

sub get_forwarded_values {
    my $self = shift;
    return $self->{xforward};
}

sub get_forwarded_name {
    my $self = shift;
    return $self->{xforward}->{name};
}

sub get_forwarded_address {
    my $self = shift;
    return $self->{xforward}->{addr};
}

sub get_forwarded_proto {
    my $self = shift;
    return $self->{xforward}->{proto};
}

sub get_forwarded_helo {
    my $self = shift;
    return $self->{xforward}->{helo};
}

sub get_forwarded_source {
    my $self = shift;
    return $self->{xforward}->{source};
}

# New subroutines in Net::Server::Mail::ESMTP space
*Net::Server::Mail::ESMTP::xforward              = \&xforward;
*Net::Server::Mail::ESMTP::get_forwarded_values  = \&get_forwarded_values;
*Net::Server::Mail::ESMTP::get_forwarded_name    = \&get_forwarded_name;
*Net::Server::Mail::ESMTP::get_forwarded_address = \&get_forwarded_address;
*Net::Server::Mail::ESMTP::get_forwarded_proto   = \&get_forwarded_proto;
*Net::Server::Mail::ESMTP::get_forwarded_helo    = \&get_forwarded_helo;
*Net::Server::Mail::ESMTP::get_forwarded_source  = \&get_forwarded_source;

1;
__END__