openhab / openhab1-addons

Add-ons for openHAB 1.x
Eclipse Public License 2.0
3.43k stars 1.71k forks source link

snmp: regex which checks on a valid community string is too restrictive #5848

Closed GruensFroeschli closed 4 years ago

GruensFroeschli commented 5 years ago

Expected Behavior

When setting a community string containing a dash (-), I expect it to be possible to read values from a device.

Current Behavior

In the file SnmpGenericBindingProvider.java: The regex checks on a valid community check with the pattern:

([0-9.a-zA-Z]+)

This disallows any community string containing other characters.

Possible Solution

Easiest would be to just extend the regex to allow any character. Strictly by RFC there is no limitation on the characters allowed. See RFC1901 [1] and RFC1157 [2] However a google-search suggest that there are some characters which should be avoided: See [3] and [4]

Steps to Reproduce (for bugs)

Configure an snmp item with a community containing a -. Example: Number LinkWaechterCombined "RSSI Combined" (LinkWaechterGartenhaus) { snmp="<[10.0.9.243:v2c:admin-community:.1.3.6.1.4.1.41524.1.1.6.5.8.1.23.0:10000]" }

Proposal:

Adjust the regex to allow any character except:

#
/
\
@

[1] https://www.ietf.org/rfc/rfc1901.txt [2] https://www.ietf.org/rfc/rfc1157.txt [3] https://community.cisco.com/t5/network-management/snmp-community-name-limitations/td-p/463305 [4] https://comm.support.ca.com/kb/what-special-characters-are-allowed-to-be-used-by-snmpv1-or-snmpv2c-community-strings/kb000046030

GruensFroeschli commented 5 years ago

Not having received any feedback, is it ok to send a pullrequest which changes the regex to: ([^@\\\/#]+) ?

GruensFroeschli commented 4 years ago

6 month ping.

9037568 commented 4 years ago

I think you'd be better off using the OH2 SNMP binding, unless there's a reason you can't.

I also don't think the suggested replacement regex works, as it would appear to fail for values like:

abcd:123
GruensFroeschli commented 4 years ago

Thank you for the response.

The proposed regex actually does match the string abcd:123. Originally tested with https://pythex.org/?regex=(%5B%5E%40%5C%5C%5C%2F%23%5D%2B)&test_string=abcd%3A123&ignorecase=0&multiline=0&dotall=0&verbose=0 And tested against java with https://www.freeformatter.com/java-regex-tester.html#ad-output

However i wasn't aware of an OH2 SNMP binding. It didn't exist when i was setting this up. As far as I'm concerned this issue can be closed and i will migrate to the OH2 snmp binding.