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

1.1       albertel    1: # The LearningOnline Network
                      2: # Switch Servers Handler
                      3: #
1.35.2.1! raeburn     4: # $Id: switchserver.pm,v 1.35 2016/12/16 15:31:11 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;
1.35.2.1! raeburn    34: use Digest::MD5 qw(md5_hex);
1.1       albertel   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);
1.35.2.1! raeburn    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;
1.35.2.1! raeburn    59:     my $delay = 0.5;
        !            60:     if ($only_body) {
        !            61:         $delay = 0;
        !            62:     }
1.14      albertel   63:     my $start_page = 
1.16      raeburn    64: 	&Apache::loncommon::start_page('Switching Server ...',undef,
1.35.2.1! raeburn    65: 				       {'redirect'       => [$delay,$url],
1.14      albertel   66: 					'only_body'      => $only_body,});
                     67:     my $end_page   = &Apache::loncommon::end_page();
                     68:     $r->print($start_page.$extra_text.$end_page);
1.30      raeburn    69:     unless ($env{'user.name'} eq 'public' && ($env{'user.domain'} eq 'public')) {
                     70:         $r->register_cleanup(\&flush_course_logs);
                     71:     }
1.14      albertel   72:     return OK;
1.30      raeburn    73: }
1.14      albertel   74: 
1.35.2.1! raeburn    75: sub balancer_cookieid {
        !            76:     my ($r,$desthost,$uname,$udom) = @_;
        !            77:     my @hosts = &Apache::lonnet::current_machine_ids();
        !            78:     my $newcookieid;
        !            79:     unless (grep(/^\Q$desthost\E$/,@hosts)) {
        !            80:         my $balancedir=$r->dir_config('lonBalanceDir');
        !            81:         $newcookieid = &md5_hex(&md5_hex(time.{}.rand().$$));
        !            82:         my $cookie = $udom.'_'.$uname.'_'.$newcookieid;
        !            83:         my $balcookie = "balanceID=$cookie; path=/; HttpOnly;";
        !            84:         if (open(my $fh,'>',"$balancedir/$cookie.id")) {
        !            85:             print $fh $desthost;
        !            86:             close($fh);
        !            87:             $r->headers_out->add('Set-cookie' => $balcookie);
        !            88:         }
        !            89:     }
        !            90:     return $newcookieid;
        !            91: }
        !            92: 
1.30      raeburn    93: sub flush_course_logs {
                     94:     &Apache::lonnet::flushcourselogs();
                     95:     return OK;
1.14      albertel   96: }
                     97: 
1.1       albertel   98: sub handler {
1.5       albertel   99:     my ($r) = @_;
1.1       albertel  100:     
                    101:     my $handle=&init_env($r);
                    102:     if (!defined($handle)) { return FORBIDDEN; }
                    103: 
                    104:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.22      raeburn   105: 				['otherserver','role','origurl','symb']);
1.5       albertel  106: 
1.13      albertel  107:     my $switch_to=&Apache::lonnet::hostname($env{'form.otherserver'});
1.17      albertel  108:     if (! $env{'form.otherserver'}) {
                    109: 	$env{'form.otherserver'} =	
                    110: 	    &Apache::lonnet::find_existing_session($env{'user.domain'},
                    111: 						   $env{'user.name'});
                    112: 	if (! $env{'form.otherserver'}) {
                    113: 	    $env{'form.otherserver'} = 
                    114: 		&Apache::lonnet::spareserver(30000,undef,1);
                    115: 	}
1.13      albertel  116: 	$switch_to=&Apache::lonnet::hostname($env{'form.otherserver'});
1.4       albertel  117:     }
1.5       albertel  118: 
1.1       albertel  119:     if (!defined($switch_to)) { return FORBIDDEN; }
1.4       albertel  120: 
1.33      raeburn   121:     my $protocol = 'http';
                    122:     if ($env{'form.otherserver'}) {
                    123:         if ($Apache::lonnet::protocol{$env{'form.otherserver'}} eq 'https') {
                    124:             $protocol = $Apache::lonnet::protocol{$env{'form.otherserver'}};
                    125:         }
1.35.2.1! raeburn   126:     }
1.33      raeburn   127: 
1.14      albertel  128:     if ($env{'user.name'} eq 'public'
                    129: 	&& $env{'user.domain'} eq 'public') {
1.33      raeburn   130: 	my $url = $protocol.'://'.$switch_to.$r->uri;
1.14      albertel  131: 	return &do_redirect($r,$url,1)
                    132:     }
                    133: 
1.28      raeburn   134:     my $skip_canhost_check = '';
1.35.2.1! raeburn   135:     my $now = time;
1.28      raeburn   136:     if ($env{'form.role'}) {
                    137:         if (!exists($env{'user.role.'.$env{'form.role'}})) {
1.35      raeburn   138:             delete($env{'form.role'});
1.28      raeburn   139:         } else {
                    140:             my ($start,$end) = split(/\./,$env{'user.role.'.$env{'form.role'}});
                    141:             if (($start && $start > $now) || ($end && $end < $now)) {
1.35      raeburn   142:                 delete($env{'form.role'});
1.28      raeburn   143:             } elsif ($env{'form.role'} eq 'au./'.$env{'user.domain'}.'/') {
                    144:                 if (&Apache::lonnet::homeserver($env{'user.name'},$env{'user.domain'}) eq $env{'form.otherserver'}) {
                    145:                     $skip_canhost_check = 1;
                    146:                 }
1.31      raeburn   147:             } elsif ($env{'form.role'} =~ m{^[ac]a\./($match_domain)/($match_username)$}) {
1.28      raeburn   148:                 if (&Apache::lonnet::homeserver($2,$1) eq $env{'form.otherserver'}) {
                    149:                     $skip_canhost_check = 1; 
                    150:                 }
                    151:             }
                    152:         }
                    153:     }
                    154: 
                    155:     unless ($skip_canhost_check) {
                    156:         my $canhost = 1;
                    157:         my $uprimary_id = &Apache::lonnet::domain($env{'user.domain'},'primary');
                    158:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
1.29      raeburn   159:         my @intdoms;
                    160:         my $internet_names = &Apache::lonnet::get_internet_names($env{'form.otherserver'});
                    161:         if (ref($internet_names) eq 'ARRAY') {
                    162:             @intdoms = @{$internet_names};
                    163:         }
1.28      raeburn   164:         unless ($uint_dom ne '' && grep(/^\Q$uint_dom\E$/,@intdoms)) {
                    165:             my $serverhomeID = &Apache::lonnet::get_server_homeID($switch_to);
                    166:             my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);
                    167:             my %defdomdefaults = &Apache::lonnet::get_domain_defaults($serverhomedom);
                    168:             my %udomdefaults = &Apache::lonnet::get_domain_defaults($env{'user.domain'});
                    169:             my $remoterev = &Apache::lonnet::get_server_loncaparev($env{'user.domain'},$env{'form.otherserver'});
                    170:             $canhost = 
                    171:                 &Apache::lonnet::can_host_session($env{'user.domain'},
                    172:                                                   $env{'form.otherserver'},
                    173:                                                   $remoterev,
                    174:                                                   $udomdefaults{'remotesessions'},
                    175:                                                   $defdomdefaults{'hostedsessions'});
                    176:         }
                    177:         unless ($canhost) { return FORBIDDEN; }
1.26      raeburn   178:     }
                    179: 
1.1       albertel  180:     #remove session env, and log event
1.5       albertel  181:     unlink($handle);
1.35.2.1! raeburn   182:     my %temp=('switchserver' => $now.':'.$env{'form.otherserver'},
1.1       albertel  183: 	      $env{'form.role'});
                    184:     &Apache::lonnet::put('email_status',\%temp);
1.34      raeburn   185:     my $logmsg = "Switch Server to $env{'form.otherserver'}";
                    186:     if ($env{'form.role'}) {
                    187:         $logmsg .= " with role: $env{'form.role'}";
                    188:     } else {
                    189:         $logmsg .= " (no role)";
                    190:     }
                    191:     $logmsg .= ' '.$ENV{'REMOTE_ADDR'};
1.1       albertel  192:     &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
1.34      raeburn   193: 			 $env{'user.home'},$logmsg);
1.1       albertel  194: 
                    195:     &Apache::loncommon::content_type($r,'text/html');
1.12      albertel  196: 
                    197:     #expire the cookie
                    198:     my $c = new CGI::Cookie(-name    => 'lonID',
                    199: 			    -value   => '',
                    200: 			    -expires => '-10y',);
                    201:     $r->header_out('Set-cookie' => $c);
1.20      albertel  202: 
                    203:     if ($r->header_only) {
                    204: 	$r->send_http_header;
                    205: 	return OK;
                    206:     }
1.1       albertel  207: # -------------------------------------------------------- Menu script and info
                    208: 	   
                    209: # ---------------------------------------------------------------- Get handover
                    210: 
1.35.2.1! raeburn   211:     my $newcookieid;
        !           212:     my $only_body = 0;
        !           213:     my ($is_balancer,$posshost,$setcookie) =
        !           214:         &Apache::lonnet::check_loadbalancing($env{'user.name'},$env{'user.domain'});
        !           215:     if ($is_balancer && $setcookie && $env{'form.otherserver'}) {
        !           216:         # Set a balancer cookie unless browser already sent LON-CAPA load balancer
        !           217:         # cookie which points at the target server
        !           218:         my ($found_server,$balancer_cookie) = &Apache::lonnet::check_for_balancer_cookie($r,1);
        !           219: 
        !           220:         if (($found_server eq $env{'form.otherserver'}) &&
        !           221:             ($balancer_cookie =~ /^\Q$env{'user.domain'}\E_\Q$env{'user.name'}\E_/)) {
        !           222:             $only_body = 1;
        !           223:         } else {
        !           224:             $newcookieid = &balancer_cookieid($r,$env{'form.otherserver'},
        !           225:                                               $env{'user.name'},$env{'user.domain'});
        !           226:         }
        !           227:     }
        !           228: 
1.3       albertel  229:     my %info=('ip'       => $ENV{'REMOTE_ADDR'},
                    230: 	      'domain'   => $env{'user.domain'},
                    231: 	      'username' => $env{'user.name'},
                    232: 	      'role'     => $env{'form.role'},
1.32      raeburn   233: 	      'server'   => $r->dir_config('lonHostID'),
                    234: 	      'balancer' => $is_balancer);
1.35.2.1! raeburn   235:     if ($newcookieid) {
        !           236:         $info{'balcookie'} = $newcookieid;
        !           237:     }
1.22      raeburn   238:     if ($env{'form.origurl'}) {
                    239:         $info{'origurl'} = $env{'form.origurl'};
                    240:     }
                    241:     if ($env{'form.symb'}) {
                    242:         $info{'symb'} = $env{'form.symb'};
                    243:     }
1.8       albertel  244:     if ($env{'request.sso.login'}) {
                    245: 	$info{'sso.login'} = $env{'request.sso.login'};
                    246:     }
1.11      raeburn   247:     if ($env{'request.sso.reloginserver'}) {
                    248:         $info{'sso.reloginserver'} = $env{'request.sso.reloginserver'};
                    249:     }
1.3       albertel  250:     my $token = &Apache::lonnet::tmpput(\%info,$env{'form.otherserver'});
1.33      raeburn   251:     my $url =$protocol.'://'.$switch_to.'/adm/login?'.
1.14      albertel  252: 	'domain='.$env{'user.domain'}.
1.21      albertel  253: 	'&amp;username='.$env{'user.name'}.
                    254: 	'&amp;token='.$token;
1.1       albertel  255: # --------------------------------------------------------------- Screen Output
1.35.2.1! raeburn   256:     return &do_redirect($r, $url, $only_body);
1.1       albertel  257: }
                    258: 
                    259: 1;
                    260: __END__
                    261: 
                    262: 
                    263: 
                    264: 
                    265: 
                    266: 
                    267: 
                    268: 

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