2 * uhttpd - Tiny single-threaded httpd
4 * Copyright (C) 2010-2012 Jo-Philipp Wich <xm@subsignal.org>
5 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
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)
48 static void uh_config_parse(void)
50 const char *path = conf.file;
58 path = "/etc/httpd.conf";
64 memset(line, 0, sizeof(line));
66 while (fgets(line, sizeof(line) - 1, c)) {
67 if ((line[0] == '/') && (strchr(line, ':') != NULL)) {
68 if (!(col1 = strchr(line, ':')) || (*col1++ = 0) ||
69 !(col2 = strchr(col1, ':')) || (*col2++ = 0) ||
70 !(eol = strchr(col2, '\n')) || (*eol++ = 0))
73 uh_auth_add(line, col1, col2);
74 } else if (!strncmp(line, "I:", 2)) {
75 if (!(col1 = strchr(line, ':')) || (*col1++ = 0) ||
76 !(eol = strchr(col1, '\n')) || (*eol++ = 0))
79 uh_index_add(strdup(col1));
80 } else if (!strncmp(line, "E404:", 5)) {
81 if (!(col1 = strchr(line, ':')) || (*col1++ = 0) ||
82 !(eol = strchr(col1, '\n')) || (*eol++ = 0))
85 conf.error_handler = strdup(col1);
87 else if ((line[0] == '*') && (strchr(line, ':') != NULL)) {
88 if (!(col1 = strchr(line, '*')) || (*col1++ = 0) ||
89 !(col2 = strchr(col1, ':')) || (*col2++ = 0) ||
90 !(eol = strchr(col2, '\n')) || (*eol++ = 0))
93 uh_interpreter_add(col1, col2);
100 static int add_listener_arg(char *arg, bool tls)
106 s = strrchr(arg, ':');
113 return uh_socket_bind(host, port, tls);
116 static int usage(const char *name)
119 "Usage: %s -p [addr:]port -h docroot\n"
120 " -f Do not fork to background\n"
121 " -c file Configuration file, default is '/etc/httpd.conf'\n"
122 " -p [addr:]port Bind to specified address and port, multiple allowed\n"
124 " -s [addr:]port Like -p but provide HTTPS on this port\n"
125 " -C file ASN.1 server certificate file\n"
126 " -K file ASN.1 server private key file\n"
128 " -h directory Specify the document root, default is '.'\n"
129 " -E string Use given virtual URL as 404 error handler\n"
130 " -I string Use given filename as index for directories, multiple allowed\n"
131 " -S Do not follow symbolic links outside of the docroot\n"
132 " -D Do not allow directory listings, send 403 instead\n"
133 " -R Enable RFC1918 filter\n"
134 " -n count Maximum allowed number of concurrent requests\n"
136 " -l string URL prefix for Lua handler, default is '/lua'\n"
137 " -L file Lua handler script, omit to disable Lua\n"
140 " -u string URL prefix for HTTP/JSON handler\n"
141 " -U file Override ubus socket path\n"
143 " -x string URL prefix for CGI handler, default is '/cgi-bin'\n"
144 " -i .ext=path Use interpreter at path for files with the given extension\n"
145 " -t seconds CGI, Lua and UBUS script timeout in seconds, default is 60\n"
146 " -T seconds Network timeout in seconds, default is 30\n"
147 " -d string URL decode given string\n"
148 " -r string Specify basic auth realm\n"
149 " -m string MD5 crypt given string\n"
155 static void init_defaults(void)
157 conf.script_timeout = 60;
158 conf.network_timeout = 30;
159 conf.http_keepalive = 20;
160 conf.max_requests = 3;
161 conf.realm = "Protected Area";
162 conf.cgi_prefix = "/cgi-bin";
163 conf.cgi_path = "/sbin:/usr/sbin:/bin:/usr/bin";
165 uh_index_add("index.html");
166 uh_index_add("index.htm");
167 uh_index_add("default.html");
168 uh_index_add("default.htm");
171 static void fixup_prefix(char *str)
178 len = strlen(str) - 1;
180 while (len > 0 && str[len] == '/')
186 int main(int argc, char **argv)
188 const char *tls_key = NULL, *tls_crt = NULL;
196 BUILD_BUG_ON(sizeof(uh_buf) < PATH_MAX);
198 uh_dispatch_add(&cgi_dispatch);
200 signal(SIGPIPE, SIG_IGN);
202 while ((ch = getopt(argc, argv, "fSDRC:K:E:I:p:s:h:c:l:L:d:r:m:n:x:i:t:T:A:u:U:")) != -1) {
211 bound += add_listener_arg(optarg, tls);
215 if (!realpath(optarg, uh_buf)) {
216 fprintf(stderr, "Error: Invalid directory %s: %s\n",
217 optarg, strerror(errno));
220 conf.docroot = strdup(uh_buf);
224 if (optarg[0] != '/') {
225 fprintf(stderr, "Error: Invalid error handler: %s\n",
229 conf.error_handler = optarg;
233 if (optarg[0] == '/') {
234 fprintf(stderr, "Error: Invalid index page: %s\n",
238 uh_index_add(optarg);
242 conf.no_symlinks = 1;
246 conf.no_dirlists = 1;
250 conf.rfc1918_filter = 1;
254 conf.max_requests = atoi(optarg);
258 fixup_prefix(optarg);
259 conf.cgi_prefix = optarg;
263 port = strchr(optarg, '=');
264 if (optarg[0] != '.' || !port) {
265 fprintf(stderr, "Error: Invalid interpreter: %s\n",
271 uh_interpreter_add(optarg, port);
275 conf.script_timeout = atoi(optarg);
279 conf.network_timeout = atoi(optarg);
283 conf.tcp_keepalive = atoi(optarg);
291 port = alloca(strlen(optarg) + 1);
295 /* "decode" plus to space to retain compat */
296 for (opt = 0; optarg[opt]; opt++)
297 if (optarg[opt] == '+')
300 /* opt now contains strlen(optarg) -- no need to re-scan */
301 if (uh_urldecode(port, opt, optarg, opt) < 0) {
302 fprintf(stderr, "uhttpd: invalid encoding\n");
309 /* basic auth realm */
316 printf("%s\n", crypt(optarg, "$1$"));
334 conf.lua_prefix = optarg;
338 conf.lua_handler = optarg;
342 return usage(argv[0]);
349 fprintf(stderr, "Error: No sockets bound, unable to continue\n");
354 if (!tls_crt || !tls_key) {
355 fprintf(stderr, "Please specify a certificate and "
356 "a key file to enable SSL support\n");
361 if (uh_tls_init(tls_key, tls_crt))
364 fprintf(stderr, "Error: TLS support not compiled in.\n");
370 if (conf.lua_handler || conf.lua_prefix) {
371 if (!conf.lua_handler || !conf.lua_prefix) {
372 fprintf(stderr, "Need handler and prefix to enable Lua support\n");
375 if (uh_plugin_init("uhttpd_lua.so"))
380 /* fork (if not disabled) */
392 cur_fd = open("/dev/null", O_WRONLY);