#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ldap.h>
#include <ldapx.h>
static char usage[] =
"\n Usage: backup <host name> <port number> <login dn> <password>"
"\n\t <object dn> <object Encryption passwd>\n"
"\n Example: backup acme.com 389 cn=admin,o=acme secret"
"\n\t cn=James,ou=sales,o=acme password\n";
int main(int argc, char **argv)
{
int rc, version, ldapPort;
char *ldapHost;
char *loginDN;
char *password = NULL;
char *encrypt = NULL;
char *dn;
char *objectInfo=NULL, *stateInfo=NULL, *chunckSize=NULL;
LDAP *ld;
int size, err = LDAP_SUCCESS;
struct timeval timeOut = {10,0};
if (argc != 6 && argc != 7)
{
printf("%s", usage);
return(1);
}
ldapHost = argv[1];
ldapPort = atoi(argv[2]);
loginDN = argv[3];
password = argv[4];
dn = argv[5];
if(argc == 7)
encrypt = argv[6];
version = LDAP_VERSION3;
ldap_set_option( NULL, LDAP_OPT_PROTOCOL_VERSION, &version);
ldap_set_option( NULL, LDAP_OPT_NETWORK_TIMEOUT, &timeOut);
printf( " Getting the handle to the LDAP connection...\n" );
if ( (ld = ldap_init( ldapHost, ldapPort )) == NULL ) {
perror( "ldap_init" );
return( 1 );
}
printf( " Binding to the directory...\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( " Backing up the directory object: %s ...\n", dn);
rc = ldap_backup_object ( ld,
dn,
encrypt,
&stateInfo,
&objectInfo,
&chunckSize,
&size);
if (rc != LDAP_SUCCESS) {
printf("ldap_backup: %s \n", ldap_err2string(rc));
goto done;
}
printf(" Backup done successfully.\n\n");
printf(" Restoring the directory object: %s ...\n", dn );
printf(" Press any key to continue ... ");getchar();
rc = ldap_restore_object ( ld,
dn,
encrypt,
objectInfo,
chunckSize,
size);
if (rc != LDAP_SUCCESS) {
printf("ldap_restore: %s \n", ldap_err2string(rc));
goto done;
}
printf("\n Object restored successfully.\n\n");
ldap_unbind_s( ld );
done:
if (objectInfo)
ldapx_memfree( objectInfo );
if (stateInfo)
ldapx_memfree( stateInfo );
return (rc);
}