Skip to content

Commit 1ac3c05

Browse files
author
Phil Sturgeon
committed
Merge pull request chriskacerguis#149 from johann8384/master
adding LDAP support
2 parents 07ac2e6 + 44f5959 commit 1ac3c05

File tree

3 files changed

+138
-2
lines changed

3 files changed

+138
-2
lines changed

application/config/ldap.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
$config['binduser'] = 'cn=Authentication,ou=Services,dc=example,dc=org';
4+
$config['basedn'] = 'dc=example,dc=org';
5+
$config['bindpw'] = 'E984asdy2';
6+
7+
/*
8+
* The host name parameter can be a space separated list of host names.
9+
* This means that the LDAP code will talk to a backup server if the main server is not operational.
10+
* There will be a delay while the code times out trying to talk to the main server but things will still work.
11+
*/
12+
13+
$config['server'] = 'ldapserver1.example.org ldapserver2.example.org';
14+
$config['port'] = NULL;
15+
16+
/*
17+
* Controls the LDAP_OPT_NETWORK_TIMEOUT option, this is how long the code will attempt to talk to the primary server if it is unreachable.
18+
*/
19+
20+
$config['timeout'] = 5;
21+
?>

application/config/rest.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,18 @@
6161
*/
6262
$config['rest_auth'] = false;
6363

64+
/*
65+
|--------------------------------------------------------------------------
66+
| REST Login
67+
|--------------------------------------------------------------------------
68+
|
69+
| Is login required and if so, which user store do we use?
70+
|
71+
| '' = use config based users, 'ldap' = use LDAP authencation
72+
|
73+
*/
74+
$config['auth_source'] = 'ldap';
75+
6476
/*
6577
|--------------------------------------------------------------------------
6678
| Override auth types for specific class/method
@@ -89,7 +101,7 @@
89101
| REST Login usernames
90102
|--------------------------------------------------------------------------
91103
|
92-
| Array of usernames and passwords for login
104+
| Array of usernames and passwords for login, if ldap is configured this is ignored
93105
|
94106
| array('admin' => '1234')
95107
|
@@ -315,4 +327,4 @@
315327
$config['rest_ajax_only'] = FALSE;
316328

317329
/* End of file config.php */
318-
/* Location: ./system/application/config/rest.php */
330+
/* Location: ./system/application/config/rest.php */

application/libraries/REST_Controller.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)