2 * uhttpd - Tiny single-threaded httpd
4 * Copyright (C) 2010-2013 Jo-Philipp Wich <xm@subsignal.org>
5 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #ifndef _DEFAULT_SOURCE
21 # define _DEFAULT_SOURCE
26 #define _XOPEN_SOURCE 700
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
36 #include <libubox/usock.h>
37 #include <libubox/utils.h>
44 static int run_server(void)
48 uh_plugin_post_init();
54 static void uh_config_parse(void)
56 const char *path
= conf
.file
;
64 path
= "/etc/httpd.conf";
70 memset(line
, 0, sizeof(line
));
72 while (fgets(line
, sizeof(line
) - 1, c
)) {
73 if ((line
[0] == '/') && (strchr(line
, ':') != NULL
)) {
74 if (!(col1
= strchr(line
, ':')) || (*col1
++ = 0) ||
75 !(col2
= strchr(col1
, ':')) || (*col2
++ = 0) ||
76 !(eol
= strchr(col2
, '\n')) || (*eol
++ = 0))
79 uh_auth_add(line
, col1
, col2
);
80 } else if (!strncmp(line
, "I:", 2)) {
81 if (!(col1
= strchr(line
, ':')) || (*col1
++ = 0) ||
82 !(eol
= strchr(col1
, '\n')) || (*eol
++ = 0))
85 uh_index_add(strdup(col1
));
86 } else if (!strncmp(line
, "E404:", 5)) {
87 if (!(col1
= strchr(line
, ':')) || (*col1
++ = 0) ||
88 !(eol
= strchr(col1
, '\n')) || (*eol
++ = 0))
91 conf
.error_handler
= strdup(col1
);
93 else if ((line
[0] == '*') && (strchr(line
, ':') != NULL
)) {
94 if (!(col1
= strchr(line
, '*')) || (*col1
++ = 0) ||
95 !(col2
= strchr(col1
, ':')) || (*col2
++ = 0) ||
96 !(eol
= strchr(col2
, '\n')) || (*eol
++ = 0))
99 uh_interpreter_add(col1
, col2
);
106 static int add_listener_arg(char *arg
, bool tls
)
113 s
= strrchr(arg
, ':');
120 if (host
&& *host
== '[') {
128 return uh_socket_bind(host
, port
, tls
);
131 static int usage(const char *name
)
134 "Usage: %s -p [addr:]port -h docroot\n"
135 " -f Do not fork to background\n"
136 " -c file Configuration file, default is '/etc/httpd.conf'\n"
137 " -p [addr:]port Bind to specified address and port, multiple allowed\n"
139 " -s [addr:]port Like -p but provide HTTPS on this port\n"
140 " -C file ASN.1 server certificate file\n"
141 " -K file ASN.1 server private key file\n"
142 " -P ciphers Colon separated list of allowed TLS ciphers\n"
143 " -q Redirect all HTTP requests to HTTPS\n"
145 " -h directory Specify the document root, default is '.'\n"
146 " -E string Use given virtual URL as 404 error handler\n"
147 " -b string Use given charset for directory listings, default to UTF-8\n"
148 " -I string Use given filename as index for directories, multiple allowed\n"
149 " -S Do not follow symbolic links outside of the docroot\n"
150 " -D Do not allow directory listings, send 403 instead\n"
151 " -R Enable RFC1918 filter\n"
152 " -n count Maximum allowed number of concurrent script requests\n"
153 " -N count Maximum allowed number of concurrent connections\n"
155 " -l string URL prefix for Lua handler\n"
156 " -L file Path to Lua handler script, -l and -L may be repeated in pairs\n"
159 " -o string URL prefix for ucode handler\n"
160 " -O file Path to ucode handler script, -o and -O may be repeated in pairs\n"
163 " -u string URL prefix for UBUS via JSON-RPC handler\n"
164 " -U file Override ubus socket path\n"
165 " -a Do not authenticate JSON-RPC requests against UBUS session api\n"
166 " -X Enable CORS HTTP headers on JSON-RPC api\n"
167 " -e Events subscription reconnection time (retry value)\n"
169 " -x string URL prefix for CGI handler, default is '/cgi-bin'\n"
170 " -y alias[=path] URL alias handle\n"
171 " -i .ext=path Use interpreter at path for files with the given extension\n"
172 " -t seconds CGI, Lua and UBUS script timeout in seconds, default is 60\n"
173 " -T seconds Network timeout in seconds, default is 30\n"
174 " -k seconds HTTP keepalive timeout\n"
175 " -A seconds TCP keepalive timeout, default is unset\n"
176 " -d string URL decode given string\n"
177 " -r string Specify basic auth realm\n"
178 " -m string MD5 crypt given string\n"
184 static void init_defaults_pre(void)
186 conf
.script_timeout
= 60;
187 conf
.network_timeout
= 30;
188 conf
.http_keepalive
= 20;
189 conf
.max_script_requests
= 3;
190 conf
.max_connections
= 100;
191 conf
.realm
= "Protected Area";
192 conf
.cgi_prefix
= "/cgi-bin";
193 conf
.cgi_path
= "/sbin:/usr/sbin:/bin:/usr/bin";
194 INIT_LIST_HEAD(&conf
.cgi_alias
);
195 INIT_LIST_HEAD(&conf
.lua_prefix
);
197 INIT_LIST_HEAD(&conf
.ucode_prefix
);
201 static void init_defaults_post(void)
203 uh_index_add("index.html");
204 uh_index_add("index.htm");
205 uh_index_add("default.html");
206 uh_index_add("default.htm");
208 if (conf
.cgi_prefix
) {
209 char *str
= malloc(strlen(conf
.docroot
) + strlen(conf
.cgi_prefix
) + 1);
211 strcpy(str
, conf
.docroot
);
212 strcat(str
, conf
.cgi_prefix
);
213 conf
.cgi_docroot_path
= str
;
214 conf
.cgi_prefix_len
= strlen(conf
.cgi_prefix
);
218 static void fixup_prefix(char *str
)
225 len
= strlen(str
) - 1;
227 while (len
>= 0 && str
[len
] == '/')
234 static void add_lua_prefix(const char *prefix
, const char *handler
) {
235 struct lua_prefix
*p
;
236 char *pprefix
, *phandler
;
238 p
= calloc_a(sizeof(*p
),
239 &pprefix
, strlen(prefix
) + 1,
240 &phandler
, strlen(handler
) + 1);
245 p
->prefix
= strcpy(pprefix
, prefix
);
246 p
->handler
= strcpy(phandler
, handler
);
248 list_add_tail(&p
->list
, &conf
.lua_prefix
);
253 static void add_ucode_prefix(const char *prefix
, const char *handler
) {
254 struct ucode_prefix
*p
;
255 char *pprefix
, *phandler
;
257 p
= calloc_a(sizeof(*p
),
258 &pprefix
, strlen(prefix
) + 1,
259 &phandler
, strlen(handler
) + 1);
264 p
->prefix
= strcpy(pprefix
, prefix
);
265 p
->handler
= strcpy(phandler
, handler
);
267 list_add_tail(&p
->list
, &conf
.ucode_prefix
);
271 int main(int argc
, char **argv
)
281 const char *tls_key
= NULL
, *tls_crt
= NULL
, *tls_ciphers
= NULL
;
284 const char *lua_prefix
= NULL
, *lua_handler
= NULL
;
287 const char *ucode_prefix
= NULL
, *ucode_handler
= NULL
;
290 BUILD_BUG_ON(sizeof(uh_buf
) < PATH_MAX
);
292 uh_dispatch_add(&cgi_dispatch
);
294 signal(SIGPIPE
, SIG_IGN
);
296 while ((ch
= getopt(argc
, argv
, "A:ab:C:c:Dd:E:e:fh:H:I:i:K:k:L:l:m:N:n:O:o:P:p:qRr:Ss:T:t:U:u:Xx:y:")) != -1) {
308 tls_ciphers
= optarg
;
312 conf
.tls_redirect
= 1;
324 fprintf(stderr
, "uhttpd: TLS support not compiled, "
325 "ignoring -%c\n", ch
);
329 optarg
= strdup(optarg
);
330 bound
+= add_listener_arg(optarg
, (ch
== 's'));
335 if (!realpath(optarg
, uh_buf
)) {
336 fprintf(stderr
, "Error: Invalid directory %s: %s\n",
337 optarg
, strerror(errno
));
340 conf
.docroot
= strdup(uh_buf
);
344 if (uh_handler_add(optarg
)) {
345 fprintf(stderr
, "Error: Failed to load handler script %s\n",
352 if (optarg
[0] != '/') {
353 fprintf(stderr
, "Error: Invalid error handler: %s\n",
357 conf
.error_handler
= optarg
;
361 if (optarg
[0] == '/') {
362 fprintf(stderr
, "Error: Invalid index page: %s\n",
366 uh_index_add(optarg
);
370 conf
.dirlist_charset
= optarg
;
374 conf
.no_symlinks
= 1;
378 conf
.no_dirlists
= 1;
382 conf
.rfc1918_filter
= 1;
386 conf
.max_script_requests
= atoi(optarg
);
390 conf
.max_connections
= atoi(optarg
);
394 fixup_prefix(optarg
);
395 conf
.cgi_prefix
= optarg
;
399 alias
= calloc(1, sizeof(*alias
));
401 fprintf(stderr
, "Error: failed to allocate alias\n");
404 alias
->alias
= strdup(optarg
);
405 alias
->path
= strchr(alias
->alias
, '=');
408 list_add(&alias
->list
, &conf
.cgi_alias
);
412 optarg
= strdup(optarg
);
413 port
= strchr(optarg
, '=');
414 if (optarg
[0] != '.' || !port
) {
415 fprintf(stderr
, "Error: Invalid interpreter: %s\n",
421 uh_interpreter_add(optarg
, port
);
425 conf
.script_timeout
= atoi(optarg
);
429 conf
.network_timeout
= atoi(optarg
);
433 conf
.http_keepalive
= atoi(optarg
);
437 conf
.tcp_keepalive
= atoi(optarg
);
445 optarg
= strdup(optarg
);
446 port
= alloca(strlen(optarg
) + 1);
450 /* "decode" plus to space to retain compat */
451 for (opt
= 0; optarg
[opt
]; opt
++)
452 if (optarg
[opt
] == '+')
455 /* opt now contains strlen(optarg) -- no need to re-scan */
456 if (uh_urldecode(port
, opt
, optarg
, opt
) < 0) {
457 fprintf(stderr
, "uhttpd: invalid encoding\n");
465 /* basic auth realm */
472 printf("%s\n", crypt(optarg
, "$1$"));
486 fprintf(stderr
, "uhttpd: Ignoring previous -%c %s\n",
493 fprintf(stderr
, "uhttpd: Ignoring previous -%c %s\n",
496 lua_handler
= optarg
;
499 if (lua_prefix
&& lua_handler
) {
500 add_lua_prefix(lua_prefix
, lua_handler
);
509 fprintf(stderr
, "uhttpd: Lua support not compiled, "
510 "ignoring -%c\n", ch
);
518 fprintf(stderr
, "uhttpd: Ignoring previous -%c %s\n",
521 ucode_prefix
= optarg
;
525 fprintf(stderr
, "uhttpd: Ignoring previous -%c %s\n",
528 ucode_handler
= optarg
;
531 if (ucode_prefix
&& ucode_handler
) {
532 add_ucode_prefix(ucode_prefix
, ucode_handler
);
534 ucode_handler
= NULL
;
541 fprintf(stderr
, "uhttpd: ucode support not compiled, "
542 "ignoring -%c\n", ch
);
547 conf
.ubus_noauth
= 1;
551 conf
.ubus_prefix
= optarg
;
555 conf
.ubus_socket
= optarg
;
563 conf
.events_retry
= atoi(optarg
);
571 fprintf(stderr
, "uhttpd: UBUS support not compiled, "
572 "ignoring -%c\n", ch
);
576 return usage(argv
[0]);
583 if (!realpath(".", uh_buf
)) {
584 fprintf(stderr
, "Error: Unable to determine work dir\n");
587 conf
.docroot
= strdup(uh_buf
);
590 init_defaults_post();
593 fprintf(stderr
, "Error: No sockets bound, unable to continue\n");
599 if (!tls_crt
|| !tls_key
) {
600 fprintf(stderr
, "Please specify a certificate and "
601 "a key file to enable SSL support\n");
605 if (uh_tls_init(tls_key
, tls_crt
, tls_ciphers
))
611 if (lua_handler
|| lua_prefix
) {
612 fprintf(stderr
, "Need handler and prefix to enable Lua support\n");
616 if (!list_empty(&conf
.lua_prefix
) && uh_plugin_init("uhttpd_lua.so"))
620 if (ucode_handler
|| ucode_prefix
) {
621 fprintf(stderr
, "Need handler and prefix to enable ucode support\n");
625 if (!list_empty(&conf
.ucode_prefix
) && uh_plugin_init("uhttpd_ucode.so"))
629 if (conf
.ubus_prefix
&& uh_plugin_init("uhttpd_ubus.so"))
633 /* fork (if not disabled) */
645 cur_fd
= open("/dev/null", O_WRONLY
);