1 /*
   2  * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.incubator.jpackage.internal;
  27 
  28 import java.io.BufferedReader;
  29 import java.io.ByteArrayInputStream;
  30 import java.io.ByteArrayOutputStream;
  31 import java.io.IOException;
  32 import java.io.InputStreamReader;
  33 import java.io.PrintStream;
  34 import java.util.ArrayList;
  35 import java.util.List;
  36 
  37 final class WindowsRegistry {
  38 
  39     // Currently we only support HKEY_LOCAL_MACHINE. Native implementation will
  40     // require support for additinal HKEY if needed.
  41     private static final int HKEY_LOCAL_MACHINE = 1;
  42 
  43     static {
  44         System.loadLibrary("jpackage");
  45     }
  46 
  47     private WindowsRegistry() {}
  48 
  49     /**
  50      * Reads the registry value for DisableRealtimeMonitoring.
  51      * @return true if DisableRealtimeMonitoring is set to 0x1,
  52      *         false otherwise.
  53      */
  54     static final boolean readDisableRealtimeMonitoring() {
  55         final String subKey = "Software\\Microsoft\\"
  56                   + "Windows Defender\\Real-Time Protection";
  57         final String value = "DisableRealtimeMonitoring";
  58         int result = readDwordValue(HKEY_LOCAL_MACHINE, subKey, value, 0);
  59         return (result == 1);
  60     }
  61 
  62     static final List<String> readExclusionsPaths() {
  63         List<String> result = new ArrayList<>();
  64         final String subKey = "Software\\Microsoft\\"
  65                 + "Windows Defender\\Exclusions\\Paths";
  66         long lKey = openRegistryKey(HKEY_LOCAL_MACHINE, subKey);
  67         if (lKey == 0) {
  68             return result;
  69         }
  70 
  71         String valueName;
  72         int index = 0;
  73         do {
  74             valueName = enumRegistryValue(lKey, index);
  75             if (valueName != null) {
  76                 result.add(valueName);
  77                 index++;
  78             }
  79         } while (valueName != null);
  80 
  81         closeRegistryKey(lKey);
  82 
  83         return result;
  84     }
  85 
  86     /**
  87      * Reads DWORD registry value.
  88      *
  89      * @param key one of HKEY predefine value
  90      * @param subKey registry sub key
  91      * @param value value to read
  92      * @param defaultValue default value in case if subKey or value not found
  93      *                     or any other errors occurred
  94      * @return value's data only if it was read successfully, otherwise
  95      *         defaultValue
  96      */
  97     private static native int readDwordValue(int key, String subKey,
  98             String value, int defaultValue);
  99 
 100     /**
 101      * Open registry key.
 102      *
 103      * @param key one of HKEY predefine value
 104      * @param subKey registry sub key
 105      * @return native handle to open key
 106      */
 107     private static native long openRegistryKey(int key, String subKey);
 108 
 109     /**
 110      * Enumerates the values for registry key.
 111      *
 112      * @param lKey native handle to open key returned by openRegistryKey
 113      * @param index index of value starting from 0. Increment until this
 114      *              function returns NULL which means no more values.
 115      * @return returns value or NULL if error or no more data
 116      */
 117     private static native String enumRegistryValue(long lKey, int index);
 118 
 119     /**
 120      * Close registry key.
 121      *
 122      * @param lKey native handle to open key returned by openRegistryKey
 123      */
 124     private static native void closeRegistryKey(long lKey);
 125 
 126     /**
 127      * Compares two Windows paths regardless case and if paths
 128      * are short or long.
 129      *
 130      * @param path1 path to compare
 131      * @param path2 path to compare
 132      * @return true if paths point to same location
 133      */
 134     public static native boolean comparePaths(String path1, String path2);
 135 }