# $DUH: helo_ipmismatch,v 1.6 2002/12/16 05:14:33 tv Exp $
#
# Copyright (c) 2002 Todd Vierling <tv@pobox.com> <tv@duh.org>.
# All rights reserved.
# Please see the COPYRIGHT file, part of the PMilter distribution,
# for full copyright and license terms.

##### helo_ipmismatch #####
#
# Rejects HELO commands of IP (currently only IPv4) address literals
# that do not match the connecting host address.

use PMilter::Session qw(:all);

my $errmsg = shift_errmsg(@_, 'Address literal %1 does not match your host address [%2]');

+{
	helo => sub {
		my $ctx = shift;
		my $helo = shift;
		my @addr = $ctx->getaddress;

		if ($helo =~ /^\[(.*)\]$/) {
			my $heloaddr = $1;

			if ($addr[0] eq SMFIA_INET) {
				$heloaddr =~ s/\.0+/./; # remove leading 0's
				return SMFIS_ACCEPT if ($heloaddr eq $addr[2]);
			} else {
				# XXX need to add IPv6 parser
				return SMFIS_ACCEPT;
			}

			my $err = $errmsg;
			$err =~ s/%1/$helo/g; # interpolate HELO arg
			$err =~ s/%2/$addr[2]/g; # interpolate real address

			return $ctx->reject("554 HELO/EHLO command rejected: $err");
		}

		return SMFIS_ACCEPT;
	}
};
