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