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