Project

General

Profile

Download (8.59 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php
2
<?php
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
	for ($i = (count($leases)-1); $i >= 0; $i--) {
38
		if ($leases[$i] == "}") {
39
			unset($iface);
40
			unset($dhcpserver);
41
		} elseif (strstr($leases[$i],"interface")) {
42
			preg_match("/\s+interface \"(\w+)\";/",$leases[$i],$iface);
43
		}  	elseif (strstr($leases[$i],"dhcp-server-identifier")) {
44
				preg_match("/\s+dhcp-server-identifier (\d+\.\d+\.\d+\.\d+);/",$leases[$i],$dhcpserver);
45
			}
46
		if ($iface == $config['interfaces']['wan'] && isset($dhcpserver)) {
47
			break;
48
		}
49
	}
50
	return $dhcpserver[1];
51
}
52

    
53

    
54
require("guiconfig.inc");
55

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

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

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

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

    
70
if (!$if || !isset($iflist[$if]))
71
	$if = "lan";
72

    
73
$pconfig['enable'] = isset($config['dhcrelay'][$if]['enable']);
74
$pconfig['server'] = $config['dhcrelay']['server'];
75
$pconfig['proxydhcp'] = isset($config['dhcrelay']['proxydhcp']);
76
$pconfig['agentoption'] = isset($config['dhcrelay']['agentoption']);
77

    
78
$ifcfg = $config['interfaces'][$if];
79

    
80

    
81
if ($_POST) {
82

    
83
	unset($input_errors);
84
	$pconfig = $_POST;
85

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

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

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

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

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

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

    
125
		write_config();
126

    
127
		$retval = 0;
128
		if (!file_exists($d_sysrebootreqd_path)) {
129
			config_lock();
130
			$retval = services_dhcrelay_configure();
131
			config_unlock();
132
		}
133
		$savemsg = get_std_save_message($retval);
134

    
135
	}
136
}
137

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

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