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

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

5.5.3.3 同期型のエントリ変更

 同期型でエントリを変更する場合のサンプルプログラムを以下に示します。

 このサンプルプログラムでは、日本語を含むDNのエントリに、日本語の属性値を設定しています。

[jp_modattrs.c]

  /*
   * Copyright (c) 2001. Fujitsu Limited. All rights reserved.
   *
   * Modify entry in the directory.
   *
   */
  
  #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    *mod_dn  = "cn=富士通 太郎,o=Fujitsu,c=JP";
  
  
  /*-------------------------------------------------------------*/
  
  int main( int argc, char **argv )
  {
      LDAP             *ld;
      LDAPMod          mod0;
      LDAPMod          mod1;
      LDAPMod          *mods[ 3 ];
      char             *vals0[ 2 ];
      char             *vals1[ 2 ];
      struct berval    **utf_vals1;
      time_t           now;
      char             buf[ 128 ];
      int              optdata;
      int              errcode;
      char             *errmsg;
      char             *mached;
      int              rtn;  
      char             *utf_mod_dn;
  
      extern char      *host;
      extern int       port;
      extern char      *binddn;
      extern char      *passwd;
      extern char      *mod_dn;
  /*----------------------------------------------------------*/
  
      printf( "%s is start\n", argv[0] );
  
      /* get a handle to an LDAP connection */
      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_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 );
      }
  
      /* construct the list of modifications to make */
      vals0[0] = "user1@fujitsu.co.jp";
      vals0[1] = NULL;
      mod0.mod_op = LDAP_MOD_REPLACE;
      mod0.mod_type = "mail";
      mod0.mod_values = vals0;
  
      time( &now );
      sprintf( buf, "このエントリは、jp_modattrsプログラムによって変更されました。%s", ctime(&now ));  
  
      /* Get rid of \n which ctime put on the end of the time string */
      if ( buf[ strlen( buf ) - 1 ] == '\n' ) {
          buf[ strlen( buf ) - 1 ] = '\0';
      }
      vals1[ 0 ] = buf;
      vals1[ 1 ] = NULL;
      rtn = do_conv_values( vals1, &utf_vals1, LDAP_CHARS_OPT_SJIS,LDAP_CHARS_OPT_UTF8);
      if ( rtn == -1 ) {
          do_unbind( ld, argv );
          printf( "%s is abnormal end\n", argv[0] );
          exit( 1 );
      } 
      else {
          mod1.mod_op = (LDAP_MOD_BVALUES | LDAP_MOD_ADD);
          mod1.mod_type = "description";
          mod1.mod_bvalues = utf_vals1;
      }
      mods[ 0 ] = &mod0;
      mods[ 1 ] = &mod1;
      mods[ 2 ] = NULL;
  
      /* make the change */
      rtn = ldap_conv_utf( mod_dn, &utf_mod_dn,LDAP_CHARS_OPT_SJIS,LDAP_CHARS_OPT_UTF8);
      if ( rtn == -1 ) {
          perror( "ldap_conv_utf" );
          do_unbind( ld, argv );
          ldap_value_free_len( utf_vals1 );
          printf( "%s is abnormal end\n", argv[0] );
          exit( 1 );
      }
  
      printf( "%s: ldap_modify_s( \"%s\", ****** )\n", argv[0], mod_dn );
      rtn = ldap_modify_s( ld, utf_mod_dn, mods );
      if ( rtn != LDAP_SUCCESS ) {
          do_error_msg( "ldap_modify_s", rtn, NULL, NULL );
          do_unbind( ld, argv );
          printf( "%s is abnormal end\n", argv[0] );
          exit( 1 );
      }
  
      do_unbind( ld, argv );
      ldap_value_free_len( utf_vals1 );
      printf( "%s is normal end\n", argv[0] );
      return( 0 );
  }

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

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