-
Notifications
You must be signed in to change notification settings - Fork 40
/
kcgiregress.c
1213 lines (1046 loc) · 25.9 KB
/
kcgiregress.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) Kristaps Dzonsons <kristaps@bsd.lv>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "config.h"
#include <sys/socket.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <poll.h>
#include <signal.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "kcgiregress.h"
#define FCGI_HDR_SIZE 8
/*
* The FastCGI header.
* This is duplicated elsewhere in the code, but is harmless as it isn't
* really going to change.
*/
struct fcgi_hdr {
uint8_t version;
uint8_t type;
#define FCGI_HDR_END 3
#define FCGI_HDR_HEAD 4
#define FCGI_HDR_DATA_IN 5
#define FCGI_HDR_DATA_OUT 6
uint16_t requestId;
uint16_t contentLength;
uint8_t paddingLength;
uint8_t reserved;
};
/*
* Non-blocking read.
* Returns <0 on failure (system error) or bytes read, which may be
* zero in the event of EOF.
*/
static ssize_t
nb_read(int fd, void *buf, size_t bufsz)
{
struct pollfd pfd;
int rc;
assert(buf != NULL && bufsz);
pfd.fd = fd;
pfd.events = POLLIN;
if ((rc = poll(&pfd, 1, -1)) < 0) {
perror("poll");
return (-1);
} else if (rc == 0) {
fprintf(stderr, "poll: timeout!?\n");
return (-1);
}
return read(fd, buf, bufsz);
}
/*
* Non-blocking write of "bufsz" bytes in "buf".
* Return TRUE on success, FALSE on failure (system error).
*/
static int
nb_write(int fd, const void *buf, size_t bufsz)
{
ssize_t ssz;
size_t sz;
struct pollfd pfd;
int rc;
if (buf == NULL || bufsz == 0)
return 1;
pfd.fd = fd;
pfd.events = POLLOUT;
for (sz = 0; sz < bufsz; sz += (size_t)ssz) {
if ((rc = poll(&pfd, 1, -1)) < 0)
perror("poll");
else if (rc == 0)
fprintf(stderr, "poll: timeout!?\n");
else if ((ssz = write(fd, buf + sz, bufsz - sz)) < 0)
perror("write");
else if (sz > SIZE_MAX - (size_t)ssz)
fprintf(stderr, "write: overflow: "
"%zu, %zd", sz, ssz);
else
continue;
return 0;
}
return 1;
}
/*
* Blocking write of "sz" bytes in "buf".
* Return TRUE on success, FALSE on failure (system error).
*/
static int
b_write(int fd, const void *buf, size_t sz)
{
ssize_t ssz;
size_t wsz = 0;
while (sz > 0) {
if ((ssz = write(fd, buf + wsz, sz)) == -1) {
perror("write");
return 0;
}
sz -= (size_t)ssz;
wsz += (size_t)ssz;
}
return 1;
}
/*
* Blocking read (discarding bytes).
* Discards "sz" bytes and returns TRUE on success, FALSE on failure
* (system error or EOF).
*/
static int
b_ignore(int fd, size_t sz)
{
ssize_t ssz;
char buf;
while (sz > 0) {
if ((ssz = read(fd, &buf, 1)) == -1) {
perror("read");
return 0;
} else if (ssz == 0) {
fputs("read: unexpected EOF\n", stderr);
return 0;
}
sz--;
}
return 1;
}
/*
* Blocking read.
* Reads "sz" bytes into buf and returns TRUE on success, FALSE on
* failure (system error or EOF).
*/
static int
b_read(int fd, void *buf, size_t sz)
{
ssize_t ssz;
size_t rsz = 0;
while (sz > 0) {
if ((ssz = read(fd, buf + rsz, sz)) == -1) {
perror("read");
return 0;
} else if (ssz == 0) {
fputs("read: unexpected EOF\n", stderr);
return 0;
}
sz -= (size_t)ssz;
rsz += (size_t)ssz;
}
return 1;
}
/*
* Write a entire FastCGI header to the child.
* Note that the members need to be in network-byte order, so perform
* your htons and so on before invoking this function.
*/
static int
fcgi_hdr_write(int fd, const struct fcgi_hdr *hdr)
{
if (!b_write(fd, &hdr->version, 1))
fprintf(stderr, "%s: version\n", __func__);
else if (!b_write(fd, &hdr->type, 1))
fprintf(stderr, "%s: type\n", __func__);
else if (!b_write(fd, &hdr->requestId, 2))
fprintf(stderr, "%s: requestId\n", __func__);
else if (!b_write(fd, &hdr->contentLength, 2))
fprintf(stderr, "%s: data length\n", __func__);
else if (!b_write(fd, &hdr->paddingLength, 1))
fprintf(stderr, "%s: pad length\n", __func__);
else if (!b_write(fd, &hdr->reserved, 1))
fprintf(stderr, "%s: reserved\n", __func__);
else
return 1;
return 0;
}
static int
fcgi_hdr_read(int fd, struct fcgi_hdr *hdr)
{
char buf[FCGI_HDR_SIZE];
if (!b_read(fd, buf, sizeof(buf))) {
fprintf(stderr, "%s: header\n", __func__);
return 0;
}
hdr->version = buf[0];
hdr->type = buf[1];
hdr->requestId = ntohs(*(uint16_t *)&buf[2]);
hdr->contentLength = ntohs(*(uint16_t *)&buf[4]);
hdr->paddingLength = buf[6];
hdr->reserved = buf[7];
return 1;
}
static int
fcgi_data_write(int fd, const void *buf, size_t sz)
{
struct fcgi_hdr hdr;
hdr.version = 1;
hdr.type = FCGI_HDR_DATA_IN;
hdr.requestId = htons(1);
hdr.contentLength = htons(sz);
hdr.paddingLength = 0;
hdr.reserved = 0;
if (!fcgi_hdr_write(fd, &hdr))
fprintf(stderr, "%s: header\n", __func__);
else if (!b_write(fd, buf, sz))
fprintf(stderr, "%s: data\n", __func__);
else
return 1;
return 0;
}
static int
fcgi_end_read(int fd, int *status)
{
uint32_t st;
uint8_t pst;
uint8_t res[3];
if (!b_read(fd, &st, 4)) {
fprintf(stderr, "%s: status\n", __func__);
return 0;
} else if (!b_read(fd, &pst, 1)) {
fprintf(stderr, "%s: flags\n", __func__);
return 0;
} else if (!b_read(fd, res, sizeof(res))) {
fprintf(stderr, "%s: reserved\n", __func__);
return 0;
}
/*
* We use EXIT_SUCCESS for our status message.
* See output.c for where this is set.
*/
*status = ntohl(st) == EXIT_SUCCESS ? 1 : 0;
return 1;
}
static int
fcgi_begin_write(int fd)
{
struct fcgi_hdr hdr;
uint16_t role = htons(1);
uint8_t flags = 0;
uint8_t res[5];
hdr.version = 1;
hdr.type = 1;
hdr.requestId = htons(1);
hdr.contentLength = htons(8);
hdr.paddingLength = 0;
hdr.reserved = 0;
memset(res, 0, sizeof(res));
if (!fcgi_hdr_write(fd, &hdr))
fprintf(stderr, "%s: header\n", __func__);
else if (!b_write(fd, &role, 2))
fprintf(stderr, "%s: role\n", __func__);
else if (!b_write(fd, &flags, 1))
fprintf(stderr, "%s: flags\n", __func__);
else if (!b_write(fd, res, sizeof(res)))
fprintf(stderr, "%s: reserved\n", __func__);
else
return 1;
return 0;
}
/*
* Set the environment variable for a CGI script.
* This involves actually setting the environment variable.
*/
static int
dochild_params_cgi(const char *key, const char *val, void *arg)
{
setenv(key, val, 1);
return 1;
}
/*
* Set the environment variable for a FastCGI script.
* We'll need to bundle it into a FastCGI request and write that to the
* child.
*/
static int
dochild_params_fcgi(const char *key, const char *val, void *arg)
{
int fd = *(int *)arg;
struct fcgi_hdr hdr;
uint32_t lenl;
uint8_t lens;
size_t sz;
sz = strlen(key) + (strlen(key) > 127 ? 4 : 1) +
strlen(val) + (strlen(val) > 127 ? 4 : 1);
/* Start with the FastCGI header. */
hdr.version = 1;
hdr.type = FCGI_HDR_HEAD;
hdr.requestId = htons(1);
hdr.contentLength = htons(sz);
hdr.paddingLength = 0;
hdr.reserved = 0;
if (!fcgi_hdr_write(fd, &hdr)) {
fprintf(stderr, "%s: header\n", __func__);
return(0);
}
/* Key and value lengths. */
if ((sz = strlen(key)) > 127) {
lenl = htonl(sz);
if (!b_write(fd, &lenl, 4)) {
fprintf(stderr, "%s: key length", __func__);
return 0;
}
} else {
lens = sz;
if (!b_write(fd, &lens, 1)) {
fprintf(stderr, "%s: key length", __func__);
return 0;
}
}
if ((sz = strlen(val)) > 127) {
lenl = htonl(sz);
if (!b_write(fd, &lenl, 4)) {
fprintf(stderr, "%s: val length", __func__);
return 0;
}
} else {
lens = sz;
if (!b_write(fd, &lens, 1)) {
fprintf(stderr, "%s: val length", __func__);
return 0;
}
}
/* Key and value data. */
if (!b_write(fd, key, strlen(key))) {
fprintf(stderr, "%s: key", __func__);
return 0;
} else if (!b_write(fd, val, strlen(val))) {
fprintf(stderr, "%s: val", __func__);
return 0;
}
return 1;
}
/*
* Parse HTTP header lines from input.
* This obviously isn't the best way of doing things, but it's simple
* and easy to fix for the purposes of this regression suite.
* This will continually parse lines from `fd' until it reaches a blank
* line, at which point it will return control.
* It returns zero on failure (read failure, or possibly write failure
* when serialising to the CGI context) or non-zero on success.
*/
static int
dochild_params(int fd, void *arg, size_t *length,
int (*fp)(const char *, const char *, void *))
{
int first;
char head[BUFSIZ], buf[BUFSIZ];
ssize_t ssz;
size_t sz;
char *cp, *path, *query, *key, *val;
char c;
extern char *__progname;
if (length != NULL)
*length = 0;
if (!fp("SCRIPT_NAME", __progname, arg))
return 0;
/*
* Read header lines without buffering and clobbering the data
* yet to be read on the wire.
* Process them as the environment input to our CGI.
*/
for (first = 1; ; ) {
/*
* Start by reading the header itself into a
* fixed-length buffer, making sure to respect that the
* descriptor is non-blocking.
*/
for (sz = 0; sz < BUFSIZ; ) {
ssz = nb_read(fd, &c, 1);
if (ssz < 0) {
perror("read");
return 0;
} else if (ssz == 0 || (head[sz++] = c) == '\n')
break;
}
/* Strip CRLF. */
if (sz < 2 || sz == BUFSIZ) {
fprintf(stderr, "Bad HTTP header\n");
return 0;
} else if (head[sz - 2] != '\r') {
fprintf(stderr, "Bad HTTP header CRLF\n");
return 0;
}
head[sz - 2] = '\0';
/* Empty line: now we're at the CGI document. */
if (head[0] == '\0')
break;
/* Process our header. */
if (first) {
/* Snarf the first GET/POST line. */
if (strncmp(head, "GET ", 4) == 0) {
if (!fp("REQUEST_METHOD", "GET", arg))
return 0;
path = head + 4;
} else if (strncmp(head, "POST ", 5) == 0) {
if (!fp("REQUEST_METHOD", "POST", arg))
return 0;
path = head + 5;
} else if (strncmp(head, "OPTIONS ", 8) == 0) {
if (!fp("REQUEST_METHOD", "OPTIONS", arg))
return 0;
path = head + 8;
} else {
fprintf(stderr, "Unknown HTTP "
"first line: %s\n", head);
return 0;
}
/* Split this into the path and query. */
cp = path;
while (*cp != '\0' && !isspace((unsigned char)*cp))
cp++;
*cp = '\0';
if ((query = strchr(path, '?')) != NULL)
*query++ = '\0';
first = 0;
if (!fp("PATH_INFO", path, arg))
return 0;
if (query != NULL)
if (!fp("QUERY_STRING", query, arg))
return 0;
continue;
}
/*
* Split headers into key/value parts.
* Strip the leading spaces on the latter.
* Let baddies (no value) just go by.
*/
key = head;
if ((val = strchr(key, ':')) == NULL)
continue;
*val++ = '\0';
while (*val != '\0' && isspace((unsigned char)*val))
val++;
/* Recognise some attributes... */
if (strcmp(key, "Content-Length") == 0) {
if (length != NULL)
*length = atoi(val);
if (!fp("CONTENT_LENGTH", val, arg))
return 0;
continue;
} else if (strcmp(key, "Content-Type") == 0) {
if (!fp("CONTENT_TYPE", val, arg))
return 0;
continue;
}
/*
* Now we have "regular" attributes that we want to cast
* directly as HTTP attributes in the CGI environment.
*/
strlcpy(buf, "HTTP_", sizeof(buf));
sz = strlcat(buf, key, sizeof(buf));
assert(sz < sizeof(buf));
for (cp = buf; *cp != '\0'; cp++)
if (*cp == '-')
*cp = '_';
else if (isalpha((unsigned char)*cp))
*cp = toupper((unsigned char)*cp);
if (!fp(buf, val, arg))
return 0;
}
return 1;
}
/*
* First, read regress().
* This is the "child" portion of that description.
* Bind to the local port over which we'll directly accept test requests
* from our regression suite.
* This port is acting as an ad hoc web server.
* Return the file descriptor on success or -1 on failure.
*/
static int
dochild_prepare(void)
{
int s, in, opt = 1;
struct sockaddr_in ad, rem;
socklen_t len;
memset(&ad, 0, sizeof(struct sockaddr_in));
/*
* Bind and listen to our reusable testing socket.
* We pretty much just choose a random port for this.
*/
if ((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
perror("socket");
return (-1);
} else if (setsockopt(s, SOL_SOCKET,
SO_REUSEADDR, &opt, sizeof(opt)) == -1) {
perror("setsockopt");
close(s);
return (-1);
}
ad.sin_family = AF_INET;
ad.sin_port = htons(KCGI_REGRESS_PORT);
ad.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
if (bind(s, (struct sockaddr *)&ad, sizeof(ad)) == -1) {
perror("bind");
close(s);
return (-1);
} else if (listen(s, 1) == -1) {
perror("listen");
close(s);
return (-1);
}
/*
* Tell our parent that we've bound to our socket and are ready
* to receive data.
* They'll do a waitpid() to see when we're sleeping, then wake
* us up when we're already ready to go.
*/
kill(getpid(), SIGSTOP);
/*
* Wait for a single testing connection.
* When we accept it, immediately note as non-blocking: kcgi(3)
* uses polling, so it will need a non-blocking descriptor.
*/
len = sizeof(struct sockaddr_in);
if ((in = accept(s, (struct sockaddr *)&rem, &len)) == -1) {
perror("accept");
close(s);
return (-1);
} else if (fcntl(in, F_SETFL, O_NONBLOCK) == -1) {
perror("fcntl: O_NONBLOCK");
close(s);
close(in);
return 0;
}
close(s);
return in;
}
/*
* Broker a FastCGI process child.
* Return zero on failure and non-zero on success.
*/
static int
dochild_fcgi(kcgi_regress_server child, void *carg)
{
int in, rc = 0, fd;
size_t sz, len, vecsz = 0, headsz;
pid_t pid;
struct sockaddr_un un;
struct sockaddr *ss;
ssize_t ssz;
extern char *__progname;
const char *end = NULL, *start, *msg, *ovec, *cp;
char sfn[22], buf[BUFSIZ];
char *vec = NULL;
void *pp;
struct fcgi_hdr hdr;
mode_t mode;
/*
* Create a temporary file, close it, then unlink it.
* The child will recreate this as a socket.
*/
strlcpy(sfn, "/tmp/kfcgi.XXXXXXXXXX", sizeof(sfn));
/* This shuts up Coverity. */
mode = umask(S_IXUSR | S_IRWXG | S_IRWXO);
if ((fd = mkstemp(sfn)) == -1) {
perror(sfn);
return 0;
} else if (close(fd) == -1 || unlink(sfn) == -1) {
perror(sfn);
return 0;
}
umask(mode);
/* Do the usual dance to set up UNIX sockets. */
ss = (struct sockaddr *)&un;
memset(&un, 0, sizeof(un));
un.sun_family = AF_UNIX;
sz = strlcpy(un.sun_path, sfn, sizeof(un.sun_path));
if (sz >= sizeof(un.sun_path)) {
fprintf(stderr, "socket path to long\n");
return 0;
}
#if !defined(__linux__) && !defined(__sun)
un.sun_len = sz;
#endif
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
perror("socket");
return 0;
} else if (bind(fd, ss, sizeof(un)) == -1) {
perror(sfn);
close(fd);
return 0;
} else if (listen(fd, 5) == -1) {
perror(sfn);
close(fd);
return 0;
}
/*
* Now fork the FastCGI process.
* We need to use a separate process because we're going to
* package the request as a FastCGI request and ship it into the
* UNIX socket.
*/
if ((pid = fork()) == -1) {
perror("fork");
unlink(sfn);
close(fd);
return 0;
} else if (pid == 0) {
if (dup2(fd, STDIN_FILENO) == -1)
_exit(EXIT_FAILURE);
close(fd);
return child(carg);
}
/*
* Close the socket, as we're going to connect to it.
* The child has a reference to the object (via its dup2), so
* we're not going to totally remove the file.
*/
close(fd);
fd = -1;
/* Get the next incoming connection and FILE-ise it. */
if ((in = dochild_prepare()) == -1)
goto out;
/*
* Open a new socket to the FastCGI object and connect to it,
* reusing the prior socket address.
* Then remove the object, as nobody needs it any more.
*/
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
perror(sfn);
goto out;
} else if (connect(fd, ss, sizeof(un)) == -1) {
perror(sfn);
goto out;
} else if (unlink(sfn) == -1) {
perror(sfn);
goto out;
}
sfn[0] = '\0';
/* Write the request, its parameters, and all data. */
if (!fcgi_begin_write(fd))
goto out;
if (!dochild_params(in, &fd, &len, dochild_params_fcgi))
goto out;
/* Forward any data from the test's parent. */
while (len > 0) {
ssz = nb_read(in, buf,
len < sizeof(buf) ? len : sizeof(buf));
if (ssz < 0) {
perror("read");
goto out;
} else if (ssz == 0)
break;
if (!fcgi_data_write(fd, buf, ssz)) {
fprintf(stderr, "%s: stdout\n", __func__);
goto out;
}
len -= ssz;
}
/* Indicate end of input. */
if (!fcgi_data_write(fd, NULL, 0)) {
fprintf(stderr, "%s: stdout (FIN)\n", __func__);
goto out;
}
/* Read and buffer into "vec" til EOF or end of headers. */
while (fcgi_hdr_read(fd, &hdr)) {
if (hdr.type == FCGI_HDR_END) {
if (!fcgi_end_read(fd, &rc)) {
fprintf(stderr, "%s: bad fin\n", __func__);
goto out;
}
break;
} else if (hdr.type != FCGI_HDR_DATA_OUT) {
fprintf(stderr, "%s: bad type: %"
PRIu8 "\n", __func__, hdr.type);
goto out;
}
if (hdr.contentLength > 0) {
pp = realloc(vec, vecsz + hdr.contentLength);
if (pp == NULL) {
perror(NULL);
goto out;
}
vec = pp;
if (!b_read(fd, vec + vecsz, hdr.contentLength)) {
fprintf(stderr, "%s: bad read\n", __func__);
goto out;
}
vecsz += hdr.contentLength;
}
if (b_ignore(fd, hdr.paddingLength) == 0) {
fprintf(stderr, "%s: bad ignore\n", __func__);
goto out;
}
if (vecsz == 0)
continue;
end = memmem(vec, vecsz, "\r\n\r\n", 4);
if (end != NULL)
break;
}
/* Uh-oh: we read the whole message and no headers. */
if (end == NULL) {
fprintf(stderr, "FastCGI script did "
"not terminate headers\n");
rc = 1;
goto out;
}
/*
* No "status" is ok: convert it to a 200.
* If we do have a status, print it with the HTTP type.
*/
headsz = (size_t)(end - vec);
if ((start = memmem(vec, headsz, "Status:", 7)) == NULL) {
msg = "HTTP/1.1 200 OK\r\n";
if (!b_write(in, msg, strlen(msg)))
goto out;
fprintf(stderr, "FastCGI script did "
"not specify status\n");
ovec = vec;
} else {
msg = "HTTP/1.1";
if (!nb_write(in, msg, strlen(msg)))
goto out;
cp = start + 7;
while (cp < end) {
if (!nb_write(in, cp, 1))
goto out;
cp++;
if (cp[-1] == '\n')
break;
}
if (!nb_write(in, vec, (size_t)(start - vec)))
goto out;
vecsz -= (cp - vec);
ovec = cp;
}
/* Print remaining buffered data. */
if (!nb_write(in, ovec, vecsz))
goto out;
/* Forward remaining script output. */
while (fcgi_hdr_read(fd, &hdr)) {
if (hdr.type == FCGI_HDR_END) {
if (!fcgi_end_read(fd, &rc)) {
fprintf(stderr, "%s: bad fin\n", __func__);
goto out;
}
rc = 1;
break;
} else if (hdr.type != FCGI_HDR_DATA_OUT) {
fprintf(stderr, "%s: bad type: %"
PRIu8 "\n", __func__, hdr.type);
goto out;
}
while (hdr.contentLength > 0) {
vecsz = hdr.contentLength > sizeof(buf) ?
sizeof(buf) : hdr.contentLength;
if (!b_read(fd, buf, vecsz)) {
fprintf(stderr, "%s: bad read\n", __func__);
goto out;
} else if (!nb_write(in, buf, vecsz)) {
fprintf(stderr, "%s: bad write\n", __func__);
goto out;
}
hdr.contentLength -= vecsz;
}
if (b_ignore(fd, hdr.paddingLength) == 0) {
fprintf(stderr, "%s: bad ignore\n", __func__);
goto out;
}
}
if (rc == 0)
fprintf(stderr, "%s: no fin\n", __func__);
out:
/*
* Begin by asking the child to exit.
* Then close all of our comm channels.
*/
kill(pid, SIGTERM);
if (in != -1)
close(in);
if (sfn[0] != '\0')
unlink(sfn);
if (fd != -1)
close(fd);
/*
* Now mandate that the child dies and reap its resources.
* FIXME: we might kill the process before it's done actually
* terminating, which is unfair and will raise spurious
* warnings elsewhere.
*/
if (waitpid(pid, NULL, 0) == -1)
perror("waitpid");
free(vec);
return rc;
}
/*
* Broker a CGI process child.
* Return zero on failure and non-zero on success.
*/
static int
dochild_cgi(kcgi_regress_server child, void *carg)
{
int in, fd[2], rc;
const char *msg;
pid_t pid;
char *vec, *end, *start, *cp, *ovec;
size_t vecsz, headsz;
void *pp;
char buf[BUFSIZ];
ssize_t ssz;
if ((in = dochild_prepare()) == -1)
return 0;
/*
* We need to do some filtering from the CGI script's output
* (just its Status message), so create a socketpair which we'll
* use to scrub its output.
* This is because the CGI protocol is stupid: it would have
* been a lot easier to just require an HTTP status message, but
* I guess the intent was to make the web server worry about
* formatting for various versions of HTTP.
* Whatever.
*/
if (socketpair(PF_LOCAL, SOCK_STREAM, 0, fd) == -1) {
perror("socketpair");
close(in);
return 0;
}
/* Launch the actual CGI process. */
if ((pid = fork()) == -1) {
perror("fork");
close(fd[0]);
close(fd[1]);
close(in);
return 0;
} else if (pid == 0) {
close(fd[1]);
/*
* First, we suck down the HTTP headeres into our CGI
* environment and run the child.
* Next, re-assign our stdin to be the server's file
* descriptor "in".
* Then assign our stdout to be fd[0].
*/
if (!dochild_params
(in, NULL, NULL, dochild_params_cgi)) {
close(in);
close(fd[0]);
return(0);
}
if (dup2(in, STDIN_FILENO) != STDIN_FILENO) {
perror("dup2");
close(in);
close(fd[0]);
_exit(EXIT_FAILURE);
}
close(in);