-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathphp_execdir.c
924 lines (811 loc) · 25 KB
/
php_execdir.c
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
/*
+----------------------------------------------------------------------+
| mod_execdir v1 |
+----------------------------------------------------------------------+
| Copyright 2022. JoungKyun.Kim All rights reserved. |
+----------------------------------------------------------------------+
| This source file is subject to version 3.0 of the PHP license, |
| 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/3_0.txt. |
| 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: JoungKyun.Kim <http://oops.org> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include "php.h"
#include "php_ini.h"
#include "SAPI.h"
#include "ext/standard/exec.h"
#include "ext/standard/info.h"
#include "php_execdir.h"
char * get_jailed_shell_cmd (char *);
#ifdef PHP_CAN_SUPPORT_PROC_OPEN
static HashTable execdir_submodules;
#endif
// If you declare any globals in php_exdcdir.h uncomment this:
ZEND_DECLARE_MODULE_GLOBALS (execdir)
/* {{{ execdir_list[]
*
* system function list to apply exec_dir
*/
static const struct execdir_overload_func execdir_list[] = {
{ "exec", "exec_re", "exec_orig" },
{ "system", "system_re", "system_orig" },
{ "passthru", "passthru_re", "passthru_orig" },
{ "pcntl_exec", "pcntl_exec_re", "pcntl_exec_orig" },
{ "shell_exec", "shell_exec_re", "shell_exec_orig" },
{ "popen", "popen_re", "popen_orig" },
#ifdef PHP_CAN_SUPPORT_PROC_OPEN
{ "proc_open", "proc_open_re", "proc_open_orig" },
{ "proc_close", "proc_close_re", "proc_close_orig" },
{ "proc_terminate", "proc_terminate_re", "proc_terminate_orig" },
{ "proc_get_status", "proc_get_status_re", "proc_get_status_orig" },
#endif
{ NULL, NULL, NULL }
};
/* }}} */
/* {{{ execdir_functions[]
*/
#include "execdir_arginfo.h"
#if PHP_VERSION_ID >= 50300
const
#endif
zend_function_entry execdir_functions[] = {
PHP_FE (exec_re, arginfo_exec_re)
PHP_FE (system_re, arginfo_system_re)
PHP_FE (passthru_re, arginfo_passthru_re)
PHP_FE (shell_exec_re, arginfo_shell_exec_re)
PHP_FE (popen_re, arginfo_popen_re)
PHP_FE (pcntl_exec_re, arginfo_pcntl_exec_re)
PHP_FE (jailed_shellcmd, arginfo_jailed_shellcmd_re)
#ifdef PHP_CAN_SUPPORT_PROC_OPEN
PHP_FE (proc_open_re, arginfo_proc_open)
PHP_FE (proc_close_re, arginfo_proc_close)
PHP_FE (proc_terminate_re, arginfo_proc_terminate)
PHP_FE (proc_get_status_re, arginfo_proc_get_status)
#endif
#if PHP_VERSION_ID >= 60000
#ifdef PHP_EXECDIR_COMPAT
PHP_FE (exec_orig, arginfo_exec_re)
PHP_FE (system_orig, arginfo_system_re)
PHP_FE (passthru_orig, arginfo_passthru_re)
PHP_FE (shell_exec_orig, arginfo_shell_exec_re)
PHP_FE (popen_orig, arginfo_popen_re)
PHP_FE (pcntl_exec_orig, arginfo_pcntl_exec_re)
#ifdef PHP_CAN_SUPPORT_PROC_OPEN
PHP_FE (proc_open_orig, arginfo_proc_open)
PHP_FE (proc_close_orig, arginfo_proc_close)
PHP_FE (proc_terminate_orig, arginfo_proc_terminate)
PHP_FE (proc_get_status_orig, arginfo_proc_get_status)
#endif
#endif
#endif
{NULL, NULL, NULL}
};
/* }}} */
#if PHP_VERSION_ID < 60000
/* {{{ execdir_hook_functions[]
*/
#if PHP_VERSION_ID >= 50300
const
#endif
zend_function_entry execdir_hook_functions[] = {
PHP_FALIAS (exec, exec_re, arginfo_exec_re)
PHP_FALIAS (system, system_re, arginfo_system_re)
PHP_FALIAS (passthru, passthru_re, arginfo_passthru_re)
PHP_FALIAS (shell_exec, shell_exec_re, arginfo_shell_exec_re)
PHP_FALIAS (popen, popen_re, arginfo_popen_re)
PHP_FALIAS (pcntl_exec, pcntl_exec_re, arginfo_pcntl_exec_re)
#ifdef PHP_CAN_SUPPORT_PROC_OPEN
PHP_FALIAS (proc_open, proc_open_re, arginfo_proc_open)
PHP_FALIAS (proc_close, proc_close_re, arginfo_proc_close)
PHP_FALIAS (proc_terminate, proc_terminate_re, arginfo_proc_terminate)
PHP_FALIAS (proc_get_status, proc_get_status_re, arginfo_proc_get_status)
#endif
{NULL, NULL, NULL}
};
/* }}} */
#endif
/* {{{ execdir_module_entry
*/
zend_module_entry execdir_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"execdir",
execdir_functions,
PHP_MINIT (execdir),
PHP_MSHUTDOWN (execdir),
PHP_RINIT (execdir),
PHP_RSHUTDOWN (execdir),
PHP_MINFO (execdir),
#if ZEND_MODULE_API_NO >= 20010901
EXECDIR_EXT_VERSION, /* Replace with version number for your extension */
#endif
STANDARD_MODULE_PROPERTIES
};
/* }}} */
/* {{{ ini_entries[]
*/
PHP_INI_BEGIN ()
STD_PHP_INI_ENTRY ("exec_dir", PHP_EXECDIR, PHP_INI_SYSTEM, OnUpdateString, exec_dir, zend_execdir_globals, execdir_globals)
PHP_INI_END ()
/* }}} */
#ifdef COMPILE_DL_EXECDIR
#ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
#endif
ZEND_GET_MODULE (execdir)
#endif
/* {{{ +-- void php_execdir_init_globals (zend_execdir_globals * execdir_globals)
*/
void php_execdir_init_globals (zend_execdir_globals * execdir_globals) {
memset (execdir_globals, 0, sizeof (zend_execdir_globals));
}
/* }}} */
/*{{{ +-- static int safe_hook_execdir (void)
*/
static int safe_hook_execdir (void) {
const struct execdir_overload_func *p;
#if PHP_VERSION_ID >= 60000
zend_function * func, * nfunc;
#ifdef PHP_EXECDIR_COMPAT
zend_function * ofunc;
#endif
#endif
#if PHP_VERSION_ID < 80000
TSRMLS_FETCH ();
#endif
p = &(execdir_list[0]);
while ( p->orig_func != NULL ) {
if ( ! execdir_hash_exists (CG (function_table), p->orig_func) ) {
p++;
continue;
}
#if PHP_VERSION_ID < 60000
#ifdef PHP_EXECDIR_COMPAT
/*
* Rename original function gets postfile '_orig'
*/
zend_hash_find (CG (function_table), p->orig_func, strlen(p->prog_func) + 1, (void **) &func);
zend_hash_add (
CG (function_table), p->ovld_func, strlen(p->prog_func) + 1,
func, sizeof (zend_function), NULL
);
#endif
/*
* Remove original function
*/
zend_hash_del (CG (function_table), p->orig_func, strlen(p->orig_func) + 1);
#else
if ( (func = zend_hash_str_find_ptr (CG (function_table), EXEC_STRING (p->orig_func))) != NULL ) {
nfunc = zend_hash_str_find_ptr (CG (function_table), EXEC_STRING (p->ovld_func));
#ifdef PHP_EXECDIR_COMPAT
ofunc = zend_hash_str_find_ptr (CG (function_table), EXEC_STRING (p->save_func));
if ( ofunc->internal_function.handler != func->internal_function.handler )
ofunc->internal_function.handler = func->internal_function.handler;
#endif
if ( func->internal_function.handler != nfunc->internal_function.handler )
func->internal_function.handler = nfunc->internal_function.handler;
}
#endif /* end of PHP_VERSION_ID < 60000 */
p++;
}
#if PHP_VERSION_ID < 60000
zend_register_functions (NULL, execdir_hook_functions, NULL, MODULE_PERSISTENT TSRMLS_CC);
#endif
return 0;
}
/* }}} */
/* {{{ +-- static void php_exec_ex (INTERNAL_FUNCTION_PARAMETERS, int mode)
*/
static void php_exec_ex (INTERNAL_FUNCTION_PARAMETERS, int mode) {
char * cmd, * jcmd;
#if PHP_VERSION_ID < 60000
int cmd_len;
#else
size_t cmd_len;
#endif
zval * ret_code = NULL, * ret_array = NULL;
int ret;
if ( mode ) {
if ( zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC, "s|z/", &cmd, &cmd_len, &ret_code) == FAILURE ) {
RETURN_FALSE;
}
} else {
if ( zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC, "s|z/z/", &cmd, &cmd_len, &ret_array, &ret_code) == FAILURE ) {
RETURN_FALSE;
}
}
if ( ! cmd_len ) {
php_error_docref (NULL TSRMLS_CC, E_WARNING, "Cannot execute a blank command");
RETURN_FALSE;
}
if ( strlen (cmd) != cmd_len ) {
php_error_docref (NULL TSRMLS_CC, E_WARNING, "NULL byte detected. Possible attack");
RETURN_FALSE;
}
jcmd = get_jailed_shell_cmd (cmd);
if ( ! ret_array ) {
ret = php_exec (mode, jcmd, NULL, return_value TSRMLS_CC);
} else {
if ( Z_TYPE_P (ret_array) != IS_ARRAY ) {
zval_dtor (ret_array);
array_init (ret_array);
}
ret = php_exec (2, jcmd, ret_array, return_value TSRMLS_CC);
}
efree (jcmd);
if ( ret_code ) {
zval_dtor (ret_code);
ZVAL_LONG (ret_code, ret);
}
}
/* }}} */
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION (execdir)
{
ZEND_INIT_MODULE_GLOBALS (execdir, php_execdir_init_globals, NULL);
// get suhosin (https://suhosin.org/)
if ( execdir_hash_exists (EG (ini_directives), "exec_dir") ) {
/* update */
zend_ini_entry_def * p = (zend_ini_entry_def *) &ini_entries;
while ( p->name ) {
zend_ini_entry * e;
#if PHP_VERSION_ID < 60000
if ( zend_hash_find (EG (ini_directives), p->name, p->name_length, (void **) &e) == FAILURE )
#else
if ( (e = zend_hash_str_find_ptr (EG (ini_directives), p->name, p->name_length)) == NULL )
#endif
{
zend_register_ini_entries (p, module_number TSRMLS_CC);
break;
}
e->modifiable = p->modifiable;
e->module_number = module_number;
e->on_modify = p->on_modify;
e->mh_arg1 = p->mh_arg1;
e->mh_arg2 = p->mh_arg2;
e->mh_arg3 = p->mh_arg3;
#if PHP_VERSION_ID < 60000
e->on_modify (e, e->value, e->value_length, e->mh_arg1, e->mh_arg2, e->mh_arg3, ZEND_INI_STAGE_STARTUP TSRMLS_CC);
#else
e->on_modify (e, e->value, e->mh_arg1, e->mh_arg2, e->mh_arg3, ZEND_INI_STAGE_STARTUP);
#endif
p++;
}
} else {
/* not registered yet, then simply use the API */
REGISTER_INI_ENTRIES ();
}
#ifdef PHP_CAN_SUPPORT_PROC_OPEN
zend_hash_init (&execdir_submodules, 0, NULL, NULL, 1);
if ( PHP_MINIT(proc_open_re)(INIT_FUNC_ARGS_PASSTHRU) == SUCCESS) {
zend_hash_str_add_empty_element (&execdir_submodules, "proc_open_re", strlen ("proc_open_re"));
}
#endif
// On cli mode, jailed_shell_cmd does nothing to do.
//if ( strcmp (sapi_module.name, "cli") != 0 )
safe_hook_execdir ();
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION (execdir)
{
UNREGISTER_INI_ENTRIES ();
#ifdef PHP_CAN_SUPPORT_PROC_OPEN
zend_hash_destroy (&execdir_submodules);
#endif
return SUCCESS;
}
/* }}} */
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION (execdir)
{
return SUCCESS;
}
/* }}} */
/* {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION (execdir)
{
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION (execdir)
{
php_info_print_table_start ();
php_info_print_table_colspan_header (2, "execdir extension support");
php_info_print_table_row (2, "Summary", "jailed system function");
php_info_print_table_row (2, "URL", "http://github.com/OOPS-ORG-PHP/mod_execdir");
php_info_print_table_row (2, "Build version", EXECDIR_EXT_VERSION);
php_info_print_table_row (2, "Default Exec DIR", PHP_EXECDIR);
php_info_print_table_end ();
DISPLAY_INI_ENTRIES ();
}
/* }}} */
/* {{{ PHP_FUNCTION (string) exec_re (string command [, array &output [, int &return_var]])
*/
PHP_FUNCTION (exec_re)
{
php_exec_ex (INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
}
/* }}} */
/* {{{ PHP_FUNCTION (string) system_re (string command [, int &return_var])
*/
PHP_FUNCTION (system_re)
{
php_exec_ex (INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
}
/* }}} */
/* {{{ PHP_FUNCTION (void) passthru_re (string command [, int &return_var])
*/
PHP_FUNCTION (passthru_re)
{
php_exec_ex (INTERNAL_FUNCTION_PARAM_PASSTHRU, 3);
}
/* }}} */
/* {{{ PHP_FUNCTION (string) shell_exec_re (string command)
* Execute command via shell and return complete output as string
* from php 5.6.22 && php 7.0.7
*/
PHP_FUNCTION (shell_exec_re)
{
FILE * in;
char * command, * jcommand;
#if PHP_VERSION_ID < 60000
size_t total_readbytes;
int command_len;
char * ret;
#else
size_t command_len;
zend_string * ret;
#endif
php_stream * stream;
if ( zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC, "s", &command, &command_len) == FAILURE ) {
return;
}
#if PHP_VERSION_ID < 50400
if ( PG (safe_mode) ) {
php_error_docref (NULL TSRMLS_CC, E_WARNING, "Cannot execute using backquotes in Safe Mode");
RETURN_FALSE;
}
#endif
if ( ! command_len ) {
php_error_docref (NULL TSRMLS_CC, E_WARNING, "Cannot execute a blank command");
RETURN_FALSE;
}
if ( strlen (command) != command_len ) {
php_error_docref (NULL TSRMLS_CC, E_WARNING, "NULL byte detected. Possible attack");
RETURN_FALSE;
}
jcommand = get_jailed_shell_cmd (command);
#ifdef PHP_WIN32
if ( (in = VCWD_POPEN (jcommand, "rt")) == NULL ) {
#else
if ( (in = VCWD_POPEN (jcommand, "r")) == NULL ) {
#endif
php_error_docref (NULL TSRMLS_CC, E_WARNING, "Unable to execute '%s'", jcommand);
efree (jcommand);
RETURN_FALSE;
}
efree (jcommand);
stream = php_stream_fopen_from_pipe (in, "rb");
#if PHP_VERSION_ID < 60000
total_readbytes = php_stream_copy_to_mem (stream, &ret, PHP_STREAM_COPY_ALL, 0);
#else
ret = php_stream_copy_to_mem (stream, PHP_STREAM_COPY_ALL, 0);
#endif
php_stream_close (stream);
#if PHP_VERSION_ID < 60000
if ( total_readbytes > 0 ) {
RETVAL_STRINGL (ret, (int) total_readbytes, 0);
}
#else
if ( ret && ZSTR_LEN (ret) > 0 ) {
RETVAL_STR (ret);
}
#endif
}
/* }}} */
/* {{{ PHP_FUNCTION (resource) popen (string command, string mode)
* Execute a command and open either a read or a write pipe to it
* From php 5.6.22 && php 7.0.7
*/
PHP_FUNCTION (popen_re)
{
char * command, * mode, * jcommand;
#if PHP_VERSION_ID < 60000
int command_len, mode_len;
#else
size_t command_len, mode_len;
#endif
FILE * fp;
php_stream * stream;
char * posix_mode;
#if PHP_VERSION_ID < 50400
if ( zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC, "ss", &command, &command_len, &mode, &mode_len) == FAILURE ) {
#else
if ( zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC, "ps", &command, &command_len, &mode, &mode_len) == FAILURE ) {
#endif
return;
}
if ( ! command_len ) {
php_error_docref (NULL TSRMLS_CC, E_WARNING, "Cannot execute a blank command");
RETURN_FALSE;
}
if ( strlen (command) != command_len ) {
php_error_docref (NULL TSRMLS_CC, E_WARNING, "NULL byte detected. Possible attack");
RETURN_FALSE;
}
posix_mode = estrndup (mode, mode_len);
#ifndef PHP_WIN32
{
char * z = memchr (posix_mode, 'b', mode_len);
if ( z ) {
memmove (z, z + 1, mode_len - (z - posix_mode));
}
}
#endif
#if PHP_VERSION_ID < 50400
{
char *b, *buf = 0, *tmp;
if ( PG (safe_mode) ) {
b = strchr (command, ' ');
if ( ! b ) {
b = strrchr (command, '/');
} else {
char * c;
c = command;
while ( (*b != '/') && (b != c) ) {
b--;
}
if ( b == c ) {
b = NULL;
}
}
if ( b ) {
spprintf (&buf, 0, "%s%s", PG (safe_mode_exec_dir), b);
} else {
spprintf (&buf, 0, "%s/%s", PG (safe_mode_exec_dir), command);
}
tmp = php_escape_shell_cmd (buf);
fp = VCWD_POPEN (tmp, posix_mode);
efree (tmp);
if ( ! fp ) {
php_error_docref2 (NULL TSRMLS_CC, buf, posix_mode, E_WARNING, "%s", strerror (errno));
efree (posix_mode);
efree (buf);
RETURN_FALSE;
}
efree (buf);
} else {
jcommand = get_jailed_shell_cmd (command);
fp = VCWD_POPEN (jcommand, posix_mode);
if ( ! fp ) {
php_error_docref2 (NULL TSRMLS_CC, command, posix_mode, E_WARNING, "%s", strerror (errno));
efree (posix_mode);
RETURN_FALSE;
}
}
}
#else
jcommand = get_jailed_shell_cmd (command);
fp = VCWD_POPEN (jcommand, posix_mode);
efree (jcommand);
if ( ! fp ) {
php_error_docref2 (NULL TSRMLS_CC, command, posix_mode, E_WARNING, "%s", strerror (errno));
efree (posix_mode);
RETURN_FALSE;
}
#endif
stream = php_stream_fopen_from_pipe (fp, mode);
if ( stream == NULL ) {
php_error_docref2 (NULL TSRMLS_CC, command, mode, E_WARNING, "%s", strerror (errno));
RETVAL_FALSE;
} else {
php_stream_to_zval (stream, return_value);
}
efree (posix_mode);
}
/* }}} */
/* {{{ PHP_FUNCTION (bool) pcntl_exec_re (string $path [, array args [, array envs]])
* Executes specified program in current process space as defined by exec(2)
*/
PHP_FUNCTION (pcntl_exec_re)
{
#if PHP_VERSION_ID >= 60000
/* {{{ for PHP 7 */
zval * args = NULL, * envs = NULL;
zval * element;
HashTable * args_hash, *envs_hash;
int argc = 0, argi = 0;
int envc = 0, envi = 0;
char ** argv = NULL, ** envp = NULL;
char ** current_arg, ** pair;
size_t pair_length;
zend_string * key;
char * path, * jpath;
size_t path_len;
zend_ulong key_num;
if ( strcmp (sapi_module.name, "cli") != 0 &&
strncmp (sapi_module.name, "cgi", 3) != 0 &&
strncmp (sapi_module.name, "fpm", 3) != 0 ) {
php_error_docref (NULL, E_WARNING, "pcntl_exec_re can only work on CLI/CGI SAPI.");
RETURN_FALSE;
}
if ( zend_parse_parameters (ZEND_NUM_ARGS (), "p|aa", &path, &path_len, &args, &envs) == FAILURE ) {
#if PHP_VERSION_ID >= 80000
RETURN_THROWS ();
#else
return;
#endif
}
if ( ZEND_NUM_ARGS () > 1 ) {
/* Build argument list */
#if PHP_VERSION_ID >= 80000
SEPARATE_ARRAY(args);
#endif
args_hash = Z_ARRVAL_P (args);
argc = zend_hash_num_elements (args_hash);
argv = safe_emalloc ((argc + 2), sizeof (char *), 0);
*argv = path;
current_arg = argv + 1;
ZEND_HASH_FOREACH_VAL (args_hash, element) {
if ( argi >= argc ) break;
#if PHP_VERSION_ID >= 80000
if ( ! try_convert_to_string (element) ) {
efree (argv);
RETURN_THROWS ();
}
#else
convert_to_string_ex (element);
#endif
*current_arg = Z_STRVAL_P (element);
argi++;
current_arg++;
} ZEND_HASH_FOREACH_END ();
*(current_arg) = NULL;
} else {
argv = emalloc (2 * sizeof (char *));
#if PHP_VERSION_ID >= 80000
argv[0] = path;
argv[1] = NULL;
#else
*argv = path;
*(argv + 1) = NULL;
#endif
}
jpath = get_jailed_shell_cmd(path);
if ( ZEND_NUM_ARGS () == 3 ) {
/* Build environment pair list */
envs_hash = Z_ARRVAL_P (envs);
envc = zend_hash_num_elements (envs_hash);
pair = envp = safe_emalloc ((envc + 1), sizeof (char *), 0);
ZEND_HASH_FOREACH_KEY_VAL (envs_hash, key_num, key, element) {
if ( envi >= envc ) break;
if ( ! key ) {
key = zend_long_to_str (key_num);
} else {
zend_string_addref (key);
}
#if PHP_VERSION_ID >= 80000
if ( ! try_convert_to_string (element) ) {
zend_string_release (key);
efree (argv);
efree (envp);
RETURN_THROWS ();
}
#else
convert_to_string_ex (element);
#endif
/* Length of element + equal sign + length of key + null */
ZEND_ASSERT(Z_STRLEN_P(element) < SIZE_MAX && ZSTR_LEN(key) < SIZE_MAX);
*pair = safe_emalloc(Z_STRLEN_P(element) + 1, sizeof(char), ZSTR_LEN(key) + 1);
pair_length = Z_STRLEN_P (element) + ZSTR_LEN (key) + 2;
strlcpy (*pair, ZSTR_VAL (key), ZSTR_LEN (key) + 1);
strlcat (*pair, "=", pair_length);
strlcat (*pair, Z_STRVAL_P (element), pair_length);
/* Cleanup */
execdir_string_release (key);
envi++;
pair++;
} ZEND_HASH_FOREACH_END ();
*(pair) = NULL;
if ( execve (jpath, argv, envp) == -1 ) {
//PCNTL_G (last_error) = errno;
php_error_docref (NULL, E_WARNING, "Error has occurred: (errno %d) %s", errno, strerror (errno));
}
/* Cleanup */
for ( pair = envp; *pair != NULL; pair++ ) efree (*pair);
efree (envp);
} else {
if ( execv (jpath, argv) == -1 ) {
//PCNTL_G (last_error) = errno;
php_error_docref (NULL, E_WARNING, "Error has occurred: (errno %d) %s", errno, strerror (errno));
}
}
/* }}} */
#else
/* {{{ before PHP 7 */
zval * args = NULL, * envs = NULL;
zval ** element;
HashTable * args_hash, * envs_hash;
int argc = 0, argi = 0;
int envc = 0, envi = 0;
int return_val = 0;
char ** argv = NULL, ** envp = NULL;
char ** current_arg, ** pair;
size_t pair_length;
char * key;
uint key_length;
char * path, * jpath = NULL;
int path_len;
ulong key_num;
#if PHP_VERSION_ID < 50400
if ( zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC, "s|aa", &path, &path_len, &args, &envs) == FAILURE )
#else
if ( zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC, "p|aa", &path, &path_len, &args, &envs) == FAILURE )
#endif
{
return;
}
#if PHP_VERSION_ID < 50400
if ( strlen (path) != path_len ) {
php_error_docref (NULL TSRMLS_CC, E_WARNING, "NULL byte detected. Possible attack");
RETURN_FALSE;
}
#endif
if ( ZEND_NUM_ARGS () > 1 ) {
/* Build argument list */
args_hash = HASH_OF (args);
argc = zend_hash_num_elements (args_hash);
argv = safe_emalloc ((argc + 2), sizeof (char *), 0);
*argv = path;
for ( zend_hash_internal_pointer_reset (args_hash), current_arg = argv + 1;
(argi < argc && (zend_hash_get_current_data (args_hash, (void **) &element) == SUCCESS));
(argi++, current_arg++, zend_hash_move_forward (args_hash)) ) {
convert_to_string_ex (element);
*current_arg = Z_STRVAL_PP (element);
}
*(current_arg) = NULL;
} else {
argv = emalloc (2 * sizeof (char *));
*argv = path;
*(argv+1) = NULL;
}
jpath = get_jailed_shell_cmd (path);
if ( ZEND_NUM_ARGS () == 3 ) {
/* Build environment pair list */
envs_hash = HASH_OF (envs);
envc = zend_hash_num_elements (envs_hash);
envp = safe_emalloc ((envc + 1), sizeof (char *), 0);
for ( zend_hash_internal_pointer_reset (envs_hash), pair = envp;
(envi < envc && (zend_hash_get_current_data (envs_hash, (void **) &element) == SUCCESS));
(envi++, pair++, zend_hash_move_forward (envs_hash)) ) {
switch (return_val = zend_hash_get_current_key_ex (envs_hash, &key, &key_length, &key_num, 0, NULL)) {
case HASH_KEY_IS_LONG:
key = emalloc (101);
snprintf (key, 100, "%ld", key_num);
key_length = strlen (key);
break;
#if PHP_VERSION_ID < 50500
case HASH_KEY_NON_EXISTANT:
#else
case HASH_KEY_NON_EXISTENT:
#endif
pair--;
continue;
}
convert_to_string_ex (element);
/* Length of element + equal sign + length of key + null */
pair_length = Z_STRLEN_PP (element) + key_length + 2;
if ( pair_length >= SIZE_MAX ) {
#if PHP_VERSION_ID < 50200
php_error_docref(NULL TSRMLS_CC, E_WARNING, "pcntl_exec(): The element lenth and ke length are greater than %ld", SIZE_MAX);
#else
php_error_docref(NULL TSRMLS_CC, E_WARNING, "The element lenth and ke length are greater than %ld", SIZE_MAX);
#endif
}
*pair = emalloc (pair_length);
strlcpy (*pair, key, key_length);
strlcat (*pair, "=", pair_length);
strlcat (*pair, Z_STRVAL_PP (element), pair_length);
/* Cleanup */
if ( return_val == HASH_KEY_IS_LONG ) efree (key);
}
*(pair) = NULL;
if ( execve (jpath, argv, envp) == -1 ) {
//PCNTL_G (last_error) = errno;
#if PHP_VERSION_ID < 50200
php_error_docref (NULL TSRMLS_CC, E_WARNING, "pcntl_exec(): Error has occurred: (errno %d) %s", errno, strerror (errno));
#else
php_error_docref (NULL TSRMLS_CC, E_WARNING, "Error has occurred: (errno %d) %s", errno, strerror (errno));
#endif
}
/* Cleanup */
for ( pair = envp; *pair != NULL; pair++ ) efree (*pair);
efree (envp);
} else {
if ( execv (jpath, argv) == -1 ) {
//PCNTL_G(last_error) = errno;
#if PHP_VERSION_ID < 50200
php_error_docref (NULL TSRMLS_CC, E_WARNING, "pcntl_exec(): Error has occurred: (errno %d) %s", errno, strerror (errno));
#else
php_error_docref (NULL TSRMLS_CC, E_WARNING, "Error has occurred: (errno %d) %s", errno, strerror (errno));
#endif
}
}
/* }}} */
#endif
efree (jpath);
efree (argv);
RETURN_FALSE;
}
/* }}} */
/* {{{ PHP_FUNCTION (string) jailed_shellcmd (string $path)
*/
PHP_FUNCTION (jailed_shellcmd)
{
char * cmd;
char * jcmd;
size_t cmdlen = 0;
if ( zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC, "s", &cmd, &cmdlen) == FAILURE ) {
return;
}
if ( ! cmdlen )
RETURN_EMPTY_STRING ();
if ( strlen (cmd) != cmdlen ) {
php_error_docref (NULL TSRMLS_CC, E_WARNING, "NULL byte detected. Possible attack");
RETURN_EMPTY_STRING ();
}
jcmd = get_jailed_shell_cmd (cmd);
RETVAL_EXECDIR_STRING (jcmd, strlen (jcmd));
efree (jcmd);
}
/* }}} */
/* PHP_EXECDIR_COMPAT functions (*_orig)
* This functions are only used PHP 7.4 and after
*/
#if PHP_VERSION_ID >= 60000
#ifdef PHP_EXECDIR_COMPAT
PHP_FUNCTION (exec_orig) { RETURN_NULL (); }
PHP_FUNCTION (system_orig) { RETURN_NULL (); }
PHP_FUNCTION (passthru_orig) { RETURN_NULL (); }
PHP_FUNCTION (shell_exec_orig) { RETURN_NULL (); }
PHP_FUNCTION (popen_orig) { RETURN_NULL (); }
PHP_FUNCTION (pcntl_exec_orig) { RETURN_NULL (); }
#ifdef PHP_CAN_SUPPORT_PROC_OPEN
PHP_FUNCTION (proc_open_orig) { RETURN_NULL (); }
PHP_FUNCTION (proc_get_status_orig) { RETURN_NULL (); }
PHP_FUNCTION (proc_close_orig) { RETURN_NULL (); }
PHP_FUNCTION (proc_terminate_orig) { RETURN_NULL (); }
#endif
#endif
#endif
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/