Annotation of loncom/auth/lonauth.pm, revision 1.87
1.1 albertel 1: # The LearningOnline Network
2: # User Authentication Module
1.27 www 3: #
1.87 ! albertel 4: # $Id: lonauth.pm,v 1.86 2006/10/12 18:18:01 albertel Exp $
1.27 www 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: #
1.1 albertel 28:
29: package Apache::lonauth;
30:
1.18 albertel 31: use strict;
1.78 albertel 32: use LONCAPA;
1.1 albertel 33: use Apache::Constants qw(:common);
34: use CGI qw(:standard);
35: use CGI::Cookie();
1.26 harris41 36: use DynaLoader; # for Crypt::DES version
1.8 www 37: use Crypt::DES;
1.45 matthew 38: use Apache::loncommon();
1.66 albertel 39: use Apache::lonnet;
1.12 www 40: use Apache::lonmenu();
1.18 albertel 41: use Fcntl qw(:flock);
1.56 www 42: use Apache::lonlocal;
1.85 albertel 43:
1.1 albertel 44: # ------------------------------------------------------------ Successful login
1.85 albertel 45: sub success {
46: my ($r, $username, $domain, $authhost, $lowerurl, $extra_env,
47: $form) = @_;
1.1 albertel 48:
1.85 albertel 49: # ------------------------------------------------------------ Get cookie ready
50: my $cookie =
51: &Apache::loncommon::init_user_environment($r, $username, $domain,
52: $authhost, $form,
1.86 albertel 53: {'extra_env' => $extra_env,});
1.4 www 54:
1.69 albertel 55: my $public=($username eq 'public' && $domain eq 'public');
56:
1.85 albertel 57: if ($public or $lowerurl eq 'noredirect') { return $cookie; }
1.78 albertel 58:
1.7 www 59: # -------------------------------------------------------------------- Log this
60:
61: &Apache::lonnet::log($domain,$username,$authhost,
62: "Login $ENV{'REMOTE_ADDR'}");
1.4 www 63:
1.14 www 64: # ------------------------------------------------- Check for critical messages
65:
1.21 www 66: my @what=&Apache::lonnet::dump('critical',$domain,$username);
1.14 www 67: if ($what[0]) {
1.22 www 68: if (($what[0] ne 'con_lost') && ($what[0]!~/^error\:/)) {
1.21 www 69: $lowerurl='/adm/email?critical=display';
1.14 www 70: }
71: }
72:
1.5 www 73: # ------------------------------------------------------------ Get cookie ready
1.1 albertel 74: $cookie="lonID=$cookie; path=/";
1.12 www 75: # -------------------------------------------------------- Menu script and info
1.85 albertel 76: my $windowinfo=&Apache::lonmenu::open($env{'browser.os'});
1.35 www 77: my $startupremote=&Apache::lonmenu::startupremote($lowerurl);
1.63 albertel 78: my $remoteinfo=&Apache::lonmenu::load_remote_msg($lowerurl);
1.35 www 79: my $setflags=&Apache::lonmenu::setflags();
80: my $maincall=&Apache::lonmenu::maincall();
1.74 albertel 81: my $start_page=&Apache::loncommon::start_page('Successful Login',
1.76 albertel 82: $startupremote,
83: {'no_inline_link' => 1,});
1.74 albertel 84: my $end_page =&Apache::loncommon::end_page();
85:
1.64 albertel 86: my $continuelink;
1.66 albertel 87: if (($env{'browser.interface'} eq 'textual') ||
88: ($env{'environment.remote'} eq 'off')) {
1.64 albertel 89: $continuelink="<a href=\"$lowerurl\">".&mt('Continue')."</a>";
90: }
1.5 www 91: # ------------------------------------------------- Output for successful login
92:
1.74 albertel 93: &Apache::loncommon::content_type($r,'text/html');
94: $r->header_out('Set-cookie' => $cookie);
95: $r->send_http_header;
1.1 albertel 96:
1.58 www 97: my %lt=&Apache::lonlocal::texthash(
98: 'wel' => 'Welcome',
99: 'mes' => 'Welcome to the Learning<i>Online</i> Network with CAPA. Please wait while your session is being set up',
100: 'pro' => 'Problems',
101: 'log' => 'loginproblems.html',
102: );
1.1 albertel 103: $r->print(<<ENDSUCCESS);
1.74 albertel 104: $start_page
1.35 www 105: $setflags
1.19 www 106: $windowinfo
1.58 www 107: <h1>$lt{'wel'}</h1>
108: $lt{'mes'}.<p>
109: <a href="/adm/$lt{'log'}">$lt{'pro'}?</a></p>
1.63 albertel 110: $remoteinfo
1.35 www 111: $maincall
1.64 albertel 112: $continuelink
1.74 albertel 113: $end_page
1.1 albertel 114: ENDSUCCESS
115: }
116:
117: # --------------------------------------------------------------- Failed login!
118:
119: sub failed {
1.85 albertel 120: my ($r,$message,$form) = @_;
1.76 albertel 121: my $start_page = &Apache::loncommon::start_page('Unsuccessful Login',undef,
122: {'no_inline_link' => 1,});
1.74 albertel 123: my $end_page = &Apache::loncommon::end_page();
124:
125: my %lt=('sorry' => &mt('Sorry ...'),
126: 'please' =>
127: &mt('Please [_1]log in again[_2].',
1.85 albertel 128: "<a href=\"/adm/login?username=$form->{'uname'}&domain=$form->{'udom'}\">",
1.74 albertel 129: '</a>'),
130: 'problemspage' => &mt('loginproblems.html'),
131: 'problems' => 'Problems',
132: );
133: &Apache::loncommon::content_type($r,'text/html');
134: $r->send_http_header;
1.1 albertel 135:
136: $r->print(<<ENDFAILED);
1.74 albertel 137: $start_page
138: <h1>$lt{'sorry'}</h1>
1.43 www 139: <p><b>$message</b></p>
1.74 albertel 140: <p>$lt{'please'}</p>
1.43 www 141: <p>
1.74 albertel 142: <a href="/adm/$lt{'problemspage'}">$lt{'problems'}</a></p>
143: $end_page
1.1 albertel 144: ENDFAILED
1.60 www 145: }
146:
1.55 www 147: # ------------------------------------------------------------------ Rerouting!
148:
149: sub reroute {
1.74 albertel 150: my ($r) = @_;
151: &Apache::loncommon::content_type($r,'text/html');
152: $r->send_http_header;
153: my $msg='<h1>Sorry ...</h1>
154: Please <a href="/">log in again</a>.';
155: &Apache::loncommon::simple_error_page($r,'Rerouting',$msg);
1.55 www 156: }
157:
1.1 albertel 158: # ---------------------------------------------------------------- Main handler
159:
160: sub handler {
161: my $r = shift;
1.85 albertel 162: my $form;
1.55 www 163: # Are we re-routing?
164: if (-e '/home/httpd/html/lon-status/reroute.txt') {
165: &reroute($r);
166: return OK;
167: }
1.56 www 168:
1.57 www 169: &Apache::lonlocal::get_language_handle($r);
1.1 albertel 170:
1.59 www 171: # -------------------------------- Prevent users from attempting to login twice
172: my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
173: my $lonid=$cookies{'lonID'};
174: my $cookie;
175: if ($lonid) {
1.87 ! albertel 176: my $handle=&LONCAPA::clean_handle($lonid->value);
1.59 www 177: my $lonidsdir=$r->dir_config('lonIDsDir');
178: if ((-e "$lonidsdir/$handle.id") && ($handle ne '')) {
179: # Indeed, a valid token is found
1.74 albertel 180: &Apache::loncommon::content_type($r,'text/html');
181: $r->send_http_header;
182: my $start_page =
183: &Apache::loncommon::start_page('Already logged in');
184: my $end_page =
185: &Apache::loncommon::end_page();
1.59 www 186: $r->print(<<ENDFAILED);
1.74 albertel 187: $start_page
1.59 www 188: <h1>You are already logged in</h1>
189: <p>Please either <a href="/adm/roles">continue the current session</a> or
190: <a href="/adm/logout">logout</a>.</p>
191: <p>
192: <a href="/adm/loginproblems.html">Problems?</a></p>
1.74 albertel 193: $end_page
1.59 www 194: ENDFAILED
195: return OK;
196: }
197: }
198:
199: # ---------------------------------------------------- No valid token, continue
200:
201:
1.1 albertel 202: my $buffer;
1.84 albertel 203: if ($r->header_in('Content-length') > 0) {
204: $r->read($buffer,$r->header_in('Content-length'),0);
205: }
1.85 albertel 206: my %form;
207: foreach my $pair (split(/&/,$buffer)) {
208: my ($name,$value) = split(/=/,$pair);
1.7 www 209: $value =~ tr/+/ /;
210: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
1.85 albertel 211: $form{$name}=$value;
1.1 albertel 212: }
213:
1.85 albertel 214: if ((!$form{'uname'}) || (!$form{'upass0'}) || (!$form{'udom'})) {
215: &failed($r,'Username, password and domain need to be specified.',
216: \%form);
1.1 albertel 217: return OK;
218: }
1.61 www 219:
220: # split user logging in and "su"-user
221:
1.85 albertel 222: ($form{'uname'},$form{'suname'})=split(/\:/,$form{'uname'});
1.87 ! albertel 223: $form{'uname'} = &LONCAPA::clean_username($form{'uname'});
! 224: $form{'suname'}= &LONCAPA::clean_username($form{'suname'});
! 225: $form{'udom'} = &LONCAPA::clean_domain( $form{'udom'});
1.1 albertel 226:
227: my $role = $r->dir_config('lonRole');
228: my $domain = $r->dir_config('lonDefDomain');
229: my $prodir = $r->dir_config('lonUsersDir');
230:
1.8 www 231: # ---------------------------------------- Get the information from login token
232:
1.85 albertel 233: my $tmpinfo=Apache::lonnet::reply('tmpget:'.$form{'logtoken'},
234: $form{'serverid'});
1.8 www 235:
236: if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost')) {
1.85 albertel 237: &failed($r,'Information needed to verify your login information is missing, inaccessible or expired.',\%form);
1.8 www 238: return OK;
1.44 www 239: } else {
1.85 albertel 240: my $reply = &Apache::lonnet::reply('tmpdel:'.$form{'logtoken'},
241: $form{'serverid'});
1.77 albertel 242: if ( $reply ne 'ok' ) {
1.85 albertel 243: &failed($r,'Session could not be opened.',\%form);
244: &Apache::lonnet::logthis("ERROR got a reply of $reply when trying to contact ". $form{'serverid'}." to get login token");
1.77 albertel 245: return OK;
1.44 www 246: }
1.8 www 247: }
1.9 www 248: my ($key,$firsturl)=split(/&/,$tmpinfo);
1.8 www 249:
250: my $keybin=pack("H16",$key);
251:
1.26 harris41 252: my $cipher;
253: if ($Crypt::DES::VERSION>=2.03) {
254: $cipher=new Crypt::DES $keybin;
255: }
256: else {
257: $cipher=new DES $keybin;
258: }
1.67 www 259: my $upass='';
260: for (my $i=0;$i<=2;$i++) {
261: my $chunk=
1.85 albertel 262: $cipher->decrypt(unpack("a8",pack("H16",substr($form{'upass'.$i},0,16))));
1.8 www 263:
1.67 www 264: $chunk.=
1.85 albertel 265: $cipher->decrypt(unpack("a8",pack("H16",substr($form{'upass'.$i},16,16))));
1.8 www 266:
1.67 www 267: $chunk=substr($chunk,1,ord(substr($chunk,0,1)));
268: $upass.=$chunk;
269: }
1.8 www 270:
1.1 albertel 271: # ---------------------------------------------------------------- Authenticate
1.85 albertel 272: my $authhost=Apache::lonnet::authenticate($form{'uname'},
1.8 www 273: $upass,
1.85 albertel 274: $form{'udom'});
1.1 albertel 275:
276: # --------------------------------------------------------------------- Failed?
277:
278: if ($authhost eq 'no_host') {
1.85 albertel 279: &failed($r,'Username and/or password could not be authenticated.',
280: \%form);
1.1 albertel 281: return OK;
282: }
283:
1.59 www 284: if (($firsturl eq '') ||
285: ($firsturl=~/^\/adm\/(logout|remote)/)) {
1.24 www 286: $firsturl='/adm/roles';
1.7 www 287: }
1.61 www 288: # --------------------------------- Are we attempting to login as somebody else?
1.85 albertel 289: if ($form{'suname'}) {
1.61 www 290: # ------------ see if the original user has enough privileges to pull this stunt
1.85 albertel 291: if (&Apache::lonnet::privileged($form{'uname'},$form{'udom'})) {
1.61 www 292: # ---------------------------------------------------- see if the su-user exists
1.85 albertel 293: unless (&Apache::lonnet::homeserver($form{'suname'},$form{'udom'})
1.61 www 294: eq 'no_host') {
1.85 albertel 295: &Apache::lonnet::logthis(&Apache::lonnet::homeserver($form{'suname'},$form{'udom'}));
1.61 www 296: # ------------------------------ see if the su-user is not too highly privileged
1.85 albertel 297: unless (&Apache::lonnet::privileged($form{'suname'},$form{'udom'})) {
1.61 www 298: # -------------------------------------------------------- actually switch users
1.85 albertel 299: &Apache::lonnet::logperm('User '.$form{'uname'}.' at '.$form{'udom'}.
300: ' logging in as '.$form{'suname'});
301: $form{'uname'}=$form{'suname'};
1.61 www 302: } else {
303: &Apache::lonnet::logthis('Attempted switch user to privileged user');
304: }
305: }
306: } else {
307: &Apache::lonnet::logthis('Non-privileged user attempting switch user');
308: }
309: }
1.85 albertel 310:
1.81 albertel 311: if ($r->dir_config("lonBalancer") eq 'yes') {
1.85 albertel 312: &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',undef,
313: \%form);
1.81 albertel 314: $r->internal_redirect('/adm/switchserver');
315: } else {
1.85 albertel 316: &success($r,$form{'uname'},$form{'udom'},$authhost,$firsturl,undef,
317: \%form);
1.81 albertel 318: }
1.1 albertel 319: return OK;
320: }
321:
322: 1;
323: __END__
1.7 www 324:
325:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>