Autopsy  4.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
UNCPathUtilities.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2016 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.coreutils;
20 
21 import java.io.File;
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;
28 import java.util.Map;
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;
34 
35 public class UNCPathUtilities {
36 
37  private static Map<String, String> drives;
38  private static final String MAPPED_DRIVES = "_mapped_drives.txt"; //NON-NLS
39  private static final String TEMP_FOLDER = "TEMP"; //NON-NLS
40  private static final String DATA_TRIGGER = "----------"; //NON-NLS
41  private static final String OK_TXT = "OK"; //NON-NLS
42  private static final String COLON = ":"; //NON-NLS
43  private static final String UNC_PATH_START = "\\\\"; //NON-NLS
44  private static final String C_DRIVE = "C:"; //NON-NLS
45  private static final int DRIVE_LEN = 2;
46  private static final int STARTING_OFFSET = 0;
47  private static final int REPLACEMENT_SIZE = 2;
48  private static final int FIRST_ITEM = 0;
49  private final String nameString;
50 
54  public UNCPathUtilities() {
55  // get UUID for this instance
56  this.nameString = UUID.randomUUID().toString();
57  drives = getMappedDrives();
58  }
59 
78  synchronized public String mappedDriveToUNC(String inputPath) {
79  if (inputPath != null) {
80  // If it is a C:, do not attempt to convert. This is for the single-user case.
81  if (inputPath.toUpperCase().startsWith(C_DRIVE)) {
82  return null;
83  }
84  if (false == isUNC(inputPath)) {
85  String uncPath = null;
86  try {
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());
91  }
92  } catch (Exception ex) {
93  // Didn't work. Skip it.
94  }
95  return uncPath;
96  } else {
97  return inputPath;
98  }
99  } else {
100  return null;
101  }
102  }
103 
121  synchronized public Path mappedDriveToUNC(Path inputPath) {
122  if (inputPath != null) {
123  String uncPath = UNCPathUtilities.this.mappedDriveToUNC(inputPath.toString());
124  if (uncPath == null) {
125  return null;
126  } else {
127  return Paths.get(uncPath);
128  }
129  } else {
130  return null;
131  }
132  }
133 
141  synchronized public boolean isDriveMapped(Path inputPath) {
142  if (inputPath != null) {
143  return isDriveMapped(inputPath.toString());
144  } else {
145  return false;
146  }
147  }
148 
156  synchronized public boolean isDriveMapped(String inputPath) {
157  if (inputPath != null) {
158  String shortenedPath = inputPath.substring(STARTING_OFFSET, DRIVE_LEN);
159  for (String s : drives.keySet()) {
160  if (shortenedPath.equals(s)) {
161  return true;
162  }
163  }
164  }
165  return false;
166  }
167 
180  synchronized public Path ipToHostName(Path inputPath) {
181  if (inputPath != null) {
182  return Paths.get(ipToHostName(inputPath.toString()));
183  } else {
184  return null;
185  }
186  }
187 
200  synchronized public String ipToHostName(String inputPath) {
201  if (inputPath != null) {
202  String result = null;
203  try {
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);
208  }
209  } catch (Exception ex) {
210  // Could not resolve hostname for IP address, return null result
211  }
212  return result;
213  } else {
214  return null;
215  }
216  }
217 
225  synchronized public static boolean isUNC(Path inputPath) {
226  if (inputPath != null) {
227  return isUNC(inputPath.toString());
228  } else {
229  return false;
230  }
231  }
232 
240  synchronized public static boolean isUNC(String inputPath) {
241  if (inputPath != null) {
242  return inputPath.startsWith(UNC_PATH_START);
243  } else {
244  return false;
245  }
246  }
247 
256  synchronized public void rescanDrives() {
257  drives = getMappedDrives();
258  }
259 
267  synchronized private Map<String, String> getMappedDrives() {
268  Map<String, String> driveMap = new HashMap<>();
269 
270  if (PlatformUtil.isWindowsOS() == false) {
271  return driveMap;
272  }
273 
274  File mappedDrive = Paths.get(System.getenv(TEMP_FOLDER), nameString + MAPPED_DRIVES).toFile();
275  try {
276  Files.deleteIfExists(mappedDrive.toPath());
277  ProcessBuilder builder = new ProcessBuilder("cmd", "/c", "net", "use"); //NON-NLS
278  builder.redirectOutput(mappedDrive);
279  builder.redirectError(mappedDrive);
280  Process p = builder.start(); // throws IOException
281  p.waitFor(10, TimeUnit.SECONDS);
282  try (Scanner scanner = new Scanner(mappedDrive)) {
283  // parse the data and place it in the hashmap
284  while (scanner.hasNext()) {
285  String entry1 = scanner.next();
286  if (entry1.startsWith(DATA_TRIGGER)) {
287  continue;
288  }
289  String entry2 = scanner.next();
290  if (entry2.startsWith(DATA_TRIGGER)) {
291  continue;
292  }
293  String entry3 = scanner.next();
294  if (entry3.startsWith(DATA_TRIGGER)) {
295  continue;
296  }
297  scanner.nextLine();
298  if (entry1.length() == DRIVE_LEN && !entry1.equals(OK_TXT) && entry1.endsWith(COLON)) {
299  driveMap.put(entry1, entry2); // if there was no leading status, populate drive
300  } else if (entry2.length() == DRIVE_LEN && entry2.endsWith(COLON)) {
301  driveMap.put(entry2, entry3); // if there was a leading status, populate drive
302  }
303  }
304  }
305  } catch (IOException | InterruptedException | NoSuchElementException | IllegalStateException ex) {
306  // if we couldn't do it, no big deal
307  Logger.getLogger(UNCPathUtilities.class.getName()).log(Level.WARNING, "Unable to parse 'net use' output", ex); //NON-NLS
308  } finally {
309  try {
310  Files.deleteIfExists(mappedDrive.toPath());
311  } catch (IOException ex) {
312  // if we couldn't do it, no big deal
313  }
314  }
315  return driveMap;
316  }
317 }
synchronized static boolean isUNC(String inputPath)
synchronized String mappedDriveToUNC(String inputPath)
synchronized static boolean isUNC(Path inputPath)
synchronized String ipToHostName(String inputPath)
synchronized Map< String, String > getMappedDrives()
synchronized boolean isDriveMapped(String inputPath)
synchronized Path mappedDriveToUNC(Path inputPath)
synchronized boolean isDriveMapped(Path inputPath)
synchronized static Logger getLogger(String name)
Definition: Logger.java:166
synchronized Path ipToHostName(Path inputPath)

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