Autopsy  4.13.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;
34 
38 @ServiceProvider(service = OptionProcessor.class)
39 public class CommandLineOptionProcessor extends OptionProcessor {
40 
41  private static final Logger logger = Logger.getLogger(CommandLineOptionProcessor.class.getName());
42  private final Option caseNameOption = Option.requiredArgument('n', "caseName");
43  private final Option caseBaseDirOption = Option.requiredArgument('o', "caseBaseDir");
44  private final Option createCaseCommandOption = Option.withoutArgument('c', "createCase");
45  private final Option dataSourcePathOption = Option.requiredArgument('s', "dataSourcePath");
46  private final Option dataSourceObjectIdOption = Option.requiredArgument('i', "dataSourceObjectId");
47  private final Option addDataSourceCommandOption = Option.withoutArgument('a', "addDataSource");
48  private final Option caseDirOption = Option.requiredArgument('d', "caseDir");
49  private final Option runIngestCommandOption = Option.withoutArgument('r', "runIngest");
50  private final Option ingestProfileOption = Option.requiredArgument('p', "ingestProfile");
51  private final Option listAllDataSourcesCommandOption = Option.withoutArgument('l', "listAllDataSources");
52  private final Option generateReportsOption = Option.withoutArgument('g', "generateReports");
53 
54  private boolean runFromCommandLine = false;
55 
56  private final List<CommandLineCommand> commands = new ArrayList<>();
57 
58  @Override
59  protected Set<Option> getOptions() {
60  Set<Option> set = new HashSet<>();
61  set.add(createCaseCommandOption);
62  set.add(caseNameOption);
63  set.add(caseBaseDirOption);
64  set.add(dataSourcePathOption);
65  set.add(addDataSourceCommandOption);
66  set.add(dataSourceObjectIdOption);
67  set.add(caseDirOption);
68  set.add(runIngestCommandOption);
69  set.add(ingestProfileOption);
70  set.add(listAllDataSourcesCommandOption);
71  set.add(generateReportsOption);
72  return set;
73  }
74 
75  @Override
76  protected void process(Env env, Map<Option, String[]> values) throws CommandException {
77  logger.log(Level.INFO, "Processing Autopsy command line options"); //NON-NLS
78  System.out.println("Processing Autopsy command line options");
79  runFromCommandLine = false;
80 
81  // input arguments must contain at least one command
82  if (!(values.containsKey(createCaseCommandOption) || values.containsKey(addDataSourceCommandOption)
83  || values.containsKey(runIngestCommandOption) || values.containsKey(listAllDataSourcesCommandOption)
84  || values.containsKey(generateReportsOption))) {
85  // not running from command line
86  logger.log(Level.INFO, "No command line commands passed in as inputs. Not running from command line."); //NON-NLS
87  System.err.println("No command line commands passed in as inputs. Not running from command line.");
88  return;
89  }
90 
91  // parse input parameters
92  String[] argDirs;
93  String inputCaseName = "";
94  if (values.containsKey(caseNameOption)) {
95  argDirs = values.get(caseNameOption);
96  if (argDirs.length < 1) {
97  logger.log(Level.SEVERE, "Missing argument 'caseName'");
98  System.err.println("Missing argument 'caseName'");
99  return;
100  }
101  inputCaseName = argDirs[0];
102 
103  if (inputCaseName == null || inputCaseName.isEmpty()) {
104  logger.log(Level.SEVERE, "'caseName' argument is empty");
105  System.err.println("'caseName' argument is empty");
106  return;
107  }
108  }
109 
110  String caseBaseDir = "";
111  if (values.containsKey(caseBaseDirOption)) {
112  argDirs = values.get(caseBaseDirOption);
113  if (argDirs.length < 1) {
114  logger.log(Level.SEVERE, "Missing argument 'caseBaseDir'");
115  System.err.println("Missing argument 'caseBaseDir'");
116  return;
117  }
118  caseBaseDir = argDirs[0];
119 
120  if (caseBaseDir == null || caseBaseDir.isEmpty()) {
121  logger.log(Level.SEVERE, "Missing argument 'caseBaseDir'");
122  System.err.println("Missing argument 'caseBaseDir'");
123  return;
124  }
125 
126  if (!(new File(caseBaseDir).exists()) || !(new File(caseBaseDir).isDirectory())) {
127  logger.log(Level.SEVERE, "''caseBaseDir'' {0} directory doesn''t exist or is not a directory", caseBaseDir);
128  System.err.println("'caseBaseDir' directory doesn't exist or is not a directory: " + caseBaseDir);
129  return;
130  }
131  }
132 
133  String dataSourcePath = "";
134  if (values.containsKey(dataSourcePathOption)) {
135 
136  argDirs = values.get(dataSourcePathOption);
137  if (argDirs.length < 1) {
138  logger.log(Level.SEVERE, "Missing argument 'dataSourcePath'");
139  System.err.println("Missing argument 'dataSourcePath'");
140  return;
141  }
142  dataSourcePath = argDirs[0];
143 
144  // verify inputs
145  if (dataSourcePath == null || dataSourcePath.isEmpty()) {
146  logger.log(Level.SEVERE, "Missing argument 'dataSourcePath'");
147  System.err.println("Missing argument 'dataSourcePath'");
148  return;
149  }
150 
151  if (!(new File(dataSourcePath).exists())) {
152  logger.log(Level.SEVERE, "Input data source file {0} doesn''t exist", dataSourcePath);
153  System.err.println("Input data source file " + dataSourcePath + " doesn't exist");
154  return;
155  }
156  }
157 
158  String dataSourceId = "";
159  if (values.containsKey(dataSourceObjectIdOption)) {
160 
161  argDirs = values.get(dataSourceObjectIdOption);
162  if (argDirs.length < 1) {
163  logger.log(Level.SEVERE, "Missing argument 'dataSourceObjectIdOption'");
164  System.err.println("Missing argument 'dataSourceObjectIdOption'");
165  return;
166  }
167  dataSourceId = argDirs[0];
168 
169  // verify inputs
170  if (dataSourceId == null || dataSourceId.isEmpty()) {
171  logger.log(Level.SEVERE, "Input data source id is empty");
172  System.err.println("Input data source id is empty");
173  return;
174  }
175  }
176 
177  String caseDir = "";
178  if (values.containsKey(caseDirOption)) {
179 
180  argDirs = values.get(caseDirOption);
181  if (argDirs.length < 1) {
182  logger.log(Level.SEVERE, "Missing argument 'caseDirOption'");
183  System.err.println("Missing argument 'caseDirOption'");
184  return;
185  }
186  caseDir = argDirs[0];
187 
188  // verify inputs
189  if (caseDir == null || caseDir.isEmpty()) {
190  logger.log(Level.SEVERE, "Missing argument 'caseDirOption'");
191  System.err.println("Missing argument 'caseDirOption'");
192  return;
193  }
194 
195  if (!(new File(caseDir).exists()) || !(new File(caseDir).isDirectory())) {
196  logger.log(Level.SEVERE, "Case directory {0} doesn''t exist or is not a directory", caseDir);
197  System.err.println("Case directory " + caseDir + " doesn't exist or is not a directory");
198  return;
199  }
200  }
201 
202  String ingestProfile = "";
203  if (values.containsKey(ingestProfileOption)) {
204 
205  argDirs = values.get(ingestProfileOption);
206  if (argDirs.length < 1) {
207  logger.log(Level.SEVERE, "Missing argument 'ingestProfile'");
208  System.err.println("Missing argument 'ingestProfile'");
209  return;
210  }
211  ingestProfile = argDirs[0];
212 
213  // verify inputs
214  if (ingestProfile == null || ingestProfile.isEmpty()) {
215  logger.log(Level.SEVERE, "Missing argument 'ingestProfile'");
216  System.err.println("Missing argument 'ingestProfile'");
217  return;
218  }
219  }
220 
221  // Create commands in order in which they should be executed:
222  // First create the "CREATE_CASE" command, if present
223  if (values.containsKey(createCaseCommandOption)) {
224 
225  // 'caseName' must always be specified for "CREATE_CASE" command
226  if (inputCaseName.isEmpty()) {
227  logger.log(Level.SEVERE, "'caseName' argument is empty");
228  System.out.println("'caseName' argument is empty");
229  runFromCommandLine = false;
230  return;
231  }
232 
233  // 'caseBaseDir' must always be specified for "CREATE_CASE" command
234  if (caseBaseDir.isEmpty()) {
235  logger.log(Level.SEVERE, "'caseBaseDir' argument is empty");
236  System.err.println("'caseBaseDir' argument is empty");
237  runFromCommandLine = false;
238  return;
239  }
240 
241  CommandLineCommand newCommand = new CommandLineCommand(CommandLineCommand.CommandType.CREATE_CASE);
242  newCommand.addInputValue(CommandLineCommand.InputType.CASE_NAME.name(), inputCaseName);
243  newCommand.addInputValue(CommandLineCommand.InputType.CASES_BASE_DIR_PATH.name(), caseBaseDir);
244  commands.add(newCommand);
245  runFromCommandLine = true;
246  }
247 
248  // Add ADD_DATA_SOURCE command, if present
249  if (values.containsKey(addDataSourceCommandOption)) {
250 
251  // 'caseDir' must only be specified if the case is not being created during the current run
252  if (!values.containsKey(createCaseCommandOption) && caseDir.isEmpty()) {
253  // new case is not being created during this run, so 'caseDir' should have been specified
254  logger.log(Level.SEVERE, "'caseDir' argument is empty");
255  System.err.println("'caseDir' argument is empty");
256  runFromCommandLine = false;
257  return;
258  }
259 
260  // 'dataSourcePath' must always be specified for "ADD_DATA_SOURCE" command
261  if (dataSourcePath.isEmpty()) {
262  logger.log(Level.SEVERE, "'dataSourcePath' argument is empty");
263  System.err.println("'dataSourcePath' argument is empty");
264  runFromCommandLine = false;
265  return;
266  }
267 
268  CommandLineCommand newCommand = new CommandLineCommand(CommandLineCommand.CommandType.ADD_DATA_SOURCE);
269  newCommand.addInputValue(CommandLineCommand.InputType.CASE_FOLDER_PATH.name(), caseDir);
270  newCommand.addInputValue(CommandLineCommand.InputType.DATA_SOURCE_PATH.name(), dataSourcePath);
271  commands.add(newCommand);
272  runFromCommandLine = true;
273  }
274 
275  // Add RUN_INGEST command, if present
276  if (values.containsKey(runIngestCommandOption)) {
277 
278  // 'caseDir' must only be specified if the case is not being created during the current run
279  if (!values.containsKey(createCaseCommandOption) && caseDir.isEmpty()) {
280  // new case is not being created during this run, so 'caseDir' should have been specified
281  logger.log(Level.SEVERE, "'caseDir' argument is empty");
282  System.err.println("'caseDir' argument is empty");
283  runFromCommandLine = false;
284  return;
285  }
286 
287  // if new data source is being added during this run, then 'dataSourceId' is not specified
288  if (!values.containsKey(addDataSourceCommandOption) && dataSourceId.isEmpty()) {
289  // data source is not being added during this run, so 'dataSourceId' should have been specified
290  logger.log(Level.SEVERE, "'dataSourceId' argument is empty");
291  System.err.println("'dataSourceId' argument is empty");
292  runFromCommandLine = false;
293  return;
294  }
295 
296  CommandLineCommand newCommand = new CommandLineCommand(CommandLineCommand.CommandType.RUN_INGEST);
297  newCommand.addInputValue(CommandLineCommand.InputType.CASE_FOLDER_PATH.name(), caseDir);
298  newCommand.addInputValue(CommandLineCommand.InputType.DATA_SOURCE_ID.name(), dataSourceId);
299  newCommand.addInputValue(CommandLineCommand.InputType.INGEST_PROFILE_NAME.name(), ingestProfile);
300  commands.add(newCommand);
301  runFromCommandLine = true;
302  }
303 
304  // Add "LIST_ALL_DATA_SOURCES" command, if present
305  if (values.containsKey(listAllDataSourcesCommandOption)) {
306 
307  // 'caseDir' must only be specified if the case is not being created during the current run
308  if (!values.containsKey(createCaseCommandOption) && caseDir.isEmpty()) {
309  // new case is not being created during this run, so 'caseDir' should have been specified
310  logger.log(Level.SEVERE, "'caseDir' argument is empty");
311  System.err.println("'caseDir' argument is empty");
312  runFromCommandLine = false;
313  return;
314  }
315 
316  CommandLineCommand newCommand = new CommandLineCommand(CommandLineCommand.CommandType.LIST_ALL_DATA_SOURCES);
317  newCommand.addInputValue(CommandLineCommand.InputType.CASE_FOLDER_PATH.name(), caseDir);
318  commands.add(newCommand);
319  runFromCommandLine = true;
320  }
321 
322  // Add "GENERATE_REPORTS" command, if present
323  if (values.containsKey(generateReportsOption)) {
324 
325  // 'caseDir' must only be specified if the case is not being created during the current run
326  if (!values.containsKey(createCaseCommandOption) && caseDir.isEmpty()) {
327  // new case is not being created during this run, so 'caseDir' should have been specified
328  logger.log(Level.SEVERE, "'caseDir' argument is empty");
329  System.err.println("'caseDir' argument is empty");
330  runFromCommandLine = false;
331  return;
332  }
333 
334  CommandLineCommand newCommand = new CommandLineCommand(CommandLineCommand.CommandType.GENERATE_REPORTS);
335  newCommand.addInputValue(CommandLineCommand.InputType.CASE_FOLDER_PATH.name(), caseDir);
336  commands.add(newCommand);
337  runFromCommandLine = true;
338  }
339  }
340 
346  public boolean isRunFromCommandLine() {
347  return runFromCommandLine;
348  }
349 
355  List<CommandLineCommand> getCommands() {
356  return commands;
357  }
358 }
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2019 Basis Technology. Generated on: Tue Jan 7 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.