Code

Updated check for boolean config values
[gosa.git] / gosa-core / html / index.php
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("core",'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     }
55     
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("core",'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());
108     $smarty->display (get_template_path('headers.tpl'));
109     $smarty->assign("version",get_gosa_version());
110     $smarty->display(get_template_path('login.tpl'));
111     exit();
116 /*****************************************************************************
117  *                               M   A   I   N                               *
118  *****************************************************************************/
120 /* Set error handler to own one, initialize time calculation
121 and start session. */
122 session::start();
123 session::set('errorsAlreadyPosted',array());
125 /* Destroy old session if exists. 
126 Else you will get your old session back, if you not logged out correctly. */
127 if(is_array(session::get_all()) && count(session::get_all())) {
128     session::destroy();
129     session::start();
132 $username= "";
134 /* Reset errors */
135 session::set('errors',"");
136 session::set('errorsAlreadyPosted',"");
137 session::set('LastError',"");
139 /* Check if we need to run setup */
140 if (!file_exists(CONFIG_DIR."/".CONFIG_FILE)) {
141     header("location:setup.php");
142     exit();
145 /* Reset errors */
146 session::set('errors',"");
148 /* Check for java script */
149 if(isset($_POST['javascript']) && $_POST['javascript'] == "true") {
150     session::global_set('js',TRUE);
151 }elseif(isset($_POST['javascript'])) {
152     session::global_set('js',FALSE);
155 /* Check if gosa.conf (.CONFIG_FILE) is accessible */
156 if (!is_readable(CONFIG_DIR."/".CONFIG_FILE)) {
157     msg_dialog::display(_("Configuration error"),sprintf(_("GOsa configuration %s/%s is not readable. Aborted."), CONFIG_DIR,CONFIG_FILE),FATAL_ERROR_DIALOG);
158     exit();
161 /* Parse configuration file */
162 $config= new config(CONFIG_DIR."/".CONFIG_FILE, $BASE_DIR);
163 session::global_set('debugLevel',$config->get_cfg_value("core",'debugLevel'));
164 if ($_SERVER["REQUEST_METHOD"] != "POST") {
165     @DEBUG (DEBUG_CONFIG, __LINE__, __FUNCTION__, __FILE__, $config->data, "config");
168 /* Enable compressed output */
169 if ($config->get_cfg_value("core","sendCompressedOutput") != "") {
170     ob_start("ob_gzhandler");
173 /* Set template compile directory */
174 $smarty->compile_dir= $config->get_cfg_value("core","templateCompileDirectory", '/var/spool/gosa');
175 $smarty->error_unassigned= true;
177 /* Check for compile directory */
178 if (!(is_dir($smarty->compile_dir) && is_writable($smarty->compile_dir))) {
179     msg_dialog::display(_("Smarty error"),sprintf(_("Compile directory %s is not accessible!"),
180         $smarty->compile_dir),FATAL_ERROR_DIALOG);
181     exit();
184 /* Check for old files in compile directory */
185 clean_smarty_compile_dir($smarty->compile_dir);
187 /* Language setup */
188 $lang= get_browser_language();
189 putenv("LANGUAGE=");
190 putenv("LANG=$lang");
191 setlocale(LC_ALL, $lang);
192 $GLOBALS['t_language']= $lang;
193 $GLOBALS['t_gettext_message_dir'] = $BASE_DIR.'/locale/';
195 /* Set the text domain as 'messages' */
196 $domain = 'messages';
197 bindtextdomain($domain, LOCALE_DIR);
198 textdomain($domain);
199 $smarty->assign ('nextfield', 'username');
201 if ($_SERVER["REQUEST_METHOD"] != "POST") {
202     @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, $lang, "Setting language to");
206 /* Check for SSL connection */
207 $ssl= "";
208 if (!isset($_SERVER['HTTPS']) ||
209     !stristr($_SERVER['HTTPS'], "on")) {
211         if (empty($_SERVER['REQUEST_URI'])) {
212             $ssl= "https://".$_SERVER['HTTP_HOST'].
213                 $_SERVER['PATH_INFO'];
214         } else {
215             $ssl= "https://".$_SERVER['HTTP_HOST'].
216                 $_SERVER['REQUEST_URI'];
217         }
218     }
220 /* If SSL is forced, just forward to the SSL enabled site */
221 if ($config->get_cfg_value("core","forceSSL") == 'true' && $ssl != '') {
222     header ("Location: $ssl");
223     exit;
226 /* Do we have htaccess authentification enabled? */
227 $htaccess_authenticated= FALSE;
228 if ($config->get_cfg_value("core","htaccessAuthentication") == "true" ) {
229     if (!isset($_SERVER['REMOTE_USER'])) {
230         msg_dialog::display(_("Configuration error"), _("Broken HTTP authentication setup!"), FATAL_ERROR_DIALOG);
231         exit;
232     }
234     $tmp= process_htaccess($_SERVER['REMOTE_USER'], isset($_SERVER['KRB5CCNAME']));
235     $username= $tmp['username'];
236     $server= $tmp['server'];
237     if ($username == "") {
238         msg_dialog::display(_("Error"), _("Cannot find a valid user for the current HTTP authentication!"), FATAL_ERROR_DIALOG);
239         exit;
240     }
241     if ($server == "") {
242         msg_dialog::display(_("Error"), _("Cannot find a unique user for the current HTTP authentication!"), FATAL_ERROR_DIALOG);
243         exit;
244     }
246     $htaccess_authenticated= TRUE;
249 /* Got a formular answer, validate and try to log in */
250 if (($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['login'])) || $htaccess_authenticated) {
252     /* Reset error messages */
253     $message= "";
255     /* Destroy old sessions, they cause a successfull login to relog again ...*/
256     if(session::global_is_set('_LAST_PAGE_REQUEST')) {
257         session::global_set('_LAST_PAGE_REQUEST',time());
258     }
260     if (!$htaccess_authenticated) {
261         $server= validate($_POST["server"]);
262     }
263     $config->set_current($server);
265     /* Admin-logon and verify */
266     $ldap = $config->get_ldap_link();
267     if (is_null($ldap) || (is_int($ldap) && $ldap == 0)) {
268         msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
269         displayLogin();
270         exit();
271     }
273     /* Check for schema file presence */
274     if ($config->get_cfg_value("core","schemaCheck") == "true") {
275         $recursive = ($config->get_cfg_value("core","ldapFollowReferrals") == "true");
276         $tls =       ($config->get_cfg_value("core","ldapTLS") == "true");
278         if(!count($ldap->get_objectclasses())) {
279             msg_dialog::display(_("LDAP error"), _("Cannot obtain information about the available LDAP schema!"), ERROR_DIALOG);
280             displayLogin();
281             exit()  ;
282         }else{
283             $cfg = array();
284             $cfg['admin']     = $config->current['ADMINDN'];
285             $cfg['password']  = $config->current['ADMINPASSWORD'];
286             $cfg['connection']= $config->current['SERVER'];
287             $cfg['tls']       = $tls;
288             $str = check_schema($cfg, $config->get_cfg_value("core","rfc2307bis") == "true");
289             $checkarr = array();
290             foreach($str as $tr) {
291                 if(isset($tr['IS_MUST_HAVE']) && !$tr['STATUS']) {
292                     msg_dialog::display(_("LDAP error"), _("Your LDAP setup contains old schema definitions:")."<br><br>".$tr['MSG'], ERROR_DIALOG);
293                     displayLogin();
294                     exit();
295                 }
296             }
297         }
298     }
300     /* Check for locking area */
301     $ldap->cat($config->get_cfg_value("core","config"), array("dn"));
302     $attrs= $ldap->fetch();
303     if (!count ($attrs)) {
304         $ldap->cd($config->current['BASE']);
305         $ldap->create_missing_trees($config->get_cfg_value("core","config"));
306     }
308     /* Check for valid input */
309     $ok= true;
310     if (!$htaccess_authenticated) {
311         $username= $_POST["username"];
312         if (!preg_match("/^[@A-Za-z0-9_.-]+$/", $username)) {
313             $message= _("Please specify a valid username!");
314             $ok= false;
315         } elseif (mb_strlen($_POST["password"], 'UTF-8') == 0) {
316             $message= _("Please specify your password!");
317             $smarty->assign ('nextfield', 'password');
318             $ok= false;
319         }
320     }
322     if ($ok) {
324         /* Login as user, initialize user ACL's */
325         if ($htaccess_authenticated) {
326             $ui= ldap_login_user_htaccess($username);
327             if ($ui === NULL || !$ui) {
328                 msg_dialog::display(_("Authentication error"), _("Cannot retrieve user information for HTTP authentication!"), FATAL_ERROR_DIALOG);
329                 exit;
330             }
331         } else {
332             $ui= ldap_login_user($username, $_POST["password"]);
333         }
334         if ($ui === NULL || !$ui) {
335             $message= _("Please check the username/password combination!");
336             $smarty->assign ('nextfield', 'password');
337             session::global_set('config',$config);
338             new log("security","login","",array(),"Authentication failed for user \"$username\"");
339         } else {
340             /* Remove all locks of this user */
341             del_user_locks($ui->dn);
343             /* Save userinfo and plugin structure */
344             session::global_set('ui',$ui);
345             session::global_set('session_cnt',0);
347       /* Let GOsa trigger a new connection for each POST, save
348       config to session. */
349             $config->get_departments();
350             $config->make_idepartments();
351             session::global_set('config',$config);
353             /* Restore filter settings from cookie, if available */
354             if($config->get_cfg_value("core","storeFilterSettings") == "true") {
356                 if(isset($_COOKIE['GOsa_Filter_Settings']) || isset($HTTP_COOKIE_VARS['GOsa_Filter_Settings'])) {
358                     if(isset($_COOKIE['GOsa_Filter_Settings'])) {
359                         $cookie_all = unserialize(base64_decode($_COOKIE['GOsa_Filter_Settings']));
360                     }else{
361                         $cookie_all = unserialize(base64_decode($HTTP_COOKIE_VARS['GOsa_Filter_Settings']));
362                     }
363                     if(isset($cookie_all[$ui->dn])) {
364                         $cookie = $cookie_all[$ui->dn];
365                         $cookie_vars= array("MultiDialogFilters","CurrentMainBase","plug");
366                         foreach($cookie_vars as $var) {
367                             if(isset($cookie[$var])) {
368                                 session::global_set($var,$cookie[$var]);
369                             }
370                         }
371                         if(isset($cookie['plug'])) {
372                             $plug =$cookie['plug'];
373                         }
374                     }
375                 }
376             }
378             /* are we using accountexpiration */
379             if ($config->get_cfg_value("core","handleExpiredAccounts") == "true") {
380                 $expired= ldap_expired_account($config, $ui->dn, $ui->username);
382                 if ($expired == 1) {
383                     $message= _("Account locked. Please contact your system administrator!");
384                     $smarty->assign ('nextfield', 'password');
385                     new log("security","login","",array(),"Account for user \"$username\" has expired") ;
386                 } elseif ($expired == 3) {
387                     $plist= new pluglist($config, $ui);
388                     foreach ($plist->dirlist as $key => $value) {
389                         if (preg_match("/\bpassword\b/i",$value)) {
390                             $plug=$key;
391                             new log("security","login","",array(),"User \"$username\" password forced to change") ;
392                             header ("Location: main.php?plug=$plug&amp;reset=1");
393                             exit;
394                         }
395                     }
396                 }
397             }
399             /* Not account expired or password forced change go to main page */
400             new log("security","login","",array(),"User \"$username\" logged in successfully") ;
401             $plist= new pluglist($config, $ui);
402             if(isset($plug) && isset($plist->dirlist[$plug])) {
403                 header ("Location: main.php?plug=".$plug."&amp;global_check=1");
404             }else{
405                 header ("Location: main.php?global_check=1");
406             }
407             exit;
408         }
409     }
412 /* Fill template with required values */
413 $smarty->assign ('date', gmdate("D, d M Y H:i:s"));
414 $smarty->assign ('username', $username);
415 $smarty->assign ('personal_img', get_template_path('images/login-head.png'));
416 $smarty->assign ('password_img', get_template_path('images/password.png'));
417 $smarty->assign ('directory_img', get_template_path('images/ldapserver.png'));
419 /* Some error to display? */
420 if (!isset($message)) {
421     $message= "";
424 $smarty->assign ("message", $message);
426 /* Translation of cookie-warning. Whether to display it, is determined by JavaScript */
427 $smarty->assign ("cookies", _("Your browser has cookies disabled: please enable cookies and reload this page before logging in!"));
429 /* Generate server list */
430 $servers= array();
431 if (isset($_POST['server'])) {
432     $selected= validate($_POST['server']);
433 } else {
434     $selected= $config->data['MAIN']['DEFAULT'];
436 foreach ($config->data['LOCATIONS'] as $key => $ignored) {
437     $servers[$key]= $key;
439 $smarty->assign ("server_options", $servers);
440 $smarty->assign ("server_id", $selected);
442 /* show login screen */
443 $smarty->assign ("PHPSESSID", session_id());
444 if (session::is_set('errors')) {
445     $smarty->assign("errors", session::get('errors'));
447 if ($error_collector != "") {
448     $smarty->assign("php_errors", preg_replace("/%BUGBODY%/",$error_collector_mailto,$error_collector)."</div>");
449 } else {
450     $smarty->assign("php_errors", "");
453 /* Set focus to the error button if we've an error message */
454 $focus= "";
455 if (session::is_set('errors') && session::get('errors') != "") {
456     $focus= '<script language="JavaScript" type="text/javascript">';
457     $focus.= 'document.forms[0].error_accept.focus();';
458     $focus.= '</script>';
460 $smarty->assign("focus", $focus);
461 displayLogin();
463 // vim:tabstop=2:expandtab:shiftwidth=2:softtabstop=2:filetype=php:syntax:ruler:
464 ?>
466 </body>
467 </html>