19 package org.sleuthkit.autopsy.commandlineingest;
 
   22 import java.util.ArrayList;
 
   23 import java.util.Collections;
 
   24 import java.util.HashSet;
 
   25 import java.util.List;
 
   28 import java.util.logging.Level;
 
   30 import org.netbeans.api.sendopts.CommandException;
 
   31 import org.netbeans.spi.sendopts.Env;
 
   32 import org.netbeans.spi.sendopts.Option;
 
   33 import org.netbeans.spi.sendopts.OptionProcessor;
 
   34 import org.openide.util.lookup.ServiceProvider;
 
   40 @ServiceProvider(service = OptionProcessor.class)
 
   44     private final Option caseNameOption = Option.requiredArgument(
'n', 
"caseName");
 
   45     private final Option caseTypeOption = Option.requiredArgument(
't', 
"caseType");
 
   46     private final Option caseBaseDirOption = Option.requiredArgument(
'o', 
"caseBaseDir");
 
   47     private final Option createCaseCommandOption = Option.withoutArgument(
'c', 
"createCase");
 
   48     private final Option dataSourcePathOption = Option.requiredArgument(
's', 
"dataSourcePath");
 
   49     private final Option dataSourceObjectIdOption = Option.requiredArgument(
'i', 
"dataSourceObjectId");
 
   50     private final Option addDataSourceCommandOption = Option.withoutArgument(
'a', 
"addDataSource");
 
   51     private final Option caseDirOption = Option.requiredArgument(
'd', 
"caseDir");
 
   52     private final Option runIngestCommandOption = Option.optionalArgument(
'r', 
"runIngest");
 
   53     private final Option listAllDataSourcesCommandOption = Option.withoutArgument(
'l', 
"listAllDataSources");
 
   54     private final Option generateReportsOption = Option.optionalArgument(
'g', 
"generateReports");
 
   55     private final Option defaultArgument = Option.defaultArguments();
 
   57     private boolean runFromCommandLine = 
false;
 
   59     private final List<CommandLineCommand> commands = 
new ArrayList<>();
 
   61     final static String CASETYPE_MULTI = 
"multi";
 
   62     final static String CASETYPE_SINGLE = 
"single";
 
   64     private String defaultArgumentValue = null;
 
   68         Set<Option> set = 
new HashSet<>();
 
   69         set.add(createCaseCommandOption);
 
   70         set.add(caseNameOption);
 
   71         set.add(caseTypeOption);
 
   72         set.add(caseBaseDirOption);
 
   73         set.add(dataSourcePathOption);
 
   74         set.add(addDataSourceCommandOption);
 
   75         set.add(dataSourceObjectIdOption);
 
   76         set.add(caseDirOption);
 
   77         set.add(runIngestCommandOption);
 
   78         set.add(listAllDataSourcesCommandOption);
 
   79         set.add(generateReportsOption);
 
   80         set.add(defaultArgument);
 
   85     protected void process(Env env, Map<Option, String[]> values) throws CommandException {
 
   86         logger.log(Level.INFO, 
"Processing Autopsy command line options"); 
 
   87         System.out.println(
"Processing Autopsy command line options");
 
   89         if (values.containsKey(defaultArgument)) {
 
   90             defaultArgumentValue = values.get(defaultArgument)[0];
 
   91             runFromCommandLine = 
true;
 
   96         if (!(values.containsKey(createCaseCommandOption) || values.containsKey(addDataSourceCommandOption)
 
   97                 || values.containsKey(runIngestCommandOption) || values.containsKey(listAllDataSourcesCommandOption)
 
   98                 || values.containsKey(generateReportsOption))) {
 
  100             handleError(
"Invalid command line, an input option must be supplied.");
 
  105         String inputCaseName = 
"";
 
  106         if (values.containsKey(caseNameOption)) {
 
  107             argDirs = values.get(caseNameOption);
 
  108             if (argDirs.length < 1) {
 
  109                 handleError(
"Missing argument 'caseName'");
 
  111             inputCaseName = argDirs[0];
 
  113             if (inputCaseName == null || inputCaseName.isEmpty()) {
 
  114                 handleError(
"'caseName' argument is empty");
 
  118         String caseType = 
"";
 
  119         if (values.containsKey(caseTypeOption)) {
 
  120             argDirs = values.get(caseTypeOption);
 
  122             if (argDirs.length < 1) {
 
  123                 handleError(
"Missing argument 'caseType'");
 
  125             caseType = argDirs[0];
 
  127             if (caseType == null || caseType.isEmpty()) {
 
  128                 handleError(
"'caseType' argument is empty");
 
  129             } 
else if (!caseType.equalsIgnoreCase(CASETYPE_MULTI) && !caseType.equalsIgnoreCase(CASETYPE_SINGLE)) {
 
  130                 handleError(
"'caseType' argument is invalid");
 
  132                 handleError(
"Unable to create multi user case. Confirm that multi user settings are configured correctly.");
 
  136         String caseBaseDir = 
"";
 
  137         if (values.containsKey(caseBaseDirOption)) {
 
  138             argDirs = values.get(caseBaseDirOption);
 
  139             if (argDirs.length < 1) {
 
  140                 handleError(
"Missing argument 'caseBaseDir'");
 
  142             caseBaseDir = argDirs[0];
 
  144             if (caseBaseDir == null || caseBaseDir.isEmpty()) {
 
  145                 handleError(
"Missing argument 'caseBaseDir' option");
 
  148             if (!(
new File(caseBaseDir).exists()) || !(
new File(caseBaseDir).isDirectory())) {
 
  149                 handleError(
"'caseBaseDir' directory doesn't exist or is not a directory: " + caseBaseDir);
 
  153         String dataSourcePath = 
"";
 
  154         if (values.containsKey(dataSourcePathOption)) {
 
  156             argDirs = values.get(dataSourcePathOption);
 
  157             if (argDirs.length < 1) {
 
  158                 handleError(
"Missing argument 'dataSourcePath'");
 
  160             dataSourcePath = argDirs[0];
 
  163             if (dataSourcePath == null || dataSourcePath.isEmpty()) {
 
  164                 handleError(
"Missing argument 'dataSourcePath'");
 
  167             if (!(
new File(dataSourcePath).exists())) {
 
  168                 handleError(
"Input data source file " + dataSourcePath + 
" doesn't exist");
 
  172         String dataSourceId = 
"";
 
  173         if (values.containsKey(dataSourceObjectIdOption)) {
 
  175             argDirs = values.get(dataSourceObjectIdOption);
 
  176             if (argDirs.length < 1) {
 
  177                 handleError(
"Missing argument 'dataSourceObjectIdOption'");
 
  179             dataSourceId = argDirs[0];
 
  182             if (dataSourceId == null || dataSourceId.isEmpty()) {
 
  183                 handleError(
"Input data source id is empty");
 
  188         if (values.containsKey(caseDirOption)) {
 
  190             argDirs = values.get(caseDirOption);
 
  191             if (argDirs.length < 1) {
 
  192                 handleError(
"Argument missing from 'caseDir' option");
 
  194             caseDir = argDirs[0];
 
  197             if (caseDir == null || caseDir.isEmpty()) {
 
  198                 handleError(
"Argument missing from 'caseDir'");
 
  201             if (!(
new File(caseDir).exists()) || !(
new File(caseDir).isDirectory())) {
 
  202                 handleError(
"Case directory " + caseDir + 
" does not exist or is not a directory");
 
  208         if (values.containsKey(createCaseCommandOption)) {
 
  211             if (inputCaseName == null || inputCaseName.isEmpty()) {
 
  212                 handleError(
"'caseName' argument is empty");
 
  216             if (caseBaseDir == null || caseBaseDir.isEmpty()) {
 
  217                 handleError(
"'caseBaseDir' argument is empty");
 
  220             CommandLineCommand newCommand = 
new CommandLineCommand(CommandLineCommand.CommandType.CREATE_CASE);
 
  221             newCommand.addInputValue(CommandLineCommand.InputType.CASE_NAME.name(), inputCaseName);
 
  222             newCommand.addInputValue(CommandLineCommand.InputType.CASES_BASE_DIR_PATH.name(), caseBaseDir);
 
  223             newCommand.addInputValue(CommandLineCommand.InputType.CASE_TYPE.name(), caseType);
 
  224             commands.add(newCommand);
 
  225             runFromCommandLine = 
true;
 
  229         if (values.containsKey(addDataSourceCommandOption)) {
 
  232             if (!values.containsKey(createCaseCommandOption) && caseDir.isEmpty()) {
 
  234                 handleError(
"'caseDir' argument is empty");
 
  238             if (dataSourcePath == null || dataSourcePath.isEmpty()) {
 
  239                 handleError(
"'dataSourcePath' argument is empty");
 
  242             CommandLineCommand newCommand = 
new CommandLineCommand(CommandLineCommand.CommandType.ADD_DATA_SOURCE);
 
  243             newCommand.addInputValue(CommandLineCommand.InputType.CASE_FOLDER_PATH.name(), caseDir);
 
  244             newCommand.addInputValue(CommandLineCommand.InputType.DATA_SOURCE_PATH.name(), dataSourcePath);
 
  245             commands.add(newCommand);
 
  246             runFromCommandLine = 
true;
 
  249         String ingestProfile = 
"";
 
  251         if (values.containsKey(runIngestCommandOption)) {
 
  253             argDirs = values.get(runIngestCommandOption);
 
  254             if(argDirs != null && argDirs.length > 0) {
 
  255                 ingestProfile = argDirs[0];
 
  259             if (!values.containsKey(createCaseCommandOption) && caseDir.isEmpty()) {
 
  261                 handleError(
"'caseDir' argument is empty");
 
  265             if (!values.containsKey(addDataSourceCommandOption) && dataSourceId.isEmpty()) {
 
  267                 handleError(
"'dataSourceId' argument is empty");
 
  270             CommandLineCommand newCommand = 
new CommandLineCommand(CommandLineCommand.CommandType.RUN_INGEST);
 
  271             newCommand.addInputValue(CommandLineCommand.InputType.CASE_FOLDER_PATH.name(), caseDir);
 
  272             newCommand.addInputValue(CommandLineCommand.InputType.DATA_SOURCE_ID.name(), dataSourceId);
 
  273             newCommand.addInputValue(CommandLineCommand.InputType.INGEST_PROFILE_NAME.name(), ingestProfile);
 
  274             commands.add(newCommand);
 
  275             runFromCommandLine = 
true;
 
  279         if (values.containsKey(listAllDataSourcesCommandOption)) {
 
  282             if (!values.containsKey(createCaseCommandOption) && (caseDir == null || caseDir.isEmpty())) {
 
  284                 handleError(
"'caseDir' argument is empty");
 
  287             CommandLineCommand newCommand = 
new CommandLineCommand(CommandLineCommand.CommandType.LIST_ALL_DATA_SOURCES);
 
  288             newCommand.addInputValue(CommandLineCommand.InputType.CASE_FOLDER_PATH.name(), caseDir);
 
  289             commands.add(newCommand);
 
  290             runFromCommandLine = 
true;
 
  294         String reportProfile = null;
 
  295         if (values.containsKey(generateReportsOption)) {
 
  298             if (!values.containsKey(createCaseCommandOption) && caseDir.isEmpty()) {
 
  300                 handleError(
"'caseDir' argument is empty");
 
  303             argDirs = values.get(generateReportsOption);
 
  304             if (argDirs.length > 0) {
 
  305                 reportProfile = argDirs[0];
 
  311             if (reportProfile != null && reportProfile.isEmpty()) {
 
  312                 handleError(
"'generateReports' argument is empty");
 
  315             CommandLineCommand newCommand = 
new CommandLineCommand(CommandLineCommand.CommandType.GENERATE_REPORTS);
 
  316             newCommand.addInputValue(CommandLineCommand.InputType.CASE_FOLDER_PATH.name(), caseDir);
 
  317             if (reportProfile != null) {
 
  318                 newCommand.addInputValue(CommandLineCommand.InputType.REPORT_PROFILE_NAME.name(), reportProfile);
 
  320             commands.add(newCommand);
 
  321             runFromCommandLine = 
true;
 
  331         return runFromCommandLine;
 
  340         return defaultArgumentValue;
 
  348     List<CommandLineCommand> getCommands() {
 
  349         return Collections.unmodifiableList(commands);
 
  359     private void handleError(String errorMessage) 
throws CommandException {
 
  360         logger.log(Level.SEVERE, errorMessage);
 
  361         throw new CommandException(1, errorMessage);
 
String getDefaultArgument()
boolean isRunFromCommandLine()
void handleError(String errorMessage)
static boolean canCreateMultiUserCases()
synchronized static Logger getLogger(String name)
Set< Option > getOptions()
void process(Env env, Map< Option, String[]> values)