-
Notifications
You must be signed in to change notification settings - Fork 7.8k
/
Copy pathcgi_main.c
2683 lines (2348 loc) · 73.3 KB
/
cgi_main.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
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.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 |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Rasmus Lerdorf <[email protected]> |
| Stig Bakken <[email protected]> |
| Zeev Suraski <[email protected]> |
| FastCGI: Ben Mansell <[email protected]> |
| Shane Caraveo <[email protected]> |
| Dmitry Stogov <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "php.h"
#include "php_globals.h"
#include "php_variables.h"
#include "php_ini_builder.h"
#include "zend_modules.h"
#include "SAPI.h"
#include <stdio.h>
#ifdef PHP_WIN32
# include "win32/time.h"
# include "win32/signal.h"
# include "win32/winutil.h"
# include <process.h>
#endif
#ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <signal.h>
#include <locale.h>
#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
#ifdef HAVE_SYS_WAIT_H
# include <sys/wait.h>
#endif
#include "zend.h"
#include "zend_extensions.h"
#include "php_ini.h"
#include "php_globals.h"
#include "php_main.h"
#include "fopen_wrappers.h"
#include "http_status_codes.h"
#include "ext/standard/php_standard.h"
#include "ext/standard/dl_arginfo.h"
#include "ext/standard/url.h"
#ifdef PHP_WIN32
# include <io.h>
# include <fcntl.h>
# include "win32/php_registry.h"
#endif
#ifdef __riscos__
# include <unixlib/local.h>
int __riscosify_control = __RISCOSIFY_STRICT_UNIX_SPECS;
#endif
#include "zend_compile.h"
#include "zend_execute.h"
#include "zend_highlight.h"
#include "php_getopt.h"
#include "fastcgi.h"
#include "cgi_main_arginfo.h"
#if defined(PHP_WIN32) && defined(HAVE_OPENSSL_EXT)
# include "openssl/applink.c"
#endif
#ifdef HAVE_VALGRIND
# include "valgrind/callgrind.h"
# ifdef HAVE_VALGRIND_CACHEGRIND_H
# include "valgrind/cachegrind.h"
# endif
#endif
#ifndef PHP_WIN32
/* XXX this will need to change later when threaded fastcgi is implemented. shane */
static struct sigaction act, old_term, old_quit, old_int;
#endif
static void (*php_php_import_environment_variables)(zval *array_ptr);
/* these globals used for forking children on unix systems */
/**
* Number of child processes that will get created to service requests
*/
static int children = 0;
/**
* Set to non-zero if we are the parent process
*/
static int parent = 1;
#ifndef PHP_WIN32
/* Did parent received exit signals SIG_TERM/SIG_INT/SIG_QUIT */
static volatile sig_atomic_t exit_signal = 0;
/* Is Parent waiting for children to exit */
static int parent_waiting = 0;
/**
* Process group
*/
static pid_t pgroup;
#endif
#define PHP_MODE_STANDARD 1
#define PHP_MODE_HIGHLIGHT 2
#define PHP_MODE_LINT 4
#define PHP_MODE_STRIP 5
static char *php_optarg = NULL;
static int php_optind = 1;
static zend_module_entry cgi_module_entry;
static const opt_struct OPTIONS[] = {
{'a', 0, "interactive"},
{'b', 1, "bindpath"},
{'C', 0, "no-chdir"},
{'c', 1, "php-ini"},
{'d', 1, "define"},
{'e', 0, "profile-info"},
{'f', 1, "file"},
{'h', 0, "help"},
{'i', 0, "info"},
{'l', 0, "syntax-check"},
{'m', 0, "modules"},
{'n', 0, "no-php-ini"},
{'q', 0, "no-header"},
{'s', 0, "syntax-highlight"},
{'s', 0, "syntax-highlighting"},
{'w', 0, "strip"},
{'?', 0, "usage"},/* help alias (both '?' and 'usage') */
{'v', 0, "version"},
{'z', 1, "zend-extension"},
{'T', 1, "timing"},
{'-', 0, NULL} /* end of args */
};
typedef struct _php_cgi_globals_struct {
HashTable user_config_cache;
char *redirect_status_env;
bool rfc2616_headers;
bool nph;
bool check_shebang_line;
bool fix_pathinfo;
bool force_redirect;
bool discard_path;
bool fcgi_logging;
#ifdef PHP_WIN32
bool impersonate;
#endif
} php_cgi_globals_struct;
/* {{{ user_config_cache
*
* Key for each cache entry is dirname(PATH_TRANSLATED).
*
* NOTE: Each cache entry config_hash contains the combination from all user ini files found in
* the path starting from doc_root through to dirname(PATH_TRANSLATED). There is no point
* storing per-file entries as it would not be possible to detect added / deleted entries
* between separate files.
*/
typedef struct _user_config_cache_entry {
time_t expires;
HashTable *user_config;
} user_config_cache_entry;
static void user_config_cache_entry_dtor(zval *el)
{
user_config_cache_entry *entry = (user_config_cache_entry *)Z_PTR_P(el);
zend_hash_destroy(entry->user_config);
free(entry->user_config);
free(entry);
}
/* }}} */
#ifdef ZTS
static int php_cgi_globals_id;
#define CGIG(v) ZEND_TSRMG(php_cgi_globals_id, php_cgi_globals_struct *, v)
#if defined(PHP_WIN32)
ZEND_TSRMLS_CACHE_DEFINE()
#endif
#else
static php_cgi_globals_struct php_cgi_globals;
#define CGIG(v) (php_cgi_globals.v)
#endif
#ifdef PHP_WIN32
#define TRANSLATE_SLASHES(path) \
{ \
char *tmp = path; \
while (*tmp) { \
if (*tmp == '\\') *tmp = '/'; \
tmp++; \
} \
}
#else
#define TRANSLATE_SLASHES(path)
#endif
#ifdef PHP_WIN32
#define WIN32_MAX_SPAWN_CHILDREN 64
HANDLE kid_cgi_ps[WIN32_MAX_SPAWN_CHILDREN];
int kids, cleaning_up = 0;
HANDLE job = NULL;
JOBOBJECT_EXTENDED_LIMIT_INFORMATION job_info = { 0 };
CRITICAL_SECTION cleanup_lock;
#endif
#ifndef HAVE_ATTRIBUTE_WEAK
static void fcgi_log(int type, const char *format, ...) {
va_list ap;
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
}
#endif
static int module_name_cmp(Bucket *f, Bucket *s)
{
return strcasecmp( ((zend_module_entry *)Z_PTR(f->val))->name,
((zend_module_entry *)Z_PTR(s->val))->name);
}
static void print_modules(void)
{
HashTable sorted_registry;
zend_module_entry *module;
zend_hash_init(&sorted_registry, 64, NULL, NULL, 1);
zend_hash_copy(&sorted_registry, &module_registry, NULL);
zend_hash_sort(&sorted_registry, module_name_cmp, 0);
ZEND_HASH_MAP_FOREACH_PTR(&sorted_registry, module) {
php_printf("%s\n", module->name);
} ZEND_HASH_FOREACH_END();
zend_hash_destroy(&sorted_registry);
}
static void print_extension_info(zend_extension *ext)
{
php_printf("%s\n", ext->name);
}
static int extension_name_cmp(const zend_llist_element **f, const zend_llist_element **s)
{
zend_extension *fe = (zend_extension*)(*f)->data;
zend_extension *se = (zend_extension*)(*s)->data;
return strcmp(fe->name, se->name);
}
static void print_extensions(void)
{
zend_llist sorted_exts;
zend_llist_copy(&sorted_exts, &zend_extensions);
sorted_exts.dtor = NULL;
zend_llist_sort(&sorted_exts, extension_name_cmp);
zend_llist_apply(&sorted_exts, (llist_apply_func_t) print_extension_info);
zend_llist_destroy(&sorted_exts);
}
#ifndef STDOUT_FILENO
#define STDOUT_FILENO 1
#endif
static inline size_t sapi_cgi_single_write(const char *str, size_t str_length)
{
#ifdef PHP_WRITE_STDOUT
int ret;
ret = write(STDOUT_FILENO, str, str_length);
if (ret <= 0) return 0;
return ret;
#else
size_t ret;
ret = fwrite(str, 1, MIN(str_length, 16384), stdout);
return ret;
#endif
}
static size_t sapi_cgi_ub_write(const char *str, size_t str_length)
{
const char *ptr = str;
size_t remaining = str_length;
size_t ret;
while (remaining > 0) {
ret = sapi_cgi_single_write(ptr, remaining);
if (!ret) {
php_handle_aborted_connection();
return str_length - remaining;
}
ptr += ret;
remaining -= ret;
}
return str_length;
}
static size_t sapi_fcgi_ub_write(const char *str, size_t str_length)
{
const char *ptr = str;
size_t remaining = str_length;
fcgi_request *request = (fcgi_request*) SG(server_context);
while (remaining > 0) {
int to_write = remaining > INT_MAX ? INT_MAX : (int)remaining;
int ret = fcgi_write(request, FCGI_STDOUT, ptr, to_write);
if (ret <= 0) {
php_handle_aborted_connection();
return str_length - remaining;
}
ptr += ret;
remaining -= ret;
}
return str_length;
}
static void sapi_cgi_flush(void *server_context)
{
if (fflush(stdout) == EOF) {
php_handle_aborted_connection();
}
}
static void sapi_fcgi_flush(void *server_context)
{
fcgi_request *request = (fcgi_request*) server_context;
if (!parent && request) {
sapi_send_headers();
if (!fcgi_flush(request, 0)) {
php_handle_aborted_connection();
}
}
}
#define SAPI_CGI_MAX_HEADER_LENGTH 1024
static int sapi_cgi_send_headers(sapi_headers_struct *sapi_headers)
{
sapi_header_struct *h;
zend_llist_position pos;
bool ignore_status = 0;
int response_status = SG(sapi_headers).http_response_code;
if (SG(request_info).no_headers == 1) {
return SAPI_HEADER_SENT_SUCCESSFULLY;
}
if (CGIG(nph) || SG(sapi_headers).http_response_code != 200)
{
int len;
bool has_status = 0;
char buf[SAPI_CGI_MAX_HEADER_LENGTH];
if (CGIG(rfc2616_headers) && SG(sapi_headers).http_status_line) {
char *s;
len = slprintf(buf, SAPI_CGI_MAX_HEADER_LENGTH, "%s", SG(sapi_headers).http_status_line);
if ((s = strchr(SG(sapi_headers).http_status_line, ' '))) {
response_status = atoi((s + 1));
}
if (len > SAPI_CGI_MAX_HEADER_LENGTH) {
len = SAPI_CGI_MAX_HEADER_LENGTH;
}
} else {
char *s;
if (SG(sapi_headers).http_status_line &&
(s = strchr(SG(sapi_headers).http_status_line, ' ')) != 0 &&
(s - SG(sapi_headers).http_status_line) >= 5 &&
strncasecmp(SG(sapi_headers).http_status_line, "HTTP/", 5) == 0
) {
len = slprintf(buf, sizeof(buf), "Status:%s", s);
response_status = atoi((s + 1));
} else {
h = (sapi_header_struct*)zend_llist_get_first_ex(&sapi_headers->headers, &pos);
while (h) {
if (h->header_len > sizeof("Status:")-1 &&
strncasecmp(h->header, "Status:", sizeof("Status:")-1) == 0
) {
has_status = 1;
break;
}
h = (sapi_header_struct*)zend_llist_get_next_ex(&sapi_headers->headers, &pos);
}
if (!has_status) {
http_response_status_code_pair *err = (http_response_status_code_pair*)http_status_map;
while (err->code != 0) {
if (err->code == SG(sapi_headers).http_response_code) {
break;
}
err++;
}
if (err->str) {
len = slprintf(buf, sizeof(buf), "Status: %d %s", SG(sapi_headers).http_response_code, err->str);
} else {
len = slprintf(buf, sizeof(buf), "Status: %d", SG(sapi_headers).http_response_code);
}
}
}
}
if (!has_status) {
PHPWRITE_H(buf, len);
PHPWRITE_H("\r\n", 2);
ignore_status = 1;
}
}
h = (sapi_header_struct*)zend_llist_get_first_ex(&sapi_headers->headers, &pos);
while (h) {
/* prevent CRLFCRLF */
if (h->header_len) {
if (h->header_len > sizeof("Status:")-1 &&
strncasecmp(h->header, "Status:", sizeof("Status:")-1) == 0
) {
if (!ignore_status) {
ignore_status = 1;
PHPWRITE_H(h->header, h->header_len);
PHPWRITE_H("\r\n", 2);
}
} else if (response_status == 304 && h->header_len > sizeof("Content-Type:")-1 &&
strncasecmp(h->header, "Content-Type:", sizeof("Content-Type:")-1) == 0
) {
h = (sapi_header_struct*)zend_llist_get_next_ex(&sapi_headers->headers, &pos);
continue;
} else {
PHPWRITE_H(h->header, h->header_len);
PHPWRITE_H("\r\n", 2);
}
}
h = (sapi_header_struct*)zend_llist_get_next_ex(&sapi_headers->headers, &pos);
}
PHPWRITE_H("\r\n", 2);
return SAPI_HEADER_SENT_SUCCESSFULLY;
}
#ifndef STDIN_FILENO
# define STDIN_FILENO 0
#endif
static size_t sapi_cgi_read_post(char *buffer, size_t count_bytes)
{
size_t read_bytes = 0;
int tmp_read_bytes;
size_t remaining_bytes;
assert(SG(request_info).content_length >= SG(read_post_bytes));
remaining_bytes = (size_t)(SG(request_info).content_length - SG(read_post_bytes));
count_bytes = MIN(count_bytes, remaining_bytes);
while (read_bytes < count_bytes) {
#ifdef PHP_WIN32
size_t diff = count_bytes - read_bytes;
unsigned int to_read = (diff > INT_MAX) ? INT_MAX : (unsigned int)diff;
tmp_read_bytes = _read(STDIN_FILENO, buffer + read_bytes, to_read);
#else
tmp_read_bytes = read(STDIN_FILENO, buffer + read_bytes, count_bytes - read_bytes);
#endif
if (tmp_read_bytes <= 0) {
break;
}
read_bytes += tmp_read_bytes;
}
return read_bytes;
}
static size_t sapi_fcgi_read_post(char *buffer, size_t count_bytes)
{
size_t read_bytes = 0;
int tmp_read_bytes;
fcgi_request *request = (fcgi_request*) SG(server_context);
size_t remaining = SG(request_info).content_length - SG(read_post_bytes);
if (remaining < count_bytes) {
count_bytes = remaining;
}
while (read_bytes < count_bytes) {
size_t diff = count_bytes - read_bytes;
int to_read = (diff > INT_MAX) ? INT_MAX : (int)diff;
tmp_read_bytes = fcgi_read(request, buffer + read_bytes, to_read);
if (tmp_read_bytes <= 0) {
break;
}
read_bytes += tmp_read_bytes;
}
return read_bytes;
}
#ifdef PHP_WIN32
/* The result needs to be freed! See sapi_getenv(). */
static char *cgi_getenv_win32(const char *name, size_t name_len)
{
char *ret = NULL;
wchar_t *keyw, *valw;
size_t size;
int rc;
keyw = php_win32_cp_conv_any_to_w(name, name_len, PHP_WIN32_CP_IGNORE_LEN_P);
if (!keyw) {
return NULL;
}
rc = _wgetenv_s(&size, NULL, 0, keyw);
if (rc || 0 == size) {
free(keyw);
return NULL;
}
valw = emalloc((size + 1) * sizeof(wchar_t));
rc = _wgetenv_s(&size, valw, size, keyw);
if (!rc) {
ret = php_win32_cp_w_to_any(valw);
}
free(keyw);
efree(valw);
return ret;
}
#endif
static char *sapi_cgi_getenv(const char *name, size_t name_len)
{
#ifndef PHP_WIN32
return getenv(name);
#else
return cgi_getenv_win32(name, name_len);
#endif
}
static char *sapi_fcgi_getenv(const char *name, size_t name_len)
{
/* when php is started by mod_fastcgi, no regular environment
* is provided to PHP. It is always sent to PHP at the start
* of a request. So we have to do our own lookup to get env
* vars. This could probably be faster somehow. */
fcgi_request *request = (fcgi_request*) SG(server_context);
char *ret = fcgi_getenv(request, name, (int)name_len);
#ifndef PHP_WIN32
if (ret) return ret;
/* if cgi, or fastcgi and not found in fcgi env
check the regular environment */
return getenv(name);
#else
if (ret) {
/* The functions outside here don't know, where does it come
from. They'll need to free the returned memory as it's
not necessary from the fcgi env. */
return strdup(ret);
}
/* if cgi, or fastcgi and not found in fcgi env
check the regular environment */
return cgi_getenv_win32(name, name_len);
#endif
}
static char *_sapi_cgi_putenv(char *name, size_t name_len, char *value)
{
#if !defined(HAVE_SETENV) || !defined(HAVE_UNSETENV)
size_t len;
char *buf;
#endif
#ifdef HAVE_SETENV
if (value) {
setenv(name, value, 1);
}
#endif
#ifdef HAVE_UNSETENV
if (!value) {
unsetenv(name);
}
#endif
#if !defined(HAVE_SETENV) || !defined(HAVE_UNSETENV)
/* if cgi, or fastcgi and not found in fcgi env
check the regular environment
this leaks, but it's only cgi anyway, we'll fix
it for 5.0
*/
len = name_len + (value ? strlen(value) : 0) + sizeof("=") + 2;
buf = (char *) malloc(len);
if (buf == NULL) {
return getenv(name);
}
#endif
#if !defined(HAVE_SETENV)
if (value) {
len = slprintf(buf, len - 1, "%s=%s", name, value);
putenv(buf);
}
#endif
#if !defined(HAVE_UNSETENV)
if (!value) {
len = slprintf(buf, len - 1, "%s=", name);
putenv(buf);
}
#endif
return getenv(name);
}
static char *sapi_cgi_read_cookies(void)
{
return getenv("HTTP_COOKIE");
}
static char *sapi_fcgi_read_cookies(void)
{
fcgi_request *request = (fcgi_request*) SG(server_context);
return FCGI_GETENV(request, "HTTP_COOKIE");
}
static void cgi_php_load_env_var(const char *var, unsigned int var_len, char *val, unsigned int val_len, void *arg)
{
zval *array_ptr = (zval *) arg;
int filter_arg = (Z_ARR_P(array_ptr) == Z_ARR(PG(http_globals)[TRACK_VARS_ENV])) ? PARSE_ENV : PARSE_SERVER;
size_t new_val_len;
if (sapi_module.input_filter(filter_arg, var, &val, strlen(val), &new_val_len)) {
php_register_variable_safe(var, val, new_val_len, array_ptr);
}
}
static void cgi_php_import_environment_variables(zval *array_ptr)
{
if (PG(variables_order) && (strchr(PG(variables_order),'E') || strchr(PG(variables_order),'e'))) {
if (Z_TYPE(PG(http_globals)[TRACK_VARS_ENV]) != IS_ARRAY) {
zend_is_auto_global(ZSTR_KNOWN(ZEND_STR_AUTOGLOBAL_ENV));
}
if (Z_TYPE(PG(http_globals)[TRACK_VARS_ENV]) == IS_ARRAY &&
Z_ARR_P(array_ptr) != Z_ARR(PG(http_globals)[TRACK_VARS_ENV])) {
zend_array_destroy(Z_ARR_P(array_ptr));
Z_ARR_P(array_ptr) = zend_array_dup(Z_ARR(PG(http_globals)[TRACK_VARS_ENV]));
return;
}
}
/* call php's original import as a catch-all */
php_php_import_environment_variables(array_ptr);
if (fcgi_is_fastcgi()) {
fcgi_request *request = (fcgi_request*) SG(server_context);
fcgi_loadenv(request, cgi_php_load_env_var, array_ptr);
}
}
static void sapi_cgi_register_variables(zval *track_vars_array)
{
size_t php_self_len;
char *php_self;
/* In CGI mode, we consider the environment to be a part of the server
* variables
*/
php_import_environment_variables(track_vars_array);
if (CGIG(fix_pathinfo)) {
char *script_name = SG(request_info).request_uri;
char *path_info;
int free_php_self;
ALLOCA_FLAG(use_heap)
if (fcgi_is_fastcgi()) {
fcgi_request *request = (fcgi_request*) SG(server_context);
path_info = FCGI_GETENV(request, "PATH_INFO");
} else {
path_info = getenv("PATH_INFO");
}
if (path_info) {
size_t path_info_len = strlen(path_info);
if (script_name) {
size_t script_name_len = strlen(script_name);
php_self_len = script_name_len + path_info_len;
php_self = do_alloca(php_self_len + 1, use_heap);
memcpy(php_self, script_name, script_name_len + 1);
memcpy(php_self + script_name_len, path_info, path_info_len + 1);
free_php_self = 1;
} else {
php_self = path_info;
php_self_len = path_info_len;
free_php_self = 0;
}
} else if (script_name) {
php_self = script_name;
php_self_len = strlen(script_name);
free_php_self = 0;
} else {
php_self = "";
php_self_len = 0;
free_php_self = 0;
}
/* Build the special-case PHP_SELF variable for the CGI version */
if (sapi_module.input_filter(PARSE_SERVER, "PHP_SELF", &php_self, php_self_len, &php_self_len)) {
php_register_variable_safe("PHP_SELF", php_self, php_self_len, track_vars_array);
}
if (free_php_self) {
free_alloca(php_self, use_heap);
}
} else {
php_self = SG(request_info).request_uri ? SG(request_info).request_uri : "";
php_self_len = strlen(php_self);
if (sapi_module.input_filter(PARSE_SERVER, "PHP_SELF", &php_self, php_self_len, &php_self_len)) {
php_register_variable_safe("PHP_SELF", php_self, php_self_len, track_vars_array);
}
}
}
static void sapi_cgi_log_message(const char *message, int syslog_type_int)
{
if (fcgi_is_fastcgi() && CGIG(fcgi_logging)) {
fcgi_request *request;
request = (fcgi_request*) SG(server_context);
if (request) {
int ret, len = (int)strlen(message);
char *buf = malloc(len+2);
memcpy(buf, message, len);
memcpy(buf + len, "\n", sizeof("\n"));
ret = fcgi_write(request, FCGI_STDERR, buf, (int)(len + 1));
free(buf);
if (ret < 0) {
php_handle_aborted_connection();
}
} else {
fprintf(stderr, "%s\n", message);
}
/* ignore return code */
} else {
fprintf(stderr, "%s\n", message);
}
}
/* {{{ php_cgi_ini_activate_user_config */
static void php_cgi_ini_activate_user_config(char *path, size_t path_len, const char *doc_root, size_t doc_root_len)
{
user_config_cache_entry *new_entry, *entry;
time_t request_time = (time_t)sapi_get_request_time();
/* Find cached config entry: If not found, create one */
if ((entry = zend_hash_str_find_ptr(&CGIG(user_config_cache), path, path_len)) == NULL) {
new_entry = pemalloc(sizeof(user_config_cache_entry), 1);
new_entry->expires = 0;
new_entry->user_config = (HashTable *) pemalloc(sizeof(HashTable), 1);
zend_hash_init(new_entry->user_config, 8, NULL, (dtor_func_t) config_zval_dtor, 1);
entry = zend_hash_str_update_ptr(&CGIG(user_config_cache), path, path_len, new_entry);
}
/* Check whether cache entry has expired and rescan if it is */
if (request_time > entry->expires) {
char *real_path = NULL;
char *s1, *s2;
size_t s_len;
/* Clear the expired config */
zend_hash_clean(entry->user_config);
if (!IS_ABSOLUTE_PATH(path, path_len)) {
size_t real_path_len;
real_path = tsrm_realpath(path, NULL);
if (real_path == NULL) {
return;
}
real_path_len = strlen(real_path);
path = real_path;
path_len = real_path_len;
}
if (path_len > doc_root_len) {
s1 = (char *) doc_root;
s2 = path;
s_len = doc_root_len;
} else {
s1 = path;
s2 = (char *) doc_root;
s_len = path_len;
}
/* we have to test if path is part of DOCUMENT_ROOT.
if it is inside the docroot, we scan the tree up to the docroot
to find more user.ini, if not we only scan the current path.
*/
#ifdef PHP_WIN32
if (strnicmp(s1, s2, s_len) == 0) {
#else
if (strncmp(s1, s2, s_len) == 0) {
#endif
char *ptr = s2 + doc_root_len;
#ifdef PHP_WIN32
while ((ptr = strpbrk(ptr, "\\/")) != NULL) {
#else
while ((ptr = strchr(ptr, DEFAULT_SLASH)) != NULL) {
#endif
*ptr = 0;
php_parse_user_ini_file(path, PG(user_ini_filename), entry->user_config);
*ptr = '/';
ptr++;
}
} else {
php_parse_user_ini_file(path, PG(user_ini_filename), entry->user_config);
}
if (real_path) {
efree(real_path);
}
entry->expires = request_time + PG(user_ini_cache_ttl);
}
/* Activate ini entries with values from the user config hash */
php_ini_activate_config(entry->user_config, PHP_INI_PERDIR, PHP_INI_STAGE_HTACCESS);
}
/* }}} */
static int sapi_cgi_activate(void)
{
/* PATH_TRANSLATED should be defined at this stage but better safe than sorry :) */
if (!SG(request_info).path_translated) {
return FAILURE;
}
if (php_ini_has_per_host_config()) {
char *server_name;
/* Activate per-host-system-configuration defined in php.ini and stored into configuration_hash during startup */
if (fcgi_is_fastcgi()) {
fcgi_request *request = (fcgi_request*) SG(server_context);
server_name = FCGI_GETENV(request, "SERVER_NAME");
} else {
server_name = getenv("SERVER_NAME");
}
/* SERVER_NAME should also be defined at this stage..but better check it anyway */
if (server_name) {
size_t server_name_len = strlen(server_name);
server_name = estrndup(server_name, server_name_len);
zend_str_tolower(server_name, server_name_len);
php_ini_activate_per_host_config(server_name, server_name_len);
efree(server_name);
}
}
if (php_ini_has_per_dir_config() ||
(PG(user_ini_filename) && *PG(user_ini_filename))
) {
char *path;
size_t path_len;
/* Prepare search path */
path_len = strlen(SG(request_info).path_translated);
/* Make sure we have trailing slash! */
if (!IS_SLASH(SG(request_info).path_translated[path_len])) {
path = emalloc(path_len + 2);
memcpy(path, SG(request_info).path_translated, path_len + 1);
path_len = zend_dirname(path, path_len);
path[path_len++] = DEFAULT_SLASH;
} else {
path = estrndup(SG(request_info).path_translated, path_len);
path_len = zend_dirname(path, path_len);
}
path[path_len] = 0;
/* Activate per-dir-system-configuration defined in php.ini and stored into configuration_hash during startup */
php_ini_activate_per_dir_config(path, path_len); /* Note: for global settings sake we check from root to path */
/* Load and activate user ini files in path starting from DOCUMENT_ROOT */
if (PG(user_ini_filename) && *PG(user_ini_filename)) {
char *doc_root;
if (fcgi_is_fastcgi()) {
fcgi_request *request = (fcgi_request*) SG(server_context);
doc_root = FCGI_GETENV(request, "DOCUMENT_ROOT");
} else {
doc_root = getenv("DOCUMENT_ROOT");
}
/* DOCUMENT_ROOT should also be defined at this stage..but better check it anyway */
if (doc_root) {
size_t doc_root_len = strlen(doc_root);
if (doc_root_len > 0 && IS_SLASH(doc_root[doc_root_len - 1])) {
--doc_root_len;
}
#ifdef PHP_WIN32
/* paths on windows should be case-insensitive */
doc_root = estrndup(doc_root, doc_root_len);
zend_str_tolower(doc_root, doc_root_len);
#endif
php_cgi_ini_activate_user_config(path, path_len, doc_root, doc_root_len);
#ifdef PHP_WIN32
efree(doc_root);
#endif
}
}
efree(path);
}
return SUCCESS;
}
static int sapi_cgi_deactivate(void)
{
/* flush only when SAPI was started. The reasons are:
1. SAPI Deactivate is called from two places: module init and request shutdown
2. When the first call occurs and the request is not set up, flush fails on FastCGI.
*/
if (SG(sapi_started)) {
if (fcgi_is_fastcgi()) {
if (
!parent &&
!fcgi_finish_request((fcgi_request*)SG(server_context), 0)) {
php_handle_aborted_connection();
}
} else {
sapi_cgi_flush(SG(server_context));
}
}
return SUCCESS;
}
static int php_cgi_startup(sapi_module_struct *sapi_module_ptr)
{
return php_module_startup(sapi_module_ptr, &cgi_module_entry);
}
/* {{{ sapi_module_struct cgi_sapi_module */
static sapi_module_struct cgi_sapi_module = {
"cgi-fcgi", /* name */
"CGI/FastCGI", /* pretty name */
php_cgi_startup, /* startup */
php_module_shutdown_wrapper, /* shutdown */
sapi_cgi_activate, /* activate */
sapi_cgi_deactivate, /* deactivate */
sapi_cgi_ub_write, /* unbuffered write */
sapi_cgi_flush, /* flush */
NULL, /* get uid */
sapi_cgi_getenv, /* getenv */
php_error, /* error handler */
NULL, /* header handler */
sapi_cgi_send_headers, /* send headers handler */
NULL, /* send header handler */
sapi_cgi_read_post, /* read POST data */
sapi_cgi_read_cookies, /* read Cookies */