Project

General

Profile

Download (8.43 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	services_dhcp.php
5
	part of m0n0wall (http://m0n0.ch/wall)
6

    
7
	Copyright (C) 2003-2004 Justin Ellison <justin@techadvise.com>.
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
function get_wan_dhcp_server() {
33
	global $config, $g;
34
	$dhclientfn = $g['vardb_path'] . "/dhclient.leases";
35
	if(file_exists($dhclientfn))
36
		$leases = file($dhclientfn);
37
	else 
38
		$leases = array();
39
	/* Start at the end, work backwards finding the latest lease for the WAN */
40
	$dhcpserver = "";
41
	$iface = "";
42
	$iface = "";
43
	for ($i = (count($leases)-1); $i >= 0; $i--) {
44
		if ($leases[$i] == "}") {
45
			unset($iface);
46
			unset($dhcpserver);
47
		} elseif (strstr($leases[$i],"interface")) {
48
			preg_match("/\s+interface \"(\w+)\";/",$leases[$i],$iface);
49
		}  	elseif (strstr($leases[$i],"dhcp-server-identifier")) {
50
				preg_match("/\s+dhcp-server-identifier (\d+\.\d+\.\d+\.\d+);/",$leases[$i],$dhcpserver);
51
			}
52
		if ($iface == $config['interfaces']['wan'] && isset($dhcpserver)) {
53
			break;
54
		}
55
	}
56
	return $dhcpserver[1];
57
}
58

    
59

    
60
require("guiconfig.inc");
61

    
62
$if = $_GET['if'];
63
if ($_POST['if'])
64
	$if = $_POST['if'];
65

    
66
$iflist = array("lan" => "LAN");
67

    
68
for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
69
	$oc = $config['interfaces']['opt' . $i];
70

    
71
	if (isset($oc['enable']) && $oc['if'] && (!$oc['bridge'])) {
72
		$iflist['opt' . $i] = $oc['descr'];
73
	}
74
}
75

    
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

    
87
if ($_POST) {
88

    
89
	unset($input_errors);
90
	$pconfig = $_POST;
91

    
92
	/* input validation */
93
	if ($_POST['enable']) {
94
		if (isset($_POST['proxydhcp']))
95
			$_POST['server'] = get_wan_dhcp_server();
96
		$reqdfields = explode(" ", "server");
97
		$reqdfieldsn = explode(",", "Destination Server");
98

    
99
		do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
100

    
101
		if (($_POST['server'] && !is_ipaddr($_POST['server'])))
102
			$input_errors[] = "A valid Destination Server IP address  must be specified.";
103

    
104
		if (!$input_errors) {
105
			/* make sure that the DHCP server isn't enabled on this interface */
106
			if (isset($config['dhcpd'][$if]['enable']))
107
				$input_errors[] = "You must disable the DHCP server on the {$iflist[$if]} interface before enabling the DHCP Relay.";
108
			/* make sure that the DHCP server isn't running on any of the implied interfaces */
109
			foreach ($config['interfaces'] as $ifname => $ifcfg) {
110
				$subnet = $ifcfg['ipaddr'] . "/" . $ifcfg['subnet'];
111
				if (ip_in_subnet($_POST['server'],$subnet))
112
					$destif = $ifname;
113
			}
114
			if (!isset($destif))
115
				$destif = "wan";
116
			if (isset($config['dhcpd'][$destif]['enable']))
117
				$input_errors[] = "You must disable the DHCP server on the {$destif} interface before enabling the DHCP Relay.";
118

    
119
			/* if proxydhcp is selected, make sure DHCP is enabled on WAN */
120
			if (isset($config['dhcrelay']['proxydhcp']) && $config['interfaces']['wan']['ipaddr'] != "dhcp")
121
				$input_errors[] = "You must have DHCP active on the WAN interface before enabling the DHCP proxy option.";
122
		}
123
	}
124

    
125
	if (!$input_errors) {
126
		$config['dhcrelay']['agentoption'] = $_POST['agentoption'] ? true : false;
127
		$config['dhcrelay']['proxydhcp'] = $_POST['proxydhcp'] ? true : false;
128
		$config['dhcrelay']['server'] = $_POST['server'];
129
		$config['dhcrelay'][$if]['enable'] = $_POST['enable'] ? true : false;
130

    
131
		write_config();
132

    
133
		$retval = 0;
134
		config_lock();
135
		$retval = services_dhcrelay_configure();
136
		config_unlock();
137
		$savemsg = get_std_save_message($retval);
138

    
139
	}
140
}
141

    
142
$pgtitle = "Services: DHCP Relay";
143
include("head.inc");
144

    
145
?>
146

    
147
<script language="JavaScript">
148
<!--
149
function enable_change(enable_over) {
150
	if (document.iform.enable.checked || enable_over) {
151
		document.iform.server.disabled = 0;
152
		document.iform.agentoption.disabled = 0;
153
		document.iform.proxydhcp.disabled = 0;
154
	} else {
155
		document.iform.server.disabled = 1;
156
		document.iform.agentoption.disabled = 1;
157
		document.iform.proxydhcp.disabled = 1;
158
	}
159
	if (document.iform.proxydhcp.checked) {
160
		document.iform.server.disabled = 1;
161
	}
162
}
163
//-->
164
</script>
165

    
166

    
167
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
168
<?php include("fbegin.inc"); ?>
169
<p class="pgtitle"><?=$pgtitle?></p>
170
<form action="services_dhcp_relay.php" method="post" name="iform" id="iform">
171
<?php if ($input_errors) print_input_errors($input_errors); ?>
172
<?php if ($savemsg) print_info_box($savemsg); ?>
173
<table width="100%" border="0" cellpadding="0" cellspacing="0">
174
  <tr><td>  
175
  <?php
176
	/* active tabs */
177
	$tab_array = array();
178
	$tabscounter = 0;
179
	$i = 0;
180
	foreach ($iflist as $ifent => $ifname) {
181
		if ($ifent == $if)
182
			$active = true;
183
		else
184
			$active = false;
185
		$tab_array[] = array($ifname, $active, "services_dhcp_relay.php?if={$ifent}");
186
	}
187
	display_top_tabs($tab_array);
188
  ?>  
189
  </td></tr>
190
  <tr>
191
    <td>
192
	<div id="mainarea">
193
              <table class="tabcont" width="100%" border="0" cellpadding="6" cellspacing="0">
194
                      <tr>
195
                        <td width="22%" valign="top" class="vtable">&nbsp;</td>
196
                        <td width="78%" class="vtable">
197
<input name="enable" type="checkbox" value="yes" <?php if ($pconfig['enable']) echo "checked"; ?> onClick="enable_change(false)">
198
                          <strong>Enable DHCP relay on
199
                          <?=htmlspecialchars($iflist[$if]);?>
200
                          interface</strong></td>
201
                      </tr>
202
			<tr>
203
	              <td width="22%" valign="top" class="vtable">&nbsp;</td>
204
                      <td width="78%" class="vtable">
205
<input name="agentoption" type="checkbox" value="yes" <?php if ($pconfig['agentoption']) echo "checked"; ?>>
206
                      <strong>Append circuit ID and agent ID to requests</strong><br>
207
                      If this is checked, the DHCP relay will append the circuit ID (pfSense interface number) and the agent ID to the DHCP request.</td>
208
        		  </tr>
209
                      <tr>
210
                        <td width="22%" valign="top" class="vncell">Destination server</td>
211
                        <td width="78%" class="vtable">
212
			<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
213
                          <br><br><input name="server" type="text" class="formfld" id="server" size="20" value="<?=htmlspecialchars($pconfig['server']);?>">
214
                          <br>
215
			  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.
216
                        </td>
217
                      </tr>
218
                      <tr>
219
                        <td width="22%" valign="top">&nbsp;</td>
220
                        <td width="78%">
221
                          <input name="if" type="hidden" value="<?=$if;?>">
222
                          <input name="Submit" type="submit" class="formbtn" value="Save" onclick="enable_change(true)">
223
                        </td>
224
                      </tr>
225
                    </table>
226
	</div>
227
    </td>
228
  </tr>
229
</table>
230
</form>
231
<script language="JavaScript">
232
<!--
233
enable_change(false);
234
//-->
235
</script>
236
<?php include("fend.inc"); ?>
237
</body>
238
</html>
(109-109/169)