import java.io.UnsupportedEncodingException;
import java.util.Vector;
import com.novell.ldap.LDAPConnection;
import com.novell.ldap.LDAPException;
import com.novell.ldap.LDAPExtendedOperation;
import com.novell.ldap.LDAPExtendedResponse;
import com.novell.ldap.LDAPLocalException;
import com.novell.ldap.extensions.LDAPBackupRequest;
import com.novell.ldap.extensions.LDAPBackupResponse;
import com.novell.ldap.extensions.LDAPRestoreRequest;
public class GetLDAPBackupRestore {
public GetLDAPBackupRestore() {
super();
}
public static void main(String[] args) {
if (!(args.length == 5 || args.length == 6 || args.length == 7)) {
System.err.println("Usage: java GetLDAPBackupRestore <host Name> "
+ "<port number> <login dn> <password>\n "
+ " <object DN> [encrypted password (optional)]" +
" [state Info (optional)]");
System.err.println("\nFor Non encrypted objects::");
System.err.println("--------------------------");
System.err.println(" Example: java GetLDAPBackupRestore Acme.com 389 "
+ "\"cn=Admin,o=Acme\" secret\n "
+ "\"cn=TestUser,o=Acme\"");
System.err.println("\nFor Encrypted objects::");
System.err.println("----------------------");
System.err.println(" Example: java GetLDAPBackupRestore Acme.com 389 "
+ "\"cn=Admin,o=Acme\" secret\n "
+ "\"cn=TestUser,o=Acme\" testpassword");
System.exit(1);
}
int ldapVersion = LDAPConnection.LDAP_V3;
String ldapHost = args[0];
int ldapPort = Integer.parseInt(args[1]);
String loginDN = args[2];
String password = args[3];
String objectDN = args[4];
String encPasswd = null;
String stateInfo = null;
if(args.length == 6 && args[5] != null)
{
encPasswd = args[5];
}
if(args.length == 7 && args[6] != null)
{
stateInfo = args[6];
}
LDAPConnection ld = new LDAPConnection();
try {
ld.connect(ldapHost, ldapPort);
ld.bind(ldapVersion, loginDN, password.getBytes("UTF8"));
System.out.println("\nLogin succeeded");
System.out.println("\n Object DN =" + objectDN);
Vector testData;
if(encPasswd == null)
testData = backup(ld, objectDN, null, stateInfo);
else
testData = backup(ld, objectDN, encPasswd.getBytes("UTF8"),
stateInfo);
if(encPasswd == null)
restore(ld, objectDN, null, testData);
else
restore(ld, objectDN, encPasswd.getBytes("UTF8"), testData);
if (ld.isConnected())
ld.disconnect();
} catch (LDAPException e) {
System.out.println("Error: " + e.toString());
} catch (UnsupportedEncodingException e) {
System.out.println("Error: " + e.toString());
}
}
public static Vector backup(LDAPConnection ld, String objectDN,
byte[] passwd, String stateInfo) {
int intInfo;
String strInfo;
byte[] returnedBuffer;
Vector testData = new Vector(4);
testData.add(0, new Integer(-1));
try {
LDAPExtendedOperation request = new LDAPBackupRequest(objectDN,
passwd, stateInfo);
LDAPExtendedResponse response = ld.extendedOperation(request);
int result = response.getResultCode();
testData.remove(0);
testData.add(0, new Integer(result));
if ((result == LDAPException.SUCCESS)
&& (response instanceof LDAPBackupResponse)) {
System.out.println("Backup Info:");
strInfo = ((LDAPBackupResponse) response).getStatusInfo();
System.out.println(" Status Info: " + strInfo);
intInfo = ((LDAPBackupResponse) response).getBufferLength();
System.out.println(" Buffer length: " + intInfo);
testData.add(new Integer(intInfo));
strInfo = ((LDAPBackupResponse) response).getChunkSizesString();
System.out.println(" Chunk sizes: " + strInfo);
testData.add(strInfo);
returnedBuffer = ((LDAPBackupResponse) response).getReturnedBuffer();
testData.add(returnedBuffer);
System.out.println("\nbackup information succeeded\n");
} else {
System.out.println("Could not backup the information.\n");
throw new LDAPException(response.getErrorMessage(), response
.getResultCode(), (String) null);
}
} catch (LDAPException e) {
System.out.println("Error: " + e.toString());
}
return testData;
}
public static void restore(LDAPConnection ld, String objectDN,
byte[] passwd, Vector testData) {
int intInfo;
String strInfo;
try {
if(((Integer)testData.get(0)).intValue() != 0){
System.out.println("Note: The test program did not proceed " +
"with restore since backup was not proper");
System.exit(0);
}
LDAPExtendedOperation request = new LDAPRestoreRequest(
objectDN, passwd,
((Integer)testData.get(1)).intValue(),
(String)testData.get(2),
(byte[])testData.get(3));
LDAPExtendedResponse response = ld.extendedOperation(request);
if ( response.getResultCode() == LDAPException.SUCCESS )
System.out.println("Restore Request succeeded\n");
else {
System.out.println("Restore Request Failed");
throw new LDAPException( response.getErrorMessage(),
response.getResultCode(),
(String)null);
}
} catch (LDAPException e) {
System.out.println("Error: " + e.toString());
}
}
}