1: # The LearningOnline Network
2: # Starts a user off based of an existing token.
3: #
4: # $Id: migrateuser.pm,v 1.25.2.5.2.5 2022/06/18 01:01:42 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::migrateuser;
30:
31: use strict;
32: use LONCAPA qw(:DEFAULT :match);
33: use Apache::Constants qw(:common :http :methods :remotehost);
34: use Apache::lonauth;
35: use Apache::lonnet;
36: use Apache::lonlocal;
37: use Apache::lonlogin();
38: use CGI::Cookie;
39:
40: sub goto_login {
41: my ($r,$domain) = @_;
42: &Apache::loncommon::content_type($r,'text/html');
43: $r->send_http_header;
44: my $url = '/adm/login';
45: if ($domain) {
46: $url .= '?domain='.$domain;
47: }
48: $r->print(&Apache::loncommon::start_page('Going to login',undef,
49: {'redirect' => [0,$url],}).
50: '<h1>'.&mt('One moment please...').'</h1>'.
51: '<p>'.&mt('Transferring to login page.').'</p>'.
52: &Apache::loncommon::end_page());
53: return OK;
54: }
55:
56: sub sso_check {
57: my ($data) = @_;
58: my %extra_env;
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: }
67: }
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;
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;
90: }
91: my ($result,$cached)=&Apache::lonnet::is_cached_new('loadbalancing',$dom_in_use);
92: unless (defined($cached)) {
93: my $cachetime = 60*60*24;
94: my %domconfig =
95: &Apache::lonnet::get_dom('configuration',['loadbalancing'],$dom_in_use);
96: if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
97: $result = &Apache::lonnet::do_cache_new('loadbalancing',$dom_in_use,
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;
120: if ($rule_in_effect ne 'offloadedto') {
121: my $hosthere;
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: }
144: }
145: }
146: }
147: }
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: }
161: if ($hostname) {
162: my $protocol = $Apache::lonnet::protocol{$switchto};
163: $protocol = 'http' if ($protocol ne 'https');
164: my $alias = &Apache::lonnet::use_proxy_alias($r,$switchto);
165: $hostname = $alias if ($alias ne '');
166: $url = $protocol.'://'.$hostname;
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: }
174: }
175: }
176: unless ($hosthere) {
177: if (($dataref->{'balancer'}) && ($dataref->{'balcookie'})) {
178: &Apache::lonnet::delbalcookie($dataref->{'balcookie'},$dataref->{'balancer'});
179: }
180: }
181: }
182: if ($dataref->{'sso.login'}) {
183: $url .= '/adm/roles';
184: } else {
185: $url .= '/adm/login';
186: if ($udom) {
187: $url .= '?domain='.$udom;
188: }
189: $message .= '<br />'.&mt('You will need to provide your password one more time.');
190: }
191: my %info= (
192: 'domain' => $udom,
193: 'username' => $dataref->{'username'},
194: 'sessionserver' => $lonhost,
195: );
196: my @names = ('origurl','symb','role','linkprot','linkkey');
197: foreach my $name (@names) {
198: if ($dataref->{$name} ne '') {
199: $info{$name} = $dataref->{$name};
200: }
201: }
202: my $iptoken = &Apache::lonnet::tmpput(\%info,$switchto,'link');
203: unless ($iptoken eq 'conlost') {
204: $url .= (($url =~ /\?/) ? '&' : '?') . 'iptoken='.$iptoken;
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);
214: }
215: return OK;
216: }
217:
218: sub logout {
219: my ($r,$ip,$handle,$data,$lti_env) = @_;
220: my $lonidsdir=$r->dir_config('lonIDsDir');
221: if (unlink("$lonidsdir/$handle.id")) {
222: if (($env{'user.linkedenv'} =~ /^[a-f0-9]+_linked$/) &&
223: (-l "$lonidsdir/$env{'user.linkedenv'}.id") &&
224: (readlink("$lonidsdir/$env{'user.linkedenv'}.id") eq "$lonidsdir/$handle.id")) {
225: unlink("$lonidsdir/$env{'user.linkedenv'}.id");
226: }
227: }
228: my %temp=('logout' => time);
229: &Apache::lonnet::put('email_status',\%temp);
230: &Apache::lonnet::log($env{'user.domain'},
231: $env{'user.name'},
232: $env{'user.home'},
233: "Logout $ip");
234:
235: &Apache::loncommon::content_type($r,'text/html');
236:
237: #expire the cookies
238: my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
239: foreach my $name (keys(%cookies)) {
240: next unless ($name =~ /^lon(|S|Link|Pub)ID$/);
241: my $c = new CGI::Cookie(-name => $name,
242: -value => '',
243: -expires => '-10y',);
244: $r->headers_out->add('Set-cookie' => $c);
245: }
246: my (%info,%user_info,%lti_info);
247: if (ref($lti_env) eq 'HASH') {
248: foreach my $key (sort(keys(%{$lti_env}))) {
249: if ($key =~ /^request\.(.+)$/) {
250: $lti_info{$1} = $lti_env->{$key};
251: }
252: }
253: }
254: my $lonhost = $r->dir_config('lonHostID');
255: if (ref($data) eq 'HASH') {
256: %user_info=('ip' => $ip,
257: 'domain' => $data->{'domain'},
258: 'username' => $data->{'username'},
259: 'home' => $data->{'home'},
260: 'role' => $data->{'role'},
261: 'origurl' => $data->{'origurl'},
262: 'symb' => $data->{'symb'},
263: 'server' => $lonhost);
264: }
265: %info = (%user_info,%lti_info);
266: my $token = &Apache::lonnet::tmpput(\%info,$lonhost);
267: my $url = '/adm/migrateuser?token='.$token;
268: $r->send_http_header;
269: $r->print(
270: &Apache::loncommon::start_page('Updating Session ...',undef,
271: {'redirect' => [0.1,$url],
272: 'only_body' => 1,}).
273: &Apache::loncommon::end_page());
274: if ($env{'request.balancercookie'}) {
275: my ($balancer,$cookie) = split(/:/,$env{'request.balancercookie'});
276: if ((&Apache::lonnet::hostname($balancer)) && ($cookie =~ /^[a-f0-9]{32}$/)) {
277: $cookie = $env{'user.domain'}.'_'.$env{'user.name'}.'_'.$cookie;
278: &Apache::lonnet::delbalcookie($cookie,$balancer);
279: }
280: }
281: $r->register_cleanup(\&flush_course_logs);
282: return;
283: }
284:
285: sub flush_course_logs {
286: &Apache::lonnet::flushcourselogs();
287: return OK;
288: }
289:
290: sub handler {
291: my ($r) = @_;
292:
293: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['token']);
294: my %data = &Apache::lonnet::tmpget($env{'form.token'});
295: if (keys(%data) == 0) {
296: return &goto_login($r);
297: }
298: my $delete = &Apache::lonnet::tmpdel($env{'form.token'});
299:
300: &Apache::lonlocal::get_language_handle($r);
301:
302: if ($delete ne 'ok') {
303: return &goto_login($r);
304: }
305:
306: if (!defined($data{'username'}) || !defined($data{'domain'})) {
307: return &goto_login($r);
308: }
309: my $ip = &Apache::lonnet::get_requestor_ip($r,REMOTE_NOLOOKUP);
310: if ($data{'ip'} ne $ip) {
311: &Apache::lonnet::logthis('IP change when session migration requested -- was: '.
312: $data{'ip'}.'; now: '.$ip.' for '.$data{'username'}.':'.$data{'domain'});
313: return &ip_changed($r,$data{'domain'},$data{'server'},\%data);
314: }
315:
316: &Apache::lonnet::logthis("Allowing access for $data{'username'}:$data{'domain'} to $data{'role'}");
317: my $home=&Apache::lonnet::homeserver($data{'username'},$data{'domain'});
318: my $udom;
319: if (&Apache::lonnet::domain($data{'domain'})) {
320: $udom=$data{'domain'};
321: }
322: if ($home =~ /(con_lost|no_such_host)/) { return &goto_login($r,$udom); }
323:
324: my $extra_env = &sso_check(\%data);
325: if (($data{'balancer'}) && ($data{'server'}) && ($data{'balcookie'})) {
326: if (ref($extra_env) eq 'HASH') {
327: $extra_env->{'request.balancercookie'} = $data{'server'}.':'.$data{'balcookie'};
328: } else {
329: $extra_env = { 'request.balancercookie' => $data{'server'}.':'.$data{'balcookie'} };
330: }
331: } elsif (($data{'server'}) && ($data{'otherbalcookie'})) {
332: my ($balancer,$balcookie) = split(/:/,$data{'otherbalcookie'});
333: if (defined(&Apache::lonnet::hostname($balancer)) && $balcookie =~ /^[a-f0-9]{32}$/) {
334: my $baldom = &Apache::lonnet::host_domain($balancer);
335: if (&Apache::lonnet::shared_institution($baldom)) {
336: my $cookieid = join('_',$udom,$data{'username'},$balcookie);
337: &Apache::lonnet::updatebalcookie($cookieid,$balancer,$data{'server'});
338: if (ref($extra_env) eq 'HASH') {
339: $extra_env->{'request.balancercookie'} = $data{'otherbalcookie'};
340: } else {
341: $extra_env = { 'request.balancercookie' => $data{'otherbalcookie'} };
342: }
343: }
344: }
345: }
346:
347: my (%form,$cid);
348: if ($data{'symb'} ne '') {
349: $form{'symb'} = $data{'symb'};
350: }
351: if ($data{'iptoken'} ne '') {
352: $form{'iptoken'} = $data{'iptoken'};
353: }
354: if ($data{'noloadbalance'} ne '') {
355: $form{'noloadbalance'} = $data{'noloadbalance'};
356: }
357: if ($data{'role'}) {
358: if ($data{'role'} =~ m{\./($match_domain)/($match_courseid)(?:/\w+|$)}) {
359: unless (&Apache::lonnet::homeserver($2,$1) eq 'no_host') {
360: $cid = $1.'_'.$2;
361: }
362: }
363: } else {
364: my ($handle,$expirepub);
365: $handle = &Apache::lonnet::check_for_valid_session($r);
366:
367: # For "public user" - remove any existing "public" cookie so actual user is logged in.
368: if ($handle) {
369: if ($handle=~/^publicuser\_/) {
370: my $lonidsdir=$r->dir_config('lonIDsDir');
371: if ($lonidsdir ne '') {
372: unlink("$lonidsdir/$handle.id");
373: }
374: undef($handle);
375: $expirepub = 1;
376: }
377: }
378: if ($handle) {
379: &Apache::lonnet::transfer_profile_to_env($r->dir_config('lonIDsDir'),
380: $handle);
381: my $checklaunch;
382: if ($data{'origurl'} =~ m{^/tiny/$match_domain/\w+$}) {
383: unless ($env{'request.linkprot'} eq $data{'linkprot'}) {
384: $checklaunch = 1;
385: }
386: unless ($env{'request.linkkey'} eq $data{'linkkey'}) {
387: $checklaunch = 1;
388: }
389: unless ($env{'request.deeplink.login'} eq $data{'deeplink.login'}) {
390: $checklaunch = 1;
391: }
392: }
393: if ($data{'linkprot'} ne '') {
394: if (($env{'user.name'} ne $data{'username'}) ||
395: ($env{'user.domain'} ne $data{'domain'})) {
396: my %linkprot_env;
397: foreach my $item ('linkprot','deeplink.login') {
398: if ($data{$item}) {
399: $linkprot_env{'request.'.$item} = $data{$item};
400: }
401: }
402: &logout($r,$ip,$handle,\%data,\%linkprot_env);
403: return OK;
404: }
405: &Apache::lonnet::appenv({'request.linkprot' => $data{'linkprot'}});
406: if ($env{'request.linkkey'}) {
407: &Apache::lonnet::delenv('request.linkkey');
408: }
409: my ($linkprotector,$deeplink) = split(/:/,$data{'linkprot'},2);
410: if ($env{'user.linkprotector'}) {
411: my @protectors = split(/,/,$env{'user.linkprotector'});
412: unless (grep(/^\Q$linkprotector\E$/,@protectors)) {
413: push(@protectors,$linkprotector);
414: @protectors = sort { $a <=> $b } @protectors;
415: &Apache::lonnet::appenv({'user.linkprotector' => join(',',@protectors)});
416: }
417: } else {
418: &Apache::lonnet::appenv({'user.linkprotector' => $linkprotector });
419: }
420: if ($env{'user.linkproturi'}) {
421: my @proturis = split(/,/,$env{'user.linkproturi'});
422: unless(grep(/^\Q$deeplink\E$/,@proturis)) {
423: push(@proturis,$deeplink);
424: @proturis = sort @proturis;
425: &Apache::lonnet::appenv({'user.linkproturi' => join(',',@proturis)});
426: }
427: } else {
428: &Apache::lonnet::appenv({'user.linkproturi' => $deeplink});
429: }
430: } elsif ($data{'linkkey'} ne '') {
431: &Apache::lonnet::appenv({'request.linkkey' => $data{'linkkey'}});
432: if ($env{'request.linkprot'}) {
433: &Apache::lonnet::delenv('request.linkprot');
434: }
435: my $deeplink = $data{'deeplink.login'};
436: my $linkkey = $data{'linkkey'};
437: if ($env{'user.deeplinkkey'} ne '') {
438: my @linkkeys = split(/,/,$env{'user.deeplinkkey'});
439: unless (grep(/^\Q$linkkey\E$/,@linkkeys)) {
440: push(@linkkeys,$linkkey);
441: &Apache::lonnet::appenv({'user.deeplinkkey' => join(',',sort(@linkkeys))});
442: }
443: } else {
444: &Apache::lonnet::appenv({'user.deeplinkkey' => $linkkey});
445: }
446: if ($deeplink) {
447: if ($env{'user.keyedlinkuri'}) {
448: my @keyeduris = split(/,/,$env{'user.keyedlinkuri'});
449: unless (grep(/^\Q$deeplink\E$/,@keyeduris)) {
450: push(@keyeduris,$deeplink);
451: &Apache::lonnet::appenv({'user.keyedlinkuri' => join(',',sort(@keyeduris))});
452: }
453: } else {
454: &Apache::lonnet::appenv({'user.keyedlinkuri' => $deeplink});
455: }
456: }
457: }
458: if ($data{'deeplink.login'}) {
459: &Apache::lonnet::appenv({'request.deeplink.login' => $data{'deeplink.login'}});
460: if ($env{'environment.remote'} eq 'on') {
461: &Apache::lonnet::appenv({'environment.remote' => 'off'});
462: }
463: }
464: if ($data{'origurl'} ne '') {
465: my $dest = $data{'origurl'};
466: if (($env{'request.deeplink.login'} eq $data{'origurl'}) &&
467: (($env{'request.linkprot'}) || ($env{'request.linkkey'} ne ''))) {
468: my %info;
469: if ($env{'request.linkprot'}) {
470: $info{'linkprot'} = $env{'request.linkprot'};
471: } elsif ($env{'request.linkkey'} ne '') {
472: $info{'linkkey'} = $env{'request.linkkey'};
473: }
474: $info{'origurl'} = $data{'origurl'};
475: if ($checklaunch) {
476: $info{'checklaunch'} = 1;
477: }
478: my $token = &Apache::lonnet::tmpput(\%info,$r->dir_config('lonHostID'),'link');
479: unless (($token eq 'con_lost') || ($token eq 'refused') ||
480: ($token eq 'unknown_cmd') || ($token eq 'no_such_host')) {
481: $dest .= (($dest =~ /\?/) ? '&' : '?') . 'ttoken='.$token;
482: }
483: }
484: $r->internal_redirect($dest);
485: } elsif ($env{'request.course.id'}) {
486: $r->internal_redirect('/adm/navmaps');
487: } else {
488: $r->internal_redirect('/adm/roles');
489: }
490: } else {
491: my $desturl = '/adm/roles';
492: if ($data{'origurl'} ne '') {
493: $desturl = $data{'origurl'};
494: if ($data{'linkprot'}) {
495: my ($linkprotector,$linkuri) = split(/:/,$data{'linkprot'},2);
496: if ($linkprotector) {
497: if (ref($extra_env) eq 'HASH') {
498: $extra_env->{'user.linkprotector'} = $linkprotector;
499: $extra_env->{'user.linkproturi'} = $linkuri;
500: $extra_env->{'request.linkprot'} = $data{'linkprot'};
501: } else {
502: $extra_env = {'user.linkprotector' => $linkprotector,
503: 'user.linkproturi' => $linkuri,
504: 'request.linkprot' => $data{'linkprot'}};
505: }
506: }
507: } elsif ($data{'linkkey'}) {
508: if (ref($extra_env) eq 'HASH') {
509: $extra_env->{'user.deeplinkkey'} = $data{'linkkey'};
510: $extra_env->{'user.keyedlinkuri'} = $data{'deeplink.login'};
511: $extra_env->{'request.linkkey'} = $data{'linkkey'};
512: } else {
513: $extra_env = {'user.deeplinkkey' => $data{'linkkey'},
514: 'user.keyedlinkuri' => $data{'deeplink.login'},
515: 'request.linkkey' => $data{'linkkey'}};
516: }
517: }
518: if ($data{'deeplink.login'}) {
519: if (ref($extra_env) eq 'HASH') {
520: $extra_env->{'request.deeplink.login'} = $data{'deeplink.login'};
521: } else {
522: $extra_env = {'request.deeplink.login' => $data{'deeplink.login'}};
523: }
524: }
525: }
526: &Apache::lonauth::success($r,$data{'username'},$data{'domain'},
527: $home,$desturl,$extra_env,\%form,'',
528: $expirepub);
529: }
530: return OK;
531: }
532:
533: my $next_url='/adm/roles?selectrole=1&'.&escape($data{'role'}).'=1';
534: if ($data{'origurl'} ne '') {
535: $next_url .= '&orgurl='.&escape($data{'origurl'});
536: }
537: if ($data{'deeplink.login'}) {
538: if (ref($extra_env) eq 'HASH') {
539: $extra_env->{'request.deeplink.login'} = $data{'deeplink.login'};
540: } else {
541: $extra_env = {'request.deeplink.login' => $data{'deeplink.login'}};
542: }
543: if ($data{'linkprot'}) {
544: $extra_env->{'request.linkprot'} = $data{'linkprot'};
545: } elsif ($data{'linkkey'} ne '') {
546: $extra_env->{'request.linkkey'} = $data{'linkkey'};
547: }
548: }
549: &Apache::lonauth::success($r,$data{'username'},$data{'domain'},$home,
550: $next_url,$extra_env,\%form,$cid);
551: return OK;
552: }
553:
554: 1;
555: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>