jdk/src/share/classes/com/sun/jmx/remote/security/MBeanServerFileAccessController.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 526 61ba2d5ea9da
child 1510 e747d3193ef2
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 526
diff changeset
     2
 * Copyright 2003-2008 Sun Microsystems, Inc.  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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.jmx.remote.security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.FileInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.AccessControlContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Collection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.security.auth.Subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p>An object of this class implements the MBeanServerAccessController
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * interface and, for each of its methods, calls an appropriate checking
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * method and then forwards the request to a wrapped MBeanServer object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * The checking method may throw a SecurityException if the operation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * not allowed; in this case the request is not forwarded to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * wrapped object.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <p>This class implements the {@link #checkRead()} and {@link #checkWrite()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * methods based on an access level properties file containing username/access
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * level pairs. The set of username/access level pairs is passed either as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * filename which denotes a properties file on disk, or directly as an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * of the {@link Properties} class.  In both cases, the name of each property
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * represents a username, and the value of the property is the associated access
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * level.  Thus, any given username either does not exist in the properties or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * has exactly one access level. The same access level can be shared by several
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * usernames.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <p>The supported access level values are <i>readonly</i> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <i>readwrite</i>.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
public class MBeanServerFileAccessController
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    extends MBeanServerAccessController {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public static final String READONLY = "readonly";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public static final String READWRITE = "readwrite";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * <p>Create a new MBeanServerAccessController that forwards all the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * MBeanServer requests to the MBeanServer set by invoking the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * #setMBeanServer} method after doing access checks based on read and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * write permissions.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * <p>This instance is initialized from the specified properties file.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @param accessFileName name of the file which denotes a properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * file on disk containing the username/access level entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @exception IOException if the file does not exist, is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * directory rather than a regular file, or for some other
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * reason cannot be opened for reading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @exception IllegalArgumentException if any of the supplied access
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * level values differs from "readonly" or "readwrite".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public MBeanServerFileAccessController(String accessFileName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        this.accessFileName = accessFileName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        props = propertiesFromFile(accessFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        checkValues(props);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * <p>Create a new MBeanServerAccessController that forwards all the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * MBeanServer requests to <code>mbs</code> after doing access checks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * based on read and write permissions.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * <p>This instance is initialized from the specified properties file.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @param accessFileName name of the file which denotes a properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * file on disk containing the username/access level entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @param mbs the MBeanServer object to which requests will be forwarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @exception IOException if the file does not exist, is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * directory rather than a regular file, or for some other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * reason cannot be opened for reading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @exception IllegalArgumentException if any of the supplied access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * level values differs from "readonly" or "readwrite".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public MBeanServerFileAccessController(String accessFileName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                                           MBeanServer mbs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        this(accessFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        setMBeanServer(mbs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * <p>Create a new MBeanServerAccessController that forwards all the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * MBeanServer requests to the MBeanServer set by invoking the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * #setMBeanServer} method after doing access checks based on read and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * write permissions.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * <p>This instance is initialized from the specified properties instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * This constructor makes a copy of the properties instance using its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * <code>clone</code> method and it is the copy that is consulted to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * the username and access level of an incoming connection. The original
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * properties object can be modified without affecting the copy. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * {@link #refresh} method is then called, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * <code>MBeanServerFileAccessController</code> will make a new copy of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * properties object at that time.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param accessFileProps properties list containing the username/access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * level entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @exception IllegalArgumentException if <code>accessFileProps</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * <code>null</code> or if any of the supplied access level values differs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * from "readonly" or "readwrite".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public MBeanServerFileAccessController(Properties accessFileProps)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (accessFileProps == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            throw new IllegalArgumentException("Null properties");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        originalProps = accessFileProps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        props = (Properties) accessFileProps.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        checkValues(props);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * <p>Create a new MBeanServerAccessController that forwards all the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * MBeanServer requests to the MBeanServer set by invoking the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * #setMBeanServer} method after doing access checks based on read and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * write permissions.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * <p>This instance is initialized from the specified properties instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * This constructor makes a copy of the properties instance using its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * <code>clone</code> method and it is the copy that is consulted to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * the username and access level of an incoming connection. The original
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * properties object can be modified without affecting the copy. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * {@link #refresh} method is then called, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * <code>MBeanServerFileAccessController</code> will make a new copy of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * properties object at that time.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @param accessFileProps properties list containing the username/access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * level entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @param mbs the MBeanServer object to which requests will be forwarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @exception IllegalArgumentException if <code>accessFileProps</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * <code>null</code> or if any of the supplied access level values differs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * from "readonly" or "readwrite".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public MBeanServerFileAccessController(Properties accessFileProps,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                                           MBeanServer mbs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        this(accessFileProps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        setMBeanServer(mbs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Check if the caller can do read operations. This method does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * nothing if so, otherwise throws SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public void checkRead() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        checkAccessLevel(READONLY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Check if the caller can do write operations.  This method does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * nothing if so, otherwise throws SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    public void checkWrite() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        checkAccessLevel(READWRITE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * <p>Refresh the set of username/access level entries.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * <p>If this instance was created using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * {@link #MBeanServerFileAccessController(String)} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * {@link #MBeanServerFileAccessController(String,MBeanServer)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * constructors to specify a file from which the entries are read,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * the file is re-read.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * <p>If this instance was created using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * {@link #MBeanServerFileAccessController(Properties)} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * {@link #MBeanServerFileAccessController(Properties,MBeanServer)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * constructors then a new copy of the <code>Properties</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * is made.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @exception IOException if the file does not exist, is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * directory rather than a regular file, or for some other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * reason cannot be opened for reading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @exception IllegalArgumentException if any of the supplied access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * level values differs from "readonly" or "readwrite".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    public void refresh() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        synchronized (props) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            if (accessFileName == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                props = (Properties) originalProps.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                props = propertiesFromFile(accessFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            checkValues(props);
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
    private static Properties propertiesFromFile(String fname)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        FileInputStream fin = new FileInputStream(fname);
526
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 2
diff changeset
   234
        try {
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 2
diff changeset
   235
            Properties p = new Properties();
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 2
diff changeset
   236
            p.load(fin);
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 2
diff changeset
   237
            return p;
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 2
diff changeset
   238
        } finally {
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 2
diff changeset
   239
            fin.close();
61ba2d5ea9da 6701459: Synchronization bug pattern found in javax.management.relation.RelationService
emcmanus
parents: 2
diff changeset
   240
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    private void checkAccessLevel(String accessLevel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        final AccessControlContext acc = AccessController.getContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        final Subject s =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            AccessController.doPrivileged(new PrivilegedAction<Subject>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    public Subject run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                        return Subject.getSubject(acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        if (s == null) return; /* security has not been enabled */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        final Set principals = s.getPrincipals();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        for (Iterator i = principals.iterator(); i.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            final Principal p = (Principal) i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            String grantedAccessLevel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            synchronized (props) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                grantedAccessLevel = props.getProperty(p.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            if (grantedAccessLevel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                if (accessLevel.equals(READONLY) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    (grantedAccessLevel.equals(READONLY) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                     grantedAccessLevel.equals(READWRITE)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                if (accessLevel.equals(READWRITE) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    grantedAccessLevel.equals(READWRITE))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        throw new SecurityException("Access denied! Invalid access level for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                                    "requested MBeanServer operation.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    private void checkValues(Properties props) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        Collection c = props.values();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        for (Iterator i = c.iterator(); i.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            final String accessLevel = (String) i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            if (!accessLevel.equals(READONLY) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                !accessLevel.equals(READWRITE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    "Syntax error in access level entry [" + accessLevel + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    private Properties props;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    private Properties originalProps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    private String accessFileName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
}