Skip to content

Commit

Permalink
SNMPv3 polling support (formatted @eczema's work)
Browse files Browse the repository at this point in the history
  • Loading branch information
eczema authored and SCadilhac committed Nov 18, 2018
1 parent 0ae3906 commit d9247cd
Show file tree
Hide file tree
Showing 16 changed files with 783 additions and 26 deletions.
304 changes: 304 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/main/java/onl/netfishers/netshot/Database.java
Expand Up @@ -66,6 +66,7 @@
import onl.netfishers.netshot.device.attribute.LongTextConfiguration;
import onl.netfishers.netshot.device.credentials.DeviceSnmpv1Community;
import onl.netfishers.netshot.device.credentials.DeviceSnmpv2cCommunity;
import onl.netfishers.netshot.device.credentials.DeviceSnmpv3Community;
import onl.netfishers.netshot.device.credentials.DeviceSshAccount;
import onl.netfishers.netshot.device.credentials.DeviceSshKeyAccount;
import onl.netfishers.netshot.device.credentials.DeviceTelnetAccount;
Expand Down Expand Up @@ -274,6 +275,7 @@ public static void init() {
.addAnnotatedClass(NetworkInterface.class)
.addAnnotatedClass(DeviceSnmpv1Community.class)
.addAnnotatedClass(DeviceSnmpv2cCommunity.class)
.addAnnotatedClass(DeviceSnmpv3Community.class)
.addAnnotatedClass(DeviceSshAccount.class)
.addAnnotatedClass(DeviceSshKeyAccount.class)
.addAnnotatedClass(DeviceTelnetAccount.class)
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/onl/netfishers/netshot/RestService.java
Expand Up @@ -109,6 +109,7 @@
import onl.netfishers.netshot.device.credentials.DeviceCliAccount;
import onl.netfishers.netshot.device.credentials.DeviceCredentialSet;
import onl.netfishers.netshot.device.credentials.DeviceSnmpCommunity;
import onl.netfishers.netshot.device.credentials.DeviceSnmpv3Community;
import onl.netfishers.netshot.device.credentials.DeviceSshKeyAccount;
import onl.netfishers.netshot.work.DebugLog;
import onl.netfishers.netshot.work.Task;
Expand Down Expand Up @@ -3097,6 +3098,14 @@ public DeviceCredentialSet setCredentialSet(@PathParam("id") Long id,
((DeviceSshKeyAccount) cliAccount).setPrivateKey(((DeviceSshKeyAccount) rsCliAccount).getPrivateKey());
}
}
else if (DeviceSnmpv3Community.class.isInstance(credentialSet)) {
((DeviceSnmpv3Community) credentialSet).setUsername( ((DeviceSnmpv3Community) rsCredentialSet).getUsername() );
((DeviceSnmpv3Community) credentialSet).setAuthType( ((DeviceSnmpv3Community) rsCredentialSet).getAuthType() );
((DeviceSnmpv3Community) credentialSet).setAuthKey( ((DeviceSnmpv3Community) rsCredentialSet).getAuthKey() );
((DeviceSnmpv3Community) credentialSet).setPrivType( ((DeviceSnmpv3Community) rsCredentialSet).getPrivType() );
((DeviceSnmpv3Community) credentialSet).setPrivKey( ((DeviceSnmpv3Community) rsCredentialSet).getPrivKey() );

}
else if (DeviceSnmpCommunity.class.isInstance(credentialSet)) {
((DeviceSnmpCommunity) credentialSet)
.setCommunity(((DeviceSnmpCommunity) rsCredentialSet)
Expand Down
122 changes: 108 additions & 14 deletions src/main/java/onl/netfishers/netshot/device/access/Snmp.java
Expand Up @@ -23,6 +23,20 @@
import onl.netfishers.netshot.device.NetworkAddress;

import org.snmp4j.CommunityTarget;
import org.snmp4j.UserTarget;
import org.snmp4j.security.SecurityLevel;
import org.snmp4j.security.AuthMD5;
import org.snmp4j.security.AuthSHA;
import org.snmp4j.security.Priv3DES;
import org.snmp4j.security.PrivAES128;
import org.snmp4j.security.PrivAES192;
import org.snmp4j.security.PrivAES256;
import org.snmp4j.security.USM;
import org.snmp4j.ScopedPDU;
import org.snmp4j.mp.MPv3;
import org.snmp4j.security.UsmUser;
import org.snmp4j.security.SecurityProtocols;
import org.snmp4j.security.SecurityModels;
import org.snmp4j.PDU;
import org.snmp4j.Target;
import org.snmp4j.event.ResponseEvent;
Expand All @@ -34,6 +48,10 @@
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.TransportMapping;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
* A SNMP poller class, to poll data from a device via SNMP.
*/
Expand All @@ -48,6 +66,14 @@ public class Snmp extends Poller {
/** The port. */
private static int PORT = 161;

/** SNMPv3 auth protocol */
private OID authProtocol;

/** SNMPv3 priv protocol */
private OID privProtocol;

private static Logger logger = LoggerFactory.getLogger(Snmp.class);

/**
* Instantiates a new snmp.
*
Expand All @@ -63,7 +89,7 @@ public Snmp(NetworkAddress address, String community, boolean v1) throws IOExcep
}

/**
* Instantiates a new snmp.
* Instantiates a new SNMPv1/2 access.
*
* @param address the address
* @param community the community
Expand All @@ -73,15 +99,62 @@ public Snmp(NetworkAddress address, String community) throws IOException {
this(address, community, false);
}

/**
* Instantiates a new snmp.
*
* @param address the address
* @param username the username
* @param password the password
*/
public Snmp(NetworkAddress address, String username, String password) {
//TODO

public Snmp(NetworkAddress address, String username, String authType, String authKey, String privType, String privKey)
throws IOException {
// TODO
// AuthSHA.ID AuthMD5.ID
// AuthHMAC128SHA224.ID AuthHMAC192SHA256.ID AuthHMAC256SHA384.ID
// AuthHMAC384SHA512.ID
// Priv3DES.ID PrivAES128.ID PrivAES192.ID PrivAES256.ID
// AUTH_NOPRIV AUTH_PRIV NOAUTH_NOPRIV

// Prepare target
logger.debug("Prepare SNMPv3 context");
this.target = new UserTarget();
this.target.setTimeout(5000);
this.target.setVersion(SnmpConstants.version3);
this.target.setAddress(new UdpAddress(address.getInetAddress(), PORT));
if (authKey == null) {
this.target.setSecurityLevel(SecurityLevel.NOAUTH_NOPRIV);
}
else if (privKey == null) {
this.target.setSecurityLevel(SecurityLevel.AUTH_NOPRIV);
}
else {
this.target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
}
this.target.setSecurityName(new OctetString(username));

// Prepare transport
logger.debug("Auth Protocol called: {}", authType);
if (authType.equals("SHA")) {
this.authProtocol = AuthSHA.ID;
logger.debug("Using SHA Auth");
}
else {
this.authProtocol = AuthMD5.ID;
}

if (privType.equals("AES128")) {
this.privProtocol = PrivAES128.ID;
}
else if (privType.equals("AES192")) {
this.privProtocol = PrivAES192.ID;
}
else if (privType.equals("AES256")) {
this.privProtocol = PrivAES256.ID;
}
else {
this.privProtocol = Priv3DES.ID;
}

USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
usm.addUser(new OctetString(username), new UsmUser(new OctetString(username), this.authProtocol,
new OctetString(authKey), this.privProtocol, new OctetString(privKey)));
SecurityModels.getInstance().addSecurityModel(usm);

start();
}

/**
Expand Down Expand Up @@ -143,24 +216,45 @@ private PDU getPDU(OID oids[]) {
for (OID oid : oids) {
pdu.add(new VariableBinding(oid));
}

pdu.setType(PDU.GET);
return pdu;
}

/**
* Gets the.
* Gets the scoped pdu.
*
* @param oids the oids
* @return the scoped pdu
*/

private ScopedPDU getScopedPDU(OID oids[]) {
ScopedPDU scopedPdu = new ScopedPDU();
for (OID oid : oids) {
scopedPdu.add(new VariableBinding(oid));
}
scopedPdu.setType(PDU.GET);
return scopedPdu;
}

/**
* Gets the a response.
*
* @param oids the oids
* @return the response event
* @throws IOException Signals that an I/O exception has occurred.
*/
public ResponseEvent get(OID oids[]) throws IOException {
ResponseEvent event = snmp.send(getPDU(oids), target, null);
ResponseEvent event;
if (this.target.getVersion() == SnmpConstants.version3) {
event = snmp.send(getScopedPDU(oids), target, null);
}
else {
event = snmp.send(getPDU(oids), target, null);
}
if (event != null) {
return event;
}
throw new RuntimeException("SNMP Get timed out");
throw new RuntimeException("SNMP Get timed out");
}


Expand Down
Expand Up @@ -85,4 +85,4 @@ else if (!getText().equals(other.getText()))
return true;
}

}
}
Expand Up @@ -56,6 +56,7 @@
@JsonSubTypes({
@Type(value = DeviceSnmpv1Community.class, name = "SNMP v1"),
@Type(value = DeviceSnmpv2cCommunity.class, name = "SNMP v2"),
@Type(value = DeviceSnmpv3Community.class, name = "SNMP v3"),
@Type(value = DeviceSshAccount.class, name = "SSH"),
@Type(value = DeviceSshKeyAccount.class, name = "SSH Key"),
@Type(value = DeviceTelnetAccount.class, name = "Telnet")
Expand Down

0 comments on commit d9247cd

Please sign in to comment.