Autopsy  4.14.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
CommandLineOptionProcessor.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2019-2019 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.commandlineingest;
20 
21 import java.io.File;
22 import java.util.ArrayList;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
27 import java.util.logging.Level;
29 import org.netbeans.api.sendopts.CommandException;
30 import org.netbeans.spi.sendopts.Env;
31 import org.netbeans.spi.sendopts.Option;
32 import org.netbeans.spi.sendopts.OptionProcessor;
33 import org.openide.util.lookup.ServiceProvider;
35 
39 @ServiceProvider(service = OptionProcessor.class)
40 public class CommandLineOptionProcessor extends OptionProcessor {
41 
42  private static final Logger logger = Logger.getLogger(CommandLineOptionProcessor.class.getName());
43  private final Option caseNameOption = Option.requiredArgument('n', "caseName");
44  private final Option caseTypeOption = Option.requiredArgument('t', "caseType");
45  private final Option caseBaseDirOption = Option.requiredArgument('o', "caseBaseDir");
46  private final Option createCaseCommandOption = Option.withoutArgument('c', "createCase");
47  private final Option dataSourcePathOption = Option.requiredArgument('s', "dataSourcePath");
48  private final Option dataSourceObjectIdOption = Option.requiredArgument('i', "dataSourceObjectId");
49  private final Option addDataSourceCommandOption = Option.withoutArgument('a', "addDataSource");
50  private final Option caseDirOption = Option.requiredArgument('d', "caseDir");
51  private final Option runIngestCommandOption = Option.withoutArgument('r', "runIngest");
52  private final Option ingestProfileOption = Option.requiredArgument('p', "ingestProfile");
53  private final Option listAllDataSourcesCommandOption = Option.withoutArgument('l', "listAllDataSources");
54  private final Option generateReportsOption = Option.withoutArgument('g', "generateReports");
55 
56  private boolean runFromCommandLine = false;
57 
58  private final List<CommandLineCommand> commands = new ArrayList<>();
59 
60  final static String CASETYPE_MULTI = "multi";
61  final static String CASETYPE_SINGLE = "single";
62 
63  @Override
64  protected Set<Option> getOptions() {
65  Set<Option> set = new HashSet<>();
66  set.add(createCaseCommandOption);
67  set.add(caseNameOption);
68  set.add(caseTypeOption);
69  set.add(caseBaseDirOption);
70  set.add(dataSourcePathOption);
71  set.add(addDataSourceCommandOption);
72  set.add(dataSourceObjectIdOption);
73  set.add(caseDirOption);
74  set.add(runIngestCommandOption);
75  set.add(ingestProfileOption);
76  set.add(listAllDataSourcesCommandOption);
77  set.add(generateReportsOption);
78  return set;
79  }
80 
81  @Override
82  protected void process(Env env, Map<Option, String[]> values) throws CommandException {
83  logger.log(Level.INFO, "Processing Autopsy command line options"); //NON-NLS
84  System.out.println("Processing Autopsy command line options");
85  runFromCommandLine = false;
86 
87  // input arguments must contain at least one command
88  if (!(values.containsKey(createCaseCommandOption) || values.containsKey(addDataSourceCommandOption)
89  || values.containsKey(runIngestCommandOption) || values.containsKey(listAllDataSourcesCommandOption)
90  || values.containsKey(generateReportsOption))) {
91  // not running from command line
92  logger.log(Level.INFO, "No command line commands passed in as inputs. Not running from command line."); //NON-NLS
93  System.err.println("No command line commands passed in as inputs. Not running from command line.");
94  return;
95  }
96 
97  // parse input parameters
98  String[] argDirs;
99  String inputCaseName = "";
100  if (values.containsKey(caseNameOption)) {
101  argDirs = values.get(caseNameOption);
102  if (argDirs.length < 1) {
103  logger.log(Level.SEVERE, "Missing argument 'caseName'");
104  System.err.println("Missing argument 'caseName'");
105  return;
106  }
107  inputCaseName = argDirs[0];
108 
109  if (inputCaseName == null || inputCaseName.isEmpty()) {
110  logger.log(Level.SEVERE, "'caseName' argument is empty");
111  System.err.println("'caseName' argument is empty");
112  return;
113  }
114  }
115 
116 
117  String caseType = "";
118  if (values.containsKey(caseTypeOption)) {
119  argDirs = values.get(caseTypeOption);
120 
121  if (argDirs.length < 1) {
122  logger.log(Level.SEVERE, "Missing argument 'caseType'");
123  System.err.println("Missing argument 'caseType'");
124  return;
125  }
126  caseType = argDirs[0];
127 
128  if (caseType == null || caseType.isEmpty()) {
129  logger.log(Level.SEVERE, "'caseType' argument is empty");
130  System.err.println("'caseType' argument is empty");
131  return;
132  }
133 
134  if (!caseType.equalsIgnoreCase(CASETYPE_MULTI) && !caseType.equalsIgnoreCase(CASETYPE_SINGLE)) {
135  logger.log(Level.SEVERE, "'caseType' argument is invalid");
136  System.err.println("'caseType' argument is invalid");
137  return;
138  }
139 
140  if (caseType.equalsIgnoreCase(CASETYPE_MULTI) && !FeatureAccessUtils.canCreateMultiUserCases()) {
141  logger.log(Level.SEVERE, "Unable to create multi user case.");
142  System.err.println("Unable to create multi user case. Confirm that multi user settings are configured correctly.");
143  return;
144  }
145  }
146 
147  String caseBaseDir = "";
148  if (values.containsKey(caseBaseDirOption)) {
149  argDirs = values.get(caseBaseDirOption);
150  if (argDirs.length < 1) {
151  logger.log(Level.SEVERE, "Missing argument 'caseBaseDir'");
152  System.err.println("Missing argument 'caseBaseDir'");
153  return;
154  }
155  caseBaseDir = argDirs[0];
156 
157  if (caseBaseDir == null || caseBaseDir.isEmpty()) {
158  logger.log(Level.SEVERE, "Missing argument 'caseBaseDir'");
159  System.err.println("Missing argument 'caseBaseDir'");
160  return;
161  }
162 
163  if (!(new File(caseBaseDir).exists()) || !(new File(caseBaseDir).isDirectory())) {
164  logger.log(Level.SEVERE, "''caseBaseDir'' {0} directory doesn''t exist or is not a directory", caseBaseDir);
165  System.err.println("'caseBaseDir' directory doesn't exist or is not a directory: " + caseBaseDir);
166  return;
167  }
168  }
169 
170  String dataSourcePath = "";
171  if (values.containsKey(dataSourcePathOption)) {
172 
173  argDirs = values.get(dataSourcePathOption);
174  if (argDirs.length < 1) {
175  logger.log(Level.SEVERE, "Missing argument 'dataSourcePath'");
176  System.err.println("Missing argument 'dataSourcePath'");
177  return;
178  }
179  dataSourcePath = argDirs[0];
180 
181  // verify inputs
182  if (dataSourcePath == null || dataSourcePath.isEmpty()) {
183  logger.log(Level.SEVERE, "Missing argument 'dataSourcePath'");
184  System.err.println("Missing argument 'dataSourcePath'");
185  return;
186  }
187 
188  if (!(new File(dataSourcePath).exists())) {
189  logger.log(Level.SEVERE, "Input data source file {0} doesn''t exist", dataSourcePath);
190  System.err.println("Input data source file " + dataSourcePath + " doesn't exist");
191  return;
192  }
193  }
194 
195  String dataSourceId = "";
196  if (values.containsKey(dataSourceObjectIdOption)) {
197 
198  argDirs = values.get(dataSourceObjectIdOption);
199  if (argDirs.length < 1) {
200  logger.log(Level.SEVERE, "Missing argument 'dataSourceObjectIdOption'");
201  System.err.println("Missing argument 'dataSourceObjectIdOption'");
202  return;
203  }
204  dataSourceId = argDirs[0];
205 
206  // verify inputs
207  if (dataSourceId == null || dataSourceId.isEmpty()) {
208  logger.log(Level.SEVERE, "Input data source id is empty");
209  System.err.println("Input data source id is empty");
210  return;
211  }
212  }
213 
214  String caseDir = "";
215  if (values.containsKey(caseDirOption)) {
216 
217  argDirs = values.get(caseDirOption);
218  if (argDirs.length < 1) {
219  logger.log(Level.SEVERE, "Missing argument 'caseDirOption'");
220  System.err.println("Missing argument 'caseDirOption'");
221  return;
222  }
223  caseDir = argDirs[0];
224 
225  // verify inputs
226  if (caseDir == null || caseDir.isEmpty()) {
227  logger.log(Level.SEVERE, "Missing argument 'caseDirOption'");
228  System.err.println("Missing argument 'caseDirOption'");
229  return;
230  }
231 
232  if (!(new File(caseDir).exists()) || !(new File(caseDir).isDirectory())) {
233  logger.log(Level.SEVERE, "Case directory {0} doesn''t exist or is not a directory", caseDir);
234  System.err.println("Case directory " + caseDir + " doesn't exist or is not a directory");
235  return;
236  }
237  }
238 
239  String ingestProfile = "";
240  if (values.containsKey(ingestProfileOption)) {
241 
242  argDirs = values.get(ingestProfileOption);
243  if (argDirs.length < 1) {
244  logger.log(Level.SEVERE, "Missing argument 'ingestProfile'");
245  System.err.println("Missing argument 'ingestProfile'");
246  return;
247  }
248  ingestProfile = argDirs[0];
249 
250  // verify inputs
251  if (ingestProfile == null || ingestProfile.isEmpty()) {
252  logger.log(Level.SEVERE, "Missing argument 'ingestProfile'");
253  System.err.println("Missing argument 'ingestProfile'");
254  return;
255  }
256  }
257 
258  // Create commands in order in which they should be executed:
259  // First create the "CREATE_CASE" command, if present
260  if (values.containsKey(createCaseCommandOption)) {
261 
262  // 'caseName' must always be specified for "CREATE_CASE" command
263  if (inputCaseName.isEmpty()) {
264  logger.log(Level.SEVERE, "'caseName' argument is empty");
265  System.out.println("'caseName' argument is empty");
266  runFromCommandLine = false;
267  return;
268  }
269 
270  // 'caseBaseDir' must always be specified for "CREATE_CASE" command
271  if (caseBaseDir.isEmpty()) {
272  logger.log(Level.SEVERE, "'caseBaseDir' argument is empty");
273  System.err.println("'caseBaseDir' argument is empty");
274  runFromCommandLine = false;
275  return;
276  }
277 
278  CommandLineCommand newCommand = new CommandLineCommand(CommandLineCommand.CommandType.CREATE_CASE);
279  newCommand.addInputValue(CommandLineCommand.InputType.CASE_NAME.name(), inputCaseName);
280  newCommand.addInputValue(CommandLineCommand.InputType.CASES_BASE_DIR_PATH.name(), caseBaseDir);
281  newCommand.addInputValue(CommandLineCommand.InputType.CASE_TYPE.name(), caseType);
282  commands.add(newCommand);
283  runFromCommandLine = true;
284  }
285 
286  // Add ADD_DATA_SOURCE command, if present
287  if (values.containsKey(addDataSourceCommandOption)) {
288 
289  // 'caseDir' must only be specified if the case is not being created during the current run
290  if (!values.containsKey(createCaseCommandOption) && caseDir.isEmpty()) {
291  // new case is not being created during this run, so 'caseDir' should have been specified
292  logger.log(Level.SEVERE, "'caseDir' argument is empty");
293  System.err.println("'caseDir' argument is empty");
294  runFromCommandLine = false;
295  return;
296  }
297 
298  // 'dataSourcePath' must always be specified for "ADD_DATA_SOURCE" command
299  if (dataSourcePath.isEmpty()) {
300  logger.log(Level.SEVERE, "'dataSourcePath' argument is empty");
301  System.err.println("'dataSourcePath' argument is empty");
302  runFromCommandLine = false;
303  return;
304  }
305 
306  CommandLineCommand newCommand = new CommandLineCommand(CommandLineCommand.CommandType.ADD_DATA_SOURCE);
307  newCommand.addInputValue(CommandLineCommand.InputType.CASE_FOLDER_PATH.name(), caseDir);
308  newCommand.addInputValue(CommandLineCommand.InputType.DATA_SOURCE_PATH.name(), dataSourcePath);
309  commands.add(newCommand);
310  runFromCommandLine = true;
311  }
312 
313  // Add RUN_INGEST command, if present
314  if (values.containsKey(runIngestCommandOption)) {
315 
316  // 'caseDir' must only be specified if the case is not being created during the current run
317  if (!values.containsKey(createCaseCommandOption) && caseDir.isEmpty()) {
318  // new case is not being created during this run, so 'caseDir' should have been specified
319  logger.log(Level.SEVERE, "'caseDir' argument is empty");
320  System.err.println("'caseDir' argument is empty");
321  runFromCommandLine = false;
322  return;
323  }
324 
325  // if new data source is being added during this run, then 'dataSourceId' is not specified
326  if (!values.containsKey(addDataSourceCommandOption) && dataSourceId.isEmpty()) {
327  // data source is not being added during this run, so 'dataSourceId' should have been specified
328  logger.log(Level.SEVERE, "'dataSourceId' argument is empty");
329  System.err.println("'dataSourceId' argument is empty");
330  runFromCommandLine = false;
331  return;
332  }
333 
334  CommandLineCommand newCommand = new CommandLineCommand(CommandLineCommand.CommandType.RUN_INGEST);
335  newCommand.addInputValue(CommandLineCommand.InputType.CASE_FOLDER_PATH.name(), caseDir);
336  newCommand.addInputValue(CommandLineCommand.InputType.DATA_SOURCE_ID.name(), dataSourceId);
337  newCommand.addInputValue(CommandLineCommand.InputType.INGEST_PROFILE_NAME.name(), ingestProfile);
338  commands.add(newCommand);
339  runFromCommandLine = true;
340  }
341 
342  // Add "LIST_ALL_DATA_SOURCES" command, if present
343  if (values.containsKey(listAllDataSourcesCommandOption)) {
344 
345  // 'caseDir' must only be specified if the case is not being created during the current run
346  if (!values.containsKey(createCaseCommandOption) && caseDir.isEmpty()) {
347  // new case is not being created during this run, so 'caseDir' should have been specified
348  logger.log(Level.SEVERE, "'caseDir' argument is empty");
349  System.err.println("'caseDir' argument is empty");
350  runFromCommandLine = false;
351  return;
352  }
353 
354  CommandLineCommand newCommand = new CommandLineCommand(CommandLineCommand.CommandType.LIST_ALL_DATA_SOURCES);
355  newCommand.addInputValue(CommandLineCommand.InputType.CASE_FOLDER_PATH.name(), caseDir);
356  commands.add(newCommand);
357  runFromCommandLine = true;
358  }
359 
360  // Add "GENERATE_REPORTS" command, if present
361  if (values.containsKey(generateReportsOption)) {
362 
363  // 'caseDir' must only be specified if the case is not being created during the current run
364  if (!values.containsKey(createCaseCommandOption) && caseDir.isEmpty()) {
365  // new case is not being created during this run, so 'caseDir' should have been specified
366  logger.log(Level.SEVERE, "'caseDir' argument is empty");
367  System.err.println("'caseDir' argument is empty");
368  runFromCommandLine = false;
369  return;
370  }
371 
372  CommandLineCommand newCommand = new CommandLineCommand(CommandLineCommand.CommandType.GENERATE_REPORTS);
373  newCommand.addInputValue(CommandLineCommand.InputType.CASE_FOLDER_PATH.name(), caseDir);
374  commands.add(newCommand);
375  runFromCommandLine = true;
376  }
377  }
378 
384  public boolean isRunFromCommandLine() {
385  return runFromCommandLine;
386  }
387 
393  List<CommandLineCommand> getCommands() {
394  return commands;
395  }
396 }
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2020 Basis Technology. Generated on: Wed Apr 8 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.