
ldap_get_entries 함수는 LDAP 검색 결과를 얻어오는 함수입니다. 이 함수를 사용하여 검색 결과를 얻어오기 위해서는 LDAP 서버의 호스트 이름, 포트 번호, 사용자 ID, 비밀번호, 검색 조건을 입력해야 합니다.
LDAP 서버의 호스트 이름과 포트 번호는 LDAP 서버의 주소로 사용됩니다. 사용자 ID와 비밀번호는 LDAP 서버에 접근하기 위한 인증 정보입니다. 검색 조건은 LDAP 서버에서 검색할 데이터의 필드를 지정합니다.
예를 들어, LDAP 서버의 사용자 정보를 검색할 때, 사용자 ID와 비밀번호는 다음과 같은 형식으로 입력해야 합니다.
- 호스트 이름: "ldap.example.com"
- 포트 번호: 389 (LDAP 기본 포트)
- 사용자 ID: "cn=admin,dc=example,dc=com"
- 비밀번호: "password"
- 검색 조건: "(objectclass=person)"
ldap_get_entries 함수를 사용하여 LDAP 검색 결과를 얻어오기 위한 예제 코드는 다음과 같습니다.
#hostingforum.kr
c
#include
int main() {
LDAP *ld;
BerValue *bv;
int rc;
struct berval *bv_array;
LDAPMessage *msg;
int count;
int i;
// LDAP 서버 연결
ld = ldap_init("ldap.example.com", 389);
if (ld == NULL) {
printf("LDAP 서버 연결 실패n");
return 1;
}
// LDAP 서버 인증
rc = ldap_bind_s(ld, "cn=admin,dc=example,dc=com", "password", LDAP_AUTH_SIMPLE);
if (rc != LDAP_SUCCESS) {
printf("LDAP 서버 인증 실패n");
ldap_unbind_ext_s(ld, NULL, NULL);
return 1;
}
// LDAP 검색
bv = ldap_malloc(sizeof(BerValue));
bv->bv_val = ldap_malloc(1024);
bv->bv_len = 1024;
strcpy(bv->bv_val, "(objectclass=person)");
bv_array = ldap_malloc(sizeof(struct berval));
bv_array->bv_val = ldap_malloc(1024);
bv_array->bv_len = 1024;
strcpy(bv_array->bv_val, "cn");
rc = ldap_search_ext_s(ld, "dc=example,dc=com", LDAP_SCOPE_SUBTREE, bv->bv_val, bv_array, 0, NULL, NULL, NULL, 0, NULL, &msg);
if (rc != LDAP_SUCCESS) {
printf("LDAP 검색 실패n");
ldap_unbind_ext_s(ld, NULL, NULL);
return 1;
}
// LDAP 검색 결과 처리
count = ldap_count_entries(ld, msg);
printf("LDAP 검색 결과 개수: %dn", count);
for (i = 0; i < count; i++) {
printf("LDAP 검색 결과 %d:n", i + 1);
printf(" cn: %sn", ldap_get_values_len(ld, msg, "cn"));
}
// LDAP 검색 결과 해제
ldap_msgfree(msg);
// LDAP 서버 연결 해제
ldap_unbind_ext_s(ld, NULL, NULL);
return 0;
}
이 예제 코드는 LDAP 서버에 접속하여 사용자 정보를 검색하고, 검색 결과를 처리합니다. LDAP 서버의 호스트 이름, 포트 번호, 사용자 ID, 비밀번호, 검색 조건을 입력하여 LDAP 검색 결과를 얻어올 수 있습니다.
2025-05-29 16:26