1: # The LearningOnline Network
2: # User Authentication Module
3: #
4: # $Id: lonauth.pm,v 1.99 2009/07/03 10:22:00 bisitz 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 Apache::createaccount;
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 $brcrum = [{'href' => '',
82: 'text' => 'Successful Login'},];
83: my $start_page=&Apache::loncommon::start_page('Successful Login',
84: $startupremote,
85: {'no_inline_link' => 1,
86: 'bread_crumbs' => $brcrum,});
87: my $end_page =&Apache::loncommon::end_page();
88:
89: my $continuelink;
90: if ($env{'environment.remote'} eq 'off') {
91: $continuelink="<a href=\"$lowerurl\">".&mt('Continue')."</a>";
92: }
93: # ------------------------------------------------- Output for successful login
94:
95: &Apache::loncommon::content_type($r,'text/html');
96: $r->header_out('Set-cookie' => $cookie);
97: $r->send_http_header;
98:
99: my %lt=&Apache::lonlocal::texthash(
100: 'wel' => 'Welcome',
101: 'mes' => 'Welcome to the Learning<i>Online</i> Network with CAPA. Please wait while your session is being set up.',
102: 'pro' => 'Login problems?',
103: 'log' => 'loginproblems.html',
104: );
105: $r->print(<<ENDSUCCESS);
106: $start_page
107: $setflags
108: $windowinfo
109: <h1>$lt{'wel'}</h1>
110: $lt{'mes'}<p>
111: <a href="/adm/$lt{'log'}">$lt{'pro'}</a></p>
112: $remoteinfo
113: $maincall
114: $continuelink
115: $end_page
116: ENDSUCCESS
117: }
118:
119: # --------------------------------------------------------------- Failed login!
120:
121: sub failed {
122: my ($r,$message,$form) = @_;
123: my $start_page = &Apache::loncommon::start_page('Unsuccessful Login',undef,
124: {'no_inline_link' => 1,});
125: my $end_page = &Apache::loncommon::end_page();
126: &Apache::loncommon::content_type($r,'text/html');
127: $r->send_http_header;
128: $r->print(
129: $start_page
130: .'<h1>'.&mt('Sorry ...').'</h1>'
131: .'<p class="LC_warning">'.&mt($message).'</p>'
132: .'<p>'.&mt('Please [_1]log in again[_2].'
133: ,"<a href=\"/adm/login?username=$form->{'uname'}&domain=$form->{'udom'}\">",'</a>')
134: .'</p>'
135: .'<p><a href="/adm/loginproblems.html">'.&mt('Login problems?').'</a></p>'
136: .$end_page
137: );
138: }
139:
140: # ------------------------------------------------------------------ Rerouting!
141:
142: sub reroute {
143: my ($r) = @_;
144: &Apache::loncommon::content_type($r,'text/html');
145: $r->send_http_header;
146: my $msg='<h1>'.&mt('Sorry ...').'</h1>'
147: .&mt('Please [_1]log in again[_2].');
148: &Apache::loncommon::simple_error_page($r,'Rerouting',$msg);
149: }
150:
151: # ---------------------------------------------------------------- Main handler
152:
153: sub handler {
154: my $r = shift;
155: my $form;
156: # Are we re-routing?
157: if (-e '/home/httpd/html/lon-status/reroute.txt') {
158: &reroute($r);
159: return OK;
160: }
161:
162: &Apache::lonlocal::get_language_handle($r);
163:
164: # -------------------------------- Prevent users from attempting to login twice
165: my $handle = &Apache::lonnet::check_for_valid_session($r);
166: if ($handle ne '') {
167: # Indeed, a valid token is found
168: &Apache::loncommon::content_type($r,'text/html');
169: $r->send_http_header;
170: my $start_page =
171: &Apache::loncommon::start_page('Already logged in');
172: my $end_page =
173: &Apache::loncommon::end_page();
174: $r->print(
175: $start_page
176: .'<h1>'.&mt('You are already logged in!').'</h1>'
177: .'<p>'.&mt('Please either [_1]continue the current session[_2] or [_3]log out[_4].'
178: ,'<a href="/adm/roles">','</a>','<a href="/adm/logout">','</a>')
179: .'</p>'
180: .'<p><a href="/adm/loginproblems.html">'.&mt('Login problems?').'</a></p>'
181: .$end_page
182: );
183: return OK;
184: }
185:
186: # ---------------------------------------------------- No valid token, continue
187:
188:
189: my $buffer;
190: if ($r->header_in('Content-length') > 0) {
191: $r->read($buffer,$r->header_in('Content-length'),0);
192: }
193: my %form;
194: foreach my $pair (split(/&/,$buffer)) {
195: my ($name,$value) = split(/=/,$pair);
196: $value =~ tr/+/ /;
197: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
198: $form{$name}=$value;
199: }
200:
201: if ((!$form{'uname'}) || (!$form{'upass0'}) || (!$form{'udom'})) {
202: &failed($r,'Username, password and domain need to be specified.',
203: \%form);
204: return OK;
205: }
206:
207: # split user logging in and "su"-user
208:
209: ($form{'uname'},$form{'suname'})=split(/\:/,$form{'uname'});
210: $form{'uname'} = &LONCAPA::clean_username($form{'uname'});
211: $form{'suname'}= &LONCAPA::clean_username($form{'suname'});
212: $form{'udom'} = &LONCAPA::clean_domain( $form{'udom'});
213:
214: my $role = $r->dir_config('lonRole');
215: my $domain = $r->dir_config('lonDefDomain');
216: my $prodir = $r->dir_config('lonUsersDir');
217: my $contact_name = &mt('LON-CAPA helpdesk');
218:
219: # ---------------------------------------- Get the information from login token
220:
221: my $tmpinfo=Apache::lonnet::reply('tmpget:'.$form{'logtoken'},
222: $form{'serverid'});
223:
224: if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost')) {
225: &failed($r,'Information needed to verify your login information is missing, inaccessible or expired.',\%form);
226: return OK;
227: } else {
228: my $reply = &Apache::lonnet::reply('tmpdel:'.$form{'logtoken'},
229: $form{'serverid'});
230: if ( $reply ne 'ok' ) {
231: &failed($r,'Session could not be opened.',\%form);
232: &Apache::lonnet::logthis("ERROR got a reply of $reply when trying to contact ". $form{'serverid'}." to get login token");
233: return OK;
234: }
235: }
236: if (!&Apache::lonnet::domain($form{'udom'})) {
237: &failed($r,'The domain you provided is not a valid LON-CAPA domain.',\%form);
238: return OK;
239: }
240: my ($key,$firsturl)=split(/&/,$tmpinfo);
241:
242: my $keybin=pack("H16",$key);
243:
244: my $cipher;
245: if ($Crypt::DES::VERSION>=2.03) {
246: $cipher=new Crypt::DES $keybin;
247: }
248: else {
249: $cipher=new DES $keybin;
250: }
251: my $upass='';
252: for (my $i=0;$i<=2;$i++) {
253: my $chunk=
254: $cipher->decrypt(unpack("a8",pack("H16",substr($form{'upass'.$i},0,16))));
255:
256: $chunk.=
257: $cipher->decrypt(unpack("a8",pack("H16",substr($form{'upass'.$i},16,16))));
258:
259: $chunk=substr($chunk,1,ord(substr($chunk,0,1)));
260: $upass.=$chunk;
261: }
262:
263: # ---------------------------------------------------------------- Authenticate
264: my @cancreate;
265: my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$form{'udom'});
266: if (ref($domconfig{'usercreation'}) eq 'HASH') {
267: if (ref($domconfig{'usercreation'}{'cancreate'}) eq 'HASH') {
268: if (ref($domconfig{'usercreation'}{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
269: @cancreate = @{$domconfig{'usercreation'}{'cancreate'}{'selfcreate'}};
270: } elsif (($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne 'none') &&
271: ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne '')) {
272: @cancreate = ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'});
273: }
274: }
275: }
276: my $defaultauth;
277: if (grep(/^login$/,@cancreate)) {
278: $defaultauth = 1;
279: }
280: my $authhost=Apache::lonnet::authenticate($form{'uname'},$upass,
281: $form{'udom'},$defaultauth);
282:
283: # --------------------------------------------------------------------- Failed?
284:
285: if ($authhost eq 'no_host') {
286: &failed($r,'Username and/or password could not be authenticated.',
287: \%form);
288: return OK;
289: } elsif ($authhost eq 'no_account_on_host') {
290: my %domconfig =
291: &Apache::lonnet::get_dom('configuration',['usercreation'],$form{'udom'});
292: if (grep(/^login$/,@cancreate)) {
293: my $start_page =
294: &Apache::loncommon::start_page('Create a user account in LON-CAPA',
295: '',{'no_inline_link' => 1,});
296: my $domdesc = &Apache::lonnet::domain($form{'udom'},'description');
297: my $lonhost = $r->dir_config('lonHostID');
298: my $origmail = $Apache::lonnet::perlvar{'lonSupportEMail'};
299: my $contacts =
300: &Apache::loncommon::build_recipient_list(undef,'helpdeskmail',
301: $form{'udom'},$origmail);
302: my ($contact_email) = split(',',$contacts);
303: my $output = &Apache::createaccount::username_check($form{'uname'},
304: $form{'udom'},$domdesc,'',
305: $lonhost,$contact_email,$contact_name);
306: &Apache::loncommon::content_type($r,'text/html');
307: $r->send_http_header;
308: &Apache::createaccount::print_header($r,$start_page);
309: $r->print('<h3>'.&mt('Account creation').'</h3>'.
310: &mt('Although your username and password were authenticated, you do not currently have a LON-CAPA account at this institution.').'<br />'.
311: $output.&Apache::loncommon::end_page());
312: return OK;
313: } else {
314: &failed($r,'Although your username and password were authenticated, you do not currently have a LON-CAPA account in this domain, and you are not permitted to create one.',\%form);
315: return OK;
316: }
317: }
318:
319: if (($firsturl eq '') ||
320: ($firsturl=~/^\/adm\/(logout|remote)/)) {
321: $firsturl='/adm/roles';
322: }
323: # --------------------------------- Are we attempting to login as somebody else?
324: if ($form{'suname'}) {
325: # ------------ see if the original user has enough privileges to pull this stunt
326: if (&Apache::lonnet::privileged($form{'uname'},$form{'udom'})) {
327: # ---------------------------------------------------- see if the su-user exists
328: unless (&Apache::lonnet::homeserver($form{'suname'},$form{'udom'})
329: eq 'no_host') {
330: &Apache::lonnet::logthis(&Apache::lonnet::homeserver($form{'suname'},$form{'udom'}));
331: # ------------------------------ see if the su-user is not too highly privileged
332: unless (&Apache::lonnet::privileged($form{'suname'},$form{'udom'})) {
333: # -------------------------------------------------------- actually switch users
334: &Apache::lonnet::logperm('User '.$form{'uname'}.' at '.$form{'udom'}.
335: ' logging in as '.$form{'suname'});
336: $form{'uname'}=$form{'suname'};
337: } else {
338: &Apache::lonnet::logthis('Attempted switch user to privileged user');
339: }
340: }
341: } else {
342: &Apache::lonnet::logthis('Non-privileged user attempting switch user');
343: }
344: }
345:
346: if ($r->dir_config("lonBalancer") eq 'yes') {
347: &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',undef,
348: \%form);
349: $r->internal_redirect('/adm/switchserver');
350: } else {
351: &success($r,$form{'uname'},$form{'udom'},$authhost,$firsturl,undef,
352: \%form);
353: }
354: return OK;
355: }
356:
357: 1;
358: __END__
359:
360:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>