Project

General

Profile

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