Annotation of loncom/auth/migrateuser.pm, revision 1.49
1.1 albertel 1: # The LearningOnline Network
2: # Starts a user off based of an existing token.
3: #
1.49 ! raeburn 4: # $Id: migrateuser.pm,v 1.48 2020/10/22 19:23:22 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.27 raeburn 74: }
75: return \%extra_env;
76: }
77:
78: sub lti_check {
79: my ($data) = @_;
80: my %lti_env;
81: if (ref($data) eq 'HASH') {
1.26 raeburn 82: if ($data->{'lti.login'}) {
1.27 raeburn 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: }
1.29 raeburn 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: }
1.32 raeburn 99: if ($data->{'lti.sourcecrs'}) {
100: $lti_env{'request.lti.sourcecrs'} = $data->{'lti.sourcecrs'};
101: }
1.26 raeburn 102: }
103: if ($data->{'lti.passbackid'}) {
1.27 raeburn 104: $lti_env{'request.lti.passbackid'} = $data->{'lti.passbackid'};
1.26 raeburn 105: }
106: if ($data->{'lti.passbackurl'}) {
1.27 raeburn 107: $lti_env{'request.lti.passbackurl'} = $data->{'lti.passbackurl'};
1.26 raeburn 108: }
109: if ($data->{'lti.rosterid'}) {
1.27 raeburn 110: $lti_env{'request.lti.rosterid'} = $data->{'lti.rosterid'};
1.26 raeburn 111: }
112: if ($data->{'lti.rosterurl'}) {
1.27 raeburn 113: $lti_env{'request.lti.rosterurl'} = $data->{'lti.rosterurl'};
1.26 raeburn 114: }
1.7 albertel 115: }
1.27 raeburn 116: return \%lti_env;
1.18 raeburn 117: }
118:
1.39 raeburn 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)) {
1.40 raeburn 127: my $audom = (split(/:/,$key))[1];
1.39 raeburn 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:
1.18 raeburn 161: sub ip_changed {
1.37 raeburn 162: my ($r,$udom,$camefrom,$idsref,$dataref) = @_;
1.18 raeburn 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;
1.22 raeburn 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;
1.21 raeburn 180: }
1.22 raeburn 181: my ($result,$cached)=&Apache::lonnet::is_cached_new('loadbalancing',$dom_in_use);
1.18 raeburn 182: unless (defined($cached)) {
183: my $cachetime = 60*60*24;
184: my %domconfig =
1.22 raeburn 185: &Apache::lonnet::get_dom('configuration',['loadbalancing'],$dom_in_use);
1.18 raeburn 186: if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
1.22 raeburn 187: $result = &Apache::lonnet::do_cache_new('loadbalancing',$dom_in_use,
1.18 raeburn 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;
1.23 raeburn 210: if ($rule_in_effect ne 'offloadedto') {
1.37 raeburn 211: my ($hosthere,@ids);
212: if (ref($idsref) eq 'ARRAY') {
213: @ids=@{$idsref};
214: }
1.23 raeburn 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: }
1.18 raeburn 236: }
237: }
238: }
239: }
1.23 raeburn 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: }
1.18 raeburn 253: if ($hostname) {
254: my $protocol = $Apache::lonnet::protocol{$switchto};
255: $protocol = 'http' if ($protocol ne 'https');
256: $url = $protocol.'://'.$hostname;
1.23 raeburn 257: if ($rule_in_effect eq 'balancer') {
258: $message .= '<br />'.
259: &mt('As a result, your LON-CAPA session is being redirected to the server where you originally logged in.');
260: } else {
261: $message .= '<br />'.
262: &mt('As a result, your LON-CAPA session is being redirected.');
263: }
1.18 raeburn 264: }
265: }
1.35 raeburn 266: unless ($hosthere) {
267: if (($dataref->{'balancer'}) && ($dataref->{'balcookie'})) {
268: &Apache::lonnet::delbalcookie($dataref->{'balcookie'},$dataref->{'balancer'});
269: }
270: }
1.18 raeburn 271: }
272: if ($dataref->{'sso.login'}) {
1.21 raeburn 273: $url .= '/adm/roles';
1.18 raeburn 274: } else {
1.21 raeburn 275: $url .= '/adm/login';
1.24 raeburn 276: if ($udom) {
277: $url .= '?domain='.$udom;
278: }
1.20 raeburn 279: $message .= '<br />'.&mt('You will need to provide your password one more time.');
1.18 raeburn 280: }
281: my %info= (
1.24 raeburn 282: 'domain' => $udom,
1.18 raeburn 283: 'username' => $dataref->{'username'},
284: 'role' => $dataref->{'role'},
285: 'sessionserver' => $lonhost,
286: );
287: if ($dataref->{'origurl'}) {
288: $info{'origurl'} = $dataref->{'origurl'};
289: }
290: if ($dataref->{'symb'}) {
291: $info{'symb'} = $dataref->{'symb'};
292: }
293: my $iptoken = &Apache::lonnet::tmpput(\%info,$switchto);
294: unless ($iptoken eq 'conlost') {
1.24 raeburn 295: $url .= ($url =~ /\?/) ? '&' : '?';
296: $url .= 'iptoken='.$iptoken;
1.18 raeburn 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);
1.10 raeburn 306: }
1.18 raeburn 307: return OK;
1.7 albertel 308: }
309:
1.28 raeburn 310: sub logout {
311: my ($r,$handle,$data,$lti_env) = @_;
1.34 raeburn 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");
1.28 raeburn 318: }
319: }
320: my %temp=('logout' => time);
321: &Apache::lonnet::put('email_status',\%temp);
1.49 ! raeburn 322: my $ip = &Apache::lonnet::get_requestor_ip();
1.28 raeburn 323: &Apache::lonnet::log($env{'user.domain'},
324: $env{'user.name'},
325: $env{'user.home'},
1.49 ! raeburn 326: "Logout $ip");
1.28 raeburn 327:
328: &Apache::loncommon::content_type($r,'text/html');
329:
1.34 raeburn 330: #expire the cookies
331: my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
332: foreach my $name (keys(%cookies)) {
333: next unless ($name =~ /^lon(|S|Link|Pub)ID$/);
334: my $c = new CGI::Cookie(-name => $name,
335: -value => '',
336: -expires => '-10y',);
337: $r->headers_out->add('Set-cookie' => $c);
1.28 raeburn 338: }
339: my (%info,%user_info,%lti_info);
340: if (ref($lti_env) eq 'HASH') {
1.44 raeburn 341: foreach my $key (sort(keys(%{$lti_env}))) {
342: if ($key =~ /^request\.(.+)$/) {
343: $lti_info{$1} = $lti_env->{$key};
344: }
345: }
1.28 raeburn 346: }
347: my $lonhost = $r->dir_config('lonHostID');
348: if (ref($data) eq 'HASH') {
1.49 ! raeburn 349: %user_info=('ip' => $ip,
1.28 raeburn 350: 'domain' => $data->{'domain'},
351: 'username' => $data->{'username'},
1.36 raeburn 352: 'home' => $data->{'home'},
1.28 raeburn 353: 'role' => $data->{'role'},
354: 'origurl' => $data->{'origurl'},
355: 'symb' => $data->{'symb'},
356: 'server' => $lonhost);
357: }
358: %info = (%user_info,%lti_info);
359: my $token = &Apache::lonnet::tmpput(\%info,$lonhost);
360: my $url = '/adm/migrateuser?token='.$token;
361: $r->send_http_header;
362: $r->print(
363: &Apache::loncommon::start_page('Updating Session ...',undef,
364: {'redirect' => [0.1,$url],
365: 'only_body' => 1,}).
366: &Apache::loncommon::end_page());
1.44 raeburn 367: if ($env{'request.balancercookie'}) {
368: my ($balancer,$cookie) = split(/:/,$env{'request.balancercookie'});
369: if ((&Apache::lonnet::hostname($balancer)) && ($cookie =~ /^[a-f0-9]{32}$/)) {
370: $cookie = $env{'user.domain'}.'_'.$env{'user.name'}.'_'.$cookie;
371: &Apache::lonnet::delbalcookie($cookie,$balancer);
372: }
373: }
1.28 raeburn 374: $r->register_cleanup(\&flush_course_logs);
375: return;
376: }
377:
1.36 raeburn 378: sub conlost_userhome {
379: my ($r,$idsref,$dataref) = @_;
380: return unless ((ref($idsref) eq 'ARRAY') && (ref($dataref) eq 'HASH'));
381: my @ids = @{$idsref};
382: my %data = %{$dataref};
1.37 raeburn 383: my ($lonhost,$lowest_load,$otherserver,$is_balancer,%conlost,%posstargets);
384: $lonhost = $r->dir_config('lonHostID');
385: if (grep(/^\Q$lonhost\E$/,split(/,/,$data{'dom_balancers'}))) {
386: $is_balancer = 1;
387: }
388: $lowest_load = 30000;
1.36 raeburn 389: if ($data{'conlost'} ne '') {
390: map { $conlost{$_} = 1; } split(/,/,$data{'conlost'});
391: }
1.37 raeburn 392: if ($data{'loncfail'}) {
1.38 raeburn 393: if ($lonhost ne $data{'from_balancer'}) {
394: my ($is_balancer,$posshost,$setcookie,$offloadto,$dom_balancers) =
395: &Apache::lonnet::check_loadbalancing($data{'username'},$data{'domain'});
396: if ($is_balancer) {
397: if (ref($offloadto) eq 'HASH') {
398: $data{'offloadto'} = '';
399: foreach my $key (keys(%{$offloadto})) {
400: if (ref($offloadto->{$key}) eq 'ARRAY') {
401: $data{'offloadto'} .= $key.'='.join(',',@{$offloadto->{$key}}).'&';
1.37 raeburn 402: }
403: }
1.38 raeburn 404: $data{'offloadto'} =~ s/\&$//;
405: } elsif (ref($offloadto) eq 'ARRAY') {
406: $data{'offloadto'} = join(',',@{$offloadto});
1.37 raeburn 407: }
408: }
409: }
410: } else {
411: $conlost{$lonhost} = 1;
412: $data{'conlost'} = join(',',sort(keys(%conlost)));
413: }
1.36 raeburn 414: if ($data{'offloadto'} =~ /\&/) {
1.37 raeburn 415: foreach my $item (split(/\&/,$data{'offloadto'})) {
1.36 raeburn 416: my ($type,$targets) = split(/\=/,$item);
417: @{$posstargets{$type}} = split(/,/,$targets);
418: }
419: } elsif ($data{'offloadto'} =~ /=/) {
420: my ($type,$targets) = split(/\=/,$data{'offloadto'});
421: @{$posstargets{$type}} = split(/,/,$targets);
422: } else {
423: @{$posstargets{'default'}} = split(/,/,$data{'offloadto'});
424: }
425: if (ref($posstargets{'primary'}) eq 'ARRAY') {
426: foreach my $try_server (@{$posstargets{'primary'}}) {
427: next if ($conlost{$try_server});
1.37 raeburn 428: next if ((grep(/^\Q$try_server\E$/,@ids)) && !$data{'loncfail'});
1.36 raeburn 429: ($otherserver,$lowest_load) =
430: &Apache::lonnet::compare_server_load($try_server,
431: $otherserver,
432: $lowest_load);
433: }
434: }
435: my $found_server = ($otherserver ne '' && $lowest_load < 100);
436: if (!$found_server) {
437: if (ref($posstargets{'default'}) eq 'ARRAY') {
438: foreach my $try_server (@{$posstargets{'default'}}) {
1.37 raeburn 439: next if ($conlost{$try_server});
440: next if ((grep(/^\Q$try_server\E$/,@ids)) && !$data{'loncfail'});
1.36 raeburn 441: ($otherserver,$lowest_load) =
1.37 raeburn 442: &Apache::lonnet::compare_server_load($try_server,
443: $otherserver,
444: $lowest_load);
1.36 raeburn 445: }
446: }
447: }
448: if ($otherserver ne '') {
449: my $switchto = &Apache::lonnet::hostname($otherserver);
450: if ($switchto ne '') {
451: $data{'server'} = $lonhost;
1.37 raeburn 452: if ($data{'loncfail'}) {
453: delete($data{'loncfail'});
454: }
1.36 raeburn 455: if (grep(/^\Q$otherserver\E$/,split(/,/,$data{'dom_balancers'}))) {
1.37 raeburn 456: $data{'noloadbalance'} = $otherserver;
1.36 raeburn 457: }
1.37 raeburn 458: &do_server_switch($r,$otherserver,$switchto,$is_balancer,\%data);
1.36 raeburn 459: return $otherserver;
1.37 raeburn 460: } else {
461: #FIXME Contents of $data{offloadto} contains invalid hostID.
462: }
463: } else {
464: my (%poss_balancers,%tried_balancers);
465: map { $poss_balancers{$_} = 1; } split(/,/,$data{'dom_balancers'});
466: map { $tried_balancers{$_} = 1; } split(/,/,$data{'tried_balancers'});
467: if (keys(%poss_balancers)) {
468: foreach my $try_server (sort(keys(%poss_balancers))) {
469: next if (grep(/^\Q$try_server\E$/,@ids));
470: next if (($data{'loncfail'}) && ($tried_balancers{$try_server}));
471: my $alreadytried;
472: foreach my $key (keys(%posstargets)) {
473: if ((ref($posstargets{$key}) eq 'ARRAY') &&
474: (grep(/^\Q$try_server\E$/,@{$posstargets{$key}}))) {
475: $alreadytried = 1;
476: last;
477: }
478: }
479: unless ($alreadytried) {
480: if (&Apache::lonnet::reply('ping',$try_server) eq $try_server) {
481: $otherserver = $try_server;
482: last;
483: }
484: }
485: }
486: }
487: if ($otherserver ne '') {
488: my $switchto = &Apache::lonnet::hostname($otherserver);
489: if ($switchto ne '') {
490: $data{'server'} = $lonhost;
491: $data{'loncfail'} = $lonhost;
492: $tried_balancers{$otherserver} = 1;
493: $data{'tried_balancers'} = join(',',sort(keys(%tried_balancers)));
494: &do_server_switch($r,$otherserver,$switchto,$is_balancer,\%data);
495: return $otherserver;
496: } else {
497: #FIXME Contents of $data{'dom_balancers'} contains invalid hostID.
1.39 raeburn 498: }
1.37 raeburn 499: } else {
500: if ($data{'loncfail'}) {
501: #FIXME Nowhere to go.
502: } else {
503: # FIXME Send back to a balancer (no token); different balancer if this is a balancer.
504: # Send command to balancer to exclude this host using http instead on lonc/lond
505: }
1.36 raeburn 506: }
507: }
1.37 raeburn 508: return;
509: }
510:
511: sub do_server_switch {
512: my ($r,$otherserver,$switchto,$is_balancer,$dataref) = @_;
513: if (ref($dataref) eq 'HASH') {
514: my $domain = $dataref->{'domain'};
515: my $username = $dataref->{'username'};
516: # If this is a balancer set a balancer cookie unless browser already sent
517: # LON-CAPA load balancer cookie which points at the target server.
518: if ($is_balancer) {
519: my $newcookieid;
520: my ($found_server,$balancer_cookie) = &Apache::lonnet::check_for_balancer_cookie($r,1);
521: unless (($found_server eq $otherserver) &&
522: ($balancer_cookie =~ /^\Q$domain\E_\Q$username\E_/)) {
523: $newcookieid =
524: &Apache::switchserver::balancer_cookieid($r,$otherserver,$username,$domain);
525: if ($newcookieid) {
526: $dataref->{'balcookie'} = $newcookieid;
527: }
528: }
529: }
530: my $token = &Apache::lonnet::tmpput($dataref,$otherserver);
531: my $protocol = $Apache::lonnet::protocol{$otherserver};
532: $protocol = 'http' if ($protocol ne 'https');
533: my $url = $protocol.'://'.$switchto.'/adm/login?domain='.$domain.
534: '&username='.$username.'&token='.$token;
535: $r->send_http_header;
536: $r->print(
537: &Apache::loncommon::start_page('Switching Server ...',undef,
538: {'redirect' => [0.1,$url]}).
539: &Apache::loncommon::end_page());
540: }
541: return;
1.36 raeburn 542: }
543:
544: sub log_switch {
1.49 ! raeburn 545: my ($r,$data,$lti_env,$ip) = @_;
1.36 raeburn 546: my $lonhost = $r->dir_config('lonHostID');
547: return unless ((ref($data) eq 'HASH') && (ref($lti_env) eq 'HASH'));
548: my $now = time;
549: my %temp=('switchserver' => $now.':'.$lonhost,$data->{'role'});
550: &Apache::lonnet::put('email_status',\%temp);
551: my $logmsg = "Switch Server to $lonhost";
552: if ($data->{'role'}) {
553: $logmsg .= " with role: ".$data->{'role'};
554: } elsif (($lti_env->{'reqcrs'}) && ($lti_env->{'reqrole'} eq 'cc')) {
555: $logmsg .= " to create new LTI course";
556: } elsif ($lti_env->{'selfenrollrole'}) {
557: $logmsg .= " to selfenroll with role: ".$lti_env->{'selfenrollrole'};
558: } else {
559: $logmsg .= " (no role)";
560: }
1.49 ! raeburn 561: $logmsg .= ' '.$ip;
1.36 raeburn 562: &Apache::lonnet::log($data->{'domain'},$data->{'username'},$data->{'home'},$logmsg);
563: }
564:
1.28 raeburn 565: sub flush_course_logs {
566: &Apache::lonnet::flushcourselogs();
567: return OK;
568: }
569:
1.1 albertel 570: sub handler {
571: my ($r) = @_;
572:
573: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['token']);
1.4 albertel 574: my %data = &Apache::lonnet::tmpget($env{'form.token'});
1.13 raeburn 575: if (keys(%data) == 0) {
576: return &goto_login($r);
577: }
1.4 albertel 578: my $delete = &Apache::lonnet::tmpdel($env{'form.token'});
579:
1.6 albertel 580: &Apache::lonlocal::get_language_handle($r);
581:
1.4 albertel 582: if ($delete ne 'ok') {
1.27 raeburn 583: return &goto_login($r,undef,\%data);
1.4 albertel 584: }
1.2 albertel 585:
1.18 raeburn 586: if (!defined($data{'username'}) || !defined($data{'domain'})) {
1.27 raeburn 587: return &goto_login($r,undef,\%data);
1.18 raeburn 588: }
1.37 raeburn 589: my ($home,@ids);
590: @ids=&Apache::lonnet::current_machine_ids();
1.49 ! raeburn 591: my $ip = &Apache::lonnet::get_requestor_ip();
! 592: if ($data{'ip'} ne $ip) {
1.24 raeburn 593: &Apache::lonnet::logthis('IP change when session migration requested -- was: '.
1.49 ! raeburn 594: $data{'ip'}.'; now: '.$ip.' for '.$data{'username'}.':'.$data{'domain'});
1.37 raeburn 595: return &ip_changed($r,$data{'domain'},$data{'server'},\@ids,\%data);
1.2 albertel 596: }
1.37 raeburn 597: if ($data{'loncfail'}) {
598: &Apache::lonnet::logthis('Returned from '.$data{'server'}.' -- no connection to library node or other access node(s)');
599: my $otherserver = &conlost_userhome($r,\@ids,\%data);
600: if ($otherserver ne '') {
601: &Apache::lonnet::logthis("Switching to $otherserver");
602: }
603: return OK;
604: } else {
605: $home=&Apache::lonnet::homeserver($data{'username'},$data{'domain'});
606: if ($home eq 'no_host') {
607: if (($data{'home'} ne '') && (&Apache::lonnet::hostname($data{'home'}))) {
608: &Apache::lonnet::reconlonc($data{'home'});
609: $home=&Apache::lonnet::homeserver($data{'username'},$data{'domain'});
610: if ($home eq 'no_host') {
611: unless (grep(/^\Q$data{'home'}\E$/,@ids)) {
612: if (&Apache::lonnet::reply('ping',$data{'home'}) eq 'con_lost') {
613: my $otherserver = &conlost_userhome($r,\@ids,\%data);
614: if ($otherserver ne '') {
615: &Apache::lonnet::logthis("No connection to home server ($data{'home'}) for $data{'username'}:$data{'domain'}. Switching to $otherserver");
616: }
1.36 raeburn 617: return OK;
618: }
619: }
620: }
621: }
622: }
623: }
624:
1.24 raeburn 625: my $udom;
626: if (&Apache::lonnet::domain($data{'domain'})) {
627: $udom=$data{'domain'};
628: }
1.36 raeburn 629: if ($home eq 'no_host') { return &goto_login($r,$udom,\%data); }
1.38 raeburn 630: if (&Apache::lonnet::hostname($home) eq '') { return &goto_login($r,$udom,\%data); }
1.36 raeburn 631:
1.39 raeburn 632: unless (grep(/^\Q$home\E$/,@ids)) {
633: my $lonhost = $r->dir_config('lonHostID');
634: my $loncaparev = $r->dir_config('lonVersion');
635: unless (&canhost($data{'username'},$data{'domain'},$lonhost,$loncaparev)) {
636: return &goto_login($r,$udom,\%data);
637: }
638: }
1.41 raeburn 639:
1.36 raeburn 640: my $rolemsg;
641: if ($data{'role'}) {
1.38 raeburn 642: $rolemsg = "role: $data{'role'}";
1.36 raeburn 643: } else {
644: $rolemsg = '(no role)';
645: }
646:
647: &Apache::lonnet::logthis("Allowing access for $data{'username'}:$data{'domain'} $rolemsg");
1.27 raeburn 648:
649: my $sso_env = &sso_check(\%data);
650: my $lti_env = <i_check(\%data);
1.2 albertel 651:
1.27 raeburn 652: my $extra_env;
653: if ((ref($sso_env) eq 'HASH') && (keys(%{$sso_env}))) {
654: $extra_env = $sso_env;
655: } elsif ((ref($lti_env) eq 'HASH') && (keys(%{$lti_env}))) {
656: $extra_env = $lti_env;
657: }
1.45 raeburn 658: if (($data{'balancer'}) && ($data{'server'}) && ($data{'balcookie'})) {
659: if (ref($extra_env) eq 'HASH') {
660: $extra_env->{'request.balancercookie'} = $data{'server'}.':'.$data{'balcookie'};
661: } else {
662: $extra_env = { 'request.balancercookie' => $data{'server'}.':'.$data{'balcookie'} };
663: }
1.48 raeburn 664: } elsif (($data{'server'}) && ($data{'otherbalcookie'})) {
665: my ($balancer,$balcookie) = split(/:/,$data{'otherbalcookie'});
666: if (defined(&Apache::lonnet::hostname($balancer)) && $balcookie =~ /^[a-f0-9]{32}$/) {
667: my $baldom = &Apache::lonnet::host_domain($balancer);
668: if (&Apache::lonnet::shared_institution($baldom)) {
669: my $cookieid = join('_',$udom,$data{'username'},$balcookie);
670: &Apache::lonnet::updatebalcookie($cookieid,$balancer,$data{'server'});
671: if (ref($extra_env) eq 'HASH') {
672: $extra_env->{'request.balancercookie'} = $data{'otherbalcookie'};
673: } else {
674: $extra_env = { 'request.balancercookie' => $data{'otherbalcookie'} };
675: }
676: }
677: }
1.45 raeburn 678: }
1.8 albertel 679:
1.37 raeburn 680: if (($data{'conlost'}) && ($data{'server'})) {
681: my @conlosts = split(/,/,$data{'conlost'});
682: my $switchfrom = $data{'server'};
683: if (@conlosts) {
684: if (grep(/^\Q$switchfrom\E$/,@conlosts)) {
1.49 ! raeburn 685: &log_switch($r,\%data,$extra_env,$ip);
1.37 raeburn 686: }
687: }
688: }
689:
1.46 raeburn 690: my (%form,$cid);
1.16 raeburn 691: if ($data{'symb'} ne '') {
692: $form{'symb'} = $data{'symb'};
693: }
1.21 raeburn 694: if ($data{'iptoken'} ne '') {
695: $form{'iptoken'} = $data{'iptoken'};
696: }
1.25 raeburn 697: if ($data{'noloadbalance'} ne '') {
698: $form{'noloadbalance'} = $data{'noloadbalance'};
699: }
1.46 raeburn 700: if ($data{'role'}) {
701: if ($data{'role'} =~ m{\./($match_domain)/($match_courseid)(?:/\w+|$)}) {
702: unless (&Apache::lonnet::homeserver($2,$1) eq 'no_host') {
703: $cid = $1.'_'.$2;
704: }
705: }
706: } else {
1.12 albertel 707: my $handle = &Apache::lonnet::check_for_valid_session($r);
708: if ($handle) {
1.11 albertel 709: &Apache::lonnet::transfer_profile_to_env($r->dir_config('lonIDsDir'),
710: $handle);
1.42 raeburn 711: if ($data{'linkprot'} ne '') {
712: my ($linkprotector,$deeplink) = split(/:/,$data{'linkprot'},2);
1.43 raeburn 713: if ($deeplink ne '') {
714: &Apache::lonnet::appenv({'request.deeplink.login' => $deeplink});
715: }
1.42 raeburn 716: if ($env{'user.linkprotector'}) {
717: my @protectors = split(/,/,$env{'user.linkprotector'});
718: unless (grep(/^\Q$linkprotector\E$/,@protectors)) {
719: push(@protectors,$linkprotector);
720: @protectors = sort { $a <=> $b } @protectors;
721: &Apache::lonnet::appenv({'user.linkprotector' => join(',',@protectors)});
722: }
723: } else {
724: &Apache::lonnet::appenv({'user.linkprotector' => $linkprotector });
1.43 raeburn 725: }
1.42 raeburn 726: if ($env{'user.linkproturi'}) {
727: my @proturis = split(/,/,$env{'user.linkproturi'});
728: unless(grep(/^\Q$deeplink\E$/,@proturis)) {
729: push(@proturis,$deeplink);
730: @proturis = sort @proturis;
731: &Apache::lonnet::appenv({'user.linkproturi' => join(',',@proturis)});
732: }
733: } else {
734: &Apache::lonnet::appenv({'user.linkproturi' => $deeplink});
735: }
1.43 raeburn 736: } elsif ($data{'deeplink.login'}) {
737: my $deeplink = $data{'deeplink.login'};
738: if ($data{'linkkey'}) {
739: my $linkkey = $data{'linkkey'};
740: if ($env{'user.deeplinkkey'}) {
741: my @linkkeys = split(/,/,$env{'user.deeplinkkey'});
742: unless (grep(/^\Q$linkkey\E$/,@linkkeys)) {
743: push(@linkkeys,$linkkey);
744: &Apache::lonnet::appenv({'user.deeplinkkey' => join(',',sort(@linkkeys))});
745: }
746: } else {
747: &Apache::lonnet::appenv({'user.deeplinkkey' => $linkkey});
748: }
749: if ($env{'user.keyedlinkuri'}) {
750: my @keyeduris = split(/,/,$env{'user.keyedlinkuri'});
751: unless (grep(/^\Q$deeplink\E$/,@keyeduris)) {
752: push(@keyeduris,$deeplink);
753: &Apache::lonnet::appenv({'user.keyedlinkuri' => join(',',sort(@keyeduris))});
754: }
755: } else {
756: &Apache::lonnet::appenv({'user.keyedlinkuri' => $deeplink});
757: }
758: }
759: &Apache::lonnet::appenv({'request.deeplink.login' => $data{'deeplink.login'}});
1.42 raeburn 760: }
1.27 raeburn 761: if ($data{'lti.login'}) {
1.28 raeburn 762: my $needslogout;
763: if ($env{'request.lti.login'}) {
764: if (($env{'user.name'} ne $data{'username'}) ||
765: ($env{'user.domain'} ne $data{'domain'})) {
766: $needslogout = 1;
767: }
768: } else {
769: $needslogout = 1;
770: }
771: # If access is via LTI, and user already has a non-LTI session cookie
772: # (and session) or has an LTI session cookie for a different username,
773: # logout the existing session, and start a new one
774: if ($needslogout) {
775: &logout($r,$handle,\%data,$lti_env);
776: } elsif (($data{'lti.reqcrs'}) && ($data{'lti.reqrole'} eq 'cc')) {
1.27 raeburn 777: $form{'lti.reqcrs'} = $data{'lti.reqcrs'};
778: $form{'lti.reqrole'} = $data{'lti.reqrole'};
779: $form{'lti.sourcecrs'} = $data{'lti.sourcecrs'};
1.29 raeburn 780: $form{'lti.uri'} = $data{'lti.uri'};
1.28 raeburn 781: if ($data{'lti.passbackid'}) {
782: $form{'lti.passbackid'} = $data{'lti.passbackid'};
783: }
784: if ($data{'lti.passbackurl'}) {
785: $form{'lti.passbackurl'} = $data{'lti.passbackurl'};
786: }
787: if ($data{'lti.rosterid'}) {
788: $form{'lti.rosterid'} = $data{'lti.rosterid'};
789: }
790: if ($data{'lti.rosterurl'}) {
791: $form{'lti.rosterurl'} = $data{'lti.rosterurl'};
792: }
1.29 raeburn 793: if ($data{'lti.target'}) {
794: $form{'lti.target'} = $data{'lti.target'};
795: }
1.27 raeburn 796: &Apache::loncommon::content_type($r,'text/html');
797: $r->send_http_header;
798: &Apache::ltiauth::lti_reqcrs($r,$data{'domain'},\%form,$data{'username'},$data{'domain'});
1.28 raeburn 799: } else {
800: if (ref($lti_env) eq 'HASH') {
801: delete($lti_env->{'reqcrs'});
802: delete($lti_env->{'reqrole'});
803: delete($lti_env->{'selfenrollrole'});
804: }
805: if ($data{'lti.selfenrollrole'}) {
1.32 raeburn 806: if (&Apache::ltiauth::lti_enroll($data{'username'},$data{'domain'},
1.28 raeburn 807: $data{'lti.selfenrollrole'}) eq 'ok') {
808: my $url = '/adm/roles?selectrole=1&'.
809: &escape($data{'lti.selfenrollrole'}).'=1';
810: if ($data{'origurl'} =~ m{/default_\d+\.sequence$}) {
811: $url .= '&orgurl='.$data{'origurl'}.'&navmap=1';
812: } elsif ($data{'origurl'} ne '') {
813: $url .= '&orgurl='.$data{'origurl'};
814: }
815: if (ref($lti_env) eq 'HASH') {
816: &Apache::lonnet::appenv($lti_env);
817: }
818: $r->internal_redirect($url);
819: } else {
820: &Apache::ltiauth::invalid_request($r,23);
821: }
822: } elsif ($data{'origurl'} ne '') {
823: my $url = $data{'origurl'};
824: if ($url =~ m{/default_\d+\.sequence$}) {
825: $url .= (($url =~/\?/)?'&':'?').'navmap=1';
826: }
827: if (ref($lti_env) eq 'HASH') {
828: &Apache::lonnet::appenv($lti_env);
1.27 raeburn 829: }
830: $r->internal_redirect($url);
831: } else {
1.28 raeburn 832: if (ref($lti_env) eq 'HASH') {
833: &Apache::lonnet::appenv($lti_env);
834: }
1.27 raeburn 835: }
836: }
837: } elsif ($data{'origurl'} ne '') {
1.14 raeburn 838: $r->internal_redirect($data{'origurl'});
839: } elsif ($env{'request.course.id'}) {
840: $r->internal_redirect('/adm/navmaps');
1.11 albertel 841: } else {
842: $r->internal_redirect('/adm/roles');
843: }
844: } else {
1.14 raeburn 845: my $desturl = '/adm/roles';
1.47 raeburn 846: if ($data{'lti.login'}) {
847: if ($data{'lti.selfenrollrole'}) {
848: $desturl .= '?selectrole=1&'.
849: &escape($data{'lti.selfenrollrole'}).'=1';
850: if ($data{'origurl'} ne '') {
851: $desturl .= '&orgurl='.$data{'origurl'};
852: if ($data{'origurl'} =~ m{/default_\d+\.sequence$}) {
853: $desturl .= '&navmap=1';
854: }
855: }
856: } elsif ($data{'origurl'} ne '') {
857: $desturl = $data{'origurl'};
1.27 raeburn 858: if ($desturl =~ m{/default_\d+\.sequence$}) {
859: $desturl .= (($desturl =~/\?/)?'&':'?').'navmap=1';
860: }
861: }
1.47 raeburn 862: } elsif ($data{'origurl'} ne '') {
863: $desturl = $data{'origurl'};
1.42 raeburn 864: if ($data{'linkprot'}) {
865: my ($linkprotector,$linkuri) = split(/:/,$data{'linkprot'},2);
866: if ($linkprotector) {
867: if (ref($extra_env) eq 'HASH') {
868: $extra_env->{'user.linkprotector'} = $linkprotector;
869: $extra_env->{'user.linkproturi'} = $linkuri;
870: } else {
871: $extra_env = {'user.linkprotector' => $linkprotector,
872: 'user.linkproturi' => $linkuri,};
873: }
874: }
1.43 raeburn 875: } elsif ($data{'deeplink.login'}) {
876: if ($data{'linkkey'}) {
877: if (ref($extra_env) eq 'HASH') {
878: $extra_env->{'user.deeplinkkey'} = $data{'linkkey'};
879: $extra_env->{'user.keyedlinkuri'} = $data{'deeplink.login'},
880: } else {
881: $extra_env = {'user.deeplinkkey' => $data{'linkkey'},
882: 'user.keyedlinkuri' => $data{'deeplink.login'}};
883: }
884: }
885: if (ref($extra_env) eq 'HASH') {
886: $extra_env->{'request.deeplink.login' => $data{'deeplink.login'}};
887: } else {
888: $extra_env = {'request.deeplink.login' => $data{'deeplink.login'}};
889: }
1.42 raeburn 890: }
1.27 raeburn 891: }
892: my $skipcritical;
1.44 raeburn 893: if ($data{'lti.login'}) {
894: if ((($data{'lti.reqcrs'}) &&
895: ($data{'lti.reqrole'} eq 'cc')) ||
896: ($data{'lti.selfenrollrole'})) {
897: $skipcritical = 1;
898: }
1.14 raeburn 899: }
1.11 albertel 900: &Apache::lonauth::success($r,$data{'username'},$data{'domain'},
1.27 raeburn 901: $home,$desturl,$extra_env,\%form,$skipcritical);
1.11 albertel 902: }
1.1 albertel 903: return OK;
904: }
1.6 albertel 905:
1.44 raeburn 906: if ($data{'lti.login'}) {
907: my $handle = &Apache::lonnet::check_for_valid_session($r);
908: if ($handle) {
909: &Apache::lonnet::transfer_profile_to_env($r->dir_config('lonIDsDir'),
910: $handle);
911: my $needslogout;
912: if ($env{'request.lti.login'}) {
913: if (($env{'user.name'} ne $data{'username'}) ||
914: ($env{'user.domain'} ne $data{'domain'})) {
915: $needslogout = 1;
916: }
917: } else {
918: $needslogout = 1;
919: }
920: if ($needslogout) {
921: &logout($r,$handle,\%data,$lti_env);
922: return OK;
1.30 raeburn 923: }
924: }
1.15 raeburn 925: }
1.44 raeburn 926:
927: # check if current user, and role match those expected.
928: my ($next_url,$reuse_session);
929: if (($env{'user.name'} eq $data{'username'}) && ($env{'user.domain'} eq $data{'domain'}) &&
930: ($env{'request.role'} eq $data{'role'}) && ($data{'origurl'} ne '')) {
931: $next_url = $data{'origurl'};
932: $reuse_session = 1;
933: } else {
934: $next_url='/adm/roles?selectrole=1&'.&escape($data{'role'}).'=1';
935: if ($data{'origurl'} ne '') {
936: $next_url .= '&orgurl='.&escape($data{'origurl'});
937: }
938: }
939: if ($data{'lti.login'}) {
940: if (($data{'origurl'} =~ m{/default_\d+\.sequence$}) ||
941: ($data{'origurl'} =~ m{^/res/.+\.sequence$})) {
942: $next_url .= '&navmap=1';
943: }
944: }
945: if ($reuse_session) {
946: $r->internal_redirect($next_url);
947: } else {
948: &Apache::lonauth::success($r,$data{'username'},$data{'domain'},$home,
1.46 raeburn 949: $next_url,$extra_env,\%form,'',$cid);
1.44 raeburn 950: }
1.6 albertel 951: return OK;
1.1 albertel 952: }
953:
954: 1;
955: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>