Project

General

Profile

Download (8.38 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
	$leases = file($dhclientfn);
36
	/* Start at the end, work backwards finding the latest lease for the WAN */
37
	$dhcpserver = "";
38
	$iface = "";
39
	$iface = "";
40
	for ($i = (count($leases)-1); $i >= 0; $i--) {
41
		if ($leases[$i] == "}") {
42
			unset($iface);
43
			unset($dhcpserver);
44
		} elseif (strstr($leases[$i],"interface")) {
45
			preg_match("/\s+interface \"(\w+)\";/",$leases[$i],$iface);
46
		}  	elseif (strstr($leases[$i],"dhcp-server-identifier")) {
47
				preg_match("/\s+dhcp-server-identifier (\d+\.\d+\.\d+\.\d+);/",$leases[$i],$dhcpserver);
48
			}
49
		if ($iface == $config['interfaces']['wan'] && isset($dhcpserver)) {
50
			break;
51
		}
52
	}
53
	return $dhcpserver[1];
54
}
55

    
56

    
57
require("guiconfig.inc");
58

    
59
$if = $_GET['if'];
60
if ($_POST['if'])
61
	$if = $_POST['if'];
62

    
63
$iflist = array("lan" => "LAN");
64

    
65
for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
66
	$oc = $config['interfaces']['opt' . $i];
67

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

    
73
if (!$if || !isset($iflist[$if]))
74
	$if = "lan";
75

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

    
81
$ifcfg = $config['interfaces'][$if];
82

    
83

    
84
if ($_POST) {
85

    
86
	unset($input_errors);
87
	$pconfig = $_POST;
88

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

    
96
		do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
97

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

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

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

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

    
128
		write_config();
129

    
130
		$retval = 0;
131
		config_lock();
132
		$retval = services_dhcrelay_configure();
133
		config_unlock();
134
		$savemsg = get_std_save_message($retval);
135

    
136
	}
137
}
138

    
139
$pgtitle = "Services: DHCP Relay";
140
include("head.inc");
141

    
142
?>
143

    
144
<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

    
163

    
164
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
165
<?php include("fbegin.inc"); ?>
166
<p class="pgtitle"><?=$pgtitle?></p>
167
<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
  <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
  </td></tr>
187
  <tr>
188
    <td>
189
	<div id="mainarea">
190
              <table class="tabcont" width="100%" border="0" cellpadding="6" cellspacing="0">
191
                      <tr>
192
                        <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
                          <strong>Enable DHCP relay on
196
                          <?=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
                      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
        		  </tr>
206
                      <tr>
207
                        <td width="22%" valign="top" class="vncell">Destination server</td>
208
                        <td width="78%" class="vtable">
209
			<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
                      <tr>
216
                        <td width="22%" valign="top">&nbsp;</td>
217
                        <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
                        </td>
221
                      </tr>
222
                    </table>
223
	</div>
224
    </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>
(105-105/162)