Project

General

Profile

Download (16.7 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
		config_lock();
149
		$retval = services_dhcpd_configure();
150
		config_unlock();
151
		$savemsg = get_std_save_message($retval);
152
	}
153
}
154

    
155
if ($_GET['act'] == "del") {
156
	if ($a_maps[$_GET['id']]) {
157
		unset($a_maps[$_GET['id']]);
158
		write_config();
159
		header("Location: services_dhcp.php?if={$if}");
160
		exit;
161
	}
162
}
163

    
164
$pgtitle = "Services: DHCP server";
165
include("head.inc");
166

    
167
?>
168

    
169
<script language="JavaScript">
170
<!--
171
function enable_change(enable_over) {
172
	var endis;
173
	endis = !(document.iform.enable.checked || enable_over);
174
	
175
	document.iform.range_from.disabled = endis;
176
	document.iform.range_to.disabled = endis;
177
	document.iform.wins1.disabled = endis;
178
	document.iform.wins2.disabled = endis;
179
	document.iform.deftime.disabled = endis;
180
	document.iform.maxtime.disabled = endis;
181
	document.iform.gateway.disabled = endis;
182
}
183
//-->
184
</script>
185

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

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