File: sock.pl

package info (click to toggle)
snap 0.08-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 440 kB
  • ctags: 473
  • sloc: perl: 7,033; makefile: 117; sh: 70
file content (239 lines) | stat: -rw-r--r-- 4,268 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#
# This is a socket-based control module for Snap.  It allows you to
# telnet to the port defined, log in with the specified password, and
# control Snap from a textmode interface.  This is useful for running
# Snap in the background and then connecting to control it periodically.
#
# Variables:
#
#   $sock_addr     - Address to listen on.  Default: 127.0.0.1
#   $sock_port     - Port to listen on.     Default: 2323
#   $sock_password - Password needed to gain access to Snap.  Required.
#   @sock_disabled - Disabled command list. Default: ('/eval', '/alias', '/exec');
#

$VERSION = "0.01";

use IO::Socket;
use FileHandle;

if (defined $SOCK_ENABLED)
{
  print "~5Error, sock already loaded!\n";
  return;
}

$sock_addr = (defined $sock_addr) ? $sock_addr : '127.0.0.1';
$sock_port = (defined $sock_port) ? $sock_port : 2323;
@sock_disabled = ('/eval', '/alias', '/exec') if ($#sock_disabled < 0);

my $server_sock = new IO::Socket::INET(LocalAddr => $sock_addr,
				       LocalPort => $sock_port,
				       Listen => 1,
				       Reuse => 1);
my $client = undef;
my $verified = undef;
my $obj = SockWindow->new(tied *STDOUT);

tie *STDOUT, 'SockWindow', $obj;

$handles{$server_sock} = { Handle => $server_sock,
			   Callback => \&handle_socket };

print "~13;Sock~c; $VERSION loaded... running on $sock_addr:$sock_port\n";

$command_hash{"/shutdown"} = "/quit";
$command_hash{"/disconnect"} = 
  sub 
    { 
      $obj->set_socket(undef);

      delete $handles{$client};

      $client->close();  
      $client = undef; 
      $verified = undef;
    };

sub handle_socket
{
  my ($sock, $handle) = @_;

  if ($handle eq $server_sock)
    {
      my $cl = $handle->accept();
      my $name = $cl->peerhost();

      return if (defined $client);
      $client = $cl;

      print "New terminal connection from $name.\n";
      debug_print("Sock", "New terminal connection from $name.\n");

      $handles{$client} = { Handle => $client,
			    Callback => \&handle_socket };

      $obj->set_socket($client);

      print "Password: ";
    }
  elsif ($handle eq $client)
    {
      my $cmd = <$handle>;

      $cmd =~ s/\015|\012//g;      

      if (! defined $verified)
        {
	  if ($cmd ne $sock_password)
	    {
	      print $handle "Error, incorrect password!\n";

              $obj->set_socket(undef);

	      delete $handles{$handle};
	      $client = undef;
	      $verified = undef;

	      return;
	    }
	  
          print "Password verified!\n";

	  $verified = 1;

	  return;
        }

      if (length($cmd) > 0)
        {
          my $bad = 0;

          $cmd =~ s/^\/quit/\/disconnect/;

          foreach (@sock_disabled)
            {
              if ($cmd =~ /^\Q$_\E/)
                {
                  print "That is a restricted command, sorry!\n";
                  $bad = 1;
                }
            }

          debug_print("Sock", "Executing command: $cmd\n");          
          do_command($sock, $cmd)  if (! $bad);
        }
      else
        {	    
          $obj->set_socket(undef);

	  delete $handles{$handle};
	  $client = undef;
	  $verified = undef;
        }
    }
}

package SockWindow;

sub new
{
  my $class = shift;
  my $self = {};

  bless $self, $class;

  $self->{"window"} = shift;
  $self->{"socket"} = undef;

  return $self;
}

sub set_socket
{
  my $self = shift;

  $self->{"socket"} = shift;
}

sub print
{
  my ($self, $text) = @_;
  my $sock = $self->{"socket"};

  if (defined $self->{"window"})
    { $self->{"window"}->print("$text") }

  return if (! $sock);

  $text =~ s/~.*?;//g;

  print $sock $text;
}

sub TIEHANDLE
{
  my $class = shift;
  my $obj = shift;

  return $obj;
}

sub WRITE
{
  my $self = shift;

  $self->print("WRITE not supported...\n");
}

sub PRINT
{
  my $self = shift;
  my $var;

  foreach $var (@_)
    { $self->print($var); }
}

sub PRINTF
{
  my $self = shift;

  $self->print("PRINTF not supported...\n");
}

sub READ
{
  my $self = shift;

  $self->print("On STDIN?  Nuhuh...\n");
}

sub READLINE
{
  my $self = shift;

  $self->print("On STDIN?  Nuhuh...\n");
}

sub GETC
{
  my $self = shift;

  $self->print("On STDOUT?  Nuhuh...\n");
}

sub CLOSE
{
  my $self = shift;
  
  $self->print("CLOSE not supported...\n");
}

sub DESTROY
{
}

1;