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