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