From cc742bbcd6acf27a34928f6ae48e23ce2dbde93f Mon Sep 17 00:00:00 2001 From: Karl Mortensen Date: Mon, 28 Dec 2015 17:12:07 -0500 Subject: [PATCH 1/6] Check if permissions are sufficient to read and write output folder --- .../sleuthkit/autopsy/coreutils/FileUtil.java | 39 +++++++++++++++++++ .../modules/photoreccarver/Bundle.properties | 4 +- .../PhotoRecCarverFileIngestModule.java | 7 ++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/Core/src/org/sleuthkit/autopsy/coreutils/FileUtil.java b/Core/src/org/sleuthkit/autopsy/coreutils/FileUtil.java index a6f11ec684..159bb6efce 100644 --- a/Core/src/org/sleuthkit/autopsy/coreutils/FileUtil.java +++ b/Core/src/org/sleuthkit/autopsy/coreutils/FileUtil.java @@ -22,6 +22,11 @@ import java.io.File; import java.io.IOException; import java.util.logging.Level; import org.openide.filesystems.FileObject; +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.FileWriter; +import java.nio.file.Files; +import java.nio.file.Path; /** * File and dir utilities @@ -29,6 +34,8 @@ import org.openide.filesystems.FileObject; public class FileUtil { private static final Logger logger = Logger.getLogger(FileUtil.class.getName()); + private static String TEST_STRING = "Testing"; + private static String TEMP_FILE_PREFIX = "Autopsy"; /** * Recursively delete all of the files and sub-directories in a directory. @@ -167,4 +174,36 @@ public class FileUtil { //with underscores. We are only keeping \ as it could be part of the path. return fileName.replaceAll("[/:\"*?<>|]+", "_"); } + + /** + * Test if the current user has read and write access to the path. + * + * @param path The path to test for read and write access. + * + * @return True if we have both read and write access, false otherwise. + */ + public static boolean arePermissionsAppropriate(Path path) { + Path p = null; + try { + p = Files.createTempFile(path, TEMP_FILE_PREFIX, null); + try (FileWriter fw = new FileWriter(p.toFile(), false)) { + fw.write(TEST_STRING); + } + + String result; + try (BufferedReader br = new BufferedReader(new FileReader(p.toFile()))) { + result = br.readLine(); + } + return result.compareTo(TEST_STRING) == 0; + } catch (Exception ex) { + return false; + } finally { + if (p != null) { + try { + p.toFile().delete(); + } catch (Exception ignored) { + } + } + } + } } diff --git a/Core/src/org/sleuthkit/autopsy/modules/photoreccarver/Bundle.properties b/Core/src/org/sleuthkit/autopsy/modules/photoreccarver/Bundle.properties index ed1afb9be9..0d18ff4e2f 100755 --- a/Core/src/org/sleuthkit/autopsy/modules/photoreccarver/Bundle.properties +++ b/Core/src/org/sleuthkit/autopsy/modules/photoreccarver/Bundle.properties @@ -23,4 +23,6 @@ PhotoRecIngestModule.NotEnoughDiskSpace.detail.msg=PhotoRec error processing {0} PhotoRecIngestModule.cancelledByUser=PhotoRec cancelled by user. PhotoRecIngestModule.error.exitValue=PhotoRec carver returned error exit value = {0} when scanning {1} PhotoRecIngestModule.error.msg=Error processing {0} with PhotoRec carver. -PhotoRecIngestModule.complete.numberOfErrors=Number of Errors while Carving\: \ No newline at end of file +PhotoRecIngestModule.complete.numberOfErrors=Number of Errors while Carving\: +PhotoRecIngestModule.PermissionsNotSufficient=Insufficient permissions accessing +PhotoRecIngestModule.PermissionsNotSufficientSeeReference=See "Shared Drive Authentication" in Autopsy help. \ No newline at end of file diff --git a/Core/src/org/sleuthkit/autopsy/modules/photoreccarver/PhotoRecCarverFileIngestModule.java b/Core/src/org/sleuthkit/autopsy/modules/photoreccarver/PhotoRecCarverFileIngestModule.java index b923c4294a..16e3659777 100755 --- a/Core/src/org/sleuthkit/autopsy/modules/photoreccarver/PhotoRecCarverFileIngestModule.java +++ b/Core/src/org/sleuthkit/autopsy/modules/photoreccarver/PhotoRecCarverFileIngestModule.java @@ -77,6 +77,7 @@ final class PhotoRecCarverFileIngestModule implements FileIngestModule { private static final String PHOTOREC_REPORT = "report.xml"; //NON-NLS private static final String LOG_FILE = "run_log.txt"; //NON-NLS private static final String TEMP_DIR_NAME = "temp"; // NON-NLS + private static final String SEP = System.getProperty("line.separator"); private static final Logger logger = Logger.getLogger(PhotoRecCarverFileIngestModule.class.getName()); private static final HashMap totalsForIngestJobs = new HashMap<>(); private static final IngestModuleReferenceCounter refCounter = new IngestModuleReferenceCounter(); @@ -383,6 +384,12 @@ final class PhotoRecCarverFileIngestModule implements FileIngestModule { if (path == null) { throw new IngestModule.IngestModuleException(NbBundle.getMessage(PhotoRecCarverFileIngestModule.class, "PhotoRecIngestModule.nonHostnameUNCPathUsed")); } + if (false == FileUtil.arePermissionsAppropriate(path)) { + throw new IngestModule.IngestModuleException( + NbBundle.getMessage(PhotoRecCarverFileIngestModule.class, "PhotoRecIngestModule.PermissionsNotSufficient") + + SEP + path.toString()+ SEP + + NbBundle.getMessage(PhotoRecCarverFileIngestModule.class, "PhotoRecIngestModule.PermissionsNotSufficientSeeReference")); + } } try { Files.createDirectory(path); From 991e52e180660fc8641ef8bb5706da38dfa8bdef Mon Sep 17 00:00:00 2001 From: Karl Mortensen Date: Tue, 29 Dec 2015 12:16:33 -0500 Subject: [PATCH 2/6] Use built-in File checks, create before checking --- .../sleuthkit/autopsy/coreutils/FileUtil.java | 13 ++-------- .../PhotoRecCarverFileIngestModule.java | 26 +++++++++---------- 2 files changed, 15 insertions(+), 24 deletions(-) diff --git a/Core/src/org/sleuthkit/autopsy/coreutils/FileUtil.java b/Core/src/org/sleuthkit/autopsy/coreutils/FileUtil.java index 159bb6efce..2725a22135 100644 --- a/Core/src/org/sleuthkit/autopsy/coreutils/FileUtil.java +++ b/Core/src/org/sleuthkit/autopsy/coreutils/FileUtil.java @@ -34,7 +34,6 @@ import java.nio.file.Path; public class FileUtil { private static final Logger logger = Logger.getLogger(FileUtil.class.getName()); - private static String TEST_STRING = "Testing"; private static String TEMP_FILE_PREFIX = "Autopsy"; /** @@ -186,16 +185,8 @@ public class FileUtil { Path p = null; try { p = Files.createTempFile(path, TEMP_FILE_PREFIX, null); - try (FileWriter fw = new FileWriter(p.toFile(), false)) { - fw.write(TEST_STRING); - } - - String result; - try (BufferedReader br = new BufferedReader(new FileReader(p.toFile()))) { - result = br.readLine(); - } - return result.compareTo(TEST_STRING) == 0; - } catch (Exception ex) { + return (p.toFile().canRead() && p.toFile().canWrite()); + } catch (IOException ex) { return false; } finally { if (p != null) { diff --git a/Core/src/org/sleuthkit/autopsy/modules/photoreccarver/PhotoRecCarverFileIngestModule.java b/Core/src/org/sleuthkit/autopsy/modules/photoreccarver/PhotoRecCarverFileIngestModule.java index 16e3659777..c93182bba9 100755 --- a/Core/src/org/sleuthkit/autopsy/modules/photoreccarver/PhotoRecCarverFileIngestModule.java +++ b/Core/src/org/sleuthkit/autopsy/modules/photoreccarver/PhotoRecCarverFileIngestModule.java @@ -378,21 +378,21 @@ final class PhotoRecCarverFileIngestModule implements FileIngestModule { */ synchronized Path createModuleOutputDirectoryForCase() throws IngestModule.IngestModuleException { Path path = Paths.get(Case.getCurrentCase().getModuleDirectory(), PhotoRecCarverIngestModuleFactory.getModuleName()); - if (UNCPathUtilities.isUNC(path)) { - // if the UNC path is using an IP address, convert to hostname - path = uncPathUtilities.ipToHostName(path); - if (path == null) { - throw new IngestModule.IngestModuleException(NbBundle.getMessage(PhotoRecCarverFileIngestModule.class, "PhotoRecIngestModule.nonHostnameUNCPathUsed")); - } - if (false == FileUtil.arePermissionsAppropriate(path)) { - throw new IngestModule.IngestModuleException( - NbBundle.getMessage(PhotoRecCarverFileIngestModule.class, "PhotoRecIngestModule.PermissionsNotSufficient") - + SEP + path.toString()+ SEP + - NbBundle.getMessage(PhotoRecCarverFileIngestModule.class, "PhotoRecIngestModule.PermissionsNotSufficientSeeReference")); - } - } try { Files.createDirectory(path); + if (UNCPathUtilities.isUNC(path)) { + // if the UNC path is using an IP address, convert to hostname + path = uncPathUtilities.ipToHostName(path); + if (path == null) { + throw new IngestModule.IngestModuleException(NbBundle.getMessage(PhotoRecCarverFileIngestModule.class, "PhotoRecIngestModule.nonHostnameUNCPathUsed")); + } + if (false == FileUtil.arePermissionsAppropriate(path)) { + throw new IngestModule.IngestModuleException( + NbBundle.getMessage(PhotoRecCarverFileIngestModule.class, "PhotoRecIngestModule.PermissionsNotSufficient") + + SEP + path.toString() + SEP + + NbBundle.getMessage(PhotoRecCarverFileIngestModule.class, "PhotoRecIngestModule.PermissionsNotSufficientSeeReference")); + } + } } catch (FileAlreadyExistsException ex) { // No worries. } catch (IOException | SecurityException | UnsupportedOperationException ex) { From a96124952c0cd4cce94ff0e84647439031ced5c2 Mon Sep 17 00:00:00 2001 From: Karl Mortensen Date: Mon, 4 Jan 2016 11:54:41 -0500 Subject: [PATCH 3/6] insert comment --- .../modules/photoreccarver/PhotoRecCarverFileIngestModule.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Core/src/org/sleuthkit/autopsy/modules/photoreccarver/PhotoRecCarverFileIngestModule.java b/Core/src/org/sleuthkit/autopsy/modules/photoreccarver/PhotoRecCarverFileIngestModule.java index c93182bba9..814b76ee18 100755 --- a/Core/src/org/sleuthkit/autopsy/modules/photoreccarver/PhotoRecCarverFileIngestModule.java +++ b/Core/src/org/sleuthkit/autopsy/modules/photoreccarver/PhotoRecCarverFileIngestModule.java @@ -389,7 +389,7 @@ final class PhotoRecCarverFileIngestModule implements FileIngestModule { if (false == FileUtil.arePermissionsAppropriate(path)) { throw new IngestModule.IngestModuleException( NbBundle.getMessage(PhotoRecCarverFileIngestModule.class, "PhotoRecIngestModule.PermissionsNotSufficient") - + SEP + path.toString() + SEP + + SEP + path.toString() + SEP // SEP breaks lines to the dialog displays nicely. + NbBundle.getMessage(PhotoRecCarverFileIngestModule.class, "PhotoRecIngestModule.PermissionsNotSufficientSeeReference")); } } From 3ce23e8b1dc950ec9247108d71d8ed02433db1db Mon Sep 17 00:00:00 2001 From: Karl Mortensen Date: Mon, 4 Jan 2016 11:55:47 -0500 Subject: [PATCH 4/6] insert comment --- .../modules/photoreccarver/PhotoRecCarverFileIngestModule.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Core/src/org/sleuthkit/autopsy/modules/photoreccarver/PhotoRecCarverFileIngestModule.java b/Core/src/org/sleuthkit/autopsy/modules/photoreccarver/PhotoRecCarverFileIngestModule.java index 814b76ee18..aa0e158794 100755 --- a/Core/src/org/sleuthkit/autopsy/modules/photoreccarver/PhotoRecCarverFileIngestModule.java +++ b/Core/src/org/sleuthkit/autopsy/modules/photoreccarver/PhotoRecCarverFileIngestModule.java @@ -389,7 +389,7 @@ final class PhotoRecCarverFileIngestModule implements FileIngestModule { if (false == FileUtil.arePermissionsAppropriate(path)) { throw new IngestModule.IngestModuleException( NbBundle.getMessage(PhotoRecCarverFileIngestModule.class, "PhotoRecIngestModule.PermissionsNotSufficient") - + SEP + path.toString() + SEP // SEP breaks lines to the dialog displays nicely. + + SEP + path.toString() + SEP // SEP is line breaks to make the dialog display nicely. + NbBundle.getMessage(PhotoRecCarverFileIngestModule.class, "PhotoRecIngestModule.PermissionsNotSufficientSeeReference")); } } From a69ba481968156a032c83a07968a88962cf613c3 Mon Sep 17 00:00:00 2001 From: Karl Mortensen Date: Mon, 4 Jan 2016 11:57:03 -0500 Subject: [PATCH 5/6] rename method --- Core/src/org/sleuthkit/autopsy/coreutils/FileUtil.java | 2 +- .../modules/photoreccarver/PhotoRecCarverFileIngestModule.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Core/src/org/sleuthkit/autopsy/coreutils/FileUtil.java b/Core/src/org/sleuthkit/autopsy/coreutils/FileUtil.java index 2725a22135..29cbd1c4b5 100644 --- a/Core/src/org/sleuthkit/autopsy/coreutils/FileUtil.java +++ b/Core/src/org/sleuthkit/autopsy/coreutils/FileUtil.java @@ -181,7 +181,7 @@ public class FileUtil { * * @return True if we have both read and write access, false otherwise. */ - public static boolean arePermissionsAppropriate(Path path) { + public static boolean hasReadWriteAccess(Path path) { Path p = null; try { p = Files.createTempFile(path, TEMP_FILE_PREFIX, null); diff --git a/Core/src/org/sleuthkit/autopsy/modules/photoreccarver/PhotoRecCarverFileIngestModule.java b/Core/src/org/sleuthkit/autopsy/modules/photoreccarver/PhotoRecCarverFileIngestModule.java index aa0e158794..bb8d8ed62f 100755 --- a/Core/src/org/sleuthkit/autopsy/modules/photoreccarver/PhotoRecCarverFileIngestModule.java +++ b/Core/src/org/sleuthkit/autopsy/modules/photoreccarver/PhotoRecCarverFileIngestModule.java @@ -386,7 +386,7 @@ final class PhotoRecCarverFileIngestModule implements FileIngestModule { if (path == null) { throw new IngestModule.IngestModuleException(NbBundle.getMessage(PhotoRecCarverFileIngestModule.class, "PhotoRecIngestModule.nonHostnameUNCPathUsed")); } - if (false == FileUtil.arePermissionsAppropriate(path)) { + if (false == FileUtil.hasReadWriteAccess(path)) { throw new IngestModule.IngestModuleException( NbBundle.getMessage(PhotoRecCarverFileIngestModule.class, "PhotoRecIngestModule.PermissionsNotSufficient") + SEP + path.toString() + SEP // SEP is line breaks to make the dialog display nicely. From 50e06fe4aa7923164a1fca661face02fc2a0f644 Mon Sep 17 00:00:00 2001 From: Karl Mortensen Date: Mon, 4 Jan 2016 15:01:03 -0500 Subject: [PATCH 6/6] remove unused imports --- Core/src/org/sleuthkit/autopsy/coreutils/FileUtil.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/Core/src/org/sleuthkit/autopsy/coreutils/FileUtil.java b/Core/src/org/sleuthkit/autopsy/coreutils/FileUtil.java index 29cbd1c4b5..b1d1c33c51 100644 --- a/Core/src/org/sleuthkit/autopsy/coreutils/FileUtil.java +++ b/Core/src/org/sleuthkit/autopsy/coreutils/FileUtil.java @@ -22,9 +22,6 @@ import java.io.File; import java.io.IOException; import java.util.logging.Level; import org.openide.filesystems.FileObject; -import java.io.BufferedReader; -import java.io.FileReader; -import java.io.FileWriter; import java.nio.file.Files; import java.nio.file.Path;