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