summaryrefslogtreecommitdiff
path: root/conmux/conmux
blob: 12bc871b4184dade0b63f03139cfba4587df8a98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
#!/usr/bin/perl
#
# conmux -- the main console multiplexor daemon
# 
# Main console multiplexor daemon.  There is one of these daemons for
# each open console supported in the system.  Clients are directed to
# this daemon via the conmux-registry deamon.
#
# (C) Copyright IBM Corp. 2004, 2005, 2006
# Author: Andy Whitcroft <andyw@uk.ibm.com>
#
# The Console Multiplexor is released under the GNU Public License V2
#
use strict;

use FindBin;
use Symbol qw(gensym);
use IO::Socket;
use IO::Multiplex;
use IPC::Open3;
use URI::Escape;
use Net::Domain;

# Find our internal libraries.
use lib $FindBin::Bin;
use lib "$FindBin::Bin/../lib/";
use lib "$FindBin::Bin/lib/";
use Conmux;

our $P = 'conmux';
our $debug = 0;

$SIG{'CHLD'} = "IGNORE";

$| = 1;

#
# CALLBACK: this class is used to provide a timed callback.  The multiplexor
# libarary allows us to set a timeout on any open file we have registered.
# So, we open a new file descriptor to /dev/null and set a timeout on that.
#
package Callback;

sub new {
	my ($class, $mux, $who, $time) = @_;
	my $self = bless { 'who' => $who }, $class;

	my ($fh);

	print "Callback::new [$self] mux<$mux> who<$who> time<$time>\n"
		if ($main::debug);

	# Open a file handle to nothing, we need this to hang the timeout
	# on in the multiplexor.  It will fail with a mux_eof, which we ignore.
	open($fh, "</dev/null") || die "$P: /dev/null: open failed - $!\n";

	$mux->add($fh);
	$mux->set_callback_object($self, $fh);

	$mux->set_timeout($fh, $time);

	$self;
}
sub mux_timeout {
	my ($self, $mux, $fh) = @_;

	print "Callback::mux_timeout [$self] mux<$mux> fh<$fh>\n"
		if ($main::debug);

	$self->{'who'}->callback_timeout();

	$mux->close($fh);
}
sub DESTROY {
	my ($self) = @_;
	print "Callback::DESTROY [$self]\n" if ($main::debug);
}

#
# LISTENER SOCKET: creates an intenet listener for new clients and
# connects them to the junction provided.
#
package ListenerSocket;

sub new {
	my ($class, $mux, $port) = @_;
	my $self = bless { 'mux' => $mux }, $class;

	print "ListenerSocket::new [$self] mux<$mux> port<$port>\n"
		if ($main::debug);

	$self->initialise($port);

	$self;
}

sub initialise {
	my ($self, $port) = @_;
	my ($sock);

	print "ListenerSocket::initialise [$self] port<$port> "
		if ($main::debug);

	# Create a listening socket and add it to the multiplexor.
	my $sock = new IO::Socket::INET(Proto     => 'tcp',
					LocalPort => $port,
					Listen    => 4,
					ReuseAddr => 1)
		or die "socket: $@";

	print "  adding $self $sock\n" if ($main::debug);
	$self->mux->listen($sock);
	$self->mux->set_callback_object($self, $sock);
	$self->listener($sock);
}

# DATA accessors.
sub mux {
	my $self = shift;
	if (@_) { $self->{'mux'} = shift }
	return $self->{'mux'};
}
sub listener {
	my $self = shift;
	if (@_) { $self->{'listener'} = shift }
	return $self->{'listener'};
}

sub address {
	my ($self) = @_;
	Net::Domain::hostfqdn() . ':' . $self->{'listener'}->sockport();
}

# JUNCTION: callbacks.
##sub junctionInput {
##}
##sub junctionEOF {
##	my ($self) = @_;
##	
##	$self->{'junction'}->junctionRemove($self, 'console-client');
##	$self->{'mux'}->close($self->{'listener'});
##}

# Handle new connections by instantiating a new client class.
sub mux_connection {
	my ($self, $mux, $fh) = @_;
	my ($client);

	print "ListenerSocket::mux_connection [$self] mux<$mux> fh<$fh>\n"
		if ($main::debug);

	# Make a new client connection.
	$client = ClientCmd->new($mux, $fh);
	print "  new connection $self $client\n" if ($main::debug);
}

sub DESTROY {
	my ($self) = @_;

	print "ListenerSocket::DESTROY [$self]\n" if ($main::debug);

	close($self->listener);
}

#
# JUNCTION: generic junction box object, connects names groups of objects
# to other named groups.
#
# Expects the following callbacks to be defined on each object registered:
#   junctionInput($from, $data)
#   junctionEOF($from, $to)
# 
package Junction;

sub new {
	my ($class) = @_;
	my $self = bless { }, $class;

	print "Junction::new [$self]\n" if ($main::debug);

	$self;
}

sub junctionAdd {
	my ($self, $client) = @_;

	print "Junction::junctionAdd [$self] client<$client>\n"
		if ($main::debug);

	# Add ourselves to the list of recipients.
	$self->{$client} = $client;
}

sub junctionInput {
	my ($self, $client, $data) = @_;
	my ($c);

	print "Junction::junctionInput [$self] client<$client> " .
		"data<$data>\n" if ($main::debug);

	# Send this data on to the clients listed in the output list.
	for $c (values %{$self}) {
		print "  sending to $c\n" if ($main::debug);
		$c->junctionInput($client, $data);
	}
}
sub junctionEOF {
	my ($self, $client) = @_;
	my ($c);

	print "Junction::junctionEOF [$self] client<$client>\n"
		if ($main::debug);

	# Send this eof on to the clients listed in the output list.
	for $c (values %{$self}) {
		print "  sending to $c\n" if ($main::debug);
		$c->junctionEOF($client);
	}
}
sub junctionRemove {
	my ($self, $client) = @_;

	print "Junction::junctionRemove [$self] client<$client>\n"
		if ($main::debug);

	# Drop this client from our lists.
	delete $self->{$client};
}

#
# PAYLOAD: generic payload object, connects itself to the requisite junction.
#
package Payload;

my %payloads = ();
my $payloads = 0;

sub lookup {
	my ($class, $name) = @_;

	$payloads{$name};
}

sub found {
	my ($class, $name, $self) = @_;

	print "Payloads::found name<$name> self<$self>\n" if ($main::debug);

	$payloads{$name} = $self;
	$payloads++;
}
sub lost {
	my ($class, $name, $self) = @_;

	print "Payloads::lost name<$name> self<$self>\n" if ($main::debug);

	undef $payloads{$name};
	if (--$payloads == 0) {
		exit(0);
	}
}

sub new {
	my ($class, $name, $title, $mux, @a) = @_;
	my $self = bless { }, $class;

	print "Payload::new [$self] name<$name> title<$title> mux<$mux>\n"
		if ($main::debug);

	Payload->found($name, $self);

	$self->name($name);
	$self->title($title);
	$self->mux($mux);
	$self->enabled(1);

	$self->cin(Junction->new);
	$self->cout(Junction->new);

	$self->initialise(@a);

	$self;
}

# Data accessors.
sub name {
	my $self = shift;
	if (@_) { $self->{'name'} = shift }
	return $self->{'name'};
}
sub title {
	my $self = shift;
	if (@_) { $self->{'title'} = shift }
	return $self->{'title'};
}
sub mux {
	my $self = shift;
	if (@_) { $self->{'mux'} = shift }
	return $self->{'mux'};
}
sub cin {
	my $self = shift;
	if (@_) { $self->{'cin'} = shift }
	return $self->{'cin'};
}
sub cout {
	my $self = shift;
	if (@_) { $self->{'cout'} = shift }
	return $self->{'cout'};
}
sub enabled {
	my $self = shift;
	if (@_) { $self->{'enabled'} = shift }
	return $self->{'enabled'};
}
sub connected {
	my $self = shift;
	if (@_) { $self->{'connected'} = shift }
	$self->transition();
	return $self->{'connected'};
}
sub transition {
	my $self = shift;
	my $time = time;
	if (($time - $self->{'trans_minor'}) > 30) {
		$self->{'trans_major'} = $time;
	}
	$self->{'trans_minor'} = $time;
}
sub retry_timeout {
	my $self = shift;
	my $time = time - $self->{'trans_major'};

	if ($time < 60) {
		return 1;
	} elsif ($time < 120) {
		return 10;
	} else {
		return 30;
	}
}
sub state {
	my $self = shift;
	my $ctime = $self->{'connected'};
	my $ttime = $self->{'trans_major'};
	my $time = time;

	if ($ctime && ($time - $ctime) > 30) {
		"connected";
	} elsif ($ttime && ($time - $ttime) < 60) {
		"transition";
	} else {
		"disconnected";
	}
}

sub initialise {
	my ($self) = @_;
	my ($sock);

	print "Payload::initialise [$self]\n" if ($main::debug);

	# Ensure we recieve client input.
	$self->cin->junctionAdd($self);

	$self->connected(time);
}

# Telnet constants.
my $TN_IAC      = sprintf("%c", 255);
my $TN_DONT     = sprintf("%c", 254);
my $TN_DO       = sprintf("%c", 253);
my $TN_WONT     = sprintf("%c", 252);
my $TN_WILL     = sprintf("%c", 251);
my $TN_SB       = sprintf("%c", 250);
my $TN_SE       = sprintf("%c", 240);
my $TN_BREAK    = sprintf("%c", 243);

my $TNOPT_ECHO	= sprintf("%c", 1);
my $TNOPT_SGA	= sprintf("%c", 3);

#
# If we get here then we have accumulated a complete telnet
# negotiation string.
#
# Telnet negotiation protocol - RFC#854:
#
# DO       We are being asked to DO an option
# DONT     We are being asked to NOT DO an option
# WILL     We are being told they will DO an option
# WONT     We are being told they will NOT DO an option
#
# DO/DONT requests indicate we should {en,dis}able a mode.
# We are expected to respond with WILL or WONT.  To prevent
# loops, we should not respond if the request matches our
# current mode.
#
# WILL/WONT requests indicate the other end would like to
# {en,dis}able a mode.  We are expected to respond with
# DO/DONT.
#
# If we want a particular mode {en,dis}abled then we may start
# negotiation of that mode with a WILL/WONT.
#
# We want the other end to perform echo by default so we will
# DO any request for ECHO and DONT all other requests.
#

sub mux_input {
	my ($self, $mux, $fh, $input) = @_;
	my ($client);

	print "Payload::mux_input [$self] mux<$mux> fh<$fh> input<$$input>\n"
		if ($main::debug);

	while ($$input ne "") {
		# Ordinary text.
		if ($$input =~ s/^([^$TN_IAC]+)//) {
			# Data coming in from the payload, this needs to go to
			# all of the clients.
			$self->cout->junctionInput($self, $1);
			next;
		}

		# IAC,SB,...,SE
		if ($$input =~ s/^$TN_IAC$TN_SB([^$TN_SE]+)$TN_SE//) {
			print "SB\n" if ($main::debug);
			next;
		}
		# IAC,[DO|DONT|WILL|WONT],<what>
		if ($$input =~ s/^$TN_IAC$TN_DO(.)//) {
			my $c = unpack("C", $1);
			print "DO<$c:$1>\n" if ($main::debug);
			# We are DONT on all options so WONT all requests.
			$self->junctionInput($self, "$TN_IAC$TN_WONT$1");
			next;
		}
		if ($$input =~ s/^$TN_IAC$TN_DONT(.)//) {
			my $c = unpack("C", $1);
			print "DONT<$c:$1>\n" if ($main::debug);
			# We are already DONT on all options, no reply.
			next;
		}
		if ($$input =~ s/^$TN_IAC$TN_WILL(.)//) {
			my $c = unpack("C", $1);
			print "WILL<$c:$1>\n" if ($main::debug);

			my $reply = $TN_DONT;
			if ($1 == $TNOPT_ECHO || $1 == $TNOPT_SGA) {
				$reply = $TN_DO;
			}
			$self->junctionInput($self, "$TN_IAC$reply$1");
			next;
		}
		if ($$input =~ s/^$TN_IAC$TN_WONT(.)//) {
			my $c = unpack("C", $1);
			print "WONT<$c:$1>\n" if ($main::debug);
			$self->junctionInput($self, "$TN_IAC$TN_DONT$1");
			next;
		}
		# IAC,<option>
		if ($$input =~ s/^$TN_IAC([^$TN_SB$TN_DO$TN_DONT$TN_WILL$TN_WONT])//) {
			print "OPTION<$1>\n" if ($main::debug);
			next;
		}

		# Incomplete ...
		if ($$input =~ /^$TN_IAC/) {
			return;
		}
	}
}
sub junctionInput {
	my ($self, $from, $data) = @_;
	my ($fh);

	print "Payload::junctionInput [$self] from<$from> data<$data>\n"
		if ($main::debug);

	##$self->{'mux'}->write($self->{'wfh'}, $data);
	# If we are connected ...
	if ($self->{'wfh'}) {
		$fh = $self->{'wfh'};
		print $fh $data;
	} else {
		$from->junctionInput($self, "<<<NOT CONNECTED>>>\n");
	}
}

sub mux_eof {
	my ($self, $mux, $fh) = @_;
	my ($client);

	print "Payload::mux_eof [$self] mux<$mux> fh<$fh>\n" if ($main::debug);

	# Check for a restartable connection.
	if ($self->can("restart")) {
		my ($timeout) = $self->retry_timeout();
		
		$self->cout->junctionInput($self, 
			"<<<PAYLOAD LOST ... retrying in $timeout secs>>>\n");

		# Schedule a timeout to trigger a reconnect.
		Callback->new($mux, $self, $timeout);

	} else {
		$self->cout->junctionEOF($self);
		$self->cin->junctionRemove($self);

		Payload->lost($self->name, $self);
	}

	# Close down the payload ...
	$mux->close($self->{'rfh'});
	##$mux->remove($self->{'wfh'});
}

sub mux_close {
	my ($self, $mux, $fh) = @_;

	$self->connected(0);

	#close($self->{'rfh'});
	close($self->{'wfh'});
	undef $self->{'rfh'};
	undef $self->{'wfh'};

	if ($self->{'pid'}) {
		# Kill the process group for this pid.
		kill 1, 0 - $self->{'pid'};
		undef $self->{'pid'};
	}
}

sub callback_timeout {
	my ($self) = @_;

	print "Payload::callback_timeout [$self]\n" if ($main::debug);

	if ($self->enabled) {
		$self->cout->junctionInput($self, "<<<PAYLOAD RESTART>>>\n");
		$self->openPayload();
	} else {
		$self->cout->junctionInput($self, "<<<PAYLOAD DISABLED>>>\n");
	}
}

sub closePayload {
	my ($self) = @_;

	if ($self->connected) {
		$self->cout->junctionInput($self, "<<<PAYLOAD CLOSED>>>\n");
		
		# Close down the payload ...
		$self->mux->close($self->{'rfh'});
	}
	if ($self->enabled) {
		$self->enabled(0);
		return 1;
	} else {
		return 0;
	}
}
sub openPayload {
	my ($self) = @_;

	$self->enabled(1);
	if (!$self->connected) {
		if ($self->can("restart")) {
			$self->restart();

			return 1;
		}
	}
	return 0;
}

sub helpAdd {
	my ($self, $cmd, $msg) = @_;

	push(@{$self->{'help'}}, [ $cmd, $msg ]);
}

sub commandHelp {
	my ($self) = @_;
	my @entries = (
		[ 'break', 'send a break sequence' ]
	);
	
	if (defined $self->{'help'}) {
		( @entries, @{$self->{'help'}} );

	} else {
		@entries;
	}
}
sub commandAdd {
	my ($self, $cmd, @a) = @_;

	$self->{'cmd'}->{$cmd} = [ @a ];
}
sub commandExec {
	my ($self, $client, $cmd, $a) = @_;
	my ($exe);

	print "Payload::commandExec [$self] client<$client> cmd<$cmd> a<$a>\n"
		if ($main::debug);

	$exe = $self->{'cmd'}->{$cmd};

	if ($cmd eq "break") {
		# Send a telnet break ...
		$self->junctionInput($self, "$TN_IAC$TN_BREAK");
		return;

	} elsif ($cmd eq "close") {
		if (!$self->enabled && !$self->connected) {
			$client->junctionInput($self,
				"console already closed\n");

		} elsif ($self->closePayload()) {
			$self->cout->junctionInput($self, "(" . $client->id .
				") triggered a console close\n");

		} else {
			$client->junctionInput($self, "ERROR: close failed\n");
		}
		return;

	} elsif ($cmd eq "open") {
		if ($self->connected) {
			$client->junctionInput($self, "console already open\n");

		} elsif ($self->openPayload()) {
			$self->cout->junctionInput($self, "(" . $client->id .
				") triggered a console open\n");

		} else {
			$client->junctionInput($self, "open failed\n");
		}
		return;
	} 

	# Ensure we error if we have no command.
	if (!$exe) {
		$client->junctionInput($self, "Command not recognised\n");
		return;
	} 

	my ($msg, $run) = @{$exe};
	if ($msg ne '') {
		$self->cout->junctionInput($self, "(" . $client->id .
			") $msg\n");
	}

	local(*IN, *OUT, *ERR);
	my ($cmd, @args) = split(m/'(.*?)'|"(.*?)"|\s(.*?)\s/g, $run . " $a");
	my @opts;
	for (my $i=0; $i < @args; $i++) {
		next if not $args[$i];
		push(@opts, $args[$i]);
	}
	my $pid = IPC::Open3::open3(*IN, *OUT, *ERR, $cmd, @opts);
	close(*IN{IO});

	# XXX: this should not be happening here.
	$self->mux->add(*OUT{IO});
	my $data = ClientData->new($self->mux, *OUT{IO});
	$data->{'id'} = "cmd:$cmd stdout";

	$data->payload($self);
	$data->cout($self->cout);

	# XXX: this should not be happening here.
	$self->mux->add(*ERR{IO});
	my $data = ClientData->new($self->mux, *ERR{IO});
	$data->{'id'} = "cmd:$cmd stderr";

	$data->payload($self);
	$data->cout($client);
}

sub DESTROY {
	my ($self) = @_;

	print "Payload::DESTROY [$self]\n" if ($main::debug);
}

#
# PAYLOAD APPLICATION: handles forking off a command as a payload.
#
package PayloadApplication;
use base 'Payload';

sub initialise {
	my ($self, $cmd) = @_;
	my ($pid, $wfh, $rfh);

	print "PayloadApplication::initialise [$self] cmd<$cmd>" 
	if ($main::debug);

	$self->SUPER::initialise();

	# XXX: we cannot use the write buffering offered by the mux package
	# without suffering a read error from the PWR file handle, there
	# is no a way to add a write-only channel.

	$self->{'args'} = $cmd;

	# Start the payload ...
	$pid = IPC::Open3::open3($wfh, $rfh, 0, "setsid " . $cmd);

	$self->{'rfh'} = $rfh;
	$self->{'wfh'} = $wfh;
	$self->{'pid'} = $pid;

	$self->mux->add($rfh);
	##$mux->add($wfh);

	$self->mux->set_callback_object($self, $rfh);
	##$mux->set_callback_object($self, $wfh);

	print "SHARE PAYLOAD: $self $wfh/$rfh (to $cmd) [fd=" .
		fileno($wfh) . "/" . fileno($rfh) . "]\n" if ($main::debug);
	print "payload '$cmd' on fd=" . fileno($wfh) . "/" .
		fileno($rfh) . "\n";
	
	$self;
}

sub restart {
	my ($self) = @_;

	$self->initialise($self->{'args'});
}

#
# PAYLOAD SOCKET: handles a network socket as payload.
#
package PayloadSocket;
use base 'Payload';

sub initialise {
	my ($self, $addr) = @_;
	my ($payload);

	print "PayloadSocket::initialise [$self] addr<$addr>\n"
		if ($main::debug);

	$self->SUPER::initialise();

	$self->{'args'} = $addr;

	# Create a listening socket and add it to the multiplexor.
	my $payload = new IO::Socket::INET(PeerAddr => $addr);
	if (!$payload) {
		$self->connected(0);
		if ($self->can("restart")) {
			my ($timeout) = $self->retry_timeout();

			$self->cout->junctionInput($self, 
				"<<<PAYLOAD ERROR ($!) ... retrying in $timeout secs>>>\n");
			# Schedule a timeout to trigger a reconnect.
			Callback->new($self->mux, $self, $timeout);
		} else {
			$self->cout->junctionEOF($self);
			$self->cin->junctionRemove($self);

			Payload->lost($self->name, $self);
		}

	} else {
		$self->{'rfh'} = $payload;
		$self->{'wfh'} = $payload;

		print "SHARE PAYLOAD: $self $payload (to $addr) [fd=" .
			fileno($payload) . "]\n" if ($main::debug);
		print "payload '$addr' on fd=" . fileno($payload) . "\n";
		$self->mux->add($payload);

		$self->mux->set_callback_object($self, $payload);
	}

	print "SHARE PAYLOAD: $self $payload ... done\n" if ($main::debug);

	$self;
}

sub restart {
	my ($self) = @_;

	$self->initialise($self->{'args'});
}

#
# CLIENT: general client object, represents a remote client channel
#
package Client;

sub new {
	my ($class, $mux, $fh) = @_;
	my $self = bless { 'mux' => $mux,
			   'fh'  => $fh }, $class;

	print "Client::new [$self] mux<$mux> fh<$fh>\n"
		if ($main::debug);

	$self->initialise();

	$self;
}

sub clone {
	my ($class, $from) = @_;

	my $self = bless { %{$from} }, $class;

	print "Client::clone [$self] from<$from>\n" if ($main::debug);

	$self->initialise();

	$self;
}

# Data accessors.
sub mux {
	my $self = shift;
	if (@_) { $self->{'mux'} = shift }
	return $self->{'mux'};
}
sub payload {
	my $self = shift;
	if (@_) { $self->{'payload'} = shift }
	return $self->{'payload'};
}
sub fh {
	my $self = shift;
	if (@_) { $self->{'fh'} = shift }
	return $self->{'fh'};
}
sub id {
	my $self = shift;
	if (@_) { $self->{'id'} = shift }
	return $self->{'id'};
}
sub announce {
	my $self = shift;
	if (@_) { $self->{'announce'} = shift }
	return $self->{'announce'};
}
sub cout {
	my $self = shift;
	if (@_) { $self->{'cout'} = shift }
	return $self->{'cout'};
}
sub cin {
	my $self = shift;
	if (@_) {
		$self->{'cin'}->junctionRemove($self) if ($self->{'cin'});
		$self->{'cin'} = shift;
		$self->{'cin'}->junctionAdd($self) if ($self->{'cin'} != undef);
	}
	return $self->{'cin'};
}

sub initialise {
	my ($self) = @_;

	print "Client::initialise [$self]\n" if ($main::debug);

	$self->mux->set_callback_object($self, $self->fh);
}

sub junctionInput {
	my ($self, $from, $data) = @_;

	print "Client::junctionInput [$self] data<$data>\n" if ($main::debug);

	$self->mux->write($self->fh, $data);
}
sub junctionEOF {
	my ($self, $from, $data) = @_;

	print "Client::junctionEOF [$self] data<$data>\n" if ($main::debug);

	$self->shutdown();
}

sub mux_eof {
	my ($self, $mux, $fh, $input) = @_;

	print "Client::mux_eof [$self] mux<$mux> fh<$fh> input<$input>\n"
		if ($main::debug);

	# Handle any pending input, then remove myself from the clients list.
	$self->mux_input($mux, $fh, $input);
	$self->cin(undef);
	$self->cout(undef);

	# Tell the multiplexor we no longer are using this channel.
	$mux->shutdown($fh, 1);
}
sub mux_close {
	my ($self, $mux, $fn) = @_;

	print "Client::close [$self]\n" if ($main::debug);

	if ($self->announce) {
		$self->announce->junctionInput($self, "(" . $self->id .
			") disconnected\n");
	}
	print "$self->{'id'} disconnected\n";
}

sub shutdown {
	my ($self) = @_;

	print "Client::shutdown [$self]\n" if ($main::debug);

	# Close myself down and tell the payload.
	$self->mux->shutdown($self->fh, 2);
}
sub DESTROY {
	my ($self) = @_;

	print "Client::DESTROY [$self]\n" if ($main::debug);
}

#
# CLIENT CMD: represents a client whilst in command mode, when we have commited
# to connecting this will pass the client connection off to a ClientData
# object.
#
package ClientCmd;
use base 'Client';

sub mux_input {
	my ($self, $mux, $fh, $input) = @_;

	print "Client::shutdown [$self] mux<$mux> fh<$fh> input<$$input>\n"
		if ($main::debug);

        while ($$input =~ s/^(.*?)\n//) {
		my ($cmd, $args) = split(' ', $1, 2);
		my (%args) = Conmux::decodeArgs($args);

		my $reply = {
			'status' => 'ENOSYS unknown command',
		};

		# XXX: check authentication if required and reject the
		# command out of hand - leak _nothing_.
		if (!defined $args{'id'}) {
			$reply->{'status'} = 'EACCES identifier required';
			goto reply;
		}
		# They are who they say they are, record who that is.
		$self->{'id'} = $args{'id'};
		
		if ($cmd eq "CONNECT") {
			# Switch over to data mode, hand this connection off
			# to a data client instance, I am done.
			my ($data, $to, $in, $out);
			$data = ClientData->clone($self);

			$to = $args{'to'};
			if (!$to) {
				$reply->{'status'} = "EINVAL CONNECT " .
					" requires 'to' specifier";
				goto reply;
			}
			my $payload = Payload->lookup($to);
			if (!defined $payload) {
				$reply->{'status'} = "EINVAL '$to' not a " .
					" valid destination specifier";
				goto reply;
			}

			$reply->{'status'} = 'OK';

			# Get the payload title and pass that back.
			$reply->{'title'} = $payload->title . ' [channel ' .
				$payload->state() . ']';
			$reply->{'state'} = $payload->state();
			# Get connected clients and pass back as the motd
			for my $cl (keys(%{$payload->cout})) {
				$reply->{'motd'} .= '(' . $payload->cout->{$cl}->id;
				$reply->{'motd'} .= ") is already connected\n";
			}

			$data->payload($payload);
			$args{'type'} = 'client' if (!$args{'type'});
			if ($args{'type'} eq 'status') {
				$data->cout($payload->cout);
			} elsif ($args{'type'} eq 'client') {
				if (!$args{'hide'}) {
					$data->announce($payload->cout);
					$payload->cout->junctionInput(
						$self,  "(" . $self->id .
						") connected\n");
				}
				$data->cin($payload->cout);
				$data->cout($payload->cin);
			} else {
				$reply->{'status'} = "EINVAL '$args{'type'}' " .
					"not a valid destination type";
				goto reply;
			}

			print "$self->{'id'} connected to $to/$args{'type'}\n";

			$self->junctionInput($self, 
				Conmux::encodeArgs($reply) .  "\n");

			# Don't handle any more input - its not going to be
			# for us.
			last;
		}

	reply:
		# We're done, send back our response to this.
		$self->junctionInput($self, Conmux::encodeArgs($reply) . "\n");
	}
}

#
# CLIENT DATA: handles a client connection when in data mode, attaches
# the client connection to the relevant junction.
#
package ClientData;
use base 'Client';

my @help = (
	[ 'msg', 'send a message to all connected clients' ],
	[ 'quit', 'disconnect from the console' ],
);
sub mux_input {
	my ($self, $mux, $fh, $input) = @_;

	print "ClientData::mux_input [$self] mux<$mux> fh<$fh> input<$$input>\n"
		if ($main::debug);

	while ($$input ne "") {
		if ($self->{'cmd'} eq '') {
			# Check for an incomplete escape ... wait for more.
			if ($$input =~ /^~$/s) {
				return;
			}
			if ($$input =~ s/^~\$//s) {
				$self->{'cmd'} = '>';
				my $title = $self->payload->title;
				$self->junctionInput($self, "\r\nCommand($title)> ");
				next;
			}
			# Its not an escape ... pass it on.
			# Ship anything before that cannot be the escape.
			if ($$input =~ s/^(.[^~]*)(~|$)/\2/s) {
				# Data coming in from the client, send it to
				# the payload.
				$self->cout->junctionInput($self, $1);
			}
		} else {
			# Consume characters upto a newline, echo them back
			# to the client as we go.
			while ($$input =~ s/^([^\r\n])//) {
				my $c = $1;
				if ($c eq "\b" || $c eq "\x7f") {
					if (length($self->{'cmd'}) > 1) {
						$c = "\b \b";
						substr($self->{'cmd'},
							-1, 1, '');
					} else {
						$c = '';
					}
				} else {
					$self->{'cmd'} .= $c;
				}
				$self->junctionInput($self, $c);
			}
			# If we arn't at a newline, then wait for more input.
			if ($$input !~ s/^[\r\n]+//) {
				return;
			}

			$self->junctionInput($self, "\n");

			my ($cmd, $a) = split(' ', substr($self->{'cmd'},
				1), 2);
			$self->{'cmd'} = '';

			if ($cmd eq '') {

			} elsif ($cmd eq 'help') {
				my @cmds = $self->payload->commandHelp();

				my $ent;
				my $help = "Conmux commands:\n";
				for $ent (@cmds, @help) {
					$help .= sprintf("  %-20s %s\n",
						$ent->[0], $ent->[1]);
				}
				$self->junctionInput($self, $help);

			} elsif ($cmd eq 'quit') {
				$self->shutdown();

			} elsif ($cmd eq 'msg') {
				$self->cin->junctionInput($self,
					"($self->{'id'}) $a\n");

			# Not a client command ... pass it to the payload.
			} else {
				$self->payload->commandExec($self, $cmd, $a);
			}
		}
	}
}

#
# LIBRARY: split a string honouring quoting.
#
package main;
sub parse($) {
    my ($str) = @_;

    my ($pos, @args, $argc, $quote, $real, $c, $inc);

    $inc = 0;
    @args = ();
    $argc = 0;
    $quote = 0;
    $real = 0;


    $pos = 0;
    while (substr($str, $pos, 1) eq " ") {
	$pos++;
    }
    for (; $pos < length($str); $pos++) {
        $c = substr($str, $pos, 1);
        if ($quote != 2 && $c eq '\\') {
            $real = 1;
            $pos++;
            $c = substr($str, $pos, 1);
        } else {
            $real = 0;
        }

        if ($quote != 2 && $c eq '"' && !$real) {
            $quote ^= 1;
        } elsif ($quote != 1 && $c eq "'" && !$real) {
            $quote ^= 2;
        } elsif ($c eq " " && $quote == 0 && !$real) {
	    while (substr($str, $pos, 1) eq " ") {
		$pos++;
	    }
	    $pos--;
	    $argc++;
        } else {
	    if ($inc) {
		$inc = 0;
		$argc++;
	    }
            $args[$argc] .= $c;
        }
    }

    @args;
}

#
# MAIN: makes the IO multiplexor, junction, listener and payload and stitches
# them all together.
#
package main;

# Usage checks.
if ($#ARGV != 0 && $#ARGV != 3) {
	print STDERR "Usage: $P <config file>\n";
	print STDERR "       $P <local port> <title> socket <host>:<port>\n";
	print STDERR "       $P <local port> <title> cmd <cmd>\n";
	exit 1
}
my @conf;
if ($#ARGV == 3) {
	my ($lport, $title, $what, $arg) = @ARGV;
	@conf = (
		"listener '$lport'",
		"'$what' console '$title' '$arg'"
	);
} else {
	my ($cf) = @ARGV;
	open(CONF, '<', $cf) || die "$P: $cf: open failed - $!\n";
	@conf = <CONF>;
	close(CONF);
}

# Make a new multiplexer.
my $mux  = new IO::Multiplex;

my ($line, $seg, $listener, $payload);
$line = '';
for $seg (@conf) {
	# Handle comments, blank lines and line continuation.
	chomp($seg); $seg =~ s/^\s+//;
	next if ($seg =~ /^#/);
	$line .= $seg;
	if ($line =~ m/\\$/) {
		chop($line);
		next;
	}
	next if (/^\s+$/);

	my ($cmd, @a) = parse($line);
	$line = '';

	if ($cmd eq "listener") {
		if ($#a != 0) {
			warn "$P: Usage: listener <port>\n" .
			     "$P:        $line\n";
			next;
		}

		my ($lport) = @a;
		my ($rhost, $rname);

		# port
		if ($lport =~ m@^\d+$@) {
			# Already in the right format.
			
		# registry/service
		} elsif ($lport =~ m@(.*)/(.*)@) {
			($rhost, $rname, $lport) = ($1, $2, 0);

		# service
		} else {
			($rhost, $rname, $lport) = ('-', $lport, 0);
		}

		# Create the client listener socket.
		$listener = ListenerSocket->new($mux, $lport);

		# Register us with the registry.
		if ($rhost) {
			Conmux::Registry::add($rhost, $rname, $listener->address);
		}

	} elsif ($cmd eq 'socket') {
		if ($#a != 2) {
			warn "$P: Usage: socket <name> <title> <host:port>\n" .
			     "$P:        $line\n";
			next;
		}
		my ($name, $title, $sock) = @a;

		# Create the payload.
		$payload = PayloadSocket->new($name, $title, $mux, $sock);

	} elsif ($cmd eq 'application') {
		if ($#a != 2) {
			warn "$P: Usage: application <name> <title> <host:port>\n" .
			     "$P:        $line\n";
			next;
		}
		my ($name, $title, $app) = @a;

		$payload = PayloadApplication->new($name, $title, $mux, $app);

	} elsif ($cmd eq 'command') {
		if ($#a != 2) {
			warn "$P: Usage: command <name> <msg> <cmd>\n" .
			     "$P:        $line\n";
			next;
		}
		my ($name, $msg, $cmd) = @a;

		$payload->commandAdd($name, $msg, $cmd);
	
	} elsif ($cmd eq 'help') {
		if ($#a != 1) {
			warn "$P: Usage: $cmd <name> <msg>\n" .
			     "$P:        $line\n";
			next;
		}
		my ($name, $msg) = @a;

		$payload->helpAdd($name, $msg);
	} else {
		warn "$P: $cmd: unknown configuration command\n";
	}
}

# Hand over to the multiplexor.
do {
	eval { $mux->loop; };
	warn "$@";
} while ($@ =~ /^Use of freed value in iteration/);
die "ERROR: $@\n";