Autopsy  4.21.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
Installer.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2017-2020 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.centralrepository.eventlisteners;
20 
21 import java.io.File;
22 import java.io.IOException;
23 import java.lang.reflect.InvocationTargetException;
24 import java.nio.file.Paths;
25 import java.util.Map;
26 import java.util.logging.Level;
27 import javax.swing.JOptionPane;
28 import javax.swing.SwingUtilities;
29 import org.apache.commons.io.FileUtils;
30 import org.openide.modules.ModuleInstall;
31 import org.openide.util.NbBundle;
41 
47 public class Installer extends ModuleInstall {
48 
49  private static final String LEGACY_DEFAULT_FOLDER = "central_repository";
50  private static final String LEGACY_DEFAULT_DB_PARENT_PATH = Paths.get(PlatformUtil.getUserDirectory().getAbsolutePath(), LEGACY_DEFAULT_FOLDER).toAbsolutePath().toString();
51  private static final String LEGACY_MODULE_SETTINGS_KEY = "CentralRepository";
52 
53  private static final Logger logger = Logger.getLogger(Installer.class.getName());
54  private static final long serialVersionUID = 1L;
55  private static Installer instance;
56 
65  public synchronized static Installer getDefault() {
66  if (instance == null) {
67  instance = new Installer();
68  }
69  return instance;
70  }
71 
77  private Installer() {
78  super();
79  }
80 
81  /*
82  * Sets up a default, single-user SQLite central
83  * repository if no central repository is configured.
84  *
85  * Called by the registered Installer for the Autopsy-Core module located in
86  * the org.sleuthkit.autopsy.core package when the already installed
87  * Autopsy-Core module is restored (during application startup).
88  */
89  @Override
90  public void restored() {
91  // must happen first to move any legacy settings that exist.
94  }
95 
96 
97 
106  private String getSettingsFilePath(String moduleName) {
107  return Paths.get(PlatformUtil.getUserConfigDirectory(), moduleName + ".properties").toString();
108  }
109 
113  private void upgradeSettingsPath() {
114  File newSettingsFile = new File(getSettingsFilePath(CentralRepoSettings.getInstance().getModuleSettingsKey()));
115  File legacySettingsFile = new File(getSettingsFilePath(LEGACY_MODULE_SETTINGS_KEY));
116  // new config has not been created, but legacy has, copy it.
117  if (!newSettingsFile.exists() && legacySettingsFile.exists()) {
118  Map<String, String> prevSettings = ModuleSettings.getConfigSettings(LEGACY_MODULE_SETTINGS_KEY);
119  String prevPath = prevSettings.get(CentralRepoSettings.getInstance().getDatabasePathKey());
120  File prevDirCheck = new File(prevPath);
121  // if a relative directory, make sure it is relative to user config.
122  if (!prevDirCheck.isAbsolute()) {
123  prevPath = Paths.get(PlatformUtil.getUserDirectory().getAbsolutePath(), prevPath).toAbsolutePath().toString();
124  }
125 
126  // if old path is default path for sqlite db, copy it over to new location and update setting.
127  if (prevPath != null
128  && Paths.get(LEGACY_DEFAULT_DB_PARENT_PATH).toAbsolutePath().toString().equals(Paths.get(prevPath).toAbsolutePath().toString())) {
129  String prevDbName = prevSettings.get(CentralRepoSettings.getInstance().getDatabaseNameKey());
130  File prevDir = new File(prevPath);
131  // copy all files starting with prevDbName in prevPath to new path location.
132  if (prevDir.exists() && prevDir.isDirectory()) {
133  new File(CentralRepoSettings.getInstance().getDefaultDbPath()).mkdirs();
134  try {
135  for (File childFile : prevDir.listFiles((dir, name) -> name.startsWith(prevDbName))) {
136  FileUtils.copyFile(childFile, new File(CentralRepoSettings.getInstance().getDefaultDbPath(), childFile.getName()));
137  }
138  } catch (IOException ex) {
139  logger.log(Level.SEVERE, "There was an error upgrading settings.", ex);
140  }
141  }
142 
143  // get the new relative path to store
144  String newRelPath = PlatformUtil.getUserDirectory().toPath().relativize(Paths.get(CentralRepoSettings.getInstance().getDefaultDbPath())).toString();
145  // update path settings accordingly
146  prevSettings.put(CentralRepoSettings.getInstance().getDatabasePathKey(), newRelPath);
147  }
148 
149  // copy settings
151  }
152  }
153 
161  Map<String, String> centralRepoSettings = ModuleSettings.getConfigSettings(CentralRepoSettings.getInstance().getModuleSettingsKey());
162  String initializedStr = centralRepoSettings.get("initialized");
163 
164  // check to see if the repo has been initialized asking to setup cr
165  boolean initialized = Boolean.parseBoolean(initializedStr);
166 
167  // if it hasn't received that flag, check for a previous install where cr is already setup
168  if (!initialized) {
169  boolean prevRepo = Boolean.parseBoolean(centralRepoSettings.get("db.useCentralRepo"));
170  // if it has been previously set up and is in use, mark as previously initialized and save the settings
171  if (prevRepo) {
172  initialized = true;
174  }
175  }
176 
177  if(initialized) {
178  return; // Nothing to do
179  }
180 
181  if (CentralRepositoryNotificationDialog.shouldDisplay()) {
183  }
184 
185  try {
188  // Set up using existing multi-user settings.
190  } else {
191  manager.setupDefaultSqliteDb();
192  }
193  } catch (CentralRepoException ex) {
194  logger.log(Level.SEVERE, "There was an error while initializing the central repository database", ex);
195 
197  }
198 
200  }
201 
208  @NbBundle.Messages({"Installer.centralRepoUpgradeFailed.title=Central repository disabled"})
211  try {
212  SwingUtilities.invokeAndWait(() -> {
213  JOptionPane.showMessageDialog(null,
214  ex.getUserMessage(),
215  NbBundle.getMessage(this.getClass(), "Installer.centralRepoUpgradeFailed.title"),
216  JOptionPane.ERROR_MESSAGE);
217  });
218  } catch (InterruptedException | InvocationTargetException e) {
219  logger.log(Level.WARNING, e.getMessage(), e);
220  }
221  }
222  }
223 
224  @Override
225  public void uninstalled() {
226  /*
227  * TODO (Jira-6108): This code is erronoeous. As documented at
228  * http://bits.netbeans.org/dev/javadoc/org-openide-modules/org/openide/modules/ModuleInstall.html#uninstalled--
229  *
230  * "Called when the module is disabled while the application is still
231  * running. Should remove whatever functionality that it had registered
232  * in ModuleInstall.restored().
233  *
234  * Beware: in practice there is no way to
235  * ensure that this method will really be called. The module might
236  * simply be deleted or disabled while the application is not running.
237  * In fact this is always the case in NetBeans 6.0; the Plugin Manager
238  * only uninstalls or disables modules between restarts. This method
239  * will still be called if you reload a module during development."
240  *
241  * THIS CODE IS NEVER EXECUTED.
242  */
243  }
244 }
static synchronized void setConfigSetting(String moduleName, String settingName, String settingVal)
static synchronized void setConfigSettings(String moduleName, Map< String, String > settings)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124
static synchronized Map< String, String > getConfigSettings(String moduleName)

Copyright © 2012-2022 Basis Technology. Generated on: Tue Feb 6 2024
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.