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 print_red(sprintf(_("GOsa configuration %s/gosa.conf is not readable. Aborted."), CONFIG_DIR));
55 echo $_SESSION['errors'];
56 exit();
57 }
59 /* Parse configuration file */
60 $config= new config(CONFIG_DIR."/gosa.conf", $BASE_DIR);
61 $_SESSION['DEBUGLEVEL']= $config->data['MAIN']['DEBUGLEVEL'];
62 if ($_SERVER["REQUEST_METHOD"] != "POST"){
63 @DEBUG (DEBUG_CONFIG, __LINE__, __FUNCTION__, __FILE__, $config->data, "config");
64 }
66 /* Set template compile directory */
67 if (isset ($config->data['MAIN']['COMPILE'])){
68 $smarty->compile_dir= $config->data['MAIN']['COMPILE'];
69 } else {
70 $smarty->compile_dir= '/var/spool/gosa';
71 }
72 $smarty->assign ('nextfield', 'username');
74 /* Check for compile directory */
75 if (!(is_dir($smarty->compile_dir) && is_writable($smarty->compile_dir))){
76 print_red(sprintf(_("Directory '%s' specified as compile directory is not accessible!"),
77 $smarty->compile_dir));
78 echo $_SESSION['errors'];
79 exit();
80 }
82 /* Check for old files in compile directory */
83 clean_smarty_compile_dir($smarty->compile_dir);
85 /* Language setup */
86 if ($config->data['MAIN']['LANG'] == ""){
87 $lang= get_browser_language();
88 } else {
89 $lang= $config->data['MAIN']['LANG'];
90 }
91 $lang.=".UTF-8";
92 putenv("LANGUAGE=");
93 putenv("LANG=$lang");
94 setlocale(LC_ALL, $lang);
95 $GLOBALS['t_language']= $lang;
96 $GLOBALS['t_gettext_message_dir'] = $BASE_DIR.'/locale/';
98 /* Set the text domain as 'messages' */
99 $domain = 'messages';
100 bindtextdomain($domain, "$BASE_DIR/locale");
101 textdomain($domain);
104 if ($_SERVER["REQUEST_METHOD"] != "POST"){
105 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, $lang, "Setting language to");
106 }
109 /* Check for SSL connection */
110 $ssl= "";
111 if (!isset($_SERVER['HTTPS']) ||
112 !stristr($_SERVER['HTTPS'], "on")) {
114 if (empty($_SERVER['REQUEST_URI'])) {
115 $ssl= "https://".$_SERVER['HTTP_HOST'].
116 $_SERVER['PATH_INFO'];
117 } else {
118 $ssl= "https://".$_SERVER['HTTP_HOST'].
119 $_SERVER['REQUEST_URI'];
120 }
121 }
123 /* If SSL is forced, just forward to the SSL enabled site */
124 if ($config->data['MAIN']['FORCESSL'] == 'true' && $ssl != ''){
125 header ("Location: $ssl");
126 exit;
127 }
129 /* Got a formular answer, validate and try to log in */
130 if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['login'])){
132 /* Reset error messages */
133 $message= "";
135 /* Destroy old sessions, they cause a successfull login to relog again ...*/
136 if(isset($_SESSION['_LAST_PAGE_REQUEST'])){
137 $_SESSION['_LAST_PAGE_REQUEST'] = time();
138 }
140 $server= validate($_POST["server"]);
141 $config->set_current($server);
143 /* Admin-logon and verify */
144 $ldap = $config->get_ldap_link();
145 if (is_null($ldap) || (is_int($ldap) && $ldap == 0)){
146 print_red (_("Can't bind to LDAP. Please contact the system administrator."));
147 echo $_SESSION['errors'];
148 $smarty->display(get_template_path('login.tpl'));
149 exit();
150 }
152 /* Check for schema file presence */
153 if(!isset($config->data['MAIN']['SCHEMA_CHECK'])){
154 $config->data['MAIN']['SCHEMA_CHECK'] = "true";
155 }
156 if(isset($config->data['MAIN']['SCHEMA_CHECK'])&&preg_match("/true/i",$config->data['MAIN']['SCHEMA_CHECK'])){
157 require_once("functions_setup.inc");
158 if(!is_schema_readable($config->current['SERVER'],$config->current['ADMIN'],$config->current['PASSWORD'])){
159 print_red(_("GOsa cannot retrieve information about the installed schema files. Please make sure, that this is possible."));
160 echo $_SESSION['errors'];
161 exit();
162 }else{
163 $str = (schema_check($config->current['SERVER'],$config->current['ADMIN'],$config->current['PASSWORD'],0,TRUE));
164 $checkarr = array();
165 foreach($str as $tr){
166 if(isset($tr['needonstartup'])){
167 print_red($tr['msg']."<br>"._("Your ldap setup contains old schema definitions. Please re-run the setup."));
168 print $_SESSION['errors'];
169 exit();
170 }
171 }
172 }
173 }
174 /* Check for locking area */
175 $ldap->cat($config->current['CONFIG'], array("dn"));
176 $attrs= $ldap->fetch();
177 if (!count ($attrs)){
178 $ldap->cd($config->current['BASE']);
179 $ldap->create_missing_trees($config->current['CONFIG']);
180 }
182 /* Check for at least one subtreeACL in the complete tree */
183 $ldap->cd($config->current['BASE']);
184 $ldap->search("(&(objectClass=gosaObject)(gosaSubtreeACL=:all))");
185 if ($ldap->count() < 1){
186 print_red(_("You're missing an administrative account for GOsa, you'll not be able to administrate anything!"));
187 displayLogin();
188 exit();
189 }
191 /* Check for valid input */
192 $username= $_POST["username"];
193 if (!ereg("^[A-Za-z0-9_.-]+$", $username)){
194 $message= _("Please specify a valid username!");
195 } elseif (mb_strlen($_POST["password"], 'UTF-8') == 0){
196 $message= _("Please specify your password!");
197 $smarty->assign ('nextfield', 'password');
198 } else {
200 /* Login as user, initialize user ACL's */
201 $ui= ldap_login_user($username, $_POST["password"]);
202 if ($ui === NULL || $ui == 0){
203 $message= _("Please check the username/password combination.");
204 $smarty->assign ('nextfield', 'password');
205 gosa_log ("Authentication failed for user \"$username\"");
206 } else {
207 /* Remove all locks of this user */
208 del_user_locks($ui->dn);
210 /* Save userinfo and plugin structure */
211 $_SESSION['ui']= $ui;
212 $_SESSION['session_cnt']= 0;
214 /* Let GOsa trigger a new connection for each POST, save
215 config to session. */
216 $config->get_departments();
217 $config->make_idepartments();
218 $_SESSION['config']= $config;
220 /* Go to main page */
221 gosa_log ("User \"$username\" logged in successfully");
222 header ("Location: main.php?global_check=1");
223 exit;
224 }
225 }
226 }
228 /* Fill template with required values */
229 $smarty->assign ('date', gmdate("D, d M Y H:i:s"));
230 $smarty->assign ('username', $username);
231 $smarty->assign ('personal_img', get_template_path('images/personal.png'));
232 $smarty->assign ('password_img', get_template_path('images/password.png'));
233 $smarty->assign ('directory_img', get_template_path('images/ldapserver.png'));
235 /* Some error to display? */
236 if (!isset($message)){
237 $message= "";
238 }
240 $smarty->assign ("message", $message);
242 /* Displasy SSL mode warning? */
243 if ($ssl != "" && $config->data['MAIN']['WARNSSL'] == 'true'){
244 $smarty->assign ("ssl", "<b>"._("Warning").":</b> "._("Session will not be encrypted.")." <a style=\"color:red;\" href=\"$ssl\"><b>"._("Enter SSL session")."</b></a>!");
245 } else {
246 $smarty->assign ("ssl", "");
247 }
249 /* Translation of cookie-warning. Whether to display it, is determined by JavaScript */
250 $smarty->assign ("cookies", "<b>"._("Warning").":</b> "._("Your browser has cookies disabled. Please enable cookies and reload this page before logging in!"));
253 /* Generate server list */
254 $servers= array();
255 if (isset($_POST['server'])){
256 $selected= validate($_POST['server']);
257 } else {
258 $selected= $config->data['MAIN']['DEFAULT'];
259 }
260 foreach ($config->data['LOCATIONS'] as $key => $ignored){
261 $servers[$key]= $key;
262 }
263 $smarty->assign ("server_options", $servers);
264 $smarty->assign ("server_id", $selected);
266 /* show login screen */
267 $smarty->display (get_template_path('headers.tpl'));
268 $smarty->assign ("PHPSESSID", session_id());
269 if (isset($_SESSION['errors'])){
270 $smarty->assign("errors", $_SESSION['errors']);
271 }
272 if ($error_collector != ""){
273 $smarty->assign("php_errors", $error_collector."</div>");
274 } else {
275 $smarty->assign("php_errors", "");
276 }
277 $smarty->display (get_template_path('login.tpl'));
285 function displayLogin()
286 {
287 global $smarty,$message,$config,$ssl,$error_collector;
288 error_reporting(E_ALL);
289 /* Fill template with required values */
290 $username = "";
291 if(isset($_POST["username"])){
292 $username= $_POST["username"];
293 }
294 $smarty->assign ('date', gmdate("D, d M Y H:i:s"));
295 $smarty->assign ('username', $username);
296 $smarty->assign ('personal_img', get_template_path('images/personal.png'));
297 $smarty->assign ('password_img', get_template_path('images/password.png'));
298 $smarty->assign ('directory_img', get_template_path('images/ldapserver.png'));
300 /* Some error to display? */
301 if (!isset($message)){
302 $message= "";
303 }
304 $smarty->assign ("message", $message);
306 /* Displasy SSL mode warning? */
307 if ($ssl != "" && $config->data['MAIN']['WARNSSL'] == 'true'){
308 $smarty->assign ("ssl", "<b>"._("Warning").":</b> "._("Session will not be encrypted.")." <a style=\"color:red;\" href=\"$ssl\"><b>"._("Enter SSL session")."</b></a>!");
309 } else {
310 $smarty->assign ("ssl", "");
311 }
313 /* Generate server list */
314 $servers= array();
315 if (isset($_POST['server'])){
316 $selected= validate($_POST['server']);
317 } else {
318 $selected= $config->data['MAIN']['DEFAULT'];
319 }
320 foreach ($config->data['LOCATIONS'] as $key => $ignored){
321 $servers[$key]= $key;
322 }
323 $smarty->assign ("server_options", $servers);
324 $smarty->assign ("server_id", $selected);
326 /* show login screen */
327 $smarty->display (get_template_path('headers.tpl'));
328 $smarty->assign ("PHPSESSID", session_id());
329 if (isset($_SESSION['errors'])){
330 $smarty->assign("errors", $_SESSION['errors']);
331 }
332 if ($error_collector != ""){
333 $smarty->assign("php_errors", $error_collector."</div>");
334 } else {
335 $smarty->assign("php_errors", "");
336 }
338 $smarty->display(get_template_path('login.tpl'));
339 exit();
340 }
344 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
345 ?>
347 </body>
348 </html>