All Downloads are FREE. Search and download functionalities are using the official Maven repository.

config.com.unbound.common.Config Maven / Gradle / Ivy

Go to download

This is a collection of JAVA libraries that implement Unbound cryptographic classes for JAVA provider, PKCS11 wrapper, cryptoki, and advapi

There is a newer version: 42761
Show newest version
package com.unbound.common;

import java.io.File;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.concurrent.ConcurrentHashMap;
import java.util.prefs.Preferences;

public class Config
{
  public static boolean isWindows = System.getProperty("os.name").startsWith("Windows");

  public static final int javaVersion;

  static
  {
    double d = Double.parseDouble(System.getProperty("java.specification.version"));
    if (d>1) javaVersion = (int)d;
    else javaVersion = (int)((d-1)*10);
  }

  public static boolean getBool(String part, String name, boolean defaultValue)
  {
    String s = getStr(part, name);
    if (s==null || s.isEmpty()) return defaultValue;
    return
         0 == s.compareTo("1")
      || 0==s.compareToIgnoreCase("true")
      || 0==s.compareToIgnoreCase("yes")
      || 0==s.compareToIgnoreCase("on");
  }

  public static boolean getBool(String part, String name)
  {
    return getBool(part, name, false);
  }

  public static int getInt(String part, String name, int defaultValue)
  {
    String s = getStr(part, name);
    if (s==null || s.isEmpty()) return defaultValue;
    try { return Integer.parseInt(s); }
    catch (Exception e) { return defaultValue; }
  }

  public static int getInt(String part, String name)
  {
    return getInt(part, name, 0);
  }

  public static String getStr(String part, String name, String defaultValue)
  {
    ConcurrentHashMap map = getMap(part);
    String s = map.get(name);
    if (s==null)
    {
      if (isWindows)
      {
        s = WindowsRegistryHandle.readStr(part, name);
      }
      else
      {
        readConfigUnix(part, map);
        s = map.get(name);
      }
      if (s==null) s = defaultValue;
      map.put(name, s);
    }
    return s;
  }

  public static String getStr(String part, String name)
  {
    return getStr(part, name, "");
  }

  private static ConcurrentHashMap log = new ConcurrentHashMap<>();
  private static ConcurrentHashMap ekm = new ConcurrentHashMap<>();
  private static ConcurrentHashMap sdk = new ConcurrentHashMap<>();
  private static ConcurrentHashMap ekp = new ConcurrentHashMap<>();

  private static ConcurrentHashMap getMap(String part)
  {
    if (0==part.compareToIgnoreCase("log")) return log;
    if (0==part.compareToIgnoreCase("ekm")) return ekm;
    if (0==part.compareToIgnoreCase("sdk")) return sdk;
    if (0==part.compareToIgnoreCase("ekp")) return ekp;
    throw new IllegalArgumentException("Unsupported configuration " + part);
  }

  private static void readConfigUnix(String part, ConcurrentHashMap map)
  {
    String path = "/etc/ekm/client.conf";
    if (0==part.compareToIgnoreCase("log")) path = "/etc/dylog.conf";
    if (0==part.compareToIgnoreCase("ekp")) path = "/etc/ekm/client.conf";

    Map temp = readFile(path);
    map.putAll(temp);
  }

  public static Map readFile(String path)
  {
    HashMap map = new HashMap<>();

    try
    {
      Scanner input = new Scanner(new File(path));
      while (input.hasNextLine())
      {
        String line = input.nextLine();
        if (line.isEmpty() || line.charAt(0) < 'A') continue;

        int equ = line.indexOf('=');
        if (equ < 0) equ = line.indexOf(':');
        if (equ < 0) continue;

        String name = line.substring(0, equ).trim();
        String value = line.substring(equ + 1).trim();
        if (!name.isEmpty() && !value.isEmpty()) map.put(name, value);
      }
      input.close();
    }
    catch (Exception e) {}

    return map;
  }



  private static final class WindowsRegistryHandle
  {
    private static final int HKEY_CURRENT_USER = 0x80000001;
    private static final int HKEY_LOCAL_MACHINE = 0x80000002;
    private static final int REG_SUCCESS = 0;
    private static final int KEY_READ = 0x20019;

    private static Preferences userRoot = Preferences.userRoot();
    private static Method regOpenKey = null;
    private static Method regCloseKey = null;
    private static Method regQueryValueEx = null;

    static
    {
      try
      {
        Class userClass = userRoot.getClass();
        regOpenKey = userClass.getDeclaredMethod("WindowsRegOpenKey", int.class, byte[].class, int.class);
        regOpenKey.setAccessible(true);
        regCloseKey = userClass.getDeclaredMethod("WindowsRegCloseKey", int.class);
        regCloseKey.setAccessible(true);
        regQueryValueEx = userClass.getDeclaredMethod("WindowsRegQueryValueEx", int.class, byte[].class);
        regQueryValueEx.setAccessible(true);
      }
      catch (NoSuchMethodException e)
      {
      }
    }

    private static byte[] toCstr(String str)
    {
      byte[] result = new byte[str.length() + 1];

      for (int i = 0; i < str.length(); i++)
      {
        result[i] = (byte) str.charAt(i);
      }
      result[str.length()] = 0;
      return result;
    }

    private static Integer open(int hkey, String key)
    {
      int[] handles = null;
      try { handles = (int[]) regOpenKey.invoke(userRoot, new Object[] {hkey, toCstr(key), KEY_READ}); }
      catch (Exception e) { }
      if (handles!=null && handles[1] == REG_SUCCESS) return handles[0];
      return null;
    }

    private static void close(Integer key)
    {
      try { regCloseKey.invoke(userRoot, key); }
      catch (Exception e) { }
    }

    private static String read(Integer key, String name)
    {
      try
      {
        byte[] valb = (byte[]) regQueryValueEx.invoke(userRoot, new Object[] {key, toCstr(name)});
        if (valb==null || valb.length==0) return null;
        return new String(valb, StandardCharsets.UTF_8).trim();
      }
      catch (Exception e) { return null; }
    }

    static String readStr(String part, String name)
    {
      String path = "SOFTWARE\\DyadicSec\\" + part;
      Integer user = open(HKEY_CURRENT_USER, path);
      if (user!=null)
      {
        String value = read(user, name);
        close(user);
        if (value!=null) return value;
      }

      Integer machine = open(HKEY_LOCAL_MACHINE, path);
      if (machine==null) return null;
      String value = read(machine, name);
      close(machine);

      return value;
    }
  }

  public static boolean getEnvBool(String name)
  {
    return "1".equals(System.getenv(name));
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy