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= $_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->data['MAIN']['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= validate($_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->display (get_template_path('headers.tpl'));
94 $smarty->display(get_template_path('login.tpl'));
95 exit();
96 }
100 /*****************************************************************************
101 * M A I N *
102 *****************************************************************************/
104 /* Set error handler to own one, initialize time calculation
105 and start session. */
106 session::start();
107 session::set('errorsAlreadyPosted',array());
109 /* Destroy old session if exists.
110 Else you will get your old session back, if you not logged out correctly. */
111 if(is_array(session::get_all()) && count(session::get_all())){
112 session::destroy();
113 session::start();
114 }
116 $username= "";
118 /* Reset errors */
119 session::set('errors',"");
120 session::set('errorsAlreadyPosted',"");
121 session::set('LastError',"");
123 /* Check if we need to run setup */
124 if (!file_exists(CONFIG_DIR."/".CONFIG_FILE)){
125 header("location:setup.php");
126 exit();
127 }
129 /* Reset errors */
130 session::set('errors',"");
132 /* Check for java script */
133 if(isset($_POST['javascript']) && $_POST['javascript'] == "true") {
134 session::set('js',TRUE);
135 }elseif(isset($_POST['javascript'])) {
136 session::set('js',FALSE);
137 }
139 /* Check if gosa.conf (.CONFIG_FILE) is accessible */
140 if (!is_readable(CONFIG_DIR."/".CONFIG_FILE)){
141 msg_dialog::display(_("Configuration error"),sprintf(_("GOsa configuration %s/%s is not readable. Aborted."), CONFIG_DIR,CONFIG_FILE),FATAL_ERROR_DIALOG);
142 exit();
143 }
145 /* Parse configuration file */
146 $config= new config(CONFIG_DIR."/".CONFIG_FILE, $BASE_DIR);
147 session::set('DEBUGLEVEL',$config->data['MAIN']['DEBUGLEVEL']);
148 if ($_SERVER["REQUEST_METHOD"] != "POST"){
149 @DEBUG (DEBUG_CONFIG, __LINE__, __FUNCTION__, __FILE__, $config->data, "config");
150 }
152 /* Enable compressed output */
153 if (isset($config->data['MAIN']['COMPRESSED']) && preg_match('/^(true|on)$/i', $config->data['MAIN']['COMPRESSED'])){
154 ob_start("ob_gzhandler");
155 }
157 /* Set template compile directory */
158 if (isset ($config->data['MAIN']['COMPILE'])){
159 $smarty->compile_dir= $config->data['MAIN']['COMPILE'];
160 } else {
161 $smarty->compile_dir= '/var/spool/gosa';
162 }
164 /* Check for compile directory */
165 if (!(is_dir($smarty->compile_dir) && is_writable($smarty->compile_dir))){
166 msg_dialog::display(_("Smarty error"),sprintf(_("Directory '%s' specified as compile directory is not accessible!"),
167 $smarty->compile_dir),FATAL_ERROR_DIALOG);
168 exit();
169 }
171 /* Check for old files in compile directory */
172 clean_smarty_compile_dir($smarty->compile_dir);
174 /* Language setup */
175 $lang= get_browser_language();
176 putenv("LANGUAGE=");
177 putenv("LANG=$lang");
178 setlocale(LC_ALL, $lang);
179 $GLOBALS['t_language']= $lang;
180 $GLOBALS['t_gettext_message_dir'] = $BASE_DIR.'/locale/';
182 /* Set the text domain as 'messages' */
183 $domain = 'messages';
184 bindtextdomain($domain, LOCALE_DIR);
185 textdomain($domain);
186 $smarty->assign ('nextfield', 'username');
188 if ($_SERVER["REQUEST_METHOD"] != "POST"){
189 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, $lang, "Setting language to");
190 }
193 /* Check for SSL connection */
194 $ssl= "";
195 if (!isset($_SERVER['HTTPS']) ||
196 !stristr($_SERVER['HTTPS'], "on")) {
198 if (empty($_SERVER['REQUEST_URI'])) {
199 $ssl= "https://".$_SERVER['HTTP_HOST'].
200 $_SERVER['PATH_INFO'];
201 } else {
202 $ssl= "https://".$_SERVER['HTTP_HOST'].
203 $_SERVER['REQUEST_URI'];
204 }
205 }
207 /* If SSL is forced, just forward to the SSL enabled site */
208 if ($config->data['MAIN']['FORCESSL'] == 'true' && $ssl != ''){
209 header ("Location: $ssl");
210 exit;
211 }
213 /* Do we have htaccess authentification enabled? */
214 $htaccess_authenticated= FALSE;
215 if (isset($config->data['MAIN']['HTACCESS_AUTH']) && preg_match('/^(yes|true)$/i', $config->data['MAIN']['HTACCESS_AUTH'])){
216 if (!isset($_SERVER['REMOTE_USER'])){
217 msg_dialog::display(_("Configuration error"), _("There is a problem with the authentication setup!"), FATAL_ERROR_DIALOG);
218 exit;
219 }
221 $tmp= process_htaccess($_SERVER['REMOTE_USER'], isset($_SERVER['KRB5CCNAME']));
222 $username= $tmp['username'];
223 $server= $tmp['server'];
224 if ($username == ""){
225 msg_dialog::display(_("Error"), _("Cannot find a valid user for the current authentication setup!"), FATAL_ERROR_DIALOG);
226 exit;
227 }
228 if ($server == ""){
229 msg_dialog::display(_("Error"), _("User information is not unique accross the configured LDAP trees!"), FATAL_ERROR_DIALOG);
230 exit;
231 }
233 $htaccess_authenticated= TRUE;
234 }
236 /* Got a formular answer, validate and try to log in */
237 if (($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['login'])) || $htaccess_authenticated){
239 /* Reset error messages */
240 $message= "";
242 /* Destroy old sessions, they cause a successfull login to relog again ...*/
243 if(session::is_set('_LAST_PAGE_REQUEST')){
244 session::set('_LAST_PAGE_REQUEST',time());
245 }
247 if (!$htaccess_authenticated){
248 $server= validate($_POST["server"]);
249 }
250 $config->set_current($server);
252 /* Admin-logon and verify */
253 $ldap = $config->get_ldap_link();
254 if (is_null($ldap) || (is_int($ldap) && $ldap == 0)){
255 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
256 displayLogin();
257 exit();
258 }
260 /* Check for schema file presence */
261 if(!isset($config->data['MAIN']['SCHEMA_CHECK'])){
262 $config->data['MAIN']['SCHEMA_CHECK'] = "true";
263 }
264 if(isset($config->data['MAIN']['SCHEMA_CHECK'])&&preg_match("/true/i",$config->data['MAIN']['SCHEMA_CHECK'])){
265 $recursive = (isset($config->current['RECURSIVE']) && $config->current['RECURSIVE'] == "true");
266 $tls = (isset($config->current['TLS']) && $config->current['TLS'] == "true");
268 if(!count($ldap->get_objectclasses())){
269 msg_dialog::display(_("LDAP error"), _("Cannot detect information about the installed LDAP schema!"), ERROR_DIALOG);
270 displayLogin();
271 exit() ;
272 }else{
273 $cfg = array();
274 $cfg['admin'] = $config->current['ADMIN'];
275 $cfg['password'] = $config->current['PASSWORD'];
276 $cfg['connection']= $config->current['SERVER'];
277 $cfg['tls'] = $tls;
278 $str = check_schema($cfg,isset($config->current['RFC2307BIS']) && preg_match("/(true|yes|on|1)/i",$config->current['RFC2307BIS']));
279 $checkarr = array();
280 foreach($str as $tr){
281 if(isset($tr['IS_MUST_HAVE']) && !$tr['STATUS']){
282 msg_dialog::display(_("LDAP error"), _("Your LDAP setup contains old schema definitions:")."<br><br><i>".$tr['MSG']."</i>", ERROR_DIALOG);
283 displayLogin();
284 exit();
285 }
286 }
287 }
288 }
290 /* Check for locking area */
291 $ldap->cat($config->current['CONFIG'], array("dn"));
292 $attrs= $ldap->fetch();
293 if (!count ($attrs)){
294 $ldap->cd($config->current['BASE']);
295 $ldap->create_missing_trees($config->current['CONFIG']);
296 }
298 /* Check for valid input */
299 $ok= true;
300 if (!$htaccess_authenticated){
301 $username= $_POST["username"];
302 if (!ereg("^[@A-Za-z0-9_.-]+$", $username)){
303 $message= _("Please specify a valid username!");
304 $ok= false;
305 } elseif (mb_strlen($_POST["password"], 'UTF-8') == 0){
306 $message= _("Please specify your password!");
307 $smarty->assign ('nextfield', 'password');
308 $ok= false;
309 }
310 }
312 if ($ok) {
314 /* Login as user, initialize user ACL's */
315 if ($htaccess_authenticated){
316 $ui= ldap_login_user_htaccess($username);
317 if ($ui === NULL || !$ui){
318 msg_dialog::display(_("Authentication error"), _("Cannot retrieve user information for htaccess authentication!"), FATAL_ERROR_DIALOG);
319 exit;
320 }
321 } else {
322 $ui= ldap_login_user($username, $_POST["password"]);
323 }
324 if ($ui === NULL || !$ui){
325 $message= _("Please check the username/password combination.");
326 $smarty->assign ('nextfield', 'password');
327 new log("security","login","",array(),"Authentication failed for user \"$username\"") ;
328 } else {
329 /* Remove all locks of this user */
330 del_user_locks($ui->dn);
332 /* Save userinfo and plugin structure */
333 session::set('ui',$ui);
334 session::set('session_cnt',0);
336 /* Let GOsa trigger a new connection for each POST, save
337 config to session. */
338 $config->get_departments();
339 $config->make_idepartments();
340 session::set('config',$config);
342 /* Restore filter settings from cookie, if available */
343 if(isset($config->data['MAIN']['SAVE_FILTER']) && preg_match("/true/",$config->data['MAIN']['SAVE_FILTER'])){
345 if(isset($_COOKIE['GOsa_Filter_Settings']) || isset($HTTP_COOKIE_VARS['GOsa_Filter_Settings'])){
347 if(isset($_COOKIE['GOsa_Filter_Settings'])){
348 $cookie_all = unserialize(base64_decode($_COOKIE['GOsa_Filter_Settings']));
349 }else{
350 $cookie_all = unserialize(base64_decode($HTTP_COOKIE_VARS['GOsa_Filter_Settings']));
351 }
352 if(isset($cookie_all[$ui->dn])){
353 $cookie = $cookie_all[$ui->dn];
354 $cookie_vars= array("MultiDialogFilters","CurrentMainBase","plug");
355 foreach($cookie_vars as $var){
356 if(isset($cookie[$var])){
357 session::set($var,$cookie[$var]);
358 }
359 }
360 if(isset($cookie['plug'])){
361 $plug =$cookie['plug'];
362 }
363 }
364 }
365 }
367 /* are we using accountexpiration */
368 if((isset($config->data['MAIN']['ACCOUNT_EXPIRATION'])) &&
369 preg_match('/true/i', $config->data['MAIN']['ACCOUNT_EXPIRATION'])){
371 $expired= ldap_expired_account($config, $ui->dn, $ui->username);
373 if ($expired == 1){
374 $message= _("Account locked. Please contact your system administrator!");
375 $smarty->assign ('nextfield', 'password');
376 new log("security","login","",array(),"Account for user \"$username\" has expired") ;
377 } elseif ($expired == 3){
378 $plist= new pluglist($config, $ui);
379 foreach ($plist->dirlist as $key => $value){
380 if (preg_match("/\bpassword\b/i",$value)){
381 $plug=$key;
382 new log("security","login","",array(),"User \"$username\" password forced to change") ;
383 header ("Location: main.php?plug=$plug&reset=1");
384 exit;
385 }
386 }
387 }
388 }
390 /* Not account expired or password forced change go to main page */
391 new log("security","login","",array(),"User \"$username\" logged in successfully") ;
392 $plist= new pluglist($config, $ui);
393 if(isset($plug) && isset($plist->dirlist[$plug])){
394 header ("Location: main.php?plug=".$plug."&global_check=1");
395 }else{
396 header ("Location: main.php?global_check=1");
397 }
398 exit;
399 }
400 }
401 }
403 /* Fill template with required values */
404 $smarty->assign ('date', gmdate("D, d M Y H:i:s"));
405 $smarty->assign ('username', $username);
406 $smarty->assign ('personal_img', get_template_path('images/login-head.png'));
407 $smarty->assign ('password_img', get_template_path('images/password.png'));
408 $smarty->assign ('directory_img', get_template_path('images/ldapserver.png'));
410 /* Some error to display? */
411 if (!isset($message)){
412 $message= "";
413 }
415 $smarty->assign ("message", $message);
417 /* Displasy SSL mode warning? */
418 if ($ssl != "" && $config->data['MAIN']['WARNSSL'] == 'true'){
419 $smarty->assign ("ssl", "<b>"._("Warning").":<\/b> "._("Session will not be encrypted.")." <a style=\"color:red;\" href=\"$ssl\"><b>"._("Enter SSL session")."<\/b></a>!");
420 } else {
421 $smarty->assign ("ssl", "");
422 }
424 /* Translation of cookie-warning. Whether to display it, is determined by JavaScript */
425 $smarty->assign ("cookies", "<b>"._("Warning").":<\/b> "._("Your browser has cookies disabled. Please enable cookies and reload this page before logging in!"));
427 /* Generate server list */
428 $servers= array();
429 if (isset($_POST['server'])){
430 $selected= validate($_POST['server']);
431 } else {
432 $selected= $config->data['MAIN']['DEFAULT'];
433 }
434 foreach ($config->data['LOCATIONS'] as $key => $ignored){
435 $servers[$key]= $key;
436 }
437 $smarty->assign ("server_options", $servers);
438 $smarty->assign ("server_id", $selected);
440 /* show login screen */
441 $smarty->assign ("PHPSESSID", session_id());
442 if (session::is_set('errors')){
443 $smarty->assign("errors", session::get('errors'));
444 }
445 if ($error_collector != ""){
446 $smarty->assign("php_errors", preg_replace("/%BUGBODY%/",$error_collector_mailto,$error_collector)."</div>");
447 } else {
448 $smarty->assign("php_errors", "");
449 }
451 /* Set focus to the error button if we've an error message */
452 $focus= "";
453 if (session::is_set('errors') && session::get('errors') != ""){
454 $focus= '<script language="JavaScript" type="text/javascript">';
455 $focus.= 'document.forms[0].error_accept.focus();';
456 $focus.= '</script>';
457 }
458 $smarty->assign("focus", $focus);
459 displayLogin();
461 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
462 ?>
464 </body>
465 </html>