Project

General

Profile

Download (8.53 KB) Statistics
| Branch: | Tag: | Revision:
1 158732bd sbeaver
<?php
2 b46bfcf5 Bill Marquette
/* $Id$ */
3 5b237745 Scott Ullrich
/*
4
	services_dnsmasq_edit.php
5
	part of m0n0wall (http://m0n0.ch/wall)
6 158732bd sbeaver
7 5b237745 Scott Ullrich
	Copyright (C) 2003-2004 Bob Zoller <bob@kludgebox.com> and Manuel Kasper <mk@neon1.net>.
8 ce77a9c4 Phil Davis
	Copyright (C) 2013-2015 Electric Sheep Fencing, LP
9 5b237745 Scott Ullrich
	All rights reserved.
10 158732bd sbeaver
11 5b237745 Scott Ullrich
	Redistribution and use in source and binary forms, with or without
12
	modification, are permitted provided that the following conditions are met:
13 158732bd sbeaver
14 5b237745 Scott Ullrich
	1. Redistributions of source code must retain the above copyright notice,
15
	   this list of conditions and the following disclaimer.
16 158732bd sbeaver
17 5b237745 Scott Ullrich
	2. Redistributions in binary form must reproduce the above copyright
18
	   notice, this list of conditions and the following disclaimer in the
19
	   documentation and/or other materials provided with the distribution.
20 158732bd sbeaver
21 5b237745 Scott Ullrich
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
22
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
23
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
25
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
	POSSIBILITY OF SUCH DAMAGE.
31
*/
32 1d333258 Scott Ullrich
/*
33 158732bd sbeaver
	pfSense_MODULE: dnsforwarder
34 1d333258 Scott Ullrich
*/
35 5b237745 Scott Ullrich
36 6b07c15a Matthew Grooms
##|+PRIV
37
##|*IDENT=page-services-dnsforwarder-edithost
38
##|*NAME=Services: DNS Forwarder: Edit host page
39
##|*DESCR=Allow access to the 'Services: DNS Forwarder: Edit host' page.
40
##|*MATCH=services_dnsmasq_edit.php*
41
##|-PRIV
42
43 4504a769 Ermal Lu?i
function hostcmp($a, $b) {
44
	return strcasecmp($a['host'], $b['host']);
45
}
46
47 0d64af59 Ermal Lu?i
function hosts_sort() {
48 158732bd sbeaver
	global $g, $config;
49 0d64af59 Ermal Lu?i
50 158732bd sbeaver
	if (!is_array($config['dnsmasq']['hosts']))
51
			return;
52 0d64af59 Ermal Lu?i
53 158732bd sbeaver
	usort($config['dnsmasq']['hosts'], "hostcmp");
54 0d64af59 Ermal Lu?i
}
55 6b07c15a Matthew Grooms
56 5b237745 Scott Ullrich
require("guiconfig.inc");
57
58 62424bdb Renato Botelho
$referer = (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '/services_dnsmasq.php');
59
60 158732bd sbeaver
if (!is_array($config['dnsmasq']['hosts']))
61 5b237745 Scott Ullrich
	$config['dnsmasq']['hosts'] = array();
62 4d6ba181 Scott Ullrich
63 5b237745 Scott Ullrich
$a_hosts = &$config['dnsmasq']['hosts'];
64
65 e41ec584 Renato Botelho
if (is_numericint($_GET['id']))
66
	$id = $_GET['id'];
67 158732bd sbeaver
68 e41ec584 Renato Botelho
if (isset($_POST['id']) && is_numericint($_POST['id']))
69 5b237745 Scott Ullrich
	$id = $_POST['id'];
70
71
if (isset($id) && $a_hosts[$id]) {
72
	$pconfig['host'] = $a_hosts[$id]['host'];
73
	$pconfig['domain'] = $a_hosts[$id]['domain'];
74
	$pconfig['ip'] = $a_hosts[$id]['ip'];
75
	$pconfig['descr'] = $a_hosts[$id]['descr'];
76 5a2a8349 Lorenz Schori
	$pconfig['aliases'] = $a_hosts[$id]['aliases'];
77 5b237745 Scott Ullrich
}
78
79
if ($_POST) {
80
	unset($input_errors);
81
	$pconfig = $_POST;
82
83
	/* input validation */
84
	$reqdfields = explode(" ", "domain ip");
85 065bc89a Rafael Lucas
	$reqdfieldsn = array(gettext("Domain"),gettext("IP address"));
86 158732bd sbeaver
87 1e9b4611 Renato Botelho
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
88 158732bd sbeaver
89 84c4efc4 Phil Davis
	if ($_POST['host']) {
90 158732bd sbeaver
		if (!is_hostname($_POST['host'])) {
91 84c4efc4 Phil Davis
			$input_errors[] = gettext("The hostname can only contain the characters A-Z, 0-9 and '-'. It may not start or end with '-'.");
92
		} else {
93
			if (!is_unqualified_hostname($_POST['host'])) {
94
				$input_errors[] = gettext("A valid hostname is specified, but the domain name part should be omitted");
95
			}
96
		}
97
	}
98 4d6ba181 Scott Ullrich
99 158732bd sbeaver
	if (($_POST['domain'] && !is_domain($_POST['domain'])))
100 065bc89a Rafael Lucas
		$input_errors[] = gettext("A valid domain must be specified.");
101 158732bd sbeaver
102
	if (($_POST['ip'] && !is_ipaddr($_POST['ip'])))
103 065bc89a Rafael Lucas
		$input_errors[] = gettext("A valid IP address must be specified.");
104 5b237745 Scott Ullrich
105 5a2a8349 Lorenz Schori
	/* collect aliases */
106
	$aliases = array();
107
	foreach ($_POST as $key => $value) {
108
		$entry = '';
109
		if (!substr_compare('aliashost', $key, 0, 9)) {
110
			$entry = substr($key, 9);
111
			$field = 'host';
112
		}
113
		elseif (!substr_compare('aliasdomain', $key, 0, 11)) {
114
			$entry = substr($key, 11);
115
			$field = 'domain';
116
		}
117
		elseif (!substr_compare('aliasdescription', $key, 0, 16)) {
118
			$entry = substr($key, 16);
119
			$field = 'description';
120
		}
121
		if (ctype_digit($entry)) {
122
			$aliases[$entry][$field] = $value;
123
		}
124
	}
125
	$pconfig['aliases']['item'] = $aliases;
126
127
	/* validate aliases */
128
	foreach ($aliases as $idx => $alias) {
129
		$aliasreqdfields = array('aliasdomain' . $idx);
130
		$aliasreqdfieldsn = array(gettext("Alias Domain"));
131
132 1e9b4611 Renato Botelho
		do_input_validation($_POST, $aliasreqdfields, $aliasreqdfieldsn, $input_errors);
133 84c4efc4 Phil Davis
		if ($alias['host']) {
134
			if (!is_hostname($alias['host'])) {
135
				$input_errors[] = gettext("Hostnames in an alias list can only contain the characters A-Z, 0-9 and '-'. They may not start or end with '-'.");
136
			} else {
137
				if (!is_unqualified_hostname($alias['host'])) {
138
					$input_errors[] = gettext("A valid alias hostname is specified, but the domain name part should be omitted");
139
				}
140
			}
141 5a2a8349 Lorenz Schori
		}
142 158732bd sbeaver
143 5a2a8349 Lorenz Schori
		if (($alias['domain'] && !is_domain($alias['domain']))) {
144
			$input_errors[] = gettext("A valid domain must be specified in alias list.");
145
		}
146
	}
147
148 5b237745 Scott Ullrich
	/* check for overlaps */
149
	foreach ($a_hosts as $hostent) {
150
		if (isset($id) && ($a_hosts[$id]) && ($a_hosts[$id] === $hostent))
151
			continue;
152
153 424be584 jim-p
		if (($hostent['host'] == $_POST['host']) && ($hostent['domain'] == $_POST['domain'])
154
			&& ((is_ipaddrv4($hostent['ip']) && is_ipaddrv4($_POST['ip'])) || (is_ipaddrv6($hostent['ip']) && is_ipaddrv6($_POST['ip'])))) {
155 065bc89a Rafael Lucas
			$input_errors[] = gettext("This host/domain already exists.");
156 5b237745 Scott Ullrich
			break;
157
		}
158
	}
159
160
	if (!$input_errors) {
161
		$hostent = array();
162
		$hostent['host'] = $_POST['host'];
163
		$hostent['domain'] = $_POST['domain'];
164
		$hostent['ip'] = $_POST['ip'];
165
		$hostent['descr'] = $_POST['descr'];
166 5a2a8349 Lorenz Schori
		$hostent['aliases']['item'] = $aliases;
167 5b237745 Scott Ullrich
168
		if (isset($id) && $a_hosts[$id])
169
			$a_hosts[$id] = $hostent;
170
		else
171
			$a_hosts[] = $hostent;
172 0e3aa71c Erik Fonnesbeck
		hosts_sort();
173 158732bd sbeaver
174 a368a026 Ermal Lu?i
		mark_subsystem_dirty('hosts');
175 158732bd sbeaver
176 5b237745 Scott Ullrich
		write_config();
177 158732bd sbeaver
178 5b237745 Scott Ullrich
		header("Location: services_dnsmasq.php");
179
		exit;
180
	}
181
}
182 b63695db Scott Ullrich
183 158732bd sbeaver
// Delete a row in the options table
184
if($_GET['act'] == "delopt") {
185
	$idx = $_GET['id'];
186
187
	if($pconfig['aliases'] && is_array($pconfig['aliases']['item'][$idx])) {
188
	   unset($pconfig['aliases']['item'][$idx]);
189
	}
190
}
191
192
// Add an option row
193
if($_GET['act'] == "addopt") {
194
    if(!is_array($pconfig['aliases']['item']))
195
        $pconfig['aliases']['item'] = array();
196
        
197
	array_push($pconfig['aliases']['item'], array('host' => null, 'domain' => null, 'description' => null));
198
}
199
200 065bc89a Rafael Lucas
$pgtitle = array(gettext("Services"),gettext("DNS forwarder"),gettext("Edit host"));
201 db88a3a2 Phil Davis
$shortcut_section = "forwarder";
202 b63695db Scott Ullrich
include("head.inc");
203
204 158732bd sbeaver
if ($input_errors)
205
	print_input_errors($input_errors);
206
207
require('classes/Form.class.php');
208
209
$form = new Form();
210
211
$section = new Form_Section('Domain override options');
212
213
$section->addInput(new Form_Input(
214
	'host',
215
	'Host',
216
	'text',
217
	$pconfig['domain']
218
))->setHelp('Name of the host, without the domain part' . '<br />' .
219
			'e.g.: "myhost"');
220
221
$section->addInput(new Form_Input(
222
	'domain',
223
	'Domain',
224
	'text',
225
	$pconfig['domain']
226
))->setHelp('Domain of the host' . '<br />' .
227
			'e.g.: "example.com"');
228
229
$section->addInput(new Form_IpAddress(
230
	'ip',
231
	'IP Address',
232
	$pconfig['ip']
233
))->setHelp('IP address of the host' . '<br />' .
234
			'e.g.: 192.168.100.100 or fd00:abcd::1');
235
236
$section->addInput(new Form_Input(
237
	'descr',
238
	'Description',
239
	'text',
240
	$pconfig['descr']
241
))->setHelp('You may enter a description here for your reference (not parsed).');
242
243
if (isset($id) && $a_hosts[$id]) {
244
	$section->addInput(new Form_Input(
245
		'id',
246
		null,
247
		'hidden',
248
		$pconfig['id']
249
	))->setHelp('You may enter a description here for your reference (not parsed).');
250
}
251
252
$form->add($section);
253
254
$section = new Form_Section('Additional names for this host');
255
256
if( $pconfig['aliases']['item']) {
257
	$counter = 0;
258
	$last = count($pconfig['aliases']['item']) - 1;
259
260
	foreach($pconfig['aliases']['item'] as $item) {
261
		$group = new Form_Group(null);
262
263
		$group->add(new Form_Input(
264
			'aliashost' . $counter,
265
			null,
266
			'text',
267 299364a4 sbeaver
			$item['host']
268 158732bd sbeaver
		))->setHelp($counter == $last ? 'Host name':null);
269
270
		$group->add(new Form_Input(
271
			'aliasdomain' . $counter,
272
			null,
273
			'text',
274 299364a4 sbeaver
			$item['domain']
275 158732bd sbeaver
		))->setHelp($counter == $last ? 'Value':null);
276
277
		$group->add(new Form_Input(
278
			'aliasdescription' . $counter,
279
			null,
280
			'text',
281 299364a4 sbeaver
			$item['description']
282 158732bd sbeaver
		))->setHelp($counter == $last ? 'Description':null);
283
284
		$btn = new Form_Button(
285
			'btn' . $counter,
286
			'Delete',
287
			'services_dnsmasq_edit.php?act=delopt' . '&id=' . $counter
288
		);
289
290
		$btn->removeClass('btn-primary')->addClass('btn-danger btn-sm');
291
		$group->add($btn);
292
		$section->add($group);
293
		$counter++;
294
	}
295
}
296
297
$btnaddopt = new Form_Button(
298
	'btnaddopt',
299
	'Add Option',
300
	'services_dnsmasq_edit.php?act=addopt'
301
);
302
303
$btnaddopt->removeClass('btn-primary')->addClass('btn-success btn-sm');
304
305
$section->addInput($btnaddopt);
306
307
$form->addGlobal(new Form_Button(
308
	'cancel',
309
	'Cancel',
310
	$referer
311
));
312
313
$form->add($section);
314
print($form);
315
316
include("foot.inc");