Skip to content

Commit 3b53b65

Browse files
committed
[ddwrt] Added strict host key checking configuration option
PR openhab#20505 Assisted-by: Windsurf:Claude Opus 4.6 Signed-off-by: Lee Ballard <ballle98@gmail.com>
1 parent 470ef87 commit 3b53b65

5 files changed

Lines changed: 34 additions & 0 deletions

File tree

bundles/org.openhab.binding.ddwrt/src/main/java/org/openhab/binding/ddwrt/internal/DDWRTNetworkConfiguration.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,11 @@ public class DDWRTNetworkConfiguration {
7171
* clients that the routers/APs cannot, or for cross-validation of presence detection.
7272
*/
7373
public boolean useLocalArpCache = false;
74+
75+
/**
76+
* When true, reject SSH connections to hosts not already in known_hosts (strict checking).
77+
* When false (default), unknown host keys are automatically accepted on first connection (TOFU)
78+
* and saved to known_hosts. Changed keys are always rejected regardless of this setting.
79+
*/
80+
public boolean strictHostKeyChecking = false;
7481
}

bundles/org.openhab.binding.ddwrt/src/main/java/org/openhab/binding/ddwrt/internal/api/DDWRTNetwork.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ public void setConfig(DDWRTNetworkConfiguration netCfg) {
9292
if (!Objects.equals(this.config, netCfg)) {
9393
logger.debug("Config changed.");
9494
this.config = netCfg;
95+
SshClientManager.getInstance().setStrictHostKeyChecking(netCfg.strictHostKeyChecking);
9596
reloadHostnameMappings();
9697

9798
// Clear failure tracking for retry logic

bundles/org.openhab.binding.ddwrt/src/main/java/org/openhab/binding/ddwrt/internal/api/SshClientManager.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ public static SshClientManager getInstance() {
5757
return INSTANCE;
5858
}
5959

60+
/**
61+
* Enable or disable strict host key checking. When enabled, unknown host keys
62+
* are rejected. When disabled (default), unknown keys are accepted on first
63+
* connection (TOFU) and saved to known_hosts. Changed keys are always rejected.
64+
*/
65+
public void setStrictHostKeyChecking(boolean strict) {
66+
this.strictHostKeyChecking = strict;
67+
logger.debug("Strict host key checking {}", strict ? "enabled" : "disabled");
68+
}
69+
6070
/**
6171
* Stop the SSH client and release resources. Called from the handler factory
6272
* deactivate to ensure clean shutdown during OSGi bundle restarts.
@@ -72,6 +82,7 @@ public void shutdown() {
7282

7383
private final SshClient client;
7484
private final String ohPrivateKeyDirString = OpenHAB.getUserDataFolder() + "/ddwrt/keys";
85+
private volatile boolean strictHostKeyChecking = false;
7586

7687
private SshClientManager() {
7788
File ohPrivateKeyDir = new File(ohPrivateKeyDirString);
@@ -99,6 +110,10 @@ private SshClientManager() {
99110
Path knownHostsPath = getHomeSshDir() != null ? Objects.requireNonNull(getHomeSshDir()).resolve("known_hosts")
100111
: Paths.get(ohPrivateKeyDirString, "known_hosts");
101112
ServerKeyVerifier verifier = new KnownHostsServerKeyVerifier((s, a, k) -> {
113+
if (strictHostKeyChecking) {
114+
logger.warn("Rejecting unknown host key for {} (strict host key checking is enabled)", a);
115+
return false;
116+
}
102117
logger.debug("TOFU: auto-accepting host key for {}", a);
103118
return true;
104119
}, knownHostsPath) {

bundles/org.openhab.binding.ddwrt/src/main/resources/OH-INF/i18n/ddwrt.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ thing-type.config.ddwrt.network.user.label = User
6060
thing-type.config.ddwrt.network.user.description = Default SSH username. Precedence: user@ in hostnames > this parameter > ~/.ssh/config > system username.
6161
thing-type.config.ddwrt.network.useSystemUser.label = Use System User
6262
thing-type.config.ddwrt.network.useSystemUser.description = Ignore the User field and resolve the username from ~/.ssh/config or the system username.
63+
thing-type.config.ddwrt.network.strictHostKeyChecking.label = Strict Host Key Checking
64+
thing-type.config.ddwrt.network.strictHostKeyChecking.description = When enabled, reject SSH connections to hosts not already in known_hosts. When disabled (default), unknown host keys are automatically accepted on first connection (TOFU) and saved to known_hosts. Changed keys are always rejected regardless of this setting.
6365
thing-type.config.ddwrt.radio.interfaceId.label = Interface ID
6466
thing-type.config.ddwrt.radio.interfaceId.description = Wireless interface identifier. Discovery sets this to deviceMAC:iface (e.g. aa:bb:cc:dd:ee:ff:wl0). For manual configuration, use just the interface name (e.g. wl0) together with parentDeviceMac.
6567
thing-type.config.ddwrt.radio.parentDeviceMac.label = Parent Device MAC

bundles/org.openhab.binding.ddwrt/src/main/resources/OH-INF/thing/thing-types.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@
7272
<default>false</default>
7373
<advanced>true</advanced>
7474
</parameter>
75+
<parameter name="strictHostKeyChecking" type="boolean">
76+
<label>Strict Host Key Checking</label>
77+
<description>When enabled, reject SSH connections to hosts not already in known_hosts. When disabled (default),
78+
unknown host keys are automatically accepted on first connection (TOFU) and saved to known_hosts. Changed keys
79+
are
80+
always rejected regardless of this setting.</description>
81+
<default>false</default>
82+
<advanced>true</advanced>
83+
</parameter>
7584
</config-description>
7685

7786
</bridge-type>

0 commit comments

Comments
 (0)