# The LearningOnline Network # Allow access to password changing via a token sent to user's e-mail. # # $Id: resetpw.pm,v 1.28 2012/10/02 18:08:12 bisitz Exp $ # # Copyright Michigan State University Board of Trustees # # This file is part of the LearningOnline Network with CAPA (LON-CAPA). # # LON-CAPA is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # LON-CAPA is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with LON-CAPA; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # /home/httpd/html/adm/gpl.txt # # http://www.lon-capa.org/ # # =pod =head1 NAME Apache::resetpw: reset user password. =head1 SYNOPSIS Handles resetting of forgotten passwords. This is part of the LearningOnline Network with CAPA project described at http://www.lon-capa.org. =head1 OVERVIEW A user with an e-mail address associated with his/her LON-CAPA username can reset a forgotten password, using a link sent to the e-mail address if the authentication type for the account is "internal". =cut package Apache::resetpw; use strict; use Apache::Constants qw(:common); use Apache::lonacc; use Apache::lonnet; use Apache::loncommon; use Apache::lonlocal; use LONCAPA; sub handler { my $r = shift; &Apache::loncommon::content_type($r,'text/html'); $r->send_http_header; if ($r->header_only) { return OK; } my $contact_name = &mt('LON-CAPA helpdesk'); my $origmail = $r->dir_config('lonSupportEMail'); my $server = $r->dir_config('lonHostID'); my $defdom = &Apache::lonnet::default_login_domain(); my $contacts = &Apache::loncommon::build_recipient_list(undef,'helpdeskmail', $defdom,$origmail); my ($contact_email) = split(',',$contacts); my $handle = &Apache::lonnet::check_for_valid_session($r); my $lonidsdir=$r->dir_config('lonIDsDir'); if ($handle ne '') { if ($handle=~/^publicuser\_/) { unlink($r->dir_config('lonIDsDir')."/$handle.id"); } else { &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle); } } &Apache::lonacc::get_posted_cgi($r); &Apache::lonlocal::get_language_handle($r); &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['token']); my @emailtypes = ('permanentemail','critnotification','notification'); my $uname = &unescape($env{'form.uname'}); my $udom = $env{'form.udom'}; my $token = $env{'form.token'}; my $brcrum = []; my $bread_crumbs_component = 'Forgotten Password'; if ($token) { push (@{$brcrum}, {href => '/adm/resetpw', text => 'Update Password'}); $bread_crumbs_component = 'Reset Password'; } else { push (@{$brcrum}, {href => '/adm/resetpw', text => 'Account Information'}); if ($uname && $udom) { push (@{$brcrum}, {href => '/adm/resetpw', text => 'Result'}); } } my $args = {bread_crumbs => $brcrum, bread_crumbs_component => $bread_crumbs_component}; $r->print(&Apache::loncommon::start_page('Reset password','',$args)); $r->print('
' .&mt("An error occurred when sending a message to the e-mail address" ." associated with your LON-CAPA account." ." Please contact the [_1] ([_2]) for assistance." ,$contact_name,$contact_email) .'
'; } } else { $msg .= '' .&mt("An error occurred creating a token required for the" ." password reset process." ." Please contact the [_1] ([_2]) for assistance." ,$contact_name,$contact_email) .'
'; } return $msg; } sub send_mail { my ($domdesc,$email,$mailmsg,$contact_name,$contact_email) = @_; my $outcome; my $requestmail = "To: $email\n". "From: $contact_name <$contact_email>\n". "Subject: ".&mt('Your LON-CAPA account')."\n". "Content-type: text/plain\;charset=UTF-8\n". "\n\n".$mailmsg."\n\n". &mt('[_1] LON-CAPA support team',$domdesc)."\n". "$contact_email\n"; if (open(MAIL, "|/usr/lib/sendmail -oi -t -odb")) { print MAIL $requestmail; close(MAIL); $outcome = 'ok'; } else { $outcome = 'fail'; } return $outcome; } sub invalid_state { my ($error,$domdesc,$contact_name,$contact_email) = @_; my $msg; if ($error eq 'invalid') { $msg = ''.&mt('The username you provided was not verified as a valid username in the LON-CAPA system for the [_1] domain.',$domdesc) .'
'.&mt('Please [_1]go back[_2] and try again.','',''); } else { if ($error eq 'baduseremail') { $msg = &mt('The e-mail address you provided does not appear to be a valid address.'); } elsif ($error eq 'mismatch') { $msg = &mt('The e-mail address you provided does not match the address recorded in the LON-CAPA system for the username and domain you provided.'); } elsif ($error eq 'missing') { $msg = &mt('A valid e-mail address was not located in the LON-CAPA system for the username and domain you provided.'); } elsif ($error eq 'authentication') { $msg = &mt('The username you provided uses an authentication type which can not be reset directly via LON-CAPA.'); } $msg = ''.$msg.'
'; if ($contact_email ne '') { my $escuri = &HTML::Entities::encode('/adm/resetpw','&<>"'); $msg .= '' .&mt('An e-mail confirming setting of the password' .' for your LON-CAPA account has been sent to [_1].' ,$data{'email'}) .'
'; } else { $msg .= '' .&mt('An error occurred when sending e-mail to [_1]' .' confirming setting of your new password.' ,$data{'email'}) .'
'; } $msg .= '' .&mt('A problem occurred when attempting to reset' .' the password for your account.' .' Please try again from your [_1]home server[_2].' ,$opentag,$closetag) .'
'; } } else { $msg .= &generic_failure_msg($contact_name,$contact_email); } } else { $r->print(&mt('The token included in an e-mail sent to you [_1] has been verified, so you may now proceed to reset the password for your LON-CAPA account.',$reqtime).'' .&mt('Sorry, the token generated when you requested a password reset has expired. Please submit a [_1]new request[_2], and follow the link to the web page included in the new e-mail that will be sent to you, to allow you to enter a new password.' ,'','') .'
'; } } else { $msg .= '' .&mt('Sorry, the URL generated when you requested reset of your password contained incomplete information. Please submit a [_1]new request[_2] for a password reset, and use the new URL that will be sent to your e-mail account to complete the process.' ,'','') .'
'; } return $msg; } sub generic_failure_msg { my ($contact_name,$contact_email) = @_; return '' .&mt('A problem occurred when attempting to reset the password for your account.' .' Please contact the [_1] - ([_2]) for assistance.', $contact_name,''.$contact_email.'') .'
'; } sub create_passwd { my $passwd = ''; my @letts = ("a".."z"); for (my $i=0; $i<8; $i++) { my $lettnum = int(rand(2)); my $item = ''; if ($lettnum) { $item = $letts[int(rand(26))]; my $uppercase = int(rand(2)); if ($uppercase) { $item =~ tr/a-z/A-Z/; } } else { $item = int(rand(10)); } $passwd .= $item; } return ($passwd); } 1;