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