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->assign ("logo", image(get_template_path("images/logo.png")));
28 $smarty->assign ("date", date("l, dS F Y H:i:s O"));
29 $smarty->display(get_template_path('password.tpl'));
30 exit();
31 }
33 /* Load required includes */
34 require_once "../include/php_setup.inc";
35 require_once "functions.inc";
37 if (!class_exists("log")) {
38 require_once("class_log.inc");
39 }
41 header("Content-type: text/html; charset=UTF-8");
43 session::start();
45 /* Destroy old session if exists.
46 Else you will get your old session back, if you not logged out correctly. */
47 if (is_array(session::get_all()) && count(session::get_all())) {
48 session::destroy();
49 session::start();
50 }
52 /* Reset errors */
53 session::global_set('js', true);
54 session::set('errors', "");
55 session::set('errorsAlreadyPosted', array());
56 session::set('LastError', "");
58 /* Check if CONFIG_FILE is accessible */
59 if (!is_readable(CONFIG_DIR."/".CONFIG_FILE)) {
60 msg_dialog::display(
61 _("Fatal error"),
62 sprintf(
63 _("GOsa configuration %s/%s is not readable. Aborted."),
64 CONFIG_DIR, CONFIG_FILE
65 ),
66 FATAL_ERROR_DIALOG
67 );
68 exit;
69 }
71 /* Parse configuration file */
72 $config= new config(CONFIG_DIR."/".CONFIG_FILE, $BASE_DIR);
74 /* Generate server list */
75 $servers= array();
76 foreach ($config->data['LOCATIONS'] as $key => $ignored) {
77 $servers[$key]= $key;
78 }
80 if (isset($_POST['server'])) {
81 $directory= get_post('server');
82 }elseif (isset($_GET['directory'])) {
83 $directory= $_GET['directory'];
84 } else {
85 $directory= $config->data['MAIN']['DEFAULT'];
86 if (!isset($servers[$directory])) {
87 $directory = key($servers);
88 }
90 }
92 // Set location and reload the configRegistry - we've now access to the ldap.
93 if(isset($servers[$directory])){
94 $config->set_current($directory);
95 $config->check_and_reload();
96 $config->configRegistry->reload(TRUE);
97 }
98 session::global_set('plist', new pluglist($config, $ui));
100 session::global_set('debugLevel', $config->get_cfg_value("core","debugLevel"));
101 if ($_SERVER["REQUEST_METHOD"] != "POST") {
102 @DEBUG(
103 DEBUG_CONFIG, __LINE__, __FUNCTION__, __FILE__, $config->data, "config"
104 );
105 }
107 /* Set template compile directory */
108 $smarty->compile_dir= $config->get_cfg_value("core", "templateCompileDirectory");
110 /* Check for compile directory */
111 if (!(is_dir($smarty->compile_dir) && is_writable($smarty->compile_dir))) {
112 msg_dialog::display(
113 _("Configuration error"),
114 sprintf(
115 _("Compile directory %s is not accessible!"),
116 bold($smarty->compile_dir)
117 ),
118 FATAL_ERROR_DIALOG
119 );
120 exit;
121 }
123 /* Check for old files in compile directory */
124 clean_smarty_compile_dir($smarty->compile_dir);
126 /* Language setup */
127 if ($config->get_cfg_value("core","language") == "") {
128 $lang= get_browser_language();
129 } else {
130 $lang= $config->get_cfg_value("core","language");
131 }
132 $lang.=".UTF-8";
133 putenv("LANGUAGE=");
134 putenv("LANG=$lang");
135 setlocale(LC_ALL, $lang);
136 $GLOBALS['t_language']= $lang;
137 $GLOBALS['t_gettext_message_dir'] = $BASE_DIR.'/locale/';
139 /* Set the text domain as 'messages' */
140 $domain = 'messages';
141 bindtextdomain($domain, LOCALE_DIR);
142 textdomain($domain);
144 $smarty->assign ("title","GOsa");
145 if (isset($_GET['directory']) && isset($servers[$_GET['directory']])) {
146 $smarty->assign("show_directory_chooser", false);
147 $directory= validate($_GET['directory']);
148 } else {
149 $smarty->assign("server_options", $servers);
150 $smarty->assign("server_id", $directory);
151 $smarty->assign("show_directory_chooser", true);
152 }
154 /* Set config to selected one */
155 $config->set_current($directory);
156 session::global_set('config', $config);
158 if ($_SERVER["REQUEST_METHOD"] != "POST") {
159 @DEBUG(
160 DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,
161 $lang, "Setting language to"
162 );
163 }
166 /* Check for SSL connection */
167 $ssl= "";
168 if (!isset($_SERVER['HTTPS']) ||
169 !stristr($_SERVER['HTTPS'], "on")) {
171 if (empty($_SERVER['REQUEST_URI'])) {
172 $ssl= "https://".$_SERVER['HTTP_HOST'].
173 $_SERVER['PATH_INFO'];
174 } else {
175 $ssl= "https://".$_SERVER['HTTP_HOST'].
176 $_SERVER['REQUEST_URI'];
177 }
178 }
180 /* If SSL is forced, just forward to the SSL enabled site */
181 if ($config->get_cfg_value("core","forceSSL") == 'true' && $ssl != '') {
182 header("Location: $ssl");
183 exit;
184 }
186 /* Check for selected password method */
187 $method= $config->get_cfg_value("core","passwordDefaultHash");
188 if (isset($_GET['method'])) {
189 $method= validate($_GET['method']);
190 $tmp = new passwordMethod($config, "dummy");
191 $available = $tmp->get_available_methods();
192 if (!isset($available[$method])) {
193 msg_dialog::display(
194 _("Password method"),
195 _("Error: Password method not available!"),
196 FATAL_ERROR_DIALOG
197 );
198 exit;
199 }
200 }
203 /* Check for selected user... */
204 if (isset($_GET['uid']) && $_GET['uid'] != "") {
205 $uid= validate($_GET['uid']);
206 $smarty->assign('display_username', false);
207 } elseif (isset($_POST['uid'])) {
208 $uid= get_post('uid');
209 $smarty->assign('display_username', true);
210 } else {
211 $uid= "";
212 $smarty->assign('display_username', true);
213 }
214 $current_password= "";
215 $smarty->assign("changed", false);
217 /* Got a formular answer, validate and try to log in */
218 if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['apply'])) {
220 /* Destroy old sessions, they cause a successfull login to relog again ...*/
221 if (session::global_is_set('_LAST_PAGE_REQUEST')) {
222 session::global_set('_LAST_PAGE_REQUEST', time());
223 }
225 $message= array();
226 $current_password= $_POST['current_password'];
228 /* Do new and repeated password fields match? */
229 $new_password= $_POST['new_password'];
230 if ($_POST['new_password'] != $_POST['new_password_repeated']) {
231 $message[]= _("The values for 'New password' and 'Repeated new password' differ!");
232 } else {
233 if ($_POST['new_password'] == "") {
234 $message[]= msgPool::required(_("New password"));
235 }
236 }
238 /* Password policy fulfilled? */
239 if ($config->get_cfg_value("core","passwordMinDiffer") != "") {
240 $l= $config->get_cfg_value("core","passwordMinDiffer");
241 if (substr($_POST['current_password'], 0, $l) ==
242 substr($_POST['new_password'], 0, $l)) {
243 $message[]= _("The password used as new and current are too similar!");
244 }
245 }
246 if ($config->get_cfg_value("core","passwordMinLength") != "") {
247 if (strlen($_POST['new_password']) <
248 $config->get_cfg_value("core","passwordMinLength")) {
249 $message[]= _("The password used as new is to short!");
250 }
251 }
252 if(!passwordMethod::is_harmless($_POST['new_password'])){
253 $message[]= _("The password contains possibly problematic Unicode characters!");
254 }
256 /* Validate */
257 if (!tests::is_uid($uid)) {
258 $message[]= msgPool::invalid(_("Login"));
259 } elseif (mb_strlen($_POST["current_password"], 'UTF-8') == 0) {
260 $message[]= msgPool::required(_("Current password"));
261 } else {
263 /* Do we have the selected user somewhere? */
264 $ui= ldap_login_user($uid, $current_password);
266 /* Load ocMapping into userinfo */
267 $tmp= new acl($config, NULL, $ui->dn);
268 $ui->ocMapping= $tmp->ocMapping;
271 $ui->loadACL();
273 if ($ui === NULL) {
274 $message[]= _("Please check the username/password combination!");
275 } else {
276 $acls = $ui->get_permissions($ui->dn, "users/password");
277 if (!preg_match("/w/i", $acls)) {
278 $message[]= _("You have no permissions to change your password!");
279 }
280 }
281 }
283 /* Do we need to show error messages? */
284 if (count($message) != 0) {
285 /* Show error message and continue editing */
286 msg_dialog::displayChecks($message);
287 } else {
289 /* Passed quality check, just try to change the password now */
290 $output= "";
291 if ($config->get_cfg_value("core","passwordHook") != "") {
292 exec(
293 $config->get_cfg_value("core","passwordHook")." ".$ui->username." ".
294 $_POST['current_password']." ".$_POST['new_password'],
295 $resarr
296 );
297 if (count($resarr) > 0) {
298 $output= join('\n', $resarr);
299 }
300 }
301 if ($output != "") {
302 $message[]= sprintf(
303 _("External password changer reported a problem: %s"),
304 $output
305 );
306 msg_dialog::displayChecks($message);
307 } else {
308 if ($method != "") {
309 change_password($ui->dn, $_POST['new_password'], 0, $method);
310 } else {
311 change_password($ui->dn, $_POST['new_password']);
312 }
313 gosa_log("User/password has been changed");
314 $smarty->assign("changed", true);
315 }
316 }
319 }
321 /* Parameter fill up */
322 $params= "";
323 foreach (array('uid', 'method', 'directory') as $index) {
324 $params.= "&$index=".urlencode($$index);
325 }
326 $params= preg_replace('/^&/', '?', $params);
327 $smarty->assign('params', $params);
329 /* Fill template with required values */
330 $smarty->assign('date', gmdate("D, d M Y H:i:s"));
331 $smarty->assign('uid', $uid);
332 $smarty->assign('password_img', get_template_path('images/password.png'));
334 /* Displasy SSL mode warning? */
335 if ($ssl != "" && $config->get_cfg_value("core","warnSSL") == 'true') {
336 $smarty->assign(
337 "ssl",
338 "<b>"._("Warning").":</b> "._("Session will not be encrypted.").
339 " <a style=\"color:red;\" href=\"".htmlentities($ssl)."\"><b>".
340 _("Enter SSL session")."</b></a>!"
341 );
342 } else {
343 $smarty->assign("ssl", "");
344 }
346 /* show login screen */
347 $smarty->assign("JS", session::global_get('js'));
348 $smarty->assign("PHPSESSID", session_id());
349 if (session::is_set('errors')) {
350 $smarty->assign("errors", session::get('errors'));;
351 }
352 if ($error_collector != "") {
353 $smarty->assign("php_errors", $error_collector."</div>");
354 } else {
355 $smarty->assign("php_errors", "");
356 }
358 $smarty->assign("msg_dialogs", msg_dialog::get_dialogs());
359 displayPWchanger();
361 ?>
363 </body>
364 </html>
365 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler: