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

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

5.5.3.10 同期型リフェラル先のエントリ検索(自動追跡)

 リフェラル先のエントリ検索(自動追跡)におけるサンプルプログラムを以下に示します。

 このサンプルプログラムでは、リフェラル先への検索をユーザアプリケーションで行う場合の検索です。

[search_ref_auto_s.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(_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=*)";
  
  
  /*-------------------------------------------------------------*/
  /*
   * main program
   */
  int main(
      int     argc,
      char    *argv[]
  )
  {
      LDAP           *ld;
      LDAPMessage    *result;
      int            rtn;
      int            optdata;
      int            errcode;
      char           *errmsg;
      char           *mached;
      int            num_entries;
      char           **referralsh;
  
      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_OPT_ON ) */
      optdata = LDAP_OPT_ON;
      printf( "%s: ldap_set_option( LDAP_OPT_REFERRALS, %d )\n", argv[0], optdata );
      rtn = ldap_set_option(ld, LDAP_OPT_REFERRALS, (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 ( 10 ) */
      optdata = 10;
      printf( "%s: ldap_set_option( LDAP_OPT_REFERRAL_HOP_LIMIT, %d )\n", argv[0], optdata );
      rtn = ldap_set_option(ld, LDAP_OPT_REFERRAL_HOP_LIMIT, (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_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 authenticate */
      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 );
      }
  
      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 = 0x0;
              mached = NULL;
              errmsg = NULL;
              ldap_parse_result( ld, result, &errcode, &mached, &errmsg, &referralsh, 0, 0 );
              ldap_msgfree( result );
          }
          do_get_ldaperror( ld, &errcode, &errmsg );
          do_error_msg( "ldap_search_s", errcode, mached, errmsg );
          if(referralsh != NULL && *referralsh != NULL) {
              fprintf( stderr, "\tldapURL\t\t\t: %s\n", *referralsh );
              ldap_value_free(referralsh);
          }
          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