PageRenderTime 36ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/usr/local/www/exec.php

https://github.com/vongrippen/pfsense
PHP | 336 lines | 220 code | 51 blank | 65 comment | 26 complexity | 552c2ba29d229742431635521e4cdf19 MD5 | raw file
  1. <?php
  2. /* $Id$ */
  3. /*
  4. Exec+ v1.02-000 - Copyright 2001-2003, All rights reserved
  5. Created by technologEase (http://www.technologEase.com).
  6. (modified for m0n0wall by Manuel Kasper <mk@neon1.net>)
  7. Redistribution and use in source and binary forms, with or without
  8. modification, are permitted provided that the following conditions are met:
  9. 1. Redistributions of source code must retain the above copyright notice,
  10. this list of conditions and the following disclaimer.
  11. 2. Redistributions in binary form must reproduce the above copyright
  12. notice, this list of conditions and the following disclaimer in the
  13. documentation and/or other materials provided with the distribution.
  14. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  15. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  16. AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  17. AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  18. OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  23. POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. /*
  26. pfSense_MODULE: shell
  27. */
  28. ##|+PRIV
  29. ##|*IDENT=page-diagnostics-command
  30. ##|*NAME=Diagnostics: Command page
  31. ##|*DESCR=Allow access to the 'Diagnostics: Command' page.
  32. ##|*MATCH=exec.php*
  33. ##|-PRIV
  34. require("guiconfig.inc");
  35. if (($_POST['submit'] == "Download") && file_exists($_POST['dlPath'])) {
  36. session_cache_limiter('public');
  37. $fd = fopen($_POST['dlPath'], "rb");
  38. header("Content-Type: application/octet-stream");
  39. header("Content-Length: " . filesize($_POST['dlPath']));
  40. header("Content-Disposition: attachment; filename=\"" .
  41. trim(htmlentities(basename($_POST['dlPath']))) . "\"");
  42. if (isset($_SERVER['HTTPS'])) {
  43. header('Pragma: ');
  44. header('Cache-Control: ');
  45. } else {
  46. header("Pragma: private");
  47. header("Cache-Control: private, must-revalidate");
  48. }
  49. fpassthru($fd);
  50. exit;
  51. } else if (($_POST['submit'] == "Upload") && is_uploaded_file($_FILES['ulfile']['tmp_name'])) {
  52. move_uploaded_file($_FILES['ulfile']['tmp_name'], "/tmp/" . $_FILES['ulfile']['name']);
  53. $ulmsg = "Uploaded file to /tmp/" . htmlentities($_FILES['ulfile']['name']);
  54. unset($_POST['txtCommand']);
  55. }
  56. if($_POST)
  57. conf_mount_rw();
  58. // Function: is Blank
  59. // Returns true or false depending on blankness of argument.
  60. function isBlank( $arg ) { return ereg( "^\s*$", $arg ); }
  61. // Function: Puts
  62. // Put string, Ruby-style.
  63. function puts( $arg ) { echo "$arg\n"; }
  64. // "Constants".
  65. $Version = '';
  66. $ScriptName = $HTTP_SERVER_VARS['SCRIPT_NAME'];
  67. // Get year.
  68. $arrDT = localtime();
  69. $intYear = $arrDT[5] + 1900;
  70. $pgtitle = array(gettext("Diagnostics"),gettext("Execute command"));
  71. include("head.inc");
  72. ?>
  73. <script language="javascript">
  74. <!--
  75. // Create recall buffer array (of encoded strings).
  76. <?php
  77. if (isBlank( $_POST['txtRecallBuffer'] )) {
  78. puts( " var arrRecallBuffer = new Array;" );
  79. } else {
  80. puts( " var arrRecallBuffer = new Array(" );
  81. $arrBuffer = explode( "&", $_POST['txtRecallBuffer'] );
  82. for ($i=0; $i < (count( $arrBuffer ) - 1); $i++) puts( " '" . htmlspecialchars($arrBuffer[$i]) . "'," );
  83. puts( " '" . htmlspecialchars($arrBuffer[count( $arrBuffer ) - 1]) . "'" );
  84. puts( " );" );
  85. }
  86. ?>
  87. // Set pointer to end of recall buffer.
  88. var intRecallPtr = arrRecallBuffer.length-1;
  89. // Functions to extend String class.
  90. function str_encode() { return escape( this ) }
  91. function str_decode() { return unescape( this ) }
  92. // Extend string class to include encode() and decode() functions.
  93. String.prototype.encode = str_encode
  94. String.prototype.decode = str_decode
  95. // Function: is Blank
  96. // Returns boolean true or false if argument is blank.
  97. function isBlank( strArg ) { return strArg.match( /^\s*$/ ) }
  98. // Function: frmExecPlus onSubmit (event handler)
  99. // Builds the recall buffer from the command string on submit.
  100. function frmExecPlus_onSubmit( form ) {
  101. if (!isBlank(form.txtCommand.value)) {
  102. // If this command is repeat of last command, then do not store command.
  103. if (form.txtCommand.value.encode() == arrRecallBuffer[arrRecallBuffer.length-1]) { return true }
  104. // Stuff encoded command string into the recall buffer.
  105. if (isBlank(form.txtRecallBuffer.value))
  106. form.txtRecallBuffer.value = form.txtCommand.value.encode();
  107. else
  108. form.txtRecallBuffer.value += '&' + form.txtCommand.value.encode();
  109. }
  110. return true;
  111. }
  112. // Function: btnRecall onClick (event handler)
  113. // Recalls command buffer going either up or down.
  114. function btnRecall_onClick( form, n ) {
  115. // If nothing in recall buffer, then error.
  116. if (!arrRecallBuffer.length) {
  117. alert( '<?=gettext("Nothing to recall"); ?>!' );
  118. form.txtCommand.focus();
  119. return;
  120. }
  121. // Increment recall buffer pointer in positive or negative direction
  122. // according to <n>.
  123. intRecallPtr += n;
  124. // Make sure the buffer stays circular.
  125. if (intRecallPtr < 0) { intRecallPtr = arrRecallBuffer.length - 1 }
  126. if (intRecallPtr > (arrRecallBuffer.length - 1)) { intRecallPtr = 0 }
  127. // Recall the command.
  128. form.txtCommand.value = arrRecallBuffer[intRecallPtr].decode();
  129. }
  130. // Function: Reset onClick (event handler)
  131. // Resets form on reset button click event.
  132. function Reset_onClick( form ) {
  133. // Reset recall buffer pointer.
  134. intRecallPtr = arrRecallBuffer.length;
  135. // Clear form (could have spaces in it) and return focus ready for cmd.
  136. form.txtCommand.value = '';
  137. form.txtCommand.focus();
  138. return true;
  139. }
  140. //-->
  141. </script>
  142. <style>
  143. <!--
  144. input {
  145. font-family: courier new, courier;
  146. font-weight: normal;
  147. font-size: 9pt;
  148. }
  149. pre {
  150. border: 2px solid #435370;
  151. background: #F0F0F0;
  152. padding: 1em;
  153. font-family: courier new, courier;
  154. white-space: pre;
  155. line-height: 10pt;
  156. font-size: 10pt;
  157. }
  158. .label {
  159. font-family: tahoma, verdana, arial, helvetica;
  160. font-size: 11px;
  161. font-weight: bold;
  162. }
  163. .button {
  164. font-family: tahoma, verdana, arial, helvetica;
  165. font-weight: bold;
  166. font-size: 11px;
  167. }
  168. -->
  169. </style>
  170. </head>
  171. <body link="#0000CC" vlink="#0000CC" alink="#0000CC">
  172. <?php include("fbegin.inc"); ?>
  173. <?php if (isBlank($_POST['txtCommand'])): ?>
  174. <p class="red"><strong><?=gettext("Note: this function is unsupported. Use it " .
  175. "on your own risk"); ?>!</strong></p>
  176. <?php endif; ?>
  177. <?php if ($ulmsg) echo "<p><strong>" . $ulmsg . "</strong></p>\n"; ?>
  178. <?php
  179. if (!isBlank($_POST['txtCommand'])) {
  180. puts("<pre>");
  181. puts("\$ " . htmlspecialchars($_POST['txtCommand']));
  182. putenv("PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin");
  183. putenv("SCRIPT_FILENAME=" . strtok($_POST['txtCommand'], " ")); /* PHP scripts */
  184. $ph = popen($_POST['txtCommand'], "r" );
  185. while ($line = fgets($ph)) echo htmlspecialchars($line);
  186. pclose($ph);
  187. puts("</pre>");
  188. }
  189. if (!isBlank($_POST['txtPHPCommand'])) {
  190. puts("<pre>");
  191. require_once("config.inc");
  192. require_once("functions.inc");
  193. echo eval($_POST['txtPHPCommand']);
  194. puts("</pre>");
  195. }
  196. ?>
  197. <div id="niftyOutter">
  198. <form action="exec.php" method="POST" enctype="multipart/form-data" name="frmExecPlus" onSubmit="return frmExecPlus_onSubmit( this );">
  199. <table>
  200. <tr>
  201. <td colspan="2" valign="top" class="vnsepcell"><?=gettext("Execute Shell command"); ?></td>
  202. </tr>
  203. <tr>
  204. <td class="label" align="right"><?=gettext("Command"); ?>:</td>
  205. <td class="type"><input id="txtCommand" name="txtCommand" type="text" class="formfld unknown" size="80" value="<?=htmlspecialchars($_POST['txtCommand']);?>"></td>
  206. </tr>
  207. <tr>
  208. <td valign="top">&nbsp;&nbsp;&nbsp;</td>
  209. <td valign="top" class="label">
  210. <input type="hidden" name="txtRecallBuffer" value="<?=htmlspecialchars($_POST['txtRecallBuffer']) ?>">
  211. <input type="button" class="button" name="btnRecallPrev" value="<" onClick="btnRecall_onClick( this.form, -1 );">
  212. <input type="submit" class="button" value="<?=gettext("Execute"); ?>">
  213. <input type="button" class="button" name="btnRecallNext" value=">" onClick="btnRecall_onClick( this.form, 1 );">
  214. <input type="button" class="button" value="<?=gettext("Clear"); ?>" onClick="return Reset_onClick( this.form );">
  215. </td>
  216. </tr>
  217. <tr>
  218. <td colspan="2" valign="top" height="16"></td>
  219. </tr>
  220. <tr>
  221. <td colspan="2" valign="top" class="vnsepcell"><?=gettext("Download"); ?></td>
  222. </tr>
  223. <tr>
  224. <td align="right"><?=gettext("File to download"); ?>:</td>
  225. <td>
  226. <input name="dlPath" type="text" class="formfld file" id="dlPath" size="50">
  227. </td></tr>
  228. <tr>
  229. <td valign="top">&nbsp;&nbsp;&nbsp;</td>
  230. <td valign="top" class="label">
  231. <input name="submit" type="submit" class="button" id="download" value="<?=gettext("Download"); ?>">
  232. </td>
  233. </tr>
  234. <tr>
  235. <td colspan="2" valign="top" height="16"></td>
  236. </tr>
  237. <tr>
  238. <td colspan="2" valign="top" class="vnsepcell"><?=gettext("Upload"); ?></td>
  239. </tr>
  240. <tr>
  241. <td align="right"><?=gettext("File to upload"); ?>:</td>
  242. <td valign="top" class="label">
  243. <input name="ulfile" type="file" class="formfld file" id="ulfile">
  244. </td></tr>
  245. <tr>
  246. <td valign="top">&nbsp;&nbsp;&nbsp;</td>
  247. <td valign="top" class="label">
  248. <input name="submit" type="submit" class="button" id="upload" value="<?=gettext("Upload"); ?>"></td>
  249. </tr>
  250. <tr>
  251. <td colspan="2" valign="top" height="16"></td>
  252. </tr>
  253. <tr>
  254. <td colspan="2" valign="top" class="vnsepcell"><?=gettext("PHP Execute"); ?></td>
  255. </tr>
  256. <tr>
  257. <td align="right"><?=gettext("Command"); ?>:</td>
  258. <td class="type"><textarea id="txtPHPCommand" name="txtPHPCommand" type="text" rows="9" cols="80"><?=htmlspecialchars($_POST['txtPHPCommand']);?></textarea></td>
  259. </tr>
  260. <tr>
  261. <td valign="top">&nbsp;&nbsp;&nbsp;</td>
  262. <td valign="top" class="label">
  263. <input type="submit" class="button" value="<?=gettext("Execute"); ?>">
  264. <p>
  265. <strong><?=gettext("Example"); ?>:</strong> interfaces_carp_setup();
  266. </td>
  267. </tr>
  268. </table>
  269. </div>
  270. <?php include("fend.inc"); ?>
  271. </form>
  272. <script language="Javascript">
  273. document.forms[0].txtCommand.focus();
  274. </script>
  275. </body>
  276. </html>
  277. <?php
  278. if($_POST)
  279. conf_mount_ro();
  280. ?>