Created
May 21, 2011 17:54
-
-
Save becker990/984725 to your computer and use it in GitHub Desktop.
LDAP openLDAP simple authentication method, returns 1 if user and pass are correct
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function ldap_auth($uname,$ldappass,$server_config){ | |
// takes three parameters: array of config | |
// add id address, dist_name is distinguished name, | |
// user and password | |
// returns 1 if user and pass are correct | |
// | |
$basedn = $server_config['dist_name']; | |
$server = $server_config['add']; | |
$port = $server_config['port']; | |
if ((trim($uname) == '') || (trim($ldappass) == '')){ | |
return 'Username or password, empty or invalid.'; | |
} | |
// using openLDAP auth, so is ,<uid> not <id> | |
// using ldap bind | |
$ldaprdn = "uid=$uname,$base_dn"; //openLDAP valid DN!!! 'uid=username,ou=XXXXX,dc=example,dc=org' | |
// connect to ldap server | |
if (!( $ldapconn = ldap_connect($server,$port))){ | |
return $this->erro_ldap($ldapconn); | |
} | |
// binding to ldap server | |
if (ldap_bind($ldapconn, $ldaprdn, $ldappass)){ | |
return 1; | |
}else{ | |
//in any error case returns the ldap error | |
return $this->erro_ldap($ldapconn); | |
} | |
}// fim da func de auth | |
private function erro_ldap($conexao){ | |
// essa funcao faz a traducao dos codigos de erros para pt-BR | |
//retorna uma string com o erro em PT | |
/* | |
LDAP_INVALID_DN_SYNTAX 0x22 | |
LDAP_INVALID_CREDENTIALS 0x31 | |
LDAP_UNAVAILABLE 0x34 | |
LDAP_UNWILLING_TO_PERFORM 0x35 | |
LDAP_TIMEOUT 0x55 | |
LDAP_NO_MEMORY 0x5a | |
*/ | |
$cod_erro = ldap_errno($conexao); | |
switch ($cod_erro) { | |
case -1: | |
return 'Parece haver um problema na sua conexao ao servidor, verifique os cabos e tente novamente.'; | |
case 0x22: | |
return 'LDAP_INVALID_DN_SYNTAX Chame o administrador e denuncie o erro.'; | |
case 0x31: | |
return 'Nome de usuario ou senha invalidos.'; | |
case 0x34: | |
return 'Servidor Indisponivel'; | |
case 0x35: | |
return 'Nome de usuario ou senha invalidos ou vazios. Servidor recusou-os.'; | |
case 0x55: | |
return 'Tempo da operacao esgotado.'; | |
case 0x5a: | |
return 'Servidor com serios problemas, chame o administrador urgente! <h1>LDAP_NO_MEMORY</h1>'; | |
} | |
$unknown_err = ldap_error($conexao); | |
return "Erro desconhecido! $unknown_err codigo:$cod_erro"; | |
}// fim da funcao de traducao |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment