6389fc4127e4456cc01e37fe7dd0dcd7618ac492
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 /* Load required includes */
24 require_once "../include/php_setup.inc";
25 require_once "functions.inc";
26 require_once "class_log.inc";
27 header("Content-type: text/html; charset=UTF-8");
30 /**
31 * Display the login page and exit().
32 *
33 */
34 function displayLogin()
35 {
36 global $smarty,$message,$config,$ssl,$error_collector, $BASE_DIR;
37 error_reporting(E_ALL | E_STRICT);
39 /* Check theme compatibility */
40 $theme= $config->get_cfg_value('theme', 'default');
41 if (file_exists("$BASE_DIR/ihtml/themes/$theme/blacklist")) {
42 $blocks= file("$BASE_DIR/ihtml/themes/$theme/blacklist");
43 foreach ($blocks as $block) {
44 if (preg_match('/'.preg_quote($block).'/', $_SERVER['HTTP_USER_AGENT'])) {
45 die(sprintf(_("Your browser (%s) is blacklisted for the current theme!"), $block));
46 }
47 }
48 }
50 /* Fill template with required values */
51 $username = "";
52 if(isset($_POST["username"])) {
53 $username= $_POST["username"];
54 }
56 $smarty->assign ("title","GOsa");
57 $smarty->assign("logo", image(get_template_path("images/logo.png")));
58 $smarty->assign('date', gmdate("D, d M Y H:i:s"));
59 $smarty->assign('username', $username);
60 $smarty->assign('personal_img', get_template_path('images/login-head.png'));
61 $smarty->assign('password_img', get_template_path('images/password.png'));
62 $smarty->assign('directory_img', get_template_path('images/ldapserver.png'));
64 /* Some error to display? */
65 if (!isset($message)) {
66 $message= "";
67 }
68 $smarty->assign("message", $message);
70 /* Displasy SSL mode warning? */
71 if ($ssl != "" && $config->get_cfg_value('warnssl') == 'true') {
72 $smarty->assign("ssl", sprintf(_("This session is not ecrypted. Click %s to enter an encrypted session."), "<a href=\"$ssl\">".bold(_("here"))."</a>"));
73 } else {
74 $smarty->assign("ssl", "");
75 }
77 if(!$config->check_session_lifetime()) {
78 $smarty->assign ("lifetime", _("The configured session lifetime will be overridden by php.ini settings!"));
79 } else {
80 $smarty->assign ("lifetime", "");
81 }
83 /* Generate server list */
84 $servers= array();
85 if (isset($_POST['server'])) {
86 $selected= validate($_POST['server']);
87 } else {
88 $selected= $config->data['MAIN']['DEFAULT'];
89 }
90 foreach ($config->data['LOCATIONS'] as $key => $ignored) {
91 $servers[$key]= $key;
92 }
93 $smarty->assign ("server_options", $servers);
94 $smarty->assign ("server_id", $selected);
96 /* show login screen */
97 $smarty->assign ("PHPSESSID", session_id());
98 if (session::is_set('errors')) {
99 $smarty->assign("errors", session::get('errors'));
100 }
101 if ($error_collector != "") {
102 $smarty->assign("php_errors", $error_collector."</div>");
103 } else {
104 $smarty->assign("php_errors", "");
105 }
106 $smarty->assign("msg_dialogs", msg_dialog::get_dialogs());
107 $smarty->assign("iePngWorkaround", $config->get_cfg_value("iePngWorkaround","false" ) == "true");
108 $smarty->assign("usePrototype", "false");
109 $smarty->display (get_template_path('headers.tpl'));
110 $smarty->assign("version",get_gosa_version());
111 $smarty->display(get_template_path('login.tpl'));
112 exit();
113 }
117 /*****************************************************************************
118 * M A I N *
119 *****************************************************************************/
121 /* Set error handler to own one, initialize time calculation
122 and start session. */
123 session::start();
124 session::set('errorsAlreadyPosted',array());
126 /* Destroy old session if exists.
127 Else you will get your old session back, if you not logged out correctly. */
128 if(is_array(session::get_all()) && count(session::get_all())) {
129 session::destroy();
130 session::start();
131 }
133 $username= "";
135 /* Reset errors */
136 session::set('errors',"");
137 session::set('errorsAlreadyPosted',"");
138 session::set('LastError',"");
140 /* Check if we need to run setup */
141 if (!file_exists(CONFIG_DIR."/".CONFIG_FILE)) {
142 header("location:setup.php");
143 exit();
144 }
146 /* Reset errors */
147 session::set('errors',"");
149 /* Check for java script */
150 if(isset($_POST['javascript']) && $_POST['javascript'] == "true") {
151 session::global_set('js',TRUE);
152 }elseif(isset($_POST['javascript'])) {
153 session::global_set('js',FALSE);
154 }
156 /* Check if gosa.conf (.CONFIG_FILE) is accessible */
157 if (!is_readable(CONFIG_DIR."/".CONFIG_FILE)) {
158 msg_dialog::display(_("Configuration error"),sprintf(_("GOsa configuration %s/%s is not readable. Aborted."), CONFIG_DIR,CONFIG_FILE),FATAL_ERROR_DIALOG);
159 exit();
160 }
162 /* Parse configuration file */
163 $config= new config(CONFIG_DIR."/".CONFIG_FILE, $BASE_DIR);
164 session::global_set('DEBUGLEVEL',$config->get_cfg_value('DEBUGLEVEL'));
165 if ($_SERVER["REQUEST_METHOD"] != "POST") {
166 @DEBUG (DEBUG_CONFIG, __LINE__, __FUNCTION__, __FILE__, $config->data, "config");
167 }
169 /* Enable compressed output */
170 if ($config->get_cfg_value("sendCompressedOutput") != "") {
171 ob_start("ob_gzhandler");
172 }
174 /* Set template compile directory */
175 $smarty->compile_dir= $config->get_cfg_value("templateCompileDirectory", '/var/spool/gosa');
176 $smarty->error_unassigned= true;
178 /* Check for compile directory */
179 if (!(is_dir($smarty->compile_dir) && is_writable($smarty->compile_dir))) {
180 msg_dialog::display(_("Smarty error"),sprintf(_("Compile directory %s is not accessible!"),
181 $smarty->compile_dir),FATAL_ERROR_DIALOG);
182 exit();
183 }
185 /* Check for old files in compile directory */
186 clean_smarty_compile_dir($smarty->compile_dir);
188 /* Language setup */
189 $lang= get_browser_language();
190 putenv("LANGUAGE=");
191 putenv("LANG=$lang");
192 setlocale(LC_ALL, $lang);
193 $GLOBALS['t_language']= $lang;
194 $GLOBALS['t_gettext_message_dir'] = $BASE_DIR.'/locale/';
196 /* Set the text domain as 'messages' */
197 $domain = 'messages';
198 bindtextdomain($domain, LOCALE_DIR);
199 textdomain($domain);
200 $smarty->assign ('nextfield', 'username');
202 if ($_SERVER["REQUEST_METHOD"] != "POST") {
203 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, $lang, "Setting language to");
204 }
207 /* Check for SSL connection */
208 $ssl= "";
209 if (!isset($_SERVER['HTTPS']) ||
210 !stristr($_SERVER['HTTPS'], "on")) {
212 if (empty($_SERVER['REQUEST_URI'])) {
213 $ssl= "https://".$_SERVER['HTTP_HOST'].
214 $_SERVER['PATH_INFO'];
215 } else {
216 $ssl= "https://".$_SERVER['HTTP_HOST'].
217 $_SERVER['REQUEST_URI'];
218 }
219 }
221 /* If SSL is forced, just forward to the SSL enabled site */
222 if ($config->get_cfg_value("forcessl") == 'true' && $ssl != '') {
223 header ("Location: $ssl");
224 exit;
225 }
227 /* Do we have htaccess authentification enabled? */
228 $htaccess_authenticated= FALSE;
229 if ($config->get_cfg_value("htaccessAuthentication") == "true" ) {
230 if (!isset($_SERVER['REMOTE_USER'])) {
231 msg_dialog::display(_("Configuration error"), _("Broken HTTP authentication setup!"), FATAL_ERROR_DIALOG);
232 exit;
233 }
235 $tmp= process_htaccess($_SERVER['REMOTE_USER'], isset($_SERVER['KRB5CCNAME']));
236 $username= $tmp['username'];
237 $server= $tmp['server'];
238 if ($username == "") {
239 msg_dialog::display(_("Error"), _("Cannot find a valid user for the current HTTP authentication!"), FATAL_ERROR_DIALOG);
240 exit;
241 }
242 if ($server == "") {
243 msg_dialog::display(_("Error"), _("Cannot find a unique user for the current HTTP authentication!"), FATAL_ERROR_DIALOG);
244 exit;
245 }
247 $htaccess_authenticated= TRUE;
248 }
250 /* Got a formular answer, validate and try to log in */
251 if (($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['login'])) || $htaccess_authenticated) {
253 /* Reset error messages */
254 $message= "";
256 /* Destroy old sessions, they cause a successfull login to relog again ...*/
257 if(session::global_is_set('_LAST_PAGE_REQUEST')) {
258 session::global_set('_LAST_PAGE_REQUEST',time());
259 }
261 if (!$htaccess_authenticated) {
262 $server= validate($_POST["server"]);
263 }
264 $config->set_current($server);
266 /* Admin-logon and verify */
267 $ldap = $config->get_ldap_link();
268 if (is_null($ldap) || (is_int($ldap) && $ldap == 0)) {
269 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
270 displayLogin();
271 exit();
272 }
274 /* Check for schema file presence */
275 if ($config->get_cfg_value("schemaCheck") == "true") {
276 $recursive = ($config->get_cfg_value("ldapFollowReferrals") == "true");
277 $tls = ($config->get_cfg_value("ldapTLS") == "true");
279 if(!count($ldap->get_objectclasses())) {
280 msg_dialog::display(_("LDAP error"), _("Cannot obtain information about the available LDAP schema!"), ERROR_DIALOG);
281 displayLogin();
282 exit() ;
283 }else{
284 $cfg = array();
285 $cfg['admin'] = $config->current['ADMINDN'];
286 $cfg['password'] = $config->current['ADMINPASSWORD'];
287 $cfg['connection']= $config->current['SERVER'];
288 $cfg['tls'] = $tls;
289 $str = check_schema($cfg, $config->get_cfg_value("rfc2307bis") == "true");
290 $checkarr = array();
291 foreach($str as $tr) {
292 if(isset($tr['IS_MUST_HAVE']) && !$tr['STATUS']) {
293 msg_dialog::display(_("LDAP error"), _("Your LDAP setup contains old schema definitions:")."<br><br>".$tr['MSG'], ERROR_DIALOG);
294 displayLogin();
295 exit();
296 }
297 }
298 }
299 }
301 /* Check for locking area */
302 $ldap->cat($config->get_cfg_value("config"), array("dn"));
303 $attrs= $ldap->fetch();
304 if (!count ($attrs)) {
305 $ldap->cd($config->current['BASE']);
306 $ldap->create_missing_trees($config->get_cfg_value("config"));
307 }
309 /* Check for valid input */
310 $ok= true;
311 if (!$htaccess_authenticated) {
312 $username= $_POST["username"];
313 if (!preg_match("/^[@A-Za-z0-9_.-]+$/", $username)) {
314 $message= _("Please specify a valid username!");
315 $ok= false;
316 } elseif (mb_strlen($_POST["password"], 'UTF-8') == 0) {
317 $message= _("Please specify your password!");
318 $smarty->assign ('nextfield', 'password');
319 $ok= false;
320 }
321 }
323 if ($ok) {
325 /* Login as user, initialize user ACL's */
326 if ($htaccess_authenticated) {
327 $ui= ldap_login_user_htaccess($username);
328 if ($ui === NULL || !$ui) {
329 msg_dialog::display(_("Authentication error"), _("Cannot retrieve user information for HTTP authentication!"), FATAL_ERROR_DIALOG);
330 exit;
331 }
332 } else {
333 $ui= ldap_login_user($username, $_POST["password"]);
334 }
335 if ($ui === NULL || !$ui) {
336 $message= _("Please check the username/password combination.");
337 $smarty->assign ('nextfield', 'password');
338 session::global_set('config',$config);
339 new log("security","login","",array(),"Authentication failed for user \"$username\"");
340 } else {
341 /* Remove all locks of this user */
342 del_user_locks($ui->dn);
344 /* Save userinfo and plugin structure */
345 session::global_set('ui',$ui);
346 session::global_set('session_cnt',0);
348 /* Let GOsa trigger a new connection for each POST, save
349 config to session. */
350 $config->get_departments();
351 $config->make_idepartments();
352 session::global_set('config',$config);
354 /* Restore filter settings from cookie, if available */
355 if($config->get_cfg_value("storeFilterSettings") == "true") {
357 if(isset($_COOKIE['GOsa_Filter_Settings']) || isset($HTTP_COOKIE_VARS['GOsa_Filter_Settings'])) {
359 if(isset($_COOKIE['GOsa_Filter_Settings'])) {
360 $cookie_all = unserialize(base64_decode($_COOKIE['GOsa_Filter_Settings']));
361 }else{
362 $cookie_all = unserialize(base64_decode($HTTP_COOKIE_VARS['GOsa_Filter_Settings']));
363 }
364 if(isset($cookie_all[$ui->dn])) {
365 $cookie = $cookie_all[$ui->dn];
366 $cookie_vars= array("MultiDialogFilters","CurrentMainBase","plug");
367 foreach($cookie_vars as $var) {
368 if(isset($cookie[$var])) {
369 session::global_set($var,$cookie[$var]);
370 }
371 }
372 if(isset($cookie['plug'])) {
373 $plug =$cookie['plug'];
374 }
375 }
376 }
377 }
379 /* are we using accountexpiration */
380 if ($config->get_cfg_value("handleExpiredAccounts") == "true") {
381 $expired= ldap_expired_account($config, $ui->dn, $ui->username);
383 if ($expired == 1) {
384 $message= _("Account locked. Please contact your system administrator!");
385 $smarty->assign ('nextfield', 'password');
386 new log("security","login","",array(),"Account for user \"$username\" has expired") ;
387 } elseif ($expired == 3) {
388 $plist= new pluglist($config, $ui);
389 foreach ($plist->dirlist as $key => $value) {
390 if (preg_match("/\bpassword\b/i",$value)) {
391 $plug=$key;
392 new log("security","login","",array(),"User \"$username\" password forced to change") ;
393 header ("Location: main.php?plug=$plug&reset=1");
394 exit;
395 }
396 }
397 }
398 }
400 /* Not account expired or password forced change go to main page */
401 new log("security","login","",array(),"User \"$username\" logged in successfully") ;
402 $plist= new pluglist($config, $ui);
403 if(isset($plug) && isset($plist->dirlist[$plug])) {
404 header ("Location: main.php?plug=".$plug."&global_check=1");
405 }else{
406 header ("Location: main.php?global_check=1");
407 }
408 exit;
409 }
410 }
411 }
413 /* Fill template with required values */
414 $smarty->assign ('date', gmdate("D, d M Y H:i:s"));
415 $smarty->assign ('username', $username);
416 $smarty->assign ('personal_img', get_template_path('images/login-head.png'));
417 $smarty->assign ('password_img', get_template_path('images/password.png'));
418 $smarty->assign ('directory_img', get_template_path('images/ldapserver.png'));
420 /* Some error to display? */
421 if (!isset($message)) {
422 $message= "";
423 }
425 $smarty->assign ("message", $message);
427 /* Translation of cookie-warning. Whether to display it, is determined by JavaScript */
428 $smarty->assign ("cookies", _("Your browser has cookies disabled: please enable cookies and reload this page before logging in!"));
430 /* Generate server list */
431 $servers= array();
432 if (isset($_POST['server'])) {
433 $selected= validate($_POST['server']);
434 } else {
435 $selected= $config->data['MAIN']['DEFAULT'];
436 }
437 foreach ($config->data['LOCATIONS'] as $key => $ignored) {
438 $servers[$key]= $key;
439 }
440 $smarty->assign ("server_options", $servers);
441 $smarty->assign ("server_id", $selected);
443 /* show login screen */
444 $smarty->assign ("PHPSESSID", session_id());
445 if (session::is_set('errors')) {
446 $smarty->assign("errors", session::get('errors'));
447 }
448 if ($error_collector != "") {
449 $smarty->assign("php_errors", preg_replace("/%BUGBODY%/",$error_collector_mailto,$error_collector)."</div>");
450 } else {
451 $smarty->assign("php_errors", "");
452 }
454 /* Set focus to the error button if we've an error message */
455 $focus= "";
456 if (session::is_set('errors') && session::get('errors') != "") {
457 $focus= '<script language="JavaScript" type="text/javascript">';
458 $focus.= 'document.forms[0].error_accept.focus();';
459 $focus.= '</script>';
460 }
461 $smarty->assign("focus", $focus);
462 displayLogin();
464 // vim:tabstop=2:expandtab:shiftwidth=2:softtabstop=2:filetype=php:syntax:ruler:
465 ?>
467 </body>
468 </html>