exec.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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. | https://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, const 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. zend_argument_value_error(1, "cannot be empty");
  198. RETURN_THROWS();
  199. }
  200. if (strlen(cmd) != cmd_len) {
  201. zend_argument_value_error(1, "must not contain any null bytes");
  202. RETURN_THROWS();
  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. /* {{{ Execute an external program */
  224. PHP_FUNCTION(exec)
  225. {
  226. php_exec_ex(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  227. }
  228. /* }}} */
  229. /* {{{ Execute an external program and display output */
  230. PHP_FUNCTION(system)
  231. {
  232. php_exec_ex(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  233. }
  234. /* }}} */
  235. /* {{{ Execute an external program and display raw output */
  236. PHP_FUNCTION(passthru)
  237. {
  238. php_exec_ex(INTERNAL_FUNCTION_PARAM_PASSTHRU, 3);
  239. }
  240. /* }}} */
  241. /* {{{ php_escape_shell_cmd
  242. Escape all chars that could possibly be used to
  243. break out of a shell command
  244. This function emalloc's a string and returns the pointer.
  245. Remember to efree it when done with it.
  246. *NOT* safe for binary strings
  247. */
  248. PHPAPI zend_string *php_escape_shell_cmd(const char *str)
  249. {
  250. size_t x, y;
  251. size_t l = strlen(str);
  252. uint64_t estimate = (2 * (uint64_t)l) + 1;
  253. zend_string *cmd;
  254. #ifndef PHP_WIN32
  255. char *p = NULL;
  256. #endif
  257. /* max command line length - two single quotes - \0 byte length */
  258. if (l > cmd_max_len - 2 - 1) {
  259. php_error_docref(NULL, E_ERROR, "Command exceeds the allowed length of %zu bytes", cmd_max_len);
  260. return ZSTR_EMPTY_ALLOC();
  261. }
  262. cmd = zend_string_safe_alloc(2, l, 0, 0);
  263. for (x = 0, y = 0; x < l; x++) {
  264. int mb_len = php_mblen(str + x, (l - x));
  265. /* skip non-valid multibyte characters */
  266. if (mb_len < 0) {
  267. continue;
  268. } else if (mb_len > 1) {
  269. memcpy(ZSTR_VAL(cmd) + y, str + x, mb_len);
  270. y += mb_len;
  271. x += mb_len - 1;
  272. continue;
  273. }
  274. switch (str[x]) {
  275. #ifndef PHP_WIN32
  276. case '"':
  277. case '\'':
  278. if (!p && (p = memchr(str + x + 1, str[x], l - x - 1))) {
  279. /* noop */
  280. } else if (p && *p == str[x]) {
  281. p = NULL;
  282. } else {
  283. ZSTR_VAL(cmd)[y++] = '\\';
  284. }
  285. ZSTR_VAL(cmd)[y++] = str[x];
  286. break;
  287. #else
  288. /* % is Windows specific for environmental variables, ^%PATH% will
  289. output PATH while ^%PATH^% will not. escapeshellcmd->val will escape all % and !.
  290. */
  291. case '%':
  292. case '!':
  293. case '"':
  294. case '\'':
  295. #endif
  296. case '#': /* This is character-set independent */
  297. case '&':
  298. case ';':
  299. case '`':
  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 '\x0A': /* excluding these two */
  316. case '\xFF':
  317. #ifdef PHP_WIN32
  318. ZSTR_VAL(cmd)[y++] = '^';
  319. #else
  320. ZSTR_VAL(cmd)[y++] = '\\';
  321. #endif
  322. ZEND_FALLTHROUGH;
  323. default:
  324. ZSTR_VAL(cmd)[y++] = str[x];
  325. }
  326. }
  327. ZSTR_VAL(cmd)[y] = '\0';
  328. if (y > cmd_max_len + 1) {
  329. php_error_docref(NULL, E_ERROR, "Escaped command exceeds the allowed length of %zu bytes", cmd_max_len);
  330. zend_string_release_ex(cmd, 0);
  331. return ZSTR_EMPTY_ALLOC();
  332. }
  333. if ((estimate - y) > 4096) {
  334. /* realloc if the estimate was way overill
  335. * Arbitrary cutoff point of 4096 */
  336. cmd = zend_string_truncate(cmd, y, 0);
  337. }
  338. ZSTR_LEN(cmd) = y;
  339. return cmd;
  340. }
  341. /* }}} */
  342. /* {{{ php_escape_shell_arg */
  343. PHPAPI zend_string *php_escape_shell_arg(const char *str)
  344. {
  345. size_t x, y = 0;
  346. size_t l = strlen(str);
  347. zend_string *cmd;
  348. uint64_t estimate = (4 * (uint64_t)l) + 3;
  349. /* max command line length - two single quotes - \0 byte length */
  350. if (l > cmd_max_len - 2 - 1) {
  351. php_error_docref(NULL, E_ERROR, "Argument exceeds the allowed length of %zu bytes", cmd_max_len);
  352. return ZSTR_EMPTY_ALLOC();
  353. }
  354. cmd = zend_string_safe_alloc(4, l, 2, 0); /* worst case */
  355. #ifdef PHP_WIN32
  356. ZSTR_VAL(cmd)[y++] = '"';
  357. #else
  358. ZSTR_VAL(cmd)[y++] = '\'';
  359. #endif
  360. for (x = 0; x < l; x++) {
  361. int mb_len = php_mblen(str + x, (l - x));
  362. /* skip non-valid multibyte characters */
  363. if (mb_len < 0) {
  364. continue;
  365. } else if (mb_len > 1) {
  366. memcpy(ZSTR_VAL(cmd) + y, str + x, mb_len);
  367. y += mb_len;
  368. x += mb_len - 1;
  369. continue;
  370. }
  371. switch (str[x]) {
  372. #ifdef PHP_WIN32
  373. case '"':
  374. case '%':
  375. case '!':
  376. ZSTR_VAL(cmd)[y++] = ' ';
  377. break;
  378. #else
  379. case '\'':
  380. ZSTR_VAL(cmd)[y++] = '\'';
  381. ZSTR_VAL(cmd)[y++] = '\\';
  382. ZSTR_VAL(cmd)[y++] = '\'';
  383. #endif
  384. ZEND_FALLTHROUGH;
  385. default:
  386. ZSTR_VAL(cmd)[y++] = str[x];
  387. }
  388. }
  389. #ifdef PHP_WIN32
  390. if (y > 0 && '\\' == ZSTR_VAL(cmd)[y - 1]) {
  391. int k = 0, n = y - 1;
  392. for (; n >= 0 && '\\' == ZSTR_VAL(cmd)[n]; n--, k++);
  393. if (k % 2) {
  394. ZSTR_VAL(cmd)[y++] = '\\';
  395. }
  396. }
  397. ZSTR_VAL(cmd)[y++] = '"';
  398. #else
  399. ZSTR_VAL(cmd)[y++] = '\'';
  400. #endif
  401. ZSTR_VAL(cmd)[y] = '\0';
  402. if (y > cmd_max_len + 1) {
  403. php_error_docref(NULL, E_ERROR, "Escaped argument exceeds the allowed length of %zu bytes", cmd_max_len);
  404. zend_string_release_ex(cmd, 0);
  405. return ZSTR_EMPTY_ALLOC();
  406. }
  407. if ((estimate - y) > 4096) {
  408. /* realloc if the estimate was way overill
  409. * Arbitrary cutoff point of 4096 */
  410. cmd = zend_string_truncate(cmd, y, 0);
  411. }
  412. ZSTR_LEN(cmd) = y;
  413. return cmd;
  414. }
  415. /* }}} */
  416. /* {{{ Escape shell metacharacters */
  417. PHP_FUNCTION(escapeshellcmd)
  418. {
  419. char *command;
  420. size_t command_len;
  421. ZEND_PARSE_PARAMETERS_START(1, 1)
  422. Z_PARAM_STRING(command, command_len)
  423. ZEND_PARSE_PARAMETERS_END();
  424. if (command_len) {
  425. if (command_len != strlen(command)) {
  426. zend_argument_value_error(1, "must not contain any null bytes");
  427. RETURN_THROWS();
  428. }
  429. RETVAL_STR(php_escape_shell_cmd(command));
  430. } else {
  431. RETVAL_EMPTY_STRING();
  432. }
  433. }
  434. /* }}} */
  435. /* {{{ Quote and escape an argument for use in a shell command */
  436. PHP_FUNCTION(escapeshellarg)
  437. {
  438. char *argument;
  439. size_t argument_len;
  440. ZEND_PARSE_PARAMETERS_START(1, 1)
  441. Z_PARAM_STRING(argument, argument_len)
  442. ZEND_PARSE_PARAMETERS_END();
  443. if (argument_len != strlen(argument)) {
  444. zend_argument_value_error(1, "must not contain any null bytes");
  445. RETURN_THROWS();
  446. }
  447. RETVAL_STR(php_escape_shell_arg(argument));
  448. }
  449. /* }}} */
  450. /* {{{ Execute command via shell and return complete output as string */
  451. PHP_FUNCTION(shell_exec)
  452. {
  453. FILE *in;
  454. char *command;
  455. size_t command_len;
  456. zend_string *ret;
  457. php_stream *stream;
  458. ZEND_PARSE_PARAMETERS_START(1, 1)
  459. Z_PARAM_STRING(command, command_len)
  460. ZEND_PARSE_PARAMETERS_END();
  461. if (!command_len) {
  462. zend_argument_value_error(1, "cannot be empty");
  463. RETURN_THROWS();
  464. }
  465. if (strlen(command) != command_len) {
  466. zend_argument_value_error(1, "must not contain any null bytes");
  467. RETURN_THROWS();
  468. }
  469. #ifdef PHP_WIN32
  470. if ((in=VCWD_POPEN(command, "rt"))==NULL) {
  471. #else
  472. if ((in=VCWD_POPEN(command, "r"))==NULL) {
  473. #endif
  474. php_error_docref(NULL, E_WARNING, "Unable to execute '%s'", command);
  475. RETURN_FALSE;
  476. }
  477. stream = php_stream_fopen_from_pipe(in, "rb");
  478. ret = php_stream_copy_to_mem(stream, PHP_STREAM_COPY_ALL, 0);
  479. php_stream_close(stream);
  480. if (ret && ZSTR_LEN(ret) > 0) {
  481. RETVAL_STR(ret);
  482. }
  483. }
  484. /* }}} */
  485. #ifdef HAVE_NICE
  486. /* {{{ Change the priority of the current process */
  487. PHP_FUNCTION(proc_nice)
  488. {
  489. zend_long pri;
  490. ZEND_PARSE_PARAMETERS_START(1, 1)
  491. Z_PARAM_LONG(pri)
  492. ZEND_PARSE_PARAMETERS_END();
  493. errno = 0;
  494. php_ignore_value(nice(pri));
  495. if (errno) {
  496. #ifdef PHP_WIN32
  497. char *err = php_win_err();
  498. php_error_docref(NULL, E_WARNING, "%s", err);
  499. php_win_err_free(err);
  500. #else
  501. php_error_docref(NULL, E_WARNING, "Only a super user may attempt to increase the priority of a process");
  502. #endif
  503. RETURN_FALSE;
  504. }
  505. RETURN_TRUE;
  506. }
  507. /* }}} */
  508. #endif