44function 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
2025function 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) {
90106function 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
130155function 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
142168function 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}
0 commit comments