Annotation of loncom/auth/switchserver.pm, revision 1.36
1.1 albertel 1: # The LearningOnline Network
2: # Switch Servers Handler
3: #
1.36 ! 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;
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.14 albertel 62: 'only_body' => $only_body,});
63: my $end_page = &Apache::loncommon::end_page();
64: $r->print($start_page.$extra_text.$end_page);
1.30 raeburn 65: unless ($env{'user.name'} eq 'public' && ($env{'user.domain'} eq 'public')) {
66: $r->register_cleanup(\&flush_course_logs);
67: }
1.14 albertel 68: return OK;
1.30 raeburn 69: }
1.14 albertel 70:
1.30 raeburn 71: sub flush_course_logs {
72: &Apache::lonnet::flushcourselogs();
73: return OK;
1.14 albertel 74: }
75:
1.1 albertel 76: sub handler {
1.5 albertel 77: my ($r) = @_;
1.1 albertel 78:
79: my $handle=&init_env($r);
80: if (!defined($handle)) { return FORBIDDEN; }
81:
82: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.22 raeburn 83: ['otherserver','role','origurl','symb']);
1.5 albertel 84:
1.13 albertel 85: my $switch_to=&Apache::lonnet::hostname($env{'form.otherserver'});
1.17 albertel 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: }
1.13 albertel 94: $switch_to=&Apache::lonnet::hostname($env{'form.otherserver'});
1.4 albertel 95: }
1.5 albertel 96:
1.1 albertel 97: if (!defined($switch_to)) { return FORBIDDEN; }
1.4 albertel 98:
1.33 raeburn 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:
1.14 albertel 106: if ($env{'user.name'} eq 'public'
107: && $env{'user.domain'} eq 'public') {
1.33 raeburn 108: my $url = $protocol.'://'.$switch_to.$r->uri;
1.36 ! raeburn 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);
1.14 albertel 115: return &do_redirect($r,$url,1)
116: }
117:
1.28 raeburn 118: my $skip_canhost_check = '';
119: if ($env{'form.role'}) {
120: if (!exists($env{'user.role.'.$env{'form.role'}})) {
1.35 raeburn 121: delete($env{'form.role'});
1.28 raeburn 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)) {
1.35 raeburn 126: delete($env{'form.role'});
1.28 raeburn 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: }
1.31 raeburn 131: } elsif ($env{'form.role'} =~ m{^[ac]a\./($match_domain)/($match_username)$}) {
1.28 raeburn 132: if (&Apache::lonnet::homeserver($2,$1) eq $env{'form.otherserver'}) {
133: $skip_canhost_check = 1;
134: }
135: }
136: }
137: }
138:
139: unless ($skip_canhost_check) {
140: my $canhost = 1;
141: my $uprimary_id = &Apache::lonnet::domain($env{'user.domain'},'primary');
142: my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
1.29 raeburn 143: my @intdoms;
144: my $internet_names = &Apache::lonnet::get_internet_names($env{'form.otherserver'});
145: if (ref($internet_names) eq 'ARRAY') {
146: @intdoms = @{$internet_names};
147: }
1.28 raeburn 148: unless ($uint_dom ne '' && grep(/^\Q$uint_dom\E$/,@intdoms)) {
149: my $serverhomeID = &Apache::lonnet::get_server_homeID($switch_to);
150: my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);
151: my %defdomdefaults = &Apache::lonnet::get_domain_defaults($serverhomedom);
152: my %udomdefaults = &Apache::lonnet::get_domain_defaults($env{'user.domain'});
153: my $remoterev = &Apache::lonnet::get_server_loncaparev($env{'user.domain'},$env{'form.otherserver'});
154: $canhost =
155: &Apache::lonnet::can_host_session($env{'user.domain'},
156: $env{'form.otherserver'},
157: $remoterev,
158: $udomdefaults{'remotesessions'},
159: $defdomdefaults{'hostedsessions'});
160: }
161: unless ($canhost) { return FORBIDDEN; }
1.26 raeburn 162: }
163:
1.1 albertel 164: #remove session env, and log event
1.5 albertel 165: unlink($handle);
1.36 ! raeburn 166: if ($env{'user.linkedenv'} ne '') {
! 167: my $lonidsdir=$r->dir_config('lonIDsDir');
! 168: if ((-l $env{'user.linkedenv'}) &&
! 169: (readlink($env{'user.linkedenv'}) eq "$lonidsdir/$handle.id")) {
! 170: unlink($env{'user.linkedenv'});
! 171: }
! 172: }
1.1 albertel 173: my %temp=('switchserver' => time.':'.$env{'form.otherserver'},
174: $env{'form.role'});
175: &Apache::lonnet::put('email_status',\%temp);
1.34 raeburn 176: my $logmsg = "Switch Server to $env{'form.otherserver'}";
177: if ($env{'form.role'}) {
178: $logmsg .= " with role: $env{'form.role'}";
179: } else {
180: $logmsg .= " (no role)";
181: }
182: $logmsg .= ' '.$ENV{'REMOTE_ADDR'};
1.1 albertel 183: &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
1.34 raeburn 184: $env{'user.home'},$logmsg);
1.1 albertel 185:
186: &Apache::loncommon::content_type($r,'text/html');
1.12 albertel 187:
188: #expire the cookie
189: my $c = new CGI::Cookie(-name => 'lonID',
190: -value => '',
191: -expires => '-10y',);
1.36 ! raeburn 192: $r->headers_out->add('Set-cookie' => $c);
! 193: if ($env{'user.linkedenv'}) {
! 194: my $linked = new CGI::Cookie(-name => 'lonLinkID',
! 195: -value => '',
! 196: -expires => '-10y',);
! 197: $r->headers_out->add('Set-cookie' => $linked);
! 198: }
1.20 albertel 199:
200: if ($r->header_only) {
201: $r->send_http_header;
202: return OK;
203: }
1.1 albertel 204: # -------------------------------------------------------- Menu script and info
205:
206: # ---------------------------------------------------------------- Get handover
207:
1.32 raeburn 208: my ($is_balancer) = &Apache::lonnet::check_loadbalancing($env{'user.name'},$env{'user.domain'});
1.3 albertel 209: my %info=('ip' => $ENV{'REMOTE_ADDR'},
210: 'domain' => $env{'user.domain'},
211: 'username' => $env{'user.name'},
212: 'role' => $env{'form.role'},
1.32 raeburn 213: 'server' => $r->dir_config('lonHostID'),
214: 'balancer' => $is_balancer);
1.22 raeburn 215: if ($env{'form.origurl'}) {
216: $info{'origurl'} = $env{'form.origurl'};
217: }
218: if ($env{'form.symb'}) {
219: $info{'symb'} = $env{'form.symb'};
220: }
1.8 albertel 221: if ($env{'request.sso.login'}) {
222: $info{'sso.login'} = $env{'request.sso.login'};
223: }
1.11 raeburn 224: if ($env{'request.sso.reloginserver'}) {
225: $info{'sso.reloginserver'} = $env{'request.sso.reloginserver'};
226: }
1.3 albertel 227: my $token = &Apache::lonnet::tmpput(\%info,$env{'form.otherserver'});
1.33 raeburn 228: my $url =$protocol.'://'.$switch_to.'/adm/login?'.
1.14 albertel 229: 'domain='.$env{'user.domain'}.
1.21 albertel 230: '&username='.$env{'user.name'}.
231: '&token='.$token;
1.1 albertel 232: # --------------------------------------------------------------- Screen Output
1.24 droeschl 233: return &do_redirect($r, $url, 0);
1.1 albertel 234: }
235:
236: 1;
237: __END__
238:
239:
240:
241:
242:
243:
244:
245:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>