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 }
35 $smarty->assign ('date', gmdate("D, d M Y H:i:s"));
36 $smarty->assign ('username', $username);
37 $smarty->assign ('personal_img', get_template_path('images/personal.png'));
38 $smarty->assign ('password_img', get_template_path('images/password.png'));
39 $smarty->assign ('directory_img', get_template_path('images/ldapserver.png'));
41 /* Some error to display? */
42 if (!isset($message)){
43 $message= "";
44 }
45 $smarty->assign ("message", $message);
47 /* Displasy SSL mode warning? */
48 if ($ssl != "" && $config->data['MAIN']['WARNSSL'] == 'true'){
49 $smarty->assign ("ssl", "<b>"._("Warning").":</b> "._("Session will not be encrypted.")." <a style=\"color:red;\" href=\"$ssl\"><b>"._("Enter SSL session")."</b></a>!");
50 } else {
51 $smarty->assign ("ssl", "");
52 }
54 /* Generate server list */
55 $servers= array();
56 if (isset($_POST['server'])){
57 $selected= validate($_POST['server']);
58 } else {
59 $selected= $config->data['MAIN']['DEFAULT'];
60 }
61 foreach ($config->data['LOCATIONS'] as $key => $ignored){
62 $servers[$key]= $key;
63 }
64 $smarty->assign ("server_options", $servers);
65 $smarty->assign ("server_id", $selected);
67 /* show login screen */
68 $smarty->assign ("PHPSESSID", session_id());
69 if (isset($_SESSION['errors'])){
70 $smarty->assign("errors", $_SESSION['errors']);
71 }
72 if ($error_collector != ""){
73 $smarty->assign("php_errors", $error_collector."</div>");
74 } else {
75 $smarty->assign("php_errors", "");
76 }
78 $smarty->display (get_template_path('headers.tpl'));
79 $smarty->display(get_template_path('login.tpl'));
80 exit();
81 }
85 /* Set error handler to own one, initialize time calculation
86 and start session. */
87 session_start ();
88 $username= "";
90 /* Reset errors */
91 $_SESSION['errors'] = "";
92 $_SESSION['errorsAlreadyPosted']= array();
93 $_SESSION['LastError'] = "";
95 /* Check if we need to run setup */
96 if (!file_exists(CONFIG_DIR."/gosa.conf")){
97 header("location:setup.php");
98 exit();
99 }
101 /* Reset errors */
102 $_SESSION['errors']= "";
104 /* Check for java script */
105 if(isset($_POST['javascript']) && $_POST['javascript'] == "true") {
106 $_SESSION['js']= TRUE;
107 }elseif(isset($_POST['javascript'])) {
108 $_SESSION['js']= FALSE;
109 }
111 /* Check if gosa.conf is accessible */
112 if (!is_readable(CONFIG_DIR."/gosa.conf")){
113 echo sprintf(_("GOsa configuration %s/gosa.conf is not readable. Aborted."), CONFIG_DIR);
114 exit();
115 }
117 /* Parse configuration file */
118 $config= new config(CONFIG_DIR."/gosa.conf", $BASE_DIR);
119 $_SESSION['DEBUGLEVEL']= $config->data['MAIN']['DEBUGLEVEL'];
120 if ($_SERVER["REQUEST_METHOD"] != "POST"){
121 @DEBUG (DEBUG_CONFIG, __LINE__, __FUNCTION__, __FILE__, $config->data, "config");
122 }
124 /* Set template compile directory */
125 if (isset ($config->data['MAIN']['COMPILE'])){
126 $smarty->compile_dir= $config->data['MAIN']['COMPILE'];
127 } else {
128 $smarty->compile_dir= '/var/spool/gosa';
129 }
130 $smarty->assign ('nextfield', 'username');
132 /* Check for compile directory */
133 if (!(is_dir($smarty->compile_dir) && is_writable($smarty->compile_dir))){
134 echo sprintf(_("Directory '%s' specified as compile directory is not accessible!"),
135 $smarty->compile_dir);
136 exit();
137 }
139 /* Check for old files in compile directory */
140 clean_smarty_compile_dir($smarty->compile_dir);
142 /* Language setup */
143 if ($config->data['MAIN']['LANG'] == ""){
144 $lang= get_browser_language();
145 } else {
146 $lang= $config->data['MAIN']['LANG'];
147 }
148 $lang.=".UTF-8";
149 putenv("LANGUAGE=");
150 putenv("LANG=$lang");
151 setlocale(LC_ALL, $lang);
152 $GLOBALS['t_language']= $lang;
153 $GLOBALS['t_gettext_message_dir'] = $BASE_DIR.'/locale/';
155 /* Set the text domain as 'messages' */
156 $domain = 'messages';
157 bindtextdomain($domain, "$BASE_DIR/locale");
158 textdomain($domain);
161 if ($_SERVER["REQUEST_METHOD"] != "POST"){
162 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, $lang, "Setting language to");
163 }
166 /* Check for SSL connection */
167 $ssl= "";
168 if (!isset($_SERVER['HTTPS']) ||
169 !stristr($_SERVER['HTTPS'], "on")) {
171 if (empty($_SERVER['REQUEST_URI'])) {
172 $ssl= "https://".$_SERVER['HTTP_HOST'].
173 $_SERVER['PATH_INFO'];
174 } else {
175 $ssl= "https://".$_SERVER['HTTP_HOST'].
176 $_SERVER['REQUEST_URI'];
177 }
178 }
180 /* If SSL is forced, just forward to the SSL enabled site */
181 if ($config->data['MAIN']['FORCESSL'] == 'true' && $ssl != ''){
182 header ("Location: $ssl");
183 exit;
184 }
186 /* Got a formular answer, validate and try to log in */
187 if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['login'])){
189 /* Reset error messages */
190 $message= "";
192 /* Destroy old sessions, they cause a successfull login to relog again ...*/
193 if(isset($_SESSION['_LAST_PAGE_REQUEST'])){
194 $_SESSION['_LAST_PAGE_REQUEST'] = time();
195 }
197 $server= validate($_POST["server"]);
198 $config->set_current($server);
200 /* Admin-logon and verify */
201 $ldap = $config->get_ldap_link();
202 if (is_null($ldap) || (is_int($ldap) && $ldap == 0)){
203 print_red (_("Can't bind to LDAP. Please contact the system administrator."));
204 displayLogin();
205 exit();
206 }
208 /* Check for schema file presence */
209 if(!isset($config->data['MAIN']['SCHEMA_CHECK'])){
210 $config->data['MAIN']['SCHEMA_CHECK'] = "true";
211 }
212 if(isset($config->data['MAIN']['SCHEMA_CHECK'])&&preg_match("/true/i",$config->data['MAIN']['SCHEMA_CHECK'])){
213 require_once("functions_setup.inc");
214 if(!is_schema_readable($config->current['SERVER'],$config->current['ADMIN'],$config->current['PASSWORD'])){
215 print_red(_("GOsa cannot retrieve information about the installed schema files. Please make sure, that this is possible."));
216 $smarty->display(get_template_path('headers.tpl'));
217 echo "<body>".$_SESSION['errors']."</body></html>";
218 exit();
219 }else{
220 $str = (schema_check($config->current['SERVER'],$config->current['ADMIN'],$config->current['PASSWORD'],0,TRUE));
221 $checkarr = array();
222 foreach($str as $tr){
223 if(isset($tr['needonstartup'])){
224 print_red($tr['msg']."<br>"._("Your ldap setup contains old schema definitions. Please re-run the setup."));
225 $smarty->display(get_template_path('headers.tpl'));
226 echo "<body>".$_SESSION['errors']."</body></html>";
227 exit();
228 }
229 }
230 }
231 }
232 /* Check for locking area */
233 $ldap->cat($config->current['CONFIG'], array("dn"));
234 $attrs= $ldap->fetch();
235 if (!count ($attrs)){
236 $ldap->cd($config->current['BASE']);
237 $ldap->create_missing_trees($config->current['CONFIG']);
238 }
240 /* Check for at least one subtreeACL in the complete tree */
241 $ldap->cd($config->current['BASE']);
242 $ldap->search("(&(objectClass=gosaObject)(gosaSubtreeACL=:all))");
243 if ($ldap->count() < 1){
244 print_red(_("You're missing an administrative account for GOsa, you'll not be able to administrate anything!"));
245 displayLogin();
246 exit();
247 }
249 /* Check for valid input */
250 $username= $_POST["username"];
251 if (!ereg("^[A-Za-z0-9_.-]+$", $username)){
252 $message= _("Please specify a valid username!");
253 } elseif (mb_strlen($_POST["password"], 'UTF-8') == 0){
254 $message= _("Please specify your password!");
255 $smarty->assign ('nextfield', 'password');
256 } else {
258 /* Login as user, initialize user ACL's */
259 $ui= ldap_login_user($username, $_POST["password"]);
260 if ($ui === NULL || $ui == 0){
261 $message= _("Please check the username/password combination.");
262 $smarty->assign ('nextfield', 'password');
263 gosa_log ("Authentication failed for user \"$username\"");
264 } else {
265 /* Remove all locks of this user */
266 del_user_locks($ui->dn);
268 /* Save userinfo and plugin structure */
269 $_SESSION['ui']= $ui;
270 $_SESSION['session_cnt']= 0;
272 /* Let GOsa trigger a new connection for each POST, save
273 config to session. */
274 $config->get_departments();
275 $config->make_idepartments();
276 $_SESSION['config']= $config;
278 /* are we using accountexpiration */
279 if((isset($config->data['MAIN']['ACCOUNT_EXPIRATION'])) &&
280 preg_match('/true/i', $config->data['MAIN']['ACCOUNT_EXPIRATION'])){
282 $expired= ldap_expired_account($config, $ui->dn, $ui->username);
284 if ($expired == 1){
285 $message= _("Account locked. Please contact your system administrator.");
286 $smarty->assign ('nextfield', 'password');
287 gosa_log ("Account for user \"$username\" has expired");
288 } elseif ($expired == 3){
289 $plist= new pluglist($config, $ui);
290 foreach ($plist->dirlist as $key => $value){
291 if (preg_match("/\bpassword\b/i",$value)){
292 $plug=$key;
293 gosa_log ("User \"$username\" password forced to change");
294 header ("Location: main.php?plug=$plug&reset=1");
295 exit;
296 }
297 }
298 }
300 /* Not account expired or password forced change go to main page */
301 gosa_log ("User \"$username\" logged in successfully");
302 header ("Location: main.php?global_check=1");
303 exit;
305 } else {
306 /* Go to main page */
307 gosa_log ("User \"$username\" logged in successfully");
308 header ("Location: main.php?global_check=1");
309 exit;
310 }
311 }
312 }
313 }
315 /* Fill template with required values */
316 $smarty->assign ('date', gmdate("D, d M Y H:i:s"));
317 $smarty->assign ('username', $username);
318 $smarty->assign ('personal_img', get_template_path('images/personal.png'));
319 $smarty->assign ('password_img', get_template_path('images/password.png'));
320 $smarty->assign ('directory_img', get_template_path('images/ldapserver.png'));
322 /* Some error to display? */
323 if (!isset($message)){
324 $message= "";
325 }
327 $smarty->assign ("message", $message);
329 /* Displasy SSL mode warning? */
330 if ($ssl != "" && $config->data['MAIN']['WARNSSL'] == 'true'){
331 $smarty->assign ("ssl", "<b>"._("Warning").":</b> "._("Session will not be encrypted.")." <a style=\"color:red;\" href=\"$ssl\"><b>"._("Enter SSL session")."</b></a>!");
332 } else {
333 $smarty->assign ("ssl", "");
334 }
336 /* Translation of cookie-warning. Whether to display it, is determined by JavaScript */
337 $smarty->assign ("cookies", "<b>"._("Warning").":</b> "._("Your browser has cookies disabled. Please enable cookies and reload this page before logging in!"));
340 /* Generate server list */
341 $servers= array();
342 if (isset($_POST['server'])){
343 $selected= validate($_POST['server']);
344 } else {
345 $selected= $config->data['MAIN']['DEFAULT'];
346 }
347 foreach ($config->data['LOCATIONS'] as $key => $ignored){
348 $servers[$key]= $key;
349 }
350 $smarty->assign ("server_options", $servers);
351 $smarty->assign ("server_id", $selected);
353 /* show login screen */
354 $smarty->display (get_template_path('headers.tpl'));
355 $smarty->assign ("PHPSESSID", session_id());
356 if (isset($_SESSION['errors'])){
357 $smarty->assign("errors", $_SESSION['errors']);
358 }
359 if ($error_collector != ""){
360 $smarty->assign("php_errors", $error_collector."</div>");
361 } else {
362 $smarty->assign("php_errors", "");
363 }
364 displayLogin();
367 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
368 ?>
370 </body>
371 </html>