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