PageRenderTime 60ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/ext/standard/exec.c

http://github.com/php/php-src
C | 587 lines | 479 code | 47 blank | 61 comment | 59 complexity | 7ad52ec50b4307fc33ddaf289910e9ef MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | http://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: Rasmus Lerdorf <rasmus@php.net> |
  14. | Ilia Alshanetsky <iliaa@php.net> |
  15. +----------------------------------------------------------------------+
  16. */
  17. #include <stdio.h>
  18. #include "php.h"
  19. #include <ctype.h>
  20. #include "php_string.h"
  21. #include "ext/standard/head.h"
  22. #include "ext/standard/file.h"
  23. #include "basic_functions.h"
  24. #include "exec.h"
  25. #include "php_globals.h"
  26. #include "SAPI.h"
  27. #if HAVE_SYS_WAIT_H
  28. #include <sys/wait.h>
  29. #endif
  30. #include <signal.h>
  31. #if HAVE_SYS_TYPES_H
  32. #include <sys/types.h>
  33. #endif
  34. #if HAVE_SYS_STAT_H
  35. #include <sys/stat.h>
  36. #endif
  37. #if HAVE_FCNTL_H
  38. #include <fcntl.h>
  39. #endif
  40. #if HAVE_UNISTD_H
  41. #include <unistd.h>
  42. #endif
  43. #include <limits.h>
  44. #ifdef PHP_WIN32
  45. # include "win32/nice.h"
  46. #endif
  47. static size_t cmd_max_len;
  48. /* {{{ PHP_MINIT_FUNCTION(exec) */
  49. PHP_MINIT_FUNCTION(exec)
  50. {
  51. #ifdef _SC_ARG_MAX
  52. cmd_max_len = sysconf(_SC_ARG_MAX);
  53. if ((size_t)-1 == cmd_max_len) {
  54. #ifdef _POSIX_ARG_MAX
  55. cmd_max_len = _POSIX_ARG_MAX;
  56. #else
  57. cmd_max_len = 4096;
  58. #endif
  59. }
  60. #elif defined(ARG_MAX)
  61. cmd_max_len = ARG_MAX;
  62. #elif defined(PHP_WIN32)
  63. /* Executed commands will run through cmd.exe. As long as it's the case,
  64. it's just the constant limit.*/
  65. cmd_max_len = 8192;
  66. #else
  67. /* This is just an arbitrary value for the fallback case. */
  68. cmd_max_len = 4096;
  69. #endif
  70. return SUCCESS;
  71. }
  72. /* }}} */
  73. static size_t strip_trailing_whitespace(char *buf, size_t bufl) {
  74. size_t l = bufl;
  75. while (l-- > 0 && isspace(((unsigned char *)buf)[l]));
  76. if (l != (bufl - 1)) {
  77. bufl = l + 1;
  78. buf[bufl] = '\0';
  79. }
  80. return bufl;
  81. }
  82. static size_t handle_line(int type, zval *array, char *buf, size_t bufl) {
  83. if (type == 1) {
  84. PHPWRITE(buf, bufl);
  85. if (php_output_get_level() < 1) {
  86. sapi_flush();
  87. }
  88. } else if (type == 2) {
  89. bufl = strip_trailing_whitespace(buf, bufl);
  90. add_next_index_stringl(array, buf, bufl);
  91. }
  92. return bufl;
  93. }
  94. /* {{{ php_exec
  95. * If type==0, only last line of output is returned (exec)
  96. * If type==1, all lines will be printed and last lined returned (system)
  97. * If type==2, all lines will be saved to given array (exec with &$array)
  98. * If type==3, output will be printed binary, no lines will be saved or returned (passthru)
  99. *
  100. */
  101. PHPAPI int php_exec(int type, char *cmd, zval *array, zval *return_value)
  102. {
  103. FILE *fp;
  104. char *buf;
  105. int pclose_return;
  106. char *b, *d=NULL;
  107. php_stream *stream;
  108. size_t buflen, bufl = 0;
  109. #if PHP_SIGCHILD
  110. void (*sig_handler)() = NULL;
  111. #endif
  112. #if PHP_SIGCHILD
  113. sig_handler = signal (SIGCHLD, SIG_DFL);
  114. #endif
  115. #ifdef PHP_WIN32
  116. fp = VCWD_POPEN(cmd, "rb");
  117. #else
  118. fp = VCWD_POPEN(cmd, "r");
  119. #endif
  120. if (!fp) {
  121. php_error_docref(NULL, E_WARNING, "Unable to fork [%s]", cmd);
  122. goto err;
  123. }
  124. stream = php_stream_fopen_from_pipe(fp, "rb");
  125. buf = (char *) emalloc(EXEC_INPUT_BUF);
  126. buflen = EXEC_INPUT_BUF;
  127. if (type != 3) {
  128. b = buf;
  129. while (php_stream_get_line(stream, b, EXEC_INPUT_BUF, &bufl)) {
  130. /* no new line found, let's read some more */
  131. if (b[bufl - 1] != '\n' && !php_stream_eof(stream)) {
  132. if (buflen < (bufl + (b - buf) + EXEC_INPUT_BUF)) {
  133. bufl += b - buf;
  134. buflen = bufl + EXEC_INPUT_BUF;
  135. buf = erealloc(buf, buflen);
  136. b = buf + bufl;
  137. } else {
  138. b += bufl;
  139. }
  140. continue;
  141. } else if (b != buf) {
  142. bufl += b - buf;
  143. }
  144. bufl = handle_line(type, array, buf, bufl);
  145. b = buf;
  146. }
  147. if (bufl) {
  148. if (buf != b) {
  149. /* Process remaining output */
  150. bufl = handle_line(type, array, buf, bufl);
  151. }
  152. /* Return last line from the shell command */
  153. bufl = strip_trailing_whitespace(buf, bufl);
  154. RETVAL_STRINGL(buf, bufl);
  155. } else { /* should return NULL, but for BC we return "" */
  156. RETVAL_EMPTY_STRING();
  157. }
  158. } else {
  159. ssize_t read;
  160. while ((read = php_stream_read(stream, buf, EXEC_INPUT_BUF)) > 0) {
  161. PHPWRITE(buf, read);
  162. }
  163. }
  164. pclose_return = php_stream_close(stream);
  165. efree(buf);
  166. done:
  167. #if PHP_SIGCHILD
  168. if (sig_handler) {
  169. signal(SIGCHLD, sig_handler);
  170. }
  171. #endif
  172. if (d) {
  173. efree(d);
  174. }
  175. return pclose_return;
  176. err:
  177. pclose_return = -1;
  178. RETVAL_FALSE;
  179. goto done;
  180. }
  181. /* }}} */
  182. static void php_exec_ex(INTERNAL_FUNCTION_PARAMETERS, int mode) /* {{{ */
  183. {
  184. char *cmd;
  185. size_t cmd_len;
  186. zval *ret_code=NULL, *ret_array=NULL;
  187. int ret;
  188. ZEND_PARSE_PARAMETERS_START(1, (mode ? 2 : 3))
  189. Z_PARAM_STRING(cmd, cmd_len)
  190. Z_PARAM_OPTIONAL
  191. if (!mode) {
  192. Z_PARAM_ZVAL(ret_array)
  193. }
  194. Z_PARAM_ZVAL(ret_code)
  195. ZEND_PARSE_PARAMETERS_END();
  196. if (!cmd_len) {
  197. php_error_docref(NULL, E_WARNING, "Cannot execute a blank command");
  198. RETURN_FALSE;
  199. }
  200. if (strlen(cmd) != cmd_len) {
  201. php_error_docref(NULL, E_WARNING, "NULL byte detected. Possible attack");
  202. RETURN_FALSE;
  203. }
  204. if (!ret_array) {
  205. ret = php_exec(mode, cmd, NULL, return_value);
  206. } else {
  207. if (Z_TYPE_P(Z_REFVAL_P(ret_array)) == IS_ARRAY) {
  208. ZVAL_DEREF(ret_array);
  209. SEPARATE_ARRAY(ret_array);
  210. } else {
  211. ret_array = zend_try_array_init(ret_array);
  212. if (!ret_array) {
  213. RETURN_THROWS();
  214. }
  215. }
  216. ret = php_exec(2, cmd, ret_array, return_value);
  217. }
  218. if (ret_code) {
  219. ZEND_TRY_ASSIGN_REF_LONG(ret_code, ret);
  220. }
  221. }
  222. /* }}} */
  223. /* {{{ proto string|false exec(string command [, array &output [, int &return_value]])
  224. Execute an external program */
  225. PHP_FUNCTION(exec)
  226. {
  227. php_exec_ex(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  228. }
  229. /* }}} */
  230. /* {{{ proto int|false system(string command [, int &return_value])
  231. Execute an external program and display output */
  232. PHP_FUNCTION(system)
  233. {
  234. php_exec_ex(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  235. }
  236. /* }}} */
  237. /* {{{ proto bool passthru(string command [, int &return_value])
  238. Execute an external program and display raw output */
  239. PHP_FUNCTION(passthru)
  240. {
  241. php_exec_ex(INTERNAL_FUNCTION_PARAM_PASSTHRU, 3);
  242. }
  243. /* }}} */
  244. /* {{{ php_escape_shell_cmd
  245. Escape all chars that could possibly be used to
  246. break out of a shell command
  247. This function emalloc's a string and returns the pointer.
  248. Remember to efree it when done with it.
  249. *NOT* safe for binary strings
  250. */
  251. PHPAPI zend_string *php_escape_shell_cmd(char *str)
  252. {
  253. register size_t x, y;
  254. size_t l = strlen(str);
  255. uint64_t estimate = (2 * (uint64_t)l) + 1;
  256. zend_string *cmd;
  257. #ifndef PHP_WIN32
  258. char *p = NULL;
  259. #endif
  260. /* max command line length - two single quotes - \0 byte length */
  261. if (l > cmd_max_len - 2 - 1) {
  262. php_error_docref(NULL, E_ERROR, "Command exceeds the allowed length of %zu bytes", cmd_max_len);
  263. return ZSTR_EMPTY_ALLOC();
  264. }
  265. cmd = zend_string_safe_alloc(2, l, 0, 0);
  266. for (x = 0, y = 0; x < l; x++) {
  267. int mb_len = php_mblen(str + x, (l - x));
  268. /* skip non-valid multibyte characters */
  269. if (mb_len < 0) {
  270. continue;
  271. } else if (mb_len > 1) {
  272. memcpy(ZSTR_VAL(cmd) + y, str + x, mb_len);
  273. y += mb_len;
  274. x += mb_len - 1;
  275. continue;
  276. }
  277. switch (str[x]) {
  278. #ifndef PHP_WIN32
  279. case '"':
  280. case '\'':
  281. if (!p && (p = memchr(str + x + 1, str[x], l - x - 1))) {
  282. /* noop */
  283. } else if (p && *p == str[x]) {
  284. p = NULL;
  285. } else {
  286. ZSTR_VAL(cmd)[y++] = '\\';
  287. }
  288. ZSTR_VAL(cmd)[y++] = str[x];
  289. break;
  290. #else
  291. /* % is Windows specific for environmental variables, ^%PATH% will
  292. output PATH while ^%PATH^% will not. escapeshellcmd->val will escape all % and !.
  293. */
  294. case '%':
  295. case '!':
  296. case '"':
  297. case '\'':
  298. #endif
  299. case '#': /* This is character-set independent */
  300. case '&':
  301. case ';':
  302. case '`':
  303. case '|':
  304. case '*':
  305. case '?':
  306. case '~':
  307. case '<':
  308. case '>':
  309. case '^':
  310. case '(':
  311. case ')':
  312. case '[':
  313. case ']':
  314. case '{':
  315. case '}':
  316. case '$':
  317. case '\\':
  318. case '\x0A': /* excluding these two */
  319. case '\xFF':
  320. #ifdef PHP_WIN32
  321. ZSTR_VAL(cmd)[y++] = '^';
  322. #else
  323. ZSTR_VAL(cmd)[y++] = '\\';
  324. #endif
  325. /* fall-through */
  326. default:
  327. ZSTR_VAL(cmd)[y++] = str[x];
  328. }
  329. }
  330. ZSTR_VAL(cmd)[y] = '\0';
  331. if (y > cmd_max_len + 1) {
  332. php_error_docref(NULL, E_ERROR, "Escaped command exceeds the allowed length of %zu bytes", cmd_max_len);
  333. zend_string_release_ex(cmd, 0);
  334. return ZSTR_EMPTY_ALLOC();
  335. }
  336. if ((estimate - y) > 4096) {
  337. /* realloc if the estimate was way overill
  338. * Arbitrary cutoff point of 4096 */
  339. cmd = zend_string_truncate(cmd, y, 0);
  340. }
  341. ZSTR_LEN(cmd) = y;
  342. return cmd;
  343. }
  344. /* }}} */
  345. /* {{{ php_escape_shell_arg
  346. */
  347. PHPAPI zend_string *php_escape_shell_arg(char *str)
  348. {
  349. size_t x, y = 0;
  350. size_t l = strlen(str);
  351. zend_string *cmd;
  352. uint64_t estimate = (4 * (uint64_t)l) + 3;
  353. /* max command line length - two single quotes - \0 byte length */
  354. if (l > cmd_max_len - 2 - 1) {
  355. php_error_docref(NULL, E_ERROR, "Argument exceeds the allowed length of %zu bytes", cmd_max_len);
  356. return ZSTR_EMPTY_ALLOC();
  357. }
  358. cmd = zend_string_safe_alloc(4, l, 2, 0); /* worst case */
  359. #ifdef PHP_WIN32
  360. ZSTR_VAL(cmd)[y++] = '"';
  361. #else
  362. ZSTR_VAL(cmd)[y++] = '\'';
  363. #endif
  364. for (x = 0; x < l; x++) {
  365. int mb_len = php_mblen(str + x, (l - x));
  366. /* skip non-valid multibyte characters */
  367. if (mb_len < 0) {
  368. continue;
  369. } else if (mb_len > 1) {
  370. memcpy(ZSTR_VAL(cmd) + y, str + x, mb_len);
  371. y += mb_len;
  372. x += mb_len - 1;
  373. continue;
  374. }
  375. switch (str[x]) {
  376. #ifdef PHP_WIN32
  377. case '"':
  378. case '%':
  379. case '!':
  380. ZSTR_VAL(cmd)[y++] = ' ';
  381. break;
  382. #else
  383. case '\'':
  384. ZSTR_VAL(cmd)[y++] = '\'';
  385. ZSTR_VAL(cmd)[y++] = '\\';
  386. ZSTR_VAL(cmd)[y++] = '\'';
  387. #endif
  388. /* fall-through */
  389. default:
  390. ZSTR_VAL(cmd)[y++] = str[x];
  391. }
  392. }
  393. #ifdef PHP_WIN32
  394. if (y > 0 && '\\' == ZSTR_VAL(cmd)[y - 1]) {
  395. int k = 0, n = y - 1;
  396. for (; n >= 0 && '\\' == ZSTR_VAL(cmd)[n]; n--, k++);
  397. if (k % 2) {
  398. ZSTR_VAL(cmd)[y++] = '\\';
  399. }
  400. }
  401. ZSTR_VAL(cmd)[y++] = '"';
  402. #else
  403. ZSTR_VAL(cmd)[y++] = '\'';
  404. #endif
  405. ZSTR_VAL(cmd)[y] = '\0';
  406. if (y > cmd_max_len + 1) {
  407. php_error_docref(NULL, E_ERROR, "Escaped argument exceeds the allowed length of %zu bytes", cmd_max_len);
  408. zend_string_release_ex(cmd, 0);
  409. return ZSTR_EMPTY_ALLOC();
  410. }
  411. if ((estimate - y) > 4096) {
  412. /* realloc if the estimate was way overill
  413. * Arbitrary cutoff point of 4096 */
  414. cmd = zend_string_truncate(cmd, y, 0);
  415. }
  416. ZSTR_LEN(cmd) = y;
  417. return cmd;
  418. }
  419. /* }}} */
  420. /* {{{ proto string escapeshellcmd(string command)
  421. Escape shell metacharacters */
  422. PHP_FUNCTION(escapeshellcmd)
  423. {
  424. char *command;
  425. size_t command_len;
  426. ZEND_PARSE_PARAMETERS_START(1, 1)
  427. Z_PARAM_STRING(command, command_len)
  428. ZEND_PARSE_PARAMETERS_END();
  429. if (command_len) {
  430. if (command_len != strlen(command)) {
  431. zend_argument_type_error(1, "must not contain any null bytes");
  432. RETURN_THROWS();
  433. }
  434. RETVAL_STR(php_escape_shell_cmd(command));
  435. } else {
  436. RETVAL_EMPTY_STRING();
  437. }
  438. }
  439. /* }}} */
  440. /* {{{ proto string escapeshellarg(string arg)
  441. Quote and escape an argument for use in a shell command */
  442. PHP_FUNCTION(escapeshellarg)
  443. {
  444. char *argument;
  445. size_t argument_len;
  446. ZEND_PARSE_PARAMETERS_START(1, 1)
  447. Z_PARAM_STRING(argument, argument_len)
  448. ZEND_PARSE_PARAMETERS_END();
  449. if (argument_len != strlen(argument)) {
  450. zend_argument_type_error(1, "must not contain any null bytes");
  451. RETURN_THROWS();
  452. }
  453. RETVAL_STR(php_escape_shell_arg(argument));
  454. }
  455. /* }}} */
  456. /* {{{ proto string|false shell_exec(string cmd)
  457. Execute command via shell and return complete output as string */
  458. PHP_FUNCTION(shell_exec)
  459. {
  460. FILE *in;
  461. char *command;
  462. size_t command_len;
  463. zend_string *ret;
  464. php_stream *stream;
  465. ZEND_PARSE_PARAMETERS_START(1, 1)
  466. Z_PARAM_STRING(command, command_len)
  467. ZEND_PARSE_PARAMETERS_END();
  468. if (!command_len) {
  469. php_error_docref(NULL, E_WARNING, "Cannot execute a blank command");
  470. RETURN_FALSE;
  471. }
  472. if (strlen(command) != command_len) {
  473. php_error_docref(NULL, E_WARNING, "NULL byte detected. Possible attack");
  474. RETURN_FALSE;
  475. }
  476. #ifdef PHP_WIN32
  477. if ((in=VCWD_POPEN(command, "rt"))==NULL) {
  478. #else
  479. if ((in=VCWD_POPEN(command, "r"))==NULL) {
  480. #endif
  481. php_error_docref(NULL, E_WARNING, "Unable to execute '%s'", command);
  482. RETURN_FALSE;
  483. }
  484. stream = php_stream_fopen_from_pipe(in, "rb");
  485. ret = php_stream_copy_to_mem(stream, PHP_STREAM_COPY_ALL, 0);
  486. php_stream_close(stream);
  487. if (ret && ZSTR_LEN(ret) > 0) {
  488. RETVAL_STR(ret);
  489. }
  490. }
  491. /* }}} */
  492. #ifdef HAVE_NICE
  493. /* {{{ proto bool proc_nice(int priority)
  494. Change the priority of the current process */
  495. PHP_FUNCTION(proc_nice)
  496. {
  497. zend_long pri;
  498. ZEND_PARSE_PARAMETERS_START(1, 1)
  499. Z_PARAM_LONG(pri)
  500. ZEND_PARSE_PARAMETERS_END();
  501. errno = 0;
  502. php_ignore_value(nice(pri));
  503. if (errno) {
  504. #ifdef PHP_WIN32
  505. char *err = php_win_err();
  506. php_error_docref(NULL, E_WARNING, "%s", err);
  507. php_win_err_free(err);
  508. #else
  509. php_error_docref(NULL, E_WARNING, "Only a super user may attempt to increase the priority of a process");
  510. #endif
  511. RETURN_FALSE;
  512. }
  513. RETURN_TRUE;
  514. }
  515. /* }}} */
  516. #endif