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 #define _DEFAULT_SOURCE
23 #define _XOPEN_SOURCE 700
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
33 #include <libubox/usock.h>
40 static int run_server(void)
44 uh_plugin_post_init();
50 static void uh_config_parse(void)
52 const char *path = conf.file;
60 path = "/etc/httpd.conf";
66 memset(line, 0, sizeof(line));
68 while (fgets(line, sizeof(line) - 1, c)) {
69 if ((line[0] == '/') && (strchr(line, ':') != NULL)) {
70 if (!(col1 = strchr(line, ':')) || (*col1++ = 0) ||
71 !(col2 = strchr(col1, ':')) || (*col2++ = 0) ||
72 !(eol = strchr(col2, '\n')) || (*eol++ = 0))
75 uh_auth_add(line, col1, col2);
76 } else if (!strncmp(line, "I:", 2)) {
77 if (!(col1 = strchr(line, ':')) || (*col1++ = 0) ||
78 !(eol = strchr(col1, '\n')) || (*eol++ = 0))
81 uh_index_add(strdup(col1));
82 } else if (!strncmp(line, "E404:", 5)) {
83 if (!(col1 = strchr(line, ':')) || (*col1++ = 0) ||
84 !(eol = strchr(col1, '\n')) || (*eol++ = 0))
87 conf.error_handler = strdup(col1);
89 else if ((line[0] == '*') && (strchr(line, ':') != NULL)) {
90 if (!(col1 = strchr(line, '*')) || (*col1++ = 0) ||
91 !(col2 = strchr(col1, ':')) || (*col2++ = 0) ||
92 !(eol = strchr(col2, '\n')) || (*eol++ = 0))
95 uh_interpreter_add(col1, col2);
102 static int add_listener_arg(char *arg, bool tls)
109 s = strrchr(arg, ':');
116 if (host && *host == '[') {
124 return uh_socket_bind(host, port, tls);
127 static int usage(const char *name)
130 "Usage: %s -p [addr:]port -h docroot\n"
131 " -f Do not fork to background\n"
132 " -c file Configuration file, default is '/etc/httpd.conf'\n"
133 " -p [addr:]port Bind to specified address and port, multiple allowed\n"
135 " -s [addr:]port Like -p but provide HTTPS on this port\n"
136 " -C file ASN.1 server certificate file\n"
137 " -K file ASN.1 server private key file\n"
138 " -q Redirect all HTTP requests to HTTPS\n"
140 " -h directory Specify the document root, default is '.'\n"
141 " -E string Use given virtual URL as 404 error handler\n"
142 " -I string Use given filename as index for directories, multiple allowed\n"
143 " -S Do not follow symbolic links outside of the docroot\n"
144 " -D Do not allow directory listings, send 403 instead\n"
145 " -R Enable RFC1918 filter\n"
146 " -n count Maximum allowed number of concurrent script requests\n"
147 " -N count Maximum allowed number of concurrent connections\n"
149 " -l string URL prefix for Lua handler, default is '/lua'\n"
150 " -L file Lua handler script, omit to disable Lua\n"
153 " -u string URL prefix for UBUS via JSON-RPC handler\n"
154 " -U file Override ubus socket path\n"
155 " -a Do not authenticate JSON-RPC requests against UBUS session api\n"
156 " -X Enable CORS HTTP headers on JSON-RPC api\n"
158 " -x string URL prefix for CGI handler, default is '/cgi-bin'\n"
159 " -y alias[=path] URL alias handle\n"
160 " -i .ext=path Use interpreter at path for files with the given extension\n"
161 " -t seconds CGI, Lua and UBUS script timeout in seconds, default is 60\n"
162 " -T seconds Network timeout in seconds, default is 30\n"
163 " -k seconds HTTP keepalive timeout\n"
164 " -d string URL decode given string\n"
165 " -r string Specify basic auth realm\n"
166 " -m string MD5 crypt given string\n"
172 static void init_defaults_pre(void)
174 conf.script_timeout = 60;
175 conf.network_timeout = 30;
176 conf.http_keepalive = 20;
177 conf.max_script_requests = 3;
178 conf.max_connections = 100;
179 conf.realm = "Protected Area";
180 conf.cgi_prefix = "/cgi-bin";
181 conf.cgi_path = "/sbin:/usr/sbin:/bin:/usr/bin";
182 INIT_LIST_HEAD(&conf.cgi_alias);
185 static void init_defaults_post(void)
187 uh_index_add("index.html");
188 uh_index_add("index.htm");
189 uh_index_add("default.html");
190 uh_index_add("default.htm");
192 if (conf.cgi_prefix) {
193 char *str = malloc(strlen(conf.docroot) + strlen(conf.cgi_prefix) + 1);
195 strcpy(str, conf.docroot);
196 strcat(str, conf.cgi_prefix);
197 conf.cgi_docroot_path = str;
198 conf.cgi_prefix_len = strlen(conf.cgi_prefix);
202 static void fixup_prefix(char *str)
209 len = strlen(str) - 1;
211 while (len >= 0 && str[len] == '/')
217 int main(int argc, char **argv)
227 const char *tls_key = NULL, *tls_crt = NULL;
230 BUILD_BUG_ON(sizeof(uh_buf) < PATH_MAX);
232 uh_dispatch_add(&cgi_dispatch);
234 signal(SIGPIPE, SIG_IGN);
236 while ((ch = getopt(argc, argv, "A:aC:c:Dd:E:fh:H:I:i:K:k:L:l:m:N:n:p:qRr:Ss:T:t:U:u:Xx:y:")) != -1) {
248 conf.tls_redirect = 1;
259 fprintf(stderr, "uhttpd: TLS support not compiled, "
260 "ignoring -%c\n", ch);
264 optarg = strdup(optarg);
265 bound += add_listener_arg(optarg, (ch == 's'));
269 if (!realpath(optarg, uh_buf)) {
270 fprintf(stderr, "Error: Invalid directory %s: %s\n",
271 optarg, strerror(errno));
274 conf.docroot = strdup(uh_buf);
278 if (uh_handler_add(optarg)) {
279 fprintf(stderr, "Error: Failed to load handler script %s\n",
286 if (optarg[0] != '/') {
287 fprintf(stderr, "Error: Invalid error handler: %s\n",
291 conf.error_handler = optarg;
295 if (optarg[0] == '/') {
296 fprintf(stderr, "Error: Invalid index page: %s\n",
300 uh_index_add(optarg);
304 conf.no_symlinks = 1;
308 conf.no_dirlists = 1;
312 conf.rfc1918_filter = 1;
316 conf.max_script_requests = atoi(optarg);
320 conf.max_connections = atoi(optarg);
324 fixup_prefix(optarg);
325 conf.cgi_prefix = optarg;
329 alias = calloc(1, sizeof(*alias));
331 fprintf(stderr, "Error: failed to allocate alias\n");
334 alias->alias = strdup(optarg);
335 alias->path = strchr(alias->alias, '=');
338 list_add(&alias->list, &conf.cgi_alias);
342 optarg = strdup(optarg);
343 port = strchr(optarg, '=');
344 if (optarg[0] != '.' || !port) {
345 fprintf(stderr, "Error: Invalid interpreter: %s\n",
351 uh_interpreter_add(optarg, port);
355 conf.script_timeout = atoi(optarg);
359 conf.network_timeout = atoi(optarg);
363 conf.http_keepalive = atoi(optarg);
367 conf.tcp_keepalive = atoi(optarg);
375 optarg = strdup(optarg);
376 port = alloca(strlen(optarg) + 1);
380 /* "decode" plus to space to retain compat */
381 for (opt = 0; optarg[opt]; opt++)
382 if (optarg[opt] == '+')
385 /* opt now contains strlen(optarg) -- no need to re-scan */
386 if (uh_urldecode(port, opt, optarg, opt) < 0) {
387 fprintf(stderr, "uhttpd: invalid encoding\n");
395 /* basic auth realm */
402 printf("%s\n", crypt(optarg, "$1$"));
413 conf.lua_prefix = optarg;
417 conf.lua_handler = optarg;
422 fprintf(stderr, "uhttpd: Lua support not compiled, "
423 "ignoring -%c\n", ch);
428 conf.ubus_noauth = 1;
432 conf.ubus_prefix = optarg;
436 conf.ubus_socket = optarg;
447 fprintf(stderr, "uhttpd: UBUS support not compiled, "
448 "ignoring -%c\n", ch);
452 return usage(argv[0]);
459 if (!realpath(".", uh_buf)) {
460 fprintf(stderr, "Error: Unable to determine work dir\n");
463 conf.docroot = strdup(uh_buf);
466 init_defaults_post();
469 fprintf(stderr, "Error: No sockets bound, unable to continue\n");
475 if (!tls_crt || !tls_key) {
476 fprintf(stderr, "Please specify a certificate and "
477 "a key file to enable SSL support\n");
481 if (uh_tls_init(tls_key, tls_crt))
487 if (conf.lua_handler || conf.lua_prefix) {
488 if (!conf.lua_handler || !conf.lua_prefix) {
489 fprintf(stderr, "Need handler and prefix to enable Lua support\n");
492 if (uh_plugin_init("uhttpd_lua.so"))
497 if (conf.ubus_prefix && uh_plugin_init("uhttpd_ubus.so"))
501 /* fork (if not disabled) */
513 cur_fd = open("/dev/null", O_WRONLY);