Diff for /loncom/LondConnection.pm between versions 1.32 and 1.44

version 1.32, 2004/06/17 10:15:46 version 1.44, 2007/03/28 20:28:29
Line 43  use LONCAPA::lonssl; Line 43  use LONCAPA::lonssl;
   
   
 my $DebugLevel=0;  my $DebugLevel=0;
 my %hostshash;  
 my %perlvar;  my %perlvar;
 my $LocalDns = ""; # Need not be defined for managers.  
 my $InsecureOk;  my $InsecureOk;
   
 #  #
Line 71  sub ReadConfig { Line 69  sub ReadConfig {
   
     my $perlvarref = read_conf('loncapa.conf');      my $perlvarref = read_conf('loncapa.conf');
     %perlvar    = %{$perlvarref};      %perlvar    = %{$perlvarref};
     my $hoststab   = read_hosts(  
  "$perlvar{lonTabDir}/hosts.tab") ||   
  die "Can't read host table!!";  
     %hostshash  = %{$hoststab};  
     $ConfigRead = 1;      $ConfigRead = 1;
           
     my $myLonCapaName = $perlvar{lonHostID};  
     Debug(8, "My loncapa name is $myLonCapaName");  
       
     if(defined $hostshash{$myLonCapaName}) {  
  Debug(8, "My loncapa name is in hosthash");  
  my @ConfigLine = @{$hostshash{$myLonCapaName}};  
  $LocalDns = $ConfigLine[3];  
  Debug(8, "Got local name $LocalDns");  
     }  
     $InsecureOk = $perlvar{loncAllowInsecure};  
       
     Debug(3, "ReadConfig - LocalDNS = $LocalDns");  
 }  
   
 #  
 #  Read a foreign configuration.  
 #  This sub is intended for the cases where the package  
 #  will be read from outside the LonCAPA environment, in that case  
 #  the client will need to explicitly provide:  
 #   - A file in hosts.tab format.  
 #   - Some idea of the 'lonCAPA' name of the local host (for building  
 #     the encryption key).  
 #  
 #  Parameters:  
 #      MyHost   - Name of this host as far as LonCAPA is concerned.  
 #      Filename - Name of a hosts.tab formatted file that will be used  
 #                 to build up the hosts table.  
 #  
 sub ReadForeignConfig {  
   
     my ($MyHost, $Filename) = @_;  
   
     &Debug(4, "ReadForeignConfig $MyHost $Filename\n");  
   
     $perlvar{lonHostID} = $MyHost; # Rmember my host.  
     my $hosttab = read_hosts($Filename) ||  
  die "Can't read hosts table!!";  
     %hostshash = %{$hosttab};  
     if($DebugLevel > 3) {  
  foreach my $host (keys %hostshash) {  
     print STDERR "host $host => $hostshash{$host}\n";  
  }  
     }  
     $ConfigRead = 1;  
   
     my $myLonCapaName = $perlvar{lonHostID};  
       
     if(defined $hostshash{$myLonCapaName}) {  
  my @ConfigLine = @{$hostshash{$myLonCapaName}};  
  $LocalDns = $ConfigLine[3];  
     }  
     $InsecureOk = $perlvar{loncAllowInsecure};      $InsecureOk = $perlvar{loncAllowInsecure};
       
     Debug(3, "ReadForeignConfig  - LocalDNS = $LocalDns");  
   
 }  }
   
 sub Debug {  sub Debug {
Line 154  Dump the internal state of the object: F Line 94  Dump the internal state of the object: F
 sub Dump {  sub Dump {
     my $self   = shift;      my $self   = shift;
     my $level  = shift;      my $level  = shift;
       my $now    = time;
       my $local  = localtime($now);
           
     if ($level <= $DebugLevel) {      if ($level >= $DebugLevel) {
  return;   return;
     }      }
   
       
     my $key;      my $key;
     my $value;      my $value;
     print STDERR "Dumping LondConnectionObject:\n";      print STDERR "[ $local ] Dumping LondConnectionObject:\n";
       print STDERR join(':',caller(1))."\n";
     while(($key, $value) = each %$self) {      while(($key, $value) = each %$self) {
  print STDERR "$key -> $value\n";   print STDERR "$key -> $value\n";
     }      }
Line 209  host the remote lond is on. This host is Line 153  host the remote lond is on. This host is
 =cut  =cut
   
 sub new {  sub new {
       my ($class, $DnsName, $Port, $lonid) = @_;
     my ($class, $Hostname, $Port) = @_;  
   
     if (!$ConfigRead) {      if (!$ConfigRead) {
  ReadConfig();   ReadConfig();
  $ConfigRead = 1;   $ConfigRead = 1;
     }      }
     &Debug(4,$class."::new( ".$Hostname.",".$Port.")\n");      &Debug(4,$class."::new( ".$DnsName.",".$Port.")\n");
   
     # The host must map to an entry in the hosts table:      # The host must map to an entry in the hosts table:
     #  We connect to the dns host that corresponds to that      #  We connect to the dns host that corresponds to that
Line 224  sub new { Line 167  sub new {
     #  negotion.  In the objec these become the Host and      #  negotion.  In the objec these become the Host and
     #  LoncapaHim fields of the object respectively.      #  LoncapaHim fields of the object respectively.
     #      #
     if (!exists $hostshash{$Hostname}) {      # if it is me use loopback for connection
  &Debug(8, "No Such host $Hostname");      if ($DnsName eq &main::my_hostname()) { $DnsName="127.0.0.1"; }
  return undef; # No such host!!!      Debug(9, "Connecting to $DnsName");
     }  
     my @ConfigLine = @{$hostshash{$Hostname}};  
     my $DnsName    = $ConfigLine[3]; # 4'th item is dns of host.  
     Debug(5, "Connecting to ".$DnsName);  
     # Now create the object...      # Now create the object...
     my $self     = { Host               => $DnsName,      my $self     = { Host               => $DnsName,
                      LoncapaHim         => $Hostname,                       LoncapaHim         => $lonid,
                      Port               => $Port,                       Port               => $Port,
                      State              => "Initialized",                       State              => "Initialized",
      AuthenticationMode => "",       AuthenticationMode => "",
                      TransactionRequest => "",                       TransactionRequest => "",
                      TransactionReply   => "",                       TransactionReply   => "",
                        NextRequest        => "",
                      InformReadable     => 0,                       InformReadable     => 0,
                      InformWritable     => 0,                       InformWritable     => 0,
                      TimeoutCallback    => undef,                       TimeoutCallback    => undef,
                      TransitionCallback => undef,                       TransitionCallback => undef,
                      Timeoutable        => 0,                       Timeoutable        => 0,
                      TimeoutValue       => 30,                       TimeoutValue       => 3,
                      TimeoutRemaining   => 0,                       TimeoutRemaining   => 0,
      LocalKeyFile       => "",       LocalKeyFile       => "",
                      CipherKey          => "",                       CipherKey          => "",
Line 256  sub new { Line 196  sub new {
        Type     => SOCK_STREAM,         Type     => SOCK_STREAM,
        Proto    => "tcp",         Proto    => "tcp",
        Timeout  => 3)) {         Timeout  => 3)) {
    Debug(8, "Error? \n$@ \n$!");
  return undef; # Inidicates the socket could not be made.   return undef; # Inidicates the socket could not be made.
     }      }
     my $socket = $self->{Socket}; # For local use only.      my $socket = $self->{Socket}; # For local use only.
     #  If we are local, we'll first try local auth mode, otherwise, we'll try the       #  If we are local, we'll first try local auth mode, otherwise, we'll try
     #  ssl auth mode:      #  the ssl auth mode:
   
     Debug(8, "Connecting to $DnsName I am $LocalDns");  
     my $key;      my $key;
     my $keyfile;      my $keyfile;
     if ($DnsName eq $LocalDns) {      if ($DnsName eq '127.0.0.1') {
  $self->{AuthenticationMode} = "local";   $self->{AuthenticationMode} = "local";
  ($key, $keyfile)         = lonlocal::CreateKeyFile();   ($key, $keyfile)         = lonlocal::CreateKeyFile();
  Debug(8, "Local key: $key, stored in $keyfile");   Debug(8, "Local key: $key, stored in $keyfile");
Line 290  sub new { Line 230  sub new {
     return undef;      return undef;
  }   }
   
     }       } else {
     else {   #  Remote peer:  I'd like to do ssl, but if my host key or certificates
  $self->{AuthenticationMode} = "ssl";   #  are not all installed, my only choice is insecure, if that's 
  $self->{TransactionRequest} = "init:ssl\n";   #  allowed:
   
    my ($ca, $cert) = lonssl::CertificateFile;
    my $sslkeyfile  = lonssl::KeyFile;
   
    if((defined $ca)  && (defined $cert) && (defined $sslkeyfile)) {
   
       $self->{AuthenticationMode} = "ssl";
       $self->{TransactionRequest} = "init:ssl\n";
    } else {
       if($InsecureOk) { # Allowed to do insecure:
    $self->{AuthenticationMode} = "insecure";
    $self->{TransactionRequest} = "init\n";
       }
       else { # Not allowed to do insecure...
    $socket->close;
    return undef;
       }
    }
     }      }
   
     #      #
Line 307  sub new { Line 265  sub new {
     #      #
     # Set socket to nonblocking I/O.      # Set socket to nonblocking I/O.
     #      #
     my $socket = $self->{Socket};  
     my $flags    = fcntl($socket, F_GETFL,0);      my $flags    = fcntl($socket, F_GETFL,0);
     if(!$flags) {      if(!$flags) {
  $socket->close;   $socket->close;
Line 482  sub Readable { Line 439  sub Readable {
     return 0;      return 0;
   
  } elsif ($self->{State} eq "ReadingVersionString") {   } elsif ($self->{State} eq "ReadingVersionString") {
     $self->{LondVersion}       = chomp($self->{TransactionReply});      chomp($self->{TransactionReply});
       $self->{LondVersion}       = $self->{TransactionReply};
     $self->Transition("SetHost");      $self->Transition("SetHost");
     $self->{InformReadable}    = 0;      $self->{InformReadable}    = 0;
     $self->{InformWritable}    = 1;      $self->{InformWritable}    = 1;
Line 533  sub Readable { Line 491  sub Readable {
     my $answer = $self->{TransactionReply};      my $answer = $self->{TransactionReply};
     if($answer =~ /^enc\:/) {      if($answer =~ /^enc\:/) {
  $answer = $self->Decrypt($answer);   $answer = $self->Decrypt($answer);
  $self->{TransactionReply} = $answer;   $self->{TransactionReply} = "$answer\n";
     }      }
       # if we have a NextRequest do it immeadiately
       if ($self->{NextRequest}) {
    $self->{TransactionRequest} = $self->{NextRequest};
    undef( $self->{NextRequest} );
    $self->{TransactionReply}   = "";
    $self->{InformWritable}     = 1;
    $self->{InformReadable}     = 0;
    $self->{Timeoutable}        = 1;
    $self->{TimeoutRemaining}   = $self->{TimeoutValue};
    $self->Transition("SendingRequest");
    return 0;
       } else {
     # finish the transaction      # finish the transaction
   
     $self->ToIdle();   $self->ToIdle();
     return 0;   return 0;
       }
  } elsif ($self->{State} eq "Disconnected") { # No connection.   } elsif ($self->{State} eq "Disconnected") { # No connection.
     return -1;      return -1;
  } else { # Internal error: Invalid state.   } else { # Internal error: Invalid state.
Line 691  sub InitiateTransaction { Line 661  sub InitiateTransaction {
  return -1; # Error indicator.   return -1; # Error indicator.
     }      }
     # if the transaction is to be encrypted encrypt the data:      # if the transaction is to be encrypted encrypt the data:
       (my $sethost, my $server,$data)=split(/:/,$data,3);
   
     if($data =~ /^encrypt\:/) {      if($data =~ /^encrypt\:/) {
  $data = $self->Encrypt($data);   $data = $self->Encrypt($data);
     }      }
   
     # Setup the trasaction      # Setup the trasaction
       # currently no version of lond supports inlining the sethost
     $self->{TransactionRequest} = $data;      if ($self->PeerVersion() <= 321) {
    if ($server ne $self->{LoncapaHim}) {
       $self->{NextRequest}        = $data;
       $self->{TransactionRequest} = "$sethost:$server\n";
       $self->{LoncapaHim}         = $server;
    } else {
       $self->{TransactionRequest}        = $data;
    }
       } else {
    $self->{LoncapaHim}         = $server;
    $self->{TransactionRequest} = "$sethost:$server:$data";
       }
     $self->{TransactionReply}   = "";      $self->{TransactionReply}   = "";
     $self->{InformWritable}     = 1;      $self->{InformWritable}     = 1;
     $self->{InformReadable}     = 0;      $self->{InformReadable}     = 0;
Line 931  sub Decrypt { Line 913  sub Decrypt {
     #  $length tells us the actual length of the decrypted string:      #  $length tells us the actual length of the decrypted string:
   
     $decrypted = substr($decrypted, 0, $length);      $decrypted = substr($decrypted, 0, $length);
       Debug(9, "Decrypted $EncryptedString to $decrypted");
   
     return $decrypted;      return $decrypted;
   
Line 1086  sub CompleteInsecure { Line 1069  sub CompleteInsecure {
     }      }
 }  }
   
 =pod  
   
 =head2 GetHostIterator  
   
 Returns a hash iterator to the host information.  Each get from   
 this iterator returns a reference to an array that contains   
 information read from the hosts configuration file.  Array elements  
 are used as follows:  
   
  [0]   - LonCapa host name.  
  [1]   - LonCapa domain name.  
  [2]   - Loncapa role (e.g. library or access).  
  [3]   - DNS name server hostname.  
  [4]   - IP address (result of e.g. nslookup [3]).  
  [5]   - Maximum connection count.  
  [6]   - Idle timeout for reducing connection count.  
  [7]   - Minimum connection count.  
   
 =cut  
   
 sub GetHostIterator {  
   
     return HashIterator->new(\%hostshash);      
 }  
   
 ###########################################################  ###########################################################
 #  #
 #  The following is an unashamed kludge that is here to  #  The following is an unashamed kludge that is here to
Line 1122  sub GetHostIterator { Line 1080  sub GetHostIterator {
 #  #
   
   
 my $confdir='/etc/httpd/conf/';  my @confdirs=('/etc/httpd/conf/','/etc/apache2/');
   
 # ------------------- Subroutine read_conf: read LON-CAPA server configuration.  # ------------------- Subroutine read_conf: read LON-CAPA server configuration.
 # This subroutine reads PerlSetVar values out of specified web server  # This subroutine reads PerlSetVar values out of specified web server
Line 1130  my $confdir='/etc/httpd/conf/'; Line 1088  my $confdir='/etc/httpd/conf/';
 sub read_conf  sub read_conf
   {    {
     my (@conf_files)=@_;      my (@conf_files)=@_;
     my %perlvar;      my (%perlvar,%configdirs);
     foreach my $filename (@conf_files,'loncapa_apache.conf')      foreach my $filename (@conf_files,'loncapa_apache.conf') {
       {          my $configdir = '';
   if($DebugLevel > 3) {          $configdirs{$filename} = [@confdirs];
       print STDERR ("Going to read $confdir.$filename\n");          while ($configdir eq '' && @{$configdirs{$filename}} > 0) {
   }              my $testdir = shift(@{$configdirs{$filename}});
  open(CONFIG,'<'.$confdir.$filename) or              if (-e $testdir.$filename) {
     die("Can't read $confdir$filename");                  $configdir = $testdir;
  while (my $configline=<CONFIG>)              }
   {          }
     if ($configline =~ /^[^\#]*PerlSetVar/)          if ($configdir eq '') {
       {              die("Couldn't find a directory containing $filename");
  my ($unused,$varname,$varvalue)=split(/\s+/,$configline);          }
    if($DebugLevel > 3) {
       print STDERR ("Going to read $configdir.$filename\n");
    }
    open(CONFIG,'<'.$configdir.$filename) or
       die("Can't read $configdir$filename");
    while (my $configline=<CONFIG>) {
       if ($configline =~ /^[^\#]*PerlSetVar/) {
           my ($unused,$varname,$varvalue)=split(/\s+/,$configline);
  chomp($varvalue);   chomp($varvalue);
  $perlvar{$varname}=$varvalue;   $perlvar{$varname}=$varvalue;
       }      }
   }   }
  close(CONFIG);   close(CONFIG);
       }      }
     if($DebugLevel > 3) {      if($DebugLevel > 3) {
  print STDERR "Dumping perlvar:\n";   print STDERR "Dumping perlvar:\n";
  foreach my $var (keys %perlvar) {   foreach my $var (keys %perlvar) {
Line 1159  sub read_conf Line 1125  sub read_conf
     return $perlvarref;      return $perlvarref;
 }  }
   
 #---------------------- Subroutine read_hosts: Read a LON-CAPA hosts.tab  
 # formatted configuration file.  
 #  
 my $RequiredCount = 5; # Required item count in hosts.tab.  
 my $DefaultMaxCon = 5; # Default value for maximum connections.  
 my $DefaultIdle   = 1000;       # Default connection idle time in seconds.  
 my $DefaultMinCon = 0;          # Default value for minimum connections.  
   
 sub read_hosts {  
     my $Filename = shift;  
     my %HostsTab;  
       
    open(CONFIG,'<'.$Filename) or die("Can't read $Filename");  
     while (my $line = <CONFIG>) {  
  if (!($line =~ /^\s*\#/)) {  
     my @items = split(/:/, $line);  
     if(scalar @items >= $RequiredCount) {  
  if (scalar @items == $RequiredCount) { # Only required items:  
     $items[$RequiredCount] = $DefaultMaxCon;  
  }  
  if(scalar @items == $RequiredCount + 1) { # up through maxcon.  
     $items[$RequiredCount+1] = $DefaultIdle;  
  }  
  if(scalar @items == $RequiredCount + 2) { # up through idle.  
     $items[$RequiredCount+2] = $DefaultMinCon;  
  }  
  {  
     my @list = @items; # probably not needed but I'm unsure of   
     # about the scope of item so...  
     $HostsTab{$list[0]} = \@list;   
  }  
     }  
  }  
     }  
     close(CONFIG);  
     my $hostref = \%HostsTab;  
     return ($hostref);  
 }  
 #  #
 #   Get the version of our peer.  Note that this is only well  #   Get the version of our peer.  Note that this is only well
 #   defined if the state machine has hit the idle state at least  #   defined if the state machine has hit the idle state at least
Line 1205  sub read_hosts { Line 1133  sub read_hosts {
 #  #
 sub PeerVersion {  sub PeerVersion {
    my $self = shift;     my $self = shift;
         my ($version) = ($self->{LondVersion} =~ /Revision: 1\.(\d+)/);
    return $self->{LondVersion};     return $version;
 }  }
   
 1;  1;
Line 1402  true if the current state requires a wri Line 1330  true if the current state requires a wri
   
 true if the current state requires timeout support.  true if the current state requires timeout support.
   
 =item GetHostIterator:  
   
 Returns an iterator into the host file hash.  
   
 =cut  =cut

Removed from v.1.32  
changed lines
  Added in v.1.44


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