#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ldap.h>
#include <ldapx.h>
#if defined(N_PLAT_NLM) && defined(LIBC)
#include <screen.h>
#endif
int listIndexes (LDAP *ld, char *serverDN );
int addIndex (LDAP *ld, char *serverDN, char *indexName, int rule,
char *attribute );
int deleteIndex (LDAP *ld, char *serverDN, char *indexName );
static char usage[] =
"Usage: index <operation type> <host name> <port number> <loginDN> <password>"
"\n <server DN> <index name> <eDirectory attribute> <rule>"
"\nwhere"
"\n<operation type>: 1 - list all indexes"
"\n 2 - add index"
"\n 3 - delete index"
"\n<rule>: (valid only while creating the index)"
"\n 0 - value"
"\n 1 - presence"
"\n 2 - substring"
"\nExamples:"
"\nindex 1 Acme.com 389 cn=admin,o=Acme secret cn=myserver,o=Acme"
"\nindex 2 Acme.com 389 cn=admin,o=Acme secret cn=myserver,o=Acme myIndex city 0"
"\nindex 3 Acme.com 389 cn=admin,o=Acme secret cn=myserver,o=Acme myIndex\n";
int main( int argc, char **argv)
{
int version, ldapPort, operationType, rule, rc;
char *ldapHost, *loginDN, *password, *dn = NULL;
char *serverDN, *indexName, *attribute;
LDAP *ld;
struct timeval timeOut = {10,0};
#if defined(N_PLAT_NLM) && defined(LIBC)
setscreenmode(SCR_NO_MODE);
#endif
if ( argc < 7 )
{
printf("%s", usage);
return ( 1 );
}
switch ( atoi(argv[1]) )
{
case 1:
if (argc != 7 )
{
printf("%s", usage);
return ( 1 );
}
break;
case 2:
if (argc != 10 )
{
printf("%s", usage);
return ( 1 );
}
indexName = argv[7];
attribute = argv[8];
rule = atoi(argv[9]);
break;
case 3:
if (argc != 8 )
{
printf("%s", usage);
return ( 1 );
}
indexName = argv[7];
break;
default:
printf("%s", usage);
return ( 1 );
};
operationType = atoi(argv[1]);
ldapHost = argv[2];
ldapPort = atoi(argv[3]);
loginDN = argv[4];
password = argv[5];
serverDN = argv[6];
version = LDAP_VERSION3;
ldap_set_option( NULL, LDAP_OPT_PROTOCOL_VERSION, &version);
ldap_set_option( NULL, LDAP_OPT_NETWORK_TIMEOUT, &timeOut);
if (( ld = ldap_init( ldapHost, ldapPort )) == NULL)
{
printf("\n LDAP session initialization failed\n");
return( 1 );
}
printf ( "\n LDAP session initialized\n");
rc = ldap_simple_bind_s( ld, loginDN, password );
if (rc != LDAP_SUCCESS )
{
printf("ldap_simple_bind_s: %s\n", ldap_err2string( rc ));
ldap_unbind_s ( ld );
return( 1 );
}
printf("\n Bind successful\n");
switch (operationType)
{
case 1 :
rc = listIndexes ( ld, serverDN );
break;
case 2 :
rc = addIndex (ld, serverDN, indexName, rule, attribute);
break;
case 3 :
rc = deleteIndex (ld, serverDN, indexName );
break;
}
ldap_unbind_s( ld );
return( rc );
}
int listIndexes (LDAP *ld, char *serverDN )
{
char *index_state[] = { "Online",
"Suspended",
"Bringing online(low)",
"Bringing online(high)",
"Creating" };
char *index_rule[] = { "Value",
"Presence",
"Substring" };
char *index_type[] = { "User defined",
"Added on attribute creation",
"required for operation",
"Syntax index" };
char *index_value[] = { "Uninitialized",
"Added from server",
"Added from local DIB",
"Deleted from local DIB",
"Modified from local DIB" };
int rc, i ;
char *attribute, **values;
char *requiredAttributes[2];
BerElement *ber;
LDAPMessage *searchResult, *entry;
requiredAttributes[0]="indexDefinition";
requiredAttributes[1]=NULL;
rc = ldap_search_ext_s(
ld,
serverDN,
LDAP_SCOPE_BASE,
"(objectClass=*)",
requiredAttributes,
0,
NULL,
NULL,
NULL,
LDAP_NO_LIMIT,
&searchResult );
if ( rc != LDAP_SUCCESS )
{
printf("ldap_search_ext_s: %s\n", ldap_err2string( rc ));
ldap_msgfree( searchResult );
return ( rc );
}
if ( (entry = ldap_first_entry( ld, searchResult )) != NULL)
{
for ( attribute = ldap_first_attribute( ld, entry, &ber );
attribute != NULL;
attribute = ldap_next_attribute( ld, entry, ber ) )
{
if (( values = ldap_get_values( ld, entry, attribute)) != NULL )
{
for ( i = 0; values[i] != NULL; i++ )
{
printf("\n Index Version: %s\n", strtok(values[i] ,"$"));
printf(" Index Name: %s\n", strtok(NULL,"$"));
printf(" Index State: %s\n",
index_state[atoi(strtok(NULL,"$"))]);
printf(" Index Rule: %s\n",
index_rule[atoi(strtok(NULL,"$"))]);
printf(" Index Type: %s\n",
index_type[atoi(strtok(NULL,"$"))]);
printf(" Index State Value: %s\n",
index_value[atoi(strtok(NULL,"$"))]);
printf(" eDirectory Attribute: %s\n", strtok(NULL,"$"));
}
ldap_value_free( values );
}
ldap_memfree( attribute );
}
ber_free(ber, 0);
}
ldap_msgfree( searchResult );
return LDAP_SUCCESS;
}
int addIndex (LDAP *ld, char *serverDN, char *indexName, int rule, char *attribute)
{
int rc;
char *definitions[2];
LDAPMod modIndex, *modify[2];
modIndex.mod_op = LDAP_MOD_ADD;
modIndex.mod_type = "indexDefinition";
definitions[0]=(char *) malloc( sizeof( "0$$2$$0$1$" ) + strlen( indexName )
+ sizeof ( rule ) + strlen( attribute ) + 1 );
sprintf(definitions[0],"0$%s$2$%d$0$1$%s",indexName,rule,attribute);
definitions[1] = NULL;
modIndex.mod_values = definitions;
modify[ 0 ] = &modIndex;
modify[ 1 ] = NULL;
rc = ldap_modify_ext_s( ld,
serverDN,
modify,
NULL,
NULL);
free( definitions[0] );
if ( rc != LDAP_SUCCESS )
{
printf("\n ldap_add_ext_s: %s\n", ldap_err2string( rc ));
return( rc );
}
printf("\n Index successfully added: %s", indexName);
printf("\n Initiating Limber Process.\n");
rc = ldap_trigger_back_process(ld, LDAP_BK_PROCESS_LIMBER);
if ( rc != LDAP_SUCCESS ) {
printf("\n ldap_trigger_back_process: %s\n", ldap_err2string( rc ));
return( rc );
}
printf("\n Limber process successfully initiated.\n");
return LDAP_SUCCESS;
}
deleteIndex (LDAP *ld, char *serverDN, char *indexName )
{
int i, rc=0;
char *attribute, *actualIndex=NULL;
char **values, *requiredAttributes[2], *definitions[2];
LDAPMessage *searchResult, *entry;
LDAPMod modIndex, *modify[2];
BerElement *ber;
requiredAttributes[0] = "indexDefinition";
requiredAttributes[1] = NULL;
rc = ldap_search_ext_s(
ld,
serverDN,
LDAP_SCOPE_BASE,
"(objectClass=*)",
requiredAttributes,
0,
NULL,
NULL,
NULL,
LDAP_NO_LIMIT,
&searchResult );
if ( rc != LDAP_SUCCESS )
{
printf("ldap_search_ext_s: %s\n", ldap_err2string( rc ));
ldap_msgfree( searchResult );
return ( rc );
}
if ( (entry = ldap_first_entry( ld, searchResult )) != NULL)
{
if ( (attribute = ldap_first_attribute( ld, entry, &ber )) != NULL )
{
if (( values = ldap_get_values( ld, entry, attribute)) != NULL )
{
for ( i = 0; values[i] != NULL; i++ )
{
char *tmpValue, *token;
tmpValue = strdup(values[i]);
token = strtok( tmpValue,"$" );
token = strtok( NULL , "$" );
if (token && (strcmp(token,indexName))==0)
{
actualIndex = strdup( values[i] );
free( tmpValue );
break;
}
free( tmpValue );
}
ldap_value_free( values );
}
ldap_memfree( attribute );
}
ber_free(ber, 0);
}
ldap_msgfree( searchResult );
if (!actualIndex)
{
printf("\n No such index: %s\n",indexName);
return ( 1 );
}
modIndex.mod_op = LDAP_MOD_DELETE;
modIndex.mod_type = "indexDefinition";
definitions[0] = actualIndex;
definitions[1] = NULL;
modIndex.mod_values = definitions;
modify[ 0 ] = &modIndex;
modify[ 1 ] = NULL;
rc = ldap_modify_ext_s( ld,
serverDN,
modify,
NULL,
NULL);
free( actualIndex );
if ( rc != LDAP_SUCCESS ) {
printf("\n ldap_modify_ext_s: %s\n", ldap_err2string( rc ));
return( rc );
}
printf("\n Index successfully deleted: %s\n", indexName);
printf("\n Initiating Limber Process.\n");
rc = ldap_trigger_back_process(ld, LDAP_BK_PROCESS_LIMBER);
if ( rc != LDAP_SUCCESS ) {
printf("\n ldap_trigger_back_process: %s\n", ldap_err2string( rc ));
return( rc );
}
printf("\n Limber process successfully initiated.\n");
return LDAP_SUCCESS;
}