added registration
[spider.git] / perl / ExtMsg.pm
1 #
2 # This class is the internal subclass that deals with the external port
3 # communications for Msg.pm
4 #
5 # This is where the cluster handles direct connections coming both in
6 # and out
7 #
8 # $Id$
9 #
10 # Copyright (c) 2001 - Dirk Koopman G1TLH
11 #
12
13 package ExtMsg;
14
15 use strict;
16 use Msg;
17 use DXVars;
18 use DXUtil;
19 use DXDebug;
20 use IO::File;
21 use IO::Socket;
22 use IPC::Open3;
23
24 use vars qw($VERSION $BRANCH);
25 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
26 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ ) || 0;
27 $main::build += $VERSION;
28 $main::branch += $BRANCH;
29
30 use vars qw(@ISA $deftimeout);
31
32 @ISA = qw(Msg);
33 $deftimeout = 60;
34
35 sub enqueue
36 {
37         my ($conn, $msg) = @_;
38         unless ($msg =~ /^[ABZ]/) {
39                 if ($msg =~ /^E[-\w]+\|([01])/ && $conn->{csort} eq 'telnet') {
40                         $conn->{echo} = $1;
41                         if ($1) {
42 #                               $conn->send_raw("\xFF\xFC\x01");
43                         } else {
44 #                               $conn->send_raw("\xFF\xFB\x01");
45                         }
46                 } else {
47                         $msg =~ s/^[-\w]+\|//;
48                         push (@{$conn->{outqueue}}, $msg . $conn->{lineend});
49                 }
50         }
51 }
52
53 sub send_raw
54 {
55         my ($conn, $msg) = @_;
56     my $sock = $conn->{sock};
57     return unless defined($sock);
58         push (@{$conn->{outqueue}}, $msg);
59         dbg("connect $conn->{cnum}: $msg") if $conn->{state} ne 'C' && isdbg('connect');
60     Msg::set_event_handler ($sock, "write" => sub {$conn->_send(0)});
61 }
62
63 sub echo
64 {
65         my $conn = shift;
66         $conn->{echo} = shift;
67 }
68
69 sub dequeue
70 {
71         my $conn = shift;
72         my $msg;
73
74         if ($conn->{csort} eq 'ax25' && exists $conn->{msg}) {
75                 $conn->{msg} =~ s/\cM/\cJ/g;
76         }
77         if ($conn->{state} eq 'WC') {
78                 if (exists $conn->{cmd}) {
79                         if (@{$conn->{cmd}}) {
80                                 dbg("connect $conn->{cnum}: $conn->{msg}") if isdbg('connect');
81                                 $conn->_docmd($conn->{msg});
82                         } 
83                 }
84                 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
85                         $conn->to_connected($conn->{call}, 'O', $conn->{csort});
86                 }
87         } elsif ($conn->{msg} =~ /\cJ/) {
88                 my @lines =  $conn->{msg} =~ /([^\cM\cJ]*)\cM?\cJ/g;
89                 if ($conn->{msg} =~ /\cJ$/) {
90                         delete $conn->{msg};
91                 } else {
92                         $conn->{msg} =~ s/([^\cM\cJ]*)\cM?\cJ//g;
93                 }
94                 while (defined ($msg = shift @lines)) {
95                         dbg("connect $conn->{cnum}: $msg") if $conn->{state} ne 'C' && isdbg('connect');
96                 
97                         $msg =~ s/\xff\xfa.*\xff\xf0|\xff[\xf0-\xfe].//g; # remove telnet options
98                         $msg =~ s/[\x00-\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g;         # immutable CSI sequence + control characters
99                         
100                         if ($conn->{state} eq 'C') {
101                                 &{$conn->{rproc}}($conn, "I$conn->{call}|$msg");
102                         } elsif ($conn->{state} eq 'WL' ) {
103                                 $msg = uc $msg;
104                                 if (is_callsign($msg) && $msg !~ m|/| ) {
105                                         my $sort = $conn->{csort};
106                                         $sort = 'local' if $conn->{peerhost} eq "127.0.0.1";
107                                         my $uref;
108                                         if ($main::passwdreq || ($uref = DXUser->get_current($msg)) && $uref->passwd ) {
109                                                 $conn->conns($msg);
110                                                 $conn->{state} = 'WP';
111                                                 $conn->{decho} = $conn->{echo};
112                                                 $conn->{echo} = 0;
113                                                 $conn->send_raw('password: ');
114                                         } else {
115                                                 $conn->to_connected($msg, 'A', $sort);
116                                         }
117                                 } else {
118                                         $conn->send_now("Sorry $msg is an invalid callsign");
119                                         $conn->disconnect;
120                                 }
121                         } elsif ($conn->{state} eq 'WP' ) {
122                                 my $uref = DXUser->get_current($conn->{call});
123                                 $msg =~ s/[\r\n]+$//;
124                                 if ($uref && $msg eq $uref->passwd) {
125                                         my $sort = $conn->{csort};
126                                         $conn->{echo} = $conn->{decho};
127                                         delete $conn->{decho};
128                                         $sort = 'local' if $conn->{peerhost} eq "127.0.0.1";
129                                         $conn->{usedpasswd} = 1;
130                                         $conn->to_connected($conn->{call}, 'A', $sort);
131                                 } else {
132                                         $conn->send_now("Sorry");
133                                         $conn->disconnect;
134                                 }
135                         } elsif ($conn->{state} eq 'WC') {
136                                 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
137                                         $conn->_docmd($msg);
138                                         if ($conn->{state} eq 'WC' && exists $conn->{cmd} &&  @{$conn->{cmd}} == 0) {
139                                                 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
140                                         }
141                                 }
142                         }
143                 }
144         }
145 }
146
147 sub to_connected
148 {
149         my ($conn, $call, $dir, $sort) = @_;
150         $conn->{state} = 'C';
151         $conn->conns($call);
152         delete $conn->{cmd};
153         $conn->{timeout}->del if $conn->{timeout};
154         delete $conn->{timeout};
155         &{$conn->{rproc}}($conn, "$dir$call|$sort");
156         $conn->_send_file("$main::data/connected") unless $conn->{outgoing};
157 }
158
159 sub new_client {
160         my $server_conn = shift;
161     my $sock = $server_conn->{sock}->accept();
162         if ($sock) {
163                 my $conn = $server_conn->new($server_conn->{rproc});
164                 $conn->{sock} = $sock;
165                 Msg::blocking($sock, 0);
166                 $conn->{blocking} = 0;
167                 eval {$conn->{peerhost} = $sock->peerhost};
168                 if ($@) {
169                         dbg($@) if isdbg('connll');
170                         $conn->disconnect;
171                 } else {
172                         eval {$conn->{peerport} = $sock->peerport};
173                         $conn->{peerport} = 0 if $@;
174                         my ($rproc, $eproc) = &{$server_conn->{rproc}} ($conn, $conn->{peerhost}, $conn->{peerport});
175                         dbg("accept $conn->{cnum} from $conn->{peerhost} $conn->{peerport}") if isdbg('connll');
176                         if ($eproc) {
177                                 $conn->{eproc} = $eproc;
178                                 Msg::set_event_handler ($sock, "error" => $eproc);
179                         }
180                         if ($rproc) {
181                                 $conn->{rproc} = $rproc;
182                                 my $callback = sub {$conn->_rcv};
183                                 Msg::set_event_handler ($sock, "read" => $callback);
184                                 # send login prompt
185                                 $conn->{state} = 'WL';
186                                 #               $conn->send_raw("\xff\xfe\x01\xff\xfc\x01\ff\fd\x22");
187                                 #               $conn->send_raw("\xff\xfa\x22\x01\x01\xff\xf0");
188                                 #               $conn->send_raw("\xFF\xFC\x01");
189                                 $conn->_send_file("$main::data/issue");
190                                 $conn->send_raw("login: ");
191                                 $conn->_dotimeout(60);
192                                 $conn->{echo} = 1;
193                         } else { 
194                                 &{$conn->{eproc}}() if $conn->{eproc};
195                                 $conn->disconnect();
196                         }
197                 }
198         } else {
199                 dbg("ExtMsg: error on accept ($!)") if isdbg('err');
200         }
201 }
202
203 sub start_connect
204 {
205         my $call = shift;
206         my $fn = shift;
207         my $conn = ExtMsg->new(\&main::new_channel); 
208         $conn->{outgoing} = 1;
209         $conn->conns($call);
210         
211         my $f = new IO::File $fn;
212         push @{$conn->{cmd}}, <$f>;
213         $f->close;
214         $conn->{state} = 'WC';
215         $conn->_dotimeout($deftimeout);
216         $conn->_docmd;
217 }
218
219 sub _docmd
220 {
221         my $conn = shift;
222         my $msg = shift;
223         my $cmd;
224
225         while ($cmd = shift @{$conn->{cmd}}) {
226                 chomp $cmd;
227                 next if $cmd =~ /^\s*\#/o;
228                 next if $cmd =~ /^\s*$/o;
229                 $conn->_doabort($1) if $cmd =~ /^\s*a\w*\s+(.*)/i;
230                 $conn->_dotimeout($1) if $cmd =~ /^\s*t\w*\s+(\d+)/i;
231                 $conn->_dolineend($1) if $cmd =~ /^\s*[Ll]\w*\s+\'((?:\\[rn])+)\'/i;
232                 if ($cmd =~ /^\s*co\w*\s+(\w+)\s+(.*)$/i) {
233                         unless ($conn->_doconnect($1, $2)) {
234                                 $conn->disconnect;
235                                 @{$conn->{cmd}} = [];    # empty any further commands
236                                 last;
237                         }  
238                 }
239                 if ($cmd =~ /^\s*\'([^\']*)\'\s+\'([^\']*)\'/) {
240                         $conn->_dochat($cmd, $msg, $1, $2);
241                         last;
242                 }
243                 if ($cmd =~ /^\s*cl\w+\s+(.*)/i) {
244                         $conn->_doclient($1);
245                         last;
246                 }
247                 last if $conn->{state} eq 'E';
248         }
249 }
250
251 sub _doconnect
252 {
253         my ($conn, $sort, $line) = @_;
254         my $r;
255
256         $sort = lc $sort;
257         dbg("CONNECT $conn->{cnum} sort: $sort command: $line") if isdbg('connect');
258         if ($sort eq 'telnet') {
259                 # this is a straight network connect
260                 my ($host, $port) = split /\s+/, $line;
261                 $port = 23 if !$port;
262                 $r = $conn->connect($host, $port);
263                 if ($r) {
264                         dbg("Connected $conn->{cnum} to $host $port") if isdbg('connect');
265                 } else {
266                         dbg("***Connect $conn->{cnum} Failed to $host $port $!") if isdbg('connect');
267                 }
268         } elsif ($sort eq 'agw') {
269                 # turn it into an AGW object
270                 bless $conn, 'AGWMsg';
271                 $r = $conn->connect($line);
272         } elsif ($sort eq 'ax25' || $sort eq 'prog') {
273                 local $^F = 10000;              # make sure it ain't closed on exec
274                 my ($a, $b) = IO::Socket->socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC);
275                 if ($a && $b) {
276                         $r = 1;
277                         $a->autoflush(1);
278                         $b->autoflush(1);
279                         my $pid = fork;
280                         if (defined $pid) {
281                                 if ($pid) {
282                                         close $b;
283                                         $conn->{sock} = $a;
284                                         $conn->{csort} = $sort;
285                                         $conn->{lineend} = "\cM" if $sort eq 'ax25';
286                                         $conn->{pid} = $pid;
287                                         if ($conn->{rproc}) {
288                                                 my $callback = sub {$conn->_rcv};
289                                                 Msg::set_event_handler ($a, read => $callback);
290                                         }
291                                         dbg("connect $conn->{cnum}: started pid: $conn->{pid} as $line") if isdbg('connect');
292                                 } else {
293                                         $^W = 0;
294                                         dbgclose();
295                                         STDIN->close;
296                                         STDOUT->close;
297                                         STDOUT->close;
298                                         *STDIN = IO::File->new_from_fd($b, 'r') or die;
299                                         *STDOUT = IO::File->new_from_fd($b, 'w') or die;
300                                         *STDERR = IO::File->new_from_fd($b, 'w') or die;
301                                         close $a;
302                                         unless ($main::is_win) {
303 #                                               $SIG{HUP} = 'IGNORE';
304                                                 $SIG{HUP} = $SIG{CHLD} = $SIG{TERM} = $SIG{INT} = 'DEFAULT';
305                                                 alarm(0);
306                                         }
307                                         exec "$line" or dbg("exec '$line' failed $!");
308                                 } 
309                         } else {
310                                 dbg("cannot fork");
311                                 $r = undef;
312                         }
313                 } else {
314                         dbg("no socket pair $!");
315                 }
316         } else {
317                 dbg("invalid type of connection ($sort)");
318         }
319         $conn->disconnect unless $r;
320         return $r;
321 }
322
323 sub _doabort
324 {
325         my $conn = shift;
326         my $string = shift;
327         dbg("connect $conn->{cnum}: abort $string") if isdbg('connect');
328         $conn->{abort} = $string;
329 }
330
331 sub _dotimeout
332 {
333         my $conn = shift;
334         my $val = shift;
335         dbg("connect $conn->{cnum}: timeout set to $val") if isdbg('connect');
336         $conn->{timeout}->del if $conn->{timeout};
337         $conn->{timeval} = $val;
338         $conn->{timeout} = Timer->new($val, sub{ &_timedout($conn) });
339 }
340
341 sub _dolineend
342 {
343         my $conn = shift;
344         my $val = shift;
345         dbg("connect $conn->{cnum}: lineend set to $val ") if isdbg('connect');
346         $val =~ s/\\r/\r/g;
347         $val =~ s/\\n/\n/g;
348         $conn->{lineend} = $val;
349 }
350
351 sub _dochat
352 {
353         my $conn = shift;
354         my $cmd = shift;
355         my $line = shift;
356         my $expect = shift;
357         my $send = shift;
358                 
359         if ($line) {
360                 if ($expect) {
361                         dbg("connect $conn->{cnum}: expecting: \"$expect\" received: \"$line\"") if isdbg('connect');
362                         if ($conn->{abort} && $line =~ /\Q$conn->{abort}/i) {
363                                 dbg("connect $conn->{cnum}: aborted on /$conn->{abort}/") if isdbg('connect');
364                                 $conn->disconnect;
365                                 delete $conn->{cmd};
366                                 return;
367                         }
368                         if ($line =~ /\Q$expect/i) {
369                                 if (length $send) {
370                                         dbg("connect $conn->{cnum}: got: \"$expect\" sending: \"$send\"") if isdbg('connect');
371                                         $conn->send_later("D$conn->{call}|$send");
372                                 }
373                                 delete $conn->{msg}; # get rid any input if a match
374                                 return;
375                         }
376                 }
377         }
378         $conn->{state} = 'WC';
379         unshift @{$conn->{cmd}}, $cmd;
380 }
381
382 sub _timedout
383 {
384         my $conn = shift;
385         dbg("connect $conn->{cnum}: timed out after $conn->{timeval} seconds") if isdbg('connect');
386         $conn->disconnect;
387 }
388
389 # handle callsign and connection type firtling
390 sub _doclient
391 {
392         my $conn = shift;
393         my $line = shift;
394         my @f = split /\s+/, $line;
395         my $call = uc $f[0] if $f[0];
396         $conn->conns($call);
397         $conn->{csort} = $f[1] if $f[1];
398         $conn->{state} = 'C';
399         &{$conn->{rproc}}($conn, "O$call|$conn->{csort}");
400         delete $conn->{cmd};
401         $conn->{timeout}->del if $conn->{timeout};
402 }
403
404 sub _send_file
405 {
406         my $conn = shift;
407         my $fn = shift;
408         
409         if (-e $fn) {
410                 my $f = new IO::File $fn;
411                 if ($f) {
412                         while (<$f>) {
413                                 chomp;
414                                 my $l = $_;
415                                 dbg("connect $conn->{cnum}: $l") if isdbg('connll');
416                                 $conn->send_raw($l . $conn->{lineend});
417                         }
418                         $f->close;
419                 }
420         }
421 }