@@ -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 *
@@ -909,6 +916,94 @@ public function validation_errors()
909916
910917 // SECURITY FUNCTIONS ---------------------------------------------------------
911918
919+ /**
920+ * Perform LDAP Authentication
921+ *
922+ * @param string $username The username to validate
923+ * @param string $password The password to validate
924+ * @return boolean
925+ */
926+ protected function _perform_ldap_auth ($ username = '' , $ password = NULL )
927+ {
928+ if (empty ($ username ))
929+ {
930+ log_message ('debug ' , 'LDAP Auth: failure, empty username ' );
931+ return false ;
932+ }
933+
934+ log_message ('debug ' , 'LDAP Auth: Loading Config ' );
935+
936+ $ this ->config ->load ('ldap.php ' , true );
937+
938+ $ ldaptimeout = $ this ->config ->item ('timeout ' , 'ldap ' );
939+ $ ldaphost = $ this ->config ->item ('server ' , 'ldap ' );
940+ $ ldapport = $ this ->config ->item ('port ' , 'ldap ' );
941+ $ ldaprdn = $ this ->config ->item ('binduser ' , 'ldap ' );
942+ $ ldappass = $ this ->config ->item ('bindpw ' , 'ldap ' );
943+ $ ldapbasedn = $ this ->config ->item ('basedn ' , 'ldap ' );
944+
945+ log_message ('debug ' , 'LDAP Auth: Connect to ' . $ ldaphost );
946+
947+ $ ldapconfig ['authrealm ' ] = $ this ->config ->item ('domain ' , 'ldap ' );
948+
949+ // connect to ldap server
950+ $ ldapconn = ldap_connect ($ ldaphost , $ ldapport );
951+
952+ if ($ ldapconn ) {
953+
954+ log_message ('debug ' , 'Setting timeout to ' . $ ldaptimeout . ' seconds ' );
955+
956+ ldap_set_option ($ ldapconn , LDAP_OPT_NETWORK_TIMEOUT , $ ldaptimeout );
957+
958+ log_message ('debug ' , 'LDAP Auth: Binding to ' . $ ldaphost . ' with dn ' . $ ldaprdn );
959+
960+ // binding to ldap server
961+ $ ldapbind = ldap_bind ($ ldapconn , $ ldaprdn , $ ldappass );
962+
963+ // verify binding
964+ if ($ ldapbind ) {
965+ log_message ('debug ' , 'LDAP Auth: bind successful ' );
966+ } else {
967+ log_message ('error ' , 'LDAP Auth: bind unsuccessful ' );
968+ return false ;
969+ }
970+
971+ }
972+
973+ // search for user
974+ if (($ res_id = ldap_search ( $ ldapconn , $ ldapbasedn , "uid= $ username " )) == false ) {
975+ log_message ('error ' , 'LDAP Auth: User ' . $ username . ' not found in search ' );
976+ return false ;
977+ }
978+
979+ if (ldap_count_entries ($ ldapconn , $ res_id ) != 1 ) {
980+ log_message ('error ' , 'LDAP Auth: failure, username ' . $ username . 'found more than once ' );
981+ return false ;
982+ }
983+
984+ if (( $ entry_id = ldap_first_entry ($ ldapconn , $ res_id ))== false ) {
985+ log_message ('error ' , 'LDAP Auth: failure, entry of searchresult could not be fetched ' );
986+ return false ;
987+ }
988+
989+ if (( $ user_dn = ldap_get_dn ($ ldapconn , $ entry_id )) == false ) {
990+ log_message ('error ' , 'LDAP Auth: failure, user-dn could not be fetched ' );
991+ return false ;
992+ }
993+
994+ // User found, could not authenticate as user
995+ if (($ link_id = ldap_bind ($ ldapconn , $ user_dn , $ password )) == false ) {
996+ log_message ('error ' , 'LDAP Auth: failure, username/password did not match: ' . $ user_dn );
997+ return false ;
998+ }
999+
1000+ log_message ('debug ' , 'LDAP Auth: Success ' . $ user_dn . ' authenticated successfully ' );
1001+
1002+ $ this ->_user_ldap_dn = $ user_dn ;
1003+ ldap_close ($ ldapconn );
1004+ return true ;
1005+ }
1006+
9121007 /**
9131008 * Check if the user is logged in.
9141009 *
@@ -923,6 +1018,14 @@ protected function _check_login($username = '', $password = NULL)
9231018 return FALSE ;
9241019 }
9251020
1021+ $ auth_source = strtolower ($ this -config->item ('auth_source ' ));
1022+
1023+ if ($ auth_source == 'ldap ' )
1024+ {
1025+ log_message ('debug ' , 'performing LDAP authentication for $username ' );
1026+ return $ this ->_perform_ldap_auth ($ username , $ password );
1027+ }
1028+
9261029 $ valid_logins = & $ this ->config ->item ('rest_valid_logins ' );
9271030
9281031 if ( ! array_key_exists ($ username , $ valid_logins ))
0 commit comments