InfoDirectory使用手引書
目次 索引 前ページ次ページ

第3部 SDK編> 第5章 アプリケーション開発環境(LDAP C API)> 5.5 サンプルプログラム> 5.5.3 サンプルプログラムの紹介

5.5.3.2 同期型のエントリ検索

 同期型のエントリ検索におけるサンプルプログラムを以下に示します。

 このサンプルプログラムでは、エントリのすべての属性値を読込んでいます。

[rdentry.c]

  /*
   * Copyright (c) 2001. Fujitsu Limited. All rights reserved.
   *
   * Search the directory for all entry.
   *
   */
  
  #include    <sys/types.h>
  #include    <stdio.h>
  #include    <stdlib.h>
  #include    <string.h>
  #if defined(unix)
  #include    <time.h>
  #elif defined(_WIN32)
  #include    <winsock.h>
  #endif
  #include    "examples.h"
  
  char    *host    = "localhost";
  int     port     = LDAP_PORT;
  char    *binddn  = "cn=admin,o=Fujitsu,c=JP";
  char    *passwd  = "admin123";
  char    *s_base  = "o=Fujitsu,c=JP";
  int     scope    = LDAP_SCOPE_SUBTREE;
  char    *filter  = "(objectclass=*)";
  
  /*-------------------------------------------------------------*/
  
  int main(
      int    argc,
      char   *argv[]
      )
  {
      LDAP              *ld;
      LDAPMessage       *result;
      int               num_entries;
      int               rtn;
      int               optdata;
      struct timeval    zerotime;
      int               finished;
      int               errcode;
      char              *errmsg;
      char              *mached;
  
      extern char       *host;
      extern int        port;
      extern char       *binddn;
      extern char       *passwd;
      extern char       *s_base;
      extern int        scope;
      extern char       *filter;
  /*----------------------------------------------------------*/
  
      printf( "%s is start\n", argv[0] );
      printf( "%s: ldap_init( \"%s\", %d )\n", argv[0], host, port );
      ld = ldap_init( host, port );  
      if ( ld == NULL ) {
          perror( "ldap_init" );
          printf( "%s is abnormal end\n", argv[0] );
          exit( 1 );
      }
  
      /* set option ( LDAP_DEREF_ALWAYS ) */
      optdata = LDAP_DEREF_ALWAYS;
      printf( "%s: ldap_set_option( LDAP_OPT_DEREF, %d )\n", argv[0], optdata);
      rtn = ldap_set_option(ld, LDAP_OPT_DEREF, (void *)(&optdata));
      if (rtn != 0 ) {
          do_error_msg( "ldap_set_option", rtn, NULL, NULL );
          do_unbind( ld, argv );
          printf( "%s is abnormal end\n", argv[0] );
          exit( 1 );
      }
  
      /* set option ( LDAP_VERSION3 ) */
      optdata = LDAP_VERSION3;
      printf( "%s: ldap_set_option( LDAP_OPT_PROTOCOL_VERSION, %d )\n",argv[0],optdata); 
      rtn = ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, (void *)(&optdata));
      if ( rtn != 0 ) {
          do_error_msg( "ldap_set_option", rtn, NULL, NULL );
          do_unbind( ld, argv );
          printf( "%s is abnormal end\n", argv[0] );
          exit( 1 );
      }
  
      /* simple aythentication */
      printf( "%s: ldap_simple_bind_s( \"%s\", \"******\" )\n", argv[0],binddn);
      rtn = ldap_simple_bind_s( ld, binddn, passwd );
      if ( rtn != LDAP_SUCCESS ) {
          do_error_msg( "ldap_simple_bind_s", rtn, NULL, NULL );
          do_unbind( ld, argv );
          printf( "%s is abnormal end\n", argv[0] );
          exit( 1 );
      }
  
      result = NULL;
      printf( "%s: ldap_search_s( \"%s\", %d, \"%s\" )\n", argv[0], s_base,scope,filter );
      rtn = ldap_search_s( ld, s_base, scope, filter, NULL, 0, &result);
      if ( rtn != LDAP_SUCCESS ) {
          if ( result != NULL ) {
              errcode = NULL;
              mached = NULL;
              errmsg = NULL;
              ldap_parse_result( ld, result, &errcode, &mached, &errmsg,0,0, 0 );
              do_error_msg( "ldap_search_s", errcode, mached, errmsg );
          } 
          else {
              do_error_msg( "ldap_search_s", rtn, NULL, NULL );
          }
          printf( "%s: Entry search error.\n", argv[0] );
          do_unbind( ld, argv );
          printf( "%s is abnormal end\n", argv[0] );
          exit( 1 );
      }
      num_entries = print_entry( ld, result, 1 );
      printf( "%s: %d entries retrieved.\n", argv[0], num_entries );
      do_unbind( ld, argv );
      printf( "%s is normal end\n", argv[0] );
      exit( 0 );
  }

目次 索引 前ページ次ページ

All Rights Reserved, Copyright(C) 富士通株式会社 2005