1: # The LearningOnline Network
2: # User Authentication Module
3: #
4: # $Id: lonauth.pm,v 1.86 2006/10/12 18:18:01 albertel Exp $
5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28:
29: package Apache::lonauth;
30:
31: use strict;
32: use LONCAPA;
33: use Apache::Constants qw(:common);
34: use CGI qw(:standard);
35: use CGI::Cookie();
36: use DynaLoader; # for Crypt::DES version
37: use Crypt::DES;
38: use Apache::loncommon();
39: use Apache::lonnet;
40: use Apache::lonmenu();
41: use Fcntl qw(:flock);
42: use Apache::lonlocal;
43:
44: # ------------------------------------------------------------ Successful login
45: sub success {
46: my ($r, $username, $domain, $authhost, $lowerurl, $extra_env,
47: $form) = @_;
48:
49: # ------------------------------------------------------------ Get cookie ready
50: my $cookie =
51: &Apache::loncommon::init_user_environment($r, $username, $domain,
52: $authhost, $form,
53: {'extra_env' => $extra_env,});
54:
55: my $public=($username eq 'public' && $domain eq 'public');
56:
57: if ($public or $lowerurl eq 'noredirect') { return $cookie; }
58:
59: # -------------------------------------------------------------------- Log this
60:
61: &Apache::lonnet::log($domain,$username,$authhost,
62: "Login $ENV{'REMOTE_ADDR'}");
63:
64: # ------------------------------------------------- Check for critical messages
65:
66: my @what=&Apache::lonnet::dump('critical',$domain,$username);
67: if ($what[0]) {
68: if (($what[0] ne 'con_lost') && ($what[0]!~/^error\:/)) {
69: $lowerurl='/adm/email?critical=display';
70: }
71: }
72:
73: # ------------------------------------------------------------ Get cookie ready
74: $cookie="lonID=$cookie; path=/";
75: # -------------------------------------------------------- Menu script and info
76: my $windowinfo=&Apache::lonmenu::open($env{'browser.os'});
77: my $startupremote=&Apache::lonmenu::startupremote($lowerurl);
78: my $remoteinfo=&Apache::lonmenu::load_remote_msg($lowerurl);
79: my $setflags=&Apache::lonmenu::setflags();
80: my $maincall=&Apache::lonmenu::maincall();
81: my $start_page=&Apache::loncommon::start_page('Successful Login',
82: $startupremote,
83: {'no_inline_link' => 1,});
84: my $end_page =&Apache::loncommon::end_page();
85:
86: my $continuelink;
87: if (($env{'browser.interface'} eq 'textual') ||
88: ($env{'environment.remote'} eq 'off')) {
89: $continuelink="<a href=\"$lowerurl\">".&mt('Continue')."</a>";
90: }
91: # ------------------------------------------------- Output for successful login
92:
93: &Apache::loncommon::content_type($r,'text/html');
94: $r->header_out('Set-cookie' => $cookie);
95: $r->send_http_header;
96:
97: my %lt=&Apache::lonlocal::texthash(
98: 'wel' => 'Welcome',
99: 'mes' => 'Welcome to the Learning<i>Online</i> Network with CAPA. Please wait while your session is being set up',
100: 'pro' => 'Problems',
101: 'log' => 'loginproblems.html',
102: );
103: $r->print(<<ENDSUCCESS);
104: $start_page
105: $setflags
106: $windowinfo
107: <h1>$lt{'wel'}</h1>
108: $lt{'mes'}.<p>
109: <a href="/adm/$lt{'log'}">$lt{'pro'}?</a></p>
110: $remoteinfo
111: $maincall
112: $continuelink
113: $end_page
114: ENDSUCCESS
115: }
116:
117: # --------------------------------------------------------------- Failed login!
118:
119: sub failed {
120: my ($r,$message,$form) = @_;
121: my $start_page = &Apache::loncommon::start_page('Unsuccessful Login',undef,
122: {'no_inline_link' => 1,});
123: my $end_page = &Apache::loncommon::end_page();
124:
125: my %lt=('sorry' => &mt('Sorry ...'),
126: 'please' =>
127: &mt('Please [_1]log in again[_2].',
128: "<a href=\"/adm/login?username=$form->{'uname'}&domain=$form->{'udom'}\">",
129: '</a>'),
130: 'problemspage' => &mt('loginproblems.html'),
131: 'problems' => 'Problems',
132: );
133: &Apache::loncommon::content_type($r,'text/html');
134: $r->send_http_header;
135:
136: $r->print(<<ENDFAILED);
137: $start_page
138: <h1>$lt{'sorry'}</h1>
139: <p><b>$message</b></p>
140: <p>$lt{'please'}</p>
141: <p>
142: <a href="/adm/$lt{'problemspage'}">$lt{'problems'}</a></p>
143: $end_page
144: ENDFAILED
145: }
146:
147: # ------------------------------------------------------------------ Rerouting!
148:
149: sub reroute {
150: my ($r) = @_;
151: &Apache::loncommon::content_type($r,'text/html');
152: $r->send_http_header;
153: my $msg='<h1>Sorry ...</h1>
154: Please <a href="/">log in again</a>.';
155: &Apache::loncommon::simple_error_page($r,'Rerouting',$msg);
156: }
157:
158: # ---------------------------------------------------------------- Main handler
159:
160: sub handler {
161: my $r = shift;
162: my $form;
163: # Are we re-routing?
164: if (-e '/home/httpd/html/lon-status/reroute.txt') {
165: &reroute($r);
166: return OK;
167: }
168:
169: &Apache::lonlocal::get_language_handle($r);
170:
171: # -------------------------------- Prevent users from attempting to login twice
172: my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
173: my $lonid=$cookies{'lonID'};
174: my $cookie;
175: if ($lonid) {
176: my $handle=$lonid->value;
177: $handle=~s/\W//g;
178: my $lonidsdir=$r->dir_config('lonIDsDir');
179: if ((-e "$lonidsdir/$handle.id") && ($handle ne '')) {
180: # Indeed, a valid token is found
181: &Apache::loncommon::content_type($r,'text/html');
182: $r->send_http_header;
183: my $start_page =
184: &Apache::loncommon::start_page('Already logged in');
185: my $end_page =
186: &Apache::loncommon::end_page();
187: $r->print(<<ENDFAILED);
188: $start_page
189: <h1>You are already logged in</h1>
190: <p>Please either <a href="/adm/roles">continue the current session</a> or
191: <a href="/adm/logout">logout</a>.</p>
192: <p>
193: <a href="/adm/loginproblems.html">Problems?</a></p>
194: $end_page
195: ENDFAILED
196: return OK;
197: }
198: }
199:
200: # ---------------------------------------------------- No valid token, continue
201:
202:
203: my $buffer;
204: if ($r->header_in('Content-length') > 0) {
205: $r->read($buffer,$r->header_in('Content-length'),0);
206: }
207: my %form;
208: foreach my $pair (split(/&/,$buffer)) {
209: my ($name,$value) = split(/=/,$pair);
210: $value =~ tr/+/ /;
211: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
212: $form{$name}=$value;
213: }
214:
215: if ((!$form{'uname'}) || (!$form{'upass0'}) || (!$form{'udom'})) {
216: &failed($r,'Username, password and domain need to be specified.',
217: \%form);
218: return OK;
219: }
220:
221: # split user logging in and "su"-user
222:
223: ($form{'uname'},$form{'suname'})=split(/\:/,$form{'uname'});
224: $form{'uname'} =~ s/\W//g;
225: $form{'suname'} =~ s/\W//g;
226: $form{'udom'} =~ s/\W//g;
227:
228: my $role = $r->dir_config('lonRole');
229: my $domain = $r->dir_config('lonDefDomain');
230: my $prodir = $r->dir_config('lonUsersDir');
231:
232: # ---------------------------------------- Get the information from login token
233:
234: my $tmpinfo=Apache::lonnet::reply('tmpget:'.$form{'logtoken'},
235: $form{'serverid'});
236:
237: if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost')) {
238: &failed($r,'Information needed to verify your login information is missing, inaccessible or expired.',\%form);
239: return OK;
240: } else {
241: my $reply = &Apache::lonnet::reply('tmpdel:'.$form{'logtoken'},
242: $form{'serverid'});
243: if ( $reply ne 'ok' ) {
244: &failed($r,'Session could not be opened.',\%form);
245: &Apache::lonnet::logthis("ERROR got a reply of $reply when trying to contact ". $form{'serverid'}." to get login token");
246: return OK;
247: }
248: }
249: my ($key,$firsturl)=split(/&/,$tmpinfo);
250:
251: my $keybin=pack("H16",$key);
252:
253: my $cipher;
254: if ($Crypt::DES::VERSION>=2.03) {
255: $cipher=new Crypt::DES $keybin;
256: }
257: else {
258: $cipher=new DES $keybin;
259: }
260: my $upass='';
261: for (my $i=0;$i<=2;$i++) {
262: my $chunk=
263: $cipher->decrypt(unpack("a8",pack("H16",substr($form{'upass'.$i},0,16))));
264:
265: $chunk.=
266: $cipher->decrypt(unpack("a8",pack("H16",substr($form{'upass'.$i},16,16))));
267:
268: $chunk=substr($chunk,1,ord(substr($chunk,0,1)));
269: $upass.=$chunk;
270: }
271:
272: # ---------------------------------------------------------------- Authenticate
273: my $authhost=Apache::lonnet::authenticate($form{'uname'},
274: $upass,
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: \%form);
282: return OK;
283: }
284:
285: if (($firsturl eq '') ||
286: ($firsturl=~/^\/adm\/(logout|remote)/)) {
287: $firsturl='/adm/roles';
288: }
289: # --------------------------------- Are we attempting to login as somebody else?
290: if ($form{'suname'}) {
291: # ------------ see if the original user has enough privileges to pull this stunt
292: if (&Apache::lonnet::privileged($form{'uname'},$form{'udom'})) {
293: # ---------------------------------------------------- see if the su-user exists
294: unless (&Apache::lonnet::homeserver($form{'suname'},$form{'udom'})
295: eq 'no_host') {
296: &Apache::lonnet::logthis(&Apache::lonnet::homeserver($form{'suname'},$form{'udom'}));
297: # ------------------------------ see if the su-user is not too highly privileged
298: unless (&Apache::lonnet::privileged($form{'suname'},$form{'udom'})) {
299: # -------------------------------------------------------- actually switch users
300: &Apache::lonnet::logperm('User '.$form{'uname'}.' at '.$form{'udom'}.
301: ' logging in as '.$form{'suname'});
302: $form{'uname'}=$form{'suname'};
303: } else {
304: &Apache::lonnet::logthis('Attempted switch user to privileged user');
305: }
306: }
307: } else {
308: &Apache::lonnet::logthis('Non-privileged user attempting switch user');
309: }
310: }
311:
312: if ($r->dir_config("lonBalancer") eq 'yes') {
313: &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',undef,
314: \%form);
315: $r->internal_redirect('/adm/switchserver');
316: } else {
317: &success($r,$form{'uname'},$form{'udom'},$authhost,$firsturl,undef,
318: \%form);
319: }
320: return OK;
321: }
322:
323: 1;
324: __END__
325:
326:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>