Project

General

Profile

Download (8.6 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
		if (!file_exists($d_sysrebootreqd_path)) {
130
			config_lock();
131
			$retval = services_dhcrelay_configure();
132
			config_unlock();
133
		}
134
		$savemsg = get_std_save_message($retval);
135

    
136
	}
137
}
138

    
139
?>
140
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
141
<html>
142
<head>
143
<title><?=gentitle("Services: DHCP relay");?></title>
144
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
145
<link href="gui.css" rel="stylesheet" type="text/css">
146
<script language="JavaScript">
147
<!--
148
function enable_change(enable_over) {
149
	if (document.iform.enable.checked || enable_over) {
150
		document.iform.server.disabled = 0;
151
		document.iform.agentoption.disabled = 0;
152
		document.iform.proxydhcp.disabled = 0;
153
	} else {
154
		document.iform.server.disabled = 1;
155
		document.iform.agentoption.disabled = 1;
156
		document.iform.proxydhcp.disabled = 1;
157
	}
158
	if (document.iform.proxydhcp.checked) {
159
		document.iform.server.disabled = 1;
160
	}
161
}
162
//-->
163
</script>
164
</head>
165

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