File:  [LON-CAPA] / loncom / auth / switchserver.pm
Revision 1.28.2.2: download - view: text, annotated - select for diffs
Wed Aug 18 19:40:23 2010 UTC (13 years, 10 months ago) by raeburn
Branches: version_2_10_X
CVS tags: version_2_10_0_RC1
- Backport 1.29.

    1: # The LearningOnline Network
    2: # Switch Servers Handler
    3: #
    4: # $Id: switchserver.pm,v 1.28.2.2 2010/08/18 19:40:23 raeburn Exp $
    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/
   27: #
   28: 
   29: package Apache::switchserver;
   30: 
   31: use strict;
   32: use Apache::Constants qw(:common);
   33: use Apache::lonnet;
   34: use Apache::lonmenu;
   35: use CGI::Cookie();
   36: use Apache::lonlocal;
   37: use LONCAPA qw(:DEFAULT :match);
   38: 
   39: sub init_env {
   40:     my ($r) = @_;
   41:     
   42:     if (-e $env{'user.environment'}) {
   43: 	return  $env{'user.environment'};
   44:     }
   45:     my $requrl=$r->uri;
   46:     my $handle= &Apache::lonnet::check_for_valid_session($r);
   47:     if ($handle ne '') {
   48: 	return undef;
   49:     }
   50:     my $lonidsdir=$r->dir_config('lonIDsDir');
   51:     &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
   52:     
   53:     return $r->dir_config('lonIDsDir')."/$handle.id";
   54: }
   55: 
   56: sub do_redirect {
   57:     my ($r,$url,$only_body,$extra_text) = @_;
   58:     $r->send_http_header;
   59:     my $start_page = 
   60: 	&Apache::loncommon::start_page('Switching Server ...',undef,
   61: 				       {'redirect'       => [0.5,$url],
   62:                                         'no_inline_link' => 1,
   63: 					'only_body'      => $only_body,});
   64:     my $end_page   = &Apache::loncommon::end_page();
   65:     $r->print($start_page.$extra_text.$end_page);
   66:     return OK;
   67: 
   68: }
   69: 
   70: sub handler {
   71:     my ($r) = @_;
   72:     
   73:     my $handle=&init_env($r);
   74:     if (!defined($handle)) { return FORBIDDEN; }
   75: 
   76:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
   77: 				['otherserver','role','origurl','symb']);
   78: 
   79:     my $switch_to=&Apache::lonnet::hostname($env{'form.otherserver'});
   80:     if (! $env{'form.otherserver'}) {
   81: 	$env{'form.otherserver'} =	
   82: 	    &Apache::lonnet::find_existing_session($env{'user.domain'},
   83: 						   $env{'user.name'});
   84: 	if (! $env{'form.otherserver'}) {
   85: 	    $env{'form.otherserver'} = 
   86: 		&Apache::lonnet::spareserver(30000,undef,1);
   87: 	}
   88: 	$switch_to=&Apache::lonnet::hostname($env{'form.otherserver'});
   89:     }
   90: 
   91:     if (!defined($switch_to)) { return FORBIDDEN; }
   92: 
   93:     if ($env{'user.name'} eq 'public'
   94: 	&& $env{'user.domain'} eq 'public') {
   95: 	my $url = 'http://'.$switch_to.$r->uri;
   96: 	return &do_redirect($r,$url,1)
   97:     }
   98: 
   99:     my $skip_canhost_check = '';
  100:     if ($env{'form.role'}) {
  101:         if (!exists($env{'user.role.'.$env{'form.role'}})) {
  102:             return FORBIDDEN;
  103:         } else {
  104:             my $now = time;
  105:             my ($start,$end) = split(/\./,$env{'user.role.'.$env{'form.role'}});
  106:             if (($start && $start > $now) || ($end && $end < $now)) {
  107:                 return FORBIDDEN;
  108:             } elsif ($env{'form.role'} eq 'au./'.$env{'user.domain'}.'/') {
  109:                 if (&Apache::lonnet::homeserver($env{'user.name'},$env{'user.domain'}) eq $env{'form.otherserver'}) {
  110:                     $skip_canhost_check = 1;
  111:                 }
  112:             } elsif ($env{'form.role'} =~ m{^[ac]a\./($match_domain)/($match_username)/$}) {
  113:                 if (&Apache::lonnet::homeserver($2,$1) eq $env{'form.otherserver'}) {
  114:                     $skip_canhost_check = 1; 
  115:                 }
  116:             }
  117:         }
  118:     }
  119: 
  120:     unless ($skip_canhost_check) {
  121:         my $canhost = 1;
  122:         my $uprimary_id = &Apache::lonnet::domain($env{'user.domain'},'primary');
  123:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
  124:         my @intdoms;
  125:         my $internet_names = &Apache::lonnet::get_internet_names($env{'form.otherserver'});
  126:         if (ref($internet_names) eq 'ARRAY') {
  127:             @intdoms = @{$internet_names};
  128:         }
  129:         unless ($uint_dom ne '' && grep(/^\Q$uint_dom\E$/,@intdoms)) {
  130:             my $serverhomeID = &Apache::lonnet::get_server_homeID($switch_to);
  131:             my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);
  132:             my %defdomdefaults = &Apache::lonnet::get_domain_defaults($serverhomedom);
  133:             my %udomdefaults = &Apache::lonnet::get_domain_defaults($env{'user.domain'});
  134:             my $remoterev = &Apache::lonnet::get_server_loncaparev($env{'user.domain'},$env{'form.otherserver'});
  135:             $canhost = 
  136:                 &Apache::lonnet::can_host_session($env{'user.domain'},
  137:                                                   $env{'form.otherserver'},
  138:                                                   $remoterev,
  139:                                                   $udomdefaults{'remotesessions'},
  140:                                                   $defdomdefaults{'hostedsessions'});
  141:         }
  142:         unless ($canhost) { return FORBIDDEN; }
  143:     }
  144: 
  145:     #remove session env, and log event
  146:     unlink($handle);
  147:     my %temp=('switchserver' => time.':'.$env{'form.otherserver'},
  148: 	      $env{'form.role'});
  149:     &Apache::lonnet::put('email_status',\%temp);
  150:     &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
  151: 			 $env{'user.home'},
  152: 			 "Switch Server to $env{'form.otherserver'} with role $env{'form.role'} $ENV{'REMOTE_ADDR'}");
  153: 
  154:     &Apache::loncommon::content_type($r,'text/html');
  155: 
  156:     #expire the cookie
  157:     my $c = new CGI::Cookie(-name    => 'lonID',
  158: 			    -value   => '',
  159: 			    -expires => '-10y',);
  160:     $r->header_out('Set-cookie' => $c);
  161: 
  162:     if ($r->header_only) {
  163: 	$r->send_http_header;
  164: 	return OK;
  165:     }
  166: # -------------------------------------------------------- Menu script and info
  167: 
  168:     my $windowinfo=
  169:         &Apache::lonmenu::close().
  170:         &Apache::lonnavmaps::close();
  171: # ---------------------------------------------------------------- Get handover
  172: 
  173:     my %info=('ip'       => $ENV{'REMOTE_ADDR'},
  174: 	      'domain'   => $env{'user.domain'},
  175: 	      'username' => $env{'user.name'},
  176: 	      'role'     => $env{'form.role'},
  177: 	      'server'   => $r->dir_config('lonHostID'));
  178:     if ($env{'form.origurl'}) {
  179:         $info{'origurl'} = $env{'form.origurl'};
  180:     }
  181:     if ($env{'form.symb'}) {
  182:         $info{'symb'} = $env{'form.symb'};
  183:     }
  184:     if ($env{'request.sso.login'}) {
  185: 	$info{'sso.login'} = $env{'request.sso.login'};
  186:     }
  187:     if ($env{'request.sso.reloginserver'}) {
  188:         $info{'sso.reloginserver'} = $env{'request.sso.reloginserver'};
  189:     }
  190:     my $token = &Apache::lonnet::tmpput(\%info,$env{'form.otherserver'});
  191:     my $url ='http://'.$switch_to.'/adm/login?'.
  192: 	'domain='.$env{'user.domain'}.
  193: 	'&amp;username='.$env{'user.name'}.
  194: 	'&amp;token='.$token;
  195: # --------------------------------------------------------------- Screen Output
  196:     &Apache::lonnet::flushcourselogs();
  197:     return &do_redirect($r,$url,0,$windowinfo);
  198: }
  199: 
  200: 1;
  201: __END__
  202: 
  203: 
  204: 
  205: 
  206: 
  207: 
  208: 
  209: 

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