#!/usr/bin/perl
#
# Copyright (c) 2007, Gregory Fleischer (gfleischer@gmail.com)
# 
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
# 
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 
# 3. Neither the name of the author nor the names of its
#    contributors may be used to endorse or promote products derived from
#    this software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

use IO::Socket;
use strict;

eval {

    $|=1;
    $SIG{CHLD}="IGNORE";

    my $port = 9051;

    my $sock = new IO::Socket::INET(
	Listen => 5,
	LocalAddr => 'technicalattack.netfirmsmysql.com',
	LocalPort => $port,
	ReuseAddr => 1
	);

    print "[*] listening on port $port", "\n";
    $sock->listen();

    while (1) {
	my $client = $sock->accept();
	eval {
	    if (0 == fork()) {
		&handle_client($client);
	    }
	};
	if ($@) {
	    print "[*] error $@";
	}
    }
};
if ($@) {
    print "[*] error $@";
}

sub handle_client {
    my $client = $_[0];
    eval {
	alarm(10);
	print "[*] accepted connection from ", 
	$client->peerhost(), ":", $client->peerport(), "\n";
	my $protocolinfo = 0;
	my $authenticated = 0;
	my $quit = 0;
	while ( my $line = <$client> ) {
	    print "[=] got $line";
	    $line=~s/\r|\n//g;
	  RESPONSE: {
	      if ($line=~/^PROTOCOLINFO(?:\s\d+)?$/i) {
		  if ($protocolinfo > 0) {
		      print $client "514 Authentication Required\r\n";
		      $quit = 1;
		      last RESPONSE;
		  } else {
		      ++$protocolinfo;
		  }
	      } elsif ($line=~/^QUIT/i) {
		  $quit = 1;
	      } elsif ($line=~/^AUTHENTICATE.*$/i) {
		  $authenticated = 1;
	      } elsif (!$authenticated) {
		  print $client "514 Authentication Required\r\n";
		  $quit = 1;
		  last RESPONSE;
	      }
	      print $client "250-$line\r\n";
	      print $client "250 OK\r\n";
	    }
	    if ($quit) {
		last;
	    }
	}
	close($client);
    };
    alarm(0);
    if ($@) {
	print "[*] error $@";
    }
    exit(0);
}