1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
|
/*
+----------------------------------------------------------------------+
| PHP HTML Embedded Scripting Language Version 3.0 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2000 PHP Development Team (See Credits file) |
+----------------------------------------------------------------------+
| This program is free software; you can redistribute it and/or modify |
| it under the terms of one of the following licenses: |
| |
| A) the GNU General Public License as published by the Free Software |
| Foundation; either version 2 of the License, or (at your option) |
| any later version. |
| |
| B) the PHP License as published by the PHP Development Team and |
| included in the distribution in the file: LICENSE |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
| |
| You should have received a copy of both licenses referred to here. |
| If you did not, or have any questions about PHP licensing, please |
| contact core@php.net. |
+----------------------------------------------------------------------+
| Author: Rasmus Lerdorf <rasmus@php.net> |
+----------------------------------------------------------------------+
*/
/* $Id: exec.c,v 1.92 2000/03/20 14:23:12 andrei Exp $ */
#include <stdio.h>
#include "php.h"
#include <ctype.h>
#include "internal_functions.h"
#include "php3_string.h"
#include "safe_mode.h"
#include "head.h"
#include "exec.h"
#ifdef HAVE_SYS_WAIT_H
# include <sys/wait.h>
#endif
/*
* 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)
*
*/
static int _Exec(int type, char *cmd, pval *array, pval *return_value)
{
FILE *fp;
char *buf, *tmp=NULL;
int buflen=0;
int t, l, ret, output=1;
int overflow_limit, lcmd, ldir;
char *b, *c, *d=NULL;
TLS_VARS;
buf = (char*) emalloc(EXEC_INPUT_BUF);
if (!buf) {
php3_error(E_WARNING, "Unable to emalloc %d bytes", EXEC_INPUT_BUF);
return -1;
}
buflen = EXEC_INPUT_BUF;
#ifdef WIN32
(void)AllocConsole(); /* We don't care if this fails. */
#endif
if (php3_ini.safe_mode) {
lcmd = strlen(cmd);
ldir = strlen(php3_ini.safe_mode_exec_dir);
l = lcmd + ldir + 2;
overflow_limit = l;
c = strchr(cmd, ' ');
if (c) *c = '\0';
if (strstr(cmd, "..")) {
php3_error(E_WARNING, "No '..' components allowed in path");
efree(buf);
return -1;
}
d = emalloc(l);
strcpy(d, php3_ini.safe_mode_exec_dir);
overflow_limit -= ldir;
b = strrchr(cmd, '/');
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);
}
tmp = _php3_escapeshellcmd(d);
efree(d);
d = tmp;
#if WIN32|WINNT
fp = popen(d, "rb");
#else
fp = popen(d, "r");
#endif
if (!fp) {
php3_error(E_WARNING, "Unable to fork [%s]", d);
efree(d);
efree(buf);
return -1;
}
} else { /* not safe_mode */
#if WIN32|WINNT
fp = popen(cmd, "rb");
#else
fp = popen(cmd, "r");
#endif
if (!fp) {
php3_error(E_WARNING, "Unable to fork [%s]", cmd);
efree(buf);
return -1;
}
}
buf[0] = '\0';
if (type == 1 || type == 3) {
output=php3_header();
}
if (type==2) {
if (array->type != IS_ARRAY) {
pval_destructor(array _INLINE_TLS);
array_init(array);
}
}
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);
if ( buf == NULL ) {
php3_error(E_WARNING, "Unable to erealloc %d bytes",
buflen + EXEC_INPUT_BUF);
return -1;
}
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);
#if APACHE
# if MODULE_MAGIC_NUMBER > 19970110
if (output) rflush(GLOBAL(php3_rqst));
# else
if (output) bflush(GLOBAL(php3_rqst)->connection->client);
# endif
#endif
#if CGI_BINARY
fflush(stdout);
#endif
#if FHTTPD
/* fhttpd doesn't flush */
#endif
#if USE_SAPI
GLOBAL(sapi_rqst)->flush(GLOBAL(sapi_rqst)->scid);
#endif
}
else if (type == 2) {
pval tmp;
/* strip trailing whitespaces */
l = strlen(buf);
t = l;
while (l-- && isspace((int)buf[l]));
if (l < t) buf[l + 1] = '\0';
tmp.value.str.len = strlen(buf);
tmp.value.str.val = estrndup(buf,tmp.value.str.len);
tmp.type = IS_STRING;
_php3_hash_next_index_insert(array->value.ht,(void *) &tmp, sizeof(pval), NULL);
}
}
/* strip trailing spaces */
l = strlen(buf);
t = l;
while (l && isspace((int)buf[--l]));
if (l < t) buf[l + 1] = '\0';
/* Return last line from the shell command */
if(php3_ini.magic_quotes_runtime) {
int len;
tmp = _php3_addslashes(buf, 0, &len, 0);
RETVAL_STRINGL(tmp,len,0);
} else {
RETVAL_STRINGL(buf,l+1,1);
}
} else {
int b, i;
while ((b = fread(buf, 1, buflen, fp)) > 0) {
for (i = 0; i < b; i++)
if (output) PUTC(buf[i]);
}
}
ret = pclose(fp);
#ifdef HAVE_SYS_WAIT_H
if (WIFEXITED(ret)) {
ret = WEXITSTATUS(ret);
}
#endif
if (d) efree(d);
efree(buf);
return ret;
}
/* {{{ proto int exec(string command [, array output [, int return_value]])
Execute an external program */
void php3_exec(INTERNAL_FUNCTION_PARAMETERS)
{
pval *arg1, *arg2, *arg3;
int arg_count = ARG_COUNT(ht);
int ret;
if (arg_count > 3 || getParameters(ht, arg_count, &arg1, &arg2, &arg3) == FAILURE) {
WRONG_PARAM_COUNT;
}
switch (arg_count) {
case 1:
ret = _Exec(0, arg1->value.str.val, NULL, return_value);
break;
case 2:
if (!ParameterPassedByReference(ht,2)) {
php3_error(E_WARNING,"Array argument to exec() not passed by reference");
}
ret = _Exec(2, arg1->value.str.val, arg2, return_value);
break;
case 3:
if (!ParameterPassedByReference(ht,2)) {
php3_error(E_WARNING,"Array argument to exec() not passed by reference");
}
if (!ParameterPassedByReference(ht,3)) {
php3_error(E_WARNING,"return_status argument to exec() not passed by reference");
}
ret = _Exec(2, arg1->value.str.val, arg2, return_value);
arg3->type = IS_LONG;
arg3->value.lval=ret;
break;
}
}
/* }}} */
/* {{{ proto int system(string command [, int return_value])
Execute an external program and display output */
void php3_system(INTERNAL_FUNCTION_PARAMETERS)
{
pval *arg1, *arg2;
int arg_count = ARG_COUNT(ht);
int ret;
if (arg_count > 2 || getParameters(ht, arg_count, &arg1, &arg2) == FAILURE) {
WRONG_PARAM_COUNT;
}
switch (arg_count) {
case 1:
ret = _Exec(1, arg1->value.str.val, NULL, return_value);
break;
case 2:
if (!ParameterPassedByReference(ht,2)) {
php3_error(E_WARNING,"return_status argument to system() not passed by reference");
}
ret = _Exec(1, arg1->value.str.val, NULL, return_value);
arg2->type = IS_LONG;
arg2->value.lval=ret;
break;
}
}
/* }}} */
/* {{{ proto void passthru(string command [, int return_value])
Execute an external program and display raw output */
void php3_passthru(INTERNAL_FUNCTION_PARAMETERS)
{
pval *arg1, *arg2;
int arg_count = ARG_COUNT(ht);
int ret;
if (arg_count > 2 || getParameters(ht, arg_count, &arg1, &arg2) == FAILURE) {
WRONG_PARAM_COUNT;
}
switch (arg_count) {
case 1:
ret = _Exec(3, arg1->value.str.val, NULL, return_value);
break;
case 2:
if (!ParameterPassedByReference(ht,2)) {
php3_error(E_WARNING,"return_status argument to system() not passed by reference");
}
ret = _Exec(3, arg1->value.str.val, NULL, return_value);
arg2->type = IS_LONG;
arg2->value.lval=ret;
break;
}
}
/* }}} */
static int php3_ind(char *s, char c)
{
register int x;
for (x = 0; s[x]; x++)
if (s[x] == c)
return x;
return -1;
}
/* 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 * _php3_escapeshellcmd(char *str) {
register int x, y, l;
char *cmd;
l = strlen(str);
cmd = emalloc(2 * l + 1);
strcpy(cmd, str);
for (x = 0; cmd[x]; x++) {
if (php3_ind("&;`'\"|*?~<>^()[]{}$\\\x0A\xFF", cmd[x]) != -1) {
for (y = l + 1; y > x; y--)
cmd[y] = cmd[y - 1];
l++; /* length has been increased */
cmd[x] = '\\';
x++; /* skip the character */
}
}
return cmd;
}
/* {{{ proto string escapeshellcmd(string command)
Escape shell metacharacters */
void php3_escapeshellcmd(INTERNAL_FUNCTION_PARAMETERS)
{
pval *arg1;
char *cmd;
TLS_VARS;
if (getParameters(ht, 1, &arg1) == FAILURE) {
WRONG_PARAM_COUNT;
}
cmd = _php3_escapeshellcmd(arg1->value.str.val);
RETVAL_STRING(cmd,1);
efree(cmd);
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
*/
|