Project

General

Profile

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