JAL-3594 JAL-3728 Added taskbar icons to desktop and Java console. Changed "Jalview...
[jalview.git] / src / jalview / bin / Cache.java
index 6648099..e6f86fe 100755 (executable)
@@ -34,6 +34,7 @@ import java.net.PasswordAuthentication;
 import java.net.URL;
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.Date;
 import java.util.Enumeration;
@@ -52,13 +53,16 @@ import org.apache.log4j.Logger;
 import org.apache.log4j.SimpleLayout;
 
 import jalview.datamodel.PDBEntry;
+import jalview.gui.Preferences;
 import jalview.gui.UserDefinedColours;
 import jalview.schemes.ColourSchemeLoader;
 import jalview.schemes.ColourSchemes;
 import jalview.schemes.UserColourScheme;
 import jalview.structure.StructureImportSettings;
 import jalview.urls.IdOrgSettings;
+import jalview.util.ChannelProperties;
 import jalview.util.ColorUtils;
+import jalview.util.MessageManager;
 import jalview.util.Platform;
 import jalview.ws.sifts.SiftsSettings;
 
@@ -292,6 +296,9 @@ public class Cache
 
   public final static String PROXYTYPE_CUSTOM = "true";
 
+  // in-memory only storage of proxy password, safer to use char array
+  public static char[] proxyAuthPassword = null;
+
   /** Jalview Properties */
   public static Properties applicationProperties = new Properties()
   {
@@ -350,7 +357,8 @@ public class Cache
       // lcastor.addAppender(ap);
       // jalview.bin.Cache.log.addAppender(ap);
       // Tell the user that debug is enabled
-      jalview.bin.Cache.log.debug("Jalview Debugging Output Follows.");
+      jalview.bin.Cache.log.debug(ChannelProperties.getProperty("app_name")
+              + " Debugging Output Follows.");
     } catch (Exception ex)
     {
       System.err.println("Problems initializing the log4j system\n");
@@ -431,7 +439,7 @@ public class Cache
     switch (proxyType)
     {
     case PROXYTYPE_NONE:
-      setProxyProperties(null, null, null, null, null, null, null, null);
+      clearProxyProperties();
       break;
     case PROXYTYPE_SYSTEM: // use system settings
       resetProxyProperties();
@@ -442,14 +450,11 @@ public class Cache
       String httpsHost = getDefault("PROXY_SERVER_HTTPS", httpHost);
       String httpsPort = getDefault("PROXY_PORT_HTTPS", httpPort);
       String httpUser = getDefault("PROXY_AUTH_USER", null);
-      String httpPassword = getDefault("PROXY_AUTH_PASSWORD", null);
       // https.proxyUser and https.proxyPassword are not able to be
-      // independently set in Preferences yet
+      // independently set in Preferences yet (or http.nonProxyHosts)
       String httpsUser = getDefault("PROXY_AUTH_USER_HTTPS", httpUser);
-      String httpsPassword = getDefault("PROXY_AUTH_PASSWORD_HTTPS",
-              httpPassword);
       setProxyProperties(httpHost, httpPort, httpsHost, httpsPort, httpUser,
-              httpPassword, httpsUser, httpsPassword);
+              proxyAuthPassword, httpsUser, proxyAuthPassword, "localhost");
       break;
     default:
       String message = "Incorrect PROXY_TYPE - should be 'none' (clear proxy properties), 'false' (system settings), 'true' (custom settings): "
@@ -658,8 +663,8 @@ public class Cache
     new BuildDetails(codeVersion, null, codeInstallation);
     if (printVersion && reportVersion)
     {
-      System.out.println(
-              "Jalview Version: " + codeVersion + codeInstallation);
+      System.out.println(ChannelProperties.getProperty("app_name")
+              + " Version: " + codeVersion + codeInstallation);
     }
   }
 
@@ -826,7 +831,8 @@ public class Cache
 
           lvclient.addAppender(log.getAppender("JalviewLogger"));
           // Tell the user that debug is enabled
-          lvclient.debug("Jalview Vamsas Client Debugging Output Follows.");
+          lvclient.debug(ChannelProperties.getProperty("app_name")
+                  + " Vamsas Client Debugging Output Follows.");
         }
       } catch (Exception e)
       {
@@ -865,7 +871,8 @@ public class Cache
 
           lgclient.addAppender(log.getAppender("JalviewLogger"));
           // Tell the user that debug is enabled
-          lgclient.debug("Jalview Groovy Client Debugging Output Follows.");
+          lgclient.debug(ChannelProperties.getProperty("app_name")
+                  + " Groovy Client Debugging Output Follows.");
         }
       } catch (Error e)
       {
@@ -927,7 +934,7 @@ public class Cache
                 .getConstructor(new Class[]
                 { String.class, String.class, String.class })
                 .newInstance(new Object[]
-                { "Jalview Desktop",
+                { ChannelProperties.getProperty("app_name") + " Desktop",
                     (vrs = jalview.bin.Cache.getProperty("VERSION") + "_"
                             + jalview.bin.Cache.getDefault("BUILD_DATE",
                                     "unknown")),
@@ -1203,10 +1210,12 @@ public class Cache
   public static String getVersionDetailsForConsole()
   {
     StringBuilder sb = new StringBuilder();
-    sb.append("Jalview Version: ");
+    sb.append(ChannelProperties.getProperty("app_name"))
+            .append(" Version: ");
     sb.append(jalview.bin.Cache.getDefault("VERSION", "TEST"));
     sb.append("\n");
-    sb.append("Jalview Installation: ");
+    sb.append(ChannelProperties.getProperty("app_name"))
+            .append(" Installation: ");
     sb.append(jalview.bin.Cache.getDefault("INSTALLATION", "unknown"));
     sb.append("\n");
     sb.append("Build Date: ");
@@ -1268,12 +1277,23 @@ public class Cache
   }
 
   // proxy properties methods
+  public static void clearProxyProperties()
+  {
+    setProxyProperties(null, null, null, null, null, null, null, null,
+            null);
+  }
+
   public static void resetProxyProperties()
   {
     setProxyProperties(startupProxyProperties[0], startupProxyProperties[1],
             startupProxyProperties[2], startupProxyProperties[3],
-            startupProxyProperties[4], startupProxyProperties[5],
-            startupProxyProperties[6], startupProxyProperties[7]);
+            startupProxyProperties[4],
+            startupProxyProperties[5] == null ? null
+                    : startupProxyProperties[5].toCharArray(),
+            startupProxyProperties[6],
+            startupProxyProperties[7] == null ? null
+                    : startupProxyProperties[7].toCharArray(),
+            startupProxyProperties[8]);
     StringBuilder sb = new StringBuilder();
     sb.append("Setting proxy properties to: http.proxyHost=")
             .append(startupProxyProperties[0]).append(", http.proxyPort=")
@@ -1292,30 +1312,105 @@ public class Cache
     Cache.debug(sb.toString());
   }
 
-  public static void setProxyProperties(String httpHost, String httpPort,
-          String httpsHost, String httpsPort, String httpUser,
-          String httpPassword)
+  public static void setProxyPropertiesFromPreferences()
   {
-    setProxyProperties(httpHost, httpPort, httpsHost, httpsPort, httpUser,
-            httpPassword, httpUser, httpPassword);
+    setProxyPropertiesFromPreferences(Cache.PROXYTYPE_SYSTEM);
+  }
+
+  public static void setProxyPropertiesFromPreferences(
+          String previousProxyType)
+  {
+    String proxyType = Cache.getDefault("USE_PROXY",
+            Cache.PROXYTYPE_SYSTEM);
+    if (previousProxyType != null
+            && !proxyType.equals(Cache.PROXYTYPE_CUSTOM) // always apply
+                                                         // customProxy
+            && proxyType.equals(previousProxyType))
+    {
+      // no change
+      return;
+    }
+    switch (proxyType)
+    {
+    case Cache.PROXYTYPE_NONE:
+      if (!previousProxyType.equals(proxyType))
+      {
+        Cache.log.info("Setting no proxy settings");
+        Cache.setProxyProperties(null, null, null, null, null, null, null,
+                null, null);
+      }
+      break;
+    case Cache.PROXYTYPE_CUSTOM:
+      // always re-set a custom proxy -- it might have changed, particularly
+      // password
+      Cache.log.info("Setting custom proxy settings");
+      boolean proxyAuthSet = Cache.getDefault("PROXY_AUTH", false);
+      Cache.setProxyProperties(Cache.getDefault("PROXY_SERVER", null),
+              Cache.getDefault("PROXY_PORT", null),
+              Cache.getDefault("PROXY_SERVER_HTTPS", null),
+              Cache.getDefault("PROXY_PORT_HTTPS", null),
+              proxyAuthSet ? Cache.getDefault("PROXY_AUTH_USERNAME", "")
+                      : null,
+              proxyAuthSet ? Cache.proxyAuthPassword : null,
+              proxyAuthSet ? Cache.getDefault("PROXY_AUTH_USERNAME", "")
+                      : null,
+              proxyAuthSet ? Cache.proxyAuthPassword : null, "localhost");
+      break;
+    default: // system proxy settings by default
+      Cache.log.info("Setting system proxy settings");
+      Cache.resetProxyProperties();
+    }
   }
 
   public static void setProxyProperties(String httpHost, String httpPort,
           String httpsHost, String httpsPort, String httpUser,
-          String httpPassword, String httpsUser, String httpsPassword)
+          char[] httpPassword, String httpsUser, char[] httpsPassword,
+          String nonProxyHosts)
   {
     setOrClearSystemProperty("http.proxyHost", httpHost);
     setOrClearSystemProperty("http.proxyPort", httpPort);
     setOrClearSystemProperty("https.proxyHost", httpsHost);
     setOrClearSystemProperty("https.proxyPort", httpsPort);
     setOrClearSystemProperty("http.proxyUser", httpUser);
-    setOrClearSystemProperty("http.proxyPassword", httpPassword);
     setOrClearSystemProperty("https.proxyUser", httpsUser);
-    setOrClearSystemProperty("https.proxyPassword", httpsPassword);
+    // note: passwords for http.proxyPassword and https.proxyPassword are sent
+    // via the Authenticator, properties do not need to be set
+
+    // are we using a custom proxy (password prompt might be required)?
+    boolean customProxySet = getDefault("USE_PROXY", PROXYTYPE_SYSTEM)
+            .equals(PROXYTYPE_CUSTOM);
+
+    /*
+     * A bug in Java means the AuthCache does not get reset, so once it has working credentials,
+     * it never asks for more, so changing the Authenticator has no effect (as getPasswordAuthentication()
+     * is not re-called).
+     * This could lead to password leak to a hostile proxy server, so I'm putting in a hack to clear
+     * the AuthCache.
+     * see https://www.generacodice.com/en/articolo/154918/Reset-the-Authenticator-credentials
+     * ...
+     * Turns out this is only accessible in Java 8, and not in Java 9 onwards, so commenting out
+     */
+    /*
+    try
+    {
+      sun.net.www.protocol.http.AuthCacheValue
+              .setAuthCache(new sun.net.www.protocol.http.AuthCacheImpl());
+    } catch (Throwable t)
+    {
+      Cache.error(t.getMessage());
+      Cache.debug(getStackTraceString(t));
+    }
+    */
+
     if (httpUser != null || httpsUser != null)
     {
       try
       {
+        char[] displayHttpPw = new char[httpPassword == null ? 0
+                : httpPassword.length];
+        Arrays.fill(displayHttpPw, '*');
+        Cache.debug("CACHE Proxy: setting new Authenticator with httpUser='"
+                + httpUser + "' httpPassword='" + displayHttpPw + "'");
         Authenticator.setDefault(new Authenticator()
         {
           @Override
@@ -1323,37 +1418,77 @@ public class Cache
           {
             if (getRequestorType() == RequestorType.PROXY)
             {
-              try
+              String protocol = getRequestingProtocol();
+              boolean needProxyPasswordSet = false;
+              if (customProxySet &&
+              // we have a username but no password for the scheme being
+              // requested
+              (protocol.equalsIgnoreCase("http")
+                      && (httpUser != null && httpUser.length() > 0
+                              && (httpPassword == null
+                                      || httpPassword.length == 0)))
+                      || (protocol.equalsIgnoreCase("https")
+                              && (httpsUser != null
+                                      && httpsUser.length() > 0
+                                      && (httpsPassword == null
+                                              || httpsPassword.length == 0))))
               {
-                if (getRequestingScheme().equalsIgnoreCase("http")
-                        && getRequestingHost().equalsIgnoreCase(httpHost)
-                        && getRequestingPort() == Integer.valueOf(httpPort))
+                // open Preferences -> Connections
+                String message = MessageManager
+                        .getString("label.proxy_password_required");
+                Preferences.openPreferences(Preferences.CONNECTIONS_TAB,
+                        message);
+                Preferences.getInstance()
+                        .proxyAuthPasswordCheckHighlight(true, true);
+              }
+              else
+              {
+                try
                 {
-                  return new PasswordAuthentication(httpUser,
-                          httpPassword == null ? new char[] {}
-                                  : httpPassword.toCharArray());
-                }
-                if (getRequestingScheme().equalsIgnoreCase("https")
-                        && getRequestingHost().equalsIgnoreCase(httpsHost)
-                        && getRequestingPort() == Integer
-                                .valueOf(httpsPort))
+                  if (protocol.equalsIgnoreCase("http")
+                          && getRequestingHost().equalsIgnoreCase(httpHost)
+                          && getRequestingPort() == Integer
+                                  .valueOf(httpPort))
+                  {
+                    Cache.debug(
+                            "AUTHENTICATOR returning PasswordAuthentication(\""
+                                    + httpUser + "\", '"
+                                    + new String(displayHttpPw) + "')");
+                    return new PasswordAuthentication(httpUser,
+                            httpPassword);
+                  }
+                  if (protocol.equalsIgnoreCase("https")
+                          && getRequestingHost().equalsIgnoreCase(httpsHost)
+                          && getRequestingPort() == Integer
+                                  .valueOf(httpsPort))
+                  {
+                    char[] displayHttpsPw = new char[httpPassword.length];
+                    Arrays.fill(displayHttpsPw, '*');
+                    Cache.debug(
+                            "AUTHENTICATOR returning PasswordAuthentication(\""
+                                    + httpsUser + "\", '" + displayHttpsPw
+                                    + "'");
+                    return new PasswordAuthentication(httpsUser,
+                            httpsPassword);
+                  }
+                } catch (NumberFormatException e)
                 {
-                  return new PasswordAuthentication(httpsUser,
-                          httpsPassword == null ? new char[] {}
-                                  : httpsPassword.toCharArray());
+                  Cache.error("Problem with proxy port values [http:"
+                          + httpPort + ", https:" + httpsPort + "]");
                 }
-              } catch (NumberFormatException e)
-              {
-                Cache.error("Problem with proxy port values [http:"
-                        + httpPort + ", https:" + httpsPort + "]");
+                Cache.debug(
+                        "AUTHENTICATOR after trying to get PasswordAuthentication");
               }
             }
             // non proxy request
+            Cache.debug("AUTHENTICATOR returning null");
             return null;
           }
         });
         // required to re-enable basic authentication (should be okay for a
         // local proxy)
+        Cache.debug(
+                "AUTHENTICATOR setting property 'jdk.http.auth.tunneling.disabledSchemes' to \"\"");
         System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
       } catch (SecurityException e)
       {
@@ -1365,21 +1500,23 @@ public class Cache
     {
       // reset the Authenticator to protect http.proxyUser and
       // http.proxyPassword Just In Case
-      Authenticator.setDefault(new Authenticator()
-      {
-        @Override
-        protected PasswordAuthentication getPasswordAuthentication()
-        {
-          return null;
-        }
-      });
+      /* as noted above, due to bug in java this doesn't work if the sun.net.www.protocol.http.AuthCache
+       * has working credentials. No workaround for Java 11.
+       */
+      Cache.debug("AUTHENTICATOR setting default Authenticator to null");
+      Authenticator.setDefault(null);
     }
 
-    // clear localhost from proxying unless nonProxyHosts already set (not
-    // currently configurable in Preferences)
-    String nonProxyHosts = startupProxyProperties[8];
-    System.setProperty("http.nonProxyHosts",
-            nonProxyHosts == null ? "localhost" : nonProxyHosts);
+    // nonProxyHosts not currently configurable in Preferences
+    Cache.debug("AUTHENTICATOR setting property 'http.nonProxyHosts' to \""
+            + nonProxyHosts + "\"");
+    setOrClearSystemProperty("http.nonProxyHosts", nonProxyHosts);
+  }
+
+  public static void setOrClearSystemProperty(String key, char[] value)
+  {
+    setOrClearSystemProperty(key,
+            (value == null) ? null : new String(value));
   }
 
   public static void setOrClearSystemProperty(String key, String value)
@@ -1398,25 +1535,32 @@ public class Cache
     }
   }
 
-  public final static int DEBUG = 10;
+  public final static int TRACE = 10;
 
-  public final static int INFO = 20;
+  public final static int DEBUG = 20;
 
-  public final static int WARN = 30;
+  public final static int INFO = 30;
 
-  public final static int ERROR = 40;
+  public final static int WARN = 40;
+
+  public final static int ERROR = 50;
 
   public static boolean println(int level, String message)
   {
     if (Cache.log == null)
     {
-      if (level >= ERROR)
+      if (level >= WARN)
         System.err.println(message);
-      else
+      else if (level >= INFO)
         System.out.println(message);
+      // not printing debug or trace messages
       return false;
     }
-    if (level >= WARN)
+    if (level >= ERROR)
+    {
+      Cache.log.error(message);
+    }
+    else if (level >= WARN)
     {
       Cache.log.warn(message);
     }
@@ -1424,13 +1568,22 @@ public class Cache
     {
       Cache.log.info(message);
     }
-    else
+    else if (level >= DEBUG)
     {
       Cache.log.debug(message);
     }
+    else
+    {
+      Cache.log.trace(message);
+    }
     return true;
   }
 
+  public static void trace(String message)
+  {
+    println(TRACE, message);
+  }
+
   public static void debug(String message)
   {
     println(DEBUG, message);
@@ -1450,4 +1603,4 @@ public class Cache
   {
     println(ERROR, message);
   }
-}
+}
\ No newline at end of file