php-src/ext/standard/exec.c

526 lines
11 KiB
C
Raw Normal View History

/*
+----------------------------------------------------------------------+
2001-12-11 15:32:16 +00:00
| PHP Version 4 |
+----------------------------------------------------------------------+
2002-12-31 16:08:15 +00:00
| Copyright (c) 1997-2003 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 2.02 of the PHP license, |
1999-07-16 13:13:16 +00:00
| that is bundled with this package in the file LICENSE, and is |
| available at through the world-wide-web at |
| http://www.php.net/license/2_02.txt. |
1999-07-16 13:13:16 +00:00
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Rasmus Lerdorf |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#include <stdio.h>
#include "php.h"
#include <ctype.h>
#include "php_string.h"
#include "safe_mode.h"
#include "ext/standard/head.h"
#include "ext/standard/file.h"
#include "exec.h"
#include "php_globals.h"
#include "SAPI.h"
#if HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
#if HAVE_SIGNAL_H
#include <signal.h>
#endif
2002-04-02 16:46:33 +00:00
#if HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#if HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#if HAVE_FCNTL_H
#include <fcntl.h>
#endif
#if HAVE_NICE && HAVE_UNISTD_H
#include <unistd.h>
#endif
/* {{{ php_Exec
* If type==0, only last line of output is returned (exec)
* If type==1, all lines will be printed and last lined returned (system)
* If type==2, all lines will be saved to given array (exec with &$array)
* If type==3, output will be printed binary, no lines will be saved or returned (passthru)
*
*/
2001-07-30 09:16:46 +00:00
int php_Exec(int type, char *cmd, pval *array, pval *return_value TSRMLS_DC)
{
FILE *fp;
char *buf, *tmp=NULL;
int buflen = 0;
int t, l, output=1;
int overflow_limit, lcmd, ldir;
char *b, *c, *d=NULL;
2002-03-16 01:28:57 +00:00
php_stream *stream = NULL;
int pclose_return = 0;
#if PHP_SIGCHILD
void (*sig_handler)();
#endif
2001-11-04 02:09:55 +00:00
buf = (char *) emalloc(EXEC_INPUT_BUF);
buflen = EXEC_INPUT_BUF;
if (PG(safe_mode)) {
lcmd = strlen(cmd);
ldir = strlen(PG(safe_mode_exec_dir));
l = lcmd + ldir + 2;
overflow_limit = l;
c = strchr(cmd, ' ');
if (c) *c = '\0';
if (strstr(cmd, "..")) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "No '..' components allowed in path");
efree(buf);
return -1;
}
d = emalloc(l);
strcpy(d, PG(safe_mode_exec_dir));
overflow_limit -= ldir;
2001-01-08 16:39:39 +00:00
b = strrchr(cmd, PHP_DIR_SEPARATOR);
if (b) {
strcat(d, b);
overflow_limit -= strlen(b);
} else {
strcat(d, "/");
strcat(d, cmd);
overflow_limit-=(strlen(cmd)+1);
}
if (c) {
*c = ' ';
strncat(d, c, overflow_limit);
}
1999-12-18 04:01:20 +00:00
tmp = php_escape_shell_cmd(d);
efree(d);
d = tmp;
#if PHP_SIGCHILD
sig_handler = signal (SIGCHLD, SIG_DFL);
#endif
2000-06-26 17:12:38 +00:00
#ifdef PHP_WIN32
fp = VCWD_POPEN(d, "rb");
2000-06-26 17:12:38 +00:00
#else
fp = VCWD_POPEN(d, "r");
2000-06-26 17:12:38 +00:00
#endif
if (!fp) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to fork [%s]", d);
efree(d);
efree(buf);
#if PHP_SIGCHILD
signal (SIGCHLD, sig_handler);
#endif
return -1;
}
2001-11-04 02:09:55 +00:00
} else { /* not safe_mode */
#if PHP_SIGCHILD
sig_handler = signal (SIGCHLD, SIG_DFL);
#endif
2000-06-26 17:12:38 +00:00
#ifdef PHP_WIN32
fp = VCWD_POPEN(cmd, "rb");
2000-06-26 17:12:38 +00:00
#else
fp = VCWD_POPEN(cmd, "r");
2000-06-26 17:12:38 +00:00
#endif
if (!fp) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to fork [%s]", cmd);
efree(buf);
#if PHP_SIGCHILD
signal (SIGCHLD, sig_handler);
#endif
return -1;
}
}
buf[0] = '\0';
if (type==2) {
if (Z_TYPE_P(array) != IS_ARRAY) {
pval_destructor(array);
array_init(array);
}
}
/* we register the resource so that case of an aborted connection the
* fd gets pclosed
*/
2002-03-15 21:03:08 +00:00
stream = php_stream_fopen_from_pipe(fp, "rb");
if (type != 3) {
l=0;
while ( !feof(fp) || l != 0 ) {
l = 0;
/* Read a line or fill the buffer, whichever comes first */
do {
if ( buflen <= (l+1) ) {
buf = erealloc(buf, buflen + EXEC_INPUT_BUF);
buflen += EXEC_INPUT_BUF;
}
if ( fgets(&(buf[l]), buflen - l, fp) == NULL ) {
/* eof */
break;
}
l += strlen(&(buf[l]));
} while ( (l > 0) && (buf[l-1] != '\n') );
if ( feof(fp) && (l == 0) ) {
break;
}
if (type == 1) {
if (output) PUTS(buf);
sapi_flush(TSRMLS_C);
}
else if (type == 2) {
/* strip trailing whitespaces */
l = strlen(buf);
t = l;
while (l-- && isspace((int)buf[l]));
1999-06-01 16:41:56 +00:00
if (l < t) {
buf[l + 1] = '\0';
}
add_next_index_string(array, buf, 1);
}
}
/* strip trailing spaces */
l = strlen(buf);
t = l;
while (l && isspace((int)buf[l - 1])) {
l--;
}
if (l < t) buf[l] = '\0';
/* Return last line from the shell command */
if (PG(magic_quotes_runtime)) {
int len;
2001-08-06 03:50:52 +00:00
tmp = php_addslashes(buf, 0, &len, 0 TSRMLS_CC);
2001-08-11 17:03:37 +00:00
RETVAL_STRINGL(tmp, len, 0);
} else {
2001-08-11 17:03:37 +00:00
RETVAL_STRINGL(buf, l, 1);
}
} else {
size_t b;
while((b = php_stream_read(stream, buf, EXEC_INPUT_BUF)) > 0) {
if (output) {
PHPWRITE(buf, b);
}
}
}
pclose_return = php_stream_close(stream);
#if PHP_SIGCHILD
signal (SIGCHLD, sig_handler);
#endif
if (d) {
efree(d);
}
efree(buf);
return pclose_return;
}
/* }}} */
/* {{{ proto string exec(string command [, array output [, int return_value]])
Execute an external program */
1999-05-16 11:19:26 +00:00
PHP_FUNCTION(exec)
{
1999-12-20 01:23:15 +00:00
pval **arg1, **arg2, **arg3;
int arg_count = ZEND_NUM_ARGS();
int ret;
if (arg_count < 1 || arg_count > 3 || zend_get_parameters_ex(arg_count, &arg1, &arg2, &arg3) == FAILURE) {
WRONG_PARAM_COUNT;
}
if (!Z_STRLEN_PP(arg1)) {
PHP_EMPTY_EXEC_PARAM;
}
switch (arg_count) {
case 1:
2001-08-11 17:03:37 +00:00
ret = php_Exec(0, Z_STRVAL_PP(arg1), NULL, return_value TSRMLS_CC);
break;
case 2:
2001-08-11 17:03:37 +00:00
ret = php_Exec(2, Z_STRVAL_PP(arg1), *arg2, return_value TSRMLS_CC);
break;
case 3:
2001-08-11 17:03:37 +00:00
ret = php_Exec(2, Z_STRVAL_PP(arg1), *arg2, return_value TSRMLS_CC);
Z_TYPE_PP(arg3) = IS_LONG;
Z_LVAL_PP(arg3)=ret;
break;
}
}
2000-03-06 15:32:05 +00:00
/* }}} */
/* {{{ proto int system(string command [, int return_value])
Execute an external program and display output */
1999-05-16 11:19:26 +00:00
PHP_FUNCTION(system)
{
1999-12-20 01:23:15 +00:00
pval **arg1, **arg2;
int arg_count = ZEND_NUM_ARGS();
int ret;
if (arg_count < 1 || arg_count > 2 || zend_get_parameters_ex(arg_count, &arg1, &arg2) == FAILURE) {
WRONG_PARAM_COUNT;
}
if (!Z_STRLEN_PP(arg1)) {
PHP_EMPTY_EXEC_PARAM;
}
switch (arg_count) {
case 1:
2001-08-11 17:03:37 +00:00
ret = php_Exec(1, Z_STRVAL_PP(arg1), NULL, return_value TSRMLS_CC);
break;
case 2:
2001-08-11 17:03:37 +00:00
ret = php_Exec(1, Z_STRVAL_PP(arg1), NULL, return_value TSRMLS_CC);
Z_TYPE_PP(arg2) = IS_LONG;
Z_LVAL_PP(arg2)=ret;
break;
}
}
/* }}} */
2000-02-11 19:19:06 +00:00
/* {{{ proto void passthru(string command [, int return_value])
Execute an external program and display raw output */
1999-05-16 11:19:26 +00:00
PHP_FUNCTION(passthru)
{
1999-12-20 01:23:15 +00:00
pval **arg1, **arg2;
int arg_count = ZEND_NUM_ARGS();
int ret;
if (arg_count < 1 || arg_count > 2 || zend_get_parameters_ex(arg_count, &arg1, &arg2) == FAILURE) {
WRONG_PARAM_COUNT;
}
if (!Z_STRLEN_PP(arg1)) {
PHP_EMPTY_EXEC_PARAM;
}
switch (arg_count) {
case 1:
2001-07-30 09:16:46 +00:00
ret = php_Exec(3, Z_STRVAL_PP(arg1), NULL, return_value TSRMLS_CC);
break;
case 2:
2001-07-30 09:16:46 +00:00
ret = php_Exec(3, Z_STRVAL_PP(arg1), NULL, return_value TSRMLS_CC);
Z_TYPE_PP(arg2) = IS_LONG;
Z_LVAL_PP(arg2)=ret;
break;
}
}
/* }}} */
/* {{{ php_escape_shell_cmd
Escape all chars that could possibly be used to
break out of a shell command
This function emalloc's a string and returns the pointer.
Remember to efree it when done with it.
*NOT* safe for binary strings
*/
char *php_escape_shell_cmd(char *str) {
register int x, y, l;
char *cmd;
l = strlen(str);
cmd = emalloc(2 * l + 1);
for (x = 0, y = 0; x < l; x++) {
switch (str[x]) {
case '#': /* This is character-set independent */
case '&':
case ';':
case '`':
case '\'':
case '"':
case '|':
case '*':
case '?':
case '~':
case '<':
case '>':
case '^':
case '(':
case ')':
case '[':
case ']':
case '{':
case '}':
case '$':
case '\\':
case '\x0A': /* excluding these two */
case '\xFF':
cmd[y++] = '\\';
/* fall-through */
default:
cmd[y++] = str[x];
}
}
cmd[y] = '\0';
return cmd;
}
/* }}} */
/* {{{ php_escape_shell_arg
*/
char *php_escape_shell_arg(char *str) {
int x, y, l;
char *cmd;
y = 0;
l = strlen(str);
cmd = emalloc(4 * l + 3); /* worst case */
cmd[y++] = '\'';
for (x = 0; x < l; x++) {
switch (str[x]) {
case '\'':
cmd[y++] = '\'';
cmd[y++] = '\\';
cmd[y++] = '\'';
/* fall-through */
default:
cmd[y++] = str[x];
}
}
cmd[y++] = '\'';
cmd[y] = '\0';
return cmd;
}
/* }}} */
2000-05-23 23:13:02 +00:00
/* {{{ proto string escapeshellcmd(string command)
2000-02-24 08:07:29 +00:00
Escape shell metacharacters */
1999-05-16 11:19:26 +00:00
PHP_FUNCTION(escapeshellcmd)
{
1999-12-20 01:23:15 +00:00
pval **arg1;
1999-08-18 17:23:01 +00:00
char *cmd = NULL;
1999-12-20 01:23:15 +00:00
if (zend_get_parameters_ex(1, &arg1) == FAILURE) {
WRONG_PARAM_COUNT;
}
1999-08-18 17:23:01 +00:00
1999-12-20 01:23:15 +00:00
convert_to_string_ex(arg1);
if (Z_STRLEN_PP(arg1)) {
cmd = php_escape_shell_cmd(Z_STRVAL_PP(arg1));
1999-08-18 17:23:01 +00:00
RETVAL_STRING(cmd, 1);
efree(cmd);
}
}
/* }}} */
/* {{{ proto string escapeshellarg(string arg)
Quote and escape an argument for use in a shell command */
PHP_FUNCTION(escapeshellarg)
{
pval **arg1;
char *cmd = NULL;
if (zend_get_parameters_ex(1, &arg1) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(arg1);
if (Z_STRLEN_PP(arg1)) {
cmd = php_escape_shell_arg(Z_STRVAL_PP(arg1));
RETVAL_STRING(cmd, 1);
efree(cmd);
}
}
/* }}} */
/* {{{ proto string shell_exec(string cmd)
Use pclose() for FILE* that has been opened via popen() */
PHP_FUNCTION(shell_exec)
{
FILE *in;
2001-08-11 17:03:37 +00:00
int readbytes, total_readbytes=0, allocated_space;
1999-12-20 01:23:15 +00:00
pval **cmd;
char *ret;
2001-08-11 17:03:37 +00:00
if (ZEND_NUM_ARGS()!=1 || zend_get_parameters_ex(1, &cmd)==FAILURE) {
WRONG_PARAM_COUNT;
}
if (PG(safe_mode)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot execute using backquotes in Safe Mode");
RETURN_FALSE;
}
1999-12-20 01:23:15 +00:00
convert_to_string_ex(cmd);
2000-02-11 15:59:30 +00:00
#ifdef PHP_WIN32
2001-08-11 17:03:37 +00:00
if ((in=VCWD_POPEN(Z_STRVAL_PP(cmd), "rt"))==NULL) {
#else
2001-08-11 17:03:37 +00:00
if ((in=VCWD_POPEN(Z_STRVAL_PP(cmd), "r"))==NULL) {
#endif
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to execute '%s'", Z_STRVAL_PP(cmd));
2002-04-25 06:43:11 +00:00
RETURN_FALSE;
}
allocated_space = EXEC_INPUT_BUF;
ret = (char *) emalloc(allocated_space);
while (1) {
2001-08-11 17:03:37 +00:00
readbytes = fread(ret+total_readbytes, 1, EXEC_INPUT_BUF, in);
if (readbytes<=0) {
break;
}
total_readbytes += readbytes;
allocated_space = total_readbytes+EXEC_INPUT_BUF;
2001-08-11 17:03:37 +00:00
ret = (char *) erealloc(ret, allocated_space);
}
pclose(in);
RETVAL_STRINGL(ret, total_readbytes, 0);
Z_STRVAL_P(return_value)[total_readbytes] = '\0';
}
2000-05-23 23:13:02 +00:00
/* }}} */
#ifdef HAVE_NICE
/* {{{ proto bool proc_nice(int priority)
Change the priority of the current process */
PHP_FUNCTION(proc_nice)
{
long pri;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &pri) == FAILURE) {
RETURN_FALSE;
}
errno = 0;
nice(pri);
if (errno) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Only a super user may attempt to increase the priority of a process.");
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
#endif
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: sw=4 ts=4 fdm=marker
* vim<600: sw=4 ts=4
*/