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 function displayPWchanger()
24 {
25 global $smarty;
27 $smarty->display(get_template_path('password.tpl'));
28 exit();
29 }
31 /* Load required includes */
32 require_once ("../include/php_setup.inc");
33 require_once ("functions.inc");
35 if(!class_exists("log")){
36 require_once("class_log.inc");
37 }
39 header("Content-type: text/html; charset=UTF-8");
41 session::start();
43 /* Destroy old session if exists.
44 Else you will get your old session back, if you not logged out correctly. */
45 if(is_array(session::get_all()) && count(session::get_all())){
46 session::destroy();
47 session::start();
48 }
50 /* Reset errors */
51 session::set('js',true);
52 session::set('errors',"");
53 session::set('errorsAlreadyPosted',array());
54 session::set('LastError',"");
56 /* Check if CONFIG_FILE is accessible */
57 if (!is_readable(CONFIG_DIR."/".CONFIG_FILE)){
58 msg_dialog::display(_("Fatal error"), sprintf(_("GOsa configuration %s/%s is not readable. Aborted."),CONFIG_DIR,CONFIG_FILE), FATAL_ERROR_DIALOG);
59 exit;
60 }
62 /* Parse configuration file */
63 $config= new config(CONFIG_DIR."/".CONFIG_FILE, $BASE_DIR);
64 session::set('DEBUGLEVEL',$config->data['MAIN']['DEBUGLEVEL']);
65 if ($_SERVER["REQUEST_METHOD"] != "POST"){
66 @DEBUG (DEBUG_CONFIG, __LINE__, __FUNCTION__, __FILE__, $config->data, "config");
67 }
69 /* Set template compile directory */
70 if (isset ($config->data['MAIN']['COMPILE'])){
71 $smarty->compile_dir= $config->data['MAIN']['COMPILE'];
72 } else {
73 $smarty->compile_dir= '/var/spool/gosa';
74 }
76 /* Check for compile directory */
77 if (!(is_dir($smarty->compile_dir) && is_writable($smarty->compile_dir))){
78 msg_dialog::display(_("Configuration error"), sprintf(_("Directory '%s' specified as compile directory is not accessible!"),
79 $smarty->compile_dir), FATAL_ERROR_DIALOG);
80 exit;
81 }
83 /* Check for old files in compile directory */
84 clean_smarty_compile_dir($smarty->compile_dir);
86 /* Language setup */
87 if ($config->data['MAIN']['LANG'] == ""){
88 $lang= get_browser_language();
89 } else {
90 $lang= $config->data['MAIN']['LANG'];
91 }
92 $lang.=".UTF-8";
93 putenv("LANGUAGE=");
94 putenv("LANG=$lang");
95 setlocale(LC_ALL, $lang);
96 $GLOBALS['t_language']= $lang;
97 $GLOBALS['t_gettext_message_dir'] = $BASE_DIR.'/locale/';
99 /* Set the text domain as 'messages' */
100 $domain = 'messages';
101 bindtextdomain($domain, LOCALE_DIR);
102 textdomain($domain);
104 /* Generate server list */
105 $servers= array();
106 foreach ($config->data['LOCATIONS'] as $key => $ignored){
107 $servers[$key]= $key;
108 }
109 if (isset($_POST['server'])){
110 $directory= validate($_POST['server']);
111 } else {
112 $directory= $config->data['MAIN']['DEFAULT'];
114 if(!isset($servers[$directory])){
115 $directory = key($servers);
116 }
117 }
118 if (isset($_GET['directory']) && isset($servers[$_GET['directory']])){
119 $smarty->assign ("show_directory_chooser", false);
120 $directory= validate($_GET['directory']);
121 } else {
122 $smarty->assign ("server_options", $servers);
123 $smarty->assign ("server_id", $directory);
124 $smarty->assign ("show_directory_chooser", true);
125 }
127 /* Set config to selected one */
128 $config->set_current($directory);
129 session::set('config',$config);
131 if ($_SERVER["REQUEST_METHOD"] != "POST"){
132 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, $lang, "Setting language to");
133 }
136 /* Check for SSL connection */
137 $ssl= "";
138 if (!isset($_SERVER['HTTPS']) ||
139 !stristr($_SERVER['HTTPS'], "on")) {
141 if (empty($_SERVER['REQUEST_URI'])) {
142 $ssl= "https://".$_SERVER['HTTP_HOST'].
143 $_SERVER['PATH_INFO'];
144 } else {
145 $ssl= "https://".$_SERVER['HTTP_HOST'].
146 $_SERVER['REQUEST_URI'];
147 }
148 }
150 /* If SSL is forced, just forward to the SSL enabled site */
151 if ($config->data['MAIN']['FORCESSL'] == 'true' && $ssl != ''){
152 header ("Location: $ssl");
153 exit;
154 }
156 /* Check for selected password method */
157 $method= $config->current['HASH'];
158 if (isset($_GET['method'])){
159 $method= validate($_GET['method']);
160 $tmp = new passwordMethod($config);
161 $available = $tmp->get_available_methods();
162 if (!isset($available[$method])){
163 msg_dialog::display(_("Password method"),_("Error: Password method not available!"),FATAL_ERROR_DIALOG);
164 exit;
165 }
166 }
169 /* Check for selected user... */
170 if (isset($_GET['uid']) && $_GET['uid'] != ""){
171 $uid= validate($_GET['uid']);
172 $smarty->assign('display_username', false);
173 } elseif (isset($_POST['uid'])){
174 $uid= validate($_POST['uid']);
175 $smarty->assign('display_username', true);
176 } else {
177 $uid= "";
178 $smarty->assign('display_username', true);
179 }
180 $current_password= "";
181 $smarty->assign("changed", false);
183 /* Got a formular answer, validate and try to log in */
184 if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['apply'])){
186 /* Destroy old sessions, they cause a successfull login to relog again ...*/
187 if(session::is_set('_LAST_PAGE_REQUEST')){
188 session::set('_LAST_PAGE_REQUEST',time());
189 }
191 $message= array();
192 $current_password= $_POST['current_password'];
194 /* Do new and repeated password fields match? */
195 $new_password= $_POST['new_password'];
196 if ($_POST['new_password'] != $_POST['new_password_repeated']){
197 $message[]= _("The passwords you've entered as 'New password' and 'Repeated new password' do not match.");
198 } else {
199 if ($_POST['new_password'] == ""){
200 $message[]= msgPool::required(_("New password"));
201 }
202 }
204 /* Password policy fulfilled? */
205 if (isset($config->data['MAIN']['PWDIFFER'])){
206 $l= $config->data['MAIN']['PWDIFFER'];
207 if (substr($_POST['current_password'], 0, $l) == substr($_POST['new_password'], 0, $l)){
208 $message[]= _("The password used as new and current are too similar.");
209 }
210 }
211 if (isset($config->data['MAIN']['PWMINLEN'])){
212 if (strlen($_POST['new_password']) < $config->data['MAIN']['PWMINLEN']){
213 $message[]= _("The password used as new is to short.");
214 }
215 }
217 /* Validate */
218 if (!tests::is_uid($uid)){
219 $message[]= msgPool::invalid(_("Login"));
220 } elseif (mb_strlen($_POST["current_password"], 'UTF-8') == 0){
221 $message[]= msgPool::required(_("Current password"));
222 } else {
224 /* Do we have the selected user somewhere? */
225 $ui= ldap_login_user ($uid, $current_password);
227 if ($ui === NULL){
228 $message[]= _("Please check the username/password combination.");
229 } else {
230 $acls = $ui->get_permissions($ui->dn,"users/password");
231 if(!preg_match("/w/i",$acls)){
232 $message[]= _("You have no permissions to change your password.");
233 }
234 }
235 }
237 /* Do we need to show error messages? */
238 if (count ($message) != 0){
239 /* Show error message and continue editing */
240 msg_dialog::displayChecks($message);
241 } else {
243 /* Passed quality check, just try to change the password now */
244 $output= "";
245 if (isset($config->data['MAIN']['EXTERNALPWDHOOK'])){
246 exec($config->data['MAIN']['EXTERNALPWDHOOK']." ".$ui->username." ".
247 $_POST['current_password']." ".$_POST['new_password'], $resarr);
248 if(count($resarr) > 0) {
249 $output= join('\n', $resarr);
250 }
251 }
252 if ($output != ""){
253 $message[]= _("External password changer reported a problem: ".$output);
254 msg_dialog::displayChecks($message);
255 } else {
256 if ($method != ""){
257 change_password ($ui->dn, $_POST['new_password'], 0, $method);
258 } else {
259 change_password ($ui->dn, $_POST['new_password']);
260 }
261 gosa_log ("User/password has been changed");
262 $smarty->assign("changed", true);
263 }
264 }
267 }
269 /* Parameter fill up */
270 $params= "";
271 foreach (array('uid', 'method', 'directory') as $index){
272 $params.= "&$index=".urlencode($$index);
273 }
274 $params= preg_replace('/^&/', '?', $params);
275 $smarty->assign('params', $params);
277 /* Fill template with required values */
278 $smarty->assign ('date', gmdate("D, d M Y H:i:s"));
279 $smarty->assign ('uid', $uid);
280 $smarty->assign ('password_img', get_template_path('images/password.png'));
282 /* Displasy SSL mode warning? */
283 if ($ssl != "" && $config->data['MAIN']['WARNSSL'] == 'true'){
284 $smarty->assign ("ssl", "<b>"._("Warning").":</b> "._("Session will not be encrypted.")." <a style=\"color:red;\" href=\"".htmlentities($ssl)."\"><b>"._("Enter SSL session")."</b></a>!");
285 } else {
286 $smarty->assign ("ssl", "");
287 }
289 /* show login screen */
290 $smarty->assign("JS",session::get('js'));
291 $smarty->assign ("PHPSESSID", session_id());
292 if (session::is_set('errors')){
293 $smarty->assign("errors", session::get('errors'));;
294 }
295 if ($error_collector != ""){
296 $smarty->assign("php_errors", $error_collector."</div>");
297 } else {
298 $smarty->assign("php_errors", "");
299 }
301 displayPWchanger();
303 ?>
305 </body>
306 </html>
307 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler: