1 7 8 package com.sun.jmx.remote.security; 9 10 import java.io.FileInputStream ; 11 import java.io.IOException ; 12 import java.security.AccessControlContext ; 13 import java.security.AccessController ; 14 import java.security.Principal ; 15 import java.security.PrivilegedAction ; 16 import java.util.Collection ; 17 import java.util.Iterator ; 18 import java.util.Properties ; 19 import java.util.Set ; 20 import javax.management.MBeanServer ; 21 import javax.security.auth.Subject ; 22 23 44 public class MBeanServerFileAccessController 45 extends MBeanServerAccessController { 46 47 public static final String READONLY = "readonly"; 48 public static final String READWRITE = "readwrite"; 49 50 68 public MBeanServerFileAccessController(String accessFileName) 69 throws IOException { 70 super(); 71 this.accessFileName = accessFileName; 72 props = propertiesFromFile(accessFileName); 73 checkValues(props); 74 } 75 76 95 public MBeanServerFileAccessController(String accessFileName, 96 MBeanServer mbs) 97 throws IOException { 98 this(accessFileName); 99 setMBeanServer(mbs); 100 } 101 102 124 public MBeanServerFileAccessController(Properties accessFileProps) 125 throws IOException { 126 super(); 127 if (accessFileProps == null) 128 throw new IllegalArgumentException ("Null properties"); 129 originalProps = accessFileProps; 130 props = (Properties ) accessFileProps.clone(); 131 checkValues(props); 132 } 133 134 158 public MBeanServerFileAccessController(Properties accessFileProps, 159 MBeanServer mbs) 160 throws IOException { 161 this(accessFileProps); 162 setMBeanServer(mbs); 163 } 164 165 169 public void checkRead() { 170 checkAccessLevel(READONLY); 171 } 172 173 177 public void checkWrite() { 178 checkAccessLevel(READWRITE); 179 } 180 181 203 public void refresh() throws IOException { 204 synchronized (props) { 205 if (accessFileName == null) 206 props = (Properties ) originalProps.clone(); 207 else 208 props = propertiesFromFile(accessFileName); 209 checkValues(props); 210 } 211 } 212 213 private static Properties propertiesFromFile(String fname) 214 throws IOException { 215 FileInputStream fin = new FileInputStream (fname); 216 Properties p = new Properties (); 217 p.load(fin); 218 fin.close(); 219 return p; 220 } 221 222 private void checkAccessLevel(String accessLevel) { 223 final AccessControlContext acc = AccessController.getContext(); 224 final Subject s = (Subject ) 225 AccessController.doPrivileged(new PrivilegedAction () { 226 public Object run() { 227 return Subject.getSubject(acc); 228 } 229 }); 230 if (s == null) return; 231 final Set principals = s.getPrincipals(); 232 for (Iterator i = principals.iterator(); i.hasNext(); ) { 233 final Principal p = (Principal ) i.next(); 234 String grantedAccessLevel; 235 synchronized (props) { 236 grantedAccessLevel = props.getProperty(p.getName()); 237 } 238 if (grantedAccessLevel != null) { 239 if (accessLevel.equals(READONLY) && 240 (grantedAccessLevel.equals(READONLY) || 241 grantedAccessLevel.equals(READWRITE))) 242 return; 243 if (accessLevel.equals(READWRITE) && 244 grantedAccessLevel.equals(READWRITE)) 245 return; 246 } 247 } 248 throw new SecurityException ("Access denied! Invalid access level for " + 249 "requested MBeanServer operation."); 250 } 251 252 private void checkValues(Properties props) { 253 Collection c = props.values(); 254 for (Iterator i = c.iterator(); i.hasNext(); ) { 255 final String accessLevel = (String ) i.next(); 256 if (!accessLevel.equals(READONLY) && 257 !accessLevel.equals(READWRITE)) { 258 throw new IllegalArgumentException ( 259 "Syntax error in access level entry [" + accessLevel + "]"); 260 } 261 } 262 } 263 264 private Properties props; 265 private Properties originalProps; 266 private String accessFileName; 267 } 268 | Popular Tags |