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.
22 #define _XOPEN_SOURCE 700
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
32 #include <libubox/usock.h>
39 static int run_server(void)
43 uh_plugin_post_init();
49 static void uh_config_parse(void)
51 const char *path
= conf
.file
;
59 path
= "/etc/httpd.conf";
65 memset(line
, 0, sizeof(line
));
67 while (fgets(line
, sizeof(line
) - 1, c
)) {
68 if ((line
[0] == '/') && (strchr(line
, ':') != NULL
)) {
69 if (!(col1
= strchr(line
, ':')) || (*col1
++ = 0) ||
70 !(col2
= strchr(col1
, ':')) || (*col2
++ = 0) ||
71 !(eol
= strchr(col2
, '\n')) || (*eol
++ = 0))
74 uh_auth_add(line
, col1
, col2
);
75 } else if (!strncmp(line
, "I:", 2)) {
76 if (!(col1
= strchr(line
, ':')) || (*col1
++ = 0) ||
77 !(eol
= strchr(col1
, '\n')) || (*eol
++ = 0))
80 uh_index_add(strdup(col1
));
81 } else if (!strncmp(line
, "E404:", 5)) {
82 if (!(col1
= strchr(line
, ':')) || (*col1
++ = 0) ||
83 !(eol
= strchr(col1
, '\n')) || (*eol
++ = 0))
86 conf
.error_handler
= strdup(col1
);
88 else if ((line
[0] == '*') && (strchr(line
, ':') != NULL
)) {
89 if (!(col1
= strchr(line
, '*')) || (*col1
++ = 0) ||
90 !(col2
= strchr(col1
, ':')) || (*col2
++ = 0) ||
91 !(eol
= strchr(col2
, '\n')) || (*eol
++ = 0))
94 uh_interpreter_add(col1
, col2
);
101 static int add_listener_arg(char *arg
, bool tls
)
108 s
= strrchr(arg
, ':');
115 if (host
&& *host
== '[') {
123 return uh_socket_bind(host
, port
, tls
);
126 static int usage(const char *name
)
129 "Usage: %s -p [addr:]port -h docroot\n"
130 " -f Do not fork to background\n"
131 " -c file Configuration file, default is '/etc/httpd.conf'\n"
132 " -p [addr:]port Bind to specified address and port, multiple allowed\n"
134 " -s [addr:]port Like -p but provide HTTPS on this port\n"
135 " -C file ASN.1 server certificate file\n"
136 " -K file ASN.1 server private key file\n"
138 " -h directory Specify the document root, default is '.'\n"
139 " -E string Use given virtual URL as 404 error handler\n"
140 " -I string Use given filename as index for directories, multiple allowed\n"
141 " -S Do not follow symbolic links outside of the docroot\n"
142 " -D Do not allow directory listings, send 403 instead\n"
143 " -R Enable RFC1918 filter\n"
144 " -n count Maximum allowed number of concurrent script requests\n"
145 " -N count Maximum allowed number of concurrent connections\n"
147 " -l string URL prefix for Lua handler, default is '/lua'\n"
148 " -L file Lua handler script, omit to disable Lua\n"
151 " -u string URL prefix for UBUS via JSON-RPC handler\n"
152 " -U file Override ubus socket path\n"
153 " -a Do not authenticate JSON-RPC requests against UBUS session api\n"
154 " -X Enable CORS HTTP headers on JSON-RPC api\n"
156 " -x string URL prefix for CGI handler, default is '/cgi-bin'\n"
157 " -i .ext=path Use interpreter at path for files with the given extension\n"
158 " -t seconds CGI, Lua and UBUS script timeout in seconds, default is 60\n"
159 " -T seconds Network timeout in seconds, default is 30\n"
160 " -k seconds HTTP keepalive timeout\n"
161 " -d string URL decode given string\n"
162 " -r string Specify basic auth realm\n"
163 " -m string MD5 crypt given string\n"
169 static void init_defaults_pre(void)
171 conf
.script_timeout
= 60;
172 conf
.network_timeout
= 30;
173 conf
.http_keepalive
= 20;
174 conf
.max_script_requests
= 3;
175 conf
.max_connections
= 100;
176 conf
.realm
= "Protected Area";
177 conf
.cgi_prefix
= "/cgi-bin";
178 conf
.cgi_path
= "/sbin:/usr/sbin:/bin:/usr/bin";
181 static void init_defaults_post(void)
183 uh_index_add("index.html");
184 uh_index_add("index.htm");
185 uh_index_add("default.html");
186 uh_index_add("default.htm");
188 if (conf
.cgi_prefix
) {
189 char *str
= malloc(strlen(conf
.docroot
) + strlen(conf
.cgi_prefix
) + 1);
190 strcpy(str
, conf
.docroot
);
191 strcat(str
, conf
.cgi_prefix
);
192 conf
.cgi_docroot_path
= str
;
196 static void fixup_prefix(char *str
)
203 len
= strlen(str
) - 1;
205 while (len
>= 0 && str
[len
] == '/')
211 int main(int argc
, char **argv
)
221 const char *tls_key
= NULL
, *tls_crt
= NULL
;
224 BUILD_BUG_ON(sizeof(uh_buf
) < PATH_MAX
);
226 uh_dispatch_add(&cgi_dispatch
);
228 signal(SIGPIPE
, SIG_IGN
);
230 while ((ch
= getopt(argc
, argv
, "afSDRXC:K:E:I:p:s:h:c:l:L:d:r:m:n:N:x:i:t:k:T:A:u:U:")) != -1) {
248 fprintf(stderr
, "uhttpd: TLS support not compiled, "
249 "ignoring -%c\n", ch
);
253 optarg
= strdup(optarg
);
254 bound
+= add_listener_arg(optarg
, (ch
== 's'));
258 if (!realpath(optarg
, uh_buf
)) {
259 fprintf(stderr
, "Error: Invalid directory %s: %s\n",
260 optarg
, strerror(errno
));
263 conf
.docroot
= strdup(uh_buf
);
267 if (optarg
[0] != '/') {
268 fprintf(stderr
, "Error: Invalid error handler: %s\n",
272 conf
.error_handler
= optarg
;
276 if (optarg
[0] == '/') {
277 fprintf(stderr
, "Error: Invalid index page: %s\n",
281 uh_index_add(optarg
);
285 conf
.no_symlinks
= 1;
289 conf
.no_dirlists
= 1;
293 conf
.rfc1918_filter
= 1;
297 conf
.max_script_requests
= atoi(optarg
);
301 conf
.max_connections
= atoi(optarg
);
305 fixup_prefix(optarg
);
306 conf
.cgi_prefix
= optarg
;
310 optarg
= strdup(optarg
);
311 port
= strchr(optarg
, '=');
312 if (optarg
[0] != '.' || !port
) {
313 fprintf(stderr
, "Error: Invalid interpreter: %s\n",
319 uh_interpreter_add(optarg
, port
);
323 conf
.script_timeout
= atoi(optarg
);
327 conf
.network_timeout
= atoi(optarg
);
331 conf
.http_keepalive
= atoi(optarg
);
335 conf
.tcp_keepalive
= atoi(optarg
);
343 optarg
= strdup(optarg
);
344 port
= alloca(strlen(optarg
) + 1);
348 /* "decode" plus to space to retain compat */
349 for (opt
= 0; optarg
[opt
]; opt
++)
350 if (optarg
[opt
] == '+')
353 /* opt now contains strlen(optarg) -- no need to re-scan */
354 if (uh_urldecode(port
, opt
, optarg
, opt
) < 0) {
355 fprintf(stderr
, "uhttpd: invalid encoding\n");
363 /* basic auth realm */
370 printf("%s\n", crypt(optarg
, "$1$"));
381 conf
.lua_prefix
= optarg
;
385 conf
.lua_handler
= optarg
;
390 fprintf(stderr
, "uhttpd: Lua support not compiled, "
391 "ignoring -%c\n", ch
);
396 conf
.ubus_noauth
= 1;
400 conf
.ubus_prefix
= optarg
;
404 conf
.ubus_socket
= optarg
;
415 fprintf(stderr
, "uhttpd: UBUS support not compiled, "
416 "ignoring -%c\n", ch
);
420 return usage(argv
[0]);
427 if (!realpath(".", uh_buf
)) {
428 fprintf(stderr
, "Error: Unable to determine work dir\n");
431 conf
.docroot
= strdup(uh_buf
);
434 init_defaults_post();
437 fprintf(stderr
, "Error: No sockets bound, unable to continue\n");
443 if (!tls_crt
|| !tls_key
) {
444 fprintf(stderr
, "Please specify a certificate and "
445 "a key file to enable SSL support\n");
449 if (uh_tls_init(tls_key
, tls_crt
))
455 if (conf
.lua_handler
|| conf
.lua_prefix
) {
456 if (!conf
.lua_handler
|| !conf
.lua_prefix
) {
457 fprintf(stderr
, "Need handler and prefix to enable Lua support\n");
460 if (uh_plugin_init("uhttpd_lua.so"))
465 if (conf
.ubus_prefix
&& uh_plugin_init("uhttpd_ubus.so"))
469 /* fork (if not disabled) */
481 cur_fd
= open("/dev/null", O_WRONLY
);