Merge pull request #90 from msimerson/utils

Utils: added is_localhost and tests.
This commit is contained in:
Matt Simerson 2014-09-10 14:19:52 -07:00
commit 2466a88cb2
5 changed files with 60 additions and 13 deletions

View File

@ -591,7 +591,7 @@ sub spool_dir {
unless ($Spool_dir) { # first time through
$self->log(LOGDEBUG, "Initializing spool_dir");
$Spool_dir = $self->config('spool_dir')
|| Qpsmtpd::Utils::tildeexp('~/tmp/');
|| Qpsmtpd::Utils->tildeexp('~/tmp/');
$Spool_dir .= "/" unless ($Spool_dir =~ m!/$!);

View File

@ -2,7 +2,7 @@ package Qpsmtpd::Utils;
use strict;
sub tildeexp {
my $path = shift;
my ($self, $path) = @_;
$path =~ s{^~([^/]*)} {
$1
? (getpwnam($1))[7]
@ -11,4 +11,13 @@ sub tildeexp {
return $path;
}
sub is_localhost {
my ($self, $ip) = @_;
return if ! $ip;
return 1 if $ip =~ /^127\./; # IPv4
return 1 if $ip =~ /:127\./; # IPv4 mapped IPv6
return 1 if $ip eq '::1'; # IPv6
return;
}
1;

View File

@ -340,14 +340,15 @@ sub is_regex_match {
sub invalid_localhost {
my ($self, $host) = @_;
return if lc $host ne 'localhost';
if ($self->qp->connection->remote_ip ne '127.0.0.1') {
my $ip = $self->qp->connection->remote_ip;
if ($ip && ($ip eq '127.0.0.1' || $ip eq '::1')) {
$self->log(LOGDEBUG, "pass, is localhost");
return;
}
#$self->log( LOGINFO, "fail, not localhost" );
return ("You are not localhost", "invalid localhost");
}
$self->log(LOGDEBUG, "pass, is localhost");
return;
}
sub is_plain_ip {
my ($self, $host) = @_;

View File

@ -11,7 +11,7 @@ sub register_tests {
$self->register_test('test_init_resolver', 2);
$self->register_test('test_is_in_badhelo', 2);
$self->register_test('test_is_regex_match', 3);
$self->register_test('test_invalid_localhost', 4);
$self->register_test('test_invalid_localhost', 5);
$self->register_test('test_is_plain_ip', 3);
$self->register_test('test_is_address_literal', 3);
$self->register_test('test_no_forward_dns', 2);
@ -73,9 +73,11 @@ sub test_invalid_localhost {
($err, $why) = $self->invalid_localhost('localhost');
ok( $err, "localhost, invalid remote IP: $why");
$self->qp->connection->remote_ip('127.0.0.1');
foreach my $ip (qw/ ::1 127.0.0.1 / ) {
$self->qp->connection->remote_ip($ip);
($err, $why) = $self->invalid_localhost('localhost');
ok( ! $err, "localhost, correct remote IP");
ok( ! $err, "localhost, correct remote IP ($ip)");
}
};
sub test_is_plain_ip {

35
t/qpsmtpd-utils.t Normal file
View File

@ -0,0 +1,35 @@
#!/usr/bin/perl
use strict;
use warnings;
use Test::More;
use lib 'lib'; # test lib/Qpsmtpd/Utils (vs site_perl)
BEGIN { use_ok('Qpsmtpd::Utils'); }
my $utils = bless {}, 'Qpsmtpd::Utils';
__tildeexp();
__is_localhost();
done_testing();
sub __is_localhost {
for my $local_ip (qw/ 127.0.0.1 ::1 2607:f060:b008:feed::127.0.0.1 127.0.0.2 /) {
ok( $utils->is_localhost($local_ip), "is_localhost, $local_ip");
}
for my $rem_ip (qw/ 128.0.0.1 ::2 2607:f060:b008:feed::128.0.0.1 /) {
ok( !$utils->is_localhost($rem_ip), "!is_localhost, $rem_ip");
}
};
sub __tildeexp {
my $path = $utils->tildeexp('~root/foo.txt');
ok( $path, "tildeexp, $path");
$path = $utils->tildeexp('no/tilde/in/path');
cmp_ok( $path, 'eq', 'no/tilde/in/path', 'tildeexp, no expansion');
};