PageRenderTime 23ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/usr/local/www/services_dhcp_edit.php

https://github.com/vongrippen/pfsense
PHP | 262 lines | 187 code | 32 blank | 43 comment | 41 complexity | 97091c8456af417ee8aa3c31161eb6db MD5 | raw file
  1. <?php
  2. /* $Id$ */
  3. /*
  4. services_dhcp_edit.php
  5. part of m0n0wall (http://m0n0.ch/wall)
  6. Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
  7. All rights reserved.
  8. Redistribution and use in source and binary forms, with or without
  9. modification, are permitted provided that the following conditions are met:
  10. 1. Redistributions of source code must retain the above copyright notice,
  11. this list of conditions and the following disclaimer.
  12. 2. Redistributions in binary form must reproduce the above copyright
  13. notice, this list of conditions and the following disclaimer in the
  14. documentation and/or other materials provided with the distribution.
  15. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  16. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17. AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  18. AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  19. OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  20. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  21. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  22. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  23. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. /*
  27. pfSense_BUILDER_BINARIES: /usr/sbin/arp
  28. pfSense_MODULE: dhcpserver
  29. */
  30. ##|+PRIV
  31. ##|*IDENT=page-services-dhcpserver-editstaticmapping
  32. ##|*NAME=Services: DHCP Server : Edit static mapping page
  33. ##|*DESCR=Allow access to the 'Services: DHCP Server : Edit static mapping' page.
  34. ##|*MATCH=services_dhcp_edit.php*
  35. ##|-PRIV
  36. function staticmapcmp($a, $b) {
  37. return ipcmp($a['ipaddr'], $b['ipaddr']);
  38. }
  39. function staticmaps_sort($ifgui) {
  40. global $g, $config;
  41. usort($config['dhcpd'][$ifgui]['staticmap'], "staticmapcmp");
  42. }
  43. require_once('globals.inc');
  44. if(!$g['services_dhcp_server_enable']) {
  45. Header("Location: /");
  46. exit;
  47. }
  48. require("guiconfig.inc");
  49. $if = $_GET['if'];
  50. if ($_POST['if'])
  51. $if = $_POST['if'];
  52. if (!$if) {
  53. header("Location: services_dhcp.php");
  54. exit;
  55. }
  56. if (!is_array($config['dhcpd'][$if]['staticmap'])) {
  57. $config['dhcpd'][$if]['staticmap'] = array();
  58. }
  59. $static_arp_enabled=isset($config['dhcpd'][$if]['staticarp']);
  60. $netboot_enabled=isset($config['dhcpd'][$if]['netboot']);
  61. $a_maps = &$config['dhcpd'][$if]['staticmap'];
  62. $ifcfgip = get_interface_ip($if);
  63. $ifcfgsn = get_interface_subnet($if);
  64. $ifcfgdescr = convert_friendly_interface_to_friendly_descr($if);
  65. $id = $_GET['id'];
  66. if (isset($_POST['id']))
  67. $id = $_POST['id'];
  68. if (isset($id) && $a_maps[$id]) {
  69. $pconfig['mac'] = $a_maps[$id]['mac'];
  70. $pconfig['hostname'] = $a_maps[$id]['hostname'];
  71. $pconfig['ipaddr'] = $a_maps[$id]['ipaddr'];
  72. $pconfig['netbootfile'] = $a_maps[$id]['netbootfile'];
  73. $pconfig['descr'] = $a_maps[$id]['descr'];
  74. } else {
  75. $pconfig['mac'] = $_GET['mac'];
  76. $pconfig['hostname'] = $_GET['hostname'];
  77. $pconfig['netbootfile'] = $_GET['netbootfile'];
  78. $pconfig['descr'] = $_GET['descr'];
  79. }
  80. if ($_POST) {
  81. unset($input_errors);
  82. $pconfig = $_POST;
  83. /* input validation */
  84. $reqdfields = explode(" ", "mac");
  85. $reqdfieldsn = array(gettext("MAC address"));
  86. do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors);
  87. /* normalize MAC addresses - lowercase and convert Windows-ized hyphenated MACs to colon delimited */
  88. $_POST['mac'] = strtolower(str_replace("-", ":", $_POST['mac']));
  89. if ($_POST['hostname']) {
  90. preg_match("/\-\$/", $_POST['hostname'], $matches);
  91. if($matches)
  92. $input_errors[] = gettext("The hostname cannot end with a hyphen according to RFC952");
  93. if (!is_hostname($_POST['hostname'])) {
  94. $input_errors[] = gettext("The hostname can only contain the characters A-Z, 0-9 and '-'.");
  95. } else {
  96. if (strpos($_POST['hostname'],'.')) {
  97. $input_errors[] = gettext("A valid hostname is specified, but the domain name part should be omitted");
  98. }
  99. }
  100. }
  101. if (($_POST['ipaddr'] && !is_ipaddr($_POST['ipaddr']))) {
  102. $input_errors[] = gettext("A valid IP address must be specified.");
  103. }
  104. if (($_POST['mac'] && !is_macaddr($_POST['mac']))) {
  105. $input_errors[] = gettext("A valid MAC address must be specified.");
  106. }
  107. if($static_arp_enabled && !$_POST['ipaddr']) {
  108. $input_errors[] = gettext("Static ARP is enabled. You must specify an IP address.");
  109. }
  110. /* check for overlaps */
  111. foreach ($a_maps as $mapent) {
  112. if (isset($id) && ($a_maps[$id]) && ($a_maps[$id] === $mapent))
  113. continue;
  114. if ((($mapent['hostname'] == $_POST['hostname']) && $mapent['hostname']) || ($mapent['mac'] == $_POST['mac'])) {
  115. $input_errors[] = gettext("This Hostname, IP or MAC address already exists.");
  116. break;
  117. }
  118. }
  119. /* make sure it's not within the dynamic subnet */
  120. if ($_POST['ipaddr']) {
  121. $dynsubnet_start = ip2ulong($config['dhcpd'][$if]['range']['from']);
  122. $dynsubnet_end = ip2ulong($config['dhcpd'][$if]['range']['to']);
  123. if ((ip2ulong($_POST['ipaddr']) >= $dynsubnet_start) &&
  124. (ip2ulong($_POST['ipaddr']) <= $dynsubnet_end)) {
  125. $input_errors[] = sprintf(gettext("The IP address must not be within the DHCP range for this interface."));
  126. }
  127. $lansubnet_start = ip2ulong(long2ip32(ip2long($ifcfgip) & gen_subnet_mask_long($ifcfgsn)));
  128. $lansubnet_end = ip2ulong(long2ip32(ip2long($ifcfgip) | (~gen_subnet_mask_long($ifcfgsn))));
  129. if ((ip2ulong($_POST['ipaddr']) < $lansubnet_start) ||
  130. (ip2ulong($_POST['ipaddr']) > $lansubnet_end)) {
  131. $input_errors[] = sprintf(gettext("The IP address must lie in the %s subnet."),$ifcfgdescr);
  132. }
  133. }
  134. if (!$input_errors) {
  135. $mapent = array();
  136. $mapent['mac'] = $_POST['mac'];
  137. $mapent['ipaddr'] = $_POST['ipaddr'];
  138. $mapent['hostname'] = $_POST['hostname'];
  139. $mapent['descr'] = $_POST['descr'];
  140. $mapent['netbootfile'] = $_POST['netbootfile'];
  141. if (isset($id) && $a_maps[$id])
  142. $a_maps[$id] = $mapent;
  143. else
  144. $a_maps[] = $mapent;
  145. staticmaps_sort($if);
  146. write_config();
  147. if(isset($config['dhcpd'][$if]['enable'])) {
  148. mark_subsystem_dirty('staticmaps');
  149. if (isset($config['dnsmasq']['regdhcpstatic']))
  150. mark_subsystem_dirty('hosts');
  151. }
  152. header("Location: services_dhcp.php?if={$if}");
  153. exit;
  154. }
  155. }
  156. $pgtitle = array(gettext("Services"),gettext("DHCP"),gettext("Edit static mapping"));
  157. $statusurl = "status_dhcp_leases.php";
  158. $logurl = "diag_logs_dhcp.php";
  159. include("head.inc");
  160. ?>
  161. <body link="#0000CC" vlink="#0000CC" alink="#0000CC">
  162. <?php include("fbegin.inc"); ?>
  163. <?php if ($input_errors) print_input_errors($input_errors); ?>
  164. <form action="services_dhcp_edit.php" method="post" name="iform" id="iform">
  165. <table width="100%" border="0" cellpadding="6" cellspacing="0">
  166. <tr>
  167. <td colspan="2" valign="top" class="listtopic"><?=gettext("Static DHCP Mapping");?></td>
  168. </tr>
  169. <tr>
  170. <td width="22%" valign="top" class="vncellreq"><?=gettext("MAC address");?></td>
  171. <td width="78%" class="vtable">
  172. <input name="mac" type="text" class="formfld unknown" id="mac" size="30" value="<?=htmlspecialchars($pconfig['mac']);?>">
  173. <?php
  174. $ip = getenv('REMOTE_ADDR');
  175. $mac = `/usr/sbin/arp -an | grep {$ip} | cut -d" " -f4`;
  176. $mac = str_replace("\n","",$mac);
  177. ?>
  178. <a OnClick="document.forms[0].mac.value='<?=$mac?>';" href="#"><?=gettext("Copy my MAC address");?></a>
  179. <br>
  180. <span class="vexpl"><?=gettext("Enter a MAC address in the following format: ".
  181. "xx:xx:xx:xx:xx:xx");?></span></td>
  182. </tr>
  183. <tr>
  184. <td width="22%" valign="top" class="vncell"><?=gettext("IP address");?></td>
  185. <td width="78%" class="vtable">
  186. <input name="ipaddr" type="text" class="formfld unknown" id="ipaddr" size="20" value="<?=htmlspecialchars($pconfig['ipaddr']);?>">
  187. <br>
  188. <?=gettext("If no IP address is given, one will be dynamically allocated from the pool.");?></td>
  189. </tr>
  190. <tr>
  191. <td width="22%" valign="top" class="vncell"><?=gettext("Hostname");?></td>
  192. <td width="78%" class="vtable">
  193. <input name="hostname" type="text" class="formfld unknown" id="hostname" size="20" value="<?=htmlspecialchars($pconfig['hostname']);?>">
  194. <br> <span class="vexpl"><?=gettext("Name of the host, without domain part.");?></span></td>
  195. </tr>
  196. <?php if($netboot_enabled) { ?>
  197. <tr>
  198. <td width="22%" valign="top" class="vncell">Netboot filename</td>
  199. <td width="78%" class="vtable">
  200. <input name="netbootfile" type="text" class="formfld unknown" id="netbootfile" size="20" value="<?=htmlspecialchars($pconfig['netbootfile']);?>">
  201. <br> <span class="vexpl">Name of the file that should be loaded when this host boots off of the network, overrides setting on main page.</span></td>
  202. </tr>
  203. <?php } ?>
  204. <tr>
  205. <td width="22%" valign="top" class="vncell"><?=gettext("Description");?></td>
  206. <td width="78%" class="vtable">
  207. <input name="descr" type="text" class="formfld unknown" id="descr" size="40" value="<?=htmlspecialchars($pconfig['descr']);?>">
  208. <br> <span class="vexpl"><?=gettext("You may enter a description here ".
  209. "for your reference (not parsed).");?></span></td>
  210. </tr>
  211. <tr>
  212. <td width="22%" valign="top">&nbsp;</td>
  213. <td width="78%">
  214. <input name="Submit" type="submit" class="formbtn" value="<?=gettext("Save");?>"> <input class="formbtn" type="button" value="<?=gettext("Cancel");?>" onclick="history.back()">
  215. <?php if (isset($id) && $a_maps[$id]): ?>
  216. <input name="id" type="hidden" value="<?=htmlspecialchars($id);?>">
  217. <?php endif; ?>
  218. <input name="if" type="hidden" value="<?=htmlspecialchars($if);?>">
  219. </td>
  220. </tr>
  221. </table>
  222. </form>
  223. <?php include("fend.inc"); ?>
  224. </body>
  225. </html>