Code

99d5954528a9d2981b4ae7f42b20a692d0c0a5a5
[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;
37   error_reporting(E_ALL | E_STRICT);
38   /* Fill template with required values */
39   $username = "";
40   if(isset($_POST["username"])){
41     $username= get_post("username");
42   }
43   $smarty->assign ('date', gmdate("D, d M Y H:i:s"));
44   $smarty->assign ('username', $username);
45   $smarty->assign ('personal_img', get_template_path('images/login-head.png'));
46   $smarty->assign ('password_img', get_template_path('images/password.png'));
47   $smarty->assign ('directory_img', get_template_path('images/ldapserver.png'));
49   /* Some error to display? */
50   if (!isset($message)){
51     $message= "";
52   }
53   $smarty->assign ("message", $message);
55   /* Displasy SSL mode warning? */
56   if ($ssl != "" && $config->get_cfg_value('warnssl') == 'true'){
57     $smarty->assign ("ssl", _("Warning").": <a style=\"color:red;\" href=\"$ssl\">"._("Session is not encrypted!")."</a>");
58   } else {
59     $smarty->assign ("ssl", "");
60   }
62   if(!$config->check_session_lifetime()){
63     $smarty->assign ("lifetime", _("Warning").": ".
64         _("The session lifetime configured in your gosa.conf will be overridden by php.ini settings."));
65   }else{
66     $smarty->assign ("lifetime", "");
67   }
69   /* Generate server list */
70   $servers= array();
71   if (isset($_POST['server'])){
72     $selected= get_post('server');
73   } else {
74     $selected= $config->data['MAIN']['DEFAULT'];
75   }
76   foreach ($config->data['LOCATIONS'] as $key => $ignored){
77     $servers[$key]= $key;
78   }
79   $smarty->assign ("server_options", $servers);
80   $smarty->assign ("server_id", $selected);
82   /* show login screen */
83   $smarty->assign ("PHPSESSID", session_id());
84   if (session::is_set('errors')){
85     $smarty->assign("errors", session::get('errors'));
86   }
87   if ($error_collector != ""){
88     $smarty->assign("php_errors", $error_collector."</div>");
89   } else {
90     $smarty->assign("php_errors", "");
91   }
92   $smarty->assign("msg_dialogs", msg_dialog::get_dialogs());
93   $smarty->assign("iePngWorkaround", $config->get_cfg_value("iePngWorkaround","false" ) == "true");
94   $smarty->assign("usePrototype", "false");
95   $smarty->display (get_template_path('headers.tpl'));
96   $smarty->assign("version",get_gosa_version());
97   $smarty->display(get_template_path('login.tpl'));
98   exit();
99 }
103 /*****************************************************************************
104  *                               M   A   I   N                               *
105  *****************************************************************************/
107 /* Set error handler to own one, initialize time calculation
108    and start session. */
109 session::start();
110 session::set('errorsAlreadyPosted',array());
112 /* Destroy old session if exists. 
113    Else you will get your old session back, if you not logged out correctly. */
114 if(is_array(session::get_all()) && count(session::get_all())){
115   session::destroy();
116   session::start();
119 $username= "";
121 /* Reset errors */
122 session::set('errors',"");
123 session::set('errorsAlreadyPosted',"");
124 session::set('LastError',"");
126 /* Check if we need to run setup */
127 if (!file_exists(CONFIG_DIR."/".CONFIG_FILE)){
128   header("location:setup.php");
129   exit();
132 /* Reset errors */
133 session::set('errors',"");
135 /* Check for java script */
136 if(isset($_POST['javascript']) && $_POST['javascript'] == "true") {
137   session::global_set('js',TRUE);
138 }elseif(isset($_POST['javascript'])) {
139   session::global_set('js',FALSE);
142 /* Check if gosa.conf (.CONFIG_FILE) is accessible */
143 if (!is_readable(CONFIG_DIR."/".CONFIG_FILE)){
144   msg_dialog::display(_("Configuration error"),sprintf(_("GOsa configuration %s/%s is not readable. Aborted."), CONFIG_DIR,CONFIG_FILE),FATAL_ERROR_DIALOG);
145   exit();
148 /* Parse configuration file */
149 $config= new config(CONFIG_DIR."/".CONFIG_FILE, $BASE_DIR);
150 session::global_set('DEBUGLEVEL',$config->get_cfg_value('DEBUGLEVEL'));
151 if ($_SERVER["REQUEST_METHOD"] != "POST"){
152   @DEBUG (DEBUG_CONFIG, __LINE__, __FUNCTION__, __FILE__, $config->data, "config");
155 /* Enable compressed output */
156 if ($config->get_cfg_value("sendCompressedOutput") != ""){
157   ob_start("ob_gzhandler");
160 /* Set template compile directory */
161 $smarty->compile_dir= $config->get_cfg_value("templateCompileDirectory", '/var/spool/gosa');
163 /* Check for compile directory */
164 if (!(is_dir($smarty->compile_dir) && is_writable($smarty->compile_dir))){
165   msg_dialog::display(_("Smarty error"),sprintf(_("Directory '%s' specified as compile directory is not accessible!"),
166         $smarty->compile_dir),FATAL_ERROR_DIALOG);
167   exit();
170 /* Check for old files in compile directory */
171 clean_smarty_compile_dir($smarty->compile_dir);
173 /* Language setup */
174 $lang= get_browser_language();
175 putenv("LANGUAGE=");
176 putenv("LANG=$lang");
177 setlocale(LC_ALL, $lang);
178 $GLOBALS['t_language']= $lang;
179 $GLOBALS['t_gettext_message_dir'] = $BASE_DIR.'/locale/';
181 /* Set the text domain as 'messages' */
182 $domain = 'messages';
183 bindtextdomain($domain, LOCALE_DIR);
184 textdomain($domain);
185 $smarty->assign ('nextfield', 'username');
187 if ($_SERVER["REQUEST_METHOD"] != "POST"){
188   @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, $lang, "Setting language to");
192 /* Check for SSL connection */
193 $ssl= "";
194 if (!isset($_SERVER['HTTPS']) ||
195     !stristr($_SERVER['HTTPS'], "on")) {
197   if (empty($_SERVER['REQUEST_URI'])) {
198     $ssl= "https://".$_SERVER['HTTP_HOST'].
199       $_SERVER['PATH_INFO'];
200   } else {
201     $ssl= "https://".$_SERVER['HTTP_HOST'].
202       $_SERVER['REQUEST_URI'];
203   }
206 /* If SSL is forced, just forward to the SSL enabled site */
207 if ($config->get_cfg_value("forcessl") == 'true' && $ssl != ''){
208   header ("Location: $ssl");
209   exit;
212 /* Do we have htaccess authentification enabled? */
213 $htaccess_authenticated= FALSE;
214 if ($config->get_cfg_value("htaccessAuthentication") == "true" ){
215   if (!isset($_SERVER['REMOTE_USER'])){
216     msg_dialog::display(_("Configuration error"), _("There is a problem with the authentication setup!"), FATAL_ERROR_DIALOG);
217     exit;
218   }
220   $tmp= process_htaccess($_SERVER['REMOTE_USER'], isset($_SERVER['KRB5CCNAME']));
221   $username= $tmp['username'];
222   $server= $tmp['server'];
223   if ($username == ""){
224     msg_dialog::display(_("Error"), _("Cannot find a valid user for the current authentication setup!"), FATAL_ERROR_DIALOG);
225     exit;
226   }
227   if ($server == ""){
228     msg_dialog::display(_("Error"), _("User information is not unique accross the configured LDAP trees!"), FATAL_ERROR_DIALOG);
229     exit;
230   }
232   $htaccess_authenticated= TRUE;
235 /* Got a formular answer, validate and try to log in */
236 if (($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['login'])) || $htaccess_authenticated){
238   /* Reset error messages */
239   $message= "";
241   /* Destroy old sessions, they cause a successfull login to relog again ...*/
242   if(session::global_is_set('_LAST_PAGE_REQUEST')){
243     session::global_set('_LAST_PAGE_REQUEST',time());
244   }
246   if (!$htaccess_authenticated){
247     $server= get_post("server");
248   }
249   $config->set_current($server);
251   /* Admin-logon and verify */
252   $ldap = $config->get_ldap_link();
253   if (is_null($ldap) || (is_int($ldap) && $ldap == 0)){
254     msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
255     displayLogin();
256     exit();
257   }
259   /* Check for schema file presence */
260   if ($config->get_cfg_value("schemaCheck") == "true"){
261     $recursive = ($config->get_cfg_value("ldapFollowReferrals") == "true");
262     $tls =       ($config->get_cfg_value("ldapTLS") == "true");
264     if(!count($ldap->get_objectclasses())){
265       msg_dialog::display(_("LDAP error"), _("Cannot detect information about the installed LDAP schema!"), ERROR_DIALOG);
266       displayLogin();
267       exit()  ;
268     }else{
269       $cfg = array();
270       $cfg['admin']     = $config->current['ADMINDN'];
271       $cfg['password']  = $config->current['ADMINPASSWORD'];
272       $cfg['connection']= $config->current['SERVER'];
273       $cfg['tls']       = $tls;
274       $str = check_schema($cfg, $config->get_cfg_value("rfc2307bis") == "true");
275       $checkarr = array();
276       foreach($str as $tr){
277         if(isset($tr['IS_MUST_HAVE']) && !$tr['STATUS']){
278           msg_dialog::display(_("LDAP error"), _("Your LDAP setup contains old schema definitions:")."<br><br><i>".$tr['MSG']."</i>", ERROR_DIALOG);
279           displayLogin();
280           exit();
281         }
282       }
283     }
284   }
286   /* Check for locking area */
287   $ldap->cat($config->get_cfg_value("config"), array("dn"));
288   $attrs= $ldap->fetch();
289   if (!count ($attrs)){
290     $ldap->cd($config->current['BASE']);
291     $ldap->create_missing_trees($config->get_cfg_value("config"));
292   }
294   /* Check for valid input */
295   $ok= true;
296   if (!$htaccess_authenticated){
297     $username= get_post("username");
298     if (!preg_match("/^[@A-Za-z0-9_.-]+$/", $username)){
299       $message= _("Please specify a valid username!");
300       $ok= false;
301     } elseif (mb_strlen(get_post("password"), 'UTF-8') == 0){
302       $message= _("Please specify your password!");
303       $smarty->assign ('nextfield', 'password');
304       $ok= false;
305     }
306   }
308   if ($ok) {
310     /* Login as user, initialize user ACL's */
311     if ($htaccess_authenticated){
312       $ui= ldap_login_user_htaccess($username);
313       if ($ui === NULL || !$ui){
314         msg_dialog::display(_("Authentication error"), _("Cannot retrieve user information for htaccess authentication!"), FATAL_ERROR_DIALOG);
315         exit;
316       }
317     } else {
318       $ui= ldap_login_user($username, get_post("password"));
319     }
320     if ($ui === NULL || !$ui){
321       $message= _("Please check the username/password combination.");
322       $smarty->assign ('nextfield', 'password');
323       session::global_set('config',$config);
324       new log("security","login","",array(),"Authentication failed for user \"$username\"");
325     } else {
326       /* Remove all locks of this user */
327       del_user_locks($ui->dn);
329       /* Save userinfo and plugin structure */
330       session::global_set('ui',$ui);
331       session::global_set('session_cnt',0);
333       /* Let GOsa trigger a new connection for each POST, save
334          config to session. */
335       $config->get_departments();
336       $config->make_idepartments();
337       session::global_set('config',$config);
339       /* Restore filter settings from cookie, if available */
340       if($config->get_cfg_value("storeFilterSettings") == "true"){
342         if(isset($_COOKIE['GOsa_Filter_Settings']) || isset($HTTP_COOKIE_VARS['GOsa_Filter_Settings'])){
344           if(isset($_COOKIE['GOsa_Filter_Settings'])){
345             $cookie_all = unserialize(base64_decode($_COOKIE['GOsa_Filter_Settings']));
346           }else{
347             $cookie_all = unserialize(base64_decode($HTTP_COOKIE_VARS['GOsa_Filter_Settings']));
348           }
349           if(isset($cookie_all[$ui->dn])){
350             $cookie = $cookie_all[$ui->dn];
351             $cookie_vars= array("MultiDialogFilters","CurrentMainBase","plug");
352             foreach($cookie_vars as $var){
353               if(isset($cookie[$var])){
354                 session::global_set($var,$cookie[$var]);
355               }
356             }
357             if(isset($cookie['plug'])){
358               $plug =$cookie['plug'];
359             }
360           }
361         }
362       }
364       /* are we using accountexpiration */
365       if ($config->get_cfg_value("handleExpiredAccounts") == "true"){
366         $expired= ldap_expired_account($config, $ui->dn, $ui->username);
368         if ($expired == POSIX_ACCOUNT_EXPIRED){
369           $message= _("Account locked. Please contact your system administrator!");
370           $smarty->assign ('nextfield', 'password');
371           new log("security","login","",array(),"Account for user \"$username\" has expired") ;
372           displayLogin();
373           exit();
374         }
375       }
377       /* Not account expired or password forced change go to main page */
378       new log("security","login","",array(),"User \"$username\" logged in successfully") ;
379       $plist= new pluglist($config, $ui);
380       if(isset($plug) && isset($plist->dirlist[$plug])){
381         header ("Location: main.php?plug=".$plug."&global_check=1");
382       }else{
383         header ("Location: main.php?global_check=1");
384       }
385       exit;
386     }
387   }
390 /* Fill template with required values */
391 $smarty->assign ('date', gmdate("D, d M Y H:i:s"));
392 $smarty->assign ('username', $username);
393 $smarty->assign ('personal_img', get_template_path('images/login-head.png'));
394 $smarty->assign ('password_img', get_template_path('images/password.png'));
395 $smarty->assign ('directory_img', get_template_path('images/ldapserver.png'));
397 /* Some error to display? */
398 if (!isset($message)){
399   $message= "";
402 $smarty->assign ("message", $message);
404 /* Displasy SSL mode warning? */
405 if ($ssl != "" && $config->get_cfg_value('WARNSSL') == 'true'){
406   $smarty->assign ("ssl", "<b>"._("Warning").":<\/b> "._("Session will not be encrypted.")." <a style=\"color:red;\" href=\"$ssl\"><b>"._("Enter SSL session")."<\/b></a>!");
407 } else {
408   $smarty->assign ("ssl", "");
411 /* Translation of cookie-warning. Whether to display it, is determined by JavaScript */
412 $smarty->assign ("cookies", "<b>"._("Warning").":<\/b> "._("Your browser has cookies disabled. Please enable cookies and reload this page before logging in!"));
414 /* Generate server list */
415 $servers= array();
416 if (isset($_POST['server'])){
417   $selected= get_post('server');
418 } else {
419   $selected= $config->data['MAIN']['DEFAULT'];
421 foreach ($config->data['LOCATIONS'] as $key => $ignored){
422   $servers[$key]= $key;
424 $smarty->assign ("server_options", $servers);
425 $smarty->assign ("server_id", $selected);
427 /* show login screen */
428 $smarty->assign ("PHPSESSID", session_id());
429 if (session::is_set('errors')){
430   $smarty->assign("errors", session::get('errors'));
432 if ($error_collector != ""){
433   $smarty->assign("php_errors", preg_replace("/%BUGBODY%/",$error_collector_mailto,$error_collector)."</div>");
434 } else {
435   $smarty->assign("php_errors", "");
438 /* Set focus to the error button if we've an error message */
439 $focus= "";
440 if (session::is_set('errors') && session::get('errors') != ""){
441   $focus= '<script language="JavaScript" type="text/javascript">';
442   $focus.= 'document.forms[0].error_accept.focus();';
443   $focus.= '</script>';
445 $smarty->assign("focus", $focus);
446 displayLogin();
448 // vim:tabstop=2:expandtab:shiftwidth=2:softtabstop=2:filetype=php:syntax:ruler:
449 ?>
451 </body>
452 </html>