OPNsense Forum
English Forums => Development and Code Review => Topic started by: pxpunx on February 27, 2022, 01:04:28 am
-
I wanted to add a syslog option to os-bind, so I played around and tested it. Code is below. It works, but I've encountered odd behavior when I try to save the configuration.
For example, if I select one or more of my new syslog options from the drop-down list I created, the configuration saves and the underlying configuration file is updated successfully. If I do not select any syslog options, the configuration saves but is not updated. If I do not select any syslog options, other standard configuration parameters also do not save. It's as if the state of the new `syslog` field controls whether or not the configuration file is updated.
I'm sure it's something ridiculous that I've missed and wanted to see if anyone had any ideas.
# /usr/local/opnsense/mvc/app/models/OPNsense/Bind/General.xml
<model>
<mount>//OPNsense/bind/general</mount>
...
<items>
...
<syslog type="OptionField">
<Required>N</Required>
<Multiple>Y</Multiple>
<OptionValues>
<default>default</default>
# note: I've tried with and without <default> to make sure it didn't conflict
<general>general</general>
<queries>queries</queries>
<rpz>rpz</rpz>
</OptionValues>
<default></default>
# tried with and without this parameter, and with and without a value set
</syslog>
...
</items>
</model>
# /usr/local/opnsense/mvc/app/controllers/OPNsense/Bind/forms/general.xml
<form>
...
<field>
<id>general.syslog</id>
<label>Enable Syslog</label>
<type>select_multiple</type>
<help>Enable syslog per logging channel.</help>
</field>
...
</form>
This feels a little crude, but it works:
# /usr/local/opnsense/service/templates/OPNsense/Bind/named.conf
...
logging {
...
{% if helpers.exists('OPNsense.bind.general.syslog') %}
channel syslog {
syslog daemon;
severity info;
};
{% endif %}
category default { default_log; {% if 'default' in OPNsense.bind.general.syslog %}syslog;{% endif %} };
category general { default_log; {% if 'general' in OPNsense.bind.general.syslog %}syslog;{% endif %} };
category queries { query_log; {% if 'queries' in OPNsense.bind.general.syslog %}syslog;{% endif %} };
category rpz { rpz_log; {% if 'rpz' in OPNsense.bind.general.syslog %}syslog;{% endif %} };
...
};
...
-
It looks like the issue has to do with the modifications I made to the named.conf template. I made some small tweaks to test and am able to save correctly. I will post an update when I sort out just what I did to ruin the template.
-
Solved!
logging {
...
{% if helpers.exists('OPNsense.bind.general.syslog') %}
channel syslog {
syslog daemon;
severity info;
};
{% endif %}
category default { default_log;{{ ' syslog;' if 'default' in OPNsense.bind.general.syslog|string }} };
category general { default_log;{{ ' syslog;' if 'general' in OPNsense.bind.general.syslog|string }} };
category queries { query_log;{{ ' syslog;' if 'queries' in OPNsense.bind.general.syslog|string }} };
category rpz { rpz_log;{{ ' syslog;' if 'rpz' in OPNsense.bind.general.syslog|string }} };
...
};
I had to add |string at the end of the variable name to force it to treat the variable like a string when performing the string search.
I also streamlined it a little.
-
Thanks for looking into this! Relevant PR for future reference:
https://github.com/opnsense/plugins/pull/2867
Cheers,
Franco