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 ;
25
27
import org .openhab .core .config .core .Configuration ;
26
28
import org .osgi .service .component .annotations .Activate ;
27
29
import org .osgi .service .component .annotations .Component ;
35
37
import org .snmp4j .Target ;
36
38
import org .snmp4j .event .ResponseListener ;
37
39
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 ;
44
40
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 ;
49
41
import org .snmp4j .security .SecurityModels ;
50
42
import org .snmp4j .security .SecurityProtocols ;
51
43
import org .snmp4j .security .USM ;
52
44
import org .snmp4j .security .UsmUser ;
53
- import org .snmp4j .smi .Address ;
54
45
import org .snmp4j .smi .OctetString ;
55
46
import org .snmp4j .smi .UdpAddress ;
56
47
import org .snmp4j .transport .DefaultUdpTransportMapping ;
67
58
public class SnmpServiceImpl implements SnmpService {
68
59
private final Logger logger = LoggerFactory .getLogger (SnmpServiceImpl .class );
69
60
61
+ private @ NonNullByDefault ({}) SnmpServiceConfiguration config ;
70
62
private @ Nullable Snmp snmp ;
71
63
private @ Nullable DefaultUdpTransportMapping transport ;
72
64
@@ -75,7 +67,9 @@ public class SnmpServiceImpl implements SnmpService {
75
67
76
68
@ Activate
77
69
public SnmpServiceImpl (Map <String , Object > config ) {
78
- addProtocols ();
70
+ SecurityProtocols .getInstance ().addDefaultProtocols ();
71
+ SecurityProtocols .getInstance ().addPrivacyProtocol (new Priv3DES ());
72
+
79
73
OctetString localEngineId = new OctetString (MPv3 .createLocalEngineID ());
80
74
USM usm = new USM (SecurityProtocols .getInstance (), localEngineId , 0 );
81
75
SecurityModels .getInstance ().addSecurityModel (usm );
@@ -85,33 +79,34 @@ public SnmpServiceImpl(Map<String, Object> config) {
85
79
86
80
@ Modified
87
81
protected void modified (Map <String , Object > config ) {
88
- SnmpServiceConfiguration snmpCfg = new Configuration (config ).as (SnmpServiceConfiguration .class );
82
+ this . config = new Configuration (config ).as (SnmpServiceConfiguration .class );
89
83
try {
90
84
shutdownSnmp ();
91
85
92
86
final DefaultUdpTransportMapping transport ;
93
87
94
- if (snmpCfg .port > 0 ) {
95
- transport = new DefaultUdpTransportMapping (new UdpAddress (snmpCfg .port ), true );
88
+ if (this . config .port > 0 ) {
89
+ transport = new DefaultUdpTransportMapping (new UdpAddress (this . config .port ), true );
96
90
} else {
97
91
transport = new DefaultUdpTransportMapping ();
98
92
}
99
93
100
- addProtocols ();
94
+ SecurityProtocols .getInstance ().addDefaultProtocols ();
95
+ SecurityProtocols .getInstance ().addPrivacyProtocol (new Priv3DES ());
101
96
102
97
final Snmp snmp = new Snmp (transport );
103
98
listeners .forEach (snmp ::addCommandResponder );
104
99
snmp .listen ();
105
100
106
101
// re-add user entries
107
- userEntries .forEach (u -> snmp . getUSM (). addUser (u . user , u . engineId ));
102
+ userEntries .forEach (u -> addUser (snmp , u ));
108
103
109
104
this .snmp = snmp ;
110
105
this .transport = transport ;
111
106
112
107
logger .debug ("initialized SNMP at {}" , transport .getAddress ());
113
108
} catch (IOException e ) {
114
- logger .warn ("could not open SNMP instance on port {}: {}" , snmpCfg .port , e .getMessage ());
109
+ logger .warn ("could not open SNMP instance on port {}: {}" , this . config .port , e .getMessage ());
115
110
}
116
111
}
117
112
@@ -125,21 +120,6 @@ public void deactivate() {
125
120
}
126
121
}
127
122
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
-
143
123
private void shutdownSnmp () throws IOException {
144
124
DefaultUdpTransportMapping transport = this .transport ;
145
125
if (transport != null ) {
@@ -172,7 +152,7 @@ public void removeCommandResponder(CommandResponder listener) {
172
152
}
173
153
174
154
@ Override
175
- public void send (PDU pdu , Target <?> target , @ Nullable Object userHandle , ResponseListener listener )
155
+ public void send (PDU pdu , Target target , @ Nullable Object userHandle , ResponseListener listener )
176
156
throws IOException {
177
157
Snmp snmp = this .snmp ;
178
158
if (snmp != null ) {
@@ -184,40 +164,35 @@ public void send(PDU pdu, Target<?> target, @Nullable Object userHandle, Respons
184
164
}
185
165
186
166
@ Override
187
- public void addUser (UsmUser user , OctetString engineId ) {
188
- UserEntry userEntry = new UserEntry (user , engineId );
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 );
189
177
userEntries .add (userEntry );
190
178
191
179
Snmp snmp = this .snmp ;
192
180
if (snmp != null ) {
193
- snmp . getUSM (). addUser (user , engineId );
181
+ addUser (snmp , userEntry );
194
182
}
195
183
}
196
184
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 ;
185
+ private static void addUser (Snmp snmp , UserEntry userEntry ) {
186
+ snmp .getUSM ().addUser (userEntry .securityName , userEntry .engineId , userEntry .user );
214
187
}
215
188
216
189
private static class UserEntry {
190
+ public OctetString securityName ;
217
191
public OctetString engineId ;
218
192
public UsmUser user ;
219
193
220
- public UserEntry (UsmUser user , OctetString engineId ) {
194
+ public UserEntry (OctetString securityName , OctetString engineId , UsmUser user ) {
195
+ this .securityName = securityName ;
221
196
this .engineId = engineId ;
222
197
this .user = user ;
223
198
}
0 commit comments