File:
[LON-CAPA] /
loncom /
auth /
switchserver.pm
Revision
1.32:
download - view:
text,
annotated -
select for diffs
Sun Sep 29 00:49:24 2013 UTC (10 years, 10 months ago) by
raeburn
Branches:
MAIN
CVS tags:
version_2_11_0_RC3,
version_2_11_0_RC2,
version_2_11_0,
HEAD
- Bug 6675
- Case where $env{'REMOTE_ADDR'} as reported to server selected to host
user session, is different from $env{'REMOTE_ADDR'} as reported to
server which handled original authentication request.
- Domain configuration for a load balancing server can be set to one of
the following, if an IP mismatch is detected by /adm/migrateuser
during credentials checking after redirect via /adm/switchserver
(i) Session will be hosted on Load Balancer
(ii) Session will be hosted on offload server
for each of (a) SSO users from load balancer's domain, (b) non-SSO users
- Setting to host on load balancer will be ignored if switch server was called
by an author or co-author switching to server housing the authoring space.
1: # The LearningOnline Network
2: # Switch Servers Handler
3: #
4: # $Id: switchserver.pm,v 1.32 2013/09/29 00:49:24 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: if ($env{'user.name'} eq 'public'
100: && $env{'user.domain'} eq 'public') {
101: my $url = 'http://'.$switch_to.$r->uri;
102: return &do_redirect($r,$url,1)
103: }
104:
105: my $skip_canhost_check = '';
106: if ($env{'form.role'}) {
107: if (!exists($env{'user.role.'.$env{'form.role'}})) {
108: return FORBIDDEN;
109: } else {
110: my $now = time;
111: my ($start,$end) = split(/\./,$env{'user.role.'.$env{'form.role'}});
112: if (($start && $start > $now) || ($end && $end < $now)) {
113: return FORBIDDEN;
114: } elsif ($env{'form.role'} eq 'au./'.$env{'user.domain'}.'/') {
115: if (&Apache::lonnet::homeserver($env{'user.name'},$env{'user.domain'}) eq $env{'form.otherserver'}) {
116: $skip_canhost_check = 1;
117: }
118: } elsif ($env{'form.role'} =~ m{^[ac]a\./($match_domain)/($match_username)$}) {
119: if (&Apache::lonnet::homeserver($2,$1) eq $env{'form.otherserver'}) {
120: $skip_canhost_check = 1;
121: }
122: }
123: }
124: }
125:
126: unless ($skip_canhost_check) {
127: my $canhost = 1;
128: my $uprimary_id = &Apache::lonnet::domain($env{'user.domain'},'primary');
129: my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
130: my @intdoms;
131: my $internet_names = &Apache::lonnet::get_internet_names($env{'form.otherserver'});
132: if (ref($internet_names) eq 'ARRAY') {
133: @intdoms = @{$internet_names};
134: }
135: unless ($uint_dom ne '' && grep(/^\Q$uint_dom\E$/,@intdoms)) {
136: my $serverhomeID = &Apache::lonnet::get_server_homeID($switch_to);
137: my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);
138: my %defdomdefaults = &Apache::lonnet::get_domain_defaults($serverhomedom);
139: my %udomdefaults = &Apache::lonnet::get_domain_defaults($env{'user.domain'});
140: my $remoterev = &Apache::lonnet::get_server_loncaparev($env{'user.domain'},$env{'form.otherserver'});
141: $canhost =
142: &Apache::lonnet::can_host_session($env{'user.domain'},
143: $env{'form.otherserver'},
144: $remoterev,
145: $udomdefaults{'remotesessions'},
146: $defdomdefaults{'hostedsessions'});
147: }
148: unless ($canhost) { return FORBIDDEN; }
149: }
150:
151: #remove session env, and log event
152: unlink($handle);
153: my %temp=('switchserver' => time.':'.$env{'form.otherserver'},
154: $env{'form.role'});
155: &Apache::lonnet::put('email_status',\%temp);
156: &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
157: $env{'user.home'},
158: "Switch Server to $env{'form.otherserver'} with role $env{'form.role'} $ENV{'REMOTE_ADDR'}");
159:
160: &Apache::loncommon::content_type($r,'text/html');
161:
162: #expire the cookie
163: my $c = new CGI::Cookie(-name => 'lonID',
164: -value => '',
165: -expires => '-10y',);
166: $r->header_out('Set-cookie' => $c);
167:
168: if ($r->header_only) {
169: $r->send_http_header;
170: return OK;
171: }
172: # -------------------------------------------------------- Menu script and info
173:
174: # ---------------------------------------------------------------- Get handover
175:
176: my ($is_balancer) = &Apache::lonnet::check_loadbalancing($env{'user.name'},$env{'user.domain'});
177: my %info=('ip' => $ENV{'REMOTE_ADDR'},
178: 'domain' => $env{'user.domain'},
179: 'username' => $env{'user.name'},
180: 'role' => $env{'form.role'},
181: 'server' => $r->dir_config('lonHostID'),
182: 'balancer' => $is_balancer);
183: if ($env{'form.origurl'}) {
184: $info{'origurl'} = $env{'form.origurl'};
185: }
186: if ($env{'form.symb'}) {
187: $info{'symb'} = $env{'form.symb'};
188: }
189: if ($env{'request.sso.login'}) {
190: $info{'sso.login'} = $env{'request.sso.login'};
191: }
192: if ($env{'request.sso.reloginserver'}) {
193: $info{'sso.reloginserver'} = $env{'request.sso.reloginserver'};
194: }
195: my $token = &Apache::lonnet::tmpput(\%info,$env{'form.otherserver'});
196: my $url ='http://'.$switch_to.'/adm/login?'.
197: 'domain='.$env{'user.domain'}.
198: '&username='.$env{'user.name'}.
199: '&token='.$token;
200: # --------------------------------------------------------------- Screen Output
201: return &do_redirect($r, $url, 0);
202: }
203:
204: 1;
205: __END__
206:
207:
208:
209:
210:
211:
212:
213:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>