Search in sources :

Example 1 with UsmUser

use of org.snmp4j.security.UsmUser in project mysql_perf_analyzer by yahoo.

the class SNMPClient method getTargetV3.

private Target getTargetV3() {
    //logger.info("Use SNMP v3, "+this.privacyprotocol +"="+this.password+", "+this.privacyprotocol+"="+this.privacypassphrase);
    OID authOID = AuthMD5.ID;
    if ("SHA".equals(this.authprotocol))
        authOID = AuthSHA.ID;
    OID privOID = PrivDES.ID;
    if (this.privacyprotocol == null || this.privacyprotocol.isEmpty())
        privOID = null;
    UsmUser user = new UsmUser(new OctetString(this.username), //auth
    authOID, //auth
    new OctetString(this.password), privOID, //enc
    this.privacypassphrase != null ? new OctetString(this.privacypassphrase) : null);
    snmp.getUSM().addUser(new OctetString(this.username), user);
    Address targetAddress = GenericAddress.parse(address);
    UserTarget target = new UserTarget();
    target.setAddress(targetAddress);
    target.setRetries(2);
    target.setTimeout(1500);
    target.setVersion(this.getVersionInt());
    if (privOID != null)
        target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
    else
        target.setSecurityLevel(SecurityLevel.AUTH_NOPRIV);
    target.setSecurityName(new OctetString(this.username));
    return target;
}
Also used : OctetString(org.snmp4j.smi.OctetString) Address(org.snmp4j.smi.Address) GenericAddress(org.snmp4j.smi.GenericAddress) OID(org.snmp4j.smi.OID) UserTarget(org.snmp4j.UserTarget) UsmUser(org.snmp4j.security.UsmUser)

Example 2 with UsmUser

use of org.snmp4j.security.UsmUser in project opennms by OpenNMS.

the class Snmp4JAgentConfig method createSnmpSession.

public Snmp createSnmpSession() throws IOException {
    final TransportMapping<?> transport = new DefaultUdpTransportMapping();
    final MessageDispatcher disp = new MessageDispatcherImpl();
    final Snmp session;
    // models we need for the specific agent
    if (!isSnmpV3()) {
        disp.addMessageProcessingModel(new MPv1());
        disp.addMessageProcessingModel(new MPv2c());
        session = new Snmp(disp, transport);
    } else {
        // Make a new USM
        final USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
        // Add the specified user to the USM
        usm.addUser(getSecurityName(), new UsmUser(getSecurityName(), getAuthProtocol(), getAuthPassPhrase(), getPrivProtocol(), getPrivPassPhrase()));
        disp.addMessageProcessingModel(new MPv3(usm));
        session = new Snmp(disp, transport);
    }
    return session;
}
Also used : OctetString(org.snmp4j.smi.OctetString) MessageDispatcher(org.snmp4j.MessageDispatcher) MPv3(org.snmp4j.mp.MPv3) MPv2c(org.snmp4j.mp.MPv2c) Snmp(org.snmp4j.Snmp) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) MessageDispatcherImpl(org.snmp4j.MessageDispatcherImpl) MPv1(org.snmp4j.mp.MPv1) UsmUser(org.snmp4j.security.UsmUser) USM(org.snmp4j.security.USM)

Example 3 with UsmUser

use of org.snmp4j.security.UsmUser in project opennms by OpenNMS.

the class MockSnmpAgent method addUsmUser.

/** {@inheritDoc} */
@Override
protected void addUsmUser(USM usm) {
    UsmUser user = new UsmUser(new OctetString("SHADES"), AuthSHA.ID, new OctetString("SHADESAuthPassword"), PrivDES.ID, new OctetString("SHADESPrivPassword"));
    usm.addUser(user.getSecurityName(), usm.getLocalEngineID(), user);
    user = new UsmUser(new OctetString("TEST"), AuthSHA.ID, new OctetString("maplesyrup"), PrivDES.ID, new OctetString("maplesyrup"));
    usm.addUser(user.getSecurityName(), usm.getLocalEngineID(), user);
    user = new UsmUser(new OctetString("opennmsUser"), AuthMD5.ID, new OctetString("0p3nNMSv3"), PrivDES.ID, new OctetString("0p3nNMSv3"));
    usm.addUser(user.getSecurityName(), usm.getLocalEngineID(), user);
    user = new UsmUser(new OctetString("SHA"), AuthSHA.ID, new OctetString("SHAAuthPassword"), null, null);
    usm.addUser(user.getSecurityName(), usm.getLocalEngineID(), user);
}
Also used : OctetString(org.snmp4j.smi.OctetString) UsmUser(org.snmp4j.security.UsmUser)

Example 4 with UsmUser

use of org.snmp4j.security.UsmUser in project camel by apache.

the class SnmpOIDPoller method doStart.

@Override
protected void doStart() throws Exception {
    super.doStart();
    this.targetAddress = GenericAddress.parse(this.endpoint.getAddress());
    // either tcp or udp
    if ("tcp".equals(endpoint.getProtocol())) {
        this.transport = new DefaultTcpTransportMapping();
    } else if ("udp".equals(endpoint.getProtocol())) {
        this.transport = new DefaultUdpTransportMapping();
    } else {
        throw new IllegalArgumentException("Unknown protocol: " + endpoint.getProtocol());
    }
    this.snmp = new Snmp(this.transport);
    if (SnmpConstants.version3 == endpoint.getSnmpVersion()) {
        UserTarget userTarget = new UserTarget();
        userTarget.setSecurityLevel(endpoint.getSecurityLevel());
        userTarget.setSecurityName(convertToOctetString(endpoint.getSecurityName()));
        userTarget.setAddress(targetAddress);
        userTarget.setRetries(endpoint.getRetries());
        userTarget.setTimeout(endpoint.getTimeout());
        userTarget.setVersion(endpoint.getSnmpVersion());
        this.target = userTarget;
        USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
        SecurityModels.getInstance().addSecurityModel(usm);
        OID authProtocol = convertAuthenticationProtocol(endpoint.getAuthenticationProtocol());
        OctetString authPwd = convertToOctetString(endpoint.getAuthenticationPassphrase());
        OID privProtocol = convertPrivacyProtocol(endpoint.getPrivacyProtocol());
        OctetString privPwd = convertToOctetString(endpoint.getPrivacyPassphrase());
        UsmUser user = new UsmUser(convertToOctetString(endpoint.getSecurityName()), authProtocol, authPwd, privProtocol, privPwd);
        usm.addUser(convertToOctetString(endpoint.getSecurityName()), user);
        ScopedPDU scopedPDU = new ScopedPDU();
        if (endpoint.getSnmpContextEngineId() != null) {
            scopedPDU.setContextEngineID(new OctetString(endpoint.getSnmpContextEngineId()));
        }
        if (endpoint.getSnmpContextName() != null) {
            scopedPDU.setContextName(new OctetString(endpoint.getSnmpContextName()));
        }
        this.pdu = scopedPDU;
    } else {
        CommunityTarget communityTarget = new CommunityTarget();
        communityTarget.setCommunity(convertToOctetString(endpoint.getSnmpCommunity()));
        communityTarget.setAddress(targetAddress);
        communityTarget.setRetries(endpoint.getRetries());
        communityTarget.setTimeout(endpoint.getTimeout());
        communityTarget.setVersion(endpoint.getSnmpVersion());
        this.target = communityTarget;
        this.pdu = new PDU();
    }
    // listen to the transport
    if (LOG.isDebugEnabled()) {
        LOG.debug("Starting OID poller on {} using {} protocol", endpoint.getAddress(), endpoint.getProtocol());
    }
    this.transport.listen();
    if (LOG.isInfoEnabled()) {
        LOG.info("Started OID poller on {} using {} protocol", endpoint.getAddress(), endpoint.getProtocol());
    }
}
Also used : OctetString(org.snmp4j.smi.OctetString) ScopedPDU(org.snmp4j.ScopedPDU) PDU(org.snmp4j.PDU) ScopedPDU(org.snmp4j.ScopedPDU) DefaultTcpTransportMapping(org.snmp4j.transport.DefaultTcpTransportMapping) Snmp(org.snmp4j.Snmp) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) OID(org.snmp4j.smi.OID) UserTarget(org.snmp4j.UserTarget) UsmUser(org.snmp4j.security.UsmUser) CommunityTarget(org.snmp4j.CommunityTarget) USM(org.snmp4j.security.USM)

Example 5 with UsmUser

use of org.snmp4j.security.UsmUser in project opennms by OpenNMS.

the class Snmp4JStrategy method registerForTraps.

@Override
public void registerForTraps(final TrapNotificationListener listener, InetAddress address, int snmpTrapPort, List<SnmpV3User> snmpUsers) throws IOException {
    final RegistrationInfo info = new RegistrationInfo(listener, address, snmpTrapPort);
    final Snmp4JTrapNotifier trapNotifier = new Snmp4JTrapNotifier(listener);
    info.setHandler(trapNotifier);
    final UdpAddress udpAddress;
    if (address == null) {
        udpAddress = new UdpAddress(snmpTrapPort);
    } else {
        udpAddress = new UdpAddress(address, snmpTrapPort);
    }
    // Set socket option SO_REUSEADDR so that we can bind to the port even if it
    // has recently been closed by passing 'true' as the second argument here.
    final DefaultUdpTransportMapping transport = new DefaultUdpTransportMapping(udpAddress, true);
    // Increase the receive buffer for the socket
    LOG.debug("Attempting to set receive buffer size to {}", Integer.MAX_VALUE);
    transport.setReceiveBufferSize(Integer.MAX_VALUE);
    LOG.debug("Actual receive buffer size is {}", transport.getReceiveBufferSize());
    info.setTransportMapping(transport);
    Snmp snmp = new Snmp(transport);
    snmp.addCommandResponder(trapNotifier);
    if (snmpUsers != null) {
        for (SnmpV3User user : snmpUsers) {
            SnmpAgentConfig config = new SnmpAgentConfig();
            config.setVersion(SnmpConfiguration.VERSION3);
            config.setSecurityName(user.getSecurityName());
            config.setAuthProtocol(user.getAuthProtocol());
            config.setAuthPassPhrase(user.getAuthPassPhrase());
            config.setPrivProtocol(user.getPrivProtocol());
            config.setPrivPassPhrase(user.getPrivPassPhrase());
            Snmp4JAgentConfig agentConfig = new Snmp4JAgentConfig(config);
            UsmUser usmUser = new UsmUser(agentConfig.getSecurityName(), agentConfig.getAuthProtocol(), agentConfig.getAuthPassPhrase(), agentConfig.getPrivProtocol(), agentConfig.getPrivPassPhrase());
            /* This doesn't work as expected. Basically SNMP4J is ignoring the engineId
                if (user.getEngineId() == null) {
                    snmp.getUSM().addUser(agentConfig.getSecurityName(), usmUser);
                } else {
                    snmp.getUSM().addUser(agentConfig.getSecurityName(), new OctetString(user.getEngineId()), usmUser);
                }
                */
            snmp.getUSM().addUser(agentConfig.getSecurityName(), usmUser);
        }
    }
    info.setSession(snmp);
    s_registrations.put(listener, info);
    snmp.listen();
}
Also used : SnmpAgentConfig(org.opennms.netmgt.snmp.SnmpAgentConfig) UdpAddress(org.snmp4j.smi.UdpAddress) Snmp(org.snmp4j.Snmp) DefaultUdpTransportMapping(org.snmp4j.transport.DefaultUdpTransportMapping) UsmUser(org.snmp4j.security.UsmUser) SnmpV3User(org.opennms.netmgt.snmp.SnmpV3User)

Aggregations

UsmUser (org.snmp4j.security.UsmUser)9 OctetString (org.snmp4j.smi.OctetString)8 Snmp (org.snmp4j.Snmp)7 DefaultUdpTransportMapping (org.snmp4j.transport.DefaultUdpTransportMapping)7 UserTarget (org.snmp4j.UserTarget)5 USM (org.snmp4j.security.USM)5 UdpAddress (org.snmp4j.smi.UdpAddress)5 PDU (org.snmp4j.PDU)4 ScopedPDU (org.snmp4j.ScopedPDU)4 ResponseEvent (org.snmp4j.event.ResponseEvent)3 IOException (java.io.IOException)2 OID (org.snmp4j.smi.OID)2 Test (org.junit.Test)1 SnmpAgentConfig (org.opennms.netmgt.snmp.SnmpAgentConfig)1 SnmpV3User (org.opennms.netmgt.snmp.SnmpV3User)1 CommunityTarget (org.snmp4j.CommunityTarget)1 MessageDispatcher (org.snmp4j.MessageDispatcher)1 MessageDispatcherImpl (org.snmp4j.MessageDispatcherImpl)1 MPv1 (org.snmp4j.mp.MPv1)1 MPv2c (org.snmp4j.mp.MPv2c)1