c5d6d85486ff5092ec958e89f4714ded0ae2743b
[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 #
9 # Copyright (c) 2001 - Dirk Koopman G1TLH
10 #
11 #       Modified Jan 2006 by John Wiseman G8BPQ to support connections to BPQ32 node,
12 #               and fix pattern matching on 'chat' abort handling
13 #
14
15 package ExtMsg;
16
17 use strict;
18 use Msg;
19 use DXVars;
20 use DXUtil;
21 use DXDebug;
22 use IO::File;
23 use IO::Socket;
24 use IPC::Open3;
25
26 use vars qw(@ISA $deftimeout);
27
28 @ISA = qw(Msg);
29 $deftimeout = 60;
30
31 sub login
32 {
33         goto &main::login;        # save some writing, this was the default
34 }
35
36 sub enqueue
37 {
38         my ($conn, $msg) = @_;
39         unless ($msg =~ /^[ABZ]/) {
40                 if ($msg =~ m{^E[-\w\/]+\|([01])} && $conn->{csort} eq 'telnet') {
41                         $conn->{echo} = $1;
42                         if ($1) {
43 #                               $conn->send_raw("\xFF\xFC\x01");
44                         } else {
45 #                               $conn->send_raw("\xFF\xFB\x01");
46                         }
47                 } else {
48                         $msg =~ s{^[-\w\/]+\|}{};
49                         push (@{$conn->{outqueue}}, $msg . $conn->{lineend});
50                 }
51         }
52 }
53
54 sub send_raw
55 {
56         my ($conn, $msg) = @_;
57         dbg((ref $conn) . " connect $conn->{cnum}: $msg") if $conn->{state} ne 'C' && isdbg('connect');
58         $conn->SUPER::send_raw($msg);
59 }
60
61 sub echo
62 {
63         my $conn = shift;
64         $conn->{echo} = shift;
65 }
66
67 sub dequeue
68 {
69         my $conn = shift;
70         my $msg;
71
72         if ($conn->ax25 && exists $conn->{msg}) {
73                 $conn->{msg} =~ s/\cM/\cJ/g;
74         }
75         if ($conn->{state} eq 'WC') {
76                 if (exists $conn->{cmd}) {
77                         if (@{$conn->{cmd}}) {
78                                 dbg("connect $conn->{cnum}: $conn->{msg}") if isdbg('connect');
79                                 $conn->_docmd($conn->{msg});
80                         } 
81                 }
82                 if ($conn->{state} eq 'WC' && exists $conn->{cmd} && @{$conn->{cmd}} == 0) {
83                         $conn->to_connected($conn->{call}, 'O', $conn->{csort});
84                 }
85         } elsif ($conn->{msg} =~ /\cJ/) {
86                 my @lines =  $conn->{msg} =~ /([^\cM\cJ]*)\cM?\cJ/g;
87                 if ($conn->{msg} =~ /\cJ$/) {
88                         delete $conn->{msg};
89                 } else {
90                         $conn->{msg} =~ s/([^\cM\cJ]*)\cM?\cJ//g;
91                 }
92                 while (defined ($msg = shift @lines)) {
93                         dbg("connect $conn->{cnum}: $msg") if $conn->{state} ne 'C' && isdbg('connect');
94                 
95                         $msg =~ s/\xff\xfa.*\xff\xf0|\xff[\xf0-\xfe].//g; # remove telnet options
96 #                       $msg =~ s/[\x00-\x08\x0a-\x19\x1b-\x1f\x80-\x9f]/./g;         # immutable CSI sequence + control characters
97                         
98                         if ($conn->{state} eq 'C') {
99                                 &{$conn->{rproc}}($conn, "I$conn->{call}|$msg");
100                         } elsif ($conn->{state} eq 'WL' ) {
101                                 $msg = uc $msg;
102                                 if ($conn->{sort} =~ /^I/ && (my ($ip, $from) = $msg =~ /^PROXY TCP[46] ([\da-fA-F:\.]+) ([\da-fA-F:\.]+)/) ) {
103                                         # SOMEONE appears to have affixed an HA Proxy to my connection
104                                         $ip =~ s|^::ffff:||; # chop off leading pseudo IPV6 stuff on dual stack listeners
105                                         $from =~ s|^::ffff:||;
106                                         if ($from eq $conn->{peerhost}) {
107                                                 dbg("ExtMsg: connect - PROXY IP change from '$conn->{peerhost}' -> '$ip'");
108                                                 $conn->{peerhost} = $ip;
109                                         } else {
110                                                 dbg("ExtMsg: connect - PROXY someone ($from) is trying to spoof '$ip'");
111                                                 $conn->send_now("Sorry $msg is an invalid callsign");
112                                                 $conn->disconnect;
113                                         }
114                                 } elsif (is_callsign($msg)) {
115                                         if ($main::allowslashcall || $msg !~ m|/|) {
116                                                 my $sort = $conn->{csort};
117                                                 $sort = 'local' if $conn->{peerhost} =~ /127\.\d+\.\d+\.\d+$/ || $conn->{peerhost} eq '::1';
118                                                 my $uref;
119                                                 if ($main::passwdreq || ($uref = DXUser::get_current($msg)) && $uref->passwd ) {
120                                                         $conn->conns($msg);
121                                                         $conn->{state} = 'WP';
122                                                         $conn->{decho} = $conn->{echo};
123                                                         $conn->{echo} = 0;
124                                                         $conn->send_raw('password: ');
125                                                 } else {
126                                                         $conn->to_connected($msg, 'A', $sort);
127                                                 }
128                                         } else {
129                                                 $conn->send_now("Sorry $msg is an invalid callsign");
130                                                 $conn->disconnect;
131                                         }
132                                 } else {
133                                         $conn->send_now("Sorry $msg is an invalid callsign");
134                                         $conn->disconnect;
135                                 }
136                         } elsif ($conn->{state} eq 'WP' ) {
137                                 my $uref = DXUser::get_current($conn->{call});
138                                 $msg =~ s/[\r\n]+$//;
139                                 if ($uref && $msg eq $uref->passwd) {
140                                         my $sort = $conn->{csort};
141                                         $conn->{echo} = $conn->{decho};
142                                         delete $conn->{decho};
143                                         $sort = 'local' if $conn->{peerhost} =~ /127\.\d+\.\d+\.\d+$/ || $conn->{peerhost} eq '::1';
144                                         $conn->{usedpasswd} = 1;
145                                         $conn->to_connected($conn->{call}, 'A', $sort);
146                                 } else {
147                                         $conn->send_now("Sorry");
148                                         $conn->disconnect;
149                                 }
150                         } elsif ($conn->{state} eq 'WC') {
151                                 if (exists $conn->{cmd} && @{$conn->{cmd}}) {
152                                         $conn->_docmd($msg);
153                                         if ($conn->{state} eq 'WC' && exists $conn->{cmd} &&  @{$conn->{cmd}} == 0) {
154                                                 $conn->to_connected($conn->{call}, 'O', $conn->{csort});
155                                         }
156                                 }
157                         }
158                 }
159         }
160 }
161
162 sub to_connected
163 {
164         my ($conn, $call, $dir, $sort) = @_;
165         $conn->{state} = 'C';
166         $conn->conns($call);
167         delete $conn->{cmd};
168         $conn->{timeout}->del if $conn->{timeout};
169         delete $conn->{timeout};
170         $conn->{csort} = $sort;
171         &{$conn->{rproc}}($conn, "$dir$call|$sort");
172         $conn->_send_file(localdata("connected")) unless $conn->{outgoing};
173 }
174
175 sub new_client {
176         
177         my $server_conn = shift;
178         my $client = shift;
179         my $conn = $server_conn->SUPER::new_client($client);
180         # send login prompt
181         $conn->{state} = 'WL';
182         $conn->_send_file(localdata("issue"));
183         $conn->send_raw("login: ");
184         $conn->_dotimeout(60);
185         $conn->{echo} = 1;
186 }
187
188 sub start_connect
189 {
190         my $call = shift;
191         my $fn = shift;
192         my $conn = ExtMsg->new(\&main::new_channel); 
193         $conn->{outgoing} = 1;
194         $conn->conns($call);
195         
196         my $f = new IO::File $fn;
197         push @{$conn->{cmd}}, <$f>;
198         $f->close;
199         $conn->{state} = 'WC';
200         $conn->_dotimeout($deftimeout);
201         $conn->_docmd;
202 }
203
204 sub _docmd
205 {
206         my $conn = shift;
207         my $msg = shift;
208         my $cmd;
209
210         while ($cmd = shift @{$conn->{cmd}}) {
211                 chomp $cmd;
212                 next if $cmd =~ /^\s*\#/o;
213                 next if $cmd =~ /^\s*$/o;
214                 $conn->_doabort($1) if $cmd =~ /^\s*a\w*\s+(.*)/i;
215                 $conn->_dotimeout($1) if $cmd =~ /^\s*t\w*\s+(\d+)/i;
216                 $conn->_dolineend($1) if $cmd =~ /^\s*[Ll]\w*\s+\'((?:\\[rn])+)\'/i;
217                 if ($cmd =~ /^\s*co\w*\s+(\w+)\s+(.*)$/i) {
218                         unless ($conn->_doconnect($1, $2)) {
219                                 $conn->disconnect;
220                                 @{$conn->{cmd}} = [];    # empty any further commands
221                                 last;
222                         }  
223                 }
224                 if ($cmd =~ /^\s*\'([^\']*)\'\s+\'([^\']*)\'/) {
225                         $conn->_dochat($cmd, $msg, $1, $2);
226                         last;
227                 }
228                 if ($cmd =~ /^\s*cl\w+\s+(.*)/i) {
229                         $conn->_doclient($1);
230                         last;
231                 }
232                 last if $conn->{state} eq 'E';
233         }
234 }
235
236 sub _doconnect
237 {
238         my ($conn, $sort, $line) = @_;
239         my $r;
240
241         $sort = lc $sort;
242         dbg("CONNECT $conn->{cnum} sort: $sort command: $line") if isdbg('connect');
243         if ($sort eq 'telnet') {
244                 # this is a straight network connect
245                 my ($host, $port) = split /\s+/, $line;
246                 $port = 23 if !$port;
247                 $r = $conn->connect($host, $port);
248                 if ($r) {
249                         dbg("Connected $conn->{cnum} to $host $port") if isdbg('connect');
250                 } else {
251                         dbg("***Connect $conn->{cnum} Failed to $host $port $!") if isdbg('connect');
252                 }
253         } elsif ($sort eq 'agw') {
254                 # turn it into an AGW object
255                 bless $conn, 'AGWMsg';
256                 $r = $conn->connect($line);
257         } elsif ($sort eq 'bpq') {
258                 # turn it into an BPQ object
259                 bless $conn, 'BPQMsg';
260                 $r = $conn->connect($line);
261         } elsif ($sort eq 'ax25' || $sort eq 'prog') {
262                 $r = $conn->start_program($line, $sort);
263         } else {
264                 dbg("invalid type of connection ($sort)");
265         }
266         $conn->disconnect unless $r;
267         return $r;
268 }
269
270 sub _doabort
271 {
272         my $conn = shift;
273         my $string = shift;
274         dbg("connect $conn->{cnum}: abort $string") if isdbg('connect');
275         $conn->{abort} = $string;
276 }
277
278 sub _dotimeout
279 {
280         my $conn = shift;
281         my $val = shift;
282         dbg("connect $conn->{cnum}: timeout set to $val") if isdbg('connect');
283         $conn->{timeout}->del if $conn->{timeout};
284         $conn->{timeval} = $val;
285         $conn->{timeout} = Timer->new($val, sub{ &_timedout($conn) });
286 }
287
288 sub _dolineend
289 {
290         my $conn = shift;
291         my $val = shift;
292         dbg("connect $conn->{cnum}: lineend set to $val ") if isdbg('connect');
293         $val =~ s/\\r/\r/g;
294         $val =~ s/\\n/\n/g;
295         $conn->{lineend} = $val;
296 }
297
298 sub _dochat
299 {
300         my $conn = shift;
301         my $cmd = shift;
302         my $line = shift;
303         my $expect = shift;
304         my $send = shift;
305                 
306         if ($line) {
307                 if ($expect) {
308                         dbg("connect $conn->{cnum}: expecting: \"$expect\" received: \"$line\"") if isdbg('connect');
309                         if ($conn->{abort} && $line =~ /$conn->{abort}/i) {
310                                 dbg("connect $conn->{cnum}: aborted on /$conn->{abort}/") if isdbg('connect');
311                                 $conn->disconnect;
312                                 delete $conn->{cmd};
313                                 return;
314                         }
315                         if ($line =~ /\Q$expect/i) {
316                                 if (length $send) {
317                                         dbg("connect $conn->{cnum}: got: \"$expect\" sending: \"$send\"") if isdbg('connect');
318                                         $conn->send_later("D$conn->{call}|$send");
319                                 }
320                                 delete $conn->{msg}; # get rid any input if a match
321                                 return;
322                         }
323                 }
324         }
325         $conn->{state} = 'WC';
326         unshift @{$conn->{cmd}}, $cmd;
327 }
328
329 sub _timedout
330 {
331         my $conn = shift;
332         dbg("connect $conn->{cnum}: timed out after $conn->{timeval} seconds") if isdbg('connect');
333         $conn->disconnect;
334 }
335
336 # handle callsign and connection type firtling
337 sub _doclient
338 {
339         my $conn = shift;
340         my $line = shift;
341         my @f = split /\s+/, $line;
342         my $call = uc $f[0] if $f[0];
343         $conn->conns($call);
344         $conn->{csort} = $f[1] if $f[1];
345         $conn->{state} = 'C';
346         eval {$conn->{peerhost} = $conn->{sock}->handle->peerhost} unless $conn->ax25;
347         &{$conn->{rproc}}($conn, "O$call|$conn->{csort}");
348         delete $conn->{cmd};
349         $conn->{timeout}->del if $conn->{timeout};
350 }
351
352 sub _send_file
353 {
354         my $conn = shift;
355         my $fn = shift;
356         
357         if (-e $fn) {
358                 my $f = new IO::File $fn;
359                 if ($f) {
360                         while (<$f>) {
361                                 chomp;
362                                 my $l = $_;
363                                 dbg("connect $conn->{cnum}: $l") if isdbg('connll');
364                                 $conn->send_raw($l . $conn->{lineend});
365                         }
366                         $f->close;
367                 }
368         }
369 }