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

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

6.3.3.2 同期型のエントリ検索

 同期型のエントリ検索におけるサンプルプログラムを以下に示します。
 このサンプルプログラムでは、エントリのすべての属性値を読込んでいます。

[rdentry.c]

/*
 * All Rights Reserved, Copyright (C) FUJITSU LIMITED 2006
 *
 * すべてのエントリを検索する。(同期)
 *
 */

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

char    *host   = SAMPLE_HOST;
int     port    = SAMPLE_PORT;
char    *binddn = SAMPLE_BINDDN;
char    *passwd = SAMPLE_PASSWORD;
char    *s_base = SAMPLE_BASEDN;
int     scope   = LDAP_SCOPE_SUBTREE;
char    *filter = "(objectclass=*)";


/*-------------------------------------------------------------*/
/* メインプログラム                                            */
int main(
    int     argc,
    char    *argv[] )
{
    LDAP        *ld;
    LDAPMessage *result;
    int         num_entries;
    int         rtn;
    int         optdata;
    int         errcode;
    char        *errmsg;
    char        *matched;

    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] );
         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( "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( "ldap_simple_bind_s", rtn, NULL, NULL );
        do_unbind( ld, argv );
        printf( "%s is abnormal end\n", argv[0] );
        return( 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 ) {
            ldap_parse_result( ld, result, &errcode, &matched, &errmsg, 0, 0, 0 );
            do_error_msg( "ldap_search_s", errcode, matched, errmsg );
            ldap_memfree( matched );
            ldap_memfree( errmsg );
        } else {
            do_error_msg( "ldap_search_s", rtn, NULL, NULL );
        }
        printf( "%s: Entry search error.\n", argv[0] );
        do_unbind( ld, argv );
        printf( "%s is abnormal end\n", argv[0] );
        return( 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] );
    return( 0 );
}

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

Copyright 2007 FUJITSU LIMITED