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.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 import static jdk.jpackage.internal.IOUtils.exec;
  38 
  39 final class WindowsRegistry {
  40 
  41     // Currently we only support HKEY_LOCAL_MACHINE. Native implementation will
  42     // require support for additinal HKEY if needed.
  43     private static final int HKEY_LOCAL_MACHINE = 1;
  44 
  45     static {
  46         System.loadLibrary("jpackage");
  47     }
  48 
  49     private WindowsRegistry() {}
  50 
  51     /**
  52      * Reads the registry value for DisableRealtimeMonitoring.
  53      * @return true if DisableRealtimeMonitoring is set to 0x1,
  54      *         false otherwise.
  55      */
  56     static final boolean readDisableRealtimeMonitoring() {
  57         final String subKey = "Software\\Microsoft\\"
  58                   + "Windows Defender\\Real-Time Protection";
  59         final String value = "DisableRealtimeMonitoring";
  60         int result = readDwordValue(HKEY_LOCAL_MACHINE, subKey, value, 0);
  61         return (result == 1);
  62     }
  63 
  64     static final List<String> readExclusionsPaths() {
  65         List<String> result = new ArrayList<>();
  66         final String subKey = "Software\\Microsoft\\"
  67                 + "Windows Defender\\Exclusions\\Paths";
  68         long lKey = openRegistryKey(HKEY_LOCAL_MACHINE, subKey);
  69         if (lKey == 0) {
  70             return result;
  71         }
  72 
  73         String valueName;
  74         int index = 0;
  75         do {
  76             valueName = enumRegistryValue(lKey, index);
  77             if (valueName != null) {
  78                 result.add(valueName);
  79                 index++;
  80             }
  81         } while (valueName != null);
  82 
  83         closeRegistryKey(lKey);
  84 
  85         return result;
  86     }
  87 
  88     /**
  89      * Reads DWORD registry value.
  90      *
  91      * @param key one of HKEY predefine value
  92      * @param subKey registry sub key
  93      * @param value value to read
  94      * @param defaultValue default value in case if subKey or value not found
  95      *                     or any other errors occurred
  96      * @return value's data only if it was read successfully, otherwise
  97      *         defaultValue
  98      */
  99     private static native int readDwordValue(int key, String subKey,
 100             String value, int defaultValue);
 101 
 102     /**
 103      * Open registry key.
 104      *
 105      * @param key one of HKEY predefine value
 106      * @param subKey registry sub key
 107      * @return native handle to open key
 108      */
 109     private static native long openRegistryKey(int key, String subKey);
 110 
 111     /**
 112      * Enumerates the values for registry key.
 113      *
 114      * @param lKey native handle to open key returned by openRegistryKey
 115      * @param index index of value starting from 0. Increment until this
 116      *              function returns NULL which means no more values.
 117      * @return returns value or NULL if error or no more data
 118      */
 119     private static native String enumRegistryValue(long lKey, int index);
 120 
 121     /**
 122      * Close registry key.
 123      *
 124      * @param lKey native handle to open key returned by openRegistryKey
 125      */
 126     private static native void closeRegistryKey(long lKey);
 127 
 128     /**
 129      * Compares two Windows paths regardless case and if paths are short or long.
 130      *
 131      * @param path1 path to compare
 132      * @param path2 path to compare
 133      * @return true if paths point to same location
 134      */
 135     public static native boolean comparePaths(String path1, String path2);
 136 }