Code

Udpated focus for password tpl
[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 header("Content-type: text/html; charset=UTF-8");
26 function displayLogin()
27 {
28   global $smarty,$message,$config,$ssl,$error_collector;
29   error_reporting(E_ALL);
30     /* Fill template with required values */
31     $username = "";
32     if(isset($_POST["username"])){
33       $username= $_POST["username"];
34     }
36     /* Place ie workaround if needed */
37     if (isset($config->data['MAIN']['IE_PNG_WORKAROUND']) && preg_match("/true/i",$config->data['MAIN']['IE_PNG_WORKAROUND'])){
38       $smarty->assign('ieworkaround', 1);
39     }
41     $smarty->assign ('date', gmdate("D, d M Y H:i:s"));
42     $smarty->assign ('username', $username);
43     $smarty->assign ('personal_img', get_template_path('images/personal.png'));
44     $smarty->assign ('password_img', get_template_path('images/password.png'));
45     $smarty->assign ('directory_img', get_template_path('images/ldapserver.png'));
47     /* Some error to display? */
48     if (!isset($message)){
49       $message= "";
50     }
51     $smarty->assign ("message", $message);
53     /* Displasy SSL mode warning? */
54     if ($ssl != "" && $config->data['MAIN']['WARNSSL'] == 'true'){
55       $smarty->assign ("ssl", "<b>"._("Warning").":</b> "._("Session will not be encrypted.")." <a style=\"color:red;\" href=\"$ssl\"><b>"._("Enter SSL session")."</b></a>!");
56     } else {
57       $smarty->assign ("ssl", "");
58     }
60     /* Generate server list */
61     $servers= array();
62     if (isset($_POST['server'])){
63       $selected= validate($_POST['server']);
64     } else {
65       $selected= $config->data['MAIN']['DEFAULT'];
66     }
67     foreach ($config->data['LOCATIONS'] as $key => $ignored){
68       $servers[$key]= $key;
69     }
70     $smarty->assign ("server_options", $servers);
71     $smarty->assign ("server_id", $selected);
73     /* show login screen */
74     $smarty->assign ("PHPSESSID", session_id());
75     if (isset($_SESSION['errors'])){
76       $smarty->assign("errors", $_SESSION['errors']);
77     }
78     if ($error_collector != ""){
79       $smarty->assign("php_errors", preg_replace("/%BUGBODY%/",$error_collector_mailto,$error_collector)."</div>");
80     } else {
81       $smarty->assign("php_errors", "");
82     }
84     $smarty->display(get_template_path('headers.tpl'));
85     $smarty->display(get_template_path('login.tpl'));
86     exit();
87 }
89 session_start();
91 /* Destroy old session if exists.
92     Else you will get your old session back, if you not logged out correctly. */
93 if(is_array($_SESSION) && count($_SESSION)){
94   session_destroy();
95   session_start();
96 }
98 $username= "";
100 /* Reset errors */
101 $_SESSION['errors']             = "";
102 $_SESSION['errorsAlreadyPosted']= array();
103 $_SESSION['LastError']          = "";
105 /* Check if we need to run setup */
106 if (!file_exists(CONFIG_DIR."/".CONFIG_FILE)){
107   header("location:setup.php");
108   exit();
111 /* Check for java script */
112 if(isset($_POST['javascript']) && $_POST['javascript'] == "true") {
113   $_SESSION['js']= TRUE;
114 }elseif(isset($_POST['javascript'])) {
115   $_SESSION['js']= FALSE;
118 /* Check if CONFIG_FILE is accessible */
119 if (!is_readable(CONFIG_DIR."/".CONFIG_FILE)){
120   echo sprintf(_("GOsa configuration %s/%s is not readable. Aborted."), CONFIG_DIR,CONFIG_FILE);
121   exit();
124 /* Parse configuration file */
125 $config= new config(CONFIG_DIR."/".CONFIG_FILE, $BASE_DIR);
126 $_SESSION['DEBUGLEVEL']= $config->data['MAIN']['DEBUGLEVEL'];
127 if ($_SERVER["REQUEST_METHOD"] != "POST"){
128   @DEBUG (DEBUG_CONFIG, __LINE__, __FUNCTION__, __FILE__, $config->data, "config");
131 /* Set template compile directory */
132 if (isset ($config->data['MAIN']['COMPILE'])){
133   $smarty->compile_dir= $config->data['MAIN']['COMPILE'];
134 } else {
135   $smarty->compile_dir= '/var/spool/gosa';
137 $smarty->assign ('nextfield', 'username');
139 /* Check for compile directory */
140 if (!(is_dir($smarty->compile_dir) && is_writable($smarty->compile_dir))){
141   echo sprintf(_("Directory '%s' specified as compile directory is not accessible!"),
142         $smarty->compile_dir);
143   exit();
146 /* Check for old files in compile directory */
147 clean_smarty_compile_dir($smarty->compile_dir);
149 /* Language setup */
150 if ($config->data['MAIN']['LANG'] == ""){
151   $lang= get_browser_language();
152 } else {
153   $lang= $config->data['MAIN']['LANG'];
155 $lang.=".UTF-8";
156 putenv("LANGUAGE=");
157 putenv("LANG=$lang");
158 setlocale(LC_ALL, $lang);
159 $GLOBALS['t_language']= $lang;
160 $GLOBALS['t_gettext_message_dir'] = $BASE_DIR.'/locale/';
162 /* Set the text domain as 'messages' */
163 $domain = 'messages';
164 bindtextdomain($domain, "$BASE_DIR/locale");
165 textdomain($domain);
168 if ($_SERVER["REQUEST_METHOD"] != "POST"){
169   @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, $lang, "Setting language to");
173 /* Check for SSL connection */
174 $ssl= "";
175 if (!isset($_SERVER['HTTPS']) ||
176     !stristr($_SERVER['HTTPS'], "on")) {
178   if (empty($_SERVER['REQUEST_URI'])) {
179     $ssl= "https://".$_SERVER['HTTP_HOST'].
180       $_SERVER['PATH_INFO'];
181   } else {
182     $ssl= "https://".$_SERVER['HTTP_HOST'].
183       $_SERVER['REQUEST_URI'];
184   }
187 /* If SSL is forced, just forward to the SSL enabled site */
188 if ($config->data['MAIN']['FORCESSL'] == 'true' && $ssl != ''){
189   header ("Location: $ssl");
190   exit;
193 /* Got a formular answer, validate and try to log in */
194 if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['login'])){
196   /* Reset error messages */
197   $message= "";
199   /* Destroy old sessions, they cause a successfull login to relog again ...*/
200   if(isset($_SESSION['_LAST_PAGE_REQUEST'])){
201     $_SESSION['_LAST_PAGE_REQUEST'] = time();
202   }
204   $server= validate($_POST["server"]);
205   $config->set_current($server);
207   /* Admin-logon and verify */
208   $ldap = $config->get_ldap_link();
209   if (is_null($ldap) || (is_int($ldap) && $ldap == 0)){
210     print_red (_("Can't bind to LDAP. Please contact the system administrator."));
211     displayLogin();
212     exit();
213   }
215   /* Check for schema file presence */
216   if(!isset($config->data['MAIN']['SCHEMA_CHECK'])){
217     $config->data['MAIN']['SCHEMA_CHECK'] = "true";
218   }
219   if(isset($config->data['MAIN']['SCHEMA_CHECK'])&&preg_match("/true/i",$config->data['MAIN']['SCHEMA_CHECK'])){
220     $recursive = (isset($config->current['RECURSIVE']) && $config->current['RECURSIVE'] == "true");
221     $tls =       (isset($config->current['TLS'])       && $config->current['TLS'] == "true");
223     if(!count($ldap->get_objectclasses())){
225       print_red(_("GOsa cannot retrieve information about the installed schema files. Please make sure, that this is possible."));
226       displayLogin();
227       exit();
228     }else{
229  
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   
236       $str = check_schema($cfg,isset($config->current['RFC2307BIS']) && preg_match("/(true|yes|on|1)/i",$config->current['RFC2307BIS']));
238       $checkarr = array();
239       foreach($str as $tr){
240         if(isset($tr['IS_MUST_HAVE']) && !$tr['STATUS']){
241           print_red($tr['MSG']."<br>"._("Your ldap setup contains old schema definitions. Please re-run the setup."));
242           displayLogin();
243           exit();
244         }
245       }
246     }
247   }
248   /* Check for locking area */
249   $ldap->cat($config->current['CONFIG'], array("dn"));
250   $attrs= $ldap->fetch();
251   if (!count ($attrs)){
252     $ldap->cd($config->current['BASE']);
253     $ldap->create_missing_trees($config->current['CONFIG']);
254   }
256   /* Check for at least one subtreeACL in the complete tree */
257   $ldap->cd($config->current['BASE']);
258   $ldap->search("(&(objectClass=gosaObject)(gosaSubtreeACL=:all))");
259   if ($ldap->count() < 1){
260     print_red(_("You're missing an administrative account for GOsa, you'll not be able to administrate anything!"));
261     displayLogin();
262     exit();
263   }
265   /* Check for valid input */
266   $username= $_POST["username"];
267   if (!ereg("^[A-Za-z0-9_.-]+$", $username)){
268     $message= _("Please specify a valid username!");
269   } elseif (mb_strlen($_POST["password"], 'UTF-8') == 0){
270     $message= _("Please specify your password!");
271     $smarty->assign ('nextfield', 'password');
272   } else {
274     /* Login as user, initialize user ACL's */
275     $ui= ldap_login_user($username, $_POST["password"]);
276     if ($ui === NULL || !$ui ){
277       $message= _("Please check the username/password combination.");
278       $smarty->assign ('nextfield', 'password');
279       gosa_log ("Authentication failed for user \"$username\"");
280     } else {
281       /* Remove all locks of this user */
282       del_user_locks($ui->dn);
284       /* Save userinfo and plugin structure */
285       $_SESSION['ui']= $ui;
286       $_SESSION['session_cnt']= 0;
288       /* Let GOsa trigger a new connection for each POST, save
289          config to session. */
290       $config->get_departments();
291       $config->make_idepartments();
292       $_SESSION['config']= $config;
294       /* Take care about zend.ze1_compatiblity_mode */
295       if (ini_get("zend.ze1_compatibility_mode") != 0){
296         $_SESSION['PHP4COMPATIBLE']= TRUE;
297       }
299       /* are we using accountexpiration */
300       if((isset($config->data['MAIN']['ACCOUNT_EXPIRATION'])) && 
301           preg_match('/true/i', $config->data['MAIN']['ACCOUNT_EXPIRATION'])){
302       
303         $expired= ldap_expired_account($config, $ui->dn, $ui->username);
305         if ($expired == 1){
306           $message= _("Account locked. Please contact your system administrator.");
307           $smarty->assign ('nextfield', 'password');
308           gosa_log ("Account for user \"$username\" has expired");
309         } elseif ($expired == 3){
310             $plist= new pluglist($config, $ui);
311             foreach ($plist->dirlist as $key => $value){
312               if (preg_match("/\bpassword\b/i",$value)){
313                 $plug=$key;
314                 gosa_log ("User \"$username\" password forced to change");
315                 header ("Location: main.php?plug=$plug&reset=1");
316                 exit;
317               }
318             }
319           }
321         /* Not account expired or password forced change go to main page */
322         gosa_log ("User \"$username\" logged in successfully");
323         header ("Location: main.php?global_check=1");
324         exit;
325         
326       } else {
327         /* Go to main page */
328         gosa_log ("User \"$username\" logged in successfully");
329         header ("Location: main.php?global_check=1");
330         exit;
331       }
332     }
333   }
336 /* Fill template with required values */
337 $smarty->assign ('date', gmdate("D, d M Y H:i:s"));
338 $smarty->assign ('username', $username);
339 $smarty->assign ('personal_img', get_template_path('images/personal.png'));
340 $smarty->assign ('password_img', get_template_path('images/password.png'));
341 $smarty->assign ('directory_img', get_template_path('images/ldapserver.png'));
343 /* Some error to display? */
344 if (!isset($message)){
345   $message= "";
348 $smarty->assign ("message", $message);
350 /* Displasy SSL mode warning? */
351 if ($ssl != "" && $config->data['MAIN']['WARNSSL'] == 'true'){
352   $smarty->assign ("ssl", "<b>"._("Warning").":<\/b> "._("Session will not be encrypted.")." <a style=\"color:red;\" href=\"$ssl\"><b>"._("Enter SSL session")."<\/b></a>!");
353 } else {
354   $smarty->assign ("ssl", "");
357 /* Translation of cookie-warning. Whether to display it, is determined by JavaScript */
358 $smarty->assign ("cookies", "<b>"._("Warning").":<\/b> "._("Your browser has cookies disabled. Please enable cookies and reload this page before logging in!"));
361 /* Generate server list */
362 $servers= array();
363 if (isset($_POST['server'])){
364   $selected= validate($_POST['server']);
365 } else {
366   $selected= $config->data['MAIN']['DEFAULT'];
368 foreach ($config->data['LOCATIONS'] as $key => $ignored){
369   $servers[$key]= $key;
371 $smarty->assign ("server_options", $servers);
372 $smarty->assign ("server_id", $selected);
374 /* show login screen */
375 $smarty->assign ("PHPSESSID", session_id());
376 if (isset($_SESSION['errors'])){
377   $smarty->assign("errors", $_SESSION['errors']);
379 if ($error_collector != ""){
380   $smarty->assign("php_errors", $error_collector."</div>");
381 } else {
382   $smarty->assign("php_errors", "");
385 displayLogin();
387 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
388 ?>
390 </body>
391 </html>