Annotation of loncom/auth/migrateuser.pm, revision 1.25.2.5.2.1
1.1 albertel 1: # The LearningOnline Network
2: # Starts a user off based of an existing token.
3: #
1.25.2.5.2.1! raeburn 4: # $Id: migrateuser.pm,v 1.25.2.5 2021/12/12 18:34:48 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.7 albertel 55: sub sso_check {
56: my ($data) = @_;
1.8 albertel 57: my %extra_env;
1.18 raeburn 58: if (ref($data) eq 'HASH') {
59: if ($data->{'sso.login'}) {
60: $extra_env{'request.sso.login'} = $data->{'sso.login'};
61: }
62: if ($data->{'sso.reloginserver'}) {
63: $extra_env{'request.sso.reloginserver'} =
64: $data->{'sso.reloginserver'};
65: }
1.7 albertel 66: }
1.18 raeburn 67: return \%extra_env;
68: }
69:
70: sub ip_changed {
71: my ($r,$udom,$camefrom,$dataref) = @_;
72: &Apache::loncommon::content_type($r,'text/html');
73: $r->send_http_header;
74: if (ref($dataref) eq 'HASH') {
75: my $title = 'LON-CAPA Session redirected';
76: my $message = &mt('Your internet address has changed since you logged in.');
77: my $rule_in_effect;
1.22 raeburn 78: if ($dataref->{'balancer'}) {
79: my $baldom = &Apache::lonnet::host_domain($camefrom);
80: my $balprimaryid = &Apache::lonnet::domain($baldom,'primary');
81: my $balintdom = &Apache::lonnet::internet_dom($balprimaryid);
82: my $uprimaryid = &Apache::lonnet::domain($udom,'primary');
83: my $uintdom = &Apache::lonnet::internet_dom($uprimaryid);
84: my $dom_in_use;
85: if (($uintdom ne '') && ($uintdom eq $balintdom)) {
86: $dom_in_use = $udom;
87: } else {
88: $dom_in_use = $baldom;
1.21 raeburn 89: }
1.22 raeburn 90: my ($result,$cached)=&Apache::lonnet::is_cached_new('loadbalancing',$dom_in_use);
1.18 raeburn 91: unless (defined($cached)) {
92: my $cachetime = 60*60*24;
93: my %domconfig =
1.22 raeburn 94: &Apache::lonnet::get_dom('configuration',['loadbalancing'],$dom_in_use);
1.18 raeburn 95: if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1.22 raeburn 96: $result = &Apache::lonnet::do_cache_new('loadbalancing',$dom_in_use,
1.18 raeburn 97: $domconfig{'loadbalancing'},$cachetime);
98: }
99: }
100: if (ref($result) eq 'HASH') {
101: (undef,my $currtargets,my $currrules) =
102: &Apache::lonnet::check_balancer_result($result,$dataref->{'server'});
103: if (ref($currrules) eq 'HASH') {
104: if ($dataref->{'sso.login'}) {
105: if ($currrules->{'_LC_ipchangesso'} ne '') {
106: $rule_in_effect = $currrules->{'_LC_ipchangesso'};
107: }
108: } else {
109: if ($currrules->{'_LC_ipchange'} ne '') {
110: $rule_in_effect = $currrules->{'_LC_ipchange'};
111: }
112: }
113: }
114: }
115: }
116: my $url;
117: my $lonhost= $r->dir_config('lonHostID');
118: my $switchto = $lonhost;
1.23 raeburn 119: if ($rule_in_effect ne 'offloadedto') {
1.18 raeburn 120: my $hosthere;
1.23 raeburn 121: my @ids=&Apache::lonnet::current_machine_ids();
122: unless ($rule_in_effect eq 'balancer') {
123: if (grep(/^\Q$rule_in_effect\E$/,@ids)) {
124: $hosthere = 1;
125: }
126: }
127: unless ($hosthere) {
128: if ($dataref->{'role'}) {
129: my ($adom,$aname);
130: if ($dataref->{'role'} =~ m{^au\./($match_domain)/$}) {
131: $adom = $1;
132: $aname = $dataref->{'username'};
133: } elsif ($dataref->{'role'} =~ m{^(?:ca|aa)\./($match_domain)/($match_username)$}) {
134: $adom = $1;
135: $aname = $2;
136: }
137: if ($adom ne '' && $aname ne '') {
138: my $ahome = &Apache::lonnet::homeserver($aname,$adom);
139: unless ($ahome eq 'no_host') {
140: if ($ahome && grep(/^\Q$ahome\E$/,@ids)) {
141: $hosthere = 1;
142: }
1.18 raeburn 143: }
144: }
145: }
146: }
1.23 raeburn 147: unless ($hosthere) {
148: my $hostname;
149: if ($rule_in_effect eq 'balancer') {
150: $hostname = &Apache::lonnet::hostname($dataref->{'server'});
151: if ($hostname) {
152: $switchto = $dataref->{'server'};
153: }
154: } else {
155: $hostname = &Apache::lonnet::hostname($rule_in_effect);
156: if ($hostname) {
157: $switchto = $rule_in_effect;
158: }
159: }
1.18 raeburn 160: if ($hostname) {
161: my $protocol = $Apache::lonnet::protocol{$switchto};
162: $protocol = 'http' if ($protocol ne 'https');
1.25.2.5 raeburn 163: my $alias = &Apache::lonnet::use_proxy_alias($r,$switchto);
164: $hostname = $alias if ($alias ne '');
1.18 raeburn 165: $url = $protocol.'://'.$hostname;
1.23 raeburn 166: if ($rule_in_effect eq 'balancer') {
167: $message .= '<br />'.
168: &mt('As a result, your LON-CAPA session is being redirected to the server where you originally logged in.');
169: } else {
170: $message .= '<br />'.
171: &mt('As a result, your LON-CAPA session is being redirected.');
172: }
1.18 raeburn 173: }
174: }
1.25.2.1 raeburn 175: unless ($hosthere) {
176: if (($dataref->{'balancer'}) && ($dataref->{'balcookie'})) {
177: &Apache::lonnet::delbalcookie($dataref->{'balcookie'},$dataref->{'balancer'});
178: }
179: }
1.18 raeburn 180: }
181: if ($dataref->{'sso.login'}) {
1.21 raeburn 182: $url .= '/adm/roles';
1.18 raeburn 183: } else {
1.21 raeburn 184: $url .= '/adm/login';
1.24 raeburn 185: if ($udom) {
186: $url .= '?domain='.$udom;
187: }
1.20 raeburn 188: $message .= '<br />'.&mt('You will need to provide your password one more time.');
1.18 raeburn 189: }
190: my %info= (
1.24 raeburn 191: 'domain' => $udom,
1.18 raeburn 192: 'username' => $dataref->{'username'},
193: 'sessionserver' => $lonhost,
194: );
1.25.2.5.2.1! raeburn 195: my @names = ('origurl','symb','role','linkprot','linkkey');
1.25.2.5 raeburn 196: foreach my $name (@names) {
197: if ($dataref->{$name} ne '') {
198: $info{$name} = $dataref->{$name};
199: }
1.18 raeburn 200: }
1.25.2.5 raeburn 201: my $iptoken = &Apache::lonnet::tmpput(\%info,$switchto,'link');
1.18 raeburn 202: unless ($iptoken eq 'conlost') {
1.25.2.5 raeburn 203: $url .= (($url =~ /\?/) ? '&' : '?') . 'iptoken='.$iptoken;
1.18 raeburn 204: }
205: $r->print(&Apache::loncommon::start_page($title,undef,
206: {'redirect' =>
207: [2,$url],}).
208: '<h1>'.&mt('One moment please...').'</h1>'.
209: '<p class="LC_warning">'.$message.'</p>'.
210: &Apache::loncommon::end_page());
211: } else {
212: return &goto_login($r);
1.10 raeburn 213: }
1.18 raeburn 214: return OK;
1.7 albertel 215: }
216:
1.1 albertel 217: sub handler {
218: my ($r) = @_;
1.25.2.5.2.1! raeburn 219:
1.1 albertel 220: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['token']);
1.4 albertel 221: my %data = &Apache::lonnet::tmpget($env{'form.token'});
1.13 raeburn 222: if (keys(%data) == 0) {
223: return &goto_login($r);
224: }
1.4 albertel 225: my $delete = &Apache::lonnet::tmpdel($env{'form.token'});
226:
1.6 albertel 227: &Apache::lonlocal::get_language_handle($r);
228:
1.4 albertel 229: if ($delete ne 'ok') {
230: return &goto_login($r);
231: }
1.2 albertel 232:
1.18 raeburn 233: if (!defined($data{'username'}) || !defined($data{'domain'})) {
234: return &goto_login($r);
235: }
1.25.2.5 raeburn 236: my $ip = &Apache::lonnet::get_requestor_ip($r,REMOTE_NOLOOKUP);
1.25.2.4 raeburn 237: if ($data{'ip'} ne $ip) {
1.24 raeburn 238: &Apache::lonnet::logthis('IP change when session migration requested -- was: '.
1.25.2.4 raeburn 239: $data{'ip'}.'; now: '.$ip.' for '.$data{'username'}.':'.$data{'domain'});
1.18 raeburn 240: return &ip_changed($r,$data{'domain'},$data{'server'},\%data);
1.2 albertel 241: }
242:
1.17 raeburn 243: &Apache::lonnet::logthis("Allowing access for $data{'username'}:$data{'domain'} to $data{'role'}");
1.2 albertel 244: my $home=&Apache::lonnet::homeserver($data{'username'},$data{'domain'});
1.24 raeburn 245: my $udom;
246: if (&Apache::lonnet::domain($data{'domain'})) {
247: $udom=$data{'domain'};
248: }
249: if ($home =~ /(con_lost|no_such_host)/) { return &goto_login($r,$udom); }
1.2 albertel 250:
1.8 albertel 251: my $extra_env = &sso_check(\%data);
1.25.2.1 raeburn 252: if (($data{'balancer'}) && ($data{'server'}) && ($data{'balcookie'})) {
253: if (ref($extra_env) eq 'HASH') {
254: $extra_env->{'request.balancercookie'} = $data{'server'}.':'.$data{'balcookie'};
255: } else {
256: $extra_env = { 'request.balancercookie' => $data{'server'}.':'.$data{'balcookie'} };
257: }
1.25.2.3 raeburn 258: } elsif (($data{'server'}) && ($data{'otherbalcookie'})) {
259: my ($balancer,$balcookie) = split(/:/,$data{'otherbalcookie'});
260: if (defined(&Apache::lonnet::hostname($balancer)) && $balcookie =~ /^[a-f0-9]{32}$/) {
261: my $baldom = &Apache::lonnet::host_domain($balancer);
262: if (&Apache::lonnet::shared_institution($baldom)) {
263: my $cookieid = join('_',$udom,$data{'username'},$balcookie);
264: &Apache::lonnet::updatebalcookie($cookieid,$balancer,$data{'server'});
265: if (ref($extra_env) eq 'HASH') {
266: $extra_env->{'request.balancercookie'} = $data{'otherbalcookie'};
267: } else {
268: $extra_env = { 'request.balancercookie' => $data{'otherbalcookie'} };
269: }
270: }
271: }
1.25.2.1 raeburn 272: }
1.8 albertel 273:
1.25.2.2 raeburn 274: my (%form,$cid);
1.16 raeburn 275: if ($data{'symb'} ne '') {
276: $form{'symb'} = $data{'symb'};
277: }
1.21 raeburn 278: if ($data{'iptoken'} ne '') {
279: $form{'iptoken'} = $data{'iptoken'};
280: }
1.25 raeburn 281: if ($data{'noloadbalance'} ne '') {
282: $form{'noloadbalance'} = $data{'noloadbalance'};
283: }
1.25.2.2 raeburn 284: if ($data{'role'}) {
285: if ($data{'role'} =~ m{\./($match_domain)/($match_courseid)(?:/\w+|$)}) {
286: unless (&Apache::lonnet::homeserver($2,$1) eq 'no_host') {
287: $cid = $1.'_'.$2;
288: }
289: }
290: } else {
1.25.2.5 raeburn 291: my ($handle,$expirepub);
1.25.2.5.2.1! raeburn 292: $handle = &Apache::lonnet::check_for_valid_session($r);
1.25.2.5 raeburn 293:
294: # For "public user" - remove any existing "public" cookie so actual user is logged in.
295: if ($handle) {
296: if ($handle=~/^publicuser\_/) {
297: my $lonidsdir=$r->dir_config('lonIDsDir');
298: if ($lonidsdir ne '') {
299: unlink("$lonidsdir/$handle.id");
300: }
301: undef($handle);
302: $expirepub = 1;
303: }
304: }
1.12 albertel 305: if ($handle) {
1.11 albertel 306: &Apache::lonnet::transfer_profile_to_env($r->dir_config('lonIDsDir'),
307: $handle);
1.25.2.5.2.1! raeburn 308: my $checklaunch;
! 309: if ($data{'origurl'} =~ m{^/tiny/$match_domain/\w+$}) {
! 310: if ($env{'request.linkprot'} ne '') {
! 311: unless ($env{'request.linkprot'} eq $data{'linkprot'}) {
! 312: $checklaunch = 1;
! 313: }
! 314: }
! 315: if ($env{'request.linkkey'} ne '') {
! 316: unless ($env{'request.linkkey'} eq $data{'linkkey'}) {
! 317: $checklaunch = 1;
! 318: }
! 319: }
! 320: if ($env{'request.deeplink.login'}) {
! 321: unless ($env{'request.deeplink.login'} eq $data{'deeplink.login'}) {
! 322: $checklaunch = 1;
! 323: }
! 324: }
! 325: }
! 326: if ($data{'linkprot'} ne '') {
! 327: &Apache::lonnet::appenv({'request.linkprot' => $data{'linkprot'}});
! 328: if ($env{'request.linkkey'}) {
! 329: &Apache::lonnet::delenv('request.linkkey');
! 330: }
! 331: my ($linkprotector,$deeplink) = split(/:/,$data{'linkprot'},2);
! 332: if ($env{'user.linkprotector'}) {
! 333: my @protectors = split(/,/,$env{'user.linkprotector'});
! 334: unless (grep(/^\Q$linkprotector\E$/,@protectors)) {
! 335: push(@protectors,$linkprotector);
! 336: @protectors = sort { $a <=> $b } @protectors;
! 337: &Apache::lonnet::appenv({'user.linkprotector' => join(',',@protectors)});
! 338: }
! 339: } else {
! 340: &Apache::lonnet::appenv({'user.linkprotector' => $linkprotector });
! 341: }
! 342: if ($env{'user.linkproturi'}) {
! 343: my @proturis = split(/,/,$env{'user.linkproturi'});
! 344: unless(grep(/^\Q$deeplink\E$/,@proturis)) {
! 345: push(@proturis,$deeplink);
! 346: @proturis = sort @proturis;
! 347: &Apache::lonnet::appenv({'user.linkproturi' => join(',',@proturis)});
! 348: }
! 349: } else {
! 350: &Apache::lonnet::appenv({'user.linkproturi' => $deeplink});
! 351: }
! 352: } elsif ($data{'linkkey'} ne '') {
! 353: &Apache::lonnet::appenv({'request.linkkey' => $data{'linkkey'}});
! 354: if ($env{'request.linkprot'}) {
! 355: &Apache::lonnet::delenv('request.linkprot');
! 356: }
! 357: my $deeplink = $data{'deeplink.login'};
! 358: my $linkkey = $data{'linkkey'};
! 359: if ($env{'user.deeplinkkey'} ne '') {
! 360: my @linkkeys = split(/,/,$env{'user.deeplinkkey'});
! 361: unless (grep(/^\Q$linkkey\E$/,@linkkeys)) {
! 362: push(@linkkeys,$linkkey);
! 363: &Apache::lonnet::appenv({'user.deeplinkkey' => join(',',sort(@linkkeys))});
! 364: }
! 365: } else {
! 366: &Apache::lonnet::appenv({'user.deeplinkkey' => $linkkey});
! 367: }
! 368: if ($deeplink) {
! 369: if ($env{'user.keyedlinkuri'}) {
! 370: my @keyeduris = split(/,/,$env{'user.keyedlinkuri'});
! 371: unless (grep(/^\Q$deeplink\E$/,@keyeduris)) {
! 372: push(@keyeduris,$deeplink);
! 373: &Apache::lonnet::appenv({'user.keyedlinkuri' => join(',',sort(@keyeduris))});
! 374: }
! 375: } else {
! 376: &Apache::lonnet::appenv({'user.keyedlinkuri' => $deeplink});
! 377: }
! 378: }
! 379: }
! 380: if ($data{'deeplink.login'}) {
! 381: &Apache::lonnet::appenv({'request.deeplink.login' => $data{'deeplink.login'}});
! 382: }
1.14 raeburn 383: if ($data{'origurl'} ne '') {
1.25.2.5.2.1! raeburn 384: my $dest = $data{'origurl'};
! 385: if (($env{'request.deeplink.login'} eq $data{'origurl'}) &&
! 386: (($env{'request.linkprot'}) || ($env{'request.linkkey'} ne ''))) {
! 387: my %info;
! 388: if ($env{'request.linkprot'}) {
! 389: $info{'linkprot'} = $env{'request.linkprot'};
! 390: } elsif ($env{'request.linkkey'} ne '') {
! 391: $info{'linkkey'} = $env{'request.linkkey'};
! 392: }
! 393: $info{'origurl'} = $data{'origurl'};
! 394: if ($checklaunch) {
! 395: $info{'checklaunch'} = 1;
! 396: }
! 397: my $token = &Apache::lonnet::tmpput(\%info,$r->dir_config('lonHostID'),'link');
! 398: unless (($token eq 'con_lost') || ($token eq 'refused') ||
! 399: ($token eq 'unknown_cmd') || ($token eq 'no_such_host')) {
! 400: $dest .= (($dest =~ /\?/) ? '&' : '?') . 'ttoken='.$token;
! 401: }
! 402: }
! 403: $r->internal_redirect($dest);
1.14 raeburn 404: } elsif ($env{'request.course.id'}) {
405: $r->internal_redirect('/adm/navmaps');
1.11 albertel 406: } else {
407: $r->internal_redirect('/adm/roles');
408: }
409: } else {
1.14 raeburn 410: my $desturl = '/adm/roles';
411: if ($data{'origurl'} ne '') {
412: $desturl = $data{'origurl'};
1.25.2.5.2.1! raeburn 413: if ($data{'linkprot'}) {
! 414: my ($linkprotector,$linkuri) = split(/:/,$data{'linkprot'},2);
! 415: if ($linkprotector) {
! 416: if (ref($extra_env) eq 'HASH') {
! 417: $extra_env->{'user.linkprotector'} = $linkprotector;
! 418: $extra_env->{'user.linkproturi'} = $linkuri;
! 419: $extra_env->{'request.linkprot'} = $data{'linkprot'};
! 420: } else {
! 421: $extra_env = {'user.linkprotector' => $linkprotector,
! 422: 'user.linkproturi' => $linkuri,
! 423: 'request.linkprot' => $data{'linkprot'}};
! 424: }
! 425: }
! 426: } elsif ($data{'linkkey'}) {
! 427: if (ref($extra_env) eq 'HASH') {
! 428: $extra_env->{'user.deeplinkkey'} = $data{'linkkey'};
! 429: $extra_env->{'user.keyedlinkuri'} = $data{'deeplink.login'};
! 430: $extra_env->{'request.linkkey'} = $data{'linkkey'};
! 431: } else {
! 432: $extra_env = {'user.deeplinkkey' => $data{'linkkey'},
! 433: 'user.keyedlinkuri' => $data{'deeplink.login'},
! 434: 'request.linkkey' => $data{'linkkey'}};
! 435: }
! 436: }
! 437: if ($data{'deeplink.login'}) {
! 438: if (ref($extra_env) eq 'HASH') {
! 439: $extra_env->{'request.deeplink.login'} = $data{'deeplink.login'};
! 440: } else {
! 441: $extra_env = {'request.deeplink.login' => $data{'deeplink.login'}};
! 442: }
! 443: }
1.14 raeburn 444: }
1.11 albertel 445: &Apache::lonauth::success($r,$data{'username'},$data{'domain'},
1.25.2.5 raeburn 446: $home,$desturl,$extra_env,\%form,'',
447: $expirepub);
1.11 albertel 448: }
1.1 albertel 449: return OK;
450: }
1.6 albertel 451:
452: my $next_url='/adm/roles?selectrole=1&'.&escape($data{'role'}).'=1';
1.15 raeburn 453: if ($data{'origurl'} ne '') {
454: $next_url .= '&orgurl='.&escape($data{'origurl'});
455: }
1.25.2.5.2.1! raeburn 456: if ($data{'deeplink.login'}) {
! 457: if (ref($extra_env) eq 'HASH') {
! 458: $extra_env->{'request.deeplink.login'} = $data{'deeplink.login'};
! 459: } else {
! 460: $extra_env = {'request.deeplink.login' => $data{'deeplink.login'}};
! 461: }
! 462: if ($data{'linkprot'}) {
! 463: $extra_env->{'request.linkprot'} = $data{'linkprot'};
! 464: } elsif ($data{'linkkey'} ne '') {
! 465: $extra_env->{'request.linkkey'} = $data{'linkkey'};
! 466: }
! 467: }
1.6 albertel 468: &Apache::lonauth::success($r,$data{'username'},$data{'domain'},$home,
1.25.2.2 raeburn 469: $next_url,$extra_env,\%form,$cid);
1.6 albertel 470: return OK;
1.1 albertel 471: }
472:
473: 1;
474: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>