1 11 12 13 package com.sun.jmx.snmp.agent; 14 15 16 17 import java.io.Serializable ; 20 import java.util.Vector ; 21 import java.util.Enumeration ; 22 import java.lang.IllegalAccessException ; 23 24 import javax.management.ObjectName ; 27 import javax.management.MBeanServer ; 28 import javax.management.MalformedObjectNameException ; 29 import javax.management.InstanceAlreadyExistsException ; 30 import javax.management.MBeanRegistrationException ; 31 import javax.management.NotCompliantMBeanException ; 32 import com.sun.jmx.snmp.SnmpOid; 33 import com.sun.jmx.snmp.SnmpVarBind; 34 import com.sun.jmx.snmp.SnmpDefinitions; 35 import com.sun.jmx.snmp.SnmpStatusException; 36 import com.sun.jmx.snmp.SnmpEngine; 37 import com.sun.jmx.snmp.SnmpUnknownModelException; 38 import com.sun.jmx.trace.Trace; 40 41 import com.sun.jmx.snmp.internal.SnmpAccessControlModel; 42 import com.sun.jmx.snmp.internal.SnmpEngineImpl; 43 44 45 49 final class LongList { 50 51 public static int DEFAULT_CAPACITY = 10; 52 53 public static int DEFAULT_INCREMENT = 10; 54 55 56 private final int DELTA; 57 private int size; 58 59 64 public long[] list; 65 66 LongList() { 67 this(DEFAULT_CAPACITY,DEFAULT_INCREMENT); 68 } 69 70 LongList(int initialCapacity) { 71 this(initialCapacity,DEFAULT_INCREMENT); 72 } 73 74 LongList(int initialCapacity, int delta) { 75 size = 0; 76 DELTA = delta; 77 list = allocate(initialCapacity); 78 } 79 80 83 public final int size() { return size;} 84 85 90 public final boolean add(final long o) { 91 if (size >= list.length) 92 resize(); 93 list[size++]=o; 94 return true; 95 } 96 97 103 public final void add(final int index, final long o) { 104 if (index > size) throw new IndexOutOfBoundsException (); 105 if (index >= list.length) resize(); 106 if (index == size) { 107 list[size++]=o; 108 return; 109 } 110 111 java.lang.System.arraycopy(list,index,list,index+1,size-index); 112 list[index]=o; 113 size++; 114 } 115 116 127 public final void add(final int at,final long[] src, final int from, 128 final int count) { 129 if (count <= 0) return; 130 if (at > size) throw new IndexOutOfBoundsException (); 131 ensure(size+count); 132 if (at < size) { 133 java.lang.System.arraycopy(list,at,list,at+count,size-at); 134 } 135 java.lang.System.arraycopy(src,from,list,at,count); 136 size+=count; 137 } 138 139 143 public final long remove(final int from, final int count) { 144 if (count < 1 || from < 0) return -1; 145 if (from+count > size) return -1; 146 147 final long o = list[from]; 148 final int oldsize = size; 149 size = size - count; 150 151 if (from == size) return o; 152 153 java.lang.System.arraycopy(list,from+count,list,from, 154 size-from); 155 return o; 156 } 157 158 163 public final long remove(final int index) { 164 if (index >= size) return -1; 165 final long o = list[index]; 166 list[index]=0; 167 if (index == --size) return o; 168 169 java.lang.System.arraycopy(list,index+1,list,index, 170 size-index); 171 return o; 172 } 173 174 180 public final long[] toArray(long[] a) { 181 java.lang.System.arraycopy(list,0,a,0,size); 182 return a; 183 } 184 185 191 public final long[] toArray() { 192 return toArray(new long[size]); 193 } 194 195 200 private final void resize() { 201 final long[] newlist = allocate(list.length + DELTA); 202 java.lang.System.arraycopy(list,0,newlist,0,size); 203 list = newlist; 204 } 205 206 213 private final void ensure(int length) { 214 if (list.length < length) { 215 final int min = list.length+DELTA; 216 length=(length<min)?min:length; 217 final long[] newlist = allocate(length); 218 java.lang.System.arraycopy(list,0,newlist,0,size); 219 list = newlist; 220 } 221 } 222 223 226 private final long[] allocate(final int length) { 227 return new long[length]; 228 } 229 230 } 231 232 235 class AcmChecker { 236 237 238 SnmpAccessControlModel model = null; 239 String principal = null; 240 int securityLevel = -1; 241 int version = -1; 242 int pduType = -1; 243 int securityModel = -1; 244 byte[] contextName = null; 245 SnmpEngineImpl engine = null; 246 LongList l = null; 247 AcmChecker(SnmpMibRequest req) { 248 engine = (SnmpEngineImpl) req.getEngine(); 249 if(engine != null) { 251 if(engine.isCheckOidActivated()) { 252 try { 253 if (isDebugOn()) 254 debug("AcmChecker", 255 " SNMP V3 Access Control to be done."); 256 model = (SnmpAccessControlModel) 257 engine.getAccessControlSubSystem(). 258 getModel(SnmpDefinitions.snmpVersionThree); 259 principal = req.getPrincipal(); 260 securityLevel = req.getSecurityLevel(); 261 pduType = req.getPdu().type; 262 version = req.getRequestPduVersion(); 263 securityModel = req.getSecurityModel(); 264 contextName = req.getAccessContextName(); 265 l = new LongList(); 266 if (isDebugOn()) 267 debug("AcmChecker", 268 "Will check oid for : principal : " + principal + 269 ";securityLevel : " + 270 securityLevel +";pduType : " + pduType + 271 ";version : " 272 + version + ";securityModel : " + 273 securityModel +";contextName : " + 274 (contextName == null ? null : 275 new String (contextName))); 276 }catch(SnmpUnknownModelException e) { 277 if (isDebugOn()) 278 debug("AcmChecker", 279 " Unknown Model, no ACM check."); 280 } 281 } 282 } 283 } 284 285 void add(int index, long arc) { 286 if(model != null) 287 l.add(index, arc); 288 } 289 290 void remove(int index) { 291 if(model != null) 292 l.remove(index); 293 } 294 295 void add(final int at,final long[] src, final int from, 296 final int count) { 297 if(model != null) 298 l.add(at,src,from,count); 299 } 300 301 void remove(final int from, final int count) { 302 if(model != null) 303 l.remove(from,count); 304 } 305 306 void checkCurrentOid() throws SnmpStatusException { 307 if(model != null) { 308 SnmpOid oid = new SnmpOid(l.toArray()); 309 if (isDebugOn()) 310 debug("check", 311 " Checking access for : " + oid); 312 model.checkAccess(version, 313 principal, 314 securityLevel, 315 pduType, 316 securityModel, 317 contextName, 318 oid); 319 } 320 } 321 322 private final static boolean isDebugOn() { 324 return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_ADAPTOR_SNMP); 325 } 326 327 private final static void debug(String func, String info) { 329 Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_ADAPTOR_SNMP, 330 "AcmChecker", func, info); 331 } 332 } 333 334 352 public abstract class SnmpMib extends SnmpMibAgent implements Serializable { 353 354 358 public SnmpMib() { 359 root= new SnmpMibOid(); 360 } 361 362 363 367 394 protected String getGroupOid(String groupName, String defaultOid) { 395 return defaultOid; 396 } 397 398 428 protected ObjectName getGroupObjectName(String name, String oid, 429 String defaultName) 430 throws MalformedObjectNameException { 431 return new ObjectName (defaultName); 432 } 433 434 473 protected void registerGroupNode(String groupName, String groupOid, 474 ObjectName groupObjName, SnmpMibNode node, 475 Object group, MBeanServer server) 476 throws NotCompliantMBeanException , MBeanRegistrationException , 477 InstanceAlreadyExistsException , IllegalAccessException { 478 root.registerNode(groupOid,node); 479 if (server != null && groupObjName != null && group != null) 480 server.registerMBean(group,groupObjName); 481 } 482 483 506 public abstract void registerTableMeta(String name, SnmpMibTable table); 507 508 517 public abstract SnmpMibTable getRegisteredTableMeta(String name); 518 519 523 527 public void get(SnmpMibRequest req) throws SnmpStatusException { 531 532 535 final int reqType = SnmpDefinitions.pduGetRequestPdu; 536 SnmpRequestTree handlers = getHandlers(req,false,false,reqType); 537 538 SnmpRequestTree.Handler h = null; 539 SnmpMibNode meta = null; 540 541 if (isDebugOn()) 542 debug("get","Processing handlers for GET... "); 543 544 for (Enumeration eh=handlers.getHandlers();eh.hasMoreElements();) { 547 h = (SnmpRequestTree.Handler) eh.nextElement(); 548 549 meta = handlers.getMetaNode(h); 553 554 final int depth = handlers.getOidDepth(h); 556 557 for (Enumeration rqs=handlers.getSubRequests(h); 558 rqs.hasMoreElements();) { 559 560 meta.get((SnmpMibSubRequest)rqs.nextElement(),depth); 562 } 563 } 564 } 565 566 570 public void set(SnmpMibRequest req) throws SnmpStatusException { 574 575 SnmpRequestTree handlers = null; 576 577 if (req instanceof SnmpMibRequestImpl) 582 handlers = ((SnmpMibRequestImpl)req).getRequestTree(); 583 584 final int reqType = SnmpDefinitions.pduSetRequestPdu; 590 if (handlers == null) handlers = getHandlers(req,false,true,reqType); 591 handlers.switchCreationFlag(false); 592 handlers.setPduType(reqType); 593 594 SnmpRequestTree.Handler h = null; 595 SnmpMibNode meta = null; 596 597 if (isDebugOn()) 598 debug("set","Processing handlers for SET... "); 599 600 for (Enumeration eh=handlers.getHandlers();eh.hasMoreElements();) { 603 h = (SnmpRequestTree.Handler) eh.nextElement(); 604 605 meta = handlers.getMetaNode(h); 609 610 final int depth = handlers.getOidDepth(h); 612 613 for (Enumeration rqs=handlers.getSubRequests(h); 614 rqs.hasMoreElements();) { 615 616 meta.set((SnmpMibSubRequest)rqs.nextElement(),depth); 618 } 619 } 620 } 621 622 628 public void check(SnmpMibRequest req) throws SnmpStatusException { 632 633 final int reqType = SnmpDefinitions.pduWalkRequest; 634 SnmpRequestTree handlers = getHandlers(req,true,true,reqType); 637 638 SnmpRequestTree.Handler h = null; 639 SnmpMibNode meta = null; 640 641 if (isDebugOn()) 642 debug("check","Processing handlers for CHECK... "); 643 644 for (Enumeration eh=handlers.getHandlers();eh.hasMoreElements();) { 647 h = (SnmpRequestTree.Handler) eh.nextElement(); 648 649 meta = handlers.getMetaNode(h); 653 654 final int depth = handlers.getOidDepth(h); 656 657 for (Enumeration rqs=handlers.getSubRequests(h); 658 rqs.hasMoreElements();) { 659 660 meta.check((SnmpMibSubRequest)rqs.nextElement(),depth); 662 } 663 } 664 665 if (req instanceof SnmpMibRequestImpl) { 669 ((SnmpMibRequestImpl)req).setRequestTree(handlers); 670 } 671 672 } 673 674 678 public void getNext(SnmpMibRequest req) throws SnmpStatusException { 682 SnmpRequestTree handlers = getGetNextHandlers(req); 685 686 SnmpRequestTree.Handler h = null; 687 SnmpMibNode meta = null; 688 689 if (isDebugOn()) 690 debug("getNext","Processing handlers for GET-NEXT... "); 691 692 for (Enumeration eh=handlers.getHandlers();eh.hasMoreElements();) { 694 h = (SnmpRequestTree.Handler) eh.nextElement(); 695 696 meta = handlers.getMetaNode(h); 700 701 int depth = handlers.getOidDepth(h); 703 704 for (Enumeration rqs=handlers.getSubRequests(h); 705 rqs.hasMoreElements();) { 706 707 meta.get((SnmpMibSubRequest)rqs.nextElement(),depth); 709 } 710 } 711 } 712 713 714 720 public void getBulk(SnmpMibRequest req, int nonRepeat, int maxRepeat) 724 throws SnmpStatusException { 725 726 getBulkWithGetNext(req, nonRepeat, maxRepeat); 727 } 728 729 737 public long[] getRootOid() { 738 739 if( rootOid == null) { 740 Vector list= new Vector (10); 741 742 root.getRootOid(list); 745 746 rootOid= new long[list.size()]; 749 int i=0; 750 for(Enumeration e= list.elements(); e.hasMoreElements(); ) { 751 Integer val= (Integer ) e.nextElement(); 752 rootOid[i++]= val.longValue(); 753 } 754 } 755 return rootOid; 756 757 } 758 759 763 778 private SnmpRequestTree getHandlers(SnmpMibRequest req, 779 boolean createflag, boolean atomic, 780 int type) 781 throws SnmpStatusException { 782 783 SnmpRequestTree handlers = 785 new SnmpRequestTree(req,createflag,type); 786 787 int index=0; 788 SnmpVarBind var = null; 789 final int ver= req.getVersion(); 790 791 for (Enumeration e= req.getElements(); e.hasMoreElements(); index++) { 793 794 var= (SnmpVarBind) e.nextElement(); 795 796 try { 797 root.findHandlingNode(var,var.oid.longValue(false), 799 0,handlers); 800 } catch(SnmpStatusException x) { 801 802 if (isDebugOn()) 803 debug("getHandlers","Couldn't find a handling node for " 804 + var.oid.toString()); 805 806 if (ver == SnmpDefinitions.snmpVersionOne) { 810 811 if (isDebugOn()) 812 debug("getHandlers","\tV1: Throwing exception"); 813 814 final SnmpStatusException sse = 818 new SnmpStatusException(x, index + 1); 819 sse.initCause(x); 820 throw sse; 821 } else if ((type == SnmpDefinitions.pduWalkRequest) || 822 (type == SnmpDefinitions.pduSetRequestPdu)) { 823 final int status = 824 SnmpRequestTree.mapSetException(x.getStatus(),ver); 825 826 if (isDebugOn()) 827 debug("getHandlers","\tSET: Throwing exception"); 828 829 final SnmpStatusException sse = 830 new SnmpStatusException(status, index + 1); 831 sse.initCause(x); 832 throw sse; 833 } else if (atomic) { 834 835 if (isDebugOn()) 837 debug("getHandlers","\tATOMIC: Throwing exception"); 838 839 final SnmpStatusException sse = 840 new SnmpStatusException(x, index + 1); 841 sse.initCause(x); 842 throw sse; 843 } 844 845 final int status = 846 SnmpRequestTree.mapGetException(x.getStatus(),ver); 847 848 if (status == SnmpStatusException.noSuchInstance) { 849 850 if (isDebugOn()) 851 debug("getHandlers", 852 "\tGET: Registering noSuchInstance"); 853 854 var.value= SnmpVarBind.noSuchInstance; 855 856 } else if (status == SnmpStatusException.noSuchObject) { 857 858 if (isDebugOn()) 859 debug("getHandlers", 860 "\tGET: Registering noSuchObject"); 861 862 var.value= SnmpVarBind.noSuchObject; 863 864 } else { 865 866 if (isDebugOn()) 867 debug("getHandlers", 868 "\tGET: Registering global error: " 869 + status); 870 871 final SnmpStatusException sse = 872 new SnmpStatusException(status, index + 1); 873 sse.initCause(x); 874 throw sse; 875 } 876 } 877 } 878 return handlers; 879 } 880 881 893 private SnmpRequestTree getGetNextHandlers(SnmpMibRequest req) 894 throws SnmpStatusException { 895 896 SnmpRequestTree handlers = new 898 SnmpRequestTree(req,false,SnmpDefinitions.pduGetNextRequestPdu); 899 900 handlers.setGetNextFlag(); 903 904 if (isDebugOn()) 905 debug("getGetNextHandlers","Received MIB request : " + req); 906 AcmChecker checker = new AcmChecker(req); 907 int index=0; 908 SnmpVarBind var = null; 909 final int ver= req.getVersion(); 910 SnmpOid original = null; 911 for (Enumeration e= req.getElements(); e.hasMoreElements(); index++) { 917 918 var = (SnmpVarBind) e.nextElement(); 919 SnmpOid result = null; 920 try { 921 if (isDebugOn()) 925 debug("getGetNextHandlers"," Next Oid of :" + var.oid); 926 result = new SnmpOid(root.findNextHandlingNode 927 (var,var.oid.longValue(false),0, 928 0,handlers, checker)); 929 930 if (isDebugOn()) 931 debug("getGetNextHandlers"," is :" + result); 932 var.oid = result; 935 } catch(SnmpStatusException x) { 936 937 942 if (ver == SnmpDefinitions.snmpVersionOne) { 943 if (isDebugOn()) 944 debug("getGetNextHandlers","\tThrowing exception" + 945 x.toString()); 946 throw new SnmpStatusException(x, index + 1); 950 } 951 if (isDebugOn()) 952 debug("getGetNextHandlers","Exception : " + x.getStatus()); 953 954 var.setSnmpValue(SnmpVarBind.endOfMibView); 955 } 956 } 957 return handlers; 958 } 959 960 private final static boolean isDebugOn() { 962 return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_ADAPTOR_SNMP); 963 } 964 965 private final static void debug(String func, String info) { 967 Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_ADAPTOR_SNMP, 968 "SnmpMib", func, info); 969 } 970 971 975 979 protected SnmpMibOid root; 980 981 982 986 989 private transient long[] rootOid= null; 990 } 991 | Popular Tags |