Interstage Application Server Smart Repository運用ガイド
|
目次
索引
|
5.3.3.4 非同期型のエントリ追加
非同期型でエントリを追加する場合のサンプルプログラムを以下に示します。
このサンプルプログラムでは、人のエントリ(inetOrgPerson)を追加しています。
[add.c]
/*
* Copyright (c) 2004. Fujitsu Limited. All rights reserved.
*
* エントリの追加を行う。
*
*/
#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"
#include "def.h"
#define NMODS 6
unsigned long global_counter = 0;
char *host = SAMPLE_HOST;
int port = SAMPLE_PORT;
char *bind_dn = SAMPLE_BINDDN;
char *passwd = SAMPLE_PASSWORD;
char *add_dn = "uid=100001," SAMPLE_BASEDN;
char *objectclass_values[] = {
"top",
"person",
"organizationalPerson",
"inetOrgPerson",
NULL
};
char *cn_values[] = {
"U001",
"User001",
NULL
};
char *sn_values[] = {
"Fujitsu",
NULL
};
char *givenname_values[] = {
"User001",
NULL
};
char *telephonenumber_values[] = {
"123-456-7890",
NULL
};
char *uid_values[] = {
"100001",
NULL
};
/*-------------------------------------------------------------*/
/*
* LDAPMod構造体の解放
*/
static void free_mods(
LDAPMod *mods[]
)
{
int i;
for ( i = 0; i < NMODS; i++ ) {
if ( mods[ i ] == NULL ) {
break;
}
free( mods[ i ] );
}
free( mods );
}
/*-------------------------------------------------------------*/
/*
* 検索結果を待ち合わせている間、他の作業
* を行うことができます。
* 他の作業を行うプログラムをここに記述し
* ます。
*/
static void do_other_work()
{
extern unsigned long global_counter;
global_counter++;
}
/*-------------------------------------------------------------*/
/*
* メイン・プログラム
*/
int main(
int argc,
char *argv[]
)
{
LDAP *ld;
LDAPMessage *result;
int i;
int rtn;
int msgid;
int finished;
struct timeval zerotime;
LDAPMod **mods;
int errcode;
char *errmsg;
char *mached;
int optdata;
extern char *host;
extern int port;
extern char *bind_dn;
extern char *passwd;
extern char *add_dn;
extern char *objectclass_values[];
extern char *cn_values[];
extern char *sn_values[];
extern char *givenname_values[];
extern char *telephonenumber_values[];
extern char *uid_values[];
/*----------------------------------------------------------*/
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プロトコル) */
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], bind_dn );
rtn = ldap_simple_bind_s( ld, bind_dn, 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 );
}
/* LDAPMod構造体の作成 */
mods = ( LDAPMod ** )calloc(( NMODS + 1 ), sizeof( LDAPMod * ));
if ( mods == NULL ) {
fprintf( stderr, "%s: Cannot allocate memory for mods array\n", argv[0] );
do_unbind( ld, argv );
printf( "%s is abnormal end\n", argv[0] );
return( 1 );
}
for ( i = 0; i < NMODS; i++ ) {
mods[ i ] = ( LDAPMod * )calloc( 1, sizeof( LDAPMod ));
if ( mods[ i ] == NULL ) {
fprintf( stderr, "%s: Cannot allocate memory for mods element\n", argv[0] );
free_mods( mods );
do_unbind( ld, argv );
printf( "%s is abnormal end\n", argv[0] );
return( 1 );
}
}
mods[ 0 ]->mod_op = 0;
mods[ 0 ]->mod_type = "objectclass";
mods[ 0 ]->mod_values = objectclass_values;
mods[ 1 ]->mod_op = 0;
mods[ 1 ]->mod_type = "cn";
mods[ 1 ]->mod_values = cn_values;
mods[ 2 ]->mod_op = 0;
mods[ 2 ]->mod_type = "sn";
mods[ 2 ]->mod_values = sn_values;
mods[ 3 ]->mod_op = 0;
mods[ 3 ]->mod_type = "givenname";
mods[ 3 ]->mod_values = givenname_values;
mods[ 4 ]->mod_op = 0;
mods[ 4 ]->mod_type = "telephonenumber";
mods[ 4 ]->mod_values = telephonenumber_values;
mods[ 5 ]->mod_op = 0;
mods[ 5 ]->mod_type = "uid";
mods[ 5 ]->mod_values = uid_values;
mods[ 6 ] = NULL;
/* エントリの追加 */
printf( "%s: ldap_add( \"%s\" )\n", argv[0], add_dn );
msgid = ldap_add( ld, add_dn, mods );
if ( msgid < 0 ) {
do_get_ldaperror( ld, &errcode, &errmsg );
do_error_msg( "ldap_add", errcode, NULL, errmsg );
do_unbind( ld, argv );
free_mods( mods );
printf( "%s is abnormal end\n", argv[0] );
return( 1 );
}
/* 結果の待ち合わせ処理 */
finished = 0;
while ( !finished ) {
printf( "%s: ldap_result( )\n", argv[0] );
result = NULL;
zerotime.tv_sec = 30L;
zerotime.tv_usec = 0L;
rtn = ldap_result( ld, msgid, LDAP_MSG_ALL, &zerotime, &result );
switch ( rtn ) {
case -1:
/* エラー発生時 */
if ( result != NULL ) {
errcode = 0;
mached = NULL;
errmsg = NULL;
ldap_parse_result( ld, result, &errcode, &mached, &errmsg, 0, 0, 0 );
do_error_msg( "ldap_result", errcode, mached, errmsg );
}
printf( "%s: Entry added error.\n", argv[0] );
do_unbind( ld, argv );
free_mods( mods );
printf( "%s is abnormal end\n", argv[0] );
return( 1 );
case 0:
/*
* タイムアウト発生時、または
* 受信可能な結果がなかった時
*/
#if defined(unix)
sleep(1);
#elif defined(_WIN32)
Sleep(1*1000);
#endif
break;
default:
/* 処理完了 */
finished = 1;
errcode = 0;
mached = NULL;
errmsg = NULL;
ldap_parse_result( ld, result, &errcode, &mached, &errmsg, 0, 0, 0 );
if ( errcode == LDAP_SUCCESS ) {
/* 正常終了時 */
printf( "%s: Entry added successfully. I counted to %ld while waiting.\n", argv[0], global_counter );
} else {
/* エラー発生時 */
do_error_msg( "ldap_result", errcode, mached, errmsg );
printf( "%s: Error while adding entry\n", argv[0] );
}
ldap_msgfree( result );
}
do_other_work();
}
/* 終了処理 */
do_unbind( ld, argv );
free_mods( mods );
printf( "%s is normal end\n", argv[0] );
return( 0 );
}
All Rights Reserved, Copyright(C) 富士通株式会社 2005