src/jdk.jpackage/windows/classes/jdk/jpackage/internal/WindowsRegistry.java
author herrick
Mon, 10 Dec 2018 16:48:25 -0500
branchJDK-8200758-branch
changeset 57067 9c17d779808e
parent 57039 98d3963b0b7b
child 57106 ea870b9ce89a
permissions -rw-r--r--
8213425: Analyze output from Source code scanner and fix where needed. Reviewed-by: almatvee
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
     1
/*
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
     2
 * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
     4
 *
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    10
 *
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    15
 * accompanied this code).
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    16
 *
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    20
 *
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    23
 * questions.
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    24
 */
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    25
57039
98d3963b0b7b 8214051: rename jpackager tool to jpackage
herrick
parents: 57038
diff changeset
    26
package jdk.jpackage.internal;
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    27
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    28
import java.io.BufferedReader;
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    29
import java.io.ByteArrayInputStream;
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    30
import java.io.ByteArrayOutputStream;
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    31
import java.io.IOException;
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    32
import java.io.InputStreamReader;
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    33
import java.io.PrintStream;
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    34
import java.util.ArrayList;
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    35
import java.util.List;
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    36
57039
98d3963b0b7b 8214051: rename jpackager tool to jpackage
herrick
parents: 57038
diff changeset
    37
import static jdk.jpackage.internal.IOUtils.exec;
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    38
57067
9c17d779808e 8213425: Analyze output from Source code scanner and fix where needed.
herrick
parents: 57039
diff changeset
    39
final class WindowsRegistry {
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    40
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    41
    private WindowsRegistry() {}
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    42
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    43
    /**
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    44
     * Reads the registry value for DisableRealtimeMonitoring.
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    45
     * @return true if DisableRealtimeMonitoring is set to 0x1,
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    46
     *         false otherwise.
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    47
     */
57067
9c17d779808e 8213425: Analyze output from Source code scanner and fix where needed.
herrick
parents: 57039
diff changeset
    48
    static final boolean readDisableRealtimeMonitoring() {
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    49
        boolean result = false;
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    50
        final String key = "HKEY_LOCAL_MACHINE\\Software\\Microsoft\\"
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    51
                  + "Windows Defender\\Real-Time Protection";
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    52
        final String subkey = "DisableRealtimeMonitoring";
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    53
        String value = readRegistry(key, subkey);
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    54
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    55
        if (!value.isEmpty()) {
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    56
            // This code could be written better but this works. It validates
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    57
            // that the result of readRegistry returned what we expect and then
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    58
            // checks for a 0x0 or 0x1. 0x0 means real time monitoring is
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    59
            // on, 0x1 means it is off. So this function returns true if
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    60
            // real-time-monitoring is disabled.
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    61
            int index = value.indexOf(subkey);
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    62
            value = value.substring(index + subkey.length());
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    63
            String reg = "REG_DWORD";
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    64
            index = value.indexOf(reg);
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    65
            value = value.substring(index + reg.length());
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    66
            String hex = "0x";
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    67
            index = value.indexOf(hex);
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    68
            value = value.substring(index + hex.length());
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    69
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    70
            if (value.equals("1")) {
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    71
                result = true;
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    72
            }
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    73
        }
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    74
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    75
        return result;
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    76
    }
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    77
57067
9c17d779808e 8213425: Analyze output from Source code scanner and fix where needed.
herrick
parents: 57039
diff changeset
    78
    static final List<String> readExclusionsPaths() {
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    79
        List<String> result = new ArrayList<String>();
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    80
        final String key = "HKEY_LOCAL_MACHINE\\Software\\Microsoft\\"
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    81
                + "Windows Defender\\Exclusions\\Paths";
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    82
        String value = readRegistry(key, "");
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    83
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    84
        if (!value.isEmpty()) {
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    85
            final String reg = "REG_DWORD";
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    86
            final String hex = "0x0";
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    87
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    88
            int index = value.indexOf(key);
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    89
            if (index == 0) {
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    90
                value = value.substring(index + key.length());
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    91
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    92
                while (value.length() > 0) {
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    93
                    index = value.indexOf(reg);
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    94
                    String name = value.substring(0, index);
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    95
                    value = value.substring(index + reg.length());
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    96
                    index = value.indexOf(hex);
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    97
                    value = value.substring(index + hex.length());
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    98
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
    99
                    if (index > 0) {
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   100
                        name = name.trim();
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   101
                        result.add(name);
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   102
                    }
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   103
                }
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   104
            }
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   105
        }
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   106
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   107
        return result;
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   108
    }
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   109
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   110
    /**
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   111
     * @param key in the registry
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   112
     * @param subkey in the registry key
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   113
     * @return registry value or null if not found
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   114
     */
57067
9c17d779808e 8213425: Analyze output from Source code scanner and fix where needed.
herrick
parents: 57039
diff changeset
   115
    static final String readRegistry(String key, String subkey){
57038
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   116
        String result = "";
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   117
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   118
        try {
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   119
            List<String> buildOptions = new ArrayList<>();
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   120
            buildOptions.add("reg");
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   121
            buildOptions.add("query");
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   122
            buildOptions.add("\"" + key + "\"");
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   123
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   124
            if (!subkey.isEmpty()) {
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   125
                buildOptions.add("/v");
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   126
                buildOptions.add(subkey);
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   127
            }
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   128
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   129
            try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   130
                    PrintStream ps = new PrintStream(baos)) {
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   131
                ProcessBuilder security = new ProcessBuilder(buildOptions);
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   132
                exec(security, false, false, ps);
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   133
                BufferedReader bfReader = new BufferedReader(
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   134
                        new InputStreamReader(
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   135
                        new ByteArrayInputStream(baos.toByteArray())));
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   136
                String line = null;
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   137
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   138
                while((line = bfReader.readLine()) != null){
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   139
                    result += line;
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   140
                }
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   141
            }
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   142
            catch (IOException e) {
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   143
            }
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   144
        }
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   145
        catch (Exception e) {
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   146
        }
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   147
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   148
        return result;
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   149
    }
b0f09e7c4680 8213963: Flatten out jpackager packages and resources
herrick
parents:
diff changeset
   150
}