InfoDirectory使用手引書
|
目次
索引
|
5.5.3.11 リフェラル先のエントリ検索(手動追跡)
リフェラル先のエントリ検索(手動追跡)におけるサンプルプログラムを以下に示します。
このサンプルプログラムでは、リフェラル先への検索をユーザアプリケーションで行う場合の検索です。
[search_ref_man.c]
/*
* Copyright (c) 2001. Fujitsu Limited. All rights reserved.
*
* Search the directory for all entry.
*
*/
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(_WIN32)
#include <winsock.h>
#endif
#include "examples.h"
#define LDAPURLHEADERSIZE 7
#define LDAPURLHEADER "ldap://"
char *host = "localhost";
int port = LDAP_PORT;
char *binddn = "cn=admin,o=Fujitsu,c=JP";
char *passwd = "admin123";
char *s_base = "o=Fujitsu,c=JP";
int scope = LDAP_SCOPE_SUBTREE;
char *filter = "(objectclass=*)";
char *cmdname;
int chase_reference(LDAP *,LDAPMessage *);
int search_reference(char ***);
int parse_url(char *, char **, int *, char **);
/*-------------------------------------------------------------*/
/*
* main program
*/
void main(
int argc,
char *argv[]
)
{
LDAP *ld;
LDAPMessage *result;
int rtn;
int optdata;
int errcode;
char *errmsg;
char *mached;
int num_entries;
char **referralsh;
extern char *host;
extern int port;
extern char *binddn;
extern char *passwd;
extern char *s_base;
extern int scope;
extern char *filter;
/*----------------------------------------------------------*/
cmdname = argv[0];
printf( "%s is start\n", cmdname );
printf( "%s: ldap_init( \"%s\", %d )\n", cmdname, host, port );
ld = ldap_init( host, port );
if ( ld == NULL ) {
perror( "ldap_init" );
printf( "%s is abnormal end\n", cmdname );
exit( 1 );
}
/* set option ( LDAP_OPT_OFF ) */
optdata = LDAP_OPT_OFF;
printf( "%s: ldap_set_option( LDAP_OPT_REFERRALS, %d )\n", cmdname, optdata );
rtn = ldap_set_option(ld, LDAP_OPT_REFERRALS, (void *)(&optdata));
if ( rtn != 0 ) {
do_error_msg( "ldap_set_option", rtn, NULL, NULL );
do_unbind( ld, argv );
printf( "%s is abnormal end\n", cmdname );
exit( 1 );
}
/* set option ( LDAP_DEREF_ALWAYS ) */
optdata = LDAP_DEREF_ALWAYS;
printf( "%s: ldap_set_option( LDAP_OPT_DEREF, %d )\n", argv[0], optdata );
rtn = ldap_set_option(ld, LDAP_OPT_DEREF, (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 );
}
/* set option ( LDAP_VERSION3 ) */
optdata = LDAP_VERSION3;
printf( "%s: ldap_set_option( LDAP_OPT_PROTOCOL_VERSION, %d )\n", cmdname, 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", cmdname );
exit( 1 );
}
/* simple authenticate */
printf( "%s: ldap_simple_bind_s( \"%s\", \"******\" )\n", cmdname, 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", cmdname );
exit( 1 );
}
printf( "%s: ldap_search_s( \"%s\", %d, \"%s\" )\n", cmdname, s_base, scope, filter );
rtn = ldap_search_s( ld, s_base, scope, filter, NULL, 0, &result );
if ( rtn != LDAP_SUCCESS ) {
if ( result != NULL ) {
errcode = 0;
mached = NULL;
errmsg = NULL;
ldap_parse_result( ld, result, &errcode, &mached, &errmsg, &referralsh, 0, 0 );
ldap_msgfree( result );
}
do_error_msg( "ldap_search_s", errcode, mached, errmsg );
if(referralsh != NULL && *referralsh != NULL) {
fprintf( stderr, "\tldapURL\t\t\t: %s\n", *referralsh );
ldap_value_free(referralsh);
}
do_unbind( ld, argv );
printf( "%s is abnormal end\n", cmdname );
exit( 1 );
}
num_entries = print_entry( ld, result, 0 );
printf( "%s: %d entries retrieved. \n", cmdname, num_entries );
printf( "%s: search referral server\n", cmdname );
if(chase_reference(ld, result) != 0) {
fprintf(stderr,"failed to chase referral\n");
ldap_msgfree( result );
do_unbind( ld, argv );
printf( "%s is abnormal end\n", cmdname );
exit( 1 );
}
ldap_msgfree( result );
do_unbind( ld, argv );
printf( "%s is normal end\n", cmdname );
exit( 0 );
}
int chase_reference(LDAP *ld,LDAPMessage *result)
{
LDAPMessage *refinfo;
char **referralsh;
int rtn = 0;
char *mached = NULL;
for ( refinfo = ldap_first_reference( ld, result );
refinfo != NULL;
refinfo = ldap_next_reference( ld, refinfo ) ) {
if((rtn = ldap_parse_reference(ld,refinfo,&referralsh,NULL,0)) == LDAP_SUCCESS) {
if(referralsh != NULL && *referralsh != NULL)
rtn = search_reference(&referralsh);
if(referralsh != NULL)
ldap_value_free(referralsh);
if(rtn != 0)
break;
}
else {
do_error_msg( "ldap_search_s", rtn, NULL, NULL );
break;
}
}
if(rtn == 0)
return 0;
else
return -1;
}
int search_reference(char ***referralsh)
{
LDAP *rld;
char *rhost;
int rport;
char *rbase;
LDAPMessage *rresult=NULL;
int rtn;
int optdata;
int num_entries;
int errcode;
char *errmsg;
char *mached;
char **rreferralsh;
char *url;
url = **referralsh;
if(parse_url(url, &rhost, &rport, &rbase) != 0) {
fprintf(stderr,"failed to parse url\n");
return( 1 );
}
printf( "%s: ldap_init( \"%s\", %d )\n", cmdname, rhost, rport );
rld = ldap_init( rhost, rport );
if ( rld == NULL ) {
perror( "ldap_init" );
return( 1 );
}
/* set option ( LDAP_OPT_OFF ) */
optdata = LDAP_OPT_OFF;
printf( "%s: ldap_set_option( LDAP_OPT_REFERRALS, %d )\n", cmdname, optdata );
rtn = ldap_set_option(rld, LDAP_OPT_REFERRALS, (void *)(&optdata));
if ( rtn != 0 ) {
do_error_msg( "ldap_set_option", rtn, NULL, NULL );
ldap_unbind( rld );
return( 1 );
}
/* set option ( LDAP_VERSION3 ) */
optdata = LDAP_VERSION3;
printf( "%s: ldap_set_option( LDAP_OPT_PROTOCOL_VERSION, %d )\n", cmdname, optdata );
rtn = ldap_set_option(rld, LDAP_OPT_PROTOCOL_VERSION, (void *)(&optdata));
if ( rtn != 0 ) {
do_error_msg( "ldap_set_option", rtn, NULL, NULL );
ldap_unbind( rld );
return( 1 );
}
printf( "%s: ldap_search_s( \"%s\", %d, \"%s\" )\n", cmdname, s_base, scope, filter );
rtn = ldap_search_s( rld, rbase, scope, filter, NULL, 0, &rresult );
if ( rtn != LDAP_SUCCESS ) {
if ( rresult != NULL ) {
errcode = 0;
mached = NULL;
errmsg = NULL;
ldap_parse_result( rld, rresult, &errcode, &mached, &errmsg, &rreferralsh, 0, 0 );
ldap_msgfree( rresult );
}
if(rreferralsh != NULL && *rreferralsh != NULL) {
fprintf( stderr, "\tldapURL\t\t\t: %s\n", *referralsh );
ldap_value_free(rreferralsh);
}
ldap_unbind( rld );
return( 1 );
}
num_entries = print_entry( rld, rresult, 1 );
printf( "%s: %d entries retrieved from %s:%d\n", cmdname, num_entries, rhost, rport );
ldap_unbind( rld );
return( 0 );
}
int parse_url(char *url, char **rhosth, int *rportp, char **rbaseh)
{
char *tp,*ptp;
char *rhost, *rbase;
int rport;
/* Parse ldapURL */
if(strncmp(url, LDAPURLHEADER, LDAPURLHEADERSIZE) != 0)
return ( -1 );
rhost = url + LDAPURLHEADERSIZE;
if(*rhost == 0x0 )
return ( -1 );
if((tp = strchr(rhost, ':')) == NULL) {
return ( -1 );
}
*tp = 0x0;
if((ptp = strchr(tp+1, '/')) != NULL)
*ptp = 0x0;
rport = atoi(tp+1);
rbase = ptp+1;
if(*rbase == 0x0 )
return ( -1 );
*rhosth = rhost;
*rbaseh = rbase;
*rportp = rport;
return ( 0 );
}
All Rights Reserved, Copyright(C) 富士通株式会社 2005