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
|
if (empty($config['dhcrelay']['interface']))
|
54
|
$pconfig['interface'] = array();
|
55
|
else
|
56
|
$pconfig['interface'] = explode(",", $config['dhcrelay']['interface']);
|
57
|
|
58
|
$pconfig['server'] = filterDestinationServers(
|
59
|
explode(',', $config['dhcrelay']['server'])
|
60
|
);
|
61
|
$pconfig['agentoption'] = isset($config['dhcrelay']['agentoption']);
|
62
|
|
63
|
$iflist = array_intersect_key(
|
64
|
get_configured_interface_with_descr(),
|
65
|
array_flip(
|
66
|
array_filter(
|
67
|
array_keys(get_configured_interface_with_descr()),
|
68
|
function($if) {
|
69
|
return is_ipaddr(get_interface_ip($if));
|
70
|
}
|
71
|
)
|
72
|
)
|
73
|
);
|
74
|
|
75
|
/* set the enabled flag which will tell us if DHCP server is enabled
|
76
|
* on any interface. We will use this to disable dhcp-relay since
|
77
|
* the two are not compatible with each other.
|
78
|
*/
|
79
|
$dhcpd_enabled = false;
|
80
|
if (is_array($config['dhcpd'])) {
|
81
|
foreach($config['dhcpd'] as $dhcpif => $dhcp) {
|
82
|
if (isset($dhcp['enable']) && isset($config['interfaces'][$dhcpif]['enable'])) {
|
83
|
$dhcpd_enabled = true;
|
84
|
break;
|
85
|
}
|
86
|
}
|
87
|
}
|
88
|
|
89
|
if ($_POST) {
|
90
|
|
91
|
unset($input_errors);
|
92
|
|
93
|
if ($_POST['server'])
|
94
|
$_POST['server'] = filterDestinationServers($_POST['server']);
|
95
|
|
96
|
$pconfig = $_POST;
|
97
|
|
98
|
/* input validation */
|
99
|
if ($_POST['enable']) {
|
100
|
$reqdfields = explode(" ", "server interface");
|
101
|
$reqdfieldsn = array(gettext("Destination Server"), gettext("Interface"));
|
102
|
|
103
|
do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
|
104
|
|
105
|
if ($_POST['server']) {
|
106
|
foreach ($_POST['server'] as $srv) {
|
107
|
if (!is_ipaddr($srv))
|
108
|
$input_errors[] = gettext("A valid Destination Server IP address must be specified.");
|
109
|
}
|
110
|
}
|
111
|
}
|
112
|
|
113
|
if (!$input_errors) {
|
114
|
$config['dhcrelay']['enable'] = $_POST['enable'] ? true : false;
|
115
|
$config['dhcrelay']['interface'] = implode(",", $_POST['interface']);
|
116
|
$config['dhcrelay']['agentoption'] = $_POST['agentoption'] ? true : false;
|
117
|
$config['dhcrelay']['server'] = implode(',', $_POST['server']);
|
118
|
|
119
|
write_config();
|
120
|
|
121
|
$retval = 0;
|
122
|
$retval = services_dhcrelay_configure();
|
123
|
$savemsg = get_std_save_message($retval);
|
124
|
|
125
|
}
|
126
|
}
|
127
|
|
128
|
$closehead = false;
|
129
|
$pgtitle = array(gettext("Services"),gettext("DHCP Relay"));
|
130
|
$shortcut_section = "dhcp";
|
131
|
include("head.inc");
|
132
|
|
133
|
if ($dhcpd_enabled)
|
134
|
{
|
135
|
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>';
|
136
|
include("foot.inc");
|
137
|
exit;
|
138
|
}
|
139
|
|
140
|
if ($input_errors)
|
141
|
print_input_errors($input_errors);
|
142
|
|
143
|
if ($savemsg)
|
144
|
print_info_box($savemsg);
|
145
|
|
146
|
$form = new Form;
|
147
|
|
148
|
$section = new Form_Section('DHCP Relay configuration');
|
149
|
|
150
|
$section->addInput(new Form_Checkbox(
|
151
|
'enable',
|
152
|
'Enable',
|
153
|
'Enable DHCP relay on interface',
|
154
|
$pconfig['enable']
|
155
|
))->toggles('.form-group:not(:first-child)');
|
156
|
|
157
|
$section->addInput(new Form_Select(
|
158
|
'interface',
|
159
|
'Interface(s)',
|
160
|
$pconfig['interface'],
|
161
|
$iflist,
|
162
|
true
|
163
|
))->setHelp('Interfaces without an IP address will not be shown.');
|
164
|
|
165
|
$section->addInput(new Form_Checkbox(
|
166
|
'agentoption',
|
167
|
'',
|
168
|
'Append circuit ID and agent ID to requests',
|
169
|
'yes',
|
170
|
$pconfig['agentoption']
|
171
|
))->setHelp(
|
172
|
'If this is checked, the DHCP relay will append the circuit ID (%s interface number) and the agent ID to the DHCP request.',
|
173
|
[$g['product_name']]
|
174
|
);
|
175
|
|
176
|
//Small function to prevent duplicate code
|
177
|
function createDestinationServerInputGroup($value = null)
|
178
|
{
|
179
|
$group = new Form_Group('Destination server');
|
180
|
$group->enableDuplication();
|
181
|
|
182
|
$group->add(new Form_IpAddress(
|
183
|
'server',
|
184
|
'Destination server',
|
185
|
$value
|
186
|
))->setHelp(
|
187
|
'This is the IP address of the server to which DHCP requests are relayed.'
|
188
|
)->setIsRepeated();
|
189
|
|
190
|
return $group;
|
191
|
}
|
192
|
|
193
|
if (!isset($pconfig['server']) || count($pconfig['server']) < 1)
|
194
|
$section->add(createDestinationServerInputGroup());
|
195
|
else
|
196
|
foreach ($pconfig['server'] as $idx => $server)
|
197
|
$section->add(createDestinationServerInputGroup($server));
|
198
|
|
199
|
$form->add($section);
|
200
|
print $form;
|
201
|
|
202
|
include("foot.inc");
|