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']['ACCOUNTEXPIRED'])) && $config->data['MAIN']['ACCOUNTEXPIRED'] == "1"){
221 $expired= ldap_expired_account($config, $ui->dn, $ui->username);
223 if ($expired == 1){
224 $message= _("Account Locked");
225 $smarty->assign ('nextfield', 'password');
226 gosa_log ("Account for user \"$username\" has expired");
227 } elseif ($expired == 3){
228 $plist= new pluglist($config, $ui);
229 foreach ($plist->dirlist as $key => $value){
230 if (preg_match("/\bpassword\b/i",$value)){
231 $plug=$key;
232 gosa_log ("User \"$username\" password forced to change");
233 header ("Location: main.php?plug=$plug&reset=1");
234 exit;
235 }
236 }
237 }
239 /* Not account expired or password forced change go to main page */
240 gosa_log ("User \"$username\" logged in successfully");
241 header ("Location: main.php?global_check=1");
242 exit;
244 } else {
245 /* Go to main page */
246 gosa_log ("User \"$username\" logged in successfully");
247 header ("Location: main.php?global_check=1");
248 exit;
249 }
250 }
251 }
252 }
254 /* Fill template with required values */
255 $smarty->assign ('date', gmdate("D, d M Y H:i:s"));
256 $smarty->assign ('username', $username);
257 $smarty->assign ('personal_img', get_template_path('images/personal.png'));
258 $smarty->assign ('password_img', get_template_path('images/password.png'));
259 $smarty->assign ('directory_img', get_template_path('images/ldapserver.png'));
261 /* Some error to display? */
262 if (!isset($message)){
263 $message= "";
264 }
266 $smarty->assign ("message", $message);
268 /* Displasy SSL mode warning? */
269 if ($ssl != "" && $config->data['MAIN']['WARNSSL'] == 'true'){
270 $smarty->assign ("ssl", "<b>"._("Warning").":</b> "._("Session will not be encrypted.")." <a style=\"color:red;\" href=\"$ssl\"><b>"._("Enter SSL session")."</b></a>!");
271 } else {
272 $smarty->assign ("ssl", "");
273 }
275 /* Translation of cookie-warning. Whether to display it, is determined by JavaScript */
276 $smarty->assign ("cookies", "<b>"._("Warning").":</b> "._("Your browser has cookies disabled. Please enable cookies and reload this page before logging in!"));
279 /* Generate server list */
280 $servers= array();
281 if (isset($_POST['server'])){
282 $selected= validate($_POST['server']);
283 } else {
284 $selected= $config->data['MAIN']['DEFAULT'];
285 }
286 foreach ($config->data['LOCATIONS'] as $key => $ignored){
287 $servers[$key]= $key;
288 }
289 $smarty->assign ("server_options", $servers);
290 $smarty->assign ("server_id", $selected);
292 /* show login screen */
293 $smarty->display (get_template_path('headers.tpl'));
294 $smarty->assign ("PHPSESSID", session_id());
295 if (isset($_SESSION['errors'])){
296 $smarty->assign("errors", $_SESSION['errors']);
297 }
298 if ($error_collector != ""){
299 $smarty->assign("php_errors", $error_collector."</div>");
300 } else {
301 $smarty->assign("php_errors", "");
302 }
303 $smarty->display (get_template_path('login.tpl'));
311 function displayLogin()
312 {
313 global $smarty,$message,$config,$ssl,$error_collector;
314 error_reporting(E_ALL);
315 /* Fill template with required values */
316 $username = "";
317 if(isset($_POST["username"])){
318 $username= $_POST["username"];
319 }
320 $smarty->assign ('date', gmdate("D, d M Y H:i:s"));
321 $smarty->assign ('username', $username);
322 $smarty->assign ('personal_img', get_template_path('images/personal.png'));
323 $smarty->assign ('password_img', get_template_path('images/password.png'));
324 $smarty->assign ('directory_img', get_template_path('images/ldapserver.png'));
326 /* Some error to display? */
327 if (!isset($message)){
328 $message= "";
329 }
330 $smarty->assign ("message", $message);
332 /* Displasy SSL mode warning? */
333 if ($ssl != "" && $config->data['MAIN']['WARNSSL'] == 'true'){
334 $smarty->assign ("ssl", "<b>"._("Warning").":</b> "._("Session will not be encrypted.")." <a style=\"color:red;\" href=\"$ssl\"><b>"._("Enter SSL session")."</b></a>!");
335 } else {
336 $smarty->assign ("ssl", "");
337 }
339 /* Generate server list */
340 $servers= array();
341 if (isset($_POST['server'])){
342 $selected= validate($_POST['server']);
343 } else {
344 $selected= $config->data['MAIN']['DEFAULT'];
345 }
346 foreach ($config->data['LOCATIONS'] as $key => $ignored){
347 $servers[$key]= $key;
348 }
349 $smarty->assign ("server_options", $servers);
350 $smarty->assign ("server_id", $selected);
352 /* show login screen */
353 $smarty->display (get_template_path('headers.tpl'));
354 $smarty->assign ("PHPSESSID", session_id());
355 if (isset($_SESSION['errors'])){
356 $smarty->assign("errors", $_SESSION['errors']);
357 }
358 if ($error_collector != ""){
359 $smarty->assign("php_errors", $error_collector."</div>");
360 } else {
361 $smarty->assign("php_errors", "");
362 }
364 $smarty->display(get_template_path('login.tpl'));
365 exit();
366 }
370 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
371 ?>
373 </body>
374 </html>