Skip to content

Commit 816e7e4

Browse files
author
maddinxx
committed
fail2ban compatible login log (no permissions for /var/log/ispconfig/login.log) - to be fixed! (FS#2244)
1 parent 996bad9 commit 816e7e4

File tree

1 file changed

+36
-32
lines changed

1 file changed

+36
-32
lines changed

interface/web/login/index.php

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,25 @@ class login_index {
3636
private $target = '';
3737
private $app;
3838
private $conf;
39-
39+
4040
public function render() {
41-
41+
4242
global $app, $conf;
43-
43+
4444
/* Redirect to page, if login form was NOT send */
4545
if(count($_POST) == 0) {
4646
if(isset($_SESSION['s']['user']) && is_array($_SESSION['s']['user']) && is_array($_SESSION['s']['module'])) {
4747
die('HEADER_REDIRECT:'.$_SESSION['s']['module']['startpage']);
4848
}
4949
}
50-
50+
5151
$app->uses('tpl');
5252
$app->tpl->newTemplate('form.tpl.htm');
53-
53+
5454
$error = '';
55-
55+
5656
$app->load_language_file('web/login/lib/lang/'.$conf["language"].'.lng');
57-
57+
5858
// Maintenance mode
5959
$maintenance_mode = false;
6060
$maintenance_mode_error = '';
@@ -64,21 +64,22 @@ public function render() {
6464
$maintenance_mode = true;
6565
$maintenance_mode_error = $app->lng('error_maintenance_mode');
6666
}
67-
67+
6868
//* Login Form was send
6969
if(count($_POST) > 0) {
70-
70+
7171
//** Check variables
7272
if(!preg_match("/^[\w\.\-\_\@]{1,128}$/", $_POST['username'])) $error = $app->lng('user_regex_error');
7373
if(!preg_match("/^.{1,64}$/i", $_POST['passwort'])) $error = $app->lng('pw_error_length');
74-
74+
7575
//** iporting variables
7676
$ip = $app->db->quote(ip2long($_SERVER['REMOTE_ADDR']));
7777
$username = $app->db->quote($_POST['username']);
7878
$passwort = $app->db->quote($_POST['passwort']);
7979
$loginAs = false;
8080
$time = time();
81-
81+
$logging = 'Failed login for user '. $username .' from '. long2ip($ip) .' at '. date('Y-m-d H:i:s');
82+
8283
if($username != '' && $passwort != '' && $error == '') {
8384
/*
8485
* Check, if there is a "login as" instead of a "normal" login
@@ -115,7 +116,7 @@ public function render() {
115116
if($alreadyfailed['times'] > 5) {
116117
$error = $app->lng('error_user_too_many_logins');
117118
} else {
118-
119+
119120
if ($loginAs){
120121
$sql = "SELECT * FROM sys_user WHERE USERNAME = '$username' and PASSWORT = '". $passwort. "'";
121122
$user = $app->db->queryOneRecord($sql);
@@ -145,24 +146,24 @@ public function render() {
145146
$user['default_group'] = $mailuser['sys_groupid'];
146147
}
147148
}
148-
149+
149150
} else {
150151
//* normal cp user login
151152
$sql = "SELECT * FROM sys_user WHERE USERNAME = '$username'";
152153
$user = $app->db->queryOneRecord($sql);
153-
154+
154155
if($user) {
155156
$saved_password = stripslashes($user['passwort']);
156-
157+
157158
if(substr($saved_password,0,3) == '$1$') {
158159
//* The password is crypt-md5 encrypted
159160
$salt = '$1$'.substr($saved_password,3,8).'$';
160-
161+
161162
if(crypt(stripslashes($passwort),$salt) != $saved_password) {
162163
$user = false;
163164
}
164165
} else {
165-
166+
166167
//* The password is md5 encrypted
167168
if(md5($passwort) != $saved_password) {
168169
$user = false;
@@ -173,7 +174,7 @@ public function render() {
173174
}
174175
}
175176
}
176-
177+
177178
if($user) {
178179
if($user['active'] == 1) {
179180
// Maintenance mode - allow logins only when maintenance mode is off or if the user is admin
@@ -182,28 +183,28 @@ public function render() {
182183
$sql = "DELETE FROM `attempts_login` WHERE `ip`='{$ip}'";
183184
$app->db->query($sql);
184185
$user = $app->db->toLower($user);
185-
186+
186187
if ($loginAs) $oldSession = $_SESSION['s'];
187188
$_SESSION = array();
188189
if ($loginAs) $_SESSION['s_old'] = $oldSession; // keep the way back!
189190
$_SESSION['s']['user'] = $user;
190191
$_SESSION['s']['user']['theme'] = isset($user['app_theme']) ? $user['app_theme'] : 'default';
191192
$_SESSION['s']['language'] = $user['language'];
192193
$_SESSION["s"]['theme'] = $_SESSION['s']['user']['theme'];
193-
194+
194195
if(is_file($_SESSION['s']['user']['startmodule'].'/lib/module.conf.php')) {
195196
include_once($_SESSION['s']['user']['startmodule'].'/lib/module.conf.php');
196197
$_SESSION['s']['module'] = $module;
197198
}
198-
199+
199200
$app->plugin->raiseEvent('login',$this);
200-
201+
201202
/*
202203
* We need LOGIN_REDIRECT instead of HEADER_REDIRECT to load the
203204
* new theme, if the logged-in user has another
204205
*/
205206
echo 'LOGIN_REDIRECT:'.$_SESSION['s']['module']['startpage'];
206-
207+
207208
exit;
208209
}
209210
} else {
@@ -223,36 +224,39 @@ public function render() {
223224
//* Incorrect login - Username and password incorrect
224225
$error = $app->lng('error_user_password_incorrect');
225226
if($app->db->errorMessage != '') $error .= '<br />'.$app->db->errorMessage != '';
226-
227-
$app->plugin->raiseEvent('login_failed',$this);
227+
228+
$app->plugin->raiseEvent('login_failed',$this);
229+
230+
//* write to log (e.g. for fail2ban)
231+
exec('echo '. $logging .' >> /tmp/login.log');
228232
}
229233
}
230234
} else {
231235
//* Username or password empty
232236
if($error == '') $error = $app->lng('error_user_password_empty');
233-
237+
234238
$app->plugin->raiseEvent('login_empty',$this);
235239
}
236240
}
237-
241+
238242
// Maintenance mode - show message when people try to log in and also when people are forcedly logged off
239243
if($maintenance_mode_error != '') $error = '<strong>'.$maintenance_mode_error.'</strong><br><br>'.$error;
240244
if($error != ''){
241245
$error = '<div class="box box_error"><h1>Error</h1>'.$error.'</div>';
242246
}
243-
247+
244248
$app->tpl->setVar('error', $error);
245-
$app->tpl->setVar('pw_lost_txt', $app->lng('pw_lost_txt'));
249+
$app->tpl->setVar('pw_lost_txt', $app->lng('pw_lost_txt'));
246250
$app->tpl->setVar('username_txt', $app->lng('username_txt'));
247251
$app->tpl->setVar('password_txt', $app->lng('password_txt'));
248252
$app->tpl->setVar('login_button_txt', $app->lng('login_button_txt'));
249253
$app->tpl->setInclude('content_tpl','login/templates/index.htm');
250254
$app->tpl_defaults();
251-
255+
252256
$this->status = 'OK';
253-
257+
254258
return $app->tpl->grab();
255-
259+
256260
} // << end function
257261

258262
} // << end class

0 commit comments

Comments
 (0)