1 <?php
2 /*
3 * This code is part of GOsa (http://www.gosa-project.org)
4 * Copyright (C) 2003-2008 GONICUS GmbH
5 *
6 * ID: $$Id$$
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
23 class password extends plugin
24 {
25 /* Definitions */
26 var $plHeadline = "Password";
27 var $plDescription = "Change user password";
29 var $proposal = "";
30 var $proposalEnabled = FALSE;
31 var $proposalSelected = FALSE;
33 var $forcedHash = NULL;
36 function password(&$config, $dn= NULL, $parent= NULL)
37 {
38 plugin::plugin($config, $dn, $parent);
40 // Try to generate a password proposal, if this is successfull
41 // then preselect the proposal usage.
42 $this->refreshProposal();
43 if($this->proposal != ""){
44 $this->proposalSelected = TRUE;
45 }
46 }
48 function forceHash($hash)
49 {
50 $this->forcedHash = $hash;
51 }
53 function refreshProposal()
54 {
55 $this->proposal = passwordMethod::getPasswordProposal($this->config);
56 $this->proposalEnabled = (!empty($this->proposal));
57 }
59 function execute()
60 {
61 plugin::execute();
62 $smarty = get_smarty();
63 $ui = get_userinfo();
65 /* Get acls */
66 $password_ACLS = $ui->get_permissions($ui->dn,"users/password");
67 $smarty->assign("ChangeACL" , $password_ACLS);
68 $smarty->assign("NotAllowed" , !preg_match("/w/i",$password_ACLS));
70 /* Display expiration template */
71 $smarty->assign("passwordExpired", FALSE);
72 if ($this->config->get_cfg_value("handleExpiredAccounts") == "true"){
73 $expired= ldap_expired_account($this->config, $ui->dn, $ui->username);
74 $smarty->assign("passwordExpired", $expired & POSIX_FORCE_PASSWORD_CHANGE);
75 if($expired == POSIX_DISALLOW_PASSWORD_CHANGE){
76 return($smarty->fetch(get_template_path("nochange.tpl", TRUE)));
77 }
78 }
81 // Refresh proposal if requested
82 if(isset($_POST['refreshProposal'])) $this->refreshProposal();
83 $smarty->assign("proposal" , $this->proposal);
84 $smarty->assign("proposalEnabled" , $this->proposalEnabled);
85 $smarty->assign("proposalSelected" , $this->proposalSelected);
89 if(isset($_POST['proposalSelected'])) $this->proposalSelected = get_post('proposalSelected') == 1;
92 /* Pwd change requested */
93 if (isset($_POST['password_finish'])){
96 if($this->proposalSelected){
97 $current_password = get_post('current_password');
98 $new_password = $this->proposal;
99 $repeated_password = $this->proposal;
100 }else{
101 $current_password = get_post('current_password');
102 $new_password = get_post('new_password');
103 $repeated_password = get_post('repeated_password');
104 }
107 /* Should we check different characters in new password */
108 $check_differ = $this->config->get_cfg_value("passwordMinDiffer") != "";
109 $differ = $this->config->get_cfg_value("passwordMinDiffer", 0);
111 /* Enable length check ? */
112 $check_length = $this->config->get_cfg_value("passwordMinLength") != "";
113 $length = $this->config->get_cfg_value("passwordMinLength", 0);
115 /* Call external password quality hook ?*/
116 $check_hook = $this->config->get_cfg_value("passwordHook") != "";
117 $hook = $this->config->get_cfg_value("passwordHook")." ".$ui->username." ".$_POST['current_password']." ".$_POST['new_password'];
118 if($check_hook){
119 exec($hook,$resarr);
120 $check_hook_output = "";
121 if(count($resarr) > 0) {
122 $check_hook_output= join('\n', $resarr);
123 }
124 }
126 /* Check given values */
127 if(!isset($current_password) || empty($current_password)){
128 msg_dialog::display(_("Password change"),
129 _("You need to specify your current password in order to proceed."),WARNING_DIALOG);
130 }elseif ($new_password != $repeated_password){
131 msg_dialog::display(_("Password change"),
132 _("The passwords you've entered as 'New password' and 'Repeated new password' do not match."),WARNING_DIALOG);
133 } elseif ($new_password == ""){
134 msg_dialog::display(_("Password change"),
135 _("The password you've entered as 'New password' is empty."),WARNING_DIALOG);
136 }elseif($check_differ && (substr($current_password, 0, $differ) == substr($new_password, 0, $differ))){
137 msg_dialog::display(_("Password change"),
138 _("The password used as new and current are too similar."),WARNING_DIALOG);
139 }elseif($check_length && (strlen($new_password) < $length)){
140 msg_dialog::display(_("Password change"),
141 _("The password used as new is to short."),WARNING_DIALOG);
142 }elseif($check_hook && $check_hook_output != ""){
143 msg_dialog::display(_("Password change"),
144 sprintf(_("External password changer reported a problem: %s."),$check_hook_output),WARNING_DIALOG);
145 }else{
147 /* Try to connect via current password */
148 $tldap = new LDAP(
149 $ui->dn,
150 $current_password,
151 $this->config->current['SERVER'],
152 $this->config->get_cfg_value("ldapFollowReferrals") == "true",
153 $this->config->get_cfg_value("ldapTLS") == "true");
155 /* connection Successfull ? */
156 if (!$tldap->success()){
157 msg_dialog::display(_("Password change"),
158 _("The password you've entered as your current password doesn't match the real one."),WARNING_DIALOG);
159 }else{
161 /* Check GOsa permissions */
162 if (!preg_match("/w/i",$password_ACLS)){
163 msg_dialog::display(_("Password change"),
164 _("You have no permission to change your password."),WARNING_DIALOG);
165 }else{
166 $this->change_password($ui->dn, $new_password,$this->forcedHash);
167 gosa_log ("User/password has been changed");
168 $ui->password= $new_password;
169 session::set('ui',$ui);
170 #$this->handle_post_events("modify",array("userPassword" => $new_password));
171 return($smarty->fetch(get_template_path("changed.tpl", TRUE)));
172 }
173 }
174 }
175 }
176 return($smarty->fetch(get_template_path("password.tpl", TRUE)));
177 }
179 function change_password($dn, $pwd, $hash)
180 {
181 if(!$hash){
182 change_password ($dn, $pwd);
183 }else{
184 change_password ($dn, $pwd,0, $hash);
185 }
186 }
189 function remove_from_parent()
190 {
191 $this->handle_post_events("remove");
192 }
194 function save()
195 {
196 }
198 static function plInfo()
199 {
200 return (array(
201 "plDescription" => _("User password"),
202 "plSelfModify" => TRUE,
203 "plDepends" => array("user"),
204 "plPriority" => 10,
205 "plSection" => array("personal" => _("My account")),
206 "plCategory" => array("users"),
207 "plOptions" => array(),
209 "plProvidedAcls" => array())
210 );
211 }
213 }
214 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
215 ?>