| 1 |
root |
1.1 |
#! perl |
| 2 |
|
|
|
| 3 |
|
|
use IO::Socket; |
| 4 |
|
|
use Storable qw/nfreeze thaw/; |
| 5 |
|
|
|
| 6 |
|
|
my %global; # for use by eval'ed commands |
| 7 |
|
|
|
| 8 |
|
|
sub tcp_serve($) { |
| 9 |
|
|
my ($fh) = @_; |
| 10 |
|
|
|
| 11 |
|
|
binmode $fh, ":raw:perlio:utf8"; |
| 12 |
|
|
|
| 13 |
|
|
my $buf; |
| 14 |
root |
1.2 |
my ($a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k, $l, $m, $n, $o, $p, $q, $r, $s, $t, $u, $v, $w, $x, $y, $z); |
| 15 |
|
|
my (%l, @l); # for use by shell users |
| 16 |
root |
1.1 |
|
| 17 |
root |
1.2 |
print $fh "Welcome!\n", |
| 18 |
root |
1.3 |
"Remember that everything entered here will be in the main coro context within cf::!\n", |
| 19 |
root |
1.2 |
"You can freely use \$a .. \$z and \@l and \%l\n"; |
| 20 |
|
|
print $fh "\n> "; |
| 21 |
root |
1.1 |
|
| 22 |
root |
1.3 |
Event->io (fd => $fh, poll => 'r', data => 0, cb => sub { |
| 23 |
root |
1.1 |
if (defined (my $cmd = <$fh>)) { |
| 24 |
|
|
my $old_fh = select $fh; |
| 25 |
|
|
|
| 26 |
|
|
if ($cmd =~ /^\s*exit\b/i) { |
| 27 |
|
|
print "will not exit() server.\n"; |
| 28 |
|
|
} else { |
| 29 |
root |
1.3 |
package cf; |
| 30 |
|
|
my @res = eval $cmd; |
| 31 |
|
|
warn "tcpshell evaluation error: $@" if $@; |
| 32 |
|
|
print $@ if $@; |
| 33 |
root |
1.2 |
|
| 34 |
root |
1.3 |
print cf::dumpval @res > 1 ? \@res : $res[0] if @res; |
| 35 |
root |
1.1 |
} |
| 36 |
|
|
|
| 37 |
root |
1.2 |
print "\n> "; |
| 38 |
root |
1.1 |
select $old_fh; |
| 39 |
|
|
} else { |
| 40 |
|
|
$_[0]->w->cancel; |
| 41 |
|
|
} |
| 42 |
|
|
}); |
| 43 |
|
|
} |
| 44 |
|
|
|
| 45 |
|
|
# now a shell listening on a tcp-port - let the firewall decide access rights |
| 46 |
|
|
if ($cf::CFG{perl_shell}) { |
| 47 |
|
|
if (my $listen = new IO::Socket::INET LocalAddr => $cf::CFG{perl_shell}, Listen => 1, ReuseAddr => 1) { |
| 48 |
|
|
Event->io (fd => $listen, poll => 'r', data => cf::WF_AUTOCANCEL, cb => sub { tcp_serve $listen->accept }); |
| 49 |
|
|
} |
| 50 |
|
|
} |