@@ -109,6 +109,13 @@ abstract class REST_Controller extends CI_Controller
109109 */
110110 protected $ _zlib_oc = FALSE ;
111111
112+ /**
113+ * The LDAP Distinguished Name of the User post authentication
114+ *
115+ * @var string
116+ */
117+ protected $ _user_ldap_dn = ''
118+
112119 /**
113120 * List all supported methods, the first will be the default format
114121 *
@@ -963,6 +970,94 @@ public function validation_errors()
963970
964971 // SECURITY FUNCTIONS ---------------------------------------------------------
965972
973+ /**
974+ * Perform LDAP Authentication
975+ *
976+ * @param string $username The username to validate
977+ * @param string $password The password to validate
978+ * @return boolean
979+ */
980+ protected function _perform_ldap_auth ($ username = '' , $ password = NULL )
981+ {
982+ if (empty ($ username ))
983+ {
984+ log_message ('debug ' , 'LDAP Auth: failure, empty username ' );
985+ return false ;
986+ }
987+
988+ log_message ('debug ' , 'LDAP Auth: Loading Config ' );
989+
990+ $ this ->config ->load ('ldap.php ' , true );
991+
992+ $ ldaptimeout = $ this ->config ->item ('timeout ' , 'ldap ' );
993+ $ ldaphost = $ this ->config ->item ('server ' , 'ldap ' );
994+ $ ldapport = $ this ->config ->item ('port ' , 'ldap ' );
995+ $ ldaprdn = $ this ->config ->item ('binduser ' , 'ldap ' );
996+ $ ldappass = $ this ->config ->item ('bindpw ' , 'ldap ' );
997+ $ ldapbasedn = $ this ->config ->item ('basedn ' , 'ldap ' );
998+
999+ log_message ('debug ' , 'LDAP Auth: Connect to ' . $ ldaphost );
1000+
1001+ $ ldapconfig ['authrealm ' ] = $ this ->config ->item ('domain ' , 'ldap ' );
1002+
1003+ // connect to ldap server
1004+ $ ldapconn = ldap_connect ($ ldaphost , $ ldapport );
1005+
1006+ if ($ ldapconn ) {
1007+
1008+ log_message ('debug ' , 'Setting timeout to ' . $ ldaptimeout . ' seconds ' );
1009+
1010+ ldap_set_option ($ ldapconn , LDAP_OPT_NETWORK_TIMEOUT , $ ldaptimeout );
1011+
1012+ log_message ('debug ' , 'LDAP Auth: Binding to ' . $ ldaphost . ' with dn ' . $ ldaprdn );
1013+
1014+ // binding to ldap server
1015+ $ ldapbind = ldap_bind ($ ldapconn , $ ldaprdn , $ ldappass );
1016+
1017+ // verify binding
1018+ if ($ ldapbind ) {
1019+ log_message ('debug ' , 'LDAP Auth: bind successful ' );
1020+ } else {
1021+ log_message ('error ' , 'LDAP Auth: bind unsuccessful ' );
1022+ return false ;
1023+ }
1024+
1025+ }
1026+
1027+ // search for user
1028+ if (($ res_id = ldap_search ( $ ldapconn , $ ldapbasedn , "uid= $ username " )) == false ) {
1029+ log_message ('error ' , 'LDAP Auth: User ' . $ username . ' not found in search ' );
1030+ return false ;
1031+ }
1032+
1033+ if (ldap_count_entries ($ ldapconn , $ res_id ) != 1 ) {
1034+ log_message ('error ' , 'LDAP Auth: failure, username ' . $ username . 'found more than once ' );
1035+ return false ;
1036+ }
1037+
1038+ if (( $ entry_id = ldap_first_entry ($ ldapconn , $ res_id ))== false ) {
1039+ log_message ('error ' , 'LDAP Auth: failure, entry of searchresult could not be fetched ' );
1040+ return false ;
1041+ }
1042+
1043+ if (( $ user_dn = ldap_get_dn ($ ldapconn , $ entry_id )) == false ) {
1044+ log_message ('error ' , 'LDAP Auth: failure, user-dn could not be fetched ' );
1045+ return false ;
1046+ }
1047+
1048+ // User found, could not authenticate as user
1049+ if (($ link_id = ldap_bind ($ ldapconn , $ user_dn , $ password )) == false ) {
1050+ log_message ('error ' , 'LDAP Auth: failure, username/password did not match: ' . $ user_dn );
1051+ return false ;
1052+ }
1053+
1054+ log_message ('debug ' , 'LDAP Auth: Success ' . $ user_dn . ' authenticated successfully ' );
1055+
1056+ $ this ->_user_ldap_dn = $ user_dn ;
1057+ ldap_close ($ ldapconn );
1058+ return true ;
1059+ }
1060+
9661061 /**
9671062 * Check if the user is logged in.
9681063 *
@@ -977,6 +1072,14 @@ protected function _check_login($username = '', $password = NULL)
9771072 return FALSE ;
9781073 }
9791074
1075+ $ auth_source = strtolower ($ this -config->item ('auth_source ' ));
1076+
1077+ if ($ auth_source == 'ldap ' )
1078+ {
1079+ log_message ('debug ' , 'performing LDAP authentication for $username ' );
1080+ return $ this ->_perform_ldap_auth ($ username , $ password );
1081+ }
1082+
9801083 $ valid_logins = & $ this ->config ->item ('rest_valid_logins ' );
9811084
9821085 if ( ! array_key_exists ($ username , $ valid_logins ))
0 commit comments