Code

Replaced gosa_log with new logging class
[gosa.git] / html / index.php
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) 2003-2005  Cajus Pollmeier
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
21 /* Load required includes */
22 require_once ("../include/php_setup.inc");
23 require_once ("functions.inc");
24 require_once ("class_log.inc");
25 header("Content-type: text/html; charset=UTF-8");
27 function displayLogin()
28 {
29   global $smarty,$message,$config,$ssl,$error_collector;
30   error_reporting(E_ALL);
31     /* Fill template with required values */
32     $username = "";
33     if(isset($_POST["username"])){
34       $username= $_POST["username"];
35     }
36     $smarty->assign ('date', gmdate("D, d M Y H:i:s"));
37     $smarty->assign ('username', $username);
38     $smarty->assign ('personal_img', get_template_path('images/personal.png'));
39     $smarty->assign ('password_img', get_template_path('images/password.png'));
40     $smarty->assign ('directory_img', get_template_path('images/ldapserver.png'));
42     /* Some error to display? */
43     if (!isset($message)){
44       $message= "";
45     }
46     $smarty->assign ("message", $message);
48     /* Displasy SSL mode warning? */
49     if ($ssl != "" && $config->data['MAIN']['WARNSSL'] == 'true'){
50       $smarty->assign ("ssl", _("Warning").": <a style=\"color:red;\" href=\"$ssl\">"._("Session is not encrypted!")."</a>");
51     } else {
52       $smarty->assign ("ssl", "");
53     }
55     /* Generate server list */
56     $servers= array();
57     if (isset($_POST['server'])){
58       $selected= validate($_POST['server']);
59     } else {
60       $selected= $config->data['MAIN']['DEFAULT'];
61     }
62     foreach ($config->data['LOCATIONS'] as $key => $ignored){
63       $servers[$key]= $key;
64     }
65     $smarty->assign ("server_options", $servers);
66     $smarty->assign ("server_id", $selected);
68     /* show login screen */
69     $smarty->assign ("PHPSESSID", session_id());
70     if (isset($_SESSION['errors'])){
71       $smarty->assign("errors", $_SESSION['errors']);
72     }
73     if ($error_collector != ""){
74       $smarty->assign("php_errors", $error_collector."</div>");
75     } else {
76       $smarty->assign("php_errors", "");
77     }
79     $smarty->display (get_template_path('headers.tpl'));
80     $smarty->display(get_template_path('login.tpl'));
81     exit();
82 }
86 /* Set error handler to own one, initialize time calculation
87    and start session. */
88 session_start ();
90 /* Destroy old session if exists. 
91     Else you will get your old session back, if you not logged out correctly. */
92 if(is_array($_SESSION) && count($_SESSION)){
93   session_destroy();
94   session_start();
95 }
97 $username= "";
99 /* Reset errors */
100 $_SESSION['errors']             = "";
101 $_SESSION['errorsAlreadyPosted']= array();
102 $_SESSION['LastError']          = "";
104 /* Check if we need to run setup */
105 if (!file_exists(CONFIG_DIR."/".CONFIG_FILE)){
106   header("location:setup.php");
107   exit();
110 /* Reset errors */
111 $_SESSION['errors']= "";
113 /* Check for java script */
114 if(isset($_POST['javascript']) && $_POST['javascript'] == "true") {
115   $_SESSION['js']= TRUE;
116 }elseif(isset($_POST['javascript'])) {
117   $_SESSION['js']= FALSE;
120 /* Check if gosa.conf (.CONFIG_FILE) is accessible */
121 if (!is_readable(CONFIG_DIR."/".CONFIG_FILE)){
122   echo sprintf(_("GOsa configuration %s/%s is not readable. Aborted."), CONFIG_DIR,CONFIG_FILE);
123   exit();
126 /* Parse configuration file */
127 $config= new config(CONFIG_DIR."/".CONFIG_FILE, $BASE_DIR);
128 $_SESSION['DEBUGLEVEL']= $config->data['MAIN']['DEBUGLEVEL'];
129 if ($_SERVER["REQUEST_METHOD"] != "POST"){
130   @DEBUG (DEBUG_CONFIG, __LINE__, __FUNCTION__, __FILE__, $config->data, "config");
133 /* Set template compile directory */
134 if (isset ($config->data['MAIN']['COMPILE'])){
135   $smarty->compile_dir= $config->data['MAIN']['COMPILE'];
136 } else {
137   $smarty->compile_dir= '/var/spool/gosa';
139 $smarty->assign ('nextfield', 'username');
141 /* Check for compile directory */
142 if (!(is_dir($smarty->compile_dir) && is_writable($smarty->compile_dir))){
143   echo sprintf(_("Directory '%s' specified as compile directory is not accessible!"),
144         $smarty->compile_dir);
145   exit();
148 /* Check for old files in compile directory */
149 clean_smarty_compile_dir($smarty->compile_dir);
151 /* Language setup */
152 if ($config->data['MAIN']['LANG'] == ""){
153   $lang= get_browser_language();
154 } else {
155   $lang= $config->data['MAIN']['LANG'];
157 $lang.=".UTF-8";
158 putenv("LANGUAGE=");
159 putenv("LANG=$lang");
160 setlocale(LC_ALL, $lang);
161 $GLOBALS['t_language']= $lang;
162 $GLOBALS['t_gettext_message_dir'] = $BASE_DIR.'/locale/';
164 /* Set the text domain as 'messages' */
165 $domain = 'messages';
166 bindtextdomain($domain, "$BASE_DIR/locale");
167 textdomain($domain);
170 if ($_SERVER["REQUEST_METHOD"] != "POST"){
171   @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, $lang, "Setting language to");
175 /* Check for SSL connection */
176 $ssl= "";
177 if (!isset($_SERVER['HTTPS']) ||
178     !stristr($_SERVER['HTTPS'], "on")) {
180   if (empty($_SERVER['REQUEST_URI'])) {
181     $ssl= "https://".$_SERVER['HTTP_HOST'].
182       $_SERVER['PATH_INFO'];
183   } else {
184     $ssl= "https://".$_SERVER['HTTP_HOST'].
185       $_SERVER['REQUEST_URI'];
186   }
189 /* If SSL is forced, just forward to the SSL enabled site */
190 if ($config->data['MAIN']['FORCESSL'] == 'true' && $ssl != ''){
191   header ("Location: $ssl");
192   exit;
195 /* Got a formular answer, validate and try to log in */
196 if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['login'])){
198   /* Reset error messages */
199   $message= "";
201   /* Destroy old sessions, they cause a successfull login to relog again ...*/
202   if(isset($_SESSION['_LAST_PAGE_REQUEST'])){
203     $_SESSION['_LAST_PAGE_REQUEST'] = time();
204   }
206   $server= validate($_POST["server"]);
207   $config->set_current($server);
209   /* Admin-logon and verify */
210   $ldap = $config->get_ldap_link();
211   if (is_null($ldap) || (is_int($ldap) && $ldap == 0)){
212     print_red (_("Can't bind to LDAP. Please contact the system administrator."));
213     displayLogin();
214     exit();
215   }
217   /* Check for schema file presence */
218   if(!isset($config->data['MAIN']['SCHEMA_CHECK'])){
219     $config->data['MAIN']['SCHEMA_CHECK'] = "true";
220   }
221   if(isset($config->data['MAIN']['SCHEMA_CHECK'])&&preg_match("/true/i",$config->data['MAIN']['SCHEMA_CHECK'])){
222     $recursive = (isset($config->current['RECURSIVE']) && $config->current['RECURSIVE'] == "true");
223     $tls =       (isset($config->current['TLS'])       && $config->current['TLS'] == "true");
225     if(!count($ldap->get_objectclasses())){
226       print_red(_("GOsa cannot retrieve information about the installed schema files. Please make sure, that this is possible."));
227       displayLogin();
228       exit()  ;
229     }else{
230       $cfg = array();
231       $cfg['admin']     = $config->current['ADMIN'];
232       $cfg['password']  = $config->current['PASSWORD'];
233       $cfg['connection']= $config->current['SERVER'];
234       $cfg['tls']       = $tls;
235       $str = check_schema($cfg,isset($config->current['RFC2307BIS']) && preg_match("/(true|yes|on|1)/i",$config->current['RFC2307BIS']));
236       $checkarr = array();
237       foreach($str as $tr){
238         if(isset($tr['IS_MUST_HAVE']) && !$tr['STATUS']){
239           print_red($tr['MSG']."<br>"._("Your ldap setup contains old schema definitions. Please re-run the setup."));
240           displayLogin();
241           exit();
242         }
243       }
244     }
245   }
246   /* Check for locking area */
247   $ldap->cat($config->current['CONFIG'], array("dn"));
248   $attrs= $ldap->fetch();
249   if (!count ($attrs)){
250     $ldap->cd($config->current['BASE']);
251     $ldap->create_missing_trees($config->current['CONFIG']);
252   }
254   /* Check for valid input */
255   $username= $_POST["username"];
256   if (!ereg("^[A-Za-z0-9_.-]+$", $username)){
257     $message= _("Please specify a valid username!");
258   } elseif (mb_strlen($_POST["password"], 'UTF-8') == 0){
259     $message= _("Please specify your password!");
260     $smarty->assign ('nextfield', 'password');
261   } else {
263     /* Login as user, initialize user ACL's */
264     $ui= ldap_login_user($username, $_POST["password"]);
265     if ($ui === NULL || $ui == 0){
266       $message= _("Please check the username/password combination.");
267       $smarty->assign ('nextfield', 'password');
268       new log("security","login","",array(),"Authentication failed for user \"$username\"") ;
269     } else {
270       /* Remove all locks of this user */
271       del_user_locks($ui->dn);
273       /* Save userinfo and plugin structure */
274       $_SESSION['ui']= $ui;
275       $_SESSION['session_cnt']= 0;
277       /* Let GOsa trigger a new connection for each POST, save
278          config to session. */
279       $config->get_departments();
280       $config->make_idepartments();
281       $_SESSION['config']= $config;
283       /* Take care about zend.ze1_compatiblity_mode */
284       if (ini_get("zend.ze1_compatibility_mode") != 0){
285         $_SESSION['PHP4COMPATIBLE']= TRUE;
286       }
288       /* are we using accountexpiration */
289       if((isset($config->data['MAIN']['ACCOUNT_EXPIRATION'])) && 
290           preg_match('/true/i', $config->data['MAIN']['ACCOUNT_EXPIRATION'])){
291       
292         $expired= ldap_expired_account($config, $ui->dn, $ui->username);
294         if ($expired == 1){
295           $message= _("Account locked. Please contact your system administrator.");
296           $smarty->assign ('nextfield', 'password');
297           new log("security","login","",array(),"Account for user \"$username\" has expired") ;
298         } elseif ($expired == 3){
299             $plist= new pluglist($config, $ui);
300             foreach ($plist->dirlist as $key => $value){
301               if (preg_match("/\bpassword\b/i",$value)){
302                 $plug=$key;
303                 new log("security","login","",array(),"User \"$username\" password forced to change") ;
304                 header ("Location: main.php?plug=$plug&amp;reset=1");
305                 exit;
306               }
307             }
308           }
310         /* Not account expired or password forced change go to main page */
311         new log("security","login","",array(),"User \"$username\" logged in successfully") ;
312         header ("Location: main.php?global_check=1");
313         exit;
314         
315       } else {
316         /* Go to main page */
317         new log("security","login","",array(),"User \"$username\" logged in successfully") ;
318         header ("Location: main.php?global_check=1");
319         exit;
320       }
321     }
322   }
325 /* Fill template with required values */
326 $smarty->assign ('date', gmdate("D, d M Y H:i:s"));
327 $smarty->assign ('username', $username);
328 $smarty->assign ('personal_img', get_template_path('images/personal.png'));
329 $smarty->assign ('password_img', get_template_path('images/password.png'));
330 $smarty->assign ('directory_img', get_template_path('images/ldapserver.png'));
332 /* Some error to display? */
333 if (!isset($message)){
334   $message= "";
337 $smarty->assign ("message", $message);
339 /* Displasy SSL mode warning? */
340 if ($ssl != "" && $config->data['MAIN']['WARNSSL'] == 'true'){
341   $smarty->assign ("ssl", "<b>"._("Warning").":<\/b> "._("Session will not be encrypted.")." <a style=\"color:red;\" href=\"$ssl\"><b>"._("Enter SSL session")."<\/b></a>!");
342 } else {
343   $smarty->assign ("ssl", "");
346 /* Translation of cookie-warning. Whether to display it, is determined by JavaScript */
347 $smarty->assign ("cookies", "<b>"._("Warning").":<\/b> "._("Your browser has cookies disabled. Please enable cookies and reload this page before logging in!"));
350 /* Generate server list */
351 $servers= array();
352 if (isset($_POST['server'])){
353   $selected= validate($_POST['server']);
354 } else {
355   $selected= $config->data['MAIN']['DEFAULT'];
357 foreach ($config->data['LOCATIONS'] as $key => $ignored){
358   $servers[$key]= $key;
360 $smarty->assign ("server_options", $servers);
361 $smarty->assign ("server_id", $selected);
363 /* show login screen */
364 $smarty->assign ("PHPSESSID", session_id());
365 if (isset($_SESSION['errors'])){
366   $smarty->assign("errors", $_SESSION['errors']);
368 if ($error_collector != ""){
369   $smarty->assign("php_errors", $error_collector."</div>");
370 } else {
371   $smarty->assign("php_errors", "");
373 displayLogin();
376 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
377 ?>
379 </body>
380 </html>