PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/www/system.php

https://bitbucket.org/cougar/nas4free.hg
PHP | 360 lines | 274 code | 32 blank | 54 comment | 59 complexity | 5c8e2cc7e293983a0c1315f8ecb1f029 MD5 | raw file
Possible License(s): Unlicense, MPL-2.0, GPL-2.0, GPL-3.0, LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /*
  3. system.php
  4. Part of NAS4Free (http://www.nas4free.org).
  5. Copyright (C) 2012 by NAS4Free Team <info@nas4free.org>.
  6. All rights reserved.
  7. Portions of freenas (http://www.freenas.org).
  8. Copyright (C) 2005-2011 by Olivier Cochard <olivier@freenas.org>.
  9. All rights reserved.
  10. Portions of m0n0wall (http://m0n0.ch/wall).
  11. Copyright (C) 2003-2006 Manuel Kasper <mk@neon1.net>.
  12. All rights reserved.
  13. Redistribution and use in source and binary forms, with or without
  14. modification, are permitted provided that the following conditions are met:
  15. 1. Redistributions of source code must retain the above copyright notice, this
  16. list of conditions and the following disclaimer.
  17. 2. Redistributions in binary form must reproduce the above copyright notice,
  18. this list of conditions and the following disclaimer in the documentation
  19. and/or other materials provided with the distribution.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  21. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  22. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  24. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  25. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  27. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. The views and conclusions contained in the software and documentation are those
  31. of the authors and should not be interpreted as representing official policies,
  32. either expressed or implied, of the NAS4Free Project.
  33. */
  34. require("auth.inc");
  35. require("guiconfig.inc");
  36. require("services.inc");
  37. $pgtitle = array(gettext("System"), gettext("General Setup"));
  38. $pconfig['hostname'] = $config['system']['hostname'];
  39. $pconfig['domain'] = $config['system']['domain'];
  40. list($pconfig['dns1'],$pconfig['dns2']) = get_ipv4dnsserver();
  41. list($pconfig['ipv6dns1'],$pconfig['ipv6dns2']) = get_ipv6dnsserver();
  42. $pconfig['username'] = $config['system']['username'];
  43. $pconfig['webguiproto'] = $config['system']['webgui']['protocol'];
  44. $pconfig['webguiport'] = !empty($config['system']['webgui']['port']) ? $config['system']['webgui']['port'] : "";
  45. $pconfig['language'] = $config['system']['language'];
  46. $pconfig['timezone'] = $config['system']['timezone'];
  47. $pconfig['ntp_enable'] = isset($config['system']['ntp']['enable']);
  48. $pconfig['ntp_timeservers'] = $config['system']['ntp']['timeservers'];
  49. $pconfig['ntp_updateinterval'] = $config['system']['ntp']['updateinterval'];
  50. $pconfig['language'] = $config['system']['language'];
  51. $pconfig['certificate'] = base64_decode($config['system']['webgui']['certificate']);
  52. $pconfig['privatekey'] = base64_decode($config['system']['webgui']['privatekey']);
  53. // Set default values if necessary.
  54. if (!$pconfig['language'])
  55. $pconfig['language'] = "English";
  56. if (!$pconfig['timezone'])
  57. $pconfig['timezone'] = "Etc/UTC";
  58. if (!$pconfig['webguiproto'])
  59. $pconfig['webguiproto'] = "http";
  60. if (!$pconfig['username'])
  61. $pconfig['username'] = "admin";
  62. if (!$pconfig['ntp_timeservers'])
  63. $pconfig['ntp_timeservers'] = "pool.ntp.org";
  64. if (!isset($pconfig['ntp_updateinterval']))
  65. $pconfig['ntp_updateinterval'] = 300;
  66. if ($_POST) {
  67. unset($input_errors);
  68. $pconfig = $_POST;
  69. // Input validation.
  70. $reqdfields = explode(" ", "hostname username");
  71. $reqdfieldsn = array(gettext("Hostname"), gettext("Username"));
  72. $reqdfieldst = explode(" ", "hostname alias");
  73. if (!empty($_POST['domain'])) {
  74. $reqdfields = array_merge($reqdfields, array("domain"));
  75. $reqdfieldsn = array_merge($reqdfieldsn, array(gettext("Domain")));
  76. $reqdfieldst = array_merge($reqdfieldst, array("domain"));
  77. }
  78. if (isset($_POST['ntp_enable'])) {
  79. $reqdfields = array_merge($reqdfields, explode(" ", "ntp_timeservers ntp_updateinterval"));
  80. $reqdfieldsn = array_merge($reqdfieldsn, array(gettext("NTP time server"), gettext("Time update interval")));
  81. $reqdfieldst = array_merge($reqdfieldst, explode(" ", "string numeric"));
  82. }
  83. if ("https" === $_POST['webguiproto']) {
  84. $reqdfields = array_merge($reqdfields, explode(" ", "certificate privatekey"));
  85. $reqdfieldsn = array_merge($reqdfieldsn, array(gettext("Certificate"), gettext("Private key")));
  86. $reqdfieldst = array_merge($reqdfieldst, explode(" ", "certificate privatekey"));
  87. }
  88. if (!empty($_POST['webguiport'])) {
  89. $reqdfields = array_merge($reqdfields, array("webguiport"));
  90. $reqdfieldsn = array_merge($reqdfieldsn, array(gettext("Port")));
  91. $reqdfieldst = array_merge($reqdfieldst, array("port"));
  92. }
  93. do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
  94. do_input_validation_type($_POST, $reqdfields, $reqdfieldsn, $reqdfieldst, $input_errors);
  95. if (($_POST['dns1'] && !is_ipv4addr($_POST['dns1'])) || ($_POST['dns2'] && !is_ipv4addr($_POST['dns2']))) {
  96. $input_errors[] = gettext("A valid IPv4 address must be specified for the primary/secondary DNS server.");
  97. }
  98. if (($_POST['ipv6dns1'] && !is_ipv6addr($_POST['ipv6dns1'])) || ($_POST['ipv6dns2'] && !is_ipv6addr($_POST['ipv6dns2']))) {
  99. $input_errors[] = gettext("A valid IPv6 address must be specified for the primary/secondary DNS server.");
  100. }
  101. if (isset($_POST['ntp_enable'])) {
  102. $t = (int)$_POST['ntp_updateinterval'];
  103. if (($t < 0) || (($t > 0) && ($t < 6)) || ($t > 1440)) {
  104. $input_errors[] = gettext("The time update interval must be either between 6 and 1440.");
  105. }
  106. foreach (explode(' ', $_POST['ntp_timeservers']) as $ts) {
  107. if (!is_domain($ts)) {
  108. $input_errors[] = gettext("A NTP time server name may only contain the characters a-z, 0-9, '-' and '.'.");
  109. }
  110. }
  111. }
  112. // Check if port is already used.
  113. if (services_is_port_used(!empty($_POST['webguiport']) ? $_POST['webguiport'] : 80, "sysgui")) {
  114. $input_errors[] = sprintf(gettext("Port %ld is already used by another service."), (!empty($_POST['webguiport']) ? $_POST['webguiport'] : 80));
  115. }
  116. // Check Webserver document root if auth is required
  117. if (isset($config['websrv']['enable'])
  118. && isset($config['websrv']['authentication']['enable'])
  119. && !is_dir($config['websrv']['documentroot'])) {
  120. $input_errors[] = gettext("Webserver document root is missing.");
  121. }
  122. if (empty($input_errors)) {
  123. // Store old values for later processing.
  124. $oldcert = $config['system']['webgui']['certificate'];
  125. $oldkey = $config['system']['webgui']['privatekey'];
  126. $oldwebguiproto = $config['system']['webgui']['protocol'];
  127. $oldwebguiport = $config['system']['webgui']['port'];
  128. $oldlanguage = $config['system']['language'];
  129. $config['system']['hostname'] = strtolower($_POST['hostname']);
  130. $config['system']['domain'] = strtolower($_POST['domain']);
  131. $config['system']['username'] = $_POST['username'];
  132. $config['system']['webgui']['protocol'] = $_POST['webguiproto'];
  133. $config['system']['webgui']['port'] = $_POST['webguiport'];
  134. $config['system']['language'] = $_POST['language'];
  135. $config['system']['timezone'] = $_POST['timezone'];
  136. $config['system']['ntp']['enable'] = isset($_POST['ntp_enable']) ? true : false;
  137. $config['system']['ntp']['timeservers'] = strtolower($_POST['ntp_timeservers']);
  138. $config['system']['ntp']['updateinterval'] = $_POST['ntp_updateinterval'];
  139. $config['system']['webgui']['certificate'] = base64_encode($_POST['certificate']);
  140. $config['system']['webgui']['privatekey'] = base64_encode($_POST['privatekey']);
  141. unset($config['system']['dnsserver']);
  142. // Only store IPv4 DNS servers when using static IPv4.
  143. if ("dhcp" !== $config['interfaces']['lan']['ipaddr']) {
  144. unset($config['system']['dnsserver']);
  145. if ($_POST['dns1'])
  146. $config['system']['dnsserver'][] = $_POST['dns1'];
  147. if ($_POST['dns2'])
  148. $config['system']['dnsserver'][] = $_POST['dns2'];
  149. }
  150. // Only store IPv6 DNS servers when using static IPv6.
  151. if ("auto" !== $config['interfaces']['lan']['ipv6addr']) {
  152. unset($config['system']['ipv6dnsserver']);
  153. if ($_POST['ipv6dns1'])
  154. $config['system']['ipv6dnsserver'][] = $_POST['ipv6dns1'];
  155. if ($_POST['ipv6dns2'])
  156. $config['system']['ipv6dnsserver'][] = $_POST['ipv6dns2'];
  157. }
  158. $olddnsallowoverride = isset($config['system']['dnsallowoverride']);
  159. $config['system']['dnsallowoverride'] = isset($_POST['dnsallowoverride']) ? true : false;
  160. write_config();
  161. set_php_timezone();
  162. // Check if a reboot is required.
  163. if (($oldwebguiproto != $config['system']['webgui']['protocol']) ||
  164. ($oldwebguiport != $config['system']['webgui']['port'])) {
  165. touch($d_sysrebootreqd_path);
  166. }
  167. if (($config['system']['webgui']['certificate'] != $oldcert) || ($config['system']['webgui']['privatekey'] != $oldkey)) {
  168. touch($d_sysrebootreqd_path);
  169. }
  170. $retval = 0;
  171. if (!file_exists($d_sysrebootreqd_path)) {
  172. config_lock();
  173. $retval |= rc_exec_service("rcconf");
  174. $retval |= rc_exec_service("timezone");
  175. $retval |= rc_exec_service("resolv");
  176. $retval |= rc_exec_service("hosts");
  177. $retval |= rc_restart_service("hostname");
  178. $retval |= rc_exec_service("userdb");
  179. $retval |= rc_exec_service("htpasswd");
  180. $retval |= rc_exec_service("websrv_htpasswd");
  181. $retval |= rc_update_service("ntpdate");
  182. $retval |= rc_update_service("mdnsresponder");
  183. $retval |= rc_update_service("bsnmpd");
  184. $retval |= rc_update_service("cron");
  185. config_unlock();
  186. }
  187. if (($pconfig['systime'] !== "Not Set") && (!empty($pconfig['systime']))) {
  188. $timestamp = strtotime($pconfig['systime']);
  189. if (FALSE !== $timestamp) {
  190. $timestamp = strftime("%g%m%d%H%M", $timestamp);
  191. // The date utility exits 0 on success, 1 if unable to set the date,
  192. // and 2 if able to set the local date, but unable to set it globally.
  193. $retval |= mwexec("/bin/date -n {$timestamp}");
  194. $pconfig['systime'] = "Not Set";
  195. }
  196. }
  197. $savemsg = get_std_save_message($retval);
  198. // Update DNS server controls.
  199. list($pconfig['dns1'],$pconfig['dns2']) = get_ipv4dnsserver();
  200. list($pconfig['ipv6dns1'],$pconfig['ipv6dns2']) = get_ipv6dnsserver();
  201. // Reload page if language has been changed, otherwise page is displayed
  202. // in previous selected language.
  203. if ($oldlanguage !== $config['system']['language']) {
  204. header("Location: system.php");
  205. exit;
  206. }
  207. }
  208. }
  209. $pglocalheader = <<< EOD
  210. <link rel="stylesheet" type="text/css" href="datechooser.css" />
  211. <script type="text/javascript" src="datechooser.js"></script>
  212. EOD;
  213. ?>
  214. <?php include("fbegin.inc");?>
  215. <script type="text/javascript">
  216. <!--
  217. function ntp_change() {
  218. switch(document.iform.ntp_enable.checked) {
  219. case false:
  220. showElementById('ntp_timeservers_tr','hide');
  221. showElementById('ntp_updateinterval_tr','hide');
  222. break;
  223. case true:
  224. showElementById('ntp_timeservers_tr','show');
  225. showElementById('ntp_updateinterval_tr','show');
  226. break;
  227. }
  228. }
  229. function webguiproto_change() {
  230. switch(document.iform.webguiproto.selectedIndex) {
  231. case 0:
  232. showElementById('privatekey_tr','hide');
  233. showElementById('certificate_tr','hide');
  234. break;
  235. default:
  236. showElementById('privatekey_tr','show');
  237. showElementById('certificate_tr','show');
  238. break;
  239. }
  240. }
  241. //-->
  242. </script>
  243. <table width="100%" border="0" cellpadding="0" cellspacing="0">
  244. <tr>
  245. <td class="tabnavtbl">
  246. <ul id="tabnav">
  247. <li class="tabact"><a href="system.php" title="<?=gettext("Reload page");?>"><span><?=gettext("General");?></span></a></li>
  248. <li class="tabinact"><a href="system_password.php"><span><?=gettext("Password");?></span></a></li>
  249. </ul>
  250. </td>
  251. </tr>
  252. <tr>
  253. <td class="tabcont">
  254. <form action="system.php" method="post" name="iform" id="iform">
  255. <?php if (!empty($input_errors)) print_input_errors($input_errors);?>
  256. <?php if (!empty($savemsg)) print_info_box($savemsg);?>
  257. <table width="100%" border="0" cellpadding="6" cellspacing="0">
  258. <tr>
  259. <td colspan="2" valign="top" class="listtopic"><?=gettext("Hostname");?></td>
  260. </tr>
  261. <?php html_inputbox("hostname", gettext("Hostname"), $pconfig['hostname'], sprintf(gettext("Name of the NAS host, without domain part e.g. %s."), "<em>" . strtolower(get_product_name()) ."</em>"), true, 40);?>
  262. <?php html_inputbox("domain", gettext("Domain"), $pconfig['domain'], sprintf(gettext("e.g. %s"), "<em>com, local</em>"), false, 40);?>
  263. <?php html_separator();?>
  264. <?php html_titleline(gettext("DNS settings"));?>
  265. <tr>
  266. <td width="22%" valign="top" class="vncell"><?=gettext("IPv4 DNS servers");?></td>
  267. <td width="78%" class="vtable">
  268. <?php $readonly = ("dhcp" === $config['interfaces']['lan']['ipaddr']) ? "readonly=\"readonly\"" : "";?>
  269. <input name="dns1" type="text" class="formfld" id="dns1" size="20" value="<?=htmlspecialchars($pconfig['dns1']);?>" <?=$readonly;?> /><br />
  270. <input name="dns2" type="text" class="formfld" id="dns2" size="20" value="<?=htmlspecialchars($pconfig['dns2']);?>" <?=$readonly;?> /><br />
  271. <span class="vexpl"><?=gettext("IPv4 addresses");?></span><br />
  272. </td>
  273. </tr>
  274. <tr>
  275. <td width="22%" valign="top" class="vncell"><?=gettext("IPv6 DNS servers");?></td>
  276. <td width="78%" class="vtable">
  277. <?php $readonly = (!isset($config['interfaces']['lan']['ipv6_enable']) || ("auto" === $config['interfaces']['lan']['ipv6addr'])) ? "readonly=\"readonly\"" : "";?>
  278. <input name="ipv6dns1" type="text" class="formfld" id="ipv6dns1" size="20" value="<?=htmlspecialchars($pconfig['ipv6dns1']);?>" <?=$readonly;?> /><br />
  279. <input name="ipv6dns2" type="text" class="formfld" id="ipv6dns2" size="20" value="<?=htmlspecialchars($pconfig['ipv6dns2']);?>" <?=$readonly;?> /><br />
  280. <span class="vexpl"><?=gettext("IPv6 addresses");?></span><br />
  281. </td>
  282. </tr>
  283. <?php html_separator();?>
  284. <?php html_titleline(gettext("WebGUI"));?>
  285. <?php html_inputbox("username", gettext("Username"), $pconfig['username'], gettext("If you want to change the username for accessing the WebGUI, enter it here."), false, 20);?>
  286. <?php html_combobox("webguiproto", gettext("Protocol"), $pconfig['webguiproto'], array("http" => "HTTP", "https" => "HTTPS"), "", false, false, "webguiproto_change()");?>
  287. <?php html_inputbox("webguiport", gettext("Port"), $pconfig['webguiport'], gettext("Enter a custom port number for the WebGUI above if you want to override the default (80 for HTTP, 443 for HTTPS)."), false, 5);?>
  288. <?php html_textarea("certificate", gettext("Certificate"), $pconfig['certificate'], gettext("Paste a signed certificate in X.509 PEM format here."), true, 65, 7, false, false);?>
  289. <?php html_textarea("privatekey", gettext("Private key"), $pconfig['privatekey'], gettext("Paste an private key in PEM format here."), true, 65, 7, false, false);?>
  290. <?php html_languagecombobox("language", gettext("Language"), $pconfig['language'], "", false);?>
  291. <?php html_separator();?>
  292. <?php html_titleline(gettext("Time"));?>
  293. <?php html_timezonecombobox("timezone", gettext("Time zone"), $pconfig['timezone'], gettext("Select the location closest to you."), false);?>
  294. <tr>
  295. <td width="22%" valign="top" class="vncell"><?=gettext("System time");?></td>
  296. <td width="78%" class="vtable">
  297. <input id="systime" size="20" maxlength="20" name="systime" type="text" value="" />
  298. <img src="cal.gif" onclick="showChooser(this, 'systime', 'chooserSpan', 1950, 2020, Date.patterns.Default, true);" alt="" />
  299. <div id="chooserSpan" class="dateChooser select-free" style="display: none; visibility: hidden; width: 160px;"></div><br />
  300. <span class="vexpl"><?=gettext("Enter desired system time directly (format mm/dd/yyyy hh:mm) or use icon to select it.");?></span>
  301. </td>
  302. </tr>
  303. <?php html_checkbox("ntp_enable", gettext("Enable NTP"), !empty($pconfig['ntp_enable']) ? true : false, gettext("Use the specified NTP server."), "", false, "ntp_change()");?>
  304. <?php html_inputbox("ntp_timeservers", gettext("NTP time server"), $pconfig['ntp_timeservers'], gettext("Use a space to separate multiple hosts (only one required). Remember to set up at least one DNS server if you enter a host name here!"), true, 40);?>
  305. <?php html_inputbox("ntp_updateinterval", gettext("Time update interval"), $pconfig['ntp_updateinterval'], gettext("Minutes between network time sync."), true, 20);?>
  306. </table>
  307. <div id="submit">
  308. <input name="Submit" type="submit" class="formbtn" value="<?=gettext("Save");?>" />
  309. </div>
  310. <?php include("formend.inc");?>
  311. </form>
  312. </td>
  313. </tr>
  314. </table>
  315. <script type="text/javascript">
  316. <!--
  317. ntp_change();
  318. webguiproto_change();
  319. //-->
  320. </script>
  321. <?php include("fend.inc");?>