1 20 21 22 23 package org.snmp4j.agent.mo.snmp; 24 25 28 import org.snmp4j.smi.*; 29 import org.snmp4j.mp.SnmpConstants; 30 import org.snmp4j.agent.*; 31 import org.snmp4j.agent.mo.*; 32 import org.snmp4j.agent.mo.snmp.*; 33 import java.util.Map ; 34 import java.util.Collections ; 35 import java.util.HashMap ; 36 import java.util.List ; 37 import java.util.*; 38 import org.snmp4j.agent.mo.snmp.SnmpTargetMIB.SnmpTargetAddrEntryRow; 39 import org.snmp4j.log.LogAdapter; 40 import org.snmp4j.log.LogFactory; 41 42 45 public class SnmpCommunityMIB 46 47 implements MOGroup 50 , CoexistenceInfoProvider 52 { 54 55 private static MOFactory moFactory = DefaultMOFactory.getInstance(); 57 58 60 private static final OID oidSnmpCommunityEntry = 61 new OID(new int[] { 1,3,6,1,6,3,18,1,1,1 }); 62 63 private static final int colSnmpCommunityName = 2; 65 private static final int colSnmpCommunitySecurityName = 3; 66 private static final int colSnmpCommunityContextEngineID = 4; 67 private static final int colSnmpCommunityContextName = 5; 68 private static final int colSnmpCommunityTransportTag = 6; 69 private static final int colSnmpCommunityStorageType = 7; 70 private static final int colSnmpCommunityStatus = 8; 71 72 private static final int idxSnmpCommunityName = 0; 74 private static final int idxSnmpCommunitySecurityName = 1; 75 private static final int idxSnmpCommunityContextEngineID = 2; 76 private static final int idxSnmpCommunityContextName = 3; 77 private static final int idxSnmpCommunityTransportTag = 4; 78 private static final int idxSnmpCommunityStorageType = 5; 79 private static final int idxSnmpCommunityStatus = 6; 80 private static MOTableSubIndex[] snmpCommunityEntryIndexes = 81 new MOTableSubIndex[] { 82 moFactory.createSubIndex(SMIConstants.SYNTAX_OCTET_STRING, 1, 32) 83 }; 84 85 private static MOTableIndex snmpCommunityEntryIndex = 86 moFactory.createIndex(snmpCommunityEntryIndexes, 87 true); 88 89 90 private MOTable snmpCommunityEntry; 91 private MOMutableTableModel snmpCommunityEntryModel; 92 private static final OID oidSnmpTargetAddrExtEntry = 93 new OID(new int[] { 1,3,6,1,6,3,18,1,2,1 }); 94 95 private static final int colSnmpTargetAddrTMask = 1; 97 private static final int colSnmpTargetAddrMMS = 2; 98 99 private static final int idxSnmpTargetAddrTMask = 0; 101 private static final int idxSnmpTargetAddrMMS = 1; 102 private static MOTableSubIndex[] snmpTargetAddrExtEntryIndexes = 103 new MOTableSubIndex[] { 104 moFactory.createSubIndex(SMIConstants.SYNTAX_OCTET_STRING, 1, 32) 105 }; 106 107 private static MOTableIndex snmpTargetAddrExtEntryIndex = 108 moFactory.createIndex(snmpTargetAddrExtEntryIndexes, 109 true); 110 111 112 private MOTable snmpTargetAddrExtEntry; 113 private MOMutableTableModel snmpTargetAddrExtEntryModel; 114 115 private MOTableRelation snmpTargetAddrEntryRelation; 116 117 private static final LogAdapter logger = 119 LogFactory.getLogger(SnmpCommunityMIB.class); 120 121 private Map coexistenceInfo; 122 private Map communityInfo; 123 private SnmpTargetMIB targetMIB; 124 private boolean sourceAddressFiltering; 125 126 public SnmpCommunityMIB(SnmpTargetMIB targetMIB) { 127 this(); 128 this.targetMIB = targetMIB; 129 setBaseTableSnmpTargetAddrEntry(targetMIB.getSnmpTargetAddrEntry()); 130 } 131 132 134 private SnmpCommunityMIB() { 135 createSnmpCommunityEntry(); 136 createSnmpTargetAddrExtEntry(); 137 } 138 139 140 public MOTable getSnmpCommunityEntry() { 141 return snmpCommunityEntry; 142 } 143 144 145 private void createSnmpCommunityEntry() { 146 MOColumn[] snmpCommunityEntryColumns = new MOColumn[7]; 147 snmpCommunityEntryColumns[idxSnmpCommunityName] = 148 new MOMutableColumn(colSnmpCommunityName, 149 SMIConstants.SYNTAX_OCTET_STRING, 150 MOAccessImpl.ACCESS_READ_CREATE, 151 null, 152 true); 153 snmpCommunityEntryColumns[idxSnmpCommunitySecurityName] = 154 new MOMutableColumn(colSnmpCommunitySecurityName, 155 SMIConstants.SYNTAX_OCTET_STRING, 156 MOAccessImpl.ACCESS_READ_CREATE, 157 null, 158 true); 159 ((MOMutableColumn)snmpCommunityEntryColumns[idxSnmpCommunitySecurityName]). 160 addMOValueValidationListener(new SnmpCommunitySecurityNameValidator()); 161 snmpCommunityEntryColumns[idxSnmpCommunityContextEngineID] = 162 new MOMutableColumn(colSnmpCommunityContextEngineID, 163 SMIConstants.SYNTAX_OCTET_STRING, 164 MOAccessImpl.ACCESS_READ_CREATE, 165 null, 166 true); 167 ((MOMutableColumn)snmpCommunityEntryColumns[idxSnmpCommunityContextEngineID]). 168 addMOValueValidationListener(new SnmpCommunityContextEngineIDValidator()); 169 snmpCommunityEntryColumns[idxSnmpCommunityContextName] = 170 new MOMutableColumn(colSnmpCommunityContextName, 171 SMIConstants.SYNTAX_OCTET_STRING, 172 MOAccessImpl.ACCESS_READ_CREATE, 173 new OctetString(new byte[] { }), 174 true); 175 ((MOMutableColumn)snmpCommunityEntryColumns[idxSnmpCommunityContextName]). 176 addMOValueValidationListener(new SnmpCommunityContextNameValidator()); 177 snmpCommunityEntryColumns[idxSnmpCommunityTransportTag] = 178 new MOMutableColumn(colSnmpCommunityTransportTag, 179 SMIConstants.SYNTAX_OCTET_STRING, 180 MOAccessImpl.ACCESS_READ_CREATE, 181 new OctetString(new byte[] { }), 182 true); 183 ((MOMutableColumn)snmpCommunityEntryColumns[idxSnmpCommunityTransportTag]). 184 addMOValueValidationListener(new SnmpCommunityTransportTagValidator()); 185 snmpCommunityEntryColumns[idxSnmpCommunityStorageType] = 186 new StorageType(colSnmpCommunityStorageType, 187 MOAccessImpl.ACCESS_READ_CREATE, 188 null, 189 true); 190 snmpCommunityEntryColumns[idxSnmpCommunityStatus] = 191 new RowStatus(colSnmpCommunityStatus); 192 193 snmpCommunityEntryModel = new DefaultMOMutableTableModel(); 194 snmpCommunityEntryModel.setRowFactory(new SnmpCommunityEntryRowFactory()); 195 snmpCommunityEntry = 196 moFactory.createTable(oidSnmpCommunityEntry, 197 snmpCommunityEntryIndex, 198 snmpCommunityEntryColumns, 199 snmpCommunityEntryModel); 200 } 201 202 public MOTable getSnmpTargetAddrExtEntry() { 203 return snmpTargetAddrExtEntry; 204 } 205 206 public void setBaseTableSnmpTargetAddrEntry(MOTable baseTable) { 207 snmpTargetAddrEntryRelation = 208 moFactory.createTableRelation(baseTable, snmpTargetAddrExtEntry); 209 snmpTargetAddrEntryRelation.createRelationShip(); 210 } 211 212 private void createSnmpTargetAddrExtEntry() { 213 MOColumn[] snmpTargetAddrExtEntryColumns = new MOColumn[2]; 214 snmpTargetAddrExtEntryColumns[idxSnmpTargetAddrTMask] = 215 new MOMutableColumn(colSnmpTargetAddrTMask, 216 SMIConstants.SYNTAX_OCTET_STRING, 217 MOAccessImpl.ACCESS_READ_CREATE, 218 new OctetString(new byte[] { }), 219 true); 220 ((MOMutableColumn)snmpTargetAddrExtEntryColumns[idxSnmpTargetAddrTMask]). 221 addMOValueValidationListener(new SnmpTargetAddrTMaskValidator()); 222 snmpTargetAddrExtEntryColumns[idxSnmpTargetAddrMMS] = 223 new MOMutableColumn(colSnmpTargetAddrMMS, 224 SMIConstants.SYNTAX_INTEGER32, 225 MOAccessImpl.ACCESS_READ_CREATE, 226 new Integer32(484), 227 true); 228 ((MOMutableColumn)snmpTargetAddrExtEntryColumns[idxSnmpTargetAddrMMS]). 229 addMOValueValidationListener(new SnmpTargetAddrMMSValidator()); 230 231 snmpTargetAddrExtEntryModel = new DefaultMOMutableTableModel(); 232 snmpTargetAddrExtEntryModel.setRowFactory(new SnmpTargetAddrExtEntryRowFactory()); 233 snmpTargetAddrExtEntry = 234 moFactory.createTable(oidSnmpTargetAddrExtEntry, 235 snmpTargetAddrExtEntryIndex, 236 snmpTargetAddrExtEntryColumns, 237 snmpTargetAddrExtEntryModel); 238 } 239 240 241 public void registerMOs(MOServer server, OctetString context) 242 throws DuplicateRegistrationException 243 { 244 server.register(this.snmpCommunityEntry, context); 246 server.register(this.snmpTargetAddrExtEntry, context); 247 } 250 251 public void unregisterMOs(MOServer server, OctetString context) { 252 server.unregister(this.snmpCommunityEntry, context); 254 server.unregister(this.snmpTargetAddrExtEntry, context); 255 } 258 259 261 263 265 269 static class SnmpCommunitySecurityNameValidator implements MOValueValidationListener { 270 271 public void validate(MOValueValidationEvent validationEvent) { 272 Variable newValue = validationEvent.getNewValue(); 273 OctetString os = (OctetString)newValue; 274 if (!(((os.length() >= 1) && (os.length() <= 32)))) { 275 validationEvent.setValidationStatus(SnmpConstants.SNMP_ERROR_WRONG_LENGTH); 276 return; 277 } 278 } 281 } 282 286 static class SnmpCommunityContextEngineIDValidator implements MOValueValidationListener { 287 288 public void validate(MOValueValidationEvent validationEvent) { 289 Variable newValue = validationEvent.getNewValue(); 290 OctetString os = (OctetString)newValue; 291 if (!(((os.length() >= 5) && (os.length() <= 32)))) { 292 validationEvent.setValidationStatus(SnmpConstants.SNMP_ERROR_WRONG_LENGTH); 293 return; 294 } 295 } 298 } 299 303 static class SnmpCommunityContextNameValidator implements MOValueValidationListener { 304 305 public void validate(MOValueValidationEvent validationEvent) { 306 Variable newValue = validationEvent.getNewValue(); 307 OctetString os = (OctetString)newValue; 308 if (!(((os.length() >= 0) && (os.length() <= 32)))) { 309 validationEvent.setValidationStatus(SnmpConstants.SNMP_ERROR_WRONG_LENGTH); 310 return; 311 } 312 } 315 } 316 320 static class SnmpCommunityTransportTagValidator implements MOValueValidationListener { 321 322 public void validate(MOValueValidationEvent validationEvent) { 323 Variable newValue = validationEvent.getNewValue(); 324 OctetString os = (OctetString)newValue; 325 if (!(((os.length() >= 0) && (os.length() <= 255)))) { 326 validationEvent.setValidationStatus(SnmpConstants.SNMP_ERROR_WRONG_LENGTH); 327 return; 328 } 329 } 332 } 333 337 static class SnmpTargetAddrTMaskValidator implements MOValueValidationListener { 338 339 public void validate(MOValueValidationEvent validationEvent) { 340 Variable newValue = validationEvent.getNewValue(); 341 OctetString os = (OctetString)newValue; 342 if (!(((os.length() >= 0) && (os.length() <= 255)))) { 343 validationEvent.setValidationStatus(SnmpConstants.SNMP_ERROR_WRONG_LENGTH); 344 return; 345 } 346 } 349 } 350 354 static class SnmpTargetAddrMMSValidator implements MOValueValidationListener { 355 356 public void validate(MOValueValidationEvent validationEvent) { 357 Variable newValue = validationEvent.getNewValue(); 358 long v = ((Integer32)newValue).getValue(); 359 if (!(((v >= 0L) && (v <= 0L)) || 360 ((v >= 484L)))) { 361 validationEvent.setValidationStatus(SnmpConstants.SNMP_ERROR_WRONG_VALUE); 362 return; 363 } 364 } 367 } 368 369 370 371 class SnmpCommunityEntryRowFactory 373 extends DefaultMOMutableRow2PCFactory 374 { 375 public synchronized MOTableRow createRow(OID index, Variable[] values) 376 throws UnsupportedOperationException 377 { 378 SnmpCommunityEntryRow row = new SnmpCommunityEntryRow(index, values); 379 if (coexistenceInfo == null) { 381 coexistenceInfo = Collections.synchronizedMap(new HashMap ()); 382 } 383 coexistenceInfo.put(values[idxSnmpCommunityName], row); 384 if (communityInfo == null) { 385 communityInfo = Collections.synchronizedMap(new HashMap ()); 386 } 387 List l = (List ) communityInfo.get(values[idxSnmpCommunitySecurityName]); 388 if (l == null) { 389 l = new LinkedList(); 390 communityInfo.put(values[idxSnmpCommunitySecurityName], l); 391 } 392 l.add(row); 393 return row; 395 } 396 397 public synchronized void freeRow(MOTableRow row) { 398 coexistenceInfo.remove(row.getValue(idxSnmpCommunityName)); 400 List l = 401 (List ) communityInfo.get(row.getValue(idxSnmpCommunitySecurityName)); 402 if (l != null) { 403 l.remove(row); 404 if (l.size() == 0) { 405 communityInfo.remove(row.getValue(idxSnmpCommunitySecurityName)); 406 } 407 } 408 } 410 } 411 412 class SnmpCommunityEntryRow extends DefaultMOMutableRow2PC { 413 public SnmpCommunityEntryRow(OID index, Variable[] values) { 414 super(index, values); 415 } 416 417 public OctetString getSnmpCommunityName() { 418 return (OctetString) getValue(idxSnmpCommunityName); 419 } 420 421 public void setSnmpCommunityName(OctetString newValue) { 422 setValue(idxSnmpCommunityName, newValue); 423 } 424 425 public OctetString getSnmpCommunitySecurityName() { 426 return (OctetString) getValue(idxSnmpCommunitySecurityName); 427 } 428 429 public void setSnmpCommunitySecurityName(OctetString newValue) { 430 setValue(idxSnmpCommunitySecurityName, newValue); 431 } 432 433 public OctetString getSnmpCommunityContextEngineID() { 434 return (OctetString) getValue(idxSnmpCommunityContextEngineID); 435 } 436 437 public void setSnmpCommunityContextEngineID(OctetString newValue) { 438 setValue(idxSnmpCommunityContextEngineID, newValue); 439 } 440 441 public OctetString getSnmpCommunityContextName() { 442 return (OctetString) getValue(idxSnmpCommunityContextName); 443 } 444 445 public void setSnmpCommunityContextName(OctetString newValue) { 446 setValue(idxSnmpCommunityContextName, newValue); 447 } 448 449 public OctetString getSnmpCommunityTransportTag() { 450 return (OctetString) getValue(idxSnmpCommunityTransportTag); 451 } 452 453 public void setSnmpCommunityTransportTag(OctetString newValue) { 454 setValue(idxSnmpCommunityTransportTag, newValue); 455 } 456 457 public Integer32 getSnmpCommunityStorageType() { 458 return (Integer32) getValue(idxSnmpCommunityStorageType); 459 } 460 461 public void setSnmpCommunityStorageType(Integer32 newValue) { 462 setValue(idxSnmpCommunityStorageType, newValue); 463 } 464 465 public Integer32 getSnmpCommunityStatus() { 466 return (Integer32) getValue(idxSnmpCommunityStatus); 467 } 468 469 public void setSnmpCommunityStatus(Integer32 newValue) { 470 setValue(idxSnmpCommunityStatus, newValue); 471 } 472 473 474 } 477 478 class SnmpTargetAddrExtEntryRowFactory 479 extends DefaultMOMutableRow2PCFactory 480 { 481 public MOTableRow createRow(OID index, Variable[] values) 482 throws UnsupportedOperationException 483 { 484 SnmpTargetAddrExtEntryRow row = new SnmpTargetAddrExtEntryRow(index, values); 485 return row; 488 } 489 490 public void freeRow(MOTableRow row) { 491 } 494 } 495 496 class SnmpTargetAddrExtEntryRow extends DefaultMOMutableRow2PC { 497 public SnmpTargetAddrExtEntryRow(OID index, Variable[] values) { 498 super(index, values); 499 } 500 501 public OctetString getSnmpTargetAddrTMask() { 502 return (OctetString) getValue(idxSnmpTargetAddrTMask); 503 } 504 505 public void setSnmpTargetAddrTMask(OctetString newValue) { 506 setValue(idxSnmpTargetAddrTMask, newValue); 507 } 508 509 public Integer32 getSnmpTargetAddrMMS() { 510 return (Integer32) getValue(idxSnmpTargetAddrMMS); 511 } 512 513 public void setSnmpTargetAddrMMS(Integer32 newValue) { 514 setValue(idxSnmpTargetAddrMMS, newValue); 515 } 516 517 518 } 521 522 523 524 526 public CoexistenceInfo getCoexistenceInfo(OctetString community) { 527 if (logger.isDebugEnabled()) { 528 logger.debug("Looking up coexistence info for '"+community+"'"); 529 } 530 if (coexistenceInfo == null) { 531 return null; 532 } 533 SnmpCommunityEntryRow row = 534 (SnmpCommunityEntryRow) coexistenceInfo.get(community); 535 if (row != null) { 536 CoexistenceInfo info = 537 new CoexistenceInfo(row.getSnmpCommunitySecurityName(), 538 row.getSnmpCommunityContextEngineID(), 539 row.getSnmpCommunityContextName(), 540 row.getSnmpCommunityTransportTag()); 541 if (logger.isDebugEnabled()) { 542 logger.debug("Found coexistence info for '"+community+"'="+info); 543 } 544 return info; 545 } 546 return null; 547 } 548 549 567 public boolean passesFilter(Address address, 568 CoexistenceInfo coexistenceInfo) { 569 if (!isSourceAddressFiltering()) { 570 if (logger.isDebugEnabled()) { 571 logger.debug("Address "+address+ 572 " passes filter, "+ 573 "because source address filtering is disabled"); 574 } 575 return true; 576 } 577 if ((coexistenceInfo.getTransportTag() == null) || 578 (coexistenceInfo.getTransportTag().length() == 0)) { 579 if (logger.isDebugEnabled()) { 580 logger.debug("Address "+address+ 581 " passes filter, because transportTag is null"); 582 } 583 return true; 584 } 585 Collection matches = 586 targetMIB.getTargetAddrRowsForTag(coexistenceInfo.getTransportTag()); 587 for (Iterator it = matches.iterator(); it.hasNext(); ) { 588 SnmpTargetAddrEntryRow row = (SnmpTargetAddrEntryRow) it.next(); 589 SnmpTargetAddrExtEntryRow extRow = (SnmpTargetAddrExtEntryRow) 590 snmpTargetAddrExtEntryModel.getRow(row.getIndex()); 591 OctetString filterTAddress = 592 (OctetString) row.getValue(SnmpTargetMIB.idxSnmpTargetAddrTAddress); 593 if (extRow != null) { 594 OctetString mask = extRow.getSnmpTargetAddrTMask(); 595 OctetString matchAddress = row.getTAddress(address); 596 if ((mask.length() == 0) && 597 (matchAddress != null) && (matchAddress.equals(filterTAddress))) { 598 coexistenceInfo.setMaxMessageSize( 599 extRow.getSnmpTargetAddrMMS().getValue()); 600 return true; 601 } 602 else if ((matchAddress != null) && 603 (mask.length() == matchAddress.length()) && 604 (matchAddress.length() == filterTAddress.length())) { 605 OctetString maskedFilterTAddress = filterTAddress.mask(mask); 606 OctetString maskedMatchTAddress = matchAddress.mask(mask); 607 if (maskedFilterTAddress.equals(maskedMatchTAddress)) { 608 coexistenceInfo.setMaxMessageSize( 609 extRow.getSnmpTargetAddrMMS().getValue()); 610 return true; 611 } 612 } 613 } 614 } 615 return false; 616 } 617 618 629 public void setSourceAddressFiltering(boolean sourceAddressFiltering) { 630 this.sourceAddressFiltering = sourceAddressFiltering; 631 } 632 633 640 public boolean isSourceAddressFiltering() { 641 return sourceAddressFiltering; 642 } 643 644 public OctetString getCommunity(OctetString securityName, 645 OctetString contextEngineID, 646 OctetString contextName) { 647 List l = (List ) communityInfo.get(securityName); 648 if (l != null) { 649 for (Iterator it = l.iterator(); it.hasNext(); ) { 650 SnmpCommunityEntryRow row = (SnmpCommunityEntryRow) it.next(); 651 if (row.getSnmpCommunityStatus().getValue() == RowStatus.active) { 652 if (((contextEngineID == null) || 653 (contextEngineID.equals(row.getSnmpCommunityContextEngineID()))) && 654 ((row.getSnmpCommunityContextName().equals(contextName)))) { 655 return row.getSnmpCommunityName(); 656 } 657 } 658 } 659 } 660 return null; 661 } 662 663 665 668 } 671 672 673 | Popular Tags |