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.

538 lines
12 KiB

27 years ago
27 years ago
18 years ago
24 years ago
19 years ago
23 years ago
25 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
22 years ago
22 years ago
22 years ago
19 years ago
27 years ago
27 years ago
19 years ago
19 years ago
18 years ago
19 years ago
19 years ago
19 years ago
19 years ago
18 years ago
19 years ago
19 years ago
26 years ago
27 years ago
19 years ago
27 years ago
19 years ago
19 years ago
27 years ago
19 years ago
19 years ago
19 years ago
23 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2009 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Rasmus Lerdorf <rasmus@php.net> |
  16. | Ilia Alshanetsky <iliaa@php.net> |
  17. +----------------------------------------------------------------------+
  18. */
  19. /* $Id$ */
  20. #include <stdio.h>
  21. #include "php.h"
  22. #include <ctype.h>
  23. #include "php_string.h"
  24. #include "safe_mode.h"
  25. #include "ext/standard/head.h"
  26. #include "ext/standard/file.h"
  27. #include "basic_functions.h"
  28. #include "exec.h"
  29. #include "php_globals.h"
  30. #include "SAPI.h"
  31. #if HAVE_SYS_WAIT_H
  32. #include <sys/wait.h>
  33. #endif
  34. #if HAVE_SIGNAL_H
  35. #include <signal.h>
  36. #endif
  37. #if HAVE_SYS_TYPES_H
  38. #include <sys/types.h>
  39. #endif
  40. #if HAVE_SYS_STAT_H
  41. #include <sys/stat.h>
  42. #endif
  43. #if HAVE_FCNTL_H
  44. #include <fcntl.h>
  45. #endif
  46. #if HAVE_NICE && HAVE_UNISTD_H
  47. #include <unistd.h>
  48. #endif
  49. /* {{{ php_exec
  50. * If type==0, only last line of output is returned (exec)
  51. * If type==1, all lines will be printed and last lined returned (system)
  52. * If type==2, all lines will be saved to given array (exec with &$array)
  53. * If type==3, output will be printed binary, no lines will be saved or returned (passthru)
  54. *
  55. */
  56. PHPAPI int php_exec(int type, char *cmd, zval *array, zval *return_value TSRMLS_DC)
  57. {
  58. FILE *fp;
  59. char *buf, *tmp=NULL;
  60. int l, pclose_return;
  61. char *cmd_p, *b, *c, *d=NULL;
  62. php_stream *stream;
  63. size_t buflen, bufl = 0;
  64. #if PHP_SIGCHILD
  65. void (*sig_handler)() = NULL;
  66. #endif
  67. if (PG(safe_mode)) {
  68. if ((c = strchr(cmd, ' '))) {
  69. *c = '\0';
  70. c++;
  71. }
  72. if (strstr(cmd, "..")) {
  73. php_error_docref(NULL TSRMLS_CC, E_WARNING, "No '..' components allowed in path");
  74. goto err;
  75. }
  76. b = strrchr(cmd, PHP_DIR_SEPARATOR);
  77. #ifdef PHP_WIN32
  78. if (b && *b == '\\' && b == cmd) {
  79. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid absolute path.");
  80. goto err;
  81. }
  82. #endif
  83. spprintf(&d, 0, "%s%s%s%s%s", PG(safe_mode_exec_dir), (b ? "" : "/"), (b ? b : cmd), (c ? " " : ""), (c ? c : ""));
  84. if (c) {
  85. *(c - 1) = ' ';
  86. }
  87. cmd_p = php_escape_shell_cmd(d);
  88. efree(d);
  89. d = cmd_p;
  90. } else {
  91. cmd_p = cmd;
  92. }
  93. #if PHP_SIGCHILD
  94. sig_handler = signal (SIGCHLD, SIG_DFL);
  95. #endif
  96. #ifdef PHP_WIN32
  97. fp = VCWD_POPEN(cmd_p, "rb");
  98. #else
  99. fp = VCWD_POPEN(cmd_p, "r");
  100. #endif
  101. if (!fp) {
  102. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to fork [%s]", cmd);
  103. goto err;
  104. }
  105. stream = php_stream_fopen_from_pipe(fp, "rb");
  106. buf = (char *) emalloc(EXEC_INPUT_BUF);
  107. buflen = EXEC_INPUT_BUF;
  108. if (type != 3) {
  109. b = buf;
  110. while (php_stream_get_line(stream, b, EXEC_INPUT_BUF, &bufl)) {
  111. /* no new line found, let's read some more */
  112. if (b[bufl - 1] != '\n' && !php_stream_eof(stream)) {
  113. if (buflen < (bufl + (b - buf) + EXEC_INPUT_BUF)) {
  114. bufl += b - buf;
  115. buflen = bufl + EXEC_INPUT_BUF;
  116. buf = erealloc(buf, buflen);
  117. b = buf + bufl;
  118. } else {
  119. b += bufl;
  120. }
  121. continue;
  122. } else if (b != buf) {
  123. bufl += b - buf;
  124. }
  125. if (type == 1) {
  126. PHPWRITE(buf, bufl);
  127. if (OG(ob_nesting_level) < 1) {
  128. sapi_flush(TSRMLS_C);
  129. }
  130. } else if (type == 2) {
  131. /* strip trailing whitespaces */
  132. l = bufl;
  133. while (l-- && isspace(((unsigned char *)buf)[l]));
  134. if (l != (int)(bufl - 1)) {
  135. bufl = l + 1;
  136. buf[bufl] = '\0';
  137. }
  138. add_next_index_stringl(array, buf, bufl, 1);
  139. }
  140. b = buf;
  141. }
  142. if (bufl) {
  143. /* strip trailing whitespaces if we have not done so already */
  144. if (type != 2) {
  145. l = bufl;
  146. while (l-- && isspace(((unsigned char *)buf)[l]));
  147. if (l != (int)(bufl - 1)) {
  148. bufl = l + 1;
  149. buf[bufl] = '\0';
  150. }
  151. }
  152. /* Return last line from the shell command */
  153. if (PG(magic_quotes_runtime)) {
  154. int len;
  155. tmp = php_addslashes(buf, bufl, &len, 0 TSRMLS_CC);
  156. RETVAL_STRINGL(tmp, len, 0);
  157. } else {
  158. RETVAL_STRINGL(buf, bufl, 1);
  159. }
  160. } else { /* should return NULL, but for BC we return "" */
  161. RETVAL_EMPTY_STRING();
  162. }
  163. } else {
  164. while((bufl = php_stream_read(stream, buf, EXEC_INPUT_BUF)) > 0) {
  165. PHPWRITE(buf, bufl);
  166. }
  167. }
  168. pclose_return = php_stream_close(stream);
  169. efree(buf);
  170. done:
  171. #if PHP_SIGCHILD
  172. if (sig_handler) {
  173. signal(SIGCHLD, sig_handler);
  174. }
  175. #endif
  176. if (d) {
  177. efree(d);
  178. }
  179. return pclose_return;
  180. err:
  181. pclose_return = -1;
  182. goto done;
  183. }
  184. /* }}} */
  185. static void php_exec_ex(INTERNAL_FUNCTION_PARAMETERS, int mode) /* {{{ */
  186. {
  187. char *cmd;
  188. int cmd_len;
  189. zval *ret_code=NULL, *ret_array=NULL;
  190. int ret;
  191. if (mode) {
  192. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|z/", &cmd, &cmd_len, &ret_code) == FAILURE) {
  193. RETURN_FALSE;
  194. }
  195. } else {
  196. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|z/z/", &cmd, &cmd_len, &ret_array, &ret_code) == FAILURE) {
  197. RETURN_FALSE;
  198. }
  199. }
  200. if (!cmd_len) {
  201. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot execute a blank command");
  202. RETURN_FALSE;
  203. }
  204. if (!ret_array) {
  205. ret = php_exec(mode, cmd, NULL, return_value TSRMLS_CC);
  206. } else {
  207. if (Z_TYPE_P(ret_array) != IS_ARRAY) {
  208. zval_dtor(ret_array);
  209. array_init(ret_array);
  210. }
  211. ret = php_exec(2, cmd, ret_array, return_value TSRMLS_CC);
  212. }
  213. if (ret_code) {
  214. zval_dtor(ret_code);
  215. ZVAL_LONG(ret_code, ret);
  216. }
  217. }
  218. /* }}} */
  219. /* {{{ proto string exec(string command [, array &output [, int &return_value]])
  220. Execute an external program */
  221. PHP_FUNCTION(exec)
  222. {
  223. php_exec_ex(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  224. }
  225. /* }}} */
  226. /* {{{ proto int system(string command [, int &return_value])
  227. Execute an external program and display output */
  228. PHP_FUNCTION(system)
  229. {
  230. php_exec_ex(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  231. }
  232. /* }}} */
  233. /* {{{ proto void passthru(string command [, int &return_value])
  234. Execute an external program and display raw output */
  235. PHP_FUNCTION(passthru)
  236. {
  237. php_exec_ex(INTERNAL_FUNCTION_PARAM_PASSTHRU, 3);
  238. }
  239. /* }}} */
  240. /* {{{ php_escape_shell_cmd
  241. Escape all chars that could possibly be used to
  242. break out of a shell command
  243. This function emalloc's a string and returns the pointer.
  244. Remember to efree it when done with it.
  245. *NOT* safe for binary strings
  246. */
  247. PHPAPI char *php_escape_shell_cmd(char *str)
  248. {
  249. register int x, y, l = strlen(str);
  250. char *cmd;
  251. char *p = NULL;
  252. size_t estimate = (2 * l) + 1;
  253. TSRMLS_FETCH();
  254. cmd = safe_emalloc(2, l, 1);
  255. for (x = 0, y = 0; x < l; x++) {
  256. int mb_len = php_mblen(str + x, (l - x));
  257. /* skip non-valid multibyte characters */
  258. if (mb_len < 0) {
  259. continue;
  260. } else if (mb_len > 1) {
  261. memcpy(cmd + y, str + x, mb_len);
  262. y += mb_len;
  263. x += mb_len - 1;
  264. continue;
  265. }
  266. switch (str[x]) {
  267. #ifndef PHP_WIN32
  268. case '"':
  269. case '\'':
  270. if (!p && (p = memchr(str + x + 1, str[x], l - x - 1))) {
  271. /* noop */
  272. } else if (p && *p == str[x]) {
  273. p = NULL;
  274. } else {
  275. cmd[y++] = '\\';
  276. }
  277. cmd[y++] = str[x];
  278. break;
  279. #else
  280. /* % is Windows specific for enviromental variables, ^%PATH% will
  281. output PATH whil ^%PATH^% not. escapeshellcmd will escape all %.
  282. */
  283. case '%':
  284. case '"':
  285. case '\'':
  286. #endif
  287. case '#': /* This is character-set independent */
  288. case '&':
  289. case ';':
  290. case '`':
  291. case '|':
  292. case '*':
  293. case '?':
  294. case '~':
  295. case '<':
  296. case '>':
  297. case '^':
  298. case '(':
  299. case ')':
  300. case '[':
  301. case ']':
  302. case '{':
  303. case '}':
  304. case '$':
  305. case '\\':
  306. case '\x0A': /* excluding these two */
  307. case '\xFF':
  308. #ifdef PHP_WIN32
  309. cmd[y++] = '^';
  310. #else
  311. cmd[y++] = '\\';
  312. #endif
  313. /* fall-through */
  314. default:
  315. cmd[y++] = str[x];
  316. }
  317. }
  318. cmd[y] = '\0';
  319. if ((estimate - y) > 4096) {
  320. /* realloc if the estimate was way overill
  321. * Arbitrary cutoff point of 4096 */
  322. cmd = erealloc(cmd, y + 1);
  323. }
  324. return cmd;
  325. }
  326. /* }}} */
  327. /* {{{ php_escape_shell_arg
  328. */
  329. PHPAPI char *php_escape_shell_arg(char *str)
  330. {
  331. int x, y = 0, l = strlen(str);
  332. char *cmd;
  333. size_t estimate = (4 * l) + 3;
  334. TSRMLS_FETCH();
  335. cmd = safe_emalloc(4, l, 3); /* worst case */
  336. #ifdef PHP_WIN32
  337. cmd[y++] = '"';
  338. #else
  339. cmd[y++] = '\'';
  340. #endif
  341. for (x = 0; x < l; x++) {
  342. int mb_len = php_mblen(str + x, (l - x));
  343. /* skip non-valid multibyte characters */
  344. if (mb_len < 0) {
  345. continue;
  346. } else if (mb_len > 1) {
  347. memcpy(cmd + y, str + x, mb_len);
  348. y += mb_len;
  349. x += mb_len - 1;
  350. continue;
  351. }
  352. switch (str[x]) {
  353. #ifdef PHP_WIN32
  354. case '"':
  355. case '%':
  356. cmd[y++] = ' ';
  357. break;
  358. #else
  359. case '\'':
  360. cmd[y++] = '\'';
  361. cmd[y++] = '\\';
  362. cmd[y++] = '\'';
  363. #endif
  364. /* fall-through */
  365. default:
  366. cmd[y++] = str[x];
  367. }
  368. }
  369. #ifdef PHP_WIN32
  370. cmd[y++] = '"';
  371. #else
  372. cmd[y++] = '\'';
  373. #endif
  374. cmd[y] = '\0';
  375. if ((estimate - y) > 4096) {
  376. /* realloc if the estimate was way overill
  377. * Arbitrary cutoff point of 4096 */
  378. cmd = erealloc(cmd, y + 1);
  379. }
  380. return cmd;
  381. }
  382. /* }}} */
  383. /* {{{ proto string escapeshellcmd(string command)
  384. Escape shell metacharacters */
  385. PHP_FUNCTION(escapeshellcmd)
  386. {
  387. char *command;
  388. int command_len;
  389. char *cmd = NULL;
  390. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &command, &command_len) == FAILURE) {
  391. return;
  392. }
  393. if (command_len) {
  394. cmd = php_escape_shell_cmd(command);
  395. RETVAL_STRING(cmd, 0);
  396. } else {
  397. RETVAL_EMPTY_STRING();
  398. }
  399. }
  400. /* }}} */
  401. /* {{{ proto string escapeshellarg(string arg)
  402. Quote and escape an argument for use in a shell command */
  403. PHP_FUNCTION(escapeshellarg)
  404. {
  405. char *argument;
  406. int argument_len;
  407. char *cmd = NULL;
  408. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &argument, &argument_len) == FAILURE) {
  409. return;
  410. }
  411. if (argument) {
  412. cmd = php_escape_shell_arg(argument);
  413. RETVAL_STRING(cmd, 0);
  414. }
  415. }
  416. /* }}} */
  417. /* {{{ proto string shell_exec(string cmd)
  418. Execute command via shell and return complete output as string */
  419. PHP_FUNCTION(shell_exec)
  420. {
  421. FILE *in;
  422. size_t total_readbytes;
  423. char *command;
  424. int command_len;
  425. char *ret;
  426. php_stream *stream;
  427. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &command, &command_len) == FAILURE) {
  428. return;
  429. }
  430. if (PG(safe_mode)) {
  431. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot execute using backquotes in Safe Mode");
  432. RETURN_FALSE;
  433. }
  434. #ifdef PHP_WIN32
  435. if ((in=VCWD_POPEN(command, "rt"))==NULL) {
  436. #else
  437. if ((in=VCWD_POPEN(command, "r"))==NULL) {
  438. #endif
  439. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to execute '%s'", command);
  440. RETURN_FALSE;
  441. }
  442. stream = php_stream_fopen_from_pipe(in, "rb");
  443. total_readbytes = php_stream_copy_to_mem(stream, &ret, PHP_STREAM_COPY_ALL, 0);
  444. php_stream_close(stream);
  445. if (total_readbytes > 0) {
  446. RETVAL_STRINGL(ret, total_readbytes, 0);
  447. }
  448. }
  449. /* }}} */
  450. #ifdef HAVE_NICE
  451. /* {{{ proto bool proc_nice(int priority)
  452. Change the priority of the current process */
  453. PHP_FUNCTION(proc_nice)
  454. {
  455. long pri;
  456. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &pri) == FAILURE) {
  457. RETURN_FALSE;
  458. }
  459. errno = 0;
  460. nice(pri);
  461. if (errno) {
  462. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Only a super user may attempt to increase the priority of a process");
  463. RETURN_FALSE;
  464. }
  465. RETURN_TRUE;
  466. }
  467. /* }}} */
  468. #endif
  469. /*
  470. * Local variables:
  471. * tab-width: 4
  472. * c-basic-offset: 4
  473. * End:
  474. * vim600: sw=4 ts=4 fdm=marker
  475. * vim<600: sw=4 ts=4
  476. */