Project

General

Profile

Download (8.53 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	services_dnsmasq_edit.php
5
	part of m0n0wall (http://m0n0.ch/wall)
6

    
7
	Copyright (C) 2003-2004 Bob Zoller <bob@kludgebox.com> and Manuel Kasper <mk@neon1.net>.
8
	Copyright (C) 2013-2015 Electric Sheep Fencing, LP
9
	All rights reserved.
10

    
11
	Redistribution and use in source and binary forms, with or without
12
	modification, are permitted provided that the following conditions are met:
13

    
14
	1. Redistributions of source code must retain the above copyright notice,
15
	   this list of conditions and the following disclaimer.
16

    
17
	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

    
21
	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
/*
33
	pfSense_MODULE: dnsforwarder
34
*/
35

    
36
##|+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
function hostcmp($a, $b) {
44
	return strcasecmp($a['host'], $b['host']);
45
}
46

    
47
function hosts_sort() {
48
	global $g, $config;
49

    
50
	if (!is_array($config['dnsmasq']['hosts']))
51
			return;
52

    
53
	usort($config['dnsmasq']['hosts'], "hostcmp");
54
}
55

    
56
require("guiconfig.inc");
57

    
58
$referer = (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '/services_dnsmasq.php');
59

    
60
if (!is_array($config['dnsmasq']['hosts']))
61
	$config['dnsmasq']['hosts'] = array();
62

    
63
$a_hosts = &$config['dnsmasq']['hosts'];
64

    
65
if (is_numericint($_GET['id']))
66
	$id = $_GET['id'];
67

    
68
if (isset($_POST['id']) && is_numericint($_POST['id']))
69
	$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
	$pconfig['aliases'] = $a_hosts[$id]['aliases'];
77
}
78

    
79
if ($_POST) {
80
	unset($input_errors);
81
	$pconfig = $_POST;
82

    
83
	/* input validation */
84
	$reqdfields = explode(" ", "domain ip");
85
	$reqdfieldsn = array(gettext("Domain"),gettext("IP address"));
86

    
87
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
88

    
89
	if ($_POST['host']) {
90
		if (!is_hostname($_POST['host'])) {
91
			$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

    
99
	if (($_POST['domain'] && !is_domain($_POST['domain'])))
100
		$input_errors[] = gettext("A valid domain must be specified.");
101

    
102
	if (($_POST['ip'] && !is_ipaddr($_POST['ip'])))
103
		$input_errors[] = gettext("A valid IP address must be specified.");
104

    
105
	/* 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
		do_input_validation($_POST, $aliasreqdfields, $aliasreqdfieldsn, $input_errors);
133
		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
		}
142

    
143
		if (($alias['domain'] && !is_domain($alias['domain']))) {
144
			$input_errors[] = gettext("A valid domain must be specified in alias list.");
145
		}
146
	}
147

    
148
	/* check for overlaps */
149
	foreach ($a_hosts as $hostent) {
150
		if (isset($id) && ($a_hosts[$id]) && ($a_hosts[$id] === $hostent))
151
			continue;
152

    
153
		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
			$input_errors[] = gettext("This host/domain already exists.");
156
			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
		$hostent['aliases']['item'] = $aliases;
167

    
168
		if (isset($id) && $a_hosts[$id])
169
			$a_hosts[$id] = $hostent;
170
		else
171
			$a_hosts[] = $hostent;
172
		hosts_sort();
173

    
174
		mark_subsystem_dirty('hosts');
175

    
176
		write_config();
177

    
178
		header("Location: services_dnsmasq.php");
179
		exit;
180
	}
181
}
182

    
183
// 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
$pgtitle = array(gettext("Services"),gettext("DNS forwarder"),gettext("Edit host"));
201
$shortcut_section = "forwarder";
202
include("head.inc");
203

    
204
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
			$item['host']
268
		))->setHelp($counter == $last ? 'Host name':null);
269

    
270
		$group->add(new Form_Input(
271
			'aliasdomain' . $counter,
272
			null,
273
			'text',
274
			$item['domain']
275
		))->setHelp($counter == $last ? 'Value':null);
276

    
277
		$group->add(new Form_Input(
278
			'aliasdescription' . $counter,
279
			null,
280
			'text',
281
			$item['description']
282
		))->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");
(144-144/241)