Project

General

Profile

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

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

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

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

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

    
62

    
63
$ifcfg = $config['interfaces'][$if];
64

    
65
if (!is_array($config['dhcpd'][$if]['staticmap'])) {
66
	$config['dhcpd'][$if]['staticmap'] = array();
67
}
68
staticmaps_sort($if);
69
$a_maps = &$config['dhcpd'][$if]['staticmap'];
70

    
71

    
72
if ($_POST) {
73

    
74
	unset($input_errors);
75

    
76
	$pconfig = $_POST;
77

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

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

    
138
		$config['dhcpd'][$if]['staticarp'] = $_POST['staticarp'] ? true : false;
139

    
140
	
141
		write_config();
142

    
143
		/* static arp configuration */
144
                if (isset($config['dhcpd'][$if]['staticarp']))
145
			interfaces_staticarp_configure($if);
146
		
147
		$retval = 0;
148
		if (!file_exists($d_sysrebootreqd_path)) {
149
			config_lock();
150
			$retval = services_dhcpd_configure();
151
			config_unlock();
152
		}
153
		$savemsg = get_std_save_message($retval);
154
		
155
		if ($retval == 0) {
156
			if (file_exists($d_staticmapsdirty_path))
157
				unlink($d_staticmapsdirty_path);
158
		}
159
	}
160
}
161

    
162
if ($_GET['act'] == "del") {
163
	if ($a_maps[$_GET['id']]) {
164
		unset($a_maps[$_GET['id']]);
165
		write_config();
166
		touch($d_staticmapsdirty_path);
167
		header("Location: services_dhcp.php?if={$if}");
168
		exit;
169
	}
170
}
171

    
172
$pgtitle = "Services: DHCP server";
173
include("head.inc");
174

    
175
?>
176

    
177
<script language="JavaScript">
178
<!--
179
function enable_change(enable_over) {
180
	var endis;
181
	endis = !(document.iform.enable.checked || enable_over);
182
	
183
	document.iform.range_from.disabled = endis;
184
	document.iform.range_to.disabled = endis;
185
	document.iform.wins1.disabled = endis;
186
	document.iform.wins2.disabled = endis;
187
	document.iform.deftime.disabled = endis;
188
	document.iform.maxtime.disabled = endis;
189
	document.iform.gateway.disabled = endis;
190
}
191
//-->
192
</script>
193

    
194
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
195
<?php include("fbegin.inc"); ?>
196
<p class="pgtitle"><?=$pgtitle?></p>
197
<form action="services_dhcp.php" method="post" name="iform" id="iform">
198
<?php if ($input_errors) print_input_errors($input_errors); ?>
199
<?php if ($savemsg) print_info_box($savemsg); ?>
200
<?php if (file_exists($d_staticmapsdirty_path)): ?><p>
201
<?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>
202
<?php endif; ?>
203
<table width="100%" border="0" cellpadding="0" cellspacing="0">
204
  <tr><td>
205
  <?php
206
	/* active tabs */
207
	$tab_array = array();
208
	$tabscounter = 0;
209
	$i = 0;
210
	foreach ($iflist as $ifent => $ifname) {
211
		if ($ifent == $if)
212
			$active = true;
213
		else
214
			$active = false;
215
		$tab_array[] = array($ifname, $active, "services_dhcp.php?if={$ifent}");
216
	}
217
	display_top_tabs($tab_array);
218
  ?>
219
  </td></tr>
220
  <tr> 
221
    <td>
222
	<div id="mainarea">
223
              <table class="tabcont" width="100%" border="0" cellpadding="6" cellspacing="0">
224
                      <tr> 
225
                        <td width="22%" valign="top" class="vtable">&nbsp;</td>
226
                        <td width="78%" class="vtable">
227
<input name="enable" type="checkbox" value="yes" <?php if ($pconfig['enable']) echo "checked"; ?> onClick="enable_change(false)">
228
                          <strong>Enable DHCP server on 
229
                          <?=htmlspecialchars($iflist[$if]);?>
230
                          interface</strong></td>
231
                      </tr>
232
				  <tr>
233
	              <td width="22%" valign="top" class="vtable">&nbsp;</td>
234
                      <td width="78%" class="vtable">
235
<input name="denyunknown" type="checkbox" value="yes" <?php if ($pconfig['denyunknown']) echo "checked"; ?>>
236
                      <strong>Deny unknown clients</strong><br>
237
                      If this is checked, only the clients defined below will get DHCP leases from this server. </td>
238
		      		  </tr>
239
                      <tr> 
240
                        <td width="22%" valign="top" class="vncellreq">Subnet</td>
241
                        <td width="78%" class="vtable"> 
242
                          <?=gen_subnet($ifcfg['ipaddr'], $ifcfg['subnet']);?>
243
                        </td>
244
                      </tr>
245
                      <tr> 
246
                        <td width="22%" valign="top" class="vncellreq">Subnet 
247
                          mask</td>
248
                        <td width="78%" class="vtable"> 
249
                          <?=gen_subnet_mask($ifcfg['subnet']);?>
250
                        </td>
251
                      </tr>
252
                      <tr> 
253
                        <td width="22%" valign="top" class="vncellreq">Available 
254
                          range</td>
255
                        <td width="78%" class="vtable"> 
256
                          <?=long2ip(ip2long($ifcfg['ipaddr']) & gen_subnet_mask_long($ifcfg['subnet']));?>
257
                          - 
258
                          <?=long2ip(ip2long($ifcfg['ipaddr']) | (~gen_subnet_mask_long($ifcfg['subnet']))); ?>
259
                        </td>
260
                      </tr>
261
                      <tr> 
262
                        <td width="22%" valign="top" class="vncellreq">Range</td>
263
                        <td width="78%" class="vtable"> 
264
                          <input name="range_from" type="text" class="formfld" id="range_from" size="20" value="<?=htmlspecialchars($pconfig['range_from']);?>"> 
265
                          &nbsp;to&nbsp; <input name="range_to" type="text" class="formfld" id="range_to" size="20" value="<?=htmlspecialchars($pconfig['range_to']);?>"></td>
266
                      </tr>
267
                      <tr> 
268
                        <td width="22%" valign="top" class="vncell">WINS servers</td>
269
                        <td width="78%" class="vtable"> 
270
                          <input name="wins1" type="text" class="formfld" id="wins1" size="20" value="<?=htmlspecialchars($pconfig['wins1']);?>"><br>
271
                          <input name="wins2" type="text" class="formfld" id="wins2" size="20" value="<?=htmlspecialchars($pconfig['wins2']);?>"></td>
272
                      </tr>
273
                     <tr> 
274
                       <td width="22%" valign="top" class="vncell">Gateway</td>
275
                       <td width="78%" class="vtable"> 
276
                         <input name="gateway" type="text" class="formfld" id="gateway" size="20" value="<?=htmlspecialchars($pconfig['gateway']);?>"><br>
277
The default is to use the IP of the firewall as the gateway.  Specify an alternate gateway here if this is not the correct gateway for your network.
278
                      </tr>
279
                      <tr> 
280
                        <td width="22%" valign="top" class="vncell">Default lease 
281
                          time</td>
282
                        <td width="78%" class="vtable"> 
283
                          <input name="deftime" type="text" class="formfld" id="deftime" size="10" value="<?=htmlspecialchars($pconfig['deftime']);?>">
284
                          seconds<br>
285
                          This is used for clients that do not ask for a specific 
286
                          expiration time.<br>
287
                          The default is 7200 seconds.</td>
288
                      </tr>
289
                      <tr> 
290
                        <td width="22%" valign="top" class="vncell">Maximum lease 
291
                          time</td>
292
                        <td width="78%" class="vtable"> 
293
                          <input name="maxtime" type="text" class="formfld" id="maxtime" size="10" value="<?=htmlspecialchars($pconfig['maxtime']);?>">
294
                          seconds<br>
295
                          This is the maximum lease time for clients that ask 
296
                          for a specific expiration time.<br>
297
                          The default is 86400 seconds.</td>
298
                      </tr>
299
                      <tr>
300
                        <td width="22%" valign="top" class="vncell">Static ARP</td>
301
                        <td width="78%" class="vtable">
302
				<table>
303
					<tr><td><input valign="middle" type="checkbox" value="yes" name="staticarp" id="staticarp" <?php if($pconfig['staticarp']) echo " checked"; ?>>&nbsp;
304
					</td><td><b>Enable Static ARP entries</b></td></tr>
305
					<tr><td>&nbsp;</td><td><span class="red"><strong>Note:</strong></span> This feature is under development.  Only the machines listed below will be able to communicate with the firewall on this NIC.  Disabling this has been tested to be broken, a reboot will be required to disable.  Be warned!
306
				</table>
307
			</td>
308
                      </tr>
309
                      <tr> 
310
                        <td width="22%" valign="top">&nbsp;</td>
311
                        <td width="78%"> 
312
                          <input name="if" type="hidden" value="<?=$if;?>"> 
313
                          <input name="Submit" type="submit" class="formbtn" value="Save" onclick="enable_change(true)"> 
314
                        </td>
315
                      </tr>
316
                      <tr> 
317
                        <td width="22%" valign="top">&nbsp;</td>
318
                        <td width="78%"> <p><span class="vexpl"><span class="red"><strong>Note:<br>
319
                            </strong></span>The DNS servers entered in <a href="system.php">System: 
320
                            General setup</a> (or the <a href="services_dnsmasq.php">DNS 
321
                            forwarder</a>, if enabled) </span><span class="vexpl">will 
322
                            be assigned to clients by the DHCP server.<br>
323
                            <br>
324
                            The DHCP lease table can be viewed on the <a href="diag_dhcp_leases.php">Diagnostics: 
325
                            DHCP leases</a> page.<br>
326
                            </span></p></td>
327
                      </tr>
328
                    </table>
329

    
330
              <table class="tabcont" width="100%" border="0" cellpadding="0" cellspacing="0">
331
                <tr>
332
                  <td width="35%" class="listhdrr">MAC address </td>
333
                  <td width="20%" class="listhdrr">IP address</td>
334
                  <td width="35%" class="listhdr">Description</td>
335
                  <td width="10%" class="list"></td>
336
				</tr>
337
			  <?php $i = 0; foreach ($a_maps as $mapent): ?>
338
                <tr>
339
                  <td class="listlr" ondblclick="document.location='services_dhcp_edit.php?if=<?=$if;?>&id=<?=$i;?>';">
340
                    <?=htmlspecialchars($mapent['mac']);?>
341
                  </td>
342
                  <td class="listr" ondblclick="document.location='services_dhcp_edit.php?if=<?=$if;?>&id=<?=$i;?>';">
343
                    <?=htmlspecialchars($mapent['ipaddr']);?>&nbsp;
344
                  </td>
345
                  <td class="listbg" ondblclick="document.location='services_dhcp_edit.php?if=<?=$if;?>&id=<?=$i;?>';">
346
                    <font color="#FFFFFFF"><?=htmlspecialchars($mapent['descr']);?>&nbsp;</font>
347
                  </td>
348
                  <td valign="middle" nowrap class="list">
349
                    <table border="0" cellspacing="0" cellpadding="1">
350
                      <tr>
351
                        <td valign="middle"><a href="services_dhcp_edit.php?if=<?=$if;?>&id=<?=$i;?>"><img src="e.gif" width="17" height="17" border="0"></a></td>
352
                        <td valign="middle"><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>
353
                      </tr>
354
                    </table>
355
                  </td>
356
                </tr>
357
		<?php $i++; endforeach; ?>
358
                <tr> 
359
                  <td class="list" colspan="3"></td>
360
                  <td class="list">
361
                    <table border="0" cellspacing="0" cellpadding="1">
362
                      <tr>
363
                        <td valign="middle"><a href="services_dhcp_edit.php?if=<?=$if;?>"><img src="plus.gif" width="17" height="17" border="0"></a></td>
364
                      </tr>
365
                    </table>
366
                  </td>
367
                </tr>
368
              </table>
369
	</div>
370
    </td>
371
  </tr>
372
</table>
373
</form>
374
<script language="JavaScript">
375
<!--
376
enable_change(false);
377
//-->
378
</script>
379
<?php include("fend.inc"); ?>
380
</body>
381
</html>
(82-82/128)