19 package org.sleuthkit.autopsy.coreutils;
22 import java.io.IOException;
23 import java.net.InetAddress;
24 import java.nio.file.Files;
25 import java.nio.file.Path;
26 import java.nio.file.Paths;
27 import java.util.HashMap;
29 import java.util.NoSuchElementException;
30 import java.util.Scanner;
31 import java.util.UUID;
32 import java.util.concurrent.TimeUnit;
33 import java.util.logging.Level;
37 private static Map<String, String>
drives;
41 private static final String
OK_TXT =
"OK";
42 private static final String
COLON =
":";
44 private static final String
C_DRIVE =
"C:";
56 this.nameString = UUID.randomUUID().toString();
79 if (inputPath != null) {
81 if (inputPath.toUpperCase().startsWith(C_DRIVE)) {
84 if (
false ==
isUNC(inputPath)) {
85 String uncPath = null;
87 String currentDrive = Paths.get(inputPath).getRoot().toString().substring(STARTING_OFFSET, REPLACEMENT_SIZE);
88 String uncMapping = drives.get(currentDrive.toUpperCase());
89 if (uncMapping != null) {
90 uncPath = uncMapping + inputPath.substring(REPLACEMENT_SIZE, inputPath.length());
92 }
catch (Exception ex) {
122 if (inputPath != null) {
124 if (uncPath == null) {
127 return Paths.get(uncPath);
142 if (inputPath != null) {
157 if (inputPath != null) {
158 String shortenedPath = inputPath.substring(STARTING_OFFSET, DRIVE_LEN);
159 for (String s : drives.keySet()) {
160 if (shortenedPath.equals(s)) {
181 if (inputPath != null) {
201 if (inputPath != null) {
202 String result = null;
204 if (
isUNC(Paths.get(inputPath))) {
205 String potentialIP = Paths.get(inputPath.substring(REPLACEMENT_SIZE)).getName(FIRST_ITEM).toString();
206 String hostname = InetAddress.getByName(potentialIP).getHostName();
207 result = inputPath.replaceAll(potentialIP, hostname);
209 }
catch (Exception ex) {
225 synchronized public static boolean isUNC(Path inputPath) {
226 if (inputPath != null) {
227 return isUNC(inputPath.toString());
240 synchronized public static boolean isUNC(String inputPath) {
241 if (inputPath != null) {
242 return inputPath.startsWith(UNC_PATH_START);
268 Map<String, String> driveMap =
new HashMap<>();
274 File mappedDrive = Paths.get(System.getenv(TEMP_FOLDER), nameString +
MAPPED_DRIVES).toFile();
276 Files.deleteIfExists(mappedDrive.toPath());
277 ProcessBuilder builder =
new ProcessBuilder(
"cmd",
"/c",
"net",
"use");
278 builder.redirectOutput(mappedDrive);
279 builder.redirectError(mappedDrive);
280 Process p = builder.start();
281 p.waitFor(10, TimeUnit.SECONDS);
282 try (Scanner scanner =
new Scanner(mappedDrive)) {
284 while (scanner.hasNext()) {
285 String entry1 = scanner.next();
286 if (entry1.startsWith(DATA_TRIGGER)) {
289 String entry2 = scanner.next();
290 if (entry2.startsWith(DATA_TRIGGER)) {
293 String entry3 = scanner.next();
294 if (entry3.startsWith(DATA_TRIGGER)) {
298 if (entry1.length() == DRIVE_LEN && !entry1.equals(OK_TXT) && entry1.endsWith(COLON)) {
299 driveMap.put(entry1, entry2);
300 }
else if (entry2.length() == DRIVE_LEN && entry2.endsWith(COLON)) {
301 driveMap.put(entry2, entry3);
305 }
catch (IOException | InterruptedException | NoSuchElementException | IllegalStateException ex) {
310 Files.deleteIfExists(mappedDrive.toPath());
311 }
catch (IOException ex) {
synchronized void rescanDrives()
synchronized static boolean isUNC(String inputPath)
static final String UNC_PATH_START
static final String COLON
synchronized String mappedDriveToUNC(String inputPath)
static final int STARTING_OFFSET
static final String DATA_TRIGGER
static final int REPLACEMENT_SIZE
synchronized static boolean isUNC(Path inputPath)
synchronized String ipToHostName(String inputPath)
synchronized Map< String, String > getMappedDrives()
synchronized boolean isDriveMapped(String inputPath)
static final int FIRST_ITEM
synchronized Path mappedDriveToUNC(Path inputPath)
static final String OK_TXT
static Map< String, String > drives
static final String TEMP_FOLDER
static final int DRIVE_LEN
static final String C_DRIVE
synchronized boolean isDriveMapped(Path inputPath)
synchronized static Logger getLogger(String name)
synchronized Path ipToHostName(Path inputPath)
static final String MAPPED_DRIVES