From ac5b8cbb44c114ab8ff4dd7450ed2811526ff120 Mon Sep 17 00:00:00 2001
From: Greg DiCristofaro <gregd@basistech.com>
Date: Tue, 2 Feb 2021 13:52:40 -0500
Subject: [PATCH] updates for embedded solr on os x

---
 .gitattributes                                |  6 ++
 .../autopsy/keywordsearch/Server.java         | 60 +++++++++++++++++++
 unix_setup.sh                                 |  3 +
 3 files changed, 69 insertions(+)

diff --git a/.gitattributes b/.gitattributes
index cd5271c982..8749e5dadb 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -13,3 +13,9 @@ Doxyfile                text
 
 *.py                  text diff=python
 *.pl			text
+
+# ensure solr scripts that are bash scripts not ending with.sh are lf instead of crlf
+/KeywordSearch/solr/bin/autopsy-solr  eol=lf
+/KeywordSearch/solr/bin/init.d/solr  eol=lf
+/KeywordSearch/solr/bin/post  eol=lf
+/KeywordSearch/solr/bin/solr  eol=lf
\ No newline at end of file
diff --git a/KeywordSearch/src/org/sleuthkit/autopsy/keywordsearch/Server.java b/KeywordSearch/src/org/sleuthkit/autopsy/keywordsearch/Server.java
index a27eb4db78..8f4abc8d90 100644
--- a/KeywordSearch/src/org/sleuthkit/autopsy/keywordsearch/Server.java
+++ b/KeywordSearch/src/org/sleuthkit/autopsy/keywordsearch/Server.java
@@ -31,6 +31,7 @@
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.net.ConnectException;
+import java.net.DatagramSocket;
 import java.net.ServerSocket;
 import java.net.SocketException;
 import java.nio.charset.Charset;
@@ -767,6 +768,23 @@ public void run() {
      * @param port the port to check for availability
      */
     static boolean isPortAvailable(int port) {
+        final String osName = PlatformUtil.getOSName().toLowerCase();
+        if (osName != null && osName.toLowerCase().startsWith("mac")) {
+            return isPortAvailableOSX(port);
+        } else {
+            return isPortAvailableDefault(port);
+        }
+    }
+
+    /**
+     * Checks to see if a specific port is available.
+     *
+     * NOTE: This is used on non-OS X systems as of right now but could be
+     * replaced with the OS X version.
+     *
+     * @param port the port to check for availability
+     */
+    static boolean isPortAvailableDefault(int port) {
         ServerSocket ss = null;
         try {
 
@@ -792,6 +810,48 @@ static boolean isPortAvailable(int port) {
         return false;
     }
 
+    /**
+     * Checks to see if a specific port is available.
+     *
+     * NOTE: This is only used on OSX for now, but could replace default 
+     * implementation in the future.
+     * 
+     * @param port The port to check for availability.
+     * @throws IllegalArgumentException If port is outside range of possible ports.
+     */
+    static boolean isPortAvailableOSX(int port) {
+        // implementation taken from https://stackoverflow.com/a/435579
+        if (port < 1 || port > 65535) {
+            throw new IllegalArgumentException("Invalid start port: " + port);
+        }
+
+        ServerSocket ss = null;
+        DatagramSocket ds = null;
+        try {
+            ss = new ServerSocket(port);
+            ss.setReuseAddress(true);
+            ds = new DatagramSocket(port);
+            ds.setReuseAddress(true);
+            return true;
+        } catch (IOException e) {
+        } finally {
+            if (ds != null) {
+                ds.close();
+            }
+
+            if (ss != null) {
+                try {
+                    ss.close();
+                } catch (IOException e) {
+                    /* should not be thrown */
+                }
+            }
+        }
+
+        return false;
+    }
+
+
     /**
      * Changes the current solr server port. Only call this after available.
      *
diff --git a/unix_setup.sh b/unix_setup.sh
index 93a5eb4bea..40b1d858b4 100644
--- a/unix_setup.sh
+++ b/unix_setup.sh
@@ -76,6 +76,9 @@ fi
 chmod u+x autopsy/markmckinnon/Export*
 chmod u+x autopsy/markmckinnon/parse*
 
+# allow solr dependencies to execute
+chmod -R u+x autopsy/solr/bin
+
 # make sure it is executable
 chmod u+x bin/autopsy
 
-- 
GitLab