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();
109 }
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;
116 }
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();
122 }
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");
129 }
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';
136 }
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();
144 }
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'];
154 }
155 putenv("LANGUAGE=");
156 putenv("LANG=$lang");
157 setlocale(LC_ALL, $lang);
158 $GLOBALS['t_language']= $lang;
159 $GLOBALS['t_gettext_message_dir'] = $BASE_DIR.'/locale/';
161 /* Set the text domain as 'messages' */
162 $domain = 'messages';
163 bindtextdomain($domain, "$BASE_DIR/locale");
164 textdomain($domain);
167 if ($_SERVER["REQUEST_METHOD"] != "POST"){
168 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, $lang, "Setting language to");
169 }
172 /* Check for SSL connection */
173 $ssl= "";
174 if (!isset($_SERVER['HTTPS']) ||
175 !stristr($_SERVER['HTTPS'], "on")) {
177 if (empty($_SERVER['REQUEST_URI'])) {
178 $ssl= "https://".$_SERVER['HTTP_HOST'].
179 $_SERVER['PATH_INFO'];
180 } else {
181 $ssl= "https://".$_SERVER['HTTP_HOST'].
182 $_SERVER['REQUEST_URI'];
183 }
184 }
186 /* If SSL is forced, just forward to the SSL enabled site */
187 if ($config->data['MAIN']['FORCESSL'] == 'true' && $ssl != ''){
188 header ("Location: $ssl");
189 exit;
190 }
192 /* Got a formular answer, validate and try to log in */
193 if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['login'])){
195 /* Reset error messages */
196 $message= "";
198 /* Destroy old sessions, they cause a successfull login to relog again ...*/
199 if(isset($_SESSION['_LAST_PAGE_REQUEST'])){
200 $_SESSION['_LAST_PAGE_REQUEST'] = time();
201 }
203 $server= validate($_POST["server"]);
204 $config->set_current($server);
206 /* Admin-logon and verify */
207 $ldap = $config->get_ldap_link();
208 if (is_null($ldap) || (is_int($ldap) && $ldap == 0)){
209 print_red (_("Can't bind to LDAP. Please contact the system administrator."));
210 displayLogin();
211 exit();
212 }
214 /* Check for schema file presence */
215 if(!isset($config->data['MAIN']['SCHEMA_CHECK'])){
216 $config->data['MAIN']['SCHEMA_CHECK'] = "true";
217 }
218 if(isset($config->data['MAIN']['SCHEMA_CHECK'])&&preg_match("/true/i",$config->data['MAIN']['SCHEMA_CHECK'])){
219 $recursive = (isset($config->current['RECURSIVE']) && $config->current['RECURSIVE'] == "true");
220 $tls = (isset($config->current['TLS']) && $config->current['TLS'] == "true");
222 if(!count($ldap->get_objectclasses())){
224 print_red(_("GOsa cannot retrieve information about the installed schema files. Please make sure, that this is possible."));
225 displayLogin();
226 exit();
227 }else{
229 $cfg = array();
230 $cfg['admin'] = $config->current['ADMIN'];
231 $cfg['password'] = $config->current['PASSWORD'];
232 $cfg['connection']= $config->current['SERVER'];
233 $cfg['tls'] = $tls;
235 $str = check_schema($cfg,isset($config->current['RFC2307BIS']) && preg_match("/(true|yes|on|1)/i",$config->current['RFC2307BIS']));
237 $checkarr = array();
238 foreach($str as $tr){
239 if(isset($tr['IS_MUST_HAVE']) && !$tr['STATUS']){
240 print_red($tr['MSG']."<br>"._("Your ldap setup contains old schema definitions. Please re-run the setup."));
241 displayLogin();
242 exit();
243 }
244 }
245 }
246 }
247 /* Check for locking area */
248 $ldap->cat($config->current['CONFIG'], array("dn"));
249 $attrs= $ldap->fetch();
250 if (!count ($attrs)){
251 $ldap->cd($config->current['BASE']);
252 $ldap->create_missing_trees($config->current['CONFIG']);
253 }
255 /* Check for at least one subtreeACL in the complete tree */
256 $ldap->cd($config->current['BASE']);
257 $ldap->search("(&(objectClass=gosaObject)(gosaSubtreeACL=:all))");
258 if ($ldap->count() < 1){
259 print_red(_("You're missing an administrative account for GOsa, you'll not be able to administrate anything!"));
260 displayLogin();
261 exit();
262 }
264 /* Check for valid input */
265 $username= $_POST["username"];
266 if (!ereg("^[A-Za-z0-9_.-]+$", $username)){
267 $message= _("Please specify a valid username!");
268 } elseif (mb_strlen($_POST["password"], 'UTF-8') == 0){
269 $message= _("Please specify your password!");
270 $smarty->assign ('nextfield', 'password');
271 } else {
273 /* Login as user, initialize user ACL's */
274 $ui= ldap_login_user($username, $_POST["password"]);
275 if ($ui === NULL || !$ui ){
276 $message= _("Please check the username/password combination.");
277 $smarty->assign ('nextfield', 'password');
278 gosa_log ("Authentication failed for user \"$username\"");
279 } else {
280 /* Remove all locks of this user */
281 del_user_locks($ui->dn);
283 /* Save userinfo and plugin structure */
284 $_SESSION['ui']= $ui;
285 $_SESSION['session_cnt']= 0;
287 /* Let GOsa trigger a new connection for each POST, save
288 config to session. */
289 $config->get_departments();
290 $config->make_idepartments();
291 $_SESSION['config']= $config;
293 /* Take care about zend.ze1_compatiblity_mode */
294 if (ini_get("zend.ze1_compatibility_mode") != 0){
295 $_SESSION['PHP4COMPATIBLE']= TRUE;
296 }
298 /* are we using accountexpiration */
299 if((isset($config->data['MAIN']['ACCOUNT_EXPIRATION'])) &&
300 preg_match('/true/i', $config->data['MAIN']['ACCOUNT_EXPIRATION'])){
302 $expired= ldap_expired_account($config, $ui->dn, $ui->username);
304 if ($expired == 1){
305 $message= _("Account locked. Please contact your system administrator.");
306 $smarty->assign ('nextfield', 'password');
307 gosa_log ("Account for user \"$username\" has expired");
308 } elseif ($expired == 3){
309 $plist= new pluglist($config, $ui);
310 foreach ($plist->dirlist as $key => $value){
311 if (preg_match("/\bpassword\b/i",$value)){
312 $plug=$key;
313 gosa_log ("User \"$username\" password forced to change");
314 header ("Location: main.php?plug=$plug&reset=1");
315 exit;
316 }
317 }
318 }
320 /* Not account expired or password forced change go to main page */
321 gosa_log ("User \"$username\" logged in successfully");
322 header ("Location: main.php?global_check=1");
323 exit;
325 } else {
326 /* Go to main page */
327 gosa_log ("User \"$username\" logged in successfully");
328 header ("Location: main.php?global_check=1");
329 exit;
330 }
331 }
332 }
333 }
335 /* Fill template with required values */
336 $smarty->assign ('date', gmdate("D, d M Y H:i:s"));
337 $smarty->assign ('username', $username);
338 $smarty->assign ('personal_img', get_template_path('images/personal.png'));
339 $smarty->assign ('password_img', get_template_path('images/password.png'));
340 $smarty->assign ('directory_img', get_template_path('images/ldapserver.png'));
342 /* Some error to display? */
343 if (!isset($message)){
344 $message= "";
345 }
347 $smarty->assign ("message", $message);
349 /* Displasy SSL mode warning? */
350 if ($ssl != "" && $config->data['MAIN']['WARNSSL'] == 'true'){
351 $smarty->assign ("ssl", "<b>"._("Warning").":<\/b> "._("Session will not be encrypted.")." <a style=\"color:red;\" href=\"$ssl\"><b>"._("Enter SSL session")."<\/b></a>!");
352 } else {
353 $smarty->assign ("ssl", "");
354 }
356 /* Translation of cookie-warning. Whether to display it, is determined by JavaScript */
357 $smarty->assign ("cookies", "<b>"._("Warning").":<\/b> "._("Your browser has cookies disabled. Please enable cookies and reload this page before logging in!"));
360 /* Generate server list */
361 $servers= array();
362 if (isset($_POST['server'])){
363 $selected= validate($_POST['server']);
364 } else {
365 $selected= $config->data['MAIN']['DEFAULT'];
366 }
367 foreach ($config->data['LOCATIONS'] as $key => $ignored){
368 $servers[$key]= $key;
369 }
370 $smarty->assign ("server_options", $servers);
371 $smarty->assign ("server_id", $selected);
373 /* show login screen */
374 $smarty->assign ("PHPSESSID", session_id());
375 if (isset($_SESSION['errors'])){
376 $smarty->assign("errors", $_SESSION['errors']);
377 }
378 if ($error_collector != ""){
379 $smarty->assign("php_errors", $error_collector."</div>");
380 } else {
381 $smarty->assign("php_errors", "");
382 }
384 displayLogin();
386 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
387 ?>
389 </body>
390 </html>