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 /* Reset error handler */
27 $error_collector= "";
28 set_error_handler('gosaRaiseError');
30 /* Set error handler to own one, initialize time calculation
31 and start session. */
32 session_start ();
33 $username= "";
35 /* Check if we need to run setup */
36 if (!file_exists(CONFIG_DIR."/gosa.conf")){
37 header("location:setup.php");
38 exit();
39 }
41 /* Reset errors */
42 $_SESSION['errors']= "";
44 /* Check for java script */
45 if (!isset($_GET['js']) && !isset($_SESSION['js'])){
46 echo '<script language="JavaScript" type="text/javascript">';
47 echo ' location = "index.php?js=true";';
48 echo '</script>';
50 $_SESSION['js']= FALSE;
51 } else {
52 $_SESSION['js']= TRUE;
53 }
55 /* Check if gosa.conf is accessable */
56 if (!is_readable(CONFIG_DIR."/gosa.conf")){
57 print_red(sprintf(_("GOsa configuration %s/gosa.conf is not readable. Aborted."), CONFIG_DIR));
58 echo $_SESSION['errors'];
59 exit();
60 }
62 /* Parse configuration file */
63 $config= new config(CONFIG_DIR."/gosa.conf", $BASE_DIR);
64 $_SESSION['DEBUGLEVEL']= $config->data['MAIN']['DEBUGLEVEL'];
65 if ($_SERVER["REQUEST_METHOD"] != "POST"){
66 @DEBUG (DEBUG_CONFIG, __LINE__, __FUNCTION__, __FILE__, $config->data, "config");
67 }
69 /* Set template compile directory */
70 if (isset ($config->data['MAIN']['COMPILE'])){
71 $smarty->compile_dir= $config->data['MAIN']['COMPILE'];
72 } else {
73 $smarty->compile_dir= '/var/spool/gosa';
74 }
75 $smarty->assign ('nextfield', 'username');
77 /* Check for compile directory */
78 if (!(is_dir($smarty->compile_dir) && is_writable($smarty->compile_dir))){
79 print_red(sprintf(_("Directory '%s' specified as compile directory is not accessable!"),
80 $smarty->compile_dir));
81 echo $_SESSION['errors'];
82 exit();
83 }
85 /* Check for old files in compile directory */
86 clean_smarty_compile_dir($smarty->compile_dir);
88 /* Language setup */
89 if ($config->data['MAIN']['LANG'] == ""){
90 $lang= get_browser_language();
91 } else {
92 $lang= $config->data['MAIN']['LANG'];
93 }
94 $lang.=".UTF-8";
95 putenv("LANGUAGE=");
96 putenv("LANG=$lang");
97 setlocale(LC_ALL, $lang);
98 $GLOBALS['t_language']= $lang;
99 $GLOBALS['t_gettext_message_dir'] = $BASE_DIR.'/locale/';
101 /* Set the text domain as 'messages' */
102 $domain = 'messages';
103 bindtextdomain($domain, "$BASE_DIR/locale");
104 textdomain($domain);
107 if ($_SERVER["REQUEST_METHOD"] != "POST"){
108 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, $lang, "Setting language to");
109 }
112 /* Check for SSL connection */
113 $ssl= "";
114 if (!isset($HTTP_SERVER_VARS['HTTPS']) ||
115 !stristr($HTTP_SERVER_VARS['HTTPS'], "on")) {
117 if (empty($HTTP_SERVER_VARS['REQUEST_URI'])) {
118 $ssl= "https://".$HTTP_SERVER_VARS['HTTP_HOST'].
119 $HTTP_SERVER_VARS['PATH_INFO'];
120 } else {
121 $ssl= "https://".$HTTP_SERVER_VARS['HTTP_HOST'].
122 $HTTP_SERVER_VARS['REQUEST_URI'];
123 }
124 }
126 /* If SSL is forced, just forward to the SSL enabled site */
127 if ($config->data['MAIN']['FORCESSL'] == 'true'){
128 header ("Location: $ssl");
129 exit;
130 }
132 /* Got a formular answer, validate and try to log in */
133 if ($_SERVER["REQUEST_METHOD"] == "POST"){
135 /* Reset error messages */
136 $message= "";
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 exit();
147 }
149 /* Check for locking area */
150 $ldap->cat($config->current['CONFIG']);
151 $attrs= $ldap->fetch();
152 if (!count ($attrs)){
153 $ldap->cd($config->current['BASE']);
154 $ldap->create_missing_trees($config->current['CONFIG']);
155 }
157 /* Check for at least one subtreeACL in the complete tree */
158 $ldap->cd($config->current['BASE']);
159 $ldap->search("(&(objectClass=gosaObject)(gosaSubtreeACL=:all))");
160 if ($ldap->count() < 1){
161 #FIXME: -> ldapsetup.tpl would be better
162 print_red(_("You're missing an administrative account for GOsa, you'll not be able to administrate anything!"));
163 echo $_SESSION['errors'];
164 exit;
165 }
167 /* Check for valid input */
168 $username= $_POST["username"];
169 if (!ereg("^[A-Za-z0-9_.-]+$", $username)){
170 $message= _("Please specify a valid username!");
171 } elseif (mb_strlen($_POST["password"], 'UTF-8') == 0){
172 $message= _("Please specify your password!");
173 $smarty->assign ('nextfield', 'password');
174 } else {
176 /* Login as user, initialize user ACL's */
177 $ui= ldap_login_user($username, $_POST["password"]);
178 if ($ui === NULL || $ui == 0){
179 $message= _("Please check the username/password combination.");
180 $smarty->assign ('nextfield', 'password');
181 gosa_log ("Authentication failed for user \"$username\"");
182 } else {
183 /* Remove all locks of this user */
184 del_user_locks($ui->dn);
186 /* Save userinfo and plugin structure */
187 $_SESSION['ui']= $ui;
188 $_SESSION['session_cnt']= 0;
190 /* Let GOsa trigger a new connection for each POST, save
191 config to session. */
192 $_SESSION['config']= $config;
194 /* Go to main page */
195 gosa_log ("User \"$username\" logged in successfully");
196 header ("Location: main.php?global_check=1");
197 exit;
198 }
199 }
200 }
202 /* Fill template with required values */
203 $smarty->assign ('date', gmdate("D, d M Y H:i:s"));
204 $smarty->assign ('username', $username);
205 $smarty->assign ('personal_img', get_template_path('images/personal.png'));
206 $smarty->assign ('password_img', get_template_path('images/password.png'));
207 $smarty->assign ('directory_img', get_template_path('images/ldapserver.png'));
209 /* Some error to display? */
210 if (!isset($message)){
211 $message= "";
212 }
213 $smarty->assign ("message", $message);
215 /* Displasy SSL mode warning? */
216 if ($ssl != "" && $config->data['MAIN']['WARNSSL'] == 'true'){
217 $smarty->assign ("ssl", "<b>"._("Warning").":</b> "._("Session will not be encrypted.")." <a style=\"color:red;\" href=\"$ssl\"><b>"._("Enter SSL session")."</b></a>!");
218 } else {
219 $smarty->assign ("ssl", "");
220 }
222 /* Generate server list */
223 $servers= array();
224 if (isset($_POST['server'])){
225 $selected= validate($_POST['server']);
226 } else {
227 $selected= $config->data['MAIN']['DEFAULT'];
228 }
229 foreach ($config->data['LOCATIONS'] as $key => $ignored){
230 $servers[$key]= $key;
231 }
232 $smarty->assign ("server_options", $servers);
233 $smarty->assign ("server_id", $selected);
235 /* show login screen */
236 $smarty->display (get_template_path('headers.tpl'));
237 $smarty->assign ("PHPSESSID", session_id());
238 if (isset($_SESSION['errors'])){
239 $smarty->assign("errors", $_SESSION['errors']);
240 }
241 if ($error_collector != ""){
242 $smarty->assign("php_errors", $error_collector."</div>");
243 } else {
244 $smarty->assign("php_errors", "");
245 }
246 $smarty->display (get_template_path('login.tpl'));
248 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
249 ?>
251 </body>
252 </html>