File:
[LON-CAPA] /
loncom /
auth /
lonauth.pm
Revision
1.100:
download - view:
text,
annotated -
select for diffs
Wed Jul 22 20:24:07 2009 UTC (15 years, 1 month ago) by
raeburn
Branches:
MAIN
CVS tags:
HEAD
- Bug 3987. Deep linking.
- Include role and symb in query string when calling log-in page.
- Role is automatically selected and resource is displayed (assuming user actually has the role, and its active, and symb is valid).
1: # The LearningOnline Network
2: # User Authentication Module
3: #
4: # $Id: lonauth.pm,v 1.100 2009/07/22 20:24:07 raeburn Exp $
5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28:
29: package Apache::lonauth;
30:
31: use strict;
32: use LONCAPA;
33: use Apache::Constants qw(:common);
34: use CGI qw(:standard);
35: use DynaLoader; # for Crypt::DES version
36: use Crypt::DES;
37: use Apache::loncommon();
38: use Apache::lonnet;
39: use Apache::lonmenu();
40: use Apache::createaccount;
41: use Fcntl qw(:flock);
42: use Apache::lonlocal;
43:
44: # ------------------------------------------------------------ Successful login
45: sub success {
46: my ($r, $username, $domain, $authhost, $lowerurl, $extra_env,
47: $form) = @_;
48:
49: # ------------------------------------------------------------ Get cookie ready
50: my $cookie =
51: &Apache::loncommon::init_user_environment($r, $username, $domain,
52: $authhost, $form,
53: {'extra_env' => $extra_env,});
54:
55: my $public=($username eq 'public' && $domain eq 'public');
56:
57: if ($public or $lowerurl eq 'noredirect') { return $cookie; }
58:
59: # -------------------------------------------------------------------- Log this
60:
61: &Apache::lonnet::log($domain,$username,$authhost,
62: "Login $ENV{'REMOTE_ADDR'}");
63:
64: # ------------------------------------------------- Check for critical messages
65:
66: my @what=&Apache::lonnet::dump('critical',$domain,$username);
67: if ($what[0]) {
68: if (($what[0] ne 'con_lost') && ($what[0]!~/^error\:/)) {
69: $lowerurl='/adm/email?critical=display';
70: }
71: }
72:
73: # ------------------------------------------------------------ Get cookie ready
74: $cookie="lonID=$cookie; path=/";
75: # -------------------------------------------------------- Menu script and info
76: my $destination = $lowerurl;
77:
78: if (defined($form->{role})) {
79: my $envkey = 'user.role.'.$form->{role};
80: my $now=time;
81: my $then=$env{'user.login.time'};
82: my $refresh=$env{'user.refresh.time'};
83: if (exists($env{$envkey})) {
84: my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus);
85: &Apache::lonnet::role_status($envkey,$then,$refresh,$now,\$role,\$where,
86: \$trolecode,\$tstatus,\$tstart,\$tend);
87: if ($tstatus eq 'is') {
88: if ($destination =~ /\?/) {
89: $destination .= '&';
90: } else {
91: $destination .= '?';
92: }
93: $destination .= 'selectrole=1&'.$form->{role}.'=1';
94: if (defined($form->{symb})) {
95: my $destsymb = $form->{symb};
96: if ($destsymb =~ /___/) {
97: # FIXME Need to deal with encrypted symbs and urls as needed.
98: my ($map,$resid,$desturl)=split(/___/,$destsymb);
99: unless ($desturl=~/^(adm|uploaded|editupload|public)/) {
100: $desturl = &Apache::lonnet::clutter($desturl);
101: }
102: $destination .= '&destinationurl='.$desturl.
103: '&destsymb='.$destsymb;
104: } else {
105: $destination .= '&destinationurl='.$destsymb;
106: }
107: }
108: }
109: }
110: }
111:
112: my $windowinfo=&Apache::lonmenu::open($env{'browser.os'});
113: my $startupremote=&Apache::lonmenu::startupremote($destination);
114: my $remoteinfo=&Apache::lonmenu::load_remote_msg($lowerurl);
115: my $setflags=&Apache::lonmenu::setflags();
116: my $maincall=&Apache::lonmenu::maincall();
117: my $brcrum = [{'href' => '',
118: 'text' => 'Successful Login'},];
119: my $start_page=&Apache::loncommon::start_page('Successful Login',
120: $startupremote,
121: {'no_inline_link' => 1,
122: 'bread_crumbs' => $brcrum,});
123: my $end_page =&Apache::loncommon::end_page();
124:
125: my $continuelink;
126: if ($env{'environment.remote'} eq 'off') {
127: $continuelink='<a href="'.$destination.'">'.&mt('Continue').'</a>';
128: }
129: # ------------------------------------------------- Output for successful login
130:
131: &Apache::loncommon::content_type($r,'text/html');
132: $r->header_out('Set-cookie' => $cookie);
133: $r->send_http_header;
134:
135: my %lt=&Apache::lonlocal::texthash(
136: 'wel' => 'Welcome',
137: 'mes' => 'Welcome to the Learning<i>Online</i> Network with CAPA. Please wait while your session is being set up.',
138: 'pro' => 'Login problems?',
139: 'log' => 'loginproblems.html',
140: );
141: $r->print(<<ENDSUCCESS);
142: $start_page
143: $setflags
144: $windowinfo
145: <h1>$lt{'wel'}</h1>
146: $lt{'mes'}<p>
147: <a href="/adm/$lt{'log'}">$lt{'pro'}</a></p>
148: $remoteinfo
149: $maincall
150: $continuelink
151: $end_page
152: ENDSUCCESS
153: }
154:
155: # --------------------------------------------------------------- Failed login!
156:
157: sub failed {
158: my ($r,$message,$form) = @_;
159: my $start_page = &Apache::loncommon::start_page('Unsuccessful Login',undef,
160: {'no_inline_link' => 1,});
161: my $retry = '/adm/login?username='.$form->{'uname'}.
162: '&domain='.$form->{'udom'};
163: if (exists($form->{role})) {
164: $retry .= '&role='.$form->{role};
165: }
166: if (exists($form->{symb})) {
167: $retry .= '&symb='.$form->{symb};
168: }
169: my $end_page = &Apache::loncommon::end_page();
170: &Apache::loncommon::content_type($r,'text/html');
171: $r->send_http_header;
172: $r->print(
173: $start_page
174: .'<h1>'.&mt('Sorry ...').'</h1>'
175: .'<p class="LC_warning">'.&mt($message).'</p>'
176: .'<p>'.&mt('Please [_1]log in again[_2].','<a href="'.$retry.'">','</a>')
177: .'</p>'
178: .'<p><a href="/adm/loginproblems.html">'.&mt('Login problems?').'</a></p>'
179: .$end_page
180: );
181: }
182:
183: # ------------------------------------------------------------------ Rerouting!
184:
185: sub reroute {
186: my ($r) = @_;
187: &Apache::loncommon::content_type($r,'text/html');
188: $r->send_http_header;
189: my $msg='<h1>'.&mt('Sorry ...').'</h1>'
190: .&mt('Please [_1]log in again[_2].');
191: &Apache::loncommon::simple_error_page($r,'Rerouting',$msg);
192: }
193:
194: # ---------------------------------------------------------------- Main handler
195:
196: sub handler {
197: my $r = shift;
198: my $form;
199: # Are we re-routing?
200: if (-e '/home/httpd/html/lon-status/reroute.txt') {
201: &reroute($r);
202: return OK;
203: }
204:
205: &Apache::lonlocal::get_language_handle($r);
206:
207: # -------------------------------- Prevent users from attempting to login twice
208: my $handle = &Apache::lonnet::check_for_valid_session($r);
209: if ($handle ne '') {
210: # Indeed, a valid token is found
211: &Apache::loncommon::content_type($r,'text/html');
212: $r->send_http_header;
213: my $start_page =
214: &Apache::loncommon::start_page('Already logged in');
215: my $end_page =
216: &Apache::loncommon::end_page();
217: $r->print(
218: $start_page
219: .'<h1>'.&mt('You are already logged in!').'</h1>'
220: .'<p>'.&mt('Please either [_1]continue the current session[_2] or [_3]log out[_4].'
221: ,'<a href="/adm/roles">','</a>','<a href="/adm/logout">','</a>')
222: .'</p>'
223: .'<p><a href="/adm/loginproblems.html">'.&mt('Login problems?').'</a></p>'
224: .$end_page
225: );
226: return OK;
227: }
228:
229: # ---------------------------------------------------- No valid token, continue
230:
231:
232: my $buffer;
233: if ($r->header_in('Content-length') > 0) {
234: $r->read($buffer,$r->header_in('Content-length'),0);
235: }
236: my %form;
237: foreach my $pair (split(/&/,$buffer)) {
238: my ($name,$value) = split(/=/,$pair);
239: $value =~ tr/+/ /;
240: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
241: $form{$name}=$value;
242: }
243:
244: if ((!$form{'uname'}) || (!$form{'upass0'}) || (!$form{'udom'})) {
245: &failed($r,'Username, password and domain need to be specified.',
246: \%form);
247: return OK;
248: }
249:
250: # split user logging in and "su"-user
251:
252: ($form{'uname'},$form{'suname'})=split(/\:/,$form{'uname'});
253: $form{'uname'} = &LONCAPA::clean_username($form{'uname'});
254: $form{'suname'}= &LONCAPA::clean_username($form{'suname'});
255: $form{'udom'} = &LONCAPA::clean_domain( $form{'udom'});
256:
257: my $role = $r->dir_config('lonRole');
258: my $domain = $r->dir_config('lonDefDomain');
259: my $prodir = $r->dir_config('lonUsersDir');
260: my $contact_name = &mt('LON-CAPA helpdesk');
261:
262: # ---------------------------------------- Get the information from login token
263:
264: my $tmpinfo=Apache::lonnet::reply('tmpget:'.$form{'logtoken'},
265: $form{'serverid'});
266:
267: if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost')) {
268: &failed($r,'Information needed to verify your login information is missing, inaccessible or expired.',\%form);
269: return OK;
270: } else {
271: my $reply = &Apache::lonnet::reply('tmpdel:'.$form{'logtoken'},
272: $form{'serverid'});
273: if ( $reply ne 'ok' ) {
274: &failed($r,'Session could not be opened.',\%form);
275: &Apache::lonnet::logthis("ERROR got a reply of $reply when trying to contact ". $form{'serverid'}." to get login token");
276: return OK;
277: }
278: }
279:
280: if (!&Apache::lonnet::domain($form{'udom'})) {
281: &failed($r,'The domain you provided is not a valid LON-CAPA domain.',\%form);
282: return OK;
283: }
284:
285: my ($key,$firsturl,$rolestr,$symbstr)=split(/&/,$tmpinfo);
286: if ($rolestr) {
287: $rolestr = &unescape($rolestr);
288: }
289: if ($symbstr) {
290: $symbstr= &unescape($symbstr);
291: }
292: if ($rolestr =~ /^role=/) {
293: (undef,$form{'role'}) = split('=',$rolestr);
294: }
295: if ($symbstr =~ /^symb=/) {
296: (undef,$form{'symb'}) = split('=',$symbstr);
297: }
298:
299: my $keybin=pack("H16",$key);
300:
301: my $cipher;
302: if ($Crypt::DES::VERSION>=2.03) {
303: $cipher=new Crypt::DES $keybin;
304: }
305: else {
306: $cipher=new DES $keybin;
307: }
308: my $upass='';
309: for (my $i=0;$i<=2;$i++) {
310: my $chunk=
311: $cipher->decrypt(unpack("a8",pack("H16",substr($form{'upass'.$i},0,16))));
312:
313: $chunk.=
314: $cipher->decrypt(unpack("a8",pack("H16",substr($form{'upass'.$i},16,16))));
315:
316: $chunk=substr($chunk,1,ord(substr($chunk,0,1)));
317: $upass.=$chunk;
318: }
319:
320: # ---------------------------------------------------------------- Authenticate
321: my @cancreate;
322: my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$form{'udom'});
323: if (ref($domconfig{'usercreation'}) eq 'HASH') {
324: if (ref($domconfig{'usercreation'}{'cancreate'}) eq 'HASH') {
325: if (ref($domconfig{'usercreation'}{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
326: @cancreate = @{$domconfig{'usercreation'}{'cancreate'}{'selfcreate'}};
327: } elsif (($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne 'none') &&
328: ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne '')) {
329: @cancreate = ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'});
330: }
331: }
332: }
333: my $defaultauth;
334: if (grep(/^login$/,@cancreate)) {
335: $defaultauth = 1;
336: }
337: my $authhost=Apache::lonnet::authenticate($form{'uname'},$upass,
338: $form{'udom'},$defaultauth);
339:
340: # --------------------------------------------------------------------- Failed?
341:
342: if ($authhost eq 'no_host') {
343: &failed($r,'Username and/or password could not be authenticated.',
344: \%form);
345: return OK;
346: } elsif ($authhost eq 'no_account_on_host') {
347: my %domconfig =
348: &Apache::lonnet::get_dom('configuration',['usercreation'],$form{'udom'});
349: if (grep(/^login$/,@cancreate)) {
350: my $start_page =
351: &Apache::loncommon::start_page('Create a user account in LON-CAPA',
352: '',{'no_inline_link' => 1,});
353: my $domdesc = &Apache::lonnet::domain($form{'udom'},'description');
354: my $lonhost = $r->dir_config('lonHostID');
355: my $origmail = $Apache::lonnet::perlvar{'lonSupportEMail'};
356: my $contacts =
357: &Apache::loncommon::build_recipient_list(undef,'helpdeskmail',
358: $form{'udom'},$origmail);
359: my ($contact_email) = split(',',$contacts);
360: my $output = &Apache::createaccount::username_check($form{'uname'},
361: $form{'udom'},$domdesc,'',
362: $lonhost,$contact_email,$contact_name);
363: &Apache::loncommon::content_type($r,'text/html');
364: $r->send_http_header;
365: &Apache::createaccount::print_header($r,$start_page);
366: $r->print('<h3>'.&mt('Account creation').'</h3>'.
367: &mt('Although your username and password were authenticated, you do not currently have a LON-CAPA account at this institution.').'<br />'.
368: $output.&Apache::loncommon::end_page());
369: return OK;
370: } else {
371: &failed($r,'Although your username and password were authenticated, you do not currently have a LON-CAPA account in this domain, and you are not permitted to create one.',\%form);
372: return OK;
373: }
374: }
375:
376: if (($firsturl eq '') ||
377: ($firsturl=~/^\/adm\/(logout|remote)/)) {
378: $firsturl='/adm/roles';
379: }
380: # --------------------------------- Are we attempting to login as somebody else?
381: if ($form{'suname'}) {
382: # ------------ see if the original user has enough privileges to pull this stunt
383: if (&Apache::lonnet::privileged($form{'uname'},$form{'udom'})) {
384: # ---------------------------------------------------- see if the su-user exists
385: unless (&Apache::lonnet::homeserver($form{'suname'},$form{'udom'})
386: eq 'no_host') {
387: &Apache::lonnet::logthis(&Apache::lonnet::homeserver($form{'suname'},$form{'udom'}));
388: # ------------------------------ see if the su-user is not too highly privileged
389: unless (&Apache::lonnet::privileged($form{'suname'},$form{'udom'})) {
390: # -------------------------------------------------------- actually switch users
391: &Apache::lonnet::logperm('User '.$form{'uname'}.' at '.$form{'udom'}.
392: ' logging in as '.$form{'suname'});
393: $form{'uname'}=$form{'suname'};
394: } else {
395: &Apache::lonnet::logthis('Attempted switch user to privileged user');
396: }
397: }
398: } else {
399: &Apache::lonnet::logthis('Non-privileged user attempting switch user');
400: }
401: }
402:
403: if ($r->dir_config("lonBalancer") eq 'yes') {
404: &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',undef,
405: \%form);
406: $r->internal_redirect('/adm/switchserver');
407: } else {
408: &success($r,$form{'uname'},$form{'udom'},$authhost,$firsturl,undef,
409: \%form);
410: }
411: return OK;
412: }
413:
414: 1;
415: __END__
416:
417:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>