22
22
import org .eclipse .jdt .annotation .NonNullByDefault ;
23
23
import org .eclipse .jdt .annotation .Nullable ;
24
24
import org .openhab .binding .snmp .internal .config .SnmpServiceConfiguration ;
25
- import org .openhab .binding .snmp .internal .types .SnmpAuthProtocol ;
26
- import org .openhab .binding .snmp .internal .types .SnmpPrivProtocol ;
27
25
import org .openhab .core .config .core .Configuration ;
28
26
import org .osgi .service .component .annotations .Activate ;
29
27
import org .osgi .service .component .annotations .Component ;
37
35
import org .snmp4j .Target ;
38
36
import org .snmp4j .event .ResponseListener ;
39
37
import org .snmp4j .mp .MPv3 ;
38
+ import org .snmp4j .security .AuthHMAC128SHA224 ;
39
+ import org .snmp4j .security .AuthHMAC192SHA256 ;
40
+ import org .snmp4j .security .AuthHMAC256SHA384 ;
41
+ import org .snmp4j .security .AuthHMAC384SHA512 ;
42
+ import org .snmp4j .security .AuthMD5 ;
43
+ import org .snmp4j .security .AuthSHA ;
40
44
import org .snmp4j .security .Priv3DES ;
45
+ import org .snmp4j .security .PrivAES128 ;
46
+ import org .snmp4j .security .PrivAES192 ;
47
+ import org .snmp4j .security .PrivAES256 ;
48
+ import org .snmp4j .security .PrivDES ;
41
49
import org .snmp4j .security .SecurityModels ;
42
50
import org .snmp4j .security .SecurityProtocols ;
43
51
import org .snmp4j .security .USM ;
44
52
import org .snmp4j .security .UsmUser ;
53
+ import org .snmp4j .smi .Address ;
45
54
import org .snmp4j .smi .OctetString ;
46
55
import org .snmp4j .smi .UdpAddress ;
47
56
import org .snmp4j .transport .DefaultUdpTransportMapping ;
58
67
public class SnmpServiceImpl implements SnmpService {
59
68
private final Logger logger = LoggerFactory .getLogger (SnmpServiceImpl .class );
60
69
61
- private @ NonNullByDefault ({}) SnmpServiceConfiguration config ;
62
70
private @ Nullable Snmp snmp ;
63
71
private @ Nullable DefaultUdpTransportMapping transport ;
64
72
@@ -67,9 +75,7 @@ public class SnmpServiceImpl implements SnmpService {
67
75
68
76
@ Activate
69
77
public SnmpServiceImpl (Map <String , Object > config ) {
70
- SecurityProtocols .getInstance ().addDefaultProtocols ();
71
- SecurityProtocols .getInstance ().addPrivacyProtocol (new Priv3DES ());
72
-
78
+ addProtocols ();
73
79
OctetString localEngineId = new OctetString (MPv3 .createLocalEngineID ());
74
80
USM usm = new USM (SecurityProtocols .getInstance (), localEngineId , 0 );
75
81
SecurityModels .getInstance ().addSecurityModel (usm );
@@ -79,34 +85,33 @@ public SnmpServiceImpl(Map<String, Object> config) {
79
85
80
86
@ Modified
81
87
protected void modified (Map <String , Object > config ) {
82
- this . config = new Configuration (config ).as (SnmpServiceConfiguration .class );
88
+ SnmpServiceConfiguration snmpCfg = new Configuration (config ).as (SnmpServiceConfiguration .class );
83
89
try {
84
90
shutdownSnmp ();
85
91
86
92
final DefaultUdpTransportMapping transport ;
87
93
88
- if (this . config .port > 0 ) {
89
- transport = new DefaultUdpTransportMapping (new UdpAddress (this . config .port ), true );
94
+ if (snmpCfg .port > 0 ) {
95
+ transport = new DefaultUdpTransportMapping (new UdpAddress (snmpCfg .port ), true );
90
96
} else {
91
97
transport = new DefaultUdpTransportMapping ();
92
98
}
93
99
94
- SecurityProtocols .getInstance ().addDefaultProtocols ();
95
- SecurityProtocols .getInstance ().addPrivacyProtocol (new Priv3DES ());
100
+ addProtocols ();
96
101
97
102
final Snmp snmp = new Snmp (transport );
98
103
listeners .forEach (snmp ::addCommandResponder );
99
104
snmp .listen ();
100
105
101
106
// re-add user entries
102
- userEntries .forEach (u -> addUser (snmp , u ));
107
+ userEntries .forEach (u -> snmp . getUSM (). addUser (u . user , u . engineId ));
103
108
104
109
this .snmp = snmp ;
105
110
this .transport = transport ;
106
111
107
112
logger .debug ("initialized SNMP at {}" , transport .getAddress ());
108
113
} catch (IOException e ) {
109
- logger .warn ("could not open SNMP instance on port {}: {}" , this . config .port , e .getMessage ());
114
+ logger .warn ("could not open SNMP instance on port {}: {}" , snmpCfg .port , e .getMessage ());
110
115
}
111
116
}
112
117
@@ -120,6 +125,21 @@ public void deactivate() {
120
125
}
121
126
}
122
127
128
+ private void addProtocols () {
129
+ SecurityProtocols secProtocols = SecurityProtocols .getInstance ();
130
+ secProtocols .addAuthenticationProtocol (new AuthMD5 ());
131
+ secProtocols .addAuthenticationProtocol (new AuthSHA ());
132
+ secProtocols .addAuthenticationProtocol (new AuthHMAC128SHA224 ());
133
+ secProtocols .addAuthenticationProtocol (new AuthHMAC192SHA256 ());
134
+ secProtocols .addAuthenticationProtocol (new AuthHMAC256SHA384 ());
135
+ secProtocols .addAuthenticationProtocol (new AuthHMAC384SHA512 ());
136
+ secProtocols .addPrivacyProtocol (new PrivDES ());
137
+ secProtocols .addPrivacyProtocol (new Priv3DES ());
138
+ secProtocols .addPrivacyProtocol (new PrivAES128 ());
139
+ secProtocols .addPrivacyProtocol (new PrivAES192 ());
140
+ secProtocols .addPrivacyProtocol (new PrivAES256 ());
141
+ }
142
+
123
143
private void shutdownSnmp () throws IOException {
124
144
DefaultUdpTransportMapping transport = this .transport ;
125
145
if (transport != null ) {
@@ -152,7 +172,7 @@ public void removeCommandResponder(CommandResponder listener) {
152
172
}
153
173
154
174
@ Override
155
- public void send (PDU pdu , Target target , @ Nullable Object userHandle , ResponseListener listener )
175
+ public void send (PDU pdu , Target <?> target , @ Nullable Object userHandle , ResponseListener listener )
156
176
throws IOException {
157
177
Snmp snmp = this .snmp ;
158
178
if (snmp != null ) {
@@ -164,35 +184,40 @@ public void send(PDU pdu, Target target, @Nullable Object userHandle, ResponseLi
164
184
}
165
185
166
186
@ Override
167
- public void addUser (String userName , SnmpAuthProtocol snmpAuthProtocol , @ Nullable String authPassphrase ,
168
- SnmpPrivProtocol snmpPrivProtocol , @ Nullable String privPassphrase , byte [] engineId ) {
169
- UsmUser usmUser = new UsmUser (new OctetString (userName ),
170
- authPassphrase != null ? snmpAuthProtocol .getOid () : null ,
171
- authPassphrase != null ? new OctetString (authPassphrase ) : null ,
172
- privPassphrase != null ? snmpPrivProtocol .getOid () : null ,
173
- privPassphrase != null ? new OctetString (privPassphrase ) : null );
174
- OctetString securityNameOctets = new OctetString (userName );
175
-
176
- UserEntry userEntry = new UserEntry (securityNameOctets , new OctetString (engineId ), usmUser );
187
+ public void addUser (UsmUser user , OctetString engineId ) {
188
+ UserEntry userEntry = new UserEntry (user , engineId );
177
189
userEntries .add (userEntry );
178
190
179
191
Snmp snmp = this .snmp ;
180
192
if (snmp != null ) {
181
- addUser (snmp , userEntry );
193
+ snmp . getUSM (). addUser (user , engineId );
182
194
}
183
195
}
184
196
185
- private static void addUser (Snmp snmp , UserEntry userEntry ) {
186
- snmp .getUSM ().addUser (userEntry .securityName , userEntry .engineId , userEntry .user );
197
+ @ Override
198
+ public void removeUser (Address address , UsmUser user , OctetString engineId ) {
199
+ Snmp snmp = this .snmp ;
200
+ if (snmp != null ) {
201
+ snmp .getUSM ().removeAllUsers (user .getSecurityName (), engineId );
202
+ snmp .removeCachedContextEngineId (address );
203
+ }
204
+ userEntries .removeIf (e -> e .engineId .equals (engineId ) && e .user .equals (user ));
205
+ }
206
+
207
+ @ Override
208
+ public byte @ Nullable [] getEngineId (Address address ) {
209
+ Snmp snmp = this .snmp ;
210
+ if (snmp != null ) {
211
+ return snmp .discoverAuthoritativeEngineID (address , 15000 );
212
+ }
213
+ return null ;
187
214
}
188
215
189
216
private static class UserEntry {
190
- public OctetString securityName ;
191
217
public OctetString engineId ;
192
218
public UsmUser user ;
193
219
194
- public UserEntry (OctetString securityName , OctetString engineId , UsmUser user ) {
195
- this .securityName = securityName ;
220
+ public UserEntry (UsmUser user , OctetString engineId ) {
196
221
this .engineId = engineId ;
197
222
this .user = user ;
198
223
}
0 commit comments