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