net-snmp: fix newline when RestrictOID is neither yes nor no
authorEric McDonald <redacted>
Sat, 7 Mar 2026 22:29:16 +0000 (14:29 -0800)
committerFlorian Eckert <redacted>
Wed, 11 Mar 2026 08:07:04 +0000 (09:07 +0100)
snmpd_access_default_add, snmpd_access_HostName_add, and
snmpd_access_HostIP_add check if RestrictOID is `yes` or `no`
but fail to write a newline for other values of RestrictOID
(e.g., `true`, `false`) or if unset, corrupting the generated config.

Fix by using config_get_bool and restructuring to write either
a complete line or nothing. Also require RestrictedOID to be
non-empty when RestrictOID is enabled.

This is a breaking change for configs where RestrictOID is set
to a value that the old code did not match as `yes` (e.g., `true`,
`1`, `on`, `enabled`) but config_get_bool evaluates as true. In
practice, this is unlikely to affect existing installs unless a
user is unknowingly relying on unintended behavior or is deliberately
relying on the old code's non-standard boolean evaluation; this
latter case is considered unlikely by this commit's author. Configs
where RestrictOID is enabled but RestrictedOID is empty are also
affected; previously a directive with a trailing space was written,
now no directive is written.

Signed-off-by: Eric McDonald <redacted>
net/net-snmp/files/snmpd.init

index 371c15071cabaf1918f0eda1f6b9e9945e0d06e2..78a62a5603fa127f5e5ef825342b96a144933752 100644 (file)
@@ -133,11 +133,15 @@ snmpd_access_default_add() {
        [ -n "$mode" ] || return 0
        config_get community "$cfg" CommunityName
        [ -n "$community" ] || return 0
-       config_get oidrestrict "$cfg" RestrictOID
+
+       config_get_bool oidrestrict "$cfg" RestrictOID 0
        config_get oid "$cfg" RestrictedOID
-       echo -n "$mode $community default" >> $CONFIGFILE
-       [ "$oidrestrict" == "yes" ] && echo " $oid" >> $CONFIGFILE
-       [ "$oidrestrict" == "no" ] && echo "" >> $CONFIGFILE
+       if [ "$oidrestrict" -eq 1 ]; then
+               [ -n "$oid" ] || return 0
+               echo "$mode $community default $oid" >> $CONFIGFILE
+       else
+               echo "$mode $community default" >> $CONFIGFILE
+       fi
 }
 
 snmpd_access_HostName_add() {
@@ -148,11 +152,15 @@ snmpd_access_HostName_add() {
        [ -n "$mode" ] || return 0
        config_get community "$cfg" CommunityName
        [ -n "$community" ] || return 0
-       config_get oidrestrict "$cfg" RestrictOID
+
+       config_get_bool oidrestrict "$cfg" RestrictOID 0
        config_get oid "$cfg" RestrictedOID
-       echo -n "$mode $community $hostname" >> $CONFIGFILE
-       [ "$oidrestrict" == "yes" ] && echo " $oid" >> $CONFIGFILE
-       [ "$oidrestrict" == "no" ] && echo "" >> $CONFIGFILE
+       if [ "$oidrestrict" -eq 1 ]; then
+               [ -n "$oid" ] || return 0
+               echo "$mode $community $hostname $oid" >> $CONFIGFILE
+       else
+               echo "$mode $community $hostname" >> $CONFIGFILE
+       fi
 }
 
 snmpd_access_HostIP_add() {
@@ -165,11 +173,15 @@ snmpd_access_HostIP_add() {
        [ -n "$mode" ] || return 0
        config_get community "$cfg" CommunityName
        [ -n "$community" ] || return 0
-       config_get oidrestrict "$cfg" RestrictOID
+
+       config_get_bool oidrestrict "$cfg" RestrictOID 0
        config_get oid "$cfg" RestrictedOID
-       echo -n "$mode $community $host_ip/$ip_mask" >> $CONFIGFILE
-       [ "$oidrestrict" == "yes" ] && echo " $oid" >> $CONFIGFILE
-       [ "$oidrestrict" == "no" ] && echo "" >> $CONFIGFILE
+       if [ "$oidrestrict" -eq 1 ]; then
+               [ -n "$oid" ] || return 0
+               echo "$mode $community $host_ip/$ip_mask $oid" >> $CONFIGFILE
+       else
+               echo "$mode $community $host_ip/$ip_mask" >> $CONFIGFILE
+       fi
 }
 
 snmpd_pass_add() {
git clone https://git.99rst.org/PROJECT