-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjakserver.c
812 lines (728 loc) · 28.2 KB
/
jakserver.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
// Copyright 2024 Vlad Mesco
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// -std=gnu99 would set this to >= 600
#ifndef _XOPEN_SOURCE
# define _XOPEN_SOURCE 600
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <ctype.h>
#include <unistd.h>
#include <getopt.h>
#include <errno.h>
#include <signal.h>
#include <err.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <arpa/inet.h>
// don't bother with POST requests bigger than 1MB
//
// without -0 (request passed through env and/or command line),
// the kernel limits you to ~5 pages of data or so. With -0,
// the body will be written to a temporary buffer (preferably
// tmpfs/mfs) and only the headers are passed via execve(2).
//
// The server just stops reading and closes the socket when the
// request size grows beyond this limit; so it should be much
// smaller without -0, and something reasonable for your usecase
// with -0. And -0 should be on some ephemeral ramdisk.
#ifndef REQUEST_SIZE_LIMIT
# define REQUEST_SIZE_LIMIT (1 * 1024 * 1024)
#endif
// don't bother with clients that take this long to say anything.
// since this runs on lan, it might as well be <5...
#ifndef TIMEOUT_LIMIT
# define TIMEOUT_LIMIT 30
#endif
// safety precaution for the handler script;
// makes sure to smother it if it goes haywire, but also
// don't bother with clients that take too long to accept
// the response.
// define to <= 0 to disable and handle timeouts in the
// handler script itself, but that will not protect
// against clients that don't finish writing the request
// in the first place
#ifndef HANDLER_TIMEOUT_LIMIT
# define HANDLER_TIMEOUT_LIMIT TIMEOUT_LIMIT
#endif
// see listen(3), this is the backlog argument passed to listen(3p)
#ifndef MAX_BACKLOG
# define MAX_BACKLOG 10
#endif
// server socket; needs to be closed by child processes, or self on exit
int gsock = 0;
// path to shell script handling requests
char* handler = NULL;
// interface to bind; ipv4
unsigned iface = INADDR_ANY;
// port to bind
unsigned port = 8080;
// be more chatty; goes from 0 to 2
int verbose = 1;
// use temporary files to pass BODY through handler's stdin
// tmpfs(5) or mfs(8) is what's intended to be used;
// NULL means pass full request through execve()
char* payloadPath = NULL;
// what's my pid again? avoid calling getpid() too much
pid_t myPid = -1;
// used by parser
static const char* KNOWN_METHODS[] = {
"GET ",
"POST ",
"PUT ",
"DELETE ",
"HEAD ",
"PATCH ",
"OPTIONS ",
"CONNECT ",
"TRACE ",
NULL
};
// parser parsing state
enum estate {
INIT = 0, // newly created
PROTO, // parsing request line, e.g. GET / HTTP/1.1
HEADERS, // parsing headers until \r\n\r\n
BODY // reading up to Content-Length bytes or REQUEST_SIZE_LIMIT
};
// parser state
struct parser {
// internal parser state, for resume in case it needed more input
enum estate state;
// internal parser state, for resume in case it needed more input
size_t ip;
// HTTP method (see KNOWN_METHODS)
char* method;
// HTTP path, includes ;parameters?query
char* path;
#define CHUNKED_MAGIC ((size_t)-1)
// Content-Length header value;
// CHUNKED_MAGIC is used to detect chunked POSTs, which are currently not supported
size_t contentLength;
// Pointer to CRLF delimited header entries;
// The left-hand-side is lowercase'd, the right-hand-side is left intact
char* headers;
// Pointer to body (raw)
char* body;
};
enum parse_return {
NOT_IMPLEMENTED = -2, // returned for known-to-be unimplemented features
ERROR = -1, // parser didn't like something
DONE = 0, // request fully parsed
MORE = 1 // the parser needs more data
};
// initialize parser once, then keep passing that state in
// if it returns MORE, it expects the caller to read more into
// buf; it expects buf is realloc(3)'d or something like that, i.e.
// preceding data is still there when called later.
// DONE means you can pass this to execute()
// ERROR means we don't want to talk to the client anymore
//
// XXX note, it expects buf to be sbuf+1 bytes long!
//
// called in child process
int parse(struct parser* parser, char* buf, size_t sbuf)
{
if(verbose >= 2) {
fprintf(stderr, "%jd: entered parser, state %d sbuf %zd\n", (intmax_t)myPid, parser->state, sbuf);
}
// pointer to end of memory buffer
char* end = buf + sbuf;
// work pointers
char* p1, *p2, *p3;
// set if headers were properly CRLF delimited; unset if nonstandard?
int p2WasCRLF = 0;
switch(parser->state) {
case INIT:
parser->ip = 0;
parser->state = PROTO;
/*fallthrough*/
case PROTO:
// parse status / protocol / request line (whatever the first line is called)
// start from the begining
p1 = buf;
// find the end of the request line
while(p1 < end && *p1 != '\n') ++p1;
if(p1 >= end) {
return MORE;
}
*p1 = '\0';
// buf[0:p1] contains the request line
p2 = p1; // silence -Wnot-initialized
// check for known request methods;
// p2 will point to after the method
for(const char** p = KNOWN_METHODS; *p; ++p) {
size_t l = strlen(*p);
if(sbuf < l) return ERROR;
if(strncmp(buf, *p, l) == 0) {
parser->method = strdup(*p);
parser->method[l - 1] = '\0'; // KNOWN_METHODS contain one space, null it out on the dup
p2 = buf + l;
break;
}
}
// buf[0:p2] is "METHOD ", p2 points to after space
// buf[p2:p1] is the path and protocol
// if not a known method, error out
if(parser->method == NULL) return ERROR;
// skip whitespace from p2
while(isspace(*p2) && p2 < p1) ++p2;
// if p2 got to the end (p1), error out, missing path and protocol
if(p2 >= p1) return ERROR;
// there should be a space after the path, followed by HTTP/1.1
p3 = p2;
while(!isspace(*p3) && p3 < p1) ++p3;
if(p3 >= p1) return ERROR;
*p3 = '\0';
p3++;
// buf[p2:p3] is the path, buf[p3:p1] is " *protocol"
// p3 is a null terminated string to the protocol
// p2 should be a null terminated string to our path now.
parser->path = strdup(p2); // buf may be realloc'd, so strdup
// check for HTTP/1.1 or HTTP/1.0;
// newer versions imply TLS, so it wouldn't even get here
while(isspace(*p3) && p3 < p1) ++p3;
if(strncmp(p3, "HTTP/1.1", 8) != 0
&& strncmp(p3, "HTTP/1.0", 8) != 0) {
return ERROR;
}
parser->ip = (p1 - buf) + 1;
// headers start at parser->ip
parser->state = HEADERS;
/*falthrough*/
case HEADERS:
// parse headers, should end with CLRF
// Limitation: continuation lines aren't really supported
// p1 will point to the begining of a header line, of the form
// H: v\r\n
p1 = buf + parser->ip;
while(p1 < end) {
// advance p2 to CRLF
p2 = p1;
while(p2 < end && *p2 != '\n') ++p2;
if(p2 >= end) {
return MORE;
}
*p2 = '\0';
// remove go to before \r\n
if(p2 > p1 && p2[-1] == '\r') {
p2WasCRLF = 1;
p2[-1] = '\0';
} else {
p2WasCRLF = 0;
}
// p1 is now a null terminated string, we can try to parse the line
// check if we actually hit CRLFCRLF, i.e. end of headers; p1 would be "" in that case
if(*p1 == '\0') {
parser->state = BODY;
p1 = p2 + 1;
break;
}
// else, find the colon
p3 = strchr(p1, ':');
if(p3 == NULL) goto undop2; // no colon, we don't like this
// else, we have a colon; null it out, so p1 is now a null terminated string to
// a header name
*p3 = '\0';
++p3;
// p3 now points to a null terminated string of maybe the value
// headers are case insitive, so lowercase everything
for(char* pp = p1; pp < p3; ++pp) {
*pp = tolower(*pp);
}
// skip leading whitespace;
// XXX note, leading whitespace implies it could be a continuation line,
// so we might be making mistakes here
while(p1 < end && isspace(*p1) && *p1) p1++;
// parse content-length, we need that to be able to
// parse the body. Only Content-Type/Content-Length single
// file disposition is supported
if(strncmp(p1, "content-length", strlen("content-length")) == 0) {
if(parser->contentLength > 0) {
if(verbose >= 2) fprintf(stderr, "%jd: content-length and/or transfer-encoding specified multiple times\n", (intmax_t)myPid);
return ERROR;
}
parser->contentLength = atoi(p3);
} else if(strncmp(p1, "transfer-encoding", strlen("tranfer-encoding")) == 0) {
if(parser->contentLength > 0) {
if(verbose >= 2) fprintf(stderr, "%jd: content-length and/or transfer-encoding specified multiple times\n", (intmax_t)myPid);
return ERROR;
}
parser->contentLength = CHUNKED_MAGIC;
}
// undo nullifications to allow someone else to read this garbage
//undop3:
p3[-1] = ':'; // p3 was pointing to the start of the header value, so -1 is :
undop2:
*p2 = '\n'; // p2 was pointing to the end of a header line, so it was \n
if(p2WasCRLF) p2[-1] = '\r'; // and if \n was preceded by \r, it was \r
//nextheader:
p1 = p2 + 1;
// p1 now points to the next header; p2/p3 no longer valid
}
if(parser->state == HEADERS) return MORE; // never encountered CRLFCRLF, ask for more
parser->headers = strdup(buf + parser->ip); // dup headers, because buf may be reallocated for larger requests
parser->ip = p1 - buf;
// parser->ip now points to start of body
parser->state = BODY;
/*fallthrough*/
case BODY:
// if we don't have content-length, we're done; no body
if(parser->contentLength == CHUNKED_MAGIC) {
// Still considering if to add this or not.
//
// It should do something like:
// - Parse [A-Fa-f0-9]\+\r\n
// - decode to decimal -> ChLen
// - if ChLen == 0
// + read \r\n or stop (don't ask for MORE for two terminator bytes)
// - Read ChLen bytes (or ask MORE)
// - Read \r\n (or ask MORE)
// - loop
//
// The problem is, a bad request will only be handled by
// the per request timeout, which is annoying. It also
// complicates this parser a lot. And it probably won't
// be all that well tested either.
//
// Also, there was a mention somewhere about sending
// headers after the body; unclear if that referred solely
// to the overall Content-Length or what, but it sounds
// like a potential headache.
return NOT_IMPLEMENTED;
} else if(parser->contentLength == 0) {
// if no contentLength, no body, we're done
parser->body = NULL;
return DONE;
} else {
if(parser->contentLength < 0) return ERROR; // FIXME it's currently unsigned...
// Sanity check: if the content length itself is bigger than
// our limit, exit early; otherwise the caller will error
// out if the overall request size is > REQUEST_SIZE_LIMIT
if(parser->contentLength > REQUEST_SIZE_LIMIT) return ERROR;
// if the buffer doesn't contain all the data we need, tell
// the caller we want more
if(sbuf - parser->ip < parser->contentLength) {
return MORE;
} else {
// else, we're done; pass the parser to execute()
parser->body = buf + parser->ip;
// body[contentLength] should not be out of bounds, we should have
// overallocated by a byte for this purpose specifically
parser->body[parser->contentLength] = '\0';
return DONE;
}
}
}
return ERROR;
}
// in-process, quick response function for clients, in case parsing failed
//
// called in child process
void send_message(int conn, int code, const char* msg)
{
char buf[1024];
sprintf(buf, "HTTP/1.1 %d\r\nContent-Type: text/plain\r\nContent-Length: %zd\r\n\r\n%s\r\n",
code, strlen(msg) + /*len(CRLF)*/2, msg);
int n = 0;
// try to talk back for 3s, then give up
while(n++ < 3) {
int hr = send(conn, buf, strlen(buf), MSG_DONTWAIT);
if(hr == -1) {
if(errno == EAGAIN || errno == EWOULDBLOCK) {
sleep(1);
continue;
}
err(EXIT_FAILURE, "send");
}
break;
}
close(conn);
exit(0);
}
void send_error(int conn)
{
if(verbose) fprintf(stderr, "%jd: rejected\n", (intmax_t)myPid);
send_message(conn, 500, "Error");
}
void send_bad_request(int conn, const char* msg)
{
if(verbose) fprintf(stderr, "%jd: rejected\n", (intmax_t)myPid);
send_message(conn, 400, msg);
}
void send_done(int conn)
{
send_message(conn, 200, "OK");
}
// runs in child only
// passes off the request to the handler script
//
// if -0 was specified, the handler will get the request body on stdin;
// otherwise, it's in an env var; the latter is leaner, but you only get
// some amount of KBs available for one request
//
// called in child process
void execute(int conn, struct parser* parser)
{
// before closing conn...
// ...check if we need to pass a body, and how
if(parser->body) {
if(!payloadPath) {
// by env var, close stdin
setenv("REQBODY", parser->body, 1);
close(STDIN_FILENO);
} else {
// set up temp buffer
int fd = mkstemp(payloadPath);
if(fd == -1) {
fprintf(stderr, "%jd: Failed to open %s, reason: %s\n",
(intmax_t)myPid, payloadPath, strerror(errno));
send_error(conn); // exits
}
unlink(payloadPath);
// fill up buffer
size_t written = 0;
// will break on:
// - error
// - written == parser->contentLength
while(1) {
ssize_t wrote = write(fd, parser->body + written, parser->contentLength - written);
if(verbose >= 2) fprintf(stderr, "%jd: write() = %zd\n", (intmax_t)myPid, wrote);
if(wrote == -1) {
if(errno == EAGAIN) {
errno = 0;
continue;
}
fprintf(stderr, "%jd: Failed to write to %s, reason: %s\n",
(intmax_t)myPid, payloadPath, strerror(errno));
send_error(conn);
} else if(wrote == 0) {
fprintf(stderr, "%jd: failed to write to %s, reason: gave up\n",
(intmax_t)myPid, payloadPath);
send_error(conn); // exits
}
written += wrote;
if(written == parser->contentLength) break;
}
// "pass" buffer to child process as its stdin
lseek(fd, 0, SEEK_SET);
dup2(fd, STDIN_FILENO);
close(fd);
}
} else {
// no body, close stdin
close(STDIN_FILENO);
}
// make the socket be the process's stdout
dup2(conn, STDOUT_FILENO);
// get rid of our copy
close(conn);
// set headers env vars
setenv("REQHEADERS", parser->headers, 1);
if(verbose) fprintf(stderr, "%jd: Executing %s %s\n", (intmax_t)myPid, parser->method, parser->path);
// exec to the handler script
int hr = execlp(handler, handler, parser->method, parser->path, NULL);
if(hr == -1)
err(EXIT_FAILURE, "execlp");
}
/*
// leaving this around in case I want to log children exiting
void sigchld(int _ignored)
{
(void)_ignored;
int wstatus;
pid_t pid;
while((pid = waitpid(-1, &wstatus, WNOHANG)) > 0)
;
}
*/
void sighandler(int _ignored)
{
(void)_ignored;
exit(0);
}
void handler_timedout(int _ignored)
{
(void)_ignored;
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
static char buf[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ':', ' ' };
if(verbose) {
int pid = myPid;
int p = 14 /* pos of ':' */;
// p will point to a string containing "<pid>: ", where <pid> is myPid stringified;
// this does not allocate new memory, since signal handler
while(pid > 0) {
buf[--p] = pid % 10 + '0';
pid /= 10;
}
// we're supposed to exit asap, so ignore rval of write
int _ignore;
_ignore = write(fileno(stderr), &buf[p], 16 - p);
(void) _ignore;
_ignore = write(fileno(stderr), "timed out\n", strlen("timed out\n"));
(void) _ignore;
// I really don't understand why they need to insist so much on the return value
// of write; this is literally the process's last dying breath, it's really not
// the time to deal with bureaucracy. I hope the compiler doesn't learn to interpret
// the above lines as "discarding the value" or I'm going to disable that
// warning entirely, and then it helps noone.
}
_exit(1);
}
// handle connection
// spawns child process to do the actual handling
//
// called in parent and child processes, parent returns early after setting up child
void handle(int conn, struct in_addr client_addr)
{
pid_t newpid = fork();
if(-1 == newpid) {
close(conn);
fprintf(stderr, "Failed to fork: %d (%s)", errno, strerror(errno));
errno = 0;
return;
}
if(newpid > 0) {
// parent
close(conn);
return;
} else {
// child; save own pid to not call getpid() too much
myPid = newpid;
}
// runs in child which:
// - execs bash
// - exits
// so no need to worry about free
if(verbose) fprintf(stderr, "%jd: Handling request from %s\n", (intmax_t)myPid, inet_ntoa(client_addr));
// close the listen() socket, not needed here
close(gsock);
gsock = 0;
// set timer now to not deal with timeouts in select
#if HANDLER_TIMEOUT_LIMIT > 0
// set an alarm for the handler script
alarm(HANDLER_TIMEOUT_LIMIT);
signal(SIGALRM, handler_timedout);
#endif
fd_set rfds;
int retval;
// read up to 1k blocks, and try parsing the request as we go
char* buf = malloc(1025);
if(!buf)
err(EXIT_FAILURE, "malloc");
char* pbuf = buf;
ssize_t sbuf = 0;
buf[1024] = '\0';
struct parser parser;
memset(&parser, 0, sizeof(struct parser));
// will exit on:
// - error
// - exec()
// - SIGALRM
// - connection opened, but nothing written by client
while(1) {
FD_ZERO(&rfds);
FD_SET(conn, &rfds);
retval = select(conn+1, &rfds, NULL, NULL, NULL);
if(-1 == retval)
err(EXIT_FAILURE, "select");
if(0 == retval) exit(1); // client didn't want to write to us, ignore
ssize_t bytes = recv(conn, pbuf, 1024, 0);
if(bytes == -1) {
if(errno == EAGAIN) continue;
err(EXIT_FAILURE, "recv");
}
if(bytes > 0) {
sbuf += bytes;
pbuf[bytes] = '\0';
}
if(verbose >= 2)
fprintf(stderr, "%jd: DEBUG: bytes %zd buf %s pbuf %s pbuf-buf %zd\n", (intmax_t)myPid, bytes, buf, pbuf, pbuf - buf);
int what = parse(&parser, buf, sbuf);
if(what == MORE) {
if(bytes == 0) {
// EOF
send_bad_request(conn, "Expected more data");
}
if(sbuf + 1025 > REQUEST_SIZE_LIMIT) {
// too big
send_bad_request(conn, "Request too large");
}
buf = realloc(buf, sbuf + 1025);
if(!buf)
err(EXIT_FAILURE, "realloc");
pbuf = buf + sbuf;
continue;
} else if(what == DONE) {
execute(conn, &parser);
send_done(conn);
} else if(what == NOT_IMPLEMENTED) {
send_message(conn, 501, "Not implemented");
} else {
send_bad_request(conn, "Bad request, or inernal bug");
}
}
}
void help(const char* argv0)
{
printf("Usage: %s -x handler_script [-H ip4] [-p port] [-q] [-v]\n"
"Version %s\n"
"by Vlad Mesco\n\n"
"\t-h print this message\n"
"\t-q quiet, prints less\n"
"\t-v verbose; may be repeated\n"
"\t-H ip4 interface to bind; default 0.0.0.0\n"
"\t-p port which port to bind; default 8080\n"
"\t-x handler_script path to an executable script to handle requests\n"
"\t-0 /dev/shm sends request body to handler_script via its stdin\n"
"\t expects a writable path, like /dev/shm or /tmp\n"
"\n"
"The handler_script will receive 2 or 3 arguments:\n"
" o the request method\n"
" o the request path\n"
"\n"
"The headers are passed through the REQHEADERS environment variable.\n"
"Unless -0 path is specified, request body is passed through the\n"
"REQBODY environment variable.\n"
"If -0 dirpath is specified, that location will be used to buffer\n"
"request bodies. The handler_script may read the body from its stdin\n"
"\n"
"Log is on STDERR\n"
,
argv0,
VERSION);
printf("\nCompilation options:\n"
"MAX_BACKLOG=%d\n"
"REQUEST_SIZE_LIMIT=%d\n"
"TIMEOUT_LIMIT=%d\n"
"HANDLER_TIMEOUT_LIMIT=%d\n"
,
MAX_BACKLOG,
REQUEST_SIZE_LIMIT,
TIMEOUT_LIMIT,
HANDLER_TIMEOUT_LIMIT);
exit(2);
}
int main(int argc, char* argv[])
{
int opt;
while((opt = getopt(argc, argv, "H:p:x:hqv0:")) != -1) {
switch(opt) {
case 'h': help(argv[0]); return 2;
case 'x': free(handler); handler = strdup(optarg); break;
case 'H': iface = inet_addr(optarg); break;
case 'p': port = atoi(optarg); break;
case 'q': verbose--; break;
case 'v': verbose++; break;
case '0': free(payloadPath); payloadPath = strdup(optarg); break;
default:
fprintf(stderr, "Unknown flag %c\n", opt);
help(argv[0]);
return 2;
}
}
if(!handler) {
fprintf(stderr, "You must specify -x handler_script\n");
exit(2);
} else {
handler = strdup(handler);
}
if(iface == INADDR_NONE) {
fprintf(stderr, "Invalid IP passed to -H\n");
exit(2);
}
if(0 != access(handler, R_OK|X_OK))
err(EXIT_FAILURE, "access(handler_script, r-x)");
if(port == 0) {
fprintf(stderr, "Port must be > 0\n");
exit(2);
}
if(payloadPath) {
struct stat sb;
if(0 != stat(payloadPath, &sb)) {
err(EXIT_FAILURE, "stat -0 path");
} else if(!S_ISDIR(sb.st_mode)) {
fprintf(stderr, "%s is not a directory\n", payloadPath);
exit(1);
} else if(0 != access(payloadPath, R_OK|W_OK|X_OK)) {
err(EXIT_FAILURE, "access(-0 path, rwx)");
}
char* dir = payloadPath;
#define TEMPLATE_TAIL "/jakXXXXXX"
size_t fullSize = strlen(payloadPath) + strlen(TEMPLATE_TAIL) + 1;
payloadPath = malloc(fullSize);
snprintf(payloadPath, fullSize, "%s%s", dir, TEMPLATE_TAIL);
payloadPath[fullSize - 1] = '\0';
free(dir);
#undef TEMPLATE_TAIL
}
myPid = getpid();
// establish server
int hr = 0;
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(-1 == sockfd)
err(EXIT_FAILURE, "socket");
int nnn = 1;
if(-1 == setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &nnn, sizeof(int)))
err(EXIT_FAILURE, "setsockopt(SO_REUSEADDR)");
struct sockaddr_in sockaddr = {
#ifdef __OpenBSD__
sizeof(struct sockaddr_in),
#endif
AF_INET,
htons(port),
{ iface }
};
hr = bind(sockfd, (struct sockaddr*)&sockaddr, sizeof(sockaddr));
if(-1 == hr)
err(EXIT_FAILURE, "bind");
hr = listen(sockfd, MAX_BACKLOG);
if(-1 == hr)
err(EXIT_FAILURE, "listen");
gsock = sockfd;
if(verbose) {
char* host = inet_ntoa(sockaddr.sin_addr);
fprintf(stderr, "Listening on %s:%u\n", host, port);
}
signal(SIGINT, sighandler);
signal(SIGQUIT, sighandler);
struct sigaction sa;
memset(&sa, 0, sizeof(struct sigaction));
//sa.sa_handler = sigchld;
sa.sa_handler = SIG_DFL;
sigemptyset(&sa.sa_mask);
// if SA_RESTART isn't set, it interrupts accept(3) once, then
// we never get another SIGCHLD ever again.
// SA_NOCLDWAIT with SIG_DFL is functinoally equivalent to reaping children,
// so skip having to handle SIGCHLD
sa.sa_flags = SA_RESTART|SA_NOCLDWAIT;
if(sigaction(SIGCHLD, &sa, NULL) == -1)
err(EXIT_FAILURE, "sigaction");
// main loop
// exits on signals
while(1) {
struct sockaddr_in client;
socklen_t client_size = sizeof(struct sockaddr_in);
memset(&client, 0, sizeof(struct sockaddr_in));
int conn = accept(sockfd, (struct sockaddr*)&client, &client_size);
if(-1 == conn) {
fprintf(stderr, "Failed to accept connection: %d (%s)\n", errno, strerror(errno));
errno = 0;
continue;
}
handle(conn, client.sin_addr);
}
}