ClearOS: Edit knockd configuration from webconfig

Today I will show you how to add an admin page to edit knockd’s configuration file from ClearOS’s webconfig:

I’ve copied most of these files’ contents from other admin pages and am not exactly sure what some functions do (like the number at the end of the menu file). However, I have not encountered any problems. Nonetheless, USE AT YOUR OWN RISK!

Let’s start with the language file:

#vi /var/webconfig/htdocs/admin/lang/knockd-conf.en_US

Paste the following into the file:

<?php
// Generated by export.php.
define("WEB_LANG_PAGE_INTRO", "This page allows you to edit your knockd configuration. WARNING: Please ensure you have stopped the knockd service before editing the configuration file!");
define("WEB_LANG_PAGE_TITLE", "Knockd Configuration");
define("WEB_LANG_KNOCKD_HEADING", "Knockd Configuration");
define("WEB_LANG_KNOCKD_LABEL", "Current Configuration");
// vi: syntax=php ts=4
?>

Now let’s move on to the menu:

#vi /var/webconfig/htdocs/menus/knockd-conf.en_US

Paste the following:

Network|Firewall|Knockd Configuration|knockd-conf.php|configuration|3150

Lastly, our main script:

#vi /var/webconfig/htdocs/admin/knockd-conf.php

Paste the following into it:

<?php
require_once("../../gui/Webconfig.inc.php");
require_once(GlobalGetLanguageTemplate(__FILE__));
///////////////////////////////////////////////////////////////////////////////
//
// Header
//
///////////////////////////////////////////////////////////////////////////////
WebAuthenticate();
WebHeader(WEB_LANG_PAGE_TITLE);
WebDialogIntro(WEB_LANG_PAGE_TITLE, "/images/icon-knockd-conf.png", WEB_LANG_PAGE_INTRO);
///////////////////////////////////////////////////////////////////////////////
//
// Handle Update
//
///////////////////////////////////////////////////////////////////////////////
if(isset($_POST['text'])){
$string= isset($_POST['text']) ? $_POST['text'] : "";
$file = fopen("/etc/knockd.conf","w");
fwrite($file, $string);
fclose($file);
};
///////////////////////////////////////////////////////////////////////////////
//
// Main
//
///////////////////////////////////////////////////////////////////////////////
$file = fopen("/etc/knockd.conf","r");
$text = fread($file,filesize("/etc/knockd.conf"));
fclose($file);
knockd_conf($text);
WebFooter();
///////////////////////////////////////////////////////////////////////////////
// F U N C T I O N S
///////////////////////////////////////////////////////////////////////////////
function knockd_conf($text)
{
WebFormOpen();
WebTableOpen(WEB_LANG_KNOCKD_HEADING, "100%");
echo "
<tr>
<td valign='top' class='mytablesubheader' nowrap>" . WEB_LANG_KNOCKD_LABELĀ  . "</td>
<td><textarea rows='19' cols='80' name='text' spellcheck='false'>$text</textarea></td>
</tr>
<tr>
<td class='mytablesubheader' nowrap>&nbsp; </td>
<td>" . WebButtonUpdate("Update") . "</td>
</tr>
";
WebTableClose("100%");
WebFormClose();
}
// vim: syntax=php ts=4
?>

Almost done! We still have one last thing to do before we can test it out. We need to make sure that the user ‘webconfig‘ can write to our kncockd installation’s configuration file. For me, the easiest way to do this was to make webconfig the owner of the configuration file:

#chown webconfig /etc/knockd.conf

Some may say this is insecure or a bad idea but, as I see it, no one from the outside world is going to have access to webconfig and it is therefore secure. If you know of a better way, then by all means use it.

The ‘User Guide’ and ‘ClearCare Support’ links wont work of course. I left them there mainly because I don’t know how to remove them and for aesthetics. Also, the icon image used is the same as the ‘Outgoing’ page’s icon since I didn’t want to re-create one and that one seemed to fit best.

Be sure to stop the service before editing the configuration file to avoid headaches. Checking the service’s status directly from the newly created configuration page is far beyond my current skill set. However, you can add the knockd daemon to the services page quite easily: Installing knockd for ClearOS

That’s it! Congratulations, we are done!

Comments are closed.