1 11 12 13 package com.sun.jmx.snmp.IPAcl; 14 15 16 17 import java.io.Serializable ; 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.FileNotFoundException ; 23 import java.net.InetAddress ; 24 import java.net.UnknownHostException ; 25 import java.util.Hashtable ; 26 import java.util.Vector ; 27 import java.util.Enumeration ; 28 import java.util.HashSet ; 29 import java.security.acl.AclEntry ; 30 import java.security.acl.NotOwnerException ; 31 32 import com.sun.jmx.snmp.InetAddressAcl; 35 import com.sun.jmx.trace.Trace; 36 37 51 52 public class SnmpAcl implements InetAddressAcl, Serializable { 53 54 static final PermissionImpl READ = new PermissionImpl("READ"); 55 static final PermissionImpl WRITE = new PermissionImpl("WRITE"); 56 57 67 public SnmpAcl(String Owner) 68 throws UnknownHostException , IllegalArgumentException { 69 this(Owner,null); 70 } 71 72 83 public SnmpAcl(String Owner, String aclFileName) 84 throws UnknownHostException , IllegalArgumentException { 85 trapDestList= new Hashtable (); 86 informDestList= new Hashtable (); 87 88 owner = new PrincipalImpl(); 90 try { 91 acl = new AclImpl(owner,Owner); 92 AclEntry ownEntry = new AclEntryImpl(owner); 93 ownEntry.addPermission(READ); 94 ownEntry.addPermission(WRITE); 95 acl.addEntry(owner,ownEntry); 96 } catch (NotOwnerException ex) { 97 if (isDebugOn()) { 98 debug("constructor", 99 "Should never get NotOwnerException as the owner"+ 100 " is built in this constructor"); 101 } 102 } 103 if (aclFileName == null) setDefaultFileName(); 104 else setAuthorizedListFile(aclFileName); 105 readAuthorizedListFile(); 106 } 107 108 114 public Enumeration entries() { 115 return acl.entries(); 116 } 117 118 122 public Enumeration communities() { 123 HashSet set = new HashSet (); 124 Vector res = new Vector (); 125 for (Enumeration e = acl.entries() ; e.hasMoreElements() ;) { 126 AclEntryImpl entry = (AclEntryImpl) e.nextElement(); 127 for (Enumeration cs = entry.communities(); 128 cs.hasMoreElements() ;) { 129 set.add((String ) cs.nextElement()); 130 } 131 } 132 Object [] objs = set.toArray(); 133 for(int i = 0; i < objs.length; i++) 134 res.addElement(objs[i]); 135 136 return res.elements(); 137 } 138 139 144 public String getName() { 145 return acl.getName(); 146 } 147 148 153 static public PermissionImpl getREAD() { 154 return READ; 155 } 156 157 162 static public PermissionImpl getWRITE() { 163 return WRITE; 164 } 165 166 171 public static String getDefaultAclFileName() { 172 final String fileSeparator = 173 System.getProperty("file.separator"); 174 final StringBuffer defaultAclName = 175 new StringBuffer (System.getProperty("java.home")). 176 append(fileSeparator).append("lib").append(fileSeparator). 177 append("snmp.acl"); 178 return defaultAclName.toString(); 179 } 180 181 187 public void setAuthorizedListFile(String filename) 188 throws IllegalArgumentException { 189 File file = new File (filename); 190 if (!file.isFile() ) { 191 if (isDebugOn()) { 192 debug("setAuthorizedListFile", 193 "ACL file not found: " + filename); 194 } 195 throw new 196 IllegalArgumentException ("The specified file ["+file+"] "+ 197 "doesn't exist or is not a file, "+ 198 "no configuration loaded"); 199 } 200 if (isTraceOn()) { 201 trace("setAuthorizedListFile", "Default file set to " + filename); 202 } 203 authorizedListFile = filename; 204 } 205 206 212 public void rereadTheFile() throws NotOwnerException , UnknownHostException { 213 alwaysAuthorized = false; 214 acl.removeAll(owner); 215 trapDestList.clear(); 216 informDestList.clear(); 217 AclEntry ownEntry = new AclEntryImpl(owner); 218 ownEntry.addPermission(READ); 219 ownEntry.addPermission(WRITE); 220 acl.addEntry(owner,ownEntry); 221 readAuthorizedListFile(); 222 } 223 224 229 public String getAuthorizedListFile() { 230 return authorizedListFile; 231 } 232 233 240 public boolean checkReadPermission(InetAddress address) { 241 if (alwaysAuthorized) return ( true ); 242 PrincipalImpl p = new PrincipalImpl(address); 243 return acl.checkPermission(p, READ); 244 } 245 246 254 public boolean checkReadPermission(InetAddress address, String community) { 255 if (alwaysAuthorized) return ( true ); 256 PrincipalImpl p = new PrincipalImpl(address); 257 return acl.checkPermission(p, community, READ); 258 } 259 260 267 public boolean checkCommunity(String community) { 268 return acl.checkCommunity(community); 269 } 270 271 278 public boolean checkWritePermission(InetAddress address) { 279 if (alwaysAuthorized) return ( true ); 280 PrincipalImpl p = new PrincipalImpl(address); 281 return acl.checkPermission(p, WRITE); 282 } 283 284 292 public boolean checkWritePermission(InetAddress address, String community) { 293 if (alwaysAuthorized) return ( true ); 294 PrincipalImpl p = new PrincipalImpl(address); 295 return acl.checkPermission(p, community, WRITE); 296 } 297 298 303 public Enumeration getTrapDestinations() { 304 return trapDestList.keys(); 305 } 306 307 314 public Enumeration getTrapCommunities(InetAddress i) { 315 Vector list = null; 316 if ((list = (Vector )trapDestList.get(i)) != null ) { 317 if (isTraceOn()) { 318 trace("getTrapCommunities", "["+i.toString()+"] is in list"); 319 } 320 return list.elements(); 321 } else { 322 list = new Vector (); 323 if (isTraceOn()) { 324 trace("getTrapCommunities", "["+i.toString()+"] is not in list"); 325 } 326 return list.elements(); 327 } 328 } 329 330 335 public Enumeration getInformDestinations() { 336 return informDestList.keys(); 337 } 338 339 346 public Enumeration getInformCommunities(InetAddress i) { 347 Vector list = null; 348 if ((list = (Vector )informDestList.get(i)) != null ) { 349 if (isTraceOn()) { 350 trace("getInformCommunities", "["+i.toString()+"] is in list"); 351 } 352 return list.elements(); 353 } else { 354 list = new Vector (); 355 if (isTraceOn()) { 356 trace("getInformCommunities", "["+i.toString()+"] is not in list"); 357 } 358 return list.elements(); 359 } 360 } 361 362 365 private void readAuthorizedListFile() { 366 367 alwaysAuthorized = false; 368 369 if (authorizedListFile == null) { 370 if (isTraceOn()) { 371 trace("readAuthorizedListFile", "alwaysAuthorized set to true"); 372 } 373 alwaysAuthorized = true ; 374 } else { 375 Parser parser = null; 377 try { 378 parser= new Parser(new FileInputStream (getAuthorizedListFile())); 379 } catch (FileNotFoundException e) { 380 if (isDebugOn()) { 381 debug("readAuthorizedListFile", "The specified file was not found, authorize everybody"); 382 } 383 alwaysAuthorized = true ; 384 return; 385 } 386 387 try { 388 JDMSecurityDefs n = parser.SecurityDefs(); 389 n.buildAclEntries(owner, acl); 390 n.buildTrapEntries(trapDestList); 391 n.buildInformEntries(informDestList); 392 } catch (ParseException e) { 393 if (isDebugOn()) { 394 debug("readAuthorizedListFile", "Parsing exception " + e); 395 } 396 throw new IllegalArgumentException (e.getMessage()); 397 } catch (Error err) { 398 if (isDebugOn()) { 399 debug("readAuthorizedListFile", "Error exception"); 400 } 401 throw new IllegalArgumentException (err.getMessage()); 402 } 403 404 for(Enumeration e = acl.entries(); e.hasMoreElements();) { 405 AclEntryImpl aa = (AclEntryImpl) e.nextElement(); 406 if (isTraceOn()) { 407 trace("readAuthorizedListFile", "===> " + aa.getPrincipal().toString()); 408 } 409 for (Enumeration eee = aa.permissions();eee.hasMoreElements();) { 410 java.security.acl.Permission perm = (java.security.acl.Permission )eee.nextElement(); 411 if (isTraceOn()) { 412 trace("readAuthorizedListFile", "perm = " + perm); 413 } 414 } 415 } 416 } 417 } 418 419 423 private void setDefaultFileName() { 424 try { 425 setAuthorizedListFile(getDefaultAclFileName()); 426 } catch (IllegalArgumentException x) { 427 } 429 } 430 431 432 435 boolean isTraceOn() { 436 return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_SNMP); 437 } 438 439 void trace(String clz, String func, String info) { 440 Trace.send(Trace.LEVEL_TRACE, Trace.INFO_SNMP, clz, func, info); 441 } 442 443 void trace(String func, String info) { 444 trace(dbgTag, func, info); 445 } 446 447 boolean isDebugOn() { 448 return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_SNMP); 449 } 450 451 void debug(String clz, String func, String info) { 452 Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_SNMP, clz, func, info); 453 } 454 455 void debug(String func, String info) { 456 debug(dbgTag, func, info); 457 } 458 459 String dbgTag = "SnmpAcl"; 460 461 464 467 private AclImpl acl = null; 468 472 private boolean alwaysAuthorized = false; 473 476 private String authorizedListFile = null; 477 480 private Hashtable trapDestList = null; 481 484 private Hashtable informDestList = null; 485 486 private PrincipalImpl owner = null; 487 } 488 | Popular Tags |