#!/usr/bin/perl
# Check firewall ports are active for an atmail instance

use IO::Socket;
my $VERSION = '1.0';
my ( $peer, $port );

##look for server argument or display usage
( $peer = $ARGV[0] ) || &usage;

##assign port description hash
my %connected = split /:/, "http:80:https:443:imap:143:imaps:993:pop3:110:pop3s:995:smtp:25:smtp587:587:smtps:465:caldav:8008:caldavs:8443";

%connected = reverse %connected;

##sort and assign hash index array
my @ports = sort by_number keys %connected;

##look for list argument
&list if $ARGV[0] eq "list";

##attempt to connect to all ports in hash index, then display open ports
foreach $port (@ports) {
    $sock = IO::Socket::INET->new("$peer:$port");
	if ($sock) {
    print "$connected{$port} ($port): OPEN\n";
	} else {
    print "$connected{$port} ($port): FAIL\n";
	}
	
}
print "Done.\n";
exit;

##usage sub
sub usage {
    print "
  Atmail port check
  
  Usage: check-firewall-ports.pl <server>
         check-firewall-ports.pl list\n\n";
    exit;
}

##list sub
sub list {
    foreach $port (@ports) {
        print "Port $port $connected{$port}\n";
    }
    exit;
}

##hash index sort sub
sub by_number {
    if ( $a < $b ) { -1 }
    elsif ( $a > $b ) { 1 }
    else { 0 }
}

=head1 NAME

Portscanner 1.0 - Reports listening ports and their associated service

=head1 Author

James Loope, cronus@pcis.net

=head1 Copyright

Do with it what you please.

=head1 DESCRIPTION

See README

=head1 README

A portscanner for those of us who haven't memorized the common ports map. It scans all common
ports between 1 and 1023 and reports open ports and their common use.

=head1 PREREQUISITES

This script requires IO::Socket

=pod OSNAMES

Linux

=pod SCRIPT CATEGORIES

Networking

=cut
