Project

General

Profile

Download (15.2 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_once("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
$pconfig['gateway'] = $config['dhcpd'][$if]['gateway'];
56
list($pconfig['wins1'],$pconfig['wins2']) = $config['dhcpd'][$if]['winsserver'];
57
$pconfig['enable'] = isset($config['dhcpd'][$if]['enable']);
58
$pconfig['denyunknown'] = isset($config['dhcpd'][$if]['denyunknown']);
59

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

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

    
68
if ($_POST) {
69

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

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

    
117
	if (!$input_errors) {
118
		$config['dhcpd'][$if]['range']['from'] = $_POST['range_from'];
119
		$config['dhcpd'][$if]['range']['to'] = $_POST['range_to'];
120
		$config['dhcpd'][$if]['defaultleasetime'] = $_POST['deftime'];
121
		$config['dhcpd'][$if]['maxleasetime'] = $_POST['maxtime'];
122
		$config['dhcpd'][$if]['enable'] = $_POST['enable'] ? true : false;
123
		$config['dhcpd'][$if]['denyunknown'] = $_POST['denyunknown'] ? true : false;
124
		
125
		unset($config['dhcpd'][$if]['winsserver']);
126
		if ($_POST['wins1'])
127
			$config['dhcpd'][$if]['winsserver'][] = $_POST['wins1'];
128
		if ($_POST['wins2'])
129
			$config['dhcpd'][$if]['winsserver'][] = $_POST['wins2'];
130
			
131
		$config['dhcpd'][$if]['gateway'] = $_POST['gateway'];
132

    
133
		write_config();
134
		
135
		$retval = 0;
136
		if (!file_exists($d_sysrebootreqd_path)) {
137
			config_lock();
138
			$retval = services_dhcpd_configure();
139
			config_unlock();
140
		}
141
		$savemsg = get_std_save_message($retval);
142
		
143
		if ($retval == 0) {
144
			if (file_exists($d_staticmapsdirty_path))
145
				unlink($d_staticmapsdirty_path);
146
		}
147
	}
148
}
149

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

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