From 3db90e930822fd2bad882876737aaa4cb4b13bf1 Mon Sep 17 00:00:00 2001
From: Lorenz Schori <support@woz.ch>
Date: Wed, 4 Nov 2009 16:44:07 +0100
Subject: [PATCH] cname support for dnsmasq

---
 etc/inc/services.inc                          |    5 +
 usr/local/www/services_dnsmasq.php            |   66 ++++++++++-
 usr/local/www/services_dnsmasq_alias_edit.php |  158 +++++++++++++++++++++++++
 3 files changed, 228 insertions(+), 1 deletions(-)
 create mode 100755 usr/local/www/services_dnsmasq_alias_edit.php

diff --git a/etc/inc/services.inc b/etc/inc/services.inc
index 2fadd68..029d37a 100644
--- a/etc/inc/services.inc
+++ b/etc/inc/services.inc
@@ -634,6 +634,11 @@ function services_dnsmasq_configure() {
 			}
 		}
 
+		if (isset($config['dnsmasq']['aliases']) && is_array($config['dnsmasq']['aliases'])) {
+			foreach($config['dnsmasq']['aliases'] as $override) {
+			        $args .= ' --cname=' . $override['alias'] . ',' . $override['host'];
+			}
+		}
 		/* suppose that dnsmasq handles our domain and don't send
 		requests for our local domain to upstream servers */
 		//if (!empty($config['system']['domain'])) {
diff --git a/usr/local/www/services_dnsmasq.php b/usr/local/www/services_dnsmasq.php
index e39af3f..6ca7d5d 100755
--- a/usr/local/www/services_dnsmasq.php
+++ b/usr/local/www/services_dnsmasq.php
@@ -53,6 +53,7 @@ if (!is_array($config['dnsmasq']['domainoverrides']))
 
 
 $a_hosts 	   = &$config['dnsmasq']['hosts'];
+$a_aliases 	   = &$config['dnsmasq']['aliases'];
 $a_domainOverrides = &$config['dnsmasq']['domainoverrides'];
 
 if ($_POST) {
@@ -73,7 +74,7 @@ if ($_POST) {
 	filter_configure();
 
 	if ($retval == 0)
-		clear_subsystem_dirty('hosts');
+    clear_subsystem_dirty('hosts');
 }
 
 if ($_GET['act'] == "del") {
@@ -86,6 +87,15 @@ if ($_GET['act'] == "del") {
                        exit;
                }
        }
+       elseif ($_GET['type'] == 'alias') {
+               if ($a_aliases[$_GET['id']]) {
+                       unset($a_aliases[$_GET['id']]);
+                       write_config();
+			mark_subsystem_dirty('hosts');
+                       header("Location: services_dnsmasq.php");
+                       exit;
+               }
+       }
        elseif ($_GET['type'] == 'doverride') {
                if ($a_domainOverrides[$_GET['id']]) {
                        unset($a_domainOverrides[$_GET['id']]);
@@ -220,6 +230,60 @@ function enable_change(enable_over) {
                     </table>
 		   </td>
 	</table>
+       &nbsp;<br>
+        <table width="100%" border="0" cellpadding="6" cellspacing="0">
+	       <tr><td>&nbsp;</td></tr>
+               <tr>
+                 <td><p>Below you can define CNAME records for the hosts you entered in the previous table.</p></td>
+               </tr>
+        </table>
+	&nbsp;<br>
+        <table width="100%" border="0" cellpadding="0" cellspacing="0">
+                <tr>
+                  <td width="30%" class="listhdrr">Alias</td>
+                  <td width="30%" class="listhdrr">Target Host</td>
+                  <td width="30%" class="listhdr">Description</td>
+                  <td width="10%" class="list">
+                    <table border="0" cellspacing="0" cellpadding="1">
+                      <tr>
+			<td width="17"></td>
+                        <td valign="middle"><a href="services_dnsmasq_alias_edit.php"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif" width="17" height="17" border="0"></a></td>
+                      </tr>
+                    </table>
+		  </td>
+		</tr>
+			  <?php $i = 0; foreach ($a_aliases as $aliasent): ?>
+                <tr>
+                  <td class="listlr" ondblclick="document.location='services_dnsmasq_alias_edit.php?id=<?=$i;?>';">
+                    <?=strtolower($aliasent['alias']);?>&nbsp;
+                  </td>
+                  <td class="listr" ondblclick="document.location='services_dnsmasq_alias_edit.php?id=<?=$i;?>';">
+                    <?=strtolower($aliasent['host']);?>&nbsp;
+                  </td>
+                  <td class="listbg" ondblclick="document.location='services_dnsmasq_alias_edit.php?id=<?=$i;?>';">
+                    <?=htmlspecialchars($aliasent['descr']);?>&nbsp;
+                  </td>
+                  <td valign="middle" nowrap class="list">
+                    <table border="0" cellspacing="0" cellpadding="1">
+                      <tr>
+                        <td valign="middle"><a href="services_dnsmasq_alias_edit.php?id=<?=$i;?>"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_e.gif" width="17" height="17" border="0"></a></td>
+                        <td><a href="services_dnsmasq.php?type=alias&act=del&id=<?=$i;?>" onclick="return confirm('Do you really want to delete this alias?')"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_x.gif" width="17" height="17" border="0"></a></td>
+		      </tr>
+                   </table>
+                </tr>
+		<?php $i++; endforeach; ?>
+                <tr>
+                  <td class="list" colspan="4"></td>
+                  <td class="list">
+                    <table border="0" cellspacing="0" cellpadding="1">
+                      <tr>
+			<td width="17"></td>
+                        <td valign="middle"><a href="services_dnsmasq_alias_edit.php"><img src="./themes/<?= $g['theme']; ?>/images/icons/icon_plus.gif" width="17" height="17" border="0"></a></td>
+                      </tr>
+                    </table>
+		   </td>
+	</table>
+	&nbsp;<br>
 <!-- update to enable domain overrides -->
         <table width="100%" border="0" cellpadding="6" cellspacing="0">
 	       <tr><td>&nbsp;</td></tr>
diff --git a/usr/local/www/services_dnsmasq_alias_edit.php b/usr/local/www/services_dnsmasq_alias_edit.php
new file mode 100755
index 0000000..259d0c6
--- /dev/null
+++ b/usr/local/www/services_dnsmasq_alias_edit.php
@@ -0,0 +1,158 @@
+<?php 
+/* $Id$ */
+/*
+	services_dnsmasq_alias_edit.php
+	part of m0n0wall (http://m0n0.ch/wall)
+	
+	Copyright (C) 2003-2004 Bob Zoller <bob@kludgebox.com> and Manuel Kasper <mk@neon1.net>.
+	All rights reserved.
+	
+	Redistribution and use in source and binary forms, with or without
+	modification, are permitted provided that the following conditions are met:
+	
+	1. Redistributions of source code must retain the above copyright notice,
+	   this list of conditions and the following disclaimer.
+	
+	2. Redistributions in binary form must reproduce the above copyright
+	   notice, this list of conditions and the following disclaimer in the
+	   documentation and/or other materials provided with the distribution.
+	
+	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
+	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
+	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
+	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+	POSSIBILITY OF SUCH DAMAGE.
+*/
+/*
+	pfSense_MODULE:	dnsforwarder
+*/
+
+##|+PRIV
+##|*IDENT=page-services-dnsforwarder-editalias
+##|*NAME=Services: DNS Forwarder: Edit alias page
+##|*DESCR=Allow access to the 'Services: DNS Forwarder: Edit alias' page.
+##|*MATCH=services_dnsmasq_edit.php*
+##|-PRIV
+
+function aliascmp($a, $b) {
+	return strcasecmp($a['alias'], $b['alias']);
+}
+
+function aliases_sort() {
+        global $g, $config;
+
+        if (!is_array($config['dnsmasq']['aliases']))
+                return;
+
+        usort($config['dnsmasq']['aliases'], "aliascmp");
+}
+
+require("guiconfig.inc");
+
+if (!is_array($config['dnsmasq']['aliases'])) 
+	$config['dnsmasq']['aliases'] = array();
+
+aliases_sort();
+$a_aliases = &$config['dnsmasq']['aliases'];
+
+$id = $_GET['id'];
+if (isset($_POST['id']))
+	$id = $_POST['id'];
+
+if (isset($id) && $a_aliases[$id]) {
+	$pconfig['alias'] = $a_aliases[$id]['alias'];
+	$pconfig['host'] = $a_aliases[$id]['host'];
+	$pconfig['descr'] = $a_aliases[$id]['descr'];
+}
+
+if ($_POST) {
+
+	unset($input_errors);
+	$pconfig = $_POST;
+
+	/* input validation */
+	$reqdfields = explode(" ", "alias,host");
+	$reqdfieldsn = explode(",", "Alias,Host");
+	
+	do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
+	
+	if ($_POST['alias'] && !is_hostname($_POST['alias']) && !is_domain($_POST['alias']))
+		$input_errors[] = "A valid alias must be specified.";
+
+	if ($_POST['host'] && !is_hostname($_POST['host'])) 
+		$input_errors[] = "A valid host must be specified.";
+		
+	if (!$input_errors) {
+		$aliasent = array();
+		$aliasent['alias'] = $_POST['alias'];
+		$aliasent['host'] = $_POST['host'];
+		$aliasent['descr'] = $_POST['descr'];
+
+		aliases_sort();
+		if (isset($id) && $a_aliases[$id])
+			$a_aliases[$id] = $aliasent;
+		else
+			$a_aliases[] = $aliasent;
+		
+		mark_subsystem_dirty('hosts');
+		
+		write_config();
+		
+		header("Location: services_dnsmasq.php");
+		exit;
+	}
+}
+
+$pgtitle = array("Services","DNS forwarder","Edit alias");
+include("head.inc");
+
+?>
+
+<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
+<?php include("fbegin.inc"); ?>
+<?php if ($input_errors) print_input_errors($input_errors); ?>
+        <form action="services_dnsmasq_edit.php" method="post" name="iform" id="iform">
+        <table width="100%" border="0" cellpadding="6" cellspacing="0">
+				<tr>
+					<td colspan="2" valign="top" class="listtopic">Edit DNS Alias entry</td>
+				</tr>	
+                <tr>
+                  <td width="22%" valign="top" class="vncell">Alias</td>
+                  <td width="78%" class="vtable"> 
+                    <input name="alias" type="text" class="formfld" id="alias" size="40" value="<?=htmlspecialchars($pconfig['alias']);?>">
+                    <br> <span class="vexpl">Name of the alias<br>
+                    e.g. <em>myalias</em></span></td>
+                </tr>
+				<tr>
+                  <td width="22%" valign="top" class="vncellreq">Host</td>
+                  <td width="78%" class="vtable"> 
+                    <input name="host" type="text" class="formfld" id="host" size="40" value="<?=htmlspecialchars($pconfig['host']);?>">
+                    <br> <span class="vexpl">Target host<br>
+                    e.g. <em>blah.com</em></span></td>
+                </tr>
+				<tr>
+                  <td width="22%" valign="top" class="vncell">Description</td>
+                  <td width="78%" class="vtable"> 
+                    <input name="descr" type="text" class="formfld" id="descr" size="40" value="<?=htmlspecialchars($pconfig['descr']);?>">
+                    <br> <span class="vexpl">You may enter a description here
+                    for your reference (not parsed).</span></td>
+                </tr>
+                <tr>
+                  <td width="22%" valign="top">&nbsp;</td>
+                  <td width="78%"> 
+                    <input name="Submit" type="submit" class="formbtn" value="Save"> <input class="formbtn" type="button" value="Cancel" onclick="history.back()">
+                    <?php if (isset($id) && $a_aliases[$id]): ?>
+                    <input name="id" type="hidden" value="<?=$id;?>">
+                    <?php endif; ?>
+                  </td>
+                </tr>
+        </table>
+</form>
+<?php include("fend.inc"); ?>
+</body>
+</html>
-- 
1.5.6.5

