Interstage Application Server Smart Repository運用ガイド
|
目次
索引

|
6.3.3.4 非同期型のエントリ追加
非同期型でエントリを追加する場合のサンプルプログラムを以下に示します。
このサンプルプログラムでは、人のエントリ(inetOrgPerson)を追加しています。
[add.c]
/*
* All Rights Reserved, Copyright (C) FUJITSU LIMITED 2006
*
* 新しいエントリを1つ追加する。
*
*/
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#if defined(unix)
#include <unistd.h>
#elif defined(_WIN32)
#include <winsock.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 timeout;
LDAPMod **mods;
int errcode;
char *errmsg;
char *matched;
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_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], 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((size_t)( 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( (size_t)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 );
ldap_memfree( 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] );
timeout.tv_sec = 30L;
timeout.tv_usec = 0L;
rtn = ldap_result( ld, msgid, LDAP_MSG_ALL, &timeout, &result );
switch ( rtn ) {
case -1:
/* エラー発生時 */
if ( result != NULL ) {
ldap_parse_result( ld, result, &errcode, &matched, &errmsg, 0, 0, 0 );
do_error_msg( "ldap_result", errcode, matched, errmsg );
ldap_msgfree( result );
ldap_memfree( matched );
ldap_memfree( 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;
ldap_parse_result( ld, result, &errcode, &matched, &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, matched, errmsg );
printf( "%s: Error while adding entry\n", argv[0] );
}
ldap_msgfree( result );
ldap_memfree( matched );
ldap_memfree( errmsg );
}
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) 富士通株式会社 2007