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::global_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(
59 _("Fatal error"),
60 sprintf(
61 _("GOsa configuration %s/%s is not readable. Aborted."),
62 CONFIG_DIR, CONFIG_FILE
63 ),
64 FATAL_ERROR_DIALOG
65 );
66 exit;
67 }
69 /* Parse configuration file */
70 $config= new config(CONFIG_DIR."/".CONFIG_FILE, $BASE_DIR);
71 session::global_set('DEBUGLEVEL', $config->get_cfg_value("debuglevel"));
72 if ($_SERVER["REQUEST_METHOD"] != "POST") {
73 @DEBUG(
74 DEBUG_CONFIG, __LINE__, __FUNCTION__, __FILE__, $config->data, "config"
75 );
76 }
78 /* Set template compile directory */
79 $smarty->compile_dir= $config->get_cfg_value(
80 "templateCompileDirectory", '/var/spool/gosa'
81 );
83 /* Check for compile directory */
84 if (!(is_dir($smarty->compile_dir) && is_writable($smarty->compile_dir))) {
85 msg_dialog::display(
86 _("Configuration error"),
87 sprintf(
88 _("Directory '%s' specified as compile directory is not accessible!"),
89 $smarty->compile_dir
90 ),
91 FATAL_ERROR_DIALOG
92 );
93 exit;
94 }
96 /* Check for old files in compile directory */
97 clean_smarty_compile_dir($smarty->compile_dir);
99 /* Language setup */
100 if ($config->get_cfg_value("language") == "") {
101 $lang= get_browser_language();
102 } else {
103 $lang= $config->get_cfg_value("language");
104 }
105 $lang.=".UTF-8";
106 putenv("LANGUAGE=");
107 putenv("LANG=$lang");
108 setlocale(LC_ALL, $lang);
109 $GLOBALS['t_language']= $lang;
110 $GLOBALS['t_gettext_message_dir'] = $BASE_DIR.'/locale/';
112 /* Set the text domain as 'messages' */
113 $domain = 'messages';
114 bindtextdomain($domain, LOCALE_DIR);
115 textdomain($domain);
117 /* Generate server list */
118 $servers= array();
119 foreach ($config->data['LOCATIONS'] as $key => $ignored) {
120 $servers[$key]= $key;
121 }
122 if (isset($_POST['server'])) {
123 $directory= validate($_POST['server']);
124 } else {
125 $directory= $config->data['MAIN']['DEFAULT'];
127 if (!isset($servers[$directory])) {
128 $directory = key($servers);
129 }
130 }
131 if (isset($_GET['directory']) && isset($servers[$_GET['directory']])) {
132 $smarty->assign("show_directory_chooser", false);
133 $directory= validate($_GET['directory']);
134 } else {
135 $smarty->assign("server_options", $servers);
136 $smarty->assign("server_id", $directory);
137 $smarty->assign("show_directory_chooser", true);
138 }
140 /* Set config to selected one */
141 $config->set_current($directory);
142 session::global_set('config', $config);
144 if ($_SERVER["REQUEST_METHOD"] != "POST") {
145 @DEBUG(
146 DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
147 $lang, "Setting language to"
148 );
149 }
152 /* Check for SSL connection */
153 $ssl= "";
154 if (!isset($_SERVER['HTTPS']) ||
155 !stristr($_SERVER['HTTPS'], "on")) {
157 if (empty($_SERVER['REQUEST_URI'])) {
158 $ssl= "https://".$_SERVER['HTTP_HOST'].
159 $_SERVER['PATH_INFO'];
160 } else {
161 $ssl= "https://".$_SERVER['HTTP_HOST'].
162 $_SERVER['REQUEST_URI'];
163 }
164 }
166 /* If SSL is forced, just forward to the SSL enabled site */
167 if ($config->get_cfg_value("forcessl") == 'true' && $ssl != '') {
168 header("Location: $ssl");
169 exit;
170 }
172 /* Check for selected password method */
173 $method= $config->get_cfg_value("hash", "crypt/md5");
174 if (isset($_GET['method'])) {
175 $method= validate($_GET['method']);
176 $tmp = new passwordMethod($config);
177 $available = $tmp->get_available_methods();
178 if (!isset($available[$method])) {
179 msg_dialog::display(
180 _("Password method"),
181 _("Error: Password method not available!"),
182 FATAL_ERROR_DIALOG
183 );
184 exit;
185 }
186 }
189 /* Check for selected user... */
190 if (isset($_GET['uid']) && $_GET['uid'] != "") {
191 $uid= validate($_GET['uid']);
192 $smarty->assign('display_username', false);
193 } elseif (isset($_POST['uid'])) {
194 $uid= validate($_POST['uid']);
195 $smarty->assign('display_username', true);
196 } else {
197 $uid= "";
198 $smarty->assign('display_username', true);
199 }
200 $current_password= "";
201 $smarty->assign("changed", false);
203 /* Got a formular answer, validate and try to log in */
204 if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['apply'])) {
206 /* Destroy old sessions, they cause a successfull login to relog again ...*/
207 if (session::global_is_set('_LAST_PAGE_REQUEST')) {
208 session::global_set('_LAST_PAGE_REQUEST', time());
209 }
211 $message= array();
212 $current_password= $_POST['current_password'];
214 /* Do new and repeated password fields match? */
215 $new_password= $_POST['new_password'];
216 if ($_POST['new_password'] != $_POST['new_password_repeated']) {
217 $message[]= _("The passwords you've entered as 'New password' and 'Repeated new password' do not match.");
218 } else {
219 if ($_POST['new_password'] == "") {
220 $message[]= msgPool::required(_("New password"));
221 }
222 }
224 /* Password policy fulfilled? */
225 if ($config->get_cfg_value("passwordMinDiffer") != "") {
226 $l= $config->get_cfg_value("passwordMinDiffer");
227 if (substr($_POST['current_password'], 0, $l) ==
228 substr($_POST['new_password'], 0, $l)) {
229 $message[]= _("The password used as new and current are too similar.");
230 }
231 }
232 if ($config->get_cfg_value("passwordMinLength") != "") {
233 if (strlen($_POST['new_password']) <
234 $config->get_cfg_value("passwordMinLength")) {
235 $message[]= _("The password used as new is to short.");
236 }
237 }
239 /* Validate */
240 if (!tests::is_uid($uid)) {
241 $message[]= msgPool::invalid(_("Login"));
242 } elseif (mb_strlen($_POST["current_password"], 'UTF-8') == 0) {
243 $message[]= msgPool::required(_("Current password"));
244 } else {
246 /* Do we have the selected user somewhere? */
247 $ui= ldap_login_user($uid, $current_password);
249 if ($ui === NULL) {
250 $message[]= _("Please check the username/password combination.");
251 } else {
252 $acls = $ui->get_permissions($ui->dn, "users/password");
253 if (!preg_match("/w/i", $acls)) {
254 $message[]= _("You have no permissions to change your password.");
255 }
256 }
257 }
259 /* Do we need to show error messages? */
260 if (count($message) != 0) {
261 /* Show error message and continue editing */
262 msg_dialog::displayChecks($message);
263 } else {
265 /* Passed quality check, just try to change the password now */
266 $output= "";
267 if ($config->get_cfg_value("passwordHook") != "") {
268 exec(
269 $config->get_cfg_value("passwordHook")." ".$ui->username." ".
270 $_POST['current_password']." ".$_POST['new_password'],
271 $resarr
272 );
273 if (count($resarr) > 0) {
274 $output= join('\n', $resarr);
275 }
276 }
277 if ($output != "") {
278 $message[]= sprintf(
279 _("External password changer reported a problem: %s"),
280 $output
281 );
282 msg_dialog::displayChecks($message);
283 } else {
284 if ($method != "") {
285 change_password($ui->dn, $_POST['new_password'], 0, $method);
286 } else {
287 change_password($ui->dn, $_POST['new_password']);
288 }
289 gosa_log("User/password has been changed");
290 $smarty->assign("changed", true);
291 }
292 }
295 }
297 /* Parameter fill up */
298 $params= "";
299 foreach (array('uid', 'method', 'directory') as $index) {
300 $params.= "&$index=".urlencode($$index);
301 }
302 $params= preg_replace('/^&/', '?', $params);
303 $smarty->assign('params', $params);
305 /* Fill template with required values */
306 $smarty->assign('date', gmdate("D, d M Y H:i:s"));
307 $smarty->assign('uid', $uid);
308 $smarty->assign('password_img', get_template_path('images/password.png'));
310 /* Displasy SSL mode warning? */
311 if ($ssl != "" && $config->get_cfg_value("warnssl") == 'true') {
312 $smarty->assign(
313 "ssl",
314 "<b>"._("Warning").":</b> "._("Session will not be encrypted.").
315 " <a style=\"color:red;\" href=\"".htmlentities($ssl)."\"><b>".
316 _("Enter SSL session")."</b></a>!"
317 );
318 } else {
319 $smarty->assign("ssl", "");
320 }
322 /* show login screen */
323 $smarty->assign("JS", session::global_get('js'));
324 $smarty->assign("PHPSESSID", session_id());
325 if (session::is_set('errors')) {
326 $smarty->assign("errors", session::get('errors'));;
327 }
328 if ($error_collector != "") {
329 $smarty->assign("php_errors", $error_collector."</div>");
330 } else {
331 $smarty->assign("php_errors", "");
332 }
334 $smarty->assign("msg_dialogs", msg_dialog::get_dialogs());
335 displayPWchanger();
337 ?>
339 </body>
340 </html>
341 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler: