1: # The LearningOnline Network
2: # User Authentication Module
3: #
4: # $Id: lonauth.pm,v 1.89 2007/10/01 23:56:14 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 DynaLoader; # for Crypt::DES version
36: use Crypt::DES;
37: use Apache::loncommon();
38: use Apache::lonnet;
39: use Apache::lonmenu();
40: use Fcntl qw(:flock);
41: use Apache::lonlocal;
42:
43: # ------------------------------------------------------------ Successful login
44: sub success {
45: my ($r, $username, $domain, $authhost, $lowerurl, $extra_env,
46: $form) = @_;
47:
48: # ------------------------------------------------------------ Get cookie ready
49: my $cookie =
50: &Apache::loncommon::init_user_environment($r, $username, $domain,
51: $authhost, $form,
52: {'extra_env' => $extra_env,});
53:
54: my $public=($username eq 'public' && $domain eq 'public');
55:
56: if ($public or $lowerurl eq 'noredirect') { return $cookie; }
57:
58: # -------------------------------------------------------------------- Log this
59:
60: &Apache::lonnet::log($domain,$username,$authhost,
61: "Login $ENV{'REMOTE_ADDR'}");
62:
63: # ------------------------------------------------- Check for critical messages
64:
65: my @what=&Apache::lonnet::dump('critical',$domain,$username);
66: if ($what[0]) {
67: if (($what[0] ne 'con_lost') && ($what[0]!~/^error\:/)) {
68: $lowerurl='/adm/email?critical=display';
69: }
70: }
71:
72: # ------------------------------------------------------------ Get cookie ready
73: $cookie="lonID=$cookie; path=/";
74: # -------------------------------------------------------- Menu script and info
75: my $windowinfo=&Apache::lonmenu::open($env{'browser.os'});
76: my $startupremote=&Apache::lonmenu::startupremote($lowerurl);
77: my $remoteinfo=&Apache::lonmenu::load_remote_msg($lowerurl);
78: my $setflags=&Apache::lonmenu::setflags();
79: my $maincall=&Apache::lonmenu::maincall();
80: my $start_page=&Apache::loncommon::start_page('Successful Login',
81: $startupremote,
82: {'no_inline_link' => 1,});
83: my $end_page =&Apache::loncommon::end_page();
84:
85: my $continuelink;
86: if (($env{'browser.interface'} eq 'textual') ||
87: ($env{'environment.remote'} eq 'off')) {
88: $continuelink="<a href=\"$lowerurl\">".&mt('Continue')."</a>";
89: }
90: # ------------------------------------------------- Output for successful login
91:
92: &Apache::loncommon::content_type($r,'text/html');
93: $r->header_out('Set-cookie' => $cookie);
94: $r->send_http_header;
95:
96: my %lt=&Apache::lonlocal::texthash(
97: 'wel' => 'Welcome',
98: 'mes' => 'Welcome to the Learning<i>Online</i> Network with CAPA. Please wait while your session is being set up',
99: 'pro' => 'Problems',
100: 'log' => 'loginproblems.html',
101: );
102: $r->print(<<ENDSUCCESS);
103: $start_page
104: $setflags
105: $windowinfo
106: <h1>$lt{'wel'}</h1>
107: $lt{'mes'}.<p>
108: <a href="/adm/$lt{'log'}">$lt{'pro'}?</a></p>
109: $remoteinfo
110: $maincall
111: $continuelink
112: $end_page
113: ENDSUCCESS
114: }
115:
116: # --------------------------------------------------------------- Failed login!
117:
118: sub failed {
119: my ($r,$message,$form) = @_;
120: my $start_page = &Apache::loncommon::start_page('Unsuccessful Login',undef,
121: {'no_inline_link' => 1,});
122: my $end_page = &Apache::loncommon::end_page();
123:
124: $message = &mt($message);
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 $handle = &Apache::lonnet::check_for_valid_session($r);
173: if ($handle ne '') {
174: # Indeed, a valid token is found
175: &Apache::loncommon::content_type($r,'text/html');
176: $r->send_http_header;
177: my $start_page =
178: &Apache::loncommon::start_page('Already logged in');
179: my $end_page =
180: &Apache::loncommon::end_page();
181: $r->print(<<ENDFAILED);
182: $start_page
183: <h1>You are already logged in</h1>
184: <p>Please either <a href="/adm/roles">continue the current session</a> or
185: <a href="/adm/logout">logout</a>.</p>
186: <p>
187: <a href="/adm/loginproblems.html">Problems?</a></p>
188: $end_page
189: ENDFAILED
190: return OK;
191: }
192:
193: # ---------------------------------------------------- No valid token, continue
194:
195:
196: my $buffer;
197: if ($r->header_in('Content-length') > 0) {
198: $r->read($buffer,$r->header_in('Content-length'),0);
199: }
200: my %form;
201: foreach my $pair (split(/&/,$buffer)) {
202: my ($name,$value) = split(/=/,$pair);
203: $value =~ tr/+/ /;
204: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
205: $form{$name}=$value;
206: }
207:
208: if ((!$form{'uname'}) || (!$form{'upass0'}) || (!$form{'udom'})) {
209: &failed($r,'Username, password and domain need to be specified.',
210: \%form);
211: return OK;
212: }
213:
214: # split user logging in and "su"-user
215:
216: ($form{'uname'},$form{'suname'})=split(/\:/,$form{'uname'});
217: $form{'uname'} = &LONCAPA::clean_username($form{'uname'});
218: $form{'suname'}= &LONCAPA::clean_username($form{'suname'});
219: $form{'udom'} = &LONCAPA::clean_domain( $form{'udom'});
220:
221: my $role = $r->dir_config('lonRole');
222: my $domain = $r->dir_config('lonDefDomain');
223: my $prodir = $r->dir_config('lonUsersDir');
224:
225: # ---------------------------------------- Get the information from login token
226:
227: my $tmpinfo=Apache::lonnet::reply('tmpget:'.$form{'logtoken'},
228: $form{'serverid'});
229:
230: if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost')) {
231: &failed($r,'Information needed to verify your login information is missing, inaccessible or expired.',\%form);
232: return OK;
233: } else {
234: my $reply = &Apache::lonnet::reply('tmpdel:'.$form{'logtoken'},
235: $form{'serverid'});
236: if ( $reply ne 'ok' ) {
237: &failed($r,'Session could not be opened.',\%form);
238: &Apache::lonnet::logthis("ERROR got a reply of $reply when trying to contact ". $form{'serverid'}." to get login token");
239: return OK;
240: }
241: }
242: my ($key,$firsturl)=split(/&/,$tmpinfo);
243:
244: my $keybin=pack("H16",$key);
245:
246: my $cipher;
247: if ($Crypt::DES::VERSION>=2.03) {
248: $cipher=new Crypt::DES $keybin;
249: }
250: else {
251: $cipher=new DES $keybin;
252: }
253: my $upass='';
254: for (my $i=0;$i<=2;$i++) {
255: my $chunk=
256: $cipher->decrypt(unpack("a8",pack("H16",substr($form{'upass'.$i},0,16))));
257:
258: $chunk.=
259: $cipher->decrypt(unpack("a8",pack("H16",substr($form{'upass'.$i},16,16))));
260:
261: $chunk=substr($chunk,1,ord(substr($chunk,0,1)));
262: $upass.=$chunk;
263: }
264:
265: # ---------------------------------------------------------------- Authenticate
266: my $authhost=Apache::lonnet::authenticate($form{'uname'},
267: $upass,
268: $form{'udom'});
269:
270: # --------------------------------------------------------------------- Failed?
271:
272: if ($authhost eq 'no_host') {
273: &failed($r,'Username and/or password could not be authenticated.',
274: \%form);
275: return OK;
276: }
277:
278: if (($firsturl eq '') ||
279: ($firsturl=~/^\/adm\/(logout|remote)/)) {
280: $firsturl='/adm/roles';
281: }
282: # --------------------------------- Are we attempting to login as somebody else?
283: if ($form{'suname'}) {
284: # ------------ see if the original user has enough privileges to pull this stunt
285: if (&Apache::lonnet::privileged($form{'uname'},$form{'udom'})) {
286: # ---------------------------------------------------- see if the su-user exists
287: unless (&Apache::lonnet::homeserver($form{'suname'},$form{'udom'})
288: eq 'no_host') {
289: &Apache::lonnet::logthis(&Apache::lonnet::homeserver($form{'suname'},$form{'udom'}));
290: # ------------------------------ see if the su-user is not too highly privileged
291: unless (&Apache::lonnet::privileged($form{'suname'},$form{'udom'})) {
292: # -------------------------------------------------------- actually switch users
293: &Apache::lonnet::logperm('User '.$form{'uname'}.' at '.$form{'udom'}.
294: ' logging in as '.$form{'suname'});
295: $form{'uname'}=$form{'suname'};
296: } else {
297: &Apache::lonnet::logthis('Attempted switch user to privileged user');
298: }
299: }
300: } else {
301: &Apache::lonnet::logthis('Non-privileged user attempting switch user');
302: }
303: }
304:
305: if ($r->dir_config("lonBalancer") eq 'yes') {
306: &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',undef,
307: \%form);
308: $r->internal_redirect('/adm/switchserver');
309: } else {
310: &success($r,$form{'uname'},$form{'udom'},$authhost,$firsturl,undef,
311: \%form);
312: }
313: return OK;
314: }
315:
316: 1;
317: __END__
318:
319:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>