File:
[LON-CAPA] /
loncom /
auth /
migrateuser.pm
Revision
1.65:
download - view:
text,
annotated -
select for diffs
Wed Aug 24 20:58:50 2022 UTC (23 months, 3 weeks ago) by
raeburn
Branches:
MAIN
CVS tags:
HEAD
- Dual SSO and non-SSO login from /adm/login
- Display of domain configuration for each server split into two tables
so input textboxes can be longer.
- "Pop-up if iframe" (Y/N) item added. If Y, when login page is in an
iframe, SSO log-in dialog will be displayed in a new window, i.e.,
not within the iframe, e.g., to satisfy sameorigin requirement in
x-frame-options in header sent by SSO server.
1: # The LearningOnline Network
2: # Starts a user off based of an existing token.
3: #
4: # $Id: migrateuser.pm,v 1.65 2022/08/24 20:58:50 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::loncommon;
37: use Apache::lonlocal;
38: use Apache::lonlogin();
39: use Apache::ltiauth;
40: use Apache::switchserver;
41: use CGI::Cookie;
42:
43: sub goto_login {
44: my ($r,$domain,$data) = @_;
45: my $write_to_opener;
46: if (ref($data) eq 'HASH') {
47: $write_to_opener = $data->{'lcssowin'};
48: }
49: if ((ref($data) eq 'HASH') && ($data->{'lti.login'})) {
50: &Apache::ltiauth::invalid_request($r,'22');
51: } else {
52: &Apache::loncommon::content_type($r,'text/html');
53: $r->send_http_header;
54: my $url = '/adm/login';
55: if ($domain) {
56: $url .= '?domain='.$domain;
57: }
58: $r->print(&Apache::loncommon::start_page('Going to login',undef,
59: {'redirect' => [0,$url,'',$write_to_opener],}).
60: '<h1>'.&mt('One moment please...').'</h1>'.
61: '<p>'.&mt('Transferring to login page.').'</p>'.
62: &Apache::loncommon::end_page());
63: }
64: return OK;
65: }
66:
67: sub sso_check {
68: my ($data) = @_;
69: my %extra_env;
70: if (ref($data) eq 'HASH') {
71: if ($data->{'sso.login'}) {
72: $extra_env{'request.sso.login'} = $data->{'sso.login'};
73: }
74: if ($data->{'sso.reloginserver'}) {
75: $extra_env{'request.sso.reloginserver'} =
76: $data->{'sso.reloginserver'};
77: }
78: }
79: return \%extra_env;
80: }
81:
82: sub lti_check {
83: my ($data) = @_;
84: my %lti_env;
85: if (ref($data) eq 'HASH') {
86: if ($data->{'lti.login'}) {
87: $lti_env{'request.lti.login'} = $data->{'lti.login'};
88: if ($data->{'lti.reqcrs'}) {
89: $lti_env{'request.lti.reqcrs'} = $data->{'lti.reqcrs'};
90: }
91: if ($data->{'lti.reqrole'}) {
92: $lti_env{'request.lti.reqrole'} = $data->{'lti.reqrole'};
93: }
94: if ($data->{'lti.selfenrollrole'}) {
95: $lti_env{'request.lti.selfenrollrole'} = $data->{'lti.selfenrollrole'};
96: }
97: if ($data->{'lti.uri'}) {
98: $lti_env{'request.lti.uri'} = $data->{'lti.uri'};
99: }
100: if ($data->{'lti.target'}) {
101: $lti_env{'request.lti.target'} = $data->{'lti.target'};
102: }
103: if ($data->{'lti.sourcecrs'}) {
104: $lti_env{'request.lti.sourcecrs'} = $data->{'lti.sourcecrs'};
105: }
106: }
107: if ($data->{'lti.passbackid'}) {
108: $lti_env{'request.lti.passbackid'} = $data->{'lti.passbackid'};
109: }
110: if ($data->{'lti.passbackurl'}) {
111: $lti_env{'request.lti.passbackurl'} = $data->{'lti.passbackurl'};
112: }
113: if ($data->{'lti.rosterid'}) {
114: $lti_env{'request.lti.rosterid'} = $data->{'lti.rosterid'};
115: }
116: if ($data->{'lti.rosterurl'}) {
117: $lti_env{'request.lti.rosterurl'} = $data->{'lti.rosterurl'};
118: }
119: }
120: return \%lti_env;
121: }
122:
123: sub canhost {
124: my ($uname,$udom,$lonhost,$loncaparev) = @_;
125: my $canhost;
126: if (&Apache::lonnet::is_library($lonhost)) {
127: my @possdoms = &Apache::lonnet::current_machine_domains();
128: my %roleshash = &Apache::lonnet::get_my_roles($uname,$udom,'userroles','',['ca','aa'],\@possdoms);
129: if (keys(%roleshash)) {
130: foreach my $key (keys(%roleshash)) {
131: my $audom = (split(/:/,$key))[1];
132: if ((&Apache::lonnet::will_trust('othcoau',$udom,$audom)) &&
133: (&Apache::lonnet::will_trust('coaurem',$audom,$udom))) {
134: $canhost = 1;
135: last;
136: }
137: }
138: }
139: }
140: unless ($canhost) {
141: my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
142: my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
143: my @intdoms;
144: my $internet_names = &Apache::lonnet::get_internet_names($lonhost);
145: if (ref($internet_names) eq 'ARRAY') {
146: @intdoms = @{$internet_names};
147: }
148: if ($uint_dom ne '' && grep(/^\Q$uint_dom\E$/,@intdoms)) {
149: $canhost = 1;
150: } else {
151: my $hostname = &Apache::lonnet::hostname($lonhost);
152: my $serverhomeID = &Apache::lonnet::get_server_homeID($hostname);
153: my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);
154: my %defdomdefaults = &Apache::lonnet::get_domain_defaults($serverhomedom);
155: my %udomdefaults = &Apache::lonnet::get_domain_defaults($udom);
156: $canhost =
157: &Apache::lonnet::can_host_session($udom,$lonhost,$loncaparev,
158: $udomdefaults{'remotesessions'},
159: $defdomdefaults{'hostedsessions'});
160: }
161: }
162: return $canhost;
163: }
164:
165: sub ip_changed {
166: my ($r,$udom,$camefrom,$idsref,$dataref) = @_;
167: &Apache::loncommon::content_type($r,'text/html');
168: $r->send_http_header;
169: if (ref($dataref) eq 'HASH') {
170: my $title = 'LON-CAPA Session redirected';
171: my $message = &mt('Your internet address has changed since you logged in.');
172: my $rule_in_effect;
173: if ($dataref->{'balancer'}) {
174: my $baldom = &Apache::lonnet::host_domain($camefrom);
175: my $balprimaryid = &Apache::lonnet::domain($baldom,'primary');
176: my $balintdom = &Apache::lonnet::internet_dom($balprimaryid);
177: my $uprimaryid = &Apache::lonnet::domain($udom,'primary');
178: my $uintdom = &Apache::lonnet::internet_dom($uprimaryid);
179: my $dom_in_use;
180: if (($uintdom ne '') && ($uintdom eq $balintdom)) {
181: $dom_in_use = $udom;
182: } else {
183: $dom_in_use = $baldom;
184: }
185: my ($result,$cached)=&Apache::lonnet::is_cached_new('loadbalancing',$dom_in_use);
186: unless (defined($cached)) {
187: my $cachetime = 60*60*24;
188: my %domconfig =
189: &Apache::lonnet::get_dom('configuration',['loadbalancing'],$dom_in_use);
190: if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
191: $result = &Apache::lonnet::do_cache_new('loadbalancing',$dom_in_use,
192: $domconfig{'loadbalancing'},$cachetime);
193: }
194: }
195: if (ref($result) eq 'HASH') {
196: (undef,my $currtargets,my $currrules) =
197: &Apache::lonnet::check_balancer_result($result,$dataref->{'server'});
198: if (ref($currrules) eq 'HASH') {
199: if ($dataref->{'sso.login'}) {
200: if ($currrules->{'_LC_ipchangesso'} ne '') {
201: $rule_in_effect = $currrules->{'_LC_ipchangesso'};
202: }
203: } else {
204: if ($currrules->{'_LC_ipchange'} ne '') {
205: $rule_in_effect = $currrules->{'_LC_ipchange'};
206: }
207: }
208: }
209: }
210: }
211: my $url;
212: my $lonhost= $r->dir_config('lonHostID');
213: my $switchto = $lonhost;
214: if ($rule_in_effect ne 'offloadedto') {
215: my ($hosthere,@ids);
216: if (ref($idsref) eq 'ARRAY') {
217: @ids=@{$idsref};
218: }
219: unless ($rule_in_effect eq 'balancer') {
220: if (grep(/^\Q$rule_in_effect\E$/,@ids)) {
221: $hosthere = 1;
222: }
223: }
224: unless ($hosthere) {
225: if ($dataref->{'role'}) {
226: my ($adom,$aname);
227: if ($dataref->{'role'} =~ m{^au\./($match_domain)/$}) {
228: $adom = $1;
229: $aname = $dataref->{'username'};
230: } elsif ($dataref->{'role'} =~ m{^(?:ca|aa)\./($match_domain)/($match_username)$}) {
231: $adom = $1;
232: $aname = $2;
233: }
234: if ($adom ne '' && $aname ne '') {
235: my $ahome = &Apache::lonnet::homeserver($aname,$adom);
236: unless ($ahome eq 'no_host') {
237: if ($ahome && grep(/^\Q$ahome\E$/,@ids)) {
238: $hosthere = 1;
239: }
240: }
241: }
242: }
243: }
244: unless ($hosthere) {
245: my $hostname;
246: if ($rule_in_effect eq 'balancer') {
247: $hostname = &Apache::lonnet::hostname($dataref->{'server'});
248: if ($hostname) {
249: $switchto = $dataref->{'server'};
250: }
251: } else {
252: $hostname = &Apache::lonnet::hostname($rule_in_effect);
253: if ($hostname) {
254: $switchto = $rule_in_effect;
255: }
256: }
257: if ($hostname) {
258: my $protocol = $Apache::lonnet::protocol{$switchto};
259: $protocol = 'http' if ($protocol ne 'https');
260: my $alias = &Apache::lonnet::use_proxy_alias($r,$switchto);
261: $hostname = $alias if ($alias ne '');
262: $url = $protocol.'://'.$hostname;
263: if ($rule_in_effect eq 'balancer') {
264: $message .= '<br />'.
265: &mt('As a result, your LON-CAPA session is being redirected to the server where you originally logged in.');
266: } else {
267: $message .= '<br />'.
268: &mt('As a result, your LON-CAPA session is being redirected.');
269: }
270: }
271: }
272: unless ($hosthere) {
273: if (($dataref->{'balancer'}) && ($dataref->{'balcookie'})) {
274: &Apache::lonnet::delbalcookie($dataref->{'balcookie'},$dataref->{'balancer'});
275: }
276: }
277: }
278: if ($dataref->{'sso.login'}) {
279: $url .= '/adm/roles';
280: } else {
281: $url .= '/adm/login';
282: if ($udom) {
283: $url .= '?domain='.$udom;
284: }
285: $message .= '<br />'.&mt('You will need to provide your password one more time.');
286: }
287: my %info= (
288: 'domain' => $udom,
289: 'username' => $dataref->{'username'},
290: 'sessionserver' => $lonhost,
291: );
292: my @names = ('origurl','symb','role','linkprotuser','linkprotexit','linkprot','linkkey');
293: foreach my $name (@names) {
294: if ($dataref->{$name} ne '') {
295: $info{$name} = $dataref->{$name};
296: }
297: }
298: my $iptoken = &Apache::lonnet::tmpput(\%info,$switchto,'link');
299: unless ($iptoken eq 'conlost') {
300: $url .= (($url =~ /\?/) ? '&' : '?') . 'iptoken='.$iptoken;
301: }
302: $r->print(&Apache::loncommon::start_page($title,undef,
303: {'redirect' =>
304: [2,$url,'',$dataref->{'lcssowin'}],}).
305: '<h1>'.&mt('One moment please...').'</h1>'.
306: '<p class="LC_warning">'.$message.'</p>'.
307: &Apache::loncommon::end_page());
308: } else {
309: return &goto_login($r);
310: }
311: return OK;
312: }
313:
314: sub logout {
315: my ($r,$ip,$handle,$data,$lti_env,$linkprot_info) = @_;
316: my $lonidsdir=$r->dir_config('lonIDsDir');
317: if (unlink("$lonidsdir/$handle.id")) {
318: if (($env{'user.linkedenv'} =~ /^[a-f0-9]+_linked$/) &&
319: (-l "$lonidsdir/$env{'user.linkedenv'}.id") &&
320: (readlink("$lonidsdir/$env{'user.linkedenv'}.id") eq "$lonidsdir/$handle.id")) {
321: unlink("$lonidsdir/$env{'user.linkedenv'}.id");
322: }
323: }
324: my %temp=('logout' => time);
325: &Apache::lonnet::put('email_status',\%temp);
326: &Apache::lonnet::log($env{'user.domain'},
327: $env{'user.name'},
328: $env{'user.home'},
329: "Logout $ip");
330:
331: &Apache::loncommon::content_type($r,'text/html');
332:
333: #expire the cookies
334: my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
335: foreach my $name (keys(%cookies)) {
336: next unless ($name =~ /^lon(|S|Link|Pub)ID$/);
337: my $c = new CGI::Cookie(-name => $name,
338: -value => '',
339: -expires => '-10y',);
340: $r->headers_out->add('Set-cookie' => $c);
341: }
342: my $lonhost = $r->dir_config('lonHostID');
343: my ($url,$token,$delay,$write_to_opener,%info,%user_info);
344: if (ref($data) eq 'HASH') {
345: %user_info=('ip' => $ip,
346: 'domain' => $data->{'domain'},
347: 'username' => $data->{'username'},
348: 'home' => $data->{'home'},
349: 'role' => $data->{'role'},
350: 'origurl' => $data->{'origurl'},
351: 'symb' => $data->{'symb'},
352: 'server' => $lonhost);
353: $write_to_opener = $data->{'lcssowin'};
354: }
355: $delay = 0.1;
356: if (ref($linkprot_info) eq 'HASH') {
357: if ($linkprot_info->{'linkprotuser'} ne '') {
358: $token = &Apache::lonnet::tmpput($linkprot_info,$lonhost);
359: $url = '/adm/login?ltoken='.$token;
360: } else {
361: foreach my $key (keys(%user_info)) {
362: if ($user_info{$key} eq '') {
363: delete($user_info{$key});
364: }
365: }
366: %info = (%user_info,%{$linkprot_info});
367: $token = &Apache::lonnet::tmpput(\%info,$lonhost);
368: $url = '/adm/migrateuser?token='.$token;
369: }
370: $delay = 0;
371: } else {
372: my %lti_info;
373: if (ref($lti_env) eq 'HASH') {
374: foreach my $key (sort(keys(%{$lti_env}))) {
375: if ($key =~ /^request\.(.+)$/) {
376: $lti_info{$1} = $lti_env->{$key};
377: }
378: }
379: }
380: %info = (%user_info,%lti_info);
381: $token = &Apache::lonnet::tmpput(\%info,$lonhost);
382: $url = '/adm/migrateuser?token='.$token;
383: }
384: $r->send_http_header;
385: $r->print(
386: &Apache::loncommon::start_page('Updating Session ...',undef,
387: {'redirect' => [$delay,$url,'',$write_to_opener],
388: 'only_body' => 1,}).
389: &Apache::loncommon::end_page());
390: if ($env{'request.balancercookie'}) {
391: my ($balancer,$cookie) = split(/:/,$env{'request.balancercookie'});
392: if ((&Apache::lonnet::hostname($balancer)) && ($cookie =~ /^[a-f0-9]{32}$/)) {
393: $cookie = $env{'user.domain'}.'_'.$env{'user.name'}.'_'.$cookie;
394: &Apache::lonnet::delbalcookie($cookie,$balancer);
395: }
396: }
397: $r->register_cleanup(\&flush_course_logs);
398: return;
399: }
400:
401: sub conlost_userhome {
402: my ($r,$idsref,$dataref) = @_;
403: return unless ((ref($idsref) eq 'ARRAY') && (ref($dataref) eq 'HASH'));
404: my @ids = @{$idsref};
405: my %data = %{$dataref};
406: my ($lonhost,$lowest_load,$otherserver,$is_balancer,%conlost,%posstargets);
407: $lonhost = $r->dir_config('lonHostID');
408: if (grep(/^\Q$lonhost\E$/,split(/,/,$data{'dom_balancers'}))) {
409: $is_balancer = 1;
410: }
411: $lowest_load = 30000;
412: if ($data{'conlost'} ne '') {
413: map { $conlost{$_} = 1; } split(/,/,$data{'conlost'});
414: }
415: if ($data{'loncfail'}) {
416: if ($lonhost ne $data{'from_balancer'}) {
417: my ($is_balancer,$posshost,$setcookie,$offloadto,$dom_balancers) =
418: &Apache::lonnet::check_loadbalancing($data{'username'},$data{'domain'});
419: if ($is_balancer) {
420: if (ref($offloadto) eq 'HASH') {
421: $data{'offloadto'} = '';
422: foreach my $key (keys(%{$offloadto})) {
423: if (ref($offloadto->{$key}) eq 'ARRAY') {
424: $data{'offloadto'} .= $key.'='.join(',',@{$offloadto->{$key}}).'&';
425: }
426: }
427: $data{'offloadto'} =~ s/\&$//;
428: } elsif (ref($offloadto) eq 'ARRAY') {
429: $data{'offloadto'} = join(',',@{$offloadto});
430: }
431: }
432: }
433: } else {
434: $conlost{$lonhost} = 1;
435: $data{'conlost'} = join(',',sort(keys(%conlost)));
436: }
437: if ($data{'offloadto'} =~ /\&/) {
438: foreach my $item (split(/\&/,$data{'offloadto'})) {
439: my ($type,$targets) = split(/\=/,$item);
440: @{$posstargets{$type}} = split(/,/,$targets);
441: }
442: } elsif ($data{'offloadto'} =~ /=/) {
443: my ($type,$targets) = split(/\=/,$data{'offloadto'});
444: @{$posstargets{$type}} = split(/,/,$targets);
445: } else {
446: @{$posstargets{'default'}} = split(/,/,$data{'offloadto'});
447: }
448: if (ref($posstargets{'primary'}) eq 'ARRAY') {
449: foreach my $try_server (@{$posstargets{'primary'}}) {
450: next if ($conlost{$try_server});
451: next if ((grep(/^\Q$try_server\E$/,@ids)) && !$data{'loncfail'});
452: ($otherserver,$lowest_load) =
453: &Apache::lonnet::compare_server_load($try_server,
454: $otherserver,
455: $lowest_load);
456: }
457: }
458: my $found_server = ($otherserver ne '' && $lowest_load < 100);
459: if (!$found_server) {
460: if (ref($posstargets{'default'}) eq 'ARRAY') {
461: foreach my $try_server (@{$posstargets{'default'}}) {
462: next if ($conlost{$try_server});
463: next if ((grep(/^\Q$try_server\E$/,@ids)) && !$data{'loncfail'});
464: ($otherserver,$lowest_load) =
465: &Apache::lonnet::compare_server_load($try_server,
466: $otherserver,
467: $lowest_load);
468: }
469: }
470: }
471: if ($otherserver ne '') {
472: my $switchto = &Apache::lonnet::hostname($otherserver);
473: if ($switchto ne '') {
474: $data{'server'} = $lonhost;
475: if ($data{'loncfail'}) {
476: delete($data{'loncfail'});
477: }
478: if (grep(/^\Q$otherserver\E$/,split(/,/,$data{'dom_balancers'}))) {
479: $data{'noloadbalance'} = $otherserver;
480: }
481: &do_server_switch($r,$otherserver,$switchto,$is_balancer,\%data);
482: return $otherserver;
483: } else {
484: #FIXME Contents of $data{offloadto} contains invalid hostID.
485: }
486: } else {
487: my (%poss_balancers,%tried_balancers);
488: map { $poss_balancers{$_} = 1; } split(/,/,$data{'dom_balancers'});
489: map { $tried_balancers{$_} = 1; } split(/,/,$data{'tried_balancers'});
490: if (keys(%poss_balancers)) {
491: foreach my $try_server (sort(keys(%poss_balancers))) {
492: next if (grep(/^\Q$try_server\E$/,@ids));
493: next if (($data{'loncfail'}) && ($tried_balancers{$try_server}));
494: my $alreadytried;
495: foreach my $key (keys(%posstargets)) {
496: if ((ref($posstargets{$key}) eq 'ARRAY') &&
497: (grep(/^\Q$try_server\E$/,@{$posstargets{$key}}))) {
498: $alreadytried = 1;
499: last;
500: }
501: }
502: unless ($alreadytried) {
503: if (&Apache::lonnet::reply('ping',$try_server) eq $try_server) {
504: $otherserver = $try_server;
505: last;
506: }
507: }
508: }
509: }
510: if ($otherserver ne '') {
511: my $switchto = &Apache::lonnet::hostname($otherserver);
512: if ($switchto ne '') {
513: $data{'server'} = $lonhost;
514: $data{'loncfail'} = $lonhost;
515: $tried_balancers{$otherserver} = 1;
516: $data{'tried_balancers'} = join(',',sort(keys(%tried_balancers)));
517: &do_server_switch($r,$otherserver,$switchto,$is_balancer,\%data);
518: return $otherserver;
519: } else {
520: #FIXME Contents of $data{'dom_balancers'} contains invalid hostID.
521: }
522: } else {
523: if ($data{'loncfail'}) {
524: #FIXME Nowhere to go.
525: } else {
526: # FIXME Send back to a balancer (no token); different balancer if this is a balancer.
527: # Send command to balancer to exclude this host using http instead on lonc/lond
528: }
529: }
530: }
531: return;
532: }
533:
534: sub do_server_switch {
535: my ($r,$otherserver,$switchto,$is_balancer,$dataref) = @_;
536: if (ref($dataref) eq 'HASH') {
537: my $domain = $dataref->{'domain'};
538: my $username = $dataref->{'username'};
539: # If this is a balancer set a balancer cookie unless browser already sent
540: # LON-CAPA load balancer cookie which points at the target server.
541: if ($is_balancer) {
542: my $newcookieid;
543: my ($found_server,$balancer_cookie) = &Apache::lonnet::check_for_balancer_cookie($r,1);
544: unless (($found_server eq $otherserver) &&
545: ($balancer_cookie =~ /^\Q$domain\E_\Q$username\E_/)) {
546: $newcookieid =
547: &Apache::switchserver::balancer_cookieid($r,$otherserver,$username,$domain);
548: if ($newcookieid) {
549: $dataref->{'balcookie'} = $newcookieid;
550: }
551: }
552: }
553: my $token = &Apache::lonnet::tmpput($dataref,$otherserver);
554: my $protocol = $Apache::lonnet::protocol{$otherserver};
555: $protocol = 'http' if ($protocol ne 'https');
556: my $url = $protocol.'://'.$switchto.'/adm/login?domain='.$domain.
557: '&username='.$username.'&token='.$token;
558: $r->send_http_header;
559: $r->print(
560: &Apache::loncommon::start_page('Switching Server ...',undef,
561: {'redirect' => [0.1,$url]}).
562: &Apache::loncommon::end_page());
563: }
564: return;
565: }
566:
567: sub log_switch {
568: my ($r,$data,$lti_env,$ip) = @_;
569: my $lonhost = $r->dir_config('lonHostID');
570: return unless ((ref($data) eq 'HASH') && (ref($lti_env) eq 'HASH'));
571: my $now = time;
572: my %temp=('switchserver' => $now.':'.$lonhost,$data->{'role'});
573: &Apache::lonnet::put('email_status',\%temp);
574: my $logmsg = "Switch Server to $lonhost";
575: if ($data->{'role'}) {
576: $logmsg .= " with role: ".$data->{'role'};
577: } elsif (($lti_env->{'reqcrs'}) && ($lti_env->{'reqrole'} eq 'cc')) {
578: $logmsg .= " to create new LTI course";
579: } elsif ($lti_env->{'selfenrollrole'}) {
580: $logmsg .= " to selfenroll with role: ".$lti_env->{'selfenrollrole'};
581: } else {
582: $logmsg .= " (no role)";
583: }
584: $logmsg .= ' '.$ip;
585: &Apache::lonnet::log($data->{'domain'},$data->{'username'},$data->{'home'},$logmsg);
586: }
587:
588: sub flush_course_logs {
589: &Apache::lonnet::flushcourselogs();
590: return OK;
591: }
592:
593: sub handler {
594: my ($r) = @_;
595:
596: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['token']);
597: my %data = &Apache::lonnet::tmpget($env{'form.token'});
598: if (keys(%data) == 0) {
599: return &goto_login($r);
600: }
601: my $delete = &Apache::lonnet::tmpdel($env{'form.token'});
602:
603: &Apache::lonlocal::get_language_handle($r);
604:
605: if ($delete ne 'ok') {
606: return &goto_login($r,undef,\%data);
607: }
608:
609: if (!defined($data{'username'}) || !defined($data{'domain'})) {
610: return &goto_login($r,undef,\%data);
611: }
612: my ($home,@ids);
613: @ids=&Apache::lonnet::current_machine_ids();
614: my $ip = &Apache::lonnet::get_requestor_ip($r,REMOTE_NOLOOKUP);
615: if ($data{'ip'} ne $ip) {
616: &Apache::lonnet::logthis('IP change when session migration requested -- was: '.
617: $data{'ip'}.'; now: '.$ip.' for '.$data{'username'}.':'.$data{'domain'});
618: return &ip_changed($r,$data{'domain'},$data{'server'},\@ids,\%data);
619: }
620: if ($data{'loncfail'}) {
621: &Apache::lonnet::logthis('Returned from '.$data{'server'}.' -- no connection to library node or other access node(s)');
622: my $otherserver = &conlost_userhome($r,\@ids,\%data);
623: if ($otherserver ne '') {
624: &Apache::lonnet::logthis("Switching to $otherserver");
625: }
626: return OK;
627: } else {
628: $home=&Apache::lonnet::homeserver($data{'username'},$data{'domain'});
629: if ($home eq 'no_host') {
630: if (($data{'home'} ne '') && (&Apache::lonnet::hostname($data{'home'}))) {
631: &Apache::lonnet::reconlonc($data{'home'});
632: $home=&Apache::lonnet::homeserver($data{'username'},$data{'domain'});
633: if ($home eq 'no_host') {
634: unless (grep(/^\Q$data{'home'}\E$/,@ids)) {
635: if (&Apache::lonnet::reply('ping',$data{'home'}) eq 'con_lost') {
636: my $otherserver = &conlost_userhome($r,\@ids,\%data);
637: if ($otherserver ne '') {
638: &Apache::lonnet::logthis("No connection to home server ($data{'home'}) for $data{'username'}:$data{'domain'}. Switching to $otherserver");
639: }
640: return OK;
641: }
642: }
643: }
644: }
645: }
646: }
647:
648: my $udom;
649: if (&Apache::lonnet::domain($data{'domain'})) {
650: $udom=$data{'domain'};
651: }
652: if ($home eq 'no_host') { return &goto_login($r,$udom,\%data); }
653: if (&Apache::lonnet::hostname($home) eq '') { return &goto_login($r,$udom,\%data); }
654:
655: unless (grep(/^\Q$home\E$/,@ids)) {
656: my $lonhost = $r->dir_config('lonHostID');
657: my $loncaparev = $r->dir_config('lonVersion');
658: unless (&canhost($data{'username'},$data{'domain'},$lonhost,$loncaparev)) {
659: return &goto_login($r,$udom,\%data);
660: }
661: }
662:
663: my $rolemsg;
664: if ($data{'role'}) {
665: $rolemsg = "role: $data{'role'}";
666: } else {
667: $rolemsg = '(no role)';
668: }
669:
670: &Apache::lonnet::logthis("Allowing access for $data{'username'}:$data{'domain'} $rolemsg");
671:
672: my $sso_env = &sso_check(\%data);
673: my $lti_env = <i_check(\%data);
674:
675: my $extra_env;
676: if ((ref($sso_env) eq 'HASH') && (keys(%{$sso_env}))) {
677: $extra_env = $sso_env;
678: } elsif ((ref($lti_env) eq 'HASH') && (keys(%{$lti_env}))) {
679: $extra_env = $lti_env;
680: }
681: if (($data{'balancer'}) && ($data{'server'}) && ($data{'balcookie'})) {
682: if (ref($extra_env) eq 'HASH') {
683: $extra_env->{'request.balancercookie'} = $data{'server'}.':'.$data{'balcookie'};
684: } else {
685: $extra_env = { 'request.balancercookie' => $data{'server'}.':'.$data{'balcookie'} };
686: }
687: } elsif (($data{'server'}) && ($data{'otherbalcookie'})) {
688: my ($balancer,$balcookie) = split(/:/,$data{'otherbalcookie'});
689: if (defined(&Apache::lonnet::hostname($balancer)) && $balcookie =~ /^[a-f0-9]{32}$/) {
690: my $baldom = &Apache::lonnet::host_domain($balancer);
691: if (&Apache::lonnet::shared_institution($baldom)) {
692: my $cookieid = join('_',$udom,$data{'username'},$balcookie);
693: &Apache::lonnet::updatebalcookie($cookieid,$balancer,$data{'server'});
694: if (ref($extra_env) eq 'HASH') {
695: $extra_env->{'request.balancercookie'} = $data{'otherbalcookie'};
696: } else {
697: $extra_env = { 'request.balancercookie' => $data{'otherbalcookie'} };
698: }
699: }
700: }
701: }
702:
703: if (($data{'conlost'}) && ($data{'server'})) {
704: my @conlosts = split(/,/,$data{'conlost'});
705: my $switchfrom = $data{'server'};
706: if (@conlosts) {
707: if (grep(/^\Q$switchfrom\E$/,@conlosts)) {
708: &log_switch($r,\%data,$extra_env,$ip);
709: }
710: }
711: }
712:
713: my (%form,$cid);
714: if ($data{'symb'} ne '') {
715: $form{'symb'} = $data{'symb'};
716: }
717: if ($data{'iptoken'} ne '') {
718: $form{'iptoken'} = $data{'iptoken'};
719: }
720: if ($data{'noloadbalance'} ne '') {
721: $form{'noloadbalance'} = $data{'noloadbalance'};
722: }
723: if ($data{'role'}) {
724: if ($data{'role'} =~ m{\./($match_domain)/($match_courseid)(?:/\w+|$)}) {
725: unless (&Apache::lonnet::homeserver($2,$1) eq 'no_host') {
726: $cid = $1.'_'.$2;
727: }
728: }
729: } else {
730: my ($handle,$expirepub);
731: $handle = &Apache::lonnet::check_for_valid_session($r);
732:
733: # For "public user" - remove any existing "public" cookie so actual user is logged in.
734: if ($handle) {
735: if ($handle=~/^publicuser\_/) {
736: my $lonidsdir=$r->dir_config('lonIDsDir');
737: if ($lonidsdir ne '') {
738: unlink("$lonidsdir/$handle.id");
739: }
740: undef($handle);
741: $expirepub = 1;
742: }
743: }
744: if ($handle) {
745: &Apache::lonnet::transfer_profile_to_env($r->dir_config('lonIDsDir'),
746: $handle);
747: my $checklaunch;
748: if ($data{'origurl'} =~ m{^/tiny/$match_domain/\w+$}) {
749: unless ($env{'request.linkprot'} eq $data{'linkprot'}) {
750: $checklaunch = 1;
751: }
752: unless ($env{'request.linkprotexit'} eq $data{'linkprotexit'}) {
753: $checklaunch = 1;
754: }
755: unless ($env{'request.linkkey'} eq $data{'linkkey'}) {
756: $checklaunch = 1;
757: }
758: unless ($env{'request.deeplink.login'} eq $data{'deeplink.login'}) {
759: $checklaunch = 1;
760: }
761: }
762: if ($data{'linkprot'} ne '') {
763: if (($env{'user.name'} ne $data{'username'}) ||
764: ($env{'user.domain'} ne $data{'domain'})) {
765: my %linkprot_env;
766: foreach my $item ('linkprot','linkprotexit','deeplink.login') {
767: if ($data{$item}) {
768: $linkprot_env{$item} = $data{$item};
769: }
770: }
771: &logout($r,$ip,$handle,\%data,undef,\%linkprot_env);
772: return OK;
773: }
774: &Apache::lonnet::appenv({'request.linkprot' => $data{'linkprot'}});
775: if ($data{'linkprotexit'}) {
776: &Apache::lonnet::appenv({'request.linkprotexit' => $data{'linkprotexit'}});
777: }
778: if ($env{'request.linkkey'}) {
779: &Apache::lonnet::delenv('request.linkkey');
780: }
781: my ($linkprotector,$deeplink) = split(/:/,$data{'linkprot'},2);
782: if ($env{'user.linkprotector'}) {
783: my @protectors = split(/,/,$env{'user.linkprotector'});
784: unless (grep(/^\Q$linkprotector\E$/,@protectors)) {
785: push(@protectors,$linkprotector);
786: @protectors = sort { $a <=> $b } @protectors;
787: &Apache::lonnet::appenv({'user.linkprotector' => join(',',@protectors)});
788: }
789: } else {
790: &Apache::lonnet::appenv({'user.linkprotector' => $linkprotector });
791: }
792: if ($env{'user.linkproturi'}) {
793: my @proturis = split(/,/,$env{'user.linkproturi'});
794: unless(grep(/^\Q$deeplink\E$/,@proturis)) {
795: push(@proturis,$deeplink);
796: @proturis = sort @proturis;
797: &Apache::lonnet::appenv({'user.linkproturi' => join(',',@proturis)});
798: }
799: } else {
800: &Apache::lonnet::appenv({'user.linkproturi' => $deeplink});
801: }
802: } elsif ($data{'linkkey'} ne '') {
803: &Apache::lonnet::appenv({'request.linkkey' => $data{'linkkey'}});
804: if ($env{'request.linkprot'}) {
805: &Apache::lonnet::delenv('request.linkprot');
806: }
807: my $deeplink = $data{'deeplink.login'};
808: my $linkkey = $data{'linkkey'};
809: if ($env{'user.deeplinkkey'} ne '') {
810: my @linkkeys = split(/,/,$env{'user.deeplinkkey'});
811: unless (grep(/^\Q$linkkey\E$/,@linkkeys)) {
812: push(@linkkeys,$linkkey);
813: &Apache::lonnet::appenv({'user.deeplinkkey' => join(',',sort(@linkkeys))});
814: }
815: } else {
816: &Apache::lonnet::appenv({'user.deeplinkkey' => $linkkey});
817: }
818: if ($deeplink) {
819: if ($env{'user.keyedlinkuri'}) {
820: my @keyeduris = split(/,/,$env{'user.keyedlinkuri'});
821: unless (grep(/^\Q$deeplink\E$/,@keyeduris)) {
822: push(@keyeduris,$deeplink);
823: &Apache::lonnet::appenv({'user.keyedlinkuri' => join(',',sort(@keyeduris))});
824: }
825: } else {
826: &Apache::lonnet::appenv({'user.keyedlinkuri' => $deeplink});
827: }
828: }
829: }
830: if ($data{'deeplink.login'}) {
831: &Apache::lonnet::appenv({'request.deeplink.login' => $data{'deeplink.login'}});
832: }
833: if ($data{'lti.login'}) {
834: my $needslogout;
835: if ($env{'request.lti.login'}) {
836: if (($env{'user.name'} ne $data{'username'}) ||
837: ($env{'user.domain'} ne $data{'domain'})) {
838: $needslogout = 1;
839: }
840: } else {
841: $needslogout = 1;
842: }
843: # If access is via LTI, and user already has a non-LTI session cookie
844: # (and session) or has an LTI session cookie for a different username,
845: # logout the existing session, and start a new one
846: if ($needslogout) {
847: &logout($r,$ip,$handle,\%data,$lti_env);
848: } elsif (($data{'lti.reqcrs'}) && ($data{'lti.reqrole'} eq 'cc')) {
849: $form{'lti.reqcrs'} = $data{'lti.reqcrs'};
850: $form{'lti.reqrole'} = $data{'lti.reqrole'};
851: $form{'lti.sourcecrs'} = $data{'lti.sourcecrs'};
852: $form{'lti.uri'} = $data{'lti.uri'};
853: if ($data{'lti.passbackid'}) {
854: $form{'lti.passbackid'} = $data{'lti.passbackid'};
855: }
856: if ($data{'lti.passbackurl'}) {
857: $form{'lti.passbackurl'} = $data{'lti.passbackurl'};
858: }
859: if ($data{'lti.rosterid'}) {
860: $form{'lti.rosterid'} = $data{'lti.rosterid'};
861: }
862: if ($data{'lti.rosterurl'}) {
863: $form{'lti.rosterurl'} = $data{'lti.rosterurl'};
864: }
865: if ($data{'lti.target'}) {
866: $form{'lti.target'} = $data{'lti.target'};
867: }
868: &Apache::loncommon::content_type($r,'text/html');
869: $r->send_http_header;
870: &Apache::ltiauth::lti_reqcrs($r,$data{'domain'},\%form,$data{'username'},$data{'domain'});
871: } else {
872: if (ref($lti_env) eq 'HASH') {
873: delete($lti_env->{'reqcrs'});
874: delete($lti_env->{'reqrole'});
875: delete($lti_env->{'selfenrollrole'});
876: }
877: if ($data{'lti.selfenrollrole'}) {
878: if (&Apache::ltiauth::lti_enroll($data{'username'},$data{'domain'},
879: $data{'lti.selfenrollrole'}) eq 'ok') {
880: my $url = '/adm/roles?selectrole=1&'.
881: &escape($data{'lti.selfenrollrole'}).'=1';
882: if ($data{'origurl'} =~ m{/default_\d+\.sequence$}) {
883: $url .= '&orgurl='.$data{'origurl'}.'&navmap=1';
884: } elsif ($data{'origurl'} ne '') {
885: $url .= '&orgurl='.$data{'origurl'};
886: }
887: if (ref($lti_env) eq 'HASH') {
888: &Apache::lonnet::appenv($lti_env);
889: }
890: $r->internal_redirect($url);
891: } else {
892: &Apache::ltiauth::invalid_request($r,23);
893: }
894: } elsif ($data{'origurl'} ne '') {
895: my $url = $data{'origurl'};
896: if ($url =~ m{/default_\d+\.sequence$}) {
897: $url .= (($url =~/\?/)?'&':'?').'navmap=1';
898: }
899: if (ref($lti_env) eq 'HASH') {
900: &Apache::lonnet::appenv($lti_env);
901: }
902: $r->internal_redirect($url);
903: } else {
904: if (ref($lti_env) eq 'HASH') {
905: &Apache::lonnet::appenv($lti_env);
906: }
907: }
908: }
909: } elsif ($data{'origurl'} ne '') {
910: my $dest = $data{'origurl'};
911: if (($env{'request.deeplink.login'} eq $data{'origurl'}) &&
912: (($env{'request.linkprot'}) || ($env{'request.linkkey'} ne ''))) {
913: my %info;
914: if ($env{'request.linkprot'}) {
915: $info{'linkprot'} = $env{'request.linkprot'};
916: foreach my $item ('linkprotuser','linkprotexit') {
917: if ($data{$item}) {
918: $info{$item} = $data{$item};
919: }
920: }
921: } elsif ($env{'request.linkkey'} ne '') {
922: $info{'linkkey'} = $env{'request.linkkey'};
923: }
924: $info{'origurl'} = $data{'origurl'};
925: if ($checklaunch) {
926: $info{'checklaunch'} = 1;
927: }
928: my $token = &Apache::lonnet::tmpput(\%info,$r->dir_config('lonHostID'),'link');
929: unless (($token eq 'con_lost') || ($token eq 'refused') || ($token =~ /^error:/) ||
930: ($token eq 'unknown_cmd') || ($token eq 'no_such_host')) {
931: $dest .= (($dest =~ /\?/) ? '&' : '?') . 'ttoken='.$token;
932: }
933: }
934: $r->internal_redirect($dest);
935: } elsif ($env{'request.course.id'}) {
936: $r->internal_redirect('/adm/navmaps');
937: } else {
938: $r->internal_redirect('/adm/roles');
939: }
940: } else {
941: my $desturl = '/adm/roles';
942: if ($data{'lti.login'}) {
943: if ($data{'lti.selfenrollrole'}) {
944: $desturl .= '?selectrole=1&'.
945: &escape($data{'lti.selfenrollrole'}).'=1';
946: if ($data{'origurl'} ne '') {
947: $desturl .= '&orgurl='.$data{'origurl'};
948: if ($data{'origurl'} =~ m{/default_\d+\.sequence$}) {
949: $desturl .= '&navmap=1';
950: }
951: }
952: } elsif ($data{'origurl'} ne '') {
953: $desturl = $data{'origurl'};
954: if ($desturl =~ m{/default_\d+\.sequence$}) {
955: $desturl .= (($desturl =~/\?/)?'&':'?').'navmap=1';
956: }
957: }
958: } elsif ($data{'origurl'} ne '') {
959: $desturl = $data{'origurl'};
960: if ($data{'linkprot'}) {
961: my ($linkprotector,$linkuri) = split(/:/,$data{'linkprot'},2);
962: if ($linkprotector) {
963: if (ref($extra_env) eq 'HASH') {
964: $extra_env->{'user.linkprotector'} = $linkprotector;
965: $extra_env->{'user.linkproturi'} = $linkuri;
966: $extra_env->{'request.linkprot'} = $data{'linkprot'};
967: } else {
968: $extra_env = {'user.linkprotector' => $linkprotector,
969: 'user.linkproturi' => $linkuri,
970: 'request.linkprot' => $data{'linkprot'}};
971: }
972: if ($data{'linkprotexit'}) {
973: $extra_env->{'request.linkprotexit'} = $data{'linkprotexit'};
974: }
975: }
976: foreach my $item ('linkprotuser','linkprotexit') {
977: if ($data{$item} ne '') {
978: $form{$item} = $data{$item};
979: }
980: }
981: } elsif ($data{'linkkey'} ne '') {
982: if (ref($extra_env) eq 'HASH') {
983: $extra_env->{'user.deeplinkkey'} = $data{'linkkey'};
984: $extra_env->{'user.keyedlinkuri'} = $data{'deeplink.login'};
985: $extra_env->{'request.linkkey'} = $data{'linkkey'};
986: } else {
987: $extra_env = {'user.deeplinkkey' => $data{'linkkey'},
988: 'user.keyedlinkuri' => $data{'deeplink.login'},
989: 'request.linkkey' => $data{'linkkey'}};
990: }
991: }
992: if ($data{'deeplink.login'}) {
993: if (ref($extra_env) eq 'HASH') {
994: $extra_env->{'request.deeplink.login'} = $data{'deeplink.login'};
995: } else {
996: $extra_env = {'request.deeplink.login' => $data{'deeplink.login'}};
997: }
998: }
999: }
1000: my $skipcritical;
1001: if ($data{'lti.login'}) {
1002: if ((($data{'lti.reqcrs'}) &&
1003: ($data{'lti.reqrole'} eq 'cc')) ||
1004: ($data{'lti.selfenrollrole'})) {
1005: $skipcritical = 1;
1006: }
1007: }
1008: &Apache::lonauth::success($r,$data{'username'},$data{'domain'},
1009: $home,$desturl,$extra_env,\%form,$skipcritical,'',
1010: $expirepub,$data{'lcssowin'});
1011: }
1012: return OK;
1013: }
1014:
1015: if ($data{'lti.login'}) {
1016: my $handle = &Apache::lonnet::check_for_valid_session($r);
1017: if ($handle) {
1018: &Apache::lonnet::transfer_profile_to_env($r->dir_config('lonIDsDir'),
1019: $handle);
1020: my $needslogout;
1021: if ($env{'request.lti.login'}) {
1022: if (($env{'user.name'} ne $data{'username'}) ||
1023: ($env{'user.domain'} ne $data{'domain'})) {
1024: $needslogout = 1;
1025: }
1026: } else {
1027: $needslogout = 1;
1028: }
1029: if ($needslogout) {
1030: &logout($r,$ip,$handle,\%data,$lti_env);
1031: return OK;
1032: }
1033: }
1034: }
1035:
1036: # check if current user, and role match those expected.
1037: my ($next_url,$reuse_session);
1038: if (($env{'user.name'} eq $data{'username'}) && ($env{'user.domain'} eq $data{'domain'}) &&
1039: ($env{'request.role'} eq $data{'role'}) && ($data{'origurl'} ne '')) {
1040: $next_url = $data{'origurl'};
1041: $reuse_session = 1;
1042: } elsif ($data{'origurl'} =~ m{^/tiny/$match_domain/\w+$}) {
1043: $next_url=$data{'origurl'};
1044: } else {
1045: if ($data{'lti.login'}) {
1046: $next_url='/adm/roles?selectrole=1&'.&escape($data{'role'}).'=1';
1047: if ($data{'origurl'} ne '') {
1048: $next_url .= '&orgurl='.&escape($data{'origurl'});
1049: }
1050: } else {
1051: $next_url='/adm/roles?selectrole=1&'.&escape($data{'role'}).'=1';
1052: if ($data{'origurl'} ne '') {
1053: $next_url .= '&orgurl='.&escape($data{'origurl'});
1054: }
1055: }
1056: }
1057: if ($data{'lti.login'}) {
1058: if (($data{'origurl'} =~ m{/default_\d+\.sequence$}) ||
1059: ($data{'origurl'} =~ m{^/res/.+\.sequence$})) {
1060: if ($reuse_session) {
1061: $next_url .= '&navmap=1';
1062: } else {
1063: $next_url .= '&navmap=1';
1064: }
1065: }
1066: }
1067: if ($reuse_session) {
1068: $r->internal_redirect($next_url);
1069: } else {
1070: if ($data{'deeplink.login'}) {
1071: if (ref($extra_env) eq 'HASH') {
1072: $extra_env->{'request.deeplink.login'} = $data{'deeplink.login'};
1073: } else {
1074: $extra_env = {'request.deeplink.login' => $data{'deeplink.login'}};
1075: }
1076: if ($data{'linkprot'}) {
1077: $extra_env->{'request.linkprot'} = $data{'linkprot'};
1078: if ($data{'linkprotexit'}) {
1079: $extra_env->{'request.linkprotexit'} = $data{'linkprotexit'};
1080: }
1081: foreach my $item ('linkprotuser','linkprotexit') {
1082: if ($data{$item}) {
1083: $form{'request.'.$item} = $data{$item};
1084: }
1085: }
1086: } elsif ($data{'linkkey'} ne '') {
1087: $extra_env->{'request.linkkey'} = $data{'linkkey'};
1088: }
1089: }
1090: &Apache::lonauth::success($r,$data{'username'},$data{'domain'},$home,
1091: $next_url,$extra_env,\%form,'',$cid,'',$data{'lcssowin'});
1092: }
1093: return OK;
1094: }
1095:
1096: 1;
1097: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>