[package] uhttpd:
[openwrt/svn-archive/archive.git] / package / uhttpd / src / uhttpd.c
1 /*
2 * uhttpd - Tiny single-threaded httpd - Main component
3 *
4 * Copyright (C) 2010 Jo-Philipp Wich <xm@subsignal.org>
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 #define _XOPEN_SOURCE 500 /* crypt() */
20
21 #include "uhttpd.h"
22 #include "uhttpd-utils.h"
23 #include "uhttpd-file.h"
24
25 #ifdef HAVE_CGI
26 #include "uhttpd-cgi.h"
27 #endif
28
29 #ifdef HAVE_LUA
30 #include "uhttpd-lua.h"
31 #endif
32
33 #ifdef HAVE_TLS
34 #include "uhttpd-tls.h"
35 #endif
36
37
38 static int run = 1;
39
40 static void uh_sigterm(int sig)
41 {
42 run = 0;
43 }
44
45 static void uh_sigchld(int sig)
46 {
47 while( waitpid(-1, NULL, WNOHANG) > 0 ) { }
48 }
49
50 static void uh_config_parse(struct config *conf)
51 {
52 FILE *c;
53 char line[512];
54 char *col1 = NULL;
55 char *col2 = NULL;
56 char *eol = NULL;
57
58 const char *path = conf->file ? conf->file : "/etc/httpd.conf";
59
60
61 if( (c = fopen(path, "r")) != NULL )
62 {
63 memset(line, 0, sizeof(line));
64
65 while( fgets(line, sizeof(line) - 1, c) )
66 {
67 if( (line[0] == '/') && (strchr(line, ':') != NULL) )
68 {
69 if( !(col1 = strchr(line, ':')) || (*col1++ = 0) ||
70 !(col2 = strchr(col1, ':')) || (*col2++ = 0) ||
71 !(eol = strchr(col2, '\n')) || (*eol++ = 0) )
72 continue;
73
74 if( !uh_auth_add(line, col1, col2) )
75 {
76 fprintf(stderr,
77 "Notice: No password set for user %s, ignoring "
78 "authentication on %s\n", col1, line
79 );
80 }
81 }
82 else if( !strncmp(line, "I:", 2) )
83 {
84 if( !(col1 = strchr(line, ':')) || (*col1++ = 0) ||
85 !(eol = strchr(col1, '\n')) || (*eol++ = 0) )
86 continue;
87
88 conf->index_file = strdup(col1);
89 }
90 else if( !strncmp(line, "E404:", 5) )
91 {
92 if( !(col1 = strchr(line, ':')) || (*col1++ = 0) ||
93 !(eol = strchr(col1, '\n')) || (*eol++ = 0) )
94 continue;
95
96 conf->error_handler = strdup(col1);
97 }
98 #ifdef HAVE_CGI
99 else if( (line[0] == '*') && (strchr(line, ':') != NULL) )
100 {
101 if( !(col1 = strchr(line, '*')) || (*col1++ = 0) ||
102 !(col2 = strchr(col1, ':')) || (*col2++ = 0) ||
103 !(eol = strchr(col2, '\n')) || (*eol++ = 0) )
104 continue;
105
106 if( !uh_interpreter_add(col1, col2) )
107 {
108 fprintf(stderr,
109 "Unable to add interpreter %s for extension %s: "
110 "Out of memory\n", col2, col1
111 );
112 }
113 }
114 #endif
115 }
116
117 fclose(c);
118 }
119 }
120
121 static int uh_socket_bind(
122 fd_set *serv_fds, int *max_fd, const char *host, const char *port,
123 struct addrinfo *hints, int do_tls, struct config *conf
124 ) {
125 int sock = -1;
126 int yes = 1;
127 int status;
128 int bound = 0;
129
130 int tcp_ka_idl = 1;
131 int tcp_ka_int = 1;
132 int tcp_ka_cnt = 3;
133
134 struct listener *l = NULL;
135 struct addrinfo *addrs = NULL, *p = NULL;
136
137 if( (status = getaddrinfo(host, port, hints, &addrs)) != 0 )
138 {
139 fprintf(stderr, "getaddrinfo(): %s\n", gai_strerror(status));
140 }
141
142 /* try to bind a new socket to each found address */
143 for( p = addrs; p; p = p->ai_next )
144 {
145 /* get the socket */
146 if( (sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1 )
147 {
148 perror("socket()");
149 goto error;
150 }
151
152 /* "address already in use" */
153 if( setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) )
154 {
155 perror("setsockopt()");
156 goto error;
157 }
158
159 /* TCP keep-alive */
160 if( setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &yes, sizeof(yes)) ||
161 setsockopt(sock, SOL_TCP, TCP_KEEPIDLE, &tcp_ka_idl, sizeof(tcp_ka_idl)) ||
162 setsockopt(sock, SOL_TCP, TCP_KEEPINTVL, &tcp_ka_int, sizeof(tcp_ka_int)) ||
163 setsockopt(sock, SOL_TCP, TCP_KEEPCNT, &tcp_ka_cnt, sizeof(tcp_ka_cnt)) )
164 {
165 fprintf(stderr, "Notice: Unable to enable TCP keep-alive: %s\n",
166 strerror(errno));
167 }
168
169 /* required to get parallel v4 + v6 working */
170 if( p->ai_family == AF_INET6 )
171 {
172 if( setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &yes, sizeof(yes)) == -1 )
173 {
174 perror("setsockopt()");
175 goto error;
176 }
177 }
178
179 /* bind */
180 if( bind(sock, p->ai_addr, p->ai_addrlen) == -1 )
181 {
182 perror("bind()");
183 goto error;
184 }
185
186 /* listen */
187 if( listen(sock, UH_LIMIT_CLIENTS) == -1 )
188 {
189 perror("listen()");
190 goto error;
191 }
192
193 /* add listener to global list */
194 if( ! (l = uh_listener_add(sock, conf)) )
195 {
196 fprintf(stderr, "uh_listener_add(): Failed to allocate memory\n");
197 goto error;
198 }
199
200 #ifdef HAVE_TLS
201 /* init TLS */
202 l->tls = do_tls ? conf->tls : NULL;
203 #endif
204
205 /* add socket to server fd set */
206 FD_SET(sock, serv_fds);
207 fd_cloexec(sock);
208 *max_fd = max(*max_fd, sock);
209
210 bound++;
211 continue;
212
213 error:
214 if( sock > 0 )
215 close(sock);
216 }
217
218 freeaddrinfo(addrs);
219
220 return bound;
221 }
222
223 static struct http_request * uh_http_header_parse(struct client *cl, char *buffer, int buflen)
224 {
225 char *method = &buffer[0];
226 char *path = NULL;
227 char *version = NULL;
228
229 char *headers = NULL;
230 char *hdrname = NULL;
231 char *hdrdata = NULL;
232
233 int i;
234 int hdrcount = 0;
235
236 static struct http_request req;
237
238 memset(&req, 0, sizeof(req));
239
240
241 /* terminate initial header line */
242 if( (headers = strfind(buffer, buflen, "\r\n", 2)) != NULL )
243 {
244 buffer[buflen-1] = 0;
245
246 *headers++ = 0;
247 *headers++ = 0;
248
249 /* find request path */
250 if( (path = strchr(buffer, ' ')) != NULL )
251 *path++ = 0;
252
253 /* find http version */
254 if( (path != NULL) && ((version = strchr(path, ' ')) != NULL) )
255 *version++ = 0;
256
257
258 /* check method */
259 if( strcmp(method, "GET") && strcmp(method, "HEAD") && strcmp(method, "POST") )
260 {
261 /* invalid method */
262 uh_http_response(cl, 405, "Method Not Allowed");
263 return NULL;
264 }
265 else
266 {
267 switch(method[0])
268 {
269 case 'G':
270 req.method = UH_HTTP_MSG_GET;
271 break;
272
273 case 'H':
274 req.method = UH_HTTP_MSG_HEAD;
275 break;
276
277 case 'P':
278 req.method = UH_HTTP_MSG_POST;
279 break;
280 }
281 }
282
283 /* check path */
284 if( !path || !strlen(path) )
285 {
286 /* malformed request */
287 uh_http_response(cl, 400, "Bad Request");
288 return NULL;
289 }
290 else
291 {
292 req.url = path;
293 }
294
295 /* check version */
296 if( (version == NULL) || (strcmp(version, "HTTP/0.9") &&
297 strcmp(version, "HTTP/1.0") && strcmp(version, "HTTP/1.1")) )
298 {
299 /* unsupported version */
300 uh_http_response(cl, 400, "Bad Request");
301 return NULL;
302 }
303 else
304 {
305 req.version = strtof(&version[5], NULL);
306 }
307
308
309 /* process header fields */
310 for( i = (int)(headers - buffer); i < buflen; i++ )
311 {
312 /* found eol and have name + value, push out header tuple */
313 if( hdrname && hdrdata && (buffer[i] == '\r' || buffer[i] == '\n') )
314 {
315 buffer[i] = 0;
316
317 /* store */
318 if( (hdrcount + 1) < array_size(req.headers) )
319 {
320 req.headers[hdrcount++] = hdrname;
321 req.headers[hdrcount++] = hdrdata;
322
323 hdrname = hdrdata = NULL;
324 }
325
326 /* too large */
327 else
328 {
329 uh_http_response(cl, 413, "Request Entity Too Large");
330 return NULL;
331 }
332 }
333
334 /* have name but no value and found a colon, start of value */
335 else if( hdrname && !hdrdata && ((i+2) < buflen) &&
336 (buffer[i] == ':') && (buffer[i+1] == ' ')
337 ) {
338 buffer[i] = 0;
339 hdrdata = &buffer[i+2];
340 }
341
342 /* have no name and found [A-Z], start of name */
343 else if( !hdrname && isalpha(buffer[i]) && isupper(buffer[i]) )
344 {
345 hdrname = &buffer[i];
346 }
347 }
348
349 /* valid enough */
350 req.redirect_status = 200;
351 return &req;
352 }
353
354 /* Malformed request */
355 uh_http_response(cl, 400, "Bad Request");
356 return NULL;
357 }
358
359
360 static struct http_request * uh_http_header_recv(struct client *cl)
361 {
362 static char buffer[UH_LIMIT_MSGHEAD];
363 char *bufptr = &buffer[0];
364 char *idxptr = NULL;
365
366 struct timeval timeout;
367
368 fd_set reader;
369
370 ssize_t blen = sizeof(buffer)-1;
371 ssize_t rlen = 0;
372
373 memset(buffer, 0, sizeof(buffer));
374
375 while( blen > 0 )
376 {
377 FD_ZERO(&reader);
378 FD_SET(cl->socket, &reader);
379
380 /* fail after 0.1s */
381 timeout.tv_sec = 0;
382 timeout.tv_usec = 100000;
383
384 /* check whether fd is readable */
385 if( select(cl->socket + 1, &reader, NULL, NULL, &timeout) > 0 )
386 {
387 /* receive data */
388 ensure_out(rlen = uh_tcp_peek(cl, bufptr, blen));
389
390 if( (idxptr = strfind(buffer, sizeof(buffer), "\r\n\r\n", 4)) )
391 {
392 ensure_out(rlen = uh_tcp_recv(cl, bufptr,
393 (int)(idxptr - bufptr) + 4));
394
395 /* header read complete ... */
396 blen -= rlen;
397 return uh_http_header_parse(cl, buffer,
398 sizeof(buffer) - blen - 1);
399 }
400 else
401 {
402 ensure_out(rlen = uh_tcp_recv(cl, bufptr, rlen));
403
404 blen -= rlen;
405 bufptr += rlen;
406 }
407 }
408 else
409 {
410 /* invalid request (unexpected eof/timeout) */
411 return NULL;
412 }
413 }
414
415 /* request entity too large */
416 uh_http_response(cl, 413, "Request Entity Too Large");
417
418 out:
419 return NULL;
420 }
421
422 #if defined(HAVE_LUA) || defined(HAVE_CGI)
423 static int uh_path_match(const char *prefix, const char *url)
424 {
425 if( (strstr(url, prefix) == url) &&
426 ((prefix[strlen(prefix)-1] == '/') ||
427 (strlen(url) == strlen(prefix)) ||
428 (url[strlen(prefix)] == '/'))
429 ) {
430 return 1;
431 }
432
433 return 0;
434 }
435 #endif
436
437 static void uh_dispatch_request(
438 struct client *cl, struct http_request *req, struct path_info *pin
439 ) {
440 #ifdef HAVE_CGI
441 struct interpreter *ipr = NULL;
442
443 if( uh_path_match(cl->server->conf->cgi_prefix, pin->name) ||
444 (ipr = uh_interpreter_lookup(pin->phys)) )
445 {
446 uh_cgi_request(cl, req, pin, ipr);
447 }
448 else
449 #endif
450 {
451 uh_file_request(cl, req, pin);
452 }
453 }
454
455 static void uh_mainloop(struct config *conf, fd_set serv_fds, int max_fd)
456 {
457 /* master file descriptor list */
458 fd_set used_fds, read_fds;
459
460 /* working structs */
461 struct http_request *req;
462 struct path_info *pin;
463 struct client *cl;
464
465 /* maximum file descriptor number */
466 int new_fd, cur_fd = 0;
467
468 /* clear the master and temp sets */
469 FD_ZERO(&used_fds);
470 FD_ZERO(&read_fds);
471
472 /* backup server descriptor set */
473 used_fds = serv_fds;
474
475 /* loop */
476 while(run)
477 {
478 /* create a working copy of the used fd set */
479 read_fds = used_fds;
480
481 /* sleep until socket activity */
482 if( select(max_fd + 1, &read_fds, NULL, NULL, NULL) == -1 )
483 {
484 perror("select()");
485 exit(1);
486 }
487
488 /* run through the existing connections looking for data to be read */
489 for( cur_fd = 0; cur_fd <= max_fd; cur_fd++ )
490 {
491 /* is a socket managed by us */
492 if( FD_ISSET(cur_fd, &read_fds) )
493 {
494 /* is one of our listen sockets */
495 if( FD_ISSET(cur_fd, &serv_fds) )
496 {
497 /* handle new connections */
498 if( (new_fd = accept(cur_fd, NULL, 0)) != -1 )
499 {
500 /* add to global client list */
501 if( (cl = uh_client_add(new_fd, uh_listener_lookup(cur_fd))) != NULL )
502 {
503 #ifdef HAVE_TLS
504 /* setup client tls context */
505 if( conf->tls )
506 conf->tls_accept(cl);
507 #endif
508
509 /* add client socket to global fdset */
510 FD_SET(new_fd, &used_fds);
511 fd_cloexec(new_fd);
512 max_fd = max(max_fd, new_fd);
513 }
514
515 /* insufficient resources */
516 else
517 {
518 fprintf(stderr,
519 "uh_client_add(): Cannot allocate memory\n");
520
521 close(new_fd);
522 }
523 }
524 }
525
526 /* is a client socket */
527 else
528 {
529 if( ! (cl = uh_client_lookup(cur_fd)) )
530 {
531 /* this should not happen! */
532 fprintf(stderr,
533 "uh_client_lookup(): No entry for fd %i!\n",
534 cur_fd);
535
536 goto cleanup;
537 }
538
539 /* parse message header */
540 if( (req = uh_http_header_recv(cl)) != NULL )
541 {
542 /* RFC1918 filtering required? */
543 if( conf->rfc1918_filter &&
544 sa_rfc1918(&cl->peeraddr) &&
545 !sa_rfc1918(&cl->servaddr) )
546 {
547 uh_http_sendhf(cl, 403, "Forbidden",
548 "Rejected request from RFC1918 IP "
549 "to public server address");
550 }
551 else
552 #ifdef HAVE_LUA
553 /* Lua request? */
554 if( conf->lua_state &&
555 uh_path_match(conf->lua_prefix, req->url) )
556 {
557 conf->lua_request(cl, req, conf->lua_state);
558 }
559 else
560 #endif
561 /* dispatch request */
562 if( (pin = uh_path_lookup(cl, req->url)) != NULL )
563 {
564 /* auth ok? */
565 if( uh_auth_check(cl, req, pin) )
566 uh_dispatch_request(cl, req, pin);
567 }
568
569 /* 404 */
570 else
571 {
572 /* Try to invoke an error handler */
573 pin = uh_path_lookup(cl, conf->error_handler);
574
575 if( pin && uh_auth_check(cl, req, pin) )
576 {
577 req->redirect_status = 404;
578 uh_dispatch_request(cl, req, pin);
579 }
580 else
581 {
582 uh_http_sendhf(cl, 404, "Not Found",
583 "No such file or directory");
584 }
585 }
586 }
587
588 #ifdef HAVE_TLS
589 /* free client tls context */
590 if( conf->tls )
591 conf->tls_close(cl);
592 #endif
593
594 cleanup:
595
596 /* close client socket */
597 close(cur_fd);
598 FD_CLR(cur_fd, &used_fds);
599
600 /* remove from global client list */
601 uh_client_remove(cur_fd);
602 }
603 }
604 }
605 }
606
607 #ifdef HAVE_LUA
608 /* destroy the Lua state */
609 if( conf->lua_state != NULL )
610 conf->lua_close(conf->lua_state);
611 #endif
612 }
613
614
615 int main (int argc, char **argv)
616 {
617 /* master file descriptor list */
618 fd_set used_fds, serv_fds, read_fds;
619
620 /* working structs */
621 struct addrinfo hints;
622 struct sigaction sa;
623 struct config conf;
624
625 /* signal mask */
626 sigset_t ss;
627
628 /* maximum file descriptor number */
629 int cur_fd, max_fd = 0;
630
631 #ifdef HAVE_TLS
632 int tls = 0;
633 int keys = 0;
634 #endif
635
636 int bound = 0;
637 int nofork = 0;
638
639 /* args */
640 int opt;
641 char bind[128];
642 char *port = NULL;
643
644 #if defined(HAVE_TLS) || defined(HAVE_LUA)
645 /* library handle */
646 void *lib;
647 #endif
648
649 /* clear the master and temp sets */
650 FD_ZERO(&used_fds);
651 FD_ZERO(&serv_fds);
652 FD_ZERO(&read_fds);
653
654 /* handle SIGPIPE, SIGINT, SIGTERM, SIGCHLD */
655 sa.sa_flags = 0;
656 sigemptyset(&sa.sa_mask);
657
658 sa.sa_handler = SIG_IGN;
659 sigaction(SIGPIPE, &sa, NULL);
660
661 sa.sa_handler = uh_sigchld;
662 sigaction(SIGCHLD, &sa, NULL);
663
664 sa.sa_handler = uh_sigterm;
665 sigaction(SIGINT, &sa, NULL);
666 sigaction(SIGTERM, &sa, NULL);
667
668 /* defer SIGCHLD */
669 sigemptyset(&ss);
670 sigaddset(&ss, SIGCHLD);
671 sigprocmask(SIG_BLOCK, &ss, NULL);
672
673 /* prepare addrinfo hints */
674 memset(&hints, 0, sizeof(hints));
675 hints.ai_family = AF_UNSPEC;
676 hints.ai_socktype = SOCK_STREAM;
677 hints.ai_flags = AI_PASSIVE;
678
679 /* parse args */
680 memset(&conf, 0, sizeof(conf));
681 memset(bind, 0, sizeof(bind));
682
683 #ifdef HAVE_TLS
684 /* load TLS plugin */
685 if( ! (lib = dlopen("uhttpd_tls.so", RTLD_LAZY | RTLD_GLOBAL)) )
686 {
687 fprintf(stderr,
688 "Notice: Unable to load TLS plugin - disabling SSL support! "
689 "(Reason: %s)\n", dlerror()
690 );
691 }
692 else
693 {
694 /* resolve functions */
695 if( !(conf.tls_init = dlsym(lib, "uh_tls_ctx_init")) ||
696 !(conf.tls_cert = dlsym(lib, "uh_tls_ctx_cert")) ||
697 !(conf.tls_key = dlsym(lib, "uh_tls_ctx_key")) ||
698 !(conf.tls_free = dlsym(lib, "uh_tls_ctx_free")) ||
699 !(conf.tls_accept = dlsym(lib, "uh_tls_client_accept")) ||
700 !(conf.tls_close = dlsym(lib, "uh_tls_client_close")) ||
701 !(conf.tls_recv = dlsym(lib, "uh_tls_client_recv")) ||
702 !(conf.tls_send = dlsym(lib, "uh_tls_client_send"))
703 ) {
704 fprintf(stderr,
705 "Error: Failed to lookup required symbols "
706 "in TLS plugin: %s\n", dlerror()
707 );
708 exit(1);
709 }
710
711 /* init SSL context */
712 if( ! (conf.tls = conf.tls_init()) )
713 {
714 fprintf(stderr, "Error: Failed to initalize SSL context\n");
715 exit(1);
716 }
717 }
718 #endif
719
720 while( (opt = getopt(argc, argv,
721 "fSDRC:K:E:I:p:s:h:c:l:L:d:r:m:x:i:t:T:")) > 0
722 ) {
723 switch(opt)
724 {
725 /* [addr:]port */
726 case 'p':
727 case 's':
728 if( (port = strrchr(optarg, ':')) != NULL )
729 {
730 if( (optarg[0] == '[') && (port > optarg) && (port[-1] == ']') )
731 memcpy(bind, optarg + 1,
732 min(sizeof(bind), (int)(port - optarg) - 2));
733 else
734 memcpy(bind, optarg,
735 min(sizeof(bind), (int)(port - optarg)));
736
737 port++;
738 }
739 else
740 {
741 port = optarg;
742 }
743
744 #ifdef HAVE_TLS
745 if( opt == 's' )
746 {
747 if( !conf.tls )
748 {
749 fprintf(stderr,
750 "Notice: TLS support is disabled, "
751 "ignoring '-s %s'\n", optarg
752 );
753 continue;
754 }
755
756 tls = 1;
757 }
758 #endif
759
760 /* bind sockets */
761 bound += uh_socket_bind(
762 &serv_fds, &max_fd, bind[0] ? bind : NULL, port,
763 &hints, (opt == 's'), &conf
764 );
765
766 memset(bind, 0, sizeof(bind));
767 break;
768
769 #ifdef HAVE_TLS
770 /* certificate */
771 case 'C':
772 if( conf.tls )
773 {
774 if( conf.tls_cert(conf.tls, optarg) < 1 )
775 {
776 fprintf(stderr,
777 "Error: Invalid certificate file given\n");
778 exit(1);
779 }
780
781 keys++;
782 }
783
784 break;
785
786 /* key */
787 case 'K':
788 if( conf.tls )
789 {
790 if( conf.tls_key(conf.tls, optarg) < 1 )
791 {
792 fprintf(stderr,
793 "Error: Invalid private key file given\n");
794 exit(1);
795 }
796
797 keys++;
798 }
799
800 break;
801 #endif
802
803 /* docroot */
804 case 'h':
805 if( ! realpath(optarg, conf.docroot) )
806 {
807 fprintf(stderr, "Error: Invalid directory %s: %s\n",
808 optarg, strerror(errno));
809 exit(1);
810 }
811 break;
812
813 /* error handler */
814 case 'E':
815 if( (strlen(optarg) == 0) || (optarg[0] != '/') )
816 {
817 fprintf(stderr, "Error: Invalid error handler: %s\n",
818 optarg);
819 exit(1);
820 }
821 conf.error_handler = optarg;
822 break;
823
824 /* index file */
825 case 'I':
826 if( (strlen(optarg) == 0) || (optarg[0] == '/') )
827 {
828 fprintf(stderr, "Error: Invalid index page: %s\n",
829 optarg);
830 exit(1);
831 }
832 conf.index_file = optarg;
833 break;
834
835 /* don't follow symlinks */
836 case 'S':
837 conf.no_symlinks = 1;
838 break;
839
840 /* don't list directories */
841 case 'D':
842 conf.no_dirlists = 1;
843 break;
844
845 case 'R':
846 conf.rfc1918_filter = 1;
847 break;
848
849 #ifdef HAVE_CGI
850 /* cgi prefix */
851 case 'x':
852 conf.cgi_prefix = optarg;
853 break;
854
855 /* interpreter */
856 case 'i':
857 if( (optarg[0] == '.') && (port = strchr(optarg, '=')) )
858 {
859 *port++ = 0;
860 uh_interpreter_add(optarg, port);
861 }
862 else
863 {
864 fprintf(stderr, "Error: Invalid interpreter: %s\n",
865 optarg);
866 exit(1);
867 }
868 break;
869 #endif
870
871 #ifdef HAVE_LUA
872 /* lua prefix */
873 case 'l':
874 conf.lua_prefix = optarg;
875 break;
876
877 /* lua handler */
878 case 'L':
879 conf.lua_handler = optarg;
880 break;
881 #endif
882
883 #if defined(HAVE_CGI) || defined(HAVE_LUA)
884 /* script timeout */
885 case 't':
886 conf.script_timeout = atoi(optarg);
887 break;
888 #endif
889
890 /* network timeout */
891 case 'T':
892 conf.network_timeout = atoi(optarg);
893 break;
894
895 /* no fork */
896 case 'f':
897 nofork = 1;
898 break;
899
900 /* urldecode */
901 case 'd':
902 if( (port = malloc(strlen(optarg)+1)) != NULL )
903 {
904 memset(port, 0, strlen(optarg)+1);
905 uh_urldecode(port, strlen(optarg), optarg, strlen(optarg));
906 printf("%s", port);
907 free(port);
908 exit(0);
909 }
910 break;
911
912 /* basic auth realm */
913 case 'r':
914 conf.realm = optarg;
915 break;
916
917 /* md5 crypt */
918 case 'm':
919 printf("%s\n", crypt(optarg, "$1$"));
920 exit(0);
921 break;
922
923 /* config file */
924 case 'c':
925 conf.file = optarg;
926 break;
927
928 default:
929 fprintf(stderr,
930 "Usage: %s -p [addr:]port [-h docroot]\n"
931 " -f Do not fork to background\n"
932 " -c file Configuration file, default is '/etc/httpd.conf'\n"
933 " -p [addr:]port Bind to specified address and port, multiple allowed\n"
934 #ifdef HAVE_TLS
935 " -s [addr:]port Like -p but provide HTTPS on this port\n"
936 " -C file ASN.1 server certificate file\n"
937 " -K file ASN.1 server private key file\n"
938 #endif
939 " -h directory Specify the document root, default is '.'\n"
940 " -E string Use given virtual URL as 404 error handler\n"
941 " -I string Use given filename as index page for directories\n"
942 " -S Do not follow symbolic links outside of the docroot\n"
943 " -D Do not allow directory listings, send 403 instead\n"
944 " -R Enable RFC1918 filter\n"
945 #ifdef HAVE_LUA
946 " -l string URL prefix for Lua handler, default is '/lua'\n"
947 " -L file Lua handler script, omit to disable Lua\n"
948 #endif
949 #ifdef HAVE_CGI
950 " -x string URL prefix for CGI handler, default is '/cgi-bin'\n"
951 " -i .ext=path Use interpreter at path for files with the given extension\n"
952 #endif
953 #if defined(HAVE_CGI) || defined(HAVE_LUA)
954 " -t seconds CGI and Lua script timeout in seconds, default is 60\n"
955 #endif
956 " -T seconds Network timeout in seconds, default is 30\n"
957 " -d string URL decode given string\n"
958 " -r string Specify basic auth realm\n"
959 " -m string MD5 crypt given string\n"
960 "\n", argv[0]
961 );
962
963 exit(1);
964 }
965 }
966
967 #ifdef HAVE_TLS
968 if( (tls == 1) && (keys < 2) )
969 {
970 fprintf(stderr, "Error: Missing private key or certificate file\n");
971 exit(1);
972 }
973 #endif
974
975 if( bound < 1 )
976 {
977 fprintf(stderr, "Error: No sockets bound, unable to continue\n");
978 exit(1);
979 }
980
981 /* default docroot */
982 if( !conf.docroot[0] && !realpath(".", conf.docroot) )
983 {
984 fprintf(stderr, "Error: Can not determine default document root: %s\n",
985 strerror(errno));
986 exit(1);
987 }
988
989 /* default realm */
990 if( ! conf.realm )
991 conf.realm = "Protected Area";
992
993 /* config file */
994 uh_config_parse(&conf);
995
996 /* default network timeout */
997 if( conf.network_timeout <= 0 )
998 conf.network_timeout = 30;
999
1000 #if defined(HAVE_CGI) || defined(HAVE_LUA)
1001 /* default script timeout */
1002 if( conf.script_timeout <= 0 )
1003 conf.script_timeout = 60;
1004 #endif
1005
1006 #ifdef HAVE_CGI
1007 /* default cgi prefix */
1008 if( ! conf.cgi_prefix )
1009 conf.cgi_prefix = "/cgi-bin";
1010 #endif
1011
1012 #ifdef HAVE_LUA
1013 /* load Lua plugin */
1014 if( ! (lib = dlopen("uhttpd_lua.so", RTLD_LAZY | RTLD_GLOBAL)) )
1015 {
1016 fprintf(stderr,
1017 "Notice: Unable to load Lua plugin - disabling Lua support! "
1018 "(Reason: %s)\n", dlerror()
1019 );
1020 }
1021 else
1022 {
1023 /* resolve functions */
1024 if( !(conf.lua_init = dlsym(lib, "uh_lua_init")) ||
1025 !(conf.lua_close = dlsym(lib, "uh_lua_close")) ||
1026 !(conf.lua_request = dlsym(lib, "uh_lua_request"))
1027 ) {
1028 fprintf(stderr,
1029 "Error: Failed to lookup required symbols "
1030 "in Lua plugin: %s\n", dlerror()
1031 );
1032 exit(1);
1033 }
1034
1035 /* init Lua runtime if handler is specified */
1036 if( conf.lua_handler )
1037 {
1038 /* default lua prefix */
1039 if( ! conf.lua_prefix )
1040 conf.lua_prefix = "/lua";
1041
1042 conf.lua_state = conf.lua_init(conf.lua_handler);
1043 }
1044 }
1045 #endif
1046
1047 /* fork (if not disabled) */
1048 if( ! nofork )
1049 {
1050 switch( fork() )
1051 {
1052 case -1:
1053 perror("fork()");
1054 exit(1);
1055
1056 case 0:
1057 /* daemon setup */
1058 if( chdir("/") )
1059 perror("chdir()");
1060
1061 if( (cur_fd = open("/dev/null", O_WRONLY)) > -1 )
1062 dup2(cur_fd, 0);
1063
1064 if( (cur_fd = open("/dev/null", O_RDONLY)) > -1 )
1065 dup2(cur_fd, 1);
1066
1067 if( (cur_fd = open("/dev/null", O_RDONLY)) > -1 )
1068 dup2(cur_fd, 2);
1069
1070 break;
1071
1072 default:
1073 exit(0);
1074 }
1075 }
1076
1077 /* server main loop */
1078 uh_mainloop(&conf, serv_fds, max_fd);
1079
1080 #ifdef HAVE_LUA
1081 /* destroy the Lua state */
1082 if( conf.lua_state != NULL )
1083 conf.lua_close(conf.lua_state);
1084 #endif
1085
1086 return 0;
1087 }
1088