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']);
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 $_SESSION['config']= $config;
218 /* Go to main page */
219 gosa_log ("User \"$username\" logged in successfully");
220 header ("Location: main.php?global_check=1");
221 exit;
222 }
223 }
224 }
226 /* Fill template with required values */
227 $smarty->assign ('date', gmdate("D, d M Y H:i:s"));
228 $smarty->assign ('username', $username);
229 $smarty->assign ('personal_img', get_template_path('images/personal.png'));
230 $smarty->assign ('password_img', get_template_path('images/password.png'));
231 $smarty->assign ('directory_img', get_template_path('images/ldapserver.png'));
233 /* Some error to display? */
234 if (!isset($message)){
235 $message= "";
236 }
238 $smarty->assign ("message", $message);
240 /* Displasy SSL mode warning? */
241 if ($ssl != "" && $config->data['MAIN']['WARNSSL'] == 'true'){
242 $smarty->assign ("ssl", "<b>"._("Warning").":</b> "._("Session will not be encrypted.")." <a style=\"color:red;\" href=\"$ssl\"><b>"._("Enter SSL session")."</b></a>!");
243 } else {
244 $smarty->assign ("ssl", "");
245 }
247 /* Translation of cookie-warning. Whether to display it, is determined by JavaScript */
248 $smarty->assign ("cookies", "<b>"._("Warning").":</b> "._("Your browser has cookies disabled. Please enable cookies and reload this page before logging in!"));
251 /* Generate server list */
252 $servers= array();
253 if (isset($_POST['server'])){
254 $selected= validate($_POST['server']);
255 } else {
256 $selected= $config->data['MAIN']['DEFAULT'];
257 }
258 foreach ($config->data['LOCATIONS'] as $key => $ignored){
259 $servers[$key]= $key;
260 }
261 $smarty->assign ("server_options", $servers);
262 $smarty->assign ("server_id", $selected);
264 /* show login screen */
265 $smarty->display (get_template_path('headers.tpl'));
266 $smarty->assign ("PHPSESSID", session_id());
267 if (isset($_SESSION['errors'])){
268 $smarty->assign("errors", $_SESSION['errors']);
269 }
270 if ($error_collector != ""){
271 $smarty->assign("php_errors", $error_collector."</div>");
272 } else {
273 $smarty->assign("php_errors", "");
274 }
275 $smarty->display (get_template_path('login.tpl'));
283 function displayLogin()
284 {
285 global $smarty,$message,$config,$ssl,$error_collector;
286 error_reporting(E_ALL);
287 /* Fill template with required values */
288 $username = "";
289 if(isset($_POST["username"])){
290 $username= $_POST["username"];
291 }
292 $smarty->assign ('date', gmdate("D, d M Y H:i:s"));
293 $smarty->assign ('username', $username);
294 $smarty->assign ('personal_img', get_template_path('images/personal.png'));
295 $smarty->assign ('password_img', get_template_path('images/password.png'));
296 $smarty->assign ('directory_img', get_template_path('images/ldapserver.png'));
298 /* Some error to display? */
299 if (!isset($message)){
300 $message= "";
301 }
302 $smarty->assign ("message", $message);
304 /* Displasy SSL mode warning? */
305 if ($ssl != "" && $config->data['MAIN']['WARNSSL'] == 'true'){
306 $smarty->assign ("ssl", "<b>"._("Warning").":</b> "._("Session will not be encrypted.")." <a style=\"color:red;\" href=\"$ssl\"><b>"._("Enter SSL session")."</b></a>!");
307 } else {
308 $smarty->assign ("ssl", "");
309 }
311 /* Generate server list */
312 $servers= array();
313 if (isset($_POST['server'])){
314 $selected= validate($_POST['server']);
315 } else {
316 $selected= $config->data['MAIN']['DEFAULT'];
317 }
318 foreach ($config->data['LOCATIONS'] as $key => $ignored){
319 $servers[$key]= $key;
320 }
321 $smarty->assign ("server_options", $servers);
322 $smarty->assign ("server_id", $selected);
324 /* show login screen */
325 $smarty->display (get_template_path('headers.tpl'));
326 $smarty->assign ("PHPSESSID", session_id());
327 if (isset($_SESSION['errors'])){
328 $smarty->assign("errors", $_SESSION['errors']);
329 }
330 if ($error_collector != ""){
331 $smarty->assign("php_errors", $error_collector."</div>");
332 } else {
333 $smarty->assign("php_errors", "");
334 }
336 $smarty->display(get_template_path('login.tpl'));
337 exit();
338 }
342 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
343 ?>
345 </body>
346 </html>