logo
Free, unlimited AI code reviews that run on commit
git-lrc git-lrc GitHub Install Now We'd appreciate a star git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

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

Author

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

Description

       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 another 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 information.

   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.

Name

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

See Also

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

Synopsis

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

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

           my $conn;
           while($conn = $server->accept)
           {
               my $esmtp = Net::Server::Mail::ESMTP->new( socket => $conn );

               # activate XFORWARD extension if remote client is localhost
               $esmtp->register('Net::Server::Mail::ESMTP::XFORWARD')
                  if($server->get_property('peeraddr') =~ /^127/);
               # adding some handlers
               $esmtp->set_callback(RCPT => \&validate_recipient);
               # adding XFORWARD handler
               $esmtp->set_callback(XFORWARD => \&xforward);
               $esmtp->process();
               $conn->close();
           }

           sub xforward {
               my $self = shift;
               # Reject non IPV4 addresses
               return 0 unless( $self->get_forwarded_address =~ /^\d+\.\d+\.\d+\.\d+$/ );
               1;
           }

           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);
           }

See Also