compare_a.c

来自「操做 openldap 的c语言源码」· C语言 代码 · 共 74 行

C
74
字号
#include "ldap.h"
#include "stdio.h"


int main()
{
  LDAP  *ld;
  int version;
  char *attrname;
  char *attrvalue;

  int rc_result;            /* ldap_result返回结果 */ 
    int rc_result2error;      /* ldap_result2error返回结果 */
    LDAPMessage *res;  
    int msgid; 

    int finish = 0;     

   if( (ld = ldap_open( "192.168.3.3", 389 ))  == NULL )
        exit( 1 );
   version = LDAP_VERSION3;

   ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION,&version);
   if(ldap_simple_bind_s(ld,"cn=root,dc=starxing,dc=com","secret")!=LDAP_SUCCESS)
   {
     ldap_perror( ld, "ldap_simple_bind_s" );
     exit( 1 );
   }

    attrname = "sn";
    attrvalue = "starxing";

  msgid = ldap_compare(ld, "cn=starxing,dc=starxing,dc=com", attrname, attrvalue);

    if (msgid == -1)
    {
        ldap_perror(ld, "ldap_compare error");
        ldap_unbind(ld);
        return -1;
    }

    while (!finish)
    {
        rc_result = ldap_result(ld, msgid, LDAP_MSG_ONE, NULL, &res);
    
        switch (rc_result)
        {
            case -1:
                ldap_perror(ld, "ldap_result");
                ldap_unbind(ld);  

                return -1;
            case 0:
                break;
            case LDAP_RES_COMPARE:
                finish = 1;
                break;
            default:
                ldap_unbind(ld);  
                return -1;
        }

     
    }

    rc_result2error = ldap_result2error(ld, res, 0);
    printf("ldap_compare: %s\n", ldap_err2string(rc_result2error)); 
    
    ldap_msgfree(res);
    ldap_unbind(ld);
    
  return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?