Interstage Application Server Smart Repository運用ガイド
目次 索引 前ページ次ページ

第6章 アプリケーションの作成(C API)> 6.3 サンプルプログラム> 6.3.3 サンプルプログラムの紹介

6.3.3.3 同期型のエントリ変更

 同期型でエントリを変更する場合のサンプルプログラムを以下に示します。
 このサンプルプログラムでは、日本語を含むDNのエントリに、日本語の属性値を設定しています。

[jp_modattrs.c]

/*
 * All Rights Reserved, Copyright (C) FUJITSU LIMITED 2006
 *
 * エントリを変更する。(日本語データを含む)
 *
 * [For Japanese]
 *
 */

#include    <sys/types.h>
#include    <stdio.h>
#include    <stdlib.h>
#include    <string.h>
#include    <time.h>
#include    "examples.h"
#include    "def.h"
#include"jp_def.h"

char    *host   = SAMPLE_HOST;
int     port    = SAMPLE_PORT;
char    *binddn = SAMPLE_BINDDN;
char    *passwd = SAMPLE_PASSWORD;
char    *mod_dn = JP_DN;


/*-------------------------------------------------------------*/
/* メインプログラム                                            */
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;
    int     optdata;
    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] );

    /* セションのオープン */
    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] );
        return( 1 );
    }

    /* オプションの設定 ( 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_jp( "ldap_set_option", rtn, NULL, NULL );
        do_unbind( ld, argv );
        printf( "%s is abnormal end\n", argv[0] );
        return( 1 );
    }

    /* 簡易認証 */
    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_jp( "ldap_simple_bind_s", rtn, NULL, NULL );
        do_unbind( ld, argv );
        printf( "%s is abnormal end\n", argv[0] );
        return( 1 );
    }

    /* 修正リスト(LDAPMod構造体)の作成 */
    vals0[0] = "user001@interstage.fujitsu.com";
    vals0[1] = NULL;
    mod0.mod_op = LDAP_MOD_REPLACE;
    mod0.mod_type = "mail";
    mod0.mod_values = vals0;

    vals1[ 0 ] = JP_MOD_MSG;
    vals1[ 1 ] = NULL;
    rtn = do_conv_values( vals1, &utf_vals1, BASE_CONV_OPT, LDAP_CHARS_OPT_UTF8 );

    if ( rtn == -1 ) {
        do_unbind( ld, argv );
        printf( "%s is abnormal end\n", argv[0] );
        return( 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;

    /* 変更する値の作成 */
    rtn = ldap_conv_utf( mod_dn, &utf_mod_dn,
                    BASE_CONV_OPT, LDAP_CHARS_OPT_UTF8 );
    if ( rtn == 0 ) {
        perror( "ldap_conv_utf" );
        do_unbind( ld, argv );
        ldap_value_free_len( utf_vals1 );
        printf( "%s is abnormal end\n", argv[0] );
        return( 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_jp( "ldap_modify_s", rtn, NULL, NULL );
        do_unbind( ld, argv );
        ldap_value_free_len( utf_vals1 );
        ldap_memfree( utf_mod_dn );
        printf( "%s is abnormal end\n", argv[0] );
        return( 1 );
    }
    /* 終了処理 */
    do_unbind( ld, argv );
    ldap_value_free_len( utf_vals1 );
    ldap_memfree( utf_mod_dn );
    printf( "%s is normal end\n", argv[0] );
    return( 0 );
}

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

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