-
Notifications
You must be signed in to change notification settings - Fork 1
/
gatling.c
2523 lines (2394 loc) · 63.6 KB
/
gatling.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
#define _GNU_SOURCE
#include "gatling.h"
#include "socket.h"
#include "io.h"
#include "buffer.h"
#include "ip4.h"
#include "ip6.h"
#include "array.h"
#include "case.h"
#include "fmt.h"
#include "iob.h"
#include "str.h"
#include "scan.h"
#include "textcode.h"
#include "uint32.h"
#include "uint16.h"
#include "mmap.h"
#include "rangecheck.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
#include <errno.h>
#ifndef __MINGW32__
#include <sys/resource.h>
#include <sys/socket.h>
#include <pwd.h>
#include <grp.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/mman.h>
#include <fnmatch.h>
#include <sys/wait.h>
#endif
#include <stdlib.h>
#include <dirent.h>
#include <time.h>
#include <signal.h>
#include "version.h"
#include <assert.h>
#ifdef SUPPORT_SMB
#include <iconv.h>
#endif
#ifdef SUPPORT_PROXY
#include <regex.h>
#endif
#include <limits.h>
#include <fcntl.h>
#include <string.h>
#include <ctype.h>
// #include <crypt.h>
#include "havealloca.h"
#include "havesetresuid.h"
#include <stdio.h>
#if !defined(__OPTIMIZE__) && defined(__linux__)
#include <sys/prctl.h>
#endif
char serverroot[1024];
#ifdef SUPPORT_MULTIPROC
unsigned long instances=1;
#endif
unsigned long timeout_secs=23;
tai6464 now,next;
#ifdef TIMEOUT_DEBUG
void new_io_timeout(int64 d,tai6464 t) {
struct taia now;
struct taia diff;
taia_now(&now);
taia_sub(&diff,&t,&now);
buffer_puts(buffer_2,"DEBUG: scheduling timeout for fd #");
buffer_putlonglong(buffer_2,d);
buffer_puts(buffer_2," in ");
buffer_putlonglong(buffer_2,diff.sec.x);
buffer_putsflush(buffer_2," seconds.\n");
io_timeout(d,t);
}
int64 new_io_timeouted() {
int64 x=io_timeouted();
buffer_puts(buffer_2,"DEBUG: io_timeouted called, returned ");
buffer_putlonglong(buffer_2,x);
buffer_putnlflush(buffer_2);
return x;
}
#define io_timeout new_io_timeout
#define io_timeouted new_io_timeouted
#endif
const char months[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
#ifdef SUPPORT_CGI
int forksock[2];
#endif
#if defined(__OpenBSD__) || defined(__NetBSD__)
#define __broken_itojun_v6__
#endif
int virtual_hosts;
int transproxy;
int directory_index;
int logging;
int nouploads;
int chmoduploads;
#ifdef __MINGW32__
char origdir[PATH_MAX];
#else
int64 origdir;
#endif
#ifdef SUPPORT_SMB
char workgroup[20]="FNORD";
int wglen;
char workgroup_utf16[100];
int wglen16;
#endif
static void carp(const char* routine) {
buffer_putmflush(buffer_2,routine,": ",strerror(errno),"\n");
}
static void panic(const char* routine) {
carp(routine);
exit(111);
}
unsigned long connections;
unsigned long http_connections, https_connections, ftp_connections, smb_connections;
unsigned long cps,cps1; /* connections per second */
unsigned long rps,rps1; /* requests per second */
unsigned long eps,eps1; /* events per second */
unsigned long long tin,tin1; /* traffic inbound */
unsigned long long tout,tout1; /* traffic outbound */
#ifdef SUPPORT_THREADED_OPEN
unsigned int threads;
int threadpipe_query[2];
int threadpipe_response[2];
void* worker_thread(void* unused) {
int src=threadpipe_query[0];
int dest=threadpipe_response[1];
(void)unused;
for (;;) {
int fd;
struct http_data* x;
if (read(src,&fd,sizeof(fd))!=fd) return 0;
x=io_getcookie(fd);
if (!x) continue;
if (fchdir(x->cwd)==-1) continue;
x->filefd=open(x->name_of_file_to_open,O_RDONLY);
write(dest,&fd,sizeof(fd));
}
}
void init_threads(int n) {
threads=0;
if (n<=0) return;
if (threads>0) {
int i;
if (pipe(threadpipe_query)==-1 || pipe(threadpipe_response)==-1) return;
for (i=0; i<n; ++i) {
pthread_t tmp;
pthread_create(&tmp,0,worker_thread,0);
pthread_detach(tmp);
}
threads=n;
}
}
#endif
#if defined(SUPPORT_PROXY) || defined(SUPPORT_CGI)
/* You configure a list of regular expressions, and if a request matches
* one of them, the request is forwarded to some other IP:port. You can
* run another httpd there that can handle CGI, PHP, JSP and whatnot. */
struct cgi_proxy* cgis,* last;
char** _envp;
/* if port==0 then execute the CGI locally */
#endif
#ifdef SUPPORT_CGI
static int add_cgi(const char* c) {
struct cgi_proxy* x=malloc(sizeof(struct cgi_proxy));
if (!x) return -1;
byte_zero(x,sizeof(struct cgi_proxy));
if (!strcmp(c,"+x"))
x->file_executable=1;
else if (regcomp(&x->r,c,REG_EXTENDED)) {
free(x);
return -1;
}
if (!last)
cgis=last=x;
else
last->next=x; last=x;
return 0;
}
#endif
void cleanup(int64 fd) {
struct http_data* h=io_getcookie(fd);
int buddyfd=-1;
if (h) {
buddyfd=h->buddy;
if (h->t==HTTPREQUEST
#ifdef SUPPORT_FTP
|| h->t==FTPCONTROL6 || h->t==FTPCONTROL4
#endif
#ifdef SUPPORT_SMB
|| h->t==SMBREQUEST
#endif
#ifdef SUPPORT_HTTPS
|| h->t==HTTPSREQUEST || h->t==HTTPSACCEPT || h->t==HTTPSPOST
#endif
) --connections;
if (h->t==HTTPREQUEST
#ifdef SUPPORT_PROXY
|| h->t==HTTPPOST
#endif
) --http_connections;
#ifdef SUPPORT_FTP
if (h->t==FTPCONTROL4 || h->t==FTPCONTROL6) --ftp_connections;
#endif
#ifdef SUPPORT_SMB
if (h->t==SMBREQUEST) --smb_connections;
#endif
#ifdef SUPPORT_HTTPS
if (h->t==HTTPSREQUEST || h->t==HTTPSPOST || h->t==HTTPSACCEPT ||
h->t==HTTPSACCEPT_CHECK || h->t==HTTPSRESPONSE) {
--https_connections;
#ifdef USE_OPENSSL
SSL_shutdown(h->ssl);
#endif
}
#endif
#if defined(SUPPORT_FTP)
if (h->t==FTPSLAVE || h->t==FTPACTIVE || h->t==FTPPASSIVE) {
if (buddyfd!=-1) {
struct http_data* b=io_getcookie(buddyfd);
if (b)
b->buddy=-1;
}
buddyfd=-1;
}
#endif
if (h->filefd!=-1) io_close(h->filefd);
array_reset(&h->r);
iob_reset(&h->iob);
#if defined(SUPPORT_FTP) || defined(SUPPORT_SMB)
free(h->ftppath);
#endif
#ifdef SUPPORT_HTTPS
#ifdef USE_OPENSSL
if (h->ssl) SSL_free(h->ssl);
#endif
#ifdef USE_POLARSSL
if (h->ssldata) {
free_tls_ctx(h->ssldata);
free(h->ssldata);
}
#endif
#endif
#ifdef SUPPORT_SMB
close_all_handles(&h->h);
#endif
free(h);
}
io_close(fd);
if (buddyfd>=0) {
h=io_getcookie(buddyfd);
if (h) h->buddy=-1;
cleanup(buddyfd);
}
}
size_t header_complete(struct http_data* r,int64 sock) {
long i;
long l=array_bytes(&r->r);
const char* c=array_start(&r->r);
if (r->t==HTTPREQUEST
#ifdef SUPPORT_PROXY
|| r->t==HTTPPOST
#endif
#ifdef SUPPORT_HTTPS
|| r->t==HTTPSREQUEST || r->t==HTTPSPOST
#endif
)
{
/* first of all, in case someone is bombarding me with crap, detect
* that early so we can drop the connection */
if (l>10) {
tai6464 tarpit;
for (i=0; i<l; ++i) {
if (c[i]==' ') goto ok;
if (c[i]<'A' || c[i]>'Z') break;
}
/* detected invalid HTTP request */
if (logging) {
char buf[100];
buffer_puts(buffer_1,"close/not_http_traffic ");
buffer_putulong(buffer_1,sock);
buffer_putspace(buffer_1);
buffer_put(buffer_1,buf,fmt_ip6c(buf,r->peerip));
buffer_putnlflush(buffer_1);
}
io_dontwantread(sock);
io_dontwantwrite(sock);
--http_connections;
changestate(r,PUNISHMENT);
tarpit=now;
tarpit.sec.x+=10;
io_timeout(sock,tarpit);
return 0;
}
ok:
/* Erdgeist nudged me into optimizing this :-)
* I'd be surprised if this optimization has any measurable
* advantage, but it sure is impressive */
for (i=0; i+1<l; i+=2) {
if (c[i+1]=='\n') {
if (c[i]=='\n') return i+2;
else if (c[i]=='\r' && i+3<l && c[i+2]=='\r' && c[i+3]=='\n')
return i+4;
--i;
} else if (c[i+1]=='\r') {
if (i+4<l && c[i+2]=='\n' && c[i+3]=='\r' && c[i+4]=='\n')
return i+5;
--i;
}
}
#ifdef SUPPORT_SMB
} else if (r->t==SMBREQUEST) {
/* SMB */
/* first four bytes are the NetBIOS session;
* byte 0: 0 ("session message"),
* bytes 1-3: message length (big endian) */
uint32 len;
if (c[0]!=0) {
// printf("can't happen error: netbios byte 0 was not 0!?\n");
return 1;
}
len=uint32_read_big(c) & 0x00ffffff;
if (l>=len+4) return len+4;
#endif
} else {
/* FTP */
for (i=0; i<l; ++i)
if (c[i]=='\n')
return i+1;
}
return 0;
}
static char oom[]="HTTP/1.0 500 internal error\r\nContent-Type: text/plain\r\nConnection: close\r\n\r\nout of memory\n";
void httperror_realm(struct http_data* r,const char* title,const char* message,const char* realm,int nobody) {
char* c;
if (r->t==HTTPSERVER4 || r->t==HTTPSERVER6 || r->t==HTTPREQUEST
#ifdef SUPPORT_HTTPS
|| r->t==HTTPSSERVER4 || r->t==HTTPSSERVER6
|| r->t==HTTPSREQUEST || r->t==HTTPSRESPONSE
#endif
) {
c=r->hdrbuf=(char*)malloc(str_len(message)+str_len(title)+str_len(realm?realm:"")+300);
if (!c) {
r->hdrbuf=oom;
r->hlen=str_len(r->hdrbuf);
buffer_putsflush(buffer_1,"error_oom\n");
iob_addbuf(&r->iob,r->hdrbuf,r->hlen);
} else {
c+=fmt_str(c,"HTTP/1.0 ");
c+=fmt_str(c,title);
c+=fmt_str(c,"\r\nContent-Type: text/html\r\nConnection: ");
c+=fmt_str(c,r->keepalive?"keep-alive":"close");
c+=fmt_str(c,"\r\nServer: " RELEASE "\r\nContent-Length: ");
c+=fmt_ulong(c,str_len(message)+str_len(title)-4+17);
if (realm) {
c+=fmt_str(c,"\r\nWWW-Authenticate: Basic realm=\"");
c+=fmt_str(c,realm);
c+=fmt_str(c,"\"");
}
c+=fmt_str(c,"\r\n\r\n");
if (!nobody) {
c+=fmt_str(c,"<title>");
c+=fmt_str(c,title+4);
c+=fmt_str(c,"</title>\n");
c+=fmt_str(c,message);
c+=fmt_str(c,"\n");
}
r->hlen=c - r->hdrbuf;
iob_addbuf_free(&r->iob,r->hdrbuf,r->hlen);
}
} else {
/* FTP */
c=r->hdrbuf=(char*)malloc(str_len(title)+3);
c+=fmt_str(c,title);
c+=fmt_str(c,"\r\n");
r->hlen=c-r->hdrbuf;
iob_addbuf_free(&r->iob,r->hdrbuf,r->hlen);
}
}
void httperror(struct http_data* r,const char* title,const char* message,int nobody) {
httperror_realm(r,title,message,0,nobody);
}
unsigned int fmt_2digits(char* dest,int i) {
dest[0]=(i/10)+'0';
dest[1]=(i%10)+'0';
return 2;
}
#if 0
_ _ _
| |__ | |_| |_ _ __
| '_ \| __| __| '_ \
| | | | |_| |_| |_) |
|_| |_|\__|\__| .__/
|_|
#endif
const char* mimetypesfilename;
char* base;
int sort_name_a(de* x,de* y) { return (str_diff(base+x->name,base+y->name)); }
int sort_name_d(de* x,de* y) { return (str_diff(base+y->name,base+x->name)); }
int sort_mtime_a(de* x,de* y) { return x->ss.st_mtime-y->ss.st_mtime; }
int sort_mtime_d(de* x,de* y) { return y->ss.st_mtime-x->ss.st_mtime; }
int sort_size_a(de* x,de* y) { return x->ss.st_size-y->ss.st_size; }
int sort_size_d(de* x,de* y) { return y->ss.st_size-x->ss.st_size; }
#ifndef __MINGW32__
static uid_t __uid;
static gid_t __gid;
static int do_switch_uid;
static int prepare_switch_uid(const char* new_uid) {
uid_t u=0;
gid_t g=0;
if (new_uid) {
if (new_uid[0]>='0' && new_uid[0]<='9') {
unsigned long l;
const char *c=new_uid+scan_ulong(new_uid,&l);
if (*c && *c!=':' && *c!='.') return -1;
if ((u=l)!=l) return -1; /* catch overflow */
if (*c) {
++c;
c=c+scan_ulong(c,&l);
if ((g=l)!=l) return -1; /* catch overflow */
if (*c) return -1;
}
} else {
struct passwd *p=getpwnam(new_uid);
if (!p) return -1;
u=p->pw_uid;
g=p->pw_gid;
}
__uid=u;
__gid=g;
do_switch_uid=1;
}
return 0;
}
int switch_uid() {
if (!do_switch_uid) return 0;
#ifdef LIBC_HAS_SETRESUID
if (setresgid(__gid,__gid,__gid) ||
setgroups(1,&__gid) ||
setresuid(__uid,__uid,__uid))
#else
if (setgid(__gid) ||
setgroups(1,&__gid) ||
setuid(__uid))
#endif
{
/* if dropping privileges failed with EPERM, we are already running
* with reduced privileges. Don't make a fuss about it then. */
if (errno==EPERM) return 0;
return -1;
}
return 0;
}
#endif
static volatile int fini;
void sighandler(int sig) {
fini=(sig==SIGINT?1:2); /* 2 for SIGHUP */
}
static int is_server_connection(enum conntype t) {
return (t==HTTPSERVER6 || t==HTTPSERVER4
#ifdef SUPPORT_FTP
|| t==FTPSERVER6 || t==FTPSERVER4
#endif
#ifdef SUPPORT_SMB
|| t==SMBSERVER6 || t==SMBSERVER4
#endif
#ifdef SUPPORT_HTTPS
|| t==HTTPSSERVER6 || t==HTTPSSERVER4
#endif
);
}
#ifdef __broken_itojun_v6__
#warning "working around idiotic openbse ipv6 stupidity - please kick itojun for this!"
int s4; /* ipv4 http socket */
#ifdef SUPPORT_FTP
int f4; /* ipv4 ftp socket */
#endif
#ifdef SUPPORT_HTTPS
int httpss4; /* ipv4 https socket */
#endif
#endif
#ifdef SUPPORT_HTTPS
char* sshd;
unsigned long ssh_timeout;
#endif
int limit_to_lan;
static void accept_server_connection(int64 i,struct http_data* H,unsigned long ftptimeout_secs,tai6464 nextftp) {
/* This is an FTP or HTTP(S) or SMB server connection.
* This read event means that someone connected to us.
* accept() the connection, establish connection type from
* server connection type, and put the new connection into the
* state table */
char ip[16];
uint16 port;
uint32 scope_id;
int n;
while (1) {
int punk;
#ifdef __broken_itojun_v6__
if (H->t==HTTPSERVER4 || H->t==FTPSERVER4
#ifdef SUPPORT_SMB
|| H->t==SMBSERVER4
#endif
#ifdef SUPPORT_HTTPS
|| H->t==HTTPSSERVER4
#endif
) {
byte_copy(ip,12,V4mappedprefix);
scope_id=0;
n=socket_accept4(i,ip+12,&port);
} else
#endif
n=socket_accept6(i,ip,&port,&scope_id);
if (n==-1) break;
punk=new_request_from_ip(ip,now.sec.x-4611686018427387914ULL)==1;
++cps1;
++connections;
if (logging) {
char buf[IP6_FMT];
buffer_puts(buffer_1,punk?(timeout_secs?"dos_tarpit ":"dos_drop "):"accept ");
buffer_putulong(buffer_1,n);
buffer_puts(buffer_1," ");
buffer_put(buffer_1,buf,fmt_ip6c(buf,ip));
buffer_puts(buffer_1," ");
buffer_putulong(buffer_1,port);
buffer_puts(buffer_1," ");
buffer_putulong(buffer_1,connections-1);
buffer_puts(buffer_1," ");
{
const char* service;
switch (H->t) {
case HTTPSERVER4:
case HTTPSERVER6: service="http"; break;
#ifdef SUPPORT_HTTPS
case HTTPSSERVER4:
case HTTPSSERVER6: service="https"; break;
#endif
#ifdef SUPPORT_FTP
case FTPSERVER4:
case FTPSERVER6: service="ftp"; break;
#endif
#ifdef SUPPORT_SMB
case SMBSERVER4:
case SMBSERVER6: service="smb"; break;
#endif
default: service="unk";
}
buffer_puts(buffer_1,service);
}
buffer_putnlflush(buffer_1);
}
if (limit_to_lan) {
int passed;
/* if the -L option is given, only accept connections from local
* or reserved IP ranges */
if (byte_equal(ip,12,V4mappedprefix)) {
unsigned char* ip4=(unsigned char*)ip+12;
passed = ip4[0]==127 || /* 127.0.0.1/8 */
ip4[0]==10 || /* RFC1918: 10.0.0.0/8 */
(ip4[0]==192 && ip4[1]==168) || /* RFC1918: 192.168.0.0/16 */
(ip4[0]==172 && ip4[1]>=16 && ip4[1]<=31) || /* RFC1918: 172.16.0.0/12 */
(ip4[0]==169 && ip4[1]==254 && ip4[2]!=0 && ip4[2]!=255); /* RFC 5735: 169.254.0.0/16 */
} else { /* IPv6 */
unsigned char* ip6=(unsigned char*)ip;
passed = byte_equal(ip6,16,V6loopback) || /* ::1 */
(ip6[0]==0xfc && (ip6[1]&0xfe)==0) || /* RF4193 ULA fc00::/7 */
(ip6[0]==0xfe && (ip6[1]&0xfc)==0xc0) || /* deprecated site-local */
(ip6[0]==0xfe && (ip6[1]&0xfc)==0x80); /* RFC5735 link-local */
}
if (!passed) {
timeout_secs=0;
punk=1;
if (logging) {
buffer_puts(buffer_1,"close/outside_lan_drop ");
buffer_putulong(buffer_1,n);
buffer_putnlflush(buffer_1);
}
}
}
if (punk && !timeout_secs) {
io_close(n);
continue;
}
#ifdef SUPPORT_SERVERSTATUS
if (H->t==HTTPSERVER4 || H->t==HTTPSERVER6) ++http_connections;
#ifdef SUPPORT_HTTPS
if (H->t==HTTPSSERVER4 || H->t==HTTPSSERVER6) ++https_connections;
#endif
#ifdef SUPPORT_FTP
if (H->t==FTPSERVER4 || H->t==FTPSERVER6) ++ftp_connections;
#endif
#ifdef SUPPORT_SMB
if (H->t==SMBSERVER4 || H->t==SMBSERVER6) ++smb_connections;
#endif
#endif
#ifdef TCP_NODELAY
{
int i=1;
setsockopt(n,IPPROTO_TCP,TCP_NODELAY,&i,sizeof(i));
}
#else
#warning TCP_NODELAY not defined
#endif
#ifdef HAVE_IO_FD_FLAGS
if (io_fd_flags(n,IO_FD_CANWRITE|IO_FD_NONBLOCK))
#else
if (io_fd_canwrite(n))
#endif
{
struct http_data* h;
h=(struct http_data*)malloc(sizeof(struct http_data));
if (h) {
io_nonblock(n);
if (!punk) {
if (H->t==HTTPSERVER6 || H->t==HTTPSERVER4
#ifdef SUPPORT_SMB
|| H->t==SMBSERVER6 || H->t==SMBSERVER4
#endif
#ifdef SUPPORT_HTTPS
|| H->t==HTTPSSERVER6 || H->t==HTTPSSERVER4
#endif
)
io_wantread(n);
else
io_wantwrite(n);
}
byte_zero(h,sizeof(struct http_data));
#ifdef STATE_DEBUG
h->myfd=n;
#endif
#ifdef __broken_itojun_v6__
if (i==s4 || i==f4) {
byte_copy(h->myip,12,V4mappedprefix);
socket_local4(n,h->myip+12,&h->myport);
} else
socket_local6(n,h->myip,&h->myport,0);
#else
socket_local6(n,h->myip,&h->myport,0);
#endif
byte_copy(h->peerip,16,ip);
h->peerport=port;
h->myscope_id=scope_id;
if (punk) {
changestate(h,PUNISHMENT);
io_timeout(n,next);
} else if (H->t==HTTPSERVER4 || H->t==HTTPSERVER6) {
changestate(h,HTTPREQUEST);
if (timeout_secs)
io_timeout(n,next);
#ifdef SUPPORT_HTTPS
} else if (H->t==HTTPSSERVER4 || H->t==HTTPSSERVER6) {
#ifdef __MINGW32__
// printf("chdir(\"%s\") -> %d\n",origdir,chdir(origdir));
// chdir(origdir);
#else
fchdir(origdir);
#endif
#ifdef USE_OPENSSL
if (init_serverside_tls(&h->ssl,n))
#elif defined(USE_POLARSSL)
h->ssldata=malloc(sizeof(*h->ssldata));
if (!h->ssldata || init_serverside_tls(h->ssldata,n))
#endif
{
if (logging) {
char a[FMT_ULONG];
a[fmt_ulong(a,n)]=0;
buffer_putmflush(buffer_1,"ssl_setup_failed ",a," ",strerror(errno),"\nclose/readerr ",a,"\n");
}
cleanup(n);
continue;
}
changestate(h,HTTPSACCEPT_CHECK);
if (timeout_secs)
io_timeout(n,nextftp);
#endif
#ifdef SUPPORT_SMB
} else if (H->t==SMBSERVER4 || H->t==SMBSERVER6) {
changestate(h,SMBREQUEST);
if (timeout_secs)
io_timeout(n,next);
#endif
#ifdef SUPPORT_FTP
} else {
if (H->t==FTPSERVER6)
changestate(h,FTPCONTROL6);
else
changestate(h,FTPCONTROL4);
iob_addbuf(&h->iob,"220 Hi there!\r\n",15);
h->keepalive=1;
if (ftptimeout_secs)
io_timeout(n,nextftp);
#endif
}
h->buddy=-1;
h->filefd=-1;
io_setcookie(n,h);
} else {
buffer_puts(buffer_1,"close/malloc_failed ");
buffer_putulong(buffer_1,n);
buffer_putnlflush(buffer_1);
io_close(n);
}
} else {
buffer_puts(buffer_1,"close/io_fd_canwrite_failed ");
buffer_putulong(buffer_1,n);
buffer_putnlflush(buffer_1);
io_close(n);
}
#ifdef SUPPORT_MULTIPROC
if (instances>1) break;
#endif
}
if (errno==EAGAIN)
io_eagain_read(i);
else
#ifdef __broken_itojun_v6__
carp(H->t==HTTPSERVER4||H->t==FTPSERVER4?"socket_accept4":"socket_accept6");
#else
if (errno==EINVAL) {
static int64 lasteinval;
if (lasteinval!=i) {
lasteinval=i;
carp("socket_accept6");
}
}
#endif
}
#ifdef SUPPORT_HTTPS
int handle_ssl_error_code(int sock,int code,int reading) {
// printf("handle_ssl_error_code(sock %d,code %d,reading %d)\n",sock,code,reading);
switch (code) {
#ifdef USE_OPENSSL
case SSL_ERROR_WANT_READ:
io_eagain_read(sock);
io_wantread(sock);
io_dontwantwrite(sock);
return 0;
case SSL_ERROR_WANT_WRITE:
io_eagain_write(sock);
io_wantwrite(sock);
io_dontwantread(sock);
return 0;
#elif defined(USE_POLARSSL)
case POLARSSL_ERR_NET_WANT_READ:
io_eagain_read(sock);
io_wantread(sock);
io_dontwantwrite(sock);
return 0;
case POLARSSL_ERR_NET_WANT_WRITE:
io_eagain_write(sock);
io_wantwrite(sock);
io_dontwantread(sock);
return 0;
#endif
#ifdef USE_OPENSSL
case SSL_ERROR_SYSCALL:
#elif defined(USE_POLARSSL)
case POLARSSL_ERR_NET_RECV_FAILED:
case POLARSSL_ERR_NET_SEND_FAILED:
case POLARSSL_ERR_NET_CONN_RESET:
errno=ECONNRESET;
#endif
// we already signal the error up and upsteam will then write an
// error message
return -1;
default:
if (logging) {
buffer_puts(buffer_1,"ssl_protocol_error ");
buffer_putulong(buffer_1,sock);
buffer_puts(buffer_1,"\nclose/readerr ");
buffer_putulong(buffer_1,sock);
buffer_putnlflush(buffer_1);
}
errno=EPROTO;
return -1;
}
}
void do_sslaccept(int sock,struct http_data* h,int reading) {
int r;
if (h->t == HTTPSACCEPT_CHECK) {
unsigned char buf[10];
changestate(h,HTTPSACCEPT);
/* now, try to peek into the buffer, and see if it looks like an SSL
* handshake */
r=recv(sock,buf,sizeof(buf),MSG_PEEK);
if (r>5) {
if (sshd && buf[0]=='S' && buf[1]=='S' && buf[2]=='H') {
if (logging) {
char numbuf[FMT_ULONG];
numbuf[fmt_ulong(numbuf,sock)]=0;
buffer_putmflush(buffer_1,"close/ssh_handshake ",numbuf,"\n");
}
{
uint32 a=0;
write(forksock[0],&a,4);
io_passfd(forksock[0],sock);
}
--https_connections;
changestate(h,PUNISHMENT);
cleanup(sock);
return;
}
/* Apparently the packets look radically different depending on
* whether it's TLS or SSLv2. GREAT! */
/* first packet must be handshake */
if (buf[0]!=0x16 || /* content type: handshake */
buf[1]>3 || /* version major */
buf[2]>3 || /* version minor */
buf[3]>2) { /* length > 0x200 */
if (buf[0]!=0x80 ||
buf[2]!=1 || /* Client Hello */
buf[3]>3 || /* version major */
buf[4]>3) { /* version minor */
tai6464 tarpit;
/* this does not look like an SSL packet. */
if (logging) {
char tmp[100];
buffer_puts(buffer_1,"close/not_ssl_traffic ");
buffer_putulong(buffer_1,sock);
buffer_putspace(buffer_1);
buffer_put(buffer_1,tmp,fmt_ip6c(tmp,h->peerip));
buffer_putnlflush(buffer_1);
}
io_dontwantread(sock);
io_dontwantwrite(sock);
--https_connections;
changestate(h,PUNISHMENT);
tarpit=now;
tarpit.sec.x+=10;
io_timeout(sock,tarpit);
return;
}
}
}
}
#ifdef USE_OPENSSL
// printf("got SSL traffic on fd %d, calling SSL_accept\n",sock); fflush(stdout);
r=SSL_get_error(h->ssl,SSL_accept(h->ssl));
// printf("SSL_accept returned %d\n",r); fflush(stdout);
// printf("do_sslaccept -> %d\n",r);
if (r==SSL_ERROR_NONE)
#elif defined(USE_POLARSSL)
r=ssl_handshake(&h->ssldata->ssl);
if (r==0)
#endif
{
#if 0
h->writefail=1;
#endif
changestate(h,HTTPSREQUEST);
if (logging) {
buffer_puts(buffer_1,"ssl_handshake_ok ");
buffer_putulong(buffer_1,sock);
buffer_putnlflush(buffer_1);
}
return;
} else
if (handle_ssl_error_code(sock,r,reading)==-1)
cleanup(sock);
}
#endif
static void handle_incoming_data(int64 i,struct http_data* h) {
int l;
if ((l=header_complete(h,i))) {
/* at this point we saw a \r\n\r\n */
long alen;
pipeline:
/* The h->mimetype reference is here so that we don't count both the HTTP
* connection and the first request on it as a dos attack. */
if (h->mimetype && new_request_from_ip(h->peerip,now.sec.x-4611686018427387914ULL)==1) {
changestate(h,PUNISHMENT);
if (logging) {
char buf[IP6_FMT];
char n[FMT_LONG];
n[fmt_ulong(n,i)]=0;
buf[fmt_ip6c(buf,h->peerip)]=0;
buffer_putmflush(buffer_1,"dos_tarpit2 ",n," ",buf,"\n");
}
if (timeout_secs) {
io_dontwantread(i);
io_dontwantwrite(i);
} else
/* if we don't have timeouts enabled, just drop the connection */
cleanup(i);
return;
}
#ifdef SUPPORT_HTTPS
if (h->t==HTTPREQUEST || h->t==HTTPSREQUEST) {
httpresponse(h,i,l);
if (h->t == HTTPSREQUEST) changestate(h,HTTPSRESPONSE);
}
#else
if (h->t==HTTPREQUEST)
httpresponse(h,i,l);
#endif
#ifdef SUPPORT_SMB
else if (h->t==SMBREQUEST) {
if (smbresponse(h,i)==-1) {
cleanup(i);
return;
}
}
#endif
#ifdef SUPPORT_FTP
else
ftpresponse(h,i);
#endif
#ifdef SUPPORT_PROXY
if (h->t != HTTPPOST
#ifdef SUPPORT_HTTPS
&& h->t != HTTPSPOST
#endif
) {
#endif
if (l < (alen=array_bytes(&h->r))) {
char* c=array_start(&h->r);
#if 0
printf("continuation: consumed %lu bytes from buffer (%lu still there)\n",
(unsigned long)l,
(unsigned long)(alen-l));
#endif
byte_copy(c,alen-l,c+l);
array_truncate(&h->r,1,alen-l);
l=header_complete(h,i);
#if 0
write(1,"\n\n",2);
write(1,array_start(&h->r),array_bytes(&h->r));