Annotation of loncom/auth/migrateuser.pm, revision 1.25.2.5
1.1 albertel 1: # The LearningOnline Network
2: # Starts a user off based of an existing token.
3: #
1.25.2.5! raeburn 4: # $Id: migrateuser.pm,v 1.25.2.4 2021/01/04 03:52:32 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:
1.2 albertel 29: package Apache::migrateuser;
1.1 albertel 30:
31: use strict;
1.19 raeburn 32: use LONCAPA qw(:DEFAULT :match);
1.25.2.5! raeburn 33: use Apache::Constants qw(:common :http :methods :remotehost);
1.1 albertel 34: use Apache::lonauth;
35: use Apache::lonnet;
1.6 albertel 36: use Apache::lonlocal;
1.18 raeburn 37: use Apache::lonlogin();
1.1 albertel 38:
39: sub goto_login {
1.24 raeburn 40: my ($r,$domain) = @_;
1.2 albertel 41: &Apache::loncommon::content_type($r,'text/html');
42: $r->send_http_header;
1.24 raeburn 43: my $url = '/adm/login';
44: if ($domain) {
45: $url .= '?domain='.$domain;
46: }
1.5 albertel 47: $r->print(&Apache::loncommon::start_page('Going to login',undef,
1.24 raeburn 48: {'redirect' => [0,$url],}).
1.5 albertel 49: '<h1>'.&mt('One moment please...').'</h1>'.
50: '<p>'.&mt('Transferring to login page.').'</p>'.
51: &Apache::loncommon::end_page());
1.2 albertel 52: return OK;
1.1 albertel 53: }
54:
1.2 albertel 55:
1.7 albertel 56: sub sso_check {
57: my ($data) = @_;
1.8 albertel 58: my %extra_env;
1.18 raeburn 59: if (ref($data) eq 'HASH') {
60: if ($data->{'sso.login'}) {
61: $extra_env{'request.sso.login'} = $data->{'sso.login'};
62: }
63: if ($data->{'sso.reloginserver'}) {
64: $extra_env{'request.sso.reloginserver'} =
65: $data->{'sso.reloginserver'};
66: }
1.7 albertel 67: }
1.18 raeburn 68: return \%extra_env;
69: }
70:
71: sub ip_changed {
72: my ($r,$udom,$camefrom,$dataref) = @_;
73: &Apache::loncommon::content_type($r,'text/html');
74: $r->send_http_header;
75: if (ref($dataref) eq 'HASH') {
76: my $title = 'LON-CAPA Session redirected';
77: my $message = &mt('Your internet address has changed since you logged in.');
78: my $rule_in_effect;
1.22 raeburn 79: if ($dataref->{'balancer'}) {
80: my $baldom = &Apache::lonnet::host_domain($camefrom);
81: my $balprimaryid = &Apache::lonnet::domain($baldom,'primary');
82: my $balintdom = &Apache::lonnet::internet_dom($balprimaryid);
83: my $uprimaryid = &Apache::lonnet::domain($udom,'primary');
84: my $uintdom = &Apache::lonnet::internet_dom($uprimaryid);
85: my $dom_in_use;
86: if (($uintdom ne '') && ($uintdom eq $balintdom)) {
87: $dom_in_use = $udom;
88: } else {
89: $dom_in_use = $baldom;
1.21 raeburn 90: }
1.22 raeburn 91: my ($result,$cached)=&Apache::lonnet::is_cached_new('loadbalancing',$dom_in_use);
1.18 raeburn 92: unless (defined($cached)) {
93: my $cachetime = 60*60*24;
94: my %domconfig =
1.22 raeburn 95: &Apache::lonnet::get_dom('configuration',['loadbalancing'],$dom_in_use);
1.18 raeburn 96: if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1.22 raeburn 97: $result = &Apache::lonnet::do_cache_new('loadbalancing',$dom_in_use,
1.18 raeburn 98: $domconfig{'loadbalancing'},$cachetime);
99: }
100: }
101: if (ref($result) eq 'HASH') {
102: (undef,my $currtargets,my $currrules) =
103: &Apache::lonnet::check_balancer_result($result,$dataref->{'server'});
104: if (ref($currrules) eq 'HASH') {
105: if ($dataref->{'sso.login'}) {
106: if ($currrules->{'_LC_ipchangesso'} ne '') {
107: $rule_in_effect = $currrules->{'_LC_ipchangesso'};
108: }
109: } else {
110: if ($currrules->{'_LC_ipchange'} ne '') {
111: $rule_in_effect = $currrules->{'_LC_ipchange'};
112: }
113: }
114: }
115: }
116: }
117: my $url;
118: my $lonhost= $r->dir_config('lonHostID');
119: my $switchto = $lonhost;
1.23 raeburn 120: if ($rule_in_effect ne 'offloadedto') {
1.18 raeburn 121: my $hosthere;
1.23 raeburn 122: my @ids=&Apache::lonnet::current_machine_ids();
123: unless ($rule_in_effect eq 'balancer') {
124: if (grep(/^\Q$rule_in_effect\E$/,@ids)) {
125: $hosthere = 1;
126: }
127: }
128: unless ($hosthere) {
129: if ($dataref->{'role'}) {
130: my ($adom,$aname);
131: if ($dataref->{'role'} =~ m{^au\./($match_domain)/$}) {
132: $adom = $1;
133: $aname = $dataref->{'username'};
134: } elsif ($dataref->{'role'} =~ m{^(?:ca|aa)\./($match_domain)/($match_username)$}) {
135: $adom = $1;
136: $aname = $2;
137: }
138: if ($adom ne '' && $aname ne '') {
139: my $ahome = &Apache::lonnet::homeserver($aname,$adom);
140: unless ($ahome eq 'no_host') {
141: if ($ahome && grep(/^\Q$ahome\E$/,@ids)) {
142: $hosthere = 1;
143: }
1.18 raeburn 144: }
145: }
146: }
147: }
1.23 raeburn 148: unless ($hosthere) {
149: my $hostname;
150: if ($rule_in_effect eq 'balancer') {
151: $hostname = &Apache::lonnet::hostname($dataref->{'server'});
152: if ($hostname) {
153: $switchto = $dataref->{'server'};
154: }
155: } else {
156: $hostname = &Apache::lonnet::hostname($rule_in_effect);
157: if ($hostname) {
158: $switchto = $rule_in_effect;
159: }
160: }
1.18 raeburn 161: if ($hostname) {
162: my $protocol = $Apache::lonnet::protocol{$switchto};
163: $protocol = 'http' if ($protocol ne 'https');
1.25.2.5! raeburn 164: my $alias = &Apache::lonnet::use_proxy_alias($r,$switchto);
! 165: $hostname = $alias if ($alias ne '');
1.18 raeburn 166: $url = $protocol.'://'.$hostname;
1.23 raeburn 167: if ($rule_in_effect eq 'balancer') {
168: $message .= '<br />'.
169: &mt('As a result, your LON-CAPA session is being redirected to the server where you originally logged in.');
170: } else {
171: $message .= '<br />'.
172: &mt('As a result, your LON-CAPA session is being redirected.');
173: }
1.18 raeburn 174: }
175: }
1.25.2.1 raeburn 176: unless ($hosthere) {
177: if (($dataref->{'balancer'}) && ($dataref->{'balcookie'})) {
178: &Apache::lonnet::delbalcookie($dataref->{'balcookie'},$dataref->{'balancer'});
179: }
180: }
1.18 raeburn 181: }
182: if ($dataref->{'sso.login'}) {
1.21 raeburn 183: $url .= '/adm/roles';
1.18 raeburn 184: } else {
1.21 raeburn 185: $url .= '/adm/login';
1.24 raeburn 186: if ($udom) {
187: $url .= '?domain='.$udom;
188: }
1.20 raeburn 189: $message .= '<br />'.&mt('You will need to provide your password one more time.');
1.18 raeburn 190: }
191: my %info= (
1.24 raeburn 192: 'domain' => $udom,
1.18 raeburn 193: 'username' => $dataref->{'username'},
194: 'sessionserver' => $lonhost,
195: );
1.25.2.5! raeburn 196: my @names = ('origurl','symb','role');
! 197: foreach my $name (@names) {
! 198: if ($dataref->{$name} ne '') {
! 199: $info{$name} = $dataref->{$name};
! 200: }
1.18 raeburn 201: }
1.25.2.5! raeburn 202: my $iptoken = &Apache::lonnet::tmpput(\%info,$switchto,'link');
1.18 raeburn 203: unless ($iptoken eq 'conlost') {
1.25.2.5! raeburn 204: $url .= (($url =~ /\?/) ? '&' : '?') . 'iptoken='.$iptoken;
1.18 raeburn 205: }
206: $r->print(&Apache::loncommon::start_page($title,undef,
207: {'redirect' =>
208: [2,$url],}).
209: '<h1>'.&mt('One moment please...').'</h1>'.
210: '<p class="LC_warning">'.$message.'</p>'.
211: &Apache::loncommon::end_page());
212: } else {
213: return &goto_login($r);
1.10 raeburn 214: }
1.18 raeburn 215: return OK;
1.7 albertel 216: }
217:
1.1 albertel 218: sub handler {
219: my ($r) = @_;
220:
221: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['token']);
1.4 albertel 222: my %data = &Apache::lonnet::tmpget($env{'form.token'});
1.13 raeburn 223: if (keys(%data) == 0) {
224: return &goto_login($r);
225: }
1.4 albertel 226: my $delete = &Apache::lonnet::tmpdel($env{'form.token'});
227:
1.6 albertel 228: &Apache::lonlocal::get_language_handle($r);
229:
1.4 albertel 230: if ($delete ne 'ok') {
231: return &goto_login($r);
232: }
1.2 albertel 233:
1.18 raeburn 234: if (!defined($data{'username'}) || !defined($data{'domain'})) {
235: return &goto_login($r);
236: }
1.25.2.5! raeburn 237: my $ip = &Apache::lonnet::get_requestor_ip($r,REMOTE_NOLOOKUP);
1.25.2.4 raeburn 238: if ($data{'ip'} ne $ip) {
1.24 raeburn 239: &Apache::lonnet::logthis('IP change when session migration requested -- was: '.
1.25.2.4 raeburn 240: $data{'ip'}.'; now: '.$ip.' for '.$data{'username'}.':'.$data{'domain'});
1.18 raeburn 241: return &ip_changed($r,$data{'domain'},$data{'server'},\%data);
1.2 albertel 242: }
243:
1.17 raeburn 244: &Apache::lonnet::logthis("Allowing access for $data{'username'}:$data{'domain'} to $data{'role'}");
1.2 albertel 245: my $home=&Apache::lonnet::homeserver($data{'username'},$data{'domain'});
1.24 raeburn 246: my $udom;
247: if (&Apache::lonnet::domain($data{'domain'})) {
248: $udom=$data{'domain'};
249: }
250: if ($home =~ /(con_lost|no_such_host)/) { return &goto_login($r,$udom); }
1.2 albertel 251:
1.8 albertel 252: my $extra_env = &sso_check(\%data);
1.25.2.1 raeburn 253: if (($data{'balancer'}) && ($data{'server'}) && ($data{'balcookie'})) {
254: if (ref($extra_env) eq 'HASH') {
255: $extra_env->{'request.balancercookie'} = $data{'server'}.':'.$data{'balcookie'};
256: } else {
257: $extra_env = { 'request.balancercookie' => $data{'server'}.':'.$data{'balcookie'} };
258: }
1.25.2.3 raeburn 259: } elsif (($data{'server'}) && ($data{'otherbalcookie'})) {
260: my ($balancer,$balcookie) = split(/:/,$data{'otherbalcookie'});
261: if (defined(&Apache::lonnet::hostname($balancer)) && $balcookie =~ /^[a-f0-9]{32}$/) {
262: my $baldom = &Apache::lonnet::host_domain($balancer);
263: if (&Apache::lonnet::shared_institution($baldom)) {
264: my $cookieid = join('_',$udom,$data{'username'},$balcookie);
265: &Apache::lonnet::updatebalcookie($cookieid,$balancer,$data{'server'});
266: if (ref($extra_env) eq 'HASH') {
267: $extra_env->{'request.balancercookie'} = $data{'otherbalcookie'};
268: } else {
269: $extra_env = { 'request.balancercookie' => $data{'otherbalcookie'} };
270: }
271: }
272: }
1.25.2.1 raeburn 273: }
1.8 albertel 274:
1.25.2.2 raeburn 275: my (%form,$cid);
1.16 raeburn 276: if ($data{'symb'} ne '') {
277: $form{'symb'} = $data{'symb'};
278: }
1.21 raeburn 279: if ($data{'iptoken'} ne '') {
280: $form{'iptoken'} = $data{'iptoken'};
281: }
1.25 raeburn 282: if ($data{'noloadbalance'} ne '') {
283: $form{'noloadbalance'} = $data{'noloadbalance'};
284: }
1.25.2.2 raeburn 285: if ($data{'role'}) {
286: if ($data{'role'} =~ m{\./($match_domain)/($match_courseid)(?:/\w+|$)}) {
287: unless (&Apache::lonnet::homeserver($2,$1) eq 'no_host') {
288: $cid = $1.'_'.$2;
289: }
290: }
291: } else {
1.25.2.5! raeburn 292: my ($handle,$expirepub);
! 293: $handle = &Apache::lonnet::check_for_valid_session($r);
! 294:
! 295: # For "public user" - remove any existing "public" cookie so actual user is logged in.
! 296: if ($handle) {
! 297: if ($handle=~/^publicuser\_/) {
! 298: my $lonidsdir=$r->dir_config('lonIDsDir');
! 299: if ($lonidsdir ne '') {
! 300: unlink("$lonidsdir/$handle.id");
! 301: }
! 302: undef($handle);
! 303: $expirepub = 1;
! 304: }
! 305: }
1.12 albertel 306: if ($handle) {
1.11 albertel 307: &Apache::lonnet::transfer_profile_to_env($r->dir_config('lonIDsDir'),
308: $handle);
1.14 raeburn 309: if ($data{'origurl'} ne '') {
310: $r->internal_redirect($data{'origurl'});
311: } elsif ($env{'request.course.id'}) {
312: $r->internal_redirect('/adm/navmaps');
1.11 albertel 313: } else {
314: $r->internal_redirect('/adm/roles');
315: }
316: } else {
1.14 raeburn 317: my $desturl = '/adm/roles';
318: if ($data{'origurl'} ne '') {
319: $desturl = $data{'origurl'};
320: }
1.11 albertel 321: &Apache::lonauth::success($r,$data{'username'},$data{'domain'},
1.25.2.5! raeburn 322: $home,$desturl,$extra_env,\%form,'',
! 323: $expirepub);
1.11 albertel 324:
325: }
1.1 albertel 326: return OK;
1.11 albertel 327:
1.1 albertel 328: }
1.6 albertel 329:
330: my $next_url='/adm/roles?selectrole=1&'.&escape($data{'role'}).'=1';
1.15 raeburn 331: if ($data{'origurl'} ne '') {
332: $next_url .= '&orgurl='.&escape($data{'origurl'});
333: }
1.6 albertel 334: &Apache::lonauth::success($r,$data{'username'},$data{'domain'},$home,
1.25.2.2 raeburn 335: $next_url,$extra_env,\%form,$cid);
1.6 albertel 336: return OK;
1.1 albertel 337: }
338:
339: 1;
340: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>