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

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

5.5.3.8 V3拡張機能を使用したエントリ検索

 V3拡張機能(サーバソートおよびページング)のエントリ検索におけるサンプルプログラムを以下に示します。

 このサンプルプログラムでは、サーバソートとページング機能を同時に指定した検索を行っています。

■[search_ext.c]

  /*
   * Copyright (c) 2001.  Fujitsu Limited.  All rights reserved.
   *
   * Search the directory for all entry with server sort and paging.
   *
   */
  
  #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
  #if defined(unix)
  #include    <unistd.h>
  #endif
  #include    "examples.h"
  
  unsigned long    global_counter = 0;
  
  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=*)";
  
  char    sortkey1[]    = "cn";
  char    sortkey2[]    = "sn";
  char    *sortarray[3] = {sortkey1, sortkey2, NULL};
  
  int     dopaging      = TRUE;
  int     pagesize      = 10;
  
  /*-------------------------------------------------------------*/
  /*
   * 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=NULL;
      LDAPMessage    *result      = NULL;
      int            rtn=0;
      int            optdata      = 0;
      int            errcode      = 0;
      char           *errmsg      = NULL;
      int            num_entries  = 0;
  
      LDAPControl    **resctrlp   = NULL;
      LDAPControl    **reqctrlp   = NULL;
      int            allpagesdone = FALSE;
  
      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_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 );
      }
  
      /* set option ( LDAP_OPT_OFF ) */
      optdata = LDAP_OPT_OFF;
      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 );
      }
  
      /* 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 );
      }
  
      /* make sort ldap control */
      printf( "%s: ldap_create_sort_control( \"sort key is %s, %s\" )\n", argv[0], sortarray[0], sortarray[1] );
      rtn = ldap_create_sort_control(ld,sortarray, 0x0, 0x0,&reqctrlp);
      if(rtn != LDAP_SUCCESS) {
          do_error_msg( "ldap_create_sort_control", rtn, NULL, NULL );
          do_unbind( ld, argv );
          printf( "%s is abnormal end\n", argv[0] );
          exit( 1 );
      }
  
      /* make paging control */
      rtn = ldap_create_paging_control(ld,pagesize,0x0,&reqctrlp);
      if(rtn != LDAP_SUCCESS) {
          do_error_msg( "ldap_create_paging_control", rtn, NULL, NULL )
          do_unbind( ld, argv );
          printf( "%s is abnormal end\n", argv[0] );
          ldap_controls_free ( reqctrlp );
          exit( 1 );
      }
  
      while (!allpagesdone) {
          /* Actually perform the search */
          if ( (rtn = ldap_search_ext_s( ld, 
                          s_base, 
                          scope, 
                          filter,
                          NULL, 
                          0,
                          reqctrlp,
                          NULL,
                          NULL,
                          0,
                          &result )) != LDAP_SUCCESS ) {
              do_error_msg( "ldap_search_ext_s", rtn, NULL, NULL );
              do_unbind( ld, argv );
              printf( "%s is abnormal end\n", argv[0] );
              ldap_controls_free ( reqctrlp );
              exit( 1 );
          }
  
          num_entries += print_entry( ld, result, 0 );
  
          printf( "%d matches\n", num_entries );
  
          /* get the response controls to see if more pages remain */
          rtn = ldap_parse_result( 
                      ld,
                      result,
                      &errcode,
                      NULL,
                      &errmsg,
                      NULL,
                      &resctrlp,
                      0);
  
          if ( rtn != LDAP_SUCCESS ) {
              do_error_msg( "ldap_parse_result", rtn, NULL, NULL );
              do_unbind( ld, argv );
              printf( "%s is abnormal end\n", argv[0] );
              ldap_controls_free ( reqctrlp );
              ldap_msgfree( result );
              exit( 1 );
          }
          else {
              if ( errcode ) {
                  do_error_msg( "ldap_parse_result", errcode, NULL, errmsg );
              }
  
              if ( errmsg ) {
                  ldap_memfree(errmsg);
              }
  
              if ( dopaging && resctrlp ) {
                  rtn = ldap_create_more_paging_control(ld,pagesize,0x0,resctrlp,&reqctrlp);
                  if(rtn == LDAP_NO_RESULTS_RETURNED)
                      allpagesdone = TRUE;
                  else if(rtn == LDAP_MORE_RESULTS_TO_RETURN)
                      allpagesdone = FALSE;
                  else {
                      do_error_msg( "ldap_create_more_paging_control", rtn, NULL, NULL );
                      do_unbind( ld, argv );
                      printf( "%s is abnormal end\n", argv[0] );
                      ldap_controls_free ( reqctrlp );
                      ldap_controls_free ( resctrlp );
                      exit( 1 );
                  }
              }
              else {
                  allpagesdone = TRUE;
                  ldap_controls_free ( reqctrlp );
              }
          }
  
          if (result) {
              ldap_msgfree ( result );
              result = NULL;
          }
          if (resctrlp) {
              ldap_controls_free ( resctrlp );
              resctrlp = NULL;
          }
      }
      ldap_controls_free ( reqctrlp );
  
      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) 富士通株式会社 2004