I think I have most the SSID probs cracked.
[spider.git] / perl / client.pl
1 #!/usr/bin/perl
2 #
3 # A thing that implements dxcluster 'protocol'
4 #
5 # This is a perl module/program that sits on the end of a dxcluster
6 # 'protocol' connection and deals with anything that might come along.
7 #
8 # this program is called by ax25d or inetd and gets raw ax25 text on its input
9 # It can also be launched into the ether by the cluster program itself for outgoing
10 # connections
11 #
12 # Calling syntax is:-
13 #
14 # client.pl [callsign] [telnet|ax25|local] [[connect] [program name and args ...]]
15 #
16 # if the callsign isn't given then the sysop callsign in DXVars.pm is assumed
17 #
18 # if there is no connection type then 'local' is assumed
19 #
20 # if there is a 'connect' keyword then it will try to launch the following program
21 # and any arguments and connect the stdin & stdout of both the program and the 
22 # client together.
23 #
24 # Copyright (c) 1998 Dirk Koopman G1TLH
25 #
26 # $Id$
27
28
29
30 # search local then perl directories
31 BEGIN {
32         # root of directory tree for this system
33         $root = "/spider"; 
34         $root = $ENV{'DXSPIDER_ROOT'} if $ENV{'DXSPIDER_ROOT'};
35         
36         unshift @INC, "$root/perl";     # this IS the right way round!
37         unshift @INC, "$root/local";
38 }
39
40 use Msg;
41 use DXVars;
42 use DXDebug;
43 use IO::Socket;
44 use IPC::Open2;
45 use FileHandle;
46 use Carp;
47
48 # cease communications
49 sub cease
50 {
51         my $sendz = shift;
52         if ($conn && $sendz) {
53                 $conn->send_now("Z$call|bye...\n");
54         }
55         $stdout->flush if $stdout;
56         kill(15, $pid) if $pid;
57         sleep(1);
58         exit(0);        
59 }
60
61 # terminate program from signal
62 sub sig_term
63 {
64         cease(1);
65 }
66
67 # terminate a child
68 sub sig_chld
69 {
70         $SIG{CHLD} = \&sig_chld;
71         $waitedpid = wait;
72 }
73
74
75 sub setmode
76 {
77         if ($mode == 1) {
78                 $mynl = "\r";
79         } else {
80                 $mynl = "\n";
81         }
82         $/ = $mynl;
83 }
84
85 # handle incoming messages
86 sub rec_socket
87 {
88         my ($con, $msg, $err) = @_;
89         if (defined $err && $err) {
90                 cease(1);
91         }
92         if (defined $msg) {
93                 my ($sort, $call, $line) = $msg =~ /^(\w)(\S+)\|(.*)$/;
94                 
95                 if ($sort eq 'D') {
96                         my $snl = $mynl;
97                         my $newsavenl = "";
98                         $snl = "" if $mode == 0;
99                         if ($mode == 2 && $line =~ />$/) {
100                                 $newsavenl = $snl;
101                                 $snl = ' ';
102                         }
103                         $line =~ s/\n/\r/og if $mode == 1;
104                         #my $p = qq($line$snl);
105                         if ($buffered) {
106                                 if (length $outqueue >= 128) {
107                                         print $stdout $outqueue;
108                                         $outqueue = "";
109                                 }
110                                 $outqueue .= "$savenl$line$snl";
111                                 $lasttime = time;
112                         } else {
113                                 print $stdout $savenl, $line, $snl;;
114                         }
115                         $savenl = $newsavenl;
116                 } elsif ($sort eq 'M') {
117                         $mode = $line;          # set new mode from cluster
118                         setmode();
119                 } elsif ($sort eq 'B') {
120                         if ($buffered && $outqueue) {
121                                 print $stdout $outqueue;
122                                 $outqueue = "";
123                         }
124                         $buffered = $line;      # set buffered or unbuffered
125                 } elsif ($sort eq 'Z') { # end, disconnect, go, away .....
126                         cease(0);
127                 }         
128         }
129         $lasttime = time; 
130 }
131
132 sub rec_stdin
133 {
134         my ($fh) = @_;
135         my $buf;
136         my @lines;
137         my $r;
138         my $first;
139         my $dangle = 0;
140         
141         $r = sysread($fh, $buf, 1024);
142         #  my $prbuf;
143         #  $prbuf = $buf;
144         #  $prbuf =~ s/\r/\\r/;
145         #  $prbuf =~ s/\n/\\n/;
146         #  print "sys: $r ($prbuf)\n";
147         if ($r > 0) {
148                 if ($mode) {
149                         $buf =~ s/\r/\n/og if $mode == 1;
150                         $dangle = !($buf =~ /\n$/);
151                         if ($buf eq "\n") {
152                                 @lines = (" ");
153                         } else {
154                                 @lines = split /\n/, $buf;
155                         }
156                         if ($dangle) {          # pull off any dangly bits
157                                 $buf = pop @lines;
158                         } else {
159                                 $buf = "";
160                         }
161                         $first = shift @lines;
162                         unshift @lines, ($lastbit . $first) if ($first);
163                         foreach $first (@lines) {
164                                 #                 print "send_now $call $first\n";
165                                 $conn->send_now("D$call|$first");
166                         }
167                         $lastbit = $buf;
168                         $savenl = "";           # reset savenl 'cos we will have done a newline on input
169                 } else {
170                         $conn->send_now("D$call|$buf");
171                 }
172         } elsif ($r == 0) {
173                 cease(1);
174         }
175         $lasttime = time;
176 }
177
178 sub doconnect
179 {
180         my ($sort, $line) = @_;
181         dbg('connect', "CONNECT sort: $sort command: $line");
182         if ($sort eq 'telnet') {
183                 # this is a straight network connect
184                 my ($host) = $line =~ /host\s+(\w+)/o;
185                 my ($port) = $line =~ /port\s+(\d+)/o;
186                 $port = 23 if !$port;
187                 
188                 $sock = IO::Socket::INET->new(PeerAddr => "$host", PeerPort => "$port", Proto => 'tcp')
189                         or die "Can't connect to $host port $port $!";
190                 
191         } elsif ($sort eq 'ax25') {
192                 my @args = split /\s+/, $line;
193                 $rfh = new FileHandle;
194                 $wfh = new FileHandle;
195                 $pid = open2($rfh, $wfh, "$line") or die "can't do $line $!";
196                 dbg('connect', "got pid $pid");
197                 $wfh->autoflush(1);
198         } else {
199                 die "invalid type of connection ($sort)";
200         }
201         $csort = $sort;
202 }
203
204 sub doabort
205 {
206         my $string = shift;
207         dbg('connect', "abort $string");
208         $abort = $string;
209 }
210
211 sub dotimeout
212 {
213         my $val = shift;
214         dbg('connect', "timeout set to $val");
215         $timeout = $val;
216 }
217
218 sub dochat
219 {
220         my ($expect, $send) = @_;
221         dbg('connect', "CHAT \"$expect\" -> \"$send\"");
222     my $line;
223         
224         #       alarm($timeout);
225         
226     if ($expect) {
227                 if ($csort eq 'telnet') {
228                         $line = <$sock>;
229                         chomp;
230                 } elsif ($csort eq 'ax25') {
231                         local $/ = "\r";
232                         $line = <$rfh>;
233                         $line =~ s/\r//og;
234                 }
235                 dbg('connect', "received \"$line\"");
236                 if ($abort && $line =~ /$abort/i) {
237                         dbg('connect', "aborted on /$abort/");
238                         cease(11);
239                 }
240         }
241         if ($send && (!$expect || $line =~ /$expect/i)) {
242                 if ($csort eq 'telnet') {
243                         $sock->print("$send\n");
244                 } elsif ($csort eq 'ax25') {
245                         local $\ = "\r";
246                         $wfh->print("$send\r");
247                 }
248                 dbg('connect', "sent \"$send\"");
249         }
250 }
251
252 sub timeout
253 {
254         dbg('connect', "timed out after $timeout seconds");
255         cease(10);
256 }
257
258
259 #
260 # initialisation
261 #
262
263 $mode = 2;                      # 1 - \n = \r as EOL, 2 - \n = \n, 0 - transparent
264 $call = "";                     # the callsign being used
265 @stdoutq = ();                  # the queue of stuff to send out to the user
266 $conn = 0;                      # the connection object for the cluster
267 $lastbit = "";                  # the last bit of an incomplete input line
268 $mynl = "\n";                   # standard terminator
269 $lasttime = time;               # lasttime something happened on the interface
270 $outqueue = "";                 # the output queue length
271 $buffered = 1;                  # buffer output
272 $savenl = "";                   # an NL that has been saved from last time
273 $timeout = 30;                  # default timeout for connects
274 $abort = "";                    # the current abort string
275 $cpath = "$root/connect";               # the basic connect directory
276
277 $pid = 0;                       # the pid of the child program
278 $csort = "";                    # the connection type
279 $sock = 0;                      # connection socket
280
281 $stdin = *STDIN;
282 $stdout = *STDOUT;
283 $rfh = 0;
284 $wfh = 0;
285
286
287 #
288 # deal with args
289 #
290
291 $call = uc shift @ARGV;
292 $call = uc $myalias if !$call; 
293 $connsort = lc shift @ARGV;
294 $connsort = 'local' if !$connsort;
295
296 #
297 # strip off any SSID if it is a telnet connection 
298 #
299 # SSID's are a problem, basically we don't allow them EXCEPT for the special case
300 # of local users. i.e. you can have a cluster call with an SSID and a usercall with
301 # an SSID and they are different to the system to those without SSIDs
302 #
303
304 $call =~ s/-\d+$//o if $mode eq 'telnet';
305 $mode = ($connsort eq 'ax25') ? 1 : 2;
306 setmode();
307
308 if ($call eq $mycall) {
309         print $stdout "You cannot connect as your cluster callsign ($mycall)", $nl;
310         cease(0);
311 }
312
313 $stdout->autoflush(1);
314
315 $SIG{'INT'} = \&sig_term;
316 $SIG{'TERM'} = \&sig_term;
317 $SIG{'HUP'} = 'IGNORE';
318 $SIG{'CHLD'} = \&sig_chld;
319
320 dbgadd('connect');
321
322 # is this an out going connection?
323 if ($connsort eq "connect") {
324         my $mcall = lc $call;
325         
326         open(IN, "$cpath/$mcall") or cease(2);
327         @in = <IN>;
328         close IN;
329         
330         #       alarm($timeout);
331         
332         for (@in) {
333                 chomp;
334                 next if /^\s*\#/o;
335                 next if /^\s*$/o;
336                 doconnect($1, $2) if /^\s*co\w*\s+(\w+)\s+(.*)$/io;
337                 doabort($1) if /^\s*a\w*\s+(.*)/io;
338                 dotimeout($1) if /^\s*t\w*\s+(\d+)/io;
339                 dochat($1, $2) if /\s*\'(.*)\'\s+\'(.*)\'/io;          
340         }
341         
342     dbg('connect', "Connected to $call, starting normal protocol");
343         dbgsub('connect');
344         
345         # if we get here we are connected
346         if ($csort eq 'ax25') {
347                 #               open(STDIN, "<&R"); 
348                 #               open(STDOUT, ">&W"); 
349                 #               close R;
350                 #               close W;
351         $stdin = $rfh;
352                 $stdout = $wfh;
353         } elsif ($csort eq 'telnet') {
354                 #               open(STDIN, "<&$sock"); 
355                 #               open(STDOUT, ">&$sock"); 
356                 #               close $sock;
357                 $stdin = $sock;
358                 $stdout = $sock;
359         }
360     alarm(0);
361     $outbound = 1;
362         $connsort = $csort;
363         $stdout->autoflush(1);
364         close STDIN;
365         close STDOUT;
366         close STDERR;
367         
368         
369         $mode = ($connsort =~ /^ax/o) ? 1 : 2;
370         setmode();
371 }
372
373 setmode();
374
375 $conn = Msg->connect("$clusteraddr", $clusterport, \&rec_socket);
376 if (! $conn) {
377         if (-r "$data/offline") {
378                 open IN, "$data/offline" or die;
379                 while (<IN>) {
380                         s/\n/\r/og if $mode == 1;
381                         print $stdout;
382                 }
383                 close IN;
384         } else {
385                 print $stdout "Sorry, the cluster $mycall is currently off-line", $mynl;
386         }
387         cease(0);
388 }
389
390 $let = $outbound ? 'O' : 'A';
391 $conn->send_now("$let$call|$connsort");
392 Msg->set_event_handler($stdin, "read" => \&rec_stdin);
393
394 for (;;) {
395         my $t;
396         Msg->event_loop(1, 0.010);
397         $t = time;
398         if ($t > $lasttime) {
399                 if ($outqueue) {
400                         print $stdout $outqueue;
401                         $outqueue = "";
402                 }
403                 $lasttime = $t;
404         }
405 }
406
407 exit(0);