📄 ldapbrower.cpp
字号:
#include <stdio.h>
#include "ldap.h"
#define HOSTNAME1 "172.19.1.175"
#define HOSTNAME "10.72.55.85"
#define PORTNUMBER 389
#define BASEDN "dc=huawei,dc=com"
#define SCOPE LDAP_SCOPE_SUBTREE
#define FILTER "(mail=*)"
///////////////
typedef unsigned long ber_tag_t;
typedef unsigned long ber_len_t;
///////////////
struct lber_options {
short lbo_valid;
unsigned short lbo_options;
int lbo_debug;
long lbo_meminuse;
};
struct berelement {
struct lber_options ber_opts;
#define ber_valid ber_opts.lbo_valid
#define ber_options ber_opts.lbo_options
#define ber_debug ber_opts.lbo_debug
/* Do not change the order of these 3 fields! see ber_get_next */
ber_tag_t ber_tag;
ber_len_t ber_len;
ber_tag_t ber_usertag;
char *ber_buf;
char *ber_ptr;
char *ber_end;
struct seqorset *ber_sos;
char *ber_rwptr;
};
/*
typedef struct
{
char *pszName ;
char *pszValue ;
unsigned long valueL;
char *updateVals[2];
} ATTR_NAME ;
typedef struct
{
char *FromIP;
int FromPort;
char *FromDN;
char *FromPWD;
char *FromBasedn;
char *ToIP;
int ToPort;
char *ToDN;
char *ToPWD;
char *ToBasedn;
char filter[100];
char logfile[300];
} gConfig;
/////////////////////////////////////////
void createEntry (LDAPMod *mods,ATTR_NAME input[],int pos)
{
mods->mod_op=LDAP_MOD_REPLACE |LDAP_MOD_ADD;
mods->mod_type =input[pos].pszName;
input[pos].updateVals[0]=input[pos].pszValue ;
input[pos].updateVals[1]=NULL;
mods->mod_values =input[pos].updateVals;
}
bool getconfig(gConfig *config,char *data)
{
FILE *cfg;char *pos,*pos1,*pos2;
////////
char modulePathname[_MAX_PATH];
char drive[_MAX_DRIVE];
char dir[_MAX_DIR];
char fullPath[_MAX_DIR]="";
memset(modulePathname, 0, sizeof(modulePathname));
memset(drive, 0, sizeof(drive));
memset(dir, 0, sizeof(dir));
GetModuleFileName(NULL, modulePathname, sizeof(modulePathname));
_splitpath (modulePathname, drive, dir, NULL, NULL);
sprintf(fullPath, "%s%s", drive, dir);
if(fullPath[strlen(fullPath)-1] != '\\')
{
strcat(fullPath, "\\");
}
sprintf(modulePathname, "%sldapreplicate.cfg", fullPath);
memset(config->logfile, 0,300);
memcpy(config->logfile,modulePathname,strlen(modulePathname)-3);
strcat(config->logfile,"out");
////////
cfg=fopen(modulePathname,"r");
if (cfg==NULL)
{
fprintf(stderr, "can not open config file!ldapreplicate.cfg");
return false;
}
fread(data,1,2000,cfg);
pos=strstr(data,"FHOSTNAME<");
pos1=strstr(pos,">");
pos2=pos1+1;
pos1[0]=0;
config->FromIP =pos+10;
/////////////////////////////////////
pos=strstr(pos2,"FBASEDN<");
pos1=strstr(pos,">");
pos2=pos1+1;
pos1[0]=0;
config->FromBasedn =pos+8;
/////////////////////////////////////////////////////
pos=strstr(pos2,"THOSTNAME<");
pos1=strstr(pos,">");
pos2=pos1+1;
pos1[0]=0;
config->ToIP =pos+10;
pos=strstr(pos2,"TBASEDN<");
pos1=strstr(pos,">");
pos2=pos1+1;
pos1[0]=0;
config->ToBasedn =pos+8;
///////////////////////
pos=strstr(pos2,"TDN<");
pos1=strstr(pos,">");
pos2=pos1+1;
pos1[0]=0;
config->ToDN =pos+4;
pos=strstr(pos2,"TPWD<");
pos1=strstr(pos,">");
pos2=pos1+1;
pos1[0]=0;
config->ToPWD =pos+5;
pos=strstr(pos2,"FILTER<");
pos1=strstr(pos,">");
pos2=pos1+1;
pos1[0]=0;
strcpy(config->filter,pos+7);
//////////
config->FromPort =389;
config->FromDN =NULL;
config->FromPWD =NULL;
config->ToPort =389;
return true;
}
*/
int main( int argc, char **argv )
{
LDAP *ld,*ld1;
LDAPMessage *res, *msg;
LDAPControl **serverctrls;
struct berval **ber1;
struct berelement *ber;
/*
//属性数最多为100个
LDAPMod mods[100];
LDAPMod *mod[100];
///////////////////////
char data[2000];
gConfig config;
//记录属性值
ATTR_NAME input[100];
int attnum,ii;
int addnum;
//证书信息纪录
unsigned char *cerbuf;
unsigned long cerlen;
cerbuf=(unsigned char *)malloc(2000);
//dn记录字段
char dnbuf[500];
*/
char *a, *dn, *matched_msg = NULL, *error_msg = NULL;
char **vals, **referrals;
int version, i, rc, parse_rc, msgtype, num_entries = 0,
num_refs = 0;
/*
if (!getconfig(&config,data))
return (1);
addnum=0;
/////////
FILE *log;char d[20],t[20];
char sText[1000];
log=fopen(config.logfile ,"a");
if (log==NULL)
{
fprintf(stderr, "can not open log file!ldapreplicate.out");
}
memset(sText,0,1000);
_strdate(d);
_strtime(t);
strcat(sText,d);strcat(sText," ");strcat(sText,t);strcat(sText,"\r\n");
fprintf(log,sText);
/////////*/
/* Get a handle to an LDAP connection. */
if ( (ld = ldap_init(argv[1],PORTNUMBER)) == NULL )
{
fprintf(stderr, "ldap_init error on HOSTNAME=%s,port=%d",HOSTNAME, PORTNUMBER );
return( 1 );
}
/////////////////
version = LDAP_VERSION3;
if ( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &version ) !=
LDAP_SUCCESS ) {
rc = ldap_get_lderrno( ld, NULL, NULL );
fprintf( stderr, "ldap_set_option: %s\n", ldap_err2string( rc ) );
ldap_unbind( ld );
return( 1 );
}
///////////////////////
/* Bind to the From server by anonymously. */
rc = ldap_simple_bind_s( ld, NULL, NULL );
if ( rc != LDAP_SUCCESS )
{
fprintf( stderr, "ldap_simple_bind_s: %s\n", ldap_err2string( rc ) );
ldap_get_lderrno( ld, &matched_msg, &error_msg );
if ( error_msg != NULL && *error_msg != '\0' )
{ fprintf( stderr, "%s\n", error_msg );
}
if ( matched_msg != NULL && *matched_msg != '\0' )
{ fprintf( stderr, "Part of the DN that matches an existing entry: %s\n", matched_msg );
}
ldap_unbind_s( ld );
return( 1 );
}
///从复制服务器中得到所有的条目///
/* Perform the search operation. */
rc = ldap_search_ext_s( ld, BASEDN , SCOPE,FILTER , NULL, 0, NULL, NULL, NULL, LDAP_NO_LIMIT, &res );
if ( rc != LDAP_SUCCESS )
{
fprintf( stderr, "ldap_search_ext_s: %s\n", ldap_err2string( rc ) );
if ( error_msg != NULL && *error_msg != '\0' )
{ fprintf( stderr, "%s\n", error_msg );
}
if ( matched_msg != NULL && *matched_msg != '\0' )
{
fprintf( stderr, "Part of the DN that matches an existing entry: %s\n", matched_msg );
}
ldap_unbind_s( ld );
ldap_unbind_s( ld1 );
return( 1 );
}
/////////////////
num_entries = ldap_count_entries( ld, res );
num_refs = ldap_count_references( ld, res );
for ( msg = ldap_first_message( ld, res ); msg != NULL; msg = ldap_next_message( ld, msg ) )
{
msgtype = ldap_msgtype( msg );
switch( msgtype )
{
/* If the result was an entry found by the search, get and print the
attributes and values of the entry. */
case LDAP_RES_SEARCH_ENTRY:
if (( dn = ldap_get_dn( ld, msg )) != NULL )
printf( "dn: %s\n", dn );
ldap_memfree( dn );
for ( a = ldap_first_attribute( ld, msg, &ber );
a != NULL; a = ldap_next_attribute( ld, msg, ber ) )
{//列举属性
if (( vals = ldap_get_values( ld, msg, a )) != NULL )
{ //得到数据值
for ( i = 0; vals[ i ] != NULL; i++ )
{
if (strcmp(a,"userCertificate")==0)
{
ber1=ldap_get_values_len(ld,msg,a);
printf( "%s:len=%d \n", a,(*ber1)->bv_len);
printf( "%s \n",vals[i]);
}
else
{
printf( "%s:%s \n", a,vals[i]);
}
}
}
////多个值
ldap_value_free( vals );
}
//得到数据值
ldap_memfree( a );
//列举属性
////////////////////
/////////////////////
if ( ber != NULL )
{
ber_free( ber, 0 );
}
printf( "\n" );
break;
case LDAP_RES_SEARCH_REFERENCE:
/* The server sent a search reference encountered during the
search operation. */
/* Parse the result and print the search references.
Ideally, rather than print them out, you would follow the
references. */
parse_rc = ldap_parse_reference( ld, msg, &referrals, NULL, 0 );
if ( parse_rc != LDAP_SUCCESS )
{
fprintf( stderr, "ldap_parse_result: %s\n", ldap_err2string( parse_rc ) );
ldap_unbind( ld );
return( 1 );
}
if ( referrals != NULL ) {
for ( i = 0; referrals[ i ] != NULL; i++ )
{
printf( "Search reference: %s\n\n", referrals[ i ] );
}
ldap_value_free( referrals );
}
break;
case LDAP_RES_SEARCH_RESULT:
/* Parse the final result received from the server. Note the last
argument is a non-zero value, which indicates that the
LDAPMessage structure will be freed when done. (No need
to call ldap_msgfree().) */
parse_rc = ldap_parse_result( ld, msg, &rc, &matched_msg, &error_msg, NULL, &serverctrls, 0 );
if ( parse_rc != LDAP_SUCCESS )
{
fprintf( stderr, "ldap_parse_result: %s\n", ldap_err2string( parse_rc ) );
ldap_unbind( ld );
return( 1 );
}
/* Check the results of the LDAP search operation. */
if ( rc != LDAP_SUCCESS )
{
fprintf( stderr, "ldap_search_ext: %s\n", ldap_err2string( rc ) );
if ( error_msg != NULL & *error_msg != '\0' )
{
fprintf( stderr, "%s\n", error_msg );
}
if ( matched_msg != NULL && *matched_msg != '\0' )
{
fprintf( stderr, "Part of the DN that matches an existing entry: %s\n", matched_msg );
}
}
else
{
printf( "Search completed successfully.\n"
"Entries found: %d\n"
"Search references returned: %d\n",
num_entries, num_refs );
}
break;
default:
break;
}
}
ldap_msgfree(res);
/* Disconnect when done. */
ldap_unbind( ld );
return( 0 );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -