Project

General

Profile

Download (9.02 KB) Statistics
| Branch: | Tag: | Revision:
1 de3fa7a6 Scott Ullrich
<?php
2 b46bfcf5 Bill Marquette
/* $Id$ */
3 5b237745 Scott Ullrich
/*
4
	services_dhcp.php
5
	part of m0n0wall (http://m0n0.ch/wall)
6 de3fa7a6 Scott Ullrich
7 5b237745 Scott Ullrich
	Copyright (C) 2003-2004 Justin Ellison <justin@techadvise.com>.
8
	All rights reserved.
9 de3fa7a6 Scott Ullrich
10 5b237745 Scott Ullrich
	Redistribution and use in source and binary forms, with or without
11
	modification, are permitted provided that the following conditions are met:
12 de3fa7a6 Scott Ullrich
13 5b237745 Scott Ullrich
	1. Redistributions of source code must retain the above copyright notice,
14
	   this list of conditions and the following disclaimer.
15 de3fa7a6 Scott Ullrich
16 5b237745 Scott Ullrich
	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 de3fa7a6 Scott Ullrich
20 5b237745 Scott Ullrich
	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 1d333258 Scott Ullrich
/*
32
	pfSense_MODULE:	dhcprelay
33
*/
34 5b237745 Scott Ullrich
35 6b07c15a Matthew Grooms
##|+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 5b237745 Scott Ullrich
function get_wan_dhcp_server() {
43
	global $config, $g;
44 29f7fa3b Chris Buechler
	$dhclientfn = $g['vardb_path'] . "/dhclient.leases." . $config['interfaces']['wan']['if'];
45 96c03796 Scott Ullrich
	if(file_exists($dhclientfn))
46
		$leases = file($dhclientfn);
47
	else 
48
		$leases = array();
49 5b237745 Scott Ullrich
	/* Start at the end, work backwards finding the latest lease for the WAN */
50 767a716e Scott Ullrich
	$dhcpserver = "";
51
	$iface = "";
52
	$iface = "";
53 5b237745 Scott Ullrich
	for ($i = (count($leases)-1); $i >= 0; $i--) {
54
		if ($leases[$i] == "}") {
55
			unset($iface);
56
			unset($dhcpserver);
57
		} elseif (strstr($leases[$i],"interface")) {
58
			preg_match("/\s+interface \"(\w+)\";/",$leases[$i],$iface);
59
		}  	elseif (strstr($leases[$i],"dhcp-server-identifier")) {
60
				preg_match("/\s+dhcp-server-identifier (\d+\.\d+\.\d+\.\d+);/",$leases[$i],$dhcpserver);
61
			}
62
		if ($iface == $config['interfaces']['wan'] && isset($dhcpserver)) {
63
			break;
64
		}
65 de3fa7a6 Scott Ullrich
	}
66 5b237745 Scott Ullrich
	return $dhcpserver[1];
67
}
68
69
70
require("guiconfig.inc");
71
72
$if = $_GET['if'];
73
if ($_POST['if'])
74
	$if = $_POST['if'];
75 de3fa7a6 Scott Ullrich
76 25b4e968 Ermal Luçi
$iflist = get_configured_interface_with_descr();
77 5b237745 Scott Ullrich
78
if (!$if || !isset($iflist[$if]))
79
	$if = "lan";
80
81
$pconfig['enable'] = isset($config['dhcrelay'][$if]['enable']);
82
$pconfig['server'] = $config['dhcrelay']['server'];
83
$pconfig['proxydhcp'] = isset($config['dhcrelay']['proxydhcp']);
84
$pconfig['agentoption'] = isset($config['dhcrelay']['agentoption']);
85
86
$ifcfg = $config['interfaces'][$if];
87
88 48c6ab80 Scott Ullrich
/*   set the enabled flag which will tell us if DHCP server is enabled
89
 *   on any interface.   We will use this to disable dhcp-relay since
90
 *   the two are not compatible with each other.
91
 */
92
$dhcpd_enabled = false;
93 b9ed163d Ermal Luçi
if (is_array($config['dhcpd'])) {
94
	foreach($config['dhcpd'] as $dhcp) 
95
		if(isset($dhcp['enable'])) 
96
			$dhcpd_enabled = true;
97 3d7b7757 Chris Buechler
}
98 5b237745 Scott Ullrich
99
if ($_POST) {
100
101
	unset($input_errors);
102
	$pconfig = $_POST;
103
104
	/* input validation */
105
	if ($_POST['enable']) {
106
		if (isset($_POST['proxydhcp']))
107
			$_POST['server'] = get_wan_dhcp_server();
108
		$reqdfields = explode(" ", "server");
109
		$reqdfieldsn = explode(",", "Destination Server");
110 de3fa7a6 Scott Ullrich
111 5b237745 Scott Ullrich
		do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
112 de3fa7a6 Scott Ullrich
113
		if (($_POST['server'] && !is_ipaddr($_POST['server'])))
114 5b237745 Scott Ullrich
			$input_errors[] = "A valid Destination Server IP address  must be specified.";
115 de3fa7a6 Scott Ullrich
116 5b237745 Scott Ullrich
		if (!$input_errors) {
117
			/* make sure that the DHCP server isn't enabled on this interface */
118 de3fa7a6 Scott Ullrich
			if (isset($config['dhcpd'][$if]['enable']))
119 5b237745 Scott Ullrich
				$input_errors[] = "You must disable the DHCP server on the {$iflist[$if]} interface before enabling the DHCP Relay.";
120
			/* make sure that the DHCP server isn't running on any of the implied interfaces */
121
			foreach ($config['interfaces'] as $ifname => $ifcfg) {
122
				$subnet = $ifcfg['ipaddr'] . "/" . $ifcfg['subnet'];
123 de3fa7a6 Scott Ullrich
				if (ip_in_subnet($_POST['server'],$subnet))
124
					$destif = $ifname;
125
			}
126
			if (!isset($destif))
127 5b237745 Scott Ullrich
				$destif = "wan";
128 de3fa7a6 Scott Ullrich
			if (isset($config['dhcpd'][$destif]['enable']))
129 5b237745 Scott Ullrich
				$input_errors[] = "You must disable the DHCP server on the {$destif} interface before enabling the DHCP Relay.";
130 de3fa7a6 Scott Ullrich
131 5b237745 Scott Ullrich
			/* if proxydhcp is selected, make sure DHCP is enabled on WAN */
132 de3fa7a6 Scott Ullrich
			if (isset($config['dhcrelay']['proxydhcp']) && $config['interfaces']['wan']['ipaddr'] != "dhcp")
133 5b237745 Scott Ullrich
				$input_errors[] = "You must have DHCP active on the WAN interface before enabling the DHCP proxy option.";
134
		}
135
	}
136
137
	if (!$input_errors) {
138
		$config['dhcrelay']['agentoption'] = $_POST['agentoption'] ? true : false;
139
		$config['dhcrelay']['proxydhcp'] = $_POST['proxydhcp'] ? true : false;
140
		$config['dhcrelay']['server'] = $_POST['server'];
141
		$config['dhcrelay'][$if]['enable'] = $_POST['enable'] ? true : false;
142 de3fa7a6 Scott Ullrich
143 5b237745 Scott Ullrich
		write_config();
144 de3fa7a6 Scott Ullrich
145 5b237745 Scott Ullrich
		$retval = 0;
146 3851094f Scott Ullrich
		$retval = services_dhcrelay_configure();
147 5b237745 Scott Ullrich
		$savemsg = get_std_save_message($retval);
148 de3fa7a6 Scott Ullrich
149 5b237745 Scott Ullrich
	}
150
}
151
152 d88c6a9f Scott Ullrich
$pgtitle = array("Services","DHCP Relay");
153 4df96eff Scott Ullrich
include("head.inc");
154
155 5b237745 Scott Ullrich
?>
156 4df96eff Scott Ullrich
157 5b237745 Scott Ullrich
<script language="JavaScript">
158
<!--
159 471877bf Scott Ullrich
function enable_change(enable_over) {
160
	if (document.iform.enable.checked || enable_over) {
161
		document.iform.server.disabled = 0;
162
		document.iform.agentoption.disabled = 0;
163
		document.iform.proxydhcp.disabled = 0;
164
	} else {
165
		document.iform.server.disabled = 1;
166
		document.iform.agentoption.disabled = 1;
167
		document.iform.proxydhcp.disabled = 1;
168
	}
169
	if (document.iform.proxydhcp.checked) {
170
		document.iform.server.disabled = 1;
171
	}
172 5b237745 Scott Ullrich
}
173
//-->
174
</script>
175 4df96eff Scott Ullrich
176 5b237745 Scott Ullrich
177
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
178
<?php include("fbegin.inc"); ?>
179
<form action="services_dhcp_relay.php" method="post" name="iform" id="iform">
180
<?php if ($input_errors) print_input_errors($input_errors); ?>
181
<?php if ($savemsg) print_info_box($savemsg); ?>
182 48c6ab80 Scott Ullrich
<?php 
183
	if ($dhcpd_enabled) {
184 3d7b7757 Chris Buechler
		echo "DHCP Server is currently enabled.  Cannot enable the DHCP Relay service while the DHCP Server is enabled on any interface.";
185 48c6ab80 Scott Ullrich
		include("fend.inc"); 
186
		echo "</body>";
187
		echo "</html>";
188
		exit;
189
	}
190
?>
191
192 5b237745 Scott Ullrich
<table width="100%" border="0" cellpadding="0" cellspacing="0">
193 e2aa3617 Scott Ullrich
  <tr><td>  
194
  <?php
195
	/* active tabs */
196
	$tab_array = array();
197
	$tabscounter = 0;
198
	$i = 0;
199
	foreach ($iflist as $ifent => $ifname) {
200
		if ($ifent == $if)
201
			$active = true;
202
		else
203
			$active = false;
204
		$tab_array[] = array($ifname, $active, "services_dhcp_relay.php?if={$ifent}");
205
	}
206
	display_top_tabs($tab_array);
207
  ?>  
208 5b237745 Scott Ullrich
  </td></tr>
209 de3fa7a6 Scott Ullrich
  <tr>
210 d732f186 Bill Marquette
    <td>
211
	<div id="mainarea">
212
              <table class="tabcont" width="100%" border="0" cellpadding="6" cellspacing="0">
213 de3fa7a6 Scott Ullrich
                      <tr>
214 5b237745 Scott Ullrich
                        <td width="22%" valign="top" class="vtable">&nbsp;</td>
215
                        <td width="78%" class="vtable">
216 471877bf Scott Ullrich
<input name="enable" type="checkbox" value="yes" <?php if ($pconfig['enable']) echo "checked"; ?> onClick="enable_change(false)">
217 de3fa7a6 Scott Ullrich
                          <strong>Enable DHCP relay on
218 5b237745 Scott Ullrich
                          <?=htmlspecialchars($iflist[$if]);?>
219
                          interface</strong></td>
220
                      </tr>
221
			<tr>
222
	              <td width="22%" valign="top" class="vtable">&nbsp;</td>
223
                      <td width="78%" class="vtable">
224
<input name="agentoption" type="checkbox" value="yes" <?php if ($pconfig['agentoption']) echo "checked"; ?>>
225
                      <strong>Append circuit ID and agent ID to requests</strong><br>
226 d86b275c Chris Buechler
                      If this is checked, the DHCP relay will append the circuit ID (<?=$g['product_name'];?> interface number) and the agent ID to the DHCP request.</td>
227 5b237745 Scott Ullrich
        		  </tr>
228 de3fa7a6 Scott Ullrich
                      <tr>
229 5b237745 Scott Ullrich
                        <td width="22%" valign="top" class="vncell">Destination server</td>
230 de3fa7a6 Scott Ullrich
                        <td width="78%" class="vtable">
231 471877bf Scott Ullrich
			<input name="proxydhcp" type="checkbox" value="yes" <?php if ($pconfig['proxydhcp']) echo "checked"; ?> onClick="enable_change(false)">  Proxy requests to DHCP server on WAN subnet
232 b5c78501 Seth Mos
                          <br><br><input name="server" type="text" class="formfld unknown" id="server" size="20" value="<?=htmlspecialchars($pconfig['server']);?>">
233 5b237745 Scott Ullrich
                          <br>
234
			  This is the IP address of the server to which the DHCP packet is relayed.  Select "Proxy requests to DHCP server on WAN subnet" to relay DHCP packets to the server that was used on the WAN interface.
235
                        </td>
236
                      </tr>
237 de3fa7a6 Scott Ullrich
                      <tr>
238 5b237745 Scott Ullrich
                        <td width="22%" valign="top">&nbsp;</td>
239 de3fa7a6 Scott Ullrich
                        <td width="78%">
240
                          <input name="if" type="hidden" value="<?=$if;?>">
241 471877bf Scott Ullrich
                          <input name="Submit" type="submit" class="formbtn" value="Save" onclick="enable_change(true)">
242 5b237745 Scott Ullrich
                        </td>
243
                      </tr>
244
                    </table>
245 d732f186 Bill Marquette
	</div>
246 5b237745 Scott Ullrich
    </td>
247
  </tr>
248
</table>
249
</form>
250
<script language="JavaScript">
251
<!--
252 471877bf Scott Ullrich
enable_change(false);
253 5b237745 Scott Ullrich
//-->
254
</script>
255
<?php include("fend.inc"); ?>
256
</body>
257
</html>