29 require_once
"Auth/Container.php";
33 require_once
"PEAR.php";
232 if (
false === extension_loaded(
'ldap')) {
239 if (is_array($params)) {
280 if (isset($this->options[
'url']) && $this->options[
'url'] !=
'') {
282 $conn_params = array($this->options[
'url']);
285 $conn_params = array($this->options[
'host'], $this->options[
'port']);
288 if (($this->conn_id = @call_user_func_array(
'ldap_connect', $conn_params)) ===
false) {
290 $this->
log(
'LDAP ERROR: '.ldap_errno($this->conn_id).
': '.ldap_error($this->conn_id),
AUTH_LOG_DEBUG);
291 return PEAR::raiseError(
'Auth_Container_LDAP: Could not connect to server.', 41);
296 if (is_numeric($this->options[
'version']) && $this->options[
'version'] > 2) {
297 $this->
log(
"Switching to LDAP version {$this->options['version']}",
AUTH_LOG_DEBUG);
298 @ldap_set_option($this->conn_id, LDAP_OPT_PROTOCOL_VERSION, $this->options[
'version']);
301 if (isset($this->options[
'start_tls']) && $this->options[
'start_tls']) {
303 if (@ldap_start_tls($this->conn_id) ===
false) {
305 $this->
log(
'LDAP ERROR: '.ldap_errno($this->conn_id).
': '.ldap_error($this->conn_id),
AUTH_LOG_DEBUG);
312 if (is_bool($this->options[
'referrals'])) {
313 $this->
log(
"Switching LDAP referrals to " . (($this->options[
'referrals']) ?
'true' :
'false'),
AUTH_LOG_DEBUG);
314 if (@ldap_set_option($this->conn_id, LDAP_OPT_REFERRALS, $this->options[
'referrals']) ===
false) {
316 $this->
log(
'LDAP ERROR: '.ldap_errno($this->conn_id).
': '.ldap_error($this->conn_id),
AUTH_LOG_DEBUG);
321 if (strlen($this->options[
'binddn']) && strlen($this->options[
'bindpw'])) {
323 $bind_params = array($this->conn_id, $this->options[
'binddn'], $this->options[
'bindpw']);
326 $bind_params = array($this->conn_id);
332 if ((@call_user_func_array(
'ldap_bind', $bind_params)) ===
false) {
334 $this->
log(
'LDAP ERROR: '.ldap_errno($this->conn_id).
': '.ldap_error($this->conn_id),
AUTH_LOG_DEBUG);
336 return PEAR::raiseError(
"Auth_Container_LDAP: Could not bind to LDAP server.", 41);
355 $this->
log(
'disconnecting from server');
356 @ldap_unbind($this->conn_id);
376 if ($this->options[
'basedn'] ==
"" && $this->
_isValidLink()) {
379 $result_id = @ldap_read($this->conn_id,
"",
"(objectclass=*)", array(
"namingContexts"));
381 if (@ldap_count_entries($this->conn_id, $result_id) == 1) {
385 $entry_id = @ldap_first_entry($this->conn_id, $result_id);
386 $attrs = @ldap_get_attributes($this->conn_id, $entry_id);
387 $basedn = $attrs[
'namingContexts'][0];
391 $this->options[
'basedn'] = $basedn;
394 @ldap_free_result($result_id);
398 if ($this->options[
'basedn'] ==
"") {
399 return PEAR::raiseError(
"Auth_Container_LDAP: LDAP search base not specified!", 41);
415 if (is_resource($this->conn_id)) {
416 if (get_resource_type($this->conn_id) ==
'ldap link') {
433 $this->options[
'url'] =
'';
434 $this->options[
'host'] =
'localhost';
435 $this->options[
'port'] =
'389';
436 $this->options[
'version'] = 2;
437 $this->options[
'referrals'] =
true;
438 $this->options[
'binddn'] =
'';
439 $this->options[
'bindpw'] =
'';
440 $this->options[
'basedn'] =
'';
441 $this->options[
'userdn'] =
'';
442 $this->options[
'userscope'] =
'sub';
443 $this->options[
'userattr'] =
'uid';
444 $this->options[
'userfilter'] =
'(objectClass=posixAccount)';
445 $this->options[
'attributes'] = array(
'');
446 $this->options[
'attrformat'] =
'AUTH';
447 $this->options[
'group'] =
'';
448 $this->options[
'groupdn'] =
'';
449 $this->options[
'groupscope'] =
'sub';
450 $this->options[
'groupattr'] =
'cn';
451 $this->options[
'groupfilter'] =
'(objectClass=groupOfUniqueNames)';
452 $this->options[
'memberattr'] =
'uniqueMember';
453 $this->options[
'memberisdn'] =
true;
454 $this->options[
'start_tls'] =
false;
455 $this->options[
'debug'] =
false;
456 $this->options[
'try_all'] =
false;
472 foreach ($array as $key => $value) {
473 if (array_key_exists($key, $this->options)) {
474 if ($key ==
'attributes') {
475 if (is_array($value)) {
476 $this->options[$key] = $value;
478 $this->options[$key] = explode(
',', $value);
481 $this->options[$key] = $value;
500 if (isset($array[
'useroc']))
501 $array[
'userfilter'] =
"(objectClass=".$array[
'useroc'].
")";
502 if (isset($array[
'groupoc']))
503 $array[
'groupfilter'] =
"(objectClass=".$array[
'groupoc'].
")";
504 if (isset($array[
'scope']))
505 $array[
'userscope'] = $array[
'scope'];
523 $function =
'ldap_list';
526 $function =
'ldap_read';
529 $function =
'ldap_search';
563 if (@ldap_get_option($this->conn_id, LDAP_OPT_PROTOCOL_VERSION, $ver) && $ver == 3) {
565 $username = utf8_encode($username);
569 $filter = sprintf(
'(&(%s=%s)%s)',
570 $this->options[
'userattr'],
572 $this->options[
'userfilter']);
575 $search_basedn = $this->options[
'userdn'];
576 if ($search_basedn !=
'' && substr($search_basedn, -1) !=
',') {
577 $search_basedn .=
',';
579 $search_basedn .= $this->options[
'basedn'];
582 $searchAttributes = $this->options[
'attributes'];
585 $func_params = array($this->conn_id, $search_basedn, $filter, $searchAttributes);
590 $this->
log(
"Searching with $func_name and filter $filter in $search_basedn",
AUTH_LOG_DEBUG);
593 if (($result_id = @call_user_func_array($func_name, $func_params)) ===
false) {
595 } elseif (@ldap_count_entries($this->conn_id, $result_id) >= 1) {
606 $entry_id = @ldap_first_entry($this->conn_id, $result_id);
609 $entry_id = @ldap_next_entry($this->conn_id, $entry_id);
610 if ($entry_id ===
false)
613 $user_dn = @ldap_get_dn($this->conn_id, $entry_id);
616 if (is_array($searchAttributes) && in_array(
'dn', $searchAttributes)) {
618 $this->_auth_obj->setAuthData(
'dn', $user_dn);
622 if ($attributes = @ldap_get_attributes($this->conn_id, $entry_id)) {
624 if (is_array($attributes) && isset($attributes[
'count']) &&
625 $attributes[
'count'] > 0) {
640 if ( strtoupper($this->options[
'attrformat']) ==
'AUTH' ) {
642 unset ($attributes[
'count']);
643 foreach ($attributes as $attributeName => $attributeValue ) {
644 if (is_int($attributeName))
continue;
645 if (is_array($attributeValue) && isset($attributeValue[
'count'])) {
646 unset ($attributeValue[
'count']);
648 if (count($attributeValue)<=1) $attributeValue = $attributeValue[0];
650 $this->_auth_obj->setAuthData($attributeName, $attributeValue);
656 $this->_auth_obj->setAuthData(
'attributes', $attributes);
660 @ldap_free_result($result_id);
664 if ($password !=
"") {
669 if (@ldap_bind($this->conn_id, $user_dn, $password)) {
673 if (strlen($this->options[
'group'])) {
676 $return = $this->
checkGroup(($this->options[
'memberisdn']) ? $user_dn : $username);
686 }
while ($this->options[
'try_all'] ==
true);
716 $filter = sprintf(
'(&(%s=%s)(%s=%s)%s)',
717 $this->options[
'groupattr'],
718 $this->options[
'group'],
719 $this->options[
'memberattr'],
721 $this->options[
'groupfilter']);
724 $search_basedn = $this->options[
'groupdn'];
725 if ($search_basedn !=
'' && substr($search_basedn, -1) !=
',') {
726 $search_basedn .=
',';
728 $search_basedn .= $this->options[
'basedn'];
730 $func_params = array($this->conn_id, $search_basedn, $filter,
731 array($this->options[
'memberattr']));
734 $this->
log(
"Searching with $func_name and filter $filter in $search_basedn",
AUTH_LOG_DEBUG);
737 if (($result_id = @call_user_func_array($func_name, $func_params)) !=
false) {
738 if (@ldap_count_entries($this->conn_id, $result_id) == 1) {
739 @ldap_free_result($result_id);
760 $metas = array(
'\\',
'*',
'(',
')',
"\x00");
761 $quoted_metas = array(
'\\\\',
'\*',
'\(',
'\)',
"\\\x00");
762 return str_replace($metas, $quoted_metas, $filter_str);