jdk/src/share/classes/com/sun/security/auth/login/ConfigFile.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 7970 af1579474d16
child 14775 2ed01c760aea
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7970
diff changeset
     2
 * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.security.auth.login;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.security.auth.AuthPermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.security.auth.login.AppConfigurationEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.URI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.text.MessageFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.util.Debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.util.ResourcesMgr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.security.util.PropertyExpander;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * This class represents a default implementation for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <code>javax.security.auth.login.Configuration</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <p> This object stores the runtime login configuration representation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * and is the amalgamation of multiple static login
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * configurations that resides in files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * The algorithm for locating the login configuration file(s) and reading their
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * information into this <code>Configuration</code> object is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *   Loop through the <code>java.security.Security</code> properties,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *   <i>login.config.url.1</i>, <i>login.config.url.2</i>, ...,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *   <i>login.config.url.X</i>.  These properties are set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *   in the Java security properties file, which is located in the file named
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *   &lt;JAVA_HOME&gt;/lib/security/java.security.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *   &lt;JAVA_HOME&gt; refers to the value of the java.home system property,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *   and specifies the directory where the JRE is installed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *   Each property value specifies a <code>URL</code> pointing to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *   login configuration file to be loaded.  Read in and load
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *   each configuration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *   The <code>java.lang.System</code> property
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *   <i>java.security.auth.login.config</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *   may also be set to a <code>URL</code> pointing to another
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *   login configuration file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *   (which is the case when a user uses the -D switch at runtime).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *   If this property is defined, and its use is allowed by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *   security property file (the Security property,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *   <i>policy.allowSystemProperty</i> is set to <i>true</i>),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *   also load that login configuration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *   If the <i>java.security.auth.login.config</i> property is defined using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *   "==" (rather than "="), then ignore all other specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *   login configurations and only load this configuration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *   If no system or security properties were set, try to read from the file,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *   ${user.home}/.java.login.config, where ${user.home} is the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *   represented by the "user.home" System property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * <p> The configuration syntax supported by this implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * is exactly that syntax specified in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <code>javax.security.auth.login.Configuration</code> class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * @see javax.security.auth.login.LoginContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
public class ConfigFile extends javax.security.auth.login.Configuration {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private StreamTokenizer st;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private int lookahead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private int linenum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private HashMap<String, LinkedList<AppConfigurationEntry>> configuration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private boolean expandProp = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private URL url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private static Debug debugConfig = Debug.getInstance("configfile");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private static Debug debugParser = Debug.getInstance("configparser");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Create a new <code>Configuration</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public ConfigFile() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            init(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            throw (SecurityException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                new SecurityException(ioe.getMessage()).initCause(ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Create a new <code>Configuration</code> object from the specified URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @param uri Create a new Configuration object from this URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public ConfigFile(URI uri) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        // only load config from the specified URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            url = uri.toURL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            init(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        } catch (MalformedURLException mue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            throw (SecurityException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                new SecurityException(mue.getMessage()).initCause(mue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            throw (SecurityException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                new SecurityException(ioe.getMessage()).initCause(ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * Read and initialize the entire login Configuration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @exception IOException if the Configuration can not be initialized. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @exception SecurityException if the caller does not have permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *                          to initialize the Configuration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private void init(URL url) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        boolean initialized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        FileReader fr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        String sep = File.separator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if ("false".equals(System.getProperty("policy.expandProperties"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            expandProp = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        // new configuration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        HashMap<String, LinkedList<AppConfigurationEntry>> newConfig =
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 7179
diff changeset
   155
                new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if (url != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
             * If the caller specified a URI via Configuration.getInstance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
             * we only read from that URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            if (debugConfig != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                debugConfig.println("reading " + url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            init(url, newConfig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            configuration = newConfig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
         * Caller did not specify URI via Configuration.getInstance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
         * Read from URLs listed in the java.security properties file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        String allowSys = java.security.Security.getProperty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                                                ("policy.allowSystemProperty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if ("true".equalsIgnoreCase(allowSys)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            String extra_config = System.getProperty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                                        ("java.security.auth.login.config");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            if (extra_config != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                boolean overrideAll = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                if (extra_config.startsWith("=")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                    overrideAll = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                    extra_config = extra_config.substring(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    extra_config = PropertyExpander.expand(extra_config);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                } catch (PropertyExpander.ExpandException peee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    MessageFormat form = new MessageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                        (ResourcesMgr.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   193
                                ("Unable.to.properly.expand.config",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                                "sun.security.util.AuthResources"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    Object[] source = {extra_config};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                    throw new IOException(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                URL configURL = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    configURL = new URL(extra_config);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                } catch (java.net.MalformedURLException mue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                    File configFile = new File(extra_config);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    if (configFile.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                        configURL = configFile.toURI().toURL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                        MessageFormat form = new MessageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                            (ResourcesMgr.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   209
                                ("extra.config.No.such.file.or.directory.",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                                "sun.security.util.AuthResources"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                        Object[] source = {extra_config};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                        throw new IOException(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                if (debugConfig != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                    debugConfig.println("reading "+configURL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                init(configURL, newConfig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                initialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                if (overrideAll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    if (debugConfig != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                        debugConfig.println("overriding other policies!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    configuration = newConfig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        int n = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        String config_url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        while ((config_url = java.security.Security.getProperty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                                        ("login.config.url."+n)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                config_url = PropertyExpander.expand
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                        (config_url).replace(File.separatorChar, '/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                if (debugConfig != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                    debugConfig.println("\tReading config: " + config_url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                init(new URL(config_url), newConfig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                initialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            } catch (PropertyExpander.ExpandException peee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                MessageFormat form = new MessageFormat
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                        (ResourcesMgr.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   246
                                ("Unable.to.properly.expand.config",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                                "sun.security.util.AuthResources"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                Object[] source = {config_url};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                throw new IOException(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            n++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        if (initialized == false && n == 1 && config_url == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            // get the config from the user's home directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            if (debugConfig != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                debugConfig.println("\tReading Policy " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                                "from ~/.java.login.config");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            config_url = System.getProperty("user.home");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            String userConfigFile = config_url +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                      File.separatorChar + ".java.login.config";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            // No longer throws an exception when there's no config file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            // at all. Returns an empty Configuration instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            if (new File(userConfigFile).exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                init(new File(userConfigFile).toURI().toURL(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    newConfig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        configuration = newConfig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    private void init(URL config,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        HashMap<String, LinkedList<AppConfigurationEntry>> newConfig)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        InputStreamReader isr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            isr = new InputStreamReader(getInputStream(config), "UTF-8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            readConfig(isr, newConfig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        } catch (FileNotFoundException fnfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            if (debugConfig != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                debugConfig.println(fnfe.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            throw new IOException(ResourcesMgr.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   289
                    ("Configuration.Error.No.such.file.or.directory",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    "sun.security.util.AuthResources"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            if (isr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                isr.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * Retrieve an entry from the Configuration using an application name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * as an index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @param applicationName the name used to index the Configuration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @return an array of AppConfigurationEntries which correspond to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *          the stacked configuration of LoginModules for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *          application, or null if this application has no configured
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *          LoginModules.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    public AppConfigurationEntry[] getAppConfigurationEntry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    (String applicationName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        LinkedList<AppConfigurationEntry> list = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        synchronized (configuration) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            list = configuration.get(applicationName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        if (list == null || list.size() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        AppConfigurationEntry[] entries =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                                new AppConfigurationEntry[list.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        Iterator<AppConfigurationEntry> iterator = list.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        for (int i = 0; iterator.hasNext(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            AppConfigurationEntry e = iterator.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            entries[i] = new AppConfigurationEntry(e.getLoginModuleName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                                                e.getControlFlag(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                                                e.getOptions());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        return entries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * Refresh and reload the Configuration by re-reading all of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * login configurations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @exception SecurityException if the caller does not have permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *                          to refresh the Configuration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    public synchronized void refresh() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        java.lang.SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        if (sm != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            sm.checkPermission(new AuthPermission("refreshLoginConfiguration"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            (new java.security.PrivilegedAction<Void>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            public Void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    init(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                } catch (java.io.IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    throw (SecurityException) new SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                                (ioe.getLocalizedMessage()).initCause(ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    private void readConfig(Reader reader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        HashMap<String, LinkedList<AppConfigurationEntry>> newConfig)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        int linenum = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        if (!(reader instanceof BufferedReader))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            reader = new BufferedReader(reader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        st = new StreamTokenizer(reader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        st.quoteChar('"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        st.wordChars('$', '$');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        st.wordChars('_', '_');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        st.wordChars('-', '-');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        st.lowerCaseMode(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        st.slashSlashComments(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        st.slashStarComments(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        st.eolIsSignificant(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        lookahead = nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        while (lookahead != StreamTokenizer.TT_EOF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            parseLoginEntry(newConfig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    private void parseLoginEntry(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        HashMap<String, LinkedList<AppConfigurationEntry>> newConfig)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        String appName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        String moduleClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        String sflag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        AppConfigurationEntry.LoginModuleControlFlag controlFlag;
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 7179
diff changeset
   395
        LinkedList<AppConfigurationEntry> configEntries = new LinkedList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        // application name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        appName = st.sval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        lookahead = nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        if (debugParser != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            debugParser.println("\tReading next config entry: " + appName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        match("{");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        // get the modules
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        while (peek("}") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            // get the module class name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            moduleClass = match("module class name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            // controlFlag (required, optional, etc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            sflag = match("controlFlag");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            if (sflag.equalsIgnoreCase("REQUIRED"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                controlFlag =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                        AppConfigurationEntry.LoginModuleControlFlag.REQUIRED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            else if (sflag.equalsIgnoreCase("REQUISITE"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                controlFlag =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                        AppConfigurationEntry.LoginModuleControlFlag.REQUISITE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            else if (sflag.equalsIgnoreCase("SUFFICIENT"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                controlFlag =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                        AppConfigurationEntry.LoginModuleControlFlag.SUFFICIENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            else if (sflag.equalsIgnoreCase("OPTIONAL"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                controlFlag =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                        AppConfigurationEntry.LoginModuleControlFlag.OPTIONAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                MessageFormat form = new MessageFormat(ResourcesMgr.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   428
                        ("Configuration.Error.Invalid.control.flag.flag",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                        "sun.security.util.AuthResources"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                Object[] source = {sflag};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                throw new IOException(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            // get the args
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 7179
diff changeset
   435
            HashMap<String, String> options = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            String key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            String value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            while (peek(";") == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                key = match("option key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                match("=");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    value = expand(match("option value"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                } catch (PropertyExpander.ExpandException peee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                    throw new IOException(peee.getLocalizedMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                options.put(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            lookahead = nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            // create the new element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            if (debugParser != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                debugParser.println("\t\t" + moduleClass + ", " + sflag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                java.util.Iterator<String> i = options.keySet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                    key = i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                    debugParser.println("\t\t\t" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                                        key +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                                        "=" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                                        options.get(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            AppConfigurationEntry entry = new AppConfigurationEntry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                                                        (moduleClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                                                        controlFlag,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                                                        options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            configEntries.add(entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        match("}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        match(";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        // add this configuration entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        if (newConfig.containsKey(appName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            MessageFormat form = new MessageFormat(ResourcesMgr.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   476
                ("Configuration.Error.Can.not.specify.multiple.entries.for.appName",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                "sun.security.util.AuthResources"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            Object[] source = {appName};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            throw new IOException(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        newConfig.put(appName, configEntries);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    private String match(String expect) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        String value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        switch(lookahead) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        case StreamTokenizer.TT_EOF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            MessageFormat form1 = new MessageFormat(ResourcesMgr.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   492
                ("Configuration.Error.expected.expect.read.end.of.file.",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                "sun.security.util.AuthResources"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            Object[] source1 = {expect};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            throw new IOException(form1.format(source1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        case '"':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        case StreamTokenizer.TT_WORD:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            if (expect.equalsIgnoreCase("module class name") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                expect.equalsIgnoreCase("controlFlag") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                expect.equalsIgnoreCase("option key") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                expect.equalsIgnoreCase("option value")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                value = st.sval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                lookahead = nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                MessageFormat form = new MessageFormat(ResourcesMgr.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   508
                        ("Configuration.Error.Line.line.expected.expect.found.value.",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                        "sun.security.util.AuthResources"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                Object[] source = {new Integer(linenum), expect, st.sval};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                throw new IOException(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        case '{':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            if (expect.equalsIgnoreCase("{")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                lookahead = nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                MessageFormat form = new MessageFormat(ResourcesMgr.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   521
                        ("Configuration.Error.Line.line.expected.expect.",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                        "sun.security.util.AuthResources"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                Object[] source = {new Integer(linenum), expect, st.sval};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                throw new IOException(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        case ';':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            if (expect.equalsIgnoreCase(";")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                lookahead = nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                MessageFormat form = new MessageFormat(ResourcesMgr.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   534
                        ("Configuration.Error.Line.line.expected.expect.",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                        "sun.security.util.AuthResources"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                Object[] source = {new Integer(linenum), expect, st.sval};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                throw new IOException(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        case '}':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            if (expect.equalsIgnoreCase("}")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                lookahead = nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                MessageFormat form = new MessageFormat(ResourcesMgr.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   547
                        ("Configuration.Error.Line.line.expected.expect.",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                        "sun.security.util.AuthResources"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                Object[] source = {new Integer(linenum), expect, st.sval};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                throw new IOException(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        case '=':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            if (expect.equalsIgnoreCase("=")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                lookahead = nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                MessageFormat form = new MessageFormat(ResourcesMgr.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   560
                        ("Configuration.Error.Line.line.expected.expect.",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                        "sun.security.util.AuthResources"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                Object[] source = {new Integer(linenum), expect, st.sval};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                throw new IOException(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            MessageFormat form = new MessageFormat(ResourcesMgr.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   569
                        ("Configuration.Error.Line.line.expected.expect.found.value.",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                        "sun.security.util.AuthResources"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            Object[] source = {new Integer(linenum), expect, st.sval};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            throw new IOException(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    private boolean peek(String expect) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        boolean found = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        switch (lookahead) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        case ',':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            if (expect.equalsIgnoreCase(","))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        case ';':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            if (expect.equalsIgnoreCase(";"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        case '{':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            if (expect.equalsIgnoreCase("{"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        case '}':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            if (expect.equalsIgnoreCase("}"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        return found;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    private int nextToken() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        int tok;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        while ((tok = st.nextToken()) == StreamTokenizer.TT_EOL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            linenum++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        return tok;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * Fast path reading from file urls in order to avoid calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * FileURLConnection.connect() which can be quite slow the first time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * it is called. We really should clean up FileURLConnection so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * this is not a problem but in the meantime this fix helps reduce
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * start up time noticeably for the new launcher. -- DAC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    private InputStream getInputStream(URL url) throws IOException {
483
d6cea43f48b6 6689000: Changes in 6675606 causing regression test failures on windows-i586
weijun
parents: 472
diff changeset
   618
        if ("file".equalsIgnoreCase(url.getProtocol())) {
d6cea43f48b6 6689000: Changes in 6675606 causing regression test failures on windows-i586
weijun
parents: 472
diff changeset
   619
            // Compatibility notes:
d6cea43f48b6 6689000: Changes in 6675606 causing regression test failures on windows-i586
weijun
parents: 472
diff changeset
   620
            //
d6cea43f48b6 6689000: Changes in 6675606 causing regression test failures on windows-i586
weijun
parents: 472
diff changeset
   621
            // Code changed from
d6cea43f48b6 6689000: Changes in 6675606 causing regression test failures on windows-i586
weijun
parents: 472
diff changeset
   622
            //   String path = url.getFile().replace('/', File.separatorChar);
d6cea43f48b6 6689000: Changes in 6675606 causing regression test failures on windows-i586
weijun
parents: 472
diff changeset
   623
            //   return new FileInputStream(path);
d6cea43f48b6 6689000: Changes in 6675606 causing regression test failures on windows-i586
weijun
parents: 472
diff changeset
   624
            //
d6cea43f48b6 6689000: Changes in 6675606 causing regression test failures on windows-i586
weijun
parents: 472
diff changeset
   625
            // The original implementation would search for "/tmp/a%20b"
d6cea43f48b6 6689000: Changes in 6675606 causing regression test failures on windows-i586
weijun
parents: 472
diff changeset
   626
            // when url is "file:///tmp/a%20b". This is incorrect. The
d6cea43f48b6 6689000: Changes in 6675606 causing regression test failures on windows-i586
weijun
parents: 472
diff changeset
   627
            // current codes fix this bug and searches for "/tmp/a b".
d6cea43f48b6 6689000: Changes in 6675606 causing regression test failures on windows-i586
weijun
parents: 472
diff changeset
   628
            // For compatibility reasons, when the file "/tmp/a b" does
d6cea43f48b6 6689000: Changes in 6675606 causing regression test failures on windows-i586
weijun
parents: 472
diff changeset
   629
            // not exist, the file named "/tmp/a%20b" will be tried.
d6cea43f48b6 6689000: Changes in 6675606 causing regression test failures on windows-i586
weijun
parents: 472
diff changeset
   630
            //
d6cea43f48b6 6689000: Changes in 6675606 causing regression test failures on windows-i586
weijun
parents: 472
diff changeset
   631
            // This also means that if both file exists, the behavior of
d6cea43f48b6 6689000: Changes in 6675606 causing regression test failures on windows-i586
weijun
parents: 472
diff changeset
   632
            // this method is changed, and the current codes choose the
d6cea43f48b6 6689000: Changes in 6675606 causing regression test failures on windows-i586
weijun
parents: 472
diff changeset
   633
            // correct one.
472
36e5cdcf9300 6675606: javax.security.auth.login.Configuration does not recognize path with spaces
weijun
parents: 2
diff changeset
   634
            try {
483
d6cea43f48b6 6689000: Changes in 6675606 causing regression test failures on windows-i586
weijun
parents: 472
diff changeset
   635
                return url.openStream();
d6cea43f48b6 6689000: Changes in 6675606 causing regression test failures on windows-i586
weijun
parents: 472
diff changeset
   636
            } catch (Exception e) {
d6cea43f48b6 6689000: Changes in 6675606 causing regression test failures on windows-i586
weijun
parents: 472
diff changeset
   637
                String file = url.getPath();
d6cea43f48b6 6689000: Changes in 6675606 causing regression test failures on windows-i586
weijun
parents: 472
diff changeset
   638
                if (url.getHost().length() > 0) {  // For Windows UNC
d6cea43f48b6 6689000: Changes in 6675606 causing regression test failures on windows-i586
weijun
parents: 472
diff changeset
   639
                    file = "//" + url.getHost() + file;
d6cea43f48b6 6689000: Changes in 6675606 causing regression test failures on windows-i586
weijun
parents: 472
diff changeset
   640
                }
d6cea43f48b6 6689000: Changes in 6675606 causing regression test failures on windows-i586
weijun
parents: 472
diff changeset
   641
                if (debugConfig != null) {
d6cea43f48b6 6689000: Changes in 6675606 causing regression test failures on windows-i586
weijun
parents: 472
diff changeset
   642
                    debugConfig.println("cannot read " + url +
d6cea43f48b6 6689000: Changes in 6675606 causing regression test failures on windows-i586
weijun
parents: 472
diff changeset
   643
                            ", try " + file);
d6cea43f48b6 6689000: Changes in 6675606 causing regression test failures on windows-i586
weijun
parents: 472
diff changeset
   644
                }
d6cea43f48b6 6689000: Changes in 6675606 causing regression test failures on windows-i586
weijun
parents: 472
diff changeset
   645
                return new FileInputStream(file);
472
36e5cdcf9300 6675606: javax.security.auth.login.Configuration does not recognize path with spaces
weijun
parents: 2
diff changeset
   646
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            return url.openStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    private String expand(String value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        throws PropertyExpander.ExpandException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        if ("".equals(value)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        if (expandProp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            String s = PropertyExpander.expand(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            if (s == null || s.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                MessageFormat form = new MessageFormat(ResourcesMgr.getString
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   665
                        ("Configuration.Error.Line.line.system.property.value.expanded.to.empty.value",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                        "sun.security.util.AuthResources"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                Object[] source = {new Integer(linenum), value};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                throw new IOException(form.format(source));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
}