
ldap_read 함수는 LDAP 서버에서 데이터를 검색하여 결과를 가져올 수 있는 함수입니다.
ldap_read 함수의 첫 번째 인자는 LDAP 연결 객체를 받고 두 번째 인자는 LDAP 검색 질의를 받습니다. 질의를 설정한 후 ldap_read 함수를 호출하여 LDAP 서버에서 데이터를 검색할 수 있습니다.
ldap_read 함수의 결과는 LDAP 결과 구조체에 저장됩니다. 이 구조체는 LDAP 결과의 각 항목을 포함합니다.
LDAP 결과 구조체의 각 항목은 LDAP 결과 항목 구조체로 구성됩니다. 이 구조체에는 항목의 DN, 속성 이름, 속성 값 등이 포함됩니다.
ldap_read 함수의 결과를 처리하는 방법은 다음과 같습니다.
1. LDAP 연결 객체를 열고 LDAP 서버에 연결합니다.
2. LDAP 검색 질의를 설정합니다.
3. ldap_read 함수를 호출하여 LDAP 서버에서 데이터를 검색합니다.
4. LDAP 결과 구조체를 가져와 각 항목을 처리합니다.
5. LDAP 결과 구조체의 각 항목을 LDAP 결과 항목 구조체로 분리합니다.
6. LDAP 결과 항목 구조체의 속성 이름과 속성 값을 가져와 처리합니다.
예를 들어, LDAP 서버에서 사용자 정보를 검색하는 경우 다음과 같이 처리할 수 있습니다.
#hostingforum.kr
c
// LDAP 연결 객체를 열고 LDAP 서버에 연결합니다.
LDAP* ld = ldap_init("ldap://localhost:389", NULL);
// LDAP 검색 질의를 설정합니다.
LDAPMessage* msg;
char* filter = "(&(objectClass=user)(cn=*))";
int rc = ldap_search_ext_s(ld, "dc=example,dc=com", LDAP_SCOPE_SUBTREE, filter, NULL, 0, NULL, NULL, 0, 0, &msg, NULL);
// ldap_read 함수를 호출하여 LDAP 서버에서 데이터를 검색합니다.
LDAPMessage* result = ldap_read(ld, msg, LDAP_SCOPE_SUBTREE, filter, NULL);
// LDAP 결과 구조체를 가져와 각 항목을 처리합니다.
LDAPMessage* entry;
int count = ldap_count_entries(ld, msg);
for (int i = 0; i < count; i++) {
entry = ldap_first_entry(ld, msg);
char* dn = ldap_get_dn(ld, entry);
char* attr_name = ldap_first_attribute(ld, entry, &msg);
char* attr_value = ldap_first_value(ld, msg);
printf("DN: %sn", dn);
printf("Attribute Name: %sn", attr_name);
printf("Attribute Value: %sn", attr_value);
ldap_memfree(dn);
ldap_memfree(attr_name);
ldap_memfree(attr_value);
}
// LDAP 결과 구조체와 연결 객체를 닫습니다.
ldap_msgfree(msg);
ldap_unbind_ext_s(ld, NULL, NULL);
이 예제에서는 LDAP 서버에서 사용자 정보를 검색하고 결과를 처리하는 방법을 보여줍니다.
2025-04-29 03:28