Annotation of loncom/auth/lonauth.pm, revision 1.19
1.1 albertel 1: # The LearningOnline Network
2: # User Authentication Module
1.2 www 3: # 5/21/99,5/22,5/25,5/26,5/27,5/29,6/2,6/11,6/14,6/15
1.4 www 4: # 16/11,12/16,
1.9 www 5: # 1/14,2/24,2/28,2/29,3/7,5/29,5/30,5/31,6/1,6/5,6/29,
1.19 ! www 6: # 7/1,7/10,10/2,10/5,10/9,10/26,10/30,11/10,
! 7: # 05/28,05/29 Gerd Kortemeyer
1.1 albertel 8:
9: package Apache::lonauth;
10:
1.18 albertel 11: use strict;
1.1 albertel 12: use Apache::Constants qw(:common);
13: use Apache::File;
14: use CGI qw(:standard);
15: use CGI::Cookie();
1.8 www 16: use Crypt::DES;
1.1 albertel 17: use Apache::lonnet();
1.12 www 18: use Apache::lonmenu();
1.18 albertel 19: use Fcntl qw(:flock);
1.1 albertel 20: # ------------------------------------------------------------ Successful login
21:
22: sub success {
1.6 www 23: my ($r, $username, $domain, $authhost,$lowerurl) = @_;
1.1 albertel 24: my $lonids=$r->dir_config('lonIDsDir');
1.4 www 25:
26: # See if old ID present, if so, remove
1.17 www 27:
28: my $filename;
29: opendir(DIR,$lonids);
30: while ($filename=readdir(DIR)) {
31: if ($filename=~/^$username\_\d+\_$domain\_$authhost\.id$/) {
32: unlink($lonids.'/'.$filename);
33: }
1.4 www 34: }
1.17 www 35: closedir(DIR);
1.4 www 36:
37: # Give them a new cookie
38:
1.17 www 39: my $cookie;
1.5 www 40: my $now=time;
41: $cookie="$username\_$now\_$domain\_$authhost";
42:
43: # Initialize roles
44:
45: my $userroles=Apache::lonnet::rolesinit($domain,$username,$authhost);
46:
1.7 www 47: # ------------------------------------ Check browser type and MathML capability
1.6 www 48:
49: my @browsertype=split(/\&/,$r->dir_config("lonBrowsDet"));
50: my %mathcap=split(/\&/,$r->dir_config("lonMathML"));
51: my $httpbrowser=$ENV{"HTTP_USER_AGENT"};
52: my $i;
53: my $clientbrowser='unknown';
54: my $clientversion='0';
55: my $clientmathml='';
56: for ($i=0;$i<=$#browsertype;$i++) {
57: my ($bname,$match,$notmatch,$vreg,$minv)=split(/\:/,$browsertype[$i]);
58: if (($httpbrowser=~/$match/i) && ($httpbrowser!~/$notmatch/i)) {
59: $clientbrowser=$bname;
60: $httpbrowser=~/$vreg/i;
61: $clientversion=$1;
62: $clientmathml=($clientversion>=$minv);
63: }
64: }
65: my $clientos='unknown';
66: if (($httpbrowser=~/linux/i) ||
67: ($httpbrowser=~/unix/i) ||
68: ($httpbrowser=~/ux/i) ||
69: ($httpbrowser=~/solaris/i)) { $clientos='unix'; }
70: if (($httpbrowser=~/vax/i) ||
71: ($httpbrowser=~/vms/i)) { $clientos='vms'; }
72: if ($httpbrowser=~/next/i) { $clientos='next'; }
73: if (($httpbrowser=~/mac/i) ||
74: ($httpbrowser=~/powerpc/i)) { $clientos='mac'; }
1.10 www 75: if ($httpbrowser=~/win/i) { $clientos='win'; }
1.6 www 76:
1.11 www 77: # ------------------------------------------------------------- Get environment
78:
79: my $userenv=Apache::lonnet::reply("dump:$domain:$username:environment",
80: $authhost);
81: if (($userenv eq 'con_lost') ||
82: ($userenv =~ /^error\:/)) {
83: $userenv='';
84: }
85: $userenv=~s/\&/\nenvironment\./g;
86: if ($userenv ne '') {
87: $userenv='environment.'.$userenv;
88: }
1.7 www 89: # --------------------------------------------------------- Write first profile
1.5 www 90:
91: {
1.1 albertel 92: my $idf=Apache::File->new(">$lonids/$cookie.id");
1.18 albertel 93: unless (flock($idf,LOCK_EX)) {
94: &Apache::lonnet::logthis("<font color=blue>WARNING: ".
1.19 ! www 95: 'Could not obtain exclusive lock in lonauth: '.$!);
1.18 albertel 96: $idf->close();
97: return 'error: '.$!;
98: }
1.11 www 99: if ($userenv ne '') { print $idf "$userenv\n"; }
1.4 www 100: print $idf "user.name=$username\n";
101: print $idf "user.domain=$domain\n";
102: print $idf "user.home=$authhost\n";
1.6 www 103: print $idf "browser.type=$clientbrowser\n";
104: print $idf "browser.version=$clientversion\n";
105: print $idf "browser.mathml=$clientmathml\n";
106: print $idf "browser.os=$clientos\n";
1.15 www 107: print $idf "request.course.fn=\n";
108: print $idf "request.course.uri=\n";
109: print $idf "request.course.sec=\n";
1.16 www 110: print $idf "request.role=cm\n";
111: print $idf "request.host=$ENV{'HTTP_HOST'}\n";
1.11 www 112: if ($userroles ne '') { print $idf "$userroles"; }
1.18 albertel 113: $idf->close();
1.1 albertel 114: }
1.19 ! www 115: $ENV{'request.role'}='cm';
1.7 www 116: # -------------------------------------------------------------------- Log this
117:
118: &Apache::lonnet::log($domain,$username,$authhost,
119: "Login $ENV{'REMOTE_ADDR'}");
1.4 www 120:
1.14 www 121: # ------------------------------------------------- Check for critical messages
122:
123: my @what=&Apache::lonnet::dump('critical');
124: if ($what[0]) {
125: if ($what[0] ne 'con_lost') {
126: $lowerurl='/adm/email/critical/'.$what[0];
127: }
128: }
129:
1.5 www 130: # ------------------------------------------------------------ Get cookie ready
131:
1.1 albertel 132: $cookie="lonID=$cookie; path=/";
1.12 www 133: # -------------------------------------------------------- Menu script and info
134: my $windowinfo=&Apache::lonmenu::open();
1.19 ! www 135: # ------------------------------------------------------------- Info for Remote
! 136: my $configmenu=&Apache::lonmenu::rawconfig();
1.5 www 137: # ------------------------------------------------- Output for successful login
138:
1.1 albertel 139: $r->send_cgi_header(<<ENDHEADER);
140: Content-type: text/html
141: Set-cookie: $cookie
142:
143: ENDHEADER
144: $r->print(<<ENDSUCCESS);
145: <html>
146: <head>
1.4 www 147: <title>Successful Login to the LearningOnline Network with CAPA</title>
1.19 ! www 148: <script>
! 149:
! 150: // --------------------------------------------- Checks if server frame defined
! 151:
! 152: function checkdef() {
! 153: if ((menuloaded==0) && (tim==0)) { setTimeout('checkdef()',100); }
! 154: }
! 155:
! 156: // ---------------------------------------------------------- The wait function
! 157:
! 158: function wait() {
! 159: if ((menuloaded==1) || (tim==1)) {
! 160: if (tim==0) {
! 161: clearTimeout(canceltim);
! 162: $configmenu
! 163: window.location='$lowerurl';
! 164: } else {
! 165: alert("Remote Control Timed Out.");
! 166: }
! 167: } else {
! 168: setTimeout('wait();',100);
! 169: }
! 170: }
! 171:
! 172: function main() {
! 173: canceltim=setTimeout('tim=1;',20000);
! 174: checkdef();
! 175: wait();
! 176: }
! 177:
! 178: </script>
1.1 albertel 179: </head>
1.19 ! www 180: <body bgcolor="#FFFFFF" onLoad="main();">
! 181: <script>
! 182: menuloaded=0;
! 183: tim=0;
! 184: </script>
! 185: $windowinfo
1.6 www 186: <h1>Welcome!</h1>
187: </body>
1.1 albertel 188: </html>
189: ENDSUCCESS
190: }
191:
192: # --------------------------------------------------------------- Failed login!
193:
194: sub failed {
195: my ($r,$message) = @_;
196: $r->send_cgi_header(<<ENDFHEADER);
197: Content-type: text/html
198:
199: ENDFHEADER
200: $r->print(<<ENDFAILED);
201: <html>
202: <head>
1.4 www 203: <title>Unsuccessful Login to the LearningOnline Network with CAPA</title>
1.1 albertel 204: </head>
205: <html>
206: <body bgcolor="#FFFFFF">
207: <h1>Sorry ...</h1>
1.4 www 208: <h2>$message to use the Learning<i>Online</i> Network with CAPA</h2>
1.1 albertel 209: </body>
210: </html>
211: ENDFAILED
212: }
213:
214: # ---------------------------------------------------------------- Main handler
215:
216: sub handler {
217: my $r = shift;
218:
219: my $buffer;
220: $r->read($buffer,$r->header_in('Content-length'));
221: my @pairs=split(/&/,$buffer);
222: my $pair; my $name; my $value; my %FORM;
223: foreach $pair (@pairs) {
224: ($name,$value) = split(/=/,$pair);
1.7 www 225: $value =~ tr/+/ /;
226: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
1.1 albertel 227: $FORM{$name}=$value;
228: }
229:
230: if ((!$FORM{'uname'}) || (!$FORM{'upass'}) || (!$FORM{'udom'})) {
231: failed($r,'Username, password and domain need to be specified');
232: return OK;
233: }
234: $FORM{'uname'} =~ s/\W//g;
235: $FORM{'udom'} =~ s/\W//g;
236:
237: my $role = $r->dir_config('lonRole');
238: my $domain = $r->dir_config('lonDefDomain');
239: my $prodir = $r->dir_config('lonUsersDir');
240:
1.8 www 241: # ---------------------------------------- Get the information from login token
242:
243: my $tmpinfo=Apache::lonnet::reply('tmpget:'.$FORM{'logtoken'},
244: $FORM{'serverid'});
245:
246: if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost')) {
247: failed($r,'Login token missing, inaccessible or expired');
248: return OK;
249: }
250:
1.9 www 251: my ($key,$firsturl)=split(/&/,$tmpinfo);
1.8 www 252:
253: my $keybin=pack("H16",$key);
254:
255: my $cipher=new DES $keybin;
256:
257: my $upass=$cipher->decrypt(
258: unpack("a8",pack("H16",substr($FORM{'upass'},0,16))));
259:
260: $upass.=$cipher->decrypt(
261: unpack("a8",pack("H16",substr($FORM{'upass'},16,16))));
262:
263: $upass=substr($upass,1,ord(substr($upass,0,1)));
264:
1.1 albertel 265: # ---------------------------------------------------------------- Authenticate
266: my $authhost=Apache::lonnet::authenticate($FORM{'uname'},
1.8 www 267: $upass,
1.1 albertel 268: $FORM{'udom'});
269:
270: # --------------------------------------------------------------------- Failed?
271:
272: if ($authhost eq 'no_host') {
273: failed($r,'Username and/or password could not be authenticated');
274: return OK;
275: }
276:
1.13 www 277: if (($firsturl eq '') || ($firsturl eq '/adm/logout')) {
1.8 www 278: $firsturl='/res/adm/pages/index.html';
1.7 www 279: }
1.1 albertel 280:
1.8 www 281: success($r,$FORM{'uname'},$FORM{'udom'},$authhost,$firsturl);
1.1 albertel 282: return OK;
283: }
284:
285: 1;
286: __END__
1.7 www 287:
288:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>