command-listener 3.47 KB
Newer Older
Duncan White's avatar
Duncan White committed
1
2
3
#!/usr/bin/perl
#
#	command-listener: daemon that listens for a connection on a
4
5
#	port, reads a single line of text (arguments) and runs a single
#	specific command with those arguments, gathering and sending the
Duncan White's avatar
Duncan White committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#	results of running that command back to the client.
#
use strict;
use warnings;

use Data::Dumper;
use IO::Socket;
use POSIX 'WNOHANG';

use csglib::Log;

# important constants
# think about logging later..
my $logfile         = "/tmp/command-listener.log";

# command to run..
22
my $command	    = "/bin/ls";		# example:-)
Duncan White's avatar
Duncan White committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113

my $port = shift @ARGV || 55001;
my $quit = 0;

my $log = new csglib::Log( scriptname=>"command-listener",
			   file=>$logfile,
			   level=>LOG_INFO ) ||
	  die "command-listener: can't create $logfile\n";


#
#  my $socket = initialize_listener( $port );
#	start listening server on port $port..
#	return the socket we receive.  In case of
#	total failure, return undef.
#
sub initialize_listener ($)
{
	my( $port ) = @_;

	# signal handler for child die events
	$SIG{CHLD} = sub { while( waitpid(-1,WNOHANG)>0 ) {} };
	# signal handle for being interrupted
	$SIG{INT}  = sub { $quit++ };
	$log->info( "trying to listen on port $port" );
	foreach (1..5) {
	    my $socket = new IO::Socket::INET(
				 'LocalPort' => $port,
				 'Listen'    => 20,
				 'Proto'     => 'tcp',
				 'Reuse'     => 1,
				 'Timeout'   => 3600 );
	    return $socket if $socket;
	    $log->info( "trying again..." );
	    sleep 1;
	}
	return undef;
}


#
# connection_loop( $listensocket, $callback );
#	take the listening socket $listensocket 
#	and a callback function $callback.
#	forever wait for connections, forking
#	a child for each connection, and making
#	the child call the callback function $callback
#	to handle one conversation with one client.
#
sub connection_loop ($$)
{
	my( $listensocket, $callback ) = @_;
	while( !$quit )
	{
		next unless my $conn = $listensocket->accept;
		defined( my $child = fork()) ||
			$log->fatal( "can't fork: $!" );
		if( $child == 0 )	# child
		{
			$listensocket->close;
			my $peer = getpeername($conn);
			my($port, $iaddr) = sockaddr_in($peer);
			my $peerhostname = gethostbyaddr($iaddr, AF_INET);
			my $peeraddr = inet_ntoa($iaddr);

			$log->info( "accepted connection from $peeraddr, host $peerhostname" );
			STDIN->fdopen($conn,'<') ||
				$log->fatal( "can't reopen STDIN: $!" );
			STDOUT->fdopen($conn,'>') ||
				$log->fatal( "can't reopen STDOUT: $!" );
			#STDERR->fdopen($conn,'>') ||
			#	$log->fatal( "can't reopen STDERR: $!" );
			$| = 1;		# auto flush
			$callback->();
			exit 0;
		}
		$conn->close;
	}
}


#
# converse();
#	handle one client<->server conversation,
#	there's no from-client data needed (if there was,
#	it would be on STDIN), simply run $command which
#	will produce output on STDOUT, which will be
#	automatically sent back to the client.
#
sub converse ()
{
114
115
116
117
118
	$log->warn( "reading line from client" );
	my $args = <STDIN>;
	unless( $args )
	{
		$log->warn( "converse: eof from client" );
119
		print "gimme a line of args:-)\n";
120
121
122
		return;
	}
	chomp $args;
123
124
125
126
127
128
	unless( $args =~ /^(\w-\s)+$/ )
	{
		$log->warn( "converse: dangerous args <<$args>> from client" );
		print "dangerous args <<$args>>, get stuffed:-)\n";
		return;
	}
129
	system( "$command $args" );
Duncan White's avatar
Duncan White committed
130
131
132
133
134
135
136
137
138
139
}


# stage 1: initialize the listener
my $socket = initialize_listener( $port );
$log->fatal( "can't create listening socket $port" ) unless $socket;

# stage 2: connection loop
$log->warn( "server listening on port $port, ready for connections" );
connection_loop( $socket, \&converse );