| | |
| | | import java.io.FileInputStream; |
| | | import java.io.InputStream; |
| | | import java.io.IOException; |
| | | import java.io.OutputStream; |
| | | import java.io.PrintStream; |
| | | import java.util.ArrayList; |
| | | import java.util.LinkedList; |
| | | import java.util.List; |
| | |
| | | import org.opends.server.protocols.ldap.ProtocolOp; |
| | | import org.opends.server.types.Attribute; |
| | | import org.opends.server.types.LDIFImportConfig; |
| | | import org.opends.server.types.NullOutputStream; |
| | | import org.opends.server.util.AddChangeRecordEntry; |
| | | import org.opends.server.util.ChangeRecordEntry; |
| | | import org.opends.server.util.LDIFException; |
| | |
| | | // The message ID counter to use for requests. |
| | | private AtomicInteger nextMessageID; |
| | | |
| | | // The print stream to use for standard error. |
| | | private PrintStream err; |
| | | |
| | | // The print stream to use for standard output. |
| | | private PrintStream out; |
| | | |
| | | // The LDIF file name. |
| | | private String fileName = null; |
| | | |
| | |
| | | * @param fileName The name of the file containing the LDIF data to use |
| | | * for the modifications. |
| | | * @param nextMessageID The message ID counter to use for requests. |
| | | * @param out The print stream to use for standard output. |
| | | * @param err The print stream to use for standard error. |
| | | */ |
| | | public LDAPModify(String fileName, AtomicInteger nextMessageID) |
| | | public LDAPModify(String fileName, AtomicInteger nextMessageID, |
| | | PrintStream out, PrintStream err) |
| | | { |
| | | if(fileName == null) |
| | | { |
| | |
| | | } |
| | | |
| | | this.nextMessageID = nextMessageID; |
| | | this.out = out; |
| | | this.err = err; |
| | | } |
| | | |
| | | |
| | |
| | | int msgID = MSGID_LDIF_FILE_INVALID_LDIF_ENTRY; |
| | | String message = getMessage(msgID, le.getLineNumber(), fileName, |
| | | String.valueOf(le)); |
| | | System.err.println(message); |
| | | err.println(message); |
| | | continue; |
| | | } |
| | | } catch (Exception e) |
| | |
| | | { |
| | | int msgID = MSGID_LDIF_FILE_READ_ERROR; |
| | | String message = getMessage(msgID, fileName, String.valueOf(e)); |
| | | System.err.println(message); |
| | | err.println(message); |
| | | continue; |
| | | } |
| | | } |
| | |
| | | } |
| | | protocolOp = new AddRequestProtocolOp(asn1OctetStr, attributes); |
| | | msgID = MSGID_PROCESSING_OPERATION; |
| | | System.out.println(getMessage(msgID, operationType, asn1OctetStr)); |
| | | out.println(getMessage(msgID, operationType, asn1OctetStr)); |
| | | break; |
| | | case DELETE: |
| | | operationType = "DELETE"; |
| | | protocolOp = new DeleteRequestProtocolOp(asn1OctetStr); |
| | | msgID = MSGID_PROCESSING_OPERATION; |
| | | System.out.println(getMessage(msgID, operationType, asn1OctetStr)); |
| | | out.println(getMessage(msgID, operationType, asn1OctetStr)); |
| | | break; |
| | | case MODIFY: |
| | | operationType = "MODIFY"; |
| | |
| | | new ArrayList<LDAPModification>(modEntry.getModifications()); |
| | | protocolOp = new ModifyRequestProtocolOp(asn1OctetStr, mods); |
| | | msgID = MSGID_PROCESSING_OPERATION; |
| | | System.out.println(getMessage(msgID, operationType, asn1OctetStr)); |
| | | out.println(getMessage(msgID, operationType, asn1OctetStr)); |
| | | break; |
| | | case MODIFY_DN: |
| | | operationType = "MODIFY DN"; |
| | |
| | | modDNEntry.deleteOldRDN()); |
| | | } |
| | | msgID = MSGID_PROCESSING_OPERATION; |
| | | System.out.println(getMessage(msgID, operationType, asn1OctetStr)); |
| | | out.println(getMessage(msgID, operationType, asn1OctetStr)); |
| | | break; |
| | | default: |
| | | break; |
| | |
| | | { |
| | | assert debugException(CLASS_NAME, "readAndExecute", ae); |
| | | msgID = MSGID_OPERATION_FAILED; |
| | | System.err.println(getMessage(msgID, operationType, asn1OctetStr, |
| | | ae.getMessage())); |
| | | err.println(getMessage(msgID, operationType, asn1OctetStr, |
| | | ae.getMessage())); |
| | | if(!modifyOptions.continueOnError()) |
| | | { |
| | | throw new IOException(ae.getMessage()); |
| | |
| | | throw new LDAPException(resultCode, msgID, msg); |
| | | } else |
| | | { |
| | | System.err.println(msg); |
| | | err.println(msg); |
| | | } |
| | | } else |
| | | { |
| | | msgID = MSGID_OPERATION_SUCCESSFUL; |
| | | String msg = getMessage(msgID, operationType, asn1OctetStr); |
| | | System.out.println(msg); |
| | | out.println(msg); |
| | | |
| | | if (errorMessage != null) |
| | | { |
| | | System.out.println(errorMessage); |
| | | out.println(errorMessage); |
| | | } |
| | | |
| | | if (referralURLs != null) |
| | | { |
| | | System.out.println(referralURLs); |
| | | out.println(referralURLs); |
| | | } |
| | | } |
| | | |
| | |
| | | if (controlValue == null) |
| | | { |
| | | msgID = MSGID_LDAPMODIFY_PREREAD_NO_VALUE; |
| | | System.err.println(getMessage(msgID)); |
| | | err.println(getMessage(msgID)); |
| | | continue; |
| | | } |
| | | |
| | |
| | | catch (ASN1Exception ae) |
| | | { |
| | | msgID = MSGID_LDAPMODIFY_PREREAD_CANNOT_DECODE_VALUE; |
| | | System.err.println(getMessage(msgID, ae.getMessage())); |
| | | err.println(getMessage(msgID, ae.getMessage())); |
| | | continue; |
| | | } |
| | | catch (LDAPException le) |
| | | { |
| | | msgID = MSGID_LDAPMODIFY_PREREAD_CANNOT_DECODE_VALUE; |
| | | System.err.println(getMessage(msgID, le.getMessage())); |
| | | err.println(getMessage(msgID, le.getMessage())); |
| | | continue; |
| | | } |
| | | |
| | | StringBuilder buffer = new StringBuilder(); |
| | | searchEntry.toLDIF(buffer, 78); |
| | | System.out.println(getMessage(MSGID_LDAPMODIFY_PREREAD_ENTRY)); |
| | | System.out.println(buffer); |
| | | out.println(getMessage(MSGID_LDAPMODIFY_PREREAD_ENTRY)); |
| | | out.println(buffer); |
| | | } |
| | | else if (oid.equals(OID_LDAP_READENTRY_POSTREAD)) |
| | | { |
| | |
| | | if (controlValue == null) |
| | | { |
| | | msgID = MSGID_LDAPMODIFY_POSTREAD_NO_VALUE; |
| | | System.err.println(getMessage(msgID)); |
| | | err.println(getMessage(msgID)); |
| | | continue; |
| | | } |
| | | |
| | |
| | | catch (ASN1Exception ae) |
| | | { |
| | | msgID = MSGID_LDAPMODIFY_POSTREAD_CANNOT_DECODE_VALUE; |
| | | System.err.println(getMessage(msgID, ae.getMessage())); |
| | | err.println(getMessage(msgID, ae.getMessage())); |
| | | continue; |
| | | } |
| | | catch (LDAPException le) |
| | | { |
| | | msgID = MSGID_LDAPMODIFY_POSTREAD_CANNOT_DECODE_VALUE; |
| | | System.err.println(getMessage(msgID, le.getMessage())); |
| | | err.println(getMessage(msgID, le.getMessage())); |
| | | continue; |
| | | } |
| | | |
| | | StringBuilder buffer = new StringBuilder(); |
| | | searchEntry.toLDIF(buffer, 78); |
| | | System.out.println(getMessage(MSGID_LDAPMODIFY_POSTREAD_ENTRY)); |
| | | System.out.println(buffer); |
| | | out.println(getMessage(MSGID_LDAPMODIFY_POSTREAD_ENTRY)); |
| | | out.println(buffer); |
| | | } |
| | | } |
| | | } |
| | |
| | | |
| | | public static void main(String[] args) |
| | | { |
| | | int retCode = mainModify(args); |
| | | int retCode = mainModify(args, true, System.out, System.err); |
| | | |
| | | if(retCode != 0) |
| | | { |
| | |
| | | |
| | | public static int mainModify(String[] args) |
| | | { |
| | | return mainModify(args, true, System.out, System.err); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * Parses the provided command-line arguments and uses that information to |
| | | * run the ldapmodify tool. |
| | | * |
| | | * @param args The command-line arguments provided to this |
| | | * program. |
| | | * @param initializeServer Indicates whether to initialize the server. |
| | | * @param outStream The output stream to use for standard output, or |
| | | * <CODE>null</CODE> if standard output is not |
| | | * needed. |
| | | * @param errStream The output stream to use for standard error, or |
| | | * <CODE>null</CODE> if standard error is not |
| | | * needed. |
| | | * |
| | | * @return The error code. |
| | | */ |
| | | |
| | | public static int mainModify(String[] args, boolean initializeServer, |
| | | OutputStream outStream, OutputStream errStream) |
| | | { |
| | | PrintStream out; |
| | | if (outStream == null) |
| | | { |
| | | out = NullOutputStream.printStream(); |
| | | } |
| | | else |
| | | { |
| | | out = new PrintStream(outStream); |
| | | } |
| | | |
| | | PrintStream err; |
| | | if (errStream == null) |
| | | { |
| | | err = NullOutputStream.printStream(); |
| | | } |
| | | else |
| | | { |
| | | err = new PrintStream(errStream); |
| | | } |
| | | |
| | | |
| | | LDAPConnectionOptions connectionOptions = new LDAPConnectionOptions(); |
| | | LDAPModifyOptions modifyOptions = new LDAPModifyOptions(); |
| | | LDAPConnection connection = null; |
| | |
| | | showUsage = new BooleanArgument("showUsage", 'H', "help", |
| | | MSGID_DESCRIPTION_SHOWUSAGE); |
| | | argParser.addArgument(showUsage); |
| | | argParser.setUsageArgument(showUsage); |
| | | argParser.setUsageArgument(showUsage, out); |
| | | controlStr = new StringArgument("controls", 'J', "controls", false, |
| | | false, true, |
| | | "{controloid[:criticality[:value|::b64value|:<fileurl]]}", |
| | |
| | | int msgID = MSGID_ENCPW_CANNOT_INITIALIZE_ARGS; |
| | | String message = getMessage(msgID, ae.getMessage()); |
| | | |
| | | System.err.println(message); |
| | | err.println(message); |
| | | return 1; |
| | | } |
| | | |
| | |
| | | int msgID = MSGID_ENCPW_ERROR_PARSING_ARGS; |
| | | String message = getMessage(msgID, ae.getMessage()); |
| | | |
| | | System.err.println(message); |
| | | System.err.println(argParser.getUsage()); |
| | | err.println(message); |
| | | err.println(argParser.getUsage()); |
| | | return 1; |
| | | } |
| | | |
| | |
| | | |
| | | if(bindPassword.isPresent() && bindPasswordFile.isPresent()) |
| | | { |
| | | System.err.println("ERROR: Both -w and -j flags specified. " + |
| | | "Please specify one."); |
| | | err.println("ERROR: Both -w and -j flags specified. " + |
| | | "Please specify one."); |
| | | return 1; |
| | | } |
| | | |
| | |
| | | } catch(ArgumentException ae) |
| | | { |
| | | assert debugException(CLASS_NAME, "main", ae); |
| | | System.err.println(ae.getMessage()); |
| | | err.println(ae.getMessage()); |
| | | return 1; |
| | | } |
| | | |
| | |
| | | if(versionNumber != 2 && versionNumber != 3) |
| | | { |
| | | int msgID = MSGID_DESCRIPTION_INVALID_VERSION; |
| | | System.err.println(getMessage(msgID, versionNumber)); |
| | | err.println(getMessage(msgID, versionNumber)); |
| | | return 1; |
| | | } |
| | | connectionOptions.setVersionNumber(versionNumber); |
| | | } catch(ArgumentException ae) |
| | | { |
| | | assert debugException(CLASS_NAME, "main", ae); |
| | | System.err.println(ae.getMessage()); |
| | | err.println(ae.getMessage()); |
| | | return 1; |
| | | } |
| | | |
| | |
| | | // read the password from the stdin. |
| | | try |
| | | { |
| | | System.out.print(getMessage(MSGID_LDAPAUTH_PASSWORD_PROMPT, |
| | | bindDNValue)); |
| | | out.print(getMessage(MSGID_LDAPAUTH_PASSWORD_PROMPT, bindDNValue)); |
| | | char[] pwChars = PasswordReader.readPassword(); |
| | | bindPasswordValue = new String(pwChars); |
| | | } catch(Exception ex) |
| | | { |
| | | assert debugException(CLASS_NAME, "main", ex); |
| | | System.err.println(ex.getMessage()); |
| | | err.println(ex.getMessage()); |
| | | return 1; |
| | | } |
| | | } else if(bindPasswordValue == null) |
| | |
| | | LDAPControl ctrl = LDAPToolUtils.getControl(ctrlString); |
| | | if(ctrl == null) |
| | | { |
| | | System.err.println("Invalid control specified:" + ctrlString); |
| | | System.out.println(argParser.getUsage()); |
| | | err.println("Invalid control specified:" + ctrlString); |
| | | err.println(argParser.getUsage()); |
| | | return 1; |
| | | } |
| | | modifyOptions.getControls().add(ctrl); |
| | |
| | | } |
| | | catch (LDAPException le) |
| | | { |
| | | System.err.println(getMessage(MSGID_LDAP_ASSERTION_INVALID_FILTER, |
| | | le.getMessage())); |
| | | err.println(getMessage(MSGID_LDAP_ASSERTION_INVALID_FILTER, |
| | | le.getMessage())); |
| | | return 1; |
| | | } |
| | | } |
| | |
| | | { |
| | | if(!connectionOptions.useSSL() && !connectionOptions.useStartTLS()) |
| | | { |
| | | System.err.println("SASL External requires either SSL or StartTLS " + |
| | | "options to be requested."); |
| | | err.println("SASL External requires either SSL or StartTLS " + |
| | | "options to be requested."); |
| | | return 1; |
| | | } |
| | | if(keyStorePathValue == null) |
| | | { |
| | | System.err.println("SASL External requires a path to the SSL " + |
| | | "client certificate keystore."); |
| | | err.println("SASL External requires a path to the SSL " + |
| | | "client certificate keystore."); |
| | | return 1; |
| | | } |
| | | } |
| | | |
| | | try |
| | | { |
| | | // Bootstrap and initialize directory data structures. |
| | | DirectoryServer.bootstrapClient(); |
| | | if (initializeServer) |
| | | { |
| | | // Bootstrap and initialize directory data structures. |
| | | DirectoryServer.bootstrapClient(); |
| | | } |
| | | |
| | | // Connect to the specified host with the supplied userDN and password. |
| | | SSLConnectionFactory sslConnectionFactory = null; |
| | |
| | | |
| | | AtomicInteger nextMessageID = new AtomicInteger(1); |
| | | connection = new LDAPConnection(hostNameValue, portNumber, |
| | | connectionOptions); |
| | | connectionOptions, out, err); |
| | | connection.connectToHost(bindDNValue, bindPasswordValue, nextMessageID); |
| | | |
| | | LDAPModify ldapModify = new LDAPModify(fileNameValue, nextMessageID); |
| | | LDAPModify ldapModify = new LDAPModify(fileNameValue, nextMessageID, |
| | | out, err); |
| | | InputStream is = System.in; |
| | | if(fileNameValue != null) |
| | | { |
| | |
| | | } catch(LDAPException le) |
| | | { |
| | | assert debugException(CLASS_NAME, "main", le); |
| | | System.err.println(le.getMessage()); |
| | | err.println(le.getMessage()); |
| | | int code = le.getResultCode(); |
| | | return code; |
| | | } catch(LDAPConnectionException lce) |
| | | { |
| | | assert debugException(CLASS_NAME, "main", lce); |
| | | System.err.println(lce.getMessage()); |
| | | err.println(lce.getMessage()); |
| | | int code = lce.getErrorCode(); |
| | | return code; |
| | | } catch(Exception e) |
| | | { |
| | | assert debugException(CLASS_NAME, "main", e); |
| | | System.err.println(e.getMessage()); |
| | | err.println(e.getMessage()); |
| | | return 1; |
| | | } finally |
| | | { |