Open Ldap 에서 query 하기..

litdream의 이미지

안녕하세요.
LDAP 라이브러리는 처음 써보는데, 갑작스레 간단히 로그인 정보를 확인하는 프로그램을 짜게 되었습니다.
리눅스에서 OpenLdap 을 써서 하려는데, 지금 테스트 코드에서도 헤메고 있습니다.

retreive 부분에서 성공을 하지 못하고 있습니다.
서버는 LDAPS(636) 으로만 통신을 하고 있습니다. 389는 timeout..
일반적으로 ldapsearch -x -h hostname -b "dn=....." 으로 하면 결과가 돌아옵니다만,
이것과 똑같은 테스트 코드를 만들어서 쿼리를 날리는데 retreive 에서 실패를 합니다.

#include <stdio.h>
#include <sys/time.h>
#include <ldap.h>

int main() 
{
	LDAP 	      *session;

	//LDAPMessage  **chain;
	LDAPMessage  *chain[1000];

	int  result, err;
	int  count;
	const char *base = "dc=people,dc=ldap,dc=umd,dc=edu";
	char filter[] = "uid=106364747";
	struct timeval   timeout = {10,0};
	
	if ( (session = ldap_init("ldaptest.umd.edu", LDAPS_PORT)) == NULL ) {
	//if ( (session = ldap_init("ldaptest.umd.edu", LDAP_PORT)) == NULL ) {
		fprintf(stderr, "ldap open fail.\n");
		exit(1);
	}
	printf ("ldap open success.\n");

	// Bind
	if ( (err=ldap_simple_bind(session, "", "")) == -1 ) {
		fprintf(stderr, "ldap bind fail.\n");
		exit(1);
	}
	printf ("ldap bind success. Rtncode = %d\n",err);

	// first query
	if ( (err = ldap_search(session, base, LDAP_SCOPE_SUBTREE, \
			filter , NULL, 0)) < 0) {
		fprintf(stderr, "ldap query fail. Errcode=%d\n",err);
		exit(1);
	}

	// retreive -- 여기가 문제!
	if ( (err=ldap_result(session, 0, 1, &timeout, chain)) < 0) {
		fprintf(stderr, "ldap retreive fail. Errcode=%d\n",err);
		exit(1);
	}	

	// Success
	printf ("ldap retreive success. \n");
	
	// Count
	result = ldap_count_messages(session, chain[0]);
	printf ("Num result: %d\n", result);

	ldap_unbind(session);
	return 0;
}

쿼리 보낼때, ldap_search_ext 가 쓰여져야 할테지만, 테스트용으로 일단 간단하게
ldap_search (depricated 되었답니다만..) 를 썼는데, 이것이 문제인지..
아니면 아예 커넥션부분이 SSL 인데 아무생각없이 포트번호만 잡아줘서 그런지...

혹시 open ldap 에 대해 잘 아시는분 부탁드립니다.
그럼 이만.. 꾸뻑.

litdream의 이미지

거의 5개월만에 자답합니다.

   // retreive -- 여기가 문제!
   if ( (err=ldap_result(session, 0, 1, &timeout, chain)) < 0) {
      fprintf(stderr, "ldap retreive fail. Errcode=%d\n",err);
      exit(1);
   }    

저기서 ldap_result() 구분을 다음과 같이 바꾸면, 일단 성공하더군요.

   if ( (err=ldap_result(session, LDAP_RES_ANY, 1, &timeout, chain)) < 0) {

물론, bind 할때 credential 을 정확히 잡아주어야 하고요.
저희 팀장이 이 프로젝을 끝내라는 압력이 지금 대단합니다.. 흑흑..
LDAP/SSL 을 사용한 secure service 를 만들어내라고 닥닥 볶임을 당합니다.

솔직히 저 두 분야는 모두 자신이 없는 부분이라 앞으로 한달간 엄청난 시련이
예상되는 바입니다. 잘되기를 바랄 뿐입죠.. 흐흐흐..

삽질의 대마왕...

litdream의 이미지

어제 책 읽고 다시 만든 ldap query 입니다.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <lber.h>
#include <ldap.h>

int
main (int argc, char **argv)
{
  LDAP *ld;
  int auth_method = LDAP_AUTH_SIMPLE;
  int desired_version = LDAP_VERSION3;
  char *ldap_host = "[hostname]";
  char *root_dn = "uid=[ldap-user-name],dc=[dc1],dc=[dc2]....";
  char *root_pw = "secret" ;
  char *base = "dc=[dca],dc=[dcb]...";
  char *filter = "[filter string]";
  char *errstring;
  char *dn = NULL;
  char *attr;
  char **vals;

  int i;
  int result;

  BerElement *ber;
  LDAPMessage *msg;
  LDAPMessage *entry;

  if ((ld = ldap_init (ldap_host, LDAP_PORT)) == NULL)
    {
      perror ("ldap_init failed");
      exit (EXIT_FAILURE);
    }

  if (ldap_set_option (ld, LDAP_OPT_PROTOCOL_VERSION, &desired_version) !=
      LDAP_OPT_SUCCESS)
    {
      ldap_perror (ld, "ldap_set_option");
      exit (EXIT_FAILURE);
    }

  if (ldap_bind_s (ld, root_dn, root_pw, auth_method) != LDAP_SUCCESS)
    {
      ldap_perror (ld, "ldap_bind");
      exit (EXIT_FAILURE);
    }

  if (ldap_search_s (ld, base, LDAP_SCOPE_SUBTREE, filter, NULL, 0, &msg)
      != LDAP_SUCCESS)
    {
      ldap_perror (ld, "ldap_search_s");
      exit (EXIT_FAILURE);
    }

  printf ("the number of entries returned was %d\n\n",
	  ldap_count_entries (ld, msg));

  for (entry = ldap_first_entry (ld, msg);
       entry != NULL; entry = ldap_next_entry (ld, entry))
    {
      if ((dn = ldap_get_dn (ld, entry)) != NULL)
	{
	  printf ("Returned dn: %s\n", dn);
	  ldap_memfree (dn);
	}

      for (attr = ldap_first_attribute (ld, entry, &ber);
	   attr != NULL; attr = ldap_next_attribute (ld, entry, ber))
	{
	  if ((vals = ldap_get_values (ld, entry, attr)) != NULL)
	    {
	      for (i = 0; vals[i] != NULL; i++)
		printf ("%s: %s\n", attr, vals[i]);
	      ldap_value_free (vals);
	    }
	  ldap_memfree (attr);
	}
      if (ber != NULL)
	ber_free (ber, 0);
      printf ("\n");
    }
  ldap_msgfree (msg);
  result = ldap_unbind_s (ld);

  if (result != 0)
    {
      fprintf (stderr, "ldap_unbind_s: %s\n", ldap_err2string (result));
      exit (1);
    }
  return 0;
}

삽질의 대마왕...

srkim2의 이미지

위에 첫 example는 ldaps 인 서버에 평문으로 ldap 질의를 날려서... 안되는것
ldap_search에서 error가 나야 할 것 처럼 보이지만 특이하게 ldap_result에서 에러 떨어지는 상태??
아닌가 해요

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.