Annotation of loncom/auth/switchserver.pm, revision 1.28.2.4

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

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