我徒劳地搜索了如何实现OpenLDAP函数ldap_sasl_bind
。
正在开发的应用程序要求我使用OpenLDAP访问Active Directory。我必须绑定到AD,我看过的每个示例程序都使用ldap_simple_bind
或类似的函数,这些函数已被弃用,因此我无法使用它们。
ldap_sasl_bind
函数有一个用户凭据参数,我必须提供用户名和密码进行身份验证。问题是cred参数接受指向struct berval
的指针,我不知道如何实现。
microsoft docs中berval
的定义如下:
typedef struct berval {
ULONG bv_len;
PCHAR bv_val;
} LDAP_BERVAL, *PLDAP_BERVAL, BERVAL, *PBERVAL, BerValue;
这是OpenLDAP中的ldap_sasl_bind
函数定义:
int ldap_sasl_bind(LDAP *ld, const char *dn, const char
*mechanism, struct berval *cred, LDAPControl *sctrls[],
LDAPControl *cctrls[], int *msgidp);
我只需访问bv_val
并为其分配值即可提供用户名,我可以在bv_len
中提供用户名的长度。但是我该如何提供密码?
BerValue bv_val
应该是指向密码字符串的二进制表示的指针,因为函数ldap_sasl_bind
使用传入的绑定dn
作为第二个参数,所以您不需要在此berval中提供用户名。
类似下面的东西应该工作(C ++):
berval cred;
cred.bv_val = (char*)password.c_str();
cred.bv_len = password.length();
res = ldap_sasl_bind(ld, dn, LDAP_SASL_SIMPLE, cred, sctrls, cctrls, msgidp);
常数LDAP_SASL_SIMPLE
相当于''
或NULL
。