Annotation of loncom/LondConnection.pm, revision 1.40

1.2       albertel    1: #   This module defines and implements a class that represents
                      2: #   a connection to a lond daemon.
                      3: #
1.40    ! albertel    4: # $Id: LondConnection.pm,v 1.39 2006/01/27 20:37:12 albertel Exp $
1.2       albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
1.1       foxr       27: #
1.14      foxr       28: 
1.1       foxr       29: package LondConnection;
                     30: 
1.10      foxr       31: use strict;
1.1       foxr       32: use IO::Socket;
                     33: use IO::Socket::INET;
                     34: use IO::Handle;
                     35: use IO::File;
                     36: use Fcntl;
                     37: use POSIX;
                     38: use Crypt::IDEA;
1.31      foxr       39: use LONCAPA::lonlocal;
                     40: use LONCAPA::lonssl;
1.14      foxr       41: 
1.1       foxr       42: 
1.12      foxr       43: 
                     44: 
1.32      foxr       45: my $DebugLevel=0;
1.12      foxr       46: my %hostshash;
                     47: my %perlvar;
1.31      foxr       48: my $LocalDns = "";		# Need not be defined for managers.
                     49: my $InsecureOk;
1.1       foxr       50: 
1.14      foxr       51: #
1.16      foxr       52: #  Set debugging level
                     53: #
                     54: sub SetDebug {
                     55:     $DebugLevel = shift;
                     56: }
                     57: 
                     58: #
1.14      foxr       59: #   The config read is done in this way to support the read of
                     60: #   the non-default configuration file in the
                     61: #   event we are being used outside of loncapa.
                     62: #
                     63: 
                     64: my $ConfigRead = 0;
                     65: 
1.1       foxr       66: #   Read the configuration file for apache to get the perl
1.31      foxr       67: #   variables set.
1.1       foxr       68: 
1.12      foxr       69: sub ReadConfig {
1.31      foxr       70:     Debug(8, "ReadConfig called");
                     71: 
1.14      foxr       72:     my $perlvarref = read_conf('loncapa.conf');
1.12      foxr       73:     %perlvar    = %{$perlvarref};
1.14      foxr       74:     my $hoststab   = read_hosts(
1.21      foxr       75: 				"$perlvar{lonTabDir}/hosts.tab") || 
1.14      foxr       76: 				die "Can't read host table!!";
1.12      foxr       77:     %hostshash  = %{$hoststab};
1.17      foxr       78:     $ConfigRead = 1;
1.12      foxr       79:     
1.31      foxr       80:     my $myLonCapaName = $perlvar{lonHostID};
                     81:     Debug(8, "My loncapa name is $myLonCapaName");
                     82:     
                     83:     if(defined $hostshash{$myLonCapaName}) {
                     84: 	Debug(8, "My loncapa name is in hosthash");
                     85: 	my @ConfigLine = @{$hostshash{$myLonCapaName}};
                     86: 	$LocalDns = $ConfigLine[3];
                     87: 	Debug(8, "Got local name $LocalDns");
                     88:     }
                     89:     $InsecureOk = $perlvar{loncAllowInsecure};
                     90:     
                     91:     Debug(3, "ReadConfig - LocalDNS = $LocalDns");
1.12      foxr       92: }
                     93: 
1.15      foxr       94: #
                     95: #  Read a foreign configuration.
                     96: #  This sub is intended for the cases where the package
                     97: #  will be read from outside the LonCAPA environment, in that case
                     98: #  the client will need to explicitly provide:
                     99: #   - A file in hosts.tab format.
                    100: #   - Some idea of the 'lonCAPA' name of the local host (for building
                    101: #     the encryption key).
                    102: #
                    103: #  Parameters:
                    104: #      MyHost   - Name of this host as far as LonCAPA is concerned.
                    105: #      Filename - Name of a hosts.tab formatted file that will be used
                    106: #                 to build up the hosts table.
                    107: #
                    108: sub ReadForeignConfig {
1.30      foxr      109: 
                    110:     my ($MyHost, $Filename) = @_;
1.15      foxr      111: 
1.17      foxr      112:     &Debug(4, "ReadForeignConfig $MyHost $Filename\n");
                    113: 
1.15      foxr      114:     $perlvar{lonHostID} = $MyHost; # Rmember my host.
                    115:     my $hosttab = read_hosts($Filename) ||
                    116: 	die "Can't read hosts table!!";
1.17      foxr      117:     %hostshash = %{$hosttab};
                    118:     if($DebugLevel > 3) {
                    119: 	foreach my $host (keys %hostshash) {
1.31      foxr      120: 	    print STDERR "host $host => $hostshash{$host}\n";
1.17      foxr      121: 	}
                    122:     }
                    123:     $ConfigRead = 1;
1.1       foxr      124: 
1.31      foxr      125:     my $myLonCapaName = $perlvar{lonHostID};
                    126:     
                    127:     if(defined $hostshash{$myLonCapaName}) {
                    128: 	my @ConfigLine = @{$hostshash{$myLonCapaName}};
                    129: 	$LocalDns = $ConfigLine[3];
                    130:     }
                    131:     $InsecureOk = $perlvar{loncAllowInsecure};
                    132:     
                    133:     Debug(3, "ReadForeignConfig  - LocalDNS = $LocalDns");
                    134: 
1.15      foxr      135: }
1.1       foxr      136: 
                    137: sub Debug {
1.30      foxr      138: 
                    139:     my ($level, $message) = @_;
                    140: 
1.1       foxr      141:     if ($level < $DebugLevel) {
1.31      foxr      142: 	print STDERR ($message."\n");
1.1       foxr      143:     }
                    144: }
1.3       albertel  145: 
                    146: =pod
                    147: 
                    148: =head2 Dump
                    149: 
1.12      foxr      150: Dump the internal state of the object: For debugging purposes, to stderr.
1.3       albertel  151: 
1.1       foxr      152: =cut
                    153: 
                    154: sub Dump {
                    155:     my $self   = shift;
1.32      foxr      156:     my $level  = shift;
1.35      foxr      157:     my $now    = time;
                    158:     my $local  = localtime($now);
1.32      foxr      159:     
1.37      albertel  160:     if ($level >= $DebugLevel) {
1.32      foxr      161: 	return;
                    162:     }
                    163: 
1.35      foxr      164:     
1.10      foxr      165:     my $key;
                    166:     my $value;
1.35      foxr      167:     print STDERR "[ $local ] Dumping LondConnectionObject:\n";
1.37      albertel  168:     print STDERR join(':',caller(1))."\n";
1.1       foxr      169:     while(($key, $value) = each %$self) {
1.22      foxr      170: 	print STDERR "$key -> $value\n";
1.1       foxr      171:     }
1.23      foxr      172:     print STDERR "-------------------------------\n";
1.1       foxr      173: }
                    174: 
                    175: =pod
1.3       albertel  176: 
                    177: Local function to do a state transition.  If the state transition
                    178: callback is defined it is called with two parameters: the self and the
                    179: old state.
                    180: 
1.1       foxr      181: =cut
1.3       albertel  182: 
1.1       foxr      183: sub Transition {
1.30      foxr      184: 
                    185:     my ($self, $newstate) = @_;
                    186: 
1.1       foxr      187:     my $oldstate = $self->{State};
                    188:     $self->{State} = $newstate;
                    189:     $self->{TimeoutRemaining} = $self->{TimeoutValue};
                    190:     if($self->{TransitionCallback}) {
                    191: 	($self->{TransitionCallback})->($self, $oldstate); 
                    192:     }
                    193: }
                    194: 
1.3       albertel  195: 
1.14      foxr      196: 
1.1       foxr      197: =pod
1.3       albertel  198: 
                    199: =head2 new
                    200: 
                    201: Construct a new lond connection.
                    202: 
                    203: Parameters (besides the class name) include:
                    204: 
                    205: =item hostname
                    206: 
                    207: host the remote lond is on. This host is a host in the hosts.tab file
                    208: 
                    209: =item port
                    210: 
                    211:  port number the remote lond is listening on.
                    212: 
1.1       foxr      213: =cut
1.3       albertel  214: 
1.1       foxr      215: sub new {
1.39      albertel  216:     my ($class, $DnsName, $Port) = @_;
1.14      foxr      217: 
                    218:     if (!$ConfigRead) {
                    219: 	ReadConfig();
                    220: 	$ConfigRead = 1;
                    221:     }
1.39      albertel  222:     &Debug(4,$class."::new( ".$DnsName.",".$Port.")\n");
1.1       foxr      223: 
                    224:     # The host must map to an entry in the hosts table:
                    225:     #  We connect to the dns host that corresponds to that
                    226:     #  system and use the hostname for the encryption key 
                    227:     #  negotion.  In the objec these become the Host and
                    228:     #  LoncapaHim fields of the object respectively.
                    229:     #
1.39      albertel  230:     if (!exists $hostshash{$DnsName}) {
                    231: 	&Debug(8, "No Such host $DnsName");
1.1       foxr      232: 	return undef;		# No such host!!!
                    233:     }
1.39      albertel  234:     my @ConfigLine = @{$hostshash{$DnsName}};
                    235:     my $Hostname    = $ConfigLine[0]; # 0'th item is the msu id of host.
1.1       foxr      236:     Debug(5, "Connecting to ".$DnsName);
1.36      albertel  237:     # if it is me use loopback for connection
                    238:     if ($DnsName eq $LocalDns) { $DnsName="127.0.0.1"; }
                    239:     Debug(8, "Connecting to $DnsName I am $LocalDns");
1.1       foxr      240:     # Now create the object...
                    241:     my $self     = { Host               => $DnsName,
1.24      foxr      242:                      LoncapaHim         => $Hostname,
                    243:                      Port               => $Port,
                    244:                      State              => "Initialized",
1.31      foxr      245: 		     AuthenticationMode => "",
1.24      foxr      246:                      TransactionRequest => "",
                    247:                      TransactionReply   => "",
1.39      albertel  248:                      NextRequest        => "",
1.24      foxr      249:                      InformReadable     => 0,
                    250:                      InformWritable     => 0,
                    251:                      TimeoutCallback    => undef,
                    252:                      TransitionCallback => undef,
                    253:                      Timeoutable        => 0,
                    254:                      TimeoutValue       => 30,
                    255:                      TimeoutRemaining   => 0,
1.31      foxr      256: 		     LocalKeyFile       => "",
1.24      foxr      257:                      CipherKey          => "",
                    258:                      LondVersion        => "Unknown",
                    259:                      Cipher             => undef};
1.1       foxr      260:     bless($self, $class);
                    261:     unless ($self->{Socket} = IO::Socket::INET->new(PeerHost => $self->{Host},
1.27      foxr      262: 					       PeerPort => $self->{Port},
                    263: 					       Type     => SOCK_STREAM,
                    264: 					       Proto    => "tcp",
                    265: 					       Timeout  => 3)) {
1.36      albertel  266: 	Debug(8, "Error? \n$@ \n$!");
1.1       foxr      267: 	return undef;		# Inidicates the socket could not be made.
                    268:     }
1.31      foxr      269:     my $socket = $self->{Socket}; # For local use only.
1.33      foxr      270:     #  If we are local, we'll first try local auth mode, otherwise, we'll try
                    271:     #  the ssl auth mode:
1.31      foxr      272: 
                    273:     my $key;
                    274:     my $keyfile;
1.36      albertel  275:     if ($DnsName eq '127.0.0.1') {
1.31      foxr      276: 	$self->{AuthenticationMode} = "local";
                    277: 	($key, $keyfile)         = lonlocal::CreateKeyFile();
                    278: 	Debug(8, "Local key: $key, stored in $keyfile");
                    279: 	   
                    280: 	#  If I can't make the key file fall back to insecure if 
                    281: 	#  allowed...else give up right away.
                    282: 
                    283: 	if(!(defined $key) || !(defined $keyfile)) {
                    284: 	    if($InsecureOk) {
                    285: 		$self->{AuthenticationMode} = "insecure";
                    286: 		$self->{TransactionRequest} = "init\n";
                    287: 	    } 
                    288: 	    else {
                    289: 		$socket->close;
                    290: 		return undef;
                    291: 	    }
                    292: 	}
                    293: 	$self->{TransactionRequest} = "init:local:$keyfile\n";
                    294: 	Debug(9, "Init string is init:local:$keyfile");
                    295: 	if(!$self->CreateCipher($key)) { # Nothing's going our way...
                    296: 	    $socket->close;
                    297: 	    return undef;
                    298: 	}
                    299: 
1.33      foxr      300:     }
1.31      foxr      301:     else {
1.33      foxr      302: 	#  Remote peer:  I'd like to do ssl, but if my host key or certificates
                    303: 	#  are not all installed, my only choice is insecure, if that's 
                    304: 	#  allowed:
                    305: 
                    306: 	my ($ca, $cert) = lonssl::CertificateFile;
                    307: 	my $sslkeyfile  = lonssl::KeyFile;
                    308: 
                    309: 	if((defined $ca)  && (defined $cert) && (defined $sslkeyfile)) {
                    310: 
                    311: 	    $self->{AuthenticationMode} = "ssl";
                    312: 	    $self->{TransactionRequest} = "init:ssl\n";
                    313: 	} else {
                    314: 	    if($InsecureOk) {		# Allowed to do insecure:
                    315: 		$self->{AuthenticationMode} = "insecure";
                    316: 		$self->{TransactionRequest} = "init\n";
                    317: 	    }
                    318: 	    else {		# Not allowed to do insecure...
                    319: 		$socket->close;
                    320: 		return undef;
                    321: 	    }
                    322: 	}
1.31      foxr      323:     }
                    324: 
1.1       foxr      325:     #
                    326:     # We're connected.  Set the state, and the events we'll accept:
                    327:     #
                    328:     $self->Transition("Connected");
                    329:     $self->{InformWritable}     = 1;    # When  socket is writable we send init
1.9       foxr      330:     $self->{Timeoutable}        = 1;    # Timeout allowed during startup negotiation. 
1.31      foxr      331: 
1.1       foxr      332:     
                    333:     #
                    334:     # Set socket to nonblocking I/O.
                    335:     #
                    336:     my $socket = $self->{Socket};
1.31      foxr      337:     my $flags    = fcntl($socket, F_GETFL,0);
                    338:     if(!$flags) {
1.1       foxr      339: 	$socket->close;
                    340: 	return undef;
                    341:     }
1.31      foxr      342:     if(!fcntl($socket, F_SETFL, $flags | O_NONBLOCK)) {
1.1       foxr      343: 	$socket->close;
                    344: 	return undef;
                    345:     }
                    346: 
                    347:     # return the object :
                    348: 
1.31      foxr      349:     Debug(9, "Initial object state: ");
1.32      foxr      350:     $self->Dump(9);
1.31      foxr      351: 
1.1       foxr      352:     return $self;
                    353: }
1.3       albertel  354: 
1.1       foxr      355: =pod
1.3       albertel  356: 
                    357: =head2 Readable
                    358: 
                    359: This member should be called when the Socket becomes readable.  Until
                    360: the read completes, action is state independet. Data are accepted into
                    361: the TransactionReply until a newline character is received.  At that
                    362: time actionis state dependent:
                    363: 
                    364: =item Connected
                    365: 
                    366: in this case we received challenge, the state changes to
                    367: ChallengeReceived, and we initiate a send with the challenge response.
                    368: 
                    369: =item ReceivingReply
                    370: 
                    371: In this case a reply has been received for a transaction, the state
                    372: goes to Idle and we disable write and read notification.
                    373: 
                    374: =item ChallengeReeived
                    375: 
                    376: we just got what should be an ok\n and the connection can now handle
                    377: transactions.
1.1       foxr      378: 
                    379: =cut
1.3       albertel  380: 
1.1       foxr      381: sub Readable {
                    382:     my $self    = shift;
                    383:     my $socket  = $self->{Socket};
                    384:     my $data    = '';
1.27      foxr      385:     my $rv;
1.31      foxr      386:     my $ConnectionMode = $self->{AuthenticationMode};
                    387: 
1.27      foxr      388:     if ($socket) {
                    389: 	eval {
                    390: 	    $rv = $socket->recv($data, POSIX::BUFSIZ, 0);
                    391: 	}
                    392:     } else {
                    393: 	$self->Transition("Disconnected");
                    394: 	return -1;
                    395:     }
1.1       foxr      396:     my $errno   = $! + 0;	             # Force numeric context.
                    397: 
1.8       foxr      398:     unless (defined($rv) && length $data) {# Read failed,
1.1       foxr      399: 	if(($errno == POSIX::EWOULDBLOCK)   ||
                    400: 	   ($errno == POSIX::EAGAIN)        ||
1.8       foxr      401: 	   ($errno == POSIX::EINTR)) {
1.1       foxr      402: 	    return 0;
                    403: 	}
                    404: 
                    405: 	# Connection likely lost.
                    406: 	&Debug(4, "Connection lost");
                    407: 	$self->{TransactionRequest} = '';
                    408: 	$socket->close();
                    409: 	$self->Transition("Disconnected");
                    410: 	return -1;
                    411:     }
                    412:     #  Append the data to the buffer.  And figure out if the read is done:
                    413: 
                    414:     &Debug(9,"Received from host: ".$data);
                    415:     $self->{TransactionReply} .= $data;
1.29      albertel  416:     if($self->{TransactionReply} =~ m/\n$/) {
1.1       foxr      417: 	&Debug(8,"Readable End of line detected");
1.31      foxr      418: 	
                    419: 
1.1       foxr      420: 	if ($self->{State}  eq "Initialized") { # We received the challenge:
1.31      foxr      421: 	    #   Our init was replied to. What happens next depends both on
                    422: 	    #  the actual init we sent (AuthenticationMode member data)
                    423: 	    #  and the response:
                    424: 	    #     AuthenticationMode == local:
                    425: 	    #       Response ok:   The key has been exchanged and
                    426: 	    #                      the key file destroyed. We can jump
                    427: 	    #                      into setting the host and requesting the
                    428: 	    #                      Later we'll also bypass key exchange.
                    429: 	    #       Response digits: 
                    430: 	    #                      Old style lond. Delete the keyfile.
                    431: 	    #                      If allowed fall back to insecure mode.
                    432: 	    #                      else close connection and fail.
                    433: 	    #       Response other:
                    434: 	    #                      Failed local auth 
                    435: 	    #                      Close connection and fail.
                    436: 	    #
                    437: 	    #    AuthenticationMode == ssl:
                    438: 	    #        Response ok:ssl
                    439: 	    #        Response digits:
                    440: 	    #        Response other:
                    441: 	    #    Authentication mode == insecure
                    442: 	    #        Response digits
                    443: 	    #        Response other:
                    444: 	    
                    445: 	    my $Response = $self->{TransactionReply};
                    446: 	    if($ConnectionMode eq "local") {
                    447: 		if($Response =~ /^ok:local/) { #  Good local auth.
                    448: 		    $self->ToVersionRequest();
                    449: 		    return 0;
                    450: 		}
                    451: 		elsif ($Response =~/^[0-9]+/) {	# Old style lond.
                    452: 		    return $self->CompleteInsecure();
                    453: 
                    454: 		}
                    455: 		else {		                # Complete flop
                    456: 		    &Debug(3, "init:local : unrecognized reply");
                    457: 		    $self->Transition("Disconnected");
                    458: 		    $socket->close;
                    459: 		    return -1;
                    460: 		}
                    461: 	    }
                    462: 	    elsif ($ConnectionMode eq "ssl") {
                    463: 		if($Response =~ /^ok:ssl/) {     # Good ssl...
                    464: 		    if($self->ExchangeKeysViaSSL()) { # Success skip to vsn stuff
                    465: 			# Need to reset to non blocking:
                    466: 
                    467: 			my $flags = fcntl($socket, F_GETFL, 0);
                    468: 			fcntl($socket, F_SETFL, $flags | O_NONBLOCK);
                    469: 			$self->ToVersionRequest();
                    470: 			return 0;
                    471: 		    }
                    472: 		    else {	         # Failed in ssl exchange.
                    473: 			&Debug(3,"init:ssl failed key negotiation!");
                    474: 			$self->Transition("Disconnected");
                    475: 			$socket->close;
                    476: 			return -1;
                    477: 		    }
                    478: 		} 
                    479: 		elsif ($Response =~ /^[0-9]+/) { # Old style lond.
                    480: 		    return $self->CompleteInsecure();
                    481: 		}
                    482: 		else {		                 # Complete flop
                    483: 		}
                    484: 	    }
                    485: 	    elsif ($ConnectionMode eq "insecure") {
                    486: 		if($self->{TransactionReply} eq "refused\n") {	# Remote doesn't have
                    487: 		    
                    488: 		    $self->Transition("Disconnected"); # in host tables.
                    489: 		    $socket->close();
                    490: 		    return -1;
                    491: 
                    492: 		}
                    493: 		return $self->CompleteInsecure();
                    494: 	    }
                    495: 	    else {
                    496: 		&Debug(1,"Authentication mode incorrect");
                    497: 		die "BUG!!! LondConnection::Readable invalid authmode";
1.1       foxr      498: 	    }
1.27      foxr      499: 
1.31      foxr      500: 
1.28      albertel  501: 	}  elsif ($self->{State} eq "ChallengeReplied") {
                    502: 	    if($self->{TransactionReply} ne "ok\n") {
                    503: 		$self->Transition("Disconnected");
                    504: 		$socket->close();
                    505: 		return -1;
                    506: 	    }
1.31      foxr      507: 	    $self->ToVersionRequest();
1.28      albertel  508: 	    return 0;
1.31      foxr      509: 
1.28      albertel  510: 	} elsif ($self->{State} eq "ReadingVersionString") {
1.38      albertel  511: 	    chomp($self->{TransactionReply});
                    512: 	    $self->{LondVersion}       = $self->{TransactionReply};
1.28      albertel  513: 	    $self->Transition("SetHost");
                    514: 	    $self->{InformReadable}    = 0;
                    515: 	    $self->{InformWritable}    = 1;
                    516: 	    my $peer = $self->{LoncapaHim};
                    517: 	    $self->{TransactionRequest}= "sethost:$peer\n";
                    518: 	    return 0;
1.24      foxr      519: 	} elsif ($self->{State} eq "HostSet") { # should be ok.
1.28      albertel  520: 	    if($self->{TransactionReply} ne "ok\n") {
                    521: 		$self->Transition("Disconnected");
                    522: 		$socket->close();
                    523: 		return -1;
                    524: 	    }
1.31      foxr      525: 	    #  If the auth mode is insecure we must still
                    526: 	    #  exchange session keys. Otherwise,
                    527: 	    #  we can just transition to idle.
                    528: 
                    529: 	    if($ConnectionMode eq "insecure") {
                    530: 		$self->Transition("RequestingKey");
                    531: 		$self->{InformReadable}  = 0;
                    532: 		$self->{InformWritable}  = 1;
                    533: 		$self->{TransactionRequest} = "ekey\n";
                    534: 		return 0;
                    535: 	    }
                    536: 	    else {
                    537: 		$self->ToIdle();
                    538: 		return 0;
                    539: 	    }
1.1       foxr      540: 	} elsif ($self->{State}  eq "ReceivingKey") {
                    541: 	    my $buildkey = $self->{TransactionReply};
                    542: 	    my $key = $self->{LoncapaHim}.$perlvar{'lonHostID'};
                    543: 	    $key=~tr/a-z/A-Z/;
                    544: 	    $key=~tr/G-P/0-9/;
                    545: 	    $key=~tr/Q-Z/0-9/;
1.31      foxr      546: 	    $key =$key.$buildkey.$key.$buildkey.$key.$buildkey;
                    547: 	    $key               = substr($key,0,32);
                    548: 	    if(!$self->CreateCipher($key)) {
1.1       foxr      549: 		$self->Transition("Disconnected");
                    550: 		$socket->close();
                    551: 		return -1;
                    552: 	    } else {
1.31      foxr      553: 		$self->ToIdle();
1.1       foxr      554: 		return 0;
                    555: 	    }
                    556: 	} elsif ($self->{State}  eq "ReceivingReply") {
                    557: 
                    558: 	    # If the data are encrypted, decrypt first.
                    559: 
                    560: 	    my $answer = $self->{TransactionReply};
                    561: 	    if($answer =~ /^enc\:/) {
                    562: 		$answer = $self->Decrypt($answer);
1.34      foxr      563: 		$self->{TransactionReply} = "$answer\n";
1.1       foxr      564: 	    }
1.39      albertel  565: 	    # if we have a NextRequest do it immeadiately
                    566: 	    if ($self->{NextRequest}) {
                    567: 		$self->{TransactionRequest} = $self->{NextRequest};
                    568: 		undef( $self->{NextRequest} );
                    569: 		$self->{TransactionReply}   = "";
                    570: 		$self->{InformWritable}     = 1;
                    571: 		$self->{InformReadable}     = 0;
                    572: 		$self->{Timeoutable}        = 1;
                    573: 		$self->{TimeoutRemaining}   = $self->{TimeoutValue};
                    574: 		$self->Transition("SendingRequest");
                    575: 		return 0;
                    576: 	    } else {
1.1       foxr      577: 	    # finish the transaction
                    578: 
1.39      albertel  579: 		$self->ToIdle();
                    580: 		return 0;
                    581: 	    }
1.1       foxr      582: 	} elsif ($self->{State} eq "Disconnected") { # No connection.
                    583: 	    return -1;
                    584: 	} else {			# Internal error: Invalid state.
                    585: 	    $self->Transition("Disconnected");
                    586: 	    $socket->close();
                    587: 	    return -1;
                    588: 	}
                    589:     }
                    590: 
                    591:     return 0;
1.27      foxr      592:     
1.1       foxr      593: }
                    594: 
                    595: 
                    596: =pod
1.3       albertel  597: 
                    598: This member should be called when the Socket becomes writable.
                    599: 
                    600: The action is state independent. An attempt is made to drain the
                    601: contents of the TransactionRequest member.  Once this is drained, we
                    602: mark the object as waiting for readability.
1.1       foxr      603: 
                    604: Returns  0 if successful, or -1 if not.
1.3       albertel  605: 
1.1       foxr      606: =cut
                    607: sub Writable {
                    608:     my $self     = shift;		# Get reference to the object.
                    609:     my $socket   = $self->{Socket};
1.26      albertel  610:     my $nwritten;
                    611:     if ($socket) {
                    612: 	eval {
                    613: 	    $nwritten = $socket->send($self->{TransactionRequest}, 0);
                    614: 	}
1.27      foxr      615:     } else {
                    616: 	# For whatever reason, there's no longer a socket left.
                    617: 
                    618: 
                    619: 	$self->Transition("Disconnected");
                    620: 	return -1;
1.26      albertel  621:     }
1.1       foxr      622:     my $errno    = $! + 0;
                    623:     unless (defined $nwritten) {
                    624: 	if($errno != POSIX::EINTR) {
                    625: 	    $self->Transition("Disconnected");
                    626: 	    return -1;
                    627: 	}
                    628:       
                    629:     }
1.10      foxr      630:     if (($nwritten >= 0)                        ||
1.1       foxr      631:         ($errno == POSIX::EWOULDBLOCK)    ||
                    632: 	($errno == POSIX::EAGAIN)         ||
                    633: 	($errno == POSIX::EINTR)          ||
                    634: 	($errno ==  0)) {
                    635: 	substr($self->{TransactionRequest}, 0, $nwritten) = ""; # rmv written part
1.27      foxr      636:       if(length $self->{TransactionRequest} == 0) {
                    637:          $self->{InformWritable} = 0;
                    638:          $self->{InformReadable} = 1;
                    639:          $self->{TransactionReply} = '';
                    640:          #
                    641:          # Figure out the next state:
                    642:          #
                    643:          if($self->{State} eq "Connected") {
                    644:             $self->Transition("Initialized");
                    645:          } elsif($self->{State} eq "ChallengeReceived") {
                    646:             $self->Transition("ChallengeReplied");
                    647:          } elsif($self->{State} eq "RequestingVersion") {
                    648:             $self->Transition("ReadingVersionString");
                    649:          } elsif ($self->{State} eq "SetHost") {
                    650:             $self->Transition("HostSet");
                    651:          } elsif($self->{State} eq "RequestingKey") {
                    652:             $self->Transition("ReceivingKey");
1.24      foxr      653: #            $self->{InformWritable} = 0;
                    654: #            $self->{InformReadable} = 1;
                    655: #            $self->{TransactionReply} = '';
1.27      foxr      656:          } elsif ($self->{State} eq "SendingRequest") {
                    657:             $self->Transition("ReceivingReply");
                    658:             $self->{TimeoutRemaining} = $self->{TimeoutValue};
                    659:          } elsif ($self->{State} eq "Disconnected") {
                    660:             return -1;
                    661:          }
                    662:          return 0;
                    663:       }
                    664:    } else {			# The write failed (e.g. partner disconnected).
                    665:       $self->Transition("Disconnected");
                    666:       $socket->close();
                    667:       return -1;
                    668:    }
                    669: 	
1.1       foxr      670: }
                    671: =pod
1.3       albertel  672: 
                    673: =head2 Tick
                    674: 
1.1       foxr      675:    Tick is called every time unit by the event framework.  It
1.3       albertel  676: 
                    677: =item 1 decrements the remaining timeout.
                    678: 
                    679: =item 2 If the timeout is zero, calls TimedOut indicating that the current operation timed out.
1.1       foxr      680: 
                    681: =cut
                    682:     
                    683: sub Tick {
                    684:     my $self = shift;
                    685:     $self->{TimeoutRemaining}--;
                    686:     if ($self->{TimeoutRemaining} < 0) {
                    687: 	$self->TimedOut();
                    688:     }
                    689: }
1.3       albertel  690: 
1.1       foxr      691: =pod
                    692: 
1.3       albertel  693: =head2 TimedOut
                    694: 
                    695: called on a timeout.  If the timeout callback is defined, it is called
                    696: with $self as its parameters.
                    697: 
                    698: =cut
                    699: 
1.1       foxr      700: sub TimedOut  {
                    701: 
                    702:     my $self = shift;
                    703:     if($self->{TimeoutCallback}) {
                    704: 	my $callback = $self->{TimeoutCallback};
                    705: 	my @args = ( $self);
                    706: 	&$callback(@args);
                    707:     }
                    708: }
1.3       albertel  709: 
1.1       foxr      710: =pod
1.3       albertel  711: 
                    712: =head2 InitiateTransaction
                    713: 
                    714: Called to initiate a transaction.  A transaction can only be initiated
                    715: when the object is idle... otherwise an error is returned.  A
                    716: transaction consists of a request to the server that will have a
                    717: reply.  This member sets the request data in the TransactionRequest
                    718: member, makes the state SendingRequest and sets the data to allow a
                    719: timout, and to request writability notification.
                    720: 
1.1       foxr      721: =cut
1.3       albertel  722: 
1.1       foxr      723: sub InitiateTransaction {
1.30      foxr      724: 
                    725:     my ($self, $data) = @_;
1.1       foxr      726: 
1.4       foxr      727:     Debug(1, "initiating transaction: ".$data);
1.1       foxr      728:     if($self->{State} ne "Idle") {
1.4       foxr      729: 	Debug(0," .. but not idle here\n");
1.1       foxr      730: 	return -1;		# Error indicator.
                    731:     }
                    732:     # if the transaction is to be encrypted encrypt the data:
1.39      albertel  733:     (my $sethost, my $server,$data)=split(/:/,$data,3);
1.1       foxr      734: 
                    735:     if($data =~ /^encrypt\:/) {
                    736: 	$data = $self->Encrypt($data);
                    737:     }
                    738: 
                    739:     # Setup the trasaction
1.39      albertel  740:     # currently no version of lond supports inlining the sethost
1.40    ! albertel  741:     if ($self->PeerVersion() <= 321) {
1.39      albertel  742: 	if ($server ne $self->{LoncapaHim}) {
                    743: 	    $self->{NextRequest}        = $data;
                    744: 	    $self->{TransactionRequest} = "$sethost:$server\n";
                    745: 	    $self->{LoncapaHim}         = $server;
                    746: 	} else {
                    747: 	    $self->{TransactionRequest}        = $data;
                    748: 	}
                    749:     } else {
1.40    ! albertel  750: 	$self->{LoncapaHim}         = $server;
1.39      albertel  751: 	$self->{TransactionRequest} = "$sethost:$server:$data";
                    752:     }
1.1       foxr      753:     $self->{TransactionReply}   = "";
                    754:     $self->{InformWritable}     = 1;
                    755:     $self->{InformReadable}     = 0;
                    756:     $self->{Timeoutable}        = 1;
                    757:     $self->{TimeoutRemaining}   = $self->{TimeoutValue};
                    758:     $self->Transition("SendingRequest");
                    759: }
                    760: 
                    761: 
                    762: =pod
1.3       albertel  763: 
                    764: =head2 SetStateTransitionCallback
                    765: 
                    766: Sets a callback for state transitions.  Returns a reference to any
                    767: prior established callback, or undef if there was none:
                    768: 
1.1       foxr      769: =cut
1.3       albertel  770: 
1.1       foxr      771: sub SetStateTransitionCallback {
                    772:     my $self        = shift;
                    773:     my $oldCallback = $self->{TransitionCallback};
                    774:     $self->{TransitionCallback} = shift;
                    775:     return $oldCallback;
                    776: }
1.3       albertel  777: 
1.1       foxr      778: =pod
1.3       albertel  779: 
                    780: =head2 SetTimeoutCallback
                    781: 
                    782: Sets the timeout callback.  Returns a reference to any prior
                    783: established callback or undef if there was none.
                    784: 
1.1       foxr      785: =cut
1.3       albertel  786: 
1.1       foxr      787: sub SetTimeoutCallback {
1.30      foxr      788: 
                    789:     my ($self, $callback) = @_;
                    790: 
1.1       foxr      791:     my $oldCallback          = $self->{TimeoutCallback};
                    792:     $self->{TimeoutCallback} = $callback;
                    793:     return $oldCallback;
                    794: }
                    795: 
                    796: =pod
1.3       albertel  797: 
1.5       foxr      798: =head2 Shutdown:
                    799: 
                    800: Shuts down the socket.
                    801: 
                    802: =cut
                    803: 
                    804: sub Shutdown {
                    805:     my $self = shift;
                    806:     my $socket = $self->GetSocket();
1.20      albertel  807:     Debug(5,"socket is -$socket-");
                    808:     if ($socket) {
                    809: 	# Ask lond to exit too.  Non blocking so
                    810: 	# there is no cost for failure.
                    811: 	eval {
                    812: 	    $socket->send("exit\n", 0);
                    813: 	    $socket->shutdown(2);
                    814: 	}
                    815:     }
1.5       foxr      816: }
                    817: 
                    818: =pod
                    819: 
1.3       albertel  820: =head2 GetState
                    821: 
                    822: selector for the object state.
                    823: 
1.1       foxr      824: =cut
1.3       albertel  825: 
1.1       foxr      826: sub GetState {
                    827:     my $self = shift;
                    828:     return $self->{State};
                    829: }
1.3       albertel  830: 
1.1       foxr      831: =pod
1.3       albertel  832: 
                    833: =head2 GetSocket
                    834: 
                    835: selector for the object socket.
                    836: 
1.1       foxr      837: =cut
1.3       albertel  838: 
1.1       foxr      839: sub GetSocket {
                    840:     my $self  = shift;
                    841:     return $self->{Socket};
                    842: }
1.3       albertel  843: 
1.5       foxr      844: 
1.1       foxr      845: =pod
1.3       albertel  846: 
                    847: =head2 WantReadable
                    848: 
                    849: Return the state of the flag that indicates the object wants to be
                    850: called when readable.
                    851: 
1.1       foxr      852: =cut
1.3       albertel  853: 
1.1       foxr      854: sub WantReadable {
                    855:     my   $self = shift;
                    856: 
                    857:     return $self->{InformReadable};
                    858: }
1.3       albertel  859: 
1.1       foxr      860: =pod
1.3       albertel  861: 
                    862: =head2 WantWritable
                    863: 
                    864: Return the state of the flag that indicates the object wants write
                    865: notification.
                    866: 
1.1       foxr      867: =cut
1.3       albertel  868: 
1.1       foxr      869: sub WantWritable {
                    870:     my $self = shift;
                    871:     return $self->{InformWritable};
                    872: }
1.3       albertel  873: 
1.1       foxr      874: =pod
1.3       albertel  875: 
                    876: =head2 WantTimeout
                    877: 
                    878: return the state of the flag that indicates the object wants to be
                    879: informed of timeouts.
                    880: 
1.1       foxr      881: =cut
1.3       albertel  882: 
1.1       foxr      883: sub WantTimeout {
                    884:     my $self = shift;
                    885:     return $self->{Timeoutable};
                    886: }
                    887: 
                    888: =pod
1.3       albertel  889: 
                    890: =head2 GetReply
                    891: 
                    892: Returns the reply from the last transaction.
                    893: 
1.1       foxr      894: =cut
1.3       albertel  895: 
1.1       foxr      896: sub GetReply {
                    897:     my $self = shift;
                    898:     return $self->{TransactionReply};
                    899: }
                    900: 
                    901: =pod
1.3       albertel  902: 
                    903: =head2 Encrypt
                    904: 
                    905: Returns the encrypted version of the command string.
                    906: 
                    907: The command input string is of the form:
                    908: 
1.1       foxr      909:   encrypt:command
1.3       albertel  910: 
                    911: The output string can be directly sent to lond as it is of the form:
                    912: 
1.1       foxr      913:   enc:length:<encodedrequest>
1.3       albertel  914: 
1.1       foxr      915: =cut
1.3       albertel  916: 
1.1       foxr      917: sub Encrypt {
1.30      foxr      918:     
                    919:     my ($self, $request) = @_;
1.1       foxr      920: 
                    921:    
                    922:     # Split the encrypt: off the request and figure out it's length.
                    923:     # the cipher works in blocks of 8 bytes.
                    924: 
                    925:     my $cmd = $request;
                    926:     $cmd    =~ s/^encrypt\://;	# strip off encrypt:
                    927:     chomp($cmd);		# strip off trailing \n
                    928:     my     $length=length($cmd);	# Get the string length.
                    929:     $cmd .= "         ";	# Pad with blanks so we can fill out a block.
                    930: 
                    931:     # encrypt the request in 8 byte chunks to create the encrypted
                    932:     # output request.
                    933: 
                    934:     my $Encoded = '';
                    935:     for(my $index = 0; $index <= $length; $index += 8) {
                    936: 	$Encoded .= 
                    937: 	    unpack("H16", 
                    938: 		   $self->{Cipher}->encrypt(substr($cmd, 
                    939: 						   $index, 8)));
                    940:     }
                    941: 
                    942:     # Build up the answer as enc:length:$encrequest.
                    943: 
                    944:     $request = "enc:$length:$Encoded\n";
                    945:     return $request;
                    946:     
                    947:     
                    948: }
1.3       albertel  949: 
                    950: =pod
                    951: 
                    952: =head2 Decrypt
                    953: 
                    954: Decrypt a response from the server.  The response is in the form:
                    955: 
                    956:  enc:<length>:<encrypted data>
                    957: 
1.1       foxr      958: =cut
1.3       albertel  959: 
1.1       foxr      960: sub Decrypt {
1.30      foxr      961: 
                    962:     my ($self, $encrypted) = @_;
1.1       foxr      963: 
                    964:     #  Bust up the response into length, and encryptedstring:
                    965: 
                    966:     my ($enc, $length, $EncryptedString) = split(/:/,$encrypted);
                    967:     chomp($EncryptedString);
                    968: 
                    969:     # Decode the data in 8 byte blocks.  The string is encoded
                    970:     # as hex digits so there are two characters per byte:
                    971: 
1.10      foxr      972:     my $decrypted = "";
1.1       foxr      973:     for(my $index = 0; $index < length($EncryptedString);
                    974: 	$index += 16) {
                    975: 	$decrypted .= $self->{Cipher}->decrypt(
                    976: 				    pack("H16",
                    977: 					 substr($EncryptedString,
                    978: 						$index, 
                    979: 						16)));
                    980:     }
                    981:     #  the answer may have trailing pads to fill out a block.
                    982:     #  $length tells us the actual length of the decrypted string:
                    983: 
                    984:     $decrypted = substr($decrypted, 0, $length);
1.34      foxr      985:     Debug(9, "Decrypted $EncryptedString to $decrypted");
1.1       foxr      986: 
                    987:     return $decrypted;
                    988: 
                    989: }
1.31      foxr      990: # ToIdle
                    991: #     Called to transition to idle... done enough it's worth subbing
                    992: #     off to ensure it's always done right!!
                    993: #
                    994: sub ToIdle {
                    995:     my $self   = shift;
                    996: 
                    997:     $self->Transition("Idle");
                    998:     $self->{InformWritiable} = 0;
                    999:     $self->{InformReadable}  = 0;
                   1000:     $self->{Timeoutable}     = 0;
                   1001: }
                   1002: 
                   1003: #  ToVersionRequest
                   1004: #    Called to transition to "RequestVersion"  also done a few times
                   1005: #    so worth subbing out.
                   1006: #
                   1007: sub ToVersionRequest {
                   1008:     my $self   = shift;
                   1009:     
                   1010:     $self->Transition("RequestingVersion");
                   1011:     $self->{InformReadable}   = 0;
                   1012:     $self->{InformWritable}   = 1;
                   1013:     $self->{TransactionRequest} = "version\n";
                   1014:     
                   1015: }
                   1016: #
                   1017: #  CreateCipher
                   1018: #    Given a cipher key stores the key in the object context,
                   1019: #    creates the cipher object, (stores that in object context),
                   1020: #    This is done a couple of places, so it's worth factoring it out.
                   1021: #
                   1022: # Parameters:
                   1023: #    (self)
                   1024: #    key - The Cipher key.
                   1025: #
                   1026: # Returns:
                   1027: #    0   - Failure to create IDEA cipher.
                   1028: #    1   - Success.
                   1029: #
                   1030: sub CreateCipher {
                   1031:     my ($self, $key)   = @_;	# According to coding std.
                   1032: 
                   1033:     $self->{CipherKey} = $key; # Save the text key...
                   1034:     my $packedkey = pack ("H32", $key);
                   1035:     my $cipher            = new IDEA $packedkey;
                   1036:     if($cipher) {
                   1037: 	$self->{Cipher} = $cipher;
                   1038: 	Debug("Cipher created  dumping socket: ");
1.32      foxr     1039: 	$self->Dump(9);
1.31      foxr     1040: 	return 1;
                   1041:     }
                   1042:     else {
                   1043: 	return 0;
                   1044:     }
                   1045: }
                   1046: # ExchangeKeysViaSSL
                   1047: #     Called to do cipher key exchange via SSL.
                   1048: #     The socket is promoted to an SSL socket. If that's successful,
                   1049: #     we read out cipher key through the socket and create an IDEA
                   1050: #     cipher object.
                   1051: # Parameters:
                   1052: #    (self)
                   1053: # Returns:
                   1054: #      true    - Success.
                   1055: #      false   - Failure.
                   1056: #
                   1057: # Assumptions:
                   1058: #  1.   The ssl session setup has timeout logic built in so we don't
                   1059: #     have to worry about DOS attacks at that stage.
                   1060: #  2.   If the ssl session gets set up we are talking to a legitimate
                   1061: #     lond so again we don't have to worry about DOS attacks.
                   1062: #  All this allows us just to call 
                   1063: sub ExchangeKeysViaSSL {
                   1064:     my $self   = shift;
                   1065:     my $socket = $self->{Socket};
                   1066: 
                   1067:     #  Get our signed certificate, the certificate authority's 
                   1068:     #  certificate and our private key file.  All of these
                   1069:     #  are needed to create the ssl connection.
                   1070: 
                   1071:     my ($SSLCACertificate,
                   1072: 	$SSLCertificate) = lonssl::CertificateFile();
                   1073:     my $SSLKey             = lonssl::KeyFile();
                   1074: 
                   1075:     #  Promote our connection to ssl and read the key from lond.
                   1076: 
                   1077:     my $SSLSocket = lonssl::PromoteClientSocket($socket,
                   1078: 						$SSLCACertificate,
                   1079: 						$SSLCertificate,
                   1080: 						$SSLKey);
                   1081:     if(defined $SSLSocket) {
                   1082: 	my $key  = <$SSLSocket>;
                   1083: 	lonssl::Close($SSLSocket);
                   1084: 	if($key) {
                   1085: 	    chomp($key);	# \n is not part of the key.
                   1086: 	    return $self->CreateCipher($key);
                   1087: 	} 
                   1088: 	else {
                   1089: 	    Debug(3, "Failed to read ssl key");
                   1090: 	    return 0;
                   1091: 	}
                   1092:     }
                   1093:     else {
                   1094: 	# Failed!!
                   1095: 	Debug(3, "Failed to negotiate SSL connection!");
                   1096: 	return 0;
                   1097:     }
                   1098:     # should not get here
                   1099:     return 0;
                   1100: 
                   1101: }
                   1102: 
                   1103: 
                   1104: 
                   1105: #
                   1106: #  CompleteInsecure:
                   1107: #      This function is called to initiate the completion of
                   1108: #      insecure challenge response negotiation.
                   1109: #      To do this, we copy the challenge string to the transaction
                   1110: #      request, flip to writability and state transition to 
                   1111: #      ChallengeReceived..
                   1112: #      All this is only possible if InsecureOk is true.
                   1113: # Parameters:
                   1114: #      (self)    - This object's context hash.
                   1115: #  Return:
                   1116: #      0   - Ok to transition.
                   1117: #     -1   - Not ok to transition (InsecureOk not ok).
                   1118: #
                   1119: sub CompleteInsecure {
                   1120:     my $self = shift;
                   1121:     if($InsecureOk) {
                   1122: 	$self->{AuthenticationMode} = "insecure";
                   1123: 	&Debug(8," Transition out of Initialized:insecure");
                   1124: 	$self->{TransactionRequest} = $self->{TransactionReply};
                   1125: 	$self->{InformWritable}     = 1;
                   1126: 	$self->{InformReadable}     = 0;
                   1127: 	$self->Transition("ChallengeReceived");
                   1128: 	$self->{TimeoutRemaining}   = $self->{TimeoutValue};
                   1129: 	return 0;
                   1130: 	
                   1131: 	
                   1132:     }
                   1133:     else {
                   1134: 	&Debug(3, "Insecure key negotiation disabled!");
                   1135: 	my $socket = $self->{Socket};
                   1136: 	$socket->close;
                   1137: 	return -1;
                   1138:     }
                   1139: }
1.1       foxr     1140: 
                   1141: =pod
1.3       albertel 1142: 
                   1143: =head2 GetHostIterator
1.1       foxr     1144: 
                   1145: Returns a hash iterator to the host information.  Each get from 
                   1146: this iterator returns a reference to an array that contains 
                   1147: information read from the hosts configuration file.  Array elements
                   1148: are used as follows:
                   1149: 
1.3       albertel 1150:  [0]   - LonCapa host name.
                   1151:  [1]   - LonCapa domain name.
                   1152:  [2]   - Loncapa role (e.g. library or access).
                   1153:  [3]   - DNS name server hostname.
1.11      foxr     1154:  [4]   - IP address (result of e.g. nslookup [3]).
1.3       albertel 1155:  [5]   - Maximum connection count.
                   1156:  [6]   - Idle timeout for reducing connection count.
                   1157:  [7]   - Minimum connection count.
1.1       foxr     1158: 
1.3       albertel 1159: =cut
1.1       foxr     1160: 
                   1161: sub GetHostIterator {
                   1162: 
                   1163:     return HashIterator->new(\%hostshash);    
                   1164: }
1.14      foxr     1165: 
                   1166: ###########################################################
                   1167: #
                   1168: #  The following is an unashamed kludge that is here to
                   1169: # allow LondConnection to be used outside of the
                   1170: # loncapa environment (e.g. by lonManage).
                   1171: # 
                   1172: #   This is a textual inclusion of pieces of the
                   1173: #   Configuration.pm module.
                   1174: #
                   1175: 
                   1176: 
                   1177: my $confdir='/etc/httpd/conf/';
                   1178: 
                   1179: # ------------------- Subroutine read_conf: read LON-CAPA server configuration.
                   1180: # This subroutine reads PerlSetVar values out of specified web server
                   1181: # configuration files.
                   1182: sub read_conf
                   1183:   {
                   1184:     my (@conf_files)=@_;
                   1185:     my %perlvar;
                   1186:     foreach my $filename (@conf_files,'loncapa_apache.conf')
                   1187:       {
1.21      foxr     1188: 	  if($DebugLevel > 3) {
1.31      foxr     1189: 	      print STDERR ("Going to read $confdir.$filename\n");
1.21      foxr     1190: 	  }
1.14      foxr     1191: 	open(CONFIG,'<'.$confdir.$filename) or
                   1192: 	    die("Can't read $confdir$filename");
                   1193: 	while (my $configline=<CONFIG>)
                   1194: 	  {
                   1195: 	    if ($configline =~ /^[^\#]*PerlSetVar/)
                   1196: 	      {
                   1197: 		my ($unused,$varname,$varvalue)=split(/\s+/,$configline);
                   1198: 		chomp($varvalue);
                   1199: 		$perlvar{$varname}=$varvalue;
                   1200: 	      }
                   1201: 	  }
                   1202: 	close(CONFIG);
                   1203:       }
1.21      foxr     1204:     if($DebugLevel > 3) {
1.31      foxr     1205: 	print STDERR "Dumping perlvar:\n";
1.21      foxr     1206: 	foreach my $var (keys %perlvar) {
1.31      foxr     1207: 	    print STDERR "$var = $perlvar{$var}\n";
1.21      foxr     1208: 	}
                   1209:     }
1.14      foxr     1210:     my $perlvarref=\%perlvar;
1.21      foxr     1211:     return $perlvarref;
                   1212: }
1.14      foxr     1213: 
                   1214: #---------------------- Subroutine read_hosts: Read a LON-CAPA hosts.tab
                   1215: # formatted configuration file.
                   1216: #
1.36      albertel 1217: my $RequiredCount = 4;		# Required item count in hosts.tab.
1.14      foxr     1218: my $DefaultMaxCon = 5;		# Default value for maximum connections.
                   1219: my $DefaultIdle   = 1000;       # Default connection idle time in seconds.
                   1220: my $DefaultMinCon = 0;          # Default value for minimum connections.
                   1221: 
                   1222: sub read_hosts {
                   1223:     my $Filename = shift;
                   1224:     my %HostsTab;
                   1225:     
1.36      albertel 1226:     open(CONFIG,'<'.$Filename) or die("Can't read $Filename");
1.14      foxr     1227:     while (my $line = <CONFIG>) {
1.36      albertel 1228: 	if ($line !~ /^\s*\#/) {
                   1229: 	    $line=~s/\s*$//;
1.14      foxr     1230: 	    my @items = split(/:/, $line);
                   1231: 	    if(scalar @items >= $RequiredCount) {
                   1232: 		if (scalar @items == $RequiredCount) { # Only required items:
                   1233: 		    $items[$RequiredCount] = $DefaultMaxCon;
                   1234: 		}
                   1235: 		if(scalar @items == $RequiredCount + 1) { # up through maxcon.
                   1236: 		    $items[$RequiredCount+1] = $DefaultIdle;
                   1237: 		}
                   1238: 		if(scalar @items == $RequiredCount + 2) { # up through idle.
                   1239: 		    $items[$RequiredCount+2] = $DefaultMinCon;
                   1240: 		}
                   1241: 		{
                   1242: 		    my @list = @items; # probably not needed but I'm unsure of 
                   1243: 		    # about the scope of item so...
1.39      albertel 1244: 		    $HostsTab{$list[3]} = \@list; 
1.14      foxr     1245: 		}
                   1246: 	    }
                   1247: 	}
                   1248:     }
                   1249:     close(CONFIG);
                   1250:     my $hostref = \%HostsTab;
                   1251:     return ($hostref);
                   1252: }
1.24      foxr     1253: #
                   1254: #   Get the version of our peer.  Note that this is only well
                   1255: #   defined if the state machine has hit the idle state at least
                   1256: #   once (well actually if it has transitioned out of 
                   1257: #   ReadingVersionString   The member data LondVersion is returned.
                   1258: #
                   1259: sub PeerVersion {
                   1260:    my $self = shift;
1.40    ! albertel 1261:    my ($version) = ($self->{LondVersion} =~ /Revision: 1\.(\d+)/);
        !          1262:    return $version;
1.24      foxr     1263: }
1.1       foxr     1264: 
                   1265: 1;
                   1266: 
                   1267: =pod
1.3       albertel 1268: 
1.1       foxr     1269: =head1 Theory
                   1270: 
1.3       albertel 1271: The lond object is a state machine.  It lives through the following states:
                   1272: 
                   1273: =item Connected:
                   1274: 
                   1275: a TCP connection has been formed, but the passkey has not yet been
                   1276: negotiated.
                   1277: 
                   1278: =item Initialized:
                   1279: 
                   1280: "init" sent.
                   1281: 
                   1282: =item ChallengeReceived:
                   1283: 
                   1284: lond sent its challenge to us.
                   1285: 
                   1286: =item ChallengeReplied:
                   1287: 
                   1288: We replied to lond's challenge waiting for lond's ok.
                   1289: 
                   1290: =item RequestingKey:
                   1291: 
                   1292: We are requesting an encryption key.
                   1293: 
                   1294: =item ReceivingKey:
                   1295: 
                   1296: We are receiving an encryption key.
                   1297: 
                   1298: =item Idle:
                   1299: 
                   1300: Connection was negotiated but no requests are active.
                   1301: 
                   1302: =item SendingRequest:
                   1303: 
                   1304: A request is being sent to the peer.
                   1305: 
                   1306: =item ReceivingReply:
                   1307: 
                   1308: Waiting for an entire reply from the peer.
                   1309: 
                   1310: =item Disconnected:
                   1311: 
                   1312: For whatever reason, the connection was dropped.
                   1313: 
                   1314: When we need to be writing data, we have a writable event. When we
                   1315: need to be reading data, a readable event established.  Events
                   1316: dispatch through the class functions Readable and Writable, and the
                   1317: watcher contains a reference to the associated object to allow object
                   1318: context to be reached.
1.1       foxr     1319: 
                   1320: =head2 Member data.
                   1321: 
1.3       albertel 1322: =item Host
                   1323: 
                   1324: Host socket is connected to.
                   1325: 
                   1326: =item Port
                   1327: 
                   1328: The port the remote lond is listening on.
                   1329: 
                   1330: =item Socket
                   1331: 
                   1332: Socket open on the connection.
                   1333: 
                   1334: =item State
                   1335: 
                   1336: The current state.
                   1337: 
1.31      foxr     1338: =item AuthenticationMode
                   1339: 
                   1340: How authentication is being done. This can be any of:
                   1341: 
                   1342:     o local - Authenticate via a key exchanged in a file.
                   1343:     o ssl   - Authenticate via a key exchaned through a temporary ssl tunnel.
                   1344:     o insecure - Exchange keys in an insecure manner.
                   1345: 
                   1346: insecure is only allowed if the configuration parameter loncAllowInsecure 
                   1347: is nonzero.
                   1348: 
1.3       albertel 1349: =item TransactionRequest
                   1350: 
                   1351: The request being transmitted.
                   1352: 
                   1353: =item TransactionReply
                   1354: 
                   1355: The reply being received from the transaction.
                   1356: 
                   1357: =item InformReadable
                   1358: 
                   1359: True if we want to be called when socket is readable.
                   1360: 
                   1361: =item InformWritable
                   1362: 
                   1363: True if we want to be informed if the socket is writable.
                   1364: 
                   1365: =item Timeoutable
                   1366: 
                   1367: True if the current operation is allowed to timeout.
                   1368: 
                   1369: =item TimeoutValue
                   1370: 
                   1371: Number of seconds in the timeout.
                   1372: 
                   1373: =item TimeoutRemaining
                   1374: 
                   1375: Number of seconds left in the timeout.
                   1376: 
                   1377: =item CipherKey
                   1378: 
                   1379: The key that was negotiated with the peer.
                   1380: 
                   1381: =item Cipher
                   1382: 
                   1383: The cipher obtained via the key.
1.1       foxr     1384: 
                   1385: 
                   1386: =head2 The following are callback like members:
1.3       albertel 1387: 
                   1388: =item Tick:
                   1389: 
                   1390: Called in response to a timer tick. Used to managed timeouts etc.
                   1391: 
                   1392: =item Readable:
                   1393: 
                   1394: Called when the socket becomes readable.
                   1395: 
                   1396: =item Writable:
                   1397: 
                   1398: Called when the socket becomes writable.
                   1399: 
                   1400: =item TimedOut:
                   1401: 
                   1402: Called when a timed operation timed out.
                   1403: 
1.1       foxr     1404: 
                   1405: =head2 The following are operational member functions.
1.3       albertel 1406: 
                   1407: =item InitiateTransaction:
                   1408: 
                   1409: Called to initiate a new transaction
                   1410: 
                   1411: =item SetStateTransitionCallback:
                   1412: 
                   1413: Called to establish a function that is called whenever the object goes
                   1414: through a state transition.  This is used by The client to manage the
                   1415: work flow for the object.
                   1416: 
                   1417: =item SetTimeoutCallback:
                   1418: 
                   1419: Set a function to be called when a transaction times out.  The
                   1420: function will be called with the object as its sole parameter.
                   1421: 
                   1422: =item Encrypt:
                   1423: 
                   1424: Encrypts a block of text according to the cipher negotiated with the
                   1425: peer (assumes the text is a command).
                   1426: 
                   1427: =item Decrypt:
                   1428: 
                   1429: Decrypts a block of text according to the cipher negotiated with the
                   1430: peer (assumes the block was a reply.
1.5       foxr     1431: 
                   1432: =item Shutdown:
                   1433: 
                   1434: Shuts off the socket.
1.1       foxr     1435: 
                   1436: =head2 The following are selector member functions:
                   1437: 
1.3       albertel 1438: =item GetState:
                   1439: 
                   1440: Returns the current state
                   1441: 
                   1442: =item GetSocket:
                   1443: 
                   1444: Gets the socekt open on the connection to lond.
                   1445: 
                   1446: =item WantReadable:
                   1447: 
                   1448: true if the current state requires a readable event.
                   1449: 
                   1450: =item WantWritable:
                   1451: 
                   1452: true if the current state requires a writable event.
                   1453: 
                   1454: =item WantTimeout:
                   1455: 
                   1456: true if the current state requires timeout support.
                   1457: 
                   1458: =item GetHostIterator:
                   1459: 
                   1460: Returns an iterator into the host file hash.
                   1461: 
1.1       foxr     1462: =cut

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>