19 package org.sleuthkit.autopsy.commandlineingest;
 
   22 import java.util.ArrayList;
 
   23 import java.util.Arrays;
 
   24 import java.util.Collections;
 
   25 import java.util.HashSet;
 
   26 import java.util.List;
 
   29 import java.util.logging.Level;
 
   30 import java.util.stream.Collectors;
 
   31 import java.util.stream.Stream;
 
   33 import org.netbeans.api.sendopts.CommandException;
 
   34 import org.netbeans.spi.sendopts.Env;
 
   35 import org.netbeans.spi.sendopts.Option;
 
   36 import org.netbeans.spi.sendopts.OptionProcessor;
 
   37 import org.openide.util.lookup.ServiceProvider;
 
   43 @ServiceProvider(service = OptionProcessor.class)
 
   47     private final Option caseNameOption = Option.requiredArgument(
'n', 
"caseName");
 
   48     private final Option caseTypeOption = Option.requiredArgument(
't', 
"caseType");
 
   49     private final Option caseBaseDirOption = Option.requiredArgument(
'o', 
"caseBaseDir");
 
   50     private final Option createCaseCommandOption = Option.withoutArgument(
'c', 
"createCase");
 
   51     private final Option dataSourcePathOption = Option.requiredArgument(
's', 
"dataSourcePath");
 
   52     private final Option dataSourceObjectIdOption = Option.requiredArgument(
'i', 
"dataSourceObjectId");
 
   53     private final Option addDataSourceCommandOption = Option.withoutArgument(
'a', 
"addDataSource");
 
   54     private final Option caseDirOption = Option.requiredArgument(
'd', 
"caseDir");
 
   55     private final Option runIngestCommandOption = Option.optionalArgument(
'r', 
"runIngest");
 
   56     private final Option listAllDataSourcesCommandOption = Option.withoutArgument(
'l', 
"listAllDataSources");
 
   57     private final Option generateReportsOption = Option.optionalArgument(
'g', 
"generateReports");
 
   58     private final Option defaultArgument = Option.defaultArguments();
 
   60     private boolean runFromCommandLine = 
false;
 
   62     private final List<CommandLineCommand> commands = 
new ArrayList<>();
 
   64     final static String CASETYPE_MULTI = 
"multi";
 
   65     final static String CASETYPE_SINGLE = 
"single";
 
   67     private String defaultArgumentValue = null;
 
   71         Set<Option> set = 
new HashSet<>();
 
   72         set.add(createCaseCommandOption);
 
   73         set.add(caseNameOption);
 
   74         set.add(caseTypeOption);
 
   75         set.add(caseBaseDirOption);
 
   76         set.add(dataSourcePathOption);
 
   77         set.add(addDataSourceCommandOption);
 
   78         set.add(dataSourceObjectIdOption);
 
   79         set.add(caseDirOption);
 
   80         set.add(runIngestCommandOption);
 
   81         set.add(listAllDataSourcesCommandOption);
 
   82         set.add(generateReportsOption);
 
   83         set.add(defaultArgument);
 
   88     protected void process(Env env, Map<Option, String[]> values) throws CommandException {
 
   89         logger.log(Level.INFO, 
"Processing Autopsy command line options"); 
 
   90         System.out.println(
"Processing Autopsy command line options");
 
   92         if (values.containsKey(defaultArgument)) {
 
   93             defaultArgumentValue = values.get(defaultArgument)[0];
 
   94             runFromCommandLine = 
true;
 
   99         if (!(values.containsKey(createCaseCommandOption) || values.containsKey(addDataSourceCommandOption)
 
  100                 || values.containsKey(runIngestCommandOption) || values.containsKey(listAllDataSourcesCommandOption)
 
  101                 || values.containsKey(generateReportsOption))) {
 
  103             handleError(
"Invalid command line, an input option must be supplied.");
 
  108         String inputCaseName = 
"";
 
  109         if (values.containsKey(caseNameOption)) {
 
  110             argDirs = values.get(caseNameOption);
 
  111             if (argDirs.length < 1) {
 
  112                 handleError(
"Missing argument 'caseName'");
 
  114             inputCaseName = argDirs[0];
 
  116             if (inputCaseName == null || inputCaseName.isEmpty()) {
 
  117                 handleError(
"'caseName' argument is empty");
 
  121         String caseType = 
"";
 
  122         if (values.containsKey(caseTypeOption)) {
 
  123             argDirs = values.get(caseTypeOption);
 
  125             if (argDirs.length < 1) {
 
  126                 handleError(
"Missing argument 'caseType'");
 
  128             caseType = argDirs[0];
 
  130             if (caseType == null || caseType.isEmpty()) {
 
  131                 handleError(
"'caseType' argument is empty");
 
  132             } 
else if (!caseType.equalsIgnoreCase(CASETYPE_MULTI) && !caseType.equalsIgnoreCase(CASETYPE_SINGLE)) {
 
  133                 handleError(
"'caseType' argument is invalid");
 
  135                 handleError(
"Unable to create multi user case. Confirm that multi user settings are configured correctly.");
 
  139         String caseBaseDir = 
"";
 
  140         if (values.containsKey(caseBaseDirOption)) {
 
  141             argDirs = values.get(caseBaseDirOption);
 
  142             if (argDirs.length < 1) {
 
  143                 handleError(
"Missing argument 'caseBaseDir'");
 
  145             caseBaseDir = argDirs[0];
 
  147             if (caseBaseDir == null || caseBaseDir.isEmpty()) {
 
  148                 handleError(
"Missing argument 'caseBaseDir' option");
 
  151             if (!(
new File(caseBaseDir).exists()) || !(
new File(caseBaseDir).isDirectory())) {
 
  152                 handleError(
"'caseBaseDir' directory doesn't exist or is not a directory: " + caseBaseDir);
 
  156         String dataSourcePath = 
"";
 
  157         if (values.containsKey(dataSourcePathOption)) {
 
  159             argDirs = values.get(dataSourcePathOption);
 
  160             if (argDirs.length < 1) {
 
  161                 handleError(
"Missing argument 'dataSourcePath'");
 
  163             dataSourcePath = argDirs[0];
 
  166             if (dataSourcePath == null || dataSourcePath.isEmpty()) {
 
  167                 handleError(
"Missing argument 'dataSourcePath'");
 
  170             if (!(
new File(dataSourcePath).exists())) {
 
  171                 handleError(
"Input data source file " + dataSourcePath + 
" doesn't exist");
 
  175         String dataSourceId = 
"";
 
  176         if (values.containsKey(dataSourceObjectIdOption)) {
 
  178             argDirs = values.get(dataSourceObjectIdOption);
 
  179             if (argDirs.length < 1) {
 
  180                 handleError(
"Missing argument 'dataSourceObjectIdOption'");
 
  182             dataSourceId = argDirs[0];
 
  185             if (dataSourceId == null || dataSourceId.isEmpty()) {
 
  186                 handleError(
"Input data source id is empty");
 
  191         if (values.containsKey(caseDirOption)) {
 
  193             argDirs = values.get(caseDirOption);
 
  194             if (argDirs.length < 1) {
 
  195                 handleError(
"Argument missing from 'caseDir' option");
 
  197             caseDir = argDirs[0];
 
  200             if (caseDir == null || caseDir.isEmpty()) {
 
  201                 handleError(
"Argument missing from 'caseDir'");
 
  204             if (!(
new File(caseDir).exists()) || !(
new File(caseDir).isDirectory())) {
 
  205                 handleError(
"Case directory " + caseDir + 
" does not exist or is not a directory");
 
  211         if (values.containsKey(createCaseCommandOption)) {
 
  214             if (inputCaseName == null || inputCaseName.isEmpty()) {
 
  215                 handleError(
"'caseName' argument is empty");
 
  219             if (caseBaseDir == null || caseBaseDir.isEmpty()) {
 
  220                 handleError(
"'caseBaseDir' argument is empty");
 
  223             CommandLineCommand newCommand = 
new CommandLineCommand(CommandLineCommand.CommandType.CREATE_CASE);
 
  224             newCommand.addInputValue(CommandLineCommand.InputType.CASE_NAME.name(), inputCaseName);
 
  225             newCommand.addInputValue(CommandLineCommand.InputType.CASES_BASE_DIR_PATH.name(), caseBaseDir);
 
  226             newCommand.addInputValue(CommandLineCommand.InputType.CASE_TYPE.name(), caseType);
 
  227             commands.add(newCommand);
 
  228             runFromCommandLine = 
true;
 
  232         if (values.containsKey(addDataSourceCommandOption)) {
 
  235             if (!values.containsKey(createCaseCommandOption) && caseDir.isEmpty()) {
 
  237                 handleError(
"'caseDir' argument is empty");
 
  241             if (dataSourcePath == null || dataSourcePath.isEmpty()) {
 
  242                 handleError(
"'dataSourcePath' argument is empty");
 
  245             CommandLineCommand newCommand = 
new CommandLineCommand(CommandLineCommand.CommandType.ADD_DATA_SOURCE);
 
  246             newCommand.addInputValue(CommandLineCommand.InputType.CASE_FOLDER_PATH.name(), caseDir);
 
  247             newCommand.addInputValue(CommandLineCommand.InputType.DATA_SOURCE_PATH.name(), dataSourcePath);
 
  248             commands.add(newCommand);
 
  249             runFromCommandLine = 
true;
 
  252         String ingestProfile = 
"";
 
  254         if (values.containsKey(runIngestCommandOption)) {
 
  256             argDirs = values.get(runIngestCommandOption);
 
  257             if(argDirs != null && argDirs.length > 0) {
 
  258                 ingestProfile = argDirs[0];
 
  262             if (!values.containsKey(createCaseCommandOption) && caseDir.isEmpty()) {
 
  264                 handleError(
"'caseDir' argument is empty");
 
  268             if (!values.containsKey(addDataSourceCommandOption) && dataSourceId.isEmpty()) {
 
  270                 handleError(
"'dataSourceId' argument is empty");
 
  273             CommandLineCommand newCommand = 
new CommandLineCommand(CommandLineCommand.CommandType.RUN_INGEST);
 
  274             newCommand.addInputValue(CommandLineCommand.InputType.CASE_FOLDER_PATH.name(), caseDir);
 
  275             newCommand.addInputValue(CommandLineCommand.InputType.DATA_SOURCE_ID.name(), dataSourceId);
 
  276             newCommand.addInputValue(CommandLineCommand.InputType.INGEST_PROFILE_NAME.name(), ingestProfile);
 
  277             commands.add(newCommand);
 
  278             runFromCommandLine = 
true;
 
  282         if (values.containsKey(listAllDataSourcesCommandOption)) {
 
  285             if (!values.containsKey(createCaseCommandOption) && (caseDir == null || caseDir.isEmpty())) {
 
  287                 handleError(
"'caseDir' argument is empty");
 
  290             CommandLineCommand newCommand = 
new CommandLineCommand(CommandLineCommand.CommandType.LIST_ALL_DATA_SOURCES);
 
  291             newCommand.addInputValue(CommandLineCommand.InputType.CASE_FOLDER_PATH.name(), caseDir);
 
  292             commands.add(newCommand);
 
  293             runFromCommandLine = 
true;
 
  297         if (values.containsKey(generateReportsOption)) {
 
  300             if (!values.containsKey(createCaseCommandOption) && caseDir.isEmpty()) {
 
  302                 handleError(
"'caseDir' argument is empty");
 
  305             List<String> reportProfiles;
 
  306             argDirs = values.get(generateReportsOption);
 
  307             if (argDirs.length > 0) {
 
  309                 reportProfiles = Stream.of(argDirs[0].split(
","))
 
  311                 .collect(Collectors.toList());
 
  313                 if (reportProfiles == null || reportProfiles.isEmpty()) {
 
  314                     handleError(
"'generateReports' argument is empty");
 
  317                 for (String reportProfile : reportProfiles) {
 
  318                     if (reportProfile.isEmpty()) {
 
  319                         handleError(
"Empty report profile name");
 
  321                     CommandLineCommand newCommand = 
new CommandLineCommand(CommandLineCommand.CommandType.GENERATE_REPORTS);
 
  322                     newCommand.addInputValue(CommandLineCommand.InputType.CASE_FOLDER_PATH.name(), caseDir);
 
  323                     newCommand.addInputValue(CommandLineCommand.InputType.REPORT_PROFILE_NAME.name(), reportProfile);
 
  324                     commands.add(newCommand);
 
  328                 CommandLineCommand newCommand = 
new CommandLineCommand(CommandLineCommand.CommandType.GENERATE_REPORTS);
 
  329                 newCommand.addInputValue(CommandLineCommand.InputType.CASE_FOLDER_PATH.name(), caseDir);
 
  330                 commands.add(newCommand);
 
  333             runFromCommandLine = 
true;
 
  343         return runFromCommandLine;
 
  352         return defaultArgumentValue;
 
  360     List<CommandLineCommand> getCommands() {
 
  361         return Collections.unmodifiableList(commands);
 
  371     private void handleError(String errorMessage) 
throws CommandException {
 
  372         logger.log(Level.SEVERE, errorMessage);
 
  373         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)