Project

General

Profile

Download (14.5 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 Manuel Kasper <mk@neon1.net>.
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
require("guiconfig.inc");
33

    
34
$if = $_GET['if'];
35
if ($_POST['if'])
36
	$if = $_POST['if'];
37
	
38
$iflist = array("lan" => "LAN");
39

    
40
for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) {
41
	$oc = $config['interfaces']['opt' . $i];
42
	
43
	if (isset($oc['enable']) && $oc['if'] && (!$oc['bridge'])) {
44
		$iflist['opt' . $i] = $oc['descr'];
45
	}
46
}
47

    
48
if (!$if || !isset($iflist[$if]))
49
	$if = "lan";
50

    
51
$pconfig['range_from'] = $config['dhcpd'][$if]['range']['from'];
52
$pconfig['range_to'] = $config['dhcpd'][$if]['range']['to'];
53
$pconfig['deftime'] = $config['dhcpd'][$if]['defaultleasetime'];
54
$pconfig['maxtime'] = $config['dhcpd'][$if]['maxleasetime'];
55
list($pconfig['wins1'],$pconfig['wins2']) = $config['dhcpd'][$if]['winsserver'];
56
$pconfig['enable'] = isset($config['dhcpd'][$if]['enable']);
57
$pconfig['denyunknown'] = isset($config['dhcpd'][$if]['denyunknown']);
58

    
59
$ifcfg = $config['interfaces'][$if];
60

    
61
if (!is_array($config['dhcpd'][$if]['staticmap'])) {
62
	$config['dhcpd'][$if]['staticmap'] = array();
63
}
64
staticmaps_sort($if);
65
$a_maps = &$config['dhcpd'][$if]['staticmap'];
66

    
67
if ($_POST) {
68

    
69
	unset($input_errors);
70
	$pconfig = $_POST;
71

    
72
	/* input validation */
73
	if ($_POST['enable']) {
74
		$reqdfields = explode(" ", "range_from range_to");
75
		$reqdfieldsn = explode(",", "Range begin,Range end");
76
		
77
		do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
78
		
79
		if (($_POST['range_from'] && !is_ipaddr($_POST['range_from']))) {
80
			$input_errors[] = "A valid range must be specified.";
81
		}
82
		if (($_POST['range_to'] && !is_ipaddr($_POST['range_to']))) {
83
			$input_errors[] = "A valid range must be specified.";
84
		}
85
		if (($_POST['wins1'] && !is_ipaddr($_POST['wins1'])) || ($_POST['wins2'] && !is_ipaddr($_POST['wins2']))) {
86
			$input_errors[] = "A valid IP address must be specified for the primary/secondary WINS server.";
87
		}
88
		if ($_POST['deftime'] && (!is_numeric($_POST['deftime']) || ($_POST['deftime'] < 60))) {
89
			$input_errors[] = "The default lease time must be at least 60 seconds.";
90
		}
91
		if ($_POST['maxtime'] && (!is_numeric($_POST['maxtime']) || ($_POST['maxtime'] < 60) || ($_POST['maxtime'] <= $_POST['deftime']))) {
92
			$input_errors[] = "The maximum lease time must be at least 60 seconds and higher than the default lease time.";
93
		}
94
		
95
		if (!$input_errors) {
96
			/* make sure the range lies within the current subnet */
97
			$subnet_start = (ip2long($ifcfg['ipaddr']) & gen_subnet_mask_long($ifcfg['subnet']));
98
			$subnet_end = (ip2long($ifcfg['ipaddr']) | (~gen_subnet_mask_long($ifcfg['subnet'])));
99
			
100
			if ((ip2long($_POST['range_from']) < $subnet_start) || (ip2long($_POST['range_from']) > $subnet_end) ||
101
			    (ip2long($_POST['range_to']) < $subnet_start) || (ip2long($_POST['range_to']) > $subnet_end)) {
102
				$input_errors[] = "The specified range lies outside of the current subnet.";	
103
			}
104
			
105
			if (ip2long($_POST['range_from']) > ip2long($_POST['range_to']))
106
				$input_errors[] = "The range is invalid (first element higher than second element).";
107
			
108
			/* make sure that the DHCP Relay isn't enabled on this interface */
109
			if (isset($config['dhcrelay'][$if]['enable']))
110
				$input_errors[] = "You must disable the DHCP relay on the {$iflist[$if]} interface before enabling the DHCP server.";
111
		}
112
	}
113

    
114
	if (!$input_errors) {
115
		$config['dhcpd'][$if]['range']['from'] = $_POST['range_from'];
116
		$config['dhcpd'][$if]['range']['to'] = $_POST['range_to'];
117
		$config['dhcpd'][$if]['defaultleasetime'] = $_POST['deftime'];
118
		$config['dhcpd'][$if]['maxleasetime'] = $_POST['maxtime'];
119
		$config['dhcpd'][$if]['enable'] = $_POST['enable'] ? true : false;
120
		$config['dhcpd'][$if]['denyunknown'] = $_POST['denyunknown'] ? true : false;
121
		
122
		unset($config['dhcpd'][$if]['winsserver']);
123
		if ($_POST['wins1'])
124
			$config['dhcpd'][$if]['winsserver'][] = $_POST['wins1'];
125
		if ($_POST['wins2'])
126
			$config['dhcpd'][$if]['winsserver'][] = $_POST['wins2'];
127
			
128
		write_config();
129
		
130
		$retval = 0;
131
		if (!file_exists($d_sysrebootreqd_path)) {
132
			config_lock();
133
			$retval = services_dhcpd_configure();
134
			config_unlock();
135
		}
136
		$savemsg = get_std_save_message($retval);
137
		
138
		if ($retval == 0) {
139
			if (file_exists($d_staticmapsdirty_path))
140
				unlink($d_staticmapsdirty_path);
141
		}
142
	}
143
}
144

    
145
if ($_GET['act'] == "del") {
146
	if ($a_maps[$_GET['id']]) {
147
		unset($a_maps[$_GET['id']]);
148
		write_config();
149
		touch($d_staticmapsdirty_path);
150
		header("Location: services_dhcp.php?if={$if}");
151
		exit;
152
	}
153
}
154
?>
155
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
156
<html>
157
<head>
158
<title><?=gentitle("Services: DHCP server");?></title>
159
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
160
<link href="gui.css" rel="stylesheet" type="text/css">
161
<script language="JavaScript">
162
<!--
163
function enable_change(enable_over) {
164
	var endis;
165
	endis = !(document.iform.enable.checked || enable_over);
166
	
167
	document.iform.range_from.disabled = endis;
168
	document.iform.range_to.disabled = endis;
169
	document.iform.wins1.disabled = endis;
170
	document.iform.wins2.disabled = endis;
171
	document.iform.deftime.disabled = endis;
172
	document.iform.maxtime.disabled = endis;
173
}
174
//-->
175
</script>
176
</head>
177

    
178
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
179
<?php include("fbegin.inc"); ?>
180
<p class="pgtitle">Services: DHCP server</p>
181
<form action="services_dhcp.php" method="post" name="iform" id="iform">
182
<?php if ($input_errors) print_input_errors($input_errors); ?>
183
<?php if ($savemsg) print_info_box($savemsg); ?>
184
<?php if (file_exists($d_staticmapsdirty_path)): ?><p>
185
<?php print_info_box_np("The static mapping configuration has been changed.<br>You must apply the changes in order for them to take effect.");?><br>
186
<input name="apply" type="submit" class="formbtn" id="apply" value="Apply changes"></p>
187
<?php endif; ?>
188
<table width="100%" border="0" cellpadding="0" cellspacing="0">
189
  <tr><td>
190
  <ul id="tabnav">
191
<?php foreach ($iflist as $ifent => $ifname):
192
	if ($ifent == $if): ?>
193
    <li class="tabact"><?=htmlspecialchars($ifname);?></li>
194
<?php else: ?>
195
    <li class="tabinact"><a href="services_dhcp.php?if=<?=$ifent;?>"><?=htmlspecialchars($ifname);?></a></li>
196
<?php endif; ?>
197
<?php endforeach; ?>
198
  </ul>
199
  </td></tr>
200
  <tr> 
201
    <td class="tabcont">
202
              <table width="100%" border="0" cellpadding="6" cellspacing="0">
203
                      <tr> 
204
                        <td width="22%" valign="top" class="vtable">&nbsp;</td>
205
                        <td width="78%" class="vtable">
206
<input name="enable" type="checkbox" value="yes" <?php if ($pconfig['enable']) echo "checked"; ?> onClick="enable_change(false)">
207
                          <strong>Enable DHCP server on 
208
                          <?=htmlspecialchars($iflist[$if]);?>
209
                          interface</strong></td>
210
                      </tr>
211
				  <tr>
212
	              <td width="22%" valign="top" class="vtable">&nbsp;</td>
213
                      <td width="78%" class="vtable">
214
<input name="denyunknown" type="checkbox" value="yes" <?php if ($pconfig['denyunknown']) echo "checked"; ?>>
215
                      <strong>Deny unknown clients</strong><br>
216
                      If this is checked, only the clients defined below will get DHCP leases from this server. </td>
217
		      		  </tr>
218
                      <tr> 
219
                        <td width="22%" valign="top" class="vncellreq">Subnet</td>
220
                        <td width="78%" class="vtable"> 
221
                          <?=gen_subnet($ifcfg['ipaddr'], $ifcfg['subnet']);?>
222
                        </td>
223
                      </tr>
224
                      <tr> 
225
                        <td width="22%" valign="top" class="vncellreq">Subnet 
226
                          mask</td>
227
                        <td width="78%" class="vtable"> 
228
                          <?=gen_subnet_mask($ifcfg['subnet']);?>
229
                        </td>
230
                      </tr>
231
                      <tr> 
232
                        <td width="22%" valign="top" class="vncellreq">Available 
233
                          range</td>
234
                        <td width="78%" class="vtable"> 
235
                          <?=long2ip(ip2long($ifcfg['ipaddr']) & gen_subnet_mask_long($ifcfg['subnet']));?>
236
                          - 
237
                          <?=long2ip(ip2long($ifcfg['ipaddr']) | (~gen_subnet_mask_long($ifcfg['subnet']))); ?>
238
                        </td>
239
                      </tr>
240
                      <tr> 
241
                        <td width="22%" valign="top" class="vncellreq">Range</td>
242
                        <td width="78%" class="vtable"> 
243
                          <input name="range_from" type="text" class="formfld" id="range_from" size="20" value="<?=htmlspecialchars($pconfig['range_from']);?>"> 
244
                          &nbsp;to&nbsp; <input name="range_to" type="text" class="formfld" id="range_to" size="20" value="<?=htmlspecialchars($pconfig['range_to']);?>"></td>
245
                      </tr>
246
                      <tr> 
247
                        <td width="22%" valign="top" class="vncell">WINS servers</td>
248
                        <td width="78%" class="vtable"> 
249
                          <input name="wins1" type="text" class="formfld" id="wins1" size="20" value="<?=htmlspecialchars($pconfig['wins1']);?>"><br>
250
                          <input name="wins2" type="text" class="formfld" id="wins2" size="20" value="<?=htmlspecialchars($pconfig['wins2']);?>"></td>
251
                      </tr>
252
                      <tr> 
253
                        <td width="22%" valign="top" class="vncell">Default lease 
254
                          time</td>
255
                        <td width="78%" class="vtable"> 
256
                          <input name="deftime" type="text" class="formfld" id="deftime" size="10" value="<?=htmlspecialchars($pconfig['deftime']);?>">
257
                          seconds<br>
258
                          This is used for clients that do not ask for a specific 
259
                          expiration time.<br>
260
                          The default is 7200 seconds.</td>
261
                      </tr>
262
                      <tr> 
263
                        <td width="22%" valign="top" class="vncell">Maximum lease 
264
                          time</td>
265
                        <td width="78%" class="vtable"> 
266
                          <input name="maxtime" type="text" class="formfld" id="maxtime" size="10" value="<?=htmlspecialchars($pconfig['maxtime']);?>">
267
                          seconds<br>
268
                          This is the maximum lease time for clients that ask 
269
                          for a specific expiration time.<br>
270
                          The default is 86400 seconds.</td>
271
                      </tr>
272
                      <tr> 
273
                        <td width="22%" valign="top">&nbsp;</td>
274
                        <td width="78%"> 
275
                          <input name="if" type="hidden" value="<?=$if;?>"> 
276
                          <input name="Submit" type="submit" class="formbtn" value="Save" onclick="enable_change(true)"> 
277
                        </td>
278
                      </tr>
279
                      <tr> 
280
                        <td width="22%" valign="top">&nbsp;</td>
281
                        <td width="78%"> <p><span class="vexpl"><span class="red"><strong>Note:<br>
282
                            </strong></span>The DNS servers entered in <a href="system.php">System: 
283
                            General setup</a> (or the <a href="services_dnsmasq.php">DNS 
284
                            forwarder</a>, if enabled) </span><span class="vexpl">will 
285
                            be assigned to clients by the DHCP server.<br>
286
                            <br>
287
                            The DHCP lease table can be viewed on the <a href="diag_dhcp_leases.php">Diagnostics: 
288
                            DHCP leases</a> page.<br>
289
                            </span></p></td>
290
                      </tr>
291
                    </table>
292
					&nbsp;<br>
293
              <table width="100%" border="0" cellpadding="0" cellspacing="0">
294
                <tr>
295
                  <td width="35%" class="listhdrr">MAC address </td>
296
                  <td width="20%" class="listhdrr">IP address</td>
297
                  <td width="35%" class="listhdr">Description</td>
298
                  <td width="10%" class="list"></td>
299
				</tr>
300
			  <?php $i = 0; foreach ($a_maps as $mapent): ?>
301
                <tr>
302
                  <td class="listlr">
303
                    <?=htmlspecialchars($mapent['mac']);?>
304
                  </td>
305
                  <td class="listr">
306
                    <?=htmlspecialchars($mapent['ipaddr']);?>&nbsp;
307
                  </td>
308
                  <td class="listbg">
309
                    <?=htmlspecialchars($mapent['descr']);?>&nbsp;
310
                  </td>
311
                  <td valign="middle" nowrap class="list"> <a href="services_dhcp_edit.php?if=<?=$if;?>&id=<?=$i;?>"><img src="e.gif" width="17" height="17" border="0"></a>
312
                     &nbsp;<a href="services_dhcp.php?if=<?=$if;?>&act=del&id=<?=$i;?>" onclick="return confirm('Do you really want to delete this mapping?')"><img src="x.gif" width="17" height="17" border="0"></a></td>
313
				</tr>
314
			  <?php $i++; endforeach; ?>
315
                <tr> 
316
                  <td class="list" colspan="3"></td>
317
                  <td class="list"> <a href="services_dhcp_edit.php?if=<?=$if;?>"><img src="plus.gif" width="17" height="17" border="0"></a></td>
318
				</tr>
319
              </table>
320
    </td>
321
  </tr>
322
</table>
323
</form>
324
<script language="JavaScript">
325
<!--
326
enable_change(false);
327
//-->
328
</script>
329
<?php include("fend.inc"); ?>
330
</body>
331
</html>
(64-64/98)