Project

General

Profile

Download (8.95 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." . $config['interfaces']['wan']['if'];
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
$ifdescrs = get_configured_interface_list();
67
foreach ($ifdescrs as $ifname) {
68
	$oc = $config['interfaces'][$ifname];
69

    
70
	if ($oc['if'] && (!$oc['bridge'])) {
71
		$iflist[$ifname] = $oc['descr'];
72
	}
73
}
74

    
75
if (!$if || !isset($iflist[$if]))
76
	$if = "lan";
77

    
78
$pconfig['enable'] = isset($config['dhcrelay'][$if]['enable']);
79
$pconfig['server'] = $config['dhcrelay']['server'];
80
$pconfig['proxydhcp'] = isset($config['dhcrelay']['proxydhcp']);
81
$pconfig['agentoption'] = isset($config['dhcrelay']['agentoption']);
82

    
83
$ifcfg = $config['interfaces'][$if];
84

    
85
/*   set the enabled flag which will tell us if DHCP server is enabled
86
 *   on any interface.   We will use this to disable dhcp-relay since
87
 *   the two are not compatible with each other.
88
 */
89
$dhcpd_enabled = false;
90
foreach($config['dhcpd'] as $dhcp) {
91
	if(isset($dhcp['enable'])) $dhcpd_enabled = true;
92
}
93

    
94
if ($_POST) {
95

    
96
	unset($input_errors);
97
	$pconfig = $_POST;
98

    
99
	/* input validation */
100
	if ($_POST['enable']) {
101
		if (isset($_POST['proxydhcp']))
102
			$_POST['server'] = get_wan_dhcp_server();
103
		$reqdfields = explode(" ", "server");
104
		$reqdfieldsn = explode(",", "Destination Server");
105

    
106
		do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
107

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

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

    
126
			/* if proxydhcp is selected, make sure DHCP is enabled on WAN */
127
			if (isset($config['dhcrelay']['proxydhcp']) && $config['interfaces']['wan']['ipaddr'] != "dhcp")
128
				$input_errors[] = "You must have DHCP active on the WAN interface before enabling the DHCP proxy option.";
129
		}
130
	}
131

    
132
	if (!$input_errors) {
133
		$config['dhcrelay']['agentoption'] = $_POST['agentoption'] ? true : false;
134
		$config['dhcrelay']['proxydhcp'] = $_POST['proxydhcp'] ? true : false;
135
		$config['dhcrelay']['server'] = $_POST['server'];
136
		$config['dhcrelay'][$if]['enable'] = $_POST['enable'] ? true : false;
137

    
138
		write_config();
139

    
140
		$retval = 0;
141
		config_lock();
142
		$retval = services_dhcrelay_configure();
143
		config_unlock();
144
		$savemsg = get_std_save_message($retval);
145

    
146
	}
147
}
148

    
149
$pgtitle = array("Services","DHCP Relay");
150
include("head.inc");
151

    
152
?>
153

    
154
<script language="JavaScript">
155
<!--
156
function enable_change(enable_over) {
157
	if (document.iform.enable.checked || enable_over) {
158
		document.iform.server.disabled = 0;
159
		document.iform.agentoption.disabled = 0;
160
		document.iform.proxydhcp.disabled = 0;
161
	} else {
162
		document.iform.server.disabled = 1;
163
		document.iform.agentoption.disabled = 1;
164
		document.iform.proxydhcp.disabled = 1;
165
	}
166
	if (document.iform.proxydhcp.checked) {
167
		document.iform.server.disabled = 1;
168
	}
169
}
170
//-->
171
</script>
172

    
173

    
174
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
175
<?php include("fbegin.inc"); ?>
176
<form action="services_dhcp_relay.php" method="post" name="iform" id="iform">
177
<?php if ($input_errors) print_input_errors($input_errors); ?>
178
<?php if ($savemsg) print_info_box($savemsg); ?>
179
<?php 
180
	if ($dhcpd_enabled) {
181
		echo "DHCP Server is currently enabled.  Cannot enable the DHCP Relay service while the DHCP Server is enabled on any interface.";
182
		include("fend.inc"); 
183
		echo "</body>";
184
		echo "</html>";
185
		exit;
186
	}
187
?>
188

    
189
<table width="100%" border="0" cellpadding="0" cellspacing="0">
190
  <tr><td>  
191
  <?php
192
	/* active tabs */
193
	$tab_array = array();
194
	$tabscounter = 0;
195
	$i = 0;
196
	foreach ($iflist as $ifent => $ifname) {
197
		if ($ifent == $if)
198
			$active = true;
199
		else
200
			$active = false;
201
		$tab_array[] = array($ifname, $active, "services_dhcp_relay.php?if={$ifent}");
202
	}
203
	display_top_tabs($tab_array);
204
  ?>  
205
  </td></tr>
206
  <tr>
207
    <td>
208
	<div id="mainarea">
209
              <table class="tabcont" width="100%" border="0" cellpadding="6" cellspacing="0">
210
                      <tr>
211
                        <td width="22%" valign="top" class="vtable">&nbsp;</td>
212
                        <td width="78%" class="vtable">
213
<input name="enable" type="checkbox" value="yes" <?php if ($pconfig['enable']) echo "checked"; ?> onClick="enable_change(false)">
214
                          <strong>Enable DHCP relay on
215
                          <?=htmlspecialchars($iflist[$if]);?>
216
                          interface</strong></td>
217
                      </tr>
218
			<tr>
219
	              <td width="22%" valign="top" class="vtable">&nbsp;</td>
220
                      <td width="78%" class="vtable">
221
<input name="agentoption" type="checkbox" value="yes" <?php if ($pconfig['agentoption']) echo "checked"; ?>>
222
                      <strong>Append circuit ID and agent ID to requests</strong><br>
223
                      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>
224
        		  </tr>
225
                      <tr>
226
                        <td width="22%" valign="top" class="vncell">Destination server</td>
227
                        <td width="78%" class="vtable">
228
			<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
229
                          <br><br><input name="server" type="text" class="formfld unknown" id="server" size="20" value="<?=htmlspecialchars($pconfig['server']);?>">
230
                          <br>
231
			  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.
232
                        </td>
233
                      </tr>
234
                      <tr>
235
                        <td width="22%" valign="top">&nbsp;</td>
236
                        <td width="78%">
237
                          <input name="if" type="hidden" value="<?=$if;?>">
238
                          <input name="Submit" type="submit" class="formbtn" value="Save" onclick="enable_change(true)">
239
                        </td>
240
                      </tr>
241
                    </table>
242
	</div>
243
    </td>
244
  </tr>
245
</table>
246
</form>
247
<script language="JavaScript">
248
<!--
249
enable_change(false);
250
//-->
251
</script>
252
<?php include("fend.inc"); ?>
253
</body>
254
</html>
(118-118/200)