Code

fixed lhs role : error while saving, skip saving if roles.conf could not be read
[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     }
35     $smarty->assign ('date', gmdate("D, d M Y H:i:s"));
36     $smarty->assign ('username', $username);
37     $smarty->assign ('personal_img', get_template_path('images/personal.png'));
38     $smarty->assign ('password_img', get_template_path('images/password.png'));
39     $smarty->assign ('directory_img', get_template_path('images/ldapserver.png'));
41     /* Some error to display? */
42     if (!isset($message)){
43       $message= "";
44     }
45     $smarty->assign ("message", $message);
47     /* Displasy SSL mode warning? */
48     if ($ssl != "" && $config->data['MAIN']['WARNSSL'] == 'true'){
49       $smarty->assign ("ssl", "<b>"._("Warning").":<\/b> "._("Session will not be encrypted.")." <a style=\"color:red;\" href=\"$ssl\"><b>"._("Enter SSL session")."<\/b></a>!");
50     } else {
51       $smarty->assign ("ssl", "");
52     }
54     /* Generate server list */
55     $servers= array();
56     if (isset($_POST['server'])){
57       $selected= validate($_POST['server']);
58     } else {
59       $selected= $config->data['MAIN']['DEFAULT'];
60     }
61     foreach ($config->data['LOCATIONS'] as $key => $ignored){
62       $servers[$key]= $key;
63     }
64     $smarty->assign ("server_options", $servers);
65     $smarty->assign ("server_id", $selected);
67     /* show login screen */
68     $smarty->assign ("PHPSESSID", session_id());
69     if (isset($_SESSION['errors'])){
70       $smarty->assign("errors", $_SESSION['errors']);
71     }
72     if ($error_collector != ""){
73       $smarty->assign("php_errors", $error_collector."</div>");
74     } else {
75       $smarty->assign("php_errors", "");
76     }
78     $smarty->display(get_template_path('headers.tpl'));
79     $smarty->display(get_template_path('login.tpl'));
80     exit();
81 }
83 session_start();
85 /* Destroy old session if exists.
86     Else you will get your old session back, if you not logged out correctly. */
87 if(is_array($_SESSION) && count($_SESSION)){
88   session_destroy();
89   session_start();
90 }
92 $username= "";
94 /* Reset errors */
95 $_SESSION['errors']             = "";
96 $_SESSION['errorsAlreadyPosted']= array();
97 $_SESSION['LastError']          = "";
99 /* Check if we need to run setup */
100 if (!file_exists(CONFIG_DIR."/gosa.conf")){
101   header("location:setup.php");
102   exit();
105 /* Reset errors */
106 $_SESSION['errors']= "";
108 /* Check for java script */
109 if(isset($_POST['javascript']) && $_POST['javascript'] == "true") {
110   $_SESSION['js']= TRUE;
111 }elseif(isset($_POST['javascript'])) {
112   $_SESSION['js']= FALSE;
115 /* Check if gosa.conf is accessible */
116 if (!is_readable(CONFIG_DIR."/gosa.conf")){
117   echo sprintf(_("GOsa configuration %s/gosa.conf is not readable. Aborted."), CONFIG_DIR);
118   exit();
121 /* Parse configuration file */
122 $config= new config(CONFIG_DIR."/gosa.conf", $BASE_DIR);
123 $_SESSION['DEBUGLEVEL']= $config->data['MAIN']['DEBUGLEVEL'];
124 if ($_SERVER["REQUEST_METHOD"] != "POST"){
125   @DEBUG (DEBUG_CONFIG, __LINE__, __FUNCTION__, __FILE__, $config->data, "config");
128 /* Set template compile directory */
129 if (isset ($config->data['MAIN']['COMPILE'])){
130   $smarty->compile_dir= $config->data['MAIN']['COMPILE'];
131 } else {
132   $smarty->compile_dir= '/var/spool/gosa';
134 $smarty->assign ('nextfield', 'username');
136 /* Check for compile directory */
137 if (!(is_dir($smarty->compile_dir) && is_writable($smarty->compile_dir))){
138   echo sprintf(_("Directory '%s' specified as compile directory is not accessible!"),
139         $smarty->compile_dir);
140   exit();
143 /* Check for old files in compile directory */
144 clean_smarty_compile_dir($smarty->compile_dir);
146 /* Language setup */
147 if ($config->data['MAIN']['LANG'] == ""){
148   $lang= get_browser_language();
149 } else {
150   $lang= $config->data['MAIN']['LANG'];
152 $lang.=".UTF-8";
153 putenv("LANGUAGE=");
154 putenv("LANG=$lang");
155 setlocale(LC_ALL, $lang);
156 $GLOBALS['t_language']= $lang;
157 $GLOBALS['t_gettext_message_dir'] = $BASE_DIR.'/locale/';
159 /* Set the text domain as 'messages' */
160 $domain = 'messages';
161 bindtextdomain($domain, "$BASE_DIR/locale");
162 textdomain($domain);
165 if ($_SERVER["REQUEST_METHOD"] != "POST"){
166   @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, $lang, "Setting language to");
170 /* Check for SSL connection */
171 $ssl= "";
172 if (!isset($_SERVER['HTTPS']) ||
173     !stristr($_SERVER['HTTPS'], "on")) {
175   if (empty($_SERVER['REQUEST_URI'])) {
176     $ssl= "https://".$_SERVER['HTTP_HOST'].
177       $_SERVER['PATH_INFO'];
178   } else {
179     $ssl= "https://".$_SERVER['HTTP_HOST'].
180       $_SERVER['REQUEST_URI'];
181   }
184 /* If SSL is forced, just forward to the SSL enabled site */
185 if ($config->data['MAIN']['FORCESSL'] == 'true' && $ssl != ''){
186   header ("Location: $ssl");
187   exit;
190 /* Got a formular answer, validate and try to log in */
191 if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['login'])){
193   /* Reset error messages */
194   $message= "";
196   /* Destroy old sessions, they cause a successfull login to relog again ...*/
197   if(isset($_SESSION['_LAST_PAGE_REQUEST'])){
198     $_SESSION['_LAST_PAGE_REQUEST'] = time();
199   }
201   $server= validate($_POST["server"]);
202   $config->set_current($server);
204   /* Admin-logon and verify */
205   $ldap = $config->get_ldap_link();
206   if (is_null($ldap) || (is_int($ldap) && $ldap == 0)){
207     print_red (_("Can't bind to LDAP. Please contact the system administrator."));
208     displayLogin();
209     exit();
210   }
212   /* Check for schema file presence */
213   if(!isset($config->data['MAIN']['SCHEMA_CHECK'])){
214     $config->data['MAIN']['SCHEMA_CHECK'] = "true";
215   }
216   if(isset($config->data['MAIN']['SCHEMA_CHECK'])&&preg_match("/true/i",$config->data['MAIN']['SCHEMA_CHECK'])){
217     require_once("functions_setup.inc");
218     if(!is_schema_readable($config->current['SERVER'],$config->current['ADMIN'],$config->current['PASSWORD'])){
219       print_red(_("GOsa cannot retrieve information about the installed schema files. Please make sure, that this is possible."));
220       $smarty->display(get_template_path('headers.tpl'));
221       echo "<body>".$_SESSION['errors']."</body></html>";
222       exit();
223     }else{
224       $str = (schema_check($config->current['SERVER'],$config->current['ADMIN'],$config->current['PASSWORD'],0,TRUE));
225       $checkarr = array();
226       foreach($str as $tr){
227         if(isset($tr['needonstartup'])){
228           print_red($tr['msg']."<br>"._("Your ldap setup contains old schema definitions. Please re-run the setup."));
229           $smarty->display(get_template_path('headers.tpl'));
230           echo "<body>".$_SESSION['errors']."</body></html>";
231           exit();
232         }
233       }
234     }
235   }
236   /* Check for locking area */
237   $ldap->cat($config->current['CONFIG'], array("dn"));
238   $attrs= $ldap->fetch();
239   if (!count ($attrs)){
240     $ldap->cd($config->current['BASE']);
241     $ldap->create_missing_trees($config->current['CONFIG']);
242   }
244   /* Check for at least one subtreeACL in the complete tree */
245   $ldap->cd($config->current['BASE']);
246   $ldap->search("(&(objectClass=gosaObject)(gosaSubtreeACL=:all))");
247   if ($ldap->count() < 1){
248     print_red(_("You're missing an administrative account for GOsa, you'll not be able to administrate anything!"));
249     displayLogin();
250     exit();
251   }
253   /* Check for valid input */
254   $username= $_POST["username"];
255   if (!ereg("^[A-Za-z0-9_.-]+$", $username)){
256     $message= _("Please specify a valid username!");
257   } elseif (mb_strlen($_POST["password"], 'UTF-8') == 0){
258     $message= _("Please specify your password!");
259     $smarty->assign ('nextfield', 'password');
260   } else {
262     /* Login as user, initialize user ACL's */
263     $ui= ldap_login_user($username, $_POST["password"]);
264     if ($ui === NULL || $ui == 0){
265       $message= _("Please check the username/password combination.");
266       $smarty->assign ('nextfield', 'password');
267       gosa_log ("Authentication failed for user \"$username\"");
268     } else {
269       /* Remove all locks of this user */
270       del_user_locks($ui->dn);
272       /* Save userinfo and plugin structure */
273       $_SESSION['ui']= $ui;
274       $_SESSION['session_cnt']= 0;
276       /* Let GOsa trigger a new connection for each POST, save
277          config to session. */
278       $config->get_departments();
279       $config->make_idepartments();
280       $_SESSION['config']= $config;
282       /* are we using accountexpiration */
283       if((isset($config->data['MAIN']['ACCOUNT_EXPIRATION'])) && 
284           preg_match('/true/i', $config->data['MAIN']['ACCOUNT_EXPIRATION'])){
285       
286         $expired= ldap_expired_account($config, $ui->dn, $ui->username);
288         if ($expired == 1){
289           $message= _("Account locked. Please contact your system administrator.");
290           $smarty->assign ('nextfield', 'password');
291           gosa_log ("Account for user \"$username\" has expired");
292         } elseif ($expired == 3){
293             $plist= new pluglist($config, $ui);
294             foreach ($plist->dirlist as $key => $value){
295               if (preg_match("/\bpassword\b/i",$value)){
296                 $plug=$key;
297                 gosa_log ("User \"$username\" password forced to change");
298                 header ("Location: main.php?plug=$plug&reset=1");
299                 exit;
300               }
301             }
302           }
304         /* Not account expired or password forced change go to main page */
305         gosa_log ("User \"$username\" logged in successfully");
306         header ("Location: main.php?global_check=1");
307         exit;
308         
309       } else {
310         /* Go to main page */
311         gosa_log ("User \"$username\" logged in successfully");
312         header ("Location: main.php?global_check=1");
313         exit;
314       }
315     }
316   }
319 /* Fill template with required values */
320 $smarty->assign ('date', gmdate("D, d M Y H:i:s"));
321 $smarty->assign ('username', $username);
322 $smarty->assign ('personal_img', get_template_path('images/personal.png'));
323 $smarty->assign ('password_img', get_template_path('images/password.png'));
324 $smarty->assign ('directory_img', get_template_path('images/ldapserver.png'));
326 /* Some error to display? */
327 if (!isset($message)){
328   $message= "";
331 $smarty->assign ("message", $message);
333 /* Displasy SSL mode warning? */
334 if ($ssl != "" && $config->data['MAIN']['WARNSSL'] == 'true'){
335   $smarty->assign ("ssl", "<b>"._("Warning").":<\/b> "._("Session will not be encrypted.")." <a style=\"color:red;\" href=\"$ssl\"><b>"._("Enter SSL session")."<\/b></a>!");
336 } else {
337   $smarty->assign ("ssl", "");
340 /* Translation of cookie-warning. Whether to display it, is determined by JavaScript */
341 $smarty->assign ("cookies", "<b>"._("Warning").":<\/b> "._("Your browser has cookies disabled. Please enable cookies and reload this page before logging in!"));
344 /* Generate server list */
345 $servers= array();
346 if (isset($_POST['server'])){
347   $selected= validate($_POST['server']);
348 } else {
349   $selected= $config->data['MAIN']['DEFAULT'];
351 foreach ($config->data['LOCATIONS'] as $key => $ignored){
352   $servers[$key]= $key;
354 $smarty->assign ("server_options", $servers);
355 $smarty->assign ("server_id", $selected);
357 /* show login screen */
358 $smarty->assign ("PHPSESSID", session_id());
359 if (isset($_SESSION['errors'])){
360   $smarty->assign("errors", $_SESSION['errors']);
362 if ($error_collector != ""){
363   $smarty->assign("php_errors", $error_collector."</div>");
364 } else {
365   $smarty->assign("php_errors", "");
367 displayLogin();
370 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
371 ?>
373 </body>
374 </html>