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