#!/usr/bin/perl #$Id: Meru-Server.pl,v 1.2 2006/05/03 13:26:33 tfiedler Exp $ use strict; use warnings; use IPC::Run qw( run timeout ); use IO::Socket::INET; use Crypt::CBC; use Carp; my $cipher = Crypt::CBC->new( -key => 'S3cr#tabcDeSal35', -cipher => 'Blowfish' ); my $port = 62750; my $LOG; $SIG{CHLD} = 'IGNORE'; $SIG{INT} = 'IGNORE'; my $listener = IO::Socket::INET->new(LocalPort => $port, Listen => 10, Proto => 'tcp', Reuse => 1); confess "Error creating a listener on port 62570: $@\n" unless $listener; open $LOG, "+>>", "Log.txt" or carp "Unable to open Log.txt: $@\n"; print "[Listening on port $port]\n"; while ( my $connection = $listener->accept) { my $child; confess "Cannot fork a process: $!\n" unless defined ( $child = fork() ); if ( $child == 0 ) { $listener->close; $connection->print("\n"); $connection->print("connected\n"); my $receive; if ( defined( $connection->recv($receive, 100,0) ) ) { chomp($receive); my $command = $cipher->decrypt($receive); print "processing \"$receive\" => \"$command\"\n"; print $LOG scalar(localtime), " $$ received: $receive\n"; print $LOG scalar(localtime), " $$ decrypted: $command\n"; $connection->print("your command was received\n"); my $return = ( execute_command("$command") == 0 ) ? "success" : "failure"; print $LOG scalar(localtime), " $$ \"$command\" $return\n"; $connection->print("Your command ended in $return\n"); $connection->print("Goodbye\n"); $connection->print("1970__"); } } else { print $LOG scalar(localtime), " $$ Connect from ", $connection->peerhost, "\n"; print "Connection from ", $connection->peerhost, "\n"; $connection->close(); } } sub execute_command { my $line = shift; my @info = ( grep /\|/, $line ) ? split /\|/, $line : $line; #my @info = split /\|/, $line || $line; my $job = $info[0]; print $LOG scalar(localtime), " $$ job = $job info = @info\n"; my $return = 2; print "job = $job\n"; $return = ( ListKillProc(@info) == 0 ) ? 0 : 1 if ( $job =~ /^ListKillProc/ ); $return = ( unlockuser(@info) == 0 ) ? 0 : 1 if ( $job =~ /^unlockuser/ ); $return = ( changepass(@info) == 0 ) ? 0 : 1 if ( $job =~ /^changepass/ ); $return = ( showprintersall(@info) == 0 ) ? 0 : 1 if ( $job =~ /^showprintersall/ ); $return = ( showprintersuser(@info) == 0 ) ? 0 : 1 if ( $job =~ /^showprintersuser/ ); $return = ( showprinter(@info) == 0 ) ? 0 : 1 if ( $job =~ /^showprinter/ ); $return = ( killprint(@info) == 0 ) ? 0 : 1 if ( $job =~ /^killprint/ ); print "return before = $return\n"; if ( $job =~ /^APP/ ) { my @return = APP(); my $exit = shift @return; print "exit = $exit\n"; print @return; return $exit; } print $LOG scalar(localtime), " $$ execution return code = $return\n"; return $return; } sub APP { my @command = ('ls', '-la'); print $LOG scalar(localtime), " $$ cmd = @command\n"; my ( $buff, $in, $err ); my $exit; run \@command, \$in, \$buff, \$err, timeout(10) and $exit = $?; print "exit = $exit\n"; print $LOG scalar(localtime), " $$ exit code =", $exit, "\n"; #print $out if $out; my @return; push (@return, $exit, $buff); print "$_\n" for @return; return @return; } sub AUTOLOAD { print "I dont know how to do $_[0]\n"; print $LOG scalar(localtime), " $$ Uh Oh we hit the Autoloader: no match for $_[0]\n"; return 1; }