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

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

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

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

 このサンプルプログラムでは、リフェラル先への検索をLDAP-APIで自動的に行う場合の検索です。

[search_ref_auto.c]

  /*
   * Copyright (c) 2001.  Fujitsu Limited.  All rights reserved.
   *
   * Search the directory for all entry, and automatically follow referral.
   *
   */
  
  #include    <sys/types.h>
  #include    <stdio.h>
  #include    <stdlib.h>
  #include    <string.h>
  #if defined(_WIN32)
  #include    <winsock.h>
  #endif
  #include    "examples.h"
  
  unsigned long    global_counter = 0;
  
  char    *host      = "localhost";
  int     port       = LDAP_PORT;
  char    *s_base    = "o=Fujitsu,c=JP";
  int     scope      = LDAP_SCOPE_SUBTREE;
  char    *filter    = "(objectclass=*)";
  
  
  /*-------------------------------------------------------------*/
  /*
   * Perform other work while polling for results.  This doesn't do anything
   * useful, but it could.
   */
  static void do_other_work()
  {
      extern unsigned long  global_counter;
      global_counter++;
  }
  
  
  /*-------------------------------------------------------------*/
  /*
   * main program
   */
  int main(
      int    argc,
      char   *argv[]
  )
  {
      LDAP              *ld;
      LDAPMessage       *result;
      LDAPMessage       *entry;
      struct timeval    zerotime;
      int               msgid;
      int               rtn;
      int               optdata;
      int               finished;
      int               errcode;
      char              *errmsg;
      char              *mached;
      int               num_entries = 0;
      char              **referralsh;
  
      extern char       *host;
      extern int        port;
      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 authentication */
      printf( "%s: ldap_simple_bind_s( anonymous )\n", argv[0] );
      rtn = ldap_simple_bind_s( ld, NULL, NULL );
      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 );
      }
  
      /* entry search */
      printf( "%s: ldap_search( \"%s\", %d, \"%s\" )\n", argv[0], s_base, scope, filter );
      msgid = ldap_search( ld, s_base, scope, filter, NULL, 0 );
      if ( msgid < 0 ) {
          do_get_ldaperror( ld, &errcode, &errmsg );
          do_error_msg( "ldap_search", errcode, NULL, errmsg );
          do_unbind( ld, argv );
          printf( "%s is abnormal end\n", argv[0] );
          exit( 1 );
      }
  
      /* Loop, polling for results until finished */
      finished = 0;
      while ( finished == 0 ) {
          printf( "%s: ldap_result( )\n", argv[0] );
          result = NULL;
          zerotime.tv_sec = 30L;
          zerotime.tv_usec = 0L;
          rtn = ldap_result( ld, msgid, LDAP_MSG_ONE, &zerotime, &result );
          switch ( rtn ) {
              case -1:
                  /* some error occurred */
                  if ( result != NULL ) {
                      errcode = 0;
                      mached = NULL;
                      errmsg = NULL;
                      ldap_parse_result( ld, result, &errcode, &mached, &errmsg, &referralsh, 0, 0 );
                      do_error_msg( "ldap_result", errcode, mached, errmsg );
                      if(referralsh != NULL && *referralsh != NULL) {
                          fprintf( stderr, "\tldapURL\t\t\t: %s\n", *referralsh );
                          ldap_value_free(referralsh);
                      }
                  }
                  printf( "%s: Entry search error.\n", argv[0] );
                  do_unbind( ld, argv );
                  printf( "%s is abnormal end\n", argv[0] );
                  exit( 1 );
              case 0:
                  /* Timeout was exceeded.
                     No entries are ready for retrieval. */
                  if ( result != NULL ) {
                      ldap_msgfree( result );
                  }
  #if defined(unix)
                  sleep(1);
  #elif defined(_WIN32)
                  Sleep(1*1000);
  #endif
                  break;
              case LDAP_RES_SEARCH_REFERENCE:
                  if((rtn = ldap_parse_reference(ld,result,&referralsh,NULL,0)) == LDAP_SUCCESS) {
                      fprintf( stderr, "\tldapURL\t\t\t: %s\n", *referralsh );
                      do_get_ldaperror( ld, &errcode, &errmsg );
                      do_error_msg( "ldap_search", errcode, NULL, errmsg );
                      ldap_value_free(referralsh);
                  }
                  if ( result != NULL ) {
                      ldap_msgfree( result );
                  }
                  do_unbind( ld, argv );
                  printf( "%s is abnormal end\n", argv[0] );
                  exit( 1 );
              case LDAP_RES_SEARCH_RESULT:
                  ldap_parse_result( ld, result, &errcode, &mached, &errmsg, &referralsh, 0, 0 );
                  if(errcode == LDAP_SUCCESS) {
                      printf("search finished.\n");
                      printf("\n");
                  } else {
                      do_error_msg( "ldap_result", errcode, mached, errmsg );
                  }
                  if(referralsh != NULL && *referralsh != NULL) {
                      fprintf( stderr, "\tldapURL\t\t\t: %s\n", *referralsh );
                      ldap_value_free(referralsh);
                  }
              default:
                  /*
                   * Either an entry is ready for retrieval, or all entries
                   * have been retrieved.
                   */
                  entry = ldap_first_entry( ld, result );
                  if ( entry == NULL ) {
                      /* All done */
                      finished = 1;
                      if ( result != NULL ) {
                          ldap_msgfree( result );
                      }
                      continue;
                  }
                  num_entries += print_entry( ld, result, 1 );
          }
          /* Do other work here while you are waiting... */
          do_other_work();
      }
  
      /* All done.  Print a summary. */
      printf( "%s: %d entries retrieved.  I counted to %ld while I was waiting.\n", argv[0], num_entries, global_counter );
  
      do_unbind( ld, argv );
      printf( "%s is normal end\n", argv[0] );
      exit( 0 );
  }

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

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