You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

231 lines
8.2 KiB

  1. <?php
  2. require_once 'includes/status_messages.php';
  3. require_once 'config.php';
  4. /**
  5. * Manage DHCP configuration
  6. */
  7. function DisplayDHCPConfig()
  8. {
  9. $status = new StatusMessages();
  10. if (!RASPI_MONITOR_ENABLED) {
  11. if (isset($_POST['savedhcpdsettings'])) {
  12. SaveDHCPConfig($status);
  13. }
  14. }
  15. exec('pidof dnsmasq | wc -l', $dnsmasq);
  16. $dnsmasq_state = ($dnsmasq[0] > 0);
  17. if (!RASPI_MONITOR_ENABLED) {
  18. if (isset($_POST['startdhcpd'])) {
  19. if ($dnsmasq_state) {
  20. $status->addMessage('dnsmasq already running', 'info');
  21. } else {
  22. exec('sudo /bin/systemctl start dnsmasq.service', $dnsmasq, $return);
  23. if ($return == 0) {
  24. $status->addMessage('Successfully started dnsmasq', 'success');
  25. $dnsmasq_state = true;
  26. } else {
  27. $status->addMessage('Failed to start dnsmasq', 'danger');
  28. }
  29. }
  30. } elseif (isset($_POST['stopdhcpd'])) {
  31. if ($dnsmasq_state) {
  32. exec('sudo /bin/systemctl stop dnsmasq.service', $dnsmasq, $return);
  33. if ($return == 0) {
  34. $status->addMessage('Successfully stopped dnsmasq', 'success');
  35. $dnsmasq_state = false;
  36. } else {
  37. $status->addMessage('Failed to stop dnsmasq', 'danger');
  38. }
  39. } else {
  40. $status->addMessage('dnsmasq already stopped', 'info');
  41. }
  42. }
  43. }
  44. getWifiInterface();
  45. $serviceStatus = $dnsmasq_state ? "up" : "down";
  46. exec("ip -o link show | awk -F': ' '{print $2}'", $interfaces);
  47. exec('cat ' . RASPI_DNSMASQ_LEASES, $leases);
  48. $ap_iface = $_SESSION['ap_interface'];
  49. echo renderTemplate(
  50. "dhcp", compact(
  51. "status",
  52. "serviceStatus",
  53. "dnsmasq_state",
  54. "ap_iface",
  55. "dhcpHost",
  56. "interfaces",
  57. "leases"
  58. )
  59. );
  60. }
  61. function SaveDHCPConfig($status)
  62. {
  63. $iface = $_POST['interface'];
  64. $return = 1;
  65. if (($_POST['dhcp-iface'] == "1")) {
  66. $errors = ValidateDHCPInput();
  67. if (empty($errors)) {
  68. $return = UpdateDnsmasqCfg($iface,$status);
  69. } else {
  70. $status->addMessage($errors, 'danger');
  71. }
  72. if ($return == 1) {
  73. $status->addMessage('Dnsmasq configuration failed to be updated.', 'danger');
  74. return false;
  75. }
  76. $return = UpdateDHCPCfg($iface,$status);
  77. // process disable dhcp option
  78. } elseif (($_POST['dhcp-iface'] == "0") && file_exists(RASPI_DNSMASQ_PREFIX.$iface.'.conf')) {
  79. // remove dhcp conf for selected interface
  80. $return = RemoveDHCPCfg($iface,$status);
  81. }
  82. if ($return == 0) {
  83. $status->addMessage('Dnsmasq configuration updated successfully.', 'success');
  84. } else {
  85. $status->addMessage('Dnsmasq configuration failed to be updated.', 'danger');
  86. return false;
  87. }
  88. return true;
  89. }
  90. function ValidateDHCPInput()
  91. {
  92. define('IFNAMSIZ', 16);
  93. $iface = $_POST['interface'];
  94. if (!preg_match('/^[a-zA-Z0-9]+$/', $iface)
  95. || strlen($iface) >= IFNAMSIZ
  96. ) {
  97. $errors .= _('Invalid interface name.').'<br />'.PHP_EOL;
  98. }
  99. if (!preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\z/', $_POST['RangeStart'])
  100. && !empty($_POST['RangeStart'])
  101. ) { // allow ''/null ?
  102. $errors .= _('Invalid DHCP range start.').'<br />'.PHP_EOL;
  103. }
  104. if (!preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\z/', $_POST['RangeEnd'])
  105. && !empty($_POST['RangeEnd'])
  106. ) { // allow ''/null ?
  107. $errors .= _('Invalid DHCP range end.').'<br />'.PHP_EOL;
  108. }
  109. if (!ctype_digit($_POST['RangeLeaseTime']) && $_POST['RangeLeaseTimeUnits'] !== 'infinite') {
  110. $errors .= _('Invalid DHCP lease time, not a number.').'<br />'.PHP_EOL;
  111. }
  112. if (!in_array($_POST['RangeLeaseTimeUnits'], array('m', 'h', 'd', 'infinite'))) {
  113. $errors .= _('Unknown DHCP lease time unit.').'<br />'.PHP_EOL;
  114. }
  115. return $errors;
  116. }
  117. function UpdateDnsmasqCfg($iface,$status)
  118. {
  119. $config = 'interface='.$iface.PHP_EOL.
  120. 'dhcp-range='.$_POST['RangeStart'].','.$_POST['RangeEnd'].
  121. ',255.255.255.0,';
  122. if ($_POST['RangeLeaseTimeUnits'] !== 'infinite') {
  123. $config .= $_POST['RangeLeaseTime'];
  124. }
  125. $config .= $_POST['RangeLeaseTimeUnits'].PHP_EOL;
  126. for ($i=0; $i < count($_POST["static_leases"]["mac"]); $i++) {
  127. $mac = trim($_POST["static_leases"]["mac"][$i]);
  128. $ip = trim($_POST["static_leases"]["ip"][$i]);
  129. if ($mac != "" && $ip != "") {
  130. $config .= "dhcp-host=$mac,$ip".PHP_EOL;
  131. }
  132. }
  133. if ($_POST['no-resolv'] == "1") {
  134. $config .= "no-resolv".PHP_EOL;
  135. }
  136. foreach ($_POST['server'] as $server) {
  137. $config .= "server=$server".PHP_EOL;
  138. }
  139. if ($_POST['log-dhcp'] == "1") {
  140. $config .= "log-dhcp".PHP_EOL;
  141. }
  142. if ($_POST['log-queries'] == "1") {
  143. $config .= "log-queries".PHP_EOL;
  144. }
  145. if ($_POST['DNS1']) {
  146. $config .= "dhcp-option=6," . $_POST['DNS1'];
  147. if ($_POST['DNS2']) {
  148. $config .= ','.$_POST['DNS2'];
  149. }
  150. $config .= PHP_EOL;
  151. }
  152. // enable these settings on the default interface
  153. if ($iface == "wlan0") {
  154. $config .= "log-facility=/tmp/dnsmasq.log".PHP_EOL;
  155. $config .= "conf-dir=/etc/dnsmasq.d".PHP_EOL;
  156. }
  157. file_put_contents("/tmp/dnsmasqdata", $config);
  158. $msg = file_exists(RASPI_DNSMASQ_PREFIX.$iface.'.conf') ? 'updated' : 'added';
  159. system('sudo cp /tmp/dnsmasqdata '.RASPI_DNSMASQ_PREFIX.$iface.'.conf', $result);
  160. if ($result == 0) {
  161. $status->addMessage('Dnsmasq configuration for '.$iface.' '.$msg.'.', 'success');
  162. }
  163. return $result;
  164. }
  165. function UpdateDHCPCfg($iface,$status)
  166. {
  167. $net_cfg = RASPI_CONFIG_NETWORKING.'/'.$iface.'.ini';
  168. if (!file_exists($net_cfg) || filesize($net_cfg) ==0 ) {
  169. $status->addMessage('Static IP address for '.$iface.' not found.', 'danger');
  170. $status->addMessage('Configure this interface in Networking > '.$iface.'.', 'danger');
  171. $return = 1;
  172. } else {
  173. $dhcp_cfg = file_get_contents(RASPI_DHCPCD_CONFIG);
  174. if (!preg_match('/^interface\s'.$iface.'$/m', $dhcp_cfg)) {
  175. // set dhcp values from ini
  176. $iface_cfg = parse_ini_file($net_cfg, false, INI_SCANNER_RAW);
  177. $ip_address = $iface_cfg['ip_address'];
  178. $domain_name_server = ($iface_cfg['domain_name_server'] =='') ? '1.1.1.1 8.8.8.8' : $iface_cfg['domain_name_server'];
  179. // append interface config to dhcpcd.conf
  180. $cfg = $dhcp_conf;
  181. $cfg[] = '# RaspAP '.$iface.' configuration';
  182. $cfg[] = 'interface '.$iface;
  183. $cfg[] = 'static ip_address='.$ip_address;
  184. $cfg[] = 'static domain_name_server='.$domain_name_server;
  185. $cfg[] = PHP_EOL;
  186. $cfg = join(PHP_EOL, $cfg);
  187. $dhcp_cfg .= $cfg;
  188. file_put_contents("/tmp/dhcpddata", $dhcp_cfg);
  189. system('sudo cp /tmp/dhcpddata '.RASPI_DHCPCD_CONFIG, $result);
  190. $status->addMessage('DHCP configuration for '.$iface.' added.', 'success');
  191. } else {
  192. $status->addMessage('DHCP for '.$iface.' already enabled.', 'success');
  193. }
  194. }
  195. return $result;
  196. }
  197. function RemoveDHCPCfg($iface,$status)
  198. {
  199. $dhcp_cfg = file_get_contents(RASPI_DHCPCD_CONFIG);
  200. $dhcp_cfg = preg_replace('/^#\sRaspAP\s'.$iface.'.*\n(.*\n){3}/m', '', $dhcp_cfg);
  201. file_put_contents("/tmp/dhcpddata", rtrim($dhcp_cfg).PHP_EOL);
  202. system('sudo cp /tmp/dhcpddata '.RASPI_DHCPCD_CONFIG, $result);
  203. if ($result == 0) {
  204. $status->addMessage('DHCP configuration for '.$iface.' removed.', 'success');
  205. } else {
  206. $status->addMessage('Failed to remove DHCP configuration for '.$iface.'.', 'danger');
  207. return $result;
  208. }
  209. // remove dnsmasq eth0 conf
  210. system('sudo rm '.RASPI_DNSMASQ_PREFIX.$iface.'.conf', $result);
  211. if ($result == 0) {
  212. $status->addMessage('Dnsmasq configuration for '.$iface.' removed.', 'success');
  213. } else {
  214. $status->addMessage('Failed to remove dnsmasq configuration for '.$iface.'.', 'danger');
  215. }
  216. return $result;
  217. }