File:  [LON-CAPA] / loncom / auth / switchserver.pm
Revision 1.37: download - view: text, annotated - select for diffs
Tue Aug 8 20:10:55 2017 UTC (6 years, 9 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Take into account trust settings for:
  - othcoau ("Co-author roles in this domain for others")
  - coaurem ("Co-author roles for this domain's users elsewhere")
  when displaying "Switch Server" link to Authoring Space's home server,
  and whether to skip check for ability to host a user on a remote server.

    1: # The LearningOnline Network
    2: # Switch Servers Handler
    3: #
    4: # $Id: switchserver.pm,v 1.37 2017/08/08 20:10:55 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: 					'only_body'      => $only_body,});
   63:     my $end_page   = &Apache::loncommon::end_page();
   64:     $r->print($start_page.$extra_text.$end_page);
   65:     unless ($env{'user.name'} eq 'public' && ($env{'user.domain'} eq 'public')) {
   66:         $r->register_cleanup(\&flush_course_logs);
   67:     }
   68:     return OK;
   69: }
   70: 
   71: sub flush_course_logs {
   72:     &Apache::lonnet::flushcourselogs();
   73:     return OK;
   74: }
   75: 
   76: sub handler {
   77:     my ($r) = @_;
   78:     
   79:     my $handle=&init_env($r);
   80:     if (!defined($handle)) { return FORBIDDEN; }
   81: 
   82:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
   83: 				['otherserver','role','origurl','symb']);
   84: 
   85:     my $switch_to=&Apache::lonnet::hostname($env{'form.otherserver'});
   86:     if (! $env{'form.otherserver'}) {
   87: 	$env{'form.otherserver'} =	
   88: 	    &Apache::lonnet::find_existing_session($env{'user.domain'},
   89: 						   $env{'user.name'});
   90: 	if (! $env{'form.otherserver'}) {
   91: 	    $env{'form.otherserver'} = 
   92: 		&Apache::lonnet::spareserver(30000,undef,1);
   93: 	}
   94: 	$switch_to=&Apache::lonnet::hostname($env{'form.otherserver'});
   95:     }
   96: 
   97:     if (!defined($switch_to)) { return FORBIDDEN; }
   98: 
   99:     my $protocol = 'http';
  100:     if ($env{'form.otherserver'}) {
  101:         if ($Apache::lonnet::protocol{$env{'form.otherserver'}} eq 'https') {
  102:             $protocol = $Apache::lonnet::protocol{$env{'form.otherserver'}};
  103:         }
  104:     } 
  105: 
  106:     if ($env{'user.name'} eq 'public'
  107: 	&& $env{'user.domain'} eq 'public') {
  108: 	my $url = $protocol.'://'.$switch_to.$r->uri;
  109:         unlink($handle);
  110:         #expire the cookie
  111:         my $c = new CGI::Cookie(-name    => 'lonPubID',
  112:                                 -value   => '',
  113:                                 -expires => '-10y',);
  114:         $r->header_out('Set-cookie' => $c);
  115: 	return &do_redirect($r,$url,1)
  116:     }
  117: 
  118:     my $skip_canhost_check = '';
  119:     if ($env{'form.role'}) {
  120:         if (!exists($env{'user.role.'.$env{'form.role'}})) {
  121:             delete($env{'form.role'});
  122:         } else {
  123:             my $now = time;
  124:             my ($start,$end) = split(/\./,$env{'user.role.'.$env{'form.role'}});
  125:             if (($start && $start > $now) || ($end && $end < $now)) {
  126:                 delete($env{'form.role'});
  127:             } elsif ($env{'form.role'} eq 'au./'.$env{'user.domain'}.'/') {
  128:                 if (&Apache::lonnet::homeserver($env{'user.name'},$env{'user.domain'}) eq $env{'form.otherserver'}) {
  129:                     $skip_canhost_check = 1;
  130:                 }
  131:             } elsif ($env{'form.role'} =~ m{^[ac]a\./($match_domain)/($match_username)$}) {
  132:                 my ($audom,$auname) = ($1,$2);
  133:                 if (&Apache::lonnet::homeserver($auname,$audom) eq $env{'form.otherserver'}) {
  134:                     if ((&Apache::lonnet::will_trust('othcoau',$audom,$env{'user.domain'})) &&
  135:                         (&Apache::lonnet::will_trust('coremau',$env{'user.domain'},$audom))) {                             
  136:                         $skip_canhost_check = 1;
  137:                     }
  138:                 }
  139:             }
  140:         }
  141:     }
  142: 
  143:     unless ($skip_canhost_check) {
  144:         my $canhost = 1;
  145:         my $uprimary_id = &Apache::lonnet::domain($env{'user.domain'},'primary');
  146:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
  147:         my @intdoms;
  148:         my $internet_names = &Apache::lonnet::get_internet_names($env{'form.otherserver'});
  149:         if (ref($internet_names) eq 'ARRAY') {
  150:             @intdoms = @{$internet_names};
  151:         }
  152:         unless ($uint_dom ne '' && grep(/^\Q$uint_dom\E$/,@intdoms)) {
  153:             my $serverhomeID = &Apache::lonnet::get_server_homeID($switch_to);
  154:             my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);
  155:             my %defdomdefaults = &Apache::lonnet::get_domain_defaults($serverhomedom);
  156:             my %udomdefaults = &Apache::lonnet::get_domain_defaults($env{'user.domain'});
  157:             my $remoterev = &Apache::lonnet::get_server_loncaparev($env{'user.domain'},$env{'form.otherserver'});
  158:             $canhost = 
  159:                 &Apache::lonnet::can_host_session($env{'user.domain'},
  160:                                                   $env{'form.otherserver'},
  161:                                                   $remoterev,
  162:                                                   $udomdefaults{'remotesessions'},
  163:                                                   $defdomdefaults{'hostedsessions'});
  164:         }
  165:         unless ($canhost) { return FORBIDDEN; }
  166:     }
  167: 
  168:     #remove session env, and log event
  169:     unlink($handle);
  170:     if ($env{'user.linkedenv'} ne '') {
  171:         my $lonidsdir=$r->dir_config('lonIDsDir');
  172:         if ((-l $env{'user.linkedenv'}) &&
  173:             (readlink($env{'user.linkedenv'}) eq "$lonidsdir/$handle.id")) {
  174:             unlink($env{'user.linkedenv'});
  175:         }
  176:     }
  177:     my %temp=('switchserver' => time.':'.$env{'form.otherserver'},
  178: 	      $env{'form.role'});
  179:     &Apache::lonnet::put('email_status',\%temp);
  180:     my $logmsg = "Switch Server to $env{'form.otherserver'}";
  181:     if ($env{'form.role'}) {
  182:         $logmsg .= " with role: $env{'form.role'}";
  183:     } else {
  184:         $logmsg .= " (no role)";
  185:     }
  186:     $logmsg .= ' '.$ENV{'REMOTE_ADDR'};
  187:     &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
  188: 			 $env{'user.home'},$logmsg);
  189: 
  190:     &Apache::loncommon::content_type($r,'text/html');
  191: 
  192:     #expire the cookie
  193:     my $c = new CGI::Cookie(-name    => 'lonID',
  194: 			    -value   => '',
  195: 			    -expires => '-10y',);
  196:     $r->headers_out->add('Set-cookie' => $c);
  197:     if ($env{'user.linkedenv'}) {
  198:         my $linked = new CGI::Cookie(-name    => 'lonLinkID',
  199:                                      -value   => '',
  200:                                      -expires => '-10y',);
  201:         $r->headers_out->add('Set-cookie' => $linked);
  202:     }
  203: 
  204:     if ($r->header_only) {
  205: 	$r->send_http_header;
  206: 	return OK;
  207:     }
  208: # -------------------------------------------------------- Menu script and info
  209: 	   
  210: # ---------------------------------------------------------------- Get handover
  211: 
  212:     my ($is_balancer) = &Apache::lonnet::check_loadbalancing($env{'user.name'},$env{'user.domain'});
  213:     my %info=('ip'       => $ENV{'REMOTE_ADDR'},
  214: 	      'domain'   => $env{'user.domain'},
  215: 	      'username' => $env{'user.name'},
  216: 	      'role'     => $env{'form.role'},
  217: 	      'server'   => $r->dir_config('lonHostID'),
  218: 	      'balancer' => $is_balancer);
  219:     if ($env{'form.origurl'}) {
  220:         $info{'origurl'} = $env{'form.origurl'};
  221:     }
  222:     if ($env{'form.symb'}) {
  223:         $info{'symb'} = $env{'form.symb'};
  224:     }
  225:     if ($env{'request.sso.login'}) {
  226: 	$info{'sso.login'} = $env{'request.sso.login'};
  227:     }
  228:     if ($env{'request.sso.reloginserver'}) {
  229:         $info{'sso.reloginserver'} = $env{'request.sso.reloginserver'};
  230:     }
  231:     my $token = &Apache::lonnet::tmpput(\%info,$env{'form.otherserver'});
  232:     my $url =$protocol.'://'.$switch_to.'/adm/login?'.
  233: 	'domain='.$env{'user.domain'}.
  234: 	'&amp;username='.$env{'user.name'}.
  235: 	'&amp;token='.$token;
  236: # --------------------------------------------------------------- Screen Output
  237:     return &do_redirect($r, $url, 0);
  238: }
  239: 
  240: 1;
  241: __END__
  242: 
  243: 
  244: 
  245: 
  246: 
  247: 
  248: 
  249: 

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