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