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 | E_STRICT);
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 if(!$config->check_session_lifetime()){
56 $smarty->assign ("lifetime", _("Warning").": ".
57 _("The session lifetime configured in your gosa.conf will be overridden by php.ini settings."));
58 }else{
59 $smarty->assign ("lifetime", "");
60 }
62 /* Generate server list */
63 $servers= array();
64 if (isset($_POST['server'])){
65 $selected= validate($_POST['server']);
66 } else {
67 $selected= $config->data['MAIN']['DEFAULT'];
68 }
69 foreach ($config->data['LOCATIONS'] as $key => $ignored){
70 $servers[$key]= $key;
71 }
72 $smarty->assign ("server_options", $servers);
73 $smarty->assign ("server_id", $selected);
75 /* show login screen */
76 $smarty->assign ("PHPSESSID", session_id());
77 if (isset($_SESSION['errors'])){
78 $smarty->assign("errors", $_SESSION['errors']);
79 }
80 if ($error_collector != ""){
81 $smarty->assign("php_errors", $error_collector."</div>");
82 } else {
83 $smarty->assign("php_errors", "");
84 }
86 $smarty->display (get_template_path('headers.tpl'));
87 $smarty->display(get_template_path('login.tpl'));
88 exit();
89 }
93 /* Set error handler to own one, initialize time calculation
94 and start session. */
95 session_start ();
97 /* Destroy old session if exists.
98 Else you will get your old session back, if you not logged out correctly. */
99 if(is_array($_SESSION) && count($_SESSION)){
100 session_destroy();
101 session_start();
102 }
104 $username= "";
106 /* Reset errors */
107 $_SESSION['errors'] = "";
108 $_SESSION['errorsAlreadyPosted']= array();
109 $_SESSION['LastError'] = "";
111 /* Check if we need to run setup */
112 if (!file_exists(CONFIG_DIR."/".CONFIG_FILE)){
113 header("location:setup.php");
114 exit();
115 }
117 /* Reset errors */
118 $_SESSION['errors']= "";
120 /* Check for java script */
121 if(isset($_POST['javascript']) && $_POST['javascript'] == "true") {
122 $_SESSION['js']= TRUE;
123 }elseif(isset($_POST['javascript'])) {
124 $_SESSION['js']= FALSE;
125 }
127 /* Check if gosa.conf (.CONFIG_FILE) is accessible */
128 if (!is_readable(CONFIG_DIR."/".CONFIG_FILE)){
129 msg_dialog::display(_("Configuration accessibility"),sprintf(_("GOsa configuration %s/%s is not readable. Aborted."), CONFIG_DIR,CONFIG_FILE),FATAL_ERROR_DIALOG);
130 exit();
131 }
133 /* Parse configuration file */
134 $config= new config(CONFIG_DIR."/".CONFIG_FILE, $BASE_DIR);
135 $_SESSION['DEBUGLEVEL']= $config->data['MAIN']['DEBUGLEVEL'];
136 if ($_SERVER["REQUEST_METHOD"] != "POST"){
137 @DEBUG (DEBUG_CONFIG, __LINE__, __FUNCTION__, __FILE__, $config->data, "config");
138 }
140 /* Enable compressed output */
141 if (isset($config->data['MAIN']['COMPRESSED']) && preg_match('/^(true|on)$/i', $config->data['MAIN']['COMPRESSED'])){
142 ob_start("ob_gzhandler");
143 }
145 /* Set template compile directory */
146 if (isset ($config->data['MAIN']['COMPILE'])){
147 $smarty->compile_dir= $config->data['MAIN']['COMPILE'];
148 } else {
149 $smarty->compile_dir= '/var/spool/gosa';
150 }
152 /* Check for compile directory */
153 if (!(is_dir($smarty->compile_dir) && is_writable($smarty->compile_dir))){
154 msg_dialog::display(_("Smarty"),sprintf(_("Directory '%s' specified as compile directory is not accessible!"),
155 $smarty->compile_dir),FATAL_ERROR_DIALOG);
156 exit();
157 }
159 /* Check for old files in compile directory */
160 clean_smarty_compile_dir($smarty->compile_dir);
162 /* Language setup */
163 $lang= get_browser_language();
164 putenv("LANGUAGE=");
165 putenv("LANG=$lang");
166 setlocale(LC_ALL, $lang);
167 $GLOBALS['t_language']= $lang;
168 $GLOBALS['t_gettext_message_dir'] = $BASE_DIR.'/locale/';
170 /* Set the text domain as 'messages' */
171 $domain = 'messages';
172 bindtextdomain($domain, "$BASE_DIR/locale");
173 textdomain($domain);
174 $smarty->assign ('nextfield', 'username');
176 if ($_SERVER["REQUEST_METHOD"] != "POST"){
177 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, $lang, "Setting language to");
178 }
181 /* Check for SSL connection */
182 $ssl= "";
183 if (!isset($_SERVER['HTTPS']) ||
184 !stristr($_SERVER['HTTPS'], "on")) {
186 if (empty($_SERVER['REQUEST_URI'])) {
187 $ssl= "https://".$_SERVER['HTTP_HOST'].
188 $_SERVER['PATH_INFO'];
189 } else {
190 $ssl= "https://".$_SERVER['HTTP_HOST'].
191 $_SERVER['REQUEST_URI'];
192 }
193 }
195 /* If SSL is forced, just forward to the SSL enabled site */
196 if ($config->data['MAIN']['FORCESSL'] == 'true' && $ssl != ''){
197 header ("Location: $ssl");
198 exit;
199 }
201 /* Do we have htaccess authentification enabled? */
202 $htaccess_authenticated= FALSE;
203 if (isset($config->data['MAIN']['HTACCESS_AUTH']) && preg_match('/^(yes|true)$/i', $config->data['MAIN']['HTACCESS_AUTH'])){
204 if (!isset($_SERVER['REMOTE_USER'])){
205 print_red(_("There is a problem with the authentication setup. Please inform your system administrator."));
206 display_error_page();
207 }
209 $tmp= process_htaccess($_SERVER['REMOTE_USER'], isset($_SERVER['KRB5CCNAME']));
210 $username= $tmp['username'];
211 $server= $tmp['server'];
212 if ($username == ""){
213 print_red(_("Cannot find a valid user for the current authentication setup."));
214 display_error_page();
215 }
216 if ($server == ""){
217 print_red(_("User information is not uniq accross the configured directories. Cannot authenticated."));
218 display_error_page();
219 }
221 $htaccess_authenticated= TRUE;
222 }
224 /* Got a formular answer, validate and try to log in */
225 if (($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['login'])) || $htaccess_authenticated){
227 /* Reset error messages */
228 $message= "";
230 /* Destroy old sessions, they cause a successfull login to relog again ...*/
231 if(isset($_SESSION['_LAST_PAGE_REQUEST'])){
232 $_SESSION['_LAST_PAGE_REQUEST'] = time();
233 }
235 if (!$htaccess_authenticated){
236 $server= validate($_POST["server"]);
237 }
238 $config->set_current($server);
240 /* Admin-logon and verify */
241 $ldap = $config->get_ldap_link();
242 if (is_null($ldap) || (is_int($ldap) && $ldap == 0)){
243 print_red (_("Can't bind to LDAP. Please contact the system administrator."));
244 displayLogin();
245 exit();
246 }
248 /* Check for schema file presence */
249 if(!isset($config->data['MAIN']['SCHEMA_CHECK'])){
250 $config->data['MAIN']['SCHEMA_CHECK'] = "true";
251 }
252 if(isset($config->data['MAIN']['SCHEMA_CHECK'])&&preg_match("/true/i",$config->data['MAIN']['SCHEMA_CHECK'])){
253 $recursive = (isset($config->current['RECURSIVE']) && $config->current['RECURSIVE'] == "true");
254 $tls = (isset($config->current['TLS']) && $config->current['TLS'] == "true");
256 if(!count($ldap->get_objectclasses())){
257 print_red(_("GOsa cannot retrieve information about the installed schema files. Please make sure, that this is possible."));
258 displayLogin();
259 exit() ;
260 }else{
261 $cfg = array();
262 $cfg['admin'] = $config->current['ADMIN'];
263 $cfg['password'] = $config->current['PASSWORD'];
264 $cfg['connection']= $config->current['SERVER'];
265 $cfg['tls'] = $tls;
266 $str = check_schema($cfg,isset($config->current['RFC2307BIS']) && preg_match("/(true|yes|on|1)/i",$config->current['RFC2307BIS']));
267 $checkarr = array();
268 foreach($str as $tr){
269 if(isset($tr['IS_MUST_HAVE']) && !$tr['STATUS']){
270 print_red($tr['MSG']."<br>"._("Your ldap setup contains old schema definitions. Please re-run the setup."));
271 displayLogin();
272 exit();
273 }
274 }
275 }
276 }
278 /* Check for locking area */
279 $ldap->cat($config->current['CONFIG'], array("dn"));
280 $attrs= $ldap->fetch();
281 if (!count ($attrs)){
282 $ldap->cd($config->current['BASE']);
283 $ldap->create_missing_trees($config->current['CONFIG']);
284 }
286 /* Check for valid input */
287 $ok= true;
288 if (!$htaccess_authenticated){
289 $username= $_POST["username"];
290 if (!ereg("^[@A-Za-z0-9_.-]+$", $username)){
291 $message= _("Please specify a valid username!");
292 $ok= false;
293 } elseif (mb_strlen($_POST["password"], 'UTF-8') == 0){
294 $message= _("Please specify your password!");
295 $smarty->assign ('nextfield', 'password');
296 $ok= false;
297 }
298 }
300 if ($ok) {
302 /* Login as user, initialize user ACL's */
303 if ($htaccess_authenticated){
304 $ui= ldap_login_user_htaccess($username);
305 if ($ui === NULL || !$ui){
306 print_red(_("Authentication via htaccess not possible. Unable to retrieve user information."));
307 display_error_page();
308 }
309 } else {
310 $ui= ldap_login_user($username, $_POST["password"]);
311 }
312 if ($ui === NULL || !$ui){
313 $message= _("Please check the username/password combination.");
314 $smarty->assign ('nextfield', 'password');
315 new log("security","login","",array(),"Authentication failed for user \"$username\"") ;
316 } else {
317 /* Remove all locks of this user */
318 del_user_locks($ui->dn);
320 /* Save userinfo and plugin structure */
321 $_SESSION['ui']= $ui;
322 $_SESSION['session_cnt']= 0;
324 /* Let GOsa trigger a new connection for each POST, save
325 config to session. */
326 $config->get_departments();
327 $config->make_idepartments();
328 $_SESSION['config']= $config;
330 /* Restore filter settings from cookie, if available */
331 if(isset($config->data['MAIN']['SAVE_FILTER']) && preg_match("/true/",$config->data['MAIN']['SAVE_FILTER'])){
333 if(isset($_COOKIE['GOsa_Filter_Settings']) || isset($HTTP_COOKIE_VARS['GOsa_Filter_Settings'])){
335 if(isset($_COOKIE['GOsa_Filter_Settings'])){
336 $cookie_all = unserialize(base64_decode($_COOKIE['GOsa_Filter_Settings']));
337 }else{
338 $cookie_all = unserialize(base64_decode($HTTP_COOKIE_VARS['GOsa_Filter_Settings']));
339 }
340 if(isset($cookie_all[$ui->dn])){
341 $cookie = $cookie_all[$ui->dn];
342 $cookie_vars= array("MultiDialogFilters","CurrentMainBase","plug");
343 foreach($cookie_vars as $var){
344 if(isset($cookie[$var])){
345 $_SESSION[$var] = $cookie[$var];
346 }
347 }
348 if(isset($cookie['plug'])){
349 $plug =$cookie['plug'];
350 }
351 }
352 }
353 }
355 /* are we using accountexpiration */
356 if((isset($config->data['MAIN']['ACCOUNT_EXPIRATION'])) &&
357 preg_match('/true/i', $config->data['MAIN']['ACCOUNT_EXPIRATION'])){
359 $expired= ldap_expired_account($config, $ui->dn, $ui->username);
361 if ($expired == 1){
362 $message= _("Account locked. Please contact your system administrator.");
363 $smarty->assign ('nextfield', 'password');
364 new log("security","login","",array(),"Account for user \"$username\" has expired") ;
365 } elseif ($expired == 3){
366 $plist= new pluglist($config, $ui);
367 foreach ($plist->dirlist as $key => $value){
368 if (preg_match("/\bpassword\b/i",$value)){
369 $plug=$key;
370 new log("security","login","",array(),"User \"$username\" password forced to change") ;
371 header ("Location: main.php?plug=$plug&reset=1");
372 exit;
373 }
374 }
375 }
376 }
378 /* Not account expired or password forced change go to main page */
379 new log("security","login","",array(),"User \"$username\" logged in successfully") ;
380 $plist= new pluglist($config, $ui);
381 if(isset($plug) && isset($plist->dirlist[$plug])){
382 header ("Location: main.php?plug=".$plug."&global_check=1");
383 }else{
384 header ("Location: main.php?global_check=1");
385 }
386 exit;
387 }
388 }
389 }
391 /* Fill template with required values */
392 $smarty->assign ('date', gmdate("D, d M Y H:i:s"));
393 $smarty->assign ('username', $username);
394 $smarty->assign ('personal_img', get_template_path('images/personal.png'));
395 $smarty->assign ('password_img', get_template_path('images/password.png'));
396 $smarty->assign ('directory_img', get_template_path('images/ldapserver.png'));
398 /* Some error to display? */
399 if (!isset($message)){
400 $message= "";
401 }
403 $smarty->assign ("message", $message);
405 /* Displasy SSL mode warning? */
406 if ($ssl != "" && $config->data['MAIN']['WARNSSL'] == 'true'){
407 $smarty->assign ("ssl", "<b>"._("Warning").":<\/b> "._("Session will not be encrypted.")." <a style=\"color:red;\" href=\"$ssl\"><b>"._("Enter SSL session")."<\/b></a>!");
408 } else {
409 $smarty->assign ("ssl", "");
410 }
412 /* Translation of cookie-warning. Whether to display it, is determined by JavaScript */
413 $smarty->assign ("cookies", "<b>"._("Warning").":<\/b> "._("Your browser has cookies disabled. Please enable cookies and reload this page before logging in!"));
416 /* Generate server list */
417 $servers= array();
418 if (isset($_POST['server'])){
419 $selected= validate($_POST['server']);
420 } else {
421 $selected= $config->data['MAIN']['DEFAULT'];
422 }
423 foreach ($config->data['LOCATIONS'] as $key => $ignored){
424 $servers[$key]= $key;
425 }
426 $smarty->assign ("server_options", $servers);
427 $smarty->assign ("server_id", $selected);
429 /* show login screen */
430 $smarty->assign ("PHPSESSID", session_id());
431 if (isset($_SESSION['errors'])){
432 $smarty->assign("errors", $_SESSION['errors']);
433 }
434 if ($error_collector != ""){
435 $smarty->assign("php_errors", preg_replace("/%BUGBODY%/",$error_collector_mailto,$error_collector)."</div>");
436 } else {
437 $smarty->assign("php_errors", "");
438 }
440 /* Set focus to the error button if we've an error message */
441 $focus= "";
442 if (isset($_SESSION['errors']) && $_SESSION['errors'] != ""){
443 $focus= '<script language="JavaScript" type="text/javascript">';
444 $focus.= 'document.forms[0].error_accept.focus();';
445 $focus.= '</script>';
446 }
447 $smarty->assign("focus", $focus);
448 displayLogin();
450 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
451 ?>
453 </body>
454 </html>