Skip to content

Commit 75e8c4b

Browse files
committed
Added extra logging support via Monolog.
1 parent 5a1cdea commit 75e8c4b

File tree

5 files changed

+156
-15
lines changed

5 files changed

+156
-15
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"require": {
3-
"directorytree/ldaprecord": "^2.2"
3+
"directorytree/ldaprecord": "^2.2",
4+
"monolog/monolog": "^2.2"
45
}
56
}

composer.lock

Lines changed: 97 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configuration.example.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
<?php
22
defined('_SFTPGO') or die;
33
define('_SFTPGO_CLI', PHP_SAPI === 'cli');
4-
define("_SFTPGO_DEBUG", false);
4+
define('_SFTPGO_LOG', false);
5+
define('_SFTPGO_DEBUG', false);
6+
define('_SFTPGO_DEBUG_ENV', false);
57

68
use LdapRecord\Connection;
9+
use Monolog\Logger;
10+
use Monolog\Handler\RotatingFileHandler;
11+
12+
// create a log channel
13+
$log = new Logger('name');
14+
$log->pushHandler(new RotatingFileHandler('logs/sftpgo-ldap.log', 30, Logger::DEBUG));
715

816
// If the debug flag is set to true, please set the username/password directly for the LDAP user you want to test below:
917
$debug_object = '{"username":"test","password":"test","ip":"::1","keyboard_interactive":"","protocol":"SSH","public_key":""}';

functions.php

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,28 @@
44
function isAllowedIP() {
55
global $allowed_ips;
66

7-
if (_SFTPGO_CLI) {
7+
logMessage('Starting Execution Process');
8+
if (defined('_SFTPGO_CLI') && _SFTPGO_CLI === true) {
9+
logMessage('CLI Execution Mode...skipping Allowed IP check');
810
return true;
911
}
1012

1113
$remoteIP = $_SERVER['REMOTE_ADDR'];
1214

1315
if (array_search($remoteIP, $allowed_ips) !== false) {
16+
logMessage('Web Execution Mode...' . $remoteIP . ' is allowed.');
1417
return true;
1518
}
1619

20+
logMessage('Web Execution Mode...' . $remoteIP . ' is not allowed.');
21+
1722
denyRequest();
1823
}
1924

2025
function authenticateUser() {
26+
logMessage('Before getData()');
2127
$data = getData();
28+
logMessage('After getData()');
2229

2330
if (!empty($data)) {
2431

@@ -27,7 +34,9 @@ function authenticateUser() {
2734

2835
foreach($connections as $connectionName => $connection) {
2936

37+
logMessage('Before connection attempt to ' . $connectionName);
3038
$connection->connect();
39+
logMessage('After connection attempt to ' . $connectionName);
3140

3241
$configuration = $connection->getConfiguration();
3342
$baseDn = $configuration->get('base_dn');
@@ -40,18 +49,25 @@ function authenticateUser() {
4049
->first();
4150

4251
if ($user) {
52+
logMessage('Username exists: ' . $data['username']);
4353
// Our user is a member of one of the allowed groups.
4454
// Continue with authentication.
4555
$userDistinguishedName = $user['distinguishedname'][0];
56+
57+
logMessage('Before authentication attempt for: ' . $data['username']);
4658
if ($connection->auth()->attempt($userDistinguishedName, $data['password'])) {
4759
// User has been successfully authenticated.
60+
logMessage('After authentication attempt for: ' . $data['username'] . ' (success!)');
4861
$output = createResponseObject($connectionName, $data['username']);
4962
createResponse($output);
5063
} else {
5164
// Username or password is incorrect.
65+
logMessage('After authentication attempt for: ' . $data['username'] . ' (failed!)');
5266
denyRequest();
5367
}
5468
}
69+
70+
logMessage('User lookup failed for: ' . $data['username']);
5571
}
5672

5773
} catch (\LdapRecord\Auth\BindException $e) {
@@ -90,20 +106,29 @@ function createResponseObject($connectionName, $username) {
90106
function getData() {
91107
if (defined('_SFTPGO_DEBUG') && _SFTPGO_DEBUG === true) {
92108
global $debug_object;
109+
logMessage('Using $debug_object from configuration.php (authentication may fail if this object does not have correct credentials at the moment.)');
93110
$data = $debug_object;
94111
}
95112

96113
if (!isset($data)) {
97114
$data = [];
98-
if (_SFTPGO_CLI) {
99-
if (isset($_ENV['SFTPGO_AUTHD_USERNAME']) && isset($_ENV['SFTPGO_AUTHD_PASSWORD'])) {
100-
$username = $_ENV['SFTPGO_AUTHD_USERNAME'];
101-
$password = $_ENV['SFTPGO_AUTHD_PASSWORD'];
102-
$ip = $_ENV['SFTPGO_AUTHD_IP'];
103-
$protocol = $_ENV['SFTPGO_AUTHD_PROTOCOL'];
104-
$public_key = $_ENV['SFTPGO_AUTHD_PUBLIC_KEY'];
105-
$keyboard_interactive = $_ENV['SFTPGO_AUTHD_KEYBOARD_INTERACTIVE'];
106-
$tls_cert = $_ENV['SFTPGO_AUTHD_TLS_CERT'];
115+
if (defined('_SFTPGO_CLI') && _SFTPGO_CLI === true) {
116+
$environment = getenv();
117+
118+
if (defined('_SFTPGO_DEBUG_ENV') && _SFTPGO_DEBUG_ENV === true) {
119+
echo json_encode($environment, true);
120+
sleep(15);
121+
exit;
122+
}
123+
124+
if (isset($environment['SFTPGO_AUTHD_USERNAME']) && isset($environment['SFTPGO_AUTHD_PASSWORD'])) {
125+
$username = $environment['SFTPGO_AUTHD_USERNAME'];
126+
$password = $environment['SFTPGO_AUTHD_PASSWORD'];
127+
$ip = $environment['SFTPGO_AUTHD_IP'];
128+
$protocol = $environment['SFTPGO_AUTHD_PROTOCOL'];
129+
$public_key = $environment['SFTPGO_AUTHD_PUBLIC_KEY'];
130+
$keyboard_interactive = $environment['SFTPGO_AUTHD_KEYBOARD_INTERACTIVE'];
131+
$tls_cert = $environment['SFTPGO_AUTHD_TLS_CERT'];
107132

108133
$data = [
109134
'username' => $username,
@@ -128,19 +153,20 @@ function getData() {
128153
}
129154

130155
function createResponse($output) {
131-
if (_SFTPGO_CLI) {
156+
if (defined('_SFTPGO_CLI') && _SFTPGO_CLI === true) {
132157
echo json_encode($output);
133158
} else {
134159
http_response_code(200);
135160
header('Content-Type: application/json');
136161
echo json_encode($output);
137162
}
138163

164+
logMessage('Authentication Successful');
139165
exit;
140166
}
141167

142168
function denyRequest() {
143-
if (_SFTPGO_CLI) {
169+
if (defined('_SFTPGO_CLI') && _SFTPGO_CLI === true) {
144170
$output = [
145171
'username' => ''
146172
];
@@ -149,6 +175,7 @@ function denyRequest() {
149175
http_response_code(500);
150176
}
151177

178+
logMessage('Authentication Failed');
152179
exit;
153180
}
154181

@@ -185,4 +212,12 @@ function homeDirectoryEntriesExist() {
185212
echo "Missing Home Directory Entry for: " . $connectionName . '<br />';
186213
}
187214
}
215+
}
216+
217+
function logMessage($message, $extra = []) {
218+
if (defined('_SFTPGO_LOG') && _SFTPGO_LOG === true) {
219+
global $log;
220+
221+
$log->info($message, $extra);
222+
}
188223
}

logs/.htaccess

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
deny from all

0 commit comments

Comments
 (0)