Project

General

Profile

Download (8.35 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 3851094f Scott Ullrich
		config_lock();
130
		$retval = services_dhcrelay_configure();
131
		config_unlock();
132 5b237745 Scott Ullrich
		$savemsg = get_std_save_message($retval);
133 de3fa7a6 Scott Ullrich
134 5b237745 Scott Ullrich
	}
135
}
136
137 4df96eff Scott Ullrich
$pgtitle = "Services: DHCP Relay";
138
include("head.inc");
139
140 5b237745 Scott Ullrich
?>
141 4df96eff Scott Ullrich
142 5b237745 Scott Ullrich
<script language="JavaScript">
143
<!--
144
function enable_change(enable_over) {
145
	if (document.iform.enable.checked || enable_over) {
146
		document.iform.server.disabled = 0;
147
		document.iform.agentoption.disabled = 0;
148
		document.iform.proxydhcp.disabled = 0;
149
	} else {
150
		document.iform.server.disabled = 1;
151
		document.iform.agentoption.disabled = 1;
152
		document.iform.proxydhcp.disabled = 1;
153
	}
154
	if (document.iform.proxydhcp.checked) {
155
		document.iform.server.disabled = 1;
156
	}
157
}
158
//-->
159
</script>
160 4df96eff Scott Ullrich
161 5b237745 Scott Ullrich
162
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
163
<?php include("fbegin.inc"); ?>
164 74f446e8 Bill Marquette
<p class="pgtitle"><?=$pgtitle?></p>
165 5b237745 Scott Ullrich
<form action="services_dhcp_relay.php" method="post" name="iform" id="iform">
166
<?php if ($input_errors) print_input_errors($input_errors); ?>
167
<?php if ($savemsg) print_info_box($savemsg); ?>
168
<table width="100%" border="0" cellpadding="0" cellspacing="0">
169 e2aa3617 Scott Ullrich
  <tr><td>  
170
  <?php
171
	/* active tabs */
172
	$tab_array = array();
173
	$tabscounter = 0;
174
	$i = 0;
175
	foreach ($iflist as $ifent => $ifname) {
176
		if ($ifent == $if)
177
			$active = true;
178
		else
179
			$active = false;
180
		$tab_array[] = array($ifname, $active, "services_dhcp_relay.php?if={$ifent}");
181
	}
182
	display_top_tabs($tab_array);
183
  ?>  
184 5b237745 Scott Ullrich
  </td></tr>
185 de3fa7a6 Scott Ullrich
  <tr>
186 d732f186 Bill Marquette
    <td>
187
	<div id="mainarea">
188
              <table class="tabcont" width="100%" border="0" cellpadding="6" cellspacing="0">
189 de3fa7a6 Scott Ullrich
                      <tr>
190 5b237745 Scott Ullrich
                        <td width="22%" valign="top" class="vtable">&nbsp;</td>
191
                        <td width="78%" class="vtable">
192
<input name="enable" type="checkbox" value="yes" <?php if ($pconfig['enable']) echo "checked"; ?> onClick="enable_change(false)">
193 de3fa7a6 Scott Ullrich
                          <strong>Enable DHCP relay on
194 5b237745 Scott Ullrich
                          <?=htmlspecialchars($iflist[$if]);?>
195
                          interface</strong></td>
196
                      </tr>
197
			<tr>
198
	              <td width="22%" valign="top" class="vtable">&nbsp;</td>
199
                      <td width="78%" class="vtable">
200
<input name="agentoption" type="checkbox" value="yes" <?php if ($pconfig['agentoption']) echo "checked"; ?>>
201
                      <strong>Append circuit ID and agent ID to requests</strong><br>
202 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>
203 5b237745 Scott Ullrich
        		  </tr>
204 de3fa7a6 Scott Ullrich
                      <tr>
205 5b237745 Scott Ullrich
                        <td width="22%" valign="top" class="vncell">Destination server</td>
206 de3fa7a6 Scott Ullrich
                        <td width="78%" class="vtable">
207 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
208
                          <br><br><input name="server" type="text" class="formfld" id="server" size="20" value="<?=htmlspecialchars($pconfig['server']);?>">
209
                          <br>
210
			  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.
211
                        </td>
212
                      </tr>
213 de3fa7a6 Scott Ullrich
                      <tr>
214 5b237745 Scott Ullrich
                        <td width="22%" valign="top">&nbsp;</td>
215 de3fa7a6 Scott Ullrich
                        <td width="78%">
216
                          <input name="if" type="hidden" value="<?=$if;?>">
217
                          <input name="Submit" type="submit" class="formbtn" value="Save" onclick="enable_change(true)">
218 5b237745 Scott Ullrich
                        </td>
219
                      </tr>
220
                    </table>
221 d732f186 Bill Marquette
	</div>
222 5b237745 Scott Ullrich
    </td>
223
  </tr>
224
</table>
225
</form>
226
<script language="JavaScript">
227
<!--
228
enable_change(false);
229
//-->
230
</script>
231
<?php include("fend.inc"); ?>
232
</body>
233
</html>