Annotation of loncom/auth/migrateuser.pm, revision 1.44
1.1 albertel 1: # The LearningOnline Network
2: # Starts a user off based of an existing token.
3: #
1.44 ! raeburn 4: # $Id: migrateuser.pm,v 1.43 2019/01/27 16:02:43 raeburn Exp $
1.1 albertel 5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28:
1.2 albertel 29: package Apache::migrateuser;
1.1 albertel 30:
31: use strict;
1.19 raeburn 32: use LONCAPA qw(:DEFAULT :match);
1.1 albertel 33: use Apache::Constants qw(:common :http :methods);
34: use Apache::lonauth;
35: use Apache::lonnet;
1.37 raeburn 36: use Apache::loncommon;
1.6 albertel 37: use Apache::lonlocal;
1.18 raeburn 38: use Apache::lonlogin();
1.27 raeburn 39: use Apache::ltiauth;
1.37 raeburn 40: use Apache::switchserver;
1.28 raeburn 41: use CGI::Cookie;
1.1 albertel 42:
43: sub goto_login {
1.27 raeburn 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: }
1.2 albertel 60: return OK;
1.1 albertel 61: }
62:
1.27 raeburn 63: sub sso_check {
1.7 albertel 64: my ($data) = @_;
1.8 albertel 65: my %extra_env;
1.18 raeburn 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: }
1.35 raeburn 74: if (($data->{'balancer'}) && ($data->{'server'}) && ($data->{'balcookie'})) {
75: $extra_env{'request.balancercookie'} = $data->{'server'}.':'.$data->{'balcookie'};
76: }
1.27 raeburn 77: }
78: return \%extra_env;
79: }
80:
81: sub lti_check {
82: my ($data) = @_;
83: my %lti_env;
84: if (ref($data) eq 'HASH') {
1.26 raeburn 85: if ($data->{'lti.login'}) {
1.27 raeburn 86: $lti_env{'request.lti.login'} = $data->{'lti.login'};
87: if ($data->{'lti.reqcrs'}) {
88: $lti_env{'request.lti.reqcrs'} = $data->{'lti.reqcrs'};
89: }
90: if ($data->{'lti.reqrole'}) {
91: $lti_env{'request.lti.reqrole'} = $data->{'lti.reqrole'};
92: }
93: if ($data->{'lti.selfenrollrole'}) {
94: $lti_env{'request.lti.selfenrollrole'} = $data->{'lti.selfenrollrole'};
95: }
1.29 raeburn 96: if ($data->{'lti.uri'}) {
97: $lti_env{'request.lti.uri'} = $data->{'lti.uri'};
98: }
99: if ($data->{'lti.target'}) {
100: $lti_env{'request.lti.target'} = $data->{'lti.target'};
101: }
1.32 raeburn 102: if ($data->{'lti.sourcecrs'}) {
103: $lti_env{'request.lti.sourcecrs'} = $data->{'lti.sourcecrs'};
104: }
1.26 raeburn 105: }
106: if ($data->{'lti.passbackid'}) {
1.27 raeburn 107: $lti_env{'request.lti.passbackid'} = $data->{'lti.passbackid'};
1.26 raeburn 108: }
109: if ($data->{'lti.passbackurl'}) {
1.27 raeburn 110: $lti_env{'request.lti.passbackurl'} = $data->{'lti.passbackurl'};
1.26 raeburn 111: }
112: if ($data->{'lti.rosterid'}) {
1.27 raeburn 113: $lti_env{'request.lti.rosterid'} = $data->{'lti.rosterid'};
1.26 raeburn 114: }
115: if ($data->{'lti.rosterurl'}) {
1.27 raeburn 116: $lti_env{'request.lti.rosterurl'} = $data->{'lti.rosterurl'};
1.26 raeburn 117: }
1.44 ! raeburn 118: if (($data->{'balancer'}) && ($data->{'server'}) && ($data->{'balcookie'})) {
! 119: $lti_env{'request.balancercookie'} = $data->{'server'}.':'.$data->{'balcookie'};
! 120: }
1.7 albertel 121: }
1.27 raeburn 122: return \%lti_env;
1.18 raeburn 123: }
124:
1.39 raeburn 125: sub canhost {
126: my ($uname,$udom,$lonhost,$loncaparev) = @_;
127: my $canhost;
128: if (&Apache::lonnet::is_library($lonhost)) {
129: my @possdoms = &Apache::lonnet::current_machine_domains();
130: my %roleshash = &Apache::lonnet::get_my_roles($uname,$udom,'userroles','',['ca','aa'],\@possdoms);
131: if (keys(%roleshash)) {
132: foreach my $key (keys(%roleshash)) {
1.40 raeburn 133: my $audom = (split(/:/,$key))[1];
1.39 raeburn 134: if ((&Apache::lonnet::will_trust('othcoau',$udom,$audom)) &&
135: (&Apache::lonnet::will_trust('coaurem',$audom,$udom))) {
136: $canhost = 1;
137: last;
138: }
139: }
140: }
141: }
142: unless ($canhost) {
143: my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
144: my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
145: my @intdoms;
146: my $internet_names = &Apache::lonnet::get_internet_names($lonhost);
147: if (ref($internet_names) eq 'ARRAY') {
148: @intdoms = @{$internet_names};
149: }
150: if ($uint_dom ne '' && grep(/^\Q$uint_dom\E$/,@intdoms)) {
151: $canhost = 1;
152: } else {
153: my $hostname = &Apache::lonnet::hostname($lonhost);
154: my $serverhomeID = &Apache::lonnet::get_server_homeID($hostname);
155: my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);
156: my %defdomdefaults = &Apache::lonnet::get_domain_defaults($serverhomedom);
157: my %udomdefaults = &Apache::lonnet::get_domain_defaults($udom);
158: $canhost =
159: &Apache::lonnet::can_host_session($udom,$lonhost,$loncaparev,
160: $udomdefaults{'remotesessions'},
161: $defdomdefaults{'hostedsessions'});
162: }
163: }
164: return $canhost;
165: }
166:
1.18 raeburn 167: sub ip_changed {
1.37 raeburn 168: my ($r,$udom,$camefrom,$idsref,$dataref) = @_;
1.18 raeburn 169: &Apache::loncommon::content_type($r,'text/html');
170: $r->send_http_header;
171: if (ref($dataref) eq 'HASH') {
172: my $title = 'LON-CAPA Session redirected';
173: my $message = &mt('Your internet address has changed since you logged in.');
174: my $rule_in_effect;
1.22 raeburn 175: if ($dataref->{'balancer'}) {
176: my $baldom = &Apache::lonnet::host_domain($camefrom);
177: my $balprimaryid = &Apache::lonnet::domain($baldom,'primary');
178: my $balintdom = &Apache::lonnet::internet_dom($balprimaryid);
179: my $uprimaryid = &Apache::lonnet::domain($udom,'primary');
180: my $uintdom = &Apache::lonnet::internet_dom($uprimaryid);
181: my $dom_in_use;
182: if (($uintdom ne '') && ($uintdom eq $balintdom)) {
183: $dom_in_use = $udom;
184: } else {
185: $dom_in_use = $baldom;
1.21 raeburn 186: }
1.22 raeburn 187: my ($result,$cached)=&Apache::lonnet::is_cached_new('loadbalancing',$dom_in_use);
1.18 raeburn 188: unless (defined($cached)) {
189: my $cachetime = 60*60*24;
190: my %domconfig =
1.22 raeburn 191: &Apache::lonnet::get_dom('configuration',['loadbalancing'],$dom_in_use);
1.18 raeburn 192: if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1.22 raeburn 193: $result = &Apache::lonnet::do_cache_new('loadbalancing',$dom_in_use,
1.18 raeburn 194: $domconfig{'loadbalancing'},$cachetime);
195: }
196: }
197: if (ref($result) eq 'HASH') {
198: (undef,my $currtargets,my $currrules) =
199: &Apache::lonnet::check_balancer_result($result,$dataref->{'server'});
200: if (ref($currrules) eq 'HASH') {
201: if ($dataref->{'sso.login'}) {
202: if ($currrules->{'_LC_ipchangesso'} ne '') {
203: $rule_in_effect = $currrules->{'_LC_ipchangesso'};
204: }
205: } else {
206: if ($currrules->{'_LC_ipchange'} ne '') {
207: $rule_in_effect = $currrules->{'_LC_ipchange'};
208: }
209: }
210: }
211: }
212: }
213: my $url;
214: my $lonhost= $r->dir_config('lonHostID');
215: my $switchto = $lonhost;
1.23 raeburn 216: if ($rule_in_effect ne 'offloadedto') {
1.37 raeburn 217: my ($hosthere,@ids);
218: if (ref($idsref) eq 'ARRAY') {
219: @ids=@{$idsref};
220: }
1.23 raeburn 221: unless ($rule_in_effect eq 'balancer') {
222: if (grep(/^\Q$rule_in_effect\E$/,@ids)) {
223: $hosthere = 1;
224: }
225: }
226: unless ($hosthere) {
227: if ($dataref->{'role'}) {
228: my ($adom,$aname);
229: if ($dataref->{'role'} =~ m{^au\./($match_domain)/$}) {
230: $adom = $1;
231: $aname = $dataref->{'username'};
232: } elsif ($dataref->{'role'} =~ m{^(?:ca|aa)\./($match_domain)/($match_username)$}) {
233: $adom = $1;
234: $aname = $2;
235: }
236: if ($adom ne '' && $aname ne '') {
237: my $ahome = &Apache::lonnet::homeserver($aname,$adom);
238: unless ($ahome eq 'no_host') {
239: if ($ahome && grep(/^\Q$ahome\E$/,@ids)) {
240: $hosthere = 1;
241: }
1.18 raeburn 242: }
243: }
244: }
245: }
1.23 raeburn 246: unless ($hosthere) {
247: my $hostname;
248: if ($rule_in_effect eq 'balancer') {
249: $hostname = &Apache::lonnet::hostname($dataref->{'server'});
250: if ($hostname) {
251: $switchto = $dataref->{'server'};
252: }
253: } else {
254: $hostname = &Apache::lonnet::hostname($rule_in_effect);
255: if ($hostname) {
256: $switchto = $rule_in_effect;
257: }
258: }
1.18 raeburn 259: if ($hostname) {
260: my $protocol = $Apache::lonnet::protocol{$switchto};
261: $protocol = 'http' if ($protocol ne 'https');
262: $url = $protocol.'://'.$hostname;
1.23 raeburn 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: }
1.18 raeburn 270: }
271: }
1.35 raeburn 272: unless ($hosthere) {
273: if (($dataref->{'balancer'}) && ($dataref->{'balcookie'})) {
274: &Apache::lonnet::delbalcookie($dataref->{'balcookie'},$dataref->{'balancer'});
275: }
276: }
1.18 raeburn 277: }
278: if ($dataref->{'sso.login'}) {
1.21 raeburn 279: $url .= '/adm/roles';
1.18 raeburn 280: } else {
1.21 raeburn 281: $url .= '/adm/login';
1.24 raeburn 282: if ($udom) {
283: $url .= '?domain='.$udom;
284: }
1.20 raeburn 285: $message .= '<br />'.&mt('You will need to provide your password one more time.');
1.18 raeburn 286: }
287: my %info= (
1.24 raeburn 288: 'domain' => $udom,
1.18 raeburn 289: 'username' => $dataref->{'username'},
290: 'role' => $dataref->{'role'},
291: 'sessionserver' => $lonhost,
292: );
293: if ($dataref->{'origurl'}) {
294: $info{'origurl'} = $dataref->{'origurl'};
295: }
296: if ($dataref->{'symb'}) {
297: $info{'symb'} = $dataref->{'symb'};
298: }
299: my $iptoken = &Apache::lonnet::tmpput(\%info,$switchto);
300: unless ($iptoken eq 'conlost') {
1.24 raeburn 301: $url .= ($url =~ /\?/) ? '&' : '?';
302: $url .= 'iptoken='.$iptoken;
1.18 raeburn 303: }
304: $r->print(&Apache::loncommon::start_page($title,undef,
305: {'redirect' =>
306: [2,$url],}).
307: '<h1>'.&mt('One moment please...').'</h1>'.
308: '<p class="LC_warning">'.$message.'</p>'.
309: &Apache::loncommon::end_page());
310: } else {
311: return &goto_login($r);
1.10 raeburn 312: }
1.18 raeburn 313: return OK;
1.7 albertel 314: }
315:
1.28 raeburn 316: sub logout {
317: my ($r,$handle,$data,$lti_env) = @_;
1.34 raeburn 318: my $lonidsdir=$r->dir_config('lonIDsDir');
319: if (unlink("$lonidsdir/$handle.id")) {
320: if (($env{'user.linkedenv'} =~ /^[a-f0-9]+_linked$/) &&
321: (-l "$lonidsdir/$env{'user.linkedenv'}.id") &&
322: (readlink("$lonidsdir/$env{'user.linkedenv'}.id") eq "$lonidsdir/$handle.id")) {
323: unlink("$lonidsdir/$env{'user.linkedenv'}.id");
1.28 raeburn 324: }
325: }
326: my %temp=('logout' => time);
327: &Apache::lonnet::put('email_status',\%temp);
328: &Apache::lonnet::log($env{'user.domain'},
329: $env{'user.name'},
330: $env{'user.home'},
331: "Logout $ENV{'REMOTE_ADDR'}");
332:
333: &Apache::loncommon::content_type($r,'text/html');
334:
1.34 raeburn 335: #expire the cookies
336: my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
337: foreach my $name (keys(%cookies)) {
338: next unless ($name =~ /^lon(|S|Link|Pub)ID$/);
339: my $c = new CGI::Cookie(-name => $name,
340: -value => '',
341: -expires => '-10y',);
342: $r->headers_out->add('Set-cookie' => $c);
1.28 raeburn 343: }
344: my (%info,%user_info,%lti_info);
345: if (ref($lti_env) eq 'HASH') {
1.44 ! raeburn 346: foreach my $key (sort(keys(%{$lti_env}))) {
! 347: if ($key =~ /^request\.(.+)$/) {
! 348: $lti_info{$1} = $lti_env->{$key};
! 349: }
! 350: }
1.28 raeburn 351: }
352: my $lonhost = $r->dir_config('lonHostID');
353: if (ref($data) eq 'HASH') {
354: %user_info=('ip' => $ENV{'REMOTE_ADDR'},
355: 'domain' => $data->{'domain'},
356: 'username' => $data->{'username'},
1.36 raeburn 357: 'home' => $data->{'home'},
1.28 raeburn 358: 'role' => $data->{'role'},
359: 'origurl' => $data->{'origurl'},
360: 'symb' => $data->{'symb'},
361: 'server' => $lonhost);
362: }
363: %info = (%user_info,%lti_info);
364: my $token = &Apache::lonnet::tmpput(\%info,$lonhost);
365: my $url = '/adm/migrateuser?token='.$token;
366: $r->send_http_header;
367: $r->print(
368: &Apache::loncommon::start_page('Updating Session ...',undef,
369: {'redirect' => [0.1,$url],
370: 'only_body' => 1,}).
371: &Apache::loncommon::end_page());
1.44 ! raeburn 372: if ($env{'request.balancercookie'}) {
! 373: my ($balancer,$cookie) = split(/:/,$env{'request.balancercookie'});
! 374: if ((&Apache::lonnet::hostname($balancer)) && ($cookie =~ /^[a-f0-9]{32}$/)) {
! 375: $cookie = $env{'user.domain'}.'_'.$env{'user.name'}.'_'.$cookie;
! 376: &Apache::lonnet::delbalcookie($cookie,$balancer);
! 377: }
! 378: }
1.28 raeburn 379: $r->register_cleanup(\&flush_course_logs);
380: return;
381: }
382:
1.36 raeburn 383: sub conlost_userhome {
384: my ($r,$idsref,$dataref) = @_;
385: return unless ((ref($idsref) eq 'ARRAY') && (ref($dataref) eq 'HASH'));
386: my @ids = @{$idsref};
387: my %data = %{$dataref};
1.37 raeburn 388: my ($lonhost,$lowest_load,$otherserver,$is_balancer,%conlost,%posstargets);
389: $lonhost = $r->dir_config('lonHostID');
390: if (grep(/^\Q$lonhost\E$/,split(/,/,$data{'dom_balancers'}))) {
391: $is_balancer = 1;
392: }
393: $lowest_load = 30000;
1.36 raeburn 394: if ($data{'conlost'} ne '') {
395: map { $conlost{$_} = 1; } split(/,/,$data{'conlost'});
396: }
1.37 raeburn 397: if ($data{'loncfail'}) {
1.38 raeburn 398: if ($lonhost ne $data{'from_balancer'}) {
399: my ($is_balancer,$posshost,$setcookie,$offloadto,$dom_balancers) =
400: &Apache::lonnet::check_loadbalancing($data{'username'},$data{'domain'});
401: if ($is_balancer) {
402: if (ref($offloadto) eq 'HASH') {
403: $data{'offloadto'} = '';
404: foreach my $key (keys(%{$offloadto})) {
405: if (ref($offloadto->{$key}) eq 'ARRAY') {
406: $data{'offloadto'} .= $key.'='.join(',',@{$offloadto->{$key}}).'&';
1.37 raeburn 407: }
408: }
1.38 raeburn 409: $data{'offloadto'} =~ s/\&$//;
410: } elsif (ref($offloadto) eq 'ARRAY') {
411: $data{'offloadto'} = join(',',@{$offloadto});
1.37 raeburn 412: }
413: }
414: }
415: } else {
416: $conlost{$lonhost} = 1;
417: $data{'conlost'} = join(',',sort(keys(%conlost)));
418: }
1.36 raeburn 419: if ($data{'offloadto'} =~ /\&/) {
1.37 raeburn 420: foreach my $item (split(/\&/,$data{'offloadto'})) {
1.36 raeburn 421: my ($type,$targets) = split(/\=/,$item);
422: @{$posstargets{$type}} = split(/,/,$targets);
423: }
424: } elsif ($data{'offloadto'} =~ /=/) {
425: my ($type,$targets) = split(/\=/,$data{'offloadto'});
426: @{$posstargets{$type}} = split(/,/,$targets);
427: } else {
428: @{$posstargets{'default'}} = split(/,/,$data{'offloadto'});
429: }
430: if (ref($posstargets{'primary'}) eq 'ARRAY') {
431: foreach my $try_server (@{$posstargets{'primary'}}) {
432: next if ($conlost{$try_server});
1.37 raeburn 433: next if ((grep(/^\Q$try_server\E$/,@ids)) && !$data{'loncfail'});
1.36 raeburn 434: ($otherserver,$lowest_load) =
435: &Apache::lonnet::compare_server_load($try_server,
436: $otherserver,
437: $lowest_load);
438: }
439: }
440: my $found_server = ($otherserver ne '' && $lowest_load < 100);
441: if (!$found_server) {
442: if (ref($posstargets{'default'}) eq 'ARRAY') {
443: foreach my $try_server (@{$posstargets{'default'}}) {
1.37 raeburn 444: next if ($conlost{$try_server});
445: next if ((grep(/^\Q$try_server\E$/,@ids)) && !$data{'loncfail'});
1.36 raeburn 446: ($otherserver,$lowest_load) =
1.37 raeburn 447: &Apache::lonnet::compare_server_load($try_server,
448: $otherserver,
449: $lowest_load);
1.36 raeburn 450: }
451: }
452: }
453: if ($otherserver ne '') {
454: my $switchto = &Apache::lonnet::hostname($otherserver);
455: if ($switchto ne '') {
456: $data{'server'} = $lonhost;
1.37 raeburn 457: if ($data{'loncfail'}) {
458: delete($data{'loncfail'});
459: }
1.36 raeburn 460: if (grep(/^\Q$otherserver\E$/,split(/,/,$data{'dom_balancers'}))) {
1.37 raeburn 461: $data{'noloadbalance'} = $otherserver;
1.36 raeburn 462: }
1.37 raeburn 463: &do_server_switch($r,$otherserver,$switchto,$is_balancer,\%data);
1.36 raeburn 464: return $otherserver;
1.37 raeburn 465: } else {
466: #FIXME Contents of $data{offloadto} contains invalid hostID.
467: }
468: } else {
469: my (%poss_balancers,%tried_balancers);
470: map { $poss_balancers{$_} = 1; } split(/,/,$data{'dom_balancers'});
471: map { $tried_balancers{$_} = 1; } split(/,/,$data{'tried_balancers'});
472: if (keys(%poss_balancers)) {
473: foreach my $try_server (sort(keys(%poss_balancers))) {
474: next if (grep(/^\Q$try_server\E$/,@ids));
475: next if (($data{'loncfail'}) && ($tried_balancers{$try_server}));
476: my $alreadytried;
477: foreach my $key (keys(%posstargets)) {
478: if ((ref($posstargets{$key}) eq 'ARRAY') &&
479: (grep(/^\Q$try_server\E$/,@{$posstargets{$key}}))) {
480: $alreadytried = 1;
481: last;
482: }
483: }
484: unless ($alreadytried) {
485: if (&Apache::lonnet::reply('ping',$try_server) eq $try_server) {
486: $otherserver = $try_server;
487: last;
488: }
489: }
490: }
491: }
492: if ($otherserver ne '') {
493: my $switchto = &Apache::lonnet::hostname($otherserver);
494: if ($switchto ne '') {
495: $data{'server'} = $lonhost;
496: $data{'loncfail'} = $lonhost;
497: $tried_balancers{$otherserver} = 1;
498: $data{'tried_balancers'} = join(',',sort(keys(%tried_balancers)));
499: &do_server_switch($r,$otherserver,$switchto,$is_balancer,\%data);
500: return $otherserver;
501: } else {
502: #FIXME Contents of $data{'dom_balancers'} contains invalid hostID.
1.39 raeburn 503: }
1.37 raeburn 504: } else {
505: if ($data{'loncfail'}) {
506: #FIXME Nowhere to go.
507: } else {
508: # FIXME Send back to a balancer (no token); different balancer if this is a balancer.
509: # Send command to balancer to exclude this host using http instead on lonc/lond
510: }
1.36 raeburn 511: }
512: }
1.37 raeburn 513: return;
514: }
515:
516: sub do_server_switch {
517: my ($r,$otherserver,$switchto,$is_balancer,$dataref) = @_;
518: if (ref($dataref) eq 'HASH') {
519: my $domain = $dataref->{'domain'};
520: my $username = $dataref->{'username'};
521: # If this is a balancer set a balancer cookie unless browser already sent
522: # LON-CAPA load balancer cookie which points at the target server.
523: if ($is_balancer) {
524: my $newcookieid;
525: my ($found_server,$balancer_cookie) = &Apache::lonnet::check_for_balancer_cookie($r,1);
526: unless (($found_server eq $otherserver) &&
527: ($balancer_cookie =~ /^\Q$domain\E_\Q$username\E_/)) {
528: $newcookieid =
529: &Apache::switchserver::balancer_cookieid($r,$otherserver,$username,$domain);
530: if ($newcookieid) {
531: $dataref->{'balcookie'} = $newcookieid;
532: }
533: }
534: }
535: my $token = &Apache::lonnet::tmpput($dataref,$otherserver);
536: my $protocol = $Apache::lonnet::protocol{$otherserver};
537: $protocol = 'http' if ($protocol ne 'https');
538: my $url = $protocol.'://'.$switchto.'/adm/login?domain='.$domain.
539: '&username='.$username.'&token='.$token;
540: $r->send_http_header;
541: $r->print(
542: &Apache::loncommon::start_page('Switching Server ...',undef,
543: {'redirect' => [0.1,$url]}).
544: &Apache::loncommon::end_page());
545: }
546: return;
1.36 raeburn 547: }
548:
549: sub log_switch {
550: my ($r,$data,$lti_env) = @_;
551: my $lonhost = $r->dir_config('lonHostID');
552: return unless ((ref($data) eq 'HASH') && (ref($lti_env) eq 'HASH'));
553: my $now = time;
554: my %temp=('switchserver' => $now.':'.$lonhost,$data->{'role'});
555: &Apache::lonnet::put('email_status',\%temp);
556: my $logmsg = "Switch Server to $lonhost";
557: if ($data->{'role'}) {
558: $logmsg .= " with role: ".$data->{'role'};
559: } elsif (($lti_env->{'reqcrs'}) && ($lti_env->{'reqrole'} eq 'cc')) {
560: $logmsg .= " to create new LTI course";
561: } elsif ($lti_env->{'selfenrollrole'}) {
562: $logmsg .= " to selfenroll with role: ".$lti_env->{'selfenrollrole'};
563: } else {
564: $logmsg .= " (no role)";
565: }
566: $logmsg .= ' '.$ENV{'REMOTE_ADDR'};
567: &Apache::lonnet::log($data->{'domain'},$data->{'username'},$data->{'home'},$logmsg);
568: }
569:
1.28 raeburn 570: sub flush_course_logs {
571: &Apache::lonnet::flushcourselogs();
572: return OK;
573: }
574:
1.1 albertel 575: sub handler {
576: my ($r) = @_;
577:
578: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['token']);
1.4 albertel 579: my %data = &Apache::lonnet::tmpget($env{'form.token'});
1.13 raeburn 580: if (keys(%data) == 0) {
581: return &goto_login($r);
582: }
1.4 albertel 583: my $delete = &Apache::lonnet::tmpdel($env{'form.token'});
584:
1.6 albertel 585: &Apache::lonlocal::get_language_handle($r);
586:
1.4 albertel 587: if ($delete ne 'ok') {
1.27 raeburn 588: return &goto_login($r,undef,\%data);
1.4 albertel 589: }
1.2 albertel 590:
1.18 raeburn 591: if (!defined($data{'username'}) || !defined($data{'domain'})) {
1.27 raeburn 592: return &goto_login($r,undef,\%data);
1.18 raeburn 593: }
1.37 raeburn 594: my ($home,@ids);
595: @ids=&Apache::lonnet::current_machine_ids();
1.18 raeburn 596: if ($data{'ip'} ne $ENV{'REMOTE_ADDR'}) {
1.24 raeburn 597: &Apache::lonnet::logthis('IP change when session migration requested -- was: '.
598: $data{'ip'}.'; now: '.$ENV{'REMOTE_ADDR'}.' for '.$data{'username'}.':'.$data{'domain'});
1.37 raeburn 599: return &ip_changed($r,$data{'domain'},$data{'server'},\@ids,\%data);
1.2 albertel 600: }
1.37 raeburn 601: if ($data{'loncfail'}) {
602: &Apache::lonnet::logthis('Returned from '.$data{'server'}.' -- no connection to library node or other access node(s)');
603: my $otherserver = &conlost_userhome($r,\@ids,\%data);
604: if ($otherserver ne '') {
605: &Apache::lonnet::logthis("Switching to $otherserver");
606: }
607: return OK;
608: } else {
609: $home=&Apache::lonnet::homeserver($data{'username'},$data{'domain'});
610: if ($home eq 'no_host') {
611: if (($data{'home'} ne '') && (&Apache::lonnet::hostname($data{'home'}))) {
612: &Apache::lonnet::reconlonc($data{'home'});
613: $home=&Apache::lonnet::homeserver($data{'username'},$data{'domain'});
614: if ($home eq 'no_host') {
615: unless (grep(/^\Q$data{'home'}\E$/,@ids)) {
616: if (&Apache::lonnet::reply('ping',$data{'home'}) eq 'con_lost') {
617: my $otherserver = &conlost_userhome($r,\@ids,\%data);
618: if ($otherserver ne '') {
619: &Apache::lonnet::logthis("No connection to home server ($data{'home'}) for $data{'username'}:$data{'domain'}. Switching to $otherserver");
620: }
1.36 raeburn 621: return OK;
622: }
623: }
624: }
625: }
626: }
627: }
628:
1.24 raeburn 629: my $udom;
630: if (&Apache::lonnet::domain($data{'domain'})) {
631: $udom=$data{'domain'};
632: }
1.36 raeburn 633: if ($home eq 'no_host') { return &goto_login($r,$udom,\%data); }
1.38 raeburn 634: if (&Apache::lonnet::hostname($home) eq '') { return &goto_login($r,$udom,\%data); }
1.36 raeburn 635:
1.39 raeburn 636: unless (grep(/^\Q$home\E$/,@ids)) {
637: my $lonhost = $r->dir_config('lonHostID');
638: my $loncaparev = $r->dir_config('lonVersion');
639: unless (&canhost($data{'username'},$data{'domain'},$lonhost,$loncaparev)) {
640: return &goto_login($r,$udom,\%data);
641: }
642: }
1.41 raeburn 643:
1.36 raeburn 644: my $rolemsg;
645: if ($data{'role'}) {
1.38 raeburn 646: $rolemsg = "role: $data{'role'}";
1.36 raeburn 647: } else {
648: $rolemsg = '(no role)';
649: }
650:
651: &Apache::lonnet::logthis("Allowing access for $data{'username'}:$data{'domain'} $rolemsg");
1.27 raeburn 652:
653: my $sso_env = &sso_check(\%data);
654: my $lti_env = <i_check(\%data);
1.2 albertel 655:
1.27 raeburn 656: my $extra_env;
657: if ((ref($sso_env) eq 'HASH') && (keys(%{$sso_env}))) {
658: $extra_env = $sso_env;
659: } elsif ((ref($lti_env) eq 'HASH') && (keys(%{$lti_env}))) {
660: $extra_env = $lti_env;
661: }
1.8 albertel 662:
1.37 raeburn 663: if (($data{'conlost'}) && ($data{'server'})) {
664: my @conlosts = split(/,/,$data{'conlost'});
665: my $switchfrom = $data{'server'};
666: if (@conlosts) {
667: if (grep(/^\Q$switchfrom\E$/,@conlosts)) {
668: &log_switch($r,\%data,$extra_env);
669: }
670: }
671: }
672:
1.16 raeburn 673: my %form;
674: if ($data{'symb'} ne '') {
675: $form{'symb'} = $data{'symb'};
676: }
1.21 raeburn 677: if ($data{'iptoken'} ne '') {
678: $form{'iptoken'} = $data{'iptoken'};
679: }
1.25 raeburn 680: if ($data{'noloadbalance'} ne '') {
681: $form{'noloadbalance'} = $data{'noloadbalance'};
682: }
1.16 raeburn 683:
1.3 albertel 684: if (!$data{'role'}) {
1.12 albertel 685: my $handle = &Apache::lonnet::check_for_valid_session($r);
686: if ($handle) {
1.11 albertel 687: &Apache::lonnet::transfer_profile_to_env($r->dir_config('lonIDsDir'),
688: $handle);
1.42 raeburn 689: if ($data{'linkprot'} ne '') {
690: my ($linkprotector,$deeplink) = split(/:/,$data{'linkprot'},2);
1.43 raeburn 691: if ($deeplink ne '') {
692: &Apache::lonnet::appenv({'request.deeplink.login' => $deeplink});
693: }
1.42 raeburn 694: if ($env{'user.linkprotector'}) {
695: my @protectors = split(/,/,$env{'user.linkprotector'});
696: unless (grep(/^\Q$linkprotector\E$/,@protectors)) {
697: push(@protectors,$linkprotector);
698: @protectors = sort { $a <=> $b } @protectors;
699: &Apache::lonnet::appenv({'user.linkprotector' => join(',',@protectors)});
700: }
701: } else {
702: &Apache::lonnet::appenv({'user.linkprotector' => $linkprotector });
1.43 raeburn 703: }
1.42 raeburn 704: if ($env{'user.linkproturi'}) {
705: my @proturis = split(/,/,$env{'user.linkproturi'});
706: unless(grep(/^\Q$deeplink\E$/,@proturis)) {
707: push(@proturis,$deeplink);
708: @proturis = sort @proturis;
709: &Apache::lonnet::appenv({'user.linkproturi' => join(',',@proturis)});
710: }
711: } else {
712: &Apache::lonnet::appenv({'user.linkproturi' => $deeplink});
713: }
1.43 raeburn 714: } elsif ($data{'deeplink.login'}) {
715: my $deeplink = $data{'deeplink.login'};
716: if ($data{'linkkey'}) {
717: my $linkkey = $data{'linkkey'};
718: if ($env{'user.deeplinkkey'}) {
719: my @linkkeys = split(/,/,$env{'user.deeplinkkey'});
720: unless (grep(/^\Q$linkkey\E$/,@linkkeys)) {
721: push(@linkkeys,$linkkey);
722: &Apache::lonnet::appenv({'user.deeplinkkey' => join(',',sort(@linkkeys))});
723: }
724: } else {
725: &Apache::lonnet::appenv({'user.deeplinkkey' => $linkkey});
726: }
727: if ($env{'user.keyedlinkuri'}) {
728: my @keyeduris = split(/,/,$env{'user.keyedlinkuri'});
729: unless (grep(/^\Q$deeplink\E$/,@keyeduris)) {
730: push(@keyeduris,$deeplink);
731: &Apache::lonnet::appenv({'user.keyedlinkuri' => join(',',sort(@keyeduris))});
732: }
733: } else {
734: &Apache::lonnet::appenv({'user.keyedlinkuri' => $deeplink});
735: }
736: }
737: &Apache::lonnet::appenv({'request.deeplink.login' => $data{'deeplink.login'}});
1.42 raeburn 738: }
1.27 raeburn 739: if ($data{'lti.login'}) {
1.28 raeburn 740: my $needslogout;
741: if ($env{'request.lti.login'}) {
742: if (($env{'user.name'} ne $data{'username'}) ||
743: ($env{'user.domain'} ne $data{'domain'})) {
744: $needslogout = 1;
745: }
746: } else {
747: $needslogout = 1;
748: }
749: # If access is via LTI, and user already has a non-LTI session cookie
750: # (and session) or has an LTI session cookie for a different username,
751: # logout the existing session, and start a new one
752: if ($needslogout) {
753: &logout($r,$handle,\%data,$lti_env);
754: } elsif (($data{'lti.reqcrs'}) && ($data{'lti.reqrole'} eq 'cc')) {
1.27 raeburn 755: $form{'lti.reqcrs'} = $data{'lti.reqcrs'};
756: $form{'lti.reqrole'} = $data{'lti.reqrole'};
757: $form{'lti.sourcecrs'} = $data{'lti.sourcecrs'};
1.29 raeburn 758: $form{'lti.uri'} = $data{'lti.uri'};
1.28 raeburn 759: if ($data{'lti.passbackid'}) {
760: $form{'lti.passbackid'} = $data{'lti.passbackid'};
761: }
762: if ($data{'lti.passbackurl'}) {
763: $form{'lti.passbackurl'} = $data{'lti.passbackurl'};
764: }
765: if ($data{'lti.rosterid'}) {
766: $form{'lti.rosterid'} = $data{'lti.rosterid'};
767: }
768: if ($data{'lti.rosterurl'}) {
769: $form{'lti.rosterurl'} = $data{'lti.rosterurl'};
770: }
1.29 raeburn 771: if ($data{'lti.target'}) {
772: $form{'lti.target'} = $data{'lti.target'};
773: }
1.27 raeburn 774: &Apache::loncommon::content_type($r,'text/html');
775: $r->send_http_header;
776: &Apache::ltiauth::lti_reqcrs($r,$data{'domain'},\%form,$data{'username'},$data{'domain'});
1.28 raeburn 777: } else {
778: if (ref($lti_env) eq 'HASH') {
779: delete($lti_env->{'reqcrs'});
780: delete($lti_env->{'reqrole'});
781: delete($lti_env->{'selfenrollrole'});
782: }
783: if ($data{'lti.selfenrollrole'}) {
1.32 raeburn 784: if (&Apache::ltiauth::lti_enroll($data{'username'},$data{'domain'},
1.28 raeburn 785: $data{'lti.selfenrollrole'}) eq 'ok') {
786: my $url = '/adm/roles?selectrole=1&'.
787: &escape($data{'lti.selfenrollrole'}).'=1';
788: if ($data{'origurl'} =~ m{/default_\d+\.sequence$}) {
789: $url .= '&orgurl='.$data{'origurl'}.'&navmap=1';
790: } elsif ($data{'origurl'} ne '') {
791: $url .= '&orgurl='.$data{'origurl'};
792: }
793: if (ref($lti_env) eq 'HASH') {
794: &Apache::lonnet::appenv($lti_env);
795: }
796: $r->internal_redirect($url);
797: } else {
798: &Apache::ltiauth::invalid_request($r,23);
799: }
800: } elsif ($data{'origurl'} ne '') {
801: my $url = $data{'origurl'};
802: if ($url =~ m{/default_\d+\.sequence$}) {
803: $url .= (($url =~/\?/)?'&':'?').'navmap=1';
804: }
805: if (ref($lti_env) eq 'HASH') {
806: &Apache::lonnet::appenv($lti_env);
1.27 raeburn 807: }
808: $r->internal_redirect($url);
809: } else {
1.28 raeburn 810: if (ref($lti_env) eq 'HASH') {
811: &Apache::lonnet::appenv($lti_env);
812: }
1.27 raeburn 813: }
814: }
815: } elsif ($data{'origurl'} ne '') {
1.14 raeburn 816: $r->internal_redirect($data{'origurl'});
817: } elsif ($env{'request.course.id'}) {
818: $r->internal_redirect('/adm/navmaps');
1.11 albertel 819: } else {
820: $r->internal_redirect('/adm/roles');
821: }
822: } else {
1.14 raeburn 823: my $desturl = '/adm/roles';
824: if ($data{'origurl'} ne '') {
825: $desturl = $data{'origurl'};
1.27 raeburn 826: if ($data{'lti.login'}) {
827: $desturl = $data{'origurl'};
828: if ($desturl =~ m{/default_\d+\.sequence$}) {
829: $desturl .= (($desturl =~/\?/)?'&':'?').'navmap=1';
830: }
831: }
1.42 raeburn 832: if ($data{'linkprot'}) {
833: my ($linkprotector,$linkuri) = split(/:/,$data{'linkprot'},2);
834: if ($linkprotector) {
835: if (ref($extra_env) eq 'HASH') {
836: $extra_env->{'user.linkprotector'} = $linkprotector;
837: $extra_env->{'user.linkproturi'} = $linkuri;
838: } else {
839: $extra_env = {'user.linkprotector' => $linkprotector,
840: 'user.linkproturi' => $linkuri,};
841: }
842: }
1.43 raeburn 843: } elsif ($data{'deeplink.login'}) {
844: if ($data{'linkkey'}) {
845: if (ref($extra_env) eq 'HASH') {
846: $extra_env->{'user.deeplinkkey'} = $data{'linkkey'};
847: $extra_env->{'user.keyedlinkuri'} = $data{'deeplink.login'},
848: } else {
849: $extra_env = {'user.deeplinkkey' => $data{'linkkey'},
850: 'user.keyedlinkuri' => $data{'deeplink.login'}};
851: }
852: }
853: if (ref($extra_env) eq 'HASH') {
854: $extra_env->{'request.deeplink.login' => $data{'deeplink.login'}};
855: } else {
856: $extra_env = {'request.deeplink.login' => $data{'deeplink.login'}};
857: }
1.42 raeburn 858: }
1.27 raeburn 859: }
860: my $skipcritical;
1.44 ! raeburn 861: if ($data{'lti.login'}) {
! 862: if ((($data{'lti.reqcrs'}) &&
! 863: ($data{'lti.reqrole'} eq 'cc')) ||
! 864: ($data{'lti.selfenrollrole'})) {
! 865: $skipcritical = 1;
! 866: }
1.14 raeburn 867: }
1.11 albertel 868: &Apache::lonauth::success($r,$data{'username'},$data{'domain'},
1.27 raeburn 869: $home,$desturl,$extra_env,\%form,$skipcritical);
1.11 albertel 870: }
1.1 albertel 871: return OK;
872: }
1.6 albertel 873:
1.44 ! raeburn 874: if ($data{'lti.login'}) {
! 875: my $handle = &Apache::lonnet::check_for_valid_session($r);
! 876: if ($handle) {
! 877: &Apache::lonnet::transfer_profile_to_env($r->dir_config('lonIDsDir'),
! 878: $handle);
! 879: my $needslogout;
! 880: if ($env{'request.lti.login'}) {
! 881: if (($env{'user.name'} ne $data{'username'}) ||
! 882: ($env{'user.domain'} ne $data{'domain'})) {
! 883: $needslogout = 1;
! 884: }
! 885: } else {
! 886: $needslogout = 1;
! 887: }
! 888: if ($needslogout) {
! 889: &logout($r,$handle,\%data,$lti_env);
! 890: return OK;
1.30 raeburn 891: }
892: }
1.15 raeburn 893: }
1.44 ! raeburn 894:
! 895: # check if current user, and role match those expected.
! 896: my ($next_url,$reuse_session);
! 897: if (($env{'user.name'} eq $data{'username'}) && ($env{'user.domain'} eq $data{'domain'}) &&
! 898: ($env{'request.role'} eq $data{'role'}) && ($data{'origurl'} ne '')) {
! 899: $next_url = $data{'origurl'};
! 900: $reuse_session = 1;
! 901: } else {
! 902: $next_url='/adm/roles?selectrole=1&'.&escape($data{'role'}).'=1';
! 903: if ($data{'origurl'} ne '') {
! 904: $next_url .= '&orgurl='.&escape($data{'origurl'});
! 905: }
! 906: }
! 907: if ($data{'lti.login'}) {
! 908: if (($data{'origurl'} =~ m{/default_\d+\.sequence$}) ||
! 909: ($data{'origurl'} =~ m{^/res/.+\.sequence$})) {
! 910: $next_url .= '&navmap=1';
! 911: }
! 912: }
! 913: if ($reuse_session) {
! 914: $r->internal_redirect($next_url);
! 915: } else {
! 916: &Apache::lonauth::success($r,$data{'username'},$data{'domain'},$home,
! 917: $next_url,$extra_env,\%form);
! 918: }
1.6 albertel 919: return OK;
1.1 albertel 920: }
921:
922: 1;
923: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>