Authentification LDAP, ldap_sasl_bind_s ne fonctionne pas mais ldap_simple_bind_s fonctionne

J’ai un problème où dans ldap_sasl_bind_s ne fonctionne pas, mais ldap_simple_bind_s fonctionne.

La chose étrange est que ldap_sasl_bind_s fonctionne même avec des mots de passe erronés et donne à l’utilisateur l’impression qu’il a saisi un mot de passe correct.

Extrait de code PFA du problème et me suggérer si quelque chose ne va pas avec mon approche.

{ int rc, aReturnVal = 0; NSSsortingng *aUserDN = [NSSsortingng ssortingngWithFormat:@"uid=%s,cn=users,dc=example,dc=com", username]; char* userDN = (char*)[aUserDN UTF8Ssortingng]; rc = ldap_simple_bind_s ( ld, userDN, password ); // TODO: ldap_simple_bind_s is a deprecated method and should not be used for long. ldap_sasl_bind_s is the right method, but is not working for now. // Find the reason and get this code up and running. // struct berval *servcred; // struct berval cred; // cred.bv_val = password; // my password // cred.bv_len = strlen(password); // rc = ldap_sasl_bind_s ( // ld, // userDN, // "DIGEST-MD5", // &cred, // NULL, // NULL, // &servcred // ); if ( rc != LDAP_SUCCESS ) { fprintf( stderr, "ldap_sasl_bind: %s\n", ldap_err2ssortingng( rc ) ); } else { aReturnVal = 1; } return aReturnVal; } 

J’ai initialisé le LDAP en utilisant le code SNIP suivant:

 rc = ldap_initialize(&ld, HOSTNAME); version = LDAP_VERSION3; ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ); ldap_set_option( ld, LDAP_OPT_REFERRALS, 0 ); 

Je dois pouvoir me connecter avec le nom d’utilisateur correct et lorsque l’utilisateur tente d’entrer un nom d’utilisateur incorrect, ldap devrait le dire.

J’ai mentionné les liens suivants et leurs liens associés pour parvenir à cette conclusion:

LDAP – Comment vérifier une combinaison nom d’utilisateur / mot de passe?

Comment authentifier un mot de passe pour un utilisateur utilisant LDAP?

L’authentification Digest-MD5 est plus compliquée que simplement envoyer un DN de liaison et un mot de passe. Vous devez utiliser ldap_sasl_interactive_bind_s et fournir un rappel afin que la bibliothèque SASL puisse combiner vos informations d’identification avec le nonce fourni par le serveur.

Ce code (adapté de cet article de blog ) fonctionne pour moi sur un serveur Active Directory:

 #include  #include  #include  #include  typedef struct { char *username; char *password; } my_authdata; int my_sasl_interact(LDAP *ld, unsigned flags, void *defaults, void *in) { my_authdata *auth = (my_authdata *)defaults; sasl_interact_t *interact = (sasl_interact_t *)in; if(ld == NULL) return LDAP_PARAM_ERROR; while(interact->id != SASL_CB_LIST_END) { char *dflt = (char *)interact->defresult; switch(interact->id) { case SASL_CB_GETREALM: dflt = NULL; break; case SASL_CB_USER: case SASL_CB_AUTHNAME: dflt = auth->username; break; case SASL_CB_PASS: dflt = auth->password; break; default: printf("my_sasl_interact asked for unknown %ld\n",interact->id); } interact->result = (dflt && *dflt) ? dflt : (char *)""; interact->len = strlen((char *)interact->result); interact++; } return LDAP_SUCCESS; } int main(int argc, char *argv[]) { if(argc < 3) { fprintf(stderr, "Usage: dmd5-bind [username] [password]\n"); return -1; } int rc; LDAP *ld = NULL; static my_authdata auth; auth.username = argv[1]; auth.password = argv[2]; char *sasl_mech = ber_strdup("DIGEST-MD5"); char *ldapuri = ber_strdup("ldap://your.server.name.here"); int protocol = LDAP_VERSION3; unsigned sasl_flags = LDAP_SASL_QUIET; char *binddn = NULL; rc = ldap_initialize(&ld, ldapuri); if(rc != LDAP_SUCCESS) { fprintf(stderr, "ldap_initialize: %s\n", ldap_err2string(rc)); return rc; } if(ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &protocol) != LDAP_OPT_SUCCESS) { fprintf(stderr, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", protocol); return -1; } rc = ldap_sasl_interactive_bind_s(ld, binddn, sasl_mech, NULL, NULL, sasl_flags, my_sasl_interact, &auth); if(rc != LDAP_SUCCESS) { ldap_perror(ld, "ldap_sasl_interactive_bind_s"); ldap_unbind_ext_s(ld, NULL, NULL); return rc; } fprintf(stdout, "Authentication succeeded\n"); rc = ldap_unbind_ext_s(ld, NULL, NULL); sasl_done(); sasl_client_init(NULL); return rc; }