Project

General

Profile

Download (5.73 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	services_dhcp_relay.php
4

    
5
	Copyright (C) 2003-2004 Justin Ellison <justin@techadvise.com>.
6
	Copyright (C) 2010 	Ermal Luçi
7
	Copyright (C) 2013-2015 Electric Sheep Fencing, LP
8
	All rights reserved.
9

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

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

    
16
	2. Redistributions in binary form must reproduce the above copyright
17
	   notice, this list of conditions and the following disclaimer in the
18
	   documentation and/or other materials provided with the distribution.
19

    
20
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
21
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
24
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
	POSSIBILITY OF SUCH DAMAGE.
30
*/
31
/*
32
	pfSense_MODULE:	dhcprelay
33
*/
34

    
35
##|+PRIV
36
##|*IDENT=page-services-dhcprelay
37
##|*NAME=Services: DHCP Relay page
38
##|*DESCR=Allow access to the 'Services: DHCP Relay' page.
39
##|*MATCH=services_dhcp_relay.php*
40
##|-PRIV
41

    
42
require("guiconfig.inc");
43
require('classes/Form.class.php');
44

    
45
function filterDestinationServers(array $destinationServers)
46
{
47
	return array_unique(
48
		array_filter($destinationServers)
49
	);
50
}
51

    
52
$pconfig['enable'] = isset($config['dhcrelay']['enable']);
53

    
54
if (empty($config['dhcrelay']['interface'])) {
55
	$pconfig['interface'] = array();
56
} else {
57
	$pconfig['interface'] = explode(",", $config['dhcrelay']['interface']);
58
}
59

    
60
$pconfig['server'] = filterDestinationServers(
61
	explode(',', $config['dhcrelay']['server'])
62
);
63

    
64
$pconfig['agentoption'] = isset($config['dhcrelay']['agentoption']);
65

    
66
$iflist = array_intersect_key(
67
	get_configured_interface_with_descr(),
68
	array_flip(
69
		array_filter(
70
			array_keys(get_configured_interface_with_descr()),
71
			function($if) {
72
				return is_ipaddr(get_interface_ip($if));
73
			}
74
		)
75
	)
76
);
77

    
78
/*   set the enabled flag which will tell us if DHCP server is enabled
79
 *   on any interface.   We will use this to disable dhcp-relay since
80
 *   the two are not compatible with each other.
81
 */
82
$dhcpd_enabled = false;
83
if (is_array($config['dhcpd'])) {
84
	foreach ($config['dhcpd'] as $dhcpif => $dhcp) {
85
		if (isset($dhcp['enable']) && isset($config['interfaces'][$dhcpif]['enable'])) {
86
			$dhcpd_enabled = true;
87
			break;
88
		}
89
	}
90
}
91

    
92
if ($_POST) {
93

    
94
	unset($input_errors);
95

    
96
	if ($_POST['server'])
97
		$_POST['server'] = filterDestinationServers($_POST['server']);
98

    
99
	$pconfig = $_POST;
100

    
101
	/* input validation */
102
	if ($_POST['enable']) {
103
		$reqdfields = explode(" ", "server interface");
104
		$reqdfieldsn = array(gettext("Destination Server"), gettext("Interface"));
105

    
106
		do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
107

    
108
		if ($_POST['server']) {
109
			$checksrv = explode(",", $_POST['server']);
110
			foreach ($checksrv as $srv) {
111
				if (!is_ipaddr($srv)) {
112
					$input_errors[] = gettext("A valid Destination Server IP address must be specified.");
113
				}
114
			}
115
		}
116
	}
117

    
118
	if (!$input_errors) {
119
		$config['dhcrelay']['enable'] = $_POST['enable'] ? true : false;
120
		$config['dhcrelay']['interface'] = implode(",", $_POST['interface']);
121
		$config['dhcrelay']['agentoption'] = $_POST['agentoption'] ? true : false;
122
		$config['dhcrelay']['server'] = implode(',', $_POST['server']);
123

    
124
		write_config();
125

    
126
		$retval = 0;
127
		$retval = services_dhcrelay_configure();
128
		$savemsg = get_std_save_message($retval);
129

    
130
	}
131
}
132

    
133
$closehead = false;
134
$pgtitle = array(gettext("Services"), gettext("DHCP Relay"));
135
$shortcut_section = "dhcp";
136
include("head.inc");
137

    
138
if ($dhcpd_enabled) {
139
	echo '<div class="alert alert-danger">DHCP Server is currently enabled. Cannot enable the DHCP Relay service while the DHCP Server is enabled on any interface.</div>';
140
	include("foot.inc");
141
	exit;
142
}
143

    
144
if ($input_errors)
145
	print_input_errors($input_errors);
146

    
147
if ($savemsg)
148
	print_info_box($savemsg);
149

    
150
$form = new Form;
151

    
152
$section = new Form_Section('DHCP Relay configuration');
153

    
154
$section->addInput(new Form_Checkbox(
155
	'enable',
156
	'Enable',
157
	'Enable DHCP relay on interface',
158
	$pconfig['enable']
159
))->toggles('.form-group:not(:first-child)');
160

    
161
$section->addInput(new Form_Select(
162
	'interface',
163
	'Interface(s)',
164
	$pconfig['interface'],
165
	$iflist,
166
	true
167
))->setHelp('Interfaces without an IP address will not be shown.');
168

    
169
$section->addInput(new Form_Checkbox(
170
	'agentoption',
171
	'',
172
	'Append circuit ID and agent ID to requests',
173
	'yes',
174
	$pconfig['agentoption']
175
))->setHelp(
176
	'If this is checked, the DHCP relay will append the circuit ID (%s interface number) and the agent ID to the DHCP request.',
177
	[$g['product_name']]
178
);
179

    
180
//Small function to prevent duplicate code
181
function createDestinationServerInputGroup($value = null)
182
{
183
	$group = new Form_Group('Destination server');
184

    
185
	$group->add(new Form_IpAddress(
186
		'server',
187
		'Destination server',
188
		$value
189
	))->setWidth(4)->setHelp(
190
		'This is the IP address of the server to which DHCP requests are relayed.'
191
	)->setIsRepeated();
192
	
193
	$group->enableDuplication(null, true); // Buttons are in-line with the input
194
	return $group;
195
}
196

    
197
if (!isset($pconfig['server']) || count($pconfig['server']) < 1)
198
	$section->add(createDestinationServerInputGroup());
199
else
200
	foreach ($pconfig['server'] as $idx => $server)
201
		$section->add(createDestinationServerInputGroup($server));
202

    
203
$form->add($section);
204
print $form;
205

    
206
include("foot.inc");
(135-135/238)