Project

General

Profile

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

    
8
	Copyright (C) 2003-2004 Justin Ellison <justin@techadvise.com>.
9
	All rights reserved.
10

    
11
	Redistribution and use in source and binary forms, with or without
12
	modification, are permitted provided that the following conditions are met:
13

    
14
	1. Redistributions of source code must retain the above copyright notice,
15
	   this list of conditions and the following disclaimer.
16

    
17
	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

    
21
	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
	}
51
	return $dhcpserver[1];
52
}
53

    
54

    
55
require("guiconfig.inc");
56

    
57
$if = $_GET['if'];
58
if ($_POST['if'])
59
	$if = $_POST['if'];
60

    
61
$iflist = array("lan" => "LAN");
62

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

    
66
	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

    
94
		do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
95

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

    
99
		if (!$input_errors) {
100
			/* make sure that the DHCP server isn't enabled on this interface */
101
			if (isset($config['dhcpd'][$if]['enable']))
102
				$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
				if (ip_in_subnet($_POST['server'],$subnet))
107
					$destif = $ifname;
108
			}
109
			if (!isset($destif))
110
				$destif = "wan";
111
			if (isset($config['dhcpd'][$destif]['enable']))
112
				$input_errors[] = "You must disable the DHCP server on the {$destif} interface before enabling the DHCP Relay.";
113

    
114
			/* if proxydhcp is selected, make sure DHCP is enabled on WAN */
115
			if (isset($config['dhcrelay']['proxydhcp']) && $config['interfaces']['wan']['ipaddr'] != "dhcp")
116
				$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

    
126
		write_config();
127

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

    
134
	}
135
}
136

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

    
140
?>
141

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

    
161

    
162
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
163
<?php include("fbegin.inc"); ?>
164
<p class="pgtitle"><?=$pgtitle?></p>
165
<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
  <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
  </td></tr>
185
  <tr>
186
    <td>
187
	<div id="mainarea">
188
              <table class="tabcont" width="100%" border="0" cellpadding="6" cellspacing="0">
189
                      <tr>
190
                        <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
                          <strong>Enable DHCP relay on
194
                          <?=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
                      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
        		  </tr>
204
                      <tr>
205
                        <td width="22%" valign="top" class="vncell">Destination server</td>
206
                        <td width="78%" class="vtable">
207
			<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
                      <tr>
214
                        <td width="22%" valign="top">&nbsp;</td>
215
                        <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
                        </td>
219
                      </tr>
220
                    </table>
221
	</div>
222
    </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>
(96-96/144)