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