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