smtpauth with unmodified qmail (Re: My solution to these licensing issues:)

Charlie Brady <[email protected]> Sat, 10 Jan 2004 19:23:09 -0500 (EST)
Newsgroups gmane.mail.qmail.dist
Message-ID <[email protected]>
On Sat, 10 Jan 2004, Chris Travers wrote:

> Recommend Postfix for areas where auth-smtp and other advanced features are
> required.

Inbound smtp auth can be done with Bruce Guenter's smtpfront-qmail. 

Outbound SMTP auth can be done via an smtp proxy listing on a loopback 
port, and a custom smtproute (either global, or per domain). So there's no 
absolute need to patch qmail for either of those.

Sample code follows, for those interested. Little tested. If anyone finds
a bug, please let me know.

#!/usr/bin/perl -w -T

package SMTPAuthProxy;

use strict;
use vars qw(@ISA);
use Net::Server::Fork;
use Net::SMTP;

@ISA = qw(Net::Server::Fork);

SMTPAuthProxy->run(
		max_servers => 4,
		proto => 'tcp',
		user => 'nobody',
		group => 'nobody',
		host => 'localhost',
		port => 26);
exit;

### over-ridden subs below

sub process_request
{
    my $self = shift;
    my $smtp = $self->{smtp};
    my $kidpid;

    die "can't fork: $!" unless defined ($kidpid = fork());
    if ($kidpid)
    {
	my $line;
	while (defined ($line = <STDIN>))
	{
	    print $smtp $line;
	}
	kill ("TERM" => $kidpid);
    }
    else
    {
	my $line;
	while (defined ($line = <$smtp>))
	{
	    print STDOUT $line;
	}
    }
}

sub post_accept_hook
{
    my $self = shift;
    my $smarthost = "mail.domain.com";  # say
    my $me = "me";
    my $name = "name"; # FIXME
    my $pass = "pass"; # FIXME

    my $smtp = Net::SMTP->new($smarthost,
			    Hello => $me,
			    #Debug => 1,
			);
    if ($smtp->supports("AUTH"))
    {
	unless ($smtp->auth($name, $pass))
	{
	    print "451 Could not auth to mail server\n";
	    warn "SMTP authentication with ISP server failed\n";
	    $smtp->quit;
	    exit;
	}
    }
    else
    {
	warn "Upstream SMTP server does not support authentication\n";
    }
    $self->{smtp} = $smtp;
    print "220 ", $smtp->banner;
}

1;

--
Charlie