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