|
@@ -0,0 +1,183 @@
|
|
|
+package de.tudarmstadt.informatik.hostage.protocol;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import de.tudarmstadt.informatik.hostage.wrapper.Packet;
|
|
|
+
|
|
|
+import org.alfresco.jlan.server.auth.spnego.OID;
|
|
|
+import org.snmp4j.TransportMapping;
|
|
|
+import org.snmp4j.agent.BaseAgent;
|
|
|
+import org.snmp4j.agent.CommandProcessor;
|
|
|
+import org.snmp4j.agent.DuplicateRegistrationException;
|
|
|
+import org.snmp4j.agent.MOGroup;
|
|
|
+import org.snmp4j.agent.ManagedObject;
|
|
|
+import org.snmp4j.agent.mo.MOTableRow;
|
|
|
+import org.snmp4j.agent.mo.snmp.RowStatus;
|
|
|
+import org.snmp4j.agent.mo.snmp.SnmpCommunityMIB;
|
|
|
+import org.snmp4j.agent.mo.snmp.SnmpNotificationMIB;
|
|
|
+import org.snmp4j.agent.mo.snmp.SnmpTargetMIB;
|
|
|
+import org.snmp4j.agent.mo.snmp.StorageType;
|
|
|
+import org.snmp4j.agent.mo.snmp.VacmMIB;
|
|
|
+import org.snmp4j.agent.security.MutableVACM;
|
|
|
+import org.snmp4j.mp.MPv3;
|
|
|
+import org.snmp4j.security.SecurityLevel;
|
|
|
+import org.snmp4j.security.SecurityModel;
|
|
|
+import org.snmp4j.security.USM;
|
|
|
+import org.snmp4j.smi.Address;
|
|
|
+import org.snmp4j.smi.GenericAddress;
|
|
|
+import org.snmp4j.smi.Integer32;
|
|
|
+import org.snmp4j.smi.OctetString;
|
|
|
+import org.snmp4j.smi.Variable;
|
|
|
+import org.snmp4j.transport.TransportMappings;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by root on 06.07.15.
|
|
|
+ */
|
|
|
+public class SNMP extends BaseAgent implements Protocol {
|
|
|
+ @Override
|
|
|
+ public int getPort() {
|
|
|
+ return 161;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean isClosed() {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean isSecure() {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<Packet> processMessage(Packet requestPacket) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public TALK_FIRST whoTalksFirst() {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String toString(){
|
|
|
+ return "SNMP";
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private String address;
|
|
|
+
|
|
|
+ public SNMP(String address) throws IOException {
|
|
|
+
|
|
|
+ super(new File("conf.agent"), new File("bootCounter.agent"),
|
|
|
+ new CommandProcessor(
|
|
|
+ new OctetString(MPv3.createLocalEngineID())));
|
|
|
+ this.address = address;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void registerManagedObjects() {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void unregisterManagedObjects() {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void addUsmUser(USM usm) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void addNotificationTargets(SnmpTargetMIB snmpTargetMIB, SnmpNotificationMIB snmpNotificationMIB) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void addViews(VacmMIB vacmMIB) {
|
|
|
+
|
|
|
+ vacmMIB.addGroup(SecurityModel.SECURITY_MODEL_SNMPv2c, new OctetString("cpublic"), new OctetString("v1v2group"), StorageType.nonVolatile);
|
|
|
+
|
|
|
+ vacmMIB.addAccess(new OctetString("v1v2group"), new OctetString("public"),
|
|
|
+ SecurityModel.SECURITY_MODEL_ANY, SecurityLevel.NOAUTH_NOPRIV,
|
|
|
+ MutableVACM.VACM_MATCH_EXACT, new OctetString("fullReadView"),
|
|
|
+ new OctetString("fullWriteView"), new OctetString("fullNotifyView"), StorageType.nonVolatile);
|
|
|
+
|
|
|
+ vacmMIB.addViewTreeFamily(new OctetString("fullReadView"), new org.snmp4j.smi.OID("1.3"),
|
|
|
+ new OctetString(), VacmMIB.vacmViewIncluded, StorageType.nonVolatile);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void addCommunities(SnmpCommunityMIB snmpCommunityMIB) {
|
|
|
+
|
|
|
+ Variable[] com2sec = new Variable[] { new OctetString("public"),
|
|
|
+
|
|
|
+ new OctetString("cpublic"), // security name
|
|
|
+
|
|
|
+ getAgent().getContextEngineID(), // local engine ID
|
|
|
+
|
|
|
+ new OctetString("public"), // default context name
|
|
|
+
|
|
|
+ new OctetString(), // transport tag
|
|
|
+
|
|
|
+ new Integer32(StorageType.nonVolatile), // storage type
|
|
|
+
|
|
|
+ new Integer32(RowStatus.active) // row status
|
|
|
+
|
|
|
+ };
|
|
|
+
|
|
|
+ MOTableRow row = snmpCommunityMIB.getSnmpCommunityEntry().createRow(
|
|
|
+
|
|
|
+ new OctetString("public2public").toSubIndex(true), com2sec);
|
|
|
+
|
|
|
+ snmpCommunityMIB.getSnmpCommunityEntry().addRow((SnmpCommunityMIB.SnmpCommunityEntryRow) row);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void start() throws IOException {
|
|
|
+
|
|
|
+ init();
|
|
|
+ // This method reads some old config from a file and causes
|
|
|
+ // unexpected behavior.
|
|
|
+ // loadConfig(ImportModes.REPLACE_CREATE);
|
|
|
+ addShutdownHook();
|
|
|
+ getServer().addContext(new OctetString("public"));
|
|
|
+ finishInit();
|
|
|
+ run();
|
|
|
+ sendColdStartNotification();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ protected void initTransportMappings() throws IOException {
|
|
|
+ transportMappings = new TransportMapping[1];
|
|
|
+ Address addr = GenericAddress.parse(address);
|
|
|
+
|
|
|
+ TransportMapping tm = TransportMappings.getInstance()
|
|
|
+ .createTransportMapping(addr);
|
|
|
+ transportMappings[0] = tm;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public void registerManagedObject(ManagedObject mo) {
|
|
|
+ try {
|
|
|
+ server.register(mo, null);
|
|
|
+ } catch (DuplicateRegistrationException ex) {
|
|
|
+
|
|
|
+ throw new RuntimeException(ex);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public void unregisterManagedObject(MOGroup moGroup){
|
|
|
+ moGroup.unregisterMOs(server,getContext(moGroup));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|