Autopsy  4.20.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 
122  synchronized public Path mappedDriveToUNC(Path inputPath) {
123  if (inputPath != null) {
124  String uncPath = UNCPathUtilities.this.mappedDriveToUNC(inputPath.toString());
125  if (uncPath == null) {
126  return null;
127  } else {
128  return Paths.get(uncPath);
129  }
130  } else {
131  return null;
132  }
133  }
134 
142  synchronized public boolean isDriveMapped(Path inputPath) {
143  if (inputPath != null) {
144  return isDriveMapped(inputPath.toString());
145  } else {
146  return false;
147  }
148  }
149 
157  synchronized public boolean isDriveMapped(String inputPath) {
158  if (inputPath != null) {
159  String shortenedPath = inputPath.substring(STARTING_OFFSET, DRIVE_LEN);
160  for (String s : drives.keySet()) {
161  if (shortenedPath.equals(s)) {
162  return true;
163  }
164  }
165  }
166  return false;
167  }
168 
181  synchronized public Path ipToHostName(Path inputPath) {
182  if (inputPath != null) {
183  return Paths.get(ipToHostName(inputPath.toString()));
184  } else {
185  return null;
186  }
187  }
188 
201  synchronized public String ipToHostName(String inputPath) {
202  if (inputPath != null) {
203  String result = null;
204  try {
205  if (isUNC(Paths.get(inputPath))) {
206  String potentialIP = Paths.get(inputPath.substring(REPLACEMENT_SIZE)).getName(FIRST_ITEM).toString();
207  String hostname = InetAddress.getByName(potentialIP).getHostName();
208  result = inputPath.replaceAll(potentialIP, hostname);
209  }
210  } catch (Exception ex) {
211  // Could not resolve hostname for IP address, return null result
212  }
213  return result;
214  } else {
215  return null;
216  }
217  }
218 
226  synchronized public static boolean isUNC(Path inputPath) {
227  if (inputPath != null) {
228  return isUNC(inputPath.toString());
229  } else {
230  return false;
231  }
232  }
233 
241  synchronized public static boolean isUNC(String inputPath) {
242  if (inputPath != null) {
243  return inputPath.startsWith(UNC_PATH_START);
244  } else {
245  return false;
246  }
247  }
248 
257  synchronized public void rescanDrives() {
258  drives = getMappedDrives();
259  }
260 
268  synchronized private Map<String, String> getMappedDrives() {
269  Map<String, String> driveMap = new HashMap<>();
270 
271  if (PlatformUtil.isWindowsOS() == false) {
272  return driveMap;
273  }
274 
275  File mappedDrive = Paths.get(System.getenv(TEMP_FOLDER), nameString + MAPPED_DRIVES).toFile();
276  try {
277  Files.deleteIfExists(mappedDrive.toPath());
278  ProcessBuilder builder = new ProcessBuilder("cmd", "/c", "net", "use"); //NON-NLS
279  builder.redirectOutput(mappedDrive);
280  builder.redirectError(mappedDrive);
281  Process p = builder.start(); // throws IOException
282  p.waitFor(10, TimeUnit.SECONDS);
283  try (Scanner scanner = new Scanner(mappedDrive)) {
284  // parse the data and place it in the hashmap
285  while (scanner.hasNext()) {
286  String entry1 = scanner.next();
287  if (entry1.startsWith(DATA_TRIGGER)) {
288  continue;
289  }
290  String entry2 = scanner.next();
291  if (entry2.startsWith(DATA_TRIGGER)) {
292  continue;
293  }
294  String entry3 = scanner.next();
295  if (entry3.startsWith(DATA_TRIGGER)) {
296  continue;
297  }
298  scanner.nextLine();
299  if (entry1.length() == DRIVE_LEN && !entry1.equals(OK_TXT) && entry1.endsWith(COLON)) {
300  driveMap.put(entry1, entry2); // if there was no leading status, populate drive
301  } else if (entry2.length() == DRIVE_LEN && entry2.endsWith(COLON)) {
302  driveMap.put(entry2, entry3); // if there was a leading status, populate drive
303  }
304  }
305  }
306  } catch (IOException | InterruptedException | NoSuchElementException | IllegalStateException ex) {
307  // if we couldn't do it, no big deal
308  Logger.getLogger(UNCPathUtilities.class.getName()).log(Level.WARNING, "Unable to parse 'net use' output", ex); //NON-NLS
309  } finally {
310  try {
311  Files.deleteIfExists(mappedDrive.toPath());
312  } catch (IOException ex) {
313  // if we couldn't do it, no big deal
314  }
315  }
316  return driveMap;
317  }
318 
331  synchronized public String convertPathToUNC(String indexDir) {
332  // if we can check for UNC paths, do so, otherwise just return the indexDir
333  String result = mappedDriveToUNC(indexDir);
334  if (result == null) {
335  rescanDrives();
336  result = mappedDriveToUNC(indexDir);
337  }
338  if (result == null) {
339  return indexDir;
340  }
341  return result;
342  }
343 }
synchronized static boolean isUNC(String inputPath)
synchronized String mappedDriveToUNC(String inputPath)
synchronized static boolean isUNC(Path inputPath)
synchronized String ipToHostName(String inputPath)
synchronized String convertPathToUNC(String indexDir)
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:124
synchronized Path ipToHostName(Path inputPath)

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