2 * uhttpd - Tiny single-threaded httpd - Main component
4 * Copyright (C) 2010 Jo-Philipp Wich <xm@subsignal.org>
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #define _XOPEN_SOURCE 500 /* crypt() */
22 #include "uhttpd-utils.h"
23 #include "uhttpd-file.h"
26 #include "uhttpd-cgi.h"
30 #include "uhttpd-lua.h"
34 #include "uhttpd-tls.h"
38 const char * http_methods
[] = { "GET", "POST", "HEAD", };
39 const char * http_versions
[] = { "HTTP/0.9", "HTTP/1.0", "HTTP/1.1", };
43 static void uh_sigterm(int sig
)
48 static void uh_config_parse(struct config
*conf
)
56 const char *path
= conf
->file
? conf
->file
: "/etc/httpd.conf";
59 if ((c
= fopen(path
, "r")) != NULL
)
61 memset(line
, 0, sizeof(line
));
63 while (fgets(line
, sizeof(line
) - 1, c
))
65 if ((line
[0] == '/') && (strchr(line
, ':') != NULL
))
67 if (!(col1
= strchr(line
, ':')) || (*col1
++ = 0) ||
68 !(col2
= strchr(col1
, ':')) || (*col2
++ = 0) ||
69 !(eol
= strchr(col2
, '\n')) || (*eol
++ = 0))
74 if (!uh_auth_add(line
, col1
, col2
))
77 "Notice: No password set for user %s, ignoring "
78 "authentication on %s\n", col1
, line
82 else if (!strncmp(line
, "I:", 2))
84 if (!(col1
= strchr(line
, ':')) || (*col1
++ = 0) ||
85 !(eol
= strchr(col1
, '\n')) || (*eol
++ = 0))
90 conf
->index_file
= strdup(col1
);
92 else if (!strncmp(line
, "E404:", 5))
94 if (!(col1
= strchr(line
, ':')) || (*col1
++ = 0) ||
95 !(eol
= strchr(col1
, '\n')) || (*eol
++ = 0))
100 conf
->error_handler
= strdup(col1
);
103 else if ((line
[0] == '*') && (strchr(line
, ':') != NULL
))
105 if (!(col1
= strchr(line
, '*')) || (*col1
++ = 0) ||
106 !(col2
= strchr(col1
, ':')) || (*col2
++ = 0) ||
107 !(eol
= strchr(col2
, '\n')) || (*eol
++ = 0))
112 if (!uh_interpreter_add(col1
, col2
))
115 "Unable to add interpreter %s for extension %s: "
116 "Out of memory\n", col2
, col1
127 static void uh_listener_cb(struct uloop_fd
*u
, unsigned int events
);
129 static int uh_socket_bind(const char *host
, const char *port
,
130 struct addrinfo
*hints
, int do_tls
,
138 int tcp_ka_idl
, tcp_ka_int
, tcp_ka_cnt
;
140 struct listener
*l
= NULL
;
141 struct addrinfo
*addrs
= NULL
, *p
= NULL
;
143 if ((status
= getaddrinfo(host
, port
, hints
, &addrs
)) != 0)
145 fprintf(stderr
, "getaddrinfo(): %s\n", gai_strerror(status
));
148 /* try to bind a new socket to each found address */
149 for (p
= addrs
; p
; p
= p
->ai_next
)
152 if ((sock
= socket(p
->ai_family
, p
->ai_socktype
, p
->ai_protocol
)) == -1)
158 /* "address already in use" */
159 if (setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &yes
, sizeof(yes
)))
161 perror("setsockopt()");
166 if (conf
->tcp_keepalive
> 0)
170 tcp_ka_int
= conf
->tcp_keepalive
;
172 if (setsockopt(sock
, SOL_SOCKET
, SO_KEEPALIVE
, &yes
, sizeof(yes
)) ||
173 setsockopt(sock
, SOL_TCP
, TCP_KEEPIDLE
, &tcp_ka_idl
, sizeof(tcp_ka_idl
)) ||
174 setsockopt(sock
, SOL_TCP
, TCP_KEEPINTVL
, &tcp_ka_int
, sizeof(tcp_ka_int
)) ||
175 setsockopt(sock
, SOL_TCP
, TCP_KEEPCNT
, &tcp_ka_cnt
, sizeof(tcp_ka_cnt
)))
177 fprintf(stderr
, "Notice: Unable to enable TCP keep-alive: %s\n",
182 /* required to get parallel v4 + v6 working */
183 if (p
->ai_family
== AF_INET6
)
185 if (setsockopt(sock
, IPPROTO_IPV6
, IPV6_V6ONLY
, &yes
, sizeof(yes
)) == -1)
187 perror("setsockopt()");
193 if (bind(sock
, p
->ai_addr
, p
->ai_addrlen
) == -1)
200 if (listen(sock
, UH_LIMIT_CLIENTS
) == -1)
206 /* add listener to global list */
207 if (!(l
= uh_listener_add(sock
, conf
)))
209 fprintf(stderr
, "uh_listener_add(): Failed to allocate memory\n");
215 l
->tls
= do_tls
? conf
->tls
: NULL
;
218 /* add socket to uloop */
220 uh_ufd_add(&l
->fd
, uh_listener_cb
, ULOOP_READ
);
235 static struct http_request
* uh_http_header_parse(struct client
*cl
,
236 char *buffer
, int buflen
)
238 char *method
= buffer
;
240 char *version
= NULL
;
242 char *headers
= NULL
;
243 char *hdrname
= NULL
;
244 char *hdrdata
= NULL
;
249 struct http_request
*req
= &cl
->request
;
252 /* terminate initial header line */
253 if ((headers
= strfind(buffer
, buflen
, "\r\n", 2)) != NULL
)
255 buffer
[buflen
-1] = 0;
260 /* find request path */
261 if ((path
= strchr(buffer
, ' ')) != NULL
)
264 /* find http version */
265 if ((path
!= NULL
) && ((version
= strchr(path
, ' ')) != NULL
))
270 if (method
&& !strcmp(method
, "GET"))
271 req
->method
= UH_HTTP_MSG_GET
;
272 else if (method
&& !strcmp(method
, "POST"))
273 req
->method
= UH_HTTP_MSG_POST
;
274 else if (method
&& !strcmp(method
, "HEAD"))
275 req
->method
= UH_HTTP_MSG_HEAD
;
279 uh_http_response(cl
, 405, "Method Not Allowed");
284 if (!path
|| !strlen(path
))
286 /* malformed request */
287 uh_http_response(cl
, 400, "Bad Request");
296 if (version
&& !strcmp(version
, "HTTP/0.9"))
297 req
->version
= UH_HTTP_VER_0_9
;
298 else if (version
&& !strcmp(version
, "HTTP/1.0"))
299 req
->version
= UH_HTTP_VER_1_0
;
300 else if (version
&& !strcmp(version
, "HTTP/1.1"))
301 req
->version
= UH_HTTP_VER_1_1
;
304 /* unsupported version */
305 uh_http_response(cl
, 400, "Bad Request");
310 http_methods
[req
->method
], req
->url
, http_versions
[req
->version
]);
312 /* process header fields */
313 for (i
= (int)(headers
- buffer
); i
< buflen
; i
++)
315 /* found eol and have name + value, push out header tuple */
316 if (hdrname
&& hdrdata
&& (buffer
[i
] == '\r' || buffer
[i
] == '\n'))
321 if ((hdrcount
+ 1) < array_size(req
->headers
))
323 D("SRV: HTTP: %s: %s\n", hdrname
, hdrdata
);
325 req
->headers
[hdrcount
++] = hdrname
;
326 req
->headers
[hdrcount
++] = hdrdata
;
328 hdrname
= hdrdata
= NULL
;
334 D("SRV: HTTP: header too big (too many headers)\n");
335 uh_http_response(cl
, 413, "Request Entity Too Large");
340 /* have name but no value and found a colon, start of value */
341 else if (hdrname
&& !hdrdata
&&
342 ((i
+1) < buflen
) && (buffer
[i
] == ':'))
345 hdrdata
= &buffer
[i
+1];
347 while ((hdrdata
+ 1) < (buffer
+ buflen
) && *hdrdata
== ' ')
351 /* have no name and found [A-Za-z], start of name */
352 else if (!hdrname
&& isalpha(buffer
[i
]))
354 hdrname
= &buffer
[i
];
359 req
->redirect_status
= 200;
363 /* Malformed request */
364 uh_http_response(cl
, 400, "Bad Request");
369 static struct http_request
* uh_http_header_recv(struct client
*cl
)
371 char *bufptr
= cl
->httpbuf
.buf
;
374 ssize_t blen
= sizeof(cl
->httpbuf
.buf
)-1;
377 memset(bufptr
, 0, sizeof(cl
->httpbuf
.buf
));
382 ensure_out(rlen
= uh_tcp_recv(cl
, bufptr
, blen
));
383 D("SRV: Client(%d) peek(%d) = %d\n", cl
->fd
.fd
, blen
, rlen
);
387 D("SRV: Client(%d) dead [%s]\n", cl
->fd
.fd
, strerror(errno
));
394 if ((idxptr
= strfind(cl
->httpbuf
.buf
, sizeof(cl
->httpbuf
.buf
),
397 /* header read complete ... */
398 cl
->httpbuf
.ptr
= idxptr
+ 4;
399 cl
->httpbuf
.len
= bufptr
- cl
->httpbuf
.ptr
;
401 return uh_http_header_parse(cl
, cl
->httpbuf
.buf
,
402 (cl
->httpbuf
.ptr
- cl
->httpbuf
.buf
));
406 /* request entity too large */
407 D("SRV: HTTP: header too big (buffer exceeded)\n");
408 uh_http_response(cl
, 413, "Request Entity Too Large");
414 #if defined(HAVE_LUA) || defined(HAVE_CGI)
415 static int uh_path_match(const char *prefix
, const char *url
)
417 if ((strstr(url
, prefix
) == url
) &&
418 ((prefix
[strlen(prefix
)-1] == '/') ||
419 (strlen(url
) == strlen(prefix
)) ||
420 (url
[strlen(prefix
)] == '/')))
429 static bool uh_dispatch_request(struct client
*cl
, struct http_request
*req
)
431 struct path_info
*pin
;
432 struct interpreter
*ipr
= NULL
;
433 struct config
*conf
= cl
->server
->conf
;
437 if (conf
->lua_state
&&
438 uh_path_match(conf
->lua_prefix
, req
->url
))
440 return conf
->lua_request(cl
, conf
->lua_state
);
447 if (conf
->ubus_state
&&
448 uh_path_match(conf
->ubus_prefix
, req
->url
))
450 return conf
->ubus_request(cl
, conf
->ubus_state
);
455 /* dispatch request */
456 if ((pin
= uh_path_lookup(cl
, req
->url
)) != NULL
)
459 if (!pin
->redirected
&& uh_auth_check(cl
, req
, pin
))
462 if (uh_path_match(conf
->cgi_prefix
, pin
->name
) ||
463 (ipr
= uh_interpreter_lookup(pin
->phys
)) != NULL
)
465 return uh_cgi_request(cl
, pin
, ipr
);
468 return uh_file_request(cl
, pin
);
475 /* Try to invoke an error handler */
476 if ((pin
= uh_path_lookup(cl
, conf
->error_handler
)) != NULL
)
479 if (uh_auth_check(cl
, req
, pin
))
481 req
->redirect_status
= 404;
483 if (uh_path_match(conf
->cgi_prefix
, pin
->name
) ||
484 (ipr
= uh_interpreter_lookup(pin
->phys
)) != NULL
)
486 return uh_cgi_request(cl
, pin
, ipr
);
489 return uh_file_request(cl
, pin
);
496 uh_http_sendhf(cl
, 404, "Not Found", "No such file or directory");
503 static void uh_socket_cb(struct uloop_fd
*u
, unsigned int events
);
505 static void uh_listener_cb(struct uloop_fd
*u
, unsigned int events
)
508 struct listener
*serv
;
512 struct sockaddr_in6 sa
;
513 socklen_t sl
= sizeof(sa
);
515 serv
= container_of(u
, struct listener
, fd
);
518 /* defer client if maximum number of requests is exceeded */
519 if (serv
->n_clients
>= conf
->max_requests
)
522 /* handle new connections */
523 if ((new_fd
= accept(u
->fd
, (struct sockaddr
*)&sa
, &sl
)) != -1)
525 D("SRV: Server(%d) accept => Client(%d)\n", u
->fd
, new_fd
);
527 /* add to global client list */
528 if ((cl
= uh_client_add(new_fd
, serv
, &sa
)) != NULL
)
530 /* add client socket to global fdset */
531 uh_ufd_add(&cl
->fd
, uh_socket_cb
, ULOOP_READ
);
532 fd_cloexec(cl
->fd
.fd
);
535 /* setup client tls context */
538 if (conf
->tls_accept(cl
) < 1)
540 D("SRV: Client(%d) SSL handshake failed, drop\n", new_fd
);
542 /* remove from global client list */
543 uh_client_remove(cl
);
550 /* insufficient resources */
553 fprintf(stderr
, "uh_client_add(): Cannot allocate memory\n");
559 static void uh_client_cb(struct client
*cl
, unsigned int events
);
561 static void uh_rpipe_cb(struct uloop_fd
*u
, unsigned int events
)
563 struct client
*cl
= container_of(u
, struct client
, rpipe
);
565 D("SRV: Client(%d) rpipe readable\n", cl
->fd
.fd
);
567 uh_client_cb(cl
, ULOOP_WRITE
);
570 static void uh_socket_cb(struct uloop_fd
*u
, unsigned int events
)
572 struct client
*cl
= container_of(u
, struct client
, fd
);
574 D("SRV: Client(%d) socket readable\n", cl
->fd
.fd
);
576 uh_client_cb(cl
, ULOOP_READ
);
579 static void uh_child_cb(struct uloop_process
*p
, int rv
)
581 struct client
*cl
= container_of(p
, struct client
, proc
);
583 D("SRV: Client(%d) child(%d) dead\n", cl
->fd
.fd
, cl
->proc
.pid
);
585 uh_client_cb(cl
, ULOOP_READ
| ULOOP_WRITE
);
588 static void uh_kill9_cb(struct uloop_timeout
*t
)
590 struct client
*cl
= container_of(t
, struct client
, timeout
);
592 if (!kill(cl
->proc
.pid
, 0))
594 D("SRV: Client(%d) child(%d) kill(SIGKILL)...\n",
595 cl
->fd
.fd
, cl
->proc
.pid
);
597 kill(cl
->proc
.pid
, SIGKILL
);
601 static void uh_timeout_cb(struct uloop_timeout
*t
)
603 struct client
*cl
= container_of(t
, struct client
, timeout
);
605 D("SRV: Client(%d) child(%d) timed out\n", cl
->fd
.fd
, cl
->proc
.pid
);
607 if (!kill(cl
->proc
.pid
, 0))
609 D("SRV: Client(%d) child(%d) kill(SIGTERM)...\n",
610 cl
->fd
.fd
, cl
->proc
.pid
);
612 kill(cl
->proc
.pid
, SIGTERM
);
614 cl
->timeout
.cb
= uh_kill9_cb
;
615 uloop_timeout_set(&cl
->timeout
, 1000);
619 static void uh_client_cb(struct client
*cl
, unsigned int events
)
623 struct http_request
*req
;
625 conf
= cl
->server
->conf
;
627 D("SRV: Client(%d) enter callback\n", cl
->fd
.fd
);
629 /* undispatched yet */
632 /* we have no headers yet and this was a write event, ignore... */
633 if (!(events
& ULOOP_READ
))
635 D("SRV: Client(%d) ignoring write event before headers\n", cl
->fd
.fd
);
639 /* attempt to receive and parse headers */
640 if (!(req
= uh_http_header_recv(cl
)))
642 D("SRV: Client(%d) failed to receive header\n", cl
->fd
.fd
);
643 uh_client_shutdown(cl
);
647 /* process expect headers */
648 foreach_header(i
, req
->headers
)
650 if (strcasecmp(req
->headers
[i
], "Expect"))
653 if (strcasecmp(req
->headers
[i
+1], "100-continue"))
655 D("SRV: Client(%d) unknown expect header (%s)\n",
656 cl
->fd
.fd
, req
->headers
[i
+1]);
658 uh_http_response(cl
, 417, "Precondition Failed");
659 uh_client_shutdown(cl
);
664 D("SRV: Client(%d) sending HTTP/1.1 100 Continue\n", cl
->fd
.fd
);
666 uh_http_sendf(cl
, NULL
, "HTTP/1.1 100 Continue\r\n\r\n");
667 cl
->httpbuf
.len
= 0; /* client will re-send the body */
672 /* RFC1918 filtering */
673 if (conf
->rfc1918_filter
&&
674 sa_rfc1918(&cl
->peeraddr
) && !sa_rfc1918(&cl
->servaddr
))
676 uh_http_sendhf(cl
, 403, "Forbidden",
677 "Rejected request from RFC1918 IP "
678 "to public server address");
680 uh_client_shutdown(cl
);
684 /* dispatch request */
685 if (!uh_dispatch_request(cl
, req
))
687 D("SRV: Client(%d) failed to dispach request\n", cl
->fd
.fd
);
688 uh_client_shutdown(cl
);
692 /* request handler spawned a pipe, register handler */
693 if (cl
->rpipe
.fd
> -1)
695 D("SRV: Client(%d) pipe(%d) spawned\n", cl
->fd
.fd
, cl
->rpipe
.fd
);
697 uh_ufd_add(&cl
->rpipe
, uh_rpipe_cb
, ULOOP_READ
);
700 /* request handler spawned a child, register handler */
703 D("SRV: Client(%d) child(%d) spawned\n", cl
->fd
.fd
, cl
->proc
.pid
);
705 cl
->proc
.cb
= uh_child_cb
;
706 uloop_process_add(&cl
->proc
);
708 cl
->timeout
.cb
= uh_timeout_cb
;
709 uloop_timeout_set(&cl
->timeout
, conf
->script_timeout
* 1000);
712 /* header processing complete */
713 D("SRV: Client(%d) dispatched\n", cl
->fd
.fd
);
714 cl
->dispatched
= true;
719 D("SRV: Client(%d) response callback signalized EOF\n", cl
->fd
.fd
);
720 uh_client_shutdown(cl
);
726 static inline int uh_inittls(struct config
*conf
)
732 if (conf
->tls
!= NULL
)
735 /* load TLS plugin */
736 if (!(lib
= dlopen("uhttpd_tls.so", RTLD_LAZY
| RTLD_GLOBAL
)))
739 "Notice: Unable to load TLS plugin - disabling SSL support! "
740 "(Reason: %s)\n", dlerror()
747 /* resolve functions */
748 if (!(conf
->tls_init
= dlsym(lib
, "uh_tls_ctx_init")) ||
749 !(conf
->tls_cert
= dlsym(lib
, "uh_tls_ctx_cert")) ||
750 !(conf
->tls_key
= dlsym(lib
, "uh_tls_ctx_key")) ||
751 !(conf
->tls_free
= dlsym(lib
, "uh_tls_ctx_free")) ||
752 !(conf
->tls_accept
= dlsym(lib
, "uh_tls_client_accept")) ||
753 !(conf
->tls_close
= dlsym(lib
, "uh_tls_client_close")) ||
754 !(conf
->tls_recv
= dlsym(lib
, "uh_tls_client_recv")) ||
755 !(conf
->tls_send
= dlsym(lib
, "uh_tls_client_send")))
758 "Error: Failed to lookup required symbols "
759 "in TLS plugin: %s\n", dlerror()
764 /* init SSL context */
765 if (!(conf
->tls
= conf
->tls_init()))
767 fprintf(stderr
, "Error: Failed to initalize SSL context\n");
776 int main (int argc
, char **argv
)
778 /* working structs */
779 struct addrinfo hints
;
783 /* maximum file descriptor number */
799 #if defined(HAVE_LUA) || defined(HAVE_TLS) || defined(HAVE_UBUS)
804 /* handle SIGPIPE, SIGINT, SIGTERM */
806 sigemptyset(&sa
.sa_mask
);
808 sa
.sa_handler
= SIG_IGN
;
809 sigaction(SIGPIPE
, &sa
, NULL
);
811 sa
.sa_handler
= uh_sigterm
;
812 sigaction(SIGINT
, &sa
, NULL
);
813 sigaction(SIGTERM
, &sa
, NULL
);
815 /* prepare addrinfo hints */
816 memset(&hints
, 0, sizeof(hints
));
817 hints
.ai_family
= AF_UNSPEC
;
818 hints
.ai_socktype
= SOCK_STREAM
;
819 hints
.ai_flags
= AI_PASSIVE
;
822 memset(&conf
, 0, sizeof(conf
));
826 while ((opt
= getopt(argc
, argv
,
827 "fSDRC:K:E:I:p:s:h:c:l:L:d:r:m:n:x:i:t:T:A:u:U:")) > 0)
834 memset(addr
, 0, sizeof(addr
));
836 if ((port
= strrchr(optarg
, ':')) != NULL
)
838 if ((optarg
[0] == '[') && (port
> optarg
) && (port
[-1] == ']'))
839 memcpy(addr
, optarg
+ 1,
840 min(sizeof(addr
), (int)(port
- optarg
) - 2));
843 min(sizeof(addr
), (int)(port
- optarg
)));
855 if (uh_inittls(&conf
))
858 "Notice: TLS support is disabled, "
859 "ignoring '-s %s'\n", optarg
869 bound
+= uh_socket_bind(addr
[0] ? addr
: NULL
, port
, &hints
,
870 (opt
== 's'), &conf
);
876 if (!uh_inittls(&conf
))
878 if (conf
.tls_cert(conf
.tls
, optarg
) < 1)
881 "Error: Invalid certificate file given\n");
892 if (!uh_inittls(&conf
))
894 if (conf
.tls_key(conf
.tls
, optarg
) < 1)
897 "Error: Invalid private key file given\n");
909 "Notice: TLS support not compiled, ignoring -%c\n",
916 if (! realpath(optarg
, conf
.docroot
))
918 fprintf(stderr
, "Error: Invalid directory %s: %s\n",
919 optarg
, strerror(errno
));
926 if ((strlen(optarg
) == 0) || (optarg
[0] != '/'))
928 fprintf(stderr
, "Error: Invalid error handler: %s\n",
932 conf
.error_handler
= optarg
;
937 if ((strlen(optarg
) == 0) || (optarg
[0] == '/'))
939 fprintf(stderr
, "Error: Invalid index page: %s\n",
943 conf
.index_file
= optarg
;
946 /* don't follow symlinks */
948 conf
.no_symlinks
= 1;
951 /* don't list directories */
953 conf
.no_dirlists
= 1;
957 conf
.rfc1918_filter
= 1;
961 conf
.max_requests
= atoi(optarg
);
967 conf
.cgi_prefix
= optarg
;
972 if ((optarg
[0] == '.') && (port
= strchr(optarg
, '=')))
975 uh_interpreter_add(optarg
, port
);
979 fprintf(stderr
, "Error: Invalid interpreter: %s\n",
988 "Notice: CGI support not compiled, ignoring -%c\n",
996 conf
.lua_prefix
= optarg
;
1001 conf
.lua_handler
= optarg
;
1007 "Notice: Lua support not compiled, ignoring -%c\n",
1015 conf
.ubus_prefix
= optarg
;
1020 conf
.ubus_socket
= optarg
;
1026 "Notice: UBUS support not compiled, ignoring -%c\n",
1031 #if defined(HAVE_CGI) || defined(HAVE_LUA)
1032 /* script timeout */
1034 conf
.script_timeout
= atoi(optarg
);
1038 /* network timeout */
1040 conf
.network_timeout
= atoi(optarg
);
1043 /* tcp keep-alive */
1045 conf
.tcp_keepalive
= atoi(optarg
);
1055 if ((port
= malloc(strlen(optarg
)+1)) != NULL
)
1057 /* "decode" plus to space to retain compat */
1058 for (opt
= 0; optarg
[opt
]; opt
++)
1059 if (optarg
[opt
] == '+')
1061 /* opt now contains strlen(optarg) -- no need to re-scan */
1062 memset(port
, 0, opt
+1);
1063 if (uh_urldecode(port
, opt
, optarg
, opt
) < 0)
1064 fprintf(stderr
, "uhttpd: invalid encoding\n");
1072 /* basic auth realm */
1074 conf
.realm
= optarg
;
1079 printf("%s\n", crypt(optarg
, "$1$"));
1090 "Usage: %s -p [addr:]port [-h docroot]\n"
1091 " -f Do not fork to background\n"
1092 " -c file Configuration file, default is '/etc/httpd.conf'\n"
1093 " -p [addr:]port Bind to specified address and port, multiple allowed\n"
1095 " -s [addr:]port Like -p but provide HTTPS on this port\n"
1096 " -C file ASN.1 server certificate file\n"
1097 " -K file ASN.1 server private key file\n"
1099 " -h directory Specify the document root, default is '.'\n"
1100 " -E string Use given virtual URL as 404 error handler\n"
1101 " -I string Use given filename as index page for directories\n"
1102 " -S Do not follow symbolic links outside of the docroot\n"
1103 " -D Do not allow directory listings, send 403 instead\n"
1104 " -R Enable RFC1918 filter\n"
1105 " -n count Maximum allowed number of concurrent requests\n"
1107 " -l string URL prefix for Lua handler, default is '/lua'\n"
1108 " -L file Lua handler script, omit to disable Lua\n"
1111 " -u string URL prefix for HTTP/JSON handler\n"
1112 " -U file Override ubus socket path\n"
1115 " -x string URL prefix for CGI handler, default is '/cgi-bin'\n"
1116 " -i .ext=path Use interpreter at path for files with the given extension\n"
1118 #if defined(HAVE_CGI) || defined(HAVE_LUA) || defined(HAVE_UBUS)
1119 " -t seconds CGI, Lua and UBUS script timeout in seconds, default is 60\n"
1121 " -T seconds Network timeout in seconds, default is 30\n"
1122 " -d string URL decode given string\n"
1123 " -r string Specify basic auth realm\n"
1124 " -m string MD5 crypt given string\n"
1133 if ((tls
== 1) && (keys
< 2))
1135 fprintf(stderr
, "Error: Missing private key or certificate file\n");
1142 fprintf(stderr
, "Error: No sockets bound, unable to continue\n");
1146 /* default docroot */
1147 if (!conf
.docroot
[0] && !realpath(".", conf
.docroot
))
1149 fprintf(stderr
, "Error: Can not determine default document root: %s\n",
1156 conf
.realm
= "Protected Area";
1159 uh_config_parse(&conf
);
1161 /* default max requests */
1162 if (conf
.max_requests
<= 0)
1163 conf
.max_requests
= 3;
1165 /* default network timeout */
1166 if (conf
.network_timeout
<= 0)
1167 conf
.network_timeout
= 30;
1169 #if defined(HAVE_CGI) || defined(HAVE_LUA) || defined(HAVE_UBUS)
1170 /* default script timeout */
1171 if (conf
.script_timeout
<= 0)
1172 conf
.script_timeout
= 60;
1176 /* default cgi prefix */
1177 if (!conf
.cgi_prefix
)
1178 conf
.cgi_prefix
= "/cgi-bin";
1182 /* load Lua plugin */
1183 if (!(lib
= dlopen("uhttpd_lua.so", RTLD_LAZY
| RTLD_GLOBAL
)))
1186 "Notice: Unable to load Lua plugin - disabling Lua support! "
1187 "(Reason: %s)\n", dlerror());
1191 /* resolve functions */
1192 if (!(conf
.lua_init
= dlsym(lib
, "uh_lua_init")) ||
1193 !(conf
.lua_close
= dlsym(lib
, "uh_lua_close")) ||
1194 !(conf
.lua_request
= dlsym(lib
, "uh_lua_request")))
1197 "Error: Failed to lookup required symbols "
1198 "in Lua plugin: %s\n", dlerror()
1203 /* init Lua runtime if handler is specified */
1204 if (conf
.lua_handler
)
1206 /* default lua prefix */
1207 if (!conf
.lua_prefix
)
1208 conf
.lua_prefix
= "/lua";
1210 conf
.lua_state
= conf
.lua_init(&conf
);
1216 /* load ubus plugin */
1217 if (!(lib
= dlopen("uhttpd_ubus.so", RTLD_LAZY
| RTLD_GLOBAL
)))
1220 "Notice: Unable to load ubus plugin - disabling ubus support! "
1221 "(Reason: %s)\n", dlerror());
1223 else if (conf
.ubus_prefix
)
1225 /* resolve functions */
1226 if (!(conf
.ubus_init
= dlsym(lib
, "uh_ubus_init")) ||
1227 !(conf
.ubus_close
= dlsym(lib
, "uh_ubus_close")) ||
1228 !(conf
.ubus_request
= dlsym(lib
, "uh_ubus_request")))
1231 "Error: Failed to lookup required symbols "
1232 "in ubus plugin: %s\n", dlerror()
1237 /* initialize ubus */
1238 conf
.ubus_state
= conf
.ubus_init(&conf
);
1242 /* fork (if not disabled) */
1256 if ((cur_fd
= open("/dev/null", O_WRONLY
)) > -1)
1259 if ((cur_fd
= open("/dev/null", O_RDONLY
)) > -1)
1262 if ((cur_fd
= open("/dev/null", O_RDONLY
)) > -1)
1272 /* server main loop */
1276 /* destroy the Lua state */
1277 if (conf
.lua_state
!= NULL
)
1278 conf
.lua_close(conf
.lua_state
);
1282 /* destroy the ubus state */
1283 if (conf
.ubus_state
!= NULL
)
1284 conf
.ubus_close(conf
.ubus_state
);